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

Commitfa06982

Browse files
committed
More regression test cases for json/jsonb extraction operators.
Cover some cases I omitted before, such as null and empty-stringelements in the path array. This exposes another inconsistency:json_extract_path complains about empty path elements butjsonb_extract_path does not.
1 parent9bac660 commitfa06982

File tree

6 files changed

+292
-12
lines changed

6 files changed

+292
-12
lines changed

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,12 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
674674

675675
(1 row)
676676

677+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
678+
?column?
679+
----------
680+
681+
(1 row)
682+
677683
select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
678684
?column?
679685
-------------
@@ -692,6 +698,50 @@ select '"foo"'::json -> 1;
692698
ERROR: cannot extract element from a scalar
693699
select '"foo"'::json -> 'z';
694700
ERROR: cannot extract element from a scalar
701+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
702+
?column?
703+
----------
704+
705+
(1 row)
706+
707+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
708+
?column?
709+
----------
710+
711+
(1 row)
712+
713+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
714+
ERROR: cannot extract array element from a non-array
715+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
716+
?column?
717+
----------
718+
719+
(1 row)
720+
721+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
722+
?column?
723+
----------
724+
725+
(1 row)
726+
727+
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
728+
?column?
729+
-------------
730+
{"b": "cc"}
731+
(1 row)
732+
733+
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
734+
?column?
735+
----------
736+
737+
(1 row)
738+
739+
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
740+
ERROR: cannot extract field from a non-object
741+
select '"foo"'::json ->> 1;
742+
ERROR: cannot extract element from a scalar
743+
select '"foo"'::json ->> 'z';
744+
ERROR: cannot extract element from a scalar
695745
-- array length
696746
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
697747
json_array_length
@@ -871,7 +921,7 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
871921
(1 row)
872922

873923
-- corner cases for same
874-
select '{"a": {"b":{"c": "foo"}}}'::json #>array[]::text[];
924+
select '{"a": {"b":{"c": "foo"}}}'::json #>'{}';
875925
?column?
876926
----------
877927

@@ -883,6 +933,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
883933
{"b":{"c": "foo"}}
884934
(1 row)
885935

936+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
937+
ERROR: cannot call json_extract_path with null path elements
938+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
939+
ERROR: cannot call json_extract_path with empty path elements
886940
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
887941
?column?
888942
--------------
@@ -949,7 +1003,7 @@ select '42'::json #> array['0'];
9491003

9501004
(1 row)
9511005

952-
select '{"a": {"b":{"c": "foo"}}}'::json #>>array[]::text[];
1006+
select '{"a": {"b":{"c": "foo"}}}'::json #>>'{}';
9531007
?column?
9541008
----------
9551009

@@ -961,6 +1015,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
9611015
{"b":{"c": "foo"}}
9621016
(1 row)
9631017

1018+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1019+
ERROR: cannot call json_extract_path_text with null path elements
1020+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1021+
ERROR: cannot call json_extract_path_text with empty path elements
9641022
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
9651023
?column?
9661024
--------------

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,12 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
674674

675675
(1 row)
676676

677+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
678+
?column?
679+
----------
680+
681+
(1 row)
682+
677683
select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
678684
?column?
679685
-------------
@@ -692,6 +698,50 @@ select '"foo"'::json -> 1;
692698
ERROR: cannot extract element from a scalar
693699
select '"foo"'::json -> 'z';
694700
ERROR: cannot extract element from a scalar
701+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
702+
?column?
703+
----------
704+
705+
(1 row)
706+
707+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
708+
?column?
709+
----------
710+
711+
(1 row)
712+
713+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
714+
ERROR: cannot extract array element from a non-array
715+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
716+
?column?
717+
----------
718+
719+
(1 row)
720+
721+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
722+
?column?
723+
----------
724+
725+
(1 row)
726+
727+
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
728+
?column?
729+
-------------
730+
{"b": "cc"}
731+
(1 row)
732+
733+
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
734+
?column?
735+
----------
736+
737+
(1 row)
738+
739+
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
740+
ERROR: cannot extract field from a non-object
741+
select '"foo"'::json ->> 1;
742+
ERROR: cannot extract element from a scalar
743+
select '"foo"'::json ->> 'z';
744+
ERROR: cannot extract element from a scalar
695745
-- array length
696746
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
697747
json_array_length
@@ -871,7 +921,7 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
871921
(1 row)
872922

