Skip to content

Commit b09d16b

Browse files
committed
[fix](Nereids) tablet prune wrong when decimal value scale is nagtive (apache#37889)
pick from master apache#37889 we use unscaled value of BigDecimal in tablet prune. So we need to ensure BigDecimal's precision and scale is same with the literal contains it.
1 parent 4d359cd commit b09d16b

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/SimplifyDecimalV3Comparison.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.common.collect.ImmutableList;
3030

3131
import java.math.BigDecimal;
32+
import java.math.RoundingMode;
3233
import java.util.List;
3334

3435
/**
@@ -72,10 +73,15 @@ private static Expression doProcess(ComparisonPredicate cp, Cast left, DecimalV3
7273
BigDecimal trailingZerosValue = right.getValue().stripTrailingZeros();
7374
int scale = org.apache.doris.analysis.DecimalLiteral.getBigDecimalScale(trailingZerosValue);
7475
int precision = org.apache.doris.analysis.DecimalLiteral.getBigDecimalPrecision(trailingZerosValue);
76+
try {
77+
trailingZerosValue = trailingZerosValue.setScale(scale, RoundingMode.UNNECESSARY);
78+
} catch (ArithmeticException e) {
79+
return cp;
80+
}
81+
7582
Expression castChild = left.child();
7683
Preconditions.checkState(castChild.getDataType() instanceof DecimalV3Type);
7784
DecimalV3Type leftType = (DecimalV3Type) castChild.getDataType();
78-
7985
if (scale <= leftType.getScale() && precision - scale <= leftType.getPrecision() - leftType.getScale()) {
8086
// precision and scale of literal all smaller than left, we don't need the cast
8187
DecimalV3Literal newRight = new DecimalV3Literal(
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !test_decimal_prune --
3+
20240750 550
4+

regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query28.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ PhysicalResultSink
3333
--------------------------PhysicalDistribute[DistributionSpecGather]
3434
----------------------------hashAgg[LOCAL]
3535
------------------------------PhysicalProject
36-
--------------------------------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))
36+
--------------------------------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))
3737
----------------------------------PhysicalOlapScan[store_sales]
3838
----------------PhysicalDistribute[DistributionSpecReplicated]
3939
------------------PhysicalLimit[LOCAL]

regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query28.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ PhysicalResultSink
3333
--------------------------PhysicalDistribute[DistributionSpecGather]
3434
----------------------------hashAgg[LOCAL]
3535
------------------------------PhysicalProject
36-
--------------------------------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))
36+
--------------------------------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))
3737
----------------------------------PhysicalOlapScan[store_sales]
3838
----------------PhysicalDistribute[DistributionSpecReplicated]
3939
------------------PhysicalLimit[LOCAL]

regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query28.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ PhysicalResultSink
3333
--------------------------PhysicalDistribute[DistributionSpecGather]
3434
----------------------------hashAgg[LOCAL]
3535
------------------------------PhysicalProject
36-
--------------------------------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))
36+
--------------------------------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))
3737
----------------------------------PhysicalOlapScan[store_sales]
3838
----------------PhysicalDistribute[DistributionSpecReplicated]
3939
------------------PhysicalLimit[LOCAL]

regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query28.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ PhysicalResultSink
3333
--------------------------PhysicalDistribute[DistributionSpecGather]
3434
----------------------------hashAgg[LOCAL]
3535
------------------------------PhysicalProject
36-
--------------------------------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))
36+
--------------------------------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))
3737
----------------------------------PhysicalOlapScan[store_sales]
3838
----------------PhysicalDistribute[DistributionSpecReplicated]
3939
------------------PhysicalLimit[LOCAL]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
suite("test_decimal_type") {
18+
19+
sql """
20+
drop table if exists test_decimal_type
21+
"""
22+
23+
sql """
24+
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");
25+
"""
26+
27+
sql """
28+
insert into test_decimal_type values (20240710, 110), (20240720,220), (20240120, 120), (20240730, 330), (20240740,440),(20240750,550);
29+
"""
30+
31+
sql """
32+
sync
33+
"""
34+
35+
order_qt_test_decimal_prune """
36+
select * from test_decimal_type where c1 = 20240750 and c2 = 550
37+
"""
38+
}

0 commit comments

Comments
 (0)