Skip site navigation (1) Skip section navigation (2)

Re: the '::' cast doesn't work in the FROM clause

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
Cc: "Merlin Moncure" <mmoncure(at)gmail(dot)com>, alexk(at)commandprompt(dot)com, pgsql-bugs(at)postgresql(dot)org
Subject: Re: the '::' cast doesn't work in the FROM clause
Date: 2011-08-29 14:47:39
Message-ID: 21023.1314629259@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-bugs
"Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov> writes:
> Merlin Moncure <mmoncure(at)gmail(dot)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

In response to

Responses

pgsql-bugs by date

Next:From: Alexey KlyukinDate: 2011-08-29 15:31:48
Subject: Re: the '::' cast doesn't work in the FROM clause
Previous:From: Kevin GrittnerDate: 2011-08-29 14:34:21
Subject: Re: the '::' cast doesn't work in the FROM clause

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group