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 D5D8D68F47; Tue, 23 Mar 2021 12:55:48 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D5D8D68F47 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616493348; bh=JAWxlWyP69jg41KAC9oeL7QN7w+RTyz5mt6XGNo1jUw=; 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=uSlBo8A1WBy+RQcQIrcwCG77lM1pernwMsOcc6duHm168ge+OrYc5mue9rMCFH6uG PX8AzmIbuuPUPRhb9UkUY7FI6MKTaWDgDVCxGRE3mdmUNwL/ZdASyKyuOgugoJMInU evjv+wS/X7ODjwmnlLyWzYQLeC+qFogE67MDuoko= Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [94.100.177.104]) (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 3D9CC68F5F for ; Tue, 23 Mar 2021 12:36:30 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3D9CC68F5F Received: by smtp44.i.mail.ru with esmtpa (envelope-from ) id 1lOdSf-0000Fa-DL; Tue, 23 Mar 2021 12:36:29 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:36:29 +0300 Message-Id: <5aa0914759a562a49744f49f88b8a099bba94d5f.1616491731.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: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F10D3016C09B407F8B88411E9FEB481E8E182A05F5380850408997EE62F708948313655126BDBD1D64B3FD2BC4F359F07465A8C6A459EFCA06 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE737BB76880A4CA9A4EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637578F58D66D7052C48638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75EEE277A723D6F48FEBF42F155AF44E0D1A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE78A0F7C24A37A3D769FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C311471C67CF6D96D5117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF894DE5C3FF01FF54BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B622657CE15C1A97F576E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7CD1D040B6C1ECEA3F93EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B6A1DCCEB63E2F10FB089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E70227F01F88B6EF2528BD9CCCA9EDD067B1EDA766A37F9254B7 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75EEE277A723D6F48FEBF42F155AF44E0D19C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3451BBE684D17D72210327A0BB57CD5ECA4B4D30AD49CA62BD54D2FF426644C06C5642403F7CF7906F1D7E09C32AA3244C61BA48523023E90CC08BB1D3BC02A91C24AF4FAF06DA24FDFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UhsYaf120t4rA== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED6D4E2D11AA4E744813655126BDBD1D64825871508D9F39705105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 42/53] sql: introduce mem_convert_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_convert_to_double(). Function mem_convert_to_double() is used to convert MEM to MEM contains double values. Part of #5818 --- src/box/sql/mem.c | 77 ++++++++++++++++++++++------------------------ src/box/sql/mem.h | 4 ++- src/box/sql/vdbe.c | 2 +- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index b9bcd3d3a..8f2c48cd5 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -769,6 +769,40 @@ mem_convert_to_integer_lossless(struct Mem *mem) return -1; } +static inline int +mem_convert_integer_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_set_double(mem, d); + return 0; +} + +static inline int +mem_convert_varstring_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_convert_to_double(struct Mem *mem) +{ + if ((mem->flags & MEM_Real) != 0) + return 0; + if ((mem->flags & (MEM_Int | MEM_UInt)) != 0) + return mem_convert_integer_to_double(mem); + if ((mem->flags & MEM_Str) != 0) + return mem_convert_varstring_to_double(mem); + return -1; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -1844,21 +1878,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) { @@ -1956,7 +1975,7 @@ sqlVdbeMemCast(Mem * pMem, enum field_type type) return -1; return 0; case FIELD_TYPE_DOUBLE: - return sqlVdbeMemRealify(pMem); + return mem_convert_to_double(pMem); case FIELD_TYPE_NUMBER: return vdbe_mem_numerify(pMem); case FIELD_TYPE_VARBINARY: @@ -2157,11 +2176,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_convert_to_double(record); case FIELD_TYPE_DOUBLE: if ((record->flags & MEM_Real) != 0) return 0; - return sqlVdbeMemRealify(record); + return mem_convert_to_double(record); case FIELD_TYPE_STRING: /* * Only attempt the conversion to TEXT if there is @@ -2205,28 +2224,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. * diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 58cbdfe9f..be793d08a 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -324,6 +324,9 @@ mem_convert_to_integer(struct Mem *mem); int mem_convert_to_integer_lossless(struct Mem *mem); +int +mem_convert_to_double(struct Mem *mem); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -387,7 +390,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 1aeed543f..3c02eae74 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -1365,7 +1365,7 @@ case OP_MustBeInt: { /* jump, in1 */ case OP_Realify: { /* in1 */ pIn1 = &aMem[pOp->p1]; if (mem_is_integer(pIn1)) { - sqlVdbeMemRealify(pIn1); + mem_convert_to_double(pIn1); } break; } -- 2.25.1