-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-21677][SQL] json_tuple throws NullPointException when column is null as string type #18930
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
Changes from 4 commits
596c280
796041f
f07a9f7
ffa575a
5d71263
0078445
5c69df5
ab16929
e0e0c74
5191ed4
ff3b9da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -361,10 +361,18 @@ case class JsonTuple(children: Seq[Expression]) | |
| // the fields to query are the remaining children | ||
| @transient private lazy val fieldExpressions: Seq[Expression] = children.tail | ||
|
|
||
| // a field name given with constant null will be replaced with this pseudo field name | ||
| private val nullFieldName = "__NullFieldName" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jmchung, could we maybe compute this foldable related optimization ahead - I think we can make a function for the above codes first and then use it for computation for each row. Did I understand correctly? I tried a rough version I thought - https://github.com/jmchung/spark/compare/SPARK-21677...HyukjinKwon:tmp-18930?expand=1, @viirya what do you think about this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I've also considered using Option here. But don't want to come out Option version from me first, so we can experience review process. It looks good to me.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @HyukjinKwon @viirya Yep, we've discarded the fake field name and use Option here. We made a slight revision to deal with the None in |
||
|
|
||
| // eagerly evaluate any foldable the field names | ||
| @transient private lazy val foldableFieldNames: IndexedSeq[String] = { | ||
| fieldExpressions.map { | ||
| case expr if expr.foldable => expr.eval().asInstanceOf[UTF8String].toString | ||
| case expr if expr.foldable => | ||
| if (expr.eval() == null) { | ||
| nullFieldName | ||
| } else { | ||
| expr.eval().asInstanceOf[UTF8String].toString | ||
| } | ||
| case _ => null | ||
| }.toIndexedSeq | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2034,4 +2034,13 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData { | |
| } | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-21677: json_tuple throws NullPointException when column is null as string type") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we move this to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an end-to-end test case. We also need to add unit test cases in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The end-to-end test at L2047 may not be able to move to It is also good to have this end-to-end tests in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gatorsmile has added unit test case in |
||
| checkAnswer(sql( | ||
| """ | ||
| |SELECT json_tuple('{"a" : 1, "b" : 2}' | ||
| |, cast(NULL AS STRING), 'b' | ||
| |, cast(NULL AS STRING), 'a') | ||
| """.stripMargin), Row(null, "2", null, "1")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jmchung Can we also add the test we discussed in slack which mixes constant field name and non constant one?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @viirya Done, the added test case contains column name, constant field name, and null field name.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: move
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, thanks |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A field name given with constant null will be replaced with this pseudo field name.