Skip to content

Native decimal remainder returns NULL when an operand does not fit the result type #2746

Open

Description

@zoddtheimmortal

Describe the bug

Native decimal % returns NULL whenever an operand doesn't fit the remainder's result type. Spark returns the correct remainder.

Spark gives decimal(p1,s1) % decimal(p2,s2) the result type decimal(min(p1-s1, p2-s2) + max(s1,s2), max(s1,s2)). This is often narrower than one of the operands. For example, decimal(15,0) % int coerces the int to decimal(10,0), so the result type is decimal(10,0). Spark computes the remainder on the original values and only checks the result against that type. This is safe because the remainder is always smaller than the divisor.

When the divisor is a column, Auron casts both operands to the result type before the native modulo. An operand that doesn't fit, such as 12345678901 in decimal(10,0), becomes NULL, so the whole remainder is NULL. Decimal % is not gated by spark.auron.decimal.arithOp.enabled, so this happens with the default configuration.

To Reproduce

spark.sql("create table t1 (a decimal(15,0), b int, x decimal(3,0), y decimal(15,0)) using parquet")
spark.sql("insert into t1 values (12345678901, 7, 7, 10000000000)")
spark.sql("select a % b, x % y from t1").show()
Query Spark Auron
a % b 3 NULL
x % y 7 NULL

Reproduced on Spark 3.5 and 4.2.

Expected behavior

Native decimal % returns the same values as Spark. The operands should keep their own precision, or be cast to a common type wide enough for both, and only the result should be checked against the result type.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions