Skip to content

Skip views in TRUNCATE ALL TABLES - #100943

Merged
alexey-milovidov merged 31 commits into
masterfrom
fix-truncate-all-tables-skip-views
May 1, 2026
Merged

alexey-milovidov merged 31 commits into
masterfrom
fix-truncate-all-tables-skip-views

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 27, 2026 •

Copy link
Copy Markdown
Member

TRUNCATE ALL TABLES FROM <database> failed with "Truncate is not supported by storage View" when the database contained views.

Add supportsTruncate virtual method to IStorage (default true), override it to false in StorageView, and delegate in StorageProxy. Both TRUNCATE ALL TABLES code paths (with and without LIKE pattern) now skip storages that don't support truncation.

Closes #78165

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

TRUNCATE ALL TABLES no longer fails when the database contains views.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.212

`TRUNCATE ALL TABLES FROM <database>` failed with "Truncate is not
supported by storage View" when the database contained views.

Add `supportsTruncate` virtual method to `IStorage` (default true),
override it to false in `StorageView`, and delegate in `StorageProxy`.
Both TRUNCATE ALL TABLES code paths (with and without LIKE pattern) now
skip storages that don't support truncation.

Closes #78165

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2f70643]

Summary: ✅


AI Review

Summary

This PR fixes TRUNCATE ALL TABLES on databases containing views by introducing IStorage::supportsTruncate, overriding it for StorageView and StorageDictionary, and using it in both truncate-all code paths in InterpreterDropQuery. The fix is coherent, includes regression coverage for both plain and LIKE forms, and I did not find additional correctness, safety, or performance issues in the changed code.

Missing context
  • ⚠️ Several CI checks are still in progress, so final runtime validation status is not yet available.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 27, 2026
