MaxGekk commented on code in PR #59135:
URL: https://github.com/apache/spark/pull/59135#discussion_r4641305329
##########
docs/sql-migration-guide.md:
##########
@@ -100,6 +100,7 @@ license: |
- Since Spark 4.4, `CREATE TABLE`, `REPLACE TABLE`, and `ALTER TABLE ... ADD
CONSTRAINT` on a Data Source V2 table fail with `SUBQUERY_IN_CHECK_CONSTRAINT`
when the condition of a CHECK constraint contains a subquery. `ALTER TABLE ...
ADD CONSTRAINT` keeps failing with
`UNSUPPORTED_SUBQUERY_EXPRESSION_CATEGORY.HIGHER_ORDER_FUNCTION` on a subquery
in a lambda function. Previously, most of these statements failed with
`INTERNAL_ERROR`, but a subquery that needed no resolution, such as `id >
(SELECT 0 AS a)`, was accepted and every write evaluated it again
(SPARK-60781). Existing constraints that contain a subquery are not rejected.
- 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, `date_add`, `date_sub`, `dateadd`, and adding or
subtracting an integer number of days to or from a date (for example `date + 1`
or `date - 1`) fail with `DATETIME_OVERFLOW` when the number of days since
1970-01-01 in the result does not fit in an `INT`, regardless of
`spark.sql.ansi.enabled`. Previously, the result silently wrapped around; for
example, `date_add(DATE'2020-01-01', 2147483647)` returned a date in year
-5877591. `try_add` and `try_subtract` on dates now return `NULL` in this case
instead of the wrapped date. The same check applies to a `RANGE` window frame
ordered by a `DATE` column with an integer offset: for example, `RANGE BETWEEN
CURRENT ROW AND 2147483647 FOLLOWING` now fails with `DATETIME_OVERFLOW`
instead of using a wrapped frame boundary, which gave wrong results.
Review Comment:
Two things about the note:
- It mentions integer days and integer `RANGE` offsets, but `date + INTERVAL
'1' DAY`, `date - INTERVAL ...` and a `RANGE` frame ordered by a `DATE` with an
`INTERVAL n DAY` offset are rewritten to `DateAdd(l,
ExtractANSIIntervalDays(r))` too (`BinaryArithmeticWithDatetimeResolver`,
`WindowEvaluatorFactoryBase`), so they now raise as well. Could the note say
that every date-plus-days operation is affected?
- With `spark.sql.ansi.enabled=false` these used to wrap and now fail, with
no way back. Other behavior changes in this section come with a
`spark.sql.legacy.*` flag. Could this have one, or could the note say that none
exists?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala:
##########
@@ -2925,6 +2926,35 @@ private[sql] object QueryExecutionErrors extends
QueryErrorsBase with ExecutionE
summary = "")
}
+ // A DATE as a SQL literal in the proleptic Gregorian calendar of DATE
values, whatever
+ // `spark.sql.legacy.timeParserPolicy` is. Under LEGACY, `toSQLValue(_,
DateType)` formats with
+ // the legacy formatter, whose rebasing to the Julian calendar overflows for
the dates within
+ // about 44,500 days of the minimum DATE, so building the error would throw
instead.
+ private def toSQLDate(days: Int): String = {
+ val formatter = new DefaultDateFormatter(
+ DateFormatter.defaultLocale,
LegacyDateFormats.LENIENT_SIMPLE_DATE_FORMAT, isParsing = false)
+ s"DATE '${formatter.format(days)}'"
+ }
+
+ def dateAddDaysOverflowError(days: Int, numDays: Int): ArithmeticException =
{
Review Comment:
The new `DATETIME_OVERFLOW` errors pass `context = Array.empty`, and
`DateAdd`/`DateSub` don't implement `SupportQueryContext`, so in `SELECT
date_add(a, x), date_add(b, y) FROM t` the error doesn't say which expression
overflowed, unlike the ANSI arithmetic errors. Could they carry the query
context, as the ANSI arithmetic expressions do, and pass `getContextOrNull()`
to these builders?
--
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]