Skip to content

rule: W014 case-without-else - warn on CASE expression without ELSE #4

@Pawansingh3889

Description

@Pawansingh3889

CASE WHEN ... THEN ... END without an ELSE branch returns NULL for
unmatched rows. Often unintended - the author thought the WHEN
conditions were exhaustive but they aren't, or the downstream code
can't handle NULL.

Should fail

SELECT CASE
  WHEN status = 'paid' THEN 1
  WHEN status = 'pending' THEN 0
END AS paid_flag
FROM orders;

Should pass

SELECT CASE
  WHEN status = 'paid' THEN 1
  WHEN status = 'pending' THEN 0
  ELSE NULL
END AS paid_flag
FROM orders;

Implementation hints

  • Multi-line rule.
  • Detect CASE ... END without ELSE between.
  • Test with nested CASE expressions - make sure outer CASE without ELSE
    still triggers when inner CASE has ELSE.
  • Severity: warning.

Estimated LOC: ~35 code + ~25 test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions