[Tarantool-patches] [PATCH v4 15/53] sql: rework vdbe_decode_msgpack_into_mem()

imeevma at tarantool.org imeevma at tarantool.org
Tue Mar 23 12:35:26 MSK 2021


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



More information about the Tarantool-patches mailing list