Skip to main content

SKY-Q505 · Quality, structure and performance

Literal NULL in a PostgreSQL NOT IN filter

Medium

A required filter uses NOT IN to test a column or scalar literal. The list on the right contains a literal NULL.

Languages
PostgreSQL
Quality gate
listed, not counted by the gate (full and diff scans). This PostgreSQL check is report-only.
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

With ordinary equality, a matching value fails the filter. For an unmatched value the comparison evaluates to unknown, so that value fails too.

What Skylos flags

orders.sql · SQLReported on line 2
SELECT id FROM ordersWHERE status NOT IN ('closed', NULL);
pyproject.toml · TOML
[tool.skylos.sql]dialect = "postgresql"

What Skylos leaves alone

orders.sql · SQL
SELECT id FROM ordersWHERE status <> 'closed';

pyproject.toml: same as in the flagged example.

Why Skylos leaves it alone

This filter keeps orders with a known status other than closed.

How Skylos decides

  1. With the PostgreSQL dialect selected, run Skylos with --quality. Skylos checks WHERE, HAVING, join conditions, aggregate FILTER and MERGE conditions.
  2. The predicate must stand alone or be required under AND. OR and NOT branches, projections, list subqueries, explicit casts and computed list items are skipped.

How to fix it

Decide whether to include rows with a missing status. Use IS DISTINCT FROM 'closed' if those rows should also pass.

orders.sql · SQL · the fix, against the flagged file
SELECT id FROM ordersRemoved: WHERE status NOT IN ('closed', NULL);Added: WHERE status <> 'closed';

When it can be wrong

  • A user-defined type can implement different comparison behavior for NULL.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Check the column type and the intended treatment of missing values.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.