From 1cdf3f3369b2c3af87cf3495c2f607871cb14f92 Mon Sep 17 00:00:00 2001 From: xiangjinwu <17769960+xiangjinwu@users.noreply.github.com> Date: Mon, 21 Oct 2024 16:27:16 +0800 Subject: [PATCH] fix(meta): rename changelog shall check `self.from` (#19030) --- e2e_test/ddl/alter_rename.slt | 40 +++++++++++++++++++++++++++++++ src/meta/src/controller/rename.rs | 7 ++++-- 2 files changed, 45 insertions(+), 2 deletions(-) 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); + } } } }