[Tarantool-patches] [PATCH v2 1/3] sql: initialize MEM used in aggregate functions

imeevma at tarantool.org imeevma at tarantool.org
Tue Apr 27 19:55:22 MSK 2021


This patch adds proper initialization for the MEM, which is used in the
aggregate functions min() and max().

Part of #4906
---
 src/box/sql/func.c    | 21 ++++++++++-----------
 src/box/sql/sqlInt.h  |  7 +++++++
 src/box/sql/vdbeapi.c | 20 ++++++++++++++++++++
 3 files changed, 37 insertions(+), 11 deletions(-)

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 9c28d5122..d282b2cea 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -1766,14 +1766,14 @@ minmaxStep(sql_context * context, int NotUsed, sql_value ** argv)
 
 	struct func_sql_builtin *func =
 		(struct func_sql_builtin *)context->func;
-	pBest = (Mem *) sql_aggregate_context(context, sizeof(*pBest));
+	pBest = sql_context_agg_mem(context);
 	if (!pBest)
 		return;
 
 	if (mem_is_null(argv[0])) {
-		if (pBest->flags)
+		if (!mem_is_null(pBest))
 			sqlSkipAccumulatorLoad(context);
-	} else if (pBest->flags) {
+	} else if (!mem_is_null(pBest)) {
 		int cmp;
 		struct coll *pColl = sqlGetFuncCollSeq(context);
 		/*
@@ -1798,14 +1798,13 @@ minmaxStep(sql_context * context, int NotUsed, sql_value ** argv)
 static void
 minMaxFinalize(sql_context * context)
 {
-	sql_value *pRes;
-	pRes = (sql_value *) sql_aggregate_context(context, 0);
-	if (pRes) {
-		if (pRes->flags) {
-			sql_result_value(context, pRes);
-		}
-		mem_destroy(pRes);
-	}
+	struct Mem *mem = context->pMem;
+	struct Mem *res;
+	if (!mem_is_agg(mem) || mem_get_agg(mem, (void **)&res) != 0)
+		return;
+	if (!mem_is_null(res))
+		sql_result_value(context, res);
+	mem_destroy(res);
 }
 
 /*
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index b548ddad4..ef8dcd693 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -484,6 +484,13 @@ void *
 sql_aggregate_context(sql_context *,
 			  int nBytes);
 
+/**
+ * Allocate or return the aggregate context containing struct MEM for a user
+ * function. A new context is allocated on the first call. Subsequent calls
+ * return the same context that was returned on prior calls.
+ */
+struct Mem *
+sql_context_agg_mem(struct sql_context *context);
 
 int
 sql_column_count(sql_stmt * pStmt);
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 655743fb1..aaae12e41 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -394,6 +394,26 @@ sql_aggregate_context(sql_context * p, int nByte)
 	return accum;
 }
 
+struct Mem *
+sql_context_agg_mem(struct sql_context *ctx)
+{
+	assert(ctx != NULL && ctx->func != NULL);
+	assert(ctx->func->def->language == FUNC_LANGUAGE_SQL_BUILTIN);
+	assert(ctx->func->def->aggregate == FUNC_AGGREGATE_GROUP);
+	struct Mem *mem;
+	if (!mem_is_agg(ctx->pMem)) {
+		if (mem_set_agg(ctx->pMem, ctx->func, sizeof(*mem)) != 0)
+			return NULL;
+		if (mem_get_agg(ctx->pMem, (void **)&mem) != 0)
+			return NULL;
+		mem_create(mem);
+		return mem;
+	}
+	if (mem_get_agg(ctx->pMem, (void **)&mem) != 0)
+		return NULL;
+	return mem;
+}
+
 /*
  * Return the number of columns in the result set for the statement pStmt.
  */
-- 
2.25.1



More information about the Tarantool-patches mailing list