spark-test commented on code in PR #60413:
URL: https://github.com/apache/spark/pull/60413#discussion_r4557909273
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -2654,6 +2654,16 @@ object SQLConf {
.booleanConf
.createWithDefault(Utils.isTesting)
+ val LEGACY_CAST_DECIMAL_TO_INTEGRAL_TYPE_VIA_DOUBLE =
+ buildConf("spark.sql.legacy.castDecimalToIntegralTypeViaDouble")
Review Comment:
**Non-blocking (P2):** This entry has no `.withBindingPolicy(...)`. For a
key without a policy, `Analyzer.bodyFollowsCaller` is false, so
`retainResolutionConfigsForAnalysis` does not copy the caller's explicitly set
value into the conf that a view or SQL-function body is parsed and resolved
under (`View.effectiveSQLConf`, `buildSQLFunctionConf`). `Cast` reads this flag
lazily from `SQLConf.get` the first time it builds its converter or generates
code, and an inline `VALUES` row is evaluated during that parse or resolution,
so such a cast reads the body conf.
Following that code path, with ANSI on, a view created under the default
setting with `CREATE VIEW v AS SELECT * FROM VALUES (CAST(0.999999999999999999
AS INT)) AS t(i)` keeps returning 0 after the session sets
`spark.sql.legacy.castDecimalToIntegralTypeViaDouble=true`, while `SELECT
CAST(0.999999999999999999 AS INT)` in the same session returns 1. Conversely, a
view created while the flag was `true` captures it, and a session that sets it
to `false` cannot override it for such rows.
The `ConfigBindingPolicy` scaladoc rules out `NOT_APPLICABLE` here, because
an early-evaluated inline table becomes a `LocalRelation` of its results, and
`PERSISTED`, because views should not freeze the behavior a bug-fix flag
replaces. That leaves `.withBindingPolicy(ConfigBindingPolicy.SESSION)`.
`SparkConfigBindingPolicySuite` will reject the entry until it declares some
policy, so the only open choice is which one.
**Verification:**
- **Behavior:** A persisted view, a temporary view and a SQL function whose
bodies cast 0.999999999999999999 to INT inside an inline VALUES row, created
with the flag at its default, return 1 after the querying session sets the flag
to true and 0 after it sets it back to false, matching a top-level query of the
same cast in that session.
- **Regression:** A view created while the flag was true returns 0 for such
a row to a session that explicitly sets the flag to false.
- **Inspection:** The new conf declares a binding policy, so the check that
every new config declares one accepts it without an entry in the frozen
exceptions list.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -2654,6 +2654,16 @@ object SQLConf {
.booleanConf
.createWithDefault(Utils.isTesting)
+ val LEGACY_CAST_DECIMAL_TO_INTEGRAL_TYPE_VIA_DOUBLE =
Review Comment:
**Nit (P3):** This legacy decimal-cast flag lands between
`DATA_SOURCE_V2_JOIN_PUSHDOWN` and `DEFAULT_DATA_SOURCE_NAME`, inside the run
of data-source configs, while `SQLConf` keeps the other legacy decimal flags
(`LEGACY_EXPONENT_LITERAL_AS_DECIMAL_ENABLED`,
`LEGACY_ALLOW_NEGATIVE_SCALE_OF_DECIMAL_ENABLED`) together among the legacy
flags. AGENTS.md asks to put a new member with the code it belongs with rather
than split an unrelated section; declaring the entry next to
`LEGACY_ALLOW_NEGATIVE_SCALE_OF_DECIMAL_ENABLED` keeps both groups intact.
**Verification:**
- **Inspection:** The new conf is declared beside the other legacy decimal
flags and no longer splits the data-source configs, and the move changes
nothing else about the entry.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastWithAnsiOnSuite.scala:
##########
@@ -94,6 +94,21 @@ class CastWithAnsiOnSuite extends CastSuiteBase with
QueryErrorsBase {
}
}
+ test("SPARK-61572: cast wide DECIMAL to integral truncates the exact value")
{
+ val wide = Decimal(BigDecimal("0.999999999999999999"), 38, 18)
Review Comment:
**Non-blocking (P2):** These fixtures are positive, far from every bound and
of precision 38 and 30, and SMALLINT and TINYINT are checked only at 0. The
other ANSI cases reach the new branch only with integral bound values that it
accepts, or that `bitLength() < 32` rejects first. Following the code, each of
these regressions of the new branch would pass the suite:
- a DOUBLE-based range check: `2147483647.999999999999` to INT, the PR's own
example, would overflow again (NULL under try_cast);
- dropping or widening `intVal.intValue() <= maxValue && intVal.intValue()
>= minValue`: generated code calls `roundToByte(true)` and narrows with
`_.toByte`, so a BigDecimal-backed 128 cast to TINYINT would return -128, while
the interpreted path still fails at its byte check;
- flooring instead of truncating toward zero: `-0.999999999999999999` would
give -1 instead of 0;
- selecting the branch by a declared precision above 18, as the docs
describe it: both fixtures would still take it, while a `DECIMAL(18,18)`
literal would not.
Cases that would catch these: `2147483647.999999999999` and
`-2147483648.999999999999` to INT (`Int.MaxValue` and `Int.MinValue`,
`CAST_OVERFLOW` under the legacy conf), `32767.999999999999999999` to SMALLINT
and `127.9999999999999999999` to TINYINT (32767 and 127), BigDecimal-backed 128
and -129 to TINYINT and 32768 to SMALLINT through
`checkExceptionInExpression[ArithmeticException](..., "overflow")`,
`-0.999999999999999999` to INT (0, and -1 under the legacy conf), and a
`DECIMAL(18,18)` value such as `Decimal(BigDecimal("0.999999999999999999"))`.
`TryCastSuite` inherits the test, so the overflow cases become NULL checks
there.
See **Shared repair plan 2** in the review body.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -2654,6 +2654,16 @@ object SQLConf {
.booleanConf
.createWithDefault(Utils.isTesting)
+ val LEGACY_CAST_DECIMAL_TO_INTEGRAL_TYPE_VIA_DOUBLE =
+ buildConf("spark.sql.legacy.castDecimalToIntegralTypeViaDouble")
+ .internal()
+ .doc("When true, an ANSI cast to TINYINT, SMALLINT or INT of a DECIMAL
value stored as a " +
+ "BigDecimal (precision > 18) converts it to a Double first, so a value
such as " +
+ "0.999999999999999999 becomes 1. When false, the exact value is
truncated toward zero.")
Review Comment:
**Nit (P3):** The flag's consumers end in `Decimal.roundToNumeric`, which
picks the DOUBLE or the exact branch whenever `decimalVal` is non-null,
whatever the declared precision. Every decimal literal is BigDecimal-backed
(`Decimal.set(BigDecimal)` keeps the `BigDecimal`), so the example
`0.999999999999999999`, written as a literal, is a `DECIMAL(18,18)` value whose
ANSI cast to INT this flag switches between 1 and 0, and struct-field decimals
read through `ArrowColumnVector` are BigDecimal-backed at `DECIMAL(18,9)`.
Conversely, `Decimal.setOrNull` keeps unscaled values below 10^18 long-backed
at any declared precision, and those are exact under either setting. Something
like:
```suggestion
.doc("When true, an ANSI cast to TINYINT, SMALLINT or INT of a DECIMAL
value held as a " +
"BigDecimal, such as a value of a DECIMAL column of precision above
18 or a decimal " +
"literal of any precision, converts it to a Double first, so
0.999999999999999999 " +
"becomes 1. When false, the exact value is truncated toward zero.")
```
See **Shared repair plan 1** in the review body.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastWithAnsiOnSuite.scala:
##########
@@ -94,6 +94,21 @@ class CastWithAnsiOnSuite extends CastSuiteBase with
QueryErrorsBase {
}
}
+ test("SPARK-61572: cast wide DECIMAL to integral truncates the exact value")
{
+ val wide = Decimal(BigDecimal("0.999999999999999999"), 38, 18)
+ val qty = Decimal(BigDecimal("999999.999999999999"), 30, 12)
+ // Default: the exact value is truncated toward zero.
+ checkEvaluation(cast(wide, IntegerType), 0)
+ checkEvaluation(cast(wide, ShortType), 0.toShort)
+ checkEvaluation(cast(wide, ByteType), 0.toByte)
+ checkEvaluation(cast(qty, IntegerType), 999999)
+ // Legacy: the value goes through a Double first, so 0.999999999999999999
rounds to 1.
+ withSQLConf(SQLConf.LEGACY_CAST_DECIMAL_TO_INTEGRAL_TYPE_VIA_DOUBLE.key ->
"true") {
Review Comment:
**Nit (P3):** Only `IntegerType` is asserted inside this block, but SMALLINT
and TINYINT reach the flag through separate sites: the interpreted
`castToShort` and `castToByte` (through `exactNumericToInt`) and the generated
`roundToShort(false)` and `roundToByte(false)` calls. If either ignored the
flag, `cast(wide, ShortType)` and `cast(wide, ByteType)` would return 0 instead
of the old 1 under the legacy conf without any test failing. Adding
`checkEvaluation(cast(wide, ShortType), 1.toShort)` and
`checkEvaluation(cast(wide, ByteType), 1.toByte)` here covers both sites, since
`checkEvaluation` runs interpreted and generated code.
See **Shared repair plan 2** in the review body.
##########
docs/sql-migration-guide.md:
##########
@@ -25,6 +25,7 @@ license: |
## Upgrading from Spark SQL 4.4 to 5.0
- Since Spark 5.0, the Spark Thrift Server refuses to start when
`hive.server2.enable.doAs` is `true` and `hive.server2.authentication` verifies
user identities (any recognized type other than `NONE`/`NOSASL`). It
impersonates the connecting user for Hive metastore calls and driver-side file
system access, but executor-side data access still runs as the server's own
service identity, so storage-level permissions are checked against the service
principal rather than the connecting user (SPARK-5159). Spark 4.4 logs a
warning for the same configuration. To restore the previous behavior, set
`spark.sql.hive.thriftServer.allowIneffectiveDoAs` to `true`. Setting
`hive.server2.enable.doAs` to `false` also starts the server, but note that it
stops impersonating metastore calls as well, so it is not a no-op.
+- Since Spark 5.0, an ANSI cast to `TINYINT`, `SMALLINT` or `INT` of a
`DECIMAL` value with precision greater than 18 truncates the exact value toward
zero, instead of first converting it to a `DOUBLE`. For example
`CAST(0.999999999999999999BD AS INT)` returns `0` rather than `1`, and a value
just inside the target range (such as `2147483647.999999999999BD` to `INT`) no
longer overflows. To restore the previous behavior, set
`spark.sql.legacy.castDecimalToIntegralTypeViaDouble` to `true`.
Review Comment:
**Nit (P3):** The changed branch is chosen by how the `Decimal` holds its
value, not by its declared precision. Decimal literals are BigDecimal-backed at
any precision, so this entry's own example `0.999999999999999999BD` is a
`DECIMAL(18,18)` value, and `CAST(0.99999999999999999 AS INT)`
(`DECIMAL(17,17)`, nearest DOUBLE 1.0) and `CAST(2147483647.9999999 AS INT)`
(`DECIMAL(17,7)`, nearest DOUBLE 2147483648.0) change as well; decimal
arithmetic results and decimals from VARIANT or Arrow data can be
BigDecimal-backed at any precision too. The entry also names only the ANSI
cast, but `try_cast` and `INSERT` under the default ANSI store assignment
policy use the same converters whatever `spark.sql.ansi.enabled` is:
`try_cast(2147483647.999999999999BD AS INT)` changes from NULL to 2147483647,
and inserting a `DECIMAL(38,18)` value 0.999999999999999999 into an INT column
stores 0 instead of 1, with ANSI mode off as well. Something like:
```suggestion
- Since Spark 5.0, an ANSI cast to `TINYINT`, `SMALLINT` or `INT` of a
`DECIMAL` value that Spark holds as a `BigDecimal`, such as a decimal literal
of any precision or a value of a `DECIMAL` column with precision greater than
18, truncates the exact value toward zero, instead of first converting it to a
`DOUBLE`. This also applies, whatever `spark.sql.ansi.enabled` is, to
`try_cast`, which now returns the truncated value where it returned `NULL`, and
to `INSERT` into a `TINYINT`, `SMALLINT` or `INT` column under the default
`ANSI` store assignment policy. For example `CAST(0.999999999999999999BD AS
INT)` returns `0` rather than `1`, and a value just inside the target range
(such as `2147483647.999999999999BD` to `INT`) no longer overflows. To restore
the previous behavior, set
`spark.sql.legacy.castDecimalToIntegralTypeViaDouble` to `true`.
```
See **Shared repair plan 1** in the review body.
##########
sql/api/src/main/scala/org/apache/spark/sql/types/Decimal.scala:
##########
@@ -305,6 +313,19 @@ final class Decimal extends Ordered[Decimal] with
Serializable {
DecimalType(this.precision, this.scale),
integralType)
}
+ } else if (exactTruncation) {
Review Comment:
**Nit (P3):** This branch makes two comments in `NativeColumnVectorSuite`,
which the PR does not touch, stale: the `checkDecimal` scaladoc says
`ArrowColumnVector` 'holds BigDecimals, which Decimal rounds to bytes, shorts
and ints through doubles', and the struct-field test says 'Decimal rounds those
to integers through doubles until SPARK-60843, so that the nested decimal then
rounds to 123456790'. The no-argument `roundToByte()`, `roundToShort()` and
`roundToInt()` that the suite calls now always pass `exactTruncation = true`,
so the nested `Decimal(expanded, 123456789.999999999, 18, 9)` rounds to
123456789, like the top-level column, whatever the legacy conf is; only an ANSI
cast under `spark.sql.legacy.castDecimalToIntegralTypeViaDouble` still converts
through a double. Both comments could say so and refer to SPARK-61572 rather
than a pending SPARK-60843 (linking the two tickets if they duplicate each
other), and the test could assert `nested.roundToInt() === 123456789` now th
at it matches.
**Verification:**
- **Inspection:** The native column vector tests' comments say that the
rounding methods they call truncate BigDecimal-backed decimals exactly, mention
the legacy conf, if at all, only as affecting ANSI casts, and no longer cite
SPARK-60843 as pending.
- **Behavior:** The struct-field test asserts that the BigDecimal-backed
nested decimal 123456789.999999999 rounds to 123456789, the result of the
long-backed top-level column.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]