dongjoon-hyun commented on code in PR #60783:
URL: https://github.com/apache/spark/pull/60783#discussion_r4659978858
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteAsOfJoin.scala:
##########
@@ -81,9 +83,34 @@ object RewriteAsOfJoin extends Rule[LogicalPlan] {
val aggExpr = Alias(nearestRight, "__nearest_right__")()
val aggregate = Aggregate(Seq.empty, Seq(aggExpr), filtered)
- val projectWithScalarSubquery = Project(
+ val projectWithCorrelatedSubquery = Project(
left.output :+ Alias(ScalarSubquery(aggregate, left.output),
"__right__")(),
left)
+ val projectWithScalarSubquery = if (conf.decorrelateInnerQueryEnabled)
{
Review Comment:
Thank you. You're right about the regression. With the configuration false,
a stream-static as-of join with a constant left as-of key and an equality such
as `k === qk + 1` returned the matching rows on `master`, and the forced
decorrelation made it fail. I confirmed it with a streaming query.
One correction: in the Update output mode, the inner join doesn't return
duplicated rows. It fails with `Update output mode is not supported for
stream-stream LeftOuter join`, because the scalar subquery becomes a left outer
join. The same failures happen with the configuration true, so DataFrame
stream-static as-of joins on this path fail by default as well. I'll file a new
JIRA issue for that.
I applied your recommendation in 084a497b34e. A streaming left side now
keeps the correlated subquery for the legacy decorrelation, with a TODO, and a
new test in `StreamingAsOfJoinSuite` runs a stream-static as-of join with the
configuration false. I also updated the PR description.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteAsOfJoin.scala:
##########
@@ -81,9 +83,34 @@ object RewriteAsOfJoin extends Rule[LogicalPlan] {
val aggExpr = Alias(nearestRight, "__nearest_right__")()
val aggregate = Aggregate(Seq.empty, Seq(aggExpr), filtered)
- val projectWithScalarSubquery = Project(
+ val projectWithCorrelatedSubquery = Project(
left.output :+ Alias(ScalarSubquery(aggregate, left.output),
"__right__")(),
left)
+ val projectWithScalarSubquery = if (conf.decorrelateInnerQueryEnabled)
{
+ projectWithCorrelatedSubquery
+ } else {
+ // Without DecorrelateInnerQuery, PullupCorrelatedPredicates only
pulls correlated
+ // predicates out of a Filter. It would leave the outer reference in
the MIN_BY order
+ // expression, and pull a non-equality predicate such as the as-of
condition above the
+ // Aggregate, which changes its result. So decorrelate this subquery
as when it is
+ // enabled: run the rules that do it then, with it enabled, on this
Project alone. A
+ // leaf stands in for `left`, so that other subqueries follow the
configuration. The
+ // later run of PullupCorrelatedPredicates keeps the result, as it
has no outer
+ // references left. Like the optimizer, skip an excluded
+ // PullOutNestedDataOuterRefExpressions; PullupCorrelatedPredicates
is non-excludable.
+ val leftPlaceholder = LocalRelation(left.output)
+ val host =
projectWithCorrelatedSubquery.withNewChildren(Seq(leftPlaceholder))
+ val pullOutExcluded =
conf.optimizerExcludedRules.toSeq.flatMap(Utils.stringToSeq)
+ .contains(PullOutNestedDataOuterRefExpressions.ruleName)
+ val decorrelationEnabled = conf.clone()
+
decorrelationEnabled.setConf(SQLConf.DECORRELATE_INNER_QUERY_ENABLED, true)
+ val decorrelated = SQLConf.withExistingConf(decorrelationEnabled) {
+ PullupCorrelatedPredicates(
+ if (pullOutExcluded) host else
PullOutNestedDataOuterRefExpressions(host))
+ }.transformUp { case r: LocalRelation if r eq leftPlaceholder =>
left }
+ assert(!decorrelated.exists(_ eq leftPlaceholder), "The placeholder
for left is left")
Review Comment:
Thank you. Right, the assertion could not fail, and it would not catch a
copied placeholder either. In 084a497b34e, I removed it and changed the swap to
compare with `==`, which also replaces a copy. An empty `LocalRelation` left
that equals the placeholder is replaced by itself.
--
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]