Skip to content

Commit e5eb7ca

Browse files
authored
Fix getting primary space index (#63)
1 parent a838f58 commit e5eb7ca

File tree

6 files changed

+72
-7
lines changed

6 files changed

+72
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77

88
## [Unreleased]
99

10+
### Fixed
11+
* Select by primary index name
12+
1013
## [0.2.0] - 2020-10-07
1114

1215
### Fixed

crud/select/filters.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ local function get_values_opts(index, fieldnos)
9696
end
9797

9898
local function get_index_by_name(space_indexes, index_name)
99-
for _, index in ipairs(space_indexes) do
99+
for i = 0, #space_indexes do
100+
local index = space_indexes[i]
100101
if index.name == index_name then
101102
return index
102103
end
@@ -608,6 +609,7 @@ filters.internal = {
608609
parse = parse,
609610
gen_filter_code = gen_filter_code,
610611
compile = compile,
612+
get_index_by_name = get_index_by_name,
611613
}
612614

613615
return filters

crud/select/plan.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ local function validate_conditions(conditions, space_indexes, space_format)
3939
end
4040

4141
local index_names = {}
42-
for _, index in ipairs(space_indexes) do
42+
for i = 0, #space_indexes do
43+
local index = space_indexes[i]
4344
index_names[index.name] = true
4445
end
4546

test/entrypoint/srv_select.lua

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ package.preload['customers-storage'] = function()
2424
if_not_exists = true,
2525
engine = engine,
2626
})
27-
customers_space:create_index('id', {
27+
--primary index
28+
customers_space:create_index('id_index', {
2829
parts = { {field = 'id'} },
2930
if_not_exists = true,
3031
})
@@ -33,11 +34,21 @@ package.preload['customers-storage'] = function()
3334
unique = false,
3435
if_not_exists = true,
3536
})
37+
customers_space:create_index('age_index', {
38+
parts = { {field = 'age'} },
39+
unique = false,
40+
if_not_exists = true,
41+
})
42+
--indexes with same names as fields
3643
customers_space:create_index('age', {
3744
parts = { {field = 'age'} },
3845
unique = false,
3946
if_not_exists = true,
4047
})
48+
customers_space:create_index('id', {
49+
parts = { {field = 'id'} },
50+
if_not_exists = true,
51+
})
4152
customers_space:create_index('full_name', {
4253
parts = {
4354
{ field = 'name', collation = 'unicode_ci' },

test/integration/select_test.lua

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,37 @@ add('test_select_all_with_batch_size', function(g)
509509
t.assert_equals(objects, get_by_ids(customers, {1, 2, 3, 4, 5, 6}))
510510
end)
511511

512+
add('test_select_by_primary_index', function(g)
513+
local customers = insert_customers(g, {
514+
{
515+
id = 1, name = "Elizabeth", last_name = "Jackson",
516+
age = 12, city = "New York",
517+
}, {
518+
id = 2, name = "Mary", last_name = "Brown",
519+
age = 46, city = "Los Angeles",
520+
}, {
521+
id = 3, name = "David", last_name = "Smith",
522+
age = 33, city = "Los Angeles",
523+
},
524+
})
525+
526+
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
527+
528+
local conditions = {{'==', 'id_index', 3}}
529+
local result, err = g.cluster.main_server.net_box:eval([[
530+
local crud = require('crud')
531+
532+
local conditions = ...
533+
534+
local result, err = crud.select('customers', conditions)
535+
return result, err
536+
]], {conditions})
537+
538+
t.assert_equals(err, nil)
539+
local objects = crud.unflatten_rows(result.rows, result.metadata)
540+
t.assert_equals(objects, get_by_ids(customers, {3}))
541+
end)
542+
512543
add('test_eq_condition_with_index', function(g)
513544
local customers = insert_customers(g, {
514545
{
@@ -538,7 +569,7 @@ add('test_eq_condition_with_index', function(g)
538569
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
539570

540571
local conditions = {
541-
{'==', 'age', 33},
572+
{'==', 'age_index', 33},
542573
}
543574

544575
-- no after
@@ -593,7 +624,7 @@ add('test_ge_condition_with_index', function(g)
593624
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
594625

595626
local conditions = {
596-
{'>=', 'age', 33},
627+
{'>=', 'age_index', 33},
597628
}
598629

599630
-- no after
@@ -648,7 +679,7 @@ add('test_le_condition_with_index',function(g)
648679
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
649680

650681
local conditions = {
651-
{'<=', 'age', 33},
682+
{'<=', 'age_index', 33},
652683
}
653684

654685
-- no after
@@ -703,7 +734,7 @@ add('test_lt_condition_with_index', function(g)
703734
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
704735

705736
local conditions = {
706-
{'<', 'age', 33},
737+
{'<', 'age_index', 33},
707738
}
708739

709740
-- no after

test/unit/select_filters_test.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,21 @@ return M]]
638638
t.assert_equals(filter_func({'a', box.NULL}), false) -- box.NULL > box.NULL is false
639639
end
640640

641+
g.test_select_filter_get_index_by_name = function()
642+
local space_indexes = {
643+
[0] = {name = 'primary'},
644+
[1] = {name = 'second'},
645+
[2] = {name = 'third'}
646+
}
647+
648+
local index = select_filters.internal.get_index_by_name(space_indexes, "primary");
649+
t.assert_equals(index, space_indexes[0]);
650+
651+
local index = select_filters.internal.get_index_by_name(space_indexes, "third");
652+
t.assert_equals(index, space_indexes[2]);
653+
654+
local index = select_filters.internal.get_index_by_name(space_indexes, "not_exist_index");
655+
t.assert_equals(index, nil)
656+
end
657+
641658
-- luacheck: pop

0 commit comments

Comments
 (0)