diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDecimalV3Comparison.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDecimalV3Comparison.java index c3c3c17dd55f42..450e9cd6055d2c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDecimalV3Comparison.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDecimalV3Comparison.java @@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableList; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.List; /** @@ -72,10 +73,15 @@ private static Expression doProcess(ComparisonPredicate cp, Cast left, DecimalV3 BigDecimal trailingZerosValue = right.getValue().stripTrailingZeros(); int scale = org.apache.doris.analysis.DecimalLiteral.getBigDecimalScale(trailingZerosValue); int precision = org.apache.doris.analysis.DecimalLiteral.getBigDecimalPrecision(trailingZerosValue); + try { + trailingZerosValue = trailingZerosValue.setScale(scale, RoundingMode.UNNECESSARY); + } catch (ArithmeticException e) { + return cp; + } + Expression castChild = left.child(); Preconditions.checkState(castChild.getDataType() instanceof DecimalV3Type); DecimalV3Type leftType = (DecimalV3Type) castChild.getDataType(); - if (scale <= leftType.getScale() && precision - scale <= leftType.getPrecision() - leftType.getScale()) { // precision and scale of literal all smaller than left, we don't need the cast DecimalV3Literal newRight = new DecimalV3Literal( diff --git a/regression-test/data/nereids_rules_p0/olap_scan_tablet_prune/test_decimal_type.out b/regression-test/data/nereids_rules_p0/olap_scan_tablet_prune/test_decimal_type.out new file mode 100644 index 00000000000000..1c2095fe8c30b5 --- /dev/null +++ b/regression-test/data/nereids_rules_p0/olap_scan_tablet_prune/test_decimal_type.out @@ -0,0 +1,4 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !test_decimal_prune -- +20240750 550 + diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query28.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query28.out index 72124f34ee3568..ae9c833b99f9ac 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query28.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query28.out @@ -33,7 +33,7 @@ PhysicalResultSink --------------------------PhysicalDistribute[DistributionSpecGather] ----------------------------hashAgg[LOCAL] ------------------------------PhysicalProject ---------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 1.6E+2)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7.6E+3))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) +--------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 160.00)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7600.00))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) ----------------------------------PhysicalOlapScan[store_sales] ----------------PhysicalDistribute[DistributionSpecReplicated] ------------------PhysicalLimit[LOCAL] diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query28.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query28.out index 72124f34ee3568..ae9c833b99f9ac 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query28.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query28.out @@ -33,7 +33,7 @@ PhysicalResultSink --------------------------PhysicalDistribute[DistributionSpecGather] ----------------------------hashAgg[LOCAL] ------------------------------PhysicalProject ---------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 1.6E+2)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7.6E+3))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) +--------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 160.00)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7600.00))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) ----------------------------------PhysicalOlapScan[store_sales] ----------------PhysicalDistribute[DistributionSpecReplicated] ------------------PhysicalLimit[LOCAL] diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query28.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query28.out index 72124f34ee3568..ae9c833b99f9ac 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query28.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query28.out @@ -33,7 +33,7 @@ PhysicalResultSink --------------------------PhysicalDistribute[DistributionSpecGather] ----------------------------hashAgg[LOCAL] ------------------------------PhysicalProject ---------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 1.6E+2)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7.6E+3))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) +--------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 160.00)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7600.00))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) ----------------------------------PhysicalOlapScan[store_sales] ----------------PhysicalDistribute[DistributionSpecReplicated] ------------------PhysicalLimit[LOCAL] diff --git a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query28.out b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query28.out index 72124f34ee3568..ae9c833b99f9ac 100644 --- a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query28.out +++ b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query28.out @@ -33,7 +33,7 @@ PhysicalResultSink --------------------------PhysicalDistribute[DistributionSpecGather] ----------------------------hashAgg[LOCAL] ------------------------------PhysicalProject ---------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 1.6E+2)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7.6E+3))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) +--------------------------------filter(((((store_sales.ss_list_price >= 150.00) AND (store_sales.ss_list_price <= 160.00)) OR ((store_sales.ss_coupon_amt >= 6600.00) AND (store_sales.ss_coupon_amt <= 7600.00))) OR ((store_sales.ss_wholesale_cost >= 9.00) AND (store_sales.ss_wholesale_cost <= 29.00))) and (store_sales.ss_quantity <= 15) and (store_sales.ss_quantity >= 11)) ----------------------------------PhysicalOlapScan[store_sales] ----------------PhysicalDistribute[DistributionSpecReplicated] ------------------PhysicalLimit[LOCAL] diff --git a/regression-test/suites/nereids_rules_p0/olap_scan_tablet_prune/test_decimal_type.groovy b/regression-test/suites/nereids_rules_p0/olap_scan_tablet_prune/test_decimal_type.groovy new file mode 100644 index 00000000000000..779e70b36c1fb6 --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/olap_scan_tablet_prune/test_decimal_type.groovy @@ -0,0 +1,38 @@ +// 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_decimal_type") { + + sql """ + drop table if exists test_decimal_type + """ + + sql """ + create table test_decimal_type (c1 decimal(8, 0), c2 decimal(18, 0)) duplicate key(c1, c2) distributed by hash(c1, c2) buckets 10 properties("replication_num"="1"); + """ + + sql """ + insert into test_decimal_type values (20240710, 110), (20240720,220), (20240120, 120), (20240730, 330), (20240740,440),(20240750,550); + """ + + sql """ + sync + """ + + order_qt_test_decimal_prune """ + select * from test_decimal_type where c1 = 20240750 and c2 = 550 + """ +}