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

AssertionError on RexBuilder when creating BigDecimal RexLiteral

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 1.17.0
    • Fix Version/s: 1.18.0
    • Component/s: None
    • Labels:
      None

      Description

      The method RexBuilder#makeExactLiteral(java.math.BigDecimal) throws an AssertionError if the BigDecimal parameter has an unscaled value that overflows long:

      public RexLiteral makeExactLiteral(BigDecimal bd) {
          ...
          long l = bd.unscaledValue().longValue(); // narrowing conversion BigInteter to long
          ...
          assert BigDecimal.valueOf(l, scale).equals(bd); // assert fails if l overflew long
          ...
      

      Moreover, with the current implementation, it can be possible to have this AssertionError, even in the cases when the variable l would not be used at all (decimal number)

      ...
      assert BigDecimal.valueOf(l, scale).equals(bd); // assert can fail, even if scale == 0 (l would not be needed)
      if (scale == 0) {
        if ((l >= Integer.MIN_VALUE) && (l <= Integer.MAX_VALUE))
          relType = typeFactory.createSqlType(SqlTypeName.INTEGER);
        else
          relType = typeFactory.createSqlType(SqlTypeName.BIGINT);
      } else {
        int precision = bd.unscaledValue().abs().toString().length();
        if (precision > scale)
          relType = typeFactory.createSqlType(SqlTypeName.DECIMAL, precision, scale);
        else
          relType = typeFactory.createSqlType(SqlTypeName.DECIMAL, scale + 1, scale);
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                julianhyde Julian Hyde
                Reporter:
                rubenql Ruben Q L
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: