Skip to content

Support single-column non-nullable string LowCardinality keys natively in HashJoin - #107264

Merged
nickitat merged 6 commits into
masterfrom
join-low-cardinality-key
Jun 24, 2026
Merged

nickitat merged 6 commits into
masterfrom
join-low-cardinality-key

Conversation

@nickitat

@nickitat nickitat commented Jun 11, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

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.

Screenshot 2026-06-12 at 13 01 24

Version info

  • Merged into: 26.7.1.23

nickitat and others added 4 commits June 11, 2026 18:11
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]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2b0382b]

Summary: ✅


AI Review

Summary

This PR adds dictionary-aware HashJoin maps for single non-nullable LowCardinality(String) and LowCardinality(FixedString) build keys, keeps numeric LowCardinality keys on the existing materialized key* path, and adds stateless plus performance coverage for the new behavior. I did not find a current code-level blocker or major issue; the earlier numeric-key runtime-filter/fixed-map concerns are addressed in the current diff.

PR Metadata
  • Changelog category: Performance Improvement matches the actual change.
  • Changelog entry: required for this category, but the current entry is todo, which is not user-readable and would publish a placeholder.
  • Suggested replacement:
Improves hash join performance for joins with a single non-nullable `LowCardinality(String)` or `LowCardinality(FixedString)` key by probing the encoded dictionary directly instead of materializing the key column.
Missing context / blind spots
  • ⚠️ Full PR CI was still pending during this review. The Praktika report helper showed no failed checks for the current PR report, but only a small completed subset was available; completed CI would close this gap.
Findings

💡 Nits

  • [PR description] The required Changelog entry is still todo. Replace it with the suggested user-facing entry above.
Final Verdict

Status: ⚠️ Request changes

Minimum required action: replace the placeholder Changelog entry. No inline code comments were posted because I did not find a code issue that maps to a changed line.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 11, 2026
Comment thread src/Interpreters/HashJoin/HashJoin.cpp Resolved
@nickitat nickitat added the ci-performance performance only label Jun 11, 2026
@nickitat nickitat removed the ci-performance performance only label Jun 11, 2026
Comment thread src/Interpreters/HashJoin/HashJoin.cpp Resolved
@EmeraldShift

EmeraldShift commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

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]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🔴 AI verdict: degradation — 1 regressed, 4 improved out of 38 analysed

1 regressed query(s). K_source=6 K_base=30 flagged=5/65

clickbench

🟢 2 improved

Flagged queries (2 of 43)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🟢 22 improvement 334 265 -20.7% <0.0001
🟢 23 improvement 297 199 -33.0% <0.0001

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)
Query Verdict Baseline med (ms) PR med (ms) Change q-value Hint
🔴 8 regression 156 181 +16.4% <0.0001
🟢 7 improvement 113 74 -34.5% <0.0001
🟢 20 improvement 248 131 -47.2% <0.0001

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
  • StressHouse run: 7b8acda2-b702-4dfe-93e9-19eb84e5f20a
  • MIRAI run: e4bf81dd-8d75-40ec-a3c8-27a4235daa66
  • PR check IDs:
    • clickbench_377946_1781273841
    • clickbench_377952_1781273841
    • clickbench_377958_1781273841
    • tpch_adapted_1_official_377968_1781273841
    • tpch_adapted_1_official_377978_1781273841
    • tpch_adapted_1_official_377992_1781273842

@clickhouse-gh

clickhouse-gh Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.40% 92.40% +0.00%
Branches 77.30% 77.30% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 127/140 (90.71%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@nickitat nickitat changed the title Support LowCardinality keys in HashJoin Support single-columns non-nullable LowCardinality keys natively in HashJoin Jun 23, 2026
@nickitat nickitat changed the title Support single-columns non-nullable LowCardinality keys natively in HashJoin Support single-column non-nullable LowCardinality keys natively in HashJoin Jun 23, 2026
@nickitat nickitat changed the title Support single-column non-nullable LowCardinality keys natively in HashJoin Support single-column non-nullable string LowCardinality keys natively in HashJoin Jun 23, 2026
@nickitat
nickitat marked this pull request as ready for review June 23, 2026 16:19
@nickitat

Copy link
Copy Markdown
Member Author

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

In this particular case, I think both your and mine changes are well isolated within the related part of the code.

@harikrishnan94 harikrishnan94 self-assigned this Jun 24, 2026
@nickitat
nickitat added this pull request to the merge queue Jun 24, 2026
Merged via the queue into master with commit afab358 Jun 24, 2026
327 of 328 checks passed
@nickitat
nickitat deleted the join-low-cardinality-key branch June 24, 2026 13:19
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 24, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…lity-key

Support single-column non-nullable string `LowCardinality` keys natively in `HashJoin`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants