Re: Returning NULL to a set returning C type function

Lists: pgsql-general
From: Bborie Park <bkpark(at)ucdavis(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Returning NULL to a set returning C type function
Date: 2011-05-11 18:01:33
Message-ID: 4DCACEFD.1050401@ucdavis.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I have a C type function that returns a set of a type. The problem I
have is that the underlying function may return NULL. When the
underlying function returns NULL, I get the error message:

ERROR: function returning set of rows cannot return null value

I'm wondering what is the proper way to correct this problem? Or is
there are correct workaround?

So, for my sample code:

CREATE TYPE histogram AS (
min double precision,
max double precision,
count integer,
proportion double precision
);

CREATE OR REPLACE FUNCTION _st_histogram(rast raster, nband int,
hasnodata boolean, sample_percent double precision, bins int, width
double precision[], right boolean)
RETURNS SETOF histogram
AS '$libdir/rtpostgis-2.0','RASTER_histogram'
LANGUAGE 'C' IMMUTABLE STRICT;

PG_FUNCTION_INFO_V1(RASTER_histogram);
Datum RASTER_histogram(PG_FUNCTION_ARGS)
{
-- if any of the args are incorrect, return NULL
PG_RETURN_NULL();
}

Thanks,
Bborie

--
Bborie Park
Programmer
Center for Vectorborne Diseases
UC Davis
530-752-8380
bkpark(at)ucdavis(dot)edu


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bborie Park <bkpark(at)ucdavis(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Returning NULL to a set returning C type function
Date: 2011-05-11 18:23:39
Message-ID: 3247.1305138219@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Bborie Park <bkpark(at)ucdavis(dot)edu> writes:
> I have a C type function that returns a set of a type. The problem I
> have is that the underlying function may return NULL. When the
> underlying function returns NULL, I get the error message:

> ERROR: function returning set of rows cannot return null value

> I'm wondering what is the proper way to correct this problem?

Don't do that ;-). You could choose either to not return any row
at all when this happens, or to construct an all-nulls row to return.
ExecMakeTableFunctionResult doesn't want to guess which behavior is
appropriate for your use-case, so it just complains.

regards, tom lane


From: Bborie Park <bkpark(at)ucdavis(dot)edu>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Returning NULL to a set returning C type function
Date: 2011-05-11 19:09:21
Message-ID: 4DCADEE1.6090901@ucdavis.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> Don't do that ;-). You could choose either to not return any row
> at all when this happens, or to construct an all-nulls row to return.
> ExecMakeTableFunctionResult doesn't want to guess which behavior is
> appropriate for your use-case, so it just complains.
>
> regards, tom lane
>

Thanks Tom. I'll go the "no return any row" route.

--
Bborie Park
Programmer
Center for Vectorborne Diseases
UC Davis
530-752-8380
bkpark(at)ucdavis(dot)edu