Skip to content

Commit

Permalink
box: introduce opts.is_multikey function option
Browse files Browse the repository at this point in the history
Needed for #1260

@TarantoolBot document
Title: A new option is_multikey for function definition

A new option is_multikey allows to specify wether new function
returns multiple values packed in a table object. This is a
native way to define multikey func_index.
  • Loading branch information
kshcherbatov committed Jul 26, 2019
1 parent abd5cce commit 282697b
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/box/alter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,7 @@ func_def_new_from_tuple(struct tuple *tuple)
tnt_raise(ClientError, ER_CREATE_FUNCTION,
tt_cstr(name, name_len), "function id is too big");
}
func_opts_create(&def->opts);
memcpy(def->name, name, name_len);
def->name[name_len] = '\0';
def->name_len = name_len;
Expand Down Expand Up @@ -2750,6 +2751,11 @@ func_def_new_from_tuple(struct tuple *tuple)
}
}
def->param_count = argc;
const char *opts = tuple_field(tuple, BOX_FUNC_FIELD_OPTS);
if (opts_decode(&def->opts, func_opts_reg, &opts,
ER_WRONG_SPACE_OPTIONS, BOX_FUNC_FIELD_OPTS,
NULL) != 0)
diag_raise();
} else {
def->is_deterministic = false;
def->is_sandboxed = false;
Expand Down
19 changes: 18 additions & 1 deletion src/box/func_def.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* SUCH DAMAGE.
*/
#include "func_def.h"
#include "opt_def.h"
#include "string.h"
#include "diag.h"
#include "error.h"
Expand All @@ -37,6 +38,22 @@ const char *func_language_strs[] = {"LUA", "C", "SQL", "SQL_BUILTIN"};

const char *func_aggregate_strs[] = {"none", "group"};

const struct func_opts func_opts_default = {
/* .is_multikey = */ false,
};

const struct opt_def func_opts_reg[] = {
OPT_DEF("is_multikey", OPT_BOOL, struct func_opts, is_multikey),
};

int
func_opts_cmp(struct func_opts *o1, struct func_opts *o2)
{
if (o1->is_multikey != o2->is_multikey)
return o1->is_multikey - o2->is_multikey;
return 0;
}

int
func_def_cmp(struct func_def *def1, struct func_def *def2)
{
Expand Down Expand Up @@ -70,7 +87,7 @@ func_def_cmp(struct func_def *def1, struct func_def *def2)
return def1->comment - def2->comment;
if (def1->comment != NULL && strcmp(def1->comment, def2->comment) != 0)
return strcmp(def1->comment, def2->comment);
return 0;
return func_opts_cmp(&def1->opts, &def2->opts);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/box/func_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "trivia/util.h"
#include "field_def.h"
#include "opt_def.h"
#include <stdbool.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -60,6 +61,25 @@ enum func_aggregate {

extern const char *func_aggregate_strs[];

/** Function options. */
struct func_opts {
/**
* True when a function returns multiple values
* packed in array.
*/
bool is_multikey;
};

extern const struct func_opts func_opts_default;
extern const struct opt_def func_opts_reg[];

/** Create index options using default values. */
static inline void
func_opts_create(struct func_opts *opts)
{
*opts = func_opts_default;
}

/**
* Definition of a function. Function body is not stored
* or replicated (yet).
Expand Down Expand Up @@ -109,6 +129,8 @@ struct func_def {
};
uint8_t all;
} exports;
/** The function options. */
struct func_opts opts;
/** Function name. */
char name[0];
};
Expand Down
3 changes: 3 additions & 0 deletions src/box/lua/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,9 @@ lbox_func_new(struct lua_State *L, struct func *func)
lua_pushstring(L, "is_deterministic");
lua_pushboolean(L, func->def->is_deterministic);
lua_settable(L, top);
lua_pushstring(L, "is_multikey");
lua_pushboolean(L, func->def->opts.is_multikey);
lua_settable(L, top);
lua_pushstring(L, "is_sandboxed");
if (func->def->body != NULL)
lua_pushboolean(L, func->def->is_sandboxed);
Expand Down
3 changes: 2 additions & 1 deletion src/box/lua/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,8 @@ box.schema.func.create = function(name, opts)
if_not_exists = 'boolean',
language = 'string', body = 'string',
is_deterministic = 'boolean',
is_sandboxed = 'boolean', comment = 'string' })
is_sandboxed = 'boolean', comment = 'string',
opts = 'table' })
local _func = box.space[box.schema.FUNC_ID]
local _vfunc = box.space[box.schema.VFUNC_ID]
local func = _vfunc.index.name:get{name}
Expand Down
14 changes: 14 additions & 0 deletions test/box/function1.result
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ box.func["function1.args"]
sql: false
id: 66
setuid: false
is_multikey: false
is_deterministic: false
name: function1.args
language: C
Expand Down Expand Up @@ -418,6 +419,7 @@ func
sql: false
id: 66
setuid: false
is_multikey: false
is_deterministic: false
comment: Divide two values
name: divide
Expand Down Expand Up @@ -489,6 +491,7 @@ func
sql: false
id: 66
setuid: false
is_multikey: false
is_deterministic: false
name: function1.divide
language: C
Expand Down Expand Up @@ -822,3 +825,14 @@ box.func.LUA:call({"return 1 + 1"})
---
- 2
...
-- Introduce function options
box.schema.func.create('test', {body = "function(tuple) return tuple end", is_deterministic = true, opts = {is_multikey = true}})
---
...
box.func['test'].is_multikey == true
---
- true
...
box.func['test']:drop()
---
...
5 changes: 5 additions & 0 deletions test/box/function1.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,8 @@ for _, v in pairs(sql_builtin_list) do ok = ok and (box.space._func.index.name:g
ok == true

box.func.LUA:call({"return 1 + 1"})

-- Introduce function options
box.schema.func.create('test', {body = "function(tuple) return tuple end", is_deterministic = true, opts = {is_multikey = true}})
box.func['test'].is_multikey == true
box.func['test']:drop()

0 comments on commit 282697b

Please sign in to comment.