Repository: spark Updated Branches: refs/heads/branch-2.0 0758df6d5 -> 5f71d13ad
[SPARK-18677] Fix parsing ['key'] in JSON path expressions. ## What changes were proposed in this pull request? This fixes the parser rule to match named expressions, which doesn't work for two reasons: 1. The name match is not coerced to a regular expression (missing .r) 2. The surrounding literals are incorrect and attempt to escape a single quote, which is unnecessary ## How was this patch tested? This adds test cases for named expressions using the bracket syntax, including one with quoted spaces. Author: Ryan Blue <[email protected]> Closes #16107 from rdblue/SPARK-18677-fix-json-path. (cherry picked from commit 48778976e0566d9c93a8c900825def82c6b81fd6) Signed-off-by: Herman van Hovell <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/5f71d13a Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/5f71d13a Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/5f71d13a Branch: refs/heads/branch-2.0 Commit: 5f71d13adc6edaa0c182000bc0efcbd996d57a04 Parents: 0758df6 Author: Ryan Blue <[email protected]> Authored: Fri Dec 2 08:41:40 2016 -0800 Committer: Herman van Hovell <[email protected]> Committed: Fri Dec 2 08:42:09 2016 -0800 ---------------------------------------------------------------------- .../catalyst/expressions/jsonExpressions.scala | 2 +- .../expressions/JsonExpressionsSuite.scala | 24 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/spark/blob/5f71d13a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala ---------------------------------------------------------------------- diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala index c14a2fb..031c6ca 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala @@ -66,7 +66,7 @@ private[this] object JsonPathParser extends RegexParsers { // parse `.name` or `['name']` child expressions def named: Parser[List[PathInstruction]] = for { - name <- '.' ~> "[^\\.\\[]+".r | "[\\'" ~> "[^\\'\\?]+" <~ "\\']" + name <- '.' ~> "[^\\.\\[]+".r | "['" ~> "[^\\'\\?]+".r <~ "']" } yield { Key :: Named(name) :: Nil } http://git-wip-us.apache.org/repos/asf/spark/blob/5f71d13a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala ---------------------------------------------------------------------- diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala index 7b75409..9a550ca 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala @@ -41,6 +41,30 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { """{"price":19.95,"color":"red"}""") } + test("$['store'].bicycle") { + checkEvaluation( + GetJsonObject(Literal(json), Literal("$['store'].bicycle")), + """{"price":19.95,"color":"red"}""") + } + + test("$.store['bicycle']") { + checkEvaluation( + GetJsonObject(Literal(json), Literal("$.store['bicycle']")), + """{"price":19.95,"color":"red"}""") + } + + test("$['store']['bicycle']") { + checkEvaluation( + GetJsonObject(Literal(json), Literal("$['store']['bicycle']")), + """{"price":19.95,"color":"red"}""") + } + + test("$['key with spaces']") { + checkEvaluation(GetJsonObject( + Literal("""{ "key with spaces": "it works" }"""), Literal("$['key with spaces']")), + "it works") + } + test("$.store.book") { checkEvaluation( GetJsonObject(Literal(json), Literal("$.store.book")), --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
