Do not flatten subqueries in a join where the subquery includes a LIMIT.

Ticket #1634.  This is just an initial fix.  Many test cases need to be
added prior to closing the ticket. (CVS 2987)

FossilOrigin-Name: af18c0f431a1a6349e40249009f2ac222f8c8114
This commit is contained in:
drh 2006-01-21 22:19:54 +00:00
parent bcdb66166e
commit ac83963afa
3 changed files with 32 additions and 17 deletions

View File

@ -1,5 +1,5 @@
C Fix\sa\sbug\sin\sos.h.\s\sHow\sdid\sthis\sslip\sby\sbefore\snow?\s(CVS\s2986) C Do\snot\sflatten\ssubqueries\sin\sa\sjoin\swhere\sthe\ssubquery\sincludes\sa\sLIMIT.\nTicket\s#1634.\s\sThis\sis\sjust\san\sinitial\sfix.\s\sMany\stest\scases\sneed\sto\sbe\nadded\sprior\sto\sclosing\sthe\sticket.\s(CVS\s2987)
D 2006-01-21T19:57:51 D 2006-01-21T22:19:55
F Makefile.in ab3ffd8d469cef4477257169b82810030a6bb967 F Makefile.in ab3ffd8d469cef4477257169b82810030a6bb967
F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -66,7 +66,7 @@ F src/pragma.c 4496cc77dc35824e1c978c3d1413b8a5a4c777d3
F src/prepare.c 5d6f5b7194ee72cecd66cab49d15159e55d63f28 F src/prepare.c 5d6f5b7194ee72cecd66cab49d15159e55d63f28
F src/printf.c f47a2f4b5387cd2ebb12e9117a1a5d6bd9a2b812 F src/printf.c f47a2f4b5387cd2ebb12e9117a1a5d6bd9a2b812
F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261 F src/random.c d40f8d356cecbd351ccfab6eaedd7ec1b54f5261
F src/select.c 7a7f4f73a120fbd42e4f5708f5da7a57721bfc7f F src/select.c 4e9072092ddba113acf3c8192152ae9e75a23702
F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96
F src/shell.c 66b073375efbdee19045e7e0cd38b85f9aff71da F src/shell.c 66b073375efbdee19045e7e0cd38b85f9aff71da
F src/sqlite.h.in 492580f7e3ff71eb43193eb7bb98e2d549889ce3 F src/sqlite.h.in 492580f7e3ff71eb43193eb7bb98e2d549889ce3
@ -344,7 +344,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P 29281dea81c909b70b2d914d7061a6df8f388195 P 29725de474f9aec81cea0041d9ac2df932258d01
R 2cfc41c8ea1cc80b34434b2ac4a057da R 155027f2bd0a2644348d1abb7c1fede1
U drh U drh
Z f6c5b51155ddd0b8f9133fdab5d2a2d3 Z 52af4bf5dc7ce62a9ad3e0cb3c9897c9

View File

@ -1 +1 @@
29725de474f9aec81cea0041d9ac2df932258d01 af18c0f431a1a6349e40249009f2ac222f8c8114

View File

@ -12,7 +12,7 @@
** This file contains C code routines that are called by the parser ** This file contains C code routines that are called by the parser
** to handle SELECT statements in SQLite. ** to handle SELECT statements in SQLite.
** **
** $Id: select.c,v 1.296 2006/01/20 18:10:57 drh Exp $ ** $Id: select.c,v 1.297 2006/01/21 22:19:55 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
@ -2020,6 +2020,10 @@ static void substSelect(Select *p, int iTable, ExprList *pEList){
** (12) The subquery is not the right term of a LEFT OUTER JOIN or the ** (12) The subquery is not the right term of a LEFT OUTER JOIN or the
** subquery has no WHERE clause. (added by ticket #350) ** subquery has no WHERE clause. (added by ticket #350)
** **
** (13) The subquery and outer query do not both use LIMIT
**
** (14) The subquery does not use OFFSET
**
** In this routine, the "p" parameter is a pointer to the outer query. ** In this routine, the "p" parameter is a pointer to the outer query.
** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query ** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query
** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
@ -2054,18 +2058,26 @@ static int flattenSubquery(
pSubitem = &pSrc->a[iFrom]; pSubitem = &pSrc->a[iFrom];
pSub = pSubitem->pSelect; pSub = pSubitem->pSelect;
assert( pSub!=0 ); assert( pSub!=0 );
if( isAgg && subqueryIsAgg ) return 0; if( isAgg && subqueryIsAgg ) return 0; /* Restriction (1) */
if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; /* Restriction (2) */
pSubSrc = pSub->pSrc; pSubSrc = pSub->pSrc;
assert( pSubSrc ); assert( pSubSrc );
if( (pSub->pLimit && p->pLimit) || pSub->pOffset || /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
(pSub->pLimit && isAgg) ) return 0; ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
if( pSubSrc->nSrc==0 ) return 0; ** because they could be computed at compile-time. But when LIMIT and OFFSET
if( pSub->isDistinct && (pSrc->nSrc>1 || isAgg) ){ ** became arbitrary expressions, we were forced to add restrictions (13)
** and (14). */
if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */
if( pSub->pOffset ) return 0; /* Restriction (14) */
if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */
if( (pSub->isDistinct || pSub->pLimit)
&& (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */
return 0; return 0;
} }
if( p->isDistinct && subqueryIsAgg ) return 0; if( p->isDistinct && subqueryIsAgg ) return 0; /* Restriction (6) */
if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ) return 0; if( (p->disallowOrderBy || p->pOrderBy) && pSub->pOrderBy ){
return 0; /* Restriction (11) */
}
/* Restriction 3: If the subquery is a join, make sure the subquery is /* Restriction 3: If the subquery is a join, make sure the subquery is
** not used as the right operand of an outer join. Examples of why this ** not used as the right operand of an outer join. Examples of why this
@ -2195,6 +2207,9 @@ static int flattenSubquery(
/* /*
** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
**
** One is tempted to try to add a and b to combine the limits. But this
** does not work if either limit is negative.
*/ */
if( pSub->pLimit ){ if( pSub->pLimit ){
p->pLimit = pSub->pLimit; p->pLimit = pSub->pLimit;