sarutak commented on PR #60337:
URL: https://github.com/apache/spark/pull/60337#issuecomment-6821816673

   Thank you for requesting the AI review, @HyukjinKwon. Its second decision 
challenge holds when I run it, and together with a measurement I made for 
#59940, I'd like to take the direction of its third option, master's ordering, 
but close this PR and move the NULL-literal fix to a PR of its own (below). 
@dongjoon-hyun, this goes back on the direction we took from your item 1 (the 
equalities still go first, but not ahead of a non-deterministic conjunct), so 
I'd like to hear from you before I do.
   
   What has changed since your review:
   
   - I had asked to hold #59940 (SPARK-60730) until this PR is in 
(https://github.com/apache/spark/pull/59940#issuecomment-6788458335), but on 
the cases I measured it does not need it, so #59940 no longer waits for this 
PR. On top of #59940, `f >= 50 && rand(10) > 0.5 && id * a > 0 && a = 1` over 
`spark.range(0, 200, 1, 1)`, with `f` an identity Scala UDF and `a` = `id % 3` 
as an alias (ANSI mode on or off) or as a column of a Parquet file (ANSI mode 
on), gets the same plan and the same rows with this PR's head (b9fa10e822d) as 
without it. Both return 31 rows, as the written order does, but not the same 
ones, as #59940 pushes `a = 1` below the projection, ahead of `rand(10)`. The 
case of #59940 that fails, `f > 0 && i * id > 0 && i = 1` with 
`CAST_INVALID_INPUT`, is left to SPARK-61437 for whole-stage codegen; with it 
off, the query also fails with `ConstantPropagation` excluded, which #59940 is 
to look at.
   - I ran what the AI review says about the other rules. Over this PR's test 
data with ANSI mode off, `df.where($"a" > 0 && rand(10) < 0.5 && $"a" === 1)` 
returns 14 rows with this PR, the count of the written order, and 13 on master. 
But `df.where($"a" > 0).where(rand(10) < 0.5 && $"a" === 1)` becomes 
`Filter(rand(10) < 0.5, Filter(isnotnull(a) AND a = 1))` and returns 13 with 
this PR too. Over the same rows written with `partitionBy("a")`, the scan for 
`rand(10) < 0.5 && a = 1` reads only the `a=1` directory (`PartitionFilters: 
[isnotnull(a), (a = 1)]`), so `rand(10)` runs only on those rows. Meanwhile, 
with ANSI mode on, this PR makes `amount / qty > 10 AND input_file_name() LIKE 
'%parquet%' AND qty = 5` over the rows `(100, 0), (100, 5)` fail with 
`DIVIDE_BY_ZERO`, where master moves `qty = 5` first and returns `(100, 5)`; 
the same holds with `rand(10) < 0.99` in the middle. That is the cost from your 
item 1, which remains for a condition with a non-deterministic conjunct.
   
   So this PR keeps the written rows of a non-deterministic conjunct in one 
rule, while `CombineFilters`, predicate pushdown and partition pruning do not, 
and it adds failures in ANSI mode for that. The optimizer does not promise 
those rows: `CombineFilters` says "The query execution/optimization does not 
guarantee the expressions are evaluated in order" and only lets a condition 
that cannot fail go ahead of a non-deterministic one, which is all that 
master's `moveEqualitiesFirst` moves, and the migration guide says since Spark 
2.3 that "the Join/Filter's deterministic predicates that are after the first 
non-deterministic predicates are also pushed down/through the child operators". 
`Expression.deterministic` asks that the non-deterministic expressions do not 
change in number and order, and none of these rules changes their number or 
order. Released versions rewrite a conjunct before a non-deterministic one with 
a later constant, which keeps the number and order of the non-determinis
 tic expressions too, but makes `rand(10)` in `a > 0 AND rand(10) < 0.5 AND a = 
1` above see the rows with `a` = 0 too, so 4.3.0 returns 16 rows instead of 14 
with ANSI mode off. Making every rule keep the written rows would be a much 
larger change, which I don't think is worth it, so I'd rather keep master's 
behavior.
   
   If you agree, I'll close this PR, resolve SPARK-61354 as Won't Fix with this 
reasoning, and open a separate PR, with its own JIRA, as this PR and its JIRA 
are about non-deterministic conditions, for the part that still fixes 
something. With `NullPropagation` excluded and ANSI mode on, `spark.range(4, 7, 
1, 1).select($"id", lit(1) as "c").where(!($"id" === 
typedLit[java.lang.Long](null) && lit(10) / $"c" > 0 && ($"id" + 0 <=> 5)))` 
returns all three rows on master, 4.1.4 and 4.0.5: `ConstantPropagation` 
replaces `id` with NULL, and `ConstantFolding` and `BooleanSimplification` then 
make the condition TRUE, although it is NULL for id 5. The separate PR stops 
propagating an `EqualTo` with a NULL literal, with two guards in `traverse`, a 
comment on them, the two Scaladoc lines that describe them, and tests, which 
makes that query return the ids 4 and 6. With the default rules, 
`NullPropagation` removes such an equality first, so results do not change.
   
   On the rest: your items 6 to 9, and the AI review's finding about the 
Scaladoc, are about code this PR adds, which goes away when it is closed, and 
so is the AI review's first decision challenge, as no new failure remains. 
Items 3 and 5 stay with SPARK-61438, and item 4 with SPARK-61139; the cases are 
in this PR's description and in the threads above. The qualification I added to 
the `replaceConstants` Scaladoc for item 5 goes away with this PR too, so the 
sentence that an error the original condition cannot raise is not raised stays 
as it is on master until SPARK-61438 fixes it together with the code. The 
`rand` case of item 2 is no longer a goal, but SPARK-61378 stays, as #59940 
also needs it for the `isnotnull(x)` that `InferFiltersFromConstraints` puts in 
front of its division query (see its description).
   


-- 
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