From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 4BAD16E468; Fri, 10 Sep 2021 19:07:53 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4BAD16E468 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1631290073; bh=3O5ieQrrsR6B0eStb6eKQW7FSrvIpKrlt8TgOhoMNaA=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=PwzOJ1QbW6o7IbXh6OjYzz86SvoYZjb8hpW59wBnOkfZg6UeZpxQQstcBjTIoMZG1 HQRwQHIT+if9GzG2S5yvCL3xRacG8w3qHMBAM12QMbFNdnPwsdZv/kuuMqpf8ZVvVQ SIyAek0LEauDkRGyrL1ysiVLiR70OwXXs3SH/H+s= Received: from smtpng2.i.mail.ru (smtpng2.i.mail.ru [94.100.179.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id B7A776E46B for ; Fri, 10 Sep 2021 19:02:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B7A776E46B Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1mOiyz-00018h-TA; Fri, 10 Sep 2021 19:02:30 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 10 Sep 2021 19:02:29 +0300 Message-Id: <3023d600fc46ecd6158c4a28914576a9c2ccad2d.1631289462.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD91AE02D33A9C88A2FF3F790D2CFB1565D68082CE688654CB400894C459B0CD1B90D77F6A19CAAB9F3C9E7A76923A1CF4259451B28AE1D56597A0345C203294F53 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE70C6BE81AD14BE2BFEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637F4EDC07301919F278638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D86ADE7BFE1B1A709102ABE5214FEB97FB117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8B55B19328CBC4F849A471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7CD1D040B6C1ECEA3F43847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBF3286B3348795C60B68AFEC72B2C0A9F6BA4AB4E814E5BA9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFB4CA5BC574AE2910699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D348BF433665406F3901EF49353595CDC03E0EF7FBC337BDE56B8C005F9B6ECB14D390624292F2A52DB1D7E09C32AA3244C25FC2D9A8E68D820510D5C75F7F9ADE6A90944CA99CF22E3729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojFfmM42hctyQOSQtpfZOzjA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D8AE578CBE4EB9EF81A42970ACEDE621183D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 12/13] sql: remove copying of result in finalizers X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch removes copying of the result in the finalizers of the SQL built-in aggregate functions. Part of #4145 --- src/box/sql/func.c | 149 ++++++++++++++++-------------------------- src/box/sql/sqlInt.h | 12 ---- src/box/sql/vdbe.c | 39 ++++------- src/box/sql/vdbeInt.h | 1 - src/box/sql/vdbeapi.c | 39 ----------- 5 files changed, 67 insertions(+), 173 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 72c1c7828..f1f08e7d6 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -59,35 +59,27 @@ step_sum(struct sql_context *ctx, int argc, struct Mem **argv) { assert(argc == 1); (void)argc; - assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_num(ctx->pMem)); + assert(ctx->pOut->type == MEM_TYPE_NULL || mem_is_num(ctx->pOut)); if (argv[0]->type == MEM_TYPE_NULL) return; - if (ctx->pMem->type == MEM_TYPE_NULL) - return mem_copy_as_ephemeral(ctx->pMem, argv[0]); - if (mem_add(ctx->pMem, argv[0], ctx->pMem) != 0) + if (ctx->pOut->type == MEM_TYPE_NULL) + return mem_copy_as_ephemeral(ctx->pOut, argv[0]); + if (mem_add(ctx->pOut, argv[0], ctx->pOut) != 0) ctx->is_aborted = true; } -/** Finalizer for the SUM() function. */ -static void -fin_sum(struct sql_context *ctx) -{ - assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_num(ctx->pMem)); - mem_copy_as_ephemeral(ctx->pOut, ctx->pMem); -} - /** Implementation of the TOTAL() function. */ static void step_total(struct sql_context *ctx, int argc, struct Mem **argv) { assert(argc == 1); (void)argc; - assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_num(ctx->pMem)); + assert(ctx->pOut->type == MEM_TYPE_NULL || mem_is_num(ctx->pOut)); if (argv[0]->type == MEM_TYPE_NULL) return; - if (ctx->pMem->type == MEM_TYPE_NULL) - mem_set_double(ctx->pMem, 0.0); - if (mem_add(ctx->pMem, argv[0], ctx->pMem) != 0) + if (ctx->pOut->type == MEM_TYPE_NULL) + mem_set_double(ctx->pOut, 0.0); + if (mem_add(ctx->pOut, argv[0], ctx->pOut) != 0) ctx->is_aborted = true; } @@ -95,11 +87,9 @@ step_total(struct sql_context *ctx, int argc, struct Mem **argv) static void fin_total(struct sql_context *ctx) { - assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_double(ctx->pMem)); - if (ctx->pMem->type == MEM_TYPE_NULL) + assert(ctx->pOut->type == MEM_TYPE_NULL || mem_is_double(ctx->pOut)); + if (ctx->pOut->type == MEM_TYPE_NULL) mem_set_double(ctx->pOut, 0.0); - else - mem_copy_as_ephemeral(ctx->pOut, ctx->pMem); } /** Implementation of the AVG() function. */ @@ -108,20 +98,20 @@ step_avg(struct sql_context *ctx, int argc, struct Mem **argv) { assert(argc == 1); (void)argc; - assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_bin(ctx->pMem)); + assert(ctx->pOut->type == MEM_TYPE_NULL || mem_is_bin(ctx->pOut)); if (argv[0]->type == MEM_TYPE_NULL) return; - if (ctx->pMem->type == MEM_TYPE_NULL) { + if (ctx->pOut->type == MEM_TYPE_NULL) { uint32_t size = 2 * sizeof(struct Mem); struct Mem *mems = sqlDbMallocRawNN(sql_get(), size); mem_create(&mems[0]); mem_create(&mems[1]); mem_copy_as_ephemeral(&mems[0], argv[0]); mem_set_uint(&mems[1], 1); - mem_set_bin_allocated(ctx->pMem, (char *)mems, size); + mem_set_bin_allocated(ctx->pOut, (char *)mems, size); return; } - struct Mem *mems = (struct Mem *)ctx->pMem->z; + struct Mem *mems = (struct Mem *)ctx->pOut->z; assert(mems[1].type = MEM_TYPE_UINT); ++mems[1].u.u; if (mem_add(&mems[0], argv[0], &mems[0]) != 0) @@ -132,10 +122,10 @@ step_avg(struct sql_context *ctx, int argc, struct Mem **argv) static void fin_avg(struct sql_context *ctx) { - assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_bin(ctx->pMem)); - if (ctx->pMem->type == MEM_TYPE_NULL) - return mem_set_null(ctx->pOut); - struct Mem *mems = (struct Mem *)ctx->pMem->z; + assert(ctx->pOut->type == MEM_TYPE_NULL || mem_is_bin(ctx->pOut)); + if (ctx->pOut->type == MEM_TYPE_NULL) + return; + struct Mem *mems = (struct Mem *)ctx->pOut->z; if (mem_div(&mems[0], &mems[1], ctx->pOut) != 0) ctx->is_aborted = true; } @@ -145,22 +135,21 @@ static void step_count(struct sql_context *ctx, int argc, struct Mem **argv) { assert(argc == 0 || argc == 1); - if (ctx->pMem->type == MEM_TYPE_NULL) - mem_set_uint(ctx->pMem, 0); + if (ctx->pOut->type == MEM_TYPE_NULL) + mem_set_uint(ctx->pOut, 0); if (argc == 1 && argv[0]->type == MEM_TYPE_NULL) return; - assert(ctx->pMem->type == MEM_TYPE_UINT); - ++ctx->pMem->u.u; + assert(ctx->pOut->type == MEM_TYPE_UINT); + ++ctx->pOut->u.u; } /** Finalizer for the COUNT() function. */ static void fin_count(struct sql_context *ctx) { - assert(ctx->pMem->type == MEM_TYPE_NULL || mem_is_uint(ctx->pMem)); - if (ctx->pMem->type == MEM_TYPE_NULL) + assert(ctx->pOut->type == MEM_TYPE_NULL || mem_is_uint(ctx->pOut)); + if (ctx->pOut->type == MEM_TYPE_NULL) return mem_set_uint(ctx->pOut, 0); - mem_copy_as_ephemeral(ctx->pOut, ctx->pMem); } /** Implementation of the MIN() and MAX() functions. */ @@ -170,12 +159,12 @@ step_minmax(struct sql_context *ctx, int argc, struct Mem **argv) assert(argc == 1); (void)argc; if (argv[0]->type == MEM_TYPE_NULL) { - if (ctx->pMem->type != MEM_TYPE_NULL) + if (ctx->pOut->type != MEM_TYPE_NULL) ctx->skipFlag = 1; return; } - if (ctx->pMem->type == MEM_TYPE_NULL) { - if (mem_copy(ctx->pMem, argv[0]) != 0) + if (ctx->pOut->type == MEM_TYPE_NULL) { + if (mem_copy(ctx->pOut, argv[0]) != 0) ctx->is_aborted = true; return; } @@ -187,22 +176,15 @@ step_minmax(struct sql_context *ctx, int argc, struct Mem **argv) * the only difference between the two being that the sense of the * comparison is inverted. */ - int cmp = mem_cmp_scalar(ctx->pMem, argv[0], ctx->coll); + int cmp = mem_cmp_scalar(ctx->pOut, argv[0], ctx->coll); if ((is_max && cmp < 0) || (!is_max && cmp > 0)) { - if (mem_copy(ctx->pMem, argv[0]) != 0) + if (mem_copy(ctx->pOut, argv[0]) != 0) ctx->is_aborted = true; return; } ctx->skipFlag = 1; } -/** Finalizer for the MIN() and MAX() functions. */ -static void -fin_minmax(struct sql_context *ctx) -{ - mem_copy(ctx->pOut, ctx->pMem); -} - /** Implementation of the GROUP_CONCAT() function. */ static void step_group_concat(struct sql_context *ctx, int argc, struct Mem **argv) @@ -212,8 +194,8 @@ step_group_concat(struct sql_context *ctx, int argc, struct Mem **argv) if (argv[0]->type == MEM_TYPE_NULL) return; assert(mem_is_str(argv[0]) || mem_is_bin(argv[0])); - if (ctx->pMem->type == MEM_TYPE_NULL) { - if (mem_copy_str(ctx->pMem, argv[0]->z, argv[0]->n) != 0) + if (ctx->pOut->type == MEM_TYPE_NULL) { + if (mem_copy_str(ctx->pOut, argv[0]->z, argv[0]->n) != 0) ctx->is_aborted = true; return; } @@ -231,24 +213,17 @@ step_group_concat(struct sql_context *ctx, int argc, struct Mem **argv) sep_len = argv[1]->n; } if (sep_len > 0) { - if (mem_append(ctx->pMem, sep, sep_len) != 0) { + if (mem_append(ctx->pOut, sep, sep_len) != 0) { ctx->is_aborted = true; return; } } - if (mem_append(ctx->pMem, argv[0]->z, argv[0]->n) != 0) { + if (mem_append(ctx->pOut, argv[0]->z, argv[0]->n) != 0) { ctx->is_aborted = true; return; } } -/** Finalizer for the GROUP_CONCAT() function. */ -static void -fin_group_concat(struct sql_context *ctx) -{ - mem_copy(ctx->pOut, ctx->pMem); -} - static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -1960,13 +1935,13 @@ static struct sql_func_definition definitions[] = { NULL}, {"GROUP_CONCAT", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, - step_group_concat, fin_group_concat}, + step_group_concat, NULL}, {"GROUP_CONCAT", 2, {FIELD_TYPE_STRING, FIELD_TYPE_STRING}, - FIELD_TYPE_STRING, step_group_concat, fin_group_concat}, + FIELD_TYPE_STRING, step_group_concat, NULL}, {"GROUP_CONCAT", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, - step_group_concat, fin_group_concat}, + step_group_concat, NULL}, {"GROUP_CONCAT", 2, {FIELD_TYPE_VARBINARY, FIELD_TYPE_VARBINARY}, - FIELD_TYPE_VARBINARY, step_group_concat, fin_group_concat}, + FIELD_TYPE_VARBINARY, step_group_concat, NULL}, {"HEX", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_STRING, hexFunc, NULL}, {"IFNULL", 2, {FIELD_TYPE_ANY, FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, @@ -1997,35 +1972,23 @@ static struct sql_func_definition definitions[] = { {"LOWER", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, LowerICUFunc, NULL}, - {"MAX", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_minmax, - fin_minmax}, - {"MAX", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_minmax, - fin_minmax}, - {"MAX", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, step_minmax, - fin_minmax}, + {"MAX", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_minmax, NULL}, + {"MAX", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_minmax, NULL}, + {"MAX", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, step_minmax, NULL}, {"MAX", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, step_minmax, - fin_minmax}, - {"MAX", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, step_minmax, - fin_minmax}, - {"MAX", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, step_minmax, - fin_minmax}, - {"MAX", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, step_minmax, - fin_minmax}, - - {"MIN", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_minmax, - fin_minmax}, - {"MIN", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_minmax, - fin_minmax}, - {"MIN", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, step_minmax, - fin_minmax}, + NULL}, + {"MAX", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, step_minmax, NULL}, + {"MAX", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, step_minmax, NULL}, + {"MAX", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, step_minmax, NULL}, + + {"MIN", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_minmax, NULL}, + {"MIN", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_minmax, NULL}, + {"MIN", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, step_minmax, NULL}, {"MIN", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, step_minmax, - fin_minmax}, - {"MIN", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, step_minmax, - fin_minmax}, - {"MIN", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, step_minmax, - fin_minmax}, - {"MIN", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, step_minmax, - fin_minmax}, + NULL}, + {"MIN", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, step_minmax, NULL}, + {"MIN", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, step_minmax, NULL}, + {"MIN", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, step_minmax, NULL}, {"NULLIF", 2, {FIELD_TYPE_ANY, FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, nullifFunc, NULL}, @@ -2059,9 +2022,9 @@ static struct sql_func_definition definitions[] = { {"SUBSTR", 3, {FIELD_TYPE_VARBINARY, FIELD_TYPE_INTEGER, FIELD_TYPE_INTEGER}, FIELD_TYPE_VARBINARY, substrFunc, NULL}, - {"SUM", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_sum, fin_sum}, - {"SUM", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_sum, fin_sum}, - {"SUM", 1, {FIELD_TYPE_DECIMAL}, FIELD_TYPE_DECIMAL, step_sum, fin_sum}, + {"SUM", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_sum, NULL}, + {"SUM", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_sum, NULL}, + {"SUM", 1, {FIELD_TYPE_DECIMAL}, FIELD_TYPE_DECIMAL, step_sum, NULL}, {"TOTAL", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_total, fin_total}, {"TOTAL", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_DOUBLE, step_total, @@ -2320,7 +2283,7 @@ sql_built_in_functions_cache_init(void) assert(desc->argc != -1 || dict->argc_min != dict->argc_max); def->param_count = desc->argc; def->returns = desc->result; - def->aggregate = desc->finalize == NULL ? + def->aggregate = (dict->flags & SQL_FUNC_AGG) == 0 ? FUNC_AGGREGATE_NONE : FUNC_AGGREGATE_GROUP; def->language = FUNC_LANGUAGE_SQL_BUILTIN; def->name_len = len; diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 710bd86cf..8782c072a 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -484,18 +484,6 @@ void sql_row_count(struct sql_context *context, MAYBE_UNUSED int unused1, MAYBE_UNUSED sql_value **unused2); -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/vdbe.c b/src/box/sql/vdbe.c index 8804e3d18..1d22c4a40 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -4145,7 +4145,6 @@ case OP_AggStep: { int argc = pOp->p1; sql_context *pCtx; Mem *pMem; - Mem t; assert(pOp->p4type==P4_FUNCCTX); pCtx = pOp->p4.pCtx; @@ -4156,8 +4155,8 @@ case OP_AggStep: { * checks to see if the register array has changed, and if so it * reinitializes the relavant parts of the sql_context object */ - if (pCtx->pMem != pMem) { - pCtx->pMem = pMem; + if (pCtx->pOut != pMem) { + pCtx->pOut = pMem; for(i = 0; i < argc; ++i) pCtx->argv[i] = &aMem[pOp->p2 + i]; } @@ -4169,18 +4168,12 @@ case OP_AggStep: { } #endif - mem_create(&t); - pCtx->pOut = &t; - pCtx->is_aborted = false; pCtx->skipFlag = 0; assert(pCtx->func->def->language == FUNC_LANGUAGE_SQL_BUILTIN); struct func_sql_builtin *func = (struct func_sql_builtin *)pCtx->func; func->call(pCtx, argc, pCtx->argv); - if (pCtx->is_aborted) { - mem_destroy(&t); + if (pCtx->is_aborted) goto abort_due_to_error; - } - assert(mem_is_null(&t)); if (pCtx->skipFlag) { assert(pOp[-1].opcode == OP_SkipLoad); i = pOp[-1].p1; @@ -4207,24 +4200,14 @@ case OP_AggFinal: { struct func_sql_builtin *func = (struct func_sql_builtin *)pOp->p4.func; struct Mem *pIn1 = &aMem[pOp->p1]; - struct sql_context ctx; - memset(&ctx, 0, sizeof(ctx)); - struct Mem t; - memset(&t, 0, sizeof(t)); - t.type = MEM_TYPE_NULL; - assert(t.flags == 0); - t.db = pIn1->db; - ctx.pOut = &t; - ctx.pMem = pIn1; - ctx.func = pOp->p4.func; - func->finalize(&ctx); - assert((pIn1->flags & MEM_Dyn) == 0); - if (pIn1->szMalloc > 0) - sqlDbFree(pIn1->db, pIn1->zMalloc); - memcpy(pIn1, &t, sizeof(t)); - - if (ctx.is_aborted) - goto abort_due_to_error; + if (func->finalize != NULL) { + struct sql_context ctx; + memset(&ctx, 0, sizeof(ctx)); + ctx.pOut = pIn1; + func->finalize(&ctx); + if (ctx.is_aborted) + goto abort_due_to_error; + } UPDATE_MAX_BLOBSIZE(pIn1); if (sqlVdbeMemTooBig(pIn1) != 0) goto too_big; diff --git a/src/box/sql/vdbeInt.h b/src/box/sql/vdbeInt.h index b9a18f1a1..5e2692d06 100644 --- a/src/box/sql/vdbeInt.h +++ b/src/box/sql/vdbeInt.h @@ -172,7 +172,6 @@ struct sql_context { Mem *pOut; /* The return value is stored here */ /* A pointer to function implementation. */ struct func *func; - Mem *pMem; /* Memory cell used to store aggregate context */ struct coll *coll; /* * True, if an error occurred during the execution of the diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 6e598513c..39f560c32 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -349,45 +349,6 @@ sql_context_db_handle(sql_context * p) return p->pOut->db; } -/* - * Allocate or return the aggregate context 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. - */ -void * -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); - if (!mem_is_agg(p->pMem) && mem_set_agg(p->pMem, p->func, nByte) != 0) - return NULL; - void *accum; - if (mem_get_agg(p->pMem, &accum) != 0) - return NULL; - 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