Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2229,11 +2229,18 @@ class Analyzer(
private def resolvePipeAggregateExpressionOrdinal(
expr: NamedExpression,
inputs: Seq[Attribute]): NamedExpression = expr match {
case UnresolvedPipeAggregateOrdinal(index) =>
case ordinal @ UnresolvedPipeAggregateOrdinal(index) =>
// In this case, the user applied the SQL pipe aggregate operator ("|> AGGREGATE") and used
// ordinals in its GROUP BY clause. This expression then refers to the i-th attribute of the
// child operator (one-based). Here we resolve the ordinal to the corresponding attribute.
inputs(index - 1)
// child operator (one-based). Here we resolve the ordinal to the corresponding attribute, or
// throw GROUP_BY_POS_OUT_OF_RANGE if it is outside the range of the child's attributes.
withPosition(ordinal) {
if (index > 0 && index <= inputs.size) {
inputs(index - 1)
} else {
throw QueryCompilationErrors.groupByPositionRangeError(index, inputs.size)
}
}
case other =>
other
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7913,9 +7913,12 @@ class AstBuilder extends DataTypeAstBuilder
// grouping and aggregate expressions, respectively. This will let the
// [[ResolveOrdinalInOrderByAndGroupBy]] rule detect the ordinal in the aggregate list
// and replace it with the corresponding attribute from the child operator.
case UnresolvedOrdinal(v: Int) =>
case ordinal @ UnresolvedOrdinal(v: Int) =>
newGroupingExpressions += UnresolvedOrdinal(newAggregateExpressions.length + 1)
newAggregateExpressions += UnresolvedPipeAggregateOrdinal(v)
// Preserve the ordinal's origin so an out-of-range error points at the position
// itself, not the whole pipe statement (matching regular GROUP BY).
newAggregateExpressions +=
CurrentOrigin.withOrigin(ordinal.origin) { UnresolvedPipeAggregateOrdinal(v) }
case e: Expression =>
newGroupingExpressions += e
newAggregateExpressions += UnresolvedAlias(e, None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3492,6 +3492,50 @@ org.apache.spark.sql.catalyst.parser.ParseException
}


-- !query
select 3 as x, 4 as y
|> aggregate sum(y) group by 5
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "GROUP_BY_POS_OUT_OF_RANGE",
"sqlState" : "42805",
"messageParameters" : {
"index" : "5",
"size" : "2"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 52,
"stopIndex" : 52,
"fragment" : "5"
} ]
}


-- !query
select 3 as x, 4 as y
|> aggregate sum(y) group by 0
-- !query analysis
org.apache.spark.sql.AnalysisException
{
"errorClass" : "GROUP_BY_POS_OUT_OF_RANGE",
"sqlState" : "42805",
"messageParameters" : {
"index" : "0",
"size" : "2"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 52,
"stopIndex" : 52,
"fragment" : "0"
} ]
}


-- !query
table courseSales
|> aggregate sum(earnings) group by rollup(course, `year`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,14 @@ table other
select 3 as x, 4 as y
|> aggregate group by all;

-- The AGGREGATE GROUP BY ordinal position is out of range.
select 3 as x, 4 as y
|> aggregate sum(y) group by 5;

-- The AGGREGATE GROUP BY ordinal position is not positive.
select 3 as x, 4 as y
|> aggregate sum(y) group by 0;

-- GROUP BY ROLLUP is not supported yet.
table courseSales
|> aggregate sum(earnings) group by rollup(course, `year`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3109,6 +3109,54 @@ org.apache.spark.sql.catalyst.parser.ParseException
}


-- !query
select 3 as x, 4 as y
|> aggregate sum(y) group by 5
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "GROUP_BY_POS_OUT_OF_RANGE",
"sqlState" : "42805",
"messageParameters" : {
"index" : "5",
"size" : "2"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 52,
"stopIndex" : 52,
"fragment" : "5"
} ]
}


-- !query
select 3 as x, 4 as y
|> aggregate sum(y) group by 0
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.AnalysisException
{
"errorClass" : "GROUP_BY_POS_OUT_OF_RANGE",
"sqlState" : "42805",
"messageParameters" : {
"index" : "0",
"size" : "2"
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 52,
"stopIndex" : 52,
"fragment" : "0"
} ]
}


-- !query
table courseSales
|> aggregate sum(earnings) group by rollup(course, `year`)
Expand Down