sarutak opened a new pull request, #60351:
URL: https://github.com/apache/spark/pull/60351

   ### What changes were proposed in this pull request?
   
   The projection split of SPARK-55014 
(`spark.sql.optimizer.splitProjectionForExpensiveFilters`, default `true`) 
stacks the conditions of a `Filter` over a `Project` with expensive aliases 
(UDFs, expensive regexes) into layers, each computing only the aliases its 
conditions need, so an expensive alias only runs on the rows the layers below 
it kept. It placed the conditions by cost: the group of conditions needing the 
fewest aliases not computed yet went lowest. So it could compute an expensive 
alias, such as a UDF, ahead of the conditions written before the condition that 
reads it, on rows those conditions would have filtered out, and evaluate a 
condition ahead of the ones written before it.
   
   This PR places the conditions in the order they are written: each layer 
computes the aliases the next condition needs and filters on it and, when it is 
deterministic, on the deterministic conditions right after it that only need 
aliases computed already. Here a condition needs the aliases it reads and the 
aliases that are not throwable and share expensive work with them, directly or 
through each other; a throwable alias is needed only by a condition that reads 
it. The split still only adds a layer while it leaves an expensive alias for a 
later one, but now it ends at the first condition that needs every expensive 
alias not computed yet, and the conditions after it are left above the 
projection too (a throwable alias not computed yet is among those aliases, so a 
condition that does not read it does not end the split); before, the split 
skipped that condition and went on placing the ones after it. Aliases sharing 
an expensive sub-expression are still computed together, which is the 
 only case where a layer of the split computes an alias ahead of the conditions 
written before the first condition reading it. This PR keeps a throwable alias 
from being pulled in this way, although, when the split places the first 
condition reading a throwable alias, that condition still pulls in those of the 
aliases sharing its work that are not throwable and not computed yet. An alias 
that can fail without reporting `throwable`, such as a UDF or, in ANSI mode, a 
division by zero, is still pulled into the layer of an earlier condition by an 
alias sharing its expensive work, so it can still be computed ahead of the 
conditions written before its first reader. As without the split, the 
projection on top still computes the aliases of the conditions left above it.
   
   In the examples of this description, unless they say otherwise, `f`, `g`, 
`h`, `j` and `n` are expensive aliases (e.g. UDFs or expensive regexes) of the 
projection under the filter, and `a` is a column of its child. As the split no 
longer evaluates a condition ahead of the ones written before it, 
non-deterministic and throwable conditions (in this description, a throwable 
condition includes one over a throwable alias), which it left in the top filter 
until now (SPARK-55014, SPARK-60602), are now placed in their written position 
too. For example, in `(rand(10) > 0.5 OR j) AND (f OR g)` the split computed 
`f` and `g` and filtered on `f OR g` below the non-deterministic condition; now 
the non-deterministic condition is evaluated first, as written. A 
non-deterministic condition the split places gets a filter of its own, which 
neither the conditions before it nor the ones after it join. When the split 
places at least one condition, this also holds for a non-deterministic 
condition left
  above the projection, either as the condition that ends the split or after 
that condition: in a shared filter, a later pass would push it below the 
conditions before it, or move the ones after it ahead of it, as cheap 
conditions over the aliases computed below. For example, in `f AND g != j AND 
(rand(10) > 0.5 OR f)`, where `g != j` ends the split, on master a later pass 
moved `rand(10) > 0.5 OR f` below the projection computing `g` and `j`, ahead 
of `g != j`, and in `n > 1 AND (rand(10) > 0.5 OR g != j) AND n < 10`, where, 
with this PR, the non-deterministic condition ends the split, master placed `n 
< 10` ahead of it, in the filter of `n > 1`, as a later pass would do in one 
top filter. When the split places nothing, the conditions left above stay in 
one filter, as with the split off.
   
   Later passes can still move a deterministic condition that is not throwable 
