Skip to content

Expression Engine

Built-in predicates and functions.

The expression engine evaluates the predicates and functions that appear inside Datalog :where clauses. It is a row-at-a-time evaluator over a tree of expressions built from variables, literals and supported predicates and functions.

There are two ways to use an expression in a clause:

  • Predicates — a boolean expression that filters rows.
    [(< ?age 30)]
  • Function binding — an expression whose result is bound to a new variable.
    [(+ ?x 1) ?y]
    [(upper ?name) ?upper-name]

Predicates return a boolean and are used as filters.

PredicateSyntaxArgument types
<(< a b)any comparable, same kind
<=(<= a b)any comparable, same kind
>(> a b)any comparable, same kind
>=(>= a b)any comparable, same kind
=(= a b)any
not=, !=(not= a b)any
not(not x)boolean
regexp_like(regexp_like subject "pattern")string, string literal pattern

regexp_like uses Rust regular expression syntax. Its pattern must be a string literal and is compiled when the query is planned. It is only available as a predicate, not as a function binding.

Functions return a value and are used in a function binding to introduce a new variable.

FunctionSyntaxArgument typesResult
+(+ a b)long/doublelong/double
-(- a b)long/doublelong/double
*(* a b)long/doublelong/double
/, quot(/ a b)long/doublelong/double
mod(mod a b)long/doublelong/double
abs(abs x)long/doublelong/double

Notes:

  • Integer arithmetic uses checked operations — overflow makes the row drop out.
  • Division or modulo by zero is treated as no result rather than an error.
  • Mixed long/double arithmetic promotes both sides to double.
FunctionSyntaxArgument typesResult
concat(concat a b)string, stringstring
upper(upper s)stringstring
lower(lower s)stringstring
strlen(strlen s)stringlong
str(str x)long, bigint, double, float, boolean, string, uuidstring

Notes:

  • str falls back to a debug-style representation for any argument type not listed above.
FunctionSyntaxArgument typeResult
year(year t)instantlong
month(month t)instantlong
FunctionSyntaxArgument typesResult
if(if condition then else)boolean, any, anyselected branch

if evaluates to the then expression only when its condition produces boolean true; otherwise it evaluates to the else expression.

  • Expressions evaluate against a tuple’s variable bindings. A type mismatch, or an arithmetic failure (overflow, divide-by-zero) or generally an argument that is nonsensical for a predicate or function all produce “no result”. The row is excluded from a predicate and produces no binding from a function.
  • Predicate evaluation returns true only when the expression yields boolean true; anything else is treated as false.
  • Expressions can be nested freely, for example:
    [(not (< ?x 10))]
    [(+ (* ?x 2) 10) ?y]