1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-14 00:22:38 +03:00

LIMIT occurs after DISTINCT. Ticket #749. (CVS 1823)

FossilOrigin-Name: e6bc8aa80824a9156e78fc99b5ac7045b97d29c3
This commit is contained in:
drh
2004-07-19 23:16:38 +00:00
parent 88215bdfde
commit ea48eb2eef
4 changed files with 58 additions and 29 deletions

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.197 2004/07/18 21:33:02 drh Exp $
** $Id: select.c,v 1.198 2004/07/19 23:16:39 drh Exp $
*/
#include "sqliteInt.h"
@@ -320,6 +320,31 @@ static void pushOntoSorter(Parse *pParse, Vdbe *v, ExprList *pOrderBy){
sqlite3VdbeAddOp(v, OP_SortPut, 0, 0);
}
/*
** Add code to implement the OFFSET and LIMIT
*/
static void codeLimiter(
Parse *pParse, /* Parsing context */
Select *p, /* The SELECT statement being coded */
int iContinue, /* Jump here to skip the current record */
int iBreak, /* Jump here to end the loop */
int nPop /* Number of times to pop stack when jumping */
){
Vdbe *v = pParse->pVdbe;
if( p->iOffset>=0 ){
int addr = sqlite3VdbeCurrentAddr(v) + 2;
if( nPop>0 ) addr++;
sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr);
if( nPop>0 ){
sqlite3VdbeAddOp(v, OP_Pop, nPop, 0);
}
sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
}
if( p->iLimit>=0 ){
sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak);
}
}
/*
** This routine generates the code for the inside of the inner loop
** of a SELECT.
@@ -345,6 +370,7 @@ static int selectInnerLoop(
){
Vdbe *v = pParse->pVdbe;
int i;
int hasDistinct; /* True if the DISTINCT keyword is present */
if( v==0 ) return 0;
assert( pEList!=0 );
@@ -352,15 +378,9 @@ static int selectInnerLoop(
/* If there was a LIMIT clause on the SELECT statement, then do the check
** to see if this row should be output.
*/
if( pOrderBy==0 ){
if( p->iOffset>=0 ){
int addr = sqlite3VdbeCurrentAddr(v);
sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr+2);
sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
}
if( p->iLimit>=0 ){
sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, iBreak);
}
hasDistinct = distinct>=0 && pEList && pEList->nExpr>0;
if( pOrderBy==0 && !hasDistinct ){
codeLimiter(pParse, p, iContinue, iBreak, 0);
}
/* Pull the requested columns.
@@ -380,7 +400,7 @@ static int selectInnerLoop(
** and this row has been seen before, then do not make this row
** part of the result.
*/
if( distinct>=0 && pEList && pEList->nExpr>0 ){
if( hasDistinct ){
#if NULL_ALWAYS_DISTINCT
sqlite3VdbeAddOp(v, OP_IsNull, -pEList->nExpr, sqlite3VdbeCurrentAddr(v)+7);
#endif
@@ -392,6 +412,9 @@ static int selectInnerLoop(
sqlite3VdbeAddOp(v, OP_Goto, 0, iContinue);
sqlite3VdbeAddOp(v, OP_String8, 0, 0);
sqlite3VdbeAddOp(v, OP_PutStrKey, distinct, 0);
if( pOrderBy==0 ){
codeLimiter(pParse, p, iContinue, iBreak, nColumn);
}
}
switch( eDest ){
@@ -559,14 +582,7 @@ static void generateSortTail(
}
sqlite3VdbeOp3(v, OP_Sort, 0, 0, (char*)pInfo, P3_KEYINFO_HANDOFF);
addr = sqlite3VdbeAddOp(v, OP_SortNext, 0, end1);
if( p->iOffset>=0 ){
sqlite3VdbeAddOp(v, OP_MemIncr, p->iOffset, addr+4);
sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
sqlite3VdbeAddOp(v, OP_Goto, 0, addr);
}
if( p->iLimit>=0 ){
sqlite3VdbeAddOp(v, OP_MemIncr, p->iLimit, end2);
}
codeLimiter(pParse, p, addr, end2, 1);
switch( eDest ){
case SRT_Table:
case SRT_TempTable: {