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

   ### What changes were proposed in this pull request?
   Extend common-type resolution so two `TIME(p)` operands of differing 
fractional-seconds precision resolve to `TIME(max(p1, p2))`:
   - `TypeCoercionHelper.findWiderDateTimeType` now widens two `TIME` operands 
to the larger precision. Because both the default (`TypeCoercion`) and ANSI 
(`AnsiTypeCoercion`) `findTightestCommonType` implementations delegate to this 
single method, the change covers UNION/INTERSECT/EXCEPT, 
COALESCE/CASE/IF/NULLIF, GREATEST/LEAST, IN lists/subqueries, and array/map 
literals.
   - `Cast.canANSIStoreAssign` allows lossless TIME widening (smaller -> larger 
precision) as a silent store assignment, while narrowing stays 
explicit-CAST-only. `UpCastRule.canUpCast` already keeps cross-precision TIME 
explicit-only, mirroring the nanosecond-timestamp precedent.
   
   Cross-family pairs (TIME vs DATE / TIMESTAMP) remain incomparable.
   
   ### Why are the changes needed?
   ANSI SQL (ISO/IEC 9075-2, result-type rule for a set of comparable datetime 
types) requires that across datetime operands of differing fractional-seconds 
precision the result type is the datetime type with the largest 
fractional-seconds precision. Previously `findWiderDateTimeType` returned 
`None` for any pair involving `TIME`, so set and conditional operations over 
mixed `TIME(p)` raised an analysis error and required an explicit `CAST`. The 
widening cast (`TIME(p1) -> TIME(p2)` via `truncateTimeToPrecision`) already 
existed, so this was purely a common-type-resolution gap.
   
   ### Does this PR introduce _any_ user-facing change?
   Yes, within the unreleased TIME type (`@Unstable`). Set and conditional 
operations over mixed `TIME(p)` now resolve `TIME(max(p))` without an explicit 
cast; values from the smaller-precision side are widened losslessly. Inserting 
a smaller-precision TIME value into a larger-precision TIME column now 
succeeds, while the reverse (narrowing) still requires an explicit cast.
   
   For example:
   ```sql
   SELECT typeof(c), c FROM (
     SELECT '12:34:56.789' :: TIME(3) AS c
       UNION ALL SELECT '01:02:03.456789' :: TIME(6)) ORDER BY c;
   -- time(6)  01:02:03.456789
   -- time(6)  12:34:56.789
   ```
   
   ### How was this patch tested?
   - Added type-coercion unit tests in `TypeCoercionSuite` and 
`AnsiTypeCoercionSuite` (widening to max precision, equal precision, and 
TIME-vs-DATE/TIMESTAMP returning `None`).
   - Added a store-assignment / up-cast contract test in `CastSuiteBase`.
   - Added golden-file coverage in `time.sql` for UNION, COALESCE, CASE, 
NULLIF, GREATEST/LEAST, array/map, IN, and store-assignment INSERTs (widening 
succeeds, narrowing fails without an explicit cast).
   - Ran the affected catalyst suites and the `time.sql` `SQLQueryTestSuite`.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Generated-by: Cursor (Claude Opus 4.8)


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