Browse Source

doc(func): json functions support

ngjaying 4 years atrás
parent
commit
226f258a33
2 changed files with 57 additions and 0 deletions
  1. 49 0
      docs/en_US/json_expr.md
  2. 8 0
      docs/en_US/sqls/built-in_functions.md

+ 49 - 0
docs/en_US/json_expr.md

@@ -120,7 +120,56 @@ SELECT followers->Group1[:1]->first FROM demo
 }
 ```
 
+# Json Path functions
 
+Kuiper provides a list of functions to allow to execute json path over struct or array columns or values. The functions are:
+
+```tsql
+json_path_exists(col, jsonpath)
+json_path_query(col, jsonpath)
+json_path_query_first(col, jsonpath)
+```
+
+Please refer to [json functions](sqls/built-in_functions.md#json-functions) for detail.
+
+All these functions share the same parameter signatures, among which, the second parameter is a jsonpath string. The jsonpath grammer used by Kuiper is based on [JsonPath](https://goessner.net/articles/JsonPath/).  
+
+The basic grammar of those expressions is to use the keys part of the JSON objects combined with some elements:
+
+- Dots `.` to move into a tree
+- Brackets `[]` for access to a given array member coupled with a position.
+- Variables, with `$` representing a JSON text and `@` for result path evaluations.
+
+So for example, when applied to the previous JSON data sample we can reach the following parts of the tree with these expressions:
+
+- `$.age` refers to 37.
+- `$.friends.first` refers to “dale”.
+- `$.friends` refers to the full array of friends.
+- `$.friends[0]` refers to the first friend listed in the previous array (contrary to arrays members are zero-based).
+- `$.friends[? @.age>60].first` or `$.friends[? (@.age>60)].first` refers to the first name of the friends whose age is bigger than 60. Notice that the space between ? and the condition is required even the condition is with braces.
+
+Developers can use the json functions in the SQL statement. Here are some examples.
+
+- Select the lastname of group1 followers
+```tsql
+SELECT json_path_query(followers, "$.Group1[*].last") FROM demo
+
+["Shavor","Miller"]
+```
+
+- Select the lastname if any of the group1 followers is older than 60
+```tsql
+SELECT name->last FROM demo where json_path_exists(followers, "$.Group1[? @.age>30]")
+
+"Anderson"
+```
+
+- Select the follower's lastname from group1 whose age is bigger than 30
+```tsql
+SELECT json_path_exists(followers, "$.Group1[? @.age>30].last") FROM demo
+
+["Miller"]
+```
 
 # *Projections*
 

+ 8 - 0
docs/en_US/sqls/built-in_functions.md

@@ -85,6 +85,14 @@ Aggregate functions perform a calculation on a set of values and return a single
 | sha256   | sha256(col1)| Hashed value of the argument                   |
 | sha384   | sha384(col1)| Hashed value of the argument                   |
 | sha512   | sha512(col1)| Hashed value of the argument                   |
+## JSON Functions
+| Function | Example     | Description                                    |
+| -------- | ----------- | ---------------------------------------------- |
+| json_path_exists      | json_path_exists(col1, "$.name")   | Checks whether JSON path returns any item for the specified JSON value. Return bool value.                   |
+| json_path_query     | json_path_query(col1, "$.name")  | Gets all items returned by JSON path for the specified JSON value.              |
+| json_path_query_first  | json_path_query_first(col1, "$.name")| Gets the first item returned by JSON path for the specified JSON value.                  |
+
+**Please refer to [json path functions](../json_expr.md#json-path-functions) for how to compose a json path.**  
 
 ## Other Functions
 | Function  | Example      | Description                                                  |