Skip to content

MIN and MAX compare through the comparison module, and SUM over no rows is NULL, with a dialect keeping the old rules #2696

Open

Description

@mathiasrw

What happens today

After #2660 every aggregate is an object with init, step and result. MIN and MAX accept only numbers, bigints, Date objects and objects that convert to a number. Text is left out, and so is a DATE column, which holds text by default.

alasql('SELECT MAX(s) AS m, COUNT(*) AS c FROM ?', [[{s: 'b'}, {s: 'a'}]]);
// [{c: 2}]    SQLite gives m = 'b'

SUM over no rows gives 0. SQLite gives NULL for SUM and 0 for TOTAL.

What to change

  1. MIN and MAX take their comparison from the comparison module of Make the standard comparison level the default, with dates by value and SQLite's order across types #3171. step skips NULL and keeps the smaller or larger value by alasql.cmp, so numbers, text and dates work as in ORDER BY.
  2. SUM returns NULL when it saw no value that is not NULL. TOTAL keeps returning 0.
  3. Add the rule aggregateValues to Rules that differ between dialects become named rules chosen when a statement is compiled #3170 with the values sqlite (the new default) and v4. With v4, MIN and MAX keep the type list and SUM over no rows is 0. Add it to alasql/dialect/v4.
  4. The scalar forms (MIN(a, b), MAX(a, b), GREATEST, LEAST) use the same comparison. Their NULL rule is not part of this step (GREATEST / LEAST (scalar MAX / MIN) give order-dependent results when an argument is NULL #2583).

What stays the same

  • MIN, MAX and SUM over numbers.
  • With alasql/dialect/v4 every aggregate gives the 4.x result.
  • In the default, MIN and MAX over text and DATE columns return a value, and SUM over no rows returns NULL.

Done when

  • A new test file named after this issue covers MIN and MAX over text, over a DATE column, over Date objects, over a mix of numbers and text, and with NULLs before a 0. It covers SUM and TOTAL over no rows and over only NULLs, each also with alasql/dialect/v4.
  • The GROUP BY matrix in the dialect test against SQLite covers these aggregates over text and over no rows, and their expected differences are removed.
  • The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.

Fixes

Fixes #609, #2481.

Depends on

#2660, #3170, #2588, #3171

Activity

  1. added this to the v5.0.0 milestone on Nov 9, 2026
  2. changed the title [-]Review #2156 for code improvements: MIN and MAX accept types from a list per aggregate[/-] [+]MIN and MAX compare through the comparison module, and SUM over no rows is NULL, with a dialect keeping the old rules[/+] on Nov 26, 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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions