Skip to content

fix: GREATEST/LEAST should ignore null arguments - #478

Open
spokodev wants to merge 1 commit into
oguimbal:masterfrom
spokodev:fix/greatest-least-null-args
Open

fix: GREATEST/LEAST should ignore null arguments#478
spokodev wants to merge 1 commit into
oguimbal:masterfrom
spokodev:fix/greatest-least-null-args

Conversation

@spokodev

@spokodev spokodev commented Aug 3, 2026

Copy link
Copy Markdown

Problem

GREATEST and LEAST return null as soon as any single argument is null:

select greatest(1, null, 3);  -- pg-mem: null
select least(2, null, 1);     -- pg-mem: null

Postgres ignores null arguments in these functions, and returns null only when every argument is null:

select greatest(1, null, 3);       -- postgres: 3
select least(2, null, 1);          -- postgres: 1
select greatest(null::int, null);  -- postgres: null

Ref: Postgres conditional expressions (GREATEST/LEAST) - null values in the argument list are ignored, and the result is null only if all arguments are null.

Cause

Both functions were declared without allowNullArguments, so a null argument short-circuited the whole call to null before the implementation ran.

Fix

Set allowNullArguments: true and filter nulls inside the implementation, returning null only when all arguments are null. Unit tests added in function-calls.spec.ts.

GREATEST and LEAST were declared without allowNullArguments, so any null
argument short-circuited the whole call to null. Postgres ignores null
arguments and only returns null when every argument is null.

  select greatest(1, null, 3);  -- was null, postgres returns 3
  select least(2, null, 1);     -- was null, postgres returns 1
@spokodev
spokodev force-pushed the fix/greatest-least-null-args branch from 2a09f88 to 692de32 Compare August 3, 2026 20:25
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.

1 participant