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 <Kevin.Grittner@wicourts.gov>
Cc: Merlin Moncure <mmoncure@gmail.com>
Cc: alexk@commandprompt.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: the '::' cast doesn't work in the FROM clause
Date: Mon, 29 Aug 2011 10:47:39 -0400
Message-ID: <21023.1314629259@sss.pgh.pa.us> (raw)
In-Reply-To: <4E5B552502000025000407EB@gw.wicourts.gov>
References: <4E5B449602000025000407D9@gw.wicourts.gov>
	<CAHyXU0x-MgGEfa+P4+6fR=4q2k542P58QBQqn7cWcEqU3sdGTg@mail.gmail.com>
	<4E5B552502000025000407EB@gw.wicourts.gov>

"Kevin Grittner" <Kevin.Grittner@wicourts.gov> writes:
> Merlin Moncure <mmoncure@gmail.com> wrote:
>> yeah, that's the correct way, but why does this work?
>> select val from random() as val;
 
> If you look at the PostgreSQL reference docs for the SELECT
> statement, a from_item can be a SELECT statement in parentheses or a
> function call (among other things).  It cannot be an arbitrary
> expression containing operators (like ::).

Right.  We also accept things that look syntactically like function
calls, so as to avoid debates with newbies about whether, say,
COALESCE(...) is a function or not.  CAST() falls into that category,
while :: doesn't.

There is actually a practical reason for this policy beyond the question
of whether CAST is a function call or not: the structure name(...) has
a well-defined syntactic extent, so there are no issues of operator
precedence to worry about when it's embedded in a larger construct.
IIRC, we ran into exactly that problem with the CREATE INDEX syntax,
which is why an expression index column has to be parenthesized unless
it looks like a function call.

So IMO there is no syntax bug here.  There is a dump/reload bug though
:-( ... if you were to do

create view vv as select val from CAST(random() as integer) as val; 

you will find that the system prints it out with the :: syntax,
which won't work.

			regards, tom lane



view thread (13+ messages)  latest in thread

Message-ID: <21023.1314629259@sss.pgh.pa.us>
Permalink:  ../21023.1314629259@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/21023.1314629259@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, Kevin.Grittner@wicourts.gov, mmoncure@gmail.com, alexk@commandprompt.com
  Subject: Re: the '::' cast doesn't work in the FROM clause
  In-Reply-To: <21023.1314629259@sss.pgh.pa.us>
  References: <4E5B449602000025000407D9@gw.wicourts.gov> <CAHyXU0x-MgGEfa+P4+6fR=4q2k542P58QBQqn7cWcEqU3sdGTg@mail.gmail.com> <4E5B552502000025000407EB@gw.wicourts.gov> <21023.1314629259@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