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 0778222CED for ; Wed, 17 Jul 2019 05:55:02 -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 ExhF_Y9FM9Bc for ; Wed, 17 Jul 2019 05:55:01 -0400 (EDT) Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (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 BACC2227B4 for ; Wed, 17 Jul 2019 05:55:01 -0400 (EDT) From: imeevma@tarantool.org Subject: [tarantool-patches] [PATCH v4 4/4] sql: do not increase row-count if INSERT or REPLACE failed Date: Wed, 17 Jul 2019 12:54:59 +0300 Message-Id: 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: korablev@tarantool.org Cc: tarantool-patches@freelists.org 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 REPLACE. Follow-up #4188 --- src/box/sql/vdbe.c | 18 ++++++++++-------- test/sql/row-count.result | 30 ++++++++++++++++++++++++++++++ test/sql/row-count.test.lua | 8 ++++++++ 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 3b38bc2..02f35c4 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -4189,8 +4189,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; @@ -4213,12 +4211,16 @@ 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) { - 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_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) { diff --git a/test/sql/row-count.result b/test/sql/row-count.result index e7841ca..69bfb78 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: check that generated IDs is not showed for failed +-- insertions in case of INSERT OR IGNORE. +-- +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..965408e 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: check that generated IDs is not showed for failed +-- insertions in case of INSERT OR IGNORE. +-- +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;") -- 2.7.4