X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id D96FA9DC8A5 for ; Wed, 28 Dec 2005 16:19:31 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 34003-06 for ; Wed, 28 Dec 2005 16:19:32 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 9EB3F9DC845 for ; Wed, 28 Dec 2005 16:19:29 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id jBSKJTLg021529; Wed, 28 Dec 2005 15:19:29 -0500 (EST) To: "George Pavlov" cc: pgsql-sql@postgresql.org Subject: Re: DISTINCT with NULLs and INT fields In-reply-to: References: Comments: In-reply-to "George Pavlov" message dated "Wed, 28 Dec 2005 11:57:38 -0800" Date: Wed, 28 Dec 2005 15:19:29 -0500 Message-ID: <21528.1135801169@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.051 required=5 tests=[AWL=0.051] X-Spam-Score: 0.051 X-Spam-Level: X-Archive-Number: 200512/225 X-Sequence-Number: 23767 "George Pavlov" 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