Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -433,11 +433,12 @@ case class JsonTuple(children: Seq[Expression])
val row = Array.ofDim[Any](fieldNames.length)

// start reading through the token stream, looking for any requested field names
val fieldNamesWithIndex = fieldNames.zipWithIndex
while (parser.nextToken() != JsonToken.END_OBJECT) {
if (parser.getCurrentToken == JsonToken.FIELD_NAME) {
// check to see if this field is desired in the output
val idx = fieldNames.indexOf(parser.getCurrentName)
if (idx >= 0) {
val indices = fieldNamesWithIndex.filter(_._1 == parser.getCurrentName).map(_._2)
if (indices.nonEmpty) {
// it is, copy the child tree to the correct location in the output row
val output = new ByteArrayOutputStream()

Expand All @@ -447,7 +448,7 @@ case class JsonTuple(children: Seq[Expression])
generator => copyCurrentStructure(generator, parser)
}

row(idx) = UTF8String.fromBytes(output.toByteArray)
indices.foreach(idx => row(idx) = UTF8String.fromBytes(output.toByteArray))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
InternalRow(UTF8String.fromString("1"), null, UTF8String.fromString("2")))
}

test("SPARK-21804: json_tuple returns null values within repeated columns except the first one") {
checkJsonTuple(
JsonTuple(Literal("""{"f1": 1, "f2": 2}""") ::
NonFoldableLiteral("f1") ::
NonFoldableLiteral("cast(NULL AS STRING)") ::
NonFoldableLiteral("f1") ::
Nil),
InternalRow(UTF8String.fromString("1"), null, UTF8String.fromString("1")))
}

val gmtId = Option(DateTimeUtils.TimeZoneGMT.getID)

test("from_json") {
Expand Down