Skip to content

Error with query that has DISTINCT with ORDER BY and aliased select list #5293

@alamb

Description

@alamb

Describe the bug
#5132 introduced a regression regarding ordering by alias columns that were also marked as DISTINCT

To Reproduce

DataFusion CLI v18.0.0
❯ create or replace table t as select column1 as value, column2 as time from (select * from (values
  (1, timestamp '2022-01-01 00:00:30'),
  (2, timestamp '2022-01-01 01:00:10'),
  (3, timestamp '2022-01-02 00:00:20')
) as sq) as sq
;
0 rows in set. Query took 0.007 seconds.
❯ SELECT DISTINCT time as "first_seen" FROM t ORDER BY time;
Plan("For SELECT DISTINCT, ORDER BY expressions time must appear in select list")

Expected behavior
The query should succeed and produce the same answer as posgres

postgres=# postgres=# create  table t as select column1 as value, column2 as time from (select * from (values
  (1, timestamp '2022-01-01 00:00:30'),
  (2, timestamp '2022-01-01 01:00:10'),
  (3, timestamp '2022-01-02 00:00:20')
) as sq) as sq
;
SELECT 3
postgres=# SELECT DISTINCT time as "first_seen" FROM t ORDER BY time;
     first_seen
---------------------
 2022-01-01 00:00:30
 2022-01-01 01:00:10
 2022-01-02 00:00:20
(3 rows)

Additional context
We found this internally in IOx.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions