Re: the '::' cast doesn't work in the FROM clause - Mailing list pgsql-bugs

From Tom Lane
Subject Re: the '::' cast doesn't work in the FROM clause
Date
Msg-id 21023.1314629259@sss.pgh.pa.us
Whole thread Raw
In response to Re: the '::' cast doesn't work in the FROM clause  ("Kevin Grittner" <Kevin.Grittner@wicourts.gov>)
Responses Re: the '::' cast doesn't work in the FROM clause
List pgsql-bugs
"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

pgsql-bugs by date:

Previous
From: "Kevin Grittner"
Date:
Subject: Re: the '::' cast doesn't work in the FROM clause
Next
From: Alexey Klyukin
Date:
Subject: Re: the '::' cast doesn't work in the FROM clause