ahead of some of the conditions written before it: one that only reads aliases 
computed in a lower layer is cheap there, and this rule pushes it, as it does 
any cheap condition that is not throwable, no lower than the filter of the 
highest layer that computes an expensive alias it reads, but not past the 
filter of a non-deterministic condition. This includes a deterministic 
condition that calls a UDF that can fail: over `spark.range(0, 10, 1, 
1).select($"id", f($"id") as "f", h($"id") as "h", k($"id") as "k")`, with `f`, 
`h` and `k` no-op UDFs and `req` a UDF that requires its input to be greater 
than 5, `req(f) > 0` in `f >= 0 AND h > 5 AND req(f) > 0 AND k >= 0` is pushed 
below `h > 5`, so the query fails with the split on and passes with it off, 
with and without whole-stage codegen, as it does on master. The conditions that 
can fail are the subject of SPARK-60730. As before, and as with the split off, a
  condition over no expensive alias, deterministic or not, is pushed below 
every expensive condition in the first pass, unless it is throwable and an 
expensive condition is written before it: e.g. `rand(10) > 0.5` in `f AND 
rand(10) > 0.5`, and `a > 1` in `(rand(10) > 0.5 OR f) AND a > 1`.
   
   ### Why are the changes needed?
   
   With `f` and `h` no-op UDFs and `g` a UDF that requires its input to be 
greater than 5:
   
   ```scala
   val f = udf((i: Long) => i)
   val h = udf((i: Long) => i)
   val g = udf((i: Long) => { require(i > 5); i })
   spark.range(0, 10, 1, 1)
     .select($"id", f($"id") as "f", h($"id") as "h", g($"id") as "g")
     .where($"f" + $"h" > 10 && $"g" > 0)
     .collect()
   ```
   
   | Whole-stage codegen | Split on, before | Split on, after | Split off |
   |---|---|---|---|
   | on (default) | `FAILED_EXECUTE_UDF` | OK (4 rows) | OK (4 rows) |
   | off | `FAILED_EXECUTE_UDF` | OK (4 rows) | `FAILED_EXECUTE_UDF` |
   
   `g > 0` needs one alias and `f + h > 10` two, so the split computed `g` 
first, on every row. Now `f` and `h` are computed and filtered on first, and 
`g` only on the 4 rows left. Without whole-stage codegen and with the split 
off, the single interpreted projection computes every alias on every row, so 
that case still fails, as before.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No, compared with the released versions: the projection split (SPARK-55014) 
is on master and branch-4.x only, not in a release. Compared with master and 
branch-4.x:
   
   - The split places the conditions of a filter in their written order rather 
than by the number of aliases they need, so queries like the one above no 
longer fail, and the expensive aliases of a condition written early run on more 
rows than when a cheaper condition written after it was placed first. If such 
an alias fails on some of those rows, the query now fails, as with the split 
off: e.g. with the UDFs above, `g + f > 0 AND h > 5` passed with the split on, 
with and without whole-stage codegen, and now fails.
   - Non-deterministic and throwable conditions are placed in their written 
position too, and a filter whose expensive conditions are all non-deterministic 
or throwable, which was not split, can now be split. The split could place the 
conditions written after such a condition below it. Now the expensive aliases 
such a condition reads run before the conditions written after it, also on the 
rows they would have dropped, and if such an alias fails there, the query now 
fails, as with the split off. After a throwable condition, a later pass can 
still move a deterministic, non-throwable condition that only reads aliases 
computed below ahead of it, as described above. For example, with the UDFs 
above and `rand(10) > 2`, which is never true, `(rand(10) > 2 OR g > 0) AND f + 
h > 10` and `f >= 0 AND (rand(10) > 2 OR g > 0) AND h > 5` passed with the 
split on, with and without whole-stage codegen, and now fail.
   - Among the conditions over an expensive alias, one written after a 
