Repository navigation
Conversation
|
Workflow [PR], commit [2ed9a1e] Summary: ❌
|
2cd92e6 to
ea8151b
Compare
9b27ecd to
698a3fd
Compare
|
📊 Cloud Performance Report 🟢 AI verdict: This PR overhauls the join-order optimizer — reworking selectivity, cardinality and cost estimation, adding equi-join vs. cross-product handling, and introducing query_plan_merge_filters_into_join. The three flagged TPC-H improvements (Q4 ×3.6 faster, Q7 ×7.5 faster, Q9 -20.4%) are all multi-table joins and align directly with better join ordering; Q7 and Q9 are clean, on-path wins, while Q4's magnitude carries some caution because that query's history is noisy. The two ClickBench deltas (Q4 -10.2%, Q15 -17.2%) are single-table queries with no joins and flat CPU time, so they cannot be caused by this join-only change; the deterministic no_change/envelope suppression is correct there. clickbench🟢 No significant changes tpch_adapted_1_official🟢 3 improved Flagged queries (3 of 22)
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. Debug info
|
698a3fd to
1c8135d
Compare
…inistic `04327_correlated_subquery_duplicate_column_join` failed in Fast test with `LOGICAL_ERROR` "Input nodes size mismatch in dag" thrown from `JoinExpressionActions` via `chooseJoinOrder`, because the `joinGraphHasOverlappingColumnNames` guard was temporarily disabled. Re-enable it. Also make tests robust to settings randomization and the new default pretty `EXPLAIN` format: - `04337_join_reorder_merge_filter_into_join`: pin `explain_query_plan_default` to `legacy` - the step-counting queries match plan lines with anchored `LIKE` patterns, which the pretty tree format breaks. - `03734_correlated_subqueries_reorder`: pin `materialize_statistics_on_insert = 1` and update `ResultRows` in the reference - CI randomizes this setting, and the NDV-based estimate depends on whether statistics are materialized (`ResultRows: 100` with statistics vs `10000` from the FK->PK heuristic without them). CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107637&sha=64d49501ceacdb91de205a56ddc0b42ee8409cc1&name_0=PR&name_1=Fast%20test PR: #107637 Co-Authored-By: Claude Fable 5 <[email protected]>
bc60009 to
81b4da6
Compare
|
Updating the branch to fix the |
6de4978 to
1f082a4
Compare
…inistic `04327_correlated_subquery_duplicate_column_join` failed in Fast test with `LOGICAL_ERROR` "Input nodes size mismatch in dag" thrown from `JoinExpressionActions` via `chooseJoinOrder`, because the `joinGraphHasOverlappingColumnNames` guard was temporarily disabled. Re-enable it. Also make tests robust to settings randomization and the new default pretty `EXPLAIN` format: - `04337_join_reorder_merge_filter_into_join`: pin `explain_query_plan_default` to `legacy` - the step-counting queries match plan lines with anchored `LIKE` patterns, which the pretty tree format breaks. - `03734_correlated_subqueries_reorder`: pin `materialize_statistics_on_insert = 1` and update `ResultRows` in the reference - CI randomizes this setting, and the NDV-based estimate depends on whether statistics are materialized (`ResultRows: 100` with statistics vs `10000` from the FK->PK heuristic without them). CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107637&sha=64d49501ceacdb91de205a56ddc0b42ee8409cc1&name_0=PR&name_1=Fast%20test PR: #107637 Co-Authored-By: Claude Fable 5 <[email protected]>
…ction A pair linked only by non-equi predicates was classified the same as a pure cross product, so once the first predicate-less pair became the best plan, every later predicate-bearing pair was skipped and greedy could commit to an unconstrained cross product first. Keep `connected` meaning "any predicate spans both sides" (as in DPsize) for the skip rule, and key the supersede rule on an equi connection only. #107637 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
|
Your PR was poisoned by the test |
| Allow to merge expressions into JOIN step during join reordering optimization. | ||
| Allow to merge `Expression` steps into JOIN step during join reordering optimization. | ||
| )", 0) \ | ||
| DECLARE(Bool, query_plan_merge_filters_into_join, false, R"( |
There was a problem hiding this comment.
Why is a new setting needed? There is query_plan_merge_filter_into_join_condition already and it has the same purpose.
There was a problem hiding this comment.
This change allows building a query graph from a plan with filters; previously, filters acted as a barrier to join reordering.
There was a problem hiding this comment.
They act at different stages on different objects, so one cannot replace the other.
query_plan_merge_filter_into_join_condition drives tryMergeFilterIntoJoinCondition, a regular query-plan optimization: it takes a Filter step sitting directly above a JOIN step and moves the condition into that join's ON clause (converting CROSS JOIN to INNER when the condition qualifies). It rewrites one join at a time and is independent of join reordering — it applies even when reordering is off.
query_plan_merge_filters_into_join controls what the join-reordering optimization absorbs into the join graph. When reordering builds a graph out of a chain of joins, Filter steps sitting between those joins would otherwise terminate the chain, so the graph stays small and the predicates never take part in the search. With the setting on, isMergeableStepAboveJoin/mergeStepIntoQueryGraph peel such a Filter and turn its condition into ordinary edges of the graph, which means the enumerator can (a) span the whole join chain instead of stopping at the filter, and (b) use those predicates for selectivity and cardinality estimation while choosing the order — the effect the changelog entry describes. It is the Filter counterpart of the existing query_plan_merge_expression_into_join, and it is named after it for that reason.
So the pre-existing setting cannot express "let WHERE predicates participate in join order optimization": by the time reordering runs, a filter it did not merge is still a plan step that bounds the graph. Conversely this new setting does not subsume the old one, which also handles the CROSS-to-INNER rewrite outside reordering.
If the naming is the concern, I am happy to rename it to something less confusable — e.g. query_plan_merge_filter_into_join_graph, which would say which of the two objects it merges into.
…inistic `04327_correlated_subquery_duplicate_column_join` failed in Fast test with `LOGICAL_ERROR` "Input nodes size mismatch in dag" thrown from `JoinExpressionActions` via `chooseJoinOrder`, because the `joinGraphHasOverlappingColumnNames` guard was temporarily disabled. Re-enable it. Also make tests robust to settings randomization and the new default pretty `EXPLAIN` format: - `04337_join_reorder_merge_filter_into_join`: pin `explain_query_plan_default` to `legacy` - the step-counting queries match plan lines with anchored `LIKE` patterns, which the pretty tree format breaks. - `03734_correlated_subqueries_reorder`: pin `materialize_statistics_on_insert = 1` and update `ResultRows` in the reference - CI randomizes this setting, and the NDV-based estimate depends on whether statistics are materialized (`ResultRows: 100` with statistics vs `10000` from the FK->PK heuristic without them). CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107637&sha=64d49501ceacdb91de205a56ddc0b42ee8409cc1&name_0=PR&name_1=Fast%20test PR: #107637 Co-Authored-By: Claude Fable 5 <[email protected]>
`collectJoinEdgesMask` kept a non-connecting predicate only at
two-relation joins and only when `fromLeft`/`fromRight`/`fromNone`
held, which are hard-coded to source relations {0} and {1}. A filter
merged into the join graph (e.g. by `query_plan_merge_filters_into_join`
from a flattened subquery) whose relation id is >= 2 was attached
nowhere, so `dpsub` silently dropped it from the plan and returned rows
that should have been filtered out. Attach single-relation predicates at
the join step where their relation forms a whole side (the leaf join of
that relation), mirroring the `dpsize` fix, and keep the two-relation
attachment only for constant predicates.
Co-Authored-By: Claude Fable 5 <[email protected]>
`solveDPsize` filtered non-connecting predicates by their raw source relations, ignoring the null-supplying relation that ON-clause conjuncts of an outer join are pinned to. A pinned single-table conjunct (e.g. `t2.value = 'x'` in `... LEFT JOIN t3 ON t2.id = t3.id AND t2.value = 'x'`) reaching the step that joins the null-supplying relation matched neither the singleton nor the constant branch and was silently dropped. Not reachable today: `dpsize` restricts itself to inner join steps, so it never assembles a plan containing an outer join and falls back instead. Align the placement rule with `collectJoinEdgesMask` (`sources | pin`) so the conjunct stays in the outer join's ON condition once that restriction is lifted, and add a `dpsize` variant of the chained outer join test that 04507_dpsub_chained_outer_join_null_side covers for `dpsub`. Co-Authored-By: Claude Fable 5 <[email protected]>
…etector After merging master, DPsub with the `CD-C` conflict detector is the default join order algorithm, and it requires every join graph condition to name the join step it comes from (`edge_conflict_ops`). Conditions merged by `query_plan_merge_filters_into_join` had none, so DPsub threw `Join graph has N conditions, but join steps are recorded for M of them`, and the operator indices of all conditions united after them were shifted. Attribute each merged condition to the root join step of the graph it is merged into, and mark it as applied above that step (`edge_above_conflict_op`): when it reads the null-supplying side of the step, DPsub waits for the step itself, so a `WHERE` condition over a flattened null-supplying subtree is applied to the padded rows after the outer join, not inside the subtree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`buildPhysicalJoinImpl` collected the inputs of the `ON` and residual conditions by walking every path of the condition DAG. A condition with shared nodes (a chain of `x + x` aliases) has exponentially many paths, so `05359_partial_result_shared_nodes_join_filter` hung once `query_plan_merge_filters_into_join` merged such a filter into the join step. Keep a visited set. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107637&sha=latest&name_0=PR #107637 Co-Authored-By: Claude Opus 5.5 <[email protected]>
`05055_join_order_greedy_non_equi_over_cross` asserts an exact greedy join order, which changes with column statistics (randomized `auto_statistics_types`), so disable them for its tables. `05292_replace_always_true_filter_join_order` checks that only a filter left between the joins splits the join graph, so pin `query_plan_merge_expression_into_join = 1` and `query_plan_merge_filters_into_join = 0`, which this PR randomizes. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107637&sha=latest&name_0=PR #107637 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged master and resolved conflicts with the DPsub / conflict-detector work (DPsub with
Two threads are still open and need a decision from @vdimir: whether to rename the setting (asked by @novikd), and whether to estimate single-table filter selectivity in this PR or in a follow-up. |
…type; visit shared condition atoms once A merged `WHERE` equality can become a join key; operands that `equals` compares but that have no common supertype (`DateTime` and `UInt32`) made the join throw `NO_COMMON_TYPE`. Keep such a filter as a step, like `extractActionsForJoinCondition` does. Flattening the merged condition into join-graph edges walked every path of the condition DAG, so a shared conjunction (`p1 = p0 AND p0`, ...) produced exponentially many identical edges. Track visited nodes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…pe and shared condition atoms Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=107637&sha=f8c29792925ea30aaa5e095c1b602137d925441a&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 ( The |
|
The fix is in progress in #120222. This is the same issue as on #112928 (#112928 (comment)): The fuzzed query contains Measured with debug builds of master |
# Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp
…n chain A `WHERE` condition computed by a separate `ExpressionStep` below the `Filter` step (e.g. with `query_plan_merge_expressions = 0`) hid an unsafe equality from `hasEqualityUnsafeAsJoinKey`, which only inspected the filter's own DAG. The check now runs in `peelMergeableStepsAboveJoin` over the expression steps below each filter too, and `addChildQueryGraph` uses the same function, so the peeled chain stops at the topmost unsafe filter. Also treat the null-safe `isNotDistinctFrom` like `equals`: physical planning consumes it as a join key as well, and its key matching differs (a NaN matches another NaN). Co-Authored-By: Claude Opus 5.5 <[email protected]>
A predicate DAG with shared conjunction nodes (`p1 AND p1` where `p1 = p0 AND p0`) made `extractConjunctionAtoms` yield exponentially many copies of the same atoms. With `query_plan_optimize_join_order_limit = 1` (randomized in CI) the filter push-down over a join then spent minutes in `filterResultForNotMatchedRows`, which made `05480_join_reorder_merge_filter_no_common_type_shared_atoms` exceed the time limit in the flaky check. Cover that path in the test too. https://s3.amazonaws.com/clickhouse-test-reports/PRs/107637/6d705238f9aec3f5178777997cee5b0c61703af9/pr/result_pr.json Related: #107637 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Thanks, @groeneai — the AST fuzzer failure ( Update in 73a6662: merged master (resolved a conflict in |
# Conflicts: # src/Processors/QueryPlan/JoinStepLogical.cpp # src/Processors/QueryPlan/Optimizations/joinOrderDPSize.cpp # src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01DFFRj3kqWUsA1C3A8DapeT
…p`, like the other edge vectors after #129617 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01DFFRj3kqWUsA1C3A8DapeT
|
🕵 Merged |
…alities unsafe as join keys `peelMergeableStepsAboveJoin` checked only the chain of steps above the nearest join. The condition of a `Filter` step can also be computed below that join, by a step that `buildQueryGraph` flattens into the same graph afterwards, so the merged filter read the resolved equality and it became a hash join key: extra rows for `Variant` and NaN with `isNotDistinctFrom`, `ILLEGAL_COLUMN` for `Dynamic`, and `NO_COMMON_TYPE` for `DateTime` and `UInt32`. The new `hasEqualityUnsafeAsJoinKeyInSubplan` walks every `Expression`, `Filter`, and `JoinStepLogical` step below the chain, which covers at least what the graph can absorb, and a filter above such an equality keeps its step. #107637 (comment) Co-Authored-By: Claude Fable 5.2 <[email protected]>
…filter into atoms `ActionsDAG::extractConjunctionAtoms` visits a shared node once, but it walked the arguments of `and` right to left, so it kept the last occurrence of a repeated atom: `notEmpty(s) AND toInt32(s) > 0 AND notEmpty(s)` became `toInt32(s) > 0 AND notEmpty(s)` after filter push down, and the conversion ran before its guard. It now walks left to right, keeps the first occurrence, and reverses the result to keep returning the atoms right to left. `mergeStepIntoQueryGraph` had the same traversal for the filter merged into the join graph (`query_plan_merge_filters_into_join`), and the edges kept that reversed order in the rebuilt join condition. It now visits the conjuncts in the written order too. #107637 (comment) #107637 (comment) Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
🕵 Merged master (no conflicts) and addressed the remaining review threads:
The previous CI run was red only because of the review-threads gate (the full suite was skipped). One thread is still open and needs a decision from @vdimir: whether to rename |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
query_plan_merge_filters_into_jointhat allows mergingFiltersteps into theJOINstep during join reordering, soWHEREpredicates participate in join order optimizationWorkflow [PR]
Sync PR [sync-upstream/pr/107637]