cloud-fan commented on a change in pull request #32072:
URL: https://github.com/apache/spark/pull/32072#discussion_r613272506



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/DecorrelateInnerQuery.scala
##########
@@ -0,0 +1,371 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.optimizer
+
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.expressions.SubExprUtils._
+import org.apache.spark.sql.catalyst.plans._
+import org.apache.spark.sql.catalyst.plans.logical._
+
+/**
+ * Decorrelate the inner query by eliminating outer references and create 
domain joins.
+ * The implementation is based on the paper: Unnesting Arbitrary Queries by 
Thomas Neumann
+ * and Alfons Kemper. https://dl.gi.de/handle/20.500.12116/2418.
+ * (1) Recursively collects outer references from the inner query until it 
reaches a node
+ *     that does not contain correlated value.
+ * (2) Inserts an optional [[DomainJoin]] node to indicate whether a domain 
(inner) join is
+ *     needed between the outer query and the specific subtree of the inner 
query.
+ * (3) Returns a list of join conditions with the outer query and a mapping 
between outer

Review comment:
       The comments of the 3 steps should go to `def decorrelate ...`, as these 
comments explain the recursive logic.
   
   The class doc should include the high-level idea
   ```
   A correlated subquery can be viewed as a "dependent" nested loop join 
between the outer and inner query.
   For each row produced by the outer query, we use it to bind the 
`OuterReference`s in the inner query, and then
   evaluate the inner query.
   
   Dependent Join
   :- Outer Query
   +- Inner Query
   
   If the `OuterReference`s are bound to the same value, the inner query 
returns the same result. Based on this, we can
   reduce the times to evaluate the inner query, by getting the distinct values 
of `OuterReference` first:
   
   Normal Join
   :- Outer Query
   +- Dependent Join
     :- Aggregate (group by: outer_ref1, outer_ref2, ...) (output: outer_ref1, 
outer_ref2, ...)
     :  +- Outer Query
     :- Inner Query
   
   The final dependent join with aggregate is called "domain join". We need to 
push down the domain join through
   the inner query until there is no outer reference anymore and the domain 
join becomes a normal join.
   
   The decorrelation function returns a join condition as well the new query 
with domain join added. The caller
   side will create another normal join using the returned join condition.
   
   E.g. decorrelate an inner query with equality predicates:
   ...
   ```




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

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