From 161520feb420a08f5515218d0336c61b49cb6161 Mon Sep 17 00:00:00 2001 From: jakevin Date: Fri, 8 Sep 2023 16:58:33 +0800 Subject: [PATCH] [feature](Nereids): enable convert CASE WHEN to IF (#24050) enable rule to convert CASE WHEN to IF. --- .../expression/ExpressionOptimization.java | 5 +- .../nereids/stats/ExpressionEstimation.java | 27 ++++++-- .../fold_constant/fold_constant_by_fe.out | 0 .../shape/query11.out | 2 +- .../shape/query21.out | 33 ++++----- .../shape/query31.out | 4 +- .../shape/query34.out | 2 +- .../shape/query39.out | 2 +- .../shape/query4.out | 4 +- .../shape/query47.out | 2 +- .../shape/query53.out | 2 +- .../shape/query57.out | 2 +- .../shape/query63.out | 2 +- .../shape/query73.out | 2 +- .../shape/query74.out | 2 +- .../shape/query89.out | 2 +- .../expression/case_when_to_if.groovy | 69 +++++++++++++++++++ .../fold_constant/fold_constant_by_be.groovy | 0 .../fold_constant/fold_constant_by_fe.groovy | 0 .../fold_constant/load.groovy | 0 20 files changed, 123 insertions(+), 39 deletions(-) rename regression-test/data/nereids_p0/{ => expression}/fold_constant/fold_constant_by_fe.out (100%) create mode 100644 regression-test/suites/nereids_p0/expression/case_when_to_if.groovy rename regression-test/suites/nereids_p0/{ => expression}/fold_constant/fold_constant_by_be.groovy (100%) rename regression-test/suites/nereids_p0/{ => expression}/fold_constant/fold_constant_by_fe.groovy (100%) rename regression-test/suites/nereids_p0/{ => expression}/fold_constant/load.groovy (100%) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java index 3f30d10ebe..869cd6b99e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/ExpressionOptimization.java @@ -18,6 +18,7 @@ package org.apache.doris.nereids.rules.expression; import org.apache.doris.nereids.rules.expression.rules.ArrayContainToArrayOverlap; +import org.apache.doris.nereids.rules.expression.rules.CaseWhenToIf; import org.apache.doris.nereids.rules.expression.rules.DistinctPredicatesRule; import org.apache.doris.nereids.rules.expression.rules.ExtractCommonFactorRule; import org.apache.doris.nereids.rules.expression.rules.OrToIn; @@ -42,8 +43,8 @@ public class ExpressionOptimization extends ExpressionRewrite { SimplifyDecimalV3Comparison.INSTANCE, SimplifyRange.INSTANCE, OrToIn.INSTANCE, - ArrayContainToArrayOverlap.INSTANCE - + ArrayContainToArrayOverlap.INSTANCE, + CaseWhenToIf.INSTANCE ); private static final ExpressionRuleExecutor EXECUTOR = new ExpressionRuleExecutor(OPTIMIZE_REWRITE_RULES); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ExpressionEstimation.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ExpressionEstimation.java index 50f27dc2a9..aa84077398 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ExpressionEstimation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/stats/ExpressionEstimation.java @@ -59,6 +59,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.FromDays; import org.apache.doris.nereids.trees.expressions.functions.scalar.Hour; import org.apache.doris.nereids.trees.expressions.functions.scalar.HoursDiff; import org.apache.doris.nereids.trees.expressions.functions.scalar.HoursSub; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; import org.apache.doris.nereids.trees.expressions.functions.scalar.Least; import org.apache.doris.nereids.trees.expressions.functions.scalar.Minute; import org.apache.doris.nereids.trees.expressions.functions.scalar.MinutesAdd; @@ -134,13 +135,25 @@ public class ExpressionEstimation extends ExpressionVisitor 0.00) THEN (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)) ELSE 0 END > CASE WHEN (year_total > 0.00) THEN (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)) ELSE 0 END) +------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_secyear.customer_id)(if((year_total > 0.00), (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)), 0) > if((year_total > 0.00), (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)), 0)) --------------hashJoin[INNER_JOIN](t_s_secyear.customer_id = t_s_firstyear.customer_id) ----------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_firstyear.customer_id) ------------------PhysicalDistribute diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query21.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query21.out index f950635ad6..58c63f10f1 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query21.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query21.out @@ -3,24 +3,25 @@ PhysicalResultSink --PhysicalTopN ----PhysicalDistribute -------filter((CASE WHEN (inv_before > 0) THEN (cast(inv_after as DOUBLE) / cast(inv_before as DOUBLE)) ELSE NULL END >= cast((2.000000 / 3.0) as DOUBLE))(CASE WHEN (inv_before > 0) THEN (cast(inv_after as DOUBLE) / cast(inv_before as DOUBLE)) ELSE NULL END <= 1.5)) ---------hashAgg[GLOBAL] -----------PhysicalDistribute -------------hashAgg[LOCAL] ---------------PhysicalProject -----------------hashJoin[INNER_JOIN](inventory.inv_warehouse_sk = warehouse.w_warehouse_sk) -------------------hashJoin[INNER_JOIN](inventory.inv_date_sk = date_dim.d_date_sk) ---------------------hashJoin[INNER_JOIN](item.i_item_sk = inventory.inv_item_sk) -----------------------PhysicalOlapScan[inventory] +------PhysicalTopN +--------filter((if((inv_before > 0), (cast(inv_after as DOUBLE) / cast(inv_before as DOUBLE)), NULL) <= 1.5)(if((inv_before > 0), (cast(inv_after as DOUBLE) / cast(inv_before as DOUBLE)), NULL) >= cast((2.000000 / 3.0) as DOUBLE))) +----------hashAgg[GLOBAL] +------------PhysicalDistribute +--------------hashAgg[LOCAL] +----------------PhysicalProject +------------------hashJoin[INNER_JOIN](inventory.inv_warehouse_sk = warehouse.w_warehouse_sk) +--------------------hashJoin[INNER_JOIN](inventory.inv_date_sk = date_dim.d_date_sk) +----------------------hashJoin[INNER_JOIN](item.i_item_sk = inventory.inv_item_sk) +------------------------PhysicalOlapScan[inventory] +------------------------PhysicalDistribute +--------------------------PhysicalProject +----------------------------filter((item.i_current_price <= 1.49)(item.i_current_price >= 0.99)) +------------------------------PhysicalOlapScan[item] ----------------------PhysicalDistribute ------------------------PhysicalProject ---------------------------filter((item.i_current_price <= 1.49)(item.i_current_price >= 0.99)) -----------------------------PhysicalOlapScan[item] +--------------------------filter((date_dim.d_date >= 2002-01-28)(date_dim.d_date <= 2002-03-29)) +----------------------------PhysicalOlapScan[date_dim] --------------------PhysicalDistribute ----------------------PhysicalProject -------------------------filter((date_dim.d_date >= 2002-01-28)(date_dim.d_date <= 2002-03-29)) ---------------------------PhysicalOlapScan[date_dim] -------------------PhysicalDistribute ---------------------PhysicalProject -----------------------PhysicalOlapScan[warehouse] +------------------------PhysicalOlapScan[warehouse] diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query31.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query31.out index 12fc4a1f7f..953ca425f0 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query31.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query31.out @@ -45,7 +45,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 ) --------PhysicalDistribute ----------PhysicalQuickSort ------------PhysicalProject ---------------hashJoin[INNER_JOIN](ws1.ca_county = ws3.ca_county)(CASE WHEN (web_sales > 0.00) THEN (cast(web_sales as DOUBLE) / cast(web_sales as DOUBLE)) ELSE NULL END > CASE WHEN (store_sales > 0.00) THEN (cast(store_sales as DOUBLE) / cast(store_sales as DOUBLE)) ELSE NULL END) +--------------hashJoin[INNER_JOIN](ws1.ca_county = ws3.ca_county)(if((web_sales > 0.00), (cast(web_sales as DOUBLE) / cast(web_sales as DOUBLE)), NULL) > if((store_sales > 0.00), (cast(store_sales as DOUBLE) / cast(store_sales as DOUBLE)), NULL)) ----------------PhysicalDistribute ------------------PhysicalProject --------------------filter((ws3.d_year = 2000)(ws3.d_qoy = 3)) @@ -56,7 +56,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 ) ----------------------PhysicalProject ------------------------filter((ss3.d_year = 2000)(ss3.d_qoy = 3)) --------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) ---------------------hashJoin[INNER_JOIN](ws1.ca_county = ws2.ca_county)(CASE WHEN (web_sales > 0.00) THEN (cast(web_sales as DOUBLE) / cast(web_sales as DOUBLE)) ELSE NULL END > CASE WHEN (store_sales > 0.00) THEN (cast(store_sales as DOUBLE) / cast(store_sales as DOUBLE)) ELSE NULL END) +--------------------hashJoin[INNER_JOIN](ws1.ca_county = ws2.ca_county)(if((web_sales > 0.00), (cast(web_sales as DOUBLE) / cast(web_sales as DOUBLE)), NULL) > if((store_sales > 0.00), (cast(store_sales as DOUBLE) / cast(store_sales as DOUBLE)), NULL)) ----------------------hashJoin[INNER_JOIN](ss1.ca_county = ws1.ca_county) ------------------------hashJoin[INNER_JOIN](ss1.ca_county = ss2.ca_county) --------------------------PhysicalDistribute diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query34.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query34.out index 6fd519ecc6..4779144765 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query34.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query34.out @@ -25,7 +25,7 @@ PhysicalResultSink ------------------------------------PhysicalOlapScan[date_dim] ----------------------------PhysicalDistribute ------------------------------PhysicalProject ---------------------------------filter(((cast(hd_buy_potential as VARCHAR(*)) = '1001-5000') OR (cast(hd_buy_potential as VARCHAR(*)) = '0-500'))(household_demographics.hd_vehicle_count > 0)(CASE WHEN (hd_vehicle_count > 0) THEN (cast(hd_dep_count as DOUBLE) / cast(hd_vehicle_count as DOUBLE)) ELSE NULL END > 1.2)) +--------------------------------filter(((cast(hd_buy_potential as VARCHAR(*)) = '1001-5000') OR (cast(hd_buy_potential as VARCHAR(*)) = '0-500'))(household_demographics.hd_vehicle_count > 0)(if((hd_vehicle_count > 0), (cast(hd_dep_count as DOUBLE) / cast(hd_vehicle_count as DOUBLE)), NULL) > 1.2)) ----------------------------------PhysicalOlapScan[household_demographics] --------------------------PhysicalDistribute ----------------------------PhysicalProject diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query39.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query39.out index 3a2ff5eb3d..9a39ef048d 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query39.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query39.out @@ -3,7 +3,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 ) --PhysicalCteProducer ( cteId=CTEId#0 ) ----PhysicalProject -------filter((CASE WHEN (mean = 0) THEN 0 ELSE (stdev / mean) END > 1)) +------filter((if((mean = 0), 0, (stdev / mean)) > 1)) --------hashAgg[GLOBAL] ----------PhysicalDistribute ------------hashAgg[LOCAL] diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query4.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query4.out index d041a585d1..640b557743 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query4.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query4.out @@ -62,11 +62,11 @@ PhysicalCteAnchor ( cteId=CTEId#0 ) ------PhysicalDistribute --------PhysicalTopN ----------PhysicalProject -------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_secyear.customer_id)(CASE WHEN (year_total > 0.000000) THEN (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)) ELSE NULL END > CASE WHEN (year_total > 0.000000) THEN (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)) ELSE NULL END) +------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_secyear.customer_id)(if((year_total > 0.000000), (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)), NULL) > if((year_total > 0.000000), (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)), NULL)) --------------PhysicalProject ----------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_firstyear.customer_id) ------------------PhysicalProject ---------------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_c_secyear.customer_id)(CASE WHEN (year_total > 0.000000) THEN (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)) ELSE NULL END > CASE WHEN (year_total > 0.000000) THEN (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)) ELSE NULL END) +--------------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_c_secyear.customer_id)(if((year_total > 0.000000), (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)), NULL) > if((year_total > 0.000000), (cast(year_total as DOUBLE) / cast(year_total as DOUBLE)), NULL)) ----------------------PhysicalProject ------------------------hashJoin[INNER_JOIN](t_s_secyear.customer_id = t_s_firstyear.customer_id) --------------------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_c_firstyear.customer_id) diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query47.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query47.out index 3c6bf21b99..ba24810881 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query47.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query47.out @@ -43,7 +43,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 ) ------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) --------------------PhysicalDistribute ----------------------PhysicalProject -------------------------filter((CASE WHEN (avg_monthly_sales > 0.0000) THEN (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)) ELSE NULL END > 0.1)(v2.d_year = 2001)(v2.avg_monthly_sales > 0.0000)) +------------------------filter((if((avg_monthly_sales > 0.0000), (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)), NULL) > 0.1)(v2.d_year = 2001)(v2.avg_monthly_sales > 0.0000)) --------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) ----------------PhysicalDistribute ------------------PhysicalProject diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query53.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query53.out index 6e6b73448b..ad58bc9f05 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query53.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query53.out @@ -5,7 +5,7 @@ PhysicalResultSink ----PhysicalDistribute ------PhysicalTopN --------PhysicalProject -----------filter((CASE WHEN (avg_quarterly_sales > 0.0000) THEN (abs((cast(sum_sales as DOUBLE) - cast(avg_quarterly_sales as DOUBLE))) / cast(avg_quarterly_sales as DOUBLE)) ELSE NULL END > 0.1)) +----------filter((if((avg_quarterly_sales > 0.0000), (abs((cast(sum_sales as DOUBLE) - cast(avg_quarterly_sales as DOUBLE))) / cast(avg_quarterly_sales as DOUBLE)), NULL) > 0.1)) ------------PhysicalWindow --------------PhysicalQuickSort ----------------PhysicalDistribute diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query57.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query57.out index 71c2cdd060..2de32702dc 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query57.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query57.out @@ -43,7 +43,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 ) ----------------------PhysicalCteConsumer ( cteId=CTEId#0 ) ------------------PhysicalDistribute --------------------PhysicalProject -----------------------filter((CASE WHEN (avg_monthly_sales > 0.0000) THEN (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)) ELSE NULL END > 0.1)(v2.d_year = 1999)(v2.avg_monthly_sales > 0.0000)) +----------------------filter((v2.d_year = 1999)(if((avg_monthly_sales > 0.0000), (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)), NULL) > 0.1)(v2.avg_monthly_sales > 0.0000)) ------------------------PhysicalCteConsumer ( cteId=CTEId#0 ) ----------------PhysicalDistribute ------------------PhysicalProject diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query63.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query63.out index fe1486106d..2834c00bfa 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query63.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query63.out @@ -5,7 +5,7 @@ PhysicalResultSink ----PhysicalDistribute ------PhysicalTopN --------PhysicalProject -----------filter((CASE WHEN (avg_monthly_sales > 0.0000) THEN (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)) ELSE NULL END > 0.1)) +----------filter((if((avg_monthly_sales > 0.0000), (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)), NULL) > 0.1)) ------------PhysicalWindow --------------PhysicalQuickSort ----------------PhysicalDistribute diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query73.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query73.out index 34a39fb42f..534e5f4477 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query73.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query73.out @@ -25,7 +25,7 @@ PhysicalResultSink ------------------------------------PhysicalOlapScan[date_dim] ----------------------------PhysicalDistribute ------------------------------PhysicalProject ---------------------------------filter(((cast(hd_buy_potential as VARCHAR(*)) = '501-1000') OR (cast(hd_buy_potential as VARCHAR(*)) = 'Unknown'))(household_demographics.hd_vehicle_count > 0)(CASE WHEN (hd_vehicle_count > 0) THEN (cast(hd_dep_count as DOUBLE) / cast(hd_vehicle_count as DOUBLE)) ELSE NULL END > 1)) +--------------------------------filter(((cast(hd_buy_potential as VARCHAR(*)) = '501-1000') OR (cast(hd_buy_potential as VARCHAR(*)) = 'Unknown'))(household_demographics.hd_vehicle_count > 0)(if((hd_vehicle_count > 0), (cast(hd_dep_count as DOUBLE) / cast(hd_vehicle_count as DOUBLE)), NULL) > 1)) ----------------------------------PhysicalOlapScan[household_demographics] --------------------------PhysicalDistribute ----------------------------PhysicalProject diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query74.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query74.out index 370d84a764..d6477a3695 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query74.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query74.out @@ -42,7 +42,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 ) ------PhysicalDistribute --------PhysicalTopN ----------PhysicalProject -------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_firstyear.customer_id)(CASE WHEN (year_total > 0) THEN (year_total / year_total) ELSE NULL END > CASE WHEN (year_total > 0) THEN (year_total / year_total) ELSE NULL END) +------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_firstyear.customer_id)(if((year_total > 0), (year_total / year_total), NULL) > if((year_total > 0), (year_total / year_total), NULL)) --------------hashJoin[INNER_JOIN](t_s_firstyear.customer_id = t_w_secyear.customer_id) ----------------hashJoin[INNER_JOIN](t_s_secyear.customer_id = t_s_firstyear.customer_id) ------------------PhysicalDistribute diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query89.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query89.out index fff718f587..4cb57595f3 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query89.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query89.out @@ -6,7 +6,7 @@ PhysicalResultSink ------PhysicalDistribute --------PhysicalTopN ----------PhysicalProject -------------filter((CASE WHEN ( not (avg_monthly_sales = 0.0000)) THEN (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)) ELSE NULL END > 0.1)) +------------filter((if(( not (avg_monthly_sales = 0.0000)), (abs((cast(sum_sales as DOUBLE) - cast(avg_monthly_sales as DOUBLE))) / cast(avg_monthly_sales as DOUBLE)), NULL) > 0.1)) --------------PhysicalWindow ----------------PhysicalQuickSort ------------------PhysicalDistribute diff --git a/regression-test/suites/nereids_p0/expression/case_when_to_if.groovy b/regression-test/suites/nereids_p0/expression/case_when_to_if.groovy new file mode 100644 index 0000000000..8c41b8d1c6 --- /dev/null +++ b/regression-test/suites/nereids_p0/expression/case_when_to_if.groovy @@ -0,0 +1,69 @@ +// 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("test_case_when_to_if") { + sql 'set enable_nereids_planner=true' + sql 'set enable_fallback_to_original_planner=false' + + sql 'drop table if exists test_case_when_to_if;' + + sql '''create table test_case_when_to_if (k1 int, k2 int) distributed by hash(k1) buckets 3 properties('replication_num' = '1');''' + + // else is empty + sql ''' + select k2, + sum(case when (k1=1) then 1 end) sum1 + from test_case_when_to_if + group by k2; + ''' + res = sql ''' + explain rewritten plan select k2, + sum(case when (k1=1) then 1 end) sum1 + from test_case_when_to_if + group by k2; + ''' + assertTrue(res.toString().contains("if")) + + // else is null + sql ''' + select k2, + sum(case when (k1=1) then 1 else null end) sum1 + from test_case_when_to_if + group by k2; + ''' + res = sql ''' + explain rewritten plan select k2, + sum(case when (k1=1) then 1 else null end) sum1 + from test_case_when_to_if + group by k2; + ''' + assertTrue(res.toString().contains("if")) + + sql ''' + select k2, + sum(case when (k1>0) then k1 else abs(k1) end) sum1 + from test_case_when_to_if + group by k2; + ''' + res = sql ''' + explain rewritten plan select k2, + sum(case when (k1>0) then k1 else abs(k1) end) sum1 + from test_case_when_to_if + group by k2; + ''' + assertTrue(res.toString().contains("if")) +} diff --git a/regression-test/suites/nereids_p0/fold_constant/fold_constant_by_be.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy similarity index 100% rename from regression-test/suites/nereids_p0/fold_constant/fold_constant_by_be.groovy rename to regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_be.groovy diff --git a/regression-test/suites/nereids_p0/fold_constant/fold_constant_by_fe.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy similarity index 100% rename from regression-test/suites/nereids_p0/fold_constant/fold_constant_by_fe.groovy rename to regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_by_fe.groovy diff --git a/regression-test/suites/nereids_p0/fold_constant/load.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/load.groovy similarity index 100% rename from regression-test/suites/nereids_p0/fold_constant/load.groovy rename to regression-test/suites/nereids_p0/expression/fold_constant/load.groovy