873923
-- corner cases for same
874-
select '{"a": {"b":{"c": "foo"}}}'::json #>array[]::text[];
924+
select '{"a": {"b":{"c": "foo"}}}'::json #>'{}';
875925
?column?
876926
----------
877927

@@ -883,6 +933,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
883933
{"b":{"c": "foo"}}
884934
(1 row)
885935

936+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
937+
ERROR: cannot call json_extract_path with null path elements
938+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
939+
ERROR: cannot call json_extract_path with empty path elements
886940
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
887941
?column?
888942
--------------
@@ -949,7 +1003,7 @@ select '42'::json #> array['0'];
9491003

9501004
(1 row)
9511005

952-
select '{"a": {"b":{"c": "foo"}}}'::json #>>array[]::text[];
1006+
select '{"a": {"b":{"c": "foo"}}}'::json #>>'{}';
9531007
?column?
9541008
----------
9551009

@@ -961,6 +1015,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
9611015
{"b":{"c": "foo"}}
9621016
(1 row)
9631017

1018+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
1019+
ERROR: cannot call json_extract_path_text with null path elements
1020+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
1021+
ERROR: cannot call json_extract_path_text with empty path elements
9641022
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
9651023
?column?
9661024
--------------

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

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
453453

454454
(1 row)
455455

456+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
457+
?column?
458+
----------
459+
460+
(1 row)
461+
456462
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
457463
?column?
458464
-------------
@@ -471,6 +477,50 @@ select '"foo"'::jsonb -> 1;
471477
ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar
472478
select '"foo"'::jsonb -> 'z';
473479
ERROR: cannot call jsonb_object_field (jsonb -> text) on a scalar
480+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
481+
?column?
482+
----------
483+
484+
(1 row)
485+
486+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
487+
?column?
488+
----------
489+
490+
(1 row)
491+
492+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
493+
ERROR: cannot call jsonb_array_element_text on an object
494+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
495+
?column?
496+
----------
497+
498+
(1 row)
499+
500+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
501+
?column?
502+
----------
503+
504+
(1 row)
505+
506+
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
507+
?column?
508+
-------------
509+
{"b": "cc"}
510+
(1 row)
511+
512+
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
513+
?column?
514+
----------
515+
516+
(1 row)
517+
518+
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
519+
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on an array
520+
select '"foo"'::jsonb ->> 1;
521+
ERROR: cannot call jsonb_array_element_text on a scalar
522+
select '"foo"'::jsonb ->> 'z';
523+
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar
474524
-- equality and inequality
475525
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
476526
?column?
@@ -1218,7 +1268,7 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
12181268
(1 row)
12191269

12201270
-- corner cases for same
1221-
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>array[]::text[];
1271+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>'{}';
12221272
?column?
12231273
----------
12241274

@@ -1230,6 +1280,14 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
12301280
{"b": {"c": "foo"}}
12311281
(1 row)
12321282

1283+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
1284+
ERROR: cannot call jsonb_extract_path with null path elements
1285+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
1286+
?column?
1287+
----------
1288+
1289+
(1 row)
1290+
12331291
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
12341292
?column?
12351293
--------------
@@ -1284,7 +1342,7 @@ select '42'::jsonb #> array['f2'];
12841342
ERROR: cannot extract path from a scalar
12851343
select '42'::jsonb #> array['0'];
12861344
ERROR: cannot extract path from a scalar
1287-
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>>array[]::text[];
1345+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>>'{}';
12881346
?column?
12891347
----------
12901348

@@ -1296,6 +1354,14 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
12961354
{"b": {"c": "foo"}}
12971355
(1 row)
12981356

1357+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
1358+
ERROR: cannot call jsonb_extract_path_text with null path elements
1359+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
1360+
?column?
1361+
----------
1362+
1363+
(1 row)
1364+
12991365
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
13001366
?column?
13011367
--------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp