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 48B786EC40; Fri, 13 Aug 2021 06:19:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 48B786EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628824776; bh=LcOqs7rB9kT7lHvHOR/Vj+rd0EAGpCEVGuV/M6fGLxk=; 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=zjGQMn9W9xSfWMPGFEZMYrGpFeYw3qiFMCm3dMYEYZWlkPy11aqiA34NoX9E5kxyR o246w4AWKL4rBnr9Z0ZuLiq5DLjkoRvKgTw+h2zPnMSOdHk+zHI9FjQQ1mBCfIpgmR 76vLj3wX3A+mCGBI947d7/fQR+6E2O2/CLViEckA= 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 2E48A6F3E3 for ; Fri, 13 Aug 2021 06:17:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2E48A6F3E3 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mENh7-0003GA-Gj; Fri, 13 Aug 2021 06:17:17 +0300 To: vdavydov@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 13 Aug 2021 06:17:17 +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: 4F1203BC0FB41BD92087353F0EC44DD91BCCB18F2C129F87F36E61E9E4584E9D182A05F5380850406E0321632F96CC84F80EB6E0114A551CC69FC8736A90A4B043FDFFA08B0F9229 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7C3943609F29D73B3EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C5BAFDCCAC60DA968638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D869EAFE6275A5D958404BF1750EAD9D59117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18618001F51B5FD3F9D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86CE8D253C15116EFE46B5E9EA93BD2A76A9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFE37876E7723AB534DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3455AC8BF8E3153BA07B4954153586E6EB3ECB9932B58F280F87842F4507E525ED0B8F76CDD2BC2C661D7E09C32AA3244CC3D28F5DB89DD6C80A41E9EE4549433E1E098CBE561D6343729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkxtv16FCx5hog== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DD4B4DEDA9A29ECE6027DBC3CE7A291A183D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 05/10] sql: runtime 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 runtime 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 | 27 +++++++++++++++++++++++++++ src/box/sql/mem.c | 2 +- src/box/sql/select.c | 4 ++++ src/box/sql/sqlInt.h | 4 ++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/box/sql/expr.c b/src/box/sql/expr.c index fb778f7e3..47005f8e3 100644 --- a/src/box/sql/expr.c +++ b/src/box/sql/expr.c @@ -137,6 +137,29 @@ sql_expr_type(struct Expr *pExpr) return pExpr->type; } +int +sql_emit_args_types(struct Vdbe *v, int reg, struct func *base, uint32_t argc) +{ + if (argc == 0 || base->def->language != FUNC_LANGUAGE_SQL_BUILTIN) + return 0; + struct func_sql_builtin *func = (struct func_sql_builtin *)base; + if (func->base.def->param_count > 0) { + sqlVdbeAddOp4(v, OP_ApplyType, reg, argc, 0, + (char *)func->param_list, P4_STATIC); + return 0; + } + assert(func->base.def->param_count == -1); + uint32_t size = argc * sizeof(enum field_type); + enum field_type *types = sqlDbMallocRawNN(sql_get(), size); + if (types == NULL) + return -1; + enum field_type type = func->param_list[0]; + for (uint32_t i = 0; i < argc; ++i) + types[i] = type; + sqlVdbeAddOp4(v, OP_ApplyType, reg, argc, 0, (char *)types, P4_DYNAMIC); + return 0; +} + enum field_type * field_type_sequence_dup(struct Parse *parse, enum field_type *types, uint32_t len) @@ -4072,6 +4095,10 @@ sqlExprCodeTarget(Parse * pParse, Expr * pExpr, int target) } else { r1 = 0; } + if (sql_emit_args_types(v, r1, func, nFarg) != 0) { + pParse->is_aborted = true; + return 0; + } if (sql_func_flag_is_set(func, SQL_FUNC_NEEDCOLL)) { sqlVdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)coll, P4_COLLSEQ); diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 066940fac..55131e684 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1216,7 +1216,7 @@ mem_cast_explicit(struct Mem *mem, enum field_type type) int mem_cast_implicit(struct Mem *mem, enum field_type type) { - if (mem->type == MEM_TYPE_NULL) + if (mem->type == MEM_TYPE_NULL || type == FIELD_TYPE_ANY) return 0; if ((mem->flags & MEM_Scalar) != 0 && type != FIELD_TYPE_SCALAR) return -1; diff --git a/src/box/sql/select.c b/src/box/sql/select.c index 774d06fa9..33cebf28a 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -5566,6 +5566,10 @@ updateAccumulator(Parse * pParse, AggInfo * pAggInfo) vdbe_insert_distinct(pParse, pF->iDistinct, pF->reg_eph, addrNext, 1, regAgg); } + if (sql_emit_args_types(v, regAgg, pF->func, nArg) != 0) { + pParse->is_aborted = true; + return; + } if (sql_func_flag_is_set(pF->func, SQL_FUNC_NEEDCOLL)) { struct coll *coll = NULL; struct ExprList_item *pItem; diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index b08ee8f68..11a685d98 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -4412,6 +4412,10 @@ sql_func_flag_is_set(struct func *func, uint16_t flag) struct func * sql_func_find(struct Expr *expr); +/** Code an OP_ApplyType opcode that will force types onto arguments. */ +int +sql_emit_args_types(struct Vdbe *v, int reg, struct func *base, uint32_t argc); + /** * Return the parameters of the function with the given name. If the function * with the given name does not exist, or the function is not a built-in SQL -- 2.25.1