[Tarantool-patches] [PATCH v4 38/53] sql: introduce mem_*_aggregate()
imeevma at tarantool.org
imeevma at tarantool.org
Tue Mar 23 12:36:17 MSK 2021
This patch introduce mem_prepare_aggregare() and mem_get_aggregate() functions.
These functions are used during execution of aggregate functions.
Part of #5818
---
src/box/sql/mem.c | 23 +++++++++++++++++++++++
src/box/sql/mem.h | 6 ++++++
src/box/sql/vdbeapi.c | 33 ++++-----------------------------
3 files changed, 33 insertions(+), 29 deletions(-)
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 078de0e62..0211069c6 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -613,6 +613,29 @@ mem_set_frame(struct Mem *mem, struct VdbeFrame *frame)
mem->field_type = field_type_MAX;
}
+int
+mem_prepare_aggregate(struct Mem *mem, struct func *func, int size)
+{
+ if (size <= 0) {
+ mem_clear(mem);
+ return 0;
+ }
+ if (sqlVdbeMemGrow(mem, size, 0) != 0)
+ return -1;
+ memset(mem->z, 0, size);
+ mem->n = size;
+ mem->flags = MEM_Agg;
+ mem->u.func = func;
+ mem->field_type = field_type_MAX;
+ return 0;
+}
+
+void *
+mem_get_aggregate(struct Mem *mem)
+{
+ return (void *)mem->z;
+}
+
int
mem_copy(struct Mem *to, const struct Mem *from)
{
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index a6c53c615..51802b450 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -271,6 +271,12 @@ mem_set_pointer(struct Mem *mem, void *ptr);
void
mem_set_frame(struct Mem *mem, struct VdbeFrame *frame);
+int
+mem_prepare_aggregate(struct Mem *mem, struct func *func, int size);
+
+void *
+mem_get_aggregate(struct Mem *mem);
+
int
mem_copy(struct Mem *to, const struct Mem *from);
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 3ad89b87c..8f871ea95 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -403,29 +403,6 @@ sqlStmtCurrentTime(sql_context * p)
return *piTime;
}
-/*
- * Create a new aggregate context for p and return a pointer to
- * its pMem->z element.
- */
-static SQL_NOINLINE void *
-createAggContext(sql_context * p, int nByte)
-{
- Mem *pMem = p->pMem;
- assert(!mem_is_aggregate(pMem));
- if (nByte <= 0) {
- mem_set_null(pMem);
- pMem->z = 0;
- } else {
- sqlVdbeMemClearAndResize(pMem, nByte);
- pMem->flags = MEM_Agg;
- pMem->u.func = p->func;
- if (pMem->z) {
- memset(pMem->z, 0, nByte);
- }
- }
- return (void *)pMem->z;
-}
-
/*
* Allocate or return the aggregate context for a user function. A new
* context is allocated on the first call. Subsequent calls return the
@@ -437,12 +414,10 @@ sql_aggregate_context(sql_context * p, int nByte)
assert(p != NULL && p->func != NULL);
assert(p->func->def->language == FUNC_LANGUAGE_SQL_BUILTIN);
assert(p->func->def->aggregate == FUNC_AGGREGATE_GROUP);
- testcase(nByte < 0);
- if (!mem_is_aggregate(p->pMem)) {
- return createAggContext(p, nByte);
- } else {
- return (void *)p->pMem->z;
- }
+ if (!mem_is_aggregate(p->pMem) &&
+ mem_prepare_aggregate(p->pMem, p->func, nByte) != 0)
+ return NULL;
+ return mem_get_aggregate(p->pMem);
}
/*
--
2.25.1
More information about the Tarantool-patches
mailing list