Skip to content

Commit

Permalink
add test for rule EliminateGroupByKeyByUniform and change computeUnif…
Browse files Browse the repository at this point in the history
…orm in LogicalProject
  • Loading branch information
feiniaofeiafei committed Nov 12, 2024
1 parent a69c3b1 commit 452bb5f
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.nereids.util.MemoPatternMatchSupported;
import org.apache.doris.nereids.util.PlanChecker;
import org.apache.doris.utframe.TestWithFeService;

import org.junit.jupiter.api.Test;

public class EliminateGroupByKeyByUniformTest extends TestWithFeService implements MemoPatternMatchSupported {
@Override
protected void runBeforeAll() throws Exception {
createDatabase("test");
createTable("create table test.eli_gbk_by_uniform_t(a int null, b int not null,"
+ "c varchar(10) null, d date, dt datetime)\n"
+ "distributed by hash(a) properties('replication_num' = '1');");
connectContext.setDatabase("test");
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
}

@Test
void testEliminateByFilter() {
PlanChecker.from(connectContext)
.analyze("select a, min(a), sum(a),b from eli_gbk_by_uniform_t where a = 1 group by a,b")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg ->
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("b")));

}

@Test
void testNotEliminateWhenOnlyOneGbyKey() {
PlanChecker.from(connectContext)
.analyze("select a, min(a), sum(a) from eli_gbk_by_uniform_t where a = 1 group by a")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg ->
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("a")));

}

@Test
void testEliminateByProjectConst() {
PlanChecker.from(connectContext)
.analyze("select sum(c1), c2 from (select a c1,1 c2, d c3 from eli_gbk_by_uniform_t) t group by c2,c3 ")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg ->
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("c3")));
}

@Test
void testEliminateByProjectUniformSlot() {
PlanChecker.from(connectContext)
.analyze("select max(c3), c1,c2,c3 from (select a c1,1 c2, d c3 from eli_gbk_by_uniform_t where a=1) t group by c1,c2,c3")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg ->
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("c3")));
}

@Test
void testEliminateDate() {
PlanChecker.from(connectContext)
.analyze("select d, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where d = '2023-01-06' group by d,a")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg ->
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("a")));
}

@Test
void testSaveOneExpr() {
PlanChecker.from(connectContext)
.analyze("select a, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where a = 1 and b=100 group by a, b,'abc'")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg ->
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("a")));
}

@Test
void testSaveOneExprProjectConst() {
PlanChecker.from(connectContext)
.analyze("select c2 from (select a c1,1 c2, 3 c3 from eli_gbk_by_uniform_t) t group by c2,c3 order by 1;")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg ->
agg.getGroupByExpressions().size() == 1
&& agg.getGroupByExpressions().get(0).toSql().equals("c2")));
}

