This is an automated email from the ASF dual-hosted git repository.

kxiao pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new c48d4f3c608 [fix](planner)the decimal type's precision and scale of 
set operation node is wrong #32787 (#32788)
c48d4f3c608 is described below

commit c48d4f3c608316685b591d84cc3f0e9178b2caeb
Author: starocean999 <40539150+starocean...@users.noreply.github.com>
AuthorDate: Tue Mar 26 10:37:29 2024 +0800

    [fix](planner)the decimal type's precision and scale of set operation node 
is wrong #32787 (#32788)
---
 .../java/org/apache/doris/analysis/Analyzer.java   | 16 ++++++++++++++
 .../suites/nereids_p0/datatype/test_cast.groovy    | 25 ++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
index 907ecf63a34..2990e924698 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
@@ -2246,6 +2246,9 @@ public class Analyzer {
                         lastCompatibleExpr, exprLists.get(j).get(i));
                 lastCompatibleExpr = exprLists.get(j).get(i);
             }
+            if (compatibleType.isDecimalV3()) {
+                compatibleType = adjustDecimalV3PrecisionAndScale((ScalarType) 
compatibleType);
+            }
             // Now that we've found a compatible type, add implicit casts if 
necessary.
             for (int j = 0; j < exprLists.size(); ++j) {
                 if (!exprLists.get(j).get(i).getType().equals(compatibleType)) 
{
@@ -2256,6 +2259,19 @@ public class Analyzer {
         }
     }
 
+    private ScalarType adjustDecimalV3PrecisionAndScale(ScalarType 
decimalV3Type) {
+        ScalarType resultType = decimalV3Type;
+        int oldPrecision = decimalV3Type.getPrecision();
+        int oldScale = decimalV3Type.getDecimalDigits();
+        int integerPart = oldPrecision - oldScale;
+        int maxPrecision = ScalarType.MAX_DECIMAL128_PRECISION;
+        if (oldPrecision > maxPrecision) {
+            int newScale = maxPrecision - integerPart;
+            resultType = ScalarType.createDecimalType(maxPrecision, newScale < 
0 ? 0 : newScale);
+        }
+        return resultType;
+    }
+
     public long getConnectId() {
         return globalState.context.getConnectionId();
     }
diff --git a/regression-test/suites/nereids_p0/datatype/test_cast.groovy 
b/regression-test/suites/nereids_p0/datatype/test_cast.groovy
index 29a32557703..73eee81cd05 100644
--- a/regression-test/suites/nereids_p0/datatype/test_cast.groovy
+++ b/regression-test/suites/nereids_p0/datatype/test_cast.groovy
@@ -96,4 +96,29 @@ suite("test_cast") {
     }
 
     qt_select """select 1.1*1.1 + cast(1.1 as decimal);"""
+
+    sql """ DROP TABLE IF EXISTS table_decimal38_4;"""
+    sql """
+        CREATE TABLE IF NOT EXISTS table_decimal38_4 (
+            `k0` decimal(38, 4)
+        )
+        DISTRIBUTED BY HASH(`k0`) BUCKETS 5 properties("replication_num" = 
"1");
+        """
+
+    sql """ DROP TABLE IF EXISTS table_decimal27_9;"""
+    sql """
+        CREATE TABLE IF NOT EXISTS table_decimal27_9 (
+            `k0` decimal(27, 9)
+        )
+        DISTRIBUTED BY HASH(`k0`) BUCKETS 5 properties("replication_num" = 
"1");
+        """
+    explain {
+        sql """select k0 from table_decimal38_4 union all select k0 from 
table_decimal27_9;"""
+        contains """AS DECIMALV3(38, 9)"""
+    }
+    sql """set enable_nereids_planner=false;"""
+    explain {
+        sql """select k0 from table_decimal38_4 union all select k0 from 
table_decimal27_9;"""
+        contains """AS DECIMALV3(38, 9)"""
+    }
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to