diff --git a/CHANGELOG.md b/CHANGELOG.md index f5ed94fe..e806365a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed +* Select by primary index name + ## [0.2.0] - 2020-10-07 ### Fixed diff --git a/crud/select/filters.lua b/crud/select/filters.lua index 88020baa..87379305 100644 --- a/crud/select/filters.lua +++ b/crud/select/filters.lua @@ -96,7 +96,8 @@ local function get_values_opts(index, fieldnos) end local function get_index_by_name(space_indexes, index_name) - for _, index in ipairs(space_indexes) do + for i = 0, #space_indexes do + local index = space_indexes[i] if index.name == index_name then return index end @@ -608,6 +609,7 @@ filters.internal = { parse = parse, gen_filter_code = gen_filter_code, compile = compile, + get_index_by_name = get_index_by_name, } return filters diff --git a/crud/select/plan.lua b/crud/select/plan.lua index c9de7a8d..181da974 100644 --- a/crud/select/plan.lua +++ b/crud/select/plan.lua @@ -39,7 +39,8 @@ local function validate_conditions(conditions, space_indexes, space_format) end local index_names = {} - for _, index in ipairs(space_indexes) do + for i = 0, #space_indexes do + local index = space_indexes[i] index_names[index.name] = true end diff --git a/test/entrypoint/srv_select.lua b/test/entrypoint/srv_select.lua index acd11313..c64bfe45 100755 --- a/test/entrypoint/srv_select.lua +++ b/test/entrypoint/srv_select.lua @@ -24,7 +24,8 @@ package.preload['customers-storage'] = function() if_not_exists = true, engine = engine, }) - customers_space:create_index('id', { + --primary index + customers_space:create_index('id_index', { parts = { {field = 'id'} }, if_not_exists = true, }) @@ -33,11 +34,21 @@ package.preload['customers-storage'] = function() unique = false, if_not_exists = true, }) + customers_space:create_index('age_index', { + parts = { {field = 'age'} }, + unique = false, + if_not_exists = true, + }) + --indexes with same names as fields customers_space:create_index('age', { parts = { {field = 'age'} }, unique = false, if_not_exists = true, }) + customers_space:create_index('id', { + parts = { {field = 'id'} }, + if_not_exists = true, + }) customers_space:create_index('full_name', { parts = { { field = 'name', collation = 'unicode_ci' }, diff --git a/test/integration/select_test.lua b/test/integration/select_test.lua index de069424..0ccfff67 100644 --- a/test/integration/select_test.lua +++ b/test/integration/select_test.lua @@ -509,6 +509,37 @@ add('test_select_all_with_batch_size', function(g) t.assert_equals(objects, get_by_ids(customers, {1, 2, 3, 4, 5, 6})) end) +add('test_select_by_primary_index', function(g) + local customers = insert_customers(g, { + { + id = 1, name = "Elizabeth", last_name = "Jackson", + age = 12, city = "New York", + }, { + id = 2, name = "Mary", last_name = "Brown", + age = 46, city = "Los Angeles", + }, { + id = 3, name = "David", last_name = "Smith", + age = 33, city = "Los Angeles", + }, + }) + + table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end) + + local conditions = {{'==', 'id_index', 3}} + local result, err = g.cluster.main_server.net_box:eval([[ + local crud = require('crud') + + local conditions = ... + + local result, err = crud.select('customers', conditions) + return result, err + ]], {conditions}) + + t.assert_equals(err, nil) + local objects = crud.unflatten_rows(result.rows, result.metadata) + t.assert_equals(objects, get_by_ids(customers, {3})) +end) + add('test_eq_condition_with_index', function(g) local customers = insert_customers(g, { { @@ -538,7 +569,7 @@ add('test_eq_condition_with_index', function(g) table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end) local conditions = { - {'==', 'age', 33}, + {'==', 'age_index', 33}, } -- no after @@ -593,7 +624,7 @@ add('test_ge_condition_with_index', function(g) table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end) local conditions = { - {'>=', 'age', 33}, + {'>=', 'age_index', 33}, } -- no after @@ -648,7 +679,7 @@ add('test_le_condition_with_index',function(g) table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end) local conditions = { - {'<=', 'age', 33}, + {'<=', 'age_index', 33}, } -- no after @@ -703,7 +734,7 @@ add('test_lt_condition_with_index', function(g) table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end) local conditions = { - {'<', 'age', 33}, + {'<', 'age_index', 33}, } -- no after diff --git a/test/unit/select_filters_test.lua b/test/unit/select_filters_test.lua index aa3c84ef..53f945ac 100644 --- a/test/unit/select_filters_test.lua +++ b/test/unit/select_filters_test.lua @@ -638,4 +638,21 @@ return M]] t.assert_equals(filter_func({'a', box.NULL}), false) -- box.NULL > box.NULL is false end +g.test_select_filter_get_index_by_name = function() + local space_indexes = { + [0] = {name = 'primary'}, + [1] = {name = 'second'}, + [2] = {name = 'third'} + } + + local index = select_filters.internal.get_index_by_name(space_indexes, "primary"); + t.assert_equals(index, space_indexes[0]); + + local index = select_filters.internal.get_index_by_name(space_indexes, "third"); + t.assert_equals(index, space_indexes[2]); + + local index = select_filters.internal.get_index_by_name(space_indexes, "not_exist_index"); + t.assert_equals(index, nil) +end + -- luacheck: pop