Skip to content

Commit 842b30a

Browse files
AnaNekTotktonada
authored andcommitted
Fix fail of select when there are no indexes in space
When `select()` is called for space with no indexes `select` fails with error `attempt to index local 'index' (a nil value)`. This fix checks if space contains indexes and if there are no indexes in space `select` returns error `NoIndexesError:new('Space %q has no indexes, space should have primary index', space_name)`. Closes #257
1 parent fb10ea9 commit 842b30a

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

crud/select/plan.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ local select_plan = {}
1616

1717
local IndexTypeError = errors.new_class('IndexTypeError', {capture_stack = false})
1818
local FilterFieldsError = errors.new_class('FilterFieldsError', {capture_stack = false})
19+
local NoIndexesError = errors.new_class('NoIndexesError', {capture_stack = false})
1920

2021
local function index_is_allowed(index)
2122
return index.type == 'TREE'
@@ -173,6 +174,10 @@ function select_plan.new(space, conditions, opts)
173174
local space_indexes = space.index
174175
local space_format = space:format()
175176

177+
if space_indexes == nil or next(space_indexes) == nil then
178+
return nil, NoIndexesError:new('Space %q has no indexes, space should have primary index', space_name)
179+
end
180+
176181
if conditions == nil then -- also cdata<NULL>
177182
conditions = {}
178183
end

test/entrypoint/srv_select.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ package.preload['customers-storage'] = function()
1313
return {
1414
role_name = 'customers-storage',
1515
init = function()
16+
box.schema.space.create('no_index_space', {
17+
format = {
18+
{name = 'id', type = 'unsigned'},
19+
{name = 'bucket_id', type = 'unsigned'},
20+
{name = 'name', type = 'string'},
21+
},
22+
if_not_exists = true,
23+
engine = engine,
24+
})
25+
1626
local customers_space = box.schema.space.create('customers', {
1727
format = {
1828
{name = 'id', type = 'unsigned'},

test/integration/select_test.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ pgroup.test_non_existent_space = function(g)
5555
t.assert_str_contains(err.err, "Space \"non_existent_space\" doesn't exist")
5656
end
5757

58+
pgroup.test_select_no_index = function(g)
59+
local obj, err = g.cluster.main_server.net_box:call(
60+
'crud.select', {'no_index_space'}
61+
)
62+
63+
t.assert_equals(obj, nil)
64+
t.assert_str_contains(err.err, "Space \"no_index_space\" has no indexes, space should have primary index")
65+
end
66+
5867
pgroup.test_not_valid_value_type = function(g)
5968
local conditions = {
6069
{'=', 'id', 'not_number'}

0 commit comments

Comments
 (0)