Skip to content

Skip UTF-8 BOM before an automatically detected header - #126591

Merged
alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
tripleaceme:fix-bom-with-header-detection
Oct 21, 2026
Merged

alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
tripleaceme:fix-bom-with-header-detection

Conversation

@tripleaceme

@tripleaceme tripleaceme commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

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 as CSVWithNames. 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 a String), 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::readPrefix and FormatWithNamesAndTypesSchemaReader::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 String value is also skipped. With header detection disabled, it is kept as before.

Changelog category (leave one):

  • Improvement

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, TSV or CustomSeparated file 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 with CANNOT_PARSE_INPUT_ASSERTION_FAILED.


Workflow [PR]
Sync PR [sync-upstream/pr/126591]

Version info

  • Merged into: 26.11.1.391-master (included in 26.11 and later)

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
@CLAassistant

CLAassistant commented Oct 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Oct 18, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@alexey-milovidov alexey-milovidov self-assigned this Oct 19, 2026
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Oct 19, 2026
…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]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Re #126591 (review) ("why do we need both?"): they fix two separate code paths. Neither mechanism works in the other path.

  • RowInputFormatWithNamesAndTypes::tryDetectHeader (the reader): readRowForHeaderDetection returns names that are already unescaped. If the BOM is still there, \xEF\xBB\xBF"name" gets read as an unquoted CSV value that keeps the quotes. Stripping three bytes afterwards would leave "name", so the BOM has to be skipped before reading. The checkpoint already present there brings it back if the first row turns out to be data.
  • FormatWithNamesAndTypesSchemaReader::tryDetectHeader (the schema reader): it can't skip the BOM up front. It decides about the header only after reading two, three or more rows, and there is no rollback. When the first row is data, the BOM belongs to its first value and changes the inferred type. For example, \xEF\xBB\xBF1,2 is inferred as String, the same as without header detection (see 01258_bom_tsv.sh). If the schema reader skipped the BOM, it would infer Int64, and then the reader, which keeps the BOM for data rows, would fail. Here the fields are still raw (they are unescaped later in readNamesFromFields), so the BOM is a plain prefix of the first field and can be removed once the header is detected.

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 String data.

I pushed 722994c, which adds comments explaining this at both places.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c1dcfb2]

Summary: ✅


AI Review

Summary

The PR skips a leading UTF-8 BOM in CSV, TSV, and CustomSeparated whenever automatic header detection is enabled, consistently in schema inference and data reading. No actionable findings remain in the current revision.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c1dcfb292 with master 69553a30f (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e4f63d9ce; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 8 s compile time in total, 6 of them have a recent master baseline.

Job report

… 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]>
@alexey-milovidov

Copy link
Copy Markdown
Member

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]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2026
Merged via the queue into ClickHouse:master with commit 61caa06 Oct 21, 2026
179 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 21, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@tripleaceme @alexey-milovidov — this PR looks backward incompatible but is categorized as Improvement.

Why: readPrefix (RowInputFormatWithNamesAndTypes.cpp:91) now strips the BOM whenever header detection is on, and that is the default. This changes the value of a BOM-prefixed headerless String field (stated in the PR body). It also breaks tables persisted by earlier schema inference with a \xEF\xBB\xBF-prefixed first column, reproduced 26.3 -> master.

Suggested category: Backward Incompatible Change. Ignore if the change is intentionally non-breaking.

@clickgapai

Copy link
Copy Markdown
Contributor

@tripleaceme @alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

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

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UTF-8 BOM is not skipped with an auto-detected CSV format: it ends up in the first column name

5 participants