Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH v5 47/52] sql: introduce mem_get_double()
Date: Thu, 15 Apr 2021 03:46:31 +0300	[thread overview]
Message-ID: <20210415004631.GA375388@tarantool.org> (raw)
In-Reply-To: <29e44fdf-521e-9df9-e7df-605ca25478a6@tarantool.org>

Thank you for the review! I reverted these and one more place in vdbe.c
Sorry, there won't be diff, only a new patch.

On Thu, Apr 15, 2021 at 02:17:41AM +0200, Vladislav Shpilevoy wrote:
> Thanks for the fixes!
> 
> > diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
> > index f7b6df0d9..1deb8e507 100644
> > --- a/src/box/sql/vdbe.c
> > +++ b/src/box/sql/vdbe.c
> > @@ -2791,7 +2789,7 @@ case OP_SeekGT: {       /* jump, in3 */
> >  			 *        (x >  4.9)    ->     (x >= 5)
> >  			 *        (x <= 4.9)    ->     (x <  5)
> >  			 */
> > -			if (pIn3->u.r<(double)iKey) {
> > +			if (mem_get_double_unsafe(pIn3) < (double)iKey) {
> 
> Why did you change this and below? There was a check above that
> pIn3 is double. So you can access u.r safely.
> 
I wanted to use mem_get_*() here, but you are right, it is not necessary here.
Fixed.

> >  				assert(OP_SeekGE==(OP_SeekGT-1));
> >  				assert(OP_SeekLT==(OP_SeekLE-1));
> >  				assert((OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001));
> > @@ -2801,7 +2799,7 @@ case OP_SeekGT: {       /* jump, in3 */
> >  			/* If the approximation iKey is smaller than the actual real search
> >  			 * term, substitute <= for < and > for >=.
> >  			 */
> > -			else if (pIn3->u.r>(double)iKey) {
> > +			else if (mem_get_double_unsafe(pIn3) > (double)iKey) {
> >  				assert(OP_SeekLE==(OP_SeekLT+1));
> >  				assert(OP_SeekGT==(OP_SeekGE+1));
> >  				assert((OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001));


New patch:


commit fd613a1c24938d79a68fd45e098660b4adc5ea11
Author: Mergen Imeev <imeevma@gmail.com>
Date:   Wed Mar 17 14:13:30 2021 +0300

    sql: introduce mem_get_double()
    
    This patch introduces mem_get_double(). This function is used to receive
    double value from MEM. If value of MEM is not double, it is converted to
    double if possible. MEM is not changed.
    
    Part of #5818

diff --git a/src/box/sql/date.c b/src/box/sql/date.c
index dffc23616..dbf460498 100644
--- a/src/box/sql/date.c
+++ b/src/box/sql/date.c
@@ -929,7 +929,7 @@ isDate(sql_context * context, int argc, sql_value ** argv, DateTime * p)
 	}
 	if ((eType = sql_value_type(argv[0])) == MP_DOUBLE
 	    || eType == MP_INT) {
-		setRawDateNumber(p, sql_value_double(argv[0]));
+		setRawDateNumber(p, mem_get_double_unsafe(argv[0]));
 	} else {
 		z = sql_value_text(argv[0]);
 		if (!z || parseDateOrTime(context, (char *)z, p)) {
diff --git a/src/box/sql/func.c b/src/box/sql/func.c
index 5503a9b16..dbf899c02 100644
--- a/src/box/sql/func.c
+++ b/src/box/sql/func.c
@@ -225,12 +225,11 @@ absFunc(sql_context * context, int argc, sql_value ** argv)
 		return;
 	}
 	default:{
-			/* Because sql_value_double() returns 0.0 if the argument is not
-			 * something that can be converted into a number, we have:
-			 * IMP: R-01992-00519 Abs(X) returns 0.0 if X is a string or blob
+			/*
+			 * Abs(X) returns 0.0 if X is a string or blob
 			 * that cannot be converted to a numeric value.
 			 */
-			double rVal = sql_value_double(argv[0]);
+			double rVal = mem_get_double_unsafe(argv[0]);
 			if (rVal < 0)
 				rVal = -rVal;
 			sql_result_double(context, rVal);
@@ -543,7 +542,7 @@ roundFunc(sql_context * context, int argc, sql_value ** argv)
 		context->is_aborted = true;
 		return;
 	}
-	r = sql_value_double(argv[0]);
+	r = mem_get_double_unsafe(argv[0]);
 	/* If Y==0 and X will fit in a 64-bit int,
 	 * handle the rounding directly,
 	 * otherwise use printf.
@@ -1049,7 +1048,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv)
 	case MP_DOUBLE:{
 			double r1, r2;
 			char zBuf[50];
-			r1 = sql_value_double(argv[0]);
+			r1 = mem_get_double_unsafe(argv[0]);
 			sql_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
 			sqlAtoF(zBuf, &r2, 20);
 			if (r1 != r2) {
@@ -1662,7 +1661,7 @@ sum_step(struct sql_context *context, int argc, sql_value **argv)
 			p->overflow = 1;
 		}
 	} else {
-		p->rSum += sql_value_double(argv[0]);
+		p->rSum += mem_get_double_unsafe(argv[0]);
 		p->approx = 1;
 	}
 }
diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 1197c2b68..00171ab55 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1110,6 +1110,29 @@ mem_get_uint(const struct Mem *mem, uint64_t *u)
 	return -1;
 }
 
+int
+mem_get_double(const struct Mem *mem, double *d)
+{
+	if ((mem->flags & MEM_Real) != 0) {
+		*d = mem->u.r;
+		return 0;
+	}
+	if ((mem->flags & MEM_Int) != 0) {
+		*d = (double)mem->u.i;
+		return 0;
+	}
+	if ((mem->flags & MEM_UInt) != 0) {
+		*d = (double)mem->u.u;
+		return 0;
+	}
+	if ((mem->flags & MEM_Str) != 0) {
+		if (sqlAtoF(mem->z, d, mem->n) == 0)
+			return -1;
+		return 0;
+	}
+	return -1;
+}
+
 int
 mem_copy(struct Mem *to, const struct Mem *from)
 {
@@ -2249,32 +2272,6 @@ mem_value_bool(const struct Mem *mem, bool *b)
 	return -1;
 }
 
-/*
- * Return the best representation of pMem that we can get into a
- * double.  If pMem is already a double or an integer, return its
- * value.  If it is a string or blob, try to convert it to a double.
- * If it is a NULL, return 0.0.
- */
-int
-sqlVdbeRealValue(Mem * pMem, double *v)
-{
-	assert(EIGHT_BYTE_ALIGNMENT(pMem));
-	if (pMem->flags & MEM_Real) {
-		*v = pMem->u.r;
-		return 0;
-	} else if (pMem->flags & MEM_Int) {
-		*v = (double)pMem->u.i;
-		return 0;
-	} else if ((pMem->flags & MEM_UInt) != 0) {
-		*v = (double)pMem->u.u;
-		return 0;
-	} else if (pMem->flags & MEM_Str) {
-		if (sqlAtoF(pMem->z, v, pMem->n))
-			return 0;
-	}
-	return -1;
-}
-
 /**************************** sql_value_  ******************************
  * The following routines extract information from a Mem or sql_value
  * structure.
@@ -2301,14 +2298,6 @@ sql_value_bytes(sql_value * pVal)
 	return sqlValueBytes(pVal);
 }
 
-double
-sql_value_double(sql_value * pVal)
-{
-	double v = 0.0;
-	sqlVdbeRealValue((Mem *) pVal, &v);
-	return v;
-}
-
 bool
 sql_value_boolean(sql_value *val)
 {
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index c2b337414..d283aa76a 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -790,6 +790,28 @@ mem_get_uint_unsafe(const struct Mem *mem)
 	return u;
 }
 
+/**
+ * Return value for MEM of DOUBLE type. For MEM of all other types convert
+ * value of the MEM to DOUBLE if possible and return converted value. Original
+ * MEM is not changed.
+ */
+int
+mem_get_double(const struct Mem *mem, double *d);
+
+/**
+ * Return value of MEM converted to double. This function is not safe since
+ * there is no proper processing in case mem_get_double() return an error. In
+ * this case this functions returns 0.
+ */
+static inline double
+mem_get_double_unsafe(const struct Mem *mem)
+{
+	double d;
+	if (mem_get_double(mem, &d) != 0)
+		return 0.;
+	return d;
+}
+
 /**
  * Simple type to str convertor. It is used to simplify
  * error reporting.
@@ -844,16 +866,12 @@ releaseMemArray(Mem * p, int N);
 
 int
 mem_value_bool(const struct Mem *mem, bool *b);
-int sqlVdbeRealValue(struct Mem *, double *);
 const void *
 sql_value_blob(struct Mem *);
 
 int
 sql_value_bytes(struct Mem *);
 
-double
-sql_value_double(struct Mem *);
-
 bool
 sql_value_boolean(struct Mem *val);
 
diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c
index eb8413f9c..2f1948ff8 100644
--- a/src/box/sql/printf.c
+++ b/src/box/sql/printf.c
@@ -152,7 +152,7 @@ getDoubleArg(PrintfArguments * p)
 {
 	if (p->nArg <= p->nUsed)
 		return 0.0;
-	return sql_value_double(p->apArg[p->nUsed++]);
+	return mem_get_double_unsafe(p->apArg[p->nUsed++]);
 }
 
 static char *
diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h
index 9b706b218..a51a5d2d6 100644
--- a/src/box/sql/sqlInt.h
+++ b/src/box/sql/sqlInt.h
@@ -442,9 +442,6 @@ sql_column_bytes(sql_stmt *, int iCol);
 int
 sql_column_bytes16(sql_stmt *, int iCol);
 
-double
-sql_column_double(sql_stmt *, int iCol);
-
 bool
 sql_column_boolean(struct sql_stmt *stmt, int column);
 
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 1f84f5693..2308587e7 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -2751,24 +2751,22 @@ case OP_SeekGT: {       /* jump, in3 */
 		if (mem_is_str(pIn3))
 			mem_to_number(pIn3);
 		int64_t i;
-		if (mem_is_uint(pIn3)) {
-			i = pIn3->u.u;
-			is_neg = false;
-		} else if (mem_is_nint(pIn3)) {
-			i = pIn3->u.i;
-			is_neg = true;
-		} else if (mem_is_double(pIn3)) {
-			if (pIn3->u.r > (double)INT64_MAX)
+		if (mem_get_int(pIn3, &i, &is_neg) != 0) {
+			if (!mem_is_double(pIn3)) {
+				diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
+					 mem_str(pIn3), "integer");
+				goto abort_due_to_error;
+			}
+			double d = pIn3->u.r;
+			assert(d >= (double)INT64_MAX || d < (double)INT64_MIN);
+			/* TODO: add [INT64_MAX, UINT64_MAX) here. */
+			if (d > (double)INT64_MAX)
 				i = INT64_MAX;
-			else if (pIn3->u.r < (double)INT64_MIN)
+			else if (d < (double)INT64_MIN)
 				i = INT64_MIN;
 			else
-				i = pIn3->u.r;
+				i = d;
 			is_neg = i < 0;
-		} else {
-			diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
-				 mem_str(pIn3), "integer");
-			goto abort_due_to_error;
 		}
 		iKey = i;
 
diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c
index 24a61d07c..bc62de431 100644
--- a/src/box/sql/vdbeapi.c
+++ b/src/box/sql/vdbeapi.c
@@ -464,12 +464,6 @@ sql_column_bytes(sql_stmt * pStmt, int i)
 	return sql_value_bytes(columnMem(pStmt, i));
 }
 
-double
-sql_column_double(sql_stmt * pStmt, int i)
-{
-	return sql_value_double(columnMem(pStmt, i));
-}
-
 bool
 sql_column_boolean(struct sql_stmt *stmt, int i)
 {

  reply	other threads:[~2021-04-15  0:46 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1618000036.git.imeevma@gmail.com>
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 41/52] sql: introduce mem_to_number() Mergen Imeev via Tarantool-patches
2021-04-13 23:25   ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 42/52] sql: introduce mem_to_str() and mem_to_str0() Mergen Imeev via Tarantool-patches
2021-04-13 22:58   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-13 23:41     ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 43/52] sql: introduce mem_cast_explicit() Mergen Imeev via Tarantool-patches
2021-04-13 22:59   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:01     ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 44/52] sql: introduce mem_cast_implicit() Mergen Imeev via Tarantool-patches
2021-04-13 22:59   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:05     ` Mergen Imeev via Tarantool-patches
2021-04-09 20:53 ` [Tarantool-patches] [PATCH v5 45/52] sql: introduce mem_get_int() Mergen Imeev via Tarantool-patches
2021-04-13 23:01   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:28     ` Mergen Imeev via Tarantool-patches
2021-04-14  1:17       ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 46/52] sql: introduce mem_get_uint() Mergen Imeev via Tarantool-patches
2021-04-13 23:04   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  0:39     ` Mergen Imeev via Tarantool-patches
2021-04-14  1:21       ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 47/52] sql: introduce mem_get_double() Mergen Imeev via Tarantool-patches
2021-04-13 23:04   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:00     ` Mergen Imeev via Tarantool-patches
2021-04-15  0:17       ` Vladislav Shpilevoy via Tarantool-patches
2021-04-15  0:46         ` Mergen Imeev via Tarantool-patches [this message]
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 48/52] sql: introduce mem_get_bool() Mergen Imeev via Tarantool-patches
2021-04-13 23:04   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:29     ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 49/52] sql: introduce mem_get_str0() and mem_as_str0() Mergen Imeev via Tarantool-patches
2021-04-13 23:06   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:43     ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 50/52] sql: introduce mem_get_bin() Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 51/52] sql: introduce mem_get_bytes_len() Mergen Imeev via Tarantool-patches
2021-04-13 23:06   ` Vladislav Shpilevoy via Tarantool-patches
2021-04-14  1:55     ` Mergen Imeev via Tarantool-patches
2021-04-15  0:21       ` Vladislav Shpilevoy via Tarantool-patches
2021-04-15  0:51         ` Mergen Imeev via Tarantool-patches
2021-04-09 21:08 ` [Tarantool-patches] [PATCH v5 52/52] sql: introduce mem_get_agg() Mergen Imeev via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210415004631.GA375388@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v5 47/52] sql: introduce mem_get_double()' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox