viirya commented on code in PR #60631:
URL: https://github.com/apache/spark/pull/60631#discussion_r4605900626
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala:
##########
@@ -2631,10 +2631,14 @@ object CombineFilters extends Rule[LogicalPlan] with
PredicateHelper {
// Combining a condition written after a non-deterministic or throwable
one evaluates it ahead
// of that one, so it must also be unable to fail. `throwable` does not
report e.g. a division
// by zero in ANSI mode, which the condition before it may have kept from
being evaluated.
+ // Nor may it read a column that can fail (see `failableOutput`).
case Filter(fc, nf @ Filter(nc, grandChild)) if nc.deterministic =>
val (leading, following) =
splitConjunctivePredicates(fc).span(p => p.deterministic &&
!p.throwable)
- val (cannotFail, rest) =
following.partition(ExprUtils.canEvaluateUnconditionally)
+ lazy val failable = failableOutput(grandChild)
Review Comment:
Since this reuses `failableOutput`, it also inherits the operators the
helper doesn't look through, which were raised on #60574. For example, with
`Filter(rand() > 0.1 AND q > 0, Filter(c, Limit(Project(10L / (id - 3) AS q,
...))))`, `q > 0` is still combined into the lower filter. Under whole-stage
codegen, that filter then computes `q` first.
Separately, a conjunct kept above now shares a non-deterministic `Filter`
with `rand()`. `ScanOperation` doesn't push that filter down, so a view column
like `CAST(dt AS DATE) AS d` no longer gets partition pruning from `rand() <
0.01 AND d = ...`. This is consistent with the single-filter case, but
SPARK-61823 added a legacy flag for a similar trade-off. Should this have one
too, or a line in the migration guide?
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/FilterPushdownSuite.scala:
##########
@@ -185,6 +185,28 @@ class FilterPushdownSuite extends PlanTest {
}
}
+ test("SPARK-61886: a condition over a column that can fail is not combined
ahead of a " +
+ "non-deterministic one") {
+ // `i` can fail as the cast does, and `j` as it reads `i`, also through
the sample, the
+ // projection and the filter above them. A condition over either stays
above, while one over
+ // the non-deterministic `r`, which is computed on every row, is combined.
+ val projected = testStringRelation
+ .select($"a", Cast($"e", IntegerType, evalMode = EvalMode.ANSI).as("i"),
Rand(1).as("r"))
+ .sample(0.0, 0.5, withReplacement = false, 1L)
+ .select($"a", $"i", IsNull($"i").as("j"), $"r")
+ val originalQuery = projected
+ .where($"a" > 0)
+ .where($"a" > 1)
+ .where(Rand(0) > 0.1 && $"i" > 0 && $"j" && $"r" > 0.5)
Review Comment:
Every deterministic column read after `Rand(0)` here is failable. So if
`failableOutput` treated every deterministic alias as failable, this test would
still pass. Could you add a conjunct after `Rand(0)` over a column that can't
fail? For example, the pass-through `$"a"`, or an alias such as `Cast($"a",
LongType).as("w")` carried through the same chain. The expected plan would show
it combined.
--
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]