Skip to content

Commit fcf636d

Browse files
huaxingaoHyukjinKwon
authored andcommitted
[SPARK-37654][SQL] Fix NPE in Row.getSeq when field is Null
### What changes were proposed in this pull request? Fix NPE ``` scala> Row(null).getSeq(0) java.lang.NullPointerException at org.apache.spark.sql.Row.getSeq(Row.scala:319) at org.apache.spark.sql.Row.getSeq$(Row.scala:319) at org.apache.spark.sql.catalyst.expressions.GenericRow.getSeq(rows.scala:166) ``` ### Why are the changes needed? bug fixing ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? new UT Closes #34928 from huaxingao/npe. Authored-by: Huaxin Gao <[email protected]> Signed-off-by: Hyukjin Kwon <[email protected]>
1 parent e1c8cc1 commit fcf636d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,10 @@ trait Row extends Serializable {
317317
*
318318
* @throws ClassCastException when data type does not match.
319319
*/
320-
def getSeq[T](i: Int): Seq[T] = getAs[scala.collection.Seq[T]](i).toSeq
320+
def getSeq[T](i: Int): Seq[T] = {
321+
val res = getAs[scala.collection.Seq[T]](i)
322+
if (res != null) res.toSeq else null
323+
}
321324

322325
/**
323326
* Returns the value at position i of array type as `java.util.List`.

sql/core/src/test/scala/org/apache/spark/sql/RowSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,10 @@ class RowSuite extends SparkFunSuite with SharedSparkSession {
105105
val empty = Row()
106106
assert(empty.toString == "[]")
107107
}
108+
109+
test("SPARK-37654: row contains a null at the requested index should return null") {
110+
assert(Row(Seq("value")).getSeq(0) === List("value"))
111+
assert(Row(Seq()).getSeq(0) === List())
112+
assert(Row(null).getSeq(0) === null)
113+
}
108114
}

0 commit comments

Comments
 (0)