0

I have a function that operates on int[]. I want to have the ability to check if the array is empty and in that case to stop since there is nothing to do.

this is my funcion:

CREATE OR REPLACE FUNCTION func2(c integer[])
  RETURNS SETOF func_type AS
$BODY$
result=plpy.execute("SELECT * FROM func1(ARRAY%s)"%c)
return result;
$BODY$
  LANGUAGE plpythonu VOLATILE

func_type defined as:

CREATE TYPE func_type AS
   (x integer,
    y numeric,
    z integer,
    zz integer);

when I added a check:

$BODY$
if not c:
    return  

I got an error:

ERROR:  returned object cannot be iterated
DETAIL:  PL/Python set-returning functions must return an iterable object.

I understand that I have to return func_type and func_type was not built since there was no plpy.execute but still how can I make it return at this point? Also, assume func3 called func2. How would func3 can check that there are 0 rows returned?

4

1 回答 1

2

If you declare the function strict then it will not be executed and will return null whenever any of its arguments is null

http://www.postgresql.org/docs/current/static/sql-createfunction.html

In case the passed array is empty and you want to return an empty set then return an empty array:

create or replace function func2(c integer[])
  returns setof func_type as
$body$

if not c:
    return []

$body$ language plpythonu;

select * from func2(array[]::int[]);
 x | y | z | zz 
---+---+---+----
(0 rows)
于 2015-06-25T15:52:11.277 回答