More regression test cases for json/jsonb extraction operators.

Cover some cases I omitted before, such as null and empty-string
elements in the path array.  This exposes another inconsistency:
json_extract_path complains about empty path elements but
jsonb_extract_path does not.
This commit is contained in:
Tom Lane 2014-08-20 19:05:05 -04:00
parent 9bac66020d
commit fa069822f5
6 changed files with 292 additions and 12 deletions

View File

@ -674,6 +674,12 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
?column?
-------------
@ -692,6 +698,50 @@ select '"foo"'::json -> 1;
ERROR: cannot extract element from a scalar
select '"foo"'::json -> 'z';
ERROR: cannot extract element from a scalar
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
ERROR: cannot extract array element from a non-array
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
?column?
-------------
{"b": "cc"}
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
ERROR: cannot extract field from a non-object
select '"foo"'::json ->> 1;
ERROR: cannot extract element from a scalar
select '"foo"'::json ->> 'z';
ERROR: cannot extract element from a scalar
-- array length
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
json_array_length
@ -871,7 +921,7 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
(1 row)
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
?column?
----------
@ -883,6 +933,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
{"b":{"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
ERROR: cannot call json_extract_path with null path elements
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
ERROR: cannot call json_extract_path with empty path elements
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
?column?
--------------
@ -949,7 +1003,7 @@ select '42'::json #> array['0'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
?column?
----------
@ -961,6 +1015,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
{"b":{"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
ERROR: cannot call json_extract_path_text with null path elements
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
ERROR: cannot call json_extract_path_text with empty path elements
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
?column?
--------------

View File

@ -674,6 +674,12 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
?column?
-------------
@ -692,6 +698,50 @@ select '"foo"'::json -> 1;
ERROR: cannot extract element from a scalar
select '"foo"'::json -> 'z';
ERROR: cannot extract element from a scalar
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
ERROR: cannot extract array element from a non-array
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
?column?
-------------
{"b": "cc"}
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
ERROR: cannot extract field from a non-object
select '"foo"'::json ->> 1;
ERROR: cannot extract element from a scalar
select '"foo"'::json ->> 'z';
ERROR: cannot extract element from a scalar
-- array length
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
json_array_length
@ -871,7 +921,7 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
(1 row)
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
?column?
----------
@ -883,6 +933,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
{"b":{"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
ERROR: cannot call json_extract_path with null path elements
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
ERROR: cannot call json_extract_path with empty path elements
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
?column?
--------------
@ -949,7 +1003,7 @@ select '42'::json #> array['0'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
?column?
----------
@ -961,6 +1015,10 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
{"b":{"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
ERROR: cannot call json_extract_path_text with null path elements
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
ERROR: cannot call json_extract_path_text with empty path elements
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
?column?
--------------

View File

@ -453,6 +453,12 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
?column?
-------------
@ -471,6 +477,50 @@ select '"foo"'::jsonb -> 1;
ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar
select '"foo"'::jsonb -> 'z';
ERROR: cannot call jsonb_object_field (jsonb -> text) on a scalar
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
ERROR: cannot call jsonb_array_element_text on an object
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
?column?
-------------
{"b": "cc"}
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on an array
select '"foo"'::jsonb ->> 1;
ERROR: cannot call jsonb_array_element_text on a scalar
select '"foo"'::jsonb ->> 'z';
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar
-- equality and inequality
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
?column?
@ -1218,7 +1268,7 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
(1 row)
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
?column?
----------
@ -1230,6 +1280,14 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
{"b": {"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
ERROR: cannot call jsonb_extract_path with null path elements
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
?column?
----------
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
?column?
--------------
@ -1284,7 +1342,7 @@ select '42'::jsonb #> array['f2'];
ERROR: cannot extract path from a scalar
select '42'::jsonb #> array['0'];
ERROR: cannot extract path from a scalar
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
?column?
----------
@ -1296,6 +1354,14 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
{"b": {"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
ERROR: cannot call jsonb_extract_path_text with null path elements
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
?column?
----------
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
?column?
--------------

View File

@ -453,6 +453,12 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
?column?
-------------
@ -471,6 +477,50 @@ select '"foo"'::jsonb -> 1;
ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar
select '"foo"'::jsonb -> 'z';
ERROR: cannot call jsonb_object_field (jsonb -> text) on a scalar
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
ERROR: cannot call jsonb_array_element_text on an object
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
?column?
-------------
{"b": "cc"}
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
?column?
----------
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on an array
select '"foo"'::jsonb ->> 1;
ERROR: cannot call jsonb_array_element_text on a scalar
select '"foo"'::jsonb ->> 'z';
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar
-- equality and inequality
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
?column?
@ -1218,7 +1268,7 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
(1 row)
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
?column?
----------
@ -1230,6 +1280,14 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
{"b": {"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
ERROR: cannot call jsonb_extract_path with null path elements
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
?column?
----------
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
?column?
--------------
@ -1284,7 +1342,7 @@ select '42'::jsonb #> array['f2'];
ERROR: cannot extract path from a scalar
select '42'::jsonb #> array['0'];
ERROR: cannot extract path from a scalar
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
?column?
----------
@ -1296,6 +1354,14 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
{"b": {"c": "foo"}}
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
ERROR: cannot call jsonb_extract_path_text with null path elements
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
?column?
----------
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
?column?
--------------

View File

@ -244,12 +244,24 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::text;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
select '"foo"'::json -> 1;
select '"foo"'::json -> 'z';
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
select '"foo"'::json ->> 1;
select '"foo"'::json ->> 'z';
-- array length
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
@ -299,8 +311,10 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','0'];
select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c'];
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b','c','d'];
@ -313,8 +327,10 @@ select '"foo"'::json #> array['z'];
select '42'::json #> array['f2'];
select '42'::json #> array['0'];
select '{"a": {"b":{"c": "foo"}}}'::json #>> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c'];
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b','c','d'];

View File

@ -113,12 +113,24 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::text;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
select '"foo"'::jsonb -> 1;
select '"foo"'::jsonb -> 'z';
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
select '"foo"'::jsonb ->> 1;
select '"foo"'::jsonb ->> 'z';
-- equality and inequality
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
SELECT '{"x":"y"}'::jsonb = '{"x":"z"}'::jsonb;
@ -270,8 +282,10 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','0'];
SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a','b','c','d'];
@ -284,8 +298,10 @@ select '"foo"'::jsonb #> array['z'];
select '42'::jsonb #> array['f2'];
select '42'::jsonb #> array['0'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array[]::text[];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a','b','c','d'];