Skip to content

min() and max() should have an effect on bare columns #3444

@LeMikaelF

Description

@LeMikaelF

SQLite special-cases bare columns (unaggregated columns that aren't functionally dependent on aggregated columns). From the documentation:

Special processing occurs when the aggregate function is either min() or max(). (...) If there is exactly one min() or max() aggregate in the query, then all bare columns in the result set take values from an input row which also contains the minimum or maximum. [... see the rest for more details]

Turso doesn't seem to have this special-case handling:

turso> create table t(a,b,c);
turso> insert into t values (1, 'a', 'a'), (1, 'b', 'b'), (1, 'c', 'c');
turso> select a, b, max(c) from t group by a; -- b column is incorrect
┌───┬───┬───────────┐
│ a │ b │ max (t.c) │
├───┼───┼───────────┤
│ 1 │ a │ c         │
└───┴───┴───────────┘
turso> select a, b, min(c) from t group by a; -- this one happens to be correct
┌───┬───┬───────────┐
│ a │ b │ min (t.c) │
├───┼───┼───────────┤
│ 1 │ a │ a         │
└───┴───┴───────────┘

SQLite, for comparison:

sqlite> create table t(a,b,c);
sqlite> insert into t values (1, 'a', 'a'), (1, 'b', 'b'), (1, 'c', 'c');
sqlite> select a, b, max(c) from t group by a;
1|c|c
sqlite> select a, b, min(c) from t group by a;
1|a|a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions