Skip to main content

SKY-Q509 · Quality, structure and performance

Unreachable PostgreSQL CASE arm

Low

PostgreSQL selects the first matching WHEN in a CASE expression. An earlier arm already covers a later arm, so the later result cannot be chosen.

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

A repeated condition can hide a missing status or send customers to the wrong reporting group. Check what the later arm was meant to test.

What Skylos flags

orders.sql · SQLReported on line 3
SELECT CASE  WHEN shipped_at IS NULL THEN 'pending'  WHEN shipped_at IS NULL THEN 'in transit'  ELSE 'delivered'END FROM orders;
pyproject.toml · TOML
[tool.skylos.sql]dialect = "postgresql"

What Skylos leaves alone

orders.sql · SQL
SELECT CASE  WHEN shipped_at IS NULL THEN 'pending'  WHEN delivered_at IS NULL THEN 'in transit'  ELSE 'delivered'END FROM orders;

pyproject.toml: same as in the flagged example.

Why Skylos leaves it alone

The second arm covers a shipped order that has not yet arrived.

How Skylos decides

  1. Run with --quality on PostgreSQL SQL files. In a searched CASE, Skylos reports later WHENs after literal TRUE, and repeated IS NULL or IS NOT NULL tests on the same unqualified column. A dotted name can call a computed-field function whose result changes between evaluations.
  2. In a simple CASE, Skylos checks repeated integer or boolean literals when the selector is a matching literal or casts to a known built-in integer or boolean type. It assumes ordinary equality for those types.

How to fix it

Check the intended condition or remove the repeated arm.

orders.sql · SQL · the fix, against the flagged file
SELECT CASE  WHEN shipped_at IS NULL THEN 'pending'Removed:   WHEN shipped_at IS NULL THEN 'in transit'Added:   WHEN delivered_at IS NULL THEN 'in transit'  ELSE 'delivered'END FROM orders;

When it can be wrong

  • A template may replace a literal TRUE before the SQL reaches the server.
  • A custom equality operator can affect simple CASE comparisons.

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

  • Review the final SQL and any custom operators used by the query.

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