Repository navigation
Fix LOGICAL_ERROR in join order optimizer for duplicate column names via merged expression - #110227
Conversation
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260713-080600 |
|
cc @vdimir @novikd — could you review this? It fixes a |
|
Workflow [PR], commit [4ce0bc6] Summary: ✅
AI ReviewSummaryThis PR fixes the reproduced Findings
Final VerdictLLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 29/29 (100.00%) · Uncovered code |
| if (child_join_step->getJoinSettings() == join_settings && join_steps_limit > 1 && allow_child_join_kind) | ||
| { | ||
| collectJoinGraphRelationHeadersForJoin(*node, join_steps_limit, join_settings, relation_headers); | ||
| collectJoinGraphRelationHeadersForJoin(*effective, join_steps_limit, join_settings, merge_expression_into_join, relation_headers); |
There was a problem hiding this comment.
collectJoinGraphRelationHeadersForJoin still does not mirror the real buildQueryGraph flattening path here. buildQueryGraph runs the per-join get_exposed_columns / "Column name clash detected between join sides" check before recursing, and if flattening a child join would expose overlapping names across that boundary it keeps that boundary collapsed instead of descending further. After this patch we now peel through non-passthrough ExpressionSteps as well, but the header collector still recurses unconditionally once join_steps_limit > 1, so it can inspect leaf headers that the optimizer would never expose and joinGraphHasOverlappingColumnNames will skip reordering for a plan that buildQueryGraph can still optimize safely. Please mirror the same side-clash suppression in this helper before descending, otherwise the new expression-step peel widens the false-positive surface into a user-visible join-order optimization regression.
There was a problem hiding this comment.
Agreed on the mechanism, and it is worth fixing: collectJoinGraphRelationHeadersForJoin has no
equivalent of the get_exposed_columns side-clash check, so it can descend where buildQueryGraph
keeps a boundary collapsed. The direction is a lost reordering, never a wrong result.
I am not adding the check in this PR, for three reasons.
-
The gap is pre-existing and untouched here.
buildQueryGraph, including the clash check and its
ownquery_plan_merge_expression_into_joinpeel insideget_exposed_columns, is byte-identical at
the merge base (3d31d8f) and at this head. The pre-PR collector already recursed on
join_steps_limit > 1with no clash check. The collector landed in2ddbf356499caa5(2026-06-04)
and the clash check in69f802454c23ee6(2026-06-11), and the mirror was never updated. -
On the peel axis this patch moves the collector toward the optimizer, not away from it.
get_exposed_columnsalready peeled non-passthrough expression steps under that setting before this
PR; the collector did not, and that undercount is what aborts the server. One more boundary class
does become reachable, so I will not claim the surface is untouched, but at those boundaries the
collector now observes the relation set the optimizer actually builds. -
A loss is not demonstrated, and it is not easy to construct. The clash check compares the exposed
column sets of the peeled join's two children, which is close to what the collector collects, so
when the check fires the collector usually finds the same duplicate and bails identically. Losing a
reordering needs a cross-side clash at the inner boundary while the flattened leaf set has no
duplicate. Without such a query the requested regression test would pass in both arms and prove
nothing.
Adding the check without a demonstrated case would be a speculative change, and this PR fixes a
master server abort still recurring on the nightly SQLancer run (latest aa1a69b2, 2026-08-10).
Mirror fidelity deserves its own PR; I have noted it in the description rather than folding it in
here.
One note for whoever picks that up: buildQueryGraph cannot simply be reused as the predicate,
since it is destructive (detachExpressions, mergeInplace(std::move(...)), and
optimizeJoinLogicalImpl on children). That is why a read-only mirror exists, and keeping the two
in sync is the underlying issue.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 29/29 (100.00%) · Uncovered code |
CI finish ledger - 3a1e171Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
None is caused by this PR: the diff is confined to Session id: cron:our-pr-ci-monitor:20260810-173000 |
Correction to the ledger above (same head, 3a1e171)Two lines in the comment above are wrong, and both fail the same way: I gave them a fix-task owner
So all three failures on this head have the same cause: the branch is from 2026-07-13 and predates Session id: cron:our-pr-ci-monitor:20260810-173000 |
…via merged expression The pre-reorder guard joinGraphHasOverlappingColumnNames peeled only passthrough expression steps when flattening the join graph, while the optimizer's addChildQueryGraph also peels a non-passthrough expression step and flattens the child join underneath when query_plan_merge_expression_into_join is enabled. The guard therefore saw one relation fewer than the optimizer produced and missed a duplicated qualified column name, so reconstructing the reordered join with JoinExpressionActions threw 'Left and right columns have same names'. Make the guard peel expression steps the same way as addChildQueryGraph so it sees the same relation set the optimizer builds, and skip reordering when column names overlap. Co-Authored-By: Claude Opus 4.8 <[email protected]>
3a1e171 to
3a78b0a
Compare
|
Rebased onto master (
All three merge commits are now ancestors of the head, verified with I re-verified the fix on the rebased tree rather than assuming it survived, since master
The only content change since the last review is a shorter comment above the peel in |
CI finish ledger - 3a78b0aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task None of these is caused by this PR: the diff touches only
Session id: cron:our-pr-ci-monitor:20260811-013000 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 21 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger — a3733eeEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Note: this run was already attempt 2 and the Session id: cron:our-pr-ci-monitor:20260817-183000 |
Re-verified on today's master — this abort is still recurring on the nightlyThe The The generated case is the same shape this PR fixes — a comma join over a multi-table view: CREATE TABLE database2_t9 (c0 INT, c1 VARCHAR, PRIMARY KEY(c0));
CREATE TABLE database2_t0 (c0 VARCHAR(500), c1 BOOLEAN, PRIMARY KEY(c0));
CREATE VIEW database2_v0 (c0) AS
SELECT (NOT CAST((NOT database2_t9.c0) AS BOOLEAN)) FROM database2_t0, database2_t9 HAVING database2_t0.c1;
SELECT * FROM database2_t0, database2_v0, database2_t9 WHERE database2_t0.c1;Verified locally against master
So the two independent reports — the nightly and issue #114113 — are the same defect, and this The branch is |
…4752-593c Refresh a five-day-old branch and re-run CI: the previous run lost seven jobs in GitHub's `Set up job` step (codeload 429/502), so they never executed. `optimizeJoin.cpp` and `JoinExpressionActions.cpp` are byte-identical between master `6ae413899fc` and the tree the fix was last verified on, and the merged `optimizeJoin.cpp` is byte-identical to the built and tested file, so the Cascades work merged meanwhile (ClickHouse#86353) does not disturb this change. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Merged master ( Checked before pushing, since master touched
@vdimir your approval carried over; nothing to re-review. |
|
Hi — this looks like a backport candidate for |
|
@groeneai @vdimir @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
@vdimir could you apply
Binaries from builds.clickhouse.com. The setting defaults to 1 on 26.7, so it reproduces there with no Why only 26.7: the bug is the peel asymmetry between the guard and Controls: the master build containing the merge is clean (oracle works), and the reorder optimizer is confirmed live on 26.6/26.5/26.3 by a separate 3-table join fixture whose plan changes with Cherry-pick onto 26.7 is clean (
|
|
Correcting the "Known residual" sentence in the description above, because it reads more broadly than I measured. #116036, filed against this PR, is a real wrong-results bug in the same guard at default settings: a merged expression whose output name shadows a column of one of its own flattened inner relations. I confirmed it, and I also confirmed it is pre-existing rather than introduced here, by running the same arms on a pre-merge snapshot ( It is not the residual that sentence describes. That one was the missing Details and the per-release scope, including 26.7, are in #116036. I am picking up the fix and will link the PR there. |
Refusing to merge an expression whose output shadows its own input made 04516_join_reorder_duplicate_names_merge_expression stop reaching the merge-then-flatten path it was written for, while both of its oracles still passed. Its view projection is renamed so it no longer shadows an inner relation, and it now asserts that the view's inner relations really are flattened into the parent graph. Verified against a build with the guard from ClickHouse#110227 reverted: the renamed test still hits the original "Left and right columns have same names" logical error, so the coverage that test exists for is intact. 05038 gains three things its previous arms could not tell apart or did not reach: that the parent join is still reordered when the merge is refused, paired with a control where reordering is off; and a stored CREATE VIEW variant, which pushes a second expression step converting the subquery result to the view structure and therefore reaches the peel through a different plan shape than view() does. The stored view returns skewed values without the fix and equal values with it. The wrapper matrix behind the both-directions claim used idempotent shadowing expressions, which cannot show a double application in their values, so it is restated with non-idempotent ones per wrapper.
Closes: #114113
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
LOGICAL_ERROR(Left and right columns have same names) in the join order optimizer that could be triggered by a comma join over a multi-table view whenquery_plan_merge_expression_into_joinis enabled.Description
Found by the SQLancerPP nightly on master (server abort in the sanitizer build), and reported independently as issue #114113:
The pre-reorder guard
joinGraphHasOverlappingColumnNamesflattens the join graph to check for duplicate qualified column names and skips reordering when it finds any. It peeled only passthrough expression steps. The optimizer'saddChildQueryGraphalso peels a non-passthrough expression step and flattens the child join beneath it whenquery_plan_merge_expression_into_joinis enabled. So the guard saw one relation fewer than the optimizer produced and missed a duplicated name (__table3.c1); reconstructing the reordered join withJoinExpressionActionsthen threw the error.Fix: make the guard peel expression steps the same way as
addChildQueryGraph, so it inspects the same relation set the optimizer builds and correctly skips reordering when names overlap.Known residual, not addressed here: this guard is a read-only mirror of
buildQueryGraph, and it does not replicate theget_exposed_columnsside-clash suppression that keeps a join boundary collapsed. That predates this PR (the mirror landed in2ddbf356499caa5, the clash check a week later in69f802454c23ee6) and can only cost a reordering, never correctness. I have not found a query where the two disagree, so I have left it for a separate change rather than adding an unexercised check here.Workflow [PR]
Sync PR [sync-upstream/pr/110227]
Version info
26.8.1.1954(included in26.8and later)