Re: inconsistency in aliasing

Lists: pgsql-generalpgsql-hackers
From: Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: inconsistency in aliasing
Date: 2009-01-14 10:46:11
Message-ID: 20090114104611.GA4168@apartia.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Hi,

This works:

critik=# select current_timestamp::abstime::int4 as score order by score;

This doesn't:

critik=# select current_timestamp::abstime::int4 as score order by score + 1;
ERROR: column "score" does not exist
LINE 1: ...urrent_timestamp::abstime::int4 as score order by score + 1 ...

Any idea ?

Thanks,

--
http://www.critikart.net


From: Reg Me Please <regmeplease(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: "Louis-David Mitterrand" <vindex+lists-pgsql-general(at)apartia(dot)org>
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 11:01:23
Message-ID: 200901141201.23496.regmeplease@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On Wednesday 14 January 2009 11:46:11 Louis-David Mitterrand wrote:
> Hi,
>
> This works:
>
> critik=# select current_timestamp::abstime::int4 as score order by score;
>
> This doesn't:
>
> critik=# select current_timestamp::abstime::int4 as score order by score +
> 1; ERROR: column "score" does not exist
> LINE 1: ...urrent_timestamp::abstime::int4 as score order by score + 1 ...
>
> Any idea ?
>
> Thanks,

Looks and smells like a bug.

--
Fahrbahn ist ein graues Band
weisse Streifen, grüner Rand


From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 11:06:47
Message-ID: 20090114110647.GC30398@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

In response to Louis-David Mitterrand :
> Hi,
>
> This works:
>
> critik=# select current_timestamp::abstime::int4 as score order by score;
>
> This doesn't:
>
> critik=# select current_timestamp::abstime::int4 as score order by score + 1;
> ERROR: column "score" does not exist
> LINE 1: ...urrent_timestamp::abstime::int4 as score order by score + 1 ...
>
> Any idea ?

Yes, you can't use the alias in the ORDER BY. Use the real column-name.

select current_timestamp::abstime::int4 as score order by current_timestamp::abstime::int4;

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net


From: Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 11:27:48
Message-ID: 20090114112748.GA5569@apartia.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On Wed, Jan 14, 2009 at 12:06:47PM +0100, A. Kretschmer wrote:
> In response to Louis-David Mitterrand :
> > Hi,
> >
> > This works:
> >
> > critik=# select current_timestamp::abstime::int4 as score order by score;
> >
> > This doesn't:
> >
> > critik=# select current_timestamp::abstime::int4 as score order by score + 1;
> > ERROR: column "score" does not exist
> > LINE 1: ...urrent_timestamp::abstime::int4 as score order by score + 1 ...
> >
> > Any idea ?
>
> Yes, you can't use the alias in the ORDER BY. Use the real column-name.
>
> select current_timestamp::abstime::int4 as score order by
> current_timestamp::abstime::int4;

Did you try

"select current_timestamp::abstime::int4 as score order by score;" ?

This seems to be an "order by <alias>"

--
http://www.critikart.net


From: "dbalinglung" <alamsurya(at)centrin(dot)net(dot)id>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 11:31:55
Message-ID: C33CB97C7C624D558CFE18559248A623@alam
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

what for of the syntax command +1 on order by ? maybe just wrong to given result about the error query on order by, it's BUG ?

dbalinglung

DataproSoft Developer

----- Original Message -----
From: "Louis-David Mitterrand" <vindex+lists-pgsql-general(at)apartia(dot)org>
To: <pgsql-general(at)postgresql(dot)org>
Sent: Wednesday, January 14, 2009 5:46 PM
Subject: [GENERAL] inconsistency in aliasing

> Hi,
>
> This works:
>
> critik=# select current_timestamp::abstime::int4 as score order by score;
>
> This doesn't:
>
> critik=# select current_timestamp::abstime::int4 as score order by score + 1;
> ERROR: column "score" does not exist
> LINE 1: ...urrent_timestamp::abstime::int4 as score order by score + 1 ...
>
> Any idea ?
>
> Thanks,
>
> --
> http://www.critikart.net
>


From: Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 11:34:06
Message-ID: 20090114113406.GA5804@apartia.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On Wed, Jan 14, 2009 at 06:31:55PM +0700, dbalinglung wrote:
> what for of the syntax command +1 on order by ? maybe just wrong to
> given result about the error query on order by, it's BUG ?

*PARSE ERROR*


From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 11:38:19
Message-ID: 20090114113819.GD30398@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

In response to Louis-David Mitterrand :
> > Yes, you can't use the alias in the ORDER BY. Use the real column-name.
> >
> > select current_timestamp::abstime::int4 as score order by
> > current_timestamp::abstime::int4;
>
> Did you try
>
> "select current_timestamp::abstime::int4 as score order by score;" ?
>
> This seems to be an "order by <alias>"

Ouch, my fault, muddled with WHERE

Thx, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net


From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 11:41:40
Message-ID: dcc563d10901140341i7fe771eel85863482285378e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On Wed, Jan 14, 2009 at 3:46 AM, Louis-David Mitterrand
<vindex+lists-pgsql-general(at)apartia(dot)org> wrote:
> Hi,
>
> This works:
>
> critik=# select current_timestamp::abstime::int4 as score order by score;
>
> This doesn't:
>
> critik=# select current_timestamp::abstime::int4 as score order by score + 1;
> ERROR: column "score" does not exist
> LINE 1: ...urrent_timestamp::abstime::int4 as score order by score + 1 ...

A number in an order by refers to a column.

select a, b, c/d as f from table order by 3;

will order by the third field in the select list. I'm guessing the +
1 is trying to add col 1 to a field it can't find. doubt it's a bug,
more like a quirk.


From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 12:07:51
Message-ID: 66444c23-faae-43e3-bc53-007c54d690f8@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Reg Me Please wrote:

> > critik=# select current_timestamp::abstime::int4 as score order
by score +
> > 1; ERROR: column "score" does not exist
> > LINE 1: ...urrent_timestamp::abstime::int4 as score order by
score + 1 ...
> >
> > Any idea ?
> >
> > Thanks,
>
> Looks and smells like a bug.

Read
http://www.postgresql.org/docs/8.3/static/queries-order.html

<quote>
The sort expression(s) can be any expression that would be valid in
the query's select list
</quote>

score+1 is not valid in the query's select list (as well as "score"
anyway)

<quote>
For backwards compatibility with the SQL92 version of the standard, a
sort_expression can instead be the name or number of an output column
</quote>

so that's why "score" alone works in the order by, despite it not being
valid in the select list.

<quote>
Note that an output column name has to stand alone, it's not allowed
as part of an expression
</quote>

Which looks like the very issue discussed here, and it just behaves as
documented.

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage:
http://www.manitou-mail.org


From: Lennin Caro <lennin(dot)caro(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org, Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org>
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 13:53:57
Message-ID: 251719.28928.qm@web59504.mail.ac4.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

--- On Wed, 1/14/09, Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org> wrote:

> From: Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org>
> Subject: Re: [GENERAL] inconsistency in aliasing
> To: pgsql-general(at)postgresql(dot)org
> Date: Wednesday, January 14, 2009, 11:27 AM
> On Wed, Jan 14, 2009 at 12:06:47PM +0100, A. Kretschmer
> wrote:
> > In response to Louis-David Mitterrand :
> > > Hi,
> > >
> > > This works:
> > >
> > > critik=# select current_timestamp::abstime::int4
> as score order by score;
> > >
> > > This doesn't:
> > >
> > > critik=# select current_timestamp::abstime::int4
> as score order by score + 1;
> > > ERROR: column "score" does not exist
> > > LINE 1: ...urrent_timestamp::abstime::int4 as
> score order by score + 1 ...
> > >
> > > Any idea ?
> >
> > Yes, you can't use the alias in the ORDER BY. Use
> the real column-name.
> >
> > select current_timestamp::abstime::int4 as score order
> by
> > current_timestamp::abstime::int4;
>
> Did you try
>
> "select current_timestamp::abstime::int4 as score
> order by score;" ?
>
> This seems to be an "order by <alias>"
>
> --
> http://www.critikart.net
>

you can't use operator in the group by, try this

select score,score+1 as score2 from (
select current_timestamp::abstime::int4 as score)
order by score2


From: Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 14:01:35
Message-ID: 20090114140135.GA10284@apartia.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

On Wed, Jan 14, 2009 at 05:53:57AM -0800, Lennin Caro wrote:
> --- On Wed, 1/14/09, Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org> wrote:
>
> > From: Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org>
> > Subject: Re: [GENERAL] inconsistency in aliasing
> > To: pgsql-general(at)postgresql(dot)org
> > Date: Wednesday, January 14, 2009, 11:27 AM
> > On Wed, Jan 14, 2009 at 12:06:47PM +0100, A. Kretschmer
> > wrote:
> > > In response to Louis-David Mitterrand :
> > > > Hi,
> > > >
> > > > This works:
> > > >
> > > > critik=# select current_timestamp::abstime::int4
> > as score order by score;
> > > >
> > > > This doesn't:
> > > >
> > > > critik=# select current_timestamp::abstime::int4
> > as score order by score + 1;
> > > > ERROR: column "score" does not exist
> > > > LINE 1: ...urrent_timestamp::abstime::int4 as
> > score order by score + 1 ...
> > > >
> > > > Any idea ?
> > >
> > > Yes, you can't use the alias in the ORDER BY. Use
> > the real column-name.
> > >
> > > select current_timestamp::abstime::int4 as score order
> > by
> > > current_timestamp::abstime::int4;
> >
> > Did you try
> >
> > "select current_timestamp::abstime::int4 as score
> > order by score;" ?
> >
> > This seems to be an "order by <alias>"
> >
> > --
> > http://www.critikart.net
> >
>
> you can't use operator in the group by, try this

Really?

select current_timestamp::abstime::int4 as score order by 1 + 1 ;

score
------------
1231941662
(1 row)

--
http://www.critikart.net


From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 14:21:02
Message-ID: c95dacb0-d958-4c7c-90f4-e387314c46da@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

Lennin Caro wrote:

> select score,score+1 as score2 from (
> select current_timestamp::abstime::int4 as score)
> order by score2

That additional score2 is not needed in the select output.

This works just fine:

=> select score from (select current_timestamp::abstime::int4 as score)
subsel order by score+1;

The point is that the subselect makes "score" available as a valid
expression to the upper select.

Best regards,
--
Daniel
PostgreSQL-powered mail user agent and storage:
http://www.manitou-mail.org


From: Emanuel Calvo Franco <postgres(dot)arg(at)gmail(dot)com>
To: General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: inconsistency in aliasing
Date: 2009-01-14 14:46:00
Message-ID: f205bb120901140646k5541639an9573349c1dda9488@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

2009/1/14 Daniel Verite <daniel(at)manitou-mail(dot)org>:
> Lennin Caro wrote:
>
>> select score,score+1 as score2 from (
>> select current_timestamp::abstime::int4 as score)
>> order by score2
>
> That additional score2 is not needed in the select output.
>
> This works just fine:
>
> => select score from (select current_timestamp::abstime::int4 as score)
> subsel order by score+1;
>

I'm been watching that the string name of order by with operator just work fine
if the column name is in the select clause. But if you use alias this
not work...

I'm don't believe is a bug, is a string name question. You can't add 1
to an alias,
but you can add 1 to a field... but the results is the same

> The point is that the subselect makes "score" available as a valid
> expression to the upper select.
>
> Best regards,
> --
> Daniel
> PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

--
Emanuel Calvo Franco
ArPUG / AOSUG Member
Postgresql Support & Admin


From: Jasen Betts <jasen(at)xnet(dot)co(dot)nz>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Re: inconsistency in aliasing
Date: 2009-01-15 10:23:16
Message-ID: gkn2qkm@reversiblemaps.ath.cx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers


On 2009-01-14, Louis-David Mitterrand <vindex+lists-pgsql-general(at)apartia(dot)org> wrote:
> Hi,
>
> This works:
>
> critik=# select current_timestamp::abstime::int4 as score order by score;
>
> This doesn't:
>
> critik=# select current_timestamp::abstime::int4 as score order by score + 1;
> ERROR: column "score" does not exist
> LINE 1: ...urrent_timestamp::abstime::int4 as score order by score + 1 ...
>
> Any idea ?
>
> Thanks,

if you really want to do that you can use a subselect,

select score from ( select current_timestamp::abstime::int4 as score )
as foo order by score+1;

but the demonstration is more interesting when the query returns several rows
and the expression changes the order.

select score from ( select generate_series(1,10) as score ) as foo
order by score % 2 desc; -- odds and evens.

:)

bye.


From: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
To: Emanuel Calvo Franco <postgres(dot)arg(at)gmail(dot)com>
Cc: General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: inconsistency in aliasing
Date: 2009-01-15 10:23:34
Message-ID: 2f4958ff0901150223p501efbc6u62b3f8f999949e6f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general pgsql-hackers

as far as I know, this bit (statement evaluation) wasn't implemented
then. It only got there in 8.4, so you can have even subselects
evaluated.
So it isn't a bug, it just wasn't implemented to work that way back than,