Skip to content

Commit

Permalink
builtins: test anon table type with json_populate
Browse files Browse the repository at this point in the history
And fix a small missing error message.

Release note: None
  • Loading branch information
jordanlewis committed Sep 29, 2021
1 parent dfdb72f commit ed9e867
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/json_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -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"}')

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions pkg/sql/sem/builtins/generator_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit ed9e867

Please sign in to comment.