From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 2E6F36EC40; Fri, 13 Aug 2021 06:14:25 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2E6F36EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628824465; bh=gddKu1MLIBpmwcYyO6LcOsl3ZTCryPUjko79mftM5PM=; h=To:Cc:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=vwQ7wl6qQPP7GyCT2pGDcjHkIgl9lSz5RPAxSMRaar6dVX0fDXazQXKxwNqaEWiwO JIEbdE9fvae0QAir0dJEXdKWJtUwyHz/LZKnirKH9oP8pMA9fIUMd5J2anwdk1jnSw lH9xrm0dOEf33lCKXTe8qRXadYPGtkibeClbuXrw= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A685A6EC40 for ; Fri, 13 Aug 2021 06:13:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A685A6EC40 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mENcx-0000ZL-QH; Fri, 13 Aug 2021 06:13:00 +0300 To: vdavydov@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Fri, 13 Aug 2021 06:12:59 +0300 Message-Id: <6764405bd84a3a57817d4cd32c7a26c738d88e53.1628824286.git.imeevma@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD91BCCB18F2C129F87F36E61E9E4584E9D182A05F53808504074059E3B80D874EBF6C61743B66378D1C4F66F5BDB4F2F8FAC41A73F4A09A0CD X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7C42AF033AFE07300EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637160171C9EBC7AFE48638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D846E75F64E6735E3CE3EA87690FE21984117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC292D688DDAD4E7BC389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8062BEEFFB5F8EA3EF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A213B5FB47DCBC3458834459D11680B5059A91936EF7B6732DE469006DEA42ADD7 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C69415AB31670C86C146196E981AA0534DAA37CF3CFFFE2909C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFE37876E7723AB534DC48ACC2A39D04F89CDFB48F4795C241BDAD6C7F3747799A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D4E96E2A5B1100E0CA4B626D1AFA1AB735089A99C93AE385292A057EB83C07A921180F5A49D0942C1D7E09C32AA3244CD7694A450645CEC22CD10A7E69B145803A92A9747B6CC886729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj0dLV0c3jbkxWxu6c2L4Pew== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D40448D5AD75BAAAE9D815939099984E983D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 3/7] sql: disallow implicit cast from NUMBER and SCALAR X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Mergen Imeev via Tarantool-patches Reply-To: imeevma@tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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" -- }) 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