Skip to content

Fix named Tuple CAST filling new elements with the zero of the column instead of the type default - #131124

Closed
Marouane-elfadli wants to merge 2 commits into
ClickHouse:masterfrom
Marouane-elfadli:fix-named-tuple-cast-enum-default
Closed

Marouane-elfadli wants to merge 2 commits into
ClickHouse:masterfrom
Marouane-elfadli:fix-named-tuple-cast-enum-default

Conversation

@Marouane-elfadli

Copy link
Copy Markdown
Contributor

Closes: #130899
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):

Fix CAST, INSERT ... SELECT and ALTER TABLE ... MODIFY COLUMN that add an element to a named Tuple (including Array(Tuple(...))): the new element of existing rows was filled with the zero of the underlying column instead of the element type's default value. For an Enum element this stored a value that is not a member of the Enum, so the table could no longer be read as text (UNKNOWN_ELEMENT_OF_ENUM) while CHECK TABLE reported no problem.

Details

Problem. When casting between named tuples, elements are matched by name and "target elements without a counterpart are filled by default values". In FunctionCast::createTupleWrapper this fill was done with:

to_element_types[i]->createColumn()->cloneResized(input_rows_count);

cloneResized on an empty column appends the raw zero of the column. The real default of a type is only inserted by IDataType::insertDefaultInto (overridden by DataTypeEnum, DataTypeTuple, DataTypeObject). For Enum8('x' = 1, 'y' = 2) the default is 'x', but the fill produced 0, which is not a member. The ALTER ... MODIFY COLUMN mutation, the INSERT ... SELECT conversion and CAST all go through this wrapper, so all of them were affected.

Solution. Fill the missing elements through the data type:

auto default_column = to_element_types[i]->createColumn();
to_element_types[i]->insertManyDefaultsInto(*default_column, input_rows_count);
converted_columns[i] = std::move(default_column);

insertManyDefaultsInto calls insertDefaultInto for each row, so:

  • Enum gets its default member (the one with the lowest value, as documented), including when 0 is a member that is not the default.
  • A nested Tuple containing an Enum gets correct defaults recursively.
  • Every other type behaves as before: it is equivalent to the old cloneResized (insertDefault), so Nullable stays NULL, Array stays [], LowCardinality, DateTime64 etc. are unchanged.

I preferred this over createColumnConstWithDefaultValue(...)->convertToFullColumnIfConst() because it needs

FunctionCast::createTupleWrapper filled target elements without a counterpart
with createColumn()->cloneResized(), which appends the raw zero of the column.
Use insertManyDefaultsInto so that Enum gets its first member and nested
Tuples are handled too.

Fixes ClickHouse#130899
Covers CAST, ALTER MODIFY COLUMN (+ OPTIMIZE FINAL), INSERT SELECT,
Array(Tuple), an Enum whose member 0 is not the default, and a nested
Tuple containing an Enum.

Refs ClickHouse#130899
@Marouane-elfadli

Copy link
Copy Markdown
Contributor Author

@PedroTadim @groeneai sorry for jumping in on this one, I had already found the cause and a fix when I saw it was assigned. I opened this PR.

Cause: in FunctionCast::createTupleWrapper, target elements without a counterpart were filled with createColumn()->cloneResized(n), which appends the raw zero of the column. For an Enum that is not a valid member.

Fix: use insertManyDefaultsInto instead, which goes through the type's insertDefaultInto, so an Enum gets its first member (and nested Tuples are handled too). Other types behave exactly as before.

I added a stateless test with the cases from the issue. @groeneai, if you already have a fix in progress, happy to compare or step aside. Thanks @hanke580 for the detailed repro.

@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Nov 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

No fix in progress on my side, so I'm not opening a competing PR. Thanks for picking it up.

One case from #130899 is not in the test: SET allow_metadata_only_named_tuple_alter = 1 before the ALTER. There the ALTER rewrites nothing (the part keeps data_version 1), and on master toInt8(attrs.status) still reads 0, also after OPTIMIZE TABLE t FINAL. A case with that setting would show that reads and merges of old parts get the default too.

@Marouane-elfadli

Copy link
Copy Markdown
Contributor Author

@groeneai i close this PR I WOULD LOVE THAT YOU OPEN ONE AND I INSPIRE FROM YOU

@groeneai

Copy link
Copy Markdown
Collaborator

Thanks, I'll open it. A correction to my earlier comment: my #130259 already changes this line (added elements get the type default), but inside a larger rework of the format readers. So I'll send the #130899 fix as its own small PR, with a test for every path in the issue including allow_metadata_only_named_tuple_alter = 1, and link it here.

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Nov 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

Opened #131186: the same createTupleWrapper fill, with a test for every path in #130899, including allow_metadata_only_named_tuple_alter = 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization

Projects

None yet

2 participants