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 8D2136EC63; Fri, 9 Apr 2021 23:27:54 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8D2136EC63 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618000074; bh=fCEQM9EUjPImCx7r7LazXCcVCkzlDO7GHh5fGvT6W8Y=; 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=KZ62GAxrnFWIMZDmNKDM2KztpiL0DX8wloC9nEKwUtoiLcIOaRsNlUknBlIYlbB0H ZusY7ryns7hMPAByhiXcq2Uhilbfg2SuX4Zec0W1Uf1/D0Gq+9dc/ngYOxhR0vgeU0 W1B/xkTDM5s0b17m+7vdpx9Nr3M0eN9LAGsRQOBE= Received: from smtpng2.m.smailru.net (smtpng2.m.smailru.net [94.100.179.3]) (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 648866EC5E for ; Fri, 9 Apr 2021 23:26:01 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 648866EC5E Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1lUxhY-0005Ae-Li; Fri, 09 Apr 2021 23:26:01 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 9 Apr 2021 23:26:00 +0300 Message-Id: <5cb7ed681028bcd7def0d2a66b5a2f9530fd1d98.1617984948.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480B1C8842CE613979723F2FB4628545A35182A05F538085040DB414FF668F12CC91BF1CF3D777DABBF37DF60EDC0266CBC15A597A0E4E27DD9 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE761966F250AC1AE21EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006379A6B93796C91DCB58638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2794FDCA2E566586F80545757785F36422AAFC1FEE5B068F7D2E47CDBA5A96583C09775C1D3CA48CF90D92131081DE748117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE7E688A9D963DC14319FA2833FD35BB23DF004C906525384302BEBFE083D3B9BA73A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E735C6EABA9B74D0DA47B5C8C57E37DE458BEDA766A37F9254B7 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2AD77751E876CB595E8F7B195E1C97831C18EEF89901168EC9FDA98CB0575C3EA X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CD0035DD76F8A8A4F64BE2B1031FA9DE6200A617F206E1D449C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34A1AF43396E40A36F23B26517EF0F3C3FF4F85DBEED77CDEC204ACFEEFA629AFA802CB50B6122E8471D7E09C32AA3244C5A411D0E72A922EF1188C7105EB5645E39C99C45E8D137E9FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojyO2lHpuZu4T5/LavOUTkCw== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822C2D86735C834E5EC19656C77EEFC5C5A83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [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: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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