Skip to content

[SPARK-62144][SQL] Fix query failures by PushPredicateThroughNonJoin when a condition that can fail follows a non-deterministic or throwable one - #60766

Open
dongjoon-hyun wants to merge 3 commits into
apache:masterfrom
dongjoon-hyun:SPARK-62144

Conversation

@dongjoon-hyun

@dongjoon-hyun dongjoon-hyun commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

This PR fixes PushPredicateThroughNonJoin so that it does not push a condition that can fail, such as a division in ANSI mode, below an operator ahead of a non-deterministic or throwable condition written before it.

For a filter over an operator, PushPredicateThroughNonJoin pushes the conjuncts that the operator lets through below it, where they are evaluated before the filter. Since #20069 (SPARK-22895), it pushes every such deterministic conjunct, no matter where it is written. #59944 (SPARK-60633) added splitPushable, which keeps a throwable conjunct above when a conjunct written before it stays above, but it still pushes the other ones. So a conjunct written after a non-deterministic or throwable one that stays above is evaluated ahead of it, on rows that the AND short-circuit would have kept from it. This is the failure that #60157 (SPARK-60980) and #60631 (SPARK-61886) fixed for CombineFilters, and #60646 (SPARK-61924) for PushPredicateThroughJoin, here reached through the other operators.

This PR changes splitPushable. Once a non-deterministic or throwable conjunct stays above, a conjunct written after it is pushed only when it can not fail:

This is the check that PredicateHelper.splitMovable makes for PushPredicateThroughJoin and, since #60692 (SPARK-62049), for CombineFilters. Only below a Project can a non-deterministic conjunct be pushed, if it is cheap. Such a conjunct can not fail when it passes the check but for rand, randn, spark_partition_id() or monotonically_increasing_id() in it, e.g. rand(10) < 0.01, so it is still pushed, as before.

Conjunct written after a non-deterministic or throwable one that stays above Before After
Can fail, e.g. 10L / (id - 3) > 0 Pushed Kept above
Can not fail, over a column that can fail Pushed Kept above
Can not fail, over columns that can not fail, e.g. id > 3 Pushed Pushed

The conjuncts kept above stay in their written order. A conjunct written after a deterministic one that stays above, such as c > 1 over the result of an aggregate, is still pushed unless it is throwable, as before. That follows CombineFilters and PushPredicateThroughJoin, which also keep the order only after a non-deterministic or throwable conjunct. So is a cheap non-deterministic conjunct below a Project, e.g. rand(10) < 0.01 written after a condition over an expensive alias: it goes ahead of the pushed conjuncts written after it, so they still only see the rows it keeps.

splitPushable is used for a filter over a Project, an Aggregate, a Window, a Union, an EventTimeWatermark and the other operators of canPushThrough, so this covers all of them. Over a Project, the conjuncts kept above go to the projection split of #58403 (SPARK-55014), which could place a cheap one in its lowest layer, that is below the projection all the same. A cheap conjunct is not split off now, since it is only kept above for its order. Before this PR, the only cheap conjuncts kept above were throwable ones, which were not split off already.

Why are the changes needed?

A query that should succeed fails. With ANSI mode, the default since Apache Spark 4.0.0, a division or a cast is enough.

-- Issue 1: It should return no row, but fails with DIVIDE_BY_ZERO.
SELECT * FROM (SELECT id, count(*) c FROM range(0, 10, 1, 1) GROUP BY id)
WHERE rand(10) * 10 > 100 AND 10L / (id - 3) > 0;

-- Issue 2: It should return no row, but fails with DIVIDE_BY_ZERO.
SELECT * FROM (SELECT id FROM range(0, 10, 1, 1) UNION ALL SELECT id FROM range(0, 10, 1, 1))
WHERE rand(10) * 10 > 100 AND 10L / (id - 3) > 0;

