Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using table alias to qualify column name in ORDER BY results in error on SELECT DISTINCT #16700

Open
joelind opened this issue May 10, 2024 · 2 comments
Assignees
Labels
Milestone

Comments

@joelind
Copy link

joelind commented May 10, 2024

Describe the bug

When using SELECT DISTINCT with a table alias, we get a syntax error telling us that ORDER BY expressions must appear in select list, even though the column is in the select list (but is aliased).

Error message/log

psql -h localhost -p 4566 -U root -d dev <<SQL
with integers as (
  select 1 as numeral, 'first' as ordinal
)

select distinct numeral, ordinal
from integers i
order by i.numeral
SQL
ERROR:  Failed to run the query

Caused by:
  Invalid input syntax: for SELECT DISTINCT, ORDER BY expressions must appear in select list

To Reproduce

This works:

psql -h localhost -p 4566 -U root -d dev <<SQL
with integers as (
  select 1 as numeral, 'first' as ordinal
)

select distinct numeral, ordinal
from integers i
order by numeral
SQL
 numeral | ordinal
---------+---------
       1 | first
(1 row)

Whereas this doesn't (only difference is that we use order by *i*.numeral:

psql -h localhost -p 4566 -U root -d dev <<SQL
with integers as (
  select 1 as numeral, 'first' as ordinal
)

select distinct numeral, ordinal
from integers i
order by i.numeral
SQL
ERROR:  Failed to run the query

Caused by:
  Invalid input syntax: for SELECT DISTINCT, ORDER BY expressions must appear in select list

I also tried ensuring that it's the literal same expression in the select list, but that fails as well:

psql -h localhost -p 4566 -U root -d dev <<SQL
with integers as (
  select 1 as numeral, 'first' as ordinal
)

select distinct i.numeral, ordinal
from integers i
order by i.numeral
SQL
ERROR:  Failed to run the query

Caused by:
  Invalid input syntax: for SELECT DISTINCT, ORDER BY expressions must appear in select list

Expected behavior

I expected to be able to use the table alias to qualify the column name in the ORDER BY clause of my query.

How did you deploy RisingWave?

Both via docker-compose.yml locally, and RisingWave Cloud.

The version of RisingWave

 psql -h localhost -p 4566 -U root -d dev <<SQL
select version()
SQL
                                    version
--------------------------------------------------------------------------------
 PostgreSQL 13.14.0-RisingWave-1.8.1 (5b6bef82dc55638c024190654c7ef9896e21687d)
(1 row)

Additional context

No response

@joelind joelind added the type/bug Something isn't working label May 10, 2024
@github-actions github-actions bot added this to the release-1.10 milestone May 10, 2024
@xiangjinwu
Copy link
Contributor

Thanks for reporting the issue. We will try to get it fixed.

However, the preferred syntax of ORDER BY is to use the unqualified output name, rather than a (qualified or unqualified) input name.

select distinct numeral as column_a, ordinal
from integers i
order by column_a; -- This is referring to the output after distinct.

select distinct numeral as column_a, ordinal
from integers i
order by numeral; -- This now fails similar to `i.numeral` because RisingWave fails to recognize it is same column as `column_a`

@xiangjinwu xiangjinwu self-assigned this May 13, 2024
@joelind
Copy link
Author

joelind commented May 13, 2024

Thanks - good to know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants