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 136846EC40; Thu, 19 Aug 2021 15:04:59 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 136846EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629374699; bh=oTi7QN/HBdEf5FpqyvYoFZ7rdjYBRb0OE1JFu/ym/Hg=; 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=WqP2Uzf+FhORaz+DpSW0JWAscKnqqULdD9j0xGc/lDoqyS55yUlwL6FWoPISWRjOJ l5h7wiJF+lUn7IsaK8fBTUyG4NYNBWPMGg4VaM8noRaDVKmA8ots4nuJNS4ejIPUOx UoHMsY7nsp0Vr9NdFs6l/JCxQM1oAvYQhd2e4lig= 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 D08EF6EC43 for ; Thu, 19 Aug 2021 15:03:05 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D08EF6EC43 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mGglE-0006Y1-U8; Thu, 19 Aug 2021 15:03:05 +0300 To: kyukhin@tarantool.org, v.ioffe@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 19 Aug 2021 15:03:04 +0300 Message-Id: <73e3ea57b144156d563034c00c646a7d65d29cee.1629374448.git.imeevma@gmail.com> 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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9736CF3E71F18CE0C3E1D5927724F4AAA182A05F538085040120F35A990032CB74B9EEE63D40D22C6B64A36E8D223F6EDE41A917FB11704CD X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7CC7DB65F82284D26EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637790A9327A9AFEF4F8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8FFCD1EF0A9EE034B402C974526E0A962117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD186FD1C55BDD38FC3FD2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6300D3B61E77C8D3B089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CF160826E4E1956AE03523E7B8765D4BD68040AAEBAD985839C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF836F5ADB0B4F9314699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A246DFF68F0EE19E41E6ECC0783D322BD5D4359B52F27E66A16726C293A3527EBA9FA4B000DB96D21D7E09C32AA3244C98E2AD6C68BC7CB8D7FFD7BB5745477B3C6EB905E3A8056BFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojGSxK+6r6oBGp3J2hmFKHZA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D8218CE3326DBDE0F365409B386F299E683D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 4/9] 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 1304710a1..8bd9a858e 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -5394,7 +5394,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 4d893affc..93f6cc067 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -2090,6 +2090,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) { @@ -2108,13 +2208,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 021e0ebd5..6ae0cebe7 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -6279,7 +6279,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 @@ -6488,6 +6488,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")); @@ -6651,6 +6653,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