Unix Technical Forum

BUG #2490: '||' and type casting for user defined types

This is a discussion on BUG #2490: '||' and type casting for user defined types within the pgsql Bugs forums, part of the PostgreSQL category; --> The following bug has been logged online: Bug reference: 2490 Logged by: Nikolay Samokhvalov Email address: pg-bugs@samokhvalov.com PostgreSQL version: ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Bugs

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 10:04 AM
Nikolay Samokhvalov
 
Posts: n/a
Default BUG #2490: '||' and type casting for user defined types


The following bug has been logged online:

Bug reference: 2490
Logged by: Nikolay Samokhvalov
Email address: pg-bugs@samokhvalov.com
PostgreSQL version: CVS
Operating system: fedora core 5
Description: '||' and type casting for user defined types
Details:

Assume that we are creating foolish type 'aaa', which works like
varchar(3),
or is simply 'string, which length is not more than 3'.

In/out functions for this type:

Datum
aaa_in(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
int len = strlen(s);
VarChar *result;

if (len > 3)
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("too much chars: length=\"%d\"",
len)));

result = (VarChar *) palloc(len + VARHDRSZ);
VARATT_SIZEP(result) = len + VARHDRSZ;
memcpy(VARDATA(result), s, len);

PG_RETURN_VARCHAR_P(result);
}

Datum
aaa_out(PG_FUNCTION_ARGS)
{
VarChar *s = PG_GETARG_VARCHAR_P(0);
char *result;
int32 len;

/* copy and add null term */
len = VARSIZE(s) - VARHDRSZ;
result = palloc(len + 1);
memcpy(result, VARDATA(s), len);
result[len] = '\0';

PG_RETURN_CSTRING(result);
}


SQL code:

CREATE FUNCTION aaa_in(cstring)
RETURNS aaa
AS 'MODULE_PATHNAME'
LANGUAGE C RETURNS NULL ON NULL INPUT;

CREATE FUNCTION aaa_out(aaa)
RETURNS cstring
AS 'MODULE_PATHNAME'
LANGUAGE C RETURNS NULL ON NULL INPUT;

CREATE TYPE aaa (
INTERNALLENGTH = -1,
INPUT = aaa_in,
OUTPUT = aaa_out,
STORAGE = extended
);

CREATE CAST (aaa AS text) WITHOUT FUNCTION
AS IMPLICIT;
CREATE CAST (text AS aaa) WITHOUT FUNCTION
AS IMPLICIT;

Well, let's do some tests. After applying sql code in the database 'trash':

trash=# select 'asd'::aaa;
aaa
-----
asd
(1 row)

trash=# select 'asdf'::aaa;
ERROR: too much chars: length="4"
trash=# select ('as' || 'df')::aaa;
aaa
------
asdf
(1 row)

In the last case, function aaa_in() wasn't invoked at all and we obtained
the sting of type aaa with length > 3...

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 10:04 AM
Tom Lane
 
Posts: n/a
Default Re: BUG #2490: '||' and type casting for user defined types

"Nikolay Samokhvalov" <pg-bugs@samokhvalov.com> writes:
> CREATE CAST (aaa AS text) WITHOUT FUNCTION
> AS IMPLICIT;
> CREATE CAST (text AS aaa) WITHOUT FUNCTION
> AS IMPLICIT;


> trash=# select ('as' || 'df')::aaa;
> aaa
> ------
> asdf
> (1 row)


> In the last case, function aaa_in() wasn't invoked at all and we obtained
> the sting of type aaa with length > 3...


Well, sure. You declared the text to aaa cast as WITHOUT FUNCTION; that
says the system can relabel any legal text value as an aaa. If you want
to enforce a length limit on the cast result, you need to write a cast
function that does it.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:40 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com