Casting a string to decimal fails or is slow when the exponent is far outside the target scale

XMLWordPrintableJSON

      `Decimal.changePrecision` rescales its BigDecimal value with `setScale` without bounding the scale change. Its cost is proportional to the change, and it throws once the change exceeds the Int range:

      1. `CAST('1e-2147483647' AS DECIMAL(10,2))` throws `java.lang.ArithmeticException: BigInteger would overflow supported range` under LEGACY, ANSI and TRY, instead of returning 0.00. `'1e-100000000'` returns 0.00 but takes ~40s, since it materializes 10^100000000.
      2. With `spark.sql.legacy.allowNegativeScaleOfDecimal=true`, `fromString` intentionally skips the integral-digit fast-fail, so `CAST('1e2147483647' AS DECIMAL(10,2))` throws `java.lang.ArithmeticException: Underflow` instead of returning NULL (non-ANSI/TRY) or `NUMERIC_VALUE_OUT_OF_RANGE` (ANSI). `'1e100000000'` is also very slow. When `changePrecision` does report the overflow under ANSI, the error message renders the value with `toPlainString`, which cannot fit `1E+2147483647` in a String.

            Assignee:
            L. C. Hsieh
            Reporter:
            L. C. Hsieh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 1h 50m
                1h 50m