Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: vdavydov@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 5/7] sql: disallow bitwise for NUMBER and SCALAR
Date: Fri, 13 Aug 2021 06:13:03 +0300	[thread overview]
Message-ID: <b9757d6d2f55965a29df72f1ab764ce4522b521b.1628824286.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1628824286.git.imeevma@gmail.com>

Part of #6221
---
 src/box/sql/mem.c               | 58 ++++++++++++---------------------
 test/sql-tap/metatypes.test.lua | 19 ++++++++++-
 2 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index e2c85e2ea..e34f24c96 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1759,6 +1759,22 @@ mem_rem(const struct Mem *left, const struct Mem *right, struct Mem *result)
 	return 0;
 }
 
+static inline int
+check_types_unsigned_bitwise(const struct Mem *a, const struct Mem *b)
+{
+	if (a->type != MEM_TYPE_UINT || mem_is_metatype(a)) {
+		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(a),
+			 "unsigned");
+		return -1;
+	}
+	if (b->type != MEM_TYPE_UINT || mem_is_metatype(b)) {
+		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(b),
+			 "unsigned");
+		return -1;
+	}
+	return 0;
+}
+
 int
 mem_bit_and(const struct Mem *left, const struct Mem *right, struct Mem *result)
 {
@@ -1766,16 +1782,8 @@ mem_bit_and(const struct Mem *left, const struct Mem *right, struct Mem *result)
 		mem_set_null(result);
 		return 0;
 	}
-	if (right->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
-			 "unsigned");
-		return -1;
-	}
-	if (left->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
-			 "unsigned");
+	if (check_types_unsigned_bitwise(right, left) != 0)
 		return -1;
-	}
 	mem_set_uint(result, left->u.u & right->u.u);
 	return 0;
 }
@@ -1787,16 +1795,8 @@ mem_bit_or(const struct Mem *left, const struct Mem *right, struct Mem *result)
 		mem_set_null(result);
 		return 0;
 	}
-	if (right->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
-			 "unsigned");
-		return -1;
-	}
-	if (left->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
-			 "unsigned");
+	if (check_types_unsigned_bitwise(right, left) != 0)
 		return -1;
-	}
 	mem_set_uint(result, left->u.u | right->u.u);
 	return 0;
 }
@@ -1809,16 +1809,8 @@ mem_shift_left(const struct Mem *left, const struct Mem *right,
 		mem_set_null(result);
 		return 0;
 	}
-	if (right->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
-			 "unsigned");
-		return -1;
-	}
-	if (left->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
-			 "unsigned");
+	if (check_types_unsigned_bitwise(right, left) != 0)
 		return -1;
-	}
 	mem_set_uint(result, right->u.u >= 64 ? 0 : left->u.u << right->u.u);
 	return 0;
 }
@@ -1831,16 +1823,8 @@ mem_shift_right(const struct Mem *left, const struct Mem *right,
 		mem_set_null(result);
 		return 0;
 	}
-	if (right->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(right),
-			 "unsigned");
-		return -1;
-	}
-	if (left->type != MEM_TYPE_UINT) {
-		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(left),
-			 "unsigned");
+	if (check_types_unsigned_bitwise(right, left) != 0)
 		return -1;
-	}
 	mem_set_uint(result, right->u.u >= 64 ? 0 : left->u.u >> right->u.u);
 	return 0;
 }
@@ -1852,7 +1836,7 @@ mem_bit_not(const struct Mem *mem, struct Mem *result)
 		mem_set_null(result);
 		return 0;
 	}
-	if (mem->type != MEM_TYPE_UINT) {
+	if (mem->type != MEM_TYPE_UINT || mem_is_metatype(mem)) {
 		diag_set(ClientError, ER_SQL_TYPE_MISMATCH, mem_str(mem),
 			 "unsigned");
 		return -1;
diff --git a/test/sql-tap/metatypes.test.lua b/test/sql-tap/metatypes.test.lua
index c0b2a4d03..50d700cc4 100755
--- a/test/sql-tap/metatypes.test.lua
+++ b/test/sql-tap/metatypes.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 local test = require("sqltester")
-test:plan(10)
+test:plan(12)
 
 -- Check that SCALAR and NUMBER meta-types works as intended.
 box.execute([[CREATE TABLE t (i INT PRIMARY KEY, s SCALAR, n NUMBER);]])
@@ -107,6 +107,23 @@ test:do_catchsql_test(
         1, "Type mismatch: can not convert scalar(1) to integer, unsigned or double"
     })
 
+-- Check that bitwise operations are prohibited for NUMBER and SCALAR values.
+test:do_catchsql_test(
+    "metatypes-4.1",
+    [[
+        SELECT 1 & CAST(1 AS NUMBER);
+    ]], {
+        1, "Type mismatch: can not convert number(1) to unsigned"
+    })
+
+test:do_catchsql_test(
+    "metatypes-4.2",
+    [[
+        SELECT CAST(1 AS SCALAR) >> 1;
+    ]], {
+        1, "Type mismatch: can not convert scalar(1) to unsigned"
+    })
+
 box.execute([[DROP TABLE t;]])
 
 test:finish_test()
-- 
2.25.1


  parent reply	other threads:[~2021-08-13  3:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-13  3:12 [Tarantool-patches] [PATCH v1 0/7] Rework SCALAR and NUMBER types in SQL Mergen Imeev via Tarantool-patches
2021-08-13  3:12 ` [Tarantool-patches] [PATCH v1 1/7] sql: remove enum field_type from struct Mem Mergen Imeev via Tarantool-patches
2021-08-13  3:12 ` [Tarantool-patches] [PATCH v1 2/7] sql: re-introduce NUMBER and SCALAR meta-types Mergen Imeev via Tarantool-patches
2021-08-13  3:12 ` [Tarantool-patches] [PATCH v1 3/7] sql: disallow implicit cast from NUMBER and SCALAR Mergen Imeev via Tarantool-patches
2021-08-13  3:13 ` [Tarantool-patches] [PATCH v1 4/7] sql: disallow arithmetic for " Mergen Imeev via Tarantool-patches
2021-08-13  3:13 ` Mergen Imeev via Tarantool-patches [this message]
2021-08-13  3:13 ` [Tarantool-patches] [PATCH v1 6/7] sql: disallow concatination for SCALAR Mergen Imeev via Tarantool-patches
2021-08-13  3:13 ` [Tarantool-patches] [PATCH v1 7/7] sql: fix comparison with SCALAR value Mergen Imeev via Tarantool-patches
2021-08-13 16:08 ` [Tarantool-patches] [PATCH v1 0/7] Rework SCALAR and NUMBER types in SQL Vladimir Davydov via Tarantool-patches
2021-08-17 11:30   ` Vitaliia Ioffe via Tarantool-patches
2021-08-18 12:29 ` Kirill Yukhin via Tarantool-patches
  -- strict thread matches above, loose matches on Subject: below --
2021-08-11 16:01 Mergen Imeev via Tarantool-patches
2021-08-11 16:01 ` [Tarantool-patches] [PATCH v1 5/7] sql: disallow bitwise for NUMBER and SCALAR Mergen Imeev via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b9757d6d2f55965a29df72f1ab764ce4522b521b.1628824286.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 5/7] sql: disallow bitwise for NUMBER and SCALAR' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox