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 D3714711BD; Tue, 26 Oct 2021 13:39:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D3714711BD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1635244787; bh=GLH2HRYPgMR722oTFr+Jdjn4QDzNnTP620xFwy0kKKg=; 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=AQGKUHvg6cuLbSrPBHQxnjfpveZIFoFjLd6dLbPvmNDGQTWIrwnpeoHdZgsJNqUvH g0ClGkNRGWuxqYssyO0M8jFMGHO4z1wmAFK/cdmNFuQbEeIy79cXbQQIbkR/krXwM2 BNbjuD6htLoMvCxAl0C51g62IIgjriX/sW6/luQc= 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 A73B2711BD for ; Tue, 26 Oct 2021 13:34:24 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A73B2711BD Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mfJmh-0000ho-Vc; Tue, 26 Oct 2021 13:34:24 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 26 Oct 2021 13:34:23 +0300 Message-Id: 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: 4F1203BC0FB41BD9D1D35DBD2D15487EB9438C392F7C4E68414DBB3B161A4FBD182A05F53808504039C3EF0FE28184A0666642408C4230333E9F8F25B4E8C50438B8C5FD8947AA2F X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7AEA4A6B3AFC9B957C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE77FAFB841CE9024F9EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F2677DF3C9BD5CDB2C653464155054067ACC7F00164DA146DAFE8445B8C89999728AA50765F7900637F924B32C592EA89F389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC81D471462564A2E19F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B953A8A48A05D51F175ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C2749EEF4D85F8CCA8F3CB09FF2F83486E1072085F9EEA62C9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF691691B2CB2A81D8699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34E3E1DC0F9BD553091472F673D2F3AAA3DB297E9B08052FD8AFAE3A6C3E87F940332E741E1689289C1D7E09C32AA3244CB217D92CE142388231C0F4B108C745EDA8CE788DE6831205729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojNvH0qP3qm71NS0Uz2P0A6Q== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DE02F1B40D6E56965566A6CE8E551835B83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 11/16] sql: refactor COUNT() 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 | 64 +++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 8be553110..8c3ddb0bf 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -151,6 +151,29 @@ fin_avg(struct sql_context *ctx) ctx->is_aborted = true; } +/** Implementation of the COUNT() function. */ +static void +step_count(struct sql_context *ctx, int argc, struct Mem **argv) +{ + assert(argc == 0 || argc == 1); + if (mem_is_null(ctx->pMem)) + mem_set_uint(ctx->pMem, 0); + if (argc == 1 && mem_is_null(argv[0])) + return; + assert(mem_is_uint(ctx->pMem)); + ++ctx->pMem->u.u; +} + +/** Finalizer for the COUNT() function. */ +static void +fin_count(struct sql_context *ctx) +{ + assert(mem_is_null(ctx->pMem) || mem_is_uint(ctx->pMem)); + if (mem_is_null(ctx->pMem)) + return mem_set_uint(ctx->pOut, 0); + mem_copy_as_ephemeral(ctx->pOut, ctx->pMem); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -1712,41 +1735,6 @@ soundexFunc(sql_context * context, int argc, sql_value ** argv) } } -/* - * The following structure keeps track of state information for the - * count() aggregate function. - */ -typedef struct CountCtx CountCtx; -struct CountCtx { - i64 n; -}; - -/* - * Routines to implement the count() aggregate function. - */ -static void -countStep(sql_context * context, int argc, sql_value ** argv) -{ - CountCtx *p; - if (argc != 0 && argc != 1) { - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, - "COUNT", "0 or 1", argc); - context->is_aborted = true; - return; - } - p = sql_aggregate_context(context, sizeof(*p)); - if ((argc == 0 || !mem_is_null(argv[0])) && p != NULL) - p->n++; -} - -static void -countFinalize(sql_context * context) -{ - CountCtx *p; - p = sql_aggregate_context(context, 0); - sql_result_uint(context, p ? p->n : 0); -} - /* * Routines to implement min() and max() aggregate functions. */ @@ -2015,9 +2003,9 @@ static struct sql_func_definition definitions[] = { NULL}, {"COALESCE", -1, {FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, sql_builtin_stub, NULL}, - {"COUNT", 0, {}, FIELD_TYPE_INTEGER, countStep, countFinalize}, - {"COUNT", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_INTEGER, countStep, - countFinalize}, + {"COUNT", 0, {}, FIELD_TYPE_INTEGER, step_count, fin_count}, + {"COUNT", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_INTEGER, step_count, + fin_count}, {"GREATEST", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, minmaxFunc, NULL}, -- 2.25.1