Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -486,14 +486,16 @@ class Analyzer(
case Pivot(groupByExprs, pivotColumn, pivotValues, aggregates, child) =>
val singleAgg = aggregates.size == 1
def outputName(value: Literal, aggregate: Expression): String = {
val scalaValue = CatalystTypeConverters.convertToScala(value.value, value.dataType)
val stringValue = Option(scalaValue).getOrElse("null").toString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The impact is not only on the data type timestamp. Any test case to cover null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, I thought https://github.com/HyukjinKwon/spark/blob/3c619dfb94723bd7a7d6a0811ab6329bf107f81b/sql/core/src/test/scala/org/apache/spark/sql/DataFramePivotSuite.scala#L220-L232 covers this.

Literal.toString handles null case before. If we remove Option(...).getOrElse("null") there, it throws NPE in those tests.

if (singleAgg) {
value.toString
stringValue
} else {
val suffix = aggregate match {
case n: NamedExpression => n.name
case _ => toPrettySQL(aggregate)
}
value + "_" + suffix
stringValue + "_" + suffix
}
}
if (aggregates.forall(a => PivotFirst.supportsDataType(a.dataType))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,13 @@ class DataFramePivotSuite extends QueryTest with SharedSQLContext{
.groupBy($"a").pivot("a").agg(min($"b")),
Row(null, Seq(null, 7), null) :: Row(1, null, Seq(1, 7)) :: Nil)
}

test("pivot with timestamp and count should not print internal representation") {
val timestamp = "2012-12-31 16:00:10.011"
val df = Seq(java.sql.Timestamp.valueOf(timestamp)).toDF("a").groupBy("a").pivot("a").count()
val expected = StructType(
StructField("a", TimestampType) ::
StructField(timestamp, LongType) :: Nil)
assert(df.schema == expected)
}
}