Skip to content

Fix CAST of a Nullable(Tuple) converting the elements of its NULL rows - #132672

Merged
nihalzp merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-cast-nullable-tuple-null-rows
Nov 24, 2026
Merged

nihalzp merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-cast-nullable-tuple-null-rows

Conversation

@groeneai

@groeneai groeneai commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #132577
Related: #120674
Related: #132576

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed CAST, accurateCastOrNull and accurateCastOrDefault of a Nullable(Tuple) value with NULL rows (also inside a Tuple or an Array), which converted the placeholders stored under the NULL rows and threw (for example Cannot parse Int32 from String) instead of returning NULL or the default for those rows. This also fixes INSERT SELECT into such a column and ALTER 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 a Nullable(Tuple(s String)) column holding a NULL row throws Code: 32 Cannot parse Int32 from String instead of returning NULL for that row.

A NULL row of a Nullable(Tuple) holds placeholders in its elements ('', a NULL, 0). prepareRemoveNullable converted 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-NULL check of a Nullable element throws 349, an Enum placeholder 0 throws 691. accurateCast already converts only the non-NULL rows and expands the result (#120674); this PR does that for every cast type when the source is a Nullable(Tuple) whose type changes, and for a Nullable(Tuple) element that accurateCastOrNull converts to a non-Nullable type. convertNotNullRows and its two calls are taken unchanged from #132576; the PRs differ only in the exclude_source_nulls predicate.

Cost, only on blocks with a NULL row: +1.2 ns/row for Tuple(Int32) to Tuple(Int64), +4-5 ns/row when a String element is carried (accurateCast on 26.10.5.57). A NULL row now holds the column zero, like a stored NULL row: for an Enum element without a 0 member, toString or concat over the result throws 691 on that row, as over a stored one (#120052); the Enum conversion 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 NULL row. 94 existing cast tests pass unchanged.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/132672]

Version info

  • Merged into: 26.12.1.383-master (included in 26.12 and later)

groeneai and others added 4 commits November 18, 2026 11:11
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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
❌ A Nullable(Tuple) element of a Tuple that accurateCastOrNull/accurateCastOrDefault converts to a non-Nullable type still converted its NULL-row placeholders (691 for an Enum element to String). Agreed, fixed: that branch now skips the NULL rows with the same block; two test lines added.
⚠️ The accurateCastOrNull case of the test built its source with a plain CAST, so it did not prove the accurateCastOrNull path on its own. Agreed, fixed: added a direct mixed-row accurateCastOrNull of a Tuple(Enum8) with a NULL row to Nullable(String) (throws 691 on master) and one with a parse failure next to a NULL row.
⚠️ NULL rows of the result now hold the column zero instead of converted placeholders; for an Enum without a 0 member toString/concat over the result throw 691 for such a row. Agreed, stated in the description: the same already happens for a stored NULL row (#120052).
⚠️ With a Map or LowCardinality(String) element of a Nullable(Tuple) target, accurateCastOrNull of a Tuple holding a Nullable(Tuple) still fails (ILLEGAL_COLUMN / logical error). Not changed here: measured the same on master and with this PR (the Map case also without any NULL row), so it is a separate defect of that branch; the LowCardinality case is the one #132577 fixes.
💡 The NULL-row filter helper duplicated the one #132576 adds. Agreed, fixed: the helper and its calls are taken unchanged from #132576.
💡 Assert the payload of a NULL row with assumeNotNull. Not done: assumeNotNull returns an arbitrary value for NULL.
💡 An element converted by identity is copied once more in the filter/expand block (same block as accurateCast). Noted; the cost is measured in the description.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CAST(x, 'Nullable(Tuple(s Int32))') over a Nullable(Tuple(s String)) column with a NULL row throws Code 32 on every run of a master debug build (also on 26.10.5.57). The new test fails on master at its first query.
b Root cause explained? prepareRemoveNullable converted the nested tuple of every row, NULL rows included, and the element conversions do not all read the null map: parsing adaptors parse the '' placeholder, the not-NULL checks see the element NULL under a NULL tuple row, an Enum 0 placeholder is serialized.
c Fix matches root cause? Yes. The existing filter-convert-expand of accurateCast (#120674) now also runs for every cast type when the source is a Nullable(Tuple) whose type changes, and for a Nullable(Tuple) element that accurateCastOrNull converts to a non-Nullable type, so no element conversion sees a NULL row. No guard at the throw sites.
d Test intent preserved / new tests added? New test 05835_cast_nullable_tuple_null_rows: String to Int32/Decimal/Date, Nullable and LowCardinality(Nullable) elements, nesting in Tuple/Array, Tuple(Enum8) to String, accurateCastOrNull (incl. a parse failure next to a NULL row), accurateCastOrNull/OrDefault of a Tuple holding a Nullable(Tuple), INSERT SELECT, MergeTree ALTER ... MODIFY COLUMN, and two cases that must still throw.
e Both directions demonstrated? Master: every case throws (32, 349, 38, 691, the ALTER 341) except three guards that behave the same on both. Fix: matches the reference 5/5, and 50/50 randomized runs.
f Fix is general across code paths? One predicate covers every caller of the cast (CAST, ::, _CAST in INSERT, ALTER and supertype conversions, accurateCastOrNull/OrDefault) and every depth: a Nullable(Tuple) inside a Tuple, Array or Map goes through the same wrapper.
g Fix generalizes across inputs (params/datatypes/wrappers)? String, Nullable(String), LowCardinality(Nullable(String)), Enum8 elements; constant and non-constant; all-NULL and no-NULL blocks (max_block_size = 1); cast_keep_nullable; Map values.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; the exceptions become NULL rows. The nested values of NULL rows are now the column default, as with accurateCast.
i Invariants and contracts preserved? The result null map is the source map; filter/expand keep the row count; blocks without NULL rows and identity casts take the unchanged path. 94 existing cast and Nullable(Tuple) tests pass unchanged.

Session id: cron:clickhouse-impl-self-slot-57:20261118-104803

@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [671aa5b]

Summary: ✅


AI Review

Summary

The PR excludes NULL rows of Nullable(Tuple) values before converting their elements, preserving NULL propagation across casts, nested containers, INSERT SELECT, and column mutations. Review of the current head, surrounding conversion paths, regression coverage, and all prior discussion found no actionable issues. The focused regression test 05835_cast_nullable_tuple_null_rows passed in Fast test.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Nov 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 671aa5b8f with master f3e79c38e (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 70 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim

Copy link
Copy Markdown
Member

@nihalzp can you help reviewing this?

@nihalzp nihalzp self-assigned this Nov 23, 2026
@nihalzp
nihalzp added this pull request to the merge queue Nov 24, 2026
Merged via the queue into ClickHouse:master with commit 79b3da9 Nov 24, 2026
384 of 386 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 24, 2026
alexey-milovidov added a commit that referenced this pull request Nov 25, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants