Skip to content

Fix reading a trailing field delimiter at the end of TSV and CSV input - #128436

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
hsusul:fix/tsv-csv-trailing-delimiter-at-eof
Nov 2, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
hsusul:fix/tsv-csv-trailing-delimiter-at-eof

Conversation

@hsusul

@hsusul hsusul commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #91776

With input_format_tsv_allow_variable_number_of_columns / input_format_csv_allow_variable_number_of_columns and input_format_tsv_empty_as_default / input_format_csv_empty_as_default enabled, a row that ends with a field delimiter (with or without a final newline) failed with ATTEMPT_TO_READ_AFTER_EOF or a parse error, because the empty field before the end of the row was only treated as default when it belonged to the last column of the schema.

Now, when the empty-as-default setting is enabled, an empty field followed by the field delimiter, a line end, or EOF receives its default value, regardless of its position in the schema. The change applies to TSV, CSV, their header variants, TSVRaw, and HiveText through their shared readers (TabSeparatedFormatReader::readField and CSVFormatReader::readField).

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix parsing empty trailing fields in TSV and CSV input with a variable number of columns and input_format_tsv_empty_as_default or input_format_csv_empty_as_default enabled, including truncated rows without a final newline.


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

Version info

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

When input ended with a field delimiter and no final newline, the empty
field after it was deserialized at EOF. Some types (integers, Decimal,
Nullable of them) throw ATTEMPT_TO_READ_AFTER_EOF there, while the same
empty field before a line end is parsed. This made the whole query fail,
e.g. for truncated files with input_format_*_allow_variable_number_of_columns.

Read such a field as if a line end followed it, so input without a final
newline is parsed exactly like the same input with one.

Closes ClickHouse#91776

@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 isn't elegant. Do I understand correctly, that the empty string field isn't parsed correctly if the file ends with it with no extra newline, and it is somehow interacts with the "variable number of columns" mode? Let's think further...

@alexey-milovidov alexey-milovidov self-assigned this Nov 1, 2026
alexey-milovidov and others added 2 commits November 1, 2026 14:35
With `input_format_tsv_empty_as_default` / `input_format_csv_empty_as_default` the readers
treated a field as empty only if it was followed by the field delimiter, or, for the last
file column, by the end of the row. With a variable number of columns a row can end before
the last file column, so an empty field there was passed to the type's parser: `Float64` and
`Date` failed even with a final newline, `Nullable(Int64)` became `0` instead of `NULL`, and
integers failed with `ATTEMPT_TO_READ_AFTER_EOF` when the input had no final newline.

A field is now empty if it is followed by the field delimiter or the end of the row,
regardless of its position. This replaces the previous workaround that read the field from
a one-byte `"\n"` buffer at EOF.

ClickHouse#91776
ClickHouse#128436 (review)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

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

Now ok.

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

clickhouse-gh Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dd91426]

Summary: ✅


AI Review

Summary

The PR fixes empty trailing fields in short TSV and CSV rows by recognizing EOF and line endings independently of the field's schema position. No actionable findings. The added regression test passed in Fast test at the current PR head, dd91426dbf56.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dd91426db with master f27411a0a (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of ab781ea76; 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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 2, 2026
Merged via the queue into ClickHouse:master with commit ec3ec25 Nov 2, 2026
179 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 2, 2026
alexey-milovidov added a commit that referenced this pull request Nov 3, 2026
#128436 changed `CSVFormatReader::readField`
to treat a field starting with `\r` as empty with `input_format_csv_empty_as_default`, so the
fuzzer file `clusterfuzz-testcase-minimized-arrow-csv-fuzz-5085419734892544` now fails with
`CANNOT_PARSE_INPUT_ASSERTION_FAILED` instead of being read as one row. This was the reason
the merge queue rejected the PR:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=gh-readonly-queue/master/pr-125134-e347281f9c9d421459e72eefc44846ec85d67c06&sha=35025568ed58256edb98198b0a30fd72fa93fb7d&name_0=MergeQueueCI

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants