Repository navigation
Push filters below WindowStep on PARTITION BY columns - #110114
alexey-milovidov merged 9 commits into
Conversation
A predicate that references only the PARTITION BY columns of a window function is safe to evaluate before the window: it only removes whole partitions, and dropping a partition never changes any window value on a surviving row, regardless of the window function or frame. Previously the plan optimizer had no WindowStep case in filterPushDown, so such predicates written after the window (outer WHERE around a windowed subquery, or a conjunct in QUALIFY) stayed above the WindowStep and never reached storage, blocking primary key / partition pruning, skip indexes and projections. The dedup idiom row_number() OVER (PARTITION BY key ...) ... QUALIFY rn = 1 AND key = 'x' therefore read and windowed the whole table. Add a WindowStep case analogous to the AggregatingStep one: split the filter conjunction and push the conjuncts whose inputs are a subset of the window PARTITION BY columns, keeping predicates on the window result above. For multiple windows each WindowStep is handled locally, so a common partition-key conjunct percolates down step by step. Closes: ClickHouse#110109
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-3:20260711-184900 |
|
cc @KochetovNicolai @vdimir for review: adds the missing |
|
Workflow [PR], commit [4cce638] Summary: ✅
AI ReviewSummaryThis PR adds the missing Final Verdict
|
The regression test parsed the ReadFromMergeTree granule counts with extract(explain, 'Granules: N/M')::UInt64. Under some randomized settings the Granules line differs or is absent for a row matched by the ILIKE filter, so the cast of an empty string threw ATTEMPT_TO_READ_AFTER_EOF and the Fast test failed. Assert instead that the partition-key predicate reached storage as a primary key Condition (matching the 'Condition:' line), a pure boolean over a text match with no arithmetic on the granule counts. Reference output is unchanged (1 1 1 1 0 0). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: This sync PR adds a single planner optimization: pushing PARTITION BY predicates below WindowStep in filterPushDown.cpp. That path is only reachable by queries containing window functions, and neither flagged ClickBench query uses one (Q4 is a COUNT DISTINCT, Q15 a GROUP BY/ORDER BY/LIMIT). Both ~22% deltas (change_factor -22.0% and -21.8%) are therefore off the changed path, and CPU time stays flat on both while only the wall-clock median moves — consistent with co-location/environment variance, not a PR effect. Q15's flagged improvement is downgraded to not_sure; Q4 was correctly held within its baseline envelope. 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
|
A predicate must be deterministic in the partition key, not merely reference only it. Pass allow_non_deterministic_functions=false (as aggregation does) so a conjunct like rand64(key) % 2 = 0 is not moved below the window, where it would drop arbitrary rows inside a surviving partition and change window values. Add a negative regression to 04365. Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — 4eab62aEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260712-033000 |
|
Fixing PR for the |
|
@groeneai Let's resolve the conflicts with master. |
…tition-filter-pushdown-110109 # Conflicts: # src/Processors/QueryPlan/Optimizations/filterPushDown.cpp
Under parallel replicas the pushed partition-key conjunct reaches storage as a plain Filter step, not a primary key Condition (the KeyCondition is not built in the local plan), so the EXPLAIN "Condition:" detection saw 0 on the ParallelReplicas CI job while correctness stayed intact. Pin enable_parallel_replicas = 0 for the plan-shape assertions, matching the sibling filter-pushdown test 00808_not_optimize_predicate. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
The |
The EXPLAIN indexes=1 assertions detect the pushed key predicate as a local primary key Condition. Under parallel replicas the table is read on remote replicas, so the coordinator plan carries ReadFromRemoteParallelReplicas and no such Condition, making count() > 0 return 0. Match sibling 04366_filter_push_down_limit_by.sql, which uses the no-parallel-replicas tag for the identical assertion pattern. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Assert that the WindowStep partition-key filter pushdown does not disable the old-analyzer optimize_read_in_window_order storage-order reuse: with the filter pushed below the window, ReadFromMergeTree still reads in storage order (Read type: InOrder) rather than falling back to a full sort. Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI finish ledger — 86a5279Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
|
|
Ledger linkage correction for the The previously-linked #110142 ( |
|
@groeneai Check CI failures. |
|
No merge conflicts (branch was MERGEABLE). Merged current master (c7659f9) to refresh CI. Rebuilt, regression test 04365 passes byte-identical. On CI failures: none were caused by this PR. The prior run (86a5279) reds were all non-PR-caused, each owned by a fixing PR:
The earlier PR-caused 04365 ParallelReplicas failure was already fixed via the no-parallel-replicas tag and stays green. Fresh CI is running on c7659f9. |
CI finish ledger — c7659f9All failures on this commit are the single ongoing infra runner outage; none are caused by this PR's diff. Every failure has an owner.
Session id: cron:our-pr-ci-monitor:20260715-153000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 10/10 (100.00%) · Uncovered code |
…rtition-filter-pushdown-110109 Push filters below WindowStep on PARTITION BY columns
…BY and GROUP BY Filter push-down moves a WHERE condition below these steps when it uses only their pass-through or key columns, so a join input such as `(SELECT DISTINCT ...)`, `(... ARRAY JOIN ...)`, a window subquery (the ClickHouse#110114 push-down), `LIMIT n BY k` or `GROUP BY k` hid the empty `IN` set from the walk and the other side was still read. Each of these steps outputs no rows for an empty input, so the walk now passes them. The filter `query_plan_fuse_filter_into_array_join` moves into a (non-LEFT) ARRAY JOIN is read like a FilterStep. Aggregation is passed only with keys and without grouping sets: keyless aggregation and a `()` grouping set output one row for an empty input (measured: `GROUP BY GROUPING SETS ((k), ())` over an empty table returns 1 row). No query reaches those exclusions today (push-down never moves a filter below them, and an `IN` subquery written inside such a subquery adds a CreatingSets step that stops the walk), so they are kept for the invariant, not covered by a test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both queries of ClickHouse#123875 must stop before the LEFT JOIN below the window reads its right side, which throws if it is read. They fail on master (the filter is pushed below the window to the join's left input) and pass with the revert. The early stop holds only for a hash join that builds the right table, without parallel replicas and with query_plan_merge_filters = 1, so the test pins those. Measured on the revert binary: parallel_hash, grace_hash, full_sorting_merge, parallel replicas and query_plan_merge_filters = 0 all read the right side, as before ClickHouse#110114. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This reverts commit 338b873 (ClickHouse#110114), reversing changes made to 662a0a0. A filter pushed below the window can reach the probe side of a hash join, which is read only after the build side, so an always-false filter such as IN over an empty subquery no longer stops the query before the build (ClickHouse#123875). Conflict resolution: ClickHouse#122975 later changed the reverted branch; the whole branch is removed, its other call sites and its test stay. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…evert Master now short-circuits `throwIf(rk > 1) = 0 AND rk <= 1`: `throwIf` is not evaluated on the rows `rk <= 1` rejects, so case 30b's query no longer raises with the optimization off or on (measured on master 2cb89c8, which does not contain this change: it returns 3). Pin short_circuit_function_evaluation to 'disable' on both queries, which restores the exception in both arms and keeps the case able to catch a prefilter that drops the rows `throwIf` throws on. ClickHouse#110114 was reverted by ClickHouse#125463, so `p < 100` is no longer pushed below the window. The prefilter still declines that query, now because the filter computes `p < 100` beside the bound. Only the case label changes; the asserted count stays 0. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #110109
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Push a filter below a window function when the predicate references only the window
PARTITION BYcolumns. Such a predicate written after the window (an outerWHEREaround a windowed subquery, or a conjunct inQUALIFY) now reaches storage and enables primary key / partition pruning, skip indexes and projections. For exampleSELECT ... row_number() OVER (PARTITION BY key ORDER BY ts) AS rn ... QUALIFY rn = 1 AND key = 'x'no longer reads and windows the whole table.Description
A predicate referencing only the
PARTITION BYcolumns of a window is safe to apply before the window: it only removes whole partitions, and removing a partition never changes any window value on a surviving row, regardless of the window function or frame.filterPushDownhad cases forAggregatingStep,TotalsHavingStep,SortingStep, join steps and others, but none forWindowStep, so such predicates stayed above the window and never reached storage.This adds a
WindowStepcase directly analogous to theAggregatingStepone: the filter conjunction is split and conjuncts whose inputs are a subset of the windowPARTITION BYcolumns are pushed below the window; predicates on the window result stay above.WindowStepdoes not change the number of rows, so deterministic functions are allowed in the pushed part. For multiple windows eachWindowStepis handled locally, so a common partition-key conjunct percolates down step by step.Verified locally that with the fix the QUALIFY and outer-WHERE spellings prune granules via the primary key (and via projections), while a predicate on the window result only is correctly kept above; results are unchanged. Regression test
04365_filter_push_down_window_partition_byasserts pruning happens for the pushed cases and does not happen for the negative cases, and that results match forcing the filter before the window.