Skip to content

Commit

Permalink
[feature](function) add json->operator convert to json_extract (#19899)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanko-7 authored May 27, 2023
1 parent f3d8af3 commit f54a068
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ INT json_extract_int(JSON j, VARCHAR json_path)
BIGINT json_extract_bigint(JSON j, VARCHAR json_path)
DOUBLE json_extract_double(JSON j, VARCHAR json_path)
STRING json_extract_string(JSON j, VARCHAR json_path)
VARCHAR json_str->json_path
```

json_extract functions extract field specified by json_path from JSON. A series of functions are provided for different datatype.
Expand Down Expand Up @@ -124,6 +125,22 @@ mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name');
| [null,"doris"] |
+-----------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> SELECT '{"id": 123, "name": "doris"}'->'$.name';
+--------------------------------------------------------+
| json_extract('{"id": 123, "name": "doris"}', '$.name') |
+--------------------------------------------------------+
| "doris" |
+--------------------------------------------------------+
1 row in set (0.01 sec)
mysql> SELECT '{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2, 3] } }'->'$.k2.k22[2]';
+--------------------------------------------------------------------------------------+
| json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2, 3] } }', '$.k2.k22[2]') |
+--------------------------------------------------------------------------------------+
| 3 |
+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ INT json_extract_int(JSON j, VARCHAR json_path)
BIGINT json_extract_bigint(JSON j, VARCHAR json_path)
DOUBLE json_extract_double(JSON j, VARCHAR json_path)
STRING json_extract_string(JSON j, VARCHAR json_path)
VARCHAR json_str->json_path
```


Expand Down Expand Up @@ -101,6 +102,22 @@ mysql> SELECT json_extract('{"id": 123, "name": "doris"}', '$.aaa', '$.name');
| [null,"doris"] |
+-----------------------------------------------------------------+
1 row in set (0.01 sec)
mysql> SELECT '{"id": 123, "name": "doris"}'->'$.name';
+--------------------------------------------------------+
| json_extract('{"id": 123, "name": "doris"}', '$.name') |
+--------------------------------------------------------+
| "doris" |
+--------------------------------------------------------+
1 row in set (0.01 sec)
mysql> SELECT '{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2, 3] } }'->'$.k2.k22[2]';
+--------------------------------------------------------------------------------------+
| json_extract('{"k1": "v1", "k2": { "k21": 6.6, "k22": [1, 2, 3] } }', '$.k2.k22[2]') |
+--------------------------------------------------------------------------------------+
| 3 |
+--------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

### keywords
Expand Down
8 changes: 8 additions & 0 deletions fe/fe-core/src/main/cup/sql_parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ precedence left KW_OVER;
precedence left KW_COLLATE;
precedence left KW_PARTITION;
precedence left KW_PARTITIONS;
precedence left ARROW;
precedence right KW_TEMPORARY;
precedence right LBRACKET;
precedence right LBRACE;
Expand Down Expand Up @@ -6385,6 +6386,13 @@ non_pred_expr ::=
{:
RESULT = new FunctionCallExpr("concat", exprs);
:}
| expr:e ARROW expr:index
{:
ArrayList<Expr> exprs = new ArrayList<>();
exprs.add(e);
exprs.add(index);
RESULT = new FunctionCallExpr("json_extract", new FunctionParams(exprs));
:}
| cast_expr:c
{: RESULT = c; :}
| case_expr:c
Expand Down
115 changes: 115 additions & 0 deletions regression-test/data/jsonb_p0/test_jsonb_load_and_function.out
Original file line number Diff line number Diff line change
Expand Up @@ -4427,3 +4427,118 @@ false
27 {"k1":"v1","k2":200} [null,null,null]
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} [null,null,null]

-- !select --
1 \N \N
2 null null
3 true null
4 false null
5 100 null
6 10000 null
7 1000000000 null
8 1152921504606846976 null
9 6.18 null
10 "abcd" null
11 {} null
12 {"k1":"v31","k2":300} "v31"
13 [] null
14 [123,456] null
15 ["abc","def"] null
16 [null,true,false,100,6.18,"abc"] null
17 [{"k1":"v41","k2":400},1,"a",3.14] ["v41"]
18 {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} "v31"
26 \N \N
27 {"k1":"v1","k2":200} "v1"
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} null

-- !select --
1 \N \N
2 null null
3 true null
4 false null
5 100 null
6 10000 null
7 1000000000 null
8 1152921504606846976 null
9 6.18 null
10 "abcd" null
11 {} null
12 {"k1":"v31","k2":300} null
13 [] null
14 [123,456] 456
15 ["abc","def"] "def"
16 [null,true,false,100,6.18,"abc"] true
17 [{"k1":"v41","k2":400},1,"a",3.14] 1
18 {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} null
26 \N \N
27 {"k1":"v1","k2":200} null
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} null

