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 1FB4A6FC8B; Fri, 1 Oct 2021 15:54:37 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1FB4A6FC8B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633092877; bh=DyEnw4tkD7BtGD8AZNi7NAL4ZMnOZll/4wPv5EmTktQ=; 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=vNicMjMLgI3ds8J7vk0pC1+vc0OODQTm/Y2vGtQXzZ79poO62CRL3JfOj+1M81Rxd ScM8NtWfsxNFK/fpQzd4cqIE6MptZzW0TubMXwSe6lvk08v3mtiIiHVlod1KiSDFcx iJo5a56sasfdkOnNPGL9qxdowynsun/DE/PqG4Mc= 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 B49E47414C for ; Fri, 1 Oct 2021 15:48:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B49E47414C Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mWHyA-0003zh-2m; Fri, 01 Oct 2021 15:48:54 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 1 Oct 2021 15:48:53 +0300 Message-Id: <440e898334cae0debebd83d8cde70bbda150f2e9.1633092363.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: 4F1203BC0FB41BD96A58C36AA2E99649E150573C3DCDB29943BD5C8B56246B79182A05F5380850404DD64437203D41737AA4B08F8E76882D7324135625556B44640FC6085C540572 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7A609AE8E79ADB41CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637D0C488966F20D4908638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D86C4EEF3A97673746C2D483AE669DC901117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8B974A882099E279BDA471835C12D1D977C4224003CC8364762BB6847A3DEAEFB0F43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B68655334FD4449CB9ECD01F8117BC8BEAAAE862A0553A39223F8577A6DFFEA7C289736CE4F78F08343847C11F186F3C59DAA53EE0834AAEE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBBB17C150BCA679326E6867DD9343A02EF1A597C035CDF239C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF783E2B6F79C23BED699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3451BBE684D17D7221F5471A619484DA509C8D924E57A96686A2586DC98EB81BAFF2C444AD478300591D7E09C32AA3244C5F34A7FD040959507BAAB1C6CF94BA923E8609A02908F271729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojJNmX3owDPmF7Z+cvfXGigg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DB01AAC3747F176B9E28BD8978610C10283D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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 384c68be8..51693d47b 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -177,6 +177,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) { @@ -224,16 +264,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 */ @@ -1739,60 +1769,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?) */ @@ -2063,35 +2039,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