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 DE0B26BD36; Tue, 23 Mar 2021 13:01:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org DE0B26BD36 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616493680; bh=raK18XkhcaPIZ/iKM1pJssXlv8AqI++1UHoQM7FFX8U=; 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=k8Sc3b94bDUIeP7seJJ72Yofkj7dxGbs6C6JTa1eLZo9hqNxMC2giKEDzoSY5zzEx kX/xM2PsLPskAl2LdJGmK84/coi+Sm3jXir+Bn8zKOPSqnNHp/2kYX79ubhabJ95Fe pawfFV0+EfIhxhqqENnLUIFvbts3CUvunsUKMNPc= Received: from smtp48.i.mail.ru (smtp48.i.mail.ru [94.100.177.108]) (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 8D98E68F65 for ; Tue, 23 Mar 2021 12:36:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8D98E68F65 Received: by smtp48.i.mail.ru with esmtpa (envelope-from ) id 1lOdT4-0002j8-PS; Tue, 23 Mar 2021 12:36:55 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:36:54 +0300 Message-Id: <7248eed5fda1db86a8da5a01b5c8887adb8d4eb5.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: 4F1203BC0FB41BD95D6E7CC48CB1F5F111116110879554077ED795A4341E98F3182A05F5380850401998134D708E9F9FE2B3D2BD1C20E533F2266A28CEE6A274054C4FF39C9F762C X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE761966F250AC1AE21EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006377E3992E1815AFF058638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75E214B4DD898C2032E91DA7842E5EA5759A471835C12D1D9774AD6D5ED66289B5259CC434672EE6371117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658359CC434672EE6371117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF1BD1948763FFC2C2BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B66280DB014E7A33FB76E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7C5E1C53F199C2BB95C8A9BA7A39EFB7666BA297DBC24807EA089D37D7C0E48F6C8AA50765F7900637BC468E7E89D8C5D6EFF80C71ABB335746BA297DBC24807EA27F269C8F02392CD4DF8A859A081327B27F269C8F02392CD5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75E214B4DD898C2032E91DA7842E5EA57599C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D95739AEDB3821B48688D710EB139D95F6B3D8F8474B77E5140CDD5FBDCDAED9DCE166258E7E0C9E1D7E09C32AA3244C398AA87D162DD3042429C35D06B82EFAFE8DA44ABE2443F7FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UgWjH48YqjUgQ== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306EDF2C1F08410DC9AA2E2B3D2BD1C20E533E5E949923882BAAD5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 53/53] sql: introduce mem_get_length() 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_length() which is used to receive length of string or binary value of MEM. Part of #5818 --- src/box/sql/func.c | 57 ++++++++++++++------------- src/box/sql/mem.c | 92 +++++-------------------------------------- src/box/sql/mem.h | 15 ++----- src/box/sql/sqlInt.h | 3 -- src/box/sql/vdbeapi.c | 35 ---------------- 5 files changed, 42 insertions(+), 160 deletions(-) diff --git a/src/box/sql/func.c b/src/box/sql/func.c index 199f3abef..6687fca3c 100644 --- a/src/box/sql/func.c +++ b/src/box/sql/func.c @@ -168,7 +168,7 @@ port_vdbemem_dump_lua(struct port *base, struct lua_State *L, bool is_flat) case MP_ARRAY: case MP_MAP: lua_pushlstring(L, mem_get_blob(param), - (size_t) sql_value_bytes(param)); + (size_t) mem_get_length(param)); break; case MP_NIL: lua_pushnil(L); @@ -230,9 +230,9 @@ port_vdbemem_get_msgpack(struct port *base, uint32_t *size) case MP_BIN: case MP_ARRAY: case MP_MAP: { - mpstream_encode_binl(&stream, sql_value_bytes(param)); + mpstream_encode_binl(&stream, mem_get_length(param)); mpstream_memcpy(&stream, mem_get_blob(param), - sql_value_bytes(param)); + mem_get_length(param)); break; } case MP_NIL: { @@ -524,14 +524,15 @@ lengthFunc(sql_context * context, int argc, sql_value ** argv) case MP_UINT: case MP_BOOL: case MP_DOUBLE:{ - sql_result_uint(context, sql_value_bytes(argv[0])); + mem_get_blob(argv[0]); + sql_result_uint(context, mem_get_length(argv[0])); break; } case MP_STR:{ const unsigned char *z = mem_get_ustr(argv[0]); if (z == 0) return; - len = sql_utf8_char_count(z, sql_value_bytes(argv[0])); + len = sql_utf8_char_count(z, mem_get_length(argv[0])); sql_result_uint(context, len); break; } @@ -647,8 +648,8 @@ position_func(struct sql_context *context, int argc, struct Mem **argv) return; } - int n_needle_bytes = sql_value_bytes(needle); - int n_haystack_bytes = sql_value_bytes(haystack); + int n_needle_bytes = mem_get_length(needle); + int n_haystack_bytes = mem_get_length(haystack); int position = 1; if (n_needle_bytes > 0) { const unsigned char *haystack_str; @@ -797,18 +798,18 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) bool unused; mem_get_integer(argv[1], &p1, &unused); if (p0type == MP_BIN) { - len = sql_value_bytes(argv[0]); + len = mem_get_length(argv[0]); z = mem_get_blob(argv[0]); if (z == 0) return; - assert(len == sql_value_bytes(argv[0])); + assert(len == mem_get_length(argv[0])); } else { z = mem_get_ustr(argv[0]); if (z == 0) return; len = 0; if (p1 < 0) - len = sql_utf8_char_count(z, sql_value_bytes(argv[0])); + len = sql_utf8_char_count(z, mem_get_length(argv[0])); } if (argc == 3) { mem_get_integer(argv[2], &p2, &unused); @@ -847,7 +848,7 @@ substrFunc(sql_context * context, int argc, sql_value ** argv) * used because '\0' is not supposed to be * end-of-string symbol. */ - int byte_size = sql_value_bytes(argv[0]); + int byte_size = mem_get_length(argv[0]); int n_chars = sql_utf8_char_count(z, byte_size); int cnt = 0; int i = 0; @@ -967,7 +968,7 @@ case_type##ICUFunc(sql_context *context, int argc, sql_value **argv) \ return; \ } \ z2 = mem_get_str(argv[0]); \ - n = sql_value_bytes(argv[0]); \ + n = mem_get_length(argv[0]); \ /* \ * Verify that the call to _bytes() \ * does not invalidate the _text() pointer. \ @@ -1305,15 +1306,15 @@ likeFunc(sql_context *context, int argc, sql_value **argv) } const char *zB = mem_get_str(argv[0]); const char *zA = mem_get_str(argv[1]); - const char *zB_end = zB + sql_value_bytes(argv[0]); - const char *zA_end = zA + sql_value_bytes(argv[1]); + const char *zB_end = zB + mem_get_length(argv[0]); + const char *zA_end = zA + mem_get_length(argv[1]); /* * Limit the length of the LIKE pattern to avoid problems * of deep recursion and N*N behavior in * sql_utf8_pattern_compare(). */ - nPat = sql_value_bytes(argv[0]); + nPat = mem_get_length(argv[0]); testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]); testcase(nPat == db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH] + 1); if (nPat > db->aLimit[SQL_LIMIT_LIKE_PATTERN_LENGTH]) { @@ -1334,7 +1335,7 @@ likeFunc(sql_context *context, int argc, sql_value **argv) const unsigned char *zEsc = mem_get_ustr(argv[2]); if (zEsc == 0) return; - if (sql_utf8_char_count(zEsc, sql_value_bytes(argv[2])) != 1) { + if (sql_utf8_char_count(zEsc, mem_get_length(argv[2])) != 1) { diag_set(ClientError, ER_SQL_EXECUTE, "ESCAPE "\ "expression must be a single character"); context->is_aborted = true; @@ -1434,7 +1435,7 @@ quoteFunc(sql_context * context, int argc, sql_value ** argv) case MP_MAP: { char *zText = 0; char const *zBlob = mem_get_blob(argv[0]); - int nBlob = sql_value_bytes(argv[0]); + int nBlob = mem_get_length(argv[0]); assert(zBlob == mem_get_blob(argv[0])); /* No encoding change */ zText = (char *)contextMalloc(context, @@ -1570,7 +1571,7 @@ hexFunc(sql_context * context, int argc, sql_value ** argv) assert(argc == 1); UNUSED_PARAMETER(argc); pBlob = mem_get_blob(argv[0]); - n = sql_value_bytes(argv[0]); + n = mem_get_length(argv[0]); assert(pBlob == mem_get_blob(argv[0])); /* No encoding change */ z = zHex = contextMalloc(context, ((i64) n) * 2 + 1); if (zHex) { @@ -1629,7 +1630,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) zStr = mem_get_ustr(argv[0]); if (zStr == 0) return; - nStr = sql_value_bytes(argv[0]); + nStr = mem_get_length(argv[0]); assert(zStr == mem_get_ustr(argv[0])); /* No encoding change */ zPattern = mem_get_ustr(argv[1]); if (zPattern == 0) { @@ -1637,7 +1638,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) || sql_context_db_handle(context)->mallocFailed); return; } - nPattern = sql_value_bytes(argv[1]); + nPattern = mem_get_length(argv[1]); if (nPattern == 0) { assert(! mem_is_null(argv[1])); sql_result_value(context, argv[0]); @@ -1647,7 +1648,7 @@ replaceFunc(sql_context * context, int argc, sql_value ** argv) zRep = mem_get_ustr(argv[2]); if (zRep == 0) return; - nRep = sql_value_bytes(argv[2]); + nRep = mem_get_length(argv[2]); assert(zRep == mem_get_ustr(argv[2])); nOut = nStr + 1; assert(nOut < SQL_MAX_LENGTH); @@ -1809,7 +1810,7 @@ trim_func_one_arg(struct sql_context *context, sql_value *arg) default_trim = (const unsigned char *) "\0"; else default_trim = (const unsigned char *) " "; - int input_str_sz = sql_value_bytes(arg); + int input_str_sz = mem_get_length(arg); const unsigned char *input_str = mem_get_ustr(arg); uint8_t trim_char_len[1] = { 1 }; trim_procedure(context, TRIM_BOTH, default_trim, trim_char_len, 1, @@ -1835,7 +1836,7 @@ trim_func_two_args(struct sql_context *context, sql_value *arg1, if ((input_str = mem_get_ustr(arg2)) == NULL) return; - int input_str_sz = sql_value_bytes(arg2); + int input_str_sz = mem_get_length(arg2); if (sql_value_type(arg1) == MP_INT || sql_value_type(arg1) == MP_UINT) { uint8_t len_one = 1; bool unused; @@ -1844,7 +1845,7 @@ trim_func_two_args(struct sql_context *context, sql_value *arg1, trim_procedure(context, n, (const unsigned char *) " ", &len_one, 1, input_str, input_str_sz); } else if ((trim_set = mem_get_ustr(arg1)) != NULL) { - int trim_set_sz = sql_value_bytes(arg1); + int trim_set_sz = mem_get_length(arg1); uint8_t *char_len; int char_cnt = trim_prepare_char_len(context, trim_set, trim_set_sz, &char_len); @@ -1873,8 +1874,8 @@ trim_func_three_args(struct sql_context *context, sql_value *arg1, (trim_set = mem_get_ustr(arg2)) == NULL) return; - int trim_set_sz = sql_value_bytes(arg2); - int input_str_sz = sql_value_bytes(arg3); + int trim_set_sz = mem_get_length(arg2); + int input_str_sz = mem_get_length(arg3); uint8_t *char_len; int char_cnt = trim_prepare_char_len(context, trim_set, trim_set_sz, &char_len); @@ -2196,7 +2197,7 @@ groupConcatStep(sql_context * context, int argc, sql_value ** argv) if (!firstTerm) { if (argc == 2) { zSep = mem_get_str(argv[1]); - nSep = sql_value_bytes(argv[1]); + nSep = mem_get_length(argv[1]); } else { zSep = ","; nSep = 1; @@ -2205,7 +2206,7 @@ groupConcatStep(sql_context * context, int argc, sql_value ** argv) sqlStrAccumAppend(pAccum, zSep, nSep); } zVal = mem_get_str(argv[0]); - nVal = sql_value_bytes(argv[0]); + nVal = mem_get_length(argv[0]); if (zVal) sqlStrAccumAppend(pAccum, zVal, nVal); } diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index bed9ae64a..83b0f4f7f 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -1341,6 +1341,16 @@ mem_get_binary(const struct Mem *mem, const char **s) return 0; } +int +mem_get_length(const struct Mem *mem) +{ + if ((mem->flags & (MEM_Str | MEM_Blob)) == 0) + return 0; + if ((mem->flags & MEM_Blob) !=0 && (mem->flags & MEM_Zero) != 0) + return mem->n + mem->u.nZero; + return mem->n; +} + int mem_copy(struct Mem *to, const struct Mem *from) { @@ -2051,41 +2061,6 @@ sql_value_type(sql_value *pVal) return mem_mp_type(mem); } -/* - * The sqlValueBytes() routine returns the number of bytes in the - * sql_value object assuming that it uses the encoding "enc". - * The valueBytes() routine is a helper function. - */ -static SQL_NOINLINE int -valueBytes(sql_value * pVal) -{ - if (mem_convert_to_string(pVal) != 0) - return 0; - return pVal->n; -} - -int -sqlValueBytes(sql_value * pVal) -{ - Mem *p = (Mem *) pVal; - assert((p->flags & MEM_Null) == 0 - || (p->flags & (MEM_Str | MEM_Blob)) == 0); - if ((p->flags & MEM_Str) != 0) { - return p->n; - } - if ((p->flags & MEM_Blob) != 0) { - if (p->flags & MEM_Zero) { - return p->n + p->u.nZero; - } else { - return p->n; - } - } - if (p->flags & MEM_Null) - return 0; - return valueBytes(pVal); -} - - #ifdef SQL_DEBUG /* * This routine prepares a memory cell for modification by breaking @@ -2447,53 +2422,6 @@ releaseMemArray(Mem * p, int N) } } -int -sql_value_bytes(sql_value * pVal) -{ - return sqlValueBytes(pVal); -} - -/* - * Return a pointer to static memory containing an SQL NULL value. - */ -const Mem * -columnNullValue(void) -{ - /* Even though the Mem structure contains an element - * of type i64, on certain architectures (x86) with certain compiler - * switches (-Os), gcc may align this Mem object on a 4-byte boundary - * instead of an 8-byte one. This all works fine, except that when - * running with SQL_DEBUG defined the sql code sometimes assert()s - * that a Mem structure is located on an 8-byte boundary. To prevent - * these assert()s from failing, when building with SQL_DEBUG defined - * using gcc, we force nullMem to be 8-byte aligned using the magical - * __attribute__((aligned(8))) macro. - */ - static const Mem nullMem -#if defined(SQL_DEBUG) && defined(__GNUC__) - __attribute__ ((aligned(8))) -#endif - = { - /* .u = */ { - 0}, - /* .flags = */ (u16) MEM_Null, - /* .eSubtype = */ (u8) 0, - /* .field_type = */ field_type_MAX, - /* .n = */ (int)0, - /* .z = */ (char *)0, - /* .zMalloc = */ (char *)0, - /* .szMalloc = */ (int)0, - /* .uTemp = */ (u32) 0, - /* .db = */ (sql *) 0, - /* .xDel = */ (void (*)(void *))0, -#ifdef SQL_DEBUG - /* .pScopyFrom = */ (Mem *) 0, - /* .pFiller = */ (void *)0, -#endif - }; - return &nullMem; -} - /* * Return true if the Mem object contains a TEXT or BLOB that is * too large - whose size exceeds SQL_MAX_LENGTH. diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 8af924d43..b3f994d9d 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -363,6 +363,9 @@ mem_get_string0(const struct Mem *mem, const char **s); int mem_get_binary(const struct Mem *mem, const char **s); +int +mem_get_length(const struct Mem *mem); + /** * Simple type to str convertor. It is used to simplify * error reporting. @@ -381,8 +384,6 @@ mem_mp_type(struct Mem *mem); enum mp_type sql_value_type(struct Mem *); -int sqlValueBytes(struct Mem *); - #ifdef SQL_DEBUG void sqlVdbeMemAboutToChange(struct Vdbe *, struct Mem *); int sqlVdbeCheckMemInvariants(struct Mem *); @@ -436,18 +437,8 @@ releaseMemArray(Mem * p, int N); #define MemSetTypeFlag(p, f) \ ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f) -/** Getters. */ - -int -sql_value_bytes(struct Mem *); - #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))]) -const Mem * -columnNullValue(void); - -/** Checkers. */ - int sqlVdbeMemTooBig(Mem *); int sqlMemCompare(const Mem *, const Mem *, const struct coll *); diff --git a/src/box/sql/sqlInt.h b/src/box/sql/sqlInt.h index 9dfd0e4cc..7b909240f 100644 --- a/src/box/sql/sqlInt.h +++ b/src/box/sql/sqlInt.h @@ -433,9 +433,6 @@ sql_stmt_compile(const char *sql, int bytes_count, struct Vdbe *re_prepared, int sql_step(sql_stmt *); -int -sql_column_bytes(sql_stmt *, int iCol); - int sql_column_bytes16(sql_stmt *, int iCol); diff --git a/src/box/sql/vdbeapi.c b/src/box/sql/vdbeapi.c index e76170965..c87b6bfaa 100644 --- a/src/box/sql/vdbeapi.c +++ b/src/box/sql/vdbeapi.c @@ -443,41 +443,6 @@ sql_data_count(sql_stmt * pStmt) return pVm->nResColumn; } -/* - * Check to see if column iCol of the given statement is valid. If - * it is, return a pointer to the Mem for the value of that column. - * If iCol is not valid, return a pointer to a Mem which has a value - * of NULL. - */ -static Mem * -columnMem(sql_stmt * pStmt, int i) -{ - Vdbe *pVm; - Mem *pOut; - - pVm = (Vdbe *) pStmt; - if (pVm == 0) - return (Mem *) columnNullValue(); - assert(pVm->db); - if (pVm->pResultSet != 0 && i < pVm->nResColumn && i >= 0) { - pOut = &pVm->pResultSet[i]; - } else { - pOut = (Mem *) columnNullValue(); - } - return pOut; -} - -/**************************** sql_column_ ****************************** - * The following routines are used to access elements of the current row - * in the result set. - */ - -int -sql_column_bytes(sql_stmt * pStmt, int i) -{ - return sql_value_bytes(columnMem(pStmt, i)); -} - char * sql_result_to_msgpack(struct sql_stmt *stmt, uint32_t *tuple_size, struct region *region) -- 2.25.1