rand(10) * 10 > 100 is false on every row, so the division should never run for id = 3. rand(10) * 10 > 100 stays above the Aggregate and the Union, as it is non-deterministic, but PushPredicateThroughNonJoin pushes 10L / (id - 3) > 0 below the Aggregate in Issue 1, and into both children of the Union in Issue 2. A Window partitioned by id and an explode fail the same way. So does a throwable conjunct written first, such as CASE WHEN c > 100 THEN raise_error('boom') ELSE false END over the result of the aggregate.

Two cases are left to separate issues, as they also fail without this PR:

  • A column that can fail behind an operator that PredicateHelper.failableOutput does not look through, such as a Union, a Window, a Generate or a repartition, or a grouping key of an Aggregate. A conjunct over such a column can still be pushed, or an inferred isnotnull over it can go ahead of the condition. For example, q > 0 written after rand(10) * 10 > 100, where a child of a Union computes q as 10L / (id - 3), is still pushed into the child.
  • The projection split of [SPARK-55014][SQL] Split projection for expensive filters #58403 (SPARK-55014), which can still filter an expensive condition that can fail, such as one over a UDF, in a lower layer ahead of a non-deterministic or throwable condition written before it.

Does this PR introduce any user-facing change?

Yes. The queries above succeed. This is a long-standing issue since v2.3.0 (2018-02-22). I confirmed that both issues fail with DIVIDE_BY_ZERO in ANSI mode on the released v3.5.9 (2026-07-12), v4.0.5 (2026-10-21), v4.2.1 (2026-10-21) and v4.3.0 (2026-10-27), with whole-stage codegen on and off. It is easier to hit since v4.0.0 (2025-05-19), which enables ANSI mode by default.

A conjunct written after a non-deterministic or throwable one that stays above is no longer pushed below the operator, unless it can not fail. So such a conjunct no longer prunes partitions or is pushed down to the data source. For example, rand(10) < 0.5 AND CAST(s AS INT) = 1 over an aggregate grouped by a string column s keeps CAST(s AS INT) = 1 above the aggregate in ANSI mode. Then rand(10) < 0.5 is evaluated on every group again, as written, instead of only on the group that the cast kept, so the rows it keeps can differ. A conjunct written before the non-deterministic or throwable one is not affected.

How was this patch tested?

Pass the CIs with the newly added test cases. They fail without this PR, except the one that checks that a cheap non-deterministic condition written after a condition over an expensive alias is still pushed, a placement that this PR keeps.

  • FilterPushdownSuite:
    • Over an Aggregate, a condition over an alias of a division, written after rand(10) > 0.5 or a throwable condition, stays above, and a condition over a grouping key is still pushed. A condition over a grouping key that the projection below computes with a division stays above too. After a deterministic condition that stays above, a condition over an alias of a division is still pushed, and so is a division in legacy mode, which can not fail, after rand(10) > 0.5.
    • Over a Union, a division written after rand(10) > 0.5 stays above, and a condition over a column is still pushed into both children.
    • Over a Window and a Sort, a condition over a column that the projection below computes with a division, written after rand(10) > 0.5, stays above, and a condition over a column that can not fail is still pushed.
    • Over a Project with an expensive alias, a cheap division written after a non-deterministic condition over that alias is neither pushed nor split off into a lower layer, and a condition over a column is still pushed.
    • Over a Project with an expensive alias, a cheap non-deterministic condition written after a condition over that alias is still pushed below the projection, as before this PR, with a division written after it in the same filter, after it.
    • Over a Project with an expensive alias, rand(10) > 0.5 written after a throwable or non-deterministic condition that stays above is still pushed below the projection, as before this PR, and rand(30) < a / b after it stays above.
  • DataFrameSuite: Issues 1 and 2, written with the DataFrame API, return no row in ANSI mode with whole-stage codegen on and off, instead of failing with DIVIDE_BY_ZERO.

