Skip to content
Snippets Groups Projects
  • Kirill Shcherbatov's avatar
    4177fe17
    box: introduce functional indexes in memxtx · 4177fe17
    Kirill Shcherbatov authored
    Closes #1260
    
    @TarantoolBot document
    Title: introduce func indexes in memtx
    Now you can define a func_index using a registered persistent
    function.
    
    There are restrictions for function and key definition for
    a functional index:
     - the referenced function must be persistent, deterministic
       and must return a scalar type or an array.
     - you must define key parts which describe the function return value
     - the function must return data which types match the
       defined key parts
     - the function may return multiple keys; this would be a multikey
       functional index; each key entry is indexed separately;
     - for multikey functional indexes, the key definition should
       start with part 1 and cover all returned key parts
     - key parts can't use JSON paths.
     - the function used for the functional index can not access tuple
       fields by name, only by index.
    
    Functional index can't be primary.
    It is not possible to change the used function after a functional
    index is defined on it. The index must be dropped first.
    
    Each key returned by functional index function (even when it is a
    single scalar) must be returned as a table i.e. {1} and must
    match the key definition.
    
    To define a multikey functional index, create a function with
    opts = {is_multikey = true} and return a table of keys.
    
    Example:
    s = box.schema.space.create('withdata')
    s:format({{name = 'name', type = 'string'},
              {name = 'address', type = 'string'}})
    pk = s:create_index('name', {parts = {1, 'string'}})
    lua_code = [[function(tuple)
                    local address = string.split(tuple[2])
                    local ret = {}
                    for _, v in pairs(address) do
    			table.insert(ret, {utf8.upper(v)})
    		end
                    return ret
                 end]]
    box.schema.func.create('address', {body = lua_code,
                           is_deterministic = true, is_sandboxed = true,
                           opts = {is_multikey = true}})
    idx = s:create_index('addr', {unique = false,
                         func = 'address',
                         parts = {{1, 'string', collation = 'unicode_ci'}}})
    s:insert({"James", "SIS Building Lambeth London UK"})
    s:insert({"Sherlock", "221B Baker St Marylebone London NW1 6XE UK"})
    idx:select('Uk')
    ---
    - - ['James', 'SIS Building Lambeth London UK']
      - ['Sherlock', '221B Baker St Marylebone London NW1 6XE UK']
    ...
    4177fe17
    History
    box: introduce functional indexes in memxtx
    Kirill Shcherbatov authored
    Closes #1260
    
    @TarantoolBot document
    Title: introduce func indexes in memtx
    Now you can define a func_index using a registered persistent
    function.
    
    There are restrictions for function and key definition for
    a functional index:
     - the referenced function must be persistent, deterministic
       and must return a scalar type or an array.
     - you must define key parts which describe the function return value
     - the function must return data which types match the
       defined key parts
     - the function may return multiple keys; this would be a multikey
       functional index; each key entry is indexed separately;
     - for multikey functional indexes, the key definition should
       start with part 1 and cover all returned key parts
     - key parts can't use JSON paths.
     - the function used for the functional index can not access tuple
       fields by name, only by index.
    
    Functional index can't be primary.
    It is not possible to change the used function after a functional
    index is defined on it. The index must be dropped first.
    
    Each key returned by functional index function (even when it is a
    single scalar) must be returned as a table i.e. {1} and must
    match the key definition.
    
    To define a multikey functional index, create a function with
    opts = {is_multikey = true} and return a table of keys.
    
    Example:
    s = box.schema.space.create('withdata')
    s:format({{name = 'name', type = 'string'},
              {name = 'address', type = 'string'}})
    pk = s:create_index('name', {parts = {1, 'string'}})
    lua_code = [[function(tuple)
                    local address = string.split(tuple[2])
                    local ret = {}
                    for _, v in pairs(address) do
    			table.insert(ret, {utf8.upper(v)})
    		end
                    return ret
                 end]]
    box.schema.func.create('address', {body = lua_code,
                           is_deterministic = true, is_sandboxed = true,
                           opts = {is_multikey = true}})
    idx = s:create_index('addr', {unique = false,
                         func = 'address',
                         parts = {{1, 'string', collation = 'unicode_ci'}}})
    s:insert({"James", "SIS Building Lambeth London UK"})
    s:insert({"Sherlock", "221B Baker St Marylebone London NW1 6XE UK"})
    idx:select('Uk')
    ---
    - - ['James', 'SIS Building Lambeth London UK']
      - ['Sherlock', '221B Baker St Marylebone London NW1 6XE UK']
    ...