dongjoon-hyun commented on a change in pull request #27759: [SPARK-31008][SQL]
Support json_array_length function
URL: https://github.com/apache/spark/pull/27759#discussion_r400594807
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
##########
@@ -791,4 +791,30 @@ class JsonExpressionsSuite extends SparkFunSuite with
ExpressionEvalHelper with
checkDecimalInfer(_, """struct<d:decimal(7,3)>""")
}
}
+
+ test("Length of JSON array") {
+ val null_json_array = """"""
+ val simple_json_array = """[1,2,3]"""
+ val empty_json_array = """[]"""
+ val json_array_of_array = """[[1],[2,3],[]]"""
+ val json_array_of_objects = """[{"a":123},{"b":"hello"}]"""
+ val complex_json_array = """[1,2,3,[33,44],{"key":[2,3,4]}]"""
+ val not_a_json_array = """{"key":"not a json array"}"""
+ val invalid_json_array = """[1,2,3,4,5"""
+
+ checkEvaluation(LengthOfJsonArray(Literal(null_json_array)), null)
+ checkEvaluation(LengthOfJsonArray(Literal(simple_json_array)), 3)
+ checkEvaluation(LengthOfJsonArray(Literal(empty_json_array)), 0)
+ checkEvaluation(LengthOfJsonArray(Literal(json_array_of_array)), 3)
+ checkEvaluation(LengthOfJsonArray(Literal(json_array_of_objects)), 2)
+ checkEvaluation(LengthOfJsonArray(Literal(complex_json_array)), 5)
+ checkEvaluation(LengthOfJsonArray(Literal(invalid_json_array)), null)
Review comment:
Shall we use a shorter pattern?
```scala
Seq(
("", null),
("[]", 0),
("[1,2,3]", 3),
("[[1],[2,3],[]]", 3),
("""[{"a":123},{"b":"hello"}]""", 2),
("""[1,2,3,[33,44],{"key":[2,3,4]}]""", 5),
("""[1,2,3,4,5""", null)
).foreach { case (literal, expectedValue) =>
checkEvaluation(LengthOfJsonArray(Literal(literal)), expectedValue)
}
val not_a_json_array = """{"key":"not a json array"}"""
...
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]