[Tarantool-patches] [PATCH v4 43/53] sql: introduce mem_convert_to_number()

imeevma at tarantool.org imeevma at tarantool.org
Tue Mar 23 12:36:31 MSK 2021


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



More information about the Tarantool-patches mailing list