[Tarantool-patches] [PATCH v5 41/52] sql: introduce mem_to_number()
imeevma at tarantool.org
imeevma at tarantool.org
Fri Apr 9 23:53:38 MSK 2021
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
More information about the Tarantool-patches
mailing list