Skip to content
Snippets Groups Projects
Commit 48778976 authored by Ryan Blue's avatar Ryan Blue Committed by Herman van Hovell
Browse files

[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 <blue@apache.org>

Closes #16107 from rdblue/SPARK-18677-fix-json-path.
parent 2f8776cc
No related branches found
No related tags found
No related merge requests found
...@@ -69,7 +69,7 @@ private[this] object JsonPathParser extends RegexParsers { ...@@ -69,7 +69,7 @@ private[this] object JsonPathParser extends RegexParsers {
// parse `.name` or `['name']` child expressions // parse `.name` or `['name']` child expressions
def named: Parser[List[PathInstruction]] = def named: Parser[List[PathInstruction]] =
for { for {
name <- '.' ~> "[^\\.\\[]+".r | "[\\'" ~> "[^\\'\\?]+" <~ "\\']" name <- '.' ~> "[^\\.\\[]+".r | "['" ~> "[^\\'\\?]+".r <~ "']"
} yield { } yield {
Key :: Named(name) :: Nil Key :: Named(name) :: Nil
} }
......
...@@ -43,6 +43,30 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper { ...@@ -43,6 +43,30 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
"""{"price":19.95,"color":"red"}""") """{"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") { test("$.store.book") {
checkEvaluation( checkEvaluation(
GetJsonObject(Literal(json), Literal("$.store.book")), GetJsonObject(Literal(json), Literal("$.store.book")),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment