Repository navigation
Conversation
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
Internal second-model review: adjudication log (click to expand)Independent-model gate (codex) plus my own cold pass: 5 findings, 1 fix round.
I kept the parameter rather than always forcing and relying on the strip at the key site: on current Session id: cron:clickhouse-review-slot-9:20260908-210300 |
|
Workflow [PR], commit [c7a5e99] Summary: ✅
AI ReviewSummaryThis PR fixes the analyzer-side Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit c7a5e99.
Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code |
|
cc @vdimir @novikd, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units158 translation units recompiled, 1686 s compile time in total, 158 of them have a recent master baseline. |
| @@ -0,0 +1,15 @@ | |||
| A Tuple element that is Nullable on one side only | |||
| 1 | |||
| (1) Tuple(Nullable(UInt64)) | |||
There was a problem hiding this comment.
Tuple(UInt64): getMostSubtype dropped the element Nullable because only the left key carried it.
On the statement this line comes from that type is not observable, because the conversion of the whole key column runs before the join filters anything, so it fails instead of printing a type (master fb2a5923):
SELECT t, toTypeName(t) FROM ... USING (t);
-- Code: 349. Cannot convert NULL value to non-Nullable type: while executing
-- 'FUNCTION _CAST(__table1.t, 'Tuple(UInt64)'_String) -> Tuple(UInt64)'
DESCRIBE (SELECT t FROM ... USING (t));
-- t Tuple(UInt64)
The old type is reachable only when the key column happens to hold no NULL, and that case is the one behavior change here. Merged USING key type:
| key types (left / right) | master | this PR |
|---|---|---|
Tuple(Nullable(Int64)) / Tuple(UInt64), a NULL present |
Code: 349 |
Tuple(Nullable(UInt64)) |
| the same types, no NULL in the data | Tuple(UInt64) |
Tuple(Nullable(UInt64)) |
Tuple(Nullable(UInt64)) / Tuple(Nullable(Int64)) |
Tuple(Nullable(UInt64)) |
unchanged |
ON a.t = b.t instead of USING |
Tuple(Nullable(Int64)) / Tuple(UInt64) |
unchanged |
Row 2 is the same test query with tuple(CAST(number + 1, 'Nullable(Int64)')) on the left: identical result rows, only the element type gains Nullable. That fallback subtype for keys with no least supertype landed in #112951 on 2026-08-29 and is not in a release, so no released type changes. The old value cannot be pinned in the .reference (one binary per run); the bottom two rows are in the test as controls.
There was a problem hiding this comment.
For USING projection colum shouln't we alway use supertype, rather then subtype?
There was a problem hiding this comment.
Yes, and that is what happens today: resolveJoinUsing computes tryGetLeastSupertype(expression_types) first and publishes it whenever it resolves. The subtype is reached only under !common_type, which is the case #112951 added it for, so this PR does not change which of the two is chosen. It changes only what the fallback subtype looks like.
Published merged USING type, master 7d76bcdf (without this PR):
| left / right key | published type | |
|---|---|---|
Tuple(Nullable(Int32)) / Tuple(UInt32) |
Tuple(Nullable(Int64)) |
supertype; element Nullable kept, and SELECT t works with a NULL present |
Tuple(Nullable(Int64)) / Tuple(UInt64), LEFT or FULL |
Code: 386 NO_COMMON_TYPE |
no supertype, and the fallback is INNER/SEMI only |
Nullable(Int64) / UInt64 |
Nullable(UInt64) |
fallback, top-level Nullable kept; the query returns 1 |
Tuple(Nullable(Int64)) / Tuple(UInt64), INNER |
Tuple(UInt64) |
fallback, element Nullable dropped, hence the Code: 349 |
Row 2 is where the absence of a supertype is visible: with LEFT the fallback does not apply, so the query stops at cannot infer common type for Tuple(Nullable(Int64)) and Tuple(UInt64). getLeastSupertype refuses that pair with "there is no signed integer type, that can exactly represent all required unsigned integer values", and the analyzer asks for the plain supertype, not the Variant form if uses by default, so there is nothing to publish for it.
Rows 1 and 3 both keep nullability, row 4 is the only one that drops it, and this PR makes row 4 agree with them. So no subtype is published where a supertype exists.
The other option for row 4 is to refuse it too, back to NO_COMMON_TYPE for a Tuple key whose element is Nullable on one side only. count() over that join answers 1 today, so that turns a working query into an error, which is why I did not pick it. Happy to switch if you prefer the refusal.
There was a problem hiding this comment.
Why we edited getMostSubtype call then?
There was a problem hiding this comment.
Because for this pair there is no supertype to publish, so the fallback is what produces the type, and that call is its only producer. tryGetLeastSupertype at QueryAnalyzer.cpp:5770 returns nullptr for Tuple(Nullable(Int64)) and Tuple(UInt64), the fallback at :5791 runs, and the argument I added changes what it yields from Tuple(UInt64) to Tuple(Nullable(UInt64)). The supertype-first order above it is untouched.
The helper has exactly two callers, with opposite requirements, and only the projection one is switched:
- projection (
resolveJoinUsing): the published type is materialized as a plain_CASTover the whole key column, before the join, before any row is dropped (cast_to_super,PlannerJoinsLogical.cpp:221-224), so it has to hold every source value. That is what the flag is documented to give: "returns type which may be used to convert each argument to" (getMostSubtype.h:14). - key comparison (
predicateOperandsToCommonType,JoinStepLogical.cpp:781): unchanged, default mode, and it additionally stripsNullablefrom Tuple elements at:784, so a key value with no representation in the subtype becomes a top level NULL and matches nothing. The tree already keeps the two targets apart: the comment at:808records that the projection casts cannot be reused as the key conversions.
With that one argument set back to false and nothing else changed, 4 of the 8 statements of the new test fail with Code: 349, while the 4 controls (count(), the ON form, out of range, Nullable on both sides) are unchanged.
The one other place this could be fixed is the projection cast itself: keep Tuple(UInt64) published and convert with accurateCastOrNull plus assumeNotNull. I did not take it because trackInputColumn walks exactly those type change nodes and throws LOGICAL_ERROR for any function other than toNullable, CAST and _CAST (optimizeJoin.cpp:1148).
If you prefer either of the alternatives, that one or the refusal from my previous comment, tell me which and I will switch.
67eb3ec
---8<--- PR BODY BELOW ---8<---
Related: #112951
Related: #117409
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Requested by @ nickitat in #117409 (comment).
Projecting the
USINGkey of anINNERorSEMIjoin whose keys have no least supertype throws, whilethe same join without that projection answers correctly:
count()over that join answers1andON a.t = b.treturns(1), so the row the projection failed onis one the join drops.
The merged
USINGcolumn is materialized by converting a whole key column to the type the analyzerpublishes, and it runs before the join filters anything, so that type has to hold every value the source
keys hold.
resolveJoinUsingenforces that at the top level only:getMostSubtypedrops theNullableof a Tuple element when the other side has none, which a top-level test cannot see, so the published type
was
Tuple(UInt64).I ask
getMostSubtypefor the merged type in itsforce_support_conversionmode, documented as "a typewhich may be used to convert each argument to". The published type becomes
Tuple(Nullable(UInt64))andthe existing conversion becomes total. The key comparison keeps the default mode, which is what its
accurateCastOrNullneeds: a key with no representation must be reported by a NULL of the whole key, notof one element. The modes differ only where an element is Nullable on exactly one side.
Worth knowing: for such a key
USINGnow publishesTuple(Nullable(U))where it publishedTuple(U),and the qualified columns of both sides agree on it. Master only since #112951, so no backport;
04669_join_key_no_supertypeis unchanged. Passingfalsefor the new argument reddens the new test.Workflow [PR]
Sync PR [sync-upstream/pr/118902]