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 @@ -2553,8 +2553,10 @@ class Analyzer(override val catalogManager: CatalogManager)
// a table `t` has two columns `c1` and `c2`, for query `SELECT ... FROM t
// GROUP BY c1 HAVING c2 = 0`, even though we can resolve column `c2` here, we
// should undo it later and fail with "Column c2 not found".
agg.child.resolve(u.nameParts, resolver).map(TempResolvedColumn(_, u.nameParts))
.getOrElse(u)
agg.child.resolve(u.nameParts, resolver).map({
Copy link
Member

Choose a reason for hiding this comment

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

Let's use a single brace next time for map in this case: https://github.com/databricks/scala-style-guide#anonymous-methods

case a: Alias => TempResolvedColumn(a.child, u.nameParts)
case o => TempResolvedColumn(o, u.nameParts)
}).getOrElse(u)
} catch {
case _: AnalysisException => u
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1115,4 +1115,30 @@ class AnalysisSuite extends AnalysisTest with Matchers {
Seq("grouping_id() can only be used with GroupingSets/Cube/Rollup"),
false)
}

test("SPARK-36275: Resolve aggregate functions should work with nested fields") {
assertAnalysisSuccess(parsePlan(
"""
|SELECT c.x, SUM(c.y)
|FROM VALUES NAMED_STRUCT('x', 'A', 'y', 1), NAMED_STRUCT('x', 'A', 'y', 2) AS t(c)
|GROUP BY c.x
|HAVING c.x > 1
|""".stripMargin))

assertAnalysisSuccess(parsePlan(
"""
|SELECT c.x, SUM(c.y)
|FROM VALUES NAMED_STRUCT('x', 'A', 'y', 1), NAMED_STRUCT('x', 'A', 'y', 2) AS t(c)
|GROUP BY c.x
|ORDER BY c.x
|""".stripMargin))

assertAnalysisError(parsePlan(
"""
|SELECT c.x
|FROM VALUES NAMED_STRUCT('x', 'A', 'y', 1), NAMED_STRUCT('x', 'A', 'y', 2) AS t(c)
|GROUP BY c.x
|ORDER BY c.x + c.y
|""".stripMargin), "cannot resolve 'c.y' given input columns: [x]" :: Nil)
}
}