Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-6389

RexBuilder.removeCastFromLiteral does not preserve semantics for some types of literals

    XMLWordPrintableJSON

Details

    Description

      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:

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

      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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            mbudiu Mihai Budiu
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: