Github user hvanhovell commented on a diff in the pull request:
https://github.com/apache/spark/pull/15936#discussion_r89156723
--- 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(
+ "GROUP BY column(s) in scalar subquery must exist in the
WHERE clause: " +
+ s"${invalidCols.toString}")
--- End diff --
`invalidCols.mkString(",")`? (No need for string interpolation)
---
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]