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 2B1186F3E5; Thu, 11 Nov 2021 13:51:04 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2B1186F3E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636627864; bh=F+RttJiztkce7Z8Mk9xuHQ12eyj9YxCUmTNJ5xNjPZM=; 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=PakwRbePaHWF8RlwE/xeCYwnLQMkIXh50f+HoGa8R1rA2HWB61KHc2K/tnc708QnN hCC6HEaSEDoCoKa7yBZSEqrB5iNmQuQRaRDaZi43re3uEbLlG3YcRjcubkL9vOmnw1 o6LyyUr882snIR0CEJs+ULudwrH7f5/J5irMOOPE= 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 9CAD471060 for ; Thu, 11 Nov 2021 13:48:53 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 9CAD471060 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7dU-0004rd-Mh; Thu, 11 Nov 2021 13:48:53 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:48:52 +0300 Message-Id: <68cfce17702bfa91afd55cd2c1de031e83a4d3f3.1636627579.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: 4F1203BC0FB41BD9731B3922EC063979F8160E2FE2FDCF123B0537AAB149205200894C459B0CD1B94BB58AB055B91D68AE862CF2FF3F45BBBD7B9CED87D255672476E39F0D35E726 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE78E88BD1CA827EF00C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE73F64378C139886A6EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F23CD82E8B9472A259223D3FA6943F1F6ACC7F00164DA146DAFE8445B8C89999728AA50765F7900637F924B32C592EA89F389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8D23BF7408B3F9022F6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A1B7E40473A7CD7417D486599A8EE979F39C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFCDE59DDCCE53AF2B699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F804B400765FFFF96BC59845358419E414DC0257C4D604A7271B634E3826CE2FDD73CF0902340DC41D7E09C32AA3244C2249B235322CF1D70C296EB417AAD3ECC86C126E7119A0FE729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bLbgNfYR8GS9w== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DD62AF3C6130F25A2E7A437703D9FDD3A83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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 | 113 +++++++++++++++++++++++---------------------- 1 file changed, 57 insertions(+), 56 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 0cd8f8f69..40bdbf739 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -782,6 +782,34 @@ func_char(struct sql_context *ctx, int argc, struct Mem *argv) mem_set_str_allocated(ctx->pOut, str, len); } +/** + * Implementation of the GREATEST() and LEAST() functions. + * + * The GREATEST() function returns the largest of the given arguments. + * The LEAST() function returns the smallest of the given arguments. + */ +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) { @@ -829,37 +857,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. */ @@ -1788,13 +1785,11 @@ static struct sql_func_dictionary dictionaries[] = { {"CHAR_LENGTH", 1, 1, 0, true, 0, NULL}, {"COALESCE", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_COALESCE, true, 0, NULL}, {"COUNT", 0, 1, SQL_FUNC_AGG, false, 0, NULL}, - {"GREATEST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_MAX | SQL_FUNC_NEEDCOLL, - true, 0, NULL}, + {"GREATEST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_NEEDCOLL, true, 0, NULL}, {"GROUP_CONCAT", 1, 2, SQL_FUNC_AGG, false, 0, NULL}, {"HEX", 1, 1, 0, true, 0, NULL}, {"IFNULL", 2, 2, SQL_FUNC_COALESCE, true, 0, NULL}, - {"LEAST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_MIN | SQL_FUNC_NEEDCOLL, - true, 0, NULL}, + {"LEAST", 2, SQL_MAX_FUNCTION_ARG, SQL_FUNC_NEEDCOLL, true, 0, NULL}, {"LENGTH", 1, 1, SQL_FUNC_LENGTH, true, 0, NULL}, {"LIKE", 2, 3, SQL_FUNC_LIKE | SQL_FUNC_NEEDCOLL, true, 0, NULL}, {"LIKELIHOOD", 2, 2, SQL_FUNC_UNLIKELY, true, 0, NULL}, @@ -1876,19 +1871,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}, @@ -1903,15 +1899,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