Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e24e47

Browse files
committed
Fix jsonpath existense checking of missing variables
The current jsonpath code assumes that the referenced variable always exists.It could only throw an error at the value valuation time. At the same timeexistence checking assumes variable is present without valuation, and errorsuppression doesn't work for missing variables.This commit makes existense checking trigger an error for missing variables.This makes the overall behavior consistent.Backpatch to 12 where jsonpath was introduced.Reported-by: David G. JohnstonDiscussion:https://postgr.es/m/CAKFQuwbeytffJkVnEqDyLZ%3DrQsznoTh1OgDoOF3VmOMkxcTMjA%40mail.gmail.comAuthor: Alexander Korotkov, David G. JohnstonBackpatch-through: 12
1 parentc0ee694 commit9e24e47

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

‎src/backend/utils/adt/jsonpath_exec.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,13 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
891891
JsonbValue*v;
892892
boolhasNext=jspGetNext(jsp,&elem);
893893

894-
if (!hasNext&& !found)
894+
if (!hasNext&& !found&&jsp->type!=jpiVariable)
895895
{
896-
res=jperOk;/* skip evaluation */
896+
/*
897+
* Skip evaluation, but not for variables. We must
898+
* trigger an error for the missing variable.
899+
*/
900+
res=jperOk;
897901
break;
898902
}
899903

‎src/test/regress/expected/jsonb_jsonpath.out

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,14 @@ SELECT jsonb_path_query('[{"a": 1}, {"a": 2}]', '$[*] ? (@.a > 10)');
16661666
------------------
16671667
(0 rows)
16681668

1669+
SELECT jsonb_path_query('[{"a": 1}]', '$undefined_var');
1670+
ERROR: could not find jsonpath variable "undefined_var"
1671+
SELECT jsonb_path_query('[{"a": 1}]', 'false');
1672+
jsonb_path_query
1673+
------------------
1674+
false
1675+
(1 row)
1676+
16691677
SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a');
16701678
ERROR: JSON object does not contain key "a"
16711679
SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}]', '$[*].a');
@@ -1736,6 +1744,14 @@ SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*].
17361744

17371745
(1 row)
17381746

1747+
SELECT jsonb_path_query_first('[{"a": 1}]', '$undefined_var');
1748+
ERROR: could not find jsonpath variable "undefined_var"
1749+
SELECT jsonb_path_query_first('[{"a": 1}]', 'false');
1750+
jsonb_path_query_first
1751+
------------------------
1752+
false
1753+
(1 row)
1754+
17391755
SELECT jsonb '[{"a": 1}, {"a": 2}]' @? '$[*].a ? (@ > 1)';
17401756
?column?
17411757
----------
@@ -1766,6 +1782,14 @@ SELECT jsonb_path_exists('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*] ? (@.
17661782
f
17671783
(1 row)
17681784

1785+
SELECT jsonb_path_exists('[{"a": 1}]', '$undefined_var');
1786+
ERROR: could not find jsonpath variable "undefined_var"
1787+
SELECT jsonb_path_exists('[{"a": 1}]', 'false');
1788+
jsonb_path_exists
1789+
-------------------
1790+
t
1791+
(1 row)
1792+
17691793
SELECT jsonb_path_match('true', '$', silent => false);
17701794
jsonb_path_match
17711795
------------------
@@ -1828,6 +1852,14 @@ SELECT jsonb_path_match('[{"a": 1}, {"a": 2}]', '$[*].a > 1');
18281852
t
18291853
(1 row)
18301854

1855+
SELECT jsonb_path_match('[{"a": 1}]', '$undefined_var');
1856+
ERROR: could not find jsonpath variable "undefined_var"
1857+
SELECT jsonb_path_match('[{"a": 1}]', 'false');
1858+
jsonb_path_match
1859+
------------------
1860+
f
1861+
(1 row)
1862+
18311863
-- test string comparison (Unicode codepoint collation)
18321864
WITH str(j, num) AS
18331865
(

‎src/test/regress/sql/jsonb_jsonpath.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ select jsonb_path_query('[null, 1, "a\b", "a\\b", "^a\\b$"]', 'lax $[*] ? (@ lik
351351

352352
SELECT jsonb_path_query('[{"a": 1}, {"a": 2}]','$[*]');
353353
SELECT jsonb_path_query('[{"a": 1}, {"a": 2}]','$[*] ? (@.a > 10)');
354+
SELECT jsonb_path_query('[{"a": 1}]','$undefined_var');
355+
SELECT jsonb_path_query('[{"a": 1}]','false');
354356

355357
SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}, {}]','strict $[*].a');
356358
SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}]','$[*].a');
@@ -366,12 +368,16 @@ SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}]', '$[*].a ? (@ == 1)');
366368
SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}]','$[*].a ? (@ > 10)');
367369
SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]','$[*].a ? (@ > $min && @ < $max)', vars=>'{"min": 1, "max": 4}');
368370
SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]','$[*].a ? (@ > $min && @ < $max)', vars=>'{"min": 3, "max": 4}');
371+
SELECT jsonb_path_query_first('[{"a": 1}]','$undefined_var');
372+
SELECT jsonb_path_query_first('[{"a": 1}]','false');
369373

370374
SELECT jsonb'[{"a": 1}, {"a": 2}]' @?'$[*].a ? (@ > 1)';
371375
SELECT jsonb'[{"a": 1}, {"a": 2}]' @?'$[*] ? (@.a > 2)';
372376
SELECT jsonb_path_exists('[{"a": 1}, {"a": 2}]','$[*].a ? (@ > 1)');
373377
SELECT jsonb_path_exists('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]','$[*] ? (@.a > $min && @.a < $max)', vars=>'{"min": 1, "max": 4}');
374378
SELECT jsonb_path_exists('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]','$[*] ? (@.a > $min && @.a < $max)', vars=>'{"min": 3, "max": 4}');
379+
SELECT jsonb_path_exists('[{"a": 1}]','$undefined_var');
380+
SELECT jsonb_path_exists('[{"a": 1}]','false');
375381

376382
SELECT jsonb_path_match('true','$', silent=> false);
377383
SELECT jsonb_path_match('false','$', silent=> false);
@@ -388,6 +394,8 @@ SELECT jsonb_path_match('[true, true]', '$[*]', silent => false);
388394
SELECT jsonb'[{"a": 1}, {"a": 2}]' @@'$[*].a > 1';
389395
SELECT jsonb'[{"a": 1}, {"a": 2}]' @@'$[*].a > 2';
390396
SELECT jsonb_path_match('[{"a": 1}, {"a": 2}]','$[*].a > 1');
397+
SELECT jsonb_path_match('[{"a": 1}]','$undefined_var');
398+
SELECT jsonb_path_match('[{"a": 1}]','false');
391399

392400
-- test string comparison (Unicode codepoint collation)
393401
WITH str(j, num)AS

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp