This is an automated email from the ASF dual-hosted git repository.

starocean999 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 5c038905110 [fix](mtmv) Fix rewrite wrongly when sync and async 
materialized view name is same (#37311)
5c038905110 is described below

commit 5c0389051108ff49d3141d41b831dc546e602945
Author: seawinde <149132972+seawi...@users.noreply.github.com>
AuthorDate: Mon Jul 8 10:51:08 2024 +0800

    [fix](mtmv) Fix rewrite wrongly when sync and async materialized view name 
is same (#37311)
    
    ## Proposed changes
    
    This is brought by https://github.com/apache/doris/pull/33699
    
    `AbstractMaterializedViewRule#isMaterializationValid` check would fail
    when sync and async materialized view name is same, because the
    Materialization valid check result is cached by name。this will cause the
    async materialized view check fail wrongly. This fix this by make sync
    materialized view name unique in every place.
---
 .../mv/AsyncMaterializationContext.java            |   7 +-
 .../exploration/mv/SyncMaterializationContext.java |   3 +-
 .../mv/same_name/sync_async_same_name.out          |  17 ++
 .../mv/same_name/sync_async_same_name.groovy       | 178 +++++++++++++++++++++
 4 files changed, 203 insertions(+), 2 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java
index eef0a36d301..f9c0261e0c9 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AsyncMaterializationContext.java
@@ -120,7 +120,12 @@ public class AsyncMaterializationContext extends 
MaterializationContext {
         if (!(relation instanceof PhysicalCatalogRelation)) {
             return false;
         }
-        return ((PhysicalCatalogRelation) relation).getTable() instanceof MTMV;
+        if (!(((PhysicalCatalogRelation) relation).getTable() instanceof 
MTMV)) {
+            return false;
+        }
+        return ((PhysicalCatalogRelation) 
relation).getTable().getFullQualifiers().equals(
+                this.getMaterializationQualifier()
+        );
     }
 
     public Plan getScanPlan() {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/SyncMaterializationContext.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/SyncMaterializationContext.java
index 1159952f7a2..781b7171407 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/SyncMaterializationContext.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/SyncMaterializationContext.java
@@ -72,7 +72,8 @@ public class SyncMaterializationContext extends 
MaterializationContext {
     @Override
     List<String> getMaterializationQualifier() {
         return ImmutableList.of(olapTable.getDatabase().getCatalog().getName(),
-                
ClusterNamespace.getNameFromFullName(olapTable.getDatabase().getFullName()), 
indexName);
+                
ClusterNamespace.getNameFromFullName(olapTable.getDatabase().getFullName()),
+                olapTable.getName(), indexName);
     }
 
     @Override
diff --git 
a/regression-test/data/nereids_rules_p0/mv/same_name/sync_async_same_name.out 
b/regression-test/data/nereids_rules_p0/mv/same_name/sync_async_same_name.out
new file mode 100644
index 00000000000..dde90507fdf
--- /dev/null
+++ 
b/regression-test/data/nereids_rules_p0/mv/same_name/sync_async_same_name.out
@@ -0,0 +1,17 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !query_mv_before --
+1      yy      77.50   33.50   9.50    5
+2      mi      57.40   56.20   1.20    2
+2      mm      43.20   43.20   43.20   1
+
+-- !query_mtmv_before --
+1      yy      0       0       11.50   11.50   11.50   1
+
+-- !query_mv_after --
+1      yy      77.50   33.50   9.50    5
+2      mi      57.40   56.20   1.20    2
+2      mm      43.20   43.20   43.20   1
+
+-- !query_mtmv_after --
+1      yy      0       0       11.50   11.50   11.50   1
+
diff --git 
a/regression-test/suites/nereids_rules_p0/mv/same_name/sync_async_same_name.groovy
 
b/regression-test/suites/nereids_rules_p0/mv/same_name/sync_async_same_name.groovy
new file mode 100644
index 00000000000..e8350d487a8
--- /dev/null
+++ 
b/regression-test/suites/nereids_rules_p0/mv/same_name/sync_async_same_name.groovy
@@ -0,0 +1,178 @@
+package mv.same_name
+// 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("sync_async_same_name") {
+    String db = context.config.getDbNameByFile(context.file)
+    sql "use ${db}"
+    sql "set runtime_filter_mode=OFF";
+    sql "SET ignore_shape_nodes='PhysicalDistribute,PhysicalProject'"
+
+    sql """
+    drop table if exists orders
+    """
+
+    sql """
+    CREATE TABLE IF NOT EXISTS orders  (
+      o_orderkey       INTEGER NOT NULL,
+      o_custkey        INTEGER NOT NULL,
+      o_orderstatus    CHAR(1) NOT NULL,
+      o_totalprice     DECIMALV3(15,2) NOT NULL,
+      o_orderdate      DATE NOT NULL,
+      o_orderpriority  CHAR(15) NOT NULL,  
+      o_clerk          CHAR(15) NOT NULL, 
+      o_shippriority   INTEGER NOT NULL,
+      O_COMMENT        VARCHAR(79) NOT NULL
+    )
+    DUPLICATE KEY(o_orderkey, o_custkey)
+    PARTITION BY RANGE(o_orderdate) (
+    PARTITION `day_2` VALUES LESS THAN ('2023-12-9'),
+    PARTITION `day_3` VALUES LESS THAN ("2023-12-11"),
+    PARTITION `day_4` VALUES LESS THAN ("2023-12-30")
+    )
+    DISTRIBUTED BY HASH(o_orderkey) BUCKETS 3
+    PROPERTIES (
+      "replication_num" = "1"
+    );
+    """
+
+    sql """
+    insert into orders values
+    (1, 1, 'o', 9.5, '2023-12-08', 'a', 'b', 1, 'yy'),
+    (1, 1, 'o', 10.5, '2023-12-08', 'a', 'b', 1, 'yy'),
+    (2, 1, 'o', 11.5, '2023-12-09', 'a', 'b', 1, 'yy'),
+    (3, 1, 'o', 12.5, '2023-12-10', 'a', 'b', 1, 'yy'),
+    (3, 1, 'o', 33.5, '2023-12-10', 'a', 'b', 1, 'yy'),
+    (4, 2, 'o', 43.2, '2023-12-11', 'c','d',2, 'mm'),
+    (5, 2, 'o', 56.2, '2023-12-12', 'c','d',2, 'mi'),
+    (5, 2, 'o', 1.2, '2023-12-12', 'c','d',2, 'mi');  
+    """
+
+    sql """analyze table orders with sync;"""
+
+    def check_rewrite_but_not_chose = { mv_sql, query_sql, mv_name ->
+
+        sql """DROP MATERIALIZED VIEW IF EXISTS ${mv_name}"""
+        sql"""
+        CREATE MATERIALIZED VIEW ${mv_name} 
+        BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL
+        DISTRIBUTED BY RANDOM BUCKETS 2
+        PROPERTIES ('replication_num' = '1') 
+        AS ${mv_sql}
+        """
+
+        def job_name = getJobName(db, mv_name);
+        waitingMTMVTaskFinished(job_name)
+        explain {
+            sql("${query_sql}")
+            check {result ->
+                def splitResult = result.split("MaterializedViewRewriteFail")
+                splitResult.length == 2 ? splitResult[0].contains(mv_name) : 
false
+            }
+        }
+    }
+
+    def common_mv_name = 'common_mv_name'
+
+    def mtmv_sql = """
+            select o_orderdate, o_shippriority, o_comment,
+            sum(o_totalprice) as sum_total,
+            max(o_totalprice) as max_total,
+            min(o_totalprice) as min_total,
+            count(*) as count_all,
+            bitmap_union(to_bitmap(case when o_shippriority > 1 and o_orderkey 
IN (1, 3) then o_custkey else null end)) cnt_1,
+            bitmap_union(to_bitmap(case when o_shippriority > 2 and o_orderkey 
IN (2) then o_custkey else null end)) as cnt_2
+            from orders
+            group by
+            o_orderdate,
+            o_shippriority,
+            o_comment;
+    """
+    def mtmv_query = """
+            select o_shippriority, o_comment,
+            count(distinct case when o_shippriority > 1 and o_orderkey IN (1, 
3) then o_custkey else null end) as cnt_1,
+            count(distinct case when O_SHIPPRIORITY > 2 and o_orderkey IN (2) 
then o_custkey else null end) as cnt_2,
+            sum(o_totalprice),
+            max(o_totalprice),
+            min(o_totalprice),
+            count(*)
+            from orders
+            where o_orderdate = '2023-12-09'
+            group by
+            o_shippriority,
+            o_comment;
+            """
+
+    def mv_query = """
+            select o_shippriority, o_comment,
+            sum(o_totalprice),
+            max(o_totalprice),
+            min(o_totalprice),
+            count(*)
+            from orders
+            group by
+            o_shippriority,
+            o_comment;
+            """
+
+
+    order_qt_query_mv_before "${mv_query}"
+    order_qt_query_mtmv_before "${mtmv_query}"
+
+
+    // create sync mv
+    sql """drop materialized view if exists ${common_mv_name} on orders;"""
+    createMV ("create materialized view ${common_mv_name} as ${mv_query};")
+
+    // create async mv
+    sql """DROP MATERIALIZED VIEW IF EXISTS ${common_mv_name}"""
+    sql"""
+        CREATE MATERIALIZED VIEW ${common_mv_name} 
+        BUILD IMMEDIATE REFRESH COMPLETE ON MANUAL
+        DISTRIBUTED BY RANDOM BUCKETS 2
+        PROPERTIES ('replication_num' = '1') 
+        AS ${mtmv_sql}
+        """
+    def job_name = getJobName(db, common_mv_name);
+    waitingMTMVTaskFinished(job_name)
+
+    // only async mv rewrite successfully
+    explain {
+        sql("${mtmv_query}")
+        check {result ->
+            def splitResult = result.split("MaterializedViewRewriteFail")
+            splitResult.length == 2 ? splitResult[0].contains(common_mv_name) 
: false
+        }
+    }
+
+    // both sync and async mv rewrite successfully
+    explain {
+        sql("${mv_query}")
+        check {result ->
+            def splitResult = result.split("MaterializedViewRewriteFail")
+            splitResult.length == 2 ? splitResult[0].contains(common_mv_name)
+                    && splitResult[0].contains("orders#${common_mv_name}") : 
false
+        }
+    }
+
+
+    order_qt_query_mv_after "${mv_query}"
+    order_qt_query_mtmv_after "${mtmv_query}"
+
+    sql """DROP MATERIALIZED VIEW IF EXISTS ${common_mv_name}"""
+    sql """drop materialized view if exists ${common_mv_name} on orders;"""
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to