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 A62D16EC60; Tue, 30 Mar 2021 02:03:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A62D16EC60 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1617059028; bh=ShWoyowu/BKfN/IVfssaM7r/oQIKub3gXy3o2x2xGUQ=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=eGOLXtrGDsEhafVu65NdAlaQYvTVRxHQwUqnFiXHmNBQe1FAdJ+99/QK2EESjoaSp yjuBMEw1G/ifeA6ljxQ1K8ch+wMMJO8HHx5GHfmO1OWR7R2byjrKl/+Ng4cojn/BAO 14nmH2TTYOkLSGcow4P8RAyJcgY9zNIL6nJJyUWs= Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [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 C8C286EC60 for ; Tue, 30 Mar 2021 02:02:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org C8C286EC60 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1lR0uL-0006Yb-Mr; Tue, 30 Mar 2021 02:02:54 +0300 To: imeevma@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org References: <3f1a796a92670c57203c5a6c7d1b47856db96917.1616491731.git.imeevma@gmail.com> Message-ID: <95ed9427-b28d-e4d8-8147-729dba11a3ae@tarantool.org> Date: Tue, 30 Mar 2021 01:02:52 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: <3f1a796a92670c57203c5a6c7d1b47856db96917.1616491731.git.imeevma@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9ED7173E37F4E32947427BE79D20CABD4ABD7C98AF5DBFD37182A05F538085040131D78AF94105D6253C0A264C719A72F1BC006487EBFD6D19E2E2F2664D0D2FF X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F2393C4755A27B53EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637829D9538242026C38638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95CE99938B3FD79E1DF345341B25479B028FE67010B1DA03042A471835C12D1D9774AD6D5ED66289B5259CC434672EE6371117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE71AE4D56B06699BBC9FA2833FD35BB23DF004C906525384302BEBFE083D3B9BA71A620F70A64A45A98AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDAE3FA6833AEA0C275ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2AD77751E876CB595E8F7B195E1C97831BA35FD678FCE4FDBFA0551508B2C535B X-C1DE0DAB: 0D63561A33F958A5DFEF5C6FE92BF3187FB863AE4673164FCE5796E84D553FBAD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34AC6E62257D6CD1C96C53F0FDB4021E8A0994A1B92B0C8B3CC5BCF88EA3D2E43C4A75345D4437DF301D7E09C32AA3244CF53178B811B0420E119F99D909102C983A76366E8A9DE7CAFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojljIiQOC84rT5Rl2H0lvkog== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822ED40F4D34A81BEBA25619540C51D0AE43841015FED1DE5223CC9A89AB576DD93FB559BB5D741EB963CF37A108A312F5C27E8A8C3839CE0E267EA787935ED9F1B X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v4 18/53] sql: introduce mem_arithmetic() 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: Vladislav Shpilevoy via Tarantool-patches Reply-To: Vladislav Shpilevoy Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Thanks for the patch! See 3 comments below. On 23.03.2021 10:35, Mergen Imeev via Tarantool-patches wrote: > This patch introduces mem_arithmetic(). Function mem_arithmetic() > executes arithmetic operations on the first and the second MEMs and > writes the result to the third MEM. > > Part of #5818 > --- > src/box/sql/mem.c | 224 +++++++++++++++++++++++++++++++++++++-------- > src/box/sql/mem.h | 6 +- > src/box/sql/vdbe.c | 111 +--------------------- > 3 files changed, 193 insertions(+), 148 deletions(-) > > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c > index f160439c9..6120939d8 100644 > --- a/src/box/sql/mem.c > +++ b/src/box/sql/mem.c > @@ -370,6 +370,192 @@ mem_concat(struct Mem *left, struct Mem *right, struct Mem *result) > return 0; > } > > +int > +mem_arithmetic(const struct Mem *left, const struct Mem *right, > + struct Mem *result, int op) > +{ > + sqlVdbeMemSetNull(result); 1. Lets use mem_clear() directly. You delete sqlVdbeMemSetNull() later anyway. > + result->field_type = FIELD_TYPE_NUMBER; > + if (((left->flags | right->flags) & MEM_Null) != 0) > + return 0; > + > + int64_t il; > + bool is_l_neg; > + double dl; > + uint16_t type_left = 0; 2. Looks surprising when you use 'l' in 3 variables before and suddenly 'left' here. Tbh, the old names with A and B looked easier to read. Up to you, I can live with both. > + if ((left->flags & MEM_Real) != 0) { > + dl = left->u.r; > + type_left = MEM_Real; > + } else if ((left->flags & MEM_Int) != 0) { > + il = left->u.i; > + type_left = MEM_Int; > + is_l_neg = true; > + } else if ((left->flags & MEM_UInt) != 0) { > + il = left->u.i; > + type_left = MEM_UInt; > + is_l_neg = false; > + } else if ((left->flags & (MEM_Str | MEM_Blob)) != 0) { > + if (sql_atoi64(left->z, &il, &is_l_neg, left->n) == 0) > + type_left = is_l_neg ? MEM_Int : MEM_UInt; > + else if (sqlAtoF(left->z, &dl, left->n) != 0) > + type_left = MEM_Real; > + } > + > + int64_t ir; > + bool is_r_neg; > + double dr; > + uint16_t type_right = 0; > + if ((right->flags & MEM_Real) != 0) { > + dr = right->u.r; > + type_right = MEM_Real; > + } else if ((right->flags & MEM_Int) != 0) { > + ir = right->u.i; > + type_right = MEM_Int; > + is_r_neg = true; > + } else if ((right->flags & MEM_UInt) != 0) { > + ir = right->u.i; > + type_right = MEM_UInt; > + is_r_neg = false; > + } else if ((right->flags & (MEM_Str | MEM_Blob)) != 0) { > + if (sql_atoi64(right->z, &ir, &is_r_neg, right->n) == 0) > + type_right = is_r_neg ? MEM_Int : MEM_UInt; > + else if (sqlAtoF(right->z, &dr, right->n) != 0) > + type_right = MEM_Real; > + } > + > + if ((type_right & (MEM_Int | MEM_UInt | MEM_Real)) == 0) { > + diag_set(ClientError, ER_SQL_TYPE_MISMATCH, > + mem_str(right), "numeric"); > + return -1; > + } > + if ((type_left & (MEM_Int | MEM_UInt | MEM_Real)) == 0) { > + diag_set(ClientError, ER_SQL_TYPE_MISMATCH, > + mem_str(left), "numeric"); > + return -1; > + } > + if (((type_left | type_right) & MEM_Real) != 0) { > + if (type_left == MEM_Int) > + dl = (double)il; > + else if (type_left == MEM_UInt) > + dl = (double)(uint64_t)il; > + > + if (type_right == MEM_Int) > + dr = (double)ir; > + else if (type_right == MEM_UInt) > + dr = (double)(uint64_t)ir; > + > + double dres; > + switch(op) { > + case OP_Add: > + dres = dl + dr; > + break; > + case OP_Subtract: > + dres = dl - dr; > + break; > + case OP_Multiply: > + dres = dl * dr; > + break; > + case OP_Divide: > + if (dr == 0.) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "division by zero"); > + return -1; > + } > + dres = dl / dr; > + break; > + case OP_Remainder: { > + int64_t il = (int64_t)dl; > + int64_t ir = (int64_t)dr; > + if (ir == 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "division by zero"); > + return -1; > + } > + if (ir == -1) > + ir = 1; > + dres = (double)(il % ir); > + break; > + } > + default: > + unreachable(); > + } > + if (sqlIsNaN(dres)) > + return 0; > + result->u.r = dres; > + result->flags = MEM_Real; > + return 0; > + } > + int64_t ires; > + /* > + * TODO: This is wrong. Both these flags should already be set. This > + * assignment done to not change behaviour of the function, which > + * is currently bugged. > + */ > + is_l_neg = (left->flags & MEM_Int) != 0; > + is_r_neg = (right->flags & MEM_Int) != 0; > + bool is_res_neg; > + switch(op) { > + case OP_Add: > + if (sql_add_int(il, is_l_neg, ir, is_r_neg, &ires, > + &is_res_neg) != 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "integer is overflowed"); > + return -1; > + } > + break; > + case OP_Subtract: > + if (sql_sub_int(il, is_l_neg, ir, is_r_neg, &ires, > + &is_res_neg) != 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "integer is overflowed"); > + return -1; > + } > + break; > + case OP_Multiply: > + if (sql_mul_int(il, is_l_neg, ir, is_r_neg, &ires, > + &is_res_neg) != 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "integer is overflowed"); > + return -1; > + } > + break; > + case OP_Divide: > + if (ir == 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "division by zero"); > + return -1; > + } > + if (sql_div_int(il, is_l_neg, ir, is_r_neg, &ires, > + &is_res_neg) != 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "integer is overflowed"); > + return -1; > + } > + break; > + case OP_Remainder: { > + if (ir == 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "division by zero"); > + return -1; > + } > + if (ir == -1) > + ir = 1; > + if (sql_rem_int(il, is_l_neg, ir, is_r_neg, &ires, > + &is_res_neg) != 0) { > + diag_set(ClientError, ER_SQL_EXECUTE, > + "integer is overflowed"); > + return -1; > + } > + break; > + } > + default: > + unreachable(); > + } > + result->u.i = ires; > + result->flags = is_res_neg ? MEM_Int : MEM_UInt; > + return 0; 3. The original code looked shorter and easier to read. Why did you pad it out so much? Additionally, since you now have a function for doing arith, you could make one function for each opcode with common parts as a static functions in mem.c, and call them right from 'case's in vdbe.c. So instead of one big case,case,case,case: arith which also has a switch inside, we would have proper case: plus, case: multiply, etc. Would be faster and would make each individual function simpler I think. The names could be mem_arith_plus(), mem_arith_mul(), mem_arith_minus(), etc.