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 E2CF66EC5B; Sat, 10 Apr 2021 00:08:19 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E2CF66EC5B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618002499; bh=GR2gaaIBtZQ/gVGEdVJpxOBeMCjxhes7HX6PopikCRw=; 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=jIVqmZx5nqqAOpVz3Vm48bUVgwh4dEr5lwS/3wpesTZlikBFHiEOuS1q4FyOGc3mx LeXiTkH6ihjb/TNT9OdkQizpKB2q+OL60DHTrkjZ7eEEhhGmAYr03uUoSuADrbWUct c8GoYSIg9gFIUO+Irl7jtLUnojKbGL0u/vkQQjt8= Received: from smtpng2.m.smailru.net (smtpng2.m.smailru.net [94.100.179.3]) (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 A08EA6EC5B for ; Sat, 10 Apr 2021 00:08:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A08EA6EC5B Received: by smtpng2.m.smailru.net with esmtpa (envelope-from ) id 1lUyMT-00042o-Hb; Sat, 10 Apr 2021 00:08:17 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Sat, 10 Apr 2021 00:08:17 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: EEAE043A70213CC8 X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E7480BE79914FF86F9151AC38CC435EA4A654182A05F5380850403BDB0AFFB9D33DC45562BFFF5892E06A707634CB6EE2DC6496B999F79A3013AA X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7059B0D8AC717918AEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063770398A047C76876C8638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B21AB39FBD654A74C5C7BB9A15A34CB88EA2BB4B7391E66DBBD2E47CDBA5A96583C09775C1D3CA48CFCA5A41EBD8A3A0199FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3ED8438A78DFE0A9E117882F4460429728AD0CFFFB425014E868A13BD56FB6657E2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2AD77751E876CB595E8F7B195E1C978315E70F26B18E57C1699D32A3E8D98C3BE X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975CD0035DD76F8A8A4F952B01EB6FD28FD7B4833CA4F8DCB32B9C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34AA13E2DDB9067862FB8460A375C911308EEFA0E8763998B929307FC1B4D7BE5C00F522669FD6C3AD1D7E09C32AA3244CAC5A36A8658EAFFE29B84A915D51EDB05A1673A01BA68E40FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojqcJA+pXcDulc0JN21GrVfA== X-Mailru-Sender: 689FA8AB762F73936BC43F508A063822D6B43EE3F11FDFDC145B8AF5F1412BB083D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v5 46/52] sql: introduce mem_get_uint() 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" Thank you for the review! My answer and new patch below. On 30.03.2021 02:08, Vladislav Shpilevoy wrote: > Thanks for the patch! > > On 23.03.2021 10:36, Mergen Imeev via Tarantool-patches wrote: >> This patch introduces mem_get_unsigned() function which is used to >> receive unsigned value from MEM. >> >> Part of #5818 >> --- >> src/box/sql/func.c | 16 +++++++++++----- >> src/box/sql/mem.c | 37 +++++++++++++++++++++++++++---------- >> src/box/sql/mem.h | 6 +++--- >> src/box/sql/sqlInt.h | 3 --- >> src/box/sql/vdbeapi.c | 6 ------ >> 5 files changed, 41 insertions(+), 27 deletions(-) >> >> diff --git a/src/box/sql/func.c b/src/box/sql/func.c >> index 0fa0f6ac7..a851d98f2 100644 >> --- a/src/box/sql/func.c >> +++ b/src/box/sql/func.c >> @@ -118,9 +118,12 @@ port_vdbemem_dump_lua(struct port *base, struct lua_State *L, bool is_flat) >> luaL_pushint64(L, n); >> break; >> } >> - case MP_UINT: >> - luaL_pushuint64(L, sql_value_uint64(param)); >> + case MP_UINT: { >> + uint64_t u; >> + mem_get_unsigned(param, &u); >> + luaL_pushuint64(L, u); > Maybe we could make 2 functions? One to get the value and ignore > the errors, and the other to get as an out parameter + return an > error? > > For instance, mem_to_uint() - returns uint64_t and internally asserts > that the value is correct. And mem_get_uint() works like your version. > > The same for the other get functions whose result is often ignored. For some functions I created a "proxy" functions in func.c the way you described, but not for this function since it is only used in a few places of sql/func.c. Should I do this for all functions? In func.c I mean. I see this as temporary measure, since I hope we will rework built-in functions one day. New patch: commit bdd3107a42b82a0e1f964819fa0bf4798fd87a4e Author: Mergen Imeev Date: Wed Mar 17 13:55:37 2021 +0300 sql: introduce mem_get_uint() This patch introduces mem_get_uint() function. This function is used to receive unsigned value from MEM. If value of MEM is not unsigned, it is converted to unsigned if possible. MEM is not changed. Part of #5818 diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 701e77d49..f2254fb29 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -201,7 +201,9 @@ absFunc(sql_context * context, int argc, sql_value ** argv) UNUSED_PARAMETER(argc); switch (sql_value_type(argv[0])) { case MP_UINT: { - sql_result_uint(context, sql_value_uint64(argv[0])); + uint64_t u; + mem_get_uint(argv[0], &u); + sql_result_uint(context, u); break; } case MP_INT: { @@ -1174,7 +1176,7 @@ charFunc(sql_context * context, int argc, sql_value ** argv) if (sql_value_type(argv[i]) == MP_INT) x = 0xfffd; else - x = sql_value_uint64(argv[i]); + mem_get_uint(argv[i], &x); if (x > 0x10ffff) x = 0xfffd; c = (unsigned)(x & 0x1fffff); @@ -1478,7 +1480,8 @@ 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; - uint64_t n = sql_value_uint64(arg1); + uint64_t n; + mem_get_uint(arg1, &n); trim_procedure(context, n, (const unsigned char *) " ", &len_one, 1, input_str, input_str_sz); } else if ((trim_set = sql_value_text(arg1)) != NULL) { @@ -1518,7 +1521,8 @@ trim_func_three_args(struct sql_context *context, sql_value *arg1, &char_len); if (char_cnt == -1) return; - uint64_t n = sql_value_uint64(arg1); + uint64_t n; + mem_get_uint(arg1, &n); trim_procedure(context, n, trim_set, char_len, char_cnt, input_str, input_str_sz); sql_free(char_len); diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 259bb7d2c..136da9d5d 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1263,6 +1263,33 @@ mem_get_int(const struct Mem *mem, int64_t *i, bool *is_neg) return -1; } +int +mem_get_uint(const struct Mem *mem, uint64_t *u) +{ + if ((mem->flags & MEM_Int) != 0) + return -1; + if ((mem->flags & MEM_UInt) != 0) { + *u = mem->u.u; + return 0; + } + if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) { + bool is_neg; + if (sql_atoi64(mem->z, (int64_t *)u, &is_neg, mem->n) != 0 || + is_neg) + return -1; + return 0; + } + if ((mem->flags & MEM_Real) != 0) { + double d = mem->u.r; + if (d >= 0 && d < (double)UINT64_MAX) { + *u = (uint64_t)d; + return 0; + } + return -1; + } + return -1; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -2507,16 +2534,6 @@ sql_value_boolean(sql_value *val) return b; } -uint64_t -sql_value_uint64(sql_value *val) -{ - int64_t i = 0; - bool is_neg; - mem_get_int((struct Mem *) val, &i, &is_neg); - assert(!is_neg); - return i; -} - const unsigned char * sql_value_text(sql_value * pVal) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 2b5385c55..87aee0e9b 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -544,6 +544,14 @@ mem_cast_implicit_old(struct Mem *mem, enum field_type type); int mem_get_int(const struct Mem *mem, int64_t *i, bool *is_neg); +/** + * Return value for MEM of UNSIGNED type. For MEM of all other types convert + * value of the MEM to UNSIGNED if possible and return converted value. Original + * MEM is not changed. + */ +int +mem_get_uint(const struct Mem *mem, uint64_t *u); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -611,9 +619,6 @@ sql_value_double(struct Mem *); bool sql_value_boolean(struct Mem *val); -uint64_t -sql_value_uint64(struct Mem *val); - const unsigned char * sql_value_text(struct Mem *); diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 0af247ebf..6ead9b261 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -448,9 +448,6 @@ sql_column_double(sql_stmt *, int iCol); bool sql_column_boolean(struct sql_stmt *stmt, int column); -uint64_t -sql_column_uint64(struct sql_stmt *stmt, int column); - const unsigned char * sql_column_text(sql_stmt *, int iCol); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index 40404a3b7..85bbbc420 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -496,12 +496,6 @@ sql_column_boolean(struct sql_stmt *stmt, int i) return sql_value_boolean(columnMem(stmt, i)); } -uint64_t -sql_column_uint64(sql_stmt * pStmt, int i) -{ - return sql_value_uint64(columnMem(pStmt, i)); -} - const unsigned char * sql_column_text(sql_stmt * pStmt, int i) {