alexey-milovidov and others added 9 commits March 28, 2026 23:36
…runcate`

`StorageDictionary` did not override `supportsTruncate`, so it inherited
`true` from `IStorage`. This meant `TRUNCATE ALL TABLES` would still
attempt to truncate dictionaries and fail with "Cannot TRUNCATE
dictionary". Override `supportsTruncate` to return false in
`StorageDictionary`, and add a dictionary to the regression test.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The test 02859_replicated_db_name_zookeeper is fixed in #101952

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158

alexey-milovidov added a commit that referenced this pull request Apr 10, 2026
…ized-value

The `_fmt_str` member (std::string_view) was not default-initialized,
so constructors that don't explicitly set it (the default constructor
and the 3-parameter constructor) left it uninitialized. When such a
message was later logged through `TextLog`, `TextLogElement::appendToBlock`
read the garbage `string_view`, triggering a MemorySanitizer error.

Add a default member initializer `{}` so every constructor path starts
with an empty `string_view`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100943&sha=03543a965cfbdbf1e13fc3b9ff285fd0cf526fff&name_0=PR&name_1=Stress%20test%20%28arm_msan%29
PR: #100943

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov — apologies for the late reply on this thread. The BuzzHouse finding from the 2026-04-25 arm_asan_ubsan run was investigated and fixed in a separate PR the same day, but I never closed the loop here.

The finding: STID 2352-49be — Logical error: 'Part all_2_11_3 intersects previous part all_0_5_2. It is a bug or a result of manual intervention in the ZooKeeper data.' (failed report).

Root cause: Unrelated to this PR. It's a rare chronic trunk bug in StorageReplicatedMergeTree::checkPartsImpl — the empty-unexpected-parts covering set is built with ActiveDataPartSet::add, which throws LOGICAL_ERROR when two empty unexpected parts have intersecting block ranges without one fully containing the other. The covering check upstream (MergeTreeData::loadDataParts) tests strict containment, so two parts can both be uncovered=true and still intersect. Same root-cause family as closed issue #97356 (STID 2352-4570).

CIDB cross-PR over 30 days: 1 hit (this PR). Over 76 days the BuzzHouse fuzzer has caught it twice — also on PR #96130 in 2026-02 (amd_binary, ParallelReplicas, s3 storage, sequential). It's not caused by TRUNCATE ALL TABLES skipping views — the checkPartsImpl code path runs at startup of an existing ReplicatedMergeTree table.

Fix: #103537 — uses an ActiveDataPartSet::tryAdd + OverlappingPartCovering helper to keep the larger of two intersecting markers, so any unexpected part covered by either marker still gets dropped (preserves TOO_MANY_UNEXPECTED_DATA_PARTS accounting). Includes a unit-test repro on gtest_active_data_part_set that deterministically reproduces the original error message and stack. CI is green (143 SUCCESS / 22 SKIPPED), reviewers requested (@tavplubix @tuanpach @tiandiwonder).

This is distinct from PR #100992 (tuanpach), which targets the separate PartLoadingTree::add path for parts with transaction metadata.

On the parallel concern from the bot review about StorageDictionary::supportsTruncate — that override is already present in this PR's diff (StorageDictionary.h:90), and the test 04070_truncate_all_tables_skip_views.sql covers both views and dictionaries. So that part is already addressed; nothing further needed here.

github-merge-queue Bot pushed a commit that referenced this pull request May 1, 2026
…atedMergeTree startup

`StorageReplicatedMergeTree::checkPartsImpl` builds a covering set of empty
unexpected parts (`set_of_empty_unexpected_parts`) by adding every empty
unexpected part whose `uncovered` flag is `true`. The `uncovered` flag is set
in `MergeTreeData::loadDataParts` to `true` when no other unexpected part's
`MergeTreePartInfo` *contains* the current one.

Two empty unexpected parts on disk can have intersecting block ranges without
one fully containing the other (for example, `all_0_5_2` and `all_2_11_3`).
Both parts pass the `uncovered = true` check, but `ActiveDataPartSet::add`
calls `addImpl`, which throws `LOGICAL_ERROR` with the "Part X intersects
previous part Y" message when the second part is added. The exception is
thrown on the `BackgroundSchedulePool` thread running
`ReplicatedMergeTreeAttachThread::runImpl`, aborting startup of the table.

Replace `add` with `tryAdd` and log a warning when the conflicting empty
marker is skipped. The set is only consulted later as a heuristic — when an
unexpected part is matched against `getContainingPart` to decide whether it
looks like a leftover of a dropped part — so keeping just one of two
intersecting empty markers is sufficient. Any unexpected part that would
have been covered by the skipped marker simply falls through to the regular
handling path that detaches it as `ignored` further down in the same loop.

Add a small `gtest_active_data_part_set` unit test that documents the
contract: `add` throws on intersection without containment while `tryAdd`
returns `HasIntersectingPart` without throwing.

Reproduction comes from the BuzzHouse fuzzer (STID `2352-49be`):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100943&sha=3308ce056bdfd5672c3c300ccae03968beac9846&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29

Original PR with the BuzzHouse finding: #100943
@clickhouse-gh

clickhouse-gh Bot commented May 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.10% +0.10%
Functions 91.10% 91.10% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 85.19% (23/27) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this May 1, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue May 1, 2026
Merged via the queue into master with commit 9569185 May 1, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-truncate-all-tables-skip-views branch May 1, 2026 05:06
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 1, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: InterpreterDropQuery::executeToDatabaseImpl (TRUNCATE ALL TABLES code paths with and without LIKE) — core code present in all supported branches.

Why: User-visible bug fix that restores documented TRUNCATE ALL TABLES behavior for databases containing views or dictionaries. The bug exists in all current stable branches: InterpreterDropQuery::executeToDatabaseImpl predates all supported releases (file added long before 25.8). PR has pr-bugfix label; lacks explicit pr-must-backport label, so flagging.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ized-value

The `_fmt_str` member (std::string_view) was not default-initialized,
so constructors that don't explicitly set it (the default constructor
and the 3-parameter constructor) left it uninitialized. When such a
message was later logged through `TextLog`, `TextLogElement::appendToBlock`
read the garbage `string_view`, triggering a MemorySanitizer error.

Add a default member initializer `{}` so every constructor path starts
with an empty `string_view`.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100943&sha=03543a965cfbdbf1e13fc3b9ff285fd0cf526fff&name_0=PR&name_1=Stress%20test%20%28arm_msan%29
PR: ClickHouse#100943

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Sep 22, 2026
The cherry-pick of #121134 only overrides `supportsTruncate` in
`StorageAlias`, but on 26.3 nothing consults that method: the virtual
was added to `IStorage` and wired into both database-wide `TRUNCATE`
paths of `InterpreterDropQuery` by #100943 ("Skip views in
`TRUNCATE ALL TABLES`"), which was never backported. Without it the
`Alias` override is dead code and the new test fails.

Bring in the source part of #100943 verbatim (`IStorage`, `StorageView`,
`StorageDictionary`, `StorageProxy`, `InterpreterDropQuery`) together
with its regression test, renumbered from 04070 to 05232 because the
04070 prefix is already taken on this branch.

Related: #100943
Related: #121134

Co-Authored-By: Claude Fable 5.1 <[email protected]>
pull Bot pushed a commit to AKJUS/ClickHouse that referenced this pull request Sep 24, 2026
TRUNCATE ALL TABLES FROM <db> collects every table whose
IStorage::supportsTruncate() is true (InterpreterDropQuery.cpp:716),
truncates them in a thread pool, and rethrows the first error only after
every job has run (:795). The predicate defaults to true
(IStorage.h:545) while the IStorage::truncate() it falls back to throws
NOT_IMPLEMENTED (IStorage.cpp:214-221). So an engine that stores nothing
at all, and therefore never implemented truncate(), is nevertheless
collected and then throws - failing the statement for the whole database
after the truncatable tables in it were already emptied. The caller gets
a statement that is both rejected and partially applied, with no way to
tell how far it got.

A false answer from that predicate means "omit this table silently",
which is sound exactly when the storage has no data of its own. That is
why StorageView, StorageDictionary and the URL scheme dispatcher already
return false. Add the same one-line override to the engines that
provably store nothing: Merge (a read-time union over other tables),
Null (discards every insert), GenerateRandom, FuzzQuery and FuzzJSON
(synthesise rows on read), Executable and ExecutablePool (run a user
script on read), and QueryRunner (a write-only sink with no read path at
all). Truncating any of them is a no-op by construction, so skipping
them removes nothing.

URL is the one member justified by an existing in-tree decision rather
than by storing nothing: StorageURL.cpp already gives
StorageURLSchemeDispatch supportsTruncate() == false plus a rethrowing
truncate(), precisely so that a table declared as URL is never
truncated. Plain StorageURL was left out, so URL('file://...') is
skipped today while URL('http://...') fails the statement. This
completes that decision rather than taking a new one.

The IStorage default deliberately stays true, so an engine nobody marked
keeps exactly today's visible error and can never be skipped silently.
Engines that hold rows of their own, or are backed by a system that does
- Buffer, MySQL, PostgreSQL, MongoDB, SQLite, BigQuery, ODBC, JDBC,
Kafka and the other streaming storages - are deliberately left failing:
marking them would report success over surviving data. StorageXDBC
(ODBC/JDBC) derives from IStorageURLBase, not from StorageURL, so the
override here does not reach it. An explicit TRUNCATE TABLE never
consults the predicate (executeToTableImpl), so it is unchanged for
every engine.

Follows ClickHouse#100943, which did the same for views and dictionaries.

Co-Authored-By: Claude Opus 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

TRUNCATE ALL TABLES failed on VIEWs

4 participants