zed
zed copied to clipboard
SQL: Referring to the same alias twice
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)
Unless I'm missing something this looks like a duplicate of #5976
Fair enough. When the queries are different it's not always obvious to me when they share a root cause.
Also fair
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!