Repository navigation
Trim the comments and tests of TopN dynamic filtering over PREWHERE - #122671
Conversation
A follow-up to ClickHouse#117968 with no behavior change, asked for in ClickHouse#117968 (comment) The `src/` diff is comment-only: with comments stripped, all six files are token-for-token identical to master. The comments ClickHouse#117968 added are cut to local invariants (60 lines to 24). The design notes they carried: - `installTopKDynamicFilter` runs after `optimizePrewhere`, after projection replacement and after `optimizeReadInOrder`: each changes the PREWHERE the filter joins, and reading in order decides whether it is added at all. - A block-dependent PREWHERE is refused in `tryOptimizeTopK`, before the read is stamped: `applyParallelReplicas` runs before the install and keeps a stamped read local, so a refusal only at install time lost plan-based parallel replicas and added no filter (81a8069). - The conjunction is built inside the cloned PREWHERE DAG. Building it apart and folding it in with `ActionsDAG::mergeNodes` binds it to any existing node under the generated column's name, which returned wrong results for a stored `__topKFilter(k)` column (73ecf4a). - The conjunction stays flat with the threshold filter first: `MergeTreeSplitPrewhereIntoReadSteps` turns the direct children of the root `and` into read steps, in order, so a nested `and` would collapse a multi-condition PREWHERE into one step, and the threshold filter reads only the sort column. - `dynamic_filter_pending` lives in `TopKFilterInfo`, not in `ReadFromMergeTree`, because a plan can be cloned between the pass that sets it and the pass that installs, and the clone carries `top_k_filter_info` over. The clone copies it instead of calling `setTopKColumn` because `condition_hash` already has the part-set salt folded in. - The re-entrancy check in `tryOptimizeTopK` exists because a second run would install a second `__topKFilter` and fold the salt into `condition_hash` twice. - `createInternalFunctionTopKFilterResolver` is used instead of `FunctionFactory::get` because the resolver needs the threshold tracker, and `updatePrewhereInfo` rebuilds the read's header, hence the converting step after the install. No code is deleted. Two comments claimed that `__topKFilter` cannot reach the WHERE `filter_actions_dag` (the stripping in `getTopKReusePredicateOnlyConditionHash`, and `isDeterministicAllowingTopKFilter` in `selectRangesToRead`). That is not shown: projection candidates build a new `filter_actions_dag` from the read's current PREWHERE (`QueryDAG::build`), and a plan optimized twice (`StorageMerge` child plans, set subplans) re-runs projection analysis over a PREWHERE that already holds `__topKFilter`. So both stay, and only the two comments are removed. The second `dependsOnItsBlock` check in `buildTopKDynamicFilterPrewhere` stays too: it covers a PREWHERE promoted after the read was stamped. Tests: `05059_top_k_dynamic_filter_shares_prewhere` goes from 365 to 155 lines and its reference from 43 to 28 lines (old lines 4 7 8 9 12-21 23-25 29-34 36 37 40 42 43, in order). Every kept query is unchanged up to whitespace. Dropped: EXPLAIN checks implied by a kept one (explicit PREWHERE, single condition, promoted WHERE, projection has the filter, unsorted projection with PREWHERE, the two partial-projection fixtures and the full-coverage count), sensitivity controls whose job a kept positive control does (`pred < 1`, `optimize_use_projections = 0` and filter-off on the unsorted projection), and result pairs covered by kept ones (`PREWHERE pred = 3`, `rowNumberInBlock`, lazy materialization with the filter off). Every refusal keeps a positive control on the same table. In 03711, 04539, 04628 and 04838 only the comment lines ClickHouse#117968 wrote change, and none of the test comments ClickHouse#117968 wrote names C++ internals any more. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Start the TopK comment in ReadFromMergeTree::clone with "Carry over the TopK marker", so the next comment's "for the same reason" refers to it again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pre-publication review: no findings from an independent model, two nits from my own review, both fixed
Session id: cron:clickhouse-review-slot-11:20260928-114100 |
|
Workflow [PR], commit [841692e] Summary: ✅
AI ReviewSummaryThis PR trims comments in the TopK/PREWHERE implementation and removes a large amount of test scaffolding from Tests
Final Verdict
LLVM Coverage ReportMeasured on commit 841692e.
Changed lines: Changed C/C++ lines covered: 46/48 (95.83%) · Uncovered code |
| SELECT count() > 0 FROM ( | ||
| EXPLAIN actions = 1 | ||
| SELECT k FROM t_topk_prewhere PREWHERE pred = 3 AND tag = 't2' ORDER BY k LIMIT 10) | ||
| SELECT count() > 0 FROM (EXPLAIN actions = 1 SELECT k FROM t_topk_prewhere PREWHERE pred = 3 AND tag = 't2' ORDER BY k LIMIT 10) |
There was a problem hiding this comment.
The reduced test only exercises the explicit-PREWHERE conjunction case (pred = 3 AND tag = 't2'), but buildTopKDynamicFilterPrewhere still has a separate branch for a lone predicate versus an existing and. The removed PREWHERE pred = 3 EXPLAIN was the only direct check of that single-condition path: the surviving WHERE coverage lives in 03711, and the other PREWHERE TopK tests assert QCC behavior rather than __topKFilter placement. If the lone-PREWHERE branch stopped sharing __topKFilter with the existing condition, the rows would still be the same and this trimmed file would stay green. I think we should keep one focused EXPLAIN for PREWHERE pred = 3 ORDER BY k LIMIT 10.
09baa85
|
@groeneai 05059_top_k_dynamic_filter_shares_prewhere is flaky (https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=852f246d984ac2436801f4038810d7757f748b54&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%203%2F3%29&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%203%2F3%29). Check if it's related to these changes and fix it please |
|
It is not caused by this PR: its It is a test defect. The fix pins that comparison to one stream ( |
|
I opened the fix in #123123: it adds |
@groeneai Stop using the term arm "arm". An "arm" either refers to a body part or to military equipment for killing people. Instead of "arm", use terms "variant", "path", "lane" etc. depending on the context. Add this to your memory. |
|
Noted and recorded in my memory; from now on I will say "variant", "path" or "case" instead. |
Related: #117968
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
A follow-up to #117968 with no behavior change, as promised in #117968 (comment).
src/: the comments Let TopN dynamic filtering share PREWHERE, with__topKFilterfirst #117968 added are cut to the invariants a reader needs at that spot (60 lines to 24). Design history, refuted alternatives and cross-file explanations are gone. Thesrc/diff is comment-only: with comments stripped, all six files are token-for-token identical to master.__topKFilternever reachesgetTopKReusePredicateOnlyConditionHashor the determinism check inselectRangesToRead(a plan that is optimized twice can re-run projection analysis over a PREWHERE that already holds it), so both stay, and only the comments claiming it are removed.05059_top_k_dynamic_filter_shares_prewheregoes from 365 to 155 lines. It keeps one on/off result comparison per behavior and theEXPLAINchecks that show whether the filter is added. Every kept query and reference line is unchanged. The test comments Let TopN dynamic filtering share PREWHERE, with__topKFilterfirst #117968 wrote in03711,04539,04628,04838and05059no longer name C++ internals.Validation: the five tests pass with randomized settings (
0505950 times, the others 10 times each). The old and the new05059both pass on this build and on a master build. Withuse_top_k_dynamic_filtering = 0, exactly the six checks that show the filter is added flip.Workflow [PR]
Sync PR [sync-upstream/pr/122671]
Version info
26.10.1.1069-master(included in26.10and later)