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 112466B962; Wed, 14 Apr 2021 02:21:33 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 112466B962 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618356093; bh=LdDO+Ivu1tnkes175ATiAjUKZqLKr1EFkc0zU5a89/Y=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=MNidP8HYkwDifpoXwRIyan2ZgiFLXOjKBLTMEcMGx5ooKgdK8kbFY9c963h8Ymvp2 NmKnDpbiRyhmpdF5HYK/sxeZQR5NHPcw2UOnkdkNGeU/42zKpLTu9fVR+phSndxnUn xnAdjjSawWtpB3oqrsl66qhKAuH4bCVYlZDRJkag= Received: from smtp61.i.mail.ru (smtp61.i.mail.ru [217.69.128.41]) (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 3645E6EC5B for ; Wed, 14 Apr 2021 02:21:31 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3645E6EC5B Received: by smtp61.i.mail.ru with esmtpa (envelope-from ) id 1lWSLa-0004mo-6c; Wed, 14 Apr 2021 02:21:30 +0300 Date: Wed, 14 Apr 2021 02:21:28 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org, tarantool-patches@dev.tarantool.org Message-ID: <20210413232128.GA85124@tarantool.org> References: <5cb7ed681028bcd7def0d2a66b5a2f9530fd1d98.1617984948.git.imeevma@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <5cb7ed681028bcd7def0d2a66b5a2f9530fd1d98.1617984948.git.imeevma@gmail.com> X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480D608FE24BC85426BB1B55F651FED8C70182A05F538085040EE3EFFC2DC04CE901A1296B426ABE257497438942EAB0A43B71A667D646C32D4 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE705B093C0FC4B30B9EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637E6006D770ADD73CF8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B25F92040E692B8771846EC84C66723A710E47C10036089472D2E47CDBA5A96583C09775C1D3CA48CFED8438A78DFE0A9E117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE709B92020B71E24959FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE78B1055E99C449777D32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C0F4E627101BDF3ED1CD04E86FAF290E2D7E9C4E3C761E06A71DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3727597FF642BA4D735872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2368A440D3B0F6089093C9A16E5BC824A2A04A2ABAA09D2538DECB10C4E1BE3BCB8CCF60F6B7FD3948E1CD14B953EB46D822D4A633E407B0C355D89D7DBCDD132 X-C1DE0DAB: 0D63561A33F958A5348FF433A0DAD1999C59AC2F2D697E24F05C41F9B09504BED59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D347324AA9FA07FF01E42AABFE654C9B257E584AD857B8EB7A406FA499CC9C9B58CA0718C2466EBF0E81D7E09C32AA3244C750EEF09699384ADA064C5C709EA2014A8CE788DE6831205FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojnA7/qPBUIXHKxb5eY0YXtQ== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED89167D855825BFEB1A1296B426ABE25721401CF93FD6FA555105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v5 40/52] sql: introduce mem_to_double() 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" Grouped _to_() functions and added assert to make sure that mem_to_double() accepts only values of simple types. Diff and new patch below. On Fri, Apr 09, 2021 at 11:26:00PM +0300, Mergen Imeev via Tarantool-patches wrote: > This patch intruduces mem_to_double(). This function is used to convert > a MEM to a MEM that contains double value. This function defines the > rules that are used during conversion from values of all other types to > double. > > Part of #5818 > --- > src/box/sql/mem.c | 81 +++++++++++++++++++++++----------------------- > src/box/sql/mem.h | 8 ++++- > src/box/sql/vdbe.c | 2 +- > 3 files changed, 48 insertions(+), 43 deletions(-) > > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c > index d3a3215bc..75d4c4d18 100644 > --- a/src/box/sql/mem.c > +++ b/src/box/sql/mem.c > @@ -771,6 +771,42 @@ mem_to_int_precise(struct Mem *mem) > return -1; > } > > +static inline int > +int_to_double(struct Mem *mem) > +{ > + double d; > + if ((mem->flags & MEM_UInt) != 0) > + d = (double)mem->u.u; > + else > + d = (double)mem->u.i; > + mem->u.r = d; > + mem->flags = MEM_Real; > + mem->field_type = FIELD_TYPE_DOUBLE; > + return 0; > +} > + > +static inline int > +bytes_to_double(struct Mem *mem) > +{ > + double d; > + if (sqlAtoF(mem->z, &d, mem->n) == 0) > + return -1; > + mem_set_double(mem, d); > + return 0; > +} > + > +int > +mem_to_double(struct Mem *mem) > +{ > + if ((mem->flags & MEM_Real) != 0) > + return 0; > + if ((mem->flags & (MEM_Int | MEM_UInt)) != 0) > + return int_to_double(mem); > + if ((mem->flags & MEM_Str) != 0) > + return bytes_to_double(mem); > + return -1; > +} > + > int > mem_copy(struct Mem *to, const struct Mem *from) > { > @@ -1861,21 +1897,6 @@ mem_apply_numeric_type(struct Mem *record) > return 0; > } > > -/* > - * Convert pMem so that it is of type MEM_Real. > - * Invalidate any prior representations. > - */ > -int > -sqlVdbeMemRealify(Mem * pMem) > -{ > - assert(EIGHT_BYTE_ALIGNMENT(pMem)); > - double v; > - if (sqlVdbeRealValue(pMem, &v)) > - return -1; > - mem_set_double(pMem, v); > - return 0; > -} > - > int > vdbe_mem_numerify(struct Mem *mem) > { > @@ -1979,7 +2000,7 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) > return -1; > return 0; > case FIELD_TYPE_DOUBLE: > - return sqlVdbeMemRealify(pMem); > + return mem_to_double(pMem); > case FIELD_TYPE_NUMBER: > return vdbe_mem_numerify(pMem); > case FIELD_TYPE_VARBINARY: > @@ -2185,11 +2206,11 @@ mem_apply_type(struct Mem *record, enum field_type type) > case FIELD_TYPE_NUMBER: > if ((record->flags & (MEM_Real | MEM_Int | MEM_UInt)) != 0) > return 0; > - return sqlVdbeMemRealify(record); > + return mem_to_double(record); > case FIELD_TYPE_DOUBLE: > if ((record->flags & MEM_Real) != 0) > return 0; > - return sqlVdbeMemRealify(record); > + return mem_to_double(record); > case FIELD_TYPE_STRING: > /* > * Only attempt the conversion to TEXT if there is > @@ -2233,28 +2254,6 @@ mem_apply_type(struct Mem *record, enum field_type type) > } > } > > -/** > - * Convert the numeric value contained in MEM to double. > - * > - * @param mem The MEM that contains the numeric value. > - * @retval 0 if the conversion was successful, -1 otherwise. > - */ > -static int > -mem_convert_to_double(struct Mem *mem) > -{ > - if ((mem->flags & MEM_Real) != 0) > - return 0; > - if ((mem->flags & (MEM_Int | MEM_UInt)) == 0) > - return -1; > - double d; > - if ((mem->flags & MEM_Int) != 0) > - d = (double)mem->u.i; > - else > - d = (double)mem->u.u; > - mem_set_double(mem, d); > - return 0; > -} > - > /** > * Convert the numeric value contained in MEM to unsigned. > * > @@ -2285,7 +2284,7 @@ mem_convert_to_numeric(struct Mem *mem, enum field_type type) > assert(mem_is_num(mem) && sql_type_is_numeric(type)); > assert(type != FIELD_TYPE_NUMBER); > if (type == FIELD_TYPE_DOUBLE) > - return mem_convert_to_double(mem); > + return mem_to_double(mem); > if (type == FIELD_TYPE_UNSIGNED) > return mem_convert_to_unsigned(mem); > assert(type == FIELD_TYPE_INTEGER); > diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h > index d3eb04c44..bf8c0f3b5 100644 > --- a/src/box/sql/mem.h > +++ b/src/box/sql/mem.h > @@ -491,6 +491,13 @@ mem_to_int(struct Mem *mem); > int > mem_to_int_precise(struct Mem *mem); > > +/** > + * Convert the given MEM to DOUBLE. This function defines the rules that are > + * used to convert values of all other types to DOUBLE. > + */ > +int > +mem_to_double(struct Mem *mem); > + > /** > * Simple type to str convertor. It is used to simplify > * error reporting. > @@ -537,7 +544,6 @@ registerTrace(int iReg, Mem *p); > */ > int > mem_apply_numeric_type(struct Mem *record); > -int sqlVdbeMemRealify(struct Mem *); > > /** > * Convert @a mem to NUMBER type, so that after conversion it has > diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c > index e61ad4251..90a901555 100644 > --- a/src/box/sql/vdbe.c > +++ b/src/box/sql/vdbe.c > @@ -1456,7 +1456,7 @@ case OP_MustBeInt: { /* jump, in1 */ > case OP_Realify: { /* in1 */ > pIn1 = &aMem[pOp->p1]; > if (mem_is_int(pIn1)) { > - sqlVdbeMemRealify(pIn1); > + mem_to_double(pIn1); > } > break; > } > -- > 2.25.1 > Diff: diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 9972b1f85..3685bf6b9 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -492,6 +492,20 @@ mem_set_null_clear(struct Mem *mem) mem->flags = MEM_Null | MEM_Cleared; } +static inline int +int_to_double(struct Mem *mem) +{ + double d; + if ((mem->flags & MEM_UInt) != 0) + d = (double)mem->u.u; + else + d = (double)mem->u.i; + mem->u.r = d; + mem->flags = MEM_Real; + mem->field_type = FIELD_TYPE_DOUBLE; + return 0; +} + static inline int bytes_to_int(struct Mem *mem) { @@ -503,6 +517,16 @@ bytes_to_int(struct Mem *mem) return 0; } +static inline int +bytes_to_double(struct Mem *mem) +{ + double d; + if (sqlAtoF(mem->z, &d, mem->n) == 0) + return -1; + mem_set_double(mem, d); + return 0; +} + static inline int double_to_int(struct Mem *mem) { @@ -578,33 +602,10 @@ mem_to_int_precise(struct Mem *mem) return -1; } -static inline int -int_to_double(struct Mem *mem) -{ - double d; - if ((mem->flags & MEM_UInt) != 0) - d = (double)mem->u.u; - else - d = (double)mem->u.i; - mem->u.r = d; - mem->flags = MEM_Real; - mem->field_type = FIELD_TYPE_DOUBLE; - return 0; -} - -static inline int -bytes_to_double(struct Mem *mem) -{ - double d; - if (sqlAtoF(mem->z, &d, mem->n) == 0) - return -1; - mem_set_double(mem, d); - return 0; -} - int mem_to_double(struct Mem *mem) { + assert((mem->flags & MEM_PURE_TYPE_MASK) != 0); if ((mem->flags & MEM_Real) != 0) return 0; if ((mem->flags & (MEM_Int | MEM_UInt)) != 0) New patch: commit f7e8e09f0160dd0cf933e4bfc29d108603291b79 Author: Mergen Imeev Date: Wed Mar 17 11:03:22 2021 +0300 sql: introduce mem_to_double() This patch intruduces mem_to_double(). This function is used to convert a MEM to a MEM that contains double value. This function defines the rules that are used during conversion from values of all other types to double. Part of #5818 diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index b22b4ba21..3685bf6b9 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -492,6 +492,20 @@ mem_set_null_clear(struct Mem *mem) mem->flags = MEM_Null | MEM_Cleared; } +static inline int +int_to_double(struct Mem *mem) +{ + double d; + if ((mem->flags & MEM_UInt) != 0) + d = (double)mem->u.u; + else + d = (double)mem->u.i; + mem->u.r = d; + mem->flags = MEM_Real; + mem->field_type = FIELD_TYPE_DOUBLE; + return 0; +} + static inline int bytes_to_int(struct Mem *mem) { @@ -503,6 +517,16 @@ bytes_to_int(struct Mem *mem) return 0; } +static inline int +bytes_to_double(struct Mem *mem) +{ + double d; + if (sqlAtoF(mem->z, &d, mem->n) == 0) + return -1; + mem_set_double(mem, d); + return 0; +} + static inline int double_to_int(struct Mem *mem) { @@ -578,6 +602,19 @@ mem_to_int_precise(struct Mem *mem) return -1; } +int +mem_to_double(struct Mem *mem) +{ + assert((mem->flags & MEM_PURE_TYPE_MASK) != 0); + if ((mem->flags & MEM_Real) != 0) + return 0; + if ((mem->flags & (MEM_Int | MEM_UInt)) != 0) + return int_to_double(mem); + if ((mem->flags & MEM_Str) != 0) + return bytes_to_double(mem); + return -1; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -1633,21 +1670,6 @@ mem_apply_numeric_type(struct Mem *record) return 0; } -/* - * Convert pMem so that it is of type MEM_Real. - * Invalidate any prior representations. - */ -int -sqlVdbeMemRealify(Mem * pMem) -{ - assert(EIGHT_BYTE_ALIGNMENT(pMem)); - double v; - if (sqlVdbeRealValue(pMem, &v)) - return -1; - mem_set_double(pMem, v); - return 0; -} - int vdbe_mem_numerify(struct Mem *mem) { @@ -1751,7 +1773,7 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) return -1; return 0; case FIELD_TYPE_DOUBLE: - return sqlVdbeMemRealify(pMem); + return mem_to_double(pMem); case FIELD_TYPE_NUMBER: return vdbe_mem_numerify(pMem); case FIELD_TYPE_VARBINARY: @@ -1957,11 +1979,11 @@ mem_apply_type(struct Mem *record, enum field_type type) case FIELD_TYPE_NUMBER: if ((record->flags & (MEM_Real | MEM_Int | MEM_UInt)) != 0) return 0; - return sqlVdbeMemRealify(record); + return mem_to_double(record); case FIELD_TYPE_DOUBLE: if ((record->flags & MEM_Real) != 0) return 0; - return sqlVdbeMemRealify(record); + return mem_to_double(record); case FIELD_TYPE_STRING: /* * Only attempt the conversion to TEXT if there is @@ -2005,28 +2027,6 @@ mem_apply_type(struct Mem *record, enum field_type type) } } -/** - * Convert the numeric value contained in MEM to double. - * - * @param mem The MEM that contains the numeric value. - * @retval 0 if the conversion was successful, -1 otherwise. - */ -static int -mem_convert_to_double(struct Mem *mem) -{ - if ((mem->flags & MEM_Real) != 0) - return 0; - if ((mem->flags & (MEM_Int | MEM_UInt)) == 0) - return -1; - double d; - if ((mem->flags & MEM_Int) != 0) - d = (double)mem->u.i; - else - d = (double)mem->u.u; - mem_set_double(mem, d); - return 0; -} - /** * Convert the numeric value contained in MEM to unsigned. * @@ -2057,7 +2057,7 @@ mem_convert_to_numeric(struct Mem *mem, enum field_type type) assert(mem_is_num(mem) && sql_type_is_numeric(type)); assert(type != FIELD_TYPE_NUMBER); if (type == FIELD_TYPE_DOUBLE) - return mem_convert_to_double(mem); + return mem_to_double(mem); if (type == FIELD_TYPE_UNSIGNED) return mem_convert_to_unsigned(mem); assert(type == FIELD_TYPE_INTEGER); diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 5aabcc1fd..485a6fed0 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -701,6 +701,13 @@ mem_to_int(struct Mem *mem); int mem_to_int_precise(struct Mem *mem); +/** + * Convert the given MEM to DOUBLE. This function defines the rules that are + * used to convert values of all other types to DOUBLE. + */ +int +mem_to_double(struct Mem *mem); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -747,7 +754,6 @@ registerTrace(int iReg, Mem *p); */ int mem_apply_numeric_type(struct Mem *record); -int sqlVdbeMemRealify(struct Mem *); /** * Convert @a mem to NUMBER type, so that after conversion it has diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index c908f13c0..008148687 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -1457,7 +1457,7 @@ case OP_MustBeInt: { /* jump, in1 */ case OP_Realify: { /* in1 */ pIn1 = &aMem[pOp->p1]; if (mem_is_int(pIn1)) { - sqlVdbeMemRealify(pIn1); + mem_to_double(pIn1); } break; }