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 AA3426B962; Wed, 14 Apr 2021 02:25:51 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org AA3426B962 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618356351; bh=F4jg0nSptsjPLN31BftUBH9aAvh9OwUn2m4x8buX774=; 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=KfmJHIfdknve+qtoNRzyNQB82jthO110sjvLmGU2r7IFj34TafntGDCO02tCgyPZB FG9bYwoFuFJuwc9tfhQY/yExusgvxWii33knwxq1e2njNBrJ/sKFc/qXlDw0xbdol/ Hs4W4GAwvnGJ9U0rQDTpQkkv/jz4gF4opD6UtKIE= Received: from smtp51.i.mail.ru (smtp51.i.mail.ru [94.100.177.111]) (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 05A2C6EC5B for ; Wed, 14 Apr 2021 02:25:50 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 05A2C6EC5B Received: by smtp51.i.mail.ru with esmtpa (envelope-from ) id 1lWSPl-0007gZ-5Z; Wed, 14 Apr 2021 02:25:49 +0300 Date: Wed, 14 Apr 2021 02:25:47 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org, tarantool-patches@dev.tarantool.org Message-ID: <20210413232547.GA91501@tarantool.org> References: <5b762310118a0e6c10c305098d5d53851a81a2e6.1618000037.git.imeevma@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <5b762310118a0e6c10c305098d5d53851a81a2e6.1618000037.git.imeevma@gmail.com> X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480B1C8842CE613979723F2FB4628545A35182A05F538085040048DD0F9EB736712B7C35D70379652912083723F17403FE702BA088EF147B98A X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7B114C2C2C20B7E62EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C5BAFDCCAC60DA968638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B22752D299DD18391B760532D35C092D49C5BC91295CC49E52D2E47CDBA5A96583C09775C1D3CA48CF17B107DEF921CE79117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE709B92020B71E24959FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE7AC4C3442B3CEE4CDD32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C07EFB3CC2C5EB17D3CD04E86FAF290E2D7E9C4E3C761E06A71DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3727597FF642BA4D735872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24209795067102C07E8F7B195E1C9783122502DE2BFB7FED1AC1B55117025DB7A X-C1DE0DAB: 0D63561A33F958A51C29BCDCD606DEB8605EE1BE7C43FD40726C4393AD81866ED59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D349A401E2B4D763A4AAAE82107B0D449F4C7B39576D90D4D9E037B6DA8F7A0406BFFC8235896C195191D7E09C32AA3244C34DE032152FB3BA85E8B3C657707A0CA63871F383B54D9B3FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojnA7/qPBUIXHCFzFjp71YFw== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED74CAA0D031351D42B7C35D70379652916ECF975C7BB2667F5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v5 41/52] sql: introduce mem_to_number() 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" Thank you for the review! Added assert. Diff and new patch below. On Fri, Apr 09, 2021 at 11:53:38PM +0300, Mergen Imeev via Tarantool-patches wrote: > This patch introduces mem_to_number(). This function is used to convert > MEM to MEM contains number. > > Part of #5818 > --- > src/box/sql/func.c | 2 +- > src/box/sql/mem.c | 60 +++++++++++++--------------------------------- > src/box/sql/mem.h | 32 ++++++------------------- > src/box/sql/vdbe.c | 2 +- > 4 files changed, 25 insertions(+), 71 deletions(-) > > diff --git a/src/box/sql/func.c b/src/box/sql/func.c > index 0b85bf365..0282aec74 100644 > --- a/src/box/sql/func.c > +++ b/src/box/sql/func.c > @@ -1641,7 +1641,7 @@ sum_step(struct sql_context *context, int argc, sql_value **argv) > if (type == MP_NIL || p == NULL) > return; > if (type != MP_DOUBLE && type != MP_INT && type != MP_UINT) { > - if (mem_apply_numeric_type(argv[0]) != 0) { > + if (type != MP_STR || mem_to_number(argv[0]) != 0) { > diag_set(ClientError, ER_SQL_TYPE_MISMATCH, > mem_str(argv[0]), "number"); > context->is_aborted = true; > diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c > index 75d4c4d18..9a0234e60 100644 > --- a/src/box/sql/mem.c > +++ b/src/box/sql/mem.c > @@ -807,6 +807,21 @@ mem_to_double(struct Mem *mem) > return -1; > } > > +int > +mem_to_number(struct Mem *mem) > +{ > + if ((mem->flags & (MEM_Int | MEM_UInt | MEM_Real)) != 0) > + return 0; > + if ((mem->flags & MEM_Bool) != 0) > + return bool_to_int(mem); > + if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) { > + if (bytes_to_int(mem) == 0) > + return 0; > + return bytes_to_double(mem); > + } > + return -1; > +} > + > int > mem_copy(struct Mem *to, const struct Mem *from) > { > @@ -1879,49 +1894,6 @@ registerTrace(int iReg, Mem *p) { > } > #endif > > -int > -mem_apply_numeric_type(struct Mem *record) > -{ > - if ((record->flags & MEM_Str) == 0) > - return -1; > - int64_t integer_value; > - bool is_neg; > - if (sql_atoi64(record->z, &integer_value, &is_neg, record->n) == 0) { > - mem_set_int(record, integer_value, is_neg); > - return 0; > - } > - double float_value; > - if (sqlAtoF(record->z, &float_value, record->n) == 0) > - return -1; > - mem_set_double(record, float_value); > - return 0; > -} > - > -int > -vdbe_mem_numerify(struct Mem *mem) > -{ > - if ((mem->flags & (MEM_Int | MEM_UInt | MEM_Real | MEM_Null)) != 0) > - return 0; > - if ((mem->flags & MEM_Bool) != 0) { > - mem->u.u = (uint64_t)mem->u.b; > - mem->flags = MEM_UInt; > - mem->field_type = FIELD_TYPE_UNSIGNED; > - return 0; > - } > - assert((mem->flags & (MEM_Blob | MEM_Str)) != 0); > - bool is_neg; > - int64_t i; > - if (sql_atoi64(mem->z, &i, &is_neg, mem->n) == 0) { > - mem_set_int(mem, i, is_neg); > - } else { > - double d; > - if (sqlAtoF(mem->z, &d, mem->n) == 0) > - return -1; > - mem_set_double(mem, d); > - } > - return 0; > -} > - > /* > * Cast the datatype of the value in pMem according to the type > * @type. Casting is different from applying type in that a cast > @@ -2002,7 +1974,7 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) > case FIELD_TYPE_DOUBLE: > return mem_to_double(pMem); > case FIELD_TYPE_NUMBER: > - return vdbe_mem_numerify(pMem); > + return mem_to_number(pMem); > case FIELD_TYPE_VARBINARY: > if ((pMem->flags & MEM_Blob) != 0) > return 0; > diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h > index bf8c0f3b5..90f46af80 100644 > --- a/src/box/sql/mem.h > +++ b/src/box/sql/mem.h > @@ -498,6 +498,13 @@ mem_to_int_precise(struct Mem *mem); > int > mem_to_double(struct Mem *mem); > > +/** > + * Convert the given MEM to NUMBER. This function defines the rules that are > + * used to convert values of all other types to NUMBER. > + */ > +int > +mem_to_number(struct Mem *mem); > + > /** > * Simple type to str convertor. It is used to simplify > * error reporting. > @@ -531,31 +538,6 @@ registerTrace(int iReg, Mem *p); > #define memIsValid(M) !mem_is_invalid(M) > #endif > > -/** > - * Try to convert a string value into a numeric representation > - * if we can do so without loss of information. Firstly, value > - * is attempted to be converted to integer, and in case of fail - > - * to floating point number. Note that function is assumed to be > - * called on memory cell containing string, i.e. mem->type == MEM_Str. > - * > - * @param record Memory cell containing value to be converted. > - * @retval 0 If value can be converted to integer or number. > - * @retval -1 Otherwise. > - */ > -int > -mem_apply_numeric_type(struct Mem *record); > - > -/** > - * Convert @a mem to NUMBER type, so that after conversion it has > - * one of types MEM_Real, MEM_Int or MEM_UInt. If conversion is > - * not possible, function returns -1. > - * > - * Beware - this function changes value and type of @a mem > - * argument. > - */ > -int > -vdbe_mem_numerify(struct Mem *mem); > - > int sqlVdbeMemCast(struct Mem *, enum field_type type); > int sqlVdbeMemStringify(struct Mem *); > int sqlVdbeMemNulTerminate(struct Mem *); > diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c > index 90a901555..7880af248 100644 > --- a/src/box/sql/vdbe.c > +++ b/src/box/sql/vdbe.c > @@ -2712,7 +2712,7 @@ case OP_SeekGT: { /* jump, in3 */ > if (mem_is_null(pIn3)) > goto skip_truncate; > if (mem_is_str(pIn3)) > - mem_apply_numeric_type(pIn3); > + mem_to_number(pIn3); > int64_t i; > if (mem_is_uint(pIn3)) { > i = pIn3->u.u; > -- > 2.25.1 > Diff: diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 0c234b18b..990e261b7 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -618,6 +618,7 @@ mem_to_double(struct Mem *mem) int mem_to_number(struct Mem *mem) { + assert((mem->flags & MEM_PURE_TYPE_MASK) != 0); if ((mem->flags & (MEM_Int | MEM_UInt | MEM_Real)) != 0) return 0; if ((mem->flags & MEM_Bool) != 0) New patch: commit fba444e64b55308f20df87d44ca31f807180cf10 Author: Mergen Imeev Date: Wed Mar 17 11:39:13 2021 +0300 sql: introduce mem_to_number() This patch introduces mem_to_number(). This function is used to convert MEM to MEM contains number. Part of #5818 diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 0b85bf365..0282aec74 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1641,7 +1641,7 @@ sum_step(struct sql_context *context, int argc, sql_value **argv) if (type == MP_NIL || p == NULL) return; if (type != MP_DOUBLE && type != MP_INT && type != MP_UINT) { - if (mem_apply_numeric_type(argv[0]) != 0) { + if (type != MP_STR || mem_to_number(argv[0]) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(argv[0]), "number"); context->is_aborted = true; diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 3685bf6b9..990e261b7 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -615,6 +615,22 @@ mem_to_double(struct Mem *mem) return -1; } +int +mem_to_number(struct Mem *mem) +{ + assert((mem->flags & MEM_PURE_TYPE_MASK) != 0); + if ((mem->flags & (MEM_Int | MEM_UInt | MEM_Real)) != 0) + return 0; + if ((mem->flags & MEM_Bool) != 0) + return bool_to_int(mem); + if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) { + if (bytes_to_int(mem) == 0) + return 0; + return bytes_to_double(mem); + } + return -1; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -1652,49 +1668,6 @@ registerTrace(int iReg, Mem *p) { } #endif -int -mem_apply_numeric_type(struct Mem *record) -{ - if ((record->flags & MEM_Str) == 0) - return -1; - int64_t integer_value; - bool is_neg; - if (sql_atoi64(record->z, &integer_value, &is_neg, record->n) == 0) { - mem_set_int(record, integer_value, is_neg); - return 0; - } - double float_value; - if (sqlAtoF(record->z, &float_value, record->n) == 0) - return -1; - mem_set_double(record, float_value); - return 0; -} - -int -vdbe_mem_numerify(struct Mem *mem) -{ - if ((mem->flags & (MEM_Int | MEM_UInt | MEM_Real | MEM_Null)) != 0) - return 0; - if ((mem->flags & MEM_Bool) != 0) { - mem->u.u = (uint64_t)mem->u.b; - mem->flags = MEM_UInt; - mem->field_type = FIELD_TYPE_UNSIGNED; - return 0; - } - assert((mem->flags & (MEM_Blob | MEM_Str)) != 0); - bool is_neg; - int64_t i; - if (sql_atoi64(mem->z, &i, &is_neg, mem->n) == 0) { - mem_set_int(mem, i, is_neg); - } else { - double d; - if (sqlAtoF(mem->z, &d, mem->n) == 0) - return -1; - mem_set_double(mem, d); - } - return 0; -} - /* * Cast the datatype of the value in pMem according to the type * @type. Casting is different from applying type in that a cast @@ -1775,7 +1748,7 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) case FIELD_TYPE_DOUBLE: return mem_to_double(pMem); case FIELD_TYPE_NUMBER: - return vdbe_mem_numerify(pMem); + return mem_to_number(pMem); case FIELD_TYPE_VARBINARY: if ((pMem->flags & MEM_Blob) != 0) return 0; diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 485a6fed0..85ad94ab3 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -708,6 +708,13 @@ mem_to_int_precise(struct Mem *mem); int mem_to_double(struct Mem *mem); +/** + * Convert the given MEM to NUMBER. This function defines the rules that are + * used to convert values of all other types to NUMBER. + */ +int +mem_to_number(struct Mem *mem); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -741,31 +748,6 @@ registerTrace(int iReg, Mem *p); #define memIsValid(M) ((M)->flags & MEM_Undefined)==0 #endif -/** - * Try to convert a string value into a numeric representation - * if we can do so without loss of information. Firstly, value - * is attempted to be converted to integer, and in case of fail - - * to floating point number. Note that function is assumed to be - * called on memory cell containing string, i.e. mem->type == MEM_Str. - * - * @param record Memory cell containing value to be converted. - * @retval 0 If value can be converted to integer or number. - * @retval -1 Otherwise. - */ -int -mem_apply_numeric_type(struct Mem *record); - -/** - * Convert @a mem to NUMBER type, so that after conversion it has - * one of types MEM_Real, MEM_Int or MEM_UInt. If conversion is - * not possible, function returns -1. - * - * Beware - this function changes value and type of @a mem - * argument. - */ -int -vdbe_mem_numerify(struct Mem *mem); - int sqlVdbeMemCast(struct Mem *, enum field_type type); int sqlVdbeMemStringify(struct Mem *); int sqlVdbeMemNulTerminate(struct Mem *); diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 008148687..de6455c50 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -2757,7 +2757,7 @@ case OP_SeekGT: { /* jump, in3 */ if (mem_is_null(pIn3)) goto skip_truncate; if (mem_is_str(pIn3)) - mem_apply_numeric_type(pIn3); + mem_to_number(pIn3); int64_t i; if (mem_is_uint(pIn3)) { i = pIn3->u.u;