From a887a1f4422a42f473c4aff7f34454d80cf3c82a Mon Sep 17 00:00:00 2001 From: Mergen Imeev <imeevma@tarantool.org> Date: Tue, 10 Oct 2023 21:15:47 +0300 Subject: [PATCH] sql: introduce sql_index_id_by_src() This patch introduces the sql_index_id_by_src() function. This function is used to search for an index based on a given element of the struct SrcList. Needed for #4467 NO_DOC=refactoring NO_TEST=refactoring NO_CHANGELOG=refactoring --- src/box/sql.c | 29 ++++++++++++++++++++++------- src/box/sql/select.c | 28 ++++++++++------------------ src/box/sql/sqlInt.h | 7 +++++++ 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/box/sql.c b/src/box/sql.c index 767fe75852..cc7bcff05f 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 fcef39b6b7..9581bbeb95 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 00623cb48b..932b22195d 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. -- GitLab