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

   ### What changes were proposed in this pull request?
   
   This PR aims to fix `LIKE` and `RLIKE` so that generated code raises the 
error of a foldable pattern only when a row evaluates the pattern, as the 
interpreted path does.
   
   `Like.doGenCode` and `RLike.doGenCode` evaluate a foldable pattern once at 
code generation time to compile it into a field. Since SPARK-39106 (Apache 
Spark 3.3.0), `ConstantFolding` keeps a foldable expression that fails to 
evaluate inside a conditional branch, because the branch may never be taken. So 
such a pattern reaches code generation, and `right.eval()` throws while 
`WholeStageCodegenExec` generates the code, before any row is processed. 
`WholeStageCodegenExec.doExecute` falls back only when compilation fails, so 
the whole query fails.
   
   This PR adds `StringRegexExpression.foldablePatternForCodegen`, which 
returns `None` when a foldable pattern fails to evaluate. Then `Like` and 
`RLike` generate the same code as for a non-foldable pattern, which evaluates 
and compiles the pattern for each row. 
`StringRegexExpression._expensivePattern` already catches `NonFatal` for the 
same kind of pattern.
   
   ### Why are the changes needed?
   
   The interpreted path compiles a foldable pattern lazily 
(`StringRegexExpression.cache`), so the following queries succeed without 
whole-stage codegen but fail with it, which is the default.
   
   ```sql
   SET spark.sql.ansi.enabled=true;
   CREATE OR REPLACE TEMPORARY VIEW o AS SELECT id AS k FROM range(1, 3);
   
   -- It should return [1,true] and [2,true], but fails with DIVIDE_BY_ZERO.
   SELECT k, IF(k > 0, true, 'x' RLIKE CAST(10 / 0 AS STRING)) FROM o;
   SELECT k, IF(k > 0, true, 'x' LIKE CAST(10 / 0 AS STRING)) FROM o;
   ```
   
   ```
   [DIVIDE_BY_ZERO] Division by zero. Use `try_divide` to tolerate divisor 
being 0 and return NULL instead. If necessary set "spark.sql.ansi.enabled" to 
"false" to bypass this error. SQLSTATE: 22012
   ```
   
   With `spark.sql.codegen.wholeStage=false`, the queries return the expected 
rows, but `UnsafeProjection` logs `Expr codegen error and falling back to 
interpreter mode` for every partition. I checked this with [v4.0.5 
(2026-10-21)](https://github.com/apache/spark/releases/tag/v4.0.5) and [v4.3.0 
(2026-10-27)](https://github.com/apache/spark/releases/tag/v4.3.0).
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Queries like the above now succeed with whole-stage codegen and return 
the same results as the interpreted path. When a row evaluates the pattern, the 
query still fails with the same error as before.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case in `RegexpExpressionsSuite`. 
Without this fix, the test fails at `Like.doGenCode` and `RLike.doGenCode`.
   
   I also ran the above queries on Apache Spark 4.3.0 with the patched 
`regexpExpressions.scala` in front of the classpath. Both queries return 
`[1,true]` and `[2,true]`. With `IF(k > 1, ...)`, which evaluates the pattern 
for `k = 1`, both queries still fail with `DIVIDE_BY_ZERO`.
   
   ### 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