non-deterministic condition is no longer evaluated ahead of it, and a 
non-deterministic one is no longer evaluated ahead of the conditions written 
before it, by the split or by a later pass; a condition over no expensive alias 
is still pushed below them, as above. So a seeded `rand()` can pick other rows 
than before. For example, over `spark.range(0, 100, 1, 1)` with no-op UDFs `f`, 
`h` and `k`, `f >= 20 AND h + k >= 60 AND (rand(10) > 0.5 OR f < 0)` returned 
42 rows with the split on and 45 with it off, and now returns 45 with both.
   - When the condition that ends the split is non-deterministic, the expensive 
aliases it needs now also run on the rows that a deterministic condition after 
it, reading only aliases computed below, would have dropped first, as that 
condition is no longer moved below it: `g` and `j` now run before `n < 10` in 
`n > 1 AND (rand(10) > 0.5 OR g != j) AND n < 10`. Likewise when a 
non-deterministic condition that reads only aliases computed below comes after 
the condition that ends the split: `g` and `j` now run before `rand(10) > 0.5 
OR f` in `f AND g != j AND (rand(10) > 0.5 OR f)`.
   - A condition that needs every expensive alias not computed yet, counting 
the aliases that are not throwable and share expensive work with the ones it 
reads, directly or through each other, now ends the split, so the conditions 
written after it are no longer split off below it. For example, in `f AND g != 
j AND j`, with `f`, `g` and `j` the only expensive aliases, `j` is no longer 
computed and filtered on below `g != j`. And `f != g AND f`, when `f` and `g` 
are the only expensive aliases and cannot be null, is no longer split, so `g` 
now runs on every row; when they can be null, the optimizer infers `isnotnull` 
conditions and places them first, and the first of them, which needs only one 
of the two aliases, still splits it.
   - A throwable condition is no longer kept above every condition the split 
places, so it can raise on rows that the conditions written after it, which the 
split used to place below it, would have filtered out.
   - A throwable alias is no longer pulled into the layer of an earlier 
condition by an alias sharing its expensive work, so that work can run twice, 
once below the filter of the condition that reads the other alias and again 
above it, and the throwable alias only runs on the rows that the conditions 
written before the first condition reading it kept, or in the projection on top 
when the split does not place that condition or no condition reads it. When the 
split decides whether it ends at a condition, the condition now needs a 
throwable alias only when it reads it. So a filter such as `f AND t`, or just 
`f`, is now split where it was not before, when the only expensive aliases of 
the projection are `f` and a throwable alias `t` sharing its work: `f` is 
computed and filtered on first, and `t` above it. For example, over 
`spark.range(0, 10, 1, 1).select($"id", gt5($"id") as "f", t as "t")`, with 
`gt5` a UDF returning `id > 5` and `t` = `CASE WHEN gt5(id) THEN true ELSE 
raise_error('bo
 om') IS NULL END`: without whole-stage codegen, both filters failed with the 
split on and off, and now pass with it on (and still fail with it off); with 
whole-stage codegen, they pass either way, and with the split on `gt5` now runs 
14 times instead of 10, as the shared part runs again on the rows `f` keeps. 
Without whole-stage codegen, this is what keeps `t` from raising on the rows 
`f` drops, with the split on; with codegen, which already defers `t` past the 
filter, it only adds the second run of the shared part. The optimizer cannot 
know in advance that codegen will cover the operator, so it pays that cost in 
both cases.
   
   ### How was this patch tested?
   
   `FilterPushdownSuite`: the tests that expected the cost order, or that 
expected non-deterministic and throwable conditions to stay in the top filter, 
now expect the written order: `the split follows the written order, not the 
cost` (was `the least demanding condition goes lowest`), `elements sharing 
expensive work are split off together`, `a non-deterministic condition is split 
off in its written position` (was `... is not split off into a lower layer`), 
`a single non-deterministic condition is split off too` (was `no split when the 
expensive conditions are non-deterministic`), and `a throwable condition is 
split off in its written position` (was `a throwable condition does not keep 
later conditions from being split off`). `all the conditions over the same 
elements are applied together` keeps its plan and is renamed to `all the 
conditions over the same aliases written one after the other are applied 
together`, as the split now puts two such conditions in one filter only when 
both 
 are deterministic and no condition that needs a new alias, nor a 
non-deterministic one, is written between them; a later pass can still move one 
that is not throwable into the earlier filter, as `n != 5` in `a throwable 
condition over computed aliases joins the filter of the condition written right 
before it, not an earlier filter over the same aliases` shows. The tests this 
PR renames get the `SPARK-60731` prefix. The SPARK-60602 tests of a throwable 
condition, a condition over a throwable alias, and the conditions left above 
the projection keep their plans, with comments that no longer refer to the cost 
order or to keeping them out of the split; the one of a throwable condition now 
also checks the order of the top filter. The one of a non-deterministic 
condition left above the projection now writes it after `f != g`, which needs 
every expensive alias not computed yet: written first, as before, it is now 
placed by the split, which `a non-deterministic or throwable condition written
  first does not keep the later conditions from being split off` covers. New 
tests:
   
   - `a throwable condition over computed aliases joins the filter of the 
condition written right before it, not an earlier filter over the same 
aliases`, also in the order of the conjuncts within each filter, which 
`comparePlans` does not check.
   - `a non-deterministic condition shares no filter with the conditions before 
or after it`; `a non-deterministic condition is split off in its written 
position` checks that the conditions after it do not join its filter when it 
computes an alias.
   - `a non-deterministic condition whose layer only computes a cheap alias is 
split off in its written position`, after a later pass pushes it below its 
projection.
   - `a later pass does not push a condition past a non-deterministic filter 
the split placed`.
   - `a non-deterministic condition left above the split filters on its own`: 
the two examples above, `f AND g != j AND (rand(10) > 0.5 OR f)` and `n > 1 AND 
(rand(10) > 0.5 OR g != j) AND n < 10`, and two non-deterministic conditions 
left above.
   - `a condition needing every expensive alias not computed yet ends the 
split`: `f AND g != j AND j`, also with a throwable condition after `j`, and 
with a throwable condition over the `f` computed below instead.
   - `a layer that needs no new alias is a filter alone`, for a 
non-deterministic condition and for the condition after it, after one 
application of the rule.
   - `a non-deterministic or throwable condition written first does not keep 
the later conditions from being split off`.
   - `a throwable alias is not pulled into the layer of an earlier condition by 
an alias sharing its expensive work`; when a condition reads it before that 
alias, it pulls that alias in; it does not pull in another throwable alias 
sharing its work; and it is not pulled in through an alias that is not 
throwable, also when a throwable alias read first pulled that alias in.
   - `a throwable alias sharing work with the aliases a condition reads does 
not make that condition end the split`, with it as the only other expensive 
alias, whether a condition reads it or not.
   - `a throwable condition over no alias joins the filter of the condition 
before it`.
   - `a Python UDF shared with a throwable alias read later runs in both 
layers`.
   - `a throwable condition over a cheap alias is split off in its written 
position`.
   
   `DataFrameSuite`: the query above returns the 4 rows in three 
configurations: split on, with and without whole-stage codegen, and split off 
with whole-stage codegen. Without whole-stage codegen and with the split on, 
`g` is called for those 4 rows only. Without codegen and with the split off, 
the query fails in `g`, as the table shows. On master, the test fails. `a 
throwable alias sharing work with an earlier condition's alias is not computed 
ahead of its first reader`: without codegen, `f AND t` with the throwable `t` 
of the user-facing section returns its 4 rows with the split on and raises 
`USER_RAISED_EXCEPTION` with it off. Six filters with a non-deterministic 
condition are split with the split on and not with it off, and return the same 
rows with it on and off. The non-deterministic condition is (1) written first; 
(2) written after two conditions and reading only the alias of the first, and 
(3) the same with a condition after it that only reads computed aliases; (4) 
written 
 after one condition and reading a new alias, with a condition after it that 
only reads computed aliases; (5) left above the split after the condition that 
ends it; and (6) ending the split, with a condition after it that only reads 
computed aliases. On master, all but (2) return other rows with the split on 
than with it off. In (2) and (3), the non-deterministic condition needs no new 
alias, so it would join the filter before it if the split allowed that; in (5) 
and (6), a later pass would swap it with another condition if they shared the 
top filter.
   
   Each of these mutants fails at least one `FilterPushdownSuite` test (the one 
marked * makes the suite abort with a `StackOverflowError`):
   
   - grouping the conditions a layer can evaluate from anywhere in the filter;
   - ordering them by the number of aliases they need;
   - splitting even when the next condition needs every expensive alias not 
computed yet (*);
   - leaving non-deterministic and throwable conditions out of the split again, 
or ending the split at a non-deterministic or throwable condition;
   - letting a non-deterministic condition join the filter of the conditions 
before it;
   - letting the deterministic conditions after a non-deterministic one join 
its filter, always or only when it computes an alias;
   - making a condition that reads no alias get a filter of its own instead of 
joining the one before it;
   - stopping the split only when the next condition needs every expensive 
alias, computed or not;
   - skipping the condition that ends the split to go on placing the ones after 
it;
   - giving a layer that needs no new alias a projection, which only the test 
applying the rule once catches, as later passes remove it;
   - not moving aliases that share expensive work together, letting an earlier 
condition's alias pull in a throwable alias sharing its work, directly or 
through another alias it pulls in, keeping a throwable alias read first from 
pulling in the aliases sharing its work, letting it pull in the throwable ones 
too, or counting a throwable alias for a condition that does not read it when 
deciding whether the split stops;
   - reversing the conjuncts of a filter;
   - letting `CombineFilters` merge a non-deterministic condition into the 
filter below it, or a filter into a non-deterministic filter below it;
   - leaving the conditions the split does not place in one top filter again, 
or splitting it at the non-deterministic conditions also when the split places 
nothing;
   - letting a non-deterministic condition left above join the filter of the 
conditions before it, a condition left above join the filter of a 
non-deterministic one before it, or two non-deterministic ones share a filter;
   - giving every condition left above a filter of its own.
   
   Seventeen of these mutants were also run against the two SPARK-60731 
`DataFrameSuite` tests. One of them fails under each of: grouping from anywhere 
in the filter, ordering by the number of aliases, leaving non-deterministic and 
throwable conditions out of the split or ending the split at one, letting a 
non-deterministic condition join the filter before it, letting the conditions 
after a non-deterministic one join its filter (both variants), skipping the 
condition that ends the split, leaving the conditions not placed in one top 
filter, letting a non-deterministic condition left above join the filter before 
it or a condition left above join its filter, letting an earlier condition's 
alias pull in a throwable alias sharing its work, and counting a throwable 
alias for a condition that does not read it. Both pass with two 
non-deterministic conditions left above sharing a filter, as none of their 
queries has two, with every condition left above in a filter of its own, which 
keeps thei
 r order, and with the two mutants that need a chain of aliases sharing work or 
a throwable alias read first, which none of their queries has.
   
   All tests in `org.apache.spark.sql.catalyst.optimizer`, `DataFrameSuite`, 
the TPC-DS and TPC-H `PlanStabilitySuite`s, `WholeStageCodegenSuite`, 
`ExtractPythonUDFsSuite`, `BatchEvalPythonExecSuite`, 
`FileSourceStrategySuite`, `PruneFileSourcePartitionsSuite` and 
`StreamingQueryManagerSuite` pass, also `FilterPushdownSuite` and 
`DataFrameSuite` with ANSI mode off, with no golden plan changes.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Kiro CLI 3.1.0 (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