I also ran the catalyst optimizer suites, DataFrameSuite and the PlanStabilitySuites locally. No golden files change. With a temporary suite (not included), I ran Issues 1 and 2 before and after this PR, along with a Window, an explode, a Project with an expensive alias and the throwable conjunct above, with whole-stage codegen on and off. All of them fail before this PR and succeed after it.

Was this patch authored or co-authored using generative AI tooling?

Generated-by: Claude Opus 5.5

@dongjoon-hyun

Copy link
Copy Markdown
Member Author

Rebase to master to sync with the latest patches.

@spark-test spark-test left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review summary

By this review's reading of the code, the fix holds for the queries it targets: after a non-deterministic or throwable condition that stays above an Aggregate, Union, Window, watermark or unary operator, a condition that divides is no longer pushed below it, and, except over a Union, neither is a condition over a column that the plan directly below computes with a division. Five findings remain. In the Project case, a cheap non-deterministic condition written after a condition over an expensive alias, such as rand(10) < 0.01 after a condition over a Scala UDF, now stays above the projection instead of being pushed below it as on the base branch, so the UDF runs on every input row, although the description says such a condition is still pushed as before. The projection split still filters an expensive condition that can fail, such as one over a throwing UDF alias, in a lower layer ahead of a non-deterministic or throwable condition written before it, so, by the same reading, a query whose rand(10) < 0.0 drops every row now fails there, where the base branch returns no row. The failable-column check does not see columns through the operators that PredicateHelper.failableOutput does not look through: the Union case checks an always-empty set, so the description's own Union example still pushes q > 0 into both children, and over an Aggregate, Window, Generate or watermark a conjunct kept above can still reach rows a join below drops, through an inferred isnotnull filter or through an operator above them. The new tests leave the new argument of the Window, watermark, unary-node and Project cases, the Project placement change and the ANSI-off path without a test whose result depends on them.

Cross-cutting observations

  • The queries that newly fail in the projection split fail only because a cheap non-deterministic condition written after an expensive one, such as rand() < 0.01 after a condition over a UDF, now stays in the top filter instead of going below the projection, where the base branch put it under every split layer: the split, unchanged in this respect, then evaluates the conditions written after it in lower layers ahead of it.

    Restoring the old placement of such a condition removes those new failures but not the split's placement of conditions that can fail after an expensive or throwable condition, which already fail on the base branch, so the split needs its own fix either way.

    Related:

    • Projection split still runs conditions that can fail ahead of a non-deterministic or throwable one
    • Project case no longer pushes a cheap non-deterministic condition written after an expensive one

Findings

5 total: 0 P0, 2 P1, 2 P2, 1 P3.

Blocking (P1)

  • Project case no longer pushes a cheap non-deterministic condition written after an expensive one — sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:3182 — see inline.
  • Projection split still runs conditions that can fail ahead of a non-deterministic or throwable one — sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:2884 — see inline.

Non-blocking (P2)

  • Union case checks an always-empty set of failable columns — sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:2964 — see inline.
  • Kept-above conjuncts still reach rows a join drops through inferred filters and nested operators — sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:3188 — see inline.

Nit (P3)

  • New tests leave most changed push cases and the ANSI-off path unpinned — sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala:1103 — see inline.

Shared repair plans

Shared repair plan 1

Covered findings:

  • Kept-above conjuncts still reach rows a join drops through inferred filters and nested operators — sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:3188
  • Union case checks an always-empty set of failable columns — sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:2964

Recommended change: Make failableOutput report, through each operator PushPredicateThroughNonJoin pushes conditions through, the failable columns of its input that it outputs: for a Union, each output column at a position where some child reports a failable column, as ConstantPropagation.failableColumnsOf does; for a Window, an EventTimeWatermark and the canPushThrough operators, the child's failable columns in its own output; and for an Aggregate, a grouping-key column, or an alias of one, that its child reports, besides its failable result columns. Rewrite the doc, which now says a Union, Generate, Window or Repartition computes every column of its input rows, to say why a condition moved below them can still move on. The Union case's call at Optimizer.scala:2964 then sees the children's columns unchanged. Add a comment beside canPushThrough that an operator added there also needs this pass-through in failableOutput, and add optimizer plan tests and end-to-end ANSI DataFrame tests for these shapes.

