dongjoon-hyun opened a new pull request, #60653:
URL: https://github.com/apache/spark/pull/60653
### What changes were proposed in this pull request?
This PR aims to fix query errors by `BooleanSimplification` when an operand
of `AND` or `OR` guards a throwable expression (`Expression.throwable`, e.g.
`raise_error` and `assert_true`). The rule moved such an expression ahead of
its guard, so it was evaluated on rows that the original predicate does not
evaluate it on.
`BooleanSimplification` now skips the following rewrites in that case.
| Case | Rewrite | Skipped when |
|---|---|---|
| Common factor elimination for conjunction | `(a OR r) AND (c OR r)` => `r
OR (a AND c)` | a common factor (`r`) is throwable |
| Common factor elimination for disjunction | `(a AND r) OR (c AND r)` => `r
AND (a OR c)` | a common factor (`r`) is throwable |
| `c` is not nullable | `(NOT c OR b) AND c` => `b AND c` | `b` is throwable
|
| `c` is not nullable | `(NOT c AND b) OR c` => `b OR c` | `b` is throwable |
The factoring is skipped rather than rewritten into another order, because
the order that keeps every operand behind its guard depends on where the common
factor is in both operands. For example, `(a AND c) OR r` is safe for `(a OR r)
AND (c OR r)`, but for `(r OR a) AND (r OR c)` it evaluates a throwable `a` on
the rows where `r` is true. A throwable common factor is rare, so this keeps
the rule simple.
The other rewrites of the rule do not evaluate any operand on more rows than
the original predicate does, so they are unchanged.
### Why are the changes needed?
`AND` and `OR` skip the right operand when the left one decides the result,
so a predicate like `a OR raise_error(...) IS NULL` raises the error only on
the rows where `a` is not true. After `BooleanSimplification` moves
`raise_error` first, the query fails although the original predicate never
evaluates `raise_error` on any row.
```sql
-- It should return no rows, but fails with USER_RAISED_EXCEPTION.
SELECT id FROM range(3) WHERE (id < 5 OR raise_error('boom') IS NULL) AND id
>= 5;
-- It should return 0, 1, 2, but fails with USER_RAISED_EXCEPTION.
SELECT id FROM range(3) WHERE (id >= 5 AND raise_error('boom') IS NULL) OR
id < 5;
```
A throwable common factor keeps its two copies in the predicate, and
subexpression elimination can evaluate such a duplicated expression ahead of
its guards by itself. So the following queries disable it to show the issue of
`BooleanSimplification` only.
```sql
SET spark.sql.subexpressionElimination.enabled=false;
-- It should return 0, 1, 2, but fails with USER_RAISED_EXCEPTION.
SELECT id FROM range(3)
WHERE (id < 5 OR raise_error('boom') IS NULL) AND (id > -5 OR
raise_error('boom') IS NULL);
-- It should return no rows, but fails with USER_RAISED_EXCEPTION.
SELECT id FROM range(3)
WHERE (id > 5 AND raise_error('boom') IS NULL) OR (id < -5 AND
raise_error('boom') IS NULL);
```
For example, the optimized filter of the third query in Apache Spark 4.3.0
is the following.
```
Filter (isnull(raise_error(USER_RAISED_EXCEPTION, map(keys: [errorMessage],
values: [boom]), NullType)) OR ((id#0L < 5) AND (id#0L > -5)))
```
### Does this PR introduce _any_ user-facing change?
Yes. The above queries return the expected results instead of failing. All
released versions fail: Apache Spark
[3.5.9](https://github.com/apache/spark/releases/tag/v3.5.9) (2026-07-12),
[4.0.5](https://github.com/apache/spark/releases/tag/v4.0.5) (2026-10-21),
[4.1.4](https://github.com/apache/spark/releases/tag/v4.1.4) (2026-10-21),
[4.2.1](https://github.com/apache/spark/releases/tag/v4.2.1) (2026-10-21) and
[4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27).
### How was this patch tested?
Pass the CIs with the newly added test cases.
- `BooleanSimplificationSuite`: the four shapes with `raise_error` are kept
as they are.
- `DataFrameSuite`: the four queries return the expected results. The two
with a common factor run with subexpression elimination disabled.
### 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]