From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: kyukhin@tarantool.org, v.ioffe@tarantool.org Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH v1 5/9] sql: runtime type check for SQL built-in functions Date: Thu, 19 Aug 2021 15:03:06 +0300 [thread overview] Message-ID: <04735e8922e02e5576a6563d4db155f4ad9dfa48.1629374449.git.imeevma@gmail.com> (raw) In-Reply-To: <cover.1629374448.git.imeevma@gmail.com> 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
next prev parent reply other threads:[~2021-08-19 12:05 UTC|newest] Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-08-19 12:02 [Tarantool-patches] [PATCH v1 0/9] Check types of SQL built-in functions arguments Mergen Imeev via Tarantool-patches 2021-08-19 12:02 ` [Tarantool-patches] [PATCH v1 1/9] sql: modify signature of TRIM() Mergen Imeev via Tarantool-patches 2021-08-19 12:02 ` [Tarantool-patches] [PATCH v1 2/9] sql: rework SQL built-in functions hash table Mergen Imeev via Tarantool-patches 2021-08-19 12:03 ` [Tarantool-patches] [PATCH v1 3/9] sql: check number of arguments during parsing Mergen Imeev via Tarantool-patches 2021-08-19 12:03 ` [Tarantool-patches] [PATCH v1 4/9] sql: static type check for SQL built-in functions Mergen Imeev via Tarantool-patches 2021-08-19 12:03 ` Mergen Imeev via Tarantool-patches [this message] 2021-08-19 12:03 ` [Tarantool-patches] [PATCH v1 6/9] sql: enable types checking for some functions Mergen Imeev via Tarantool-patches 2021-08-19 12:03 ` [Tarantool-patches] [PATCH v1 7/9] sql: fix result type of min() and max() functions Mergen Imeev via Tarantool-patches 2021-08-19 12:03 ` [Tarantool-patches] [PATCH v1 8/9] sql: check argument types of sum(), avg(), total() Mergen Imeev via Tarantool-patches 2021-08-19 12:03 ` [Tarantool-patches] [PATCH v1 9/9] sql: arguments check for string value functions Mergen Imeev via Tarantool-patches 2021-08-19 12:26 ` [Tarantool-patches] [PATCH v1 0/9] Check types of SQL built-in functions arguments Kirill Yukhin via Tarantool-patches 2021-08-19 15:50 ` Vitaliia Ioffe via Tarantool-patches 2021-08-19 16:16 ` Kirill Yukhin via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=04735e8922e02e5576a6563d4db155f4ad9dfa48.1629374449.git.imeevma@gmail.com \ --to=tarantool-patches@dev.tarantool.org \ --cc=imeevma@tarantool.org \ --cc=kyukhin@tarantool.org \ --cc=v.ioffe@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v1 5/9] sql: runtime type check for SQL built-in functions' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox