You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ur...@apache.org on 2026/10/15 18:28:46 UTC
(spark) branch branch-4.x updated: [SPARK-60239][SQL] Reject aliased GROUP BY ordinals in pipe AGGREGATE
This is an automated email from the ASF dual-hosted git repository.
uros-b pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new 7afc1055b135 [SPARK-60239][SQL] Reject aliased GROUP BY ordinals in pipe AGGREGATE
7afc1055b135 is described below
commit 7afc1055b135f49098101e06d434e3afc7836ffc
Author: marko-radoicic-db <ma...@databricks.com>
AuthorDate: Thu Oct 15 18:28:21 2026 +0000
[SPARK-60239][SQL] Reject aliased GROUP BY ordinals in pipe AGGREGATE
### What changes were proposed in this pull request?
Reject aliases on integer-literal ordinals in the `GROUP BY` clause of pipe `AGGREGATE`.
For example, `TABLE t |> AGGREGATE sum(x) AS s GROUP BY 2 AS grp` now raises
`UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE` (`0A000`), the same error
class pipe `AGGREGATE` already uses for its other `GROUP BY` restrictions (ALL, CUBE, ROLLUP,
GROUPING SETS, GROUPING/GROUPING_ID and window functions):
```text
[UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE] The feature is not supported:
The SQL pipe operator syntax with aggregation (using |> AGGREGATE) does not support aliases
on GROUP BY ordinals. SQLSTATE: 0A000
```
The query context points at the aliased grouping expression (`2 AS grp`).
`AstBuilder.visitOperatorPipeAggregate`, next to the other pipe `AGGREGATE` restrictions, raises the error when a top-level grouping
expression is an aliased integer literal and `spark.sql.groupByOrdinal` is true. This covers
both `AS grp` and implicit `grp` aliases, as well as parenthesized and negative literals.
Bare ordinals and aliases on other grouping expressions (including `1 + 1`,
`CAST(2 AS INT)`, `2L` and `2.0`) remain supported. With `spark.sql.groupByOrdinal=false`,
integer literals are not ordinals, so an aliased integer literal stays a valid constant
grouping key. Regular `GROUP BY`, which rejects every alias with `PARSE_SYNTAX_ERROR`, is
unchanged.
The new internal config `spark.sql.legacy.allowAliasedPipeAggregateOrdinals` (default
`false`) restores the previous behavior. The SQL migration guide documents the change, and
the pipe-syntax reference notes that ordinals cannot be aliased.
### Why are the changes needed?
In pipe `AGGREGATE`, a bare integer grouping key is an ordinal, but an aliased integer
grouping key is not replaced by an ordinal because the literal is wrapped in an `Alias`.
`GROUP BY 2 AS grp` therefore silently groups by the constant `2` and outputs a column
`grp = 2`, while `GROUP BY 2` groups by the second input column. The aliased form is
ambiguous, and users who write it most likely mean the ordinal.
### Does this PR introduce _any_ user-facing change?
Yes. When `spark.sql.groupByOrdinal` is true (the default), pipe `AGGREGATE` queries that
alias an integer-literal grouping key, such as
`TABLE t |> AGGREGATE sum(x) AS s GROUP BY 2 AS grp`, previously grouped by a constant and
now fail with `UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE`. Bare ordinals
and aliases on non-ordinal grouping expressions are unchanged. Setting
`spark.sql.legacy.allowAliasedPipeAggregateOrdinals` to `true` restores the previous behavior.
### How was this patch tested?
Added four tests to `PlanParserSuite`:
- Pipe `AGGREGATE` raises the new error, with the aliased grouping expression as the query
context, for 24 aliased integer-literal forms (ordinals `0`, `1`, `2`, `3`, `-1` and `(2)`;
`AS grp` and `grp` aliases; with and without a preceding grouping key), while regular
`GROUP BY` keeps `PARSE_SYNTAX_ERROR` for the same forms.
- Bare ordinals in pipe `AGGREGATE` still parse to `UnresolvedOrdinal` /
`UnresolvedPipeAggregateOrdinal`.
- Aliases on non-ordinal grouping expressions are preserved.
- With the legacy config enabled, or with `spark.sql.groupByOrdinal=false`, pipe `AGGREGATE`
accepts aliased integer literals as constant grouping keys, while regular `GROUP BY` still
rejects them.
Added golden-file cases to `pipe-operators.sql` for aliased integer literals (`2 AS grp`,
`x, 2 grp`, `(2) AS grp`, `-1 AS grp`), aliases on other grouping expressions (`x`, `1 + 1`,
`2L`), the legacy config, and `spark.sql.groupByOrdinal=false`.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude Opus 5.5)
This pull request and its description were written by Isaac.
Closes #59463 from marko-radoicic-db/disallow-pipe-group-by-ordinal-alias.
Authored-by: marko-radoicic-db <ma...@databricks.com>
Signed-off-by: Uros Bojanic <22...@users.noreply.github.com>
(cherry picked from commit 41f1694f7ba7cc3dd247d00dc42d13f5a8b1eaf7)
Signed-off-by: Uros Bojanic <22...@users.noreply.github.com>
---
docs/sql-migration-guide.md | 1 +
docs/sql-pipe-syntax.md | 3 +-
.../spark/sql/catalyst/parser/AstBuilder.scala | 11 ++
.../org/apache/spark/sql/internal/SQLConf.scala | 12 ++
.../sql/catalyst/parser/PlanParserSuite.scala | 74 ++++++++-
.../analyzer-results/pipe-operators.sql.out | 153 +++++++++++++++++++
.../resources/sql-tests/inputs/pipe-operators.sql | 35 +++++
.../sql-tests/results/pipe-operators.sql.out | 169 +++++++++++++++++++++
8 files changed, 456 insertions(+), 2 deletions(-)
diff --git a/docs/sql-migration-guide.md b/docs/sql-migration-guide.md
index 3a86a507272e..6914d962682c 100644
--- a/docs/sql-migration-guide.md
+++ b/docs/sql-migration-guide.md
@@ -44,6 +44,7 @@ license: |
- Since Spark 4.4, `spark.sql.sources.v2.bucketing.partitionKeyOrdering.enabled` defaults to `true`. A V2 scan that reports a keyed partitioning but no explicit ordering now also reports itself sorted by its partition key expressions, because every row of such a partition evaluates them to the same value. Partition transforms such as `days(ts)` or `bucket(8, id)` are left out of that ordering. This lets Spark drop a `Sort` it would otherwise place above the scan. An aggregate that groups [...]
- Since Spark 4.4, `spark.sql.sources.v2.bucketing.preserveKeyOrderingOnCoalesce.enabled` defaults to `true`. When `GroupPartitionsExec` merges several input partitions that share one partition key value into a single output partition, it now keeps sort orders over the partition key expressions in the ordering it reports, since those expressions are constant within the merged partition. Sort orders over partition transforms such as `days(ts)` or `bucket(8, id)` are left out. Orders over [...]
- Since Spark 4.4, the value columns of `UNPIVOT` and `Dataset.unpivot` merge the nested nullability (array element, map value, and struct field nullability) of all unpivoted values. Previously, a value column took the nested nullability of the first listed value, so a `NULL` array element, map value, or struct field of a later value could be returned as a non-`NULL` value such as `0`, and `IS NULL` on it returned `false`. A view created by an earlier Spark version over such an `UNPIVOT` [...]
+- Since Spark 4.4, when `spark.sql.groupByOrdinal` is true (the default), the pipe `AGGREGATE` operator rejects an aliased integer literal in its `GROUP BY` clause, such as `TABLE t |> AGGREGATE sum(x) AS s GROUP BY 2 AS grp`, with `UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE`, because a bare integer literal in the same position is an ordinal. Previously the aliased literal was grouped as a constant. Bare ordinals and aliases on other grouping expressions are not affecte [...]
## Upgrading from Spark SQL 4.2 to 4.3
diff --git a/docs/sql-pipe-syntax.md b/docs/sql-pipe-syntax.md
index 3d757db96623..91ab636ab420 100644
--- a/docs/sql-pipe-syntax.md
+++ b/docs/sql-pipe-syntax.md
@@ -437,7 +437,8 @@ aggregate functions. The `GROUP BY` expressions may include one-based ordinals.
in which such ordinals refer to the expressions in the accompanying `SELECT` clause, in SQL pipe
syntax, they refer to the columns of the relation produced by the preceding operator instead. For
example, in `TABLE t |> AGGREGATE COUNT(*) GROUP BY 2`, we refer to the second column of the input
-table `t`.
+table `t`. An ordinal cannot be aliased, so `TABLE t |> AGGREGATE COUNT(*) GROUP BY 2 AS grp`
+returns an error. Other grouping expressions can be aliased.
There is no need to repeat entire expressions between `GROUP BY` and `SELECT`, since the `AGGREGATE`
operator automatically includes the evaluated grouping expressions in its output. By the same token,
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
index 84f4bd9ef25c..30f17afa12fa 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala
@@ -8302,6 +8302,17 @@ class AstBuilder extends DataTypeAstBuilder
error("GROUP BY ALL")
case _ =>
}
+ // With `spark.sql.groupByOrdinal` enabled, an integer literal is a GROUP BY ordinal, so
+ // an alias on it is ambiguous with grouping by the constant. Point the error at the
+ // aliased grouping expression rather than the whole clause.
+ if (conf.groupByOrdinal &&
+ !conf.getConf(SQLConf.LEGACY_ALLOW_ALIASED_PIPE_AGGREGATE_ORDINALS)) {
+ a.groupingExpressions.foreach {
+ case alias @ Alias(Literal(_: Int, IntegerType), _) =>
+ CurrentOrigin.withOrigin(alias.origin) { error("aliases on GROUP BY ordinals") }
+ case _ =>
+ }
+ }
def visit(e: Expression): Unit = {
e match {
case _: Cube => error("GROUP BY CUBE")
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
index ff955fae8f34..29f880b526cc 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala
@@ -8236,6 +8236,18 @@ object SQLConf {
.booleanConf
.createWithDefault(true)
+ val LEGACY_ALLOW_ALIASED_PIPE_AGGREGATE_ORDINALS =
+ buildConf("spark.sql.legacy.allowAliasedPipeAggregateOrdinals")
+ .internal()
+ .doc("When true (legacy behavior), an aliased integer literal in the GROUP BY clause of " +
+ "the pipe AGGREGATE operator, such as 'GROUP BY 2 AS grp', is accepted and grouped as " +
+ "a constant. When false (the default) and spark.sql.groupByOrdinal is true, it fails " +
+ "with an error, because a bare integer literal there is an ordinal.")
+ .version("4.4.0")
+ .withBindingPolicy(ConfigBindingPolicy.SESSION)
+ .booleanConf
+ .createWithDefault(false)
+
val LEGACY_PERCENTILE_DISC_CALCULATION = buildConf("spark.sql.legacy.percentileDiscCalculation")
.internal()
.doc("If true, the old bogus percentile_disc calculation is used. The old calculation " +
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala
index 7f88e7044c4a..b9104a9af403 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/PlanParserSuite.scala
@@ -22,7 +22,7 @@ import scala.annotation.nowarn
import org.apache.spark.SparkThrowable
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.{FunctionIdentifier, TableIdentifier}
-import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, RelationChanges, RelationTimeTravel, UnresolvedAlias, UnresolvedAttribute, UnresolvedFunction, UnresolvedGenerator, UnresolvedInlineTable, UnresolvedInsertTarget, UnresolvedRelation, UnresolvedStar, UnresolvedSubqueryColumnAliases, UnresolvedTableValuedFunction, UnresolvedTVFAliases}
+import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, RelationChanges, RelationTimeTravel, UnresolvedAlias, UnresolvedAttribute, UnresolvedFunction, UnresolvedGenerator, UnresolvedInlineTable, UnresolvedInsertTarget, UnresolvedOrdinal, UnresolvedPipeAggregateOrdinal, UnresolvedRelation, UnresolvedStar, UnresolvedSubqueryColumnAliases, UnresolvedTableValuedFunction, UnresolvedTVFAliases}
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans._
import org.apache.spark.sql.catalyst.plans.logical._
@@ -548,6 +548,78 @@ class PlanParserSuite extends AnalysisTest {
parameters = Map("error" -> "'b'", "hint" -> ": extra input 'b'"))
}
+ test("pipe AGGREGATE rejects aliases on GROUP BY ordinals") {
+ for {
+ ordinal <- Seq("0", "1", "2", "3", "-1", "(2)")
+ alias <- Seq("AS grp", "grp")
+ prefix <- Seq("", "g, ")
+ } {
+ val groupingExpression = s"$ordinal $alias"
+ val query = s"TABLE t |> AGGREGATE sum(x) AS s GROUP BY $prefix$groupingExpression"
+ val start = query.lastIndexOf(groupingExpression)
+ checkError(
+ exception = parseException(query),
+ condition = "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ sqlState = Some("0A000"),
+ parameters = Map("case" -> "aliases on GROUP BY ordinals"),
+ context = ExpectedContext(
+ fragment = groupingExpression,
+ start = start,
+ stop = start + groupingExpression.length - 1))
+
+ // Regular GROUP BY rejects every alias with a syntax error.
+ checkError(
+ exception = parseException(s"SELECT x, sum(x) FROM t GROUP BY $prefix$groupingExpression"),
+ condition = "PARSE_SYNTAX_ERROR",
+ sqlState = Some("42601"),
+ parameters = Map("error" -> "'grp'", "hint" -> ": extra input 'grp'"))
+ }
+ }
+
+ test("pipe AGGREGATE preserves unaliased GROUP BY ordinals") {
+ Seq(1, 2, 3).foreach { ordinal =>
+ val query = s"TABLE t |> AGGREGATE sum(x) AS s GROUP BY $ordinal"
+ val aggregate = parsePlan(query).asInstanceOf[Aggregate]
+ assert(aggregate.groupingExpressions == Seq(UnresolvedOrdinal(1)))
+ assert(aggregate.aggregateExpressions.head == UnresolvedPipeAggregateOrdinal(ordinal))
+ }
+ }
+
+ test("pipe AGGREGATE preserves aliases on non-ordinal GROUP BY expressions") {
+ Seq("g", "1 + 1", "CAST(2 AS INT)", "2L", "2.0").foreach { expression =>
+ val query = s"TABLE t |> AGGREGATE sum(x) AS s GROUP BY $expression AS grp"
+ val aggregate = parsePlan(query).asInstanceOf[Aggregate]
+ val alias = aggregate.groupingExpressions.head.asInstanceOf[Alias]
+ assert(alias.child == parseExpression(expression))
+ assert(alias.name == "grp")
+ assert(aggregate.aggregateExpressions.head == alias)
+ }
+ }
+
+ test("pipe AGGREGATE groups by aliased integer literals as constants with the legacy config " +
+ "or without GROUP BY ordinals") {
+ Seq(
+ SQLConf.LEGACY_ALLOW_ALIASED_PIPE_AGGREGATE_ORDINALS.key -> "true",
+ SQLConf.GROUP_BY_ORDINAL.key -> "false").foreach { sqlConf =>
+ withSQLConf(sqlConf) {
+ Seq("AS grp", "grp").foreach { alias =>
+ val query = s"TABLE t |> AGGREGATE sum(x) AS s GROUP BY 2 $alias"
+ val aggregate = parsePlan(query).asInstanceOf[Aggregate]
+ val groupingAlias = aggregate.groupingExpressions.head.asInstanceOf[Alias]
+ assert(groupingAlias.child == Literal(2))
+ assert(groupingAlias.name == "grp")
+ assert(aggregate.aggregateExpressions.head == groupingAlias)
+
+ checkError(
+ exception = parseException(s"SELECT x, sum(x) FROM t GROUP BY 2 $alias"),
+ condition = "PARSE_SYNTAX_ERROR",
+ sqlState = Some("42601"),
+ parameters = Map("error" -> "'grp'", "hint" -> ": extra input 'grp'"))
+ }
+ }
+ }
+ }
+
test("limit") {
val sql = "select * from t"
val plan = table("t").select(star())
diff --git a/sql/core/src/test/resources/sql-tests/analyzer-results/pipe-operators.sql.out b/sql/core/src/test/resources/sql-tests/analyzer-results/pipe-operators.sql.out
index 666c5c91f8bf..02d87c5a7b06 100644
--- a/sql/core/src/test/resources/sql-tests/analyzer-results/pipe-operators.sql.out
+++ b/sql/core/src/test/resources/sql-tests/analyzer-results/pipe-operators.sql.out
@@ -3536,6 +3536,159 @@ org.apache.spark.sql.AnalysisException
}
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp
+-- !query analysis
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 52,
+ "stopIndex" : 59,
+ "fragment" : "2 as grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by x, 2 grp
+-- !query analysis
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 55,
+ "stopIndex" : 59,
+ "fragment" : "2 grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by (2) as grp
+-- !query analysis
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 52,
+ "stopIndex" : 61,
+ "fragment" : "(2) as grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by -1 as grp
+-- !query analysis
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 52,
+ "stopIndex" : 60,
+ "fragment" : "-1 as grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by x as grp
+-- !query analysis
+Aggregate [x#x], [x#x AS grp#x, sum(y#x) AS sum(y)#xL]
++- Project [3 AS x#x, 4 AS y#x]
+ +- OneRowRelation
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 1 + 1 as grp
+-- !query analysis
+Aggregate [(1 + 1)], [(1 + 1) AS grp#x, sum(y#x) AS sum(y)#xL]
++- Project [3 AS x#x, 4 AS y#x]
+ +- OneRowRelation
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2L as grp
+-- !query analysis
+Aggregate [2], [2 AS grp#xL, sum(y#x) AS sum(y)#xL]
++- Project [3 AS x#x, 4 AS y#x]
+ +- OneRowRelation
+
+
+-- !query
+set spark.sql.legacy.allowAliasedPipeAggregateOrdinals=true
+-- !query analysis
+SetCommand (spark.sql.legacy.allowAliasedPipeAggregateOrdinals,Some(true))
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp
+-- !query analysis
+Aggregate [2], [2 AS grp#x, sum(y#x) AS sum(y)#xL]
++- Project [3 AS x#x, 4 AS y#x]
+ +- OneRowRelation
+
+
+-- !query
+set spark.sql.legacy.allowAliasedPipeAggregateOrdinals=false
+-- !query analysis
+SetCommand (spark.sql.legacy.allowAliasedPipeAggregateOrdinals,Some(false))
+
+
+-- !query
+set spark.sql.groupByOrdinal=false
+-- !query analysis
+SetCommand (spark.sql.groupByOrdinal,Some(false))
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp
+-- !query analysis
+Aggregate [2], [2 AS grp#x, sum(y#x) AS sum(y)#xL]
++- Project [3 AS x#x, 4 AS y#x]
+ +- OneRowRelation
+
+
+-- !query
+set spark.sql.groupByOrdinal=true
+-- !query analysis
+SetCommand (spark.sql.groupByOrdinal,Some(true))
+
+
-- !query
table courseSales
|> aggregate sum(earnings) group by rollup(course, `year`)
diff --git a/sql/core/src/test/resources/sql-tests/inputs/pipe-operators.sql b/sql/core/src/test/resources/sql-tests/inputs/pipe-operators.sql
index 916b114f17f5..4827ea2c121e 100644
--- a/sql/core/src/test/resources/sql-tests/inputs/pipe-operators.sql
+++ b/sql/core/src/test/resources/sql-tests/inputs/pipe-operators.sql
@@ -1136,6 +1136,41 @@ select 3 as x, 4 as y
select 3 as x, 4 as y
|> aggregate sum(y) group by 0;
+-- An integer-literal ordinal in the AGGREGATE GROUP BY clause cannot be aliased.
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp;
+
+select 3 as x, 4 as y
+|> aggregate sum(y) group by x, 2 grp;
+
+select 3 as x, 4 as y
+|> aggregate sum(y) group by (2) as grp;
+
+select 3 as x, 4 as y
+|> aggregate sum(y) group by -1 as grp;
+
+-- Aliases are allowed on grouping expressions other than integer literals.
+select 3 as x, 4 as y
+|> aggregate sum(y) group by x as grp;
+
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 1 + 1 as grp;
+
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2L as grp;
+
+-- The legacy config restores grouping by an aliased integer literal as a constant.
+set spark.sql.legacy.allowAliasedPipeAggregateOrdinals=true;
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp;
+set spark.sql.legacy.allowAliasedPipeAggregateOrdinals=false;
+
+-- An aliased integer literal is a constant grouping key when GROUP BY ordinals are disabled.
+set spark.sql.groupByOrdinal=false;
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp;
+set spark.sql.groupByOrdinal=true;
+
-- GROUP BY ROLLUP is not supported yet.
table courseSales
|> aggregate sum(earnings) group by rollup(course, `year`)
diff --git a/sql/core/src/test/resources/sql-tests/results/pipe-operators.sql.out b/sql/core/src/test/resources/sql-tests/results/pipe-operators.sql.out
index 578e1478ea23..d20c2e2917a6 100644
--- a/sql/core/src/test/resources/sql-tests/results/pipe-operators.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/pipe-operators.sql.out
@@ -3157,6 +3157,175 @@ org.apache.spark.sql.AnalysisException
}
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 52,
+ "stopIndex" : 59,
+ "fragment" : "2 as grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by x, 2 grp
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 55,
+ "stopIndex" : 59,
+ "fragment" : "2 grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by (2) as grp
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 52,
+ "stopIndex" : 61,
+ "fragment" : "(2) as grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by -1 as grp
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.sql.catalyst.parser.ParseException
+{
+ "errorClass" : "UNSUPPORTED_FEATURE.PIPE_OPERATOR_AGGREGATE_UNSUPPORTED_CASE",
+ "sqlState" : "0A000",
+ "messageParameters" : {
+ "case" : "aliases on GROUP BY ordinals"
+ },
+ "queryContext" : [ {
+ "objectType" : "",
+ "objectName" : "",
+ "startIndex" : 52,
+ "stopIndex" : 60,
+ "fragment" : "-1 as grp"
+ } ]
+}
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by x as grp
+-- !query schema
+struct<grp:int,sum(y):bigint>
+-- !query output
+3 4
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 1 + 1 as grp
+-- !query schema
+struct<grp:int,sum(y):bigint>
+-- !query output
+2 4
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2L as grp
+-- !query schema
+struct<grp:bigint,sum(y):bigint>
+-- !query output
+2 4
+
+
+-- !query
+set spark.sql.legacy.allowAliasedPipeAggregateOrdinals=true
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.legacy.allowAliasedPipeAggregateOrdinals true
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp
+-- !query schema
+struct<grp:int,sum(y):bigint>
+-- !query output
+2 4
+
+
+-- !query
+set spark.sql.legacy.allowAliasedPipeAggregateOrdinals=false
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.legacy.allowAliasedPipeAggregateOrdinals false
+
+
+-- !query
+set spark.sql.groupByOrdinal=false
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.groupByOrdinal false
+
+
+-- !query
+select 3 as x, 4 as y
+|> aggregate sum(y) group by 2 as grp
+-- !query schema
+struct<grp:int,sum(y):bigint>
+-- !query output
+2 4
+
+
+-- !query
+set spark.sql.groupByOrdinal=true
+-- !query schema
+struct<key:string,value:string>
+-- !query output
+spark.sql.groupByOrdinal true
+
+
-- !query
table courseSales
|> aggregate sum(earnings) group by rollup(course, `year`)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org