Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ case class JsonToStruct(schema: StructType, options: Map[String, String], child:
override def dataType: DataType = schema

override def nullSafeEval(json: Any): Any = {
try parser.parse(json.toString).head catch {
try parser.parse(json.toString).headOption.orNull catch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not for this PR but maybe loosely related I guess) I was thinking it is a bit odd that we support to only read the single row when it is a json array. It seems, for example,

import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._
val schema = StructType(StructField("a", IntegerType) :: Nil)
Seq(("""[{"a": 1}, {"a": 2}]""")).toDF("struct").select(from_json(col("struct"), schema)).show()
+--------------------+
|jsontostruct(struct)|
+--------------------+
|                 [1]|
+--------------------+

I think maybe we should not support this in that function or it should work like a generator expression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HyukjinKwon that seems fair. Feel free to work in this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for confirming.

case _: SparkSQLJsonProcessingException => null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
)
}

test("SPARK-19543: from_json empty input column") {
val schema = StructType(StructField("a", IntegerType) :: Nil)
checkEvaluation(
JsonToStruct(schema, Map.empty, Literal.create(" ", StringType)),
null
)
}

test("to_json") {
val schema = StructType(StructField("a", IntegerType) :: Nil)
val struct = Literal.create(create_row(1), schema)
Expand Down