Skip to content

Commit fde1949

Browse files
committed
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. Release note (sql change): Add the `jsonb_path_query_first` function, which returns the first result from `jsonb_path_query`.
1 parent 6f7eeb0 commit fde1949

File tree

14 files changed

+182
-1
lines changed

14 files changed

+182
-1
lines changed

docs/generated/sql/functions.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,21 @@ JSON object, and its fields provide named values to be substituted
13061306
into the jsonpath expression. If the silent argument is true, the
13071307
function suppresses the following errors: missing object field or
13081308
array element, unexpected JSON item type, datetime and numeric errors.</p>
1309+
</span></td><td>Immutable</td></tr>
1310+
<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
1311+
specified JSON value, or NULL if there are no results.</p>
1312+
</span></td><td>Immutable</td></tr>
1313+
<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
1314+
specified JSON value, or NULL if there are no results. The vars
1315+
argument must be a JSON object, and its fields provide named values
1316+
to be substituted into the jsonpath expression.</p>
1317+
</span></td><td>Immutable</td></tr>
1318+
<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
1319+
specified JSON value, or NULL if there are no results. The vars
1320+
argument must be a JSON object, and its fields provide named values
1321+
to be substituted into the jsonpath expression. If the silent argument is true, the
1322+
function suppresses the following errors: missing object field or
1323+
array element, unexpected JSON item type, datetime and numeric errors.</p>
13091324
</span></td><td>Immutable</td></tr></tbody>
13101325
</table>
13111326

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

Lines changed: 7 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: 7 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: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
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: 7 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: 7 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: 7 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/local-legacy-schema-changer/generated_test.go

Lines changed: 7 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/local-vec-off/generated_test.go

Lines changed: 7 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)