Skip to content

Fix VALUES (DEFAULT), the RowBinaryWithDefaults default flag and JSON empty_as_default storing 0 into Enum columns - #131198

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/explicit-default-uses-type-default
Nov 13, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/explicit-default-uses-type-default

Conversation

@groeneai

@groeneai groeneai commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #130898

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 DEFAULT in INSERT ... VALUES, the per-field default flag of RowBinaryWithDefaults and RowBinaryWithNamesAndTypesAndDefaults, and an empty string read with input_format_json_empty_as_default = 1 storing 0 instead of the default of the column type. An Enum column without a 0 element received a value outside the type, after which the table could not be read (UNKNOWN_ELEMENT_OF_ENUM); an Enum whose 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 an Enum8('a' = 1, 'b' = 2) column stores 0: CHECK TABLE passes and every SELECT of the table then fails with UNKNOWN_ELEMENT_OF_ENUM. The RowBinaryWithDefaults default flag and "" with input_format_json_empty_as_default do the same, also inside a Tuple or a typed JSON path.

These three places call IColumn::insertDefault, the zero of the column, instead of IDataType::insertDefaultInto. They report the value as missing, but that only triggers a column's DEFAULT expression, so a column without one keeps the zero.

The change asks the type for its default in ValuesBlockInputFormat::tryReadValue, BinaryFormatReader::readFieldImpl and JSONUtils::readField / deserializeEmpyStringAsDefaultOrNested, as ValuesBlockInputFormat already does for NULL and JSONEachRow for a missing column. Types with a trivial default keep insertDefault, so a top-level Nothing in format() is still accepted; Tuple(Nothing, <Enum>) in format() now fails with NOT_IMPLEMENTED, as JSONEachRow already 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 RowBinaryWithDefaults stream where every field is a default flag of a UInt64 column.

Not changed here: values missing from the input (#131189, #130259, #130975), empty CSV/TSV fields (#130975), explicit NULL with input_format_null_as_default (#131190), empty strings in nested Array/Map/Tuple elements, Avro input_format_avro_allow_missing_fields, and DEFAULT after a row parsed as an expression.


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

Version info

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

groeneai and others added 2 commits November 13, 2026 02:48
…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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (2 rounds): 2 agreed and fixed, 5 disagreed with evidence
Finding Verdict
⚠️ The test did not cover a typed JSON path Agreed, fixed: the fixture has a JSON(e Enum8) column through all three sites
💡 RowBinaryWithNamesAndTypesAndDefaults docs said insertDefault for marker 0x01 Agreed, fixed
❌ Absent columns in RowBinaryWithNamesAndTypesAndDefaults still get 0 Disagree: a missing-value path (ColumnMapping::insertDefaultsForNotSeenColumns), changed by #130975
❌ CSV/TSV empty fields still get 0 Disagree: other formats, changed by #130975
💡 Extra virtual call per defaulted value Disagree: about 1.6 ns, none when values are present; stated in the description

Round 2 (re-review of the fixes): no findings.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: INSERT INTO t VALUES (1, DEFAULT) into Enum8('a' = 1, 'b' = 2), then SELECT * fails with UNKNOWN_ELEMENT_OF_ENUM on every run (master, 26.8.18.2, 26.3.42.3); same for the RowBinaryWithDefaults flag and JSON "" with input_format_json_empty_as_default = 1.
b Root cause explained? The three sites insert IColumn::insertDefault (the column zero) instead of IDataType::insertDefaultInto; read = false only triggers a DEFAULT expression, so a column without one keeps the zero.
c Fix matches root cause? Yes: the three sites ask the type for its default; types with a trivial default keep insertDefault.
d Test intent preserved / new tests added? New test covers all three sites, Tuple(Enum), a typed JSON(e Enum8) path, a DEFAULT expression column, JSONStringsEachRow, RowBinaryWithNamesAndTypesAndDefaults and a top-level Nothing; existing tests unchanged.
e Both directions demonstrated? Fails on master (Code: 691), passes with the fix; 50 randomized runs green; ASan+UBSan clean.
f Fix is general across code paths? All three explicit-default sites; missing-column paths are already correct or handled elsewhere. Nested JSON elements, NULL with input_format_null_as_default (#131190), JSONColumns expression defaults (#127113) and Avro missing fields are listed as not changed.
g Fix generalizes across inputs (params/datatypes/wrappers)? Enum without 0, Enum with a non-first 0, Tuple(Enum), JSON(e Enum8), Nullable/Array/Map/String (unchanged), top-level Nothing (accepted), sparse-serialized destination columns (checked).
h Backward compatible? (maintainer-approved exception only) Yes: no setting or format change, existing parts untouched; only Tuple(Nothing, Enum) in format() now fails with NOT_IMPLEMENTED, as JSONEachRow already does for a missing column of that type.
i Invariants and contracts preserved? Every branch inserts exactly one row (DataTypeTuple via addElementSafe, DataTypeObject rolls back partial inserts), so the Values rollback and equal column sizes hold; the read flag is unchanged.

Session id: cron:clickhouse-impl-self-slot-57:20261113-060700

@clickhouse-gh

clickhouse-gh Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [07b9ee5]

Summary: ✅


AI Review

Summary

The PR makes explicit defaults in Values, the default markers in RowBinaryWithDefaults and RowBinaryWithNamesAndTypesAndDefaults, and top-level JSON empty-string defaults use the data type's default, preventing invalid or incorrect Enum values. The changed paths preserve missing-value reporting and exception rollback. No actionable findings remain after reviewing the current diff, callers, type implementations, and prior discussion. The explicitly deferred nested-element and expression-parser behavior is accepted as the scope of this partial fix. The new 05628_explicit_default_uses_type_default regression test passed in Fast test at the reviewed head.

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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Nov 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 07b9ee5da with master b271a9290 (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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.63, programs/clickhouse-keeper ×0.67 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.7 s 9.1 s +5.5 s (+153%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.3 s +5.2 s (+253%)
Compile time of recompiled translation units

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

Job report

@PedroTadim PedroTadim self-assigned this Nov 13, 2026
@PedroTadim
PedroTadim added this pull request to the merge queue Nov 13, 2026
Merged via the queue into ClickHouse:master with commit c08eec9 Nov 13, 2026
604 of 608 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 13, 2026
PedroTadim pushed a commit to raimannma/ClickHouse that referenced this pull request Nov 17, 2026
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]>
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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). 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.

3 participants