You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by ya...@apache.org on 2023/01/11 10:06:26 UTC
[doris] branch branch-1.2-lts updated: [fix](decimalv2) fix loss of precision when cast to decimalv2 literal (#15629)
This is an automated email from the ASF dual-hosted git repository.
yangzhg pushed a commit to branch branch-1.2-lts
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-1.2-lts by this push:
new 1f0cb7dda3 [fix](decimalv2) fix loss of precision when cast to decimalv2 literal (#15629)
1f0cb7dda3 is described below
commit 1f0cb7dda3fb967b2063191eaef79f3ef2b8bec8
Author: luozenglin <37...@users.noreply.github.com>
AuthorDate: Fri Jan 6 16:02:46 2023 +0800
[fix](decimalv2) fix loss of precision when cast to decimalv2 literal (#15629)
---
.../org/apache/doris/analysis/DecimalLiteral.java | 1 +
.../org/apache/doris/analysis/FloatLiteral.java | 4 +-
.../java/org/apache/doris/analysis/IntLiteral.java | 4 +-
.../org/apache/doris/analysis/LargeIntLiteral.java | 4 +-
.../org/apache/doris/analysis/StringLiteral.java | 5 ++-
.../java/org/apache/doris/catalog/ScalarType.java | 5 ++-
regression-test/data/query_p0/union/test_union.out | 44 ++++++++++++----------
7 files changed, 38 insertions(+), 29 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 e07d246b83..0a445857d2 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
@@ -335,6 +335,7 @@ public class DecimalLiteral extends LiteralExpr {
@Override
protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
if (targetType.isDecimalV2() && type.isDecimalV2()) {
+ setType(targetType);
return this;
} else if ((targetType.isDecimalV3() && type.isDecimalV3()
&& (((ScalarType) targetType).decimalPrecision() >= value.precision())
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
index b6b9fb14fe..f9fd62dedc 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java
@@ -191,7 +191,9 @@ public class FloatLiteral extends LiteralExpr {
return this;
} else if (targetType.isDecimalV2()) {
// the double constructor does an exact translation, use valueOf() instead.
- return new DecimalLiteral(BigDecimal.valueOf(value));
+ DecimalLiteral res = new DecimalLiteral(BigDecimal.valueOf(value));
+ res.setType(targetType);
+ return res;
} else if (targetType.isDecimalV3()) {
DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
res.setType(ScalarType.createDecimalV3Type(res.getType().getPrecision(),
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java
index ed90678bcc..0d3749fa86 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java
@@ -310,9 +310,7 @@ public class IntLiteral extends LiteralExpr {
}
} else if (targetType.isFloatingPointType()) {
return new FloatLiteral(new Double(value), targetType);
- } else if (targetType.isDecimalV2()) {
- return new DecimalLiteral(new BigDecimal(value));
- } else if (targetType.isDecimalV3()) {
+ } else if (targetType.isDecimalV2() || targetType.isDecimalV3()) {
DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
res.setType(targetType);
return res;
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
index 654ef89d9b..9d19179704 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java
@@ -220,9 +220,7 @@ public class LargeIntLiteral extends LiteralExpr {
protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
if (targetType.isFloatingPointType()) {
return new FloatLiteral(new Double(value.doubleValue()), targetType);
- } else if (targetType.isDecimalV2()) {
- return new DecimalLiteral(new BigDecimal(value));
- } else if (targetType.isDecimalV3()) {
+ } else if (targetType.isDecimalV2() || targetType.isDecimalV3()) {
DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
res.setType(targetType);
return res;
diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java
index 89e4de09f5..9fa1690e44 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java
@@ -41,6 +41,7 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
+import java.math.BigDecimal;
import java.util.Objects;
public class StringLiteral extends LiteralExpr {
@@ -244,7 +245,9 @@ public class StringLiteral extends LiteralExpr {
case DECIMAL32:
case DECIMAL64:
case DECIMAL128:
- return new DecimalLiteral(value);
+ DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
+ res.setType(targetType);
+ return res;
default:
break;
}
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/ScalarType.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/ScalarType.java
index 4a58b2457d..ae2c04b3fd 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/ScalarType.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/ScalarType.java
@@ -1027,7 +1027,10 @@ public class ScalarType extends Type {
}
if (t1.isDecimalV2() || t2.isDecimalV2()) {
- return MAX_DECIMALV2_TYPE;
+ if (t1.isFloatingPointType() || t2.isFloatingPointType()) {
+ return MAX_DECIMALV2_TYPE;
+ }
+ return t1.isDecimalV2() ? t1 : t2;
}
if ((t1.isDecimalV3() && t2.isFixedPointType()) || (t2.isDecimalV3() && t1.isFixedPointType())) {
diff --git a/regression-test/data/query_p0/union/test_union.out b/regression-test/data/query_p0/union/test_union.out
index 302eb78140..fe0f9c1810 100644
--- a/regression-test/data/query_p0/union/test_union.out
+++ b/regression-test/data/query_p0/union/test_union.out
@@ -1,20 +1,20 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !union1 --
-123.123000000 0.1 true
+123.123 0.1 true
-- !union2 --
-123.123000000 0.1 true world
+123.123 0.1 true world
-- !union3 --
-- !union4 --
--654.654000000 0.0
--0.123000000 987456.123
-0E-9 3.141592653
-0.666000000 -987.001
-123.123000000 0.1
-243.325000000 -0.0
-604587.000000000 0.1
+-654.654 0.0
+-0.123 987456.123
+0.000 3.141592653
+0.666 -987.001
+123.123 0.1
+243.325 -0.0
+604587.000 0.1
-- !union5 --
false 1 1989 1001 11011902 123.123 true 1989-03-21 1989-03-21T13:00 wangjuoo4 0.1 6.333 string12345 170141183460469231731687303715884105727
@@ -229,31 +229,31 @@ false 3 1989 1002 11011905 24453.325 false 2012-03-14 2000-01-01T00:00 yunlj8@nk
15 1992 3021 11011920 0.000 true 3.141592653 20.455999488 9999-12-12 2015-04-02T00:00
-- !union26 --
-0 1E-7
-1 2.0000000
-1 2.0000000
+0.0001 1E-7
+1.0000 2.0000000
+1.0100 2.0000000
-- !union27 --
1 2
hell0
-- !union28 --
-1 2.00000
+1.00000000 2.00000
-- !union29 --
-1 2.00000
+1.00000000 2.00000
-- !union30 --
-1 2.00000
-1 2.00000
-1 2.00000
+1.00000000 2.00000
+1.00000000 2.00000
+1.00000000 2.00000
-- !union31 --
-1 2.00000
-1 2.00000
+1.00000000 2.00000
+1.00000000 2.00000
-- !union32 --
-1 2.00000
+1.00000000 2.00000
-- !union33 --
2016-07-01
@@ -360,3 +360,7 @@ hell0
11011903
11011905
+-- !union35 --
+2016-07-01
+2016-07-02
+
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org