Skip to content

Commit 1092587

Browse files
luozenglinpull[bot]
authored andcommitted
[fix](decimalv2) fix loss of precision when cast to decimalv2 literal (#15629)
1 parent 7cb149d commit 1092587

7 files changed

Lines changed: 35 additions & 29 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/analysis/DecimalLiteral.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ public void roundFloor(int newScale) {
335335
@Override
336336
protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
337337
if (targetType.isDecimalV2() && type.isDecimalV2()) {
338+
setType(targetType);
338339
return this;
339340
} else if ((targetType.isDecimalV3() && type.isDecimalV3()
340341
&& (((ScalarType) targetType).decimalPrecision() >= value.precision())

‎fe/fe-core/src/main/java/org/apache/doris/analysis/FloatLiteral.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
191191
return this;
192192
} else if (targetType.isDecimalV2()) {
193193
// the double constructor does an exact translation, use valueOf() instead.
194-
return new DecimalLiteral(BigDecimal.valueOf(value));
194+
DecimalLiteral res = new DecimalLiteral(BigDecimal.valueOf(value));
195+
res.setType(targetType);
196+
return res;
195197
} else if (targetType.isDecimalV3()) {
196198
DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
197199
res.setType(ScalarType.createDecimalV3Type(targetType.getPrecision(),

‎fe/fe-core/src/main/java/org/apache/doris/analysis/IntLiteral.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
310310
}
311311
} else if (targetType.isFloatingPointType()) {
312312
return new FloatLiteral(new Double(value), targetType);
313-
} else if (targetType.isDecimalV2()) {
314-
return new DecimalLiteral(new BigDecimal(value));
315-
} else if (targetType.isDecimalV3()) {
313+
} else if (targetType.isDecimalV2() || targetType.isDecimalV3()) {
316314
DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
317315
res.setType(targetType);
318316
return res;

‎fe/fe-core/src/main/java/org/apache/doris/analysis/LargeIntLiteral.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,7 @@ protected void toThrift(TExprNode msg) {
220220
protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
221221
if (targetType.isFloatingPointType()) {
222222
return new FloatLiteral(new Double(value.doubleValue()), targetType);
223-
} else if (targetType.isDecimalV2()) {
224-
return new DecimalLiteral(new BigDecimal(value));
225-
} else if (targetType.isDecimalV3()) {
223+
} else if (targetType.isDecimalV2() || targetType.isDecimalV3()) {
226224
DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
227225
res.setType(targetType);
228226
return res;

‎fe/fe-core/src/main/java/org/apache/doris/analysis/StringLiteral.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.io.DataOutput;
4242
import java.io.IOException;
4343
import java.io.UnsupportedEncodingException;
44+
import java.math.BigDecimal;
4445
import java.util.Objects;
4546

4647
public class StringLiteral extends LiteralExpr {
@@ -244,7 +245,9 @@ protected Expr uncheckedCastTo(Type targetType) throws AnalysisException {
244245
case DECIMAL32:
245246
case DECIMAL64:
246247
case DECIMAL128:
247-
return new DecimalLiteral(value);
248+
DecimalLiteral res = new DecimalLiteral(new BigDecimal(value));
249+
res.setType(targetType);
250+
return res;
248251
default:
249252
break;
250253
}

‎fe/fe-core/src/main/java/org/apache/doris/catalog/ScalarType.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,10 @@ public static ScalarType getAssignmentCompatibleType(
10261026
}
10271027

10281028
if (t1.isDecimalV2() || t2.isDecimalV2()) {
1029-
return MAX_DECIMALV2_TYPE;
1029+
if (t1.isFloatingPointType() || t2.isFloatingPointType()) {
1030+
return MAX_DECIMALV2_TYPE;
1031+
}
1032+
return t1.isDecimalV2() ? t1 : t2;
10301033
}
10311034

10321035
if ((t1.isDecimalV3() && t2.isFixedPointType()) || (t2.isDecimalV3() && t1.isFixedPointType())) {

‎regression-test/data/query_p0/union/test_union.out‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
-- This file is automatically generated. You should know what you did if you want to edit this
22
-- !union1 --
3-
123.123000000 0.1 true
3+
123.123 0.1 true
44

55
-- !union2 --
6-
123.123000000 0.1 true world
6+
123.123 0.1 true world
77

88
-- !union3 --
99

1010
-- !union4 --
11-
-654.654000000 0.0
12-
-0.123000000 987456.123
13-
0E-9 3.141592653
14-
0.666000000 -987.001
15-
123.123000000 0.1
16-
243.325000000 -0.0
17-
604587.000000000 0.1
11+
-654.654 0.0
12+
-0.123 987456.123
13+
0.000 3.141592653
14+
0.666 -987.001
15+
123.123 0.1
16+
243.325 -0.0
17+
604587.000 0.1
1818

1919
-- !union5 --
2020
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
229229
15 1992 3021 11011920 0.000 true 3.141592653 20.455999488 9999-12-12 2015-04-02T00:00
230230

231231
-- !union26 --
232-
0 1E-7
233-
1 2.0000000
234-
1 2.0000000
232+
0.0001 1E-7
233+
1.0000 2.0000000
234+
1.0100 2.0000000
235235

236236
-- !union27 --
237237
1 2
238238
hell0
239239

240240
-- !union28 --
241-
1 2.00000
241+
1.00000000 2.00000
242242

243243
-- !union29 --
244-
1 2.00000
244+
1.00000000 2.00000
245245

246246
-- !union30 --
247-
1 2.00000
248-
1 2.00000
249-
1 2.00000
247+
1.00000000 2.00000
248+
1.00000000 2.00000
249+
1.00000000 2.00000
250250

251251
-- !union31 --
252-
1 2.00000
253-
1 2.00000
252+
1.00000000 2.00000
253+
1.00000000 2.00000
254254

255255
-- !union32 --
256-
1 2.00000
256+
1.00000000 2.00000
257257

258258
-- !union33 --
259259
2016-07-01
@@ -363,3 +363,4 @@ hell0
363363
-- !union35 --
364364
2016-07-01
365365
2016-07-02
366+

0 commit comments

Comments
 (0)