Skip to content

Commit

Permalink
[SPARK-3800][SQL] Clean aliases from grouping expressions
Browse files Browse the repository at this point in the history
Author: Michael Armbrust <[email protected]>

Closes #2658 from marmbrus/nestedAggs and squashes the following commits:

862b763 [Michael Armbrust] Merge remote-tracking branch 'origin/master' into nestedAggs
3234521 [Michael Armbrust] Merge remote-tracking branch 'origin/master' into nestedAggs
8b06fdc [Michael Armbrust] possible fix for grouping on nested fields
  • Loading branch information
marmbrus committed Oct 20, 2014
1 parent 1b3ce61 commit e9c1afa
Show file tree
Hide file tree
Showing 2 changed files with 35 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 @@ -89,6 +90,23 @@ class Analyzer(catalog: Catalog, registry: FunctionRegistry, caseSensitive: Bool
}
}

/**
* Removes no-op Alias expressions from the plan.
*/
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)
}
}

/**
* Checks for non-aggregated attributes with aggregation
*/
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 @@ -43,6 +43,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(
sql("SELECT ABS(-1.3)"),
Expand Down

0 comments on commit e9c1afa

Please sign in to comment.