Tarantool development patches archive
 help / color / mirror / Atom feed
From: Mergen Imeev <imeevma@tarantool.org>
To: "n.pettik" <korablev@tarantool.org>
Cc: tarantool-patches@freelists.org
Subject: [tarantool-patches] Re: [PATCH v4 4/4] sql: do not increase row-count if INSERT or REPLACE failed
Date: Fri, 19 Jul 2019 12:36:50 +0300	[thread overview]
Message-ID: <20190719093650.GB22697@tarantool.org> (raw)
In-Reply-To: <1C2735EE-5E20-43D6-B8BA-3B83C7B54482@tarantool.org>

On Wed, Jul 17, 2019 at 07:57:16PM +0300, n.pettik wrote:
> Subject says:
> "sql: do not increase row-count if INSERT or REPLACE failed"
> 
> But in fact it is about INSERT OR IGNORE statement.
Fixed.

Diff beween patches:

diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index a8ff8af..69ad3fc 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -4220,31 +4220,37 @@ case OP_IdxInsert: {
 		rc = tarantoolsqlEphemeralInsert(space, pIn2->z,
 						     pIn2->z + pIn2->n);
 	}
-	if (rc == 0) {
-		if (pOp->p5 & OPFLAG_NCHANGE)
-			p->nChange++;
-		if (pOp->p3 > 0 && ((aMem[pOp->p3].flags) & MEM_Null) != 0) {
-			assert(space->sequence != NULL);
-			int64_t value;
-			if (sequence_get_value(space->sequence, &value) != 0)
-				goto abort_due_to_error;
-			vdbe_add_new_autoinc_id(p, value);
+	if (rc != 0) {
+		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++;
+			break;
 		}
+		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;
+		}
+		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 (pOp->p5 & OPFLAG_NCHANGE)
+		p->nChange++;
+	if (pOp->p3 > 0 && ((aMem[pOp->p3].flags) & MEM_Null) != 0) {
+		assert(space->sequence != NULL);
+		int64_t value;
+		if (sequence_get_value(space->sequence, &value) != 0)
+			goto abort_due_to_error;
+		vdbe_add_new_autoinc_id(p, value);
 	}
-	if (rc != 0)
-		goto abort_due_to_error;
 	break;
 }
 
diff --git a/test/sql/row-count.result b/test/sql/row-count.result
index 69bfb78..94ebaf0 100644
--- a/test/sql/row-count.result
+++ b/test/sql/row-count.result
@@ -336,8 +336,8 @@ box.execute("DROP TABLE t1;")
 - row_count: 1
 ...
 --
--- gh-4188: check that generated IDs is not showed for failed
--- insertions in case of INSERT OR IGNORE.
+-- 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));")
 ---
diff --git a/test/sql/row-count.test.lua b/test/sql/row-count.test.lua
index 965408e..af7f168 100644
--- a/test/sql/row-count.test.lua
+++ b/test/sql/row-count.test.lua
@@ -74,8 +74,8 @@ box.execute("DROP TABLE t3;")
 box.execute("DROP TABLE t1;")
 
 --
--- gh-4188: check that generated IDs is not showed for failed
--- insertions in case of INSERT OR IGNORE.
+-- 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);")


New patch:

From c987059d25f8a63931a1bb809d5ac71c53d64656 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

diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c
index 8ac3afb..69ad3fc 100644
--- a/src/box/sql/vdbe.c
+++ b/src/box/sql/vdbe.c
@@ -4198,8 +4198,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;
@@ -4222,27 +4220,37 @@ 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) {
+			/*
+			 * 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++;
+			break;
+		}
+		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;
+		}
+		goto abort_due_to_error;
+	}
+	if (pOp->p5 & OPFLAG_NCHANGE)
+		p->nChange++;
+	if (pOp->p3 > 0 && ((aMem[pOp->p3].flags) & MEM_Null) != 0) {
 		assert(space->sequence != NULL);
 		int64_t value;
 		if (sequence_get_value(space->sequence, &value) != 0)
 			goto abort_due_to_error;
 		vdbe_add_new_autoinc_id(p, value);
 	}
-
-	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 e7841ca..94ebaf0 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 9f5215c..af7f168 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;")

  parent reply	other threads:[~2019-07-19  9:36 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-17  9:54 [tarantool-patches] [PATCH v4 0/4] sql: do not show IDs generated by trigger imeevma
2019-07-17  9:54 ` [tarantool-patches] [PATCH v4 1/4] sql: remove unnecessary AUTOINCREMENT ID generation imeevma
2019-07-17  9:54 ` [tarantool-patches] [PATCH v4 2/4] sql: skip autoinc IDs generated inside SQL trigger imeevma
2019-07-17 16:58   ` [tarantool-patches] " n.pettik
2019-07-19  9:33     ` Mergen Imeev
2019-07-17  9:54 ` [tarantool-patches] [PATCH v4 3/4] sql: remove VDBE from TXN imeevma
2019-07-17  9:54 ` [tarantool-patches] [PATCH v4 4/4] sql: do not increase row-count if INSERT or REPLACE failed imeevma
2019-07-17 16:57   ` [tarantool-patches] " n.pettik
2019-07-17 18:08     ` [tarantool-patches] " Мерген Имеев
2019-07-19  9:36     ` Mergen Imeev [this message]
2019-07-22 10:48       ` n.pettik
2019-07-22 11:26         ` Mergen Imeev
2019-07-24 13:55 ` [tarantool-patches] Re: [PATCH v4 0/4] sql: do not show IDs generated by trigger Kirill Yukhin

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=20190719093650.GB22697@tarantool.org \
    --to=imeevma@tarantool.org \
    --cc=korablev@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='[tarantool-patches] Re: [PATCH v4 4/4] sql: do not increase row-count if INSERT or REPLACE failed' \
    /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