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 809B17414E; Fri, 1 Oct 2021 10:49:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 809B17414E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633074560; bh=UXn27CsfCHN6h14ivMf469yZL8YTaOkTtCUvjDEfhEE=; 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=x7YMApHSpoEMMckRMSKRhDMETDGfbROlf3L6jiAtvhSeY3j8P9dbdGFb8D26zoRnS 2l57nXbWB+cqG1iCjqWmZtah4ITIWoq7IPzm6E6vO4qPclR4WuFVWzHjvy7vpGFXLd SktBZfK7mohIaiAu2gL1DVnc1gNSeEWzPRA1PUdI= 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 E08A37414F for ; Fri, 1 Oct 2021 10:43:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E08A37414F Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mWDCD-0004oN-Jn; Fri, 01 Oct 2021 10:43:55 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 1 Oct 2021 10:43:05 +0300 Message-Id: <8d151d2fb1c2f9a766529d8d9126d92715bef295.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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD96A58C36AA2E9964976B67DC8394C8DAB0EDC1900E6C7EF8D182A05F538085040ADABE37BFCDBE79C33E62D8F3D99FD310F221DEA1C32035C028FB01823498BFD X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7AEA4A6B3AFC9B957C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE7BF27F4FA2823CCF0EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F20EBA6F0A46E64E9D9A3585E1770A594DCC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F790063783E00425F71A4181389733CBF5DBD5E9B5C8C57E37DE458B9E9CE733340B9D5F3BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E735E4A630A5B664A4FFC4224003CC83647689D4C264860C145E X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBBB17C150BCA6793BD02443CAC2C3106C9A10174BBA288379C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF783E2B6F79C23BED699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34411AA3F52B5778B6D987978018063AAD77CFB48B00EB94AC9DB9C09765775C124EAE27174C3738A01D7E09C32AA3244CB14E78ECACE17542CEFD07469587FF2B4DBEAD0ED6C55A80729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojJNmX3owDPmHAASOvDZ2oyg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DCB499A1A89D0474F00183CE9EF0468C983D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v3 10/15] 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 4ee0937a5..6c4f80243 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -154,6 +154,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) { @@ -1708,41 +1731,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. */ @@ -2011,9 +1999,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