-- !select --
1 \N \N
2 null \N
3 true \N
4 false \N
5 100 \N
6 10000 \N
7 1000000000 \N
8 1152921504606846976 \N
9 6.18 \N
10 "abcd" \N
11 {} \N
12 {"k1":"v31","k2":300} \N
13 [] \N
14 [123,456] \N
15 ["abc","def"] \N
16 [null,true,false,100,6.18,"abc"] \N
17 [{"k1":"v41","k2":400},1,"a",3.14] \N
18 {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} \N
26 \N \N
27 {"k1":"v1","k2":200} \N
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} \N

-- !select --
1 \N \N
2 null null
3 true null
4 false null
5 100 null
6 10000 null
7 1000000000 null
8 1152921504606846976 null
9 6.18 null
10 "abcd" null
11 {} null
12 {"k1":"v31","k2":300} null
13 [] null
14 [123,456] null
15 ["abc","def"] null
16 [null,true,false,100,6.18,"abc"] null
17 [{"k1":"v41","k2":400},1,"a",3.14] null
18 {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} 400
26 \N \N
27 {"k1":"v1","k2":200} null
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} null

-- !select --
1 \N \N
2 null null
3 true null
4 false null
5 100 null
6 10000 null
7 1000000000 null
8 1152921504606846976 null
9 6.18 null
10 "abcd" null
11 {} null
12 {"k1":"v31","k2":300} null
13 [] null
14 [123,456] null
15 ["abc","def"] null
16 [null,true,false,100,6.18,"abc"] null
17 [{"k1":"v41","k2":400},1,"a",3.14] null
18 {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} "v41"
26 \N \N
27 {"k1":"v1","k2":200} null
28 {"a.b.c":{"k1.a1":"v31","k2":300},"a":"niu"} null

Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,24 @@ DORIS
-- !sql --
[6.6,[1,2,3],2]

-- !sql --
"doris"

-- !sql --
\N

-- !sql --
{"k21":6.6,"k22":[1,2,3]}

-- !sql --
3

-- !sql --
[1,2,3]

-- !sql --
doris

-- !sql --
123

Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,10 @@ suite("test_jsonb_load_and_function", "p0") {
qt_select """SELECT id, j, JSON_EXTRACT(j, '\$.k2', '\$.x.y') FROM ${testTable} ORDER BY id"""
qt_select """SELECT id, j, JSON_EXTRACT(j, '\$.k2', null) FROM ${testTable} ORDER BY id"""
qt_select """SELECT id, j, JSON_EXTRACT(j, '\$.a1[0].k1', '\$.a1[0].k2', '\$.a1[2]') FROM ${testTable} ORDER BY id"""

qt_select """SELECT id, j, j->'\$.k1' FROM ${testTable} ORDER BY id"""
qt_select """SELECT id, j, j->'\$.[1]' FROM ${testTable} ORDER BY id"""
qt_select """SELECT id, j, j->null FROM ${testTable} ORDER BY id"""
qt_select """SELECT id, j, j->'\$.a1[0].k2' FROM ${testTable} ORDER BY id"""
qt_select """SELECT id, j, j->'\$.a1[0]'->'\$.k1' FROM ${testTable} ORDER BY id"""
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ suite("test_json_function") {
qt_sql "SELECT json_extract(null, '\$.id');"
qt_sql "SELECT json_extract('{\"k1\": \"v1\", \"k2\": { \"k21\": 6.6, \"k22\": [1, 2, 3] } }', '\$.k1', '\$.k2');"
qt_sql "SELECT json_extract('{\"k1\": \"v1\", \"k2\": { \"k21\": 6.6, \"k22\": [1, 2, 3] } }', '\$.k2.k21', '\$.k2.k22', '\$.k2.k22[1]');"

qt_sql "SELECT '{\"id\": 123, \"name\": \"doris\"}'->'\$.name';"
qt_sql "SELECT null->'\$.id';"
qt_sql "SELECT '{\"k1\": \"v1\", \"k2\": { \"k21\": 6.6, \"k22\": [1, 2, 3] } }'->'\$.k2';"
qt_sql "SELECT '{\"k1\": \"v1\", \"k2\": { \"k21\": 6.6, \"k22\": [1, 2, 3] } }'->'\$.k2.k22[2]';"
qt_sql "SELECT '{\"k1\": \"v1\", \"k2\": { \"k21\": 6.6, \"k22\": [1, 2, 3] } }'->'\$.k2'->'\$.k22'"
qt_sql "SELECT json_unquote('{\"id\": 123, \"name\": \"doris\"}'->'\$.name');"
qt_sql "SELECT json_extract('{\"id\": 123, \"name\": \"doris\"}', '\$.id', '\$.name')->'\$.[0]';"
}

0 comments on commit f54a068

Please sign in to comment.