[Feature](materialized-view) support create mv with count(*) (#30313)

support create mv with count(*)
This commit is contained in:
Pxl
2024-01-24 17:45:47 +08:00
committed by yiguolei
parent 101b2593fc
commit 7e60369ba2
5 changed files with 271 additions and 2 deletions

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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;"
}

View File

@ -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;"
}