Repository navigation
Fix CAST of a Nullable(Tuple) converting the elements of its NULL rows - #132672
Conversation
A NULL row of a Nullable(Tuple) stores placeholders in its elements ('' for
a String, a NULL in a Nullable element, 0 for an Enum). prepareRemoveNullable
ran the Tuple conversion over the nested column of every row, and the
element conversions do not all read the null map they are handed: the
function adaptors parse the placeholder '' (Code 32/38), the not-NULL
precheck of a Nullable element and the LowCardinality containsNull() check
see the element NULL under a NULL tuple row (Code 349), and an Enum
placeholder 0 serialized to String throws 691. So CAST, INSERT SELECT and
ALTER TABLE ... MODIFY COLUMN from a Nullable(Tuple) with NULL rows threw
instead of returning NULL for those rows; the ALTER left a stuck mutation
and an unreadable column.
accurateCast already filters the NULL rows out, converts, and expands the
result (exclude_source_nulls, ClickHouse#120674). Enable it for every cast type when
the source is a Nullable(Tuple) whose nested type changes. The filter costs
+1.2 ns/row per Int element and +4-5 ns/row per String element on blocks
that contain a NULL row (release 26.10.5.57, measured through accurateCast);
blocks without NULLs are unchanged. The nested values of NULL rows in the
result are now the column default, as with accurateCast.
Related: ClickHouse#132577
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The existing accurateCastOrNull line takes its source from a plain CAST whose all-NULL fast path already fixes the payload, so it did not pin the NULL-row exclusion for accurateCastOrNull. The new case converts a mixed Nullable(Tuple(Enum8)) to Nullable(String) directly (Code 691 on master, NULL for the NULL row with the fix). The second line keeps a parse failure next to a NULL row returning NULL for both, which behaves the same with and without the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…L cast branch The report-failures-as-NULL branch of `prepareRemoveNullable` (a `Nullable` source converted to a non-`Nullable` element of a `Tuple` under `accurateCastOrNull` / `accurateCastOrDefault`) ran the wrapper over every row, so the placeholders stored under the NULL rows of a `Nullable(Tuple)` were converted too, e.g. an `Enum8` `0` placeholder serialized to `String` threw `Unexpected value 0 in enum` although `wrapInNullable` would have made that row NULL anyway. That branch now also filters the NULL rows out, converts and expands, via the block the nullable-result branch already had, moved into the file-local `convertRowsWithoutNulls`. The predicate is shared by both branches. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Take the NULL-row filter helper `convertNotNullRows`, the position of the `exclude_source_nulls` predicate and both call sites unchanged from ClickHouse#132576, so the two PRs share one helper and differ only in the predicate. The helper now returns the expanded column; the nullable-result branch wraps it with the source null map, and the report-failures-as-NULL branch hands it to the existing `wrapInNullable` tail, which merges the same map. No behaviour change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (click to expand)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261118-104803 |
|
Workflow [PR], commit [671aa5b] Summary: ✅
AI ReviewSummaryThe PR excludes Final VerdictStatus: ✅ Approve |
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, 70 s compile time in total, 7 of them have a recent master baseline. |
|
@nihalzp can you help reviewing this? |
79b3da9
…t` after merging `master` #132672 changed `CAST` to `Nullable(Tuple)`: a `NULL` row now holds the column defaults instead of the converted placeholders. The file the test writes with `CAST(... AS Nullable(Tuple(s Nullable(String), u String)))` therefore has a `NULL` in the field `s` of the null struct row, where it had an empty string, and the columns read it as it is written: `(NULL,'')` and `NULL`. These are the own nulls of the field `s`, not the null map of the struct, which still does not reach the subcolumns (the next file of the test, with a non-nullable `s`, covers that). Co-Authored-By: Claude Fable 5.2 <[email protected]>
Related: #132577
Related: #120674
Related: #132576
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
CAST,accurateCastOrNullandaccurateCastOrDefaultof aNullable(Tuple)value withNULLrows (also inside aTupleor anArray), which converted the placeholders stored under theNULLrows and threw (for exampleCannot parse Int32 from String) instead of returningNULLor the default for those rows. This also fixesINSERT SELECTinto such a column andALTER TABLE ... MODIFY COLUMN, whose mutation got stuck and left the column unreadable.Description
On 26.10.5.57,
CAST(x, 'Nullable(Tuple(s Int32))')over aNullable(Tuple(s String))column holding aNULLrow throwsCode: 32 Cannot parse Int32 from Stringinstead of returningNULLfor that row.A
NULLrow of aNullable(Tuple)holds placeholders in its elements ('', aNULL,0).prepareRemoveNullableconverted the nested tuple of every row, and the element conversions do not all read the null map: parsing adaptors parse''(Code 32/38), the not-NULLcheck of aNullableelement throws 349, anEnumplaceholder0throws 691.accurateCastalready converts only the non-NULLrows and expands the result (#120674); this PR does that for every cast type when the source is aNullable(Tuple)whose type changes, and for aNullable(Tuple)element thataccurateCastOrNullconverts to a non-Nullabletype.convertNotNullRowsand its two calls are taken unchanged from #132576; the PRs differ only in theexclude_source_nullspredicate.Cost, only on blocks with a
NULLrow: +1.2 ns/row forTuple(Int32)toTuple(Int64), +4-5 ns/row when aStringelement is carried (accurateCaston 26.10.5.57). ANULLrow now holds the column zero, like a storedNULLrow: for anEnumelement without a 0 member,toStringorconcatover the result throws 691 on that row, as over a stored one (#120052); theEnumconversion used to write its first member.Every new test case throws on master and passes with the fix (50 randomized runs), except three same-on-both guards: two inputs that must still throw and a parse failure next to a
NULLrow. 94 existing cast tests pass unchanged.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132672]
Version info
26.12.1.383-master(included in26.12and later)