Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias

From: Gabriele Monfardini <gabrimonfa(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias
Date: 2016-06-15 07:45:13
Message-ID: CACw3ADjcJiJDg1G8MYNdHFJLXoefEQt3rC9k+HWODzL=jrQQnA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, Jun 14, 2016 at 5:00 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> gabrimonfa(at)gmail(dot)com writes:
> > CREATE TABLE table1 (id integer primary key, name varchar);
> > CREATE TABLE table2 (id integer primary key, home varchar);
>
> > SELECT DISTINCT name FROM table1 t1 INNER JOIN table2 t2 ON (t1.id =
> t2.id)
> > ORDER BY t1.name;
> > ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select
> list
> > SELECT DISTINCT name FROM table1 t1 INNER JOIN table2 t2 ON (t1.id =
> t2.id)
> > ORDER BY name;
> > [ok]
>
> The reason for the discrepancy is that "t1.name" refers to an output
> column of t1, while "name" refers to an output column of the unnamed JOIN.
> While those are semantically equivalent in this particular case, they are
> not so in general --- in particular, had this been a FULL JOIN, they
> would definitely not be equivalent. PG's parser treats them as different
> variables and therefore sees "ORDER BY t1.name" as unrelated to the value
> being distinct'ed on.
>
> We might someday try to make the parser smarter about recognizing such
> equivalences earlier, but I'm not terribly excited about it.
>

yes, it would probably not worth the effort.
Thank you for the explanation.
Best regards,

Gabriele Monfardini

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Michael Paquier 2016-06-15 08:03:28 Re: pg_dump - wrong order with inheritance
Previous Message Michael Paquier 2016-06-15 07:37:12 Re: BUG #13907: Restore materialized view throw permission denied