[tarantool-patches] [PATCH] sql: disallow specifying ID in <DEFAULT> clause

Nikita Pettik korablev at tarantool.org
Wed Sep 19 19:20:27 MSK 2018


Each column may feature default value, but this value must be constant.
Before this patch, it was allowed to do things like:

CREATE TABLE te14 (s1 INT PRIMARY KEY, s2 INT DEFAULT s1);

Which results in assertion fault on insertion. Lets prohibit IDs
(i.e. columns' names) in <DEFAULT> clause.

Closes #3695
---
Branch: https://github.com/tarantool/tarantool/tree/np/gh-3695-disallow-ID-be-default
Issue: https://github.com/tarantool/tarantool/issues/3695

 src/box/sql/parse.y           |  5 -----
 test/sql-tap/default.test.lua | 45 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/src/box/sql/parse.y b/src/box/sql/parse.y
index 60cf3f3e3..70bc026e3 100644
--- a/src/box/sql/parse.y
+++ b/src/box/sql/parse.y
@@ -264,11 +264,6 @@ ccons ::= DEFAULT MINUS(A) term(X).      {
   v.zEnd = X.zEnd;
   sqlite3AddDefaultValue(pParse,&v);
 }
-ccons ::= DEFAULT id(X).              {
-  ExprSpan v;
-  spanExpr(&v, pParse, TK_STRING, X);
-  sqlite3AddDefaultValue(pParse,&v);
-}
 
 // In addition to the type name, we also care about the primary key and
 // UNIQUE constraints.
diff --git a/test/sql-tap/default.test.lua b/test/sql-tap/default.test.lua
index 2f08a51ad..88bfa219f 100755
--- a/test/sql-tap/default.test.lua
+++ b/test/sql-tap/default.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 test = require("sqltester")
-test:plan(11)
+test:plan(15)
 
 --!./tcltestrunner.lua
 -- 2005 August 18
@@ -205,6 +205,49 @@ test:do_catchsql_test(
 	-- </default-4.4>
 })
 
+-- gh-3695: IDs (i.e. columns' names) are not allowed as
+-- default values.
+--
+test:do_catchsql_test(
+    "default-5.1",
+    [[
+        CREATE TABLE t6(id INTEGER PRIMARY KEY, b TEXT DEFAULT(id));
+    ]], {
+    -- <default-5.1>
+    1, "default value of column [B] is not constant"
+    -- </default-5.1>
+})
+
+test:do_catchsql_test(
+    "default-5.2",
+    [[
+        CREATE TABLE t6(id INTEGER PRIMARY KEY, b TEXT DEFAULT id);
+    ]], {
+    -- <default-5.2>
+    1, "near \"id\": syntax error"
+    -- </default-5.2>
+})
 
+test:do_catchsql_test(
+    "default-5.3",
+    [[
+        CREATE TABLE t6(id INTEGER PRIMARY KEY, b TEXT DEFAULT "id");
+    ]], {
+    -- <default-5.3>
+    1, "near \"\"id\"\": syntax error"
+    -- </default-5.3>
+})
+
+test:do_execsql_test(
+    "default-5.4",
+    [[
+        CREATE TABLE t6(id INTEGER PRIMARY KEY, b INT DEFAULT('id'));
+        INSERT INTO t6(id) VALUES(1);
+        SELECT * FROM t6;
+    ]], {
+    -- <default-5.4>
+    1, 'id'
+    -- </default-5.4>
+})
 
 test:finish_test()
-- 
2.15.1





More information about the Tarantool-patches mailing list