Github user chenghao-intel commented on a diff in the pull request:

    https://github.com/apache/spark/pull/3249#discussion_r25551872
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -422,6 +424,108 @@ class Analyzer(catalog: Catalog,
             Generate(g, join = false, outer = false, None, child)
         }
       }
    +
    +  /**
    +   * Transforms the query which has subquery expressions in where clause 
to left semi join.
    +   * select T1.x from T1 where T1.x in (select T2.y from T2) transformed to
    +   * select T1.x from T1 left semi join T2 on T1.x = T2.y.
    +   */
    +  object SubQueryExpressions extends Rule[LogicalPlan] {
    +
    +    def apply(plan: LogicalPlan): LogicalPlan = plan transform {
    +      case p: LogicalPlan if !p.childrenResolved => p
    +      case filter @ Filter(conditions, child) =>
    +        val subqueryExprs = conditions.collect {
    +          case In(exp, Seq(SubqueryExpression(subquery))) => (exp, 
subquery)
    +        }
    +        // Replace subqueries with a dummy true literal since they are 
evaluated separately now.
    +        val transformedConds = conditions.transform {
    +          case In(_, Seq(SubqueryExpression(_))) => Literal(true)
    +        }
    +        subqueryExprs match {
    +          case Seq() => filter // No subqueries.
    +          case Seq((exp, subquery)) =>
    +            createLeftSemiJoin(
    +              child,
    +              exp,
    +              subquery,
    +              transformedConds)
    +          case _ =>
    +            throw new TreeNodeException(filter, "Only one SubQuery 
expression is supported.")
    +        }
    +    }
    +
    +    /**
    +     * Create LeftSemi join with parent query to the subquery which is 
mentioned in 'IN' predicate
    +     * And combine the subquery conditions and parent query conditions.
    +     */ 
    +    def createLeftSemiJoin(left: LogicalPlan,
    +        value: Expression,
    +        subquery: LogicalPlan,
    +        parentConds: Expression) : LogicalPlan = {
    +      val (transformedPlan, subqueryConds) = 
transformAndGetConditions(value, subquery)
    +      // Add both parent query conditions and subquery conditions as join 
conditions
    +      val allPredicates = And(parentConds, subqueryConds)
    +      Join(left, transformedPlan, LeftSemi, Some(allPredicates))
    +    }
    +
    +    /**
    +     * Transform the subquery LogicalPlan and add the expressions which 
are used as filters to the
    +     * projection. And also return filter conditions used in subquery
    +     */
    +    def transformAndGetConditions(value: Expression,
    +          subquery: LogicalPlan): (LogicalPlan, Expression) = {
    +      val expr = new scala.collection.mutable.ArrayBuffer[Expression]()
    +      // TODO : we only decorelate subqueries in very specific cases like 
the cases mentioned above
    +      // in documentation. The more complex queries like using of 
subqueries inside subqueries can 
    +      // be supported in future.
    +      val transformedPlan = subquery transform {
    +        case project @ Project(projectList, f @ Filter(condition, child)) 
=>
    +          // Don't support more than one item in select list of subquery
    +          if(projectList.size > 1) {
    +            throw new TreeNodeException(
    +                project,
    +                "SubQuery can contain only one item in Select List")
    +          }
    +          val resolvedChild = ResolveRelations(child)
    --- End diff --
    
    Why do you resolve the relation here? I believe the subquery should be 
resolved already before entering the rule of `SubQueryExpressions`, is that the 
limitation of using the `SubQueryExpression`, which can not check if children 
logical plan if it's resolved?


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

Reply via email to