Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D20B8B5DC35 for ; Mon, 29 Aug 2011 12:32:04 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 49452-01 for ; Mon, 29 Aug 2011 15:31:57 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from spinlock.commandprompt.com (host-215.commandprompt.net [207.173.203.215]) by mail.postgresql.org (Postfix) with ESMTP id 42680B5DC36 for ; Mon, 29 Aug 2011 12:31:57 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by spinlock.commandprompt.com (Postfix) with ESMTP id 612AA34804A; Mon, 29 Aug 2011 08:31:56 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at commandprompt.com Received: from spinlock.commandprompt.com ([127.0.0.1]) by localhost (spinlock.commandprompt.com.commandprompt.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id udKXghkoOJ3Y; Mon, 29 Aug 2011 08:31:53 -0700 (PDT) Received: from [10.0.10.10] (unknown [88.135.117.31]) by spinlock.commandprompt.com (Postfix) with ESMTPSA id 69B39348046; Mon, 29 Aug 2011 08:31:51 -0700 (PDT) Subject: Re: the '::' cast doesn't work in the FROM clause Mime-Version: 1.0 (Apple Message framework v1244.3) Content-Type: text/plain; charset=windows-1252 From: Alexey Klyukin In-Reply-To: <21023.1314629259@sss.pgh.pa.us> Date: Mon, 29 Aug 2011 18:31:48 +0300 Cc: Kevin Grittner , Merlin Moncure , pgsql-bugs@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <4E5B449602000025000407D9@gw.wicourts.gov> <4E5B552502000025000407EB@gw.wicourts.gov> <21023.1314629259@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.1244.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201108/176 X-Sequence-Number: 30988 On Aug 29, 2011, at 5:47 PM, Tom Lane wrote: > "Kevin Grittner" writes: >> Merlin Moncure wrote: >>> yeah, that's the correct way, but why does this work? >>> select val from random() as val; >=20 >> 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 ::). >=20 > 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.=20 >=20 > 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. >=20 > So IMO there is no syntax bug here. =20 I agree, thank you and Kevin for the great explanation! > There is a dump/reload bug though :-( ... if you were to do >=20 > create view vv as select val from CAST(random() as integer) as val;=20 >=20 > 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 =96 Command Prompt, Inc.