@Test
void testNotRewriteWhenHasRepeat() {
PlanChecker.from(connectContext)
.analyze("select c2 from (select a c1,1 c2, 3 c3 from eli_gbk_by_uniform_t) t group by grouping sets((c2),(c3)) order by 1;")
.rewrite()
.printlnTree()
.matches(logicalAggregate().when(agg -> agg.getGroupByExpressions().size() == 3));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !empty_tranform_not_to_scalar_agg --

-- !empty_tranform_multi_column --

-- !empty_tranform_multi_column --
1 1 2 2

-- !tranform_to_scalar_agg_not_null_column --

-- !project_const --
\N 1
\N 1
2 1
2 1
4 1
6 1
6 1
10 1

-- !project_slot_uniform --
2023-01-01 1 1 2023-01-01

-- !upper_refer --

-- !upper_refer_varchar_alias --
cherry 3

-- !upper_refer_date --
2023-01-06

-- !upper_refer_datetime_not_to_scalar_agg --
2023-01-06T15:00

-- !upper_refer_datetime --
2023-01-06T15:00

-- !project_no_other_agg_func --
1
1
1
1
1
1
1
1

-- !project_const_not_to_scalar_agg_multi --
1

-- !not_to_scalar_agg_multi --
1 1 2 2

-- !conflict_equal_value --

-- !project_slot_uniform_confict_value --

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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("eliminate_group_by_key_by_uniform") {
sql "drop table if exists eli_gbk_by_uniform_t"
sql """create table eli_gbk_by_uniform_t(a int null, b int not null, c varchar(10) null, d date, dt datetime)
distributed by hash(a) properties("replication_num"="1");
"""
qt_empty_tranform_not_to_scalar_agg "select a, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where a = 1 group by a"
qt_empty_tranform_multi_column "select a,b, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where a = 1 and b=2 group by a,b"

sql """
INSERT INTO eli_gbk_by_uniform_t (a, b, c, d, dt) VALUES
(1, 100, 'apple', '2023-01-01', '2023-01-01 10:00:00'),
(1, 100, 'apple', '2023-01-01', '2023-01-01 10:00:00'),
(2, 101, 'banana', '2023-01-02', '2023-01-02 11:00:00'),
(3, 102, 'cherry', '2023-01-03', '2023-01-03 12:00:00'),
(3, 102, 'cherry', '2023-01-03', '2023-01-03 12:00:00'),
(NULL, 103, 'date', '2023-01-04', '2023-01-04 13:00:00'),
(4, 104, 'elderberry', '2023-01-05', '2023-01-05 14:00:00'),
(5, 105, NULL, '2023-01-06', '2023-01-06 15:00:00'),
(5, 105, NULL, '2023-01-06', '2023-01-06 15:00:00'),
(6, 106, 'fig', '2023-01-07', '2023-01-07 16:00:00'),
(NULL, 107, 'grape', '2023-01-08', '2023-01-08 17:00:00');
"""
qt_empty_tranform_multi_column "select a, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where a = 1 group by a, b,'abc' order by 1,2,3,4"
qt_tranform_to_scalar_agg_not_null_column "select b, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where b = 1 group by a, b order by 1,2,3,4"

qt_project_const "select sum(c1), c2 from (select a c1,1 c2, d c3 from eli_gbk_by_uniform_t) t group by c2,c3 order by 1,2;"
qt_project_slot_uniform "select max(c3), c1,c2,c3 from (select a c1,1 c2, d c3 from eli_gbk_by_uniform_t where a=1) t group by c1,c2,c3 order by 1,2,3,4;"

qt_upper_refer "select b from (select b, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where b = 1 group by a, b) t order by b"
qt_upper_refer_varchar_alias "select c1,c2 from (select c as c1, min(a) c2, sum(a), count(a) from eli_gbk_by_uniform_t where c = 'cherry' group by a, b,c) t order by c1,c2"
qt_upper_refer_date "select d from (select d, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where d = '2023-01-06' group by d,a) t order by 1"
qt_upper_refer_datetime_not_to_scalar_agg "select dt from (select dt, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where dt = '2023-01-06 15:00:00' group by dt) t order by 1"
qt_upper_refer_datetime "select dt from (select dt, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where dt = '2023-01-06 15:00:00' group by dt, a) t order by 1"

qt_project_no_other_agg_func "select c2 from (select a c1,1 c2, d c3 from eli_gbk_by_uniform_t) t group by c2,c3 order by 1;"
qt_project_const_not_to_scalar_agg_multi "select c2 from (select a c1,1 c2, 3 c3 from eli_gbk_by_uniform_t) t group by c2,c3 order by 1;"
qt_not_to_scalar_agg_multi "select a, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where a = 1 and b=100 group by a, b,'abc' order by 1,2,3,4"
qt_conflict_equal_value "select a, min(a), sum(a), count(a) from eli_gbk_by_uniform_t where a = 1 and a=2 group by a, b,'abc' order by 1,2,3,4"
qt_project_slot_uniform_confict_value "select max(c3), c1,c2,c3 from (select a c1,1 c2, d c3 from eli_gbk_by_uniform_t where a=1) t where c2=2 group by c1,c2,c3 order by 1,2,3,4;"

}

0 comments on commit 452bb5f

Please sign in to comment.