Skip to content

Fix INSERT into a Set or Join table on a plain_rewritable disk - #132449

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-plain-rewritable-move-into-virtual-dir
Nov 22, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-plain-rewritable-move-into-virtual-dir

Conversation

@groeneai

@groeneai groeneai commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #131994
Related: #91935

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):

Fixed LOGICAL_ERROR Directory '...' is virtual on the first INSERT into a Set or Join table whose disk setting names a plain_rewritable disk, on ALTER TABLE ... DELETE of such a Join table, and on clickhouse-disks move or link into a directory that mkdir --parents created only as a parent.

Description

On 26.10 (also 26.8 and 26.3, checked on 26.10.4.207, 26.8.37.168 and 26.3.63.123), INSERT INTO a Set or Join table with SETTINGS disk = '<plain_rewritable disk>' fails with LOGICAL_ERROR: Directory 'store/...' is virtual, which aborts debug and sanitizer builds. Found by La Casa Del Dolor: report.

On plain_rewritable, createDirectoryRecursive writes a prefix.path only for the leaf, so the ancestors exist only as virtual directories. StorageSetOrJoinBase::restore creates <path>/tmp, which leaves the table directory virtual, and the INSERT then moves tmp/<n>.bin into it. Writing a file into a virtual directory already materializes it (#91935); moving or hard-linking a file into one did not.

moveFile/replaceFile and createHardLink now materialize a virtual target directory the same way generateObjectKeyForPath does for a new file (useMissingDirectory + createDirectoryRecursive), with the semantics of a write: if another transaction materializes or replaces the directory before the commit, the commit fails with DIRECTORY_ALREADY_EXISTS; if it removes the directory, the commit recreates it. Of two concurrent first INSERTs into a fresh table one succeeds and the other can fail with DIRECTORY_ALREADY_EXISTS (retryable); today both fail with LOGICAL_ERROR.

Validation: the new stateless test (Set and Join, read back after DETACH/ATTACH) aborts the server on master and passes with the fix (30/30 randomized runs). Two gtests cover a move and a hard link with hard links on and off, and a concurrent replacement of the target; removing the useMissingDirectory guard makes the latter fail.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.200-master (included in 26.12 and later)
  • Backported to: 26.11.1.2926

groeneai and others added 3 commits November 17, 2026 16:48
…rectory

A new `Set` or `Join` table on a `plain_rewritable` disk creates `<path>/tmp`
with `createDirectories`, which records only the leaf, so `<path>` exists only
as a virtual node. The first INSERT then `replaceFile`s `tmp/<n>.bin` into it
and the commit throws `LOGICAL_ERROR: Directory '...' is virtual`.

The stateless test covers the Set/Join INSERT. The gtests cover a move and a
hard link (also with hard links disabled) into a virtual directory, a
concurrent removal of the virtual target, and a concurrent replacement of it by
a directory in the explicit form.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… or linked into it

On a `plain_rewritable` disk `createDirectoryRecursive` writes a `prefix.path`
only for the leaf, so its ancestors exist only as virtual nodes. Writing a file
into such a directory already materializes it (ClickHouse#91935, in
`generateObjectKeyForPath`), but `moveFile`/`replaceFile` and `createHardLink`
did not, and their commit threw `LOGICAL_ERROR: Directory '...' is virtual`.
That is hit by the first INSERT into every `Set`/`Join` table on such a disk
(`StorageSetOrJoinBase::restore` creates `<path>/tmp`, and the INSERT then
`replaceFile`s `tmp/<n>.bin` into `<path>`), by a `Join` mutation, and by
`clickhouse-disks move`/`link`.

The new `materializeVirtualDirectory` is called for the target directory of a
move and of a hard link. It plans a `CreateDirectoryOperation` for it in a new
materialize-only mode and records the directory in the uncommitted state, so
the move or link plans against a real, empty, implicit directory. Like the
write path, it registers `useMissingDirectory`, so a commit fails with
`DIRECTORY_ALREADY_EXISTS` when another transaction materialized or replaced
the directory in between (the move, the real hard link and the
hard-links-disabled copy were all planned for the implicit form). The
materialize-only operation throws `DIRECTORY_DOESNT_EXIST` when the directory
was removed in between, instead of recreating it. A target directory that does
not exist at all still fails as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the sentence in materializeVirtualDirectory that narrated the new code's useMissingDirectory precondition (it is in the fix commit message).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own review of the diff (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 materializeVirtualDirectory repeats the planning block of createDirectoryRecursive with materialize_only set; one shared builder could serve both AGREE, not changed noted, not blocking

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-48:20261117-182800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Michicosun, could you review this? On plain_rewritable, moveFile/replaceFile and createHardLink now materialize a target directory that exists only as a virtual parent, like writes already do, so the first INSERT into a Set or Join table on such a disk no longer hits the 'Directory is virtual' logical error.

@groeneai

groeneai commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CREATE TABLE t (a UInt64) ENGINE = Set SETTINGS disk = '<plain_rewritable disk>'; INSERT INTO t VALUES (1) fails every time with LOGICAL_ERROR Directory 'store/...' is virtual on master and on 26.10.4.207, 26.8.37.168, 26.3.63.123.
b Root cause explained? StorageSetOrJoinBase::restore runs createDirectories(<path>/tmp); on plain_rewritable only the leaf gets a prefix.path, so <path> is virtual. The INSERT moves tmp/<n>.bin into it, and MoveFileOperation throws is virtual: only writes materialized a virtual directory (#91935).
c Fix matches root cause? Yes. Moves and hard links now materialize a virtual target directory in their transaction, like writes; the is virtual checks stay as invariant checks.
d Test intent preserved / new tests added? New stateless test 05816_set_join_on_plain_rewritable_disk (Set and Join, DETACH/ATTACH read-back) and two gtests (move and hard link with hard links on/off, concurrent replacement of the target; the concurrent-removal gtest was dropped with the materialize_only mode in 3a04983).
e Both directions demonstrated? Stateless test: master server aborts with the CI stack, fix passes (50/50 randomized, 10/10 without randomization). gtests: the move/link case aborts on master, the replacement case fails on master, all pass with the fix. Disabling the materialize-only check or the useMissingDirectory precondition makes exactly its gtest fail.
f Fix is general across code paths? Covers every operation that puts a file into a directory: write (already), moveFile/replaceFile, createHardLink (real link and the copy used when hard links are disabled). Callers fixed with it: Set/Join INSERT, Join mutation, clickhouse-disks move/link.
g Fix generalizes across inputs (params/datatypes/wrappers)? Target states: virtual (fixed), missing (unchanged error), real (unchanged), root (skipped), removed or replaced concurrently (clean errors, nothing recreated). 20x8 racing first INSERTs: only DIRECTORY_ALREADY_EXISTS failures, sets consistent after reload.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; a materialized directory is an ordinary prefix.path. Tables created before the fix get their directory materialized on the first INSERT.
i Invariants and contracts preserved? The move/link is planned against a real, empty, implicit directory, and the precondition keeps that form true at commit. A removal is detected before any write, so undo has nothing to revert. Restart and reload read the moved files back (gtest restart, server restart probe).

Session id: cron:clickhouse-impl-slot-40:20261117-164501

@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3a04983]

Summary: ✅


AI Review

Summary

PR #132449 fixes moves and hard links into virtual directories on plain_rewritable disks, including the first INSERT into Set and Join tables, by reusing the existing file-write materialization path. Reviewed commit 3a04983ad164e28b4b4a801ac6f7e93e89ca0322 and all prior discussion. No actionable findings.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors labels Nov 17, 2026

@Michicosun Michicosun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

look how materialization works for create file and do the same, do not change all operations with additional flag just to support corner case

@clickhouse-gh clickhouse-gh Bot added the comp-object-storage-disks Object-storage disk layer: DiskObjectStorage, metadata storage adapters, transaction support for ... label Nov 17, 2026
clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 17, 2026
Review: ClickHouse#132449 (review)

The target directory of a move or a hard link is now materialized exactly as
`generateObjectKeyForPath` materializes the parent of a new file:
`useMissingDirectory` plus `createDirectoryRecursive`. The `materialize_only`
mode of `CreateDirectoryOperation` is removed, so the operations are the same
as on master.

As for a write, a target directory removed by a concurrent transaction after
the move was planned is recreated by the move instead of failing it. The
`ConcurrentRemoveOfVirtualMoveTarget` test, which pinned the removed mode, is
dropped. A move into a directory that does not exist at all still fails as
before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3a04983ad with master 4ae47eea3 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.77 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 7.2 s 14.8 s +9.3 s (+167%)
Compile time of recompiled translation units

8 translation units recompiled, 23 s compile time in total, 8 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks, done in 3a04983: the materialize_only flag is removed and CreateDirectoryOperation is unchanged from master. A move or a hard link into a virtual directory now materializes it the same way generateObjectKeyForPath does for a new file (useMissingDirectory + createDirectoryRecursive).

One consequence, the same as for a write: if a concurrent transaction removes the virtual target after the move was planned, the move recreates the directory instead of failing (checked in a gtest, the moved file is intact after a restart). I dropped the gtest that pinned the old behavior and updated the description.

@clickhouse-gh
clickhouse-gh Bot dismissed their stale review November 17, 2026 20:22

AI review of a later commit does not approve the PR, see the AI Review comment.

@PedroTadim

Copy link
Copy Markdown
Member

Thanks @Michicosun I was going to ask your help

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good.

@alexey-milovidov alexey-milovidov self-assigned this Nov 22, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 22, 2026
Merged via the queue into ClickHouse:master with commit 6e49d66 Nov 22, 2026
193 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 22, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 22, 2026
Backport #132449 to 26.11: Fix `INSERT` into a `Set` or `Join` table on a `plain_rewritable` disk
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 25, 2026
Cherry pick #132449 to 26.10: Fix `INSERT` into a `Set` or `Join` table on a `plain_rewritable` disk
robot-clickhouse added a commit that referenced this pull request Nov 25, 2026
…on a `plain_rewritable` disk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-object-storage-disks Object-storage disk layer: DiskObjectStorage, metadata storage adapters, transaction support for ... pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.10-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants