[Tarantool-patches] [PATCH v1 5/9] sql: runtime type check for SQL built-in functions
imeevma at tarantool.org
imeevma at tarantool.org
Thu Aug 19 15:03:06 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 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
More information about the Tarantool-patches
mailing list