diff --git a/src/box/box.lua b/src/box/box.lua index 372c0792f0286776c867107e3022da8d6e028857..1ee6e13af4b6840227597d12436c3f4856c22ad2 100644 --- a/src/box/box.lua +++ b/src/box/box.lua @@ -102,15 +102,23 @@ function box.update(space, key, format, ...) ...)) end --- Assumes that spaceno has a TREE int32 (NUM) primary key +-- Assumes that spaceno has a TREE int32 (NUM) or int64 (NUM64) primary key -- inserts a tuple after getting the next value of the -- primary key and returns it back to the user function box.auto_increment(spaceno, ...) spaceno = tonumber(spaceno) local max_tuple = box.space[spaceno].index[0].idx:max() - local max = -1 + local max = 0 if max_tuple ~= nil then - max = box.unpack('i', max_tuple[0]) + max = max_tuple[0] + local fmt = 'i' + if #max == 8 then fmt = 'l' end + max = box.unpack(fmt, max) + else + -- first time + if box.space[spaceno].index[0].key_field[0].type == "NUM64" then + max = tonumber64(max) + end end return box.insert(spaceno, max + 1, ...) end diff --git a/test/big/lua.result b/test/big/lua.result index e6b2e3261694f82d0316c3e9f109cc6aa3da3178..ec58da48abe225b5093b6dade5eaf3fd62c33643 100644 --- a/test/big/lua.result +++ b/test/big/lua.result @@ -327,7 +327,7 @@ lua box.space[18]:truncate() ... lua box.auto_increment(18, 'a') --- - - 0: {'a'} + - 1: {'a'} ... lua box.insert(18, 5) --- @@ -344,6 +344,28 @@ lua box.auto_increment(18, 'c') lua box.space[18]:truncate() --- ... +lua box.space[25]:truncate() +--- +... +lua box.auto_increment(25, 'a') +--- + - 1: {'a'} +... +lua box.insert(25, tonumber64(5)) +--- + - 5: {} +... +lua box.auto_increment(25, 'b') +--- + - 6: {'b'} +... +lua box.auto_increment(25, 'c') +--- + - 7: {'c'} +... +lua box.space[25]:truncate() +--- +... lua t=box.insert(12, '1', '2', '3', '4', '5', '6', '7') --- ... diff --git a/test/big/lua.test b/test/big/lua.test index 2f90fb5ef18ac6297cfefebbf07ccf2ec013ac6b..1dca02e1307b291c4ec610a003842c1ac06ea56f 100644 --- a/test/big/lua.test +++ b/test/big/lua.test @@ -125,6 +125,16 @@ exec admin "lua box.auto_increment(18, 'b')" exec admin "lua box.auto_increment(18, 'c')" exec admin "lua box.space[18]:truncate()" +# +# Tests for lua box.auto_increment with NUM64 keys +# +exec admin "lua box.space[25]:truncate()" +exec admin "lua box.auto_increment(25, 'a')" +exec admin "lua box.insert(25, tonumber64(5))" +exec admin "lua box.auto_increment(25, 'b')" +exec admin "lua box.auto_increment(25, 'c')" +exec admin "lua box.space[25]:truncate()" + # # Tests for lua tuple:transform() # diff --git a/test/big/tarantool.cfg b/test/big/tarantool.cfg index 06c6c576a70f5f7e656e2be8e45395bea327c757..d1d62e138a2ea28498c133755318d8230ebab94d 100644 --- a/test/big/tarantool.cfg +++ b/test/big/tarantool.cfg @@ -252,3 +252,10 @@ space[23].index[0].key_field[0].fieldno = 2 space[23].index[0].key_field[0].type = "NUM" space[23].index[0].key_field[1].fieldno = 1 space[23].index[0].key_field[1].type = "NUM" + +# lua box.auto_increment() with NUM64 keys testing +space[25].enabled = 1 +space[25].index[0].type = "TREE" +space[25].index[0].unique = 1 +space[25].index[0].key_field[0].fieldno = 0 +space[25].index[0].key_field[0].type = "NUM64"