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 E02D46E462; Fri, 8 Oct 2021 20:32:36 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E02D46E462 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633714357; bh=IyZjW/IevZqjRZxEWe4Q30RHxlY4XmxT4fkRwhhxiwU=; 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=LFnJOT0BOpfLUMN6vtWIElYr5xVyhF1P+m90dCJRSruMCNH9Qulnl07y2RaG0hb7u 1QYcWuAeGAMCXOlOP5imW3L9tF29UeUGnimzbA3z5jJrBo9K3YzzitoplHqnpXF/O2 VS1jZ0SvirhMw7Bwe3M98pd8lrtqLUoundSAlRgI= 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 AE3586E462 for ; Fri, 8 Oct 2021 20:31:37 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AE3586E462 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mYtib-00080R-2s; Fri, 08 Oct 2021 20:31:37 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 8 Oct 2021 20:31:36 +0300 Message-Id: <54d782249ceddfa9d8dbc427413c3b1df669c5d6.1633713432.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: 4F1203BC0FB41BD9A6D4E3B1981C4C7DECD3BE02BC5B2D4F269A0E3D85100611182A05F538085040F41337C34D9770DBE1E05FEE318E799AEAEC7CF1ECEF490635F28A48FF554536 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE746D93DAA4671895CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006375080A183704825A98638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8C9E68F8973457B3D7972BA622ADCF3F9117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC2EE5AD8F952D28FBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18C26CFBAC0749D213D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B62CFFCC7B69C47339089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C650BD2383FF25EF5BB83F2B3DC2BD976179565C87218843F9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF520746E264B47742699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34FA5A4D623E7343BBA67D870F8AE9B9B5B94707393268C2EF407ACACBA9BD15B6F7F0829F7E9720211D7E09C32AA3244C9BFD86BFA331AD103AF33B2F1D7C2470B018FE5BB746DCD1729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMZ06aokA6brgDZJQneDAMQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D16B27655D7C491A6316D5F5B49B79A0C83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 02/21] sql: refactor GREATEST() and LEAST() 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 | 102 ++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index dd5e7d785..7886f5f40 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -777,6 +777,29 @@ func_char(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_str_allocated(ctx->pOut, str, (char *)ptr - str); } +/** Implementation of the GREATEST() and LEAST() functions. */ +static void +func_greatest_least(struct sql_context *ctx, int argc, struct Mem *argv) +{ + assert(argc > 1); + int mask = ctx->func->def->name[0] == 'G' ? -1 : 0; + assert(ctx->func->def->name[0] == 'G' || + ctx->func->def->name[0] == 'L'); + + if (mem_is_null(&argv[0])) + return; + int best = 0; + for (int i = 1; i < argc; ++i) { + if (mem_is_null(&argv[i])) + return; + int cmp = mem_cmp_scalar(&argv[best], &argv[i], ctx->coll); + if ((cmp ^ mask) >= 0) + best = i; + } + if (mem_copy(ctx->pOut, &argv[best]) != 0) + ctx->is_aborted = true; +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -824,37 +847,6 @@ sql_func_uuid(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_uuid(ctx->pOut, &uuid); } -/* - * Implementation of the non-aggregate min() and max() functions - */ -static void -minmaxFunc(struct sql_context *context, int argc, struct Mem *argv) -{ - int i; - int iBest; - struct coll *pColl; - struct func *func = context->func; - int mask = sql_func_flag_is_set(func, SQL_FUNC_MAX) ? -1 : 0; - if (argc < 2) { - diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, - mask ? "GREATEST" : "LEAST", "at least two", argc); - context->is_aborted = true; - return; - } - pColl = context->coll; - assert(mask == -1 || mask == 0); - iBest = 0; - if (mem_is_null(&argv[0])) - return; - for (i = 1; i < argc; i++) { - if (mem_is_null(&argv[i])) - return; - if ((mem_cmp_scalar(&argv[iBest], &argv[i], pColl) ^ mask) >= 0) - iBest = i; - } - sql_result_value(context, &argv[iBest]); -} - /* * Return the type of the argument. */ @@ -1869,19 +1861,20 @@ static struct sql_func_definition definitions[] = { {"COUNT", 1, {FIELD_TYPE_ANY}, FIELD_TYPE_INTEGER, step_count, fin_count}, - {"GREATEST", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, minmaxFunc, - NULL}, - {"GREATEST", -1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, minmaxFunc, - NULL}, - {"GREATEST", -1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, minmaxFunc, - NULL}, + {"GREATEST", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, + func_greatest_least, NULL}, + {"GREATEST", -1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, + func_greatest_least, NULL}, + {"GREATEST", -1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, + func_greatest_least, NULL}, {"GREATEST", -1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, - minmaxFunc, NULL}, - {"GREATEST", -1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, minmaxFunc, NULL}, - {"GREATEST", -1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, minmaxFunc, - NULL}, - {"GREATEST", -1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, minmaxFunc, - NULL}, + func_greatest_least, NULL}, + {"GREATEST", -1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, + func_greatest_least, NULL}, + {"GREATEST", -1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, + func_greatest_least, NULL}, + {"GREATEST", -1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, + func_greatest_least, NULL}, {"GROUP_CONCAT", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, step_group_concat, NULL}, @@ -1896,15 +1889,20 @@ static struct sql_func_definition definitions[] = { {"IFNULL", 2, {FIELD_TYPE_ANY, FIELD_TYPE_ANY}, FIELD_TYPE_SCALAR, sql_builtin_stub, NULL}, - {"LEAST", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, minmaxFunc, - NULL}, - {"LEAST", -1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, minmaxFunc, NULL}, - {"LEAST", -1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, minmaxFunc, NULL}, - {"LEAST", -1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, minmaxFunc, - NULL}, - {"LEAST", -1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, minmaxFunc, NULL}, - {"LEAST", -1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, minmaxFunc, NULL}, - {"LEAST", -1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, minmaxFunc, NULL}, + {"LEAST", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, + func_greatest_least, NULL}, + {"LEAST", -1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, + func_greatest_least, NULL}, + {"LEAST", -1, {FIELD_TYPE_NUMBER}, FIELD_TYPE_NUMBER, + func_greatest_least, NULL}, + {"LEAST", -1, {FIELD_TYPE_VARBINARY}, FIELD_TYPE_VARBINARY, + func_greatest_least, NULL}, + {"LEAST", -1, {FIELD_TYPE_UUID}, FIELD_TYPE_UUID, + func_greatest_least, NULL}, + {"LEAST", -1, {FIELD_TYPE_STRING}, FIELD_TYPE_STRING, + func_greatest_least, NULL}, + {"LEAST", -1, {FIELD_TYPE_SCALAR}, FIELD_TYPE_SCALAR, + func_greatest_least, NULL}, {"LENGTH", 1, {FIELD_TYPE_STRING}, FIELD_TYPE_INTEGER, func_char_length, NULL}, -- 2.25.1