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
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
SELECT CASE WHEN shipped_at IS NULL THEN 'pending' WHEN shipped_at IS NULL THEN 'in transit' ELSE 'delivered'END FROM orders;[tool.skylos.sql]dialect = "postgresql"What Skylos leaves alone
SELECT CASE WHEN shipped_at IS NULL THEN 'pending' WHEN delivered_at IS NULL THEN 'in transit' ELSE 'delivered'END FROM orders;Why Skylos leaves it alone
The second arm covers a shipped order that has not yet arrived.
How Skylos decides
- 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.
- 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.
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.