GitHub user HyukjinKwon opened a pull request:
https://github.com/apache/spark/pull/17224
[SPARK-19882][SQL] Pivot with null as the pivot value throws NPE
## What changes were proposed in this pull request?
This PR fixes two problems as below:
Actually, there are two problems here.
**It should not throw an exception**
An optimisation to this was introduced in order to prevent equality
comparison per row which seems making it slow when `pivotValues` are too many
up to my understanding.
It seems this tightly assumes that pivot value can't be `null`. I could not
find a clean and easy workaround to support this and wonder if it is worth.
Please guide me if anyone knows a clean and short way to fix it in this two
aggregation path.
**Fix to count `null`**
```scala
Seq(Tuple1(None), Tuple1(Some(1))).toDF("a").groupBy($"a").count().show()
```
Before (Spark 1.6),
```
+----+----+---+
| a|null| 1|
+----+----+---+
|null| 0| 0|
| 1| 0| 1|
+----+----+---+
```
Before (current master),
```
java.lang.NullPointerException was thrown.
java.lang.NullPointerException
at
org.apache.spark.sql.catalyst.expressions.aggregate.PivotFirst$$anonfun$4.apply(PivotFirst.scala:145)
at
org.apache.spark.sql.catalyst.expressions.aggregate.PivotFirst$$anonfun$4.apply(PivotFirst.scala:143)
at scala.collection.immutable.List.map(List.scala:273)
at
org.apache.spark.sql.catalyst.expressions.aggregate.PivotFirst.<init>(PivotFirst.scala:143)
at
org.apache.spark.sql.catalyst.analysis.Analyzer$ResolvePivot$$anonfun$apply$7$$anonfun$24.apply(Analyzer.scala:509)
```
After,
```
+----+----+---+
| a|null| 1|
+----+----+---+
|null| 1| 0|
| 1| 0| 1|
+----+----+---+
```
It seems we should count null given
```scala
Seq(Tuple1(None), Tuple1(Some(1))).toDF("a").groupBy($"a").count().show()
```
```
+----+-----+
| a|count|
+----+-----+
|null| 1|
| 1| 1|
+----+-----+
```
## How was this patch tested?
Unit tests in `DataFramePivotSuite`.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/HyukjinKwon/spark SPARK-19882
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/spark/pull/17224.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #17224
----
commit 0476565d893098112d853a9f5747a94799a11ae2
Author: hyukjinkwon <[email protected]>
Date: 2017-03-09T12:18:49Z
Pivot with null as the pivot value throws NPE
----
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]