Skip to content

Detect UTF-16 BOM in CSV/TSV/JSONEachRow input and process it accordingly. - #89360

Draft
hp77-creator wants to merge 49 commits into
ClickHouse:masterfrom
hp77-creator:feature-6476
Draft

hp77-creator wants to merge 49 commits into
ClickHouse:masterfrom
hp77-creator:feature-6476

Conversation

@hp77-creator

@hp77-creator hp77-creator commented Nov 1, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

You can now use files which are in UTF-16 BOM or UTF-32 BOM encoding for CSV/TSV/JSONEachRow file formats in ClickHouse. Resolves #6476.

Documentation entry for user-facing changes

Motivation: Currently UTF-16 BOM & UTF-32 BOM are not supported in ClickHouse, this PR intends to support it

Example use:
For a CSV file with UTF-16 BOM, doing the following:

SELECT * FROM file('test_utf16.csv', CSVWithNames);

should give results which a normal SELECT statement would give

Details

Resolves #6476


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

@hp77-creator hp77-creator changed the title Feature: Detect UTF-16 BOM in CSV/TSV/JSONEachRow input and process it accordingly. Detect UTF-16 BOM in CSV/TSV/JSONEachRow input and process it accordingly. Nov 1, 2025
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Nov 1, 2025
@clickhouse-gh

clickhouse-gh Bot commented Nov 1, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f31b036]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
Build ClickHouse FAIL
Build (arm_tidy, 3/4) FAIL
Build ClickHouse FAIL cidb
Code Review DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_asan_ubsan) DROPPED
Build (arm_binary) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 1, 2025
@hp77-creator
hp77-creator marked this pull request as ready for review November 12, 2025 09:17
@hp77-creator
hp77-creator marked this pull request as draft November 12, 2025 11:10
@hp77-creator
hp77-creator marked this pull request as ready for review December 13, 2025 05:03
@hp77-creator
hp77-creator marked this pull request as draft December 13, 2025 05:03
Comment thread src/Storages/StorageFile.cpp Resolved
Comment thread src/IO/UTFConvertingReadBuffer.cpp Resolved
Comment thread src/IO/UTFConvertingReadBuffer.cpp Resolved
Comment thread src/Storages/StorageFile.cpp Resolved
Comment thread src/Formats/FormatFactory.cpp Resolved
Comment thread src/IO/UTFConvertingReadBuffer.cpp Resolved
alexey-milovidov pushed a commit to yurifedoseev/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit that referenced this pull request Jun 6, 2026
…rg::alter

PR #106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. #89360, #103540, #106120, #106386, #106404, #106522, #105102,
#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to gregakinman/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to niyue/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit that referenced this pull request Jun 6, 2026
…rg::alter

PR #106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. #89360, #103540, #106120, #106386, #106404, #106522, #105102,
#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit that referenced this pull request Jun 6, 2026
…rg::alter

PR #106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. #89360, #103540, #106120, #106386, #106404, #106522, #105102,
#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit to JTCunning/ClickHouse that referenced this pull request Jun 6, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
alexey-milovidov pushed a commit that referenced this pull request Jun 6, 2026
…rg::alter

PR #106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. #89360, #103540, #106120, #106386, #106404, #106522, #105102,
#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
…in `RowInputFormatWithNamesAndTypes::setReadBuffer`

