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 13c22a7e9202f..4f6c9a8c703e3 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 fd9655fdbef42..9e8f3fbeaef44 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) + } }