Skip to content

Commit b3fa0b4

Browse files
craig[bot]normanchenn
andcommitted
Merge #144006
144006: builtins: add `jsonb_path_query_array`, `jsonb_path_query_first`, `jsonb_path_match` r=normanchenn a=normanchenn #### builtins: add jsonb_path_query_array This commit adds the `jsonb_path_query_array` function, which wraps `jsonb_path_query` and returns a json array containing all the items returned from the query. Informs: #22513 Release note (sql change): Add the `jsonb_path_query_array` function, which returns the result of `jsonb_path_query` wrapped in a JSON array. #### builtins: add jsonb_path_query_first This commit adds the `jsonb_path_query_first` function, which wraps `jsonb_path_query` and returns the first element that was returned from the function. Informs: #22513 Release note (sql change): Add the `jsonb_path_query_first` function, which returns the first result from `jsonb_path_query`. #### builtins: add jsonb_path_match This commit adds the `jsonb_path_match` function, which returns a boolean that evaluates the predicate query, and returns null if the query result isn't a predicate. Informs: #22513 Release note (sql change): Add the `jsonb_path_match` function, which returns the result of a predicate query. Co-authored-by: Norman Chen <[email protected]>
2 parents 9985f68 + 063edc4 commit b3fa0b4

File tree

17 files changed

+619
-40
lines changed

17 files changed

+619
-40
lines changed

docs/generated/sql/functions.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,29 @@ argument is true, the function suppresses the following errors:
12801280
missing object field or array element, unexpected JSON item type,
12811281
datetime and numeric errors.</p>
12821282
</span></td><td>Immutable</td></tr>
1283+
<tr><td><a name="jsonb_path_match"></a><code>jsonb_path_match(target: jsonb, path: jsonpath) &rarr; <a href="bool.html">bool</a></code></td><td><span class="funcdesc"><p>Returns the SQL boolean result of a JSON path predicate check
1284+
for the specified JSON value. (This is useful only with predicate check
1285+
expressions, not SQL-standard JSON path expressions, since it will
1286+
either fail or return NULL if the path result is not a single boolean
1287+
value.)</p>
1288+
</span></td><td>Immutable</td></tr>
1289+
<tr><td><a name="jsonb_path_match"></a><code>jsonb_path_match(target: jsonb, path: jsonpath, vars: jsonb) &rarr; <a href="bool.html">bool</a></code></td><td><span class="funcdesc"><p>Returns the SQL boolean result of a JSON path predicate check
1290+
for the specified JSON value. (This is useful only with predicate check
1291+
expressions, not SQL-standard JSON path expressions, since it will
1292+
either fail or return NULL if the path result is not a single boolean
1293+
value.) The vars argument must be a JSON object, and its fields provide
1294+
named values to be substituted into the jsonpath expression.</p>
1295+
</span></td><td>Immutable</td></tr>
1296+
<tr><td><a name="jsonb_path_match"></a><code>jsonb_path_match(target: jsonb, path: jsonpath, vars: jsonb, silent: <a href="bool.html">bool</a>) &rarr; <a href="bool.html">bool</a></code></td><td><span class="funcdesc"><p>Returns the SQL boolean result of a JSON path predicate check
1297+
for the specified JSON value. (This is useful only with predicate check
1298+
expressions, not SQL-standard JSON path expressions, since it will
1299+
either fail or return NULL if the path result is not a single boolean
1300+
value.) The vars argument must be a JSON object, and its fields provide
1301+
named values to be substituted into the jsonpath expression. If the
1302+
silent argument is true, the function suppresses the following errors:
1303+
missing object field or array element, unexpected JSON item type,
1304+
datetime and numeric errors.</p>
1305+
</span></td><td>Immutable</td></tr>
12831306
<tr><td><a name="jsonb_path_query"></a><code>jsonb_path_query(target: jsonb, path: jsonpath) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns all JSON items returned by the JSON path for the specified JSON value.</p>
12841307
</span></td><td>Immutable</td></tr>
12851308
<tr><td><a name="jsonb_path_query"></a><code>jsonb_path_query(target: jsonb, path: jsonpath, vars: jsonb) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns all JSON items returned by the JSON path for the specified JSON value.
@@ -1291,6 +1314,36 @@ The vars argument must be a JSON object, and its fields provide named values
12911314
to be substituted into the jsonpath expression. If the silent argument is true,
12921315
the function suppresses the following errors: missing object field or array
12931316
element, unexpected JSON item type, datetime and numeric errors.</p>
1317+
</span></td><td>Immutable</td></tr>
1318+
<tr><td><a name="jsonb_path_query_array"></a><code>jsonb_path_query_array(target: jsonb, path: jsonpath) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns all JSON items returned by the JSON path for the
1319+
specified JSON value, as a JSON array.</p>
1320+
</span></td><td>Immutable</td></tr>
1321+
<tr><td><a name="jsonb_path_query_array"></a><code>jsonb_path_query_array(target: jsonb, path: jsonpath, vars: jsonb) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns all JSON items returned by the JSON path for the
1322+
specified JSON value, as a JSON array. The vars argument must be a
1323+
JSON object, and its fields provide named values to be substituted
1324+
into the jsonpath expression.</p>
1325+
</span></td><td>Immutable</td></tr>
1326+
<tr><td><a name="jsonb_path_query_array"></a><code>jsonb_path_query_array(target: jsonb, path: jsonpath, vars: jsonb, silent: <a href="bool.html">bool</a>) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns all JSON items returned by the JSON path for the
1327+
specified JSON value, as a JSON array. The vars argument must be a
1328+
JSON object, and its fields provide named values to be substituted
1329+
into the jsonpath expression. If the silent argument is true, the
1330+
function suppresses the following errors: missing object field or
1331+
array element, unexpected JSON item type, datetime and numeric errors.</p>
1332+
</span></td><td>Immutable</td></tr>
1333+
<tr><td><a name="jsonb_path_query_first"></a><code>jsonb_path_query_first(target: jsonb, path: jsonpath) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns the first JSON item returned by the JSON path for the
1334+
specified JSON value, or NULL if there are no results.</p>
1335+
</span></td><td>Immutable</td></tr>
1336+
<tr><td><a name="jsonb_path_query_first"></a><code>jsonb_path_query_first(target: jsonb, path: jsonpath, vars: jsonb) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns the first JSON item returned by the JSON path for the
1337+
specified JSON value, or NULL if there are no results. The vars
1338+
argument must be a JSON object, and its fields provide named values
1339+
to be substituted into the jsonpath expression.</p>
1340+
</span></td><td>Immutable</td></tr>
1341+
<tr><td><a name="jsonb_path_query_first"></a><code>jsonb_path_query_first(target: jsonb, path: jsonpath, vars: jsonb, silent: <a href="bool.html">bool</a>) &rarr; jsonb</code></td><td><span class="funcdesc"><p>Returns the first JSON item returned by the JSON path for the
1342+
specified JSON value, or NULL if there are no results. The vars
1343+
argument must be a JSON object, and its fields provide named values
1344+
to be substituted into the jsonpath expression. If the silent argument is true, the
1345+
function suppresses the following errors: missing object field or
1346+
array element, unexpected JSON item type, datetime and numeric errors.</p>
12941347
</span></td><td>Immutable</td></tr></tbody>
12951348
</table>
12961349

pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ccl/logictestccl/tests/local-read-committed/generated_test.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ccl/logictestccl/tests/local-repeatable-read/generated_test.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# LogicTest: !local-mixed-24.3 !local-mixed-25.1
2+
3+
query B
4+
SELECT jsonb_path_match('{}', '1 + 1 == 2');
5+
----
6+
true
7+
8+
query B
9+
SELECT jsonb_path_match('{}', '"abc" starts with "b"');
10+
----
11+
false
12+
13+
query T
14+
SELECT jsonb_path_match('{}', 'strict $', '{}', true);
15+
----
16+
NULL
17+
18+
statement error pgcode 22038 pq: jsonb_path_match\(\): single boolean result is expected
19+
SELECT jsonb_path_match('{}', 'strict $', '{}', false);
20+
21+
query T
22+
SELECT jsonb_path_match('{}', '$', '{}', true);
23+
----
24+
NULL
25+
26+
statement error pgcode 22038 pq: jsonb_path_match\(\): single boolean result is expected
27+
SELECT jsonb_path_match('{}', '$', '{}', false);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# LogicTest: !local-mixed-24.3 !local-mixed-25.1
2+
3+
query T
4+
SELECT jsonb_path_query_array('{}', '$')
5+
----
6+
[{}]
7+
8+
query T
9+
SELECT jsonb_path_query_array('{"a": [1, 2, {"b": [4, 5]}, null, [true, false]]}', '$.a[*]')
10+
----
11+
[1, 2, {"b": [4, 5]}, null, [true, false]]
12+
13+
query T
14+
SELECT jsonb_path_query_array('{"a": [[{"b": 1, "c": "hello"}, {"b": 2, "c": "world"}, {"b": 1, "c": "!"}], [{"b": 1, "c": "hello"}, {"b": 2, "c": "world"}, {"b": 1, "c": "!"}]]}', '$.a ? (@.b == 1)');
15+
----
16+
[[{"b": 1, "c": "hello"}, {"b": 2, "c": "world"}, {"b": 1, "c": "!"}], [{"b": 1, "c": "hello"}, {"b": 2, "c": "world"}, {"b": 1, "c": "!"}]]
17+
18+
query T
19+
SELECT jsonb_path_query_array('{}', 'strict $.a', '{}', true);
20+
----
21+
[]
22+
23+
statement error pgcode 2203A pq: jsonb_path_query_array\(\): JSON object does not contain key "a"
24+
SELECT jsonb_path_query_array('{}', 'strict $.a', '{}', false);
25+
26+
query T
27+
SELECT jsonb_path_query_array('{}', '$.a', '{}', true);
28+
----
29+
[]
30+
31+
query T
32+
SELECT jsonb_path_query_array('{}', '$.a', '{}', false);
33+
----
34+
[]
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# LogicTest: !local-mixed-24.3 !local-mixed-25.1
2+
3+
query T
4+
SELECT jsonb_path_query_first('[1, 2, 3]', '$[*]');
5+
----
6+
1
7+
8+
query T
9+
SELECT jsonb_path_query_first('[2, 3]', '$[*]');
10+
----
11+
2
12+
13+
query T
14+
SELECT jsonb_path_query_first('[]', '$[*]');
15+
----
16+
NULL
17+
18+
query T
19+
SELECT jsonb_path_query_first('{}', 'strict $.a', '{}', true);
20+
----
21+
NULL
22+
23+
statement error pgcode 2203A pq: jsonb_path_query_first\(\): JSON object does not contain key "a"
24+
SELECT jsonb_path_query_first('{}', 'strict $.a', '{}', false);
25+
26+
query T
27+
SELECT jsonb_path_query_first('{}', '$.a', '{}', true);
28+
----
29+
NULL
30+
31+
query T
32+
SELECT jsonb_path_query_first('{}', '$.a', '{}', false);
33+
----
34+
NULL

pkg/sql/logictest/tests/fakedist-disk/generated_test.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/logictest/tests/fakedist-vec-off/generated_test.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/sql/logictest/tests/fakedist/generated_test.go

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)