Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Yp0EI-0003i7-PH for pgsql-bugs@arkaria.postgresql.org; Sun, 03 May 2015 20:11:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Yp0EI-0000Oy-AG for pgsql-bugs@arkaria.postgresql.org; Sun, 03 May 2015 20:11:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Yp0EH-0000Or-91 for pgsql-bugs@postgresql.org; Sun, 03 May 2015 20:11:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1Yp0EE-0003cc-JY for pgsql-bugs@postgresql.org; Sun, 03 May 2015 20:11:08 +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 t43KB30F024816; Sun, 3 May 2015 16:11: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: <23667.1430681283@sss.pgh.pa.us> References: <16778.1430673231@sss.pgh.pa.us> <1850649255.594603.1430679374313.JavaMail.yahoo@mail.yahoo.com> <23667.1430681283@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sun, 03 May 2015 15:28:03 -0400" Date: Sun, 03 May 2015 16:11:03 -0400 Message-ID: <24815.1430683863@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 I wrote: > 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. On doing a more thorough search, I see that I missed one reference: the result expression(s) of a CASE construct are defined as ::= | NULL ::= which might seem to put the lie to my thesis, except that in the Syntax Rules we read At least one in a shall specify a . That means this is legal: CASE WHEN ... THEN 42 ELSE NULL END; but this isn't: CASE WHEN ... THEN NULL ELSE NULL END; So the core point stands. The spec doesn't allow a bare NULL literal anywhere that its type can't be determined from the most closely nested semantic context. It's a bit weird that they encode this as a syntax rule rather than a semantic rule, but that's what they did. 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