Skip to content

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 #132007

Closed

Description

@PedroTadim

🕵️

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

Activity

  1. added
    potential bug
    on Nov 16, 2026
  2. added
    comp-data-types
    on Nov 16, 2026
  3. PedroTadim commented on Nov 16, 2026

    @PedroTadim
    MemberAuthor

    @groeneai I think you should fix this, if there is no fix already?

  4. groeneai commented on Nov 16, 2026

    @groeneai
    Collaborator

    Acknowledged. No fix exists yet: #131422 covers only the top-level Values path and leaves a nested element at 0. I'm investigating the nested Array/Tuple/Map case; I'll follow up here with a PR (or my findings) shortly.

  5. added
    bug
    and removed
    potential bug
    on Nov 16, 2026
  6. groeneai commented on Nov 16, 2026

    @groeneai
    Collaborator

    The fix is in #132086. Every nested Array/Tuple/Map text reader now passes the element type to the null-as-default helpers, so a NULL element gets the Enum default ('a' in your repro) instead of 0. That covers all 21 nested call sites, including the try variants used by Variant parsing and the "" path of input_format_json_empty_as_default. It reuses the element-type mechanism of #131190, with no second default mechanism.

  7. added a commit that references this issue on Nov 17, 2026
    6b6d35c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcomp-data-types

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions