Mihai Budiu created CALCITE-6389:
------------------------------------

             Summary: RexBuilder.removeCastFromLiteral does not preserve 
semantics for decimal literals
                 Key: CALCITE-6389
                 URL: https://issues.apache.org/jira/browse/CALCITE-6389
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.36.0
            Reporter: Mihai Budiu


This is a bug which I keep fixing as part of several pull requests (not yet 
merged), so I have decided to make it into a separate issue.

The code in removeCastFromLiteral is supposed to remove casts which are useless.
However, the code is too aggressive for decimal literals. In particular, this 
fragment:

{code:java}
    if (toType.getSqlTypeName() == SqlTypeName.DECIMAL) {
      final BigDecimal decimalValue = (BigDecimal) value;
      return SqlTypeUtil.isValidDecimalValue(decimalValue, toType);
    }
{code}

There are at least two bugs here:
- this code removes casts from literals even if they represent types such as 
interval
- this code does not check properly that the literal can be represented by the 
type specified by the cast. In particular, the function 
SqlTypeUtil.isValidDecimalValue does not correctly consider the scale of the 
resulting type, and may return 'true' even when a number requires rescaling. 
Removing casts in such a case changes the literal's value. I have submitted a 
fix for this bug as part of [CALCITE-6322], but that PR hasn't been merged yet.




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to