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 CFCC971050; Wed, 20 Oct 2021 19:52:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CFCC971050 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1634748739; bh=bbOV7oDFEHij7ugeffudtlT1YlC1q8unIqzdTJ7MNeI=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=eR3o6wMrrxxa2Rd1VLlNCpHD3uXH0DNpB9VNl7ShE46LkulwI6sNauWTIoRkw5f3W HNdSmUXqCWGEVC7vIMA6WprMNndP90BY8BXdbigHNk1RXCANH/VCJqNV4Fr5E8rGBP 1WgOZjcC6EfAalmOp+Hsr5Z+++XF7ns8vZva5vFo= 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 1675571050 for ; Wed, 20 Oct 2021 19:52:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 1675571050 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mdEp8-0005ug-7r; Wed, 20 Oct 2021 19:52:18 +0300 Date: Wed, 20 Oct 2021 19:52:16 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20211020165216.GA203963@tarantool.org> References: <1243ff4d634abefca92dd265e81a64a4d347b56b.1633105483.git.imeevma@gmail.com> <4dd3b621-96a6-5c23-0c92-3a3cf0f398ee@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <4dd3b621-96a6-5c23-0c92-3a3cf0f398ee@tarantool.org> X-4EC0790: 10 X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD9C7814344C8C501C83E7F929BE7EA67118FCDA824C75B5BF5182A05F538085040E30F31AEEDDC46A3433609ACBAEC9DFA72AF58B819D65BB566115CA0CA797A2D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE77633BACAB33B9508C2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE731B6267C8418DDA1EA1F7E6F0F101C6723150C8DA25C47586E58E00D9D99D84E1BDDB23E98D2D38BBCA57AF85F7723F21F39E9F44111ABB6D0BF66E367E502CACC7F00164DA146DAFE8445B8C89999728AA50765F7900637F6B57BC7E64490618DEB871D839B7333395957E7521B51C2DFABB839C843B9C08941B15DA834481F8AA50765F790063783E00425F71A4181389733CBF5DBD5E9B5C8C57E37DE458B9E9CE733340B9D5F3BBE47FD9DD3FB595F5C1EE8F4F765FC72CEEB2601E22B093A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E7354E672349037D5FA5C4224003CC83647689D4C264860C145E X-C1DE0DAB: 0D63561A33F958A52D012268DD2167CA6644E5273CBA9FA26F1C6709D7E8573CD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7557E988E9157162368E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D343DCFC3BEDFB0242D44DBC31DBD46D6F43641BCE987E6991F764EDDCF19E0C7C3EC2FFDEBF44BC4A81D7E09C32AA3244C6CCCB9AA6C5F58F6B4259315B00528783A76366E8A9DE7CA729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojMPcpz6kU2m/debQ2BvigtQ== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D786C4A85975EE80F2979D63BF627BF7283D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: Re: [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: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi! Thank you for the review! My answers, diff and new patch below. On Fri, Oct 08, 2021 at 11:55:33PM +0200, Vladislav Shpilevoy wrote: > Hi! Thanks for the patch! > > On 01.10.2021 18:29, imeevma@tarantool.org wrote: > > 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; > > 1. Need mem_is_null(). The same for the other places if there > are more. > Fixed here in all other places in this patch-set. > > + assert(mem_is_int(arg)); > > + uint64_t u = mem_is_uint(arg) ? arg->u.u : (uint64_t)-arg->u.i; > > 2. You could make return when mem_is_uint(). It would remove '?' and > mem_set_uint() which would calls mem_clear() inside. > I am not sure that I understood correctly. In case of argument being uint we can use mem_copy_as_ephemeral() instead of mem_set_uint(), but I am not sure if it would be better. > > + mem_set_uint(ctx->pOut, u); Diff: diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 5f90fb597..dbeb38bee 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -239,7 +239,7 @@ 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) + 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; @@ -252,9 +252,9 @@ 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) + if (mem_is_null(arg)) return; - assert(arg->type == MEM_TYPE_DOUBLE); + assert(mem_is_double(arg)); mem_set_double(ctx->pOut, arg->u.r < 0 ? -arg->u.r : arg->u.r); } New patch: commit 2cf5a2417954cb06bcc17cabf0636b6ba130cf8d Author: Mergen Imeev Date: Tue Sep 21 14:39:57 2021 +0300 sql: refactor ABS() funcion Part of #4145 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},