Repository navigation
Description
🕵️
Describe what's wrong
With input_format_null_as_default = 1 (the default), a NULL element inside an Array, Tuple or Map value of a non-Nullable Enum is stored as 0. 0 is not a value of the Enum, so the stored value is invalid: reading the column afterwards fails with UNKNOWN_ELEMENT_OF_ENUM. Once such a row is inserted into a table, SELECT of that column fails for that table.
A top-level NULL gets the Enum default instead (the first value, since #131190), and for other element types, such as Array(UInt8), the 0 is a valid default. Only a nested Enum element ends up with an invalid value.
It happens in every text format I tried (Values, JSONEachRow, CSV, TSV), with input_format_values_interpret_expressions on and off. An explicit CAST([NULL, 'b'], 'Array(Enum8(...))') is rejected with Cannot convert NULL value to non-Nullable type.
Does it reproduce on the most recent release?
Yes: master 26.11.1.2418, 26.8, and 24.8.14.39 LTS.
How to reproduce
CREATE TABLE t (a Array(Enum8('a' = 1, 'b' = 2))) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t VALUES ([NULL, 'b']);
SELECT CAST(a, 'Array(Int8)') FROM t; -- [0,2]
SELECT a FROM t; -- Code: 691. Unexpected value 0 in enum. (UNKNOWN_ELEMENT_OF_ENUM)The same value comes from other formats and container types:
SELECT CAST(a, 'Array(Int8)') FROM format(JSONEachRow, $$a Array(Enum8('a' = 1, 'b' = 2))$$, '{"a":[null,"b"]}'); -- [0,2]
SELECT CAST(a, 'Array(Int8)') FROM format(CSV, $$a Array(Enum8('a' = 1, 'b' = 2))$$, $$"[NULL,'b']"$$); -- [0,2]
SELECT CAST(t, 'Tuple(Int8)') FROM format(Values, $$t Tuple(e Enum8('a' = 1, 'b' = 2))$$, '((NULL))'); -- (0)
SELECT CAST(m, 'Map(String, Int8)') FROM format(Values, $$m Map(String, Enum8('a' = 1, 'b' = 2))$$, $${'k': NULL}$$); -- {'k':0}Expected behavior
A NULL element of a non-Nullable Enum gets the Enum default, as a top-level NULL does, or is rejected. An Enum column should never hold a value outside the Enum.
Error message and/or stacktrace
Code: 691. DB::Exception: Unexpected value 0 in enum. (UNKNOWN_ELEMENT_OF_ENUM)
Related issues and pull requests
Related: #131190
Related: #131422
Additional context
#131190 made a top-level NULL take the Enum default by passing the column type to the null-as-default helper, and lists the Tuple element case as not covered. SerializationArray and SerializationTuple (and Map, through them) still call the SerializationNullable::deserializeNullAsDefaultOrNested* helpers without the element type, so the element gets the column's raw default, which for Enum is 0.
I could not find the change that introduced it: it already reproduces on 24.8.14.39 LTS.
Version info
- Resolved by: Fix a
NULLelement of anArray,TupleorMapofEnumstored as 0 withinput_format_null_as_default#132086 - Merged into:
26.11.1.2590-master(included in26.11and later)
Activity
PedroTadim commentedon Nov 16, 2026
@groeneai I think you should fix this, if there is no fix already?
groeneai commentedon Nov 16, 2026
Acknowledged. No fix exists yet: #131422 covers only the top-level
Valuespath and leaves a nested element at 0. I'm investigating the nestedArray/Tuple/Mapcase; I'll follow up here with a PR (or my findings) shortly.groeneai commentedon Nov 16, 2026
The fix is in #132086. Every nested
Array/Tuple/Maptext reader now passes the element type to the null-as-default helpers, so aNULLelement gets theEnumdefault ('a'in your repro) instead of 0. That covers all 21 nested call sites, including thetryvariants used byVariantparsing and the""path ofinput_format_json_empty_as_default. It reuses the element-type mechanism of #131190, with no second default mechanism.Use the element type default for a NULL element of Array, Tuple and Map