Skip to content

Commit 025b349

Browse files
nokiaMSgithubgxll
authored andcommitted
[feat][expr] Fix int and float result overflow in some operations.
1 parent b45289f commit 025b349

File tree

6 files changed

+75
-28
lines changed

6 files changed

+75
-28
lines changed

coding/src/test/java/io/dingodb/expr/coding/TestExprCoder.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,21 @@ public class TestExprCoder {
5757
arguments(val(3.1415926), "15400921FB4D12D84A"),
5858
arguments(val(3E8), "1541B1E1A300000000"),
5959
arguments(val("abc"), "1703616263"),
60-
arguments(op(ADD, val(1), val(1)), "110111018301"),
61-
arguments(op(ADD, val(2), val(3)), "110211038301"),
60+
61+
//{CAST{ CONST{1} -> BIGINT}} BIGINT_ADD {CAST{ CONST{1} -> BIGINT}}
62+
arguments(op(ADD, val(1), val(1)), "1101F0211101F0218302"),
63+
64+
//{CAST{{CONST INT} 2 -> BIGINT} BIGINT_ADD CAST{{CONST INT} 3 -> BIGINT}}
65+
arguments(op(ADD, val(2), val(3)), "1102f0211103f0218302"),
66+
6267
arguments(op(ADD, val(1L), val(1L)), "120112018302"),
6368
arguments(op(ADD, val(2L), val(3L)), "120212038302"),
64-
arguments(op(ADD, val(3), op(MUL, val(4), val(6))), "11031104110685018301"),
65-
arguments(op(EQ, op(ADD, val(5), val(6)), val(11)), "110511068301110B9101"),
69+
arguments(op(ADD, val(3), op(MUL, val(4), val(6))), "11031104F0211106F0218502"),
70+
arguments(op(EQ, op(ADD, val(5), val(6)), val(11)), "1105F0211106F0218302110BF0219102"),
6671
arguments(op(GT, val("abc"), val("a")), "17036162631701619307"),
6772
arguments(
6873
op(AND, op(GT, op(ADD, val(7), val(8)), val(14)), op(LT, val(6), val(5))),
69-
"110711088301110E930111061105950152"
74+
"1107F0211108F0218302110EF021930211061105950152"
7075
),
7176
arguments(op(TO_LONG, val(21)), "1115F021"),
7277
arguments(op(AND, val(false), val(null, Types.BOOL)), "230352"),

runtime/src/main/java/io/dingodb/expr/runtime/op/BinaryOp.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package io.dingodb.expr.runtime.op;
1818

19+
import io.dingodb.expr.common.type.FloatType;
20+
import io.dingodb.expr.common.type.IntType;
1921
import io.dingodb.expr.common.type.Type;
22+
import io.dingodb.expr.common.type.Types;
2023
import io.dingodb.expr.runtime.EvalContext;
2124
import io.dingodb.expr.runtime.ExprConfig;
2225
import io.dingodb.expr.runtime.exception.EvalNotImplemented;
@@ -63,10 +66,33 @@ public OpKey bestKeyOf(@NonNull Type @NonNull [] types) {
6366
Type type0 = operand0.getType();
6467
Type type1 = operand1.getType();
6568
BinaryOp op = getOp(keyOf(type0, type1));
66-
if (op != null) {
69+
boolean needCast = false;
70+
71+
if (op != null && type0 instanceof IntType && type1 instanceof IntType
72+
&& (op.getOpType() == OpType.ADD || op.getOpType() == OpType.SUB
73+
|| op.getOpType() == OpType.MUL || op.getOpType() == OpType.DIV)) {
74+
needCast = true;
75+
} else if (op != null && type0 instanceof FloatType && type1 instanceof FloatType
76+
&& (op.getOpType() == OpType.ADD || op.getOpType() == OpType.SUB
77+
|| op.getOpType() == OpType.MUL || op.getOpType() == OpType.DIV)) {
78+
needCast = true;
79+
}
80+
81+
if (op != null && !needCast) {
6782
result = op.createExpr(operand0, operand1);
6883
} else {
69-
Type[] types = new Type[]{type0, type1};
84+
Type[] types;
85+
if (op != null && type0 instanceof IntType && type1 instanceof IntType
86+
&& (op.getOpType() == OpType.ADD || op.getOpType() == OpType.SUB || op.getOpType() == OpType.MUL)) {
87+
types = new Type[]{Types.LONG, Types.LONG};
88+
} else if (op != null && type0 instanceof FloatType && type1 instanceof FloatType
89+
&& (op.getOpType() == OpType.ADD || op.getOpType() == OpType.SUB
90+
|| op.getOpType() == OpType.MUL || op.getOpType() == OpType.DIV)) {
91+
types = new Type[]{Types.DOUBLE, Types.DOUBLE};
92+
} else {
93+
types = new Type[]{type0, type1};
94+
}
95+
7096
BinaryOp op1 = getOp(bestKeyOf(types));
7197
if (op1 != null) {
7298
result = op1.createExpr(doCast(operand0, types[0], config), doCast(operand1, types[1], config));

test/src/test/java/io/dingodb/expr/test/TestToDebugString.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,24 @@ public class TestToDebugString {
4747

4848
private static @NonNull Stream<Arguments> getParameters1() {
4949
return Stream.of(
50-
arguments("1 + 2", "AddIntInt[ADD](Val[1, INT], Val[2, INT])"),
51-
arguments("1 + 2*3", "AddIntInt[ADD](Val[1, INT], MulIntInt[MUL](Val[2, INT], Val[3, INT]))"),
52-
arguments("1*(2 + 3)", "MulIntInt[MUL](Val[1, INT], AddIntInt[ADD](Val[2, INT], Val[3, INT]))")
50+
arguments("1 + 2",
51+
"AddLongLong[ADD]("
52+
+
53+
"LongCastInt[CASTLONG](Val[1, INT]), "
54+
+
55+
"LongCastInt[CASTLONG](Val[2, INT]))"),
56+
arguments("1 + 2*3",
57+
"AddAnyAny[ADD]("
58+
+
59+
"Val[1, INT], MulLongLong[MUL](LongCastInt[CASTLONG](Val[2, INT]), "
60+
+
61+
"LongCastInt[CASTLONG](Val[3, INT])))"),
62+
arguments("1*(2 + 3)",
63+
"MulLongLong[MUL]("
64+
+
65+
"LongCastInt[CASTLONG](Val[1, INT]), "
66+
+
67+
"AddLongLong[ADD](LongCastInt[CASTLONG](Val[2, INT]), LongCastInt[CASTLONG](Val[3, INT])))")
5368
);
5469
}
5570

test/src/test/java/io/dingodb/expr/test/TestToString.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ public class TestToString {
6161

6262
private static @NonNull Stream<Arguments> getParameters1() {
6363
return Stream.of(
64-
arguments("1 + 2", "1 + 2"),
65-
arguments("1 + 2*3", "1 + 2*3"),
66-
arguments("1*(2 + 3)", "1*(2 + 3)"),
64+
arguments("1 + 2", "CASTLONG(1) + CASTLONG(2)"),
65+
arguments("1 + 2*3", "1 + CASTLONG(2)*CASTLONG(3)"),
66+
arguments("1*(2 + 3)",
67+
"CASTLONG(1)*(CASTLONG(2) + CASTLONG(3))"),
6768
arguments("1 < 2 && 3 < 4 || false", "1 < 2 && 3 < 4 || false"),
6869
//arguments("5 + a*3.2 - 1.0", "CASTDECIMAL(5) + CASTDECIMAL($[0])*3.2 - 1.0"),
6970
//arguments("(a + (c + b)) * max(a, b)",
@@ -91,9 +92,9 @@ public class TestToString {
9192

9293
private static @NonNull Stream<Arguments> getParameters2() {
9394
return Stream.of(
94-
arguments("1 + 2", "3"),
95-
arguments("1 + 2*3", "7"),
96-
arguments("1*(2 + 3)", "5"),
95+
arguments("1 + 2", "LONG(3)"),
96+
arguments("1 + 2*3", "LONG(7)"),
97+
arguments("1*(2 + 3)", "LONG(5)"),
9798
arguments("1 < 2 && 3 < 4 or false", "true"),
9899
//arguments("5 + a*3.2 - 1.0", "DECIMAL(5) + CASTDECIMAL($[0])*3.2 - 1.0"),
99100
//arguments("(a + (c + b)) * max(a, b)",

test/src/test/java/io/dingodb/expr/test/cases/EvalConstProvider.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,28 +302,28 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
302302
arguments(op(NEG, 1.1f), -1.1f),
303303
arguments(op(NEG, 1.1), -1.1),
304304
arguments(op(NEG, dec(1.1)), BigDecimal.valueOf(-1.1)),
305-
arguments(op(ADD, 1, 2), 3),
305+
arguments(op(ADD, 1, 2), 3L),
306306
arguments(op(ADD, 1L, 2L), 3L),
307-
arguments(op(ADD, 1.1f, 2.2f), 3.3f),
307+
arguments(op(ADD, 1.1f, 2.2f), 3.3d),
308308
arguments(op(ADD, 1.1, 2.2), 3.3),
309309
arguments(op(ADD, dec(1.1), dec(2.2)), BigDecimal.valueOf(3.3)),
310310
arguments(op(ADD, 1, 2L), 3L),
311311
arguments(op(ADD, 1L, 2.2f), 3.2f),
312312
arguments(op(ADD, 1.1f, 2.2), 3.3),
313313
arguments(op(ADD, 1.1, dec(2.2)), BigDecimal.valueOf(3.3)),
314314
//arguments(op(ADD, "a", "bc"), "0"),
315-
arguments(op(SUB, 1, 2), -1),
315+
arguments(op(SUB, 1, 2), -1L),
316316
arguments(op(SUB, 1L, 2L), -1L),
317-
arguments(op(SUB, 1.1f, 2.2f), -1.1f),
317+
arguments(op(SUB, 1.1f, 2.2f), -1.1d),
318318
arguments(op(SUB, 1.1, 2.2), -1.1),
319319
arguments(op(SUB, dec(1.1), dec(2.2)), BigDecimal.valueOf(-1.1)),
320320
arguments(op(SUB, 1, 2L), -1L),
321321
arguments(op(SUB, 1L, 2.2f), -1.2f),
322322
arguments(op(SUB, 1.1f, 2.2), -1.1),
323323
arguments(op(SUB, 1.1, dec(2.2)), BigDecimal.valueOf(-1.1)),
324-
arguments(op(MUL, 1, 2), 2),
324+
arguments(op(MUL, 1, 2), 2L),
325325
arguments(op(MUL, 1L, 2L), 2L),
326-
arguments(op(MUL, 1.1f, 2.2f), 2.42f),
326+
arguments(op(MUL, 1.1f, 2.2f), 2.42d),
327327
arguments(op(MUL, 1.1, 2.2), 2.42),
328328
arguments(op(MUL, dec(1.1), dec(2.2)), BigDecimal.valueOf(2.42)),
329329
arguments(op(MUL, 1, 2L), 2L),
@@ -332,7 +332,7 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
332332
arguments(op(MUL, 1.1, dec(2.2)), BigDecimal.valueOf(2.42)),
333333
//arguments(op(DIV, 1, 2), 0.5),
334334
//arguments(op(DIV, 1L, 2L), 0.5),
335-
arguments(op(DIV, 1.1f, 2.2f), 0.5f),
335+
arguments(op(DIV, 1.1f, 2.2f), 0.5d),
336336
arguments(op(DIV, 1.1, 2.2), 0.5),
337337
arguments(op(DIV, dec(1.1), dec(2.2)), BigDecimal.valueOf(0.5).setScale(5)),
338338
//arguments(op(DIV, 1, 2L), 0.5),

test/src/test/java/io/dingodb/expr/test/cases/ParseEvalConstProvider.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,18 @@ public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
8989
// Arithmetics
9090
arguments("+ null", null),
9191
arguments("- null", null),
92-
arguments("1 + 2", 3),
92+
arguments("1 + 2", 3L),
9393
arguments("null + null", null),
94-
arguments("1 + 2 * 3", 7),
95-
arguments("(1 + 2) * 3", 9),
96-
arguments("(1 + 2) * (5 - (3 + 4))", -6),
94+
arguments("1 + 2 * 3", 7L),
95+
arguments("(1 + 2) * 3", 9L),
96+
arguments("(1 + 2) * (5 - (3 + 4))", -6L),
9797
arguments("3 * 1.5 + 2.34", new BigDecimal("6.84")),
9898
arguments("2 * -3.14e2", new BigDecimal("-6.28e2")),
9999
arguments("5e4 + 3e3", new BigDecimal("5.3e4")),
100100
//arguments("1 / 100", 0.01),
101101
arguments("1.0 / 100", new BigDecimal("0.01000")),
102102
arguments("double(1.0) / 100", 0.01),
103-
arguments("1 + (2 * 3-4)", 3),
103+
arguments("1 + (2 * 3-4)", 3L),
104104

105105
// Relations & logics
106106
arguments("3 < 4", true),

0 commit comments

Comments
 (0)