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 6E93324599 for ; Mon, 15 Jul 2019 13:59:35 -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 R51UtQnh_hga for ; Mon, 15 Jul 2019 13:59:35 -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 2FB66244EE for ; Mon, 15 Jul 2019 13:59:35 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.8\)) Subject: [tarantool-patches] Re: [PATCH v3 4/4] sql: do not show generated IDs if INSERT failed From: "n.pettik" In-Reply-To: Date: Mon, 15 Jul 2019 20:59:32 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: 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 Cc: Imeev Mergen I=E2=80=99ve edited commit message: sql: omit auto-inc ID if INSERT OR IGNORE failed =20 If INSERT statement is executed with IGNORE error action (i.e. INSERT OR IGNORE ...), it will return generated autoincrement identifiers. Even in case the insert has failed. For example: =20 CREATE TABLE t (i INT PRIMARY KEY AUTOINCREMENT, a INT check (a > 0)); INSERT OR IGNORE INTO t VALUES (null, 1), (null, -1), (null, 2); =20 In this case only two identifiers should be returned, but before = this patch all three are appeared. So, let's account only identifiers = which are really inserted to the space. Also, row-count shows now number of successful insertions in case of INSERT OR IGNORE. =20 Follow-up #4188 TBO I dislike your approach for several reasons. Firstly, it introduces another one auxiliary flag, which enlarges branching, complicates code reading etc. Secondly, it changes program counter in OP_Insert: generally speaking, I=E2=80=99d not consider implicit incrementation of program counter to be a good practice. You even don=E2=80=99t check that the next instruction is OP_SaveAutoinc=E2=80=A6 Could you spend some more time trying come up with another solution? Also, I wonder why didn=E2=80=99t you split OP_SaveAutoinc into two opcode: OP_IsNull + OP_SaveAutoinc which would unconditionally save autroincremented value to the vdbe? Obvious diff: diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c index d1622428c..e9a9c3afc 100644 --- a/src/box/sql/insert.c +++ b/src/box/sql/insert.c @@ -735,11 +735,11 @@ sqlInsert(Parse * pParse, /* Parser context */ vdbe_emit_constraint_checks(pParse, space, regIns + 1, on_error, endOfLoop, 0); fk_constraint_emit_check(pParse, space, 0, regIns, 0); + bool has_autoinc_field =3D autoinc_fieldno < UINT32_MAX = && + pParse->triggered_space =3D=3D = NULL; vdbe_emit_insertion_completion(v, space, regIns + 1, space->def->field_count, - on_error, - autoinc_fieldno < = UINT32_MAX && - pParse->triggered_space = =3D=3D NULL); + on_error, = has_autoinc_field); /* * Save the newly generated autoincrement value to * VDBE context. We don't need to do so for @@ -748,8 +748,7 @@ sqlInsert(Parse * pParse, /* Parser context */ * autoincrement values is returned as a result of * query execution. */ - if (autoinc_fieldno < UINT32_MAX && - pParse->triggered_space =3D=3D NULL) { + if (has_autoinc_field) { sqlVdbeAddOp2(v, OP_SaveAutoincValue, = space->def->id, regData + autoinc_fieldno); } diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 884c6605c..366f1f989 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -4236,8 +4236,10 @@ case OP_IdxInsert: { * should be the following opcode if the space has * a field with AUTOINCREMENT. */ - if (rc !=3D 0 && (pOp->p5 & OPFLAG_AUTOINC) !=3D 0) + if (rc !=3D 0 && (pOp->p5 & OPFLAG_AUTOINC) !=3D 0) { ++pOp; + assert(pOp->opcode =3D=3D OP_SaveAutoincValue); + } /* Ignore any kind of failes and do not raise error = message */ rc =3D 0;