Why this works: Once failableOutput reports a column through these operators, the check the PR added sees it in every case that hands it the plan below, however many such operators lie between the filter and the projection that computes the column, so the conjunct stays above the barrier; and InferFiltersFromConstraints, whose guard reads failableOutput of the filter's child, no longer infers isnotnull or another filter over that column ahead of the condition, so nothing derived from it reaches the projection below.

Risks: CombineFilters, PushPredicateThroughJoin, ReorderJoin and the inner-join extraction pattern, through splitMovable, and InferFiltersFromConstraints become more conservative over a Union, Window, Generate, watermark, repartition or Aggregate above a column that can fail: a condition over such a column written after a non-deterministic or throwable condition stays where it is, and an inferred filter over such a column is no longer added ahead of a filter's condition even without such a condition, which can change optimized plans outside these findings, including PlanStabilitySuite golden plans; reverting the change to failableOutput rolls it back.

Constraints: Keep the existing pass-through for Filter, Sample, the limits, Offset, Sort and Join and the existing Project and Aggregate result-column rules, and map a Union's columns by output position, not by name or expression ID.

Exclusions:

  • PushdownPredicatesAndPruneColumnsForCTEDef, which pushes a CTE reference's predicates into its definition by its own rule, is left as it is.

Verification:

  • Regression: Plan tests show that after a non-deterministic condition, a condition over a column that a union child, a projection below a Window under a Sort, or a projection below an Aggregate's grouping key computes with an ANSI division stays above the barrier, while a condition over a plain column is still pushed.
  • Regression: End-to-end ANSI queries through the full optimizer, with whole-stage codegen on and off, over an Aggregate grouped by and a Window partitioned by a nullable column that a projection over an inner join computes with a division failing on an unmatched row, and over a Union whose child computes such a column, return no row when the condition written first is false on every row.
  • Compatibility: Inferred-filter and join-pushdown coverage shows InferFiltersFromConstraints still adds filters over columns that can not fail through these operators, and CombineFilters and PushPredicateThroughJoin still move conditions that can not fail, with any changed optimized plans reviewed for an expected cause.

PR description suggestions

  • The sentence "A Union and a Generate compute every column of their child on every row, so a query that reads such a column still fails, although the conjunct now stays above" does not match the code: the Union case checks no failable column, so q > 0 in that example is still pushed into both children. Also, with whole-stage codegen (and spark.sql.codegen.wholeStage.union.enabled, on by default) a Union passes each child column to its parent unevaluated, so whether such a query fails once the conjunct stays above depends on codegen. Update the sentence to say what such a query does once the Union case checks its children's columns.

Generated by Omnigent on Databricks

// The head of `rest`, if any, is the first conjunct that cannot be pushed, so it stays above.
// So does the head of `ordered`, if any, as it is non-deterministic or throwable.
val (reorderable, ordered) = rest.span { c =>
pushedForm(c).deterministic && !pushedForm(c).throwable

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking (P1): In the Project case canPush is isCheap(used) (Optimizer.scala:2862), which admits a cheap non-deterministic conjunct such as rand(10) < 0.01 or spark_partition_id() = 0. When it is written after a conjunct over an expensive alias, this span stops at it, so it heads ordered, and ExprUtils.canEvaluateUnconditionally rejects it because it is non-deterministic, so it stays above the projection. The base branch pushed it below (rest.drop(1).partition(c => canPush(c) && !throwable(c))).

By this review's reading of the code, with a Scala UDF score, spark.range(1000000).select($"id", score($"id") as "s").where($"s" > 0.5 && rand(10) < 0.01) was planned as Filter(s > 0.5) over the projection over Filter(rand(10) < 0.01), so score ran on about 1% of the rows. Now the rule returns the filter unchanged and score runs on every row, a cost that grows with the input. With a Python UDF alias, which counts as expensive by default, each of those rows also goes to the Python worker, so a UDF that raises on a row the condition drops, such as a row of another partition under spark_partition_id() = 0, now fails a query that returns its rows on the base branch.

The description says "A conjunct written after a deterministic one that stays above ... is still pushed unless it is throwable, as before", and the stated problem does not need this: pushed ahead as before, the non-deterministic conjunct still keeps every later conjunct that can fail behind it in the same pushed filter. Letting a cheap, non-throwable conjunct continue the reorderable part, for example rest.span(c => (pushedForm(c).deterministic || canPush(c)) && !pushedForm(c).throwable), restores the base placement, and only the Project case changes, since every other caller's canPush already requires determinism. If giving a seeded rand the rows the conditions before it keep is the intent, the description should state that regression and its cost instead.

Verification:

  • Regression: A plan test over a projection with an expensive UDF alias shows a cheap non-deterministic condition written after a condition over that alias pushed below the projection, with a cheap ANSI division written after it pushed in the same filter after it.
  • Behavior: A plan test with a Python UDF alias and spark_partition_id() = 0 written after a condition over it shows the partition condition below the projection, so the UDF evaluation only gets that partition's rows.
  • Regression: An end-to-end query with rand(10) < 0.0 written between a condition over one Scala UDF alias and two conditions over a second alias whose UDF throws on some row returns no row with whole-stage codegen on and off.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. You are right, this placement did not need to change. A cheap non-deterministic conjunct that can be pushed now continues the part that is pushed as before ((deterministic || canPush) && !throwable), so rand(10) < 0.01 goes below the projection again, and the conjuncts pushed after it follow it in the same filter. I added a plan test for this placement and corrected the PR description.

val (splittable, notSplittable) = expensiveWithUsed.partition {
case (cond, _, replaced) => cond.deterministic && !replaced.throwable
case (cond, used, replaced) =>
cond.deterministic && !replaced.throwable && !isCheap(used)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking (P1): This partition keeps only cheap conditions out of the split, so an expensive condition that can fail, such as a comparison over a Scala or Python UDF alias or an RLIKE whose pattern comes from the data, still goes to splitProjectForExpensiveConditions when it is written after a non-deterministic or throwable condition that stays above. The split orders by cost and filters it in a lower layer (Optimizer.scala:3304-3327), below the top filter that keeps that condition, although the new comments at Optimizer.scala:2857-2859 and 2877-2879 and the splitPushable doc say a condition that can fail written after it is not evaluated ahead of it.

By this review's reading of the code, with Scala UDFs ok and parse, where parse throws on 'x', spark.range(10).selectExpr("IF(id = 3, 'x', CAST(id AS STRING)) AS e").select($"e", ok($"e") as "f", parse($"e") as "g").where($"f" && rand(10) < 0.0 && $"g" > 0 && $"g" < 100) returns no row on the base branch, which pushes rand(10) < 0.0 below every layer. With this PR, rand(10) < 0.0 stays in the top filter, and the split puts Filter(g > 0 AND g < 100) over a projection computing parse(e) below it, so parse runs on every row and the query fails on 'x'. With an expensive or throwable barrier, such as (Rand(10) > 0.5 || j) in the SPARK-55014 test at FilterPushdownSuite.scala:748, the base branch already filters such a condition below it, and this PR leaves that unchanged. The unchanged tests at FilterPushdownSuite.scala:748 and 799 expect an RLIKE over concat(e, tag), whose pattern comes from the data, in a layer below the barrier.

Recommended change: In the Project case, also keep out of the split every kept-above condition written after the first non-deterministic or throwable condition that stays above when it can fail, that is when ExprUtils.canEvaluateUnconditionally rejects it with the aliases substituted or it reads a column failableOutput(grandChild) reports, as the PR already does for cheap conditions; those conditions stay in the top filter in written order. Update the comment at Optimizer.scala:2872-2879 and the expected plans of 'SPARK-55014: a non-deterministic condition is not split off into a lower layer' and 'SPARK-60602: a throwable condition does not keep later conditions from being split off', and add projection-split plan tests and an end-to-end UDF query test.

Why this works: The split then only receives conditions written before the first non-deterministic or throwable condition that stays above, or after it but unable to fail, so whatever layer it puts them in, no condition that can fail is evaluated ahead of a condition written before it that stays above.

Risks: Every condition over an expensive alias written after a non-deterministic or throwable condition stays in the top filter, since no UDF or regex passes the never-fail check, so its alias runs on the rows that reach the top filter rather than on fewer rows in a lower layer, including for UDFs that never fail; two existing split tests change their expected plans. Reverting the partition change rolls it back.

Constraints: Conditions written before the first non-deterministic or throwable condition that stays above, and conditions after it that can not fail, keep going to the split, and the top filter keeps its written order.

Exclusions:

  • With whole-stage codegen off, a projection computes every alias for every row it gets, so after an expensive or throwable barrier a query whose top projection computes an alias that can fail still fails, as on the base branch; the repair does not change how a projection computes a column.

Verification:

  • Regression: Plan tests with an expensive non-deterministic condition, and with a throwable condition, followed by an expensive condition over an RLIKE whose pattern comes from the data keep that condition in the top filter after the barrier, and the two existing split tests that expect it in a lower layer are updated to match.
  • Regression: With whole-stage codegen on, an end-to-end query over a projection with two Scala UDF aliases f and g, written as (rand(10) < 0.0 OR f) AND g > 0 AND g < 100, where g's UDF throws on a row on which f is false, returns the rows the written condition keeps, where the base branch fails.
  • Behavior: A plan test shows that expensive conditions written before a barrier are still split into lower layers.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the fix above, rand(10) * 10 > 100 goes below the split again, so your query returns no row, as before this PR. I confirmed it with Scala UDFs. The remaining part, where the split filters an expensive condition that can fail in a lower layer after an expensive or throwable condition that stays above, also fails without this PR, and fixing it changes what the SPARK-55014 and SPARK-60602 tests expect. So I'll handle it in a separate JIRA. I narrowed the comment of the Project case to the cheap conjuncts, and the PR description mentions the remaining part.

// Union could change the rows, so non-deterministic predicate can't be pushed down
val (pushDown, stayUp) = splitPushableConjuncts(condition)(_.deterministic)
val (pushDown, stayUp) =
splitPushableConjuncts(condition, failableOutput(union))(_.deterministic)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (P2): failableOutput(union) is always empty: PredicateHelper.failableOutput (predicates.scala:267-291) has no Union case, so the failable-column test in splitPushable never keeps a conjunct above here. The other push cases pass the plan below the operator (Optimizer.scala:2864, 2949, 2995, 3093 and 3141).

By this review's reading of the code, in ANSI mode SELECT * FROM (SELECT CAST(l.id * 1000000000 AS INT) AS k FROM range(10) AS l JOIN range(3) AS r ON l.id = r.id UNION ALL SELECT CAST(id AS INT) AS k FROM range(10)) WHERE rand(10) * 10 > 100 AND k > 0 should return no row, since the first child computes k only for ids 0 to 2. The Union case still pushes k > 0 into both children; in the first one the Project case pushes it through the projection as CAST(l.id * 1000000000 AS INT) > 0, and PushPredicateThroughJoin moves it into l, where the cast overflows on id 3. Over a Window partitioned by k on the same join, the PR keeps k > 0 above and that query returns no row. The description's own example, q > 0 over a child computing 10L / (id - 3), is pushed the same way, although the description says the conjunct now stays above. Passing the children's columns only at this call site would not cover a nullable column, since InferFiltersFromConstraints also reads failableOutput of the union (Optimizer.scala:2377-2391).

See Shared repair plan 1 in the review body.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, failableOutput has no Union case, and the PR description was wrong about that example. I corrected it. As you noted, InferFiltersFromConstraints reads it too, so I'll fix it in failableOutput itself in a separate JIRA, together with the next comment.

val (pushedReorderable, stayUpReorderable) = reorderable.partition(canPush)
val (pushedOrdered, stayUpOrdered) = ordered.partition { c =>
canPush(c) && ExprUtils.canEvaluateUnconditionally(pushedForm(c)) &&
!pushedForm(c).references.exists(failableColumns.contains)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (P2): This check only sees the columns failableOutput reports for the plan directly below the operator, and failableOutput (predicates.scala:267-291) reports nothing through a Window, Generate, Union, EventTimeWatermark, Expand or repartition, nor an Aggregate's grouping keys. A pushed conjunct lands in a filter of its own, where nothing written before it stays above, so it can move on below the next operator.

By this review's reading of the code, in ANSI mode, with l(id, b) holding a row with b = 3 whose id r lacks, SELECT * FROM (SELECT k, count(*) AS c FROM (SELECT 10L / (l.b - 3) AS k FROM l JOIN r ON l.id = r.id) GROUP BY k) WHERE rand(10) * 10 > 100 AND k > 0 should return no row. The Aggregate case keeps k > 0 above, but InferFiltersFromConstraints' Filter case (Optimizer.scala:2377-2391) then adds isnotnull(k) ahead of the condition, because a division is always nullable and failableOutput of the Aggregate reports no grouping key. In the next batch that leading conjunct is pushed below the aggregate, through the projection as isnotnull(10L / (l.b - 3)), and into l, where it divides by zero on the unmatched row, as k > 0 itself did on the base branch. A Window partitioned by k, a Generate or a watermark takes the same path. Over a Sort or a repartition above a Window partitioned by k, the unary case gets an empty set and pushes k > 0 itself below the barrier, even when k is not nullable.

No new test reaches either path: the optimizer batch in FilterPushdownSuite runs no InferFiltersFromConstraints, and the DataFrameSuite test only uses conditions that divide themselves.

See Shared repair plan 1 in the review body.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed. These queries also fail without this PR, and the same gap reaches CombineFilters and PushPredicateThroughJoin through splitMovable. A change to failableOutput changes all of its callers, so I'll handle it in a separate JIRA. The PR description mentions it.

private def ansiDivide(l: Expression, r: Expression): Expression =
Divide(l, r, NumericEvalContext(EvalMode.ANSI))

test("SPARK-62144: a condition that can fail is not pushed through an aggregate ahead of a " +

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (P3): These tests reach the new failable-column argument only through the Aggregate case (overFailableKey at FilterPushdownSuite.scala:1119-1123), and the barrier only through the Aggregate, Union and Project-split cases. The Window (Optimizer.scala:2949), EventTimeWatermark (2995), unary-node (3141) and Project (2864) cases also gained the argument, but the SPARK-60633 tests that reach them (FilterPushdownSuite.scala:939-1097) use LocalRelation children and write only conditions that cannot fail after the barrier, so, as this review reads them, replacing any of those arguments with an empty set would leave every test green. No test has a cheap non-deterministic condition written after a condition over an expensive alias, the shape whose placement the Project case changes, and every new division is built in ANSI mode, so treating a legacy-mode division as able to fail would not be caught either.

Verification:

  • Regression: Each new per-case plan test fails when that case's failable columns are replaced by an empty set, and passes with the case as fixed.
  • Behavior: The legacy-mode test shows a non-ANSI division written after Rand(10) > 0.5 pushed below the operator, and the placement test expects the placement that restoring the base branch's behavior for that condition gives.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a plan test over a Window and a Sort whose column comes from a division in the projection below, which fails when either call site passes an empty set, the placement test over a Project, and a division in legacy mode that is still pushed after rand(10) > 0.5.

@dongjoon-hyun

Copy link
Copy Markdown
Member Author

Rebase to master to sync with the latest patches.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants