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 B17C06EC40; Wed, 11 Aug 2021 19:03:13 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B17C06EC40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1628697793; 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=J+RQ6F+kQPwxCAW1YjdibnifIzUDkxokWqppVQVxqYjDmru5FWkZOIOEVV1MIFubV qsECaQKcbgP7ZFfsrOU1FTP3d4FWIAICjg3OUi6ywPJeOPywPg4ho8p3VWymdpVr38 W/ZtVmkPD6ms5TIrZ/JwxaRA0rvVeLecoE/R+7Bw= 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 D83AC6EC44 for ; Wed, 11 Aug 2021 19:01:44 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D83AC6EC44 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1mDqfo-0005hg-5z; Wed, 11 Aug 2021 19:01:44 +0300 To: v.shpilevoy@tarantool.org Cc: tarantool-patches@dev.tarantool.org Date: Wed, 11 Aug 2021 19:01:43 +0300 Message-Id: <4240278f094b296f9257b46161f40b8fae2ac213.1628697448.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-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD92087353F0EC44DD906AB4890CDABF0C5CB76CEE71D3E4007182A05F538085040C40C72C8CC4784FAAF90D42833855A90495EC891AF3B5C518AC524296B8B97F4 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7C42AF033AFE07300EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637160171C9EBC7AFE48638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8F30266DDBDC76E7792C712938F940DBB117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC292D688DDAD4E7BC389733CBF5DBD5E9C8A9BA7A39EFB766F5D81C698A659EA7CC7F00164DA146DA9985D098DBDEAEC8062BEEFFB5F8EA3EF6B57BC7E6449061A352F6E88A58FB86F5D81C698A659EA7E827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8BDC0F6C5B2EEF3D0C75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C6B668463F4F89F865684ADCE4E495E4263D0FC774F21F2529C2B6934AE262D3EE7EAB7254005DCED7532B743992DF240BDC6A1CF3F042BAD6DF99611D93F60EFA183FDCE24978B01699F904B3F4130E343918A1A30D5E7FCCB5012B2E24CD356 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34D4E96E2A5B1100E0CA4B626D1AFA1AB735089A99C93AE385292A057EB83C07A921180F5A49D0942C1D7E09C32AA3244CFB6F7C6B82227BB450994367C4542E7C8580396430872480729B2BEF169E0186 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2bioj6qlzQV0oSZOjtcjM3+4XIw== X-Mailru-Sender: 689FA8AB762F7393C37E3C1AEC41BA5D0D55938A5BF64F8A0190C38B4154305783D72C36FC87018B9F80AB2734326CD2FB559BB5D741EB96352A0ABBE4FDA4210A04DAD6CC59E33667EA787935ED9F1B 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