Skip to content

fix(query): allow create Materialized View with Table in different Database - #20906

Open
b41sh wants to merge 3 commits into
databendlabs:mainfrom
b41sh:fix-mv-diff-db
Open

b41sh wants to merge 3 commits into
databendlabs:mainfrom
b41sh:fix-mv-diff-db

Conversation

@b41sh

@b41sh b41sh commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

I hereby agree to the terms of the CLA available at: https://docs.databend.com/dev/policies/cla/

Summary

  • Allow creating a materialized view in a different database from its source table, e.g. CREATE MATERIALIZED VIEW db2.mv AS SELECT c1 FROM db1.t WHERE 1 = 1. The source still has to be in the same catalog as the MV.
  • Query rewrite: when looking up an MV's database, use mget_database_names_by_ids instead of get_db_name_by_id. A dropped database keeps its id-to-name mapping until GC, and get_db_name_by_id still returns that name. Before this change, dropping the MV's database made every query on the source table fail with UnknownDatabase. MVs in a dropped database are now skipped, the same as dropped MVs.
  • system.materialized_views: list_materialized_views_impl now reads the source database's DatabaseMeta in the same MGET. An MV whose source database is dropped is reported as invalid = 1 instead of 0.
  • Tests: 05_0017_ddl_materialized_view.test covers creating a cross-database MV, refreshing it, reading it, the invalid state after DROP DATABASE and UNDROP DATABASE on the source database, and querying the source after the MV's database is dropped.

fixes: #[Link the issue here]

Tests

  • Unit Test
  • Logic Test
  • Benchmark Test
  • No Test - Explain why

Type of change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Breaking Change (fix or feature that could cause existing functionality not to work as expected)
  • Documentation Update
  • Refactoring
  • Performance Improvement
  • Other (please describe):

AI assistance

  • AI usage: An AI coding agent drafted the binder and meta changes and the logic test updates
  • Responsible human: @b41sh
  • The responsible human has read every line of this diff and can explain each change

This change is Reviewable

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T13:09:12.228574Z dbe8814 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions github-actions Bot added the pr-bugfix this PR patches a bug in codebase label Oct 30, 2026
@b41sh
b41sh requested a review from TCeason October 30, 2026 13:02

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dbe881496b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

⏱ CI timing · Dev

run 41074104023 · attempt 2 (wall time includes re-run gap) · 53.0 min (main p50 63.3 min, −10.3 min)

stage this run main p50 Δ
total 53.0 min 63.3 min −10.3 min
build (aarch64, ci profile) 21.0 min 23.4 min −2.4 min
├ deps + crates (parallel) 4.1 min — —
├ databend-query codegen 2.9 min — —
├ databend-binaries link 11.9 min — —
└ sccache hit rate 98.5 % — —
test_unit 18.1 min 17.6 min +0.5 min
mac_check — 13.1 min —
tests after build (49 jobs) 29.6 min 30.4 min −0.8 min
├ queue p50 / max 3.9 min / 20.8 min 4.9 min / 26.9 min −1.1 min
├ sqllogic standalone (8 jobs, max) 4.4 min 4.3 min +0.1 min
├ sqllogic cluster (8 jobs, max) 7.9 min 7.2 min +0.7 min
├ sqllogic stage (2 jobs, max) 6.5 min 5.0 min +1.5 min
├ sqllogic other (10 jobs, max) 5.9 min 5.7 min +0.2 min
├ stateless (2 jobs, max) 7.1 min 6.7 min +0.4 min
├ stateful (4 jobs, max) 5.1 min 4.8 min +0.4 min
├ compat (6 jobs, max) 6.4 min 5.7 min +0.8 min
├ meta / logs / lineage / udf (5 jobs, max) 4.6 min 4.4 min +0.1 min
├ ee (2 jobs, max) 1.6 min 1.3 min +0.3 min
├ private_tasks (1 jobs, max) 8.8 min 8.8 min −0.0 min
└ fuzz (1 jobs, max) 1.3 min 1.1 min +0.2 min

Minutes unless stated; durations exclude queue time unless stated; "max" = longest job in the group. Baseline = median of the last 10 successful runs (Dev: Main on main; Cloud: recent Cloud runs). Build breakdown has no baseline. Updated on each push.

This branch has not been deployed

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

Labels

pr-bugfix this PR patches a bug in codebase

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant