Repository navigation
Fix named Tuple CAST filling new elements with the zero of the column instead of the type default - #131124
Fix named Tuple CAST filling new elements with the zero of the column instead of the type default#131124Marouane-elfadli wants to merge 2 commits into
Conversation
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
|
@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 Fix: use 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. |
|
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: |
|
@groeneai i close this PR I WOULD LOVE THAT YOU OPEN ONE AND I INSPIRE FROM YOU |
|
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 |
Closes: #130899
Related: #130898
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
CAST,INSERT ... SELECTandALTER TABLE ... MODIFY COLUMNthat add an element to a namedTuple(includingArray(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 anEnumelement this stored a value that is not a member of theEnum, so the table could no longer be read as text (UNKNOWN_ELEMENT_OF_ENUM) whileCHECK TABLEreported 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::createTupleWrapperthis fill was done with:to_element_types[i]->createColumn()->cloneResized(input_rows_count);cloneResizedon an empty column appends the raw zero of the column. The real default of a type is only inserted byIDataType::insertDefaultInto(overridden byDataTypeEnum,DataTypeTuple,DataTypeObject). ForEnum8('x' = 1, 'y' = 2)the default is'x', but the fill produced0, which is not a member. TheALTER ... MODIFY COLUMNmutation, theINSERT ... SELECTconversion andCASTall go through this wrapper, so all of them were affected.Solution. Fill the missing elements through the data type:
insertManyDefaultsIntocallsinsertDefaultIntofor each row, so:Enumgets its default member (the one with the lowest value, as documented), including when0is a member that is not the default.Tuplecontaining anEnumgets correct defaults recursively.cloneResized(insertDefault), soNullablestaysNULL,Arraystays[],LowCardinality,DateTime64etc. are unchanged.I preferred this over
createColumnConstWithDefaultValue(...)->convertToFullColumnIfConst()because it needs