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 52E936EC40; Tue, 17 Aug 2021 15:23:58 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 52E936EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1629203038; bh=P5suMleqCjz7zw40VGvSaMx/S95dkFspapWIXoMUUQM=; 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=CZbDBhpijSHHC6YeYDrs/7gqICNOXyOJyakzCurLpMOBCfDH19QhIzDjjyAAX/mOu kUAG0mxtWc/Mr5fPjfl+ABNJ7EcnU/WOZuA+TfFvIGA84tpyZhLcqhleTMpb9A8qTK 5p/Xs06WXC37wF4Ka/DfY/ZeVFQ6XTjj4Ydawd8k= Received: from smtp46.i.mail.ru (smtp46.i.mail.ru [94.100.177.106]) (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 D93516EC40 for ; Tue, 17 Aug 2021 15:23:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D93516EC40 Received: by smtp46.i.mail.ru with esmtpa (envelope-from ) id 1mFy8I-0003v1-Ot; Tue, 17 Aug 2021 15:23:55 +0300 To: Safin Timur , imeevma@tarantool.org, korablev@tarantool.org Cc: tarantool-patches@dev.tarantool.org References: <20b1ddcb-2cbc-5cad-9036-5c317c9f7c71@tarantool.org> Message-ID: <06ce85a2-d053-70a6-4dc3-bd84c16ca160@tarantool.org> Date: Tue, 17 Aug 2021 15:23:54 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: <20b1ddcb-2cbc-5cad-9036-5c317c9f7c71@tarantool.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD9736CF3E71F18CE0C3E1D5927724F4AAA182A05F53808504063C713CD9D7AD86DECC629ADD5C1D42B8B102E4B2D9A503D33D670438FAA9E44 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76C0A440987CA342DC2099A533E45F2D0395957E7521B51C2CFCAF695D4D8E9FCEA1F7E6F0F101C6778DA827A17800CE74323F140F3EE5B6A8F08D7030A58E5AD1A62830130A00468AEEEE3FBA3A834EE7353EFBB553375665B73F950BC6E7FFB18388F4300952A89D63E60726DB46929A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE77A825AB47F0FC8649FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C37EF884183F8E4D67117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE1407959CC434672EE6371089D37D7C0E48F6C8AA50765F79006373BA04B6A498D0BA4731C566533BA786AA5CC5B56E945C8DA X-C1DE0DAB: 0D63561A33F958A5E8177C69AB9DE71EB8B1E3CA10F58114D6E4B652725A0619D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7567C209D01CC1E34B410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34B5900AD87B4159A41EB765A7CC81B9CDF029EA54076FA5209313AFC74BEE280C7BC5845EDD6EB4C91D7E09C32AA3244C34C2D892B9F343921767B3D0F7540250BBA718C7E6A9E042927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojuRQ/H5n28trxo/fq8z3OWw== X-Mailru-Sender: 3B9A0136629DC9125D61937A2360A446E8852A26AAE48213721192FED892DD4CE6AA8FE5C48F72C4424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v1 4/4] sql: introduce decimal to 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: Serge Petrenko via Tarantool-patches Reply-To: Serge Petrenko Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 16.08.2021 22:48, Safin Timur пишет: > > > On 16.08.2021 18:57, Mergen Imeev via Tarantool-patches wrote: >> This patch introduces arithmetic for DECIMAL in SQL. After this patch, >> DECIMAL values can participate in arithmetic along with INTEGER, >> UNSIGNED, DOUBLE, and other DECIMAL values. >> >> Part of #4415 >> --- >>   src/box/sql/mem.c             | 124 +++++++++++++++++++++++++++++- >>   test/sql-tap/decimal.test.lua | 141 +++++++++++++++++++++++++++++++++- >>   2 files changed, 262 insertions(+), 3 deletions(-) >> >> diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c >> index ff8b40d7f..a4ec98f34 100644 >> --- a/src/box/sql/mem.c >> +++ b/src/box/sql/mem.c >> @@ -1733,6 +1733,18 @@ mem_get_int(const struct Mem *mem, int64_t *i, >> bool >> *is_neg) >>           } >>           return -1; >>       } >> +    if (mem->type == MEM_TYPE_DEC) { >> +        if (decimal_is_neg(&mem->u.d)) { >> +            if (decimal_to_int64(&mem->u.d, i) == NULL) >> +                return -1; >> +            *is_neg = *i < 0; >> +            return 0; >> +        } >> +        if (decimal_to_uint64(&mem->u.d, (uint64_t *)i) == NULL) >> +            return -1; >> +        *is_neg = false; >> +        return 0; >> +    } >>       return -1; >>   } >>   @@ -1760,6 +1772,19 @@ mem_get_uint(const struct Mem *mem, uint64_t >> *u) >>           } >>           return -1; >>       } >> +    if (mem->type == MEM_TYPE_DEC) { >> +        if (decimal_is_neg(&mem->u.d)) { >> +            int64_t i; >> +            if (decimal_to_int64(&mem->u.d, &i) == NULL || i < >> 0) >> +                return -1; >> +            assert(i == 0); >> +            *u = 0; >> +            return 0; >> +        } >> +        if (decimal_to_uint64(&mem->u.d, u) == NULL) >> +            return -1; >> +        return 0; >> +    } >>       return -1; >>   } >>   @@ -1778,6 +1803,10 @@ mem_get_double(const struct Mem *mem, double >> *d) >>           *d = (double)mem->u.u; >>           return 0; >>       } >> +    if (mem->type == MEM_TYPE_DEC) { >> +        *d = atof(decimal_str(&mem->u.d)); >> +        return 0; >> +    } > > 1st question is - was it intentionally that you call here atof, while > few lines belowe we use sqlAtoF? > > 2nd complain is - it all looks that we miss decimal_to_double() > function in src/core/decimal.c (where we do have decimal_from_double() > but not the reverse direction). It will look more consistent if this > implementation would be there. And, I stll believe, there is better > way for converting decimal to double, than converting it to string, > and then to double. > > [Though quick navigation over decNumber API didn't reveal the direct > approach. Serge, could you please recommend the easiest way here?] I agree, it would be best to introduce decimal_to_double() to src/lib/core/decimal I can't think of an easy way here. The only one I see is construct the double value digit by digit. This mustn't be too different from decimal->string->double conversion. > > >> @@ -1946,12 +2003,12 @@ mem_concat(struct Mem *a, struct Mem *b, struct >> Mem *result) >>   static inline int >>   check_types_numeric_arithmetic(const struct Mem *a, const struct >> Mem *b) >>   { >> -    if (!mem_is_num(a) || mem_is_metatype(a) || a->type == >> MEM_TYPE_DEC) { >> +    if (!mem_is_num(a) || mem_is_metatype(a)) { > > And now it looks better than before, and less confusing, I do not have > complains here anymore... > > >> diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(a), >>                "integer, unsigned or double"); >>           return -1; >>       } >> -    if (!mem_is_num(b) || mem_is_metatype(b) || b->type == >> MEM_TYPE_DEC) { >> +    if (!mem_is_num(b) || mem_is_metatype(b)) { > > And here. > >> diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(b), >>                "integer, unsigned or double"); >>           return -1; > > Thanks, > Timur -- Serge Petrenko