Skip to content

Non-matched RIGHT JOIN row carries the outer SELECT's constant column (not defaulted) when the join is wrapped in a subquery #106923

Description

@qoega

Wrong result: constant column from the left side of a non-matched RIGHT JOIN is not defaulted when the query is wrapped in a subquery

Summary

For a RIGHT JOIN row with no left-side match, all left-side columns must be
filled with defaults (join_use_nulls = 0). A constant column from the
left subquery is filled correctly when the join is the top-level query, but
keeps its constant value when the same query is wrapped in
SELECT * FROM (...) — constant folding propagates the literal through the
outer-join nullability boundary.

Found by the AST-fuzzer correctness-oracle suite from PR
#99980 (Subquery wrap
oracle).

Minimal repro

No tables needed:

SELECT 1, * FROM (SELECT 2 AS x, 1) AS a RIGHT JOIN (SELECT 3 AS y) AS b ON y = x;
-- 1  0  0  3     (correct: both left-side columns defaulted on the non-matched row)

SELECT * FROM (SELECT 1, * FROM (SELECT 2 AS x, 1) AS a RIGHT JOIN (SELECT 3 AS y) AS b ON y = x);
-- 1  0  1  3     (WRONG: the left subquery's constant column `1` survives as 1)

The two queries must return identical rows. Reproduced on 26.6.1.1
(master 76c2edca2fd, release build) with clickhouse local, default
settings.

Fix area

Constant folding / projection of constant columns through RIGHT/FULL
join steps when the join is inside a subquery: the plan must materialize
(or default-fill) left-side constant columns for non-matched rows the same
way it does for non-constant ones. Likely in the analyzer/planner join
output-column handling (src/Planner/PlannerJoinTree.cpp or the logical
JOIN step's output header constant-ness), where wrapping in a subquery
changes whether the literal is still considered a constant at the outer
scope.

Original fuzzer hit (for reference)

2026-06-10 ~02:40Z, fuzz-lab cycle 9e instance 14 (TSan build), run dir
tmp/fuzz_lab/runs/20260610-0214-dd73eb7e3a9/inst/14/
(mismatch_artifacts/ holds the batch log + seed shard). Subquery wrap
oracle: reference vs wrapped rows differ in the constant column.

Activity

  1. qoega commented on Jun 10, 2026

    @qoega
    MemberAuthor

    @groeneai could you please reproduce and fix this one? Repro is in the issue body — deterministic on master 76c2edca2fd, default settings.

  2. added
    fuzz
    sqlancer
    on Jun 10, 2026
  3. groeneai commented on Jun 11, 2026

    @groeneai
    Collaborator

    Reproduced and fixed in #107127.

    Root cause: the planner addresses a subquery output column by source_alias + "." + column_name only, with no positional discriminator. When a subquery has two output columns with the same name (here the outer SELECT 1 literal colliding with the inner column also named 1, exposed via *), the outer SELECT * matcher resolves both to a single column identifier, so one value (the constant) overwrites the other. With a non-matched RIGHT JOIN row that meant the constant kept its literal instead of being defaulted.

    The fix gives a subquery's duplicate-named output columns distinct internal identifiers in the analyzer while keeping the original display names. The two wrapped/unwrapped queries now return identical rows. FULL JOIN behaves the same. Added regression test 04327_subquery_duplicate_column_name_constant_default.

  4. qoega commented on Jun 11, 2026

    @qoega
    MemberAuthor

    Update: the same defect reproduces with LEFT JOIN — a constant column from the right (non-matched) side survives wrapping instead of being defaulted:

    SELECT 1, * FROM (SELECT 2 AS x) AS a LEFT JOIN (SELECT 1, 3 AS y) AS b ON y = x;
    -- 1  2  0  0     (correct: right-side constant `1` and `y` defaulted on the non-matched row)
    
    SELECT * FROM (SELECT 1, * FROM (SELECT 2 AS x) AS a LEFT JOIN (SELECT 1, 3 AS y) AS b ON y = x);
    -- 1  2  1  0     (WRONG: the right-side constant `1` survives as 1)

    So the fix area covers constant columns from the non-preserved side of any outer join (LEFT/RIGHT/FULL), not just RIGHT. Reproduced on master f5d2d2434d2, default settings.

  5. groeneai commented on Jun 11, 2026

    @groeneai
    Collaborator

    @qoega confirmed: the LEFT JOIN case is already fixed by #107127 (the root cause is join-agnostic).

    Verified against the PR binary (default settings, enable_analyzer=1):

    -- unwrapped (correct baseline)
    SELECT 1, * FROM (SELECT 2 AS x) AS a LEFT JOIN (SELECT 1, 3 AS y) AS b ON y = x;
    -- 1  2  0  0
    
    -- wrapped (was 1 2 1 0 on master, now correct)
    SELECT * FROM (SELECT 1, * FROM (SELECT 2 AS x) AS a LEFT JOIN (SELECT 1, 3 AS y) AS b ON y = x);
    -- 1  2  0  0
    

    The bug is not specific to RIGHT JOIN: it is a duplicate-output-column-name collision at the subquery boundary (two columns both named 1, backed by different expressions). The fix disambiguates such columns in the analyzer, so it covers LEFT/RIGHT/FULL alike. I added your LEFT JOIN case to the 04327 regression test in the PR.

  6. added
    comp-joins
    on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-joinsfuzzsqlancer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions