Skip to content

Fix a NULL element of an Array, Tuple or Map of Enum stored as 0 with input_format_null_as_default - #132086

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-nested-enum-element-null-as-default
Nov 17, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-nested-enum-element-null-as-default

Conversation

@groeneai

@groeneai groeneai commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #132007
Related: #131190
Related: #131189

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):

Fix a NULL element of an Array, Tuple or Map (key or value) of a non-Nullable Enum being stored as 0 instead of the default of the element type when read with input_format_null_as_default from text formats (Values, JSONEachRow, CSV, TSV and others) or by a CAST from String. For an Enum without a 0 element, every later SELECT of the column failed with UNKNOWN_ELEMENT_OF_ENUM. The same is fixed for an empty string element with input_format_json_empty_as_default.

Description

On 26.10.2.103 (also reported on 24.8 LTS and 26.8), INSERT INTO t VALUES ([NULL, 'b']) into a Array(Enum8('a' = 1, 'b' = 2)) stores [0, 2] with the default input_format_null_as_default = 1, and SELECT a FROM t then fails with UNKNOWN_ELEMENT_OF_ENUM. The same happens for a Tuple element, a Map key or value, in JSONEachRow, JSONCompactEachRow, CSV, TSV, CAST('[NULL]' AS Array(Enum8(...))) and Variant text parsing.

Root cause: since #131190 the SerializationNullable::deserializeNullAsDefaultOrNested* helpers insert the default of the type they are given, and every top-level reader passes it. The element readers of SerializationArray, SerializationMap and SerializationTuple passed no type, so a nested null still got the column zero: Array and Map held no element type, and the try variants of the helpers had no type parameter.

