[Tarantool-patches] [PATCH v4 33/53] sql: introduce mem_append_to_binary()

imeevma at tarantool.org imeevma at tarantool.org
Tue Mar 23 12:36:06 MSK 2021


This patch introduces mem_append_to_binary() function. Function
mem_append_to_binary() receives MEM with binary value and append to it
another binary value. MEM is updated accordingly.

Part of #5818
---
 src/box/sql/mem.c     | 16 ++++++++++++++++
 src/box/sql/mem.h     |  3 +++
 src/box/sql/vdbeaux.c | 23 +++++++++++++++--------
 3 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 63a4d0d3c..7885caaf5 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -492,6 +492,22 @@ mem_set_zerobinary(struct Mem *mem, int n)
 	mem->field_type = FIELD_TYPE_VARBINARY;
 }
 
+int
+mem_append_to_binary(struct Mem *mem, const char *value, uint32_t size)
+{
+	if ((mem->flags & MEM_Blob) == 0) {
+		diag_set(ClientError, ER_INCONSISTENT_TYPES, "varbinary",
+			 mem_type_to_str(mem));
+		return -1;
+	}
+	assert(mem->field_type == FIELD_TYPE_VARBINARY);
+	if (sqlVdbeMemGrow(mem, mem->n + size, 1) != 0)
+		return -1;
+	memcpy(&mem->z[mem->n], value, size);
+	mem->n += size;
+	return 0;
+}
+
 int
 mem_copy(struct Mem *to, const struct Mem *from)
 {
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 4b34bba6c..3bd04bbbd 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -235,6 +235,9 @@ mem_set_zerobinary(struct Mem *mem, int n);
 int
 mem_copy_binary(struct Mem *mem, const char *value, uint32_t size);
 
+int
+mem_append_to_binary(struct Mem *mem, const char *value, uint32_t size);
+
 int
 mem_copy(struct Mem *to, const struct Mem *from);
 
diff --git a/src/box/sql/vdbeaux.c b/src/box/sql/vdbeaux.c
index 86da1449c..2e8669138 100644
--- a/src/box/sql/vdbeaux.c
+++ b/src/box/sql/vdbeaux.c
@@ -1305,19 +1305,26 @@ sqlVdbeList(Vdbe * p)
 			 * has not already been seen.
 			 */
 			if (pOp->p4type == P4_SUBPROGRAM) {
-				int nByte = (nSub + 1) * sizeof(SubProgram *);
 				int j;
 				for (j = 0; j < nSub; j++) {
 					if (apSub[j] == pOp->p4.pProgram)
 						break;
 				}
-				if (j == nSub &&
-				    sqlVdbeMemGrow(pSub, nByte,
-						   nSub != 0) == 0) {
-					apSub = (SubProgram **) pSub->z;
-					apSub[nSub++] = pOp->p4.pProgram;
-					pSub->flags |= MEM_Blob;
-					pSub->n = nSub * sizeof(SubProgram *);
+				if (j == nSub) {
+					uint32_t size = sizeof(SubProgram *);
+					char *value = (char *)&pOp->p4.pProgram;
+					if (nSub == 0) {
+						if (mem_copy_binary(pSub, value,
+								    size) != 0)
+							return -1;
+					} else {
+						assert(0);
+						if (mem_append_to_binary(pSub,
+									 value,
+									 size) != 0)
+							return -1;
+					}
+					++nSub;
 				}
 			}
 		}
-- 
2.25.1



More information about the Tarantool-patches mailing list