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 85B9E6EC40; Thu, 19 Aug 2021 15:05:29 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 85B9E6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629374729; bh=b/lKAw4Ip4/7qftVu9lTcNCTBaa4WkoM1o8gqIn2ofk=; 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=TL+qGG45/3gaLBBhKnLxs349e/8t7U4q3EnIVOsg62OfblTXpheZkbLcyPIL+WeOs CoPYG9XaDpCTKcEhpcgj/0hSdjfTeqfRHQz4D+XMJT0bfFfbQJQCV4dT+40qnd3Uie jh8FpCRKnQRPxDmts7FgcElxf5LqDKQxEPuHpWaw= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (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 174316EC45 for ; Thu, 19 Aug 2021 15:03:08 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 174316EC45 Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mGglG-000136-Pd; Thu, 19 Aug 2021 15:03:07 +0300 To: kyukhin@tarantool.org, v.ioffe@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 19 Aug 2021 15:03:06 +0300 Message-Id: <04735e8922e02e5576a6563d4db155f4ad9dfa48.1629374449.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: 4F1203BC0FB41BD92087353F0EC44DD906AB4890CDABF0C5CB76CEE71D3E4007182A05F538085040D5672EB7FC508CBD9A1CF4AD7DC499DA77F12C8BA7D1BB2E305AF3CC97949745 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7599DC91D9EF62C21EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006370CE4B4B08BC34B6C8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D88EF746D66F6DF8C0C65C101C7751231D117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD182CC0D3CB04F14752D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6300D3B61E77C8D3B089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CF160826E4E1956AEF912A92165F396E0EB28C484BFD358EE9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF836F5ADB0B4F9314699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D340DAE5B306C240CF52CD7BC2D14D4E3CAC78E3B195D618C1ADE5D40A589FF1D43B10E4E798B9663A31D7E09C32AA3244C98E0AD6C68BC7CB86942CE1CB0FA0788D9ADFF0C0BDB8D1FFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojGSxK+6r6oBEsKM748f89aQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D3578ED729DF4D46373C217185B3D925883D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 5/9] 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 8bd9a858e..88f476794 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) @@ -4076,6 +4099,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 74febd182..0aca76112 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1526,7 +1526,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 6ae0cebe7..2fe38e319 100644 --- a/src/box/sql/select.c +++ b/src/box/sql/select.c @@ -5617,6 +5617,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 35dee3ec1..d78076868 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -4401,6 +4401,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