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 33531711BE; Tue, 26 Oct 2021 13:40:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 33531711BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1635244819; bh=6WHDsKCK7PlTA6DTyMiqT0R9Mcjs0Y4JcLJ2T5V8d0o=; 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=mwOXZbNQADRUoWH51b1tnteHNgVdYhdHZL8aXfz9diXMLaP3G1lghZ4ARn1hCQGB9 bIzC0mfzzlKCrtCVEqTn/qczS180Sjo2SmkoW6m+YwDHEe0h7DQmjAY+FO13wBz+16 66A2e8XJvS06z9eITf5Ljnp8XHuP/AiVfWJWwzR4= 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 7DB71711BE for ; Tue, 26 Oct 2021 13:34:26 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 7DB71711BE Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mfJmj-0000jD-KT; Tue, 26 Oct 2021 13:34:25 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 26 Oct 2021 13:34:25 +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: 78E4E2B564C1792B X-77F55803: 4F1203BC0FB41BD9D1D35DBD2D15487EB9438C392F7C4E682D6CFA95551ADAC3182A05F538085040DCC318EDCB73B6B9BAE5519B87A12A26C2A07D2FA70BF6C23500D1A21ED5E5BE X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7DB45732B85BEE83BEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063748E7A03516F25E8E8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8D5D536CD690592D69294AE5987FBCE41117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C2749EEF4D85F8CCA9C910D32FCA7577B67FBF03463F7D2589C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF691691B2CB2A81D8699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D95739AEDB3821B41936977A8647EA9CB0C69327113ECA42DBA1E97A2D02DC66B80C7ADE10C330E01D7E09C32AA3244C6187CF2B56EC7F19D79BCB64A2967E54F165894D92D62706729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojNvH0qP3qm70d4Jfq5GFluQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D433BF0DECCF28E4FD386B483D1FC1CC983D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 12/16] sql: refactor MIN() and MAX() functions 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 | 162 +++++++++++++++++++-------------------------- src/box/sql/vdbe.c | 1 - 2 files changed, 69 insertions(+), 94 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 8c3ddb0bf..531facdb9 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -174,6 +174,46 @@ fin_count(struct sql_context *ctx) mem_copy_as_ephemeral(ctx->pOut, ctx->pMem); } +/** Implementation of the MIN() and MAX() functions. */ +static void +step_minmax(struct sql_context *ctx, int argc, struct Mem **argv) +{ + assert(argc == 1); + (void)argc; + if (mem_is_null(argv[0])) { + if (!mem_is_null(ctx->pMem)) + ctx->skipFlag = 1; + return; + } + if (mem_is_null(ctx->pMem)) { + if (mem_copy(ctx->pMem, argv[0]) != 0) + ctx->is_aborted = true; + return; + } + + uint32_t flags = ((struct func_sql_builtin *)ctx->func)->flags; + bool is_max = (flags & SQL_FUNC_MAX) != 0; + /* + * This step function is used for both the min() and max() aggregates, + * the only difference between the two being that the sense of the + * comparison is inverted. + */ + int cmp = mem_cmp_scalar(ctx->pMem, argv[0], ctx->coll); + if ((is_max && cmp < 0) || (!is_max && cmp > 0)) { + if (mem_copy(ctx->pMem, argv[0]) != 0) + ctx->is_aborted = true; + return; + } + ctx->skipFlag = 1; +} + +/** Finalizer for the MIN() and MAX() functions. */ +static void +fin_minmax(struct sql_context *ctx) +{ + mem_copy(ctx->pOut, ctx->pMem); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -221,16 +261,6 @@ sql_func_uuid(struct sql_context *ctx, int argc, struct Mem **argv) mem_set_uuid(ctx->pOut, &uuid); } -/* - * Indicate that the accumulator load should be skipped on this - * iteration of the aggregate loop. - */ -static void -sqlSkipAccumulatorLoad(sql_context * context) -{ - context->skipFlag = 1; -} - /* * Implementation of the non-aggregate min() and max() functions */ @@ -1735,60 +1765,6 @@ soundexFunc(sql_context * context, int argc, sql_value ** argv) } } -/* - * Routines to implement min() and max() aggregate functions. - */ -static void -minmaxStep(sql_context * context, int NotUsed, sql_value ** argv) -{ - Mem *pArg = (Mem *) argv[0]; - Mem *pBest; - UNUSED_PARAMETER(NotUsed); - - struct func_sql_builtin *func = - (struct func_sql_builtin *)context->func; - pBest = sql_context_agg_mem(context); - if (!pBest) - return; - - if (mem_is_null(argv[0])) { - if (!mem_is_null(pBest)) - sqlSkipAccumulatorLoad(context); - } else if (!mem_is_null(pBest)) { - struct coll *pColl = context->coll; - /* - * This step function is used for both the min() - * and max() aggregates, the only difference - * between the two being that the sense of the - * comparison is inverted. - */ - bool is_max = (func->flags & SQL_FUNC_MAX) != 0; - int cmp = mem_cmp_scalar(pBest, pArg, pColl); - if ((is_max && cmp < 0) || (!is_max && cmp > 0)) { - if (mem_copy(pBest, pArg) != 0) - context->is_aborted = true; - } else { - sqlSkipAccumulatorLoad(context); - } - } else { - pBest->db = sql_context_db_handle(context); - if (mem_copy(pBest, pArg) != 0) - context->is_aborted = true; - } -} - -static void -minMaxFinalize(sql_context * context) -{ - struct Mem *mem = context->pMem; - struct Mem *res; - if (mem_get_agg(mem, (void **)&res) != 0) - return; - if (!mem_is_null(res)) - sql_result_value(context, res); - mem_destroy(res); -} - /* * group_concat(EXPR, ?SEPARATOR?) */ @@ -2059,35 +2035,35 @@ static struct sql_func_definition definitions[] = { {"LOWER", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, LowerICUFunc, NULL}, - {"MAX", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, minmaxStep, - minMaxFinalize}, - {"MAX", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, minmaxStep, - minMaxFinalize}, - {"MAX", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, minmaxStep, - minMaxFinalize}, - {"MAX", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, minmaxStep, - minMaxFinalize}, - {"MAX", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, minmaxStep, - minMaxFinalize}, - {"MAX", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, minmaxStep, - minMaxFinalize}, - {"MAX", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, minmaxStep, - minMaxFinalize}, - - {"MIN", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, minmaxStep, - minMaxFinalize}, - {"MIN", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, minmaxStep, - minMaxFinalize}, - {"MIN", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, minmaxStep, - minMaxFinalize}, - {"MIN", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, minmaxStep, - minMaxFinalize}, - {"MIN", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, minmaxStep, - minMaxFinalize}, - {"MIN", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, minmaxStep, - minMaxFinalize}, - {"MIN", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, minmaxStep, - minMaxFinalize}, + {"MAX", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_minmax, + fin_minmax}, + {"MAX", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_minmax, + fin_minmax}, + {"MAX", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, step_minmax, + fin_minmax}, + {"MAX", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, step_minmax, + fin_minmax}, + {"MAX", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, step_minmax, + fin_minmax}, + {"MAX", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, step_minmax, + fin_minmax}, + {"MAX", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, step_minmax, + fin_minmax}, + + {"MIN", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_minmax, + fin_minmax}, + {"MIN", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_minmax, + fin_minmax}, + {"MIN", 1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, step_minmax, + fin_minmax}, + {"MIN", 1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, step_minmax, + fin_minmax}, + {"MIN", 1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, step_minmax, + fin_minmax}, + {"MIN", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, step_minmax, + fin_minmax}, + {"MIN", 1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, step_minmax, + fin_minmax}, {"NULLIF", 2, {FIELD_TYPE_ANY, FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, nullifFunc, NULL}, diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index a3d403176..96eb76244 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -4161,7 +4161,6 @@ case OP_AggStep: { } #endif - pMem->n++; mem_create(&t); pCtx->pOut = &t; pCtx->is_aborted = false; -- 2.25.1