Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -436,7 +436,8 @@ case class JsonTuple(children: Seq[Expression])
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)
val jsonField = parser.getCurrentName
var idx = fieldNames.indexOf(jsonField)
if (idx >= 0) {
// it is, copy the child tree to the correct location in the output row
val output = new ByteArrayOutputStream()
Expand All @@ -447,7 +448,18 @@ case class JsonTuple(children: Seq[Expression])
generator => copyCurrentStructure(generator, parser)
}

row(idx) = UTF8String.fromBytes(output.toByteArray)
val jsonValue = UTF8String.fromBytes(output.toByteArray)
row(idx) = jsonValue
idx = idx + 1

// SPARK-21804: json_tuple returns null values within repeated columns
// except the first one; so that we need to check the remaining fields.
while (idx < fieldNames.length) {
if (fieldNames(idx) == jsonField) {
row(idx) = jsonValue
}
idx = idx + 1
}

@gatorsmile gatorsmile Aug 24, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you rewrite it using less lines? A more Scala way?

@viirya viirya Aug 24, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have followed @HyukjinKwon's suggestion #19017 (review) to avoid functional transformation with a while-loop, since this is a hot path. It makes sense to me.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You still can simplify the codes a lot without functional transformation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If I comment out the L451-452, the repeated fields still have the same jsonValue because fieldNames(idx) == jsonField, but the first comparison is not necessary since idx >= 0 means matched.

Could you please give me some advice?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you maybe have a suggestion? The current status looks fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

            row(idx) = jsonValue
            idx = idx + 1

            // SPARK-21804: json_tuple returns null values within repeated columns
            // except the first one; so that we need to check the remaining fields.
            while (idx < fieldNames.length) {
              if (fieldNames(idx) == jsonField) {
                row(idx) = jsonValue
              }
              idx = idx + 1
            }

->

            do {
              row(idx) = jsonValue
              idx = fieldNames.indexOf(jsonField, idx + 1)
            } while (idx >= 0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am also thinking if we should use a Hash table. However,,, the number of columns is not large. Thus, it might not get a noticeable benefit.

}
}
}
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