Skip to content

Speed up bloom filter index building for LowCardinality columns - #106410

Merged
murphy-4o merged 7 commits into
ClickHouse:masterfrom
EmeraldShift:bloom-lc-hash
Jun 12, 2026
Merged

murphy-4o merged 7 commits into
ClickHouse:masterfrom
EmeraldShift:bloom-lc-hash

Conversation

@EmeraldShift

@EmeraldShift EmeraldShift commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

When building a bloom_filter index 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.

This PR adds 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.

This handles scalar LowCardinality, Array(LowCardinality(...)), and LowCardinality(Nullable(...)). On a 5M-row Array(LowCardinality(String)) table the INSERT goes from ~27s to ~1.2s.

Closes: #106372

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Improved insertion performance for LowCardinality columns with bloom_filter indexes.

Version info

  • Merged into: 26.6.1.694

@murphy-4o murphy-4o added the can be tested Allows running workflows for external contributors label Jun 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [20b1fbb]

Summary: ✅


AI Review

Summary

The PR changes bloom_filter skip-index construction so LowCardinality columns hash only the distinct dictionary entries referenced by each granule, while preserving the existing query-side hash contract and adding stateless plus performance coverage for scalar, array, nullable, and large-dictionary cases. I did not find any unresolved correctness, safety, or performance-review blockers in the current revision.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 4, 2026
@murphy-4o murphy-4o self-assigned this Jun 4, 2026
Comment thread src/Interpreters/BloomFilterHash.h Resolved
Comment thread src/Interpreters/BloomFilterHash.h Resolved
Comment thread src/Interpreters/BloomFilterHash.h Resolved
Comment thread src/Interpreters/BloomFilterHash.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

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
  • StressHouse run: ac88e55a-7230-4662-9b0f-c81386258183
  • MIRAI run: fb316c1b-12d2-4246-a89a-8a4f250653dc
  • PR check IDs:
    • clickbench_272962_1781191432
    • clickbench_273047_1781191462
    • clickbench_273067_1781191463
    • tpch_adapted_1_official_273087_1781191463
    • tpch_adapted_1_official_273326_1781191501
    • tpch_adapted_1_official_273412_1781191534

Comment thread src/Columns/ColumnLowCardinality.cpp Resolved
Comment thread src/Columns/ColumnLowCardinality.cpp Resolved
@EmeraldShift
EmeraldShift force-pushed the bloom-lc-hash branch 4 times, most recently from 625d3d0 to e8b42b0 Compare June 4, 2026 16:56
@EmeraldShift
EmeraldShift requested a review from murphy-4o June 4, 2026 17:04
Comment thread src/Columns/ColumnLowCardinality.cpp Resolved
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.
@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.30% +0.10%

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

Full report · Diff report

@murphy-4o
murphy-4o enabled auto-merge June 12, 2026 02:39
@murphy-4o
murphy-4o added this pull request to the merge queue Jun 12, 2026
Merged via the queue into ClickHouse:master with commit 78808f0 Jun 12, 2026
327 of 328 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 12, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @EmeraldShift @murphy-4o — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

alexbakharew pushed a commit that referenced this pull request Jun 18, 2026
alexbakharew added a commit that referenced this pull request Jun 24, 2026
pull Bot pushed a commit to admariner/ClickHouse that referenced this pull request Jul 30, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inserting data on bloom filter indexed column is much slower with LowCardinality

4 participants