From 6aec4790134ae5c836ae5bb5c1d19576b51cf54b Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Tue, 19 Mar 2024 10:09:14 +0800 Subject: [PATCH] [fix](planner)decimalv3 literal's precision and scale is not correctly set (#32288) --- .../apache/doris/analysis/DecimalLiteral.java | 3 +++ .../ExtractCommonFactorsRuleFunctionTest.java | 4 ++-- .../decimalv3/test_decimalv3_where.out | 6 ++++++ .../decimalv3/test_decimalv3_where.groovy | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java index 19389a6f2e..f64ba831c3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java @@ -310,6 +310,9 @@ public class DecimalLiteral extends NumericLiteralExpr { int integerPart = Math.max(this.value.precision() - this.value.scale(), type.getPrecision() - ((ScalarType) type).decimalScale()); this.type = ScalarType.createDecimalV3Type(integerPart + scale, scale); + BigDecimal adjustedValue = value.scale() < 0 ? value + : value.setScale(scale, RoundingMode.HALF_UP); + this.value = Objects.requireNonNull(adjustedValue); } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java b/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java index c2f31ab90a..f145f66cfa 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/rewrite/ExtractCommonFactorsRuleFunctionTest.java @@ -259,8 +259,8 @@ public class ExtractCommonFactorsRuleFunctionTest { Assert.assertTrue(planString.contains("`l_partkey` = `p_partkey`")); Assert.assertTrue(planString.contains("`l_shipmode` IN ('AIR', 'AIR REG')")); Assert.assertTrue(planString.contains("`l_shipinstruct` = 'DELIVER IN PERSON'")); - Assert.assertTrue(planString.contains("(`l_quantity` >= 9) AND (`l_quantity` <= 19) " - + "OR (`l_quantity` >= 20) AND (`l_quantity` <= 36)")); + Assert.assertTrue(planString.contains("(`l_quantity` >= 9.00) AND (`l_quantity` <= 19.00) " + + "OR (`l_quantity` >= 20.00) AND (`l_quantity` <= 36.00)")); Assert.assertTrue(planString.contains("`p_size` >= 1")); Assert.assertTrue(planString.contains("`p_brand` IN ('Brand#11', 'Brand#21', 'Brand#32')")); Assert.assertTrue(planString.contains("`p_size` <= 15")); diff --git a/regression-test/data/datatype_p0/decimalv3/test_decimalv3_where.out b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_where.out index 36084d4d21..bd3a5c0a98 100644 --- a/regression-test/data/datatype_p0/decimalv3/test_decimalv3_where.out +++ b/regression-test/data/datatype_p0/decimalv3/test_decimalv3_where.out @@ -4,3 +4,9 @@ 2 spark 10 96 1 doris 20 324 +-- !select -- +1.00100 2.00200 + +-- !select_after_update -- +1.00100 0.00010 + diff --git a/regression-test/suites/datatype_p0/decimalv3/test_decimalv3_where.groovy b/regression-test/suites/datatype_p0/decimalv3/test_decimalv3_where.groovy index 287a77cbd1..18b3bf7c91 100644 --- a/regression-test/suites/datatype_p0/decimalv3/test_decimalv3_where.groovy +++ b/regression-test/suites/datatype_p0/decimalv3/test_decimalv3_where.groovy @@ -23,4 +23,23 @@ suite("test_decimalv3_where") { sql """CREATE TABLE ${tableName} ( `id` varchar(11) NULL COMMENT '唯一标识', `name` varchar(10) NULL COMMENT '采集时间', `age` int(11) NULL, `dr` decimalv3(10, 0) ) ENGINE=OLAP UNIQUE KEY(`id`) COMMENT 'test' DISTRIBUTED BY HASH(`id`) BUCKETS 10 PROPERTIES ( "replication_allocation" = "tag.location.default: 1", "in_memory" = "false", "storage_format" = "V2", "light_schema_change" = "true", "disable_auto_compaction" = "false" );""" sql """insert into ${tableName} values (1,'doris',20,324.10),(2,'spark',10,95.5),(3,'flink',9,20)""" qt_decimalv3 "select * from ${tableName} where dr != 1 order by age;" + + sql """set enable_nereids_planner=false;""" + sql """drop table if exists test_sys_update_basic_test_update_decimal_tb""" + sql """CREATE TABLE test_sys_update_basic_test_update_decimal_tb ( + k1 DECIMAL(10, 5) NULL, + v1 DECIMAL(10, 5) NULL + ) UNIQUE KEY(k1) DISTRIBUTED BY HASH(k1) BUCKETS 5 PROPERTIES ( + "replication_allocation" = "tag.location.default: 1" + );""" + sql """insert into test_sys_update_basic_test_update_decimal_tb values + (1.001, 2.002), (1.002, 0.00000002), (1.003, 0.100000001), (1.004, 0.100044001), (1.005, 0.100045001);""" + qt_select """select * from test_sys_update_basic_test_update_decimal_tb where k1 = 1.001;""" + + sql """ + UPDATE test_sys_update_basic_test_update_decimal_tb SET v1=0.0001 WHERE k1 = 1.001; + """ + qt_select_after_update """ + select * from test_sys_update_basic_test_update_decimal_tb where k1 = 1.001; + """ }