From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id A88536EC5F; Thu, 15 Apr 2021 03:46:35 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A88536EC5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618447595; bh=PPLO9zY9NalPH/UuJ+B6JGSmp3aKExcMa8U5qHcgyc8=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=R4SVpLQ/cFQ2E8JT5/hj/r1mIwGwo3h+lqQPnVbdTeanPEyLupOVa8T1lZdnMf5k4 3mFwGjLO1KZalXFBhA13fR/q1EeB4pQqQNfpno55bLLZJ8uIkXdlCSap2DgXuKxkz9 M/5sy8QVziW1kTw9ABWGCxygavU16aGlKYY51lig= Received: from smtp59.i.mail.ru (smtp59.i.mail.ru [217.69.128.39]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id BB60F6EC5F for ; Thu, 15 Apr 2021 03:46:33 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org BB60F6EC5F Received: by smtp59.i.mail.ru with esmtpa (envelope-from ) id 1lWq9R-0006wg-1W; Thu, 15 Apr 2021 03:46:33 +0300 Date: Thu, 15 Apr 2021 03:46:31 +0300 To: Vladislav Shpilevoy Cc: tarantool-patches@dev.tarantool.org Message-ID: <20210415004631.GA375388@tarantool.org> References: <3bba79b9-7bd4-541d-7287-f8e844e8c6c5@tarantool.org> <20210414010015.GA145710@tarantool.org> <29e44fdf-521e-9df9-e7df-605ca25478a6@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <29e44fdf-521e-9df9-e7df-605ca25478a6@tarantool.org> X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E74809299FB6B3996B874F289A033C44AD400182A05F5380850409AD7B7361A5B40C286A8F4E7F1AE2E0DBB3C6CA6C2C8935237F40AAE4447E2C6 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE76042E2DB3E33BF2BEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637E1AAA91777E61F958638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B2650F8CCA6B99271AFA7E61BD44121D80262F3407DF4BB9EED2E47CDBA5A96583C09775C1D3CA48CF17B107DEF921CE79117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE767883B903EA3BAEA9FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE7FA3D786573799A4ED32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C0431528A915EB7FE6CD04E86FAF290E2D7E9C4E3C761E06A71DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3727597FF642BA4D735872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2368A440D3B0F6089093C9A16E5BC824A2A04A2ABAA09D25379311020FFC8D4AD48C8163CD7E2ABE7D1DEE7FD901B04EB X-C1DE0DAB: 0D63561A33F958A560DB37D3C57C6ACB5027D2DE37801A84EEBD5E662E7C7919D59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346840168BCAD8054E680C34192AD7B52FF1788A7E690D930BA9EC05831EC24A61B4E24D17125707CF1D7E09C32AA3244C67C95E954F5060262E4865CE98E22367E646F07CC2D4F3D8729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojrcJA+pXcDunt10WWJKyFnQ== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED39E2086B3EC3CA6986A8F4E7F1AE2E0DEFC4EB6826DC22485105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH v5 47/52] sql: introduce mem_get_double() X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: Mergen Imeev Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 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) {