Skip to content

Commit 3e1b6b3

Browse files
committed
[SPARK-17016][SQL] Improve group-by/order-by ordinal error reporting
1 parent 7a6a3c3 commit 3e1b6b3

File tree

1 file changed

+12
-12
lines changed
  • sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis

1 file changed

+12
-12
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,7 @@ class Analyzer(
547547
case a: Aggregate if containsStar(a.aggregateExpressions) =>
548548
if (conf.groupByOrdinal && a.groupingExpressions.exists(IntegerIndex.unapply(_).nonEmpty)) {
549549
failAnalysis(
550-
"Group by position: star is not allowed to use in the select list " +
551-
"when using ordinals in group by")
550+
"Star (*) is not allowed in select list when GROUP BY ordinal position is used")
552551
} else {
553552
a.copy(aggregateExpressions = buildExpandedProjectList(a.aggregateExpressions, a.child))
554553
}
@@ -723,9 +722,9 @@ class Analyzer(
723722
if (index > 0 && index <= child.output.size) {
724723
SortOrder(child.output(index - 1), direction)
725724
} else {
726-
throw new UnresolvedException(s,
727-
s"Order/sort By position: $index does not exist " +
728-
s"The Select List is indexed from 1 to ${child.output.size}")
725+
s.failAnalysis(
726+
s"ORDER BY position $index is not in select list " +
727+
s"(valid range is [1, ${child.output.size}])")
729728
}
730729
case o => o
731730
}
@@ -737,17 +736,18 @@ class Analyzer(
737736
if conf.groupByOrdinal && aggs.forall(_.resolved) &&
738737
groups.exists(IntegerIndex.unapply(_).nonEmpty) =>
739738
val newGroups = groups.map {
740-
case IntegerIndex(index) if index > 0 && index <= aggs.size =>
739+
case ordinal @ IntegerIndex(index) if index > 0 && index <= aggs.size =>
741740
aggs(index - 1) match {
742741
case e if ResolveAggregateFunctions.containsAggregate(e) =>
743-
throw new UnresolvedException(a,
744-
s"Group by position: the '$index'th column in the select contains an " +
745-
s"aggregate function: ${e.sql}. Aggregate functions are not allowed in GROUP BY")
742+
ordinal.failAnalysis(
743+
s"GROUP BY position $index is an aggregate function, and " +
744+
"aggregate functions are not allowed in GROUP BY")
746745
case o => o
747746
}
748-
case IntegerIndex(index) =>
749-
throw new UnresolvedException(a,
750-
s"Group by position: '$index' exceeds the size of the select list '${aggs.size}'.")
747+
case ordinal @ IntegerIndex(index) =>
748+
ordinal.failAnalysis(
749+
s"GROUP BY position $index is not in select list " +
750+
s"(valid range is [1, ${aggs.size}])")
751751
case o => o
752752
}
753753
Aggregate(newGroups, aggs, child)

0 commit comments

Comments
 (0)