Skip to main content
Branch on conditions, substitute defaults for missing values, and convert between types when building segment definitions. Unless noted otherwise, each function works on both the PostgreSQL and ClickHouse backends.

CASE WHEN

Evaluates conditions in order and returns the value for the first one that is true, with an optional ELSE fallback. Syntax
Arguments
  • condition — a boolean expression tested in order.
  • result — value returned when its WHEN condition is the first true one.
  • default — value returned when no condition matches; NULL if the ELSE branch is omitted.
Returned value
  • The result of the first matching branch, otherwise the ELSE value (or NULL).
Example
Bucket users by lifetime value

coalesce

Returns the first non-NULL argument, evaluating them left to right. Syntax
Arguments
  • expr1, expr2, ... — one or more expressions of compatible types.
Returned value
  • The first non-NULL argument, or NULL if every argument is NULL.
Example
Fall back to a default plan

if

Returns one of two values depending on a boolean condition. Syntax
Arguments
  • condition — a boolean expression.
  • then — value returned when condition is true.
  • else — value returned when condition is false.
Returned value
  • then when condition is true, otherwise else.
Example
Flag high-value users

multiIf

Evaluates condition/value pairs in order and returns the value for the first true condition, with a final default. A compact alternative to CASE WHEN. Syntax
Arguments
  • condition1, condition2, ... — boolean expressions tested in order.
  • value1, value2, ... — value returned for the corresponding condition.
  • default — value returned when no condition is true.
Returned value
  • The value of the first true condition, otherwise default.
Example
Bucket users by lifetime value

transform

Maps a value against parallel “from” and “to” arrays — an inline lookup table. If the value isn’t found, it returns the default (4-argument form) or the value unchanged (3-argument form). Syntax
Arguments
  • expr — the value to map.
  • from_array — array of source values to match expr against.
  • to_array — array of replacement values, matched by position to from_array.
  • default — value returned when expr matches nothing. Omit it (3-argument form) to return expr unchanged on no match.
Returned value
  • The mapped value, or the default / original value when there’s no match.
Example
Users whose plan maps to a paid tier

nullif

Returns NULL when the two arguments are equal, otherwise returns the first argument. Handy for guarding against division by zero or blanking sentinel values. Syntax
Arguments
  • expr1 — the value returned when the two differ.
  • expr2 — the value compared against expr1.
Returned value
  • NULL when expr1 = expr2, otherwise expr1.
Example
Blank out empty plan strings

:: (cast)

Converts a value from one data type to another. :: is the PostgreSQL-style cast operator; CAST(expr AS type) is the equivalent standard SQL form. Syntax
Arguments
  • expr — the value or expression to convert.
  • type — the target data type, for example DOUBLE PRECISION, INTEGER, or TEXT.
Returned value
  • expr converted to type. Returns NULL when expr is NULL.
Example
Cast JSON properties with both forms

ifnull

Returns the first argument if it is not NULL, otherwise the second. Equivalent to a two-argument coalesce. Syntax
Arguments
  • expr — the expression to test.
  • alt — value returned when expr is NULL.
Returned value
  • expr when it is non-NULL, otherwise alt.
Example
Default a missing plan to free

assumeNotNull

Tells the query engine that an expression is never NULL, turning a nullable type into a non-nullable one. Use it only when you are certain no NULLs are present. Syntax
Arguments
  • expr — a possibly-nullable expression.
Returned value
  • The same value with a non-nullable type. Behavior is undefined if expr is actually NULL.
Example
Treat a filtered column as non-null

toNullable

Converts an expression to a nullable type without changing its value. The inverse of assumeNotNull. Syntax
Arguments
  • expr — any expression.
Returned value
  • The same value carried in a nullable type.
Example
Make a column nullable

xor

Logical exclusive OR. Returns true when exactly one of the two boolean arguments is true. Syntax
Arguments
  • a — a boolean expression.
  • b — a boolean expression.
Returned value
  • Boolean true when exactly one of a and b is true, otherwise false.
Example
Users on trial or with spend, but not both