sql: support user-defined functions in SQL
Closes #2200 Closes #4113 Closes #2233 @TarantoolBot document Title: The box.internal.sql_function_create is forbidden 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: - name: lua('return box.cfg.memtx_memory') type: any rows: - [268435456]
Showing
- src/box/call.c 1 addition, 0 deletionssrc/box/call.c
- src/box/execute.c 1 addition, 0 deletionssrc/box/execute.c
- src/box/lua/call.c 4 additions, 0 deletionssrc/box/lua/call.c
- src/box/lua/schema.lua 2 additions, 1 deletionsrc/box/lua/schema.lua
- src/box/port.c 4 additions, 0 deletionssrc/box/port.c
- src/box/port.h 17 additions, 0 deletionssrc/box/port.h
- src/box/sql/expr.c 3 additions, 3 deletionssrc/box/sql/expr.c
- src/box/sql/func.c 278 additions, 0 deletionssrc/box/sql/func.c
- src/box/sql/vdbe.c 43 additions, 0 deletionssrc/box/sql/vdbe.c
- src/lib/core/port.h 15 additions, 0 deletionssrc/lib/core/port.h
- test/box/function1.result 176 additions, 0 deletionstest/box/function1.result
- test/box/function1.test.lua 63 additions, 0 deletionstest/box/function1.test.lua
- test/sql-tap/alias.test.lua 5 additions, 6 deletionstest/sql-tap/alias.test.lua
- test/sql-tap/check.test.lua 7 additions, 4 deletionstest/sql-tap/check.test.lua
- test/sql-tap/func5.test.lua 21 additions, 6 deletionstest/sql-tap/func5.test.lua
- test/sql-tap/lua_sql.test.lua 64 additions, 57 deletionstest/sql-tap/lua_sql.test.lua
- test/sql-tap/subquery.test.lua 13 additions, 8 deletionstest/sql-tap/subquery.test.lua
- test/sql-tap/trigger9.test.lua 7 additions, 1 deletiontest/sql-tap/trigger9.test.lua
- test/sql/errinj.result 0 additions, 26 deletionstest/sql/errinj.result
- test/sql/errinj.test.lua 0 additions, 10 deletionstest/sql/errinj.test.lua
Loading
Please register or sign in to comment