- Prevent double-wrapping inside `IInputFormat::setReadBuffer` by verifying the incoming buffer type is not `PeekableReadBuffer` or `UTFConvertingReadBuffer`.
- In `RowInputFormatWithNamesAndTypes::setReadBuffer`, call `IInputFormat::setReadBuffer` first to wrap the input stream before storing it in `format_reader`.
- Add stateless regression test `04245_async_insert_json_compact_utf_bom.sh`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=89360&sha=latest&name_0=PR
PR: ClickHouse#89360
Comment thread src/Formats/FormatFactory.cpp Resolved
This commit resolves CANNOT_PARSE_INPUT_ASSERTION_FAILED exceptions
that occurred during CustomSeparated and similar format parsing.
The failures were caused by two compounding issues introduced in recent commits:
1. Incorrect Offset Calculation in PeekableReadBuffer:
The calculation of offsetFromCheckpoint was reverted to use offset, but
rollbackToCheckpoint was incorrectly left using sub_buf->position.
Because offset implicitly includes the underlying sub_buf->offset,
adding it to sub_buf->position effectively added the offset twice.
This caused pos to overshoot its target by the exact offset length
during rollbacks. Fixed by restoring pos = buffer.begin + ...
2. Double wrapping of UTFConvertingReadBuffer:
The setReadBuffer methods in CustomSeparatedRowInputFormat and
other formats were modified to unconditionally wrap the incoming buffer
in a UTFConvertingReadBuffer. However, FormatFactory already applies
this wrapper. The resulting double wrapping bloated the stack and
caused the inner buffer to have a non-zero initial offset, which
triggered the PeekableReadBuffer arithmetic bug.
Fixed by adding a typeid_cast check to prevent duplicate wrappers.
Comment thread src/Formats/FormatFactory.cpp Outdated
// Add ParallelReadBuffer and decompression if needed.

auto owned_buf = wrapReadBufferIfNeeded(_buf, compression, creators, format_settings, settings, is_remote_fs, parser_shared_resources);
if (shouldDetectUTFBOM(name))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Wrapping the visible input stream here changes byte accounting in a way the rest of the stack does not understand. IRowInputFormat / JSONColumnsBlockInputFormatBase compute progress and max_bytes_to_read via getDataOffsetMaybeCompressed(getReadBuffer()), and schema inference uses in.count() for input_format_max_bytes_to_read_for_schema_inference; neither path unwraps generic ReadBufferWrapperBase. Once the source goes through UTFConvertingReadBuffer, those counters measure transcoded UTF-8 bytes instead of source bytes, so UTF-16 / UTF-32 inputs can read roughly 2-4x more raw data than the configured byte limits allow. Please recurse through generic wrappers in the byte-accounting helpers (or otherwise expose the underlying source offset from UTFConvertingReadBuffer) before enabling this wrapper here.

Comment thread src/IO/UTFConvertingReadBuffer.cpp Outdated

bool UTFConvertingReadBuffer::poll(size_t timeout_microseconds)
{
if (hasPendingData() || pending_bytes_count > 0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ poll is no longer the always-ready regression, but it still over-promises readiness for UTF-16/UTF-32 when only a partial code unit is buffered.

A concrete case is a streaming UTF-16 source that currently has only FF FE 7B available. detectBOM consumes the BOM and leaves one raw byte in pending_bytes, so this branch returns true, but the next readUTF16CodeUnit still has to call impl->next() for the second byte and can block past input_format_max_block_wait_ms. The same issue exists whenever impl->poll() is true with fewer than the bytes needed to decode the next code point.

Please make readiness depend on having enough buffered raw bytes to decode at least one UTF-16/UTF-32 code point without another blocking read, taking pending_high_surrogate into account. A focused streaming test that splits a code unit across the timeout boundary would catch this.

@abashkeev abashkeev added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 3, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…rg::alter

PR ClickHouse#106102 (merged 2026-06-05 17:43 UTC) refactored Iceberg::alter to
declare last_version and compression_method outside an if/else that
assigns them in both branches. Clang-tidy cppcoreguidelines-init-variables
flags the bare declarations and Build (arm_tidy) is built with
-warnings-as-errors, so the build fails for every PR whose CI ran on
master after this commit.

CIDB shows 30+ unrelated PRs hitting this in the last 2 days
(e.g. ClickHouse#89360, ClickHouse#103540, ClickHouse#106120, ClickHouse#106386, ClickHouse#106404, ClickHouse#106522, ClickHouse#105102,
ClickHouse#106590).

Initialize both variables to safe defaults at declaration. They are
unconditionally overwritten in both if and else branches before use,
so behavior is unchanged. The new defaults are only relevant if a
future code path skips both assignments, in which case last_version=0
and compression_method=CompressionMethod::None are sane no-op values
(the same defaults the old structured-binding form would produce
through default-construction of the destructured aggregate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
(cherry picked from commit d72cac5)
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026

This branch has not been deployed

No deployments
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect UTF-16 BOM in CSV/TSV/JSONEachRow input and process it accordingly.

4 participants