Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: vdavydov@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 05/10] sql: runtime type check for SQL built-in functions
Date: Fri, 13 Aug 2021 06:17:17 +0300	[thread overview]
Message-ID: <c6145b44018fac15cd4a45d116769cb4a2f6b67e.1628824421.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1628824421.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 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


  parent reply	other threads:[~2021-08-13  3:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-13  3:17 [Tarantool-patches] [PATCH v1 00/10] Check types of SQL built-in functions arguments Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 01/10] sql: modify signature of TRIM() Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 02/10] sql: rework SQL built-in functions hash table Mergen Imeev via Tarantool-patches
2021-08-16 13:53   ` Vladimir Davydov via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 03/10] sql: check number of arguments during parsing Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 04/10] sql: static type check for SQL built-in functions Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` Mergen Imeev via Tarantool-patches [this message]
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 06/10] sql: enable types checking for some functions Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 07/10] sql: fix result type of min() and max() functions Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 08/10] sql: check argument types of sum(), avg(), total() Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 09/10] sql: fix quote() function Mergen Imeev via Tarantool-patches
2021-08-13  3:17 ` [Tarantool-patches] [PATCH v1 10/10] sql: arguments check for string value functions Mergen Imeev via Tarantool-patches
2021-08-19 11:49 ` [Tarantool-patches] [PATCH v1 00/10] Check types of SQL built-in functions arguments Vladimir Davydov 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=c6145b44018fac15cd4a45d116769cb4a2f6b67e.1628824421.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 05/10] 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