Repository navigation
Reduce peak memory during JSON advanced shared data merges - #109636
Conversation
… materialization During ADVANCED serialization of JSON shared data, `flattenAndBucketSharedDataPaths` materialized ColumnDynamic columns for ALL paths across ALL buckets simultaneously. With `Array(JSON)` containing many unique paths, this caused 53-75 GiB peak memory on merges of small parts (454 rows, ~28 MiB). Replace with `flattenSharedDataPathsForBucket` which processes one bucket at a time: only one bucket's ColumnDynamic columns are alive at any point, reducing peak memory by ~num_buckets. Path names are saved separately (as lightweight strings) for the copy section. Remove now-unused `flattenAndBucketSharedDataPaths`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
…tting Introduce a new `advanced_chunked` serialization version (v3) for JSON shared data that splits large row ranges into smaller chunks during serialization. This bounds peak memory during merges of JSON columns with many unique paths, as only one chunk of data needs to be materialized at a time. Key changes: - New `ADVANCED_CHUNKED` serialization version with separate Compact (stream-then-chunks) and Wide (chunk-then-streams) layouts - New `object_shared_data_target_chunk_rows` MergeTree setting (default 8192) controlling chunk size, with small-last-chunk merging - New `SerializationArray::serializeOffsetsBinaryBulk` overload taking a raw `WriteBuffer &` to avoid extra mark writes in Compact parts - `string_view` optimization in serialization helpers to avoid copying path strings - `scanPathNamesForBucket` for lightweight path enumeration without column materialization - `advanced_chunked` set as default serialization version for 26.7 - Test randomization: `advanced_chunked` added to random choices, generic `dependent_settings` mechanism for `object_shared_data_target_chunk_rows` derived from randomized `index_granularity` - 15 new test files covering chunked serialization across compact/wide parts, various query patterns, path indexes, and regression scenarios - Documentation updates in `newjson.md` and setting doc strings Co-Authored-By: Claude Opus 4.6 <[email protected]>
…ata-merge-memory # Conflicts: # src/DataTypes/Serializations/SerializationObjectHelpers.h # src/Storages/MergeTree/MergeTreeSettings.cpp
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Don't look at the number of ilnes, most of them are tests |
|
Workflow [PR], commit [75357d3] Summary: ❌
AI ReviewSummaryAdds a new Missing context / blind spots
Findings
Final VerdictNeeds changes before merge. LLVM Coverage ReportMeasured on commit 75357d3.
Changed lines: Changed C/C++ lines covered: 838/874 (95.88%) · Uncovered code |
| { | ||
| addSettingsChanges(merge_tree_settings_changes_history, "26.7", | ||
| { | ||
| {"object_shared_data_serialization_version", "advanced", "advanced_chunked", "Enable advanced_chunked shared data serialization version by default to reduce peak memory during merges of JSON columns with many unique paths"}, |
There was a problem hiding this comment.
I am not sure I will enable it by default in this PR, most likely I will do it later in a separate PR, so this might be removed.
There was a problem hiding this comment.
can enabling it mess up rolling restarts?
There was a problem hiding this comment.
Yes, but with compatibility setting this won't be a problem. But anyway I think I will enable new chunked serialization later in a separate PR.
| for (size_t bucket = 0; bucket != buckets; ++bucket) | ||
| { | ||
| const auto & flattened_paths = flattened_paths_buckets[bucket]; | ||
| auto flattened_paths = flattenSharedDataPathsForBucket( |
There was a problem hiding this comment.
So we hash all paths for every bucket?
There was a problem hiding this comment.
Yes, that's the tradeoff here. But I believe it should not lead to any noticeable slowdown, because hashing is much faster than flattening hundreds of paths into separate columns.
| else if (serialization_version.value == SerializationVersion::ADVANCED_CHUNKED) | ||
| { | ||
| size_t end = limit && offset + limit < column.size() ? offset + limit : column.size(); | ||
| size_t total_rows = end - offset; |
There was a problem hiding this comment.
Can this be 0? Because it can mess up reads AFAIS.
There was a problem hiding this comment.
Yes it can, for example if we have empty Array(JSON). But how it can mess up reads? We could have 0 rows in granules before with the same empty Array(JSON).
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 860/899 (95.66%) · Uncovered code |
…:ClickHouse/ClickHouse into fix-json-shared-data-merge-memory
In Compact parts the writer always emits at least one chunk per granule, and an empty (0-row) granule still writes one empty chunk. The reader's `while (total_chunk_rows < rows_offset + limit)` loops read zero chunks when `rows_offset + limit == 0`, leaving that empty chunk's structure bytes unconsumed. Without substream marks (`write_marks_for_substreams_in_compact_parts = 0`) the column is read sequentially, so a trailing substream in the same column (e.g. a Tuple element after a nested `Array(JSON)`) then reads from a misaligned position and returns wrong data. Read at least one chunk (`do`/`while`) so the empty chunk is always consumed, matching the writer. Affects `advanced` and `advanced_chunked` shared-data serializations when a nested JSON column has a fully empty granule followed by more data in the same column. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The `object_shared_data_serialization_version` default change and the new `object_shared_data_target_chunk_rows` setting were recorded under the `26.7` block, but this change targets `26.8`. Move both entries to the current `26.8` MergeTree block so the settings history check passes. Co-Authored-By: Claude Opus 4.8 <[email protected]>
| /// 10001 rows → 10001 (1 < 5000, single chunk) | ||
| size_t num_chunks = total_rows / target_chunk_rows; | ||
| size_t last_chunk_remainder = total_rows % target_chunk_rows; | ||
| if (last_chunk_remainder > 0 && last_chunk_remainder >= target_chunk_rows / 2) |
There was a problem hiding this comment.
object_shared_data_target_chunk_rows is a NonZeroUInt64, so odd targets are legal here, but this threshold rounds target / 2 down. With target_chunk_rows = 3 and total_rows = 4, last_chunk_remainder == 1 still satisfies >= 1, so we emit chunks 3, 1 even though the code comment and the new setting docs promise that a trailing chunk smaller than half the target is merged into the previous chunk. That makes the last chunk smaller than the advertised minimum and increases chunk count for odd targets. Comparing against the real half (last_chunk_remainder * 2 >= target_chunk_rows or >= (target_chunk_rows + 1) / 2) would keep the documented behavior.
…on-shared-data-merge-memory
| select 'select json.arr[].arr1, json.arr[].arr99'; | ||
| select json.arr[].arr1, json.arr[].arr99 from test_compact_advanced; | ||
| select 'select json.arr[].arr99, json.arr[].arr1'; | ||
| select json.arr[].arr4, json.arr[].arr1 from test_compact_advanced; |
There was a problem hiding this comment.
This line still executes json.arr[].arr4, json.arr[].arr1, even though the label above says this case is supposed to cover json.arr[].arr99, json.arr[].arr1. That means the compact-part suite never actually exercises the “missing subcolumn first, existing subcolumn second” order that the wide tests cover, so a compact-only regression in that branch would still pass here. The same copy-paste typo is duplicated in 04415_json_shared_data_advanced_chunked_serialization_compact_part_big.sql:64.
|
Dear @antonio2368, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
…on-shared-data-merge-memory
…on-shared-data-merge-memory
Inserting a row with 65535/65536 paths cost ~1.4s and the merge that rewrites it ~1.6s, which is the entire runtime of these tests. That is fine in a normal job (7.1s) but the flaky check runs ~nproc-1 concurrent copies of each new test against one server, inflating a run ~78x, so both tests sat at 511-600s and hit the 600s per-test timeout. Keep the UInt8/UInt16 path-index widths (255 and 256 paths); the UInt16->UInt32 switch at 65536 is dropped - it was verified working and was the whole cost. Both tests now run in ~0.2s. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109636&sha=d8a692ddd76d09682442c1841160ac9572975bda&name_0=PR&name_1=Stateless+tests+%28amd_debug%2C+flaky+check%29 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`serializeChunkBucketData` takes `WriteBuffer & data_stream`, but two `next` calls were left as pointer dereferences, so `dbms` did not compile and every build job failed or was dropped. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=109636&sha=cd9532c89c749ad3129359eae0165940df285897&name_0=PR&name_1=Fast%20test PR: #109636 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Conflict in `SerializationObjectSharedData.cpp`: master sized compressed blocks in the advanced JSON shared data writer by `min_compress_block_size` (#86c03889b45), in the inline ADVANCED block that this branch moved into `serializeChunkBucketData`. Kept the helper and ported the feature into it, so both `advanced` and `advanced_chunked` inherit the setting and only start a new block at a substream or path boundary once the current block reached `min_compress_block_size`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
@Avogar is it possible to backport it into 26.8lts? |
|
Hey @Avogar, is this going to be backported to v26.8 LTS? |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reduce peak memory during merges of JSON columns with many unique paths in advanced shared data by materializing columns per-bucket instead of all at once, and by introducing a new
advanced_chunkedshared data serialization that splits row ranges into smaller chunks so only one flattened chunk needs to be in memory at a time. The chunk size is controlled by the newobject_shared_data_target_chunk_rowsMergeTree setting. Closes #108992Workflow [PR]
Sync PR [sync-upstream/pr/109636]