[Tarantool-patches] [PATCH v1 1/8] sql: refactor ABS() funcion

Mergen Imeev imeevma at tarantool.org
Wed Oct 20 19:52:16 MSK 2021


Hi! Thank you for the review! My answers, diff and new patch below.

On Fri, Oct 08, 2021 at 11:55:33PM +0200, Vladislav Shpilevoy wrote:
> Hi! Thanks for the patch!
> 
> On 01.10.2021 18:29, imeevma at tarantool.org wrote:
> > Part of #4145
> > ---
> >  src/box/sql/func.c | 83 +++++++++++++++++-----------------------------
> >  1 file changed, 30 insertions(+), 53 deletions(-)
> > 
> > diff --git a/src/box/sql/func.c b/src/box/sql/func.c
> > index b23636d7e..54b03f359 100644
> > --- a/src/box/sql/func.c
> > +++ b/src/box/sql/func.c
> > @@ -237,6 +237,32 @@ step_group_concat(struct sql_context *ctx, int argc, struct Mem *argv)
> >  		ctx->is_aborted = true;
> >  }
> >  
> > +/** Implementations of the ABS() function. */
> > +static void
> > +func_abs_int(struct sql_context *ctx, int argc, struct Mem *argv)
> > +{
> > +	assert(argc == 1);
> > +	(void)argc;
> > +	struct Mem *arg = &argv[0];
> > +	if (arg->type == MEM_TYPE_NULL)
> > +		return;
> 
> 1. Need mem_is_null(). The same for the other places if there
> are more.
> 
Fixed here in all other places in this patch-set.

> > +	assert(mem_is_int(arg));
> > +	uint64_t u = mem_is_uint(arg) ? arg->u.u : (uint64_t)-arg->u.i;
> 
> 2. You could make return when mem_is_uint(). It would remove '?' and
> mem_set_uint() which would calls mem_clear() inside.
> 
I am not sure that I understood correctly. In case of argument being uint we
can use mem_copy_as_ephemeral() instead of mem_set_uint(), but I am not sure
if it would be better.

> > +	mem_set_uint(ctx->pOut, u);


Diff:

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 5f90fb597..dbeb38bee 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -239,7 +239,7 @@ func_abs_int(struct sql_context *ctx, int argc, struct Mem *argv)
 	assert(argc == 1);
 	(void)argc;
 	struct Mem *arg = &argv[0];
-	if (arg->type == MEM_TYPE_NULL)
+	if (mem_is_null(arg))
 		return;
 	assert(mem_is_int(arg));
 	uint64_t u = mem_is_uint(arg) ? arg->u.u : (uint64_t)-arg->u.i;
@@ -252,9 +252,9 @@ func_abs_double(struct sql_context *ctx, int argc, struct Mem *argv)
 	assert(argc == 1);
 	(void)argc;
 	struct Mem *arg = &argv[0];
-	if (arg->type == MEM_TYPE_NULL)
+	if (mem_is_null(arg))
 		return;
-	assert(arg->type == MEM_TYPE_DOUBLE);
+	assert(mem_is_double(arg));
 	mem_set_double(ctx->pOut, arg->u.r < 0 ? -arg->u.r : arg->u.r);
 }
 

New patch:

commit 2cf5a2417954cb06bcc17cabf0636b6ba130cf8d
Author: Mergen Imeev <imeevma at gmail.com>
Date:   Tue Sep 21 14:39:57 2021 +0300

    sql: refactor ABS() funcion
    
    Part of #4145

diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 0cf17572b..dbeb38bee 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -232,6 +232,32 @@ step_group_concat(struct sql_context *ctx, int argc, struct Mem *argv)
 		ctx->is_aborted = true;
 }
 
+/** Implementations of the ABS() function. */
+static void
+func_abs_int(struct sql_context *ctx, int argc, struct Mem *argv)
+{
+	assert(argc == 1);
+	(void)argc;
+	struct Mem *arg = &argv[0];
+	if (mem_is_null(arg))
+		return;
+	assert(mem_is_int(arg));
+	uint64_t u = mem_is_uint(arg) ? arg->u.u : (uint64_t)-arg->u.i;
+	mem_set_uint(ctx->pOut, u);
+}
+
+static void
+func_abs_double(struct sql_context *ctx, int argc, struct Mem *argv)
+{
+	assert(argc == 1);
+	(void)argc;
+	struct Mem *arg = &argv[0];
+	if (mem_is_null(arg))
+		return;
+	assert(mem_is_double(arg));
+	mem_set_double(ctx->pOut, arg->u.r < 0 ? -arg->u.r : arg->u.r);
+}
+
 static const unsigned char *
 mem_as_ustr(struct Mem *mem)
 {
@@ -395,57 +421,6 @@ lengthFunc(struct sql_context *context, int argc, struct Mem *argv)
 	}
 }
 
-/*
- * Implementation of the abs() function.
- *
- * IMP: R-23979-26855 The abs(X) function returns the absolute value of
- * the numeric argument X.
- */
-static void
-absFunc(struct sql_context *context, int argc, struct Mem *argv)
-{
-	assert(argc == 1);
-	UNUSED_PARAMETER(argc);
-	switch (sql_value_type(&argv[0])) {
-	case MP_UINT: {
-		sql_result_uint(context, mem_get_uint_unsafe(&argv[0]));
-		break;
-	}
-	case MP_INT: {
-		int64_t value = mem_get_int_unsafe(&argv[0]);
-		assert(value < 0);
-		sql_result_uint(context, -value);
-		break;
-	}
-	case MP_NIL:{
-			/* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
-			sql_result_null(context);
-			break;
-		}
-	case MP_BOOL:
-	case MP_BIN:
-	case MP_EXT:
-	case MP_ARRAY:
-	case MP_MAP: {
-		diag_set(ClientError, ER_INCONSISTENT_TYPES, "number",
-			 mem_str(&argv[0]));
-		context->is_aborted = true;
-		return;
-	}
-	default:{
-			/*
-			 * Abs(X) returns 0.0 if X is a string or blob
-			 * that cannot be converted to a numeric value.
-			 */
-			double rVal = mem_get_double_unsafe(&argv[0]);
-			if (rVal < 0)
-				rVal = -rVal;
-			sql_result_double(context, rVal);
-			break;
-		}
-	}
-}
-
 /**
  * Implementation of the position() function.
  *
@@ -1930,8 +1905,10 @@ struct sql_func_definition {
  * function should be defined in succession.
  */
 static struct sql_func_definition definitions[] = {
-	{"ABS", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, absFunc, NULL},
-	{"ABS", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, absFunc, NULL},
+	{"ABS", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, func_abs_int,
+	 NULL},
+	{"ABS", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, func_abs_double,
+	 NULL},
 	{"AVG", 1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_INTEGER, step_avg, fin_avg},
 	{"AVG", 1, {FIELD_TYPE_DOUBLE}, FIELD_TYPE_DOUBLE, step_avg, fin_avg},
 	{"CHAR", -1, {FIELD_TYPE_INTEGER}, FIELD_TYPE_STRING, charFunc, NULL},


More information about the Tarantool-patches mailing list