Skip to content

Implement new input output format Buffers - #91156

Merged
nihalzp merged 42 commits into
ClickHouse:masterfrom
nihalzp:buffers-input-output-format
Dec 13, 2025
Merged

nihalzp merged 42 commits into
ClickHouse:masterfrom
nihalzp:buffers-input-output-format

Conversation

@nihalzp

@nihalzp nihalzp commented Nov 29, 2025 •

Copy link
Copy Markdown
Member

Write to a file:

SELECT
    number AS num,
    number * number AS num_square
FROM numbers(10)
INTO OUTFILE 'squares.buffers'
FORMAT Buffers;

Read back with an explicit column types:

SELECT
    count() AS cnt,
    sum(col_1) AS sum_col_1,
    sum(col_2)  AS sum_col_2
FROM file(
    'squares.buffers',
    'Buffers',
    'col_1 UInt64, col_2 UInt64'
);
  ┌─cnt─┬─sum_col_1─┬─sum_col_2─┐
  │  10 │        45 │       285 │
  └─────┴───────────┴───────────┘

If you have a table with same column types, you can populate it directly:

CREATE TABLE number_squares
(
    a UInt64,
    b UInt64
) ENGINE = Memory;

INSERT INTO number_squares
FROM INFILE 'squares.buffers'
FORMAT Buffers;

Inspect the table:

SELECT * FROM number_squares;
  ┌─a─┬──b─┐
  │ 0 │  0 │
  │ 1 │  1 │
  │ 2 │  4 │
  │ 3 │  9 │
  │ 4 │ 16 │
  │ 5 │ 25 │
  │ 6 │ 36 │
  │ 7 │ 49 │
  │ 8 │ 64 │
  │ 9 │ 81 │
  └───┴────┘

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Implement new input output format Buffers. This format is similar to Native; however, unlike Native, it does not store column names, column types, or any extra metadata. Closes #84017.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@clickhouse-gh

clickhouse-gh Bot commented Nov 29, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [007e253]

Summary: ❌

job_name test_name status info comment
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting in Function_not: the query: FAIL cidb, issue

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Nov 29, 2025
@nihalzp

nihalzp commented Nov 29, 2025

Copy link
Copy Markdown
Member Author

@alexey-milovidov Let me know what you think.

I have used ISerialization::serializeBinary() instead of ISerialization::serializeBinaryBulkWithMultipleStreams() so that the bytes of the each row come one after another.

@Avogar Avogar self-assigned this Nov 29, 2025
@Avogar

Avogar commented Nov 29, 2025

Copy link
Copy Markdown
Member

I have used ISerialization::serializeBinary() instead of ISerialization::serializeBinaryBulkWithMultipleStreams() so that the bytes of the each row come one after another.

If this format is intended for data exchange between ClickHouse servers, it's better to use serializeBinaryBulkWithMultipleStreams to serialize the data in columnar format, it will be much more efficient both for serilaization and deserialization

@Avogar

Avogar commented Nov 29, 2025

Copy link
Copy Markdown
Member

Also, it will be more efficient if the structure of the block will be the following:

  • number of buffers
  • size of the first buffer
  • first buffer data
  • ...
  • size of the last buffer
  • last buffer data

In this case we won't need to keep buffers for all columns in memory at once

Or even better, without buffers sizes at all, for deserialization it's enough to know the number of rows in the block:

  • number of buffers
  • number of rows
  • all buffers data one by one

And no need for storing buffers in memory at all.

But again, I don't fully understand the purpose of this data format, @alexey-milovidov

@alexey-milovidov

alexey-milovidov commented Nov 29, 2025 •

Copy link
Copy Markdown
Member

@Avogar, primarily, it is for efficient data exchange between ClickHouse and WebAssembly functions.

We should use serializeBinaryBulkWithMultipleStreams, as it is more natural and efficient for this use case.

number of buffers
size of the first buffer
first buffer data
...
size of the last buffer
last buffer data

I agree, this is better. Slightly less convenient for the processing code, but it makes sense to lower memory consumption when writing to a file or network. For most cases, the difference does not matter, as it will be an in-memory data exchange.

@nihalzp
nihalzp requested a review from Avogar December 4, 2025 10:01
Comment thread docs/en/interfaces/formats/Buffers.md Resolved
Comment thread src/Formats/BuffersReader.cpp Resolved
Comment thread src/Formats/BuffersReader.cpp Resolved
Comment thread src/Formats/BuffersReader.cpp Resolved
Comment thread src/Formats/BuffersReader.cpp Resolved
Comment thread src/Formats/BuffersReader.cpp Resolved
Comment thread src/Formats/BuffersReader.cpp Resolved
Comment thread src/Formats/BuffersWriter.h Resolved
@nihalzp
nihalzp requested a review from Avogar December 6, 2025 04:36
Comment thread src/Formats/BuffersWriter.cpp Resolved
@nihalzp
nihalzp requested a review from Avogar December 12, 2025 14:48

@Avogar Avogar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks great!

@nihalzp
nihalzp added this pull request to the merge queue Dec 13, 2025
Merged via the queue into ClickHouse:master with commit f67a42a Dec 13, 2025
@nihalzp
nihalzp deleted the buffers-input-output-format branch December 13, 2025 03:38
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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.

A new input/output format, Buffers

4 participants