dongjoon-hyun commented on code in PR #60764:
URL: https://github.com/apache/spark/pull/60764#discussion_r4656690295
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +135,87 @@ 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 groups the outer rows by its columns, joins them back with
`<=>` and reads the outer
+ * query once more. 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. The outer query must also return the
same rows again, so it
+ * must not be streaming or non-deterministic. Otherwise nothing is kept,
unless the filter is
+ * used by a set operation, where the domain has every outer column anyway,
and a correlated
+ * conjunct written after the kept ones is not kept either.
+ *
+ * TODO: Keep the written order also for these outer columns and outer
queries, 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. Also detect an outer
query that returns
+ * other rows again although its children are deterministic, e.g. a
`LogicalRDD` over random
+ * values, or another non-deterministic conjunct of the filter that holds an
EXISTS subquery.
+ */
+ private def splitKeptInPlace(
+ conditions: Seq[Expression],
+ child: LogicalPlan,
+ outerPlan: LogicalPlan,
+ 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 equatedBelow = AttributeSet(child.collect {
+ case Filter(cond, _) => cond
+ case Join(_, _, _, Some(cond), _) => cond
+ }.flatMap(cond =>
collectEquivalentOuterReferences(splitConjunctivePredicates(cond)).keys))
+ lazy val outerIsRepeatable =
+ !outerPlan.isStreaming && outerPlan.children.forall(_.deterministic)
Review Comment:
Thank you. I confirmed it (269 rows at 75de73016d0). Now, when the operator
that holds the subquery is a `Filter`, its conjuncts without an IN or EXISTS
subquery have to be deterministic too, so this query is decorrelated as on
master: 504 rows in my run, with a plain left semi join. The plan test covers
it. The `EXISTS ... LIMIT 1` form falls back too now and returns 0 rows as on
master, which looks like a separate issue on master.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +135,87 @@ 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 groups the outer rows by its columns, joins them back with
`<=>` and reads the outer
+ * query once more. 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. The outer query must also return the
same rows again, so it
+ * must not be streaming or non-deterministic. Otherwise nothing is kept,
unless the filter is
+ * used by a set operation, where the domain has every outer column anyway,
and a correlated
+ * conjunct written after the kept ones is not kept either.
+ *
+ * TODO: Keep the written order also for these outer columns and outer
queries, 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. Also detect an outer
query that returns
+ * other rows again although its children are deterministic, e.g. a
`LogicalRDD` over random
+ * values, or another non-deterministic conjunct of the filter that holds an
EXISTS subquery.
+ */
+ private def splitKeptInPlace(
+ conditions: Seq[Expression],
+ child: LogicalPlan,
+ outerPlan: LogicalPlan,
+ 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 equatedBelow = AttributeSet(child.collect {
+ case Filter(cond, _) => cond
+ case Join(_, _, _, Some(cond), _) => cond
Review Comment:
Done.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -621,18 +712,22 @@ object DecorrelateInnerQuery extends PredicateHelper {
val (equalityCond, predicates) =
if (underSetOp) (Seq.empty[Expression], correlated)
else correlated.partition(canPullUpOverAgg)
- val outerReferences = collectOuterReferences(predicates)
+ // No equality equates an outer column that a kept conjunct
reads, here or below (see
+ // `splitKeptInPlace`), so the kept conjuncts read its domain
attribute.
+ val outerReferences = collectOuterReferences(predicates ++
keptInPlace)
val newOuterReferences =
parentOuterReferences ++ outerReferences -- equivalences.keySet
- val (newChild, joinCond, outerReferenceMap) =
- decorrelate(child, newOuterReferences, aggregated, underSetOp)
+ 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,
newOuterReferenceMap)
Review Comment:
Added the assertion.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala:
##########
@@ -133,6 +135,87 @@ 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 groups the outer rows by its columns, joins them back with
`<=>` and reads the outer
+ * query once more. 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. The outer query must also return the
same rows again, so it
+ * must not be streaming or non-deterministic. Otherwise nothing is kept,
unless the filter is
+ * used by a set operation, where the domain has every outer column anyway,
and a correlated
+ * conjunct written after the kept ones is not kept either.
Review Comment:
Thank you. I used your wording.
--
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]