Repository navigation
Lightweight Updates v2 - #116007
Lightweight Updates v2#116007
Conversation
|
Workflow [PR], commit [b369659] Summary: ✅
AI ReviewSummaryThis PR re-lands Findings❌ Blockers
Tests
Final VerdictRequest changes until the mixed-version rollout contract is corrected. The implementation may be close, but the current docs/history still describe safe upgrade paths that the code does not actually guarantee. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 1457/1521 (95.79%) · Uncovered code |
A lightweight `UPDATE` on a table whose sorting key carries a direction threw
Logical error: The size of reverse_flags (N) does not match the size of KeyDescription N+2
from `InterpreterUpdateQuery::execute` -> `MergeTreeData::updateLightweightImpl` ->
`getPatchPartMetadataV2` -> `KeyDescription::getKeyFromAST`. It was reported against 29
distinct pull requests, by `BuzzHouse`, the `AST fuzzer` and `Stress test`, on both
architectures, and is why the first landing of this work was reverted in #116005.
`getPatchPartMetadataV2` supplied `_block_number` and `_block_offset` twice: inline in the
`ORDER BY` tuple, and again as `additional_columns` of `getKeyFromAST`. Only the second
route maintains `reverse_flags`. In `buildKeyColumns` the `N` cloned children of a
directional sorting key are `ASTStorageOrderByElement`s and contribute `N` entries to both
the expression list and `reverse_flags`; the two inline identifiers are not order-by
elements, so they add two expressions and no flags; and the `additional_columns` loop, which
is what would append the two matching `false` flags, then skips both names because they are
already among the key columns. The key ends up with `N + 2` expressions against `N` flags.
Dropping the two inline `push_back` calls leaves the columns to `additional_columns`, which
appends them to the expression list and appends a `false` flag each when the flags are
non-empty. `expression_list_ast` and `column_names` are byte for byte what they were --
same columns, same order -- so `getEffectivePatchSortingKeySize` and
`getTableSortingKeyExpressionFromPatch`, which read the patch key's expression list and
expect the two system columns at its end, are unaffected. Only `reverse_flags` changes, from
`N` to `N + 2`. An ascending sorting key produces no flags at all and is untouched, which is
why this only ever broke for a directional key.
This also removes a latent out-of-bounds read: `KeyDescription::getOriginalExpressionList`
indexes `reverse_flags[i]` across the whole expression list, which was two entries short.
It was unreachable only because `getKeyFromAST` threw first.
`getEffectivePatchSortingKey` and the v1 path clone key children without passing
`additional_columns`, so their sizes always agree; they are left alone.
Test `05038_lwu_v2_reverse_sorting_key` covers a descending key, a mixed-direction key and
an ascending control. It fails without this change with the logical error above.
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes139 object files changed (+498.45 KiB total), 2 added.
738 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 units1958 translation units recompiled, 11460 s compile time in total, 1958 of them have a recent master baseline. |
`03100_lwu_deletes_3` failed the `Stateless tests (amd_asan_ubsan, flaky check)` job with `Test runs too long (> 180s)` at 209s: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116007&sha=7d8004e22603dec5906a82841217020bf312c7cc&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29 #116007 The test is a long-standing offender rather than anything new: it also failed this gate five times in the original #103182, and its median in that same job is unchanged by this pull request (44.5s there against 45.1s here), so the `reverse_flags` fix is not implicated. Its sorting key is entirely ascending, which leaves `reverse_flags` empty and the changed code path a no-op for it. Each of the three inserts drops from 100000 rows over 10000 distinct `id`s to 20000 rows over 2000, so every `id` still occurs exactly ten times per partition and all the predicates scale by the same factor of five. The shape of what is exercised is untouched: the same three inserts, three `UPDATE`s and four `DELETE`s, the same overlapping delete ranges, the same delete of rows a previous patch had updated, and the same four patch parts merging into one. `sum(v1)` stays at `42 * 10 * 5`; the row counts and patch row counts scale. Measured against a local build with the exact randomized `MergeTree` settings of the failed run - `merge_max_block_size = 19` above all, which makes the two `OPTIMIZE FINAL` merges linear in row count - the test goes from 1.58s to 0.91s, a 42% cut (min of five runs). Under default settings the saving is only 12%, because the cost is otherwise dominated by fixed per-statement replicated-part overhead; it is the adversarial settings the flaky check randomizes into that make row count matter. Five was preferred to a larger factor because it retains two granules per part at the granularity that run randomized (12441), where a factor of ten would leave one, while capturing nearly all of the saving (42% against 47%). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ting Add an integration test with a server running under the `compatibility` setting in the default profile: lightweight updates write patch parts in v1 format by default, and the explicit `patch_parts_version` table setting takes precedence over `compatibility`. Requested in: #116007 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
Patch-part metadata (columns and patch part index) can be taken only from an active sibling patch part; the partition id stores only a hash of the patch column names. Return false gracefully instead of throwing a logical error in createEmptyPart. #116007 (comment) Co-Authored-By: Claude Fable 5 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=12 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
Cherry pick #116007 to 26.8: Lightweight Updates v2
Backport #116007 to 26.8: Lightweight Updates v2
The test lists the five patch parts produced by its five lightweight UPDATEs
from `system.parts`, runs an explicit
`OPTIMIZE TABLE ... PARTITION ID 'patch-...' FINAL`, and lists them again to
assert they collapsed into one part. The name printed by the second listing was
not deterministic.
Patch parts are ordinary background merge candidates: `MergeTreePartsCollector`
declares `affordable_kinds{Regular, Patch}` and nothing in selection exempts
them. Five tiny parts in one partition are an ideal merge range, so a background
merge can collapse them into `patch-...-all_3_11_1_10` before the explicit
`OPTIMIZE` runs. The `OPTIMIZE` then re-merges that already-merged part, and
since a merged part's level is max(source levels) + 1
(`FutureMergedMutatedPart`), the printed name becomes `_3_11_2_10` while the
reference says `_3_11_1_10`. Only the level subfield differs; the row counts and
the data are unaffected.
The fix pins `max_bytes_to_merge_at_max_space_in_pool = 1` so the background
selector cannot pick the parts up, while the test's own `OPTIMIZE FINAL` still
merges them: merges initiated by `OPTIMIZE FINAL` ignore that limit, which is
documented on the setting and holds mechanically, since the only reader of it on
the merge path is `CompactionStatistics::getMaxSourcePartsBytesForMerge`, called
from the background selector, and
`MergeTreeDataMergerMutator::selectAllPartsToMergeWithinPartition` (the
`OPTIMIZE` path) consults no size limit at all.
The assertions are unchanged and `.reference` is untouched, so the test keeps
pinning the final level to exactly 1: if a background merge ever wins again, it
fails loudly rather than being masked.
This is the same fix ClickHouse#114236 applied to the v2 twin
(`03100_lwu_07_merge_patches`). That twin also carries a level mask from
ClickHouse#108594, but the mask is not needed here and is deliberately not added: it hides
only the level subfield, which the pin already makes deterministic, and it would
throw away the level assertion. The same idiom is used for the same purpose by
`03100_lwu_54_patch_merge_spanning_part_version`, added in ClickHouse#115064: it pins
`max_bytes_to_merge_at_max_space_in_pool = 1` with the comment "Only the
explicit `OPTIMIZE` below may merge the patch parts".
The v1 clone (`c324f831097ec4c`) was taken from a master state that predated
both hardenings, and ClickHouse#116007 restored the file byte-identically, so it never
received either one.
…ed as paths, two readings of one input Blind reviews of the pull requests that introduced nine bugs fixed later (ported in ClickHouse#131294-ClickHouse#131302) showed these gaps: - Data the server wrote itself and reads back (parts, patch parts, packed-file indexes, `ssd_cache` files) was treated as trusted, so out-of-bounds reads on it were dismissed or rated as Nits (ClickHouse#126553, ClickHouse#116007, ClickHouse#86095). - No prompt for overflow of sizes computed from user-controlled counts (`ColumnString::replicate`, ClickHouse#61075). - No prompt for names read from data that become paths (packed archive index, ClickHouse#93414). - A security check that reads its input differently from the consumer (first vs last repeated key, Boolean spellings), which the real review of ClickHouse#106855 missed. Also ask for a separate pass over parsers and security decisions in large pull requests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #103182
Related: #116005
Re-lands #103182 (
Lightweight Updates v2), which was reverted in #116005, together with a fix for the defect that caused the revert.What went wrong the first time
A lightweight
UPDATEon a table whose sorting key carries a direction threw:InterpreterUpdateQuery::execute->MergeTreeData::updateLightweightImpl->getPatchPartMetadataV2->KeyDescription::getKeyFromAST. Between 2026-08-21 and the revert it was reported against 29 distinct pull requests, byBuzzHouse, theAST fuzzerandStress test, on both architectures and in several sanitizer builds.The fix
getPatchPartMetadataV2supplied_block_numberand_block_offsettwice: inline in theORDER BYtuple, and again asadditional_columnsofgetKeyFromAST. Only the second route maintainsreverse_flags. InbuildKeyColumns:Ncloned children of a directional sorting key areASTStorageOrderByElements, contributingNentries to both the expression list andreverse_flags;additional_columnsloop, which is what appends the two matchingfalseflags, then skips both names because they are already among the key columns.The key therefore had
N + 2expressions againstNflags. Dropping the two inlinepush_backcalls leaves the columns toadditional_columns, which appends them to the expression list and appends afalseflag each when the flags are non-empty.expression_list_astandcolumn_namescome out exactly as before — same columns, same order — sogetEffectivePatchSortingKeySizeandgetTableSortingKeyExpressionFromPatch, which read the patch key's expression list and expect the two system columns at its end, are unaffected. Onlyreverse_flagschanges, fromNtoN + 2. An ascending sorting key produces no flags at all and is untouched, which is why only a directional key ever broke.This also removes a latent out-of-bounds read:
KeyDescription::getOriginalExpressionListindexesreverse_flags[i]across the whole expression list, which was two entries short. It was unreachable only becausegetKeyFromASTthrew first.getEffectivePatchSortingKeyand the v1 path clone key children without passingadditional_columns, so their sizes always agree; they are left alone.Testing
05038_lwu_v2_reverse_sorting_keycovers a descending sorting key, a mixed-direction key (ORDER BY (a, b DESC)) and an ascending control. Against the unfixed binary its first statement fails withThe size of reverse_flags (1) does not match the size of KeyDescription 3; with the fix it produces the reference output. The reference was cross-checked against an all-ascending copy of the same test, whoseSELECTs all carry an explicitORDER BYand so must yield identical rows.Separately,
03100_lwu_deletes_3and its_v1clone are scaled down by a factor of five. The test failed theStateless tests (amd_asan_ubsan, flaky check)job here withTest runs too long (> 180s)at 209s, having also failed that gate five times in #103182 itself; its median in that job is unchanged by this pull request (44.5s there against 45.1s here), and its sorting key is entirely ascending, so the change above is a no-op for it. Each insert now writes 20000 rows over 2000 distinctids instead of 100000 over 10000, which keeps everyidoccurring exactly ten times and scales every predicate by the same factor, so the same three inserts, threeUPDATEs and fourDELETEs run against the same overlapping delete ranges and the same four patch parts merge into one. Under the randomizedMergeTreesettings of the failed run —merge_max_block_size = 19above all, which makes the twoOPTIMIZE FINALmerges linear in row count — this cuts the test from 1.58s to 0.91s, a 42% saving; under default settings it would be worth only 12%.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Lightweight
UPDATEqueries now write patch parts that carry the table's sort-key columns, so applying a patch no longer has to hold the whole patch part in memory.Workflow [PR]
Sync PR [sync-upstream/pr/116007]
Version info
26.8.1.2006