Skip to content

Cut Iceberg data files by their compressed size - #127670

Merged
alexey-milovidov merged 1 commit into
masterfrom
iceberg-cut-files-by-compressed-size
Oct 24, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
iceberg-cut-files-by-compressed-size

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

MultipleFileWriter compared iceberg_insert_max_bytes_in_data_file and iceberg_compaction_max_bytes_in_data_file against the uncompressed size of the chunks written to the current file, while the compaction selects its input files by their compressed size (file_size_in_bytes) below iceberg_data_file_size_lower_threshold_compaction. When the compression ratio of the data is above max_bytes / lower_threshold (1.33 with the defaults, so almost any real data), every output file of the compaction stays below the lower threshold, is selected again, and the compaction rewrites the same data forever without making the files larger.

Now the limit is compared against the bytes written to the current file so far (buffer->count()), the same measure as file_size_in_bytes and the meaning of the Iceberg property write.target-file-size-bytes. A file can exceed the limit by what the output format still buffers (incomplete Parquet row groups and the ones being encoded), which keeps it above the lower threshold, so it is not selected again. The same writer is used by INSERT, so inserted and compacted files are measured the same way.

Closes: https://github.com/ClickHouse/clickhouse-private/issues/83527

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

The settings iceberg_insert_max_bytes_in_data_file and iceberg_compaction_max_bytes_in_data_file now limit the compressed size of a data file, as written to the storage, instead of the uncompressed size of the data. This is consistent with the Iceberg property write.target-file-size-bytes and with iceberg_data_file_size_lower_threshold_compaction, and fixes the Iceberg compaction endlessly rewriting files that compress well.

🤖 Generated with Claude Code


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

Version info

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

`MultipleFileWriter` compared `iceberg_insert_max_bytes_in_data_file` and
`iceberg_compaction_max_bytes_in_data_file` against the uncompressed size of the
chunks written to the current file, while the compaction selects its input files
by their compressed size (`file_size_in_bytes`). When the compression ratio of the
data is above `max_bytes / lower_threshold` (1.33 with the defaults), every output
file of the compaction stays below `iceberg_data_file_size_lower_threshold_compaction`,
so the compaction selects it again and rewrites the same data forever.

Now the limit is compared against the bytes written to the current file so far,
the same measure as `file_size_in_bytes`, and the meaning of the Iceberg property
`write.target-file-size-bytes`. A file can exceed the limit by what the output
format still buffers (incomplete Parquet row groups and the ones being encoded),
which keeps it above the lower threshold.

ClickHouse/clickhouse-private#83527

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

clickhouse-gh Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7c9c375]

Summary: ✅


AI Review

Summary

The PR changes Iceberg data-file rollover to count compressed bytes and adds an insert regression test. The change addresses undersized output, but buffered Parquet row groups can instead produce oversized files that remain eligible for compaction. Request changes to establish convergence across both size thresholds.

Missing context / blind spots
  • ⚠️ The linked private issue and Cloud compaction implementation are unavailable in this checkout. A Cloud compaction regression demonstrating that the files described below are not repeatedly selected would close the remaining planner-side uncertainty.
  • ⚠️ The current-head Praktika workflow is still running; targeted stateless tests and functional bugfix validation were pending when checked. There is no local ClickHouse binary, so this review uses source traces rather than a runtime reproduction.
Findings

⚠️ Majors

  • MultipleFileWriter.cpp:91: Compressed-byte rollover does not ensure compaction convergence. The default Parquet row-group byte target and compaction file target are both 512 MiB. Two row groups encoding to 480 MiB each produce at least 960 MiB: the first leaves the byte count below the target, and the second takes the file above the approximately 921.6 MiB upper compaction threshold. Rewriting with the same boundaries need not shrink it. Parallel encoding can increase overshoot because completed groups are reaped inside ParquetBlockOutputFormat::consume, after the rollover check, and IOutputFormat::flush does not drain pending groups. Bound row-group/in-flight output relative to the compaction size window, or prevent repeated selection of unavoidable oversized output.
Tests
  • ⚠️ Add a focused compaction regression using lightly compressible data, realistic row-group sizes, and parallel encoding. Verify that completed output is not selected again solely because of its size. The new insert test uses 100-row groups, disables parallel encoding, and never exercises compaction; it cannot prove this invariant.
Final Verdict

⚠️ Request changes. Prevent repeated compaction of oversized output and add focused regression coverage for convergence with normal writer buffering.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 24, 2026
/// Comparing it against the uncompressed size makes the files smaller than the target by the compression ratio,
/// and the output of the compaction is selected by the compaction again. The file can exceed the limit
/// by what the output format has not written yet: an incomplete Parquet row group and the row groups being encoded.
if (!current_file_num_rows || *current_file_num_rows >= max_data_file_num_rows || buffer->count() >= max_data_file_num_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.

⚠️ Bound buffered output against the compaction upper threshold

This can replace the small-file rewrite loop with an oversized-file rewrite loop. With the defaults, output_format_parquet_row_group_size_bytes is 512 MiB, the target is 512 MiB, and iceberg_data_file_size_upper_threshold_compaction is about 921.6 MiB. If each row group encodes to 480 MiB, the first group leaves buffer->count() below the target, so the second is accepted and the result is at least 960 MiB: eligible for compaction again. Rewriting it with the same row-group boundaries need not reduce its size. This is possible even with synchronous encoding; with parallel encoding, completed groups are only reaped inside ParquetBlockOutputFormat::consume, after this check, and additional groups can already be queued. IOutputFormat::flush does not drain them.

Please bound row-group/in-flight output relative to the target and upper threshold, or ensure the planner does not repeatedly select these unavoidable oversized outputs. Add a focused compaction regression with lightly compressible data and normal row-group/parallel settings that verifies the resulting files are not immediately selected again. The added test forces 100-row groups and disables parallel encoding, so it does not establish convergence under the default settings.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a different matter.

@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Oct 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 7c9c3757b with master 5a33e4237 (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

@alexey-milovidov alexey-milovidov self-assigned this Oct 24, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 24, 2026
Merged via the queue into master with commit b81b4ba Oct 24, 2026
178 checks passed
@alexey-milovidov
alexey-milovidov deleted the iceberg-cut-files-by-compressed-size branch October 24, 2026 05:48
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 24, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov a measurement on this change: with default Parquet settings the size limit barely applies, because buffer->count() lags far behind the file being produced.

SET allow_experimental_insert_into_iceberg = 1, max_insert_threads = 1,
    iceberg_insert_max_rows_in_data_file = 1000000000, iceberg_insert_max_bytes_in_data_file = 2816000;
CREATE TABLE t (id Int64, s String) ENGINE = IcebergLocal('/tmp/lake/t', 'Parquet');
INSERT INTO t SELECT number, toString(number) FROM numbers(32000000);
SELECT count(), max(file_size_in_bytes) FROM system.iceberg_files WHERE table = 't' AND content = 0;

clickhouse local from master b866a0ab, Parquet settings passed as command-line options:

Parquet settings files max file
defaults 7 29,201,453 (10.4x)
bloom_filter_flush_threshold_bytes=0 9 22,691,951 (8.1x)
bloom_filter_flush_threshold_bytes=0, parallel_encoding=0 31 5,760,908 (2.05x, one 1M-row block per file)

Two things are not in buffer->count() yet:

  1. Row groups being encoded with parallel_encoding. They reach out only in reapCompletedRowGroups, and consume keeps up to about 4 * output_format_parquet_row_group_bytes (2 GiB uncompressed) in flight. This is the dominant part.
  2. Bloom filters, held in memory up to output_format_parquet_bloom_filter_flush_threshold_bytes (128 MiB) and written before the footer. With 4M rows they are 16.8 MB of a 22.6 MB file.

Which one do you prefer?

  • A. Exact: Iceberg data files write bloom filters after each row group, and MultipleFileWriter makes the Parquet format finish and write the row groups being encoded before it compares. buffer->count() is then the file size so far except an incomplete row group, which gives the last row of the table. Encoding no longer overlaps the arrival of the next chunk, and the bloom filters move next to their row groups.
  • B. Estimate: keep the pipelining and compare buffer->count() plus what the format still holds (bloom filters and encoded row groups exactly, the rest at the compression ratio of the file so far).
  • C. Keep it and document the bound in the setting descriptions.

I can make the PR for A or B.

middas-0706 pushed a commit to middas-0706/ClickHouse that referenced this pull request Nov 1, 2026
Since `MultipleFileWriter` compares `iceberg_insert_max_bytes_in_data_file` and
`iceberg_compaction_max_bytes_in_data_file` with the bytes written to the file, the limit
was ignored for files smaller than a Parquet row group: the writer writes a row group only
when it is complete, and the default `output_format_parquet_row_group_size_bytes` is
512 MiB before compression, so the size of such a file stayed near zero until it was
finished. An insert of 16 MiB with a limit of 1 MiB wrote a single file of 14 MB, and the
Iceberg compaction no longer split a file above the upper threshold. The row groups are
now not bigger than the limit.

Caused by: ClickHouse#127670

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tsenart pushed a commit to perfloop-oss/ClickHouse that referenced this pull request Nov 14, 2026
`MultipleFileWriter` created the output format with
`getOutputFormatParallelIfPossible`. For a text format such as `JSON`,
that is `ParallelFormattingOutputFormat`, whose collector thread writes
to the data file buffer while `consume` reads `buffer->count()` to cut
the file (since ClickHouse#127670):
a data race reported by TSan. Use the non-parallel output format; the
Iceberg data file formats are not formatted in parallel anyway.

La Casa Del Dolor (amd_tsan), STID 1003-46d8, master, 2026-11-13:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=c1e69ea54ad361a685f9791174c1f7d22409ad2b&name_0=MasterCI&name_1=La%20Casa%20Del%20Dolor%20%28amd_tsan%29

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... 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

Development

Successfully merging this pull request may close these issues.

3 participants