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 3D60A71072; Thu, 11 Nov 2021 14:00:10 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3D60A71072 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1636628410; bh=0HfhO8zG1k+x9vOj26E5gjwswMlGApS1Uay6LV4fiKc=; 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=AB9LchEWjF8/Rhz5JaolE4pK9R/U+WK9BU/4Zl3zY3uqIpNYJHj/YrKs/Q89xtK2V QHX9OyGutH4Z/SpouCXp2EUY6sz6uAvdyeH+V1rzn5LUV1KoQnaYAHQa6vsM2GCHjx Wp/Xte23jrpbbm1i/pFgqEm9EeAL7AyvEK9I/obc= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (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 5BA8271072 for ; Thu, 11 Nov 2021 13:49:28 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 5BA8271072 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1ml7e3-0005Bu-BQ; Thu, 11 Nov 2021 13:49:27 +0300 To: kyukhin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Thu, 11 Nov 2021 13:49:26 +0300 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9731B3922EC063979EDB9C2FB80C34FA759345864EB942FB700894C459B0CD1B98FF5F43FFEFC38ED76ADCBDC7431F5B6BD7B9CED87D25567141B3585208100AE X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7D6964C9E324ABA58EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637CE3A619BB4CB99268638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8EA1FDB0BBF4443BE95070BD77551235B117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC8C7ADC89C2F0B2A5A471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F44604297287769387670735201E561CDFBCA1751FBDFBBEFFF4125B51D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B66F6A3E018CF4DC80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C7E9FEBB9C11794A1F529DB03E7E867DF090D67AD4BB25E179C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFCDE59DDCCE53AF2B699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34F6CC1FD3366963364DA894520333F60728EAD665D92EC6371DC91E22637CED3BC00D9AEC8B7EA37C1D7E09C32AA3244CBA77105BD0CCE96CE1AF8A26AF41CA843FD9C8CA1B0515E0729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj4t8MBgWr8bIQ01s9A5gpIg== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5DAAD350E46A6E4F6C2E1D1AA0C529D6C983D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 20/21] sql: remove MEM_Term flag 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 removes the MEM_Term flag, because after changes in the SQL built-in functions, this flag is no longer used. Needed for #4145 --- src/box/sql/mem.c | 78 +++-------------------------------------- src/box/sql/mem.h | 36 ------------------- src/box/sql/printf.c | 20 +++++++++-- src/box/sql/vdbe.h | 4 ++- src/box/sql/vdbeaux.c | 31 ++++------------ src/box/sql/whereexpr.c | 18 +++++++--- 6 files changed, 43 insertions(+), 144 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 4150a24f0..1f5df3332 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -346,21 +346,18 @@ void mem_set_str0_ephemeral(struct Mem *mem, char *value) { set_str_const(mem, value, strlen(value), MEM_Ephem); - mem->flags |= MEM_Term; } void mem_set_str0_static(struct Mem *mem, char *value) { set_str_const(mem, value, strlen(value), MEM_Static); - mem->flags |= MEM_Term; } void mem_set_str0_allocated(struct Mem *mem, char *value) { set_str_dynamic(mem, value, strlen(value), 0); - mem->flags |= MEM_Term; } int @@ -392,7 +389,6 @@ mem_copy_str0(struct Mem *mem, const char *value) if (mem_copy_str(mem, value, len + 1) != 0) return -1; mem->n = len; - mem->flags |= MEM_Term; return 0; } @@ -657,24 +653,12 @@ int_to_str0(struct Mem *mem) return mem_copy_str0(mem, str); } -static inline int -str_to_str0(struct Mem *mem) -{ - assert(mem->type == MEM_TYPE_STR); - if (sqlVdbeMemGrow(mem, mem->n + 1, 1) != 0) - return -1; - mem->z[mem->n] = '\0'; - mem->flags |= MEM_Term; - mem->flags &= ~MEM_Scalar; - return 0; -} - static inline int str_to_bin(struct Mem *mem) { assert(mem->type == MEM_TYPE_STR); mem->type = MEM_TYPE_BIN; - mem->flags &= ~(MEM_Term | MEM_Scalar); + mem->flags &= ~MEM_Scalar; return 0; } @@ -725,18 +709,6 @@ bin_to_str(struct Mem *mem) return 0; } -static inline int -bin_to_str0(struct Mem *mem) -{ - assert(mem->type == MEM_TYPE_BIN); - if (sqlVdbeMemGrow(mem, mem->n + 1, 1) != 0) - return -1; - mem->z[mem->n] = '\0'; - mem->type = MEM_TYPE_STR; - mem->flags = MEM_Term; - return 0; -} - static inline int bin_to_uuid(struct Mem *mem) { @@ -1002,7 +974,7 @@ double_to_str0(struct Mem *mem) sql_snprintf(BUF_SIZE, mem->z, "%!.15g", mem->u.r); mem->n = strlen(mem->z); mem->type = MEM_TYPE_STR; - mem->flags = MEM_Term; + mem->flags = 0; return 0; } @@ -1284,39 +1256,6 @@ mem_to_number(struct Mem *mem) return -1; } -int -mem_to_str0(struct Mem *mem) -{ - assert(mem->type < MEM_TYPE_INVALID); - switch (mem->type) { - case MEM_TYPE_STR: - if ((mem->flags & MEM_Term) != 0) { - mem->flags &= ~MEM_Scalar; - return 0; - } - return str_to_str0(mem); - case MEM_TYPE_INT: - case MEM_TYPE_UINT: - return int_to_str0(mem); - case MEM_TYPE_DOUBLE: - return double_to_str0(mem); - case MEM_TYPE_BOOL: - return bool_to_str0(mem); - case MEM_TYPE_BIN: - return bin_to_str0(mem); - case MEM_TYPE_MAP: - return map_to_str0(mem); - case MEM_TYPE_ARRAY: - return array_to_str0(mem); - case MEM_TYPE_UUID: - return uuid_to_str0(mem); - case MEM_TYPE_DEC: - return dec_to_str0(mem); - default: - return -1; - } -} - int mem_to_str(struct Mem *mem) { @@ -1763,15 +1702,6 @@ mem_get_bool(const struct Mem *mem, bool *b) return -1; } -int -mem_get_str0(const struct Mem *mem, const char **s) -{ - if (mem->type != MEM_TYPE_STR || (mem->flags & MEM_Term) == 0) - return -1; - *s = mem->z; - return 0; -} - int mem_get_bin(const struct Mem *mem, const char **s) { @@ -1814,7 +1744,7 @@ mem_copy(struct Mem *to, const struct Mem *from) to->szMalloc = sqlDbMallocSize(to->db, to->zMalloc); memcpy(to->zMalloc, to->z, to->n); to->z = to->zMalloc; - to->flags &= MEM_Term; + to->flags = 0; return 0; } @@ -1831,7 +1761,7 @@ mem_copy_as_ephemeral(struct Mem *to, const struct Mem *from) return; if ((to->flags & (MEM_Static | MEM_Ephem)) != 0) return; - to->flags &= MEM_Term; + to->flags = 0; to->flags |= MEM_Ephem; return; } diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index d2e9cc135..1ef8a945b 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -98,13 +98,6 @@ struct Mem { /** MEM is of SCALAR meta-type. */ #define MEM_Scalar 0x0002 #define MEM_Cleared 0x0200 /* NULL set by OP_Null, not from data */ - -/* Whenever Mem contains a valid string or blob representation, one of - * the following flags must be set to determine the memory management - * policy for Mem.z. The MEM_Term flag tells us whether or not the - * string is \000 or \u0000 terminated - */ -#define MEM_Term 0x0400 /* String rep is nul terminated */ #define MEM_Static 0x1000 /* Mem.z points to a static string */ #define MEM_Ephem 0x2000 /* Mem.z points to an ephemeral string */ @@ -610,14 +603,6 @@ mem_to_number(struct Mem *mem); int mem_to_str(struct Mem *mem); -/** - * Convert the given MEM to STRING. This function and the function above define - * the rules that are used to convert values of all other types to STRING. In - * this function, the string received after convertion is NULL-terminated. - */ -int -mem_to_str0(struct Mem *mem); - /** Convert the given MEM to given type according to explicit cast rules. */ int mem_cast_explicit(struct Mem *mem, enum field_type type); @@ -722,27 +707,6 @@ mem_get_bool_unsafe(const struct Mem *mem) return b; } -/** - * Return value for MEM of STRING type if MEM contains a NULL-terminated string. - * Otherwise convert value of the MEM to NULL-terminated string if possible and - * return converted value. Original MEM is not changed. - */ -int -mem_get_str0(const struct Mem *mem, const char **s); - -/** - * Return value for MEM of STRING type if MEM contains NULL-terminated string. - * Otherwise convert MEM to MEM of string type that contains NULL-terminated - * string and return its value. Return NULL if conversion is impossible. - */ -static inline const char * -mem_as_str0(struct Mem *mem) -{ - if (mem_to_str0(mem) != 0) - return NULL; - return mem->z; -} - /** * Return value for MEM of VARBINARY type. For MEM of all other types convert * value of the MEM to VARBINARY if possible and return converted value. diff --git a/src/box/sql/printf.c b/src/box/sql/printf.c index 5b61646e3..8da7c9878 100644 --- a/src/box/sql/printf.c +++ b/src/box/sql/printf.c @@ -160,8 +160,20 @@ getTextArg(PrintfArguments * p) { if (p->nArg <= p->nUsed) return 0; - struct Mem *mem = &p->apArg[p->nUsed++]; - return (char *)mem_as_str0(mem); + struct Mem mem; + mem_create(&mem); + mem_copy_as_ephemeral(&mem, &p->apArg[p->nUsed++]); + if (mem_to_str(&mem) != 0) { + mem_destroy(&mem); + return NULL; + } + char *str = sqlDbMallocRawNN(sql_get(), mem.n + 1); + if (str == NULL) + return NULL; + memcpy(str, mem.z, mem.n); + str[mem.n] = '\0'; + mem_destroy(&mem); + return str; } /* @@ -677,6 +689,7 @@ sqlVXPrintf(StrAccum * pAccum, /* Accumulate results here */ if (bArgList) { bufpt = getTextArg(pArgList); c = bufpt ? bufpt[0] : 0; + zExtra = bufpt; } else { c = va_arg(ap, int); } @@ -697,7 +710,7 @@ sqlVXPrintf(StrAccum * pAccum, /* Accumulate results here */ case etDYNSTRING: if (bArgList) { bufpt = getTextArg(pArgList); - xtype = etSTRING; + xtype = etDYNSTRING; } else { bufpt = va_arg(ap, char *); } @@ -727,6 +740,7 @@ sqlVXPrintf(StrAccum * pAccum, /* Accumulate results here */ if (bArgList) { escarg = getTextArg(pArgList); + zExtra = escarg; } else { escarg = va_arg(ap, char *); } diff --git a/src/box/sql/vdbe.h b/src/box/sql/vdbe.h index 106555bb1..48e687d49 100644 --- a/src/box/sql/vdbe.h +++ b/src/box/sql/vdbe.h @@ -260,6 +260,9 @@ vdbe_metadata_set_col_autoincrement(struct Vdbe *p, int idx); int vdbe_metadata_set_col_span(struct Vdbe *p, int idx, const char *span); +const struct Mem * +vdbe_get_bound_value(struct Vdbe *vdbe, int id); + void sqlVdbeCountChanges(Vdbe *); sql *sqlVdbeDb(Vdbe *); void sqlVdbeSetSql(Vdbe *, const char *z, int n); @@ -268,7 +271,6 @@ void sqlVdbeSwap(Vdbe *, Vdbe *); struct VdbeOp * sqlVdbeTakeOpArray(struct Vdbe *p, int *pnOp); -sql_value *sqlVdbeGetBoundValue(Vdbe *, int); char *sqlVdbeExpandSql(Vdbe *, const char *); /** diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index b71f65966..109ab1446 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -2285,31 +2285,12 @@ sqlVdbeDb(Vdbe * v) return v->db; } -/* - * Return a pointer to an sql_value structure containing the value bound - * parameter iVar of VM v. Except, if the value is an SQL NULL, return - * 0 instead. Unless it is NULL, apply type to the value before returning it. - * - * The returned value must be freed by the caller using sqlValueFree(). - */ -sql_value * -sqlVdbeGetBoundValue(struct Vdbe *v, int iVar) -{ - assert(iVar > 0); - if (v) { - Mem *pMem = &v->aVar[iVar - 1]; - if (!mem_is_null(pMem)) { - sql_value *pRet = sqlValueNew(v->db); - if (pRet == NULL) - return NULL; - if (mem_copy(pRet, pMem) != 0) { - sqlValueFree(pRet); - return NULL; - } - return pRet; - } - } - return 0; +const struct Mem * +vdbe_get_bound_value(struct Vdbe *vdbe, int id) +{ + if (vdbe == NULL || id < 0 || id >= vdbe->nVar) + return NULL; + return &vdbe->aVar[id]; } void diff --git a/src/box/sql/whereexpr.c b/src/box/sql/whereexpr.c index 6849f13ec..08a1b4e3a 100644 --- a/src/box/sql/whereexpr.c +++ b/src/box/sql/whereexpr.c @@ -268,7 +268,6 @@ like_optimization_is_valid(Parse *pParse, Expr *pExpr, Expr **ppPrefix, int cnt; /* Database connection. */ sql *db = pParse->db; - sql_value *pVal = 0; /* Opcode of pRight. */ int op; /* Result code to return. */ @@ -306,13 +305,22 @@ like_optimization_is_valid(Parse *pParse, Expr *pExpr, Expr **ppPrefix, return 0; op = pRight->op; + struct region *region = &pParse->region; + size_t svp = region_used(region); if (op == TK_VARIABLE) { Vdbe *pReprepare = pParse->pReprepare; int iCol = pRight->iColumn; - pVal = sqlVdbeGetBoundValue(pReprepare, iCol); - if (pVal != NULL && mem_is_str(pVal)) { - if (mem_as_str0(pVal) == NULL) + const struct Mem *var = vdbe_get_bound_value(pReprepare, iCol); + if (var != NULL && mem_is_str(var)) { + uint32_t size = var->n + 1; + char *str = region_alloc(region, size); + if (str == NULL) { + diag_set(OutOfMemory, size, "region", "str"); return -1; + } + memcpy(str, var->z, var->n); + str[var->n] = '\0'; + z = str; } assert(pRight->op == TK_VARIABLE || pRight->op == TK_REGISTER); } else if (op == TK_STRING) { @@ -356,8 +364,8 @@ like_optimization_is_valid(Parse *pParse, Expr *pExpr, Expr **ppPrefix, } } + region_truncate(region, svp); rc = (z != 0); - sqlValueFree(pVal); return rc; } -- 2.25.1