From: Alexey Klyukin <alexk@commandprompt.com>
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: pgsql-bugs@postgresql.org
Subject: Re: the '::' cast doesn't work in the FROM clause
Date: Mon, 29 Aug 2011 16:15:44 +0300
Message-ID: <7B691F0D-A0E3-4195-98E7-F0C5833A2B74@commandprompt.com> (raw)
In-Reply-To: <4E5B449602000025000407D9@gw.wicourts.gov>
References: <4E5B449602000025000407D9@gw.wicourts.gov>
On Aug 29, 2011, at 3:49 PM, Kevin Grittner wrote:
> Alexey Klyukin wrote:
>
>> The following statement produces an error message in PostgreSQL 8.4
>> - 9.2 (head):
>>
>> postgres=# select val from random()::integer as val;
>
>> The same statement rewritten with CAST AS works as expected:
>>
>> postgres=# select val from CAST(random() as integer) as val;
>
>> The documentation says these casts are equivalent, so either that's
>> wrong, or this is a bug.
>
> Please point out where you think the documentation says that.
Here:
>
> A type cast specifies a conversion from one data type to another. PostgreSQL accepts two equivalent syntaxes for type casts:
>
> CAST ( expression AS type )
> expression::type
>
http://www.postgresql.org/docs/9.0/interactive/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS> The way I read it, this is the correct syntax:
>
> test=# select val from (select random()::integer) as x(val);
> val
> -----
> 1
> (1 row)
>
> 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.
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 – Command Prompt, Inc.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: alexk@commandprompt.com, Kevin.Grittner@wicourts.gov
Subject: Re: the '::' cast doesn't work in the FROM clause
In-Reply-To: <7B691F0D-A0E3-4195-98E7-F0C5833A2B74@commandprompt.com>
References: <4E5B449602000025000407D9@gw.wicourts.gov> <7B691F0D-A0E3-4195-98E7-F0C5833A2B74@commandprompt.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox