[Tarantool-patches] [PATCH v4 21/53] sql: introduce mem_bit_not()

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


This patch introduces mem_bit_not(). Function mem_bit_not() executes
bitwise not operation on the first MEM and writes the result to the
second MEM.

Part of #5818
---
 src/box/sql/mem.c  | 19 +++++++++++++++++++
 src/box/sql/mem.h  |  3 +++
 src/box/sql/vdbe.c | 14 ++------------
 3 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 2b455e39f..c84b3775d 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -607,6 +607,25 @@ mem_bitwise(struct Mem *left, struct Mem *right, struct Mem *result, int op)
 	return 0;
 }
 
+int
+mem_bit_not(struct Mem *mem, struct Mem *result)
+{
+	mem_clear(result);
+	result->field_type = FIELD_TYPE_INTEGER;
+	if ((mem->flags & MEM_Null) != 0)
+		return 0;
+	int64_t i;
+	bool unused;
+	if (sqlVdbeIntValue(mem, &i, &unused) != 0) {
+		diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
+			 mem_str(mem), "integer");
+		return -1;
+	}
+	i = ~i;
+	mem_set_int(result, i, i < 0);
+	return 0;
+}
+
 static int
 compare_blobs(const struct Mem *left, const struct Mem *right, int *result)
 {
diff --git a/src/box/sql/mem.h b/src/box/sql/mem.h
index 8971e3d59..3561b49b1 100644
--- a/src/box/sql/mem.h
+++ b/src/box/sql/mem.h
@@ -199,6 +199,9 @@ mem_arithmetic(const struct Mem *left, const struct Mem *right,
 int
 mem_bitwise(struct Mem *left, struct Mem *right, struct Mem *result, int op);
 
+int
+mem_bit_not(struct Mem *mem, struct Mem *result);
+
 int
 mem_compare(const struct Mem *left, const struct Mem *right, int *result,
 	    enum field_type type, struct coll *coll);
diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index cdcf7b2e8..80497b7b2 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -1792,18 +1792,8 @@ case OP_Not: {                /* same as TK_NOT, in1, out2 */
 case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
 	pIn1 = &aMem[pOp->p1];
 	pOut = vdbe_prepare_null_out(p, pOp->p2);
-	/* Force NULL be of type INTEGER. */
-	pOut->field_type = FIELD_TYPE_INTEGER;
-	if (!mem_is_null(pIn1)) {
-		int64_t i;
-		bool is_neg;
-		if (sqlVdbeIntValue(pIn1, &i, &is_neg) != 0) {
-			diag_set(ClientError, ER_SQL_TYPE_MISMATCH,
-				 mem_str(pIn1), "integer");
-			goto abort_due_to_error;
-		}
-		mem_set_i64(pOut, ~i);
-	}
+	if (mem_bit_not(pIn1, pOut) != 0)
+		goto abort_due_to_error;
 	break;
 }
 
-- 
2.25.1



More information about the Tarantool-patches mailing list