Re: Weird NULL behavior

Lists: pgsql-sql
From: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>
To: PostgreSQL Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Weird NULL behavior
Date: 2002-11-07 11:41:03
Message-ID: 20021107114103.43007.qmail@web80306.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Hi:

Has anyone encountered this before?
SELECT CAST ( (NULL*NULL) AS NUMERIC(2,0));


returns the following error message:
Cannot cast type '"char"' to '"numeric"'

But the following sql statements returns NULL:
select NULL:
select NULL * NULL;
select cast ( NULL as NUMERIC(2,0));

Thank you in advance,
ludwig.

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com


From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>
Cc: PostgreSQL Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Weird NULL behavior
Date: 2002-11-07 16:41:26
Message-ID: 20021107083423.A97075-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Thu, 7 Nov 2002, Ludwig Lim wrote:

> Hi:
>
> Has anyone encountered this before?
> SELECT CAST ( (NULL*NULL) AS NUMERIC(2,0));
>
>
> returns the following error message:
> Cannot cast type '"char"' to '"numeric"'

It seems to me that it's trying to decide on a type
for the expression NULL * NULL. It's a NULL, but a
NULL of what type? I think the spec gets around
this by disallowing such structures AFAIK (NULL
can be used in like row value constructors, case
and cast). I think the "sql" way of doing the
above would be
select cast(cast(NULL as NUMERIC(2,0))*cast(NULL as NUMERIC(2,0))
as NUMERIC(2,0));


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
Cc: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>, PostgreSQL Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Weird NULL behavior
Date: 2002-11-07 19:09:24
Message-ID: 28164.1036696164@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com> writes:
> On Thu, 7 Nov 2002, Ludwig Lim wrote:
>> SELECT CAST ( (NULL*NULL) AS NUMERIC(2,0));
>> Cannot cast type '"char"' to '"numeric"'

> It seems to me that it's trying to decide on a type
> for the expression NULL * NULL. It's a NULL, but a
> NULL of what type?

Yeah, and it's picking "char" (the single-byte datatype), because
(a) the NULLs are initially regarded as type UNKNOWN, and (b) if we
don't have any other way to make a decision we try assuming that
UNKNOWNs are of string category, and (c) the only datatype in string
category that has a "*" operator is "char".

I am kind of inclined to remove the arithmetic operators on "char"
(+,-,*,/) in 7.4 --- they don't seem to have any real-world uses,
and as this example illustrates, they are perfectly positioned to
capture cases that probably ought to be errors.

But as you say, the proper solution for Ludwig's problem is to cast the
NULLs themselves to numeric, not the result of the multiplication.

regards, tom lane


From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>, PostgreSQL Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Weird NULL behavior
Date: 2002-11-07 19:35:56
Message-ID: 20021107113216.S99207-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

On Thu, 7 Nov 2002, Tom Lane wrote:

> Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com> writes:
> > On Thu, 7 Nov 2002, Ludwig Lim wrote:
> >> SELECT CAST ( (NULL*NULL) AS NUMERIC(2,0));
> >> Cannot cast type '"char"' to '"numeric"'
>
> > It seems to me that it's trying to decide on a type
> > for the expression NULL * NULL. It's a NULL, but a
> > NULL of what type?
>
> Yeah, and it's picking "char" (the single-byte datatype), because
> (a) the NULLs are initially regarded as type UNKNOWN, and (b) if we
> don't have any other way to make a decision we try assuming that
> UNKNOWNs are of string category, and (c) the only datatype in string
> category that has a "*" operator is "char".
>
> I am kind of inclined to remove the arithmetic operators on "char"
> (+,-,*,/) in 7.4 --- they don't seem to have any real-world uses,
> and as this example illustrates, they are perfectly positioned to
> capture cases that probably ought to be errors.

That seems to make sense. I assume that they were there so that someone
could treat it as a 1 byte integer?


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
Cc: Ludwig Lim <lud_nowhere_man(at)yahoo(dot)com>, PostgreSQL Mailing List <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Weird NULL behavior
Date: 2002-11-07 20:00:51
Message-ID: 28472.1036699251@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com> writes:
> On Thu, 7 Nov 2002, Tom Lane wrote:
>> I am kind of inclined to remove the arithmetic operators on "char"
>> (+,-,*,/) in 7.4 --- they don't seem to have any real-world uses,
>> and as this example illustrates, they are perfectly positioned to
>> capture cases that probably ought to be errors.

> That seems to make sense. I assume that they were there so that someone
> could treat it as a 1 byte integer?

Presumably ... but defining a numeric type named "int1" would be a lot
more sensible than overloading "char" for the purpose.

regards, tom lane