From 3869a64db16da31dbee5e0f14a99da165a508f74 Mon Sep 17 00:00:00 2001 From: seawinde Date: Wed, 11 Dec 2024 11:52:21 +0800 Subject: [PATCH] fix code usage --- .../expression/PredicatesSplitterTest.java | 18 +- .../mv/date_trunc/mv_with_date_trunc.out | 132 +--- .../mv/date_trunc/mv_with_date_trunc.groovy | 588 ++---------------- 3 files changed, 58 insertions(+), 680 deletions(-) diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/PredicatesSplitterTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/PredicatesSplitterTest.java index a0719f2c86927d..9ba77c7ae4082a 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/PredicatesSplitterTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/PredicatesSplitterTest.java @@ -24,7 +24,7 @@ import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.expressions.SlotReference; -import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; +import org.apache.doris.nereids.util.ExpressionUtils; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -67,23 +67,27 @@ private void assetEquals(String expression, if (!StringUtils.isEmpty(expectedEqualExpr)) { Expression equalExpression = replaceUnboundSlot(PARSER.parseExpression(expectedEqualExpr), mem); - Assertions.assertEquals(equalExpression, splitPredicate.getEqualPredicateMap()); + Assertions.assertEquals(ExpressionUtils.extractConjunctionToSet(equalExpression), + splitPredicate.getEqualPredicateMap().keySet()); } else { - Assertions.assertEquals(splitPredicate.getEqualPredicateMap(), BooleanLiteral.TRUE); + Assertions.assertTrue(splitPredicate.getEqualPredicateMap().isEmpty()); } if (!StringUtils.isEmpty(expectedRangeExpr)) { Expression rangeExpression = replaceUnboundSlot(PARSER.parseExpression(expectedRangeExpr), mem); - Assertions.assertEquals(rangeExpression, splitPredicate.getRangePredicate()); + Assertions.assertEquals(ExpressionUtils.extractConjunctionToSet(rangeExpression), + splitPredicate.getRangePredicateMap().keySet()); } else { - Assertions.assertEquals(splitPredicate.getRangePredicate(), BooleanLiteral.TRUE); + Assertions.assertTrue(splitPredicate.getRangePredicateMap().isEmpty()); } if (!StringUtils.isEmpty(expectedResidualExpr)) { Expression residualExpression = replaceUnboundSlot(PARSER.parseExpression(expectedResidualExpr), mem); - Assertions.assertEquals(residualExpression, splitPredicate.getResidualPredicateMap()); + Assertions.assertEquals( + ExpressionUtils.extractConjunctionToSet(residualExpression), + splitPredicate.getResidualPredicateMap().keySet()); } else { - Assertions.assertEquals(splitPredicate.getResidualPredicateMap(), BooleanLiteral.TRUE); + Assertions.assertTrue(splitPredicate.getResidualPredicateMap().isEmpty()); } } diff --git a/regression-test/data/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.out b/regression-test/data/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.out index 24c4117275dc25..24d2c31a3ec0f4 100644 --- a/regression-test/data/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.out +++ b/regression-test/data/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.out @@ -1,143 +1,33 @@ -- This file is automatically generated. You should know what you did if you want to edit this --- !query1_0_before -- -b 3 - --- !query1_0_after -- -b 3 - --- !query1_1_before -- -b 3 - --- !query1_1_after -- -b 3 - --- !query1_2_before -- -b 1 - --- !query1_2_after -- -b 1 - --- !query1_3_before -- -b 18 - --- !query1_3_after -- -b 18 - --- !query1_4_before -- -b 18 - --- !query1_4_after -- -b 18 - --- !query1_5_before -- -b 18 - --- !query1_5_after -- -b 18 - --- !query1_6_before -- +-- !query3_2_before -- b 15 - --- !query1_6_after -- -b 15 - --- !query1_7_before -- -b 15 - --- !query1_7_after -- -b 15 - --- !query1_8_before -- -b 6 - --- !query1_8_after -- -b 6 - --- !query1_9_before -- -b 6 - --- !query1_9_after -- -b 6 - --- !query1_10_before -- -b 21 d 3 --- !query1_10_after -- -b 21 -d 3 - --- !query1_11_before -- -b 21 -d 3 - --- !query1_11_after -- -b 21 -d 3 - --- !query1_12_before -- -b 6 - --- !query1_12_after -- -b 6 - --- !query1_13_before -- -b 6 - --- !query1_13_after -- -b 6 - --- !query1_14_before -- -b 24 -d 3 - --- !query1_14_after -- -b 24 +-- !query3_2_after -- +b 15 d 3 --- !query1_15_before -- -b 24 +-- !query3_3_before -- +b 15 d 3 --- !query1_15_after -- -b 24 +-- !query3_3_after -- +b 15 d 3 --- !query2_0_before -- -b 6 - --- !query2_0_after -- -b 6 - --- !query2_1_before -- - --- !query2_1_after -- - --- !query2_2_before -- -b 18 - --- !query2_2_after -- -b 18 - --- !query2_3_before -- -b 18 - --- !query2_3_after -- -b 18 - --- !query3_0_before -- +-- !query3_4_before -- b 15 d 3 --- !query3_0_after -- +-- !query3_4_after -- b 15 d 3 --- !query3_1_before -- +-- !query3_5_before -- b 15 d 3 --- !query3_1_after -- +-- !query3_5_after -- b 15 d 3 diff --git a/regression-test/suites/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.groovy b/regression-test/suites/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.groovy index b52ba7bf78944b..5d43aa01a50f7d 100644 --- a/regression-test/suites/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.groovy +++ b/regression-test/suites/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.groovy @@ -97,7 +97,7 @@ suite("mv_with_date_trunc") { // 1. expr in date_trunc is simple col // 1.1 positive case, use field data type is datetime // day - def mv1_0 = """ + def mv3_2 = """ select l_shipmode, date_trunc(l_commitdate, 'day') as day_trunc, @@ -108,622 +108,106 @@ suite("mv_with_date_trunc") { l_shipmode, date_trunc(l_commitdate, 'day'); """ - def query1_0 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_commitdate >= '2023-10-17' and l_commitdate < '2023-10-18' - group by - l_shipmode; - """ - order_qt_query1_0_before "${query1_0}" - async_mv_rewrite_success(db, mv1_0, query1_0, "mv1_0") - order_qt_query1_0_after "${query1_0}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_0""" - - - def mv1_1 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'day') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'day'); - """ - def query1_1 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-10-17' <= l_commitdate and '2023-10-18' > l_commitdate - group by - l_shipmode; - """ - order_qt_query1_1_before "${query1_1}" - async_mv_rewrite_success(db, mv1_1, query1_1, "mv1_1") - order_qt_query1_1_after "${query1_1}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_1""" - - // second - def mv1_2 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'second') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'second'); - """ - def query1_2 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_commitdate >= '2023-10-17 00:05:08' and l_commitdate < '2023-10-18' - group by - l_shipmode; - """ - order_qt_query1_2_before "${query1_2}" - async_mv_rewrite_success(db, mv1_2, query1_2, "mv1_2") - order_qt_query1_2_after "${query1_2}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_2""" - - - def mv1_3 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'second') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'second'); - """ - def query1_3 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-01-17 00:05:08' <= l_shipdate and '2023-10-18' > l_shipdate - group by - l_shipmode; - """ - order_qt_query1_3_before "${query1_3}" - 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""" - - // minute - def mv1_4 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'minute') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'minute'); - """ - def query1_4 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_shipdate >= '2023-01-17 00:05:00' and l_shipdate < '2023-10-18' - group by - l_shipmode; - """ - order_qt_query1_4_before "${query1_4}" - async_mv_rewrite_success(db, mv1_4, query1_4, "mv1_4") - order_qt_query1_4_after "${query1_4}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_4""" - - - def mv1_5 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'minute') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'minute'); - """ - def query1_5 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-10-18' > l_commitdate and '2023-01-17 00:05:00' <= l_commitdate - group by - l_shipmode; - """ - order_qt_query1_5_before "${query1_5}" - async_mv_rewrite_success(db, mv1_5, query1_5, "mv1_5") - order_qt_query1_5_after "${query1_5}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_5""" - - - // hour - def mv1_6 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'hour') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'hour'); - """ - def query1_6 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_commitdate < '2023-10-18' and l_commitdate >= '2023-05-17 01:00:00' - group by - l_shipmode; - """ - order_qt_query1_6_before "${query1_6}" - async_mv_rewrite_success(db, mv1_6, query1_6, "mv1_6") - order_qt_query1_6_after "${query1_6}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_6""" - - - def mv1_7 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'hour') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'hour'); - """ - def query1_7 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-05-17 01:00:00' <= l_commitdate and '2023-10-18' > l_commitdate - group by - l_shipmode; - """ - order_qt_query1_7_before "${query1_7}" - async_mv_rewrite_success(db, mv1_7, query1_7, "mv1_7") - order_qt_query1_7_after "${query1_7}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_7""" - - - // week - def mv1_8 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'week') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'week'); - """ - def query1_8 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_shipdate < '2023-09-04' and '2023-01-16' <= l_shipdate - group by - l_shipmode; - """ - order_qt_query1_8_before "${query1_8}" - async_mv_rewrite_success(db, mv1_8, query1_8, "mv1_8") - order_qt_query1_8_after "${query1_8}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_8""" - - - def mv1_9 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'week') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'week'); - """ - def query1_9 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-01-16' <= l_commitdate and '2023-09-04' > l_commitdate - group by - l_shipmode; - """ - order_qt_query1_9_before "${query1_9}" - async_mv_rewrite_success(db, mv1_9, query1_9, "mv1_9") - order_qt_query1_9_after "${query1_9}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_9""" - - - // month - def mv1_10 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'month') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'month'); - """ - def query1_10 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_shipdate >= '2023-02-01' and l_shipdate < '2023-12-01' - group by - l_shipmode; - """ - order_qt_query1_10_before "${query1_10}" - async_mv_rewrite_success(db, mv1_10, query1_10, "mv1_10") - order_qt_query1_10_after "${query1_10}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_10""" - - - def mv1_11 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'month') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'month'); - """ - def query1_11 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-02-01' <= l_commitdate and '2023-12-01' > l_commitdate - group by - l_shipmode; - """ - order_qt_query1_11_before "${query1_11}" - async_mv_rewrite_success(db, mv1_11, query1_11, "mv1_11") - order_qt_query1_11_after "${query1_11}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_11""" - - - // quarter - def mv1_12 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'quarter') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'quarter'); - """ - def query1_12 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_commitdate >= '2023-04-01' and l_commitdate < '2023-10-01' - group by - l_shipmode; - """ - order_qt_query1_12_before "${query1_12}" - async_mv_rewrite_success(db, mv1_12, query1_12, "mv1_12") - order_qt_query1_12_after "${query1_12}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_12""" - - - def mv1_13 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'quarter') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'quarter'); - """ - def query1_13 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-04-01' <= l_shipdate and '2023-10-01' > l_shipdate - group by - l_shipmode; - """ - order_qt_query1_13_before "${query1_13}" - async_mv_rewrite_success(db, mv1_13, query1_13, "mv1_13") - order_qt_query1_13_after "${query1_13}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_13""" - - - // year - def mv1_14 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'year') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'year'); - """ - def query1_14 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_commitdate >= '2023-01-01' and l_commitdate < '2024-01-01' - group by - l_shipmode; - """ - order_qt_query1_14_before "${query1_14}" - async_mv_rewrite_success(db, mv1_14, query1_14, "mv1_14") - order_qt_query1_14_after "${query1_14}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_14""" - - - def mv1_15 = """ - select - l_shipmode, - date_trunc(l_commitdate, 'year') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_commitdate, 'year'); - """ - def query1_15 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-01-01' <= l_commitdate and '2024-01-01' > l_commitdate - group by - l_shipmode; - """ - order_qt_query1_15_before "${query1_15}" - async_mv_rewrite_success(db, mv1_5, query1_5, "mv1_15") - order_qt_query1_15_after "${query1_15}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv1_15""" - - - - // use ComparisonPredicate except >= and <, should fail - def mv2_0 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'week') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'week'); - """ - def query2_0 = """ + def query3_2 = """ select l_shipmode, count(*) from lineitem where - l_shipdate <= '2023-09-04' and '2023-01-16' <= l_shipdate - group by - l_shipmode; - """ - order_qt_query2_0_before "${query2_0}" - async_mv_rewrite_fail(db, mv2_0, query2_0, "mv2_0") - order_qt_query2_0_after "${query2_0}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv2_0""" - - - def mv2_1 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'week') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'week'); - """ - def query2_1 = """ - select - l_shipmode, - count(*) - from - lineitem - where - '2023-01-16' = l_shipdate - group by - l_shipmode; - """ - order_qt_query2_1_before "${query2_1}" - async_mv_rewrite_fail(db, mv2_1, query2_1, "mv2_1") - order_qt_query2_1_after "${query2_1}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv2_1""" - - // use un expect constant should fail - def mv2_2 = """ - select - l_shipmode, - date_trunc(l_shipdate, 'minute') as day_trunc, - count(*) - from - lineitem - group by - l_shipmode, - date_trunc(l_shipdate, 'minute'); - """ - def query2_2 = """ - select - l_shipmode, - count(*) - from - lineitem - where - l_shipdate >= '2023-01-17 00:05:01' and l_shipdate < '2023-10-18' + date_add(l_commitdate, INTERVAL 2 DAY) >= '2023-10-01' and date_add(l_commitdate, INTERVAL 2 DAY) < '2023-10-25' group by l_shipmode; """ - order_qt_query2_2_before "${query2_2}" - // l_shipdate >= '2023-01-17 00:05:01' because l_shipdate data type is date, so - // simply to l_shipdate >= '2023-01-18 00:00:00' - async_mv_rewrite_success(db, mv2_2, query2_2, "mv2_2") - order_qt_query2_2_after "${query2_2}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv2_2""" + order_qt_query3_2_before "${query3_2}" + async_mv_rewrite_success(db, mv3_2, query3_2, "mv3_2") + order_qt_query3_2_after "${query3_2}" + sql """ DROP MATERIALIZED VIEW IF EXISTS mv3_2""" - def mv2_3 = """ + def mv3_3 = """ select l_shipmode, - date_trunc(l_commitdate, 'minute') as day_trunc, + date_trunc(l_commitdate, 'day') as day_trunc, count(*) from lineitem group by l_shipmode, - date_trunc(l_commitdate, 'minute'); + date_trunc(l_commitdate, 'day'); """ - def query2_3 = """ + def query3_3 = """ select l_shipmode, count(*) from lineitem where - l_commitdate >= '2023-01-17 00:05:01' and l_commitdate < '2023-10-18' + '2023-10-01' <= date_add(l_commitdate, INTERVAL 2 DAY) and '2023-10-30' > date_add(l_commitdate, INTERVAL 2 DAY) group by l_shipmode; """ - order_qt_query2_3_before "${query2_3}" - async_mv_rewrite_fail(db, mv2_3, query2_3, "mv2_3") - order_qt_query2_3_after "${query2_3}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv2_3""" + order_qt_query3_3_before "${query3_3}" + async_mv_rewrite_success(db, mv3_3, query3_3, "mv3_3") + order_qt_query3_3_after "${query3_3}" + sql """ DROP MATERIALIZED VIEW IF EXISTS mv3_3""" - // 2. expr in date_trunc is cmplex expr - // success - def mv3_0 = """ + // use ComparisonPredicate except >= and <, should fail + def mv3_4 = """ select l_shipmode, - date_add(l_commitdate, INTERVAL 2 DAY) as day_trunc, + date_trunc(l_commitdate, 'day') as day_trunc, count(*) from lineitem group by l_shipmode, - date_add(l_commitdate, INTERVAL 2 DAY); + date_trunc(l_commitdate, 'day'); """ - def query3_0 = """ + def query3_4 = """ select l_shipmode, count(*) from lineitem where - date_add(l_commitdate, INTERVAL 2 DAY) >= '2023-10-01' and date_add(l_commitdate, INTERVAL 2 DAY) < '2023-10-25' + date_add(l_commitdate, INTERVAL 2 DAY) >= '2023-10-01' and date_add(l_commitdate, INTERVAL 2 DAY) <= '2023-10-25' group by l_shipmode; """ - order_qt_query3_0_before "${query3_0}" - async_mv_rewrite_fail(db, mv3_0, query3_0, "mv3_0") - order_qt_query3_0_after "${query3_0}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv3_0""" + order_qt_query3_4_before "${query3_4}" + async_mv_rewrite_fail(db, mv3_4, query3_4, "mv3_4") + order_qt_query3_4_after "${query3_4}" + sql """ DROP MATERIALIZED VIEW IF EXISTS mv3_4""" - def mv3_1 = """ + // use un expect constant should fail + def mv3_5 = """ select l_shipmode, - date_trunc(date_add(l_commitdate, INTERVAL 2 DAY), 'day') as day_trunc, + date_trunc(l_commitdate, 'day') as day_trunc, count(*) from lineitem group by l_shipmode, - date_trunc(date_add(l_commitdate, INTERVAL 2 DAY), 'day'); + date_trunc(l_commitdate, 'day'); """ - def query3_1 = """ + def query3_5 = """ select l_shipmode, count(*) from lineitem where - '2023-10-01' <= date_add(l_commitdate, INTERVAL 2 DAY) and '2023-10-30' > date_add(l_commitdate, INTERVAL 2 DAY) + '2023-10-01' <= date_add(l_commitdate, INTERVAL 2 DAY) and '2023-10-30 00:10:00' > date_add(l_commitdate, INTERVAL 2 DAY) group by l_shipmode; """ - order_qt_query3_1_before "${query3_1}" - async_mv_rewrite_fail(db, mv3_1, query3_1, "mv3_1") - order_qt_query3_1_after "${query3_1}" - sql """ DROP MATERIALIZED VIEW IF EXISTS mv3_1""" - - // use ComparisonPredicate except >= and <, should fail - // use un expect constant should fail + order_qt_query3_5_before "${query3_5}" + async_mv_rewrite_fail(db, mv3_5, query3_5, "mv3_5") + order_qt_query3_5_after "${query3_5}" + sql """ DROP MATERIALIZED VIEW IF EXISTS mv3_5""" }