[Tarantool-patches] [PATCH v5 40/52] sql: introduce mem_to_double()
Mergen Imeev
imeevma at tarantool.org
Wed Apr 14 02:21:28 MSK 2021
Grouped <type>_to_<type>() functions and added assert to make sure that
mem_to_double() accepts only values of simple types. Diff and new patch below.
On Fri, Apr 09, 2021 at 11:26:00PM +0300, Mergen Imeev via Tarantool-patches wrote:
> 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
>
Diff:
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 9972b1f85..3685bf6b9 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -492,6 +492,20 @@ mem_set_null_clear(struct Mem *mem)
mem->flags = MEM_Null | MEM_Cleared;
}
+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_int(struct Mem *mem)
{
@@ -503,6 +517,16 @@ bytes_to_int(struct Mem *mem)
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;
+}
+
static inline int
double_to_int(struct Mem *mem)
{
@@ -578,33 +602,10 @@ 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)
{
+ assert((mem->flags & MEM_PURE_TYPE_MASK) != 0);
if ((mem->flags & MEM_Real) != 0)
return 0;
if ((mem->flags & (MEM_Int | MEM_UInt)) != 0)
New patch:
commit f7e8e09f0160dd0cf933e4bfc29d108603291b79
Author: Mergen Imeev <imeevma at gmail.com>
Date: Wed Mar 17 11:03:22 2021 +0300
sql: introduce mem_to_double()
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
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index b22b4ba21..3685bf6b9 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -492,6 +492,20 @@ mem_set_null_clear(struct Mem *mem)
mem->flags = MEM_Null | MEM_Cleared;
}
+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_int(struct Mem *mem)
{
@@ -503,6 +517,16 @@ bytes_to_int(struct Mem *mem)
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;
+}
+
static inline int
double_to_int(struct Mem *mem)
{
@@ -578,6 +602,19 @@ mem_to_int_precise(struct Mem *mem)
return -1;
}
+int
+mem_to_double(struct Mem *mem)
+{
+ assert((mem->flags & MEM_PURE_TYPE_MASK) != 0);
+ 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)
{
@@ -1633,21 +1670,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)
{
@@ -1751,7 +1773,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:
@@ -1957,11 +1979,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
@@ -2005,28 +2027,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.
*
@@ -2057,7 +2057,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 5aabcc1fd..485a6fed0 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -701,6 +701,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.
@@ -747,7 +754,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 c908f13c0..008148687 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -1457,7 +1457,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;
}
More information about the Tarantool-patches
mailing list