Skip to content

Revert "Push filters below WindowStep on PARTITION BY columns" - #125463

Merged
nihalzp merged 2 commits into
ClickHouse:masterfrom
groeneai:revert-110114-window-partition-filter-pushdown
Oct 13, 2026
Merged

nihalzp merged 2 commits into
ClickHouse:masterfrom
groeneai:revert-110114-window-partition-filter-pushdown

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #123875
Related: #110114
Related: #110109
Related: #123927

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A filter on the PARTITION BY columns of a window function is no longer pushed below the window (this reverts #110114). The push-down made queries up to 8 times slower when the window reads a JOIN and the filter removes every row, for example IN over an empty subquery, because the join built its right side before the filter could stop the query. Closes #123875.

Description

Reverts #110114, as requested in #123875 (comment); #110109 is to be reopened.

#110114 lets a WHERE on PARTITION BY columns pass the window step, and join push-down then moves it to the hash join's left input. A hash join reads that input only after building its right side, so an always-false filter such as c1 IN (SELECT ... FROM empty_table) no longer stops the query before the build. Results were never wrong.

Issue queries (compatibility = '24.12', max_threads = 8), master CI release binaries before (662a0a0) and after (28ab4b6) #110114, median of 5:

Query before after
window over LEFT JOIN, outer IN 42 ms, no join build 181 ms, 2,000,000-row build
rn = 1 AND c1 IN (...) 41 ms, no build 240 ms, 2,000,000-row build
same LEFT JOIN, no window 203 ms, 2,000,000-row build 227 ms, 2,000,000-row build

The revert does not apply cleanly: #122975 later added allow_index_hints = false to the removed call. The whole window branch is removed; #122975's other call sites and its test stay. The test of #110114 is deleted. The new test 05393_window_over_join_empty_in_set runs both issue queries with a join right side that throws if it is read: it fails on master and passes with the revert.

Validation (debug build, both binaries): with the revert, both issue queries do no join build again and read the same SelectedRows as the build before #110114, with identical results. The deleted test's push-down probes flip from 1 to 0, its correctness rows are unchanged. 291 window and QUALIFY stateless tests give identical pass/fail sets with and without the revert, randomized and deterministic.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125463]

Version info

  • Merged into: 26.10.1.2262-master (included in 26.10 and later)
  • Backported to: 26.9.19.14, 26.8.27.14

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]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings).

