diff --git a/src/box/sql/build.c b/src/box/sql/build.c index 819c29677fce9bdac8a138a4865f3dc8bd19cad1..f2b58cf772c17fdca52d64961df4f17759f98797 100644 --- a/src/box/sql/build.c +++ b/src/box/sql/build.c @@ -3344,24 +3344,20 @@ int sql_fieldno_by_name(struct Parse *parse_context, struct Expr *field_name, uint32_t *fieldno) { - struct space_def *def = parse_context->create_table_def.new_space->def; + const struct space *space = parse_context->create_table_def.new_space; struct Expr *name = sqlExprSkipCollate(field_name); if (name->op != TK_ID) { diag_set(ClientError, ER_INDEX_DEF_UNSUPPORTED, "Expressions"); parse_context->is_aborted = true; return -1; } - uint32_t i; - for (i = 0; i < def->field_count; ++i) { - if (strcmp(def->fields[i].name, name->u.zToken) == 0) - break; - } - if (i == def->field_count) { + uint32_t id = sql_fieldno_by_expr(space, name); + if (id == UINT32_MAX) { diag_set(ClientError, ER_SQL_CANT_RESOLVE_FIELD, name->u.zToken); parse_context->is_aborted = true; return -1; } - *fieldno = i; + *fieldno = id; return 0; } diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c index d8a872251bbfa558e8498464933d88edebaca971..5775ef8b840a0d759452dfb5b67c1142e58ac3f9 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -5436,3 +5436,9 @@ sqlClearTempRegCache(Parse * pParse) pParse->nRangeReg = 0; } +uint32_t +sql_fieldno_by_expr(const struct space *space, const struct Expr *expr) +{ + assert(expr->op == TK_ID); + return sql_space_fieldno(space, expr->u.zToken); +} diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index bf9fa94d1a917d5d00395b924c80f0102b44f700..4c2464c1c1de36c68e724dd894b8b6b1bc395846 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -3153,6 +3153,13 @@ sql_fieldno_by_token(const struct space *space, const struct Token *name); uint32_t sql_fieldno_by_id(const struct space *space, const struct IdList_item *id); +/** + * Return the fieldno of the field with the name defined by the expression. + * Return UINT32_MAX if the field was not found. + */ +uint32_t +sql_fieldno_by_expr(const struct space *space, const struct Expr *expr); + /** * Return the tuple foreign key constraint with the name defined by the token. * Return NULL if the tuple foreign key constraint was not found.