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 DDFCF6B467; Tue, 23 Mar 2021 12:59:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DDFCF6B467 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616493559; bh=4jij3+TOXnM2nT6G/jNCsvlV8vYPczYGEEj5rfHX5M4=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=QxUAPiiNWTLgIk4gU2bcOrazQ8QzmAM5G0ONbrOixuA6o1CWbWhFyXNzCBuBmyowo OWUFXvYk/IzvhaMSW8AaZ/5IeworC8ZyIjEdrsB8B6vZQigGZ+SWHNSfemfL5naxCG eK1909GbRQtek3T5Z9b1SibvQc3x1LjC/jqDPOro= Received: from smtp54.i.mail.ru (smtp54.i.mail.ru [217.69.128.34]) (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 67FC468F4B for ; Tue, 23 Mar 2021 12:36:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 67FC468F4B Received: by smtp54.i.mail.ru with esmtpa (envelope-from ) id 1lOdSv-0007tp-LB; Tue, 23 Mar 2021 12:36:46 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:36:45 +0300 Message-Id: <68e134919c05da0cb154d282ebd5ec9375d883e3.1616491731.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F10D3016C09B407F8B88411E9FEB481E8E182A05F538085040CB5A5DDC2AA356A5781CD4D24B32BEE8A51D0BECB7BEC322763996496E28D48F X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7C27E92EFAD44F80DEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637328FC23D015AFE6E8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75E703FC209F337E4C44750647EDA698094A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE7328B01A8D746D8839FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3A12191B5F2BB8629117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF856009BA58C5E1BBBA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B622657CE15C1A97F576E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7CD1D040B6C1ECEA3F93EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B6A1DCCEB63E2F10FB089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E70285AA09D4B315C2C6BD9CCCA9EDD067B1EDA766A37F9254B7 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75E703FC209F337E4C44750647EDA6980949C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D346E67BC984B8ABCB5752EDDB660CDFB45C5384B52BDD51D6A7904298CECB91BA2AEE55851C5C9D7831D7E09C32AA3244C1CC30ADF9A6DA562921100DB82487AA2795D98D676DD64D0FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UhVBrwkyBniVA== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED3D824BB8E3E9DC64781CD4D24B32BEE884876E3DC5E244DF5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 49/53] 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: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" This patch introduces mem_get_double() which is used to receive double value from MEM. Part of #5818 --- src/box/sql/func.c | 29 +++++++++++++--------- src/box/sql/mem.c | 57 +++++++++++++++++-------------------------- src/box/sql/mem.h | 7 +++--- src/box/sql/printf.c | 4 ++- src/box/sql/sqlInt.h | 3 --- src/box/sql/vdbeapi.c | 6 ----- 6 files changed, 47 insertions(+), 59 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index a851d98f2..15fb2f14f 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -124,9 +124,12 @@ port_vdbemem_dump_lua(struct port *base, struct lua_State *L, bool is_flat) luaL_pushuint64(L, u); break; } - case MP_DOUBLE: - lua_pushnumber(L, sql_value_double(param)); + case MP_DOUBLE: { + double d; + mem_get_double(param, &d); + lua_pushnumber(L, d); break; + } case MP_STR: lua_pushstring(L, (const char *) sql_value_text(param)); break; @@ -180,8 +183,9 @@ port_vdbemem_get_msgpack(struct port *base, uint32_t *size) break; } case MP_DOUBLE: { - mpstream_encode_double(&stream, - sql_value_double(param)); + double d; + mem_get_double(param, &d); + mpstream_encode_double(&stream, d); break; } case MP_STR: { @@ -543,12 +547,13 @@ 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; + if (mem_get_double(argv[0], &rVal) != 0) + rVal = 0; if (rVal < 0) rVal = -rVal; sql_result_double(context, rVal); @@ -866,7 +871,7 @@ roundFunc(sql_context * context, int argc, sql_value ** argv) context->is_aborted = true; return; } - r = sql_value_double(argv[0]); + mem_get_double(argv[0], &r); /* If Y==0 and X will fit in a 64-bit int, * handle the rounding directly, * otherwise use printf. @@ -1373,7 +1378,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]); + mem_get_double(argv[0], &r1); sql_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1); sqlAtoF(zBuf, &r2, 20); if (r1 != r2) { @@ -1994,7 +1999,9 @@ sum_step(struct sql_context *context, int argc, sql_value **argv) p->overflow = 1; } } else { - p->rSum += sql_value_double(argv[0]); + double d; + mem_get_double(argv[0], &d); + p->rSum += d; p->approx = 1; } } diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 6ab6d77ad..8d4f0f92c 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1286,6 +1286,29 @@ mem_get_unsigned(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) { @@ -2454,32 +2477,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. @@ -2506,14 +2503,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 ebc9c6c20..f9d229baf 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -351,6 +351,9 @@ mem_get_integer(const struct Mem *mem, int64_t *i, bool *is_neg); int mem_get_unsigned(const struct Mem *mem, uint64_t *u); +int +mem_get_double(const struct Mem *mem, double *d); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -429,16 +432,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 d9140b484..0790a52a7 100644 --- a/src/box/sql/printf.c +++ b/src/box/sql/printf.c @@ -155,7 +155,9 @@ getDoubleArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0.0; - return sql_value_double(p->apArg[p->nUsed++]); + double d; + mem_get_double(p->apArg[p->nUsed++], &d); + return d; } static char * diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 7f9539aea..3e2cc4a66 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/vdbeapi.c b/src/box/sql/vdbeapi.c index b6db98031..9a97b2d77 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -485,12 +485,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) { -- 2.25.1