Change: one fix for the whole class, through the mechanism the top-level readers already use (the element type's insertDefaultInto), so no second default mechanism is added to ISerialization. SerializationArray gets its element type and SerializationMap its key and value types, as #131189 did for SerializationTuple (not part of the pool key: the element serialization key already determines the default). The try helpers get the same nested_type parameter, and all 21 nested call sites pass the element type, including the "" path of input_format_json_empty_as_default. Only types with a non-trivial default change: Enum, and a Tuple or JSON holding one.

Not covered: a Values row that falls back to template expressions ([NULL, 2]) and a nested null read from Parquet are rejected (Code: 349 / Code: 691) rather than stored as 0, before and after this change.

Validation: the new test fails on master (24 rows store 0, the MergeTree table is unreadable) and passes with the fix (50/50 randomized runs). 1426 related existing tests: no new failures.


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

Version info

  • Merged into: 26.11.1.2590-master (included in 26.11 and later)

With `input_format_null_as_default` (the default) a NULL element of an
`Array`, a `Tuple` or a `Map` key or value whose type is a non-Nullable
`Enum` was stored as the column zero. 0 is not a member of e.g.
`Enum8('a' = 1, 'b' = 2)`, so the part was written and every later read of
the column failed with UNKNOWN_ELEMENT_OF_ENUM. The same happened for an
empty string element with `input_format_json_empty_as_default`.

Since ClickHouse#131190 / ClickHouse#131198 the `SerializationNullable::deserializeNullAsDefaultOrNested*`
and `JSONUtils::deserializeEmpyStringAsDefaultOrNested` helpers insert
`IDataType::insertDefaultInto` when they are given the type, and every
top-level reader passes it. The element readers of the composite
serializations did not: `SerializationArray` and `SerializationMap` held no
element type, `SerializationTuple` held `elem_types` (ClickHouse#131189) without
passing them, and the `tryDeserializeNullAsDefaultOrNested*` helpers had no
type parameter at all (used by every `tryDeserialize*` of a composite, e.g.
`Variant` text parsing).

`SerializationArray` now holds its element type and `SerializationMap` its
key and value types (mandatory in `create`, so a missed caller does not
compile); `SerializationArray::SubcolumnCreator` passes `prev_type`. The six
`try` helpers take the same trailing `nested_type` as the throwing ones, and
all 21 nested call sites (text, JSON, CSV, CSV split tuple columns, and the
JSON "" path) pass the element type.

The types are deliberately not part of the pool keys: hashing an Enum type
per serialization request was measured 51.9x slower on
`system.metric_log`'s `Map(Enum16(...), Int64)` in ClickHouse#131189. A pooled
serialization keeps the first creator's type, which is correct because
equal element serialization keys imply equal defaults: only `Enum`
(whose key hashes every value), `Tuple` (recursive key) and `JSON`
(never pooled) have a non-trivial default.

Closes ClickHouse#132007

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (3 nits, none blocking).

# Sev Finding Verdict Evidence / action
1 💡 The three try helpers with no caller (TextEscaped, TextRaw, WholeText) also get nested_type DISAGREE Kept so every try helper matches its non-try twin; the missing type on the try helpers is part of this bug
2 💡 A pooled Array/Map serialization keeps the first creator's type; a creator passing no type could share the key DISAGREE The only untyped subcolumn walk (TypoCorrection.cpp) wraps SerializationNamed element serializations, so its keys differ; no type falls back to the old insertDefault(), never UB
3 💡 Only the Array try path has a test row (the Variant one); the Map/Tuple try sites do not AGREE, not blocking Same one-argument forward as the Array site, which is proven live (removing only that forwarding brings the Variant row back to 0)

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261116-171400

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @antonio2368, could you review this? A NULL (or JSON empty string) element of an Array, Tuple or Map read with input_format_null_as_default got the column zero instead of the element type default, which stored an out-of-domain Enum value; SerializationArray and SerializationMap now hold their element types and all nested call sites pass them to the null-as-default helpers, pool keys unchanged.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. INSERT INTO t VALUES ([NULL, 'b']) into a Array(Enum8('a' = 1, 'b' = 2)) stores [0,2] and SELECT a fails with 691 on master, every run; the new test fails the same way on the master binary.
b Root cause explained? The nested element readers of SerializationArray, SerializationMap and SerializationTuple call the null-as-default and empty-as-default helpers without the element type, so a null element gets IColumn::insertDefault() (the column zero) instead of IDataType::insertDefaultInto. Array/Map held no element type, and the try helpers had no type parameter.
c Fix matches root cause? Yes. Every nested call site now passes the element type, the same mechanism the top-level readers use since #131190. No guard, no new mechanism, pool keys unchanged.
d Test intent preserved / new tests added? New test 05813_null_as_default_nested_enum_element: Array/Tuple/Map (key and value) in JSONEachRow, JSONCompactEachRow, Values (expressions on and off), TSV, CSV (nested CSV, split tuple columns), CAST from String (also into a Variant, the try path), nested containers, Enum16, the "" path, 3 controls and a MergeTree round trip.
e Both directions demonstrated? Master binary: 24 rows store 0 and the table read fails with 691. Fix binary: passes, 50/50 randomized runs. Removing only the try helpers' forwarding brings the Variant row back to 0.
f Fix is general across code paths? All 21 nested sites (throwing and try, text/JSON/CSV/"") in the three composite serializations. The other text readers were already typed. Measured siblings: the Values template-expression path and Parquet reject a nested null (349 / 691) instead of storing 0, unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Array, Tuple, Map key and value, Array(Array), Array(Tuple), Map(String, Tuple), Array(JSON), Enum8 and Enum16 with a negative first member. Nullable elements keep NULL, trivial element types keep 0, input_format_null_as_default = 0 still rejects.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, no format change, no pool-key change. Only the null/"" branch of an element type with a non-trivial default changes, from an invalid value to the type default.
i Invariants and contracts preserved? create takes the type as a mandatory argument, so a missed caller is a compile error. A pooled Array/Map serialization keeps the first creator's type, which is correct because equal element serialization keys imply equal defaults (Enum hashes all values, Tuple is recursive, JSON is not pooled). The type is read only on the null/"" branch; one element is inserted per value either way.

Session id: cron:clickhouse-impl-slot-40:20261116-160604

@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6b6d35c]

Summary: ✅


AI Review

Summary

Reviewed #132086 at 6b6d35c29148dbb1a7be420cc86a9e1d20f9fae7. The change supplies element types to the nested Array, Tuple, and Map text readers so NULL and JSON empty-string defaults produce valid Enum values. No actionable findings. The new regression test, 05813_null_as_default_nested_enum_element, passed in the PR fast-test job; tests were not rerun locally.

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-data-types Type system & core datatypes used across query planning/execution. labels Nov 16, 2026

@clickhouse-gh clickhouse-gh Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review approves this commit, see the AI Review comment.

@clickhouse-gh

clickhouse-gh Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6b6d35c29 with master 4553bee2f (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

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

Job report

@PedroTadim PedroTadim self-assigned this Nov 17, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Nov 17, 2026
Merged via the queue into ClickHouse:master with commit 4076a2c Nov 17, 2026
381 of 383 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 17, 2026
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-data-types Type system & core datatypes used across query planning/execution. 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.

A NULL element of an Array, Tuple or Map of a non-Nullable Enum is stored as 0, an invalid value, with input_format_null_as_default

3 participants