Skip to content

fix(postgres): handle null/undefined values in $in/$nin queries - #11152

Merged
ArtyomSavchenko merged 1 commit into
hcengineering:developfrom
MichaelUray:fix/postgres-in-null-undefined
Oct 19, 2026
Merged

ArtyomSavchenko merged 1 commit into
hcengineering:developfrom
MichaelUray:fix/postgres-in-null-undefined

Conversation

@MichaelUray

Copy link
Copy Markdown
Contributor

Fixes #9697

Problem and change

A query like { dueDate: { $in: [undefined, null] } } produced invalid SQL
on Postgres/CockroachDB: "data#>>'{dueDate}' = ANY(ARRAY[]::jsonb[])", which
fails with "unsupported comparison operator: = ANY <jsonb[]>".
inferType() only looked at the first two array elements, so an array of
null-ish values was typed as jsonb[], and the $in/$nin branches still
emitted an empty ANY/ALL comparison after the null values were filtered out.

inferType() now uses the first non-null element of an array. $in/$nin
treat undefined like null (as the Mongo adapter does): when only null-ish
values are given they translate to plain IS NULL / IS NOT NULL, otherwise
the remaining values are compared and combined with the null check.

Tests

New cases in foundations/server/packages/postgres/src/__tests__/conversion.spec.ts and utils.spec.ts; they fail without the fix. Checked on a self-hosted CockroachDB-backed instance: $in: [null], $in: [undefined, null], $nin: [null] and mixed lists return the expected issues, and the Due date filters (Today, Not specified, exact date) load without the "unsupported comparison operator" error.

A query like { dueDate: { $in: [undefined, null] } } produced invalid SQL
on Postgres/CockroachDB: "data#>>'{dueDate}' = ANY(ARRAY[]::jsonb[])", which
fails with "unsupported comparison operator: <string> = ANY <jsonb[]>".
inferType() only looked at the first two array elements, so an array of
null-ish values was typed as jsonb[], and the $in/$nin branches still
emitted an empty ANY/ALL comparison after the null values were filtered out.

inferType() now uses the first non-null element of an array. $in/$nin
treat undefined like null (as the Mongo adapter does): when only null-ish
values are given they translate to plain IS NULL / IS NOT NULL, otherwise
the remaining values are compared and combined with the null check.

Fixes hcengineering#9697

Signed-off-by: Michael Uray <michaeluray@users.noreply.github.com>
@ArtyomSavchenko
ArtyomSavchenko merged commit 949c85f into hcengineering:develop Oct 19, 2026
13 checks passed
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.

Cockroach adapter uses wrong type for dueDate

2 participants