Repository navigation
Fix arrayIntersect matching values that overflow the common element type when the arguments are Nullable - #118485
Conversation
…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]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 gate findings plus 6 from my own
Also checked and clean, so you do not have to re-derive it: One pre-existing behavior I noticed and deliberately left alone: the intersect result pass consults Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260906-223800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260906-221800 |
|
Workflow [PR], commit [a0cd55e] Summary: ✅
AI ReviewSummaryThis PR fixes Final Verdict✅ No findings. LLVM Coverage ReportMeasured on commit a0cd55e.
Changed lines: Changed C/C++ lines covered: 7/7 (100.00%) · Uncovered code |
|
cc @Ergus, could you review this? The overflow guard in |
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, 12 s compile time in total, 7 of them have a recent master baseline. |
…ersect-nullable-overflow-mask
|
@groeneai @PedroTadim — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
The finding is real and is fixed in #118966: stripping |
Closes: #114587
Related: #117577
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes
arrayIntersectreturning elements that do not survive the cast to the common element type when the overflowing argument isNullable:arrayIntersect([toNullable(1)], [toNullable(257)])returned[1]wherearrayIntersect([1], [257])correctly returned[]. The guard that drops such elements tested the element type without removingNullable, so it never fired for aNullableorLowCardinality(Nullable)argument, in any of its integer,Date,DateTimeandDateTime64arms.arrayUnionandarraySymmetricDifferenceshare that guard, where one notNullableargument of a different width failed withILLEGAL_COLUMNinstead:arrayUnion([1], [toNullable(257)])now returns[1,257], likearrayUnion([1], [257]).Description
prepareArraysbuilds an overflow mask so elements that do not survive the narrowing cast cannot match, by re-comparing the cast column against the original withnotEquals(:400-419, consumed at:714) for integer-like element types only. Both types feeding that test came fromremoveNullable(<arg>.type), which strips the wrapper off the array type, neverNullablehere, and not off the element type. So the element type ofArray(Nullable(UInt8))staysNullable(UInt8),WhichDataTypereportsTypeIndex::Nullable, all four predicates return false, the mask block is skipped, and the truncated257 -> 1matches1.The fix removes
Nullablefrom both element types at their single declaration site, covering all four arms. Both are needed, not only the condition: the nested columns are already unwrapped out ofColumnNullableabove, so leaving the typesNullablemakescallFunctionNotEqualsdescribe a column it does not hold and throwILLEGAL_COLUMN. No cast, result type, type list or NULL handling changes; the same shape already exists at:281.arrayUnionandarraySymmetricDifferenceshare this code, and there the strip also removes an error: with one notNullableargument of a different width the guard compared aNullabletype against a plain column, soarrayUnion([1], [toNullable(257)])failed withILLEGAL_COLUMN. It now returns[1,257], as it always has withouttoNullable;arraySymmetricDifferenceis the same.Validated in both directions from a deterministic one-query repro and 46 probes over mixed widths, type wrappers, const and materialized paths, NULL elements, boundary values and all three set functions: no input gains a new exception, and every changed case returns what the not
Nullablespelling has always returned.Workflow [PR]
Sync PR [sync-upstream/pr/118485]