[
https://issues.apache.org/jira/browse/CALCITE-7968?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Alessandro Solimando updated CALCITE-7968:
------------------------------------------
Description:
_SqlTypeUtil#integerBound_ computes the bounds of a DECIMAL with
_SqlTypeName#getLimit_
([here|https://github.com/apache/calcite/blob/a5557abef8aa8a83fe7241e4f3c11c95c30d2563/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java#L658-L664]):
{code:java}
final int precision = isDecimal ? type.getPrecision() : -1;
final int scale = isDecimal ? type.getScale() : -1;
final Object limit =
typeName.getLimit(upper, SqlTypeName.Limit.OVERFLOW,
false,
precision,
scale);
{code}
For DECIMAL(p, 0), _getLimit_ computes -(10^p - 1) and 10^p - 1, and then
clamps them to the range of BIGINT
([here|https://github.com/apache/calcite/blob/a5557abef8aa8a83fe7241e4f3c11c95c30d2563/core/src/main/java/org/apache/calcite/sql/type/SqlTypeName.java#L606-L612]):
{code:java}
case OVERFLOW:
final BigDecimal other =
(BigDecimal) BIGINT.getLimit(sign, limit, beyond, -1, -1);
if (other != null && decimal.compareTo(other) == (sign ? 1 : -1)) {
decimal = other;
}
break;
{code}
For p >= 19, _integerBound_ therefore returns the bounds of BIGINT, although
DECIMAL(19, 0) holds values up to 9999999999999999999 and BIGINT only up to
9223372036854775807. _RexUtil#isLosslessCast_ decides a DECIMAL to integer cast
by comparing these bounds
([here|https://github.com/apache/calcite/blob/a5557abef8aa8a83fe7241e4f3c11c95c30d2563/core/src/main/java/org/apache/calcite/rex/RexUtil.java#L1988-L1993]),
so it returns true for a cast from DECIMAL(19, 0) to BIGINT, and for any
DECIMAL(p, 0) with p >= 19 in a type system that allows it.
The javadoc of _isLosslessCast_ says that the method does not return false
positives. Its callers, such as
_TableModify#expectedInputRowTypeForAssignment_, _TypeCoercionImpl_ and
_RexSimplify_, can drop a cast that overflows. In our case, an INSERT of a
DECIMAL(19, 0) value that does not fit into a BIGINT column stored NULL instead
of failing.
Reproducer (for {_}RexLosslessCastTest{_}):
{code:java}
@Test void testLosslessCastDecimalToBigint() {
final RelDataType bigIntType = typeFactory.createSqlType(SqlTypeName.BIGINT);
final RelDataType dec_18_0 = typeFactory.createSqlType(SqlTypeName.DECIMAL,
18, 0);
final RelDataType dec_19_0 = typeFactory.createSqlType(SqlTypeName.DECIMAL,
19, 0);
assertThat(RexUtil.isLosslessCast(dec_18_0, bigIntType), is(true));
assertThat(RexUtil.isLosslessCast(dec_19_0, bigIntType), is(false));
}
{code}
The test currently fails as follows:
{noformat}
java.lang.AssertionError:
Expected: is <false>
but: was <true>
{noformat}
Proposed fix: compute the bounds of DECIMAL(p, 0) in _integerBound_ as -(10^p -
1) and 10^p - 1, without the clamp to the range of BIGINT.
was:
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:
!image-2026-11-25-18-38-08-071.png!
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:
{code:java}
switch (limit) {
case OVERFLOW:
final BigDecimal other =
(BigDecimal) BIGINT.getLimit(sign, limit, beyond, -1, -1);
if (other != null && decimal.compareTo(other) == (sign ? 1 : -1)) {
decimal = other;
}
break;
default:
break;
}
{code}
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.
> RexUtil.isLosslessCast returns true for a cast from DECIMAL(19, 0) to BIGINT
> ----------------------------------------------------------------------------
>
> Key: CALCITE-7968
> URL: https://issues.apache.org/jira/browse/CALCITE-7968
> Project: Calcite
> Issue Type: Bug
> Reporter: Weihua Zhang
> Priority: Major
> Attachments: image-2026-11-25-18-38-08-071.png
>
>
> _SqlTypeUtil#integerBound_ computes the bounds of a DECIMAL with
> _SqlTypeName#getLimit_
> ([here|https://github.com/apache/calcite/blob/a5557abef8aa8a83fe7241e4f3c11c95c30d2563/core/src/main/java/org/apache/calcite/sql/type/SqlTypeUtil.java#L658-L664]):
> {code:java}
> final int precision = isDecimal ? type.getPrecision() : -1;
> final int scale = isDecimal ? type.getScale() : -1;
> final Object limit =
> typeName.getLimit(upper, SqlTypeName.Limit.OVERFLOW,
> false,
> precision,
> scale);
> {code}
> For DECIMAL(p, 0), _getLimit_ computes -(10^p - 1) and 10^p - 1, and then
> clamps them to the range of BIGINT
> ([here|https://github.com/apache/calcite/blob/a5557abef8aa8a83fe7241e4f3c11c95c30d2563/core/src/main/java/org/apache/calcite/sql/type/SqlTypeName.java#L606-L612]):
> {code:java}
> case OVERFLOW:
> final BigDecimal other =
> (BigDecimal) BIGINT.getLimit(sign, limit, beyond, -1, -1);
> if (other != null && decimal.compareTo(other) == (sign ? 1 : -1)) {
> decimal = other;
> }
> break;
> {code}
> For p >= 19, _integerBound_ therefore returns the bounds of BIGINT, although
> DECIMAL(19, 0) holds values up to 9999999999999999999 and BIGINT only up to
> 9223372036854775807. _RexUtil#isLosslessCast_ decides a DECIMAL to integer
> cast by comparing these bounds
> ([here|https://github.com/apache/calcite/blob/a5557abef8aa8a83fe7241e4f3c11c95c30d2563/core/src/main/java/org/apache/calcite/rex/RexUtil.java#L1988-L1993]),
> so it returns true for a cast from DECIMAL(19, 0) to BIGINT, and for any
> DECIMAL(p, 0) with p >= 19 in a type system that allows it.
> The javadoc of _isLosslessCast_ says that the method does not return false
> positives. Its callers, such as
> _TableModify#expectedInputRowTypeForAssignment_, _TypeCoercionImpl_ and
> _RexSimplify_, can drop a cast that overflows. In our case, an INSERT of a
> DECIMAL(19, 0) value that does not fit into a BIGINT column stored NULL
> instead of failing.
> Reproducer (for {_}RexLosslessCastTest{_}):
> {code:java}
> @Test void testLosslessCastDecimalToBigint() {
> final RelDataType bigIntType =
> typeFactory.createSqlType(SqlTypeName.BIGINT);
> final RelDataType dec_18_0 = typeFactory.createSqlType(SqlTypeName.DECIMAL,
> 18, 0);
> final RelDataType dec_19_0 = typeFactory.createSqlType(SqlTypeName.DECIMAL,
> 19, 0);
> assertThat(RexUtil.isLosslessCast(dec_18_0, bigIntType), is(true));
> assertThat(RexUtil.isLosslessCast(dec_19_0, bigIntType), is(false));
> }
> {code}
> The test currently fails as follows:
> {noformat}
> java.lang.AssertionError:
> Expected: is <false>
> but: was <true>
> {noformat}
> Proposed fix: compute the bounds of DECIMAL(p, 0) in _integerBound_ as -(10^p
> - 1) and 10^p - 1, without the clamp to the range of BIGINT.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)