From: drh Date: Thu, 13 Aug 2015 20:07:13 +0000 (+0000) Subject: Merge in all the trunk changes from the previous year. This breaks the X-Git-Tag: version-3.10.0~191^2~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bec2476afc536aaa57e4813c60c019d30c2b602e;p=thirdparty%2Fsqlite.git Merge in all the trunk changes from the previous year. This breaks the cursor-hint mechanism, but provides a baseline for trouble-shooting. FossilOrigin-Name: 82a7a61bc0883b1e7432548e4890791717aa1bb3 --- bec2476afc536aaa57e4813c60c019d30c2b602e diff --cc main.mk index 3523243994,f3bdae6a83..44f4bbf094 --- a/main.mk +++ b/main.mk @@@ -240,9 -259,9 +259,10 @@@ TESTSRC = $(TOP)/src/test_autoext.c \ $(TOP)/src/test_async.c \ $(TOP)/src/test_backup.c \ + $(TOP)/src/test_blob.c \ $(TOP)/src/test_btree.c \ $(TOP)/src/test_config.c \ + $(TOP)/src/test_cursorhint.c \ $(TOP)/src/test_demovfs.c \ $(TOP)/src/test_devsym.c \ $(TOP)/src/test_fs.c \ diff --cc manifest index 6cff144128,3ec7d5e743..b82bc92d48 --- a/manifest +++ b/manifest @@@ -1,14 -1,14 +1,14 @@@ - C Add\ssimple\stests\sfor\snew\ssqlite3BtreeCursorHint()\sfunctionality. - D 2014-07-15T11:59:44.612 -C Add\sthe\ssqlite3rbu_savestate()\sfunction\sto\sthe\srbu\sextension.\sTo\sforce\srbu\sto\ssave\sits\sstate\sto\sdisk\swithout\sclosing\sthe\ssqlite3rbu*\shandle. -D 2015-08-13T18:26:09.242 ++C Merge\sin\sall\sthe\strunk\schanges\sfrom\sthe\sprevious\syear.\s\sThis\sbreaks\sthe\ncursor-hint\smechanism,\sbut\sprovides\sa\sbaseline\sfor\strouble-shooting. ++D 2015-08-13T20:07:13.412 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f - F Makefile.in e1a9b4258bbde53f5636f4e238c65b7e11459e2b + F Makefile.in 2fc9ca6bf5949d415801c007ed3004a4bdb7c380 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 - F Makefile.msc bb1f271c8ee9773489c89be00f3f8ad7ed7ae8e0 - F Makefile.vxworks db21ed42a01d5740e656b16f92cb5d8d5e5dd315 - F README cd04a36fbc7ea56932a4052d7d0b7f09f27c33d6 - F VERSION 8ed548d87d0a27fd7d7620476f9e25f9fa742d73 + F Makefile.msc 5f7861c62c41fe8e3205ef14b90ebed28fa21f1b + F Makefile.vxworks e1b65dea203f054e71653415bd8f96dcaed47858 -F README.md 8ecc12493ff9f820cdea6520a9016001cb2e59b7 ++F README.md 8ecc12493ff9f820cdea6520a9016001cb2e59b7 w README + F VERSION ccfc4d1576dbfdeece0a4372a2e6a2e37d3e7975 F aclocal.m4 a5c22d164aff7ed549d53a90fa56d56955281f50 - F addopcodes.awk 87ca612393d0f439550634bd2c156ea9ff6195ae + F addopcodes.awk 9eb448a552d5c0185cf62c463f9c173cedae3811 F art/sqlite370.eps aa97a671332b432a54e1d74ff5e8775be34200c2 F art/sqlite370.ico af56c1d00fee7cd4753e8631ed60703ed0fc6e90 F art/sqlite370.jpg d512473dae7e378a67e28ff96a34da7cb331def2 @@@ -23,15 -23,15 +23,15 @@@ F autoconf/depcomp 0b26f101e3bc9fd1ff0b F autoconf/install-sh 06ee6336e63bb845c8439d777c32eb2eccc4fbf1 x F autoconf/ltmain.sh 7a658a24028f02331c1d2446562758083c5eadd1 F autoconf/missing d7c9981a81af13370d4ed152b24c0a82b7028585 x - F autoconf/tea/Makefile.in 5c3b0bdfb66c20d55ebff59d1718864461570ca9 + F autoconf/tea/Makefile.in b438a7020446c8a8156e8d97c8914a04833da6fd F autoconf/tea/README 3e9a3c060f29a44344ab50aec506f4db903fb873 F autoconf/tea/aclocal.m4 52c47aac44ce0ddb1f918b6993e8beb8eee88f43 - F autoconf/tea/configure.in e0466b881b53f31f5a4a69e7a91ad130902fb359 - F autoconf/tea/doc/sqlite3.n e268faa1691c33663d3a7faf9fa30673d14bd879 -F autoconf/tea/configure.ac 93d43c79e936fb16556e22498177d7e8571efa04 ++F autoconf/tea/configure.ac 93d43c79e936fb16556e22498177d7e8571efa04 w autoconf/tea/configure.in + F autoconf/tea/doc/sqlite3.n e1fe45d4f5286ee3d0ccc877aca2a0def488e9bb F autoconf/tea/license.terms 13bd403c9610fd2b76ece0ab50c4c5eda933d523 F autoconf/tea/pkgIndex.tcl.in 3ef61715cf1c7bdcff56947ffadb26bc991ca39d - F autoconf/tea/tclconfig/install-sh b087e5c4b92820c60bffb74acc1d9c2d40d80b8f - F autoconf/tea/tclconfig/tcl.m4 a68179d7cc45524fa59db428a36610e20bdba808 + F autoconf/tea/tclconfig/install-sh bdd5e293591621ae60d9824d86a4b1c5f22c3d00 + F autoconf/tea/tclconfig/tcl.m4 66ddf0a5d5e4b1d29bff472c0985fd7fa89d0fb5 F autoconf/tea/win/makefile.vc f89d0184d0eee5f7e356ea407964dcd139939928 F autoconf/tea/win/nmakehlp.c 2070e086f39866b353a482d3a14dedaf26196506 F autoconf/tea/win/rules.vc c511f222b80064096b705dbeb97060ee1d6b6d63 @@@ -127,196 -235,214 +235,215 @@@ F ext/rtree/rtree2.test acbb3a4ce0f4fbc F ext/rtree/rtree3.test a494da55c30ee0bc9b01a91c80c81b387b22d2dc F ext/rtree/rtree4.test c8fe384f60ebd49540a5fecc990041bf452eb6e0 F ext/rtree/rtree5.test 6a510494f12454bf57ef28f45bc7764ea279431e - F ext/rtree/rtree6.test fe0bd377a21c68ce2826129d14354c884cb1f354 + F ext/rtree/rtree6.test 773a90db2dce6a8353dd0d5b64bca69b29761196 F ext/rtree/rtree7.test 1fa710b9e6bf997a0c1a537b81be7bb6fded1971 F ext/rtree/rtree8.test db79c812f9e4a11f9b1f3f9934007884610a713a - F ext/rtree/rtree9.test d86ebf08ff6328895613ed577dd8a2a37c472c34 + F ext/rtree/rtree9.test b5eb13849545dfd271a54ff16784cb00d8792aea F ext/rtree/rtreeA.test ace05e729a36e342d40cf94e9efc7b4723d9dcdf - F ext/rtree/rtreeB.test 983e567b49b5dca165940f66b87e161aa30e82b2 - F ext/rtree/rtreeC.test 03975565f40a0bee165f613143e4dec716dd5a59 + F ext/rtree/rtreeB.test c85f9ce78766c4e68b8b89fbf2979ee9cfa82b4e + F ext/rtree/rtreeC.test 90aaaffe2fd4f0dcd12289cad5515f6d41f45ffd + F ext/rtree/rtreeD.test 636630357638f5983701550b37f0f5867130d2ca + F ext/rtree/rtreeE.test 45a147a64a76306172819562309681d8e90f94bb + F ext/rtree/rtreeF.test 66deb9fd1611c7ca2e374adba63debdc2dbb12b4 F ext/rtree/rtree_perf.tcl 6c18c1f23cd48e0f948930c98dfdd37dfccb5195 F ext/rtree/rtree_util.tcl 06aab2ed5b826545bf215fff90ecb9255a8647ea - F ext/rtree/sqlite3rtree.h c34c1e41d1ab80bb8ad09aae402c9c956871a765 + F ext/rtree/sqlite3rtree.h 9c5777af3d2921c7b4ae4954e8e5697502289d28 F ext/rtree/tkt3363.test 142ab96eded44a3615ec79fba98c7bde7d0f96de F ext/rtree/viewrtree.tcl eea6224b3553599ae665b239bd827e182b466024 + F ext/userauth/sqlite3userauth.h 19cb6f0e31316d0ee4afdfb7a85ef9da3333a220 + F ext/userauth/user-auth.txt e6641021a9210364665fe625d067617d03f27b04 + F ext/userauth/userauth.c 5fa3bdb492f481bbc1709fc83c91ebd13460c69e F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 - F magic.txt f439556c5ce01ced70987e5ee86549a45165d9ff - F main.mk a6fa2dee82ed65ef48edc82baa0b480366f53212 - F mkdll.sh 7d09b23c05d56532e9d44a50868eb4b12ff4f74a - F mkextu.sh 416f9b7089d80e5590a29692c9d9280a10dbad9f - F mkextw.sh d2a981497b404d6498f5ff3e3b1f3816bdfcb338 + F magic.txt 8273bf49ba3b0c8559cb2774495390c31fd61c60 -F main.mk 73167b34b0e67c0be32c1da2d988a376851c9ab1 ++F main.mk 389329247c56b23329a9285155106cd6b6ebad42 F mkopcodec.awk c2ff431854d702cdd2d779c9c0d1f58fa16fa4ea - F mkopcodeh.awk c6b3fa301db6ef7ac916b14c60868aeaec1337b5 + F mkopcodeh.awk 0e7f04a8eb90f92259e47d80110e4e98d7ce337a F mkso.sh fd21c06b063bb16a5d25deea1752c2da6ac3ed83 F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271 F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504 - F mptest/crash01.test cce8e306d8596d5a2e497e27112dae1f6e5e3538 + F mptest/crash01.test 61e61469e257df0850df4293d7d4d6c2af301421 F mptest/crash02.subtest f4ef05adcd15d60e5d2bd654204f2c008b519df8 - F mptest/mptest.c 499a74af4be293b7c1c7c3d40f332b67227dd739 - F mptest/multiwrite01.test 499ad0310da8dff8e8f98d2e272fc2a8aa741b2e + F mptest/mptest.c fca59f0a922e03f95ed17c44b1515ed37a841c81 + F mptest/multiwrite01.test dab5c5f8f9534971efce679152c5146da265222d F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b - F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc + F sqlite3.1 fc7ad8990fc8409983309bb80de8c811a7506786 F sqlite3.pc.in 48fed132e7cb71ab676105d2a4dc77127d8c1f3a - F src/alter.c 2af0330bb1b601af7a7789bf7229675fd772a083 - F src/analyze.c 581d5c18ce89c6f45d4dca65914d0de5b4dad41f - F src/attach.c 0a17c9364895316ca4f52d06a97a72c0af1ae8b3 - F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34 - F src/backup.c 1809a7caa2504233bdddd12f5018422421789537 - F src/bitvec.c 19a4ba637bd85f8f63fc8c9bae5ade9fb05ec1cb - F src/btmutex.c 976f45a12e37293e32cae0281b15a21d48a8aaa7 - F src/btree.c 9e6d189894f54744d1a20c63de5afb66342f0904 - F src/btree.h 450f1c6022ec89da529a57f3e704e87c6c14b34d - F src/btreeInt.h f038e818bfadf75afbd09819ed93c26a333d39e0 - F src/build.c 9b40580b62916612678bdb69ce0286e39c29a862 - F src/callback.c f99a8957ba2adf369645fac0db09ad8adcf1caa2 - F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac - F src/ctime.c 77779efbe78dd678d84bfb4fc2e87b6b6ad8dccd - F src/date.c 593c744b2623971e45affd0bde347631bdfa4625 - F src/delete.c b36db1f79ee50eaca979660c9dd36437f5410b93 - F src/expr.c cdfc50b604ea9c9bbaa44f1f0a5c6cc835e84a32 + F src/alter.c 48e14b8aea28dc58baafe3cfcb8889c086b7744a + F src/analyze.c f89727c36f997bd2bf6c5e546c2f51dc94e6f2a4 + F src/attach.c e944d0052b577703b9b83aac1638452ff42a8395 + F src/auth.c b56c78ebe40a2110fd361379f7e8162d23f92240 + F src/backup.c 4d9134dc988a87838c06056c89c0e8c4700a0452 + F src/bitvec.c d1f21d7d91690747881f03940584f4cc548c9d3d + F src/btmutex.c 45a968cc85afed9b5e6cf55bf1f42f8d18107f79 -F src/btree.c f48b3ef91676c06a90a8832987ecef6b94c931ee -F src/btree.h 969adc948e89e449220ff0ff724c94bb2a52e9f1 ++F src/btree.c 060f2482ef871363617b13c68a1d1c09faf356cc ++F src/btree.h 88803367b892853c05052e12a946b66508eb6610 + F src/btreeInt.h 8177c9ab90d772d6d2c6c517e05bed774b7c92c0 + F src/build.c 4acc35c4e0a2d94c906abd164568cd6fc989cfbb + F src/callback.c 7b44ce59674338ad48b0e84e7b72f935ea4f68b0 + F src/complete.c addcd8160b081131005d5bc2d34adf20c1c5c92f + F src/ctime.c 5a0b735dc95604766f5dac73973658eef782ee8b + F src/date.c 8ec787fed4929d8ccdf6b1bc360fccc3e1d2ca58 -F src/dbstat.c f402e77e25089c6003d0c60b3233b9b3947d599a ++F src/dbstat.c f402e77e25089c6003d0c60b3233b9b3947d599a w src/test_stat.c + F src/delete.c 8857a6f27560718f65d43bdbec86c967ae1f8dfa -F src/expr.c c5c58e4d01c7ceb2266791d8d877f1b23a88e316 ++F src/expr.c 0cd7fabb57cb42a9eacd7e8224add005786ae428 F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb - F src/fkey.c 2ab0f5384b70594468ef3ac5c7ed8ca24bfd17d5 - F src/func.c ef30d26ae4d79bbc7300c74e77fd117a0ba30235 - F src/global.c 1d7bb7ea8254ae6a68ed9bfaf65fcb3d1690b486 - F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4 - F src/hash.h 8890a25af81fb85a9ad7790d32eedab4b994da22 + F src/fkey.c c9b63a217d86582c22121699a47f22f524608869 + F src/func.c 824bea430d3a2b7dbc62806ad54da8fdb8ed9e3f + F src/global.c 508e4087f7b41d688e4762dcf4d4fe28cfbc87f9 + F src/hash.c 4263fbc955f26c2e8cdc0cf214bc42435aa4e4f5 + F src/hash.h c8f3c31722cf3277d03713909761e152a5b81094 F src/hwtime.h d32741c8f4df852c7d959236615444e2b1063b08 - F src/insert.c 3cf8012325857d162f74389420b14be7976a538d + F src/insert.c 842026863c726df5420f896a8c10eb3e150f9864 F src/journal.c b4124532212b6952f42eb2c12fa3c25701d8ba8d - F src/legacy.c 0df0b1550b9cc1f58229644735e317ac89131f12 - F src/lempar.c cdf0a000315332fc9b50b62f3b5e22e080a0952b - F src/loadext.c 867c7b330b740c6c917af9956b13b81d0a048303 - F src/main.c 355fc9ab213b43a4d8a96aadf2a84622e6668f0a - F src/malloc.c 543a8eb5508eaf4cadf55a9b503379eba2088128 + F src/legacy.c ba1863ea58c4c840335a84ec276fc2b25e22bc4e + F src/lempar.c 92bafa308607dd985ca389a788cd9e0a2b608712 + F src/loadext.c dfcee8c7c032cd0fd55af3e0fc1fcfb01e426df2 + F src/main.c 0a60b7ca8252c3a6f95438fa4ce8fe5b275c69f2 + F src/malloc.c 19461e159bccf0e2cf06a50e867963d0a7b124a8 F src/mem0.c 6a55ebe57c46ca1a7d98da93aaa07f99f1059645 - F src/mem1.c c0c990fcaddff810ea277b4fb5d9138603dd5d4b - F src/mem2.c dce31758da87ec2cfa52ba4c5df1aed6e07d8e8f + F src/mem1.c abe6ee469b6c5a35c7f22bfeb9c9bac664a1c987 + F src/mem2.c f1940d9e91948dd6a908fbb9ce3835c36b5d83c3 F src/mem3.c 61c9d47b792908c532ca3a62b999cf21795c6534 - F src/mem5.c 19d9271cb936742707b6118ed44d779657c7c511 - F src/memjournal.c 0683aac6cab6ec2b5374c0db37c0deb2436a3785 - F src/mutex.c d3b66a569368015e0fcb1ac15f81c119f504d3bc - F src/mutex.h 5bc526e19dccc412b7ff04642f6fdad3fdfdabea - F src/mutex_noop.c 7682796b7d8d39bf1c138248858efcd10c9e1553 - F src/mutex_unix.c c3a4e00f96ba068a8dbef34084465979aaf369cc - F src/mutex_w32.c 6108c88e1cb38d8fbb3534b170793815cbedbf97 - F src/notify.c 976dd0f6171d4588e89e874fcc765e92914b6d30 - F src/os.c b4ad71336fd96f97776f75587cd9e8218288f5be - F src/os.h 4a46270a64e9193af4a0aaa3bc2c66dc07c29b3f - F src/os_common.h 92815ed65f805560b66166e3583470ff94478f04 - F src/os_unix.c 60a7b3b23e6fcf83a50d1e320b280b551724e11f - F src/os_win.c 4323dd0bac4f7a7037fc4cf87fb4692d17f0b108 - F src/pager.c d27dc8aaeac16c90eb0c859c074683bfc4533c41 - F src/pager.h f094af9f6ececfaa8a1e93876905a4f34233fb0c - F src/parse.y acee1a9958539e21263362b194594c5255ad2fca - F src/pcache.c f8043b433a57aba85384a531e3937a804432a346 - F src/pcache.h a5e4f5d9f5d592051d91212c5949517971ae6222 - F src/pcache1.c a467393909a4ed7ca9de066d85ba5c5b04a5be63 - F src/pragma.c 5ab7279d132143feb77f773688a24ab05da75fd7 - F src/prepare.c 359d1a1e9c9bd4488e4dd3a1aaaf2d2ebb9bb768 - F src/printf.c da9119eb31a187a4b99f60aa4a225141c0ebb74b - F src/random.c 0b2dbc37fdfbfa6bd455b091dfcef5bdb32dba68 - F src/resolve.c 7eda9097b29fcf3d2b42fdc17d1de672134e09b6 - F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0 - F src/select.c 44fa5a4a270ce70b59acb616b01bc897e6d1e7ac - F src/shell.c 936a72ff784efff3832cce274a96ed0b036e6758 - F src/sqlite.h.in 125dc0b76f0116f1cd6f13536db52ba981e1c5bd - F src/sqlite3.rc 11094cc6a157a028b301a9f06b3d03089ea37c3e - F src/sqlite3ext.h 886f5a34de171002ad46fae8c36a7d8051c190fc - F src/sqliteInt.h ccf72d12670132233750c6275a8ed978a95839aa - F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d - F src/status.c 7ac05a5c7017d0b9f0b4bcd701228b784f987158 - F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e - F src/tclsqlite.c 571af8d366a8c00106c8c4bfa5b2ab07a9e58baf - F src/test1.c 760e0419705f712d80595f47199568cd7e3b57a4 - F src/test2.c 7355101c085304b90024f2261e056cdff13c6c35 - F src/test3.c 1c0e5d6f080b8e33c1ce8b3078e7013fdbcd560c - F src/test4.c 9b32d22f5f150abe23c1830e2057c4037c45b3df - F src/test5.c a6d1ac55ac054d0b2b8f37b5e655b6c92645a013 - F src/test6.c 4f958b71334695e65746d357dac77709732b28db - F src/test7.c 72b732baa5642f795655ba1126ea032af46ecfd2 - F src/test8.c c7aab1d9fbbf54fc33d43b73aa24aa55f9eaf534 + F src/mem5.c 61eeb90134f9a5be6c2e68d8daae7628b25953fb + F src/memjournal.c 3eb2c0b51adbd869cb6a44780323f05fa904dc85 + F src/msvc.h d9ba56c6851227ab44b3f228a35f3f5772296495 + F src/mutex.c 529e95739f815300a33c73fd8a7d6bdf0c24bd18 + F src/mutex.h 779d588e3b7756ec3ecf7d78cde1d84aba414f85 + F src/mutex_noop.c 9d4309c075ba9cc7249e19412d3d62f7f94839c4 + F src/mutex_unix.c b0d280089df0f49545f1318f45d61d07d2f674a8 + F src/mutex_w32.c b601f9e3073f7bd2c1f42a8c0ce59e42d6a08f85 + F src/notify.c 9711a7575036f0d3040ba61bc6e217f13a9888e7 + F src/os.c 8fd25588eeba74068d41102d26810e216999b6c8 + F src/os.h 3e57a24e2794a94d3cf2342c6d9a884888cd96bf + F src/os_common.h abdb9a191a367793268fe553d25bab894e986a0e + F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa + F src/os_unix.c 388c023582b17890f10c980b30ec1922b471753b + F src/os_win.c 40b3af7a47eb1107d0d69e592bec345a3b7b798a + F src/os_win.h eb7a47aa17b26b77eb97e4823f20a00b8bda12ca + F src/pager.c aa916ca28606ccf4b6877dfc2b643ccbca86589f + F src/pager.h 6d435f563b3f7fcae4b84433b76a6ac2730036e2 + F src/parse.y 6d60dda8f8d418b6dc034f1fbccd816c459983a8 + F src/pcache.c cde06aa50962595e412d497e22fd2e07878ba1f0 + F src/pcache.h 9968603796240cdf83da7e7bef76edf90619cea9 + F src/pcache1.c d08939800abf3031bd0affd5a13fbc4d7ba3fb68 + F src/pragma.c 669bc0fdb3fb5554e18330e8dd9743319bac16f4 + F src/pragma.h 631a91c8b0e6ca8f051a1d8a4a0da4150e04620a + F src/prepare.c 82e5db1013846a819f198336fed72c44c974e7b1 + F src/printf.c 2bc439ff20a4aad0e0ad50a37a67b5eae7d20edc + F src/random.c ba2679f80ec82c4190062d756f22d0c358180696 + F src/resolve.c 2d47554370de8de6dd5be060cef9559eec315005 + F src/rowset.c eccf6af6d620aaa4579bd3b72c1b6395d9e9fa1e -F src/select.c 57ef3d98c4400b93eea318813be41b2af2da2217 ++F src/select.c 00acffc2e87904d9be0ac8e0fa365fd3326fa6fc + F src/shell.c b1f91e60918df3a68efad1e3a11696b9a7e23d23 + F src/sqlite.h.in 447ead0a6b3293206f04a0896553955d07cfb4b9 + F src/sqlite3.rc 992c9f5fb8285ae285d6be28240a7e8d3a7f2bad + F src/sqlite3ext.h a0b948ebc89bac13941254641326a6aa248c2cc4 -F src/sqliteInt.h 9401d7d9124210dfd5d283af45e0addbc1455c2e ++F src/sqliteInt.h b12cb9c5e277061a79d6f81ca3d8bb6ef1d5c6f1 + F src/sqliteLimit.h 216557999cb45f2e3578ed53ebefe228d779cb46 + F src/status.c f266ad8a2892d659b74f0f50cb6a88b6e7c12179 + F src/table.c 51b46b2a62d1b3a959633d593b89bab5e2c9155e -F src/tclsqlite.c d9439b6a910985b7fff43ba6756bcef00de22649 ++F src/tclsqlite.c 72f4dd747d5604a508bbb6935817d3afe5ffd6e2 + F src/test1.c d339ae9b9baf9221c657c9628c9061d88bd831f6 + F src/test2.c 577961fe48961b2f2e5c8b56ee50c3f459d3359d + F src/test3.c 64d2afdd68feac1bb5e2ffb8226c8c639f798622 + F src/test4.c d168f83cc78d02e8d35567bb5630e40dcd85ac1e + F src/test5.c 5a34feec76d9b3a86aab30fd4f6cc9c48cbab4c1 + F src/test6.c 41cacf3b0dd180823919bf9e1fbab287c9266723 + F src/test7.c 9c89a4f1ed6bb13af0ed805b8d782bd83fcd57e3 + F src/test8.c 610e3d523018ca63b08081795e76794a2121ec38 F src/test9.c bea1e8cf52aa93695487badedd6e1886c321ea60 F src/test_async.c 21e11293a2f72080eda70e1124e9102044531cd8 F src/test_autoext.c dea8a01a7153b9adc97bd26161e4226329546e12 - F src/test_backup.c 3875e899222b651e18b662f86e0e50daa946344e - F src/test_btree.c 5b89601dcb42a33ba8b820a6b763cc9cb48bac16 - F src/test_config.c 10d0e00dd6315879a6d9fac20bd063c7bbbfb8f8 - F src/test_cursorhint.c 7c37315e15d6543689e8b48979a8eb370baa3cfc - F src/test_demovfs.c 69b2085076654ebc18014cbc6386f04409c959a9 + F src/test_backup.c 2e6e6a081870150f20c526a2e9d0d29cda47d803 + F src/test_blob.c e5a7a81d61a780da79101aeb1e60d300af169e07 + F src/test_btree.c 2e9978eca99a9a4bfa8cae949efb00886860a64f + F src/test_config.c fb2e5d354d9a077f5fbb261652eff4787deb104f ++F src/test_cursorhint.c bba837177d1693037f5c83dcb443cf453ee656ed + F src/test_demovfs.c 0de72c2c89551629f58486fde5734b7d90758852 F src/test_devsym.c e7498904e72ba7491d142d5c83b476c4e76993bc F src/test_fs.c ced436e3d4b8e4681328409b8081051ce614e28f - F src/test_func.c f8235719dff4bf9ffee04c55a190af8782ce9ab5 + F src/test_func.c f1ac201465472e76a73e2f3695c3553c63e7322a F src/test_hexio.c abfdecb6fa58c354623978efceb088ca18e379cd F src/test_init.c 66b33120ffe9cd853b5a905ec850d51151337b32 - F src/test_intarray.c 87847c71c3c36889c0bcc9c4baf9d31881665d61 - F src/test_intarray.h 2ece66438cfd177b78d1bfda7a4180cd3a10844d - F src/test_journal.c f5c0a05b7b3d5930db769b5ee6c3766dc2221a64 - F src/test_loadext.c df586c27176e3c2cb2e099c78da67bf14379a56e - F src/test_malloc.c 1ff5b1243d96124c9a180f3b89424820a1f337f3 - F src/test_multiplex.c 9f304bf04170c91c0318238d512df2da039eb1c8 - F src/test_multiplex.h 110a8c4d356e0aa464ca8730375608a9a0b61ae1 - F src/test_mutex.c 293042d623ebba969160f471a82aa1551626454f - F src/test_onefile.c 0396f220561f3b4eedc450cef26d40c593c69a25 - F src/test_osinst.c 90a845c8183013d80eccb1f29e8805608516edba + F src/test_intarray.c 870124b95ec4c645d4eb84f15efb7133528fb1a5 + F src/test_intarray.h 9dc57417fb65bc7835cc18548852cc08cc062202 + F src/test_journal.c 5360fbe1d1e4416ca36290562fd5a2e3f70f32aa + F src/test_loadext.c a5251f956ab6af21e138dc1f9c0399394a510cb4 + F src/test_malloc.c 27d9e11b6e9d30267465d41ad81edbe24256408b + F src/test_multiplex.c 9fefd23f6cc3fa9bf0748a5e453167e7b9f193ce + F src/test_multiplex.h c08e4e8f8651f0c5e0509b138ff4d5b43ed1f5d3 + F src/test_mutex.c dbdfaff8580071f2212a0deae3325a93a737819c + F src/test_onefile.c 38f7cbe79d5bafe95bde683cc3a53b8ca16daf10 + F src/test_osinst.c 5423dc1d355f594371f27dd292ca54bd320b8196 F src/test_pcache.c a5cd24730cb43c5b18629043314548c9169abb00 - F src/test_quota.c 30c64f0ef84734f2231a686df41ed882b0c59bc0 - F src/test_quota.h 8761e463b25e75ebc078bd67d70e39b9c817a0cb - F src/test_rtree.c f3d1d12538dccb75fd916e3fa58f250edbdd3b47 - F src/test_schema.c cd12a2223c3a394f4d07bb93bdf6d344c5c121b6 + F src/test_quota.c 180813f43683be5725458fc1ff13ac455d8e722d + F src/test_quota.h 2a8ad1952d1d2ca9af0ce0465e56e6c023b5e15d + F src/test_rtree.c 43fff4c5a01576d6d213f27472598801a247890c + F src/test_schema.c 2bdba21b82f601da69793e1f1d11bf481a79b091 F src/test_server.c a2615049954cbb9cfb4a62e18e2f0616e4dc38fe - F src/test_sqllog.c c1c1bbedbcaf82b93d83e4f9dd990e62476a680e - F src/test_stat.c 9898687a6c2beca733b0dd6fe19163d987826d31 - F src/test_superlock.c 2b97936ca127d13962c3605dbc9a4ef269c424cd - F src/test_syscall.c 16dbe79fb320fadb5acd7a0a59f49e52ab2d2091 + F src/test_sqllog.c b690c12933f50ff46491e0d56a251f84ae16e914 + F src/test_superlock.c 06797157176eb7085027d9dd278c0d7a105e3ec9 + F src/test_syscall.c 2e21ca7f7dc54a028f1967b63f1e76155c356f9b F src/test_tclvar.c f4dc67d5f780707210d6bb0eb6016a431c04c7fa - F src/test_thread.c 1e133a40b50e9c035b00174035b846e7eef481cb - F src/test_vfs.c e72f555ef7a59080f898fcf1a233deb9eb704ea9 - F src/test_vfstrace.c 3a0ab304682fecbceb689e7d9b904211fde11d78 + F src/test_thread.c af391ec03d23486dffbcc250b7e58e073f172af9 + F src/test_vfs.c 3b65d42e18b262805716bd96178c81da8f2d9283 + F src/test_vfstrace.c bab9594adc976cbe696ff3970728830b4c5ed698 F src/test_wsd.c 41cadfd9d97fe8e3e4e44f61a4a8ccd6f7ca8fe9 - F src/tokenize.c ec4c1a62b890bf1dbcdb966399e140b904c700a4 - F src/trigger.c d84e1f3669e9a217731a14a9d472b1c7b87c87ba - F src/update.c d1c2477dcf14d90999d1935af4efb4806553250b - F src/utf.c 6fc6c88d50448c469c5c196acf21617a24f90269 - F src/util.c 76ed0519296e3f62e97e57dab1999e34184c8e49 - F src/vacuum.c 3728d74919d4fb1356f9e9a13e27773db60b7179 - F src/vdbe.c 54e269c6268a6f37697e27d6bc572674f14b1581 - F src/vdbe.h 57b87844270b2e92647b8b82a8948f7a29efae8d - F src/vdbeInt.h 05fbda0e061dbc4aaa2709a8cccf3515c245b263 - F src/vdbeapi.c 93a22a9ba2abe292d5c2cf304d7eb2e894dde0ed - F src/vdbeaux.c c7c9219cb31cef9917db3b3b41604ac74c5bc41b - F src/vdbeblob.c 8cd05a5630e6d5563ad017bf82edaf812b28acde - F src/vdbemem.c 20e349d2ca928802fc8f2d42a2cc488fd6981d3f - F src/vdbesort.c 9d83601f9d6243fe70dd0169a2820c5ddfd48147 - F src/vdbetrace.c e7ec40e1999ff3c6414424365d5941178966dcbc - F src/vtab.c 21b932841e51ebd7d075e2d0ad1415dce8d2d5fd - F src/wal.c 7dc3966ef98b74422267e7e6e46e07ff6c6eb1b4 + F src/threads.c 6bbcc9fe50c917864d48287b4792d46d6e873481 + F src/tokenize.c 57cb3720f53f84d811def2069c2b169b6be539a5 + F src/treeview.c c84b1a8ebc7f1d00cd76ce4958eeb3ae1021beed + F src/trigger.c 322f23aad694e8f31d384dcfa386d52a48d3c52f + F src/update.c 487747b328b7216bb7f6af0695d6937d5c9e605f + F src/utf.c fc6b889ba0779b7722634cdeaa25f1930d93820c + F src/util.c bc9dd64b5db544218b871b66243871c202b2781f + F src/vacuum.c 2ddd5cad2a7b9cef7f9e431b8c7771634c6b1701 -F src/vdbe.c 2ab1decd98925f8cd846993dde9dccaa69cdf0ef -F src/vdbe.h 7a75045d879118b9d3af7e8b3c108f2f27c51473 ++F src/vdbe.c eeef2e6ee7c377c31a1b3f6c2b2812f3f711c2d7 ++F src/vdbe.h 529bb4a7bedcd28dccba5abb3927e3c5cb70a832 + F src/vdbeInt.h 8b54e01ad0463590e7cffabce0bc36da9ee4f816 + F src/vdbeapi.c adabbd66eb2e3a10f3998485ee0be7e326d06ee4 -F src/vdbeaux.c 787f5f9d58f4c6f39294ed06909ba602d1a402e6 ++F src/vdbeaux.c 6194de3a1c44acc4027612240e1d9d664070814f + F src/vdbeblob.c 4f2e8e075d238392df98c5e03a64342465b03f90 + F src/vdbemem.c ae38a0d35ae71cf604381a887c170466ba518090 + F src/vdbesort.c f5009e7a35e3065635d8918b9a31f498a499976b + F src/vdbetrace.c 8befe829faff6d9e6f6e4dee5a7d3f85cc85f1a0 + F src/vtab.c 082b35a25a26e3d36f365ca8cd73c1922532f05e + F src/vxworks.h c18586c8edc1bddbc15c004fa16aeb1e1342b4fb + F src/wal.c 6fb6b68969e4692593c2552c4e7bff5882de2cb8 F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4 - F src/walker.c e9e593d5bb798c3e67fc3893dfe7055c9e7d8d74 - F src/where.c 907fd82f4f8fc89d8a72d44caf5a5679c0769630 - F src/whereInt.h 96a75c61f1d2b9d4a8e4bb17d89deb0cf7cba358 + F src/walker.c c253b95b4ee44b21c406e2a1052636c31ea27804 + F src/where.c c745d3aa78ad1aa8982febb99f2f17ee5cbac069 + F src/whereInt.h 5f87e3c4b0551747d119730dfebddd3c54f04047 -F src/wherecode.c 5da5049224f12db314931ae7e0919b4914a2a0b1 ++F src/wherecode.c 00a5d75121f528e03c96a48b2bd46d91ec41c866 + F src/whereexpr.c 9ce1c9cfedbf80c93c7d899497025ec8256ce652 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 + F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 - F test/aggnested.test 45c0201e28045ad38a530b5a144b73cd4aa2cfd6 + F test/aggnested.test b35b4cd69fc913f90d39a575e171e1116c3a4bb7 F test/alias.test 4529fbc152f190268a15f9384a5651bbbabc9d87 F test/all.test 6ff7b43c2b4b905c74dc4a813d201d0fa64c5783 - F test/alter.test e88dfa77e020c2b48e52a8020c70171ab828e079 + F test/alter.test 2facdddf08d0d48e75dc6cc312cd2b030f4835dd F test/alter2.test 7ea05c7d92ac99349a802ef7ada17294dd647060 F test/alter3.test 49c9d9fba2b8fcdce2dedeca97bbf1f369cc548d - F test/alter4.test 8e93bf7a7e6919b14b0c9a6c1e4908bcf21b0165 + F test/alter4.test c461150723ac957f3b2214aa0b11552cd72023ec F test/altermalloc.test e81ac9657ed25c6c5bb09bebfa5a047cd8e4acfc F test/amatch1.test b5ae7065f042b7f4c1c922933f4700add50cdb9f - F test/analyze.test 1772936d66471c65221e437b6d1999c3a03166c4 - F test/analyze3.test 412f690dfe95b337475e3e78a84a85d25f6f125d + F test/analyze.test 3eb35a4af972f98422e5dc0586501b17d103d321 + F test/analyze3.test 0f0ee6135b293a0e5af471a8423b80b688469d71 F test/analyze4.test eff2df19b8dd84529966420f29ea52edc6b56213 F test/analyze5.test 765c4e284aa69ca172772aa940946f55629bc8c4 - F test/analyze6.test d31defa011a561b938b4608d3538c1b4e0b5e92c + F test/analyze6.test f1c552ce39cca4ec922a7e4e0e5d0203d6b3281f F test/analyze7.test bb1409afc9e8629e414387ef048b8e0e3e0bdc4f - F test/analyze8.test 093d15c1c888eed5034304a98c992f7360130b88 - F test/analyze9.test 339e87723cd4dc158dc5e9095acd8df9e87faf79 - F test/analyzeA.test 1a5c40079894847976d983ca39c707aaa44b6944 + F test/analyze8.test c05a461d0a6b05991106467d0c47480f2e709c82 + F test/analyze9.test 3dd9e203fad353ec8027b18a6d9a92af59f4e727 + F test/analyzeA.test 3335697f6700c7052295cfd0067fc5b2aacddf9a F test/analyzeB.test 8bf35ee0a548aea831bf56762cb8e7fdb1db083d + F test/analyzeC.test 555a6cc388b9818b6eda6df816f01ce0a75d3a93 + F test/analyzeD.test 08f9d0bee4e118a66fff3a32d02dbe0ee0a2b594 + F test/analyzeE.test 8684e8ac5722fb97c251887ad97e5d496a98af1d + F test/analyzeF.test 7ccd7a04f7d3061bde1a8a4dacc4792edccf6bf2 + F test/analyzer1.test 498e2ff4b62740c2751c3a2f8b744fe26689fae9 F test/async.test 1d0e056ba1bb9729283a0f22718d3a25e82c277b F test/async2.test c0a9bd20816d7d6a2ceca7b8c03d3d69c28ffb8b F test/async3.test d73a062002376d7edc1fe3edff493edbec1fc2f7 @@@ -1143,12 -1366,14 +1368,14 @@@ F tool/sqldiff.c a6988cc6e10e08662d73df F tool/stack_usage.tcl f8e71b92cdb099a147dad572375595eae55eca43 F tool/symbols-mingw.sh 4dbcea7e74768305384c9fd2ed2b41bbf9f0414d F tool/symbols.sh fec58532668296d7c7dc48be9c87f75ccdb5814f - F tool/tostr.awk e75472c2f98dd76e06b8c9c1367f4ab07e122d06 - F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381 + F tool/tostr.awk 11760e1b94a5d3dcd42378f3cc18544c06cfa576 + F tool/varint.c 5d94cb5003db9dbbcbcc5df08d66f16071aee003 + F tool/vdbe-compress.tcl 5926c71f9c12d2ab73ef35c29376e756eb68361c + F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4 - F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01 - F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff - P f9dddd008c6ef7940a1d66363fbb456cff7207c1 - R 9fb1051a18f0e187efee634c86c45e62 - U dan - Z 04e07c13931dc702da8721e6980ca969 + F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b + F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P ee966af8ff79189f8b90f3557dea21059517dfe2 -R 5dcd61cef4d3f59a3e6f61e4cf60159a -U dan -Z bdea05956aba407287af182409a84a65 ++P 1efa6ed584172291edce78faf9021e577583d03b 851a875ad6b81f90960caf4d03b116afc911858d ++R cf16c1d2c53a66a19fa5c8a95e530360 ++U drh ++Z da64436fc89077040f5c1e43409c46ff diff --cc manifest.uuid index f1780b6a8f,f6ed1de738..279ca65dab --- a/manifest.uuid +++ b/manifest.uuid @@@ -1,1 -1,1 +1,1 @@@ - 1efa6ed584172291edce78faf9021e577583d03b -851a875ad6b81f90960caf4d03b116afc911858d ++82a7a61bc0883b1e7432548e4890791717aa1bb3 diff --cc src/btree.h index 15816d68ac,3edc2b3b57..05246b765d --- a/src/btree.h +++ b/src/btree.h @@@ -166,10 -183,8 +183,11 @@@ int sqlite3BtreeMovetoUnpacked int bias, int *pRes ); - int sqlite3BtreeCursorHasMoved(BtCursor*, int*); + int sqlite3BtreeCursorHasMoved(BtCursor*); + int sqlite3BtreeCursorRestore(BtCursor*, int*); +#ifdef SQLITE_ENABLE_CURSOR_HINTS +void sqlite3BtreeCursorHint(BtCursor*, int, const Expr*); +#endif int sqlite3BtreeDelete(BtCursor*); int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey, const void *pData, int nData, diff --cc src/expr.c index 8601d4a9b9,5acb909667..0b9b1fd0ea --- a/src/expr.c +++ b/src/expr.c @@@ -1260,26 -1394,11 +1394,27 @@@ int sqlite3ExprIsTableConstant(Expr *p ** is considered a variable but a single-quoted string (ex: 'abc') is ** a constant. */ - int sqlite3ExprIsConstantOrFunction(Expr *p){ - return exprIsConst(p, 2); + int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ + assert( isInit==0 || isInit==1 ); + return exprIsConst(p, 4+isInit, 0); } +#ifdef SQLITE_ENABLE_CURSOR_HINTS +/* +** Walk an expression tree. Return 1 if the expression contains a +** subquery of some kind. Return 0 if there are no subqueries. +*/ +int sqlite3ExprContainsSubquery(Expr *p){ + Walker w; + memset(&w, 0, sizeof(w)); - w.u.i = 1; ++ w.eCode = 1; + w.xExprCallback = sqlite3ExprWalkNoop; + w.xSelectCallback = selectNodeIsConstant; + sqlite3WalkExpr(&w, p); - return w.u.i==0; ++ return w.u.n==0; +} +#endif + /* ** If the expression p codes a constant integer that is small enough ** to fit in a 32-bit integer, return 1 and put the value of the integer diff --cc src/select.c index bb979b8f0e,8ac98f1759..4e21e72572 --- a/src/select.c +++ b/src/select.c @@@ -3773,10 -4543,9 +4543,9 @@@ static void sqlite3SelectAddTypeInfo(Pa #ifndef SQLITE_OMIT_SUBQUERY Walker w; memset(&w, 0, sizeof(w)); - w.xSelectCallback = selectAddSubqueryTypeInfo; + w.xSelectCallback2 = selectAddSubqueryTypeInfo; - w.xExprCallback = exprWalkNoop; + w.xExprCallback = sqlite3ExprWalkNoop; w.pParse = pParse; - w.bSelectDepthFirst = 1; sqlite3WalkSelect(&w, pSelect); #endif } diff --cc src/sqliteInt.h index 8cb7820e3d,9c79c91dfa..4f0055c452 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@@ -1062,9 -1308,7 +1308,8 @@@ struct sqlite3 #define SQLITE_SubqCoroutine 0x0100 /* Evaluate subqueries as coroutines */ #define SQLITE_Transitive 0x0200 /* Transitive constraints */ #define SQLITE_OmitNoopJoin 0x0400 /* Omit unused tables in joins */ - #define SQLITE_Stat3 0x0800 /* Use the SQLITE_STAT3 table */ - #define SQLITE_AdjustOutEst 0x1000 /* Adjust output estimates using WHERE */ + #define SQLITE_Stat34 0x0800 /* Use STAT3 or STAT4 data */ +#define SQLITE_CursorHints 0x2000 /* Add OP_CursorHint opcodes */ #define SQLITE_AllOpts 0xffff /* All optimizations */ /* @@@ -2971,11 -3401,10 +3403,11 @@@ void sqlite3CloseSavepoints(sqlite3 *) void sqlite3LeaveMutexAndCloseZombie(sqlite3*); int sqlite3ExprIsConstant(Expr*); int sqlite3ExprIsConstantNotJoin(Expr*); - int sqlite3ExprIsConstantOrFunction(Expr*); + int sqlite3ExprIsConstantOrFunction(Expr*, u8); + int sqlite3ExprIsTableConstant(Expr*,int); +int sqlite3ExprContainsSubquery(Expr*); int sqlite3ExprIsInteger(Expr*, int*); int sqlite3ExprCanBeNull(const Expr*); - void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int); int sqlite3ExprNeedsNoAffinityChange(const Expr*, char); int sqlite3IsRowid(const char*); void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8); diff --cc src/tclsqlite.c index c592d854cc,f024317e90..f8073fe3ef --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@@ -3672,7 -3738,7 +3738,8 @@@ static void init_all(Tcl_Interp *interp extern int Sqlitetest9_Init(Tcl_Interp*); extern int Sqlitetestasync_Init(Tcl_Interp*); extern int Sqlitetest_autoext_Init(Tcl_Interp*); + extern int Sqlitetest_blob_Init(Tcl_Interp*); + extern int Sqlitetest_cursorhint_Init(Tcl_Interp*); extern int Sqlitetest_demovfs_Init(Tcl_Interp *); extern int Sqlitetest_func_Init(Tcl_Interp*); extern int Sqlitetest_hexio_Init(Tcl_Interp*); @@@ -3716,8 -3783,8 +3784,10 @@@ Sqlitetest9_Init(interp); Sqlitetestasync_Init(interp); Sqlitetest_autoext_Init(interp); + Sqlitetest_blob_Init(interp); + Sqlitetest_cursorhint_Init(interp); + Sqlitetest_demovfs_Init(interp); + Sqlitetest_demovfs_Init(interp); Sqlitetest_func_Init(interp); Sqlitetest_hexio_Init(interp); Sqlitetest_init_Init(interp); diff --cc src/test_cursorhint.c index 9d2450da27,0000000000..0f94232696 mode 100644,000000..100644 --- a/src/test_cursorhint.c +++ b/src/test_cursorhint.c @@@ -1,162 -1,0 +1,162 @@@ +/* +** 2008 March 19 +** +** 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. +** +************************************************************************* +** Code for testing all sorts of SQLite interfaces. This code +** implements new SQL functions used by the test scripts. +*/ +#include "sqlite3.h" +#include "tcl.h" +#include +#include +#include + +#include "sqliteInt.h" +#include "vdbeInt.h" + +struct CursorHintGlobal { + Tcl_Interp *interp; + Tcl_Obj *pScript; +} cursorhintglobal; + +static char *exprToString(Mem *aMem, Expr *pExpr){ + char *zRet = 0; + char *zBinOp = 0; + + switch( pExpr->op ){ + case TK_STRING: + zRet = sqlite3_mprintf("%Q", pExpr->u.zToken); + break; + + case TK_INTEGER: + zRet = sqlite3_mprintf("%d", pExpr->u.iValue); + break; + + case TK_NULL: + zRet = sqlite3_mprintf("%s", "NULL"); + break; + + case TK_REGISTER: { + Mem *pMem = &aMem[pExpr->iTable]; + if( pMem->flags & MEM_Int ){ + zRet = sqlite3_mprintf("%lld", pMem->u.i); + } + else if( pMem->flags & MEM_Real ){ - zRet = sqlite3_mprintf("%f", pMem->r); ++ zRet = sqlite3_mprintf("%f", pMem->u.r); + } + else if( pMem->flags & MEM_Str ){ + zRet = sqlite3_mprintf("%.*Q", pMem->n, pMem->z); + } + else if( pMem->flags & MEM_Blob ){ + } + else{ + zRet = sqlite3_mprintf("%s", "NULL"); + } + break; + } + + case TK_COLUMN: { + zRet = sqlite3_mprintf("col(%d)", (int)pExpr->iColumn); + break; + } + + case TK_LT: zBinOp = "<"; break; + case TK_LE: zBinOp = "<="; break; + case TK_GT: zBinOp = ">"; break; + case TK_GE: zBinOp = ">="; break; + case TK_NE: zBinOp = "!="; break; + case TK_EQ: zBinOp = "=="; break; + case TK_IS: zBinOp = "IS"; break; + case TK_ISNOT: zBinOp = "IS NOT"; break; + case TK_AND: zBinOp = "AND"; break; + case TK_OR: zBinOp = "OR"; break; + case TK_PLUS: zBinOp = "+"; break; + case TK_STAR: zBinOp = "*"; break; + case TK_MINUS: zBinOp = "-"; break; + case TK_REM: zBinOp = "%"; break; + case TK_BITAND: zBinOp = "&"; break; + case TK_BITOR: zBinOp = "|"; break; + case TK_SLASH: zBinOp = "/"; break; + case TK_LSHIFT: zBinOp = "<<"; break; + case TK_RSHIFT: zBinOp = ">>"; break; + case TK_CONCAT: zBinOp = "||"; break; + + default: + zRet = sqlite3_mprintf("%s", "expr"); + break; + } + + if( zBinOp ){ + zRet = sqlite3_mprintf("(%z %s %z)", + exprToString(aMem, pExpr->pLeft), + zBinOp, + exprToString(aMem, pExpr->pRight) + ); + } + + return zRet; +} + +void sqlite3BtreeCursorHintTest(Mem *aMem, Expr *pExpr){ + if( cursorhintglobal.pScript ){ + Tcl_Obj *pEval = Tcl_DuplicateObj(cursorhintglobal.pScript); + char *zExpr; + Tcl_Obj *pObj; + Tcl_IncrRefCount(pEval); + zExpr = exprToString(aMem, pExpr); + pObj = Tcl_NewStringObj(zExpr, -1); + sqlite3_free(zExpr); + Tcl_ListObjAppendElement(cursorhintglobal.interp, pEval, pObj); + Tcl_EvalObjEx(cursorhintglobal.interp, pEval, TCL_GLOBAL_ONLY); + Tcl_DecrRefCount(pEval); + } +} + +/* +** Usage: cursorhint_hook SCRIPT +*/ +static int install_cursorhint_hook( + ClientData clientData, /* Pointer to sqlite3_enable_XXX function */ + Tcl_Interp *interp, /* The TCL interpreter that invoked this command */ + int objc, /* Number of arguments */ + Tcl_Obj *CONST objv[] /* Command arguments */ +){ + if( objc!=1 && objc!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "?SCRIPT?"); + return TCL_ERROR; + } + if( cursorhintglobal.pScript ){ + Tcl_DecrRefCount(cursorhintglobal.pScript); + memset(&cursorhintglobal, 0, sizeof(cursorhintglobal)); + } + if( objc==2 ){ + cursorhintglobal.interp = interp; + cursorhintglobal.pScript = Tcl_DuplicateObj(objv[1]); + } + return TCL_OK; +} + +/* +** Register commands with the TCL interpreter. +*/ +int Sqlitetest_cursorhint_Init(Tcl_Interp *interp){ + static struct { + char *zName; + Tcl_ObjCmdProc *xProc; + } aObjCmd[] = { + { "cursorhint_hook", install_cursorhint_hook }, + }; + int i; + for(i=0; ip2 ) goto jump_to_p2; break; } - #endif +#ifdef SQLITE_ENABLE_CURSOR_HINTS +/* Opcode: CursorHint P1 P2 * P4 * +** +** Provide a hint to cursor P1 that it only needs to return rows that +** satisfy the Expr tree given in P4. P2 is the table number of cursor P1 +** such that references to cursor P1 in the Expr tree are given by +** Expr.iTable==P2. +*/ +case OP_CursorHint: { + VdbeCursor *pC; + + assert( pOp->p1>=0 && pOp->p1nCursor ); + assert( pOp->p4type==P4_EXPR ); + pC = p->apCsr[pOp->p1]; + if( pC ){ + sqlite3BtreeCursorHint(pC->pCursor, pOp->p2, pOp->p4.pExpr); +#ifdef SQLITE_TEST + void sqlite3BtreeCursorHintTest(Mem*, Expr*); + sqlite3BtreeCursorHintTest(p->aMem, pOp->p4.pExpr); +#endif + } + break; +} +#endif /* SQLITE_ENABLE_CURSOR_HINTS */ /* Opcode: Noop * * * * * ** diff --cc src/wherecode.c index 0000000000,9747f7f375..fccd00a62e mode 000000,100644..100644 --- a/src/wherecode.c +++ b/src/wherecode.c @@@ -1,0 -1,1505 +1,1591 @@@ + /* + ** 2015-06-06 + ** + ** 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 module contains C code that generates VDBE code used to process + ** the WHERE clause of SQL statements. + ** + ** This file was split off from where.c on 2015-06-06 in order to reduce the + ** size of where.c and make it easier to edit. This file contains the routines + ** that actually generate the bulk of the WHERE loop code. The original where.c + ** file retains the code that does query planning and analysis. + */ + #include "sqliteInt.h" + #include "whereInt.h" + + #ifndef SQLITE_OMIT_EXPLAIN + /* + ** This routine is a helper for explainIndexRange() below + ** + ** pStr holds the text of an expression that we are building up one term + ** at a time. This routine adds a new term to the end of the expression. + ** Terms are separated by AND so add the "AND" text for second and subsequent + ** terms only. + */ + static void explainAppendTerm( + StrAccum *pStr, /* The text expression being built */ + int iTerm, /* Index of this term. First is zero */ + const char *zColumn, /* Name of the column */ + const char *zOp /* Name of the operator */ + ){ + if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); + sqlite3StrAccumAppendAll(pStr, zColumn); + sqlite3StrAccumAppend(pStr, zOp, 1); + sqlite3StrAccumAppend(pStr, "?", 1); + } + + /* + ** Argument pLevel describes a strategy for scanning table pTab. This + ** function appends text to pStr that describes the subset of table + ** rows scanned by the strategy in the form of an SQL expression. + ** + ** For example, if the query: + ** + ** SELECT * FROM t1 WHERE a=1 AND b>2; + ** + ** is run and there is an index on (a, b), then this function returns a + ** string similar to: + ** + ** "a=? AND b>?" + */ + static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){ + Index *pIndex = pLoop->u.btree.pIndex; + u16 nEq = pLoop->u.btree.nEq; + u16 nSkip = pLoop->nSkip; + int i, j; + Column *aCol = pTab->aCol; + i16 *aiColumn = pIndex->aiColumn; + + if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return; + sqlite3StrAccumAppend(pStr, " (", 2); + for(i=0; i=nSkip ){ + explainAppendTerm(pStr, i, z, "="); + }else{ + if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5); + sqlite3XPrintf(pStr, 0, "ANY(%s)", z); + } + } + + j = i; + if( pLoop->wsFlags&WHERE_BTM_LIMIT ){ + char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; + explainAppendTerm(pStr, i++, z, ">"); + } + if( pLoop->wsFlags&WHERE_TOP_LIMIT ){ + char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName; + explainAppendTerm(pStr, i, z, "<"); + } + sqlite3StrAccumAppend(pStr, ")", 1); + } + + /* + ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN + ** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was + ** defined at compile-time. If it is not a no-op, a single OP_Explain opcode + ** is added to the output to describe the table scan strategy in pLevel. + ** + ** If an OP_Explain opcode is added to the VM, its address is returned. + ** Otherwise, if no OP_Explain is coded, zero is returned. + */ + int sqlite3WhereExplainOneScan( + Parse *pParse, /* Parse context */ + SrcList *pTabList, /* Table list this loop refers to */ + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ + int iLevel, /* Value for "level" column of output */ + int iFrom, /* Value for "from" column of output */ + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ + ){ + int ret = 0; + #if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS) + if( pParse->explain==2 ) + #endif + { + struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; + Vdbe *v = pParse->pVdbe; /* VM being constructed */ + sqlite3 *db = pParse->db; /* Database handle */ + int iId = pParse->iSelectId; /* Select id (left-most output column) */ + int isSearch; /* True for a SEARCH. False for SCAN. */ + WhereLoop *pLoop; /* The controlling WhereLoop object */ + u32 flags; /* Flags that describe this loop */ + char *zMsg; /* Text to add to EQP output */ + StrAccum str; /* EQP output string */ + char zBuf[100]; /* Initial space for EQP output string */ + + pLoop = pLevel->pWLoop; + flags = pLoop->wsFlags; + if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0; + + isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 + || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0)) + || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); + + sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH); + sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN"); + if( pItem->pSelect ){ + sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId); + }else{ + sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName); + } + + if( pItem->zAlias ){ + sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias); + } + if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){ + const char *zFmt = 0; + Index *pIdx; + + assert( pLoop->u.btree.pIndex!=0 ); + pIdx = pLoop->u.btree.pIndex; + assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) ); + if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){ + if( isSearch ){ + zFmt = "PRIMARY KEY"; + } + }else if( flags & WHERE_PARTIALIDX ){ + zFmt = "AUTOMATIC PARTIAL COVERING INDEX"; + }else if( flags & WHERE_AUTO_INDEX ){ + zFmt = "AUTOMATIC COVERING INDEX"; + }else if( flags & WHERE_IDX_ONLY ){ + zFmt = "COVERING INDEX %s"; + }else{ + zFmt = "INDEX %s"; + } + if( zFmt ){ + sqlite3StrAccumAppend(&str, " USING ", 7); + sqlite3XPrintf(&str, 0, zFmt, pIdx->zName); + explainIndexRange(&str, pLoop, pItem->pTab); + } + }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){ + const char *zRange; + if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){ + zRange = "(rowid=?)"; + }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ + zRange = "(rowid>? AND rowid?)"; + }else{ + assert( flags&WHERE_TOP_LIMIT); + zRange = "(rowidu.vtab.idxNum, pLoop->u.vtab.idxStr); + } + #endif + #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS + if( pLoop->nOut>=10 ){ + sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut)); + }else{ + sqlite3StrAccumAppend(&str, " (~1 row)", 9); + } + #endif + zMsg = sqlite3StrAccumFinish(&str); + ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC); + } + return ret; + } + #endif /* SQLITE_OMIT_EXPLAIN */ + + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS + /* + ** Configure the VM passed as the first argument with an + ** sqlite3_stmt_scanstatus() entry corresponding to the scan used to + ** implement level pLvl. Argument pSrclist is a pointer to the FROM + ** clause that the scan reads data from. + ** + ** If argument addrExplain is not 0, it must be the address of an + ** OP_Explain instruction that describes the same loop. + */ + void sqlite3WhereAddScanStatus( + Vdbe *v, /* Vdbe to add scanstatus entry to */ + SrcList *pSrclist, /* FROM clause pLvl reads data from */ + WhereLevel *pLvl, /* Level to add scanstatus() entry for */ + int addrExplain /* Address of OP_Explain (or 0) */ + ){ + const char *zObj = 0; + WhereLoop *pLoop = pLvl->pWLoop; + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 && pLoop->u.btree.pIndex!=0 ){ + zObj = pLoop->u.btree.pIndex->zName; + }else{ + zObj = pSrclist->a[pLvl->iFrom].zName; + } + sqlite3VdbeScanStatus( + v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj + ); + } + #endif + + + /* + ** Disable a term in the WHERE clause. Except, do not disable the term + ** if it controls a LEFT OUTER JOIN and it did not originate in the ON + ** or USING clause of that join. + ** + ** Consider the term t2.z='ok' in the following queries: + ** + ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' + ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' + ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' + ** + ** The t2.z='ok' is disabled in the in (2) because it originates + ** in the ON clause. The term is disabled in (3) because it is not part + ** of a LEFT OUTER JOIN. In (1), the term is not disabled. + ** + ** Disabling a term causes that term to not be tested in the inner loop + ** of the join. Disabling is an optimization. When terms are satisfied + ** by indices, we disable them to prevent redundant tests in the inner + ** loop. We would get the correct results if nothing were ever disabled, + ** but joins might run a little slower. The trick is to disable as much + ** as we can without disabling too much. If we disabled in (1), we'd get + ** the wrong answer. See ticket #813. + ** + ** If all the children of a term are disabled, then that term is also + ** automatically disabled. In this way, terms get disabled if derived + ** virtual terms are tested first. For example: + ** + ** x GLOB 'abc*' AND x>='abc' AND x<'acd' + ** \___________/ \______/ \_____/ + ** parent child1 child2 + ** + ** Only the parent term was in the original WHERE clause. The child1 + ** and child2 terms were added by the LIKE optimization. If both of + ** the virtual child terms are valid, then testing of the parent can be + ** skipped. + ** + ** Usually the parent term is marked as TERM_CODED. But if the parent + ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead. + ** The TERM_LIKECOND marking indicates that the term should be coded inside + ** a conditional such that is only evaluated on the second pass of a + ** LIKE-optimization loop, when scanning BLOBs instead of strings. + */ + static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ + int nLoop = 0; + while( pTerm + && (pTerm->wtFlags & TERM_CODED)==0 + && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) + && (pLevel->notReady & pTerm->prereqAll)==0 + ){ + if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){ + pTerm->wtFlags |= TERM_LIKECOND; + }else{ + pTerm->wtFlags |= TERM_CODED; + } + if( pTerm->iParent<0 ) break; + pTerm = &pTerm->pWC->a[pTerm->iParent]; + pTerm->nChild--; + if( pTerm->nChild!=0 ) break; + nLoop++; + } + } + + /* + ** Code an OP_Affinity opcode to apply the column affinity string zAff + ** to the n registers starting at base. + ** + ** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the + ** beginning and end of zAff are ignored. If all entries in zAff are + ** SQLITE_AFF_BLOB, then no code gets generated. + ** + ** This routine makes its own copy of zAff so that the caller is free + ** to modify zAff after this routine returns. + */ + static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ + Vdbe *v = pParse->pVdbe; + if( zAff==0 ){ + assert( pParse->db->mallocFailed ); + return; + } + assert( v!=0 ); + + /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning + ** and end of the affinity string. + */ + while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){ + n--; + base++; + zAff++; + } + while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){ + n--; + } + + /* Code the OP_Affinity opcode if there is anything left to do. */ + if( n>0 ){ + sqlite3VdbeAddOp2(v, OP_Affinity, base, n); + sqlite3VdbeChangeP4(v, -1, zAff, n); + sqlite3ExprCacheAffinityChange(pParse, base, n); + } + } + + + /* + ** Generate code for a single equality term of the WHERE clause. An equality + ** term can be either X=expr or X IN (...). pTerm is the term to be + ** coded. + ** + ** The current value for the constraint is left in register iReg. + ** + ** For a constraint of the form X=expr, the expression is evaluated and its + ** result is left on the stack. For constraints of the form X IN (...) + ** this routine sets up a loop that will iterate over all values of X. + */ + static int codeEqualityTerm( + Parse *pParse, /* The parsing context */ + WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ + WhereLevel *pLevel, /* The level of the FROM clause we are working on */ + int iEq, /* Index of the equality term within this level */ + int bRev, /* True for reverse-order IN operations */ + int iTarget /* Attempt to leave results in this register */ + ){ + Expr *pX = pTerm->pExpr; + Vdbe *v = pParse->pVdbe; + int iReg; /* Register holding results */ + + assert( iTarget>0 ); + if( pX->op==TK_EQ || pX->op==TK_IS ){ + iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); + }else if( pX->op==TK_ISNULL ){ + iReg = iTarget; + sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); + #ifndef SQLITE_OMIT_SUBQUERY + }else{ + int eType; + int iTab; + struct InLoop *pIn; + WhereLoop *pLoop = pLevel->pWLoop; + + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 + && pLoop->u.btree.pIndex!=0 + && pLoop->u.btree.pIndex->aSortOrder[iEq] + ){ + testcase( iEq==0 ); + testcase( bRev ); + bRev = !bRev; + } + assert( pX->op==TK_IN ); + iReg = iTarget; + eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0); + if( eType==IN_INDEX_INDEX_DESC ){ + testcase( bRev ); + bRev = !bRev; + } + iTab = pX->iTable; + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0); + VdbeCoverageIf(v, bRev); + VdbeCoverageIf(v, !bRev); + assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 ); + pLoop->wsFlags |= WHERE_IN_ABLE; + if( pLevel->u.in.nIn==0 ){ + pLevel->addrNxt = sqlite3VdbeMakeLabel(v); + } + pLevel->u.in.nIn++; + pLevel->u.in.aInLoop = + sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, + sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); + pIn = pLevel->u.in.aInLoop; + if( pIn ){ + pIn += pLevel->u.in.nIn - 1; + pIn->iCur = iTab; + if( eType==IN_INDEX_ROWID ){ + pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); + }else{ + pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); + } + pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen; + sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v); + }else{ + pLevel->u.in.nIn = 0; + } + #endif + } + disableTerm(pLevel, pTerm); + return iReg; + } + + /* + ** Generate code that will evaluate all == and IN constraints for an + ** index scan. + ** + ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). + ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 + ** The index has as many as three equality constraints, but in this + ** example, the third "c" value is an inequality. So only two + ** constraints are coded. This routine will generate code to evaluate + ** a==5 and b IN (1,2,3). The current values for a and b will be stored + ** in consecutive registers and the index of the first register is returned. + ** + ** In the example above nEq==2. But this subroutine works for any value + ** of nEq including 0. If nEq==0, this routine is nearly a no-op. + ** The only thing it does is allocate the pLevel->iMem memory cell and + ** compute the affinity string. + ** + ** The nExtraReg parameter is 0 or 1. It is 0 if all WHERE clause constraints + ** are == or IN and are covered by the nEq. nExtraReg is 1 if there is + ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that + ** occurs after the nEq quality constraints. + ** + ** This routine allocates a range of nEq+nExtraReg memory cells and returns + ** the index of the first memory cell in that range. The code that + ** calls this routine will use that memory range to store keys for + ** start and termination conditions of the loop. + ** key value of the loop. If one or more IN operators appear, then + ** this routine allocates an additional nEq memory cells for internal + ** use. + ** + ** Before returning, *pzAff is set to point to a buffer containing a + ** copy of the column affinity string of the index allocated using + ** sqlite3DbMalloc(). Except, entries in the copy of the string associated + ** with equality constraints that use BLOB or NONE affinity are set to + ** SQLITE_AFF_BLOB. This is to deal with SQL such as the following: + ** + ** CREATE TABLE t1(a TEXT PRIMARY KEY, b); + ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; + ** + ** In the example above, the index on t1(a) has TEXT affinity. But since + ** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity, + ** no conversion should be attempted before using a t2.b value as part of + ** a key to search the index. Hence the first byte in the returned affinity + ** string in this example would be set to SQLITE_AFF_BLOB. + */ + static int codeAllEqualityTerms( + Parse *pParse, /* Parsing context */ + WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ + int bRev, /* Reverse the order of IN operators */ + int nExtraReg, /* Number of extra registers to allocate */ + char **pzAff /* OUT: Set to point to affinity string */ + ){ + u16 nEq; /* The number of == or IN constraints to code */ + u16 nSkip; /* Number of left-most columns to skip */ + Vdbe *v = pParse->pVdbe; /* The vm under construction */ + Index *pIdx; /* The index being used for this loop */ + WhereTerm *pTerm; /* A single constraint term */ + WhereLoop *pLoop; /* The WhereLoop object */ + int j; /* Loop counter */ + int regBase; /* Base register */ + int nReg; /* Number of registers to allocate */ + char *zAff; /* Affinity string to return */ + + /* This module is only called on query plans that use an index. */ + pLoop = pLevel->pWLoop; + assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 ); + nEq = pLoop->u.btree.nEq; + nSkip = pLoop->nSkip; + pIdx = pLoop->u.btree.pIndex; + assert( pIdx!=0 ); + + /* Figure out how many memory cells we will need then allocate them. + */ + regBase = pParse->nMem + 1; + nReg = pLoop->u.btree.nEq + nExtraReg; + pParse->nMem += nReg; + + zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); + if( !zAff ){ + pParse->db->mallocFailed = 1; + } + + if( nSkip ){ + int iIdxCur = pLevel->iIdxCur; + sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + VdbeComment((v, "begin skip-scan on %s", pIdx->zName)); + j = sqlite3VdbeAddOp0(v, OP_Goto); + pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT), + iIdxCur, 0, regBase, nSkip); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + sqlite3VdbeJumpHere(v, j); + for(j=0; jaiColumn[j]>=0 ); + VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName)); + } + } + + /* Evaluate the equality constraints + */ + assert( zAff==0 || (int)strlen(zAff)>=nEq ); + for(j=nSkip; jaLTerm[j]; + assert( pTerm!=0 ); + /* The following testcase is true for indices with redundant columns. + ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ + testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j); + if( r1!=regBase+j ){ + if( nReg==1 ){ + sqlite3ReleaseTempReg(pParse, regBase); + regBase = r1; + }else{ + sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); + } + } + testcase( pTerm->eOperator & WO_ISNULL ); + testcase( pTerm->eOperator & WO_IN ); + if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ + Expr *pRight = pTerm->pExpr->pRight; + if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); + VdbeCoverage(v); + } + if( zAff ){ + if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ + zAff[j] = SQLITE_AFF_BLOB; + } + if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ + zAff[j] = SQLITE_AFF_BLOB; + } + } + } + } + *pzAff = zAff; + return regBase; + } + + /* + ** If the most recently coded instruction is a constant range contraint + ** that originated from the LIKE optimization, then change the P3 to be + ** pLoop->iLikeRepCntr and set P5. + ** + ** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range + ** expression: "x>='ABC' AND x<'abd'". But this requires that the range + ** scan loop run twice, once for strings and a second time for BLOBs. + ** The OP_String opcodes on the second pass convert the upper and lower + ** bound string contants to blobs. This routine makes the necessary changes + ** to the OP_String opcodes for that to happen. + */ + static void whereLikeOptimizationStringFixup( + Vdbe *v, /* prepared statement under construction */ + WhereLevel *pLevel, /* The loop that contains the LIKE operator */ + WhereTerm *pTerm /* The upper or lower bound just coded */ + ){ + if( pTerm->wtFlags & TERM_LIKEOPT ){ + VdbeOp *pOp; + assert( pLevel->iLikeRepCntr>0 ); + pOp = sqlite3VdbeGetOp(v, -1); + assert( pOp!=0 ); + assert( pOp->opcode==OP_String8 + || pTerm->pWC->pWInfo->pParse->db->mallocFailed ); + pOp->p3 = pLevel->iLikeRepCntr; + pOp->p5 = 1; + } + } + ++#ifdef SQLITE_ENABLE_CURSOR_HINTS ++ ++/* ++** This function is called on every node of an expression tree used as an ++** argument to the OP_CursorHint instruction. If the node is a TK_COLUMN ++** that accesses any cursor other than (pWalker->u.i), do the following: ++** ++** 1) allocate a register and code an OP_Column instruction to read ++** the specified column into the new register, and ++** ++** 2) transform the expression node to a TK_REGISTER node that reads ++** from the newly populated register. ++*/ ++static int codeCursorHintFixExpr(Walker *pWalker, Expr *pExpr){ ++ int rc = WRC_Continue; ++ if( pExpr->op==TK_COLUMN && pExpr->iTable!=pWalker->u.n ){ ++ Vdbe *v = pWalker->pParse->pVdbe; ++ int reg = ++pWalker->pParse->nMem; /* Register for column value */ ++ sqlite3ExprCodeGetColumnOfTable( ++ v, pExpr->pTab, pExpr->iTable, pExpr->iColumn, reg ++ ); ++ pExpr->op = TK_REGISTER; ++ pExpr->iTable = reg; ++ }else if( pExpr->op==TK_AGG_FUNCTION ){ ++ /* An aggregate function in the WHERE clause of a query means this must ++ ** be a correlated sub-query, and expression pExpr is an aggregate from ++ ** the parent context. Do not walk the function arguments in this case. ++ ** ++ ** todo: It should be possible to replace this node with a TK_REGISTER ++ ** expression, as the result of the expression must be stored in a ++ ** register at this point. The same holds for TK_AGG_COLUMN nodes. */ ++ rc = WRC_Prune; ++ } ++ return rc; ++} ++ ++/* ++** Insert an OP_CursorHint instruction if it is appropriate to do so. ++*/ ++static void codeCursorHint( ++ WhereInfo *pWInfo, ++ int iLevel ++){ ++ Parse *pParse = pWInfo->pParse; ++ sqlite3 *db = pParse->db; ++ Vdbe *v = pParse->pVdbe; ++ WhereLevel *pLevel; ++ Expr *pExpr = 0; ++ int iCur; ++ WhereClause *pWC; ++ WhereTerm *pTerm; ++ WhereLoop *pWLoop; ++ int i, j; ++ ++ if( OptimizationDisabled(db, SQLITE_CursorHints) ) return; ++ pLevel = &pWInfo->a[iLevel]; ++ pWLoop = pLevel->pWLoop; ++ iCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor; ++ pWC = &pWInfo->sWC; ++ for(i=0; inTerm; i++){ ++ pTerm = &pWC->a[i]; ++ if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; ++ if( pTerm->prereqAll & pLevel->notReady ) continue; ++ if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue; ++ if( sqlite3ExprContainsSubquery(pTerm->pExpr) ) continue; ++ for(j=0; jnLTerm && pWLoop->aLTerm[j]!=pTerm; j++){} ++ if( jnLTerm ) continue; ++ pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0)); ++ } ++ if( pExpr!=0 ){ ++ const char *a = (const char*)pExpr; ++ Walker sWalker; ++ memset(&sWalker, 0, sizeof(sWalker)); ++ sWalker.xExprCallback = codeCursorHintFixExpr; ++ sWalker.pParse = pParse; ++ sWalker.u.n = pLevel->iTabCur; ++ sqlite3WalkExpr(&sWalker, pExpr); ++ sqlite3VdbeAddOp4(v, OP_CursorHint, pLevel->iTabCur, iCur, 0, a, P4_EXPR); ++ } ++} ++#else ++# define codeCursorHint(A,B) /* No-op */ ++#endif /* SQLITE_ENABLE_CURSOR_HINTS */ + + /* + ** Generate code for the start of the iLevel-th loop in the WHERE clause + ** implementation described by pWInfo. + */ + Bitmask sqlite3WhereCodeOneLoopStart( + WhereInfo *pWInfo, /* Complete information about the WHERE clause */ + int iLevel, /* Which level of pWInfo->a[] should be coded */ + Bitmask notReady /* Which tables are currently available */ + ){ + int j, k; /* Loop counters */ + int iCur; /* The VDBE cursor for the table */ + int addrNxt; /* Where to jump to continue with the next IN case */ + int omitTable; /* True if we use the index only */ + int bRev; /* True if we need to scan in reverse order */ + WhereLevel *pLevel; /* The where level to be coded */ + WhereLoop *pLoop; /* The WhereLoop object being coded */ + WhereClause *pWC; /* Decomposition of the entire WHERE clause */ + WhereTerm *pTerm; /* A WHERE clause term */ + Parse *pParse; /* Parsing context */ + sqlite3 *db; /* Database connection */ + Vdbe *v; /* The prepared stmt under constructions */ + struct SrcList_item *pTabItem; /* FROM clause term being coded */ + int addrBrk; /* Jump here to break out of the loop */ + int addrCont; /* Jump here to continue with next cycle */ + int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ + int iReleaseReg = 0; /* Temp register to free before returning */ + + pParse = pWInfo->pParse; + v = pParse->pVdbe; + pWC = &pWInfo->sWC; + db = pParse->db; + pLevel = &pWInfo->a[iLevel]; + pLoop = pLevel->pWLoop; + pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; + iCur = pTabItem->iCursor; + pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur); + bRev = (pWInfo->revMask>>iLevel)&1; + omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 + && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0; + VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName)); + + /* Create labels for the "break" and "continue" instructions + ** for the current loop. Jump to addrBrk to break out of a loop. + ** Jump to cont to go immediately to the next iteration of the + ** loop. + ** + ** When there is an IN operator, we also have a "addrNxt" label that + ** means to continue with the next IN value combination. When + ** there are no IN operators in the constraints, the "addrNxt" label + ** is the same as "addrBrk". + */ + addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); + addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); + + /* If this is the right table of a LEFT OUTER JOIN, allocate and + ** initialize a memory cell that records if this table matches any + ** row of the left table of the join. + */ + if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ + pLevel->iLeftJoin = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); + VdbeComment((v, "init LEFT JOIN no-match flag")); + } + + /* Special case of a FROM clause subquery implemented as a co-routine */ + if( pTabItem->viaCoroutine ){ + int regYield = pTabItem->regReturn; + sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub); + pLevel->p2 = sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk); + VdbeCoverage(v); + VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName)); + pLevel->op = OP_Goto; + }else + + #ifndef SQLITE_OMIT_VIRTUALTABLE + if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){ + /* Case 1: The table is a virtual-table. Use the VFilter and VNext + ** to access the data. + */ + int iReg; /* P3 Value for OP_VFilter */ + int addrNotFound; + int nConstraint = pLoop->nLTerm; + + sqlite3ExprCachePush(pParse); + iReg = sqlite3GetTempRange(pParse, nConstraint+2); + addrNotFound = pLevel->addrBrk; + for(j=0; jaLTerm[j]; + if( pTerm==0 ) continue; + if( pTerm->eOperator & WO_IN ){ + codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget); + addrNotFound = pLevel->addrNxt; + }else{ + sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget); + } + } + sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg); + sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1); + sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg, + pLoop->u.vtab.idxStr, + pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC); + VdbeCoverage(v); + pLoop->u.vtab.needFree = 0; + for(j=0; ju.vtab.omitMask>>j)&1 ){ + disableTerm(pLevel, pLoop->aLTerm[j]); + } + } + pLevel->op = OP_VNext; + pLevel->p1 = iCur; + pLevel->p2 = sqlite3VdbeCurrentAddr(v); + sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); + sqlite3ExprCachePop(pParse); + }else + #endif /* SQLITE_OMIT_VIRTUALTABLE */ + + if( (pLoop->wsFlags & WHERE_IPK)!=0 + && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0 + ){ + /* Case 2: We can directly reference a single row using an + ** equality comparison against the ROWID field. Or + ** we reference multiple rows using a "rowid IN (...)" + ** construct. + */ + assert( pLoop->u.btree.nEq==1 ); + pTerm = pLoop->aLTerm[0]; + assert( pTerm!=0 ); + assert( pTerm->pExpr!=0 ); + assert( omitTable==0 ); + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + iReleaseReg = ++pParse->nMem; + iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg); + if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg); + addrNxt = pLevel->addrNxt; + sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); + VdbeCoverage(v); + sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + VdbeComment((v, "pk")); + pLevel->op = OP_Noop; + }else if( (pLoop->wsFlags & WHERE_IPK)!=0 + && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0 + ){ + /* Case 3: We have an inequality comparison against the ROWID field. + */ + int testOp = OP_Noop; + int start; + int memEndValue = 0; + WhereTerm *pStart, *pEnd; + + assert( omitTable==0 ); + j = 0; + pStart = pEnd = 0; + if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++]; + if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++]; + assert( pStart!=0 || pEnd!=0 ); + if( bRev ){ + pTerm = pStart; + pStart = pEnd; + pEnd = pTerm; + } ++ codeCursorHint(pWInfo, iLevel); + if( pStart ){ + Expr *pX; /* The expression that defines the start bound */ + int r1, rTemp; /* Registers for holding the start boundary */ + + /* The following constant maps TK_xx codes into corresponding + ** seek opcodes. It depends on a particular ordering of TK_xx + */ + const u8 aMoveOp[] = { + /* TK_GT */ OP_SeekGT, + /* TK_LE */ OP_SeekLE, + /* TK_LT */ OP_SeekLT, + /* TK_GE */ OP_SeekGE + }; + assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ + assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ + assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ + + assert( (pStart->wtFlags & TERM_VNULL)==0 ); + testcase( pStart->wtFlags & TERM_VIRTUAL ); + pX = pStart->pExpr; + assert( pX!=0 ); + testcase( pStart->leftCursor!=iCur ); /* transitive constraints */ + r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); + sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); + VdbeComment((v, "pk")); + VdbeCoverageIf(v, pX->op==TK_GT); + VdbeCoverageIf(v, pX->op==TK_LE); + VdbeCoverageIf(v, pX->op==TK_LT); + VdbeCoverageIf(v, pX->op==TK_GE); + sqlite3ExprCacheAffinityChange(pParse, r1, 1); + sqlite3ReleaseTempReg(pParse, rTemp); + disableTerm(pLevel, pStart); + }else{ + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + } + if( pEnd ){ + Expr *pX; + pX = pEnd->pExpr; + assert( pX!=0 ); + assert( (pEnd->wtFlags & TERM_VNULL)==0 ); + testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */ + testcase( pEnd->wtFlags & TERM_VIRTUAL ); + memEndValue = ++pParse->nMem; + sqlite3ExprCode(pParse, pX->pRight, memEndValue); + if( pX->op==TK_LT || pX->op==TK_GT ){ + testOp = bRev ? OP_Le : OP_Ge; + }else{ + testOp = bRev ? OP_Lt : OP_Gt; + } + disableTerm(pLevel, pEnd); + } + start = sqlite3VdbeCurrentAddr(v); + pLevel->op = bRev ? OP_Prev : OP_Next; + pLevel->p1 = iCur; + pLevel->p2 = start; + assert( pLevel->p5==0 ); + if( testOp!=OP_Noop ){ + iRowidReg = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); + VdbeCoverageIf(v, testOp==OP_Le); + VdbeCoverageIf(v, testOp==OP_Lt); + VdbeCoverageIf(v, testOp==OP_Ge); + VdbeCoverageIf(v, testOp==OP_Gt); + sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); + } + }else if( pLoop->wsFlags & WHERE_INDEXED ){ + /* Case 4: A scan using an index. + ** + ** The WHERE clause may contain zero or more equality + ** terms ("==" or "IN" operators) that refer to the N + ** left-most columns of the index. It may also contain + ** inequality constraints (>, <, >= or <=) on the indexed + ** column that immediately follows the N equalities. Only + ** the right-most column can be an inequality - the rest must + ** use the "==" and "IN" operators. For example, if the + ** index is on (x,y,z), then the following clauses are all + ** optimized: + ** + ** x=5 + ** x=5 AND y=10 + ** x=5 AND y<10 + ** x=5 AND y>5 AND y<10 + ** x=5 AND y=5 AND z<=10 + ** + ** The z<10 term of the following cannot be used, only + ** the x=5 term: + ** + ** x=5 AND z<10 + ** + ** N may be zero if there are inequality constraints. + ** If there are no inequality constraints, then N is at + ** least one. + ** + ** This case is also used when there are no WHERE clause + ** constraints but an index is selected anyway, in order + ** to force the output order to conform to an ORDER BY. + */ + static const u8 aStartOp[] = { + 0, + 0, + OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ + OP_Last, /* 3: (!start_constraints && startEq && bRev) */ + OP_SeekGT, /* 4: (start_constraints && !startEq && !bRev) */ + OP_SeekLT, /* 5: (start_constraints && !startEq && bRev) */ + OP_SeekGE, /* 6: (start_constraints && startEq && !bRev) */ + OP_SeekLE /* 7: (start_constraints && startEq && bRev) */ + }; + static const u8 aEndOp[] = { + OP_IdxGE, /* 0: (end_constraints && !bRev && !endEq) */ + OP_IdxGT, /* 1: (end_constraints && !bRev && endEq) */ + OP_IdxLE, /* 2: (end_constraints && bRev && !endEq) */ + OP_IdxLT, /* 3: (end_constraints && bRev && endEq) */ + }; + u16 nEq = pLoop->u.btree.nEq; /* Number of == or IN terms */ + int regBase; /* Base register holding constraint values */ + WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ + WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ + int startEq; /* True if range start uses ==, >= or <= */ + int endEq; /* True if range end uses ==, >= or <= */ + int start_constraints; /* Start of range is constrained */ + int nConstraint; /* Number of constraint terms */ + Index *pIdx; /* The index we will be using */ + int iIdxCur; /* The VDBE cursor for the index */ + int nExtraReg = 0; /* Number of extra registers needed */ + int op; /* Instruction opcode */ + char *zStartAff; /* Affinity for start of range constraint */ + char cEndAff = 0; /* Affinity for end of range constraint */ + u8 bSeekPastNull = 0; /* True to seek past initial nulls */ + u8 bStopAtNull = 0; /* Add condition to terminate at NULLs */ + + pIdx = pLoop->u.btree.pIndex; + iIdxCur = pLevel->iIdxCur; + assert( nEq>=pLoop->nSkip ); + + /* If this loop satisfies a sort order (pOrderBy) request that + ** was passed to this function to implement a "SELECT min(x) ..." + ** query, then the caller will only allow the loop to run for + ** a single iteration. This means that the first row returned + ** should not have a NULL value stored in 'x'. If column 'x' is + ** the first one after the nEq equality constraints in the index, + ** this requires some special handling. + */ + assert( pWInfo->pOrderBy==0 + || pWInfo->pOrderBy->nExpr==1 + || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 ); + if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0 + && pWInfo->nOBSat>0 + && (pIdx->nKeyCol>nEq) + ){ + assert( pLoop->nSkip==0 ); + bSeekPastNull = 1; + nExtraReg = 1; + } + + /* Find any inequality constraint terms for the start and end + ** of the range. + */ + j = nEq; + if( pLoop->wsFlags & WHERE_BTM_LIMIT ){ + pRangeStart = pLoop->aLTerm[j++]; + nExtraReg = 1; + /* Like optimization range constraints always occur in pairs */ + assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || + (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 ); + } + if( pLoop->wsFlags & WHERE_TOP_LIMIT ){ + pRangeEnd = pLoop->aLTerm[j++]; + nExtraReg = 1; + if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){ + assert( pRangeStart!=0 ); /* LIKE opt constraints */ + assert( pRangeStart->wtFlags & TERM_LIKEOPT ); /* occur in pairs */ + pLevel->iLikeRepCntr = ++pParse->nMem; + testcase( bRev ); + testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC ); + sqlite3VdbeAddOp2(v, OP_Integer, + bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC), + pLevel->iLikeRepCntr); + VdbeComment((v, "LIKE loop counter")); + pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v); + } + if( pRangeStart==0 + && (j = pIdx->aiColumn[nEq])>=0 + && pIdx->pTable->aCol[j].notNull==0 + ){ + bSeekPastNull = 1; + } + } + assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 ); + + /* Generate code to evaluate all constraint terms using == or IN + ** and store the values of those terms in an array of registers + ** starting at regBase. + */ + regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff); + assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq ); + if( zStartAff ) cEndAff = zStartAff[nEq]; + addrNxt = pLevel->addrNxt; + + /* If we are doing a reverse order scan on an ascending index, or + ** a forward order scan on a descending index, interchange the + ** start and end terms (pRangeStart and pRangeEnd). + */ + if( (nEqnKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC)) + || (bRev && pIdx->nKeyCol==nEq) + ){ + SWAP(WhereTerm *, pRangeEnd, pRangeStart); + SWAP(u8, bSeekPastNull, bStopAtNull); + } + + testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 ); + testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 ); + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 ); + testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 ); + startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); + endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); + start_constraints = pRangeStart || nEq>0; + + /* Seek the index cursor to the start of the range. */ ++ codeCursorHint(pWInfo, iLevel); + nConstraint = nEq; + if( pRangeStart ){ + Expr *pRight = pRangeStart->pExpr->pRight; + sqlite3ExprCode(pParse, pRight, regBase+nEq); + whereLikeOptimizationStringFixup(v, pLevel, pRangeStart); + if( (pRangeStart->wtFlags & TERM_VNULL)==0 + && sqlite3ExprCanBeNull(pRight) + ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); + VdbeCoverage(v); + } + if( zStartAff ){ + if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){ + /* Since the comparison is to be performed with no conversions + ** applied to the operands, set the affinity to apply to pRight to + ** SQLITE_AFF_BLOB. */ + zStartAff[nEq] = SQLITE_AFF_BLOB; + } + if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ + zStartAff[nEq] = SQLITE_AFF_BLOB; + } + } + nConstraint++; + testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); + }else if( bSeekPastNull ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + nConstraint++; + startEq = 0; + start_constraints = 1; + } + codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff); + op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; + assert( op!=0 ); + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); + VdbeCoverage(v); + VdbeCoverageIf(v, op==OP_Rewind); testcase( op==OP_Rewind ); + VdbeCoverageIf(v, op==OP_Last); testcase( op==OP_Last ); + VdbeCoverageIf(v, op==OP_SeekGT); testcase( op==OP_SeekGT ); + VdbeCoverageIf(v, op==OP_SeekGE); testcase( op==OP_SeekGE ); + VdbeCoverageIf(v, op==OP_SeekLE); testcase( op==OP_SeekLE ); + VdbeCoverageIf(v, op==OP_SeekLT); testcase( op==OP_SeekLT ); + + /* Load the value for the inequality constraint at the end of the + ** range (if any). + */ + nConstraint = nEq; + if( pRangeEnd ){ + Expr *pRight = pRangeEnd->pExpr->pRight; + sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); + sqlite3ExprCode(pParse, pRight, regBase+nEq); + whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd); + if( (pRangeEnd->wtFlags & TERM_VNULL)==0 + && sqlite3ExprCanBeNull(pRight) + ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt); + VdbeCoverage(v); + } + if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB + && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff) + ){ + codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff); + } + nConstraint++; + testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); + }else if( bStopAtNull ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + endEq = 0; + nConstraint++; + } + sqlite3DbFree(db, zStartAff); + + /* Top of the loop body */ + pLevel->p2 = sqlite3VdbeCurrentAddr(v); + + /* Check if the index cursor is past the end of the range. */ + if( nConstraint ){ + op = aEndOp[bRev*2 + endEq]; + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); + testcase( op==OP_IdxGT ); VdbeCoverageIf(v, op==OP_IdxGT ); + testcase( op==OP_IdxGE ); VdbeCoverageIf(v, op==OP_IdxGE ); + testcase( op==OP_IdxLT ); VdbeCoverageIf(v, op==OP_IdxLT ); + testcase( op==OP_IdxLE ); VdbeCoverageIf(v, op==OP_IdxLE ); + } + + /* Seek the table cursor, if required */ + disableTerm(pLevel, pRangeStart); + disableTerm(pLevel, pRangeEnd); + if( omitTable ){ + /* pIdx is a covering index. No need to access the main table. */ + }else if( HasRowid(pIdx->pTable) ){ + iRowidReg = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ + }else if( iCur!=iIdxCur ){ + Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable); + iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol); + for(j=0; jnKeyCol; j++){ + k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]); + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j); + } + sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont, + iRowidReg, pPk->nKeyCol); VdbeCoverage(v); + } + + /* Record the instruction used to terminate the loop. Disable + ** WHERE clause terms made redundant by the index range scan. + */ + if( pLoop->wsFlags & WHERE_ONEROW ){ + pLevel->op = OP_Noop; + }else if( bRev ){ + pLevel->op = OP_Prev; + }else{ + pLevel->op = OP_Next; + } + pLevel->p1 = iIdxCur; + pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0; + if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){ + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; + }else{ + assert( pLevel->p5==0 ); + } + }else + + #ifndef SQLITE_OMIT_OR_OPTIMIZATION + if( pLoop->wsFlags & WHERE_MULTI_OR ){ + /* Case 5: Two or more separately indexed terms connected by OR + ** + ** Example: + ** + ** CREATE TABLE t1(a,b,c,d); + ** CREATE INDEX i1 ON t1(a); + ** CREATE INDEX i2 ON t1(b); + ** CREATE INDEX i3 ON t1(c); + ** + ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) + ** + ** In the example, there are three indexed terms connected by OR. + ** The top of the loop looks like this: + ** + ** Null 1 # Zero the rowset in reg 1 + ** + ** Then, for each indexed term, the following. The arguments to + ** RowSetTest are such that the rowid of the current row is inserted + ** into the RowSet. If it is already present, control skips the + ** Gosub opcode and jumps straight to the code generated by WhereEnd(). + ** + ** sqlite3WhereBegin() + ** RowSetTest # Insert rowid into rowset + ** Gosub 2 A + ** sqlite3WhereEnd() + ** + ** Following the above, code to terminate the loop. Label A, the target + ** of the Gosub above, jumps to the instruction right after the Goto. + ** + ** Null 1 # Zero the rowset in reg 1 + ** Goto B # The loop is finished. + ** + ** A: # Return data, whatever. + ** + ** Return 2 # Jump back to the Gosub + ** + ** B: + ** + ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then + ** use an ephemeral index instead of a RowSet to record the primary + ** keys of the rows we have already seen. + ** + */ + WhereClause *pOrWc; /* The OR-clause broken out into subterms */ + SrcList *pOrTab; /* Shortened table list or OR-clause generation */ + Index *pCov = 0; /* Potential covering index (or NULL) */ + int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */ + + int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ + int regRowset = 0; /* Register for RowSet object */ + int regRowid = 0; /* Register holding rowid */ + int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ + int iRetInit; /* Address of regReturn init */ + int untestedTerms = 0; /* Some terms not completely tested */ + int ii; /* Loop counter */ + u16 wctrlFlags; /* Flags for sub-WHERE clause */ + Expr *pAndExpr = 0; /* An ".. AND (...)" expression */ + Table *pTab = pTabItem->pTab; + + pTerm = pLoop->aLTerm[0]; + assert( pTerm!=0 ); + assert( pTerm->eOperator & WO_OR ); + assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); + pOrWc = &pTerm->u.pOrInfo->wc; + pLevel->op = OP_Return; + pLevel->p1 = regReturn; + + /* Set up a new SrcList in pOrTab containing the table being scanned + ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. + ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). + */ + if( pWInfo->nLevel>1 ){ + int nNotReady; /* The number of notReady tables */ + struct SrcList_item *origSrc; /* Original list of tables */ + nNotReady = pWInfo->nLevel - iLevel - 1; + pOrTab = sqlite3StackAllocRaw(db, + sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); + if( pOrTab==0 ) return notReady; + pOrTab->nAlloc = (u8)(nNotReady + 1); + pOrTab->nSrc = pOrTab->nAlloc; + memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); + origSrc = pWInfo->pTabList->a; + for(k=1; k<=nNotReady; k++){ + memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); + } + }else{ + pOrTab = pWInfo->pTabList; + } + + /* Initialize the rowset register to contain NULL. An SQL NULL is + ** equivalent to an empty rowset. Or, create an ephemeral index + ** capable of holding primary keys in the case of a WITHOUT ROWID. + ** + ** Also initialize regReturn to contain the address of the instruction + ** immediately following the OP_Return at the bottom of the loop. This + ** is required in a few obscure LEFT JOIN cases where control jumps + ** over the top of the loop into the body of it. In this case the + ** correct response for the end-of-loop code (the OP_Return) is to + ** fall through to the next instruction, just as an OP_Next does if + ** called on an uninitialized cursor. + */ + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ + if( HasRowid(pTab) ){ + regRowset = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + regRowset = pParse->nTab++; + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol); + sqlite3VdbeSetP4KeyInfo(pParse, pPk); + } + regRowid = ++pParse->nMem; + } + iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); + + /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y + ** Then for every term xN, evaluate as the subexpression: xN AND z + ** That way, terms in y that are factored into the disjunction will + ** be picked up by the recursive calls to sqlite3WhereBegin() below. + ** + ** Actually, each subexpression is converted to "xN AND w" where w is + ** the "interesting" terms of z - terms that did not originate in the + ** ON or USING clause of a LEFT JOIN, and terms that are usable as + ** indices. + ** + ** This optimization also only applies if the (x1 OR x2 OR ...) term + ** is not contained in the ON clause of a LEFT JOIN. + ** See ticket http://www.sqlite.org/src/info/f2369304e4 + */ + if( pWC->nTerm>1 ){ + int iTerm; + for(iTerm=0; iTermnTerm; iTerm++){ + Expr *pExpr = pWC->a[iTerm].pExpr; + if( &pWC->a[iTerm] == pTerm ) continue; + if( ExprHasProperty(pExpr, EP_FromJoin) ) continue; + if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue; + if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue; + testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO ); + pExpr = sqlite3ExprDup(db, pExpr, 0); + pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr); + } + if( pAndExpr ){ + pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0); + } + } + + /* Run a separate WHERE clause for each term of the OR clause. After + ** eliminating duplicates from other WHERE clauses, the action for each + ** sub-WHERE clause is to to invoke the main loop body as a subroutine. + */ + wctrlFlags = WHERE_OMIT_OPEN_CLOSE + | WHERE_FORCE_TABLE + | WHERE_ONETABLE_ONLY + | WHERE_NO_AUTOINDEX; + for(ii=0; iinTerm; ii++){ + WhereTerm *pOrTerm = &pOrWc->a[ii]; + if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){ + WhereInfo *pSubWInfo; /* Info for single OR-term scan */ + Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */ + int j1 = 0; /* Address of jump operation */ + if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){ + pAndExpr->pLeft = pOrExpr; + pOrExpr = pAndExpr; + } + /* Loop through table entries that match term pOrTerm. */ + WHERETRACE(0xffff, ("Subplan for OR-clause:\n")); + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0, + wctrlFlags, iCovCur); + assert( pSubWInfo || pParse->nErr || db->mallocFailed ); + if( pSubWInfo ){ + WhereLoop *pSubLoop; + int addrExplain = sqlite3WhereExplainOneScan( + pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 + ); + sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain); + + /* This is the sub-WHERE clause body. First skip over + ** duplicate rows from prior sub-WHERE clauses, and record the + ** rowid (or PRIMARY KEY) for the current row so that the same + ** row will be skipped in subsequent sub-WHERE clauses. + */ + if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ + int r; + int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); + if( HasRowid(pTab) ){ + r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0); + j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet); + VdbeCoverage(v); + }else{ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + int nPk = pPk->nKeyCol; + int iPk; + + /* Read the PK into an array of temp registers. */ + r = sqlite3GetTempRange(pParse, nPk); + for(iPk=0; iPkaiColumn[iPk]; + int rx; + rx = sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur,r+iPk,0); + if( rx!=r+iPk ){ + sqlite3VdbeAddOp2(v, OP_SCopy, rx, r+iPk); + } + } + + /* Check if the temp table already contains this key. If so, + ** the row has already been included in the result set and + ** can be ignored (by jumping past the Gosub below). Otherwise, + ** insert the key into the temp table and proceed with processing + ** the row. + ** + ** Use some of the same optimizations as OP_RowSetTest: If iSet + ** is zero, assume that the key cannot already be present in + ** the temp table. And if iSet is -1, assume that there is no + ** need to insert the key into the temp table, as it will never + ** be tested for. */ + if( iSet ){ + j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk); + VdbeCoverage(v); + } + if( iSet>=0 ){ + sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid); + sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0); + if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + } + + /* Release the array of temp registers */ + sqlite3ReleaseTempRange(pParse, r, nPk); + } + } + + /* Invoke the main loop body as a subroutine */ + sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); + + /* Jump here (skipping the main loop body subroutine) if the + ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */ + if( j1 ) sqlite3VdbeJumpHere(v, j1); + + /* The pSubWInfo->untestedTerms flag means that this OR term + ** contained one or more AND term from a notReady table. The + ** terms from the notReady table could not be tested and will + ** need to be tested later. + */ + if( pSubWInfo->untestedTerms ) untestedTerms = 1; + + /* If all of the OR-connected terms are optimized using the same + ** index, and the index is opened using the same cursor number + ** by each call to sqlite3WhereBegin() made by this loop, it may + ** be possible to use that index as a covering index. + ** + ** If the call to sqlite3WhereBegin() above resulted in a scan that + ** uses an index, and this is either the first OR-connected term + ** processed or the index is the same as that used by all previous + ** terms, set pCov to the candidate covering index. Otherwise, set + ** pCov to NULL to indicate that no candidate covering index will + ** be available. + */ + pSubLoop = pSubWInfo->a[0].pWLoop; + assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 ); + if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0 + && (ii==0 || pSubLoop->u.btree.pIndex==pCov) + && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex)) + ){ + assert( pSubWInfo->a[0].iIdxCur==iCovCur ); + pCov = pSubLoop->u.btree.pIndex; + wctrlFlags |= WHERE_REOPEN_IDX; + }else{ + pCov = 0; + } + + /* Finish the loop through table entries that match term pOrTerm. */ + sqlite3WhereEnd(pSubWInfo); + } + } + } + pLevel->u.pCovidx = pCov; + if( pCov ) pLevel->iIdxCur = iCovCur; + if( pAndExpr ){ + pAndExpr->pLeft = 0; + sqlite3ExprDelete(db, pAndExpr); + } + sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); + sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); + sqlite3VdbeResolveLabel(v, iLoopBody); + + if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab); + if( !untestedTerms ) disableTerm(pLevel, pTerm); + }else + #endif /* SQLITE_OMIT_OR_OPTIMIZATION */ + + { + /* Case 6: There is no usable index. We must do a complete + ** scan of the entire table. + */ + static const u8 aStep[] = { OP_Next, OP_Prev }; + static const u8 aStart[] = { OP_Rewind, OP_Last }; + assert( bRev==0 || bRev==1 ); + if( pTabItem->isRecursive ){ + /* Tables marked isRecursive have only a single row that is stored in + ** a pseudo-cursor. No need to Rewind or Next such cursors. */ + pLevel->op = OP_Noop; + }else{ ++ codeCursorHint(pWInfo, iLevel); + pLevel->op = aStep[bRev]; + pLevel->p1 = iCur; + pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); + VdbeCoverageIf(v, bRev==0); + VdbeCoverageIf(v, bRev!=0); + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; + } + } + + #ifdef SQLITE_ENABLE_STMT_SCANSTATUS + pLevel->addrVisit = sqlite3VdbeCurrentAddr(v); + #endif + + /* Insert code to test every subexpression that can be completely + ** computed using the current set of tables. + */ + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ + Expr *pE; + int skipLikeAddr = 0; + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + testcase( pTerm->wtFlags & TERM_CODED ); + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ + testcase( pWInfo->untestedTerms==0 + && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); + pWInfo->untestedTerms = 1; + continue; + } + pE = pTerm->pExpr; + assert( pE!=0 ); + if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ + continue; + } + if( pTerm->wtFlags & TERM_LIKECOND ){ + assert( pLevel->iLikeRepCntr>0 ); + skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr); + VdbeCoverage(v); + } + sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); + if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr); + pTerm->wtFlags |= TERM_CODED; + } + + /* Insert code to test for implied constraints based on transitivity + ** of the "==" operator. + ** + ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123" + ** and we are coding the t1 loop and the t2 loop has not yet coded, + ** then we cannot use the "t1.a=t2.b" constraint, but we can code + ** the implied "t1.a=123" constraint. + */ + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ + Expr *pE, *pEAlt; + WhereTerm *pAlt; + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue; + if( (pTerm->eOperator & WO_EQUIV)==0 ) continue; + if( pTerm->leftCursor!=iCur ) continue; + if( pLevel->iLeftJoin ) continue; + pE = pTerm->pExpr; + assert( !ExprHasProperty(pE, EP_FromJoin) ); + assert( (pTerm->prereqRight & pLevel->notReady)!=0 ); + pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady, + WO_EQ|WO_IN|WO_IS, 0); + if( pAlt==0 ) continue; + if( pAlt->wtFlags & (TERM_CODED) ) continue; + testcase( pAlt->eOperator & WO_EQ ); + testcase( pAlt->eOperator & WO_IS ); + testcase( pAlt->eOperator & WO_IN ); + VdbeModuleComment((v, "begin transitive constraint")); + pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt)); + if( pEAlt ){ + *pEAlt = *pAlt->pExpr; + pEAlt->pLeft = pE->pLeft; + sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL); + sqlite3StackFree(db, pEAlt); + } + } + + /* For a LEFT OUTER JOIN, generate code that will record the fact that + ** at least one row of the right table has matched the left table. + */ + if( pLevel->iLeftJoin ){ + pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); + VdbeComment((v, "record LEFT JOIN hit")); + sqlite3ExprCacheClear(pParse); + for(pTerm=pWC->a, j=0; jnTerm; j++, pTerm++){ + testcase( pTerm->wtFlags & TERM_VIRTUAL ); + testcase( pTerm->wtFlags & TERM_CODED ); + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->prereqAll & pLevel->notReady)!=0 ){ + assert( pWInfo->untestedTerms ); + continue; + } + assert( pTerm->pExpr ); + sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); + pTerm->wtFlags |= TERM_CODED; + } + } + + return pLevel->notReady; + }