dongjoon-hyun commented on code in PR #60764:
URL: https://github.com/apache/spark/pull/60764#discussion_r4656394987
##########
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:
Thank you. I confirmed all four queries. Now nothing is kept when a filter
or a join below equates an outer column that a kept conjunct reads to an inner
column, so these filters are decorrelated as on master. I added the four
queries to `SubquerySuite` and the derived-table shape to the plan test.
##########
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:
Confirmed (986 rows on the previous head). Now nothing is kept unless the
children of the operator that holds the subquery are deterministic, as you
suggested, and the TODO names the outer inputs that this check does not catch.
##########
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:
Confirmed. The checks now look into the SQL type of a user-defined type, so
this filter falls back. I added a UDT case to the plan test.
##########
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:
Done. Under a set operation the checks are skipped, since the domain has
every outer column anyway. The plan test checks that `a > 0` still moves below
the domain join there.
##########
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:
Thank you. `DecorrelateInnerQuery.apply` runs `BooleanSimplification` first,
which removes a deterministic conjunct written again and keeps the first one,
and a non-deterministic conjunct never moves. So deciding by value gives the
same split here, and I kept the simpler form.
##########
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:
Done. A conjunct written after the kept ones has to read an inner column to
be kept, so `y <=> z` is pulled up again in the plan test.
##########
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:
I'll update the TODO with the JIRA IDs once I file them.
##########
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:
Fixed. It now says that they are moved into a filter below.
--
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]