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 33E126EC45; Fri, 13 Aug 2021 06:19:05 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 33E126EC45 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628824745; bh=ZyzYzzI2nRDfrfNTfhwILB5vkV0mci/X9KjOkIwlnGQ=; 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=Gbah9HHWGo0FlyeO0wxuhUNr3KuKTCA6mACBpolG9hdYsk6W+jFlEEQtATqQ4Ktmr MOt7jAeoE5usLsPSPT+yOqaR4/wIOjf3fiG9qgW2VpJ1TBwZfOF//RSmcRD+Co882Z vf96B+iTxjJVOME1QRBty7bggNUKcpsPW1YygvYo= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 862CE6F3D8 for ; Fri, 13 Aug 2021 06:17:16 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 862CE6F3D8 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mENh5-0003FO-N6; Fri, 13 Aug 2021 06:17:16 +0300 To: vdavydov@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 13 Aug 2021 06:17:15 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD91BCCB18F2C129F87F36E61E9E4584E9D182A05F538085040BB25AA8BFDCE84E29D4213C30AEAE3436060E9C09D00FB3ABB56781909B0764A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE79FB50E46F38499D1EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063707C1E244DAE02CF68638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D80782D217BC40556A17D7EC00F027411F117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18F04B652EEC242312D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5059A91936EF7B6732D4DAB4A962E748491 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86C612B5E54871F7FE9B7B9689BEFE272499C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFE37876E7723AB534DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D347324AA9FA07FF01ED770868383ADC3910C6276956A7D1600D08971B61C6E453692C71023ED797D6F1D7E09C32AA3244C5C8BD8C245E248E541BE51DDAC1B2F1BCE0B41342B755BCD729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkyZFt6ENMUkgQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D0A975FD7B828A9FEBB9184AB2E32B2EC83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 04/10] sql: static type check for SQL built-in functions 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 a static type checking mechanism for SQL built-in functions. However, it is currently disabled as the functions themselves need to be prepared for such changes. Part of #6105 --- src/box/sql/expr.c | 6 ++- src/box/sql/func.c | 119 +++++++++++++++++++++++++++++++++++++++--- src/box/sql/prepare.c | 2 +- src/box/sql/select.c | 6 ++- 4 files changed, 123 insertions(+), 10 deletions(-) diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c index c67a7091c..fb778f7e3 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -5390,7 +5390,11 @@ analyzeAggregate(Walker * pWalker, Expr * pExpr) (pExpr, EP_IntValue)); pItem->func = sql_func_find(pExpr); - assert(pItem->func != NULL); + if (pItem->func == NULL) { + pParse->is_aborted = + true; + return WRC_Abort; + } assert(pItem->func->def-> language == FUNC_LANGUAGE_SQL_BUILTIN && diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 27e38ec16..b5e6bfe2a 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -2099,6 +2099,106 @@ built_in_func_put(struct sql_func_dictionary *dict) } } +/** + * Check if there is no need to cast argument to accepted type. Also, in some + * cases operation 'op' may be important, for example when given argument is + * NULL or is variable. + * + * Returns TRUE when: + * - when operation is NULL; + * - when accepted type and argument type are equal; + * - when accepted type is ANY; + * - when accepted type is INTEGER and argument type is UNSIGNED. + */ +static inline bool +is_exact(int op, enum field_type a, enum field_type b) +{ + return op == TK_NULL || a == b || a == FIELD_TYPE_ANY || + (a == FIELD_TYPE_INTEGER && b == FIELD_TYPE_UNSIGNED); +} + +/** + * Check if the argument MEM type will not change during cast. It means that + * either is_exact() returns TRUE or accepted type is metatype that includes + * argument type. + * + * Returns TRUE when: + * - is_exact() returns TRUE; + * - when accepted type is NUMBER and argument type is numeric type; + * - when accepted type is SCALAR and argument type is not MAP or ARRAY. + */ +static inline bool +is_upcast(int op, enum field_type a, enum field_type b) +{ + return is_exact(op, a, b) || + (a == FIELD_TYPE_NUMBER && sql_type_is_numeric(b)) || + (a == FIELD_TYPE_SCALAR && b != FIELD_TYPE_MAP && + b != FIELD_TYPE_ARRAY); +} + +/** + * Check if there is a chance that the argument can be cast to accepted type + * according to implicit cast rules. + * + * Returns TRUE when: + * - is_upcast() returns TRUE; + * - when accepted type and argument type are numeric types; + * - when argument is binded value; + * - when argument type is ANY, which means that is was not resolved. + */ +static inline bool +is_castable(int op, enum field_type a, enum field_type b) +{ + return is_upcast(op, a, b) || op == TK_VARIABLE || + (sql_type_is_numeric(a) && sql_type_is_numeric(b)) || + b == FIELD_TYPE_ANY; +} + +enum check_type { + CHECK_TYPE_EXACT, + CHECK_TYPE_UPCAST, + CHECK_TYPE_CASTABLE, +}; + +static struct func * +find_compatible(struct Expr *expr, struct sql_func_dictionary *dict, + enum check_type check) +{ + int n = expr->x.pList != NULL ? expr->x.pList->nExpr : 0; + for (uint32_t i = 0; i < dict->count; ++i) { + struct func_sql_builtin *func = dict->functions[i]; + int argc = func->base.def->param_count; + if (argc != n && argc != -1) + continue; + if (n == 0) + return &func->base; + + enum field_type *types = func->param_list; + bool is_match = true; + for (int j = 0; j < n && is_match; ++j) { + struct Expr *e = expr->x.pList->a[j].pExpr; + enum field_type a = types[argc != -1 ? j : 0]; + enum field_type b = sql_expr_type(e); + switch (check) { + case CHECK_TYPE_EXACT: + is_match = is_exact(e->op, a, b); + break; + case CHECK_TYPE_UPCAST: + is_match = is_upcast(e->op, a, b); + break; + case CHECK_TYPE_CASTABLE: + is_match = is_castable(e->op, a, b); + break; + default: + unreachable(); + } + } + if (is_match) + return &func->base; + } + return NULL; +} + static struct func * find_built_in_func(struct Expr *expr, struct sql_func_dictionary *dict) { @@ -2117,13 +2217,18 @@ find_built_in_func(struct Expr *expr, struct sql_func_dictionary *dict) diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, name, str, n); return NULL; } - struct func *func = NULL; - for (uint32_t i = 0; i < dict->count; ++i) { - func = &dict->functions[i]->base; - if (func->def->param_count == n) - break; - } - return func; + struct func *func = find_compatible(expr, dict, CHECK_TYPE_EXACT); + if (func != NULL) + return func; + func = find_compatible(expr, dict, CHECK_TYPE_UPCAST); + if (func != NULL) + return func; + func = find_compatible(expr, dict, CHECK_TYPE_CASTABLE); + if (func != NULL) + return func; + diag_set(ClientError, ER_SQL_EXECUTE, + tt_sprintf("wrong arguments for function %s()", name)); + return NULL; } struct func * diff --git a/src/box/sql/prepare.c b/src/box/sql/prepare.c index d859a0243..b986630e9 100644 --- a/src/box/sql/prepare.c +++ b/src/box/sql/prepare.c @@ -89,7 +89,7 @@ sql_stmt_compile(const char *zSql, int nBytes, struct Vdbe *pReprepare, } else { sqlRunParser(&sParse, zSql); } - assert(0 == sParse.nQueryLoop); + assert(0 == sParse.nQueryLoop || sParse.is_aborted); if (db->mallocFailed) sParse.is_aborted = true; diff --git a/src/box/sql/select.c b/src/box/sql/select.c index a26ca54a3..774d06fa9 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -6264,7 +6264,7 @@ sqlSelect(Parse * pParse, /* The parser context */ sNC.ncFlags &= ~NC_InAggFunc; } sAggInfo.mxReg = pParse->nMem; - if (db->mallocFailed) + if (pParse->is_aborted) goto select_end; /* Processing for aggregates with GROUP BY is very different and @@ -6473,6 +6473,8 @@ sqlSelect(Parse * pParse, /* The parser context */ */ sqlVdbeJumpHere(v, addr1); updateAccumulator(pParse, &sAggInfo); + if (pParse->is_aborted) + goto select_end; sqlVdbeAddOp2(v, OP_Integer, 1, iUseFlag); VdbeComment((v, "indicate data in accumulator")); @@ -6636,6 +6638,8 @@ sqlSelect(Parse * pParse, /* The parser context */ goto select_end; } updateAccumulator(pParse, &sAggInfo); + if (pParse->is_aborted) + goto select_end; assert(pMinMax == 0 || pMinMax->nExpr == 1); if (sqlWhereIsOrdered(pWInfo) > 0) { sqlVdbeGoto(v, -- 2.25.1