Skip to content

The box.internal.sql_function_create is forbidden #879

Closed
@TarantoolBot

Description

@TarantoolBot

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

  1. function language and language-specific options(e.g. you
    are able to define a persistent Lua function)
  2. 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
  3. the function returns type: a Tarantool type string describes
    a type of value returned by function
  4. param_list - a table of Tarantool's types strings desccribe
    function argument types
  5. 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")

-- 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")

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')')
  • metadata:

Metadata

Metadata

Assignees

Labels

featureA new functionalitysql[location] SQL manual

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions