execute_create_function

Function execute_create_function 

Source
pub fn execute_create_function(
    stmt: &CreateFunctionStmt,
    db: &mut Database,
) -> Result<(), ExecutorError>
Expand description

Execute CREATE FUNCTION statement (SQL:1999 Feature P001)

Creates a user-defined function in the database catalog with optional characteristics.

Functions differ from procedures:

  • Functions RETURN a value and can be used in expressions
  • Functions are read-only (cannot modify database tables)
  • Functions only support IN parameters (no OUT/INOUT)
  • Functions have recursion depth limiting (max 100 levels)

§Characteristics (Phase 6)

  • DETERMINISTIC: Indicates same input always produces same output
  • SQL SECURITY: DEFINER (default) or INVOKER
  • COMMENT: Documentation string
  • LANGUAGE: SQL (only supported language)

§Examples

Simple function with RETURN expression:

CREATE FUNCTION add_ten(x INT) RETURNS INT
  DETERMINISTIC
  RETURN x + 10;

Complex function with BEGIN…END body:

CREATE FUNCTION factorial(n INT) RETURNS INT
  DETERMINISTIC
  COMMENT 'Calculate factorial of n'
BEGIN
  DECLARE result INT DEFAULT 1;
  DECLARE i INT DEFAULT 2;

  WHILE i <= n DO
    SET result = result * i;
    SET i = i + 1;
  END WHILE;

  RETURN result;
END;

§Errors

Returns error if:

  • Function name already exists
  • Invalid parameter types or return type
  • Body parsing fails