Repository navigation
Conversation
`ie_join` casts a `String`/`FixedString` inequality to `String` and demotes a Tuple or Float equality to `equals`. Both disagree with join-key comparison, so another algorithm runs when one can. Cover the available-algorithm boundaries and the only-`ie_join` behavior.
|
Workflow [PR], commit [32d781a] Summary: ✅
AI ReviewSummaryThe PR preserves equality-key comparison semantics by declining preferred Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block. Fix both remaining comparison-semantics gaps and add the focused regression cases above. |
| { | ||
| const auto left = removeNullable(recursiveRemoveLowCardinality(left_type)); | ||
| const auto right = removeNullable(recursiveRemoveLowCardinality(right_type)); | ||
| return (isString(left) && isFixedString(right)) || (isFixedString(left) && isString(right)); |
There was a problem hiding this comment.
[P1] Check mixed string types inside Tuple equality keys
This helper only examines the outer type, so Tuple(String) versus Tuple(FixedString(1)) returns false. keyMatchesValuesEqualsRejects also returns false for these types, leaving ie_join preferred even though tuple element comparison zero-pads the strings and the common join-key conversion does not. The original result inconsistency therefore remains for compound equality keys.
On the PR-head fast-test binary, this returns 1; changing only join_algorithm to 'hash' returns 0:
SELECT count()
FROM values('s String, x Int32, y Int32', ('a\0', 1, 3)) l
ALL INNER JOIN values('s FixedString(1), x Int32, y Int32', ('a', 2, 2)) r
ON tuple(l.s) = tuple(r.s) AND l.x < r.x AND l.y > r.y
SETTINGS join_algorithm = 'ie_join,hash',
query_plan_optimize_join_order_limit = 1,
enable_join_runtime_filters = 0;Compare aligned tuple element types recursively, following the scalar comparison semantics, and add this compound-key case to the regression test.
| const bool can_run_grace_hash = join_settings.canRunGraceHash() && strictness != JoinStrictness::Asof | ||
| && (isInner(kind) || isLeft(kind) || isRight(kind) || isFull(kind)); |
There was a problem hiding this comment.
[P1] Recognize ratio-based spilling when checking grace_hash
canRunGraceHash checks the raw max_bytes_before_external_join and ignores max_bytes_ratio_before_external_join. Runtime algorithm selection instead uses the effective threshold, which includes the ratio. Consequently, this guard treats a runnable grace_hash alternative as unavailable and preserves the incorrect NaN result when spilling is configured through the ratio alone.
On the PR-head fast-test binary, this returns 0; changing only join_algorithm to 'grace_hash' returns 1:
SELECT count()
FROM values('f Float64, x Int32, y Int32', (nan, 1, 3)) l
ALL INNER JOIN values('f Float64, x Int32, y Int32', (nan, 2, 2)) r
ON l.f = r.f AND l.x < r.x AND l.y > r.y
SETTINGS join_algorithm = 'ie_join,grace_hash',
max_bytes_before_external_join = 0,
max_bytes_ratio_before_external_join = 0.1,
query_plan_optimize_join_order_limit = 1,
enable_join_runtime_filters = 0;Use the same effective spill-threshold calculation as runtime selection and cover a ratio-only threshold in the regression test.
| return op == JoinConditionOperator::Equals && JoinCommon::tryGetCommonSubtypeForJoinKeys(left_type, right_type) != nullptr; | ||
| } | ||
|
|
||
| static bool hasAlgorithmForMixedEqualityJoin(const JoinOperator & join_operator, const JoinSettings & join_settings) |
There was a problem hiding this comment.
It's unclear why we are considering join_algorithm there if we only need to prohibit some inequalities specifically for ie_join. We could simply skip unsupported type pairs in the tryGetIEJoinKeyCondition function, so they won't be chosen as an ie join key.
There was a problem hiding this comment.
My original intention was to preserve the query results when the comparison semantics differ. But you’re right.I’ll adjust it accordingly.
| } | ||
|
|
||
| /// `String`/`FixedString` comparison zero-pads. Their common join key is `String`, which does not. | ||
| static bool stringAndFixedStringComparisonDiffersAsJoinKey(const DataTypePtr & left_type, const DataTypePtr & right_type) |
There was a problem hiding this comment.
By the way, I tried to support the String/FixedString combination by converting keys to String, with zero bytes padded or trimmed accordingly, and it seemed to work. However, that PR was not merged though: #118561
But ignoring such a case should be fine for now.
| break; | ||
| } | ||
|
|
||
| if (JoinCommon::keyMatchesValuesEqualsRejects(lhs.getType()) |
There was a problem hiding this comment.
While ignoring String and Tuple(Nullable) is fine, I think supporting floats is crucial for such an algorithm, since they are a generic numeric type and probably widely used. Perhaps we can support them by extracting a NaN mask in the algorithm and handling it the same way as the null mask for Nullable types. We could also extract the null mask not only from top-level Nullable types, so that Tuple(Nullable) would work too. I can check how difficult it would be to support. Then we can decide whether to quickly follow up with float support, or maybe add a setting to keep the current behaviour.
There was a problem hiding this comment.
It seems I got the change slightly wrong. We already support NaN in ie_join inequality keys; the PR is really about the equality = being evaluated differently when it is executed as a residual join condition rather than as a join key. I'm not sure which behavior is the correct one. Technically, the ie_join behavior is more correct: it matches the equals function, and we should not try to mimic the hash join behavior, but rather document such non-obvious behavior for each join algorithm. I believe the same applies to the block nested loop join (it has no dedicated join_algorithm value and is executed as a last resort).
This also matches the direction we are taking elsewhere. #115970 makes hash tables, including hash join keys, agree with equals on -0.0, so after it the only float value where a join key and equals differ is NaN. There was an attempt to close that gap too, by handling NaNs in the hash join, but it turned out to be overcomplicated, and we preferred to keep this one deliberate inconsistency and document it: #106540, #112409
@groeneai check how each algorithm behaves in similar cases, i.e. where handling a JOIN ON condition as a key differs from or agrees with evaluating the same expression as a filter, check that it is documented properly (taking #115970 into account, so that -0.0 is not described as a remaining difference), and open a PR to document it where it is not.
There was a problem hiding this comment.
Acknowledged. I am checking, for each join algorithm, where a JOIN ON condition handled as a key differs from the same expression evaluated as a filter, and comparing that with the current docs; I will follow up here with a docs PR (or my findings) shortly.
There was a problem hiding this comment.
Docs PR: #129361. It adds a per-algorithm "Comparison of JOIN keys" section to the JOIN algorithms page; the JOIN statement page is left alone because #129276 rewrites it.
I measured every algorithm on master with one row on each side. As a key (direct, the hash family, the Join engine, the merge family, and the equality part of ASOF), NaN matches NaN (the hash-based ones only for the same bit pattern), and so do two (1, NULL) tuples and two NULL Variant values. As a condition (ie_join residual, block nested loop) the equality follows equals and none of them match. Two cases depend on more than the algorithm: with a non-zero max_rows_in_set_to_optimize_join, full_sorting_merge first filters each input by a set of the other side's keys that compares NaN by bits and applies only to blocks that arrive after that set is complete, so NaN values with different bit patterns matched in 16 to 29 of 50 runs; and a direct JOIN into a complex_key_direct (or complex_key_cache, complex_key_ssd_cache) dictionary with a CLICKHOUSE source sends the keys as text, where every NaN is written as nan, so only that bit pattern matches.
I left out -0.0 (#115970) and a String with trailing zero bytes against a FixedString (#118561), since those PRs remove both differences. One more case for #118561: WHERE x = y over two tables is planned as a join key (it stays a filter only for the NaN and nested NULL types), so on master it also drops the 'a\0' / toFixedString('a', 1) pair that equals accepts. The -0.0 / 0.0 pair is dropped the same way, which #115970 already covers.
There was a problem hiding this comment.
You’re right—the issue is not NaN handling in IEJoin’s inequality keys, but the difference between evaluating an equality as a residual expression and comparing it as a join key.
I agree that we should not switch algorithms merely to inherit the hash join-key semantics. The remaining differences between algorithms are better made explicit
through the per-algorithm documentation in #129361.
I’ll wait for both #118561 and #115970 to land, then rebase and narrow this PR accordingly.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 26 s compile time in total, 7 of them have a recent master baseline. |
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]>
|
Thank you for the review and guidance, @vdimir. |
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
Motivation
When
ie_joinis listed first, the planner claims two inequality conditions before extracting equality keys. The remaining equality conditions are evaluated as residual filters. This can change results because join-key comparison does not always match scalarequals:NaNvalues andTuplevalues containingNULL, while scalar equality cannot.String/FixedStringcomparisons also differ because scalar comparison zero-pads strings, whereas conversion to their common join-key type can lose that behavior.Avoid preferring
ie_joinwhen demoting an equality condition would change the result, every equality condition can be converted to a valid join key, and another enabled algorithm can execute the join.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix incorrect results when
ie_joinis preferred for joins whose conditions have different semantics as residual expressions and join keys.Workflow [PR]
Sync PR [sync-upstream/pr/129219]