Weihua Zhang created CALCITE-7968:
-------------------------------------
Summary: SqlTypeUtil.integerBound clamps the bounds of DECIMAL(p,
0) with p >= 19 to the range of BIGINT, so RexUtil.isLosslessCast(DECIMAL(19,
0), BIGINT) returns true
Key: CALCITE-7968
URL: https://issues.apache.org/jira/browse/CALCITE-7968
Project: Calcite
Issue Type: Bug
Reporter: Weihua Zhang
RexUtil.isLosslessCast(DECIMAL(19, 0), BIGINT) returns true, but the cast is
not lossless: DECIMAL(19, 0) holds 9999999999999999999, which is greater than
the maximum BIGINT value, 9223372036854775807. The javadoc of isLosslessCast
says that the method "does not return false positives".
isLosslessCast decides a DECIMAL to integer cast with
SqlTypeUtil.integerRangeContains, which compares the bounds that
SqlTypeUtil.integerBound returns. With the default type system:
┌────────────────┬─────────────────────────────────────────────┬─────────────────────────────────────────────┐
│ Type │ integerBound (actual) │
Expected │
├────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────────────┤
│ DECIMAL(18, 0) │ [-999999999999999999, 999999999999999999] │ same
│
├────────────────┼─────────────────────────────────────────────┼─────────────────────────────────────────────┤
│ DECIMAL(19, 0) │ [-9223372036854775808, 9223372036854775807] │
[-9999999999999999999, 9999999999999999999] │
└────────────────┴─────────────────────────────────────────────┴─────────────────────────────────────────────┘
So integerRangeContains(BIGINT, DECIMAL(19, 0)) returns true.
integerBound (added in CALCITE-7174) gets the bounds of a DECIMAL from
SqlTypeName.DECIMAL.getLimit(upper, Limit.OVERFLOW, false, precision, scale).
That method computes ±(10^p - 1), but then clamps the result to the range of
BIGINT:
case OVERFLOW:
final BigDecimal other =
(BigDecimal) BIGINT.getLimit(sign, limit, beyond, -1, -1);
if (other != null && decimal.compareTo(other) == (sign ? 1 : -1)) {
decimal = other;
}
integerBound treats the clamped value as the range of the DECIMAL type. The
same probably happens for any precision greater than 19 in a type system that
allows it.
Reproducer (for SqlTypeUtilTest and RexLosslessCastTest):
final RelDataType bigint = f.typeFactory.createSqlType(SqlTypeName.BIGINT);
final RelDataType dec19 =
f.typeFactory.createSqlType(SqlTypeName.DECIMAL, 19, 0);
// Both currently return true
assertThat(SqlTypeUtil.integerRangeContains(bigint, dec19), is(false));
assertThat(RexUtil.isLosslessCast(dec19, bigint), is(false));
The existing tests only check DECIMAL(9, 0) and DECIMAL(10, 0) against INTEGER,
which are below the precision where the clamp applies.
Impact:
Code that calls isLosslessCast may treat a cast from DECIMAL(19, 0) to BIGINT
as safe and drop a check or a cast that it needs. For example,
TableModify.expectedInputRowTypeForAssignment and several simplifications in
RexSimplify call it. We found the bug because an INSERT of a DECIMAL(19, 0)
value that does not fit into a BIGINT column stored NULL instead of failing.
That part of the path is in our own planner, so it is only an example of the
consequences.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)