1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-11-18 10:21:03 +03:00

When planning a query using sorting, resolve ties in the solver by selecting

loop plans with the smaller unsorted cost.

FossilOrigin-Name: f261678c90297f9767040c577796cc1d4db598aa44b0de6906cb73eaef377dee
This commit is contained in:
drh
2017-05-22 00:45:15 +00:00
parent e46515b53f
commit 78436d4c66
3 changed files with 33 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
C Prevent\sa\spossible\sNULL\spointer\sdereference\sin\sthe\sOP_Found\sopcode\sthat\ncan\sfollow\san\sOOM\serror.\s\sProblem\sfound\sby\sOSS-Fuzz. C When\splanning\sa\squery\susing\ssorting,\sresolve\sties\sin\sthe\ssolver\sby\sselecting\nloop\splans\swith\sthe\ssmaller\sunsorted\scost.
D 2017-05-19T22:51:00.217 D 2017-05-22T00:45:15.475
F Makefile.in 1cc758ce3374a32425e4d130c2fe7b026b20de5b8843243de75f087c0a2661fb F Makefile.in 1cc758ce3374a32425e4d130c2fe7b026b20de5b8843243de75f087c0a2661fb
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc 8eeb80162074004e906b53d7340a12a14c471a83743aab975947e95ce061efcc F Makefile.msc 8eeb80162074004e906b53d7340a12a14c471a83743aab975947e95ce061efcc
@@ -484,7 +484,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
F src/wal.c 40c543f0a2195d1b0dc88ef12142bea690009344 F src/wal.c 40c543f0a2195d1b0dc88ef12142bea690009344
F src/wal.h 06b2a0b599cc0f53ea97f497cf8c6b758c999f71 F src/wal.h 06b2a0b599cc0f53ea97f497cf8c6b758c999f71
F src/walker.c b71a992b413b3a022572eccf29ef4b4890223791 F src/walker.c b71a992b413b3a022572eccf29ef4b4890223791
F src/where.c a354396efcd326340ce17ac0c8af52ca8188ce65e26fda30e5af286dfc2d59f8 F src/where.c ba36d01178f5957523cc600b95173057ae9a13ab378b4fc6ab5e14776767057b
F src/whereInt.h 2a4b634d63ce488b46d4b0da8f2eaa8f9aeab202bc25ef76f007de5e3fba1f20 F src/whereInt.h 2a4b634d63ce488b46d4b0da8f2eaa8f9aeab202bc25ef76f007de5e3fba1f20
F src/wherecode.c 8ad48867660519e262a401720845dc76934f86f558ec9606335fafcd7a2554f8 F src/wherecode.c 8ad48867660519e262a401720845dc76934f86f558ec9606335fafcd7a2554f8
F src/whereexpr.c e913aaa7b73ffcce66abcea5f197e2c538d48b5df78d0b7bba8ff4d73cc2e745 F src/whereexpr.c e913aaa7b73ffcce66abcea5f197e2c538d48b5df78d0b7bba8ff4d73cc2e745
@@ -1580,7 +1580,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 946b87a5282f00d8c532a51f4390e6de781b9a0d6626cb3c077c5622895dc540 P c2de178fe7e2e4e0d764e7e6ac637cfc8c053580c43f7246318dafad2974de3c
R 892c6abfc58a4043f0c864668f2509c6 Q +962531e7c1f3ff604271ddf9f47b6234dfd47702ccf24849f55b80814e7be267
R 0174bb863aa0f92aa80b928036d9451c
U drh U drh
Z fe52b75d8efb4b802f93708f3666bfdc Z 7c43670f11f196aa8059f227597167dc

View File

@@ -1 +1 @@
c2de178fe7e2e4e0d764e7e6ac637cfc8c053580c43f7246318dafad2974de3c f261678c90297f9767040c577796cc1d4db598aa44b0de6906cb73eaef377dee

View File

