[Tarantool-patches] [PATCH v5 40/52] sql: introduce mem_to_double()

imeevma at tarantool.org imeevma at tarantool.org
Fri Apr 9 23:26:00 MSK 2021


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



More information about the Tarantool-patches mailing list