Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id E2D3FB5DC56 for ; Mon, 29 Aug 2011 11:47:48 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 16384-03 for ; Mon, 29 Aug 2011 14:47:42 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 832FEB5DC35 for ; Mon, 29 Aug 2011 11:47:42 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p7TEld11021024; Mon, 29 Aug 2011 10:47:39 -0400 (EDT) To: "Kevin Grittner" cc: "Merlin Moncure" , alexk@commandprompt.com, pgsql-bugs@postgresql.org Subject: Re: the '::' cast doesn't work in the FROM clause In-reply-to: <4E5B552502000025000407EB@gw.wicourts.gov> References: <4E5B449602000025000407D9@gw.wicourts.gov> <4E5B552502000025000407EB@gw.wicourts.gov> Comments: In-reply-to "Kevin Grittner" message dated "Mon, 29 Aug 2011 09:00:21 -0500" Date: Mon, 29 Aug 2011 10:47:39 -0400 Message-ID: <21023.1314629259@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201108/175 X-Sequence-Number: 30987 "Kevin Grittner" writes: > Merlin Moncure 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