[tarantool-patches] [PATCH 5/6] sql: fix error message for improperly created index

Nikita Pettik korablev at tarantool.org
Wed Jan 9 15:13:19 MSK 2019


Table can be created without any indexes (for instance, from Lua-land).
On the other hand, bytecode generated for CREATE INDEX statement
attempts at finding entry in _index space with given space id.
In case it is not found error "wrong space id" is raised. On the other
hand, it is obvious that such message is appeared when table doesn't
have any created indexes yet. Hence, lets fix this message to indicate
that primary key should be created before any secondary indexes.

Closes #3914
---
 src/box/sql/build.c          |  4 ++--
 test/sql-tap/index1.test.lua | 28 +++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/src/box/sql/build.c b/src/box/sql/build.c
index ca9d469fd..1d7b6c827 100644
--- a/src/box/sql/build.c
+++ b/src/box/sql/build.c
@@ -2072,8 +2072,8 @@ generate_index_id(struct Parse *parse, uint32_t space_id, int cursor)
 	sqlite3VdbeJumpHere(v, seek_adr);
 	sqlite3VdbeJumpHere(v, seek_adr + 1);
 	sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_ERROR, ON_CONFLICT_ACTION_FAIL, 0,
-			  sqlite3MPrintf(parse->db, "Invalid space id: %d",
-					 space_id), P4_DYNAMIC);
+			  "can not add a secondary key before primary",
+			  P4_STATIC);
 
 	sqlite3VdbeJumpHere(v, goto_succ_addr);
 	/* Fetch iid from the row and increment it. */
diff --git a/test/sql-tap/index1.test.lua b/test/sql-tap/index1.test.lua
index 49f61a52a..ccb6753c9 100755
--- a/test/sql-tap/index1.test.lua
+++ b/test/sql-tap/index1.test.lua
@@ -1,6 +1,6 @@
 #!/usr/bin/env tarantool
 test = require("sqltester")
-test:plan(70)
+test:plan(73)
 
 --!./tcltestrunner.lua
 -- 2001 September 15
@@ -1016,5 +1016,31 @@ if (0 > 0)
 
 end
 
+test:do_test(
+    "index-22.1.0",
+    function()
+        format = {}
+        format[1] = { name = 'id', type = 'scalar'}
+        format[2] = { name = 'f2', type = 'scalar'}
+        s = box.schema.create_space('T', {format = format})
+    end,
+    {})
+
+test:do_catchsql_test(
+    "alter-8.1.1",
+    [[
+        CREATE UNIQUE INDEX pk ON t("id");
+    ]], {
+        1, "can not add a secondary key before primary"
+    })
+
+test:do_catchsql_test(
+    "alter-8.2",
+    [[
+        ALTER TABLE t ADD CONSTRAINT pk PRIMARY KEY("id");
+        CREATE UNIQUE INDEX i ON t("id");
+    ]], {
+        0
+})
 
 test:finish_test()
-- 
2.15.1





More information about the Tarantool-patches mailing list