On Mon, Nov 22, 2004 at 03:34:17PM -0500, Tom Lane wrote:
>
> Thanks for the examples. I extended this into a simple regression test
> which I've added to CVS. If anyone would like to add some test cases
> for more interesting stuff (triggers and error handling come to mind),
> step right up ...
How far do you want to go with checking return types? Some of the
following test cases are approaching "garbage in, garbage out"
territory and I don't know how much effort you want to put into
protecting programmers from themselves. Some of the cases already
raise errors; for consistency I'm inclined to think they all should.
Tests run against plperl.c 1.63, SPI.xs 1.11.
Test Case 1: scalar expected, non-scalar returned
CREATE FUNCTION test1() RETURNS TEXT AS $$
return ["test"];
$$ LANGUAGE plperl;
SELECT test1();
test1
------------------
ARRAY(0x8427a58)
(1 row)
Test Case 2: list of scalars expected, scalar returned
CREATE FUNCTION test2() RETURNS SETOF TEXT AS $$
return "test";
$$ LANGUAGE plperl;
SELECT * FROM test2();
ERROR: plperl: set-returning function must return reference to array
Test Case 3: list of scalars expected, list of non-scalars returned
CREATE FUNCTION test3() RETURNS SETOF TEXT AS $$
return [["test 1"], ["test 2"]];
$$ LANGUAGE plperl;
SELECT * FROM test3();
test3
------------------
ARRAY(0x8424e10)
ARRAY(0x8424fcc)
(2 rows)
Test Case 4: hash expected, non-hash returned
CREATE TYPE footype AS (txtval TEXT, intval INTEGER);
CREATE FUNCTION test4() RETURNS footype AS $$
return "test";
$$ LANGUAGE plperl;
SELECT test4();
ERROR: plperl: composite-returning function must return a reference to hash
Test Case 5: hash expected, keys not scalars
CREATE FUNCTION test5() RETURNS footype AS $$
return {["txtval"] => "test", ["intval"] => 42};
$$ LANGUAGE plperl;
SELECT test5();
ERROR: plperl: invalid attribute "ARRAY(0x8431950)" in hash
Test Case 6: hash expected, value not scalar
CREATE FUNCTION test6() RETURNS footype AS $$
return {txtval => ["test"], intval => 42};
$$ LANGUAGE plperl;
SELECT * FROM test6();
txtval | intval
------------------+--------
ARRAY(0x8433f9c) | 42
(1 row)
Test Case 7: list of hashes expected, list of non-hashes returned
CREATE FUNCTION test7() RETURNS SETOF footype AS $$
return ["test 1", 42];
$$ LANGUAGE plperl;
SELECT * FROM test7();
ERROR: plperl: element of result array is not a reference to hash
Test Case 8: list of hashes expected, hash key not scalar
CREATE FUNCTION test8() RETURNS SETOF footype AS $$
return [{["txtval"] => "test 1", ["intval"] => 1}];
$$ LANGUAGE plperl;
SELECT * FROM test8();
ERROR: plperl: invalid attribute "ARRAY(0x8437b48)" in hash
Test Case 9: list of hashes expected, hash value not scalar
CREATE FUNCTION test9() RETURNS SETOF footype AS $$
return [{txtval => ["test 1"], intval => 42}];
$$ LANGUAGE plperl;
SELECT * FROM test9();
txtval | intval
------------------+--------
ARRAY(0x8438d74) | 42
(1 row)
--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster