Repository navigation
Cherry pick #119873 to 26.8: Combine small blocks into one Arrow IPC record batch on output - #120398
Merged
robot-ch-test-poll3 merged 9 commits intoSep 16, 2026
Merged
Conversation
ArrowIPCBlockOutputFormat::consume wrote exactly one record batch per incoming
chunk, and nothing squashes on the SELECT ... FORMAT path:
markOutputFormatPrefersLargeBlocks("ArrowStream") is only consulted through
IStorage::prefersLargeBlocks() on the INSERT path. A selective query therefore
emitted one small batch per scanned block, and the fixed FlatBuffers metadata
and buffer padding of each batch dominated the payload. On the reported example,
SELECT number FROM numbers(268435456) WHERE number % 65536 = 0 FORMAT
ArrowStream produced 721,040 bytes in 4,096 one-row batches; at
output_format_arrow_record_batch_size = 65409 and
output_format_arrow_record_batch_size_bytes = 1048576 it produces 16,728 bytes
in one batch, byte-identical to a hand-written single-batch encoding of the
same rows.
consume becomes a staging front end and its former body moves verbatim into
writeChunk, which is also the seam PR #117754 rewrites to split an oversized
chunk, so either merge order is a mechanical rebase and the two compose in the
right order: accumulate up to the target, then split what a single batch still
cannot address.
Sub-target chunks are appended into one accumulator built from
cloneEmptyColumns() rather than from the first chunk: FilterTransform passes
result_size_hint = -1, so ColumnVector::filter reserves the whole source block
and resize_exact does not release it, and adopting the first chunk would make
the accumulator carry that reservation for the lifetime of every batch. A chunk
that already reaches a target flushes what is staged and is then written
un-copied, which keeps a large block out of the accumulator and bounds a
combined batch below twice the target; gating that branch on an empty
accumulator instead would let a few staged rows absorb a 1,048,449-row INSERT
block.
The size is measured on the materialized chunk because ColumnConst::byteSize
reports one stored value, and materializeChunk is a precondition of appending
at all: MessageQueueSink drives this writer through IOutputFormat::write with
no MaterializingTransform in front of it, and appending into an accumulator
cloned from a ColumnConst would advance the row count without copying values.
The targets bound the accumulated data as Chunk::bytes() measures it, on the
same terms as min_insert_block_size_bytes, not retained memory: a
LowCardinality shared dictionary and an aggregate state's foreign arenas sit
outside that measure. Measured against blocks made equally large with
max_block_size instead, the accumulator adds between -1.0 and +1.7 MiB for a
1 MiB target.
The precedent and the refuted alternative, for the record:
output_format_parquet_row_group_size_bytes stages on the cruder
chunk.allocatedBytes() (ParquetBlockOutputFormat.cpp:121) and under-counts
LowCardinality the same way, because byteSize() counts the deduplicated
dictionary once while the encoder writes one value per row unless
output_format_arrow_low_cardinality_as_dictionary is set. Summing each incoming
chunk's bytes as Squashing does was rejected, not overlooked:
ColumnLowCardinality::filter keeps the whole source dictionary and filters only
the indexes (ColumnLowCardinality.h:126-130), so over the 4,096 one-row chunks
this change exists to combine, that sum over-counts by about the chunk count and
would flush at the first one. The byte setting's description states the measure
and advises pairing it with the row criterion.
Both settings default to 0, which is byte-identical to the previous behaviour,
and SettingsChangesHistory records 0 -> 0 so compatibility below 26.9 keeps it.
Closes: #119815
Co-Authored-By: Claude Opus 5 <[email protected]>
`ColumnLowCardinality::byteSize` counts one index per row plus each distinct value once, and nothing at all for the values while the dictionary is still shared with the block the column came from (ColumnLowCardinality.h:196-197, and the empty-plus-shared adoption at ColumnLowCardinality.cpp:245-250). That has two consequences for `output_format_arrow_record_batch_size_bytes` and only one of them was documented: - a batch of repeated values encodes to more than the target, because the record batch writes one value per row; - a block filtered out of a larger one keeps that block's dictionary, so it can reach the target on its own. Measured on a four-block fixture with one surviving row per block: the byte target ends every batch at that single row and combines nothing, and since whichever target is reached first ends a batch, it also ends the batches a paired row target would have combined. This is the accounting of `min_insert_block_size_bytes`, which the setting documents itself against: `Squashing::oneMinReached` uses the same `Chunk::bytes` measure, the same per-chunk pre-check and the same disjunction of the two criteria (Squashing.cpp:395-411), and it produces identical block counts on that fixture in all three configurations. The behaviour is therefore unchanged; the setting now states what it counts, and 05211 pins both halves with a `String` control and the row-target remedy beside them. The `staged` comment no longer claims the accumulator holds none of the source block's allocations, which the shared-dictionary path would falsify.
`consume` cloned empty columns and appended the first chunk of every batch into them; it now takes that chunk itself and appends only the chunks that follow. Neither reason for the copy holds: the capacity a filtered column retains is one source block's worth, and `Chunk::bytes` sums `byteSize`, not `allocatedBytes`, so it never reaches the flush criterion; where an inherited size does end a batch early, the result is one record batch per block, which is the previous behaviour and what the early-out above already accepts for a `LowCardinality` block keeping its source dictionary. The output is unchanged. 18 fixtures covering both targets, the dictionary-encoded path, filtered, nested and table-backed `LowCardinality` (including a wide part written with one dictionary for the whole part) and the `Arrow` file footer produce byte-identical payloads before and after, with the same batch boundaries. Appending into the adopted chunk cannot write through to another holder, because `Chunk::append` mutates through `IColumn::mutate`, which clones a column that is still shared. Checked on the reachable shape rather than by reading: a `Memory` table, whose source hands the chunk the stored block's own columns, is unchanged after its rows are dumped as one combined batch, and a second dump is identical to the first. On a high-cardinality `LowCardinality(String)` result with blocks one row under the row target, where the dropped copy is a per-row dictionary remap rather than a memcpy, 4M rows take 0.38 s instead of 0.43 s (best of 7 alternating runs, debug build). On a wide `String` result the difference is below the noise floor.
Describe buffering, streaming latency, and `LowCardinality` size caveats. Carried over unchanged from EmeraldShift/ClickHouse@88d7dbf, requested in #119873 (comment). Every statement was checked against the shipped behaviour before carrying it. Co-authored-by: Michael Jarrett <[email protected]>
The repository asks for `f` rather than `f()` where the text names a function itself instead of applying it (.claude/CLAUDE.md). `numbers`, `numbers_mt` and `ColumnLowCardinality::byteSize` are named, not called, in these three comments. Co-Authored-By: Claude Opus 5 <[email protected]>
…-batch-coalescing-119815 # Conflicts: # src/Core/SettingsChangesHistory.cpp
…alescing-119815 Combine small blocks into one Arrow IPC record batch on output
The only conflict is `src/Core/SettingsChangesHistory.cpp`: master records
the two new settings under `26.9`, a block that does not exist on this
release branch. Drop the incoming block and record
output_format_arrow_record_batch_size
output_format_arrow_record_batch_size_bytes
under `26.8` instead, the branch's current version block, which is what
`check_settings_changes_history` in ci/jobs/check_style.py requires.
Everything else applies unchanged: the resulting diff against the release
branch is the original change, entry for entry.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #119873
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job