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

AssertionError on RexBuilder when creating BigDecimal RexLiteral

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 1.17.0
    • 1.18.0
    • None
    • 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

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

              Dates

                Created:
                Updated:
                Resolved: