Github user hvanhovell commented on a diff in the pull request:
https://github.com/apache/spark/pull/12720#discussion_r61375586
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
---
@@ -866,71 +867,189 @@ class Analyzer(
* Note: CTEs are handled in CTESubstitution.
*/
object ResolveSubquery extends Rule[LogicalPlan] with PredicateHelper {
-
/**
- * Resolve the correlated predicates in the clauses (e.g. WHERE or
HAVING) of a
- * sub-query by using the plan the predicates should be correlated to.
+ * Resolve a subquery using the outer plan. This rule creates a
dedicated analyzer which can
+ * also resolve outer plan references.
*/
- private def resolveCorrelatedSubquery(
- sub: LogicalPlan, outer: LogicalPlan,
- aliases: scala.collection.mutable.Map[Attribute, Alias]):
LogicalPlan = {
- // First resolve as much of the sub-query as possible
- val analyzed = execute(sub)
- if (analyzed.resolved) {
- analyzed
- } else {
- // Only resolve the lowest plan that is not resolved by outer
plan, otherwise it could be
- // resolved by itself
- val resolvedByOuter = analyzed transformDown {
- case q: LogicalPlan if q.childrenResolved && !q.resolved =>
- q transformExpressions {
- case u @ UnresolvedAttribute(nameParts) =>
- withPosition(u) {
- try {
- val outerAttrOpt = outer.resolve(nameParts, resolver)
- if (outerAttrOpt.isDefined) {
- val outerAttr = outerAttrOpt.get
- if (q.inputSet.contains(outerAttr)) {
- // Got a conflict, create an alias for the
attribute come from outer table
- val alias = Alias(outerAttr, outerAttr.toString)()
- val attr = alias.toAttribute
- aliases += attr -> alias
- attr
- } else {
- outerAttr
- }
- } else {
- u
- }
- } catch {
- case a: AnalysisException => u
- }
- }
+ def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+ // Only a few unary nodes (Project/Filter/Aggregate/Having) can
contain subqueries.
+ case q: UnaryNode if q.childrenResolved =>
+ q transformExpressions {
+ case e: SubqueryExpression if !e.query.resolved =>
+ val analyzer = new Analyzer(catalog, conf) {
+ override val extendedCheckRules = self.extendedCheckRules
+ override val extendedResolutionRules =
self.extendedResolutionRules :+
+ ResolveOuterReferences(q.child, resolver)
}
+ e.withNewPlan(analyzer.execute(e.query))
--- End diff --
The new rule will uses a special expression for all outer references calles
`OuterReference`. The outer reference basically hides the outer reference and
makes sure no-collisions happen. We resolve collisions when we pull out the
predicates (by adding a single project if we need one).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]