dongjoon-hyun opened a new pull request, #60083:
URL: https://github.com/apache/spark/pull/60083

   ### What changes were proposed in this pull request?
   
   This PR aims to fix `ConstantFolding` dropping the error of a failing LEGACY 
cast when the session has ANSI mode off.
   
   `ConstantFolding.hasNoSideEffect` treats every non-ANSI `Cast` as free of 
side effects under a non-ANSI session, so `size(array(...))` over such a cast 
is folded to a constant without evaluating the cast. However, some LEGACY casts 
still fail at runtime, for example:
   - `CAST(INTERVAL '1000' SECOND AS TINYINT)` fails with `CAST_OVERFLOW`.
   - `CAST('abc' AS INTERVAL DAY TO SECOND)` fails with 
`INVALID_INTERVAL_FORMAT`.
   - `CAST(12345 AS VARCHAR(2))` fails with `EXCEED_LIMIT_LENGTH` when 
`spark.sql.preserveCharVarcharTypeInfo` is `true`.
   
   This PR adds `Cast.isNoThrowLegacyCast`, which accepts only the LEGACY casts 
among numeric, boolean and string types, without a CHAR/VARCHAR length 
constraint on the target. These casts wrap around on overflow or return null 
for an invalid input instead of failing. `ConstantFolding` now skips only such 
LEGACY casts and TRY casts, which return null instead of failing.
   
   The session check stays, so this PR does not fold anything new in ANSI 
sessions. It only stops folding the LEGACY casts that are not accepted by 
`Cast.isNoThrowLegacyCast` in non-ANSI sessions.
   
   ### Why are the changes needed?
   
   To return the cast error instead of a wrong result. This bug was introduced 
at Apache Spark 3.3.0 by SPARK-37392 (#34823), which started to skip every cast 
in `ConstantFolding.hasNoSideEffect` under a non-ANSI session.
   - https://github.com/apache/spark/releases/tag/v3.3.0 (2022-06-09)
   
   For example, with `spark.sql.ansi.enabled=false`:
   
   ```sql
   CREATE TABLE t(i INTERVAL SECOND) USING parquet;
   INSERT INTO t VALUES (INTERVAL '1000' SECOND);
   
   SELECT CAST(i AS TINYINT) FROM t;
   -- [CAST_OVERFLOW] ...
   
   SELECT size(array(CAST(i AS TINYINT))) FROM t;
   -- Before: 1
   -- After:  [CAST_OVERFLOW] ...
   ```
   
   The case of SPARK-37392, `size(array(CAST(int_col AS STRING), ...))`, is 
still folded.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. When the session has ANSI mode off, `size()` of an array built from a 
failing LEGACY cast now returns the cast error instead of the array size. The 
result is unchanged when the cast does not fail.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude 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