Repository navigation
Propagate predicates across equi join - #108377
Conversation
|
Workflow [PR], commit [5b386d8] Summary: ✅
AI ReviewSummaryThis PR adds PR Metadata
Exact replacement: Findings
Tests
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit 5b386d8.
Changed lines: Changed C/C++ lines covered: 230/241 (95.44%) · Uncovered code |
ReadFromMergeTree::applyFilters short-circuits when `indexes` is already populated. The initial second-pass PK traversal populates it before the lift adds new filters, so a plain re-run of `optimizePrimaryKeyConditionAndLimit` was a no-op — the FilterStep appeared in the plan but the source still scanned every granule. The analysis is also cached in `analyzed_result_ptr`, so even after rebuilding `indexes`, `getAnalysisResult` (used by EXPLAIN and range selection) returns the stale cached result. Add a public `ReadFromMergeTree::invalidateIndexes()` that drops both `indexes` and `analyzed_result_ptr`, and call it on the affected sources before re-running PK condition analysis. Move the PK re-run into the leave callback right after the lift fires, BEFORE `convertLogicalJoinToPhysical` reorganizes the JOIN's children (after the conversion the physical structure has back-edges that a plain tree DFS can't cope with), and give it its own `Stack` since `traverseQueryPlan` starts with `stack.clear()` which would corrupt the outer walk's stack. Verified locally on 200M+200M rows, max_threads=1: Point lookup k = 100000000 78 ms vs 2.08 s ~27x Narrow range, ~1k matches 81 ms vs 2.16 s ~27x LEFT join, ~10k matches 82 ms vs 2.32 s ~28x IN list, 6 keys 80 ms vs 2.11 s ~26x Wide range (10 % of target) 3.04 s vs 7.37 s ~2.4x Overhead (filter on non-equi col) 3.76 s vs 3.77 s ~1.00x Perf XML bumped to 200M+200M and max_threads=1 so the LIFT-OFF baseline lands in the 1-2 s window where the speedup is visible in wall clock; adds IN-list, wide-range, and overhead-check coverage.
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 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
|
… INPUT JoinStepLogical equi-keys can be expressions produced by join pre-actions (e.g. ON l.k = r.k + 1). The substituted column ends up as an INPUT name of the lifted FilterStep, which sits above the target child — so the name must exist in the child's output header. Skip pairs whose target key is not a raw INPUT in the corresponding direction. The guard was originally added in 0f818df and removed by 09875f5 alongside the source-filter snapshot refactor. Restore it.
Add a focused EXPLAIN test for the regression shape that prompted the target-key INPUT guard — ON o.orderkey = l.orderkey + 1, where the rhs equi-key is a computed pre-join expression. Verify no Lifted equi-join filter is inserted and the result matches the setting-off plan.
The previous INPUT-only guard was too strict — JoinStepLogical's equi-key JoinActionRef can be an ALIAS or other non-INPUT wrapper even for simple 'ON l.k = r.k', so the guard rejected valid pairs and the lift stopped firing on the existing 'inner eq' / 'left between' / 'multi-clause' cases. Replace it with a direct check: the target column's name must exist in the target child's output header. This is the actual invariant the FilterStep needs (its INPUT must resolve to a column the child produces), and it naturally rejects computed equi-keys like 'ON l.k = r.k + 1' whose rhs name is 'plus(...)' that isn't in the right child's header.
The previous merge from master accidentally rolled back contrib/azure from 20f8b46b8 to e1c250fe5, dropping the Base64Decode fix for global-buffer-overflow and undefined left shift on malformed input. Restore the pointer to the master revision; this submodule bump is unrelated to the lift-predicate change. Addresses #108377 (comment)
Under randomized parallel-replicas settings the plan reads through remote-replica steps, the lift correctly bails (no local MergeTree target), and the EXPLAIN countIf drops to 0. The test checks the local read path, so pin the setting.
| if (propagated_dag->getOutputs().size() != 1) | ||
| return 0; | ||
|
|
||
| /// Drop conjuncts the target already has: equal result_name means structurally equal atom |
There was a problem hiding this comment.
Matching by name probably does not always guarantee a result, since DAGs may in theory have arbitrary renames. However, I cannot come up with a better solution at the moment.
There was a problem hiding this comment.
agree, I don't have either
|
Hi! Quick question. This doesn't seem to propagate PREWHERE predicates, although they can? Is this leftover deliberate, and am I missing something? |
@PedroTadim |
Adds a query-plan optimization (
query_plan_propagate_predicate_across_join) that, for an equi-join, copies filter conjuncts from one side onto the other via equi-key substitution, so that index pruning (MergeTreeprimary key) on the other side picks them up.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added query-plan optimization
query_plan_propagate_predicate_across_jointhat copies equi-join filter conjuncts acrossMergeTreejoin inputs for better primary-key pruning.Perf results show 70-80x improvement on specific queries.
Workflow [PR]
Sync PR [sync-upstream/pr/108377]
Version info
26.9.1.727(included in26.9and later)