zed icon indicating copy to clipboard operation
zed copied to clipboard

SQL: Output not stable with ORDER BY column number on GROUP BY'd aggregation

Open philrz opened this issue 8 months ago • 0 comments

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

With the following input data integers2.sup:

{i:1(int32)}
{i:2(int32)}
{i:3(int32)}
{i:null(int32)}

The output of the following query differs in repeated runs with super.

$ super -version
Version: 944989687

$ super -c "SELECT i, SUM(i) FROM 'integers2.sup' GROUP BY i ORDER BY 1;"
{i:1(int32),"SUM(i)":1}
{i:2(int32),"SUM(i)":2}
{i:3(int32),"SUM(i)":3}
{i:null(int32),"SUM(i)":null(int64)}

$ super -c "SELECT i, SUM(i) FROM 'integers2.sup' GROUP BY i ORDER BY 1;"
{i:3(int32),"SUM(i)":3}
{i:null(int32),"SUM(i)":null(int64)}
{i:1(int32),"SUM(i)":1}
{i:2(int32),"SUM(i)":2}

However, if the SQL ends with a reference to the column by name via ORDER BY i the output is stable.

In Postgres, the output against a table with the same input data is stable with both ORDER BY 1 and ORDER BY i.

philrz avatar Jun 23 '25 20:06 philrz