@@ -4003,8 +4003,8 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
** this candidate as not viable. */ ** this candidate as not viable. */
#ifdef WHERETRACE_ENABLED /* 0x4 */ #ifdef WHERETRACE_ENABLED /* 0x4 */
if( sqlite3WhereTrace&0x4 ){ if( sqlite3WhereTrace&0x4 ){
sqlite3DebugPrintf("Skip %s cost=%-3d,%3d order=%c\n", sqlite3DebugPrintf("Skip %s cost=%-3d,%3d,%3d order=%c\n",
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
isOrdered>=0 ? isOrdered+'0' : '?'); isOrdered>=0 ? isOrdered+'0' : '?');
} }
#endif #endif
@@ -4022,26 +4022,36 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
pTo = &aTo[jj]; pTo = &aTo[jj];
#ifdef WHERETRACE_ENABLED /* 0x4 */ #ifdef WHERETRACE_ENABLED /* 0x4 */
if( sqlite3WhereTrace&0x4 ){ if( sqlite3WhereTrace&0x4 ){
sqlite3DebugPrintf("New %s cost=%-3d,%3d order=%c\n", sqlite3DebugPrintf("New %s cost=%-3d,%3d,%3d order=%c\n",
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
isOrdered>=0 ? isOrdered+'0' : '?'); isOrdered>=0 ? isOrdered+'0' : '?');
} }
#endif #endif
}else{ }else{
/* Control reaches here if best-so-far path pTo=aTo[jj] covers the /* Control reaches here if best-so-far path pTo=aTo[jj] covers the
** same set of loops and has the sam isOrdered setting as the ** same set of loops and has the same isOrdered setting as the
** candidate path. Check to see if the candidate should replace ** candidate path. Check to see if the candidate should replace
** pTo or if the candidate should be skipped */ ** pTo or if the candidate should be skipped.
if( pTo->rCost<rCost || (pTo->rCost==rCost && pTo->nRow<=nOut) ){ **
** The conditional is an expanded vector comparison equivalent to:
** (pTo->rCost,pTo->nRow,pTo->rUnsorted) <= (rCost,nOut,rUnsorted)
*/
if( pTo->rCost<rCost
|| (pTo->rCost==rCost
&& (pTo->nRow<nOut
|| (pTo->nRow==nOut && pTo->rUnsorted<=rUnsorted)
)
)
){
#ifdef WHERETRACE_ENABLED /* 0x4 */ #ifdef WHERETRACE_ENABLED /* 0x4 */
if( sqlite3WhereTrace&0x4 ){ if( sqlite3WhereTrace&0x4 ){
sqlite3DebugPrintf( sqlite3DebugPrintf(
"Skip %s cost=%-3d,%3d order=%c", "Skip %s cost=%-3d,%3d,%3d order=%c",
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
isOrdered>=0 ? isOrdered+'0' : '?'); isOrdered>=0 ? isOrdered+'0' : '?');
sqlite3DebugPrintf(" vs %s cost=%-3d,%d order=%c\n", sqlite3DebugPrintf(" vs %s cost=%-3d,%3d,%3d order=%c\n",
wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
} }
#endif #endif
/* Discard the candidate path from further consideration */ /* Discard the candidate path from further consideration */
@@ -4054,12 +4064,12 @@ static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
#ifdef WHERETRACE_ENABLED /* 0x4 */ #ifdef WHERETRACE_ENABLED /* 0x4 */
if( sqlite3WhereTrace&0x4 ){ if( sqlite3WhereTrace&0x4 ){
sqlite3DebugPrintf( sqlite3DebugPrintf(
"Update %s cost=%-3d,%3d order=%c", "Update %s cost=%-3d,%3d,%3d order=%c",
wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, wherePathName(pFrom, iLoop, pWLoop), rCost, nOut, rUnsorted,
isOrdered>=0 ? isOrdered+'0' : '?'); isOrdered>=0 ? isOrdered+'0' : '?');
sqlite3DebugPrintf(" was %s cost=%-3d,%3d order=%c\n", sqlite3DebugPrintf(" was %s cost=%-3d,%3d,%3d order=%c\n",
wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow, wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?'); pTo->rUnsorted, pTo->isOrdered>=0 ? pTo->isOrdered+'0' : '?');
} }
#endif #endif
} }