This is an automated email from the ASF dual-hosted git repository. morrysnow pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push: new 9119d2180c3 [fix](mv) Fix sync mv def sql parse not set the db when create (#48162) 9119d2180c3 is described below commit 9119d2180c3a2c926798ec1f4a0f8ee29783eac0 Author: seawinde <w...@selectdb.com> AuthorDate: Mon Feb 24 12:11:50 2025 +0800 [fix](mv) Fix sync mv def sql parse not set the db when create (#48162) ### What problem does this PR solve? Problem Summary: Fix sync mv def sql parse not set the db when create Such as table schema is as following create database db1; use db1; create table d_table( k1 int null, k2 int not null, k3 bigint null, k4 varchar(100) null ) duplicate key (k1,k2,k3) distributed BY hash(k1) buckets 3 properties("replication_num" = "1"); create materialized view mv1 as select abs(k1)+k2+1,sum(abs(k2+2)+k3+3) from d_table group by abs(k1)+k2+1; when we create sync mv, this sync mv is on the `d_table` in `db1`, when we use anther database such as `db2` if we run such query as following,would rewrite fail and the result is wrong select abs(k1)+k2+1,sum(abs(k2+2)+k3+3) from db1.d_table group by abs(k1)+k2+1; ### Release note Fix sync mv def sql parse not set the db when create --- .../doris/catalog/MaterializedIndexMeta.java | 4 ++ .../main/java/org/apache/doris/mtmv/MTMVCache.java | 11 ++-- .../mv/InitMaterializationContextHook.java | 5 +- .../query_in_different_db.out | Bin 0 -> 172 bytes .../query_in_different_db.groovy | 69 +++++++++++++++++++++ 5 files changed, 81 insertions(+), 8 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java index 5dd5776c761..b0d483751d7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java @@ -154,6 +154,10 @@ public class MaterializedIndexMeta implements Writable, GsonPostProcessable { this.indexes = newIndexes; } + public String getDbName() { + return dbName; + } + public List<Column> getSchema() { return getSchema(true); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java index 517acb048e0..bc99fe8bfd1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVCache.java @@ -110,6 +110,9 @@ public class MTMVCache { NereidsPlanner planner = new NereidsPlanner(mvSqlStatementContext); boolean originalRewriteFlag = createCacheContext.getSessionVariable().enableMaterializedViewRewrite; createCacheContext.getSessionVariable().enableMaterializedViewRewrite = false; + Plan originPlan; + Plan mvPlan; + Optional<StructInfo> structInfoOptional; try { // Can not convert to table sink, because use the same column from different table when self join // the out slot is wrong @@ -120,13 +123,6 @@ public class MTMVCache { // No need cost for performance planner.planWithLock(unboundMvPlan, PhysicalProperties.ANY, ExplainLevel.REWRITTEN_PLAN); } - } finally { - createCacheContext.getSessionVariable().enableMaterializedViewRewrite = originalRewriteFlag; - } - Plan originPlan; - Plan mvPlan; - Optional<StructInfo> structInfoOptional; - try { originPlan = planner.getCascadesContext().getRewritePlan(); // Eliminate result sink because sink operator is useless in query rewrite by materialized view // and the top sort can also be removed @@ -150,6 +146,7 @@ public class MTMVCache { planner.getCascadesContext(), new BitSet()); } finally { + createCacheContext.getSessionVariable().enableMaterializedViewRewrite = originalRewriteFlag; if (currentContext != null) { currentContext.setThreadLocalInfo(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java index 765cf620576..63b336e8eca 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/InitMaterializationContextHook.java @@ -257,8 +257,11 @@ public class InitMaterializationContextHook implements PlannerHook { LOG.warn(String.format("can't parse %s ", createMvSql)); continue; } + ConnectContext basicMvContext = MTMVPlanUtil.createBasicMvContext( + cascadesContext.getConnectContext()); + basicMvContext.setDatabase(meta.getDbName()); MTMVCache mtmvCache = MTMVCache.from(querySql.get(), - MTMVPlanUtil.createBasicMvContext(cascadesContext.getConnectContext()), true, + basicMvContext, true, false, cascadesContext.getConnectContext()); contexts.add(new SyncMaterializationContext(mtmvCache.getLogicalPlan(), mtmvCache.getOriginalPlan(), olapTable, meta.getIndexId(), indexName, diff --git a/regression-test/data/mv_p0/query_in_different_db/query_in_different_db.out b/regression-test/data/mv_p0/query_in_different_db/query_in_different_db.out new file mode 100644 index 00000000000..1bd5dfd6530 Binary files /dev/null and b/regression-test/data/mv_p0/query_in_different_db/query_in_different_db.out differ diff --git a/regression-test/suites/mv_p0/query_in_different_db/query_in_different_db.groovy b/regression-test/suites/mv_p0/query_in_different_db/query_in_different_db.groovy new file mode 100644 index 00000000000..1e8d5cf21c7 --- /dev/null +++ b/regression-test/suites/mv_p0/query_in_different_db/query_in_different_db.groovy @@ -0,0 +1,69 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite ("query_in_different_db") { + + String db = context.config.getDbNameByFile(context.file) + sql "use ${db}" + sql """ DROP TABLE IF EXISTS d_table; """ + sql """ + create table d_table( + k1 int null, + k2 int not null, + k3 bigint null, + k4 varchar(100) null + ) + duplicate key (k1,k2,k3) + distributed BY hash(k1) buckets 3 + properties("replication_num" = "1"); + """ + + sql "insert into d_table select 1,1,1,'a';" + sql "insert into d_table select 1,1,1,'a';" + sql "insert into d_table select 1,1,1,'a';" + sql "insert into d_table select 2,2,2,'b';" + sql "insert into d_table select 2,2,2,'b';" + sql "insert into d_table select 2,2,2,'b';" + sql "insert into d_table select 3,-3,null,'c';" + sql "insert into d_table select 3,-3,null,'c';" + sql "insert into d_table select 3,-3,null,'c';" + sql "insert into d_table select -4,-4,-4,'d';" + sql "insert into d_table select -4,-4,-4,'d';" + sql "insert into d_table select -4,-4,-4,'d';" + + create_sync_mv(db, "d_table", "mv_in_${db}", """ + select abs(k1)+k2+1,sum(abs(k2+2)+k3+3) from d_table group by abs(k1)+k2+1 + """) + + sql "analyze table d_table with sync;" + sql """alter table d_table modify column k1 set stats ('row_count'='12');""" + // use another db, mv rewrite should be correct + sql """drop database IF EXISTS test_query_in_different_db""" + + sql """ + create database test_query_in_different_db; + """ + sql """ + use test_query_in_different_db; + """ + + // query with index should success + order_qt_select_with_index "select * from ${db}.d_table index mv_in_${db}" + + mv_rewrite_success("select abs(k1)+k2+1,sum(abs(k2+2)+k3+3) from ${db}.d_table group by abs(k1)+k2+1", "mv_in_${db}") + order_qt_select_mv "select abs(k1)+k2+1,sum(abs(k2+2)+k3+3) from ${db}.d_table group by abs(k1)+k2+1;" +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org