Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 4E49CB5DC26 for ; Mon, 29 Aug 2011 10:15:59 -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 76141-02 for ; Mon, 29 Aug 2011 13:15:53 +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 D35B6B5DC35 for ; Mon, 29 Aug 2011 10:15:52 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by spinlock.commandprompt.com (Postfix) with ESMTP id 9B01634804A; Mon, 29 Aug 2011 06:15:52 -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 QjKBZK57BHHI; Mon, 29 Aug 2011 06:15:49 -0700 (PDT) Received: from [10.0.10.10] (unknown [88.135.117.31]) by spinlock.commandprompt.com (Postfix) with ESMTPSA id D2FF3348046; Mon, 29 Aug 2011 06:15:48 -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: <4E5B449602000025000407D9@gw.wicourts.gov> Date: Mon, 29 Aug 2011 16:15:44 +0300 Cc: pgsql-bugs@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: <7B691F0D-A0E3-4195-98E7-F0C5833A2B74@commandprompt.com> References: <4E5B449602000025000407D9@gw.wicourts.gov> To: Kevin Grittner 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/168 X-Sequence-Number: 30980 On Aug 29, 2011, at 3:49 PM, Kevin Grittner wrote: > Alexey Klyukin wrote: >=20 >> The following statement produces an error message in PostgreSQL 8.4 >> - 9.2 (head): >>=20 >> postgres=3D# select val from random()::integer as val; >=20 >> The same statement rewritten with CAST AS works as expected: >>=20 >> postgres=3D# select val from CAST(random() as integer) as val; >=20 >> The documentation says these casts are equivalent, so either that's >> wrong, or this is a bug. >=20 > Please point out where you think the documentation says that. Here: >=20 > A type cast specifies a conversion from one data type to another. = PostgreSQL accepts two equivalent syntaxes for type casts: >=20 > CAST ( expression AS type ) > expression::type >=20 = http://www.postgresql.org/docs/9.0/interactive/sql-expressions.html#SQL-SY= NTAX-TYPE-CASTS > The way I read it, this is the correct syntax: >=20 > test=3D# select val from (select random()::integer) as x(val); > val=20 > ----- > 1 > (1 row) >=20 > Not only are you missing required parentheses and the SELECT keyword, > you're returning a record rather than a scalar value. SELECT val FROM random() AS val (same as the problematic query, but w/o = casts) doesn't produce any errors and IMO is a valid syntax. Here's a = quote from the SELECT documentation: > Function calls can appear in the FROM clause. (This is especially = useful for functions that return result sets, but any function can be = used.) This acts as though its output were created as a temporary table = for the duration of this single SELECT command.=20 http://www.postgresql.org/docs/9.0/interactive/sql-select.html The problem is that 2 types of casts behave differently when applied to = random() in this query. -- Alexey Klyukin http://www.commandprompt.com The PostgreSQL Company =96 Command Prompt, Inc.