MaxGekk commented on code in PR #59954:
URL: https://github.com/apache/spark/pull/59954#discussion_r4641559824
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -629,14 +650,29 @@ case class Multiply(
// scalastyle:on
override def resultDecimalType(p1: Int, s1: Int, p2: Int, s2: Int):
DecimalType = {
val resultScale = s1 + s2
- val resultPrecision = p1 + p2 + 1
+ val resultPrecision = unboundedResultPrecision(p1, p2)
if (allowPrecisionLoss) {
DecimalType.adjustPrecisionScale(resultPrecision, resultScale)
} else {
DecimalType.bounded(resultPrecision, resultScale)
}
}
+ // The result precision before it is bounded by the maximum precision. A
result type of this
+ // precision holds every exact product.
+ private def unboundedResultPrecision(p1: Int, p2: Int): Int = p1 + p2 + 1
+
+ // See Add.canBeRegrouped.
+ private def canBeRegrouped: Boolean = evalMode match {
+ case EvalMode.TRY => false
+ case EvalMode.LEGACY if childrenResolved => (left.dataType,
right.dataType) match {
Review Comment:
nit: when the children of a LEGACY step are not resolved, this falls through
to `case _ => true`, so the step counts as safe to regroup.
`Multiply.canonicalized` has no `resolved` guard (`Add` has one), so an
unresolved `Multiply` chain canonicalizes flattened and changes form once its
types are known. Would `false` be the safer default for a LEGACY step whose
children are not resolved? The same pattern is at L464.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -442,14 +442,33 @@ case class Add(
// scalastyle:on
override def resultDecimalType(p1: Int, s1: Int, p2: Int, s2: Int):
DecimalType = {
val resultScale = max(s1, s2)
- val resultPrecision = max(p1 - s1, p2 - s2) + resultScale + 1
+ val resultPrecision = unboundedResultPrecision(p1, s1, p2, s2)
if (allowPrecisionLoss) {
DecimalType.adjustPrecisionScale(resultPrecision, resultScale)
} else {
DecimalType.bounded(resultPrecision, resultScale)
}
}
+ // The result precision before it is bounded by the maximum precision. A
result type of this
+ // precision holds every exact sum.
+ private def unboundedResultPrecision(p1: Int, s1: Int, p2: Int, s2: Int):
Int =
+ max(p1 - s1, p2 - s2) + max(s1, s2) + 1
+
+ // Whether canonicalization can regroup this step with an enclosing Add of
the same evaluation
+ // context. A TRY step returns NULL exactly when its own operation
overflows. So does a non-ANSI
+ // decimal step whose result type is bounded by the maximum precision, which
can also round under
+ // precision loss. Regrouping such a step can change the result.
+ private def canBeRegrouped: Boolean = evalMode match {
Review Comment:
`canBeRegrouped` returns true for every ANSI step, but with
`spark.sql.decimalOperations.allowPrecisionLoss=true` an ANSI decimal step
whose precision exceeds 38 reduces its scale and rounds without raising, which
is the rounding the comment gives as the reason not to regroup LEGACY steps.
For example, with ANSI on and `a`, `b`, `c` of `DECIMAL(38, 10)`, `a = b =
0.0000000005` and `c = 0`, `a + b` is `DECIMAL(38, 9)`, so `(a + b) + c` is
`0.000000001` while `(a + c) + b` is `0.000000002`. Both canonicalize to the
same tree, so `SELECT (a + c) + b ... GROUP BY (a + b) + c` is accepted. This
predates the PR, but could the same precision check apply to ANSI decimal steps
when precision loss is allowed? `Multiply.canBeRegrouped` (L666) has the same
gap.
##########
docs/sql-migration-guide.md:
##########
@@ -26,6 +26,8 @@ license: |
- 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, Spark requires Java 21 or later. Users who ran Spark 4.x on
Java 17 can see two differences that come from Java. The datetime pattern
letter `F` is the aligned week of month instead of the aligned day of week in
month (JDK-8282081). For example, `date_format(date '2019-12-31', 'F')` returns
`5` instead of `3`. Converting a `FLOAT` or `DOUBLE` value to a string gives
the shortest decimal that represents the value (JDK-4511638). For example,
`CAST(CAST(4567890123456789 AS FLOAT) AS STRING)` returns `4.56789E15` instead
of `4.5678899E15`. A cast from `FLOAT` or `DOUBLE` to `DECIMAL` goes through
this string, so its result can change as well. For example,
`CAST(1.0751133388005187E17D AS DECIMAL(38, 0))` returns `107511333880051870`
instead of `107511333880051872`.
+- Since Spark 5.0, `try_add` and `try_multiply` combined with regular `+` or
`*` follow each operator's own overflow behavior, and a chain of `try_add` or
`try_multiply` returns `NULL` exactly when one of its written steps overflows.
Previously, the optimizer could regroup such an expression and evaluate its
operations in the mode of the outermost one. For example, with
`spark.sql.ansi.enabled` set to `true`, `try_add(x + y, 1)` fails with
`ARITHMETIC_OVERFLOW` when `x + y` overflows instead of returning `NULL`,
`try_add(x, y) + 1` returns `NULL` when `x + y` overflows instead of failing,
and `try_add(try_add(x, -5), y)` returns `x - 5 + y` when only `x + y`
overflows instead of `NULL`. The previous results were wrong, and only
expressions over non-constant inputs were affected. To get `NULL` when an inner
`+` or `*` overflows, use `try_add` or `try_multiply` for it as well, for
example `try_add(try_add(x, y), 1)`.
Review Comment:
Two things about this entry:
- Differently grouped `try_add`/`try_multiply` chains no longer canonicalize
to the same expression, so a `GROUP BY` that used to resolve, e.g. `SELECT
try_add(try_add(a, b), 1) FROM t GROUP BY try_add(try_add(a, 1), b)`, now fails
with `MISSING_AGGREGATION`. The decimal entry below says so for LEGACY
decimals; could this one say it for TRY chains?
- nit: the entries say "Since Spark 5.0", which fits only if this is
master-only. `branch-4.x` is 4.5.0-SNAPSHOT, so if this correctness fix also
goes to `branch-4.x`, they belong in a 4.4 to 4.5 section.
##########
docs/streaming/ss-migration-guide.md:
##########
@@ -23,6 +23,16 @@ Note that this migration guide describes the items specific
to Structured Stream
Many items of SQL migration can be applied when migrating Structured Streaming
to higher versions.
Please refer [Migration Guide: SQL, Datasets and
DataFrame](../sql-migration-guide.html).
+## Upgrading from Structured Streaming 4.4 to 5.0
+
+- Since Spark 5.0, `try_add` and `try_multiply` combined with regular `+` or
`*` follow each
Review Comment:
nit: this note is under "Upgrading from Structured Streaming 4.4 to 5.0" but
says the restart fails for queries "started before Spark 4.3". The old rewrite
is still on 4.3 and 4.4, so a query started on those and restarted on 5.0 sees
the nullable key too. Should it say "started before Spark 5.0"?
--
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]