Repository navigation
Add lock free topk threshold tracker for numeric types. - #116512
Conversation
|
Workflow [PR], commit [aeb1475] Summary: ✅
AI ReviewSummaryThis PR replaces the shared-mutex Missing context / blind spots
Findings
Tests
Final Verdict❌ Changes requested — the PR still routes float sort keys into MergeTree top-k pruning paths that can return wrong results around LLVM Coverage ReportMeasured on commit aeb1475.
Changed lines: Changed C/C++ lines covered: 73/76 (96.05%) · Uncovered code |
`createTopKThresholdTracker` routes floating point sort keys to the lock-free `TopKThresholdTrackerNumeric`, whose boundary logic is specific to floats: a `NaN` boundary is never published as the threshold, and the initial threshold is `+-inf` rather than the maximum finite value. Compare the optimized and the unoptimized result for `Float64` / `Float32` data containing `nan`, `inf` and `-inf`, for the default `NULLS LAST` ordering and for `NULLS FIRST`, which flips where `NaN` sorts. A second table covers the case where the whole top-k consists of infinities, so the published threshold equals the initial sentinel and must not exclude the values it was reached by. Both the `__topKFilter` prewhere path and the `isValueInsideThreshold` granule-skipping path are exercised. Requested in ClickHouse#116512 (comment) Co-Authored-By: Claude Opus 5 <[email protected]>
|
This is great, thank you! Ive seen some worst-case manycore TopK queries spinning 50% of CPU time on RWLock cache line ping-ponging. I suppose this cuts the number of atomic operations in at least half, and significantly less when a good threshold is already established :) |
|
📊 Cloud Performance Report ✅ AI verdict: This PR splits the top-K threshold tracker into a lock-free version for numeric sort keys and a mutex-based version for everything else. Only clickbenchFlagged queries (4 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. 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 Object file sizes33 object files changed (+3.50 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units299 translation units recompiled, 3024 s compile time in total, 299 of them have a recent master baseline. |
The minmax index skips nan unless the whole granule is nan, and the top-k mark preselection ranks granules via raw Field comparison which orders nan above +inf. This produced wrong results in three ways: - a granule holding nan next to finite values is indistinguishable from one without nan, so nan-first orderings (NULLS FIRST) could drop the granule that must supply the leading nan rows; - an all-nan granule (min = max = nan) ranked as the best candidate for DESC with the default NULLS LAST, crowding out the granules with the actual finite extremes. Fix: disable the skip-index top-k path for float sort columns with nulls_direction != direction (plumbed through TopKFilterInfo), and rank an all-nan granule as the worst - still selectable - candidate by substituting its ranking value with +-inf. Review: ClickHouse#116512 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
…S FIRST Once a finite threshold is published, __topKFilter sent non-nullable floats through the vectorized path, where plain lessOrEquals / greaterOrEquals return 0 whenever either side is nan, so nan rows arriving in later blocks were filtered out even though NULLS FIRST sorts them before every finite value. Route such floats through the nan-aware compareColumn path that respects nulls_direction. ClickHouse#116512 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
eb9fb4a to
7053cb4
Compare
|
It went too far with fixes for nans which are unrelated to changes in this PR. Let's fix them separately: #116705. |
Derive the "not set" state from `threshold` itself: `testAndSet` can never store the sentinel initial value (a candidate equal to it fails the strict comparison and NaN returns early), so `threshold != sentinel` means a real value was published. With a single atomic there is no separate publication to order, which fixes the race where a reader could observe `isSet` as true while still loading the sentinel from `threshold`. Addresses review comment: ClickHouse#116512 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
Pin `use_top_k_dynamic_filtering` explicitly to both 0 and 1 in `top_k_threshold_tracker.xml` so each query deterministically measures a single path, per review comment ClickHouse#116512 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
|
Dear @shankar-iyer, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
…rmat top-K path The Parquet top-K dynamic filtering path merged from master still forward-declared the concrete `TopKThresholdTracker` in `FormatFilterInfo.h` and created it with `std::make_shared<TopKThresholdTracker>` in `optimizeTopK.cpp`. Point the alias to `ITopKThresholdTracker` and create the tracker with `createTopKThresholdTracker`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=116512&sha=d7973245fdf74b761aed20ae7a12eecb3d7ba512&name_0=PR PR: ClickHouse#116512 Co-Authored-By: Claude Opus 5.5 <[email protected]>
With a finite threshold, `__topKFilter` drops `nan` rows under `NULLS FIRST` (preexisting bug, ClickHouse#116705). The test hit it in the ParallelReplicas + s3 storage job: the coordinator handed out the preselected granules in two requests, the first of which published a finite threshold before the granules with `nan` were read. Keep `NULLS FIRST` only on `tab_sentinel`, where every block's top-k is `nan`, which the numeric tracker never publishes. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=116512&sha=8a66b58c628844ce9ed5b167dd080478cfc30b16&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 PR: ClickHouse#116512 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Resolve the conflicts of the 26.9 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from #116512, which on master split `TopKThresholdTracker` into `ITopKThresholdTracker` with the lock-free `TopKThresholdTrackerNumeric` and `TopKThresholdTrackerGeneric`, created by `createTopKThresholdTracker`. 26.9 has the single `TopKThresholdTracker` struct, and #116512 is not marked for backport, so the resolution keeps the 26.9 tracker and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`), as `ITopKThresholdTracker` does on master. - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.9 does not have. - `FunctionTopKFilter.cpp` merged without conflicts (the hunks of #125221 verbatim). - `TopKThresholdTracker.cpp` keeps the 26.9 version: its conflicting hunks are #116512's. - The test drops its `GROUP BY` query and that query's three reference lines, because it uses `enable_group_by_top_k_dynamic_filtering`, which 26.9 does not have. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflicts of the 26.8 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from #116512, which on master split `TopKThresholdTracker` into `ITopKThresholdTracker` with the lock-free `TopKThresholdTrackerNumeric` and `TopKThresholdTrackerGeneric`, created by `createTopKThresholdTracker`. 26.8 has the single `TopKThresholdTracker` struct, and #116512 is not marked for backport, so the resolution keeps the 26.8 tracker and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`), as `ITopKThresholdTracker` does on master. - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.8 does not have. - `FunctionTopKFilter.cpp` merged without conflicts (the hunks of #125221 verbatim). - `TopKThresholdTracker.cpp` keeps the 26.8 version: its conflicting hunks are #116512's. - The test drops its `GROUP BY` query and that query's three reference lines, because it uses `enable_group_by_top_k_dynamic_filtering`, which 26.8 does not have. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflicts of the 26.7 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from #116512, which on master split `TopKThresholdTracker` into `ITopKThresholdTracker` with the lock-free `TopKThresholdTrackerNumeric` and `TopKThresholdTrackerGeneric`, created by `createTopKThresholdTracker`. 26.7 has the single `TopKThresholdTracker` struct, and #116512 is not marked for backport, so the resolution keeps the 26.7 tracker and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`), as `ITopKThresholdTracker` does on master. - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.7 does not have. - `FunctionTopKFilter.cpp` merged without conflicts (the hunks of #125221 verbatim). - `TopKThresholdTracker.cpp` keeps the 26.7 version: its conflicting hunks are #116512's. - The test drops its `GROUP BY` query and that query's three reference lines, because it uses `enable_group_by_top_k_optimization` and `enable_group_by_top_k_dynamic_filtering`, which 26.7 does not have. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve the conflicts of the 26.3 cherry-pick of #125221 ("Fix `ORDER BY ... LIMIT` over an `Interval` column with top-K dynamic filtering"). The conflicts come from master-only changes to the top-K filter: #116512 split `TopKThresholdTracker` into `ITopKThresholdTracker` with lock-free implementations in a new `TopKThresholdTracker.cpp`, and later PRs rewrote `FunctionTopKFilter`. 26.3 has the original header-only `TopKThresholdTracker` constructed from the sort direction, and a `FunctionTopKFilter` with a single comparison path. The resolution keeps both and applies only what #125221 changes: - `TopKThresholdTracker` takes and keeps the type of the sort column (`getDataType`). - `optimizeTopK.cpp` passes `sort_column.type` where it creates the tracker. On master this is done inside `createTopKThresholdTracker`, which 26.3 does not have. - `FunctionTopKFilter` gets the threshold type from the tracker, without `LowCardinality`, and passes it as the source type of the threshold conversion, as on master. - `TopKThresholdTracker.cpp` stays absent, as it is on 26.3. - The test enables `use_top_k_dynamic_filtering`, which is off by default on 26.3 and not randomized by its test runner, and drops two queries with their reference lines: the `Array` one uses `use_top_k_dynamic_filtering_for_variable_length_types` and the `GROUP BY` one uses `enable_group_by_top_k_*`, none of which 26.3 has. Co-Authored-By: Claude Opus 5.5 <[email protected]>

Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improved performance of queries with
ORDER BY ... LIMITthat use top-k pruning by minmax indexes (settinguse_skip_indexes_for_top_k) for large tables with many granules.Workflow [PR]
Sync PR [sync-upstream/pr/116512]
Version info
26.10.1.1664-master(included in26.10and later)