dbTalk Databases Forums  

sql function returning composite type

comp.databases.postgresql.bugs comp.databases.postgresql.bugs


Discuss sql function returning composite type in the comp.databases.postgresql.bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ivan
 
Posts: n/a

Default sql function returning composite type - 09-21-2004 , 08:33 AM






Hello,

Suppose we have sql function which returns composite type.
When such function's last select statement doesn't return any row
error occurs -
ERROR: function returning row cannot return null value.
But if we use similar function that returns set of same type
error not occurs.

Example:

--------------------------------------------------------
CREATE TYPE "test_type" AS (
"id" integer,
"name" character varying(64),
"description" text
);

CREATE FUNCTION "test"(character varying) RETURNS "test_type"
AS '
select "id", "name", "description"
from "test"
where "name" = $1;
'
LANGUAGE sql SECURITY DEFINER;

CREATE FUNCTION "test2"(character varying) RETURNS SETOF "test_type"
AS '
select "id", "name", "description"
from "test"
where "name" = $1;
'
LANGUAGE sql SECURITY DEFINER;


CREATE TABLE "test" (
"id" serial NOT NULL,
"name" character varying(64) NOT NULL,
"description" text,
"update_time" timestamp without time zone DEFAULT now() NOT NULL
);

INSERT INTO "test" ("id", "name", "description", "update_time") VALUES (1, 'first', 'first row', '2004-09-21 15:32:41.171');
INSERT INTO "test" ("id", "name", "description", "update_time") VALUES (2, 'second', 'second row', '2004-09-21 15:32:54.64');
INSERT INTO "test" ("id", "name", "description", "update_time") VALUES (3, 'third', 'third row', '2004-09-21 15:33:08.406');

ALTER TABLE ONLY "test"
ADD CONSTRAINT "pk_test_id" PRIMARY KEY ("id");

ALTER TABLE ONLY "test"
ADD CONSTRAINT "unq_test_name" UNIQUE ("name");

--------------------------------------------------------
select * from "test"('second')
will return one row with data
but
select * from "test"('secon')
will raise an ERROR
and
select * from "test2"('secon')
will return empty set.

I think that is more conveniently that when no data is fetched
such function returns instance of composite type with nulls.

In case of function returning record type we also "know" schema of
the last select.

Of course it is possible to use plpgsql function and select into
but sql functions is smaller and don't require handler.

--
Best regards,
Ivan mailto:Ivan-Sun1 (AT) mail (DOT) ru


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend


Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: sql function returning composite type - 09-21-2004 , 09:17 AM






Ivan <Ivan-Sun1 (AT) mail (DOT) ru> writes:
Quote:
Suppose we have sql function which returns composite type.
When such function's last select statement doesn't return any row
error occurs -
ERROR: function returning row cannot return null value.
Works for me (tested in 7.4.5 and CVS tip).

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: sql function returning composite type - 09-21-2004 , 11:44 AM



Ivan <Ivan-Sun1 (AT) mail (DOT) ru> writes:
Quote:
Error occurs when do
select * from "test"('secon')
Ah. I was trying "select test('foo')" which does work. Will look at
it.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
  #4  
Old   
Tom Lane
 
Posts: n/a

Default Re: sql function returning composite type - 09-22-2004 , 12:42 PM



Ivan <Ivan-Sun1 (AT) mail (DOT) ru> writes:
Quote:
Still have the same error - ERROR: function returning row cannot return null value
I've applied a patch to fix this for 8.0.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.