Re: When is an explicit cast necessary?

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Alan Millington <admillington(at)yahoo(dot)co(dot)uk>
Cc: Greg Smith <greg(at)2ndquadrant(dot)com>, Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: When is an explicit cast necessary?
Date: 2010-04-10 10:51:41
Message-ID: 8A9CE1BF-F854-4AA0-9AA6-D232A0034D1E@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9 Apr 2010, at 16:57, Alan Millington wrote:

> However, every language that I have ever used will implicitly convert an integer 1 to a smallint (or short) 1 when required to do so. How can such a cast be called "surprising behaviour", or produce "misleading results", to quote the first article?

No, they probably don't cast integer values down to smallint. What they do is cast the smallint up to integer, as that's a safe cast. After all, numbers that fit in an int may not fit in a smallint (try "select 75000::smallint;" for example) and you'd lose data casting it down, but it's fine the other way around.

Since your function has smallint as one of its parameter types the database can't cast the smallint up to an int like it would normally do in such cases as the function doesn't accept integer values for that parameter. PG can't do much but throw an error.

Alban Hertroys

--
Screwing up is an excellent way to attach something to the ceiling.

!DSPAM:737,4bc0584910411899921361!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alan Millington 2010-04-10 17:15:18 Re: When is an explicit cast necessary?
Previous Message Allan Kamau 2010-04-10 09:54:46 @> and <@ (contains and is contained by) operations on large arrays