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 8F5686F3E5; Thu, 11 Nov 2021 13:45:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8F5686F3E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636627557; bh=AycC7JphlHeezsLofVaeouLu43FeEVc9KssdGT1p9Uc=; 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=fex1WL+Z0X8uz2UWdfYTaL3MPpr8KBdELmPoPC8Jq0nKWbnM3XTekqZxEfc8xXUgP Wug/U3CgdW2rHeRfjnVZUs0g62taYPgOn4tJBytOdpi13e5erTFFIJcYsjfStI36OA Pmttj2xNnugRGp5Qg0MEQpJVUHL8o0HxZjyJkF/U= 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 AD1E06F3E5 for ; Thu, 11 Nov 2021 13:45:26 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AD1E06F3E5 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7a9-0002rS-Up; Thu, 11 Nov 2021 13:45:26 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:45:25 +0300 Message-Id: <1bae5402b319dbc4ebda1d44f2f73167f89bba82.1636627366.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: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9731B3922EC0639792BB520D6BC54996EEEF308B3433E7AC900894C459B0CD1B96D362FF8B35A251E9C0526C0450DDB5ABD7B9CED87D2556745678E5A44F257A1 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F544D30F1A6FA191EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006372521E7C1CE72986C8638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8E3DC709E075A9B2ACC7315BA8D9EE2A5117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A1FE62BDBE03EBCBA02A7D94CE70ABCFFD9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF309DFB797F6729CB699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34BC3EEE75EF3BACCF6A097F9C2B55F7D62DD9C91F18BBAB77A9D31CB90736DC899290C1C8CF3190D01D7E09C32AA3244CB15E3BC06C0CE6049530E1A985C7BBD28894E9C85370243E729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bJu3ztqbcegaw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D19E06224DF372C50606012EE0AA1E53783D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 1/8] sql: refactor ABS() funcion 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 | 83 +++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 53 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 0cf17572b..dbeb38bee 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -232,6 +232,32 @@ step_group_concat(struct sql_context *ctx, int argc, struct Mem *argv) ctx->is_aborted = true; } +/** Implementations of the ABS() function. */ +static void +func_abs_int(struct sql_context *ctx, int argc, struct Mem *argv) +{ + assert(argc == 1); + (void)argc; + struct Mem *arg = &argv[0]; + if (mem_is_null(arg)) + return; + assert(mem_is_int(arg)); + uint64_t u = mem_is_uint(arg) ? arg->u.u : (uint64_t)-arg->u.i; + mem_set_uint(ctx->pOut, u); +} + +static void +func_abs_double(struct sql_context *ctx, int argc, struct Mem *argv) +{ + assert(argc == 1); + (void)argc; + struct Mem *arg = &argv[0]; + if (mem_is_null(arg)) + return; + assert(mem_is_double(arg)); + mem_set_double(ctx->pOut, arg->u.r < 0 ? -arg->u.r : arg->u.r); +} + static const unsigned char * mem_as_ustr(struct Mem *mem) { @@ -395,57 +421,6 @@ lengthFunc(struct sql_context *context, int argc, struct Mem *argv) } } -/* - * Implementation of the abs() function. - * - * IMP: R-23979-26855 The abs(X) function returns the absolute value of - * the numeric argument X. - */ -static void -absFunc(struct sql_context *context, int argc, struct Mem *argv) -{ - assert(argc == 1); - UNUSED_PARAMETER(argc); - switch (sql_value_type(&argv[0])) { - case MP_UINT: { - sql_result_uint(context, mem_get_uint_unsafe(&argv[0])); - break; - } - case MP_INT: { - int64_t value = mem_get_int_unsafe(&argv[0]); - assert(value < 0); - sql_result_uint(context, -value); - break; - } - case MP_NIL:{ - /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ - sql_result_null(context); - break; - } - case MP_BOOL: - case MP_BIN: - case MP_EXT: - case MP_ARRAY: - case MP_MAP: { - diag_set(ClientError, ER_INCONSISTENT_TYPES, "number", - mem_str(&argv[0])); - context->is_aborted = true; - return; - } - default:{ - /* - * Abs(X) returns 0.0 if X is a string or blob - * that cannot be converted to a numeric value. - */ - double rVal = mem_get_double_unsafe(&argv[0]); - if (rVal < 0) - rVal = -rVal; - sql_result_double(context, rVal); - break; - } - } -} - /** * Implementation of the position() function. * @@ -1930,8 +1905,10 @@ struct sql_func_definition { * function should be defined in succession. */ static struct sql_func_definition definitions[] = { - {"ABS", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, absFunc, NULL}, - {"ABS", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, absFunc, NULL}, + {"ABS", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, func_abs_int, + NULL}, + {"ABS", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, func_abs_double, + NULL}, {"AVG", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_avg, fin_avg}, {"AVG", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_avg, fin_avg}, {"CHAR", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_STRING, charFunc, NULL}, -- 2.25.1