Repository navigation
Use the top-K dynamic filter for ORDER BY of a single String column on MergeTree - #125122
alexey-milovidov merged 5 commits into
Conversation
…n MergeTree Since ClickHouse#104216 `use_top_k_dynamic_filtering` is off for variable-length sort columns: a dominating value that is the threshold (e.g. mostly empty strings) keeps most rows, so the filter only adds cost. The new setting `use_top_k_dynamic_filtering_for_single_string_sort_column` (default on) enables it for a single `String` sort column of a `MergeTree` read (also inside `Nullable`). Once most rows of a block are equal to the threshold, `__topKFilter` also drops those rows, which keeps the result for a single sort column. A tracker version limits the tie count to once per threshold value. From then on the read records nothing in the query condition cache: a granule emptied that way may hold rows of the result, and the cache is a union over runs, so overlapping runs that keep different tied rows together mark every tied granule (measured with strict reads recorded: `ORDER BY s LIMIT 1` returned 0 rows in 19/20 cycles of 16 concurrent clients). `MergeTreeSelectProcessor::readCurrentTask` loads the per-read flag after the PREWHERE of the block ran. A sparse argument is compared through its values instead of being converted to a full column. ClickBench `hits` (100M rows), release, x86, first run vs master at 4 threads: `MobilePhoneModel LIMIT 300` 0.69x, `HitColor LIMIT 10` 0.71x, `PageCharset LIMIT 10` 0.65x, `Title DESC` 0.84x; q26 1.06x with the query condition cache (1.00x at 32 threads or without it), its repeat 318 -> 27 ms. Two-column, q24, q27, GROUP BY and fixed-length controls are unchanged. `Nullable(String)` copies of the columns: `Title DESC` 0.45x, `SearchPhrase <> ''` 0.77x, `MobilePhoneModel LIMIT 300` 0.84x. `LowCardinality` columns are not included: `__topKFilter` is not deterministic in the scope of a query, so the default implementation converts such an argument to a full column on every block, and the filter cost more than it saved (`PageCharset LIMIT 10` 1.97x, `MobilePhoneModel LIMIT 300` 1.08x). Closes ClickHouse#125075 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop comments restating the new Mode enum, compare() and the optimizeTopK gate; shorten the isDeterministicAllowingTopKFilter note to one line. 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) plus my own cold review, three rounds; the last round had no new findings from the model.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261010-235800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261010-232300 |
|
Workflow [PR], commit [b760476] Summary: ✅
AI ReviewSummaryEnables top-K dynamic filtering by default for a single Final VerdictStatus: ✅ Approve. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR extends top-K dynamic filtering in PREWHERE to a single String ORDER BY column read from a MergeTree table, and drops rows equal to the threshold when they dominate a block. The only flagged query, clickbench Q33 (-5.9%), was correctly left as no change: its 988.5 ms median falls inside master's recent range of 960-1243 ms, its history is noisy, and CPU time barely moved. It also never runs the changed path, because it sorts by an aggregate and not by a String column. No ClickBench query that sorts by a single String column (for example ORDER BY SearchPhrase LIMIT 10) was flagged in either direction, so this run shows no measurable effect from the PR. clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units413 translation units recompiled, 3765 s compile time in total, 413 of them have a recent master baseline. |
…string-sort-column Kept both sides of the conflicts: in `FunctionTopKFilter::executeCompare`, the per-`Mode` comparison function together with master's `threshold_type` conversion. In `ITopKThresholdTracker`, master's `data_type` member together with this branch's `version` member. In `tests/clickhouse-test`, master removed the dependency of the now-obsolete `use_statistics_cache` setting. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ing_sort_column` to 26.11 `master` is now 26.11, so a new setting must be recorded in the 26.11 block; otherwise `compatibility = '26.10'` would enable it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…loned With `correlated_subqueries_use_in_memory_buffer = 0`, `materializeQueryPlanReferences` clones the referenced subplan. `SortingStep::clone` and `ReadFromMergeTree::clone` share the threshold tracker, so several sorts publish to one threshold. A threshold can then be backed only by the rows of another copy, and dropping the rows equal to it can leave a copy without rows: `SELECT s, (SELECT count() FROM numbers(3) WHERE toString(number) >= q.s) FROM (SELECT s FROM t ORDER BY s LIMIT 10) AS q` returned NULL for the subquery over a column of empty strings. `ReadFromMergeTree::clone` now marks the tracker as shared, and `__topKFilter` keeps the rows equal to the threshold for such a tracker, as it does for every other sort. Reads that are not cloned (the default, buffered plan) are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #125075
Related: #104216
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ORDER BY <column> LIMIT non aMergeTreetable now uses the top-K dynamic filter when the only sort column is aString(also insideNullable), controlled by the new settinguse_top_k_dynamic_filtering_for_single_string_sort_column(enabled by default). When most rows of a block are equal to the threshold, as in a column of mostly empty strings, the filter also skips those rows. For example, ClickBenchWHERE SearchPhrase <> '' ORDER BY SearchPhrase LIMIT 10repeated with the query condition cache is about 12 times faster.Description
Since #104216 the filter is off for variable-length sort columns: with
ORDER BY MobilePhoneModel LIMIT 300(94% empty) the threshold becomes''andMobilePhoneModel <= ''keeps every empty row. Such queries read and sort all rows on every run.For a single
StringorNullable(String)sort column the filter starts as before (<=/>=). Once most rows of a block are equal to the threshold, it drops them too for the rest of the read, unless the read is cloned (correlated_subqueries_use_in_memory_buffer = 0). With one sort column every published threshold is then-th ofnrows that stay in the sort, so the sort keys of the result do not change.GROUP BY key LIMIT n, Parquet,Memory,LowCardinality, more sort columns and fixed-length types keep today's behavior. A sparse argument is compared through its values instead of being converted on every call.After that switch the read records nothing in the query condition cache. Such a granule may hold rows of the result, and the cache is a union over runs, so overlapping runs that keep different tied rows would mark every tied granule (with those granules recorded, 16 concurrent
ORDER BY s LIMIT 1returned 0 rows in 19/20 cycles; 0/20 here).ClickBench
hits(100M rows, Release, 8 paired rounds, geomean vs master), first run at 4 threads:MobilePhoneModel LIMIT 3000.69x,HitColor LIMIT 100.71x; q26 repeat with the cache 318 -> 27 ms, q26 first run 1.06x [1.02, 1.10] (1.00x at 32 threads or without the cache). Two-column, q24, q27,GROUP BYand fixed-length controls are flat.Nullable(String)copies:Title DESC0.45x,SearchPhrase0.77x,MobilePhoneModel0.84x.LowCardinalityis excluded: the filter materializes it on every block (PageCharset LIMIT 101.97x).I can fold this into
use_top_k_dynamic_filteringwithout a new setting if you prefer.Idea from #125075.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125122]
Version info
26.11.1.706-master(included in26.11and later)