Skip to content
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

[SPARK-8093] [SQL] Remove empty structs inferred from JSON documents #6799

Closed
wants to merge 1 commit into from
Closed
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
52 changes: 35 additions & 17 deletions sql/core/src/main/scala/org/apache/spark/sql/json/InferSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private[sql] object InferSchema {
}

// perform schema inference on each row and merge afterwards
schemaData.mapPartitions { iter =>
val rootType = schemaData.mapPartitions { iter =>
val factory = new JsonFactory()
iter.map { row =>
try {
Expand All @@ -55,8 +55,13 @@ private[sql] object InferSchema {
StructType(Seq(StructField(columnNameOfCorruptRecords, StringType)))
}
}
}.treeAggregate[DataType](StructType(Seq()))(compatibleRootType, compatibleRootType) match {
case st: StructType => nullTypeToStringType(st)
}.treeAggregate[DataType](StructType(Seq()))(compatibleRootType, compatibleRootType)

canonicalizeType(rootType) match {
case Some(st: StructType) => st
case _ =>
// canonicalizeType erases all empty structs, including the only one we want to keep
StructType(Seq())
}
}

Expand Down Expand Up @@ -116,22 +121,35 @@ private[sql] object InferSchema {
}
}

private def nullTypeToStringType(struct: StructType): StructType = {
val fields = struct.fields.map {
case StructField(fieldName, dataType, nullable, _) =>
val newType = dataType match {
case NullType => StringType
case ArrayType(NullType, containsNull) => ArrayType(StringType, containsNull)
case ArrayType(struct: StructType, containsNull) =>
ArrayType(nullTypeToStringType(struct), containsNull)
case struct: StructType => nullTypeToStringType(struct)
case other: DataType => other
}
/**
* Convert NullType to StringType and remove StructTypes with no fields
*/
private def canonicalizeType: DataType => Option[DataType] = {
case at@ArrayType(elementType, _) =>
for {
canonicalType <- canonicalizeType(elementType)
} yield {
at.copy(canonicalType)
}

StructField(fieldName, newType, nullable)
}
case StructType(fields) =>
val canonicalFields = for {
field <- fields
if field.name.nonEmpty
canonicalType <- canonicalizeType(field.dataType)
} yield {
field.copy(dataType = canonicalType)
}

if (canonicalFields.nonEmpty) {
Some(StructType(canonicalFields))
} else {
// per SPARK-8093: empty structs should be deleted
None
}

StructType(fields)
case NullType => Some(StringType)
case other => Some(other)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1103,4 +1103,8 @@ class JsonSuite extends QueryTest with TestJsonData {
}
}

test("SPARK-8093 Erase empty structs") {
val emptySchema = InferSchema(emptyRecords, 1.0, "")
assert(StructType(Seq()) === emptySchema)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,14 @@ trait TestJsonData {
"""{"b":"str_b_4", "a":"str_a_4", "c":"str_c_4"}""" ::
"""]""" :: Nil)

def emptyRecords: RDD[String] =
ctx.sparkContext.parallelize(
"""{""" ::
"""""" ::
"""{"a": {}}""" ::
"""{"a": {"b": {}}}""" ::
"""{"b": [{"c": {}}]}""" ::
"""]""" :: Nil)

def empty: RDD[String] = ctx.sparkContext.parallelize(Seq[String]())
}