Skip to content
Snippets Groups Projects
Commit b72486f8 authored by Andrew Ray's avatar Andrew Ray Committed by Herman van Hovell
Browse files

[SPARK-17458][SQL] Alias specified for aggregates in a pivot are not honored

## What changes were proposed in this pull request?

This change preserves aliases that are given for pivot aggregations

## How was this patch tested?

New unit test

Author: Andrew Ray <ray.andrew@gmail.com>

Closes #15111 from aray/SPARK-17458.
parent 1202075c
No related branches found
No related tags found
No related merge requests found
......@@ -373,7 +373,15 @@ class Analyzer(
case Pivot(groupByExprs, pivotColumn, pivotValues, aggregates, child) =>
val singleAgg = aggregates.size == 1
def outputName(value: Literal, aggregate: Expression): String = {
if (singleAgg) value.toString else value + "_" + aggregate.sql
if (singleAgg) {
value.toString
} else {
val suffix = aggregate match {
case n: NamedExpression => n.name
case _ => aggregate.sql
}
value + "_" + suffix
}
}
if (aggregates.forall(a => PivotFirst.supportsDataType(a.dataType))) {
// Since evaluating |pivotValues| if statements for each input row can get slow this is an
......
......@@ -197,4 +197,15 @@ class DataFramePivotSuite extends QueryTest with SharedSQLContext{
Row(2013, Seq(48000.0, 7.0), Seq(30000.0, 7.0)) :: Nil
)
}
test("pivot preserves aliases if given") {
assertResult(
Array("year", "dotNET_foo", "dotNET_avg(`earnings`)", "Java_foo", "Java_avg(`earnings`)")
)(
courseSales.groupBy($"year")
.pivot("course", Seq("dotNET", "Java"))
.agg(sum($"earnings").as("foo"), avg($"earnings")).columns
)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment