Repository navigation
Conversation
…dai's patch from ClickHouse#128573, unchanged) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ngle-column queries a PREWHERE for it Reworks the previous commit (the patch from ClickHouse#128573): - The bound arms only on the PREWHERE path (`need_filter`), so that the values replaced by '' are dropped inside the reader. Without PREWHERE they leave the source, where `AddingDefaultsTransform` (a `DEFAULT` expression over the sort column) and filter-step nodes outside `filter_actions_dag` evaluate them: `CREATE TABLE t (s String, d UInt8 DEFAULT intDiv(100, length(s))) ENGINE = File(Parquet, ...)` and `SELECT s, d FROM t WHERE s != '' ORDER BY s LIMIT 10 SETTINGS optimize_move_to_prewhere = 0` threw `Division by zero` with the previous commit. - `MergeTreeWhereOptimizer` moves a conjunct that rejects '' on the source's statistics-only `String` top-K sort column to PREWHERE even when it covers every queried column, like 9e651db does for string value filters. `SELECT s FROM file(...) WHERE s != '' ORDER BY s LIMIT n` (ClickBench Q26) then gets a PREWHERE. `SourceStepWithFilterBase::getTopKFilter` exposes the info that `tryOptimizeTopK` set in the first optimization pass. - Who may read the column before the filter is decided by the allowed-readers rule of `extractStringValueFilters` (factored into `columnsWithOtherReaders`), via the new `filterReadsColumnOnlyInConjunctsRejectingEmptyString`, instead of a list of function names: `PREWHERE length(s) < 1000 AND s != ''` leaves the bound off. - A column written to the columns cache in at least one row group gets no bound, the rule master already applies to the string value filter (61767f9, 45d9bde). - The PLAIN bound lives in `PlainStringDecoderWithTopKBound`, so `PlainStringDecoder` is unchanged. - The dictionary mask is allocated when the dictionary is decoded (`initTopKMask`) and counted in `Dictionary::decodedFootprintUpperBound`, like `string_value_filter_mask`; the dictionary keeps only the version of the threshold it was built from, not the threshold. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lumn query to the perf test Co-Authored-By: Claude Opus 5.5 <[email protected]>
… the bound-off paths on base code Follow-up of the replan r1 (Gate A round 6), with the measurements of IMPL r1: - The bound now covers DELTA_LENGTH_BYTE_ARRAY and DELTA_BYTE_ARRAY pages, like the string filter of PREWHERE does. Otherwise the conjunct moved to PREWHERE for the bound costs the PREWHERE path on such files with nothing to gain. The per-value checks (threshold refresh every 4096 values, pause after a window of 256 checked values without a replacement) live in one helper, `TopKStringBoundChecker`, used by the PLAIN and both DELTA decoders. The bound-off decoders are the base classes. Measured on URL rewritten as DELTA_LENGTH_BYTE_ARRAY (hits_0..9, 4 CPUs): 0.767 [0.690, 0.854] of base. - No move to PREWHERE for the bound when the columns cache may be written: the reader keeps the values of a column it writes, and a top-K read outputs its sort column anyway, so the move only cost memory (+32% peak memory measured on Q26 with `use_columns_cache = 1`). - The dictionary records which per-entry mask it applies (`entry_mask`), so that `appendIndexes` and `appendRepeated` have one gate in front of both masks, and the top-K mask refreshes itself from inside its own branch. The bound-off dictionary path executes the base instructions (`perf stat` instructions:u within 0.1% of base on strings decoded in 1-row runs; before: +0.9%). The test gets results arms on the DELTA files and a DESC + OFFSET arm, counter arms for both DELTA encodings, and an EXPLAIN arm for the columns cache. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s top-K mask applies A single-step PREWHERE that reads only the sort column decodes its dictionary as `ColumnReplicated`, which bypasses the per-row gather where the top-K mask of the dictionary is applied. Do not use the replicated decoding for a column with the bound. Kept as a separate commit: measured on 4 CPUs, it gains nothing significant on ClickBench Q26 over the 100 partitioned files (0.961 [0.892, 1.034] of the previous commit) and costs 11% on Q26 over the single hits.parquet (1.109 [1.082, 1.136]); see impl-findings-r2. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Revert the previous commit. A single-step PREWHERE that reads only the sort column keeps decoding its dictionary as `ColumnReplicated`, which copies no string at decode time; the top-K mask of the dictionary then does not run for those subchunks, which is sound because they hold the real values. Measured on 4 CPUs, 14 rounds, with the reverted commit against the one before it: ClickBench Q26 over the single hits.parquet 1.109 [1.082, 1.136], Q26 over the 100 partitioned files 0.961 [0.892, 1.034]. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ir defects A tie on the sort key is only dropped wrongly when a copy of the threshold value arrives after the threshold reached it and wins the tie-break: `ORDER BY t, k DESC LIMIT 1` (and DESC) has that shape, the `LIMIT 6` arms do not. A top-K read writes a query condition cache entry only when its filter has a second conjunct, so the cache arm also reads `WHERE s != '' AND k % 3 != 0`. Checked with mutations: a non-strict comparison with the threshold now fails the tie arms on all four encodings, and without the all-matched verdict in `getMatchedBuckets` the plain count returns 12000 rows instead of 13333. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…esult is used elsewhere `WITH notEmpty(s) AS ok ... PREWHERE throwIf(NOT ok) = 0 AND ok ORDER BY s LIMIT n` shares the `notEmpty(s)` node, so `throwIf` saw the replaced '' through it and threw, while the readers check only looked at direct readers of `s`. The bound now requires every PREWHERE node that depends on the column to be part of the filter's conjunction; `extractStringValueFilters` is back to master's code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…p-k-string-decode-bound Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten or remove the comments of the top-K String bound that narrate its own design; keep the invariants. 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; 3 rounds, 2 findings, 0 on the last two heads) plus my own review (4 nits).
Also noticed: master's scan-time string filter (#115848) has the same gap as finding 1 when a column has two conditions ( Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261102-201400 |
|
cc @grantholly-clickhouse @antaljanosbenjamin, could you review this and add the |
|
Workflow [PR], commit [f6803eb] Summary: ✅
AI ReviewSummaryThis PR avoids copying Parquet Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested; normal CI validation remains required. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes60 object files changed (+68.77 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units236 translation units recompiled, 2694 s compile time in total, 236 of them have a recent master baseline. |
`Prefetcher::pickRangesAndCreateTaskLocked` coalesces past cancelled ranges and then wrote `task` and `task_offset` for every range it covered, cancelled ones included. A cancelled request may have had a task: `PrefetchHandle::reset` reads `request->task` after the exchange to `Cancelled`, without the mutex, so that write is a data race, and when the write lands first, `reset` can decrement the new task's refcount instead of the old one's (the new buffer can be freed while a covered request still reads it, the old one is never released). The top-K dictionary page range (a second range at the dictionary page's start, read alone and cancelled at the end of the dictionary stage) makes this reachable: ThreadSanitizer reported it on 05474_parquet_top_k_string_dictionary_bound in `Stateless tests (amd_tsan, flaky check)`. A local stress of that test's multi-threaded queries on a TSan build reproduced it in 23 of 2682 runs before this change and 0 of 6720 after. Co-Authored-By: Claude Opus 5.5 <[email protected]>
It ran longer than 180 s in the flaky checks (185-267 s), where the thread fuzzer is active in every clickhouse-local the test starts, so the cost is per query and per row group read. The files now have 8 row groups of 500 rows instead of 20 of 1000, and the queries whose plan does not depend on the encoding (explicit schemas, the DEFAULT tables, the filters that arm or do not arm the bound) run once instead of once per file. Every encoding still runs the decoding arms: OFFSET, values '' in the file, ties at the threshold and a second filter step. One clickhouse-local runs all of them, 49 queries in each mode instead of 93. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Prefetcher::pickRangesAndCreateTaskLocked` coalesces past cancelled ranges and then wrote `task` and `task_offset` for every range it covered, cancelled ones included. A cancelled request may have had a task: `PrefetchHandle::reset` reads `request->task` after the exchange to `Cancelled`, without the mutex, so that write is a data race, and when the write lands first, `reset` can decrement the new task's refcount instead of the old one's (the new buffer can be freed while a covered request still reads it, the old one is never released). The top-K dictionary page range (a second range at the dictionary page's start, read alone and cancelled at the end of the dictionary stage) makes this reachable: ThreadSanitizer reported it on 05474_parquet_top_k_string_dictionary_bound in `Stateless tests (amd_tsan, flaky check)` of the stacked PR ClickHouse#128757. A local stress of that test's multi-threaded queries on a TSan build of this branch reproduced it in 12 of 1920 runs before this change and 0 of 6400 after. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
On current master (b8c902a, which includes #128290), this PR cuts the time of ClickBench Q26 ( The PR no longer merges cleanly:
Q38 hot is unchanged. No other query is significantly slower in either metric; the geometric mean of the 43 ratios is 0.994 hot and 1.002 for the first run. Neither Q15 nor Q38 has a top-K filter, so this PR's bound is never set for them; I have not investigated them further. Q26's result, and every query's full result with Conflict resolution#129882 adds
Master from b8c902a to f9e6c60 does not change the Parquet reader, PREWHERE or the Method and testsMaster b8c902a was compared with this PR's head 8e3b1d2 merged into it using the resolution above. Both are Release builds (clang 21, no LTO) on an r6i.8xlarge (Xeon 8375C). The server and client are pinned to cores 0-15, with For each round, query and build, the server was restarted and the query run 3 times, as in ClickBench: the first run is run 1 (the files stay in the page cache) and hot is the best of runs 2 and 3. Build order alternated between rounds. There were 8 rounds and the first was dropped. The table gives medians of the 7 paired rounds, with 95% Student-t intervals on the paired log ratios. Using |
…p-k-string-decode-bound Resolve conflicts with ClickHouse#129882 and ClickHouse#126530: `optimize` takes both `top_k_filter_column` and `top_k_string_sort_column`, the step predicates keep both conditions, and `get_columns_written_to_cache` returns master's `VectorWithMemoryTracking<bool>`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Resolved in f6803eb by merging master ( |
Closes: #128573
Related: #128290
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ORDER BY <String column> LIMIT noverParquetfiles with a filter that rejects the empty string (such asWHERE URL != '') no longer copies the values that are already beyond the top-K threshold: ClickBenchORDER BY URL LIMIT 10andORDER BY Title LIMIT 10over the 100 Parquet files are about 40% faster with 4 threads, Q26 about 13%. Closes #128573.Description
Patch from @ snelheidai's #128573, reworked; measurements are mine.
The partial sort above the reader discards almost every decoded value of the sort column. When that column is a
Stringread without conversion, and PREWHERE needs it in its first step and reads it only in conjuncts that reject'', whose results only feed the filter, the PLAIN,DELTA_LENGTH_BYTE_ARRAYandDELTA_BYTE_ARRAYdecoders write''for a value strictly beyond the threshold (ties are kept), and dictionary pages use a per-entry mask; PREWHERE drops those rows before anything else reads the column. A single-columnWHERE s != ''of such a read is moved to PREWHERE. While the bound is active, the query condition cache sees row groups as matched. With the columns cache enabled (#123890) neither the move nor the bound applies.Release, 4 CPUs, same results (time ratio [95% CI]; peak memory ratio):
URL <> '' ORDER BY URL LIMIT 10Title <> '' ORDER BY Title LIMIT 10URLasDELTA_LENGTH_BYTE_ARRAYPREWHEREalone: 1.03 (URL), 1.04 (Title). Costs: Q26 over the single 14 GBhits.parquetis 0.919 [0.902, 0.937], where the PREWHERE alone would be 0.745, with peak memory 61 -> 99 MB; on reverse-ordered files that statistics cannot skip, the moved PREWHERE costs 1.07-1.14x and, with a dictionary, one mask build per column chunk another 1.03x. Test05490_parquet_top_k_string_decode_boundfails on master.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128757]