Skip to content

Support null complex values in arrays and maps #2432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -133,9 +133,13 @@ class DataFrameValueWriter(writeUnknownTypes: Boolean = false) extends Filtering
generator.writeBeginArray()
if (value != null) {
value.foreach { v =>
val result = write(schema, v, generator)
if (!result.isSuccesful()) {
return handleUnknown(value, generator)
if (v == null) {
generator.writeNull()
} else {
val result = write(schema, v, generator)
if (!result.isSuccesful()) {
return handleUnknown(value, generator)
}
}
}
}
Expand All @@ -159,9 +163,13 @@ class DataFrameValueWriter(writeUnknownTypes: Boolean = false) extends Filtering
for ((k, v) <- value) {
if (shouldKeep(generator.getParentPath(), k.toString())) {
generator.writeFieldName(k.toString)
val result = write(schema.valueType, v, generator)
if (!result.isSuccesful()) {
return handleUnknown(v, generator)
if (v == null) {
generator.writeNull()
} else {
val result = write(schema.valueType, v, generator)
if (!result.isSuccesful()) {
return handleUnknown(v, generator)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,31 @@ class DataFrameValueWriterTest {
}
}

@Test
def testNullStructInArray(): Unit = {
val schema = StructType(Seq(StructField("s", ArrayType(StructType(Seq(StructField("a", StringType)))))))
val row = Row(Array(null))
assertEquals("""{"s":[null]}""", serialize(row, schema))
}

@Test
def testNullStructInMap(): Unit = {
val schema = StructType(Seq(StructField("s", MapType(StringType, StructType(Seq(StructField("b", StringType)))))))
val row = Row(Map("a" -> null))
assertEquals("""{"s":{"a":null}}""", serialize(row, schema))
}

@Test
def testNullNestedArray(): Unit = {
val schema = StructType(Seq(StructField("s", ArrayType(ArrayType(StringType)))))
val row = Row(Array(null))
assertEquals("""{"s":[null]}""", serialize(row, schema))
}

@Test
def testNullNestedMap(): Unit = {
val schema = StructType(Seq(StructField("s", ArrayType(MapType(StringType, StringType)))))
val row = Row(Array(null))
assertEquals("""{"s":[null]}""", serialize(row, schema))
}
}