Skip to content

Window aggregates read the output row: SUM(v) OVER (PARTITION BY g) gives 0 when v is not selected #2689

Open

Description

@mathiasrw

SUM, COUNT, AVG, MIN and MAX with OVER (PARTITION BY …) read the partition and the value from the output row by name. When the column is not in the SELECT list under its own name, or the argument is an expression, the result is wrong and there is no error.

Measured on develop (94ccf84), data g, v: ('x', 1), ('x', 2), ('y', 5), next to SQLite 3.43:

Query AlaSQL SQLite
SELECT g, v, SUM(v) OVER (PARTITION BY g) AS s 3, 3, 5 3, 3, 5
SELECT v, SUM(v) OVER (PARTITION BY g) AS s (no g in the list) 8, 8, 8 3, 3, 5
SELECT g, SUM(v) OVER (PARTITION BY g) AS s (no v in the list) 0, 0, 0 3, 3, 5
SELECT g, v AS val, SUM(v) OVER (PARTITION BY g) AS s 0, 0, 0 3, 3, 5
SELECT g, v, SUM(v * 2) OVER (PARTITION BY g) AS s 0, 0, 0 6, 6, 10

Cause

yy.Select.compile (src/40select.js, from #2411) computes the window aggregates after the SELECT list is built, from res[i][columnid]: the output row, and only a plain column name. The source row and the compiled expression are not used.

Direction

Compute the partition key and the argument from the source row with the compiled expressions, the way the SELECT list itself is computed, then attach the result to the output row. Nobody can rely on 0 or a sum over all partitions here, so this can go into 4.x.

The window's own ORDER BY (a running sum) is not applied either; that is the same gap as #2591, which is in 5.0.

Related: #156, #898, #2408, #2411, #2591.

Activity

  1. added this to the v4.x Focus milestone on Oct 20, 2026
  2. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I'd like to work on this one.

  3. mateuscmtropical commented on Nov 11, 2026

    @mateuscmtropical
    Contributor

    Hi, I've opened a PR for this: #3052

  4. modified the milestones: v4.x Focus, v5.0.0 on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions