zed icon indicating copy to clipboard operation
zed copied to clipboard

SQL: Referring to the same alias twice

Open philrz opened this issue 8 months ago • 3 comments

Repro is with super commit 9449896. This was found via a query from a sqllogictest.

Repro with test data:

$ cat integers.sup
{i:3(int32),j:4(int32)}
{i:3(int32),j:4(int32)}
{i:2(int32),j:4(int32)}

$ super -version
Version: 944989687

$ super -c "SELECT i % 2 AS k, SUM(i) FROM 'integers.sup' GROUP BY k, k ORDER BY 1;"
{k:1,"SUM(i)":6}
{k:0,"SUM(i)":2}

Whereas in Postgres, it looks like the ORDER BY had the intended effect.

$ psql postgres
psql (17.5 (Homebrew))
Type "help" for help.

postgres=# SELECT * FROM integers;
 i | j 
---+---
 3 | 4
 3 | 4
 2 | 4
(3 rows)

postgres=# SELECT i % 2 AS k, SUM(i) FROM integers GROUP BY k, k ORDER BY 1;
 k | sum 
---+-----
 0 |   2
 1 |   6
(2 rows)

philrz avatar Jun 23 '25 18:06 philrz

Unless I'm missing something this looks like a duplicate of #5976

mattnibs avatar Jun 28 '25 00:06 mattnibs

Fair enough. When the queries are different it's not always obvious to me when they share a root cause.

philrz avatar Jun 28 '25 17:06 philrz

Also fair

mattnibs avatar Jun 28 '25 20:06 mattnibs

Verified in super commit 69d0499.

The ORDER BY now has the expected impact, producing a query result that matches the one from Postgres.

$ super -version
Version: 69d049968

$ super -c "SELECT i % 2 AS k, SUM(i) FROM 'integers.sup' GROUP BY k, k ORDER BY 1;"
{k:0,"SUM(i)":2}
{k:1,"SUM(i)":6}

Thanks @mattnibs!

philrz avatar Jun 30 '25 18:06 philrz