aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Blue <blue@apache.org>2016-12-02 08:41:40 -0800
committerHerman van Hovell <hvanhovell@databricks.com>2016-12-02 08:41:40 -0800
commit48778976e0566d9c93a8c900825def82c6b81fd6 (patch)
tree05ac39a104bc4581ba8ea8332242e85986949088
parent2f8776ccad532fbed17381ff97d302007918b8d8 (diff)
downloadspark-48778976e0566d9c93a8c900825def82c6b81fd6.tar.gz
spark-48778976e0566d9c93a8c900825def82c6b81fd6.tar.bz2
spark-48778976e0566d9c93a8c900825def82c6b81fd6.zip
[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.
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala2
-rw-r--r--sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala24
2 files changed, 25 insertions, 1 deletions
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 b61583d0da..667ff649d1 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
@@ -69,7 +69,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
}
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 3b0e90824b..618b8b29e8 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
@@ -43,6 +43,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")),