Skip to content

Commit 00c1990

Browse files
oleg-jukovecDifferentialOrange
authored andcommitted
select: fix traverse start key in case condition + after
A comparator that choosed a starting key for select from a condition value and after always returned a smaller one. This led to an unsymmetric behavior depending on the direction of a traverse. Plus conditions didn't work correctly in all cases.
1 parent d4ed93a commit 00c1990

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,27 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3131
- [1, 477, 'Alexey', 'Adams', 20]
3232
...
3333

34+
* `crud.select()` filtration by a first condition if the condition is '>'
35+
or '>=' and it's value > `after`.
36+
37+
Before this patch:
38+
39+
tarantool> crud.select('developers', {{'>=', 'id', 5}}, {first = 10, after = rows[2]}).rows
40+
---
41+
- - [3, 2804, 'Pavel', 'Adams', 27]
42+
- [4, 1161, 'Mikhail', 'Liston', 51]
43+
- [5, 1172, 'Dmitry', 'Jacobi', 16]
44+
- [6, 1064, 'Alexey', 'Sidorov', 31]
45+
...
46+
47+
After this patch:
48+
49+
tarantool> crud.select('developers', {{'>=', 'id', 5}}, {first = 10, after = rows[2]}).rows
50+
---
51+
- [5, 1172, 'Dmitry', 'Jacobi', 16]
52+
- [6, 1064, 'Alexey', 'Sidorov', 31]
53+
...
54+
3455
## [0.11.2] - 23-05-22
3556

3657
### Added

crud/select/executor.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ end
4242
local generate_value
4343

4444
if has_keydef then
45-
generate_value = function(after_tuple, scan_value, index_parts)
45+
generate_value = function(after_tuple, scan_value, index_parts, tarantool_iter)
46+
local cmp_operator = select_comparators.get_cmp_operator(tarantool_iter)
4647
local key_def = keydef_lib.new(index_parts)
47-
if key_def:compare_with_key(after_tuple, scan_value) < 0 then
48+
local cmp = key_def:compare_with_key(after_tuple, scan_value)
49+
if (cmp_operator == '<' and cmp < 0) or (cmp_operator == '>' and cmp > 0) then
4850
return key_def:extract_key(after_tuple)
4951
end
5052
end

test/integration/select_test.lua

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,94 @@ pgroup.test_negative_first = function(g)
273273
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {6}))
274274
end
275275

276+
pgroup.test_positive_first = function(g)
277+
local customers = helpers.insert_objects(g, 'customers', {
278+
{
279+
id = 1, name = "Elizabeth", last_name = "Jackson",
280+
age = 11, city = "New York",
281+
}, {
282+
id = 2, name = "Mary", last_name = "Brown",
283+
age = 22, city = "Los Angeles",
284+
}, {
285+
id = 3, name = "David", last_name = "Smith",
286+
age = 33, city = "Los Angeles",
287+
}, {
288+
id = 4, name = "William", last_name = "White",
289+
age = 44, city = "Chicago",
290+
}, {
291+
id = 5, name = "Jack", last_name = "Sparrow",
292+
age = 55, city = "London",
293+
}, {
294+
id = 6, name = "William", last_name = "Terner",
295+
age = 66, city = "Oxford",
296+
}, {
297+
id = 7, name = "Elizabeth", last_name = "Swan",
298+
age = 77, city = "Cambridge",
299+
}, {
300+
id = 8, name = "Hector", last_name = "Barbossa",
301+
age = 88, city = "London",
302+
},
303+
})
304+
305+
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
306+
307+
-- id >= 3
308+
-- first 10 after 5
309+
local conditions = {
310+
{'>=', 'id', 3},
311+
}
312+
local first = 10
313+
local after = crud_utils.flatten(customers[5], g.space_format)
314+
local result, err = g.cluster.main_server.net_box:call(
315+
'crud.select', {'customers', conditions, {first=first, after=after}})
316+
317+
t.assert_equals(err, nil)
318+
local objects = crud.unflatten_rows(result.rows, result.metadata)
319+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {6, 7, 8}))
320+
321+
-- id <= 3
322+
-- first 10 after 5
323+
local conditions = {
324+
{'<=', 'id', 3},
325+
}
326+
local first = 10
327+
local after = crud_utils.flatten(customers[5], g.space_format)
328+
local result, err = g.cluster.main_server.net_box:call(
329+
'crud.select', {'customers', conditions, {first=first, after=after}})
330+
331+
t.assert_equals(err, nil)
332+
local objects = crud.unflatten_rows(result.rows, result.metadata)
333+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {3, 2, 1}))
334+
335+
-- id <= 6
336+
-- first 10 after 5
337+
local conditions = {
338+
{'<=', 'id', 6},
339+
}
340+
local first = 10
341+
local after = crud_utils.flatten(customers[5], g.space_format)
342+
local result, err = g.cluster.main_server.net_box:call(
343+
'crud.select', {'customers', conditions, {first=first, after=after}})
344+
345+
t.assert_equals(err, nil)
346+
local objects = crud.unflatten_rows(result.rows, result.metadata)
347+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {4, 3, 2, 1}))
348+
349+
-- id >= 6
350+
-- first 10 after 2
351+
local conditions = {
352+
{'>=', 'id', 6},
353+
}
354+
local first = 10
355+
local after = crud_utils.flatten(customers[2], g.space_format)
356+
local result, err = g.cluster.main_server.net_box:call(
357+
'crud.select', {'customers', conditions, {first=first, after=after}})
358+
359+
t.assert_equals(err, nil)
360+
local objects = crud.unflatten_rows(result.rows, result.metadata)
361+
t.assert_equals(objects, helpers.get_objects_by_idxs(customers, {6, 7, 8}))
362+
end
363+
276364
pgroup.test_negative_first_with_batch_size = function(g)
277365
local customers = helpers.insert_objects(g, 'customers', {
278366
{

0 commit comments

Comments
 (0)