From 1e509a03037e06cb57c96dd95ce703d5047ae170 Mon Sep 17 00:00:00 2001
From: Mergen Imeev <imeevma@gmail.com>
Date: Wed, 17 Jul 2019 12:26:04 +0300
Subject: [PATCH] sql: do not increase row-count if INSERT OR IGNORE fails

If INSERT statement is executed with IGNORE error action (i.e.
INSERT OR IGNORE ...), it will return number of rows inserted.
For example:

CREATE TABLE t (i INT PRIMARY KEY, a INT check (a > 0));
INSERT OR IGNORE INTO t VALUES (1, 1), (2, -1), (3, 2);

Should return:
---
- row_count: 2
...

However it was three before this patch. So, let's account number
of successful insertions in case of INSERT OR IGNORE.

Follow-up #4188
---
 src/box/sql/vdbe.c          | 40 +++++++++++++++++++++----------------
 test/sql/row-count.result   | 30 ++++++++++++++++++++++++++++
 test/sql/row-count.test.lua |  8 ++++++++
 3 files changed, 61 insertions(+), 17 deletions(-)

diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 45676e8e06..c0948693c4 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -4255,8 +4255,6 @@ case OP_IdxReplace:
 case OP_IdxInsert: {
 	pIn2 = &aMem[pOp->p1];
 	assert((pIn2->flags & MEM_Blob) != 0);
-	if (pOp->p5 & OPFLAG_NCHANGE)
-		p->nChange++;
 	if (ExpandBlob(pIn2) != 0)
 		goto abort_due_to_error;
 	struct space *space;
@@ -4279,26 +4277,34 @@ case OP_IdxInsert: {
 		rc = tarantoolsqlEphemeralInsert(space, pIn2->z,
 						     pIn2->z + pIn2->n);
 	}
-	if (rc == 0 && pOp->p3 > 0 && ((aMem[pOp->p3].flags) & MEM_Null) != 0) {
+	if (rc != 0) {
+		if ((pOp->p5 & OPFLAG_OE_IGNORE) != 0) {
+			/*
+			 * Ignore any kind of fails and do not
+			 * raise error message. If we are in
+			 * trigger, increment ignore raised
+			 * counter.
+			 */
+			rc = 0;
+			if (p->pFrame != NULL)
+				p->ignoreRaised++;
+			break;
+		}
+		if ((pOp->p5 & OPFLAG_OE_FAIL) != 0) {
+			p->errorAction = ON_CONFLICT_ACTION_FAIL;
+		} else if ((pOp->p5 & OPFLAG_OE_ROLLBACK) != 0) {
+			p->errorAction = ON_CONFLICT_ACTION_ROLLBACK;
+		}
+		goto abort_due_to_error;
+	}
+	if ((pOp->p5 & OPFLAG_NCHANGE) != 0)
+		p->nChange++;
+	if (pOp->p3 > 0 && ((aMem[pOp->p3].flags) & MEM_Null) != 0) {
 		assert(space->sequence != NULL);
 		int64_t value = sequence_get_value(space->sequence);
 		if (vdbe_add_new_autoinc_id(p, value) != 0)
 			goto abort_due_to_error;
 	}
-
-	if (pOp->p5 & OPFLAG_OE_IGNORE) {
-		/* Ignore any kind of failes and do not raise error message */
-		rc = 0;
-		/* If we are in trigger, increment ignore raised counter */
-		if (p->pFrame)
-			p->ignoreRaised++;
-	} else if (pOp->p5 & OPFLAG_OE_FAIL) {
-		p->errorAction = ON_CONFLICT_ACTION_FAIL;
-	} else if (pOp->p5 & OPFLAG_OE_ROLLBACK) {
-		p->errorAction = ON_CONFLICT_ACTION_ROLLBACK;
-	}
-	if (rc != 0)
-		goto abort_due_to_error;
 	break;
 }
 
diff --git a/test/sql/row-count.result b/test/sql/row-count.result
index e7841caa17..94ebaf0a87 100644
--- a/test/sql/row-count.result
+++ b/test/sql/row-count.result
@@ -335,3 +335,33 @@ box.execute("DROP TABLE t1;")
 ---
 - row_count: 1
 ...
+--
+-- gh-4188: make sure that in case of INSERT OR IGNORE only
+-- successful inserts are counted.
+--
+box.execute("CREATE TABLE t (i INT PRIMARY KEY AUTOINCREMENT, a INT check (a > 0));")
+---
+- row_count: 1
+...
+box.execute("INSERT OR IGNORE INTO t VALUES (null, 1), (null, -1), (null, 2);")
+---
+- autoincrement_ids:
+  - 1
+  - 3
+  row_count: 2
+...
+box.execute("SELECT * FROM t;")
+---
+- metadata:
+  - name: I
+    type: integer
+  - name: A
+    type: integer
+  rows:
+  - [1, 1]
+  - [3, 2]
+...
+box.execute("DROP TABLE t;")
+---
+- row_count: 1
+...
diff --git a/test/sql/row-count.test.lua b/test/sql/row-count.test.lua
index 9f5215c939..af7f1680c9 100644
--- a/test/sql/row-count.test.lua
+++ b/test/sql/row-count.test.lua
@@ -73,3 +73,11 @@ box.execute("DROP TABLE t2;")
 box.execute("DROP TABLE t3;")
 box.execute("DROP TABLE t1;")
 
+--
+-- gh-4188: make sure that in case of INSERT OR IGNORE only
+-- successful inserts are counted.
+--
+box.execute("CREATE TABLE t (i INT PRIMARY KEY AUTOINCREMENT, a INT check (a > 0));")
+box.execute("INSERT OR IGNORE INTO t VALUES (null, 1), (null, -1), (null, 2);")
+box.execute("SELECT * FROM t;")
+box.execute("DROP TABLE t;")
-- 
GitLab