Repository navigation
Keep the part sorted when TTL GROUP BY ... SET rewrites a sorting key column - #119863
Conversation
… column `TTL ... GROUP BY ... SET` emits the aggregated rows at the position of their group in the merged stream, but a `SET` expression may assign arbitrary values to a column of the sorting key, so an aggregated row could be less than the previously emitted one. The merge wrote such a part as is: a debug build threw `Sort order of blocks violated` in `CheckSortedTransform`, and a release build produced a part whose primary index does not match the data. Now `TTLAggregationAlgorithm` tracks the sorting key of the last emitted row. If the next row violates the order, its sorting key is replaced with the key of the previous row, so the output stays sorted and the primary index stays consistent with the data. Closes: #108514 Co-Authored-By: Claude Fable 5.1 <[email protected]>
… on the number of merges Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [b9df624] Summary: ✅
AI ReviewSummaryThis PR fixes the primary-key / skip-index inconsistency from Findings❌ Blockers
Final VerdictChanges requested: the index-consistency fix looks correct now, but the part can still persist wrong user-visible values through dependent stored generated columns after LLVM Coverage ReportMeasured on commit b9df624.
Changed lines: Changed C/C++ lines covered: 99/105 (94.29%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes23 object files changed (+69.57 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units29 translation units recompiled, 230 s compile time in total, 29 of them have a recent master baseline. |
…n columns, and cover the mutation path The first version of `TTLAggregationAlgorithm::restoreSortOrder` compared and rewrote the sorting key expression columns that the merge had calculated before the TTL step (`toStartOfDay(ts)`). Those columns are not stored: the next merge recalculates the sorting key from the stored `ts`, so the part was still out of order on disk and `CheckSortedTransform` threw the logical error `Sort order of blocks violated` when the part was merged again. In addition, after `SET` the expression columns held the values calculated before it, so the primary index did not correspond to the data even for rows in the right order. Now the algorithm calculates the full sorting key from the source columns of the block itself (this also covers a mutation, whose header has only the primary key columns), replaces the source columns of an out-of-order row with the values of the previous row, and writes the recalculated sorting key columns into the block for the primary index. The test checks the physical order of the rows, compares the primary index with the data, covers `MATERIALIZE TTL` with a `PRIMARY KEY` shorter than `ORDER BY`, and merges the written part again. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119863&sha=aa22ea9de8ab2beba8635e97cf52b40bbb29dabf&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29 PR: #119863 Co-Authored-By: Claude Fable 5.1 <[email protected]>
…ms in a mutation `MutateAllPartColumnsTask` materialized the primary key and the skip index expressions before `TTLTransform`, so the cached expression columns described the data of the source part. `TTL ... GROUP BY ... SET` assigns new values to the aggregated rows (and `restoreSortOrder` copies the sorting key source columns forward), so an index such as `INDEX idx_month toYYYYMM(ts) TYPE minmax` kept the month of the source part and pruned the rows of the new part away. Move the index expression step after the TTL transforms and cover it with a `MATERIALIZE TTL` test. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119863&sha=55a94809f223e37c899cc2c6c0f85acce3b803ec&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29 ( |
|
Investigated. Same route as #119814, the open fix you named, so there is no new PR from me. Fix-presence check on this head: #119814's tip What the failing run shows (
That is the mechanism #119814 fixes, and #119814 also adds Nothing here comes from this PR. Its diff is in the TTL merge path ( |
TTL ... GROUP BY ... SETemits the aggregated rows at the position of their group in the merged stream, but aSETexpression may assign arbitrary values to a column the sorting key is calculated from (for exampleORDER BY (k, toStartOfDay(ts))withSET k = max(v)orSET ts = ...), so an aggregated row could be less than the previously emitted one. The merge wrote such a part as is: a debug build threw the logical errorSort order of blocks violatedinCheckSortedTransform, and a release build silently produced a part whose order and primary index do not match the data.Now
TTLAggregationAlgorithmcalculates the full sorting key of every emitted row from the data (a mutation has only the primary key columns in its header, and afterSETthe sorting key columns calculated by the merge before the TTL step are stale) and compares it with the previously emitted row. If the order is violated, the columns the sorting key is calculated from (kandtsin the example) are replaced in the row with the values of the previous row, so the row gets the same sorting key and the part stays sorted after it is written and read again. The sorting key columns of the header, which are used for the primary index, are recalculated from the resulting data, so the primary index corresponds to the data. This is a simpler alternative to #108550, which re-sorts the output of the TTL step.The test checks the physical order of the rows, compares the primary index (
index_granularity = 1) with the data, covers theMATERIALIZE TTLmutation withPRIMARY KEYshorter thanORDER BY, and merges the written part again, which checks its order in debug builds.Closes: #108514
Related: #108550
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the order of rows in a data part written by a merge or a mutation with
TTL ... GROUP BY ... SETwhen theSETclause assigns a new value to a column the sorting key is calculated from. Previously such a merge could produce a part that is not sorted and whose primary index does not match the data (a logical errorSort order of blocks violatedin debug builds). Now the columns the sorting key is calculated from are replaced in an out-of-order row with the values of the previous row. Closes #108514.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/119863]
Version info
26.10.1.527-master(included in26.10and later)