Skip to content

Fix NULL handling in VAR, STDEV, VARP, STDEVP, STD and STDDEV (part of #2481) - #2581

Open
joshuablac wants to merge 1 commit into
AlaSQL:developfrom
joshuablac:fix/variance-nulls-2481
Open

joshuablac wants to merge 1 commit into
AlaSQL:developfrom
joshuablac:fix/variance-nulls-2481

Conversation

@joshuablac

Copy link
Copy Markdown

Relates to #2481. This fixes its VAR and VARP sections and the duplicate STDEV definition. The GREATEST/LEAST, window MIN/MAX and QUART sections are not touched; #2479 and #2480 already cover MIN/MAX.

Problem

The variance aggregates don't skip NULL values:

[1, 2, 3, 4] plus… Aggregate develop With this PR
a NULL VARP / STDEVP / STD / STDDEV 2 / 1.414 (NULL counted as 0) 1.25 / 1.118
a row without the column (undefined) all six aggregates undefined (NaN) same as without the row
(only NULLs) all six aggregates 0 undefined (NULL)
(a single value 5) VAR / STDEV 0 undefined (NULL)

VAR and STDEV already skipped null, but not undefined, which is how AlaSQL usually represents a missing value. VARP skipped neither.

Fix

In src/55functions.js:

  • VAR and VARP skip NULL values (null, undefined, or NaN, the same values the operator null check treats as NULL). STDEV, STDEVP, STD and STDDEV delegate to them.
  • With nothing to aggregate they return undefined (NULL) instead of 0, like AVG, and like VAR_POP / VAR_SAMP in SQL.
  • The sample variance (VAR, STDEV) is NULL for fewer than two values, as SQL's VAR_SAMP / STDDEV_SAMP are. The population variance of a single value stays 0.
  • STDEV was defined twice with identical bodies; the duplicate is removed.

Behavior changes to note:

  • VAR / STDEV of a single value goes from 0 to undefined.
  • All six aggregates over all-NULL input go from 0 to undefined.

No existing test depended on the old values.

Tests

  • Added test/test2481.js:
    • results without NULL
    • NULL and missing values first and last
    • only NULLs
    • a single value
    • per-group results
  • 5 of the 6 fail on develop and all pass with this change. The "without NULL" case passes either way and guards the formulas.
  • Full suite (bun test, same command as yarn test-only): 2255 pass, 213 skip. The only failures are the 2 CLI tests in test2149.js, which fail the same way on unmodified develop in my local setup (a space in my checkout path, and the version-injection step of build.sh not running).

This fix was written with AI assistance (Claude Code) and verified locally as described above.

🤖 Generated with Claude Code

…SQL#2481

VARP (and STDEVP, STD, STDDEV through it) counted NULL as 0, so
VARP over [1, 2, 3, 4, NULL] gave 2 instead of 1.25. VAR and STDEV only
skipped null, so a row with a missing value (undefined) turned every
variance aggregate into NULL. An all-NULL group gave 0.

All six aggregates now skip NULL values (null, undefined or NaN) and
return NULL (undefined) when there is nothing to aggregate. The sample
variance (VAR, STDEV) is NULL for fewer than two values, as in SQL. The
duplicate definition of STDEV is removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 12, 2026 16:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants