Repository navigation
Fix Memory table abort on ALTER TABLE ... APPLY PATCHES (STID 2380-3043) - #105286
alexey-milovidov merged 8 commits into
Conversation
`ALTER TABLE <memory_tbl> (APPLY PATCHES)` (and the equivalent
`APPLY DELETED MASK`) used to raise
Logical error: Mutation of `Memory` table produced incomplete output:
block 0 has 0 rows, expected N
(`STID: 2380-3043`, surfaced by `BuzzHouse` on master and 8+ unrelated PRs
in a 24h window).
`MutationsInterpreter::prepare` short-circuits both commands without adding
a mutation stage (`MutationsInterpreter.cpp:1238-1243`). On a `Memory` table
the resulting pipeline still pulls one output block per input block, but
each one has zero rows. `StorageMemory::mutate` then walks the partial-column
path and fails the per-block row-count invariant.
`Memory` tables do not own patch parts (lightweight updates are a `MergeTree`
feature) and do not maintain a `_row_exists` deletion mask, so both commands
are semantically no-ops here: the post-state the user is asking for (all
patches applied, all deleted rows physically removed) is already satisfied.
Filter them out before constructing the mutation pipeline; if all commands
are filtered, return immediately. A `UPDATE` or `DELETE` mixed in the same
`ALTER` still executes normally.
CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99972&sha=f7959f604f297d728373e5f6ed753d9a0bde5a3c&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29
Reported on ClickHouse#99972
Regression test `04254_memory_apply_patches_no_op` exercises:
- parenthesised `(APPLY PATCHES)` (the `BuzzHouse` form)
- bare `APPLY PATCHES`
- `APPLY DELETED MASK`
- mixed `UPDATE ... , APPLY PATCHES SETTINGS mutations_sync = 1`
|
cc @CurtizJ @azat @alexey-milovidov — could you review? Fix for |
|
Pre-PR Validation Gate answers (per
|
|
Workflow [PR], commit [b6ccaf5] Summary: ✅
AI ReviewSummaryThis PR makes Final VerdictStatus: ✅ Approve |
|
@groeneai are there any other table engines that could be tested on this scenario as well? |
ClickHouse#104081 renamed the `server_died` event to `stop_testing` in `clickhouse-test` and replaced the parent-side print `Server died, terminating all processes...` with `Stopping tests, terminating all processes...`. The functional-tests results parser still searched for the old string, so when a Bugfix validation run actually killed the server it failed to emit the synthetic `Server died` leaf that the inversion step in `ci/jobs/functional_tests.py` flips to OK — and the job reported `Failed to reproduce the bug` even though the bug had reproduced. Realign the two SIGN constants to the wording the runner now emits and rename the carried variable/field to `stop_testing` to match. Observed on the `Bugfix validation (functional tests)` job of ClickHouse#105286 (server crashed under the test, log: `Stopping tests, terminating all processes...`). Refs ClickHouse#104081
Per @PedroTadim's question on PR ClickHouse#105286: audit every storage that overrides `mutate` and verify only `Memory` was vulnerable to the LOGICAL_ERROR. Other engines either own patches natively (`MergeTree` family), forward (`Alias`, `MaterializedView`), or reject unsupported commands upstream in `checkMutationIsPossible` -- the `IStorage` default for `Log`/`StripeLog`/`TinyLog`/`Null`/`Set`, or a per-engine override for `Join`/ `KeeperMap`/`EmbeddedRocksDB`/`Redis`. `Memory` was the unique outlier: an overridden `mutate` with a row-count invariant, paired with an effectively empty `checkMutationIsPossible`. Lock that contract in so a future refactor cannot silently let `APPLY PATCHES` reach an engine without lightweight-update support. Engines covered: `Log`, `StripeLog`, `TinyLog`, `Null`, `Set`, `Join`. `KeeperMap`/`Redis`/`EmbeddedRocksDB` need external services and are out of scope for a stateless test.
|
Audit and update pushed. I went through every storage that overrides Every other engine is already safe:
To lock the contract in so a future refactor cannot silently let Verified locally: 50/50 passes under full randomization with the fix; reproduces the original |
|
Check the CI failure. |
…emory-apply-patches
|
@tuanpach — triage result for Failure is not PR-caused. It is the chronic The two Cross-PR data (CIDB, 30d):
Upstream fix: #104905 (UBSan null deref in StorageTimeSeries ctor) merged at 2026-05-20 07:10 UTC, ~4 minutes after your ping. The PR's commit Action taken: merged master into this branch ( No fix-up to the Memory APPLY PATCHES change is needed. |
|
@groeneai merge with master one more time |
…emory-apply-patches
|
@PedroTadim master merged in 6739104. CI will re-run automatically. |
| void StorageMemory::mutate(const MutationCommands & commands, ContextPtr context) | ||
| { | ||
| std::lock_guard lock(mutex); | ||
|
|
There was a problem hiding this comment.
Wouldn't it be better to throw an error? This way we just silently don't do anything.
Okay, I see that Memory engine just silently ignores things, so this should be good.
|
@groeneai check the CI failure |
|
@PedroTadim CI triage for
Both checks are unrelated infra. No fix-up needed. (And thanks antaljanosbenjamin for the double-check on the no-op semantics. The engine audit in the earlier reply confirms every other engine either owns patches natively or rejects via |
CI ledger — covered SHA:
|
| Check | Test | Disposition |
|---|---|---|
Stateless tests (amd_tsan, parallel, 1/2) |
00096_obfuscator_save_load, 00175_obfuscator_schema_inference |
flaky chronic trunk — tasks 2026-06-02-ci-00096obfuscatorsaveload-chronic-f / 2026-06-02-ci-00175obfuscatorschemainference-ab |
Unit tests (asan_ubsan) |
(check-level) | infra/chronic UBSan UntrackedMemoryCounter — task 2026-06-03-ci-p1-chronic-ubsan-in-untrackedmemoryc |
No PR-caused failures. tuanpach approved 2026-05-21. Awaiting maintainer merge.
Session: cron:our-pr-ci-monitor:20260604-213000
CI Ledger — covered SHA: d6676e4CI fully finished on this HEAD (master-merge of 2026-06-03 05:54Z, ~46h ago). Finish Workflow SUCCESS @ 2026-06-03T10:58:24Z, Mergeable Check SUCCESS, PR aggregator SUCCESS. 141 pass + 19 skipped + 2 failures, all non-PR-caused:
Both classes are pre-existing chronic/trunk noise, not introduced by this PR. The Memory APPLY PATCHES no-op fix itself ( posted-comment self-reference: #105286 (this comment) @ tuanpach approved 2026-05-21 + 2026-05-22 (last approval predates this master-merge, but the merge is master-only and didn't touch this PR's files). Awaiting final merge. Session: cron:our-pr-ci-monitor:20260605-033000 |
|
@groeneai merge with master one more time |
…emory-apply-patches
|
@PedroTadim master merged in b6ccaf5 (clean, zero conflicts). CI will re-run automatically. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 18/18 (100.00%) | Lost baseline coverage: none · Uncovered code |
…ect (STID 2380-3043) Fixes `STID: 2380-3043` (`Logical error: Mutation of \`Memory\` table produced incomplete output: block 0 has 0 rows, expected N`), reported by `@alexey-milovidov` on ClickHouse#106615 and seen on master and 8+ unrelated PRs. Reproducer (master HEAD, deterministic; aborts the server in debug builds): CREATE TABLE t (id UInt64, value String) ENGINE = Memory; INSERT INTO t VALUES (1, 'a'), (2, 'b'); ALTER TABLE t (APPLY PATCHES); -- or: ALTER TABLE t MATERIALIZE STATISTICS ALL; -- or: ALTER TABLE t MATERIALIZE INDEX nonex; Root cause: `MutationsInterpreter` short-circuits `APPLY_PATCHES` and `APPLY_DELETED_MASK` in its prepare loop without adding a stage. For `MATERIALIZE_INDEX`, `MATERIALIZE_PROJECTION`, `MATERIALIZE_STATISTICS`, and `MATERIALIZE_TTL`, the prepare loop also adds no column dependencies when the target structure does not exist on the storage (`Memory` has no indices, projections, statistics or TTL). The resulting pipeline pulls one output block per input block but with zero rows in each. `StorageMemory::mutate` then walks the partial-column path and fails the per-block row-count invariant. Fix: filter at the storage entry point. `Memory` tables hold raw column blocks in RAM and have no on-storage analog of indices, projections, statistics, TTL, patch parts, or a `_row_exists` deletion mask. Mutation commands that target those structures are semantic no-ops on `Memory` and are removed from the command set before constructing the pipeline. If all commands are filtered out, `mutate` returns immediately. A real `UPDATE` / `DELETE` / `MATERIALIZE_COLUMN` mixed into the same `ALTER` still executes. This supersedes ClickHouse#105286, which filtered only `APPLY_PATCHES` and `APPLY_DELETED_MASK`; CIDB shows the same `LOGICAL_ERROR` reaching the same code path via `MATERIALIZE STATISTICS ALL`, `MATERIALIZE INDEX <name>`, and `MATERIALIZE PROJECTION <name>` shapes that the narrower fix did not cover. Regression test `04322_memory_mutate_no_op_commands_2380_3043` exercises every no-op shape (`APPLY PATCHES` parens form and bare form, `APPLY DELETED MASK`, `MATERIALIZE STATISTICS ALL`, `MATERIALIZE INDEX nonex`, `MATERIALIZE PROJECTION nonex`) plus a mixed `UPDATE ... APPLY PATCHES SETTINGS mutations_sync = 1` to prove that real updates still execute. The existing `01867_fix_storage_memory_mutation` and `04229_memory_mutation_timeout_break` tests still pass. Caused by: ClickHouse#103706 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Fixes
STID: 2380-3043(Logical error: Mutation of \Memory` table produced incomplete output: block 0 has 0 rows, expected N), reported by @alexey-milovidov on https://github.com/ClickHouse/ClickHouse/pull/99972 as a chronicBuzzHousefinding hitting master and 8+ unrelated PRs (#99804,#100185,#100586,#104606,#105107,#105161,#105233`).CI report demonstrating the original failure:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=99972&sha=f7959f604f297d728373e5f6ed753d9a0bde5a3c&name_0=PR&name_1=BuzzHouse%20%28amd_tsan%29
Reproducer (master HEAD, deterministic)
Root cause
MutationsInterpreter::prepareshort-circuitsAPPLY_PATCHES/APPLY_DELETED_MASKwithout adding a mutation stage(
MutationsInterpreter.cpp:1238-1243). The resulting pipeline still pulls oneoutput block per input block, but each block has zero rows.
StorageMemory::mutatethen walks the partial-column path and fails the per-block row-count invariant
at
StorageMemory.cpp:317.Fix
Memorytables do not own patch parts (lightweight updates are aMergeTreefeature —
IStorage::supportsLightweightUpdatereturns"Table with engine Memory doesn't support lightweight updates") and do notmaintain a
_row_existsdeletion mask. Both commands are therefore semanticno-ops on
Memory: the post-state the user is asking for (all patches applied,all deleted rows physically removed) is already satisfied.
StorageMemory::mutatenow filtersAPPLY_PATCHES/APPLY_DELETED_MASKoutof the command set before constructing the mutation pipeline. If all commands
are filtered out, it returns immediately. A real
UPDATEorDELETEmixedinto the same
ALTERstill executes normally — exercised by the regressiontest.
Regression test
04254_memory_apply_patches_no_opcovers:ALTER TABLE m (APPLY PATCHES)(theBuzzHouseform)ALTER TABLE m APPLY PATCHESALTER TABLE m APPLY DELETED MASKALTER TABLE m UPDATE value = upper(value) WHERE id = 1, APPLY PATCHES SETTINGS mutations_sync = 1Verified locally: 50/50 passes with
--test-runs 50(randomised settings on).01867_fix_storage_memory_mutationand04229_memory_mutation_timeout_breakstill pass.
Closes #106846
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a logical error
Mutation of \Memory` table produced incomplete outputraised when runningALTER TABLE <memory_table> APPLY PATCHESorALTER TABLE <memory_table> APPLY DELETED MASK.Memory` tables do not own patch parts or deletion masks, so both commands are now correctly treated as no-ops.Documentation entry for user-facing changes
Version info
26.6.1.751