Commit 41dd50e8 authored by Tom Lane's avatar Tom Lane

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.
parent ebf20f65
...@@ -10152,10 +10152,14 @@ table2-mapping ...@@ -10152,10 +10152,14 @@ table2-mapping
<note> <note>
<para> <para>
There are parallel variants of these operators for both the There are parallel variants of these operators for both the
<type>json</type> and <type>jsonb</type> types. The operators <type>json</type> and <type>jsonb</type> types.
The field/element/path extraction operators
return the same type as their left-hand input (either <type>json</type> return the same type as their left-hand input (either <type>json</type>
or <type>jsonb</type>), except for those specified as or <type>jsonb</type>), except for those specified as
returning <type>text</>, which coerce the value to text. returning <type>text</>, which coerce the value to text.
The field/element/path extraction operators return NULL, rather than
failing, if the JSON input does not have the right structure to match
the request; for example if no such element exists.
</para> </para>
</note> </note>
<para> <para>
......
...@@ -415,9 +415,6 @@ SELECT jdoc-&gt;'guid', jdoc-&gt;'name' FROM api WHERE jdoc -&gt; 'tags' ? 'qui' ...@@ -415,9 +415,6 @@ SELECT jdoc-&gt;'guid', jdoc-&gt;'name' FROM api WHERE jdoc -&gt; 'tags' ? 'qui'
the <literal>"tags"</> key is common, defining an index like this the <literal>"tags"</> key is common, defining an index like this
may be worthwhile: may be worthwhile:
<programlisting> <programlisting>
-- Note that the "jsonb -&gt; text" operator can only be called on a JSON
-- object, so as a consequence of creating this index the root of each
-- "jdoc" value must be an object. This is enforced during insertion.
CREATE INDEX idxgintags ON api USING gin ((jdoc -&gt; 'tags')); CREATE INDEX idxgintags ON api USING gin ((jdoc -&gt; 'tags'));
</programlisting> </programlisting>
Now, the <literal>WHERE</> clause <literal>jdoc -&gt; 'tags' ? 'qui'</> Now, the <literal>WHERE</> clause <literal>jdoc -&gt; 'tags' ? 'qui'</>
......
...@@ -136,6 +136,39 @@ ...@@ -136,6 +136,39 @@
</para> </para>
</listitem> </listitem>
<listitem>
<para>
The <link linkend="functions-json-op-table"><type>json</type>
<literal>#&gt;</> <type>text[]</> path extraction operator</link> now
returns its lefthand input, not NULL, if the array is empty (Tom Lane)
</para>
<para>
This is consistent with the notion that this represents zero
applications of the simple field/element extraction
operator <literal>-&gt;</>. Similarly, <type>json</type>
<literal>#&gt;&gt;</> <type>text[]</> with an empty array merely
coerces its lefthand input to text.
</para>
</listitem>
<listitem>
<para>
Corner cases in
the <link linkend="functions-json-op-table"><type>JSON</type>
field/element/path extraction operators</link> now return NULL rather
than raising an error (Tom Lane)
</para>
<para>
For example, applying field extraction to a JSON array now yields NULL
not an error. This is more consistent (since some comparable cases such
as no-such-field already returned NULL), and it makes it safe to create
expression indexes that use these operators, since they will now not
throw errors for any valid JSON input.
</para>
</listitem>
<listitem> <listitem>
<para> <para>
Cause consecutive whitespace in <link Cause consecutive whitespace in <link
......
This diff is collapsed.
...@@ -506,11 +506,19 @@ INSERT INTO test_json VALUES ...@@ -506,11 +506,19 @@ INSERT INTO test_json VALUES
SELECT test_json -> 'x' SELECT test_json -> 'x'
FROM test_json FROM test_json
WHERE json_type = 'scalar'; WHERE json_type = 'scalar';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
SELECT test_json -> 'x' SELECT test_json -> 'x'
FROM test_json FROM test_json
WHERE json_type = 'array'; WHERE json_type = 'array';
ERROR: cannot extract field from a non-object ?column?
----------
(1 row)
SELECT test_json -> 'x' SELECT test_json -> 'x'
FROM test_json FROM test_json
WHERE json_type = 'object'; WHERE json_type = 'object';
...@@ -538,7 +546,11 @@ WHERE json_type = 'object'; ...@@ -538,7 +546,11 @@ WHERE json_type = 'object';
SELECT test_json -> 2 SELECT test_json -> 2
FROM test_json FROM test_json
WHERE json_type = 'scalar'; WHERE json_type = 'scalar';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
SELECT test_json -> 2 SELECT test_json -> 2
FROM test_json FROM test_json
WHERE json_type = 'array'; WHERE json_type = 'array';
...@@ -550,7 +562,11 @@ WHERE json_type = 'array'; ...@@ -550,7 +562,11 @@ WHERE json_type = 'array';
SELECT test_json -> 2 SELECT test_json -> 2
FROM test_json FROM test_json
WHERE json_type = 'object'; WHERE json_type = 'object';
ERROR: cannot extract array element from a non-array ?column?
----------
(1 row)
SELECT test_json->>2 SELECT test_json->>2
FROM test_json FROM test_json
WHERE json_type = 'array'; WHERE json_type = 'array';
...@@ -667,7 +683,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int; ...@@ -667,7 +683,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
?column? ?column?
---------- ----------
...@@ -693,11 +713,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json -> 3; ...@@ -693,11 +713,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z'; 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; select '"foo"'::json -> 1;
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
select '"foo"'::json -> 'z'; select '"foo"'::json -> 'z';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
?column? ?column?
---------- ----------
...@@ -711,7 +749,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int; ...@@ -711,7 +749,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
?column? ?column?
---------- ----------
...@@ -737,11 +779,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3; ...@@ -737,11 +779,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z'; 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; select '"foo"'::json ->> 1;
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
select '"foo"'::json ->> 'z'; select '"foo"'::json ->> 'z';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
-- array length -- array length
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]'); SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
json_array_length json_array_length
...@@ -922,9 +982,33 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1']; ...@@ -922,9 +982,33 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
-- corner cases for same -- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}'; select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
?column?
---------------------------
{"a": {"b":{"c": "foo"}}}
(1 row)
select '[1,2,3]'::json #> '{}';
?column? ?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) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a']; select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
...@@ -934,9 +1018,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a']; ...@@ -934,9 +1018,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null]; 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', '']; 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']; select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
?column? ?column?
-------------- --------------
...@@ -985,6 +1077,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b']; ...@@ -985,6 +1077,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
?column?
----------
null
(1 row)
select '"foo"'::json #> array['z']; select '"foo"'::json #> array['z'];
?column? ?column?
---------- ----------
...@@ -1004,6 +1102,30 @@ select '42'::json #> array['0']; ...@@ -1004,6 +1102,30 @@ select '42'::json #> array['0'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}'; 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? ?column?
---------- ----------
...@@ -1016,9 +1138,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a']; ...@@ -1016,9 +1138,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null]; 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', '']; 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']; select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
?column? ?column?
-------------- --------------
...@@ -1067,6 +1197,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b']; ...@@ -1067,6 +1197,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
?column?
----------
(1 row)
select '"foo"'::json #>> array['z']; select '"foo"'::json #>> array['z'];
?column? ?column?
---------- ----------
......
...@@ -506,11 +506,19 @@ INSERT INTO test_json VALUES ...@@ -506,11 +506,19 @@ INSERT INTO test_json VALUES
SELECT test_json -> 'x' SELECT test_json -> 'x'
FROM test_json FROM test_json
WHERE json_type = 'scalar'; WHERE json_type = 'scalar';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
SELECT test_json -> 'x' SELECT test_json -> 'x'
FROM test_json FROM test_json
WHERE json_type = 'array'; WHERE json_type = 'array';
ERROR: cannot extract field from a non-object ?column?
----------
(1 row)
SELECT test_json -> 'x' SELECT test_json -> 'x'
FROM test_json FROM test_json
WHERE json_type = 'object'; WHERE json_type = 'object';
...@@ -538,7 +546,11 @@ WHERE json_type = 'object'; ...@@ -538,7 +546,11 @@ WHERE json_type = 'object';
SELECT test_json -> 2 SELECT test_json -> 2
FROM test_json FROM test_json
WHERE json_type = 'scalar'; WHERE json_type = 'scalar';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
SELECT test_json -> 2 SELECT test_json -> 2
FROM test_json FROM test_json
WHERE json_type = 'array'; WHERE json_type = 'array';
...@@ -550,7 +562,11 @@ WHERE json_type = 'array'; ...@@ -550,7 +562,11 @@ WHERE json_type = 'array';
SELECT test_json -> 2 SELECT test_json -> 2
FROM test_json FROM test_json
WHERE json_type = 'object'; WHERE json_type = 'object';
ERROR: cannot extract array element from a non-array ?column?
----------
(1 row)
SELECT test_json->>2 SELECT test_json->>2
FROM test_json FROM test_json
WHERE json_type = 'array'; WHERE json_type = 'array';
...@@ -667,7 +683,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int; ...@@ -667,7 +683,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> 'z';
?column? ?column?
---------- ----------
...@@ -693,11 +713,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json -> 3; ...@@ -693,11 +713,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z'; 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; select '"foo"'::json -> 1;
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
select '"foo"'::json -> 'z'; select '"foo"'::json -> 'z';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::text;
?column? ?column?
---------- ----------
...@@ -711,7 +749,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int; ...@@ -711,7 +749,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> 'z';
?column? ?column?
---------- ----------
...@@ -737,11 +779,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3; ...@@ -737,11 +779,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z'; 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; select '"foo"'::json ->> 1;
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
select '"foo"'::json ->> 'z'; select '"foo"'::json ->> 'z';
ERROR: cannot extract element from a scalar ?column?
----------
(1 row)
-- array length -- array length
SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]'); SELECT json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]');
json_array_length json_array_length
...@@ -922,9 +982,33 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1']; ...@@ -922,9 +982,33 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
-- corner cases for same -- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}'; select '{"a": {"b":{"c": "foo"}}}'::json #> '{}';
?column?
---------------------------
{"a": {"b":{"c": "foo"}}}
(1 row)
select '[1,2,3]'::json #> '{}';
?column? ?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) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a']; select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
...@@ -934,9 +1018,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a']; ...@@ -934,9 +1018,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null]; 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', '']; 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']; select '{"a": {"b":{"c": "foo"}}}'::json #> array['a','b'];
?column? ?column?
-------------- --------------
...@@ -985,6 +1077,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b']; ...@@ -985,6 +1077,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::json #> array['1','b'];
?column?
----------
null
(1 row)
select '"foo"'::json #> array['z']; select '"foo"'::json #> array['z'];
?column? ?column?
---------- ----------
...@@ -1004,6 +1102,30 @@ select '42'::json #> array['0']; ...@@ -1004,6 +1102,30 @@ select '42'::json #> array['0'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}'; 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? ?column?
---------- ----------
...@@ -1016,9 +1138,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a']; ...@@ -1016,9 +1138,17 @@ select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null]; 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', '']; 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']; select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a','b'];
?column? ?column?
-------------- --------------
...@@ -1067,6 +1197,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b']; ...@@ -1067,6 +1197,12 @@ select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::json #>> array['1','b'];
?column?
----------
(1 row)
select '"foo"'::json #>> array['z']; select '"foo"'::json #>> array['z'];
?column? ?column?
---------- ----------
......
...@@ -311,9 +311,17 @@ INSERT INTO test_jsonb VALUES ...@@ -311,9 +311,17 @@ INSERT INTO test_jsonb VALUES
('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'), ('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}}'); ('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'; 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'; 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'; SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
?column? ?column?
---------- ----------
...@@ -327,9 +335,17 @@ SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object'; ...@@ -327,9 +335,17 @@ SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row) (1 row)
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar'; 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'; 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'; SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
?column? ?column?
---------- ----------
...@@ -337,7 +353,11 @@ SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object'; ...@@ -337,7 +353,11 @@ SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row) (1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar'; 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'; SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
?column? ?column?
---------- ----------
...@@ -351,7 +371,11 @@ SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array'; ...@@ -351,7 +371,11 @@ SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
(1 row) (1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object'; 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'; SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
?column? ?column?
----------- -----------
...@@ -383,7 +407,11 @@ SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object'; ...@@ -383,7 +407,11 @@ SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
(1 row) (1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar'; 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'; SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
?column? ?column?
---------- ----------
...@@ -391,7 +419,11 @@ SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array'; ...@@ -391,7 +419,11 @@ SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
(1 row) (1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object'; 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'; SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
ERROR: cannot call jsonb_object_keys on a scalar ERROR: cannot call jsonb_object_keys on a scalar
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array'; 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; ...@@ -446,7 +478,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
?column? ?column?
---------- ----------
...@@ -472,11 +508,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3; ...@@ -472,11 +508,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z'; 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; select '"foo"'::jsonb -> 1;
ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar ?column?
----------
(1 row)
select '"foo"'::jsonb -> 'z'; 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; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
?column? ?column?
---------- ----------
...@@ -490,7 +544,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int; ...@@ -490,7 +544,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
?column? ?column?
---------- ----------
...@@ -516,11 +574,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3; ...@@ -516,11 +574,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z'; 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; select '"foo"'::jsonb ->> 1;
ERROR: cannot call jsonb_array_element_text on a scalar ?column?
----------
(1 row)
select '"foo"'::jsonb ->> 'z'; select '"foo"'::jsonb ->> 'z';
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar ?column?
----------
(1 row)
-- equality and inequality -- equality and inequality
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb; SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
?column? ?column?
...@@ -1269,9 +1345,33 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1']; ...@@ -1269,9 +1345,33 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
-- corner cases for same -- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}'; 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? ?column?
---------- ----------
"foo"
(1 row)
select '42'::jsonb #> '{}';
?column?
----------
42
(1 row)
select 'null'::jsonb #> '{}';
?column?
----------
null
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a']; select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
...@@ -1281,7 +1381,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a']; ...@@ -1281,7 +1381,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null]; 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', '']; select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
?column? ?column?
---------- ----------
...@@ -1336,13 +1440,55 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b']; ...@@ -1336,13 +1440,55 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
?column?
----------
null
(1 row)
select '"foo"'::jsonb #> array['z']; select '"foo"'::jsonb #> array['z'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #> array['f2']; select '42'::jsonb #> array['f2'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #> array['0']; select '42'::jsonb #> array['0'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}'; 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? ?column?
---------- ----------
...@@ -1355,7 +1501,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a']; ...@@ -1355,7 +1501,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null]; 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', '']; select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
?column? ?column?
---------- ----------
...@@ -1410,12 +1560,30 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b']; ...@@ -1410,12 +1560,30 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
?column?
----------
(1 row)
select '"foo"'::jsonb #>> array['z']; select '"foo"'::jsonb #>> array['z'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #>> array['f2']; select '42'::jsonb #>> array['f2'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #>> array['0']; select '42'::jsonb #>> array['0'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
-- array_elements -- array_elements
SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]'); SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
jsonb_array_elements jsonb_array_elements
...@@ -2105,7 +2273,11 @@ SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e'; ...@@ -2105,7 +2273,11 @@ SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
(1 row) (1 row)
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error 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; SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
?column? ?column?
---------- ----------
......
...@@ -311,9 +311,17 @@ INSERT INTO test_jsonb VALUES ...@@ -311,9 +311,17 @@ INSERT INTO test_jsonb VALUES
('array','["zero", "one","two",null,"four","five", [1,2,3],{"f1":9}]'), ('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}}'); ('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'; 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'; 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'; SELECT test_json -> 'x' FROM test_jsonb WHERE json_type = 'object';
?column? ?column?
---------- ----------
...@@ -327,9 +335,17 @@ SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object'; ...@@ -327,9 +335,17 @@ SELECT test_json -> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row) (1 row)
SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'scalar'; 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'; 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'; SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
?column? ?column?
---------- ----------
...@@ -337,7 +353,11 @@ SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object'; ...@@ -337,7 +353,11 @@ SELECT test_json ->> 'field2' FROM test_jsonb WHERE json_type = 'object';
(1 row) (1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'scalar'; 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'; SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'array';
?column? ?column?
---------- ----------
...@@ -351,7 +371,11 @@ SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array'; ...@@ -351,7 +371,11 @@ SELECT test_json -> 9 FROM test_jsonb WHERE json_type = 'array';
(1 row) (1 row)
SELECT test_json -> 2 FROM test_jsonb WHERE json_type = 'object'; 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'; SELECT test_json ->> 6 FROM test_jsonb WHERE json_type = 'array';
?column? ?column?
----------- -----------
...@@ -383,7 +407,11 @@ SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object'; ...@@ -383,7 +407,11 @@ SELECT test_json ->> 'field6' FROM test_jsonb WHERE json_type = 'object';
(1 row) (1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'scalar'; 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'; SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
?column? ?column?
---------- ----------
...@@ -391,7 +419,11 @@ SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array'; ...@@ -391,7 +419,11 @@ SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'array';
(1 row) (1 row)
SELECT test_json ->> 2 FROM test_jsonb WHERE json_type = 'object'; 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'; SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'scalar';
ERROR: cannot call jsonb_object_keys on a scalar ERROR: cannot call jsonb_object_keys on a scalar
SELECT jsonb_object_keys(test_json) FROM test_jsonb WHERE json_type = 'array'; 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; ...@@ -446,7 +478,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> 'z';
?column? ?column?
---------- ----------
...@@ -472,11 +508,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3; ...@@ -472,11 +508,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z'; 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; select '"foo"'::jsonb -> 1;
ERROR: cannot call jsonb_array_element (jsonb -> int) on a scalar ?column?
----------
(1 row)
select '"foo"'::jsonb -> 'z'; 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; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::text;
?column? ?column?
---------- ----------
...@@ -490,7 +544,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int; ...@@ -490,7 +544,11 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> null::int;
(1 row) (1 row)
select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 1; 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'; select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> 'z';
?column? ?column?
---------- ----------
...@@ -516,11 +574,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3; ...@@ -516,11 +574,29 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
(1 row) (1 row)
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z'; 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; select '"foo"'::jsonb ->> 1;
ERROR: cannot call jsonb_array_element_text on a scalar ?column?
----------
(1 row)
select '"foo"'::jsonb ->> 'z'; select '"foo"'::jsonb ->> 'z';
ERROR: cannot call jsonb_object_field_text (jsonb ->> text) on a scalar ?column?
----------
(1 row)
-- equality and inequality -- equality and inequality
SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb; SELECT '{"x":"y"}'::jsonb = '{"x":"y"}'::jsonb;
?column? ?column?
...@@ -1269,9 +1345,33 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1']; ...@@ -1269,9 +1345,33 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
-- corner cases for same -- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}'; 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? ?column?
---------- ----------
"foo"
(1 row)
select '42'::jsonb #> '{}';
?column?
----------
42
(1 row)
select 'null'::jsonb #> '{}';
?column?
----------
null
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a']; select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
...@@ -1281,7 +1381,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a']; ...@@ -1281,7 +1381,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null]; 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', '']; select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', ''];
?column? ?column?
---------- ----------
...@@ -1336,13 +1440,55 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b']; ...@@ -1336,13 +1440,55 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::jsonb #> array['1','b'];
?column?
----------
null
(1 row)
select '"foo"'::jsonb #> array['z']; select '"foo"'::jsonb #> array['z'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #> array['f2']; select '42'::jsonb #> array['f2'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #> array['0']; select '42'::jsonb #> array['0'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}'; 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? ?column?
---------- ----------
...@@ -1355,7 +1501,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a']; ...@@ -1355,7 +1501,11 @@ select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a'];
(1 row) (1 row)
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null]; 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', '']; select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', ''];
?column? ?column?
---------- ----------
...@@ -1410,12 +1560,30 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b']; ...@@ -1410,12 +1560,30 @@ select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','b'];
(1 row) (1 row)
select '[{"b": "c"}, {"b": null}]'::jsonb #>> array['1','b'];
?column?
----------
(1 row)
select '"foo"'::jsonb #>> array['z']; select '"foo"'::jsonb #>> array['z'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #>> array['f2']; select '42'::jsonb #>> array['f2'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
select '42'::jsonb #>> array['0']; select '42'::jsonb #>> array['0'];
ERROR: cannot extract path from a scalar ?column?
----------
(1 row)
-- array_elements -- array_elements
SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]'); SELECT jsonb_array_elements('[1,true,[1,[2,3]],null,{"f1":1,"f2":[7,8,9]},false]');
jsonb_array_elements jsonb_array_elements
...@@ -2105,7 +2273,11 @@ SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e'; ...@@ -2105,7 +2273,11 @@ SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'e';
(1 row) (1 row)
SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 0; --expecting error 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; SELECT '["a","b","c",[1,2],null]'::jsonb -> 0;
?column? ?column?
---------- ----------
......
...@@ -248,6 +248,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> ''; ...@@ -248,6 +248,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json -> '';
select '[{"b": "c"}, {"b": "cc"}]'::json -> 1; select '[{"b": "c"}, {"b": "cc"}]'::json -> 1;
select '[{"b": "c"}, {"b": "cc"}]'::json -> 3; select '[{"b": "c"}, {"b": "cc"}]'::json -> 3;
select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z'; select '[{"b": "c"}, {"b": "cc"}]'::json -> 'z';
select '{"a": "c", "b": null}'::json -> 'b';
select '"foo"'::json -> 1; select '"foo"'::json -> 1;
select '"foo"'::json -> 'z'; select '"foo"'::json -> 'z';
...@@ -259,6 +260,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> ''; ...@@ -259,6 +260,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json ->> '';
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1; select '[{"b": "c"}, {"b": "cc"}]'::json ->> 1;
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3; select '[{"b": "c"}, {"b": "cc"}]'::json ->> 3;
select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z'; select '[{"b": "c"}, {"b": "cc"}]'::json ->> 'z';
select '{"a": "c", "b": null}'::json ->> 'b';
select '"foo"'::json ->> 1; select '"foo"'::json ->> 1;
select '"foo"'::json ->> 'z'; select '"foo"'::json ->> 'z';
...@@ -312,6 +314,10 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1']; ...@@ -312,6 +314,10 @@ select '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::json#>>array['f2','1'];
-- corner cases for same -- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::json #> '{}'; 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'];
select '{"a": {"b":{"c": "foo"}}}'::json #> array['a', null]; 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', ''];
...@@ -323,11 +329,16 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #> array['a','1','b']; ...@@ -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 '{"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['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::json #> array['z','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 '"foo"'::json #> array['z'];
select '42'::json #> array['f2']; select '42'::json #> array['f2'];
select '42'::json #> array['0']; select '42'::json #> array['0'];
select '{"a": {"b":{"c": "foo"}}}'::json #>> '{}'; 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'];
select '{"a": {"b":{"c": "foo"}}}'::json #>> array['a', null]; 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', ''];
...@@ -339,6 +350,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::json #>> array['a','1','b']; ...@@ -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 '{"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['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::json #>> array['z','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 '"foo"'::json #>> array['z'];
select '42'::json #>> array['f2']; select '42'::json #>> array['f2'];
select '42'::json #>> array['0']; select '42'::json #>> array['0'];
......
...@@ -117,6 +117,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> ''; ...@@ -117,6 +117,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb -> '';
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1; select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 1;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3; select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 3;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z'; select '[{"b": "c"}, {"b": "cc"}]'::jsonb -> 'z';
select '{"a": "c", "b": null}'::jsonb -> 'b';
select '"foo"'::jsonb -> 1; select '"foo"'::jsonb -> 1;
select '"foo"'::jsonb -> 'z'; select '"foo"'::jsonb -> 'z';
...@@ -128,6 +129,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> ''; ...@@ -128,6 +129,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb ->> '';
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1; select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 1;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3; select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 3;
select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z'; select '[{"b": "c"}, {"b": "cc"}]'::jsonb ->> 'z';
select '{"a": "c", "b": null}'::jsonb ->> 'b';
select '"foo"'::jsonb ->> 1; select '"foo"'::jsonb ->> 1;
select '"foo"'::jsonb ->> 'z'; select '"foo"'::jsonb ->> 'z';
...@@ -283,6 +285,10 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1']; ...@@ -283,6 +285,10 @@ SELECT '{"f2":["f3",1],"f4":{"f5":99,"f6":"stringy"}}'::jsonb#>>array['f2','1'];
-- corner cases for same -- corner cases for same
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> '{}'; 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'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #> array['a', null]; 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', ''];
...@@ -294,11 +300,16 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #> array['a','1','b']; ...@@ -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 '{"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['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::jsonb #> array['z','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 '"foo"'::jsonb #> array['z'];
select '42'::jsonb #> array['f2']; select '42'::jsonb #> array['f2'];
select '42'::jsonb #> array['0']; select '42'::jsonb #> array['0'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> '{}'; 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'];
select '{"a": {"b":{"c": "foo"}}}'::jsonb #>> array['a', null]; 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', ''];
...@@ -310,6 +321,7 @@ select '{"a": [{"b": "c"}, {"b": "cc"}]}'::jsonb #>> array['a','1','b']; ...@@ -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 '{"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['1','b'];
select '[{"b": "c"}, {"b": "cc"}]'::jsonb #>> array['z','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 '"foo"'::jsonb #>> array['z'];
select '42'::jsonb #>> array['f2']; select '42'::jsonb #>> array['f2'];
select '42'::jsonb #>> array['0']; select '42'::jsonb #>> array['0'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment