Re: [GENERAL] select random order by random

2007-11-02 Thread John D. Burger
On Nov 1, 2007, at 18:57, Tom Lane wrote: In the usual tradition of SQL99, the spec text is enormously less readable than SQL92 was, but I *think* this says nearly the same thing as what we do: a plain column reference in ORDER BY is first sought as an output column name, and failing that

Re: [GENERAL] select random order by random

2007-11-02 Thread Ron Mayer
Chris Browne wrote: If I replicate your query, with extra columns, AND NAMES, I get the following: [EMAIL PROTECTED]:5433=# select random() as r1, random() as r2, random() as r3 from generate_series(1,10) order by random(); r1 | r2 |r3

[GENERAL] select random order by random

2007-11-01 Thread piotr_sobolewski
Dear sirs, I was very surprised when I executed such SQL query (under PostgreSQL 8.2): select random() from generate_series(1, 10) order by random(); I thought I would receive ten random numbers in random order. But I received ten random numbers sorted numerically: random

Re: [GENERAL] select random order by random

2007-11-01 Thread brian
piotr_sobolewski wrote: Dear sirs, I was very surprised when I executed such SQL query (under PostgreSQL 8.2): select random() from generate_series(1, 10) order by random(); I thought I would receive ten random numbers in random order. But I received ten random numbers sorted numerically:

Re: [GENERAL] select random order by random

2007-11-01 Thread Lee Keel
-Original Message- From: [EMAIL PROTECTED] [mailto:pgsql-general- [EMAIL PROTECTED] On Behalf Of piotr_sobolewski Sent: Thursday, November 01, 2007 9:25 AM To: pgsql-general@postgresql.org Subject: [GENERAL] select random order by random Dear sirs, I was very surprised when I

Re: [GENERAL] select random order by random

2007-11-01 Thread Scott Marlowe
On 11/1/07, Lee Keel [EMAIL PROTECTED] wrote: Dear sirs, I was very surprised when I executed such SQL query (under PostgreSQL 8.2): select random() from generate_series(1, 10) order by random(); I thought I would receive ten random numbers in random order. But I received ten

Re: [GENERAL] select random order by random

2007-11-01 Thread Gregory Stark
Scott Marlowe [EMAIL PROTECTED] writes: I think that Piotr expected the random() to be evaluated in both places separately. My guess is that it was recognized by the planner as the same function and evaluated once per row only. If you try this: select random() from generate_series(1, 10)

Re: [GENERAL] select random order by random

2007-11-01 Thread Richard Huxton
Gregory Stark wrote: Scott Marlowe [EMAIL PROTECTED] writes: I think that Piotr expected the random() to be evaluated in both places separately. My guess is that it was recognized by the planner as the same function and evaluated once per row only. If you try this: select random() from

Re: [GENERAL] select random order by random

2007-11-01 Thread Sam Mason
On Thu, Nov 01, 2007 at 04:49:16PM +, Richard Huxton wrote: Gregory Stark wrote: This does strike me as wrong. random() is marked volatile and the planner ought not collapse multiple calls into one. I think I agree with the earlier poster. Surely these two queries should be

Re: [GENERAL] select random order by random

2007-11-01 Thread Tom Lane
Richard Huxton [EMAIL PROTECTED] writes: Gregory Stark wrote: This does strike me as wrong. random() is marked volatile and the planner ought not collapse multiple calls into one. I think I agree with the earlier poster. Surely these two queries should be equivalent? SELECT random()

Re: [GENERAL] select random order by random

2007-11-01 Thread piotr_sobolewski
On Thursday 01 November 2007 17:08, brian wrote: I was very surprised when I executed such SQL query (under PostgreSQL 8.2): select random() from generate_series(1, 10) order by random(); I don't understand - why the result is like that? It seems like in each row both random()s were

Re: [GENERAL] select random order by random

2007-11-01 Thread piotr_sobolewski
On Thursday 01 November 2007 17:16, Scott Marlowe wrote: I was very surprised when I executed such SQL query (under PostgreSQL 8.2): select random() from generate_series(1, 10) order by random(); (...) My guess is that it was recognized by the planner as the same function and

Re: [GENERAL] select random order by random

2007-11-01 Thread Martijn van Oosterhout
On Thu, Nov 01, 2007 at 02:22:58PM -0400, Tom Lane wrote: SELECT random()FROM generate_series(1, 10) ORDER BY random(); SELECT random() AS foo FROM generate_series(1, 10) ORDER BY foo; (BTW, this is not the planner's fault; the collapsing of the two targetlist entries into one

Re: [GENERAL] select random order by random

2007-11-01 Thread Scott Marlowe
On 11/1/07, Martijn van Oosterhout [EMAIL PROTECTED] wrote: On Thu, Nov 01, 2007 at 02:22:58PM -0400, Tom Lane wrote: SELECT random()FROM generate_series(1, 10) ORDER BY random(); SELECT random() AS foo FROM generate_series(1, 10) ORDER BY foo; (BTW, this is not the planner's

Re: [GENERAL] select random order by random

2007-11-01 Thread Tom Lane
Martijn van Oosterhout [EMAIL PROTECTED] writes: Something twigged telling me that in fact the latter expression is not in standard SQL but a (very common) extension. A sort key is clearly indicated to be a value expression with no indication anywhere that column aliases are allowed here

Re: [GENERAL] select random order by random

2007-11-01 Thread Gregory Stark
Scott Marlowe [EMAIL PROTECTED] writes: On 11/1/07, Martijn van Oosterhout [EMAIL PROTECTED] wrote: On Thu, Nov 01, 2007 at 02:22:58PM -0400, Tom Lane wrote: SELECT random()FROM generate_series(1, 10) ORDER BY random(); SELECT random() AS foo FROM generate_series(1, 10) ORDER BY

Re: [GENERAL] select random order by random

2007-11-01 Thread Tom Lane
Gregory Stark [EMAIL PROTECTED] writes: So I guess having the parser do this substitution kind of makes sense if you're thinking about things the way the spec does. It doesn't make much sense if you're thinking the way Postgres does of having arbitrary expressions there independent of what's