Fix a bug reported on the mailing list concerning a conflict between "INSERT INTO ... SELECT" statements and the "SELECT max(x) FROM tbl" optimization. (CVS 2227)

FossilOrigin-Name: 5a9da62ae303800ded99942aed30eadeb3863da3
This commit is contained in:
danielk1977 2005-01-17 08:57:09 +00:00
parent 5558a8a697
commit 3719d7f9c4
4 changed files with 45 additions and 15 deletions

View File

@ -1,5 +1,5 @@
C Fix\ssome\smemory\sleak\sproblems\swith\scorrupt.test\sand\sauto-vacuum\sdatabases.\s(CVS\s2226)
D 2005-01-17T07:53:44
C Fix\sa\sbug\sreported\son\sthe\smailing\slist\sconcerning\sa\sconflict\sbetween\s"INSERT\sINTO\s...\sSELECT"\sstatements\sand\sthe\s"SELECT\smax(x)\sFROM\stbl"\soptimization.\s(CVS\s2227)
D 2005-01-17T08:57:09
F Makefile.in 78d6d0af3725aef32468ac9923444d7645d21a28
F Makefile.linux-gcc a9e5a0d309fa7c38e7c14d3ecf7690879d3a5457
F README a01693e454a00cc117967e3f9fdab2d4d52e9bc1
@ -57,7 +57,7 @@ F src/parse.y ceba179b9703657180963568f54b0e75f33e36e1
F src/pragma.c ac594f74c90ffec043c43e49358719ffeb491eec
F src/printf.c 3d20b21cfecadacecac3fb7274e746cb81d3d357
F src/random.c eff68e3f257e05e81eae6c4d50a51eb88beb4ff3
F src/select.c e29c70ec5f5dbb66c6bf8ca3ab3b41cbb8daa41b
F src/select.c f74bdde6e77e7c351084646edbc859b08e0a3414
F src/shell.c 591364a0e9ca4ce53873e21e0294476c0c2b4770
F src/sqlite.h.in 0d5e48e506845b74a845c9470e01d3f472b59611
F src/sqliteInt.h 641b348a109a080262d9f3603f2e94143d4383f2
@ -132,7 +132,7 @@ F test/hook.test f8605cde4c77b2c6a4a73723bf6c507796a64dda
F test/in.test b92a2df9162e1cbd33c6449a29a05e6955b1741a
F test/index.test 1294997b4743007af57f8148c63ba14f07ad31ab
F test/index2.test ed2409af110aa06ec0c9fedfa050031887c38707
F test/insert.test 56f9c20c9adc8d707490c4ffa5d4daa94826ea03
F test/insert.test b18db896ff0b030224c586807390c9a6565ef5d4
F test/insert2.test 0bb50ff999e35a21549d8ee5dc44db8ac24d31a7
F test/insert3.test fa7cb5b01709a1bca3e28c82c80c1d44386b3676
F test/interrupt.test 5b4d8389e6cf2d01b94f87cfd02d9df1073bfb2d
@ -269,7 +269,7 @@ F www/tclsqlite.tcl e73f8f8e5f20e8277619433f7970060ab01088fc
F www/vdbe.tcl 095f106d93875c94b47367384ebc870517431618
F www/version3.tcl 092a01f5ef430d2c4acc0ae558d74c4bb89638a0
F www/whentouse.tcl c3b50d3ac31c54be2a1af9b488a89d22f1e6e746
P a01159e8cb93199763d191b739da2a43ec88aab3
R fbe60a34d6230243e3a7636baa8c41f3
P 6244252915fa312a6c4d192464023d95aaef4661
R 8fdfbf5efd695da44259ce4a14c6dc0d
U danielk1977
Z 7a02515a37f4bed08ff76d7a050b0eb0
Z f3732671f0bcdc732596f3a051a6bbad

View File

@ -1 +1 @@
6244252915fa312a6c4d192464023d95aaef4661
5a9da62ae303800ded99942aed30eadeb3863da3

View File

@ -12,7 +12,7 @@
** This file contains C code routines that are called by the parser
** to handle SELECT statements in SQLite.
**
** $Id: select.c,v 1.221 2005/01/15 01:52:32 drh Exp $
** $Id: select.c,v 1.222 2005/01/17 08:57:09 danielk1977 Exp $
*/
#include "sqliteInt.h"
@ -2057,7 +2057,6 @@ static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
ExprList *pEList, *pList, eList;
struct ExprList_item eListItem;
SrcList *pSrc;
/* Check to see if this query is a simple min() or max() query. Return
** zero if it is not.
@ -2130,17 +2129,25 @@ static int simpleMinMaxQuery(Parse *pParse, Select *p, int eDest, int iParm){
if( pIdx==0 ){
sqlite3VdbeAddOp(v, seekOp, base, 0);
}else{
/* Even though the cursor used to open the index here is closed
** as soon as a single value has been read from it, allocate it
** using (pParse->nTab++) to prevent the cursor id from being
** reused. This is important for statements of the form
** "INSERT INTO x SELECT max() FROM x".
*/
int iIdx;
iIdx = pParse->nTab++;
sqlite3VdbeAddOp(v, OP_Integer, pIdx->iDb, 0);
sqlite3VdbeOp3(v, OP_OpenRead, base+1, pIdx->tnum,
sqlite3VdbeOp3(v, OP_OpenRead, iIdx, pIdx->tnum,
(char*)&pIdx->keyInfo, P3_KEYINFO);
if( seekOp==OP_Rewind ){
sqlite3VdbeAddOp(v, OP_String, 0, 0);
sqlite3VdbeAddOp(v, OP_MakeRecord, 1, 0);
seekOp = OP_MoveGt;
}
sqlite3VdbeAddOp(v, seekOp, base+1, 0);
sqlite3VdbeAddOp(v, OP_IdxRecno, base+1, 0);
sqlite3VdbeAddOp(v, OP_Close, base+1, 0);
sqlite3VdbeAddOp(v, seekOp, iIdx, 0);
sqlite3VdbeAddOp(v, OP_IdxRecno, iIdx, 0);
sqlite3VdbeAddOp(v, OP_Close, iIdx, 0);
sqlite3VdbeAddOp(v, OP_MoveGe, base, 0);
}
eList.nExpr = 1;

View File

@ -11,7 +11,7 @@
# This file implements regression tests for SQLite library. The
# focus of this file is testing the INSERT statement.
#
# $Id: insert.test,v 1.21 2004/11/10 15:27:38 danielk1977 Exp $
# $Id: insert.test,v 1.22 2005/01/17 08:57:09 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -304,6 +304,29 @@ ifcapable {reindex} {
} {}
}
# Test that the special optimization for queries of the form
# "SELECT max(x) FROM tbl" where there is an index on tbl(x) works with
# INSERT statments.
do_test insert-7.1 {
execsql {
DROP TABLE t1;
CREATE TABLE t1(a);
INSERT INTO t1 VALUES(1);
INSERT INTO t1 VALUES(2);
CREATE INDEX i1 ON t1(a);
}
} {}
do_test insert-7.2 {
execsql {
INSERT INTO t1 SELECT max(a) FROM t1;
}
} {}
do_test insert-7.3 {
execsql {
SELECT a FROM t1;
}
} {1 2 2}
integrity_check insert-99.0
finish_test