[Tarantool-patches] [PATCH v1 3/7] sql: disallow implicit cast from NUMBER and SCALAR
imeevma at tarantool.org
imeevma at tarantool.org
Wed Aug 11 19:01:43 MSK 2021
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
More information about the Tarantool-patches
mailing list