Skip to content

Commit

Permalink
possible fix for grouping on nested fields
Browse files Browse the repository at this point in the history
  • Loading branch information
marmbrus committed Oct 5, 2014
1 parent cf1d32e commit 8b06fdc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Analyzer(catalog: Catalog, registry: FunctionRegistry, caseSensitive: Bool
ResolveFunctions ::
GlobalAggregates ::
UnresolvedHavingClauseAttributes ::
TrimAliases ::
typeCoercionRules ++
extendedRules : _*),
Batch("Check Analysis", Once,
Expand Down Expand Up @@ -88,6 +89,21 @@ class Analyzer(catalog: Catalog, registry: FunctionRegistry, caseSensitive: Bool
}
}

/**
* Removes Alias expressions
*/
object TrimAliases extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case Aggregate(groups, aggs, child) =>
Aggregate(
groups.map {_ transform {
case Alias(c, _) => c
} },
aggs,
child)
}
}

/**
* Replaces [[UnresolvedRelation]]s with concrete relations from the catalog.
*/
Expand Down
17 changes: 17 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
TimeZone.setDefault(origZone)
}

test("grouping on nested fields") {
jsonRDD(sparkContext.parallelize("""{"nested": {"attribute": 1}, "value": 2}""" :: Nil))
.registerTempTable("rows")

checkAnswer(
sql(
"""
|select attribute, sum(cnt)
|from (
| select nested.attribute, count(*) as cnt
| from rows
| group by nested.attribute) a
|group by attribute
""".stripMargin),
Row(1, 1) :: Nil)
}


test("SPARK-3176 Added Parser of SQL ABS()") {
checkAnswer(
Expand Down

0 comments on commit 8b06fdc

Please sign in to comment.