[
https://issues.apache.org/jira/browse/SPARK-62122?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Dustin Smith updated SPARK-62122:
---------------------------------
Description:
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.
The same lookup fails for a scale above 18, with no legacy setting: a
{{DECIMAL(5,2)}} value cast to {{DECIMAL(38,19)}} and then to an integer type.
{{try_cast}} returns {{NULL}} there.
was:
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.
Summary: Fix integral conversions of decimals with a scale below 0 or
above 18 (was: Converting a compact decimal with a negative scale to an
integral type fails with ArrayIndexOutOfBoundsException)
> Fix integral conversions of decimals with a scale below 0 or above 18
> ---------------------------------------------------------------------
>
> Key: SPARK-62122
> URL: https://issues.apache.org/jira/browse/SPARK-62122
> Project: Spark
> Issue Type: Bug
> Components: SQL
> Affects Versions: 4.3.0, 4.4.0
> Reporter: Dustin Smith
> Priority: Major
> Labels: pull-request-available
>
> 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.
> The same lookup fails for a scale above 18, with no legacy setting: a
> {{DECIMAL(5,2)}} value cast to {{DECIMAL(38,19)}} and then to an integer
> type. {{try_cast}} returns {{NULL}} there.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]