Skip to content

Standard Library

XTDB provides a rich standard library of predicates and functions, for use in both XTQL and SQL.

The following control structures are available in XTDB:

If

(if <predicate>
  <then-expr>
  <else-expr>)
  • If the predicate evaluates to true, returns the value of then-expr, else returns the value of else-expr.

  • The predicate must return a boolean value. To turn any value into a boolean use the boolean function, which returns false if its argument is false, null or absent; otherwise it returns true.

  • XTQL only

Case / Cond

case tests the result of the test-expr against each of the value-expr`s until a match is found - it then returns the value of the corresponding `result-expr.

(case <test-expr>
  <value-expr> <result-expr>
  ...

  <default-expr>?)
  • If no match is found, and a default-expr is present, it will return the value of that expression.

cond checks the value of each predicate expression in turn, until one is true - it then returns the value of the corresponding result-expr

(cond
  <predicate> <result-expr>
  ...
  <default-expr>?)
  • If none of the predicates return true, and a default-expr is present, it will return the value of that expression.

  • Clojure users: note that there’s no :else required in the default expression.

In SQL, both use cases are covered by the CASE keyword:

CASE <test-expr>
     WHEN <value-expr> THEN <result-expr>
     [ WHEN ... ]
     [ ELSE <default-expr> ]
  END
CASE WHEN <predicate> THEN <result-expr>
     [ WHEN ... ]
     [ ELSE <default-expr> ]
  END

Let

Returns the value of body-expr, with symbol bound to the result of binding-expr.

(let [<symbol> <binding-expr>]
  <body-expr>)
  • Only one symbol/binding pair is permitted.

  • XTQL only

If Some

If binding-expr returns a non-null value, returns the result of then-expr with symbol bound to the result of the binding expression. Otherwise, returns the value of else-expr.

(if-some [<symbol> <binding-expr>]
  <then-expr>
  <else-expr>)
  • XTQL only.

Coalesce / Null If

'Coalesce' returns the first non-null value of its arguments:

(coalesce <expr>*)
COALESCE(<expr>, ...)

'Null if' returns null if expr1 equals expr2; otherwise it returns the value of expr1.

(null-if <expr1> <expr2>)
NULLIF(<expr1>, <expr2>)