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

commit 7e60369ba2eb0808acb532bfb73c807155e06dd7
Author: Pxl <[email protected]>
AuthorDate: Wed Jan 24 17:45:47 2024 +0800

    [Feature](materialized-view) support create mv with count(*) (#30313)
    
    support create mv with count(*)
---
 .../doris/analysis/CreateMaterializedViewStmt.java | 11 ++-
 .../data/mv_p0/count_star/count_star.out           | 32 ++++++++
 regression-test/data/mv_p0/sum_count/sum_count.out | 61 ++++++++++++++
 .../suites/mv_p0/count_star/count_star.groovy      | 77 ++++++++++++++++++
 .../suites/mv_p0/sum_count/sum_count.groovy        | 92 ++++++++++++++++++++++
 5 files changed, 271 insertions(+), 2 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java
 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java
index 4122b4f0d09..2631f7c4e1a 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateMaterializedViewStmt.java
@@ -478,11 +478,18 @@ public class CreateMaterializedViewStmt extends DdlStmt {
         }
     }
 
+    private Expr getAggfunctionSlot(FunctionCallExpr functionCallExpr) throws 
AnalysisException {
+        if (functionCallExpr.getFnParams() != null && 
functionCallExpr.getFnParams().isStar()) {
+            // convert count(*) to count(1)
+            return LiteralExpr.create("1", Type.BIGINT);
+        }
+        return functionCallExpr.getChildren().get(0);
+    }
+
     private MVColumnItem buildMVColumnItem(Analyzer analyzer, FunctionCallExpr 
functionCallExpr)
             throws AnalysisException {
         String functionName = functionCallExpr.getFnName().getFunction();
-        List<Expr> childs = functionCallExpr.getChildren();
-        Expr defineExpr = childs.get(0);
+        Expr defineExpr = getAggfunctionSlot(functionCallExpr);
         Type baseType = defineExpr.getType();
         AggregateType mvAggregateType = null;
         Type type;
diff --git a/regression-test/data/mv_p0/count_star/count_star.out 
b/regression-test/data/mv_p0/count_star/count_star.out
new file mode 100644
index 00000000000..2ba7c948535
--- /dev/null
+++ b/regression-test/data/mv_p0/count_star/count_star.out
@@ -0,0 +1,32 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !select_star --
+-4     -4      -4      d
+1      1       1       a
+1      1       1       a
+1      1       1       a
+2      1       1       a
+2      1       1       a
+2      2       2       b
+3      -3      \N      c
+3      2       \N      c
+
+-- !select_mv --
+-4     d       1
+1      a       3
+2      a       2
+2      b       1
+3      c       2
+
+-- !select_mv --
+1      a       3
+
+-- !select_mv --
+1      a       3
+2      a       2
+
+-- !select_mv --
+9
+
+-- !select_mv --
+5
+
diff --git a/regression-test/data/mv_p0/sum_count/sum_count.out 
b/regression-test/data/mv_p0/sum_count/sum_count.out
new file mode 100644
index 00000000000..92eae313db6
--- /dev/null
+++ b/regression-test/data/mv_p0/sum_count/sum_count.out
@@ -0,0 +1,61 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !select_star --
+-4     -4      -4      d
+1      1       1       a
+1      1       1       a
+1      1       1       a
+2      1       1       a
+2      1       1       a
+2      2       2       b
+3      -3      \N      c
+3      2       \N      c
+
+-- !select_mv --
+-4     d       -4      1
+1      a       3       3
+2      a       2       2
+2      b       2       1
+3      c       -1      2
+
+-- !select_mv --
+-4     d       -4      1
+1      a       3       3
+2      a       4       2
+2      b       2       1
+3      c       6       2
+
+-- !select_mv --
+-4     d       1       1
+1      a       3       3
+2      a       2       2
+2      b       1       1
+3      c       2       2
+
+-- !select_mv --
+-4     d       2
+1      a       6
+2      a       4
+2      b       2
+3      c       4
+
+-- !select_mv --
+-4     d       3
+1      a       9
+2      a       6
+2      b       3
+3      c       6
+
+-- !select_mv --
+-4     d       1       1
+1      a       3       3
+2      a       2       2
+2      b       1       1
+3      c       2       2
+
+-- !select_mv --
+-4     d       1
+1      a       3
+2      a       2
+2      b       1
+3      c       2
+
diff --git a/regression-test/suites/mv_p0/count_star/count_star.groovy 
b/regression-test/suites/mv_p0/count_star/count_star.groovy
new file mode 100644
index 00000000000..f18a9d0a9e2
--- /dev/null
+++ b/regression-test/suites/mv_p0/count_star/count_star.groovy
@@ -0,0 +1,77 @@
+// 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.
+
+import org.codehaus.groovy.runtime.IOGroovyMethods
+
+suite ("count_star") {
+    sql """set enable_nereids_planner=true;"""
+    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 2,2,2,'b';"
+    sql "insert into d_table select 3,-3,null,'c';"
+    sql "insert into d_table values(1,1,1,'a'),(1,1,1,'a');"
+
+    createMV ("create materialized view kstar as select k1,k4,count(*) from 
d_table group by k1,k4;")
+
+    sql "insert into d_table select -4,-4,-4,'d';"
+    sql "insert into d_table select 3,2,null,'c';"
+    sql "insert into d_table values(2,1,1,'a'),(2,1,1,'a');"
+    qt_select_star "select * from d_table order by k1,k2,k3,k4;"
+
+    explain {
+        sql("select k1,k4,count(*) from d_table group by k1,k4;")
+        contains "(kstar)"
+    }
+    qt_select_mv "select k1,k4,count(*) from d_table group by k1,k4 order by 
1,2;"
+
+    explain {
+        sql("select k1,k4,count(*) from d_table where k1=1 group by k1,k4;")
+        contains "(kstar)"
+    }
+    qt_select_mv "select k1,k4,count(*) from d_table where k1=1 group by k1,k4 
order by 1,2;"
+
+    explain {
+        sql("select k1,k4,count(*) from d_table where k3=1 group by k1,k4;")
+        contains "(d_table)"
+    }
+    qt_select_mv "select k1,k4,count(*) from d_table where k3=1 group by k1,k4 
order by 1,2;"
+
+    explain {
+        sql("select count(*) from d_table;")
+        contains "(kstar)"
+    }
+    qt_select_mv "select count(*) from d_table;"
+
+    explain {
+        sql("select count(*) from d_table where k3=1;")
+        contains "(d_table)"
+    }
+    qt_select_mv "select count(*) from d_table where k3=1;"
+}
diff --git a/regression-test/suites/mv_p0/sum_count/sum_count.groovy 
b/regression-test/suites/mv_p0/sum_count/sum_count.groovy
new file mode 100644
index 00000000000..fbda0e07aac
--- /dev/null
+++ b/regression-test/suites/mv_p0/sum_count/sum_count.groovy
@@ -0,0 +1,92 @@
+// 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.
+
+import org.codehaus.groovy.runtime.IOGroovyMethods
+
+suite ("sum_count") {
+    sql """set enable_nereids_planner=true;"""
+    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 2,2,2,'b';"
+    sql "insert into d_table select 3,-3,null,'c';"
+    sql "insert into d_table values(1,1,1,'a'),(1,1,1,'a');"
+
+    createMV ("create materialized view kavg as select k1,k4,sum(k2),count(k2) 
from d_table group by k1,k4;")
+    createMV ("create materialized view kavg_key as select 
k1,k4,sum(k1),count(k4) from d_table group by k1,k4;")
+    createMV ("create materialized view kavg_const1 as select 
k1,k4,sum(1),count(1) from d_table group by k1,k4;")
+    createMV ("create materialized view kavg_const2 as select 
k1,k4,sum(2),count(3) from d_table group by k1,k4;")
+
+    sql "insert into d_table select -4,-4,-4,'d';"
+    sql "insert into d_table select 3,2,null,'c';"
+    sql "insert into d_table values(2,1,1,'a'),(2,1,1,'a');"
+    qt_select_star "select * from d_table order by k1,k2,k3,k4;"
+
+    explain {
+        sql("select k1,k4,sum(k2),count(k2) from d_table group by k1,k4;")
+        contains "(kavg)"
+    }
+    qt_select_mv "select k1,k4,sum(k2),count(k2) from d_table group by k1,k4 
order by 1,2;"
+
+    explain {
+        sql("select k1,k4,sum(k1),count(k4) from d_table group by k1,k4;")
+        contains "(kavg_key)"
+    }
+    qt_select_mv "select k1,k4,sum(k1),count(k4) from d_table group by k1,k4 
order by 1,2;"
+
+    explain {
+        sql("select k1,k4,sum(1),count(1) from d_table group by k1,k4;")
+        contains "(kavg_const1)"
+    }
+    qt_select_mv "select k1,k4,sum(1),count(1) from d_table group by k1,k4 
order by 1,2;"
+
+    explain {
+        sql("select k1,k4,sum(2) from d_table group by k1,k4;")
+        contains "(kavg_const2)"
+    }
+    qt_select_mv "select k1,k4,sum(2) from d_table group by k1,k4 order by 
1,2;"
+
+    explain {
+        sql("select k1,k4,sum(3) from d_table group by k1,k4;")
+        contains "(d_table)"
+    }
+    qt_select_mv "select k1,k4,sum(3) from d_table group by k1,k4 order by 
1,2;"
+
+    explain {
+        sql("select k1,k4,sum(1),count(3) from d_table group by k1,k4;")
+        contains "(kavg_const1)"
+    }
+    qt_select_mv "select k1,k4,sum(1),count(3) from d_table group by k1,k4 
order by 1,2;"
+
+    explain {
+        sql("select k1,k4,count(*) from d_table group by k1,k4;")
+        contains "(kavg_const1)"
+    }
+    qt_select_mv "select k1,k4,count(*) from d_table group by k1,k4 order by 
1,2;"
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to