Repository navigation
Support single-column non-nullable string LowCardinality keys natively in HashJoin - #107264
Conversation
A join on a single non-nullable LowCardinality(T) key materialized the key
column on both the build and probe sides, discarding the dictionary. Add
dictionary-aware maps (low_cardinality_key{8,16,32,64,string,fixed_string}) so
the key column stays dictionary-encoded: the probe deduplicates the hash-table
lookup per dictionary index within a block, and the key is not materialized.
- New map variants + a HashJoin-local LowCardinalityKeyGetterForJoin in
KeyGetter.h that wraps a base method over the dictionary's nested column. It is
const-correct on the probe side, produces an offset-carrying FindResult (for
JoinUsedFlags), caches a pointer into the stable hash-table cell (not a copy,
which would dangle in the lazy output), and does not deduplicate on the build
side (every row must be appended).
- chooseMethod detects a single non-nullable LowCardinality key; the key column
is kept un-materialized at the build and probe sites for those maps.
LowCardinality(Nullable(T)), composite keys and two-level maps fall back to the
existing materialized path.
~2-3x faster for string keys (hashjoin_low_cardinality_key.xml); neutral for
numeric LowCardinality, where the materialized baseline is already cheap.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
…nary-aware getter
The dictionary-aware map is selected from the right/build key being LowCardinality,
but join key compatibility allows a plain T probe against a LowCardinality(T) build
without a cast (the old analyzer keeps the mixed types; the new analyzer casts them
first). Probe execution then built LowCardinalityKeyGetterForJoin unconditionally,
which threw LOGICAL_ERROR ("Expected LowCardinality column") when the probe column
was not ColumnLowCardinality, failing valid mixed-type joins at runtime
(e.g. 01049_join_low_card_crash under enable_analyzer = 0).
Make the getter run the base method directly on a plain column (no dictionary
indirection or deduplication); the map stores key values, so a plain probe and a
dictionary-encoded build still produce compatible keys. Add 04335 covering the
mixed-type contract across join kinds.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
…are join The dictionary-aware map is chosen only when the build/right dictionary is non-nullable, but join key compatibility ignores nullability, so a LowCardinality(Nullable(T)) probe can still reach the map. extractNestedColumnsAndNullMap does not look inside LowCardinality, and the dictionary-aware getter has no null-key path, so a NULL probe key was resolved to dictionary position 0 and could match a build key (e.g. the empty string) — NULL must never join. Materialize a LowCardinality(Nullable(T)) key in materializeColumnsKeepLowCardinality so the join extracts its null map and skips NULL keys (the getter then runs on the plain nested column). Non-nullable LowCardinality keys still keep the dictionary. Extend 04335 with a nullable-probe-vs-non-nullable-build case (build holds an empty string the NULL must not match). Co-Authored-By: Claude Opus 4.8 <[email protected]>
The dictionary-aware maps (low_cardinality_key*) are selected for LowCardinality build keys, but StorageJoin's read dispatch (APPLY_FOR_JOIN_VARIANTS_LIMITED) did not list them, so SELECT from a Join-engine table with a LowCardinality key, and joinGet on it, failed with UNSUPPORTED_JOIN_KEYS (e.g. 01586_storage_join_low_cardinality_key). These maps are physically identical to their non-LowCardinality counterparts and store key values, so they are read back the same way: fillColumns reconstructs the key into the output column, which already has the parent LowCardinality type. Add them to the limited variant set. Add 04336 covering SELECT and joinGet for LowCardinality string and numeric keys against a plain-keyed Join table. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Workflow [PR], commit [2b0382b] Summary: ✅
AI ReviewSummaryThis PR adds dictionary-aware PR Metadata
Missing context / blind spots
Findings💡 Nits
Final VerdictStatus: Minimum required action: replace the placeholder |
|
Nice. Is there anything to share between this PR and #106410? I'm excited for more places in ClickHouse to take advantage of LowCardinality dictionary encoding, and I don't want to duplicate work in all those places :) EDIT: fix PR link |
Routing LowCardinality(UInt8/16/32/64) build keys to the dictionary-aware map made them skip post-build optimizations of the materialized key* path: tryConvertToFixedHashMap converts a dense small-range key32/key64 to a range*_key* FixedHashMap (under enable_join_fixed_hash_table_conversion), and key8/key16 and the converted range*_key* maps can publish the shared fixed-hash-table runtime filter. The dictionary-aware map skips both, which can regress a LowCardinality(UInt32) build with a dense range and a plain or high-cardinality probe — for no measurable gain, since the dictionary-aware map's benefit is concentrated on variable-length string keys. Route numeric LowCardinality keys back to the materialized key* path and drop the now unused low_cardinality_key8/16/32/64 variants (maps, getters, dispatch, limited set). The dictionary-aware map is kept for String and FixedString keys, which are not in the conversion or runtime-filter path. 04328 keeps a numeric case to guard correctness of the materialized fallback. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
📊 Cloud Performance Report 🔴 AI verdict: 1 regressed query(s). K_source=6 K_base=30 flagged=5/65 clickbench🟢 2 improved Flagged queries (2 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🔴 1 regressed · 🟢 2 improved Flagged queries (3 of 22)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 127/140 (90.71%) | Lost baseline coverage: none · Uncovered code |
LowCardinality keys natively in HashJoin
LowCardinality keys natively in HashJoinLowCardinality keys natively in HashJoin
LowCardinality keys natively in HashJoinLowCardinality keys natively in HashJoin
In this particular case, I think both your and mine changes are well isolated within the related part of the code. |
…lity-key Support single-column non-nullable string `LowCardinality` keys natively in `HashJoin`
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Single-column non-nullable string LowCardinality keys are natively supported now in
HashJoin.Non-key columns are not being materialized already, AFAIU. For the key columns, it seems to be unavoidable when we need to squash multiple column values into a single key in a multi-key JOIN. So, it leaves us with only the single-column case. We already have useful optimisations for compact numeric ranges, and benchmarks haven't shown any significant improvements for numeric columns on top of that. But the string case can actually be improved noticeably.
Version info
26.7.1.23