Repository navigation
Share the values of a sparse column filtered by its own non-default rows in PREWHERE - #129738
Conversation
…ows in `PREWHERE` A condition such as `s != ''` on a sparse column is computed on its values only and returned as Sparse(UInt8) sharing the column's offsets, so the PREWHERE filter's sparse indices are exactly the column's non-default rows. `filterColumns` copied every kept value with `filter`; when the offsets equal the sparse indices, use `index`, which shares `values` since the previous commit. Idea and analysis by snelheidai in ClickHouse#129686. 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; 2 findings) plus my own review (4 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261106-134300 |
|
Workflow [PR], commit [cb7e101] Summary: ✅
AI ReviewSummaryPR #129738, head Missing context / blind spots
Final Verdict✅ Approve — no new inline findings or thread updates are warranted. |
|
cc @Avogar, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 11 s compile time in total, 7 of them have a recent master baseline. |
|
On the order in
100M rows in one part: In a profile of the The measured change, on top of this PRdiff --git a/src/Storages/MergeTree/MergeTreeRangeReader.cpp b/src/Storages/MergeTree/MergeTreeRangeReader.cpp
index 501280925c5..5f8ca58a682 100644
--- a/src/Storages/MergeTree/MergeTreeRangeReader.cpp
+++ b/src/Storages/MergeTree/MergeTreeRangeReader.cpp
@@ -97,7 +97,13 @@ static void filterColumns(Columns & columns, const FilterWithCachedCount & filte
throw Exception(ErrorCodes::LOGICAL_ERROR, "Size of column {} doesn't match size of filter {}",
column->size(), filter.size());
- if (canInplaceFilter(column, filter.getColumn()))
+ if (const auto * column_sparse = sparse_indices ? typeid_cast<const ColumnSparse *>(column.get()) : nullptr;
+ column_sparse && column_sparse->getOffsetsData() == sparse_indices->getData())
+ {
+ /// The filter keeps exactly the non-default rows of this column, so `index` shares its values.
+ column = column_sparse->index(*sparse_indices, 0);
+ }
+ else if (canInplaceFilter(column, filter.getColumn()))
{
/// The contract is - not to filter in-place if the column is shared. But if there're some shared subcolumns,
/// we'll clone them via IColumn::mutate() and then safely filter in-place.
@@ -105,12 +111,6 @@ static void filterColumns(Columns & columns, const FilterWithCachedCount & filte
mutable_column->filter(filter_data);
column = std::move(mutable_column);
}
- else if (const auto * column_sparse = sparse_indices ? typeid_cast<const ColumnSparse *>(column.get()) : nullptr;
- column_sparse && column_sparse->getOffsetsData() == sparse_indices->getData())
- {
- /// The filter keeps exactly the non-default rows of this column, so `index` shares its values.
- column = column_sparse->index(*sparse_indices, 0);
- }
else
{
column = column->filter(filter_data, filter.countBytesInFilter()); |
The column the `PREWHERE` condition was computed from never takes the in-place branch: the filter shares its `offsets`, so `use_count() > 1`. But another uniquely owned Sparse column with exactly the same non-default rows did, and the in-place `ColumnSparse::filter` still rewrites its offsets and filters its values, where `index` shares them. On a miss the check costs a `typeid_cast` and, for Sparse columns, an offsets comparison that stops at a size difference or the first different offset. Asked in ClickHouse#129738 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #129686
Related: #129705
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Faster
PREWHEREfiltering of a sparse column by a condition that keeps exactly its non-default rows (for examplePREWHERE s != ''on a sparseStringcolumn): the column's values are shared instead of copied.Description
Depends on #129705 (its commit is the first one here).
A condition such as
s != ''on a sparse column is computed on its values only and returned asSparse(UInt8)sharing the column's offsets, so thePREWHEREfilter's sparse indices are exactly the column's non-default rows.filterColumnsinMergeTreeRangeReader.cppstill copied every kept value withfilter. Now, when the column's offsets equal the filter's sparse indices, it callsindex, which sharesvaluessince #129705. It runs before the in-place branch, so other sparse columns with the same non-default rows share their values too. On a miss it costs a type check and a size compare (plus an element compare up to the first difference when the sizes match).Measured locally (pinned cores, ThinLTO clang-22, paired runs, 95% CI), vs #129705 alone unless stated, 1 thread / 16 threads:
PREWHERE MobilePhoneModel <> '' GROUP BY MobilePhoneModel: -18.3% [-20.8, -15.7] / -12.4% [-16.6, -7.9].WHERE MobilePhoneModel <> '' GROUP BY MobilePhoneModel, UTMSource(moved toPREWHERE): -11.6% / -7.1%.PREWHERE s != '', 1 thread, 3 x 40 runs): a column with the same non-default rows -8.6% to -19%; the filtered column unchanged; a sparse column with other rows -5.3% to +4.0% (no consistent change); peak memory equal.sparse_column_filterquerymax(str) ... PREWHERE str != '': -6% to -9% in two runs at every ratio.sparse_column*perf test row is slower by more than 2% (p < 0.01) in both runs; numericPREWHEREand the controls are unchanged; results are identical.Tests:
01780_column_sparse_filtergets sparse-vs-fullPREWHEREcomparisons (one and two steps, small blocks); each reaches the new branch (checked by making it throw), and the output is identical on master.Idea and analysis by snelheidai in #129686.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/129738]
Version info
26.11.1.2001-master(included in26.11and later)