Repository navigation
Conversation
…BY has one column TopN dynamic filtering over Parquet skipped a row group by its statistics only when its minimum (maximum for DESC) was strictly past the running top-K threshold. The comparison is non-strict because with several ORDER BY columns a row equal to the threshold on the first column can still win on a later column. With a single ORDER BY column it cannot: every published threshold is backed by at least `limit` rows already in the sort whose key sorts no later than it (PartialSortingTransform publishes row `limit - 1` of a sorted block with at least `limit` rows, MergeSortingTransform publishes after a remerge to exactly `limit` rows), so a row equal to the threshold can only replace an equal row and the ORDER BY values of the result do not change. WITH TIES never reaches this path. When many row groups share the minimum of the file (e.g. ClickBench `ORDER BY EventDate LIMIT 10`), every one of them used to be read in full. `tryTopKForFormatSource` now passes the number of sort columns to the format (`FormatTopKFilterInfo::single_sort_column`), and `topKShouldSkipRowGroup` uses the new `TopKThresholdTracker::isValueStrictlyInsideThreshold` for a single sort column. Multi-column sorts, the per-row `__topKFilter` and the MergeTree paths are unchanged (MergeTree's `getTopKMarks` already ignores ties for one sort column). The perf test `parquet_top_k_row_group_order` gets an `ORDER BY m, k` control query; its existing `ORDER BY m` query is the one this change speeds up. Analysis and patch by snelheidai in ClickHouse#124164. Closes ClickHouse#124164 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the duplicated field comment on single_sort_column; keep the why of the strict comparison once, in topKShouldSkipRowGroup. 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; 0 findings) plus my own cold review (4 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261006-100100 |
|
Workflow [PR], commit [0b09b8b] Summary: ✅
AI ReviewSummaryPR #124195 lets single-column Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units323 translation units recompiled, 4136 s compile time in total, 323 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
|
Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@groeneai The strict skip here needs the query-condition-cache fence you added for MergeTree in #125122: with the TopK cache now on master, overlapping runs can make later runs return wrong results. Since #122064, a TopK read of a Parquet With this PR (Reader.cpp#L514-L515), a row group whose min (max for The TopK key exists without Smallest fences:
Test: the wrong result needs overlapping runs, so a deterministic check of the fence fits better, like 05355's "no cache entries after dropping ties" pair. Over 05333's file as a This is source-derived (master a7f7136, this PR at d7d4200); I have not reproduced it on Parquet. |
…e-sort-column-ties
…ache With one sort column the top-K threshold skips a row group whose minimum (maximum for DESC) equals the threshold. Such a row group can hold rows equal to the n-th value, but it returns no row, so the TopK query condition cache recorded it as holding no row of the result. Overlapping runs that keep different tied row groups then record all of them, and a later run returns fewer rows: after 16 overlapping `ORDER BY k LIMIT 1` reads of 16 Parquet files, the next read returned no row in 5 of 100 cycles (0 of 120 with this change, 0 of 40 on master). `getMatchedBuckets` now counts a row group skipped by the threshold of a single sort column as matched, as it already did for statistics-only reads, and `getTopKBestValuesOfBuckets` reports the bound of its statistics as its best value, so the final-threshold check of the cache writers still records it as empty when that bound sorts strictly beyond the final threshold. Master is merged in for `skipped_by_top_k` (ClickHouse#124827), which the fence uses. Co-Authored-By: Claude Opus 5.5 <[email protected]>
With one sort column the threshold now skips the row groups of `parquet_top_k_best_value` whose minimum equals it, so its queries no longer record the best value over almost every row (measured on 40 row groups: 30 skipped, 9.4M of 40M rows read). Add the same queries with a second sort column, which keep the non-strict comparison and read every row group. Also document in `IInputFormat` that a bucket skipped by the threshold reports the bound of its statistics as its best value. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A cloned `ReadFromObjectStorageStep` shares the threshold tracker with the other copy, as `SortingStep::clone` does, so the threshold can come from the sort of the other copy, which this read has not yet given enough rows that sort no later than it. With a correlated subquery read without the in-memory buffer, the strict comparison then skipped every row group of the second copy: `sum(n)` was NULL instead of 9. Mark the tracker as shared by plan copies on clone, as `ReadFromMergeTree` does, and compare strictly only while it is not shared, as `__topKFilter` does for MergeTree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@groeneai When #124899 is merged too, fence 1 (e3d4bff) misses one case: a tied row group that #124899's late skip stops after it has returned some rows is recorded as empty in the TopK cache entry. Neither PR alone is affected. In this PR alone, every skip (admission and Example with
This is the record fence 1 prevents for a tied group that returned nothing, so overlapping runs can again mark every tied row group as empty. Smallest fix: in --- a/src/Processors/Formats/Impl/ParquetV3BlockInputFormat.cpp
+++ b/src/Processors/Formats/Impl/ParquetV3BlockInputFormat.cpp
@@ -457,14 +457,15 @@
{
if (!row_group.need_to_process)
continue;
- if (row_group.top_k_best_value)
- res.emplace_back(row_group.row_group_idx, (*row_group.top_k_best_value)[0]);
- else if (row_group.skipped_by_top_k.load() && row_group.top_k_sort_column_range)
+ /// A row group can be skipped after returning some rows; only the bound also covers the skipped ones.
+ if (row_group.skipped_by_top_k.load() && row_group.top_k_sort_column_range)
{
const Range & range = *row_group.top_k_sort_column_range;
const bool ascending = format_filter_info->top_k_filter->threshold_tracker->getDirection() == 1;
res.emplace_back(row_group.row_group_idx, ascending ? range.left : range.right);
}
+ else if (row_group.top_k_best_value)
+ res.emplace_back(row_group.row_group_idx, (*row_group.top_k_best_value)[0]);
}
return res;
}The bound sorts no later than any row of the group, so a group is still recorded as empty only when all its rows sort strictly beyond the final threshold. With this PR alone the change is a no-op, because a marked group has returned nothing and so has no folded best value; it can therefore land here whichever PR merges first. This corrects my #124899 comment: the mark keeps a late skip of a tied row group from bypassing your fence "in either merge order" only if the group returned no row. No test is proposed. The partial skip needs other rows to bring the threshold to the group's min between two of its subgroups, a scheduling race for which I found no cheap deterministic shape. From source only, not reproduced: this PR at 74b8a36, #124899 at 335dacd, master at 22a323d. |
With one sort column the top-K threshold skips a Parquet row group whose minimum (maximum for DESC) equals it. A skip that happens after the row group has already returned some rows (the per-subgroup recheck of ClickHouse#124899) kept the best value of the returned rows as the row group's best value, so the TopK query condition cache recorded the row group as empty whenever that value sorts past the final threshold, although the unread rows may equal it. Overlapping runs can then mark every tied row group, and a later run returns no row. Report the bound of the statistics first: it sorts no later than any row of the row group. On this branch alone a skipped row group has returned no rows, so this changes nothing until ClickHouse#124899 is merged too. Measured with ClickHouse#124899's ReadManager change applied (debug build): two Parquet files whose only k = 0 row is in the last row subgroup, 16 overlapping `ORDER BY k LIMIT 1` reads, then one more read. Before: no row in 22 of 30 cycles through file(), 1 of 40 with a File table. After: 0 of 60 and 0 of 40. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A Nullable key is compared by the generic threshold tracker, whose strict comparison is a separate implementation from the numeric one. Reading the file with a `Nullable(UInt64)` structure covers it: with that comparison made non-strict, both new queries read 1095 rows instead of 1005, and the existing `UInt64` ones are unaffected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-sort-column-ties Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #124164
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ORDER BY x LIMIT nover Parquet files with a single sort column now also skips row groups whose minimum (maximum forDESC) equals the current top-K threshold. This speeds up queries where many row groups share the smallest (largest) value, such asORDER BY EventDate LIMIT 10on ClickBenchhits.parquet.Description
Analysis and patch by @ snelheidai (#124164).
TopN dynamic filtering skipped a Parquet row group by its statistics only when its minimum (maximum for
DESC) was strictly past the top-K threshold. With several sort columns a row equal to the threshold can still win on a later column. With one it cannot: every published threshold is backed bylimitrows that sort no later than it, so an equal row can only replace an equal row (WITH TIESnever takes this path). The planner now tells the format whether there is a single sort column, andtopKShouldSkipRowGroupthen compares strictly, unless a cloned object storage read shares the threshold with another copy.Such a row group can hold rows equal to the n-th value, so the TopK query condition cache counts it as matched, with the bound of its statistics as its best value: it is still cached as empty when that bound is strictly past the final threshold. Without this, after 16 overlapping
ORDER BY k LIMIT 1reads of 16 files the next read returned no row in 5 of 100 cycles (0 of 120 with it).Release build, ClickBench
hits.parquet(226 row groups), geomean time fix/base [95% CI], 4 threads:ORDER BY EventDate LIMIT 10: row groups read (one thread) 42 -> 1, 0.80 [0.75, 0.84]ORDER BY EventDate DESC LIMIT 10: 26 -> 1, 0.92 [0.88, 0.97]ORDER BY WatchID,ORDER BY EventDate, UserID: unchangedparquet_top_k_row_group_order.xml,ORDER BY m LIMIT 10: 191 of 200 row groups skipped instead of 0, 0.35 [0.33, 0.36]The test checks rows read with one and two sort columns, 11 queries against the unoptimized result, that a rerun over a
Filetable skips the same row groups and none through the cache, and a cloneds3read.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/124195]