Repository navigation
Preserve outer-join key NDV estimates - #122305
Conversation
Tighten equi-join key distinct counts only on sides that can lose rows, preserving outer and anti join inputs while filtering the output side of semi joins. Share the propagation helper between join ordering and Cascades.
|
Workflow [PR], commit [12cec9d] Summary: ✅
AI ReviewSummaryThis PR factors outer/semi/anti-aware join-key NDV propagation into a shared helper and updates the aggregation-pushdown expectations that change as a result. The remaining gap is that supported null-safe equi-joins still bypass the new propagation entirely, and the PR does not yet include focused runtime evidence for the claimed Findings
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 12cec9d.
Changed lines: Changed C/C++ lines covered: 54/54 (100.00%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only changes how the join-order and cascades planners estimate distinct join-key counts for outer, semi and anti joins, plus the matching tests. The one flagged query, clickbench Q23 at -40.7% (86 ms to 51 ms), reads a single table and has no joins, so it never runs the changed code. The base measurements were very noisy and the query has been trending on master, so it is downgraded to not_sure. No tpch_adapted_1_official queries were flagged, even though that benchmark is where multi-join planning changes would show up. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
m-selmi
left a comment
There was a problem hiding this comment.
Looks good besides the minor fast-test fix!
Preserving the fact key NDV across a `LEFT JOIN` makes the near-unique case local and allows partial aggregation in the three-join case. Update their `EXPLAIN` references and comments.
| left_it->second.num_distinct_values = min_ndv; | ||
| right_it->second.num_distinct_values = min_ndv; | ||
| QueryPlanOptimizations::updateJoinKeyDistinctCounts( | ||
| left_it->second, |
There was a problem hiding this comment.
IS NOT DISTINCT FROM / JoinConditionOperator::NullSafeEquals is already treated as an equi-join key elsewhere (for example joinOrderCommon::computeSelectivity and gtest_join_spill_trigger_plan_setting.cpp both rely on that), but this path still only reaches updateJoinKeyDistinctCounts for Equals. As a result, a LEFT/RIGHT/FULL/SEMI/ANTI join written with a null-safe key keeps the pre-fix NDV propagation in both join ordering and StatisticsDerivation, so the optimizer bug this PR is fixing still reproduces on a supported join-key form. Please route NullSafeEquals through the same update in both call sites.
| SELECT '-- 2b. negative: non-deterministic join condition function (`rand`) blocks the pushdown, classic shape'; | ||
| EXPLAIN SELECT count() FROM t_push_facts AS t1 LEFT JOIN t_push_dims AS t2 ON t1.key = t2.key AND rand() % 2 = 0 GROUP BY t1.key; | ||
|
|
||
| -- Preserving the left key's NDV after the `LEFT JOIN` makes a local aggregation cheaper |
There was a problem hiding this comment.
This comment is the core performance claim of the PR, but the added evidence only shows that the chosen plan shape changed. Since NDV heuristics can also make a query slower by steering it to a different plan, can we add one focused before/after measurement for this motivating case (even EXPLAIN ANALYZE or a small perf test would be enough)? The generic cloud benchmark report on the PR is no_change, so there is not yet evidence behind the Performance Improvement changelog category.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units21 translation units recompiled, 112 s compile time in total, 21 of them have a recent master baseline. |
|
Does this address the same problem as #121075? |
No, it's a different mechanism. This one here, makes sure that the correct NDV is propagated depending on the join type. The other one is addressing an foreign key related NDV heuristic. |
… counts Cascades keeps row estimates as Float64 because a cross product of large inputs can exceed the UInt64 range, and the cost model compares such values. Distinct-value counts are UInt64, and ten places in StatisticsDerivation.cpp turned an estimate into one with a plain cast, which is undefined behavior for values of 2^64 and above. Since ClickHouse#122305 the join-key bound casts each input's row count separately, so one huge input of an equality join is enough. The AST fuzzer hit it with TPC-H Q18 from 05024_tpch_join_order_plans_aggregation_pushdown with its ANDs turned into ORs: customer x orders x lineitem is a 1.35e24-row cross product under the SF100 hints, and it is the left input of the semi join made by rewrite_in_to_join (STID 4584-4b08, StatisticsDerivation.cpp:304). Add toUInt64Saturating to RelationStatistics, next to the statistics helpers shared with join ordering, and use it at every conversion. It is identical to the plain cast below 2^64. Clamping the estimate itself would flatten the cost model, and std::min with Float64(UINT64_MAX) is 2^64, still out of range, hence the >= check. The test reaches the left and right join keys, an aggregation key, DISTINCT and INTERSECT. Its INTERSECT query projects columns of both cross product inputs: with only one input's columns the cross product returns no rows under the distributed Cascades plan, on master too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Extracted from #121736.
Previously, equi-join key distinct-value estimates were unconditionally reduced to the minimum estimate from both inputs. This was incorrect for join sides whose rows or key domain are preserved.
This PR introduces a shared helper used by both the join-order optimizer and Cascades statistics derivation. It handles outer, semi, and anti joins consistently while deduplicating the propagation logic.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improves
JOINcardinality estimation by propagating join-key distinct-value estimates according to join semantics. Outer joins retain the preserved-side estimate, anti joins keep their input estimates, and semi joins narrow only the output side, preventing underestimation that could lead to suboptimal query plans.Workflow [PR]
Sync PR [sync-upstream/pr/122305]
Version info
26.10.1.1297-master(included in26.10and later)