-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-55501][SQL] Fix listagg distinct + within group order by bug #54297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 14 commits
5068624
b4b5fd2
7b9e0ce
27c51ef
66806b6
1fd6675
22f5cbe
59b2b91
d5a67c8
819c066
6c3a2cc
cab7732
1b63b07
f5e7cec
30830ed
8aca7b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -449,9 +449,7 @@ trait CheckAnalysis extends LookupCatalog with QueryErrorsBase with PlanToString | |
| messageParameters = Map("funcName" -> toSQLExpr(w))) | ||
|
|
||
| case agg @ AggregateExpression(listAgg: ListAgg, _, _, _, _) | ||
| if agg.isDistinct && listAgg.needSaveOrderValue => | ||
| throw QueryCompilationErrors.functionAndOrderExpressionMismatchError( | ||
| listAgg.prettyName, listAgg.child, listAgg.orderExpressions) | ||
| if agg.isDistinct => listAgg.validateDistinctOrderCompatibility() | ||
|
||
|
|
||
| case w: WindowExpression => | ||
| WindowResolution.validateResolvedWindowExpression(w) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,7 +79,9 @@ class AggregateExpressionResolver( | |
| * `handleOuterAggregateExpression`); | ||
| * - Validation: | ||
| * 1. [[ListAgg]] is not allowed in DISTINCT aggregates if it contains [[SortOrder]] different | ||
| * from its child; | ||
| * from its child. However, when [[SQLConf.LISTAGG_ALLOW_DISTINCT_CAST_WITH_ORDER]] is | ||
| * enabled, a mismatch is tolerated if it is solely due to a [[Cast]] whose source type is | ||
| * injective when cast to string (see [[ListAgg.validateDistinctOrderCompatibility]]); | ||
| * 2. Nested aggregate functions are not allowed; | ||
| * 3. Nondeterministic expressions in the subtree of a related aggregate function are not | ||
| * allowed; | ||
|
|
@@ -113,26 +115,25 @@ class AggregateExpressionResolver( | |
| } | ||
| } | ||
|
|
||
| private def validateResolvedAggregateExpression(aggregateExpression: AggregateExpression): Unit = | ||
| private def validateResolvedAggregateExpression( | ||
| aggregateExpression: AggregateExpression): Unit = { | ||
| aggregateExpression match { | ||
| case agg @ AggregateExpression(listAgg: ListAgg, _, _, _, _) | ||
| if agg.isDistinct && listAgg.needSaveOrderValue => | ||
| throwFunctionAndOrderExpressionMismatchError(listAgg) | ||
| if agg.isDistinct => listAgg.validateDistinctOrderCompatibility() | ||
|
||
| case _ => | ||
| if (expressionResolutionContextStack.peek().hasAggregateExpressions) { | ||
| throwNestedAggregateFunction(aggregateExpression) | ||
| } | ||
|
|
||
| aggregateExpression.aggregateFunction.children.foreach { child => | ||
| if (!child.deterministic) { | ||
| throwAggregateFunctionWithNondeterministicExpression( | ||
| aggregateExpression, | ||
| child | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (expressionResolutionContextStack.peek().hasAggregateExpressions) { | ||
| throwNestedAggregateFunction(aggregateExpression) | ||
| } | ||
|
|
||
| aggregateExpression.aggregateFunction.children.foreach { child => | ||
| if (!child.deterministic) { | ||
| throwAggregateFunctionWithNondeterministicExpression(aggregateExpression, child) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * If the [[AggregateExpression]] has outer references in its subtree, we need to handle it in a | ||
| * special way. The whole process is explained in the [[SubqueryScope]] scaladoc, but in short | ||
|
|
@@ -212,14 +213,6 @@ class AggregateExpressionResolver( | |
| } | ||
| } | ||
|
|
||
| private def throwFunctionAndOrderExpressionMismatchError(listAgg: ListAgg) = { | ||
| throw QueryCompilationErrors.functionAndOrderExpressionMismatchError( | ||
| listAgg.prettyName, | ||
| listAgg.child, | ||
| listAgg.orderExpressions | ||
| ) | ||
| } | ||
|
|
||
| private def throwNestedAggregateFunction(aggregateExpression: AggregateExpression): Nothing = { | ||
| throw new AnalysisException( | ||
| errorClass = "NESTED_AGGREGATE_FUNCTION", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.