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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -622,17 +681,21 @@ object DecorrelateInnerQuery extends PredicateHelper {
                 if (underSetOp) (Seq.empty[Expression], correlated)
                 else correlated.partition(canPullUpOverAgg)
               val outerReferences = collectOuterReferences(predicates)
-              val newOuterReferences =
-                parentOuterReferences ++ outerReferences -- equivalences.keySet
-              val (newChild, joinCond, outerReferenceMap) =
-                decorrelate(child, newOuterReferences, aggregated, underSetOp)
+              // The kept conjuncts read the domain attributes of their outer 
columns, also when an
+              // equality used as a join condition equates such a column to an 
inner one.
+              val newOuterReferences = (parentOuterReferences ++ 
outerReferences --
+                equivalences.keySet) ++ collectOuterReferences(keptInPlace)
+              val (newChild, joinCond, outerReferenceMap) = decorrelate(
+                movedBelow.reduceOption(And).map(Filter(_, 
child)).getOrElse(child),
+                newOuterReferences, aggregated, underSetOp)
               // Add the outer references mapping collected from the equality 
conditions.
               val newOuterReferenceMap = outerReferenceMap ++ equivalences
               // Replace all outer references in the non-equality predicates.
               val newCorrelated = replaceOuterReferences(predicates, 
newOuterReferenceMap)
+              val newKeptInPlace = replaceOuterReferences(keptInPlace, 
outerReferenceMap)

Review Comment:
   This replaces the kept conjuncts through `outerReferenceMap`, which comes 
from the child. When a lower operator has a correlated equality on a kept 
column, the child maps that column to the inner column, and no domain attribute 
is built for it. Then a kept conjunct that reads the outer column reads the 
inner one instead, and it is evaluated on rows that the equality would remove. 
With `<guard>` = `CASE WHEN g < 0 THEN raise_error('negative g') ELSE true 
END`, `t1(k) = (1)`, `t2(k, id, g) = (0, 1, 1)` and `t3(id) = (1)` under ANSI 
mode:
   
   ```sql
   SELECT * FROM t1 WHERE (SELECT max(t2.g) FROM t2 JOIN t3 ON t2.k = t1.k AND 
t2.id = t3.id
     WHERE CASE WHEN g < 0 THEN raise_error('negative g') ELSE true END AND 1 / 
t1.k > 0) IS NOT NULL
   ```
   
   Master returns no row, and this PR fails with DIVIDE_BY_ZERO, because `1 / 
t1.k` becomes `1 / t2.k` and runs on the row with `t2.k = 0`. The same happens 
with the equality in a derived table, `FROM (SELECT * FROM t2 WHERE t2.k = 
t1.k) s WHERE <guard> AND 1 / t1.k > 0`, and with two `where` calls in the 
DataFrame API, with `guard` holding the `<guard>` SQL text: 
`spark.table("t1").where(spark.table("t2").where(col("k") === 
col("t1.k").outer()).where(expr(guard) && (lit(1) / col("t1.k").outer() > 
0)).select(max("g")).scalar().isNotNull)`. After `INSERT INTO t2 VALUES (-1, 1, 
1), (1, 1, 5)`, the query below, whose guard reads only the outer column, fails 
with USER_RAISED_EXCEPTION, where master returns `(1)`: the guard is rewritten 
to read `t2.k` and raises on the row with `t2.k = -1`.
   
   ```sql
   SELECT * FROM t1 WHERE (SELECT max(t2.g) FROM t2 JOIN t3 ON t2.k = t1.k AND 
t2.id = t3.id
     WHERE CASE WHEN t1.k < 0 THEN raise_error('neg k') ELSE true END) IS NOT 
NULL
   ```
   
   The TODO says that the case across operators is not covered, but this case 
does not behave as on master: it fails where master returns the right answer. 
The earlier review's recommended change for this replacement (on 6ff4bf6dad2) 
also asked to fall back when the child maps a kept column to an inner attribute 
because a filter below equates it, as in the derived-table and DataFrame cases 
here; an equality in an inner join's ON condition puts the column into the same 
`outerReferenceMap` through the Join case. Your reply says that the kept 
conjuncts now read only the domain attributes of their outer columns, but here 
`1 / t1.k` still reads `t2.k`. Could you return `(Nil, Nil, conditions)` when a 
`Filter` or an inner `Join` in the child's subtree equates a kept column to an 
inner column? `splitKeptInPlace` runs before the child is decorrelated, so the 
map itself is not available there. Could you also add the queries above as 
tests?
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +134,59 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated or used by 
a set operation, in
+   * their written order, into the ones moved below its domain join, 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, 
and so are the
+   * correlated ones written after them that read their outer columns. They 
read the domain
+   * attributes of those columns, so they are evaluated in their written order 
on the values of
+   * each outer row. The uncorrelated conjuncts that may move are moved below 
the domain join,
+   * where they can still be pushed into a scan.
+   *
+   * Nothing is kept when the domain can not stand for the outer columns that 
the kept conjuncts
+   * read. It groups the outer rows by them and joins them back with `<=>`, so 
each column must be
+   * orderable, its equal values must be identical, unlike a string of a 
non-binary collation or a
+   * float that can be -0.0, and the outer query must not be streaming.
+   *
+   * TODO: Keep the written order also for these outer columns, for a filter 
whose results are not
+   * aggregated, e.g. in an EXISTS subquery, 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,
+      outerPlan: LogicalPlan): (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)
+    val prefixOuterReferences = collectOuterReferences(prefix)
+    val (onKeptColumns, others) =
+      
rest.partition(collectOuterReferences(_).exists(prefixOuterReferences.contains))
+    val keptInPlace = prefix ++ onKeptColumns
+    val domainAttributes = collectOuterReferences(keptInPlace)
+    val canBuildDomain = (domainAttributes.isEmpty || !outerPlan.isStreaming) 
&&
+      domainAttributes.forall { a =>
+        RowOrdering.isOrderable(a.dataType) && 
UnsafeRowUtils.isBinaryStable(a.dataType) &&
+          !NormalizeFloatingNumbers.needNormalize(a.dataType)

Review Comment:
   The check added for the earlier -0.0 item does not look into the `sqlType` 
of a UDT: `UnsafeRowUtils.isBinaryStable` returns true and 
`NormalizeFloatingNumbers.needNormalize` returns false for any UDT, so a UDT 
that holds a DOUBLE passes it. I used a UDT with `sqlType = 
ArrayType(DoubleType, false)` and an outer view `u1(id, p) = (1, P(-0.0)), (1, 
P(0.0))`, with `u2(id, g) = (1, 5), (2, 6)` and 
`spark.sql.autoBroadcastJoinThreshold=-1` (AQE off):
   
   ```sql
   SELECT u1.id, (SELECT max(g) FROM u2 WHERE u2.id = u1.id AND
     CASE WHEN u1.p IS NULL THEN raise_error('n') ELSE true END) m FROM u1
   ```
   
   Master returns `(1, 5)` twice, and this PR returns it four times. I think 
the domain keeps -0.0 and 0.0 as two groups and the sort merge join back to the 
outer query compares them as equal; with a broadcast hash join, which compares 
the keys' bytes, the rows were right, which fits that. The same happens with 
the guard written first, where the domain join is a CartesianProduct. A plain 
DOUBLE column falls back as intended. Could you check the UDT's `sqlType`, e.g. 
by unwrapping UDTs recursively before these checks?
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +134,59 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated or used by 
a set operation, in
+   * their written order, into the ones moved below its domain join, 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, 
and so are the
+   * correlated ones written after them that read their outer columns. They 
read the domain
+   * attributes of those columns, so they are evaluated in their written order 
on the values of
+   * each outer row. The uncorrelated conjuncts that may move are moved below 
the domain join,
+   * where they can still be pushed into a scan.
+   *
+   * Nothing is kept when the domain can not stand for the outer columns that 
the kept conjuncts
+   * read. It groups the outer rows by them and joins them back with `<=>`, so 
each column must be
+   * orderable, its equal values must be identical, unlike a string of a 
non-binary collation or a
+   * float that can be -0.0, and the outer query must not be streaming.
+   *
+   * TODO: Keep the written order also for these outer columns, for a filter 
whose results are not
+   * aggregated, e.g. in an EXISTS subquery, 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,
+      outerPlan: LogicalPlan): (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)
+    val prefixOuterReferences = collectOuterReferences(prefix)
+    val (onKeptColumns, others) =
+      
rest.partition(collectOuterReferences(_).exists(prefixOuterReferences.contains))
+    val keptInPlace = prefix ++ onKeptColumns
+    val domainAttributes = collectOuterReferences(keptInPlace)
+    val canBuildDomain = (domainAttributes.isEmpty || !outerPlan.isStreaming) 
&&
+      domainAttributes.forall { a =>
+        RowOrdering.isOrderable(a.dataType) && 
UnsafeRowUtils.isBinaryStable(a.dataType) &&
+          !NormalizeFloatingNumbers.needNormalize(a.dataType)
+      }
+    if (prefix.isEmpty || !canBuildDomain) {

Review Comment:
   nit: under a set operation, master already puts every outer reference of the 
set operation's subtree into the domain (`collectOuterReferencesInPlanTree(s)` 
in the `Union` / `SetOperation` case). So when `canBuildDomain` is false there, 
falling back only keeps the reordering, and no domain join is avoided. Could 
the check apply only when `underSetOp` is false, by passing it to 
`splitKeptInPlace`?
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +134,59 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated or used by 
a set operation, in
+   * their written order, into the ones moved below its domain join, the ones 
kept in the filter
+   * and the others, which are decorrelated as before.

Review Comment:
   nit: "the ones moved below its domain join": there may be no domain join, 
e.g. for `t2.g > 0 AND CASE WHEN t2.g < 0 THEN raise_error('negative g') ELSE 
true END AND t2.k = t1.k`, where `t2.g > 0` is moved into a filter below the 
kept guard, but no domain join is built because the kept guard reads no outer 
column and the equality is pulled up. The same goes for "moved below the domain 
join" in the second paragraph.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -561,7 +615,12 @@ object DecorrelateInnerQuery extends PredicateHelper {
         plan match {
           case Filter(condition, child) =>
             val conditions = splitConjunctivePredicates(condition)
-            val (correlated, uncorrelated) = 
conditions.partition(containsOuter)
+            // Keep the written order around a non-deterministic or throwable 
conjunct when the
+            // results are aggregated, see `splitKeptInPlace`.
+            val (movedBelow, keptInPlace, rest) =
+              if (aggregated || underSetOp) splitKeptInPlace(conditions, 
child, outerPlan)

Review Comment:
   nit: the non-aggregated branch below does not use `movedBelow` and 
`keptInPlace`. They are empty there today only because of this condition, so a 
later change that widens it would silently drop conjuncts. Could you add an 
assertion in that branch that both are empty?
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +134,59 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated or used by 
a set operation, in
+   * their written order, into the ones moved below its domain join, 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, 
and so are the
+   * correlated ones written after them that read their outer columns. They 
read the domain
+   * attributes of those columns, so they are evaluated in their written order 
on the values of
+   * each outer row. The uncorrelated conjuncts that may move are moved below 
the domain join,
+   * where they can still be pushed into a scan.
+   *
+   * Nothing is kept when the domain can not stand for the outer columns that 
the kept conjuncts
+   * read. It groups the outer rows by them and joins them back with `<=>`, so 
each column must be
+   * orderable, its equal values must be identical, unlike a string of a 
non-binary collation or a
+   * float that can be -0.0, and the outer query must not be streaming.
+   *
+   * TODO: Keep the written order also for these outer columns, for a filter 
whose results are not
+   * aggregated, e.g. in an EXISTS subquery, 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,
+      outerPlan: LogicalPlan): (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)
+    val prefixOuterReferences = collectOuterReferences(prefix)
+    val (onKeptColumns, others) =
+      
rest.partition(collectOuterReferences(_).exists(prefixOuterReferences.contains))
+    val keptInPlace = prefix ++ onKeptColumns
+    val domainAttributes = collectOuterReferences(keptInPlace)
+    val canBuildDomain = (domainAttributes.isEmpty || !outerPlan.isStreaming) 
&&

Review Comment:
   The domain join recomputes the outer query. So when the outer input does not 
return the same rows again, the domain misses outer values. With a table `b(k 
INT, v INT)` filled by `INSERT INTO b SELECT id, id FROM range(100000)` and 
`rk` = `udf(() => scala.util.Random.nextInt(100000)).asNondeterministic()`:
   
   ```sql
   SELECT count(*) FROM (SELECT kk, (SELECT max(b.v) FROM b WHERE b.k = o.kk AND
     CASE WHEN b.v < 0 THEN raise_error('neg') ELSE true END) m
     FROM (SELECT rk() kk FROM range(1000)) o) WHERE m IS NULL OR m <> kk
   ```
   
   Master returns 0, because it uses the equality as a grouping key and builds 
no domain. This PR returned 994 in my run, and the guard never fires here. 
Without the guard, both return 0. Master's own domain joins (e.g. for a 
non-equality correlation under an aggregate) have the same problem, but this PR 
adds a domain to a filter in which an equality is followed by a conjunct that 
may not move, such as this guard; master decorrelates this filter without one. 
Could you check that the outer input is deterministic in `canBuildDomain`, e.g. 
`(domainAttributes.isEmpty || (!outerPlan.isStreaming && 
outerPlan.children.forall(_.deterministic)))`? `outerPlan` here is the operator 
that holds the subquery. For a scalar subquery the domain is built from its 
child, so `outerPlan.deterministic` would also fall back for `SELECT rand(), 
(SELECT ...) FROM t`. For an `EXISTS` or `IN` subquery in a `Filter`, I think 
`RewritePredicateSubquery` builds the domain over `Filter(<the other conjuncts 
of that 
 filter>, child)`, so those conjuncts would need to be deterministic too, which 
`outerPlan.children.forall(_.deterministic)` does not check (I have not 
measured that case). It does not catch every input that can return other rows 
(e.g. a `LogicalRDD` over random values, or a non-deterministic CTE referenced 
twice), so the TODO could name the rest.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +134,59 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated or used by 
a set operation, in
+   * their written order, into the ones moved below its domain join, 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, 
and so are the
+   * correlated ones written after them that read their outer columns. They 
read the domain
+   * attributes of those columns, so they are evaluated in their written order 
on the values of
+   * each outer row. The uncorrelated conjuncts that may move are moved below 
the domain join,
+   * where they can still be pushed into a scan.
+   *
+   * Nothing is kept when the domain can not stand for the outer columns that 
the kept conjuncts
+   * read. It groups the outer rows by them and joins them back with `<=>`, so 
each column must be
+   * orderable, its equal values must be identical, unlike a string of a 
non-binary collation or a
+   * float that can be -0.0, and the outer query must not be streaming.
+   *
+   * TODO: Keep the written order also for these outer columns, for a filter 
whose results are not
+   * aggregated, e.g. in an EXISTS subquery, 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,
+      outerPlan: LogicalPlan): (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)
+    val prefixOuterReferences = collectOuterReferences(prefix)
+    val (onKeptColumns, others) =
+      
rest.partition(collectOuterReferences(_).exists(prefixOuterReferences.contains))

Review Comment:
   nit: this also keeps a conjunct written after the prefix that reads only 
outer columns and cannot fail, such as `OuterReference(y) <=> 
OuterReference(z)` in the new plan test. That adds `z` to the domain: one more 
grouping and join column, and its type can now make the whole filter fall back. 
Such a conjunct could stay a join condition with the outer query, e.g. by also 
requiring `cond.references.nonEmpty`.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +134,59 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated or used by 
a set operation, in
+   * their written order, into the ones moved below its domain join, 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, 
and so are the
+   * correlated ones written after them that read their outer columns. They 
read the domain
+   * attributes of those columns, so they are evaluated in their written order 
on the values of
+   * each outer row. The uncorrelated conjuncts that may move are moved below 
the domain join,
+   * where they can still be pushed into a scan.
+   *
+   * Nothing is kept when the domain can not stand for the outer columns that 
the kept conjuncts
+   * read. It groups the outer rows by them and joins them back with `<=>`, so 
each column must be
+   * orderable, its equal values must be identical, unlike a string of a 
non-binary collation or a
+   * float that can be -0.0, and the outer query must not be streaming.
+   *
+   * TODO: Keep the written order also for these outer columns, for a filter 
whose results are not
+   * aggregated, e.g. in an EXISTS subquery, 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,
+      outerPlan: LogicalPlan): (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))

Review Comment:
   nit: `mayNotMove` checks the value against a `Set`. So when the same 
conjunct is written both before and after the throwable one, e.g. under ANSI 
mode `1 / a > 0 AND <guard> AND 1 / a > 0` with a throwable `<guard>` such as 
`CASE WHEN b < 0 THEN raise_error('negative b') ELSE true END`, the first one 
also counts as one that may not move, and it is not moved below the domain 
join. This is only conservative, but deciding by position would avoid it: treat 
a conjunct as one that may not move only when it is in `stayUp` and at or after 
the first non-deterministic or throwable conjunct, which is where 
`splitMovable` stops moving conjuncts unconditionally.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +134,59 @@ object DecorrelateInnerQuery extends PredicateHelper {
     }
   }
 
+  /**
+   * Splits the conjuncts of a filter whose results are aggregated or used by 
a set operation, in
+   * their written order, into the ones moved below its domain join, 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, 
and so are the
+   * correlated ones written after them that read their outer columns. They 
read the domain
+   * attributes of those columns, so they are evaluated in their written order 
on the values of
+   * each outer row. The uncorrelated conjuncts that may move are moved below 
the domain join,
+   * where they can still be pushed into a scan.
+   *
+   * Nothing is kept when the domain can not stand for the outer columns that 
the kept conjuncts
+   * read. It groups the outer rows by them and joins them back with `<=>`, so 
each column must be
+   * orderable, its equal values must be identical, unlike a string of a 
non-binary collation or a
+   * float that can be -0.0, and the outer query must not be streaming.
+   *
+   * TODO: Keep the written order also for these outer columns, for a filter 
whose results are not

Review Comment:
   nit: could the TODO name the JIRA you said you will file for the 
ON-condition case, e.g. `TODO(SPARK-xxxxx)`, and the JIRAs for the other cases 
it lists once they are filed?
   



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