You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Non-matched RIGHT JOIN row carries the outer SELECT's constant column (not defaulted) when the join is wrapped in a subquery #106923
comp-joinsJOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla...fuzzProblem found by one of the fuzzerssqlancerIssue found by SQLancer tool
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:
SELECT1, *FROM (SELECT2AS x, 1) AS a RIGHT JOIN (SELECT3AS y) AS b ON y = x;
-- 1 0 0 3 (correct: both left-side columns defaulted on the non-matched row)SELECT*FROM (SELECT1, *FROM (SELECT2AS x, 1) AS a RIGHT JOIN (SELECT3AS 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.
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.
Update: the same defect reproduces with LEFT JOIN — a constant column from the right (non-matched) side survives wrapping instead of being defaulted:
SELECT1, *FROM (SELECT2AS x) AS a LEFT JOIN (SELECT1, 3AS 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 (SELECT1, *FROM (SELECT2AS x) AS a LEFT JOIN (SELECT1, 3AS 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.
@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.
comp-joinsJOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla...fuzzProblem found by one of the fuzzerssqlancerIssue found by SQLancer tool
Activity
qoega commentedon Jun 10, 2026
@groeneai could you please reproduce and fix this one? Repro is in the issue body — deterministic on master
76c2edca2fd, default settings.groeneai commentedon Jun 11, 2026
Reproduced and fixed in #107127.
Root cause: the planner addresses a subquery output column by
source_alias + "." + column_nameonly, with no positional discriminator. When a subquery has two output columns with the same name (here the outerSELECT 1literal colliding with the inner column also named1, exposed via*), the outerSELECT *matcher resolves both to a single column identifier, so one value (the constant) overwrites the other. With a non-matchedRIGHT JOINrow 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.Document the COLUMNS(name) list-matcher contract for duplicate names
Skip hidden duplicate names in the unflatten-nested prefix resolver
qoega commentedon Jun 11, 2026
Update: the same defect reproduces with
LEFT JOIN— a constant column from the right (non-matched) side survives wrapping instead of being defaulted: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.groeneai commentedon Jun 11, 2026
@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):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.