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 88EAF711AC; Tue, 26 Oct 2021 13:38:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 88EAF711AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1635244726; bh=oYBniZJJ7npLfni+vAHhGhaBLbpZtf8v1oc3EmwuKZU=; 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=aTfobwMpKVUbT8nx0y6+sf1hFNdjzl1yiW+kXfyrTEUPSWIgeIfg1ZYB+xT6Dyr2P jtrPKmnSueFPqF2xY1rjhi65k1IMeT+6/37FQ1dMUxCon4pNG6d6bXOyM+Yle5V356 AXmfp7fn2FUEJ1CMV0FLm02rkoaDbNlfTtJextnc= 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 C7E01711AC for ; Tue, 26 Oct 2021 13:34:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C7E01711AC Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mfJmd-0000fs-SU; Tue, 26 Oct 2021 13:34:20 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 26 Oct 2021 13:34:19 +0300 Message-Id: <5846c0651e86748a62d3d9a302540aa7133674db.1635244212.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: 4F1203BC0FB41BD9D1D35DBD2D15487E657BD385B6B7DC1C21BEC69EAD562B5E182A05F5380850401E11F739CF4EB7B4185CBA33904FF4FA8BCC37667B66025905125E322ED0AA15 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE747D938A1F6F567D4EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637D82ED515D6052E03EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F222FA12040F4E2D4FFE084312B88E259CCC7F00164DA146DAFE8445B8C89999728AA50765F7900637F924B32C592EA89F389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC81D471462564A2E19F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B953A8A48A05D51F175ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C2749EEF4D85F8CCAFDADE0C66DE05A1E034E0FAF371A4AA59C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF691691B2CB2A81D8699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A5BF8FA0BD61F9942E5B3200593D0987241E9B04F69C189EA52AF00C4B3619B3629E85988ABDB6121D7E09C32AA3244C15BF0D4925B1A1B7AB69A89AF58ACDEC408A6A02710B7304729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojNvH0qP3qm70VryQz7k/XxA== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D51EF2827039BEC028D85EE390844D59883D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 09/16] 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 f18f1b4d1..c3c7ebec0 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) { @@ -1700,17 +1726,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. @@ -2134,10 +2149,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