Repository navigation
Stop PK-range join sharding at a key column that can hold a NaN - #118918
Conversation
The layer split of `query_plan_join_shard_by_pk_ranges` filters every layer with `greater(tuple(pk), tuple(border))`. An IEEE comparison answers false for `NaN` against anything, so a row with a `NaN` key fails the filter of every layer - including the last one, which only has a lower bound - and is dropped at read time. With a `Float` primary key holding `NaN` the join silently returned fewer rows: the merge join's own comparator treats `NaN` as equal to `NaN`, so those rows do participate in the join when the optimization is off. Every other consumer of `splitIntersectingPartsRangesIntoLayers` gates on `isSafePrimaryKey`, which rejects `Float32`/`Float64`/`BFloat16`, `Nullable` and containers of those. The join path had no such check. Since it splits by a primary key *prefix*, the check is applied per column while the common prefix is computed: an unsafe column ends the prefix, so a key like `(id UInt64, x Float64)` joined on `id` keeps sharding. `isSafePrimaryDataKeyType` moves out of the anonymous namespace of `PartsSplitter.cpp` to be declared alongside `isSafePrimaryKey`.
|
Workflow [PR], commit [d3b4605] Summary: ✅
AI ReviewSummaryThis PR fixes the row-loss bug in Tests
Final VerdictLLVM Coverage Report |
| DROP TABLE t_join_shard_left; | ||
| DROP TABLE t_join_shard_right; | ||
|
|
||
| -- An integer key still shards: the same counts with the optimization on and off. |
There was a problem hiding this comment.
The new behavior worth pinning here is not just that a fully safe key still returns the right rows; the existing query_plan_join_shard_by_pk_ranges tests already cover ordinary safe-key sharding. This UInt64 control would still pass if findCommonPrimaryKeyPrefixByJoinKey started rejecting any PK that merely contains a float in a later component, which is exactly the mixed-key regression this patch claims to avoid. Please replace or extend it with a mixed key such as ORDER BY (id, x) joined on id, and add an EXPLAIN / Sharding: assertion (or another direct plan-level proof) that the join is still sharded.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units119 translation units recompiled, 1273 s compile time in total, 119 of them have a recent master baseline. |
…loat key A join runtime filter is a pre-filter: the join re-evaluates its own condition afterwards, so a filter that passes too many rows is harmless, while one that rejects a row the join would have matched loses that row for good. ExactSetRuntimeFilter::finishInsert replaced set membership with the SQL `equals` function (`notEquals` for the negated, ANTI-join filter) as soon as the build side collapsed to a single distinct key. Hash tables compare float keys bitwise, deliberately, and `equals` does not agree with that: NaN is not equal to itself, so the non-negated filter rejected NaN rows the join does match, and 0.0 equals -0.0, so the negated filter rejected rows ANTI owed as non-matched. A plain inner join on an all-NaN Float64 key returned no rows at stock defaults, and the correlated scalar subquery in the linked issue came back NULL because the filter had emptied the probe arm of the join it was decorrelated into. Take the shortcut only where `equals` agrees with a bitwise comparison. The predicate descends the key type, so it also covers a float inside Array, Tuple, Map or LowCardinality, and it matches JSON at the root because a float can appear on a path discovered while reading, which is not among the type's static children. Variant and Dynamic need no term: canContainNull is true for both, so the adjacent !argument_can_have_nulls check already sends them to the Set path. Measured, not assumed. The same shape was used in ClickHouse#118918 for the sibling primary-key range sharding NaN bug. Releases 26.7 and 26.8 carry the same shortcut, so this is not a master-only regression. Closes: ClickHouse#119679
The PK-range join sharding (
query_plan_join_shard_by_pk_ranges) splits the parts of both sides into layers by primary-key ranges and filters every layer withgreater(tuple(pk), tuple(border)). An IEEE comparison answers false forNaNagainst anything, so a row with aNaNkey fails the filter of every layer - including the last one, which only has a lower bound - and is dropped at read time. With aFloatprimary key holdingNaN,SELECT count() FROM l INNER JOIN r ON l.k = r.kreturned 1000 instead of 1025, and aLEFT JOINlost theNaNrows of the left table entirely: the merge join's own comparator treatsNaNas equal toNaN, so those rows do participate in the join without the optimization.Every other consumer of
splitIntersectingPartsRangesIntoLayers- theFINALsplit, the distributedFINALbucketing inReadFromMergeTreeandoptimizeLazyFinal- already gates onisSafePrimaryKey, which rejectsFloat32/Float64/BFloat16,Nullable, and containers of those. The join path had no such check. Since it splits by a primary-key prefix, the check is applied per column while the common prefix is being computed: an unsafe column ends the prefix instead of disabling the optimization for the whole table, so a key like(id UInt64, x Float64)joined onidkeeps sharding.Closes: #117873
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed rows being silently dropped from a join when
query_plan_join_shard_by_pk_rangesis enabled and the primary key is a floating-point column containingNaN.Workflow [PR]
Sync PR [sync-upstream/pr/118918]
Version info
26.9.1.1097(included in26.9and later)