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

From: Alexey Klyukin <alexk(at)commandprompt(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Kevin Grittner <Kevin(dot)Grittner(at)wicourts(dot)gov>, Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: the '::' cast doesn't work in the FROM clause
Date: 2011-08-29 15:31:48
Message-ID: B4AA1AAA-86F1-4ACC-B02D-0AB86C1A782D@commandprompt.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs


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

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

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.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2011-08-29 16:27:43 Re: BUG #6181: concat_ws() incompatibility with MySQL
Previous Message Tom Lane 2011-08-29 14:47:39 Re: the '::' cast doesn't work in the FROM clause