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

    https://github.com/apache/spark/pull/15936#discussion_r89155790
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala
 ---
    @@ -117,19 +117,36 @@ trait CheckAnalysis extends PredicateHelper {
                     failAnalysis(s"Window specification $s is not valid 
because $m")
                   case None => w
                 }
    +          case s @ ScalarSubquery(query, conditions, _)
    +            // If no correlation, the output must be exactly one column
    +            if (conditions.isEmpty && query.output.size != 1) =>
    +              failAnalysis(
    +                s"Scalar subquery must return only one column, but got 
${query.output.size}")
     
               case s @ ScalarSubquery(query, conditions, _) if 
conditions.nonEmpty =>
    -            // Make sure correlated scalar subqueries contain one row for 
every outer row by
    -            // enforcing that they are aggregates which contain exactly 
one aggregate expressions.
    -            // The analyzer has already checked that subquery contained 
only one output column, and
    -            // added all the grouping expressions to the aggregate.
    -            def checkAggregate(a: Aggregate): Unit = {
    -              val aggregates = a.expressions.flatMap(_.collect {
    +            def checkAggregate(agg: Aggregate): Unit = {
    +              // Make sure correlated scalar subqueries contain one row 
for every outer row by
    +              // enforcing that they are aggregates which contain exactly 
one aggregate expressions.
    +              // The analyzer has already checked that subquery contained 
only one output column,
    +              // and added all the grouping expressions to the aggregate.
    +              val aggregates = agg.expressions.flatMap(_.collect {
                     case a: AggregateExpression => a
                   })
                   if (aggregates.isEmpty) {
                     failAnalysis("The output of a correlated scalar subquery 
must be aggregated")
                   }
    +
    +              // SPARK-18504: block cases where GROUP BY columns
    +              // are not part of the correlated columns
    +              val groupByCols = 
ExpressionSet.apply(agg.groupingExpressions.flatMap(_.references))
    +              val predicateCols = 
ExpressionSet.apply(conditions.flatMap(_.references))
    +              val invalidCols = groupByCols.diff(predicateCols)
    +              // GROUP BY columns must be a subset of columns in the 
predicates
    +              if (invalidCols.nonEmpty) {
    +                failAnalysis(s"""
    +                  |GROUP BY column(s) in scalar subquery must exist in the 
WHERE clause:
    +                  |${invalidCols.toString}""".stripMargin.replaceAll("\n", 
" "))
    --- End diff --
    
    Right. Thank you.


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