# Sev Finding Verdict Evidence / action
1 ⚠️ The window cases of 05291_index_hint_below_window cannot fail once nothing is pushed below a window DISAGREE No filter reaches below a WindowStep after the revert, so the indexHint guard cannot go missing on its own; if a window push-down returns (#110109), these result checks for #121177 are what catches a missing guard. The test belongs to #122975, so I left it as is.
2 💡 ActionsDAG.h:515 and the "mirror of tryPushDownFilter" comment in convertOuterJoinToInnerJoinTransitively.cpp still mention a window case AGREE, not changed Both statements stay true. I kept the revert to the reverted code.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261012-171000

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On master the issue queries build 2,000,000 right-side rows and select 10,000,000 rows on every run (JoinBuildTableRowCount, SelectedRows); the deleted test's four push-down probes print 1.
b Root cause explained? #110114 lets a WHERE on PARTITION BY columns pass the window step; join push-down then moves it to the hash join's left input, which is read only after the right side is built, so the empty-IN early stop fires after the build instead of before it.
c Fix matches root cause? Yes. A maintainer-requested revert that removes exactly the branch moving the filter below the window; nothing else in planning changes.
d Test intent preserved / new tests added? The reverted PR's own test is deleted and no test is added (#110109 is being reopened for a new design; a test pinning the old plan would constrain it). The other 291 window and QUALIFY tests are unchanged and pass as before.
e Both directions demonstrated? Yes, on Build-ID-verified binaries: master builds 2,000,000 rows, the revert builds none (SelectedRows 327,680 / 523,272, as before #110114); the deleted test's probes flip from 1 to 0 with identical correctness rows.
f Fix is general across code paths? The removed branch was the only code that moved a filter below a window. Outer-to-inner join conversion through a window reads a filter above it and is unaffected (its test passes); the LIMIT BY push-down (#110116) is not reverted.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for a revert: the removed code had no type or setting dependence beyond the partition keys.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; planning returns to 26.6 behaviour and results are identical. Queries that gained from the push-down (QUALIFY rn = 1 AND key = 'x') read the whole table again, as on 26.6.
i Invariants and contracts preserved? The filter stays where the query put it. #122975's allow_index_hints plumbing keeps its two remaining callers; no dangling include or symbol. 291-test A/B: identical pass/fail sets, randomized and deterministic.

Session id: cron:clickhouse-impl-slot-6:20261012-164900

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d55d182]

Summary: ⏳

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, 1/3) FAIL
05029_url_function_schema_inference_hard_timeout_during_backoff FAIL cidb
AST fuzzer (amd_release, oracle) FAIL
AST fuzzer oracle mismatch FAIL cidb
LLVM Coverage FAIL
Generate LLVM Coverage Diff Report FAIL cidb
Print Uncovered Code FAIL cidb Uncovered code analysis did not run: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

AI Review

Summary

This PR reverts the WindowStep filter push-down added by #110114 and replaces the deleted plan-shape coverage with a focused stateless regression test for #123875. I did not find a remaining contract violation in the current patch: the removed branch is the tryPushDownFilter case that moved predicates below WindowStep, and the new LEFT JOIN / empty-IN reproducer now covers the regression the revert is meant to fix.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

⚠️ No coverage measurement for commit d55d182: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Oct 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

⚠️ AI verdict: not_sure — 1 query(s) regressed out of 31 analysed

This PR removes one optimization: filters on PARTITION BY columns are no longer pushed below WindowStep. Only queries with window functions take that code path. The benchmark flagged one query, clickbench Q18, as +42.6% slower (824.5 ms to 1175.5 ms, with CPU time up by a similar amount). The shift is too large to override, so the regression verdict stays. However, Q18 is a plain GROUP BY with no window function and never reaches the changed code, and it has a history of noisy results on master. Most likely something other than this diff caused the slowdown, such as a different base commit or variance on the test machine. Re-run the benchmark before treating it as caused by this PR.

clickbench

🔴 1 regressed

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🔴 18 regression 824 1176 +42.6% <0.0001 aggregation: Q18 is +42.6% slower and uses more CPU, but it is a GROUP BY with no window function, so the removed Window filter pushdown never runs. The delta is too large to override, though this query is noisy on master.

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
  • StressHouse run: 251a5323-ae61-4079-bfb1-f1c6f6594300
  • MIRAI run: 1c8835e5-82cc-42cc-9633-7e553bb6e205
  • PR check IDs:
    • clickbench_1111318_1791833904
    • clickbench_1111330_1791833904
    • clickbench_1111336_1791833905
    • tpch_adapted_1_official_1111348_1791833904
    • tpch_adapted_1_official_1111375_1791833904
    • tpch_adapted_1_official_1111389_1791833905

@nihalzp nihalzp self-assigned this Oct 13, 2026
@nihalzp nihalzp added the ci-force-all Do not skip any jobs when running label Oct 13, 2026
@nihalzp

nihalzp commented Oct 13, 2026

Copy link
Copy Markdown
Member

Running the CI because this is not a plain revert.

@nihalzp nihalzp closed this Oct 13, 2026
@nihalzp nihalzp reopened this Oct 13, 2026
@nihalzp
nihalzp added this pull request to the merge queue Oct 13, 2026
@nihalzp
nihalzp removed this pull request from the merge queue due to a manual request Oct 13, 2026
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]>
@nihalzp
nihalzp enabled auto-merge October 13, 2026 07:44
@nihalzp
nihalzp disabled auto-merge October 13, 2026 09:02
@nihalzp
nihalzp added this pull request to the merge queue Oct 13, 2026
Merged via the queue into ClickHouse:master with commit 002a17c Oct 13, 2026
171 of 176 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 13, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 13, 2026
robot-clickhouse-ci-1 added a commit that referenced this pull request Oct 14, 2026
Cherry pick #125463 to 26.8: Revert "Push filters below WindowStep on PARTITION BY columns"
robot-clickhouse added a commit that referenced this pull request Oct 14, 2026
nihalzp added a commit that referenced this pull request Oct 14, 2026
Cherry pick #125463 to 26.9: Revert "Push filters below WindowStep on PARTITION BY columns"
robot-clickhouse added a commit that referenced this pull request Oct 14, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #125463 to 26.8: Revert "Push filters below WindowStep on PARTITION BY columns"
clickhouse-gh Bot added a commit that referenced this pull request Oct 14, 2026
Backport #125463 to 26.9: Revert "Push filters below WindowStep on PARTITION BY columns"
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 15, 2026
…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]>
raimannma added a commit to raimannma/ClickHouse that referenced this pull request Nov 16, 2026
…uch a condition

The filter push-down does not pass a window any more (ClickHouse#125463), so neither a condition on the keys
nor a runtime join filter restricted the generated window, which read and sorted the whole table.
The pass now copies the deterministic conditions on the keys of the windows into the derived table,
where they remove whole partitions only, and rewrites only when there is such a condition. The rule
for conditions on tables joined on the keys, which relied on the runtime filter, is removed with its
tests; the other tests get a condition on the key, so they still check the rewritten query.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors ci-force-all Do not skip any jobs when running comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty IN (subquery) filter on a window PARTITION BY key no longer stops early, LEFT JOIN right side is built, about 5x slower (26.6 -> 26.8)

4 participants