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 453E268F42; Tue, 23 Mar 2021 12:56:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 453E268F42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616493378; bh=a5cq9ypJULFJJ2MAwOSt7hXkWR/Y4vh9K0ImSTLDJHM=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=JwkrjpVtCWqHPnoTcYhWWb4uu/Bs3FEBvs9QwJvVO1+kKAfomi778t24QQbf/0UuF 7HS1RFGu2HRZrXdGauW7GFAjX4jlFAX4PrYgnBaPfS5ncf6C4tkNmOJxL52mOJ5uFk NKQBuUDyB5E6GNtDv1T1gziz4dsa15izph0vOLyI= Received: from smtp38.i.mail.ru (smtp38.i.mail.ru [94.100.177.98]) (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 5FB0568F60 for ; Tue, 23 Mar 2021 12:36:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5FB0568F60 Received: by smtp38.i.mail.ru with esmtpa (envelope-from ) id 1lOdSh-0007Iz-Jn; Tue, 23 Mar 2021 12:36:32 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:36:31 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F179C48A9DDACBFB6F5347129BC2C9341C182A05F538085040B1CBF09BF28A7ECA47E6209C39596AE28B6B28EB1C5AA905C36A277D7B7F185D X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE711269A7C2F827F16EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006378D64EDD178B2A1008638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75E0243514B4E242E52B8FE119F8C36996FA471835C12D1D9774AD6D5ED66289B5278DA827A17800CE74A95F4E53E8DCE969FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3BEC81E4AEBD6D2BF117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF4B26726BD97DF1CFBA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B66CE449B29A2285B576E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7CD1D040B6C1ECEA3F93EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B6A1DCCEB63E2F10FB089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E70285AA09D4B315C2C6BD9CCCA9EDD067B1EDA766A37F9254B7 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24A6D60772A99906F8E1CD14B953EB46D656FDE122CAD6EB9355D89D7DBCDD132 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75E0243514B4E242E52B8FE119F8C36996F9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34753B45383CEFF204879F9DE8BECD945161A54B3D69C5C6E07009ECA812797A6FD90255F4CC5EC6C91D7E09C32AA3244C95465F1FAC1EE6E439D1256A1319494D4DBEAD0ED6C55A80FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UgPBMDWOPQBIg== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED2EA47AC88F4896D947E6209C39596AE25F9045B74F665CC85105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 43/53] sql: introduce mem_convert_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: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch introduces mem_convert_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 | 58 +++++++++++++--------------------------------- src/box/sql/mem.h | 28 +++------------------- src/box/sql/vdbe.c | 2 +- 4 files changed, 21 insertions(+), 69 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 1b3929259..b644c39d8 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -1948,7 +1948,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_convert_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 8f2c48cd5..8600b5c41 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -803,6 +803,21 @@ mem_convert_to_double(struct Mem *mem) return -1; } +int +mem_convert_to_number(struct Mem *mem) +{ + if ((mem->flags & (MEM_Int | MEM_UInt | MEM_Real)) != 0) + return 0; + if ((mem->flags & MEM_Bool) != 0) + return mem_convert_boolean_to_integer(mem); + if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) { + if (mem_convert_varstring_to_integer(mem) == 0) + return 0; + return mem_convert_varstring_to_double(mem); + } + return -1; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -1860,47 +1875,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_integer(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_set_integer(mem, (int64_t)mem->u.b, false); - 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_integer(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 @@ -1977,7 +1951,7 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) case FIELD_TYPE_DOUBLE: return mem_convert_to_double(pMem); case FIELD_TYPE_NUMBER: - return vdbe_mem_numerify(pMem); + return mem_convert_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 be793d08a..d5f0d7891 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -327,6 +327,9 @@ mem_convert_to_integer_lossless(struct Mem *mem); int mem_convert_to_double(struct Mem *mem); +int +mem_convert_to_number(struct Mem *mem); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -377,31 +380,6 @@ registerTrace(int iReg, Mem *p); # define memAboutToChange(P,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 3c02eae74..6799cc9aa 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -2621,7 +2621,7 @@ case OP_SeekGT: { /* jump, in3 */ if (mem_is_null(pIn3)) goto skip_truncate; if (mem_is_string(pIn3)) - mem_apply_numeric_type(pIn3); + mem_convert_to_number(pIn3); int64_t i; if (mem_is_integer(pIn3) && !mem_is_unsigned(pIn3)) { i = pIn3->u.i; -- 2.25.1