Skip to content

fix: Bound the parquet converters that cannot be measured before they run - #120538

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

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

Conversation

@erezrokah

@erezrokah erezrokah commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Depends on: #120028
Related: #120027

Carries #120028 as its first commit until that lands, since a pull request based on another branch
gets no CI here.

#120028 bounds a batch by the bytes it holds, measured after conversion. That is too late for a
converter whose cost cannot be predicted from the column: a JSON value's size is not
ColumnObject::byteSizeAt, which counts the values but neither the keys nor the punctuation
serialization writes out. ConverterJSON now stops at a byte budget and reports how much of the
batch it produced, and its buffers grow with that rather than with the count it was asked for.

before after
1024 rows of ~2.1 MB JSON 101s, 7.58 GB 12s, 6.62 GB
the same as Array(JSON) 369s, 8.39 GB 21s, 8.39 GB
a record crossing the budget mid-row 19s, 3.04 GB 10.5s, 0.79 GB
Array(UInt64), 280M elements Uncompressed page is too big: 2240000022 writes

Batches are also measured by what the encoding writes rather than payload alone, since plain
BYTE_ARRAY prefixes every value with its 4-byte length, and every physical type's batch is bounded
before it reaches a page. Only BYTE_ARRAY and JSON are cut before conversion, though: converters
that stage widened or transformed values (UInt8/UInt16, DateTime64, UUID, Decimal*,
UInt128/UInt256, FixedString) still allocate staging for the whole batch before the cut. That
costs memory on a wide row, not correctness, and is left to a follow-up. Two invariants here are argued rather than demonstrated: reaching
the page limit through those prefixes alone needs upwards of 16.7M values in one record, and the
fixed-width case 2^28, and building either costs more than 18 GB, so neither is in the test.

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 spending minutes and gigabytes on wide JSON values, and failing with Uncompressed page is too big for a row holding more than 2 GiB of fixed-width values. Batches are now bounded by what serializing and encoding them costs.


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

@erezrokah erezrokah changed the title fix: Bound a parquet write batch by bytes, not only by rows fix: Bound the parquet converters that cannot be measured before they run Sep 17, 2026
@erezrokah
erezrokah marked this pull request as draft September 17, 2026 09:05
@erezrokah
erezrokah marked this pull request as ready for review September 17, 2026 09:14
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Sep 17, 2026
@erezrokah
erezrokah marked this pull request as draft September 17, 2026 09:31
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 18, 2026
@erezrokah
erezrokah force-pushed the parquet-writer-batch-byte-bound-only branch from d83aaf5 to a82fb92 Compare September 21, 2026 11:25
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, far above any page a
caller would ask for, so ordinary data comes out byte for byte as before. A record that
would not fit a page at all is split wherever it happens to be, and the column and offset
indexes are dropped for that chunk, because neither can describe pages that start
mid-record. The dictionary check is also made before Put and extended to
FIXED_LEN_BYTE_ARRAY, which shares that builder.
A JSON value's size is not the column's byteSizeAt: that counts the values but neither the
keys nor the punctuation serialization writes out, and ColumnObject reports no serialized
size at all. Cutting the batch after conversion therefore let ConverterJSON materialize the
whole batch first.

ConverterJSON now stops at a byte budget and reports how much of the batch it produced,
including for repeated carriers, where the batch gives back the tail of the record it
stopped inside. A record that wants more than the budget is converted whole, and only that
record rather than the rest of the batch behind it. Its buffers grow with what it produced
instead of with what it was asked for.

Batches are also budgeted by what the encoding writes rather than by payload alone - plain
BYTE_ARRAY prefixes every value with its 4-byte length - and every physical type is
budgeted, not only the two byte-array ones: Array(UInt64) of 280 million elements failed
with `Uncompressed page is too big: 2240000022`.

Writing 1024 rows of ~2.1 MB JSON goes from 101s to 12s; the same as Array(JSON) from 369s
to 21s.
@erezrokah
erezrokah force-pushed the parquet-writer-batch-byte-bound-only branch from a82fb92 to 9dd1c52 Compare September 21, 2026 11:27
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Sep 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [268474e]

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
Performance Comparison (arm_release, master_head, 2/6) FAIL Performance dashboard
read_in_reverse_order_final #0 FAIL dashboard
read_in_reverse_order_final #1 FAIL dashboard
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

This PR bounds Parquet write batches by bytes, adds self-budgeting to the JSON converter, and drops page indexes when an oversized record has to be split. The current patch fixes several wide-value failure modes, but two important staging paths are still not actually bounded, so I would keep this in Request changes.

Missing context / blind spots
  • ⚠️ Full CI coverage was not complete at review time: amd_debug, ASan, TSan, MSan, and the remaining binary builds were still queued or in progress on commit 268474e6a49d6cd3878a8e5abb7b36eba41848fa.
Findings

⚠️ Majors

  • [src/Processors/Formats/Impl/Parquet/Write.cpp:1290] The new byte cap still runs before getBatch only for BYTE_ARRAY, so the widening / transformed fixed-width converters (UInt8/UInt16 -> INT32, DateTime64 -> INT64, UUID, Decimal*, UInt128/UInt256, FixedString -> FIXED_LEN_BYTE_ARRAY) still allocate full-row staging before lines 1361-1377 can shrink the batch. A single wide record therefore still reintroduces the same multi-GiB buf / data_buf failure mode on paths the changelog describes as fixed. Suggested fix: cut these converters before getBatch too, or make them self-budget the way ConverterJSON does.
  • [src/Processors/Formats/Impl/Parquet/Write.cpp:1344] Retrying the first oversized JSON record with max_record_bytes only budgets serialized chars; ConverterJSON's lengths and buf still grow with value count. One Array(JSON) row with many tiny scalars can stay under the 2 GiB page-split threshold yet still allocate multi-GiB metadata on this retry, so the converter is not actually bounded on high-cardinality rows. Suggested fix: add a metadata / value-count budget to ConverterJSON, or fall back to dropping the page index and splitting the record when the retry itself would exceed the staging budget.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: bound the remaining fixed-width staging converters before getBatch, and keep the JSON whole-record retry from re-materializing high-cardinality rows.

LLVM Coverage Report

⚠️ No coverage measurement for commit 268474e: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_5.profdata.

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

if constexpr (std::is_same_v<ParquetDType, parquet::ByteArrayType>)

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.

The byte cap only runs before conversion for BYTE_ARRAY, but getBatch below still sees the full record size for every converter that owns staging storage. That leaves the same failure mode on widening / transformed paths such as UInt8/UInt16 -> INT32, DateTime64 -> INT64, UUID, Decimal*, UInt128/UInt256, and FixedString -> FIXED_LEN_BYTE_ARRAY: a single wide row still makes getBatch allocate count-sized buf / data_buf before lines 1361-1377 get a chance to shrink the batch.

So the new invariant from the PR body ("every physical type is budgeted") does not hold yet. This needs either an early byte cut for every converter with temporary storage, or the same self-budgeting hook that ConverterJSON now implements.

def_count = def_count_kept;
data_count = values_in(def_count_kept);

converter.setByteBudget(max_record_bytes);

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.

Retrying the first oversized record under max_record_bytes still leaves this path unbounded on value count, because ConverterJSON only budgets chars. For Array(JSON) with one high-cardinality row of tiny scalars, this retry can keep the whole row (the encoded page is still well below the 2 GiB split threshold) while lengths and buf grow for every element in the row. That turns a sub-2 GiB page into multi-GiB staging before limit_batch_by_bytes gets a chance to cut anything, so the new "budgeted converter" invariant is still false for high-cardinality rows.

This needs either a second budget on per-value metadata or the same "drop the page index and split the row" fallback once the retry itself would exceed the staging budget.

@erezrokah

Copy link
Copy Markdown
Contributor Author

Closing. #122482 supersedes the core batch fix; the JSON converter budgeting here is not carried forward for now.

@erezrokah erezrokah closed this Oct 1, 2026
nihalzp pushed a commit to nihalzp/ClickHouse that referenced this pull request Oct 9, 2026
…ch cut

Addresses the AI review blockers on this PR. `limit_batch_by_bytes` counted only the payload of a
`BYTE_ARRAY` value, while plain encoding also writes a 4-byte length prefix per value, so a
repeated record of many small values could stay below `max_record_bytes` and still build a page
past `INT32_MAX`. The cut now measures `4 + len` per value; the returned total stays payload-only
because it feeds `unencoded_byte_array_data_bytes`.

Fixed-width physical types were not budgeted at all, so one huge `Array(UInt64)` record still
reached `encoder->Put` whole. They are now budgeted by `sizeof(c_type)`; a batch that cannot reach
the budget skips the walk, so ordinary columns pay nothing.

Both changes are ported from ClickHouse#120538.

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.

2 participants