ClickHouse / ClickHouse/ClickHouse

`RENAME DATABASE` query doesn't work for materialized views

Open
#72,380 6 comments 1 reaction 1 assignee Claimed by @shankar-iyer View on GitHub
comp-materialized-view external potential bug
Dominant language
C++
Stars
49.9k
Forks
9k
Avg merge
21h 32m
Merged PRs (30d)
515

Description

**Company or project name**

Prefer not to specify

**Describe what's wrong**

`RENAME DATABASE` query doesn't seem to be working correctly with materialized view statements.
After renaming a database with `RENAME DATABASE old_name TO new_name` query, the materialized views would still reference tables with the `old_name` as could be seen with `SHOW CREATE new_name.materialized_view` statement in the repro. That leads to a bunch of issues with database permissions and `SELECT` queries. And while `INSERT` queries into the main table seem to be working, they are not triggering any relative materialized views.

Repro: https://fiddle.clickhouse.com/797f730d-d037-4b64-8d17-ce47911fccdd

**Does it reproduce on the most recent release?**

Yes

**Enable crash reporting**

Doesn't crash

**How to reproduce**

I first got this on `24.8.1.10452` in ClickHouse Cloud, but I'm also able to reproduce it on current latest.

```
CREATE DATABASE test;

CREATE TABLE test.sample (
id UUID DEFAULT generateUUIDv4(),
data TEXT DEFAULT ''
)
ENGINE = MergeTree
PRIMARY KEY (id)
ORDER BY (id);

-- Create materialized view without explicitly specifying target table
CREATE MATERIALIZED VIEW test.inline_mat_view (
uuid UUID,
data TEXT
)
ENGINE MergeTree
ORDER BY (uuid)
AS SELECT
id as uuid,
data
FROM test.sample;

-- Create table for materialized view explicitly
CREATE TABLE test.explicit_table (
uuid UUID,
data TEXT
)
ENGINE MergeTree
ORDER BY (uuid);

-- Create materialized view pointing to explicitly created table
CREATE MATERIALIZED VIEW test.explicit_mat_view TO test.explicit_table
AS SELECT
id as uuid,
data
FROM test.sample;

-- Output original CREATE statements for both materialized views
SHOW CREATE test.inline_mat_view;
SHOW CREATE test.explicit_mat_view;

RENAME DATABASE test TO dev;

-- Inserting data into main table doesn't trigger any error, but materialized views are not updated
INSERT INTO dev.sample (data)
VALUES
('test1'),
('test2'),
('test3'),
('test4'),
('test5');

-- CREATE statements for materialized views after renaming the database still points to the old database name
SHOW CREATE dev.inline_mat_view;
SHOW CREATE dev.explicit_mat_view;

-- Exception due to missing 'test' database
SELECT * FROM dev.inline_mat_view;
SELECT * FROM dev.explicit_mat_view;
```

**Expected behavior**

When renaming the database I would expect materialized view to be updated accordingly and to point to the correct table in the renamed database.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.