Skip to content

ORDER BY LOWER(c) in a query grouped by LOWER(c) does not sort #2817

Open

Description

@mathiasrw

In a grouped query, ORDER BY with a function of the group key (ORDER BY LOWER(c) after GROUP BY LOWER(c)) does not sort. Ordering by the alias works, and the same ORDER BY without GROUP BY works.

Seen in the PostgreSQL regression tests run (alasql-pgsqltest) in select_having.sql and select_implicit.sql, for example SELECT lower(c), count(c) FROM test_having GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a) ORDER BY lower(c). Reproduced on develop (38bf3c3):

alasql('CREATE TABLE th (a INT, c TEXT)');
alasql("INSERT INTO th VALUES (0, 'XXXX'), (1, 'bbbb'), (2, 'BBBB'), (3, 'cccc')");
alasql('SELECT LOWER(c) AS l, COUNT(*) AS n FROM th GROUP BY LOWER(c) ORDER BY LOWER(c)');
// [{l: 'xxxx', n: 1}, {l: 'bbbb', n: 2}, {l: 'cccc', n: 1}]
alasql('SELECT LOWER(c) AS l, COUNT(*) AS n FROM th GROUP BY LOWER(c) ORDER BY l');
// [{l: 'bbbb', n: 2}, {l: 'cccc', n: 1}, {l: 'xxxx', n: 1}]

Expected: the first query sorts like the second: bbbb, cccc, xxxx.

Breaking?

No. The rows come in an order nobody asked for.

Related: #2630.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions