-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-45920][SQL] group by ordinal should be idempotent #43797
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
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -1999,7 +1999,18 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor | |
| throw QueryCompilationErrors.groupByPositionRefersToAggregateFunctionError( | ||
| index, ordinalExpr) | ||
| } else { | ||
| ordinalExpr | ||
| trimAliases(ordinalExpr) match { | ||
| // HACK ALERT: If the ordinal expression is also an integer literal, don't use it | ||
| // but still keep the ordinal literal. The reason is we may repeatedly | ||
| // analyze the plan. Using a different integer literal may lead to | ||
| // a repeat GROUP BY ordinal resolution which is wrong. GROUP BY | ||
| // constant is meaningless so whatever value does not matter here. | ||
| // TODO: GROUP BY ordinal should pull out grouping expressions to a Project, then | ||
|
||
| // the resolved ordinal expression is always `AttributeReference`. | ||
| case Literal(_: Int, IntegerType) => | ||
| Literal(index) | ||
|
Comment on lines
+2011
to
+2012
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this only for IntegerType? If
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm only fixing repeated analysis (analyzing an analyzed plan). It's a much bigger topic to support analyzing an optimized plan, so constant folding is not considered here. |
||
| case _ => ordinalExpr | ||
| } | ||
| } | ||
| } else { | ||
| throw QueryCompilationErrors.groupByPositionRangeError(index, aggs.size) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,11 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.TestRelations.testRelation2 | ||
| import org.apache.spark.sql.catalyst.analysis.TestRelations.{testRelation, testRelation2} | ||
| import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
| import org.apache.spark.sql.catalyst.dsl.plans._ | ||
| import org.apache.spark.sql.catalyst.expressions.Literal | ||
| import org.apache.spark.sql.catalyst.expressions.{GenericInternalRow, Literal} | ||
| import org.apache.spark.sql.catalyst.plans.logical.LocalRelation | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
| class SubstituteUnresolvedOrdinalsSuite extends AnalysisTest { | ||
|
|
@@ -67,4 +68,22 @@ class SubstituteUnresolvedOrdinalsSuite extends AnalysisTest { | |
| testRelation2.groupBy(Literal(1), Literal(2))($"a", $"b")) | ||
| } | ||
| } | ||
|
|
||
| test("group by ordinal repeated analysis") { | ||
|
||
| val plan = testRelation.groupBy(Literal(1))(Literal(100).as("a")).analyze | ||
| comparePlans( | ||
| plan, | ||
| testRelation.groupBy(Literal(1))(Literal(100).as("a")) | ||
| ) | ||
|
|
||
| val testRelationWithData = testRelation.copy(data = Seq(new GenericInternalRow(Array(1: Any)))) | ||
| // Copy the plan to reset its `analyzed` flag, so that analyzer rules will re-apply. | ||
| val copiedPlan = plan.transform { | ||
| case _: LocalRelation => testRelationWithData | ||
| } | ||
| comparePlans( | ||
| copiedPlan.analyze, // repeated analysis | ||
| testRelationWithData.groupBy(Literal(1))(Literal(100).as("a")) | ||
| ) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is a temporary fix only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to backport this surgical fix, and only do the refactoring in the master branch. So yes, this is a temporary fix for the master branch.