Module table | Tarantool
Check out the new release policy

Module table

The table module has everything in the standard Lua table library, and some Tarantool extensions.

Write table to see the list of functions:

  • clear (LuaJIT extension = erase all elements)
  • concat (concatenate)
  • copy (make a copy of an array)
  • deepcopy (see the description below)
  • foreach
  • foreachi
  • getn (get the number of elements in an array)
  • insert (insert an element into an array)
  • maxn (get the largest index)
  • move (move elements between tables)
  • new (LuaJIT extension = return a new table with pre-allocated elements)
  • remove (remove an element from an array)
  • sort (sort the elements of an array)

In this section we only discuss the additional function that the Tarantool developers have added: deepcopy.


Return a “deep” copy of the table – a copy which follows nested structures to any depth and does not depend on pointers, it copies the contents.

  • input-table – (table) the table to copy

the copy of the table




tarantool> input_table = {1,{'a','b'}}

tarantool> output_table = table.deepcopy(input_table)

tarantool> output_table
- - 1
  - - a
    - b
table.sort(input-table[, comparison-function])

Put the input-table contents in sorted order.

The basic Lua table.sort has a default comparison function: function (a, b) return a < b end.

That is efficient and standard. However, sometimes Tarantool users will want an equivalent to table.sort which has any of these features:

  1. If the table contains nils, except nils at the end, the results must still be correct. That is not the case with the default tarantool_sort, and it cannot be fixed by making a comparison that checks whether a and b are nil. (Before trying certain Internet suggestions, test with {1, nil, 2, -1, 44, 1e308, nil, 2, nil, nil, 0}.
  2. If strings are to be sorted in a language-aware way, there must be a parameter for collation.
  3. If the table has a mix of types, then they must be sorted as booleans, then numbers, then strings, then byte arrays.

Since all those features are available in Tarantool spaces, the solution for Tarantool is simple: make a temporary Tarantool space, put the table contents into it, retrieve the tuples from it in order, and overwrite the table.

Here then is tarantool_sort() which does the same thing as table.sort but has those extra features. It is not fast and it requires a database privilege, so it should only be used if the extra features are necessary.


function tarantool_sort(input_table, collation)
    local c = collation or 'binary'
    local tmp_name = 'Temporary_for_tarantool_sort'
    pcall(function()[tmp_name]:drop() end), {temporary = true})[tmp_name]:create_index('I')[tmp_name]:create_index('I2',
                                     {unique = false,
                                      parts={{2, 'scalar',
                                              collation = c,
                                              is_nullable = true}}})
    for i = 1, table.maxn(input_table) do[tmp_name]:insert{i, input_table[i]}
    local t =[tmp_name].index.I2:select()
    for i = 1, table.maxn(input_table) do
        input_table[i] = t[i][2]

For example, suppose table t = {1, 'A', -88.3, nil, true, 'b', 'B', nil, 'À'}.

After tarantool_sort(t, 'unicode_ci') t contains {nil, nil, true, -88.3, 1, 'A', 'À', 'b', 'B'}.

Found what you were looking for?