Dustin Smith created SPARK-62122:
------------------------------------
Summary: Fail with ArrayIndexOutOfBoundsException when converting
a compact decimal with a negative scale to an integral type
Key: SPARK-62122
URL: https://issues.apache.org/jira/browse/SPARK-62122
Project: Spark
Issue Type: Bug
Components: SQL
Affects Versions: 5.0.0
Reporter: Dustin Smith
With {{spark.sql.legacy.allowNegativeScaleOfDecimal=true}}, a {{Decimal}} can
hold a negative scale in its compact (long-backed) form: {{setOrNull(unscaled:
Long, precision, scale)}} and {{changePrecision}} both accept it, for example
when a value is cast to {{DECIMAL(5, -2)}}.
Converting such a value to an integral type then fails with
{{ArrayIndexOutOfBoundsException}} instead of returning a result.
{{Decimal.actualLongVal}} computes {{longVal / POW_10(_scale)}}, and {{POW_10}}
is an array indexed by the scale, so a negative scale reads before its start.
{{actualLongVal}} is used by {{toLong}}, {{toInt}}, {{roundToLong}},
{{roundToInt}}, {{roundToShort}}, {{roundToByte}} and the
{{BigInt}}/{{BigInteger}} conversions, so both ANSI and non-ANSI casts of such
a value to {{TINYINT}}, {{SMALLINT}}, {{INT}} and {{BIGINT}} are affected.
A BigDecimal-backed value with a negative scale does not hit this, since it
goes through {{BigDecimal.toBigInteger}}.
Proposed fix: for a negative scale, multiply the unscaled value by
{{10^-scale}} with an overflow check instead of dividing, or fall back to the
BigDecimal path, and add tests for compact values with negative scales.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]