Skip to content

fix: Bound a parquet write batch by bytes, not only by rows - #120028

Closed
erezrokah wants to merge 16 commits into
ClickHouse:masterfrom
cloudquery:parquet-writer-batch-byte-bound
Closed

erezrokah wants to merge 16 commits into
ClickHouse:masterfrom
cloudquery:parquet-writer-batch-byte-bound

Conversation

@erezrokah

@erezrokah erezrokah commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Related: #119300
Related: #120027
Related: #117754
Extended by: #120538

writeColumnImpl bites off a batch by row count alone (output_format_parquet_batch_size, 1024
rows), whatever those rows weigh, so 1024 values of a few MB reach the encoder as one
multi-gigabyte batch. That overruns the 32-bit offsets of the BinaryBuilder behind the dictionary
encoder, and then the 32-bit size of the data page.

Writing 1024 rows of ~2.1 MB:

String FixedString(2100016) peak RSS
master array cannot contain more than 2147483646 bytes, have 2148302982 ... have 2148316368 6.59 GB
moving the dictionary check alone Uncompressed page is too big: 2150407082 same 10.90 GB
this PR writes, round-trips exactly writes, round-trips exactly 2.57 GB

A batch is cut at the first record boundary at or after 64 MiB, so ordinary data is written byte for
byte as before (0.00% size change over String, FixedString, LowCardinality, Nullable,
Array, numeric and page-index columns, and value widths from 64 B to 64 KiB). A record that would
not fit a page at all is split wherever it happens to be, dropping that chunk's column and offset
indexes, since neither can describe pages starting mid-record.

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):

Fixed Parquet output failing with Capacity error: array cannot contain more than 2147483646 bytes or Uncompressed page is too big when a batch of rows held more than 2 GiB of String or FixedString data. A batch is now bounded by bytes as well as by rows, which also lowers the peak memory of the write.


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

@clickhouse-gh clickhouse-gh Bot added comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). external Author is not a member of the ClickHouse organization labels Sep 14, 2026
@rschu1ze rschu1ze added the can be tested Allows running workflows for external contributors label Sep 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d3c980b]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_binary, flaky check) FAIL
05175_parquet_wide_values_no_32bit_overflow FAIL cidb
05175_parquet_wide_values_no_32bit_overflow FAIL cidb
05175_parquet_wide_values_no_32bit_overflow FAIL cidb
05175_parquet_wide_values_no_32bit_overflow FAIL cidb
05175_parquet_wide_values_no_32bit_overflow FAIL cidb
Too many test failures FAIL cidb
Stateless tests (arm_binary, sequential) FAIL
05175_parquet_wide_values_no_32bit_overflow FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

This PR materially improves the Parquet writer for wide String / FixedString rows, but the current head still leaves two previously discussed > 2 GiB overflow paths open. Because those paths are in the same byte-bounding contract the PR is trying to establish, I do not think the patch is merge-ready yet.

Findings

❌ Blockers

  • src/Processors/Formats/Impl/Parquet/Write.cpp:1266 The new byte cut still does not apply to fixed-width physical types. limit_batch_by_bytes runs only for parquet::ByteArrayType and parquet::FLBAType, so repeated fixed-width rows such as Array(UInt64) still pass the entire record to encoder->Put when output_format_parquet_write_page_index = 1. A single large row can therefore still hit Uncompressed page is too big before any split or preflush logic runs. Suggested fix: restore a byte budget for the remaining physical types as well, using sizeof(c_type) (or the actual packed width for booleans) when computing batch_byte_size, so the oversized-record split and the pre-Put page-limit guard apply to numeric pages too.

  • src/Processors/Formats/Impl/Parquet/Write.cpp:1269 Plain BYTE_ARRAY pages are still budgeted by payload bytes only. The 4-byte length prefix is added only in the later “would this next batch overflow the current page?” check, so it is invisible when deciding whether a single repeated record must be split. A row with many small BYTE_ARRAY values can still stay under max_record_bytes here and then exceed INT32_MAX when FlushValues writes 4 + len per value. Suggested fix: make the batch cut count encoded BYTE_ARRAY size (sizeof(uint32_t) + len per present value) while continuing to report payload-only bytes in unencoded_byte_array_data_bytes.

Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit d3c980b.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 69/80 (86.25%) · Uncovered code

Full report · Diff report

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

clickhouse-gh Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit d3c980b416ac2b57d32041db8c4da3fcd38c4ca9 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 17, 2026
The dmesg grep in the fuzzer jobs' OOM branch matched host-global kills only:
'Out of memory: Killed process', 'oom_reaper: reaped process' and
'oom-kill:constraint=CONSTRAINT_NONE'.

A cgroup-scoped kill writes none of them. The kernel prints
'oom-kill:constraint=CONSTRAINT_MEMCG' for the constraint, and its victim line
reads 'Memory cgroup out of memory: Killed process', whose lowercase
'out of memory' the existing capital-O pattern cannot match.

While that branch stays silent, FuzzerLogParser falls through to
'Server unresponsive: memory limit exceeded' and names whichever Code: 241 was
last in the server log, that is, a bystander query instead of the kill. That
fall-through name carries 90 rows across 42 pull requests in the last 30 days,
75 of them on the AST fuzzer amd_debug family.

Measured on two real dmesg captures of memcg kills of clickhouse-serv, from
ClickHouse#118877 head f082cbb and ClickHouse#120028 head 53f179a: the three existing patterns
match 0 lines in both, and the two added patterns match 1 line each, so the
branch does not fire before this change and does after it. The global-OOM
capture recorded earlier on this branch still matches, and the same capture
with its OOM lines removed still does not.

One downstream effect is deliberate and worth naming: a cgroup-scoped row now
lands as the synthetic 'OOM in dmesg' rather than as 'Server unresponsive:
memory limit exceeded', so revert_ci_regressions.py treats it as an aborted run
instead of investigating it. That is the intended reading, and it matches the
treatment this branch already gives the host-global kill. The old name was
assigned from whichever Code: 241 happened to be last in the server log, so it
named a bystander query and no single change to revert, while spending one of
the four investigation slots a run has. Per that file's own docstring an
over-inclusive list costs at most a wasted investigation, never a wrong revert.
@alexey-milovidov alexey-milovidov self-assigned this Sep 20, 2026
@erezrokah
erezrokah force-pushed the parquet-writer-batch-byte-bound branch from 5cb2254 to aafb6bb Compare September 21, 2026 11:24
writeColumnImpl bit off a batch by row count alone (output_format_parquet_batch_size,
1024 rows), whatever those rows weighed, so 1024 values of a few MB reached the
encoder as one multi-gigabyte batch and overran two separate 32-bit limits.

arrow keeps the dictionary of BYTE_ARRAY and FIXED_LEN_BYTE_ARRAY in a BinaryBuilder
addressed with 32-bit offsets. The existing fallback to plain encoding asked whether
the dictionary had grown too big only after Put, so a batch over that limit threw
inside Put before the fallback could run. Checking before Put is not enough on its
own: the same batch then becomes one data page, whose size is 32-bit as well, and the
write fails with `Uncompressed page is too big` instead.

A batch is now cut at the first record boundary at or after 64 MiB. The budget only
has to keep those 32-bit quantities out of reach, so it sits far above any page a
caller would ask for and ordinary data comes out byte for byte as it did before. The
dictionary check is also made before Put and extended to FIXED_LEN_BYTE_ARRAY, which
shares that builder.
The bound was only applied at record boundaries, because that is where pages have to
start when the page index is written, and output_format_parquet_write_page_index is on
by default. A single Array(String) row larger than 2 GiB therefore stayed in one batch
and still failed with `Uncompressed page is too big`.

Such a record is now split at 1 GiB wherever it happens to be. Neither the column index
nor the offset index can describe a chunk whose pages start mid-record, so both are
dropped for it rather than written wrong: the offset index addresses whole rows through
first_row_index, and a continuation page has no row of its own to point at.
The threshold was 1 GiB, well below the page size that forces the split, so a record
between 1 GiB and 2 GiB was split and lost its page index even though master writes it
whole with a valid one. It is now just under the limit that makes the split necessary,
so only a record that would otherwise fail to write is affected.
The test timed out on the stateless runs. It peaked around 15 GB: the oversized record was
built by mapping over a range of 2200 million-byte strings, and read back with a GROUP BY
on a 2.2 GB array. The record is now collected with groupArray over many small values,
which costs 4.7 GB, and the read backs no longer group by a huge value or take uniqExact
over one - a hash of it answers the same question. Runtime goes from 12s to 3s locally.

The record still holds 2.2 GB, more than a page can address, so it is still split: with the
bound disabled the write fails with `Uncompressed page is too big: 2208800020`.
limit_batch_by_bytes ran on the converted values, so the converter had already built the whole
batch by the time it was measured. ConverterJSON serializes each value into storage of its own,
which meant a batch of wide Object values was materialized in full before being shrunk.

The batch is now cut from the source column's sizes first, and measured again afterwards because
serializing a value can grow it. Writing 1024 rows of ~2.1 MB JSON goes from 101s to 12s and from
7.58 GB to 6.62 GB peak RSS.
Cutting the batch from ColumnObject::byteSizeAt does not work: it counts the values but
not the keys or the punctuation SerializationJSON writes back out, which is why
getSerializedValueSize returns nullopt for it. Rows with many long keys and small values
stayed under the cut and were still materialized in full.

ConverterJSON now stops serializing once the batch reaches the budget and reports how much
of it it produced, and the caller shrinks the batch to that. Only for a column with no
repetition levels: a shorter batch has to stay a whole number of records when pages follow
record boundaries, and the values that would complete one have not been converted.

Writing 1024 rows whose keys serialize to ~4 MB each goes from 382s and 4.88 GB peak RSS to
19s and 0.66 GB. The wide `JSON` case in 05175 now uses long keys, which is the shape the
source column's own size does not see.
The budget was only given to a column with no repetition levels, so Array(JSON) and
Map(..., JSON) went back to being cut by ColumnObject::byteSizeAt, which does not measure
what serializing a value costs.

The converter is bounded for those as well now. Where pages follow record boundaries the
batch gives back the tail of the record it stopped inside, and a record that wants more than
the budget on its own is converted whole, because it cannot be split. Writing 1024 rows of
Array(JSON) whose keys serialize to ~4 MB each goes from 369s to 21s.

Peak RSS is unchanged at 8.4 GB for that query: it is the values themselves, not the
converter's buffer, and bounding the batch does not make the column smaller.
When the converter stopped inside the first record of the batch, the fallback asked it for
the whole provisional batch again without a budget. That batch is write_batch_size values
extended to the end of a record, so everything behind the first row was serialized as well,
and then dropped by the cut that follows.

Only the first record is converted now. On 11 rows of 100 JSON values of ~700 KB, where one
batch covers every row: 19s and 3.04 GB peak RSS before, 10.5s and 0.79 GB after, and the
file is byte for byte the same.
Plain BYTE_ARRAY prefixes every value with its 4-byte length, and the cuts counted only the
payload. A record of many small values - FixedString written as BYTE_ARRAY reaches this,
with output_format_parquet_fixed_string_as_fixed_byte_array = 0 - can therefore stay under
max_record_bytes while the page it builds does not fit.

Both cuts now add the per-value overhead: 4 bytes for BYTE_ARRAY, none for
FIXED_LEN_BYTE_ARRAY, which writes values back to back. The total they return is still the
payload alone, because that is what unencoded_byte_array_data_bytes reports.
…atch

Only BYTE_ARRAY and FIXED_LEN_BYTE_ARRAY were budgeted, so a record of fixed-width values
went to the encoder whole however large it was. Array(UInt64) of 280 million elements failed
with `Uncompressed page is too big: 2240000022`; it writes now. A batch of write_batch_size
fixed-width values is a few kilobytes, so ordinary columns never reach the cut.

ConverterJSON also sized buf and stash from the number of values it was asked for, which for
a repeated carrier is the whole record however many values that is - allocating for all of
them before the budget could stop the loop. It now appends the serialized values to one
buffer and hands out pointers into it once that buffer has stopped growing, so both it and
the lengths grow only as far as the budget allows.
The retry disabled the budget outright, so a record larger than a page was serialized whole
however large it was - which for JSON the source column's size does not predict.

It is budgeted by max_record_bytes instead. A record that fits a page is converted whole as
before; one that does not is cut where the budget runs out, which is what the split below
would do with it anyway, at the cost of the page index. The converter's peak is then what a
page can hold rather than whatever the record happens to be.

Checked with the limits lowered to 8 MiB and 32 MiB against a 70 MB record: the write splits
mid-record, the file reads back the same, and the retry stops at the budget instead of
serializing the whole record.
This branch had grown two changes with different standing: the batch byte bound, which is
demonstrable, and converter-level batching for JSON carriers, whose remaining invariants
need more than 2 GiB inside one record and cannot be exercised by a test runner.

Only the byte bound and the oversized-record split are left here. The converter work
continues on parquet-json-converter-budget and opens once this lands, since it builds on
this change.
Bounding the batch is not enough on its own: the page keeps taking batches until it reaches
output_format_parquet_data_page_size, and that setting has no upper bound. Set near the
32-bit page limit, ordinary String data still reached `Uncompressed page is too big`.

The page target is capped at the same limit a record is split on, so it no longer depends on
the setting staying small. The default of 1 MiB is unaffected.
@erezrokah
erezrokah force-pushed the parquet-writer-batch-byte-bound branch from aafb6bb to f6ea1ec Compare September 21, 2026 11:27
The page size was only checked after `encoder->Put`, against a target of 2 GiB - 64 MiB. A
record can legally be almost that large on its own, so with a raised
`output_format_parquet_data_page_size` a ~1.99 GB `Array(String)` record left the page just under
the target, the next ~200 MB record went in whole, and the page failed with
`Uncompressed page is too big: 2190016121`.

When the page already holds data and the next batch's encoded size would push it past the limit,
the page is flushed first and the batch starts the next one. The check runs before the batch's
statistics and bloom filter hashes are accumulated, so nothing is attributed to the page it no
longer joins. Dictionary-encoded pages hold only indices and are left alone.
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Superseded by #122482. The branch lives in an organization-owned fork, and GitHub does not let maintainers push to those even with maintainer edits enabled, so I could not push the fixes here. #122482 keeps your commits, so the authorship is yours, and adds: the 4-byte BYTE_ARRAY prefix and the fixed-width budget in the batch cut (the two AI review blockers, ported from #120538), and a fix for 05175 being OOM-killed on CI (parallel encoding copied the 2.2 GB column; peak goes from 8.8 GB to 2.5 GB). #120538 can be rebased onto #122482. Thank you, @erezrokah!

nihalzp pushed a commit to nihalzp/ClickHouse that referenced this pull request Oct 9, 2026
The oversized-record case was killed on every CI run (`Stateless tests (arm_binary, sequential)`
and the flaky check): `groupArray` plus parallel encoding, which hands the encoder a copy of the
whole 2.2 GB column, peaked at 8.8 GB. Building the array with `arrayMap` and disabling
`output_format_parquet_parallel_encoding` brings it to 2.5 GB, in line with the other cases.

Also covers `FixedString` written as `BYTE_ARRAY`, which goes through the length-prefix budget.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120028&sha=d3c980b416ac2b57d32041db8c4da3fcd38c4ca9&name_0=PR
PR: ClickHouse#120028

Co-Authored-By: Claude Opus 5.5 (1M context) <[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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants