[fix](planner)avg function may use wrong decimal precision and scale (#30364)

This commit is contained in:
starocean999
2024-01-29 15:26:19 +08:00
committed by yiguolei
parent 90c0806178
commit 3b85e3de1b
3 changed files with 20 additions and 2 deletions

View File

@ -971,8 +971,20 @@ public class FunctionCallExpr extends Expr {
// DecimalV3 scale lower than DEFAULT_MIN_AVG_DECIMAL128_SCALE should do cast
if (fnName.getFunction().equalsIgnoreCase("avg") && arg.type.isDecimalV3()
&& arg.type.getDecimalDigits() < ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE) {
Type t = ScalarType.createDecimalType(arg.type.getPrimitiveType(), arg.type.getPrecision(),
ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE);
int precision = arg.type.getPrecision();
int scale = arg.type.getDecimalDigits();
precision = precision - scale + ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE;
scale = ScalarType.DEFAULT_MIN_AVG_DECIMAL128_SCALE;
if (SessionVariable.getEnableDecimal256()) {
if (precision > ScalarType.MAX_DECIMAL256_PRECISION) {
precision = ScalarType.MAX_DECIMAL256_PRECISION;
}
} else {
if (precision > ScalarType.MAX_DECIMAL128_PRECISION) {
precision = ScalarType.MAX_DECIMAL128_PRECISION;
}
}
Type t = ScalarType.createDecimalType(arg.type.getPrimitiveType(), precision, scale);
Expr e = getChild(0).castTo(t);
setChild(0, e);
}