Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/12720#discussion_r61354430
  
    --- 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 --
    
    Also, the existing code will create new exprId if there is any ambitious, 
but the new code does not. 


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to