diff --git a/src/box/sql/analyze.c b/src/box/sql/analyze.c index e97fefb3aa7f3736ae712b90bea89d0482e6c75c..fb4ad2a81d9721150e66360c28b4fae06f3cf8d9 100644 --- a/src/box/sql/analyze.c +++ b/src/box/sql/analyze.c @@ -719,6 +719,10 @@ callStatGet(Vdbe * v, int regStat4, int iParam, int regOut) { assert(regOut != regStat4 && regOut != regStat4 + 1); sqlVdbeAddOp2(v, OP_Integer, iParam, regStat4 + 1); + /* + * Function sql_func_by_signature() was removed, so after enabling this + * part should be changed. + */ struct func *func = sql_func_by_signature("_sql_stat_get", 2); assert(func != NULL); sqlVdbeAddOp4(v, OP_BuiltinFunction0, 0, regStat4, regOut, @@ -858,6 +862,10 @@ vdbe_emit_analyze_space(struct Parse *parse, struct space *space) sqlVdbeAddOp2(v, OP_Count, idx_cursor, stat4_reg + 3); sqlVdbeAddOp2(v, OP_Integer, part_count, stat4_reg + 1); sqlVdbeAddOp2(v, OP_Integer, part_count, stat4_reg + 2); + /* + * Function sql_func_by_signature() was removed, so after + * enabling this part should be changed. + */ struct func *init_func = sql_func_by_signature("_sql_stat_init", 3); assert(init_func != NULL); @@ -959,6 +967,10 @@ vdbe_emit_analyze_space(struct Parse *parse, struct space *space) sqlVdbeAddOp3(v, OP_MakeRecord, stat_key_reg, pk_part_count, key_reg); assert(chng_reg == (stat4_reg + 1)); + /* + * Function sql_func_by_signature() was removed, so after + * enabling this part should be changed. + */ struct func *push_func = sql_func_by_signature("_sql_stat_push", 3); assert(push_func != NULL); diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c index 270d4647400332161c2bfa264194491d3817929f..6f40183acc770000c25de433e910b190e10936d0 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -3922,7 +3922,6 @@ sqlExprCodeTarget(Parse * pParse, Expr * pExpr, int target) case TK_FUNCTION:{ ExprList *pFarg; /* List of function arguments */ int nFarg; /* Number of function arguments */ - const char *zId; /* The function name */ u32 constMask = 0; /* Mask of function arguments that are constant */ int i; /* Loop counter */ struct coll *coll = NULL; @@ -3935,11 +3934,8 @@ sqlExprCodeTarget(Parse * pParse, Expr * pExpr, int target) } nFarg = pFarg ? pFarg->nExpr : 0; assert(!ExprHasProperty(pExpr, EP_IntValue)); - zId = pExpr->u.zToken; - struct func *func = sql_func_by_signature(zId, nFarg); + struct func *func = sql_func_find(pExpr); if (func == NULL) { - diag_set(ClientError, ER_NO_SUCH_FUNCTION, - zId); pParse->is_aborted = true; break; } @@ -5396,14 +5392,8 @@ analyzeAggregate(Walker * pWalker, Expr * pExpr) pItem->iMem = ++pParse->nMem; assert(!ExprHasProperty (pExpr, EP_IntValue)); - const char *name = - pExpr->u.zToken; - uint32_t argc = - pExpr->x.pList != NULL ? - pExpr->x.pList->nExpr : 0; pItem->func = - sql_func_by_signature( - name, argc); + sql_func_find(pExpr); assert(pItem->func != NULL); assert(pItem->func->def-> language == diff --git a/src/box/sql/func.c b/src/box/sql/func.c index ccc8430aab8e1500fbc5ca1ee2a09217e35fb00b..9347c5e74b1a986486670aabf09771b8e16dacbe 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1913,24 +1913,12 @@ sql_is_like_func(struct Expr *expr) expr->x.pList->nExpr != 2) return 0; assert(!ExprHasProperty(expr, EP_xIsSelect)); - struct func *func = sql_func_by_signature(expr->u.zToken, 2); + struct func *func = sql_func_find(expr); if (func == NULL || !sql_func_flag_is_set(func, SQL_FUNC_LIKE)) return 0; return 1; } -struct func * -sql_func_by_signature(const char *name, int argc) -{ - struct func *base = func_by_name(name, strlen(name)); - if (base == NULL || !base->def->exports.sql) - return NULL; - - if (base->def->param_count != -1 && base->def->param_count != argc) - return NULL; - return base; -} - static int func_sql_builtin_call_stub(struct func *func, struct port *args, struct port *ret) @@ -2634,6 +2622,30 @@ static struct { }, }; +struct func * +sql_func_find(struct Expr *expr) +{ + const char *name = expr->u.zToken; + struct func *func = func_by_name(name, strlen(name)); + if (func == NULL) { + diag_set(ClientError, ER_NO_SUCH_FUNCTION, name); + return NULL; + } + if (!func->def->exports.sql) { + diag_set(ClientError, ER_SQL_PARSER_GENERIC, + tt_sprintf("function %s() is not available in SQL", + name)); + return NULL; + } + int n = expr->x.pList != NULL ? expr->x.pList->nExpr : 0; + if (func->def->param_count != -1 && func->def->param_count != n) { + diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, name, + tt_sprintf("%d", func->def->param_count), n); + return NULL; + } + return func; +} + uint32_t sql_func_flags(const char *name) { diff --git a/src/box/sql/resolve.c b/src/box/sql/resolve.c index 11b6139e37dd6f70624f768661ca8b90e7e94147..35faddab522f13967d010311688082afa69c4212 100644 --- a/src/box/sql/resolve.c +++ b/src/box/sql/resolve.c @@ -598,28 +598,8 @@ resolveExprStep(Walker * pWalker, Expr * pExpr) assert(!ExprHasProperty(pExpr, EP_xIsSelect)); zId = pExpr->u.zToken; nId = sqlStrlen30(zId); - struct func *func = func_by_name(zId, nId); + struct func *func = sql_func_find(pExpr); if (func == NULL) { - diag_set(ClientError, ER_NO_SUCH_FUNCTION, zId); - pParse->is_aborted = true; - pNC->nErr++; - return WRC_Abort; - } - if (!func->def->exports.sql) { - diag_set(ClientError, ER_SQL_PARSER_GENERIC, - tt_sprintf("function %.*s() is not " - "available in SQL", - nId, zId)); - pParse->is_aborted = true; - pNC->nErr++; - return WRC_Abort; - } - if (func->def->param_count != -1 && - func->def->param_count != n) { - uint32_t argc = func->def->param_count; - const char *err = tt_sprintf("%d", argc); - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, - func->def->name, err, n); pParse->is_aborted = true; pNC->nErr++; return WRC_Abort; diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 7b332ba3915cac4fccd239243affc31c39aaf221..1f87e682376667beca666eeb3c628ee58f75dee8 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -4337,17 +4337,9 @@ sql_func_flag_is_set(struct func *func, uint16_t flag) return (((struct func_sql_builtin *)func)->flags & flag) != 0; } -/** - * A SQL method to find a function in a hash by its name and - * count of arguments. Only functions that have 'SQL' engine - * export field set true and have exactly the same signature - * are returned. - * - * Returns not NULL function pointer when a valid and exported - * to SQL engine function is found and NULL otherwise. - */ +/** Return a function that matches the parameters described in given expr. */ struct func * -sql_func_by_signature(const char *name, int argc); +sql_func_find(struct Expr *expr); /** * Return the parameters of the function with the given name. If the function diff --git a/src/box/sql/vdbemem.c b/src/box/sql/vdbemem.c index 2c50996167e58d336926ec868106b1804c89c1e8..499089c8dfefda99b62854fc894b6e70fc4d23fa 100644 --- a/src/box/sql/vdbemem.c +++ b/src/box/sql/vdbemem.c @@ -148,7 +148,7 @@ valueFromFunction(sql * db, /* The database connection */ pList = p->x.pList; if (pList) nVal = pList->nExpr; - struct func *func = sql_func_by_signature(p->u.zToken, nVal); + struct func *func = sql_func_find(p); if (func == NULL || func->def->language != FUNC_LANGUAGE_SQL_BUILTIN || !func->def->is_deterministic || sql_func_flag_is_set(func, SQL_FUNC_NEEDCOLL)) diff --git a/test/sql-tap/func5.test.lua b/test/sql-tap/func5.test.lua index 98a8b9adaaf7ec348564c712c42222d4acddad9a..829e9e37dd3ec68a9dc5fa8ab09d9e4f5749fb94 100755 --- a/test/sql-tap/func5.test.lua +++ b/test/sql-tap/func5.test.lua @@ -1,6 +1,6 @@ #!/usr/bin/env tarantool local test = require("sqltester") -test:plan(25) +test:plan(27) --!./tcltestrunner.lua -- 2010 August 27 @@ -316,4 +316,36 @@ test:do_catchsql_test( box.func.COUNTER1:drop() box.func.COUNTER2:drop() +-- +-- Make sure the correct error is displayed if the function throws an error when +-- setting the default value. +-- +local body = 'function(x) return 1 end' +box.schema.func.create('F1', {language = 'Lua', returns = 'number', body = body, + param_list = {}, exports = {'LUA'}}); +box.execute([[CREATE TABLE t01(i INT PRIMARY KEY, a INT DEFAULT(f1(1)));]]) +test:do_catchsql_test( + "func-7.1", + [[ + INSERT INTO t01(i) VALUES(1); + ]], { + 1, "function F1() is not available in SQL" + }) + +box.schema.func.create('F2', {language = 'Lua', returns = 'number', body = body, + exports = {'LUA', 'SQL'}}); +box.execute([[CREATE TABLE t02(i INT PRIMARY KEY, a INT DEFAULT(f2(1)));]]) +test:do_catchsql_test( + "func-7.2", + [[ + INSERT INTO t02(i) VALUES(1); + ]], { + 1, "Wrong number of arguments is passed to F2(): expected 0, got 1" + }) + +box.func.F1:drop() +box.func.F2:drop() +box.space.T01:drop() +box.space.T02:drop() + test:finish_test()