Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v2 1/3] sql: initialize MEM used in aggregate functions
Date: Tue, 27 Apr 2021 19:55:22 +0300
Message-ID: <05d176765118a7357b5b15d7fcfc08104463522c.1619542456.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1619542456.git.imeevma@gmail.com>

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


       reply	other threads:[~2021-04-27 16:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1619542456.git.imeevma@gmail.com>
2021-04-27 16:55 ` Mergen Imeev via Tarantool-patches [this message]
2021-04-29 21:02   ` Vladislav Shpilevoy via Tarantool-patches
2021-05-17 11:38     ` Mergen Imeev via Tarantool-patches
2021-04-27 16:55 ` [Tarantool-patches] [PATCH v2 2/3] sql: make mem_is_bin() to check only for VARBINARY Mergen Imeev via Tarantool-patches
2021-04-29 21:05   ` Vladislav Shpilevoy via Tarantool-patches
2021-05-17 12:05     ` Mergen Imeev 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=05d176765118a7357b5b15d7fcfc08104463522c.1619542456.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    /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

Tarantool development patches archive

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://lists.tarantool.org/tarantool-patches/0 tarantool-patches/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 tarantool-patches tarantool-patches/ https://lists.tarantool.org/tarantool-patches \
		tarantool-patches@dev.tarantool.org.
	public-inbox-index tarantool-patches

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git