Repository navigation
Fix TopK dynamic filtering on a projection that stores _part_offset and on a column named __topKFilter(...) - #122486
Conversation
`installTopKDynamicFilter` merges `__topKFilter(<sort column>)` into the read's PREWHERE in the second optimization pass. It gave a wrong answer in two cases: - A normal projection that stores `_part_offset` takes over the TopK stamp, but its read produces the column as `_parent_part_offset` and renames it above the read. The filter's `_part_offset` input is not in the read's block, so `ORDER BY _part_offset ... LIMIT` over such a projection threw `NOT_FOUND_COLUMN_IN_BLOCK` (ClickHouse#122469). Refuse when the read does not read the stamp's sort column, the predicate `tryOptimizeTopK` checks when it stamps the read. Translating the column is not an option: a `_parent_part_offset` condition in the projection PREWHERE together with lazy materialization returns wrong values, which is what 26.8 and 26.9 return for this query. - In the merged-PREWHERE branch the generated node was only checked against the PREWHERE DAG. A stored column of the same name (`__topKFilter(k)`) is read after PREWHERE, and the reader does not read a column that a PREWHERE step already computes, so the column read back as the filter's 1s (ClickHouse#122470). Also refuse when the read's header has that name, as the no-PREWHERE branch does. Both are refusals, so the worst case is a missed optimization.
State the projection-read rename as the invariant it is: only a projection that stores `_part_offset` reads it as `_parent_part_offset`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; one full pass; 0 findings), plus my own cold review (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20260926-230900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260926-222100 |
|
Workflow [PR], commit [1758228] Summary: ✅
AI ReviewSummaryThe PR prevents invalid TopK PREWHERE installation when a projection renames Missing context / blind spots
Final VerdictStatus: ✅ Approve |
CI finish ledger - 1f8fe49Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 169 check runs completed (129 pass, 39 not selected, 1 red), Session id: cron:our-pr-ci-monitor:20260927-090146 |
|
Fixing PR for the AST fuzzer (amd_debug, targeted) 'Equal values are not contiguous' (STID: 2508-348f) row of the ledger above: #122520 |
# Conflicts: # src/Processors/QueryPlan/Optimizations/optimizeTopK.cpp
|
#122520 should be merged first |
|
Agreed. The |
Backport #122486 to 26.10: Fix TopK dynamic filtering on a projection that stores `_part_offset` and on a column named `__topKFilter(...)`
… that stores `_part_offset` and on a column named `__topKFilter(...)`
Closes: #122469
Closes: #122470
Related: #117968
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results and
NOT_FOUND_COLUMN_IN_BLOCKerrors inORDER BY ... LIMITqueries withuse_top_k_dynamic_filtering(enabled by default) when the query sorts by_part_offsetand is served by a projection that stores_part_offset, and when the table has a column named__topKFilter(<sort column>).Description
On 26.8.10.6 and 26.9.1.1629, at default settings,
SELECT _part_offset, a, b FROM t WHERE b < 5000 ORDER BY _part_offset LIMIT 3over a projectionp (SELECT a, b, _part_offset ORDER BY b)returns wronga(13 29827 2947instead of13 13 2947), and on a table with a column__topKFilter(k)the querySELECT k, `__topKFilter(k)` AS x FROM t WHERE pred = 3 ORDER BY k, x LIMIT 3fails withNOT_FOUND_COLUMN_IN_BLOCK. Since #117968 the first query throws and the second returns1for that column.Both come from
installTopKDynamicFilter, which merges__topKFilter(<sort column>)into the read's PREWHERE:_part_offsetproduces it as_parent_part_offset, but it takes over the TopK stamp, so the filter's input is not in its block. The install now refuses when the read does not read the sort column, the checktryOptimizeTopKmakes when it stamps the read. Binding the filter to_parent_part_offsetis what 26.8/26.9 do, and it returns the wrong values above.__topKFilter(k)node was checked only against the PREWHERE DAG. The reader does not read a stored column whose name a PREWHERE step computes, so the stored__topKFilter(k)was replaced by the filter's result. The install now also refuses when the read's header has that name, as the branch without an existing PREWHERE already did.Both are refusals, so the worst case is a missed optimization. The fix should follow #117968 to the branches it is backported to.
The new test fails on master and passes here in 50 randomized runs; removing either check turns its own section red. A separate, TopK-independent case of wrong values with a
_part_offsetcondition, a projection and lazy materialization is not changed here.Workflow [PR]
Sync PR [sync-upstream/pr/122486]
Version info
26.11.1.474-master(included in26.11and later)