From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id A48432A93D for ; Fri, 24 Aug 2018 06:57:14 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id DNCzjohctGFD for ; Fri, 24 Aug 2018 06:57:14 -0400 (EDT) Received: from smtp44.i.mail.ru (smtp44.i.mail.ru [94.100.177.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 6294C2A93C for ; Fri, 24 Aug 2018 06:57:14 -0400 (EDT) From: imeevma@tarantool.org Subject: [tarantool-patches] [PATCH v3 1/2] sql: move autoincrement in vdbe Date: Fri, 24 Aug 2018 13:57:10 +0300 Message-Id: <2ab414e943955a89905f4f7919406d74d348a078.1535107514.git.imeevma@gmail.com> In-Reply-To: References: Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org, korablev@tarantool.org Cc: v.shpilevoy@tarantool.org This patch expands changes made in issue #2981. Now NULL in primary key always replaced by generated value inside of vdbe. It allows us to get last_insert_id with easy. Part of #2618 --- src/box/sql/insert.c | 13 +++++++++---- src/box/sql/vdbe.c | 6 +++++- test/sql/gh-2981-check-autoinc.result | 13 +++++++++++++ test/sql/gh-2981-check-autoinc.test.lua | 6 +++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c index 0e92f62..6afa0a2 100644 --- a/src/box/sql/insert.c +++ b/src/box/sql/insert.c @@ -739,9 +739,7 @@ sqlite3Insert(Parse * pParse, /* Parser context */ if (j < 0 || nColumn == 0 || (pColumn && j >= pColumn->nId)) { if (i == pTab->iAutoIncPKey) { - sqlite3VdbeAddOp2(v, - OP_NextAutoincValue, - pTab->def->id, + sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore); continue; } @@ -824,7 +822,14 @@ sqlite3Insert(Parse * pParse, /* Parser context */ iRegStore); } } - + /* + * Replace NULL in PK with autoincrement by + * generated value. + */ + if (pTab->iAutoIncPKey >= 0) { + sqlite3VdbeAddOp2(v, OP_NextAutoincValue, pTab->def->id, + regData + pTab->iAutoIncPKey); + } /* Generate code to check constraints and generate index keys and do the insertion. */ diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index dc3f5c0..c6ece15 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -1157,6 +1157,10 @@ case OP_NextAutoincValue: { assert(pOp->p1 > 0); assert(pOp->p2 > 0); + pIn2 = &p->aMem[pOp->p2]; + if ((pIn2->flags & MEM_Null) == 0) + break; + struct space *space = space_by_id(pOp->p1); if (space == NULL) { rc = SQL_TARANTOOL_ERROR; @@ -3755,7 +3759,7 @@ case OP_FCopy: { /* out2 */ if ((pOp->p3 & OPFLAG_NOOP_IF_NULL) && (pIn1->flags & MEM_Null)) { pOut = &aMem[pOp->p2]; - if (pOut->flags & MEM_Undefined) pOut->flags = MEM_Null; + pOut->flags = MEM_Null; /* Flag is set and register is NULL -> do nothing */ } else { assert(memIsValid(pIn1)); diff --git a/test/sql/gh-2981-check-autoinc.result b/test/sql/gh-2981-check-autoinc.result index b0f55e6..43bd42b 100644 --- a/test/sql/gh-2981-check-autoinc.result +++ b/test/sql/gh-2981-check-autoinc.result @@ -19,6 +19,9 @@ box.sql.execute("CREATE TABLE t2 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, s2 INTEG box.sql.execute("CREATE TABLE t3 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, s2 INTEGER, CHECK (s1 < 10));"); --- ... +box.sql.execute("CREATE TABLE t4 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, s2 INTEGER, CHECK (s1 <> 19));"); +--- +... box.sql.execute("insert into t1 values (18, null);") --- ... @@ -47,6 +50,13 @@ box.sql.execute("insert into t3(s2) values (null)") --- - error: 'CHECK constraint failed: T3' ... +box.sql.execute("insert into t4 values (18, null);") +--- +... +box.sql.execute("insert into t4 values (null, null);") +--- +- error: 'CHECK constraint failed: T4' +... box.sql.execute("DROP TABLE t1") --- ... @@ -56,3 +66,6 @@ box.sql.execute("DROP TABLE t2") box.sql.execute("DROP TABLE t3") --- ... +box.sql.execute("DROP TABLE t4") +--- +... diff --git a/test/sql/gh-2981-check-autoinc.test.lua b/test/sql/gh-2981-check-autoinc.test.lua index 98a5fb4..593ff3a 100644 --- a/test/sql/gh-2981-check-autoinc.test.lua +++ b/test/sql/gh-2981-check-autoinc.test.lua @@ -7,6 +7,7 @@ box.cfg{} box.sql.execute("CREATE TABLE t1 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, s2 INTEGER, CHECK (s1 <> 19));"); box.sql.execute("CREATE TABLE t2 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, s2 INTEGER, CHECK (s1 <> 19 AND s1 <> 25));"); box.sql.execute("CREATE TABLE t3 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, s2 INTEGER, CHECK (s1 < 10));"); +box.sql.execute("CREATE TABLE t4 (s1 INTEGER PRIMARY KEY AUTOINCREMENT, s2 INTEGER, CHECK (s1 <> 19));"); box.sql.execute("insert into t1 values (18, null);") box.sql.execute("insert into t1(s2) values (null);") @@ -19,7 +20,10 @@ box.sql.execute("insert into t2(s2) values (null);") box.sql.execute("insert into t3 values (9, null)") box.sql.execute("insert into t3(s2) values (null)") +box.sql.execute("insert into t4 values (18, null);") +box.sql.execute("insert into t4 values (null, null);") + box.sql.execute("DROP TABLE t1") box.sql.execute("DROP TABLE t2") box.sql.execute("DROP TABLE t3") - +box.sql.execute("DROP TABLE t4") -- 2.7.4