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

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new fe1d0c1  [fix](materialized-view)(planner) fix mv rewrite bug (#7362)
fe1d0c1 is described below

commit fe1d0c1428823ab7480e71f25d97c58111e9e5cf
Author: tinkerrrr <62875019+tinker...@users.noreply.github.com>
AuthorDate: Sun Dec 26 11:00:39 2021 +0800

    [fix](materialized-view)(planner) fix mv rewrite bug (#7362)
    
    Close related [#7361]
    
    As the sql described in 
[#7361](https://github.com/apache/incubator-doris/issues/7361)
    
    ```
    select k1, count(k2) / count(1) from UserTable group by k1
    ```
    
    Before this pr, `count(k2) / count(1)` will be rewritten as 
`sum(UserTable.mv_count_k2) / count(1)`,
    and will be kept in second-round analyze, which could cause mv select fail.
    
    After this pr, `count(k2) / count(1)` will still be rewritten as 
`sum(UserTable.mv_count_k2) / count(1)`,
    but won't be kept in second-round analyze, so query could successfully run.
---
 .../java/org/apache/doris/analysis/QueryStmt.java  |  3 ++
 .../java/org/apache/doris/analysis/SelectStmt.java | 11 +++++++
 .../apache/doris/analysis/SetOperationStmt.java    |  7 ++++
 .../java/org/apache/doris/qe/StmtExecutor.java     | 11 +++++++
 .../planner/MaterializedViewFunctionTest.java      | 37 ++++++++++++++++------
 5 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java
index e029571..db0abfd 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/QueryStmt.java
@@ -713,6 +713,9 @@ public abstract class QueryStmt extends StatementBase {
         fromInsert = false;
     }
 
+    // DORIS-7361, Reset selectList to keep clean
+    public abstract void resetSelectList();
+
     public void setFromInsert(boolean value) {
         this.fromInsert = value;
     }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
index 813e0b5..0c25894 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
@@ -113,6 +113,9 @@ public class SelectStmt extends QueryStmt {
     // Table alias generator used during query rewriting.
     private TableAliasGenerator tableAliasGenerator = null;
 
+    // Members that need to be reset to origin
+    private SelectList originSelectList;
+
     public SelectStmt(ValueList valueList, ArrayList<OrderByElement> 
orderByElement, LimitElement limitElement) {
         super(orderByElement, limitElement);
         this.valueList = valueList;
@@ -131,6 +134,7 @@ public class SelectStmt extends QueryStmt {
             LimitElement limitElement) {
         super(orderByElements, limitElement);
         this.selectList = selectList;
+        this.originSelectList = selectList.clone();
         if (fromClause == null) {
             fromClause_ = new FromClause();
         } else {
@@ -188,6 +192,13 @@ public class SelectStmt extends QueryStmt {
     }
 
     @Override
+    public void resetSelectList() {
+        if (originSelectList != null) {
+            selectList = originSelectList;
+        }
+    }
+
+    @Override
     public QueryStmt clone() {
         return new SelectStmt(this);
     }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java
index 0d43295..5e345a1 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SetOperationStmt.java
@@ -155,6 +155,13 @@ public class SetOperationStmt extends QueryStmt {
         setOpsResultExprs_.clear();
     }
 
+    @Override
+    public void resetSelectList() {
+        for (SetOperand operand : operands) {
+            operand.getQueryStmt().resetSelectList();
+        }
+    }
+
     public List<SetOperand> getOperands() { return operands; }
     public List<SetOperand> getDistinctOperands() { return distinctOperands_; }
     public boolean hasDistinctOps() { return !distinctOperands_.isEmpty(); }
diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java 
b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
index 7222a24..c519332 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java
@@ -684,6 +684,17 @@ public class StmtExecutor implements ProfileWriter {
         analyzer = new Analyzer(context.getCatalog(), context);
 
         parsedStmt.reset();
+
+        // DORIS-7361
+        // Need to reset selectList before second-round analyze, because exprs 
in selectList could be rewritten by mvExprRewriter
+        // in first-round analyze, which could cause analyze failure.
+        if (parsedStmt instanceof QueryStmt) {
+            ((QueryStmt) parsedStmt).resetSelectList();
+        }
+
+        if (parsedStmt instanceof InsertStmt) {
+            ((InsertStmt) parsedStmt).getQueryStmt().resetSelectList();
+        }
     }
 
     // Because this is called by other thread
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/planner/MaterializedViewFunctionTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/planner/MaterializedViewFunctionTest.java
index 3f65554..09d28a7 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/planner/MaterializedViewFunctionTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/planner/MaterializedViewFunctionTest.java
@@ -288,6 +288,34 @@ public class MaterializedViewFunctionTest {
         dorisAssert.query(query).explainContains(QUERY_USE_EMPS_MV);
     }
 
+    /**
+     * Aggregation query with two aggregation operators
+     */
+    @Test
+    public void testAggQueryOnAggMV11() throws Exception {
+        String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as 
select deptno, count(salary) "
+                + "from " + EMPS_TABLE_NAME + " group by deptno;";
+        String query = "select deptno, count(salary) + count(1) from " + 
EMPS_TABLE_NAME
+                + " group by deptno;";
+        dorisAssert.withMaterializedView(createMVSQL);
+        dorisAssert.query(query).explainContains(QUERY_USE_EMPS);
+    }
+
+    /**
+     * Aggregation query with set operand
+     */
+    @Test
+    public void testAggQueryWithSetOperandOnAggMV() throws Exception {
+        String createMVSQL = "create materialized view " + EMPS_MV_NAME + " as 
select deptno, count(salary) "
+                + "from " + EMPS_TABLE_NAME + " group by deptno;";
+        String query = "select deptno, count(salary) + count(1) from " + 
EMPS_TABLE_NAME
+                + " group by deptno union " +
+                "select deptno, count(salary) + count(1) from " + 
EMPS_TABLE_NAME
+                + " group by deptno;";
+        dorisAssert.withMaterializedView(createMVSQL);
+        dorisAssert.query(query).explainContains(QUERY_USE_EMPS);
+    }
+
     @Test
     public void testJoinOnLeftProjectToJoin() throws Exception {
         String createEmpsMVSQL = "create materialized view " + EMPS_MV_NAME
@@ -413,15 +441,6 @@ public class MaterializedViewFunctionTest {
     }
 
     @Test
-    public void testQueryOnStarAndJoin() throws Exception {
-        String createEmpsMVSQL = "create materialized view " + EMPS_MV_NAME + 
" as select time, deptno, empid, name, " +
-                "salary, commission from " + EMPS_TABLE_NAME + " order by 
time, deptno, empid;";
-        String query = "select * from " + EMPS_TABLE_NAME + " join depts on " 
+ EMPS_TABLE_NAME + ".deptno = " +
-                DEPTS_TABLE_NAME + ".deptno";
-        
dorisAssert.withMaterializedView(createEmpsMVSQL).query(query).explainContains(QUERY_USE_EMPS_MV);
-    }
-
-    @Test
     public void testAggregateMVAggregateFuncs1() throws Exception {
         String createEmpsMVSQL = "create materialized view " + EMPS_MV_NAME + 
" as select empid, deptno, sum(salary) "
                 + "from " + EMPS_TABLE_NAME + " group by empid, deptno;";

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

Reply via email to