diff --git a/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java b/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java index ad06b3771704a..57523ed56dde5 100644 --- a/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java +++ b/core/trino-main/src/main/java/io/trino/sql/analyzer/StatementAnalyzer.java @@ -2858,6 +2858,7 @@ private GroupingSetAnalysis analyzeGroupBy(QuerySpecification node, Scope scope, } column = outputExpressions.get(toIntExact(ordinal - 1)); + verifyNoAggregateWindowOrGroupingFunctions(metadata, column, "GROUP BY clause"); } else { verifyNoAggregateWindowOrGroupingFunctions(metadata, column, "GROUP BY clause"); diff --git a/core/trino-main/src/test/java/io/trino/sql/analyzer/TestAnalyzer.java b/core/trino-main/src/test/java/io/trino/sql/analyzer/TestAnalyzer.java index 94016c08b0f7f..84c9f05eee299 100644 --- a/core/trino-main/src/test/java/io/trino/sql/analyzer/TestAnalyzer.java +++ b/core/trino-main/src/test/java/io/trino/sql/analyzer/TestAnalyzer.java @@ -440,6 +440,18 @@ public void testGroupByInvalidOrdinal() .hasErrorCode(INVALID_COLUMN_REFERENCE); } + @Test + public void testGroupByAggregation() + { + assertFails("SELECT x, sum(y) FROM (VALUES (1, 2)) t(x, y) GROUP BY x, sum(y)") + .hasErrorCode(EXPRESSION_NOT_SCALAR) + .hasMessageMatching(".* GROUP BY clause cannot contain aggregations, window functions or grouping operations: .*"); + + assertFails("SELECT x, sum(y) FROM (VALUES (1, 2)) t(x, y) GROUP BY 1, 2") + .hasErrorCode(EXPRESSION_NOT_SCALAR) + .hasMessageMatching(".* GROUP BY clause cannot contain aggregations, window functions or grouping operations: .*"); + } + @Test public void testGroupByWithSubquerySelectExpression() {