[bug](decimalv3) Fix wrong decimal scale for arithmetic expr (#15316)

This commit is contained in:
Gabriel
2022-12-24 21:57:46 +08:00
committed by GitHub
parent 6151a43e9c
commit 82d316b419
3 changed files with 28 additions and 5 deletions

View File

@ -533,14 +533,14 @@ public class ArithmeticExpr extends Expr {
scale = Math.max(t1Scale, t2Scale);
precision = Math.max(widthOfIntPart1, widthOfIntPart2) + scale;
}
if (precision < scale) {
type = castBinaryOp(Type.DOUBLE);
break;
}
if (precision > ScalarType.MAX_DECIMAL128_PRECISION) {
// TODO(gabriel): if precision is bigger than 38?
precision = ScalarType.MAX_DECIMAL128_PRECISION;
}
if (precision < scale) {
type = castBinaryOp(Type.DOUBLE);
break;
}
type = ScalarType.createDecimalV3Type(precision, scale);
if (op == Operator.ADD || op == Operator.SUBTRACT) {
if (!Type.matchExactType(type, children.get(0).type)) {
@ -550,7 +550,12 @@ public class ArithmeticExpr extends Expr {
castChild(type, 1);
}
} else if (op == Operator.DIVIDE && (t2Scale != 0) && t1.isDecimalV3()) {
castChild(ScalarType.createDecimalV3Type(precision, t1Scale + t2Scale), 0);
int targetScale = t1Scale + t2Scale;
if (precision < targetScale) {
type = castBinaryOp(Type.DOUBLE);
break;
}
castChild(ScalarType.createDecimalV3Type(precision, targetScale), 0);
}
break;
case INT_DIVIDE:

View File

@ -17,3 +17,18 @@
1.440000000000000000000000000000000000
1.800000000000000000000000000000000000
-- !select --
1.7160000000000002
1.8719999999999999
2.34
-- !select --
2.9446560000000006
3.504384
5.4756
-- !select --
1.7424
2.0736
3.2399999999999998

View File

@ -45,5 +45,8 @@ suite("test_arithmetic_expressions") {
qt_select "select k1 * k2 from ${table1} order by k1"
qt_select "select * from (select k1 * k2 from ${table1} union all select k3 from ${table1}) a order by 1"
qt_select "select k1 * k2 * k3 from ${table1} order by k1"
qt_select "select k1 * k2 * k3 * k1 * k2 * k3 from ${table1} order by k1"
qt_select "select k1 * k2 / k3 * k1 * k2 * k3 from ${table1} order by k1"
sql "drop table if exists ${table1}"
}