Skip to content

Commit 2c5eabe

Browse files
oleg-jukovecDifferentialOrange
authored andcommitted
select: fix result order with negative first
crud.select reverses order of the results from the cluster in case of negative first. It is necessary because the pagination results should be in the same order when moving in both directions. The reverse function did not work correctly.
1 parent 00c1990 commit 2c5eabe

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
5252
- [6, 1064, 'Alexey', 'Sidorov', 31]
5353
...
5454

55+
* `crud.select()` results order with negative `first`.
56+
5557
## [0.11.2] - 23-05-22
5658

5759
### Added

crud/common/utils.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ function utils.invert_tarantool_iter(iter)
455455
end
456456

457457
function utils.reverse_inplace(t)
458-
for i = 1,#t - 1 do
458+
for i = 1,math.floor(#t / 2) do
459459
t[i], t[#t - i + 1] = t[#t - i + 1], t[i]
460460
end
461461
return t

test/integration/select_test.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ pgroup.test_negative_first = function(g)
206206
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
207207

208208
-- no conditions
209-
-- first -3 after 5
210-
local first = -3
211-
local after = crud_utils.flatten(customers[5], g.space_format)
209+
-- first -4 after 6
210+
local first = -4
211+
local after = crud_utils.flatten(customers[6], g.space_format)
212212
local result, err = g.cluster.main_server.net_box:call(
213213
'crud.select', {'customers', nil, {first=first, after=after}})
214214

215215
t.assert_equals(err, nil)
216216
local objects = crud.unflatten_rows(result.rows, result.metadata)
217-
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {2, 3, 4}))
217+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {2, 3, 4, 5}))
218218

219219
-- id >= 2
220220
-- first -2 after 5

test/unit/reverse_inplace_test.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local t = require('luatest')
2+
local g = t.group('utils')
3+
4+
local utils = require('crud.common.utils')
5+
6+
local reverse_inplace_cases = {
7+
single_value = {1},
8+
two_values = {1, 2},
9+
three_values = {1, 2, 3},
10+
four_values = {1, 2, 3, 4},
11+
uneven_values = {1, 2, 3, 4, 5, 6, 7, 8, 9},
12+
even_values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
13+
}
14+
15+
for case_name, case in pairs(reverse_inplace_cases) do
16+
g["test_reverse_inplace_" .. case_name] = function()
17+
local case_reversed = {}
18+
for i=#case,1,-1 do
19+
table.insert(case_reversed, case[i])
20+
end
21+
case = utils.reverse_inplace(case)
22+
t.assert_equals(case, case_reversed)
23+
end
24+
end

0 commit comments

Comments
 (0)