query_arrow_stream method of ClickHouse Connect returns a PyArrow RecordBatch on each iteration.
The client doesn’t choose the size of these batches: ClickHouse writes one record batch for every block of the query result, so small blocks give small chunks.
To get larger chunks, pass output_format_arrow_record_batch_size in the settings of the query. It makes the server combine small blocks into batches of about that many rows. The setting exists since ClickHouse 26.9 (and in 26.8 starting from 26.8.7). On older servers, raise the settings that limit the size of the blocks, described below.
Example
Create a table with a wideString column:
ArrowStream output of ClickHouse 26.12 for this table (the last batch of a result can be smaller):
Combine blocks in the Arrow output
output_format_arrow_record_batch_size sets a target number of rows per record batch and output_format_arrow_record_batch_size_bytes a target size in bytes. Both are 0 by default, which means one record batch per block. With a target:
- Blocks are collected until one of the targets is reached, and then written as one batch.
- The targets never split a block. With a row target, a combined batch holds fewer than twice that many rows.
- A batch is split only by
output_format_arrow_max_record_batch_size_bytes, a limit on theString, binary andArraydata of one batch. Its default,2147483646, is the largest buffer that Arrow can address, so it matters only for batches with about 2 GiB of such data, or when you lower it. - The server buffers the blocks, so it uses more memory and the first batch arrives later. A result that never reaches the target arrives as one batch when the query finishes.
WHERE leaves a few rows in every block, as in the last two rows of the table above: the block size settings below don’t merge such blocks.
Settings that limit the size of a block
For a query that reads aMergeTree table:
max_block_size(default65409) is the maximum number of rows in a block.preferred_block_size_bytes(default1000000) lowers the number of rows when the rows are wide, but not below one granule. That is why the example returns batches of 8192 rows by default. Set it to0to turn this limit off.- A filter removes rows from every block, and the small blocks that remain aren’t merged before the output.
s3, url or file table function, the Parquet reader has its own settings, and max_block_size isn’t used:
input_format_parquet_max_block_size(default65409) is the maximum number of rows.input_format_parquet_prefer_block_bytes(default16744704) is the preferred size of a block in bytes. On a server with a memory limit, ClickHouse also caps it at a small fraction of that limit, so very large values have no further effect.- A block never contains rows of more than one row group of the file.
Things to keep in mind
- Pass the settings in the
settingsargument of the query method or ofget_client, see Settings argument. The user must be allowed to change them: for a setting that the server marks as read-only for the user, for example withreadonly = 1, ClickHouse Connect raisesProgrammingErrorunless itsinvalid_setting_actionoption is set todroporsend, see Additional options. query_df_arrow_streamconverts the same Arrow batches to DataFrames, so the same settings control the size of its chunks.- The size of a block isn’t a guarantee. Treat the settings as limits and targets, and write the application so that it accepts batches of any size.
- Larger batches need more memory on the server and in the client for every batch.