From 57d8c1270e1538d1f02e4fa1cdb1d8ded82f7c70 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 24 Jun 2014 21:22:40 -0700 Subject: Fix handling of nested JSON objects in json_populate_recordset and friends. populate_recordset_object_start() improperly created a new hash table (overwriting the link to the existing one) if called at nest levels greater than one. This resulted in previous fields not appearing in the final output, as reported by Matti Hameister in bug #10728. In 9.4 the problem also affects json_to_recordset. This perhaps missed detection earlier because the default behavior is to throw an error for nested objects: you have to pass use_json_as_text = true to see the problem. In addition, fix query-lifespan leakage of the hashtable created by json_populate_record(). This is pretty much the same problem recently fixed in dblink: creating an intended-to-be-temporary context underneath the executor's per-tuple context isn't enough to make it go away at the end of the tuple cycle, because MemoryContextReset is not MemoryContextResetAndDeleteChildren. Michael Paquier and Tom Lane --- src/test/regress/expected/json.out | 15 +++++++++++++++ src/test/regress/expected/json_1.out | 15 +++++++++++++++ src/test/regress/sql/json.sql | 6 ++++++ 3 files changed, 36 insertions(+) (limited to 'src/test') diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out index 8b8556b2e04..d1e32a19a52 100644 --- a/src/test/regress/expected/json.out +++ b/src/test/regress/expected/json.out @@ -1007,6 +1007,13 @@ select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,3 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]',true) q; ERROR: invalid input syntax for type timestamp: "[100,200,300]" +create type jpop2 as (a int, b json, c int, d int); +select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]',true) q; + a | b | c | d +---+---------+---+--- + 2 | {"z":4} | 3 | 6 +(1 row) + -- using the default use_json_as_text argument select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; a | b | c @@ -1223,3 +1230,11 @@ select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar"," 2 | bar | t (2 rows) +select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]', true) + as x(a int, b json, c boolean); + a | b | c +---+-------------+--- + 1 | {"d":"foo"} | t + 2 | {"d":"bar"} | f +(2 rows) + diff --git a/src/test/regress/expected/json_1.out b/src/test/regress/expected/json_1.out index b32c3ed09f3..93cb693b2fb 100644 --- a/src/test/regress/expected/json_1.out +++ b/src/test/regress/expected/json_1.out @@ -1007,6 +1007,13 @@ select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,3 select * from json_populate_recordset(row('def',99,null)::jpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]',true) q; ERROR: invalid input syntax for type timestamp: "[100,200,300]" +create type jpop2 as (a int, b json, c int, d int); +select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]',true) q; + a | b | c | d +---+---------+---+--- + 2 | {"z":4} | 3 | 6 +(1 row) + -- using the default use_json_as_text argument select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; a | b | c @@ -1219,3 +1226,11 @@ select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar"," 2 | bar | t (2 rows) +select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]', true) + as x(a int, b json, c boolean); + a | b | c +---+-------------+--- + 1 | {"d":"foo"} | t + 2 | {"d":"bar"} | f +(2 rows) + diff --git a/src/test/regress/sql/json.sql b/src/test/regress/sql/json.sql index 3d5ed50126e..bc8bb629781 100644 --- a/src/test/regress/sql/json.sql +++ b/src/test/regress/sql/json.sql @@ -325,6 +325,9 @@ select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":"blurfl"," select * from json_populate_recordset(row('def',99,null)::jpop,'[{"a":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]',true) q; select * from json_populate_recordset(row('def',99,null)::jpop,'[{"c":[100,200,300],"x":43.2},{"a":{"z":true},"b":3,"c":"2012-01-20 10:42:53"}]',true) q; +create type jpop2 as (a int, b json, c int, d int); +select * from json_populate_recordset(null::jpop2, '[{"a":2,"c":3,"b":{"z":4},"d":6}]',true) q; + -- using the default use_json_as_text argument select * from json_populate_recordset(null::jpop,'[{"a":"blurfl","x":43.2},{"b":3,"c":"2012-01-20 10:42:53"}]') q; @@ -447,3 +450,6 @@ select * from json_to_record('{"a":1,"b":"foo","c":"bar"}',true) select * from json_to_recordset('[{"a":1,"b":"foo","d":false},{"a":2,"b":"bar","c":true}]',false) as x(a int, b text, c boolean); + +select * from json_to_recordset('[{"a":1,"b":{"d":"foo"},"c":true},{"a":2,"c":false,"b":{"d":"bar"}}]', true) + as x(a int, b json, c boolean); -- cgit v1.2.3