Repository navigation
Speed up bloom filter index building for LowCardinality columns - #106410
Conversation
|
Workflow [PR], commit [20b1fbb] Summary: ✅
AI ReviewSummaryThe PR changes Final Verdict
|
|
📊 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
|
625d3d0 to
e8b42b0
Compare
05d1c43 to
c929d62
Compare
When building a bloom_filter granule over a LowCardinality column, MergeTreeIndexAggregatorBloomFilter::update hashes the column and then inserts the hash of every row into a hash set, one insert per row. A bloom filter only needs the set of distinct values, so for a column with few distinct values but many rows almost all of those inserts are duplicates that get discarded. For Array(LowCardinality(String)) this is especially bad: one insert per array element across every row in the granule. Add BloomFilterHash::hashWithColumnDistinct, which returns only the distinct hashes for the requested slice. For a LowCardinality column it hashes each dictionary value once, walks the index map to find which dictionary entries actually occur in the slice, and returns one hash per occurring entry. The aggregator inserts that small set instead of one hash per row. For other columns it returns one hash per row, exactly as before. The hashes inserted are a subset of what the previous path inserted (the duplicates the hash set would have collapsed anyway), so the resulting bloom filter is byte-identical: existing on-disk indexes stay valid and query-time predicate hashing, which still goes through hashWithColumn, is unchanged. Handles scalar LowCardinality, Array(LowCardinality(...)), and LowCardinality(Nullable(...)). On a 5M-row Array(LowCardinality(String)) table the INSERT goes from ~27s to ~1.2s. Fixes ClickHouse#106372.
Addresses review feedback on the LowCardinality bloom-filter index build. The previous version hashed the whole LowCardinality dictionary inside hashWithColumnDistinct. The skip-index aggregator calls it once per granule over the same insert block, and a LowCardinality column shares one dictionary across the whole block, so this rehashed the entire block dictionary for every granule. For a large dictionary spread over many granules that scaled with granules * dictionary_size and could be slower than the original per-row path. Now the LowCardinality path first finds the dictionary entries the granule's slice actually uses and hashes only those, so the work is bounded by the slice's rows. The distinct-entry scan is exposed as ColumnLowCardinality::getDistinctIndexes, which reads the index column's raw typed data directly instead of going through a per-row accessor. Other cleanups from review: - hashWithColumnDistinct now special-cases only ColumnLowCardinality and falls back to hashWithColumn for everything else, instead of duplicating its body. - Trimmed the helper doc comments. - Extended the stateless test with a large dictionary spread across many granules. On a 2M-row Array(LowCardinality(String)) table with a near-unique dictionary the INSERT goes from ~16s to ~1.5s; the small-dictionary case is unchanged (~27s to ~1.2s on 5M rows).
…nary scan The first version marked used dictionary entries in a flag array and then scanned the entire dictionary to collect the ones that were set. The bloom aggregator calls this once per granule over the same block dictionary, so for a large dictionary that collection was O(dictionary) work per granule. Instead, collect each dictionary position the first time the slice references it. The flag array is still sized by the dictionary (so any position is a valid index), but we only read and write the entries the slice touches, and we no longer scan the whole dictionary -- the work is proportional to the slice length. The returned positions are now in first-seen order instead of ascending; order does not matter to the bloom filter, which only needs the set. A standalone microbenchmark of this function (1M rows, 8192-row granules) drops from ~57ms to ~4ms per block at a 1M-entry dictionary, and is also faster at small dictionaries. The end-to-end INSERT time is unchanged, since this step is a small part of the insert.
…String)) bloom_filter_insert.xml already covers Array(String). This adds the LowCardinality counterpart from issue ClickHouse#106372, in two shapes: a low-cardinality one (few distinct values) and a high-cardinality one (≈ one distinct value per element), which exercises the dictionary-sized work the old code did per granule.
Populate the hash set with the range's positions, then reserve the output to the exact distinct count and copy the keys out, instead of appending to the output inline while inserting. This sizes the output array to the number of distinct positions rather than to the (larger) range length, at the cost of a separate pass over the set; the order of the result is now unspecified, which the callers do not rely on.
c929d62 to
e581ff6
Compare
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 60/67 (89.55%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 5 line(s) · Uncovered code |
78808f0
|
Hi @EmeraldShift @murphy-4o — while reviewing this PR I found the following:
Could you review the test PR and add the |
This reverts commit a9af02b.
This reverts commit a9af02b.
When building a
bloom_filterindex over aLowCardinalitycolumn,MergeTreeIndexAggregatorBloomFilter::updatehashes the column and then inserts the hash of every row into a hash set, one insert per row. A bloom filter only needs the set of distinct values, so for a column with few distinct values but many rows almost all of those inserts are duplicates that get discarded. ForArray(LowCardinality(String))this is especially bad: one insert per array element across every row in the granule.This PR adds
BloomFilterHash::hashWithColumnDistinct, which returns only the distinct hashes for the requested slice. For aLowCardinalitycolumn it hashes each dictionary value once, walks the index map to find which dictionary entries actually occur in the slice, and returns one hash per occurring entry. The aggregator inserts that small set instead of one hash per row. For other columns it returns one hash per row, exactly as before.The hashes inserted are a subset of what the previous path inserted (the duplicates the hash set would have collapsed anyway), so the resulting bloom filter is byte-identical: existing on-disk indexes stay valid and query-time predicate hashing, which still goes through
hashWithColumn, is unchanged.This handles scalar
LowCardinality,Array(LowCardinality(...)), andLowCardinality(Nullable(...)). On a 5M-rowArray(LowCardinality(String))table the INSERT goes from ~27s to ~1.2s.Closes: #106372
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improved insertion performance for
LowCardinalitycolumns withbloom_filterindexes.Version info
26.6.1.694