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 0623F6EC5E; Fri, 9 Apr 2021 23:53:43 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0623F6EC5E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618001623; bh=ZELlS7ojJ1cnJiiXPqjcmz1CwdeHU5NSms42n/8mYeM=; 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=uFsj6zn6+7WWquaHuXVHtpITCAxbJGtb7/adZcBxpSJWQOXGgHbUstrrWGx4AG6wJ AD9HvQTaYFYAvCBG+NF/BpMAodq1MueOhg3hrGMFSWqChn5jr3dDoYtzHN9g72keCh ZeSS7546zfDEEyPosTHfFnxcm++yOyyTNTF5wIzE= Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (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 27EDE6EC5E for ; Fri, 9 Apr 2021 23:53:40 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 27EDE6EC5E Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1lUy8J-0006lw-72; Fri, 09 Apr 2021 23:53:39 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 9 Apr 2021 23:53:38 +0300 Message-Id: <5b762310118a0e6c10c305098d5d53851a81a2e6.1618000037.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: 4F1203BC0FB41BD92FFCB8E6708E7480BE79914FF86F9151AC38CC435EA4A654182A05F5380850406D506D0C7EB3C5CF7488FD4059317BCC3B6328AAB712F90C54E0E5F6C91C14DD X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7F9D05773942AAE9CEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063727BBC20C3D5F36038638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2AECA83E1FE8B15E4A92859DFD20342A1CB629EEF1311BF91D2E47CDBA5A96583C09775C1D3CA48CF4964A708C60C975A117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE79AE9BAF3542BD4619FA2833FD35BB23DF004C906525384302BEBFE083D3B9BA73A03B725D353964B0B7D0EA88DDEDAC722CA9DD8327EE4930A3850AC1BE2E735E4A630A5B664A4FFC4224003CC83647689D4C264860C145E X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2AD77751E876CB595E8F7B195E1C9783113105DDC94723787B59FADFEE23B5201 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CD0035DD76F8A8A4FADC6E9C5E44C07DA5C9E2972194AEAB59C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D344A2E3AE0A8792E692A4C757C27D11FC06A8783FC3CC03FAA686F5EB50AACBCC90E32A7E5A013A0191D7E09C32AA3244C019EC2EFEA79E49ECDC1ED09749136A097FE24653F78E668FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojqcJA+pXcDulOmwaNa+DTYg== X-Mailru-Sender: 689FA8AB762F73936BC43F508A0638227DDB317910193D4E5C428E8425C5677E83D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [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: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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