NobiGo commented on code in PR #4093:
URL: https://github.com/apache/calcite/pull/4093#discussion_r1887760820
##########
core/src/main/java/org/apache/calcite/sql/validate/implicit/AbstractTypeCoercion.java:
##########
@@ -612,17 +612,14 @@ private RelDataType getTightestCommonTypeOrThrow(
}
if (SqlTypeUtil.isExactNumeric(type1) &&
SqlTypeUtil.isExactNumeric(type2)) {
- if (SqlTypeUtil.isDecimal(type1)) {
- // Use max precision
+ if (SqlTypeUtil.isDecimal(type1) || SqlTypeUtil.isDecimal(type2)) {
+ // Precision used must be large enough to fit either of the types
+ int maxScale = Math.max(type1.getScale(), type2.getScale());
RelDataType result =
- factory.createSqlType(type1.getSqlTypeName(),
- Math.max(type1.getPrecision(), type2.getPrecision()),
type1.getScale());
- return factory.createTypeWithNullability(result, type1.isNullable() ||
type2.isNullable());
- } else if (SqlTypeUtil.isDecimal(type2)) {
- // Use max precision
- RelDataType result =
- factory.createSqlType(type2.getSqlTypeName(),
- Math.max(type1.getPrecision(), type2.getPrecision()),
type2.getScale());
+ factory.createSqlType(SqlTypeName.DECIMAL,
+ Math.max(type1.getPrecision() - type1.getScale(),
+ type2.getPrecision() - type2.getScale()) + maxScale,
+ maxScale);
return factory.createTypeWithNullability(result, type1.isNullable() ||
type2.isNullable());
}
if (type1.getPrecision() > type2.getPrecision()) {
Review Comment:
Can this handle types without precision and proportion? For example:
TINYINT、SMALLINT、INTEGER and BIGINT? Or the default behavior is right.
If type1 is TINYINT type2 is SMALLINT, then the common type becomes
SMALLINT. This is right.
But when type1 is SMALLINT and type2 is TINYINT. then the common type
becomes TINYINT. This should be unreasonable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]