Skip to content

Fix join-order conflict detector: strictness loss, join_use_nulls unsoundness, and cross-boundary planning failure - #118494

Merged
fkastrati merged 10 commits into
masterfrom
conflict-detector-dpsub-followup
Sep 8, 2026
Merged

fkastrati merged 10 commits into
masterfrom
conflict-detector-dpsub-followup

Conversation

@fkastrati

@fkastrati fkastrati commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Related: #118520
Related: #117553
Closes: #118520
Closes: #118413

Follow-up to the DPsub join-order conflict detectors (CD-A / CD-C) added in #117553. The settings are not in a released version yet, so these are pre-release fixes to that experimental feature. Fixes three defects surfaced by #118520 and by a differential SQL fuzz (dpsub+CD vs greedy vs the no-reorder baseline, which now agree on every query).

Settings renamed for clarity (renamed in place, since unreleased):

  • query_plan_optimize_join_order_use_cd_a_conflict_detector -> query_plan_optimize_join_order_use_conflict_detector_a
  • query_plan_optimize_join_order_use_cd_c_conflict_detector -> query_plan_optimize_join_order_use_conflict_detector_c

1. Strictness dropped to ALL. Enabling a detector turned ANY/SEMI/ANTI joins into ALL joins (rows multiplied, ANTI returned matched rows, NOT_IMPLEMENTED guards stopped firing). chooseJoinOrder re-stamped the graph's single strictness onto every reconstructed join, gated on the detector settings, but per-entry strictness is populated only when the detector actually reorders a semi/anti join. The fix keeps per-entry strictness iff the graph actually contains a non-All entry (a CD-reordered semi/anti join, which may be nested under an inner top join), otherwise stamps the uniform strictness (covers greedy, and a capped ANY join).

2. Unsound outer-join reorder under join_use_nulls = 0. The detectors' null-rejection analysis assumes an unmatched outer row is a real NULL. Under the default join_use_nulls = 0 the padding is a type default (0/''), so a null-rejecting predicate still matches the padded 0 and (t1 LEFT JOIN t2) LEFT JOIN t3 reordered to t1 LEFT JOIN (t2 LEFT JOIN t3) changed the result. Null-rejection is now only trusted when join_use_nulls = 1; otherwise the detectors fall back to the conservative, correct reordering.

3. Code: 717 on cross-boundary joins. With a detector on, DPsub failed to plan a join whose inner ON clause crosses an outer-join boundary (... JOIN t3 ON t2.y = t3.y AND t1.z = t3.z). An operator was applied at a split even when a relation its predicate needs was not joined yet; now an operator is only applied when all of its predicate relations are present.

Tests: 05098_conflict_detector_preserves_strictness, 05099_conflict_detector_outer_join_join_use_nulls, and 04305_dpsub_outer_join (already covers both detectors).

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...


Workflow [PR]
Sync PR [sync-upstream/pr/118494]

Version info

  • Merged into: 26.9.1.995 (included in 26.9 and later)

Rename the two DPsub join-reordering settings to a clearer form:
  query_plan_optimize_join_order_use_cd_a_conflict_detector
    -> query_plan_optimize_join_order_use_conflict_detector_a
  query_plan_optimize_join_order_use_cd_c_conflict_detector
    -> query_plan_optimize_join_order_use_conflict_detector_c

Also rename the corresponding internal QueryGraph members
use_cd_a_conflict_detector / use_cd_c_conflict_detector to
use_conflict_detector_a / use_conflict_detector_c to match.
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8f4111a]

Summary: ✅


AI Review

Summary

This PR renames the DPsub conflict-detector settings and fixes the reported strictness, join_use_nulls, and cross-boundary planning regressions. The new tests cover those cases, but the conflict-detector acceptor still loses the join kind for non-inner joins whose ON clause references only one side, so the current patch is not ready to merge.

Findings

❌ Blockers

  • [src/Processors/QueryPlan/Optimizations/joinOrderDPSub.cpp:299] The conflict-detector path ignores non-empty single-sided nels. For t1 LEFT JOIN t2 ON t2.x = 5, involved is false at the only {t1}|{t2} split, resolveJoinMask leaves the node as Inner, and buildPhysicalPlan reconstructs INNER JOIN ON t2.x = 5, dropping preserved rows from t1. The old join_kinds path preserved the outer/semi/anti kind for these pinned single-table ON clauses; the conflict-detector path still needs an equivalent fallback.
Final Verdict

❌ Changes needed before merge: isValidJoinOrderMaskConflict still changes results for outer/semi/anti joins whose ON predicate stays on one side of the join.

LLVM Coverage Report

Measured on commit 8f4111a.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.20% 81.20% +0.00%

