Tarantool development patches archive
 help / color / mirror / Atom feed
From: imeevma@tarantool.org
To: tarantool-patches@freelists.org, korablev@tarantool.org
Cc: v.shpilevoy@tarantool.org
Subject: [tarantool-patches] [PATCH v3 1/2] sql: move autoincrement in vdbe
Date: Fri, 24 Aug 2018 13:57:10 +0300	[thread overview]
Message-ID: <2ab414e943955a89905f4f7919406d74d348a078.1535107514.git.imeevma@gmail.com> (raw)
In-Reply-To: <cover.1535107514.git.imeevma@gmail.com>

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

  reply	other threads:[~2018-08-24 10:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-24 10:57 [tarantool-patches] [PATCH v3 0/2] sql: return last_insert_id via IPROTO imeevma
2018-08-24 10:57 ` imeevma [this message]
2018-08-27 14:27   ` [tarantool-patches] Re: [PATCH v3 1/2] sql: move autoincrement in vdbe n.pettik
2018-08-24 10:57 ` [tarantool-patches] [PATCH v3 2/2] sql: return last_insert_id via IPROTO imeevma
2018-08-27 14:25   ` [tarantool-patches] " n.pettik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2ab414e943955a89905f4f7919406d74d348a078.1535107514.git.imeevma@gmail.com \
    --to=imeevma@tarantool.org \
    --cc=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --cc=v.shpilevoy@tarantool.org \
    --subject='Re: [tarantool-patches] [PATCH v3 1/2] sql: move autoincrement in vdbe' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox