Repository navigation
Evaluate the PARTITION BY expression of file and object storage writes once per INSERT, not once per block - #128217
Conversation
…s once per INSERT Since ClickHouse#92844 the partition-key actions of a non-deterministic expression were rebuilt for every chunk, so a stateful function such as `rowNumberInAllBlocks()` restarted its counter at 0 on every chunk and `now64()` was re-captured per chunk: `INSERT INTO FUNCTION file(...) PARTITION BY intDiv(rowNumberInAllBlocks(), N)` put every row of 1000-row chunks into partition 0 (regression in 26.1). Before ClickHouse#92844 the actions were built once in the strategy constructor, which is per INSERT for table functions but per TABLE for object-storage engines, so `now64()` was frozen for the table's lifetime. The right lifetime for both is one INSERT: `PartitionedSink` now gets the actions once from the strategy (`getPartitionKeyActions`) and applies them to all its chunks. Deterministic actions stay cached in the strategy; a non-deterministic expression gets fresh actions per sink, i.e. per INSERT, so concurrent INSERTs into one engine table do not share a counter. None of the partitioned storages supports parallel insert, so an INSERT has exactly one `PartitionedSink`. Closes ClickHouse#128211 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reword the getPartitionKeyActions contract: the actions are rebuilt per call, not the function evaluated once per INSERT. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding; 1 full pass).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261030-111200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261030-102000 |
|
cc @mstetsyuk @kssenii, could you review this and add the |
|
Workflow [PR], commit [5b8153d] Summary: ✅
AI ReviewSummaryPR #128217 fixes partitioned writes through Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units177 translation units recompiled, 1749 s compile time in total, 177 of them have a recent master baseline. |
|
@mstetsyuk can you review this? It seems to fix a regression from #92844 |
| virtual ~IPartitionStrategy() = default; | ||
|
|
||
| virtual ColumnPtr computePartitionKey(const Chunk & chunk) const = 0; | ||
| /// Rebuilt on every call if non-deterministic. A sink calls it once and reuses the result for all its chunks, |
There was a problem hiding this comment.
Yes, this is reasonable.
And about non-determinism... This means a partition key that depends on a dictionary that could change. So it's not like randomness.
690d3be
|
A user on #128211 asks for this fix in 26.8. The regression came with #92844 (first released in 26.1), so all supported release branches have it: 26.3, 26.8, 26.9 and 26.10, and this fix is in none of them. Cherry-pick check against the current branch heads: clean on 26.10. On 26.8 and 26.9, one hunk in A backport needs |
Stable 26.8 Backport of ClickHouse#128217 - Evaluate the `PARTITION BY` expression of file and object storage writes once per INSERT, not once per block
Antalya 26.6 Backport of ClickHouse#128217 - Evaluate the `PARTITION BY` expression of file and object storage writes once per INSERT, not once per block
Antalya 26.8 Backport of ClickHouse#128217 - Evaluate the `PARTITION BY` expression of file and object storage writes once per INSERT, not once per block
Stable 26.3 Backport of ClickHouse#128217 - Evaluate the `PARTITION BY` expression of file and object storage writes once per INSERT, not once per block
Antalya 26.3 Backport of ClickHouse#128217 - Evaluate the `PARTITION BY` expression of file and object storage writes once per INSERT, not once per block
…tateful-function-spans-chunks Evaluate the `PARTITION BY` expression of file and object storage writes once per INSERT, not once per block Conflicts committed as-is; the next commit resolves them: - src/Storages/IPartitionStrategy.cpp (both modified) - src/Storages/IPartitionStrategy.h (both modified)
Kept antalya's `computePartitionKey(Block&)` overrides, added the new `getPartitionKeyActions()` overrides, and left out upstream's `getPathForRead`/`getPathForWrite` context lines.
Closes: #128211
Related: #92844
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
PARTITION BYoffile,urland object storage writes (table functions and table engines) evaluating stateful and non-deterministic functions anew for every block since 26.1:PARTITION BY intDiv(rowNumberInAllBlocks(), N)restarted its counter on every block and wrote rows into the wrong files, andnow64()produced one file per block instead of one perINSERT. Closes #128211.Description
On 26.3 and 26.8,
INSERT INTO FUNCTION file('bad_{_partition_id}.parquet') PARTITION BY intDiv(rowNumberInAllBlocks(), 1000) SELECT number FROM numbers(5000) SETTINGS max_block_size = 1000, max_insert_threads = 1writes all 5000 rows intobad_0.parquetinstead of five 1000-row files (correct on 25.3, 25.8 and 25.12, wrong since 26.1).Root cause: #92844 stopped caching the partition-key actions of a non-deterministic expression, so that
now64()is not frozen for the lifetime of anS3table, and rebuilt them for every chunk instead. A freshrowNumberInAllBlocksthen starts at 0 on every chunk, andnow64()is re-captured per chunk.Change:
PartitionedSink, the only caller and the one object that lives exactly one INSERT forfile,urland object storage, gets the actions once from the strategy (getPartitionKeyActions) and applies them to all its chunks. Deterministic actions stay cached in the strategy. A non-deterministic expression gets fresh actions per INSERT:now64()gives one value per INSERT, and the counter restarts with every INSERT, also for anS3table (on 25.8 it continued across the table's INSERTs until a restart), so concurrent INSERTs do not share it. No partitioned storage supports parallel insert, so an INSERT has one such sink.Validation: the new test
05459_partition_by_stateful_function_spans_chunks(file(),S3engine with two INSERTs,now64(9)) fails on master (10/10) and passes with the fix (50/50 randomized). The #92844 testtest_s3_non_deterministic_partition_byand 15 existing partitioned-write stateless tests pass on both binaries. The reporter's 200000-row MergeTree shape gives 4 x 50000 rows with the fix (2 x 100000 on master).Workflow [PR]
Sync PR [sync-upstream/pr/128217]
Version info
26.11.1.1574-master(included in26.11and later)