Repository navigation
Convert the JOIN keys to the type of the values that they have in common - #112951
Conversation
There is no type that is able to hold all the values of both `UInt64` and `Int64`, so a JOIN on such keys was rejected with `NO_COMMON_TYPE`. But a JOIN does not need the least supertype. It is enough to convert both keys to the type of the values that they have in common - the most subtype, which is `UInt64` for this pair, holding the values from `0` to `9223372036854775807`. A value outside of this range is not equal to any value from the other side, so `accurateCastOrNull` converts it to NULL, and it does not match anything. This is applied to the equality conditions in the `ON` section, and to `USING` for INNER JOIN, where the result contains only the matched values. It is not applied to null-safe comparisons, where NULL matches NULL, and to the ASOF inequality, which needs the order of the values, not only their equality. A runtime filter is not built for such a JOIN, because a `NOT IN` filter over a set with NULLs would drop the rows that have to be preserved. Closes: #21794
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [bc5c703] Summary: ✅
AI ReviewSummaryThis PR teaches analyzer-based Final VerdictStatus: ✅ Approve LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 109/111 (98.20%) · Uncovered code |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 71/72 (98.61%) · Uncovered code |
… explicitly The fallback is implemented in `QueryAnalyzer::resolveJoin` and `JoinStepLogical`, so with `enable_analyzer = 0` the old `TableJoin::inferJoinKeyCommonType` path still rejects such a JOIN. Say this in the documentation and set `enable_analyzer = 1` in the test, which was failing in the "old analyzer" CI run. Also correct the documentation, which said that the conversion is not applied to `USING`, while it is applied to `USING` of an `INNER JOIN`.
|
🕵 The The |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units1202 translation units recompiled, 8755 s compile time in total, 1202 of them have a recent master baseline. |
|
We might be better off changing the behavior in all cases. But I understand the need to preserve compatibility, so the solution is good. |
…-subtype # Conflicts: # docs/en/sql-reference/statements/select/join.md
…ck for ASOF USING equality prefix The subtype fallback in `predicateOperandsToCommonType` could reuse a plain `CAST` node from `actions_after_join_map` (added by `buildJoinUsingCondition` for the `USING` output column) when the common subtype is already `Nullable`. A plain `CAST` wraps out-of-range values instead of turning them into `NULL`, so `-1` could match `18446744073709551615`. Now the fallback always applies `accurateCastOrNull` to the keys. In `QueryAnalyzer::resolveJoin`, the fallback was disabled for every `USING` key of an ASOF join, while only the last column in the `USING` list is compared by the order of the values. Now only that column is excluded, so `ASOF JOIN ... USING (k, t)` accepts an equality key `k` of `UInt64` on one side and `Int64` on the other, consistent with the equivalent `ON` form.
A user-written SEMI JOIN is represented with `JoinKind::Left`/`JoinKind::Right` and `JoinStrictness::Semi`, so the fallback gated to `JoinKind::Inner` rejected `SEMI JOIN ... USING` with `NO_COMMON_TYPE` while the equivalent `ON` condition worked. Only the matched rows of the preserved side survive a SEMI JOIN and the result of `USING` takes the key of the preserved side, so the fallback is safe there. ANTI JOIN keeps exactly the unmatched rows, so it stays excluded. Co-Authored-By: Claude Fable 5 <[email protected]>
…master After the merge, the null-safe `IS NOT DISTINCT FROM` join predicate on `UInt64`/`Int64` keys is type-checked during join planning and fails with `NO_COMMON_TYPE` instead of `ILLEGAL_TYPE_OF_ARGUMENT`. The query is still rejected; accept both error codes in the test hint. Co-Authored-By: Claude Fable 5 <[email protected]>
|
🕵 The remaining CI failures on
@groeneai, investigate these failures and provide fixes in separate PRs. If a fix is already in progress, link it here. The |
|
Investigated both. Neither is caused by this PR, and they have different owners. 1.
|
|
#113653 opens this, and it does not do what I said it would. Two corrections to my earlier comment. The remedy is different, and better. I said I would make the rename happen before shutdown races it. The rename is not the problem: the lock it waits for is held by a leaked task. Rotating the log earlier would have moved And my timing argument was wrong. I wrote that One more correction while I am here: the 60 s is not the default. The allowlist remedy stays rejected, for the reason I gave: it would mask a real lock stall on the authentication log. |
The `is_implemented` macro in `01721_join_implicit_cast_long.sql.j2` expands
to an empty string, so a query line written as `...; {{ is_implemented(join_algorithm) }}`
is echoed with a trailing space. The four `FULL JOIN` / `RIGHT JOIN` lines of the
`Int64` / `UInt64` no-supertype block were switched to that form by this pull request,
but commit `8f17828c7bc3` then stripped the trailing space from the reference instead
of adding it, so the test failed with `result differs with reference` in every
stateless job.
Restore the trailing space, matching every other `is_implemented` line in the
reference. Verified locally: the rendered test now matches the rendered reference
exactly with empty stderr.
|
🕵 Fixed the real red on
The
@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112951&sha=fec9a3274a7890b17b500bbed359850cb21b9af0&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. Merged |
|
Confirmed not caused by this branch. The fix is #113242, and I verified it covers this route rather than assuming the STID match. Carrier, re-derived from this run's artifacts: Your master reach is right. I checked it on New, and the reason for the daily volume: the wave is amplified by my own test. So the stress lanes can be quietened without waiting on the #113735 ruling. I will send a separate PR pinning |
|
The separate PR is #115839. One difference from what I described above: the pin is one file-level Measured on one debug binary differing only in the test file, with the fuzzer armed: unpinned #113242 still owns the defect itself, and I will post here when it merges. |
alexey-milovidov
left a comment
There was a problem hiding this comment.
The changes are correct and fairly simple.
The only downside of this is some loss of generality. But it is required to fix the bug and improve SQL compatibility.
The merged USING column is materialized by converting a whole key column to
the type the analyzer publishes for it, and that conversion runs before the
join filters anything: `buildJoinUsingCondition` registers a plain `_CAST`
into that type, and `convertToPhysical` places it in the pre-join DAG. The
published type therefore has to hold every value the source keys hold.
`resolveJoinUsing` applies that rule at the top level only, publishing
`makeNullable(subtype)` when either source key is Nullable. `getMostSubtype`
drops the `Nullable` of a Tuple element when the other side's element has
none, and that nullability is invisible to a top-level test, so for
`Tuple(Nullable(Int64))` joined against `Tuple(UInt64)` the published type
was `Tuple(UInt64)` and a NULL element failed the conversion:
SELECT t
FROM (SELECT tuple(if(number = 0, NULL, CAST(1, 'Nullable(Int64)'))) AS t FROM numbers(2)) AS a
INNER JOIN (SELECT tuple(CAST(1, 'UInt64')) AS t) AS b USING (t);
-- Code: 349. Cannot convert NULL value to non-Nullable type
`count()` over that same join answers 1 and `ON a.t = b.t` returns `(1)`, so
the row the projection failed on is one the join drops anyway.
Ask `getMostSubtype` for the merged USING type in its `force_support_conversion`
mode, whose documented contract is exactly "a type which may be used to convert
each argument to": the published type becomes `Tuple(Nullable(UInt64))` and the
existing conversion becomes total. The key comparison keeps the default mode,
which is what `predicateOperandsToCommonType`'s `accurateCastOrNull` needs, a
value with no representation being reported by a NULL of the whole key rather
than of one element. The two modes differ only where a Tuple element is
Nullable on exactly one side. On master the key site strips such elements
itself (`removeNullableInsideTuple`), so the two modes could also be one; the
parameter keeps each call site's target explicit and leaves the key path's
computation unchanged.
Related: ClickHouse#112951
Related: ClickHouse#117409
Closes: #21794
There is no type that is able to hold all the values of both
UInt64andInt64, so a JOIN on such keys was rejected:But a JOIN does not need the least supertype. It is enough to convert both keys to the type of the values that they have in common - the most subtype, which is
UInt64for this pair, holding the values from0to9223372036854775807. A value outside of this range is not equal to any value from the other side, soaccurateCastOrNullconverts it to NULL, and it does not match anything.This is applied to the equality conditions in the
ONsection, and to theUSINGsection of anINNER JOINor aSEMI JOIN, where the result contains only the matched values. It is not applied to:USINGthat can emit unmatched rows (LEFT,RIGHT,FULL,ANTI), where the key is also a column of the result and can hold the unmatched values;IS NOT DISTINCT FROM), where NULL matches NULL, so the values converted to NULL would match each other;USINGlist), which needs the order of the values, not only their equality;Jointable engine whose stored key is not itself the common type, because its hash table is prebuilt over the original key values and cannot be converted - the opposite direction, where only the probe key has to be converted, is supported.A runtime filter is not built for such a JOIN, because a
NOT INfilter over a set with NULLs would drop the rows that have to be preserved.Only the queries that were failing before are affected - the type inference is unchanged whenever the least supertype exists.
It is implemented in the analyzer, which is enabled by default; with
enable_analyzer = 0the oldTableJoin::inferJoinKeyCommonTypepath still rejects such a JOIN, exactly as before.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Allow a JOIN on keys that have no common supertype, such as
UInt64andInt64. Previously such a query was rejected withThere is no supertype for types UInt64, Int64. Now the keys are converted to the type of the values that they have in common, and a value outside of this range does not match anything. This requires the analyzer (enable_analyzer = 1, the default).Workflow [PR]
Sync PR [sync-upstream/pr/112951]