summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane2014-08-22 17:17:58 +0000
committerTom Lane2014-08-22 17:17:58 +0000
commit41dd50e84df39e31595f3472b0cb6d00f63b3f99 (patch)
tree34d4d479d154d440d7f8ed6e0d43905af9917e55 /src/test
parentebf20f65d9383166d4b883258c091b245cf36217 (diff)
Fix corner-case behaviors in JSON/JSONB field extraction operators.
Cause the path extraction operators to return their lefthand input, not NULL, if the path array has no elements. This seems more consistent since the case ought to correspond to applying the simple extraction operator (->) zero times. Cause other corner cases in field/element/path extraction to return NULL rather than failing. This behavior is arguably more useful than throwing an error, since it allows an expression index using these operators to be built even when not all values in the column are suitable for the extraction being indexed. Moreover, we already had multiple inconsistencies between the path extraction operators and the simple extraction operators, as well as inconsistencies between the JSON and JSONB code paths. Adopt a uniform rule of returning NULL rather than throwing an error when the JSON input does not have a structure that permits the request to be satisfied. Back-patch to 9.4. Update the release notes to list this as a behavior change since 9.3.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/json.out170
-rw-r--r--src/test/regress/expected/json_1.out170
-rw-r--r--src/test/regress/expected/jsonb.out224
-rw-r--r--src/test/regress/expected/jsonb_1.out224
-rw-r--r--src/test/regress/sql/json.sql12
-rw-r--r--src/test/regress/sql/jsonb.sql12
6 files changed, 726 insertions, 86 deletions
diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out
index b438e49bf9..bb4d9ed4be 100644
--- a/src/test/regress/expected/json.out
+++ b/src/test/regress/expected/json.out
@@ -506,11 +506,19 @@ INSERT INTO test_json VALUES
SELECT test_json -> 'x'
FROM test_json
WHERE json_type = 'scalar';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x'
FROM test_json
WHERE json_type = 'array';
-ERROR: cannot extract field from a non-object
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x'
FROM test_json
WHERE json_type = 'object';
@@ -538,7 +546,11 @@ WHERE json_type = 'object';
SELECT test_json -> 2
FROM test_json
WHERE json_type = 'scalar';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 2
FROM test_json
WHERE json_type = 'array';
@@ -550,7 +562,11 @@ WHERE json_type = 'array';
SELECT test_json -> 2
FROM test_json
WHERE json_type = 'object';
-ERROR: cannot extract array element from a non-array
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json->>2
FROM test_json
WHERE json_type = 'array';
@@ -667,7 +683,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
-ERROR: cannot extract array element from a non-array
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
?column?
----------
@@ -693,11 +713,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
-ERROR: cannot extract field from a non-object
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::json -> 'b';
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::json -> 1;
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json -> 'z';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
?column?
----------
@@ -711,7 +749,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
-ERROR: cannot extract array element from a non-array
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
?column?
----------
@@ -737,11 +779,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
-ERROR: cannot extract field from a non-object
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::json ->> 'b';
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json ->> 1;
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json ->> 'z';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
-- array length
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
json_array_length
@@ -922,9 +982,33 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
+ ?column?
+---------------------------
+ {"a": {"b":{"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::json #> '{}';
?column?
----------
-
+ [1,2,3]
+(1 row)
+
+select '"foo"'::json #> '{}';
+ ?column?
+----------
+ "foo"
+(1 row)
+
+select '42'::json #> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::json #> '{}';
+ ?column?
+----------
+ null
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
@@ -934,9 +1018,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
-ERROR: cannot call json_extract_path with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
-ERROR: cannot call json_extract_path with empty path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
?column?
--------------
@@ -985,6 +1077,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::json #> array['z'];
?column?
----------
@@ -1004,6 +1102,30 @@ select '42'::json #> array['0'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
+ ?column?
+---------------------------
+ {"a": {"b":{"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::json #>> '{}';
+ ?column?
+----------
+ [1,2,3]
+(1 row)
+
+select '"foo"'::json #>> '{}';
+ ?column?
+----------
+ foo
+(1 row)
+
+select '42'::json #>> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::json #>> '{}';
?column?
----------
@@ -1016,9 +1138,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
-ERROR: cannot call json_extract_path_text with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
-ERROR: cannot call json_extract_path_text with empty path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
?column?
--------------
@@ -1067,6 +1197,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json #>> array['z'];
?column?
----------
diff --git a/src/test/regress/expected/json_1.out b/src/test/regress/expected/json_1.out
index 077fcbd0ed..83c1d7d492 100644
--- a/src/test/regress/expected/json_1.out
+++ b/src/test/regress/expected/json_1.out
@@ -506,11 +506,19 @@ INSERT INTO test_json VALUES
SELECT test_json -> 'x'
FROM test_json
WHERE json_type = 'scalar';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x'
FROM test_json
WHERE json_type = 'array';
-ERROR: cannot extract field from a non-object
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x'
FROM test_json
WHERE json_type = 'object';
@@ -538,7 +546,11 @@ WHERE json_type = 'object';
SELECT test_json -> 2
FROM test_json
WHERE json_type = 'scalar';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 2
FROM test_json
WHERE json_type = 'array';
@@ -550,7 +562,11 @@ WHERE json_type = 'array';
SELECT test_json -> 2
FROM test_json
WHERE json_type = 'object';
-ERROR: cannot extract array element from a non-array
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json->>2
FROM test_json
WHERE json_type = 'array';
@@ -667,7 +683,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1;
-ERROR: cannot extract array element from a non-array
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
?column?
----------
@@ -693,11 +713,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
-ERROR: cannot extract field from a non-object
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::json -> 'b';
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::json -> 1;
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json -> 'z';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
?column?
----------
@@ -711,7 +749,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1;
-ERROR: cannot extract array element from a non-array
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
?column?
----------
@@ -737,11 +779,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
-ERROR: cannot extract field from a non-object
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::json ->> 'b';
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json ->> 1;
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json ->> 'z';
-ERROR: cannot extract element from a scalar
+ ?column?
+----------
+
+(1 row)
+
-- array length
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
json_array_length
@@ -922,9 +982,33 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
+ ?column?
+---------------------------
+ {"a": {"b":{"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::json #> '{}';
?column?
----------
-
+ [1,2,3]
+(1 row)
+
+select '"foo"'::json #> '{}';
+ ?column?
+----------
+ "foo"
+(1 row)
+
+select '42'::json #> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::json #> '{}';
+ ?column?
+----------
+ null
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
@@ -934,9 +1018,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null];
-ERROR: cannot call json_extract_path with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', ''];
-ERROR: cannot call json_extract_path with empty path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
?column?
--------------
@@ -985,6 +1077,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::json #> array['z'];
?column?
----------
@@ -1004,6 +1102,30 @@ select '42'::json #> array['0'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
+ ?column?
+---------------------------
+ {"a": {"b":{"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::json #>> '{}';
+ ?column?
+----------
+ [1,2,3]
+(1 row)
+
+select '"foo"'::json #>> '{}';
+ ?column?
+----------
+ foo
+(1 row)
+
+select '42'::json #>> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::json #>> '{}';
?column?
----------
@@ -1016,9 +1138,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null];
-ERROR: cannot call json_extract_path_text with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', ''];
-ERROR: cannot call json_extract_path_text with empty path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
?column?
--------------
@@ -1067,6 +1197,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::json #>> array['z'];
?column?
----------
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out
index ea4d6e1f4c..eb37da7168 100644
--- a/src/test/regress/expected/jsonb.out
+++ b/src/test/regress/expected/jsonb.out
@@ -311,9 +311,17 @@ INSERT INTO test_jsonb VALUES
('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on an array
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
?column?
----------
@@ -327,9 +335,17 @@ SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row)
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on an array
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
?column?
----------
@@ -337,7 +353,11 @@ SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
?column?
----------
@@ -351,7 +371,11 @@ SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
(1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
-ERROR: cannot call jsonb_array_element (jsonb -> int) on an object
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
?column?
-----------
@@ -383,7 +407,11 @@ SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
(1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_array_element_text on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
?column?
----------
@@ -391,7 +419,11 @@ SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
(1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
-ERROR: cannot call jsonb_array_element_text on an object
+ ?column?
+----------
+
+(1 row)
+
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
ERROR: cannot call jsonb_object_keys on a scalar
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array';
@@ -446,7 +478,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
-ERROR: cannot call jsonb_array_element (jsonb -> int) on an object
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
?column?
----------
@@ -472,11 +508,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on an array
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::jsonb -> 'b';
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::jsonb -> 1;
-ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb -> 'z';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
?column?
----------
@@ -490,7 +544,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
-ERROR: cannot call jsonb_array_element_text on an object
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
?column?
----------
@@ -516,11 +574,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on an array
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::jsonb ->> 'b';
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb ->> 1;
-ERROR: cannot call jsonb_array_element_text on a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb ->> 'z';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
-- equality and inequality
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
?column?
@@ -1269,9 +1345,33 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
+ ?column?
+----------------------------
+ {"a": {"b": {"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::jsonb #> '{}';
+ ?column?
+-----------
+ [1, 2, 3]
+(1 row)
+
+select '"foo"'::jsonb #> '{}';
?column?
----------
-
+ "foo"
+(1 row)
+
+select '42'::jsonb #> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::jsonb #> '{}';
+ ?column?
+----------
+ null
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
@@ -1281,7 +1381,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
-ERROR: cannot call jsonb_extract_path with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
?column?
----------
@@ -1336,13 +1440,55 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::jsonb #> array['z'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #> array['f2'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #> array['0'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
+ ?column?
+----------------------------
+ {"a": {"b": {"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::jsonb #>> '{}';
+ ?column?
+-----------
+ [1, 2, 3]
+(1 row)
+
+select '"foo"'::jsonb #>> '{}';
+ ?column?
+----------
+ foo
+(1 row)
+
+select '42'::jsonb #>> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::jsonb #>> '{}';
?column?
----------
@@ -1355,7 +1501,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
-ERROR: cannot call jsonb_extract_path_text with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
?column?
----------
@@ -1410,12 +1560,30 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb #>> array['z'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #>> array['f2'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #>> array['0'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
-- array_elements
SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
jsonb_array_elements
@@ -2105,7 +2273,11 @@ SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
(1 row)
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
-ERROR: cannot call jsonb_array_element (jsonb -> int) on an object
+ ?column?
+----------
+
+(1 row)
+
SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
?column?
----------
diff --git a/src/test/regress/expected/jsonb_1.out b/src/test/regress/expected/jsonb_1.out
index 4c2d5ae0b3..f3bfc7bcf5 100644
--- a/src/test/regress/expected/jsonb_1.out
+++ b/src/test/regress/expected/jsonb_1.out
@@ -311,9 +311,17 @@ INSERT INTO test_jsonb VALUES
('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'),
('object','{"field1":"val1","field2":"val2","field3":null, "field4": 4, "field5": [1,2,3], "field6": {"f1":9}}');
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'array';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on an array
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
?column?
----------
@@ -327,9 +335,17 @@ SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row)
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'array';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on an array
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
?column?
----------
@@ -337,7 +353,11 @@ SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
?column?
----------
@@ -351,7 +371,11 @@ SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
(1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object';
-ERROR: cannot call jsonb_array_element (jsonb -> int) on an object
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
?column?
-----------
@@ -383,7 +407,11 @@ SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
(1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar';
-ERROR: cannot call jsonb_array_element_text on a scalar
+ ?column?
+----------
+
+(1 row)
+
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
?column?
----------
@@ -391,7 +419,11 @@ SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
(1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object';
-ERROR: cannot call jsonb_array_element_text on an object
+ ?column?
+----------
+
+(1 row)
+
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
ERROR: cannot call jsonb_object_keys on a scalar
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array';
@@ -446,7 +478,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1;
-ERROR: cannot call jsonb_array_element (jsonb -> int) on an object
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
?column?
----------
@@ -472,11 +508,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on an array
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::jsonb -> 'b';
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::jsonb -> 1;
-ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb -> 'z';
-ERROR: cannot call jsonb_object_field (jsonb -> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
?column?
----------
@@ -490,7 +544,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1;
-ERROR: cannot call jsonb_array_element_text on an object
+ ?column?
+----------
+
+(1 row)
+
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
?column?
----------
@@ -516,11 +574,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
(1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on an array
+ ?column?
+----------
+
+(1 row)
+
+select '{"a": "c", "b": null}'::jsonb ->> 'b';
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb ->> 1;
-ERROR: cannot call jsonb_array_element_text on a scalar
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb ->> 'z';
-ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar
+ ?column?
+----------
+
+(1 row)
+
-- equality and inequality
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
?column?
@@ -1269,9 +1345,33 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
+ ?column?
+----------------------------
+ {"a": {"b": {"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::jsonb #> '{}';
+ ?column?
+-----------
+ [1, 2, 3]
+(1 row)
+
+select '"foo"'::jsonb #> '{}';
?column?
----------
-
+ "foo"
+(1 row)
+
+select '42'::jsonb #> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::jsonb #> '{}';
+ ?column?
+----------
+ null
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
@@ -1281,7 +1381,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null];
-ERROR: cannot call jsonb_extract_path with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
?column?
----------
@@ -1336,13 +1440,55 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
+ ?column?
+----------
+ null
+(1 row)
+
select '"foo"'::jsonb #> array['z'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #> array['f2'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #> array['0'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
+ ?column?
+----------------------------
+ {"a": {"b": {"c": "foo"}}}
+(1 row)
+
+select '[1,2,3]'::jsonb #>> '{}';
+ ?column?
+-----------
+ [1, 2, 3]
+(1 row)
+
+select '"foo"'::jsonb #>> '{}';
+ ?column?
+----------
+ foo
+(1 row)
+
+select '42'::jsonb #>> '{}';
+ ?column?
+----------
+ 42
+(1 row)
+
+select 'null'::jsonb #>> '{}';
?column?
----------
@@ -1355,7 +1501,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null];
-ERROR: cannot call jsonb_extract_path_text with null path elements
+ ?column?
+----------
+
+(1 row)
+
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
?column?
----------
@@ -1410,12 +1560,30 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
(1 row)
+select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
+ ?column?
+----------
+
+(1 row)
+
select '"foo"'::jsonb #>> array['z'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #>> array['f2'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
select '42'::jsonb #>> array['0'];
-ERROR: cannot extract path from a scalar
+ ?column?
+----------
+
+(1 row)
+
-- array_elements
SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
jsonb_array_elements
@@ -2105,7 +2273,11 @@ SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
(1 row)
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error
-ERROR: cannot call jsonb_array_element (jsonb -> int) on an object
+ ?column?
+----------
+
+(1 row)
+
SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
?column?
----------
diff --git a/src/test/regress/sql/json.sql b/src/test/regress/sql/json.sql
index 4db5547401..c9801321e0 100644
--- a/src/test/regress/sql/json.sql
+++ b/src/test/regress/sql/json.sql
@@ -248,6 +248,7 @@ 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 '{"a": "c", "b": null}'::json -> 'b';
select '"foo"'::json -> 1;
select '"foo"'::json -> 'z';
@@ -259,6 +260,7 @@ 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 '{"a": "c", "b": null}'::json ->> 'b';
select '"foo"'::json ->> 1;
select '"foo"'::json ->> 'z';
@@ -312,6 +314,10 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
+select '[1,2,3]'::json #> '{}';
+select '"foo"'::json #> '{}';
+select '42'::json #> '{}';
+select 'null'::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', ''];
@@ -323,11 +329,16 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b'];
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','z','b'];
select '[{"b": "c"}, {"b": "cc"}]'::json #> array['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
+select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
select '"foo"'::json #> array['z'];
select '42'::json #> array['f2'];
select '42'::json #> array['0'];
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}';
+select '[1,2,3]'::json #>> '{}';
+select '"foo"'::json #>> '{}';
+select '42'::json #>> '{}';
+select 'null'::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', ''];
@@ -339,6 +350,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b'];
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','z','b'];
select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
+select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
select '"foo"'::json #>> array['z'];
select '42'::json #>> array['f2'];
select '42'::json #>> array['0'];
diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql
index 141dda9508..ed266d5c88 100644
--- a/src/test/regress/sql/jsonb.sql
+++ b/src/test/regress/sql/jsonb.sql
@@ -117,6 +117,7 @@ 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 '{"a": "c", "b": null}'::jsonb -> 'b';
select '"foo"'::jsonb -> 1;
select '"foo"'::jsonb -> 'z';
@@ -128,6 +129,7 @@ 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 '{"a": "c", "b": null}'::jsonb ->> 'b';
select '"foo"'::jsonb ->> 1;
select '"foo"'::jsonb ->> 'z';
@@ -283,6 +285,10 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
-- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}';
+select '[1,2,3]'::jsonb #> '{}';
+select '"foo"'::jsonb #> '{}';
+select '42'::jsonb #> '{}';
+select 'null'::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', ''];
@@ -294,11 +300,16 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b'];
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','z','b'];
select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
+select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
select '"foo"'::jsonb #> array['z'];
select '42'::jsonb #> array['f2'];
select '42'::jsonb #> array['0'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}';
+select '[1,2,3]'::jsonb #>> '{}';
+select '"foo"'::jsonb #>> '{}';
+select '42'::jsonb #>> '{}';
+select 'null'::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', ''];
@@ -310,6 +321,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b'];
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','z','b'];
select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
+select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
select '"foo"'::jsonb #>> array['z'];
select '42'::jsonb #>> array['f2'];
select '42'::jsonb #>> array['0'];