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

   [SPARK-60983][SQL] Fix query errors by ConstantPropagation when a constant 
is substituted into an expression that can fail
   
   ### What changes were proposed in this pull request?
   
   `ConstantPropagation` replaces an attribute with the literal of an equality 
conjunct, such as `y = 0`, in the other comparisons of the same `AND`. The 
literal is the value of the attribute only on the rows that pass the whole 
`AND`. However, the rewritten conjuncts also run on other rows, and a part that 
becomes constant is folded by `ConstantFolding` before any row is read. So an 
expression that can fail, such as a division, an ANSI cast or an array index, 
can raise an error that the query cannot raise in any evaluation order of its 
conjuncts:
   
   - `10 / (id - y) > 0 AND y = 0` becomes `10 / (id - 0) > 0 AND y = 0`, which 
divides by zero on a row with `id = 0` and `y = 1`.
   - `y = 0 AND id > 10 / y` becomes `y = 0 AND id > 10 / 0`, which fails 
during the optimization, even on an empty table.
   
   This PR replaces an attribute only where its value cannot change whether an 
expression fails:
   
   - under operators that cannot fail on their own, such as comparisons, `AND`, 
`OR`, `NOT`, `IS NULL` and `COALESCE`. This is the allowlist of 
`ExprUtils.canEvaluateUnconditionally`, which is now also exposed as 
`ExprUtils.isTotalOperator`.
   - in a subexpression whose attributes are all replaced, when it folds to a 
literal without an error. For example, `y = 5 AND x = y + 1` still becomes `y = 
5 AND x = 6`.
   
   Otherwise, the expression keeps its attributes. The comparisons in the 
pulled up condition of a correlated `IN` or `EXISTS` subquery are still 
rewritten as before.
   
   ### Why are the changes needed?
   
   The queries below cannot raise an error in any order of their conjuncts, but 
fail with `DIVIDE_BY_ZERO`.
   
   ```sql
   CREATE TABLE t1(id BIGINT, y BIGINT) USING parquet;
   INSERT INTO t1 VALUES (0, 1), (6, 0);
   -- It should return (6, 0), but fails with DIVIDE_BY_ZERO.
   SELECT * FROM t1 WHERE 10 / (id - y) > 0 AND y = 0;
   
   CREATE TABLE t2(id BIGINT, y BIGINT) USING parquet;
   -- It should return no rows, but fails with DIVIDE_BY_ZERO, with or without 
rows in `t2`.
   SELECT * FROM t2 WHERE y = 0 AND id > 10 / y;
   ```
   
   The same happens with other expressions that can fail. For example, `s = 
'abc' AND CAST(s AS INT) > 0` fails with `CAST_INVALID_INPUT` on a table 
without `'abc'`, and `arr[i] > 0 AND i = 5` fails with `INVALID_ARRAY_INDEX` on 
a row with a shorter array and `i = 1`. A UDF or `assert_true` also fails like 
this with ANSI mode off.
   
   `ConstantPropagation` has substituted into all comparisons since SPARK-42500 
(#42038), and ANSI mode has been on by default since SPARK-44444 (#46013). Both 
are in [v4.0.0](https://github.com/apache/spark/tree/v4.0.0) (2025-05-19). 
Before that, it only substituted into `=` and `<=>` (SPARK-20758, #17993, 
[v2.3.0](https://github.com/apache/spark/tree/v2.3.0) (2018-02-22)).
   
   This PR should be merged after #60162 (SPARK-60985). Without it, 
`InferFiltersFromConstraints` infers the expression that this PR no longer 
rewrites, such as `10 / (id - 0) > 0` from `10 / (id - y) > 0 AND y = 0`, and 
puts it ahead of the original conditions. Then a query that works today, such 
as `SELECT * FROM t WHERE id > 5 AND y = 0 AND 10 / (id - y) > 0` on rows `(0, 
0)` and `(6, 0)`, would fail with `DIVIDE_BY_ZERO`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The queries above, which fail with an error, now return their results. 
Some comparisons keep an attribute instead of a literal, for example `y = 5 AND 
x / y > 1` and `y = 5 AND CASE WHEN y > 0 THEN x ELSE z END > 1`. The results 
are the same.
   
   ### How was this patch tested?
   
   - Added `SPARK-60983: Do not propagate a constant into an expression that 
can fail` to `ConstantPropagationSuite`. It covers an expression that can fail 
and does not become constant, one that becomes constant but fails to fold, one 
that folds, and a comparison whose other side can fail. Without this change, 
the test fails.
   - The existing `SPARK-32290: SingleColumn Null Aware Anti Join Optimize` in 
`SubquerySuite` covers the comparisons in the condition of a correlated 
subquery. It fails if they are not rewritten.
   - Ran all suites in `org.apache.spark.sql.catalyst.optimizer`, 
`ExprUtilsSuite`, the TPC-DS and TPC-H `PlanStabilitySuite`s with ANSI mode on 
and off, `SQLQueryTestSuite`, `SQLQuerySuite`, `DataFrameSuite`, `JoinSuite`, 
`SubquerySuite`, `ExplainSuite`, `CachedTableSuite`, 
`DynamicPartitionPruningV1SuiteAEOff`, `ParquetV1FilterSuite`, 
`ParquetV2FilterSuite`, `OrcV1FilterSuite`, `FileSourceStrategySuite`, 
`PruneFileSourcePartitionsSuite` and the DSv2 strategy and SQL suites. All pass 
and no golden files change, except `udtf/udtf.sql` in `SQLQueryTestSuite`, 
which also fails without this change in my environment because of the error 
message of a newer Python.
   - Ran the queries above and the variants with a cast, an array index, a UDF, 
`assert_true`, `HAVING`, a window, a view and `<=>` before and after this 
change, together with the change of #60162. They no longer fail, and `y = 0 AND 
id > 10 / y` still fails on a row with `y = 0`.
   
   ### 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