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
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
SELECT id FROM ordersWHERE status NOT IN ('closed', NULL);[tool.skylos.sql]dialect = "postgresql"What Skylos leaves alone
SELECT id FROM ordersWHERE status <> 'closed';Why Skylos leaves it alone
This filter keeps orders with a known status other than closed.
How Skylos decides
- With the PostgreSQL dialect selected, run Skylos with --quality. Skylos checks WHERE, HAVING, join conditions, aggregate FILTER and MERGE conditions.
- 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.
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.