-
Notifications
You must be signed in to change notification settings - Fork 28.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-25048][SQL] Pivoting by multiple columns in Scala/Java #22316
Changes from 4 commits
0580725
1221db3
a097b29
ef8e22a
673ef00
8ccf845
382640b
49b47fb
4daeb92
d645d06
43972ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import org.apache.spark.sql.catalyst.expressions.aggregate._ | |
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.util.toPrettySQL | ||
import org.apache.spark.sql.execution.aggregate.TypedAggregateExpression | ||
import org.apache.spark.sql.functions.lit | ||
import org.apache.spark.sql.internal.SQLConf | ||
import org.apache.spark.sql.types.{NumericType, StructType} | ||
|
||
|
@@ -406,6 +407,14 @@ class RelationalGroupedDataset protected[sql]( | |
* df.groupBy($"year").pivot($"course", Seq("dotNET", "Java")).sum($"earnings") | ||
* }}} | ||
* | ||
* For pivoting by multiple columns, use the `struct` function to combine the columns and values: | ||
* | ||
* {{{ | ||
* df.groupBy($"year") | ||
* .pivot(struct($"course", $"training"), Seq(struct(lit("java"), lit("Experts")))) | ||
* .agg(sum($"earnings")) | ||
* }}} | ||
* | ||
* @param pivotColumn the column to pivot. | ||
* @param values List of values that will be translated to columns in the output DataFrame. | ||
* @since 2.4.0 | ||
|
@@ -416,7 +425,7 @@ class RelationalGroupedDataset protected[sql]( | |
new RelationalGroupedDataset( | ||
df, | ||
groupingExprs, | ||
RelationalGroupedDataset.PivotType(pivotColumn.expr, values.map(Literal.apply))) | ||
RelationalGroupedDataset.PivotType(pivotColumn.expr, values.map(lit(_).expr))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't see any advantages of this. It is longer and slower. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MaxGekk, just for doubly doubly sure, shell we There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Could you explain, please. Why do you expect some behavior change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. now we eventually call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from a quick look, seems There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true in general but specifically is decimal precision more correct? |
||
case _: RelationalGroupedDataset.PivotType => | ||
throw new UnsupportedOperationException("repeated pivots are not supported") | ||
case _ => | ||
|
@@ -561,5 +570,5 @@ private[sql] object RelationalGroupedDataset { | |
/** | ||
* To indicate it's the PIVOT | ||
*/ | ||
private[sql] case class PivotType(pivotCol: Expression, values: Seq[Literal]) extends GroupType | ||
private[sql] case class PivotType(pivotCol: Expression, values: Seq[Expression]) extends GroupType | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -308,4 +308,27 @@ class DataFramePivotSuite extends QueryTest with SharedSQLContext { | |||
|
||||
assert(exception.getMessage.contains("aggregate functions are not allowed")) | ||||
} | ||||
|
||||
test("pivoting column list with values") { | ||||
val expected = Row(2012, 10000.0, null) :: Row(2013, 48000.0, 30000.0) :: Nil | ||||
val df = trainingSales | ||||
.groupBy($"sales.year") | ||||
.pivot(struct(lower($"sales.course"), $"training"), Seq( | ||||
struct(lit("dotnet"), lit("Experts")), | ||||
struct(lit("java"), lit("Dummies"))) | ||||
).agg(sum($"sales.earnings")) | ||||
|
||||
checkAnswer(df, expected) | ||||
} | ||||
|
||||
test("pivoting column list") { | ||||
val exception = intercept[RuntimeException] { | ||||
trainingSales | ||||
.groupBy($"sales.year") | ||||
.pivot(struct(lower($"sales.course"), $"training")) | ||||
.agg(sum($"sales.earnings")) | ||||
.collect() | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My changes don't throw the exception. It is thrown in the collect() : spark/sql/core/src/main/scala/org/apache/spark/sql/RelationalGroupedDataset.scala Line 385 in 41c2227
@maropu Do you propose to catch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried in your branch;
I miss something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, you don't. The exception for sure is thrown inside of
@maropu Could you explain, please, why do you think Just in case, in the PR, I don't aim to change behavior of existing method: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think invalid queries basically throw `AnalysisException. But, yea, indeed, we'd better to keep the current behaivour. Thanks! |
||||
} | ||||
assert(exception.getMessage.contains("Unsupported literal type")) | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the documentation states it's an overloaded version of
the `pivot` method with `pivotColumn` of the `String` type.
, shall we move this contents to that method?Also, I would document this, for instance,
From Spark 2.4.0, values can be literal columns, for instance,
struct
. For pivoting by multiple columns, use thestruct
function to combine the columns and values.