aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordanielk1977 <danielk1977@noemail.net>2005-01-17 08:57:09 +0000
committerdanielk1977 <danielk1977@noemail.net>2005-01-17 08:57:09 +0000
commit3719d7f9c4829ee2624bcd64f0e89d86a85eb3c8 (patch)
tree6ee0c0a56d0e6e99a5361a274a716a79a8da5127 /src
parent5558a8a6973a69cf00115cef1802abf0de2e6229 (diff)
downloadsqlite-3719d7f9c4829ee2624bcd64f0e89d86a85eb3c8.tar.gz
sqlite-3719d7f9c4829ee2624bcd64f0e89d86a85eb3c8.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/select.c19
1 files changed, 13 insertions, 6 deletions
diff --git a/src/select.c b/src/select.c
index ac12ff48a..be8c57893 100644
--- a/src/select.c
+++ b/src/select.c
@@ -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;