Skip to content

Revert "Lightweight Updates v2" - #116005

Merged
alexey-milovidov merged 1 commit into
masterfrom
revert-103182-lightweight-updates-v2
Aug 23, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
revert-103182-lightweight-updates-v2

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

Related: #103182
Related: #111059

Reverts #103182 (Lightweight Updates v2, merged as 70a0c0ff637), which introduced a LOGICAL_ERROR on any lightweight UPDATE of a table whose sorting key has a direction.

The failure

Logical error: The size of reverse_flags (A) does not match the size of KeyDescription B  (STID: 3072-3c97)

InterpreterUpdateQuery::execute -> MergeTreeData::updateLightweightImpl -> getPatchPartMetadataV2 -> KeyDescription::getKeyFromAST.

Example report: Stress test (arm_debug) (PR #115540, an unrelated Web UI change).

It is not confined to one pull request. Since the first occurrence on 2026-08-21 18:02 it has been reported against 29 distinct pull requests across BuzzHouse, AST fuzzer and Stress test, on both architectures and in several sanitizer builds. There is no tracking issue for it yet.

Cause

getPatchPartMetadataV2 builds the patch part's sorting key by cloning the table's original sorting-key AST children and then appending two bare identifiers, in src/Storages/MergeTree/PatchParts/PatchPartsUtils.cpp:

for (const auto & child : sorting_key_expr_list->children)
    order_by_expression->arguments->children.push_back(child->clone());

order_by_expression->arguments->children.push_back(make_intrusive<ASTIdentifier>(BlockNumberColumn::name));
order_by_expression->arguments->children.push_back(make_intrusive<ASTIdentifier>(BlockOffsetColumn::name));

part_metadata.sorting_key = KeyDescription::getKeyFromAST(
    order_by_expression, patch_part_desc, /*virtuals=*/ {}, local_context,
    /*additional_columns=*/ {{BlockNumberColumn::name, ...}, {BlockOffsetColumn::name, ...}});

_block_number and _block_offset are therefore supplied twice: once inline in the tuple and once as additional_columns. Only the second route maintains reverse_flags. In buildKeyColumns (src/Storages/KeyDescription.cpp):

  • the N cloned children are ASTStorageOrderByElements, so they contribute N entries to both reverse_flags and the expression list;
  • the two appended identifiers are not order-by elements, so they contribute 2 expression-list entries and no reverse_flags entries;
  • the additional_columns loop, which is what appends the matching false flags, skips both names because they are already in column_names from the tuple.

The result is reverse_flags.size() == N against an expression list of N + 2, which trips the consistency check in KeyDescription::getKeyFromAST.

The check itself and direction-carrying key descriptions (#111059, merged 2026-07-24) both predate the breakage; they were correct together for a month. Before #103182 the patch-part sorting key was built from bare ASTIdentifiers, so reverse_flags was always empty and the check's !reverse_flags.empty() guard made it unreachable. This was confirmed by diffing 70a0c0ff637^1 against 70a0c0ff637.

An ascending sorting key produces empty reverse_flags and short-circuits the guard, which is why the failure is intermittent and has so far only been found by fuzzers.

Note

A narrow fix looks possible — dropping the two inline push_back calls and letting additional_columns add the columns, which appends the matching false flags. That is a change to the feature's own code and is better made by its author with a regression test for a ORDER BY ... DESC table under a lightweight UPDATE; this pull request only takes the failure off master in the meantime.

Revert mechanics

git revert -m 1 70a0c0ff637. One conflict, in tests/queries/0_stateless/04104_lightweight_update_v2_eager_patch_discard.sql: the test was added by #103182 and later adjusted by dddfaccd441, and is removed here along with the rest of the feature. After the revert, src/Storages/MergeTree/PatchParts/SourcePartsSetForPatch.{h,cpp} are restored and PatchPartIndex.{h,cpp} and applyPatchesLegacy.{h,cpp} are removed; no getPatchPartMetadataV2 reference and no dangling include of a deleted header remains in src/.

The feature is not in any release — the newest is v26.7.5.10-stable, and #103182 landed in the unreleased 26.8 — so there is no changelog entry to retract.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...


Workflow [PR]
Sync PR [sync-upstream/pr/116005]

Version info

  • Merged into: 26.8.1.1956 (included in 26.8 and later)

@alexey-milovidov alexey-milovidov self-assigned this Aug 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [36bb4f1]

@alexey-milovidov
alexey-milovidov merged commit 78da318 into master Aug 23, 2026
2 of 4 checks passed
@alexey-milovidov
alexey-milovidov deleted the revert-103182-lightweight-updates-v2 branch August 23, 2026 18:13
@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Aug 23, 2026
This was referenced Aug 23, 2026
alexey-milovidov added a commit that referenced this pull request Aug 23, 2026
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.
@mintlify

mintlify Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Preview deployment for your docs. Learn more about Mintlify Previews.

Project Status Preview Updated (UTC)
ClickHouse-docs 🔴 Failed – Aug 23, 2026, 6:38 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants