spark-test commented on code in PR #60558:
URL: https://github.com/apache/spark/pull/60558#discussion_r4589784726
##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala:
##########
@@ -399,17 +399,24 @@ abstract class JdbcDialect extends Serializable with
Logging {
val timestampFormatter = TimestampFormatter.getFractionFormatter(
DateTimeUtils.getZoneId(SQLConf.get.sessionLocalTimeZone))
s"'${timestampFormatter.format(timestampValue)}'"
+ case timestampValue: Instant if
SQLConf.get.legacyJdbcSessionZoneTimestampLiteral =>
+ compileInstantInSessionZone(timestampValue)
case timestampValue: Instant =>
- val timestampFormatter = TimestampFormatter.getFractionFormatter(
- DateTimeUtils.getZoneId(SQLConf.get.sessionLocalTimeZone))
- s"'${timestampFormatter.format(timestampValue)}'"
+ // Same text as the Timestamp a non-java8 API session would pass:
JVM-zone wall clock.
+
compileValue(DateTimeUtils.toJavaTimestamp(DateTimeUtils.instantToMicros(timestampValue)))
Review Comment:
**Blocking (P1):** With `spark.sql.timestampNanosTypes.enabled` on, a
literal such as `TIMESTAMP '2020-02-02 04:13:14.123456789'` reaches this branch
as an `Instant` that still carries all nine digits (through
`TimestampLTZNanosTypeOps.toScala`, under either java8API setting).
`instantToMicros` then drops everything below a microsecond, so the pushed text
ends in `.123456`, where the base branch's formatter wrote all nine digits.
Against a JDBC catalog with `preferTimestampNanos=true` over a `TIMESTAMP(9)`
column (for example H2 or DB2, or Oracle with `zonelessTimestampAsNTZ=false`),
the database therefore compares against a different value. By the review's
reading of the code, `ts = TIMESTAMP '2020-02-02 04:13:14.123456789'` misses
the stored `.123456789` row, and `ts > ...` also returns rows in `(.123456,
.123456789]`. `JDBCScanBuilder.pushPredicates` does not hand compiled
predicates back for Spark to re-check, so these rows come back silently. That
includes sessions whose time
zone matches the JVM's, where the base branch returned the right rows; on
Oracle, a query that used to fail now returns wrong rows. Microsecond
`TIMESTAMP` values keep their digits, and none of the new tests uses a non-zero
fraction, which is why they pass. Restoring the nanoseconds on the `Timestamp`
before compiling it keeps every digit, the same way the JDBC writer already
handles `TIMESTAMP_LTZ(7-9)` values (`JdbcUtils.scala:575-580`).
`OracleDialect.scala:270` needs the same change.
```suggestion
val ts =
DateTimeUtils.toJavaTimestamp(DateTimeUtils.instantToMicros(timestampValue))
ts.setNanos(timestampValue.getNano)
compileValue(ts)
```
**Verification:**
- **Regression:** With the nanosecond timestamp preview enabled and a JDBC
catalog that sets preferTimestampNanos over a TIMESTAMP(9) column holding
2020-02-02 04:13:14.123456789, equality, range and IN filters on the literal
TIMESTAMP '2020-02-02 04:13:14.123456789' are fully pushed. They return the
same rows as Spark's own evaluation under both java8API settings, with the
session time zone equal to and different from the JVM time zone.
- **Behavior:** For an instant with micro- and sub-microsecond digits, such
as 2026-06-26T05:00:00.123456789Z, compileValue gives the same text as the
equivalent java.sql.Timestamp with those nanoseconds for every built-in dialect
URL the rendering test covers, and the base and Oracle texts end in .123456789.
- **Compatibility:** For an instant with only microsecond digits, such as
2026-06-26T05:00:00.123456Z, the base and Oracle dialects render the same text,
ending in .123456, as the java.sql.Timestamp that a session with java8API off
pushes. The texts under the legacy flag and for SQL Server keep their current
form.
##########
docs/sql-migration-guide.md:
##########
@@ -99,6 +99,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, when `spark.sql.datetime.java8API.enabled` is `true`, a
`TIMESTAMP` value in a filter pushed down to a JDBC data source is written as
wall-clock time in the JVM default time zone, the same as when the
configuration is `false`. Previously, it was written in the session time zone
(`spark.sql.session.timeZone`), so a query could return different rows with and
without filter pushdown when the session and JVM time zones differ.
`JdbcDialect.compileValue` follows the same rule for a `java.time.Instant`, so
a custom dialect that calls it may produce different SQL. The SQL Server
dialect still uses the session time zone. To restore the previous behavior, set
`spark.sql.legacy.jdbc.sessionZoneTimestampLiteral.enabled` to `true`.
Review Comment:
**Nit (P3):** For the Oracle dialect the previous behavior was not
session-zone text. `OracleDialect.compileValue` had no `Instant` case, so the
value was written unquoted (for example `2026-06-26T05:00:00Z`) and the pushed
filter failed. With `spark.sql.legacy.jdbc.sessionZoneTimestampLiteral.enabled`
set, the new Oracle case is skipped and that unquoted value comes back, as the
new `JDBCSuite` test asserts. A reader of this entry learns neither that Oracle
timestamp filters now run nor that setting the flag makes them fail again for
every Oracle source in the session. One possible wording:
```suggestion
- Since Spark 4.4, when `spark.sql.datetime.java8API.enabled` is `true`, a
`TIMESTAMP` value in a filter pushed down to a JDBC data source is written as
wall-clock time in the JVM default time zone, the same as when the
configuration is `false`. Previously, it was written in the session time zone
(`spark.sql.session.timeZone`), so a query could return different rows with and
without filter pushdown when the session and JVM time zones differ. The Oracle
dialect was an exception: it wrote the value unquoted, so such pushed filters
failed, and it now writes a `{ts '...'}` literal in the JVM default time zone.
`JdbcDialect.compileValue` follows the same rule for a `java.time.Instant`, so
a custom dialect that calls it may produce different SQL. The SQL Server
dialect still uses the session time zone. To restore the previous behavior, set
`spark.sql.legacy.jdbc.sessionZoneTimestampLiteral.enabled` to `true`; for the
Oracle dialect this brings back the unquoted value, so its pushed `TIM
ESTAMP` filters fail again.
```
**Verification:**
- **Inspection:** The entry's account of Oracle matches the code and the
internal configuration description. Before the change the Oracle dialect sent
the Instant unquoted and the pushed filter failed. It now sends the
escape-quoted JVM-zone literal. The legacy flag restores the unquoted literal.
--
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]