diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java index c7049edfe57ecd..8789e07a165ac3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/AbstractMaterializedViewRule.java @@ -51,6 +51,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.ElementAt; import org.apache.doris.nereids.trees.expressions.functions.scalar.NonNullable; import org.apache.doris.nereids.trees.expressions.functions.scalar.Nullable; +import org.apache.doris.nereids.trees.expressions.literal.DateLiteral; import org.apache.doris.nereids.trees.expressions.literal.Literal; import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral; import org.apache.doris.nereids.trees.plans.JoinType; @@ -578,6 +579,14 @@ protected List rewriteExpression(List sourceEx Map targetToTargetReplacementMappingQueryBased = flattenExpressionMap.get(0); + // viewExprParamToDateTruncMap is {slot#0 : date_trunc(slot#0, 'day')} + Map viewExprParamToDateTruncMap = new HashMap<>(); + targetToTargetReplacementMappingQueryBased.keySet().forEach(expr -> { + if (expr instanceof DateTrunc) { + viewExprParamToDateTruncMap.put(expr.child(0), (DateTrunc) expr); + } + }); + List rewrittenExpressions = new ArrayList<>(); for (int exprIndex = 0; exprIndex < sourceShuttledExpressions.size(); exprIndex++) { Expression expressionShuttledToRewrite = sourceShuttledExpressions.get(exprIndex); @@ -599,37 +608,36 @@ protected List rewriteExpression(List sourceEx // if contains any slot to rewrite, which means can not be rewritten by target, // expressionShuttledToRewrite is slot#0 > '2024-01-01' but mv plan output is date_trunc(slot#0, 'day') // which would try to rewrite - // viewExpressionParamToDateTruncMap is {slot#0 : date_trunc(slot#0, 'day')} - Map viewExpressionParamToDateTruncMap = new HashMap<>(); - targetToTargetReplacementMappingQueryBased.keySet().forEach(expr -> { - if (expr instanceof DateTrunc) { - viewExpressionParamToDateTruncMap.put(expr.child(0), (DateTrunc) expr); - } - }); - Expression queryUsedExpr = expressionShuttledToRewrite.child(0); + Expression queryShuttledExprParam = expressionShuttledToRewrite.child(0); - if (!queryExprToInfoMap.containsKey(sourceExpressionsToWrite.get(exprIndex)) - || !viewExpressionParamToDateTruncMap.containsKey(queryUsedExpr)) { - // mv date_trunc expression can not offer expression for query, - // can not try to rewrite by date_trunc, bail out + Expression queryOriginalExpr = sourceExpressionsToWrite.get(exprIndex); + if (!queryExprToInfoMap.containsKey(queryOriginalExpr) + || !viewExprParamToDateTruncMap.containsKey(queryShuttledExprParam)) { + // query expr contains expression info or mv out contains date_trunc expression, + // if not, can not try to rewritten by view date_trunc, bail out return ImmutableList.of(); } - Map datetruncMap = new HashMap<>(); - Literal queryLiteral = queryExprToInfoMap.get(expressionShuttledToRewrite) == null - ? null : queryExprToInfoMap.get(expressionShuttledToRewrite).literal; - datetruncMap.put(queryUsedExpr, queryLiteral); - Expression replacedWithLiteral = ExpressionUtils.replace( - viewExpressionParamToDateTruncMap.get(queryUsedExpr), datetruncMap); - Expression foldedExpressionWithLiteral = FoldConstantRuleOnFE.evaluate(replacedWithLiteral, + Literal queryUsedLiteral = queryExprToInfoMap.get(queryOriginalExpr).literal; + if (!(queryUsedLiteral instanceof DateLiteral)) { + return ImmutableList.of(); + } + datetruncMap.put(queryShuttledExprParam, queryUsedLiteral); + Expression dateTruncWithLiteral = ExpressionUtils.replace( + viewExprParamToDateTruncMap.get(queryShuttledExprParam), datetruncMap); + Expression foldedExpressionWithLiteral = FoldConstantRuleOnFE.evaluate(dateTruncWithLiteral, new ExpressionRewriteContext(cascadesContext)); - if (foldedExpressionWithLiteral.equals(queryLiteral)) { - // after date_trunc simplify if equals to original expression, could rewritten by mv + if (!(foldedExpressionWithLiteral instanceof DateLiteral)) { + return ImmutableList.of(); + } + if (((DateLiteral) foldedExpressionWithLiteral).getDouble() == queryUsedLiteral.getDouble()) { + // after date_trunc simplify if equals to original expression, expr could be rewritten by mv replacedExpression = ExpressionUtils.replace(expressionShuttledToRewrite, targetToTargetReplacementMappingQueryBased, - viewExpressionParamToDateTruncMap); + viewExprParamToDateTruncMap); } if (replacedExpression.anyMatch(slotsToRewrite::contains)) { + // has expression not rewritten successfully, bail out return ImmutableList.of(); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/Predicates.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/Predicates.java index b3dc2d26835220..18095d5f60ccb4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/Predicates.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/mv/Predicates.java @@ -313,6 +313,9 @@ public boolean isInvalid() { return Objects.equals(this, INVALID_INSTANCE); } + /** + * Get expression list in predicates + */ public List toList() { if (isInvalid()) { return ImmutableList.of(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java index e6070007962ff9..738b6d3af43a57 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java @@ -408,8 +408,10 @@ public static Expression replace(Expression expr, Map * input expression: a > 1 - * replaceMap: a -> b + c - * + * replaceMap: d -> b + c, transferMap: a -> d + * firstly try to get mapping expression from replaceMap by a, if can not then + * get mapping d from transferMap by a + * and get mapping b + c from replaceMap by d * output: * b + c > 1 * 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 new file mode 100644 index 00000000000000..24c4117275dc25 --- /dev/null +++ b/regression-test/data/nereids_rules_p0/mv/date_trunc/mv_with_date_trunc.out @@ -0,0 +1,143 @@ +-- 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 -- +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 +d 3 + +-- !query1_15_before -- +b 24 +d 3 + +-- !query1_15_after -- +b 24 +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 -- +b 15 +d 3 + +-- !query3_0_after -- +b 15 +d 3 + +-- !query3_1_before -- +b 15 +d 3 + +-- !query3_1_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 59db8204406510..b52ba7bf78944b 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 @@ -53,7 +53,7 @@ suite("mv_with_date_trunc") { l_linenumber ) PARTITION BY RANGE(l_shipdate) ( FROM - ('2023-10-16') TO ('2023-11-30') INTERVAL 1 DAY + ('2022-12-31') TO ('2023-12-31') INTERVAL 1 DAY ) DISTRIBUTED BY HASH(l_orderkey) BUCKETS 3 PROPERTIES ("replication_num" = "1"); """ @@ -61,74 +61,669 @@ suite("mv_with_date_trunc") { sql """ insert into lineitem values - (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', - '2023-10-17', '2023-10-17 00:10:00', - '2023-10-17', 'a', 'b', 'yyyyyyyyy' - ), - (2, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', - '2023-10-18', '2023-10-18 08:10:00', - '2023-10-18', 'a', 'b', 'yyyyyyyyy' - ), - (3, 2, 3, 6, 7.5, 8.5, 9.5, 10.5, 'k', 'o', - '2023-10-19', '2023-10-19 12:10:00', - '2023-10-19', 'c', 'd', 'xxxxxxxxx' - ); + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-01-01', '2023-01-01 00:00:01', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-01-01', '2023-01-01 02:01:45', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-01-01', '2023-01-01 03:02:43', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-04-01', '2023-04-01 05:03:42', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-04-01', '2023-04-01 07:04:40', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-04-01', '2023-04-01 09:05:36', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-07-01', '2023-07-01 11:06:35', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-07-01', '2023-07-01 13:07:34', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-07-01', '2023-07-01 15:08:30', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-01', '2023-10-01 17:09:25', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-01', '2023-10-01 19:10:24', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-01', '2023-10-01 21:11:23', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-15', '2023-10-15 16:12:22', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-15', '2023-10-15 00:05:12', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-15', '2023-10-15 23:59:59', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-16', '2023-10-16 00:00:00', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-16', '2023-10-16 00:05:03', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-16', '2023-10-16 23:59:59', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-17', '2023-10-17 00:00:00', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-17', '2023-10-17 00:05:05', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (1, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k', '2023-10-17', '2023-10-17 23:59:59', '2023-10-17', 'a', 'b', 'yyyyyyyyy'), + (2, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k','2023-10-18', '2023-10-18 00:00:00','2023-10-18', 'a', 'b', 'yyyyyyyyy'), + (2, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k','2023-10-18', '2023-10-18 00:05:12','2023-10-18', 'a', 'b', 'yyyyyyyyy'), + (2, 2, 3, 4, 5.5, 6.5, 7.5, 8.5, 'o', 'k','2023-10-18', '2023-10-18 23:59:59','2023-10-18', 'a', 'b', 'yyyyyyyyy'), + (3, 2, 3, 6, 7.5, 8.5, 9.5, 10.5, 'k', 'o','2023-10-19', '2023-10-19 00:00:00','2023-10-19', 'c', 'd', 'xxxxxxxxx'), + (3, 2, 3, 6, 7.5, 8.5, 9.5, 10.5, 'k', 'o','2023-10-19', '2023-10-19 00:05:15','2023-10-19', 'c', 'd', 'xxxxxxxxx'), + (3, 2, 3, 6, 7.5, 8.5, 9.5, 10.5, 'k', 'o','2023-10-19', '2023-10-19 23:59:59','2023-10-19', 'c', 'd', 'xxxxxxxxx'); """ + sql """analyze table lineitem with sync;""" + sql """alter table lineitem modify column l_comment set stats ('row_count'='9');""" + + // second, minute ,hour, day, week, month, quarter, year + // 1. expr in date_trunc is simple col + // 1.1 positive case, use field data type is datetime + // day def mv1_0 = """ + 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_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, 'day') as day_trunc, + date_trunc(l_shipdate, 'week') as day_trunc, count(*) from lineitem group by l_shipmode, - date_trunc(l_shipdate, 'day'); + date_trunc(l_shipdate, 'week'); """ - def query1_0 = """ + def query2_0 = """ select l_shipmode, count(*) from lineitem where - l_shipdate >= '2023-10-17' and l_shipdate < '2023-10-18' + l_shipdate <= '2023-09-04' and '2023-01-16' <= l_shipdate 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""" + 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' + 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""" -// def mv1_0 = """ -// select -// l_shipmode, -// date_trunc(l_shipdate, 'day') as day_trunc, -// count(*) -// from -// lineitem -// group by -// l_shipmode, -// date_trunc(l_shipdate, 'day'); -// """ -// def query1_0 = """ -// select -// l_shipmode, -// count(*) -// from -// lineitem -// where -// l_shipdate >= '2023-10-17' and l_shipdate < '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 mv2_3 = """ + select + l_shipmode, + date_trunc(l_commitdate, 'minute') as day_trunc, + count(*) + from + lineitem + group by + l_shipmode, + date_trunc(l_commitdate, 'minute'); + """ + def query2_3 = """ + select + l_shipmode, + count(*) + from + lineitem + where + l_commitdate >= '2023-01-17 00:05:01' and l_commitdate < '2023-10-18' + 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""" + + + // 2. expr in date_trunc is cmplex expr + // success + def mv3_0 = """ + select + l_shipmode, + date_add(l_commitdate, INTERVAL 2 DAY) as day_trunc, + count(*) + from + lineitem + group by + l_shipmode, + date_add(l_commitdate, INTERVAL 2 DAY); + """ + def query3_0 = """ + 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' + 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""" + + + def mv3_1 = """ + select + l_shipmode, + date_trunc(date_add(l_commitdate, INTERVAL 2 DAY), 'day') as day_trunc, + count(*) + from + lineitem + group by + l_shipmode, + date_trunc(date_add(l_commitdate, INTERVAL 2 DAY), 'day'); + """ + def query3_1 = """ + 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) + 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 }