Repository navigation
Skip UTF-8 BOM before an automatically detected header - #126591
alexey-milovidov merged 8 commits into
Conversation
With `input_format_csv_detect_header` (and the TSV and CustomSeparated equivalents), a BOM was skipped only for formats with an explicit header, so it ended up in the first column name during schema inference, and the header row was then parsed as data. The BOM is now skipped only when the first row is detected as a header. If the first row is data, the BOM is kept as before, so a `String` value in the first column is not changed. Closes: ClickHouse#126424
alexey-milovidov
left a comment
There was a problem hiding this comment.
This looks like two fixes - one skips the BOM and another removes it from the first field. The first is cleaner. Why do we need both?
…hema reader Address review: ClickHouse#126591 (review) `RowInputFormatWithNamesAndTypes::tryDetectHeader` reads unescaped names, so the BOM must be skipped before reading (otherwise a quoted CSV name is read as an unquoted value with the quotes); the checkpoint allows to restore it if the first row is data. `FormatWithNamesAndTypesSchemaReader::tryDetectHeader` decides about the header only after reading several rows and cannot roll back; if the first row is data, the BOM is a part of its value and affects type inference. The fields there are raw, so the BOM is removed from the first field only when the header is detected. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Re #126591 (review) ("why do we need both?"): they fix two separate code paths. Neither mechanism works in the other path.
Skipping the BOM in both places would only work if we always dropped it whenever header detection is enabled. That changes the existing behavior for headerless I pushed 722994c, which adds comments explaining this at both places. |
|
Workflow [PR], commit [c1dcfb2] Summary: ✅
AI ReviewSummaryThe PR skips a leading UTF-8 BOM in Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 8 s compile time in total, 6 of them have a recent master baseline. |
… BOM as data Address review: the schema reader removed the BOM from the first field after the row had been split into fields, so a quoted first name containing a delimiter was split incorrectly, and the `JSON` escaping rule of `CustomSeparated` rejected the row at the BOM. Now, if there is a BOM, the first row is read twice: without the BOM as a header candidate, and with the BOM as data, so the inferred types of a headerless first row do not change. This replaces `removeBOMFromFirstField`, so the BOM is handled in the same way (skipped before reading) in the input format and in the schema reader. Also, a data row can be valid only with the BOM (for example `"abc"x` is an unquoted CSV value after a BOM). If reading it without the BOM fails with a parse error, it is not a header and is parsed with the BOM, as without header detection. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…etection Reading the first row without the BOM as a header candidate changed the state of the readers: `CustomSeparatedFormatReader` cached the number of columns, `CustomSeparatedSchemaReader` and `TabSeparatedFormatReader` cleared their first-row flags. The second read of the first row (as data, with the BOM) used that state, so headerless data could be split into a different number of columns, or rejected when `format_custom_row_between_delimiter` was set. Add `resetStateBeforeFirstRow` to the format and schema readers and call it after every rollback to the beginning of the first row. In schema inference, if the number of fields in the first row determines how the following rows are split (`CustomSeparated` without `input_format_custom_allow_variable_number_of_columns`), the first row can be a header only if it has the same number of fields with and without the BOM. Address review: ClickHouse#126591 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
…kip a BOM before a result prefix In the schema reader, the header classification now uses only the first row read without the BOM, and the provisional column names use its number of fields. The first row read with the BOM can split a quoted name containing a delimiter into fields of other types, which used to veto a valid header, e.g. `"a,2020,b",c` after a BOM. Its types are still used if the first row is data. `CustomSeparatedFormatReader::skipPrefixBeforeHeader` now skips a BOM before a nonempty `format_custom_result_before_delimiter`: the BOM cannot be a part of the first value there, and otherwise the prefix check fails at the BOM. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…h of the following rows In `FormatWithNamesAndTypesSchemaReader::tryDetectHeader`, the first row read without the BOM is used as a header only if the following rows have the same number of fields, when it differs from the first row read with the BOM. Otherwise the first row is data, as without header detection, e.g. `"a,2020,b",c` after a BOM followed by `1,2,3,4`. Previously this failed, or, with `input_format_csv_allow_variable_number_of_columns`, lost columns. The types of the rows read during header detection are now combined with support for a variable number of columns, as in `IRowSchemaReader::readSchema`, instead of `chooseResultColumnTypes` that throws when the widths differ. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… and check the width of the type names row When the first row read with a BOM is data, the end-of-data exit of `FormatWithNamesAndTypesSchemaReader::tryDetectHeader` now combines the types of all rows read so far, so the column count and types of the following rows are not lost with a variable number of columns. When the variant of the first row without the BOM has another number of fields than the variant with the BOM, the row with type names must also have the same number of fields as the header candidate, otherwise the first row is data. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Can't we detect BOM before schema inference (at the start of reading), remember the finding, and act accordingly (text formats will skip, binary formats will keep)? Maybe simply run it for text formats only? |
Instead of reading the first row twice (with and without the BOM) and reconciling the two variants, skip the BOM at the start of reading in text formats whenever the header can be detected automatically, in both `RowInputFormatWithNamesAndTypes::readPrefix` and `FormatWithNamesAndTypesSchemaReader::readSchema`. Binary formats don't support header detection, so they keep the BOM. As a consequence, with header detection enabled, a BOM before a headerless first `String` value is also skipped. With header detection disabled, it is kept as before. ClickHouse#126591 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@tripleaceme @alexey-milovidov — this PR looks backward incompatible but is categorized as Why: Suggested category: |
|
@tripleaceme @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
Closes: #126424
With header detection (
input_format_csv_detect_header,input_format_tsv_detect_header,input_format_custom_detect_header), a UTF-8 BOM was skipped only for formats with an explicit header, such asCSVWithNames. Schema inference kept the BOM in the first column name, the reader then did not recognize the header row and parsed it as data. Even when the query succeeded (the first column is aString), the first column name started with the BOM, and for a quoted header it was"name".Now, in text formats, the BOM is skipped at the start of reading whenever header detection is enabled, in both
RowInputFormatWithNamesAndTypes::readPrefixandFormatWithNamesAndTypesSchemaReader::readSchema, in the same way as for formats with an explicit header. Binary formats don't support header detection and keep the BOM.As a consequence, with header detection enabled (the default), a BOM before a headerless first
Stringvalue is also skipped. With header detection disabled, it is kept as before.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A UTF-8 BOM at the beginning of a
CSV,TSVorCustomSeparatedfile is now skipped when the header is detected automatically (which is enabled by default). Previously, the BOM became a part of the first column name, and the query could fail withCANNOT_PARSE_INPUT_ASSERTION_FAILED.Workflow [PR]
Sync PR [sync-upstream/pr/126591]
Version info
26.11.1.391-master(included in26.11and later)