This is an automated email from the ASF dual-hosted git repository.

gurwls223 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new fcf636d  [SPARK-37654][SQL] Fix NPE in Row.getSeq when field is Null
fcf636d is described below

commit fcf636d9eb8d645c24be3db2d599aba2d7e2955a
Author: Huaxin Gao <[email protected]>
AuthorDate: Fri Dec 17 13:43:24 2021 +0900

    [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]>
---
 sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala  | 5 ++++-
 sql/core/src/test/scala/org/apache/spark/sql/RowSuite.scala | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala
index 13c22a7..4f6c9a8 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/Row.scala
@@ -317,7 +317,10 @@ trait Row extends Serializable {
    *
    * @throws ClassCastException when data type does not match.
    */
-  def getSeq[T](i: Int): Seq[T] = getAs[scala.collection.Seq[T]](i).toSeq
+  def getSeq[T](i: Int): Seq[T] = {
+    val res = getAs[scala.collection.Seq[T]](i)
+    if (res != null) res.toSeq else null
+  }
 
   /**
    * Returns the value at position i of array type as `java.util.List`.
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/RowSuite.scala 
b/sql/core/src/test/scala/org/apache/spark/sql/RowSuite.scala
index fd9655f..9e8f3fb 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/RowSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/RowSuite.scala
@@ -105,4 +105,10 @@ class RowSuite extends SparkFunSuite with 
SharedSparkSession {
     val empty = Row()
     assert(empty.toString == "[]")
   }
+
+  test("SPARK-37654: row contains a null at the requested index should return 
null") {
+    assert(Row(Seq("value")).getSeq(0) === List("value"))
+    assert(Row(Seq()).getSeq(0) === List())
+    assert(Row(null).getSeq(0) === null)
+  }
 }

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to