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 @@ -116,7 +116,19 @@ class ResolveReferencesInAggregate(val catalogManager: CatalogManager) extends S
groupExprs.map { g =>
g.transformWithPruning(_.containsPattern(UNRESOLVED_ATTRIBUTE)) {
case u: UnresolvedAttribute =>
selectList.find(ne => conf.resolver(ne.name, u.name)).getOrElse(u)
val (result, index) =
selectList.zipWithIndex.find(ne => conf.resolver(ne._1.name, u.name))
.getOrElse((u, -1))

trimAliases(result) match {
// HACK ALERT: If the expanded grouping expression is an integer literal, don't use it
// but use an integer literal of the index. The reason is we may
// repeatedly analyze the plan, and the original integer literal may cause
// failures with a later GROUP BY ordinal resolution. GROUP BY constant is
// meaningless so whatever value does not matter here.
case IntegerLiteral(_) => Literal(index + 1)
case _ => result
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,22 @@ class SubstituteUnresolvedOrdinalsSuite extends AnalysisTest {
testRelationWithData.groupBy(Literal(1))(Literal(100).as("a"))
)
}

test("SPARK-47895: group by alias repeated analysis") {
val plan = testRelation.groupBy($"b")(Literal(100).as("b")).analyze
comparePlans(
plan,
testRelation.groupBy(Literal(1))(Literal(100).as("b"))
)

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("b"))
)
}
}