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 8F7B068F4D; Tue, 23 Mar 2021 12:58:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8F7B068F4D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616493499; bh=V4ODIOpu10S7Cc8yPrPn1CLdaM6xIp8kknyk1GykdYM=; 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=dROy4IjH9UHKiC9Xqmzj8SPPUv58DJufw+bSD8zWPvW060WeLQES9KiCoCWWGSjF7 EC9kfdLvUI8NRRAKVoDrtsHL0SX19O9ffrE8S5kXHuwTtsZU+e5pC3QCYGs98YPSZM 6QQ7aPPo4kPvgHDeYX2spchRoTQRhFVPVsvwYDPY= Received: from smtp61.i.mail.ru (smtp61.i.mail.ru [217.69.128.41]) (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 81A0468F64 for ; Tue, 23 Mar 2021 12:36:42 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 81A0468F64 Received: by smtp61.i.mail.ru with esmtpa (envelope-from ) id 1lOdSr-0005P8-On; Tue, 23 Mar 2021 12:36:42 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:36:41 +0300 Message-Id: <5c19097b726f62d9ba6027decf6f7412fa3e1972.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: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD95D6E7CC48CB1F5F145AC670795810553045AFCF1215FE694182A05F53808504034A46E1249239CEDFF6421ECE1B308E58B6B28EB1C5AA905D34D7C474FE66999 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7E628FE8A185FCFBEEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006371B0187663A04449C8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75E5676289E9554016202E5E406B88B6DB8A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE71AE4D56B06699BBC9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3E478A468B35FE767117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CFEB76C46DA57105D6BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B66CE449B29A2285B576E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7CD1D040B6C1ECEA3F93EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B6A1DCCEB63E2F10FB089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E70285AA09D4B315C2C6BD9CCCA9EDD067B1EDA766A37F9254B7 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75E5676289E9554016202E5E406B88B6DB89C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3488E5CD2E691F5566F47F899F78F54908072B211CF366C3BABFAB70899A4045970500AD0C114120D01D7E09C32AA3244CEE6E827D4A2D29D94C6699730DDE93B14DBEAD0ED6C55A80FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6Uj0lPlBf3C5ig== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED605D1EFAEC5CEDF4FF6421ECE1B308E55F9045B74F665CC85105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 47/53] sql: introduce mem_get_integer() 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_integer() which is used to receive integer value from MEM. Part of #5818 --- src/box/sql/func.c | 55 ++++++++++++------- src/box/sql/mem.c | 123 +++++++++++++----------------------------- src/box/sql/mem.h | 10 ++-- src/box/sql/printf.c | 5 +- src/box/sql/sqlInt.h | 6 --- src/box/sql/vdbeapi.c | 12 ----- 6 files changed, 80 insertions(+), 131 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index b644c39d8..0fa0f6ac7 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -111,9 +111,13 @@ port_vdbemem_dump_lua(struct port *base, struct lua_State *L, bool is_flat) sql_value *param = (sql_value *)((struct Mem *)port->mem + i); switch (sql_value_type(param)) { - case MP_INT: - luaL_pushint64(L, sql_value_int64(param)); + case MP_INT: { + bool unused; + int64_t n; + mem_get_integer(param, &n, &unused); + luaL_pushint64(L, n); break; + } case MP_UINT: luaL_pushuint64(L, sql_value_uint64(param)); break; @@ -157,7 +161,9 @@ port_vdbemem_get_msgpack(struct port *base, uint32_t *size) (sql_value *)((struct Mem *)port->mem + i); switch (sql_value_type(param)) { case MP_INT: { - sql_int64 val = sql_value_int64(param); + bool unused; + int64_t val; + mem_get_integer(param, &val, &unused); if (val < 0) { mpstream_encode_int(&stream, val); break; @@ -509,7 +515,9 @@ absFunc(sql_context * context, int argc, sql_value ** argv) break; } case MP_INT: { - int64_t value = sql_value_int64(argv[0]); + bool unused; + int64_t value; + mem_get_integer(argv[0], &value, &unused); assert(value < 0); sql_result_uint(context, -value); break; @@ -725,7 +733,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) const unsigned char *z2; int len; int p0type; - i64 p1, p2; + int64_t p1, p2; int negP2 = 0; if (argc != 2 && argc != 3) { @@ -740,7 +748,8 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) return; } p0type = sql_value_type(argv[0]); - p1 = sql_value_int(argv[1]); + bool unused; + mem_get_integer(argv[1], &p1, &unused); if (p0type == MP_BIN) { len = sql_value_bytes(argv[0]); z = sql_value_blob(argv[0]); @@ -756,7 +765,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) len = sql_utf8_char_count(z, sql_value_bytes(argv[0])); } if (argc == 3) { - p2 = sql_value_int(argv[2]); + mem_get_integer(argv[2], &p2, &unused); if (p2 < 0) { p2 = -p2; negP2 = 1; @@ -827,7 +836,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) static void roundFunc(sql_context * context, int argc, sql_value ** argv) { - int n = 0; + int64_t n = 0; double r; if (argc != 1 && argc != 2) { diag_set(ClientError, ER_FUNC_WRONG_ARG_COUNT, "ROUND", @@ -838,7 +847,8 @@ roundFunc(sql_context * context, int argc, sql_value ** argv) if (argc == 2) { if (mem_is_null(argv[1])) return; - n = sql_value_int(argv[1]); + bool unused; + mem_get_integer(argv[1], &n, &unused); if (n < 0) n = 0; } @@ -981,7 +991,7 @@ randomFunc(sql_context * context, int NotUsed, sql_value ** NotUsed2) static void randomBlob(sql_context * context, int argc, sql_value ** argv) { - int n; + int64_t n; unsigned char *p; assert(argc == 1); UNUSED_PARAMETER(argc); @@ -991,7 +1001,8 @@ randomBlob(sql_context * context, int argc, sql_value ** argv) context->is_aborted = true; return; } - n = sql_value_int(argv[0]); + bool unused; + mem_get_integer(argv[0], &n, &unused); if (n < 1) return; p = contextMalloc(context, n); @@ -1532,10 +1543,11 @@ hexFunc(sql_context * context, int argc, sql_value ** argv) static void zeroblobFunc(sql_context * context, int argc, sql_value ** argv) { - i64 n; + int64_t n; assert(argc == 1); UNUSED_PARAMETER(argc); - n = sql_value_int64(argv[0]); + bool unused; + mem_get_integer(argv[0], &n, &unused); if (n < 0) n = 0; if (sql_result_zeroblob64(context, n) != 0) { @@ -1779,9 +1791,11 @@ trim_func_two_args(struct sql_context *context, sql_value *arg1, int input_str_sz = sql_value_bytes(arg2); if (sql_value_type(arg1) == MP_INT || sql_value_type(arg1) == MP_UINT) { uint8_t len_one = 1; - trim_procedure(context, sql_value_int(arg1), - (const unsigned char *) " ", &len_one, 1, - input_str, input_str_sz); + bool unused; + int64_t n; + mem_get_integer(arg1, &n, &unused); + trim_procedure(context, n, (const unsigned char *) " ", + &len_one, 1, input_str, input_str_sz); } else if ((trim_set = sql_value_text(arg1)) != NULL) { int trim_set_sz = sql_value_bytes(arg1); uint8_t *char_len; @@ -1819,7 +1833,10 @@ trim_func_three_args(struct sql_context *context, sql_value *arg1, &char_len); if (char_cnt == -1) return; - trim_procedure(context, sql_value_int(arg1), trim_set, char_len, + bool unused; + int64_t n; + mem_get_integer(arg1, &n, &unused); + trim_procedure(context, n, trim_set, char_len, char_cnt, input_str, input_str_sz); sql_free(char_len); } @@ -1958,7 +1975,9 @@ sum_step(struct sql_context *context, int argc, sql_value **argv) } p->cnt++; if (type == MP_INT || type == MP_UINT) { - int64_t v = sql_value_int64(argv[0]); + bool unused; + int64_t v; + mem_get_integer(argv[0], &v, &unused); if (type == MP_INT) p->rSum += v; else diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 1baf4c482..540931723 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1227,6 +1227,38 @@ mem_implicit_cast_old(struct Mem *mem, enum field_type type) return -1; } +int +mem_get_integer(const struct Mem *mem, int64_t *i, bool *is_neg) +{ + if ((mem->flags & MEM_Int) != 0) { + *i = mem->u.i; + *is_neg = true; + return 0; + } + if ((mem->flags & MEM_UInt) != 0) { + *i = mem->u.i; + *is_neg = false; + return 0; + } + if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) + return sql_atoi64(mem->z, i, is_neg, mem->n); + if ((mem->flags & MEM_Real) != 0) { + double d = mem->u.r; + if (d < 0 && d >= (double)INT64_MIN) { + *i = (int64_t)d; + *is_neg = true; + return 0; + } + if (d >= 0 && d < (double)UINT64_MAX) { + *i = (int64_t)(uint64_t)d; + *is_neg = false; + return 0; + } + return -1; + } + return -1; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -1536,12 +1568,12 @@ mem_bitwise(struct Mem *left, struct Mem *right, struct Mem *result, int op) int64_t r; int64_t res; bool unused; - if (sqlVdbeIntValue(left, &l, &unused) != 0) { + if (mem_get_integer(left, &l, &unused) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left), "integer"); return -1; } - if (sqlVdbeIntValue(right, &r, &unused) != 0) { + if (mem_get_integer(right, &r, &unused) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right), "integer"); return -1; @@ -1586,7 +1618,7 @@ mem_bit_not(struct Mem *mem, struct Mem *result) return 0; int64_t i; bool unused; - if (sqlVdbeIntValue(mem, &i, &unused) != 0) { + if (mem_get_integer(mem, &i, &unused) != 0) { diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(mem), "integer"); return -1; @@ -1867,35 +1899,6 @@ valueToText(sql_value * pVal) return pVal->z; } -/* - * Convert a 64-bit IEEE double into a 64-bit signed integer. - * If the double is out of range of a 64-bit signed integer then - * return the closest available 64-bit signed integer. - */ -static int -doubleToInt64(double r, int64_t *i) -{ - /* - * Many compilers we encounter do not define constants for the - * minimum and maximum 64-bit integers, or they define them - * inconsistently. And many do not understand the "LL" notation. - * So we define our own static constants here using nothing - * larger than a 32-bit integer constant. - */ - static const int64_t maxInt = LARGEST_INT64; - static const int64_t minInt = SMALLEST_INT64; - if (r <= (double)minInt) { - *i = minInt; - return -1; - } else if (r >= (double)maxInt) { - *i = maxInt; - return -1; - } else { - *i = (int64_t) r; - return *i != r; - } -} - /* * It is already known that pMem contains an unterminated string. * Add the zero terminator. @@ -2424,42 +2427,6 @@ mem_value_bool(const struct Mem *mem, bool *b) return -1; } -/* - * Return some kind of integer value which is the best we can do - * at representing the value that *pMem describes as an integer. - * If pMem is an integer, then the value is exact. If pMem is - * a floating-point then the value returned is the integer part. - * If pMem is a string or blob, then we make an attempt to convert - * it into an integer and return that. If pMem represents an - * an SQL-NULL value, return 0. - * - * If pMem represents a string value, its encoding might be changed. - */ -int -sqlVdbeIntValue(Mem * pMem, int64_t *i, bool *is_neg) -{ - int flags; - assert(EIGHT_BYTE_ALIGNMENT(pMem)); - flags = pMem->flags; - if (flags & MEM_Int) { - *i = pMem->u.i; - *is_neg = true; - return 0; - } else if (flags & MEM_UInt) { - *i = pMem->u.u; - *is_neg = false; - return 0; - } else if (flags & MEM_Real) { - *is_neg = pMem->u.r < 0; - return doubleToInt64(pMem->u.r, i); - } else if (flags & (MEM_Str)) { - assert(pMem->z || pMem->n == 0); - if (sql_atoi64(pMem->z, i, is_neg, pMem->n) == 0) - return 0; - } - 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 @@ -2530,30 +2497,12 @@ sql_value_boolean(sql_value *val) return b; } -int -sql_value_int(sql_value * pVal) -{ - int64_t i = 0; - bool is_neg; - sqlVdbeIntValue((Mem *) pVal, &i, &is_neg); - return (int)i; -} - -sql_int64 -sql_value_int64(sql_value * pVal) -{ - int64_t i = 0; - bool unused; - sqlVdbeIntValue((Mem *) pVal, &i, &unused); - return i; -} - uint64_t sql_value_uint64(sql_value *val) { int64_t i = 0; bool is_neg; - sqlVdbeIntValue((struct Mem *) val, &i, &is_neg); + mem_get_integer((struct Mem *) val, &i, &is_neg); assert(!is_neg); return i; } diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index c0b9f4f99..7857efc1d 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -345,6 +345,9 @@ mem_implicit_cast(struct Mem *mem, enum field_type type); int mem_implicit_cast_old(struct Mem *mem, enum field_type type); +int +mem_get_integer(const struct Mem *mem, int64_t *i, bool *is_neg); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -423,7 +426,6 @@ releaseMemArray(Mem * p, int N); int mem_value_bool(const struct Mem *mem, bool *b); -int sqlVdbeIntValue(struct Mem *, int64_t *, bool *is_neg); int sqlVdbeRealValue(struct Mem *, double *); const void * sql_value_blob(struct Mem *); @@ -437,12 +439,6 @@ sql_value_double(struct Mem *); bool sql_value_boolean(struct Mem *val); -int -sql_value_int(struct Mem *); - -sql_int64 -sql_value_int64(struct Mem *); - uint64_t sql_value_uint64(struct Mem *val); diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c index cf32ba3f3..d9140b484 100644 --- a/src/box/sql/printf.c +++ b/src/box/sql/printf.c @@ -144,7 +144,10 @@ getIntArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0; - return sql_value_int64(p->apArg[p->nUsed++]); + int64_t i; + bool unused; + mem_get_integer(p->apArg[p->nUsed++], &i, &unused); + return (sql_int64)i; } static double diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index f7ead1343..14385705f 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -445,15 +445,9 @@ sql_column_bytes16(sql_stmt *, int iCol); double sql_column_double(sql_stmt *, int iCol); -int -sql_column_int(sql_stmt *, int iCol); - bool sql_column_boolean(struct sql_stmt *stmt, int column); -sql_int64 -sql_column_int64(sql_stmt *, int iCol); - uint64_t sql_column_uint64(struct sql_stmt *stmt, int column); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 8f871ea95..abdc5f0d0 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -491,24 +491,12 @@ sql_column_double(sql_stmt * pStmt, int i) return sql_value_double(columnMem(pStmt, i)); } -int -sql_column_int(sql_stmt * pStmt, int i) -{ - return sql_value_int(columnMem(pStmt, i)); -} - bool sql_column_boolean(struct sql_stmt *stmt, int i) { return sql_value_boolean(columnMem(stmt, i)); } -sql_int64 -sql_column_int64(sql_stmt * pStmt, int i) -{ - return sql_value_int64(columnMem(pStmt, i)); -} - uint64_t sql_column_uint64(sql_stmt * pStmt, int i) { -- 2.25.1