diff --git a/e2e_test/ddl/alter_rename.slt b/e2e_test/ddl/alter_rename.slt index b614d03e6929..6d94bed9bb86 100644 --- a/e2e_test/ddl/alter_rename.slt +++ b/e2e_test/ddl/alter_rename.slt @@ -280,3 +280,43 @@ DROP TABLE t2; statement ok DROP TABLE t_as_1; + +# BEGIN references in changelog + +statement ok +CREATE TABLE a (a0 int); + +statement ok +CREATE TABLE b (b0 int); + +statement ok +CREATE MATERIALIZED VIEW mv AS +WITH a_log AS changelog FROM a, + b_log AS changelog FROM b +SELECT 'a' AS tbl, * FROM a_log +UNION ALL +SELECT 'b', * FROM b_log; + +query T +SELECT definition FROM rw_materialized_views WHERE name = 'mv'; +---- +CREATE MATERIALIZED VIEW mv AS WITH a_log AS changelog from a, b_log AS changelog from b SELECT 'a' AS tbl, * FROM a_log UNION ALL SELECT 'b', * FROM b_log + +statement ok +ALTER TABLE a RENAME TO c; + +query T +SELECT definition FROM rw_materialized_views WHERE name = 'mv'; +---- +CREATE MATERIALIZED VIEW mv AS WITH a_log AS changelog from "c", b_log AS changelog from b SELECT 'a' AS tbl, * FROM a_log UNION ALL SELECT 'b', * FROM b_log + +statement ok +DROP MATERIALIZED VIEW mv; + +statement ok +DROP TABLE b; + +statement ok +DROP TABLE c; + +# END references in changelog diff --git a/src/meta/src/controller/rename.rs b/src/meta/src/controller/rename.rs index 7f387525e93e..57746df2222c 100644 --- a/src/meta/src/controller/rename.rs +++ b/src/meta/src/controller/rename.rs @@ -155,8 +155,11 @@ impl QueryRewriter<'_> { for cte_table in &mut with.cte_tables { match &mut cte_table.cte_inner { risingwave_sqlparser::ast::CteInner::Query(query) => self.visit_query(query), - risingwave_sqlparser::ast::CteInner::ChangeLog(from) => { - *from = ObjectName(vec![Ident::new_unchecked(self.to)]) + risingwave_sqlparser::ast::CteInner::ChangeLog(name) => { + let idx = name.0.len() - 1; + if name.0[idx].real_value() == self.from { + name.0[idx] = Ident::with_quote_unchecked('"', self.to); + } } } }