Repository navigation
Fix an explicit NULL storing 0 in an Enum column with input_format_null_as_default - #131190
Conversation
…ull_as_default
With `input_format_null_as_default = 1` (the default), an explicit null for a
non-`Nullable` column is read by the `SerializationNullable::deserializeNullAsDefaultOrNested*`
helpers. Their shared core `deserializeImpl` handled a detected null with
`IColumn::insertDefault()`, the zero of the column, because the helpers only
received the nested serialization, never the type. For `Enum` the type default is
the first element, and 0 may not be a member at all, so
`INSERT INTO t FORMAT JSONEachRow {"e": null}` into an `Enum8('a' = 1, 'b' = 2)`
column stored 0 and every later `SELECT` of the table failed with
`UNKNOWN_ELEMENT_OF_ENUM`. A `Tuple` or `JSON` type holding an `Enum` had the same
problem. `AddingDefaultsTransform` only replaces columns with a `DEFAULT`
expression, so for every other column the zero was final.
The six non-`try` helpers now take the type and `deserializeImpl` inserts
`IDataType::insertDefaultInto` when the type's default is not trivial; for every
type whose default IS the column zero the statement is unchanged (this also keeps
a top-level `Nothing` null accepted, since `Nothing::insertDefaultInto` throws).
All top-level callers pass the type they already have in scope (JSON*, CSV, TSV,
TSKV, CustomSeparated/Template/Regexp/Freeform through the escaping rules,
MySQLDump, Values, GeoJSON). `BSONEachRowRowInputFormat::readField` had the same
`insertDefault()` on a BSON null; it now uses the same guarded call, which also
covers BSON array, tuple and map elements because `readField` recurses with the
element type. The `Nullable` paths pass no type and are unchanged.
Nested elements of text-format `Array`/`Map`/`Tuple` values still insert the
column zero: those serializations hold no element types.
Closes ClickHouse#118515
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 (2 nits, no code change).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20261113-040200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261113-025400 |
|
Workflow [PR], commit [ecb16d9] Summary: ✅
AI ReviewSummaryReviewed #131190 at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No changes requested and no new inline findings posted. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units30 translation units recompiled, 116 s compile time in total, 30 of them have a recent master baseline. |
3a137cc
…l_as_default With input_format_null_as_default = 1, an Avro null (a union(null, X) value or a field of Avro type null) or a MsgPack nil read into a non-Nullable column appended IColumn::insertDefault(), the zero of the column, and reported the value as missing. Only AddingDefaultsTransform consumes that bit, and only for columns with a DEFAULT expression, so for every other column the zero was final. For Enum the zero is outside the domain (or a member that is not the first one): the stored value is wrong and SELECT of a table holding it fails with UNKNOWN_ELEMENT_OF_ENUM. A Tuple or JSON holding an Enum had the same problem. Insert IDataType::insertDefaultInto when the type's default is not trivial, as ValuesBlockInputFormat and the JSONEachRow/BSON missing-field paths already do. The isDefaultInsertTrivial() guard keeps the old statement for every other type; it matters for Nothing, whose insertDefaultInto throws while a Nothing null is accepted today. The missing bit is unchanged, so a DEFAULT expression still takes precedence. Array elements, Map keys/values and record fields go through the same statements because both readers recurse with the element type. The text formats and BSONEachRow get the same rule in ClickHouse#131190. The columnar readers (Native, Parquet, Arrow, ORC) lose the default through a different mechanism and are not changed here. Related: ClickHouse#118515 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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]>
Closes: #118515
Related: #130975
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an explicit
NULLread into a non-Nullablecolumn withinput_format_null_as_defaultstoring the zero of the column instead of the default of its type in the text formats andBSONEachRow. AnEnumwithout a 0 element got 0, after which everySELECTof the table failed withUNKNOWN_ELEMENT_OF_ENUM;INSERT ... VALUES (NULL)already stored the default.Description
On 26.8.2.3,
INSERT INTO t FORMAT JSONEachRow {"e": null}into anEnum8('a' = 1, 'b' = 2)column stores 0 with the defaultinput_format_null_as_default = 1, and thenSELECT * FROM tfails withUNKNOWN_ELEMENT_OF_ENUM. The same happens for\Nin CSV, TSV, TSKV, CustomSeparated, Regexp and MySQLDump, for a BSONnull, and for aTupleorJSONholding anEnum(also throughValues). AnEnumwhose 0 is a member but not the first one silently got the wrong value.Root cause: the
SerializationNullable::deserializeNullAsDefaultOrNested*helpers only receive the nested serialization, so on a null they insertIColumn::insertDefault(), the column zero.AddingDefaultsTransformonly replaces columns that have aDEFAULTexpression, so for all other columns the zero is final.BSONEachRowRowInputFormat::readFieldhad the same call.Change: the helpers take the type and insert
IDataType::insertDefaultIntowhenisDefaultInsertTrivial()is false, keepinginsertDefault()otherwise, so trivial types,Nothingand theNullablepaths are unchanged. Every top-level caller passes the type it already has; BSON uses the same check, which also covers its array, tuple and map elements. A whole-value null forTuple(Nothing, Enum8(...))informat()now fails withNOT_IMPLEMENTED, as it does when that column is missing.Not covered: a null element inside a text-format
Array/Map/Tuplevalue, and the Avro, MsgPack and columnar readers.Validation: the new test fails on master and passes with the fix (50/50 randomized runs, ASan clean); 236 existing format,
null_as_defaultandEnumtests give identical results on both binaries. Over 10M all-null TSV rows, trivial types stay within noise and an all-Enumfile is 4.7% slower single-threaded, since it now writes the real default.Workflow [PR]
Sync PR [sync-upstream/pr/131190]
Version info
26.11.1.2205-master(included in26.11and later)