Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org, tsafin@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v5 41/52] sql: introduce mem_to_number()
Date: Fri,  9 Apr 2021 23:53:38 +0300	[thread overview]
Message-ID: <5b762310118a0e6c10c305098d5d53851a81a2e6.1618000037.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1618000036.git.imeevma@gmail.com>

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


       reply	other threads:[~2021-04-09 20:53 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1618000036.git.imeevma@gmail.com>
2021-04-09 20:53 ` Mergen Imeev via Tarantool-patches [this message]
2021-04-13 23:25   ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 42/52] sql: introduce mem_to_str() and mem_to_str0() Mergen Imeev via Tarantool-patches
2021-04-13 22:58   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-13 23:41     ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 43/52] sql: introduce mem_cast_explicit() Mergen Imeev via Tarantool-patches
2021-04-13 22:59   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:01     ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 44/52] sql: introduce mem_cast_implicit() Mergen Imeev via Tarantool-patches
2021-04-13 22:59   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:05     ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 45/52] sql: introduce mem_get_int() Mergen Imeev via Tarantool-patches
2021-04-13 23:01   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:28     ` Mergen Imeev via Tarantool-patches
2021-04-14  1:17       ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 46/52] sql: introduce mem_get_uint() Mergen Imeev via Tarantool-patches
2021-04-13 23:04   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:39     ` Mergen Imeev via Tarantool-patches
2021-04-14  1:21       ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 47/52] sql: introduce mem_get_double() Mergen Imeev via Tarantool-patches
2021-04-13 23:04   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:00     ` Mergen Imeev via Tarantool-patches
2021-04-15  0:17       ` Vladislav Shpilevoy via Tarantool-patches
2021-04-15  0:46         ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 48/52] sql: introduce mem_get_bool() Mergen Imeev via Tarantool-patches
2021-04-13 23:04   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:29     ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 49/52] sql: introduce mem_get_str0() and mem_as_str0() Mergen Imeev via Tarantool-patches
2021-04-13 23:06   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:43     ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 50/52] sql: introduce mem_get_bin() Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 51/52] sql: introduce mem_get_bytes_len() Mergen Imeev via Tarantool-patches
2021-04-13 23:06   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:55     ` Mergen Imeev via Tarantool-patches
2021-04-15  0:21       ` Vladislav Shpilevoy via Tarantool-patches
2021-04-15  0:51         ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 52/52] sql: introduce mem_get_agg() Mergen Imeev via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5b762310118a0e6c10c305098d5d53851a81a2e6.1618000037.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=tsafin@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v5 41/52] sql: introduce mem_to_number()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox