diff --git a/src/box/sql.c b/src/box/sql.c index 767fe75852b025d106745b4f61f667f39ae5ec45..cc7bcff05f3d4f04d56aacacdd639eae2048fe05 100644 --- a/src/box/sql.c +++ b/src/box/sql.c @@ -1634,21 +1634,36 @@ sql_space_by_src(const struct SrcList_item *src) return space_by_name0(src->zName); } +/** + * Return id of index with the given name. Return UINT32_MAX if the index was + * not found. + */ +static uint32_t +sql_space_index_id(const struct space *space, const char *name) +{ + for (uint32_t i = 0; i < space->index_count; ++i) { + if (strcmp(space->index[i]->def->name, name) == 0) + return space->index[i]->def->iid; + } + return UINT32_MAX; +} + uint32_t sql_index_id_by_token(const struct space *space, const struct Token *name) { char *name_str = sql_name_from_token(name); - uint32_t res = UINT32_MAX; - for (uint32_t i = 0; i < space->index_count; ++i) { - if (strcmp(space->index[i]->def->name, name_str) == 0) { - res = space->index[i]->def->iid; - break; - } - } + uint32_t res = sql_space_index_id(space, name_str); sql_xfree(name_str); return res; } +uint32_t +sql_index_id_by_src(const struct SrcList_item *src) +{ + assert(src->space != NULL && src->fg.isIndexedBy != 0); + return sql_space_index_id(src->space, src->u1.zIndexedBy); +} + uint32_t sql_fieldno_by_token(const struct space *space, const struct Token *name) { diff --git a/src/box/sql/select.c b/src/box/sql/select.c index fcef39b6b7a9ceec79dd52f015b0146d930dd3cb..9581bbeb9500482a9bb31902e45e96cda1c2dd02 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -4501,25 +4501,17 @@ is_simple_count(struct Select *select, struct AggInfo *agg_info) int sqlIndexedByLookup(Parse * pParse, struct SrcList_item *pFrom) { - if (pFrom->space != NULL && pFrom->fg.isIndexedBy) { - struct space *space = pFrom->space; - char *zIndexedBy = pFrom->u1.zIndexedBy; - struct index *idx = NULL; - for (uint32_t i = 0; i < space->index_count; ++i) { - if (strcmp(space->index[i]->def->name, - zIndexedBy) == 0) { - idx = space->index[i]; - break; - } - } - if (idx == NULL) { - diag_set(ClientError, ER_NO_SUCH_INDEX_NAME, - zIndexedBy, space->def->name); - pParse->is_aborted = true; - return -1; - } - pFrom->pIBIndex = idx->def; + if (pFrom->space == NULL || pFrom->fg.isIndexedBy == 0) + return 0; + uint32_t index_id = sql_index_id_by_src(pFrom); + if (index_id == UINT32_MAX) { + diag_set(ClientError, ER_NO_SUCH_INDEX_NAME, + pFrom->u1.zIndexedBy, pFrom->space->def->name); + pParse->is_aborted = true; + return -1; } + assert(index_id <= pFrom->space->index_id_max); + pFrom->pIBIndex = pFrom->space->index_map[index_id]->def; return 0; } diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 00623cb48b136ae9bf43fc172f7f505fad401ba1..932b22195db50bb47f7d3f65e7bd061c80a1d330 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -3125,6 +3125,13 @@ sql_space_by_src(const struct SrcList_item *src); uint32_t sql_index_id_by_token(const struct space *space, const struct Token *name); +/** + * Return index with name defined by the element of struct SrcList. Return NULL + * if the index was not found. + */ +uint32_t +sql_index_id_by_src(const struct SrcList_item *src); + /** * Return the fieldno of the field with the name defined by the token. Return * UINT32_MAX if the field was not found.