[fix](planner)the decimal type of's precision and scale setope ration codn is wrong (#32787)

This commit is contained in:
starocean999
2024-03-26 20:04:04 +08:00
committed by yiguolei
parent fdf6b8fe8d
commit 2d398dfe1f
2 changed files with 23 additions and 0 deletions

View File

@ -2327,6 +2327,9 @@ public class Analyzer {
lastCompatibleExpr, exprLists.get(j).get(i));
lastCompatibleExpr = exprLists.get(j).get(i);
}
if (compatibleType.isDecimalV3()) {
compatibleType = adjustDecimalV3PrecisionAndScale((ScalarType) compatibleType);
}
// Now that we've found a compatible type, add implicit casts if necessary.
for (int j = 0; j < exprLists.size(); ++j) {
if (!exprLists.get(j).get(i).getType().equals(compatibleType)) {
@ -2337,6 +2340,21 @@ public class Analyzer {
}
}
private ScalarType adjustDecimalV3PrecisionAndScale(ScalarType decimalV3Type) {
ScalarType resultType = decimalV3Type;
int oldPrecision = decimalV3Type.getPrecision();
int oldScale = decimalV3Type.getDecimalDigits();
int integerPart = oldPrecision - oldScale;
int maxPrecision =
SessionVariable.getEnableDecimal256() ? ScalarType.MAX_DECIMAL256_PRECISION
: ScalarType.MAX_DECIMAL128_PRECISION;
if (oldPrecision > maxPrecision) {
int newScale = maxPrecision - integerPart;
resultType = ScalarType.createDecimalType(maxPrecision, newScale < 0 ? 0 : newScale);
}
return resultType;
}
public long getConnectId() {
return globalState.context.getConnectionId();
}

View File

@ -113,4 +113,9 @@ suite("test_cast") {
sql """select k0 from table_decimal38_4 union all select k0 from table_decimal27_9;"""
contains """AS DECIMALV3(38, 4)"""
}
sql """set enable_nereids_planner=false;"""
explain {
sql """select k0 from table_decimal38_4 union all select k0 from table_decimal27_9;"""
contains """AS DECIMALV3(38, 4)"""
}
}