dongjoon-hyun commented on code in PR #60764:
URL: https://github.com/apache/spark/pull/60764#discussion_r4651107349
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -641,14 +653,16 @@ object DecorrelateInnerQuery extends PredicateHelper {
val newJoinCond = joinCond ++ equalityCond
(newFilter, newJoinCond, newOuterReferenceMap)
} else {
- // Results of this sub-tree is not aggregated, so all correlated
predicates
- // can be directly used as outer query join conditions.
- val newOuterReferences = parentOuterReferences --
equivalences.keySet
+ // Results of this sub-tree is not aggregated, so all correlated
predicates that are
+ // not kept in place can be directly used as outer query join
conditions.
+ val newOuterReferences = parentOuterReferences ++
+ collectOuterReferences(keptInPlace) -- equivalences.keySet
val (newChild, joinCond, outerReferenceMap) =
decorrelate(child, newOuterReferences, aggregated, underSetOp)
// Add the outer references mapping collected from the equality
conditions.
val newOuterReferenceMap = outerReferenceMap ++ equivalences
- val newFilter = uncorrelated match {
+ val newKeptInPlace = replaceOuterReferences(keptInPlace,
newOuterReferenceMap)
Review Comment:
Thank you. Confirmed with your query (DIVIDE_BY_ZERO at the previous head,
no row on master). Now the kept conjuncts read only the domain attributes of
their outer columns, and a correlated conjunct written after them that reads
one of those columns is kept in the filter too, so a cannot-fail equality there
still becomes the key of the domain join. Added both the plan check and your
scalar query to the tests.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -641,14 +653,16 @@ object DecorrelateInnerQuery extends PredicateHelper {
val newJoinCond = joinCond ++ equalityCond
(newFilter, newJoinCond, newOuterReferenceMap)
} else {
- // Results of this sub-tree is not aggregated, so all correlated
predicates
- // can be directly used as outer query join conditions.
- val newOuterReferences = parentOuterReferences --
equivalences.keySet
+ // Results of this sub-tree is not aggregated, so all correlated
predicates that are
+ // not kept in place can be directly used as outer query join
conditions.
+ val newOuterReferences = parentOuterReferences ++
Review Comment:
Confirmed. Now nothing is kept when an outer column that the kept conjuncts
read is not orderable, so such a filter is decorrelated as before. In addition,
this PR changes only the filters whose results are aggregated now, so the
`EXISTS` query here is decorrelated exactly as on master.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -561,7 +561,18 @@ object DecorrelateInnerQuery extends PredicateHelper {
plan match {
case Filter(condition, child) =>
val conditions = splitConjunctivePredicates(condition)
- val (correlated, uncorrelated) =
conditions.partition(containsOuter)
+ // A 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 are reordered. So the ones written up to the last
one that may not be
+ // evaluated ahead of the others (see `splitMovable`, taking an
outer reference as a
+ // column, which can not fail) stay first in the filter, in their
written order, with
+ // their outer references replaced.
+ val (keptInPlace, rest) = {
+ val stayUp = splitMovable(conditions.map(stripOuterReference),
Seq(child))._2.toSet
+ conditions.splitAt(
+ conditions.lastIndexWhere(cond =>
stayUp.contains(stripOuterReference(cond))) + 1)
Review Comment:
This PR no longer changes a filter whose results are not aggregated, so this
`EXISTS` query plans the hash semi join again. Under an `Aggregate`, a
correlated equality like `CAST(t2.s AS INT) = t1.k` can not be pulled up and
gets a domain already on master.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -641,14 +653,16 @@ object DecorrelateInnerQuery extends PredicateHelper {
val newJoinCond = joinCond ++ equalityCond
(newFilter, newJoinCond, newOuterReferenceMap)
} else {
- // Results of this sub-tree is not aggregated, so all correlated
predicates
- // can be directly used as outer query join conditions.
- val newOuterReferences = parentOuterReferences --
equivalences.keySet
+ // Results of this sub-tree is not aggregated, so all correlated
predicates that are
+ // not kept in place can be directly used as outer query join
conditions.
+ val newOuterReferences = parentOuterReferences ++
+ collectOuterReferences(keptInPlace) -- equivalences.keySet
val (newChild, joinCond, outerReferenceMap) =
decorrelate(child, newOuterReferences, aggregated, underSetOp)
// Add the outer references mapping collected from the equality
conditions.
val newOuterReferenceMap = outerReferenceMap ++ equivalences
- val newFilter = uncorrelated match {
+ val newKeptInPlace = replaceOuterReferences(keptInPlace,
newOuterReferenceMap)
+ val newFilter = (newKeptInPlace ++ uncorrelated) match {
Review Comment:
Thank you. The uncorrelated conjuncts that may move are now placed below the
domain join, so they are still pushed into the scan. The plan check covers it.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -561,7 +561,18 @@ object DecorrelateInnerQuery extends PredicateHelper {
plan match {
case Filter(condition, child) =>
val conditions = splitConjunctivePredicates(condition)
- val (correlated, uncorrelated) =
conditions.partition(containsOuter)
+ // A 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 are reordered. So the ones written up to the last
one that may not be
+ // evaluated ahead of the others (see `splitMovable`, taking an
outer reference as a
+ // column, which can not fail) stay first in the filter, in their
written order, with
+ // their outer references replaced.
+ val (keptInPlace, rest) = {
Review Comment:
Confirmed, it fails on master too. Since it is not a regression of this PR,
I added it to the TODO and I'll file a new JIRA issue for it.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -561,7 +561,18 @@ object DecorrelateInnerQuery extends PredicateHelper {
plan match {
case Filter(condition, child) =>
val conditions = splitConjunctivePredicates(condition)
- val (correlated, uncorrelated) =
conditions.partition(containsOuter)
+ // A 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 are reordered. So the ones written up to the last
one that may not be
+ // evaluated ahead of the others (see `splitMovable`, taking an
outer reference as a
+ // column, which can not fail) stay first in the filter, in their
written order, with
+ // their outer references replaced.
+ val (keptInPlace, rest) = {
+ val stayUp = splitMovable(conditions.map(stripOuterReference),
Seq(child))._2.toSet
Review Comment:
Agreed that the comment claimed too much. It now says that `splitMovable`
takes an outer reference as a column, and the TODO names this case.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -561,7 +561,18 @@ object DecorrelateInnerQuery extends PredicateHelper {
plan match {
case Filter(condition, child) =>
val conditions = splitConjunctivePredicates(condition)
- val (correlated, uncorrelated) =
conditions.partition(containsOuter)
+ // A conjunct used as a join condition with the outer query is
evaluated after the
Review Comment:
Added a TODO that names the cases this PR does not cover.
--
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]