aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-03-02 23:31:39 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2016-03-02 23:31:39 -0500
commit429d6846572fe43dc11496adfeaecadf23925e3f (patch)
tree6c5fbe751a1f68d5eac717e5b457a986ae6dbce8 /src/test
parentca778d01e7f0f216bf2a8b0f45952c4c3ef5321a (diff)
downloadpostgresql-429d6846572fe43dc11496adfeaecadf23925e3f.tar.gz
postgresql-429d6846572fe43dc11496adfeaecadf23925e3f.zip
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.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/json.out16
-rw-r--r--src/test/regress/expected/jsonb.out16
-rw-r--r--src/test/regress/sql/json.sql7
-rw-r--r--src/test/regress/sql/jsonb.sql8
4 files changed, 47 insertions, 0 deletions
diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out
index 502f9838897..efcdc4141e3 100644
--- a/src/test/regress/expected/json.out
+++ b/src/test/regress/expected/json.out
@@ -1599,6 +1599,22 @@ 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)
+
-- json_strip_nulls
select json_strip_nulls(null);
json_strip_nulls
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out
index 4789e4e57b9..416918dd9fb 100644
--- a/src/test/regress/expected/jsonb.out
+++ b/src/test/regress/expected/jsonb.out
@@ -2001,6 +2001,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
diff --git a/src/test/regress/sql/json.sql b/src/test/regress/sql/json.sql
index 39f1b70f4da..603288bd1a1 100644
--- a/src/test/regress/sql/json.sql
+++ b/src/test/regress/sql/json.sql
@@ -519,6 +519,13 @@ 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);
-- json_strip_nulls
diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql
index 4b244776095..c6716841344 100644
--- a/src/test/regress/sql/jsonb.sql
+++ b/src/test/regress/sql/jsonb.sql
@@ -508,6 +508,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"}';