sarutak commented on code in PR #60764:
URL: https://github.com/apache/spark/pull/60764#discussion_r4660738154


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +135,89 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated, e.g. under 
an Aggregate, a
+   * Window, a Distinct, a Limit or an Offset, or used by a set operation, in 
their written order,
+   * into the ones moved into a filter below, the ones kept in the filter and 
the others, which are
+   * decorrelated as before.
+   *
+   * A correlated conjunct used as a join condition with the outer query is 
evaluated after the
+   * filter, or ahead of it once pushed into the outer query, and the 
conjuncts left in the filter
+   * read the inner column that such an equality equates to an outer column. 
So when a conjunct
+   * may not be evaluated ahead of the others (see `splitMovable`, which takes 
an outer reference
+   * as a column), it and the ones written before it are kept in the filter. 
They read the domain
+   * attributes of their outer columns, so they are evaluated in their written 
order on the values
+   * of each outer row. When they read outer columns, the correlated conjuncts 
written after them
+   * that read an inner column are kept too, so that the equalities among them 
become keys of the
+   * domain join. The uncorrelated conjuncts that may move are moved below, 
where they can still be
+   * pushed into a scan.
+   *
+   * The domain reads the outer query once more, which gives other rows for 
some outer queries,
+   * e.g. a non-deterministic or sampled one. So only a filter that has a 
domain join anyway keeps
+   * its conjuncts in place: one used by a set operation, or one with an outer 
column that a parent
+   * reads or that a correlated conjunct reads that can not be pulled up over 
an Aggregate. The
+   * domain also groups the outer rows by its columns and joins them back with 
`<=>`. So an outer
+   * column that a kept conjunct reads must be orderable, its equal values 
must be identical,
+   * unlike a string of a non-binary collation or a floating-point value, also 
in a user-defined
+   * type, and no filter or join below may equate it to an inner column, which 
would stand for it
+   * instead. Otherwise nothing is kept, unless the filter is used by a set 
operation, where the
+   * domain has every outer column anyway. A correlated conjunct written after 
the kept ones that
+   * reads an outer column that can not be in the domain is decorrelated as 
before.
+   *
+   * TODO: Keep the written order also for a filter without a domain join, 
e.g. one whose
+   * correlated conjuncts are all equalities, where the outer query must be 
one that returns the
+   * same rows again, for these outer columns, for a filter whose results are 
not aggregated, e.g.
+   * in an EXISTS subquery without a Limit or a set operation, for the 
condition of a join in the
+   * subquery, across operators, e.g. for a throwable conjunct in a filter 
above another one with a
+   * correlated equality, and for an outer column that the outer query 
computes with an expression
+   * that can fail.
+   */
+  private def splitKeptInPlace(
+      conditions: Seq[Expression],
+      child: LogicalPlan,
+      parentOuterReferences: AttributeSet,
+      underSetOp: Boolean): (Seq[Expression], Seq[Expression], 
Seq[Expression]) = {
+    val stayUp = splitMovable(conditions.map(stripOuterReference), 
Seq(child))._2.toSet
+    def mayNotMove(cond: Expression): Boolean = 
stayUp.contains(stripOuterReference(cond))
+    val (prefix, rest) = 
conditions.splitAt(conditions.lastIndexWhere(mayNotMove) + 1)
+    lazy val hasDomainJoin = underSetOp || {
+      val correlated = conditions.filter(containsOuter)
+      (parentOuterReferences ++ 
collectOuterReferences(correlated.filterNot(canPullUpOverAgg)) --
+        collectEquivalentOuterReferences(correlated).keySet).nonEmpty
+    }

Review Comment:
   This computes master's domain columns from this filter only, but on master a 
filter or an inner join below also removes the outer columns that its 
equalities (`OuterReference = Attribute`, without a cast) equate, before the 
leaf builds the `DomainJoin`. So this can be true where master builds no domain 
join, e.g. for `(SELECT max(s.v) FROM (SELECT * FROM gb WHERE gb.k = o.t) s 
WHERE <guard> AND 1 / (o.k + 1) > 0 AND s.v >= o.t)` over `(SELECT CAST(id AS 
INT) t, rk() k FROM range(1000)) o`, with `m` compared with `t`: this filter 
passes `t` down for `s.v >= o.t`, but the filter below equates `t` to `gb.k`. 
Master's plan has only the left outer join, and it returned 0 wrong rows; this 
PR adds a domain join over `k` with no join condition and returned 990 (without 
the guard, both 0 with master's plan). Could the gate also subtract 
`equatedBelow`, or require `equatedBelow` to be empty (simpler, but it also 
falls back where master still builds a domain join over the other outer columns
 )?
   



##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuerySuite.scala:
##########
@@ -210,6 +212,124 @@ class DecorrelateInnerQuerySuite extends PlanTest {
     check(innerPlan, outerPlan, correctAnswer, Seq(x <=> x))
   }
 
+  test("SPARK-62142: keep the predicates up to the last one that may not move 
in the filter") {

Review Comment:
   nit: no test fails when 
`collectOuterReferences(correlated.filterNot(canPullUpOverAgg))` in 
`hasDomainJoin` becomes `collectOuterReferences(correlated)` (I ran that 
mutant). A plan case in this test (which runs under ANSI mode) with 
`Filter(guard && Literal(1) % OuterReference(x) === 0, testRelation)` under the 
`Aggregate`, where master pulls `1 % x = 0` up and builds no domain, expecting 
nothing to be kept, would catch it.
   



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