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 4C15568F42; Tue, 23 Mar 2021 12:43:18 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4C15568F42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616492598; bh=FwF62hD5OFSUomix+BpemzK5bCqqqebAS8Pq3iyGcuA=; 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=IuquTjBs9UrgEeTHzQP3xow7WkXwZSJlIeoagPxUDBxas8rGPY4cI7+BVLxiDDCgH y9YCdSP11BwlXBip8xvpjYcNh1vGnbI7FjW/primIJ5bu5zeNV1hcMvPvy6xf+Uhnx /ETfsz3/emWzhI5O7x/oV0E551aW5sel1AF0Z30k= Received: from smtp49.i.mail.ru (smtp49.i.mail.ru [94.100.177.109]) (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 6ACA568F46 for ; Tue, 23 Mar 2021 12:35:31 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 6ACA568F46 Received: by smtp49.i.mail.ru with esmtpa (envelope-from ) id 1lOdRi-00035g-ES; Tue, 23 Mar 2021 12:35:30 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:35:30 +0300 Message-Id: <4daf53a7a48238f0148fe8e5376b6daca15c82ab.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: 4F1203BC0FB41BD95D6E7CC48CB1F5F111116110879554077ED795A4341E98F3182A05F538085040709B160A6419C2E40F76B14AF2641272F2266A28CEE6A274BFABFB25996C4B12 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7BC21C294753CF0FAEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063736901D4B68B3D4FF8638F802B75D45FF914D58D5BE9E6BC131B5C99E7648C95C686AF563A045C75EF06FDE9E4FC5B9DAB9CEA819DE36EA57A471835C12D1D9774AD6D5ED66289B5278DA827A17800CE70F3DDF2BBF19B93A9FA2833FD35BB23D2EF20D2F80756B5F868A13BD56FB6657A471835C12D1D977725E5C173C3A84C3E3800B164E348C91117882F4460429728AD0CFFFB425014E868A13BD56FB6657D81D268191BDAD3DC09775C1D3CA48CF660E54D1C9FB2B13BA3038C0950A5D36C8A9BA7A39EFB766EC990983EF5C0329BA3038C0950A5D36D5E8D9A59859A8B64341E4E1DBBE86AA76E601842F6C81A1F004C906525384307823802FF610243DF43C7A68FF6260569E8FC8737B5C2249EC8D19AE6D49635B3BBE47FD9DD3FB59A8DF7F3B2552694A57739F23D657EF2B13377AFFFEAFD26923F8577A6DFFEA7C7D149460FF1CF91893EC92FD9297F6715571747095F342E857739F23D657EF2BD5E8D9A59859A8B6EC71F829093BE88A089D37D7C0E48F6C5571747095F342E857739F23D657EF2B6825BDBE14D8E7024847893F9AA87235E5BFE6E7EFDEDCD789D4C264860C145E X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A2BBE337FB72E923155C0AF1600DCBC20B386D740E071D760DBE8F73199320C1D0 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75EF06FDE9E4FC5B9DAB9CEA819DE36EA579C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34BDCC71B3781C9C9573C4953748451E99AECF44E25667EFC7B32736BEE9BA59F922B1A28717F832F11D7E09C32AA3244CD6DF996D3ED8AF1612648045D7AE19F9FE8DA44ABE2443F7FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6UhIg9oUKtPDfQ== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306EDB13943BC56BB5BD60F76B14AF2641272E5E949923882BAAD5105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 17/53] sql: introduce mem_concat() 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_concat(). Function mem_concat() concatenates values from two MEMs in case these values are strings or binaries and writes the result to the third MEM. Part of #5818 --- src/box/sql/mem.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ src/box/sql/mem.h | 7 +++++++ src/box/sql/vdbe.c | 52 ++-------------------------------------------- 3 files changed, 60 insertions(+), 50 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 775474f69..f160439c9 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -319,6 +319,57 @@ mem_move(struct Mem *to, struct Mem *from) return 0; } +int +mem_concat(struct Mem *left, struct Mem *right, struct Mem *result) +{ + assert(result != right); + sqlVdbeMemSetNull(result); + result->field_type = field_type_MAX; + + if (((left->flags | right->flags) & MEM_Null) != 0) { + /* Force NULL be of type STRING. */ + result->field_type = FIELD_TYPE_STRING; + return 0; + } + + /* Concatenation operation can be applied only to strings and blobs. */ + if ((right->flags & (MEM_Str | MEM_Blob)) == 0) { + diag_set(ClientError, ER_INCONSISTENT_TYPES, + "text or varbinary", mem_type_to_str(right)); + return -1; + } + if ((left->flags & (MEM_Str | MEM_Blob)) == 0) { + diag_set(ClientError, ER_INCONSISTENT_TYPES, + "text or varbinary", mem_type_to_str(left)); + return -1; + } + + /* Moreover, both operands must be of the same type. */ + if ((right->flags & MEM_Str) != (left->flags & MEM_Str)) { + diag_set(ClientError, ER_INCONSISTENT_TYPES, + mem_type_to_str(left), mem_type_to_str(right)); + return -1; + } + + if (ExpandBlob(left) != 0 || ExpandBlob(right) != 0) + return -1; + + uint32_t size = left->n + right->n; + if ((int)size > sql_get()->aLimit[SQL_LIMIT_LENGTH]) { + diag_set(ClientError, ER_SQL_EXECUTE, "string or blob too big"); + return -1; + } + if (sqlVdbeMemGrow(result, size, result == left) != 0) + return -1; + + result->flags = left->flags & (MEM_Str | MEM_Blob); + if (result != left) + memcpy(result->z, left->z, left->n); + memcpy(&result->z[left->n], right->z, right->n); + result->n = size; + return 0; +} + static inline bool mem_has_msgpack_subtype(struct Mem *mem) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index aead71df2..9b0eb8444 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -185,6 +185,13 @@ mem_copy_as_ephemeral(struct Mem *to, const struct Mem *from); int mem_move(struct Mem *to, struct Mem *from); +/** + * Concatenate strings or binaries from the first and the second MEMs and write + * the result to the third MEM. + */ +int +mem_concat(struct Mem *left, struct Mem *right, struct Mem *result); + /* One or more of the following flags are set to indicate the validOK * representations of the value stored in the Mem struct. * diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index d76ba0b40..c04bd40f3 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -1028,59 +1028,11 @@ case OP_ResultRow: { * types (i.e. TEXT and BLOB). */ case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */ - i64 nByte; - pIn1 = &aMem[pOp->p1]; pIn2 = &aMem[pOp->p2]; - pOut = vdbe_prepare_null_out(p, pOp->p3); - assert(pIn1!=pOut); - if (mem_is_null(pIn1) || mem_is_null(pIn2)) { - /* Force NULL be of type STRING. */ - pOut->field_type = FIELD_TYPE_STRING; - break; - } - /* - * Concatenation operation can be applied only to - * strings and blobs. - */ - bool str_type_p1 = mem_is_varstring(pIn1); - bool str_type_p2 = mem_is_varstring(pIn2); - if (!str_type_p1 || !str_type_p2) { - char *inconsistent_type = !str_type_p1 ? - mem_type_to_str(pIn1) : - mem_type_to_str(pIn2); - diag_set(ClientError, ER_INCONSISTENT_TYPES, - "text or varbinary", inconsistent_type); - goto abort_due_to_error; - } - - /* Moreover, both operands must be of the same type. */ - if (mem_is_string(pIn1) != mem_is_string(pIn2)) { - diag_set(ClientError, ER_INCONSISTENT_TYPES, - mem_type_to_str(pIn2), mem_type_to_str(pIn1)); - goto abort_due_to_error; - } - if (ExpandBlob(pIn1) != 0 || ExpandBlob(pIn2) != 0) + pOut = &aMem[pOp->p3]; + if (mem_concat(pIn2, pIn1, pOut) != 0) goto abort_due_to_error; - nByte = pIn1->n + pIn2->n; - if (nByte>db->aLimit[SQL_LIMIT_LENGTH]) { - goto too_big; - } - if (sqlVdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2)) { - goto no_mem; - } - if (mem_is_string(pIn1)) - MemSetTypeFlag(pOut, MEM_Str); - else - MemSetTypeFlag(pOut, MEM_Blob); - if (pOut!=pIn2) { - memcpy(pOut->z, pIn2->z, pIn2->n); - } - memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n); - pOut->z[nByte]=0; - pOut->z[nByte+1] = 0; - pOut->flags |= MEM_Term; - pOut->n = (int)nByte; UPDATE_MAX_BLOBSIZE(pOut); break; } -- 2.25.1