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

    https://github.com/apache/spark/pull/9055#discussion_r42585365
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala
 ---
    @@ -270,6 +273,146 @@ class Analyzer(
       }
     
       /**
    +   * Rewrite the [[Exists]] [[In]] with left semi join or anti join.
    +   */
    +  object RewriteFilterSubQuery extends Rule[LogicalPlan] with 
PredicateHelper {
    +    def unapply(condition: Expression): Option[(Expression, 
Seq[Expression])] = {
    +      if (condition.resolved == false) {
    +        return None
    +      }
    +
    +      val conjuctions = splitConjunctivePredicates(condition).map(_ 
transformDown {
    +          // Remove the Cast expression for SubQueryExpression.
    +          case Cast(f: SubQueryExpression, BooleanType) => f
    +        }
    +      )
    +
    +      val (subqueries, others) = conjuctions.partition(c => 
c.isInstanceOf[SubQueryExpression])
    +      if (subqueries.isEmpty) {
    +        None
    +      } else if (subqueries.length > 1) {
    +        throw new AnalysisException(
    +          s"Only 1 SubQuery expression is supported, but we got 
$subqueries")
    +      } else {
    +        val subQueryExpr = subqueries(0).asInstanceOf[SubQueryExpression]
    +        // try to resolve the subquery
    +
    +        val subquery = Analyzer.this.execute(subQueryExpr.subquery) match {
    +          case Distinct(child) => child // Distinct is useless for semi 
join, ignore it.
    +          case other => other
    +        }
    +        Some((subQueryExpr.withNewSubQuery(subquery), others))
    +      }
    +    }
    +
    +    def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
    +      case f if f.childrenResolved == false => f
    +
    +      case f @ Filter(RewriteFilterSubQuery(subquery, others), left) =>
    +        subquery match {
    +          case Exists(Project(_, Filter(condition, right)), positive) =>
    +            checkAnalysis(right)
    +            if (condition.resolved) {
    +              // Apparently, it should be not resolved here, since EXIST 
should be correlated.
    +              throw new AnalysisException(
    +                s"Exist clause should be correlated, but we got 
$condition")
    +            }
    +            Join(others.reduceOption(And).map(Filter(_, 
left)).getOrElse(left), right,
    +              if (positive) LeftSemi else LeftAnti,
    +              Some(ResolveReferences.tryResolveAttributes(condition, 
right)))
    +
    +          case Exists(right, positive) =>
    +            throw new AnalysisException(s"Exist clause should be 
correlated, but we got $right")
    +
    +          case InSubquery(key, Project(projectList, Filter(condition, 
right)), positive) =>
    +            checkAnalysis(right)
    +            if (projectList.length != 1) {
    +              throw new AnalysisException(
    +                s"Expect only 1 projection in In Subquery Expression, but 
we got $projectList")
    +            } else {
    +              val rightKey = 
ResolveReferences.tryResolveAttributes(projectList(0), right)
    --- End diff --
    
    This is a good question, actually this is a workaround to solve the 
ambiguous references issue like:.
    `SELECT 'value FROM src WHERE 'key IN (SELECT 'key FROM src b WHERE 'key > 
100)`
    Literally, we will transform the SQL as:
    
    `SELECT 'value FROM src LEFT SEMI JOIN src b ON 'key = 'key and 'key > 
100`, this is reference ambiguous for `'key`!
    
    The `ResolveReferences.tryResolveAttributes` will partially resolve the 
project list and filter condition of the subquery, and then what we got looks 
like:
    `SELECT 'value FROM src LEFI SEMI JOIN src b ON 'key = key#123 and key#123 
> 100`
    
    And then we will leave the unresolved attributes for the other rules.
    
    There is another doable solution is complete the alias for the attributes / 
relations like first:
    `SELECT 'value FROM src WHERE 'key IN (SELECT 'key FROM src b WHERE 'key > 
100)` =>
    `SELECT 'a.value FROM src a WHERE 'a.key IN (SELECT 'b.key FROM src b WHERE 
'b.key > 100)` =>
    `SELECT 'a.value FROM src a LEFT SEMI JOIN src b ON 'a.key = 'b.key and 
'b.key > 100`
    But this probably requires more code change, and probably will confusing 
people when they check the generated logical plan.



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