Repository navigation
Fix join-order conflict detector: strictness loss, join_use_nulls unsoundness, and cross-boundary planning failure - #118494
Conversation
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.
|
Workflow [PR], commit [8f4111a] Summary: ✅
AI ReviewSummaryThis PR renames the DPsub conflict-detector settings and fixes the reported strictness, Findings❌ Blockers
Final Verdict❌ Changes needed before merge: LLVM Coverage ReportMeasured on commit 8f4111a.
Changed lines: Changed C/C++ lines covered: 68/69 (98.55%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units119 translation units recompiled, 950 s compile time in total, 119 of them have a recent master baseline. |
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
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
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 — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
`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]>
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_aquery_plan_optimize_join_order_use_cd_c_conflict_detector->query_plan_optimize_join_order_use_conflict_detector_c1. Strictness dropped to
ALL. Enabling a detector turnedANY/SEMI/ANTIjoins intoALLjoins (rows multiplied,ANTIreturned matched rows,NOT_IMPLEMENTEDguards stopped firing).chooseJoinOrderre-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-Allentry (a CD-reordered semi/anti join, which may be nested under an inner top join), otherwise stamps the uniform strictness (coversgreedy, and a cappedANYjoin).2. Unsound outer-join reorder under
join_use_nulls = 0. The detectors' null-rejection analysis assumes an unmatched outer row is a realNULL. Under the defaultjoin_use_nulls = 0the padding is a type default (0/''), so a null-rejecting predicate still matches the padded0and(t1 LEFT JOIN t2) LEFT JOIN t3reordered tot1 LEFT JOIN (t2 LEFT JOIN t3)changed the result. Null-rejection is now only trusted whenjoin_use_nulls = 1; otherwise the detectors fall back to the conservative, correct reordering.3.
Code: 717on 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, and04305_dpsub_outer_join(already covers both detectors).Changelog category (leave one):
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
26.9.1.995(included in26.9and later)