CREATE FUNCTION

Creates a user defined function from a lambda expression. The expression must consist of function parameters, constants, operators, or other function calls.

Syntax

  1. CREATE FUNCTION name AS (parameter0, ...) -> expression

A function can have an arbitrary number of parameters.

There are a few restrictions:

  • The name of a function must be unique among user defined and system functions.
  • Recursive functions are not allowed.
  • All variables used by a function must be specified in its parameter list.

If any restriction is violated then an exception is raised.

Example

Query:

  1. CREATE FUNCTION linear_equation AS (x, k, b) -> k*x + b;
  2. SELECT number, linear_equation(number, 2, 1) FROM numbers(3);

Result:

  1. ┌─number─┬─plus(multiply(2, number), 1)─┐
  2. 0 1
  3. 1 3
  4. 2 5
  5. └────────┴──────────────────────────────┘

A conditional function is called in a user defined function in the following query:

  1. CREATE FUNCTION parity_str AS (n) -> if(n % 2, 'odd', 'even');
  2. SELECT number, parity_str(number) FROM numbers(3);

Result:

  1. ┌─number─┬─if(modulo(number, 2), 'odd', 'even')─┐
  2. 0 even
  3. 1 odd
  4. 2 even
  5. └────────┴──────────────────────────────────────┘