From 41ff64c3d4f065c754d21af3f1ccd91c351a1240 Mon Sep 17 00:00:00 2001 From: Pratham Desai Date: Wed, 9 Jun 2021 17:22:48 -0400 Subject: [PATCH] Validate ordinal based grouping expressions during analysis --- .../io/trino/sql/analyzer/StatementAnalyzer.java | 1 + .../java/io/trino/sql/analyzer/TestAnalyzer.java | 12 ++++++++++++ 2 files changed, 13 insertions(+) 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() {