Skip to content

Commit

Permalink
Support pivoting using array column for pivot(column) API
Browse files Browse the repository at this point in the history
  • Loading branch information
HyukjinKwon committed Dec 19, 2018
1 parent 4b3fe3a commit 741ee69
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ object Literal {
case t: Timestamp => Literal(DateTimeUtils.fromJavaTimestamp(t), TimestampType)
case d: Date => Literal(DateTimeUtils.fromJavaDate(d), DateType)
case a: Array[Byte] => Literal(a, BinaryType)
case a: collection.mutable.WrappedArray[_] => apply(a.array)
case a: Array[_] =>
val elementType = componentTypeToDataType(a.getClass.getComponentType())
val dataType = ArrayType(elementType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
checkArrayLiteral(Array("a", "b", "c"))
checkArrayLiteral(Array(1.0, 4.0))
checkArrayLiteral(Array(CalendarInterval.MICROS_PER_DAY, CalendarInterval.MICROS_PER_HOUR))
val arr = collection.mutable.WrappedArray.make(Array(1.0, 4.0))
checkEvaluation(Literal(arr), toCatalyst(arr))
}

test("seq") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,15 @@ class DataFramePivotSuite extends QueryTest with SharedSQLContext {
}
assert(exception.getMessage.contains("Unsupported literal type"))
}

test("SPARK-26403: pivoting by array column") {
val df = Seq(
(2, Seq.empty[String]),
(2, Seq("a", "x")),
(3, Seq.empty[String]),
(3, Seq("a", "x"))).toDF("x", "s")
val expected = Seq((3, 1, 1), (2, 1, 1)).toDF
val actual = df.groupBy("x").pivot("s").count()
checkAnswer(actual, expected)
}
}

0 comments on commit 741ee69

Please sign in to comment.