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

   A follow-up to my review above, @dongjoon-hyun. I measured one more shape, 
and it changes what I suggested there: a check of the outer input in 
`DecorrelateInnerQuery`, i.e. the one-line check or the allowlist I proposed, 
cannot cover it, because when it runs, the outer input is still only a 
`Project` over `range(1000)`, which is deterministic and on the allowlist. (The 
allowlist would still cover (2)-(4) of the review.)
   
   With the same `gb`, `<sub>` and settings as in my review, and `rkf` = 
`udf((x: Long) => scala.util.Random.nextInt(100000)).asNondeterministic()`, 
this query counts the rows whose `m` is wrong, so it should return 0:
   
   ```sql
   SELECT count(*) FROM (
     SELECT * FROM (SELECT kk, <sub> m FROM (SELECT id kk FROM range(1000)) o) v
     WHERE rkf(kk) > 50000) x
   WHERE m IS NULL OR m <> kk
   ```
   
   Master returned 0, this PR returned 254, and this PR with the one-line check 
returned 257. When `PullupCorrelatedPredicates` decorrelates the subquery, the 
filter on `rkf(kk)` is still above the `Project` that holds the subquery, so 
the outer input of that `Project` is deterministic and both checks accept it. 
`PushDownPredicates` pushes the filter below that `Project` later, before 
`RewriteCorrelatedScalarSubquery` builds the domain joins. The counts fit the 
domain being built from a second evaluation of `rkf(kk)` (about 250 of the 
about 500 kept rows expected).
   
   My PR #60296 (SPARK-60501), which you are reviewing, has the same problem to 
solve: it also routes more correlated scalar subqueries to the domain joins of 
`DecorrelateInnerQuery`, and it has to reject the outer inputs that give other 
rows when the domain joins compute them again. Its current head 18ef1084088 has 
these checks, in `PullupCorrelatedPredicates` (`subquery.scala`) except the 
last one, which is in `LimitPushDown`:
   
   - `repeatableInput`: an allowlist of outer inputs that give the same rows 
again. It accepts only known operators with deterministic expressions, 
aggregate functions whose result does not depend on the row order, and 
relations other than a `LogicalRDD` and a streaming source, and checks the 
plans of the subqueries in the input too. It rejects anything else, e.g. a 
`Sample` or a CTE reference. Unlike the allowlist in my review, it takes any 
other relation, e.g. DSv2 or JDBC, to give the same rows again, as Spark does 
elsewhere, e.g. in a self-join.
   - `sameWhenEqual` and `keyStable`: they reject outer columns 
(`sameWhenEqual`) and grouping keys or `DISTINCT`, `INTERSECT` and `EXCEPT` 
rows in the outer input (`keyStable`) of a type whose values that compare equal 
can differ, such as -0.0 and 0.0, a non-binary collation, or a UDT over them 
(`keyStable` lets plain floating-point keys through, since 
`NormalizeFloatingNumbers` normalizes them).
   - `mayMoveIntoOuterInput`: whether the rules that run before 
`RewriteCorrelatedScalarSubquery`, e.g. `PushDownPredicates`, `ReorderJoin` and 
`CollapseProject`, may still move something that gives other rows into the 
outer input, as `PushDownPredicates` does with the filter on `rkf(kk)` above. 
It needs the whole plan that `PullupCorrelatedPredicates` runs on, which 
`DecorrelateInnerQuery` does not see. It is conservative: it falls back for any 
non-deterministic filter or join condition anywhere in the plan.
   - `DomainJoin.keepLimitAbove`: `LimitPushDown` does not push a `LIMIT` 
through a `Project` into the join below it, or a `LIMIT 1` into a group-only 
`Aggregate` below it, when a scalar subquery of the `Project` has a domain join 
that #60296's new routing marked with `keepLimitAbove`, so that the domain is 
not built from a limited copy of the outer input. (In the one `LIMIT` shape I 
tried here, a `LIMIT 10` over a `LEFT JOIN` outer input, this PR gave 0 wrong 
rows and the same number of `LocalLimit`s as master, so I don't know whether 
this PR needs it.)
   
   Today these checks only decide whether #60296's own new routing applies, so 
they do not change the queries in my reviews here. By reading the code, if they 
ran on these queries, they would reject (1)-(4) of my review and the 
pushed-down filter above (I have not run them): (1) and the filter above 
because `mayMoveIntoOuterInput` falls back for any non-deterministic filter, 
and (2)-(4) because `repeatableInput` rejects a CTE reference, a `Sample` and a 
`LogicalRDD`. The non-deterministic outer input and the UDT over a DOUBLE from 
my earlier reviews are already handled in this PR. #60296 runs these checks 
only for scalar subqueries, so for the `IN` and `EXISTS` subqueries that this 
PR changes, such as (1), they would have to run there too. 
`RewritePredicateSubquery` builds their domain over the filter's conjuncts 
without a subquery and the `IN` and `EXISTS` conjuncts written before them in 
the same filter, already rewritten into the plan, as in (1); I have not checked 
whether that ne
 eds anything beyond these checks.
   
   The checks in this PR (`outerIsRepeatable` and `canBeInDomain`) answer the 
same question for the outer input. To avoid writing and reviewing the same 
checks twice, how would you like to proceed?
   
   - (A) This PR uses the checks of #60296. `PullupCorrelatedPredicates` would 
tell `DecorrelateInnerQuery` whether the outer input can be computed again, and 
this PR would keep the checks on the outer columns that the kept conjuncts read 
(which could call `sameWhenEqual` of #60296) and what is specific to it, e.g. 
`equatedBelow`. This PR would then have to wait for #60296. I can help with the 
change, and I would run the probes from both PRs on this PR.
   - (B) Keep the checks separate, and this PR covers the shape above in its 
own way.
   
   I would rather not make #60296 depend on this PR the other way around: as 
its description says, separate backport PRs for `branch-4.3`, `branch-4.2`, 
`branch-4.1` and `branch-4.0` will follow it, and each of them would then need 
this PR, or a copy of its checks, in that branch first. (A) has the same cost 
for the backports of this PR, so (B) is fine if you would rather not wait for 
#60296.
   
   Either works for me.


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