From: Tom Lane <tgl@sss.pgh.pa.us>
To: George Pavlov <gpavlov@mynewplace.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: DISTINCT with NULLs and INT fields
Date: Wed, 28 Dec 2005 15:19:29 -0500
Message-ID: <21528.1135801169@sss.pgh.pa.us> (raw)
In-Reply-To: <CCB89282FCE1024EA3DCE687A96A5164039EC532@ehost010-6.exch010.intermedia.net>
References: <CCB89282FCE1024EA3DCE687A96A5164039EC532@ehost010-6.exch010.intermedia.net>
"George Pavlov" <gpavlov@mynewplace.com> writes:
> What does The SQL Standard say about this one?
> insert into foo (a, b) select distinct null, null from bar;
> -- ERROR: column "b" is of type integer but expression is of type text
According to the SQL spec that query is illegal on its face --- the spec
does not allow a bare NULL keyword except in contexts where the datatype
is immediately evident. You'd have had to write
SELECT DISTINCT CAST(NULL AS varchar), CAST(NULL AS int)
to be fully spec-conformant.
Postgres handles NULL the same way as untyped literals ('foo'), so we
tend to prefer resolving to type text when things are ambiguous. In
this case the DISTINCT operation forces a type choice (if you don't know
what type the data is, you can hardly decide whether two values are equal
or not) and so by the time the INSERT sees it, it looks like a SELECT
of two text columns.
> Looks like SQL Server makes the NULLs be INTs, but is probably
> better at doing the implicit conversion from INT.
"Better" is in the eye of the beholder. It surprises me not at all that
Microsoft would be lax about implicit coercions, but that doesn't make
it a good idea to coerce anything to anything else without complaint.
You might as well not have a type system at all, if you're going to
destroy its ability to detect mistakes that way.
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, gpavlov@mynewplace.com
Subject: Re: DISTINCT with NULLs and INT fields
In-Reply-To: <21528.1135801169@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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