Repository navigation
Normalize trailing zero bytes of String join keys compared with FixedString - #118561
Conversation
…String
The comparison functions compare `FixedString` with `String` as if the shorter
value were padded with zero bytes, so `toFixedString('a', 3) = 'a\0\0'` and
`toFixedString('a', 3) = 'a'` both hold. Join keys were cast to the common
supertype `String`, which drops the trailing zero bytes of the `FixedString`
side only, so `JOIN ON x = y` missed such pairs and `IEJoin` ordered them the
other way round than the `<` operator, disagreeing with the `hash` algorithm
that evaluates the same inequality as a residual filter.
Apply `trimRight(key, '\0')` to the `String` operand whenever the other
operand is a `FixedString`, in `predicateOperandsToCommonType`, so that all
join keys follow the zero-padded comparison of the operator.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [35f4237] Summary: ✅
AI ReviewSummaryReviewed Findings❌ Blockers
Final VerdictStatus: The author has deliberately deferred the findings above; their existing threads were left resolved and received no duplicate replies. The minimum remaining action is to link follow-up issues for the untracked |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes21 object files changed (+123.45 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units185 translation units recompiled, 2338 s compile time in total, 185 of them have a recent master baseline. |
…Keys The join runtime filter and the shuffle of a distributed join take the keys from `preCalculateKeys` and hash them at their least supertype. The `String` side kept its trailing zero bytes there, so with `s JOIN fs ON s.y = fs.x` the filter built on the cast `FixedString` value `'a'` rejected the probe row `'a\0\0'`, and the join lost rows that the `=` operator considers equal. `05136_join_fixed_string_zero_bytes` caught it once the randomized settings disabled the join order optimization, which had hidden the failing orientation. Move the zero-byte normalization into a helper shared by `predicateOperandsToCommonType` and `preCalculateKeys`: the latter now casts the `FixedString` side to the common type and trims the `String` side, so the extracted keys compare byte-wise and the join step itself sees `String` keys. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Add a "Comparison of JOIN keys" section to the JOIN algorithms page and point the `ie_join` and block nested loop sections at it. A JOIN key is compared by the algorithm, not by `equals`. The hash family compares `Float` keys by their bits, so a `NaN` matches only a `NaN` with the same bit pattern; the sort-merge family compares with `IColumn::compareAt`, where any two `NaN` are equal; both match a `Tuple` with a `NULL` element and a `NULL` `Variant`, for which `equals` returns `NULL`. `ie_join` evaluates a remaining equality as a residual filter or post-join filter with `equals`, and the block nested loop fallback evaluates the whole `ON` condition, so neither matches those pairs. Measured per algorithm on master (26.11.1.1) with one row on each side. `-0.0` vs `0.0` and a `String` with trailing zero bytes vs `FixedString` are left out on purpose: ClickHouse#115970 and ClickHouse#118561 remove those differences. The JOIN statement page is not edited because ClickHouse#129276 rewrites it. Related: ClickHouse#129219 Related: ClickHouse#112409 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The JOIN casts both keys to their common type and compares the results.
Several optimizations assume that `=` means the same and treat a key pair
`x = y` as a plain equality: the join reorderer builds transitive
equivalence classes from it, the Cascades optimizer shuffles on it, the
filter pushdown substitutes `CAST(x, supertype)` for `y`, and a join can
be converted to `IN`. For `FixedString` vs `String` this does not hold:
`toFixedString('a', 3)` is equal to both `'a'` and `'a\0\0'`, so
`x = y AND x = z` does not imply `y = z`, and no expression of `x` is the
matched `y`. The reorderer dropped or invented `y = z`, the Cascades
shuffle sent matching rows to different nodes, the pushed-down copy of a
filter removed rows the join matches, and `IN` missed them.
Add `JoinCommon::isExactJoinKeyEquality`, which accepts only the pairs for
which the assumption holds: equal types up to `Nullable` and
`LowCardinality`, two numbers, two date or time types, or two
`FixedString`. Gate the four optimizations with it; every other pair is
declined, so new types are safe by default.
Review comments:
#118561 (comment)
#118561 (comment)
Co-Authored-By: Claude Fable 5.1 <[email protected]>
…o inner rewrite `isNotDistinctFrom` cast Nullable `FixedString` and `String` operands to the common type `String`, which trims the trailing zero bytes of the `FixedString` side only, so the function disagreed with `=` and with a JOIN on it, which normalizes such keys. Compare the nested values as `=` does. `tryConvertAnyOuterJoinToInnerJoin` treats a GROUP BY on the join keys as proof that each row matches at most one group, which does not hold for a `FixedString` vs `String` key pair. Skip such keys. #118561 (comment) #118561 (comment) Co-Authored-By: Claude Fable 5.1 <[email protected]>
Tuples are compared element-wise, so `tuple(toFixedString('a', 3)) = tuple('a\0\0')` is true, but a JOIN on
such keys cast both to the common type, which trims only the `FixedString` element, and missed these rows.
`predicateOperandsToCommonType` and `preCalculateKeys` now trim the `String` elements too, at any depth of
nested tuples, including `Nullable(Tuple)`. Arrays and maps are compared after a cast to the common type by
the operators as well, so they are left as is.
`isNotDistinctFrom` and `isDistinctFrom` compared `Nullable(Tuple)` operands after a cast, unlike `equals`.
They now compare such tuple elements zero-padded too, so the JOIN on `<=>` agrees with the operator.
#118561 (comment)
Co-Authored-By: Claude Fable 5.2 <[email protected]>
yariks5s
left a comment
There was a problem hiding this comment.
I'm afraid isExactJoinKeyEquality + rejecting in some cases will affect performance. But I think it is okay considering the simplicity of the fix.
current implementation lgtm (considering the bug with tuples), but I have quick questions about nice-to-have things:
questions
-
for shuffle, could we use normalized keys instead of excluding the pair? I think we can lose valid distributed plan
-
Also I think we reject cases like tuple(uint8) vs tuple(uint16)
`Nullable` tuples with a `FixedString` vs `String` element pair are compared element by element since 107fa81. An empty tuple element then reached `FunctionComparison`, which throws `NOT_IMPLEMENTED` for zero-sized tuples. Before that commit such operands were cast to the common type and compared as whole values, which accepts an empty tuple, so the comparison returned a result. Two empty tuples are now not distinct. This also holds without `Nullable`, where both functions threw the same exception before. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…g` conversion mode #118561 added a cast of the keys to the common type in `preCalculateKeys` (used by the runtime filters). It was built without a context, which now keeps the trailing zero bytes of a `FixedString`, so `JOIN ... USING` with runtime filters dropped all rows (`05334_cast_fixed_string_to_string_join_keys`). Convert the keys as `predicateOperandsToCommonType` does: without the trailing zero bytes and under the distinct name `_CAST_strip_trailing_zeros`. Update the tests from master that pin the old conversion: - `05136_join_fixed_string_zero_bytes`: arrays with `FixedString` elements are now compared element by element, zero-padded like the scalars, so the operator and the join both give 8 instead of 4. - `05137_join_fixed_string_zero_bytes_runtime_filter`: the new name of the key cast in `EXPLAIN`. - `05813_parquet_fixed_string_null_as_default`: a NULL read as `String` is the default `FixedString(2)` converted to `String`, which now keeps its two zero bytes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
ClickHouse#129219 concluded that `ie_join` should not yield to another algorithm when a residual `equals` differs from a join key: the remaining differences (`NaN`, `NULL` inside tuples) are documented per algorithm in ClickHouse#129361, and ClickHouse#118561 makes `String`/`FixedString` keys, including `ie_join` range keys, compare like the scalar functions. The `hash` requirement for merging ranges now states its actual reason: a merged range is a non-equi `ON` condition. Add the `String`/`FixedString` regressions from the review of ClickHouse#128140, and drop the `parallel_hash` case: `parallel_hash` is now the same algorithm as `hash`. Related: ClickHouse#129219 Related: ClickHouse#128140
Drop the `String`/`FixedString` cases (fixed in master by ClickHouse#118561), the `NaN`/`Tuple(Nullable)` key cases (documented per algorithm in ClickHouse#129361), and the reorder cases that repeat one query under other join or reorder algorithms. Shorten the comment of the performance test.
Closes: #116831
toFixedString('a', 3) = 'a'andtoFixedString('a', 3) = 'a\0\0'are both true, but a JOIN on aFixedStringand aStringkey cast both toString, which trims only theFixedStringside. SoJOIN ON x = ymissed such rows, andie_joinordered keys differently from<. Now theStringkey is trimmed too, and the join agrees with the comparison operators for every algorithm. Such an equality is not transitive, so the optimizations that derive predicates from join keys (transitive predicates, Cascades shuffle keys, filter copy to the other join side, join toIN) skip these key pairs viaJoinCommon::isExactJoinKeyEquality, which allows only equal types, numbers, date and time types, andFixedStringpairs.Tuples are compared element-wise, so the same applies to
FixedStringandStringelements of tuple keys at any depth, includingNullable(Tuple): theStringelements are trimmed too. Arrays and maps are compared after a cast to the common type by the operators as well, so they are unchanged.isNotDistinctFromandisDistinctFromcomparedNullableoperands of these types after a cast, unlikeequals; they now compare them zero-padded, also as tuple elements, so a JOIN on<=>agrees with the operator.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
JOINonFixedStringandStringkey pairs, also as elements of tuple keys: values that differ only in trailing zero bytes now match and sort the same way as with the comparison operators.isNotDistinctFromandisDistinctFromnow agree withequalsforNullablevalues of these types. Optimizations that derive predicates from join keys (transitive predicates, filter pushdown to the other side) no longer apply to such key pairs.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/118561]
Version info
26.11.1.2640-master(included in26.11and later)