Skip to content

Optimize bucketed LowCardinality map reconstruction - #107434

Open
EmeraldShift wants to merge 1 commit into
ClickHouse:masterfrom
EmeraldShift:bucketed-map-lc-reconstruction
Open

EmeraldShift wants to merge 1 commit into
ClickHouse:masterfrom
EmeraldShift:bucketed-map-lc-reconstruction

Conversation

@EmeraldShift

Copy link
Copy Markdown
Contributor

Reading a bucketed Map back as a full map has to stitch the per-bucket streams back into row order. Before this change, a LowCardinality key or value column used the generic insertion path for each small row/bucket slice. Each of those tiny inserts had to translate source bucket dictionary indexes into the destination dictionary, so the read paid the dictionary-remapping overhead over and over again.

This keeps the same logical reconstruction order, but does the LowCardinality remapping in bulk. Each bucket is first represented as a compact dictionary plus indexes. The bucket dictionaries are concatenated into a temporary dictionary, a single row-ordered index stream is built, and that temporary dictionary-encoded stream is merged into the output ColumnLowCardinality once.

The same helper is used for full Map reconstruction and for mapKeys / mapValues bucket reassembly. In full Map reconstruction, the helper is tried independently for keys and values, so it handles Map(LowCardinality(K), V), Map(K, LowCardinality(V)), and Map(LowCardinality(K), LowCardinality(V)). The first successful LowCardinality collection also builds the shared map offsets from bucket lengths; when both streams are collected, the old row/bucket insertion loop is skipped entirely.

The temporary dictionary is only an intermediate representation. insertRangeFromDictionaryEncodedColumn still inserts into the destination dictionary using the normal ColumnLowCardinality path, so unexpected duplicate temporary dictionary values remain correct. In the expected bucketed-map case, keys should be disjoint across buckets because bucket assignment is deterministic by key.

Local benchmark details: clickhouse local with --print-profile-events --profile-events-delay-ms=-1. CPU numbers are OSCPUVirtualTimeMicroseconds medians; lower is better. The main benchmark uses 50k rows, 200 keys/row, and 32 buckets.

Bucketed reconstruction, true baseline vs current patch:

ShapeQueryBaseline CPUCurrent CPUSpeedup
Map(LC(String), String)full map1.866s0.883s2.1x
Map(LC(String), String)mapKeys1.224s0.266s4.6x
Map(LC(String), String)mapKeys + mapValues1.879s0.904s2.1x
Map(String, LC(String))full map1.239s0.335s3.7x
Map(String, LC(String))mapValues1.077s0.100s10.8x
Map(String, LC(String))key lookup + full map1.395s0.377s3.7x
Map(LC(String), LC(String))full map2.128s0.158s13.5x
Map(LC(String), LC(String))mapKeys + mapValues2.284s0.358s6.4x

Query-shape comparison against non-bucketed/basic serialization for Map(LowCardinality(String), String):

QueryBasic CPUBucketed baseline CPUBucketed current CPUBucketed current vs basic
full map0.354s1.866s0.883s2.5x slower
one key + full map0.475s1.954s0.916s1.9x slower
81 keys + full map3.029s4.575s3.550s1.2x slower
81 keys only3.319s0.887s0.885s3.8x faster

Non-LowCardinality sanity check from the same run. These paths continue through the existing generic insertion path:

ShapeQueryBaseline CPUCurrent CPU
Map(String, String)full map1.146s1.149s
Map(String, String)mapKeys0.383s0.378s
Map(String, String)mapValues0.767s0.794s

The optimization narrows the full-map regression of bucketed Map(LowCardinality(...), ...) reads while preserving the main benefit of bucketed maps: reading one or many specific keys without the full map remains much faster than basic serialization.

Changelog category (leave one):

  • Performance Improvement

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

Optimized loading LowCardinality data when reading mapKeys(m), mapValues(m), or the whole map, when using the bucketed map serialization.

@UnamedRus

Copy link
Copy Markdown
Contributor

I actually probably didn't mind if Map(LC(Key), ..) would use shared dictionary for all buckets, but probably now it's too late to do that.

Do you somehow use that fact, that for LC(key), because map is sharded, it's guaranteed that values will not intersect.
So, we can kinda build SharedLC dictionary using:

shared_dict_id = dict_id * NUM_BUCKETS_IN_SELECT + bucket_id; --

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [74ad91e]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_msan, 7/8) FAIL
test_replicated_database/test.py::test_replicated_table_structure_alter FAIL cidb, issue

AI Review

Summary

This PR replaces the per-slice LowCardinality remapping used while reassembling bucketed Map data with a shared bulk helper for full-map reads and mapKeys / mapValues. After reviewing the current diff, the existing discussion, and the resolved bot threads, I did not find any unresolved correctness, compatibility, or evidence gaps in the current version; the earlier index-width and no-fasttest concerns are addressed.

Final Verdict
  • Status: ✅ Approve

@EmeraldShift
EmeraldShift force-pushed the bucketed-map-lc-reconstruction branch from 7731f93 to e2937e2 Compare June 14, 2026 23:59
@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 15, 2026
@EmeraldShift
EmeraldShift force-pushed the bucketed-map-lc-reconstruction branch from e2937e2 to cf1b204 Compare June 15, 2026 00:24
@clickhouse-gh

clickhouse-gh Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

This sync PR changes only bucketed Map reconstruction when keys/values are LowCardinality (a new SerializationMapLowCardinalityBuckets fast path). ClickBench has no Map(LowCardinality) columns, so neither Q4 (-13.7%) nor Q15 (-13.1%) can be affected by it. Both apparent improvements land inside master's current variance band and were correctly held at no_change; they are off-path run-to-run variance, not a real effect of this change.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a7effef9-3649-4d5b-9a08-9090e704999d
  • MIRAI run: 666b1204-c041-49a8-a175-ab7a58fe46e2
  • PR check IDs:
    • clickbench_351990_1783886246
    • clickbench_352003_1783886246
    • clickbench_352009_1783886246
    • tpch_adapted_1_official_352015_1783886246
    • tpch_adapted_1_official_352022_1783886246
    • tpch_adapted_1_official_352031_1783886246

@EmeraldShift
EmeraldShift force-pushed the bucketed-map-lc-reconstruction branch from cf1b204 to 74ad91e Compare July 12, 2026 17:32
@clickhouse-gh

clickhouse-gh Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 73/74 (98.65%) · Uncovered code

Full report · Diff report

@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Dear @Avogar, 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.

@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026

This branch has not been deployed

No deployments
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 comp-map-datatype Map datatype implementation and semantics. external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants