francis0407 commented on a change in pull request #23783: [SPARK-26854][SQL] 
Support ANY/SOME subquery
URL: https://github.com/apache/spark/pull/23783#discussion_r256837080
 
 

 ##########
 File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
 ##########
 @@ -1148,6 +1165,12 @@ class AstBuilder(conf: SQLConf) extends 
SqlBaseBaseVisitor[AnyRef] with Logging
         EqualNullSafe(e, expression(ctx.right))
       case SqlBaseParser.DISTINCT =>
         Not(EqualNullSafe(e, expression(ctx.right)))
+      case SqlBaseParser.ANY | SqlBaseParser.SOME =>
+        invertIfNotDefined(AnySubquery(
+            getValueExpressions(e),
+            getComparisonOperator(
+              
ctx.comparisonOperator.getChild(0).asInstanceOf[TerminalNode].getSymbol.getType),
+            ListQuery(plan(ctx.query))))
 
 Review comment:
   
https://github.com/apache/spark/blob/10539dd4e5fbac7cde0971b55320878156ff4e94/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala#L1079
   Here, `SqlBaseParser.NEQ` and `SqlBaseParser.NEQJ` will be parsed as 
Not(EqualTo(left, right)) which returns an `Expression` instead of 
`BinaryComparison`.
   I have an idea to reuse it, but still need to re-match.
   We can use `NotEqualTo(left, right)` in `getComparison(operator:Int, left: 
Expression, right:Expression)` but re-match it later.
   ```scala
   def getComparison(operator: Int, left: Expression, right: Expression): 
BinaryComparison = operator match{
     ...
     case SqlBaseParser.NEQ | SqlBaseParser.NEQJ =>
           NotEqualTo(left, right)
     ...
   }
   ```
   ```scala
   def visitComparison(ctx: ComparisonContext): Expression = {
     ...
     getComparison(operator, left, right) match {
       case _: NotEqualTo =>
         Not(EqualTo(left, right))
       case _ =>
         _
     }
   }
   ```
   As for `AnySubquery`, we still need a  function to rewrite these 
expression(used in `RewritePredicateSubquery`).
   
   ```scala
   def getComparisonExpression(cmp: BinaryComparison)(left: Expression, right: 
Expression)
     : Expression = cmp match{
     case _: EqualTo => EqualTo(left, right)
     ...
     case _: NotEqualTo => Not(EqualTo(left, right))
     ...
   }
   ```
   Do you have any better ideas?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to