Fix json_to_record() bug with nested objects.

A thinko concerning nesting depth caused json_to_record() to produce bogus
output if a field of its input object contained a sub-object with a field
name matching one of the requested output column names.  Per bug #13996
from Johann Visagie.

I added a regression test case based on his example, plus parallel tests
for json_to_recordset, jsonb_to_record, jsonb_to_recordset.  The latter
three do not exhibit the same bug (which suggests that we may be missing
some opportunities to share code...) but testing seems like a good idea
in any case.

Back-patch to 9.4 where these functions were introduced.
This commit is contained in:
Tom Lane 2016-03-02 23:31:39 -05:00
parent 55965eb7d2
commit 597e41e45e
5 changed files with 49 additions and 2 deletions

View File

@ -2350,7 +2350,7 @@ hash_object_field_end(void *state, char *fname, bool isnull)
/*
* Ignore nested fields.
*/
if (_state->lex->lex_level > 2)
if (_state->lex->lex_level > 1)
return;
/*

View File

@ -1552,3 +1552,19 @@ select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":fa
2 | {"d":"bar"} | f
(2 rows)
select *, c is null as c_is_null
from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
as t(a int, b json, c text, x int);
a | b | c | x | c_is_null
---+-----------------+---+---+-----------
1 | {"c":16, "d":2} | | 8 | t
(1 row)
select *, c is null as c_is_null
from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
as t(a int, b json, c text, x int);
a | b | c | x | c_is_null
---+-----------------+---+---+-----------
1 | {"c":16, "d":2} | | 8 | t
(1 row)

View File

@ -1760,6 +1760,22 @@ select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar",
2 | bar | t
(2 rows)
select *, c is null as c_is_null
from jsonb_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::jsonb)
as t(a int, b jsonb, c text, x int);
a | b | c | x | c_is_null
---+-------------------+---+---+-----------
1 | {"c": 16, "d": 2} | | 8 | t
(1 row)
select *, c is null as c_is_null
from jsonb_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::jsonb)
as t(a int, b jsonb, c text, x int);
a | b | c | x | c_is_null
---+-------------------+---+---+-----------
1 | {"c": 16, "d": 2} | | 8 | t
(1 row)
-- indexing
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
count

View File

@ -487,7 +487,6 @@ select json_object('{a,b,NULL,"d e f"}','{1,2,3,"a b c"}');
select json_object('{a,b,"","d e f"}','{1,2,3,"a b c"}');
-- json_to_record and json_to_recordset
select * from json_to_record('{"a":1,"b":"foo","c":"bar"}')
@ -498,3 +497,11 @@ select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","
select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]')
as x(a int, b json, c boolean);
select *, c is null as c_is_null
from json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
as t(a int, b json, c text, x int);
select *, c is null as c_is_null
from json_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::json)
as t(a int, b json, c text, x int);

View File

@ -372,6 +372,14 @@ select * from jsonb_to_record('{"a":1,"b":"foo","c":"bar"}')
select * from jsonb_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]')
as x(a int, b text, c boolean);
select *, c is null as c_is_null
from jsonb_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::jsonb)
as t(a int, b jsonb, c text, x int);
select *, c is null as c_is_null
from jsonb_to_recordset('[{"a":1, "b":{"c":16, "d":2}, "x":8}]'::jsonb)
as t(a int, b jsonb, c text, x int);
-- indexing
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":null}';
SELECT count(*) FROM testjsonb WHERE j @> '{"wait":"CC"}';