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 78A8D6DB05; Fri, 1 Oct 2021 19:29:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 78A8D6DB05 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1633105795; bh=vZIlMozj7JrYW+Dqqp7DqwQky7ruz4aJfrkH2Pf07sw=; 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=Dk3gnUeBDeMXlypu+TiscnFy6ICw3++1QLN4jUT5bjPGdAl/z/5+UTHQBik9zCzTx WZS/5Dfyg7N+B3KNwEVEMyXllJsitHZJEHc/ANPhq24xi3FQhvIW6t0kYU14RP9p6/ sec3/IQRQnQpYHZMhdsgTeguypmHtzHauCIIdJ0g= 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 4FA016DB05 for ; Fri, 1 Oct 2021 19:29:26 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4FA016DB05 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mWLPZ-0004Ep-M3; Fri, 01 Oct 2021 19:29:26 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 1 Oct 2021 19:29:25 +0300 Message-Id: <1243ff4d634abefca92dd265e81a64a4d347b56b.1633105483.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: 4F1203BC0FB41BD96A58C36AA2E996498302BFE8288A953FFF2204EAAF1B1FDF182A05F5380850409099016C034E91A5594BF32B083575BD935C5496D9658C931FB473248E265D90 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BB17EE3498E810FEEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C2EE9128AC0EB2C78638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8DF79F5C1EBCB39717B8DA8D630C172A1117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CBBB17C150BCA679348CAECA95470605FD7E8F620F27FA8FD9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF783E2B6F79C23BED699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D342B8615F5CFAD9D0213CBE5102FCE9BE2E26BC5F672763DC4ACE40F17A8FC540BF4AA67C288BE15241D7E09C32AA3244CD8580157092767A1888FAB8BF93E72ED6C24832127668422729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj/2kGGGWRGj4G+1LilFArXw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DF7219B67011DCF09AFDBC8E5BA55C19A83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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 b23636d7e..54b03f359 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -237,6 +237,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 (arg->type == MEM_TYPE_NULL) + 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 (arg->type == MEM_TYPE_NULL) + return; + assert(arg->type == MEM_TYPE_DOUBLE); + 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) { @@ -400,57 +426,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. * @@ -1936,8 +1911,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