[DataType](Deciamlv3) change the avg function scale of decimalv3 (#15445)

This commit is contained in:
HappenLee
2022-12-30 00:27:51 +08:00
committed by GitHub
parent 73f7ccb58f
commit 9a517d6a8f
67 changed files with 4811 additions and 4802 deletions

View File

@ -125,7 +125,7 @@ public class FunctionCallExpr extends Expr {
Preconditions.checkArgument(children != null && children.size() > 0);
if (children.get(0).getType().isDecimalV3()) {
return ScalarType.createDecimalV3Type(ScalarType.MAX_DECIMAL128_PRECISION,
((ScalarType) children.get(0).getType()).getScalarScale());
Math.max(((ScalarType) children.get(0).getType()).getScalarScale(), 4));
} else {
return returnType;
}
@ -739,6 +739,14 @@ public class FunctionCallExpr extends Expr {
&& ((!arg.type.isNumericType() && !arg.type.isNull()) || arg.type.isOnlyMetricType())) {
throw new AnalysisException(fnName.getFunction() + " requires a numeric parameter: " + this.toSql());
}
// 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);
Expr e = getChild(0).castTo(t);
setChild(0, e);
}
if (fnName.getFunction().equalsIgnoreCase("sum_distinct")
&& ((!arg.type.isNumericType() && !arg.type.isNull()) || arg.type.isOnlyMetricType())) {
throw new AnalysisException(

View File

@ -84,6 +84,7 @@ public class ScalarType extends Type {
public static final int MAX_DECIMAL32_PRECISION = 9;
public static final int MAX_DECIMAL64_PRECISION = 18;
public static final int MAX_DECIMAL128_PRECISION = 38;
public static final int DEFAULT_MIN_AVG_DECIMAL128_SCALE = 4;
public static final int MAX_DATETIMEV2_SCALE = 6;
private static final Logger LOG = LogManager.getLogger(ScalarType.class);

View File

@ -354,7 +354,7 @@ public abstract class Type {
public boolean isDateType() {
return isScalarType(PrimitiveType.DATE) || isScalarType(PrimitiveType.DATETIME)
|| isScalarType(PrimitiveType.DATEV2) || isScalarType(PrimitiveType.DATETIMEV2);
|| isScalarType(PrimitiveType.DATEV2) || isScalarType(PrimitiveType.DATETIMEV2);
}
public boolean isDatetime() {
@ -609,7 +609,7 @@ public abstract class Type {
* Helper for exceedsMaxNestingDepth(). Recursively computes the max nesting depth,
* terminating early if MAX_NESTING_DEPTH is reached. Returns true if this type
* exceeds the MAX_NESTING_DEPTH, false otherwise.
*
* <p>
* Examples of types and their nesting depth:
* INT --> 1
* STRUCT<f1:INT> --> 2
@ -1004,7 +1004,7 @@ public abstract class Type {
* of the assignment-compatible type. For strict compatibility, this can be done
* without any loss of precision. For non-strict compatibility, there may be loss of
* precision, e.g. if converting from BIGINT to FLOAT.
*
* <p>
* We chose not to follow MySQL's type casting behavior as described here:
* http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html
* for the following reasons:
@ -1567,7 +1567,7 @@ public abstract class Type {
// int family type and char family type should cast to char family type
if ((t1ResultType.isFixedPointType() && t2ResultType.isCharFamily())
|| (t2ResultType.isFixedPointType() && t1ResultType.isCharFamily())) {
return t1.isStringType() ? t1 : t2;
return t1.isStringType() ? t1 : t2;
}
if (t1ResultType == PrimitiveType.BIGINT && t2ResultType == PrimitiveType.BIGINT) {