dongjoon-hyun opened a new pull request, #60771: URL: https://github.com/apache/spark/pull/60771
### What changes were proposed in this pull request? This PR fixes `CombineFilters` so that it does not combine a condition that can fail into the filter below when the condition of that filter can be NULL. `CombineFilters` rewrites `Filter(fc, Filter(nc, child))` into `Filter(nc AND fc, child)`. As written, `fc` is evaluated only on the rows where `nc` is TRUE: the inner filter drops a row on which `nc` is FALSE or NULL. After the rewrite, `NULL AND x` still evaluates `x`, so `fc` is also evaluated on the rows where `nc` is NULL, and fails there if it can fail, e.g. a division by zero in ANSI mode. This PR changes the following: - `PredicateHelper.canBeNull(condition)` (new) returns whether a filter condition can be NULL. A column that a conjunct `IS NOT NULL` checks is taken as not nullable in the other conjuncts, as `FilterExec` does, because the condition is FALSE, not NULL, when that column is NULL. - `PredicateHelper.canFail(cond, failable)` (new) is the check `splitMovable` already did inline: `ExprUtils.canEvaluateUnconditionally` rejects the conjunct, or it reads a column of `failableOutput`. It also looks only at the key of a dynamic partition pruning filter (`DynamicPruningSubquery`) or of a runtime bloom filter (`might_contain` of a scalar subquery), whose subquery is computed once. Otherwise, as `canEvaluateUnconditionally` rejects any subquery, such a filter over a scan filter that can be NULL, e.g. the `OR`s of TPC-DS q13, would stay in a separate filter, and dynamic partition pruning would no longer prune the partitions of the scan. - `CombineFilters` passes `afterBarrier = nc.throwable || canBeNull(nc)` to `splitMovable`. #60711 (SPARK-62069) passes `nc.throwable`, which covers the filter that `ReorderJoin` keeps in place of the top join, whose first condition is throwable. With `afterBarrier`, which #60725 (SPARK-62091) added, the conjuncts are split as if they were all written after a non-deterministic or throwable conjunct, so only the ones that can not fail may move. So when `nc` can be NULL, even if it is not throwable, the conjuncts of `fc` that can not fail are still combined into the inner filter, and the others stay in a filter above it. - `OperationHelper` (`PhysicalOperation` and `ScanOperation`) collects adjacent filters over a scan and combines them into one condition in physical planning, e.g. `FileSourceStrategy` puts them into one `FilterExec`, so the query still fails when `CombineFilters` keeps them apart. A new `canCombine` applies the same check: `PhysicalOperation` stops collecting at such a filter, and `ScanOperation` returns it and the filters above it as `filtersStayUp`, as it does for a non-deterministic one. | Conjunct of the outer filter | The inner condition can not be NULL | The inner condition can be NULL | |---|---|---| | Can not fail, e.g. `k > 0` | Combined | Combined | | Can fail, e.g. `10 / (k - w) > 0` | Combined | Before: Combined, After: Kept above | `InferFiltersFromConstraints` adds `IS NOT NULL` checks for the columns of a null-intolerant condition such as `a > 0`. Then the inner condition can not be NULL, and `CombineFilters` combines the filters as before in the batch that runs after it. So most plans do not change. The filters stay apart only when the inner condition can still be NULL, e.g. an `OR` or a `CASE WHEN` over a nullable column. Among the TPC-DS plans, only q39b changes: over `CASE WHEN mean = 0.0 THEN false ELSE stdev / mean > 1.0 END`, which is NULL when `stdev` is, `CASE WHEN mean = 0.0 THEN false ELSE stdev / mean > 1.5 END` now stays in a separate `Filter` in the same whole-stage codegen stage. `ExprUtils.canEvaluateUnconditionally` does not accept a `CASE WHEN`, so this is conservative. This PR does not wrap the inner condition as `coalesce(nc, false)`. The constraints of a `Filter` would lose the `IS NOT NULL` checks inferred from `nc`, and `RewritePredicateSubquery` and the subexpression elimination of `FilterExec` would still evaluate parts of `fc` ahead of the wrapper. ### Why are the changes needed? A query that should succeed fails. ```sql SET spark.sql.ansi.enabled=true; CREATE TABLE t(k INT, f INT, g INT, w INT) USING parquet; INSERT INTO t VALUES (1, 5, NULL, 1), (2, 5, 1, 0); -- It should return 2, but fails with DIVIDE_BY_ZERO. SELECT k FROM (SELECT * FROM t WHERE f > g OR k > 5) WHERE 10 / (k - w) > 0; ``` `f > g OR k > 5` is NULL on `k = 1`, so the inner `WHERE` drops that row, and `10 / (k - w)` should never be computed for it. `CombineFilters` merges both into `(f > g OR k > 5) AND 10 / (k - w) > 0`, which divides by zero on `k = 1`. ### Does this PR introduce _any_ user-facing change? Yes. The query above returns `2`. This is a long-standing issue. All of [v3.5.9](https://github.com/apache/spark/releases/tag/v3.5.9) (2026-07-12), [v4.0.5](https://github.com/apache/spark/releases/tag/v4.0.5) (2026-10-21), [v4.1.4](https://github.com/apache/spark/releases/tag/v4.1.4) (2026-10-21), [v4.2.1](https://github.com/apache/spark/releases/tag/v4.2.1) (2026-10-21) and [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27) fail with `spark.sql.codegen.wholeStage=false`. With whole-stage codegen on, the default, they return `2`, because the generated `FilterExec` code dropped a row at the first NULL conjunct. #59573 (SPARK-60315), which is not released yet, made it evaluate the remaining conjuncts as the interpreted path does, so `master`, `branch-4.4` and `branch-4.x` fail with the default configs too. When the inner condition can be NULL, a conjunct of the outer filter that can fail is now in a separate filter above it, in both the optimized plan and the physical plan. Like a filter after a non-deterministic one, it is not pushed down to the data source, as `ScanOperation` returns it in `filtersStayUp`. For example, `.filter(ln($"bonus") > 7).filter(exp($"bonus") > 0)` over a JDBC table no longer pushes `EXP(BONUS) > 0.0` down to the database, because `LN(BONUS) > 7.0` can be NULL and `ExprUtils.canEvaluateUnconditionally` does not accept `exp`. The check is conservative, so this also holds for functions that can not fail in fact. Written in one filter, `.filter(ln($"bonus") > 7 && exp($"bonus") > 0)`, both conditions are pushed down as before. ### How was this patch tested? Pass the CIs with the newly added test cases. All of them fail without this PR. - `FilterPushdownSuite`: a division over a filter `c = 1` stays above it, and `a = 1` is combined into it. Over `c IS NOT NULL AND c = 1`, the division is combined. - `PhysicalOperationSuite`: `PhysicalOperation` does not collect an ANSI cast over a filter `b = 'x'`, and `ScanOperation` returns it in `filtersStayUp`. A condition that can not fail, or one over `b IS NOT NULL AND b = 'x'`, is collected. - `DataFrameSuite`: the query above, written with the DataFrame API over a Parquet table, returns `2` with whole-stage codegen on and off. Without the `OperationHelper` change, it still fails, as `FileSourceStrategy` combines the two filters. Some existing tests combined a condition that can fail into a filter that can be NULL. They are changed so that they keep testing what they were written for: - `FilterPushdownSuite`: the filters below now check `IS NOT NULL` first, for a division before a non-deterministic or throwable conjunct (SPARK-60980), and for a sequence without a step (SPARK-46707). - `JDBCV2Suite` "scan with filter push-down": `df13`, `df15` and `df16` check that math functions are pushed down to H2. Each wrote its conditions as separate filters over `LN(BONUS) > 7.0`, `LOG(2.0, BONUS) > 10.0` or `SIN(BONUS) < -0.08`, which can be NULL. They now write them in one filter, and push down the same filters as before. The golden files of TPC-DS q39b for the `PlanStabilitySuite`s are regenerated. Without the `canFail` handling of the dynamic partition pruning and runtime bloom filters, 12 more plans changed (q13, q16, q40, q48, q80, q80a, q85, q94 and q95): those filters stayed in a separate `Filter`, and for q13, q40, q48 and q85, dynamic partition pruning was no longer a partition filter of the scan. I also ran these suites locally, and they pass: the catalyst `optimizer` and `planning` suites, the `PlanStabilitySuite`s, `DataFrameSuite`, `FileSourceStrategySuite`, `ParquetV1FilterSuite`, `ParquetV2FilterSuite`, `OrcV1FilterSuite`, `OrcV2FilterSuite`, `JDBCV2Suite`, `JDBCSuite`, `DataSourceV2Suite`, `InMemoryColumnarQuerySuite`, `ExplainSuite`, `ParquetV1SchemaPruningSuite`, `PruneFileSourcePartitionsSuite` and `MergeSubplansSuite`. With a temporary suite (not included), in ANSI mode with whole-stage codegen on and off, the query above returns `2`, also with `CASE WHEN f < 0 THEN raise_error('neg') ELSE f > g END` as the inner `WHERE` clause. That is the case that #60711 (SPARK-62069) lists as still failing, as the filters over the file scan are merged again in physical planning. ### 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]
