Skip to content

Fix CREATE OR REPLACE TABLE losing source-side view dependencies - #104907

Closed
serprex wants to merge 1 commit into
ClickHouse:masterfrom
serprex:fix-create-or-replace-table
Closed

serprex wants to merge 1 commit into
ClickHouse:masterfrom
serprex:fix-create-or-replace-table

Conversation

@serprex

@serprex serprex commented May 14, 2026 •

Copy link
Copy Markdown
Member

InterpreterRenameQuery::executeToTables cross-swaps the harvested from_dependent_views and to_dependent_views after the physical rename, which is correct for EXCHANGE TABLES a, b (both tables persist, edges follow data). doCreateOrReplaceTable routes both CREATE OR REPLACE TABLE and REPLACE TABLE through the same rename interpreter, but the displaced table is dropped immediately afterwards — so the cross-swap pins the source-view edges to the soon-to-be-dropped table and the new occupant of the name ends up with system.tables.dependencies_table empty. InsertDependenciesBuilder then visits no MVs on the next insert and rows are silently lost.

Tag the synthetic rename built by doCreateOrReplaceTable with a new ASTRenameQuery::is_create_or_replace flag and, when set, restore each harvested dependent-view list onto the table id it came from instead of swapping them. EXCHANGE TABLES a, b keeps its existing semantics.

Regression introduced in #98779

Closes #104906

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

fix REPLACE TABLE dropping MV dependencies

`InterpreterRenameQuery::executeToTables` cross-swaps the harvested
`from_dependent_views` and `to_dependent_views` after the physical
rename, which is correct for `EXCHANGE TABLES a, b` (both tables
persist, edges follow data). `doCreateOrReplaceTable` routes both
`CREATE OR REPLACE TABLE` and `REPLACE TABLE` through the same
rename interpreter, but the displaced table is dropped immediately
afterwards — so the cross-swap pins the source-view edges to the
soon-to-be-dropped table and the new occupant of the name ends up
with `system.tables.dependencies_table` empty. `InsertDependenciesBuilder`
then visits no MVs on the next insert and rows are silently lost.

Tag the synthetic rename built by `doCreateOrReplaceTable` with a new
`ASTRenameQuery::is_create_or_replace` flag and, when set, restore each
harvested dependent-view list onto the table id it came from instead
of swapping them. `EXCHANGE TABLES a, b` keeps its existing semantics.

Regression introduced in ClickHouse#98779
@clickhouse-gh

clickhouse-gh Bot commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ef11941]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_msan, WasmEdge, parallel, 1/2) FAIL
04039_merge_tree_snapshot_teardown_race FAIL cidb, issue
03823_optimize_dry_run_patch_parts FAIL cidb
04045_reverse_order_virtual_row FAIL cidb
Unit tests (asan_ubsan, function_prop_fuzzer) FAIL
FunctionsStress.stress FAIL cidb, issue
AllTests FAIL cidb

AI Review

Summary

This PR fixes a real correctness bug in CREATE OR REPLACE TABLE/REPLACE TABLE by keeping source-side view dependencies attached to the surviving table name instead of the displaced table that gets dropped after the internal swap. The change is localized, the dependency transfer logic in InterpreterRenameQuery matches the intended contract for replace flows, and the new stateless regression test covers both CREATE OR REPLACE TABLE and REPLACE TABLE behavior.

Final Verdict

✅ No blocking issues found in the current diff.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 14, 2026
@serprex
serprex requested a review from alexey-milovidov May 14, 2026 04:47
@clickhouse-gh

clickhouse-gh Bot commented May 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 92.00% 92.00% +0.00%
Branches 76.60% 76.50% -0.10%

Changed lines: 100.00% (16/16) · Uncovered code

Full report · Diff report

@CheSema CheSema self-assigned this May 14, 2026

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.

Not totally sure but by the symmetry it might have required different rollback according if (rename.is_create_or_replace) is true or not.

@CheSema

CheSema commented May 15, 2026

Copy link
Copy Markdown
Member

close in favor of #105029

@CheSema CheSema closed this May 15, 2026
pull Bot pushed a commit to CrazyForks/ClickHouse that referenced this pull request May 18, 2026
`EXCHANGE TABLES a, b` (and the synthetic exchange used by
`CREATE OR REPLACE TABLE` / `REPLACE TABLE`) cross-swapped the
source-side `view_dependencies` edges along with the data. The
`MV`'s stored `select_table_id` is not rewritten by the rename,
so cross-swapping orphaned the `MV`:

* `INSERT INTO a` after exchange found no dependents — the edge
  had been moved to `b`.
* `INSERT INTO b` was rejected by
  `InsertDependenciesBuilder::observePath` because the `MV`'s
  recorded source name `a` no longer matched the parent `b`.

Both inserts silently dropped rows.

Restore the pre-ClickHouse#98779 semantics for `EXCHANGE`: keep source-view
edges keyed by the original name. The `MV` continues to read from
whatever now lives at the name it was created against. Plain
`RENAME TABLE` keeps the post-ClickHouse#98779 re-keying behavior used by
`01155_rename_move_materialized_view`.

This also subsumes the special case from ClickHouse#104907 — the synthetic
`CREATE OR REPLACE` exchange routes through the same
`exchange_tables=true` branch.

Tests:

* `04240_exchange_tables_drops_mv_source_dependencies` —
  regression test for ClickHouse#105021. Now passes; `MV` continues to feed
  `dst` after `EXCHANGE TABLES`. Reference updated to the fixed
  behavior.
* `04235_create_or_replace_source_table_view_dependencies` —
  cherry from ClickHouse#104907 (originally authored by @serprex). Passes
  with the unified fix.
* `04241_rename_table_mv_source_dependencies` — pins a
  pre-existing latent bug in plain same-database `RENAME TABLE`
  (silent data loss, alive-but-orphaned `MV`). Not a ClickHouse#98779
  regression; not fixed by this PR. Root cause shared with
  ClickHouse#76303; out of scope.

The chosen behavior here ("option C": edges follow the name) is a
regression repair — it matches v26.4. A future redesign to
"option A" (identity-bound, atomic `MV.as_select` rewrite, like
PostgreSQL) is tracked separately.

Closes ClickHouse#105021

Co-Authored-By: Philip Dubé <[email protected]>
Co-Authored-By: Claude Opus 4.7 (1M context) <[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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CREATE OR REPLACE breaks MV

2 participants