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 39DF76DB05; Fri, 1 Oct 2021 10:46:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 39DF76DB05 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633074409; bh=jWe1Qxq7qTDSioonu7n6j3HAWMbOuW24wRSFFqwCuLY=; 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=FvZm7fUKltUpIOp9OJfm56g6/ZPeN8RJrltstMx9ZMfFeTvNCHoo6o6E1+PXWDGmw msd+tSVQaVFaXp46oj6rb/UtCPy19ZgTAitRtBt+Jbe/R+e+WezJlFEpAm+V7bFTNj KR8sixcuNnn+0XZe73ZWMI1IHwucNO2QtLH+vt0A= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 A92F274147 for ; Fri, 1 Oct 2021 10:43:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A92F274147 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mWDC8-0004d6-L5; Fri, 01 Oct 2021 10:43:01 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 1 Oct 2021 10:43:00 +0300 Message-Id: <5b43feb44fe656d099486babe4d348953120b150.1633073759.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: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD96A58C36AA2E996499DED20B83AFF2DE94FA0C0F669C426FD182A05F5380850408EB7FF176C0302DE85E3A142B859AA9173FB07E3F56A1E5EC99827C7158F9A26 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE777D9B7C31E2C1495EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637205505A8D8EF484BEA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F2DD126BB56E49FF85826AE808DED63C86CC7F00164DA146DAFE8445B8C89999728AA50765F7900637F924B32C592EA89F389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8989FD0BDF65E50FBF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B50512C3F35F3A29A9BFD6846BB425FF0246 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBBB17C150BCA6793DA76DF361475BB6983E5EE5E195170399C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF783E2B6F79C23BED699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3480A9008907CB2FD04D615C88906A77B77DBA25F5D324A75D86D5FEF9DB4D75F08D4F141B9BB1CBD21D7E09C32AA3244CC4950D613C7996524940DE985F9AD54C05AB220A9D022EBC729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojJNmX3owDPmEDG0g76EKT8A== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D55BD4F10769CAD55F7CCF3049B8144A983D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3 08/15] sql: refactor TOTAL() function 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" Part of #4145 --- src/box/sql/func.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 9d96ac12b..e809ea90c 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -76,6 +76,32 @@ fin_sum(struct sql_context *ctx) 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(mem_is_null(ctx->pMem) || mem_is_num(ctx->pMem)); + if (mem_is_null(argv[0])) + return; + if (mem_is_null(ctx->pMem)) + mem_set_double(ctx->pMem, 0.0); + if (mem_add(ctx->pMem, argv[0], ctx->pMem) != 0) + ctx->is_aborted = true; +} + +/** Finalizer for the TOTAL() function. */ +static void +fin_total(struct sql_context *ctx) +{ + assert(mem_is_null(ctx->pMem) || mem_is_double(ctx->pMem)); + if (mem_is_null(ctx->pMem)) + mem_set_double(ctx->pOut, 0.0); + else + mem_copy_as_ephemeral(ctx->pOut, ctx->pMem); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -1693,17 +1719,6 @@ avgFinalize(sql_context * context) context->is_aborted = true; } -static void -totalFinalize(sql_context * context) -{ - SumCtx *p; - p = sql_aggregate_context(context, 0); - if (p == NULL || p->count == 0) - mem_set_double(context->pOut, 0.0); - else - mem_copy_as_ephemeral(context->pOut, &p->mem); -} - /* * The following structure keeps track of state information for the * count() aggregate function. @@ -2127,10 +2142,10 @@ static struct sql_func_definition definitions[] = { FIELD_TYPE_VARBINARY, substrFunc, NULL}, {"SUM", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_sum, fin_sum}, {"SUM", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_sum, fin_sum}, - {"TOTAL", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_DOUBLE, sum_step, - totalFinalize}, - {"TOTAL", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, sum_step, - totalFinalize}, + {"TOTAL", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_DOUBLE, step_total, + fin_total}, + {"TOTAL", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_total, + fin_total}, {"TRIM", 2, {FIELD_TYPE_STRING, FIELD_TYPE_INTEGER}, FIELD_TYPE_STRING, trim_func, NULL}, -- 2.25.1