pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Casts in 7.0 vs 6.5 (was Re: [SQL] 7.0beta bug (or feature)?)
2+ messages / 2 participants
[nested] [flat]

* Casts in 7.0 vs 6.5 (was Re: [SQL] 7.0beta bug (or feature)?)
@ 2000-03-07 23:59  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Tom Lane @ 2000-03-07 23:59 UTC (permalink / raw)
  To: Kyle Bateman <kyle@actarg.com>; +Cc: pgsql-sql; pgsql-hackers@postgresql.org

Kyle Bateman <kyle@actarg.com> writes:
> This function would load OK in 6.5 but doesn't work in 7.0beta1:

> create function date_week(date) returns text as '
>         select date_week($1::text);
>         ' LANGUAGE 'sql';

> ERROR:  No such function 'date_week' with the specified attributes

> ... apparently, the parser does not recognize
> the fact that a cast is present and tries to look for an existing
> function date_week(date) instead of date_week(text).

Yup, 7.0beta1 is missing a couple of lines of code needed to handle
casts applied to function parameters.  Thanks for catching that.
I have patched current CVS sources; tonight's snapshot should have
the fix.

> It looks like either there is a problem in the parser with casting, or
> there is a new way of doing things I should adapt to.

7.0 currently is a little snippier about casts than prior releases were;
it wants the cast to equate directly to an available conversion.
So what I'm getting from your example now is

regression=# create function date_week(date) returns text as '
regression'#         select date_week($1::text);
regression'#         ' LANGUAGE 'sql';
ERROR:  Cannot cast type 'date' to 'text'

because there isn't a text(date) function.  But there is a
text(timestamp) function, and a timestamp(date) function,
so this works:

regression=# create function date_week(date) returns text as '
regression'#         select date_week($1::timestamp::text);
regression'#         ' LANGUAGE 'sql';
CREATE

6.5 would do the intermediate conversion to timestamp (then called
datetime) silently, but 7.0 won't.  Note that both versions will
happily take
		select date_week(text($1));
and interpolate the intermediate conversion step as part of resolving
the overloaded function name text().  7.0 is only picky about
conversions written as casts.

I am not sure whether this should be regarded as a bug or a feature.
On the one hand you could argue that ambiguous casts are a bad thing,
but on the other hand, if text(foo) works, why shouldn't foo::text work?

One thing to realize while considering whether to change this is that if
we generalize the behavior of casts, we may also affect the behavior of
implicit casts, such as the one applied to convert supplied data in an
INSERT or UPDATE to the target column type.  This could result in loss
of error detection capability.  Currently, both 6.5 and 7.0 do this:

regression=# create table foo(f1 text);
CREATE
regression=# insert into foo values('now'::date);
ERROR:  Attribute 'f1' is of type 'text' but expression is of type 'date'
        You will need to rewrite or cast the expression

but if we allow datevalue::text to work, then (barring still more
pushups in the code) the above will be accepted.  Should it be?

Comments anyone?

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Casts in 7.0 vs 6.5 (was Re: [SQL] 7.0beta bug (or feature)?)
@ 2000-03-08 16:22  Kyle Bateman <kyle@actarg.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Kyle Bateman @ 2000-03-08 16:22 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-sql; pgsql-hackers@postgreSQL.org

Tom Lane wrote:

>
> I am not sure whether this should be regarded as a bug or a feature.
> On the one hand you could argue that ambiguous casts are a bad thing,
> but on the other hand, if text(foo) works, why shouldn't foo::text work?
>
> One thing to realize while considering whether to change this is that if
> we generalize the behavior of casts, we may also affect the behavior of
> implicit casts, such as the one applied to convert supplied data in an
> INSERT or UPDATE to the target column type.  This could result in loss
> of error detection capability.  Currently, both 6.5 and 7.0 do this:
>
> regression=# create table foo(f1 text);
> CREATE
> regression=# insert into foo values('now'::date);
> ERROR:  Attribute 'f1' is of type 'text' but expression is of type 'date'
>         You will need to rewrite or cast the expression
>
> but if we allow datevalue::text to work, then (barring still more
> pushups in the code) the above will be accepted.  Should it be?
>
> Comments anyone?
>

What if you could to a "set AutoCasting=yes" just as you might set the
datestyle variable.  Then the DBA could decide whether type mismatches
should be quitely translated or reported?

Attachments:

kyle.vcf (text/x-vcard, 290B) download begin:vcard n:Bateman;Kyle tel;fax:801-377-8096 tel;work:801-377-8033x101 x-mozilla-html:FALSE url:www.actiontarget.com org:Action Target Inc adr:;;PO Box 636;Provo;UT;84603;US version:2.1 email;internet:kyle@actiontarget.com title:President x-mozilla-cpt:;-15520 fn:Kyle Bateman end:vcard
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2000-03-08 16:22 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-03-07 23:59 Casts in 7.0 vs 6.5 (was Re: [SQL] 7.0beta bug (or feature)?) Tom Lane <tgl@sss.pgh.pa.us>
2000-03-08 16:22 ` Kyle Bateman <kyle@actarg.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox