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 ECD0E6EC40; Sat, 25 Sep 2021 14:34:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org ECD0E6EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1632569688; bh=rDLazKGzLcCDDFOnaA3cDfDF84gd70djdc7MK8Wka2c=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Bh9aTUI164dqm91n2GdhrxZmflYhGfdvVaEh7ov8w8i7nfgUd3H9xnqmd/vIslpcK z4vDFT7PM+AapCb4cQgHppKAIy4SHwNSyVJTiYoEsd1ZXUFq960ykQnEJdMLsty6DE s6Qo88Aa5V2Y8qU9ZBUTOX8/USs/NihF8/Bx4JgU= 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 254C46EC40 for ; Sat, 25 Sep 2021 14:34:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 254C46EC40 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mU5x7-0005uX-Hc; Sat, 25 Sep 2021 14:34:45 +0300 Date: Sat, 25 Sep 2021 14:34:44 +0300 To: v.shpilevoy@tarantool.org, tarantool-patches@dev.tarantool.org Message-ID: <20210925113444.GG290467@tarantool.org> References: <95ca63319d073250a4e310f04d453e85e44c3dfd.1632220375.git.imeevma@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <95ca63319d073250a4e310f04d453e85e44c3dfd.1632220375.git.imeevma@gmail.com> X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD96A58C36AA2E99649D004E862036064FD5FB5D381B6BC775A182A05F538085040A4BE6E3FB2DCFD5401777E55766ACF617EA929AC3F37901ACC340D958B681B24 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE77603ADE015AF816DEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637BC6099E116BE74F18638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D89D44FB93FFD84ADE303E0FC2CED37F6C117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8B2EE5AD8F952D28FBA471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7CD1D040B6C1ECEA3F43847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: 0D63561A33F958A50BFCDCF0F3F33251E1BD22B4064F12C7179D51FE2CD30AD8D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA75BFC02AB3DF06BA5A410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34C2BE7381A0AD3DF169B77C9FFC9F0D5A3D2244D1C88D8207074281BF001049B59D300822A9CC41981D7E09C32AA3244C7644DCA35E7AB22E5B9D9BC13BCAA71BA8CE788DE6831205729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojqHUEAOTxd4jcEh9hUPrdTw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D811536DC797167E8BCB70DBA6C20054883D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v2 10/15] sql: rework COUNT() 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: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thank you for the review! I replaced manual checks of MEM type by mem_is_*() functions. Diff and new patch below. On Tue, Sep 21, 2021 at 01:59:23PM +0300, Mergen Imeev via Tarantool-patches wrote: > This patch does some refactoring for the SQL built-in aggregate function > COUNT(). > > 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 b5f154fb1..113df423a 100644 > --- a/src/box/sql/func.c > +++ b/src/box/sql/func.c > @@ -150,6 +150,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 (ctx->pMem->type == MEM_TYPE_NULL) > + mem_set_uint(ctx->pMem, 0); > + if (argc == 1 && argv[0]->type == MEM_TYPE_NULL) > + return; > + assert(ctx->pMem->type == MEM_TYPE_UINT); > + ++ctx->pMem->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) > + 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) > { > @@ -1704,41 +1727,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. > */ > @@ -2008,9 +1996,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 > Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index b4c25c935..00db6512a 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -159,11 +159,11 @@ 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) + if (mem_is_null(ctx->pMem)) mem_set_uint(ctx->pMem, 0); - if (argc == 1 && argv[0]->type == MEM_TYPE_NULL) + if (argc == 1 && mem_is_null(argv[0])) return; - assert(ctx->pMem->type == MEM_TYPE_UINT); + assert(mem_is_uint(ctx->pMem)); ++ctx->pMem->u.u; } @@ -171,8 +171,8 @@ step_count(struct sql_context *ctx, int argc, struct Mem **argv) 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(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); } New patch: commit 000ac8430821d33aaa939d426c30c1de2d5a7cab Author: Mergen Imeev Date: Thu Sep 9 18:26:04 2021 +0300 sql: rework COUNT() This patch does some refactoring for the SQL built-in aggregate function COUNT(). Part of #4145 diff --git a/src/box/sql/func.c b/src/box/sql/func.c index e436ffbe1..00db6512a 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. */ @@ -2012,9 +2000,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},