sarutak commented on code in PR #60366:
URL: https://github.com/apache/spark/pull/60366#discussion_r4609231328


##########
docs/sql-migration-guide.md:
##########
@@ -100,6 +100,7 @@ license: |
 - Since Spark 4.4, a CHECK constraint that `ALTER TABLE ... ADD CONSTRAINT` 
adds to a Data Source V2 table, and whose condition reads the current time, 
date, catalog, user and the like, such as `ts <= current_timestamp()` or `db = 
current_database()`, is checked against their values at the time of each write. 
Previously, it was checked against their values at the time of the `ALTER 
TABLE`, unless a write could not read back a predicate for its condition, such 
as one with a lambda function or one that passes a column to `datediff`, `year` 
or `upper` (SPARK-60032). Such a constraint is stored without a predicate, so 
each write parses and analyzes its SQL text under the session of the write, as 
it does for a constraint without a predicate that `CREATE TABLE` adds. A 
setting of the write such as `spark.sql.ansi.enforceReservedKeywords`, which 
reserves `TIME`, or `spark.sql.ansi.doubleQuotedIdentifiers` can then make it 
fail to parse the text or read it differently. A row-level operation
  that copies unchanged rows, such as a `DELETE` or `UPDATE` on a copy-on-write 
table, fails with `CHECK_CONSTRAINT_VIOLATION` on a copied row that no longer 
satisfies the condition. For example, `ts >= current_timestamp() - INTERVAL 30 
DAYS` now fails such a `DELETE` once a copied row is older than 30 days. A 
condition that Spark detects a write could resolve differently is stored as 
before, so a write accepts and rejects the same rows for it as before. Examples 
are conditions that also read a variable, a `CHAR` column, a name with more 
than one part such as a column qualified by the table name, or a constant that 
depends on the session time zone, such as `'2024-01-01 00:00:00'` cast to a 
timestamp. So are conditions with a name such as `current_date` or `user` 
without parentheses, which a write can read as a column name, and conditions 
whose text the parser settings of the `ALTER TABLE`, such as 
`spark.sql.ansi.doubleQuotedIdentifiers` or 
`spark.sql.parser.escapedStringLiterals`, r
 ead differently from their default values. ANSI mode, 
`spark.sql.timestampType` and the session time zone are taken from the session 
for this comparison, as for a generation expression. A condition that reads the 
current date or time only through a typed literal such as `DATE 'today'` is 
also stored as before. An existing constraint keeps its stored predicate until 
it is dropped and added again. `Check.predicate()` is null for a constraint 
stored without a predicate, so a connector that validates or enforces it itself 
has to evaluate `Check.predicateSql()`. To restore the previous behavior, set 
`spark.sql.legacy.freezeCurrentValuesInCheckConstraint` to `true` before adding 
the constraint. A constraint added without it is not changed.
 - Since Spark 4.4, a function created by `CREATE FUNCTION ... AS 'class'` 
whose class implements 
`org.apache.spark.sql.connector.catalog.functions.UnboundFunction` and extends 
either a Hive UDF class (such as `UDF` or `GenericUDF`) or 
`org.apache.spark.sql.expressions.UserDefinedAggregateFunction` runs as a 
DataSource V2 function instead of a Hive UDF or Spark UDAF, including functions 
created in earlier versions. Its result type, argument coercion, and null 
handling are defined by the V2 implementation selected by 
`UnboundFunction.bind`. Hive's `@UDFType` and Spark UDAF's `deterministic` 
property do not control the V2 path. For scalar functions, determinism uses 
`BoundFunction.isDeterministic()` (default `true`) and the arguments' 
determinism. A call with constant arguments can therefore become eligible for 
constant folding during query planning instead of being evaluated per row.
 - Since Spark 4.4, `next_day` and `last_day` fail with `DATETIME_OVERFLOW` 
when the result is later than the maximum `DATE`, `+5881580-07-11`, and `trunc` 
fails with it when the result is earlier than the minimum `DATE`, 
`-5877641-06-23`, regardless of `spark.sql.ansi.enabled`. Previously, these 
functions silently returned a wrong date, in most cases one at the other end of 
the `DATE` range; for example, `last_day(DATE'+5881580-07-11')` returned 
`-5877641-07-12` and `trunc(DATE'-5877641-06-23', 'MONTH')` returned 
`+5881580-06-20`, while `trunc` to `QUARTER` failed with `ARITHMETIC_OVERFLOW`. 
`next_day` and `trunc` to `WEEK` also returned a wrong date for the first days 
of the `DATE` range; for example, `next_day(DATE'-5877641-06-23', 'MO')` and 
`trunc(DATE'-5877641-07-01', 'WEEK')` returned `-5877641-06-25`, a Thursday, 
and now return `-5877641-06-29`.
+- Since Spark 4.4, in a correlated `LATERAL` subquery, or a scalar subquery 
whose COUNT bug `DecorrelateInnerQuery` handles (that is, not a single 
top-level aggregate), a scalar Python UDF applied to an aggregate function of 
an aggregate without `GROUP BY`, such as `inv(count(*))` where `inv` divides by 
its argument, is evaluated once per distinct value of the correlated columns, 
above the left outer join, on that row's aggregate values. A query whose outer 
rows all have matching rows no longer fails: previously the UDF applied to the 
result over zero rows, such as `inv(0)`, was kept in a conditional branch and, 
because Spark evaluates a Python UDF in a conditional branch for every row, it 
was evaluated even on the outer rows that have matching rows. For an outer row 
without matching rows the UDF is now evaluated on the aggregate functions' 
results over zero rows, so a query that relied on guarding such a UDF, for 
example `CASE WHEN count(*) = 0 THEN 0 ELSE inv(10 / count(*)) END`, 
 can now fail there: with `DIVIDE_BY_ZERO` in ANSI mode, or by calling the UDF 
with the values over zero rows (e.g. `NULL` for `max`) without ANSI mode. This 
matches the same subquery without correlation, and a guarded Python UDF outside 
any subquery, which already fail this way; previously the correlated form's 
result over zero rows was simplified to the guarded constant by the optimizer. 
A scalar subquery that is a single top-level aggregate, such as `(SELECT 
inv(count(*)) FROM b WHERE b.k = a.k)`, is not affected.

Review Comment:
   No migration-guide entry: no query that returns results today changes which 
rows it returns or their values, with one narrow exception, so there is nothing 
a user needs to act on. The exception is that a non-deterministic UDF, or a 
seeded non-deterministic expression in a marked column's arguments, is now 
evaluated only for the outer rows without matching rows, so its call count and 
a seeded value can change; the rows that read the result are the same. This is 
an internal behavior change, reversible with the config, so I did not add a 
migration-guide entry, matching SPARK-45507 (the earlier count-bug fix), which 
added none. For L983, the guarded form keeps master's result on both the 
`DecorrelateInnerQuery` and the `constructLeftJoins` paths, so the two paths no 
longer disagree. What remains until SPARK-61660 is that an unguarded 
`inv(count(*))` still fails as a single top-level `Aggregate` (the 
`constructLeftJoins` path), while it works in `LATERAL`; since neither path 
needs a mi
 gration-guide entry, SPARK-61660 does not have to land in the same release. I 
added a `TODO(SPARK-61660)` at CASE 2/3 of `constructLeftJoins`, where the same 
marker would apply.
   



-- 
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]

Reply via email to