From 8af4d3ace6364780acd0514e24efbef6629f810b Mon Sep 17 00:00:00 2001 From: drh Date: Tue, 6 May 2003 20:35:16 +0000 Subject: [PATCH] Restrain the flattener in the presence of outer joins. Ticket #306. (CVS 958) FossilOrigin-Name: 6d4b6597e560578253960d9876dc8c8657f41fef --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/select.c | 23 +++++++++++++++++++++-- test/join.test | 28 +++++++++++++++++++++++++++- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/manifest b/manifest index a1f30771d0..f53daea036 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Added\stests\strying\s(unsuccessfully)\sto\sreproduce\sticket\s#304.\s(CVS\s957) -D 2003-05-04T20:42:56 +C Restrain\sthe\sflattener\sin\sthe\spresence\sof\souter\sjoins.\s\sTicket\s#306.\s(CVS\s958) +D 2003-05-06T20:35:16 F Makefile.in 004acec253ecdde985c8ecd5b7c9accdb210378f F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd @@ -43,7 +43,7 @@ F src/parse.y 39b5240cb78047dc56d6d37c398baed7ba556779 F src/pragma.c 118fe400d71b7fdcc03580d5eab6bb5aa00772a5 F src/printf.c fc5fdef6e92ad205005263661fe9716f55a49f3e F src/random.c 19e8e00fe0df32a742f115773f57651be327cabe -F src/select.c 3fe63e3a29df661ba72a67eecd77e8ee82801def +F src/select.c c06b4605bca03d8237a3fc4098179bf3a7133702 F src/shell.c 89a14538b40fbfa15b0ad10b3367aab272bd9f15 F src/shell.tcl 27ecbd63dd88396ad16d81ab44f73e6c0ea9d20e F src/sqlite.h.in eec06462cba262c0ee03f38462a18a4bc66dda4e @@ -87,7 +87,7 @@ F test/insert.test 5697ba098e4d8a6f0151f281b7e39dec9c439e05 F test/insert2.test c288375a64dad3295044714f0dfed4a193cf067f F test/intpkey.test 39f49fd993350f7f3ab255e5cfbf9a09d8f8800e F test/ioerr.test 5dbaf09f96b56ee01cf3edd762b96eb4ad2c9ca4 -F test/join.test c97267c19294bf1fa4e81087edad179828bced88 +F test/join.test 16c91ec27170c20e2a10796775e62c5c3dcbda44 F test/limit.test 9ffb965a0f5bf7152187ef3d8d1249b96e5620bf F test/lock.test 388a3a10962d2d571c0c1821cc35bf069ee73473 F test/main.test 6a851b5992c4881a725a3d9647e629199df8de9d @@ -165,7 +165,7 @@ F www/speed.tcl cb4c10a722614aea76d2c51f32ee43400d5951be F www/sqlite.tcl ffde644361e1d8e2a44a235ff23ad3b43d640df2 F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218 -P 5656fe48b192dc84cb5977f826ff99d81684791f -R cdec87c4eaf662069dea6dbf74494ed5 +P fda637f453d35b74f676997fcf056a948ccd771b +R ce92f17887cce1b923da966e242b319f U drh -Z 054b394f2a79b867a0d598db2de3f85c +Z ab57ff50799dad75d8d41943b87540c0 diff --git a/manifest.uuid b/manifest.uuid index c9a5ceaf49..fcf0939e77 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -fda637f453d35b74f676997fcf056a948ccd771b \ No newline at end of file +6d4b6597e560578253960d9876dc8c8657f41fef \ No newline at end of file diff --git a/src/select.c b/src/select.c index 0f8c3c8844..82138a108e 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.137 2003/05/02 16:04:17 drh Exp $ +** $Id: select.c,v 1.138 2003/05/06 20:35:16 drh Exp $ */ #include "sqliteInt.h" @@ -1555,7 +1555,8 @@ substExprList(ExprList *pList, int iTable, ExprList *pEList){ ** ** (2) The subquery is not an aggregate or the outer query is not a join. ** -** (3) (No longer a restriction) +** (3) The subquery is not the right operand of a left outer join, or +** the subquery is not itself a join. (Ticket #306) ** ** (4) The subquery is not DISTINCT or the outer query is not a join. ** @@ -1620,6 +1621,22 @@ static int flattenSubquery( if( (p->isDistinct || p->nLimit>=0) && subqueryIsAgg ) return 0; if( p->pOrderBy && pSub->pOrderBy ) return 0; + /* 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 + ** is not allowed: + ** + ** t1 LEFT OUTER JOIN (t2 JOIN t3) + ** + ** If we flatten the above, we would get + ** + ** (t1 LEFT OUTER JOIN t2) JOIN t3 + ** + ** which is not at all the same thing. + */ + if( pSubSrc->nSrc>1 && iFrom>0 && (pSrc->a[iFrom-1].jointype & JT_OUTER)!=0 ){ + return 0; + } + /* If we reach this point, it means flattening is permitted for the ** iFrom-th entry of the FROM clause in the outer query. */ @@ -1635,6 +1652,7 @@ static int flattenSubquery( iParent = pSrc->a[iFrom].iCursor; { int nSubSrc = pSubSrc->nSrc; + int jointype = pSrc->a[iFrom].jointype; if( pSrc->a[iFrom].pTab && pSrc->a[iFrom].pTab->isTransient ){ sqliteDeleteTable(0, pSrc->a[iFrom].pTab); @@ -1655,6 +1673,7 @@ static int flattenSubquery( pSrc->a[i+iFrom] = pSubSrc->a[i]; memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); } + pSrc->a[iFrom+nSubSrc-1].jointype = jointype; } /* Now begin substituting subquery result set expressions for diff --git a/test/join.test b/test/join.test index c8cb10da01..1d62845cb4 100644 --- a/test/join.test +++ b/test/join.test @@ -12,7 +12,7 @@ # # This file implements tests for joins, including outer joins. # -# $Id: join.test,v 1.8 2003/02/20 01:48:13 drh Exp $ +# $Id: join.test,v 1.9 2003/05/06 20:35:17 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -377,4 +377,30 @@ do_test join-7.1 { } } {1 999 999 2 131 130 999} +# Make sure a left join where the right table is really a view that +# is itself a join works right. Ticket #306. +# +do_test join-8.1 { + execsql { + BEGIN; + CREATE TABLE t9(a INTEGER PRIMARY KEY, b); + INSERT INTO t9 VALUES(1,11); + INSERT INTO t9 VALUES(2,22); + CREATE TABLE t10(x INTEGER PRIMARY KEY, y); + INSERT INTO t10 VALUES(1,2); + INSERT INTO t10 VALUES(3,3); + CREATE TABLE t11(p INTEGER PRIMARY KEY, q); + INSERT INTO t11 VALUES(2,111); + INSERT INTO t11 VALUES(3,333); + CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p; + COMMIT; + SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x ); + } +} {1 11 1 111 2 22 {} {}} +do_test join-8.2 { + execsql { + SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x ); + } +} {1 111 1 11 3 333 {} {}} + finish_test