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 AA9212AC73 for ; Wed, 18 Apr 2018 12:33:43 -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 FMGSfFgn78G9 for ; Wed, 18 Apr 2018 12:33:43 -0400 (EDT) Received: from mail-lf0-f67.google.com (mail-lf0-f67.google.com [209.85.215.67]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 1DF082AC6F for ; Wed, 18 Apr 2018 12:33:42 -0400 (EDT) Received: by mail-lf0-f67.google.com with SMTP id d20-v6so3576412lfe.3 for ; Wed, 18 Apr 2018 09:33:42 -0700 (PDT) MIME-Version: 1.0 References: <1524065531-32467-1-git-send-email-hollow653@gmail.com> In-Reply-To: <1524065531-32467-1-git-send-email-hollow653@gmail.com> From: Hollow111 Date: Wed, 18 Apr 2018 16:33:30 +0000 Message-ID: Subject: [tarantool-patches] Re: [PATCH] sql: xfer optimization issue Content-Type: multipart/alternative; boundary="0000000000008ab8cc056a2204ac" 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: korablev@tarantool.org --0000000000008ab8cc056a2204ac Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello. I have notices some mistakes. Newer diff: diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c index ae8dafb..b27fc23 100644 --- a/src/box/sql/insert.c +++ b/src/box/sql/insert.c @@ -1908,24 +1908,25 @@ xferOptimization(Parse * pParse, /* Parser context */ break; } assert(pSrcIdx); - emit_open_cursor(pParse, iSrc, pSrcIdx->tnum); - sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); - VdbeComment((v, "%s", pSrcIdx->zName)); - emit_open_cursor(pParse, iDest, pDestIdx->tnum); - sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); - sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); - VdbeComment((v, "%s", pDestIdx->zName)); - addr1 =3D sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); - VdbeCoverage(v); - sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); - sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); - if (pDestIdx->idxType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY) + if (pDestIdx->idxType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY) { + emit_open_cursor(pParse, iSrc, pSrcIdx->tnum); + sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); + VdbeComment((v, "%s", pSrcIdx->zName)); + emit_open_cursor(pParse, iDest, pDestIdx->tnum); + sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); + sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); + VdbeComment((v, "%s", pDestIdx->zName)); + addr1 =3D sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); + VdbeCoverage(v); + sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE); - sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1 + 1); - VdbeCoverage(v); - sqlite3VdbeJumpHere(v, addr1); - sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); - sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1 + 1); + VdbeCoverage(v); + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + } } if (emptySrcTest) sqlite3VdbeJumpHere(v, emptySrcTest); diff --git a/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua b/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua new file mode 100755 index 0000000..3b2bcc6 --- /dev/null +++ b/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua @@ -0,0 +1,52 @@ +#!/usr/bin/env tarantool +test =3D require("sqltester") +test:plan(3) + +test:do_execsql_test( + "xfer-optimization-1.1", + [[ + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER UNIQUE); + INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3); + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER UNIQUE); + INSERT INTO t2 SELECT * FROM t1; + DROP TABLE t1; + DROP TABLE t2; + ]], { + -- + + -- + }) + +test:do_execsql_test( + "xfer-optimization-1.2", + [[ + CREATE TABLE t1(id INTEGER PRIMARY KEY, b INTEGER); + CREATE TABLE t2(id INTEGER PRIMARY KEY, b INTEGER); + CREATE INDEX i1 ON t1(b); + CREATE INDEX i2 ON t2(b); + INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3); + INSERT INTO t2 SELECT * FROM t1; + DROP TABLE t1; + DROP TABLE t2; + ]], { + -- + + -- + }) + +test:do_execsql_test( + "xfer-optimization-1.3", + [[ + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3); + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER); + INSERT INTO t2 SELECT * FROM t1; + DROP TABLE t1; + DROP TABLE t2; + ]], { + -- + + -- + }) + +test:finish_test() =D1=81=D1=80, 18 =D0=B0=D0=BF=D1=80. 2018 =D0=B3. =D0=B2 18:32, N.Tatunov <= hollow653@gmail.com>: > Currently insertion from the table to another one > with the same schema using SELECT works wrong. > The problem lies in xfer optimization which opens cursors for > all of the indexes and inserts data excessively. > The bug was fixed so the data should now insert > correctly. > > Closes #3307 > --- > > Branch: > https://github.com/tarantool/tarantool/tree/N_Tatunov/gh-3307-xfer-optimi= zation-issue > Issue: https://github.com/tarantool/tarantool/issues/3307 > > src/box/sql/insert.c | 35 ++++++++-------- > .../sql-tap/gh-3307-xfer-optimization-issue.result | 0 > .../gh-3307-xfer-optimization-issue.test.lua | 49 > ++++++++++++++++++++++ > 3 files changed, 67 insertions(+), 17 deletions(-) > create mode 100644 test/sql-tap/gh-3307-xfer-optimization-issue.result > create mode 100644 test/sql-tap/gh-3307-xfer-optimization-issue.test.lua > > diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c > index ae8dafb..b27fc23 100644 > --- a/src/box/sql/insert.c > +++ b/src/box/sql/insert.c > @@ -1908,24 +1908,25 @@ xferOptimization(Parse * pParse, /* Parser > context */ > break; > } > assert(pSrcIdx); > - emit_open_cursor(pParse, iSrc, pSrcIdx->tnum); > - sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); > - VdbeComment((v, "%s", pSrcIdx->zName)); > - emit_open_cursor(pParse, iDest, pDestIdx->tnum); > - sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); > - sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); > - VdbeComment((v, "%s", pDestIdx->zName)); > - addr1 =3D sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); > - VdbeCoverage(v); > - sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); > - sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData); > - if (pDestIdx->idxType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY) > + if (pDestIdx->idxType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY) { > + emit_open_cursor(pParse, iSrc, pSrcIdx->tnum); > + sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx); > + VdbeComment((v, "%s", pSrcIdx->zName)); > + emit_open_cursor(pParse, iDest, pDestIdx->tnum); > + sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx); > + sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR); > + VdbeComment((v, "%s", pDestIdx->zName)); > + addr1 =3D sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0= ); > + VdbeCoverage(v); > + sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); > + sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData= ); > sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE); > - sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1 + 1); > - VdbeCoverage(v); > - sqlite3VdbeJumpHere(v, addr1); > - sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); > - sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); > + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1 + 1); > + VdbeCoverage(v); > + sqlite3VdbeJumpHere(v, addr1); > + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); > + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); > + } > } > if (emptySrcTest) > sqlite3VdbeJumpHere(v, emptySrcTest); > diff --git a/test/sql-tap/gh-3307-xfer-optimization-issue.result > b/test/sql-tap/gh-3307-xfer-optimization-issue.result > new file mode 100644 > index 0000000..e69de29 > diff --git a/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua > b/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua > new file mode 100644 > index 0000000..e45235e > --- /dev/null > +++ b/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua > @@ -0,0 +1,49 @@ > +#!/usr/bin/env tarantool > +test =3D require("sqltester") > +test:plan(1) > + > +-- gh-3307 - sql: INSERT with SELECT is not working in some cases. > + > +test:do_exesql_test( > + "xfer-optimization-1.1", > + [[ > + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER UNIQUE); > + INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3); > + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER UNIQUE); > + INSERT INTO t2 SELECT * FROM t1; > + DROP TABLE t1; > + DROP TABLE t2; > + ]], { > + -- > + -- > + }) > + > +test:do_exesql_test( > + "xfer-optimization-1.2", > + [[ > + CREATE TABLE t1(id INTEGER PRIMARY KEY, b INTEGER); > + CREATE TABLE t2(id INTEGER PRIMARY KEY, b INTEGER); > + CREATE INDEX i1 ON t1(b); > + CREATE INDEX i2 ON t2(b); > + INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3); > + INSERT INTO t2 SELECT * FROM t1; > + DROP TABLE t1; > + DROP TABLE t2; > + ]], { > + -- > + -- > + }) > + > +test:do_exesql_test( > + "xfer-optimization-1.1", > + [[ > + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER); > + INSERT INTO t1 VALUES (1, 1), (2, 2), (3, 3); > + CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER); > + INSERT INTO t2 SELECT * FROM t1; > + DROP TABLE t1; > + DROP TABLE t2; > + ]], { > + -- > + -- > + }) > -- > 2.7.4 > > --0000000000008ab8cc056a2204ac Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello. I have notices some mistakes.
Newer diff:
diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
in= dex ae8dafb..b27fc23 100644
--- a/src/box/sql/insert.c
= +++ b/src/box/sql/insert.c
@@ -1908,24 +1908,25 @@ xferOptimizati= on(Parse * pParse, /* Parser context= */
=C2=A0 break;
=
=C2=A0 }
=C2=A0 assert(pSrcIdx);
- emit_open_cursor(pParse, iSrc, pSrcIdx->tn= um);
- sqlite3VdbeSetP4Ke= yInfo(pParse, pSrcIdx);
- VdbeComment((v, "%s", pSrcIdx->zName));
- emit_open_cursor(pParse, iDest, pDestIdx->= ;tnum);
- sqlite3VdbeSetP= 4KeyInfo(pParse, pDestIdx);
- sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
- VdbeComment((v, "%s", pDestIdx->zName));=
- addr1 =3D sqlite3VdbeA= ddOp2(v, OP_Rewind, iSrc, 0);
- = VdbeCoverage(v);
- sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
- sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regDat= a);
- if (pDestIdx->id= xType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY)
+ if (pDestIdx->idxType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY)= {
+ emit_open_cursor(pP= arse, iSrc, pSrcIdx->tnum);
+ = sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
+ VdbeComment((v, "%s", pSrcIdx->= zName));
+ emit_open_cur= sor(pParse, iDest, pDestIdx->tnum);
+ sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
+ sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR= );
+ VdbeComment((v, &qu= ot;%s", pDestIdx->zName));
+ addr1 =3D sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
= + VdbeCoverage(v);
+ sqlite3VdbeAddOp2(v, OP_RowData, iSrc= , regData);
+ sqlite3Vdb= eAddOp2(v, OP_IdxInsert, iDest, regData);
=C2=A0 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE);
= - sqlite3VdbeAddOp2(v, OP_Next, iSr= c, addr1 + 1);
- VdbeCove= rage(v);
- sqlite3VdbeJum= pHere(v, addr1);
- sqlite= 3VdbeAddOp2(v, OP_Close, iSrc, 0);
- sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
+ sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1 += 1);
+ VdbeCoverage(v);<= /div>
+ sqlite3VdbeJumpHere(v,= addr1);
+ sqlite3VdbeAd= dOp2(v, OP_Close, iSrc, 0);
+ <= /span>sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
+ }
=C2=A0= }
=C2=A0 if (empty= SrcTest)
=C2=A0 sqlite3Vd= beJumpHere(v, emptySrcTest);
diff --git a/test/sql-tap/gh-3307-xf= er-optimization-issue.test.lua b/test/sql-tap/gh-3307-xfer-optimization-iss= ue.test.lua
new file mode 100755
index 0000000..3b2bcc6=
--- /dev/null
+++ b/test/sql-tap/gh-3307-xfer-optimiza= tion-issue.test.lua
@@ -0,0 +1,52 @@
+#!/usr/bin/env ta= rantool
+test =3D require("sqltester")
+test:= plan(3)
+
+test:do_execsql_test(
+ "xfer-optimization-1.1",
+= [[
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER UNIQUE= );
+ INSERT INTO t1 VALUE= S (1, 1), (2, 2), (3, 3);
+ CREATE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER UNIQUE);
+ INSERT INTO t2 SELECT * FROM t1;
+ DROP TABLE t1;
+<= span style=3D"white-space:pre"> DROP TABLE t2;
+ ]], {
+ -- <xfer-optimization-1.1>
+
+ -- <xfer-optimization-1.1>
<= div>+ })
+
+test= :do_execsql_test(
+ "= xfer-optimization-1.2",
+ [[
+ CREATE TABLE t1= (id INTEGER PRIMARY KEY, b INTEGER);
+ CREATE TABLE t2(id INTEGER PRIMARY KEY, b INTEGER);
+ CREATE INDEX i1 ON t1(b);
<= div>+ CREATE INDEX i2 ON t2(b);
+ INSERT INTO t1 VALUES (1, = 1), (2, 2), (3, 3);
+ INS= ERT INTO t2 SELECT * FROM t1;
+ = DROP TABLE t1;
+ D= ROP TABLE t2;
+ ]], {
+ -- <xfer-optimization-1.= 2>
+
+ -- &l= t;xfer-optimization-1.2>
+ })
+
+test:do_execsql_test(
+ "xfer-optimization-1.3",
+= [[
+ CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
+ INSERT INTO t1 VALUES (1, 1= ), (2, 2), (3, 3);
+ CREA= TE TABLE t2(a INTEGER PRIMARY KEY, b INTEGER);
+ INSERT INTO t2 SELECT * FROM t1;
+ DROP TABLE t1;
+ DROP TABLE t2;
+ ]], {
+ = -- <xfer-optimization-1.3>
+
+ -- <xfer-optimization-1.3>
+ })
+
+test:finish_test()=


