Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion crud/select/filters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion crud/select/plan.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion test/entrypoint/srv_select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand All @@ -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' },
Expand Down
39 changes: 35 additions & 4 deletions test/integration/select_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/unit/select_filters_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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