Repository navigation
Fix VALUES (DEFAULT), the RowBinaryWithDefaults default flag and JSON empty_as_default storing 0 into Enum columns - #131198
Conversation
…thDefaults and JSON The `DEFAULT` keyword in `VALUES`, the per-field default flag of `RowBinaryWithDefaults` / `RowBinaryWithNamesAndTypesAndDefaults` and an empty string read with `input_format_json_empty_as_default` inserted `IColumn::insertDefault`, the zero of the column, instead of `IDataType::insertDefaultInto`. The site reports the value as missing, but that only triggers a column's `DEFAULT` expression, so a column without one keeps the zero. For an `Enum` without a 0 element (and a `Tuple` or typed `JSON` path holding one) that stores a value outside the type: `CHECK TABLE` passes and every `SELECT` fails with `UNKNOWN_ELEMENT_OF_ENUM`. For an `Enum` whose 0 element is not the first one it stores the wrong value. Ask the type for its default at the three sites, as `ValuesBlockInputFormat` already does for `NULL` with `input_format_null_as_default` and the JSONEachRow / TSKV readers do for a missing column. `BinaryFormatReader::readFieldImpl` now receives the type; `JSONUtils::deserializeEmpyStringAsDefaultOrNested` takes an optional type, which only the top-level `readField` passes (nested Array / Map / Tuple elements keep the old path). Types with a trivial default keep `insertDefault`: `IDataTypeDummy` (`Nothing`) reports a trivial default but throws from `insertDefaultInto`, and a top-level `Nothing` is accepted by all three sites in `format()`. `Tuple(Nothing, <Enum>)` now fails with `NOT_IMPLEMENTED`, which is what the JSONEachRow missing-column path already returns for that type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s docs
Extend 05628 with a `JSON(e Enum8('a' = 1, 'b' = 2))` column so the typed
`JSON` path is asserted at all three sites (`VALUES` `DEFAULT`, the
RowBinaryWithDefaults default flag, JSON `""` with
`input_format_json_empty_as_default`): master stores 0 there and the
`SELECT` fails with `UNKNOWN_ELEMENT_OF_ENUM`.
The "Schema evolution" table of the `RowBinaryWithNamesAndTypesAndDefaults`
documentation still described the `0x01` cell marker as `insertDefault`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (2 rounds): 2 agreed and fixed, 5 disagreed with evidence
Round 2 (re-review of the fixes): no findings. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-self-slot-57:20261113-060700 |
|
Workflow [PR], commit [07b9ee5] Summary: ✅
AI ReviewSummaryThe PR makes explicit defaults in Final VerdictStatus: ✅ Approve |
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 units38 translation units recompiled, 137 s compile time in total, 38 of them have a recent master baseline. |
c08eec9
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]>
Related: #130898
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
DEFAULTinINSERT ... VALUES, the per-field default flag ofRowBinaryWithDefaultsandRowBinaryWithNamesAndTypesAndDefaults, and an empty string read withinput_format_json_empty_as_default = 1storing 0 instead of the default of the column type. AnEnumcolumn without a 0 element received a value outside the type, after which the table could not be read (UNKNOWN_ELEMENT_OF_ENUM); anEnumwhose 0 element is not the first one received the wrong value.Description
On 26.8 and 26.3,
INSERT INTO t VALUES (1, DEFAULT)into anEnum8('a' = 1, 'b' = 2)column stores 0:CHECK TABLEpasses and everySELECTof the table then fails withUNKNOWN_ELEMENT_OF_ENUM. TheRowBinaryWithDefaultsdefault flag and""withinput_format_json_empty_as_defaultdo the same, also inside aTupleor a typedJSONpath.These three places call
IColumn::insertDefault, the zero of the column, instead ofIDataType::insertDefaultInto. They report the value as missing, but that only triggers a column'sDEFAULTexpression, so a column without one keeps the zero.The change asks the type for its default in
ValuesBlockInputFormat::tryReadValue,BinaryFormatReader::readFieldImplandJSONUtils::readField/deserializeEmpyStringAsDefaultOrNested, asValuesBlockInputFormatalready does forNULLandJSONEachRowfor a missing column. Types with a trivial default keepinsertDefault, so a top-levelNothinginformat()is still accepted;Tuple(Nothing, <Enum>)informat()now fails withNOT_IMPLEMENTED, asJSONEachRowalready does when such a column is missing.The new test fails on master and passes with the fix, 50 randomized runs, ASan. Cost in a release build: none when values are present; for a defaulted value of a type whose default is the column zero, one extra virtual call (about 1.6 ns), +7.9% on a parse-only
RowBinaryWithDefaultsstream where every field is a default flag of aUInt64column.Not changed here: values missing from the input (#131189, #130259, #130975), empty
CSV/TSVfields (#130975), explicitNULLwithinput_format_null_as_default(#131190), empty strings in nestedArray/Map/Tupleelements, Avroinput_format_avro_allow_missing_fields, andDEFAULTafter a row parsed as an expression.Workflow [PR]
Sync PR [sync-upstream/pr/131198]
Version info
26.11.1.2206-master(included in26.11and later)