Skip to main content
The 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 wide String column:
Count the rows of every batch with and without the setting:
The following numbers of rows per batch were measured on the 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 the String, binary and Array data 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.
Use it in particular when a selective 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 a MergeTree table:
  • max_block_size (default 65409) is the maximum number of rows in a block.
  • preferred_block_size_bytes (default 1000000) 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 to 0 to turn this limit off.
  • A filter removes rows from every block, and the small blocks that remain aren’t merged before the output.
For a query that reads Parquet files with the 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 (default 65409) is the maximum number of rows.
  • input_format_parquet_prefer_block_bytes (default 16744704) 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.
Measured for a Parquet file with 1,000,000 rows in one row group, created by the first statement and read by the second:

Things to keep in mind

  • Pass the settings in the settings argument of the query method or of get_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 with readonly = 1, ClickHouse Connect raises ProgrammingError unless its invalid_setting_action option is set to drop or send, see Additional options.
  • query_df_arrow_stream converts 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.