Repository navigation
Fix a logical error comparing arrays whose element type is Nothing - #113609
diegomestre2 merged 5 commits into
Conversation
Comparing two arrays whose element types have no least supertype takes the element-wise lexicographic path added by ClickHouse#110245. When a position holds a bare `Nothing`, `getReturnTypeImpl` declared a plain `UInt8` while the executing element comparator declared `Nothing`, so the unconditional `assert_cast<const ColumnUInt8 &>` on the element result received a `ColumnNothing` and killed the server: SELECT CAST([], 'Array(Nullable(Nothing))') > [[1]]; Logical error: 'Bad cast from type DB::ColumnNothing to DB::ColumnVector<char8_t>' The divergence exists because validation recurses into `getReturnTypeImpl` directly, which has no `Nothing` handling, while execution goes through the overload resolver, which short-circuits to `DataTypeNothing`. It is reachable from ordinary SQL, both during constant folding and at runtime over a table, and through `Tuple` members at any array depth. Close it from both ends. Validation now rejects a position that nothing can decide: a bare `Nothing`, directly or through a `Tuple` member. Two `Nothing` shapes stay decidable and keep answering as they do today -- `Nullable(Nothing)` is decided by its own all-set null map, and `Array(Nothing)` shares a supertype with `Array(T)` so it never reaches this branch. Each side is classified independently, because one side's null map never decides a position on the other side. `Array` and `Map` are not descended, matching `FunctionsNullSafeCmp::containsNothing`; depth is still covered because the array branch recurses once per level and is only consulted where that level has no supertype. Execution answers what validation accepted and fails closed otherwise: a result is synthesised only where a direct `Nothing` argument is paired with its own all-set null map, and any other `Nothing` element type throws instead of fabricating a `false`. The direct-recursion optimisation is kept. An untyped NULL or empty array against a non-supertypable partner now answers instead of aborting, matching the supertype path. The regression never shipped in a release.
…erand orders The bare equality arms are reached through the execution-side guard as well, so they pass even when the analysis-side classifier is pairwise rather than per-side. Asserting toTypeName in both operand orders pins the classifier itself: a pairwise form stops at the first tuple member and accepts a pair whose other member holds a bare Nothing.
Internal second-model review (click to expand)An independent model reviewed this change cold, without access to the author's notes, and its
The block is entered by every accepted What is genuinely unpinned is only the 💡 Stress mode rewrites the new test's 💡 Gitignored Session id: cron:clickhouse-review-slot-10:20260806-015500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260806-010002 |
|
cc @diegomestre2 @nihalzp, could you review this? The array-no-supertype branch of |
|
Workflow [PR], commit [8d4c298] Summary: ✅
AI ReviewSummaryThis PR fixes the array lexicographic comparison path when a no-supertype element pair contains a bare Final Verdict✅ No remaining review findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 28/31 (90.32%) · Uncovered code |
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 units19 translation units recompiled, 230 s compile time in total, 19 of them have a recent master baseline. |
CI finish ledger - 68101e1CI is fully finished on this head: 175 checks completed, 155 success, 20 skipped (each with a No failures. Nothing to own. Session id: cron:our-pr-ci-monitor:20260811-223000 |
…n test The suite asserted the rejection only where a bare `Nothing` was aligned against a side that carries values (`Nullable(UInt64)`), so the shape where BOTH sides hold a bare `Nothing` in the same position and differ only in the signedness of the carrying member was untested. That shape matters because the element resolver does accept it: for `Tuple(Nothing, UInt64)` vs `Tuple(Nothing, Int64)` the recursive `getReturnTypeImpl` answers `Nullable(UInt8)`, which reads like a decidable comparison. It is not one. `IFunction.cpp:872-877` returns `DataTypeNothing` as soon as any argument type is `Nothing`, so that outer `Nullable(UInt8)` is the null-wrapping of a `Nothing` result rather than a comparison outcome: measured over 9 populated row pairs the column is null 9 times out of 9, while the carrying member compared on its own decides all 9. Admitting the pair on the strength of that return type would therefore produce a comparison that cannot answer for any row. Added rows, and what each one pins (measured against a master build without this fix): - `toTypeName(a = b)` - master analyses it successfully, so this pins the declared type. - `a = b` and `b = a` - master aborts with `Bad cast from type DB::ColumnNothing to DB::ColumnVector<char8_t>` (`executeArrayLexicographic` -> `assert_cast`), so these pin the crash in both operand orders. - `a > b` and `b > a` - already `ILLEGAL_TYPE_OF_ARGUMENT` on master, because only `equals` and `notEquals` admit a `Nullable(UInt8)` element result there. Kept as documentation of the ordering half; they are not new coverage. A `Tuple(String, ...)` pair with the same signedness difference answers `0`/`0` and stays accepted, so the rows above pin the `Nothing` member rather than the mixed signedness.
CI finish ledger - 8d4c298CI is fully finished on this head: 176 checks completed, 155 success, 21 skipped (each with a No failures. Nothing to own. Session id: cron:our-pr-ci-monitor:20260812-233000 |
diegomestre2
left a comment
There was a problem hiding this comment.
It should avoid the issue with Nothing type, looks good!
…nt array comparison) and re-roll 04357
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Comparing two arrays whose element types have no least supertype aborted the server with
Bad cast from type DB::ColumnNothing to DB::ColumnVector<char8_t>when an aligned element positionheld a bare
Nothing, for exampleSELECT CAST([], 'Array(Nullable(Nothing))') > [[1]], or overreal columns of
Array(Tuple(Nothing, UInt64))andArray(Tuple(Array(UInt8), Int64)). The aborthappened during constant folding as well as at execution, so analysis alone could kill the server.
Root cause:
getReturnTypeImpl's array-no-supertype branch declared a plainUInt8, while theelement comparator it later builds declares
Nothingfor a value-less position. Execution thenassert_cast-ed thatColumnNothingtoColumnUInt8. A bareNothingposition is genuinelyundecidable: it holds no values, a
Tuplemember has no null map covering it, and there is no lengthto tie-break on.
The fix makes the declared type honest. A new per-side
containsUndecidableNothingpredicate rejectssuch a pair during analysis with
ILLEGAL_TYPE_OF_ARGUMENT, andcompareGatheredElementskeeps afail-closed guard for defence in depth. Each side is classified independently, because one side's
null map must never decide a position on the other side. The predicate deliberately mirrors
FunctionsNullSafeCmp::containsNothingand does not descend intoArray/Map; deeper positions staycovered because the caller recurses once per array level.
Two
Nothingshapes remain decidable and keep answering, now correctly rather than crashing:Nullable(Nothing)(decided by its own null map) andArray(Nothing)(shares a supertype withArray(T)). Those answers were checked against the same comparison on a pair that does have asupertype, and match exactly in both operand orders.
Introduced by #110245, which is on
masteronly, so no released version is affected and no backportis needed.
Tracked in #113640 (CI signature STID 1499-2747), which also collects the
Equalitysibling atFunctionsComparison.h:1485.Validation
Both directions, one command per binary (pre-fix and fixed builds of the same branch):
SELECT CAST([],'Array(Nullable(Nothing))') = [[1]]Bad cast ... ColumnNothing ...0SELECT CAST([],'Array(Nullable(Nothing))') > [[1]]0SELECT [NULL] = [[1]]Code: 440The values the fixed build returns were compared against the supertype path (
[1]in place of[[1]]) and match exactly:0 1 0 0 1 1for the six operators,0 1 1 1 0 0for an empty alignedprefix,
0 1forisNotDistinctFrom/isDistinctFrom, and0 0in the reversed operand order.Coverage: all eight operators the introducing PR added;
Nothingdirect, underNullable, nested inTuple, and under 1-, 2- and 3-deepArraywrappers;Map(k, Nothing)andArray(Nothing)asmust-still-answer controls; empty and non-empty aligned prefixes. An empty range is rejected too, so
validity never depends on the data.
No existing assertion was weakened: all 47 pre-existing reference lines of
04549_array_comparison_bigger_types_nullableare unchanged. 200/200 runs pass across--test-runs 50 --order randomand--test-runs 50 --no-random-settings, and the feature's owneight-test suite has no failures. Nine mutation arms were run against the new tests; eight are caught,
and the ninth only makes the secondary fail-closed guard unreachable, which no input can pin because
the analysis-time check rejects every such pair first.
Version info
26.8.1.1319(included in26.8and later)