stevomitric opened a new pull request, #60347:
URL: https://github.com/apache/spark/pull/60347

   ### What changes were proposed in this pull request?
   1. Goal: casting a special datetime string (`epoch`, `now`, `today`, 
`tomorrow`, `yesterday`) to `TIMESTAMP_NTZ(p)` / `TIMESTAMP_LTZ(p)` with p in 
[7, 9] gives the same value as the microsecond cast, typed at the requested 
precision, wherever Spark folds such a cast today: explicit casts, implicit 
casts in comparisons, column defaults and DSv2 static partition values.
   2. Problem: the `SpecialDatetimeValues` rule only rewrites casts to `DATE`, 
`TIMESTAMP` and `TIMESTAMP_NTZ`, so a nanosecond target falls through to the 
string parser, which doesn't know special values.
   ```sql
   SET spark.sql.ansi.enabled=false;
   SELECT CAST('epoch' AS TIMESTAMP_NTZ), CAST('epoch' AS TIMESTAMP_NTZ(9));
   ```
   Before: `1970-01-01 00:00:00 NULL` (with ANSI on, the second cast raises 
`CAST_INVALID_INPUT`) -> Target: `1970-01-01 00:00:00       1970-01-01 00:00:00`
   3. Solution: the rule now covers the nanosecond types and folds such a cast 
into a literal of the exact target type. The special value is resolved once as 
an instant or a local date-time, which the existing microsecond helpers convert 
as before and new nanosecond helpers floor to the target precision, so `now` 
keeps its sub-microsecond digits like `current_timestamp(p)`. Non-foldable 
strings are not converted, as for the microsecond types. When evaluating the 
foldable string fails, the rule now leaves the cast in place instead of failing 
the query, so the error is raised only if the cast is evaluated, as with 
constant folding.
   
   ### Why are the changes needed?
   The nanosecond timestamp SPIP (SPARK-56822) requires that functions behave 
like the microsecond types for p in [7, 9]. Today the same cast returns a value 
for the microsecond type and NULL or `CAST_INVALID_INPUT` for the nanosecond 
types, and an implicit cast in a filter silently drops rows.
   
   ### Does this PR introduce _any_ user-facing change?
   Yes, for nanosecond timestamps, which are behind 
`spark.sql.timestampNanosTypes.enabled`: a foldable cast of a special datetime 
string to `TIMESTAMP_NTZ(p)` / `TIMESTAMP_LTZ(p)` returns the special value 
instead of NULL or `CAST_INVALID_INPUT`. For every date and timestamp target, a 
cast of a foldable string that fails to evaluate (for example 
`element_at(array('a'), 2)` with ANSI on) now fails the query only if the cast 
is evaluated: it no longer fails in a `CASE` branch that is never taken, or in 
a column that the query prunes.
   
   ### How was this patch tested?
   New tests.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Co-authred-by: Cloud Opus 5.5
   


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