From ed808acd1bc3befbdb43cf8258addeccbcf99a50 Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 15 Apr 2010 13:29:37 +0000 Subject: [PATCH 1/3] The query planner fix of check-in [33b1f584ef] should have been on the trunk. FossilOrigin-Name: f538d759beda67e042b66181b65bd432488f7650 --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/where.c | 21 ++++++++++++++++----- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/manifest b/manifest index 00193d3865..eae8fb2fb8 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Further\srefinements\sto\stable\sorder\sselection\son\sjoin\squery\splanning. -D 2010-04-15T01:04:54 +C The\squery\splanner\sfix\sof\scheck-in\s[33b1f584ef]\sshould\shave\sbeen\son\sthe\strunk. +D 2010-04-15T13:29:38 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 4f2f967b7e58a35bb74fb7ec8ae90e0f4ca7868b F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -225,7 +225,7 @@ F src/vdbemem.c 2a82f455f6ca6f78b59fb312f96054c04ae0ead1 F src/vdbetrace.c 864cef96919323482ebd9986f2132435115e9cc2 F src/vtab.c a0f8a40274e4261696ef57aa806de2776ab72cda F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f -F src/where.c 74ff6f0d96b3190f8abe2ceaa09858441749e8ff +F src/where.c faadd9c2bf08868e5135192b44e0d753e363a885 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 F test/all.test 14165b3e32715b700b5f0cbf8f6e3833dda0be45 @@ -800,14 +800,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P b87cb0c2bd9c52a938795a974e101879b81210e3 -R ac19ec910df4152a1528b71c1365be54 +P defaf0d99a807027f8883bf821b6482025f9f54e +R a8916448766fe2dc0eee7acf074d6482 U drh -Z 406deb0be8ed4b7e4bb6e640f59e7f15 +Z 28450adfa9ef42da4766c0ed11d89126 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFLxmY4oxKgR168RlERAsvwAJ4tlJVE6cdAiLgjUGC8Snj86Xjm4ACeIy59 -w6HyVplE+rrSW13BtidRydE= -=uibN +iD8DBQFLxxTGoxKgR168RlERAr/aAJwLuSD1d16efrb/1zmza7D1Lt3gJwCeMzR0 +I06a/9mduq2NTfZ7gOTnT48= +=kB+a -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index ddeecd995c..a17fe9f4af 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -defaf0d99a807027f8883bf821b6482025f9f54e \ No newline at end of file +f538d759beda67e042b66181b65bd432488f7650 \ No newline at end of file diff --git a/src/where.c b/src/where.c index 77ecdfef46..e13ca51d03 100644 --- a/src/where.c +++ b/src/where.c @@ -2737,12 +2737,23 @@ static void bestBtreeIndex( ** matters if the current index is the least costly, so do not bother ** with this step if we already know this index will not be chosen. ** Also, never reduce the output row count below 2 using this step. + ** + ** Do not reduce the output row count if pSrc is the only table that + ** is notReady; if notReady is a power of two. This will be the case + ** when the main sqlite3WhereBegin() loop is scanning for a table with + ** and "optimal" index, and on such a scan the output row count + ** reduction is not valid because it does not update the "pCost->used" + ** bitmap. The notReady bitmap will also be a power of two when we + ** are scanning for the last table in a 64-way join. We are willing + ** to bypass this optimization in that corner case. */ - if( nRow>2 && cost<=pCost->rCost ){ - int k; - int nSkipEq = nEq; - int nSkipRange = nBound; - Bitmask thisTab = getMask(pWC->pMaskSet, iCur); + if( nRow>2 && cost<=pCost->rCost && (notReady & (notReady-1))!=0 ){ + int k; /* Loop counter */ + int nSkipEq = nEq; /* Number of == constraints to skip */ + int nSkipRange = nBound; /* Number of < constraints to skip */ + Bitmask thisTab; /* Bitmap for pSrc */ + + thisTab = getMask(pWC->pMaskSet, iCur); for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){ if( pTerm->wtFlags & TERM_VIRTUAL ) continue; if( (pTerm->prereqAll & notReady)!=thisTab ) continue; From f391327824be0074aac69b1b17a4a01400526c5a Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 15 Apr 2010 23:24:29 +0000 Subject: [PATCH 2/3] Disable query flattening when the outer query is a compound SELECT and the inner query has a LIMIT clause. Ticket [02a8e81d44]. FossilOrigin-Name: f96782b389b5b97b488dc5814f7082e0393f64cd --- manifest | 19 ++++++++++--------- manifest.uuid | 2 +- src/select.c | 6 +++--- test/tkt-02a8e81d44.test | 31 +++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 test/tkt-02a8e81d44.test diff --git a/manifest b/manifest index eae8fb2fb8..d36430f66f 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C The\squery\splanner\sfix\sof\scheck-in\s[33b1f584ef]\sshould\shave\sbeen\son\sthe\strunk. -D 2010-04-15T13:29:38 +C Disable\squery\sflattening\swhen\sthe\souter\squery\sis\sa\scompound\sSELECT\sand\nthe\sinner\squery\shas\sa\sLIMIT\sclause.\s\sTicket\s[02a8e81d44]. +D 2010-04-15T23:24:29 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 4f2f967b7e58a35bb74fb7ec8ae90e0f4ca7868b F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -167,7 +167,7 @@ F src/printf.c 5f5b65a83e63f2096a541a340722a509fa0240a7 F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50 F src/resolve.c ac5f1a713cd1ae77f08b83cc69581e11bf5ae6f9 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697 -F src/select.c 5a08245cb18b7ddf2456274653599cbf738d3830 +F src/select.c b7c9a40bc1567bceff52ad4b73108734ee4bf268 F src/shell.c c40427c7245535a04a9cb4a417b6cc05c022e6a4 F src/sqlite.h.in 0aeb53778e1591032fdf12ba7aa059250beb480a F src/sqlite3ext.h 69dfb8116af51b84a029cddb3b35062354270c89 @@ -614,6 +614,7 @@ F test/thread2.test 6e0997f7beabb6a7e471bd18740ed04805c785f4 F test/thread_common.tcl b65e6b1d1d90dc885e10ad080896c6c56eef0819 F test/threadtest1.c 6029d9c5567db28e6dc908a0c63099c3ba6c383b F test/threadtest2.c ace893054fa134af3fc8d6e7cfecddb8e3acefb9 +F test/tkt-02a8e81d44.test 58494de77be2cf249228ada3f313fa399821c6ab F test/tkt-2ea2425d34.test 1cf13e6f75d149b3209a0cb32927a82d3d79fb28 F test/tkt-31338dca7e.test 5741cd48de500347a437ba1be58c8335e83c5a5e F test/tkt-3fe897352e.test 10de1a67bd5c66b238a4c96abe55531b37bb4f00 @@ -800,14 +801,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P defaf0d99a807027f8883bf821b6482025f9f54e -R a8916448766fe2dc0eee7acf074d6482 +P f538d759beda67e042b66181b65bd432488f7650 +R 34d0a17088c0fdff5f60f2ea4cf2882d U drh -Z 28450adfa9ef42da4766c0ed11d89126 +Z b46bcb95ee573d80fced144df93d24f1 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFLxxTGoxKgR168RlERAr/aAJwLuSD1d16efrb/1zmza7D1Lt3gJwCeMzR0 -I06a/9mduq2NTfZ7gOTnT48= -=kB+a +iD8DBQFLx6AxoxKgR168RlERAq3yAJ9VLNfRM+IKVowVDGm7aiEH4HuZmQCaA/ai +zk4R64zbGoGnQ/UpEadFxjM= +=0vth -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index a17fe9f4af..7b9f5249de 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f538d759beda67e042b66181b65bd432488f7650 \ No newline at end of file +f96782b389b5b97b488dc5814f7082e0393f64cd \ No newline at end of file diff --git a/src/select.c b/src/select.c index 487a8b9207..47e409ffc8 100644 --- a/src/select.c +++ b/src/select.c @@ -2526,8 +2526,8 @@ static void substSelect( ** (14) The subquery does not use OFFSET ** ** (15) The outer query is not part of a compound select or the -** subquery does not have both an ORDER BY and a LIMIT clause. -** (See ticket #2339) +** subquery does not have a LIMIT clause. +** (See ticket #2339 and ticket [02a8e81d44]). ** ** (16) The outer query is not an aggregate or the subquery does ** not contain ORDER BY. (Ticket #2942) This used to not matter @@ -2610,7 +2610,7 @@ static int flattenSubquery( ** and (14). */ if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ if( pSub->pOffset ) return 0; /* Restriction (14) */ - if( p->pRightmost && pSub->pLimit && pSub->pOrderBy ){ + if( p->pRightmost && pSub->pLimit ){ return 0; /* Restriction (15) */ } if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ diff --git a/test/tkt-02a8e81d44.test b/test/tkt-02a8e81d44.test new file mode 100644 index 0000000000..4a48fb029e --- /dev/null +++ b/test/tkt-02a8e81d44.test @@ -0,0 +1,31 @@ +# 2010 April 15 +# +# 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. +# +# This file implements tests to verify that ticket [02a8e81d44] has been +# fixed. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +do_test tkt-02a838-1.1 { + execsql { + CREATE TABLE t1(a); + INSERT INTO t1 VALUES(1); + INSERT INTO t1 VALUES(2); + INSERT INTO t1 VALUES(4); + INSERT INTO t1 VALUES(5); + SELECT * FROM (SELECT a FROM t1 LIMIT 1) UNION ALL SELECT 3; + } +} {1 3} + +finish_test From 3674bfd1b6cc4524402e169dea419b508d38e306 Mon Sep 17 00:00:00 2001 From: drh Date: Sat, 17 Apr 2010 12:53:19 +0000 Subject: [PATCH 3/3] Change sqlite3_step() so that it automatically calls sqlite3_reset() instead of returning SQLITE_MISUSE when invoked on a prepared statement that previously returned any value other than SQLITE_ROW. FossilOrigin-Name: 3e646e3f4cd0ca288e444561e951cecfdaee2ab5 --- manifest | 24 ++++++++++++------------ manifest.uuid | 2 +- src/sqlite.h.in | 8 ++++++++ src/vdbeapi.c | 9 ++++++--- test/capi2.test | 8 ++++++-- test/fkey2.test | 2 +- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/manifest b/manifest index d36430f66f..9fe9013723 100644 --- a/manifest +++ b/manifest @@ -1,8 +1,8 @@ -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 -C Disable\squery\sflattening\swhen\sthe\souter\squery\sis\sa\scompound\sSELECT\sand\nthe\sinner\squery\shas\sa\sLIMIT\sclause.\s\sTicket\s[02a8e81d44]. -D 2010-04-15T23:24:29 +C Change\ssqlite3_step()\sso\sthat\sit\sautomatically\scalls\ssqlite3_reset()\sinstead\nof\sreturning\sSQLITE_MISUSE\swhen\sinvoked\son\sa\sprepared\sstatement\sthat\npreviously\sreturned\sany\svalue\sother\sthan\sSQLITE_ROW. +D 2010-04-17T12:53:20 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 4f2f967b7e58a35bb74fb7ec8ae90e0f4ca7868b F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -169,7 +169,7 @@ F src/resolve.c ac5f1a713cd1ae77f08b83cc69581e11bf5ae6f9 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697 F src/select.c b7c9a40bc1567bceff52ad4b73108734ee4bf268 F src/shell.c c40427c7245535a04a9cb4a417b6cc05c022e6a4 -F src/sqlite.h.in 0aeb53778e1591032fdf12ba7aa059250beb480a +F src/sqlite.h.in dc98616304e3e776008655671d81e3ad3028ada7 F src/sqlite3ext.h 69dfb8116af51b84a029cddb3b35062354270c89 F src/sqliteInt.h bd7ff54663bdd5b57e0eb8b49aca5a3a3c60119a F src/sqliteLimit.h 3afab2291762b5d09ae20c18feb8e9fa935a60a6 @@ -218,7 +218,7 @@ F src/vacuum.c b1d542c8919d4d11119f78069e1906a1ad07e0ee F src/vdbe.c 2abd931ea2aec3eacc6426677f40cc5a1071d34e F src/vdbe.h 471f6a3dcec4817ca33596fe7f6654d56c0e75f3 F src/vdbeInt.h 19ebc8c2a2e938340051ee65af3f377fb99102d1 -F src/vdbeapi.c 74c25680046a116b24b95393914d3669c23305dc +F src/vdbeapi.c 11bcc381e81e797fcf3e81fa6a14ec16a04801cc F src/vdbeaux.c 3028b2d50df39697e21263685349b5ff1333a29b F src/vdbeblob.c 5327132a42a91e8b7acfb60b9d2c3b1c5c863e0e F src/vdbemem.c 2a82f455f6ca6f78b59fb312f96054c04ae0ead1 @@ -276,7 +276,7 @@ F test/boundary4.tcl 0bb4b1a94f4fc5ae59b79b9a2b7a140c405e2983 F test/boundary4.test 89e02fa66397b8a325d5eb102b5806f961f8ec4b F test/busy.test 76b4887f8b9160ba903c1ac22e8ff406ad6ae2f0 F test/cache.test 3ff445c445742a7b6b9ba6e1d62a25263f9424b9 -F test/capi2.test 172c717ed101e78e0798dd21b9896a22366f35b4 +F test/capi2.test 00032d7504b9c14f1b36331670c5e7b0f73e3c5d F test/capi3.test 168e2cd66c58c510955b0f299750e4de73b8d952 F test/capi3b.test 664eb55318132f292f2c436f90906f578cad6b97 F test/capi3c.test 493385107dcedfaf4f2b1c3738c8c1fa00362006 @@ -348,7 +348,7 @@ F test/expr.test 9f521ae22f00e074959f72ce2e55d46b9ed23f68 F test/filectrl.test 8923a6dc7630f31c8a9dd3d3d740aa0922df7bf8 F test/filefmt.test 84e3d0fe9f12d0d2ac852465c6f8450aea0d6f43 F test/fkey1.test 01c7de578e11747e720c2d9aeef27f239853c4da -F test/fkey2.test e71f5baf9bb42cdba4700d73cba6f4d82fd6b925 +F test/fkey2.test 82652daf32ec5a40634a10bc8db571b84d39c7fe F test/fkey3.test 42f88d6048d8dc079e2a8cf7baad1cc1483a7620 F test/fkey_malloc.test a5ede29bd2f6e56dea78c3d43fb86dd696c068c8 F test/format4.test 1f0cac8ff3895e9359ed87e41aaabee982a812eb @@ -801,14 +801,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P f538d759beda67e042b66181b65bd432488f7650 -R 34d0a17088c0fdff5f60f2ea4cf2882d +P f96782b389b5b97b488dc5814f7082e0393f64cd +R b3683c8d5f87dead098717870c446ce4 U drh -Z b46bcb95ee573d80fced144df93d24f1 +Z 68bc4fd82825f4b7f1278d3f78ee95b8 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) -iD8DBQFLx6AxoxKgR168RlERAq3yAJ9VLNfRM+IKVowVDGm7aiEH4HuZmQCaA/ai -zk4R64zbGoGnQ/UpEadFxjM= -=0vth +iD8DBQFLya9GoxKgR168RlERAvQaAJwLUmtTGSRsZdMt+rOX4V9Acu7enQCdFgG+ +yMxp/Ep2vaOwFANf9gUAX1Q= +=yLEB -----END PGP SIGNATURE----- diff --git a/manifest.uuid b/manifest.uuid index 7b9f5249de..9d8bb1e648 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f96782b389b5b97b488dc5814f7082e0393f64cd \ No newline at end of file +3e646e3f4cd0ca288e444561e951cecfdaee2ab5 \ No newline at end of file diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 78f3773e99..c1363031ea 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -2885,6 +2885,14 @@ const void *sqlite3_column_decltype16(sqlite3_stmt*,int); ** be the case that the same database connection is being used by two or ** more threads at the same moment in time. ** +** For all versions of SQLite up to and including 3.6.23.1, it was required +** after sqlite3_step() returned anything other than [SQLITE_ROW] that +** [sqlite3_reset()] be called before any subsequent invocation of +** sqlite3_step(). Failure to invoke [sqlite3_reset()] in this way would +** result in an [SQLITE_MISUSE] return from sqlite3_step(). But after +** version 3.6.23.1, sqlite3_step() began calling [sqlite3_reset()] +** automatically in this circumstance rather than returning [SQLITE_MISUSE]. +** ** Goofy Interface Alert: In the legacy interface, the sqlite3_step() ** API always returns a generic error code, [SQLITE_ERROR], following any ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call diff --git a/src/vdbeapi.c b/src/vdbeapi.c index 125f325d18..29a2a429bc 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -321,9 +321,12 @@ static int sqlite3Step(Vdbe *p){ assert(p); if( p->magic!=VDBE_MAGIC_RUN ){ - sqlite3_log(SQLITE_MISUSE, - "attempt to step a halted statement: [%s]", p->zSql); - return SQLITE_MISUSE_BKPT; + /* We used to require that sqlite3_reset() be called before retrying + ** sqlite3_step() after any error. But after 3.6.23, we changed this + ** so that sqlite3_reset() would be called automatically instead of + ** throwing the error. + */ + sqlite3_reset((sqlite3_stmt*)p); } /* Check that malloc() has not failed. If it has, return early. */ diff --git a/test/capi2.test b/test/capi2.test index 8bcb25a834..b4aa970c8e 100644 --- a/test/capi2.test +++ b/test/capi2.test @@ -71,15 +71,19 @@ do_test capi2-1.6 { do_test capi2-1.7 { list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM] } {2 {} {name rowid text INTEGER}} -do_test capi2-1.8-misuse { + +# This used to be SQLITE_MISUSE. But now we automatically reset prepared +# statements. +do_test capi2-1.8 { sqlite3_step $VM -} {SQLITE_MISUSE} +} {SQLITE_ROW} # Update: In v2, once SQLITE_MISUSE is returned the statement handle cannot # be interrogated for more information. However in v3, since the column # count, names and types are determined at compile time, these are still # accessible after an SQLITE_MISUSE error. do_test capi2-1.9 { + sqlite3_reset $VM list [sqlite3_column_count $VM] [get_row_values $VM] [get_column_names $VM] } {2 {} {name rowid text INTEGER}} do_test capi2-1.10 { diff --git a/test/fkey2.test b/test/fkey2.test index 10a624ef0a..2134431b02 100644 --- a/test/fkey2.test +++ b/test/fkey2.test @@ -1410,7 +1410,7 @@ do_test fkey2-17.1.2 { } {SQLITE_CONSTRAINT} do_test fkey2-17.1.3 { sqlite3_step $STMT -} {SQLITE_MISUSE} +} {SQLITE_CONSTRAINT} do_test fkey2-17.1.4 { sqlite3_finalize $STMT } {SQLITE_CONSTRAINT}