Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,9 @@ public void castToSetOpsCompatibleTypes(List<List<Expr>> exprLists)
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)) {
Expand All @@ -2337,6 +2340,21 @@ public void castToSetOpsCompatibleTypes(List<List<Expr>> exprLists)
}
}

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();
}
Expand Down
5 changes: 5 additions & 0 deletions regression-test/suites/nereids_p0/datatype/test_cast.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)"""
}
}