Re: Question, how intelligent is optimizer with subplans? - Mailing list pgsql-general

From Tom Lane
Subject Re: Question, how intelligent is optimizer with subplans?
Date
Msg-id 21650.1043096985@sss.pgh.pa.us
Whole thread Raw
In response to Re: Question, how intelligent is optimizer with subplans?  (Greg Stark <gsstark@mit.edu>)
List pgsql-general
Greg Stark <gsstark@MIT.EDU> writes:
> On second thought what I wanted to do should work, I think. I'm not clear why
> the first of these works fine but the second doesn't. What I want to do is
> effectively the second of these:
> slo=> select (select count(*) from t2) as x from t order by x;
>  x
> ---
>  0
> (1 row)

> slo=> select (select count(*) from t2) as x from t order by sign(x);
> ERROR:  Attribute "x" not found

The first of those works because the SQL spec says it should: unadorned
names appearing in ORDER BY are output column names per the spec.

The second of those is not legal per SQL spec (you can't put anything
except an unadorned output column name or number in ORDER BY, according
to the spec).  Postgres accepts expressions in ORDER BY, but we consider
them to be expressions in the input column names.

You could do something like

SELECT x
FROM
  (select count(*) as x from t2) as ss
ORDER BY sign(x);

            regards, tom lane

pgsql-general by date:

Previous
From: elein
Date:
Subject: Re: Writing apps for ORDBMS
Next
From: Stephan Szabo
Date:
Subject: Re: Help on query plan. (was: select like and indexes)