pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Kevin Grittner <kgrittn@ymail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Jeff Davis <pgsql@j-davis.com>
Cc: Postgres-Bugs <pgsql-bugs@postgresql.org>
Subject: Re: Failure to coerce unknown type to specific type
Date: Sun, 03 May 2015 16:11:03 -0400
Message-ID: <24815.1430683863@sss.pgh.pa.us> (raw)
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>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

I wrote:
> The reason these things aren't legal per spec is that the spec says that
> a bare NULL keyword is a <contextually typed value specification> a/k/a
> <implicitly typed value specification>, 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
> <contextually typed table value constructor> which is a VALUES clause
> that can contain a <contextually typed value specification>.  On the
> other hand, result subexpressions of a CASE are just <value expression>s,
> and you will not find any production that allows a bare NULL literal
> to be a <value expression>.  So far as I can find in SQL:2008, the
> only contexts where <contextually typed anything> 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

	<result> ::= <result expression>
	             | NULL
	<result expression> ::= <value expression>

which might seem to put the lie to my thesis, except that in the
Syntax Rules we read

	At least one <result> in a <case specification> shall specify
	a <result expression>.

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



view thread (42+ messages)  latest in thread

Message-ID: <24815.1430683863@sss.pgh.pa.us>
Permalink:  ../24815.1430683863@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/24815.1430683863@sss.pgh.pa.us

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, kgrittn@ymail.com, robertmhaas@gmail.com, pgsql@j-davis.com
  Subject: Re: Failure to coerce unknown type to specific type
  In-Reply-To: <24815.1430683863@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> <24815.1430683863@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