/ Check-in [c39cb0e2]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge recent trunk enhancements and fixes.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: c39cb0e2571f58c87053de009e2c135d71b2c3af
User & Date: drh 2015-06-11 18:01:29
Context
2015-06-17
18:18
Merge all recent enhancements and fixes from trunk. check-in: 199bfb67 user: drh tags: sessions
2015-06-11
18:01
Merge recent trunk enhancements and fixes. check-in: c39cb0e2 user: drh tags: sessions
17:26
Add the sqlite3changegroup_xxx() APIs to the sessions module. For combining multiple changesets or patchsets. check-in: 0c1a901c user: dan tags: sessions
14:19
Remove stray outputs from the test suite. check-in: afc6db9b user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   180    180            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   181    181            memjournal.lo \
   182    182            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
   183    183            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   184    184            pager.lo parse.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   185    185            random.lo resolve.lo rowset.lo rtree.lo \
   186    186            sqlite3session.lo select.lo status.lo \
   187         -         table.lo threads.lo tokenize.lo trigger.lo \
          187  +         table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
   188    188            update.lo util.lo vacuum.lo \
   189    189            vdbe.lo vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   190         -         vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo
          190  +         vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \
          191  +         utf.lo vtab.lo
   191    192   
   192    193   # Object files for the amalgamation.
   193    194   #
   194    195   LIBOBJS1 = sqlite3.lo
   195    196   
   196    197   # Determine the real value of LIBOBJ based on the 'configure' script
   197    198   #
................................................................................
   272    273     $(TOP)/src/sqlite3ext.h \
   273    274     $(TOP)/src/sqliteInt.h \
   274    275     $(TOP)/src/sqliteLimit.h \
   275    276     $(TOP)/src/table.c \
   276    277     $(TOP)/src/threads.c \
   277    278     $(TOP)/src/tclsqlite.c \
   278    279     $(TOP)/src/tokenize.c \
          280  +  $(TOP)/src/treeview.c \
   279    281     $(TOP)/src/trigger.c \
   280    282     $(TOP)/src/utf.c \
   281    283     $(TOP)/src/update.c \
   282    284     $(TOP)/src/util.c \
   283    285     $(TOP)/src/vacuum.c \
   284    286     $(TOP)/src/vdbe.c \
   285    287     $(TOP)/src/vdbe.h \
................................................................................
   292    294     $(TOP)/src/vdbeInt.h \
   293    295     $(TOP)/src/vtab.c \
   294    296     $(TOP)/src/vxworks.h \
   295    297     $(TOP)/src/wal.c \
   296    298     $(TOP)/src/wal.h \
   297    299     $(TOP)/src/walker.c \
   298    300     $(TOP)/src/where.c \
          301  +  $(TOP)/src/wherecode.c \
          302  +  $(TOP)/src/whereexpr.c \
   299    303     $(TOP)/src/whereInt.h
   300    304   
   301    305   # Source code for extensions
   302    306   #
   303    307   SRC += \
   304    308     $(TOP)/ext/fts1/fts1.c \
   305    309     $(TOP)/ext/fts1/fts1.h \
................................................................................
   456    460     $(TOP)/src/util.c \
   457    461     $(TOP)/src/vdbeapi.c \
   458    462     $(TOP)/src/vdbeaux.c \
   459    463     $(TOP)/src/vdbe.c \
   460    464     $(TOP)/src/vdbemem.c \
   461    465     $(TOP)/src/vdbetrace.c \
   462    466     $(TOP)/src/where.c \
          467  +  $(TOP)/src/wherecode.c \
          468  +  $(TOP)/src/whereexpr.c \
   463    469     parse.c \
   464    470     $(TOP)/ext/fts3/fts3.c \
   465    471     $(TOP)/ext/fts3/fts3_aux.c \
   466    472     $(TOP)/ext/fts3/fts3_expr.c \
   467    473     $(TOP)/ext/fts3/fts3_term.c \
   468    474     $(TOP)/ext/fts3/fts3_tokenizer.c \
   469    475     $(TOP)/ext/fts3/fts3_write.c \
................................................................................
   532    538   # Databases containing fuzzer test cases
   533    539   #
   534    540   FUZZDATA = \
   535    541     $(TOP)/test/fuzzdata1.db \
   536    542     $(TOP)/test/fuzzdata2.db \
   537    543     $(TOP)/test/fuzzdata3.db
   538    544   
          545  +# Standard options to testfixture
          546  +#
          547  +TESTOPTS = --verbose=file --output=test-out.txt
          548  +
   539    549   # This is the default Makefile target.  The objects listed here
   540    550   # are what get build when you type just "make" with no arguments.
   541    551   #
   542    552   all:	sqlite3.h libsqlite3.la sqlite3$(TEXE) $(HAVE_TCL:1=libtclsqlite3.la)
   543    553   
   544    554   Makefile: $(TOP)/Makefile.in
   545    555   	./config.status
................................................................................
   805    815   
   806    816   threads.lo:	$(TOP)/src/threads.c $(HDR)
   807    817   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/threads.c
   808    818   
   809    819   tokenize.lo:	$(TOP)/src/tokenize.c keywordhash.h $(HDR)
   810    820   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/tokenize.c
   811    821   
          822  +treeview.lo:	$(TOP)/src/treeview.c $(HDR)
          823  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/treeview.c
          824  +
   812    825   trigger.lo:	$(TOP)/src/trigger.c $(HDR)
   813    826   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/trigger.c
   814    827   
   815    828   update.lo:	$(TOP)/src/update.c $(HDR)
   816    829   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/update.c
   817    830   
   818    831   utf.lo:	$(TOP)/src/utf.c $(HDR)
................................................................................
   853    866   
   854    867   walker.lo:	$(TOP)/src/walker.c $(HDR)
   855    868   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/walker.c
   856    869   
   857    870   where.lo:	$(TOP)/src/where.c $(HDR)
   858    871   	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/where.c
   859    872   
          873  +wherecode.lo:	$(TOP)/src/wherecode.c $(HDR)
          874  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/wherecode.c
          875  +
          876  +whereexpr.lo:	$(TOP)/src/whereexpr.c $(HDR)
          877  +	$(LTCOMPILE) $(TEMP_STORE) -c $(TOP)/src/whereexpr.c
          878  +
   860    879   tclsqlite.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   861    880   	$(LTCOMPILE) -DUSE_TCL_STUBS=1 -c $(TOP)/src/tclsqlite.c
   862    881   
   863    882   tclsqlite-shell.lo:	$(TOP)/src/tclsqlite.c $(HDR)
   864    883   	$(LTCOMPILE) -DTCLSH=1 -o $@ -c $(TOP)/src/tclsqlite.c
   865    884   
   866    885   tclsqlite-stubs.lo:	$(TOP)/src/tclsqlite.c $(HDR)
................................................................................
   985   1004   
   986   1005   testfixture$(TEXE):	$(TESTFIXTURE_SRC)
   987   1006   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TEMP_STORE) $(TESTFIXTURE_FLAGS) \
   988   1007   		-o $@ $(TESTFIXTURE_SRC) $(LIBTCL) $(TLIBS)
   989   1008   
   990   1009   # A very detailed test running most or all test cases
   991   1010   fulltest:	$(TESTPROGS) fuzztest
   992         -	./testfixture$(TEXE) $(TOP)/test/all.test
         1011  +	./testfixture$(TEXE) $(TOP)/test/all.test $(TESTOPTS)
   993   1012   
   994   1013   # Really really long testing
   995   1014   soaktest:	$(TESTPROGS)
   996         -	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1
         1015  +	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1 $(TESTOPTS)
   997   1016   
   998   1017   # Do extra testing but not everything.
   999   1018   fulltestonly:	$(TESTPROGS)
  1000   1019   	./testfixture$(TEXE) $(TOP)/test/full.test
  1001   1020   
  1002   1021   # Fuzz testing
  1003         -fuzztest:	fuzzcheck$(TEXE)
         1022  +fuzztest:	fuzzcheck$(TEXE) $(FUZZDATA)
  1004   1023   	./fuzzcheck$(TEXE) $(FUZZDATA)
  1005   1024   
  1006         -# This is the common case.  Run many tests but not those that take
  1007         -# a really long time.
         1025  +valgrindfuzz:	fuzzcheck$(TEXT) $(FUZZDATA)
         1026  +	valgrind ./fuzzcheck$(TEXE) --cell-size-check --quiet $(FUZZDATA)
         1027  +
         1028  +# Minimal testing that runs in less than 3 minutes
         1029  +#
         1030  +quicktest:	./testfixture$(TEXE)
         1031  +	./testfixture$(TEXE) $(TOP)/test/extraquick.test $(TESTOPTS)
         1032  +
         1033  +# This is the common case.  Run many tests that do not take too long,
         1034  +# including fuzzcheck, sqlite3_analyzer, and sqldiff tests.
  1008   1035   #
  1009   1036   test:	$(TESTPROGS) fuzztest
  1010         -	./testfixture$(TEXE) $(TOP)/test/veryquick.test
         1037  +	./testfixture$(TEXE) $(TOP)/test/veryquick.test $(TESTOPTS)
  1011   1038   
  1012   1039   # Run a test using valgrind.  This can take a really long time
  1013   1040   # because valgrind is so much slower than a native machine.
  1014   1041   #
  1015         -valgrindtest:	$(TESTPROGS) fuzzcheck$(TEXE)
  1016         -	valgrind -v ./fuzzcheck$(TEXE) --cell-size-check --quiet $(FUZZDATA)
  1017         -	OMIT_MISUSE=1 valgrind -v ./testfixture$(TEXE) $(TOP)/test/permutations.test valgrind
         1042  +valgrindtest:	$(TESTPROGS) valgrindfuzz
         1043  +	OMIT_MISUSE=1 valgrind -v ./testfixture$(TEXE) $(TOP)/test/permutations.test valgrind $(TESTOPTS)
  1018   1044   
  1019   1045   # A very fast test that checks basic sanity.  The name comes from
  1020   1046   # the 60s-era electronics testing:  "Turn it on and see if smoke
  1021   1047   # comes out."
  1022   1048   #
  1023   1049   smoketest:	$(TESTPROGS) fuzzcheck$(TEXE)
  1024         -	./testfixture$(TEXE) $(TOP)/test/main.test
         1050  +	./testfixture$(TEXE) $(TOP)/test/main.test $(TESTOPTS)
  1025   1051   
  1026   1052   sqlite3_analyzer.c: sqlite3.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
  1027   1053   	echo "#define TCLSH 2" > $@
  1028   1054   	echo "#define SQLITE_ENABLE_DBSTAT_VTAB 1" >> $@
  1029   1055   	cat sqlite3.c $(TOP)/src/tclsqlite.c >> $@
  1030   1056   	echo "static const char *tclsh_main_loop(void){" >> $@
  1031   1057   	echo "static const char *zMainloop = " >> $@

Changes to Makefile.msc.

   842    842            main.lo malloc.lo mem0.lo mem1.lo mem2.lo mem3.lo mem5.lo \
   843    843            memjournal.lo \
   844    844            mutex.lo mutex_noop.lo mutex_unix.lo mutex_w32.lo \
   845    845            notify.lo opcodes.lo os.lo os_unix.lo os_win.lo \
   846    846            pager.lo pcache.lo pcache1.lo pragma.lo prepare.lo printf.lo \
   847    847            random.lo resolve.lo rowset.lo rtree.lo \
   848    848            sqlite3session.lo select.lo status.lo \
   849         -         table.lo threads.lo tokenize.lo trigger.lo \
          849  +         table.lo threads.lo tokenize.lo treeview.lo trigger.lo \
   850    850            update.lo util.lo vacuum.lo \
   851    851            vdbeapi.lo vdbeaux.lo vdbeblob.lo vdbemem.lo vdbesort.lo \
   852         -         vdbetrace.lo wal.lo walker.lo where.lo utf.lo vtab.lo
          852  +         vdbetrace.lo wal.lo walker.lo where.lo wherecode.lo whereexpr.lo \
          853  +         utf.lo vtab.lo
   853    854   
   854    855   # Object files for the amalgamation.
   855    856   #
   856    857   LIBOBJS1 = sqlite3.lo
   857    858   
   858    859   # Determine the real value of LIBOBJ based on the 'configure' script
   859    860   #
................................................................................
   946    947     $(TOP)\src\sqlite3ext.h \
   947    948     $(TOP)\src\sqliteInt.h \
   948    949     $(TOP)\src\sqliteLimit.h \
   949    950     $(TOP)\src\table.c \
   950    951     $(TOP)\src\threads.c \
   951    952     $(TOP)\src\tclsqlite.c \
   952    953     $(TOP)\src\tokenize.c \
          954  +  $(TOP)\src\treeview.c \
   953    955     $(TOP)\src\trigger.c \
   954    956     $(TOP)\src\utf.c \
   955    957     $(TOP)\src\update.c \
   956    958     $(TOP)\src\util.c \
   957    959     $(TOP)\src\vacuum.c \
   958    960     $(TOP)\src\vdbe.c \
   959    961     $(TOP)\src\vdbe.h \
................................................................................
   966    968     $(TOP)\src\vdbeInt.h \
   967    969     $(TOP)\src\vtab.c \
   968    970     $(TOP)\src\vxworks.h \
   969    971     $(TOP)\src\wal.c \
   970    972     $(TOP)\src\wal.h \
   971    973     $(TOP)\src\walker.c \
   972    974     $(TOP)\src\where.c \
          975  +  $(TOP)\src\wherecode.c \
          976  +  $(TOP)\src\whereexpr.c \
   973    977     $(TOP)\src\whereInt.h
   974    978   
   975    979   # Source code for extensions
   976    980   #
   977    981   SRC3 = \
   978    982     $(TOP)\ext\fts1\fts1.c \
   979    983     $(TOP)\ext\fts1\fts1.h \
................................................................................
  1132   1136     $(TOP)\src\vdbeapi.c \
  1133   1137     $(TOP)\src\vdbeaux.c \
  1134   1138     $(TOP)\src\vdbe.c \
  1135   1139     $(TOP)\src\vdbemem.c \
  1136   1140     $(TOP)\src\vdbesort.c \
  1137   1141     $(TOP)\src\vdbetrace.c \
  1138   1142     $(TOP)\src\where.c \
         1143  +  $(TOP)\src\wherecode.c \
         1144  +  $(TOP)\src\whereexpr.c \
  1139   1145     parse.c \
  1140   1146     $(TOP)\ext\fts3\fts3.c \
  1141   1147     $(TOP)\ext\fts3\fts3_aux.c \
  1142   1148     $(TOP)\ext\fts3\fts3_expr.c \
  1143   1149     $(TOP)\ext\fts3\fts3_tokenizer.c \
  1144   1150     $(TOP)\ext\fts3\fts3_tokenize_vtab.c \
  1145   1151     $(TOP)\ext\fts3\fts3_unicode.c \
................................................................................
  1217   1223   # Databases containing fuzzer test cases
  1218   1224   #
  1219   1225   FUZZDATA = \
  1220   1226     $(TOP)\test\fuzzdata1.db \
  1221   1227     $(TOP)\test\fuzzdata2.db \
  1222   1228     $(TOP)\test\fuzzdata3.db
  1223   1229   
         1230  +# Standard options to testfixture
         1231  +#
         1232  +TESTOPTS = --verbose=file --output=test-out.txt
  1224   1233   
  1225   1234   # This is the default Makefile target.  The objects listed here
  1226   1235   # are what get build when you type just "make" with no arguments.
  1227   1236   #
  1228   1237   all:	dll libsqlite3.lib sqlite3.exe libtclsqlite3.lib
  1229   1238   
  1230   1239   libsqlite3.lib:	$(LIBOBJ)
................................................................................
  1503   1512   
  1504   1513   threads.lo:	$(TOP)\src\threads.c $(HDR)
  1505   1514   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\threads.c
  1506   1515   
  1507   1516   tokenize.lo:	$(TOP)\src\tokenize.c keywordhash.h $(HDR)
  1508   1517   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\tokenize.c
  1509   1518   
         1519  +treeview.lo:	$(TOP)\src\treeview.c $(HDR)
         1520  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\treeview.c
         1521  +
  1510   1522   trigger.lo:	$(TOP)\src\trigger.c $(HDR)
  1511   1523   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\trigger.c
  1512   1524   
  1513   1525   update.lo:	$(TOP)\src\update.c $(HDR)
  1514   1526   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\update.c
  1515   1527   
  1516   1528   utf.lo:	$(TOP)\src\utf.c $(HDR)
................................................................................
  1551   1563   
  1552   1564   walker.lo:	$(TOP)\src\walker.c $(HDR)
  1553   1565   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\walker.c
  1554   1566   
  1555   1567   where.lo:	$(TOP)\src\where.c $(HDR)
  1556   1568   	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\where.c
  1557   1569   
         1570  +wherecode.lo:	$(TOP)\src\wherecode.c $(HDR)
         1571  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\wherecode.c
         1572  +
         1573  +whereexpr.lo:	$(TOP)\src\whereexpr.c $(HDR)
         1574  +	$(LTCOMPILE) $(CORE_COMPILE_OPTS) -c $(TOP)\src\whereexpr.c
         1575  +
  1558   1576   tclsqlite.lo:	$(TOP)\src\tclsqlite.c $(HDR)
  1559   1577   	$(LTCOMPILE) $(NO_WARN) -DUSE_TCL_STUBS=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  1560   1578   
  1561   1579   tclsqlite-shell.lo:	$(TOP)\src\tclsqlite.c $(HDR)
  1562   1580   	$(LTCOMPILE) $(NO_WARN) -DTCLSH=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  1563   1581   
  1564   1582   tclsqlite3.exe:	tclsqlite-shell.lo $(SQLITE3C) $(LIBRESOBJS)
................................................................................
  1686   1704   testfixture.exe:	$(TESTFIXTURE_SRC) $(LIBRESOBJS) $(HDR)
  1687   1705   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TESTFIXTURE_FLAGS) \
  1688   1706   		-DBUILD_sqlite -I$(TCLINCDIR) \
  1689   1707   		$(TESTFIXTURE_SRC) \
  1690   1708   		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LIBRESOBJS) $(LTLIBS) $(TLIBS)
  1691   1709   
  1692   1710   extensiontest: testfixture.exe testloadext.dll
  1693         -	.\testfixture.exe $(TOP)\test\loadext.test
         1711  +	.\testfixture.exe $(TOP)\test\loadext.test $(TESTOPTS)
  1694   1712   
  1695   1713   fulltest:	$(TESTPROGS) fuzztest
  1696         -	.\testfixture.exe $(TOP)\test\all.test
         1714  +	.\testfixture.exe $(TOP)\test\all.test $(TESTOPTS)
  1697   1715   
  1698   1716   soaktest:	$(TESTPROGS)
  1699         -	.\testfixture.exe $(TOP)\test\all.test -soak=1
         1717  +	.\testfixture.exe $(TOP)\test\all.test -soak=1 $(TESTOPTS)
  1700   1718   
  1701   1719   fulltestonly:	$(TESTPROGS) fuzztest
  1702   1720   	.\testfixture.exe $(TOP)\test\full.test
  1703   1721   
  1704   1722   queryplantest:	testfixture.exe sqlite3.exe
  1705         -	.\testfixture.exe $(TOP)\test\permutations.test queryplanner
         1723  +	.\testfixture.exe $(TOP)\test\permutations.test queryplanner $(TESTOPTS)
  1706   1724   
  1707   1725   fuzztest:	fuzzcheck.exe
  1708   1726   	.\fuzzcheck.exe $(FUZZDATA)
  1709   1727   
         1728  +# Minimal testing that runs in less than 3 minutes (on a fast machine)
         1729  +#
         1730  +quicktest:	testfixture.exe
         1731  +	.\testfixture.exe $(TOP)\test\extraquick.test $(TESTOPTS)
         1732  +
         1733  +# This is the common case.  Run many tests that do not take too long,
         1734  +# including fuzzcheck, sqlite3_analyzer, and sqldiff tests.
         1735  +#
  1710   1736   test:	$(TESTPROGS) fuzztest
  1711         -	.\testfixture.exe $(TOP)\test\veryquick.test
         1737  +	.\testfixture.exe $(TOP)\test\veryquick.test $(TESTOPTS)
  1712   1738   
  1713   1739   smoketest:	$(TESTPROGS)
  1714         -	.\testfixture.exe $(TOP)\test\main.test
         1740  +	.\testfixture.exe $(TOP)\test\main.test $(TESTOPTS)
  1715   1741   
  1716   1742   sqlite3_analyzer.c: $(SQLITE3C) $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
  1717   1743   	echo #define TCLSH 2 > $@
  1718   1744   	echo #define SQLITE_ENABLE_DBSTAT_VTAB 1 >> $@
  1719   1745   	copy $@ + $(SQLITE3C) + $(TOP)\src\tclsqlite.c $@
  1720   1746   	echo static const char *tclsh_main_loop(void){ >> $@
  1721   1747   	echo static const char *zMainloop = >> $@

Changes to README.md.

   174    174   
   175    175   Unfortunately, years of effort have gone into optimizating SQLite, both
   176    176   for small size and high performance.  And optimizations tend to result in
   177    177   complex code.  So there is a lot of complexity in the SQLite implementation.
   178    178   
   179    179   Key files:
   180    180   
   181         -  *  **sqlite3.h** - This file defines the public interface to the SQLite
          181  +  *  **sqlite.h.in** - This file defines the public interface to the SQLite
   182    182        library.  Readers will need to be familiar with this interface before
   183    183        trying to understand how the library works internally.
   184    184   
   185    185     *  **sqliteInt.h** - this header file defines many of the data objects
   186    186        used internally by SQLite.
   187    187   
   188    188     *  **parse.y** - This file describes the LALR(1) grammer that SQLite uses
   189         -     to parse SQL statements, and the actions that are taken at each stop
          189  +     to parse SQL statements, and the actions that are taken at each step
   190    190        in the parsing process.
   191    191   
   192    192     *  **vdbe.c** - This file implements the virtual machine that runs
   193    193        prepared statements.  There are various helper files whose names
   194    194        begin with "vdbe".  The VDBE has access to the vdbeInt.h header file
   195    195        which defines internal data objects.  The rest of SQLite interacts
   196    196        with the VDBE through an interface defined by vdbe.h.
................................................................................
   205    205   
   206    206     *  **pager.c** - This file contains the "pager" implementation, the
   207    207        module that implements transactions.
   208    208   
   209    209     *  **os_unix.c** and **os_win.c** - These two files implement the interface
   210    210        between SQLite and the underlying operating system using the run-time
   211    211        pluggable VFS interface.
          212  +
          213  +  *  **shell.c** - This file is not part of the core SQLite library.  This
          214  +     is the file that, when linked against sqlite3.a, generates the
          215  +     "sqlite3.exe" command-line shell.
          216  +
          217  +  *  **tclsqlite.c** - This file implements the Tcl bindings for SQLite.  It
          218  +     is not part of the core SQLite library.  But as most of the tests in this
          219  +     repository are written in Tcl, the Tcl language bindings are important.
          220  +
          221  +There are many other source files.  Each has a suscinct header comment that
          222  +describes its purpose and role within the larger system.
   212    223   
   213    224   
   214    225   ## Contacts
   215    226   
   216    227   The main SQLite webpage is [http://www.sqlite.org/](http://www.sqlite.org/)
   217    228   with geographically distributed backup servers at
   218    229   [http://www2.sqlite.org/](http://www2.sqlite.org) and
   219    230   [http://www3.sqlite.org/](http://www3.sqlite.org).

Changes to configure.

 10703  10703       fi
 10704  10704   
 10705  10705       # Start autosearch by asking tclsh
 10706  10706       if test x"${ac_cv_c_tclconfig}" = x ; then
 10707  10707         if test x"$cross_compiling" = xno; then
 10708  10708           for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}`
 10709  10709           do
        10710  +          if test -f "$i/tclConfig.sh" ; then
        10711  +            ac_cv_c_tclconfig="$i"
        10712  +            break
        10713  +          fi
        10714  +        done
        10715  +      fi
        10716  +    fi
        10717  +
        10718  +    # On ubuntu 14.10, $auto_path on tclsh is not quite correct.
        10719  +    # So try again after applying corrections.
        10720  +    if test x"${ac_cv_c_tclconfig}" = x ; then
        10721  +      if test x"$cross_compiling" = xno; then
        10722  +        for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD} | sed 's,/tcltk/tcl,/tcl,g'`
        10723  +        do
 10710  10724             if test -f "$i/tclConfig.sh" ; then
 10711  10725               ac_cv_c_tclconfig="$i"
 10712  10726               break
 10713  10727             fi
 10714  10728           done
 10715  10729         fi
 10716  10730       fi

Changes to configure.ac.

   308    308       fi
   309    309   
   310    310       # Start autosearch by asking tclsh
   311    311       if test x"${ac_cv_c_tclconfig}" = x ; then
   312    312         if test x"$cross_compiling" = xno; then
   313    313           for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD}`
   314    314           do
          315  +          if test -f "$i/tclConfig.sh" ; then
          316  +            ac_cv_c_tclconfig="$i"
          317  +            break
          318  +          fi
          319  +        done
          320  +      fi
          321  +    fi
          322  +
          323  +    # On ubuntu 14.10, $auto_path on tclsh is not quite correct.
          324  +    # So try again after applying corrections.
          325  +    if test x"${ac_cv_c_tclconfig}" = x ; then
          326  +      if test x"$cross_compiling" = xno; then
          327  +        for i in `echo 'puts stdout $auto_path' | ${TCLSH_CMD} | sed 's,/tcltk/tcl,/tcl,g'`
          328  +        do
   315    329             if test -f "$i/tclConfig.sh" ; then
   316    330               ac_cv_c_tclconfig="$i"
   317    331               break
   318    332             fi
   319    333           done
   320    334         fi
   321    335       fi

Changes to ext/rtree/rtreeC.test.

   264    264       sqlite3 db2 test.db
   265    265       db2 eval { DROP TABLE sqlite_stat1 }
   266    266       db2 close
   267    267       execsql { SELECT * FROM rt }
   268    268     } {1 2.0 3.0}
   269    269     db close
   270    270   }
          271  +
          272  +#--------------------------------------------------------------------
          273  +# Test that queries featuring LEFT or CROSS JOINS are handled correctly.
          274  +# Handled correctly in this case means:
          275  +#
          276  +#   * Terms with prereqs that appear to the left of a LEFT JOIN against
          277  +#     the virtual table are always available to xBestIndex.
          278  +#
          279  +#   * Terms with prereqs that appear to the right of a LEFT JOIN against
          280  +#     the virtual table are never available to xBestIndex.
          281  +#
          282  +# And the same behaviour for CROSS joins.
          283  +#
          284  +reset_db
          285  +do_execsql_test 7.0 {
          286  +  CREATE TABLE xdir(x1);
          287  +  CREATE TABLE ydir(y1);
          288  +  CREATE VIRTUAL TABLE rt USING rtree_i32(id, xmin, xmax, ymin, ymax);
          289  +
          290  +  INSERT INTO xdir VALUES(5);
          291  +  INSERT INTO ydir VALUES(10);
          292  +
          293  +  INSERT INTO rt VALUES(1, 2, 7, 12, 14);      -- Not a hit
          294  +  INSERT INTO rt VALUES(2, 2, 7, 8, 12);       -- A hit!
          295  +  INSERT INTO rt VALUES(3, 7, 11, 8, 12);      -- Not a hit!
          296  +  INSERT INTO rt VALUES(4, 5, 5, 10, 10);      -- A hit!
          297  +
          298  +}
          299  +
          300  +proc do_eqp_execsql_test {tn sql res} {
          301  +  set query "EXPLAIN QUERY PLAN $sql ; $sql "
          302  +  uplevel [list do_execsql_test $tn $query $res]
          303  +}
          304  +
          305  +do_eqp_execsql_test 7.1 {
          306  +  SELECT id FROM xdir, rt, ydir 
          307  +  ON (y1 BETWEEN ymin AND ymax)
          308  +  WHERE (x1 BETWEEN xmin AND xmax);
          309  +} {
          310  +  0 0 0 {SCAN TABLE xdir} 
          311  +  0 1 2 {SCAN TABLE ydir} 
          312  +  0 2 1 {SCAN TABLE rt VIRTUAL TABLE INDEX 2:B2D3B0D1}
          313  +  2 4
          314  +}
          315  +
          316  +do_eqp_execsql_test 7.2 {
          317  +  SELECT * FROM xdir, rt LEFT JOIN ydir 
          318  +  ON (y1 BETWEEN ymin AND ymax)
          319  +  WHERE (x1 BETWEEN xmin AND xmax);
          320  +} {
          321  +  0 0 0 {SCAN TABLE xdir} 
          322  +  0 1 1 {SCAN TABLE rt VIRTUAL TABLE INDEX 2:B0D1}
          323  +  0 2 2 {SCAN TABLE ydir} 
          324  +
          325  +  5 1 2 7 12 14 {}
          326  +  5 2 2 7  8 12 10
          327  +  5 4 5 5 10 10 10
          328  +}
          329  +
          330  +do_eqp_execsql_test 7.3 {
          331  +  SELECT id FROM xdir, rt CROSS JOIN ydir 
          332  +  ON (y1 BETWEEN ymin AND ymax)
          333  +  WHERE (x1 BETWEEN xmin AND xmax);
          334  +} {
          335  +  0 0 0 {SCAN TABLE xdir} 
          336  +  0 1 1 {SCAN TABLE rt VIRTUAL TABLE INDEX 2:B0D1}
          337  +  0 2 2 {SCAN TABLE ydir} 
          338  +  2 4
          339  +}
          340  +
          341  +do_eqp_execsql_test 7.4 {
          342  +  SELECT id FROM rt, xdir CROSS JOIN ydir 
          343  +  ON (y1 BETWEEN ymin AND ymax)
          344  +  WHERE (x1 BETWEEN xmin AND xmax);
          345  +} {
          346  +  0 0 1 {SCAN TABLE xdir} 
          347  +  0 1 0 {SCAN TABLE rt VIRTUAL TABLE INDEX 2:B0D1}
          348  +  0 2 2 {SCAN TABLE ydir} 
          349  +  2 4
          350  +}
          351  +
          352  +finish_test
          353  +
   271    354   
   272    355   
   273    356   finish_test

Changes to main.mk.

    63     63            icu.o insert.o journal.o legacy.o loadext.o \
    64     64            main.o malloc.o mem0.o mem1.o mem2.o mem3.o mem5.o \
    65     65            memjournal.o \
    66     66            mutex.o mutex_noop.o mutex_unix.o mutex_w32.o \
    67     67            notify.o opcodes.o os.o os_unix.o os_win.o \
    68     68            pager.o pcache.o pcache1.o pragma.o prepare.o printf.o \
    69     69            random.o resolve.o rowset.o rtree.o select.o sqlite3ota.o status.o \
    70         -         table.o threads.o tokenize.o trigger.o \
           70  +         table.o threads.o tokenize.o treeview.o trigger.o \
    71     71            update.o userauth.o util.o vacuum.o \
    72     72            vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \
    73         -	 vdbetrace.o wal.o walker.o where.o utf.o vtab.o
           73  +	 vdbetrace.o wal.o walker.o where.o wherecode.o whereexpr.o \
           74  +         utf.o vtab.o
    74     75   
    75     76   LIBOBJ += sqlite3session.o
    76     77   
    77     78   
    78     79   
    79     80   # All of the source code files.
    80     81   #
................................................................................
   150    151     $(TOP)/src/sqlite3ext.h \
   151    152     $(TOP)/src/sqliteInt.h \
   152    153     $(TOP)/src/sqliteLimit.h \
   153    154     $(TOP)/src/table.c \
   154    155     $(TOP)/src/tclsqlite.c \
   155    156     $(TOP)/src/threads.c \
   156    157     $(TOP)/src/tokenize.c \
          158  +  $(TOP)/src/treeview.c \
   157    159     $(TOP)/src/trigger.c \
   158    160     $(TOP)/src/utf.c \
   159    161     $(TOP)/src/update.c \
   160    162     $(TOP)/src/util.c \
   161    163     $(TOP)/src/vacuum.c \
   162    164     $(TOP)/src/vdbe.c \
   163    165     $(TOP)/src/vdbe.h \
................................................................................
   170    172     $(TOP)/src/vdbeInt.h \
   171    173     $(TOP)/src/vtab.c \
   172    174     $(TOP)/src/vxworks.h \
   173    175     $(TOP)/src/wal.c \
   174    176     $(TOP)/src/wal.h \
   175    177     $(TOP)/src/walker.c \
   176    178     $(TOP)/src/where.c \
          179  +  $(TOP)/src/wherecode.c \
          180  +  $(TOP)/src/whereexpr.c \
   177    181     $(TOP)/src/whereInt.h
   178    182   
   179    183   # Source code for extensions
   180    184   #
   181    185   SRC += \
   182    186     $(TOP)/ext/fts1/fts1.c \
   183    187     $(TOP)/ext/fts1/fts1.h \
................................................................................
   339    343     $(TOP)/src/utf.c \
   340    344     $(TOP)/src/util.c \
   341    345     $(TOP)/src/vdbeapi.c \
   342    346     $(TOP)/src/vdbeaux.c \
   343    347     $(TOP)/src/vdbe.c \
   344    348     $(TOP)/src/vdbemem.c \
   345    349     $(TOP)/src/where.c \
          350  +  $(TOP)/src/wherecode.c \
          351  +  $(TOP)/src/whereexpr.c \
   346    352     parse.c \
   347    353     $(TOP)/ext/fts3/fts3.c \
   348    354     $(TOP)/ext/fts3/fts3_aux.c \
   349    355     $(TOP)/ext/fts3/fts3_expr.c \
   350    356     $(TOP)/ext/fts3/fts3_tokenizer.c \
   351    357     $(TOP)/ext/fts3/fts3_write.c \
   352    358     $(TOP)/ext/async/sqlite3async.c \
................................................................................
   414    420   # Databases containing fuzzer test cases
   415    421   #
   416    422   FUZZDATA = \
   417    423     $(TOP)/test/fuzzdata1.db \
   418    424     $(TOP)/test/fuzzdata2.db \
   419    425     $(TOP)/test/fuzzdata3.db
   420    426   
          427  +# Standard options to testfixture
          428  +#
          429  +TESTOPTS = --verbose=file --output=test-out.txt
          430  +
   421    431   # This is the default Makefile target.  The objects listed here
   422    432   # are what get build when you type just "make" with no arguments.
   423    433   #
   424    434   all:	sqlite3.h libsqlite3.a sqlite3$(EXE)
   425    435   
   426    436   libsqlite3.a:	$(LIBOBJ)
   427    437   	$(AR) libsqlite3.a $(LIBOBJ)
................................................................................
   671    681   fts3-testfixture$(EXE): sqlite3.c fts3amal.c $(TESTSRC) $(TOP)/src/tclsqlite.c
   672    682   	$(TCCX) $(TCL_FLAGS) -DTCLSH=1 $(TESTFIXTURE_FLAGS)                  \
   673    683   	-DSQLITE_ENABLE_FTS3=1                                               \
   674    684   		$(TESTSRC) $(TOP)/src/tclsqlite.c sqlite3.c fts3amal.c       \
   675    685   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   676    686   
   677    687   fulltest:	$(TESTPROGS) fuzztest
   678         -	./testfixture$(EXE) $(TOP)/test/all.test
          688  +	./testfixture$(EXE) $(TOP)/test/all.test $(TESTOPTS)
   679    689   
   680    690   soaktest:	$(TESTPROGS)
   681         -	./testfixture$(EXE) $(TOP)/test/all.test -soak=1
          691  +	./testfixture$(EXE) $(TOP)/test/all.test -soak=1 $(TESTOPTS)
   682    692   
   683    693   fulltestonly:	$(TESTPROGS) fuzztest
   684         -	./testfixture$(EXE) $(TOP)/test/full.test
          694  +	./testfixture$(EXE) $(TOP)/test/full.test $(TESTOPTS)
   685    695   
   686    696   queryplantest:	testfixture$(EXE) sqlite3$(EXE)
   687         -	./testfixture$(EXE) $(TOP)/test/permutations.test queryplanner
          697  +	./testfixture$(EXE) $(TOP)/test/permutations.test queryplanner $(TESTOPTS)
   688    698   
   689    699   fuzztest:	fuzzcheck$(EXE) $(FUZZDATA)
   690    700   	./fuzzcheck$(EXE) $(FUZZDATA)
   691    701   
          702  +valgrindfuzz:	fuzzcheck$(EXE) $(FUZZDATA)
          703  +	valgrind ./fuzzcheck$(EXE) --cell-size-check --quiet $(FUZZDATA)
          704  +
          705  +# A very quick test using only testfixture and omitting all the slower
          706  +# tests.  Designed to run in under 3 minutes on a workstation.
          707  +#
          708  +quicktest:	./testfixture$(EXE)
          709  +	./testfixture$(EXE) $(TOP)/test/extraquick.test $(TESTOPTS)
          710  +
          711  +# The default test case.  Runs most of the faster standard TCL tests,
          712  +# and fuzz tests, and sqlite3_analyzer and sqldiff tests.
          713  +#
   692    714   test:	$(TESTPROGS) fuzztest
   693         -	./testfixture$(EXE) $(TOP)/test/veryquick.test
          715  +	./testfixture$(EXE) $(TOP)/test/veryquick.test $(TESTOPTS)
   694    716   
   695    717   # Run a test using valgrind.  This can take a really long time
   696    718   # because valgrind is so much slower than a native machine.
   697    719   #
   698         -valgrindtest:	$(TESTPROGS) fuzzcheck$(EXE) $(FUZZDATA)
   699         -	valgrind -v ./fuzzcheck$(EXE) --cell-size-check --quiet $(FUZZDATA)
   700         -	OMIT_MISUSE=1 valgrind -v ./testfixture$(EXE) $(TOP)/test/permutations.test valgrind
          720  +valgrindtest:	$(TESTPROGS) valgrindfuzz
          721  +	OMIT_MISUSE=1 valgrind -v \
          722  +	./testfixture$(EXE) $(TOP)/test/permutations.test valgrind $(TESTOPTS)
   701    723   
   702    724   # A very fast test that checks basic sanity.  The name comes from
   703    725   # the 60s-era electronics testing:  "Turn it on and see if smoke
   704    726   # comes out."
   705    727   #
   706    728   smoketest:	$(TESTPROGS) fuzzcheck$(EXE)
   707         -	./testfixture$(EXE) $(TOP)/test/main.test
          729  +	./testfixture$(EXE) $(TOP)/test/main.test $(TESTOPTS)
   708    730   
   709    731   # The next two rules are used to support the "threadtest" target. Building
   710    732   # threadtest runs a few thread-safety tests that are implemented in C. This
   711    733   # target is invoked by the releasetest.tcl script.
   712    734   # 
   713    735   THREADTEST3_SRC = $(TOP)/test/threadtest3.c    \
   714    736                     $(TOP)/test/tt3_checkpoint.c \

Changes to src/alter.c.

   688    688   
   689    689     /* Ensure the default expression is something that sqlite3ValueFromExpr()
   690    690     ** can handle (i.e. not CURRENT_TIME etc.)
   691    691     */
   692    692     if( pDflt ){
   693    693       sqlite3_value *pVal = 0;
   694    694       int rc;
   695         -    rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal);
          695  +    rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
   696    696       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
   697    697       if( rc!=SQLITE_OK ){
   698    698         db->mallocFailed = 1;
   699    699         return;
   700    700       }
   701    701       if( !pVal ){
   702    702         sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");

Changes to src/btree.c.

  1268   1268       ** freeblock form a big-endian integer which is the size of the freeblock
  1269   1269       ** in bytes, including the 4-byte header. */
  1270   1270       size = get2byte(&aData[pc+2]);
  1271   1271       if( size>=nByte ){
  1272   1272         int x = size - nByte;
  1273   1273         testcase( x==4 );
  1274   1274         testcase( x==3 );
  1275         -      if( x<4 ){
         1275  +      if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
         1276  +        *pRc = SQLITE_CORRUPT_BKPT;
         1277  +        return 0;
         1278  +      }else if( x<4 ){
  1276   1279           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
  1277   1280           ** number of bytes in fragments may not exceed 60. */
  1278   1281           if( aData[hdr+7]>=60 ){
  1279   1282             if( pbDefrag ) *pbDefrag = 1;
  1280   1283             return 0;
  1281   1284           }
  1282   1285           /* Remove the slot from the free-list. Update the number of
  1283   1286           ** fragmented bytes within the page. */
  1284   1287           memcpy(&aData[iAddr], &aData[pc], 2);
  1285   1288           aData[hdr+7] += (u8)x;
  1286         -      }else if( pc < pPg->cellOffset+2*pPg->nCell || size+pc > usableSize ){
  1287         -        *pRc = SQLITE_CORRUPT_BKPT;
  1288         -        return 0;
  1289   1289         }else{
  1290   1290           /* The slot remains on the free-list. Reduce its size to account
  1291   1291            ** for the portion used by the new allocation. */
  1292   1292           put2byte(&aData[pc+2], x);
  1293   1293         }
  1294   1294         return &aData[pc + x];
  1295   1295       }
................................................................................
  6137   6137       assert( sqlite3PagerIswriteable(pPage->pDbPage) );
  6138   6138       data = pPage->aData;
  6139   6139       cellOffset = pPage->cellOffset;
  6140   6140       end = cellOffset + 2*pPage->nCell;
  6141   6141       ins = cellOffset + 2*i;
  6142   6142       rc = allocateSpace(pPage, sz, &idx);
  6143   6143       if( rc ){ *pRC = rc; return; }
  6144         -    /* The allocateSpace() routine guarantees the following two properties
  6145         -    ** if it returns success */
  6146         -    assert( idx >= end+2 );
         6144  +    /* The allocateSpace() routine guarantees the following properties
         6145  +    ** if it returns successfully */
         6146  +    assert( idx >= 0 && (idx >= end+2 || CORRUPT_DB) );
  6147   6147       assert( idx+sz <= (int)pPage->pBt->usableSize );
  6148   6148       pPage->nCell++;
  6149   6149       pPage->nFree -= (u16)(2 + sz);
  6150   6150       memcpy(&data[idx], pCell, sz);
  6151   6151       if( iChild ){
  6152   6152         put4byte(&data[idx], iChild);
  6153   6153       }

Changes to src/build.c.

  1088   1088       p->aCol = aNew;
  1089   1089     }
  1090   1090     pCol = &p->aCol[p->nCol];
  1091   1091     memset(pCol, 0, sizeof(p->aCol[0]));
  1092   1092     pCol->zName = z;
  1093   1093    
  1094   1094     /* If there is no type specified, columns have the default affinity
  1095         -  ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
         1095  +  ** 'BLOB'. If there is a type specified, then sqlite3AddColumnType() will
  1096   1096     ** be called next to set pCol->affinity correctly.
  1097   1097     */
  1098         -  pCol->affinity = SQLITE_AFF_NONE;
         1098  +  pCol->affinity = SQLITE_AFF_BLOB;
  1099   1099     pCol->szEst = 1;
  1100   1100     p->nCol++;
  1101   1101   }
  1102   1102   
  1103   1103   /*
  1104   1104   ** This routine is called by the parser while in the middle of
  1105   1105   ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
................................................................................
  1126   1126   **
  1127   1127   ** Substring     | Affinity
  1128   1128   ** --------------------------------
  1129   1129   ** 'INT'         | SQLITE_AFF_INTEGER
  1130   1130   ** 'CHAR'        | SQLITE_AFF_TEXT
  1131   1131   ** 'CLOB'        | SQLITE_AFF_TEXT
  1132   1132   ** 'TEXT'        | SQLITE_AFF_TEXT
  1133         -** 'BLOB'        | SQLITE_AFF_NONE
         1133  +** 'BLOB'        | SQLITE_AFF_BLOB
  1134   1134   ** 'REAL'        | SQLITE_AFF_REAL
  1135   1135   ** 'FLOA'        | SQLITE_AFF_REAL
  1136   1136   ** 'DOUB'        | SQLITE_AFF_REAL
  1137   1137   **
  1138   1138   ** If none of the substrings in the above table are found,
  1139   1139   ** SQLITE_AFF_NUMERIC is returned.
  1140   1140   */
................................................................................
  1152   1152         zChar = zIn;
  1153   1153       }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
  1154   1154         aff = SQLITE_AFF_TEXT;
  1155   1155       }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
  1156   1156         aff = SQLITE_AFF_TEXT;
  1157   1157       }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
  1158   1158           && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
  1159         -      aff = SQLITE_AFF_NONE;
         1159  +      aff = SQLITE_AFF_BLOB;
  1160   1160         if( zIn[0]=='(' ) zChar = zIn;
  1161   1161   #ifndef SQLITE_OMIT_FLOATING_POINT
  1162   1162       }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
  1163   1163           && aff==SQLITE_AFF_NUMERIC ){
  1164   1164         aff = SQLITE_AFF_REAL;
  1165   1165       }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
  1166   1166           && aff==SQLITE_AFF_NUMERIC ){
................................................................................
  1544   1544     }
  1545   1545     sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
  1546   1546     k = sqlite3Strlen30(zStmt);
  1547   1547     identPut(zStmt, &k, p->zName);
  1548   1548     zStmt[k++] = '(';
  1549   1549     for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
  1550   1550       static const char * const azType[] = {
  1551         -        /* SQLITE_AFF_NONE    */ "",
         1551  +        /* SQLITE_AFF_BLOB    */ "",
  1552   1552           /* SQLITE_AFF_TEXT    */ " TEXT",
  1553   1553           /* SQLITE_AFF_NUMERIC */ " NUM",
  1554   1554           /* SQLITE_AFF_INTEGER */ " INT",
  1555   1555           /* SQLITE_AFF_REAL    */ " REAL"
  1556   1556       };
  1557   1557       int len;
  1558   1558       const char *zType;
  1559   1559   
  1560   1560       sqlite3_snprintf(n-k, &zStmt[k], zSep);
  1561   1561       k += sqlite3Strlen30(&zStmt[k]);
  1562   1562       zSep = zSep2;
  1563   1563       identPut(zStmt, &k, pCol->zName);
  1564         -    assert( pCol->affinity-SQLITE_AFF_NONE >= 0 );
  1565         -    assert( pCol->affinity-SQLITE_AFF_NONE < ArraySize(azType) );
  1566         -    testcase( pCol->affinity==SQLITE_AFF_NONE );
         1564  +    assert( pCol->affinity-SQLITE_AFF_BLOB >= 0 );
         1565  +    assert( pCol->affinity-SQLITE_AFF_BLOB < ArraySize(azType) );
         1566  +    testcase( pCol->affinity==SQLITE_AFF_BLOB );
  1567   1567       testcase( pCol->affinity==SQLITE_AFF_TEXT );
  1568   1568       testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
  1569   1569       testcase( pCol->affinity==SQLITE_AFF_INTEGER );
  1570   1570       testcase( pCol->affinity==SQLITE_AFF_REAL );
  1571   1571       
  1572         -    zType = azType[pCol->affinity - SQLITE_AFF_NONE];
         1572  +    zType = azType[pCol->affinity - SQLITE_AFF_BLOB];
  1573   1573       len = sqlite3Strlen30(zType);
  1574         -    assert( pCol->affinity==SQLITE_AFF_NONE 
         1574  +    assert( pCol->affinity==SQLITE_AFF_BLOB 
  1575   1575               || pCol->affinity==sqlite3AffinityType(zType, 0) );
  1576   1576       memcpy(&zStmt[k], zType, len);
  1577   1577       k += len;
  1578   1578       assert( k<=n );
  1579   1579     }
  1580   1580     sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
  1581   1581     return zStmt;
................................................................................
  3697   3697     int i;
  3698   3698     struct SrcList_item *pItem;
  3699   3699     if( pList==0 ) return;
  3700   3700     for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
  3701   3701       sqlite3DbFree(db, pItem->zDatabase);
  3702   3702       sqlite3DbFree(db, pItem->zName);
  3703   3703       sqlite3DbFree(db, pItem->zAlias);
  3704         -    sqlite3DbFree(db, pItem->zIndex);
         3704  +    sqlite3DbFree(db, pItem->zIndexedBy);
  3705   3705       sqlite3DeleteTable(db, pItem->pTab);
  3706   3706       sqlite3SelectDelete(db, pItem->pSelect);
  3707   3707       sqlite3ExprDelete(db, pItem->pOn);
  3708   3708       sqlite3IdListDelete(db, pItem->pUsing);
  3709   3709     }
  3710   3710     sqlite3DbFree(db, pList);
  3711   3711   }
................................................................................
  3770   3770   ** Add an INDEXED BY or NOT INDEXED clause to the most recently added 
  3771   3771   ** element of the source-list passed as the second argument.
  3772   3772   */
  3773   3773   void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
  3774   3774     assert( pIndexedBy!=0 );
  3775   3775     if( p && ALWAYS(p->nSrc>0) ){
  3776   3776       struct SrcList_item *pItem = &p->a[p->nSrc-1];
  3777         -    assert( pItem->notIndexed==0 && pItem->zIndex==0 );
         3777  +    assert( pItem->notIndexed==0 && pItem->zIndexedBy==0 );
  3778   3778       if( pIndexedBy->n==1 && !pIndexedBy->z ){
  3779   3779         /* A "NOT INDEXED" clause was supplied. See parse.y 
  3780   3780         ** construct "indexed_opt" for details. */
  3781   3781         pItem->notIndexed = 1;
  3782   3782       }else{
  3783         -      pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
         3783  +      pItem->zIndexedBy = sqlite3NameFromToken(pParse->db, pIndexedBy);
  3784   3784       }
  3785   3785     }
  3786   3786   }
  3787   3787   
  3788   3788   /*
  3789   3789   ** When building up a FROM clause in the parser, the join operator
  3790   3790   ** is initially attached to the left operand.  But the code generator

Changes to src/delete.c.

   806    806     int nCol;
   807    807   
   808    808     if( piPartIdxLabel ){
   809    809       if( pIdx->pPartIdxWhere ){
   810    810         *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
   811    811         pParse->iPartIdxTab = iDataCur;
   812    812         sqlite3ExprCachePush(pParse);
   813         -      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
   814         -                         SQLITE_JUMPIFNULL);
          813  +      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel, 
          814  +                            SQLITE_JUMPIFNULL);
   815    815       }else{
   816    816         *piPartIdxLabel = 0;
   817    817       }
   818    818     }
   819    819     nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
   820    820     regBase = sqlite3GetTempRange(pParse, nCol);
   821    821     if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;

Changes to src/expr.c.

   187    187     if( aff1 && aff2 ){
   188    188       /* Both sides of the comparison are columns. If one has numeric
   189    189       ** affinity, use that. Otherwise use no affinity.
   190    190       */
   191    191       if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
   192    192         return SQLITE_AFF_NUMERIC;
   193    193       }else{
   194         -      return SQLITE_AFF_NONE;
          194  +      return SQLITE_AFF_BLOB;
   195    195       }
   196    196     }else if( !aff1 && !aff2 ){
   197    197       /* Neither side of the comparison is a column.  Compare the
   198    198       ** results directly.
   199    199       */
   200         -    return SQLITE_AFF_NONE;
          200  +    return SQLITE_AFF_BLOB;
   201    201     }else{
   202    202       /* One side is a column, the other is not. Use the columns affinity. */
   203    203       assert( aff1==0 || aff2==0 );
   204    204       return (aff1 + aff2);
   205    205     }
   206    206   }
   207    207   
................................................................................
   217    217     assert( pExpr->pLeft );
   218    218     aff = sqlite3ExprAffinity(pExpr->pLeft);
   219    219     if( pExpr->pRight ){
   220    220       aff = sqlite3CompareAffinity(pExpr->pRight, aff);
   221    221     }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
   222    222       aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
   223    223     }else if( !aff ){
   224         -    aff = SQLITE_AFF_NONE;
          224  +    aff = SQLITE_AFF_BLOB;
   225    225     }
   226    226     return aff;
   227    227   }
   228    228   
   229    229   /*
   230    230   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
   231    231   ** idx_affinity is the affinity of an indexed column. Return true
   232    232   ** if the index with affinity idx_affinity may be used to implement
   233    233   ** the comparison in pExpr.
   234    234   */
   235    235   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
   236    236     char aff = comparisonAffinity(pExpr);
   237    237     switch( aff ){
   238         -    case SQLITE_AFF_NONE:
          238  +    case SQLITE_AFF_BLOB:
   239    239         return 1;
   240    240       case SQLITE_AFF_TEXT:
   241    241         return idx_affinity==SQLITE_AFF_TEXT;
   242    242       default:
   243    243         return sqlite3IsNumericAffinity(idx_affinity);
   244    244     }
   245    245   }
................................................................................
  1037   1037       pNewItem->jointype = pOldItem->jointype;
  1038   1038       pNewItem->iCursor = pOldItem->iCursor;
  1039   1039       pNewItem->addrFillSub = pOldItem->addrFillSub;
  1040   1040       pNewItem->regReturn = pOldItem->regReturn;
  1041   1041       pNewItem->isCorrelated = pOldItem->isCorrelated;
  1042   1042       pNewItem->viaCoroutine = pOldItem->viaCoroutine;
  1043   1043       pNewItem->isRecursive = pOldItem->isRecursive;
  1044         -    pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
         1044  +    pNewItem->zIndexedBy = sqlite3DbStrDup(db, pOldItem->zIndexedBy);
  1045   1045       pNewItem->notIndexed = pOldItem->notIndexed;
  1046   1046       pNewItem->pIndex = pOldItem->pIndex;
  1047   1047       pTab = pNewItem->pTab = pOldItem->pTab;
  1048   1048       if( pTab ){
  1049   1049         pTab->nRef++;
  1050   1050       }
  1051   1051       pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
................................................................................
  1264   1264   ** Walker.eCode value determines the type of "constant" we are looking
  1265   1265   ** for.
  1266   1266   **
  1267   1267   ** These callback routines are used to implement the following:
  1268   1268   **
  1269   1269   **     sqlite3ExprIsConstant()                  pWalker->eCode==1
  1270   1270   **     sqlite3ExprIsConstantNotJoin()           pWalker->eCode==2
  1271         -**     sqlite3ExprRefOneTableOnly()             pWalker->eCode==3
         1271  +**     sqlite3ExprIsTableConstant()             pWalker->eCode==3
  1272   1272   **     sqlite3ExprIsConstantOrFunction()        pWalker->eCode==4 or 5
  1273   1273   **
  1274   1274   ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression
  1275   1275   ** is found to not be a constant.
  1276   1276   **
  1277   1277   ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions
  1278   1278   ** in a CREATE TABLE statement.  The Walker.eCode value is 5 when parsing
................................................................................
  1372   1372   ** an ON or USING clause.
  1373   1373   */
  1374   1374   int sqlite3ExprIsConstantNotJoin(Expr *p){
  1375   1375     return exprIsConst(p, 2, 0);
  1376   1376   }
  1377   1377   
  1378   1378   /*
  1379         -** Walk an expression tree.  Return non-zero if the expression constant
         1379  +** Walk an expression tree.  Return non-zero if the expression is constant
  1380   1380   ** for any single row of the table with cursor iCur.  In other words, the
  1381   1381   ** expression must not refer to any non-deterministic function nor any
  1382   1382   ** table other than iCur.
  1383   1383   */
  1384   1384   int sqlite3ExprIsTableConstant(Expr *p, int iCur){
  1385   1385     return exprIsConst(p, 3, iCur);
  1386   1386   }
................................................................................
  1478   1478   ** This routine is used to determine if the OP_Affinity operation
  1479   1479   ** can be omitted.  When in doubt return FALSE.  A false negative
  1480   1480   ** is harmless.  A false positive, however, can result in the wrong
  1481   1481   ** answer.
  1482   1482   */
  1483   1483   int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
  1484   1484     u8 op;
  1485         -  if( aff==SQLITE_AFF_NONE ) return 1;
         1485  +  if( aff==SQLITE_AFF_BLOB ) return 1;
  1486   1486     while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
  1487   1487     op = p->op;
  1488   1488     if( op==TK_REGISTER ) op = p->op2;
  1489   1489     switch( op ){
  1490   1490       case TK_INTEGER: {
  1491   1491         return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
  1492   1492       }
................................................................................
  1929   1929           */
  1930   1930           int i;
  1931   1931           ExprList *pList = pExpr->x.pList;
  1932   1932           struct ExprList_item *pItem;
  1933   1933           int r1, r2, r3;
  1934   1934   
  1935   1935           if( !affinity ){
  1936         -          affinity = SQLITE_AFF_NONE;
         1936  +          affinity = SQLITE_AFF_BLOB;
  1937   1937           }
  1938   1938           if( pKeyInfo ){
  1939   1939             assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  1940   1940             pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  1941   1941           }
  1942   1942   
  1943   1943           /* Loop through each expression in <exprlist>. */
................................................................................
  3307   3307     assert( pExpr->op!=TK_REGISTER );
  3308   3308     sqlite3ExprCode(pParse, pExpr, target);
  3309   3309     iMem = ++pParse->nMem;
  3310   3310     sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
  3311   3311     exprToRegister(pExpr, iMem);
  3312   3312   }
  3313   3313   
  3314         -#ifdef SQLITE_DEBUG
  3315         -/*
  3316         -** Generate a human-readable explanation of an expression tree.
  3317         -*/
  3318         -void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
  3319         -  const char *zBinOp = 0;   /* Binary operator */
  3320         -  const char *zUniOp = 0;   /* Unary operator */
  3321         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
  3322         -  if( pExpr==0 ){
  3323         -    sqlite3TreeViewLine(pView, "nil");
  3324         -    sqlite3TreeViewPop(pView);
  3325         -    return;
  3326         -  }
  3327         -  switch( pExpr->op ){
  3328         -    case TK_AGG_COLUMN: {
  3329         -      sqlite3TreeViewLine(pView, "AGG{%d:%d}",
  3330         -            pExpr->iTable, pExpr->iColumn);
  3331         -      break;
  3332         -    }
  3333         -    case TK_COLUMN: {
  3334         -      if( pExpr->iTable<0 ){
  3335         -        /* This only happens when coding check constraints */
  3336         -        sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
  3337         -      }else{
  3338         -        sqlite3TreeViewLine(pView, "{%d:%d}",
  3339         -                             pExpr->iTable, pExpr->iColumn);
  3340         -      }
  3341         -      break;
  3342         -    }
  3343         -    case TK_INTEGER: {
  3344         -      if( pExpr->flags & EP_IntValue ){
  3345         -        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
  3346         -      }else{
  3347         -        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
  3348         -      }
  3349         -      break;
  3350         -    }
  3351         -#ifndef SQLITE_OMIT_FLOATING_POINT
  3352         -    case TK_FLOAT: {
  3353         -      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
  3354         -      break;
  3355         -    }
  3356         -#endif
  3357         -    case TK_STRING: {
  3358         -      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
  3359         -      break;
  3360         -    }
  3361         -    case TK_NULL: {
  3362         -      sqlite3TreeViewLine(pView,"NULL");
  3363         -      break;
  3364         -    }
  3365         -#ifndef SQLITE_OMIT_BLOB_LITERAL
  3366         -    case TK_BLOB: {
  3367         -      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
  3368         -      break;
  3369         -    }
  3370         -#endif
  3371         -    case TK_VARIABLE: {
  3372         -      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
  3373         -                          pExpr->u.zToken, pExpr->iColumn);
  3374         -      break;
  3375         -    }
  3376         -    case TK_REGISTER: {
  3377         -      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
  3378         -      break;
  3379         -    }
  3380         -    case TK_AS: {
  3381         -      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
  3382         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3383         -      break;
  3384         -    }
  3385         -    case TK_ID: {
  3386         -      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
  3387         -      break;
  3388         -    }
  3389         -#ifndef SQLITE_OMIT_CAST
  3390         -    case TK_CAST: {
  3391         -      /* Expressions of the form:   CAST(pLeft AS token) */
  3392         -      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
  3393         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3394         -      break;
  3395         -    }
  3396         -#endif /* SQLITE_OMIT_CAST */
  3397         -    case TK_LT:      zBinOp = "LT";     break;
  3398         -    case TK_LE:      zBinOp = "LE";     break;
  3399         -    case TK_GT:      zBinOp = "GT";     break;
  3400         -    case TK_GE:      zBinOp = "GE";     break;
  3401         -    case TK_NE:      zBinOp = "NE";     break;
  3402         -    case TK_EQ:      zBinOp = "EQ";     break;
  3403         -    case TK_IS:      zBinOp = "IS";     break;
  3404         -    case TK_ISNOT:   zBinOp = "ISNOT";  break;
  3405         -    case TK_AND:     zBinOp = "AND";    break;
  3406         -    case TK_OR:      zBinOp = "OR";     break;
  3407         -    case TK_PLUS:    zBinOp = "ADD";    break;
  3408         -    case TK_STAR:    zBinOp = "MUL";    break;
  3409         -    case TK_MINUS:   zBinOp = "SUB";    break;
  3410         -    case TK_REM:     zBinOp = "REM";    break;
  3411         -    case TK_BITAND:  zBinOp = "BITAND"; break;
  3412         -    case TK_BITOR:   zBinOp = "BITOR";  break;
  3413         -    case TK_SLASH:   zBinOp = "DIV";    break;
  3414         -    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
  3415         -    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
  3416         -    case TK_CONCAT:  zBinOp = "CONCAT"; break;
  3417         -    case TK_DOT:     zBinOp = "DOT";    break;
  3418         -
  3419         -    case TK_UMINUS:  zUniOp = "UMINUS"; break;
  3420         -    case TK_UPLUS:   zUniOp = "UPLUS";  break;
  3421         -    case TK_BITNOT:  zUniOp = "BITNOT"; break;
  3422         -    case TK_NOT:     zUniOp = "NOT";    break;
  3423         -    case TK_ISNULL:  zUniOp = "ISNULL"; break;
  3424         -    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
  3425         -
  3426         -    case TK_COLLATE: {
  3427         -      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
  3428         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3429         -      break;
  3430         -    }
  3431         -
  3432         -    case TK_AGG_FUNCTION:
  3433         -    case TK_FUNCTION: {
  3434         -      ExprList *pFarg;       /* List of function arguments */
  3435         -      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
  3436         -        pFarg = 0;
  3437         -      }else{
  3438         -        pFarg = pExpr->x.pList;
  3439         -      }
  3440         -      if( pExpr->op==TK_AGG_FUNCTION ){
  3441         -        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
  3442         -                             pExpr->op2, pExpr->u.zToken);
  3443         -      }else{
  3444         -        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
  3445         -      }
  3446         -      if( pFarg ){
  3447         -        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
  3448         -      }
  3449         -      break;
  3450         -    }
  3451         -#ifndef SQLITE_OMIT_SUBQUERY
  3452         -    case TK_EXISTS: {
  3453         -      sqlite3TreeViewLine(pView, "EXISTS-expr");
  3454         -      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3455         -      break;
  3456         -    }
  3457         -    case TK_SELECT: {
  3458         -      sqlite3TreeViewLine(pView, "SELECT-expr");
  3459         -      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3460         -      break;
  3461         -    }
  3462         -    case TK_IN: {
  3463         -      sqlite3TreeViewLine(pView, "IN");
  3464         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
  3465         -      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  3466         -        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
  3467         -      }else{
  3468         -        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
  3469         -      }
  3470         -      break;
  3471         -    }
  3472         -#endif /* SQLITE_OMIT_SUBQUERY */
  3473         -
  3474         -    /*
  3475         -    **    x BETWEEN y AND z
  3476         -    **
  3477         -    ** This is equivalent to
  3478         -    **
  3479         -    **    x>=y AND x<=z
  3480         -    **
  3481         -    ** X is stored in pExpr->pLeft.
  3482         -    ** Y is stored in pExpr->pList->a[0].pExpr.
  3483         -    ** Z is stored in pExpr->pList->a[1].pExpr.
  3484         -    */
  3485         -    case TK_BETWEEN: {
  3486         -      Expr *pX = pExpr->pLeft;
  3487         -      Expr *pY = pExpr->x.pList->a[0].pExpr;
  3488         -      Expr *pZ = pExpr->x.pList->a[1].pExpr;
  3489         -      sqlite3TreeViewLine(pView, "BETWEEN");
  3490         -      sqlite3TreeViewExpr(pView, pX, 1);
  3491         -      sqlite3TreeViewExpr(pView, pY, 1);
  3492         -      sqlite3TreeViewExpr(pView, pZ, 0);
  3493         -      break;
  3494         -    }
  3495         -    case TK_TRIGGER: {
  3496         -      /* If the opcode is TK_TRIGGER, then the expression is a reference
  3497         -      ** to a column in the new.* or old.* pseudo-tables available to
  3498         -      ** trigger programs. In this case Expr.iTable is set to 1 for the
  3499         -      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
  3500         -      ** is set to the column of the pseudo-table to read, or to -1 to
  3501         -      ** read the rowid field.
  3502         -      */
  3503         -      sqlite3TreeViewLine(pView, "%s(%d)", 
  3504         -          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
  3505         -      break;
  3506         -    }
  3507         -    case TK_CASE: {
  3508         -      sqlite3TreeViewLine(pView, "CASE");
  3509         -      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
  3510         -      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
  3511         -      break;
  3512         -    }
  3513         -#ifndef SQLITE_OMIT_TRIGGER
  3514         -    case TK_RAISE: {
  3515         -      const char *zType = "unk";
  3516         -      switch( pExpr->affinity ){
  3517         -        case OE_Rollback:   zType = "rollback";  break;
  3518         -        case OE_Abort:      zType = "abort";     break;
  3519         -        case OE_Fail:       zType = "fail";      break;
  3520         -        case OE_Ignore:     zType = "ignore";    break;
  3521         -      }
  3522         -      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
  3523         -      break;
  3524         -    }
  3525         -#endif
  3526         -    default: {
  3527         -      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
  3528         -      break;
  3529         -    }
  3530         -  }
  3531         -  if( zBinOp ){
  3532         -    sqlite3TreeViewLine(pView, "%s", zBinOp);
  3533         -    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
  3534         -    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
  3535         -  }else if( zUniOp ){
  3536         -    sqlite3TreeViewLine(pView, "%s", zUniOp);
  3537         -    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
  3538         -  }
  3539         -  sqlite3TreeViewPop(pView);
  3540         -}
  3541         -#endif /* SQLITE_DEBUG */
  3542         -
  3543         -#ifdef SQLITE_DEBUG
  3544         -/*
  3545         -** Generate a human-readable explanation of an expression list.
  3546         -*/
  3547         -void sqlite3TreeViewExprList(
  3548         -  TreeView *pView,
  3549         -  const ExprList *pList,
  3550         -  u8 moreToFollow,
  3551         -  const char *zLabel
  3552         -){
  3553         -  int i;
  3554         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
  3555         -  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
  3556         -  if( pList==0 ){
  3557         -    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
  3558         -  }else{
  3559         -    sqlite3TreeViewLine(pView, "%s", zLabel);
  3560         -    for(i=0; i<pList->nExpr; i++){
  3561         -      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
  3562         -#if 0
  3563         -     if( pList->a[i].zName ){
  3564         -        sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
  3565         -      }
  3566         -      if( pList->a[i].bSpanIsTab ){
  3567         -        sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
  3568         -      }
  3569         -#endif
  3570         -    }
  3571         -  }
  3572         -  sqlite3TreeViewPop(pView);
  3573         -}
  3574         -#endif /* SQLITE_DEBUG */
  3575         -
  3576   3314   /*
  3577   3315   ** Generate code that pushes the value of every element of the given
  3578   3316   ** expression list into a sequence of registers beginning at target.
  3579   3317   **
  3580   3318   ** Return the number of elements evaluated.
  3581   3319   **
  3582   3320   ** The SQLITE_ECEL_DUP flag prevents the arguments from being
................................................................................
  3959   3697         }
  3960   3698         break;
  3961   3699       }
  3962   3700     }
  3963   3701     sqlite3ReleaseTempReg(pParse, regFree1);
  3964   3702     sqlite3ReleaseTempReg(pParse, regFree2);
  3965   3703   }
         3704  +
         3705  +/*
         3706  +** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before
         3707  +** code generation, and that copy is deleted after code generation. This
         3708  +** ensures that the original pExpr is unchanged.
         3709  +*/
         3710  +void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){
         3711  +  sqlite3 *db = pParse->db;
         3712  +  Expr *pCopy = sqlite3ExprDup(db, pExpr, 0);
         3713  +  if( db->mallocFailed==0 ){
         3714  +    sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull);
         3715  +  }
         3716  +  sqlite3ExprDelete(db, pCopy);
         3717  +}
         3718  +
  3966   3719   
  3967   3720   /*
  3968   3721   ** Do a deep comparison of two expression trees.  Return 0 if the two
  3969   3722   ** expressions are completely identical.  Return 1 if they differ only
  3970   3723   ** by a COLLATE operator at the top level.  Return 2 if there are differences
  3971   3724   ** other than the top-level COLLATE operator.
  3972   3725   **

Changes to src/insert.c.

    52     52   /*
    53     53   ** Return a pointer to the column affinity string associated with index
    54     54   ** pIdx. A column affinity string has one character for each column in 
    55     55   ** the table, according to the affinity of the column:
    56     56   **
    57     57   **  Character      Column affinity
    58     58   **  ------------------------------
    59         -**  'A'            NONE
           59  +**  'A'            BLOB
    60     60   **  'B'            TEXT
    61     61   **  'C'            NUMERIC
    62     62   **  'D'            INTEGER
    63     63   **  'F'            REAL
    64     64   **
    65     65   ** An extra 'D' is appended to the end of the string to cover the
    66     66   ** rowid that appears as the last column in every index.
................................................................................
    95     95     }
    96     96    
    97     97     return pIdx->zColAff;
    98     98   }
    99     99   
   100    100   /*
   101    101   ** Compute the affinity string for table pTab, if it has not already been
   102         -** computed.  As an optimization, omit trailing SQLITE_AFF_NONE affinities.
          102  +** computed.  As an optimization, omit trailing SQLITE_AFF_BLOB affinities.
   103    103   **
   104         -** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and
          104  +** If the affinity exists (if it is no entirely SQLITE_AFF_BLOB values) and
   105    105   ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
   106    106   ** for register iReg and following.  Or if affinities exists and iReg==0,
   107    107   ** then just set the P4 operand of the previous opcode (which should  be
   108    108   ** an OP_MakeRecord) to the affinity string.
   109    109   **
   110    110   ** A column affinity string has one character per column:
   111    111   **
   112    112   **  Character      Column affinity
   113    113   **  ------------------------------
   114         -**  'A'            NONE
          114  +**  'A'            BLOB
   115    115   **  'B'            TEXT
   116    116   **  'C'            NUMERIC
   117    117   **  'D'            INTEGER
   118    118   **  'E'            REAL
   119    119   */
   120    120   void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
   121    121     int i;
................................................................................
   129    129       }
   130    130   
   131    131       for(i=0; i<pTab->nCol; i++){
   132    132         zColAff[i] = pTab->aCol[i].affinity;
   133    133       }
   134    134       do{
   135    135         zColAff[i--] = 0;
   136         -    }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
          136  +    }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
   137    137       pTab->zColAff = zColAff;
   138    138     }
   139    139     i = sqlite3Strlen30(zColAff);
   140    140     if( i ){
   141    141       if( iReg ){
   142    142         sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
   143    143       }else{
................................................................................
  1389   1389       iThisCur = iIdxCur+ix;
  1390   1390       addrUniqueOk = sqlite3VdbeMakeLabel(v);
  1391   1391   
  1392   1392       /* Skip partial indices for which the WHERE clause is not true */
  1393   1393       if( pIdx->pPartIdxWhere ){
  1394   1394         sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
  1395   1395         pParse->ckBase = regNewData+1;
  1396         -      sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
  1397         -                         SQLITE_JUMPIFNULL);
         1396  +      sqlite3ExprIfFalseDup(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
         1397  +                            SQLITE_JUMPIFNULL);
  1398   1398         pParse->ckBase = 0;
  1399   1399       }
  1400   1400   
  1401   1401       /* Create a record for this index entry as it should appear after
  1402   1402       ** the insert or update.  Store that record in the aRegIdx[ix] register
  1403   1403       */
  1404   1404       regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);

Changes to src/printf.c.

     1      1   /*
     2      2   ** The "printf" code that follows dates from the 1980's.  It is in
     3         -** the public domain.  The original comments are included here for
     4         -** completeness.  They are very out-of-date but might be useful as
     5         -** an historical reference.  Most of the "enhancements" have been backed
     6         -** out so that the functionality is now the same as standard printf().
            3  +** the public domain. 
     7      4   **
     8      5   **************************************************************************
     9      6   **
    10      7   ** This file contains code for a set of "printf"-like routines.  These
    11      8   ** routines format strings much like the printf() from the standard C
    12      9   ** library, though the implementation here has enhancements to support
    13         -** SQLlite.
           10  +** SQLite.
    14     11   */
    15     12   #include "sqliteInt.h"
    16     13   
    17     14   /*
    18     15   ** Conversion types fall into various categories as defined by the
    19     16   ** following enumeration.
    20     17   */
................................................................................
  1054   1051     va_end(ap);
  1055   1052     sqlite3StrAccumFinish(&acc);
  1056   1053     fprintf(stdout,"%s", zBuf);
  1057   1054     fflush(stdout);
  1058   1055   }
  1059   1056   #endif
  1060   1057   
  1061         -#ifdef SQLITE_DEBUG
  1062         -/*************************************************************************
  1063         -** Routines for implementing the "TreeView" display of hierarchical
  1064         -** data structures for debugging.
  1065         -**
  1066         -** The main entry points (coded elsewhere) are:
  1067         -**     sqlite3TreeViewExpr(0, pExpr, 0);
  1068         -**     sqlite3TreeViewExprList(0, pList, 0, 0);
  1069         -**     sqlite3TreeViewSelect(0, pSelect, 0);
  1070         -** Insert calls to those routines while debugging in order to display
  1071         -** a diagram of Expr, ExprList, and Select objects.
  1072         -**
  1073         -*/
  1074         -/* Add a new subitem to the tree.  The moreToFollow flag indicates that this
  1075         -** is not the last item in the tree. */
  1076         -TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
  1077         -  if( p==0 ){
  1078         -    p = sqlite3_malloc64( sizeof(*p) );
  1079         -    if( p==0 ) return 0;
  1080         -    memset(p, 0, sizeof(*p));
  1081         -  }else{
  1082         -    p->iLevel++;
  1083         -  }
  1084         -  assert( moreToFollow==0 || moreToFollow==1 );
  1085         -  if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
  1086         -  return p;
  1087         -}
  1088         -/* Finished with one layer of the tree */
  1089         -void sqlite3TreeViewPop(TreeView *p){
  1090         -  if( p==0 ) return;
  1091         -  p->iLevel--;
  1092         -  if( p->iLevel<0 ) sqlite3_free(p);
  1093         -}
  1094         -/* Generate a single line of output for the tree, with a prefix that contains
  1095         -** all the appropriate tree lines */
  1096         -void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
  1097         -  va_list ap;
  1098         -  int i;
  1099         -  StrAccum acc;
  1100         -  char zBuf[500];
  1101         -  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
  1102         -  if( p ){
  1103         -    for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
  1104         -      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
  1105         -    }
  1106         -    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
  1107         -  }
  1108         -  va_start(ap, zFormat);
  1109         -  sqlite3VXPrintf(&acc, 0, zFormat, ap);
  1110         -  va_end(ap);
  1111         -  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
  1112         -  sqlite3StrAccumFinish(&acc);
  1113         -  fprintf(stdout,"%s", zBuf);
  1114         -  fflush(stdout);
  1115         -}
  1116         -/* Shorthand for starting a new tree item that consists of a single label */
  1117         -void sqlite3TreeViewItem(TreeView *p, const char *zLabel, u8 moreToFollow){
  1118         -  p = sqlite3TreeViewPush(p, moreToFollow);
  1119         -  sqlite3TreeViewLine(p, "%s", zLabel);
  1120         -}
  1121         -#endif /* SQLITE_DEBUG */
  1122   1058   
  1123   1059   /*
  1124   1060   ** variable-argument wrapper around sqlite3VXPrintf().
  1125   1061   */
  1126   1062   void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
  1127   1063     va_list ap;
  1128   1064     va_start(ap,zFormat);
  1129   1065     sqlite3VXPrintf(p, bFlags, zFormat, ap);
  1130   1066     va_end(ap);
  1131   1067   }

Changes to src/select.c.

    17     17   /*
    18     18   ** Trace output macros
    19     19   */
    20     20   #if SELECTTRACE_ENABLED
    21     21   /***/ int sqlite3SelectTrace = 0;
    22     22   # define SELECTTRACE(K,P,S,X)  \
    23     23     if(sqlite3SelectTrace&(K))   \
    24         -    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",(S)->zSelName,(S)),\
           24  +    sqlite3DebugPrintf("%*s%s.%p: ",(P)->nSelectIndent*2-2,"",\
           25  +        (S)->zSelName,(S)),\
    25     26       sqlite3DebugPrintf X
    26     27   #else
    27     28   # define SELECTTRACE(K,P,S,X)
    28     29   #endif
    29     30   
    30     31   
    31     32   /*
................................................................................
   770    771         case WHERE_DISTINCT_UNIQUE: {
   771    772           sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
   772    773           break;
   773    774         }
   774    775   
   775    776         default: {
   776    777           assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
   777         -        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
          778  +        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol,
          779  +                     regResult);
   778    780           break;
   779    781         }
   780    782       }
   781    783       if( pSort==0 ){
   782    784         codeOffset(v, p->iOffset, iContinue);
   783    785       }
   784    786     }
................................................................................
   823    825         if( eDest==SRT_DistFifo ){
   824    826           /* If the destination is DistFifo, then cursor (iParm+1) is open
   825    827           ** on an ephemeral index. If the current row is already present
   826    828           ** in the index, do not write it to the output. If not, add the
   827    829           ** current row to the index and proceed with writing it to the
   828    830           ** output table as well.  */
   829    831           int addr = sqlite3VdbeCurrentAddr(v) + 4;
   830         -        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
          832  +        sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
          833  +        VdbeCoverage(v);
   831    834           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
   832    835           assert( pSort==0 );
   833    836         }
   834    837   #endif
   835    838         if( pSort ){
   836    839           pushOntoSorter(pParse, pSort, p, r1+nPrefixReg, 1, nPrefixReg);
   837    840         }else{
................................................................................
  1306   1309   ** The declaration type for any expression other than a column is NULL.
  1307   1310   **
  1308   1311   ** This routine has either 3 or 6 parameters depending on whether or not
  1309   1312   ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
  1310   1313   */
  1311   1314   #ifdef SQLITE_ENABLE_COLUMN_METADATA
  1312   1315   # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
         1316  +#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
         1317  +# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
         1318  +#endif
  1313   1319   static const char *columnTypeImpl(
  1314   1320     NameContext *pNC, 
  1315   1321     Expr *pExpr,
         1322  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1316   1323     const char **pzOrigDb,
  1317   1324     const char **pzOrigTab,
  1318   1325     const char **pzOrigCol,
         1326  +#endif
  1319   1327     u8 *pEstWidth
  1320   1328   ){
         1329  +  char const *zType = 0;
         1330  +  int j;
         1331  +  u8 estWidth = 1;
         1332  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
  1321   1333     char const *zOrigDb = 0;
  1322   1334     char const *zOrigTab = 0;
  1323   1335     char const *zOrigCol = 0;
  1324         -#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
  1325         -# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
  1326         -static const char *columnTypeImpl(
  1327         -  NameContext *pNC, 
  1328         -  Expr *pExpr,
  1329         -  u8 *pEstWidth
  1330         -){
  1331         -#endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
  1332         -  char const *zType = 0;
  1333         -  int j;
  1334         -  u8 estWidth = 1;
         1336  +#endif
  1335   1337   
  1336   1338     if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
  1337   1339     switch( pExpr->op ){
  1338   1340       case TK_AGG_COLUMN:
  1339   1341       case TK_COLUMN: {
  1340   1342         /* The expression is a column. Locate the table the column is being
  1341   1343         ** extracted from in NameContext.pSrcList. This table may be real
................................................................................
  1701   1703     if( db->mallocFailed ) return;
  1702   1704     memset(&sNC, 0, sizeof(sNC));
  1703   1705     sNC.pSrcList = pSelect->pSrc;
  1704   1706     a = pSelect->pEList->a;
  1705   1707     for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
  1706   1708       p = a[i].pExpr;
  1707   1709       if( pCol->zType==0 ){
  1708         -      pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
         1710  +      pCol->zType = sqlite3DbStrDup(db, 
         1711  +                        columnType(&sNC, p,0,0,0, &pCol->szEst));
  1709   1712       }
  1710   1713       szAll += pCol->szEst;
  1711   1714       pCol->affinity = sqlite3ExprAffinity(p);
  1712         -    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
         1715  +    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
  1713   1716       pColl = sqlite3ExprCollSeq(pParse, p);
  1714   1717       if( pColl && pCol->zColl==0 ){
  1715   1718         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
  1716   1719       }
  1717   1720     }
  1718   1721     pTab->szTabRow = sqlite3LogEst(szAll*4);
  1719   1722   }
................................................................................
  3211   3214   **   (8)  The subquery does not use LIMIT or the outer query is not a join.
  3212   3215   **
  3213   3216   **   (9)  The subquery does not use LIMIT or the outer query does not use
  3214   3217   **        aggregates.
  3215   3218   **
  3216   3219   **  (**)  Restriction (10) was removed from the code on 2005-02-05 but we
  3217   3220   **        accidently carried the comment forward until 2014-09-15.  Original
  3218         -**        text: "The subquery does not use aggregates or the outer query does not
  3219         -**        use LIMIT."
         3221  +**        text: "The subquery does not use aggregates or the outer query 
         3222  +**        does not use LIMIT."
  3220   3223   **
  3221   3224   **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
  3222   3225   **
  3223   3226   **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
  3224   3227   **        a separate restriction deriving from ticket #350.
  3225   3228   **
  3226   3229   **  (13)  The subquery and outer query do not both use LIMIT.
................................................................................
  3705   3708     /* Finially, delete what is left of the subquery and return
  3706   3709     ** success.
  3707   3710     */
  3708   3711     sqlite3SelectDelete(db, pSub1);
  3709   3712   
  3710   3713   #if SELECTTRACE_ENABLED
  3711   3714     if( sqlite3SelectTrace & 0x100 ){
  3712         -    sqlite3DebugPrintf("After flattening:\n");
         3715  +    SELECTTRACE(0x100,pParse,p,("After flattening:\n"));
  3713   3716       sqlite3TreeViewSelect(0, p, 0);
  3714   3717     }
  3715   3718   #endif
  3716   3719   
  3717   3720     return 1;
         3721  +}
         3722  +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
         3723  +
         3724  +
         3725  +
         3726  +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
         3727  +/*
         3728  +** Make copies of relevant WHERE clause terms of the outer query into
         3729  +** the WHERE clause of subquery.  Example:
         3730  +**
         3731  +**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1) WHERE x=5 AND y=10;
         3732  +**
         3733  +** Transformed into:
         3734  +**
         3735  +**    SELECT * FROM (SELECT a AS x, c-d AS y FROM t1 WHERE a=5 AND c-d=10)
         3736  +**     WHERE x=5 AND y=10;
         3737  +**
         3738  +** The hope is that the terms added to the inner query will make it more
         3739  +** efficient.
         3740  +**
         3741  +** Do not attempt this optimization if:
         3742  +**
         3743  +**   (1) The inner query is an aggregate.  (In that case, we'd really want
         3744  +**       to copy the outer WHERE-clause terms onto the HAVING clause of the
         3745  +**       inner query.  But they probably won't help there so do not bother.)
         3746  +**
         3747  +**   (2) The inner query is the recursive part of a common table expression.
         3748  +**
         3749  +**   (3) The inner query has a LIMIT clause (since the changes to the WHERE
         3750  +**       close would change the meaning of the LIMIT).
         3751  +**
         3752  +**   (4) The inner query is the right operand of a LEFT JOIN.  (The caller
         3753  +**       enforces this restriction since this routine does not have enough
         3754  +**       information to know.)
         3755  +**
         3756  +** Return 0 if no changes are made and non-zero if one or more WHERE clause
         3757  +** terms are duplicated into the subquery.
         3758  +*/
         3759  +static int pushDownWhereTerms(
         3760  +  sqlite3 *db,          /* The database connection (for malloc()) */
         3761  +  Select *pSubq,        /* The subquery whose WHERE clause is to be augmented */
         3762  +  Expr *pWhere,         /* The WHERE clause of the outer query */
         3763  +  int iCursor           /* Cursor number of the subquery */
         3764  +){
         3765  +  Expr *pNew;
         3766  +  int nChng = 0;
         3767  +  if( pWhere==0 ) return 0;
         3768  +  if( (pSubq->selFlags & (SF_Aggregate|SF_Recursive))!=0 ){
         3769  +     return 0; /* restrictions (1) and (2) */
         3770  +  }
         3771  +  if( pSubq->pLimit!=0 ){
         3772  +     return 0; /* restriction (3) */
         3773  +  }
         3774  +  while( pWhere->op==TK_AND ){
         3775  +    nChng += pushDownWhereTerms(db, pSubq, pWhere->pRight, iCursor);
         3776  +    pWhere = pWhere->pLeft;
         3777  +  }
         3778  +  if( sqlite3ExprIsTableConstant(pWhere, iCursor) ){
         3779  +    nChng++;
         3780  +    while( pSubq ){
         3781  +      pNew = sqlite3ExprDup(db, pWhere, 0);
         3782  +      pNew = substExpr(db, pNew, iCursor, pSubq->pEList);
         3783  +      pSubq->pWhere = sqlite3ExprAnd(db, pSubq->pWhere, pNew);
         3784  +      pSubq = pSubq->pPrior;
         3785  +    }
         3786  +  }
         3787  +  return nChng;
  3718   3788   }
  3719   3789   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3720   3790   
  3721   3791   /*
  3722   3792   ** Based on the contents of the AggInfo structure indicated by the first
  3723   3793   ** argument, this function checks if the following are true:
  3724   3794   **
................................................................................
  3797   3867   ** If the source-list item passed as an argument was augmented with an
  3798   3868   ** INDEXED BY clause, then try to locate the specified index. If there
  3799   3869   ** was such a clause and the named index cannot be found, return 
  3800   3870   ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate 
  3801   3871   ** pFrom->pIndex and return SQLITE_OK.
  3802   3872   */
  3803   3873   int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
  3804         -  if( pFrom->pTab && pFrom->zIndex ){
         3874  +  if( pFrom->pTab && pFrom->zIndexedBy ){
  3805   3875       Table *pTab = pFrom->pTab;
  3806         -    char *zIndex = pFrom->zIndex;
         3876  +    char *zIndexedBy = pFrom->zIndexedBy;
  3807   3877       Index *pIdx;
  3808   3878       for(pIdx=pTab->pIndex; 
  3809         -        pIdx && sqlite3StrICmp(pIdx->zName, zIndex); 
         3879  +        pIdx && sqlite3StrICmp(pIdx->zName, zIndexedBy); 
  3810   3880           pIdx=pIdx->pNext
  3811   3881       );
  3812   3882       if( !pIdx ){
  3813         -      sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
         3883  +      sqlite3ErrorMsg(pParse, "no such index: %s", zIndexedBy, 0);
  3814   3884         pParse->checkSchema = 1;
  3815   3885         return SQLITE_ERROR;
  3816   3886       }
  3817   3887       pFrom->pIndex = pIdx;
  3818   3888     }
  3819   3889     return SQLITE_OK;
  3820   3890   }
................................................................................
  4743   4813       p->pOrderBy = 0;
  4744   4814       p->selFlags &= ~SF_Distinct;
  4745   4815     }
  4746   4816     sqlite3SelectPrep(pParse, p, 0);
  4747   4817     memset(&sSort, 0, sizeof(sSort));
  4748   4818     sSort.pOrderBy = p->pOrderBy;
  4749   4819     pTabList = p->pSrc;
  4750         -  pEList = p->pEList;
  4751   4820     if( pParse->nErr || db->mallocFailed ){
  4752   4821       goto select_end;
  4753   4822     }
         4823  +  assert( p->pEList!=0 );
  4754   4824     isAgg = (p->selFlags & SF_Aggregate)!=0;
  4755         -  assert( pEList!=0 );
  4756   4825   #if SELECTTRACE_ENABLED
  4757   4826     if( sqlite3SelectTrace & 0x100 ){
  4758   4827       SELECTTRACE(0x100,pParse,p, ("after name resolution:\n"));
  4759   4828       sqlite3TreeViewSelect(0, p, 0);
  4760   4829     }
  4761   4830   #endif
  4762   4831   
  4763   4832   
  4764         -  /* Begin generating code.
  4765         -  */
  4766         -  v = sqlite3GetVdbe(pParse);
  4767         -  if( v==0 ) goto select_end;
  4768         -
  4769   4833     /* If writing to memory or generating a set
  4770   4834     ** only a single column may be output.
  4771   4835     */
  4772   4836   #ifndef SQLITE_OMIT_SUBQUERY
  4773         -  if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
         4837  +  if( checkForMultiColumnSelectError(pParse, pDest, p->pEList->nExpr) ){
  4774   4838       goto select_end;
  4775   4839     }
  4776   4840   #endif
         4841  +
         4842  +  /* Try to flatten subqueries in the FROM clause up into the main query
         4843  +  */
         4844  +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
         4845  +  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
         4846  +    struct SrcList_item *pItem = &pTabList->a[i];
         4847  +    Select *pSub = pItem->pSelect;
         4848  +    int isAggSub;
         4849  +    if( pSub==0 ) continue;
         4850  +    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
         4851  +    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
         4852  +      /* This subquery can be absorbed into its parent. */
         4853  +      if( isAggSub ){
         4854  +        isAgg = 1;
         4855  +        p->selFlags |= SF_Aggregate;
         4856  +      }
         4857  +      i = -1;
         4858  +    }
         4859  +    pTabList = p->pSrc;
         4860  +    if( db->mallocFailed ) goto select_end;
         4861  +    if( !IgnorableOrderby(pDest) ){
         4862  +      sSort.pOrderBy = p->pOrderBy;
         4863  +    }
         4864  +  }
         4865  +#endif
         4866  +
         4867  +  /* Get a pointer the VDBE under construction, allocating a new VDBE if one
         4868  +  ** does not already exist */
         4869  +  v = sqlite3GetVdbe(pParse);
         4870  +  if( v==0 ) goto select_end;
         4871  +
         4872  +#ifndef SQLITE_OMIT_COMPOUND_SELECT
         4873  +  /* Handle compound SELECT statements using the separate multiSelect()
         4874  +  ** procedure.
         4875  +  */
         4876  +  if( p->pPrior ){
         4877  +    rc = multiSelect(pParse, p, pDest);
         4878  +    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
         4879  +#if SELECTTRACE_ENABLED
         4880  +    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
         4881  +    pParse->nSelectIndent--;
         4882  +#endif
         4883  +    return rc;
         4884  +  }
         4885  +#endif
  4777   4886   
  4778   4887     /* Generate code for all sub-queries in the FROM clause
  4779   4888     */
  4780   4889   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  4781         -  for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
         4890  +  for(i=0; i<pTabList->nSrc; i++){
  4782   4891       struct SrcList_item *pItem = &pTabList->a[i];
  4783   4892       SelectDest dest;
  4784   4893       Select *pSub = pItem->pSelect;
  4785         -    int isAggSub;
  4786         -
  4787   4894       if( pSub==0 ) continue;
  4788   4895   
  4789   4896       /* Sometimes the code for a subquery will be generated more than
  4790   4897       ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
  4791   4898       ** for example.  In that case, do not regenerate the code to manifest
  4792   4899       ** a view or the co-routine to implement a view.  The first instance
  4793   4900       ** is sufficient, though the subroutine to manifest the view does need
................................................................................
  4804   4911       ** may contain expression trees of at most
  4805   4912       ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
  4806   4913       ** more conservative than necessary, but much easier than enforcing
  4807   4914       ** an exact limit.
  4808   4915       */
  4809   4916       pParse->nHeight += sqlite3SelectExprHeight(p);
  4810   4917   
  4811         -    isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
  4812         -    if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
  4813         -      /* This subquery can be absorbed into its parent. */
  4814         -      if( isAggSub ){
  4815         -        isAgg = 1;
  4816         -        p->selFlags |= SF_Aggregate;
         4918  +    /* Make copies of constant WHERE-clause terms in the outer query down
         4919  +    ** inside the subquery.  This can help the subquery to run more efficiently.
         4920  +    */
         4921  +    if( (pItem->jointype & JT_OUTER)==0
         4922  +     && pushDownWhereTerms(db, pSub, p->pWhere, pItem->iCursor)
         4923  +    ){
         4924  +#if SELECTTRACE_ENABLED
         4925  +      if( sqlite3SelectTrace & 0x100 ){
         4926  +        SELECTTRACE(0x100,pParse,p,("After WHERE-clause push-down:\n"));
         4927  +        sqlite3TreeViewSelect(0, p, 0);
  4817   4928         }
  4818         -      i = -1;
  4819         -    }else if( pTabList->nSrc==1
  4820         -           && (p->selFlags & SF_All)==0
  4821         -           && OptimizationEnabled(db, SQLITE_SubqCoroutine)
         4929  +#endif
         4930  +    }
         4931  +
         4932  +    /* Generate code to implement the subquery
         4933  +    */
         4934  +    if( pTabList->nSrc==1
         4935  +     && (p->selFlags & SF_All)==0
         4936  +     && OptimizationEnabled(db, SQLITE_SubqCoroutine)
  4822   4937       ){
  4823   4938         /* Implement a co-routine that will return a single row of the result
  4824   4939         ** set on each invocation.
  4825   4940         */
  4826   4941         int addrTop = sqlite3VdbeCurrentAddr(v)+1;
  4827   4942         pItem->regReturn = ++pParse->nMem;
  4828   4943         sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
................................................................................
  4865   4980         pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
  4866   4981         if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
  4867   4982         retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
  4868   4983         VdbeComment((v, "end %s", pItem->pTab->zName));
  4869   4984         sqlite3VdbeChangeP1(v, topAddr, retAddr);
  4870   4985         sqlite3ClearTempRegCache(pParse);
  4871   4986       }
  4872         -    if( /*pParse->nErr ||*/ db->mallocFailed ){
  4873         -      goto select_end;
  4874         -    }
         4987  +    if( db->mallocFailed ) goto select_end;
  4875   4988       pParse->nHeight -= sqlite3SelectExprHeight(p);
  4876         -    pTabList = p->pSrc;
  4877         -    if( !IgnorableOrderby(pDest) ){
  4878         -      sSort.pOrderBy = p->pOrderBy;
  4879         -    }
  4880   4989     }
  4881         -  pEList = p->pEList;
  4882   4990   #endif
         4991  +
         4992  +  /* Various elements of the SELECT copied into local variables for
         4993  +  ** convenience */
         4994  +  pEList = p->pEList;
  4883   4995     pWhere = p->pWhere;
  4884   4996     pGroupBy = p->pGroupBy;
  4885   4997     pHaving = p->pHaving;
  4886   4998     sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
  4887   4999   
  4888         -#ifndef SQLITE_OMIT_COMPOUND_SELECT
  4889         -  /* If there is are a sequence of queries, do the earlier ones first.
  4890         -  */
  4891         -  if( p->pPrior ){
  4892         -    rc = multiSelect(pParse, p, pDest);
  4893         -    explainSetInteger(pParse->iSelectId, iRestoreSelectId);
  4894   5000   #if SELECTTRACE_ENABLED
  4895         -    SELECTTRACE(1,pParse,p,("end compound-select processing\n"));
  4896         -    pParse->nSelectIndent--;
  4897         -#endif
  4898         -    return rc;
         5001  +  if( sqlite3SelectTrace & 0x400 ){
         5002  +    SELECTTRACE(0x400,pParse,p,("After all FROM-clause analysis:\n"));
         5003  +    sqlite3TreeViewSelect(0, p, 0);
  4899   5004     }
  4900   5005   #endif
  4901   5006   
  4902   5007     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
  4903   5008     ** if the select-list is the same as the ORDER BY list, then this query
  4904   5009     ** can be rewritten as a GROUP BY. In other words, this:
  4905   5010     **
................................................................................
  4911   5016     **
  4912   5017     ** The second form is preferred as a single index (or temp-table) may be 
  4913   5018     ** used for both the ORDER BY and DISTINCT processing. As originally 
  4914   5019     ** written the query must use a temp-table for at least one of the ORDER 
  4915   5020     ** BY and DISTINCT, and an index or separate temp-table for the other.
  4916   5021     */
  4917   5022     if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
  4918         -   && sqlite3ExprListCompare(sSort.pOrderBy, p->pEList, -1)==0
         5023  +   && sqlite3ExprListCompare(sSort.pOrderBy, pEList, -1)==0
  4919   5024     ){
  4920   5025       p->selFlags &= ~SF_Distinct;
  4921         -    p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
  4922         -    pGroupBy = p->pGroupBy;
         5026  +    pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
  4923   5027       /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
  4924   5028       ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
  4925   5029       ** original setting of the SF_Distinct flag, not the current setting */
  4926   5030       assert( sDistinct.isTnct );
  4927   5031     }
  4928   5032   
  4929         -  /* If there is an ORDER BY clause, then this sorting
  4930         -  ** index might end up being unused if the data can be 
  4931         -  ** extracted in pre-sorted order.  If that is the case, then the
  4932         -  ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
  4933         -  ** we figure out that the sorting index is not needed.  The addrSortIndex
  4934         -  ** variable is used to facilitate that change.
         5033  +  /* If there is an ORDER BY clause, then create an ephemeral index to
         5034  +  ** do the sorting.  But this sorting ephemeral index might end up
         5035  +  ** being unused if the data can be extracted in pre-sorted order.
         5036  +  ** If that is the case, then the OP_OpenEphemeral instruction will be
         5037  +  ** changed to an OP_Noop once we figure out that the sorting index is
         5038  +  ** not needed.  The sSort.addrSortIndex variable is used to facilitate
         5039  +  ** that change.
  4935   5040     */
  4936   5041     if( sSort.pOrderBy ){
  4937   5042       KeyInfo *pKeyInfo;
  4938   5043       pKeyInfo = keyInfoFromExprList(pParse, sSort.pOrderBy, 0, pEList->nExpr);
  4939   5044       sSort.iECursor = pParse->nTab++;
  4940   5045       sSort.addrSortIndex =
  4941   5046         sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
................................................................................
  4958   5063     p->nSelectRow = LARGEST_INT64;
  4959   5064     computeLimitRegisters(pParse, p, iEnd);
  4960   5065     if( p->iLimit==0 && sSort.addrSortIndex>=0 ){
  4961   5066       sqlite3VdbeGetOp(v, sSort.addrSortIndex)->opcode = OP_SorterOpen;
  4962   5067       sSort.sortFlags |= SORTFLAG_UseSorter;
  4963   5068     }
  4964   5069   
  4965         -  /* Open a virtual index to use for the distinct set.
         5070  +  /* Open an ephemeral index to use for the distinct set.
  4966   5071     */
  4967   5072     if( p->selFlags & SF_Distinct ){
  4968   5073       sDistinct.tabTnct = pParse->nTab++;
  4969   5074       sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
  4970         -                                sDistinct.tabTnct, 0, 0,
  4971         -                                (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
  4972         -                                P4_KEYINFO);
         5075  +                             sDistinct.tabTnct, 0, 0,
         5076  +                             (char*)keyInfoFromExprList(pParse, p->pEList,0,0),
         5077  +                             P4_KEYINFO);
  4973   5078       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  4974   5079       sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
  4975   5080     }else{
  4976   5081       sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
  4977   5082     }
  4978   5083   
  4979   5084     if( !isAgg && pGroupBy==0 ){
................................................................................
  5043   5148           pItem->u.x.iAlias = 0;
  5044   5149         }
  5045   5150         if( p->nSelectRow>100 ) p->nSelectRow = 100;
  5046   5151       }else{
  5047   5152         p->nSelectRow = 1;
  5048   5153       }
  5049   5154   
  5050         -
  5051   5155       /* If there is both a GROUP BY and an ORDER BY clause and they are
  5052   5156       ** identical, then it may be possible to disable the ORDER BY clause 
  5053   5157       ** on the grounds that the GROUP BY will cause elements to come out 
  5054         -    ** in the correct order. It also may not - the GROUP BY may use a
         5158  +    ** in the correct order. It also may not - the GROUP BY might use a
  5055   5159       ** database index that causes rows to be grouped together as required
  5056   5160       ** but not actually sorted. Either way, record the fact that the
  5057   5161       ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
  5058   5162       ** variable.  */
  5059   5163       if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
  5060   5164         orderByGrp = 1;
  5061   5165       }
................................................................................
  5225   5329         ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
  5226   5330         ** Then compare the current GROUP BY terms against the GROUP BY terms
  5227   5331         ** from the previous row currently stored in a0, a1, a2...
  5228   5332         */
  5229   5333         addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
  5230   5334         sqlite3ExprCacheClear(pParse);
  5231   5335         if( groupBySort ){
  5232         -        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx, sortOut,sortPTab);
         5336  +        sqlite3VdbeAddOp3(v, OP_SorterData, sAggInfo.sortingIdx,
         5337  +                          sortOut, sortPTab);
  5233   5338         }
  5234   5339         for(j=0; j<pGroupBy->nExpr; j++){
  5235   5340           if( groupBySort ){
  5236   5341             sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
  5237   5342           }else{
  5238   5343             sAggInfo.directMode = 1;
  5239   5344             sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
................................................................................
  5297   5402         */
  5298   5403         addrSetAbort = sqlite3VdbeCurrentAddr(v);
  5299   5404         sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
  5300   5405         VdbeComment((v, "set abort flag"));
  5301   5406         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5302   5407         sqlite3VdbeResolveLabel(v, addrOutputRow);
  5303   5408         addrOutputRow = sqlite3VdbeCurrentAddr(v);
  5304         -      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
         5409  +      sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
         5410  +      VdbeCoverage(v);
  5305   5411         VdbeComment((v, "Groupby result generator entry point"));
  5306   5412         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  5307   5413         finalizeAggFunctions(pParse, &sAggInfo);
  5308   5414         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
  5309   5415         selectInnerLoop(pParse, p, p->pEList, -1, &sSort,
  5310   5416                         &sDistinct, pDest,
  5311   5417                         addrOutputRow+1, addrSetAbort);
................................................................................
  5461   5567       explainTempTable(pParse, "DISTINCT");
  5462   5568     }
  5463   5569   
  5464   5570     /* If there is an ORDER BY clause, then we need to sort the results
  5465   5571     ** and send them to the callback one by one.
  5466   5572     */
  5467   5573     if( sSort.pOrderBy ){
  5468         -    explainTempTable(pParse, sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
         5574  +    explainTempTable(pParse,
         5575  +                     sSort.nOBSat>0 ? "RIGHT PART OF ORDER BY":"ORDER BY");
  5469   5576       generateSortTail(pParse, p, &sSort, pEList->nExpr, pDest);
  5470   5577     }
  5471   5578   
  5472   5579     /* Jump here to skip this query
  5473   5580     */
  5474   5581     sqlite3VdbeResolveLabel(v, iEnd);
  5475   5582   
................................................................................
  5493   5600     sqlite3DbFree(db, sAggInfo.aFunc);
  5494   5601   #if SELECTTRACE_ENABLED
  5495   5602     SELECTTRACE(1,pParse,p,("end processing\n"));
  5496   5603     pParse->nSelectIndent--;
  5497   5604   #endif
  5498   5605     return rc;
  5499   5606   }
  5500         -
  5501         -#ifdef SQLITE_DEBUG
  5502         -/*
  5503         -** Generate a human-readable description of a the Select object.
  5504         -*/
  5505         -void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
  5506         -  int n = 0;
  5507         -  pView = sqlite3TreeViewPush(pView, moreToFollow);
  5508         -  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
  5509         -    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
  5510         -    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
  5511         -  );
  5512         -  if( p->pSrc && p->pSrc->nSrc ) n++;
  5513         -  if( p->pWhere ) n++;
  5514         -  if( p->pGroupBy ) n++;
  5515         -  if( p->pHaving ) n++;
  5516         -  if( p->pOrderBy ) n++;
  5517         -  if( p->pLimit ) n++;
  5518         -  if( p->pOffset ) n++;
  5519         -  if( p->pPrior ) n++;
  5520         -  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
  5521         -  if( p->pSrc && p->pSrc->nSrc ){
  5522         -    int i;
  5523         -    pView = sqlite3TreeViewPush(pView, (n--)>0);
  5524         -    sqlite3TreeViewLine(pView, "FROM");
  5525         -    for(i=0; i<p->pSrc->nSrc; i++){
  5526         -      struct SrcList_item *pItem = &p->pSrc->a[i];
  5527         -      StrAccum x;
  5528         -      char zLine[100];
  5529         -      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
  5530         -      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
  5531         -      if( pItem->zDatabase ){
  5532         -        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
  5533         -      }else if( pItem->zName ){
  5534         -        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
  5535         -      }
  5536         -      if( pItem->pTab ){
  5537         -        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
  5538         -      }
  5539         -      if( pItem->zAlias ){
  5540         -        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
  5541         -      }
  5542         -      if( pItem->jointype & JT_LEFT ){
  5543         -        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
  5544         -      }
  5545         -      sqlite3StrAccumFinish(&x);
  5546         -      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
  5547         -      if( pItem->pSelect ){
  5548         -        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
  5549         -      }
  5550         -      sqlite3TreeViewPop(pView);
  5551         -    }
  5552         -    sqlite3TreeViewPop(pView);
  5553         -  }
  5554         -  if( p->pWhere ){
  5555         -    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
  5556         -    sqlite3TreeViewExpr(pView, p->pWhere, 0);
  5557         -    sqlite3TreeViewPop(pView);
  5558         -  }
  5559         -  if( p->pGroupBy ){
  5560         -    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
  5561         -  }
  5562         -  if( p->pHaving ){
  5563         -    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
  5564         -    sqlite3TreeViewExpr(pView, p->pHaving, 0);
  5565         -    sqlite3TreeViewPop(pView);
  5566         -  }
  5567         -  if( p->pOrderBy ){
  5568         -    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
  5569         -  }
  5570         -  if( p->pLimit ){
  5571         -    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
  5572         -    sqlite3TreeViewExpr(pView, p->pLimit, 0);
  5573         -    sqlite3TreeViewPop(pView);
  5574         -  }
  5575         -  if( p->pOffset ){
  5576         -    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
  5577         -    sqlite3TreeViewExpr(pView, p->pOffset, 0);
  5578         -    sqlite3TreeViewPop(pView);
  5579         -  }
  5580         -  if( p->pPrior ){
  5581         -    const char *zOp = "UNION";
  5582         -    switch( p->op ){
  5583         -      case TK_ALL:         zOp = "UNION ALL";  break;
  5584         -      case TK_INTERSECT:   zOp = "INTERSECT";  break;
  5585         -      case TK_EXCEPT:      zOp = "EXCEPT";     break;
  5586         -    }
  5587         -    sqlite3TreeViewItem(pView, zOp, (n--)>0);
  5588         -    sqlite3TreeViewSelect(pView, p->pPrior, 0);
  5589         -    sqlite3TreeViewPop(pView);
  5590         -  }
  5591         -  sqlite3TreeViewPop(pView);
  5592         -}
  5593         -#endif /* SQLITE_DEBUG */

Changes to src/sqlite.h.in.

   952    952   **
   953    953   ** <li>[[SQLITE_FCNTL_WIN32_SET_HANDLE]]
   954    954   ** The [SQLITE_FCNTL_WIN32_SET_HANDLE] opcode is used for debugging.  This
   955    955   ** opcode causes the xFileControl method to swap the file handle with the one
   956    956   ** pointed to by the pArg argument.  This capability is used during testing
   957    957   ** and only needs to be supported when SQLITE_TEST is defined.
   958    958   **
   959         -* <li>[[SQLITE_FCNTL_WAL_BLOCK]]
          959  +** <li>[[SQLITE_FCNTL_WAL_BLOCK]]
   960    960   ** The [SQLITE_FCNTL_WAL_BLOCK] is a signal to the VFS layer that it might
   961    961   ** be advantageous to block on the next WAL lock if the lock is not immediately
   962    962   ** available.  The WAL subsystem issues this signal during rare
   963    963   ** circumstances in order to fix a problem with priority inversion.
   964    964   ** Applications should <em>not</em> use this file-control.
   965    965   **
   966    966   ** <li>[[SQLITE_FCNTL_ZIPVFS]]

Changes to src/sqliteInt.h.

  1505   1505   ** the speed a little by numbering the values consecutively.  
  1506   1506   **
  1507   1507   ** But rather than start with 0 or 1, we begin with 'A'.  That way,
  1508   1508   ** when multiple affinity types are concatenated into a string and
  1509   1509   ** used as the P4 operand, they will be more readable.
  1510   1510   **
  1511   1511   ** Note also that the numeric types are grouped together so that testing
  1512         -** for a numeric type is a single comparison.  And the NONE type is first.
         1512  +** for a numeric type is a single comparison.  And the BLOB type is first.
  1513   1513   */
  1514         -#define SQLITE_AFF_NONE     'A'
         1514  +#define SQLITE_AFF_BLOB     'A'
  1515   1515   #define SQLITE_AFF_TEXT     'B'
  1516   1516   #define SQLITE_AFF_NUMERIC  'C'
  1517   1517   #define SQLITE_AFF_INTEGER  'D'
  1518   1518   #define SQLITE_AFF_REAL     'E'
  1519   1519   
  1520   1520   #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
  1521   1521   
................................................................................
  2264   2264   #ifndef SQLITE_OMIT_EXPLAIN
  2265   2265       u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
  2266   2266   #endif
  2267   2267       int iCursor;      /* The VDBE cursor number used to access this table */
  2268   2268       Expr *pOn;        /* The ON clause of a join */
  2269   2269       IdList *pUsing;   /* The USING clause of a join */
  2270   2270       Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
  2271         -    char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
         2271  +    char *zIndexedBy; /* Identifier from "INDEXED BY <zIndex>" clause */
  2272   2272       Index *pIndex;    /* Index structure corresponding to zIndex, if any */
  2273   2273     } a[1];             /* One entry for each identifier on the list */
  2274   2274   };
  2275   2275   
  2276   2276   /*
  2277   2277   ** Permitted values of the SrcList.a.jointype field
  2278   2278   */
................................................................................
  3178   3178     void sqlite3DebugPrintf(const char*, ...);
  3179   3179   #endif
  3180   3180   #if defined(SQLITE_TEST)
  3181   3181     void *sqlite3TestTextToPtr(const char*);
  3182   3182   #endif
  3183   3183   
  3184   3184   #if defined(SQLITE_DEBUG)
  3185         -  TreeView *sqlite3TreeViewPush(TreeView*,u8);
  3186         -  void sqlite3TreeViewPop(TreeView*);
  3187         -  void sqlite3TreeViewLine(TreeView*, const char*, ...);
  3188         -  void sqlite3TreeViewItem(TreeView*, const char*, u8);
  3189   3185     void sqlite3TreeViewExpr(TreeView*, const Expr*, u8);
  3190   3186     void sqlite3TreeViewExprList(TreeView*, const ExprList*, u8, const char*);
  3191   3187     void sqlite3TreeViewSelect(TreeView*, const Select*, u8);
  3192   3188   #endif
  3193   3189   
  3194   3190   
  3195   3191   void sqlite3SetString(char **, sqlite3*, const char*);
................................................................................
  3338   3334   int sqlite3ExprCodeTarget(Parse*, Expr*, int);
  3339   3335   void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
  3340   3336   int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
  3341   3337   #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
  3342   3338   #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
  3343   3339   void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
  3344   3340   void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
         3341  +void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
  3345   3342   Table *sqlite3FindTable(sqlite3*,const char*, const char*);
  3346   3343   Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
  3347   3344   Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
  3348   3345   Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
  3349   3346   void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
  3350   3347   void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
  3351   3348   void sqlite3Vacuum(Parse*);

Added src/treeview.c.

            1  +/*
            2  +** 2015-06-08
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** This file contains C code to implement the TreeView debugging routines.
           14  +** These routines print a parse tree to standard output for debugging and
           15  +** analysis. 
           16  +**
           17  +** The interfaces in this file is only available when compiling
           18  +** with SQLITE_DEBUG.
           19  +*/
           20  +#include "sqliteInt.h"
           21  +#ifdef SQLITE_DEBUG
           22  +
           23  +/*
           24  +** Add a new subitem to the tree.  The moreToFollow flag indicates that this
           25  +** is not the last item in the tree.
           26  +*/
           27  +static TreeView *sqlite3TreeViewPush(TreeView *p, u8 moreToFollow){
           28  +  if( p==0 ){
           29  +    p = sqlite3_malloc64( sizeof(*p) );
           30  +    if( p==0 ) return 0;
           31  +    memset(p, 0, sizeof(*p));
           32  +  }else{
           33  +    p->iLevel++;
           34  +  }
           35  +  assert( moreToFollow==0 || moreToFollow==1 );
           36  +  if( p->iLevel<sizeof(p->bLine) ) p->bLine[p->iLevel] = moreToFollow;
           37  +  return p;
           38  +}
           39  +
           40  +/*
           41  +** Finished with one layer of the tree
           42  +*/
           43  +static void sqlite3TreeViewPop(TreeView *p){
           44  +  if( p==0 ) return;
           45  +  p->iLevel--;
           46  +  if( p->iLevel<0 ) sqlite3_free(p);
           47  +}
           48  +
           49  +/*
           50  +** Generate a single line of output for the tree, with a prefix that contains
           51  +** all the appropriate tree lines
           52  +*/
           53  +static void sqlite3TreeViewLine(TreeView *p, const char *zFormat, ...){
           54  +  va_list ap;
           55  +  int i;
           56  +  StrAccum acc;
           57  +  char zBuf[500];
           58  +  sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
           59  +  if( p ){
           60  +    for(i=0; i<p->iLevel && i<sizeof(p->bLine)-1; i++){
           61  +      sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|   " : "    ", 4);
           62  +    }
           63  +    sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
           64  +  }
           65  +  va_start(ap, zFormat);
           66  +  sqlite3VXPrintf(&acc, 0, zFormat, ap);
           67  +  va_end(ap);
           68  +  if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
           69  +  sqlite3StrAccumFinish(&acc);
           70  +  fprintf(stdout,"%s", zBuf);
           71  +  fflush(stdout);
           72  +}
           73  +
           74  +/*
           75  +** Shorthand for starting a new tree item that consists of a single label
           76  +*/
           77  +static void sqlite3TreeViewItem(TreeView *p, const char *zLabel,u8 moreFollows){
           78  +  p = sqlite3TreeViewPush(p, moreFollows);
           79  +  sqlite3TreeViewLine(p, "%s", zLabel);
           80  +}
           81  +
           82  +
           83  +/*
           84  +** Generate a human-readable description of a the Select object.
           85  +*/
           86  +void sqlite3TreeViewSelect(TreeView *pView, const Select *p, u8 moreToFollow){
           87  +  int n = 0;
           88  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
           89  +  sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x",
           90  +    ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
           91  +    ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags
           92  +  );
           93  +  if( p->pSrc && p->pSrc->nSrc ) n++;
           94  +  if( p->pWhere ) n++;
           95  +  if( p->pGroupBy ) n++;
           96  +  if( p->pHaving ) n++;
           97  +  if( p->pOrderBy ) n++;
           98  +  if( p->pLimit ) n++;
           99  +  if( p->pOffset ) n++;
          100  +  if( p->pPrior ) n++;
          101  +  sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
          102  +  if( p->pSrc && p->pSrc->nSrc ){
          103  +    int i;
          104  +    pView = sqlite3TreeViewPush(pView, (n--)>0);
          105  +    sqlite3TreeViewLine(pView, "FROM");
          106  +    for(i=0; i<p->pSrc->nSrc; i++){
          107  +      struct SrcList_item *pItem = &p->pSrc->a[i];
          108  +      StrAccum x;
          109  +      char zLine[100];
          110  +      sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
          111  +      sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
          112  +      if( pItem->zDatabase ){
          113  +        sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
          114  +      }else if( pItem->zName ){
          115  +        sqlite3XPrintf(&x, 0, " %s", pItem->zName);
          116  +      }
          117  +      if( pItem->pTab ){
          118  +        sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
          119  +      }
          120  +      if( pItem->zAlias ){
          121  +        sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
          122  +      }
          123  +      if( pItem->jointype & JT_LEFT ){
          124  +        sqlite3XPrintf(&x, 0, " LEFT-JOIN");
          125  +      }
          126  +      sqlite3StrAccumFinish(&x);
          127  +      sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1); 
          128  +      if( pItem->pSelect ){
          129  +        sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
          130  +      }
          131  +      sqlite3TreeViewPop(pView);
          132  +    }
          133  +    sqlite3TreeViewPop(pView);
          134  +  }
          135  +  if( p->pWhere ){
          136  +    sqlite3TreeViewItem(pView, "WHERE", (n--)>0);
          137  +    sqlite3TreeViewExpr(pView, p->pWhere, 0);
          138  +    sqlite3TreeViewPop(pView);
          139  +  }
          140  +  if( p->pGroupBy ){
          141  +    sqlite3TreeViewExprList(pView, p->pGroupBy, (n--)>0, "GROUPBY");
          142  +  }
          143  +  if( p->pHaving ){
          144  +    sqlite3TreeViewItem(pView, "HAVING", (n--)>0);
          145  +    sqlite3TreeViewExpr(pView, p->pHaving, 0);
          146  +    sqlite3TreeViewPop(pView);
          147  +  }
          148  +  if( p->pOrderBy ){
          149  +    sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
          150  +  }
          151  +  if( p->pLimit ){
          152  +    sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
          153  +    sqlite3TreeViewExpr(pView, p->pLimit, 0);
          154  +    sqlite3TreeViewPop(pView);
          155  +  }
          156  +  if( p->pOffset ){
          157  +    sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
          158  +    sqlite3TreeViewExpr(pView, p->pOffset, 0);
          159  +    sqlite3TreeViewPop(pView);
          160  +  }
          161  +  if( p->pPrior ){
          162  +    const char *zOp = "UNION";
          163  +    switch( p->op ){
          164  +      case TK_ALL:         zOp = "UNION ALL";  break;
          165  +      case TK_INTERSECT:   zOp = "INTERSECT";  break;
          166  +      case TK_EXCEPT:      zOp = "EXCEPT";     break;
          167  +    }
          168  +    sqlite3TreeViewItem(pView, zOp, (n--)>0);
          169  +    sqlite3TreeViewSelect(pView, p->pPrior, 0);
          170  +    sqlite3TreeViewPop(pView);
          171  +  }
          172  +  sqlite3TreeViewPop(pView);
          173  +}
          174  +
          175  +/*
          176  +** Generate a human-readable explanation of an expression tree.
          177  +*/
          178  +void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){
          179  +  const char *zBinOp = 0;   /* Binary operator */
          180  +  const char *zUniOp = 0;   /* Unary operator */
          181  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
          182  +  if( pExpr==0 ){
          183  +    sqlite3TreeViewLine(pView, "nil");
          184  +    sqlite3TreeViewPop(pView);
          185  +    return;
          186  +  }
          187  +  switch( pExpr->op ){
          188  +    case TK_AGG_COLUMN: {
          189  +      sqlite3TreeViewLine(pView, "AGG{%d:%d}",
          190  +            pExpr->iTable, pExpr->iColumn);
          191  +      break;
          192  +    }
          193  +    case TK_COLUMN: {
          194  +      if( pExpr->iTable<0 ){
          195  +        /* This only happens when coding check constraints */
          196  +        sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn);
          197  +      }else{
          198  +        sqlite3TreeViewLine(pView, "{%d:%d}",
          199  +                             pExpr->iTable, pExpr->iColumn);
          200  +      }
          201  +      break;
          202  +    }
          203  +    case TK_INTEGER: {
          204  +      if( pExpr->flags & EP_IntValue ){
          205  +        sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue);
          206  +      }else{
          207  +        sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken);
          208  +      }
          209  +      break;
          210  +    }
          211  +#ifndef SQLITE_OMIT_FLOATING_POINT
          212  +    case TK_FLOAT: {
          213  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
          214  +      break;
          215  +    }
          216  +#endif
          217  +    case TK_STRING: {
          218  +      sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
          219  +      break;
          220  +    }
          221  +    case TK_NULL: {
          222  +      sqlite3TreeViewLine(pView,"NULL");
          223  +      break;
          224  +    }
          225  +#ifndef SQLITE_OMIT_BLOB_LITERAL
          226  +    case TK_BLOB: {
          227  +      sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
          228  +      break;
          229  +    }
          230  +#endif
          231  +    case TK_VARIABLE: {
          232  +      sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)",
          233  +                          pExpr->u.zToken, pExpr->iColumn);
          234  +      break;
          235  +    }
          236  +    case TK_REGISTER: {
          237  +      sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable);
          238  +      break;
          239  +    }
          240  +    case TK_AS: {
          241  +      sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken);
          242  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          243  +      break;
          244  +    }
          245  +    case TK_ID: {
          246  +      sqlite3TreeViewLine(pView,"ID \"%w\"", pExpr->u.zToken);
          247  +      break;
          248  +    }
          249  +#ifndef SQLITE_OMIT_CAST
          250  +    case TK_CAST: {
          251  +      /* Expressions of the form:   CAST(pLeft AS token) */
          252  +      sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken);
          253  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          254  +      break;
          255  +    }
          256  +#endif /* SQLITE_OMIT_CAST */
          257  +    case TK_LT:      zBinOp = "LT";     break;
          258  +    case TK_LE:      zBinOp = "LE";     break;
          259  +    case TK_GT:      zBinOp = "GT";     break;
          260  +    case TK_GE:      zBinOp = "GE";     break;
          261  +    case TK_NE:      zBinOp = "NE";     break;
          262  +    case TK_EQ:      zBinOp = "EQ";     break;
          263  +    case TK_IS:      zBinOp = "IS";     break;
          264  +    case TK_ISNOT:   zBinOp = "ISNOT";  break;
          265  +    case TK_AND:     zBinOp = "AND";    break;
          266  +    case TK_OR:      zBinOp = "OR";     break;
          267  +    case TK_PLUS:    zBinOp = "ADD";    break;
          268  +    case TK_STAR:    zBinOp = "MUL";    break;
          269  +    case TK_MINUS:   zBinOp = "SUB";    break;
          270  +    case TK_REM:     zBinOp = "REM";    break;
          271  +    case TK_BITAND:  zBinOp = "BITAND"; break;
          272  +    case TK_BITOR:   zBinOp = "BITOR";  break;
          273  +    case TK_SLASH:   zBinOp = "DIV";    break;
          274  +    case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
          275  +    case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
          276  +    case TK_CONCAT:  zBinOp = "CONCAT"; break;
          277  +    case TK_DOT:     zBinOp = "DOT";    break;
          278  +
          279  +    case TK_UMINUS:  zUniOp = "UMINUS"; break;
          280  +    case TK_UPLUS:   zUniOp = "UPLUS";  break;
          281  +    case TK_BITNOT:  zUniOp = "BITNOT"; break;
          282  +    case TK_NOT:     zUniOp = "NOT";    break;
          283  +    case TK_ISNULL:  zUniOp = "ISNULL"; break;
          284  +    case TK_NOTNULL: zUniOp = "NOTNULL"; break;
          285  +
          286  +    case TK_COLLATE: {
          287  +      sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken);
          288  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          289  +      break;
          290  +    }
          291  +
          292  +    case TK_AGG_FUNCTION:
          293  +    case TK_FUNCTION: {
          294  +      ExprList *pFarg;       /* List of function arguments */
          295  +      if( ExprHasProperty(pExpr, EP_TokenOnly) ){
          296  +        pFarg = 0;
          297  +      }else{
          298  +        pFarg = pExpr->x.pList;
          299  +      }
          300  +      if( pExpr->op==TK_AGG_FUNCTION ){
          301  +        sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q",
          302  +                             pExpr->op2, pExpr->u.zToken);
          303  +      }else{
          304  +        sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken);
          305  +      }
          306  +      if( pFarg ){
          307  +        sqlite3TreeViewExprList(pView, pFarg, 0, 0);
          308  +      }
          309  +      break;
          310  +    }
          311  +#ifndef SQLITE_OMIT_SUBQUERY
          312  +    case TK_EXISTS: {
          313  +      sqlite3TreeViewLine(pView, "EXISTS-expr");
          314  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
          315  +      break;
          316  +    }
          317  +    case TK_SELECT: {
          318  +      sqlite3TreeViewLine(pView, "SELECT-expr");
          319  +      sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
          320  +      break;
          321  +    }
          322  +    case TK_IN: {
          323  +      sqlite3TreeViewLine(pView, "IN");
          324  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
          325  +      if( ExprHasProperty(pExpr, EP_xIsSelect) ){
          326  +        sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0);
          327  +      }else{
          328  +        sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
          329  +      }
          330  +      break;
          331  +    }
          332  +#endif /* SQLITE_OMIT_SUBQUERY */
          333  +
          334  +    /*
          335  +    **    x BETWEEN y AND z
          336  +    **
          337  +    ** This is equivalent to
          338  +    **
          339  +    **    x>=y AND x<=z
          340  +    **
          341  +    ** X is stored in pExpr->pLeft.
          342  +    ** Y is stored in pExpr->pList->a[0].pExpr.
          343  +    ** Z is stored in pExpr->pList->a[1].pExpr.
          344  +    */
          345  +    case TK_BETWEEN: {
          346  +      Expr *pX = pExpr->pLeft;
          347  +      Expr *pY = pExpr->x.pList->a[0].pExpr;
          348  +      Expr *pZ = pExpr->x.pList->a[1].pExpr;
          349  +      sqlite3TreeViewLine(pView, "BETWEEN");
          350  +      sqlite3TreeViewExpr(pView, pX, 1);
          351  +      sqlite3TreeViewExpr(pView, pY, 1);
          352  +      sqlite3TreeViewExpr(pView, pZ, 0);
          353  +      break;
          354  +    }
          355  +    case TK_TRIGGER: {
          356  +      /* If the opcode is TK_TRIGGER, then the expression is a reference
          357  +      ** to a column in the new.* or old.* pseudo-tables available to
          358  +      ** trigger programs. In this case Expr.iTable is set to 1 for the
          359  +      ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
          360  +      ** is set to the column of the pseudo-table to read, or to -1 to
          361  +      ** read the rowid field.
          362  +      */
          363  +      sqlite3TreeViewLine(pView, "%s(%d)", 
          364  +          pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
          365  +      break;
          366  +    }
          367  +    case TK_CASE: {
          368  +      sqlite3TreeViewLine(pView, "CASE");
          369  +      sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
          370  +      sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0);
          371  +      break;
          372  +    }
          373  +#ifndef SQLITE_OMIT_TRIGGER
          374  +    case TK_RAISE: {
          375  +      const char *zType = "unk";
          376  +      switch( pExpr->affinity ){
          377  +        case OE_Rollback:   zType = "rollback";  break;
          378  +        case OE_Abort:      zType = "abort";     break;
          379  +        case OE_Fail:       zType = "fail";      break;
          380  +        case OE_Ignore:     zType = "ignore";    break;
          381  +      }
          382  +      sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken);
          383  +      break;
          384  +    }
          385  +#endif
          386  +    default: {
          387  +      sqlite3TreeViewLine(pView, "op=%d", pExpr->op);
          388  +      break;
          389  +    }
          390  +  }
          391  +  if( zBinOp ){
          392  +    sqlite3TreeViewLine(pView, "%s", zBinOp);
          393  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 1);
          394  +    sqlite3TreeViewExpr(pView, pExpr->pRight, 0);
          395  +  }else if( zUniOp ){
          396  +    sqlite3TreeViewLine(pView, "%s", zUniOp);
          397  +    sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
          398  +  }
          399  +  sqlite3TreeViewPop(pView);
          400  +}
          401  +
          402  +/*
          403  +** Generate a human-readable explanation of an expression list.
          404  +*/
          405  +void sqlite3TreeViewExprList(
          406  +  TreeView *pView,
          407  +  const ExprList *pList,
          408  +  u8 moreToFollow,
          409  +  const char *zLabel
          410  +){
          411  +  int i;
          412  +  pView = sqlite3TreeViewPush(pView, moreToFollow);
          413  +  if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST";
          414  +  if( pList==0 ){
          415  +    sqlite3TreeViewLine(pView, "%s (empty)", zLabel);
          416  +  }else{
          417  +    sqlite3TreeViewLine(pView, "%s", zLabel);
          418  +    for(i=0; i<pList->nExpr; i++){
          419  +      sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1);
          420  +    }
          421  +  }
          422  +  sqlite3TreeViewPop(pView);
          423  +}
          424  +
          425  +#endif /* SQLITE_DEBUG */

Changes to src/vdbe.c.

   276    276   **    is not possible.  Note that the integer representation is
   277    277   **    always preferred, even if the affinity is REAL, because
   278    278   **    an integer representation is more space efficient on disk.
   279    279   **
   280    280   ** SQLITE_AFF_TEXT:
   281    281   **    Convert pRec to a text representation.
   282    282   **
   283         -** SQLITE_AFF_NONE:
          283  +** SQLITE_AFF_BLOB:
   284    284   **    No-op.  pRec is unchanged.
   285    285   */
   286    286   static void applyAffinity(
   287    287     Mem *pRec,          /* The value to apply affinity to */
   288    288     char affinity,      /* The affinity to be applied */
   289    289     u8 enc              /* Use this text encoding */
   290    290   ){
................................................................................
  1786   1786   ** <li value="100"> INTEGER
  1787   1787   ** <li value="101"> REAL
  1788   1788   ** </ul>
  1789   1789   **
  1790   1790   ** A NULL value is not changed by this routine.  It remains NULL.
  1791   1791   */
  1792   1792   case OP_Cast: {                  /* in1 */
  1793         -  assert( pOp->p2>=SQLITE_AFF_NONE && pOp->p2<=SQLITE_AFF_REAL );
         1793  +  assert( pOp->p2>=SQLITE_AFF_BLOB && pOp->p2<=SQLITE_AFF_REAL );
  1794   1794     testcase( pOp->p2==SQLITE_AFF_TEXT );
  1795         -  testcase( pOp->p2==SQLITE_AFF_NONE );
         1795  +  testcase( pOp->p2==SQLITE_AFF_BLOB );
  1796   1796     testcase( pOp->p2==SQLITE_AFF_NUMERIC );
  1797   1797     testcase( pOp->p2==SQLITE_AFF_INTEGER );
  1798   1798     testcase( pOp->p2==SQLITE_AFF_REAL );
  1799   1799     pIn1 = &aMem[pOp->p1];
  1800   1800     memAboutToChange(p, pIn1);
  1801   1801     rc = ExpandBlob(pIn1);
  1802   1802     sqlite3VdbeMemCast(pIn1, pOp->p2, encoding);
................................................................................
  2599   2599   ** P4 may be a string that is P2 characters long.  The nth character of the
  2600   2600   ** string indicates the column affinity that should be used for the nth
  2601   2601   ** field of the index key.
  2602   2602   **
  2603   2603   ** The mapping from character to affinity is given by the SQLITE_AFF_
  2604   2604   ** macros defined in sqliteInt.h.
  2605   2605   **
  2606         -** If P4 is NULL then all index fields have the affinity NONE.
         2606  +** If P4 is NULL then all index fields have the affinity BLOB.
  2607   2607   */
  2608   2608   case OP_MakeRecord: {
  2609   2609     u8 *zNewRecord;        /* A buffer to hold the data for the new record */
  2610   2610     Mem *pRec;             /* The new record */
  2611   2611     u64 nData;             /* Number of bytes of data space */
  2612   2612     int nHdr;              /* Number of bytes of header space */
  2613   2613     i64 nByte;             /* Data space required for this record */

Changes to src/vdbemem.c.

   584    584   ** is forced.  In other words, the value is converted into the desired
   585    585   ** affinity even if that results in loss of data.  This routine is
   586    586   ** used (for example) to implement the SQL "cast()" operator.
   587    587   */
   588    588   void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
   589    589     if( pMem->flags & MEM_Null ) return;
   590    590     switch( aff ){
   591         -    case SQLITE_AFF_NONE: {   /* Really a cast to BLOB */
          591  +    case SQLITE_AFF_BLOB: {   /* Really a cast to BLOB */
   592    592         if( (pMem->flags & MEM_Blob)==0 ){
   593    593           sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
   594    594           assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
   595    595           MemSetTypeFlag(pMem, MEM_Blob);
   596    596         }else{
   597    597           pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
   598    598         }
................................................................................
  1303   1303       if( ExprHasProperty(pExpr, EP_IntValue) ){
  1304   1304         sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
  1305   1305       }else{
  1306   1306         zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
  1307   1307         if( zVal==0 ) goto no_mem;
  1308   1308         sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
  1309   1309       }
  1310         -    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
         1310  +    if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
  1311   1311         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
  1312   1312       }else{
  1313   1313         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
  1314   1314       }
  1315   1315       if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
  1316   1316       if( enc!=SQLITE_UTF8 ){
  1317   1317         rc = sqlite3VdbeChangeEncoding(pVal, enc);

Changes to src/where.c.

    14     14   ** generating the code that loops through a table looking for applicable
    15     15   ** rows.  Indices are selected and used to speed the search when doing
    16     16   ** so is applicable.  Because this module is responsible for selecting
    17     17   ** indices, you might also think of this module as the "query optimizer".
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "whereInt.h"
           21  +
           22  +/* Forward declaration of methods */
           23  +static int whereLoopResize(sqlite3*, WhereLoop*, int);
           24  +
           25  +/* Test variable that can be set to enable WHERE tracing */
           26  +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
           27  +/***/ int sqlite3WhereTrace = 0;
           28  +#endif
           29  +
    21     30   
    22     31   /*
    23     32   ** Return the estimated number of output rows from a WHERE clause
    24     33   */
    25     34   u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
    26     35     return sqlite3LogEstToInt(pWInfo->nRowOut);
    27     36   }
................................................................................
   123    132   whereOrInsert_done:
   124    133     p->prereq = prereq;
   125    134     p->rRun = rRun;
   126    135     if( p->nOut>nOut ) p->nOut = nOut;
   127    136     return 1;
   128    137   }
   129    138   
   130         -/*
   131         -** Initialize a preallocated WhereClause structure.
   132         -*/
   133         -static void whereClauseInit(
   134         -  WhereClause *pWC,        /* The WhereClause to be initialized */
   135         -  WhereInfo *pWInfo        /* The WHERE processing context */
   136         -){
   137         -  pWC->pWInfo = pWInfo;
   138         -  pWC->pOuter = 0;
   139         -  pWC->nTerm = 0;
   140         -  pWC->nSlot = ArraySize(pWC->aStatic);
   141         -  pWC->a = pWC->aStatic;
   142         -}
   143         -
   144         -/* Forward reference */
   145         -static void whereClauseClear(WhereClause*);
   146         -
   147         -/*
   148         -** Deallocate all memory associated with a WhereOrInfo object.
   149         -*/
   150         -static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
   151         -  whereClauseClear(&p->wc);
   152         -  sqlite3DbFree(db, p);
   153         -}
   154         -
   155         -/*
   156         -** Deallocate all memory associated with a WhereAndInfo object.
   157         -*/
   158         -static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
   159         -  whereClauseClear(&p->wc);
   160         -  sqlite3DbFree(db, p);
   161         -}
   162         -
   163         -/*
   164         -** Deallocate a WhereClause structure.  The WhereClause structure
   165         -** itself is not freed.  This routine is the inverse of whereClauseInit().
   166         -*/
   167         -static void whereClauseClear(WhereClause *pWC){
   168         -  int i;
   169         -  WhereTerm *a;
   170         -  sqlite3 *db = pWC->pWInfo->pParse->db;
   171         -  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
   172         -    if( a->wtFlags & TERM_DYNAMIC ){
   173         -      sqlite3ExprDelete(db, a->pExpr);
   174         -    }
   175         -    if( a->wtFlags & TERM_ORINFO ){
   176         -      whereOrInfoDelete(db, a->u.pOrInfo);
   177         -    }else if( a->wtFlags & TERM_ANDINFO ){
   178         -      whereAndInfoDelete(db, a->u.pAndInfo);
   179         -    }
   180         -  }
   181         -  if( pWC->a!=pWC->aStatic ){
   182         -    sqlite3DbFree(db, pWC->a);
   183         -  }
   184         -}
   185         -
   186         -/*
   187         -** Add a single new WhereTerm entry to the WhereClause object pWC.
   188         -** The new WhereTerm object is constructed from Expr p and with wtFlags.
   189         -** The index in pWC->a[] of the new WhereTerm is returned on success.
   190         -** 0 is returned if the new WhereTerm could not be added due to a memory
   191         -** allocation error.  The memory allocation failure will be recorded in
   192         -** the db->mallocFailed flag so that higher-level functions can detect it.
   193         -**
   194         -** This routine will increase the size of the pWC->a[] array as necessary.
   195         -**
   196         -** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
   197         -** for freeing the expression p is assumed by the WhereClause object pWC.
   198         -** This is true even if this routine fails to allocate a new WhereTerm.
   199         -**
   200         -** WARNING:  This routine might reallocate the space used to store
   201         -** WhereTerms.  All pointers to WhereTerms should be invalidated after
   202         -** calling this routine.  Such pointers may be reinitialized by referencing
   203         -** the pWC->a[] array.
   204         -*/
   205         -static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
   206         -  WhereTerm *pTerm;
   207         -  int idx;
   208         -  testcase( wtFlags & TERM_VIRTUAL );
   209         -  if( pWC->nTerm>=pWC->nSlot ){
   210         -    WhereTerm *pOld = pWC->a;
   211         -    sqlite3 *db = pWC->pWInfo->pParse->db;
   212         -    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
   213         -    if( pWC->a==0 ){
   214         -      if( wtFlags & TERM_DYNAMIC ){
   215         -        sqlite3ExprDelete(db, p);
   216         -      }
   217         -      pWC->a = pOld;
   218         -      return 0;
   219         -    }
   220         -    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
   221         -    if( pOld!=pWC->aStatic ){
   222         -      sqlite3DbFree(db, pOld);
   223         -    }
   224         -    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
   225         -    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
   226         -  }
   227         -  pTerm = &pWC->a[idx = pWC->nTerm++];
   228         -  if( p && ExprHasProperty(p, EP_Unlikely) ){
   229         -    pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
   230         -  }else{
   231         -    pTerm->truthProb = 1;
   232         -  }
   233         -  pTerm->pExpr = sqlite3ExprSkipCollate(p);
   234         -  pTerm->wtFlags = wtFlags;
   235         -  pTerm->pWC = pWC;
   236         -  pTerm->iParent = -1;
   237         -  return idx;
   238         -}
   239         -
   240         -/*
   241         -** This routine identifies subexpressions in the WHERE clause where
   242         -** each subexpression is separated by the AND operator or some other
   243         -** operator specified in the op parameter.  The WhereClause structure
   244         -** is filled with pointers to subexpressions.  For example:
   245         -**
   246         -**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
   247         -**           \________/     \_______________/     \________________/
   248         -**            slot[0]            slot[1]               slot[2]
   249         -**
   250         -** The original WHERE clause in pExpr is unaltered.  All this routine
   251         -** does is make slot[] entries point to substructure within pExpr.
   252         -**
   253         -** In the previous sentence and in the diagram, "slot[]" refers to
   254         -** the WhereClause.a[] array.  The slot[] array grows as needed to contain
   255         -** all terms of the WHERE clause.
   256         -*/
   257         -static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
   258         -  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
   259         -  pWC->op = op;
   260         -  if( pE2==0 ) return;
   261         -  if( pE2->op!=op ){
   262         -    whereClauseInsert(pWC, pExpr, 0);
   263         -  }else{
   264         -    whereSplit(pWC, pE2->pLeft, op);
   265         -    whereSplit(pWC, pE2->pRight, op);
   266         -  }
   267         -}
   268         -
   269         -/*
   270         -** Initialize a WhereMaskSet object
   271         -*/
   272         -#define initMaskSet(P)  (P)->n=0
   273         -
   274    139   /*
   275    140   ** Return the bitmask for the given cursor number.  Return 0 if
   276    141   ** iCursor is not in the set.
   277    142   */
   278         -static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
          143  +Bitmask sqlite3WhereGetMask(WhereMaskSet *pMaskSet, int iCursor){
   279    144     int i;
   280    145     assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
   281    146     for(i=0; i<pMaskSet->n; i++){
   282    147       if( pMaskSet->ix[i]==iCursor ){
   283    148         return MASKBIT(i);
   284    149       }
   285    150     }
................................................................................
   295    160   ** array will never overflow.
   296    161   */
   297    162   static void createMask(WhereMaskSet *pMaskSet, int iCursor){
   298    163     assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
   299    164     pMaskSet->ix[pMaskSet->n++] = iCursor;
   300    165   }
   301    166   
   302         -/*
   303         -** These routines walk (recursively) an expression tree and generate
   304         -** a bitmask indicating which tables are used in that expression
   305         -** tree.
   306         -*/
   307         -static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
   308         -static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
   309         -static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
   310         -  Bitmask mask = 0;
   311         -  if( p==0 ) return 0;
   312         -  if( p->op==TK_COLUMN ){
   313         -    mask = getMask(pMaskSet, p->iTable);
   314         -    return mask;
   315         -  }
   316         -  mask = exprTableUsage(pMaskSet, p->pRight);
   317         -  mask |= exprTableUsage(pMaskSet, p->pLeft);
   318         -  if( ExprHasProperty(p, EP_xIsSelect) ){
   319         -    mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
   320         -  }else{
   321         -    mask |= exprListTableUsage(pMaskSet, p->x.pList);
   322         -  }
   323         -  return mask;
   324         -}
   325         -static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
   326         -  int i;
   327         -  Bitmask mask = 0;
   328         -  if( pList ){
   329         -    for(i=0; i<pList->nExpr; i++){
   330         -      mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
   331         -    }
   332         -  }
   333         -  return mask;
   334         -}
   335         -static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
   336         -  Bitmask mask = 0;
   337         -  while( pS ){
   338         -    SrcList *pSrc = pS->pSrc;
   339         -    mask |= exprListTableUsage(pMaskSet, pS->pEList);
   340         -    mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
   341         -    mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
   342         -    mask |= exprTableUsage(pMaskSet, pS->pWhere);
   343         -    mask |= exprTableUsage(pMaskSet, pS->pHaving);
   344         -    if( ALWAYS(pSrc!=0) ){
   345         -      int i;
   346         -      for(i=0; i<pSrc->nSrc; i++){
   347         -        mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
   348         -        mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
   349         -      }
   350         -    }
   351         -    pS = pS->pPrior;
   352         -  }
   353         -  return mask;
   354         -}
   355         -
   356         -/*
   357         -** Return TRUE if the given operator is one of the operators that is
   358         -** allowed for an indexable WHERE clause term.  The allowed operators are
   359         -** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
   360         -*/
   361         -static int allowedOp(int op){
   362         -  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
   363         -  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
   364         -  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
   365         -  assert( TK_GE==TK_EQ+4 );
   366         -  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
   367         -}
   368         -
   369         -/*
   370         -** Commute a comparison operator.  Expressions of the form "X op Y"
   371         -** are converted into "Y op X".
   372         -**
   373         -** If left/right precedence rules come into play when determining the
   374         -** collating sequence, then COLLATE operators are adjusted to ensure
   375         -** that the collating sequence does not change.  For example:
   376         -** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
   377         -** the left hand side of a comparison overrides any collation sequence 
   378         -** attached to the right. For the same reason the EP_Collate flag
   379         -** is not commuted.
   380         -*/
   381         -static void exprCommute(Parse *pParse, Expr *pExpr){
   382         -  u16 expRight = (pExpr->pRight->flags & EP_Collate);
   383         -  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
   384         -  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
   385         -  if( expRight==expLeft ){
   386         -    /* Either X and Y both have COLLATE operator or neither do */
   387         -    if( expRight ){
   388         -      /* Both X and Y have COLLATE operators.  Make sure X is always
   389         -      ** used by clearing the EP_Collate flag from Y. */
   390         -      pExpr->pRight->flags &= ~EP_Collate;
   391         -    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
   392         -      /* Neither X nor Y have COLLATE operators, but X has a non-default
   393         -      ** collating sequence.  So add the EP_Collate marker on X to cause
   394         -      ** it to be searched first. */
   395         -      pExpr->pLeft->flags |= EP_Collate;
   396         -    }
   397         -  }
   398         -  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
   399         -  if( pExpr->op>=TK_GT ){
   400         -    assert( TK_LT==TK_GT+2 );
   401         -    assert( TK_GE==TK_LE+2 );
   402         -    assert( TK_GT>TK_EQ );
   403         -    assert( TK_GT<TK_LE );
   404         -    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
   405         -    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
   406         -  }
   407         -}
   408         -
   409         -/*
   410         -** Translate from TK_xx operator to WO_xx bitmask.
   411         -*/
   412         -static u16 operatorMask(int op){
   413         -  u16 c;
   414         -  assert( allowedOp(op) );
   415         -  if( op==TK_IN ){
   416         -    c = WO_IN;
   417         -  }else if( op==TK_ISNULL ){
   418         -    c = WO_ISNULL;
   419         -  }else if( op==TK_IS ){
   420         -    c = WO_IS;
   421         -  }else{
   422         -    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
   423         -    c = (u16)(WO_EQ<<(op-TK_EQ));
   424         -  }
   425         -  assert( op!=TK_ISNULL || c==WO_ISNULL );
   426         -  assert( op!=TK_IN || c==WO_IN );
   427         -  assert( op!=TK_EQ || c==WO_EQ );
   428         -  assert( op!=TK_LT || c==WO_LT );
   429         -  assert( op!=TK_LE || c==WO_LE );
   430         -  assert( op!=TK_GT || c==WO_GT );
   431         -  assert( op!=TK_GE || c==WO_GE );
   432         -  assert( op!=TK_IS || c==WO_IS );
   433         -  return c;
   434         -}
   435         -
   436    167   /*
   437    168   ** Advance to the next WhereTerm that matches according to the criteria
   438    169   ** established when the pScan object was initialized by whereScanInit().
   439    170   ** Return NULL if there are no more matching WhereTerms.
   440    171   */
   441    172   static WhereTerm *whereScanNext(WhereScan *pScan){
   442    173     int iCur;            /* The cursor on the LHS of the term */
................................................................................
   580    311   ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
   581    312   ** then try for the one with no dependencies on <expr> - in other words where
   582    313   ** <expr> is a constant expression of some kind.  Only return entries of
   583    314   ** the form "X <op> Y" where Y is a column in another table if no terms of
   584    315   ** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
   585    316   ** exist, try to return a term that does not use WO_EQUIV.
   586    317   */
   587         -static WhereTerm *findTerm(
          318  +WhereTerm *sqlite3WhereFindTerm(
   588    319     WhereClause *pWC,     /* The WHERE clause to be searched */
   589    320     int iCur,             /* Cursor number of LHS */
   590    321     int iColumn,          /* Column number of LHS */
   591    322     Bitmask notReady,     /* RHS must not overlap with this mask */
   592    323     u32 op,               /* Mask of WO_xx values describing operator */
   593    324     Index *pIdx           /* Must be compatible with this index, if not NULL */
   594    325   ){
................................................................................
   607    338         if( pResult==0 ) pResult = p;
   608    339       }
   609    340       p = whereScanNext(&scan);
   610    341     }
   611    342     return pResult;
   612    343   }
   613    344   
   614         -/* Forward reference */
   615         -static void exprAnalyze(SrcList*, WhereClause*, int);
   616         -
   617         -/*
   618         -** Call exprAnalyze on all terms in a WHERE clause.  
   619         -*/
   620         -static void exprAnalyzeAll(
   621         -  SrcList *pTabList,       /* the FROM clause */
   622         -  WhereClause *pWC         /* the WHERE clause to be analyzed */
   623         -){
   624         -  int i;
   625         -  for(i=pWC->nTerm-1; i>=0; i--){
   626         -    exprAnalyze(pTabList, pWC, i);
   627         -  }
   628         -}
   629         -
   630         -#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
   631         -/*
   632         -** Check to see if the given expression is a LIKE or GLOB operator that
   633         -** can be optimized using inequality constraints.  Return TRUE if it is
   634         -** so and false if not.
   635         -**
   636         -** In order for the operator to be optimizible, the RHS must be a string
   637         -** literal that does not begin with a wildcard.  The LHS must be a column
   638         -** that may only be NULL, a string, or a BLOB, never a number. (This means
   639         -** that virtual tables cannot participate in the LIKE optimization.)  The
   640         -** collating sequence for the column on the LHS must be appropriate for
   641         -** the operator.
   642         -*/
   643         -static int isLikeOrGlob(
   644         -  Parse *pParse,    /* Parsing and code generating context */
   645         -  Expr *pExpr,      /* Test this expression */
   646         -  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
   647         -  int *pisComplete, /* True if the only wildcard is % in the last character */
   648         -  int *pnoCase      /* True if uppercase is equivalent to lowercase */
   649         -){
   650         -  const char *z = 0;         /* String on RHS of LIKE operator */
   651         -  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
   652         -  ExprList *pList;           /* List of operands to the LIKE operator */
   653         -  int c;                     /* One character in z[] */
   654         -  int cnt;                   /* Number of non-wildcard prefix characters */
   655         -  char wc[3];                /* Wildcard characters */
   656         -  sqlite3 *db = pParse->db;  /* Database connection */
   657         -  sqlite3_value *pVal = 0;
   658         -  int op;                    /* Opcode of pRight */
   659         -
   660         -  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
   661         -    return 0;
   662         -  }
   663         -#ifdef SQLITE_EBCDIC
   664         -  if( *pnoCase ) return 0;
   665         -#endif
   666         -  pList = pExpr->x.pList;
   667         -  pLeft = pList->a[1].pExpr;
   668         -  if( pLeft->op!=TK_COLUMN 
   669         -   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
   670         -   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
   671         -  ){
   672         -    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
   673         -    ** be the name of an indexed column with TEXT affinity. */
   674         -    return 0;
   675         -  }
   676         -  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
   677         -
   678         -  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
   679         -  op = pRight->op;
   680         -  if( op==TK_VARIABLE ){
   681         -    Vdbe *pReprepare = pParse->pReprepare;
   682         -    int iCol = pRight->iColumn;
   683         -    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
   684         -    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
   685         -      z = (char *)sqlite3_value_text(pVal);
   686         -    }
   687         -    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
   688         -    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
   689         -  }else if( op==TK_STRING ){
   690         -    z = pRight->u.zToken;
   691         -  }
   692         -  if( z ){
   693         -    cnt = 0;
   694         -    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
   695         -      cnt++;
   696         -    }
   697         -    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
   698         -      Expr *pPrefix;
   699         -      *pisComplete = c==wc[0] && z[cnt+1]==0;
   700         -      pPrefix = sqlite3Expr(db, TK_STRING, z);
   701         -      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
   702         -      *ppPrefix = pPrefix;
   703         -      if( op==TK_VARIABLE ){
   704         -        Vdbe *v = pParse->pVdbe;
   705         -        sqlite3VdbeSetVarmask(v, pRight->iColumn);
   706         -        if( *pisComplete && pRight->u.zToken[1] ){
   707         -          /* If the rhs of the LIKE expression is a variable, and the current
   708         -          ** value of the variable means there is no need to invoke the LIKE
   709         -          ** function, then no OP_Variable will be added to the program.
   710         -          ** This causes problems for the sqlite3_bind_parameter_name()
   711         -          ** API. To work around them, add a dummy OP_Variable here.
   712         -          */ 
   713         -          int r1 = sqlite3GetTempReg(pParse);
   714         -          sqlite3ExprCodeTarget(pParse, pRight, r1);
   715         -          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
   716         -          sqlite3ReleaseTempReg(pParse, r1);
   717         -        }
   718         -      }
   719         -    }else{
   720         -      z = 0;
   721         -    }
   722         -  }
   723         -
   724         -  sqlite3ValueFree(pVal);
   725         -  return (z!=0);
   726         -}
   727         -#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
   728         -
   729         -
   730         -#ifndef SQLITE_OMIT_VIRTUALTABLE
   731         -/*
   732         -** Check to see if the given expression is of the form
   733         -**
   734         -**         column MATCH expr
   735         -**
   736         -** If it is then return TRUE.  If not, return FALSE.
   737         -*/
   738         -static int isMatchOfColumn(
   739         -  Expr *pExpr      /* Test this expression */
   740         -){
   741         -  ExprList *pList;
   742         -
   743         -  if( pExpr->op!=TK_FUNCTION ){
   744         -    return 0;
   745         -  }
   746         -  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
   747         -    return 0;
   748         -  }
   749         -  pList = pExpr->x.pList;
   750         -  if( pList->nExpr!=2 ){
   751         -    return 0;
   752         -  }
   753         -  if( pList->a[1].pExpr->op != TK_COLUMN ){
   754         -    return 0;
   755         -  }
   756         -  return 1;
   757         -}
   758         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
   759         -
   760         -/*
   761         -** If the pBase expression originated in the ON or USING clause of
   762         -** a join, then transfer the appropriate markings over to derived.
   763         -*/
   764         -static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
   765         -  if( pDerived ){
   766         -    pDerived->flags |= pBase->flags & EP_FromJoin;
   767         -    pDerived->iRightJoinTable = pBase->iRightJoinTable;
   768         -  }
   769         -}
   770         -
   771         -/*
   772         -** Mark term iChild as being a child of term iParent
   773         -*/
   774         -static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
   775         -  pWC->a[iChild].iParent = iParent;
   776         -  pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
   777         -  pWC->a[iParent].nChild++;
   778         -}
   779         -
   780         -/*
   781         -** Return the N-th AND-connected subterm of pTerm.  Or if pTerm is not
   782         -** a conjunction, then return just pTerm when N==0.  If N is exceeds
   783         -** the number of available subterms, return NULL.
   784         -*/
   785         -static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
   786         -  if( pTerm->eOperator!=WO_AND ){
   787         -    return N==0 ? pTerm : 0;
   788         -  }
   789         -  if( N<pTerm->u.pAndInfo->wc.nTerm ){
   790         -    return &pTerm->u.pAndInfo->wc.a[N];
   791         -  }
   792         -  return 0;
   793         -}
   794         -
   795         -/*
   796         -** Subterms pOne and pTwo are contained within WHERE clause pWC.  The
   797         -** two subterms are in disjunction - they are OR-ed together.
   798         -**
   799         -** If these two terms are both of the form:  "A op B" with the same
   800         -** A and B values but different operators and if the operators are
   801         -** compatible (if one is = and the other is <, for example) then
   802         -** add a new virtual AND term to pWC that is the combination of the
   803         -** two.
   804         -**
   805         -** Some examples:
   806         -**
   807         -**    x<y OR x=y    -->     x<=y
   808         -**    x=y OR x=y    -->     x=y
   809         -**    x<=y OR x<y   -->     x<=y
   810         -**
   811         -** The following is NOT generated:
   812         -**
   813         -**    x<y OR x>y    -->     x!=y     
   814         -*/
   815         -static void whereCombineDisjuncts(
   816         -  SrcList *pSrc,         /* the FROM clause */
   817         -  WhereClause *pWC,      /* The complete WHERE clause */
   818         -  WhereTerm *pOne,       /* First disjunct */
   819         -  WhereTerm *pTwo        /* Second disjunct */
   820         -){
   821         -  u16 eOp = pOne->eOperator | pTwo->eOperator;
   822         -  sqlite3 *db;           /* Database connection (for malloc) */
   823         -  Expr *pNew;            /* New virtual expression */
   824         -  int op;                /* Operator for the combined expression */
   825         -  int idxNew;            /* Index in pWC of the next virtual term */
   826         -
   827         -  if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
   828         -  if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
   829         -  if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
   830         -   && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
   831         -  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
   832         -  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
   833         -  if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
   834         -  if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
   835         -  /* If we reach this point, it means the two subterms can be combined */
   836         -  if( (eOp & (eOp-1))!=0 ){
   837         -    if( eOp & (WO_LT|WO_LE) ){
   838         -      eOp = WO_LE;
   839         -    }else{
   840         -      assert( eOp & (WO_GT|WO_GE) );
   841         -      eOp = WO_GE;
   842         -    }
   843         -  }
   844         -  db = pWC->pWInfo->pParse->db;
   845         -  pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
   846         -  if( pNew==0 ) return;
   847         -  for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
   848         -  pNew->op = op;
   849         -  idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
   850         -  exprAnalyze(pSrc, pWC, idxNew);
   851         -}
   852         -
   853         -#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
   854         -/*
   855         -** Analyze a term that consists of two or more OR-connected
   856         -** subterms.  So in:
   857         -**
   858         -**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
   859         -**                          ^^^^^^^^^^^^^^^^^^^^
   860         -**
   861         -** This routine analyzes terms such as the middle term in the above example.
   862         -** A WhereOrTerm object is computed and attached to the term under
   863         -** analysis, regardless of the outcome of the analysis.  Hence:
   864         -**
   865         -**     WhereTerm.wtFlags   |=  TERM_ORINFO
   866         -**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
   867         -**
   868         -** The term being analyzed must have two or more of OR-connected subterms.
   869         -** A single subterm might be a set of AND-connected sub-subterms.
   870         -** Examples of terms under analysis:
   871         -**
   872         -**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
   873         -**     (B)     x=expr1 OR expr2=x OR x=expr3
   874         -**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
   875         -**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
   876         -**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
   877         -**     (F)     x>A OR (x=A AND y>=B)
   878         -**
   879         -** CASE 1:
   880         -**
   881         -** If all subterms are of the form T.C=expr for some single column of C and
   882         -** a single table T (as shown in example B above) then create a new virtual
   883         -** term that is an equivalent IN expression.  In other words, if the term
   884         -** being analyzed is:
   885         -**
   886         -**      x = expr1  OR  expr2 = x  OR  x = expr3
   887         -**
   888         -** then create a new virtual term like this:
   889         -**
   890         -**      x IN (expr1,expr2,expr3)
   891         -**
   892         -** CASE 2:
   893         -**
   894         -** If there are exactly two disjuncts one side has x>A and the other side
   895         -** has x=A (for the same x and A) then add a new virtual conjunct term to the
   896         -** WHERE clause of the form "x>=A".  Example:
   897         -**
   898         -**      x>A OR (x=A AND y>B)    adds:    x>=A
   899         -**
   900         -** The added conjunct can sometimes be helpful in query planning.
   901         -**
   902         -** CASE 3:
   903         -**
   904         -** If all subterms are indexable by a single table T, then set
   905         -**
   906         -**     WhereTerm.eOperator              =  WO_OR
   907         -**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
   908         -**
   909         -** A subterm is "indexable" if it is of the form
   910         -** "T.C <op> <expr>" where C is any column of table T and 
   911         -** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
   912         -** A subterm is also indexable if it is an AND of two or more
   913         -** subsubterms at least one of which is indexable.  Indexable AND 
   914         -** subterms have their eOperator set to WO_AND and they have
   915         -** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
   916         -**
   917         -** From another point of view, "indexable" means that the subterm could
   918         -** potentially be used with an index if an appropriate index exists.
   919         -** This analysis does not consider whether or not the index exists; that
   920         -** is decided elsewhere.  This analysis only looks at whether subterms
   921         -** appropriate for indexing exist.
   922         -**
   923         -** All examples A through E above satisfy case 2.  But if a term
   924         -** also satisfies case 1 (such as B) we know that the optimizer will
   925         -** always prefer case 1, so in that case we pretend that case 2 is not
   926         -** satisfied.
   927         -**
   928         -** It might be the case that multiple tables are indexable.  For example,
   929         -** (E) above is indexable on tables P, Q, and R.
   930         -**
   931         -** Terms that satisfy case 2 are candidates for lookup by using
   932         -** separate indices to find rowids for each subterm and composing
   933         -** the union of all rowids using a RowSet object.  This is similar
   934         -** to "bitmap indices" in other database engines.
   935         -**
   936         -** OTHERWISE:
   937         -**
   938         -** If neither case 1 nor case 2 apply, then leave the eOperator set to
   939         -** zero.  This term is not useful for search.
   940         -*/
   941         -static void exprAnalyzeOrTerm(
   942         -  SrcList *pSrc,            /* the FROM clause */
   943         -  WhereClause *pWC,         /* the complete WHERE clause */
   944         -  int idxTerm               /* Index of the OR-term to be analyzed */
   945         -){
   946         -  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
   947         -  Parse *pParse = pWInfo->pParse;         /* Parser context */
   948         -  sqlite3 *db = pParse->db;               /* Database connection */
   949         -  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
   950         -  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
   951         -  int i;                                  /* Loop counters */
   952         -  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
   953         -  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
   954         -  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
   955         -  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
   956         -  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
   957         -
   958         -  /*
   959         -  ** Break the OR clause into its separate subterms.  The subterms are
   960         -  ** stored in a WhereClause structure containing within the WhereOrInfo
   961         -  ** object that is attached to the original OR clause term.
   962         -  */
   963         -  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
   964         -  assert( pExpr->op==TK_OR );
   965         -  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
   966         -  if( pOrInfo==0 ) return;
   967         -  pTerm->wtFlags |= TERM_ORINFO;
   968         -  pOrWc = &pOrInfo->wc;
   969         -  whereClauseInit(pOrWc, pWInfo);
   970         -  whereSplit(pOrWc, pExpr, TK_OR);
   971         -  exprAnalyzeAll(pSrc, pOrWc);
   972         -  if( db->mallocFailed ) return;
   973         -  assert( pOrWc->nTerm>=2 );
   974         -
   975         -  /*
   976         -  ** Compute the set of tables that might satisfy cases 1 or 2.
   977         -  */
   978         -  indexable = ~(Bitmask)0;
   979         -  chngToIN = ~(Bitmask)0;
   980         -  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
   981         -    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
   982         -      WhereAndInfo *pAndInfo;
   983         -      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
   984         -      chngToIN = 0;
   985         -      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
   986         -      if( pAndInfo ){
   987         -        WhereClause *pAndWC;
   988         -        WhereTerm *pAndTerm;
   989         -        int j;
   990         -        Bitmask b = 0;
   991         -        pOrTerm->u.pAndInfo = pAndInfo;
   992         -        pOrTerm->wtFlags |= TERM_ANDINFO;
   993         -        pOrTerm->eOperator = WO_AND;
   994         -        pAndWC = &pAndInfo->wc;
   995         -        whereClauseInit(pAndWC, pWC->pWInfo);
   996         -        whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
   997         -        exprAnalyzeAll(pSrc, pAndWC);
   998         -        pAndWC->pOuter = pWC;
   999         -        testcase( db->mallocFailed );
  1000         -        if( !db->mallocFailed ){
  1001         -          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
  1002         -            assert( pAndTerm->pExpr );
  1003         -            if( allowedOp(pAndTerm->pExpr->op) ){
  1004         -              b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
  1005         -            }
  1006         -          }
  1007         -        }
  1008         -        indexable &= b;
  1009         -      }
  1010         -    }else if( pOrTerm->wtFlags & TERM_COPIED ){
  1011         -      /* Skip this term for now.  We revisit it when we process the
  1012         -      ** corresponding TERM_VIRTUAL term */
  1013         -    }else{
  1014         -      Bitmask b;
  1015         -      b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
  1016         -      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
  1017         -        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
  1018         -        b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
  1019         -      }
  1020         -      indexable &= b;
  1021         -      if( (pOrTerm->eOperator & WO_EQ)==0 ){
  1022         -        chngToIN = 0;
  1023         -      }else{
  1024         -        chngToIN &= b;
  1025         -      }
  1026         -    }
  1027         -  }
  1028         -
  1029         -  /*
  1030         -  ** Record the set of tables that satisfy case 3.  The set might be
  1031         -  ** empty.
  1032         -  */
  1033         -  pOrInfo->indexable = indexable;
  1034         -  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
  1035         -
  1036         -  /* For a two-way OR, attempt to implementation case 2.
  1037         -  */
  1038         -  if( indexable && pOrWc->nTerm==2 ){
  1039         -    int iOne = 0;
  1040         -    WhereTerm *pOne;
  1041         -    while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
  1042         -      int iTwo = 0;
  1043         -      WhereTerm *pTwo;
  1044         -      while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
  1045         -        whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
  1046         -      }
  1047         -    }
  1048         -  }
  1049         -
  1050         -  /*
  1051         -  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
  1052         -  ** we have to do some additional checking to see if case 1 really
  1053         -  ** is satisfied.
  1054         -  **
  1055         -  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
  1056         -  ** that there is no possibility of transforming the OR clause into an
  1057         -  ** IN operator because one or more terms in the OR clause contain
  1058         -  ** something other than == on a column in the single table.  The 1-bit
  1059         -  ** case means that every term of the OR clause is of the form
  1060         -  ** "table.column=expr" for some single table.  The one bit that is set
  1061         -  ** will correspond to the common table.  We still need to check to make
  1062         -  ** sure the same column is used on all terms.  The 2-bit case is when
  1063         -  ** the all terms are of the form "table1.column=table2.column".  It
  1064         -  ** might be possible to form an IN operator with either table1.column
  1065         -  ** or table2.column as the LHS if either is common to every term of
  1066         -  ** the OR clause.
  1067         -  **
  1068         -  ** Note that terms of the form "table.column1=table.column2" (the
  1069         -  ** same table on both sizes of the ==) cannot be optimized.
  1070         -  */
  1071         -  if( chngToIN ){
  1072         -    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
  1073         -    int iColumn = -1;         /* Column index on lhs of IN operator */
  1074         -    int iCursor = -1;         /* Table cursor common to all terms */
  1075         -    int j = 0;                /* Loop counter */
  1076         -
  1077         -    /* Search for a table and column that appears on one side or the
  1078         -    ** other of the == operator in every subterm.  That table and column
  1079         -    ** will be recorded in iCursor and iColumn.  There might not be any
  1080         -    ** such table and column.  Set okToChngToIN if an appropriate table
  1081         -    ** and column is found but leave okToChngToIN false if not found.
  1082         -    */
  1083         -    for(j=0; j<2 && !okToChngToIN; j++){
  1084         -      pOrTerm = pOrWc->a;
  1085         -      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
  1086         -        assert( pOrTerm->eOperator & WO_EQ );
  1087         -        pOrTerm->wtFlags &= ~TERM_OR_OK;
  1088         -        if( pOrTerm->leftCursor==iCursor ){
  1089         -          /* This is the 2-bit case and we are on the second iteration and
  1090         -          ** current term is from the first iteration.  So skip this term. */
  1091         -          assert( j==1 );
  1092         -          continue;
  1093         -        }
  1094         -        if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
  1095         -          /* This term must be of the form t1.a==t2.b where t2 is in the
  1096         -          ** chngToIN set but t1 is not.  This term will be either preceded
  1097         -          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
  1098         -          ** and use its inversion. */
  1099         -          testcase( pOrTerm->wtFlags & TERM_COPIED );
  1100         -          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
  1101         -          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
  1102         -          continue;
  1103         -        }
  1104         -        iColumn = pOrTerm->u.leftColumn;
  1105         -        iCursor = pOrTerm->leftCursor;
  1106         -        break;
  1107         -      }
  1108         -      if( i<0 ){
  1109         -        /* No candidate table+column was found.  This can only occur
  1110         -        ** on the second iteration */
  1111         -        assert( j==1 );
  1112         -        assert( IsPowerOfTwo(chngToIN) );
  1113         -        assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
  1114         -        break;
  1115         -      }
  1116         -      testcase( j==1 );
  1117         -
  1118         -      /* We have found a candidate table and column.  Check to see if that
  1119         -      ** table and column is common to every term in the OR clause */
  1120         -      okToChngToIN = 1;
  1121         -      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
  1122         -        assert( pOrTerm->eOperator & WO_EQ );
  1123         -        if( pOrTerm->leftCursor!=iCursor ){
  1124         -          pOrTerm->wtFlags &= ~TERM_OR_OK;
  1125         -        }else if( pOrTerm->u.leftColumn!=iColumn ){
  1126         -          okToChngToIN = 0;
  1127         -        }else{
  1128         -          int affLeft, affRight;
  1129         -          /* If the right-hand side is also a column, then the affinities
  1130         -          ** of both right and left sides must be such that no type
  1131         -          ** conversions are required on the right.  (Ticket #2249)
  1132         -          */
  1133         -          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
  1134         -          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
  1135         -          if( affRight!=0 && affRight!=affLeft ){
  1136         -            okToChngToIN = 0;
  1137         -          }else{
  1138         -            pOrTerm->wtFlags |= TERM_OR_OK;
  1139         -          }
  1140         -        }
  1141         -      }
  1142         -    }
  1143         -
  1144         -    /* At this point, okToChngToIN is true if original pTerm satisfies
  1145         -    ** case 1.  In that case, construct a new virtual term that is 
  1146         -    ** pTerm converted into an IN operator.
  1147         -    */
  1148         -    if( okToChngToIN ){
  1149         -      Expr *pDup;            /* A transient duplicate expression */
  1150         -      ExprList *pList = 0;   /* The RHS of the IN operator */
  1151         -      Expr *pLeft = 0;       /* The LHS of the IN operator */
  1152         -      Expr *pNew;            /* The complete IN operator */
  1153         -
  1154         -      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
  1155         -        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
  1156         -        assert( pOrTerm->eOperator & WO_EQ );
  1157         -        assert( pOrTerm->leftCursor==iCursor );
  1158         -        assert( pOrTerm->u.leftColumn==iColumn );
  1159         -        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
  1160         -        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
  1161         -        pLeft = pOrTerm->pExpr->pLeft;
  1162         -      }
  1163         -      assert( pLeft!=0 );
  1164         -      pDup = sqlite3ExprDup(db, pLeft, 0);
  1165         -      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
  1166         -      if( pNew ){
  1167         -        int idxNew;
  1168         -        transferJoinMarkings(pNew, pExpr);
  1169         -        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
  1170         -        pNew->x.pList = pList;
  1171         -        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
  1172         -        testcase( idxNew==0 );
  1173         -        exprAnalyze(pSrc, pWC, idxNew);
  1174         -        pTerm = &pWC->a[idxTerm];
  1175         -        markTermAsChild(pWC, idxNew, idxTerm);
  1176         -      }else{
  1177         -        sqlite3ExprListDelete(db, pList);
  1178         -      }
  1179         -      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
  1180         -    }
  1181         -  }
  1182         -}
  1183         -#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
  1184         -
  1185         -/*
  1186         -** We already know that pExpr is a binary operator where both operands are
  1187         -** column references.  This routine checks to see if pExpr is an equivalence
  1188         -** relation:
  1189         -**   1.  The SQLITE_Transitive optimization must be enabled
  1190         -**   2.  Must be either an == or an IS operator
  1191         -**   3.  Not originating the ON clause of an OUTER JOIN
  1192         -**   4.  The affinities of A and B must be compatible
  1193         -**   5a. Both operands use the same collating sequence OR
  1194         -**   5b. The overall collating sequence is BINARY
  1195         -** If this routine returns TRUE, that means that the RHS can be substituted
  1196         -** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
  1197         -** This is an optimization.  No harm comes from returning 0.  But if 1 is
  1198         -** returned when it should not be, then incorrect answers might result.
  1199         -*/
  1200         -static int termIsEquivalence(Parse *pParse, Expr *pExpr){
  1201         -  char aff1, aff2;
  1202         -  CollSeq *pColl;
  1203         -  const char *zColl1, *zColl2;
  1204         -  if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
  1205         -  if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
  1206         -  if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
  1207         -  aff1 = sqlite3ExprAffinity(pExpr->pLeft);
  1208         -  aff2 = sqlite3ExprAffinity(pExpr->pRight);
  1209         -  if( aff1!=aff2
  1210         -   && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
  1211         -  ){
  1212         -    return 0;
  1213         -  }
  1214         -  pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
  1215         -  if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
  1216         -  pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  1217         -  /* Since pLeft and pRight are both a column references, their collating
  1218         -  ** sequence should always be defined. */
  1219         -  zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
  1220         -  pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
  1221         -  zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
  1222         -  return sqlite3StrICmp(zColl1, zColl2)==0;
  1223         -}
  1224         -
  1225         -/*
  1226         -** The input to this routine is an WhereTerm structure with only the
  1227         -** "pExpr" field filled in.  The job of this routine is to analyze the
  1228         -** subexpression and populate all the other fields of the WhereTerm
  1229         -** structure.
  1230         -**
  1231         -** If the expression is of the form "<expr> <op> X" it gets commuted
  1232         -** to the standard form of "X <op> <expr>".
  1233         -**
  1234         -** If the expression is of the form "X <op> Y" where both X and Y are
  1235         -** columns, then the original expression is unchanged and a new virtual
  1236         -** term of the form "Y <op> X" is added to the WHERE clause and
  1237         -** analyzed separately.  The original term is marked with TERM_COPIED
  1238         -** and the new term is marked with TERM_DYNAMIC (because it's pExpr
  1239         -** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
  1240         -** is a commuted copy of a prior term.)  The original term has nChild=1
  1241         -** and the copy has idxParent set to the index of the original term.
  1242         -*/
  1243         -static void exprAnalyze(
  1244         -  SrcList *pSrc,            /* the FROM clause */
  1245         -  WhereClause *pWC,         /* the WHERE clause */
  1246         -  int idxTerm               /* Index of the term to be analyzed */
  1247         -){
  1248         -  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
  1249         -  WhereTerm *pTerm;                /* The term to be analyzed */
  1250         -  WhereMaskSet *pMaskSet;          /* Set of table index masks */
  1251         -  Expr *pExpr;                     /* The expression to be analyzed */
  1252         -  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
  1253         -  Bitmask prereqAll;               /* Prerequesites of pExpr */
  1254         -  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
  1255         -  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
  1256         -  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
  1257         -  int noCase = 0;                  /* uppercase equivalent to lowercase */
  1258         -  int op;                          /* Top-level operator.  pExpr->op */
  1259         -  Parse *pParse = pWInfo->pParse;  /* Parsing context */
  1260         -  sqlite3 *db = pParse->db;        /* Database connection */
  1261         -
  1262         -  if( db->mallocFailed ){
  1263         -    return;
  1264         -  }
  1265         -  pTerm = &pWC->a[idxTerm];
  1266         -  pMaskSet = &pWInfo->sMaskSet;
  1267         -  pExpr = pTerm->pExpr;
  1268         -  assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
  1269         -  prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
  1270         -  op = pExpr->op;
  1271         -  if( op==TK_IN ){
  1272         -    assert( pExpr->pRight==0 );
  1273         -    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1274         -      pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
  1275         -    }else{
  1276         -      pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
  1277         -    }
  1278         -  }else if( op==TK_ISNULL ){
  1279         -    pTerm->prereqRight = 0;
  1280         -  }else{
  1281         -    pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
  1282         -  }
  1283         -  prereqAll = exprTableUsage(pMaskSet, pExpr);
  1284         -  if( ExprHasProperty(pExpr, EP_FromJoin) ){
  1285         -    Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
  1286         -    prereqAll |= x;
  1287         -    extraRight = x-1;  /* ON clause terms may not be used with an index
  1288         -                       ** on left table of a LEFT JOIN.  Ticket #3015 */
  1289         -  }
  1290         -  pTerm->prereqAll = prereqAll;
  1291         -  pTerm->leftCursor = -1;
  1292         -  pTerm->iParent = -1;
  1293         -  pTerm->eOperator = 0;
  1294         -  if( allowedOp(op) ){
  1295         -    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
  1296         -    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
  1297         -    u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
  1298         -    if( pLeft->op==TK_COLUMN ){
  1299         -      pTerm->leftCursor = pLeft->iTable;
  1300         -      pTerm->u.leftColumn = pLeft->iColumn;
  1301         -      pTerm->eOperator = operatorMask(op) & opMask;
  1302         -    }
  1303         -    if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
  1304         -    if( pRight && pRight->op==TK_COLUMN ){
  1305         -      WhereTerm *pNew;
  1306         -      Expr *pDup;
  1307         -      u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
  1308         -      if( pTerm->leftCursor>=0 ){
  1309         -        int idxNew;
  1310         -        pDup = sqlite3ExprDup(db, pExpr, 0);
  1311         -        if( db->mallocFailed ){
  1312         -          sqlite3ExprDelete(db, pDup);
  1313         -          return;
  1314         -        }
  1315         -        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
  1316         -        if( idxNew==0 ) return;
  1317         -        pNew = &pWC->a[idxNew];
  1318         -        markTermAsChild(pWC, idxNew, idxTerm);
  1319         -        if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
  1320         -        pTerm = &pWC->a[idxTerm];
  1321         -        pTerm->wtFlags |= TERM_COPIED;
  1322         -
  1323         -        if( termIsEquivalence(pParse, pDup) ){
  1324         -          pTerm->eOperator |= WO_EQUIV;
  1325         -          eExtraOp = WO_EQUIV;
  1326         -        }
  1327         -      }else{
  1328         -        pDup = pExpr;
  1329         -        pNew = pTerm;
  1330         -      }
  1331         -      exprCommute(pParse, pDup);
  1332         -      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
  1333         -      pNew->leftCursor = pLeft->iTable;
  1334         -      pNew->u.leftColumn = pLeft->iColumn;
  1335         -      testcase( (prereqLeft | extraRight) != prereqLeft );
  1336         -      pNew->prereqRight = prereqLeft | extraRight;
  1337         -      pNew->prereqAll = prereqAll;
  1338         -      pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
  1339         -    }
  1340         -  }
  1341         -
  1342         -#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
  1343         -  /* If a term is the BETWEEN operator, create two new virtual terms
  1344         -  ** that define the range that the BETWEEN implements.  For example:
  1345         -  **
  1346         -  **      a BETWEEN b AND c
  1347         -  **
  1348         -  ** is converted into:
  1349         -  **
  1350         -  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
  1351         -  **
  1352         -  ** The two new terms are added onto the end of the WhereClause object.
  1353         -  ** The new terms are "dynamic" and are children of the original BETWEEN
  1354         -  ** term.  That means that if the BETWEEN term is coded, the children are
  1355         -  ** skipped.  Or, if the children are satisfied by an index, the original
  1356         -  ** BETWEEN term is skipped.
  1357         -  */
  1358         -  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
  1359         -    ExprList *pList = pExpr->x.pList;
  1360         -    int i;
  1361         -    static const u8 ops[] = {TK_GE, TK_LE};
  1362         -    assert( pList!=0 );
  1363         -    assert( pList->nExpr==2 );
  1364         -    for(i=0; i<2; i++){
  1365         -      Expr *pNewExpr;
  1366         -      int idxNew;
  1367         -      pNewExpr = sqlite3PExpr(pParse, ops[i], 
  1368         -                             sqlite3ExprDup(db, pExpr->pLeft, 0),
  1369         -                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
  1370         -      transferJoinMarkings(pNewExpr, pExpr);
  1371         -      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1372         -      testcase( idxNew==0 );
  1373         -      exprAnalyze(pSrc, pWC, idxNew);
  1374         -      pTerm = &pWC->a[idxTerm];
  1375         -      markTermAsChild(pWC, idxNew, idxTerm);
  1376         -    }
  1377         -  }
  1378         -#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
  1379         -
  1380         -#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
  1381         -  /* Analyze a term that is composed of two or more subterms connected by
  1382         -  ** an OR operator.
  1383         -  */
  1384         -  else if( pExpr->op==TK_OR ){
  1385         -    assert( pWC->op==TK_AND );
  1386         -    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
  1387         -    pTerm = &pWC->a[idxTerm];
  1388         -  }
  1389         -#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  1390         -
  1391         -#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
  1392         -  /* Add constraints to reduce the search space on a LIKE or GLOB
  1393         -  ** operator.
  1394         -  **
  1395         -  ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
  1396         -  **
  1397         -  **          x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
  1398         -  **
  1399         -  ** The last character of the prefix "abc" is incremented to form the
  1400         -  ** termination condition "abd".  If case is not significant (the default
  1401         -  ** for LIKE) then the lower-bound is made all uppercase and the upper-
  1402         -  ** bound is made all lowercase so that the bounds also work when comparing
  1403         -  ** BLOBs.
  1404         -  */
  1405         -  if( pWC->op==TK_AND 
  1406         -   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
  1407         -  ){
  1408         -    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
  1409         -    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
  1410         -    Expr *pNewExpr1;
  1411         -    Expr *pNewExpr2;
  1412         -    int idxNew1;
  1413         -    int idxNew2;
  1414         -    const char *zCollSeqName;     /* Name of collating sequence */
  1415         -    const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
  1416         -
  1417         -    pLeft = pExpr->x.pList->a[1].pExpr;
  1418         -    pStr2 = sqlite3ExprDup(db, pStr1, 0);
  1419         -
  1420         -    /* Convert the lower bound to upper-case and the upper bound to
  1421         -    ** lower-case (upper-case is less than lower-case in ASCII) so that
  1422         -    ** the range constraints also work for BLOBs
  1423         -    */
  1424         -    if( noCase && !pParse->db->mallocFailed ){
  1425         -      int i;
  1426         -      char c;
  1427         -      pTerm->wtFlags |= TERM_LIKE;
  1428         -      for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
  1429         -        pStr1->u.zToken[i] = sqlite3Toupper(c);
  1430         -        pStr2->u.zToken[i] = sqlite3Tolower(c);
  1431         -      }
  1432         -    }
  1433         -
  1434         -    if( !db->mallocFailed ){
  1435         -      u8 c, *pC;       /* Last character before the first wildcard */
  1436         -      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
  1437         -      c = *pC;
  1438         -      if( noCase ){
  1439         -        /* The point is to increment the last character before the first
  1440         -        ** wildcard.  But if we increment '@', that will push it into the
  1441         -        ** alphabetic range where case conversions will mess up the 
  1442         -        ** inequality.  To avoid this, make sure to also run the full
  1443         -        ** LIKE on all candidate expressions by clearing the isComplete flag
  1444         -        */
  1445         -        if( c=='A'-1 ) isComplete = 0;
  1446         -        c = sqlite3UpperToLower[c];
  1447         -      }
  1448         -      *pC = c + 1;
  1449         -    }
  1450         -    zCollSeqName = noCase ? "NOCASE" : "BINARY";
  1451         -    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
  1452         -    pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
  1453         -           sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
  1454         -           pStr1, 0);
  1455         -    transferJoinMarkings(pNewExpr1, pExpr);
  1456         -    idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
  1457         -    testcase( idxNew1==0 );
  1458         -    exprAnalyze(pSrc, pWC, idxNew1);
  1459         -    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
  1460         -    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
  1461         -           sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
  1462         -           pStr2, 0);
  1463         -    transferJoinMarkings(pNewExpr2, pExpr);
  1464         -    idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
  1465         -    testcase( idxNew2==0 );
  1466         -    exprAnalyze(pSrc, pWC, idxNew2);
  1467         -    pTerm = &pWC->a[idxTerm];
  1468         -    if( isComplete ){
  1469         -      markTermAsChild(pWC, idxNew1, idxTerm);
  1470         -      markTermAsChild(pWC, idxNew2, idxTerm);
  1471         -    }
  1472         -  }
  1473         -#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
  1474         -
  1475         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  1476         -  /* Add a WO_MATCH auxiliary term to the constraint set if the
  1477         -  ** current expression is of the form:  column MATCH expr.
  1478         -  ** This information is used by the xBestIndex methods of
  1479         -  ** virtual tables.  The native query optimizer does not attempt
  1480         -  ** to do anything with MATCH functions.
  1481         -  */
  1482         -  if( isMatchOfColumn(pExpr) ){
  1483         -    int idxNew;
  1484         -    Expr *pRight, *pLeft;
  1485         -    WhereTerm *pNewTerm;
  1486         -    Bitmask prereqColumn, prereqExpr;
  1487         -
  1488         -    pRight = pExpr->x.pList->a[0].pExpr;
  1489         -    pLeft = pExpr->x.pList->a[1].pExpr;
  1490         -    prereqExpr = exprTableUsage(pMaskSet, pRight);
  1491         -    prereqColumn = exprTableUsage(pMaskSet, pLeft);
  1492         -    if( (prereqExpr & prereqColumn)==0 ){
  1493         -      Expr *pNewExpr;
  1494         -      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
  1495         -                              0, sqlite3ExprDup(db, pRight, 0), 0);
  1496         -      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
  1497         -      testcase( idxNew==0 );
  1498         -      pNewTerm = &pWC->a[idxNew];
  1499         -      pNewTerm->prereqRight = prereqExpr;
  1500         -      pNewTerm->leftCursor = pLeft->iTable;
  1501         -      pNewTerm->u.leftColumn = pLeft->iColumn;
  1502         -      pNewTerm->eOperator = WO_MATCH;
  1503         -      markTermAsChild(pWC, idxNew, idxTerm);
  1504         -      pTerm = &pWC->a[idxTerm];
  1505         -      pTerm->wtFlags |= TERM_COPIED;
  1506         -      pNewTerm->prereqAll = pTerm->prereqAll;
  1507         -    }
  1508         -  }
  1509         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
  1510         -
  1511         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  1512         -  /* When sqlite_stat3 histogram data is available an operator of the
  1513         -  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
  1514         -  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
  1515         -  ** virtual term of that form.
  1516         -  **
  1517         -  ** Note that the virtual term must be tagged with TERM_VNULL.
  1518         -  */
  1519         -  if( pExpr->op==TK_NOTNULL
  1520         -   && pExpr->pLeft->op==TK_COLUMN
  1521         -   && pExpr->pLeft->iColumn>=0
  1522         -   && OptimizationEnabled(db, SQLITE_Stat34)
  1523         -  ){
  1524         -    Expr *pNewExpr;
  1525         -    Expr *pLeft = pExpr->pLeft;
  1526         -    int idxNew;
  1527         -    WhereTerm *pNewTerm;
  1528         -
  1529         -    pNewExpr = sqlite3PExpr(pParse, TK_GT,
  1530         -                            sqlite3ExprDup(db, pLeft, 0),
  1531         -                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
  1532         -
  1533         -    idxNew = whereClauseInsert(pWC, pNewExpr,
  1534         -                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
  1535         -    if( idxNew ){
  1536         -      pNewTerm = &pWC->a[idxNew];
  1537         -      pNewTerm->prereqRight = 0;
  1538         -      pNewTerm->leftCursor = pLeft->iTable;
  1539         -      pNewTerm->u.leftColumn = pLeft->iColumn;
  1540         -      pNewTerm->eOperator = WO_GT;
  1541         -      markTermAsChild(pWC, idxNew, idxTerm);
  1542         -      pTerm = &pWC->a[idxTerm];
  1543         -      pTerm->wtFlags |= TERM_COPIED;
  1544         -      pNewTerm->prereqAll = pTerm->prereqAll;
  1545         -    }
  1546         -  }
  1547         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  1548         -
  1549         -  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1550         -  ** an index for tables to the left of the join.
  1551         -  */
  1552         -  pTerm->prereqRight |= extraRight;
  1553         -}
  1554         -
  1555    345   /*
  1556    346   ** This function searches pList for an entry that matches the iCol-th column
  1557    347   ** of index pIdx.
  1558    348   **
  1559    349   ** If such an expression is found, its index in pList->a[] is returned. If
  1560    350   ** no expression is found, -1 is returned.
  1561    351   */
................................................................................
  1585    375     return -1;
  1586    376   }
  1587    377   
  1588    378   /*
  1589    379   ** Return true if the DISTINCT expression-list passed as the third argument
  1590    380   ** is redundant.
  1591    381   **
  1592         -** A DISTINCT list is redundant if the database contains some subset of
  1593         -** columns that are unique and non-null.
          382  +** A DISTINCT list is redundant if any subset of the columns in the
          383  +** DISTINCT list are collectively unique and individually non-null.
  1594    384   */
  1595    385   static int isDistinctRedundant(
  1596    386     Parse *pParse,            /* Parsing context */
  1597    387     SrcList *pTabList,        /* The FROM clause */
  1598    388     WhereClause *pWC,         /* The WHERE clause */
  1599    389     ExprList *pDistinct       /* The result set that needs to be DISTINCT */
  1600    390   ){
................................................................................
  1632    422     **   3. All of those index columns for which the WHERE clause does not
  1633    423     **      contain a "col=X" term are subject to a NOT NULL constraint.
  1634    424     */
  1635    425     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1636    426       if( !IsUniqueIndex(pIdx) ) continue;
  1637    427       for(i=0; i<pIdx->nKeyCol; i++){
  1638    428         i16 iCol = pIdx->aiColumn[i];
  1639         -      if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
          429  +      if( 0==sqlite3WhereFindTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
  1640    430           int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
  1641    431           if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
  1642    432             break;
  1643    433           }
  1644    434         }
  1645    435       }
  1646    436       if( i==pIdx->nKeyCol ){
................................................................................
  1963    753   ** Allocate and populate an sqlite3_index_info structure. It is the 
  1964    754   ** responsibility of the caller to eventually release the structure
  1965    755   ** by passing the pointer returned by this function to sqlite3_free().
  1966    756   */
  1967    757   static sqlite3_index_info *allocateIndexInfo(
  1968    758     Parse *pParse,
  1969    759     WhereClause *pWC,
          760  +  Bitmask mUnusable,              /* Ignore terms with these prereqs */
  1970    761     struct SrcList_item *pSrc,
  1971    762     ExprList *pOrderBy
  1972    763   ){
  1973    764     int i, j;
  1974    765     int nTerm;
  1975    766     struct sqlite3_index_constraint *pIdxCons;
  1976    767     struct sqlite3_index_orderby *pIdxOrderBy;
................................................................................
  1979    770     int nOrderBy;
  1980    771     sqlite3_index_info *pIdxInfo;
  1981    772   
  1982    773     /* Count the number of possible WHERE clause constraints referring
  1983    774     ** to this virtual table */
  1984    775     for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  1985    776       if( pTerm->leftCursor != pSrc->iCursor ) continue;
          777  +    if( pTerm->prereqRight & mUnusable ) continue;
  1986    778       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
  1987    779       testcase( pTerm->eOperator & WO_IN );
  1988    780       testcase( pTerm->eOperator & WO_ISNULL );
  1989    781       testcase( pTerm->eOperator & WO_IS );
  1990    782       testcase( pTerm->eOperator & WO_ALL );
  1991    783       if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
  1992    784       if( pTerm->wtFlags & TERM_VNULL ) continue;
................................................................................
  2033    825     *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
  2034    826     *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
  2035    827                                                                      pUsage;
  2036    828   
  2037    829     for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
  2038    830       u8 op;
  2039    831       if( pTerm->leftCursor != pSrc->iCursor ) continue;
          832  +    if( pTerm->prereqRight & mUnusable ) continue;
  2040    833       assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
  2041    834       testcase( pTerm->eOperator & WO_IN );
  2042    835       testcase( pTerm->eOperator & WO_IS );
  2043    836       testcase( pTerm->eOperator & WO_ISNULL );
  2044    837       testcase( pTerm->eOperator & WO_ALL );
  2045    838       if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV|WO_IS))==0 ) continue;
  2046    839       if( pTerm->wtFlags & TERM_VNULL ) continue;
................................................................................
  2754   1547       WHERETRACE(0x10,("IN row estimate: est=%d\n", nRowEst));
  2755   1548     }
  2756   1549     assert( pBuilder->nRecValid==nRecValid );
  2757   1550     return rc;
  2758   1551   }
  2759   1552   #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  2760   1553   
  2761         -/*
  2762         -** Disable a term in the WHERE clause.  Except, do not disable the term
  2763         -** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  2764         -** or USING clause of that join.
  2765         -**
  2766         -** Consider the term t2.z='ok' in the following queries:
  2767         -**
  2768         -**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
  2769         -**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
  2770         -**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
  2771         -**
  2772         -** The t2.z='ok' is disabled in the in (2) because it originates
  2773         -** in the ON clause.  The term is disabled in (3) because it is not part
  2774         -** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
  2775         -**
  2776         -** Disabling a term causes that term to not be tested in the inner loop
  2777         -** of the join.  Disabling is an optimization.  When terms are satisfied
  2778         -** by indices, we disable them to prevent redundant tests in the inner
  2779         -** loop.  We would get the correct results if nothing were ever disabled,
  2780         -** but joins might run a little slower.  The trick is to disable as much
  2781         -** as we can without disabling too much.  If we disabled in (1), we'd get
  2782         -** the wrong answer.  See ticket #813.
  2783         -**
  2784         -** If all the children of a term are disabled, then that term is also
  2785         -** automatically disabled.  In this way, terms get disabled if derived
  2786         -** virtual terms are tested first.  For example:
  2787         -**
  2788         -**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
  2789         -**      \___________/     \______/     \_____/
  2790         -**         parent          child1       child2
  2791         -**
  2792         -** Only the parent term was in the original WHERE clause.  The child1
  2793         -** and child2 terms were added by the LIKE optimization.  If both of
  2794         -** the virtual child terms are valid, then testing of the parent can be 
  2795         -** skipped.
  2796         -**
  2797         -** Usually the parent term is marked as TERM_CODED.  But if the parent
  2798         -** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
  2799         -** The TERM_LIKECOND marking indicates that the term should be coded inside
  2800         -** a conditional such that is only evaluated on the second pass of a
  2801         -** LIKE-optimization loop, when scanning BLOBs instead of strings.
  2802         -*/
  2803         -static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
  2804         -  int nLoop = 0;
  2805         -  while( pTerm
  2806         -      && (pTerm->wtFlags & TERM_CODED)==0
  2807         -      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
  2808         -      && (pLevel->notReady & pTerm->prereqAll)==0
  2809         -  ){
  2810         -    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
  2811         -      pTerm->wtFlags |= TERM_LIKECOND;
  2812         -    }else{
  2813         -      pTerm->wtFlags |= TERM_CODED;
  2814         -    }
  2815         -    if( pTerm->iParent<0 ) break;
  2816         -    pTerm = &pTerm->pWC->a[pTerm->iParent];
  2817         -    pTerm->nChild--;
  2818         -    if( pTerm->nChild!=0 ) break;
  2819         -    nLoop++;
  2820         -  }
  2821         -}
  2822         -
  2823         -/*
  2824         -** Code an OP_Affinity opcode to apply the column affinity string zAff
  2825         -** to the n registers starting at base. 
  2826         -**
  2827         -** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
  2828         -** beginning and end of zAff are ignored.  If all entries in zAff are
  2829         -** SQLITE_AFF_NONE, then no code gets generated.
  2830         -**
  2831         -** This routine makes its own copy of zAff so that the caller is free
  2832         -** to modify zAff after this routine returns.
  2833         -*/
  2834         -static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
  2835         -  Vdbe *v = pParse->pVdbe;
  2836         -  if( zAff==0 ){
  2837         -    assert( pParse->db->mallocFailed );
  2838         -    return;
  2839         -  }
  2840         -  assert( v!=0 );
  2841         -
  2842         -  /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
  2843         -  ** and end of the affinity string.
  2844         -  */
  2845         -  while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
  2846         -    n--;
  2847         -    base++;
  2848         -    zAff++;
  2849         -  }
  2850         -  while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
  2851         -    n--;
  2852         -  }
  2853         -
  2854         -  /* Code the OP_Affinity opcode if there is anything left to do. */
  2855         -  if( n>0 ){
  2856         -    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
  2857         -    sqlite3VdbeChangeP4(v, -1, zAff, n);
  2858         -    sqlite3ExprCacheAffinityChange(pParse, base, n);
  2859         -  }
  2860         -}
  2861         -
  2862         -
  2863         -/*
  2864         -** Generate code for a single equality term of the WHERE clause.  An equality
  2865         -** term can be either X=expr or X IN (...).   pTerm is the term to be 
  2866         -** coded.
  2867         -**
  2868         -** The current value for the constraint is left in register iReg.
  2869         -**
  2870         -** For a constraint of the form X=expr, the expression is evaluated and its
  2871         -** result is left on the stack.  For constraints of the form X IN (...)
  2872         -** this routine sets up a loop that will iterate over all values of X.
  2873         -*/
  2874         -static int codeEqualityTerm(
  2875         -  Parse *pParse,      /* The parsing context */
  2876         -  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
  2877         -  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
  2878         -  int iEq,            /* Index of the equality term within this level */
  2879         -  int bRev,           /* True for reverse-order IN operations */
  2880         -  int iTarget         /* Attempt to leave results in this register */
  2881         -){
  2882         -  Expr *pX = pTerm->pExpr;
  2883         -  Vdbe *v = pParse->pVdbe;
  2884         -  int iReg;                  /* Register holding results */
  2885         -
  2886         -  assert( iTarget>0 );
  2887         -  if( pX->op==TK_EQ || pX->op==TK_IS ){
  2888         -    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
  2889         -  }else if( pX->op==TK_ISNULL ){
  2890         -    iReg = iTarget;
  2891         -    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
  2892         -#ifndef SQLITE_OMIT_SUBQUERY
  2893         -  }else{
  2894         -    int eType;
  2895         -    int iTab;
  2896         -    struct InLoop *pIn;
  2897         -    WhereLoop *pLoop = pLevel->pWLoop;
  2898         -
  2899         -    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
  2900         -      && pLoop->u.btree.pIndex!=0
  2901         -      && pLoop->u.btree.pIndex->aSortOrder[iEq]
  2902         -    ){
  2903         -      testcase( iEq==0 );
  2904         -      testcase( bRev );
  2905         -      bRev = !bRev;
  2906         -    }
  2907         -    assert( pX->op==TK_IN );
  2908         -    iReg = iTarget;
  2909         -    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
  2910         -    if( eType==IN_INDEX_INDEX_DESC ){
  2911         -      testcase( bRev );
  2912         -      bRev = !bRev;
  2913         -    }
  2914         -    iTab = pX->iTable;
  2915         -    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
  2916         -    VdbeCoverageIf(v, bRev);
  2917         -    VdbeCoverageIf(v, !bRev);
  2918         -    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
  2919         -    pLoop->wsFlags |= WHERE_IN_ABLE;
  2920         -    if( pLevel->u.in.nIn==0 ){
  2921         -      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  2922         -    }
  2923         -    pLevel->u.in.nIn++;
  2924         -    pLevel->u.in.aInLoop =
  2925         -       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
  2926         -                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
  2927         -    pIn = pLevel->u.in.aInLoop;
  2928         -    if( pIn ){
  2929         -      pIn += pLevel->u.in.nIn - 1;
  2930         -      pIn->iCur = iTab;
  2931         -      if( eType==IN_INDEX_ROWID ){
  2932         -        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
  2933         -      }else{
  2934         -        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
  2935         -      }
  2936         -      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
  2937         -      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
  2938         -    }else{
  2939         -      pLevel->u.in.nIn = 0;
  2940         -    }
  2941         -#endif
  2942         -  }
  2943         -  disableTerm(pLevel, pTerm);
  2944         -  return iReg;
  2945         -}
  2946         -
  2947         -/*
  2948         -** Generate code that will evaluate all == and IN constraints for an
  2949         -** index scan.
  2950         -**
  2951         -** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
  2952         -** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
  2953         -** The index has as many as three equality constraints, but in this
  2954         -** example, the third "c" value is an inequality.  So only two 
  2955         -** constraints are coded.  This routine will generate code to evaluate
  2956         -** a==5 and b IN (1,2,3).  The current values for a and b will be stored
  2957         -** in consecutive registers and the index of the first register is returned.
  2958         -**
  2959         -** In the example above nEq==2.  But this subroutine works for any value
  2960         -** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
  2961         -** The only thing it does is allocate the pLevel->iMem memory cell and
  2962         -** compute the affinity string.
  2963         -**
  2964         -** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
  2965         -** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
  2966         -** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
  2967         -** occurs after the nEq quality constraints.
  2968         -**
  2969         -** This routine allocates a range of nEq+nExtraReg memory cells and returns
  2970         -** the index of the first memory cell in that range. The code that
  2971         -** calls this routine will use that memory range to store keys for
  2972         -** start and termination conditions of the loop.
  2973         -** key value of the loop.  If one or more IN operators appear, then
  2974         -** this routine allocates an additional nEq memory cells for internal
  2975         -** use.
  2976         -**
  2977         -** Before returning, *pzAff is set to point to a buffer containing a
  2978         -** copy of the column affinity string of the index allocated using
  2979         -** sqlite3DbMalloc(). Except, entries in the copy of the string associated
  2980         -** with equality constraints that use NONE affinity are set to
  2981         -** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
  2982         -**
  2983         -**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
  2984         -**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
  2985         -**
  2986         -** In the example above, the index on t1(a) has TEXT affinity. But since
  2987         -** the right hand side of the equality constraint (t2.b) has NONE affinity,
  2988         -** no conversion should be attempted before using a t2.b value as part of
  2989         -** a key to search the index. Hence the first byte in the returned affinity
  2990         -** string in this example would be set to SQLITE_AFF_NONE.
  2991         -*/
  2992         -static int codeAllEqualityTerms(
  2993         -  Parse *pParse,        /* Parsing context */
  2994         -  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
  2995         -  int bRev,             /* Reverse the order of IN operators */
  2996         -  int nExtraReg,        /* Number of extra registers to allocate */
  2997         -  char **pzAff          /* OUT: Set to point to affinity string */
  2998         -){
  2999         -  u16 nEq;                      /* The number of == or IN constraints to code */
  3000         -  u16 nSkip;                    /* Number of left-most columns to skip */
  3001         -  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
  3002         -  Index *pIdx;                  /* The index being used for this loop */
  3003         -  WhereTerm *pTerm;             /* A single constraint term */
  3004         -  WhereLoop *pLoop;             /* The WhereLoop object */
  3005         -  int j;                        /* Loop counter */
  3006         -  int regBase;                  /* Base register */
  3007         -  int nReg;                     /* Number of registers to allocate */
  3008         -  char *zAff;                   /* Affinity string to return */
  3009         -
  3010         -  /* This module is only called on query plans that use an index. */
  3011         -  pLoop = pLevel->pWLoop;
  3012         -  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
  3013         -  nEq = pLoop->u.btree.nEq;
  3014         -  nSkip = pLoop->nSkip;
  3015         -  pIdx = pLoop->u.btree.pIndex;
  3016         -  assert( pIdx!=0 );
  3017         -
  3018         -  /* Figure out how many memory cells we will need then allocate them.
  3019         -  */
  3020         -  regBase = pParse->nMem + 1;
  3021         -  nReg = pLoop->u.btree.nEq + nExtraReg;
  3022         -  pParse->nMem += nReg;
  3023         -
  3024         -  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
  3025         -  if( !zAff ){
  3026         -    pParse->db->mallocFailed = 1;
  3027         -  }
  3028         -
  3029         -  if( nSkip ){
  3030         -    int iIdxCur = pLevel->iIdxCur;
  3031         -    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
  3032         -    VdbeCoverageIf(v, bRev==0);
  3033         -    VdbeCoverageIf(v, bRev!=0);
  3034         -    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
  3035         -    j = sqlite3VdbeAddOp0(v, OP_Goto);
  3036         -    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
  3037         -                            iIdxCur, 0, regBase, nSkip);
  3038         -    VdbeCoverageIf(v, bRev==0);
  3039         -    VdbeCoverageIf(v, bRev!=0);
  3040         -    sqlite3VdbeJumpHere(v, j);
  3041         -    for(j=0; j<nSkip; j++){
  3042         -      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
  3043         -      assert( pIdx->aiColumn[j]>=0 );
  3044         -      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
  3045         -    }
  3046         -  }    
  3047         -
  3048         -  /* Evaluate the equality constraints
  3049         -  */
  3050         -  assert( zAff==0 || (int)strlen(zAff)>=nEq );
  3051         -  for(j=nSkip; j<nEq; j++){
  3052         -    int r1;
  3053         -    pTerm = pLoop->aLTerm[j];
  3054         -    assert( pTerm!=0 );
  3055         -    /* The following testcase is true for indices with redundant columns. 
  3056         -    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  3057         -    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
  3058         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
  3059         -    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
  3060         -    if( r1!=regBase+j ){
  3061         -      if( nReg==1 ){
  3062         -        sqlite3ReleaseTempReg(pParse, regBase);
  3063         -        regBase = r1;
  3064         -      }else{
  3065         -        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
  3066         -      }
  3067         -    }
  3068         -    testcase( pTerm->eOperator & WO_ISNULL );
  3069         -    testcase( pTerm->eOperator & WO_IN );
  3070         -    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
  3071         -      Expr *pRight = pTerm->pExpr->pRight;
  3072         -      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
  3073         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
  3074         -        VdbeCoverage(v);
  3075         -      }
  3076         -      if( zAff ){
  3077         -        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
  3078         -          zAff[j] = SQLITE_AFF_NONE;
  3079         -        }
  3080         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
  3081         -          zAff[j] = SQLITE_AFF_NONE;
  3082         -        }
  3083         -      }
  3084         -    }
  3085         -  }
  3086         -  *pzAff = zAff;
  3087         -  return regBase;
  3088         -}
  3089         -
  3090         -#ifndef SQLITE_OMIT_EXPLAIN
  3091         -/*
  3092         -** This routine is a helper for explainIndexRange() below
  3093         -**
  3094         -** pStr holds the text of an expression that we are building up one term
  3095         -** at a time.  This routine adds a new term to the end of the expression.
  3096         -** Terms are separated by AND so add the "AND" text for second and subsequent
  3097         -** terms only.
  3098         -*/
  3099         -static void explainAppendTerm(
  3100         -  StrAccum *pStr,             /* The text expression being built */
  3101         -  int iTerm,                  /* Index of this term.  First is zero */
  3102         -  const char *zColumn,        /* Name of the column */
  3103         -  const char *zOp             /* Name of the operator */
  3104         -){
  3105         -  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
  3106         -  sqlite3StrAccumAppendAll(pStr, zColumn);
  3107         -  sqlite3StrAccumAppend(pStr, zOp, 1);
  3108         -  sqlite3StrAccumAppend(pStr, "?", 1);
  3109         -}
  3110         -
  3111         -/*
  3112         -** Argument pLevel describes a strategy for scanning table pTab. This 
  3113         -** function appends text to pStr that describes the subset of table
  3114         -** rows scanned by the strategy in the form of an SQL expression.
  3115         -**
  3116         -** For example, if the query:
  3117         -**
  3118         -**   SELECT * FROM t1 WHERE a=1 AND b>2;
  3119         -**
  3120         -** is run and there is an index on (a, b), then this function returns a
  3121         -** string similar to:
  3122         -**
  3123         -**   "a=? AND b>?"
  3124         -*/
  3125         -static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
  3126         -  Index *pIndex = pLoop->u.btree.pIndex;
  3127         -  u16 nEq = pLoop->u.btree.nEq;
  3128         -  u16 nSkip = pLoop->nSkip;
  3129         -  int i, j;
  3130         -  Column *aCol = pTab->aCol;
  3131         -  i16 *aiColumn = pIndex->aiColumn;
  3132         -
  3133         -  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
  3134         -  sqlite3StrAccumAppend(pStr, " (", 2);
  3135         -  for(i=0; i<nEq; i++){
  3136         -    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
  3137         -    if( i>=nSkip ){
  3138         -      explainAppendTerm(pStr, i, z, "=");
  3139         -    }else{
  3140         -      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
  3141         -      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
  3142         -    }
  3143         -  }
  3144         -
  3145         -  j = i;
  3146         -  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
  3147         -    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
  3148         -    explainAppendTerm(pStr, i++, z, ">");
  3149         -  }
  3150         -  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
  3151         -    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
  3152         -    explainAppendTerm(pStr, i, z, "<");
  3153         -  }
  3154         -  sqlite3StrAccumAppend(pStr, ")", 1);
  3155         -}
  3156         -
  3157         -/*
  3158         -** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
  3159         -** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
  3160         -** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
  3161         -** is added to the output to describe the table scan strategy in pLevel.
  3162         -**
  3163         -** If an OP_Explain opcode is added to the VM, its address is returned.
  3164         -** Otherwise, if no OP_Explain is coded, zero is returned.
  3165         -*/
  3166         -static int explainOneScan(
  3167         -  Parse *pParse,                  /* Parse context */
  3168         -  SrcList *pTabList,              /* Table list this loop refers to */
  3169         -  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
  3170         -  int iLevel,                     /* Value for "level" column of output */
  3171         -  int iFrom,                      /* Value for "from" column of output */
  3172         -  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
  3173         -){
  3174         -  int ret = 0;
  3175         -#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
  3176         -  if( pParse->explain==2 )
  3177         -#endif
  3178         -  {
  3179         -    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
  3180         -    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
  3181         -    sqlite3 *db = pParse->db;     /* Database handle */
  3182         -    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
  3183         -    int isSearch;                 /* True for a SEARCH. False for SCAN. */
  3184         -    WhereLoop *pLoop;             /* The controlling WhereLoop object */
  3185         -    u32 flags;                    /* Flags that describe this loop */
  3186         -    char *zMsg;                   /* Text to add to EQP output */
  3187         -    StrAccum str;                 /* EQP output string */
  3188         -    char zBuf[100];               /* Initial space for EQP output string */
  3189         -
  3190         -    pLoop = pLevel->pWLoop;
  3191         -    flags = pLoop->wsFlags;
  3192         -    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
  3193         -
  3194         -    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
  3195         -            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
  3196         -            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
  3197         -
  3198         -    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
  3199         -    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
  3200         -    if( pItem->pSelect ){
  3201         -      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
  3202         -    }else{
  3203         -      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
  3204         -    }
  3205         -
  3206         -    if( pItem->zAlias ){
  3207         -      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
  3208         -    }
  3209         -    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
  3210         -      const char *zFmt = 0;
  3211         -      Index *pIdx;
  3212         -
  3213         -      assert( pLoop->u.btree.pIndex!=0 );
  3214         -      pIdx = pLoop->u.btree.pIndex;
  3215         -      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
  3216         -      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
  3217         -        if( isSearch ){
  3218         -          zFmt = "PRIMARY KEY";
  3219         -        }
  3220         -      }else if( flags & WHERE_PARTIALIDX ){
  3221         -        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
  3222         -      }else if( flags & WHERE_AUTO_INDEX ){
  3223         -        zFmt = "AUTOMATIC COVERING INDEX";
  3224         -      }else if( flags & WHERE_IDX_ONLY ){
  3225         -        zFmt = "COVERING INDEX %s";
  3226         -      }else{
  3227         -        zFmt = "INDEX %s";
  3228         -      }
  3229         -      if( zFmt ){
  3230         -        sqlite3StrAccumAppend(&str, " USING ", 7);
  3231         -        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
  3232         -        explainIndexRange(&str, pLoop, pItem->pTab);
  3233         -      }
  3234         -    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
  3235         -      const char *zRange;
  3236         -      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
  3237         -        zRange = "(rowid=?)";
  3238         -      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
  3239         -        zRange = "(rowid>? AND rowid<?)";
  3240         -      }else if( flags&WHERE_BTM_LIMIT ){
  3241         -        zRange = "(rowid>?)";
  3242         -      }else{
  3243         -        assert( flags&WHERE_TOP_LIMIT);
  3244         -        zRange = "(rowid<?)";
  3245         -      }
  3246         -      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
  3247         -      sqlite3StrAccumAppendAll(&str, zRange);
  3248         -    }
  3249         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  3250         -    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
  3251         -      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
  3252         -                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
  3253         -    }
  3254         -#endif
  3255         -#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
  3256         -    if( pLoop->nOut>=10 ){
  3257         -      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
  3258         -    }else{
  3259         -      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
  3260         -    }
  3261         -#endif
  3262         -    zMsg = sqlite3StrAccumFinish(&str);
  3263         -    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
  3264         -  }
  3265         -  return ret;
  3266         -}
  3267         -#else
  3268         -# define explainOneScan(u,v,w,x,y,z) 0
  3269         -#endif /* SQLITE_OMIT_EXPLAIN */
  3270         -
  3271         -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  3272         -/*
  3273         -** Configure the VM passed as the first argument with an
  3274         -** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
  3275         -** implement level pLvl. Argument pSrclist is a pointer to the FROM 
  3276         -** clause that the scan reads data from.
  3277         -**
  3278         -** If argument addrExplain is not 0, it must be the address of an 
  3279         -** OP_Explain instruction that describes the same loop.
  3280         -*/
  3281         -static void addScanStatus(
  3282         -  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
  3283         -  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
  3284         -  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
  3285         -  int addrExplain                 /* Address of OP_Explain (or 0) */
  3286         -){
  3287         -  const char *zObj = 0;
  3288         -  WhereLoop *pLoop = pLvl->pWLoop;
  3289         -  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
  3290         -    zObj = pLoop->u.btree.pIndex->zName;
  3291         -  }else{
  3292         -    zObj = pSrclist->a[pLvl->iFrom].zName;
  3293         -  }
  3294         -  sqlite3VdbeScanStatus(
  3295         -      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
  3296         -  );
  3297         -}
  3298         -#else
  3299         -# define addScanStatus(a, b, c, d) ((void)d)
  3300         -#endif
  3301         -
  3302         -/*
  3303         -** If the most recently coded instruction is a constant range contraint
  3304         -** that originated from the LIKE optimization, then change the P3 to be
  3305         -** pLoop->iLikeRepCntr and set P5.
  3306         -**
  3307         -** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
  3308         -** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
  3309         -** scan loop run twice, once for strings and a second time for BLOBs.
  3310         -** The OP_String opcodes on the second pass convert the upper and lower
  3311         -** bound string contants to blobs.  This routine makes the necessary changes
  3312         -** to the OP_String opcodes for that to happen.
  3313         -*/
  3314         -static void whereLikeOptimizationStringFixup(
  3315         -  Vdbe *v,                /* prepared statement under construction */
  3316         -  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
  3317         -  WhereTerm *pTerm        /* The upper or lower bound just coded */
  3318         -){
  3319         -  if( pTerm->wtFlags & TERM_LIKEOPT ){
  3320         -    VdbeOp *pOp;
  3321         -    assert( pLevel->iLikeRepCntr>0 );
  3322         -    pOp = sqlite3VdbeGetOp(v, -1);
  3323         -    assert( pOp!=0 );
  3324         -    assert( pOp->opcode==OP_String8 
  3325         -            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
  3326         -    pOp->p3 = pLevel->iLikeRepCntr;
  3327         -    pOp->p5 = 1;
  3328         -  }
  3329         -}
  3330         -
  3331         -/*
  3332         -** Generate code for the start of the iLevel-th loop in the WHERE clause
  3333         -** implementation described by pWInfo.
  3334         -*/
  3335         -static Bitmask codeOneLoopStart(
  3336         -  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
  3337         -  int iLevel,          /* Which level of pWInfo->a[] should be coded */
  3338         -  Bitmask notReady     /* Which tables are currently available */
  3339         -){
  3340         -  int j, k;            /* Loop counters */
  3341         -  int iCur;            /* The VDBE cursor for the table */
  3342         -  int addrNxt;         /* Where to jump to continue with the next IN case */
  3343         -  int omitTable;       /* True if we use the index only */
  3344         -  int bRev;            /* True if we need to scan in reverse order */
  3345         -  WhereLevel *pLevel;  /* The where level to be coded */
  3346         -  WhereLoop *pLoop;    /* The WhereLoop object being coded */
  3347         -  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
  3348         -  WhereTerm *pTerm;               /* A WHERE clause term */
  3349         -  Parse *pParse;                  /* Parsing context */
  3350         -  sqlite3 *db;                    /* Database connection */
  3351         -  Vdbe *v;                        /* The prepared stmt under constructions */
  3352         -  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
  3353         -  int addrBrk;                    /* Jump here to break out of the loop */
  3354         -  int addrCont;                   /* Jump here to continue with next cycle */
  3355         -  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
  3356         -  int iReleaseReg = 0;      /* Temp register to free before returning */
  3357         -
  3358         -  pParse = pWInfo->pParse;
  3359         -  v = pParse->pVdbe;
  3360         -  pWC = &pWInfo->sWC;
  3361         -  db = pParse->db;
  3362         -  pLevel = &pWInfo->a[iLevel];
  3363         -  pLoop = pLevel->pWLoop;
  3364         -  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
  3365         -  iCur = pTabItem->iCursor;
  3366         -  pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
  3367         -  bRev = (pWInfo->revMask>>iLevel)&1;
  3368         -  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
  3369         -           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
  3370         -  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
  3371         -
  3372         -  /* Create labels for the "break" and "continue" instructions
  3373         -  ** for the current loop.  Jump to addrBrk to break out of a loop.
  3374         -  ** Jump to cont to go immediately to the next iteration of the
  3375         -  ** loop.
  3376         -  **
  3377         -  ** When there is an IN operator, we also have a "addrNxt" label that
  3378         -  ** means to continue with the next IN value combination.  When
  3379         -  ** there are no IN operators in the constraints, the "addrNxt" label
  3380         -  ** is the same as "addrBrk".
  3381         -  */
  3382         -  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
  3383         -  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
  3384         -
  3385         -  /* If this is the right table of a LEFT OUTER JOIN, allocate and
  3386         -  ** initialize a memory cell that records if this table matches any
  3387         -  ** row of the left table of the join.
  3388         -  */
  3389         -  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
  3390         -    pLevel->iLeftJoin = ++pParse->nMem;
  3391         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
  3392         -    VdbeComment((v, "init LEFT JOIN no-match flag"));
  3393         -  }
  3394         -
  3395         -  /* Special case of a FROM clause subquery implemented as a co-routine */
  3396         -  if( pTabItem->viaCoroutine ){
  3397         -    int regYield = pTabItem->regReturn;
  3398         -    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
  3399         -    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
  3400         -    VdbeCoverage(v);
  3401         -    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
  3402         -    pLevel->op = OP_Goto;
  3403         -  }else
  3404         -
  3405         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  3406         -  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
  3407         -    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
  3408         -    **          to access the data.
  3409         -    */
  3410         -    int iReg;   /* P3 Value for OP_VFilter */
  3411         -    int addrNotFound;
  3412         -    int nConstraint = pLoop->nLTerm;
  3413         -
  3414         -    sqlite3ExprCachePush(pParse);
  3415         -    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
  3416         -    addrNotFound = pLevel->addrBrk;
  3417         -    for(j=0; j<nConstraint; j++){
  3418         -      int iTarget = iReg+j+2;
  3419         -      pTerm = pLoop->aLTerm[j];
  3420         -      if( pTerm==0 ) continue;
  3421         -      if( pTerm->eOperator & WO_IN ){
  3422         -        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
  3423         -        addrNotFound = pLevel->addrNxt;
  3424         -      }else{
  3425         -        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
  3426         -      }
  3427         -    }
  3428         -    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
  3429         -    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
  3430         -    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
  3431         -                      pLoop->u.vtab.idxStr,
  3432         -                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
  3433         -    VdbeCoverage(v);
  3434         -    pLoop->u.vtab.needFree = 0;
  3435         -    for(j=0; j<nConstraint && j<16; j++){
  3436         -      if( (pLoop->u.vtab.omitMask>>j)&1 ){
  3437         -        disableTerm(pLevel, pLoop->aLTerm[j]);
  3438         -      }
  3439         -    }
  3440         -    pLevel->op = OP_VNext;
  3441         -    pLevel->p1 = iCur;
  3442         -    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3443         -    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
  3444         -    sqlite3ExprCachePop(pParse);
  3445         -  }else
  3446         -#endif /* SQLITE_OMIT_VIRTUALTABLE */
  3447         -
  3448         -  if( (pLoop->wsFlags & WHERE_IPK)!=0
  3449         -   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
  3450         -  ){
  3451         -    /* Case 2:  We can directly reference a single row using an
  3452         -    **          equality comparison against the ROWID field.  Or
  3453         -    **          we reference multiple rows using a "rowid IN (...)"
  3454         -    **          construct.
  3455         -    */
  3456         -    assert( pLoop->u.btree.nEq==1 );
  3457         -    pTerm = pLoop->aLTerm[0];
  3458         -    assert( pTerm!=0 );
  3459         -    assert( pTerm->pExpr!=0 );
  3460         -    assert( omitTable==0 );
  3461         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
  3462         -    iReleaseReg = ++pParse->nMem;
  3463         -    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
  3464         -    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
  3465         -    addrNxt = pLevel->addrNxt;
  3466         -    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
  3467         -    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
  3468         -    VdbeCoverage(v);
  3469         -    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
  3470         -    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  3471         -    VdbeComment((v, "pk"));
  3472         -    pLevel->op = OP_Noop;
  3473         -  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
  3474         -         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
  3475         -  ){
  3476         -    /* Case 3:  We have an inequality comparison against the ROWID field.
  3477         -    */
  3478         -    int testOp = OP_Noop;
  3479         -    int start;
  3480         -    int memEndValue = 0;
  3481         -    WhereTerm *pStart, *pEnd;
  3482         -
  3483         -    assert( omitTable==0 );
  3484         -    j = 0;
  3485         -    pStart = pEnd = 0;
  3486         -    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
  3487         -    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
  3488         -    assert( pStart!=0 || pEnd!=0 );
  3489         -    if( bRev ){
  3490         -      pTerm = pStart;
  3491         -      pStart = pEnd;
  3492         -      pEnd = pTerm;
  3493         -    }
  3494         -    if( pStart ){
  3495         -      Expr *pX;             /* The expression that defines the start bound */
  3496         -      int r1, rTemp;        /* Registers for holding the start boundary */
  3497         -
  3498         -      /* The following constant maps TK_xx codes into corresponding 
  3499         -      ** seek opcodes.  It depends on a particular ordering of TK_xx
  3500         -      */
  3501         -      const u8 aMoveOp[] = {
  3502         -           /* TK_GT */  OP_SeekGT,
  3503         -           /* TK_LE */  OP_SeekLE,
  3504         -           /* TK_LT */  OP_SeekLT,
  3505         -           /* TK_GE */  OP_SeekGE
  3506         -      };
  3507         -      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
  3508         -      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
  3509         -      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
  3510         -
  3511         -      assert( (pStart->wtFlags & TERM_VNULL)==0 );
  3512         -      testcase( pStart->wtFlags & TERM_VIRTUAL );
  3513         -      pX = pStart->pExpr;
  3514         -      assert( pX!=0 );
  3515         -      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
  3516         -      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
  3517         -      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
  3518         -      VdbeComment((v, "pk"));
  3519         -      VdbeCoverageIf(v, pX->op==TK_GT);
  3520         -      VdbeCoverageIf(v, pX->op==TK_LE);
  3521         -      VdbeCoverageIf(v, pX->op==TK_LT);
  3522         -      VdbeCoverageIf(v, pX->op==TK_GE);
  3523         -      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
  3524         -      sqlite3ReleaseTempReg(pParse, rTemp);
  3525         -      disableTerm(pLevel, pStart);
  3526         -    }else{
  3527         -      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
  3528         -      VdbeCoverageIf(v, bRev==0);
  3529         -      VdbeCoverageIf(v, bRev!=0);
  3530         -    }
  3531         -    if( pEnd ){
  3532         -      Expr *pX;
  3533         -      pX = pEnd->pExpr;
  3534         -      assert( pX!=0 );
  3535         -      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
  3536         -      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
  3537         -      testcase( pEnd->wtFlags & TERM_VIRTUAL );
  3538         -      memEndValue = ++pParse->nMem;
  3539         -      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
  3540         -      if( pX->op==TK_LT || pX->op==TK_GT ){
  3541         -        testOp = bRev ? OP_Le : OP_Ge;
  3542         -      }else{
  3543         -        testOp = bRev ? OP_Lt : OP_Gt;
  3544         -      }
  3545         -      disableTerm(pLevel, pEnd);
  3546         -    }
  3547         -    start = sqlite3VdbeCurrentAddr(v);
  3548         -    pLevel->op = bRev ? OP_Prev : OP_Next;
  3549         -    pLevel->p1 = iCur;
  3550         -    pLevel->p2 = start;
  3551         -    assert( pLevel->p5==0 );
  3552         -    if( testOp!=OP_Noop ){
  3553         -      iRowidReg = ++pParse->nMem;
  3554         -      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
  3555         -      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  3556         -      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
  3557         -      VdbeCoverageIf(v, testOp==OP_Le);
  3558         -      VdbeCoverageIf(v, testOp==OP_Lt);
  3559         -      VdbeCoverageIf(v, testOp==OP_Ge);
  3560         -      VdbeCoverageIf(v, testOp==OP_Gt);
  3561         -      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
  3562         -    }
  3563         -  }else if( pLoop->wsFlags & WHERE_INDEXED ){
  3564         -    /* Case 4: A scan using an index.
  3565         -    **
  3566         -    **         The WHERE clause may contain zero or more equality 
  3567         -    **         terms ("==" or "IN" operators) that refer to the N
  3568         -    **         left-most columns of the index. It may also contain
  3569         -    **         inequality constraints (>, <, >= or <=) on the indexed
  3570         -    **         column that immediately follows the N equalities. Only 
  3571         -    **         the right-most column can be an inequality - the rest must
  3572         -    **         use the "==" and "IN" operators. For example, if the 
  3573         -    **         index is on (x,y,z), then the following clauses are all 
  3574         -    **         optimized:
  3575         -    **
  3576         -    **            x=5
  3577         -    **            x=5 AND y=10
  3578         -    **            x=5 AND y<10
  3579         -    **            x=5 AND y>5 AND y<10
  3580         -    **            x=5 AND y=5 AND z<=10
  3581         -    **
  3582         -    **         The z<10 term of the following cannot be used, only
  3583         -    **         the x=5 term:
  3584         -    **
  3585         -    **            x=5 AND z<10
  3586         -    **
  3587         -    **         N may be zero if there are inequality constraints.
  3588         -    **         If there are no inequality constraints, then N is at
  3589         -    **         least one.
  3590         -    **
  3591         -    **         This case is also used when there are no WHERE clause
  3592         -    **         constraints but an index is selected anyway, in order
  3593         -    **         to force the output order to conform to an ORDER BY.
  3594         -    */  
  3595         -    static const u8 aStartOp[] = {
  3596         -      0,
  3597         -      0,
  3598         -      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
  3599         -      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
  3600         -      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
  3601         -      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
  3602         -      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
  3603         -      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
  3604         -    };
  3605         -    static const u8 aEndOp[] = {
  3606         -      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
  3607         -      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
  3608         -      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
  3609         -      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
  3610         -    };
  3611         -    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
  3612         -    int regBase;                 /* Base register holding constraint values */
  3613         -    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
  3614         -    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
  3615         -    int startEq;                 /* True if range start uses ==, >= or <= */
  3616         -    int endEq;                   /* True if range end uses ==, >= or <= */
  3617         -    int start_constraints;       /* Start of range is constrained */
  3618         -    int nConstraint;             /* Number of constraint terms */
  3619         -    Index *pIdx;                 /* The index we will be using */
  3620         -    int iIdxCur;                 /* The VDBE cursor for the index */
  3621         -    int nExtraReg = 0;           /* Number of extra registers needed */
  3622         -    int op;                      /* Instruction opcode */
  3623         -    char *zStartAff;             /* Affinity for start of range constraint */
  3624         -    char cEndAff = 0;            /* Affinity for end of range constraint */
  3625         -    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
  3626         -    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
  3627         -
  3628         -    pIdx = pLoop->u.btree.pIndex;
  3629         -    iIdxCur = pLevel->iIdxCur;
  3630         -    assert( nEq>=pLoop->nSkip );
  3631         -
  3632         -    /* If this loop satisfies a sort order (pOrderBy) request that 
  3633         -    ** was passed to this function to implement a "SELECT min(x) ..." 
  3634         -    ** query, then the caller will only allow the loop to run for
  3635         -    ** a single iteration. This means that the first row returned
  3636         -    ** should not have a NULL value stored in 'x'. If column 'x' is
  3637         -    ** the first one after the nEq equality constraints in the index,
  3638         -    ** this requires some special handling.
  3639         -    */
  3640         -    assert( pWInfo->pOrderBy==0
  3641         -         || pWInfo->pOrderBy->nExpr==1
  3642         -         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
  3643         -    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
  3644         -     && pWInfo->nOBSat>0
  3645         -     && (pIdx->nKeyCol>nEq)
  3646         -    ){
  3647         -      assert( pLoop->nSkip==0 );
  3648         -      bSeekPastNull = 1;
  3649         -      nExtraReg = 1;
  3650         -    }
  3651         -
  3652         -    /* Find any inequality constraint terms for the start and end 
  3653         -    ** of the range. 
  3654         -    */
  3655         -    j = nEq;
  3656         -    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
  3657         -      pRangeStart = pLoop->aLTerm[j++];
  3658         -      nExtraReg = 1;
  3659         -      /* Like optimization range constraints always occur in pairs */
  3660         -      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
  3661         -              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
  3662         -    }
  3663         -    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
  3664         -      pRangeEnd = pLoop->aLTerm[j++];
  3665         -      nExtraReg = 1;
  3666         -      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
  3667         -        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
  3668         -        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
  3669         -        pLevel->iLikeRepCntr = ++pParse->nMem;
  3670         -        testcase( bRev );
  3671         -        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
  3672         -        sqlite3VdbeAddOp2(v, OP_Integer,
  3673         -                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
  3674         -                          pLevel->iLikeRepCntr);
  3675         -        VdbeComment((v, "LIKE loop counter"));
  3676         -        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
  3677         -      }
  3678         -      if( pRangeStart==0
  3679         -       && (j = pIdx->aiColumn[nEq])>=0 
  3680         -       && pIdx->pTable->aCol[j].notNull==0
  3681         -      ){
  3682         -        bSeekPastNull = 1;
  3683         -      }
  3684         -    }
  3685         -    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
  3686         -
  3687         -    /* Generate code to evaluate all constraint terms using == or IN
  3688         -    ** and store the values of those terms in an array of registers
  3689         -    ** starting at regBase.
  3690         -    */
  3691         -    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
  3692         -    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
  3693         -    if( zStartAff ) cEndAff = zStartAff[nEq];
  3694         -    addrNxt = pLevel->addrNxt;
  3695         -
  3696         -    /* If we are doing a reverse order scan on an ascending index, or
  3697         -    ** a forward order scan on a descending index, interchange the 
  3698         -    ** start and end terms (pRangeStart and pRangeEnd).
  3699         -    */
  3700         -    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
  3701         -     || (bRev && pIdx->nKeyCol==nEq)
  3702         -    ){
  3703         -      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
  3704         -      SWAP(u8, bSeekPastNull, bStopAtNull);
  3705         -    }
  3706         -
  3707         -    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
  3708         -    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
  3709         -    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
  3710         -    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
  3711         -    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
  3712         -    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
  3713         -    start_constraints = pRangeStart || nEq>0;
  3714         -
  3715         -    /* Seek the index cursor to the start of the range. */
  3716         -    nConstraint = nEq;
  3717         -    if( pRangeStart ){
  3718         -      Expr *pRight = pRangeStart->pExpr->pRight;
  3719         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
  3720         -      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
  3721         -      if( (pRangeStart->wtFlags & TERM_VNULL)==0
  3722         -       && sqlite3ExprCanBeNull(pRight)
  3723         -      ){
  3724         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  3725         -        VdbeCoverage(v);
  3726         -      }
  3727         -      if( zStartAff ){
  3728         -        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
  3729         -          /* Since the comparison is to be performed with no conversions
  3730         -          ** applied to the operands, set the affinity to apply to pRight to 
  3731         -          ** SQLITE_AFF_NONE.  */
  3732         -          zStartAff[nEq] = SQLITE_AFF_NONE;
  3733         -        }
  3734         -        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
  3735         -          zStartAff[nEq] = SQLITE_AFF_NONE;
  3736         -        }
  3737         -      }  
  3738         -      nConstraint++;
  3739         -      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
  3740         -    }else if( bSeekPastNull ){
  3741         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  3742         -      nConstraint++;
  3743         -      startEq = 0;
  3744         -      start_constraints = 1;
  3745         -    }
  3746         -    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
  3747         -    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
  3748         -    assert( op!=0 );
  3749         -    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
  3750         -    VdbeCoverage(v);
  3751         -    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
  3752         -    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
  3753         -    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
  3754         -    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
  3755         -    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
  3756         -    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
  3757         -
  3758         -    /* Load the value for the inequality constraint at the end of the
  3759         -    ** range (if any).
  3760         -    */
  3761         -    nConstraint = nEq;
  3762         -    if( pRangeEnd ){
  3763         -      Expr *pRight = pRangeEnd->pExpr->pRight;
  3764         -      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
  3765         -      sqlite3ExprCode(pParse, pRight, regBase+nEq);
  3766         -      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
  3767         -      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
  3768         -       && sqlite3ExprCanBeNull(pRight)
  3769         -      ){
  3770         -        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
  3771         -        VdbeCoverage(v);
  3772         -      }
  3773         -      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
  3774         -       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
  3775         -      ){
  3776         -        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
  3777         -      }
  3778         -      nConstraint++;
  3779         -      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
  3780         -    }else if( bStopAtNull ){
  3781         -      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
  3782         -      endEq = 0;
  3783         -      nConstraint++;
  3784         -    }
  3785         -    sqlite3DbFree(db, zStartAff);
  3786         -
  3787         -    /* Top of the loop body */
  3788         -    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
  3789         -
  3790         -    /* Check if the index cursor is past the end of the range. */
  3791         -    if( nConstraint ){
  3792         -      op = aEndOp[bRev*2 + endEq];
  3793         -      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
  3794         -      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
  3795         -      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
  3796         -      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
  3797         -      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
  3798         -    }
  3799         -
  3800         -    /* Seek the table cursor, if required */
  3801         -    disableTerm(pLevel, pRangeStart);
  3802         -    disableTerm(pLevel, pRangeEnd);
  3803         -    if( omitTable ){
  3804         -      /* pIdx is a covering index.  No need to access the main table. */
  3805         -    }else if( HasRowid(pIdx->pTable) ){
  3806         -      iRowidReg = ++pParse->nMem;
  3807         -      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
  3808         -      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
  3809         -      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
  3810         -    }else if( iCur!=iIdxCur ){
  3811         -      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  3812         -      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
  3813         -      for(j=0; j<pPk->nKeyCol; j++){
  3814         -        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
  3815         -        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
  3816         -      }
  3817         -      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
  3818         -                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
  3819         -    }
  3820         -
  3821         -    /* Record the instruction used to terminate the loop. Disable 
  3822         -    ** WHERE clause terms made redundant by the index range scan.
  3823         -    */
  3824         -    if( pLoop->wsFlags & WHERE_ONEROW ){
  3825         -      pLevel->op = OP_Noop;
  3826         -    }else if( bRev ){
  3827         -      pLevel->op = OP_Prev;
  3828         -    }else{
  3829         -      pLevel->op = OP_Next;
  3830         -    }
  3831         -    pLevel->p1 = iIdxCur;
  3832         -    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
  3833         -    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
  3834         -      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  3835         -    }else{
  3836         -      assert( pLevel->p5==0 );
  3837         -    }
  3838         -  }else
  3839         -
  3840         -#ifndef SQLITE_OMIT_OR_OPTIMIZATION
  3841         -  if( pLoop->wsFlags & WHERE_MULTI_OR ){
  3842         -    /* Case 5:  Two or more separately indexed terms connected by OR
  3843         -    **
  3844         -    ** Example:
  3845         -    **
  3846         -    **   CREATE TABLE t1(a,b,c,d);
  3847         -    **   CREATE INDEX i1 ON t1(a);
  3848         -    **   CREATE INDEX i2 ON t1(b);
  3849         -    **   CREATE INDEX i3 ON t1(c);
  3850         -    **
  3851         -    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
  3852         -    **
  3853         -    ** In the example, there are three indexed terms connected by OR.
  3854         -    ** The top of the loop looks like this:
  3855         -    **
  3856         -    **          Null       1                # Zero the rowset in reg 1
  3857         -    **
  3858         -    ** Then, for each indexed term, the following. The arguments to
  3859         -    ** RowSetTest are such that the rowid of the current row is inserted
  3860         -    ** into the RowSet. If it is already present, control skips the
  3861         -    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
  3862         -    **
  3863         -    **        sqlite3WhereBegin(<term>)
  3864         -    **          RowSetTest                  # Insert rowid into rowset
  3865         -    **          Gosub      2 A
  3866         -    **        sqlite3WhereEnd()
  3867         -    **
  3868         -    ** Following the above, code to terminate the loop. Label A, the target
  3869         -    ** of the Gosub above, jumps to the instruction right after the Goto.
  3870         -    **
  3871         -    **          Null       1                # Zero the rowset in reg 1
  3872         -    **          Goto       B                # The loop is finished.
  3873         -    **
  3874         -    **       A: <loop body>                 # Return data, whatever.
  3875         -    **
  3876         -    **          Return     2                # Jump back to the Gosub
  3877         -    **
  3878         -    **       B: <after the loop>
  3879         -    **
  3880         -    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
  3881         -    ** use an ephemeral index instead of a RowSet to record the primary
  3882         -    ** keys of the rows we have already seen.
  3883         -    **
  3884         -    */
  3885         -    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
  3886         -    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
  3887         -    Index *pCov = 0;             /* Potential covering index (or NULL) */
  3888         -    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
  3889         -
  3890         -    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
  3891         -    int regRowset = 0;                        /* Register for RowSet object */
  3892         -    int regRowid = 0;                         /* Register holding rowid */
  3893         -    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
  3894         -    int iRetInit;                             /* Address of regReturn init */
  3895         -    int untestedTerms = 0;             /* Some terms not completely tested */
  3896         -    int ii;                            /* Loop counter */
  3897         -    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
  3898         -    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
  3899         -    Table *pTab = pTabItem->pTab;
  3900         -   
  3901         -    pTerm = pLoop->aLTerm[0];
  3902         -    assert( pTerm!=0 );
  3903         -    assert( pTerm->eOperator & WO_OR );
  3904         -    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
  3905         -    pOrWc = &pTerm->u.pOrInfo->wc;
  3906         -    pLevel->op = OP_Return;
  3907         -    pLevel->p1 = regReturn;
  3908         -
  3909         -    /* Set up a new SrcList in pOrTab containing the table being scanned
  3910         -    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
  3911         -    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
  3912         -    */
  3913         -    if( pWInfo->nLevel>1 ){
  3914         -      int nNotReady;                 /* The number of notReady tables */
  3915         -      struct SrcList_item *origSrc;     /* Original list of tables */
  3916         -      nNotReady = pWInfo->nLevel - iLevel - 1;
  3917         -      pOrTab = sqlite3StackAllocRaw(db,
  3918         -                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
  3919         -      if( pOrTab==0 ) return notReady;
  3920         -      pOrTab->nAlloc = (u8)(nNotReady + 1);
  3921         -      pOrTab->nSrc = pOrTab->nAlloc;
  3922         -      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
  3923         -      origSrc = pWInfo->pTabList->a;
  3924         -      for(k=1; k<=nNotReady; k++){
  3925         -        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
  3926         -      }
  3927         -    }else{
  3928         -      pOrTab = pWInfo->pTabList;
  3929         -    }
  3930         -
  3931         -    /* Initialize the rowset register to contain NULL. An SQL NULL is 
  3932         -    ** equivalent to an empty rowset.  Or, create an ephemeral index
  3933         -    ** capable of holding primary keys in the case of a WITHOUT ROWID.
  3934         -    **
  3935         -    ** Also initialize regReturn to contain the address of the instruction 
  3936         -    ** immediately following the OP_Return at the bottom of the loop. This
  3937         -    ** is required in a few obscure LEFT JOIN cases where control jumps
  3938         -    ** over the top of the loop into the body of it. In this case the 
  3939         -    ** correct response for the end-of-loop code (the OP_Return) is to 
  3940         -    ** fall through to the next instruction, just as an OP_Next does if
  3941         -    ** called on an uninitialized cursor.
  3942         -    */
  3943         -    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  3944         -      if( HasRowid(pTab) ){
  3945         -        regRowset = ++pParse->nMem;
  3946         -        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
  3947         -      }else{
  3948         -        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
  3949         -        regRowset = pParse->nTab++;
  3950         -        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
  3951         -        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
  3952         -      }
  3953         -      regRowid = ++pParse->nMem;
  3954         -    }
  3955         -    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
  3956         -
  3957         -    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
  3958         -    ** Then for every term xN, evaluate as the subexpression: xN AND z
  3959         -    ** That way, terms in y that are factored into the disjunction will
  3960         -    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
  3961         -    **
  3962         -    ** Actually, each subexpression is converted to "xN AND w" where w is
  3963         -    ** the "interesting" terms of z - terms that did not originate in the
  3964         -    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
  3965         -    ** indices.
  3966         -    **
  3967         -    ** This optimization also only applies if the (x1 OR x2 OR ...) term
  3968         -    ** is not contained in the ON clause of a LEFT JOIN.
  3969         -    ** See ticket http://www.sqlite.org/src/info/f2369304e4
  3970         -    */
  3971         -    if( pWC->nTerm>1 ){
  3972         -      int iTerm;
  3973         -      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
  3974         -        Expr *pExpr = pWC->a[iTerm].pExpr;
  3975         -        if( &pWC->a[iTerm] == pTerm ) continue;
  3976         -        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
  3977         -        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
  3978         -        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
  3979         -        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
  3980         -        pExpr = sqlite3ExprDup(db, pExpr, 0);
  3981         -        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
  3982         -      }
  3983         -      if( pAndExpr ){
  3984         -        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
  3985         -      }
  3986         -    }
  3987         -
  3988         -    /* Run a separate WHERE clause for each term of the OR clause.  After
  3989         -    ** eliminating duplicates from other WHERE clauses, the action for each
  3990         -    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
  3991         -    */
  3992         -    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
  3993         -                | WHERE_FORCE_TABLE
  3994         -                | WHERE_ONETABLE_ONLY
  3995         -                | WHERE_NO_AUTOINDEX;
  3996         -    for(ii=0; ii<pOrWc->nTerm; ii++){
  3997         -      WhereTerm *pOrTerm = &pOrWc->a[ii];
  3998         -      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
  3999         -        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
  4000         -        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
  4001         -        int j1 = 0;                     /* Address of jump operation */
  4002         -        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
  4003         -          pAndExpr->pLeft = pOrExpr;
  4004         -          pOrExpr = pAndExpr;
  4005         -        }
  4006         -        /* Loop through table entries that match term pOrTerm. */
  4007         -        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
  4008         -        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
  4009         -                                      wctrlFlags, iCovCur);
  4010         -        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
  4011         -        if( pSubWInfo ){
  4012         -          WhereLoop *pSubLoop;
  4013         -          int addrExplain = explainOneScan(
  4014         -              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
  4015         -          );
  4016         -          addScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
  4017         -
  4018         -          /* This is the sub-WHERE clause body.  First skip over
  4019         -          ** duplicate rows from prior sub-WHERE clauses, and record the
  4020         -          ** rowid (or PRIMARY KEY) for the current row so that the same
  4021         -          ** row will be skipped in subsequent sub-WHERE clauses.
  4022         -          */
  4023         -          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
  4024         -            int r;
  4025         -            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
  4026         -            if( HasRowid(pTab) ){
  4027         -              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
  4028         -              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
  4029         -              VdbeCoverage(v);
  4030         -            }else{
  4031         -              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
  4032         -              int nPk = pPk->nKeyCol;
  4033         -              int iPk;
  4034         -
  4035         -              /* Read the PK into an array of temp registers. */
  4036         -              r = sqlite3GetTempRange(pParse, nPk);
  4037         -              for(iPk=0; iPk<nPk; iPk++){
  4038         -                int iCol = pPk->aiColumn[iPk];
  4039         -                sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
  4040         -              }
  4041         -
  4042         -              /* Check if the temp table already contains this key. If so,
  4043         -              ** the row has already been included in the result set and
  4044         -              ** can be ignored (by jumping past the Gosub below). Otherwise,
  4045         -              ** insert the key into the temp table and proceed with processing
  4046         -              ** the row.
  4047         -              **
  4048         -              ** Use some of the same optimizations as OP_RowSetTest: If iSet
  4049         -              ** is zero, assume that the key cannot already be present in
  4050         -              ** the temp table. And if iSet is -1, assume that there is no 
  4051         -              ** need to insert the key into the temp table, as it will never 
  4052         -              ** be tested for.  */ 
  4053         -              if( iSet ){
  4054         -                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
  4055         -                VdbeCoverage(v);
  4056         -              }
  4057         -              if( iSet>=0 ){
  4058         -                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
  4059         -                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
  4060         -                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  4061         -              }
  4062         -
  4063         -              /* Release the array of temp registers */
  4064         -              sqlite3ReleaseTempRange(pParse, r, nPk);
  4065         -            }
  4066         -          }
  4067         -
  4068         -          /* Invoke the main loop body as a subroutine */
  4069         -          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
  4070         -
  4071         -          /* Jump here (skipping the main loop body subroutine) if the
  4072         -          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
  4073         -          if( j1 ) sqlite3VdbeJumpHere(v, j1);
  4074         -
  4075         -          /* The pSubWInfo->untestedTerms flag means that this OR term
  4076         -          ** contained one or more AND term from a notReady table.  The
  4077         -          ** terms from the notReady table could not be tested and will
  4078         -          ** need to be tested later.
  4079         -          */
  4080         -          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
  4081         -
  4082         -          /* If all of the OR-connected terms are optimized using the same
  4083         -          ** index, and the index is opened using the same cursor number
  4084         -          ** by each call to sqlite3WhereBegin() made by this loop, it may
  4085         -          ** be possible to use that index as a covering index.
  4086         -          **
  4087         -          ** If the call to sqlite3WhereBegin() above resulted in a scan that
  4088         -          ** uses an index, and this is either the first OR-connected term
  4089         -          ** processed or the index is the same as that used by all previous
  4090         -          ** terms, set pCov to the candidate covering index. Otherwise, set 
  4091         -          ** pCov to NULL to indicate that no candidate covering index will 
  4092         -          ** be available.
  4093         -          */
  4094         -          pSubLoop = pSubWInfo->a[0].pWLoop;
  4095         -          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
  4096         -          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
  4097         -           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
  4098         -           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
  4099         -          ){
  4100         -            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
  4101         -            pCov = pSubLoop->u.btree.pIndex;
  4102         -            wctrlFlags |= WHERE_REOPEN_IDX;
  4103         -          }else{
  4104         -            pCov = 0;
  4105         -          }
  4106         -
  4107         -          /* Finish the loop through table entries that match term pOrTerm. */
  4108         -          sqlite3WhereEnd(pSubWInfo);
  4109         -        }
  4110         -      }
  4111         -    }
  4112         -    pLevel->u.pCovidx = pCov;
  4113         -    if( pCov ) pLevel->iIdxCur = iCovCur;
  4114         -    if( pAndExpr ){
  4115         -      pAndExpr->pLeft = 0;
  4116         -      sqlite3ExprDelete(db, pAndExpr);
  4117         -    }
  4118         -    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
  4119         -    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
  4120         -    sqlite3VdbeResolveLabel(v, iLoopBody);
  4121         -
  4122         -    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
  4123         -    if( !untestedTerms ) disableTerm(pLevel, pTerm);
  4124         -  }else
  4125         -#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  4126         -
  4127         -  {
  4128         -    /* Case 6:  There is no usable index.  We must do a complete
  4129         -    **          scan of the entire table.
  4130         -    */
  4131         -    static const u8 aStep[] = { OP_Next, OP_Prev };
  4132         -    static const u8 aStart[] = { OP_Rewind, OP_Last };
  4133         -    assert( bRev==0 || bRev==1 );
  4134         -    if( pTabItem->isRecursive ){
  4135         -      /* Tables marked isRecursive have only a single row that is stored in
  4136         -      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
  4137         -      pLevel->op = OP_Noop;
  4138         -    }else{
  4139         -      pLevel->op = aStep[bRev];
  4140         -      pLevel->p1 = iCur;
  4141         -      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
  4142         -      VdbeCoverageIf(v, bRev==0);
  4143         -      VdbeCoverageIf(v, bRev!=0);
  4144         -      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  4145         -    }
  4146         -  }
  4147         -
  4148         -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
  4149         -  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
  4150         -#endif
  4151         -
  4152         -  /* Insert code to test every subexpression that can be completely
  4153         -  ** computed using the current set of tables.
  4154         -  */
  4155         -  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  4156         -    Expr *pE;
  4157         -    int skipLikeAddr = 0;
  4158         -    testcase( pTerm->wtFlags & TERM_VIRTUAL );
  4159         -    testcase( pTerm->wtFlags & TERM_CODED );
  4160         -    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  4161         -    if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
  4162         -      testcase( pWInfo->untestedTerms==0
  4163         -               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
  4164         -      pWInfo->untestedTerms = 1;
  4165         -      continue;
  4166         -    }
  4167         -    pE = pTerm->pExpr;
  4168         -    assert( pE!=0 );
  4169         -    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
  4170         -      continue;
  4171         -    }
  4172         -    if( pTerm->wtFlags & TERM_LIKECOND ){
  4173         -      assert( pLevel->iLikeRepCntr>0 );
  4174         -      skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
  4175         -      VdbeCoverage(v);
  4176         -    }
  4177         -    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
  4178         -    if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
  4179         -    pTerm->wtFlags |= TERM_CODED;
  4180         -  }
  4181         -
  4182         -  /* Insert code to test for implied constraints based on transitivity
  4183         -  ** of the "==" operator.
  4184         -  **
  4185         -  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
  4186         -  ** and we are coding the t1 loop and the t2 loop has not yet coded,
  4187         -  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
  4188         -  ** the implied "t1.a=123" constraint.
  4189         -  */
  4190         -  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
  4191         -    Expr *pE, *pEAlt;
  4192         -    WhereTerm *pAlt;
  4193         -    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  4194         -    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
  4195         -    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
  4196         -    if( pTerm->leftCursor!=iCur ) continue;
  4197         -    if( pLevel->iLeftJoin ) continue;
  4198         -    pE = pTerm->pExpr;
  4199         -    assert( !ExprHasProperty(pE, EP_FromJoin) );
  4200         -    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
  4201         -    pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
  4202         -                    WO_EQ|WO_IN|WO_IS, 0);
  4203         -    if( pAlt==0 ) continue;
  4204         -    if( pAlt->wtFlags & (TERM_CODED) ) continue;
  4205         -    testcase( pAlt->eOperator & WO_EQ );
  4206         -    testcase( pAlt->eOperator & WO_IS );
  4207         -    testcase( pAlt->eOperator & WO_IN );
  4208         -    VdbeModuleComment((v, "begin transitive constraint"));
  4209         -    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
  4210         -    if( pEAlt ){
  4211         -      *pEAlt = *pAlt->pExpr;
  4212         -      pEAlt->pLeft = pE->pLeft;
  4213         -      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
  4214         -      sqlite3StackFree(db, pEAlt);
  4215         -    }
  4216         -  }
  4217         -
  4218         -  /* For a LEFT OUTER JOIN, generate code that will record the fact that
  4219         -  ** at least one row of the right table has matched the left table.  
  4220         -  */
  4221         -  if( pLevel->iLeftJoin ){
  4222         -    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
  4223         -    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
  4224         -    VdbeComment((v, "record LEFT JOIN hit"));
  4225         -    sqlite3ExprCacheClear(pParse);
  4226         -    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
  4227         -      testcase( pTerm->wtFlags & TERM_VIRTUAL );
  4228         -      testcase( pTerm->wtFlags & TERM_CODED );
  4229         -      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
  4230         -      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
  4231         -        assert( pWInfo->untestedTerms );
  4232         -        continue;
  4233         -      }
  4234         -      assert( pTerm->pExpr );
  4235         -      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
  4236         -      pTerm->wtFlags |= TERM_CODED;
  4237         -    }
  4238         -  }
  4239         -
  4240         -  return pLevel->notReady;
  4241         -}
  4242   1554   
  4243   1555   #ifdef WHERETRACE_ENABLED
  4244   1556   /*
  4245   1557   ** Print the content of a WhereTerm object
  4246   1558   */
  4247   1559   static void whereTermPrint(WhereTerm *pTerm, int iTerm){
  4248   1560     if( pTerm==0 ){
................................................................................
  4401   1713       int i;
  4402   1714       for(i=0; i<pWInfo->nLevel; i++){
  4403   1715         WhereLevel *pLevel = &pWInfo->a[i];
  4404   1716         if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
  4405   1717           sqlite3DbFree(db, pLevel->u.in.aInLoop);
  4406   1718         }
  4407   1719       }
  4408         -    whereClauseClear(&pWInfo->sWC);
         1720  +    sqlite3WhereClauseClear(&pWInfo->sWC);
  4409   1721       while( pWInfo->pLoops ){
  4410   1722         WhereLoop *p = pWInfo->pLoops;
  4411   1723         pWInfo->pLoops = p->pNextLoop;
  4412   1724         whereLoopDelete(db, p);
  4413   1725       }
  4414   1726       sqlite3DbFree(db, pWInfo);
  4415   1727     }
................................................................................
  5353   2665     return rc;
  5354   2666   }
  5355   2667   
  5356   2668   #ifndef SQLITE_OMIT_VIRTUALTABLE
  5357   2669   /*
  5358   2670   ** Add all WhereLoop objects for a table of the join identified by
  5359   2671   ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
         2672  +**
         2673  +** If there are no LEFT or CROSS JOIN joins in the query, both mExtra and
         2674  +** mUnusable are set to 0. Otherwise, mExtra is a mask of all FROM clause
         2675  +** entries that occur before the virtual table in the FROM clause and are
         2676  +** separated from it by at least one LEFT or CROSS JOIN. Similarly, the
         2677  +** mUnusable mask contains all FROM clause entries that occur after the
         2678  +** virtual table and are separated from it by at least one LEFT or 
         2679  +** CROSS JOIN. 
         2680  +**
         2681  +** For example, if the query were:
         2682  +**
         2683  +**   ... FROM t1, t2 LEFT JOIN t3, t4, vt CROSS JOIN t5, t6;
         2684  +**
         2685  +** then mExtra corresponds to (t1, t2) and mUnusable to (t5, t6).
         2686  +**
         2687  +** All the tables in mExtra must be scanned before the current virtual 
         2688  +** table. So any terms for which all prerequisites are satisfied by 
         2689  +** mExtra may be specified as "usable" in all calls to xBestIndex. 
         2690  +** Conversely, all tables in mUnusable must be scanned after the current
         2691  +** virtual table, so any terms for which the prerequisites overlap with
         2692  +** mUnusable should always be configured as "not-usable" for xBestIndex.
  5360   2693   */
  5361   2694   static int whereLoopAddVirtual(
  5362   2695     WhereLoopBuilder *pBuilder,  /* WHERE clause information */
  5363         -  Bitmask mExtra
         2696  +  Bitmask mExtra,              /* Tables that must be scanned before this one */
         2697  +  Bitmask mUnusable            /* Tables that must be scanned after this one */
  5364   2698   ){
  5365   2699     WhereInfo *pWInfo;           /* WHERE analysis context */
  5366   2700     Parse *pParse;               /* The parsing context */
  5367   2701     WhereClause *pWC;            /* The WHERE clause */
  5368   2702     struct SrcList_item *pSrc;   /* The FROM clause term to search */
  5369   2703     Table *pTab;
  5370   2704     sqlite3 *db;
................................................................................
  5377   2711     int nConstraint;
  5378   2712     int seenIn = 0;              /* True if an IN operator is seen */
  5379   2713     int seenVar = 0;             /* True if a non-constant constraint is seen */
  5380   2714     int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
  5381   2715     WhereLoop *pNew;
  5382   2716     int rc = SQLITE_OK;
  5383   2717   
         2718  +  assert( (mExtra & mUnusable)==0 );
  5384   2719     pWInfo = pBuilder->pWInfo;
  5385   2720     pParse = pWInfo->pParse;
  5386   2721     db = pParse->db;
  5387   2722     pWC = pBuilder->pWC;
  5388   2723     pNew = pBuilder->pNew;
  5389   2724     pSrc = &pWInfo->pTabList->a[pNew->iTab];
  5390   2725     pTab = pSrc->pTab;
  5391   2726     assert( IsVirtual(pTab) );
  5392         -  pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
         2727  +  pIdxInfo = allocateIndexInfo(pParse, pWC, mUnusable, pSrc,pBuilder->pOrderBy);
  5393   2728     if( pIdxInfo==0 ) return SQLITE_NOMEM;
  5394   2729     pNew->prereq = 0;
  5395   2730     pNew->rSetup = 0;
  5396   2731     pNew->wsFlags = WHERE_VIRTUALTABLE;
  5397   2732     pNew->nLTerm = 0;
  5398   2733     pNew->u.vtab.needFree = 0;
  5399   2734     pUsage = pIdxInfo->aConstraintUsage;
................................................................................
  5415   2750         pTerm = &pWC->a[j];
  5416   2751         switch( iPhase ){
  5417   2752           case 0:    /* Constants without IN operator */
  5418   2753             pIdxCons->usable = 0;
  5419   2754             if( (pTerm->eOperator & WO_IN)!=0 ){
  5420   2755               seenIn = 1;
  5421   2756             }
  5422         -          if( pTerm->prereqRight!=0 ){
         2757  +          if( (pTerm->prereqRight & ~mExtra)!=0 ){
  5423   2758               seenVar = 1;
  5424   2759             }else if( (pTerm->eOperator & WO_IN)==0 ){
  5425   2760               pIdxCons->usable = 1;
  5426   2761             }
  5427   2762             break;
  5428   2763           case 1:    /* Constants with IN operators */
  5429   2764             assert( seenIn );
  5430         -          pIdxCons->usable = (pTerm->prereqRight==0);
         2765  +          pIdxCons->usable = (pTerm->prereqRight & ~mExtra)==0;
  5431   2766             break;
  5432   2767           case 2:    /* Variables without IN */
  5433   2768             assert( seenVar );
  5434   2769             pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
  5435   2770             break;
  5436   2771           default:   /* Variables with IN */
  5437   2772             assert( seenVar && seenIn );
................................................................................
  5522   2857   }
  5523   2858   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  5524   2859   
  5525   2860   /*
  5526   2861   ** Add WhereLoop entries to handle OR terms.  This works for either
  5527   2862   ** btrees or virtual tables.
  5528   2863   */
  5529         -static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
         2864  +static int whereLoopAddOr(
         2865  +  WhereLoopBuilder *pBuilder, 
         2866  +  Bitmask mExtra, 
         2867  +  Bitmask mUnusable
         2868  +){
  5530   2869     WhereInfo *pWInfo = pBuilder->pWInfo;
  5531   2870     WhereClause *pWC;
  5532   2871     WhereLoop *pNew;
  5533   2872     WhereTerm *pTerm, *pWCEnd;
  5534   2873     int rc = SQLITE_OK;
  5535   2874     int iCur;
  5536   2875     WhereClause tempWC;
................................................................................
  5581   2920             for(i=0; i<sSubBuild.pWC->nTerm; i++){
  5582   2921               whereTermPrint(&sSubBuild.pWC->a[i], i);
  5583   2922             }
  5584   2923           }
  5585   2924   #endif
  5586   2925   #ifndef SQLITE_OMIT_VIRTUALTABLE
  5587   2926           if( IsVirtual(pItem->pTab) ){
  5588         -          rc = whereLoopAddVirtual(&sSubBuild, mExtra);
         2927  +          rc = whereLoopAddVirtual(&sSubBuild, mExtra, mUnusable);
  5589   2928           }else
  5590   2929   #endif
  5591   2930           {
  5592   2931             rc = whereLoopAddBtree(&sSubBuild, mExtra);
  5593   2932           }
  5594   2933           if( rc==SQLITE_OK ){
  5595         -          rc = whereLoopAddOr(&sSubBuild, mExtra);
         2934  +          rc = whereLoopAddOr(&sSubBuild, mExtra, mUnusable);
  5596   2935           }
  5597   2936           assert( rc==SQLITE_OK || sCur.n==0 );
  5598   2937           if( sCur.n==0 ){
  5599   2938             sSum.n = 0;
  5600   2939             break;
  5601   2940           }else if( once ){
  5602   2941             whereOrMove(&sSum, &sCur);
................................................................................
  5650   2989   static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
  5651   2990     WhereInfo *pWInfo = pBuilder->pWInfo;
  5652   2991     Bitmask mExtra = 0;
  5653   2992     Bitmask mPrior = 0;
  5654   2993     int iTab;
  5655   2994     SrcList *pTabList = pWInfo->pTabList;
  5656   2995     struct SrcList_item *pItem;
         2996  +  struct SrcList_item *pEnd = &pTabList->a[pWInfo->nLevel];
  5657   2997     sqlite3 *db = pWInfo->pParse->db;
  5658         -  int nTabList = pWInfo->nLevel;
  5659   2998     int rc = SQLITE_OK;
  5660         -  u8 priorJoinType = 0;
  5661   2999     WhereLoop *pNew;
         3000  +  u8 priorJointype = 0;
  5662   3001   
  5663   3002     /* Loop over the tables in the join, from left to right */
  5664   3003     pNew = pBuilder->pNew;
  5665   3004     whereLoopInit(pNew);
  5666         -  for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
         3005  +  for(iTab=0, pItem=pTabList->a; pItem<pEnd; iTab++, pItem++){
         3006  +    Bitmask mUnusable = 0;
  5667   3007       pNew->iTab = iTab;
  5668         -    pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
  5669         -    if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
         3008  +    pNew->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, pItem->iCursor);
         3009  +    if( ((pItem->jointype|priorJointype) & (JT_LEFT|JT_CROSS))!=0 ){
         3010  +      /* This condition is true when pItem is the FROM clause term on the
         3011  +      ** right-hand-side of a LEFT or CROSS JOIN.  */
  5670   3012         mExtra = mPrior;
  5671   3013       }
  5672         -    priorJoinType = pItem->jointype;
         3014  +    priorJointype = pItem->jointype;
  5673   3015       if( IsVirtual(pItem->pTab) ){
  5674         -      rc = whereLoopAddVirtual(pBuilder, mExtra);
         3016  +      struct SrcList_item *p;
         3017  +      for(p=&pItem[1]; p<pEnd; p++){
         3018  +        if( mUnusable || (p->jointype & (JT_LEFT|JT_CROSS)) ){
         3019  +          mUnusable |= sqlite3WhereGetMask(&pWInfo->sMaskSet, p->iCursor);
         3020  +        }
         3021  +      }
         3022  +      rc = whereLoopAddVirtual(pBuilder, mExtra, mUnusable);
  5675   3023       }else{
  5676   3024         rc = whereLoopAddBtree(pBuilder, mExtra);
  5677   3025       }
  5678   3026       if( rc==SQLITE_OK ){
  5679         -      rc = whereLoopAddOr(pBuilder, mExtra);
         3027  +      rc = whereLoopAddOr(pBuilder, mExtra, mUnusable);
  5680   3028       }
  5681   3029       mPrior |= pNew->maskSelf;
  5682   3030       if( rc || db->mallocFailed ) break;
  5683   3031     }
         3032  +
  5684   3033     whereLoopClear(db, pNew);
  5685   3034     return rc;
  5686   3035   }
  5687   3036   
  5688   3037   /*
  5689   3038   ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
  5690   3039   ** parameters) to see if it outputs rows in the requested ORDER BY
................................................................................
  5782   3131       ** loops.
  5783   3132       */
  5784   3133       for(i=0; i<nOrderBy; i++){
  5785   3134         if( MASKBIT(i) & obSat ) continue;
  5786   3135         pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
  5787   3136         if( pOBExpr->op!=TK_COLUMN ) continue;
  5788   3137         if( pOBExpr->iTable!=iCur ) continue;
  5789         -      pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
         3138  +      pTerm = sqlite3WhereFindTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
  5790   3139                          ~ready, WO_EQ|WO_ISNULL|WO_IS, 0);
  5791   3140         if( pTerm==0 ) continue;
  5792   3141         if( (pTerm->eOperator&(WO_EQ|WO_IS))!=0 && pOBExpr->iColumn>=0 ){
  5793   3142           const char *z1, *z2;
  5794   3143           pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
  5795   3144           if( !pColl ) pColl = db->pDfltColl;
  5796   3145           z1 = pColl->zName;
................................................................................
  5919   3268       if( isOrderDistinct ){
  5920   3269         orderDistinctMask |= pLoop->maskSelf;
  5921   3270         for(i=0; i<nOrderBy; i++){
  5922   3271           Expr *p;
  5923   3272           Bitmask mTerm;
  5924   3273           if( MASKBIT(i) & obSat ) continue;
  5925   3274           p = pOrderBy->a[i].pExpr;
  5926         -        mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
         3275  +        mTerm = sqlite3WhereExprUsage(&pWInfo->sMaskSet,p);
  5927   3276           if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
  5928   3277           if( (mTerm&~orderDistinctMask)==0 ){
  5929   3278             obSat |= MASKBIT(i);
  5930   3279           }
  5931   3280         }
  5932   3281       }
  5933   3282     } /* End the loop over all WhereLoops from outer-most down to inner-most */
................................................................................
  6392   3741     
  6393   3742     pWInfo = pBuilder->pWInfo;
  6394   3743     if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
  6395   3744     assert( pWInfo->pTabList->nSrc>=1 );
  6396   3745     pItem = pWInfo->pTabList->a;
  6397   3746     pTab = pItem->pTab;
  6398   3747     if( IsVirtual(pTab) ) return 0;
  6399         -  if( pItem->zIndex ) return 0;
         3748  +  if( pItem->zIndexedBy ) return 0;
  6400   3749     iCur = pItem->iCursor;
  6401   3750     pWC = &pWInfo->sWC;
  6402   3751     pLoop = pBuilder->pNew;
  6403   3752     pLoop->wsFlags = 0;
  6404   3753     pLoop->nSkip = 0;
  6405         -  pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
         3754  +  pTerm = sqlite3WhereFindTerm(pWC, iCur, -1, 0, WO_EQ|WO_IS, 0);
  6406   3755     if( pTerm ){
  6407   3756       testcase( pTerm->eOperator & WO_IS );
  6408   3757       pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
  6409   3758       pLoop->aLTerm[0] = pTerm;
  6410   3759       pLoop->nLTerm = 1;
  6411   3760       pLoop->u.btree.nEq = 1;
  6412   3761       /* TUNING: Cost of a rowid lookup is 10 */
................................................................................
  6417   3766         assert( pLoop->aLTermSpace==pLoop->aLTerm );
  6418   3767         if( !IsUniqueIndex(pIdx)
  6419   3768          || pIdx->pPartIdxWhere!=0 
  6420   3769          || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace) 
  6421   3770         ) continue;
  6422   3771         opMask = pIdx->uniqNotNull ? (WO_EQ|WO_IS) : WO_EQ;
  6423   3772         for(j=0; j<pIdx->nKeyCol; j++){
  6424         -        pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
         3773  +        pTerm = sqlite3WhereFindTerm(pWC, iCur, pIdx->aiColumn[j], 0, opMask, pIdx);
  6425   3774           if( pTerm==0 ) break;
  6426   3775           testcase( pTerm->eOperator & WO_IS );
  6427   3776           pLoop->aLTerm[j] = pTerm;
  6428   3777         }
  6429   3778         if( j!=pIdx->nKeyCol ) continue;
  6430   3779         pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
  6431   3780         if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
................................................................................
  6438   3787         pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
  6439   3788         break;
  6440   3789       }
  6441   3790     }
  6442   3791     if( pLoop->wsFlags ){
  6443   3792       pLoop->nOut = (LogEst)1;
  6444   3793       pWInfo->a[0].pWLoop = pLoop;
  6445         -    pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
         3794  +    pLoop->maskSelf = sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
  6446   3795       pWInfo->a[0].iTabCur = iCur;
  6447   3796       pWInfo->nRowOut = 1;
  6448   3797       if( pWInfo->pOrderBy ) pWInfo->nOBSat =  pWInfo->pOrderBy->nExpr;
  6449   3798       if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
  6450   3799         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  6451   3800       }
  6452   3801   #ifdef SQLITE_DEBUG
................................................................................
  6632   3981     sWLB.pNew->cId = '*';
  6633   3982   #endif
  6634   3983   
  6635   3984     /* Split the WHERE clause into separate subexpressions where each
  6636   3985     ** subexpression is separated by an AND operator.
  6637   3986     */
  6638   3987     initMaskSet(pMaskSet);
  6639         -  whereClauseInit(&pWInfo->sWC, pWInfo);
  6640         -  whereSplit(&pWInfo->sWC, pWhere, TK_AND);
         3988  +  sqlite3WhereClauseInit(&pWInfo->sWC, pWInfo);
         3989  +  sqlite3WhereSplit(&pWInfo->sWC, pWhere, TK_AND);
  6641   3990       
  6642   3991     /* Special case: a WHERE clause that is constant.  Evaluate the
  6643   3992     ** expression and either jump over all of the code or fall thru.
  6644   3993     */
  6645   3994     for(ii=0; ii<sWLB.pWC->nTerm; ii++){
  6646   3995       if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
  6647   3996         sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
................................................................................
  6678   4027     for(ii=0; ii<pTabList->nSrc; ii++){
  6679   4028       createMask(pMaskSet, pTabList->a[ii].iCursor);
  6680   4029     }
  6681   4030   #ifndef NDEBUG
  6682   4031     {
  6683   4032       Bitmask toTheLeft = 0;
  6684   4033       for(ii=0; ii<pTabList->nSrc; ii++){
  6685         -      Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
         4034  +      Bitmask m = sqlite3WhereGetMask(pMaskSet, pTabList->a[ii].iCursor);
  6686   4035         assert( (m-1)==toTheLeft );
  6687   4036         toTheLeft |= m;
  6688   4037       }
  6689   4038     }
  6690   4039   #endif
  6691   4040   
  6692         -  /* Analyze all of the subexpressions.  Note that exprAnalyze() might
  6693         -  ** add new virtual terms onto the end of the WHERE clause.  We do not
  6694         -  ** want to analyze these virtual terms, so start analyzing at the end
  6695         -  ** and work forward so that the added virtual terms are never processed.
  6696         -  */
  6697         -  exprAnalyzeAll(pTabList, &pWInfo->sWC);
  6698         -  if( db->mallocFailed ){
  6699         -    goto whereBeginError;
  6700         -  }
         4041  +  /* Analyze all of the subexpressions. */
         4042  +  sqlite3WhereExprAnalyze(pTabList, &pWInfo->sWC);
         4043  +  if( db->mallocFailed ) goto whereBeginError;
  6701   4044   
  6702   4045     if( wctrlFlags & WHERE_WANT_DISTINCT ){
  6703   4046       if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
  6704   4047         /* The DISTINCT marking is pointless.  Ignore it. */
  6705   4048         pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  6706   4049       }else if( pOrderBy==0 ){
  6707   4050         /* Try to ORDER BY the result set to make distinct processing easier */
................................................................................
  6709   4052         pWInfo->pOrderBy = pResultSet;
  6710   4053       }
  6711   4054     }
  6712   4055   
  6713   4056     /* Construct the WhereLoop objects */
  6714   4057     WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
  6715   4058   #if defined(WHERETRACE_ENABLED)
  6716         -  /* Display all terms of the WHERE clause */
  6717         -  if( sqlite3WhereTrace & 0x100 ){
         4059  +  if( sqlite3WhereTrace & 0x100 ){ /* Display all terms of the WHERE clause */
  6718   4060       int i;
  6719   4061       for(i=0; i<sWLB.pWC->nTerm; i++){
  6720   4062         whereTermPrint(&sWLB.pWC->a[i], i);
  6721   4063       }
  6722   4064     }
  6723   4065   #endif
  6724   4066   
  6725   4067     if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
  6726   4068       rc = whereLoopAddAll(&sWLB);
  6727   4069       if( rc ) goto whereBeginError;
  6728   4070     
  6729         -    /* Display all of the WhereLoop objects if wheretrace is enabled */
  6730         -#ifdef WHERETRACE_ENABLED /* !=0 */
  6731         -    if( sqlite3WhereTrace ){
         4071  +#ifdef WHERETRACE_ENABLED
         4072  +    if( sqlite3WhereTrace ){    /* Display all of the WhereLoop objects */
  6732   4073         WhereLoop *p;
  6733   4074         int i;
  6734         -      static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
  6735         -                                       "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
         4075  +      static const char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
         4076  +                                             "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
  6736   4077         for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
  6737   4078           p->cId = zLabel[i%sizeof(zLabel)];
  6738   4079           whereLoopPrint(p, sWLB.pWC);
  6739   4080         }
  6740   4081       }
  6741   4082   #endif
  6742   4083     
................................................................................
  6749   4090     }
  6750   4091     if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
  6751   4092        pWInfo->revMask = (Bitmask)(-1);
  6752   4093     }
  6753   4094     if( pParse->nErr || NEVER(db->mallocFailed) ){
  6754   4095       goto whereBeginError;
  6755   4096     }
  6756         -#ifdef WHERETRACE_ENABLED /* !=0 */
         4097  +#ifdef WHERETRACE_ENABLED
  6757   4098     if( sqlite3WhereTrace ){
  6758   4099       sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
  6759   4100       if( pWInfo->nOBSat>0 ){
  6760   4101         sqlite3DebugPrintf(" ORDERBY=%d,0x%llx", pWInfo->nOBSat, pWInfo->revMask);
  6761   4102       }
  6762   4103       switch( pWInfo->eDistinct ){
  6763   4104         case WHERE_DISTINCT_UNIQUE: {
................................................................................
  6780   4121     }
  6781   4122   #endif
  6782   4123     /* Attempt to omit tables from the join that do not effect the result */
  6783   4124     if( pWInfo->nLevel>=2
  6784   4125      && pResultSet!=0
  6785   4126      && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
  6786   4127     ){
  6787         -    Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
  6788         -    if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
         4128  +    Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
         4129  +    if( sWLB.pOrderBy ){
         4130  +      tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
         4131  +    }
  6789   4132       while( pWInfo->nLevel>=2 ){
  6790   4133         WhereTerm *pTerm, *pEnd;
  6791   4134         pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
  6792   4135         if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
  6793   4136         if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
  6794   4137          && (pLoop->wsFlags & WHERE_ONEROW)==0
  6795   4138         ){
................................................................................
  6812   4155     }
  6813   4156     WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
  6814   4157     pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
  6815   4158   
  6816   4159     /* If the caller is an UPDATE or DELETE statement that is requesting
  6817   4160     ** to use a one-pass algorithm, determine if this is appropriate.
  6818   4161     ** The one-pass algorithm only works if the WHERE clause constrains
  6819         -  ** the statement to update a single row.
         4162  +  ** the statement to update or delete a single row.
  6820   4163     */
  6821   4164     assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
  6822   4165     if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 
  6823   4166      && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
  6824   4167       pWInfo->okOnePass = 1;
  6825   4168       if( HasRowid(pTabList->a[0].pTab) ){
  6826   4169         pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
  6827   4170       }
  6828   4171     }
  6829   4172   
  6830   4173     /* Open all tables in the pTabList and any indices selected for
  6831   4174     ** searching those tables.
  6832   4175     */
  6833         -  notReady = ~(Bitmask)0;
  6834   4176     for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
  6835   4177       Table *pTab;     /* Table to open */
  6836   4178       int iDb;         /* Index of database containing table/index */
  6837   4179       struct SrcList_item *pTabItem;
  6838   4180   
  6839   4181       pTabItem = &pTabList->a[pLevel->iFrom];
  6840   4182       pTab = pTabItem->pTab;
................................................................................
  6915   4257           ){
  6916   4258             sqlite3VdbeChangeP5(v, OPFLAG_SEEKEQ); /* Hint to COMDB2 */
  6917   4259           }
  6918   4260           VdbeComment((v, "%s", pIx->zName));
  6919   4261         }
  6920   4262       }
  6921   4263       if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
  6922         -    notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
  6923   4264     }
  6924   4265     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
  6925   4266     if( db->mallocFailed ) goto whereBeginError;
  6926   4267   
  6927   4268     /* Generate the code to do the search.  Each iteration of the for
  6928   4269     ** loop below generates code for a single nested loop of the VM
  6929   4270     ** program.
................................................................................
  6937   4278   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  6938   4279       if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
  6939   4280         constructAutomaticIndex(pParse, &pWInfo->sWC,
  6940   4281                   &pTabList->a[pLevel->iFrom], notReady, pLevel);
  6941   4282         if( db->mallocFailed ) goto whereBeginError;
  6942   4283       }
  6943   4284   #endif
  6944         -    addrExplain = explainOneScan(
         4285  +    addrExplain = sqlite3WhereExplainOneScan(
  6945   4286           pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags
  6946   4287       );
  6947   4288       pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
  6948         -    notReady = codeOneLoopStart(pWInfo, ii, notReady);
         4289  +    notReady = sqlite3WhereCodeOneLoopStart(pWInfo, ii, notReady);
  6949   4290       pWInfo->iContinue = pLevel->addrCont;
  6950   4291       if( (wsFlags&WHERE_MULTI_OR)==0 && (wctrlFlags&WHERE_ONETABLE_ONLY)==0 ){
  6951         -      addScanStatus(v, pTabList, pLevel, addrExplain);
         4292  +      sqlite3WhereAddScanStatus(v, pTabList, pLevel, addrExplain);
  6952   4293       }
  6953   4294     }
  6954   4295   
  6955   4296     /* Done. */
  6956   4297     VdbeModuleComment((v, "Begin WHERE-core"));
  6957   4298     return pWInfo;
  6958   4299   

Changes to src/whereInt.h.

    15     15   ** a separate source file for easier editing.
    16     16   */
    17     17   
    18     18   /*
    19     19   ** Trace output macros
    20     20   */
    21     21   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    22         -/***/ int sqlite3WhereTrace = 0;
           22  +/***/ int sqlite3WhereTrace;
    23     23   #endif
    24     24   #if defined(SQLITE_DEBUG) \
    25     25       && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
    26     26   # define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
    27     27   # define WHERETRACE_ENABLED 1
    28     28   #else
    29     29   # define WHERETRACE(K,X)
................................................................................
   157    157   */
   158    158   #define N_OR_COST 3
   159    159   struct WhereOrSet {
   160    160     u16 n;                      /* Number of valid a[] entries */
   161    161     WhereOrCost a[N_OR_COST];   /* Set of best costs */
   162    162   };
   163    163   
   164         -
   165         -/* Forward declaration of methods */
   166         -static int whereLoopResize(sqlite3*, WhereLoop*, int);
   167         -
   168    164   /*
   169    165   ** Each instance of this object holds a sequence of WhereLoop objects
   170    166   ** that implement some or all of a query plan.
   171    167   **
   172    168   ** Think of each WhereLoop object as a node in a graph with arcs
   173    169   ** showing dependencies and costs for travelling between nodes.  (That is
   174    170   ** not a completely accurate description because WhereLoop costs are a
................................................................................
   368    364   ** no gaps.
   369    365   */
   370    366   struct WhereMaskSet {
   371    367     int n;                        /* Number of assigned cursor values */
   372    368     int ix[BMS];                  /* Cursor assigned to each bit */
   373    369   };
   374    370   
          371  +/*
          372  +** Initialize a WhereMaskSet object
          373  +*/
          374  +#define initMaskSet(P)  (P)->n=0
          375  +
   375    376   /*
   376    377   ** This object is a convenience wrapper holding all information needed
   377    378   ** to construct WhereLoop objects for a particular query.
   378    379   */
   379    380   struct WhereLoopBuilder {
   380    381     WhereInfo *pWInfo;        /* Information about this WHERE */
   381    382     WhereClause *pWC;         /* WHERE clause terms */
................................................................................
   418    419     int iBreak;               /* Jump here to break out of the loop */
   419    420     int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
   420    421     int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
   421    422     WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
   422    423     WhereClause sWC;          /* Decomposition of the WHERE clause */
   423    424     WhereLevel a[1];          /* Information about each nest loop in WHERE */
   424    425   };
          426  +
          427  +/*
          428  +** Private interfaces - callable only by other where.c routines.
          429  +**
          430  +** where.c:
          431  +*/
          432  +Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
          433  +WhereTerm *sqlite3WhereFindTerm(
          434  +  WhereClause *pWC,     /* The WHERE clause to be searched */
          435  +  int iCur,             /* Cursor number of LHS */
          436  +  int iColumn,          /* Column number of LHS */
          437  +  Bitmask notReady,     /* RHS must not overlap with this mask */
          438  +  u32 op,               /* Mask of WO_xx values describing operator */
          439  +  Index *pIdx           /* Must be compatible with this index, if not NULL */
          440  +);
          441  +
          442  +/* wherecode.c: */
          443  +#ifndef SQLITE_OMIT_EXPLAIN
          444  +int sqlite3WhereExplainOneScan(
          445  +  Parse *pParse,                  /* Parse context */
          446  +  SrcList *pTabList,              /* Table list this loop refers to */
          447  +  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
          448  +  int iLevel,                     /* Value for "level" column of output */
          449  +  int iFrom,                      /* Value for "from" column of output */
          450  +  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
          451  +);
          452  +#else
          453  +# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
          454  +#endif /* SQLITE_OMIT_EXPLAIN */
          455  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
          456  +void sqlite3WhereAddScanStatus(
          457  +  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
          458  +  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
          459  +  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
          460  +  int addrExplain                 /* Address of OP_Explain (or 0) */
          461  +);
          462  +#else
          463  +# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
          464  +#endif
          465  +Bitmask sqlite3WhereCodeOneLoopStart(
          466  +  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
          467  +  int iLevel,          /* Which level of pWInfo->a[] should be coded */
          468  +  Bitmask notReady     /* Which tables are currently available */
          469  +);
          470  +
          471  +/* whereexpr.c: */
          472  +void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
          473  +void sqlite3WhereClauseClear(WhereClause*);
          474  +void sqlite3WhereSplit(WhereClause*,Expr*,u8);
          475  +Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
          476  +Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
          477  +void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
          478  +
          479  +
          480  +
          481  +
   425    482   
   426    483   /*
   427    484   ** Bitmasks for the operators on WhereTerm objects.  These are all
   428    485   ** operators that are of interest to the query planner.  An
   429    486   ** OR-ed combination of these values can be used when searching for
   430    487   ** particular WhereTerms within a WhereClause.
   431    488   */

Added src/wherecode.c.

            1  +/*
            2  +** 2015-06-06
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This module contains C code that generates VDBE code used to process
           13  +** the WHERE clause of SQL statements.
           14  +**
           15  +** This file was split off from where.c on 2015-06-06 in order to reduce the
           16  +** size of where.c and make it easier to edit.  This file contains the routines
           17  +** that actually generate the bulk of the WHERE loop code.  The original where.c
           18  +** file retains the code that does query planning and analysis.
           19  +*/
           20  +#include "sqliteInt.h"
           21  +#include "whereInt.h"
           22  +
           23  +#ifndef SQLITE_OMIT_EXPLAIN
           24  +/*
           25  +** This routine is a helper for explainIndexRange() below
           26  +**
           27  +** pStr holds the text of an expression that we are building up one term
           28  +** at a time.  This routine adds a new term to the end of the expression.
           29  +** Terms are separated by AND so add the "AND" text for second and subsequent
           30  +** terms only.
           31  +*/
           32  +static void explainAppendTerm(
           33  +  StrAccum *pStr,             /* The text expression being built */
           34  +  int iTerm,                  /* Index of this term.  First is zero */
           35  +  const char *zColumn,        /* Name of the column */
           36  +  const char *zOp             /* Name of the operator */
           37  +){
           38  +  if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
           39  +  sqlite3StrAccumAppendAll(pStr, zColumn);
           40  +  sqlite3StrAccumAppend(pStr, zOp, 1);
           41  +  sqlite3StrAccumAppend(pStr, "?", 1);
           42  +}
           43  +
           44  +/*
           45  +** Argument pLevel describes a strategy for scanning table pTab. This 
           46  +** function appends text to pStr that describes the subset of table
           47  +** rows scanned by the strategy in the form of an SQL expression.
           48  +**
           49  +** For example, if the query:
           50  +**
           51  +**   SELECT * FROM t1 WHERE a=1 AND b>2;
           52  +**
           53  +** is run and there is an index on (a, b), then this function returns a
           54  +** string similar to:
           55  +**
           56  +**   "a=? AND b>?"
           57  +*/
           58  +static void explainIndexRange(StrAccum *pStr, WhereLoop *pLoop, Table *pTab){
           59  +  Index *pIndex = pLoop->u.btree.pIndex;
           60  +  u16 nEq = pLoop->u.btree.nEq;
           61  +  u16 nSkip = pLoop->nSkip;
           62  +  int i, j;
           63  +  Column *aCol = pTab->aCol;
           64  +  i16 *aiColumn = pIndex->aiColumn;
           65  +
           66  +  if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
           67  +  sqlite3StrAccumAppend(pStr, " (", 2);
           68  +  for(i=0; i<nEq; i++){
           69  +    char *z = aiColumn[i] < 0 ? "rowid" : aCol[aiColumn[i]].zName;
           70  +    if( i>=nSkip ){
           71  +      explainAppendTerm(pStr, i, z, "=");
           72  +    }else{
           73  +      if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
           74  +      sqlite3XPrintf(pStr, 0, "ANY(%s)", z);
           75  +    }
           76  +  }
           77  +
           78  +  j = i;
           79  +  if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
           80  +    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
           81  +    explainAppendTerm(pStr, i++, z, ">");
           82  +  }
           83  +  if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
           84  +    char *z = aiColumn[j] < 0 ? "rowid" : aCol[aiColumn[j]].zName;
           85  +    explainAppendTerm(pStr, i, z, "<");
           86  +  }
           87  +  sqlite3StrAccumAppend(pStr, ")", 1);
           88  +}
           89  +
           90  +/*
           91  +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
           92  +** command, or if either SQLITE_DEBUG or SQLITE_ENABLE_STMT_SCANSTATUS was
           93  +** defined at compile-time. If it is not a no-op, a single OP_Explain opcode 
           94  +** is added to the output to describe the table scan strategy in pLevel.
           95  +**
           96  +** If an OP_Explain opcode is added to the VM, its address is returned.
           97  +** Otherwise, if no OP_Explain is coded, zero is returned.
           98  +*/
           99  +int sqlite3WhereExplainOneScan(
          100  +  Parse *pParse,                  /* Parse context */
          101  +  SrcList *pTabList,              /* Table list this loop refers to */
          102  +  WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
          103  +  int iLevel,                     /* Value for "level" column of output */
          104  +  int iFrom,                      /* Value for "from" column of output */
          105  +  u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
          106  +){
          107  +  int ret = 0;
          108  +#if !defined(SQLITE_DEBUG) && !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
          109  +  if( pParse->explain==2 )
          110  +#endif
          111  +  {
          112  +    struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
          113  +    Vdbe *v = pParse->pVdbe;      /* VM being constructed */
          114  +    sqlite3 *db = pParse->db;     /* Database handle */
          115  +    int iId = pParse->iSelectId;  /* Select id (left-most output column) */
          116  +    int isSearch;                 /* True for a SEARCH. False for SCAN. */
          117  +    WhereLoop *pLoop;             /* The controlling WhereLoop object */
          118  +    u32 flags;                    /* Flags that describe this loop */
          119  +    char *zMsg;                   /* Text to add to EQP output */
          120  +    StrAccum str;                 /* EQP output string */
          121  +    char zBuf[100];               /* Initial space for EQP output string */
          122  +
          123  +    pLoop = pLevel->pWLoop;
          124  +    flags = pLoop->wsFlags;
          125  +    if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return 0;
          126  +
          127  +    isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
          128  +            || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
          129  +            || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
          130  +
          131  +    sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
          132  +    sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
          133  +    if( pItem->pSelect ){
          134  +      sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
          135  +    }else{
          136  +      sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
          137  +    }
          138  +
          139  +    if( pItem->zAlias ){
          140  +      sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
          141  +    }
          142  +    if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
          143  +      const char *zFmt = 0;
          144  +      Index *pIdx;
          145  +
          146  +      assert( pLoop->u.btree.pIndex!=0 );
          147  +      pIdx = pLoop->u.btree.pIndex;
          148  +      assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
          149  +      if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
          150  +        if( isSearch ){
          151  +          zFmt = "PRIMARY KEY";
          152  +        }
          153  +      }else if( flags & WHERE_PARTIALIDX ){
          154  +        zFmt = "AUTOMATIC PARTIAL COVERING INDEX";
          155  +      }else if( flags & WHERE_AUTO_INDEX ){
          156  +        zFmt = "AUTOMATIC COVERING INDEX";
          157  +      }else if( flags & WHERE_IDX_ONLY ){
          158  +        zFmt = "COVERING INDEX %s";
          159  +      }else{
          160  +        zFmt = "INDEX %s";
          161  +      }
          162  +      if( zFmt ){
          163  +        sqlite3StrAccumAppend(&str, " USING ", 7);
          164  +        sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
          165  +        explainIndexRange(&str, pLoop, pItem->pTab);
          166  +      }
          167  +    }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
          168  +      const char *zRange;
          169  +      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
          170  +        zRange = "(rowid=?)";
          171  +      }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
          172  +        zRange = "(rowid>? AND rowid<?)";
          173  +      }else if( flags&WHERE_BTM_LIMIT ){
          174  +        zRange = "(rowid>?)";
          175  +      }else{
          176  +        assert( flags&WHERE_TOP_LIMIT);
          177  +        zRange = "(rowid<?)";
          178  +      }
          179  +      sqlite3StrAccumAppendAll(&str, " USING INTEGER PRIMARY KEY ");
          180  +      sqlite3StrAccumAppendAll(&str, zRange);
          181  +    }
          182  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          183  +    else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
          184  +      sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
          185  +                  pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
          186  +    }
          187  +#endif
          188  +#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
          189  +    if( pLoop->nOut>=10 ){
          190  +      sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
          191  +    }else{
          192  +      sqlite3StrAccumAppend(&str, " (~1 row)", 9);
          193  +    }
          194  +#endif
          195  +    zMsg = sqlite3StrAccumFinish(&str);
          196  +    ret = sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg,P4_DYNAMIC);
          197  +  }
          198  +  return ret;
          199  +}
          200  +#endif /* SQLITE_OMIT_EXPLAIN */
          201  +
          202  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
          203  +/*
          204  +** Configure the VM passed as the first argument with an
          205  +** sqlite3_stmt_scanstatus() entry corresponding to the scan used to 
          206  +** implement level pLvl. Argument pSrclist is a pointer to the FROM 
          207  +** clause that the scan reads data from.
          208  +**
          209  +** If argument addrExplain is not 0, it must be the address of an 
          210  +** OP_Explain instruction that describes the same loop.
          211  +*/
          212  +void sqlite3WhereAddScanStatus(
          213  +  Vdbe *v,                        /* Vdbe to add scanstatus entry to */
          214  +  SrcList *pSrclist,              /* FROM clause pLvl reads data from */
          215  +  WhereLevel *pLvl,               /* Level to add scanstatus() entry for */
          216  +  int addrExplain                 /* Address of OP_Explain (or 0) */
          217  +){
          218  +  const char *zObj = 0;
          219  +  WhereLoop *pLoop = pLvl->pWLoop;
          220  +  if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0  &&  pLoop->u.btree.pIndex!=0 ){
          221  +    zObj = pLoop->u.btree.pIndex->zName;
          222  +  }else{
          223  +    zObj = pSrclist->a[pLvl->iFrom].zName;
          224  +  }
          225  +  sqlite3VdbeScanStatus(
          226  +      v, addrExplain, pLvl->addrBody, pLvl->addrVisit, pLoop->nOut, zObj
          227  +  );
          228  +}
          229  +#endif
          230  +
          231  +
          232  +/*
          233  +** Disable a term in the WHERE clause.  Except, do not disable the term
          234  +** if it controls a LEFT OUTER JOIN and it did not originate in the ON
          235  +** or USING clause of that join.
          236  +**
          237  +** Consider the term t2.z='ok' in the following queries:
          238  +**
          239  +**   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
          240  +**   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
          241  +**   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
          242  +**
          243  +** The t2.z='ok' is disabled in the in (2) because it originates
          244  +** in the ON clause.  The term is disabled in (3) because it is not part
          245  +** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
          246  +**
          247  +** Disabling a term causes that term to not be tested in the inner loop
          248  +** of the join.  Disabling is an optimization.  When terms are satisfied
          249  +** by indices, we disable them to prevent redundant tests in the inner
          250  +** loop.  We would get the correct results if nothing were ever disabled,
          251  +** but joins might run a little slower.  The trick is to disable as much
          252  +** as we can without disabling too much.  If we disabled in (1), we'd get
          253  +** the wrong answer.  See ticket #813.
          254  +**
          255  +** If all the children of a term are disabled, then that term is also
          256  +** automatically disabled.  In this way, terms get disabled if derived
          257  +** virtual terms are tested first.  For example:
          258  +**
          259  +**      x GLOB 'abc*' AND x>='abc' AND x<'acd'
          260  +**      \___________/     \______/     \_____/
          261  +**         parent          child1       child2
          262  +**
          263  +** Only the parent term was in the original WHERE clause.  The child1
          264  +** and child2 terms were added by the LIKE optimization.  If both of
          265  +** the virtual child terms are valid, then testing of the parent can be 
          266  +** skipped.
          267  +**
          268  +** Usually the parent term is marked as TERM_CODED.  But if the parent
          269  +** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
          270  +** The TERM_LIKECOND marking indicates that the term should be coded inside
          271  +** a conditional such that is only evaluated on the second pass of a
          272  +** LIKE-optimization loop, when scanning BLOBs instead of strings.
          273  +*/
          274  +static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
          275  +  int nLoop = 0;
          276  +  while( pTerm
          277  +      && (pTerm->wtFlags & TERM_CODED)==0
          278  +      && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
          279  +      && (pLevel->notReady & pTerm->prereqAll)==0
          280  +  ){
          281  +    if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
          282  +      pTerm->wtFlags |= TERM_LIKECOND;
          283  +    }else{
          284  +      pTerm->wtFlags |= TERM_CODED;
          285  +    }
          286  +    if( pTerm->iParent<0 ) break;
          287  +    pTerm = &pTerm->pWC->a[pTerm->iParent];
          288  +    pTerm->nChild--;
          289  +    if( pTerm->nChild!=0 ) break;
          290  +    nLoop++;
          291  +  }
          292  +}
          293  +
          294  +/*
          295  +** Code an OP_Affinity opcode to apply the column affinity string zAff
          296  +** to the n registers starting at base. 
          297  +**
          298  +** As an optimization, SQLITE_AFF_BLOB entries (which are no-ops) at the
          299  +** beginning and end of zAff are ignored.  If all entries in zAff are
          300  +** SQLITE_AFF_BLOB, then no code gets generated.
          301  +**
          302  +** This routine makes its own copy of zAff so that the caller is free
          303  +** to modify zAff after this routine returns.
          304  +*/
          305  +static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
          306  +  Vdbe *v = pParse->pVdbe;
          307  +  if( zAff==0 ){
          308  +    assert( pParse->db->mallocFailed );
          309  +    return;
          310  +  }
          311  +  assert( v!=0 );
          312  +
          313  +  /* Adjust base and n to skip over SQLITE_AFF_BLOB entries at the beginning
          314  +  ** and end of the affinity string.
          315  +  */
          316  +  while( n>0 && zAff[0]==SQLITE_AFF_BLOB ){
          317  +    n--;
          318  +    base++;
          319  +    zAff++;
          320  +  }
          321  +  while( n>1 && zAff[n-1]==SQLITE_AFF_BLOB ){
          322  +    n--;
          323  +  }
          324  +
          325  +  /* Code the OP_Affinity opcode if there is anything left to do. */
          326  +  if( n>0 ){
          327  +    sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
          328  +    sqlite3VdbeChangeP4(v, -1, zAff, n);
          329  +    sqlite3ExprCacheAffinityChange(pParse, base, n);
          330  +  }
          331  +}
          332  +
          333  +
          334  +/*
          335  +** Generate code for a single equality term of the WHERE clause.  An equality
          336  +** term can be either X=expr or X IN (...).   pTerm is the term to be 
          337  +** coded.
          338  +**
          339  +** The current value for the constraint is left in register iReg.
          340  +**
          341  +** For a constraint of the form X=expr, the expression is evaluated and its
          342  +** result is left on the stack.  For constraints of the form X IN (...)
          343  +** this routine sets up a loop that will iterate over all values of X.
          344  +*/
          345  +static int codeEqualityTerm(
          346  +  Parse *pParse,      /* The parsing context */
          347  +  WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
          348  +  WhereLevel *pLevel, /* The level of the FROM clause we are working on */
          349  +  int iEq,            /* Index of the equality term within this level */
          350  +  int bRev,           /* True for reverse-order IN operations */
          351  +  int iTarget         /* Attempt to leave results in this register */
          352  +){
          353  +  Expr *pX = pTerm->pExpr;
          354  +  Vdbe *v = pParse->pVdbe;
          355  +  int iReg;                  /* Register holding results */
          356  +
          357  +  assert( iTarget>0 );
          358  +  if( pX->op==TK_EQ || pX->op==TK_IS ){
          359  +    iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
          360  +  }else if( pX->op==TK_ISNULL ){
          361  +    iReg = iTarget;
          362  +    sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
          363  +#ifndef SQLITE_OMIT_SUBQUERY
          364  +  }else{
          365  +    int eType;
          366  +    int iTab;
          367  +    struct InLoop *pIn;
          368  +    WhereLoop *pLoop = pLevel->pWLoop;
          369  +
          370  +    if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
          371  +      && pLoop->u.btree.pIndex!=0
          372  +      && pLoop->u.btree.pIndex->aSortOrder[iEq]
          373  +    ){
          374  +      testcase( iEq==0 );
          375  +      testcase( bRev );
          376  +      bRev = !bRev;
          377  +    }
          378  +    assert( pX->op==TK_IN );
          379  +    iReg = iTarget;
          380  +    eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0);
          381  +    if( eType==IN_INDEX_INDEX_DESC ){
          382  +      testcase( bRev );
          383  +      bRev = !bRev;
          384  +    }
          385  +    iTab = pX->iTable;
          386  +    sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
          387  +    VdbeCoverageIf(v, bRev);
          388  +    VdbeCoverageIf(v, !bRev);
          389  +    assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
          390  +    pLoop->wsFlags |= WHERE_IN_ABLE;
          391  +    if( pLevel->u.in.nIn==0 ){
          392  +      pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
          393  +    }
          394  +    pLevel->u.in.nIn++;
          395  +    pLevel->u.in.aInLoop =
          396  +       sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
          397  +                              sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
          398  +    pIn = pLevel->u.in.aInLoop;
          399  +    if( pIn ){
          400  +      pIn += pLevel->u.in.nIn - 1;
          401  +      pIn->iCur = iTab;
          402  +      if( eType==IN_INDEX_ROWID ){
          403  +        pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
          404  +      }else{
          405  +        pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
          406  +      }
          407  +      pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
          408  +      sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
          409  +    }else{
          410  +      pLevel->u.in.nIn = 0;
          411  +    }
          412  +#endif
          413  +  }
          414  +  disableTerm(pLevel, pTerm);
          415  +  return iReg;
          416  +}
          417  +
          418  +/*
          419  +** Generate code that will evaluate all == and IN constraints for an
          420  +** index scan.
          421  +**
          422  +** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
          423  +** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
          424  +** The index has as many as three equality constraints, but in this
          425  +** example, the third "c" value is an inequality.  So only two 
          426  +** constraints are coded.  This routine will generate code to evaluate
          427  +** a==5 and b IN (1,2,3).  The current values for a and b will be stored
          428  +** in consecutive registers and the index of the first register is returned.
          429  +**
          430  +** In the example above nEq==2.  But this subroutine works for any value
          431  +** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
          432  +** The only thing it does is allocate the pLevel->iMem memory cell and
          433  +** compute the affinity string.
          434  +**
          435  +** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
          436  +** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
          437  +** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
          438  +** occurs after the nEq quality constraints.
          439  +**
          440  +** This routine allocates a range of nEq+nExtraReg memory cells and returns
          441  +** the index of the first memory cell in that range. The code that
          442  +** calls this routine will use that memory range to store keys for
          443  +** start and termination conditions of the loop.
          444  +** key value of the loop.  If one or more IN operators appear, then
          445  +** this routine allocates an additional nEq memory cells for internal
          446  +** use.
          447  +**
          448  +** Before returning, *pzAff is set to point to a buffer containing a
          449  +** copy of the column affinity string of the index allocated using
          450  +** sqlite3DbMalloc(). Except, entries in the copy of the string associated
          451  +** with equality constraints that use BLOB or NONE affinity are set to
          452  +** SQLITE_AFF_BLOB. This is to deal with SQL such as the following:
          453  +**
          454  +**   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
          455  +**   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
          456  +**
          457  +** In the example above, the index on t1(a) has TEXT affinity. But since
          458  +** the right hand side of the equality constraint (t2.b) has BLOB/NONE affinity,
          459  +** no conversion should be attempted before using a t2.b value as part of
          460  +** a key to search the index. Hence the first byte in the returned affinity
          461  +** string in this example would be set to SQLITE_AFF_BLOB.
          462  +*/
          463  +static int codeAllEqualityTerms(
          464  +  Parse *pParse,        /* Parsing context */
          465  +  WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
          466  +  int bRev,             /* Reverse the order of IN operators */
          467  +  int nExtraReg,        /* Number of extra registers to allocate */
          468  +  char **pzAff          /* OUT: Set to point to affinity string */
          469  +){
          470  +  u16 nEq;                      /* The number of == or IN constraints to code */
          471  +  u16 nSkip;                    /* Number of left-most columns to skip */
          472  +  Vdbe *v = pParse->pVdbe;      /* The vm under construction */
          473  +  Index *pIdx;                  /* The index being used for this loop */
          474  +  WhereTerm *pTerm;             /* A single constraint term */
          475  +  WhereLoop *pLoop;             /* The WhereLoop object */
          476  +  int j;                        /* Loop counter */
          477  +  int regBase;                  /* Base register */
          478  +  int nReg;                     /* Number of registers to allocate */
          479  +  char *zAff;                   /* Affinity string to return */
          480  +
          481  +  /* This module is only called on query plans that use an index. */
          482  +  pLoop = pLevel->pWLoop;
          483  +  assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
          484  +  nEq = pLoop->u.btree.nEq;
          485  +  nSkip = pLoop->nSkip;
          486  +  pIdx = pLoop->u.btree.pIndex;
          487  +  assert( pIdx!=0 );
          488  +
          489  +  /* Figure out how many memory cells we will need then allocate them.
          490  +  */
          491  +  regBase = pParse->nMem + 1;
          492  +  nReg = pLoop->u.btree.nEq + nExtraReg;
          493  +  pParse->nMem += nReg;
          494  +
          495  +  zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
          496  +  if( !zAff ){
          497  +    pParse->db->mallocFailed = 1;
          498  +  }
          499  +
          500  +  if( nSkip ){
          501  +    int iIdxCur = pLevel->iIdxCur;
          502  +    sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
          503  +    VdbeCoverageIf(v, bRev==0);
          504  +    VdbeCoverageIf(v, bRev!=0);
          505  +    VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
          506  +    j = sqlite3VdbeAddOp0(v, OP_Goto);
          507  +    pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
          508  +                            iIdxCur, 0, regBase, nSkip);
          509  +    VdbeCoverageIf(v, bRev==0);
          510  +    VdbeCoverageIf(v, bRev!=0);
          511  +    sqlite3VdbeJumpHere(v, j);
          512  +    for(j=0; j<nSkip; j++){
          513  +      sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
          514  +      assert( pIdx->aiColumn[j]>=0 );
          515  +      VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
          516  +    }
          517  +  }    
          518  +
          519  +  /* Evaluate the equality constraints
          520  +  */
          521  +  assert( zAff==0 || (int)strlen(zAff)>=nEq );
          522  +  for(j=nSkip; j<nEq; j++){
          523  +    int r1;
          524  +    pTerm = pLoop->aLTerm[j];
          525  +    assert( pTerm!=0 );
          526  +    /* The following testcase is true for indices with redundant columns. 
          527  +    ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
          528  +    testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
          529  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
          530  +    r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
          531  +    if( r1!=regBase+j ){
          532  +      if( nReg==1 ){
          533  +        sqlite3ReleaseTempReg(pParse, regBase);
          534  +        regBase = r1;
          535  +      }else{
          536  +        sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
          537  +      }
          538  +    }
          539  +    testcase( pTerm->eOperator & WO_ISNULL );
          540  +    testcase( pTerm->eOperator & WO_IN );
          541  +    if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
          542  +      Expr *pRight = pTerm->pExpr->pRight;
          543  +      if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){
          544  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
          545  +        VdbeCoverage(v);
          546  +      }
          547  +      if( zAff ){
          548  +        if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){
          549  +          zAff[j] = SQLITE_AFF_BLOB;
          550  +        }
          551  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
          552  +          zAff[j] = SQLITE_AFF_BLOB;
          553  +        }
          554  +      }
          555  +    }
          556  +  }
          557  +  *pzAff = zAff;
          558  +  return regBase;
          559  +}
          560  +
          561  +/*
          562  +** If the most recently coded instruction is a constant range contraint
          563  +** that originated from the LIKE optimization, then change the P3 to be
          564  +** pLoop->iLikeRepCntr and set P5.
          565  +**
          566  +** The LIKE optimization trys to evaluate "x LIKE 'abc%'" as a range
          567  +** expression: "x>='ABC' AND x<'abd'".  But this requires that the range
          568  +** scan loop run twice, once for strings and a second time for BLOBs.
          569  +** The OP_String opcodes on the second pass convert the upper and lower
          570  +** bound string contants to blobs.  This routine makes the necessary changes
          571  +** to the OP_String opcodes for that to happen.
          572  +*/
          573  +static void whereLikeOptimizationStringFixup(
          574  +  Vdbe *v,                /* prepared statement under construction */
          575  +  WhereLevel *pLevel,     /* The loop that contains the LIKE operator */
          576  +  WhereTerm *pTerm        /* The upper or lower bound just coded */
          577  +){
          578  +  if( pTerm->wtFlags & TERM_LIKEOPT ){
          579  +    VdbeOp *pOp;
          580  +    assert( pLevel->iLikeRepCntr>0 );
          581  +    pOp = sqlite3VdbeGetOp(v, -1);
          582  +    assert( pOp!=0 );
          583  +    assert( pOp->opcode==OP_String8 
          584  +            || pTerm->pWC->pWInfo->pParse->db->mallocFailed );
          585  +    pOp->p3 = pLevel->iLikeRepCntr;
          586  +    pOp->p5 = 1;
          587  +  }
          588  +}
          589  +
          590  +
          591  +/*
          592  +** Generate code for the start of the iLevel-th loop in the WHERE clause
          593  +** implementation described by pWInfo.
          594  +*/
          595  +Bitmask sqlite3WhereCodeOneLoopStart(
          596  +  WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
          597  +  int iLevel,          /* Which level of pWInfo->a[] should be coded */
          598  +  Bitmask notReady     /* Which tables are currently available */
          599  +){
          600  +  int j, k;            /* Loop counters */
          601  +  int iCur;            /* The VDBE cursor for the table */
          602  +  int addrNxt;         /* Where to jump to continue with the next IN case */
          603  +  int omitTable;       /* True if we use the index only */
          604  +  int bRev;            /* True if we need to scan in reverse order */
          605  +  WhereLevel *pLevel;  /* The where level to be coded */
          606  +  WhereLoop *pLoop;    /* The WhereLoop object being coded */
          607  +  WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
          608  +  WhereTerm *pTerm;               /* A WHERE clause term */
          609  +  Parse *pParse;                  /* Parsing context */
          610  +  sqlite3 *db;                    /* Database connection */
          611  +  Vdbe *v;                        /* The prepared stmt under constructions */
          612  +  struct SrcList_item *pTabItem;  /* FROM clause term being coded */
          613  +  int addrBrk;                    /* Jump here to break out of the loop */
          614  +  int addrCont;                   /* Jump here to continue with next cycle */
          615  +  int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
          616  +  int iReleaseReg = 0;      /* Temp register to free before returning */
          617  +
          618  +  pParse = pWInfo->pParse;
          619  +  v = pParse->pVdbe;
          620  +  pWC = &pWInfo->sWC;
          621  +  db = pParse->db;
          622  +  pLevel = &pWInfo->a[iLevel];
          623  +  pLoop = pLevel->pWLoop;
          624  +  pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
          625  +  iCur = pTabItem->iCursor;
          626  +  pLevel->notReady = notReady & ~sqlite3WhereGetMask(&pWInfo->sMaskSet, iCur);
          627  +  bRev = (pWInfo->revMask>>iLevel)&1;
          628  +  omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0 
          629  +           && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
          630  +  VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
          631  +
          632  +  /* Create labels for the "break" and "continue" instructions
          633  +  ** for the current loop.  Jump to addrBrk to break out of a loop.
          634  +  ** Jump to cont to go immediately to the next iteration of the
          635  +  ** loop.
          636  +  **
          637  +  ** When there is an IN operator, we also have a "addrNxt" label that
          638  +  ** means to continue with the next IN value combination.  When
          639  +  ** there are no IN operators in the constraints, the "addrNxt" label
          640  +  ** is the same as "addrBrk".
          641  +  */
          642  +  addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
          643  +  addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
          644  +
          645  +  /* If this is the right table of a LEFT OUTER JOIN, allocate and
          646  +  ** initialize a memory cell that records if this table matches any
          647  +  ** row of the left table of the join.
          648  +  */
          649  +  if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
          650  +    pLevel->iLeftJoin = ++pParse->nMem;
          651  +    sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
          652  +    VdbeComment((v, "init LEFT JOIN no-match flag"));
          653  +  }
          654  +
          655  +  /* Special case of a FROM clause subquery implemented as a co-routine */
          656  +  if( pTabItem->viaCoroutine ){
          657  +    int regYield = pTabItem->regReturn;
          658  +    sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
          659  +    pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
          660  +    VdbeCoverage(v);
          661  +    VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
          662  +    pLevel->op = OP_Goto;
          663  +  }else
          664  +
          665  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          666  +  if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
          667  +    /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
          668  +    **          to access the data.
          669  +    */
          670  +    int iReg;   /* P3 Value for OP_VFilter */
          671  +    int addrNotFound;
          672  +    int nConstraint = pLoop->nLTerm;
          673  +
          674  +    sqlite3ExprCachePush(pParse);
          675  +    iReg = sqlite3GetTempRange(pParse, nConstraint+2);
          676  +    addrNotFound = pLevel->addrBrk;
          677  +    for(j=0; j<nConstraint; j++){
          678  +      int iTarget = iReg+j+2;
          679  +      pTerm = pLoop->aLTerm[j];
          680  +      if( pTerm==0 ) continue;
          681  +      if( pTerm->eOperator & WO_IN ){
          682  +        codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
          683  +        addrNotFound = pLevel->addrNxt;
          684  +      }else{
          685  +        sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
          686  +      }
          687  +    }
          688  +    sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
          689  +    sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
          690  +    sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
          691  +                      pLoop->u.vtab.idxStr,
          692  +                      pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
          693  +    VdbeCoverage(v);
          694  +    pLoop->u.vtab.needFree = 0;
          695  +    for(j=0; j<nConstraint && j<16; j++){
          696  +      if( (pLoop->u.vtab.omitMask>>j)&1 ){
          697  +        disableTerm(pLevel, pLoop->aLTerm[j]);
          698  +      }
          699  +    }
          700  +    pLevel->op = OP_VNext;
          701  +    pLevel->p1 = iCur;
          702  +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
          703  +    sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
          704  +    sqlite3ExprCachePop(pParse);
          705  +  }else
          706  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
          707  +
          708  +  if( (pLoop->wsFlags & WHERE_IPK)!=0
          709  +   && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
          710  +  ){
          711  +    /* Case 2:  We can directly reference a single row using an
          712  +    **          equality comparison against the ROWID field.  Or
          713  +    **          we reference multiple rows using a "rowid IN (...)"
          714  +    **          construct.
          715  +    */
          716  +    assert( pLoop->u.btree.nEq==1 );
          717  +    pTerm = pLoop->aLTerm[0];
          718  +    assert( pTerm!=0 );
          719  +    assert( pTerm->pExpr!=0 );
          720  +    assert( omitTable==0 );
          721  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
          722  +    iReleaseReg = ++pParse->nMem;
          723  +    iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
          724  +    if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
          725  +    addrNxt = pLevel->addrNxt;
          726  +    sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
          727  +    sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
          728  +    VdbeCoverage(v);
          729  +    sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
          730  +    sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
          731  +    VdbeComment((v, "pk"));
          732  +    pLevel->op = OP_Noop;
          733  +  }else if( (pLoop->wsFlags & WHERE_IPK)!=0
          734  +         && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
          735  +  ){
          736  +    /* Case 3:  We have an inequality comparison against the ROWID field.
          737  +    */
          738  +    int testOp = OP_Noop;
          739  +    int start;
          740  +    int memEndValue = 0;
          741  +    WhereTerm *pStart, *pEnd;
          742  +
          743  +    assert( omitTable==0 );
          744  +    j = 0;
          745  +    pStart = pEnd = 0;
          746  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
          747  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
          748  +    assert( pStart!=0 || pEnd!=0 );
          749  +    if( bRev ){
          750  +      pTerm = pStart;
          751  +      pStart = pEnd;
          752  +      pEnd = pTerm;
          753  +    }
          754  +    if( pStart ){
          755  +      Expr *pX;             /* The expression that defines the start bound */
          756  +      int r1, rTemp;        /* Registers for holding the start boundary */
          757  +
          758  +      /* The following constant maps TK_xx codes into corresponding 
          759  +      ** seek opcodes.  It depends on a particular ordering of TK_xx
          760  +      */
          761  +      const u8 aMoveOp[] = {
          762  +           /* TK_GT */  OP_SeekGT,
          763  +           /* TK_LE */  OP_SeekLE,
          764  +           /* TK_LT */  OP_SeekLT,
          765  +           /* TK_GE */  OP_SeekGE
          766  +      };
          767  +      assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
          768  +      assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
          769  +      assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
          770  +
          771  +      assert( (pStart->wtFlags & TERM_VNULL)==0 );
          772  +      testcase( pStart->wtFlags & TERM_VIRTUAL );
          773  +      pX = pStart->pExpr;
          774  +      assert( pX!=0 );
          775  +      testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
          776  +      r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
          777  +      sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
          778  +      VdbeComment((v, "pk"));
          779  +      VdbeCoverageIf(v, pX->op==TK_GT);
          780  +      VdbeCoverageIf(v, pX->op==TK_LE);
          781  +      VdbeCoverageIf(v, pX->op==TK_LT);
          782  +      VdbeCoverageIf(v, pX->op==TK_GE);
          783  +      sqlite3ExprCacheAffinityChange(pParse, r1, 1);
          784  +      sqlite3ReleaseTempReg(pParse, rTemp);
          785  +      disableTerm(pLevel, pStart);
          786  +    }else{
          787  +      sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
          788  +      VdbeCoverageIf(v, bRev==0);
          789  +      VdbeCoverageIf(v, bRev!=0);
          790  +    }
          791  +    if( pEnd ){
          792  +      Expr *pX;
          793  +      pX = pEnd->pExpr;
          794  +      assert( pX!=0 );
          795  +      assert( (pEnd->wtFlags & TERM_VNULL)==0 );
          796  +      testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
          797  +      testcase( pEnd->wtFlags & TERM_VIRTUAL );
          798  +      memEndValue = ++pParse->nMem;
          799  +      sqlite3ExprCode(pParse, pX->pRight, memEndValue);
          800  +      if( pX->op==TK_LT || pX->op==TK_GT ){
          801  +        testOp = bRev ? OP_Le : OP_Ge;
          802  +      }else{
          803  +        testOp = bRev ? OP_Lt : OP_Gt;
          804  +      }
          805  +      disableTerm(pLevel, pEnd);
          806  +    }
          807  +    start = sqlite3VdbeCurrentAddr(v);
          808  +    pLevel->op = bRev ? OP_Prev : OP_Next;
          809  +    pLevel->p1 = iCur;
          810  +    pLevel->p2 = start;
          811  +    assert( pLevel->p5==0 );
          812  +    if( testOp!=OP_Noop ){
          813  +      iRowidReg = ++pParse->nMem;
          814  +      sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
          815  +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
          816  +      sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
          817  +      VdbeCoverageIf(v, testOp==OP_Le);
          818  +      VdbeCoverageIf(v, testOp==OP_Lt);
          819  +      VdbeCoverageIf(v, testOp==OP_Ge);
          820  +      VdbeCoverageIf(v, testOp==OP_Gt);
          821  +      sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
          822  +    }
          823  +  }else if( pLoop->wsFlags & WHERE_INDEXED ){
          824  +    /* Case 4: A scan using an index.
          825  +    **
          826  +    **         The WHERE clause may contain zero or more equality 
          827  +    **         terms ("==" or "IN" operators) that refer to the N
          828  +    **         left-most columns of the index. It may also contain
          829  +    **         inequality constraints (>, <, >= or <=) on the indexed
          830  +    **         column that immediately follows the N equalities. Only 
          831  +    **         the right-most column can be an inequality - the rest must
          832  +    **         use the "==" and "IN" operators. For example, if the 
          833  +    **         index is on (x,y,z), then the following clauses are all 
          834  +    **         optimized:
          835  +    **
          836  +    **            x=5
          837  +    **            x=5 AND y=10
          838  +    **            x=5 AND y<10
          839  +    **            x=5 AND y>5 AND y<10
          840  +    **            x=5 AND y=5 AND z<=10
          841  +    **
          842  +    **         The z<10 term of the following cannot be used, only
          843  +    **         the x=5 term:
          844  +    **
          845  +    **            x=5 AND z<10
          846  +    **
          847  +    **         N may be zero if there are inequality constraints.
          848  +    **         If there are no inequality constraints, then N is at
          849  +    **         least one.
          850  +    **
          851  +    **         This case is also used when there are no WHERE clause
          852  +    **         constraints but an index is selected anyway, in order
          853  +    **         to force the output order to conform to an ORDER BY.
          854  +    */  
          855  +    static const u8 aStartOp[] = {
          856  +      0,
          857  +      0,
          858  +      OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
          859  +      OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
          860  +      OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
          861  +      OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
          862  +      OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
          863  +      OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
          864  +    };
          865  +    static const u8 aEndOp[] = {
          866  +      OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
          867  +      OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
          868  +      OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
          869  +      OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
          870  +    };
          871  +    u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
          872  +    int regBase;                 /* Base register holding constraint values */
          873  +    WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
          874  +    WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
          875  +    int startEq;                 /* True if range start uses ==, >= or <= */
          876  +    int endEq;                   /* True if range end uses ==, >= or <= */
          877  +    int start_constraints;       /* Start of range is constrained */
          878  +    int nConstraint;             /* Number of constraint terms */
          879  +    Index *pIdx;                 /* The index we will be using */
          880  +    int iIdxCur;                 /* The VDBE cursor for the index */
          881  +    int nExtraReg = 0;           /* Number of extra registers needed */
          882  +    int op;                      /* Instruction opcode */
          883  +    char *zStartAff;             /* Affinity for start of range constraint */
          884  +    char cEndAff = 0;            /* Affinity for end of range constraint */
          885  +    u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
          886  +    u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
          887  +
          888  +    pIdx = pLoop->u.btree.pIndex;
          889  +    iIdxCur = pLevel->iIdxCur;
          890  +    assert( nEq>=pLoop->nSkip );
          891  +
          892  +    /* If this loop satisfies a sort order (pOrderBy) request that 
          893  +    ** was passed to this function to implement a "SELECT min(x) ..." 
          894  +    ** query, then the caller will only allow the loop to run for
          895  +    ** a single iteration. This means that the first row returned
          896  +    ** should not have a NULL value stored in 'x'. If column 'x' is
          897  +    ** the first one after the nEq equality constraints in the index,
          898  +    ** this requires some special handling.
          899  +    */
          900  +    assert( pWInfo->pOrderBy==0
          901  +         || pWInfo->pOrderBy->nExpr==1
          902  +         || (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)==0 );
          903  +    if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
          904  +     && pWInfo->nOBSat>0
          905  +     && (pIdx->nKeyCol>nEq)
          906  +    ){
          907  +      assert( pLoop->nSkip==0 );
          908  +      bSeekPastNull = 1;
          909  +      nExtraReg = 1;
          910  +    }
          911  +
          912  +    /* Find any inequality constraint terms for the start and end 
          913  +    ** of the range. 
          914  +    */
          915  +    j = nEq;
          916  +    if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
          917  +      pRangeStart = pLoop->aLTerm[j++];
          918  +      nExtraReg = 1;
          919  +      /* Like optimization range constraints always occur in pairs */
          920  +      assert( (pRangeStart->wtFlags & TERM_LIKEOPT)==0 || 
          921  +              (pLoop->wsFlags & WHERE_TOP_LIMIT)!=0 );
          922  +    }
          923  +    if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
          924  +      pRangeEnd = pLoop->aLTerm[j++];
          925  +      nExtraReg = 1;
          926  +      if( (pRangeEnd->wtFlags & TERM_LIKEOPT)!=0 ){
          927  +        assert( pRangeStart!=0 );                     /* LIKE opt constraints */
          928  +        assert( pRangeStart->wtFlags & TERM_LIKEOPT );   /* occur in pairs */
          929  +        pLevel->iLikeRepCntr = ++pParse->nMem;
          930  +        testcase( bRev );
          931  +        testcase( pIdx->aSortOrder[nEq]==SQLITE_SO_DESC );
          932  +        sqlite3VdbeAddOp2(v, OP_Integer,
          933  +                          bRev ^ (pIdx->aSortOrder[nEq]==SQLITE_SO_DESC),
          934  +                          pLevel->iLikeRepCntr);
          935  +        VdbeComment((v, "LIKE loop counter"));
          936  +        pLevel->addrLikeRep = sqlite3VdbeCurrentAddr(v);
          937  +      }
          938  +      if( pRangeStart==0
          939  +       && (j = pIdx->aiColumn[nEq])>=0 
          940  +       && pIdx->pTable->aCol[j].notNull==0
          941  +      ){
          942  +        bSeekPastNull = 1;
          943  +      }
          944  +    }
          945  +    assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
          946  +
          947  +    /* Generate code to evaluate all constraint terms using == or IN
          948  +    ** and store the values of those terms in an array of registers
          949  +    ** starting at regBase.
          950  +    */
          951  +    regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
          952  +    assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
          953  +    if( zStartAff ) cEndAff = zStartAff[nEq];
          954  +    addrNxt = pLevel->addrNxt;
          955  +
          956  +    /* If we are doing a reverse order scan on an ascending index, or
          957  +    ** a forward order scan on a descending index, interchange the 
          958  +    ** start and end terms (pRangeStart and pRangeEnd).
          959  +    */
          960  +    if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
          961  +     || (bRev && pIdx->nKeyCol==nEq)
          962  +    ){
          963  +      SWAP(WhereTerm *, pRangeEnd, pRangeStart);
          964  +      SWAP(u8, bSeekPastNull, bStopAtNull);
          965  +    }
          966  +
          967  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
          968  +    testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
          969  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
          970  +    testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
          971  +    startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
          972  +    endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
          973  +    start_constraints = pRangeStart || nEq>0;
          974  +
          975  +    /* Seek the index cursor to the start of the range. */
          976  +    nConstraint = nEq;
          977  +    if( pRangeStart ){
          978  +      Expr *pRight = pRangeStart->pExpr->pRight;
          979  +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
          980  +      whereLikeOptimizationStringFixup(v, pLevel, pRangeStart);
          981  +      if( (pRangeStart->wtFlags & TERM_VNULL)==0
          982  +       && sqlite3ExprCanBeNull(pRight)
          983  +      ){
          984  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
          985  +        VdbeCoverage(v);
          986  +      }
          987  +      if( zStartAff ){
          988  +        if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_BLOB){
          989  +          /* Since the comparison is to be performed with no conversions
          990  +          ** applied to the operands, set the affinity to apply to pRight to 
          991  +          ** SQLITE_AFF_BLOB.  */
          992  +          zStartAff[nEq] = SQLITE_AFF_BLOB;
          993  +        }
          994  +        if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
          995  +          zStartAff[nEq] = SQLITE_AFF_BLOB;
          996  +        }
          997  +      }  
          998  +      nConstraint++;
          999  +      testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
         1000  +    }else if( bSeekPastNull ){
         1001  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
         1002  +      nConstraint++;
         1003  +      startEq = 0;
         1004  +      start_constraints = 1;
         1005  +    }
         1006  +    codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
         1007  +    op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
         1008  +    assert( op!=0 );
         1009  +    sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
         1010  +    VdbeCoverage(v);
         1011  +    VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
         1012  +    VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
         1013  +    VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
         1014  +    VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
         1015  +    VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
         1016  +    VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
         1017  +
         1018  +    /* Load the value for the inequality constraint at the end of the
         1019  +    ** range (if any).
         1020  +    */
         1021  +    nConstraint = nEq;
         1022  +    if( pRangeEnd ){
         1023  +      Expr *pRight = pRangeEnd->pExpr->pRight;
         1024  +      sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
         1025  +      sqlite3ExprCode(pParse, pRight, regBase+nEq);
         1026  +      whereLikeOptimizationStringFixup(v, pLevel, pRangeEnd);
         1027  +      if( (pRangeEnd->wtFlags & TERM_VNULL)==0
         1028  +       && sqlite3ExprCanBeNull(pRight)
         1029  +      ){
         1030  +        sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
         1031  +        VdbeCoverage(v);
         1032  +      }
         1033  +      if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_BLOB
         1034  +       && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
         1035  +      ){
         1036  +        codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
         1037  +      }
         1038  +      nConstraint++;
         1039  +      testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
         1040  +    }else if( bStopAtNull ){
         1041  +      sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
         1042  +      endEq = 0;
         1043  +      nConstraint++;
         1044  +    }
         1045  +    sqlite3DbFree(db, zStartAff);
         1046  +
         1047  +    /* Top of the loop body */
         1048  +    pLevel->p2 = sqlite3VdbeCurrentAddr(v);
         1049  +
         1050  +    /* Check if the index cursor is past the end of the range. */
         1051  +    if( nConstraint ){
         1052  +      op = aEndOp[bRev*2 + endEq];
         1053  +      sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
         1054  +      testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
         1055  +      testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
         1056  +      testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
         1057  +      testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
         1058  +    }
         1059  +
         1060  +    /* Seek the table cursor, if required */
         1061  +    disableTerm(pLevel, pRangeStart);
         1062  +    disableTerm(pLevel, pRangeEnd);
         1063  +    if( omitTable ){
         1064  +      /* pIdx is a covering index.  No need to access the main table. */
         1065  +    }else if( HasRowid(pIdx->pTable) ){
         1066  +      iRowidReg = ++pParse->nMem;
         1067  +      sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
         1068  +      sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
         1069  +      sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
         1070  +    }else if( iCur!=iIdxCur ){
         1071  +      Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
         1072  +      iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
         1073  +      for(j=0; j<pPk->nKeyCol; j++){
         1074  +        k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
         1075  +        sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
         1076  +      }
         1077  +      sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
         1078  +                           iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
         1079  +    }
         1080  +
         1081  +    /* Record the instruction used to terminate the loop. Disable 
         1082  +    ** WHERE clause terms made redundant by the index range scan.
         1083  +    */
         1084  +    if( pLoop->wsFlags & WHERE_ONEROW ){
         1085  +      pLevel->op = OP_Noop;
         1086  +    }else if( bRev ){
         1087  +      pLevel->op = OP_Prev;
         1088  +    }else{
         1089  +      pLevel->op = OP_Next;
         1090  +    }
         1091  +    pLevel->p1 = iIdxCur;
         1092  +    pLevel->p3 = (pLoop->wsFlags&WHERE_UNQ_WANTED)!=0 ? 1:0;
         1093  +    if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
         1094  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         1095  +    }else{
         1096  +      assert( pLevel->p5==0 );
         1097  +    }
         1098  +  }else
         1099  +
         1100  +#ifndef SQLITE_OMIT_OR_OPTIMIZATION
         1101  +  if( pLoop->wsFlags & WHERE_MULTI_OR ){
         1102  +    /* Case 5:  Two or more separately indexed terms connected by OR
         1103  +    **
         1104  +    ** Example:
         1105  +    **
         1106  +    **   CREATE TABLE t1(a,b,c,d);
         1107  +    **   CREATE INDEX i1 ON t1(a);
         1108  +    **   CREATE INDEX i2 ON t1(b);
         1109  +    **   CREATE INDEX i3 ON t1(c);
         1110  +    **
         1111  +    **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
         1112  +    **
         1113  +    ** In the example, there are three indexed terms connected by OR.
         1114  +    ** The top of the loop looks like this:
         1115  +    **
         1116  +    **          Null       1                # Zero the rowset in reg 1
         1117  +    **
         1118  +    ** Then, for each indexed term, the following. The arguments to
         1119  +    ** RowSetTest are such that the rowid of the current row is inserted
         1120  +    ** into the RowSet. If it is already present, control skips the
         1121  +    ** Gosub opcode and jumps straight to the code generated by WhereEnd().
         1122  +    **
         1123  +    **        sqlite3WhereBegin(<term>)
         1124  +    **          RowSetTest                  # Insert rowid into rowset
         1125  +    **          Gosub      2 A
         1126  +    **        sqlite3WhereEnd()
         1127  +    **
         1128  +    ** Following the above, code to terminate the loop. Label A, the target
         1129  +    ** of the Gosub above, jumps to the instruction right after the Goto.
         1130  +    **
         1131  +    **          Null       1                # Zero the rowset in reg 1
         1132  +    **          Goto       B                # The loop is finished.
         1133  +    **
         1134  +    **       A: <loop body>                 # Return data, whatever.
         1135  +    **
         1136  +    **          Return     2                # Jump back to the Gosub
         1137  +    **
         1138  +    **       B: <after the loop>
         1139  +    **
         1140  +    ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
         1141  +    ** use an ephemeral index instead of a RowSet to record the primary
         1142  +    ** keys of the rows we have already seen.
         1143  +    **
         1144  +    */
         1145  +    WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
         1146  +    SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
         1147  +    Index *pCov = 0;             /* Potential covering index (or NULL) */
         1148  +    int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
         1149  +
         1150  +    int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
         1151  +    int regRowset = 0;                        /* Register for RowSet object */
         1152  +    int regRowid = 0;                         /* Register holding rowid */
         1153  +    int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
         1154  +    int iRetInit;                             /* Address of regReturn init */
         1155  +    int untestedTerms = 0;             /* Some terms not completely tested */
         1156  +    int ii;                            /* Loop counter */
         1157  +    u16 wctrlFlags;                    /* Flags for sub-WHERE clause */
         1158  +    Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
         1159  +    Table *pTab = pTabItem->pTab;
         1160  +   
         1161  +    pTerm = pLoop->aLTerm[0];
         1162  +    assert( pTerm!=0 );
         1163  +    assert( pTerm->eOperator & WO_OR );
         1164  +    assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
         1165  +    pOrWc = &pTerm->u.pOrInfo->wc;
         1166  +    pLevel->op = OP_Return;
         1167  +    pLevel->p1 = regReturn;
         1168  +
         1169  +    /* Set up a new SrcList in pOrTab containing the table being scanned
         1170  +    ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
         1171  +    ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
         1172  +    */
         1173  +    if( pWInfo->nLevel>1 ){
         1174  +      int nNotReady;                 /* The number of notReady tables */
         1175  +      struct SrcList_item *origSrc;     /* Original list of tables */
         1176  +      nNotReady = pWInfo->nLevel - iLevel - 1;
         1177  +      pOrTab = sqlite3StackAllocRaw(db,
         1178  +                            sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
         1179  +      if( pOrTab==0 ) return notReady;
         1180  +      pOrTab->nAlloc = (u8)(nNotReady + 1);
         1181  +      pOrTab->nSrc = pOrTab->nAlloc;
         1182  +      memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
         1183  +      origSrc = pWInfo->pTabList->a;
         1184  +      for(k=1; k<=nNotReady; k++){
         1185  +        memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
         1186  +      }
         1187  +    }else{
         1188  +      pOrTab = pWInfo->pTabList;
         1189  +    }
         1190  +
         1191  +    /* Initialize the rowset register to contain NULL. An SQL NULL is 
         1192  +    ** equivalent to an empty rowset.  Or, create an ephemeral index
         1193  +    ** capable of holding primary keys in the case of a WITHOUT ROWID.
         1194  +    **
         1195  +    ** Also initialize regReturn to contain the address of the instruction 
         1196  +    ** immediately following the OP_Return at the bottom of the loop. This
         1197  +    ** is required in a few obscure LEFT JOIN cases where control jumps
         1198  +    ** over the top of the loop into the body of it. In this case the 
         1199  +    ** correct response for the end-of-loop code (the OP_Return) is to 
         1200  +    ** fall through to the next instruction, just as an OP_Next does if
         1201  +    ** called on an uninitialized cursor.
         1202  +    */
         1203  +    if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         1204  +      if( HasRowid(pTab) ){
         1205  +        regRowset = ++pParse->nMem;
         1206  +        sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
         1207  +      }else{
         1208  +        Index *pPk = sqlite3PrimaryKeyIndex(pTab);
         1209  +        regRowset = pParse->nTab++;
         1210  +        sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
         1211  +        sqlite3VdbeSetP4KeyInfo(pParse, pPk);
         1212  +      }
         1213  +      regRowid = ++pParse->nMem;
         1214  +    }
         1215  +    iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
         1216  +
         1217  +    /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
         1218  +    ** Then for every term xN, evaluate as the subexpression: xN AND z
         1219  +    ** That way, terms in y that are factored into the disjunction will
         1220  +    ** be picked up by the recursive calls to sqlite3WhereBegin() below.
         1221  +    **
         1222  +    ** Actually, each subexpression is converted to "xN AND w" where w is
         1223  +    ** the "interesting" terms of z - terms that did not originate in the
         1224  +    ** ON or USING clause of a LEFT JOIN, and terms that are usable as 
         1225  +    ** indices.
         1226  +    **
         1227  +    ** This optimization also only applies if the (x1 OR x2 OR ...) term
         1228  +    ** is not contained in the ON clause of a LEFT JOIN.
         1229  +    ** See ticket http://www.sqlite.org/src/info/f2369304e4
         1230  +    */
         1231  +    if( pWC->nTerm>1 ){
         1232  +      int iTerm;
         1233  +      for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
         1234  +        Expr *pExpr = pWC->a[iTerm].pExpr;
         1235  +        if( &pWC->a[iTerm] == pTerm ) continue;
         1236  +        if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
         1237  +        if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
         1238  +        if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
         1239  +        testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
         1240  +        pExpr = sqlite3ExprDup(db, pExpr, 0);
         1241  +        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
         1242  +      }
         1243  +      if( pAndExpr ){
         1244  +        pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
         1245  +      }
         1246  +    }
         1247  +
         1248  +    /* Run a separate WHERE clause for each term of the OR clause.  After
         1249  +    ** eliminating duplicates from other WHERE clauses, the action for each
         1250  +    ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
         1251  +    */
         1252  +    wctrlFlags =  WHERE_OMIT_OPEN_CLOSE
         1253  +                | WHERE_FORCE_TABLE
         1254  +                | WHERE_ONETABLE_ONLY
         1255  +                | WHERE_NO_AUTOINDEX;
         1256  +    for(ii=0; ii<pOrWc->nTerm; ii++){
         1257  +      WhereTerm *pOrTerm = &pOrWc->a[ii];
         1258  +      if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
         1259  +        WhereInfo *pSubWInfo;           /* Info for single OR-term scan */
         1260  +        Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
         1261  +        int j1 = 0;                     /* Address of jump operation */
         1262  +        if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
         1263  +          pAndExpr->pLeft = pOrExpr;
         1264  +          pOrExpr = pAndExpr;
         1265  +        }
         1266  +        /* Loop through table entries that match term pOrTerm. */
         1267  +        WHERETRACE(0xffff, ("Subplan for OR-clause:\n"));
         1268  +        pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
         1269  +                                      wctrlFlags, iCovCur);
         1270  +        assert( pSubWInfo || pParse->nErr || db->mallocFailed );
         1271  +        if( pSubWInfo ){
         1272  +          WhereLoop *pSubLoop;
         1273  +          int addrExplain = sqlite3WhereExplainOneScan(
         1274  +              pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
         1275  +          );
         1276  +          sqlite3WhereAddScanStatus(v, pOrTab, &pSubWInfo->a[0], addrExplain);
         1277  +
         1278  +          /* This is the sub-WHERE clause body.  First skip over
         1279  +          ** duplicate rows from prior sub-WHERE clauses, and record the
         1280  +          ** rowid (or PRIMARY KEY) for the current row so that the same
         1281  +          ** row will be skipped in subsequent sub-WHERE clauses.
         1282  +          */
         1283  +          if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
         1284  +            int r;
         1285  +            int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
         1286  +            if( HasRowid(pTab) ){
         1287  +              r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
         1288  +              j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
         1289  +              VdbeCoverage(v);
         1290  +            }else{
         1291  +              Index *pPk = sqlite3PrimaryKeyIndex(pTab);
         1292  +              int nPk = pPk->nKeyCol;
         1293  +              int iPk;
         1294  +
         1295  +              /* Read the PK into an array of temp registers. */
         1296  +              r = sqlite3GetTempRange(pParse, nPk);
         1297  +              for(iPk=0; iPk<nPk; iPk++){
         1298  +                int iCol = pPk->aiColumn[iPk];
         1299  +                sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
         1300  +              }
         1301  +
         1302  +              /* Check if the temp table already contains this key. If so,
         1303  +              ** the row has already been included in the result set and
         1304  +              ** can be ignored (by jumping past the Gosub below). Otherwise,
         1305  +              ** insert the key into the temp table and proceed with processing
         1306  +              ** the row.
         1307  +              **
         1308  +              ** Use some of the same optimizations as OP_RowSetTest: If iSet
         1309  +              ** is zero, assume that the key cannot already be present in
         1310  +              ** the temp table. And if iSet is -1, assume that there is no 
         1311  +              ** need to insert the key into the temp table, as it will never 
         1312  +              ** be tested for.  */ 
         1313  +              if( iSet ){
         1314  +                j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
         1315  +                VdbeCoverage(v);
         1316  +              }
         1317  +              if( iSet>=0 ){
         1318  +                sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
         1319  +                sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
         1320  +                if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
         1321  +              }
         1322  +
         1323  +              /* Release the array of temp registers */
         1324  +              sqlite3ReleaseTempRange(pParse, r, nPk);
         1325  +            }
         1326  +          }
         1327  +
         1328  +          /* Invoke the main loop body as a subroutine */
         1329  +          sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
         1330  +
         1331  +          /* Jump here (skipping the main loop body subroutine) if the
         1332  +          ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
         1333  +          if( j1 ) sqlite3VdbeJumpHere(v, j1);
         1334  +
         1335  +          /* The pSubWInfo->untestedTerms flag means that this OR term
         1336  +          ** contained one or more AND term from a notReady table.  The
         1337  +          ** terms from the notReady table could not be tested and will
         1338  +          ** need to be tested later.
         1339  +          */
         1340  +          if( pSubWInfo->untestedTerms ) untestedTerms = 1;
         1341  +
         1342  +          /* If all of the OR-connected terms are optimized using the same
         1343  +          ** index, and the index is opened using the same cursor number
         1344  +          ** by each call to sqlite3WhereBegin() made by this loop, it may
         1345  +          ** be possible to use that index as a covering index.
         1346  +          **
         1347  +          ** If the call to sqlite3WhereBegin() above resulted in a scan that
         1348  +          ** uses an index, and this is either the first OR-connected term
         1349  +          ** processed or the index is the same as that used by all previous
         1350  +          ** terms, set pCov to the candidate covering index. Otherwise, set 
         1351  +          ** pCov to NULL to indicate that no candidate covering index will 
         1352  +          ** be available.
         1353  +          */
         1354  +          pSubLoop = pSubWInfo->a[0].pWLoop;
         1355  +          assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
         1356  +          if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
         1357  +           && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
         1358  +           && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
         1359  +          ){
         1360  +            assert( pSubWInfo->a[0].iIdxCur==iCovCur );
         1361  +            pCov = pSubLoop->u.btree.pIndex;
         1362  +            wctrlFlags |= WHERE_REOPEN_IDX;
         1363  +          }else{
         1364  +            pCov = 0;
         1365  +          }
         1366  +
         1367  +          /* Finish the loop through table entries that match term pOrTerm. */
         1368  +          sqlite3WhereEnd(pSubWInfo);
         1369  +        }
         1370  +      }
         1371  +    }
         1372  +    pLevel->u.pCovidx = pCov;
         1373  +    if( pCov ) pLevel->iIdxCur = iCovCur;
         1374  +    if( pAndExpr ){
         1375  +      pAndExpr->pLeft = 0;
         1376  +      sqlite3ExprDelete(db, pAndExpr);
         1377  +    }
         1378  +    sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
         1379  +    sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
         1380  +    sqlite3VdbeResolveLabel(v, iLoopBody);
         1381  +
         1382  +    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
         1383  +    if( !untestedTerms ) disableTerm(pLevel, pTerm);
         1384  +  }else
         1385  +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
         1386  +
         1387  +  {
         1388  +    /* Case 6:  There is no usable index.  We must do a complete
         1389  +    **          scan of the entire table.
         1390  +    */
         1391  +    static const u8 aStep[] = { OP_Next, OP_Prev };
         1392  +    static const u8 aStart[] = { OP_Rewind, OP_Last };
         1393  +    assert( bRev==0 || bRev==1 );
         1394  +    if( pTabItem->isRecursive ){
         1395  +      /* Tables marked isRecursive have only a single row that is stored in
         1396  +      ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
         1397  +      pLevel->op = OP_Noop;
         1398  +    }else{
         1399  +      pLevel->op = aStep[bRev];
         1400  +      pLevel->p1 = iCur;
         1401  +      pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
         1402  +      VdbeCoverageIf(v, bRev==0);
         1403  +      VdbeCoverageIf(v, bRev!=0);
         1404  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         1405  +    }
         1406  +  }
         1407  +
         1408  +#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
         1409  +  pLevel->addrVisit = sqlite3VdbeCurrentAddr(v);
         1410  +#endif
         1411  +
         1412  +  /* Insert code to test every subexpression that can be completely
         1413  +  ** computed using the current set of tables.
         1414  +  */
         1415  +  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
         1416  +    Expr *pE;
         1417  +    int skipLikeAddr = 0;
         1418  +    testcase( pTerm->wtFlags & TERM_VIRTUAL );
         1419  +    testcase( pTerm->wtFlags & TERM_CODED );
         1420  +    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
         1421  +    if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
         1422  +      testcase( pWInfo->untestedTerms==0
         1423  +               && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
         1424  +      pWInfo->untestedTerms = 1;
         1425  +      continue;
         1426  +    }
         1427  +    pE = pTerm->pExpr;
         1428  +    assert( pE!=0 );
         1429  +    if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
         1430  +      continue;
         1431  +    }
         1432  +    if( pTerm->wtFlags & TERM_LIKECOND ){
         1433  +      assert( pLevel->iLikeRepCntr>0 );
         1434  +      skipLikeAddr = sqlite3VdbeAddOp1(v, OP_IfNot, pLevel->iLikeRepCntr);
         1435  +      VdbeCoverage(v);
         1436  +    }
         1437  +    sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
         1438  +    if( skipLikeAddr ) sqlite3VdbeJumpHere(v, skipLikeAddr);
         1439  +    pTerm->wtFlags |= TERM_CODED;
         1440  +  }
         1441  +
         1442  +  /* Insert code to test for implied constraints based on transitivity
         1443  +  ** of the "==" operator.
         1444  +  **
         1445  +  ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
         1446  +  ** and we are coding the t1 loop and the t2 loop has not yet coded,
         1447  +  ** then we cannot use the "t1.a=t2.b" constraint, but we can code
         1448  +  ** the implied "t1.a=123" constraint.
         1449  +  */
         1450  +  for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
         1451  +    Expr *pE, *pEAlt;
         1452  +    WhereTerm *pAlt;
         1453  +    if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
         1454  +    if( (pTerm->eOperator & (WO_EQ|WO_IS))==0 ) continue;
         1455  +    if( (pTerm->eOperator & WO_EQUIV)==0 ) continue;
         1456  +    if( pTerm->leftCursor!=iCur ) continue;
         1457  +    if( pLevel->iLeftJoin ) continue;
         1458  +    pE = pTerm->pExpr;
         1459  +    assert( !ExprHasProperty(pE, EP_FromJoin) );
         1460  +    assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
         1461  +    pAlt = sqlite3WhereFindTerm(pWC, iCur, pTerm->u.leftColumn, notReady,
         1462  +                    WO_EQ|WO_IN|WO_IS, 0);
         1463  +    if( pAlt==0 ) continue;
         1464  +    if( pAlt->wtFlags & (TERM_CODED) ) continue;
         1465  +    testcase( pAlt->eOperator & WO_EQ );
         1466  +    testcase( pAlt->eOperator & WO_IS );
         1467  +    testcase( pAlt->eOperator & WO_IN );
         1468  +    VdbeModuleComment((v, "begin transitive constraint"));
         1469  +    pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
         1470  +    if( pEAlt ){
         1471  +      *pEAlt = *pAlt->pExpr;
         1472  +      pEAlt->pLeft = pE->pLeft;
         1473  +      sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
         1474  +      sqlite3StackFree(db, pEAlt);
         1475  +    }
         1476  +  }
         1477  +
         1478  +  /* For a LEFT OUTER JOIN, generate code that will record the fact that
         1479  +  ** at least one row of the right table has matched the left table.  
         1480  +  */
         1481  +  if( pLevel->iLeftJoin ){
         1482  +    pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
         1483  +    sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
         1484  +    VdbeComment((v, "record LEFT JOIN hit"));
         1485  +    sqlite3ExprCacheClear(pParse);
         1486  +    for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
         1487  +      testcase( pTerm->wtFlags & TERM_VIRTUAL );
         1488  +      testcase( pTerm->wtFlags & TERM_CODED );
         1489  +      if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
         1490  +      if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
         1491  +        assert( pWInfo->untestedTerms );
         1492  +        continue;
         1493  +      }
         1494  +      assert( pTerm->pExpr );
         1495  +      sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
         1496  +      pTerm->wtFlags |= TERM_CODED;
         1497  +    }
         1498  +  }
         1499  +
         1500  +  return pLevel->notReady;
         1501  +}

Added src/whereexpr.c.

            1  +/*
            2  +** 2015-06-08
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +** This module contains C code that generates VDBE code used to process
           13  +** the WHERE clause of SQL statements.
           14  +**
           15  +** This file was originally part of where.c but was split out to improve
           16  +** readability and editabiliity.  This file contains utility routines for
           17  +** analyzing Expr objects in the WHERE clause.
           18  +*/
           19  +#include "sqliteInt.h"
           20  +#include "whereInt.h"
           21  +
           22  +/* Forward declarations */
           23  +static void exprAnalyze(SrcList*, WhereClause*, int);
           24  +
           25  +/*
           26  +** Deallocate all memory associated with a WhereOrInfo object.
           27  +*/
           28  +static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
           29  +  sqlite3WhereClauseClear(&p->wc);
           30  +  sqlite3DbFree(db, p);
           31  +}
           32  +
           33  +/*
           34  +** Deallocate all memory associated with a WhereAndInfo object.
           35  +*/
           36  +static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
           37  +  sqlite3WhereClauseClear(&p->wc);
           38  +  sqlite3DbFree(db, p);
           39  +}
           40  +
           41  +/*
           42  +** Add a single new WhereTerm entry to the WhereClause object pWC.
           43  +** The new WhereTerm object is constructed from Expr p and with wtFlags.
           44  +** The index in pWC->a[] of the new WhereTerm is returned on success.
           45  +** 0 is returned if the new WhereTerm could not be added due to a memory
           46  +** allocation error.  The memory allocation failure will be recorded in
           47  +** the db->mallocFailed flag so that higher-level functions can detect it.
           48  +**
           49  +** This routine will increase the size of the pWC->a[] array as necessary.
           50  +**
           51  +** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
           52  +** for freeing the expression p is assumed by the WhereClause object pWC.
           53  +** This is true even if this routine fails to allocate a new WhereTerm.
           54  +**
           55  +** WARNING:  This routine might reallocate the space used to store
           56  +** WhereTerms.  All pointers to WhereTerms should be invalidated after
           57  +** calling this routine.  Such pointers may be reinitialized by referencing
           58  +** the pWC->a[] array.
           59  +*/
           60  +static int whereClauseInsert(WhereClause *pWC, Expr *p, u16 wtFlags){
           61  +  WhereTerm *pTerm;
           62  +  int idx;
           63  +  testcase( wtFlags & TERM_VIRTUAL );
           64  +  if( pWC->nTerm>=pWC->nSlot ){
           65  +    WhereTerm *pOld = pWC->a;
           66  +    sqlite3 *db = pWC->pWInfo->pParse->db;
           67  +    pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
           68  +    if( pWC->a==0 ){
           69  +      if( wtFlags & TERM_DYNAMIC ){
           70  +        sqlite3ExprDelete(db, p);
           71  +      }
           72  +      pWC->a = pOld;
           73  +      return 0;
           74  +    }
           75  +    memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
           76  +    if( pOld!=pWC->aStatic ){
           77  +      sqlite3DbFree(db, pOld);
           78  +    }
           79  +    pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
           80  +    memset(&pWC->a[pWC->nTerm], 0, sizeof(pWC->a[0])*(pWC->nSlot-pWC->nTerm));
           81  +  }
           82  +  pTerm = &pWC->a[idx = pWC->nTerm++];
           83  +  if( p && ExprHasProperty(p, EP_Unlikely) ){
           84  +    pTerm->truthProb = sqlite3LogEst(p->iTable) - 270;
           85  +  }else{
           86  +    pTerm->truthProb = 1;
           87  +  }
           88  +  pTerm->pExpr = sqlite3ExprSkipCollate(p);
           89  +  pTerm->wtFlags = wtFlags;
           90  +  pTerm->pWC = pWC;
           91  +  pTerm->iParent = -1;
           92  +  return idx;
           93  +}
           94  +
           95  +/*
           96  +** Return TRUE if the given operator is one of the operators that is
           97  +** allowed for an indexable WHERE clause term.  The allowed operators are
           98  +** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
           99  +*/
          100  +static int allowedOp(int op){
          101  +  assert( TK_GT>TK_EQ && TK_GT<TK_GE );
          102  +  assert( TK_LT>TK_EQ && TK_LT<TK_GE );
          103  +  assert( TK_LE>TK_EQ && TK_LE<TK_GE );
          104  +  assert( TK_GE==TK_EQ+4 );
          105  +  return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL || op==TK_IS;
          106  +}
          107  +
          108  +/*
          109  +** Commute a comparison operator.  Expressions of the form "X op Y"
          110  +** are converted into "Y op X".
          111  +**
          112  +** If left/right precedence rules come into play when determining the
          113  +** collating sequence, then COLLATE operators are adjusted to ensure
          114  +** that the collating sequence does not change.  For example:
          115  +** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
          116  +** the left hand side of a comparison overrides any collation sequence 
          117  +** attached to the right. For the same reason the EP_Collate flag
          118  +** is not commuted.
          119  +*/
          120  +static void exprCommute(Parse *pParse, Expr *pExpr){
          121  +  u16 expRight = (pExpr->pRight->flags & EP_Collate);
          122  +  u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
          123  +  assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
          124  +  if( expRight==expLeft ){
          125  +    /* Either X and Y both have COLLATE operator or neither do */
          126  +    if( expRight ){
          127  +      /* Both X and Y have COLLATE operators.  Make sure X is always
          128  +      ** used by clearing the EP_Collate flag from Y. */
          129  +      pExpr->pRight->flags &= ~EP_Collate;
          130  +    }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
          131  +      /* Neither X nor Y have COLLATE operators, but X has a non-default
          132  +      ** collating sequence.  So add the EP_Collate marker on X to cause
          133  +      ** it to be searched first. */
          134  +      pExpr->pLeft->flags |= EP_Collate;
          135  +    }
          136  +  }
          137  +  SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
          138  +  if( pExpr->op>=TK_GT ){
          139  +    assert( TK_LT==TK_GT+2 );
          140  +    assert( TK_GE==TK_LE+2 );
          141  +    assert( TK_GT>TK_EQ );
          142  +    assert( TK_GT<TK_LE );
          143  +    assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
          144  +    pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
          145  +  }
          146  +}
          147  +
          148  +/*
          149  +** Translate from TK_xx operator to WO_xx bitmask.
          150  +*/
          151  +static u16 operatorMask(int op){
          152  +  u16 c;
          153  +  assert( allowedOp(op) );
          154  +  if( op==TK_IN ){
          155  +    c = WO_IN;
          156  +  }else if( op==TK_ISNULL ){
          157  +    c = WO_ISNULL;
          158  +  }else if( op==TK_IS ){
          159  +    c = WO_IS;
          160  +  }else{
          161  +    assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
          162  +    c = (u16)(WO_EQ<<(op-TK_EQ));
          163  +  }
          164  +  assert( op!=TK_ISNULL || c==WO_ISNULL );
          165  +  assert( op!=TK_IN || c==WO_IN );
          166  +  assert( op!=TK_EQ || c==WO_EQ );
          167  +  assert( op!=TK_LT || c==WO_LT );
          168  +  assert( op!=TK_LE || c==WO_LE );
          169  +  assert( op!=TK_GT || c==WO_GT );
          170  +  assert( op!=TK_GE || c==WO_GE );
          171  +  assert( op!=TK_IS || c==WO_IS );
          172  +  return c;
          173  +}
          174  +
          175  +
          176  +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
          177  +/*
          178  +** Check to see if the given expression is a LIKE or GLOB operator that
          179  +** can be optimized using inequality constraints.  Return TRUE if it is
          180  +** so and false if not.
          181  +**
          182  +** In order for the operator to be optimizible, the RHS must be a string
          183  +** literal that does not begin with a wildcard.  The LHS must be a column
          184  +** that may only be NULL, a string, or a BLOB, never a number. (This means
          185  +** that virtual tables cannot participate in the LIKE optimization.)  The
          186  +** collating sequence for the column on the LHS must be appropriate for
          187  +** the operator.
          188  +*/
          189  +static int isLikeOrGlob(
          190  +  Parse *pParse,    /* Parsing and code generating context */
          191  +  Expr *pExpr,      /* Test this expression */
          192  +  Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
          193  +  int *pisComplete, /* True if the only wildcard is % in the last character */
          194  +  int *pnoCase      /* True if uppercase is equivalent to lowercase */
          195  +){
          196  +  const char *z = 0;         /* String on RHS of LIKE operator */
          197  +  Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
          198  +  ExprList *pList;           /* List of operands to the LIKE operator */
          199  +  int c;                     /* One character in z[] */
          200  +  int cnt;                   /* Number of non-wildcard prefix characters */
          201  +  char wc[3];                /* Wildcard characters */
          202  +  sqlite3 *db = pParse->db;  /* Database connection */
          203  +  sqlite3_value *pVal = 0;
          204  +  int op;                    /* Opcode of pRight */
          205  +
          206  +  if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
          207  +    return 0;
          208  +  }
          209  +#ifdef SQLITE_EBCDIC
          210  +  if( *pnoCase ) return 0;
          211  +#endif
          212  +  pList = pExpr->x.pList;
          213  +  pLeft = pList->a[1].pExpr;
          214  +  if( pLeft->op!=TK_COLUMN 
          215  +   || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
          216  +   || IsVirtual(pLeft->pTab)  /* Value might be numeric */
          217  +  ){
          218  +    /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
          219  +    ** be the name of an indexed column with TEXT affinity. */
          220  +    return 0;
          221  +  }
          222  +  assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
          223  +
          224  +  pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
          225  +  op = pRight->op;
          226  +  if( op==TK_VARIABLE ){
          227  +    Vdbe *pReprepare = pParse->pReprepare;
          228  +    int iCol = pRight->iColumn;
          229  +    pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_BLOB);
          230  +    if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
          231  +      z = (char *)sqlite3_value_text(pVal);
          232  +    }
          233  +    sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
          234  +    assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
          235  +  }else if( op==TK_STRING ){
          236  +    z = pRight->u.zToken;
          237  +  }
          238  +  if( z ){
          239  +    cnt = 0;
          240  +    while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
          241  +      cnt++;
          242  +    }
          243  +    if( cnt!=0 && 255!=(u8)z[cnt-1] ){
          244  +      Expr *pPrefix;
          245  +      *pisComplete = c==wc[0] && z[cnt+1]==0;
          246  +      pPrefix = sqlite3Expr(db, TK_STRING, z);
          247  +      if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
          248  +      *ppPrefix = pPrefix;
          249  +      if( op==TK_VARIABLE ){
          250  +        Vdbe *v = pParse->pVdbe;
          251  +        sqlite3VdbeSetVarmask(v, pRight->iColumn);
          252  +        if( *pisComplete && pRight->u.zToken[1] ){
          253  +          /* If the rhs of the LIKE expression is a variable, and the current
          254  +          ** value of the variable means there is no need to invoke the LIKE
          255  +          ** function, then no OP_Variable will be added to the program.
          256  +          ** This causes problems for the sqlite3_bind_parameter_name()
          257  +          ** API. To work around them, add a dummy OP_Variable here.
          258  +          */ 
          259  +          int r1 = sqlite3GetTempReg(pParse);
          260  +          sqlite3ExprCodeTarget(pParse, pRight, r1);
          261  +          sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
          262  +          sqlite3ReleaseTempReg(pParse, r1);
          263  +        }
          264  +      }
          265  +    }else{
          266  +      z = 0;
          267  +    }
          268  +  }
          269  +
          270  +  sqlite3ValueFree(pVal);
          271  +  return (z!=0);
          272  +}
          273  +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
          274  +
          275  +
          276  +#ifndef SQLITE_OMIT_VIRTUALTABLE
          277  +/*
          278  +** Check to see if the given expression is of the form
          279  +**
          280  +**         column MATCH expr
          281  +**
          282  +** If it is then return TRUE.  If not, return FALSE.
          283  +*/
          284  +static int isMatchOfColumn(
          285  +  Expr *pExpr      /* Test this expression */
          286  +){
          287  +  ExprList *pList;
          288  +
          289  +  if( pExpr->op!=TK_FUNCTION ){
          290  +    return 0;
          291  +  }
          292  +  if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
          293  +    return 0;
          294  +  }
          295  +  pList = pExpr->x.pList;
          296  +  if( pList->nExpr!=2 ){
          297  +    return 0;
          298  +  }
          299  +  if( pList->a[1].pExpr->op != TK_COLUMN ){
          300  +    return 0;
          301  +  }
          302  +  return 1;
          303  +}
          304  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
          305  +
          306  +/*
          307  +** If the pBase expression originated in the ON or USING clause of
          308  +** a join, then transfer the appropriate markings over to derived.
          309  +*/
          310  +static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
          311  +  if( pDerived ){
          312  +    pDerived->flags |= pBase->flags & EP_FromJoin;
          313  +    pDerived->iRightJoinTable = pBase->iRightJoinTable;
          314  +  }
          315  +}
          316  +
          317  +/*
          318  +** Mark term iChild as being a child of term iParent
          319  +*/
          320  +static void markTermAsChild(WhereClause *pWC, int iChild, int iParent){
          321  +  pWC->a[iChild].iParent = iParent;
          322  +  pWC->a[iChild].truthProb = pWC->a[iParent].truthProb;
          323  +  pWC->a[iParent].nChild++;
          324  +}
          325  +
          326  +/*
          327  +** Return the N-th AND-connected subterm of pTerm.  Or if pTerm is not
          328  +** a conjunction, then return just pTerm when N==0.  If N is exceeds
          329  +** the number of available subterms, return NULL.
          330  +*/
          331  +static WhereTerm *whereNthSubterm(WhereTerm *pTerm, int N){
          332  +  if( pTerm->eOperator!=WO_AND ){
          333  +    return N==0 ? pTerm : 0;
          334  +  }
          335  +  if( N<pTerm->u.pAndInfo->wc.nTerm ){
          336  +    return &pTerm->u.pAndInfo->wc.a[N];
          337  +  }
          338  +  return 0;
          339  +}
          340  +
          341  +/*
          342  +** Subterms pOne and pTwo are contained within WHERE clause pWC.  The
          343  +** two subterms are in disjunction - they are OR-ed together.
          344  +**
          345  +** If these two terms are both of the form:  "A op B" with the same
          346  +** A and B values but different operators and if the operators are
          347  +** compatible (if one is = and the other is <, for example) then
          348  +** add a new virtual AND term to pWC that is the combination of the
          349  +** two.
          350  +**
          351  +** Some examples:
          352  +**
          353  +**    x<y OR x=y    -->     x<=y
          354  +**    x=y OR x=y    -->     x=y
          355  +**    x<=y OR x<y   -->     x<=y
          356  +**
          357  +** The following is NOT generated:
          358  +**
          359  +**    x<y OR x>y    -->     x!=y     
          360  +*/
          361  +static void whereCombineDisjuncts(
          362  +  SrcList *pSrc,         /* the FROM clause */
          363  +  WhereClause *pWC,      /* The complete WHERE clause */
          364  +  WhereTerm *pOne,       /* First disjunct */
          365  +  WhereTerm *pTwo        /* Second disjunct */
          366  +){
          367  +  u16 eOp = pOne->eOperator | pTwo->eOperator;
          368  +  sqlite3 *db;           /* Database connection (for malloc) */
          369  +  Expr *pNew;            /* New virtual expression */
          370  +  int op;                /* Operator for the combined expression */
          371  +  int idxNew;            /* Index in pWC of the next virtual term */
          372  +
          373  +  if( (pOne->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
          374  +  if( (pTwo->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE))==0 ) return;
          375  +  if( (eOp & (WO_EQ|WO_LT|WO_LE))!=eOp
          376  +   && (eOp & (WO_EQ|WO_GT|WO_GE))!=eOp ) return;
          377  +  assert( pOne->pExpr->pLeft!=0 && pOne->pExpr->pRight!=0 );
          378  +  assert( pTwo->pExpr->pLeft!=0 && pTwo->pExpr->pRight!=0 );
          379  +  if( sqlite3ExprCompare(pOne->pExpr->pLeft, pTwo->pExpr->pLeft, -1) ) return;
          380  +  if( sqlite3ExprCompare(pOne->pExpr->pRight, pTwo->pExpr->pRight, -1) )return;
          381  +  /* If we reach this point, it means the two subterms can be combined */
          382  +  if( (eOp & (eOp-1))!=0 ){
          383  +    if( eOp & (WO_LT|WO_LE) ){
          384  +      eOp = WO_LE;
          385  +    }else{
          386  +      assert( eOp & (WO_GT|WO_GE) );
          387  +      eOp = WO_GE;
          388  +    }
          389  +  }
          390  +  db = pWC->pWInfo->pParse->db;
          391  +  pNew = sqlite3ExprDup(db, pOne->pExpr, 0);
          392  +  if( pNew==0 ) return;
          393  +  for(op=TK_EQ; eOp!=(WO_EQ<<(op-TK_EQ)); op++){ assert( op<TK_GE ); }
          394  +  pNew->op = op;
          395  +  idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
          396  +  exprAnalyze(pSrc, pWC, idxNew);
          397  +}
          398  +
          399  +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
          400  +/*
          401  +** Analyze a term that consists of two or more OR-connected
          402  +** subterms.  So in:
          403  +**
          404  +**     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
          405  +**                          ^^^^^^^^^^^^^^^^^^^^
          406  +**
          407  +** This routine analyzes terms such as the middle term in the above example.
          408  +** A WhereOrTerm object is computed and attached to the term under
          409  +** analysis, regardless of the outcome of the analysis.  Hence:
          410  +**
          411  +**     WhereTerm.wtFlags   |=  TERM_ORINFO
          412  +**     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
          413  +**
          414  +** The term being analyzed must have two or more of OR-connected subterms.
          415  +** A single subterm might be a set of AND-connected sub-subterms.
          416  +** Examples of terms under analysis:
          417  +**
          418  +**     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
          419  +**     (B)     x=expr1 OR expr2=x OR x=expr3
          420  +**     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
          421  +**     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
          422  +**     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
          423  +**     (F)     x>A OR (x=A AND y>=B)
          424  +**
          425  +** CASE 1:
          426  +**
          427  +** If all subterms are of the form T.C=expr for some single column of C and
          428  +** a single table T (as shown in example B above) then create a new virtual
          429  +** term that is an equivalent IN expression.  In other words, if the term
          430  +** being analyzed is:
          431  +**
          432  +**      x = expr1  OR  expr2 = x  OR  x = expr3
          433  +**
          434  +** then create a new virtual term like this:
          435  +**
          436  +**      x IN (expr1,expr2,expr3)
          437  +**
          438  +** CASE 2:
          439  +**
          440  +** If there are exactly two disjuncts and one side has x>A and the other side
          441  +** has x=A (for the same x and A) then add a new virtual conjunct term to the
          442  +** WHERE clause of the form "x>=A".  Example:
          443  +**
          444  +**      x>A OR (x=A AND y>B)    adds:    x>=A
          445  +**
          446  +** The added conjunct can sometimes be helpful in query planning.
          447  +**
          448  +** CASE 3:
          449  +**
          450  +** If all subterms are indexable by a single table T, then set
          451  +**
          452  +**     WhereTerm.eOperator              =  WO_OR
          453  +**     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
          454  +**
          455  +** A subterm is "indexable" if it is of the form
          456  +** "T.C <op> <expr>" where C is any column of table T and 
          457  +** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
          458  +** A subterm is also indexable if it is an AND of two or more
          459  +** subsubterms at least one of which is indexable.  Indexable AND 
          460  +** subterms have their eOperator set to WO_AND and they have
          461  +** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
          462  +**
          463  +** From another point of view, "indexable" means that the subterm could
          464  +** potentially be used with an index if an appropriate index exists.
          465  +** This analysis does not consider whether or not the index exists; that
          466  +** is decided elsewhere.  This analysis only looks at whether subterms
          467  +** appropriate for indexing exist.
          468  +**
          469  +** All examples A through E above satisfy case 3.  But if a term
          470  +** also satisfies case 1 (such as B) we know that the optimizer will
          471  +** always prefer case 1, so in that case we pretend that case 3 is not
          472  +** satisfied.
          473  +**
          474  +** It might be the case that multiple tables are indexable.  For example,
          475  +** (E) above is indexable on tables P, Q, and R.
          476  +**
          477  +** Terms that satisfy case 3 are candidates for lookup by using
          478  +** separate indices to find rowids for each subterm and composing
          479  +** the union of all rowids using a RowSet object.  This is similar
          480  +** to "bitmap indices" in other database engines.
          481  +**
          482  +** OTHERWISE:
          483  +**
          484  +** If none of cases 1, 2, or 3 apply, then leave the eOperator set to
          485  +** zero.  This term is not useful for search.
          486  +*/
          487  +static void exprAnalyzeOrTerm(
          488  +  SrcList *pSrc,            /* the FROM clause */
          489  +  WhereClause *pWC,         /* the complete WHERE clause */
          490  +  int idxTerm               /* Index of the OR-term to be analyzed */
          491  +){
          492  +  WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
          493  +  Parse *pParse = pWInfo->pParse;         /* Parser context */
          494  +  sqlite3 *db = pParse->db;               /* Database connection */
          495  +  WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
          496  +  Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
          497  +  int i;                                  /* Loop counters */
          498  +  WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
          499  +  WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
          500  +  WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
          501  +  Bitmask chngToIN;         /* Tables that might satisfy case 1 */
          502  +  Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
          503  +
          504  +  /*
          505  +  ** Break the OR clause into its separate subterms.  The subterms are
          506  +  ** stored in a WhereClause structure containing within the WhereOrInfo
          507  +  ** object that is attached to the original OR clause term.
          508  +  */
          509  +  assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
          510  +  assert( pExpr->op==TK_OR );
          511  +  pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
          512  +  if( pOrInfo==0 ) return;
          513  +  pTerm->wtFlags |= TERM_ORINFO;
          514  +  pOrWc = &pOrInfo->wc;
          515  +  sqlite3WhereClauseInit(pOrWc, pWInfo);
          516  +  sqlite3WhereSplit(pOrWc, pExpr, TK_OR);
          517  +  sqlite3WhereExprAnalyze(pSrc, pOrWc);
          518  +  if( db->mallocFailed ) return;
          519  +  assert( pOrWc->nTerm>=2 );
          520  +
          521  +  /*
          522  +  ** Compute the set of tables that might satisfy cases 1 or 3.
          523  +  */
          524  +  indexable = ~(Bitmask)0;
          525  +  chngToIN = ~(Bitmask)0;
          526  +  for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
          527  +    if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
          528  +      WhereAndInfo *pAndInfo;
          529  +      assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
          530  +      chngToIN = 0;
          531  +      pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
          532  +      if( pAndInfo ){
          533  +        WhereClause *pAndWC;
          534  +        WhereTerm *pAndTerm;
          535  +        int j;
          536  +        Bitmask b = 0;
          537  +        pOrTerm->u.pAndInfo = pAndInfo;
          538  +        pOrTerm->wtFlags |= TERM_ANDINFO;
          539  +        pOrTerm->eOperator = WO_AND;
          540  +        pAndWC = &pAndInfo->wc;
          541  +        sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
          542  +        sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
          543  +        sqlite3WhereExprAnalyze(pSrc, pAndWC);
          544  +        pAndWC->pOuter = pWC;
          545  +        testcase( db->mallocFailed );
          546  +        if( !db->mallocFailed ){
          547  +          for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
          548  +            assert( pAndTerm->pExpr );
          549  +            if( allowedOp(pAndTerm->pExpr->op) ){
          550  +              b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
          551  +            }
          552  +          }
          553  +        }
          554  +        indexable &= b;
          555  +      }
          556  +    }else if( pOrTerm->wtFlags & TERM_COPIED ){
          557  +      /* Skip this term for now.  We revisit it when we process the
          558  +      ** corresponding TERM_VIRTUAL term */
          559  +    }else{
          560  +      Bitmask b;
          561  +      b = sqlite3WhereGetMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
          562  +      if( pOrTerm->wtFlags & TERM_VIRTUAL ){
          563  +        WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
          564  +        b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pOther->leftCursor);
          565  +      }
          566  +      indexable &= b;
          567  +      if( (pOrTerm->eOperator & WO_EQ)==0 ){
          568  +        chngToIN = 0;
          569  +      }else{
          570  +        chngToIN &= b;
          571  +      }
          572  +    }
          573  +  }
          574  +
          575  +  /*
          576  +  ** Record the set of tables that satisfy case 3.  The set might be
          577  +  ** empty.
          578  +  */
          579  +  pOrInfo->indexable = indexable;
          580  +  pTerm->eOperator = indexable==0 ? 0 : WO_OR;
          581  +
          582  +  /* For a two-way OR, attempt to implementation case 2.
          583  +  */
          584  +  if( indexable && pOrWc->nTerm==2 ){
          585  +    int iOne = 0;
          586  +    WhereTerm *pOne;
          587  +    while( (pOne = whereNthSubterm(&pOrWc->a[0],iOne++))!=0 ){
          588  +      int iTwo = 0;
          589  +      WhereTerm *pTwo;
          590  +      while( (pTwo = whereNthSubterm(&pOrWc->a[1],iTwo++))!=0 ){
          591  +        whereCombineDisjuncts(pSrc, pWC, pOne, pTwo);
          592  +      }
          593  +    }
          594  +  }
          595  +
          596  +  /*
          597  +  ** chngToIN holds a set of tables that *might* satisfy case 1.  But
          598  +  ** we have to do some additional checking to see if case 1 really
          599  +  ** is satisfied.
          600  +  **
          601  +  ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
          602  +  ** that there is no possibility of transforming the OR clause into an
          603  +  ** IN operator because one or more terms in the OR clause contain
          604  +  ** something other than == on a column in the single table.  The 1-bit
          605  +  ** case means that every term of the OR clause is of the form
          606  +  ** "table.column=expr" for some single table.  The one bit that is set
          607  +  ** will correspond to the common table.  We still need to check to make
          608  +  ** sure the same column is used on all terms.  The 2-bit case is when
          609  +  ** the all terms are of the form "table1.column=table2.column".  It
          610  +  ** might be possible to form an IN operator with either table1.column
          611  +  ** or table2.column as the LHS if either is common to every term of
          612  +  ** the OR clause.
          613  +  **
          614  +  ** Note that terms of the form "table.column1=table.column2" (the
          615  +  ** same table on both sizes of the ==) cannot be optimized.
          616  +  */
          617  +  if( chngToIN ){
          618  +    int okToChngToIN = 0;     /* True if the conversion to IN is valid */
          619  +    int iColumn = -1;         /* Column index on lhs of IN operator */
          620  +    int iCursor = -1;         /* Table cursor common to all terms */
          621  +    int j = 0;                /* Loop counter */
          622  +
          623  +    /* Search for a table and column that appears on one side or the
          624  +    ** other of the == operator in every subterm.  That table and column
          625  +    ** will be recorded in iCursor and iColumn.  There might not be any
          626  +    ** such table and column.  Set okToChngToIN if an appropriate table
          627  +    ** and column is found but leave okToChngToIN false if not found.
          628  +    */
          629  +    for(j=0; j<2 && !okToChngToIN; j++){
          630  +      pOrTerm = pOrWc->a;
          631  +      for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
          632  +        assert( pOrTerm->eOperator & WO_EQ );
          633  +        pOrTerm->wtFlags &= ~TERM_OR_OK;
          634  +        if( pOrTerm->leftCursor==iCursor ){
          635  +          /* This is the 2-bit case and we are on the second iteration and
          636  +          ** current term is from the first iteration.  So skip this term. */
          637  +          assert( j==1 );
          638  +          continue;
          639  +        }
          640  +        if( (chngToIN & sqlite3WhereGetMask(&pWInfo->sMaskSet,
          641  +                                            pOrTerm->leftCursor))==0 ){
          642  +          /* This term must be of the form t1.a==t2.b where t2 is in the
          643  +          ** chngToIN set but t1 is not.  This term will be either preceded
          644  +          ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term 
          645  +          ** and use its inversion. */
          646  +          testcase( pOrTerm->wtFlags & TERM_COPIED );
          647  +          testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
          648  +          assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
          649  +          continue;
          650  +        }
          651  +        iColumn = pOrTerm->u.leftColumn;
          652  +        iCursor = pOrTerm->leftCursor;
          653  +        break;
          654  +      }
          655  +      if( i<0 ){
          656  +        /* No candidate table+column was found.  This can only occur
          657  +        ** on the second iteration */
          658  +        assert( j==1 );
          659  +        assert( IsPowerOfTwo(chngToIN) );
          660  +        assert( chngToIN==sqlite3WhereGetMask(&pWInfo->sMaskSet, iCursor) );
          661  +        break;
          662  +      }
          663  +      testcase( j==1 );
          664  +
          665  +      /* We have found a candidate table and column.  Check to see if that
          666  +      ** table and column is common to every term in the OR clause */
          667  +      okToChngToIN = 1;
          668  +      for(; i>=0 && okToChngToIN; i--, pOrTerm++){
          669  +        assert( pOrTerm->eOperator & WO_EQ );
          670  +        if( pOrTerm->leftCursor!=iCursor ){
          671  +          pOrTerm->wtFlags &= ~TERM_OR_OK;
          672  +        }else if( pOrTerm->u.leftColumn!=iColumn ){
          673  +          okToChngToIN = 0;
          674  +        }else{
          675  +          int affLeft, affRight;
          676  +          /* If the right-hand side is also a column, then the affinities
          677  +          ** of both right and left sides must be such that no type
          678  +          ** conversions are required on the right.  (Ticket #2249)
          679  +          */
          680  +          affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
          681  +          affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
          682  +          if( affRight!=0 && affRight!=affLeft ){
          683  +            okToChngToIN = 0;
          684  +          }else{
          685  +            pOrTerm->wtFlags |= TERM_OR_OK;
          686  +          }
          687  +        }
          688  +      }
          689  +    }
          690  +
          691  +    /* At this point, okToChngToIN is true if original pTerm satisfies
          692  +    ** case 1.  In that case, construct a new virtual term that is 
          693  +    ** pTerm converted into an IN operator.
          694  +    */
          695  +    if( okToChngToIN ){
          696  +      Expr *pDup;            /* A transient duplicate expression */
          697  +      ExprList *pList = 0;   /* The RHS of the IN operator */
          698  +      Expr *pLeft = 0;       /* The LHS of the IN operator */
          699  +      Expr *pNew;            /* The complete IN operator */
          700  +
          701  +      for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
          702  +        if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
          703  +        assert( pOrTerm->eOperator & WO_EQ );
          704  +        assert( pOrTerm->leftCursor==iCursor );
          705  +        assert( pOrTerm->u.leftColumn==iColumn );
          706  +        pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
          707  +        pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
          708  +        pLeft = pOrTerm->pExpr->pLeft;
          709  +      }
          710  +      assert( pLeft!=0 );
          711  +      pDup = sqlite3ExprDup(db, pLeft, 0);
          712  +      pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
          713  +      if( pNew ){
          714  +        int idxNew;
          715  +        transferJoinMarkings(pNew, pExpr);
          716  +        assert( !ExprHasProperty(pNew, EP_xIsSelect) );
          717  +        pNew->x.pList = pList;
          718  +        idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
          719  +        testcase( idxNew==0 );
          720  +        exprAnalyze(pSrc, pWC, idxNew);
          721  +        pTerm = &pWC->a[idxTerm];
          722  +        markTermAsChild(pWC, idxNew, idxTerm);
          723  +      }else{
          724  +        sqlite3ExprListDelete(db, pList);
          725  +      }
          726  +      pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 3 */
          727  +    }
          728  +  }
          729  +}
          730  +#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
          731  +
          732  +/*
          733  +** We already know that pExpr is a binary operator where both operands are
          734  +** column references.  This routine checks to see if pExpr is an equivalence
          735  +** relation:
          736  +**   1.  The SQLITE_Transitive optimization must be enabled
          737  +**   2.  Must be either an == or an IS operator
          738  +**   3.  Not originating in the ON clause of an OUTER JOIN
          739  +**   4.  The affinities of A and B must be compatible
          740  +**   5a. Both operands use the same collating sequence OR
          741  +**   5b. The overall collating sequence is BINARY
          742  +** If this routine returns TRUE, that means that the RHS can be substituted
          743  +** for the LHS anyplace else in the WHERE clause where the LHS column occurs.
          744  +** This is an optimization.  No harm comes from returning 0.  But if 1 is
          745  +** returned when it should not be, then incorrect answers might result.
          746  +*/
          747  +static int termIsEquivalence(Parse *pParse, Expr *pExpr){
          748  +  char aff1, aff2;
          749  +  CollSeq *pColl;
          750  +  const char *zColl1, *zColl2;
          751  +  if( !OptimizationEnabled(pParse->db, SQLITE_Transitive) ) return 0;
          752  +  if( pExpr->op!=TK_EQ && pExpr->op!=TK_IS ) return 0;
          753  +  if( ExprHasProperty(pExpr, EP_FromJoin) ) return 0;
          754  +  aff1 = sqlite3ExprAffinity(pExpr->pLeft);
          755  +  aff2 = sqlite3ExprAffinity(pExpr->pRight);
          756  +  if( aff1!=aff2
          757  +   && (!sqlite3IsNumericAffinity(aff1) || !sqlite3IsNumericAffinity(aff2))
          758  +  ){
          759  +    return 0;
          760  +  }
          761  +  pColl = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, pExpr->pRight);
          762  +  if( pColl==0 || sqlite3StrICmp(pColl->zName, "BINARY")==0 ) return 1;
          763  +  pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
          764  +  /* Since pLeft and pRight are both a column references, their collating
          765  +  ** sequence should always be defined. */
          766  +  zColl1 = ALWAYS(pColl) ? pColl->zName : 0;
          767  +  pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight);
          768  +  zColl2 = ALWAYS(pColl) ? pColl->zName : 0;
          769  +  return sqlite3StrICmp(zColl1, zColl2)==0;
          770  +}
          771  +
          772  +/*
          773  +** Recursively walk the expressions of a SELECT statement and generate
          774  +** a bitmask indicating which tables are used in that expression
          775  +** tree.
          776  +*/
          777  +static Bitmask exprSelectUsage(WhereMaskSet *pMaskSet, Select *pS){
          778  +  Bitmask mask = 0;
          779  +  while( pS ){
          780  +    SrcList *pSrc = pS->pSrc;
          781  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pEList);
          782  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pGroupBy);
          783  +    mask |= sqlite3WhereExprListUsage(pMaskSet, pS->pOrderBy);
          784  +    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pWhere);
          785  +    mask |= sqlite3WhereExprUsage(pMaskSet, pS->pHaving);
          786  +    if( ALWAYS(pSrc!=0) ){
          787  +      int i;
          788  +      for(i=0; i<pSrc->nSrc; i++){
          789  +        mask |= exprSelectUsage(pMaskSet, pSrc->a[i].pSelect);
          790  +        mask |= sqlite3WhereExprUsage(pMaskSet, pSrc->a[i].pOn);
          791  +      }
          792  +    }
          793  +    pS = pS->pPrior;
          794  +  }
          795  +  return mask;
          796  +}
          797  +
          798  +/*
          799  +** The input to this routine is an WhereTerm structure with only the
          800  +** "pExpr" field filled in.  The job of this routine is to analyze the
          801  +** subexpression and populate all the other fields of the WhereTerm
          802  +** structure.
          803  +**
          804  +** If the expression is of the form "<expr> <op> X" it gets commuted
          805  +** to the standard form of "X <op> <expr>".
          806  +**
          807  +** If the expression is of the form "X <op> Y" where both X and Y are
          808  +** columns, then the original expression is unchanged and a new virtual
          809  +** term of the form "Y <op> X" is added to the WHERE clause and
          810  +** analyzed separately.  The original term is marked with TERM_COPIED
          811  +** and the new term is marked with TERM_DYNAMIC (because it's pExpr
          812  +** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
          813  +** is a commuted copy of a prior term.)  The original term has nChild=1
          814  +** and the copy has idxParent set to the index of the original term.
          815  +*/
          816  +static void exprAnalyze(
          817  +  SrcList *pSrc,            /* the FROM clause */
          818  +  WhereClause *pWC,         /* the WHERE clause */
          819  +  int idxTerm               /* Index of the term to be analyzed */
          820  +){
          821  +  WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
          822  +  WhereTerm *pTerm;                /* The term to be analyzed */
          823  +  WhereMaskSet *pMaskSet;          /* Set of table index masks */
          824  +  Expr *pExpr;                     /* The expression to be analyzed */
          825  +  Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
          826  +  Bitmask prereqAll;               /* Prerequesites of pExpr */
          827  +  Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
          828  +  Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
          829  +  int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
          830  +  int noCase = 0;                  /* uppercase equivalent to lowercase */
          831  +  int op;                          /* Top-level operator.  pExpr->op */
          832  +  Parse *pParse = pWInfo->pParse;  /* Parsing context */
          833  +  sqlite3 *db = pParse->db;        /* Database connection */
          834  +
          835  +  if( db->mallocFailed ){
          836  +    return;
          837  +  }
          838  +  pTerm = &pWC->a[idxTerm];
          839  +  pMaskSet = &pWInfo->sMaskSet;
          840  +  pExpr = pTerm->pExpr;
          841  +  assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
          842  +  prereqLeft = sqlite3WhereExprUsage(pMaskSet, pExpr->pLeft);
          843  +  op = pExpr->op;
          844  +  if( op==TK_IN ){
          845  +    assert( pExpr->pRight==0 );
          846  +    if( ExprHasProperty(pExpr, EP_xIsSelect) ){
          847  +      pTerm->prereqRight = exprSelectUsage(pMaskSet, pExpr->x.pSelect);
          848  +    }else{
          849  +      pTerm->prereqRight = sqlite3WhereExprListUsage(pMaskSet, pExpr->x.pList);
          850  +    }
          851  +  }else if( op==TK_ISNULL ){
          852  +    pTerm->prereqRight = 0;
          853  +  }else{
          854  +    pTerm->prereqRight = sqlite3WhereExprUsage(pMaskSet, pExpr->pRight);
          855  +  }
          856  +  prereqAll = sqlite3WhereExprUsage(pMaskSet, pExpr);
          857  +  if( ExprHasProperty(pExpr, EP_FromJoin) ){
          858  +    Bitmask x = sqlite3WhereGetMask(pMaskSet, pExpr->iRightJoinTable);
          859  +    prereqAll |= x;
          860  +    extraRight = x-1;  /* ON clause terms may not be used with an index
          861  +                       ** on left table of a LEFT JOIN.  Ticket #3015 */
          862  +  }
          863  +  pTerm->prereqAll = prereqAll;
          864  +  pTerm->leftCursor = -1;
          865  +  pTerm->iParent = -1;
          866  +  pTerm->eOperator = 0;
          867  +  if( allowedOp(op) ){
          868  +    Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
          869  +    Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
          870  +    u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
          871  +    if( pLeft->op==TK_COLUMN ){
          872  +      pTerm->leftCursor = pLeft->iTable;
          873  +      pTerm->u.leftColumn = pLeft->iColumn;
          874  +      pTerm->eOperator = operatorMask(op) & opMask;
          875  +    }
          876  +    if( op==TK_IS ) pTerm->wtFlags |= TERM_IS;
          877  +    if( pRight && pRight->op==TK_COLUMN ){
          878  +      WhereTerm *pNew;
          879  +      Expr *pDup;
          880  +      u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
          881  +      if( pTerm->leftCursor>=0 ){
          882  +        int idxNew;
          883  +        pDup = sqlite3ExprDup(db, pExpr, 0);
          884  +        if( db->mallocFailed ){
          885  +          sqlite3ExprDelete(db, pDup);
          886  +          return;
          887  +        }
          888  +        idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
          889  +        if( idxNew==0 ) return;
          890  +        pNew = &pWC->a[idxNew];
          891  +        markTermAsChild(pWC, idxNew, idxTerm);
          892  +        if( op==TK_IS ) pNew->wtFlags |= TERM_IS;
          893  +        pTerm = &pWC->a[idxTerm];
          894  +        pTerm->wtFlags |= TERM_COPIED;
          895  +
          896  +        if( termIsEquivalence(pParse, pDup) ){
          897  +          pTerm->eOperator |= WO_EQUIV;
          898  +          eExtraOp = WO_EQUIV;
          899  +        }
          900  +      }else{
          901  +        pDup = pExpr;
          902  +        pNew = pTerm;
          903  +      }
          904  +      exprCommute(pParse, pDup);
          905  +      pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
          906  +      pNew->leftCursor = pLeft->iTable;
          907  +      pNew->u.leftColumn = pLeft->iColumn;
          908  +      testcase( (prereqLeft | extraRight) != prereqLeft );
          909  +      pNew->prereqRight = prereqLeft | extraRight;
          910  +      pNew->prereqAll = prereqAll;
          911  +      pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
          912  +    }
          913  +  }
          914  +
          915  +#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
          916  +  /* If a term is the BETWEEN operator, create two new virtual terms
          917  +  ** that define the range that the BETWEEN implements.  For example:
          918  +  **
          919  +  **      a BETWEEN b AND c
          920  +  **
          921  +  ** is converted into:
          922  +  **
          923  +  **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
          924  +  **
          925  +  ** The two new terms are added onto the end of the WhereClause object.
          926  +  ** The new terms are "dynamic" and are children of the original BETWEEN
          927  +  ** term.  That means that if the BETWEEN term is coded, the children are
          928  +  ** skipped.  Or, if the children are satisfied by an index, the original
          929  +  ** BETWEEN term is skipped.
          930  +  */
          931  +  else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
          932  +    ExprList *pList = pExpr->x.pList;
          933  +    int i;
          934  +    static const u8 ops[] = {TK_GE, TK_LE};
          935  +    assert( pList!=0 );
          936  +    assert( pList->nExpr==2 );
          937  +    for(i=0; i<2; i++){
          938  +      Expr *pNewExpr;
          939  +      int idxNew;
          940  +      pNewExpr = sqlite3PExpr(pParse, ops[i], 
          941  +                             sqlite3ExprDup(db, pExpr->pLeft, 0),
          942  +                             sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
          943  +      transferJoinMarkings(pNewExpr, pExpr);
          944  +      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
          945  +      testcase( idxNew==0 );
          946  +      exprAnalyze(pSrc, pWC, idxNew);
          947  +      pTerm = &pWC->a[idxTerm];
          948  +      markTermAsChild(pWC, idxNew, idxTerm);
          949  +    }
          950  +  }
          951  +#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
          952  +
          953  +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
          954  +  /* Analyze a term that is composed of two or more subterms connected by
          955  +  ** an OR operator.
          956  +  */
          957  +  else if( pExpr->op==TK_OR ){
          958  +    assert( pWC->op==TK_AND );
          959  +    exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
          960  +    pTerm = &pWC->a[idxTerm];
          961  +  }
          962  +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */
          963  +
          964  +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
          965  +  /* Add constraints to reduce the search space on a LIKE or GLOB
          966  +  ** operator.
          967  +  **
          968  +  ** A like pattern of the form "x LIKE 'aBc%'" is changed into constraints
          969  +  **
          970  +  **          x>='ABC' AND x<'abd' AND x LIKE 'aBc%'
          971  +  **
          972  +  ** The last character of the prefix "abc" is incremented to form the
          973  +  ** termination condition "abd".  If case is not significant (the default
          974  +  ** for LIKE) then the lower-bound is made all uppercase and the upper-
          975  +  ** bound is made all lowercase so that the bounds also work when comparing
          976  +  ** BLOBs.
          977  +  */
          978  +  if( pWC->op==TK_AND 
          979  +   && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
          980  +  ){
          981  +    Expr *pLeft;       /* LHS of LIKE/GLOB operator */
          982  +    Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
          983  +    Expr *pNewExpr1;
          984  +    Expr *pNewExpr2;
          985  +    int idxNew1;
          986  +    int idxNew2;
          987  +    const char *zCollSeqName;     /* Name of collating sequence */
          988  +    const u16 wtFlags = TERM_LIKEOPT | TERM_VIRTUAL | TERM_DYNAMIC;
          989  +
          990  +    pLeft = pExpr->x.pList->a[1].pExpr;
          991  +    pStr2 = sqlite3ExprDup(db, pStr1, 0);
          992  +
          993  +    /* Convert the lower bound to upper-case and the upper bound to
          994  +    ** lower-case (upper-case is less than lower-case in ASCII) so that
          995  +    ** the range constraints also work for BLOBs
          996  +    */
          997  +    if( noCase && !pParse->db->mallocFailed ){
          998  +      int i;
          999  +      char c;
         1000  +      pTerm->wtFlags |= TERM_LIKE;
         1001  +      for(i=0; (c = pStr1->u.zToken[i])!=0; i++){
         1002  +        pStr1->u.zToken[i] = sqlite3Toupper(c);
         1003  +        pStr2->u.zToken[i] = sqlite3Tolower(c);
         1004  +      }
         1005  +    }
         1006  +
         1007  +    if( !db->mallocFailed ){
         1008  +      u8 c, *pC;       /* Last character before the first wildcard */
         1009  +      pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
         1010  +      c = *pC;
         1011  +      if( noCase ){
         1012  +        /* The point is to increment the last character before the first
         1013  +        ** wildcard.  But if we increment '@', that will push it into the
         1014  +        ** alphabetic range where case conversions will mess up the 
         1015  +        ** inequality.  To avoid this, make sure to also run the full
         1016  +        ** LIKE on all candidate expressions by clearing the isComplete flag
         1017  +        */
         1018  +        if( c=='A'-1 ) isComplete = 0;
         1019  +        c = sqlite3UpperToLower[c];
         1020  +      }
         1021  +      *pC = c + 1;
         1022  +    }
         1023  +    zCollSeqName = noCase ? "NOCASE" : "BINARY";
         1024  +    pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
         1025  +    pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
         1026  +           sqlite3ExprAddCollateString(pParse,pNewExpr1,zCollSeqName),
         1027  +           pStr1, 0);
         1028  +    transferJoinMarkings(pNewExpr1, pExpr);
         1029  +    idxNew1 = whereClauseInsert(pWC, pNewExpr1, wtFlags);
         1030  +    testcase( idxNew1==0 );
         1031  +    exprAnalyze(pSrc, pWC, idxNew1);
         1032  +    pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
         1033  +    pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
         1034  +           sqlite3ExprAddCollateString(pParse,pNewExpr2,zCollSeqName),
         1035  +           pStr2, 0);
         1036  +    transferJoinMarkings(pNewExpr2, pExpr);
         1037  +    idxNew2 = whereClauseInsert(pWC, pNewExpr2, wtFlags);
         1038  +    testcase( idxNew2==0 );
         1039  +    exprAnalyze(pSrc, pWC, idxNew2);
         1040  +    pTerm = &pWC->a[idxTerm];
         1041  +    if( isComplete ){
         1042  +      markTermAsChild(pWC, idxNew1, idxTerm);
         1043  +      markTermAsChild(pWC, idxNew2, idxTerm);
         1044  +    }
         1045  +  }
         1046  +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
         1047  +
         1048  +#ifndef SQLITE_OMIT_VIRTUALTABLE
         1049  +  /* Add a WO_MATCH auxiliary term to the constraint set if the
         1050  +  ** current expression is of the form:  column MATCH expr.
         1051  +  ** This information is used by the xBestIndex methods of
         1052  +  ** virtual tables.  The native query optimizer does not attempt
         1053  +  ** to do anything with MATCH functions.
         1054  +  */
         1055  +  if( isMatchOfColumn(pExpr) ){
         1056  +    int idxNew;
         1057  +    Expr *pRight, *pLeft;
         1058  +    WhereTerm *pNewTerm;
         1059  +    Bitmask prereqColumn, prereqExpr;
         1060  +
         1061  +    pRight = pExpr->x.pList->a[0].pExpr;
         1062  +    pLeft = pExpr->x.pList->a[1].pExpr;
         1063  +    prereqExpr = sqlite3WhereExprUsage(pMaskSet, pRight);
         1064  +    prereqColumn = sqlite3WhereExprUsage(pMaskSet, pLeft);
         1065  +    if( (prereqExpr & prereqColumn)==0 ){
         1066  +      Expr *pNewExpr;
         1067  +      pNewExpr = sqlite3PExpr(pParse, TK_MATCH, 
         1068  +                              0, sqlite3ExprDup(db, pRight, 0), 0);
         1069  +      idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
         1070  +      testcase( idxNew==0 );
         1071  +      pNewTerm = &pWC->a[idxNew];
         1072  +      pNewTerm->prereqRight = prereqExpr;
         1073  +      pNewTerm->leftCursor = pLeft->iTable;
         1074  +      pNewTerm->u.leftColumn = pLeft->iColumn;
         1075  +      pNewTerm->eOperator = WO_MATCH;
         1076  +      markTermAsChild(pWC, idxNew, idxTerm);
         1077  +      pTerm = &pWC->a[idxTerm];
         1078  +      pTerm->wtFlags |= TERM_COPIED;
         1079  +      pNewTerm->prereqAll = pTerm->prereqAll;
         1080  +    }
         1081  +  }
         1082  +#endif /* SQLITE_OMIT_VIRTUALTABLE */
         1083  +
         1084  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1085  +  /* When sqlite_stat3 histogram data is available an operator of the
         1086  +  ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
         1087  +  ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
         1088  +  ** virtual term of that form.
         1089  +  **
         1090  +  ** Note that the virtual term must be tagged with TERM_VNULL.
         1091  +  */
         1092  +  if( pExpr->op==TK_NOTNULL
         1093  +   && pExpr->pLeft->op==TK_COLUMN
         1094  +   && pExpr->pLeft->iColumn>=0
         1095  +   && OptimizationEnabled(db, SQLITE_Stat34)
         1096  +  ){
         1097  +    Expr *pNewExpr;
         1098  +    Expr *pLeft = pExpr->pLeft;
         1099  +    int idxNew;
         1100  +    WhereTerm *pNewTerm;
         1101  +
         1102  +    pNewExpr = sqlite3PExpr(pParse, TK_GT,
         1103  +                            sqlite3ExprDup(db, pLeft, 0),
         1104  +                            sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
         1105  +
         1106  +    idxNew = whereClauseInsert(pWC, pNewExpr,
         1107  +                              TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
         1108  +    if( idxNew ){
         1109  +      pNewTerm = &pWC->a[idxNew];
         1110  +      pNewTerm->prereqRight = 0;
         1111  +      pNewTerm->leftCursor = pLeft->iTable;
         1112  +      pNewTerm->u.leftColumn = pLeft->iColumn;
         1113  +      pNewTerm->eOperator = WO_GT;
         1114  +      markTermAsChild(pWC, idxNew, idxTerm);
         1115  +      pTerm = &pWC->a[idxTerm];
         1116  +      pTerm->wtFlags |= TERM_COPIED;
         1117  +      pNewTerm->prereqAll = pTerm->prereqAll;
         1118  +    }
         1119  +  }
         1120  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1121  +
         1122  +  /* Prevent ON clause terms of a LEFT JOIN from being used to drive
         1123  +  ** an index for tables to the left of the join.
         1124  +  */
         1125  +  pTerm->prereqRight |= extraRight;
         1126  +}
         1127  +
         1128  +/***************************************************************************
         1129  +** Routines with file scope above.  Interface to the rest of the where.c
         1130  +** subsystem follows.
         1131  +***************************************************************************/
         1132  +
         1133  +/*
         1134  +** This routine identifies subexpressions in the WHERE clause where
         1135  +** each subexpression is separated by the AND operator or some other
         1136  +** operator specified in the op parameter.  The WhereClause structure
         1137  +** is filled with pointers to subexpressions.  For example:
         1138  +**
         1139  +**    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
         1140  +**           \________/     \_______________/     \________________/
         1141  +**            slot[0]            slot[1]               slot[2]
         1142  +**
         1143  +** The original WHERE clause in pExpr is unaltered.  All this routine
         1144  +** does is make slot[] entries point to substructure within pExpr.
         1145  +**
         1146  +** In the previous sentence and in the diagram, "slot[]" refers to
         1147  +** the WhereClause.a[] array.  The slot[] array grows as needed to contain
         1148  +** all terms of the WHERE clause.
         1149  +*/
         1150  +void sqlite3WhereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
         1151  +  Expr *pE2 = sqlite3ExprSkipCollate(pExpr);
         1152  +  pWC->op = op;
         1153  +  if( pE2==0 ) return;
         1154  +  if( pE2->op!=op ){
         1155  +    whereClauseInsert(pWC, pExpr, 0);
         1156  +  }else{
         1157  +    sqlite3WhereSplit(pWC, pE2->pLeft, op);
         1158  +    sqlite3WhereSplit(pWC, pE2->pRight, op);
         1159  +  }
         1160  +}
         1161  +
         1162  +/*
         1163  +** Initialize a preallocated WhereClause structure.
         1164  +*/
         1165  +void sqlite3WhereClauseInit(
         1166  +  WhereClause *pWC,        /* The WhereClause to be initialized */
         1167  +  WhereInfo *pWInfo        /* The WHERE processing context */
         1168  +){
         1169  +  pWC->pWInfo = pWInfo;
         1170  +  pWC->pOuter = 0;
         1171  +  pWC->nTerm = 0;
         1172  +  pWC->nSlot = ArraySize(pWC->aStatic);
         1173  +  pWC->a = pWC->aStatic;
         1174  +}
         1175  +
         1176  +/*
         1177  +** Deallocate a WhereClause structure.  The WhereClause structure
         1178  +** itself is not freed.  This routine is the inverse of sqlite3WhereClauseInit().
         1179  +*/
         1180  +void sqlite3WhereClauseClear(WhereClause *pWC){
         1181  +  int i;
         1182  +  WhereTerm *a;
         1183  +  sqlite3 *db = pWC->pWInfo->pParse->db;
         1184  +  for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
         1185  +    if( a->wtFlags & TERM_DYNAMIC ){
         1186  +      sqlite3ExprDelete(db, a->pExpr);
         1187  +    }
         1188  +    if( a->wtFlags & TERM_ORINFO ){
         1189  +      whereOrInfoDelete(db, a->u.pOrInfo);
         1190  +    }else if( a->wtFlags & TERM_ANDINFO ){
         1191  +      whereAndInfoDelete(db, a->u.pAndInfo);
         1192  +    }
         1193  +  }
         1194  +  if( pWC->a!=pWC->aStatic ){
         1195  +    sqlite3DbFree(db, pWC->a);
         1196  +  }
         1197  +}
         1198  +
         1199  +
         1200  +/*
         1201  +** These routines walk (recursively) an expression tree and generate
         1202  +** a bitmask indicating which tables are used in that expression
         1203  +** tree.
         1204  +*/
         1205  +Bitmask sqlite3WhereExprUsage(WhereMaskSet *pMaskSet, Expr *p){
         1206  +  Bitmask mask = 0;
         1207  +  if( p==0 ) return 0;
         1208  +  if( p->op==TK_COLUMN ){
         1209  +    mask = sqlite3WhereGetMask(pMaskSet, p->iTable);
         1210  +    return mask;
         1211  +  }
         1212  +  mask = sqlite3WhereExprUsage(pMaskSet, p->pRight);
         1213  +  mask |= sqlite3WhereExprUsage(pMaskSet, p->pLeft);
         1214  +  if( ExprHasProperty(p, EP_xIsSelect) ){
         1215  +    mask |= exprSelectUsage(pMaskSet, p->x.pSelect);
         1216  +  }else{
         1217  +    mask |= sqlite3WhereExprListUsage(pMaskSet, p->x.pList);
         1218  +  }
         1219  +  return mask;
         1220  +}
         1221  +Bitmask sqlite3WhereExprListUsage(WhereMaskSet *pMaskSet, ExprList *pList){
         1222  +  int i;
         1223  +  Bitmask mask = 0;
         1224  +  if( pList ){
         1225  +    for(i=0; i<pList->nExpr; i++){
         1226  +      mask |= sqlite3WhereExprUsage(pMaskSet, pList->a[i].pExpr);
         1227  +    }
         1228  +  }
         1229  +  return mask;
         1230  +}
         1231  +
         1232  +
         1233  +/*
         1234  +** Call exprAnalyze on all terms in a WHERE clause.  
         1235  +**
         1236  +** Note that exprAnalyze() might add new virtual terms onto the
         1237  +** end of the WHERE clause.  We do not want to analyze these new
         1238  +** virtual terms, so start analyzing at the end and work forward
         1239  +** so that the added virtual terms are never processed.
         1240  +*/
         1241  +void sqlite3WhereExprAnalyze(
         1242  +  SrcList *pTabList,       /* the FROM clause */
         1243  +  WhereClause *pWC         /* the WHERE clause to be analyzed */
         1244  +){
         1245  +  int i;
         1246  +  for(i=pWC->nTerm-1; i>=0; i--){
         1247  +    exprAnalyze(pTabList, pWC, i);
         1248  +  }
         1249  +}

Added test/affinity2.test.

            1  +# 2015-06-02
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this file is type affinity in comparison operations.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +
           18  +do_execsql_test affinity2-100 {
           19  +  CREATE TABLE t1(
           20  +    xi INTEGER,
           21  +    xr REAL,
           22  +    xb BLOB,
           23  +    xn NUMERIC,
           24  +    xt TEXT
           25  +  );
           26  +  INSERT INTO t1(rowid,xi,xr,xb,xn,xt) VALUES(1,1,1,1,1,1);
           27  +  INSERT INTO t1(rowid,xi,xr,xb,xn,xt) VALUES(2,'2','2','2','2','2');
           28  +  INSERT INTO t1(rowid,xi,xr,xb,xn,xt) VALUES(3,'03','03','03','03','03');
           29  +
           30  +} {}
           31  +do_execsql_test affinity2-110 {
           32  +  SELECT xi, typeof(xi) FROM t1 ORDER BY rowid;
           33  +} {1 integer 2 integer 3 integer}
           34  +do_execsql_test affinity2-120 {
           35  +  SELECT xr, typeof(xr) FROM t1 ORDER BY rowid;
           36  +} {1.0 real 2.0 real 3.0 real}
           37  +do_execsql_test affinity2-130 {
           38  +  SELECT xb, typeof(xb) FROM t1 ORDER BY rowid;
           39  +} {1 integer 2 text 03 text}
           40  +do_execsql_test affinity2-140 {
           41  +  SELECT xn, typeof(xn) FROM t1 ORDER BY rowid;
           42  +} {1 integer 2 integer 3 integer}
           43  +do_execsql_test affinity2-150 {
           44  +  SELECT xt, typeof(xt) FROM t1 ORDER BY rowid;
           45  +} {1 text 2 text 03 text}
           46  +
           47  +do_execsql_test affinity2-200 {
           48  +  SELECT rowid, xi==xt, xi==xb, xi==+xt FROM t1 ORDER BY rowid;
           49  +} {1 1 1 1 2 1 1 1 3 1 1 1}
           50  +do_execsql_test affinity2-210 {
           51  +  SELECT rowid, xr==xt, xr==xb, xr==+xt FROM t1 ORDER BY rowid;
           52  +} {1 1 1 1 2 1 1 1 3 1 1 1}
           53  +do_execsql_test affinity2-220 {
           54  +  SELECT rowid, xn==xt, xn==xb, xn==+xt FROM t1 ORDER BY rowid;
           55  +} {1 1 1 1 2 1 1 1 3 1 1 1}
           56  +
           57  +do_execsql_test affinity2-300 {
           58  +  SELECT rowid, xt==+xi, xt==xi, xt==xb FROM t1 ORDER BY rowid;
           59  +} {1 1 1 0 2 1 1 1 3 0 1 1}
           60  +
           61  +finish_test

Changes to test/analyzer1.test.

    19     19     return
    20     20   }
    21     21   
    22     22   if {$tcl_platform(platform)=="windows"} {
    23     23     set PROG "sqlite3_analyzer.exe"
    24     24   } else {
    25     25     set PROG "./sqlite3_analyzer"
           26  +}
           27  +if {![file exe $PROG]} {
           28  +  puts "analyzer1 cannot run because $PROG is not available"
           29  +  finish_test
           30  +  return
    26     31   }
    27     32   db close
    28     33   forcedelete test.db test.db-journal test.db-wal
    29     34   sqlite3 db test.db
    30     35   
    31     36   do_test analyzer1-1.0 {
    32     37     db eval {

Changes to test/e_walauto.test.

   167    167     } 0
   168    168   
   169    169     # EVIDENCE-OF: R-33080-59193 Checkpoints initiated by this mechanism 
   170    170     # are PASSIVE.
   171    171     #
   172    172     set ::busy_callback_count 0
   173    173     proc busy_callback {args} {
   174         -  puts Hello
   175    174       incr ::busy_callback_count
   176    175       return 0
   177    176     }
   178    177     do_test 1.$tn.12.1 {
   179    178       sqlite3_wal_checkpoint_v2 db truncate
   180    179       autocheckpoint db 100 
   181    180       db busy busy_callback

Added test/extraquick.test.

            1  +#
            2  +#    May you do good and not evil.
            3  +#    May you find forgiveness for yourself and forgive others.
            4  +#    May you share freely, never taking more than you give.
            5  +#
            6  +#***********************************************************************
            7  +# This file runs most of the tests run by veryquick.test except for those
            8  +# that take a long time.
            9  +#
           10  +
           11  +set testdir [file dirname $argv0]
           12  +source $testdir/permutations.test
           13  +
           14  +run_test_suite extraquick
           15  +
           16  +finish_test

Changes to test/filectrl.test.

    35     35     db close
    36     36     sqlite3 db test_control_lockproxy.db
    37     37     file_control_lockproxy_test db [get_pwd]
    38     38   } {}
    39     39   do_test filectrl-1.6 {
    40     40     sqlite3 db test.db
    41     41     set fn [file_control_tempfilename db]
    42         -  puts -nonewline \[$fn\]
    43     42     set fn
    44     43   } {/etilqs_/}
    45     44   db close
    46     45   forcedelete .test_control_lockproxy.db-conch test.proxy
    47     46   finish_test

Changes to test/fts3d.test.

   209    209       SELECT OFFSETS(t1) FROM t1
   210    210        WHERE t1 MATCH 'this OR that OR was OR a OR is OR test' ORDER BY docid;
   211    211     }
   212    212   } [list {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4} \
   213    213           {0 1 0 4 0 2 5 3 0 3 9 1 0 5 11 4} \
   214    214           {0 0 0 4 0 4 5 2 0 3 8 1 0 5 10 4}]
   215    215   
   216         -puts [db eval {SELECT c FROM t1 } ]
          216  +db eval {SELECT c FROM t1 }
   217    217   check_terms_all fts3d-4.1      {a four is test that this was}
   218    218   check_doclist_all fts3d-4.1.1  a {[1 0[2]] [2 0[2]] [3 0[2]]}
   219    219   check_doclist_all fts3d-4.1.2  four {}
   220    220   check_doclist_all fts3d-4.1.3  is {[1 0[1]] [3 0[1]]}
   221    221   #check_doclist_all fts3d-4.1.4  one {}
   222    222   check_doclist_all fts3d-4.1.5  test {[1 0[3]] [2 0[3]] [3 0[3]]}
   223    223   check_doclist_all fts3d-4.1.6  that {[2 0[0]]}

Changes to test/fts4incr.test.

    43     43         MATCH '"land of canaan"' AND docid < 1030000 } 7
    44     44   } {
    45     45     foreach s {0 1} {
    46     46       execsql "INSERT INTO t1(t1) VALUES('test-no-incr-doclist=$s')"
    47     47       do_execsql_test 2.$tn.$s $q $res
    48     48       set t($s) [lindex [time [list execsql $q] 100] 0]
    49     49     }
    50         -  puts "with optimization: $t(0)    without: $t(1)"
           50  +  if {0} {
           51  +    puts "with optimization: $t(0)    without: $t(1)"
           52  +  }
    51     53   }
    52     54   
    53     55   do_test 2.1 {
    54     56     execsql {
    55     57       CREATE VIRTUAL TABLE t2 USING fts4(order=DESC);
    56     58     }
    57     59     set num [list one two three four five six seven eight nine ten]

Changes to test/fuzzdata3.db.

cannot compute difference between binary files

Changes to test/index5.test.

    63     63       } elseif {$iNext==($iPrev-1)} { 
    64     64         incr nBackward 
    65     65       } else {
    66     66         incr nNoncont
    67     67       }
    68     68       set iPrev $iNext
    69     69     }
    70         -  puts -nonewline \
    71         -      " (forward=$nForward, back=$nBackward, noncontiguous=$nNoncont)"
           70  +  if {0} {
           71  +    puts -nonewline \
           72  +        " (forward=$nForward, back=$nBackward, noncontiguous=$nNoncont)"
           73  +  }
    72     74   
    73     75     expr {$nForward > 2*($nBackward + $nNoncont)}
    74     76   } {1}
    75     77   db close
    76     78   tvfs delete
    77     79   
    78     80   finish_test

Changes to test/index6.test.

   322    322   do_execsql_test index6-8.2 {
   323    323     SELECT * FROM t8a LEFT JOIN t8b ON (x = 'value' AND y = a)
   324    324   } {
   325    325     1 one value 1 
   326    326     2 two {} {} 
   327    327     3 three value 3
   328    328   }
          329  +
          330  +# 2015-06-11.  Assertion fault found by AFL
          331  +#
          332  +do_execsql_test index6-9.1 {
          333  +  CREATE TABLE t9(a int, b int, c int);
          334  +  CREATE INDEX t9ca ON t9(c,a) WHERE a in (10,12,20);
          335  +  INSERT INTO t9 VALUES(1,1,9),(10,2,35),(11,15,82),(20,19,5),(NULL,7,3);
          336  +  UPDATE t9 SET b=c WHERE a in (10,12,20);
          337  +  SELECT a,b,c,'|' FROM t9 ORDER BY a;
          338  +} {{} 7 3 | 1 1 9 | 10 35 35 | 11 15 82 | 20 5 5 |}
          339  +do_execsql_test index6-9.2 {
          340  +  DROP TABLE t9;
          341  +  CREATE TABLE t9(a int, b int, c int, PRIMARY KEY(a)) WITHOUT ROWID;
          342  +  CREATE INDEX t9ca ON t9(c,a) WHERE a in (10,12,20);
          343  +  INSERT INTO t9 VALUES(1,1,9),(10,2,35),(11,15,82),(20,19,5);
          344  +  UPDATE t9 SET b=c WHERE a in (10,12,20);
          345  +  SELECT a,b,c,'|' FROM t9 ORDER BY a;
          346  +} {1 1 9 | 10 35 35 | 11 15 82 | 20 5 5 |}
          347  +
   329    348   
   330    349   finish_test

Changes to test/join.test.

   682    682     if {[lsearch [db eval {PRAGMA compile_options}] MEMDEBUG]<0} {
   683    683       jointest join-12.10 65534 {1 {at most 64 tables in a join}}
   684    684       jointest join-12.11 65535 {1 {too many references to "t14": max 65535}}
   685    685       jointest join-12.12 65536 {1 {too many references to "t14": max 65535}}
   686    686       jointest join-12.13 65537 {1 {too many references to "t14": max 65535}}
   687    687     }
   688    688   }
          689  +
          690  +
          691  +#-------------------------------------------------------------------------
          692  +# Test a problem with reordering tables following a LEFT JOIN.
          693  +#
          694  +do_execsql_test join-13.0 {
          695  +  CREATE TABLE aa(a);
          696  +  CREATE TABLE bb(b);
          697  +  CREATE TABLE cc(c);
          698  +
          699  +  INSERT INTO aa VALUES(45);
          700  +  INSERT INTO cc VALUES(45);
          701  +  INSERT INTO cc VALUES(45);
          702  +}
          703  +
          704  +do_execsql_test join-13.1 {
          705  +  SELECT * FROM aa LEFT JOIN bb, cc WHERE cc.c=aa.a;
          706  +} {45 {} 45 45 {} 45}
          707  +
          708  +# In the following, the order of [cc] and [bb] must not be exchanged, even
          709  +# though this would be helpful if the query used an inner join.
          710  +do_execsql_test join-13.2 {
          711  +  CREATE INDEX ccc ON cc(c);
          712  +  SELECT * FROM aa LEFT JOIN bb, cc WHERE cc.c=aa.a;
          713  +} {45 {} 45 45 {} 45}
          714  +
   689    715   
   690    716   finish_test

Changes to test/permutations.test.

    95     95   
    96     96   if {$::tcl_platform(platform)!="unix"} {
    97     97     set alltests [test_set $alltests -exclude crash.test crash2.test]
    98     98   }
    99     99   set alltests [test_set $alltests -exclude {
   100    100     all.test        async.test         quick.test  veryquick.test
   101    101     memleak.test    permutations.test  soak.test   fts3.test
   102         -  mallocAll.test  rtree.test         full.test   session.test
          102  +  mallocAll.test  rtree.test         full.test   extraquick.test
          103  +  session.test
   103    104   }]
   104    105   
   105    106   set allquicktests [test_set $alltests -exclude {
   106    107     async2.test async3.test backup_ioerr.test corrupt.test
   107    108     corruptC.test crash.test crash2.test crash3.test crash4.test crash5.test
   108    109     crash6.test crash7.test delete3.test e_fts3.test fts3rnd.test
   109    110     fkey_malloc.test fuzz.test fuzz3.test fuzz_malloc.test in2.test loadext.test
................................................................................
   145    146   #   veryquick
   146    147   #   quick
   147    148   #   full
   148    149   #
   149    150   lappend ::testsuitelist xxx
   150    151   
   151    152   test_suite "veryquick" -prefix "" -description {
   152         -  "Very" quick test suite. Runs in less than 5 minutes on a workstation. 
          153  +  "Very" quick test suite. Runs in minutes on a workstation.
   153    154     This test suite is the same as the "quick" tests, except that some files
   154    155     that test malloc and IO errors are omitted.
   155    156   } -files [
   156         -  test_set $allquicktests -exclude *malloc* *ioerr* *fault*
          157  +  test_set $allquicktests -exclude *malloc* *ioerr* *fault* *bigfile*
          158  +]
          159  +
          160  +test_suite "extraquick" -prefix "" -description {
          161  +  "Extra" quick test suite. Runs in a few minutes on a workstation.
          162  +  This test suite is the same as the "veryquick" tests, except that
          163  +  slower tests are omitted.
          164  +} -files [
          165  +  test_set $allquicktests -exclude *malloc* *ioerr* *fault* *bigfile* \
          166  +     wal3.test fts4merge* sort2.test mmap1.test walcrash* \
          167  +     percentile.test where8m.test walcksum.test savepoint3.test \
          168  +     fuzzer1.test fuzzer3.test fts3expr3.test
   157    169   ]
   158    170   
   159    171   test_suite "mmap" -prefix "mm-" -description {
   160    172     Similar to veryquick. Except with memory mapping enabled.
   161    173   } -presql {
   162    174     pragma mmap_size = 268435456;
   163    175   } -files [

Changes to test/progress.test.

   160    160       CREATE TABLE abc(a, b, c);
   161    161       INSERT INTO abc VALUES(1, 2, 3);
   162    162       INSERT INTO abc VALUES(4, 5, 6);
   163    163       INSERT INTO abc VALUES(7, 8, 9);
   164    164     }
   165    165   
   166    166     set ::res [list]
   167         -  explain {SELECT a, b, c FROM abc} 
   168    167     db eval {SELECT a, b, c FROM abc} {
   169    168       lappend ::res $a $b $c
   170    169       db progress 5 "expr 1"
   171    170       catch {db eval {SELECT a, b, c FROM abc} { }} msg
   172    171       db progress 5 "expr 0"
   173    172       lappend ::res $msg
   174    173     }
   175    174   
   176    175     set ::res
   177    176   } {1 2 3 interrupted 4 5 6 interrupted 7 8 9 interrupted}
   178    177   
   179    178   finish_test

Changes to test/releasetest.tcl.

   543    543         }
   544    544   
   545    545         -info {
   546    546           puts "Command-line Options:"
   547    547           puts "   --srcdir $::SRCDIR"
   548    548           puts "   --platform [list $platform]"
   549    549           puts "   --config [list $config]"
   550         -        if {$::QUICK}     {puts "   --quick"}
          550  +        if {$::QUICK} {
          551  +          if {$::QUICK==1} {puts "   --quick"}
          552  +          if {$::QUICK==2} {puts "   --veryquick"}
          553  +        }
   551    554           if {$::MSVC}      {puts "   --msvc"}
   552    555           if {$::BUILDONLY} {puts "   --buildonly"}
   553    556           if {$::DRYRUN}    {puts "   --dryrun"}
   554    557           if {$::TRACE}     {puts "   --trace"}
   555    558           puts "\nAvailable --platform options:"
   556    559           foreach y [lsort [array names ::Platforms]] {
   557    560             puts "   [list $y]"
................................................................................
   641    644       if {$::MSVC && ($zConfig eq "Sanitize" || "checksymbols" in $target
   642    645              || "valgrindtest" in $target)} {
   643    646         puts "Skipping $zConfig / $target for MSVC..."
   644    647         continue
   645    648       }
   646    649       if {$target ne "checksymbols"} {
   647    650         switch -- $::QUICK {
   648         -         1 {set target test}
          651  +         1 {set target quicktest}
   649    652            2 {set target smoketest}
   650    653         }
   651    654         if {$::BUILDONLY} {
   652    655           set target testfixture
   653    656           if {$::MSVC} {append target .exe}
   654    657         }
   655    658       }

Changes to test/select8.test.

    28     28     INSERT INTO songs VALUES(6,'two',11);
    29     29   }
    30     30   set result [execsql {
    31     31     SELECT DISTINCT artist,sum(timesplayed) AS total      
    32     32     FROM songs      
    33     33     GROUP BY LOWER(artist)      
    34     34   }]
    35         -puts result=$result
    36     35   do_test select8-1.1 {
    37     36     execsql {
    38     37       SELECT DISTINCT artist,sum(timesplayed) AS total      
    39     38       FROM songs      
    40     39       GROUP BY LOWER(artist)      
    41     40       LIMIT 1 OFFSET 1
    42     41     }

Changes to test/shared4.test.

    12     12   # Test the btree mutex protocol for shared cache mode.
    13     13   #
    14     14   # $Id: shared4.test,v 1.2 2008/08/04 03:51:24 danielk1977 Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
    18     18   db close
    19         -puts hello
    20     19   
    21     20   # This script is only valid if we are running shared-cache mode in a
    22     21   # threadsafe-capable database engine.
    23     22   #
    24     23   ifcapable !shared_cache||!compound {
    25     24     finish_test
    26     25     return

Changes to test/sqldiff1.test.

    14     14   set testdir [file dirname $argv0]
    15     15   source $testdir/tester.tcl
    16     16   
    17     17   if {$tcl_platform(platform)=="windows"} {
    18     18     set PROG "sqldiff.exe"
    19     19   } else {
    20     20     set PROG "./sqldiff"
           21  +}
           22  +if {![file exe $PROG]} {
           23  +  puts "sqldiff cannot run because $PROG is not available"
           24  +  finish_test
           25  +  return
    21     26   }
    22     27   db close
    23     28   forcedelete test.db test2.db
    24     29   sqlite3 db test.db
    25     30   
    26     31   do_test sqldiff-1.0 {
    27     32     db eval {

Changes to test/tester.tcl.

    77     77   #
    78     78   #      wal_is_wal_mode
    79     79   #      wal_set_journal_mode   ?DB?
    80     80   #      wal_check_journal_mode TESTNAME?DB?
    81     81   #      permutation
    82     82   #      presql
    83     83   #
           84  +# Command to test whether or not --verbose=1 was specified on the command
           85  +# line (returns 0 for not-verbose, 1 for verbose and 2 for "verbose in the
           86  +# output file only").
           87  +#
           88  +#      verbose
           89  +#
    84     90   
    85     91   # Set the precision of FP arithmatic used by the interpreter. And
    86     92   # configure SQLite to take database file locks on the page that begins
    87     93   # 64KB into the database file instead of the one 1GB in. This means
    88     94   # the code that handles that special case can be tested without creating
    89     95   # very large database files.
    90     96   #
................................................................................
   384    390     #   --backtrace=N
   385    391     #   --binarylog=N
   386    392     #   --soak=N
   387    393     #   --file-retries=N
   388    394     #   --file-retry-delay=N
   389    395     #   --start=[$permutation:]$testfile
   390    396     #   --match=$pattern
          397  +  #   --verbose=$val
          398  +  #   --output=$filename
          399  +  #   --help
   391    400     #
   392    401     set cmdlinearg(soft-heap-limit)    0
   393    402     set cmdlinearg(maxerror)        1000
   394    403     set cmdlinearg(malloctrace)        0
   395    404     set cmdlinearg(backtrace)         10
   396    405     set cmdlinearg(binarylog)          0
   397    406     set cmdlinearg(soak)               0
   398    407     set cmdlinearg(file-retries)       0
   399    408     set cmdlinearg(file-retry-delay)   0
   400    409     set cmdlinearg(start)             ""
   401    410     set cmdlinearg(match)             ""
          411  +  set cmdlinearg(verbose)           ""
          412  +  set cmdlinearg(output)            ""
   402    413   
   403    414     set leftover [list]
   404    415     foreach a $argv {
   405    416       switch -regexp -- $a {
   406    417         {^-+pause$} {
   407    418           # Wait for user input before continuing. This is to give the user an
   408    419           # opportunity to connect profiling tools to the process.
................................................................................
   453    464         }
   454    465         {^-+match=.+$} {
   455    466           foreach {dummy cmdlinearg(match)} [split $a =] break
   456    467   
   457    468           set ::G(match) $cmdlinearg(match)
   458    469           if {$::G(match) == ""} {unset ::G(match)}
   459    470         }
          471  +
          472  +      {^-+output=.+$} {
          473  +        foreach {dummy cmdlinearg(output)} [split $a =] break
          474  +        if {$cmdlinearg(verbose)==""} {
          475  +          set cmdlinearg(verbose) 2
          476  +        }
          477  +      }
          478  +      {^-+verbose=.+$} {
          479  +        foreach {dummy cmdlinearg(verbose)} [split $a =] break
          480  +        if {$cmdlinearg(verbose)=="file"} {
          481  +          set cmdlinearg(verbose) 2
          482  +        } elseif {[string is boolean -strict $cmdlinearg(verbose)]==0} {
          483  +          error "option --verbose= must be set to a boolean or to \"file\""
          484  +        }
          485  +      }
          486  +
   460    487         default {
   461    488           lappend leftover $a
   462    489         }
   463    490       }
   464    491     }
   465    492     set argv $leftover
   466    493   
................................................................................
   480    507     }
   481    508   
   482    509     # Set the backtrace depth, if malloc tracing is enabled.
   483    510     #
   484    511     if {$cmdlinearg(malloctrace)} {
   485    512       sqlite3_memdebug_backtrace $cmdlinearg(backtrace)
   486    513     }
          514  +
          515  +  if {$cmdlinearg(output)!=""} {
          516  +    puts "Copying output to file $cmdlinearg(output)"
          517  +    set ::G(output_fd) [open $cmdlinearg(output) w]
          518  +    fconfigure $::G(output_fd) -buffering line
          519  +  }
          520  +
          521  +  if {$cmdlinearg(verbose)==""} {
          522  +    set cmdlinearg(verbose) 1
          523  +  }
   487    524   }
   488    525   
   489    526   # Update the soft-heap-limit each time this script is run. In that
   490    527   # way if an individual test file changes the soft-heap-limit, it
   491    528   # will be reset at the start of the next test file.
   492    529   #
   493    530   sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
................................................................................
   550    587     set f [set_test_counter fail_list]
   551    588     lappend f $name
   552    589     set_test_counter fail_list $f
   553    590     set_test_counter errors [expr [set_test_counter errors] + 1]
   554    591   
   555    592     set nFail [set_test_counter errors]
   556    593     if {$nFail>=$::cmdlinearg(maxerror)} {
   557         -    puts "*** Giving up..."
          594  +    output2 "*** Giving up..."
   558    595       finalize_testing
   559    596     }
   560    597   }
   561    598   
   562    599   # Remember a warning message to be displayed at the conclusion of all testing
   563    600   #
   564    601   proc warning {msg {append 1}} {
   565         -  puts "Warning: $msg"
          602  +  output2 "Warning: $msg"
   566    603     set warnList [set_test_counter warn_list]
   567    604     if {$append} {
   568    605       lappend warnList $msg
   569    606     }
   570    607     set_test_counter warn_list $warnList
   571    608   }
   572    609   
   573    610   
   574    611   # Increment the number of tests run
   575    612   #
   576    613   proc incr_ntest {} {
   577    614     set_test_counter count [expr [set_test_counter count] + 1]
   578    615   }
          616  +
          617  +# Return true if --verbose=1 was specified on the command line. Otherwise,
          618  +# return false.
          619  +#
          620  +proc verbose {} {
          621  +  return $::cmdlinearg(verbose)
          622  +}
          623  +
          624  +# Use the following commands instead of [puts] for test output within
          625  +# this file. Test scripts can still use regular [puts], which is directed
          626  +# to stdout and, if one is open, the --output file.
          627  +#
          628  +# output1: output that should be printed if --verbose=1 was specified.
          629  +# output2: output that should be printed unconditionally.
          630  +# output2_if_no_verbose: output that should be printed only if --verbose=0.
          631  +#
          632  +proc output1 {args} {
          633  +  set v [verbose]
          634  +  if {$v==1} {
          635  +    uplevel output2 $args
          636  +  } elseif {$v==2} {
          637  +    uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
          638  +  }
          639  +}
          640  +proc output2 {args} {
          641  +  set nArg [llength $args]
          642  +  uplevel puts $args
          643  +}
          644  +proc output2_if_no_verbose {args} {
          645  +  set v [verbose]
          646  +  if {$v==0} {
          647  +    uplevel output2 $args
          648  +  } elseif {$v==2} {
          649  +    uplevel puts [lrange $args 0 end-1] stdout [lrange $args end end]
          650  +  }
          651  +}
          652  +
          653  +# Override the [puts] command so that if no channel is explicitly 
          654  +# specified the string is written to both stdout and to the file 
          655  +# specified by "--output=", if any.
          656  +#
          657  +proc puts_override {args} {
          658  +  set nArg [llength $args]
          659  +  if {$nArg==1 || ($nArg==2 && [string first [lindex $args 0] -nonewline]==0)} {
          660  +    uplevel puts_original $args
          661  +    if {[info exists ::G(output_fd)]} {
          662  +      uplevel puts [lrange $args 0 end-1] $::G(output_fd) [lrange $args end end]
          663  +    }
          664  +  } else {
          665  +    # A channel was explicitly specified.
          666  +    uplevel puts_original $args
          667  +  }
          668  +}
          669  +rename puts puts_original
          670  +proc puts {args} { uplevel puts_override $args }
   579    671   
   580    672   
   581    673   # Invoke the do_test procedure to run a single test
   582    674   #
   583    675   proc do_test {name cmd expected} {
   584    676     global argv cmdlinearg
   585    677   
................................................................................
   600    692   #  }
   601    693   
   602    694     if {[info exists ::G(perm:prefix)]} {
   603    695       set name "$::G(perm:prefix)$name"
   604    696     }
   605    697   
   606    698     incr_ntest
   607         -  puts -nonewline $name...
          699  +  output1 -nonewline $name...
   608    700     flush stdout
   609    701   
   610    702     if {![info exists ::G(match)] || [string match $::G(match) $name]} {
   611    703       if {[catch {uplevel #0 "$cmd;\n"} result]} {
   612         -      puts "\nError: $result"
          704  +      output2_if_no_verbose -nonewline $name...
          705  +      output2 "\nError: $result"
   613    706         fail_test $name
   614    707       } else {
   615    708         if {[regexp {^~?/.*/$} $expected]} {
   616    709           # "expected" is of the form "/PATTERN/" then the result if correct if
   617    710           # regular expression PATTERN matches the result.  "~/PATTERN/" means
   618    711           # the regular expression must not match.
   619    712           if {[string index $expected 0]=="~"} {
................................................................................
   649    742         } else {
   650    743           set ok [expr {[string compare $result $expected]==0}]
   651    744         }
   652    745         if {!$ok} {
   653    746           # if {![info exists ::testprefix] || $::testprefix eq ""} {
   654    747           #   error "no test prefix"
   655    748           # }
   656         -        puts "\nExpected: \[$expected\]\n     Got: \[$result\]"
          749  +        output2_if_no_verbose -nonewline $name...
          750  +        output2 "\nExpected: \[$expected\]\n     Got: \[$result\]"
   657    751           fail_test $name
   658    752         } else {
   659         -        puts " Ok"
          753  +        output1 " Ok"
   660    754         }
   661    755       }
   662    756     } else {
   663         -    puts " Omitted"
          757  +    output1 " Omitted"
   664    758       omit_test $name "pattern mismatch" 0
   665    759     }
   666    760     flush stdout
   667    761   }
   668    762   
   669    763   proc dumpbytes {s} {
   670    764     set r ""
................................................................................
   839    933     }
   840    934   }
   841    935   
   842    936   # Run an SQL script.
   843    937   # Return the number of microseconds per statement.
   844    938   #
   845    939   proc speed_trial {name numstmt units sql} {
   846         -  puts -nonewline [format {%-21.21s } $name...]
          940  +  output2 -nonewline [format {%-21.21s } $name...]
   847    941     flush stdout
   848    942     set speed [time {sqlite3_exec_nr db $sql}]
   849    943     set tm [lindex $speed 0]
   850    944     if {$tm == 0} {
   851    945       set rate [format %20s "many"]
   852    946     } else {
   853    947       set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
   854    948     }
   855    949     set u2 $units/s
   856         -  puts [format {%12d uS %s %s} $tm $rate $u2]
          950  +  output2 [format {%12d uS %s %s} $tm $rate $u2]
   857    951     global total_time
   858    952     set total_time [expr {$total_time+$tm}]
   859    953     lappend ::speed_trial_times $name $tm
   860    954   }
   861    955   proc speed_trial_tcl {name numstmt units script} {
   862         -  puts -nonewline [format {%-21.21s } $name...]
          956  +  output2 -nonewline [format {%-21.21s } $name...]
   863    957     flush stdout
   864    958     set speed [time {eval $script}]
   865    959     set tm [lindex $speed 0]
   866    960     if {$tm == 0} {
   867    961       set rate [format %20s "many"]
   868    962     } else {
   869    963       set rate [format %20.5f [expr {1000000.0*$numstmt/$tm}]]
   870    964     }
   871    965     set u2 $units/s
   872         -  puts [format {%12d uS %s %s} $tm $rate $u2]
          966  +  output2 [format {%12d uS %s %s} $tm $rate $u2]
   873    967     global total_time
   874    968     set total_time [expr {$total_time+$tm}]
   875    969     lappend ::speed_trial_times $name $tm
   876    970   }
   877    971   proc speed_trial_init {name} {
   878    972     global total_time
   879    973     set total_time 0
   880    974     set ::speed_trial_times [list]
   881    975     sqlite3 versdb :memory:
   882    976     set vers [versdb one {SELECT sqlite_source_id()}]
   883    977     versdb close
   884         -  puts "SQLite $vers"
          978  +  output2 "SQLite $vers"
   885    979   }
   886    980   proc speed_trial_summary {name} {
   887    981     global total_time
   888         -  puts [format {%-21.21s %12d uS TOTAL} $name $total_time]
          982  +  output2 [format {%-21.21s %12d uS TOTAL} $name $total_time]
   889    983   
   890    984     if { 0 } {
   891    985       sqlite3 versdb :memory:
   892    986       set vers [lindex [versdb one {SELECT sqlite_source_id()}] 0]
   893    987       versdb close
   894         -    puts "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
          988  +    output2 "CREATE TABLE IF NOT EXISTS time(version, script, test, us);"
   895    989       foreach {test us} $::speed_trial_times {
   896         -      puts "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
          990  +      output2 "INSERT INTO time VALUES('$vers', '$name', '$test', $us);"
   897    991       }
   898    992     }
   899    993   }
   900    994   
   901    995   # Run this routine last
   902    996   #
   903    997   proc finish_test {} {
................................................................................
   933   1027       close $fd
   934   1028       foreach x $content {set known_error($x) 1}
   935   1029       foreach x [set_test_counter fail_list] {
   936   1030         if {[info exists known_error($x)]} {incr nKnown}
   937   1031       }
   938   1032     }
   939   1033     if {$nKnown>0} {
   940         -    puts "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
         1034  +    output2 "[expr {$nErr-$nKnown}] new errors and $nKnown known errors\
   941   1035            out of $nTest tests"
   942   1036     } else {
   943         -    puts "$nErr errors out of $nTest tests"
         1037  +    output2 "$nErr errors out of $nTest tests"
   944   1038     }
   945   1039     if {$nErr>$nKnown} {
   946         -    puts -nonewline "Failures on these tests:"
         1040  +    output2 -nonewline "Failures on these tests:"
   947   1041       foreach x [set_test_counter fail_list] {
   948         -      if {![info exists known_error($x)]} {puts -nonewline " $x"}
         1042  +      if {![info exists known_error($x)]} {output2 -nonewline " $x"}
   949   1043       }
   950         -    puts ""
         1044  +    output2 ""
   951   1045     }
   952   1046     foreach warning [set_test_counter warn_list] {
   953         -    puts "Warning: $warning"
         1047  +    output2 "Warning: $warning"
   954   1048     }
   955   1049     run_thread_tests 1
   956   1050     if {[llength $omitList]>0} {
   957         -    puts "Omitted test cases:"
         1051  +    output2 "Omitted test cases:"
   958   1052       set prec {}
   959   1053       foreach {rec} [lsort $omitList] {
   960   1054         if {$rec==$prec} continue
   961   1055         set prec $rec
   962         -      puts [format {  %-12s %s} [lindex $rec 0] [lindex $rec 1]]
         1056  +      output2 [format {  %-12s %s} [lindex $rec 0] [lindex $rec 1]]
   963   1057       }
   964   1058     }
   965   1059     if {$nErr>0 && ![working_64bit_int]} {
   966         -    puts "******************************************************************"
   967         -