stevomitric opened a new pull request, #60348: URL: https://github.com/apache/spark/pull/60348
### What changes were proposed in this pull request? 1. Goal: writing a nanosecond-precision timestamp into a VARIANT fails at analysis with a clear error, while reading one from a variant keeps working. 2. Problem: every write path passes analysis and then crashes, because Spark's variant encoding has no nanosecond primitive. ```sql SELECT CAST(TIMESTAMP_NTZ '2020-01-01 00:00:00.123456789' AS VARIANT); ``` Before: `[INTERNAL_ERROR] The Spark SQL phase optimization failed with an internal error.` (over a column: `scala.MatchError: TimestampNTZNanosType(9)`; `try_cast` returns NULL) -> Target: `[DATATYPE_MISMATCH.CAST_WITHOUT_SUGGESTION] Cannot resolve "CAST(TIMESTAMP_NTZ '2020-01-01 00:00:00.123456789' AS VARIANT)" due to data type mismatch: cannot cast "TIMESTAMP_NTZ(9)" to "VARIANT". SQLSTATE: 42K09` 3. Solution: the type check shared by both directions now takes the direction, and the write direction accepts only the datetime types the variant encoding can store (DATE, TIMESTAMP, TIMESTAMP_NTZ), the same way intervals are rejected. This covers `CAST` / `try_cast` (also nested in arrays, maps and structs), `to_variant_object`, `variant_from_arrays`, `variant_from_entries`, `variant_insert` / `variant_set` / `variant_array_append` and their `try_` forms, `VALUES`, and `INSERT` under the legacy store assignment policy. TIME, which has the same gap, gets the same error. ### Why are the changes needed? A crash or a silent NULL breaks the nanosecond SPIP's parity rule (SPARK-56822); a classed error doesn't. Storing nanoseconds in a variant needs new binary primitives (Parquet Variant type ids 18/19), which is left to a follow-up. ### Does this PR introduce _any_ user-facing change? Yes, for nanosecond timestamps (behind `spark.sql.timestampNanosTypes.enabled`) and TIME. Writing one into a VARIANT now fails at analysis instead of crashing at runtime (or returning NULL from `try_cast`). Reading them from a variant is unchanged. ### How was this patch tested? New tests. ### Was this patch authored or co-authored using generative AI tooling? Co-authored-by: Isaac -- 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]
