Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YozYf-0001DU-EL for pgsql-bugs@arkaria.postgresql.org; Sun, 03 May 2015 19:28:09 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YozYe-0002pc-Oi for pgsql-bugs@arkaria.postgresql.org; Sun, 03 May 2015 19:28:08 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YozYe-0002pW-1E for pgsql-bugs@postgresql.org; Sun, 03 May 2015 19:28:08 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1YozYb-0002S8-Tv for pgsql-bugs@postgresql.org; Sun, 03 May 2015 19:28:06 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t43JS3Pc023668; Sun, 3 May 2015 15:28:03 -0400 From: Tom Lane To: Kevin Grittner cc: Robert Haas , Jeff Davis , Postgres-Bugs Subject: Re: Failure to coerce unknown type to specific type In-reply-to: <1850649255.594603.1430679374313.JavaMail.yahoo@mail.yahoo.com> References: <16778.1430673231@sss.pgh.pa.us> <1850649255.594603.1430679374313.JavaMail.yahoo@mail.yahoo.com> Comments: In-reply-to Kevin Grittner message dated "Sun, 03 May 2015 18:56:14 -0000" Date: Sun, 03 May 2015 15:28:03 -0400 Message-ID: <23667.1430681283@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Kevin Grittner writes: > Tom Lane wrote: >> I don't have a lot of sympathy for that one. coalesce(null, null) >> isn't legal at all per SQL spec > I don't get that from my reading of the SQL spec. A COALESCE > clause is (and always has been) considered a short form of the CASE > clause (not to be mistaken for a function, for example). The spec > section 6.11 1) c) very explicitly requires > COALESCE(NULL, NULL) > be the exact equivalent of > CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END Sure, and that isn't legal per spec either (see below). > Yet in PostgreSQL the long form of the CASE clause returns the same > thing as a bare NULL, while the short form (COALESCE) gives an > error. Hm? I get the same thing for either variant: regression=# create table x (d date); CREATE TABLE regression=# insert into x values (coalesce(null, null)); ERROR: column "d" is of type date but expression is of type text LINE 1: insert into x values (coalesce(null, null)); ^ HINT: You will need to rewrite or cast the expression. regression=# insert into x values ( CASE WHEN NULL IS NOT NULL THEN NULL ELSE NULL END ); ERROR: column "d" is of type date but expression is of type text LINE 1: insert into x values ( CASE WHEN NULL IS NOT NULL THEN NULL ... ^ HINT: You will need to rewrite or cast the expression. The reason these things aren't legal per spec is that the spec says that a bare NULL keyword is a a/k/a , and those are only valid in situations where a type can be inferred from the *immediate* context. For example, insert into x values (null); is legal because the source of an INSERT can be a which is a VALUES clause that can contain a . On the other hand, result subexpressions of a CASE are just s, and you will not find any production that allows a bare NULL literal to be a . So far as I can find in SQL:2008, the only contexts where is syntactically legal are (1) INSERT, MERGE, and UPDATE source expressions, (2) CAST source expressions, and (3) table-column DEFAULT expressions, all of which have a well-defined target type available from the immediately surrounding semantic context. In short, the text of the spec only allows a bare NULL literal as the immediate argument of one of those constructs. Allowing it in any other context is an extension. The spec doesn't have a notion of unknown-type literal in the same way we do, but we've modeled our handling of those as being like bare NULL literals for type resolution purposes. Our choice has been to resolve as text in situations where there is no other info available from immediate context. I agree with the spec that it would be a bad idea to allow "action at a distance" in the sense of allowing such info to propagate through multiple levels of semantic context. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs