pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias
3+ messages / 3 participants
[nested] [flat]

* BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias
@ 2016-06-14 09:48 gabrimonfa@gmail.com
  2016-06-14 15:00 ` Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: gabrimonfa@gmail.com @ 2016-06-14 09:48 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      14188
Logged by:          Gabriele Monfardini
Email address:      gabrimonfa@gmail.com
PostgreSQL version: 9.5.3
Operating system:   Debian
Description:        

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
RIGA 1: ...t1 INNER JOIN table2 t2 ON (t1.id = t2.id) ORDER BY t1.name;

SELECT DISTINCT name FROM table1 t1 INNER JOIN table2 t2 ON (t1.id = t2.id)
ORDER BY name;
 name 
------
(0 righe)

Sure name may be qualified in SELECT list but it is not ambiguous.

Other example:
CREATE TABLE table1 (id INTEGER, NAME VARCHAR);
CREATE TABLE table2 (id INTEGER, home VARCHAR);
 
SELECT DISTINCT * FROM table1 t1 INNER JOIN table2 t2 ON (t1.id = t2.id)
ORDER BY NAME;
 id | NAME | id | home
----+------+----+------
(0 righe)
 
SELECT DISTINCT * 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
RIGA 1: ...t1 INNER JOIN table2 t2 ON (t1.id = t2.id) ORDER BY t1.NAME;
 
SELECT DISTINCT t1.*,t2.* FROM table1 t1 INNER JOIN table2 t2 ON (t1.id =
t2.id) ORDER BY t1.NAME;
 id | NAME | id | home
----+------+----+------
(0 righe)


Surely query may be made smarter always qualifying attributes in SELECT list
but I think it is may be considered valid SQL.

Or it is to be considered invalid since "name" without qualification in
SELECT is considered not an attribute of t1 but an attribute of "(t1 join
t2)" and thus t1.name is not in select list?



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias
  2016-06-14 09:48 BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias gabrimonfa@gmail.com
@ 2016-06-14 15:00 ` Tom Lane <tgl@sss.pgh.pa.us>
  2016-06-15 07:45   ` Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias Gabriele Monfardini <gabrimonfa@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2016-06-14 15:00 UTC (permalink / raw)
  To: gabrimonfa@gmail.com; +Cc: pgsql-bugs

gabrimonfa@gmail.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.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias
  2016-06-14 09:48 BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias gabrimonfa@gmail.com
  2016-06-14 15:00 ` Re: BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-06-15 07:45   ` Gabriele Monfardini <gabrimonfa@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Gabriele Monfardini @ 2016-06-15 07:45 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

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

> gabrimonfa@gmail.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

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2016-06-15 07:45 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-06-14 09:48 BUG #14188: "FOR SELECT DISTINCT, ORDER BY expressions must appear IN SELECT list" error and table alias gabrimonfa@gmail.com
2016-06-14 15:00 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-06-15 07:45   ` Gabriele Monfardini <gabrimonfa@gmail.com>

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