pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Klyukin <alexk@commandprompt.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: Merlin Moncure <mmoncure@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: the '::' cast doesn't work in the FROM clause
Date: Mon, 29 Aug 2011 18:31:48 +0300
Message-ID: <B4AA1AAA-86F1-4ACC-B02D-0AB86C1A782D@commandprompt.com> (raw)
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>


On Aug 29, 2011, at 5:47 PM, Tom Lane wrote:

> "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.

I was wondering exactly why CAST() is permitted, while it's not a function (in Kevin's example, texteq is a function) and the explanation above answers my question. 

> 
> 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.  

I agree, thank you and Kevin for the great explanation!


> 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.

Would it be acceptable/sufficient to output  CAST(...) instead of '::'  for all casts in pg_dump to fix this problem, assuming that CAST can be used anywhere where '::' is accepted?

--
Alexey Klyukin        http://www.commandprompt.com
The PostgreSQL Company – Command Prompt, Inc.







view thread (13+ messages)  latest in thread

Message-ID: <B4AA1AAA-86F1-4ACC-B02D-0AB86C1A782D@commandprompt.com>
Permalink:  ../B4AA1AAA-86F1-4ACC-B02D-0AB86C1A782D@commandprompt.com/
Also on:    postgresql.org/message-id/B4AA1AAA-86F1-4ACC-B02D-0AB86C1A782D@commandprompt.com

 · 

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: alexk@commandprompt.com, tgl@sss.pgh.pa.us, Kevin.Grittner@wicourts.gov, mmoncure@gmail.com
  Subject: Re: the '::' cast doesn't work in the FROM clause
  In-Reply-To: <B4AA1AAA-86F1-4ACC-B02D-0AB86C1A782D@commandprompt.com>
  References: <4E5B449602000025000407D9@gw.wicourts.gov> <CAHyXU0x-MgGEfa+P4+6fR=4q2k542P58QBQqn7cWcEqU3sdGTg@mail.gmail.com> <4E5B552502000025000407EB@gw.wicourts.gov> <21023.1314629259@sss.pgh.pa.us> <B4AA1AAA-86F1-4ACC-B02D-0AB86C1A782D@commandprompt.com>

* 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