Description
Legacy mechanism box.internal.sql_function_create to make some
Lua function available in SQL is forbidden now.
To make some function available in SQL you need to use
box.schema.func.create() mechanism: you need to specify
- function language and language-specific options(e.g. you
are able to define a persistent Lua function) - whether this function is_deterministic or not: deterministic
functions allows to generate more efficient SQL VDBE bytecode
so you better specify it when it is true - the function returns type: a Tarantool type string describes
a type of value returned by function - param_list - a table of Tarantool's types strings desccribe
function argument types - exports - a table of Tarantool's frontends where this function
should be available ('LUA' by default). You need to specify
{'LUA', 'SQL'} to make function available both in SQL requests
and visible in box.func folder
Example:
-- Case1: C function
-- function1.so has int divide() symbol
box.schema.func.create("function1.divide", {language = 'C',
returns = 'number',
param_list = {'number', 'number'},
is_deterministic = true,
exports = {'LUA', 'SQL'}})
box.execute('SELECT "function1.divide"(6, 3)')
- metadata:
- name: '"function1.divide"(6, 3)'
type: number
rows: - [2]
box.schema.func.drop("function1.divide")
- name: '"function1.divide"(6, 3)'
-- Case2: Persistent Lua function
box.schema.func.create("SUMMARIZE", {language = 'LUA',
returns = 'number',
body = 'function (a, b) return a + b end',
param_list = {'number', 'number'},
is_deterministic = true,
exports = {'LUA', 'SQL'}})
box.execute('SELECT summarize(1, 2)')
- metadata:
- name: summarize(1, 2)
type: number
rows: - [3]
box.schema.func.drop("summarize")
- name: summarize(1, 2)
Moreover there is a special predefined Lua function LUA that
allows to evaluate a custom Lua expressions in SQL.
You need to pass a string in form "return ...." to LUA function
that returns more than one value of any type.
Example:
box.execute('SELECT lua('return 1 + 1')')
- metadata:
- name: lua('return 1 + 1')
type: any
rows: - [2]
box.execute('SELECT lua('return box.cfg.memtx_memory')')
- name: lua('return 1 + 1')
- metadata:
- name: lua('return box.cfg.memtx_memory')
type: any
rows: - [268435456]
Requested by @kshcherbatov in tarantool/tarantool@d4a7459.
- name: lua('return box.cfg.memtx_memory')