CASE WHEN

All functions > CONDITIONAL > CASE WHEN

Returns the result of the first condition that evaluates to true

Syntax

CASE [ { FIRST | UNIQUE | MULTI } [ subject | ( subject, subject ... ) ] ] WHEN condition_or_cells THEN result { WHEN condition_or_cells THEN result ... } [ ELSE default ] END

Notes

  • Write this as CASE WHEN … THEN … [ELSE …] END in FeatureQL (same shape as familiar SQL). Conditions are evaluated in order; the first one that is TRUE selects its THEN value.
  • If no WHEN is TRUE and ELSE is omitted, the result is NULL (typed with the common type of the branch values—use NULL::<type> or typed NULL(...) where inference needs a type).
  • Evaluation short-circuits: once a condition is TRUE, later branches are not evaluated.
  • Every THEN expression and the ELSE expression (if present) must be type-compatible.
  • Decision tables: a policy keyword after CASE makes the compiler check the arms. UNIQUE: arms never overlap. FIRST: the first matching arm wins and every arm can win. MULTI: returns ARRAY(T) of every matching value, in arm order. Without ELSE, UNIQUE and FIRST must cover every input, NULL included.
  • Grid form: after the policy, name the inputs once (CASE UNIQUE (age, country)) and write one cell per input in each arm. In a cell, _ alone matches any value, a condition on _ tests that input (_ >= 18), and a constant means equality ('FR').
  • Under a policy, each condition compares one input with constants. See Decision tables for the checks, the allowed conditions and every error and warning.
  • FIRST, UNIQUE and MULTI are keywords only directly after CASE. For a simple CASE on a feature with one of those names, write CASE (first) WHEN ….

Examples

Searched CASE

FeatureQL
RETURN
    -- Second WHEN is first true branch
    f1 := CASE
        WHEN FALSE THEN 1
        WHEN TRUE THEN 2
        WHEN FALSE THEN 3
        ELSE 999
    END,
    -- No WHEN true, ELSE applies
    f2 := CASE
        WHEN FALSE THEN 1
        WHEN FALSE THEN 2
        ELSE 999
    END,
    -- First true branch short-circuits
    f3 := CASE
        WHEN TRUE THEN 1
        WHEN TRUE THEN 2
        ELSE 999
    END,
    -- No match and no ELSE → NULL
    f4 := CASE
        WHEN FALSE THEN 1
        WHEN FALSE THEN 2
    END
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINT
29991null

String results

FeatureQL
RETURN
    -- Expression in WHEN
    f1 := CASE
        WHEN 1 = 1 THEN 'yes'
        ELSE 'no'
    END,
    -- First matching inequality
    f2 := CASE
        WHEN 10 > 5 THEN 'big'
        WHEN 10 > 20 THEN 'huge'
        ELSE 'small'
    END,
    -- Single WHEN with ELSE
    f3 := CASE
        WHEN TRUE THEN 42
        ELSE 0
    END
;
Result
f1 VARCHARf2 VARCHARf3 BIGINT
yesbig42

Decision tables

FeatureQL
RETURN
    -- UNIQUE: arms proven disjoint
    f1 := CASE UNIQUE 25
        WHEN _ < 18  THEN 'minor'
        WHEN _ >= 18 THEN 'adult'
        ELSE 'unknown'
    END,
    -- FIRST grid: the exception comes first
    f2 := CASE FIRST ('FR', 16)
        WHEN ('FR', _ < 18) THEN 'blocked'
        WHEN ('FR', _     ) THEN 'domestic'
        ELSE 'international'
    END,
    -- MULTI: every matching value, in arm order
    f3 := CASE MULTI 5000
        WHEN _ >= 100  THEN 'large'
        WHEN _ >= 1000 THEN 'huge'
        ELSE 'none'
    END,
    -- MULTI with no match returns ARRAY(<else>)
    f4 := CASE MULTI 5
        WHEN _ >= 100  THEN 'large'
        WHEN _ >= 1000 THEN 'huge'
        ELSE 'none'
    END
;
Result
f1 VARCHARf2 VARCHARf3 ARRAYf4 ARRAY
adultblocked[large, huge][none]