From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 340646EC41; Mon, 9 Aug 2021 10:19:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 340646EC41 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628493595; bh=lnXBIqDtF5JQ63TKSpjizg3AOyrXDUsXhsOvYn7qGXo=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=CIYL9+TJAHjYR6O+CM0iTD6Zr1RWbZEQEhjLpwpD4XdBROCfxfRw1Qfh17XDjG1v/ mmgaI/a4ZRUlJRjUNy5sa8rzFVthLYkU+n69MaMs5jo9upwQ2hivIsK9l0QVTg/W5h v0lUwRmZjKK75h5SghoLN9S7Fxfa+YajbNzonZsE= Received: from smtp57.i.mail.ru (smtp57.i.mail.ru [217.69.128.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 000EE6EC41 for ; Mon, 9 Aug 2021 10:18:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 000EE6EC41 Received: by smtp57.i.mail.ru with esmtpa (envelope-from ) id 1mCzYm-0001j2-TY; Mon, 09 Aug 2021 10:18:57 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Mon, 9 Aug 2021 10:18:56 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD94BB7C0677F3ED9D05EB08EAFC9298EA6182A05F538085040F38FF9199D0CD6EF0D2F3AD20490B5274837DF3669EFD37141ED27AAF746CF40 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE775092E6AE21AB47AC2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE757F64E7FD849EB4FEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F25D3C29D24EAFF097FDBCB589118A1273CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F7900637F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA73AA81AA40904B5D9A18204E546F3947CD72B8CFDDF92C982302FCEF25BFAB3454AD6D5ED66289B52698AB9A7B718F8C46E0066C2D8992A16725E5C173C3A84C3AEB4DDBFD72C8FA176E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7C289736CE4F78F08343847C11F186F3C59DAA53EE0834AAEE X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B505D8E9F0B02E82798D6246B31C8BC6C2BB X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C4C7A0BC55FA0FE5FCB7EDD5838D525CDDD4EBC64C52B35122A4D3AC2B3E9D84F8B1881A6453793CE9C32612AADDFBE061C61BE10805914D3804EBA3D8E7E5B87ABF8C51168CD8EBDBF80095D1ED7F4578DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346F44602F3B4946345CD0A5B9E6253A2AD8EA5D77C74BD7BD32B6303C2E7D545FAE60DFE9409FF1551D7E09C32AA3244C324F6DD768A81EC18A8823BC2542F4669CA7333006C390A0729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMTMPlNJj3She6496brMwrQ== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED5EAA38F5AE8F69470D2F3AD20490B527E28E2421BF3B429A5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v2 2/6] sql: introduce sql_func_find() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch introduces the sql_func_find() function. This function allows us to centralize the look up of functions during parsing, which simplifies code and fixes some incorrect error messages. Part of #6106 --- src/box/sql/analyze.c | 12 ++++++++++++ src/box/sql/expr.c | 14 ++------------ src/box/sql/func.c | 38 ++++++++++++++++++++++++------------- src/box/sql/resolve.c | 22 +-------------------- src/box/sql/sqlInt.h | 12 ++---------- src/box/sql/vdbemem.c | 2 +- test/sql-tap/func5.test.lua | 34 ++++++++++++++++++++++++++++++++- 7 files changed, 76 insertions(+), 58 deletions(-) diff --git a/src/box/sql/analyze.c b/src/box/sql/analyze.c index b87f69512..afa2331a1 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 80f2d349a..20d22455c 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -3957,7 +3957,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; @@ -3970,11 +3969,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; } @@ -5431,14 +5427,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 28d383293..7cdcce6bc 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1934,24 +1934,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) @@ -2655,6 +2643,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 11b6139e3..35faddab5 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 a92de0a2f..c6927e1e4 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -4362,17 +4362,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 2c5099616..499089c8d 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 9b1526aaf..13698582b 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 @@ -314,4 +314,36 @@ test:do_execsql_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() -- 2.25.1