Repository navigation
Make the result nullability of arrayIntersect follow its declared return type - #117577
PedroTadim merged 2 commits into
Conversation
…eturn type `getReturnTypeImpl` declares `Array(getMostSubtype(nested_types, true))`, which is `Nullable` only when every argument is. `executeImpl` separately computes the cast target as `getMostSubtype(data_types, true, force_support_conversion = true)`, and that third flag makes the common type `Nullable` as soon as one argument is. `castColumns` preserves each argument's own nullability only for the types in a hand-written list (`isNumber`, `isDate`, `isDateTime`, `isDateTime64`, `isStringOrFixedString`); every other nested type is cast to that common type, so all arguments become `Nullable`. `execute` then derived `all_nullable` from those cast arguments and used it for the `ColumnNullable` wrap, the `use_null_map` flag and the `NULL`-emission gate. For an intersection with some-but-not-all `Nullable` arguments that produced a `Nullable` column against a not `Nullable` declaration, and emitted a `NULL` that an intersection with a not `Nullable` argument cannot contain. Record the declared nullability once on `UnpackedArrays` and use it at those sites instead. No cast is changed, no type list extended and no type check loosened, so this covers every affected nested type at once rather than only the one that was reported. There are two symptoms, because `castRemoveNullable` discards the null map when it strips a wrap. `Date32` has its own dispatch branch and never reaches that stripper, so the mismatch surfaces as an `Unexpected return type from arrayIntersect` exception, thrown from any of the six `columnMatchesType` guards (`Analyzer/Utils.cpp`, `resolveFunction.cpp`, `ColumnFunction.cpp`, `ActionsDAG.cpp` twice, `ExpressionActions.cpp`). `UUID`, `IPv4`, `IPv6`, `Time`, `Time64`, `Interval`, `JSON` and `QBit` take the generic branch, where the stripper hides the mismatch and the emitted `NULL` silently becomes a default-valued element; for `Enum8` and `Enum16` that element is not a valid enum value, so it throws `Unexpected value 0 in enum` instead. `arrayUnion` and `arraySymmetricDifference` declare `getLeastSupertype`, which is `Nullable` exactly when the cast makes all arguments `Nullable`, so their declared nullability and the old derivation always agreed. They are unaffected, and measured unchanged across the whole type matrix. Introduced by 3ba2d11, which added the `isDate32` dispatch branch without adding `isDate32` to the `castColumns` list. Found by `FunctionsStress` in `Unit tests (tsan, function_prop_fuzzer)`. Validated in both directions from a deterministic single-query repro and over 23 nested types in both argument positions, with and without an actual `NULL`: exactly the 14 defective probes change, across 11 nested types, and every control is byte-identical. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model reviewRound 1 of an independent cold review plus a second-model gate, adjudicated against the recorded Blocker raised and refuted ❌ Agreed and fixed
Notes, not blocking 💡 The overflow-mask guard in the same function tests 💡 |
|
Workflow [PR], commit [6ed4ec7] Summary: ✅
AI ReviewSummaryThis PR makes Missing context / blind spots
Final Verdict✅ No review findings. LLVM Coverage Report |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260901-173800, cron:clickhouse-impl-slot-5:20260901-191600 |
|
cc @george-larionov @KochetovNicolai, 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 units7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline. |
Picks up ClickHouse#117580, which repairs the bugfix-validation master-build S3 lookup. Without it `Bugfix validation (functional tests, amd64/aarch64)` aborts before running any test and reds the Workflow Post Hook.
CI finish ledger - c5e46b7Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task whose
The rest of the run is green: 148 of 170 check runs Session id: cron:our-pr-ci-monitor:20260902-023000 |
…sk tests
castColumns narrows every argument to the most subtype of the element types, so
a wider argument can be truncated (257 to 1 as UInt8). prepareArrays stops the
truncated values from matching by building an overflow mask: it re-compares the
cast column against the original with notEquals and skips every element where
they differ. The mask is built only for integer-like element types.
Both element types feeding that test came from
typeid_cast<const DataTypeArray &>(*removeNullable(<arg>.type)).getNestedType()
where removeNullable strips the wrapper off the array type, which is never
Nullable here so it is a no-op, and not off the element type. For
Array(Nullable(UInt16)) the element type therefore stayed Nullable(UInt16),
WhichDataType reported TypeIndex::Nullable, and isInteger, isDate, isDateTime
and isDateTime64 all returned false. The whole mask block was skipped and
arrayIntersect([toNullable(1)], [toNullable(257)]) returned [1] while the not
Nullable spelling arrayIntersect([1], [257]) has always returned [].
The condition reads the initial argument's type, so the guard was lost only
when the overflowing argument was itself Array(Nullable(...)); one Nullable
argument on the non-overflowing side left it working.
Stripping Nullable at the single declaration site fixes all four arms at once
and covers Array(LowCardinality(Nullable(T))) too, since IExecutableFunction
materializes a nested LowCardinality away before executeImpl runs. Both types
are stripped, not only the ones in the condition: arg.nested_column and
initial_column are already unwrapped out of ColumnNullable above, so leaving
the types Nullable makes each ColumnWithTypeAndName describe a column it does
not hold, which reaches columnGetNested via defaultImplementationForNulls and
throws ILLEGAL_COLUMN. That variant was built and measured rather than assumed.
The same shape already exists in this function, where castColumns feeds
isNumber/isDate/isDateTime/isDateTime64/isStringOrFixedString from
removeNullable(type_nested). No cast, result type, type list or NULL handling
changes.
arrayUnion and arraySymmetricDifference share prepareArrays, and there the
strip also removes an error. Their common element type comes from
getLeastSupertype, so it is Nullable as soon as one argument is, and a not
Nullable argument of a different width is cast to Array(Nullable(T)) while its
own initial column stays unwrapped. Pre-fix the guard therefore saw a plain
initial element type, fired, and handed callFunctionNotEquals a still Nullable
cast type against that unwrapped column: arrayUnion([1], [toNullable(257)])
and arraySymmetricDifference([1], [toNullable(257)]) failed with
ILLEGAL_COLUMN. Both now return [1,257], which is what each has always
returned without toNullable, and the arrayUnion case is one of the test lines.
arrayIntersect never reaches that state because getMostSubtype keeps Nullable
only when every argument has it, so its not Nullable argument is not cast at
all.
The mask stays unconditional. It is redundant for the sibling modes, whose
common element type comes from getLeastSupertype, which covers all values of
its inputs and throws NO_COMMON_TYPE rather than returning a lossy type, so
their masks are all zero. That redundancy is already there on master for a not
Nullable argument of a differing width, so it is not introduced here, and
gating construction on the set mode would make those two functions depend on
that property holding for every present and future type pair, with a silently
wrong result rather than a dead comparison if it ever stops holding. Narrowing
the guard is a separate change to two functions this fix is not about.
Verified in both directions from a deterministic one-query repro and across 46
probes over mixed widths, LowCardinality(Nullable), const and materialized
paths, NULL elements, boundary values, decimals, strings, tuples, mixed
nullability and all three set functions: no input gains a new exception, one
loses the ILLEGAL_COLUMN above, and every case whose result moves now returns
what the not Nullable spelling has always returned. Date32 stays outside the
guard's type list and is unchanged.
Closes: ClickHouse#114587
Related: ClickHouse#117577
Co-Authored-By: Claude Opus 5 <[email protected]>
Related: #114587
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes
arrayIntersectwhen only some of its arguments areNullable. Such a call could fail withUnexpected return type from arrayIntersectoverArray(Nullable(Date32))andArray(Date32), return a spurious default-valued element forUUID,IPv4,IPv6,Time,Time64,Interval,JSONandQBit, or fail withUnexpected value 0 in enumforEnum8andEnum16. An intersection that has a notNullableargument cannot containNULL, so the result now follows the declared return type;arrayUnionandarraySymmetricDifferenceare not affected.Description
getReturnTypeImpldeclaresArray(getMostSubtype(nested_types, true)), which isNullableonly when every argument is.executeImplseparately computes the cast target withforce_support_conversion = true, which makes itNullableas soon as one argument is.castColumnspreserves each argument's own nullability only for the types in a hand-written list (numbers,Date,DateTime,DateTime64,String/FixedString); every other type is cast to that common type, so all arguments becomeNullable.executethen derived the result's nullability from those cast arguments, wrapping the result inColumnNullablealthough the declared type was notNullable, and emitting aNULLthe intersection cannot contain.The fix records the declared nullability once on
UnpackedArraysand uses it for the wrap, the null-map flag and theNULL-emission gate. No cast is changed, no type list extended and no type check loosened, so every affected nested type is covered at once, not only the reported one.Hence two symptoms.
Date32has its own dispatch branch, so the mismatch surfaces as an exception; the other types take the generic branch, wherecastRemoveNullablestrips the wrap and discards the null map, so the emittedNULLsilently becomes a default-valued element instead. Both are reachable on the ordinary execution path, not only under constant folding.Found by
FunctionsStressinUnit tests (tsan, function_prop_fuzzer): report. Validated in both directions from a deterministic one-query repro and over 23 nested types in both argument positions: exactly the 14 defective probes change, across 11 nested types, and every control is byte-identical. #114587 is a different defect in the same function and stays untouched: this change alters no cast and no overflow mask.Workflow [PR]
Sync PR [sync-upstream/pr/117577]
Workflow [PR]
Sync PR [sync-upstream/pr/117577]
Version info
26.9.1.523(included in26.9and later)