Repository navigation
Fix CSVWithNames header column count for tuples serialized into separate columns - #107371
alexey-milovidov merged 13 commits into
Conversation
…ate columns With output_format_csv_serialize_tuple_into_separate_columns enabled (the default), SerializationTuple::serializeTextCSV writes each tuple value as N separate CSV cells, but CSVRowOutputFormat::writePrefix wrote the header from the top-level column names only. The result was a header with fewer fields than the data (issue ClickHouse#107342). The types row of CSVWithNamesAndTypes had the same problem, and CustomSeparated* with the CSV escaping rule was affected too. Add output_format_csv_header_serialize_tuple_into_separate_columns (default 1, opt-out). When enabled together with the existing value-flattening setting, the header (names row and types row) flattens each tuple column into its leaf fields recursively, using dotted names (User.ID, User.Name) and the leaf type names, so the header width matches the data width. A flattened header is not read back into a tuple by name when input_format_with_names_use_header = 1; such data round-trips with the setting set to 0 on output or input_format_with_names_use_header = 0 on input. This is documented on the setting. Closes: ClickHouse#107342 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-3:20260612-204200 |
|
cc @Avogar @al13n321 could you review this? It fixes the CSVWithNames/CSVWithNamesAndTypes header having fewer columns than the data when tuples are serialized into separate columns (issue #107342), by flattening the header rows the same way the values are flattened, behind a new opt-out setting output_format_csv_header_serialize_tuple_into_separate_columns (default 1). |
|
Workflow [PR], commit [78e0365] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
…s the tuple delimiter In CustomSeparated, tuple values are flattened into separate columns under the CSV escaping rule, and the tuple elements are joined with csv.tuple_delimiter (format_csv_delimiter). The format itself joins fields with custom.field_delimiter (format_custom_field_delimiter), which defaults to tab. When those two delimiters differ (for example the default tab field delimiter, or format_custom_field_delimiter = '|'), a Tuple(ID UInt64, Name String) value is still written as one custom field like 1,"a", while the previous prefix flattened the header into several fields such as User.ID|User.Name. That reintroduced the same header/data width mismatch in the opposite direction. Flatten the CustomSeparated header only when the custom field delimiter is the same single character as csv.tuple_delimiter, so the flattened header matches how the tuple value is laid out. The plain CSV path is unaffected because there csv.delimiter and csv.tuple_delimiter are both format_csv_delimiter. Extends the regression test with non-comma CustomSeparatedWithNames cases. Closes: ClickHouse#107342 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…d Nullable tuples The header leaf-count logic in FlattenTupleForCSVHeader diverged from the CSV value serializers in two cases, so the header width did not always match the data width: - An empty Tuple() value still occupies one field (SerializationTuple writes nothing between the surrounding delimiters), but the helper recursed into zero elements and emitted no header cell. It now emits one cell for an empty tuple. - A non-null Nullable(Tuple(...)) value serializes through the nested tuple (SerializationNullable delegates to it) and is flattened into separate columns, but the helper saw the top-level Nullable and kept one header cell. It now unwraps Nullable and flattens the inner tuple. Scalar Nullable types serialize as one field and keep one cell with the full Nullable type name. Extends the 04340 regression test with a mixed empty-tuple row, a non-null Nullable(Tuple(...)) column, and a scalar Nullable column. Regenerates 00300_csv and 02149_schema_inference_formats_with_schema_2 references: with output_format_csv_header_serialize_tuple_into_separate_columns default on, CSVWithNames(AndTypes) now flattens tuple columns in the header to match the already-flattened data. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ttening The output_format_csv_header_serialize_tuple_into_separate_columns description stated the flattening applies to the CSV escaping rule of CustomSeparated* without the delimiter condition the code enforces. The header is flattened for CustomSeparated* only when format_custom_escaping_rule = 'CSV' and format_custom_field_delimiter is a single character equal to format_csv_delimiter (= csv.tuple_delimiter); otherwise the header stays unflattened so it matches the one-field tuple value. Update the doc text so the documented contract matches the code. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
CI finished on this commit. All failures are confirmed unrelated to this PR (a CSV header doc-wording change cannot cause any of them):
The PR change is doc-only on top of the validated fix. No PR-caused failures. |
|
CI fully finished on HEAD CI ledger (covered SHA 569e75d):
All real test jobs pass. Ready for review. |
Rewrite the regression test from a .sql test with the no-parallel tag into a .sh test driven by clickhouse-local. The round-trip part wrote fixed-name files (04340_optout.csv, 04340_flat.csv) into the shared server user_files, so it needed no-parallel to avoid colliding with a concurrent copy of itself. clickhouse-local runs in CLICKHOUSE_TMP (unique per test run) and the round-trip files are prefixed with CLICKHOUSE_TEST_UNIQUE_NAME, so the test no longer touches shared state and runs in parallel with itself. The expected output is unchanged. Per reviewer request on ClickHouse#107371. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…t_with_types_use_header = 0 The output_format_csv_header_serialize_tuple_into_separate_columns note told readers to set input_format_with_names_use_header = 0 to read a flattened header back into a Tuple positionally. For CSVWithNamesAndTypes the types row is flattened too, so RowInputFormatWithNamesAndTypes::readPrefix validates the flattened types row against the single top-level Tuple input field (while input_format_with_types_use_header stays enabled) and rejects it with INCORRECT_DATA "The number of data types differs from the number of column names". The positional round-trip therefore also needs input_format_with_types_use_header = 0. Update the setting note accordingly and add the CSVWithNamesAndTypes positional round-trip case to 04340_csv_with_names_tuple_header.sh. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
CI finished on a4f8106. The only red test check, |
…dd test case The WithNamesAndTypes round-trip caveat for output_format_csv_header_serialize_tuple_into_separate_columns applies to CustomSeparatedWithNamesAndTypes too, not only CSVWithNamesAndTypes. When the setting flattens a CustomSeparatedWithNamesAndTypes header (format_custom_escaping_rule = 'CSV' and a single-character format_custom_field_delimiter equal to format_csv_delimiter), the types row is flattened into the leaf type fields (UInt64, String) while the input schema still has one top-level Tuple column. RowInputFormatWithNamesAndTypes::readPrefix validates that types row whenever input_format_with_types_use_header = 1 and rejects it (INCORRECT_DATA), exactly like CSVWithNamesAndTypes. So the positional read needs input_format_with_types_use_header = 0 in addition to input_format_with_names_use_header = 0. Doc note now names both *WithNamesAndTypes formats, and the test adds the CustomSeparatedWithNamesAndTypes positional round-trip case next to the CSV one. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
@groeneai merge with master and move new setting in history in new 26.7 section |
|
@groeneai, please take a look at ^ |
…o 26.7 settings history master bumped to 26.7.1, so the new setting's SettingsChangesHistory entry moves from the 26.6 block to a new 26.7 block (per reviewer request).
|
@alexey-milovidov done. Merged fresh master into the branch (8b2ed65) and moved |
Prior CI run on the previous head aborted in the Config Workflow bootstrap: the praktika fetch-previously-failed-tests step hit a transient play.clickhouse.com read timeout on all retries, the 600s job timed out (exit -15), and all downstream jobs were dropped. CIDB is healthy again; this empty commit re-runs the workflow on the same validated tree. Co-Authored-By: Claude Opus 4.8 <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 68/68 (100.00%) | Lost baseline coverage: none · Uncovered code |
|
@groeneai we have a problem: :) select number % 2 ? NULL : toNullable(tuple(1,2,3)) as t from numbers(4) format CSVWithNamesAndTypes
SELECT if(number % 2, NULL, toNullable(tuple(1, 2, 3))) AS t
FROM numbers(4)
FORMAT CSVWithNamesAndTypes
Query id: 138dc233-60a3-44bd-a5b2-748f6e03f713
"t"
"Nullable(Tuple(UInt8, UInt8, UInt8))"
1,2,3
\N
1,2,3
\N
4 rows in set. Elapsed: 0.002 sec.In case of |
|
@Avogar thanks, confirmed. The root cause: a Fix in #108959: a Note: |
A Nullable(Tuple(...)) had a row-dependent CSV width: a NULL row wrote a single \N field, while a non-null row was flattened into separate columns (N fields) under output_format_csv_serialize_tuple_into_separate_columns. The CSVWithNames(AndTypes) header was flattened to N columns, matching only the non-null rows, so NULL rows desynced the column count and the data did not round-trip (CANNOT_PARSE_INPUT). Keep a Nullable(Tuple) as a single quoted CSV value (it is not flattened), so every row is exactly one field and the header stays one cell. A bare Tuple (not wrapped in Nullable) is still flattened as before. Input is made symmetric so the value round-trips. Follow-up to ClickHouse#107371, reported by Avogar. Co-Authored-By: Claude Opus 4.8 <[email protected]>
A Nullable(Tuple(...)) had a row-dependent CSV width: a NULL row wrote a single \N field, while a non-null row was flattened into separate columns (N fields) under output_format_csv_serialize_tuple_into_separate_columns. The CSVWithNames(AndTypes) header was flattened to N columns, matching only the non-null rows, so NULL rows desynced the column count and the data did not round-trip (CANNOT_PARSE_INPUT). Keep a Nullable(Tuple) as a single quoted CSV value (it is not flattened), so every row is exactly one field and the header stays one cell. A bare Tuple (not wrapped in Nullable) is still flattened as before. Input is made symmetric so the value round-trips. Follow-up to ClickHouse#107371, reported by Avogar. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Rewrite the regression test from a .sql test with the no-parallel tag into a .sh test driven by clickhouse-local. The round-trip part wrote fixed-name files (04340_optout.csv, 04340_flat.csv) into the shared server user_files, so it needed no-parallel to avoid colliding with a concurrent copy of itself. clickhouse-local runs in CLICKHOUSE_TMP (unique per test run) and the round-trip files are prefixed with CLICKHOUSE_TEST_UNIQUE_NAME, so the test no longer touches shared state and runs in parallel with itself. The expected output is unchanged. Per reviewer request on ClickHouse#107371. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…-tuple-header-107342 Fix CSVWithNames header column count for tuples serialized into separate columns
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the
CSVWithNamesandCSVWithNamesAndTypesheader having fewer columns than the data whenoutput_format_csv_serialize_tuple_into_separate_columnsis enabled (the default). The header (and the types row) now flattensTuplecolumns into their leaf fields with dotted names (e.g.t.a,t.b), so the header column count matches the data. A new settingoutput_format_csv_header_serialize_tuple_into_separate_columns(default1) controls this and can be set to0to restore the previous single-name header.Description
Closes: #107342
When
output_format_csv_serialize_tuple_into_separate_columnsis enabled (the default),SerializationTuple::serializeTextCSVwrites each tuple value as N separate CSV cells, but the header was written from the top-level column names only, so the header had fewer fields than the data. Example before the fix:After the fix (default):
The same applied to the types row of
CSVWithNamesAndTypesand toCustomSeparated*with the CSV escaping rule.The new opt-out setting
output_format_csv_header_serialize_tuple_into_separate_columns(default1) flattens the header (names and types rows) recursively, mirroring the value flattening. Nested tuples expand recursively (t.a.x), unnamed tuples use positional element names (t.1,t.2), and non-tuple columns are unchanged.Round-trip note: a flattened header is not read back into a tuple by name when
input_format_with_names_use_header = 1(the input reader matches a tuple by its single name). Such data round-trips with the new setting set to0on output, or withinput_format_with_names_use_header = 0on input. This is documented on the setting.Test:
tests/queries/0_stateless/04340_csv_with_names_tuple_header.