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,19 +116,7 @@ class ResolveReferencesInAggregate(val catalogManager: CatalogManager) extends S
groupExprs.map { g =>
g.transformWithPruning(_.containsPattern(UNRESOLVED_ATTRIBUTE)) {
case u: UnresolvedAttribute =>
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hack only works if the resolved integer literal is the root expression, which may not be true here.

Good catch!

// 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
}
selectList.find(ne => conf.resolver(ne.name, u.name)).getOrElse(u)
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,4 @@ 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"))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ org.apache.spark.sql.catalyst.ExtendedAnalysisException
}


-- !query
SELECT MAX(col1), 3 as abc FROM VALUES(1),(2),(3),(4) GROUP BY col1 % abc
-- !query analysis
Aggregate [(col1#x % 3)], [max(col1#x) AS max(col1)#x, 3 AS abc#x]
+- LocalRelation [col1#x]


-- !query
set spark.sql.groupByAliases=false
-- !query analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ SELECT a AS k, COUNT(non_existing) FROM testData GROUP BY k;
-- Aggregate functions cannot be used in GROUP BY
SELECT COUNT(b) AS k FROM testData GROUP BY k;

-- Ordinal is replaced correctly when grouping by alias of a literal
SELECT MAX(col1), 3 as abc FROM VALUES(1),(2),(3),(4) GROUP BY col1 % abc;

-- turn off group by aliases
set spark.sql.groupByAliases=false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ org.apache.spark.sql.catalyst.ExtendedAnalysisException
}


-- !query
SELECT MAX(col1), 3 as abc FROM VALUES(1),(2),(3),(4) GROUP BY col1 % abc
-- !query schema
struct<max(col1):int,abc:int>
-- !query output
2 3
3 3
4 3


-- !query
set spark.sql.groupByAliases=false
-- !query schema
Expand Down