From ed9e8672f8b68f4a83624971273612e576ae07ac Mon Sep 17 00:00:00 2001 From: Jordan Lewis Date: Tue, 28 Sep 2021 11:20:54 -0300 Subject: [PATCH] builtins: test anon table type with json_populate And fix a small missing error message. Release note: None --- .../testdata/logic_test/json_builtins | 49 +++++++++++++++++++ pkg/sql/sem/builtins/generator_builtins.go | 3 ++ 2 files changed, 52 insertions(+) diff --git a/pkg/sql/logictest/testdata/logic_test/json_builtins b/pkg/sql/logictest/testdata/logic_test/json_builtins index af96e9962926..1125c9fb8bfc 100644 --- a/pkg/sql/logictest/testdata/logic_test/json_builtins +++ b/pkg/sql/logictest/testdata/logic_test/json_builtins @@ -1262,6 +1262,31 @@ SELECT (json_populate_record(((1.01, 2, 3) AS d, c, a), '{"a": 3, "c": 10, "d": d 11.001 +statement ok +CREATE TABLE testtab ( + i int, + ia int[], + t text, + ta text[], + ts timestamp, + j jsonb +) + +query ITTTTT +SELECT * FROM json_populate_record(NULL::testtab, '{"i": 3, "ia": [1,2,3], "t": "foo", "ta": ["a", "b"], "ts": "2017-01-01 00:00", "j": {"a": "b", "c": 3, "d": [1,false,true,null,{"1":"2"}]}}'::JSON) +---- +3 {1,2,3} foo {a,b} 2017-01-01 00:00:00 +0000 +0000 {"a": "b", "c": 3, "d": [1, false, true, null, {"1": "2"}]} + +query T +SELECT json_populate_record(NULL::testtab, '{"i": 3, "ia": [1,2,3], "t": "foo", "ta": ["a", "b"], "ts": "2017-01-01 00:00", "j": {"a": "b", "c": 3, "d": [1,false,true,null,{"1":"2"}]}}'::JSON) +---- +(3,"{1,2,3}",foo,"{a,b}","2017-01-01 00:00:00","{""a"": ""b"", ""c"": 3, ""d"": [1, false, true, null, {""1"": ""2""}]}") + +query ITTTTT +SELECT * FROM json_populate_record(NULL::testtab, NULL) +---- +NULL NULL NULL NULL NULL NULL + query error could not parse \"foo\" as type int SELECT json_populate_record(((3,) AS a), '{"a": "foo"}') @@ -1312,3 +1337,27 @@ SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, query FIT SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, a), '[]') ---- + +query error argument of json_populate_recordset must be an array +SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, a), '{"foo": "bar"}') + +query error argument of json_populate_recordset must be an array +SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, a), 'true') + +query error argument of json_populate_recordset must be an array +SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, a), '0') + +query error argument of json_populate_recordset must be an array +SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, a), 'null') + +query error argument of json_populate_recordset must be an array of objects +SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, a), '[null]') + +query error argument of json_populate_recordset must be an array of objects +SELECT * FROM json_populate_recordset(((NULL::NUMERIC, 2::INT, 3::TEXT) AS d, c, a), '[{"foo":"bar"}, 3]') + +query ITTTTT +SELECT * FROM json_populate_recordset(NULL::testtab, '[{"i": 3, "ia": [1,2,3], "t": "foo", "ta": ["a", "b"], "ts": "2017-01-01 00:00", "j": {"a": "b", "c": 3, "d": [1,false,true,null,{"1":"2"}]}}, {}]'::JSON) +---- +3 {1,2,3} foo {a,b} 2017-01-01 00:00:00 +0000 +0000 {"a": "b", "c": 3, "d": [1, false, true, null, {"1": "2"}]} +NULL NULL NULL NULL NULL NULL diff --git a/pkg/sql/sem/builtins/generator_builtins.go b/pkg/sql/sem/builtins/generator_builtins.go index 40a240ffe12a..3add6c917770 100644 --- a/pkg/sql/sem/builtins/generator_builtins.go +++ b/pkg/sql/sem/builtins/generator_builtins.go @@ -1421,6 +1421,9 @@ func (j *jsonPopulateRecordSetGenerator) Values() (tree.Datums, error) { if err != nil { return nil, err } + if obj.Type() != json.ObjectJSONType { + return nil, pgerror.Newf(pgcode.InvalidParameterValue, "argument of json_populate_recordset must be an array of objects") + } output := tree.NewDTupleWithLen(j.input.ResolvedType(), j.input.D.Len()) for i := range j.input.D { output.D[i] = j.input.D[i]