Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: v.shpilevoy@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v1 3/7] sql: disallow implicit cast from NUMBER and SCALAR
Date: Wed, 11 Aug 2021 19:01:43 +0300	[thread overview]
Message-ID: <4240278f094b296f9257b46161f40b8fae2ac213.1628697448.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1628697448.git.imeevma@gmail.com>

This patch disallows implicit cast during INSERT and UPDATE from SCALAR
to any other scalar types and from NUMBER to any other numeric types.

Part of #6221
---
 src/box/sql/mem.c                    |  5 +++
 test/sql-tap/metatypes.test.lua      | 46 +++++++++++++++++++++++++++-
 test/sql-tap/tkt-7bbfb7d442.test.lua |  4 +--
 test/sql-tap/trigger9.test.lua       |  2 +-
 4 files changed, 53 insertions(+), 4 deletions(-)

diff --git a/src/box/sql/mem.c b/src/box/sql/mem.c
index 41aa40fdb..8468c0030 100644
--- a/src/box/sql/mem.c
+++ b/src/box/sql/mem.c
@@ -1218,6 +1218,11 @@ mem_cast_implicit(struct Mem *mem, enum field_type type)
 {
 	if (mem->type == MEM_TYPE_NULL)
 		return 0;
+	if ((mem->flags & MEM_Scalar) != 0 && type != FIELD_TYPE_SCALAR)
+		return -1;
+	if ((mem->flags & MEM_Number) != 0 && type != FIELD_TYPE_SCALAR &&
+	    type != FIELD_TYPE_NUMBER)
+		return -1;
 	switch (type) {
 	case FIELD_TYPE_UNSIGNED:
 		if (mem->type == MEM_TYPE_UINT) {
diff --git a/test/sql-tap/metatypes.test.lua b/test/sql-tap/metatypes.test.lua
index 89802ec09..2748ab781 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(3)
+test:plan(8)
 
 -- Check that SCALAR and NUMBER meta-types works as intended.
 box.execute([[CREATE TABLE t (i INT PRIMARY KEY, s SCALAR, n NUMBER);]])
@@ -46,6 +46,50 @@ test:do_execsql_test(
         "number","number","NULL","NULL","NULL","NULL"
     })
 
+--
+-- Check that implicit cast from NUMBER to numeric types and from SCALAR to
+-- scalar types is prohibited.
+--
+test:do_catchsql_test(
+    "metatypes-2.1",
+    [[
+        INSERT INTO t(i) VALUES(CAST(7 AS SCALAR));
+    ]], {
+        1, "Type mismatch: can not convert scalar(7) to integer"
+    })
+
+test:do_catchsql_test(
+    "metatypes-2.2",
+    [[
+        INSERT INTO t(i, n) VALUES(8, CAST(1.5 AS SCALAR));
+    ]], {
+        1, "Type mismatch: can not convert scalar(1.5) to number"
+    })
+
+test:do_catchsql_test(
+    "metatypes-2.3",
+    [[
+        INSERT INTO t(i) VALUES(CAST(9 AS NUMBER));
+    ]], {
+        1, "Type mismatch: can not convert number(9) to integer"
+    })
+
+test:do_catchsql_test(
+    "metatypes-2.4",
+    [[
+        UPDATE t SET i = CAST(10 AS SCALAR);
+    ]], {
+        1, "Type mismatch: can not convert scalar(10) to integer"
+    })
+
+test:do_catchsql_test(
+    "metatypes-2.5",
+    [[
+        UPDATE t SET i = CAST(11 AS NUMBER);
+    ]], {
+        1, "Type mismatch: can not convert number(11) to integer"
+    })
+
 box.execute([[DROP TABLE t;]])
 
 test:finish_test()
diff --git a/test/sql-tap/tkt-7bbfb7d442.test.lua b/test/sql-tap/tkt-7bbfb7d442.test.lua
index 448f884d8..71caad0ea 100755
--- a/test/sql-tap/tkt-7bbfb7d442.test.lua
+++ b/test/sql-tap/tkt-7bbfb7d442.test.lua
@@ -67,14 +67,14 @@ if (1 > 0)
             DELETE FROM t3
         ]])
 
-    test:do_execsql_test(
+    test:do_catchsql_test(
         1.4,
         [[
             INSERT INTO t3(t3_a) SELECT 1 UNION SELECT 2 UNION SELECT 3;
             SELECT * FROM t3;
         ]], {
             -- <1.4>
-            1, "I", 2, "II", 3, "III"
+            1, "Type mismatch: can not convert scalar(1) to integer"
             -- </1.4>
         })
 
diff --git a/test/sql-tap/trigger9.test.lua b/test/sql-tap/trigger9.test.lua
index 89b5b89ef..855557e4a 100755
--- a/test/sql-tap/trigger9.test.lua
+++ b/test/sql-tap/trigger9.test.lua
@@ -390,7 +390,7 @@ test:do_execsql_test(
     "trigger9-3.6",
     [[
         CREATE VIEW v1 AS
-          SELECT sum(a) AS a, max(b) AS b FROM t3 GROUP BY t3.a HAVING b>'two';
+          SELECT CAST(sum(a) AS INTEGER) AS a, max(b) AS b FROM t3 GROUP BY t3.a HAVING b>'two';
         DROP TRIGGER IF EXISTS trig1;
         CREATE TRIGGER trig1 INSTEAD OF UPDATE ON v1 FOR EACH ROW BEGIN
           INSERT INTO t2 VALUES(old.a);
-- 
2.25.1


  parent reply	other threads:[~2021-08-11 16:03 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-11 16:01 [Tarantool-patches] [PATCH v1 0/7] Rework SCALAR and NUMBER types in SQL Mergen Imeev via Tarantool-patches
2021-08-11 16:01 ` [Tarantool-patches] [PATCH v1 1/7] sql: remove enum field_type from struct Mem Mergen Imeev via Tarantool-patches
2021-08-11 16:01 ` [Tarantool-patches] [PATCH v1 2/7] sql: re-introduce NUMBER and SCALAR meta-types Mergen Imeev via Tarantool-patches
2021-08-12 18:51   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-12 22:22     ` Mergen Imeev via Tarantool-patches
2021-08-11 16:01 ` Mergen Imeev via Tarantool-patches [this message]
2021-08-11 16:01 ` [Tarantool-patches] [PATCH v1 4/7] sql: disallow arithmetic for NUMBER and SCALAR Mergen Imeev via Tarantool-patches
2021-08-11 16:01 ` [Tarantool-patches] [PATCH v1 5/7] sql: disallow bitwise " Mergen Imeev via Tarantool-patches
2021-08-11 16:01 ` [Tarantool-patches] [PATCH v1 6/7] sql: disallow concatination for SCALAR Mergen Imeev via Tarantool-patches
2021-08-11 16:01 ` [Tarantool-patches] [PATCH v1 7/7] sql: fix comparison with SCALAR value Mergen Imeev via Tarantool-patches
2021-08-12 18:50   ` Vladislav Shpilevoy via Tarantool-patches
2021-08-12 22:23     ` Mergen Imeev via Tarantool-patches
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 3/7] sql: disallow implicit cast from 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=4240278f094b296f9257b46161f40b8fae2ac213.1628697448.git.imeevma@gmail.com \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imeevma@tarantool.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v1 3/7] sql: disallow implicit cast from 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