Repository navigation
Fix wrong results for hash joins on a single LowCardinality wide-integer key - #113230
Conversation
…ger key HashJoin::chooseMethod selects the packed keys128/keys256 map for a single numeric key of 16 or 32 bytes. That map's key getter reads key_sizes to copy the key bytes, but the branch that selects it never assigned key_sizes. For a non-LowCardinality column this went unnoticed, because the all_fixed loop above the branch had already stored the same value. ColumnLowCardinality reports isFixedAndContiguous() as false, so the loop breaks before assigning, while isNumeric() and sizeOfValueIfFixed() are forwarded to the dictionary and still report a 16 or 32 byte numeric key. The branch is thus entered with key_sizes[0] left at 0, usePreparedKeys rejects it, and packFixed copies zero bytes. Every build and probe key becomes the all-zero value, so all rows land in one bucket and the join degenerates into a cross product. Affected types are LowCardinality over UInt128, Int128, UInt256 and Int256, including their Nullable variants. Keys of 8 bytes or less were never affected because HashMethodOneNumber ignores key_sizes. Wrong results reproduced under the hash, parallel_hash and grace_hash algorithms. Assigning key_sizes[0] in that branch is a no-op for every non-LowCardinality key, where the value written is the one already present, and it changes no map selection, so no query plan or performance characteristic changes. It also repairs ConcurrentHashJoin::selectDispatchBlock, which reads the same key_sizes and until now dispatched every row of such a join to a single shard. Verified by printing key_sizes after chooseMethod: it was 0 for LowCardinality numeric keys and 16 for plain UInt128, and the non-LowCardinality values are identical before and after the change.
The fix in this branch is a byte count (key_sizes[0] = size_of_field), but every affected assertion used values 0/1/5/7, which differ only in byte 0. A wrong-but-nonzero byte count, or a later truncation in the packing path, would have satisfied the reference exactly. Each affected type now also joins two values that share their low 8 bytes (128-bit) or low 16 bytes (256-bit) and differ only above, so a copy that reads fewer bytes than the key type makes them collide. For Int128 and Int256 the pair is -1 against 2^64-1 / 2^128-1, which covers the sign and high half. Verified by mutation: with key_sizes[0] hard-coded to 8 the 128-bit rows fail, with 16 the 256-bit rows fail, and the unmutated fix passes all of them. The LowCardinality(Nullable(UInt128)) tables now also contain NULL on both sides, with a row-level assertion, so NULL-key semantics on an affected wide type are observable rather than implied by a count. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (2 rounds, 6 findings adjudicated)This PR was reviewed cold by a second model before publication, in two rounds. Round 1 bounced the ❌ BlockersNone.
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-47:20260803-211000 |
|
cc @nickitat @rschu1ze, could you review this? |
|
Workflow [PR], commit [121bcb7] Summary: ❌
AI ReviewSummaryThis PR fixes a real wrong-results bug in Final Verdict✅ No findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
|
@harikrishnan94 Like to take this one for review? |
harikrishnan94
left a comment
There was a problem hiding this comment.
Also explain to me the code flow of how this can happen
@harikrishnan94 asked for a shorter comment at the `chooseMethod` assignment. The comment now states only the local invariant: the `all_fixed` loop above bails out before assigning `key_sizes` for a `LowCardinality` column. Why that matters (the packed `keys128`/`keys256` getters read `key_sizes` to copy the key bytes, so a zero width collapses every key to one bucket) belongs in the commit message and the pull request description, not at the call site. Trimmed the test comments the same way while in there, and wrapped literal names in inline code blocks per `.claude/CLAUDE.md`. No executable change: the diff is comments only.
|
The walkthrough, all line numbers at 1. The key column is still 2. The 3. The single-numeric branch is entered anyway, because both of its predicates forward to the dictionary: 4.
So every build and probe row hashes to the same key and unequal values compare equal. 5. Keys of 8 bytes or less were never affected: they select One thing worth noting: |
6b45106
Backport #113230 to 26.7: Fix wrong results for hash joins on a single LowCardinality wide-integer key
Backport #113230 to 26.6: Fix wrong results for hash joins on a single LowCardinality wide-integer key
Backport #113230 to 25.8: Fix wrong results for hash joins on a single LowCardinality wide-integer key
Backport #113230 to 26.5: Fix wrong results for hash joins on a single LowCardinality wide-integer key
…on a single LowCardinality wide-integer key
…on a single LowCardinality wide-integer key
Backport #113230 to 26.3: Fix wrong results for hash joins on a single LowCardinality wide-integer key
…on a single LowCardinality wide-integer key
…on a single LowCardinality wide-integer key
Related: #112957
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results when a hash join has a single
LowCardinalitykey of a type wider than 8 bytes (UInt128,Int128,UInt256,Int256, and theirNullablevariants). Such a join silently returned rows whose key values are not equal.Description
HashJoin::chooseMethodselects the packedkeys128/keys256map for a single numeric key of 16 or 32 bytes. That map's key getter readskey_sizesto copy the key bytes, but the selecting branch never assigned it.For a non-
LowCardinalitycolumn this went unnoticed, because theall_fixedloop above had already stored the same value.ColumnLowCardinality::isFixedAndContiguousisfalse, so that loop breaks before assigning, whileisNumericandsizeOfValueIfFixedforward to the dictionary and still report a 16 or 32 byte numeric key. The branch is thus entered withkey_sizes[0]left at 0,usePreparedKeysrejects it, andpackFixedcopies zero bytes. Every key becomes all-zero, so keys that differ compare equal and the join reports non-matching rows as matches.The setting above is only needed to declare the column. Ordinary queries reach this at default settings too, because the analyzer produces the type on its own:
toTypeName(toLowCardinality(toUInt128(materialize(0))))isLowCardinality(UInt128).The fix assigns
key_sizes[0] = size_of_fieldin that branch. For every non-LowCardinalitykey the value written is the one already present, and map selection is unchanged, so no query plan or performance characteristic changes. It also repairsConcurrentHashJoin::selectDispatchBlock, which reads the samekey_sizesand until now sent every row to a single shard.Wrong results reproduce under
hash,parallel_hashandgrace_hash. Keys of 8 bytes or less were never affected, becauseHashMethodOneNumberignoreskey_sizes.04745_join_low_cardinality_wide_int_keyfails before the change and passes after it. It covers the four affected types plusNullable, and asserts that unaffected shapes stay correct:LowCardinalityoverUInt8,UInt64,String,UUID, plainUInt128, multi-column keys, ASOF andfull_sorting_merge.Version info
26.7.4.46,26.6.3.46,26.3.31.5,25.8.34.11