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

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 856b64e9747 [opt](mtmv) Support single mv rewrite when query is select 
star (#48742)
856b64e9747 is described below

commit 856b64e974795a191de4505f00a97cf5dd77132b
Author: seawinde <w...@selectdb.com>
AuthorDate: Sat Mar 8 16:25:57 2025 +0800

    [opt](mtmv) Support single mv rewrite when query is select star (#48742)
---
 .../org/apache/doris/nereids/rules/RuleSet.java    |   2 +
 .../org/apache/doris/nereids/rules/RuleType.java   |   1 +
 .../mv/AbstractMaterializedViewScanRule.java       |  84 +++++++++++++++++++++
 .../mv/MaterializedViewOnlyScanRule.java           |  42 +++++++++++
 .../data/nereids_rules_p0/mv/scan/scan_table.out   | Bin 1263 -> 2100 bytes
 .../nereids_rules_p0/mv/scan/scan_table.groovy     |  22 +++++-
 6 files changed, 147 insertions(+), 4 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java
index 0c84cf4015b..06f95814007 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleSet.java
@@ -49,6 +49,7 @@ import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewFilterProje
 import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewFilterProjectScanRule;
 import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewFilterScanRule;
 import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewOnlyJoinRule;
+import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewOnlyScanRule;
 import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewProjectAggregateRule;
 import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewProjectFilterAggregateRule;
 import 
org.apache.doris.nereids.rules.exploration.mv.MaterializedViewProjectFilterJoinRule;
@@ -270,6 +271,7 @@ public class RuleSet {
             .add(MaterializedViewProjectScanRule.INSTANCE)
             .add(MaterializedViewProjectFilterScanRule.INSTANCE)
             .add(MaterializedViewAggregateOnNoneAggregateRule.INSTANCE)
+            .add(MaterializedViewOnlyScanRule.INSTANCE)
             .build();
 
     public static final List<Rule> DPHYP_REORDER_RULES = 
ImmutableList.<Rule>builder()
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
index 6e9bf6f118d..776681fd889 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java
@@ -406,6 +406,7 @@ public enum RuleType {
     MATERIALIZED_VIEW_PROJECT_SCAN(RuleTypeClass.EXPLORATION),
     MATERIALIZED_VIEW_FILTER_PROJECT_SCAN(RuleTypeClass.EXPLORATION),
     MATERIALIZED_VIEW_PROJECT_FILTER_SCAN(RuleTypeClass.EXPLORATION),
+    MATERIALIZED_VIEW_ONLY_SCAN(RuleTypeClass.EXPLORATION),
 
     // implementation rules
     
LOGICAL_ONE_ROW_RELATION_TO_PHYSICAL_ONE_ROW_RELATION(RuleTypeClass.IMPLEMENTATION),
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewScanRule.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewScanRule.java
new file mode 100644
index 00000000000..7cd49c94b09
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewScanRule.java
@@ -0,0 +1,84 @@
+// 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.
+
+package org.apache.doris.nereids.rules.exploration.mv;
+
+import org.apache.doris.nereids.CascadesContext;
+import 
org.apache.doris.nereids.rules.exploration.mv.StructInfo.PlanCheckContext;
+import org.apache.doris.nereids.rules.exploration.mv.mapping.SlotMapping;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+
+import com.google.common.collect.ImmutableSet;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * This is responsible for single table rewriting according to different 
pattern
+ * */
+public abstract class AbstractMaterializedViewScanRule extends 
AbstractMaterializedViewRule {
+
+    @Override
+    protected Plan rewriteQueryByView(MatchMode matchMode,
+            StructInfo queryStructInfo,
+            StructInfo viewStructInfo,
+            SlotMapping targetToSourceMapping,
+            Plan tempRewritedPlan,
+            MaterializationContext materializationContext,
+            CascadesContext cascadesContext) {
+        // Rewrite top projects, represent the query projects by view
+        List<Expression> expressionsRewritten = rewriteExpression(
+                queryStructInfo.getExpressions(),
+                queryStructInfo.getTopPlan(),
+                materializationContext.getShuttledExprToScanExprMapping(),
+                targetToSourceMapping,
+                queryStructInfo.getTableBitSet()
+        );
+        // Can not rewrite, bail out
+        if (expressionsRewritten.isEmpty()) {
+            materializationContext.recordFailReason(queryStructInfo,
+                    "Rewrite expressions by view in scan fail",
+                    () -> String.format("expressionToRewritten is %s,\n 
mvExprToMvScanExprMapping is %s,\n"
+                                    + "targetToSourceMapping = %s", 
queryStructInfo.getExpressions(),
+                            
materializationContext.getShuttledExprToScanExprMapping(),
+                            targetToSourceMapping));
+            return null;
+        }
+        return new LogicalProject<>(
+                expressionsRewritten.stream()
+                        .map(expression -> expression instanceof 
NamedExpression ? expression : new Alias(expression))
+                        .map(NamedExpression.class::cast)
+                        .collect(Collectors.toList()),
+                tempRewritedPlan);
+    }
+
+    /**
+     * Check scan is whether valid or not. Support join's input only support 
project, filter, join,
+     * logical relation, simple aggregate node. Con not have aggregate above 
on join.
+     * Join condition should be slot reference equals currently.
+     */
+    @Override
+    protected boolean checkQueryPattern(StructInfo structInfo, CascadesContext 
cascadesContext) {
+        PlanCheckContext checkContext = PlanCheckContext.of(ImmutableSet.of());
+        return 
structInfo.getTopPlan().accept(StructInfo.SCAN_PLAN_PATTERN_CHECKER, 
checkContext)
+                && !checkContext.isContainsTopAggregate();
+    }
+}
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewOnlyScanRule.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewOnlyScanRule.java
new file mode 100644
index 00000000000..24211ac8b08
--- /dev/null
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/MaterializedViewOnlyScanRule.java
@@ -0,0 +1,42 @@
+// 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.
+
+package org.apache.doris.nereids.rules.exploration.mv;
+
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * MaterializedViewOnlyScanRule
+ */
+public class MaterializedViewOnlyScanRule extends 
AbstractMaterializedViewScanRule {
+
+    public static final MaterializedViewOnlyScanRule INSTANCE = new 
MaterializedViewOnlyScanRule();
+
+    @Override
+    public List<Rule> buildRules() {
+        return ImmutableList.of(
+                
any().when(LogicalCatalogRelation.class::isInstance).thenApplyMultiNoThrow(ctx 
-> {
+                    return rewrite(ctx.root, ctx.cascadesContext);
+                }).toRule(RuleType.MATERIALIZED_VIEW_ONLY_SCAN));
+    }
+}
diff --git a/regression-test/data/nereids_rules_p0/mv/scan/scan_table.out 
b/regression-test/data/nereids_rules_p0/mv/scan/scan_table.out
index 9af8d1bfb63..cc7aa623aa8 100644
Binary files a/regression-test/data/nereids_rules_p0/mv/scan/scan_table.out and 
b/regression-test/data/nereids_rules_p0/mv/scan/scan_table.out differ
diff --git a/regression-test/suites/nereids_rules_p0/mv/scan/scan_table.groovy 
b/regression-test/suites/nereids_rules_p0/mv/scan/scan_table.groovy
index c42888e35ab..4e40d1854d5 100644
--- a/regression-test/suites/nereids_rules_p0/mv/scan/scan_table.groovy
+++ b/regression-test/suites/nereids_rules_p0/mv/scan/scan_table.groovy
@@ -116,7 +116,7 @@ suite("mv_scan_table") {
     insert into partsupp values
     (2, 3, 9, 10.01, 'supply1'),
     (2, 3, 10, 11.01, 'supply2');
-    
+
     """
 
     sql """analyze table orders with sync;"""
@@ -124,8 +124,8 @@ suite("mv_scan_table") {
     sql """analyze table partsupp with sync;"""
 
     sql """alter table orders modify column o_comment set stats 
('row_count'='8');"""
-   sql """alter table lineitem modify column l_comment set stats 
('row_count'='5');"""
-sql """alter table partsupp modify column ps_comment set stats 
('row_count'='2');"""
+    sql """alter table lineitem modify column l_comment set stats 
('row_count'='5');"""
+    sql """alter table partsupp modify column ps_comment set stats 
('row_count'='2');"""
 
     // with filter
     def mv1_0 =
@@ -186,4 +186,18 @@ sql """alter table partsupp modify column ps_comment set 
stats ('row_count'='2')
     async_mv_rewrite_success(db, mv1_3, query1_3, "mv1_3")
     order_qt_query1_3_after "${query1_3}"
     sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_3"""
-}
+
+    def mv1_4 =
+            """
+        select *
+        from lineitem
+        """
+    def query1_4 = """
+        select *
+        from lineitem
+        """
+    order_qt_query1_4_before "${query1_4}"
+    async_mv_rewrite_success_without_check_chosen(db, mv1_4, query1_4, "mv1_4")
+    order_qt_query1_4_after "${query1_4}"
+    sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_4"""
+}
\ No newline at end of file


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

Reply via email to