Repository navigation
Evaluate PREWHERE over a Memory table in multiple steps - #122604
Conversation
A conjunction in `PREWHERE` is now split into steps with `tryBuildPrewhereSteps`, like `MergeTreeSelectProcessor` does under `enable_multiple_prewhere_read_steps`. Each step reads its columns only for the rows that passed the previous steps, so a condition over a wide column (e.g. `URL <> ''` next to `CounterID = 62`) no longer decompresses that column for every block of a `compress = true` table. The result is assembled by name in the order of the output header. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The steps of a `Memory` table filter the block physically, so a stateful function such as `runningConcurrency` or `rowNumberInBlock` would only see the rows that passed the preceding conditions (`MergeTree` keeps intermediate steps unfiltered unless the next condition may throw). This broke `04872_running_concurrency_join_conversion_wrong_results`. Also fill the reference of `05289_memory_prewhere_multiple_steps`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [f23c323] Summary: ✅
AI ReviewSummaryThis PR teaches Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit f23c323.
Changed lines: Changed C/C++ lines covered: 87/87 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 25 s compile time in total, 8 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
The query added earlier filtered on `s3 != ''` without selecting `s3`, so the optimizer rewrote the condition to `s3.size != 0`, which reads only the cheap `size` subcolumn, and the query did not exercise the change (28.4 -> 28.7 ms in the performance check). Use the same query as #123155, which selects the column it filters on, so that the two PRs make an identical change to the file. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…for `Memory` `Memory` filters the block physically between `PREWHERE` steps, so a function that is non-deterministic in scope of the query without being stateful, such as `blockSize` or `blockSerializedSize`, would see only the rows that passed the preceding conditions and change the result depending on `enable_multiple_prewhere_read_steps`. Gate the split with `hasNonDeterministicOrStatefulFunctions` instead of `hasStatefulFunctions`. Address review: #122604 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
The master merge resolution of `read_missing_columns` passed `std::move(*column_it)` into the `ColumnWithTypeAndName` braced initializer, whose only matching constructor takes `const ColumnPtr &`, so the move was a no-op and `Build (arm_tidy)` failed. Pass the column directly. Also correct two comments that the read-time evaluation of `DEFAULT` expressions made inaccurate. `StorageMemory::supportedPrewhereColumns` claimed the in-source filter reads the columns of all its steps before running the first one, which no longer holds since ClickHouse#122604 (each step reads its inputs only for the rows that passed the previous ones). `supportsTopKDynamicFilter` said the source fills such a column with the defaults of the type; it now evaluates the expression, and the exclusion is still needed because the threshold step runs before the row-level security filter: with it lifted, a user restricted by `USING k = 1` gets `Cannot parse string 'secret'` from `ORDER BY d LIMIT 1` over `d UInt64 DEFAULT toUInt64(secret)`, quoting a hidden row. Both exclusions are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since ClickHouse#116248 and ClickHouse#122604, a WHERE over a `Memory` table is moved to PREWHERE and split into steps by `tryBuildPrewhereSteps`, so the `Memory` cases of this test no longer reach the filter-step split they were written for. With `allow_reorder_prewhere_conditions = 0`, the steps compute `enabled = 1` (the ON copy) and `toNullable(enabled) = 1` (the WHERE copy) separately under one column name, and the read fails with AMBIGUOUS_COLUMN_NAME. Which written order reaches that depends on the conjunct order, which this PR changes. That failure is a defect of the PREWHERE split, not of the filter step: master fails the same way when the ON condition is written first, and 26.8.21.12 fails with `MergeTree` tables. It is left to a separate fix. Failed run: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122160&sha=4c1aab48fb2eaede08abeddd7e866d076c948214&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
PREWHEREover aMemorytable (added in #116248) evaluated the whole conjunction as one step. So every column used by any condition was decompressed for every block before anything was filtered. For acompress = truetable, a cheap condition on a wide column cancelled the gain. In ClickBench Q37,URL <> ''next toCounterID = 62made the source decompress the wholeURLcolumn, even thoughCounterID = 62matches rows in only 18 of 1,229 blocks. This is why theclickhouse-memoryentry in ClickHouse/ClickBench#1590 showed no improvement from #116248.Now the conjunction is split into steps with
tryBuildPrewhereSteps, the same codeMergeTreeuses underenable_multiple_prewhere_read_steps. Each step reads its columns only for the rows that passed the previous steps, and blocks where no row passes are skipped without reading the columns of the later steps. The result is assembled by name in the order of the output header, as inMergeTreeSelectProcessor.A PREWHERE containing a stateful function (
runningConcurrency,rowNumberInBlock, ...) or a function that is non-deterministic in scope of the query (blockSize,rand, ...) stays in a single step.Memorysteps filter the block physically, so such a function would otherwise see only the rows that passed the preceding conditions.MergeTreeavoids this because its intermediate steps stay unfiltered unless the next condition may throw.04872_running_concurrency_join_conversion_wrong_resultscovers this.ClickBench, 100M rows loaded into
Memory SETTINGS compress = truewith the benchmark'sINSERT ... SELECT FROM file(), aarch64. Same binary, onlyenable_multiple_prewhere_read_stepstoggled. Median of 5 runs, on a heavily loaded shared machine; CPU time comes fromUserTimeMicroseconds + SystemTimeMicroseconds.Queries without a conjunction are not affected. A query of this shape is added to
tests/performance/memory_prewhere.xml. It is the same change as #123155, which adds the query on its own to give a baseline on master.Related: #116248
Related: ClickHouse/ClickBench#1590
Related: #123155
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
PREWHEREover aMemorytable is evaluated in multiple steps, like forMergeTree(enable_multiple_prewhere_read_steps): the columns of each next condition are read only for the rows that passed the previous ones. For a table withcompress = true, a condition on a wide column next to a selective one no longer decompresses that column for every block, which makes such queries up to several times faster.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122604]
Version info
26.10.1.1321-master(included in26.10and later)