Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-9046 Decimal type support improvement and bug fix
  3. SPARK-8800

Spark SQL Decimal Division operation loss of precision/scale when type is defined as DecimalType.Unlimited

    XMLWordPrintableJSON

Details

    • Sub-task
    • Status: Resolved
    • Blocker
    • Resolution: Invalid
    • None
    • 1.5.0
    • SQL
    • None
    • Spark 1.5 release

    Description

      According to specification defined in Java doc over BigDecimal :

      http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html

      When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.) As a corollary of computing the exact result, the rounding mode setting of a MathContext object with a precision setting of 0 is not used and thus irrelevant. In the case of divide, the exact quotient could have an infinitely long decimal expansion; for example, 1 divided by 3. If the quotient has a nonterminating decimal expansion and the operation is specified to return an exact result, an ArithmeticException is thrown. Otherwise, the exact result of the division is returned, as done for other operations.

      when Decimal data is defined as DecimalType.Unlimited in Spark SQL, the exact result of the division should be returned or truncated to precision = 38 which is in align with what Hive supports. the current behavior is as shown following, which cause we lose the accuracy of Decimal division operation.

      scala> val aa = Decimal(2) / Decimal(3);
      aa: org.apache.spark.sql.types.Decimal = 1

      here is another example where we should return 0.125 instead of 0

      scala> val aa = Decimal(1) /Decimal(8)
      aa: org.apache.spark.sql.types.Decimal = 0

      Attachments

        Issue Links

          Activity

            People

              viirya L. C. Hsieh
              jihongma Jihong Ma
              Yin Huai Yin Huai
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: