In joins of the form: "A, B left C" make sure that the reordering optimization

does not put table A after table C.  Ticket #1652. (CVS 3052)

FossilOrigin-Name: 248b9be93d9532e31c640432b75c3310e180acb3
This commit is contained in:
drh 2006-02-01 02:45:02 +00:00
parent 8e2e2a1624
commit 570b935c2a
4 changed files with 66 additions and 13 deletions

View File

@ -1,5 +1,5 @@
C Added\ssupport\sfor\sthe\s-enable-thread-override-lock\soption\son\sthe\nconfigure\sscript.\s(CVS\s3051) C In\sjoins\sof\sthe\sform:\s"A,\sB\sleft\sC"\smake\ssure\sthat\sthe\sreordering\soptimization\ndoes\snot\sput\stable\sA\safter\stable\sC.\s\sTicket\s#1652.\s(CVS\s3052)
D 2006-02-01T01:55:17 D 2006-02-01T02:45:02
F Makefile.in 5d8dff443383918b700e495de42ec65bc1c8865b F Makefile.in 5d8dff443383918b700e495de42ec65bc1c8865b
F Makefile.linux-gcc 74ba0eadf88748a9ce3fd03d2a3ede2e6715baec F Makefile.linux-gcc 74ba0eadf88748a9ce3fd03d2a3ede2e6715baec
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -96,7 +96,7 @@ F src/vdbeapi.c dcb2636f49b4807e34960d52a2fc257b3a751140
F src/vdbeaux.c 9bf50cdb6a6c40b8c06ca9a8d87cf90120a16797 F src/vdbeaux.c 9bf50cdb6a6c40b8c06ca9a8d87cf90120a16797
F src/vdbefifo.c 9efb94c8c3f4c979ebd0028219483f88e57584f5 F src/vdbefifo.c 9efb94c8c3f4c979ebd0028219483f88e57584f5
F src/vdbemem.c 2034e93b32c14bda6e306bb54e3a8e930b963027 F src/vdbemem.c 2034e93b32c14bda6e306bb54e3a8e930b963027
F src/where.c 8409e00fa2cb5fce873b4c911165cfed097e9c49 F src/where.c c7d71d5e55c9c4c1e948089280fb0dec7c7d1ef6
F tclinstaller.tcl 046e3624671962dc50f0481d7c25b38ef803eb42 F tclinstaller.tcl 046e3624671962dc50f0481d7c25b38ef803eb42
F test/aggerror.test b854de90f530ae37be68fbfe6de40e111358cbb2 F test/aggerror.test b854de90f530ae37be68fbfe6de40e111358cbb2
F test/all.test 5df90d015ca63fcef2a4b62c24f7316b66c4bfd4 F test/all.test 5df90d015ca63fcef2a4b62c24f7316b66c4bfd4
@ -277,6 +277,7 @@ F test/varint.test ab7b110089a08b9926ed7390e7e97bdefeb74102
F test/view.test 354bd0ceb363e88da8ce98fb47e18550d7594b13 F test/view.test 354bd0ceb363e88da8ce98fb47e18550d7594b13
F test/where.test ee7c9a6659b07e1ee61177f6e7ff71565ee2c9df F test/where.test ee7c9a6659b07e1ee61177f6e7ff71565ee2c9df
F test/where2.test fde821b9cb8e20d53ccd2e71482b063c5b1e222a F test/where2.test fde821b9cb8e20d53ccd2e71482b063c5b1e222a
F test/where3.test 6356013ce1c8ddc22a65c880dfff2b2c985634cb
F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b F tool/diffdb.c 7524b1b5df217c20cd0431f6789851a4e0cb191b
F tool/lemon.c 26d271a753ef87fe1e6194f53c594ab5e6783d85 F tool/lemon.c 26d271a753ef87fe1e6194f53c594ab5e6783d85
F tool/lempar.c 424df14a48736bb961ed47acf30c26d66ed85a62 F tool/lempar.c 424df14a48736bb961ed47acf30c26d66ed85a62
@ -349,7 +350,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 e83a19e8cb6d721b02502925b362f2e86b2de742 P 82f558bd11ddb20792fbc9f579b40ad38d7f6af5
R 8e940d36d3cad60499f251caa2aa3738 R 2af434e8b15703eb7f034d5057b5c6a7
U drh U drh
Z cc40a051ac76c8707a4ca4b120503958 Z 400ad0ae9ac8ef551d8c82316e3538c4

View File

@ -1 +1 @@
82f558bd11ddb20792fbc9f579b40ad38d7f6af5 248b9be93d9532e31c640432b75c3310e180acb3

View File

@ -16,7 +16,7 @@
** so is applicable. Because this module is responsible for selecting ** so is applicable. Because this module is responsible for selecting
** indices, you might also think of this module as the "query optimizer". ** indices, you might also think of this module as the "query optimizer".
** **
** $Id: where.c,v 1.203 2006/01/24 12:09:20 danielk1977 Exp $ ** $Id: where.c,v 1.204 2006/02/01 02:45:02 drh Exp $
*/ */
#include "sqliteInt.h" #include "sqliteInt.h"
@ -1500,9 +1500,16 @@ WhereInfo *sqlite3WhereBegin(
double lowestCost; /* Cost of the pBest */ double lowestCost; /* Cost of the pBest */
int bestJ = 0; /* The value of j */ int bestJ = 0; /* The value of j */
Bitmask m; /* Bitmask value for j or bestJ */ Bitmask m; /* Bitmask value for j or bestJ */
int once = 0; /* True when first table is seen */
lowestCost = SQLITE_BIG_DBL; lowestCost = SQLITE_BIG_DBL;
for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){ for(j=iFrom, pTabItem=&pTabList->a[j]; j<pTabList->nSrc; j++, pTabItem++){
if( once &&
((pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0
|| (j>0 && (pTabItem[-1].jointype & (JT_LEFT|JT_CROSS))!=0))
){
break;
}
m = getMask(&maskSet, pTabItem->iCursor); m = getMask(&maskSet, pTabItem->iCursor);
if( (m & notReady)==0 ){ if( (m & notReady)==0 ){
if( j==iFrom ) iFrom++; if( j==iFrom ) iFrom++;
@ -1512,17 +1519,13 @@ WhereInfo *sqlite3WhereBegin(
(i==0 && ppOrderBy) ? *ppOrderBy : 0, (i==0 && ppOrderBy) ? *ppOrderBy : 0,
&pIdx, &flags, &nEq); &pIdx, &flags, &nEq);
if( cost<lowestCost ){ if( cost<lowestCost ){
once = 1;
lowestCost = cost; lowestCost = cost;
pBest = pIdx; pBest = pIdx;
bestFlags = flags; bestFlags = flags;
bestNEq = nEq; bestNEq = nEq;
bestJ = j; bestJ = j;
} }
if( (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0
|| (j>0 && (pTabItem[-1].jointype & (JT_LEFT|JT_CROSS))!=0)
){
break;
}
} }
TRACE(("*** Optimizer choose table %d for loop %d\n", bestJ, TRACE(("*** Optimizer choose table %d for loop %d\n", bestJ,
pLevel-pWInfo->a)); pLevel-pWInfo->a));

49
test/where3.test Normal file
View File

@ -0,0 +1,49 @@
# 2006 January 31
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the join reordering optimization
# in cases that include a LEFT JOIN.
#
# $Id: where3.test,v 1.1 2006/02/01 02:45:02 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# The following is from ticket #1652.
#
# A comma join then a left outer join: A,B left join C.
# Arrange indices so that the B table is chosen to go first.
# Also put an index on C, but make sure that A is chosen before C.
#
do_test where3-1.1 {
execsql {
CREATE TABLE t1(a, b);
CREATE TABLE t2(p, q);
CREATE TABLE t3(x, y);
INSERT INTO t1 VALUES(111,'one');
INSERT INTO t1 VALUES(222,'two');
INSERT INTO t1 VALUES(333,'three');
INSERT INTO t2 VALUES(1,111);
INSERT INTO t2 VALUES(2,222);
INSERT INTO t2 VALUES(4,444);
CREATE INDEX t2i1 ON t2(p);
INSERT INTO t3 VALUES(999,'nine');
CREATE INDEX t3i1 ON t3(x);
SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x WHERE p=2 AND a=q;
}
} {222 two 2 222 {} {}}
finish_test