= =D1=81=D1=80, 18 =D0=B0=D0=BF=D1=80. 2018 =D0=B3. =D0=B2 18:32, N.Tatunov &= lt;hollow653@gmail.com>:
<= /div>
Currently insertion from the table to a= nother one
with the same schema using SELECT works wrong.
The problem lies in xfer optimization which opens cursors for
all of the indexes and inserts data excessively.
The bug was fixed so the data should now insert
correctly.

Closes #3307
---

Branch: https:/= /github.com/tarantool/tarantool/tree/N_Tatunov/gh-3307-xfer-optimization-is= sue
Issue: https://github.com/tarantool/tarantool/is= sues/3307

=C2=A0src/box/sql/insert.c=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| 35 ++++++++= --------
=C2=A0.../sql-tap/gh-3307-xfer-optimization-issue.result |=C2=A0 0
=C2=A0.../gh-3307-xfer-optimization-issue.test.lua=C2=A0 =C2=A0 =C2=A0 =C2= =A0| 49 ++++++++++++++++++++++
=C2=A03 files changed, 67 insertions(+), 17 deletions(-)
=C2=A0create mode 100644 test/sql-tap/gh-3307-xfer-optimization-issue.resul= t
=C2=A0create mode 100644 test/sql-tap/gh-3307-xfer-optimization-issue.test.= lua

diff --git a/src/box/sql/insert.c b/src/box/sql/insert.c
index ae8dafb..b27fc23 100644
--- a/src/box/sql/insert.c
+++ b/src/box/sql/insert.c
@@ -1908,24 +1908,25 @@ xferOptimization(Parse * pParse,=C2=A0 =C2=A0 =C2= =A0 =C2=A0 /* Parser context */
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 break;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 assert(pSrcIdx); -=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0emit_open_cursor(pP= arse, iSrc, pSrcIdx->tnum);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeSetP4Key= Info(pParse, pSrcIdx);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VdbeComment((v, &qu= ot;%s", pSrcIdx->zName));
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0emit_open_cursor(pP= arse, iDest, pDestIdx->tnum);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeSetP4Key= Info(pParse, pDestIdx);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeChangeP5= (v, OPFLAG_BULKCSR);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VdbeComment((v, &qu= ot;%s", pDestIdx->zName));
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0addr1 =3D sqlite3Vd= beAddOp2(v, OP_Rewind, iSrc, 0);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VdbeCoverage(v); -=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeAddOp2(v= , OP_RowData, iSrc, regData);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeAddOp2(v= , OP_IdxInsert, iDest, regData);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (pDestIdx->id= xType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY)
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (pDestIdx->id= xType =3D=3D SQLITE_IDXTYPE_PRIMARYKEY) {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0emit_open_cursor(pParse, iSrc, pSrcIdx->tnum);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0VdbeComment((v, "%s", pSrcIdx->zName));
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0emit_open_cursor(pParse, iDest, pDestIdx->tnum);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0VdbeComment((v, "%s", pDestIdx->zName));
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0addr1 =3D sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0VdbeCoverage(v);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeAddOp2(v, OP_IdxInsert, iDest, regData);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeAddOp2(v= , OP_Next, iSrc, addr1 + 1);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0VdbeCoverage(v); -=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeJumpHere= (v, addr1);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeAddOp2(v= , OP_Close, iSrc, 0);
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0sqlite3VdbeAddOp2(v= , OP_Close, iDest, 0);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1 + 1);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0VdbeCoverage(v);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeJumpHere(v, addr1);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}
=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (emptySrcTest)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sqlite3VdbeJumpHere= (v, emptySrcTest);
diff --git a/test/sql-tap/gh-3307-xfer-optimization-issue.result b/test/sql= -tap/gh-3307-xfer-optimization-issue.result
new file mode 100644
index 0000000..e69de29
diff --git a/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua b/test/s= ql-tap/gh-3307-xfer-optimization-issue.test.lua
new file mode 100644
index 0000000..e45235e
--- /dev/null
+++ b/test/sql-tap/gh-3307-xfer-optimization-issue.test.lua
@@ -0,0 +1,49 @@
+#!/usr/bin/env tarantool
+test =3D require("sqltester")
+test:plan(1)
+
+-- gh-3307 - sql: INSERT with SELECT is not working in some cases.
+
+test:do_exesql_test(
+=C2=A0 =C2=A0 =C2=A0 =C2=A0"xfer-optimization-1.1",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0[[
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE TABLE t1(a I= NTEGER PRIMARY KEY, b INTEGER UNIQUE);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INSERT INTO t1 VALU= ES (1, 1), (2, 2), (3, 3);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE TABLE t2(a I= NTEGER PRIMARY KEY, b INTEGER UNIQUE);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INSERT INTO t2 SELE= CT * FROM t1;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DROP TABLE t1;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DROP TABLE t2;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0]], {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- <xfer-optimiz= ation-1.1>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- <xfer-optimiz= ation-1.1>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0})
+
+test:do_exesql_test(
+=C2=A0 =C2=A0 =C2=A0 =C2=A0"xfer-optimization-1.2",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0[[
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE TABLE t1(id = INTEGER PRIMARY KEY, b INTEGER);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE TABLE t2(id = INTEGER PRIMARY KEY, b INTEGER);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE INDEX i1 ON = t1(b);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE INDEX i2 ON = t2(b);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INSERT INTO t1 VALU= ES (1, 1), (2, 2), (3, 3);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INSERT INTO t2 SELE= CT * FROM t1;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DROP TABLE t1;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DROP TABLE t2;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0]], {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- <xfer-optimiz= ation-1.2>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- <xfer-optimiz= ation-1.2>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0})
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0
+test:do_exesql_test(
+=C2=A0 =C2=A0 =C2=A0 =C2=A0"xfer-optimization-1.1",
+=C2=A0 =C2=A0 =C2=A0 =C2=A0[[
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE TABLE t1(a I= NTEGER PRIMARY KEY, b INTEGER);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INSERT INTO t1 VALU= ES (1, 1), (2, 2), (3, 3);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0CREATE TABLE t2(a I= NTEGER PRIMARY KEY, b INTEGER);
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0INSERT INTO t2 SELE= CT * FROM t1;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DROP TABLE t1;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DROP TABLE t2;
+=C2=A0 =C2=A0 =C2=A0 =C2=A0]], {
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- <xfer-optimiz= ation-1.1>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- <xfer-optimiz= ation-1.1>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0})
--
2.7.4

--0000000000008ab8cc056a2204ac--