Repository navigation
Support binary for aggregate_function_input_format, unify implementation - #91358
GrigoryPervakov wants to merge 87 commits into
Conversation
|
Workflow [PR], commit [15a9bb7] Summary: ✅
AI ReviewSummaryThis PR extends Missing context / blind spots
Final Verdict✅ Approve from code review perspective; no new inline findings. LLVM Coverage ReportMeasured on commit 15a9bb7.
Changed lines: Changed C/C++ lines covered: 345/365 (94.52%) · Uncovered code |
There was a problem hiding this comment.
Pull request overview
This PR refactors the implementation of aggregate_function_input_format to support binary formats (e.g., RowBinary) and unifies the deserialization logic across different input formats.
Key Changes:
- Added support for binary format deserialization in addition to text-based formats
- Unified the deserialization implementation using a template-based approach (
deserializeFromValues) - Converted the bash-based test (
.sh) to a SQL-based test (.sql) for better maintainability
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/queries/0_stateless/03645_aggregate_function_input_format.sql | New SQL test file with comprehensive test cases for aggregate function input formats including binary formats |
| tests/queries/0_stateless/03645_aggregate_function_input_format.sh | Removed bash test script (replaced by .sql file) |
| tests/queries/0_stateless/03645_aggregate_function_input_format.reference | Updated reference output to include new binary format test results |
| src/DataTypes/Serializations/SerializationAggregateFunction.h | Removed obsolete deserializeBasedOnInput method declaration |
| src/DataTypes/Serializations/SerializationAggregateFunction.cpp | Major refactoring: unified deserialization logic, added binary format support, introduced template-based approach |
51eca8b to
cf9882d
Compare
cf9882d to
4632266
Compare
4632266 to
396ee5c
Compare
396ee5c to
0d6411e
Compare
…-insert-format # Conflicts: # src/Core/Settings.cpp # src/DataTypes/Serializations/SerializationAggregateFunction.cpp
…inputs The refactor in 0d6411e dropped the check that `function->deserialize` consumed the entire bounded buffer when reading a state from a string-based path. As a result, garbage suffixes were silently accepted for `Escaped`/`Quoted`/`WholeText`/`JSON`/`CSV` state inputs, regressing tests that expect `BAD_ARGUMENTS` for malformed states (`02477_single_value_data_string_regression`, `03716_topk_bad_data`, `04027_redundant_trailing_bytes_aggregate_function_deserialization`, `04027_reverseUTF8_invalid_utf8`, `04048_sumcountif_nullable_backward_compat`). Add an opt-in `check_buffer_consumed` parameter to the shared `deserializeFromString` helper. The text-format callers — which read a single value into a `ReadBufferFromString` — pass `true`, restoring the master-branch behavior of throwing `BAD_ARGUMENTS` on trailing bytes. The binary per-row path keeps the default (`false`) so streaming deserialization of subsequent rows is preserved. CI failures: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=91358&sha=a7a14a69d127be5290c774b24618c22e45097fca&name_0=PR&name_1=Fast%20test PR: #91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
In `SerializationAggregateFunction::deserializeBinary(Field &, ...)` for `value`/`array` modes, the temporary `place` is created via `function->create(place)` and used only to build `s.data` via `function->serialize`. On the success path the matching `function->destroy(place)` was missing, so aggregate functions whose state holds non-trivial resources (e.g. containers, external allocations) leaked those resources for every row parsed in `value`/`array` modes. PR: #91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`createStateFromValues` previously derived the row count from `arg_columns[0]->size()`. For zero-argument aggregate functions like `AggregateFunction(count)`, `columns_ptrs` is empty in `value`/`array` modes, so `arg_columns[0]` is invalid and the server segfaulted. Pass the row count explicitly: `1` for value mode, the parsed array length for array mode. Also skip building tuple-component pointers when there are zero arguments, so the empty-tuple state for `count` doesn't try to dereference a non-existent ColumnTuple. PR: #91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`RowBinary` does not support schema inference, so `INSERT ... SELECT * FROM file(..., RowBinary)` was unstable in the flaky check: with some random settings the schema was inferred from the destination table (test passed), with others schema inference was attempted on the file and failed with `RowBinary file format doesn't support schema inference`. Pass the structure explicitly to every `file(..., RowBinary)` read in Tests 19-22 so the test no longer depends on which schema inference path is taken. Also add Tests 23-24 covering zero-argument `AggregateFunction(count)` for both `value` and `array` modes (previously caused a segfault, fixed in the preceding commit). CI failures: - https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=91358&sha=3bcfba683502caccd778999a3e21ceb223030b18&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29 - https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=91358&sha=3bcfba683502caccd778999a3e21ceb223030b18&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%201%2F2%29 PR: #91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…-insert-format # Conflicts: # src/DataTypes/Serializations/SerializationAggregateFunction.cpp
…nd a custom CSV null representation in `array` mode Two backward-compatibility gaps in the unified `aggregate_function_input_format` parsing, found by the review: 1. A single `Tuple` argument in `value` mode. Released read the whole field as a string and parsed it with the tuple's `deserializeTextCSV`, i.e. the flattened CSV form of the tuple (`"2,foo"` in `CSV`, `2,foo` in `TabSeparated`, `TSVRaw`, the quoted `VALUES` form and the string-wrapped `JSONEachRow` form for `AggregateFunction(any, Tuple(UInt64, String))`). The unified path handed the tuple to the source format's tuple parser: in `CSV` it consumed one outer cell per element and swallowed the following columns (`1,2,foo,9` was accepted as `x = (2, 'foo'), b = 9`), and the whole-field entrypoints rejected the flattened form. Now the field stays bounded and its content is parsed as released did (`useLegacyTupleValueParsing`); the native `(2,'foo')` form is accepted too when the content opens with a parenthesis. `Nullable(Tuple)` is unaffected: its released form was already the bounded native one. 2. The `N`/`n` dispatch in single-argument `array` mode sent every such bareword to the quoted serialization, which only knows the `NULL` keyword, so a custom `format_csv_null_representation` such as `Nothing` (`[Nothing,1]` built `[NULL,1]` on released) threw. Only the exact `NULL` keyword takes the quoted parse now; every other bareword keeps the released CSV element parse, which recognizes the representation and `NaN`. Both verified against released `26.7.1`.
…d a custom `format_csv_null_representation` in `array` mode
|
🕵 Status (2026-09-06):
|
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=91358&sha=8640f3397185cc700ffa06b44cfc01acd0aa02b7&name_0=PR&name_1=AST%20fuzzer%20(amd_release,%20oracle) and provide a fix in a separate PR. If the fix is already in progress, link it here. It is a TLP |
|
Acknowledged. I investigated: this is an oracle false positive, and it is unrelated to this PR. The carrier is CREATE OR REPLACE VIEW tmp AS SELECT initialQueryStartTime() AS it, now() AS t FROM system.one WHERE NOT ignore(sleep(0.5));So The gate gap: I will follow up here with a fix PR. |
|
Fix PR: #118484 It screens the definitions a read evaluates, not only the query text: Run under the oracle, Nothing is needed on this PR. |
In `value` mode `deserializeFromValues` builds a temporary tuple of the argument types for every argument count except one, so a zero-argument aggregate such as `AggregateFunction(count)` gets the empty tuple. The guard that disables `input_format_csv_deserialize_separate_columns_into_tuple` for the temporary tuple only covered more than one argument, so the empty tuple still went through the separate-columns branch of `SerializationTuple::deserializeTextCSV`, which adds a row without consuming any bytes. A `CSV` row `1,(),9` for `a UInt8, x AggregateFunction(count), b UInt8` therefore failed with `CANNOT_PARSE_INPUT_ASSERTION_FAILED`, because the `()` cell was left to the following column, while released `26.7.1` pre-read one bounded field and parsed it with the tuple's `deserializeWholeText`. Widen the guard to every argument count that builds a temporary tuple. Verified against released `26.7.1`: `1,(),9` and `1,"()",9` now give one counted row again, an empty field stays the default state, and a field that is not the empty tuple is rejected instead of spilling into the next column. The other formats were already field-local for the empty tuple (`TSV`, `TSVRaw`, `JSONEachRow`, `Values` all match released), and `RowBinary` needs no bytes for it, exactly like a plain `Tuple()` column. New test `05136_aggregate_function_input_format_zero_argument_value`.
|
🕵 Status (2026-09-07):
|
`serializeBinary(Field, ...)` always writes the length-prefixed opaque state, but `deserializeBinary(Field, ...)` read the same bytes as a `value` / `array` payload whenever `aggregate_function_input_format` was not `state`, so the two stopped being inverses of each other. The `Field` overloads are the representation of an `AggregateFunction` value nested inside another value: the argument of `argMax`, the value of a `sumMap`, or a `JSON` path that does not fit into the dynamic paths and goes to the shared data. Such a carrier only ever reads back what `serializeBinary(Field, ...)` wrote, and it may keep the consumed bytes as they are, so reading the length prefix as an argument value corrupts both the value and everything that follows it in the row: for `AggregateFunction(any, UInt64)` the value branch consumed the length prefix plus seven bytes of the state as the `UInt64` argument and left the rest behind. Read the length-prefixed state unconditionally, exactly as before this change set, and keep the `value` / `array` forms on the `IColumn` path, which is what a binary input format reads a column with, including a column nested inside a `Tuple` or a `Dynamic`. New test `05137_aggregate_function_input_format_binary_field_carrier`.
|
🕵 Pass update. CI on New test Remaining: fresh CI on |
…inputs The refactor in 0d6411e dropped the check that `function->deserialize` consumed the entire bounded buffer when reading a state from a string-based path. As a result, garbage suffixes were silently accepted for `Escaped`/`Quoted`/`WholeText`/`JSON`/`CSV` state inputs, regressing tests that expect `BAD_ARGUMENTS` for malformed states (`02477_single_value_data_string_regression`, `03716_topk_bad_data`, `04027_redundant_trailing_bytes_aggregate_function_deserialization`, `04027_reverseUTF8_invalid_utf8`, `04048_sumcountif_nullable_backward_compat`). Add an opt-in `check_buffer_consumed` parameter to the shared `deserializeFromString` helper. The text-format callers — which read a single value into a `ReadBufferFromString` — pass `true`, restoring the master-branch behavior of throwing `BAD_ARGUMENTS` on trailing bytes. The binary per-row path keeps the default (`false`) so streaming deserialization of subsequent rows is preserved. CI failures: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=91358&sha=a7a14a69d127be5290c774b24618c22e45097fca&name_0=PR&name_1=Fast%20test PR: ClickHouse#91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
In `SerializationAggregateFunction::deserializeBinary(Field &, ...)` for `value`/`array` modes, the temporary `place` is created via `function->create(place)` and used only to build `s.data` via `function->serialize`. On the success path the matching `function->destroy(place)` was missing, so aggregate functions whose state holds non-trivial resources (e.g. containers, external allocations) leaked those resources for every row parsed in `value`/`array` modes. PR: ClickHouse#91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`createStateFromValues` previously derived the row count from `arg_columns[0]->size()`. For zero-argument aggregate functions like `AggregateFunction(count)`, `columns_ptrs` is empty in `value`/`array` modes, so `arg_columns[0]` is invalid and the server segfaulted. Pass the row count explicitly: `1` for value mode, the parsed array length for array mode. Also skip building tuple-component pointers when there are zero arguments, so the empty-tuple state for `count` doesn't try to dereference a non-existent ColumnTuple. PR: ClickHouse#91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`RowBinary` does not support schema inference, so `INSERT ... SELECT * FROM file(..., RowBinary)` was unstable in the flaky check: with some random settings the schema was inferred from the destination table (test passed), with others schema inference was attempted on the file and failed with `RowBinary file format doesn't support schema inference`. Pass the structure explicitly to every `file(..., RowBinary)` read in Tests 19-22 so the test no longer depends on which schema inference path is taken. Also add Tests 23-24 covering zero-argument `AggregateFunction(count)` for both `value` and `array` modes (previously caused a segfault, fixed in the preceding commit). CI failures: - https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=91358&sha=3bcfba683502caccd778999a3e21ceb223030b18&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29 - https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=91358&sha=3bcfba683502caccd778999a3e21ceb223030b18&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%201%2F2%29 PR: ClickHouse#91358 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…ion` `SerializationAggregateFunction::deserializeBinary(Field &, ...)` is the exact inverse of `serializeBinary(Field &, ...)` and stays on the length-prefixed opaque state regardless of `aggregate_function_input_format`, because a carrier such as the shared data of a `JSON` column keeps the consumed bytes as they are and reads them back with the same representation. The `value` and `array` forms therefore only apply to an `AggregateFunction` cell that the format reads as a column. Say so in the `RowBinary` documentation (source of truth: the doc string in `BinaryRowInputFormat.cpp`) and in the description of the setting, instead of advertising blanket `RowBinary` support for every `AggregateFunction` value. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Pass update (2026-09-12), head
No unresolved review threads. Waiting on fresh CI and the human merge. |
|
I thought about a different implementation: We support deserialization of aggregate function states in the We implement it in the following way: - ask the data format to deserialize a different data structure (header), where instead of We add a transformation on top, which constructs an In this way, it will be actually unified, as I imagined in the original design. |
…and a transformation
Instead of implementing the `value` and `array` modes inside `SerializationAggregateFunction`
for every text escaping rule, `FormatFactory::getInput` asks the input format to parse a header
in which every `AggregateFunction(f, T...)` is replaced with the type of the values `f` aggregates
(`T`, or `Tuple(T...)`, or `Array` of it), and wraps the format into
`AggregateFunctionStatesFromValuesInputFormat`, which builds the states from the parsed values
and outputs chunks of the original header. The replacement is recursive inside `Array`, `Tuple` and `Map`.
This makes the setting work uniformly in every input format, including `RowBinary`, `Native`
and `Parquet`, with the parallel parsing, asynchronous inserts and defaults for omitted fields.
`SerializationAggregateFunction` reads only states again.
The values are written as the format writes the argument type: in the JSON formats the value
is a JSON value (`{"x": [1, 2, 3]}`), not a string containing it.
As proposed in ClickHouse#91358 (comment)
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Addresses issues in #88088
Support the
RowBinaryfamily of input formats foraggregate_function_input_format = 'value'andaggregate_function_input_format = 'array', and unify the value/array deserialization across all input formats (text and binary) behind a single implementation that deserializes into a temporary column of the argument type (orArrayof it) and builds theAggregateFunctionstate viaaddBatchSinglePlace.The setting
aggregate_function_input_formatremains production-tier: it was already released as such inv25.12andv26.1, so this PR does not change its feature tier and is not a backward-incompatible change.Related: #88088
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Support
aggregate_function_input_format = 'value'andaggregate_function_input_format = 'array'for binary input formats such asRowBinarywhen inserting intoAggregateFunctioncolumns, and unify the deserialization implementation across input formats.Documentation entry for user-facing changes
Workflow [PR]
Sync PR [sync-upstream/pr/91358]