[Tarantool-patches] [PATCH v1 05/10] sql: runtime type check for SQL built-in functions

imeevma at tarantool.org imeevma at tarantool.org
Fri Aug 13 06:17:17 MSK 2021


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



More information about the Tarantool-patches mailing list