Changed lines: Changed C/C++ lines covered: 68/69 (98.55%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Sep 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8f4111ab9 with master 4ee25bab4 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

119 translation units recompiled, 950 s compile time in total, 119 of them have a recent master baseline.

Job report

Enabling a join-order conflict detector
(query_plan_optimize_join_order_use_conflict_detector_a/_c) silently
turned ANY/SEMI/ANTI joins into ALL joins, so results were wrong: rows
multiplied, ANTI returned matched rows, and the NOT_IMPLEMENTED guards on
ANY/SEMI RIGHT JOIN stopped firing.

chooseJoinOrder re-stamps the graph's single strictness onto each
reconstructed join, skipping only when per-entry strictness is already
populated. That skip was gated on the detector settings being on, but
per-entry strictness is populated only in the much narrower case where the
detector actually reorders a semi/anti join (cda_reorder_semi_anti). So
whenever a detector was on but that reorder never happened -- the default
greedy algorithm never consults a detector, and an ANY join is never
reordered -- the skip was taken while nothing wrote a non-All strictness,
and the join emerged as ALL. This also left the wrong strictness for the
swap-suppression check further down.

Gate the skip on the exact predicate that populates per-entry strictness
instead of on the settings.

Related: #118520
Regression test for #118520:
ANY/SEMI/ANTI joins must keep their strictness with a join-order conflict
detector enabled, under both the greedy and dpsub algorithms.

Related: #118520
The join-order conflict detectors (CD-A/CD-C) use null-rejection (paper
Definition 1) to unlock the footnoted assoc/asscom reorderings of outer
joins. That reasoning is only sound when an unmatched outer-join row is
padded with a real SQL NULL. Under the default join_use_nulls = 0 the
padding is a type default (0/''), so a null-rejecting predicate such as
t2.id = t3.id still matches the padded 0, and reordering
(t1 LEFT JOIN t2) LEFT JOIN t3 into t1 LEFT JOIN (t2 LEFT JOIN t3) silently
changed the result.

Only treat a relation as null-rejecting when join_use_nulls = 1; otherwise
the detectors fall back to the conservative, correct outer-join reordering.
Plumb join_use_nulls into QueryPlanOptimizationSettings for this.

Related: #118520
With a conflict detector enabled, DPsub reported 'Failed to find a valid
join order' for a join whose inner ON clause crosses an outer-join
boundary, e.g. t1 RIGHT JOIN t2 ON t1.x = t2.x JOIN t3 ON t2.y = t3.y AND
t1.z = t3.z. The inner operator's ON clause references {t1,t2,t3}, so when
DPsub solved the sub-problem {t1,t2} that operator's nel still straddled
the {t1}|{t2} split and it was treated as applied there -- but its
predicate needs t3, which is not joined yet. Its required set then fit
neither side, rejecting every ordering of {t1,t2} and leaving no valid
plan.

isValidJoinOrderMaskConflict now only applies an operator at a split when
all of its predicate relations are present (nel is a subset of the joined
set); otherwise the predicate belongs to a higher node. Cross-operator
ordering is still enforced by CD-C's conflict rules, which are checked
separately, so this only admits the previously-rejected valid plan.

Related: #118520
The earlier strictness fix kept per-entry strictness only when the top
join's strictness was Semi/Anti. But a conflict detector can reorder a
semi/anti join nested under an inner top join, whose graph strictness is
All, so the re-stamp flattened it (a nested LEFT ANTI became a plain LEFT
join, multiplying rows). Conversely an ANY join under sole dpsub is capped
and never populated, so it must still be re-stamped.

Decide once per graph whether any DP entry carries a specific (non-All)
strictness: if so the graph mixes strictnesses and every entry's own value
is authoritative (keep it); otherwise the graph is uniform and its single
strictness is stamped onto the reconstructed joins. Add nested semi/anti
cases to the regression test.

Related: #118520
@fkastrati fkastrati changed the title Rename join-order conflict-detector settings Fix join-order conflict detector: strictness loss, join_use_nulls unsoundness, and cross-boundary planning failure Sep 7, 2026
fkastrati and others added 3 commits September 7, 2026 17:43
04305_dpsub_outer_join uses plain dpsub and never enables a conflict
detector, so it did not exercise isValidJoinOrderMaskConflict where the
Code 717 fix lives. Add LEFT/RIGHT cross-boundary cases with CD-C/CD-A
enabled to 05099, which errored with Code 717 before the fix.

Related: #118520
@fkastrati
fkastrati enabled auto-merge September 8, 2026 15:34
@fkastrati
fkastrati added this pull request to the merge queue Sep 8, 2026
Merged via the queue into master with commit 417aa99 Sep 8, 2026
174 checks passed
@fkastrati
fkastrati deleted the conflict-detector-dpsub-followup branch September 8, 2026 20:01
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 8, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@fkastrati — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

PedroTadim pushed a commit to groeneai/ClickHouse that referenced this pull request Sep 19, 2026
`query_plan_optimize_join_order_use_conflict_detector_a` and
`query_plan_optimize_join_order_use_conflict_detector_c` are replaced by a single
`query_plan_optimize_join_order_conflict_detector`, taking `a`, `c`, or the empty
string (the default, meaning no conflict detector).

This is the naming asked for in
ClickHouse#118413. That issue was closed by
ClickHouse#118494, which renamed the settings to
`..._use_conflict_detector_a` and `_c` rather than collapsing them into one setting
with the requested values.

Two booleans could express a state the feature does not have - both detectors at
once - which the documentation had to resolve with a precedence rule ("CD-C takes
precedence over CD-A when both are enabled"). A single value cannot be ambiguous, so
the rule is gone.

`QueryGraph::use_conflict_detector_a` and `_c` likewise become a single
`QueryGraph::conflict_detector` of the new `JoinOrderConflictDetector` enum.

The settings never appeared in a release: `v26.9.1.1-new` does not contain them, so
the two `26.9` entries in `SettingsChangesHistory` are replaced by one `26.10` entry
rather than being kept as obsolete aliases.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enabling a join-order conflict detector silently turns ANY / SEMI / ANTI joins into ALL joins Shamefully bad setting names and their documentation

3 participants