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 3567468F42; Tue, 23 Mar 2021 12:42:17 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3567468F42 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1616492537; bh=GOB6icx1+iUhsQK8HenHytnPtzgfqNUXz8TNG11pm7A=; 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=Us4AQJg6HgM7FSgQE/9SONcmPY8eKkMZqWX5bS4R4OUbWILeOLgckHFNUOGZIIk5M ykmLDJXDk4BYxrpbb1KJ04alXb8GDzEC1uuPM5dZc7abu0qb7IjE0vatRvrs+hLsZ7 n1bQSM6w8CknC6tgHaamaJNKbh2zeqXmDMc2LNog= Received: from smtp39.i.mail.ru (smtp39.i.mail.ru [94.100.177.99]) (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 3ACD668F43 for ; Tue, 23 Mar 2021 12:35:27 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 3ACD668F43 Received: by smtp39.i.mail.ru with esmtpa (envelope-from ) id 1lOdRe-0004RA-IK; Tue, 23 Mar 2021 12:35:26 +0300 To: v.shpilevoy@tarantool.org, tsafin@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Tue, 23 Mar 2021 12:35:26 +0300 Message-Id: <0baa01ddd89e572df674bdb920d0616a4d465eda.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: 4F1203BC0FB41BD95D6E7CC48CB1F5F145AC670795810553045AFCF1215FE694182A05F5380850408B55AABF4B4D144D526DEAA77FC0BB108467AD310AB96CF2452E5F347D09D5C7 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE75644E22E05AA81AEB287FD4696A6DC2FA8DF7F3B2552694A4E2F5AFA99E116B42401471946AA11AF23F8577A6DFFEA7C364F8074C6DFACE28F08D7030A58E5ADC58D69EE07B14084C6CDE5D1141D2B1CF5450AD3DA2BBC42AAEE8F2990372E3100C14276A7588F8D9FA2833FD35BB23D9E625A9149C048EE9ECD01F8117BC8BEA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD18CB629EEF1311BF91D2E47CDBA5A96583BA9C0B312567BB2376E601842F6C81A19E625A9149C048EE4B6963042765DA4B1AB2475877E8919AD8FC6C240DEA7642DBF02ECDB25306B2B78CF848AE20165D0A6AB1C7CE11FEE3D22D25B442375FF1C0837EA9F3D19764C4224003CC836476EA7A3FFF5B025636E2021AF6380DFAD1A18204E546F3947CB11811A4A51E3B096D1867E19FE14079BD4B6F7A4D31EC0BEC64975D915A344093EC92FD9297F6718AA50765F7900637B17452A121C296D4A7F4EDE966BC389F395957E7521B51C24C7702A67D5C33162DBA43225CD8A89FF7E713A9F722088E5E1C53F199C2BB95B5C8C57E37DE458B4C7702A67D5C3316FA3894348FB808DBA1CE242F1348D5363B503F486389A921A5CC5B56E945C8DA X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24A6D60772A99906F8E1CD14B953EB46DCC2DEBD921E31D0F355D89D7DBCDD132 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C686AF563A045C75E7F7CC82C8F13B94F6ED420248EB908549C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EF0417BEADF48D1460699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D349FF8F8245A2FAA7B5451EBC526505F5947C3134EFDF6C12222C9EE0265C344B5A6113AF830646FD71D7E09C32AA3244C3FD0B78C06D019ABBE9F034B9CC632E855E75C8D0ED9F6EEFACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojWJZv20R+6Ujl6c2UHIUoMA== X-Mailru-Sender: 5C3750E245F362008BC1685FEC6306ED13E25DC4FB7F04CC526DEAA77FC0BB10BDDB26EEA8DAF3A65105BD0848736F9966FEC6BF5C9C28D97E07721503EA2E00ED97202A5A4E92BF7402F9BA4338D657ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v4 15/53] sql: rework vdbe_decode_msgpack_into_mem() 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" The original vdbe_decode_msgpack_into_mem() returns a MEM that contains string and binary values as ephemeral. This patch renames this function to vdbe_decode_msgpack_into_mem_ephemeral() and introduces new vdbe_decode_msgpack_into_mem(), which returns a MEM that contains string and binary values in newly allocated memory. This patch actually changes behavior in this case: CREATE TABLE t1(m VARBINARY primary key); INSERT INTO t1 VALUES(x'6178'), (x'6278'), (x'6379'); SELECT count(*), substr(m,2,1) AS m FROM t1 GROUP BY m; SELECT count(*), substr(m,2,1) AS mx FROM t1 GROUP BY mx; But it doesn't change behaviour for this: CREATE TABLE t2(m STRING primary key); INSERT INTO t2 VALUES('ax'), ('bx'), ('cy'); SELECT count(*), substr(m,2,1) AS m FROM t2 GROUP BY m; SELECT count(*), substr(m,2,1) AS mx FROM t2 GROUP BY mx; Part of #5818 Part of #5890 --- src/box/sql/mem.c | 16 +++++++++++++++- src/box/sql/mem.h | 17 ++++++++++++++++- src/box/sql/vdbe.c | 18 ------------------ src/box/sql/vdbeaux.c | 2 +- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c index 3d42ac63c..a2316cc90 100644 --- a/src/box/sql/mem.c +++ b/src/box/sql/mem.c @@ -2253,7 +2253,8 @@ sqlVdbeRecordCompareMsgpack(const void *key1, } int -vdbe_decode_msgpack_into_mem(const char *buf, struct Mem *mem, uint32_t *len) +vdbe_decode_msgpack_into_ephemeral_mem(const char *buf, struct Mem *mem, + uint32_t *len) { const char *start_buf = buf; switch (mp_typeof(*buf)) { @@ -2354,6 +2355,19 @@ install_blob: return 0; } +int +vdbe_decode_msgpack_into_mem(const char *buf, struct Mem *mem, uint32_t *len) +{ + if (vdbe_decode_msgpack_into_ephemeral_mem(buf, mem, len) != 0) + return -1; + if ((mem->flags & (MEM_Str | MEM_Blob)) != 0) { + assert((mem->flags & MEM_Ephem) != 0); + if (sqlVdbeMemGrow(mem, mem->n, 1) != 0) + return -1; + } + return 0; +} + void mpstream_encode_vdbe_mem(struct mpstream *stream, struct Mem *var) { diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h index 801f3cba6..5edb0d80a 100644 --- a/src/box/sql/mem.h +++ b/src/box/sql/mem.h @@ -538,7 +538,22 @@ int sqlVdbeRecordCompareMsgpack(const void *key1, struct UnpackedRecord *key2); /** - * Decode msgpack and save value into VDBE memory cell. + * Decode msgpack and save value into VDBE memory cell. String and binary string + * values set as ephemeral. + * + * @param buf Buffer to deserialize msgpack from. + * @param mem Memory cell to write value into. + * @param len[out] Length of decoded part. + * @retval Return code: < 0 in case of error. + * @retval 0 on success. + */ +int +vdbe_decode_msgpack_into_ephemeral_mem(const char *buf, struct Mem *mem, + uint32_t *len); + +/** + * Decode msgpack and save value into VDBE memory cell. String and binary string + * values copied to newly allocated memory. * * @param buf Buffer to deserialize msgpack from. * @param mem Memory cell to write value into. diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 4654b1610..d76ba0b40 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -375,24 +375,6 @@ vdbe_field_ref_fetch(struct vdbe_field_ref *field_ref, uint32_t fieldno, uint32_t dummy; if (vdbe_decode_msgpack_into_mem(data, dest_mem, &dummy) != 0) return -1; - - /* - * Add 0 termination (at most for strings) - * Not sure why do we check MEM_Ephem - */ - if (mem_is_string(dest_mem) && mem_is_ephemeral(dest_mem)) { - int len = dest_mem->n; - if (dest_mem->szMalloc < len + 1) { - if (sqlVdbeMemGrow(dest_mem, len + 1, 1) != 0) - return -1; - } else { - dest_mem->z = - memcpy(dest_mem->zMalloc, dest_mem->z, len); - dest_mem->flags &= ~MEM_Ephem; - } - dest_mem->z[len] = 0; - dest_mem->flags |= MEM_Term; - } UPDATE_MAX_BLOBSIZE(dest_mem); return 0; } diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c index 21ac84099..e58526401 100644 --- a/src/box/sql/vdbeaux.c +++ b/src/box/sql/vdbeaux.c @@ -2334,7 +2334,7 @@ sqlVdbeRecordUnpackMsgpack(struct key_def *key_def, /* Information about the rec pMem->szMalloc = 0; pMem->z = 0; uint32_t sz = 0; - vdbe_decode_msgpack_into_mem(zParse, pMem, &sz); + vdbe_decode_msgpack_into_ephemeral_mem(zParse, pMem, &sz); assert(sz != 0); zParse += sz; pMem++; -- 2.25.1