-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-8992][SQL] Add pivot to dataframe api
This adds a pivot method to the dataframe api. Following the lead of cube and rollup this adds a Pivot operator that is translated into an Aggregate by the analyzer. Currently the syntax is like: ~~courseSales.pivot(Seq($"year"), $"course", Seq("dotNET", "Java"), sum($"earnings"))~~ ~~Would we be interested in the following syntax also/alternatively? and~~ courseSales.groupBy($"year").pivot($"course", "dotNET", "Java").agg(sum($"earnings")) //or courseSales.groupBy($"year").pivot($"course").agg(sum($"earnings")) Later we can add it to `SQLParser`, but as Hive doesn't support it we cant add it there, right? ~~Also what would be the suggested Java friendly method signature for this?~~ Author: Andrew Ray <ray.andrew@gmail.com> Closes #7841 from aray/sql-pivot.
- Loading branch information
Showing
6 changed files
with
255 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
sql/core/src/test/scala/org/apache/spark/sql/DataFramePivotSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql | ||
|
||
import org.apache.spark.sql.functions._ | ||
import org.apache.spark.sql.test.SharedSQLContext | ||
|
||
class DataFramePivotSuite extends QueryTest with SharedSQLContext{ | ||
import testImplicits._ | ||
|
||
test("pivot courses with literals") { | ||
checkAnswer( | ||
courseSales.groupBy($"year").pivot($"course", lit("dotNET"), lit("Java")) | ||
.agg(sum($"earnings")), | ||
Row(2012, 15000.0, 20000.0) :: Row(2013, 48000.0, 30000.0) :: Nil | ||
) | ||
} | ||
|
||
test("pivot year with literals") { | ||
checkAnswer( | ||
courseSales.groupBy($"course").pivot($"year", lit(2012), lit(2013)).agg(sum($"earnings")), | ||
Row("dotNET", 15000.0, 48000.0) :: Row("Java", 20000.0, 30000.0) :: Nil | ||
) | ||
} | ||
|
||
test("pivot courses with literals and multiple aggregations") { | ||
checkAnswer( | ||
courseSales.groupBy($"year").pivot($"course", lit("dotNET"), lit("Java")) | ||
.agg(sum($"earnings"), avg($"earnings")), | ||
Row(2012, 15000.0, 7500.0, 20000.0, 20000.0) :: | ||
Row(2013, 48000.0, 48000.0, 30000.0, 30000.0) :: Nil | ||
) | ||
} | ||
|
||
test("pivot year with string values (cast)") { | ||
checkAnswer( | ||
courseSales.groupBy("course").pivot("year", "2012", "2013").sum("earnings"), | ||
Row("dotNET", 15000.0, 48000.0) :: Row("Java", 20000.0, 30000.0) :: Nil | ||
) | ||
} | ||
|
||
test("pivot year with int values") { | ||
checkAnswer( | ||
courseSales.groupBy("course").pivot("year", 2012, 2013).sum("earnings"), | ||
Row("dotNET", 15000.0, 48000.0) :: Row("Java", 20000.0, 30000.0) :: Nil | ||
) | ||
} | ||
|
||
test("pivot courses with no values") { | ||
// Note Java comes before dotNet in sorted order | ||
checkAnswer( | ||
courseSales.groupBy($"year").pivot($"course").agg(sum($"earnings")), | ||
Row(2012, 20000.0, 15000.0) :: Row(2013, 30000.0, 48000.0) :: Nil | ||
) | ||
} | ||
|
||
test("pivot year with no values") { | ||
checkAnswer( | ||
courseSales.groupBy($"course").pivot($"year").agg(sum($"earnings")), | ||
Row("dotNET", 15000.0, 48000.0) :: Row("Java", 20000.0, 30000.0) :: Nil | ||
) | ||
} | ||
|
||
test("pivot max values inforced") { | ||
sqlContext.conf.setConf(SQLConf.DATAFRAME_PIVOT_MAX_VALUES, 1) | ||
intercept[RuntimeException]( | ||
courseSales.groupBy($"year").pivot($"course") | ||
) | ||
sqlContext.conf.setConf(SQLConf.DATAFRAME_PIVOT_MAX_VALUES, | ||
SQLConf.DATAFRAME_PIVOT_MAX_VALUES.defaultValue.get) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters