/ Check-in [6a5c59dd]
Login

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

Overview
Comment:Merge the latest trunk changes (PRAGMA busy_timeout and the ORDER BY query planner enhancements) into the apple-osx branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | apple-osx
Files: files | file ages | folders
SHA1: 6a5c59dd7e0de9b5a2136f1c333afe522f724a71
User & Date: drh 2012-09-28 10:57:42
Context
2014-05-08
23:01
Initial attempt to merge in all trunk changes over the previous 1.5 years. This check-in compiles but there are compiler warnings and "make test" segfaults after only running a few test modules. check-in: 9411d7dc user: drh tags: apple-osx
2012-09-28
10:57
Merge the latest trunk changes (PRAGMA busy_timeout and the ORDER BY query planner enhancements) into the apple-osx branch. check-in: 6a5c59dd user: drh tags: apple-osx
00:44
Query planner enhancements to be more agressive about optimizing out ORDER BY clauses - in particular the query planner now has the ability to omit ORDER BY clauses that span multiple tables in a join. check-in: 1e874629 user: drh tags: trunk
2012-08-31
14:10
Merge in latest trunk changes. check-in: bc9b9cd0 user: dan tags: apple-osx
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   897    897   
   898    898   
   899    899   fulltest:	testfixture$(TEXE) sqlite3$(TEXE)
   900    900   	./testfixture$(TEXE) $(TOP)/test/all.test
   901    901   
   902    902   soaktest:	testfixture$(TEXE) sqlite3$(TEXE)
   903    903   	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1
          904  +
          905  +fulltestonly:	testfixture$(TEXE) sqlite3$(TEXE)
          906  +	./testfixture$(TEXE) $(TOP)/test/full.test
   904    907   
   905    908   test:	testfixture$(TEXE) sqlite3$(TEXE)
   906    909   	./testfixture$(TEXE) $(TOP)/test/veryquick.test
   907    910   
   908    911   sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
   909    912   	echo "#define TCLSH 2" > $@
   910    913   	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@

Changes to Makefile.msc.

    77     77   # Check for the command macro LD.  This should point to the linker binary for
    78     78   # the target platform.  If it is not defined, simply define it to the legacy
    79     79   # default value 'link.exe'.
    80     80   #
    81     81   !IFNDEF LD
    82     82   LD = link.exe
    83     83   !ENDIF
           84  +
           85  +# Check for the predefined command macro RC.  This should point to the resource
           86  +# compiler binary for the target platform.  If it is not defined, simply define
           87  +# it to the legacy default value 'rc.exe'.
           88  +#
           89  +!IFNDEF RC
           90  +RC = rc.exe
           91  +!ENDIF
    84     92   
    85     93   # Check for the command macro NCC.  This should point to the compiler binary
    86     94   # for the platform the compilation process is taking place on.  If it is not
    87     95   # defined, simply define it to have the same value as the CC macro.  When
    88     96   # cross-compiling, it is suggested that this macro be modified via the command
    89     97   # line (since nmake itself does not provide a built-in method to guess it).
    90     98   # For example, to use the x86 compiler when cross-compiling for x64, a command
................................................................................
   145    153   NLTLIBPATHS = "/LIBPATH:$(NCRTLIBPATH)" "/LIBPATH:$(NSDKLIBPATH)"
   146    154   !ENDIF
   147    155   
   148    156   # C compiler and options for use in building executables that
   149    157   # will run on the target platform.  (BCC and TCC are usually the
   150    158   # same unless your are cross-compiling.)
   151    159   #
   152         -TCC = $(CC) -W3 -DSQLITE_OS_WIN=1 -I. -I$(TOP)\src -fp:precise
          160  +TCC = $(CC) -W3 -DSQLITE_OS_WIN=1 -I$(TOP) -I$(TOP)\src -fp:precise
          161  +RCC = $(RC) -DSQLITE_OS_WIN=1 -I$(TOP) -I$(TOP)\src
   153    162   
   154    163   # When compiling the library for use in the WinRT environment,
   155    164   # the following compile-time options must be used as well to
   156    165   # disable use of Win32 APIs that are not available and to enable
   157    166   # use of Win32 APIs that are specific to Windows 8 and/or WinRT.
   158    167   #
   159    168   !IF $(FOR_WINRT)!=0
   160    169   TCC = $(TCC) -DSQLITE_OS_WINRT=1
          170  +RCC = $(RCC) -DSQLITE_OS_WINRT=1
   161    171   TCC = $(TCC) -DWINAPI_FAMILY=WINAPI_PARTITION_APP
          172  +RCC = $(RCC) -DWINAPI_FAMILY=WINAPI_PARTITION_APP
   162    173   !ENDIF
   163    174   
   164    175   # Also, we need to dynamically link to the correct MSVC runtime
   165    176   # when compiling for WinRT (e.g. debug or release) OR if the
   166    177   # USE_CRT_DLL option is set to force dynamically linking to the
   167    178   # MSVC runtime library.
   168    179   #
................................................................................
   182    193   
   183    194   # The mksqlite3c.tcl and mksqlite3h.tcl scripts will pull in
   184    195   # any extension header files by default.  For non-amalgamation
   185    196   # builds, we need to make sure the compiler can find these.
   186    197   #
   187    198   !IF $(USE_AMALGAMATION)==0
   188    199   TCC = $(TCC) -I$(TOP)\ext\fts3
          200  +RCC = $(RCC) -I$(TOP)\ext\fts3
   189    201   TCC = $(TCC) -I$(TOP)\ext\rtree
          202  +RCC = $(RCC) -I$(TOP)\ext\rtree
   190    203   !ENDIF
   191    204   
   192    205   # Define -DNDEBUG to compile without debugging (i.e., for production usage)
   193    206   # Omitting the define will cause extra debugging code to be inserted and
   194    207   # includes extra comments when "EXPLAIN stmt" is used.
   195    208   #
   196    209   !IF $(DEBUG)==0
   197    210   TCC = $(TCC) -DNDEBUG
   198    211   BCC = $(BCC) -DNDEBUG
          212  +RCC = $(RCC) -DNDEBUG
   199    213   !ENDIF
   200    214   
   201    215   !IF $(DEBUG)>1
   202    216   TCC = $(TCC) -DSQLITE_DEBUG
          217  +RCC = $(RCC) -DSQLITE_DEBUG
   203    218   !ENDIF
   204    219   
   205    220   !IF $(DEBUG)>3
   206    221   TCC = $(TCC) -DSQLITE_DEBUG_OS_TRACE=1
          222  +RCC = $(RCC) -DSQLITE_DEBUG_OS_TRACE=1
   207    223   !ENDIF
   208    224   
   209    225   !IF $(DEBUG)>4
   210    226   TCC = $(TCC) -DSQLITE_ENABLE_IOTRACE
          227  +RCC = $(RCC) -DSQLITE_ENABLE_IOTRACE
   211    228   !ENDIF
   212    229   
   213    230   #
   214    231   # Prevent warnings about "insecure" MSVC runtime library functions
   215    232   # being used.
   216    233   #
   217    234   TCC = $(TCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
   218    235   BCC = $(BCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
          236  +RCC = $(RCC) -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS
   219    237   
   220    238   #
   221    239   # Prevent warnings about "deprecated" POSIX functions being used.
   222    240   #
   223    241   TCC = $(TCC) -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS
   224    242   BCC = $(BCC) -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS
          243  +RCC = $(RCC) -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS
   225    244   
   226    245   #
   227    246   # Use the SQLite debugging heap subsystem?
   228    247   #
   229    248   !IF $(MEMDEBUG)!=0
   230    249   TCC = $(TCC) -DSQLITE_MEMDEBUG=1
          250  +RCC = $(RCC) -DSQLITE_MEMDEBUG=1
   231    251   
   232    252   #
   233    253   # Use native Win32 heap subsystem instead of malloc/free?
   234    254   #
   235    255   !ELSEIF $(WIN32HEAP)!=0
   236    256   TCC = $(TCC) -DSQLITE_WIN32_MALLOC=1
          257  +RCC = $(RCC) -DSQLITE_WIN32_MALLOC=1
   237    258   
   238    259   #
   239    260   # Validate the heap on every call into the native Win32 heap subsystem?
   240    261   #
   241    262   !IF $(DEBUG)>2
   242    263   TCC = $(TCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1
          264  +RCC = $(RCC) -DSQLITE_WIN32_MALLOC_VALIDATE=1
   243    265   !ENDIF
   244    266   !ENDIF
   245    267   
   246    268   # The locations of the Tcl header and library files.  Also, the library that
   247    269   # non-stubs enabled programs using Tcl must link against.  These variables
   248    270   # (TCLINCDIR, TCLLIBDIR, and LIBTCL) may be overridden via the environment
   249    271   # prior to running nmake in order to match the actual installed location and
................................................................................
   294    316   # The library that programs using readline() must link against.
   295    317   #
   296    318   LIBREADLINE =
   297    319   
   298    320   # Should the database engine be compiled threadsafe
   299    321   #
   300    322   TCC = $(TCC) -DSQLITE_THREADSAFE=1
          323  +RCC = $(RCC) -DSQLITE_THREADSAFE=1
   301    324   
   302    325   # Do threads override each others locks by default (1), or do we test (-1)
   303    326   #
   304    327   TCC = $(TCC) -DSQLITE_THREAD_OVERRIDE_LOCK=-1
          328  +RCC = $(RCC) -DSQLITE_THREAD_OVERRIDE_LOCK=-1
   305    329   
   306    330   # Any target libraries which libsqlite must be linked against
   307    331   #
   308    332   !IFNDEF TLIBS
   309    333   TLIBS =
   310    334   !ENDIF
   311    335   
................................................................................
   312    336   # Flags controlling use of the in memory btree implementation
   313    337   #
   314    338   # SQLITE_TEMP_STORE is 0 to force temporary tables to be in a file, 1 to
   315    339   # default to file, 2 to default to memory, and 3 to force temporary
   316    340   # tables to always be in memory.
   317    341   #
   318    342   TCC = $(TCC) -DSQLITE_TEMP_STORE=1
          343  +RCC = $(RCC) -DSQLITE_TEMP_STORE=1
   319    344   
   320    345   # Enable/disable loadable extensions, and other optional features
   321    346   # based on configuration. (-DSQLITE_OMIT*, -DSQLITE_ENABLE*).
   322    347   # The same set of OMIT and ENABLE flags should be passed to the
   323    348   # LEMON parser generator and the mkkeywordhash tool as well.
   324    349   
   325    350   # BEGIN standard options
................................................................................
   329    354   # END standard options
   330    355   
   331    356   # BEGIN required Windows option
   332    357   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_MAX_TRIGGER_DEPTH=100
   333    358   # END required Windows option
   334    359   
   335    360   TCC = $(TCC) $(OPT_FEATURE_FLAGS)
          361  +RCC = $(RCC) $(OPT_FEATURE_FLAGS)
   336    362   
   337    363   # Add in any optional parameters specified on the make commane line
   338    364   # ie.  make "OPTS=-DSQLITE_ENABLE_FOO=1 -DSQLITE_OMIT_FOO=1".
   339    365   TCC = $(TCC) $(OPTS)
          366  +RCC = $(RCC) $(OPTS)
   340    367   
   341    368   # If symbols are enabled, enable PDBs.
   342    369   # If debugging is enabled, disable all optimizations and enable PDBs.
   343    370   !IF $(DEBUG)>0
   344    371   TCC = $(TCC) -Od -D_DEBUG
   345    372   BCC = $(BCC) -Od -D_DEBUG
          373  +RCC = $(RCC) -D_DEBUG
   346    374   !ELSE
   347    375   TCC = $(TCC) -O2
   348    376   BCC = $(BCC) -O2
   349    377   !ENDIF
   350    378   
   351    379   !IF $(DEBUG)>0 || $(SYMBOLS)!=0
   352    380   TCC = $(TCC) -Zi
   353    381   BCC = $(BCC) -Zi
   354    382   !ENDIF
   355    383   
   356    384   # If ICU support is enabled, add the compiler options for it.
   357    385   !IF $(USE_ICU)!=0
   358    386   TCC = $(TCC) -DSQLITE_ENABLE_ICU=1
          387  +RCC = $(RCC) -DSQLITE_ENABLE_ICU=1
   359    388   TCC = $(TCC) -I$(TOP)\ext\icu
          389  +RCC = $(RCC) -I$(TOP)\ext\icu
   360    390   TCC = $(TCC) -I$(ICUINCDIR)
          391  +RCC = $(RCC) -I$(ICUINCDIR)
   361    392   !ENDIF
   362    393   
   363         -# libtool compile/link
          394  +# Command line prefixes for compiling code, compiling resources,
          395  +# linking, etc.
   364    396   LTCOMPILE = $(TCC) -Fo$@
          397  +LTRCOMPILE = $(RCC) -r
   365    398   LTLIB = lib.exe
   366    399   LTLINK = $(TCC) -Fe$@
   367    400   
   368    401   # If a platform was set, force the linker to target that.
   369    402   # Note that the vcvars*.bat family of batch files typically
   370    403   # set this for you.  Otherwise, the linker will attempt
   371    404   # to deduce the binary type based on the object files.
................................................................................
   729    762   
   730    763   libsqlite3.lib:	$(LIBOBJ)
   731    764   	$(LTLIB) $(LTLIBOPTS) /OUT:$@ $(LIBOBJ) $(TLIBS)
   732    765   
   733    766   libtclsqlite3.lib:	tclsqlite.lo libsqlite3.lib
   734    767   	$(LTLIB) $(LTLIBOPTS) $(LTLIBPATHS) /OUT:$@ tclsqlite.lo libsqlite3.lib $(LIBTCL:tcl=tclstub) $(TLIBS)
   735    768   
   736         -sqlite3.exe:	$(TOP)\src\shell.c libsqlite3.lib sqlite3.h
          769  +sqlite3.exe:	$(TOP)\src\shell.c libsqlite3.lib sqlite3res.lo sqlite3.h
   737    770   	$(LTLINK) $(READLINE_FLAGS) \
   738    771   		$(TOP)\src\shell.c \
   739         -		/link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib $(LIBREADLINE) $(LTLIBS) $(TLIBS)
          772  +		/link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib sqlite3res.lo $(LIBREADLINE) $(LTLIBS) $(TLIBS)
   740    773   
   741    774   # This target creates a directory named "tsrc" and fills it with
   742    775   # copies of all of the C source code and header files needed to
   743    776   # build on the target system.  Some of the C source code and header
   744    777   # files are automatically generated.  This target takes care of
   745    778   # all that automatic generation.
   746    779   #
................................................................................
   780    813   #
   781    814   parse.lo:	parse.c $(HDR)
   782    815   	$(LTCOMPILE) -c parse.c
   783    816   
   784    817   opcodes.lo:	opcodes.c
   785    818   	$(LTCOMPILE) -c opcodes.c
   786    819   
          820  +# Rule to build the Win32 resources object file.
          821  +#
          822  +sqlite3res.lo:	$(TOP)\src\sqlite3.rc $(HDR)
          823  +	echo #ifndef SQLITE_RESOURCE_VERSION > sqlite3rc.h
          824  +	for /F %%V in ('type "$(TOP)\VERSION"') do ( \
          825  +		echo #define SQLITE_RESOURCE_VERSION %%V \
          826  +			| $(NAWK) "/.*/ { gsub(/[.]/,\",\");print }" >> sqlite3rc.h \
          827  +	)
          828  +	echo #endif >> sqlite3rc.h
          829  +	$(LTRCOMPILE) -fo sqlite3res.lo $(TOP)\src\sqlite3.rc
          830  +
   787    831   # Rules to build individual *.lo files from files in the src directory.
   788    832   #
   789    833   alter.lo:	$(TOP)\src\alter.c $(HDR)
   790    834   	$(LTCOMPILE) -c $(TOP)\src\alter.c
   791    835   
   792    836   analyze.lo:	$(TOP)\src\analyze.c $(HDR)
   793    837   	$(LTCOMPILE) -c $(TOP)\src\analyze.c
................................................................................
   995   1039   
   996   1040   tclsqlite.lo:	$(TOP)\src\tclsqlite.c $(HDR)
   997   1041   	$(LTCOMPILE) -DUSE_TCL_STUBS=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
   998   1042   
   999   1043   tclsqlite-shell.lo:	$(TOP)\src\tclsqlite.c $(HDR)
  1000   1044   	$(LTCOMPILE) -DTCLSH=1 -DBUILD_sqlite -I$(TCLINCDIR) -c $(TOP)\src\tclsqlite.c
  1001   1045   
  1002         -tclsqlite3.exe:	tclsqlite-shell.lo libsqlite3.lib
  1003         -	$(LTLINK) tclsqlite-shell.lo \
  1004         -		/link $(LTLINKOPTS) $(LTLIBPATHS) libsqlite3.lib $(LTLIBS) $(TLIBS)
         1046  +tclsqlite3.exe:	tclsqlite-shell.lo libsqlite3.lib sqlite3res.lo
         1047  +	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /OUT:$@ libsqlite3.lib tclsqlite-shell.lo sqlite3res.lo $(LTLIBS) $(TLIBS)
  1005   1048   
  1006   1049   # Rules to build opcodes.c and opcodes.h
  1007   1050   #
  1008   1051   opcodes.c:	opcodes.h $(TOP)\mkopcodec.awk
  1009   1052   	$(NAWK) -f $(TOP)\mkopcodec.awk opcodes.h > opcodes.c
  1010   1053   
  1011   1054   opcodes.h:	parse.h $(TOP)\src\vdbe.c $(TOP)\mkopcodeh.awk
................................................................................
  1110   1153   TESTFIXTURE_SRC1 = sqlite3.c
  1111   1154   !IF $(USE_AMALGAMATION)==0
  1112   1155   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC0)
  1113   1156   !ELSE
  1114   1157   TESTFIXTURE_SRC = $(TESTSRC) $(TOP)\src\tclsqlite.c $(TESTFIXTURE_SRC1)
  1115   1158   !ENDIF
  1116   1159   
  1117         -testfixture.exe:	$(TESTFIXTURE_SRC) $(HDR)
         1160  +testfixture.exe:	$(TESTFIXTURE_SRC) sqlite3res.lo $(HDR)
  1118   1161   	$(LTLINK) -DSQLITE_NO_SYNC=1 $(TESTFIXTURE_FLAGS) \
  1119   1162   		-DBUILD_sqlite -I$(TCLINCDIR) \
  1120   1163   		$(TESTFIXTURE_SRC) \
  1121         -		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LTLIBS) $(TLIBS)
         1164  +		/link $(LTLINKOPTS) $(LTLIBPATHS) sqlite3res.lo $(LTLIBS) $(TLIBS)
  1122   1165   
  1123   1166   fulltest:	testfixture.exe sqlite3.exe
  1124   1167   	.\testfixture.exe $(TOP)\test\all.test
  1125   1168   
  1126   1169   soaktest:	testfixture.exe sqlite3.exe
  1127   1170   	.\testfixture.exe $(TOP)\test\all.test -soak=1
         1171  +
         1172  +fulltestonly:	testfixture.exe sqlite3.exe
         1173  +	.\testfixture.exe $(TOP)\test\full.test
  1128   1174   
  1129   1175   test:	testfixture.exe sqlite3.exe
  1130   1176   	.\testfixture.exe $(TOP)\test\veryquick.test
  1131   1177   
  1132   1178   sqlite3_analyzer.c: sqlite3.c $(TOP)\src\test_stat.c $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
  1133   1179   	copy sqlite3.c + $(TOP)\src\test_stat.c + $(TOP)\src\tclsqlite.c $@
  1134   1180   	echo static const char *tclsh_main_loop(void){ >> $@
  1135   1181   	echo static const char *zMainloop = >> $@
  1136   1182   	$(NAWK) -f $(TOP)\tool\tostr.awk $(TOP)\tool\spaceanal.tcl >> $@
  1137   1183   	echo ; return zMainloop; } >> $@
  1138   1184   
  1139         -sqlite3_analyzer.exe:	sqlite3_analyzer.c
         1185  +sqlite3_analyzer.exe:	sqlite3_analyzer.c sqlite3res.lo
  1140   1186   	$(LTLINK) -DBUILD_sqlite -DTCLSH=2 -I$(TCLINCDIR) sqlite3_analyzer.c \
  1141         -		/link $(LTLINKOPTS) $(LTLIBPATHS) $(LTLIBS) $(TLIBS)
         1187  +		/link $(LTLINKOPTS) $(LTLIBPATHS) sqlite3res.lo $(LTLIBS) $(TLIBS)
  1142   1188   
  1143   1189   clean:
  1144   1190   	del /Q *.lo *.ilk *.lib *.obj *.pdb sqlite3.exe libsqlite3.lib
  1145   1191   	del /Q *.da *.bb *.bbg gmon.out
  1146   1192   	del /Q sqlite3.h opcodes.c opcodes.h
  1147   1193   	del /Q lemon.exe lempar.c parse.*
  1148   1194   	del /Q mkkeywordhash.exe keywordhash.h
................................................................................
  1149   1195   	-rmdir /Q/S .deps
  1150   1196   	-rmdir /Q/S .libs
  1151   1197   	-rmdir /Q/S quota2a
  1152   1198   	-rmdir /Q/S quota2b
  1153   1199   	-rmdir /Q/S quota2c
  1154   1200   	-rmdir /Q/S tsrc
  1155   1201   	del /Q .target_source
  1156         -	del /Q tclsqlite3.exe
         1202  +	del /Q tclsqlite3.exe tclsqlite3.exp
  1157   1203   	del /Q testfixture.exe testfixture.exp test.db
  1158   1204   	del /Q sqlite3.dll sqlite3.lib sqlite3.exp sqlite3.def
  1159   1205   	del /Q sqlite3.c
         1206  +	del /Q sqlite3rc.h
  1160   1207   	del /Q sqlite3_analyzer.exe sqlite3_analyzer.exp sqlite3_analyzer.c
  1161   1208   	del /Q sqlite-output.vsix
  1162   1209   
  1163         -#
  1164         -# Windows section
         1210  +# Dynamic link library section.
  1165   1211   #
  1166   1212   dll: sqlite3.dll
  1167   1213   
  1168   1214   sqlite3.def: libsqlite3.lib
  1169   1215   	echo EXPORTS > sqlite3.def
  1170   1216   	dumpbin /all libsqlite3.lib \
  1171   1217   		| $(NAWK) "/ 1 _?sqlite3_/ { sub(/^.* _?/,\"\");print }" \
  1172   1218   		| sort >> sqlite3.def
  1173   1219   
  1174         -sqlite3.dll: $(LIBOBJ) sqlite3.def
  1175         -	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ) $(LTLIBS) $(TLIBS)
         1220  +sqlite3.dll: $(LIBOBJ) sqlite3res.lo sqlite3.def
         1221  +	$(LD) $(LDFLAGS) $(LTLINKOPTS) $(LTLIBPATHS) /DLL /DEF:sqlite3.def /OUT:$@ $(LIBOBJ) sqlite3res.lo $(LTLIBS) $(TLIBS)

Changes to Makefile.vxworks.

   620    620   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   621    621   
   622    622   fulltest:	testfixture$(EXE) sqlite3$(EXE)
   623    623   	./testfixture$(EXE) $(TOP)/test/all.test
   624    624   
   625    625   soaktest:	testfixture$(EXE) sqlite3$(EXE)
   626    626   	./testfixture$(EXE) $(TOP)/test/all.test -soak=1
          627  +
          628  +fulltestonly:	testfixture$(EXE) sqlite3$(EXE)
          629  +	./testfixture$(EXE) $(TOP)/test/full.test
   627    630   
   628    631   test:	testfixture$(EXE) sqlite3$(EXE)
   629    632   	./testfixture$(EXE) $(TOP)/test/veryquick.test
   630    633   
   631    634   sqlite3_analyzer$(EXE):	$(TOP)/src/tclsqlite.c sqlite3.c $(TESTSRC) \
   632    635   			$(TOP)/tool/spaceanal.tcl
   633    636   	sed \

Changes to VERSION.

     1         -3.7.14
            1  +3.7.15

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.62 for sqlite 3.7.14.
            3  +# Generated by GNU Autoconf 2.62 for sqlite 3.7.15.
     4      4   #
     5      5   # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
     6      6   # 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
     7      7   # This configure script is free software; the Free Software Foundation
     8      8   # gives unlimited permission to copy, distribute and modify it.
     9      9   ## --------------------- ##
    10     10   ## M4sh Initialization.  ##
................................................................................
   739    739   MFLAGS=
   740    740   MAKEFLAGS=
   741    741   SHELL=${CONFIG_SHELL-/bin/sh}
   742    742   
   743    743   # Identity of this package.
   744    744   PACKAGE_NAME='sqlite'
   745    745   PACKAGE_TARNAME='sqlite'
   746         -PACKAGE_VERSION='3.7.14'
   747         -PACKAGE_STRING='sqlite 3.7.14'
          746  +PACKAGE_VERSION='3.7.15'
          747  +PACKAGE_STRING='sqlite 3.7.15'
   748    748   PACKAGE_BUGREPORT=''
   749    749   
   750    750   # Factoring default headers for most tests.
   751    751   ac_includes_default="\
   752    752   #include <stdio.h>
   753    753   #ifdef HAVE_SYS_TYPES_H
   754    754   # include <sys/types.h>
................................................................................
  1481   1481   #
  1482   1482   # Report the --help message.
  1483   1483   #
  1484   1484   if test "$ac_init_help" = "long"; then
  1485   1485     # Omit some internal or obsolete options to make the list less imposing.
  1486   1486     # This message is too long to be a string in the A/UX 3.1 sh.
  1487   1487     cat <<_ACEOF
  1488         -\`configure' configures sqlite 3.7.14 to adapt to many kinds of systems.
         1488  +\`configure' configures sqlite 3.7.15 to adapt to many kinds of systems.
  1489   1489   
  1490   1490   Usage: $0 [OPTION]... [VAR=VALUE]...
  1491   1491   
  1492   1492   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1493   1493   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1494   1494   
  1495   1495   Defaults for the options are specified in brackets.
................................................................................
  1546   1546     --build=BUILD     configure for building on BUILD [guessed]
  1547   1547     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1548   1548   _ACEOF
  1549   1549   fi
  1550   1550   
  1551   1551   if test -n "$ac_init_help"; then
  1552   1552     case $ac_init_help in
  1553         -     short | recursive ) echo "Configuration of sqlite 3.7.14:";;
         1553  +     short | recursive ) echo "Configuration of sqlite 3.7.15:";;
  1554   1554      esac
  1555   1555     cat <<\_ACEOF
  1556   1556   
  1557   1557   Optional Features:
  1558   1558     --disable-option-checking  ignore unrecognized --enable/--with options
  1559   1559     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1560   1560     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1662   1662       cd "$ac_pwd" || { ac_status=$?; break; }
  1663   1663     done
  1664   1664   fi
  1665   1665   
  1666   1666   test -n "$ac_init_help" && exit $ac_status
  1667   1667   if $ac_init_version; then
  1668   1668     cat <<\_ACEOF
  1669         -sqlite configure 3.7.14
         1669  +sqlite configure 3.7.15
  1670   1670   generated by GNU Autoconf 2.62
  1671   1671   
  1672   1672   Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
  1673   1673   2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  1674   1674   This configure script is free software; the Free Software Foundation
  1675   1675   gives unlimited permission to copy, distribute and modify it.
  1676   1676   _ACEOF
  1677   1677     exit
  1678   1678   fi
  1679   1679   cat >config.log <<_ACEOF
  1680   1680   This file contains any messages produced by compilers while
  1681   1681   running configure, to aid debugging if configure makes a mistake.
  1682   1682   
  1683         -It was created by sqlite $as_me 3.7.14, which was
         1683  +It was created by sqlite $as_me 3.7.15, which was
  1684   1684   generated by GNU Autoconf 2.62.  Invocation command line was
  1685   1685   
  1686   1686     $ $0 $@
  1687   1687   
  1688   1688   _ACEOF
  1689   1689   exec 5>>config.log
  1690   1690   {
................................................................................
 14030  14030   
 14031  14031   exec 6>&1
 14032  14032   
 14033  14033   # Save the log message, to keep $[0] and so on meaningful, and to
 14034  14034   # report actual input values of CONFIG_FILES etc. instead of their
 14035  14035   # values after options handling.
 14036  14036   ac_log="
 14037         -This file was extended by sqlite $as_me 3.7.14, which was
        14037  +This file was extended by sqlite $as_me 3.7.15, which was
 14038  14038   generated by GNU Autoconf 2.62.  Invocation command line was
 14039  14039   
 14040  14040     CONFIG_FILES    = $CONFIG_FILES
 14041  14041     CONFIG_HEADERS  = $CONFIG_HEADERS
 14042  14042     CONFIG_LINKS    = $CONFIG_LINKS
 14043  14043     CONFIG_COMMANDS = $CONFIG_COMMANDS
 14044  14044     $ $0 $@
................................................................................
 14083  14083   $config_commands
 14084  14084   
 14085  14085   Report bugs to <bug-autoconf@gnu.org>."
 14086  14086   
 14087  14087   _ACEOF
 14088  14088   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 14089  14089   ac_cs_version="\\
 14090         -sqlite config.status 3.7.14
        14090  +sqlite config.status 3.7.15
 14091  14091   configured by $0, generated by GNU Autoconf 2.62,
 14092  14092     with options \\"`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`\\"
 14093  14093   
 14094  14094   Copyright (C) 2008 Free Software Foundation, Inc.
 14095  14095   This config.status script is free software; the Free Software Foundation
 14096  14096   gives unlimited permission to copy, distribute and modify it."
 14097  14097   

Changes to main.mk.

   547    547   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   548    548   
   549    549   fulltest:	testfixture$(EXE) sqlite3$(EXE)
   550    550   	./testfixture$(EXE) $(TOP)/test/all.test
   551    551   
   552    552   soaktest:	testfixture$(EXE) sqlite3$(EXE)
   553    553   	./testfixture$(EXE) $(TOP)/test/all.test -soak=1
          554  +
          555  +fulltestonly:	testfixture$(EXE) sqlite3$(EXE)
          556  +	./testfixture$(EXE) $(TOP)/test/full.test
   554    557   
   555    558   test:	testfixture$(EXE) sqlite3$(EXE)
   556    559   	./testfixture$(EXE) $(TOP)/test/veryquick.test
   557    560   
   558    561   # The next two rules are used to support the "threadtest" target. Building
   559    562   # threadtest runs a few thread-safety tests that are implemented in C. This
   560    563   # target is invoked by the releasetest.tcl script.

Deleted publish.sh.

     1         -#!/bin/sh
     2         -#
     3         -# This script is used to compile SQLite and package everything up
     4         -# so that it is ready to move to the SQLite website.
     5         -#
     6         -
     7         -# Set srcdir to the name of the directory that contains the publish.sh
     8         -# script.
     9         -#
    10         -srcdir=`echo "$0" | sed 's%\(^.*\)/[^/][^/]*$%\1%'`
    11         -
    12         -# Get the makefile.
    13         -#
    14         -cp $srcdir/Makefile.linux-gcc ./Makefile
    15         -chmod +x $srcdir/install-sh
    16         -
    17         -# Get the current version number - needed to help build filenames
    18         -#
    19         -VERS=`cat $srcdir/VERSION`
    20         -VERSW=`sed 's/\./_/g' $srcdir/VERSION`
    21         -echo "VERSIONS: $VERS $VERSW"
    22         -
    23         -# Start by building an sqlite shell for linux.
    24         -#
    25         -make clean
    26         -make sqlite3.c
    27         -CFLAGS="-Os -DSQLITE_ENABLE_FTS3=0 -DSQLITE_ENABLE_RTREE=0"
    28         -CFLAGS="$CFLAGS -DSQLITE_THREADSAFE=0"
    29         -echo '***** '"COMPILING sqlite3-$VERS.bin..."
    30         -gcc $CFLAGS -Itsrc sqlite3.c tsrc/shell.c -o sqlite3 -ldl
    31         -strip sqlite3
    32         -mv sqlite3 sqlite3-$VERS.bin
    33         -gzip sqlite3-$VERS.bin
    34         -chmod 644 sqlite3-$VERS.bin.gz
    35         -mv sqlite3-$VERS.bin.gz doc
    36         -
    37         -# Build the sqlite.so and tclsqlite.so shared libraries
    38         -# under Linux
    39         -#
    40         -TCLDIR=/home/drh/tcltk/846/linux/846linux
    41         -TCLSTUBLIB=$TCLDIR/libtclstub8.4g.a
    42         -CFLAGS="-Os -DSQLITE_ENABLE_FTS3=3 -DSQLITE_ENABLE_RTREE=1"
    43         -CFLAGS="$CFLAGS -DHAVE_LOCALTIME_R=1 -DHAVE_GMTIME_R=1"
    44         -CFLAGS="$CFLAGS -DSQLITE_ENABLE_COLUMN_METADATA=1"
    45         -echo '***** BUILDING shared libraries for linux'
    46         -gcc $CFLAGS -shared tclsqlite3.c $TCLSTUBLIB -o tclsqlite3.so -lpthread
    47         -strip tclsqlite3.so
    48         -chmod 644 tclsqlite3.so
    49         -mv tclsqlite3.so tclsqlite-$VERS.so
    50         -gzip tclsqlite-$VERS.so
    51         -mv tclsqlite-$VERS.so.gz doc
    52         -gcc $CFLAGS -shared sqlite3.c -o sqlite3.so -lpthread
    53         -strip sqlite3.so
    54         -chmod 644 sqlite3.so
    55         -mv sqlite3.so sqlite-$VERS.so
    56         -gzip sqlite-$VERS.so
    57         -mv sqlite-$VERS.so.gz doc
    58         -
    59         -
    60         -# Build the tclsqlite3.dll and sqlite3.dll shared libraries.
    61         -#
    62         -. $srcdir/mkdll.sh
    63         -echo '***** PACKAGING shared libraries for windows'
    64         -echo zip doc/tclsqlite-$VERSW.zip tclsqlite3.dll
    65         -zip doc/tclsqlite-$VERSW.zip tclsqlite3.dll
    66         -echo zip doc/sqlitedll-$VERSW.zip sqlite3.dll sqlite3.def
    67         -zip doc/sqlitedll-$VERSW.zip sqlite3.dll sqlite3.def
    68         -
    69         -# Build the sqlite.exe executable for windows.
    70         -#
    71         -OPTS='-DSTATIC_BUILD=1 -DNDEBUG=1 -DSQLITE_THREADSAFE=0'
    72         -OPTS="$OPTS -DSQLITE_ENABLE_FTS3=1 -DSQLITE_ENABLE_RTREE=1"
    73         -i386-mingw32msvc-gcc -Os $OPTS -Itsrc -I$TCLDIR sqlite3.c tsrc/shell.c \
    74         -      -o sqlite3.exe
    75         -zip doc/sqlite-$VERSW.zip sqlite3.exe
    76         -
    77         -# Build a source archive useful for windows.
    78         -#
    79         -make target_source
    80         -cd tsrc
    81         -echo '***** BUILDING preprocessed source archives'
    82         -rm fts[12]* icu*
    83         -rm -f ../doc/sqlite-source-$VERSW.zip
    84         -zip ../doc/sqlite-source-$VERSW.zip *
    85         -cd ..
    86         -cp tsrc/sqlite3.h tsrc/sqlite3ext.h .
    87         -cp tsrc/shell.c .
    88         -pwd
    89         -zip doc/sqlite-amalgamation-$VERSW.zip sqlite3.c sqlite3.h sqlite3ext.h shell.c sqlite3.def
    90         -
    91         -# Construct a tarball of the source tree
    92         -#
    93         -echo '***** BUILDING source archive'
    94         -ORIGIN=`pwd`
    95         -cd $srcdir
    96         -chmod +x configure
    97         -cd ..
    98         -mv sqlite sqlite-$VERS
    99         -EXCLUDE=`find sqlite-$VERS -print | egrep '(www/|art/|doc/|contrib/|_FOSSIL_)' | sed 's,^, --exclude ,'`
   100         -echo "tar czf $ORIGIN/doc/sqlite-$VERS.tar.gz $EXCLUDE sqlite-$VERS"
   101         -tar czf $ORIGIN/doc/sqlite-$VERS.tar.gz $EXCLUDE sqlite-$VERS
   102         -mv sqlite-$VERS sqlite
   103         -cd $ORIGIN
   104         -
   105         -#
   106         -# Build RPMS (binary) and Source RPM
   107         -#
   108         -
   109         -# Make sure we are properly setup to build RPMs
   110         -#
   111         -echo "%HOME %{expand:%%(cd; pwd)}" > $HOME/.rpmmacros
   112         -echo "%_topdir %{HOME}/rpm" >> $HOME/.rpmmacros
   113         -mkdir $HOME/rpm
   114         -mkdir $HOME/rpm/BUILD
   115         -mkdir $HOME/rpm/SOURCES
   116         -mkdir $HOME/rpm/RPMS
   117         -mkdir $HOME/rpm/SRPMS
   118         -mkdir $HOME/rpm/SPECS
   119         -
   120         -# create the spec file from the template
   121         -sed s/SQLITE_VERSION/$VERS/g $srcdir/spec.template > $HOME/rpm/SPECS/sqlite.spec
   122         -
   123         -# copy the source tarball to the rpm directory
   124         -cp doc/sqlite-$VERS.tar.gz $HOME/rpm/SOURCES/.
   125         -
   126         -# build all the rpms
   127         -rpm -ba $HOME/rpm/SPECS/sqlite.spec >& rpm-$vers.log
   128         -
   129         -# copy the RPMs into the build directory.
   130         -mv $HOME/rpm/RPMS/i386/sqlite*-$vers*.rpm doc
   131         -mv $HOME/rpm/SRPMS/sqlite-$vers*.rpm doc
   132         -
   133         -# Build the website
   134         -#
   135         -#cp $srcdir/../historical/* doc
   136         -#make doc
   137         -#cd doc
   138         -#chmod 644 *.gz

Deleted publish_osx.sh.

     1         -#!/bin/sh
     2         -#
     3         -# This script is used to compile SQLite and package everything up
     4         -# so that it is ready to move to the SQLite website.
     5         -#
     6         -
     7         -# Set srcdir to the name of the directory that contains the publish.sh
     8         -# script.
     9         -#
    10         -srcdir=`echo "$0" | sed 's%\(^.*\)/[^/][^/]*$%\1%'`
    11         -
    12         -# Get the makefile.
    13         -#
    14         -cp $srcdir/Makefile.linux-gcc ./Makefile
    15         -chmod +x $srcdir/install-sh
    16         -
    17         -# Get the current version number - needed to help build filenames
    18         -#
    19         -VERS=`cat $srcdir/VERSION`
    20         -VERSW=`sed 's/\./_/g' $srcdir/VERSION`
    21         -echo "VERSIONS: $VERS $VERSW"
    22         -
    23         -# Start by building an sqlite shell for linux.
    24         -#
    25         -make clean
    26         -make sqlite3.c
    27         -CFLAGS="-Os -DSQLITE_ENABLE_FTS3=1 -DSQLITE_THREADSAFE=0"
    28         -NAME=sqlite3-$VERS-osx-x86.bin
    29         -echo '***** '"COMPILING $NAME..."
    30         -gcc $CFLAGS -Itsrc sqlite3.c tsrc/shell.c -o $NAME -ldl
    31         -strip $NAME
    32         -chmod 644 $NAME
    33         -gzip $NAME
    34         -mkdir -p doc
    35         -mv $NAME.gz doc

Changes to src/alter.c.

   660    660       pDflt = 0;
   661    661     }
   662    662   
   663    663     /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
   664    664     ** If there is a NOT NULL constraint, then the default value for the
   665    665     ** column must not be NULL.
   666    666     */
   667         -  if( pCol->isPrimKey ){
          667  +  if( pCol->colFlags & COLFLAG_PRIMKEY ){
   668    668       sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
   669    669       return;
   670    670     }
   671    671     if( pNew->pIndex ){
   672    672       sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
   673    673       return;
   674    674     }

Changes to src/build.c.

  1165   1165       sqlite3ErrorMsg(pParse, 
  1166   1166         "table \"%s\" has more than one primary key", pTab->zName);
  1167   1167       goto primary_key_exit;
  1168   1168     }
  1169   1169     pTab->tabFlags |= TF_HasPrimaryKey;
  1170   1170     if( pList==0 ){
  1171   1171       iCol = pTab->nCol - 1;
  1172         -    pTab->aCol[iCol].isPrimKey = 1;
         1172  +    pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
  1173   1173     }else{
  1174   1174       for(i=0; i<pList->nExpr; i++){
  1175   1175         for(iCol=0; iCol<pTab->nCol; iCol++){
  1176   1176           if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
  1177   1177             break;
  1178   1178           }
  1179   1179         }
  1180   1180         if( iCol<pTab->nCol ){
  1181         -        pTab->aCol[iCol].isPrimKey = 1;
         1181  +        pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
  1182   1182         }
  1183   1183       }
  1184   1184       if( pList->nExpr>1 ) iCol = -1;
  1185   1185     }
  1186   1186     if( iCol>=0 && iCol<pTab->nCol ){
  1187   1187       zType = pTab->aCol[iCol].zType;
  1188   1188     }

Changes to src/ctime.c.

   333    333     "OMIT_XFER_OPT",
   334    334   #endif
   335    335   #ifdef SQLITE_PERFORMANCE_TRACE
   336    336     "PERFORMANCE_TRACE",
   337    337   #endif
   338    338   #ifdef SQLITE_PROXY_DEBUG
   339    339     "PROXY_DEBUG",
          340  +#endif
          341  +#ifdef SQLITE_RTREE_INT_ONLY
          342  +  "RTREE_INT_ONLY",
   340    343   #endif
   341    344   #ifdef SQLITE_SECURE_DELETE
   342    345     "SECURE_DELETE",
   343    346   #endif
   344    347   #ifdef SQLITE_SMALL_STACK
   345    348     "SMALL_STACK",
   346    349   #endif

Changes to src/delete.c.

   634    634       }else{
   635    635         sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j);
   636    636         sqlite3ColumnDefault(v, pTab, idx, -1);
   637    637       }
   638    638     }
   639    639     if( doMakeRec ){
   640    640       const char *zAff;
   641         -    if( pTab->pSelect || (pParse->db->flags & SQLITE_IdxRealAsInt)!=0 ){
          641  +    if( pTab->pSelect
          642  +     || OptimizationDisabled(pParse->db, SQLITE_IdxRealAsInt)
          643  +    ){
   642    644         zAff = 0;
   643    645       }else{
   644    646         zAff = sqlite3IndexAffinityStr(v, pIdx);
   645    647       }
   646    648       sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut);
   647    649       sqlite3VdbeChangeP4(v, -1, zAff, P4_TRANSIENT);
   648    650     }
   649    651     sqlite3ReleaseTempRange(pParse, regBase, nCol+1);
   650    652     return regBase;
   651    653   }

Changes to src/expr.c.

  1416   1416   int sqlite3CodeOnce(Parse *pParse){
  1417   1417     Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
  1418   1418     return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
  1419   1419   }
  1420   1420   
  1421   1421   /*
  1422   1422   ** This function is used by the implementation of the IN (...) operator.
  1423         -** It's job is to find or create a b-tree structure that may be used
  1424         -** either to test for membership of the (...) set or to iterate through
  1425         -** its members, skipping duplicates.
         1423  +** The pX parameter is the expression on the RHS of the IN operator, which
         1424  +** might be either a list of expressions or a subquery.
  1426   1425   **
  1427         -** The index of the cursor opened on the b-tree (database table, database index 
  1428         -** or ephermal table) is stored in pX->iTable before this function returns.
         1426  +** The job of this routine is to find or create a b-tree object that can
         1427  +** be used either to test for membership in the RHS set or to iterate through
         1428  +** all members of the RHS set, skipping duplicates.
         1429  +**
         1430  +** A cursor is opened on the b-tree object that the RHS of the IN operator
         1431  +** and pX->iTable is set to the index of that cursor.
         1432  +**
  1429   1433   ** The returned value of this function indicates the b-tree type, as follows:
  1430   1434   **
  1431   1435   **   IN_INDEX_ROWID - The cursor was opened on a database table.
  1432   1436   **   IN_INDEX_INDEX - The cursor was opened on a database index.
  1433   1437   **   IN_INDEX_EPH -   The cursor was opened on a specially created and
  1434   1438   **                    populated epheremal table.
  1435   1439   **
  1436         -** An existing b-tree may only be used if the SELECT is of the simple
  1437         -** form:
         1440  +** An existing b-tree might be used if the RHS expression pX is a simple
         1441  +** subquery such as:
  1438   1442   **
  1439   1443   **     SELECT <column> FROM <table>
         1444  +**
         1445  +** If the RHS of the IN operator is a list or a more complex subquery, then
         1446  +** an ephemeral table might need to be generated from the RHS and then
         1447  +** pX->iTable made to point to the ephermeral table instead of an
         1448  +** existing table.  
  1440   1449   **
  1441   1450   ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
  1442   1451   ** through the set members, skipping any duplicates. In this case an
  1443   1452   ** epheremal table must be used unless the selected <column> is guaranteed
  1444   1453   ** to be unique - either because it is an INTEGER PRIMARY KEY or it
  1445   1454   ** has a UNIQUE constraint or UNIQUE index.
  1446   1455   **
................................................................................
  1529   1538         ** to this collation sequence.  */
  1530   1539         CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
  1531   1540   
  1532   1541         /* Check that the affinity that will be used to perform the 
  1533   1542         ** comparison is the same as the affinity of the column. If
  1534   1543         ** it is not, it is not possible to use any index.
  1535   1544         */
  1536         -      char aff = comparisonAffinity(pX);
  1537         -      int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
         1545  +      int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
  1538   1546   
  1539   1547         for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
  1540   1548           if( (pIdx->aiColumn[0]==iCol)
  1541   1549            && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
  1542   1550            && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
  1543   1551           ){
  1544   1552             int iAddr;
................................................................................
  1658   1666     }
  1659   1667   #endif
  1660   1668   
  1661   1669     switch( pExpr->op ){
  1662   1670       case TK_IN: {
  1663   1671         char affinity;              /* Affinity of the LHS of the IN */
  1664   1672         KeyInfo keyInfo;            /* Keyinfo for the generated table */
         1673  +      static u8 sortOrder = 0;    /* Fake aSortOrder for keyInfo */
  1665   1674         int addr;                   /* Address of OP_OpenEphemeral instruction */
  1666   1675         Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
  1667   1676   
  1668   1677         if( rMayHaveNull ){
  1669   1678           sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
  1670   1679         }
  1671   1680   
................................................................................
  1685   1694         ** is used.
  1686   1695         */
  1687   1696         pExpr->iTable = pParse->nTab++;
  1688   1697         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
  1689   1698         if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
  1690   1699         memset(&keyInfo, 0, sizeof(keyInfo));
  1691   1700         keyInfo.nField = 1;
         1701  +      keyInfo.aSortOrder = &sortOrder;
  1692   1702   
  1693   1703         if( ExprHasProperty(pExpr, EP_xIsSelect) ){
  1694   1704           /* Case 1:     expr IN (SELECT ...)
  1695   1705           **
  1696   1706           ** Generate code to write the results of the select into the temporary
  1697   1707           ** table allocated and opened above.
  1698   1708           */
................................................................................
  1725   1735           struct ExprList_item *pItem;
  1726   1736           int r1, r2, r3;
  1727   1737   
  1728   1738           if( !affinity ){
  1729   1739             affinity = SQLITE_AFF_NONE;
  1730   1740           }
  1731   1741           keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
         1742  +        keyInfo.aSortOrder = &sortOrder;
  1732   1743   
  1733   1744           /* Loop through each expression in <exprlist>. */
  1734   1745           r1 = sqlite3GetTempReg(pParse);
  1735   1746           r2 = sqlite3GetTempReg(pParse);
  1736   1747           sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
  1737   1748           for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
  1738   1749             Expr *pE2 = pItem->pExpr;
................................................................................
  2051   2062     assert( iReg>0 );  /* Register numbers are always positive */
  2052   2063     assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
  2053   2064   
  2054   2065     /* The SQLITE_ColumnCache flag disables the column cache.  This is used
  2055   2066     ** for testing only - to verify that SQLite always gets the same answer
  2056   2067     ** with and without the column cache.
  2057   2068     */
  2058         -  if( pParse->db->flags & SQLITE_ColumnCache ) return;
         2069  +  if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
  2059   2070   
  2060   2071     /* First replace any existing entry.
  2061   2072     **
  2062   2073     ** Actually, the way the column cache is currently used, we are guaranteed
  2063   2074     ** that the object will never already be in cache.  Verify this guarantee.
  2064   2075     */
  2065   2076   #ifndef NDEBUG
................................................................................
  2248   2259   /*
  2249   2260   ** Generate code to move content from registers iFrom...iFrom+nReg-1
  2250   2261   ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
  2251   2262   */
  2252   2263   void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
  2253   2264     int i;
  2254   2265     struct yColCache *p;
  2255         -  if( NEVER(iFrom==iTo) ) return;
  2256         -  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg);
         2266  +  assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
         2267  +  sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
  2257   2268     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
  2258   2269       int x = p->iReg;
  2259   2270       if( x>=iFrom && x<iFrom+nReg ){
  2260   2271         p->iReg += iTo-iFrom;
  2261   2272       }
  2262   2273     }
  2263   2274   }
  2264   2275   
  2265         -/*
  2266         -** Generate code to copy content from registers iFrom...iFrom+nReg-1
  2267         -** over to iTo..iTo+nReg-1.
  2268         -*/
  2269         -void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){
  2270         -  int i;
  2271         -  if( NEVER(iFrom==iTo) ) return;
  2272         -  for(i=0; i<nReg; i++){
  2273         -    sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, iFrom+i, iTo+i);
  2274         -  }
  2275         -}
  2276         -
  2277   2276   #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
  2278   2277   /*
  2279   2278   ** Return true if any register in the range iFrom..iTo (inclusive)
  2280   2279   ** is used as part of the column cache.
  2281   2280   **
  2282   2281   ** This routine is used within assert() and testcase() macros only
  2283   2282   ** and does not appear in a normal build.
................................................................................
  3379   3378   ** interface.  This allows test logic to verify that the same answer is
  3380   3379   ** obtained for queries regardless of whether or not constants are
  3381   3380   ** precomputed into registers or if they are inserted in-line.
  3382   3381   */
  3383   3382   void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
  3384   3383     Walker w;
  3385   3384     if( pParse->cookieGoto ) return;
  3386         -  if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return;
         3385  +  if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
  3387   3386     w.xExprCallback = evalConstExpr;
  3388   3387     w.xSelectCallback = 0;
  3389   3388     w.pParse = pParse;
  3390   3389     sqlite3WalkExpr(&w, pExpr);
  3391   3390   }
  3392   3391   
  3393   3392   

Changes to src/fkey.c.

   921    921         /* Check if any parent key columns are being modified. */
   922    922         for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
   923    923           for(i=0; i<p->nCol; i++){
   924    924             char *zKey = p->aCol[i].zCol;
   925    925             int iKey;
   926    926             for(iKey=0; iKey<pTab->nCol; iKey++){
   927    927               Column *pCol = &pTab->aCol[iKey];
   928         -            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){
          928  +            if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey)
          929  +                      : (pCol->colFlags & COLFLAG_PRIMKEY)!=0) ){
   929    930                 if( aChange[iKey]>=0 ) return 1;
   930    931                 if( iKey==pTab->iPKey && chngRowid ) return 1;
   931    932               }
   932    933             }
   933    934           }
   934    935         }
   935    936       }

Changes to src/func.c.

   366    366           z1[i] = sqlite3Tolower(z2[i]);
   367    367         }
   368    368         sqlite3_result_text(context, z1, n, sqlite3_free);
   369    369       }
   370    370     }
   371    371   }
   372    372   
   373         -
   374         -#if 0  /* This function is never used. */
   375    373   /*
   376         -** The COALESCE() and IFNULL() functions used to be implemented as shown
   377         -** here.  But now they are implemented as VDBE code so that unused arguments
   378         -** do not have to be computed.  This legacy implementation is retained as
   379         -** comment.
          374  +** The COALESCE() and IFNULL() functions are implemented as VDBE code so
          375  +** that unused argument values do not have to be computed.  However, we
          376  +** still need some kind of function implementation for this routines in
          377  +** the function table.  That function implementation will never be called
          378  +** so it doesn't matter what the implementation is.  We might as well use
          379  +** the "version()" function as a substitute.
   380    380   */
   381         -/*
   382         -** Implementation of the IFNULL(), NVL(), and COALESCE() functions.  
   383         -** All three do the same thing.  They return the first non-NULL
   384         -** argument.
   385         -*/
   386         -static void ifnullFunc(
   387         -  sqlite3_context *context,
   388         -  int argc,
   389         -  sqlite3_value **argv
   390         -){
   391         -  int i;
   392         -  for(i=0; i<argc; i++){
   393         -    if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
   394         -      sqlite3_result_value(context, argv[i]);
   395         -      break;
   396         -    }
   397         -  }
   398         -}
   399         -#endif /* NOT USED */
   400    381   #define ifnullFunc versionFunc   /* Substitute function - never called */
   401    382   
   402    383   /*
   403    384   ** Implementation of random().  Return a random integer.  
   404    385   */
   405    386   static void randomFunc(
   406    387     sqlite3_context *context,
................................................................................
   511    492   /*
   512    493   ** For LIKE and GLOB matching on EBCDIC machines, assume that every
   513    494   ** character is exactly one byte in size.  Also, all characters are
   514    495   ** able to participate in upper-case-to-lower-case mappings in EBCDIC
   515    496   ** whereas only characters less than 0x80 do in ASCII.
   516    497   */
   517    498   #if defined(SQLITE_EBCDIC)
   518         -# define sqlite3Utf8Read(A,C)  (*(A++))
          499  +# define sqlite3Utf8Read(A)    (*((*A)++))
   519    500   # define GlogUpperToLower(A)   A = sqlite3UpperToLower[A]
   520    501   #else
   521    502   # define GlogUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
   522    503   #endif
   523    504   
   524    505   static const struct compareInfo globInfo = { '*', '?', '[', 0 };
   525    506   /* The correct SQL-92 behavior is for the LIKE operator to ignore
................................................................................
   568    549     int seen;
   569    550     u8 matchOne = pInfo->matchOne;
   570    551     u8 matchAll = pInfo->matchAll;
   571    552     u8 matchSet = pInfo->matchSet;
   572    553     u8 noCase = pInfo->noCase; 
   573    554     int prevEscape = 0;     /* True if the previous character was 'escape' */
   574    555   
   575         -  while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){
   576         -    if( !prevEscape && c==matchAll ){
   577         -      while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll
          556  +  while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
          557  +    if( c==matchAll && !prevEscape ){
          558  +      while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
   578    559                  || c == matchOne ){
   579         -        if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){
          560  +        if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
   580    561             return 0;
   581    562           }
   582    563         }
   583    564         if( c==0 ){
   584    565           return 1;
   585    566         }else if( c==esc ){
   586         -        c = sqlite3Utf8Read(zPattern, &zPattern);
          567  +        c = sqlite3Utf8Read(&zPattern);
   587    568           if( c==0 ){
   588    569             return 0;
   589    570           }
   590    571         }else if( c==matchSet ){
   591    572           assert( esc==0 );         /* This is GLOB, not LIKE */
   592    573           assert( matchSet<0x80 );  /* '[' is a single-byte character */
   593    574           while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
   594    575             SQLITE_SKIP_UTF8(zString);
   595    576           }
   596    577           return *zString!=0;
   597    578         }
   598         -      while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){
          579  +      while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
   599    580           if( noCase ){
   600    581             GlogUpperToLower(c2);
   601    582             GlogUpperToLower(c);
   602    583             while( c2 != 0 && c2 != c ){
   603         -            c2 = sqlite3Utf8Read(zString, &zString);
          584  +            c2 = sqlite3Utf8Read(&zString);
   604    585               GlogUpperToLower(c2);
   605    586             }
   606    587           }else{
   607    588             while( c2 != 0 && c2 != c ){
   608         -            c2 = sqlite3Utf8Read(zString, &zString);
          589  +            c2 = sqlite3Utf8Read(&zString);
   609    590             }
   610    591           }
   611    592           if( c2==0 ) return 0;
   612    593           if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
   613    594         }
   614    595         return 0;
   615         -    }else if( !prevEscape && c==matchOne ){
   616         -      if( sqlite3Utf8Read(zString, &zString)==0 ){
          596  +    }else if( c==matchOne && !prevEscape ){
          597  +      if( sqlite3Utf8Read(&zString)==0 ){
   617    598           return 0;
   618    599         }
   619    600       }else if( c==matchSet ){
   620    601         u32 prior_c = 0;
   621    602         assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
   622    603         seen = 0;
   623    604         invert = 0;
   624         -      c = sqlite3Utf8Read(zString, &zString);
          605  +      c = sqlite3Utf8Read(&zString);
   625    606         if( c==0 ) return 0;
   626         -      c2 = sqlite3Utf8Read(zPattern, &zPattern);
          607  +      c2 = sqlite3Utf8Read(&zPattern);
   627    608         if( c2=='^' ){
   628    609           invert = 1;
   629         -        c2 = sqlite3Utf8Read(zPattern, &zPattern);
          610  +        c2 = sqlite3Utf8Read(&zPattern);
   630    611         }
   631    612         if( c2==']' ){
   632    613           if( c==']' ) seen = 1;
   633         -        c2 = sqlite3Utf8Read(zPattern, &zPattern);
          614  +        c2 = sqlite3Utf8Read(&zPattern);
   634    615         }
   635    616         while( c2 && c2!=']' ){
   636    617           if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
   637         -          c2 = sqlite3Utf8Read(zPattern, &zPattern);
          618  +          c2 = sqlite3Utf8Read(&zPattern);
   638    619             if( c>=prior_c && c<=c2 ) seen = 1;
   639    620             prior_c = 0;
   640    621           }else{
   641    622             if( c==c2 ){
   642    623               seen = 1;
   643    624             }
   644    625             prior_c = c2;
   645    626           }
   646         -        c2 = sqlite3Utf8Read(zPattern, &zPattern);
          627  +        c2 = sqlite3Utf8Read(&zPattern);
   647    628         }
   648    629         if( c2==0 || (seen ^ invert)==0 ){
   649    630           return 0;
   650    631         }
   651    632       }else if( esc==c && !prevEscape ){
   652    633         prevEscape = 1;
   653    634       }else{
   654         -      c2 = sqlite3Utf8Read(zString, &zString);
          635  +      c2 = sqlite3Utf8Read(&zString);
   655    636         if( noCase ){
   656    637           GlogUpperToLower(c);
   657    638           GlogUpperToLower(c2);
   658    639         }
   659    640         if( c!=c2 ){
   660    641           return 0;
   661    642         }
................................................................................
   719    700       const unsigned char *zEsc = sqlite3_value_text(argv[2]);
   720    701       if( zEsc==0 ) return;
   721    702       if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
   722    703         sqlite3_result_error(context, 
   723    704             "ESCAPE expression must be a single character", -1);
   724    705         return;
   725    706       }
   726         -    escape = sqlite3Utf8Read(zEsc, &zEsc);
          707  +    escape = sqlite3Utf8Read(&zEsc);
   727    708     }
   728    709     if( zA && zB ){
   729    710       struct compareInfo *pInfo = sqlite3_user_data(context);
   730    711   #ifdef SQLITE_TEST
   731    712       sqlite3_like_count++;
   732    713   #endif
   733    714       

Changes to src/global.c.

   129    129   };
   130    130   #endif
   131    131   
   132    132   #ifndef SQLITE_USE_URI
   133    133   # define  SQLITE_USE_URI 0
   134    134   #endif
   135    135   
          136  +#ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
          137  +# define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
          138  +#endif
          139  +
   136    140   /*
   137    141   ** The following singleton contains the global configuration for
   138    142   ** the SQLite library.
   139    143   */
   140    144   SQLITE_WSD struct Sqlite3Config sqlite3Config = {
   141    145      SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
   142    146      1,                         /* bCoreMutex */
   143    147      SQLITE_THREADSAFE==1,      /* bFullMutex */
   144    148      SQLITE_USE_URI,            /* bOpenUri */
          149  +   SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
   145    150      0x7ffffffe,                /* mxStrlen */
   146    151      128,                       /* szLookaside */
   147    152      500,                       /* nLookaside */
   148    153      {0,0,0,0,0,0,0,0},         /* m */
   149    154      {0,0,0,0,0,0,0,0,0},       /* mutex */
   150    155      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
   151    156      (void*)0,                  /* pHeap */

Changes to src/insert.c.

   315    315   ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
   316    316   ** above are all no-ops
   317    317   */
   318    318   # define autoIncBegin(A,B,C) (0)
   319    319   # define autoIncStep(A,B,C)
   320    320   #endif /* SQLITE_OMIT_AUTOINCREMENT */
   321    321   
          322  +
          323  +/*
          324  +** Generate code for a co-routine that will evaluate a subquery one
          325  +** row at a time.
          326  +**
          327  +** The pSelect parameter is the subquery that the co-routine will evaluation.
          328  +** Information about the location of co-routine and the registers it will use
          329  +** is returned by filling in the pDest object.
          330  +**
          331  +** Registers are allocated as follows:
          332  +**
          333  +**   pDest->iSDParm      The register holding the next entry-point of the
          334  +**                       co-routine.  Run the co-routine to its next breakpoint
          335  +**                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
          336  +**
          337  +**   pDest->iSDParm+1    The register holding the "completed" flag for the
          338  +**                       co-routine. This register is 0 if the previous Yield
          339  +**                       generated a new result row, or 1 if the subquery
          340  +**                       has completed.  If the Yield is called again
          341  +**                       after this register becomes 1, then the VDBE will
          342  +**                       halt with an SQLITE_INTERNAL error.
          343  +**
          344  +**   pDest->iSdst        First result register.
          345  +**
          346  +**   pDest->nSdst        Number of result registers.
          347  +**
          348  +** This routine handles all of the register allocation and fills in the
          349  +** pDest structure appropriately.
          350  +**
          351  +** Here is a schematic of the generated code assuming that X is the 
          352  +** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
          353  +** completed flag reg[pDest->iSDParm+1], and R and S are the range of
          354  +** registers that hold the result set, reg[pDest->iSdst] through
          355  +** reg[pDest->iSdst+pDest->nSdst-1]:
          356  +**
          357  +**         X <- A
          358  +**         EOF <- 0
          359  +**         goto B
          360  +**      A: setup for the SELECT
          361  +**         loop rows in the SELECT
          362  +**           load results into registers R..S
          363  +**           yield X
          364  +**         end loop
          365  +**         cleanup after the SELECT
          366  +**         EOF <- 1
          367  +**         yield X
          368  +**         halt-error
          369  +**      B:
          370  +**
          371  +** To use this subroutine, the caller generates code as follows:
          372  +**
          373  +**         [ Co-routine generated by this subroutine, shown above ]
          374  +**      S: yield X
          375  +**         if EOF goto E
          376  +**         if skip this row, goto C
          377  +**         if terminate loop, goto E
          378  +**         deal with this row
          379  +**      C: goto S
          380  +**      E:
          381  +*/
          382  +int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
          383  +  int regYield;       /* Register holding co-routine entry-point */
          384  +  int regEof;         /* Register holding co-routine completion flag */
          385  +  int addrTop;        /* Top of the co-routine */
          386  +  int j1;             /* Jump instruction */
          387  +  int rc;             /* Result code */
          388  +  Vdbe *v;            /* VDBE under construction */
          389  +
          390  +  regYield = ++pParse->nMem;
          391  +  regEof = ++pParse->nMem;
          392  +  v = sqlite3GetVdbe(pParse);
          393  +  addrTop = sqlite3VdbeCurrentAddr(v);
          394  +  sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
          395  +  VdbeComment((v, "Co-routine entry point"));
          396  +  sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
          397  +  VdbeComment((v, "Co-routine completion flag"));
          398  +  sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
          399  +  j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
          400  +  rc = sqlite3Select(pParse, pSelect, pDest);
          401  +  assert( pParse->nErr==0 || rc );
          402  +  if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
          403  +  if( rc ) return rc;
          404  +  sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
          405  +  sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
          406  +  sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
          407  +  VdbeComment((v, "End of coroutine"));
          408  +  sqlite3VdbeJumpHere(v, j1);                             /* label B: */
          409  +  return rc;
          410  +}
          411  +
          412  +
   322    413   
   323    414   /* Forward declaration */
   324    415   static int xferOptimization(
   325    416     Parse *pParse,        /* Parser context */
   326    417     Table *pDest,         /* The table we are inserting into */
   327    418     Select *pSelect,      /* A SELECT statement to use as the data source */
   328    419     int onError,          /* How to handle constraint errors */
................................................................................
   564    655   
   565    656     /* Figure out how many columns of data are supplied.  If the data
   566    657     ** is coming from a SELECT statement, then generate a co-routine that
   567    658     ** produces a single row of the SELECT on each invocation.  The
   568    659     ** co-routine is the common header to the 3rd and 4th templates.
   569    660     */
   570    661     if( pSelect ){
   571         -    /* Data is coming from a SELECT.  Generate code to implement that SELECT
   572         -    ** as a co-routine.  The code is common to both the 3rd and 4th
   573         -    ** templates:
   574         -    **
   575         -    **         EOF <- 0
   576         -    **         X <- A
   577         -    **         goto B
   578         -    **      A: setup for the SELECT
   579         -    **         loop over the tables in the SELECT
   580         -    **           load value into register R..R+n
   581         -    **           yield X
   582         -    **         end loop
   583         -    **         cleanup after the SELECT
   584         -    **         EOF <- 1
   585         -    **         yield X
   586         -    **         halt-error
   587         -    **
   588         -    ** On each invocation of the co-routine, it puts a single row of the
   589         -    ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1.
   590         -    ** (These output registers are allocated by sqlite3Select().)  When
   591         -    ** the SELECT completes, it sets the EOF flag stored in regEof.
   592         -    */
   593         -    int rc, j1;
          662  +    /* Data is coming from a SELECT.  Generate a co-routine to run that
          663  +    ** SELECT. */
          664  +    int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
          665  +    if( rc ) goto insert_cleanup;
   594    666   
   595         -    regEof = ++pParse->nMem;
   596         -    sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);      /* EOF <- 0 */
   597         -    VdbeComment((v, "SELECT eof flag"));
   598         -    sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem);
   599         -    addrSelect = sqlite3VdbeCurrentAddr(v)+2;
   600         -    sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iSDParm);
   601         -    j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
   602         -    VdbeComment((v, "Jump over SELECT coroutine"));
   603         -
   604         -    /* Resolve the expressions in the SELECT statement and execute it. */
   605         -    rc = sqlite3Select(pParse, pSelect, &dest);
   606         -    assert( pParse->nErr==0 || rc );
   607         -    if( rc || NEVER(pParse->nErr) || db->mallocFailed ){
   608         -      goto insert_cleanup;
   609         -    }
   610         -    sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);         /* EOF <- 1 */
   611         -    sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);   /* yield X */
   612         -    sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
   613         -    VdbeComment((v, "End of SELECT coroutine"));
   614         -    sqlite3VdbeJumpHere(v, j1);                          /* label B: */
   615         -
          667  +    regEof = dest.iSDParm + 1;
   616    668       regFromSelect = dest.iSdst;
   617    669       assert( pSelect->pEList );
   618    670       nColumn = pSelect->pEList->nExpr;
   619    671       assert( dest.nSdst==nColumn );
   620    672   
   621    673       /* Set useTempTable to TRUE if the result of the SELECT statement
   622    674       ** should be written into a temporary table (template 4).  Set to

Changes to src/main.c.

   473    473         break;
   474    474       }
   475    475   
   476    476       case SQLITE_CONFIG_URI: {
   477    477         sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
   478    478         break;
   479    479       }
          480  +
          481  +    case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
          482  +      sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
          483  +      break;
          484  +    }
   480    485   
   481    486       default: {
   482    487         rc = SQLITE_ERROR;
   483    488         break;
   484    489       }
   485    490     }
   486    491     va_end(ap);
................................................................................
  1119   1124     int (*xBusy)(void*,int),
  1120   1125     void *pArg
  1121   1126   ){
  1122   1127     sqlite3_mutex_enter(db->mutex);
  1123   1128     db->busyHandler.xFunc = xBusy;
  1124   1129     db->busyHandler.pArg = pArg;
  1125   1130     db->busyHandler.nBusy = 0;
         1131  +  db->busyTimeout = 0;
  1126   1132     sqlite3_mutex_leave(db->mutex);
  1127   1133     return SQLITE_OK;
  1128   1134   }
  1129   1135   
  1130   1136   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  1131   1137   /*
  1132   1138   ** This routine sets the progress callback for an Sqlite database to the
................................................................................
  1156   1162   
  1157   1163   /*
  1158   1164   ** This routine installs a default busy handler that waits for the
  1159   1165   ** specified number of milliseconds before returning 0.
  1160   1166   */
  1161   1167   int sqlite3_busy_timeout(sqlite3 *db, int ms){
  1162   1168     if( ms>0 ){
  1163         -    db->busyTimeout = ms;
  1164   1169       sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
         1170  +    db->busyTimeout = ms;
  1165   1171     }else{
  1166   1172       sqlite3_busy_handler(db, 0, 0);
  1167   1173     }
  1168   1174     return SQLITE_OK;
  1169   1175   }
  1170   1176   
  1171   1177   /*
................................................................................
  1769   1775       return SQLITE_MISUSE_BKPT;
  1770   1776     }
  1771   1777     if( !db || db->mallocFailed ){
  1772   1778       return SQLITE_NOMEM;
  1773   1779     }
  1774   1780     return db->errCode;
  1775   1781   }
         1782  +
         1783  +/*
         1784  +** Return a string that describes the kind of error specified in the
         1785  +** argument.  For now, this simply calls the internal sqlite3ErrStr()
         1786  +** function.
         1787  +*/
         1788  +const char *sqlite3_errstr(int rc){
         1789  +  return sqlite3ErrStr(rc);
         1790  +}
  1776   1791   
  1777   1792   /*
  1778   1793   ** Create a new collating function for database "db".  The name is zName
  1779   1794   ** and the encoding is enc.
  1780   1795   */
  1781   1796   static int createCollation(
  1782   1797     sqlite3* db,
................................................................................
  2859   2874     **     2. The table is not a view and the column name identified an 
  2860   2875     **        explicitly declared column. Copy meta information from *pCol.
  2861   2876     */ 
  2862   2877     if( pCol ){
  2863   2878       zDataType = pCol->zType;
  2864   2879       zCollSeq = pCol->zColl;
  2865   2880       notnull = pCol->notNull!=0;
  2866         -    primarykey  = pCol->isPrimKey!=0;
         2881  +    primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
  2867   2882       autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
  2868   2883     }else{
  2869   2884       zDataType = "INTEGER";
  2870   2885       primarykey = 1;
  2871   2886     }
  2872   2887     if( !zCollSeq ){
  2873   2888       zCollSeq = "BINARY";
................................................................................
  3131   3146       ** operation N should be 0.  The idea is that a test program (like the
  3132   3147       ** SQL Logic Test or SLT test module) can run the same SQL multiple times
  3133   3148       ** with various optimizations disabled to verify that the same answer
  3134   3149       ** is obtained in every case.
  3135   3150       */
  3136   3151       case SQLITE_TESTCTRL_OPTIMIZATIONS: {
  3137   3152         sqlite3 *db = va_arg(ap, sqlite3*);
  3138         -      int x = va_arg(ap,int);
  3139         -      db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask);
         3153  +      db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
  3140   3154         break;
  3141   3155       }
  3142   3156   
  3143   3157   #ifdef SQLITE_N_KEYWORD
  3144   3158       /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
  3145   3159       **
  3146   3160       ** If zWord is a keyword recognized by the parser, then return the

Changes to src/os_win.c.

    20     20   #endif
    21     21   
    22     22   /*
    23     23   ** Include code that is common to all os_*.c files
    24     24   */
    25     25   #include "os_common.h"
    26     26   
           27  +/*
           28  +** Compiling and using WAL mode requires several APIs that are only
           29  +** available in Windows platforms based on the NT kernel.
           30  +*/
           31  +#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
           32  +# error "WAL mode requires support from the Windows NT kernel, compile\
           33  + with SQLITE_OMIT_WAL."
           34  +#endif
           35  +
    27     36   /*
    28     37   ** Macro to find the minimum of two numeric values.
    29     38   */
    30     39   #ifndef MIN
    31     40   # define MIN(x,y) ((x)<(y)?(x):(y))
    32     41   #endif
    33     42   
................................................................................
   303    312     { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
   304    313   #else
   305    314     { "CreateFileW",             (SYSCALL)0,                       0 },
   306    315   #endif
   307    316   
   308    317   #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
   309    318           LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
          319  +
          320  +#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
          321  +        !defined(SQLITE_OMIT_WAL))
          322  +  { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
          323  +#else
          324  +  { "CreateFileMappingA",      (SYSCALL)0,                       0 },
          325  +#endif
          326  +
          327  +#define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
          328  +        DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
   310    329   
   311    330   #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
   312    331           !defined(SQLITE_OMIT_WAL))
   313    332     { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
   314    333   #else
   315    334     { "CreateFileMappingW",      (SYSCALL)0,                       0 },
   316    335   #endif
   317    336   
   318    337   #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
   319         -        DWORD,DWORD,DWORD,LPCWSTR))aSyscall[6].pCurrent)
          338  +        DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
   320    339   
   321    340   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
   322    341     { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
   323    342   #else
   324    343     { "CreateMutexW",            (SYSCALL)0,                       0 },
   325    344   #endif
   326    345   
   327    346   #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
   328         -        LPCWSTR))aSyscall[7].pCurrent)
          347  +        LPCWSTR))aSyscall[8].pCurrent)
   329    348   
   330    349   #if defined(SQLITE_WIN32_HAS_ANSI)
   331    350     { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
   332    351   #else
   333    352     { "DeleteFileA",             (SYSCALL)0,                       0 },
   334    353   #endif
   335    354   
   336         -#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[8].pCurrent)
          355  +#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
   337    356   
   338    357   #if defined(SQLITE_WIN32_HAS_WIDE)
   339    358     { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
   340    359   #else
   341    360     { "DeleteFileW",             (SYSCALL)0,                       0 },
   342    361   #endif
   343    362   
   344         -#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[9].pCurrent)
          363  +#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
   345    364   
   346    365   #if SQLITE_OS_WINCE
   347    366     { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
   348    367   #else
   349    368     { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
   350    369   #endif
   351    370   
   352    371   #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
   353         -        LPFILETIME))aSyscall[10].pCurrent)
          372  +        LPFILETIME))aSyscall[11].pCurrent)
   354    373   
   355    374   #if SQLITE_OS_WINCE
   356    375     { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
   357    376   #else
   358    377     { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
   359    378   #endif
   360    379   
   361    380   #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
   362         -        LPSYSTEMTIME))aSyscall[11].pCurrent)
          381  +        LPSYSTEMTIME))aSyscall[12].pCurrent)
   363    382   
   364    383     { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
   365    384   
   366         -#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[12].pCurrent)
          385  +#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
   367    386   
   368    387   #if defined(SQLITE_WIN32_HAS_ANSI)
   369    388     { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
   370    389   #else
   371    390     { "FormatMessageA",          (SYSCALL)0,                       0 },
   372    391   #endif
   373    392   
   374    393   #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
   375         -        DWORD,va_list*))aSyscall[13].pCurrent)
          394  +        DWORD,va_list*))aSyscall[14].pCurrent)
   376    395   
   377    396   #if defined(SQLITE_WIN32_HAS_WIDE)
   378    397     { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
   379    398   #else
   380    399     { "FormatMessageW",          (SYSCALL)0,                       0 },
   381    400   #endif
   382    401   
   383    402   #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
   384         -        DWORD,va_list*))aSyscall[14].pCurrent)
          403  +        DWORD,va_list*))aSyscall[15].pCurrent)
   385    404   
   386    405     { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
   387    406   
   388         -#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[15].pCurrent)
          407  +#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
   389    408   
   390    409     { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
   391    410   
   392         -#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[16].pCurrent)
          411  +#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
   393    412   
   394    413   #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
   395    414     { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
   396    415   #else
   397    416     { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
   398    417   #endif
   399    418   
   400    419   #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
   401         -        LPDWORD))aSyscall[17].pCurrent)
          420  +        LPDWORD))aSyscall[18].pCurrent)
   402    421   
   403    422   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
   404    423     { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
   405    424   #else
   406    425     { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
   407    426   #endif
   408    427   
   409    428   #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
   410         -        LPDWORD))aSyscall[18].pCurrent)
          429  +        LPDWORD))aSyscall[19].pCurrent)
   411    430   
   412    431   #if defined(SQLITE_WIN32_HAS_ANSI)
   413    432     { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
   414    433   #else
   415    434     { "GetFileAttributesA",      (SYSCALL)0,                       0 },
   416    435   #endif
   417    436   
   418         -#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[19].pCurrent)
          437  +#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
   419    438   
   420    439   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
   421    440     { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
   422    441   #else
   423    442     { "GetFileAttributesW",      (SYSCALL)0,                       0 },
   424    443   #endif
   425    444   
   426         -#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[20].pCurrent)
          445  +#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
   427    446   
   428    447   #if defined(SQLITE_WIN32_HAS_WIDE)
   429    448     { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
   430    449   #else
   431    450     { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
   432    451   #endif
   433    452   
   434    453   #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
   435         -        LPVOID))aSyscall[21].pCurrent)
          454  +        LPVOID))aSyscall[22].pCurrent)
   436    455   
   437    456   #if !SQLITE_OS_WINRT
   438    457     { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
   439    458   #else
   440    459     { "GetFileSize",             (SYSCALL)0,                       0 },
   441    460   #endif
   442    461   
   443         -#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[22].pCurrent)
          462  +#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
   444    463   
   445    464   #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
   446    465     { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
   447    466   #else
   448    467     { "GetFullPathNameA",        (SYSCALL)0,                       0 },
   449    468   #endif
   450    469   
   451    470   #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
   452         -        LPSTR*))aSyscall[23].pCurrent)
          471  +        LPSTR*))aSyscall[24].pCurrent)
   453    472   
   454    473   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
   455    474     { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
   456    475   #else
   457    476     { "GetFullPathNameW",        (SYSCALL)0,                       0 },
   458    477   #endif
   459    478   
   460    479   #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
   461         -        LPWSTR*))aSyscall[24].pCurrent)
          480  +        LPWSTR*))aSyscall[25].pCurrent)
   462    481   
   463    482     { "GetLastError",            (SYSCALL)GetLastError,            0 },
   464    483   
   465         -#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[25].pCurrent)
          484  +#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
   466    485   
   467    486   #if SQLITE_OS_WINCE
   468    487     /* The GetProcAddressA() routine is only available on Windows CE. */
   469    488     { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
   470    489   #else
   471    490     /* All other Windows platforms expect GetProcAddress() to take
   472    491     ** an ANSI string regardless of the _UNICODE setting */
   473    492     { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
   474    493   #endif
   475    494   
   476    495   #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
   477         -        LPCSTR))aSyscall[26].pCurrent)
          496  +        LPCSTR))aSyscall[27].pCurrent)
   478    497   
   479    498   #if !SQLITE_OS_WINRT
   480    499     { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
   481    500   #else
   482    501     { "GetSystemInfo",           (SYSCALL)0,                       0 },
   483    502   #endif
   484    503   
   485         -#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[27].pCurrent)
          504  +#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
   486    505   
   487    506     { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
   488    507   
   489         -#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[28].pCurrent)
          508  +#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
   490    509   
   491    510   #if !SQLITE_OS_WINCE
   492    511     { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
   493    512   #else
   494    513     { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
   495    514   #endif
   496    515   
   497    516   #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
   498         -        LPFILETIME))aSyscall[29].pCurrent)
          517  +        LPFILETIME))aSyscall[30].pCurrent)
   499    518   
   500    519   #if defined(SQLITE_WIN32_HAS_ANSI)
   501    520     { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
   502    521   #else
   503    522     { "GetTempPathA",            (SYSCALL)0,                       0 },
   504    523   #endif
   505    524   
   506         -#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[30].pCurrent)
          525  +#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
   507    526   
   508    527   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
   509    528     { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
   510    529   #else
   511    530     { "GetTempPathW",            (SYSCALL)0,                       0 },
   512    531   #endif
   513    532   
   514         -#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[31].pCurrent)
          533  +#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
   515    534   
   516    535   #if !SQLITE_OS_WINRT
   517    536     { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
   518    537   #else
   519    538     { "GetTickCount",            (SYSCALL)0,                       0 },
   520    539   #endif
   521    540   
   522         -#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[32].pCurrent)
          541  +#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
   523    542   
   524    543   #if defined(SQLITE_WIN32_HAS_ANSI)
   525    544     { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
   526    545   #else
   527    546     { "GetVersionExA",           (SYSCALL)0,                       0 },
   528    547   #endif
   529    548   
   530    549   #define osGetVersionExA ((BOOL(WINAPI*)( \
   531         -        LPOSVERSIONINFOA))aSyscall[33].pCurrent)
          550  +        LPOSVERSIONINFOA))aSyscall[34].pCurrent)
   532    551   
   533    552     { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
   534    553   
   535    554   #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
   536         -        SIZE_T))aSyscall[34].pCurrent)
          555  +        SIZE_T))aSyscall[35].pCurrent)
   537    556   
   538    557   #if !SQLITE_OS_WINRT
   539    558     { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
   540    559   #else
   541    560     { "HeapCreate",              (SYSCALL)0,                       0 },
   542    561   #endif
   543    562   
   544    563   #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
   545         -        SIZE_T))aSyscall[35].pCurrent)
          564  +        SIZE_T))aSyscall[36].pCurrent)
   546    565   
   547    566   #if !SQLITE_OS_WINRT
   548    567     { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
   549    568   #else
   550    569     { "HeapDestroy",             (SYSCALL)0,                       0 },
   551    570   #endif
   552    571   
   553         -#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[36].pCurrent)
          572  +#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
   554    573   
   555    574     { "HeapFree",                (SYSCALL)HeapFree,                0 },
   556    575   
   557         -#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[37].pCurrent)
          576  +#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
   558    577   
   559    578     { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
   560    579   
   561    580   #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
   562         -        SIZE_T))aSyscall[38].pCurrent)
          581  +        SIZE_T))aSyscall[39].pCurrent)
   563    582   
   564    583     { "HeapSize",                (SYSCALL)HeapSize,                0 },
   565    584   
   566    585   #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
   567         -        LPCVOID))aSyscall[39].pCurrent)
          586  +        LPCVOID))aSyscall[40].pCurrent)
   568    587   
   569    588   #if !SQLITE_OS_WINRT
   570    589     { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
   571    590   #else
   572    591     { "HeapValidate",            (SYSCALL)0,                       0 },
   573    592   #endif
   574    593   
   575    594   #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
   576         -        LPCVOID))aSyscall[40].pCurrent)
          595  +        LPCVOID))aSyscall[41].pCurrent)
   577    596   
   578    597   #if defined(SQLITE_WIN32_HAS_ANSI)
   579    598     { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
   580    599   #else
   581    600     { "LoadLibraryA",            (SYSCALL)0,                       0 },
   582    601   #endif
   583    602   
   584         -#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[41].pCurrent)
          603  +#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
   585    604   
   586    605   #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
   587    606     { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
   588    607   #else
   589    608     { "LoadLibraryW",            (SYSCALL)0,                       0 },
   590    609   #endif
   591    610   
   592         -#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[42].pCurrent)
          611  +#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
   593    612   
   594    613   #if !SQLITE_OS_WINRT
   595    614     { "LocalFree",               (SYSCALL)LocalFree,               0 },
   596    615   #else
   597    616     { "LocalFree",               (SYSCALL)0,                       0 },
   598    617   #endif
   599    618   
   600         -#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[43].pCurrent)
          619  +#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
   601    620   
   602    621   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
   603    622     { "LockFile",                (SYSCALL)LockFile,                0 },
   604    623   #else
   605    624     { "LockFile",                (SYSCALL)0,                       0 },
   606    625   #endif
   607    626   
   608    627   #ifndef osLockFile
   609    628   #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   610         -        DWORD))aSyscall[44].pCurrent)
          629  +        DWORD))aSyscall[45].pCurrent)
   611    630   #endif
   612    631   
   613    632   #if !SQLITE_OS_WINCE
   614    633     { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
   615    634   #else
   616    635     { "LockFileEx",              (SYSCALL)0,                       0 },
   617    636   #endif
   618    637   
   619    638   #ifndef osLockFileEx
   620    639   #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
   621         -        LPOVERLAPPED))aSyscall[45].pCurrent)
          640  +        LPOVERLAPPED))aSyscall[46].pCurrent)
   622    641   #endif
   623    642   
   624    643   #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
   625    644     { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
   626    645   #else
   627    646     { "MapViewOfFile",           (SYSCALL)0,                       0 },
   628    647   #endif
   629    648   
   630    649   #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   631         -        SIZE_T))aSyscall[46].pCurrent)
          650  +        SIZE_T))aSyscall[47].pCurrent)
   632    651   
   633    652     { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
   634    653   
   635    654   #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
   636         -        int))aSyscall[47].pCurrent)
          655  +        int))aSyscall[48].pCurrent)
   637    656   
   638    657     { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
   639    658   
   640    659   #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
   641         -        LARGE_INTEGER*))aSyscall[48].pCurrent)
          660  +        LARGE_INTEGER*))aSyscall[49].pCurrent)
   642    661   
   643    662     { "ReadFile",                (SYSCALL)ReadFile,                0 },
   644    663   
   645    664   #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
   646         -        LPOVERLAPPED))aSyscall[49].pCurrent)
          665  +        LPOVERLAPPED))aSyscall[50].pCurrent)
   647    666   
   648    667     { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
   649    668   
   650         -#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[50].pCurrent)
          669  +#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
   651    670   
   652    671   #if !SQLITE_OS_WINRT
   653    672     { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
   654    673   #else
   655    674     { "SetFilePointer",          (SYSCALL)0,                       0 },
   656    675   #endif
   657    676   
   658    677   #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
   659         -        DWORD))aSyscall[51].pCurrent)
          678  +        DWORD))aSyscall[52].pCurrent)
   660    679   
   661    680   #if !SQLITE_OS_WINRT
   662    681     { "Sleep",                   (SYSCALL)Sleep,                   0 },
   663    682   #else
   664    683     { "Sleep",                   (SYSCALL)0,                       0 },
   665    684   #endif
   666    685   
   667         -#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[52].pCurrent)
          686  +#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
   668    687   
   669    688     { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
   670    689   
   671    690   #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
   672         -        LPFILETIME))aSyscall[53].pCurrent)
          691  +        LPFILETIME))aSyscall[54].pCurrent)
   673    692   
   674    693   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
   675    694     { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
   676    695   #else
   677    696     { "UnlockFile",              (SYSCALL)0,                       0 },
   678    697   #endif
   679    698   
   680    699   #ifndef osUnlockFile
   681    700   #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   682         -        DWORD))aSyscall[54].pCurrent)
          701  +        DWORD))aSyscall[55].pCurrent)
   683    702   #endif
   684    703   
   685    704   #if !SQLITE_OS_WINCE
   686    705     { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
   687    706   #else
   688    707     { "UnlockFileEx",            (SYSCALL)0,                       0 },
   689    708   #endif
   690    709   
   691    710   #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
   692         -        LPOVERLAPPED))aSyscall[55].pCurrent)
          711  +        LPOVERLAPPED))aSyscall[56].pCurrent)
   693    712   
   694    713   #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
   695    714     { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
   696    715   #else
   697    716     { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
   698    717   #endif
   699    718   
   700         -#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[56].pCurrent)
          719  +#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
   701    720   
   702    721     { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
   703    722   
   704    723   #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
   705         -        LPCSTR,LPBOOL))aSyscall[57].pCurrent)
          724  +        LPCSTR,LPBOOL))aSyscall[58].pCurrent)
   706    725   
   707    726     { "WriteFile",               (SYSCALL)WriteFile,               0 },
   708    727   
   709    728   #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
   710         -        LPOVERLAPPED))aSyscall[58].pCurrent)
          729  +        LPOVERLAPPED))aSyscall[59].pCurrent)
   711    730   
   712    731   #if SQLITE_OS_WINRT
   713    732     { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
   714    733   #else
   715    734     { "CreateEventExW",          (SYSCALL)0,                       0 },
   716    735   #endif
   717    736   
   718    737   #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
   719         -        DWORD,DWORD))aSyscall[59].pCurrent)
          738  +        DWORD,DWORD))aSyscall[60].pCurrent)
   720    739   
   721    740   #if !SQLITE_OS_WINRT
   722    741     { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
   723    742   #else
   724    743     { "WaitForSingleObject",     (SYSCALL)0,                       0 },
   725    744   #endif
   726    745   
   727    746   #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
   728         -        DWORD))aSyscall[60].pCurrent)
          747  +        DWORD))aSyscall[61].pCurrent)
   729    748   
   730    749   #if SQLITE_OS_WINRT
   731    750     { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
   732    751   #else
   733    752     { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
   734    753   #endif
   735    754   
   736    755   #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
   737         -        BOOL))aSyscall[61].pCurrent)
          756  +        BOOL))aSyscall[62].pCurrent)
   738    757   
   739    758   #if SQLITE_OS_WINRT
   740    759     { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
   741    760   #else
   742    761     { "SetFilePointerEx",        (SYSCALL)0,                       0 },
   743    762   #endif
   744    763   
   745    764   #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
   746         -        PLARGE_INTEGER,DWORD))aSyscall[62].pCurrent)
          765  +        PLARGE_INTEGER,DWORD))aSyscall[63].pCurrent)
   747    766   
   748    767   #if SQLITE_OS_WINRT
   749    768     { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
   750    769   #else
   751    770     { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
   752    771   #endif
   753    772   
   754    773   #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
   755         -        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[63].pCurrent)
          774  +        FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[64].pCurrent)
   756    775   
   757    776   #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
   758    777     { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
   759    778   #else
   760    779     { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
   761    780   #endif
   762    781   
   763    782   #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
   764         -        SIZE_T))aSyscall[64].pCurrent)
          783  +        SIZE_T))aSyscall[65].pCurrent)
   765    784   
   766    785   #if SQLITE_OS_WINRT
   767    786     { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
   768    787   #else
   769    788     { "CreateFile2",             (SYSCALL)0,                       0 },
   770    789   #endif
   771    790   
   772    791   #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
   773         -        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[65].pCurrent)
          792  +        LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[66].pCurrent)
   774    793   
   775    794   #if SQLITE_OS_WINRT
   776    795     { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
   777    796   #else
   778    797     { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
   779    798   #endif
   780    799   
   781    800   #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
   782         -        DWORD))aSyscall[66].pCurrent)
          801  +        DWORD))aSyscall[67].pCurrent)
   783    802   
   784    803   #if SQLITE_OS_WINRT
   785    804     { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
   786    805   #else
   787    806     { "GetTickCount64",          (SYSCALL)0,                       0 },
   788    807   #endif
   789    808   
   790         -#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[67].pCurrent)
          809  +#define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[68].pCurrent)
   791    810   
   792    811   #if SQLITE_OS_WINRT
   793    812     { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
   794    813   #else
   795    814     { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
   796    815   #endif
   797    816   
   798    817   #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
   799         -        LPSYSTEM_INFO))aSyscall[68].pCurrent)
          818  +        LPSYSTEM_INFO))aSyscall[69].pCurrent)
   800    819   
   801    820   #if defined(SQLITE_WIN32_HAS_ANSI)
   802    821     { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
   803    822   #else
   804    823     { "OutputDebugStringA",      (SYSCALL)0,                       0 },
   805    824   #endif
   806    825   
   807         -#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[69].pCurrent)
          826  +#define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[70].pCurrent)
   808    827   
   809    828   #if defined(SQLITE_WIN32_HAS_WIDE)
   810    829     { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
   811    830   #else
   812    831     { "OutputDebugStringW",      (SYSCALL)0,                       0 },
   813    832   #endif
   814    833   
   815         -#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[70].pCurrent)
          834  +#define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[71].pCurrent)
   816    835   
   817    836     { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
   818    837   
   819         -#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[71].pCurrent)
          838  +#define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[72].pCurrent)
   820    839   
   821    840   #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
   822    841     { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
   823    842   #else
   824    843     { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
   825    844   #endif
   826    845   
   827    846   #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
   828         -        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[72].pCurrent)
          847  +        LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[73].pCurrent)
   829    848   
   830    849   }; /* End of the overrideable system calls */
   831    850   
   832    851   /*
   833    852   ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
   834    853   ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
   835    854   ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
................................................................................
   979    998   ** API as long as we don't call it when running Win95/98/ME.  A call to
   980    999   ** this routine is used to determine if the host is Win95/98/ME or
   981   1000   ** WinNT/2K/XP so that we will know whether or not we can safely call
   982   1001   ** the LockFileEx() API.
   983   1002   */
   984   1003   #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
   985   1004   # define isNT()  (1)
         1005  +#elif !defined(SQLITE_WIN32_HAS_WIDE)
         1006  +# define isNT()  (0)
   986   1007   #else
   987   1008     static int isNT(void){
   988   1009       if( sqlite3_os_type==0 ){
   989   1010         OSVERSIONINFOA sInfo;
   990   1011         sInfo.dwOSVersionInfoSize = sizeof(sInfo);
   991   1012         osGetVersionExA(&sInfo);
   992   1013         sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
   993   1014       }
   994   1015       return sqlite3_os_type==2;
   995   1016     }
   996         -#endif /* SQLITE_OS_WINCE */
         1017  +#endif
   997   1018   
   998   1019   #ifdef SQLITE_WIN32_MALLOC
   999   1020   /*
  1000   1021   ** Allocate nBytes of memory.
  1001   1022   */
  1002   1023   static void *winMemMalloc(int nBytes){
  1003   1024     HANDLE hHeap;
................................................................................
  1199   1220     int nChar;
  1200   1221     LPWSTR zWideFilename;
  1201   1222   
  1202   1223     nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
  1203   1224     if( nChar==0 ){
  1204   1225       return 0;
  1205   1226     }
  1206         -  zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
         1227  +  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
  1207   1228     if( zWideFilename==0 ){
  1208   1229       return 0;
  1209   1230     }
  1210   1231     nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
  1211   1232                                   nChar);
  1212   1233     if( nChar==0 ){
  1213   1234       sqlite3_free(zWideFilename);
................................................................................
  1224   1245     int nByte;
  1225   1246     char *zFilename;
  1226   1247   
  1227   1248     nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
  1228   1249     if( nByte == 0 ){
  1229   1250       return 0;
  1230   1251     }
  1231         -  zFilename = sqlite3_malloc( nByte );
         1252  +  zFilename = sqlite3MallocZero( nByte );
  1232   1253     if( zFilename==0 ){
  1233   1254       return 0;
  1234   1255     }
  1235   1256     nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
  1236   1257                                   0, 0);
  1237   1258     if( nByte == 0 ){
  1238   1259       sqlite3_free(zFilename);
................................................................................
  1254   1275     int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  1255   1276   
  1256   1277     nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
  1257   1278                                   0)*sizeof(WCHAR);
  1258   1279     if( nByte==0 ){
  1259   1280       return 0;
  1260   1281     }
  1261         -  zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
         1282  +  zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
  1262   1283     if( zMbcsFilename==0 ){
  1263   1284       return 0;
  1264   1285     }
  1265   1286     nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
  1266   1287                                   nByte);
  1267   1288     if( nByte==0 ){
  1268   1289       sqlite3_free(zMbcsFilename);
................................................................................
  1283   1304     char *zFilename;
  1284   1305     int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
  1285   1306   
  1286   1307     nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
  1287   1308     if( nByte == 0 ){
  1288   1309       return 0;
  1289   1310     }
  1290         -  zFilename = sqlite3_malloc( nByte );
         1311  +  zFilename = sqlite3MallocZero( nByte );
  1291   1312     if( zFilename==0 ){
  1292   1313       return 0;
  1293   1314     }
  1294   1315     nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
  1295   1316                                   nByte, 0, 0);
  1296   1317     if( nByte == 0 ){
  1297   1318       sqlite3_free(zFilename);
................................................................................
  2939   2960     int nName;                         /* Size of zName in bytes */
  2940   2961   
  2941   2962     assert( pDbFd->pShm==0 );    /* Not previously opened */
  2942   2963   
  2943   2964     /* Allocate space for the new sqlite3_shm object.  Also speculatively
  2944   2965     ** allocate space for a new winShmNode and filename.
  2945   2966     */
  2946         -  p = sqlite3_malloc( sizeof(*p) );
         2967  +  p = sqlite3MallocZero( sizeof(*p) );
  2947   2968     if( p==0 ) return SQLITE_IOERR_NOMEM;
  2948         -  memset(p, 0, sizeof(*p));
  2949   2969     nName = sqlite3Strlen30(pDbFd->zPath);
  2950         -  pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
         2970  +  pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
  2951   2971     if( pNew==0 ){
  2952   2972       sqlite3_free(p);
  2953   2973       return SQLITE_IOERR_NOMEM;
  2954   2974     }
  2955         -  memset(pNew, 0, sizeof(*pNew) + nName + 17);
  2956   2975     pNew->zFilename = (char*)&pNew[1];
  2957   2976     sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  2958   2977     sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
  2959   2978   
  2960   2979     /* Look to see if there is an existing winShmNode that can be used.
  2961   2980     ** If no matching winShmNode currently exists, create a new one.
  2962   2981     */
................................................................................
  3285   3304       if( !apNew ){
  3286   3305         rc = SQLITE_IOERR_NOMEM;
  3287   3306         goto shmpage_out;
  3288   3307       }
  3289   3308       pShmNode->aRegion = apNew;
  3290   3309   
  3291   3310       while( pShmNode->nRegion<=iRegion ){
  3292         -      HANDLE hMap;                /* file-mapping handle */
         3311  +      HANDLE hMap = NULL;         /* file-mapping handle */
  3293   3312         void *pMap = 0;             /* Mapped memory region */
  3294   3313        
  3295   3314   #if SQLITE_OS_WINRT
  3296   3315         hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
  3297   3316             NULL, PAGE_READWRITE, nByte, NULL
  3298   3317         );
  3299         -#else
         3318  +#elif defined(SQLITE_WIN32_HAS_WIDE)
  3300   3319         hMap = osCreateFileMappingW(pShmNode->hFile.h, 
  3301   3320             NULL, PAGE_READWRITE, 0, nByte, NULL
  3302   3321         );
         3322  +#elif defined(SQLITE_WIN32_HAS_ANSI)
         3323  +      hMap = osCreateFileMappingA(pShmNode->hFile.h, 
         3324  +          NULL, PAGE_READWRITE, 0, nByte, NULL
         3325  +      );
  3303   3326   #endif
  3304   3327         OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
  3305   3328                  (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
  3306   3329                  hMap ? "ok" : "failed"));
  3307   3330         if( hMap ){
  3308   3331           int iOffset = pShmNode->nRegion*szRegion;
  3309   3332           int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
................................................................................
  4044   4067     }else{
  4045   4068       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
  4046   4069     }
  4047   4070     return SQLITE_OK;
  4048   4071   #endif
  4049   4072   
  4050   4073   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
  4051         -  int nByte;
         4074  +  DWORD nByte;
  4052   4075     void *zConverted;
  4053   4076     char *zOut;
  4054   4077   
  4055   4078     /* If this path name begins with "/X:", where "X" is any alphabetic
  4056   4079     ** character, discard the initial "/" from the pathname.
  4057   4080     */
  4058   4081     if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
................................................................................
  4078   4101     }
  4079   4102     zConverted = convertUtf8Filename(zRelative);
  4080   4103     if( zConverted==0 ){
  4081   4104       return SQLITE_IOERR_NOMEM;
  4082   4105     }
  4083   4106     if( isNT() ){
  4084   4107       LPWSTR zTemp;
  4085         -    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
  4086         -    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
         4108  +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
         4109  +    if( nByte==0 ){
         4110  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4111  +                  "GetFullPathNameW1", zConverted);
         4112  +      sqlite3_free(zConverted);
         4113  +      return SQLITE_CANTOPEN_FULLPATH;
         4114  +    }
         4115  +    nByte += 3;
         4116  +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  4087   4117       if( zTemp==0 ){
  4088   4118         sqlite3_free(zConverted);
  4089   4119         return SQLITE_IOERR_NOMEM;
  4090   4120       }
  4091         -    osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
         4121  +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
         4122  +    if( nByte==0 ){
         4123  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4124  +                  "GetFullPathNameW2", zConverted);
         4125  +      sqlite3_free(zConverted);
         4126  +      sqlite3_free(zTemp);
         4127  +      return SQLITE_CANTOPEN_FULLPATH;
         4128  +    }
  4092   4129       sqlite3_free(zConverted);
  4093   4130       zOut = unicodeToUtf8(zTemp);
  4094   4131       sqlite3_free(zTemp);
  4095   4132     }
  4096   4133   #ifdef SQLITE_WIN32_HAS_ANSI
  4097   4134     else{
  4098   4135       char *zTemp;
  4099         -    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
  4100         -    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
         4136  +    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
         4137  +    if( nByte==0 ){
         4138  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4139  +                  "GetFullPathNameA1", zConverted);
         4140  +      sqlite3_free(zConverted);
         4141  +      return SQLITE_CANTOPEN_FULLPATH;
         4142  +    }
         4143  +    nByte += 3;
         4144  +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  4101   4145       if( zTemp==0 ){
  4102   4146         sqlite3_free(zConverted);
  4103   4147         return SQLITE_IOERR_NOMEM;
  4104   4148       }
  4105         -    osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
         4149  +    nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
         4150  +    if( nByte==0 ){
         4151  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4152  +                  "GetFullPathNameA2", zConverted);
         4153  +      sqlite3_free(zConverted);
         4154  +      sqlite3_free(zTemp);
         4155  +      return SQLITE_CANTOPEN_FULLPATH;
         4156  +    }
  4106   4157       sqlite3_free(zConverted);
  4107   4158       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  4108   4159       sqlite3_free(zTemp);
  4109   4160     }
  4110   4161   #endif
  4111   4162     if( zOut ){
  4112   4163       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
................................................................................
  4356   4407       winSetSystemCall,    /* xSetSystemCall */
  4357   4408       winGetSystemCall,    /* xGetSystemCall */
  4358   4409       winNextSystemCall,   /* xNextSystemCall */
  4359   4410     };
  4360   4411   
  4361   4412     /* Double-check that the aSyscall[] array has been constructed
  4362   4413     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  4363         -  assert( ArraySize(aSyscall)==73 );
         4414  +  assert( ArraySize(aSyscall)==74 );
  4364   4415   
  4365   4416   #ifndef SQLITE_OMIT_WAL
  4366   4417     /* get memory map allocation granularity */
  4367   4418     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  4368   4419   #if SQLITE_OS_WINRT
  4369   4420     osGetNativeSystemInfo(&winSysInfo);
  4370   4421   #else
................................................................................
  4375   4426   
  4376   4427     sqlite3_vfs_register(&winVfs, 1);
  4377   4428     return SQLITE_OK; 
  4378   4429   }
  4379   4430   
  4380   4431   int sqlite3_os_end(void){ 
  4381   4432   #if SQLITE_OS_WINRT
  4382         -  if( sleepObj != NULL ){
         4433  +  if( sleepObj!=NULL ){
  4383   4434       osCloseHandle(sleepObj);
  4384   4435       sleepObj = NULL;
  4385   4436     }
  4386   4437   #endif
  4387   4438     return SQLITE_OK;
  4388   4439   }
  4389   4440   
  4390   4441   #endif /* SQLITE_OS_WIN */

Changes to src/pager.c.

  4456   4456     /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
  4457   4457     if( zPathname ){
  4458   4458       assert( nPathname>0 );
  4459   4459       pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
  4460   4460       memcpy(pPager->zFilename, zPathname, nPathname);
  4461   4461       if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
  4462   4462       memcpy(pPager->zJournal, zPathname, nPathname);
  4463         -    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
         4463  +    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
  4464   4464       sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
  4465   4465   #ifndef SQLITE_OMIT_WAL
  4466   4466       pPager->zWal = &pPager->zJournal[nPathname+8+1];
  4467   4467       memcpy(pPager->zWal, zPathname, nPathname);
  4468   4468       memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
  4469   4469       sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
  4470   4470   #endif

Changes to src/pragma.c.

   990    990              pCol->zType ? pCol->zType : "", 0);
   991    991           sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
   992    992           if( pCol->zDflt ){
   993    993             sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
   994    994           }else{
   995    995             sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
   996    996           }
   997         -        sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
          997  +        sqlite3VdbeAddOp2(v, OP_Integer,
          998  +                            (pCol->colFlags&COLFLAG_PRIMKEY)!=0, 6);
   998    999           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
   999   1000         }
  1000   1001       }
  1001   1002     }else
  1002   1003   
  1003   1004     if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
  1004   1005       Index *pIdx;
................................................................................
  1249   1250         /* Do the b-tree integrity checks */
  1250   1251         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
  1251   1252         sqlite3VdbeChangeP5(v, (u8)i);
  1252   1253         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
  1253   1254         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1254   1255            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
  1255   1256            P4_DYNAMIC);
  1256         -      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
         1257  +      sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
  1257   1258         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
  1258   1259         sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
  1259   1260         sqlite3VdbeJumpHere(v, addr);
  1260   1261   
  1261   1262         /* Make sure all the indices are constructed correctly.
  1262   1263         */
  1263   1264         for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
................................................................................
  1554   1555     ** This pragma attempts to free as much memory as possible from the
  1555   1556     ** current database connection.
  1556   1557     */
  1557   1558     if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
  1558   1559       sqlite3_db_release_memory(db);
  1559   1560     }else
  1560   1561   
         1562  +  /*
         1563  +  **   PRAGMA busy_timeout
         1564  +  **   PRAGMA busy_timeout = N
         1565  +  **
         1566  +  ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
         1567  +  ** if one is set.  If no busy handler or a different busy handler is set
         1568  +  ** then 0 is returned.  Setting the busy_timeout to 0 or negative
         1569  +  ** disables the timeout.
         1570  +  */
         1571  +  if( sqlite3StrICmp(zLeft, "busy_timeout")==0 ){
         1572  +    if( zRight ){
         1573  +      sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
         1574  +    }
         1575  +    returnSingleInt(pParse, "timeout",  &db->busyTimeout);
         1576  +  }else
         1577  +
  1561   1578   #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  1562   1579     /*
  1563   1580     ** Report the current state of file logs for all databases
  1564   1581     */
  1565   1582     if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
  1566   1583       static const char *const azLockName[] = {
  1567   1584         "unlocked", "shared", "reserved", "pending", "exclusive"

Changes to src/prepare.c.

   133    133   ** database.  iDb==1 should never be used.  iDb>=2 is used for
   134    134   ** auxiliary databases.  Return one of the SQLITE_ error codes to
   135    135   ** indicate success or failure.
   136    136   */
   137    137   static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
   138    138     int rc;
   139    139     int i;
          140  +#ifndef SQLITE_OMIT_DEPRECATED
   140    141     int size;
          142  +#endif
   141    143     Table *pTab;
   142    144     Db *pDb;
   143    145     char const *azArg[4];
   144    146     int meta[5];
   145    147     InitData initData;
   146    148     char const *zMasterSchema;
   147    149     char const *zMasterName;

Changes to src/select.c.

   521    521       return 1;
   522    522     }else{
   523    523       return 0;
   524    524     }
   525    525   }
   526    526   #endif
   527    527   
          528  +/*
          529  +** An instance of the following object is used to record information about
          530  +** how to process the DISTINCT keyword, to simplify passing that information
          531  +** into the selectInnerLoop() routine.
          532  +*/
          533  +typedef struct DistinctCtx DistinctCtx;
          534  +struct DistinctCtx {
          535  +  u8 isTnct;      /* True if the DISTINCT keyword is present */
          536  +  u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
          537  +  int tabTnct;    /* Ephemeral table used for DISTINCT processing */
          538  +  int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
          539  +};
          540  +
   528    541   /*
   529    542   ** This routine generates the code for the inside of the inner loop
   530    543   ** of a SELECT.
   531    544   **
   532    545   ** If srcTab and nColumn are both zero, then the pEList expressions
   533    546   ** are evaluated in order to get the data for this row.  If nColumn>0
   534    547   ** then data is pulled from srcTab and pEList is used only to get the
................................................................................
   537    550   static void selectInnerLoop(
   538    551     Parse *pParse,          /* The parser context */
   539    552     Select *p,              /* The complete select statement being coded */
   540    553     ExprList *pEList,       /* List of values being extracted */
   541    554     int srcTab,             /* Pull data from this table */
   542    555     int nColumn,            /* Number of columns in the source table */
   543    556     ExprList *pOrderBy,     /* If not NULL, sort results using this key */
   544         -  int distinct,           /* If >=0, make sure results are distinct */
          557  +  DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
   545    558     SelectDest *pDest,      /* How to dispose of the results */
   546    559     int iContinue,          /* Jump here to continue with next row */
   547    560     int iBreak              /* Jump here to break out of the inner loop */
   548    561   ){
   549    562     Vdbe *v = pParse->pVdbe;
   550    563     int i;
   551    564     int hasDistinct;        /* True if the DISTINCT keyword is present */
................................................................................
   553    566     int eDest = pDest->eDest;   /* How to dispose of results */
   554    567     int iParm = pDest->iSDParm; /* First argument to disposal method */
   555    568     int nResultCol;             /* Number of result columns */
   556    569   
   557    570     assert( v );
   558    571     if( NEVER(v==0) ) return;
   559    572     assert( pEList!=0 );
   560         -  hasDistinct = distinct>=0;
          573  +  hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
   561    574     if( pOrderBy==0 && !hasDistinct ){
   562    575       codeOffset(v, p, iContinue);
   563    576     }
   564    577   
   565    578     /* Pull the requested columns.
   566    579     */
   567    580     if( nColumn>0 ){
................................................................................
   593    606     /* If the DISTINCT keyword was present on the SELECT statement
   594    607     ** and this row has been seen before, then do not make this row
   595    608     ** part of the result.
   596    609     */
   597    610     if( hasDistinct ){
   598    611       assert( pEList!=0 );
   599    612       assert( pEList->nExpr==nColumn );
   600         -    codeDistinct(pParse, distinct, iContinue, nColumn, regResult);
          613  +    switch( pDistinct->eTnctType ){
          614  +      case WHERE_DISTINCT_ORDERED: {
          615  +        VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
          616  +        int iJump;              /* Jump destination */
          617  +        int regPrev;            /* Previous row content */
          618  +
          619  +        /* Allocate space for the previous row */
          620  +        regPrev = pParse->nMem+1;
          621  +        pParse->nMem += nColumn;
          622  +
          623  +        /* Change the OP_OpenEphemeral coded earlier to an OP_Null
          624  +        ** sets the MEM_Cleared bit on the first register of the
          625  +        ** previous value.  This will cause the OP_Ne below to always
          626  +        ** fail on the first iteration of the loop even if the first
          627  +        ** row is all NULLs.
          628  +        */
          629  +        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
          630  +        pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
          631  +        pOp->opcode = OP_Null;
          632  +        pOp->p1 = 1;
          633  +        pOp->p2 = regPrev;
          634  +
          635  +        iJump = sqlite3VdbeCurrentAddr(v) + nColumn;
          636  +        for(i=0; i<nColumn; i++){
          637  +          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
          638  +          if( i<nColumn-1 ){
          639  +            sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
          640  +          }else{
          641  +            sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
          642  +          }
          643  +          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
          644  +          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
          645  +        }
          646  +        assert( sqlite3VdbeCurrentAddr(v)==iJump );
          647  +        sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nColumn-1);
          648  +        break;
          649  +      }
          650  +
          651  +      case WHERE_DISTINCT_UNIQUE: {
          652  +        sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
          653  +        break;
          654  +      }
          655  +
          656  +      default: {
          657  +        assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
          658  +        codeDistinct(pParse, pDistinct->tabTnct, iContinue, nColumn, regResult);
          659  +        break;
          660  +      }
          661  +    }
   601    662       if( pOrderBy==0 ){
   602    663         codeOffset(v, p, iContinue);
   603    664       }
   604    665     }
   605    666   
   606    667     switch( eDest ){
   607    668       /* In this mode, write each query result to the key of the temporary
................................................................................
   651    712   #ifndef SQLITE_OMIT_SUBQUERY
   652    713       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
   653    714       ** then there should be a single item on the stack.  Write this
   654    715       ** item into the set table with bogus data.
   655    716       */
   656    717       case SRT_Set: {
   657    718         assert( nColumn==1 );
   658         -      p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
          719  +      pDest->affSdst =
          720  +                  sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
   659    721         if( pOrderBy ){
   660    722           /* At first glance you would think we could optimize out the
   661    723           ** ORDER BY in this case since the order of entries in the set
   662    724           ** does not matter.  But there might be a LIMIT clause, in which
   663    725           ** case the order does matter */
   664    726           pushOntoSorter(pParse, pOrderBy, p, regResult);
   665    727         }else{
   666    728           int r1 = sqlite3GetTempReg(pParse);
   667         -        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1);
          729  +        sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
   668    730           sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
   669    731           sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
   670    732           sqlite3ReleaseTempReg(pParse, r1);
   671    733         }
   672    734         break;
   673    735       }
   674    736   
................................................................................
   927    989         sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
   928    990         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   929    991         break;
   930    992       }
   931    993   #ifndef SQLITE_OMIT_SUBQUERY
   932    994       case SRT_Set: {
   933    995         assert( nColumn==1 );
   934         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1);
          996  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
          997  +                        &pDest->affSdst, 1);
   935    998         sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
   936    999         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
   937   1000         break;
   938   1001       }
   939   1002       case SRT_Mem: {
   940   1003         assert( nColumn==1 );
   941   1004         sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
................................................................................
  1242   1305   **
  1243   1306   ** Return SQLITE_OK on success.  If a memory allocation error occurs,
  1244   1307   ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
  1245   1308   */
  1246   1309   static int selectColumnsFromExprList(
  1247   1310     Parse *pParse,          /* Parsing context */
  1248   1311     ExprList *pEList,       /* Expr list from which to derive column names */
  1249         -  int *pnCol,             /* Write the number of columns here */
         1312  +  i16 *pnCol,             /* Write the number of columns here */
  1250   1313     Column **paCol          /* Write the new column list here */
  1251   1314   ){
  1252   1315     sqlite3 *db = pParse->db;   /* Database connection */
  1253   1316     int i, j;                   /* Loop counters */
  1254   1317     int cnt;                    /* Index added to make the name unique */
  1255   1318     Column *aCol, *pCol;        /* For looping over result columns */
  1256   1319     int nCol;                   /* Number of columns in the result set */
................................................................................
  1764   1827           }
  1765   1828           iBreak = sqlite3VdbeMakeLabel(v);
  1766   1829           iCont = sqlite3VdbeMakeLabel(v);
  1767   1830           computeLimitRegisters(pParse, p, iBreak);
  1768   1831           sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
  1769   1832           iStart = sqlite3VdbeCurrentAddr(v);
  1770   1833           selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr,
  1771         -                        0, -1, &dest, iCont, iBreak);
         1834  +                        0, 0, &dest, iCont, iBreak);
  1772   1835           sqlite3VdbeResolveLabel(v, iCont);
  1773   1836           sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
  1774   1837           sqlite3VdbeResolveLabel(v, iBreak);
  1775   1838           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
  1776   1839         }
  1777   1840         break;
  1778   1841       }
................................................................................
  1842   1905         computeLimitRegisters(pParse, p, iBreak);
  1843   1906         sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
  1844   1907         r1 = sqlite3GetTempReg(pParse);
  1845   1908         iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
  1846   1909         sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
  1847   1910         sqlite3ReleaseTempReg(pParse, r1);
  1848   1911         selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr,
  1849         -                      0, -1, &dest, iCont, iBreak);
         1912  +                      0, 0, &dest, iCont, iBreak);
  1850   1913         sqlite3VdbeResolveLabel(v, iCont);
  1851   1914         sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
  1852   1915         sqlite3VdbeResolveLabel(v, iBreak);
  1853   1916         sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
  1854   1917         sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
  1855   1918         break;
  1856   1919       }
................................................................................
  1888   1951   
  1889   1952       for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
  1890   1953         *apColl = multiSelectCollSeq(pParse, p, i);
  1891   1954         if( 0==*apColl ){
  1892   1955           *apColl = db->pDfltColl;
  1893   1956         }
  1894   1957       }
         1958  +    pKeyInfo->aSortOrder = (u8*)apColl;
  1895   1959   
  1896   1960       for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
  1897   1961         for(i=0; i<2; i++){
  1898   1962           int addr = pLoop->addrOpenEphm[i];
  1899   1963           if( addr<0 ){
  1900   1964             /* If [0] is unused then [1] is also unused.  So we can
  1901   1965             ** always safely abort as soon as the first unused slot is found */
................................................................................
  1961   2025     if( regPrev ){
  1962   2026       int j1, j2;
  1963   2027       j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
  1964   2028       j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
  1965   2029                                 (char*)pKeyInfo, p4type);
  1966   2030       sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
  1967   2031       sqlite3VdbeJumpHere(v, j1);
  1968         -    sqlite3ExprCodeCopy(pParse, pIn->iSdst, regPrev+1, pIn->nSdst);
         2032  +    sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
  1969   2033       sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
  1970   2034     }
  1971   2035     if( pParse->db->mallocFailed ) return 0;
  1972   2036   
  1973   2037     /* Suppress the first OFFSET entries if there is an OFFSET clause
  1974   2038     */
  1975   2039     codeOffset(v, p, iContinue);
................................................................................
  1996   2060       /* If we are creating a set for an "expr IN (SELECT ...)" construct,
  1997   2061       ** then there should be a single item on the stack.  Write this
  1998   2062       ** item into the set table with bogus data.
  1999   2063       */
  2000   2064       case SRT_Set: {
  2001   2065         int r1;
  2002   2066         assert( pIn->nSdst==1 );
  2003         -      p->affinity = 
         2067  +      pDest->affSdst = 
  2004   2068            sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
  2005   2069         r1 = sqlite3GetTempReg(pParse);
  2006         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &p->affinity, 1);
         2070  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
  2007   2071         sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
  2008   2072         sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
  2009   2073         sqlite3ReleaseTempReg(pParse, r1);
  2010   2074         break;
  2011   2075       }
  2012   2076   
  2013   2077   #if 0  /* Never occurs on an ORDER BY query */
................................................................................
  2741   2805     struct SrcList_item *pSubitem;   /* The subquery */
  2742   2806     sqlite3 *db = pParse->db;
  2743   2807   
  2744   2808     /* Check to see if flattening is permitted.  Return 0 if not.
  2745   2809     */
  2746   2810     assert( p!=0 );
  2747   2811     assert( p->pPrior==0 );  /* Unable to flatten compound queries */
  2748         -  if( db->flags & SQLITE_QueryFlattener ) return 0;
         2812  +  if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
  2749   2813     pSrc = p->pSrc;
  2750   2814     assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
  2751   2815     pSubitem = &pSrc->a[iFrom];
  2752   2816     iParent = pSubitem->iCursor;
  2753   2817     pSub = pSubitem->pSelect;
  2754   2818     assert( pSub!=0 );
  2755   2819     if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
................................................................................
  3781   3845     int isAgg;             /* True for select lists like "count(*)" */
  3782   3846     ExprList *pEList;      /* List of columns to extract. */
  3783   3847     SrcList *pTabList;     /* List of tables to select from */
  3784   3848     Expr *pWhere;          /* The WHERE clause.  May be NULL */
  3785   3849     ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
  3786   3850     ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
  3787   3851     Expr *pHaving;         /* The HAVING clause.  May be NULL */
  3788         -  int isDistinct;        /* True if the DISTINCT keyword is present */
  3789         -  int distinct;          /* Table to use for the distinct set */
  3790   3852     int rc = 1;            /* Value to return from this function */
  3791   3853     int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
  3792         -  int addrDistinctIndex; /* Address of an OP_OpenEphemeral instruction */
         3854  +  DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
  3793   3855     AggInfo sAggInfo;      /* Information used by aggregate queries */
  3794   3856     int iEnd;              /* Address of the end of the query */
  3795   3857     sqlite3 *db;           /* The database connection */
  3796   3858   
  3797   3859   #ifndef SQLITE_OMIT_EXPLAIN
  3798   3860     int iRestoreSelectId = pParse->iSelectId;
  3799   3861     pParse->iSelectId = pParse->iNextSelectId++;
................................................................................
  3911   3973       }
  3912   3974     }
  3913   3975     pEList = p->pEList;
  3914   3976   #endif
  3915   3977     pWhere = p->pWhere;
  3916   3978     pGroupBy = p->pGroupBy;
  3917   3979     pHaving = p->pHaving;
  3918         -  isDistinct = (p->selFlags & SF_Distinct)!=0;
         3980  +  sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
  3919   3981   
  3920   3982   #ifndef SQLITE_OMIT_COMPOUND_SELECT
  3921   3983     /* If there is are a sequence of queries, do the earlier ones first.
  3922   3984     */
  3923   3985     if( p->pPrior ){
  3924   3986       if( p->pRightmost==0 ){
  3925   3987         Select *pLoop, *pRight = 0;
................................................................................
  3946   4008     ** identical, then disable the ORDER BY clause since the GROUP BY
  3947   4009     ** will cause elements to come out in the correct order.  This is
  3948   4010     ** an optimization - the correct answer should result regardless.
  3949   4011     ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
  3950   4012     ** to disable this optimization for testing purposes.
  3951   4013     */
  3952   4014     if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0
  3953         -         && (db->flags & SQLITE_GroupByOrder)==0 ){
         4015  +         && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
  3954   4016       pOrderBy = 0;
  3955   4017     }
  3956   4018   
  3957   4019     /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and 
  3958   4020     ** if the select-list is the same as the ORDER BY list, then this query
  3959   4021     ** can be rewritten as a GROUP BY. In other words, this:
  3960   4022     **
................................................................................
  3972   4034     if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct 
  3973   4035      && sqlite3ExprListCompare(pOrderBy, p->pEList)==0
  3974   4036     ){
  3975   4037       p->selFlags &= ~SF_Distinct;
  3976   4038       p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
  3977   4039       pGroupBy = p->pGroupBy;
  3978   4040       pOrderBy = 0;
         4041  +    /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
         4042  +    ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
         4043  +    ** original setting of the SF_Distinct flag, not the current setting */
         4044  +    assert( sDistinct.isTnct );
  3979   4045     }
  3980   4046   
  3981   4047     /* If there is an ORDER BY clause, then this sorting
  3982   4048     ** index might end up being unused if the data can be 
  3983   4049     ** extracted in pre-sorted order.  If that is the case, then the
  3984   4050     ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
  3985   4051     ** we figure out that the sorting index is not needed.  The addrSortIndex
................................................................................
  4012   4078       sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
  4013   4079       p->selFlags |= SF_UseSorter;
  4014   4080     }
  4015   4081   
  4016   4082     /* Open a virtual index to use for the distinct set.
  4017   4083     */
  4018   4084     if( p->selFlags & SF_Distinct ){
  4019         -    KeyInfo *pKeyInfo;
  4020         -    distinct = pParse->nTab++;
  4021         -    pKeyInfo = keyInfoFromExprList(pParse, p->pEList);
  4022         -    addrDistinctIndex = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0,
  4023         -        (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
         4085  +    sDistinct.tabTnct = pParse->nTab++;
         4086  +    sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
         4087  +                                sDistinct.tabTnct, 0, 0,
         4088  +                                (char*)keyInfoFromExprList(pParse, p->pEList),
         4089  +                                P4_KEYINFO_HANDOFF);
  4024   4090       sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
         4091  +    sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
  4025   4092     }else{
  4026         -    distinct = addrDistinctIndex = -1;
         4093  +    sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
  4027   4094     }
  4028   4095   
  4029         -  /* Aggregate and non-aggregate queries are handled differently */
  4030   4096     if( !isAgg && pGroupBy==0 ){
  4031         -    ExprList *pDist = (isDistinct ? p->pEList : 0);
         4097  +    /* No aggregate functions and no GROUP BY clause */
         4098  +    ExprList *pDist = (sDistinct.isTnct ? p->pEList : 0);
  4032   4099   
  4033   4100       /* Begin the database scan. */
  4034         -    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, pDist, 0,0);
         4101  +    pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, pDist, 0,0);
  4035   4102       if( pWInfo==0 ) goto select_end;
  4036   4103       if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut;
         4104  +    if( pWInfo->eDistinct ) sDistinct.eTnctType = pWInfo->eDistinct;
         4105  +    if( pOrderBy && pWInfo->nOBSat==pOrderBy->nExpr ) pOrderBy = 0;
  4037   4106   
  4038   4107       /* If sorting index that was created by a prior OP_OpenEphemeral 
  4039   4108       ** instruction ended up not being needed, then change the OP_OpenEphemeral
  4040   4109       ** into an OP_Noop.
  4041   4110       */
  4042   4111       if( addrSortIndex>=0 && pOrderBy==0 ){
  4043   4112         sqlite3VdbeChangeToNoop(v, addrSortIndex);
  4044   4113         p->addrOpenEphm[2] = -1;
  4045   4114       }
  4046   4115   
  4047         -    if( pWInfo->eDistinct ){
  4048         -      VdbeOp *pOp;                /* No longer required OpenEphemeral instr. */
  4049         -     
  4050         -      assert( addrDistinctIndex>=0 );
  4051         -      pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
  4052         -
  4053         -      assert( isDistinct );
  4054         -      assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED 
  4055         -           || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE 
  4056         -      );
  4057         -      distinct = -1;
  4058         -      if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED ){
  4059         -        int iJump;
  4060         -        int iExpr;
  4061         -        int iFlag = ++pParse->nMem;
  4062         -        int iBase = pParse->nMem+1;
  4063         -        int iBase2 = iBase + pEList->nExpr;
  4064         -        pParse->nMem += (pEList->nExpr*2);
  4065         -
  4066         -        /* Change the OP_OpenEphemeral coded earlier to an OP_Integer. The
  4067         -        ** OP_Integer initializes the "first row" flag.  */
  4068         -        pOp->opcode = OP_Integer;
  4069         -        pOp->p1 = 1;
  4070         -        pOp->p2 = iFlag;
  4071         -
  4072         -        sqlite3ExprCodeExprList(pParse, pEList, iBase, 1);
  4073         -        iJump = sqlite3VdbeCurrentAddr(v) + 1 + pEList->nExpr + 1 + 1;
  4074         -        sqlite3VdbeAddOp2(v, OP_If, iFlag, iJump-1);
  4075         -        for(iExpr=0; iExpr<pEList->nExpr; iExpr++){
  4076         -          CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[iExpr].pExpr);
  4077         -          sqlite3VdbeAddOp3(v, OP_Ne, iBase+iExpr, iJump, iBase2+iExpr);
  4078         -          sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
  4079         -          sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
  4080         -        }
  4081         -        sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iContinue);
  4082         -
  4083         -        sqlite3VdbeAddOp2(v, OP_Integer, 0, iFlag);
  4084         -        assert( sqlite3VdbeCurrentAddr(v)==iJump );
  4085         -        sqlite3VdbeAddOp3(v, OP_Move, iBase, iBase2, pEList->nExpr);
  4086         -      }else{
  4087         -        pOp->opcode = OP_Noop;
  4088         -      }
  4089         -    }
  4090         -
  4091   4116       /* Use the standard inner loop. */
  4092         -    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, distinct, pDest,
         4117  +    selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, &sDistinct, pDest,
  4093   4118                       pWInfo->iContinue, pWInfo->iBreak);
  4094   4119   
  4095   4120       /* End the database scan loop.
  4096   4121       */
  4097   4122       sqlite3WhereEnd(pWInfo);
  4098   4123     }else{
  4099         -    /* This is the processing for aggregate queries */
         4124  +    /* This case when there exist aggregate functions or a GROUP BY clause
         4125  +    ** or both */
  4100   4126       NameContext sNC;    /* Name context for processing aggregate information */
  4101   4127       int iAMem;          /* First Mem address for storing current GROUP BY */
  4102   4128       int iBMem;          /* First Mem address for previous GROUP BY */
  4103   4129       int iUseFlag;       /* Mem address holding flag indicating that at least
  4104   4130                           ** one row of the input to the aggregator has been
  4105   4131                           ** processed */
  4106   4132       int iAbortFlag;     /* Mem address which causes query abort if positive */
................................................................................
  4200   4226   
  4201   4227         /* Begin a loop that will extract all source rows in GROUP BY order.
  4202   4228         ** This might involve two separate loops with an OP_Sort in between, or
  4203   4229         ** it might be a single loop that uses an index to extract information
  4204   4230         ** in the right order to begin with.
  4205   4231         */
  4206   4232         sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
  4207         -      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0, 0, 0);
         4233  +      pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0, 0, 0);
  4208   4234         if( pWInfo==0 ) goto select_end;
  4209         -      if( pGroupBy==0 ){
         4235  +      if( pWInfo->nOBSat==pGroupBy->nExpr ){
  4210   4236           /* The optimizer is able to deliver rows in group by order so
  4211   4237           ** we do not have to sort.  The OP_OpenEphemeral table will be
  4212   4238           ** cancelled later because we still need to use the pKeyInfo
  4213   4239           */
  4214         -        pGroupBy = p->pGroupBy;
  4215   4240           groupBySort = 0;
  4216   4241         }else{
  4217   4242           /* Rows are coming out in undetermined order.  We have to push
  4218   4243           ** each row into a sorting index, terminate the first loop,
  4219   4244           ** then loop over the sorting index in order to get the output
  4220   4245           ** in sorted order
  4221   4246           */
  4222   4247           int regBase;
  4223   4248           int regRecord;
  4224   4249           int nCol;
  4225   4250           int nGroupBy;
  4226   4251   
  4227   4252           explainTempTable(pParse, 
  4228         -            isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY");
         4253  +            (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
         4254  +                    "DISTINCT" : "GROUP BY");
  4229   4255   
  4230   4256           groupBySort = 1;
  4231   4257           nGroupBy = pGroupBy->nExpr;
  4232   4258           nCol = nGroupBy + 1;
  4233   4259           j = nGroupBy+1;
  4234   4260           for(i=0; i<sAggInfo.nColumn; i++){
  4235   4261             if( sAggInfo.aCol[i].iSorterColumn>=j ){
................................................................................
  4353   4379         addrOutputRow = sqlite3VdbeCurrentAddr(v);
  4354   4380         sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
  4355   4381         VdbeComment((v, "Groupby result generator entry point"));
  4356   4382         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  4357   4383         finalizeAggFunctions(pParse, &sAggInfo);
  4358   4384         sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
  4359   4385         selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy,
  4360         -                      distinct, pDest,
         4386  +                      &sDistinct, pDest,
  4361   4387                         addrOutputRow+1, addrSetAbort);
  4362   4388         sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
  4363   4389         VdbeComment((v, "end groupby result generator"));
  4364   4390   
  4365   4391         /* Generate a subroutine that will reset the group-by accumulator
  4366   4392         */
  4367   4393         sqlite3VdbeResolveLabel(v, addrReset);
................................................................................
  4456   4482           **     satisfying the 'ORDER BY' clause than it does in other cases.
  4457   4483           **     Refer to code and comments in where.c for details.
  4458   4484           */
  4459   4485           ExprList *pMinMax = 0;
  4460   4486           u8 flag = minMaxQuery(p);
  4461   4487           if( flag ){
  4462   4488             assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) );
         4489  +          assert( p->pEList->a[0].pExpr->x.pList->nExpr==1 );
  4463   4490             pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0);
  4464   4491             pDel = pMinMax;
  4465   4492             if( pMinMax && !db->mallocFailed ){
  4466   4493               pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
  4467   4494               pMinMax->a[0].pExpr->op = TK_COLUMN;
  4468   4495             }
  4469   4496           }
  4470   4497     
  4471   4498           /* This case runs if the aggregate has no GROUP BY clause.  The
  4472   4499           ** processing is much simpler since there is only a single row
  4473   4500           ** of output.
  4474   4501           */
  4475   4502           resetAccumulator(pParse, &sAggInfo);
  4476         -        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax,0,flag,0);
         4503  +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
  4477   4504           if( pWInfo==0 ){
  4478   4505             sqlite3ExprListDelete(db, pDel);
  4479   4506             goto select_end;
  4480   4507           }
  4481   4508           updateAccumulator(pParse, &sAggInfo);
  4482         -        if( !pMinMax && flag ){
         4509  +        assert( pMinMax==0 || pMinMax->nExpr==1 );
         4510  +        if( pWInfo->nOBSat>0 ){
  4483   4511             sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
  4484   4512             VdbeComment((v, "%s() by index",
  4485   4513                   (flag==WHERE_ORDERBY_MIN?"min":"max")));
  4486   4514           }
  4487   4515           sqlite3WhereEnd(pWInfo);
  4488   4516           finalizeAggFunctions(pParse, &sAggInfo);
  4489   4517         }
  4490   4518   
  4491   4519         pOrderBy = 0;
  4492   4520         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
  4493         -      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 
         4521  +      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0, 
  4494   4522                         pDest, addrEnd, addrEnd);
  4495   4523         sqlite3ExprListDelete(db, pDel);
  4496   4524       }
  4497   4525       sqlite3VdbeResolveLabel(v, addrEnd);
  4498   4526       
  4499   4527     } /* endif aggregate query */
  4500   4528   
  4501         -  if( distinct>=0 ){
         4529  +  if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
  4502   4530       explainTempTable(pParse, "DISTINCT");
  4503   4531     }
  4504   4532   
  4505   4533     /* If there is an ORDER BY clause, then we need to sort the results
  4506   4534     ** and send them to the callback one by one.
  4507   4535     */
  4508   4536     if( pOrderBy ){

Changes to src/shell.c.

   692    692           for(i=0; i<nArg; i++){
   693    693             int w, n;
   694    694             if( i<ArraySize(p->colWidth) ){
   695    695               w = p->colWidth[i];
   696    696             }else{
   697    697               w = 0;
   698    698             }
   699         -          if( w<=0 ){
          699  +          if( w==0 ){
   700    700               w = strlen30(azCol[i] ? azCol[i] : "");
   701    701               if( w<10 ) w = 10;
   702    702               n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue);
   703    703               if( w<n ) w = n;
   704    704             }
   705    705             if( i<ArraySize(p->actualWidth) ){
   706    706               p->actualWidth[i] = w;
   707    707             }
   708    708             if( p->showHeader ){
   709         -            fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": "  ");
          709  +            if( w<0 ){
          710  +              fprintf(p->out,"%*.*s%s",-w,-w,azCol[i], i==nArg-1 ? "\n": "  ");
          711  +            }else{
          712  +              fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": "  ");
          713  +            }
   710    714             }
   711    715           }
   712    716           if( p->showHeader ){
   713    717             for(i=0; i<nArg; i++){
   714    718               int w;
   715    719               if( i<ArraySize(p->actualWidth) ){
   716    720                  w = p->actualWidth[i];
          721  +               if( w<0 ) w = -w;
   717    722               }else{
   718    723                  w = 10;
   719    724               }
   720    725               fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
   721    726                      "----------------------------------------------------------",
   722    727                       i==nArg-1 ? "\n": "  ");
   723    728             }
................................................................................
   731    736           }else{
   732    737              w = 10;
   733    738           }
   734    739           if( p->mode==MODE_Explain && azArg[i] && 
   735    740              strlen30(azArg[i])>w ){
   736    741             w = strlen30(azArg[i]);
   737    742           }
   738         -        fprintf(p->out,"%-*.*s%s",w,w,
   739         -            azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
          743  +        if( w<0 ){
          744  +          fprintf(p->out,"%*.*s%s",-w,-w,
          745  +              azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
          746  +        }else{
          747  +          fprintf(p->out,"%-*.*s%s",w,w,
          748  +              azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": "  ");
          749  +        }
   740    750         }
   741    751         break;
   742    752       }
   743    753       case MODE_Semi:
   744    754       case MODE_List: {
   745    755         if( p->cnt++==0 && p->showHeader ){
   746    756           for(i=0; i<nArg; i++){
................................................................................
  1412   1422     "                         column   Left-aligned columns.  (See .width)\n"
  1413   1423     "                         html     HTML <table> code\n"
  1414   1424     "                         insert   SQL insert statements for TABLE\n"
  1415   1425     "                         line     One value per line\n"
  1416   1426     "                         list     Values delimited by .separator string\n"
  1417   1427     "                         tabs     Tab-separated values\n"
  1418   1428     "                         tcl      TCL list elements\n"
  1419         -  ".nullvalue STRING      Print STRING in place of NULL values\n"
         1429  +  ".nullvalue STRING      Use STRING in place of NULL values\n"
  1420   1430     ".output FILENAME       Send output to FILENAME\n"
  1421   1431     ".output stdout         Send output to the screen\n"
         1432  +  ".print STRING...       Print literal STRING\n"
  1422   1433     ".prompt MAIN CONTINUE  Replace the standard prompts\n"
  1423   1434     ".quit                  Exit this program\n"
  1424   1435     ".read FILENAME         Execute SQL in FILENAME\n"
  1425   1436     ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
  1426   1437     ".schema ?TABLE?        Show the CREATE statements\n"
  1427   1438     "                         If TABLE specified, only show tables matching\n"
  1428   1439     "                         LIKE pattern TABLE.\n"
................................................................................
  2065   2076           p->out = stdout;
  2066   2077           rc = 1;
  2067   2078         } else {
  2068   2079           sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
  2069   2080         }
  2070   2081       }
  2071   2082     }else
         2083  +
         2084  +  if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
         2085  +    int i;
         2086  +    for(i=1; i<nArg; i++){
         2087  +      if( i>1 ) fprintf(p->out, " ");
         2088  +      fprintf(p->out, "%s", azArg[i]);
         2089  +    }
         2090  +    fprintf(p->out, "\n");
         2091  +  }else
  2072   2092   
  2073   2093     if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){
  2074   2094       if( nArg >= 2) {
  2075   2095         strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
  2076   2096       }
  2077   2097       if( nArg >= 3) {
  2078   2098         strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
................................................................................
  2488   2508         sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
  2489   2509         if( zVfsName ){
  2490   2510           printf("%s\n", zVfsName);
  2491   2511           sqlite3_free(zVfsName);
  2492   2512         }
  2493   2513       }
  2494   2514     }else
         2515  +
         2516  +#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
         2517  +  if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
         2518  +    extern int sqlite3WhereTrace;
         2519  +    sqlite3WhereTrace = atoi(azArg[1]);
         2520  +  }else
         2521  +#endif
  2495   2522   
  2496   2523     if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
  2497   2524       int j;
  2498   2525       assert( nArg<=ArraySize(azArg) );
  2499   2526       for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
  2500   2527         p->colWidth[j-1] = atoi(azArg[j]);
  2501   2528       }

Changes to src/sqlite.h.in.

   469    469   #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
   470    470   #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
   471    471   #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
   472    472   #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
   473    473   #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
   474    474   #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
   475    475   #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
          476  +#define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
   476    477   #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
   477    478   #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
   478    479   #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
   479    480   #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
   480    481   
   481    482   /*
   482    483   ** CAPI3REF: Flags For File Open Operations
................................................................................
  1562   1563   ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  1563   1564   ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  1564   1565   ** connection is opened. If it is globally disabled, filenames are
  1565   1566   ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1566   1567   ** database connection is opened. By default, URI handling is globally
  1567   1568   ** disabled. The default value may be changed by compiling with the
  1568   1569   ** [SQLITE_USE_URI] symbol defined.
         1570  +**
         1571  +** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
         1572  +** <dd> This option taks a single integer argument which is interpreted as
         1573  +** a boolean in order to enable or disable the use of covering indices for
         1574  +** full table scans in the query optimizer.  The default setting is determined
         1575  +** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
         1576  +** if that compile-time option is omitted.
         1577  +** The ability to disable the use of covering indices for full table scans
         1578  +** is because some incorrectly coded legacy applications might malfunction
         1579  +** malfunction when the optimization is enabled.  Providing the ability to
         1580  +** disable the optimization allows the older, buggy application code to work
         1581  +** without change even with newer versions of SQLite.
  1569   1582   **
  1570   1583   ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
  1571   1584   ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
  1572   1585   ** <dd> These options are obsolete and should not be used by new code.
  1573   1586   ** They are retained for backwards compatibility but are now no-ops.
  1574   1587   ** </dl>
  1575   1588   */
................................................................................
  1588   1601   #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  1589   1602   #define SQLITE_CONFIG_PCACHE       14  /* no-op */
  1590   1603   #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  1591   1604   #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1592   1605   #define SQLITE_CONFIG_URI          17  /* int */
  1593   1606   #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1594   1607   #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
         1608  +#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
  1595   1609   
  1596   1610   /*
  1597   1611   ** CAPI3REF: Database Connection Configuration Options
  1598   1612   **
  1599   1613   ** These constants are the available integer configuration options that
  1600   1614   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1601   1615   **
................................................................................
  2596   2610   **     the value passed as the fourth parameter to sqlite3_open_v2().
  2597   2611   **
  2598   2612   **   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
  2599   2613   **     "rwc", or "memory". Attempting to set it to any other value is
  2600   2614   **     an error)^. 
  2601   2615   **     ^If "ro" is specified, then the database is opened for read-only 
  2602   2616   **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the 
  2603         -**     third argument to sqlite3_prepare_v2(). ^If the mode option is set to 
         2617  +**     third argument to sqlite3_open_v2(). ^If the mode option is set to 
  2604   2618   **     "rw", then the database is opened for read-write (but not create) 
  2605   2619   **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had 
  2606   2620   **     been set. ^Value "rwc" is equivalent to setting both 
  2607   2621   **     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
  2608   2622   **     set to "memory" then a pure [in-memory database] that never reads
  2609   2623   **     or writes from disk is used. ^It is an error to specify a value for
  2610   2624   **     the mode parameter that is less restrictive than that specified by
................................................................................
  2747   2761   **
  2748   2762   ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
  2749   2763   ** text that describes the error, as either UTF-8 or UTF-16 respectively.
  2750   2764   ** ^(Memory to hold the error message string is managed internally.
  2751   2765   ** The application does not need to worry about freeing the result.
  2752   2766   ** However, the error string might be overwritten or deallocated by
  2753   2767   ** subsequent calls to other SQLite interface functions.)^
         2768  +**
         2769  +** ^The sqlite3_errstr() interface returns the English-language text
         2770  +** that describes the [result code], as UTF-8.
         2771  +** ^(Memory to hold the error message string is managed internally
         2772  +** and must not be freed by the application)^.
  2754   2773   **
  2755   2774   ** When the serialized [threading mode] is in use, it might be the
  2756   2775   ** case that a second error occurs on a separate thread in between
  2757   2776   ** the time of the first error and the call to these interfaces.
  2758   2777   ** When that happens, the second error will be reported since these
  2759   2778   ** interfaces always report the most recent result.  To avoid
  2760   2779   ** this, each thread can obtain exclusive use of the [database connection] D
................................................................................
  2766   2785   ** was invoked incorrectly by the application.  In that case, the
  2767   2786   ** error code and message may or may not be set.
  2768   2787   */
  2769   2788   int sqlite3_errcode(sqlite3 *db);
  2770   2789   int sqlite3_extended_errcode(sqlite3 *db);
  2771   2790   const char *sqlite3_errmsg(sqlite3*);
  2772   2791   const void *sqlite3_errmsg16(sqlite3*);
         2792  +const char *sqlite3_errstr(int);
  2773   2793   
  2774   2794   /*
  2775   2795   ** CAPI3REF: SQL Statement Object
  2776   2796   ** KEYWORDS: {prepared statement} {prepared statements}
  2777   2797   **
  2778   2798   ** An instance of this object represents a single SQL statement.
  2779   2799   ** This object is variously known as a "prepared statement" or a
................................................................................
  4732   4752   ** future releases of SQLite.  Applications that care about shared
  4733   4753   ** cache setting should set it explicitly.
  4734   4754   **
  4735   4755   ** ^Note: This method is deprecated on MacOS X 10.7 and iOS version 5.0
  4736   4756   ** and will always return SQLITE_MISUSE, instead of calling this function
  4737   4757   ** shared cache mode should be enabled per-database connection via 
  4738   4758   ** sqlite3_open_v2 with SQLITE_OPEN_SHAREDCACHE instead.
         4759  +**
         4760  +** This interface is threadsafe on processors where writing a
         4761  +** 32-bit integer is atomic.
  4739   4762   **
  4740   4763   ** See Also:  [SQLite Shared-Cache Mode]
  4741   4764   */
  4742   4765   int sqlite3_enable_shared_cache(int);
  4743   4766   
  4744   4767   /*
  4745   4768   ** CAPI3REF: Attempt To Free Heap Memory

Added src/sqlite3.rc.

            1  +/*
            2  +** 2012 September 2
            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 code and resources that are specific to Windows.
           14  +*/
           15  +
           16  +#if !defined(_WIN32_WCE)
           17  +#include "winresrc.h"
           18  +#else
           19  +#include "windows.h"
           20  +#endif
           21  +
           22  +#include "sqlite3.h"
           23  +#include "sqlite3rc.h"
           24  +
           25  +/*
           26  + * English (U.S.) resources
           27  + */
           28  +
           29  +#ifdef _WIN32
           30  +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
           31  +#pragma code_page(1252)
           32  +#endif /* _WIN32 */
           33  +
           34  +/*
           35  + * Version
           36  + */
           37  +
           38  +VS_VERSION_INFO VERSIONINFO
           39  +  FILEVERSION SQLITE_RESOURCE_VERSION
           40  +  PRODUCTVERSION SQLITE_RESOURCE_VERSION
           41  +  FILEFLAGSMASK 0x3F
           42  +#if defined(_DEBUG)
           43  +  FILEFLAGS 0x1L
           44  +#else
           45  +  FILEFLAGS 0x0L
           46  +#endif
           47  +  FILEOS VOS__WINDOWS32
           48  +  FILETYPE VFT_APP
           49  +  FILESUBTYPE VFT2_UNKNOWN
           50  +BEGIN
           51  +  BLOCK "StringFileInfo"
           52  +  BEGIN
           53  +    BLOCK "040904b0"
           54  +    BEGIN
           55  +      VALUE "CompanyName", "SQLite Development Team"
           56  +      VALUE "FileDescription", "SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine."
           57  +      VALUE "FileVersion", SQLITE_VERSION
           58  +      VALUE "InternalName", "sqlite3"
           59  +      VALUE "LegalCopyright", "http://www.sqlite.org/copyright.html"
           60  +      VALUE "ProductName", "SQLite"
           61  +      VALUE "ProductVersion", SQLITE_VERSION
           62  +      VALUE "SourceId", SQLITE_SOURCE_ID
           63  +    END
           64  +  END
           65  +  BLOCK "VarFileInfo"
           66  +  BEGIN
           67  +    VALUE "Translation", 0x409, 1200
           68  +  END
           69  +END

Changes to src/sqliteInt.h.

   657    657   typedef struct LookasideSlot LookasideSlot;
   658    658   typedef struct Module Module;
   659    659   typedef struct NameContext NameContext;
   660    660   typedef struct Parse Parse;
   661    661   typedef struct RowSet RowSet;
   662    662   typedef struct Savepoint Savepoint;
   663    663   typedef struct Select Select;
          664  +typedef struct SelectDest SelectDest;
   664    665   typedef struct SrcList SrcList;
   665    666   typedef struct StrAccum StrAccum;
   666    667   typedef struct Table Table;
   667    668   typedef struct TableLock TableLock;
   668    669   typedef struct Token Token;
   669    670   typedef struct Trigger Trigger;
   670    671   typedef struct TriggerPrg TriggerPrg;
................................................................................
   832    833     Db *aDb;                      /* All backends */
   833    834     int nDb;                      /* Number of backends currently in use */
   834    835     int flags;                    /* Miscellaneous flags. See below */
   835    836     i64 lastRowid;                /* ROWID of most recent insert (see above) */
   836    837     unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
   837    838     int errCode;                  /* Most recent error code (SQLITE_*) */
   838    839     int errMask;                  /* & result codes with this before returning */
          840  +  u8 dbOptFlags;                /* Flags to enable/disable optimizations */
   839    841     u8 autoCommit;                /* The auto-commit flag. */
   840    842     u8 temp_store;                /* 1: file 2: memory 0: default */
   841    843     u8 mallocFailed;              /* True if we have seen a malloc failure */
   842    844     u8 dfltLockMode;              /* Default locking-mode for attached dbs */
   843    845     signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
   844    846     u8 suppressErr;               /* Do not issue error messages if true */
   845    847     u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
................................................................................
   936    938   ** A macro to discover the encoding of a database.
   937    939   */
   938    940   #define ENC(db) ((db)->aDb[0].pSchema->enc)
   939    941   
   940    942   /*
   941    943   ** Possible values for the sqlite3.flags.
   942    944   */
   943         -#define SQLITE_VdbeTrace      0x00000100  /* True to trace VDBE execution */
   944         -#define SQLITE_InternChanges  0x00000200  /* Uncommitted Hash table changes */
   945         -#define SQLITE_FullColNames   0x00000400  /* Show full column names on SELECT */
   946         -#define SQLITE_ShortColNames  0x00000800  /* Show short columns names */
   947         -#define SQLITE_CountRows      0x00001000  /* Count rows changed by INSERT, */
          945  +#define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
          946  +#define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
          947  +#define SQLITE_FullColNames   0x00000004  /* Show full column names on SELECT */
          948  +#define SQLITE_ShortColNames  0x00000008  /* Show short columns names */
          949  +#define SQLITE_CountRows      0x00000010  /* Count rows changed by INSERT, */
   948    950                                             /*   DELETE, or UPDATE and return */
   949    951                                             /*   the count using a callback. */
   950         -#define SQLITE_NullCallback   0x00002000  /* Invoke the callback once if the */
          952  +#define SQLITE_NullCallback   0x00000020  /* Invoke the callback once if the */
   951    953                                             /*   result set is empty */
   952         -#define SQLITE_SqlTrace       0x00004000  /* Debug print SQL as it executes */
   953         -#define SQLITE_VdbeListing    0x00008000  /* Debug listings of VDBE programs */
   954         -#define SQLITE_WriteSchema    0x00010000  /* OK to update SQLITE_MASTER */
   955         -                         /*   0x00020000  Unused */
   956         -#define SQLITE_IgnoreChecks   0x00040000  /* Do not enforce check constraints */
   957         -#define SQLITE_ReadUncommitted 0x0080000  /* For shared-cache mode */
   958         -#define SQLITE_LegacyFileFmt  0x00100000  /* Create new databases in format 1 */
   959         -#define SQLITE_FullFSync      0x00200000  /* Use full fsync on the backend */
   960         -#define SQLITE_CkptFullFSync  0x00400000  /* Use full fsync for checkpoint */
   961         -#define SQLITE_RecoveryMode   0x00800000  /* Ignore schema errors */
   962         -#define SQLITE_ReverseOrder   0x01000000  /* Reverse unordered SELECTs */
   963         -#define SQLITE_RecTriggers    0x02000000  /* Enable recursive triggers */
   964         -#define SQLITE_ForeignKeys    0x04000000  /* Enforce foreign key constraints  */
   965         -#define SQLITE_AutoIndex      0x08000000  /* Enable automatic indexes */
   966         -#define SQLITE_PreferBuiltin  0x10000000  /* Preference to built-in funcs */
   967         -#define SQLITE_LoadExtension  0x20000000  /* Enable load_extension */
   968         -#define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
   969         -
   970         -/*
   971         -** Bits of the sqlite3.flags field that are used by the
   972         -** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
   973         -** These must be the low-order bits of the flags field.
   974         -*/
   975         -#define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
   976         -#define SQLITE_ColumnCache    0x02        /* Disable the column cache */
   977         -#define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
   978         -#define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
   979         -#define SQLITE_IndexCover     0x10        /* Disable index covering table */
   980         -#define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
   981         -#define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
   982         -#define SQLITE_IdxRealAsInt   0x80        /* Store REAL as INT in indices */
   983         -#define SQLITE_DistinctOpt    0x80        /* DISTINCT using indexes */
   984         -#define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
          954  +#define SQLITE_SqlTrace       0x00000040  /* Debug print SQL as it executes */
          955  +#define SQLITE_VdbeListing    0x00000080  /* Debug listings of VDBE programs */
          956  +#define SQLITE_WriteSchema    0x00000100  /* OK to update SQLITE_MASTER */
          957  +                         /*   0x00000200  Unused */
          958  +#define SQLITE_IgnoreChecks   0x00000400  /* Do not enforce check constraints */
          959  +#define SQLITE_ReadUncommitted 0x0000800  /* For shared-cache mode */
          960  +#define SQLITE_LegacyFileFmt  0x00001000  /* Create new databases in format 1 */
          961  +#define SQLITE_FullFSync      0x00002000  /* Use full fsync on the backend */
          962  +#define SQLITE_CkptFullFSync  0x00004000  /* Use full fsync for checkpoint */
          963  +#define SQLITE_RecoveryMode   0x00008000  /* Ignore schema errors */
          964  +#define SQLITE_ReverseOrder   0x00010000  /* Reverse unordered SELECTs */
          965  +#define SQLITE_RecTriggers    0x00020000  /* Enable recursive triggers */
          966  +#define SQLITE_ForeignKeys    0x00040000  /* Enforce foreign key constraints  */
          967  +#define SQLITE_AutoIndex      0x00080000  /* Enable automatic indexes */
          968  +#define SQLITE_PreferBuiltin  0x00100000  /* Preference to built-in funcs */
          969  +#define SQLITE_LoadExtension  0x00200000  /* Enable load_extension */
          970  +#define SQLITE_EnableTrigger  0x00400000  /* True to enable triggers */
          971  +
          972  +/*
          973  +** Bits of the sqlite3.dbOptFlags field that are used by the
          974  +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
          975  +** selectively disable various optimizations.
          976  +*/
          977  +#define SQLITE_QueryFlattener 0x0001   /* Query flattening */
          978  +#define SQLITE_ColumnCache    0x0002   /* Column cache */
          979  +#define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
          980  +#define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
          981  +#define SQLITE_IdxRealAsInt   0x0010   /* Store REAL as INT in indices */
          982  +#define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
          983  +#define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
          984  +#define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
          985  +#define SQLITE_AllOpts        0x00ff   /* All optimizations */
          986  +
          987  +/*
          988  +** Macros for testing whether or not optimizations are enabled or disabled.
          989  +*/
          990  +#ifndef SQLITE_OMIT_BUILTIN_TEST
          991  +#define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
          992  +#define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
          993  +#else
          994  +#define OptimizationDisabled(db, mask)  0
          995  +#define OptimizationEnabled(db, mask)   1
          996  +#endif
   985    997   
   986    998   /*
   987    999   ** Possible values for the sqlite.magic field.
   988   1000   ** The numbers are obtained at random and have no special meaning, other
   989   1001   ** than being distinct from one another.
   990   1002   */
   991   1003   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
................................................................................
  1128   1140   */
  1129   1141   struct Column {
  1130   1142     char *zName;     /* Name of this column */
  1131   1143     Expr *pDflt;     /* Default value of this column */
  1132   1144     char *zDflt;     /* Original text of the default value */
  1133   1145     char *zType;     /* Data type for this column */
  1134   1146     char *zColl;     /* Collating sequence.  If NULL, use the default */
  1135         -  u8 notNull;      /* True if there is a NOT NULL constraint */
  1136         -  u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
         1147  +  u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
  1137   1148     char affinity;   /* One of the SQLITE_AFF_... values */
  1138         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  1139         -  u8 isHidden;     /* True if this column is 'hidden' */
  1140         -#endif
         1149  +  u16 colFlags;    /* Boolean properties.  See COLFLAG_ defines below */
  1141   1150   };
  1142   1151   
         1152  +/* Allowed values for Column.colFlags:
         1153  +*/
         1154  +#define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
         1155  +#define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
         1156  +
  1143   1157   /*
  1144   1158   ** A "Collating Sequence" is defined by an instance of the following
  1145   1159   ** structure. Conceptually, a collating sequence consists of a name and
  1146   1160   ** a comparison routine that defines the order of that sequence.
  1147   1161   **
  1148   1162   ** There may two separate implementations of the collation function, one
  1149   1163   ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
................................................................................
  1291   1305   ** refers VDBE cursor number that holds the table open, not to the root
  1292   1306   ** page number.  Transient tables are used to hold the results of a
  1293   1307   ** sub-query that appears instead of a real table name in the FROM clause 
  1294   1308   ** of a SELECT statement.
  1295   1309   */
  1296   1310   struct Table {
  1297   1311     char *zName;         /* Name of the table or view */
  1298         -  int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
  1299         -  int nCol;            /* Number of columns in this table */
  1300   1312     Column *aCol;        /* Information about each column */
  1301   1313     Index *pIndex;       /* List of SQL indexes on this table. */
  1302         -  int tnum;            /* Root BTree node for this table (see note above) */
  1303         -  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
  1304   1314     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
  1305         -  u16 nRef;            /* Number of pointers to this Table */
  1306         -  u8 tabFlags;         /* Mask of TF_* values */
  1307         -  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1308   1315     FKey *pFKey;         /* Linked list of all foreign keys in this table */
  1309   1316     char *zColAff;       /* String defining the affinity of each column */
  1310   1317   #ifndef SQLITE_OMIT_CHECK
  1311   1318     ExprList *pCheck;    /* All CHECK constraints */
  1312   1319   #endif
         1320  +  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
         1321  +  int tnum;            /* Root BTree node for this table (see note above) */
         1322  +  i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
         1323  +  i16 nCol;            /* Number of columns in this table */
         1324  +  u16 nRef;            /* Number of pointers to this Table */
         1325  +  u8 tabFlags;         /* Mask of TF_* values */
         1326  +  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1313   1327   #ifndef SQLITE_OMIT_ALTERTABLE
  1314   1328     int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
  1315   1329   #endif
  1316   1330   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1317         -  VTable *pVTable;     /* List of VTable objects. */
  1318   1331     int nModuleArg;      /* Number of arguments to the module */
  1319   1332     char **azModuleArg;  /* Text of all module args. [0] is module name */
         1333  +  VTable *pVTable;     /* List of VTable objects. */
  1320   1334   #endif
  1321   1335     Trigger *pTrigger;   /* List of triggers stored in pSchema */
  1322   1336     Schema *pSchema;     /* Schema that contains this table */
  1323   1337     Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
  1324   1338   };
  1325   1339   
  1326   1340   /*
................................................................................
  1336   1350   /*
  1337   1351   ** Test to see whether or not a table is a virtual table.  This is
  1338   1352   ** done as a macro so that it will be optimized out when virtual
  1339   1353   ** table support is omitted from the build.
  1340   1354   */
  1341   1355   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1342   1356   #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
  1343         -#  define IsHiddenColumn(X) ((X)->isHidden)
         1357  +#  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
  1344   1358   #else
  1345   1359   #  define IsVirtual(X)      0
  1346   1360   #  define IsHiddenColumn(X) 0
  1347   1361   #endif
  1348   1362   
  1349   1363   /*
  1350   1364   ** Each foreign key constraint is an instance of the following structure.
................................................................................
  1688   1702     CollSeq *pColl;        /* The collation type of the column or 0 */
  1689   1703   
  1690   1704     /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  1691   1705     ** space is allocated for the fields below this point. An attempt to
  1692   1706     ** access them will result in a segfault or malfunction.
  1693   1707     *********************************************************************/
  1694   1708   
         1709  +#if SQLITE_MAX_EXPR_DEPTH>0
         1710  +  int nHeight;           /* Height of the tree headed by this node */
         1711  +#endif
  1695   1712     int iTable;            /* TK_COLUMN: cursor number of table holding column
  1696   1713                            ** TK_REGISTER: register number
  1697   1714                            ** TK_TRIGGER: 1 -> new, 0 -> old */
  1698   1715     ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
  1699   1716                            ** TK_VARIABLE: variable number (always >= 1). */
  1700   1717     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  1701   1718     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
  1702   1719     u8 flags2;             /* Second set of flags.  EP2_... */
  1703   1720     u8 op2;                /* TK_REGISTER: original value of Expr.op
  1704   1721                            ** TK_COLUMN: the value of p5 for OP_Column
  1705   1722                            ** TK_AGG_FUNCTION: nesting depth */
  1706   1723     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  1707   1724     Table *pTab;           /* Table for TK_COLUMN expressions. */
  1708         -#if SQLITE_MAX_EXPR_DEPTH>0
  1709         -  int nHeight;           /* Height of the tree headed by this node */
  1710         -#endif
  1711   1725   };
  1712   1726   
  1713   1727   /*
  1714   1728   ** The following are the meanings of bits in the Expr.flags field.
  1715   1729   */
  1716   1730   #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
  1717   1731   #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
................................................................................
  1911   1925   ** Within the union, pIdx is only used when wsFlags&WHERE_INDEXED is true.
  1912   1926   ** pTerm is only used when wsFlags&WHERE_MULTI_OR is true.  And pVtabIdx
  1913   1927   ** is only used when wsFlags&WHERE_VIRTUALTABLE is true.  It is never the
  1914   1928   ** case that more than one of these conditions is true.
  1915   1929   */
  1916   1930   struct WherePlan {
  1917   1931     u32 wsFlags;                   /* WHERE_* flags that describe the strategy */
  1918         -  u32 nEq;                       /* Number of == constraints */
         1932  +  u16 nEq;                       /* Number of == constraints */
         1933  +  u16 nOBSat;                    /* Number of ORDER BY terms satisfied */
  1919   1934     double nRow;                   /* Estimated number of rows (for EQP) */
  1920   1935     union {
  1921   1936       Index *pIdx;                   /* Index when WHERE_INDEXED is true */
  1922   1937       struct WhereTerm *pTerm;       /* WHERE clause term for OR-search */
  1923   1938       sqlite3_index_info *pVtabIdx;  /* Virtual table index to use */
  1924   1939     } u;
  1925   1940   };
................................................................................
  1987   2002   ** The WHERE clause processing routine has two halves.  The
  1988   2003   ** first part does the start of the WHERE loop and the second
  1989   2004   ** half does the tail of the WHERE loop.  An instance of
  1990   2005   ** this structure is returned by the first half and passed
  1991   2006   ** into the second half to give some continuity.
  1992   2007   */
  1993   2008   struct WhereInfo {
  1994         -  Parse *pParse;       /* Parsing and code generating context */
  1995         -  u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
  1996         -  u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
  1997         -  u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
  1998         -  u8 eDistinct;
  1999         -  SrcList *pTabList;             /* List of tables in the join */
  2000         -  int iTop;                      /* The very beginning of the WHERE loop */
  2001         -  int iContinue;                 /* Jump here to continue with next record */
  2002         -  int iBreak;                    /* Jump here to break out of the loop */
  2003         -  int nLevel;                    /* Number of nested loop */
  2004         -  struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
  2005         -  double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
  2006         -  double nRowOut;                /* Estimated number of output rows */
  2007         -  WhereLevel a[1];               /* Information about each nest loop in WHERE */
         2009  +  Parse *pParse;            /* Parsing and code generating context */
         2010  +  SrcList *pTabList;        /* List of tables in the join */
         2011  +  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
         2012  +  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
         2013  +  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
         2014  +  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
         2015  +  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
         2016  +  int iTop;                 /* The very beginning of the WHERE loop */
         2017  +  int iContinue;            /* Jump here to continue with next record */
         2018  +  int iBreak;               /* Jump here to break out of the loop */
         2019  +  int nLevel;               /* Number of nested loop */
         2020  +  struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
         2021  +  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
         2022  +  double nRowOut;           /* Estimated number of output rows */
         2023  +  WhereLevel a[1];          /* Information about each nest loop in WHERE */
  2008   2024   };
  2009   2025   
  2010         -#define WHERE_DISTINCT_UNIQUE 1
  2011         -#define WHERE_DISTINCT_ORDERED 2
         2026  +/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
         2027  +#define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
         2028  +#define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
         2029  +#define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
         2030  +#define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
  2012   2031   
  2013   2032   /*
  2014   2033   ** A NameContext defines a context in which to resolve table and column
  2015   2034   ** names.  The context consists of a list of tables (the pSrcList) field and
  2016   2035   ** a list of named expression (pEList).  The named expression list may
  2017   2036   ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
  2018   2037   ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
................................................................................
  2063   2082   ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
  2064   2083   ** These addresses must be stored so that we can go back and fill in
  2065   2084   ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
  2066   2085   ** the number of columns in P2 can be computed at the same time
  2067   2086   ** as the OP_OpenEphm instruction is coded because not
  2068   2087   ** enough information about the compound query is known at that point.
  2069   2088   ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
  2070         -** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
         2089  +** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
  2071   2090   ** sequences for the ORDER BY clause.
  2072   2091   */
  2073   2092   struct Select {
  2074   2093     ExprList *pEList;      /* The fields of the result */
  2075   2094     u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  2076         -  char affinity;         /* MakeRecord with this affinity for SRT_Set */
  2077   2095     u16 selFlags;          /* Various SF_* values */
  2078   2096     int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  2079   2097     int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
  2080   2098     double nSelectRow;     /* Estimated number of result rows */
  2081   2099     SrcList *pSrc;         /* The FROM clause */
  2082   2100     Expr *pWhere;          /* The WHERE clause */
  2083   2101     ExprList *pGroupBy;    /* The GROUP BY clause */
................................................................................
  2120   2138   #define SRT_Mem          6  /* Store result in a memory cell */
  2121   2139   #define SRT_Set          7  /* Store results as keys in an index */
  2122   2140   #define SRT_Table        8  /* Store result as data with an automatic rowid */
  2123   2141   #define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
  2124   2142   #define SRT_Coroutine   10  /* Generate a single row of result */
  2125   2143   
  2126   2144   /*
  2127         -** A structure used to customize the behavior of sqlite3Select(). See
  2128         -** comments above sqlite3Select() for details.
         2145  +** An instance of this object describes where to put of the results of
         2146  +** a SELECT statement.
  2129   2147   */
  2130         -typedef struct SelectDest SelectDest;
  2131   2148   struct SelectDest {
  2132         -  u8 eDest;         /* How to dispose of the results */
  2133         -  u8 affSdst;       /* Affinity used when eDest==SRT_Set */
         2149  +  u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
         2150  +  char affSdst;     /* Affinity used when eDest==SRT_Set */
  2134   2151     int iSDParm;      /* A parameter used by the eDest disposal method */
  2135   2152     int iSdst;        /* Base register where results are written */
  2136   2153     int nSdst;        /* Number of registers allocated */
  2137   2154   };
  2138   2155   
  2139   2156   /*
  2140   2157   ** During code generation of statements that do inserts into AUTOINCREMENT 
................................................................................
  2468   2485   ** This structure also contains some state information.
  2469   2486   */
  2470   2487   struct Sqlite3Config {
  2471   2488     int bMemstat;                     /* True to enable memory status */
  2472   2489     int bCoreMutex;                   /* True to enable core mutexing */
  2473   2490     int bFullMutex;                   /* True to enable full mutexing */
  2474   2491     int bOpenUri;                     /* True to interpret filenames as URIs */
         2492  +  int bUseCis;                      /* Use covering indices for full-scans */
  2475   2493     int mxStrlen;                     /* Maximum string length */
  2476   2494     int szLookaside;                  /* Default lookaside buffer size */
  2477   2495     int nLookaside;                   /* Default lookaside buffer count */
  2478   2496     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  2479   2497     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
  2480   2498     sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
  2481   2499     void *pHeap;                      /* Heap storage space */
................................................................................
  2784   2802   #ifndef SQLITE_OMIT_AUTOINCREMENT
  2785   2803     void sqlite3AutoincrementBegin(Parse *pParse);
  2786   2804     void sqlite3AutoincrementEnd(Parse *pParse);
  2787   2805   #else
  2788   2806   # define sqlite3AutoincrementBegin(X)
  2789   2807   # define sqlite3AutoincrementEnd(X)
  2790   2808   #endif
         2809  +int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
  2791   2810   void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
  2792   2811   void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  2793   2812   IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  2794   2813   int sqlite3IdListIndex(IdList*,const char*);
  2795   2814   SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
  2796   2815   SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
  2797   2816   SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
................................................................................
  2813   2832   int sqlite3IsReadOnly(Parse*, Table*, int);
  2814   2833   void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
  2815   2834   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
  2816   2835   Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
  2817   2836   #endif
  2818   2837   void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  2819   2838   void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  2820         -WhereInfo *sqlite3WhereBegin(
  2821         -    Parse*,SrcList*,Expr*,ExprList**,ExprList*,u16,int);
         2839  +WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  2822   2840   void sqlite3WhereEnd(WhereInfo*);
  2823   2841   int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  2824   2842   void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  2825   2843   void sqlite3ExprCodeMove(Parse*, int, int, int);
  2826         -void sqlite3ExprCodeCopy(Parse*, int, int, int);
  2827   2844   void sqlite3ExprCacheStore(Parse*, int, int, int);
  2828   2845   void sqlite3ExprCachePush(Parse*);
  2829   2846   void sqlite3ExprCachePop(Parse*, int);
  2830   2847   void sqlite3ExprCacheRemove(Parse*, int, int);
  2831   2848   void sqlite3ExprCacheClear(Parse*);
  2832   2849   void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  2833   2850   int sqlite3ExprCode(Parse*, Expr*, int);
................................................................................
  2959   2976   int sqlite3FixExprList(DbFixer*, ExprList*);
  2960   2977   int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
  2961   2978   int sqlite3AtoF(const char *z, double*, int, u8);
  2962   2979   int sqlite3GetInt32(const char *, int*);
  2963   2980   int sqlite3Atoi(const char*);
  2964   2981   int sqlite3Utf16ByteLen(const void *pData, int nChar);
  2965   2982   int sqlite3Utf8CharLen(const char *pData, int nByte);
  2966         -u32 sqlite3Utf8Read(const u8*, const u8**);
         2983  +u32 sqlite3Utf8Read(const u8**);
  2967   2984   
  2968   2985   /*
  2969   2986   ** Routines to read and write variable-length integers.  These used to
  2970   2987   ** be defined locally, but now we use the varint routines in the util.c
  2971   2988   ** file.  Code should use the MACRO forms below, as the Varint32 versions
  2972   2989   ** are coded to assume the single byte case is already handled (which 
  2973   2990   ** the MACRO form does).

Changes to src/tclsqlite.c.

  2554   2554         Tcl_WrongNumArgs(interp, 2, objv, "KEY");
  2555   2555         return TCL_ERROR;
  2556   2556       }
  2557   2557   #ifdef SQLITE_HAS_CODEC
  2558   2558       pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
  2559   2559       rc = sqlite3_rekey(pDb->db, pKey, nKey);
  2560   2560       if( rc ){
  2561         -      Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
         2561  +      Tcl_AppendResult(interp, sqlite3_errstr(rc), 0);
  2562   2562         rc = TCL_ERROR;
  2563   2563       }
  2564   2564   #endif
  2565   2565       break;
  2566   2566     }
  2567   2567   
  2568   2568     /*    $db restore ?DATABASE? FILENAME
................................................................................
  2925   2925     const char *zVfs = 0;
  2926   2926     int flags;
  2927   2927     Tcl_DString translatedFilename;
  2928   2928   #ifdef SQLITE_HAS_CODEC
  2929   2929     void *pKey = 0;
  2930   2930     int nKey = 0;
  2931   2931   #endif
         2932  +  int rc;
  2932   2933   
  2933   2934     /* In normal use, each TCL interpreter runs in a single thread.  So
  2934   2935     ** by default, we can turn of mutexing on SQLite database connections.
  2935   2936     ** However, for testing purposes it is useful to have mutexes turned
  2936   2937     ** on.  So, by default, mutexes default off.  But if compiled with
  2937   2938     ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on.
  2938   2939     */
................................................................................
  3029   3030     if( p==0 ){
  3030   3031       Tcl_SetResult(interp, "malloc failed", TCL_STATIC);
  3031   3032       return TCL_ERROR;
  3032   3033     }
  3033   3034     memset(p, 0, sizeof(*p));
  3034   3035     zFile = Tcl_GetStringFromObj(objv[2], 0);
  3035   3036     zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
  3036         -  sqlite3_open_v2(zFile, &p->db, flags, zVfs);
         3037  +  rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs);
  3037   3038     Tcl_DStringFree(&translatedFilename);
  3038         -  if( SQLITE_OK!=sqlite3_errcode(p->db) ){
  3039         -    zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
  3040         -    sqlite3_close(p->db);
  3041         -    p->db = 0;
         3039  +  if( p->db ){
         3040  +    if( SQLITE_OK!=sqlite3_errcode(p->db) ){
         3041  +      zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
         3042  +      sqlite3_close(p->db);
         3043  +      p->db = 0;
         3044  +    }
         3045  +  }else{
         3046  +    zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc));
  3042   3047     }
  3043   3048   #ifdef SQLITE_HAS_CODEC
  3044   3049     if( p->db ){
  3045   3050       sqlite3_key(p->db, pKey, nKey);
  3046   3051     }
  3047   3052   #endif
  3048   3053     if( p->db==0 ){

Changes to src/test1.c.

  6127   6127     const char *zOpt;
  6128   6128     int onoff;
  6129   6129     int mask = 0;
  6130   6130     static const struct {
  6131   6131       const char *zOptName;
  6132   6132       int mask;
  6133   6133     } aOpt[] = {
  6134         -    { "all",              SQLITE_OptMask        },
         6134  +    { "all",              SQLITE_AllOpts        },
  6135   6135       { "query-flattener",  SQLITE_QueryFlattener },
  6136   6136       { "column-cache",     SQLITE_ColumnCache    },
  6137         -    { "index-sort",       SQLITE_IndexSort      },
  6138         -    { "index-search",     SQLITE_IndexSearch    },
  6139         -    { "index-cover",      SQLITE_IndexCover     },
  6140   6137       { "groupby-order",    SQLITE_GroupByOrder   },
  6141   6138       { "factor-constants", SQLITE_FactorOutConst },
  6142   6139       { "real-as-int",      SQLITE_IdxRealAsInt   },
         6140  +    { "distinct-opt",     SQLITE_DistinctOpt    },
         6141  +    { "cover-idx-scan",   SQLITE_CoverIdxScan   },
         6142  +    { "order-by-idx-join",SQLITE_OrderByIdxJoin },
  6143   6143     };
  6144   6144   
  6145   6145     if( objc!=4 ){
  6146   6146       Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
  6147   6147       return TCL_ERROR;
  6148   6148     }
  6149   6149     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;

Changes to src/test_malloc.c.

  1090   1090       return TCL_ERROR;
  1091   1091     }
  1092   1092     Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  1093   1093     return TCL_OK;
  1094   1094   }
  1095   1095   
  1096   1096   /*
  1097         -** Usage:
  1098         -**
  1099         -**   sqlite3_config_heap NBYTE NMINALLOC
         1097  +** Usage:    sqlite3_config_heap NBYTE NMINALLOC
  1100   1098   */
  1101   1099   static int test_config_heap(
  1102   1100     void * clientData, 
  1103   1101     Tcl_Interp *interp,
  1104   1102     int objc,
  1105   1103     Tcl_Obj *CONST objv[]
  1106   1104   ){
................................................................................
  1129   1127     }
  1130   1128   
  1131   1129     Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
  1132   1130     return TCL_OK;
  1133   1131   }
  1134   1132   
  1135   1133   /*
  1136         -** tclcmd:     sqlite3_config_error  [DB]
         1134  +** Usage:    sqlite3_config_error  [DB]
  1137   1135   **
  1138   1136   ** Invoke sqlite3_config() or sqlite3_db_config() with invalid
  1139   1137   ** opcodes and verify that they return errors.
  1140   1138   */
  1141   1139   static int test_config_error(
  1142   1140     void * clientData, 
  1143   1141     Tcl_Interp *interp,
................................................................................
  1167   1165         return TCL_ERROR;
  1168   1166       }
  1169   1167     }
  1170   1168     return TCL_OK;
  1171   1169   }
  1172   1170   
  1173   1171   /*
  1174         -** tclcmd:     sqlite3_config_uri  BOOLEAN
         1172  +** Usage:    sqlite3_config_uri  BOOLEAN
  1175   1173   **
  1176         -** Invoke sqlite3_config() or sqlite3_db_config() with invalid
  1177         -** opcodes and verify that they return errors.
         1174  +** Enables or disables interpretation of URI parameters by default using
         1175  +** SQLITE_CONFIG_URI.
  1178   1176   */
  1179   1177   static int test_config_uri(
  1180   1178     void * clientData, 
  1181   1179     Tcl_Interp *interp,
  1182   1180     int objc,
  1183   1181     Tcl_Obj *CONST objv[]
  1184   1182   ){
................................................................................
  1196   1194     rc = sqlite3_config(SQLITE_CONFIG_URI, bOpenUri);
  1197   1195     Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
  1198   1196   
  1199   1197     return TCL_OK;
  1200   1198   }
  1201   1199   
  1202   1200   /*
  1203         -** Usage:    
         1201  +** Usage:    sqlite3_config_cis  BOOLEAN
  1204   1202   **
  1205         -**   sqlite3_dump_memsys3  FILENAME
  1206         -**   sqlite3_dump_memsys5  FILENAME
         1203  +** Enables or disables the use of the covering-index scan optimization.
         1204  +** SQLITE_CONFIG_COVERING_INDEX_SCAN.
         1205  +*/
         1206  +static int test_config_cis(
         1207  +  void * clientData, 
         1208  +  Tcl_Interp *interp,
         1209  +  int objc,
         1210  +  Tcl_Obj *CONST objv[]
         1211  +){
         1212  +  int rc;
         1213  +  int bUseCis;
         1214  +
         1215  +  if( objc!=2 ){
         1216  +    Tcl_WrongNumArgs(interp, 1, objv, "BOOL");
         1217  +    return TCL_ERROR;
         1218  +  }
         1219  +  if( Tcl_GetBooleanFromObj(interp, objv[1], &bUseCis) ){
         1220  +    return TCL_ERROR;
         1221  +  }
         1222  +
         1223  +  rc = sqlite3_config(SQLITE_CONFIG_COVERING_INDEX_SCAN, bUseCis);
         1224  +  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
         1225  +
         1226  +  return TCL_OK;
         1227  +}
         1228  +
         1229  +/*
         1230  +** Usage:    sqlite3_dump_memsys3  FILENAME
         1231  +**           sqlite3_dump_memsys5  FILENAME
  1207   1232   **
  1208   1233   ** Write a summary of unfreed memsys3 allocations to FILENAME.
  1209   1234   */
  1210   1235   static int test_dump_memsys3(
  1211   1236     void * clientData,
  1212   1237     Tcl_Interp *interp,
  1213   1238     int objc,
................................................................................
  1447   1472        { "sqlite3_db_status",          test_db_status                ,0 },
  1448   1473        { "install_malloc_faultsim",    test_install_malloc_faultsim  ,0 },
  1449   1474        { "sqlite3_config_heap",        test_config_heap              ,0 },
  1450   1475        { "sqlite3_config_memstatus",   test_config_memstatus         ,0 },
  1451   1476        { "sqlite3_config_lookaside",   test_config_lookaside         ,0 },
  1452   1477        { "sqlite3_config_error",       test_config_error             ,0 },
  1453   1478        { "sqlite3_config_uri",         test_config_uri               ,0 },
         1479  +     { "sqlite3_config_cis",         test_config_cis               ,0 },
  1454   1480        { "sqlite3_db_config_lookaside",test_db_config_lookaside      ,0 },
  1455   1481        { "sqlite3_dump_memsys3",       test_dump_memsys3             ,3 },
  1456   1482        { "sqlite3_dump_memsys5",       test_dump_memsys3             ,5 },
  1457   1483        { "sqlite3_install_memsys3",    test_install_memsys3          ,0 },
  1458   1484        { "sqlite3_memdebug_vfs_oom_test", test_vfs_oom_test          ,0 },
  1459   1485     };
  1460   1486     int i;

Changes to src/test_spellfix.c.

    26     26   #include <ctype.h>
    27     27   
    28     28   /*
    29     29   ** Character classes for ASCII characters:
    30     30   **
    31     31   **   0   ''        Silent letters:   H W
    32     32   **   1   'A'       Any vowel:   A E I O U (Y)
    33         -**   2   'B'       A bilabeal stop or fricative:  B F P V
           33  +**   2   'B'       A bilabeal stop or fricative:  B F P V W
    34     34   **   3   'C'       Other fricatives or back stops:  C G J K Q S X Z
    35     35   **   4   'D'       Alveolar stops:  D T
    36     36   **   5   'H'       Letter H at the beginning of a word
    37     37   **   6   'L'       Glide:  L
    38     38   **   7   'R'       Semivowel:  R
    39     39   **   8   'M'       Nasals:  M N
    40         -**   9   'W'       Letter W at the beginning of a word
    41         -**   10  'Y'       Letter Y at the beginning of a word.
    42         -**   11  '9'       Digits: 0 1 2 3 4 5 6 7 8 9
    43         -**   12  ' '       White space
    44         -**   13  '?'       Other.
           40  +**   9   'Y'       Letter Y at the beginning of a word.
           41  +**   10  '9'       Digits: 0 1 2 3 4 5 6 7 8 9
           42  +**   11  ' '       White space
           43  +**   12  '?'       Other.
    45     44   */
    46     45   #define CCLASS_SILENT         0
    47     46   #define CCLASS_VOWEL          1
    48     47   #define CCLASS_B              2
    49     48   #define CCLASS_C              3
    50     49   #define CCLASS_D              4
    51     50   #define CCLASS_H              5
    52     51   #define CCLASS_L              6
    53     52   #define CCLASS_R              7
    54     53   #define CCLASS_M              8
    55         -#define CCLASS_W              9
    56         -#define CCLASS_Y             10
    57         -#define CCLASS_DIGIT         11
    58         -#define CCLASS_SPACE         12
    59         -#define CCLASS_OTHER         13
           54  +#define CCLASS_Y              9
           55  +#define CCLASS_DIGIT         10
           56  +#define CCLASS_SPACE         11
           57  +#define CCLASS_OTHER         12
    60     58   
    61     59   /*
    62     60   ** The following table gives the character class for non-initial ASCII
    63     61   ** characters.
    64     62   */
    65     63   static const unsigned char midClass[] = {
    66     64    /*   */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   /*   */ CCLASS_OTHER,
................................................................................
    88     86    /* B */ CCLASS_B,        /* C */ CCLASS_C,       /* D */ CCLASS_D,
    89     87    /* E */ CCLASS_VOWEL,    /* F */ CCLASS_B,       /* G */ CCLASS_C,
    90     88    /* H */ CCLASS_SILENT,   /* I */ CCLASS_VOWEL,   /* J */ CCLASS_C,
    91     89    /* K */ CCLASS_C,        /* L */ CCLASS_L,       /* M */ CCLASS_M,
    92     90    /* N */ CCLASS_M,        /* O */ CCLASS_VOWEL,   /* P */ CCLASS_B,
    93     91    /* Q */ CCLASS_C,        /* R */ CCLASS_R,       /* S */ CCLASS_C,
    94     92    /* T */ CCLASS_D,        /* U */ CCLASS_VOWEL,   /* V */ CCLASS_B,
    95         - /* W */ CCLASS_SILENT,   /* X */ CCLASS_C,       /* Y */ CCLASS_VOWEL,
           93  + /* W */ CCLASS_B,        /* X */ CCLASS_C,       /* Y */ CCLASS_VOWEL,
    96     94    /* Z */ CCLASS_C,        /* [ */ CCLASS_OTHER,   /* \ */ CCLASS_OTHER,
    97     95    /* ] */ CCLASS_OTHER,    /* ^ */ CCLASS_OTHER,   /* _ */ CCLASS_OTHER,
    98     96    /* ` */ CCLASS_OTHER,    /* a */ CCLASS_VOWEL,   /* b */ CCLASS_B,
    99     97    /* c */ CCLASS_C,        /* d */ CCLASS_D,       /* e */ CCLASS_VOWEL,
   100     98    /* f */ CCLASS_B,        /* g */ CCLASS_C,       /* h */ CCLASS_SILENT,
   101     99    /* i */ CCLASS_VOWEL,    /* j */ CCLASS_C,       /* k */ CCLASS_C,
   102    100    /* l */ CCLASS_L,        /* m */ CCLASS_M,       /* n */ CCLASS_M,
   103    101    /* o */ CCLASS_VOWEL,    /* p */ CCLASS_B,       /* q */ CCLASS_C,
   104    102    /* r */ CCLASS_R,        /* s */ CCLASS_C,       /* t */ CCLASS_D,
   105         - /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_SILENT,
          103  + /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_B,
   106    104    /* x */ CCLASS_C,        /* y */ CCLASS_VOWEL,   /* z */ CCLASS_C,
   107    105    /* { */ CCLASS_OTHER,    /* | */ CCLASS_OTHER,   /* } */ CCLASS_OTHER,
   108    106    /* ~ */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   
   109    107   };
   110    108   /* 
   111    109   ** This tables gives the character class for ASCII characters that form the
   112    110   ** initial character of a word.  The only difference from midClass is with
................................................................................
   138    136    /* B */ CCLASS_B,        /* C */ CCLASS_C,       /* D */ CCLASS_D,
   139    137    /* E */ CCLASS_VOWEL,    /* F */ CCLASS_B,       /* G */ CCLASS_C,
   140    138    /* H */ CCLASS_SILENT,   /* I */ CCLASS_VOWEL,   /* J */ CCLASS_C,
   141    139    /* K */ CCLASS_C,        /* L */ CCLASS_L,       /* M */ CCLASS_M,
   142    140    /* N */ CCLASS_M,        /* O */ CCLASS_VOWEL,   /* P */ CCLASS_B,
   143    141    /* Q */ CCLASS_C,        /* R */ CCLASS_R,       /* S */ CCLASS_C,
   144    142    /* T */ CCLASS_D,        /* U */ CCLASS_VOWEL,   /* V */ CCLASS_B,
   145         - /* W */ CCLASS_W,        /* X */ CCLASS_C,       /* Y */ CCLASS_Y,
          143  + /* W */ CCLASS_B,        /* X */ CCLASS_C,       /* Y */ CCLASS_Y,
   146    144    /* Z */ CCLASS_C,        /* [ */ CCLASS_OTHER,   /* \ */ CCLASS_OTHER,
   147    145    /* ] */ CCLASS_OTHER,    /* ^ */ CCLASS_OTHER,   /* _ */ CCLASS_OTHER,
   148    146    /* ` */ CCLASS_OTHER,    /* a */ CCLASS_VOWEL,   /* b */ CCLASS_B,
   149    147    /* c */ CCLASS_C,        /* d */ CCLASS_D,       /* e */ CCLASS_VOWEL,
   150    148    /* f */ CCLASS_B,        /* g */ CCLASS_C,       /* h */ CCLASS_SILENT,
   151    149    /* i */ CCLASS_VOWEL,    /* j */ CCLASS_C,       /* k */ CCLASS_C,
   152    150    /* l */ CCLASS_L,        /* m */ CCLASS_M,       /* n */ CCLASS_M,
   153    151    /* o */ CCLASS_VOWEL,    /* p */ CCLASS_B,       /* q */ CCLASS_C,
   154    152    /* r */ CCLASS_R,        /* s */ CCLASS_C,       /* t */ CCLASS_D,
   155         - /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_W,
          153  + /* u */ CCLASS_VOWEL,    /* v */ CCLASS_B,       /* w */ CCLASS_B,
   156    154    /* x */ CCLASS_C,        /* y */ CCLASS_Y,       /* z */ CCLASS_C,
   157    155    /* { */ CCLASS_OTHER,    /* | */ CCLASS_OTHER,   /* } */ CCLASS_OTHER,
   158    156    /* ~ */ CCLASS_OTHER,    /*   */ CCLASS_OTHER,   
   159    157   };
   160    158   
   161    159   /*
   162    160   ** Mapping from the character class number (0-13) to a symbol for each
   163    161   ** character class.  Note that initClass[] can be used to map the class
   164    162   ** symbol back into the class number.
   165    163   */
   166         -static const unsigned char className[] = ".ABCDHLRMWY9 ?";
          164  +static const unsigned char className[] = ".ABCDHLRMY9 ?";
   167    165   
   168    166   /*
   169    167   ** Generate a "phonetic hash" from a string of ASCII characters
   170    168   ** in zIn[0..nIn-1].
   171    169   **
   172    170   **   * Map characters by character class as defined above.
   173    171   **   * Omit double-letters
................................................................................
  1892   1890     const char *zModule = argv[0];
  1893   1891     const char *zDbName = argv[1];
  1894   1892     const char *zTableName = argv[2];
  1895   1893     int nDbName;
  1896   1894     int rc = SQLITE_OK;
  1897   1895     int i;
  1898   1896   
  1899         -  nDbName = strlen(zDbName);
         1897  +  nDbName = (int)strlen(zDbName);
  1900   1898     pNew = sqlite3_malloc( sizeof(*pNew) + nDbName + 1);
  1901   1899     if( pNew==0 ){
  1902   1900       rc = SQLITE_NOMEM;
  1903   1901     }else{
  1904   1902       memset(pNew, 0, sizeof(*pNew));
  1905   1903       pNew->zDbName = (char*)&pNew[1];
  1906   1904       memcpy(pNew->zDbName, zDbName, nDbName+1);
................................................................................
  2232   2230   
  2233   2231     if( pCur->a==0 || p->rc ) return;   /* Prior memory allocation failure */
  2234   2232     zClass = (char*)phoneticHash((unsigned char*)zQuery, nQuery);
  2235   2233     if( zClass==0 ){
  2236   2234       p->rc = SQLITE_NOMEM;
  2237   2235       return;
  2238   2236     }
  2239         -  nClass = strlen(zClass);
         2237  +  nClass = (int)strlen(zClass);
  2240   2238     if( nClass>SPELLFIX_MX_HASH-2 ){
  2241   2239       nClass = SPELLFIX_MX_HASH-2;
  2242   2240       zClass[nClass] = 0;
  2243   2241     }
  2244   2242     if( nClass<=iScope ){
  2245   2243       if( nClass>2 ){
  2246   2244         iScope = nClass-1;
................................................................................
  2406   2404     zPattern = (char*)transliterate(zMatchThis, sqlite3_value_bytes(argv[0]));
  2407   2405     sqlite3_free(pCur->zPattern);
  2408   2406     pCur->zPattern = zPattern;
  2409   2407     if( zPattern==0 ){
  2410   2408       x.rc = SQLITE_NOMEM;
  2411   2409       goto filter_exit;
  2412   2410     }
  2413         -  nPattern = strlen(zPattern);
         2411  +  nPattern = (int)strlen(zPattern);
  2414   2412     if( zPattern[nPattern-1]=='*' ) nPattern--;
  2415   2413     zSql = sqlite3_mprintf(
  2416   2414        "SELECT id, word, rank, k1"
  2417   2415        "  FROM \"%w\".\"%w_vocab\""
  2418   2416        " WHERE langid=%d AND k2>=?1 AND k2<?2",
  2419   2417        p->zDbName, p->zTableName, iLang
  2420   2418     );
................................................................................
  2567   2565       case SPELLFIX_COL_SCORE: {
  2568   2566         sqlite3_result_int(ctx, pCur->a[pCur->iRow].iScore);
  2569   2567         break;
  2570   2568       }
  2571   2569       case SPELLFIX_COL_MATCHLEN: {
  2572   2570         int iMatchlen = pCur->a[pCur->iRow].iMatchlen;
  2573   2571         if( iMatchlen<0 ){
  2574         -        int nPattern = strlen(pCur->zPattern);
         2572  +        int nPattern = (int)strlen(pCur->zPattern);
  2575   2573           char *zWord = pCur->a[pCur->iRow].zWord;
  2576         -        int nWord = strlen(zWord);
         2574  +        int nWord = (int)strlen(zWord);
  2577   2575   
  2578   2576           if( nPattern>0 && pCur->zPattern[nPattern-1]=='*' ){
  2579   2577             char *zTranslit;
  2580   2578             int res;
  2581   2579             zTranslit = (char *)transliterate((unsigned char *)zWord, nWord);
  2582   2580             if( !zTranslit ) return SQLITE_NOMEM;
  2583   2581             res = editdist1(pCur->zPattern, zTranslit, &iMatchlen);

Changes to src/utf.c.

   160    160         c = (c<<6) + (0x3f & *(zIn++));                      \
   161    161       }                                                      \
   162    162       if( c<0x80                                             \
   163    163           || (c&0xFFFFF800)==0xD800                          \
   164    164           || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
   165    165     }
   166    166   u32 sqlite3Utf8Read(
   167         -  const unsigned char *zIn,       /* First byte of UTF-8 character */
   168         -  const unsigned char **pzNext    /* Write first byte past UTF-8 char here */
          167  +  const unsigned char **pz    /* Pointer to string from which to read char */
   169    168   ){
   170    169     unsigned int c;
   171    170   
   172    171     /* Same as READ_UTF8() above but without the zTerm parameter.
   173    172     ** For this routine, we assume the UTF8 string is always zero-terminated.
   174    173     */
   175         -  c = *(zIn++);
          174  +  c = *((*pz)++);
   176    175     if( c>=0xc0 ){
   177    176       c = sqlite3Utf8Trans1[c-0xc0];
   178         -    while( (*zIn & 0xc0)==0x80 ){
   179         -      c = (c<<6) + (0x3f & *(zIn++));
          177  +    while( (*(*pz) & 0xc0)==0x80 ){
          178  +      c = (c<<6) + (0x3f & *((*pz)++));
   180    179       }
   181    180       if( c<0x80
   182    181           || (c&0xFFFFF800)==0xD800
   183    182           || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
   184    183     }
   185         -  *pzNext = zIn;
   186    184     return c;
   187    185   }
   188    186   
   189    187   
   190    188   
   191    189   
   192    190   /*
................................................................................
   279    277     }
   280    278     z = zOut;
   281    279   
   282    280     if( pMem->enc==SQLITE_UTF8 ){
   283    281       if( desiredEnc==SQLITE_UTF16LE ){
   284    282         /* UTF-8 -> UTF-16 Little-endian */
   285    283         while( zIn<zTerm ){
   286         -        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
   287    284           READ_UTF8(zIn, zTerm, c);
   288    285           WRITE_UTF16LE(z, c);
   289    286         }
   290    287       }else{
   291    288         assert( desiredEnc==SQLITE_UTF16BE );
   292    289         /* UTF-8 -> UTF-16 Big-endian */
   293    290         while( zIn<zTerm ){
   294         -        /* c = sqlite3Utf8Read(zIn, zTerm, (const u8**)&zIn); */
   295    291           READ_UTF8(zIn, zTerm, c);
   296    292           WRITE_UTF16BE(z, c);
   297    293         }
   298    294       }
   299    295       pMem->n = (int)(z - zOut);
   300    296       *z++ = 0;
   301    297     }else{
................................................................................
   415    411   */
   416    412   int sqlite3Utf8To8(unsigned char *zIn){
   417    413     unsigned char *zOut = zIn;
   418    414     unsigned char *zStart = zIn;
   419    415     u32 c;
   420    416   
   421    417     while( zIn[0] && zOut<=zIn ){
   422         -    c = sqlite3Utf8Read(zIn, (const u8**)&zIn);
          418  +    c = sqlite3Utf8Read((const u8**)&zIn);
   423    419       if( c!=0xfffd ){
   424    420         WRITE_UTF8(zOut, c);
   425    421       }
   426    422     }
   427    423     *zOut = 0;
   428    424     return (int)(zOut - zStart);
   429    425   }
................................................................................
   520    516     for(i=0; i<0x00110000; i++){
   521    517       z = zBuf;
   522    518       WRITE_UTF8(z, i);
   523    519       n = (int)(z-zBuf);
   524    520       assert( n>0 && n<=4 );
   525    521       z[0] = 0;
   526    522       z = zBuf;
   527         -    c = sqlite3Utf8Read(z, (const u8**)&z);
          523  +    c = sqlite3Utf8Read((const u8**)&z);
   528    524       t = i;
   529    525       if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
   530    526       if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
   531    527       assert( c==t );
   532    528       assert( (z-zBuf)==n );
   533    529     }
   534    530     for(i=0; i<0x00110000; i++){

Changes to src/vdbe.c.

   952    952     pOut->z = pOp->p4.z;
   953    953     pOut->n = pOp->p1;
   954    954     pOut->enc = encoding;
   955    955     UPDATE_MAX_BLOBSIZE(pOut);
   956    956     break;
   957    957   }
   958    958   
   959         -/* Opcode: Null * P2 P3 * *
          959  +/* Opcode: Null P1 P2 P3 * *
   960    960   **
   961    961   ** Write a NULL into registers P2.  If P3 greater than P2, then also write
   962         -** NULL into register P3 and ever register in between P2 and P3.  If P3
          962  +** NULL into register P3 and every register in between P2 and P3.  If P3
   963    963   ** is less than P2 (typically P3 is zero) then only register P2 is
   964         -** set to NULL
          964  +** set to NULL.
          965  +**
          966  +** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
          967  +** NULL values will not compare equal even if SQLITE_NULLEQ is set on
          968  +** OP_Ne or OP_Eq.
   965    969   */
   966    970   case OP_Null: {           /* out2-prerelease */
   967    971     int cnt;
          972  +  u16 nullFlag;
   968    973     cnt = pOp->p3-pOp->p2;
   969    974     assert( pOp->p3<=p->nMem );
   970         -  pOut->flags = MEM_Null;
          975  +  pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
   971    976     while( cnt>0 ){
   972    977       pOut++;
   973    978       memAboutToChange(p, pOut);
   974    979       VdbeMemRelease(pOut);
   975         -    pOut->flags = MEM_Null;
          980  +    pOut->flags = nullFlag;
   976    981       cnt--;
   977    982     }
   978    983     break;
   979    984   }
   980    985   
   981    986   
   982    987   /* Opcode: Blob P1 P2 * P4
................................................................................
  1011   1016     sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
  1012   1017     UPDATE_MAX_BLOBSIZE(pOut);
  1013   1018     break;
  1014   1019   }
  1015   1020   
  1016   1021   /* Opcode: Move P1 P2 P3 * *
  1017   1022   **
  1018         -** Move the values in register P1..P1+P3-1 over into
  1019         -** registers P2..P2+P3-1.  Registers P1..P1+P1-1 are
         1023  +** Move the values in register P1..P1+P3 over into
         1024  +** registers P2..P2+P3.  Registers P1..P1+P3 are
  1020   1025   ** left holding a NULL.  It is an error for register ranges
  1021         -** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
         1026  +** P1..P1+P3 and P2..P2+P3 to overlap.
  1022   1027   */
  1023   1028   case OP_Move: {
  1024   1029     char *zMalloc;   /* Holding variable for allocated memory */
  1025   1030     int n;           /* Number of registers left to copy */
  1026   1031     int p1;          /* Register to copy from */
  1027   1032     int p2;          /* Register to copy to */
  1028   1033   
  1029         -  n = pOp->p3;
         1034  +  n = pOp->p3 + 1;
  1030   1035     p1 = pOp->p1;
  1031   1036     p2 = pOp->p2;
  1032   1037     assert( n>0 && p1>0 && p2>0 );
  1033   1038     assert( p1+n<=p2 || p2+n<=p1 );
  1034   1039   
  1035   1040     pIn1 = &aMem[p1];
  1036   1041     pOut = &aMem[p2];
................................................................................
  1051   1056       REGISTER_TRACE(p2++, pOut);
  1052   1057       pIn1++;
  1053   1058       pOut++;
  1054   1059     }
  1055   1060     break;
  1056   1061   }
  1057   1062   
  1058         -/* Opcode: Copy P1 P2 * * *
         1063  +/* Opcode: Copy P1 P2 P3 * *
  1059   1064   **
  1060         -** Make a copy of register P1 into register P2.
         1065  +** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
  1061   1066   **
  1062   1067   ** This instruction makes a deep copy of the value.  A duplicate
  1063   1068   ** is made of any string or blob constant.  See also OP_SCopy.
  1064   1069   */
  1065         -case OP_Copy: {             /* in1, out2 */
         1070  +case OP_Copy: {
         1071  +  int n;
         1072  +
         1073  +  n = pOp->p3;
  1066   1074     pIn1 = &aMem[pOp->p1];
  1067   1075     pOut = &aMem[pOp->p2];
  1068   1076     assert( pOut!=pIn1 );
  1069         -  sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
  1070         -  Deephemeralize(pOut);
  1071         -  REGISTER_TRACE(pOp->p2, pOut);
         1077  +  while( 1 ){
         1078  +    sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
         1079  +    Deephemeralize(pOut);
         1080  +    REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
         1081  +    if( (n--)==0 ) break;
         1082  +    pOut++;
         1083  +    pIn1++;
         1084  +  }
  1072   1085     break;
  1073   1086   }
  1074   1087   
  1075   1088   /* Opcode: SCopy P1 P2 * * *
  1076   1089   **
  1077   1090   ** Make a shallow copy of register P1 into register P2.
  1078   1091   **
................................................................................
  1733   1746   ** memcmp() is used to compare text string.  If both values are
  1734   1747   ** numeric, then a numeric comparison is used. If the two values
  1735   1748   ** are of different types, then numbers are considered less than
  1736   1749   ** strings and strings are considered less than blobs.
  1737   1750   **
  1738   1751   ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
  1739   1752   ** store a boolean result (either 0, or 1, or NULL) in register P2.
         1753  +**
         1754  +** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
         1755  +** equal to one another, provided that they do not have their MEM_Cleared
         1756  +** bit set.
  1740   1757   */
  1741   1758   /* Opcode: Ne P1 P2 P3 P4 P5
  1742   1759   **
  1743   1760   ** This works just like the Lt opcode except that the jump is taken if
  1744   1761   ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
  1745   1762   ** additional information.
  1746   1763   **
................................................................................
  1799   1816       /* One or both operands are NULL */
  1800   1817       if( pOp->p5 & SQLITE_NULLEQ ){
  1801   1818         /* If SQLITE_NULLEQ is set (which will only happen if the operator is
  1802   1819         ** OP_Eq or OP_Ne) then take the jump or not depending on whether
  1803   1820         ** or not both operands are null.
  1804   1821         */
  1805   1822         assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
  1806         -      res = (flags1 & flags3 & MEM_Null)==0;
         1823  +      assert( (flags1 & MEM_Cleared)==0 );
         1824  +      if( (flags1&MEM_Null)!=0
         1825  +       && (flags3&MEM_Null)!=0
         1826  +       && (flags3&MEM_Cleared)==0
         1827  +      ){
         1828  +        res = 0;  /* Results are equal */
         1829  +      }else{
         1830  +        res = 1;  /* Results are not equal */
         1831  +      }
  1807   1832       }else{
  1808   1833         /* SQLITE_NULLEQ is clear and at least one operand is NULL,
  1809   1834         ** then the result is always NULL.
  1810   1835         ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
  1811   1836         */
  1812   1837         if( pOp->p5 & SQLITE_STOREP2 ){
  1813   1838           pOut = &aMem[pOp->p2];
................................................................................
  3274   3299   **
  3275   3300   ** This opcode works like OP_OpenEphemeral except that it opens
  3276   3301   ** a transient index that is specifically designed to sort large
  3277   3302   ** tables using an external merge-sort algorithm.
  3278   3303   */
  3279   3304   case OP_SorterOpen: {
  3280   3305     VdbeCursor *pCx;
         3306  +
  3281   3307   #ifndef SQLITE_OMIT_MERGE_SORT
  3282   3308     pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
  3283   3309     if( pCx==0 ) goto no_mem;
  3284   3310     pCx->pKeyInfo = pOp->p4.pKeyInfo;
  3285   3311     pCx->pKeyInfo->enc = ENC(p->db);
  3286   3312     pCx->isSorter = 1;
  3287   3313     rc = sqlite3VdbeSorterInit(db, pCx);
................................................................................
  4164   4190   
  4165   4191   /* Opcode: SorterData P1 P2 * * *
  4166   4192   **
  4167   4193   ** Write into register P2 the current sorter data for sorter cursor P1.
  4168   4194   */
  4169   4195   case OP_SorterData: {
  4170   4196     VdbeCursor *pC;
         4197  +
  4171   4198   #ifndef SQLITE_OMIT_MERGE_SORT
  4172   4199     pOut = &aMem[pOp->p2];
  4173   4200     pC = p->apCsr[pOp->p1];
  4174   4201     assert( pC->isSorter );
  4175   4202     rc = sqlite3VdbeSorterRowkey(pC, pOut);
  4176   4203   #else
  4177   4204     pOp->opcode = OP_RowKey;
................................................................................
  4702   4729   ** See also: Clear
  4703   4730   */
  4704   4731   case OP_Destroy: {     /* out2-prerelease */
  4705   4732     int iMoved;
  4706   4733     int iCnt;
  4707   4734     Vdbe *pVdbe;
  4708   4735     int iDb;
         4736  +
  4709   4737   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4710   4738     iCnt = 0;
  4711   4739     for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
  4712   4740       if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->inVtabMethod<2 && pVdbe->pc>=0 ){
  4713   4741         iCnt++;
  4714   4742       }
  4715   4743     }
................................................................................
  5491   5519   ** Write a string containing the final journal-mode to register P2.
  5492   5520   */
  5493   5521   case OP_JournalMode: {    /* out2-prerelease */
  5494   5522     Btree *pBt;                     /* Btree to change journal mode of */
  5495   5523     Pager *pPager;                  /* Pager associated with pBt */
  5496   5524     int eNew;                       /* New journal mode */
  5497   5525     int eOld;                       /* The old journal mode */
         5526  +#ifndef SQLITE_OMIT_WAL
  5498   5527     const char *zFilename;          /* Name of database file for pPager */
         5528  +#endif
  5499   5529   
  5500   5530     eNew = pOp->p3;
  5501   5531     assert( eNew==PAGER_JOURNALMODE_DELETE 
  5502   5532          || eNew==PAGER_JOURNALMODE_TRUNCATE 
  5503   5533          || eNew==PAGER_JOURNALMODE_PERSIST 
  5504   5534          || eNew==PAGER_JOURNALMODE_OFF
  5505   5535          || eNew==PAGER_JOURNALMODE_MEMORY
................................................................................
  6071   6101   ** If tracing is enabled (by the sqlite3_trace()) interface, then
  6072   6102   ** the UTF-8 string contained in P4 is emitted on the trace callback.
  6073   6103   */
  6074   6104   case OP_Trace: {
  6075   6105     char *zTrace;
  6076   6106     char *z;
  6077   6107   
  6078         -  if( db->xTrace && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
         6108  +  if( db->xTrace
         6109  +   && !p->doingRerun
         6110  +   && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
         6111  +  ){
  6079   6112       z = sqlite3VdbeExpandSql(p, zTrace);
  6080   6113       db->xTrace(db->pTraceArg, z);
  6081   6114       sqlite3DbFree(db, z);
  6082   6115     }
  6083   6116   #ifdef SQLITE_DEBUG
  6084   6117     if( (db->flags & SQLITE_SqlTrace)!=0
  6085   6118      && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0

Changes to src/vdbeInt.h.

   183    183   #define MEM_Str       0x0002   /* Value is a string */
   184    184   #define MEM_Int       0x0004   /* Value is an integer */
   185    185   #define MEM_Real      0x0008   /* Value is a real number */
   186    186   #define MEM_Blob      0x0010   /* Value is a BLOB */
   187    187   #define MEM_RowSet    0x0020   /* Value is a RowSet object */
   188    188   #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
   189    189   #define MEM_Invalid   0x0080   /* Value is undefined */
   190         -#define MEM_TypeMask  0x00ff   /* Mask of type bits */
          190  +#define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
          191  +#define MEM_TypeMask  0x01ff   /* Mask of type bits */
          192  +
   191    193   
   192    194   /* Whenever Mem contains a valid string or blob representation, one of
   193    195   ** the following flags must be set to determine the memory management
   194    196   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
   195    197   ** string is \000 or \u0000 terminated
   196    198   */
   197    199   #define MEM_Term      0x0200   /* String rep is nul terminated */
................................................................................
   269    271     Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
   270    272     StrAccum str;      /* The string being accumulated */
   271    273     int nIndent;       /* Number of elements in aIndent */
   272    274     u16 aIndent[100];  /* Levels of indentation */
   273    275     char zBase[100];   /* Initial space */
   274    276   };
   275    277   
          278  +/* A bitfield type for use inside of structures.  Always follow with :N where
          279  +** N is the number of bits.
          280  +*/
          281  +typedef unsigned bft;  /* Bit Field Type */
          282  +
   276    283   /*
   277    284   ** An instance of the virtual machine.  This structure contains the complete
   278    285   ** state of the virtual machine.
   279    286   **
   280    287   ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
   281    288   ** is really a pointer to an instance of this structure.
   282    289   **
................................................................................
   310    317     char **azVar;           /* Name of variables */
   311    318     ynVar nVar;             /* Number of entries in aVar[] */
   312    319     ynVar nzVar;            /* Number of entries in azVar[] */
   313    320     u32 cacheCtr;           /* VdbeCursor row cache generation counter */
   314    321     int pc;                 /* The program counter */
   315    322     int rc;                 /* Value to return */
   316    323     u8 errorAction;         /* Recovery action to do in case of an error */
   317         -  u8 explain;             /* True if EXPLAIN present on SQL command */
   318         -  u8 changeCntOn;         /* True to update the change-counter */
   319         -  u8 expired;             /* True if the VM needs to be recompiled */
   320         -  u8 runOnlyOnce;         /* Automatically expire on reset */
   321    324     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   322         -  u8 inVtabMethod;        /* See comments above */
   323         -  u8 usesStmtJournal;     /* True if uses a statement journal */
   324         -  u8 readOnly;            /* True for read-only statements */
   325         -  u8 isPrepareV2;         /* True if prepared with prepare_v2() */
          325  +  bft explain:2;          /* True if EXPLAIN present on SQL command */
          326  +  bft inVtabMethod:2;     /* See comments above */
          327  +  bft changeCntOn:1;      /* True to update the change-counter */
          328  +  bft expired:1;          /* True if the VM needs to be recompiled */
          329  +  bft runOnlyOnce:1;      /* Automatically expire on reset */
          330  +  bft usesStmtJournal:1;  /* True if uses a statement journal */
          331  +  bft readOnly:1;         /* True for read-only statements */
          332  +  bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
          333  +  bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
   326    334     int nChange;            /* Number of db changes made since last reset */
   327    335     yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
   328    336     yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
   329    337     int iStatement;         /* Statement number (or 0 if has not opened stmt) */
   330    338     int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
   331    339   #ifndef SQLITE_OMIT_TRACE
   332    340     i64 startTime;          /* Time when query started - used for profiling */

Changes to src/vdbeapi.c.

   491    491     sqlite3 *db;             /* The database connection */
   492    492   
   493    493     if( vdbeSafetyNotNull(v) ){
   494    494       return SQLITE_MISUSE_BKPT;
   495    495     }
   496    496     db = v->db;
   497    497     sqlite3_mutex_enter(db->mutex);
          498  +  v->doingRerun = 0;
   498    499     while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
   499    500            && cnt++ < SQLITE_MAX_SCHEMA_RETRY
   500    501            && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
   501    502       sqlite3_reset(pStmt);
          503  +    v->doingRerun = 1;
   502    504       assert( v->expired==0 );
   503    505     }
   504    506     if( rc2!=SQLITE_OK && ALWAYS(v->isPrepareV2) && ALWAYS(db->pErr) ){
   505    507       /* This case occurs after failing to recompile an sql statement. 
   506    508       ** The error message from the SQL compiler has already been loaded 
   507    509       ** into the database handle. This block copies the error message 
   508    510       ** from the database handle into the statement and sets the statement

Changes to src/vdbeaux.c.

   741    741       nByte = sizeof(*pKeyInfo) + (nField-1)*sizeof(pKeyInfo->aColl[0]) + nField;
   742    742       pKeyInfo = sqlite3DbMallocRaw(0, nByte);
   743    743       pOp->p4.pKeyInfo = pKeyInfo;
   744    744       if( pKeyInfo ){
   745    745         u8 *aSortOrder;
   746    746         memcpy((char*)pKeyInfo, zP4, nByte - nField);
   747    747         aSortOrder = pKeyInfo->aSortOrder;
   748         -      if( aSortOrder ){
   749         -        pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
   750         -        memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
   751         -      }
          748  +      assert( aSortOrder!=0 );
          749  +      pKeyInfo->aSortOrder = (unsigned char*)&pKeyInfo->aColl[nField];
          750  +      memcpy(pKeyInfo->aSortOrder, aSortOrder, nField);
   752    751         pOp->p4type = P4_KEYINFO;
   753    752       }else{
   754    753         p->db->mallocFailed = 1;
   755    754         pOp->p4type = P4_NOTUSED;
   756    755       }
   757    756     }else if( n==P4_KEYINFO_HANDOFF ){
   758    757       pOp->p4.p = (void*)zP4;
................................................................................
   857    856     char *zP4 = zTemp;
   858    857     assert( nTemp>=20 );
   859    858     switch( pOp->p4type ){
   860    859       case P4_KEYINFO_STATIC:
   861    860       case P4_KEYINFO: {
   862    861         int i, j;
   863    862         KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
          863  +      assert( pKeyInfo->aSortOrder!=0 );
   864    864         sqlite3_snprintf(nTemp, zTemp, "keyinfo(%d", pKeyInfo->nField);
   865    865         i = sqlite3Strlen30(zTemp);
   866    866         for(j=0; j<pKeyInfo->nField; j++){
   867    867           CollSeq *pColl = pKeyInfo->aColl[j];
   868    868           if( pColl ){
   869    869             int n = sqlite3Strlen30(pColl->zName);
   870    870             if( i+n>nTemp-6 ){
   871    871               memcpy(&zTemp[i],",...",4);
   872    872               break;
   873    873             }
   874    874             zTemp[i++] = ',';
   875         -          if( pKeyInfo->aSortOrder && pKeyInfo->aSortOrder[j] ){
          875  +          if( pKeyInfo->aSortOrder[j] ){
   876    876               zTemp[i++] = '-';
   877    877             }
   878    878             memcpy(&zTemp[i], pColl->zName,n+1);
   879    879             i += n;
   880    880           }else if( i+4<nTemp-6 ){
   881    881             memcpy(&zTemp[i],",nil",4);
   882    882             i += 4;
................................................................................
  2581   2581       return 0;
  2582   2582     }
  2583   2583     if( flags&MEM_Int ){
  2584   2584       /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  2585   2585   #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
  2586   2586       i64 i = pMem->u.i;
  2587   2587       u64 u;
  2588         -    if( file_format>=4 && (i&1)==i ){
  2589         -      return 8+(u32)i;
  2590         -    }
  2591   2588       if( i<0 ){
  2592   2589         if( i<(-MAX_6BYTE) ) return 6;
  2593   2590         /* Previous test prevents:  u = -(-9223372036854775808) */
  2594   2591         u = -i;
  2595   2592       }else{
  2596   2593         u = i;
  2597   2594       }
  2598         -    if( u<=127 ) return 1;
         2595  +    if( u<=127 ){
         2596  +      return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
         2597  +    }
  2599   2598       if( u<=32767 ) return 2;
  2600   2599       if( u<=8388607 ) return 3;
  2601   2600       if( u<=2147483647 ) return 4;
  2602   2601       if( u<=MAX_6BYTE ) return 5;
  2603   2602       return 6;
  2604   2603     }
  2605   2604     if( flags&MEM_Real ){
................................................................................
  2876   2875       if( !p ) return 0;
  2877   2876     }else{
  2878   2877       p = (UnpackedRecord*)&pSpace[nOff];
  2879   2878       *ppFree = 0;
  2880   2879     }
  2881   2880   
  2882   2881     p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
         2882  +  assert( pKeyInfo->aSortOrder!=0 );
  2883   2883     p->pKeyInfo = pKeyInfo;
  2884   2884     p->nField = pKeyInfo->nField + 1;
  2885   2885     return p;
  2886   2886   }
  2887   2887   
  2888   2888   /*
  2889   2889   ** Given the nKey-byte encoding of a record in pKey[], populate the 
................................................................................
  2969   2969     ** to ignore the compiler warnings and leave this variable uninitialized.
  2970   2970     */
  2971   2971     /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
  2972   2972     
  2973   2973     idx1 = getVarint32(aKey1, szHdr1);
  2974   2974     d1 = szHdr1;
  2975   2975     nField = pKeyInfo->nField;
         2976  +  assert( pKeyInfo->aSortOrder!=0 );
  2976   2977     while( idx1<szHdr1 && i<pPKey2->nField ){
  2977   2978       u32 serial_type1;
  2978   2979   
  2979   2980       /* Read the serial types for the next element in each key. */
  2980   2981       idx1 += getVarint32( aKey1+idx1, serial_type1 );
  2981   2982       if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
  2982   2983   
................................................................................
  2988   2989       */
  2989   2990       rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
  2990   2991                              i<nField ? pKeyInfo->aColl[i] : 0);
  2991   2992       if( rc!=0 ){
  2992   2993         assert( mem1.zMalloc==0 );  /* See comment below */
  2993   2994   
  2994   2995         /* Invert the result if we are using DESC sort order. */
  2995         -      if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
         2996  +      if( i<nField && pKeyInfo->aSortOrder[i] ){
  2996   2997           rc = -rc;
  2997   2998         }
  2998   2999       
  2999   3000         /* If the PREFIX_SEARCH flag is set and all fields except the final
  3000   3001         ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
  3001   3002         ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
  3002   3003         ** This is used by the OP_IsUnique opcode.

Changes to src/vtab.c.

   524    524         sqlite3VtabUnlock(pVTable);
   525    525         rc = SQLITE_ERROR;
   526    526       }else{
   527    527         int iCol;
   528    528         /* If everything went according to plan, link the new VTable structure
   529    529         ** into the linked list headed by pTab->pVTable. Then loop through the 
   530    530         ** columns of the table to see if any of them contain the token "hidden".
   531         -      ** If so, set the Column.isHidden flag and remove the token from
          531  +      ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
   532    532         ** the type string.  */
   533    533         pVTable->pNext = pTab->pVTable;
   534    534         pTab->pVTable = pVTable;
   535    535   
   536    536         for(iCol=0; iCol<pTab->nCol; iCol++){
   537    537           char *zType = pTab->aCol[iCol].zType;
   538    538           int nType;
................................................................................
   555    555             for(j=i; (j+nDel)<=nType; j++){
   556    556               zType[j] = zType[j+nDel];
   557    557             }
   558    558             if( zType[i]=='\0' && i>0 ){
   559    559               assert(zType[i-1]==' ');
   560    560               zType[i-1] = '\0';
   561    561             }
   562         -          pTab->aCol[iCol].isHidden = 1;
          562  +          pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
   563    563           }
   564    564         }
   565    565       }
   566    566     }
   567    567   
   568    568     sqlite3DbFree(db, zModuleName);
   569    569     return rc;

Changes to src/where.c.

    19     19   #include "sqliteInt.h"
    20     20   
    21     21   
    22     22   /*
    23     23   ** Trace output macros
    24     24   */
    25     25   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    26         -int sqlite3WhereTrace = 0;
           26  +/***/ int sqlite3WhereTrace = 0;
    27     27   #endif
    28         -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
           28  +#if defined(SQLITE_DEBUG) \
           29  +    && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
    29     30   # define WHERETRACE(X)  if(sqlite3WhereTrace) sqlite3DebugPrintf X
    30     31   #else
    31     32   # define WHERETRACE(X)
    32     33   #endif
    33     34   
    34     35   /* Forward reference
    35     36   */
................................................................................
   260    261   #define WHERE_ORDERBY      0x01000000  /* Output will appear in correct order */
   261    262   #define WHERE_REVERSE      0x02000000  /* Scan in reverse order */
   262    263   #define WHERE_UNIQUE       0x04000000  /* Selects no more than one row */
   263    264   #define WHERE_VIRTUALTABLE 0x08000000  /* Use virtual-table processing */
   264    265   #define WHERE_MULTI_OR     0x10000000  /* OR using multiple indices */
   265    266   #define WHERE_TEMP_INDEX   0x20000000  /* Uses an ephemeral index */
   266    267   #define WHERE_DISTINCT     0x40000000  /* Correct order for DISTINCT */
          268  +#define WHERE_COVER_SCAN   0x80000000  /* Full scan of a covering index */
          269  +
          270  +/*
          271  +** This module contains many separate subroutines that work together to
          272  +** find the best indices to use for accessing a particular table in a query.
          273  +** An instance of the following structure holds context information about the
          274  +** index search so that it can be more easily passed between the various
          275  +** routines.
          276  +*/
          277  +typedef struct WhereBestIdx WhereBestIdx;
          278  +struct WhereBestIdx {
          279  +  Parse *pParse;                  /* Parser context */
          280  +  WhereClause *pWC;               /* The WHERE clause */
          281  +  struct SrcList_item *pSrc;      /* The FROM clause term to search */
          282  +  Bitmask notReady;               /* Mask of cursors not available */
          283  +  Bitmask notValid;               /* Cursors not available for any purpose */
          284  +  ExprList *pOrderBy;             /* The ORDER BY clause */
          285  +  ExprList *pDistinct;            /* The select-list if query is DISTINCT */
          286  +  sqlite3_index_info **ppIdxInfo; /* Index information passed to xBestIndex */
          287  +  int i, n;                       /* Which loop is being coded; # of loops */
          288  +  WhereLevel *aLevel;             /* Info about outer loops */
          289  +  WhereCost cost;                 /* Lowest cost query plan */
          290  +};
   267    291   
   268    292   /*
   269    293   ** Initialize a preallocated WhereClause structure.
   270    294   */
   271    295   static void whereClauseInit(
   272    296     WhereClause *pWC,        /* The WhereClause to be initialized */
   273    297     Parse *pParse,           /* The parsing context */
................................................................................
  1403   1427     /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1404   1428     ** an index for tables to the left of the join.
  1405   1429     */
  1406   1430     pTerm->prereqRight |= extraRight;
  1407   1431   }
  1408   1432   
  1409   1433   /*
  1410         -** Return TRUE if any of the expressions in pList->a[iFirst...] contain
  1411         -** a reference to any table other than the iBase table.
         1434  +** Return TRUE if the given index is UNIQUE and all columns past the
         1435  +** first nSkip columns are NOT NULL.
  1412   1436   */
  1413         -static int referencesOtherTables(
  1414         -  ExprList *pList,          /* Search expressions in ths list */
  1415         -  WhereMaskSet *pMaskSet,   /* Mapping from tables to bitmaps */
  1416         -  int iFirst,               /* Be searching with the iFirst-th expression */
  1417         -  int iBase                 /* Ignore references to this table */
  1418         -){
  1419         -  Bitmask allowed = ~getMask(pMaskSet, iBase);
  1420         -  while( iFirst<pList->nExpr ){
  1421         -    if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){
  1422         -      return 1;
  1423         -    }
         1437  +static int indexIsUniqueNotNull(Index *pIdx, int nSkip){
         1438  +  Table *pTab = pIdx->pTable;
         1439  +  int i;
         1440  +  if( pIdx->onError==OE_None ) return 0;
         1441  +  for(i=nSkip; i<pIdx->nColumn; i++){
         1442  +    int j = pIdx->aiColumn[i];
         1443  +    if( j>=0 && pTab->aCol[j].notNull==0 ) return 0;
  1424   1444     }
  1425         -  return 0;
         1445  +  return 1;
  1426   1446   }
  1427   1447   
  1428   1448   /*
  1429   1449   ** This function searches the expression list passed as the second argument
  1430   1450   ** for an expression of type TK_COLUMN that refers to the same column and
  1431   1451   ** uses the same collation sequence as the iCol'th column of index pIdx.
  1432   1452   ** Argument iBase is the cursor number used for the table that pIdx refers
................................................................................
  1584   1604     }
  1585   1605   
  1586   1606     return 0;
  1587   1607   }
  1588   1608   
  1589   1609   /*
  1590   1610   ** This routine decides if pIdx can be used to satisfy the ORDER BY
  1591         -** clause.  If it can, it returns 1.  If pIdx cannot satisfy the
  1592         -** ORDER BY clause, this routine returns 0.
         1611  +** clause, either in whole or in part.  The return value is the 
         1612  +** cumulative number of terms in the ORDER BY clause that are satisfied
         1613  +** by the index pIdx and other indices in outer loops.
  1593   1614   **
  1594         -** pOrderBy is an ORDER BY clause from a SELECT statement.  pTab is the
  1595         -** left-most table in the FROM clause of that same SELECT statement and
  1596         -** the table has a cursor number of "base".  pIdx is an index on pTab.
         1615  +** The table being queried has a cursor number of "base".  pIdx is the
         1616  +** index that is postulated for use to access the table.
  1597   1617   **
  1598   1618   ** nEqCol is the number of columns of pIdx that are used as equality
  1599         -** constraints.  Any of these columns may be missing from the ORDER BY
  1600         -** clause and the match can still be a success.
         1619  +** constraints and where the other side of the == is an ordered column
         1620  +** or constant.  An "order column" in the previous sentence means a column
         1621  +** in table from an outer loop whose values will always appear in the 
         1622  +** correct order due to othre index, or because the outer loop generates
         1623  +** a unique result.  Any of the first nEqCol columns of pIdx may be missing
         1624  +** from the ORDER BY clause and the match can still be a success.
  1601   1625   **
  1602         -** All terms of the ORDER BY that match against the index must be either
  1603         -** ASC or DESC.  (Terms of the ORDER BY clause past the end of a UNIQUE
  1604         -** index do not need to satisfy this constraint.)  The *pbRev value is
  1605         -** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
  1606         -** the ORDER BY clause is all ASC.
         1626  +** The *pbRev value is set to 0 order 1 depending on whether or not
         1627  +** pIdx should be run in the forward order or in reverse order.
  1607   1628   */
  1608   1629   static int isSortingIndex(
  1609         -  Parse *pParse,          /* Parsing context */
  1610         -  WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */
  1611         -  Index *pIdx,            /* The index we are testing */
  1612         -  int base,               /* Cursor number for the table to be sorted */
  1613         -  ExprList *pOrderBy,     /* The ORDER BY clause */
  1614         -  int nEqCol,             /* Number of index columns with == constraints */
  1615         -  int wsFlags,            /* Index usages flags */
  1616         -  int *pbRev              /* Set to 1 if ORDER BY is DESC */
  1617         -){
  1618         -  int i, j;                       /* Loop counters */
  1619         -  int sortOrder = 0;              /* XOR of index and ORDER BY sort direction */
  1620         -  int nTerm;                      /* Number of ORDER BY terms */
  1621         -  struct ExprList_item *pTerm;    /* A term of the ORDER BY clause */
  1622         -  sqlite3 *db = pParse->db;
  1623         -
  1624         -  if( !pOrderBy ) return 0;
  1625         -  if( wsFlags & WHERE_COLUMN_IN ) return 0;
  1626         -  if( pIdx->bUnordered ) return 0;
  1627         -
         1630  +  WhereBestIdx *p,    /* Best index search context */
         1631  +  Index *pIdx,        /* The index we are testing */
         1632  +  int base,           /* Cursor number for the table to be sorted */
         1633  +  int nEqCol,         /* Number of index columns with ordered == constraints */
         1634  +  int wsFlags,        /* Index usages flags */
         1635  +  int bOuterRev,      /* True if outer loops scan in reverse order */
         1636  +  int *pbRev          /* Set to 1 for reverse-order scan of pIdx */
         1637  +){
         1638  +  int i;                        /* Number of pIdx terms used */
         1639  +  int j;                        /* Number of ORDER BY terms satisfied */
         1640  +  int sortOrder = 0;            /* XOR of index and ORDER BY sort direction */
         1641  +  int nTerm;                    /* Number of ORDER BY terms */
         1642  +  struct ExprList_item *pTerm;  /* A term of the ORDER BY clause */
         1643  +  ExprList *pOrderBy;           /* The ORDER BY clause */
         1644  +  Parse *pParse = p->pParse;    /* Parser context */
         1645  +  sqlite3 *db = pParse->db;     /* Database connection */
         1646  +  int nPriorSat;                /* ORDER BY terms satisfied by outer loops */
         1647  +  int seenRowid = 0;            /* True if an ORDER BY rowid term is seen */
         1648  +  int nEqOneRow;                /* Idx columns that ref unique values */
         1649  +
         1650  +  if( p->i==0 ){
         1651  +    nPriorSat = 0;
         1652  +    nEqOneRow = nEqCol;
         1653  +  }else{
         1654  +    if( OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
         1655  +    nPriorSat = p->aLevel[p->i-1].plan.nOBSat;
         1656  +    sortOrder = bOuterRev;
         1657  +    nEqOneRow = 0;
         1658  +  }
         1659  +  if( p->i>0 && nEqCol==0 /*&& !allOuterLoopsUnique(p)*/ ) return nPriorSat;
         1660  +  pOrderBy = p->pOrderBy;
         1661  +  if( !pOrderBy ) return nPriorSat;
         1662  +  if( wsFlags & WHERE_COLUMN_IN ) return nPriorSat;
         1663  +  if( pIdx->bUnordered ) return nPriorSat;
  1628   1664     nTerm = pOrderBy->nExpr;
  1629   1665     assert( nTerm>0 );
  1630   1666   
  1631   1667     /* Argument pIdx must either point to a 'real' named index structure, 
  1632   1668     ** or an index structure allocated on the stack by bestBtreeIndex() to
  1633   1669     ** represent the rowid index that is part of every table.  */
  1634   1670     assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) );
................................................................................
  1637   1673     ** the index.
  1638   1674     **
  1639   1675     ** Note that indices have pIdx->nColumn regular columns plus
  1640   1676     ** one additional column containing the rowid.  The rowid column
  1641   1677     ** of the index is also allowed to match against the ORDER BY
  1642   1678     ** clause.
  1643   1679     */
  1644         -  for(i=j=0, pTerm=pOrderBy->a; j<nTerm && i<=pIdx->nColumn; i++){
         1680  +  for(i=0,j=nPriorSat,pTerm=&pOrderBy->a[j]; j<nTerm && i<=pIdx->nColumn; i++){
  1645   1681       Expr *pExpr;       /* The expression of the ORDER BY pTerm */
  1646   1682       CollSeq *pColl;    /* The collating sequence of pExpr */
  1647   1683       int termSortOrder; /* Sort order for this term */
  1648   1684       int iColumn;       /* The i-th column of the index.  -1 for rowid */
  1649   1685       int iSortOrder;    /* 1 for DESC, 0 for ASC on the i-th index term */
  1650   1686       const char *zColl; /* Name of the collating sequence for i-th index term */
  1651   1687   
................................................................................
  1681   1717         }else if( i==pIdx->nColumn ){
  1682   1718           /* Index column i is the rowid.  All other terms match. */
  1683   1719           break;
  1684   1720         }else{
  1685   1721           /* If an index column fails to match and is not constrained by ==
  1686   1722           ** then the index cannot satisfy the ORDER BY constraint.
  1687   1723           */
  1688         -        return 0;
         1724  +        return nPriorSat;
  1689   1725         }
  1690   1726       }
  1691   1727       assert( pIdx->aSortOrder!=0 || iColumn==-1 );
  1692   1728       assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 );
  1693   1729       assert( iSortOrder==0 || iSortOrder==1 );
  1694   1730       termSortOrder = iSortOrder ^ pTerm->sortOrder;
  1695         -    if( i>nEqCol ){
         1731  +    if( i>nEqOneRow ){
  1696   1732         if( termSortOrder!=sortOrder ){
  1697   1733           /* Indices can only be used if all ORDER BY terms past the
  1698   1734           ** equality constraints are all either DESC or ASC. */
  1699         -        return 0;
         1735  +        break;
  1700   1736         }
  1701   1737       }else{
  1702   1738         sortOrder = termSortOrder;
  1703   1739       }
  1704   1740       j++;
  1705   1741       pTerm++;
  1706         -    if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){
  1707         -      /* If the indexed column is the primary key and everything matches
  1708         -      ** so far and none of the ORDER BY terms to the right reference other
  1709         -      ** tables in the join, then we are assured that the index can be used 
  1710         -      ** to sort because the primary key is unique and so none of the other
  1711         -      ** columns will make any difference
  1712         -      */
  1713         -      j = nTerm;
         1742  +    if( iColumn<0 ){
         1743  +      seenRowid = 1;
         1744  +      break;
  1714   1745       }
  1715   1746     }
         1747  +  *pbRev = sortOrder;
  1716   1748   
  1717         -  *pbRev = sortOrder!=0;
  1718         -  if( j>=nTerm ){
  1719         -    /* All terms of the ORDER BY clause are covered by this index so
  1720         -    ** this index can be used for sorting. */
  1721         -    return 1;
  1722         -  }
  1723         -  if( pIdx->onError!=OE_None && i==pIdx->nColumn
  1724         -      && (wsFlags & WHERE_COLUMN_NULL)==0
  1725         -      && !referencesOtherTables(pOrderBy, pMaskSet, j, base) 
         1749  +  /* If there was an "ORDER BY rowid" term that matched, or it is only
         1750  +  ** possible for a single row from this table to match, then skip over
         1751  +  ** any additional ORDER BY terms dealing with this table.
         1752  +  */
         1753  +  if( seenRowid ||
         1754  +     (   (wsFlags & WHERE_COLUMN_NULL)==0
         1755  +      && i>=pIdx->nColumn
         1756  +      && indexIsUniqueNotNull(pIdx, nEqCol)
         1757  +     )
  1726   1758     ){
  1727         -    Column *aCol = pIdx->pTable->aCol;
  1728         -
  1729         -    /* All terms of this index match some prefix of the ORDER BY clause,
  1730         -    ** the index is UNIQUE, and no terms on the tail of the ORDER BY
  1731         -    ** refer to other tables in a join. So, assuming that the index entries
  1732         -    ** visited contain no NULL values, then this index delivers rows in
  1733         -    ** the required order.
  1734         -    **
  1735         -    ** It is not possible for any of the first nEqCol index fields to be
  1736         -    ** NULL (since the corresponding "=" operator in the WHERE clause would 
  1737         -    ** not be true). So if all remaining index columns have NOT NULL 
  1738         -    ** constaints attached to them, we can be confident that the visited
  1739         -    ** index entries are free of NULLs.  */
  1740         -    for(i=nEqCol; i<pIdx->nColumn; i++){
  1741         -      if( aCol[pIdx->aiColumn[i]].notNull==0 ) break;
         1759  +    /* Advance j over additional ORDER BY terms associated with base */
         1760  +    WhereMaskSet *pMS = p->pWC->pMaskSet;
         1761  +    Bitmask m = ~getMask(pMS, base);
         1762  +    while( j<nTerm && (exprTableUsage(pMS, pOrderBy->a[j].pExpr)&m)==0 ){
         1763  +      j++;
  1742   1764       }
  1743         -    return (i==pIdx->nColumn);
  1744   1765     }
  1745         -  return 0;
         1766  +  return j;
  1746   1767   }
  1747   1768   
  1748   1769   /*
  1749   1770   ** Prepare a crude estimate of the logarithm of the input value.
  1750   1771   ** The results need not be exact.  This is only used for estimating
  1751   1772   ** the total cost of performing operations with O(logN) or O(NlogN)
  1752   1773   ** complexity.  Because N is just a guess, it is no great tragedy if
................................................................................
  1805   1826   #define TRACE_IDX_INPUTS(A)
  1806   1827   #define TRACE_IDX_OUTPUTS(A)
  1807   1828   #endif
  1808   1829   
  1809   1830   /* 
  1810   1831   ** Required because bestIndex() is called by bestOrClauseIndex() 
  1811   1832   */
  1812         -static void bestIndex(
  1813         -    Parse*, WhereClause*, struct SrcList_item*,
  1814         -    Bitmask, Bitmask, ExprList*, WhereCost*);
         1833  +static void bestIndex(WhereBestIdx*);
  1815   1834   
  1816   1835   /*
  1817   1836   ** This routine attempts to find an scanning strategy that can be used 
  1818   1837   ** to optimize an 'OR' expression that is part of a WHERE clause. 
  1819   1838   **
  1820   1839   ** The table associated with FROM clause term pSrc may be either a
  1821   1840   ** regular B-Tree table or a virtual table.
  1822   1841   */
  1823         -static void bestOrClauseIndex(
  1824         -  Parse *pParse,              /* The parsing context */
  1825         -  WhereClause *pWC,           /* The WHERE clause */
  1826         -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
  1827         -  Bitmask notReady,           /* Mask of cursors not available for indexing */
  1828         -  Bitmask notValid,           /* Cursors not available for any purpose */
  1829         -  ExprList *pOrderBy,         /* The ORDER BY clause */
  1830         -  WhereCost *pCost            /* Lowest cost query plan */
  1831         -){
         1842  +static void bestOrClauseIndex(WhereBestIdx *p){
  1832   1843   #ifndef SQLITE_OMIT_OR_OPTIMIZATION
  1833         -  const int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
         1844  +  WhereClause *pWC = p->pWC;           /* The WHERE clause */
         1845  +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
         1846  +  const int iCur = pSrc->iCursor;      /* The cursor of the table  */
  1834   1847     const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur);  /* Bitmask for pSrc */
  1835   1848     WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm];        /* End of pWC->a[] */
  1836         -  WhereTerm *pTerm;                 /* A single term of the WHERE clause */
         1849  +  WhereTerm *pTerm;                    /* A single term of the WHERE clause */
  1837   1850   
  1838   1851     /* The OR-clause optimization is disallowed if the INDEXED BY or
  1839   1852     ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
  1840   1853     if( pSrc->notIndexed || pSrc->pIndex!=0 ){
  1841   1854       return;
  1842   1855     }
  1843   1856     if( pWC->wctrlFlags & WHERE_AND_ONLY ){
  1844   1857       return;
  1845   1858     }
  1846   1859   
  1847   1860     /* Search the WHERE clause terms for a usable WO_OR term. */
  1848   1861     for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  1849   1862       if( pTerm->eOperator==WO_OR 
  1850         -     && ((pTerm->prereqAll & ~maskSrc) & notReady)==0
         1863  +     && ((pTerm->prereqAll & ~maskSrc) & p->notReady)==0
  1851   1864        && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 
  1852   1865       ){
  1853   1866         WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
  1854   1867         WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
  1855   1868         WhereTerm *pOrTerm;
  1856   1869         int flags = WHERE_MULTI_OR;
  1857   1870         double rTotal = 0;
  1858   1871         double nRow = 0;
  1859   1872         Bitmask used = 0;
         1873  +      WhereBestIdx sBOI;
  1860   1874   
         1875  +      sBOI = *p;
         1876  +      sBOI.pOrderBy = 0;
         1877  +      sBOI.pDistinct = 0;
         1878  +      sBOI.ppIdxInfo = 0;
  1861   1879         for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
  1862         -        WhereCost sTermCost;
  1863   1880           WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
  1864   1881             (pOrTerm - pOrWC->a), (pTerm - pWC->a)
  1865   1882           ));
  1866   1883           if( pOrTerm->eOperator==WO_AND ){
  1867         -          WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
  1868         -          bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
         1884  +          sBOI.pWC = &pOrTerm->u.pAndInfo->wc;
         1885  +          bestIndex(&sBOI);
  1869   1886           }else if( pOrTerm->leftCursor==iCur ){
  1870   1887             WhereClause tempWC;
  1871   1888             tempWC.pParse = pWC->pParse;
  1872   1889             tempWC.pMaskSet = pWC->pMaskSet;
  1873   1890             tempWC.pOuter = pWC;
  1874   1891             tempWC.op = TK_AND;
  1875   1892             tempWC.a = pOrTerm;
  1876   1893             tempWC.wctrlFlags = 0;
  1877   1894             tempWC.nTerm = 1;
  1878         -          bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
         1895  +          sBOI.pWC = &tempWC;
         1896  +          bestIndex(&sBOI);
  1879   1897           }else{
  1880   1898             continue;
  1881   1899           }
  1882         -        rTotal += sTermCost.rCost;
  1883         -        nRow += sTermCost.plan.nRow;
  1884         -        used |= sTermCost.used;
  1885         -        if( rTotal>=pCost->rCost ) break;
         1900  +        rTotal += sBOI.cost.rCost;
         1901  +        nRow += sBOI.cost.plan.nRow;
         1902  +        used |= sBOI.cost.used;
         1903  +        if( rTotal>=p->cost.rCost ) break;
  1886   1904         }
  1887   1905   
  1888   1906         /* If there is an ORDER BY clause, increase the scan cost to account 
  1889   1907         ** for the cost of the sort. */
  1890         -      if( pOrderBy!=0 ){
         1908  +      if( p->pOrderBy!=0 ){
  1891   1909           WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n",
  1892   1910                       rTotal, rTotal+nRow*estLog(nRow)));
  1893   1911           rTotal += nRow*estLog(nRow);
  1894   1912         }
  1895   1913   
  1896   1914         /* If the cost of scanning using this OR term for optimization is
  1897   1915         ** less than the current cost stored in pCost, replace the contents
  1898   1916         ** of pCost. */
  1899   1917         WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow));
  1900         -      if( rTotal<pCost->rCost ){
  1901         -        pCost->rCost = rTotal;
  1902         -        pCost->used = used;
  1903         -        pCost->plan.nRow = nRow;
  1904         -        pCost->plan.wsFlags = flags;
  1905         -        pCost->plan.u.pTerm = pTerm;
         1918  +      if( rTotal<p->cost.rCost ){
         1919  +        p->cost.rCost = rTotal;
         1920  +        p->cost.used = used;
         1921  +        p->cost.plan.nRow = nRow;
         1922  +        p->cost.plan.wsFlags = flags;
         1923  +        p->cost.plan.u.pTerm = pTerm;
  1906   1924         }
  1907   1925       }
  1908   1926     }
  1909   1927   #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  1910   1928   }
  1911   1929   
  1912   1930   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
................................................................................
  1935   1953   ** If the query plan for pSrc specified in pCost is a full table scan
  1936   1954   ** and indexing is allows (if there is no NOT INDEXED clause) and it
  1937   1955   ** possible to construct a transient index that would perform better
  1938   1956   ** than a full table scan even when the cost of constructing the index
  1939   1957   ** is taken into account, then alter the query plan to use the
  1940   1958   ** transient index.
  1941   1959   */
  1942         -static void bestAutomaticIndex(
  1943         -  Parse *pParse,              /* The parsing context */
  1944         -  WhereClause *pWC,           /* The WHERE clause */
  1945         -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
  1946         -  Bitmask notReady,           /* Mask of cursors that are not available */
  1947         -  WhereCost *pCost            /* Lowest cost query plan */
  1948         -){
  1949         -  double nTableRow;           /* Rows in the input table */
  1950         -  double logN;                /* log(nTableRow) */
         1960  +static void bestAutomaticIndex(WhereBestIdx *p){
         1961  +  Parse *pParse = p->pParse;            /* The parsing context */
         1962  +  WhereClause *pWC = p->pWC;            /* The WHERE clause */
         1963  +  struct SrcList_item *pSrc = p->pSrc;  /* The FROM clause term to search */
         1964  +  double nTableRow;                     /* Rows in the input table */
         1965  +  double logN;                          /* log(nTableRow) */
  1951   1966     double costTempIdx;         /* per-query cost of the transient index */
  1952   1967     WhereTerm *pTerm;           /* A single term of the WHERE clause */
  1953   1968     WhereTerm *pWCEnd;          /* End of pWC->a[] */
  1954   1969     Table *pTable;              /* Table tht might be indexed */
  1955   1970   
  1956   1971     if( pParse->nQueryLoop<=(double)1 ){
  1957   1972       /* There is no point in building an automatic index for a single scan */
  1958   1973       return;
  1959   1974     }
  1960   1975     if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){
  1961   1976       /* Automatic indices are disabled at run-time */
  1962   1977       return;
  1963   1978     }
  1964         -  if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
         1979  +  if( (p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){
  1965   1980       /* We already have some kind of index in use for this query. */
  1966   1981       return;
  1967   1982     }
  1968   1983     if( pSrc->notIndexed ){
  1969   1984       /* The NOT INDEXED clause appears in the SQL. */
  1970   1985       return;
  1971   1986     }
................................................................................
  1975   1990     }
  1976   1991   
  1977   1992     assert( pParse->nQueryLoop >= (double)1 );
  1978   1993     pTable = pSrc->pTab;
  1979   1994     nTableRow = pTable->nRowEst;
  1980   1995     logN = estLog(nTableRow);
  1981   1996     costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1);
  1982         -  if( costTempIdx>=pCost->rCost ){
         1997  +  if( costTempIdx>=p->cost.rCost ){
  1983   1998       /* The cost of creating the transient table would be greater than
  1984   1999       ** doing the full table scan */
  1985   2000       return;
  1986   2001     }
  1987   2002   
  1988   2003     /* Search for any equality comparison term */
  1989   2004     pWCEnd = &pWC->a[pWC->nTerm];
  1990   2005     for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
  1991         -    if( termCanDriveIndex(pTerm, pSrc, notReady) ){
         2006  +    if( termCanDriveIndex(pTerm, pSrc, p->notReady) ){
  1992   2007         WHERETRACE(("auto-index reduces cost from %.1f to %.1f\n",
  1993         -                    pCost->rCost, costTempIdx));
  1994         -      pCost->rCost = costTempIdx;
  1995         -      pCost->plan.nRow = logN + 1;
  1996         -      pCost->plan.wsFlags = WHERE_TEMP_INDEX;
  1997         -      pCost->used = pTerm->prereqRight;
         2008  +                    p->cost.rCost, costTempIdx));
         2009  +      p->cost.rCost = costTempIdx;
         2010  +      p->cost.plan.nRow = logN + 1;
         2011  +      p->cost.plan.wsFlags = WHERE_TEMP_INDEX;
         2012  +      p->cost.used = pTerm->prereqRight;
  1998   2013         break;
  1999   2014       }
  2000   2015     }
  2001   2016   }
  2002   2017   #else
  2003         -# define bestAutomaticIndex(A,B,C,D,E)  /* no-op */
         2018  +# define bestAutomaticIndex(A)  /* no-op */
  2004   2019   #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
  2005   2020   
  2006   2021   
  2007   2022   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  2008   2023   /*
  2009   2024   ** Generate code to construct the Index object for an automatic index
  2010   2025   ** and to set up the WhereLevel object pLevel so that the code generator
................................................................................
  2157   2172   
  2158   2173   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2159   2174   /*
  2160   2175   ** Allocate and populate an sqlite3_index_info structure. It is the 
  2161   2176   ** responsibility of the caller to eventually release the structure
  2162   2177   ** by passing the pointer returned by this function to sqlite3_free().
  2163   2178   */
  2164         -static sqlite3_index_info *allocateIndexInfo(
  2165         -  Parse *pParse, 
  2166         -  WhereClause *pWC,
  2167         -  struct SrcList_item *pSrc,
  2168         -  ExprList *pOrderBy
  2169         -){
         2179  +static sqlite3_index_info *allocateIndexInfo(WhereBestIdx *p){
         2180  +  Parse *pParse = p->pParse; 
         2181  +  WhereClause *pWC = p->pWC;
         2182  +  struct SrcList_item *pSrc = p->pSrc;
         2183  +  ExprList *pOrderBy = p->pOrderBy;
  2170   2184     int i, j;
  2171   2185     int nTerm;
  2172   2186     struct sqlite3_index_constraint *pIdxCons;
  2173   2187     struct sqlite3_index_orderby *pIdxOrderBy;
  2174   2188     struct sqlite3_index_constraint_usage *pUsage;
  2175   2189     WhereTerm *pTerm;
  2176   2190     int nOrderBy;
................................................................................
  2192   2206   
  2193   2207     /* If the ORDER BY clause contains only columns in the current 
  2194   2208     ** virtual table then allocate space for the aOrderBy part of
  2195   2209     ** the sqlite3_index_info structure.
  2196   2210     */
  2197   2211     nOrderBy = 0;
  2198   2212     if( pOrderBy ){
  2199         -    for(i=0; i<pOrderBy->nExpr; i++){
         2213  +    int n = pOrderBy->nExpr;
         2214  +    for(i=0; i<n; i++){
  2200   2215         Expr *pExpr = pOrderBy->a[i].pExpr;
  2201   2216         if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
  2202   2217       }
  2203         -    if( i==pOrderBy->nExpr ){
  2204         -      nOrderBy = pOrderBy->nExpr;
         2218  +    if( i==n){
         2219  +      nOrderBy = n;
  2205   2220       }
  2206   2221     }
  2207   2222   
  2208   2223     /* Allocate the sqlite3_index_info structure
  2209   2224     */
  2210   2225     pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
  2211   2226                              + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
................................................................................
  2321   2336   ** same virtual table.  The sqlite3_index_info structure is created
  2322   2337   ** and initialized on the first invocation and reused on all subsequent
  2323   2338   ** invocations.  The sqlite3_index_info structure is also used when
  2324   2339   ** code is generated to access the virtual table.  The whereInfoDelete() 
  2325   2340   ** routine takes care of freeing the sqlite3_index_info structure after
  2326   2341   ** everybody has finished with it.
  2327   2342   */
  2328         -static void bestVirtualIndex(
  2329         -  Parse *pParse,                  /* The parsing context */
  2330         -  WhereClause *pWC,               /* The WHERE clause */
  2331         -  struct SrcList_item *pSrc,      /* The FROM clause term to search */
  2332         -  Bitmask notReady,               /* Mask of cursors not available for index */
  2333         -  Bitmask notValid,               /* Cursors not valid for any purpose */
  2334         -  ExprList *pOrderBy,             /* The order by clause */
  2335         -  WhereCost *pCost,               /* Lowest cost query plan */
  2336         -  sqlite3_index_info **ppIdxInfo  /* Index information passed to xBestIndex */
  2337         -){
         2343  +static void bestVirtualIndex(WhereBestIdx *p){
         2344  +  Parse *pParse = p->pParse;      /* The parsing context */
         2345  +  WhereClause *pWC = p->pWC;      /* The WHERE clause */
         2346  +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
  2338   2347     Table *pTab = pSrc->pTab;
  2339   2348     sqlite3_index_info *pIdxInfo;
  2340   2349     struct sqlite3_index_constraint *pIdxCons;
  2341   2350     struct sqlite3_index_constraint_usage *pUsage;
  2342   2351     WhereTerm *pTerm;
  2343   2352     int i, j;
  2344   2353     int nOrderBy;
  2345   2354     double rCost;
  2346   2355   
  2347   2356     /* Make sure wsFlags is initialized to some sane value. Otherwise, if the 
  2348   2357     ** malloc in allocateIndexInfo() fails and this function returns leaving
  2349   2358     ** wsFlags in an uninitialized state, the caller may behave unpredictably.
  2350   2359     */
  2351         -  memset(pCost, 0, sizeof(*pCost));
  2352         -  pCost->plan.wsFlags = WHERE_VIRTUALTABLE;
         2360  +  memset(&p->cost, 0, sizeof(p->cost));
         2361  +  p->cost.plan.wsFlags = WHERE_VIRTUALTABLE;
  2353   2362   
  2354   2363     /* If the sqlite3_index_info structure has not been previously
  2355   2364     ** allocated and initialized, then allocate and initialize it now.
  2356   2365     */
  2357         -  pIdxInfo = *ppIdxInfo;
         2366  +  pIdxInfo = *p->ppIdxInfo;
  2358   2367     if( pIdxInfo==0 ){
  2359         -    *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
         2368  +    *p->ppIdxInfo = pIdxInfo = allocateIndexInfo(p);
  2360   2369     }
  2361   2370     if( pIdxInfo==0 ){
  2362   2371       return;
  2363   2372     }
  2364   2373   
  2365   2374     /* At this point, the sqlite3_index_info structure that pIdxInfo points
  2366   2375     ** to will have been initialized, either during the current invocation or
................................................................................
  2397   2406     ** each time.
  2398   2407     */
  2399   2408     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
  2400   2409     pUsage = pIdxInfo->aConstraintUsage;
  2401   2410     for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
  2402   2411       j = pIdxCons->iTermOffset;
  2403   2412       pTerm = &pWC->a[j];
  2404         -    pIdxCons->usable = (pTerm->prereqRight&notReady) ? 0 : 1;
         2413  +    pIdxCons->usable = (pTerm->prereqRight&p->notReady) ? 0 : 1;
  2405   2414     }
  2406   2415     memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
  2407   2416     if( pIdxInfo->needToFreeIdxStr ){
  2408   2417       sqlite3_free(pIdxInfo->idxStr);
  2409   2418     }
  2410   2419     pIdxInfo->idxStr = 0;
  2411   2420     pIdxInfo->idxNum = 0;
  2412   2421     pIdxInfo->needToFreeIdxStr = 0;
  2413   2422     pIdxInfo->orderByConsumed = 0;
  2414   2423     /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
  2415   2424     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2);
  2416   2425     nOrderBy = pIdxInfo->nOrderBy;
  2417         -  if( !pOrderBy ){
         2426  +  if( !p->pOrderBy ){
  2418   2427       pIdxInfo->nOrderBy = 0;
  2419   2428     }
  2420   2429   
  2421   2430     if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
  2422   2431       return;
  2423   2432     }
  2424   2433   
  2425   2434     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
  2426   2435     for(i=0; i<pIdxInfo->nConstraint; i++){
  2427   2436       if( pUsage[i].argvIndex>0 ){
  2428         -      pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
         2437  +      p->cost.used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight;
  2429   2438       }
  2430   2439     }
  2431   2440   
  2432   2441     /* If there is an ORDER BY clause, and the selected virtual table index
  2433   2442     ** does not satisfy it, increase the cost of the scan accordingly. This
  2434   2443     ** matches the processing for non-virtual tables in bestBtreeIndex().
  2435   2444     */
  2436   2445     rCost = pIdxInfo->estimatedCost;
  2437         -  if( pOrderBy && pIdxInfo->orderByConsumed==0 ){
         2446  +  if( p->pOrderBy && pIdxInfo->orderByConsumed==0 ){
  2438   2447       rCost += estLog(rCost)*rCost;
  2439   2448     }
  2440   2449   
  2441   2450     /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
  2442   2451     ** inital value of lowestCost in this loop. If it is, then the
  2443   2452     ** (cost<lowestCost) test below will never be true.
  2444   2453     ** 
  2445   2454     ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT 
  2446   2455     ** is defined.
  2447   2456     */
  2448   2457     if( (SQLITE_BIG_DBL/((double)2))<rCost ){
  2449         -    pCost->rCost = (SQLITE_BIG_DBL/((double)2));
         2458  +    p->cost.rCost = (SQLITE_BIG_DBL/((double)2));
  2450   2459     }else{
  2451         -    pCost->rCost = rCost;
         2460  +    p->cost.rCost = rCost;
  2452   2461     }
  2453         -  pCost->plan.u.pVtabIdx = pIdxInfo;
         2462  +  p->cost.plan.u.pVtabIdx = pIdxInfo;
  2454   2463     if( pIdxInfo->orderByConsumed ){
  2455         -    pCost->plan.wsFlags |= WHERE_ORDERBY;
         2464  +    p->cost.plan.wsFlags |= WHERE_ORDERBY;
  2456   2465     }
  2457         -  pCost->plan.nEq = 0;
         2466  +  p->cost.plan.nEq = 0;
  2458   2467     pIdxInfo->nOrderBy = nOrderBy;
  2459   2468   
  2460   2469     /* Try to find a more efficient access pattern by using multiple indexes
  2461   2470     ** to optimize an OR expression within the WHERE clause. 
  2462   2471     */
  2463         -  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
         2472  +  bestOrClauseIndex(p);
  2464   2473   }
  2465   2474   #endif /* SQLITE_OMIT_VIRTUALTABLE */
  2466   2475   
  2467   2476   #ifdef SQLITE_ENABLE_STAT3
  2468   2477   /*
  2469   2478   ** Estimate the location of a particular key among all keys in an
  2470   2479   ** index.  Store the results in aStat as follows:
................................................................................
  2855   2864       *pnRow = nRowEst;
  2856   2865       WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
  2857   2866     }
  2858   2867     return rc;
  2859   2868   }
  2860   2869   #endif /* defined(SQLITE_ENABLE_STAT3) */
  2861   2870   
         2871  +/*
         2872  +** Check to see if column iCol of the table with cursor iTab will appear
         2873  +** in sorted order according to the current query plan.  Return true if
         2874  +** it will and false if not.  
         2875  +**
         2876  +** If *pbRev is initially 2 (meaning "unknown") then set *pbRev to the
         2877  +** sort order of iTab.iCol.  If *pbRev is 0 or 1 but does not match
         2878  +** the sort order of iTab.iCol, then consider the column to be unordered.
         2879  +*/
         2880  +static int isOrderedColumn(WhereBestIdx *p, int iTab, int iCol, int *pbRev){
         2881  +  int i, j;
         2882  +  WhereLevel *pLevel = &p->aLevel[p->i-1];
         2883  +  Index *pIdx;
         2884  +  u8 sortOrder;
         2885  +  for(i=p->i-1; i>=0; i--, pLevel--){
         2886  +    if( pLevel->iTabCur!=iTab ) continue;
         2887  +    if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
         2888  +      pIdx = pLevel->plan.u.pIdx;
         2889  +      if( iCol<0 ){
         2890  +        sortOrder = 0;
         2891  +        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
         2892  +      }else{
         2893  +        for(j=0; j<pIdx->nColumn; j++){
         2894  +          if( iCol==pIdx->aiColumn[j] ) break;
         2895  +        }
         2896  +        if( j>=pIdx->nColumn ) return 0;
         2897  +        sortOrder = pIdx->aSortOrder[j];
         2898  +        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
         2899  +      }
         2900  +    }else{
         2901  +      if( iCol!=(-1) ) return 0;
         2902  +      sortOrder = 0;
         2903  +      testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
         2904  +    }
         2905  +    if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
         2906  +      assert( sortOrder==0 || sortOrder==1 );
         2907  +      testcase( sortOrder==1 );
         2908  +      sortOrder = 1 - sortOrder;
         2909  +    }
         2910  +    if( *pbRev==2 ){
         2911  +      *pbRev = sortOrder;
         2912  +      return 1;
         2913  +    }
         2914  +    return (*pbRev==sortOrder);
         2915  +  }
         2916  +  return 0;
         2917  +}
         2918  +
         2919  +/*
         2920  +** pTerm is an == constraint.  Check to see if the other side of
         2921  +** the == is a constant or a value that is guaranteed to be ordered
         2922  +** by outer loops.  Return 1 if pTerm is ordered, and 0 if not.
         2923  +*/
         2924  +static int isOrderedTerm(WhereBestIdx *p, WhereTerm *pTerm, int *pbRev){
         2925  +  Expr *pExpr = pTerm->pExpr;
         2926  +  assert( pExpr->op==TK_EQ );
         2927  +  assert( pExpr->pLeft!=0 && pExpr->pLeft->op==TK_COLUMN );
         2928  +  assert( pExpr->pRight!=0 );
         2929  +  if( p->i==0 ){
         2930  +    return 1;  /* All == are ordered in the outer loop */
         2931  +  }
         2932  +  if( pTerm->prereqRight==0 ){
         2933  +    return 1;  /* RHS of the == is a constant */
         2934  +  }
         2935  +  if( pExpr->pRight->op==TK_COLUMN 
         2936  +   && isOrderedColumn(p, pExpr->pRight->iTable, pExpr->pRight->iColumn, pbRev)
         2937  +  ){
         2938  +    return 1;
         2939  +  }
         2940  +
         2941  +  /* If we cannot prove that the constraint is ordered, assume it is not */
         2942  +  return 0;
         2943  +}
         2944  +
  2862   2945   
  2863   2946   /*
  2864   2947   ** Find the best query plan for accessing a particular table.  Write the
  2865         -** best query plan and its cost into the WhereCost object supplied as the
  2866         -** last parameter.
         2948  +** best query plan and its cost into the p->cost.
  2867   2949   **
  2868   2950   ** The lowest cost plan wins.  The cost is an estimate of the amount of
  2869   2951   ** CPU and disk I/O needed to process the requested result.
  2870   2952   ** Factors that influence cost include:
  2871   2953   **
  2872   2954   **    *  The estimated number of rows that will be retrieved.  (The
  2873   2955   **       fewer the better.)
................................................................................
  2884   2966   ** then the cost is calculated in the usual way.
  2885   2967   **
  2886   2968   ** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table 
  2887   2969   ** in the SELECT statement, then no indexes are considered. However, the 
  2888   2970   ** selected plan may still take advantage of the built-in rowid primary key
  2889   2971   ** index.
  2890   2972   */
  2891         -static void bestBtreeIndex(
  2892         -  Parse *pParse,              /* The parsing context */
  2893         -  WhereClause *pWC,           /* The WHERE clause */
  2894         -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
  2895         -  Bitmask notReady,           /* Mask of cursors not available for indexing */
  2896         -  Bitmask notValid,           /* Cursors not available for any purpose */
  2897         -  ExprList *pOrderBy,         /* The ORDER BY clause */
  2898         -  ExprList *pDistinct,        /* The select-list if query is DISTINCT */
  2899         -  WhereCost *pCost            /* Lowest cost query plan */
  2900         -){
         2973  +static void bestBtreeIndex(WhereBestIdx *p){
         2974  +  Parse *pParse = p->pParse;  /* The parsing context */
         2975  +  WhereClause *pWC = p->pWC;  /* The WHERE clause */
         2976  +  struct SrcList_item *pSrc = p->pSrc; /* The FROM clause term to search */
  2901   2977     int iCur = pSrc->iCursor;   /* The cursor of the table to be accessed */
  2902   2978     Index *pProbe;              /* An index we are evaluating */
  2903   2979     Index *pIdx;                /* Copy of pProbe, or zero for IPK index */
  2904   2980     int eqTermMask;             /* Current mask of valid equality operators */
  2905   2981     int idxEqTermMask;          /* Index mask of valid equality operators */
  2906   2982     Index sPk;                  /* A fake index object for the primary key */
  2907   2983     tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
  2908   2984     int aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
  2909         -  int wsFlagMask;             /* Allowed flags in pCost->plan.wsFlag */
         2985  +  int wsFlagMask;             /* Allowed flags in p->cost.plan.wsFlag */
  2910   2986   
  2911   2987     /* Initialize the cost to a worst-case value */
  2912         -  memset(pCost, 0, sizeof(*pCost));
  2913         -  pCost->rCost = SQLITE_BIG_DBL;
         2988  +  memset(&p->cost, 0, sizeof(p->cost));
         2989  +  p->cost.rCost = SQLITE_BIG_DBL;
  2914   2990   
  2915   2991     /* If the pSrc table is the right table of a LEFT JOIN then we may not
  2916   2992     ** use an index to satisfy IS NULL constraints on that table.  This is
  2917   2993     ** because columns might end up being NULL if the table does not match -
  2918   2994     ** a circumstance which the index cannot help us discover.  Ticket #2177.
  2919   2995     */
  2920   2996     if( pSrc->jointype & JT_LEFT ){
................................................................................
  2959   3035     /* Loop over all indices looking for the best one to use
  2960   3036     */
  2961   3037     for(; pProbe; pIdx=pProbe=pProbe->pNext){
  2962   3038       const tRowcnt * const aiRowEst = pProbe->aiRowEst;
  2963   3039       double cost;                /* Cost of using pProbe */
  2964   3040       double nRow;                /* Estimated number of rows in result set */
  2965   3041       double log10N = (double)1;  /* base-10 logarithm of nRow (inexact) */
  2966         -    int rev;                    /* True to scan in reverse order */
         3042  +    int bRev = 2;               /* 0=forward scan.  1=reverse.  2=undecided */
  2967   3043       int wsFlags = 0;
  2968   3044       Bitmask used = 0;
  2969   3045   
  2970   3046       /* The following variables are populated based on the properties of
  2971   3047       ** index being evaluated. They are then used to determine the expected
  2972   3048       ** cost and number of rows returned.
  2973   3049       **
................................................................................
  2992   3068       **
  2993   3069       **    nInMul is set to 1.
  2994   3070       **
  2995   3071       **    If there exists a WHERE term of the form "x IN (SELECT ...)", then 
  2996   3072       **    the sub-select is assumed to return 25 rows for the purposes of 
  2997   3073       **    determining nInMul.
  2998   3074       **
         3075  +    **  nOrdered:
         3076  +    **    The number of equality terms that are constrainted by outer loop
         3077  +    **    variables that are well-ordered.
         3078  +    **
  2999   3079       **  bInEst:  
  3000   3080       **    Set to true if there was at least one "x IN (SELECT ...)" term used 
  3001   3081       **    in determining the value of nInMul.  Note that the RHS of the
  3002   3082       **    IN operator must be a SELECT, not a value list, for this variable
  3003   3083       **    to be true.
  3004   3084       **
  3005   3085       **  rangeDiv:
................................................................................
  3010   3090       **    space to 1/16th of its original size (rangeDiv==16).
  3011   3091       **
  3012   3092       **  bSort:   
  3013   3093       **    Boolean. True if there is an ORDER BY clause that will require an 
  3014   3094       **    external sort (i.e. scanning the index being evaluated will not 
  3015   3095       **    correctly order records).
  3016   3096       **
         3097  +    **  bDistinct:
         3098  +    **    Boolean. True if there is a DISTINCT clause that will require an 
         3099  +    **    external btree.
         3100  +    **
  3017   3101       **  bLookup: 
  3018   3102       **    Boolean. True if a table lookup is required for each index entry
  3019   3103       **    visited.  In other words, true if this is not a covering index.
  3020   3104       **    This is always false for the rowid primary key index of a table.
  3021   3105       **    For other indexes, it is true unless all the columns of the table
  3022   3106       **    used by the SELECT statement are present in the index (such an
  3023   3107       **    index is sometimes described as a covering index).
................................................................................
  3026   3110       **    of column c, but the first does not because columns a and b are
  3027   3111       **    both available in the index.
  3028   3112       **
  3029   3113       **             SELECT a, b    FROM tbl WHERE a = 1;
  3030   3114       **             SELECT a, b, c FROM tbl WHERE a = 1;
  3031   3115       */
  3032   3116       int nEq;                      /* Number of == or IN terms matching index */
         3117  +    int nOrdered;                 /* Number of ordered terms matching index */
  3033   3118       int bInEst = 0;               /* True if "x IN (SELECT...)" seen */
  3034   3119       int nInMul = 1;               /* Number of distinct equalities to lookup */
  3035   3120       double rangeDiv = (double)1;  /* Estimated reduction in search space */
  3036   3121       int nBound = 0;               /* Number of range constraints seen */
  3037         -    int bSort = !!pOrderBy;       /* True if external sort required */
  3038         -    int bDist = !!pDistinct;      /* True if index cannot help with DISTINCT */
         3122  +    int bSort;                    /* True if external sort required */
         3123  +    int bDist;                    /* True if index cannot help with DISTINCT */
  3039   3124       int bLookup = 0;              /* True if not a covering index */
         3125  +    int nOBSat = 0;               /* Number of ORDER BY terms satisfied */
         3126  +    int nOrderBy;                 /* Number of ORDER BY terms */
  3040   3127       WhereTerm *pTerm;             /* A single term of the WHERE clause */
  3041   3128   #ifdef SQLITE_ENABLE_STAT3
  3042   3129       WhereTerm *pFirstTerm = 0;    /* First term matching the index */
  3043   3130   #endif
         3131  +
         3132  +    nOrderBy = p->pOrderBy ? p->pOrderBy->nExpr : 0;
         3133  +    bSort = nOrderBy>0 && (p->i==0 || p->aLevel[p->i-1].plan.nOBSat<nOrderBy);
         3134  +    bDist = p->i==0 && p->pDistinct!=0;
  3044   3135   
  3045   3136       /* Determine the values of nEq and nInMul */
  3046         -    for(nEq=0; nEq<pProbe->nColumn; nEq++){
         3137  +    for(nEq=nOrdered=0; nEq<pProbe->nColumn; nEq++){
  3047   3138         int j = pProbe->aiColumn[nEq];
  3048         -      pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
         3139  +      pTerm = findTerm(pWC, iCur, j, p->notReady, eqTermMask, pIdx);
  3049   3140         if( pTerm==0 ) break;
  3050   3141         wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
  3051   3142         testcase( pTerm->pWC!=pWC );
  3052   3143         if( pTerm->eOperator & WO_IN ){
  3053   3144           Expr *pExpr = pTerm->pExpr;
  3054   3145           wsFlags |= WHERE_COLUMN_IN;
  3055   3146           if( ExprHasProperty(pExpr, EP_xIsSelect) ){
................................................................................
  3058   3149             bInEst = 1;
  3059   3150           }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
  3060   3151             /* "x IN (value, value, ...)" */
  3061   3152             nInMul *= pExpr->x.pList->nExpr;
  3062   3153           }
  3063   3154         }else if( pTerm->eOperator & WO_ISNULL ){
  3064   3155           wsFlags |= WHERE_COLUMN_NULL;
         3156  +        if( nEq==nOrdered ) nOrdered++;
         3157  +      }else if( bSort && nEq==nOrdered && isOrderedTerm(p, pTerm, &bRev) ){
         3158  +        nOrdered++;
  3065   3159         }
  3066   3160   #ifdef SQLITE_ENABLE_STAT3
  3067   3161         if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
  3068   3162   #endif
  3069   3163         used |= pTerm->prereqRight;
  3070   3164       }
  3071   3165    
................................................................................
  3082   3176         testcase( wsFlags & WHERE_COLUMN_IN );
  3083   3177         testcase( wsFlags & WHERE_COLUMN_NULL );
  3084   3178         if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
  3085   3179           wsFlags |= WHERE_UNIQUE;
  3086   3180         }
  3087   3181       }else if( pProbe->bUnordered==0 ){
  3088   3182         int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
  3089         -      if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
  3090         -        WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
  3091         -        WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
         3183  +      if( findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
         3184  +        WhereTerm *pTop, *pBtm;
         3185  +        pTop = findTerm(pWC, iCur, j, p->notReady, WO_LT|WO_LE, pIdx);
         3186  +        pBtm = findTerm(pWC, iCur, j, p->notReady, WO_GT|WO_GE, pIdx);
  3092   3187           whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
  3093   3188           if( pTop ){
  3094   3189             nBound = 1;
  3095   3190             wsFlags |= WHERE_TOP_LIMIT;
  3096   3191             used |= pTop->prereqRight;
  3097   3192             testcase( pTop->pWC!=pWC );
  3098   3193           }
................................................................................
  3106   3201         }
  3107   3202       }
  3108   3203   
  3109   3204       /* If there is an ORDER BY clause and the index being considered will
  3110   3205       ** naturally scan rows in the required order, set the appropriate flags
  3111   3206       ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
  3112   3207       ** will scan rows in a different order, set the bSort variable.  */
  3113         -    if( isSortingIndex(
  3114         -          pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, nEq, wsFlags, &rev)
  3115         -    ){
  3116         -      bSort = 0;
  3117         -      wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
  3118         -      wsFlags |= (rev ? WHERE_REVERSE : 0);
         3208  +    assert( bRev>=0 && bRev<=2 );
         3209  +    if( bSort ){
         3210  +      testcase( bRev==0 );
         3211  +      testcase( bRev==1 );
         3212  +      testcase( bRev==2 );
         3213  +      nOBSat = isSortingIndex(p, pProbe, iCur, nOrdered,
         3214  +                              wsFlags, bRev&1, &bRev);
         3215  +      if( nOrderBy==nOBSat ){
         3216  +        bSort = 0;
         3217  +        wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
         3218  +      }
         3219  +      if( bRev & 1 ) wsFlags |= WHERE_REVERSE;
  3119   3220       }
  3120   3221   
  3121   3222       /* If there is a DISTINCT qualifier and this index will scan rows in
  3122   3223       ** order of the DISTINCT expressions, clear bDist and set the appropriate
  3123   3224       ** flags in wsFlags. */
  3124         -    if( isDistinctIndex(pParse, pWC, pProbe, iCur, pDistinct, nEq)
         3225  +    if( bDist
         3226  +     && isDistinctIndex(pParse, pWC, pProbe, iCur, p->pDistinct, nEq)
  3125   3227        && (wsFlags & WHERE_COLUMN_IN)==0
  3126   3228       ){
  3127   3229         bDist = 0;
  3128   3230         wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_DISTINCT;
  3129   3231       }
  3130   3232   
  3131   3233       /* If currently calculating the cost of using an index (not the IPK
  3132   3234       ** index), determine if all required column data may be obtained without 
  3133   3235       ** using the main table (i.e. if the index is a covering
  3134   3236       ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
  3135   3237       ** wsFlags. Otherwise, set the bLookup variable to true.  */
  3136         -    if( pIdx && wsFlags ){
         3238  +    if( pIdx ){
  3137   3239         Bitmask m = pSrc->colUsed;
  3138   3240         int j;
  3139   3241         for(j=0; j<pIdx->nColumn; j++){
  3140   3242           int x = pIdx->aiColumn[j];
  3141   3243           if( x<BMS-1 ){
  3142   3244             m &= ~(((Bitmask)1)<<x);
  3143   3245           }
................................................................................
  3194   3296       ** on one page and hence more pages have to be fetched.
  3195   3297       **
  3196   3298       ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
  3197   3299       ** not give us data on the relative sizes of table and index records.
  3198   3300       ** So this computation assumes table records are about twice as big
  3199   3301       ** as index records
  3200   3302       */
  3201         -    if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
         3303  +    if( (wsFlags&~WHERE_REVERSE)==WHERE_IDX_ONLY
         3304  +     && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
         3305  +     && sqlite3GlobalConfig.bUseCis
         3306  +     && OptimizationEnabled(pParse->db, SQLITE_CoverIdxScan)
         3307  +    ){
         3308  +      /* This index is not useful for indexing, but it is a covering index.
         3309  +      ** A full-scan of the index might be a little faster than a full-scan
         3310  +      ** of the table, so give this case a cost slightly less than a table
         3311  +      ** scan. */
         3312  +      cost = aiRowEst[0]*3 + pProbe->nColumn;
         3313  +      wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
         3314  +    }else if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
  3202   3315         /* The cost of a full table scan is a number of move operations equal
  3203   3316         ** to the number of rows in the table.
  3204   3317         **
  3205   3318         ** We add an additional 4x penalty to full table scans.  This causes
  3206   3319         ** the cost function to err on the side of choosing an index over
  3207   3320         ** choosing a full scan.  This 4x full-scan penalty is an arguable
  3208   3321         ** decision and one which we expect to revisit in the future.  But
  3209   3322         ** it seems to be working well enough at the moment.
  3210   3323         */
  3211   3324         cost = aiRowEst[0]*4;
         3325  +      wsFlags &= ~WHERE_IDX_ONLY;
  3212   3326       }else{
  3213   3327         log10N = estLog(aiRowEst[0]);
  3214   3328         cost = nRow;
  3215   3329         if( pIdx ){
  3216   3330           if( bLookup ){
  3217   3331             /* For an index lookup followed by a table lookup:
  3218   3332             **    nInMul index searches to find the start of each index range
................................................................................
  3239   3353       /* Add in the estimated cost of sorting the result.  Actual experimental
  3240   3354       ** measurements of sorting performance in SQLite show that sorting time
  3241   3355       ** adds C*N*log10(N) to the cost, where N is the number of rows to be 
  3242   3356       ** sorted and C is a factor between 1.95 and 4.3.  We will split the
  3243   3357       ** difference and select C of 3.0.
  3244   3358       */
  3245   3359       if( bSort ){
  3246         -      cost += nRow*estLog(nRow)*3;
         3360  +      cost += nRow*estLog(nRow*(nOrderBy - nOBSat)/nOrderBy)*3;
  3247   3361       }
  3248   3362       if( bDist ){
  3249   3363         cost += nRow*estLog(nRow)*3;
  3250   3364       }
  3251   3365   
  3252   3366       /**** Cost of using this index has now been computed ****/
  3253   3367   
................................................................................
  3263   3377       ** mask will only have one bit set - the bit for the current table.
  3264   3378       ** The notValid mask, on the other hand, always has all bits set for
  3265   3379       ** tables that are not in outer loops.  If notReady is used here instead
  3266   3380       ** of notValid, then a optimal index that depends on inner joins loops
  3267   3381       ** might be selected even when there exists an optimal index that has
  3268   3382       ** no such dependency.
  3269   3383       */
  3270         -    if( nRow>2 && cost<=pCost->rCost ){
         3384  +    if( nRow>2 && cost<=p->cost.rCost ){
  3271   3385         int k;                       /* Loop counter */
  3272   3386         int nSkipEq = nEq;           /* Number of == constraints to skip */
  3273   3387         int nSkipRange = nBound;     /* Number of < constraints to skip */
  3274   3388         Bitmask thisTab;             /* Bitmap for pSrc */
  3275   3389   
  3276   3390         thisTab = getMask(pWC->pMaskSet, iCur);
  3277   3391         for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){
  3278   3392           if( pTerm->wtFlags & TERM_VIRTUAL ) continue;
  3279         -        if( (pTerm->prereqAll & notValid)!=thisTab ) continue;
         3393  +        if( (pTerm->prereqAll & p->notValid)!=thisTab ) continue;
  3280   3394           if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){
  3281   3395             if( nSkipEq ){
  3282   3396               /* Ignore the first nEq equality matches since the index
  3283   3397               ** has already accounted for these */
  3284   3398               nSkipEq--;
  3285   3399             }else{
  3286   3400               /* Assume each additional equality match reduces the result
................................................................................
  3307   3421           }
  3308   3422         }
  3309   3423         if( nRow<2 ) nRow = 2;
  3310   3424       }
  3311   3425   
  3312   3426   
  3313   3427       WHERETRACE((
  3314         -      "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
  3315         -      "         notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
         3428  +      "%s(%s):\n"
         3429  +      "    nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%08x\n"
         3430  +      "    notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f\n"
         3431  +      "    used=0x%llx nOrdered=%d nOBSat=%d\n",
  3316   3432         pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), 
  3317   3433         nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
  3318         -      notReady, log10N, nRow, cost, used
         3434  +      p->notReady, log10N, nRow, cost, used, nOrdered, nOBSat
  3319   3435       ));
  3320   3436   
  3321   3437       /* If this index is the best we have seen so far, then record this
  3322   3438       ** index and its cost in the pCost structure.
  3323   3439       */
  3324   3440       if( (!pIdx || wsFlags)
  3325         -     && (cost<pCost->rCost || (cost<=pCost->rCost && nRow<pCost->plan.nRow))
         3441  +     && (cost<p->cost.rCost || (cost<=p->cost.rCost && nRow<p->cost.plan.nRow))
  3326   3442       ){
  3327         -      pCost->rCost = cost;
  3328         -      pCost->used = used;
  3329         -      pCost->plan.nRow = nRow;
  3330         -      pCost->plan.wsFlags = (wsFlags&wsFlagMask);
  3331         -      pCost->plan.nEq = nEq;
  3332         -      pCost->plan.u.pIdx = pIdx;
         3443  +      p->cost.rCost = cost;
         3444  +      p->cost.used = used;
         3445  +      p->cost.plan.nRow = nRow;
         3446  +      p->cost.plan.wsFlags = (wsFlags&wsFlagMask);
         3447  +      p->cost.plan.nEq = nEq;
         3448  +      p->cost.plan.nOBSat = nOBSat;
         3449  +      p->cost.plan.u.pIdx = pIdx;
  3333   3450       }
  3334   3451   
  3335   3452       /* If there was an INDEXED BY clause, then only that one index is
  3336   3453       ** considered. */
  3337   3454       if( pSrc->pIndex ) break;
  3338   3455   
  3339   3456       /* Reset masks for the next index in the loop */
................................................................................
  3342   3459     }
  3343   3460   
  3344   3461     /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
  3345   3462     ** is set, then reverse the order that the index will be scanned
  3346   3463     ** in. This is used for application testing, to help find cases
  3347   3464     ** where application behaviour depends on the (undefined) order that
  3348   3465     ** SQLite outputs rows in in the absence of an ORDER BY clause.  */
  3349         -  if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
  3350         -    pCost->plan.wsFlags |= WHERE_REVERSE;
         3466  +  if( !p->pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){
         3467  +    p->cost.plan.wsFlags |= WHERE_REVERSE;
  3351   3468     }
  3352   3469   
  3353         -  assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 );
  3354         -  assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 );
         3470  +  assert( p->pOrderBy || (p->cost.plan.wsFlags&WHERE_ORDERBY)==0 );
         3471  +  assert( p->cost.plan.u.pIdx==0 || (p->cost.plan.wsFlags&WHERE_ROWID_EQ)==0 );
  3355   3472     assert( pSrc->pIndex==0 
  3356         -       || pCost->plan.u.pIdx==0 
  3357         -       || pCost->plan.u.pIdx==pSrc->pIndex 
         3473  +       || p->cost.plan.u.pIdx==0 
         3474  +       || p->cost.plan.u.pIdx==pSrc->pIndex 
  3358   3475     );
  3359   3476   
  3360   3477     WHERETRACE(("best index is: %s\n", 
  3361         -    ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" : 
  3362         -         pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk")
         3478  +    ((p->cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" : 
         3479  +         p->cost.plan.u.pIdx ? p->cost.plan.u.pIdx->zName : "ipk")
  3363   3480     ));
  3364   3481     
  3365         -  bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
  3366         -  bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost);
  3367         -  pCost->plan.wsFlags |= eqTermMask;
         3482  +  bestOrClauseIndex(p);
         3483  +  bestAutomaticIndex(p);
         3484  +  p->cost.plan.wsFlags |= eqTermMask;
  3368   3485   }
  3369   3486   
  3370   3487   /*
  3371   3488   ** Find the query plan for accessing table pSrc->pTab. Write the
  3372   3489   ** best query plan and its cost into the WhereCost object supplied 
  3373   3490   ** as the last parameter. This function may calculate the cost of
  3374   3491   ** both real and virtual table scans.
         3492  +**
         3493  +** This function does not take ORDER BY or DISTINCT into account.  Nor
         3494  +** does it remember the virtual table query plan.  All it does is compute
         3495  +** the cost while determining if an OR optimization is applicable.  The
         3496  +** details will be reconsidered later if the optimization is found to be
         3497  +** applicable.
  3375   3498   */
  3376         -static void bestIndex(
  3377         -  Parse *pParse,              /* The parsing context */
  3378         -  WhereClause *pWC,           /* The WHERE clause */
  3379         -  struct SrcList_item *pSrc,  /* The FROM clause term to search */
  3380         -  Bitmask notReady,           /* Mask of cursors not available for indexing */
  3381         -  Bitmask notValid,           /* Cursors not available for any purpose */
  3382         -  ExprList *pOrderBy,         /* The ORDER BY clause */
  3383         -  WhereCost *pCost            /* Lowest cost query plan */
  3384         -){
         3499  +static void bestIndex(WhereBestIdx *p){
  3385   3500   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3386         -  if( IsVirtual(pSrc->pTab) ){
  3387         -    sqlite3_index_info *p = 0;
  3388         -    bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
  3389         -    if( p->needToFreeIdxStr ){
  3390         -      sqlite3_free(p->idxStr);
         3501  +  if( IsVirtual(p->pSrc->pTab) ){
         3502  +    sqlite3_index_info *pIdxInfo = 0;
         3503  +    p->ppIdxInfo = &pIdxInfo;
         3504  +    bestVirtualIndex(p);
         3505  +    if( pIdxInfo->needToFreeIdxStr ){
         3506  +      sqlite3_free(pIdxInfo->idxStr);
  3391   3507       }
  3392         -    sqlite3DbFree(pParse->db, p);
         3508  +    sqlite3DbFree(p->pParse->db, pIdxInfo);
  3393   3509     }else
  3394   3510   #endif
  3395   3511     {
  3396         -    bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
         3512  +    bestBtreeIndex(p);
  3397   3513     }
  3398   3514   }
  3399   3515   
  3400   3516   /*
  3401   3517   ** Disable a term in the WHERE clause.  Except, do not disable the term
  3402   3518   ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  3403   3519   ** or USING clause of that join.
................................................................................
  4248   4364         pLevel->op = OP_Noop;
  4249   4365       }else if( bRev ){
  4250   4366         pLevel->op = OP_Prev;
  4251   4367       }else{
  4252   4368         pLevel->op = OP_Next;
  4253   4369       }
  4254   4370       pLevel->p1 = iIdxCur;
         4371  +    if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
         4372  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         4373  +    }else{
         4374  +      assert( pLevel->p5==0 );
         4375  +    }
  4255   4376     }else
  4256   4377   
  4257   4378   #ifndef SQLITE_OMIT_OR_OPTIMIZATION
  4258   4379     if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
  4259   4380       /* Case 4:  Two or more separately indexed terms connected by OR
  4260   4381       **
  4261   4382       ** Example:
................................................................................
  4642   4763   **        move the row2 cursor to a null row
  4643   4764   **        goto start
  4644   4765   **      fi
  4645   4766   **    end
  4646   4767   **
  4647   4768   ** ORDER BY CLAUSE PROCESSING
  4648   4769   **
  4649         -** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
         4770  +** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
  4650   4771   ** if there is one.  If there is no ORDER BY clause or if this routine
  4651         -** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
         4772  +** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
  4652   4773   **
  4653   4774   ** If an index can be used so that the natural output order of the table
  4654   4775   ** scan is correct for the ORDER BY clause, then that index is used and
  4655         -** *ppOrderBy is set to NULL.  This is an optimization that prevents an
  4656         -** unnecessary sort of the result set if an index appropriate for the
  4657         -** ORDER BY clause already exists.
         4776  +** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr.  This
         4777  +** is an optimization that prevents an unnecessary sort of the result set
         4778  +** if an index appropriate for the ORDER BY clause already exists.
  4658   4779   **
  4659   4780   ** If the where clause loops cannot be arranged to provide the correct
  4660         -** output order, then the *ppOrderBy is unchanged.
         4781  +** output order, then WhereInfo.nOBSat is 0.
  4661   4782   */
  4662   4783   WhereInfo *sqlite3WhereBegin(
  4663   4784     Parse *pParse,        /* The parser context */
  4664   4785     SrcList *pTabList,    /* A list of all tables to be scanned */
  4665   4786     Expr *pWhere,         /* The WHERE clause */
  4666         -  ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
         4787  +  ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
  4667   4788     ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
  4668   4789     u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
  4669   4790     int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
  4670   4791   ){
  4671         -  int i;                     /* Loop counter */
  4672   4792     int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
  4673   4793     int nTabList;              /* Number of elements in pTabList */
  4674   4794     WhereInfo *pWInfo;         /* Will become the return value of this function */
  4675   4795     Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
  4676   4796     Bitmask notReady;          /* Cursors that are not yet positioned */
         4797  +  WhereBestIdx sWBI;         /* Best index search context */
  4677   4798     WhereMaskSet *pMaskSet;    /* The expression mask set */
  4678         -  WhereClause *pWC;               /* Decomposition of the WHERE clause */
  4679         -  struct SrcList_item *pTabItem;  /* A single entry from pTabList */
  4680         -  WhereLevel *pLevel;             /* A single level in the pWInfo list */
  4681         -  int iFrom;                      /* First unused FROM clause element */
         4799  +  WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
         4800  +  int iFrom;                 /* First unused FROM clause element */
  4682   4801     int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
         4802  +  int ii;                    /* Loop counter */
  4683   4803     sqlite3 *db;               /* Database connection */
  4684   4804   
         4805  +
         4806  +  /* Variable initialization */
         4807  +  memset(&sWBI, 0, sizeof(sWBI));
         4808  +  sWBI.pParse = pParse;
         4809  +
  4685   4810     /* The number of tables in the FROM clause is limited by the number of
  4686   4811     ** bits in a Bitmask 
  4687   4812     */
  4688   4813     testcase( pTabList->nSrc==BMS );
  4689   4814     if( pTabList->nSrc>BMS ){
  4690   4815       sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
  4691   4816       return 0;
................................................................................
  4717   4842       pWInfo = 0;
  4718   4843       goto whereBeginError;
  4719   4844     }
  4720   4845     pWInfo->nLevel = nTabList;
  4721   4846     pWInfo->pParse = pParse;
  4722   4847     pWInfo->pTabList = pTabList;
  4723   4848     pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
  4724         -  pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
         4849  +  pWInfo->pWC = sWBI.pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo];
  4725   4850     pWInfo->wctrlFlags = wctrlFlags;
  4726   4851     pWInfo->savedNQueryLoop = pParse->nQueryLoop;
  4727         -  pMaskSet = (WhereMaskSet*)&pWC[1];
         4852  +  pMaskSet = (WhereMaskSet*)&sWBI.pWC[1];
         4853  +  sWBI.aLevel = pWInfo->a;
  4728   4854   
  4729   4855     /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
  4730   4856     ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
  4731         -  if( db->flags & SQLITE_DistinctOpt ) pDistinct = 0;
         4857  +  if( OptimizationDisabled(db, SQLITE_DistinctOpt) ) pDistinct = 0;
  4732   4858   
  4733   4859     /* Split the WHERE clause into separate subexpressions where each
  4734   4860     ** subexpression is separated by an AND operator.
  4735   4861     */
  4736   4862     initMaskSet(pMaskSet);
  4737         -  whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
         4863  +  whereClauseInit(sWBI.pWC, pParse, pMaskSet, wctrlFlags);
  4738   4864     sqlite3ExprCodeConstants(pParse, pWhere);
  4739         -  whereSplit(pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
         4865  +  whereSplit(sWBI.pWC, pWhere, TK_AND);   /* IMP: R-15842-53296 */
  4740   4866       
  4741   4867     /* Special case: a WHERE clause that is constant.  Evaluate the
  4742   4868     ** expression and either jump over all of the code or fall thru.
  4743   4869     */
  4744   4870     if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){
  4745   4871       sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL);
  4746   4872       pWhere = 0;
................................................................................
  4763   4889     ** with virtual tables.
  4764   4890     **
  4765   4891     ** Note that bitmasks are created for all pTabList->nSrc tables in
  4766   4892     ** pTabList, not just the first nTabList tables.  nTabList is normally
  4767   4893     ** equal to pTabList->nSrc but might be shortened to 1 if the
  4768   4894     ** WHERE_ONETABLE_ONLY flag is set.
  4769   4895     */
  4770         -  assert( pWC->vmask==0 && pMaskSet->n==0 );
  4771         -  for(i=0; i<pTabList->nSrc; i++){
  4772         -    createMask(pMaskSet, pTabList->a[i].iCursor);
         4896  +  assert( sWBI.pWC->vmask==0 && pMaskSet->n==0 );
         4897  +  for(ii=0; ii<pTabList->nSrc; ii++){
         4898  +    createMask(pMaskSet, pTabList->a[ii].iCursor);
  4773   4899   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4774         -    if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){
  4775         -      pWC->vmask |= ((Bitmask)1 << i);
         4900  +    if( ALWAYS(pTabList->a[ii].pTab) && IsVirtual(pTabList->a[ii].pTab) ){
         4901  +      sWBI.pWC->vmask |= ((Bitmask)1 << ii);
  4776   4902       }
  4777   4903   #endif
  4778   4904     }
  4779   4905   #ifndef NDEBUG
  4780   4906     {
  4781   4907       Bitmask toTheLeft = 0;
  4782         -    for(i=0; i<pTabList->nSrc; i++){
  4783         -      Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor);
         4908  +    for(ii=0; ii<pTabList->nSrc; ii++){
         4909  +      Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
  4784   4910         assert( (m-1)==toTheLeft );
  4785   4911         toTheLeft |= m;
  4786   4912       }
  4787   4913     }
  4788   4914   #endif
  4789   4915   
  4790   4916     /* Analyze all of the subexpressions.  Note that exprAnalyze() might
  4791   4917     ** add new virtual terms onto the end of the WHERE clause.  We do not
  4792   4918     ** want to analyze these virtual terms, so start analyzing at the end
  4793   4919     ** and work forward so that the added virtual terms are never processed.
  4794   4920     */
  4795         -  exprAnalyzeAll(pTabList, pWC);
         4921  +  exprAnalyzeAll(pTabList, sWBI.pWC);
  4796   4922     if( db->mallocFailed ){
  4797   4923       goto whereBeginError;
  4798   4924     }
  4799   4925   
  4800   4926     /* Check if the DISTINCT qualifier, if there is one, is redundant. 
  4801   4927     ** If it is, then set pDistinct to NULL and WhereInfo.eDistinct to
  4802   4928     ** WHERE_DISTINCT_UNIQUE to tell the caller to ignore the DISTINCT.
  4803   4929     */
  4804         -  if( pDistinct && isDistinctRedundant(pParse, pTabList, pWC, pDistinct) ){
         4930  +  if( pDistinct && isDistinctRedundant(pParse, pTabList, sWBI.pWC, pDistinct) ){
  4805   4931       pDistinct = 0;
  4806   4932       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
  4807   4933     }
  4808   4934   
  4809   4935     /* Chose the best index to use for each table in the FROM clause.
  4810   4936     **
  4811   4937     ** This loop fills in the following fields:
................................................................................
  4817   4943     **   pWInfo->a[].iTabCur   The VDBE cursor for the database table
  4818   4944     **   pWInfo->a[].iIdxCur   The VDBE cursor for the index
  4819   4945     **   pWInfo->a[].pTerm     When wsFlags==WO_OR, the OR-clause term
  4820   4946     **
  4821   4947     ** This loop also figures out the nesting order of tables in the FROM
  4822   4948     ** clause.
  4823   4949     */
  4824         -  notReady = ~(Bitmask)0;
         4950  +  sWBI.notValid = ~(Bitmask)0;
         4951  +  sWBI.pOrderBy = pOrderBy;
         4952  +  sWBI.n = nTabList;
         4953  +  sWBI.pDistinct = pDistinct;
  4825   4954     andFlags = ~0;
  4826   4955     WHERETRACE(("*** Optimizer Start ***\n"));
  4827         -  for(i=iFrom=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
         4956  +  for(sWBI.i=iFrom=0, pLevel=pWInfo->a; sWBI.i<nTabList; sWBI.i++, pLevel++){
  4828   4957       WhereCost bestPlan;         /* Most efficient plan seen so far */
  4829   4958       Index *pIdx;                /* Index for FROM table at pTabItem */
  4830   4959       int j;                      /* For looping over FROM tables */
  4831   4960       int bestJ = -1;             /* The value of j */
  4832   4961       Bitmask m;                  /* Bitmask value for j or bestJ */
  4833   4962       int isOptimal;              /* Iterator for optimal/non-optimal search */
  4834   4963       int nUnconstrained;         /* Number tables without INDEXED BY */
  4835   4964       Bitmask notIndexed;         /* Mask of tables that cannot use an index */
  4836   4965   
  4837   4966       memset(&bestPlan, 0, sizeof(bestPlan));
  4838   4967       bestPlan.rCost = SQLITE_BIG_DBL;
  4839         -    WHERETRACE(("*** Begin search for loop %d ***\n", i));
         4968  +    WHERETRACE(("*** Begin search for loop %d ***\n", sWBI.i));
  4840   4969   
  4841   4970       /* Loop through the remaining entries in the FROM clause to find the
  4842   4971       ** next nested loop. The loop tests all FROM clause entries
  4843   4972       ** either once or twice. 
  4844   4973       **
  4845   4974       ** The first test is always performed if there are two or more entries
  4846   4975       ** remaining and never performed if there is only one FROM clause entry
................................................................................
  4848   4977       ** this context an optimal scan is one that uses the same strategy
  4849   4978       ** for the given FROM clause entry as would be selected if the entry
  4850   4979       ** were used as the innermost nested loop.  In other words, a table
  4851   4980       ** is chosen such that the cost of running that table cannot be reduced
  4852   4981       ** by waiting for other tables to run first.  This "optimal" test works
  4853   4982       ** by first assuming that the FROM clause is on the inner loop and finding
  4854   4983       ** its query plan, then checking to see if that query plan uses any
  4855         -    ** other FROM clause terms that are notReady.  If no notReady terms are
  4856         -    ** used then the "optimal" query plan works.
         4984  +    ** other FROM clause terms that are sWBI.notValid.  If no notValid terms
         4985  +    ** are used then the "optimal" query plan works.
  4857   4986       **
  4858   4987       ** Note that the WhereCost.nRow parameter for an optimal scan might
  4859   4988       ** not be as small as it would be if the table really were the innermost
  4860   4989       ** join.  The nRow value can be reduced by WHERE clause constraints
  4861   4990       ** that do not use indices.  But this nRow reduction only happens if the
  4862   4991       ** table really is the innermost join.  
  4863   4992       **
................................................................................
  4880   5009       ** as the cost of a linear scan through table t1, a simple greedy 
  4881   5010       ** algorithm may choose to use t2 for the outer loop, which is a much
  4882   5011       ** costlier approach.
  4883   5012       */
  4884   5013       nUnconstrained = 0;
  4885   5014       notIndexed = 0;
  4886   5015       for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
  4887         -      Bitmask mask;             /* Mask of tables not yet ready */
  4888         -      for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
         5016  +      for(j=iFrom, sWBI.pSrc=&pTabList->a[j]; j<nTabList; j++, sWBI.pSrc++){
  4889   5017           int doNotReorder;    /* True if this table should not be reordered */
  4890         -        WhereCost sCost;     /* Cost information from best[Virtual]Index() */
  4891         -        ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
  4892         -        ExprList *pDist;     /* DISTINCT clause for index to optimize */
  4893   5018     
  4894         -        doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
         5019  +        doNotReorder =  (sWBI.pSrc->jointype & (JT_LEFT|JT_CROSS))!=0;
  4895   5020           if( j!=iFrom && doNotReorder ) break;
  4896         -        m = getMask(pMaskSet, pTabItem->iCursor);
  4897         -        if( (m & notReady)==0 ){
         5021  +        m = getMask(pMaskSet, sWBI.pSrc->iCursor);
         5022  +        if( (m & sWBI.notValid)==0 ){
  4898   5023             if( j==iFrom ) iFrom++;
  4899   5024             continue;
  4900   5025           }
  4901         -        mask = (isOptimal ? m : notReady);
  4902         -        pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
  4903         -        pDist = (i==0 ? pDistinct : 0);
  4904         -        if( pTabItem->pIndex==0 ) nUnconstrained++;
         5026  +        sWBI.notReady = (isOptimal ? m : sWBI.notValid);
         5027  +        if( sWBI.pSrc->pIndex==0 ) nUnconstrained++;
  4905   5028     
  4906   5029           WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
  4907   5030                       j, isOptimal));
  4908         -        assert( pTabItem->pTab );
         5031  +        assert( sWBI.pSrc->pTab );
  4909   5032   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4910         -        if( IsVirtual(pTabItem->pTab) ){
  4911         -          sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
  4912         -          bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
  4913         -                           &sCost, pp);
         5033  +        if( IsVirtual(sWBI.pSrc->pTab) ){
         5034  +          sWBI.ppIdxInfo = &pWInfo->a[j].pIdxInfo;
         5035  +          bestVirtualIndex(&sWBI);
  4914   5036           }else 
  4915   5037   #endif
  4916   5038           {
  4917         -          bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
  4918         -              pDist, &sCost);
         5039  +          bestBtreeIndex(&sWBI);
  4919   5040           }
  4920         -        assert( isOptimal || (sCost.used&notReady)==0 );
         5041  +        assert( isOptimal || (sWBI.cost.used&sWBI.notValid)==0 );
  4921   5042   
  4922   5043           /* If an INDEXED BY clause is present, then the plan must use that
  4923   5044           ** index if it uses any index at all */
  4924         -        assert( pTabItem->pIndex==0 
  4925         -                  || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
  4926         -                  || sCost.plan.u.pIdx==pTabItem->pIndex );
         5045  +        assert( sWBI.pSrc->pIndex==0 
         5046  +                  || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
         5047  +                  || sWBI.cost.plan.u.pIdx==sWBI.pSrc->pIndex );
  4927   5048   
  4928         -        if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
         5049  +        if( isOptimal && (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){
  4929   5050             notIndexed |= m;
  4930   5051           }
  4931   5052   
  4932   5053           /* Conditions under which this table becomes the best so far:
  4933   5054           **
  4934   5055           **   (1) The table must not depend on other tables that have not
  4935         -        **       yet run.
         5056  +        **       yet run.  (In other words, it must not depend on tables
         5057  +        **       in inner loops.)
  4936   5058           **
  4937   5059           **   (2) A full-table-scan plan cannot supercede indexed plan unless
  4938   5060           **       the full-table-scan is an "optimal" plan as defined above.
  4939   5061           **
  4940   5062           **   (3) All tables have an INDEXED BY clause or this table lacks an
  4941   5063           **       INDEXED BY clause or this table uses the specific
  4942   5064           **       index specified by its INDEXED BY clause.  This rule ensures
................................................................................
  4945   5067           **       will be detected and relayed back to the application later.
  4946   5068           **       The NEVER() comes about because rule (2) above prevents
  4947   5069           **       An indexable full-table-scan from reaching rule (3).
  4948   5070           **
  4949   5071           **   (4) The plan cost must be lower than prior plans or else the
  4950   5072           **       cost must be the same and the number of rows must be lower.
  4951   5073           */
  4952         -        if( (sCost.used&notReady)==0                       /* (1) */
  4953         -            && (bestJ<0 || (notIndexed&m)!=0               /* (2) */
         5074  +        if( (sWBI.cost.used&sWBI.notValid)==0                    /* (1) */
         5075  +            && (bestJ<0 || (notIndexed&m)!=0                     /* (2) */
  4954   5076                   || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0
  4955         -                || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
  4956         -            && (nUnconstrained==0 || pTabItem->pIndex==0   /* (3) */
  4957         -                || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
  4958         -            && (bestJ<0 || sCost.rCost<bestPlan.rCost      /* (4) */
  4959         -                || (sCost.rCost<=bestPlan.rCost 
  4960         -                 && sCost.plan.nRow<bestPlan.plan.nRow))
         5077  +                || (sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)
         5078  +            && (nUnconstrained==0 || sWBI.pSrc->pIndex==0        /* (3) */
         5079  +                || NEVER((sWBI.cost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0))
         5080  +            && (bestJ<0 || sWBI.cost.rCost<bestPlan.rCost        /* (4) */
         5081  +                || (sWBI.cost.rCost<=bestPlan.rCost 
         5082  +                 && sWBI.cost.plan.nRow<bestPlan.plan.nRow))
  4961   5083           ){
  4962   5084             WHERETRACE(("=== table %d is best so far"
  4963         -                      " with cost=%g and nRow=%g\n",
  4964         -                      j, sCost.rCost, sCost.plan.nRow));
  4965         -          bestPlan = sCost;
         5085  +                      " with cost=%.1f, nRow=%.1f, nOBSat=%d\n",
         5086  +                      j, sWBI.cost.rCost, sWBI.cost.plan.nRow,
         5087  +                      sWBI.cost.plan.nOBSat));
         5088  +          bestPlan = sWBI.cost;
  4966   5089             bestJ = j;
  4967   5090           }
  4968   5091           if( doNotReorder ) break;
  4969   5092         }
  4970   5093       }
  4971   5094       assert( bestJ>=0 );
  4972         -    assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
  4973         -    WHERETRACE(("*** Optimizer selects table %d for loop %d"
  4974         -                " with cost=%g and nRow=%g\n",
  4975         -                bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
  4976         -    /* The ALWAYS() that follows was added to hush up clang scan-build */
  4977         -    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
  4978         -      *ppOrderBy = 0;
         5095  +    assert( sWBI.notValid & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
         5096  +    WHERETRACE(("*** Optimizer selects table %d for loop %d with:\n"
         5097  +                "    cost=%.1f, nRow=%.1f, nOBSat=%d wsFlags=0x%08x\n",
         5098  +                bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow,
         5099  +                bestPlan.plan.nOBSat, bestPlan.plan.wsFlags));
         5100  +    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
         5101  +      pWInfo->nOBSat = pOrderBy->nExpr;
  4979   5102       }
  4980   5103       if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
  4981   5104         assert( pWInfo->eDistinct==0 );
  4982   5105         pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
  4983   5106       }
  4984   5107       andFlags &= bestPlan.plan.wsFlags;
  4985   5108       pLevel->plan = bestPlan.plan;
................................................................................
  4992   5115           pLevel->iIdxCur = iIdxCur;
  4993   5116         }else{
  4994   5117           pLevel->iIdxCur = pParse->nTab++;
  4995   5118         }
  4996   5119       }else{
  4997   5120         pLevel->iIdxCur = -1;
  4998   5121       }
  4999         -    notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
         5122  +    sWBI.notValid &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor);
  5000   5123       pLevel->iFrom = (u8)bestJ;
  5001   5124       if( bestPlan.plan.nRow>=(double)1 ){
  5002   5125         pParse->nQueryLoop *= bestPlan.plan.nRow;
  5003   5126       }
  5004   5127   
  5005   5128       /* Check that if the table scanned by this loop iteration had an
  5006   5129       ** INDEXED BY clause attached to it, that the named index is being
................................................................................
  5024   5147     if( pParse->nErr || db->mallocFailed ){
  5025   5148       goto whereBeginError;
  5026   5149     }
  5027   5150   
  5028   5151     /* If the total query only selects a single row, then the ORDER BY
  5029   5152     ** clause is irrelevant.
  5030   5153     */
  5031         -  if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
  5032         -    *ppOrderBy = 0;
         5154  +  if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
         5155  +    pWInfo->nOBSat = pOrderBy->nExpr;
  5033   5156     }
  5034   5157   
  5035   5158     /* If the caller is an UPDATE or DELETE statement that is requesting
  5036   5159     ** to use a one-pass algorithm, determine if this is appropriate.
  5037   5160     ** The one-pass algorithm only works if the WHERE clause constraints
  5038   5161     ** the statement to update a single row.
  5039   5162     */
................................................................................
  5045   5168   
  5046   5169     /* Open all tables in the pTabList and any indices selected for
  5047   5170     ** searching those tables.
  5048   5171     */
  5049   5172     sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
  5050   5173     notReady = ~(Bitmask)0;
  5051   5174     pWInfo->nRowOut = (double)1;
  5052         -  for(i=0, pLevel=pWInfo->a; i<nTabList; i++, pLevel++){
         5175  +  for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
  5053   5176       Table *pTab;     /* Table to open */
  5054   5177       int iDb;         /* Index of database containing table/index */
         5178  +    struct SrcList_item *pTabItem;
  5055   5179   
  5056   5180       pTabItem = &pTabList->a[pLevel->iFrom];
  5057   5181       pTab = pTabItem->pTab;
  5058   5182       pLevel->iTabCur = pTabItem->iCursor;
  5059   5183       pWInfo->nRowOut *= pLevel->plan.nRow;
  5060   5184       iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  5061   5185       if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
................................................................................
  5083   5207           assert( n<=pTab->nCol );
  5084   5208         }
  5085   5209       }else{
  5086   5210         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
  5087   5211       }
  5088   5212   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
  5089   5213       if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){
  5090         -      constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel);
         5214  +      constructAutomaticIndex(pParse, sWBI.pWC, pTabItem, notReady, pLevel);
  5091   5215       }else
  5092   5216   #endif
  5093   5217       if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
  5094   5218         Index *pIx = pLevel->plan.u.pIdx;
  5095   5219         KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
  5096   5220         int iIndexCur = pLevel->iIdxCur;
  5097   5221         assert( pIx->pSchema==pTab->pSchema );
  5098   5222         assert( iIndexCur>=0 );
  5099   5223         sqlite3VdbeAddOp4(v, OP_OpenRead, iIndexCur, pIx->tnum, iDb,
  5100   5224                           (char*)pKey, P4_KEYINFO_HANDOFF);
  5101   5225         VdbeComment((v, "%s", pIx->zName));
  5102   5226       }
  5103   5227       sqlite3CodeVerifySchema(pParse, iDb);
  5104         -    notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor);
         5228  +    notReady &= ~getMask(sWBI.pWC->pMaskSet, pTabItem->iCursor);
  5105   5229     }
  5106   5230     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
  5107   5231     if( db->mallocFailed ) goto whereBeginError;
  5108   5232   
  5109   5233     /* Generate the code to do the search.  Each iteration of the for
  5110   5234     ** loop below generates code for a single nested loop of the VM
  5111   5235     ** program.
  5112   5236     */
  5113   5237     notReady = ~(Bitmask)0;
  5114         -  for(i=0; i<nTabList; i++){
  5115         -    pLevel = &pWInfo->a[i];
  5116         -    explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
  5117         -    notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
         5238  +  for(ii=0; ii<nTabList; ii++){
         5239  +    pLevel = &pWInfo->a[ii];
         5240  +    explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
         5241  +    notReady = codeOneLoopStart(pWInfo, ii, wctrlFlags, notReady);
  5118   5242       pWInfo->iContinue = pLevel->addrCont;
  5119   5243     }
  5120   5244   
  5121   5245   #ifdef SQLITE_TEST  /* For testing and debugging use only */
  5122   5246     /* Record in the query plan information about the current table
  5123   5247     ** and the index used to access it (if any).  If the table itself
  5124   5248     ** is not used, its name is just '{}'.  If no index is used
  5125   5249     ** the index is listed as "{}".  If the primary key is used the
  5126   5250     ** index name is '*'.
  5127   5251     */
  5128         -  for(i=0; i<nTabList; i++){
         5252  +  for(ii=0; ii<nTabList; ii++){
  5129   5253       char *z;
  5130   5254       int n;
  5131         -    pLevel = &pWInfo->a[i];
         5255  +    int w;
         5256  +    struct SrcList_item *pTabItem;
         5257  +
         5258  +    pLevel = &pWInfo->a[ii];
         5259  +    w = pLevel->plan.wsFlags;
  5132   5260       pTabItem = &pTabList->a[pLevel->iFrom];
  5133   5261       z = pTabItem->zAlias;
  5134   5262       if( z==0 ) z = pTabItem->pTab->zName;
  5135   5263       n = sqlite3Strlen30(z);
  5136   5264       if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
  5137         -      if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
         5265  +      if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
  5138   5266           memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
  5139   5267           nQPlan += 2;
  5140   5268         }else{
  5141   5269           memcpy(&sqlite3_query_plan[nQPlan], z, n);
  5142   5270           nQPlan += n;
  5143   5271         }
  5144   5272         sqlite3_query_plan[nQPlan++] = ' ';
  5145   5273       }
  5146         -    testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ );
  5147         -    testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE );
  5148         -    if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
         5274  +    testcase( w & WHERE_ROWID_EQ );
         5275  +    testcase( w & WHERE_ROWID_RANGE );
         5276  +    if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
  5149   5277         memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
  5150   5278         nQPlan += 2;
  5151         -    }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
         5279  +    }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
  5152   5280         n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
  5153   5281         if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
  5154   5282           memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
  5155   5283           nQPlan += n;
  5156   5284           sqlite3_query_plan[nQPlan++] = ' ';
  5157   5285         }
  5158   5286       }else{

Changes to test/analyze6.test.

    57     57   # The lowest cost plan is to scan CAT and for each integer there, do a single
    58     58   # lookup of the first corresponding entry in EV then read off the equal values
    59     59   # in EV.  (Prior to the 2011-03-04 enhancement to where.c, this query would
    60     60   # have used EV for the outer loop instead of CAT - which was about 3x slower.)
    61     61   #
    62     62   do_test analyze6-1.1 {
    63     63     eqp {SELECT count(*) FROM ev, cat WHERE x=y}
    64         -} {0 0 1 {SCAN TABLE cat (~16 rows)} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
           64  +} {0 0 1 {SCAN TABLE cat USING COVERING INDEX catx (~16 rows)} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
    65     65   
    66     66   # The same plan is chosen regardless of the order of the tables in the
    67     67   # FROM clause.
    68     68   #
    69     69   do_test analyze6-1.2 {
    70     70     eqp {SELECT count(*) FROM cat, ev WHERE x=y}
    71         -} {0 0 0 {SCAN TABLE cat (~16 rows)} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
           71  +} {0 0 0 {SCAN TABLE cat USING COVERING INDEX catx (~16 rows)} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?) (~32 rows)}}
    72     72   
    73     73   
    74     74   # Ticket [83ea97620bd3101645138b7b0e71c12c5498fe3d] 2011-03-30
    75     75   # If ANALYZE is run on an empty table, make sure indices are used
    76     76   # on the table.
    77     77   #
    78     78   do_test analyze6-2.1 {

Changes to test/autovacuum.test.

   110    110       # Ensure the data remaining in the table is what was expected.
   111    111       foreach d $delete {
   112    112         set idx [lsearch $::tbl_data [make_str $d $ENTRY_LEN]]
   113    113         set ::tbl_data [lreplace $::tbl_data $idx $idx]
   114    114       }
   115    115       do_test autovacuum-1.$tn.($delete).3 {
   116    116         execsql {
   117         -        select a from av1
          117  +        select a from av1 order by rowid
   118    118         }
   119    119       } $::tbl_data
   120    120     }
   121    121   
   122    122     # All rows have been deleted. Ensure the file has shrunk to 4 pages.
   123    123     do_test autovacuum-1.$tn.3 {
   124    124       file_pages

Changes to test/backcompat.test.

   209    209   
   210    210   #-------------------------------------------------------------------------
   211    211   # Test that WAL and wal-index files may be shared between different 
   212    212   # SQLite versions.
   213    213   #
   214    214   do_allbackcompat_test {
   215    215     if {[code1 {sqlite3 -version}] >= "3.7.0"
          216  +   && [code1 {set ::sqlite_options(wal)}]
   216    217      && [code2 {sqlite3 -version}] >= "3.7.0"
          218  +   && [code2 {set ::sqlite_options(wal)}]
   217    219     } {
   218    220   
   219    221       do_test backcompat-2.1.1 { sql1 {
   220    222         PRAGMA journal_mode = WAL;
   221    223         CREATE TABLE t1(a PRIMARY KEY, b UNIQUE);
   222    224         INSERT INTO t1 VALUES('I', 1);
   223    225         INSERT INTO t1 VALUES('II', 2);

Changes to test/collate4.test.

    90     90   do_test collate4-1.1.4 {
    91     91     cksort {SELECT b FROM collate4t1 ORDER BY b}
    92     92   } {{} A B a b nosort}
    93     93   do_test collate4-1.1.5 {
    94     94     cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE TEXT}
    95     95   } {{} A B a b nosort}
    96     96   do_test collate4-1.1.6 {
    97         -  cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE NOCASE}
           97  +  cksort {SELECT b FROM collate4t1 ORDER BY b COLLATE NOCASE, rowid}
    98     98   } {{} a A b B sort}
    99     99   
   100    100   do_test collate4-1.1.7 {
   101    101     execsql {
   102    102       CREATE TABLE collate4t2(
   103    103         a PRIMARY KEY COLLATE NOCASE, 
   104    104         b UNIQUE COLLATE TEXT
................................................................................
   167    167       INSERT INTO collate4t4 VALUES( 'B', 'B' );
   168    168       INSERT INTO collate4t4 VALUES( 'A', 'A' );
   169    169       CREATE INDEX collate4i3 ON collate4t4(a COLLATE TEXT);
   170    170       CREATE INDEX collate4i4 ON collate4t4(b COLLATE NOCASE);
   171    171     }
   172    172   } {}
   173    173   do_test collate4-1.1.22 {
   174         -  cksort {SELECT a FROM collate4t4 ORDER BY a}
          174  +  cksort {SELECT a FROM collate4t4 ORDER BY a, rowid}
   175    175   } {{} a A b B sort}
   176    176   do_test collate4-1.1.23 {
   177         -  cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE NOCASE}
          177  +  cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE NOCASE, rowid}
   178    178   } {{} a A b B sort}
   179    179   do_test collate4-1.1.24 {
   180         -  cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE TEXT}
          180  +  cksort {SELECT a FROM collate4t4 ORDER BY a COLLATE TEXT, rowid}
   181    181   } {{} A B a b nosort}
   182    182   do_test collate4-1.1.25 {
   183    183     cksort {SELECT b FROM collate4t4 ORDER BY b}
   184    184   } {{} A B a b sort}
   185    185   do_test collate4-1.1.26 {
   186    186     cksort {SELECT b FROM collate4t4 ORDER BY b COLLATE TEXT}
   187    187   } {{} A B a b sort}
................................................................................
   218    218   do_test collate4-1.2.3 {
   219    219     cksort {SELECT a FROM collate4t1 ORDER BY a COLLATE text}
   220    220   } {{} A B a b sort}
   221    221   do_test collate4-1.2.4 {
   222    222     cksort {SELECT a FROM collate4t1 ORDER BY a, b}
   223    223   } {{} A a B b nosort}
   224    224   do_test collate4-1.2.5 {
   225         -  cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE nocase}
          225  +  cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE nocase, rowid}
   226    226   } {{} a A b B sort}
   227    227   do_test collate4-1.2.6 {
   228    228     cksort {SELECT a FROM collate4t1 ORDER BY a, b COLLATE text}
   229    229   } {{} A a B b nosort}
   230    230   
   231    231   do_test collate4-1.2.7 {
   232    232     execsql {
................................................................................
   267    267       INSERT INTO collate4t3 VALUES( NULL, NULL );
   268    268       INSERT INTO collate4t3 VALUES( 'B', 'B' );
   269    269       INSERT INTO collate4t3 VALUES( 'A', 'A' );
   270    270       CREATE INDEX collate4i2 ON collate4t3(a COLLATE TEXT, b COLLATE NOCASE);
   271    271     }
   272    272   } {}
   273    273   do_test collate4-1.2.15 {
   274         -  cksort {SELECT a FROM collate4t3 ORDER BY a}
          274  +  cksort {SELECT a FROM collate4t3 ORDER BY a, rowid}
   275    275   } {{} a A b B sort}
   276    276   do_test collate4-1.2.16 {
   277         -  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE nocase}
          277  +  cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE nocase, rowid}
   278    278   } {{} a A b B sort}
   279    279   do_test collate4-1.2.17 {
   280    280     cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text}
   281    281   } {{} A B a b nosort}
   282    282   do_test collate4-1.2.18 {
   283    283     cksort {SELECT a FROM collate4t3 ORDER BY a COLLATE text, b}
   284    284   } {{} A B a b sort}
................................................................................
   360    360   } {A A 19}
   361    361   do_test collate4-2.1.4 {
   362    362     execsql {
   363    363       DROP INDEX collate4i1;
   364    364       CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
   365    365     }
   366    366     count {
   367         -    SELECT * FROM collate4t2, collate4t1 WHERE a = b;
          367  +    SELECT * FROM collate4t2, collate4t1 WHERE a = b
          368  +     ORDER BY collate4t2.rowid, collate4t1.rowid
   368    369     }
   369    370   } {A a A A 19}
   370    371   do_test collate4-2.1.5 {
   371    372     count {
   372    373       SELECT * FROM collate4t2, collate4t1 WHERE b = a;
   373    374     }
   374    375   } {A A 4}
   375    376   ifcapable subquery {
   376    377     do_test collate4-2.1.6 {
   377    378       count {
   378         -      SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
          379  +      SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
          380  +       ORDER BY rowid
   379    381       }
   380    382     } {a A 10}
   381    383     do_test collate4-2.1.7 {
   382    384       execsql {
   383    385         DROP INDEX collate4i1;
   384    386         CREATE INDEX collate4i1 ON collate4t1(a);
   385    387       }
   386    388       count {
   387         -      SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2);
          389  +      SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
          390  +       ORDER BY rowid
   388    391       }
   389    392     } {a A 6}
   390    393     do_test collate4-2.1.8 {
   391    394       count {
   392    395         SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
   393    396       }
   394    397     } {a A 5}
   395    398     do_test collate4-2.1.9 {
   396    399       execsql {
   397    400         DROP INDEX collate4i1;
   398    401         CREATE INDEX collate4i1 ON collate4t1(a COLLATE TEXT);
   399    402       }
   400    403       count {
   401         -      SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
          404  +      SELECT a FROM collate4t1 WHERE a IN ('z', 'a') ORDER BY rowid;
   402    405       }
   403    406     } {a A 9}
   404    407   }
   405    408   do_test collate4-2.1.10 {
   406    409     execsql {
   407    410       DROP TABLE collate4t1;
   408    411       DROP TABLE collate4t2;

Changes to test/collate5.test.

   217    217   # These tests - collate5-3.* - focus on compound SELECT queries that 
   218    218   # feature ORDER BY clauses.
   219    219   #
   220    220   do_test collate5-3.0 {
   221    221     execsql {
   222    222       SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2 ORDER BY 1;
   223    223     }
   224         -} {a A a A b B b B n N}
          224  +} {/[aA] [aA] [aA] [aA] [bB] [bB] [bB] [bB] [nN] [nN]/}
   225    225   do_test collate5-3.1 {
   226    226     execsql {
   227    227       SELECT a FROM collate5t2 UNION ALL SELECT a FROM collate5t1 ORDER BY 1;
   228    228     }
   229    229   } {A A B B N a a b b n}
   230    230   do_test collate5-3.2 {
   231    231     execsql {
................................................................................
   278    278       SELECT a, count(*) FROM collate5t1 GROUP BY a;
   279    279     }]
   280    280   } {a 2 b 2}
   281    281   do_test collate5-4.2 {
   282    282     execsql {
   283    283       SELECT a, b, count(*) FROM collate5t1 GROUP BY a, b ORDER BY a, b;
   284    284     }
   285         -} {A 1.0 2 b 2 1 B 3 1}
          285  +} {/[aA] 1(.0)? 2 [bB] 2 1 [bB] 3 1/}
   286    286   do_test collate5-4.3 {
   287    287     execsql {
   288    288       DROP TABLE collate5t1;
   289    289     }
   290    290   } {}
   291    291   
   292    292   finish_test

Changes to test/corruptD.test.

   103    103   #-------------------------------------------------------------------------
   104    104   # The following tests, corruptD-1.1.*, focus on the page header field
   105    105   # containing the offset of the first free block in a page. 
   106    106   #
   107    107   do_test corruptD-1.1.1 {
   108    108     incr_change_counter
   109    109     hexio_write test.db [expr 1024+1] FFFF
   110         -  catchsql { SELECT * FROM t1 }
          110  +  catchsql { SELECT * FROM t1 ORDER BY rowid }
   111    111   } {1 {database disk image is malformed}}
   112    112   do_test corruptD-1.1.2 {
   113    113     incr_change_counter
   114    114     hexio_write test.db [expr 1024+1] [hexio_render_int32 1021]
   115         -  catchsql { SELECT * FROM t1 }
          115  +  catchsql { SELECT * FROM t1 ORDER BY rowid }
   116    116   } {1 {database disk image is malformed}}
   117    117   
   118    118   #-------------------------------------------------------------------------
   119    119   # The following tests, corruptD-1.2.*, focus on the offsets contained
   120    120   # in the first 2 byte of each free-block on the free-list.
   121    121   #
   122    122   do_test corruptD-1.2.1 {

Changes to test/corruptE.test.

    45     45       INSERT OR IGNORE INTO t1 SELECT x*5,y FROM t1;
    46     46       INSERT OR IGNORE INTO t1 SELECT x*7,y FROM t1;
    47     47       INSERT OR IGNORE INTO t1 SELECT x*11,y FROM t1;
    48     48       INSERT OR IGNORE INTO t1 SELECT x*13,y FROM t1;
    49     49       INSERT OR IGNORE INTO t1 SELECT x*17,y FROM t1;
    50     50       INSERT OR IGNORE INTO t1 SELECT x*19,y FROM t1;
    51     51       CREATE INDEX t1i1 ON t1(x);
    52         -    CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0;
           52  +    CREATE TABLE t2 AS SELECT x,2 as y FROM t1 WHERE rowid%5!=0 ORDER BY rowid;
    53     53       COMMIT;
    54     54     }
    55     55   } {}
    56     56   
    57     57   ifcapable {integrityck} {
    58     58     integrity_check corruptE-1.2
    59     59   }

Added test/coveridxscan.test.

            1  +# 2012 September 17
            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  +#
           12  +# Tests for the optimization which attempts to use a covering index
           13  +# for a full-table scan (under the theory that the index will be smaller
           14  +# and require less I/O and hence will run faster.)
           15  +#
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +
           20  +set testprefix coveridxscan
           21  +
           22  +do_test 1.1 {
           23  +  db eval {
           24  +    CREATE TABLE t1(a,b,c);
           25  +    INSERT INTO t1 VALUES(5,4,3), (4,8,2), (3,2,1);
           26  +    CREATE INDEX t1ab ON t1(a,b);
           27  +    CREATE INDEX t1b ON t1(b);
           28  +    SELECT a FROM t1;
           29  +  }
           30  +  # covering index used for the scan, hence values are increasing
           31  +} {3 4 5}
           32  +
           33  +do_test 1.2 {
           34  +  db eval {
           35  +    SELECT a, c FROM t1;
           36  +  }
           37  +  # There is no covering index, hence the values are in rowid order
           38  +} {5 3 4 2 3 1}
           39  +
           40  +do_test 1.3 {
           41  +  db eval {
           42  +    SELECT b FROM t1;
           43  +  }
           44  +  # Choice of two indices: use the one with fewest columns
           45  +} {2 4 8}
           46  +
           47  +do_test 2.1 {
           48  +  optimization_control db cover-idx-scan 0
           49  +  db eval {SELECT a FROM t1}
           50  +  # With the optimization turned off, output in rowid order
           51  +} {5 4 3}
           52  +do_test 2.2 {
           53  +  db eval {SELECT a, c FROM t1}
           54  +} {5 3 4 2 3 1}
           55  +do_test 2.3 {
           56  +  db eval {SELECT b FROM t1}
           57  +} {4 8 2}
           58  +
           59  +db close
           60  +sqlite3_shutdown
           61  +sqlite3_config_cis 0
           62  +sqlite3 db test.db
           63  +
           64  +do_test 3.1 {
           65  +  db eval {SELECT a FROM t1}
           66  +  # With the optimization configured off, output in rowid order
           67  +} {5 4 3}
           68  +do_test 3.2 {
           69  +  db eval {SELECT a, c FROM t1}
           70  +} {5 3 4 2 3 1}
           71  +do_test 3.3 {
           72  +  db eval {SELECT b FROM t1}
           73  +} {4 8 2}
           74  +
           75  +db close
           76  +sqlite3_shutdown
           77  +sqlite3_config_cis 1
           78  +sqlite3 db test.db
           79  +
           80  +# The CIS optimization is enabled again.  Covering indices are once again
           81  +# used for all table scans.
           82  +do_test 4.1 {
           83  +  db eval {SELECT a FROM t1}
           84  +} {3 4 5}
           85  +do_test 4.2 {
           86  +  db eval {SELECT a, c FROM t1}
           87  +} {5 3 4 2 3 1}
           88  +do_test 4.3 {
           89  +  db eval {SELECT b FROM t1}
           90  +} {2 4 8}
           91  +
           92  +
           93  +finish_test

Changes to test/dbstatus2.test.

    84     84     execsql { INSERT INTO t1 VALUES(4, randomblob(600)) }
    85     85     db_write db
    86     86   } {0 4 0}
    87     87   do_test 2.3 { db_write db 1 } {0 4 0}
    88     88   do_test 2.4 { db_write db 0 } {0 0 0}
    89     89   do_test 2.5 { db_write db 1 } {0 0 0}
    90     90   
    91         -do_test 2.6 { 
    92         -  execsql { PRAGMA journal_mode = WAL }
    93         -  db_write db 1
    94         -} {0 1 0}
           91  +ifcapable wal {
           92  +  do_test 2.6 { 
           93  +    execsql { PRAGMA journal_mode = WAL }
           94  +    db_write db 1
           95  +  } {0 1 0}
           96  +}
    95     97   do_test 2.7 { 
    96     98     execsql { INSERT INTO t1 VALUES(5, randomblob(600)) }
    97     99     db_write db
    98    100   } {0 4 0}
    99    101   do_test 2.8 { db_write db 1 } {0 4 0}
   100    102   do_test 2.9 { db_write db 0 } {0 0 0}
   101    103    
   102    104   finish_test

Changes to test/distinct.test.

   171    171     9   "b COLLATE nocase FROM t1 ORDER BY b COLLATE nocase" {}      {B}
   172    172   } {
   173    173     do_execsql_test    2.$tn.1 "SELECT DISTINCT $sql" $res
   174    174     do_temptables_test 2.$tn.2 "SELECT DISTINCT $sql" $temptables
   175    175   }
   176    176   
   177    177   do_execsql_test 2.A {
   178         -  SELECT (SELECT DISTINCT o.a FROM t1 AS i) FROM t1 AS o;
          178  +  SELECT (SELECT DISTINCT o.a FROM t1 AS i) FROM t1 AS o ORDER BY rowid;
   179    179   } {a A a A}
   180    180   
   181         -
   182         -
          181  +do_test 3.0 {
          182  +  db eval {
          183  +    CREATE TABLE t3(a INTEGER, b INTEGER, c, UNIQUE(a,b));
          184  +    INSERT INTO t3 VALUES
          185  +        (null, null, 1),
          186  +        (null, null, 2),
          187  +        (null, 3, 4),
          188  +        (null, 3, 5),
          189  +        (6, null, 7),
          190  +        (6, null, 8);
          191  +    SELECT DISTINCT a, b FROM t3 ORDER BY +a, +b;
          192  +  }
          193  +} {{} {} {} 3 6 {}}
          194  +do_test 3.1 {
          195  +  regexp {OpenEphemeral} [db eval {
          196  +    EXPLAIN SELECT DISTINCT a, b FROM t3 ORDER BY +a, +b;
          197  +  }]
          198  +} {0}
   183    199   
   184    200   finish_test

Changes to test/e_createtable.test.

  1587   1587     do_execsql_test  4.17.$tn.1 "BEGIN; INSERT INTO $tbl VALUES(3, 'three')"
  1588   1588   
  1589   1589     do_catchsql_test 4.17.$tn.2 " 
  1590   1590       INSERT INTO $tbl SELECT ((a%2)*a+3), 'three' FROM $tbl
  1591   1591     " $res
  1592   1592   
  1593   1593     do_test e_createtable-4.17.$tn.3 { sqlite3_get_autocommit db } $ac
  1594         -  do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl" $data
         1594  +  do_execsql_test 4.17.$tn.4 "SELECT * FROM $tbl ORDER BY rowid" $data
  1595   1595   }
  1596   1596   catchsql COMMIT
  1597   1597   
  1598   1598   # EVIDENCE-OF: R-12645-39772 Or, if a constraint definition does not
  1599   1599   # include a conflict-clause or it is a CHECK constraint, the default
  1600   1600   # conflict resolution algorithm is ABORT.
  1601   1601   #

Changes to test/e_fkey.test.

  2056   2056       INSERT INTO cA VALUES(X'ABCD');
  2057   2057       INSERT INTO cB VALUES(X'1234');
  2058   2058     }
  2059   2059   } {}
  2060   2060   do_test e_fkey-45.2 {
  2061   2061     execsql {
  2062   2062       DELETE FROM pA WHERE rowid = 3;
  2063         -    SELECT quote(x) FROM pA;
         2063  +    SELECT quote(x) FROM pA ORDER BY rowid;
  2064   2064     }
  2065   2065   } {X'0000' X'9999' X'1234'}
  2066   2066   do_test e_fkey-45.3 {
  2067   2067     execsql { SELECT quote(c) FROM cA }
  2068   2068   } {X'0000'}
  2069   2069   do_test e_fkey-45.4 {
  2070   2070     execsql {
  2071   2071       UPDATE pA SET x = X'8765' WHERE rowid = 4;
  2072         -    SELECT quote(x) FROM pA;
         2072  +    SELECT quote(x) FROM pA ORDER BY rowid;
  2073   2073     }
  2074   2074   } {X'0000' X'9999' X'8765'}
  2075   2075   do_test e_fkey-45.5 {
  2076   2076     execsql { SELECT quote(c) FROM cB }
  2077   2077   } {X'9999'}
  2078   2078   
  2079   2079   #-------------------------------------------------------------------------
................................................................................
  2321   2321       INSERT INTO parent VALUES(1);
  2322   2322       INSERT INTO child VALUES(1);
  2323   2323     }
  2324   2324   } {}
  2325   2325   do_test e_fkey-51.2 {
  2326   2326     execsql {
  2327   2327       UPDATE parent SET x = 22;
  2328         -    SELECT * FROM parent ; SELECT 'xxx' ; SELECT a FROM child;
         2328  +    SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
  2329   2329     }
  2330   2330   } {22 21 23 xxx 22}
  2331   2331   do_test e_fkey-51.3 {
  2332   2332     execsql {
  2333   2333       DELETE FROM child;
  2334   2334       DELETE FROM parent;
  2335   2335       INSERT INTO parent VALUES(-1);
  2336   2336       INSERT INTO child VALUES(-1);
  2337   2337       UPDATE parent SET x = 22;
  2338         -    SELECT * FROM parent ; SELECT 'xxx' ; SELECT a FROM child;
         2338  +    SELECT * FROM parent ORDER BY rowid; SELECT 'xxx' ; SELECT a FROM child;
  2339   2339     }
  2340   2340   } {22 23 21 xxx 23}
  2341   2341   
  2342   2342   
  2343   2343   #-------------------------------------------------------------------------
  2344   2344   # Verify that ON UPDATE actions only actually take place if the parent key
  2345   2345   # is set to a new value that is distinct from the old value. The default

Changes to test/e_select.test.

  1019   1019   #   These tests also show that the following is not untrue:
  1020   1020   #
  1021   1021   # EVIDENCE-OF: R-25883-55063 The expressions in the GROUP BY clause do
  1022   1022   # not have to be expressions that appear in the result.
  1023   1023   #
  1024   1024   do_select_tests e_select-4.9 {
  1025   1025     1  "SELECT group_concat(one), two FROM b1 GROUP BY two" {
  1026         -    4,5 f   1 o   7,6   s 3,2 t
         1026  +    /#,# f   1 o   #,#   s #,# t/
  1027   1027     }
  1028   1028     2  "SELECT group_concat(one), sum(one) FROM b1 GROUP BY (one>4)" {
  1029         -    1,4,3,2 10    5,7,6 18
         1029  +    1,2,3,4 10    5,6,7 18
  1030   1030     }
  1031   1031     3  "SELECT group_concat(one) FROM b1 GROUP BY (two>'o'), one%2" {
  1032   1032       4  1,5    2,6   3,7
  1033   1033     }
  1034   1034     4  "SELECT group_concat(one) FROM b1 GROUP BY (one==2 OR two=='o')" {
  1035   1035       4,3,5,7,6    1,2
  1036   1036     }
  1037   1037   }
  1038   1038   
  1039   1039   # EVIDENCE-OF: R-14926-50129 For the purposes of grouping rows, NULL
  1040   1040   # values are considered equal.
  1041   1041   #
  1042   1042   do_select_tests e_select-4.10 {
  1043         -  1  "SELECT group_concat(y) FROM b2 GROUP BY x" {0,1   3   2,4}
         1043  +  1  "SELECT group_concat(y) FROM b2 GROUP BY x" {/#,#   3   #,#/}
  1044   1044     2  "SELECT count(*) FROM b2 GROUP BY CASE WHEN y<4 THEN NULL ELSE 0 END" {4 1}
  1045   1045   } 
  1046   1046   
  1047   1047   # EVIDENCE-OF: R-10470-30318 The usual rules for selecting a collation
  1048   1048   # sequence with which to compare text values apply when evaluating
  1049   1049   # expressions in a GROUP BY clause.
  1050   1050   #
................................................................................
  1741   1741        1 2 3    1 2 -20    1 4  93    1 5 -1   
  1742   1742     }
  1743   1743     7  "SELECT * FROM d1 ORDER BY 1 DESC, 2, 3" {
  1744   1744        2 4 93   2 5 -1     1 2 -20    1 2 3    
  1745   1745        1 2 7    1 2 8      1 4  93    1 5 -1   
  1746   1746     }
  1747   1747     8  "SELECT z, x FROM d1 ORDER BY 2" {
  1748         -     3 1     8 1    7 1   -20 1 
  1749         -     93 1   -1 1   -1 2   93 2
         1748  +     /# 1    # 1    # 1   # 1 
         1749  +      # 1    # 1    # 2   # 2/
  1750   1750     }
  1751   1751     9  "SELECT z, x FROM d1 ORDER BY 1" {
  1752         -     -20 1  -1 2   -1 1   3 1     
  1753         -     7 1     8 1   93 2   93 1   
         1752  +     /-20 1  -1 #   -1 #   3 1
         1753  +     7 1     8 1   93 #   93 #/   
  1754   1754     }
  1755   1755   }
  1756   1756   
  1757   1757   # EVIDENCE-OF: R-63286-51977 If the ORDER BY expression is an identifier
  1758   1758   # that corresponds to the alias of one of the output columns, then the
  1759   1759   # expression is considered an alias for that column.
  1760   1760   #
................................................................................
  1762   1762     1   "SELECT z+1 AS abc FROM d1 ORDER BY abc" {
  1763   1763       -19 0 0 4 8 9 94 94
  1764   1764     }
  1765   1765     2   "SELECT z+1 AS abc FROM d1 ORDER BY abc DESC" {
  1766   1766       94 94 9 8 4 0 0 -19
  1767   1767     }
  1768   1768     3  "SELECT z AS x, x AS z FROM d1 ORDER BY z" {
  1769         -    3 1    8 1    7 1    -20 1    93 1    -1 1    -1 2    93 2
         1769  +    /# 1    # 1    # 1    # 1    # 1    # 1    # 2    # 2/
  1770   1770     }
  1771   1771     4  "SELECT z AS x, x AS z FROM d1 ORDER BY x" {
  1772         -    -20 1    -1 2    -1 1    3 1    7 1    8 1    93 2    93 1
         1772  +    /-20 1    -1 #    -1 #    3 1    7 1    8 1    93 #    93 #/
  1773   1773     }
  1774   1774   }
  1775   1775   
  1776   1776   # EVIDENCE-OF: R-65068-27207 Otherwise, if the ORDER BY expression is
  1777   1777   # any other expression, it is evaluated and the returned value used to
  1778   1778   # order the output rows.
  1779   1779   #

Changes to test/eqp.test.

    58     58     SELECT a FROM t1 ORDER BY a
    59     59   } {
    60     60     0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
    61     61   }
    62     62   do_eqp_test 1.4 {
    63     63     SELECT a FROM t1 ORDER BY +a
    64     64   } {
    65         -  0 0 0 {SCAN TABLE t1 (~1000000 rows)}
           65  +  0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
    66     66     0 0 0 {USE TEMP B-TREE FOR ORDER BY}
    67     67   }
    68     68   do_eqp_test 1.5 {
    69     69     SELECT a FROM t1 WHERE a=4
    70     70   } {
    71     71     0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?) (~10 rows)}
    72     72   }
................................................................................
   162    162   det 2.3.1 "SELECT max(x) FROM t2" {
   163    163     0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1 (~1 rows)}
   164    164   }
   165    165   det 2.3.2 "SELECT min(x) FROM t2" {
   166    166     0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1 (~1 rows)}
   167    167   }
   168    168   det 2.3.3 "SELECT min(x), max(x) FROM t2" {
   169         -  0 0 0 {SCAN TABLE t2 (~1000000 rows)}
          169  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)}
   170    170   }
   171    171   
   172    172   det 2.4.1 "SELECT * FROM t1 WHERE rowid=?" {
   173    173     0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)}
   174    174   }
   175    175   
   176    176   
................................................................................
   335    335     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)} 
   336    336   }
   337    337   
   338    338   do_eqp_test 4.3.1 {
   339    339     SELECT x FROM t1 UNION SELECT x FROM t2
   340    340   } {
   341    341     1 0 0 {SCAN TABLE t1 (~1000000 rows)} 
   342         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          342  +  2 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)} 
   343    343     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)} 
   344    344   }
   345    345   
   346    346   do_eqp_test 4.3.2 {
   347    347     SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1
   348    348   } {
   349    349     2 0 0 {SCAN TABLE t1 (~1000000 rows)} 
   350         -  3 0 0 {SCAN TABLE t2 (~1000000 rows)} 
          350  +  3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1 (~1000000 rows)} 
   351    351     1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)}
   352    352     4 0 0 {SCAN TABLE t1 (~1000000 rows)} 
   353    353     0 0 0 {COMPOUND SUBQUERIES 1 AND 4 USING TEMP B-TREE (UNION)}
   354    354   }
   355    355   do_eqp_test 4.3.3 {
   356    356     SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1 ORDER BY 1
   357    357   } {
................................................................................
   443    443   # 0|0|0|SCAN TABLE t2 (~1000000 rows) 0|0|0|EXECUTE SCALAR SUBQUERY 1
   444    444   # 1|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)
   445    445   # 0|0|0|EXECUTE CORRELATED SCALAR SUBQUERY 2 2|0|0|SEARCH TABLE t1 USING
   446    446   # INDEX i3 (b=?) (~10 rows)
   447    447   det 5.9 {
   448    448     SELECT (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2
   449    449   } {
   450         -  0 0 0 {SCAN TABLE t2 (~1000000 rows)}
          450  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX i4 (~1000000 rows)}
   451    451     0 0 0 {EXECUTE SCALAR SUBQUERY 1}
   452    452     1 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?) (~10 rows)}
   453    453     0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2}
   454    454     2 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?) (~10 rows)}
   455    455   }
   456    456   
   457    457   # EVIDENCE-OF: R-17911-16445 sqlite> EXPLAIN QUERY PLAN SELECT
................................................................................
   467    467   }
   468    468   
   469    469   # EVIDENCE-OF: R-18544-33103 sqlite> EXPLAIN QUERY PLAN SELECT * FROM
   470    470   # (SELECT * FROM t2 WHERE c=1), t1; 0|0|0|SEARCH TABLE t2 USING INDEX i4
   471    471   # (c=?) (~10 rows) 0|1|1|SCAN TABLE t1 (~1000000 rows)
   472    472   det 5.11 "SELECT * FROM (SELECT * FROM t2 WHERE c=1), t1" {
   473    473     0 0 0 {SEARCH TABLE t2 USING INDEX i4 (c=?) (~10 rows)}
   474         -  0 1 1 {SCAN TABLE t1 (~1000000 rows)}
          474  +  0 1 1 {SCAN TABLE t1 USING COVERING INDEX i2 (~1000000 rows)}
   475    475   }
   476    476   
   477    477   # EVIDENCE-OF: R-40701-42164 sqlite> EXPLAIN QUERY PLAN SELECT a FROM
   478    478   # t1 UNION SELECT c FROM t2; 1|0|0|SCAN TABLE t1 (~1000000 rows)
   479    479   # 2|0|0|SCAN TABLE t2 (~1000000 rows) 0|0|0|COMPOUND SUBQUERIES 1 AND 2
   480    480   # USING TEMP B-TREE (UNION)
   481    481   det 5.12 "SELECT a FROM t1 UNION SELECT c FROM t2" {
   482         -  1 0 0 {SCAN TABLE t1 (~1000000 rows)}
   483         -  2 0 0 {SCAN TABLE t2 (~1000000 rows)}
          482  +  1 0 0 {SCAN TABLE t1 USING COVERING INDEX i1 (~1000000 rows)}
          483  +  2 0 0 {SCAN TABLE t2 USING COVERING INDEX i4 (~1000000 rows)}
   484    484     0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)}
   485    485   }
   486    486   
   487    487   # EVIDENCE-OF: R-61538-24748 sqlite> EXPLAIN QUERY PLAN SELECT a FROM
   488    488   # t1 EXCEPT SELECT d FROM t2 ORDER BY 1; 1|0|0|SCAN TABLE t1 USING
   489    489   # COVERING INDEX i2 (~1000000 rows) 2|0|0|SCAN TABLE t2 (~1000000 rows)
   490    490   # 2|0|0|USE TEMP B-TREE FOR ORDER BY 0|0|0|COMPOUND SUBQUERIES 1 AND 2

Added test/full.test.

            1  +# 2012 September 12
            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 runs the "full" test suite. It is a peer of the quick.test
           12  +# and all.test scripts.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/permutations.test
           17  +
           18  +run_test_suite full
           19  +
           20  +finish_test

Added test/in5.test.

            1  +# 2012 September 18
            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  +#
           12  +
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +
           16  +do_test in5-1.1 {
           17  +  execsql {
           18  +    CREATE TABLE t1x(x INTEGER PRIMARY KEY);
           19  +    INSERT INTO t1x VALUES(1),(3),(5),(7),(9);
           20  +    CREATE TABLE t1y(y INTEGER UNIQUE);
           21  +    INSERT INTO t1y VALUES(2),(4),(6),(8);
           22  +    CREATE TABLE t1z(z TEXT UNIQUE);
           23  +    INSERT INTO t1z VALUES('a'),('c'),('e'),('g');
           24  +    CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT, d TEXT);
           25  +    INSERT INTO t2 VALUES(1,2,'a','12a'),(1,2,'b','12b'),
           26  +                         (2,3,'g','23g'),(3,5,'c','35c'),
           27  +                         (4,6,'h','46h'),(5,6,'e','56e');
           28  +    CREATE TABLE t3x AS SELECT x FROM t1x;
           29  +    CREATE TABLE t3y AS SELECT y FROM t1y;
           30  +    CREATE TABLE t3z AS SELECT z FROM t1z;
           31  +    SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY c;
           32  +  }
           33  +} {12a 56e}
           34  +do_test in5-1.2 {
           35  +  execsql {
           36  +    SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
           37  +  }
           38  +} {23g}
           39  +do_test in5-1.3 {
           40  +  execsql {
           41  +    SELECT d FROM t2 WHERE a IN t3x AND b IN t3y AND c IN t3z ORDER BY d;
           42  +  }
           43  +} {12a 56e}
           44  +
           45  +
           46  +do_test in5-2.1 {
           47  +  execsql {
           48  +    CREATE INDEX t2abc ON t2(a,b,c);
           49  +    SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
           50  +  }
           51  +} {12a 56e}
           52  +do_test in5-2.2 {
           53  +  execsql {
           54  +    SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
           55  +  }
           56  +} {23g}
           57  +do_test in5-2.3 {
           58  +  regexp {OpenEphemeral} [db eval {
           59  +    EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
           60  +  }]
           61  +} {0}
           62  +do_test in5-2.4 {
           63  +  execsql {
           64  +    SELECT d FROM t2 WHERE a IN t3x AND b IN t3y AND c IN t3z ORDER BY d;
           65  +  }
           66  +} {12a 56e}
           67  +do_test in5-2.5.1 {
           68  +  regexp {OpenEphemeral} [db eval {
           69  +    EXPLAIN SELECT d FROM t2 WHERE a IN t3x AND b IN t1y AND c IN t1z
           70  +  }]
           71  +} {1}
           72  +do_test in5-2.5.2 {
           73  +  regexp {OpenEphemeral} [db eval {
           74  +    EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t3y AND c IN t1z
           75  +  }]
           76  +} {1}
           77  +do_test in5-2.5.3 {
           78  +  regexp {OpenEphemeral} [db eval {
           79  +    EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t3z
           80  +  }]
           81  +} {1}
           82  +
           83  +do_test in5-3.1 {
           84  +  execsql {
           85  +    DROP INDEX t2abc;
           86  +    CREATE INDEX t2ab ON t2(a,b);
           87  +    SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
           88  +  }
           89  +} {12a 56e}
           90  +do_test in5-3.2 {
           91  +  execsql {
           92  +    SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
           93  +  }
           94  +} {23g}
           95  +do_test in5-3.3 {
           96  +  regexp {OpenEphemeral} [db eval {
           97  +    EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
           98  +  }]
           99  +} {0}
          100  +
          101  +do_test in5-4.1 {
          102  +  execsql {
          103  +    DROP INDEX t2ab;
          104  +    CREATE INDEX t2abcd ON t2(a,b,c,d);
          105  +    SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
          106  +  }
          107  +} {12a 56e}
          108  +do_test in5-4.2 {
          109  +  execsql {
          110  +    SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
          111  +  }
          112  +} {23g}
          113  +do_test in5-4.3 {
          114  +  regexp {OpenEphemeral} [db eval {
          115  +    EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
          116  +  }]
          117  +} {0}
          118  +
          119  +
          120  +do_test in5-5.1 {
          121  +  execsql {
          122  +    DROP INDEX t2abcd;
          123  +    CREATE INDEX t2cbad ON t2(c,b,a,d);
          124  +    SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z ORDER BY d;
          125  +  }
          126  +} {12a 56e}
          127  +do_test in5-5.2 {
          128  +  execsql {
          129  +    SELECT d FROM t2 WHERE a IN t1y AND b IN t1x AND c IN t1z ORDER BY d;
          130  +  }
          131  +} {23g}
          132  +do_test in5-5.3 {
          133  +  regexp {OpenEphemeral} [db eval {
          134  +    EXPLAIN SELECT d FROM t2 WHERE a IN t1x AND b IN t1y AND c IN t1z
          135  +  }]
          136  +} {0}
          137  +
          138  +finish_test

Changes to test/incrblob.test.

   433    433       execsql {
   434    434         BEGIN;
   435    435         INSERT INTO blobs(k, v, i) VALUES('a', 'different', 'connection');
   436    436       } db2
   437    437     } {}
   438    438     do_test incrblob-6.2 {
   439    439       execsql {
   440         -      SELECT rowid FROM blobs
          440  +      SELECT rowid FROM blobs ORDER BY rowid
   441    441       }
   442    442     } {1 2 3}
   443    443     do_test incrblob-6.3 {
   444    444       set rc [catch {
   445    445         db incrblob blobs v 1
   446    446       } msg]
   447    447       list $rc $msg

Changes to test/intpkey.test.

   372    372     }
   373    373     count {
   374    374       SELECT * FROM t1 WHERE a=0;
   375    375     }
   376    376   } {0 zero entry 0}
   377    377   do_test intpkey-5.2 {
   378    378     execsql {
   379         -    SELECT rowid, a FROM t1
          379  +    SELECT rowid, a FROM t1 ORDER BY rowid
   380    380     }
   381    381   } {-4 -4 0 0 5 5 6 6 11 11}
   382    382   
   383    383   # Test the ability of the COPY command to put data into a
   384    384   # table that contains an integer primary key.
   385    385   #
   386    386   # COPY command has been removed.  But we retain these tests so

Changes to test/like.test.

   402    402   } {ABC {ABC abc xyz} abc abcd nosort {} i1}
   403    403   do_test like-5.2 {
   404    404     set sqlite_like_count
   405    405   } 12
   406    406   do_test like-5.3 {
   407    407     execsql {
   408    408       CREATE TABLE t2(x TEXT COLLATE NOCASE);
   409         -    INSERT INTO t2 SELECT * FROM t1;
          409  +    INSERT INTO t2 SELECT * FROM t1 ORDER BY rowid;
   410    410       CREATE INDEX i2 ON t2(x COLLATE NOCASE);
   411    411     }
   412    412     set sqlite_like_count 0
   413    413     queryplan {
   414    414       SELECT x FROM t2 WHERE x LIKE 'abc%' ORDER BY 1
   415    415     }
   416    416   } {abc ABC {ABC abc xyz} abcd nosort {} i2}
................................................................................
   658    658       }]
   659    659     } {0 {x hello}}
   660    660     ifcapable explain {
   661    661       do_test like-9.4.3 {
   662    662         set res [sqlite3_exec_hex db {
   663    663            EXPLAIN QUERY PLAN SELECT x FROM t2 WHERE x LIKE '%ff%25'
   664    664         }]
   665         -      regexp {INDEX i2} $res
   666         -    } {0}
          665  +      regexp {SCAN TABLE t2} $res
          666  +    } {1}
   667    667     }
   668    668     do_test like-9.5.1 {
   669    669       set res [sqlite3_exec_hex db {
   670    670          SELECT x FROM t2 WHERE x LIKE '%fe%25'
   671    671       }]
   672    672     } {0 {}}
   673    673     ifcapable explain {

Changes to test/lock.test.

   243    243   #
   244    244   do_test lock-2.8 {
   245    245     db2 timeout 400
   246    246     execsql BEGIN
   247    247     execsql {UPDATE t1 SET a = 0 WHERE 0}
   248    248     catchsql {BEGIN EXCLUSIVE;} db2
   249    249   } {1 {database is locked}}
          250  +do_test lock-2.8b {
          251  +  db2 eval {PRAGMA busy_timeout}
          252  +} {400}
   250    253   do_test lock-2.9 {
   251    254     db2 timeout 0
   252    255     execsql COMMIT
   253    256   } {}
          257  +do_test lock-2.9b {
          258  +  db2 eval {PRAGMA busy_timeout}
          259  +} {0}
   254    260   integrity_check lock-2.10
          261  +do_test lock-2.11 {
          262  +  db2 eval {PRAGMA busy_timeout(400)}
          263  +  execsql BEGIN
          264  +  execsql {UPDATE t1 SET a = 0 WHERE 0}
          265  +  catchsql {BEGIN EXCLUSIVE;} db2
          266  +} {1 {database is locked}}
          267  +do_test lock-2.11b {
          268  +  db2 eval {PRAGMA busy_timeout}
          269  +} {400}
          270  +do_test lock-2.12 {
          271  +  db2 eval {PRAGMA busy_timeout(0)}
          272  +  execsql COMMIT
          273  +} {}
          274  +do_test lock-2.12b {
          275  +  db2 eval {PRAGMA busy_timeout}
          276  +} {0}
          277  +integrity_check lock-2.13
   255    278   
   256    279   # Try to start two transactions in a row
   257    280   #
   258    281   do_test lock-3.1 {
   259    282     execsql {BEGIN TRANSACTION}
   260    283     set r [catch {execsql {BEGIN TRANSACTION}} msg]
   261    284     execsql {ROLLBACK}

Added test/orderby1.test.

            1  +# 2012 Sept 27
            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 testing that the optimizations that disable
           13  +# ORDER BY clauses when the natural order of a query is correct.
           14  +#
           15  +
           16  +
           17  +set testdir [file dirname $argv0]
           18  +source $testdir/tester.tcl
           19  +set ::testprefix orderby1
           20  +
           21  +# Generate test data for a join.  Verify that the join gets the
           22  +# correct answer.
           23  +#
           24  +do_test 1.0 {
           25  +  db eval {
           26  +    BEGIN;
           27  +    CREATE TABLE album(
           28  +      aid INTEGER PRIMARY KEY,
           29  +      title TEXT UNIQUE NOT NULL
           30  +    );
           31  +    CREATE TABLE track(
           32  +      tid INTEGER PRIMARY KEY,
           33  +      aid INTEGER NOT NULL REFERENCES album,
           34  +      tn INTEGER NOT NULL,
           35  +      name TEXT,
           36  +      UNIQUE(aid, tn)
           37  +    );
           38  +    INSERT INTO album VALUES(1, '1-one'), (2, '2-two'), (3, '3-three');
           39  +    INSERT INTO track VALUES
           40  +        (NULL, 1, 1, 'one-a'),
           41  +        (NULL, 2, 2, 'two-b'),
           42  +        (NULL, 3, 3, 'three-c'),
           43  +        (NULL, 1, 3, 'one-c'),
           44  +        (NULL, 2, 1, 'two-a'),
           45  +        (NULL, 3, 1, 'three-a');
           46  +    COMMIT;
           47  +  }
           48  +} {}
           49  +do_test 1.1a {
           50  +  db eval {
           51  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
           52  +  }
           53  +} {one-a one-c two-a two-b three-a three-c}
           54  +
           55  +# Verify that the ORDER BY clause is optimized out
           56  +#
           57  +do_test 1.1b {
           58  +  db eval {
           59  +    EXPLAIN QUERY PLAN
           60  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
           61  +  }
           62  +} {~/ORDER BY/}  ;# ORDER BY optimized out
           63  +
           64  +# The same query with ORDER BY clause optimization disabled via + operators
           65  +# should give exactly the same answer.
           66  +#
           67  +do_test 1.2a {
           68  +  db eval {
           69  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
           70  +  }
           71  +} {one-a one-c two-a two-b three-a three-c}
           72  +
           73  +# The output is sorted manually in this case.
           74  +#
           75  +do_test 1.2b {
           76  +  db eval {
           77  +    EXPLAIN QUERY PLAN
           78  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
           79  +  }
           80  +} {/ORDER BY/}   ;# separate sorting pass due to "+" on ORDER BY terms
           81  +
           82  +# The same query with ORDER BY optimizations turned off via built-in test.
           83  +#
           84  +do_test 1.3a {
           85  +  optimization_control db order-by-idx-join 0
           86  +  db cache flush
           87  +  db eval {
           88  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
           89  +  }
           90  +} {one-a one-c two-a two-b three-a three-c}
           91  +do_test 1.3b {
           92  +  db eval {
           93  +    EXPLAIN QUERY PLAN
           94  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
           95  +  }
           96  +} {/ORDER BY/}   ;# separate sorting pass due to disabled optimization
           97  +optimization_control db all 1
           98  +db cache flush
           99  +
          100  +# Reverse order sorts
          101  +#
          102  +do_test 1.4a {
          103  +  db eval {
          104  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
          105  +  }
          106  +} {three-a three-c two-a two-b one-a one-c}
          107  +do_test 1.4b {
          108  +  db eval {
          109  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn
          110  +  }
          111  +} {three-a three-c two-a two-b one-a one-c}  ;# verify same order after sorting
          112  +do_test 1.4c {
          113  +  db eval {
          114  +    EXPLAIN QUERY PLAN
          115  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
          116  +  }
          117  +} {/ORDER BY/}  ;# separate sorting pass due to mixed DESC/ASC
          118  +
          119  +
          120  +do_test 1.5a {
          121  +  db eval {
          122  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          123  +  }
          124  +} {one-c one-a two-b two-a three-c three-a}
          125  +do_test 1.5b {
          126  +  db eval {
          127  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
          128  +  }
          129  +} {one-c one-a two-b two-a three-c three-a}  ;# verify same order after sorting
          130  +do_test 1.5c {
          131  +  db eval {
          132  +    EXPLAIN QUERY PLAN
          133  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          134  +  }
          135  +} {/ORDER BY/}  ;# separate sorting pass due to mixed DESC/ASC
          136  +
          137  +do_test 1.6a {
          138  +  db eval {
          139  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
          140  +  }
          141  +} {three-c three-a two-b two-a one-c one-a}
          142  +do_test 1.6b {
          143  +  db eval {
          144  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
          145  +  }
          146  +} {three-c three-a two-b two-a one-c one-a}  ;# verify same order after sorting
          147  +do_test 1.6c {
          148  +  db eval {
          149  +    EXPLAIN QUERY PLAN
          150  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
          151  +  }
          152  +} {~/ORDER BY/}  ;# ORDER BY optimized-out
          153  +
          154  +
          155  +# Reconstruct the test data to use indices rather than integer primary keys.
          156  +#
          157  +do_test 2.0 {
          158  +  db eval {
          159  +    BEGIN;
          160  +    DROP TABLE album;
          161  +    DROP TABLE track;
          162  +    CREATE TABLE album(
          163  +      aid INT PRIMARY KEY,
          164  +      title TEXT NOT NULL
          165  +    );
          166  +    CREATE INDEX album_i1 ON album(title, aid);
          167  +    CREATE TABLE track(
          168  +      aid INTEGER NOT NULL REFERENCES album,
          169  +      tn INTEGER NOT NULL,
          170  +      name TEXT,
          171  +      UNIQUE(aid, tn)
          172  +    );
          173  +    INSERT INTO album VALUES(1, '1-one'), (2, '2-two'), (3, '3-three');
          174  +    INSERT INTO track VALUES
          175  +        (1, 1, 'one-a'),
          176  +        (2, 2, 'two-b'),
          177  +        (3, 3, 'three-c'),
          178  +        (1, 3, 'one-c'),
          179  +        (2, 1, 'two-a'),
          180  +        (3, 1, 'three-a');
          181  +    COMMIT;
          182  +  }
          183  +} {}
          184  +do_test 2.1a {
          185  +  db eval {
          186  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
          187  +  }
          188  +} {one-a one-c two-a two-b three-a three-c}
          189  +
          190  +# Verify that the ORDER BY clause is optimized out
          191  +#
          192  +do_test 2.1b {
          193  +  db eval {
          194  +    EXPLAIN QUERY PLAN
          195  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
          196  +  }
          197  +} {~/ORDER BY/}  ;# ORDER BY optimized out
          198  +
          199  +# The same query with ORDER BY clause optimization disabled via + operators
          200  +# should give exactly the same answer.
          201  +#
          202  +do_test 2.2a {
          203  +  db eval {
          204  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
          205  +  }
          206  +} {one-a one-c two-a two-b three-a three-c}
          207  +
          208  +# The output is sorted manually in this case.
          209  +#
          210  +do_test 2.2b {
          211  +  db eval {
          212  +    EXPLAIN QUERY PLAN
          213  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
          214  +  }
          215  +} {/ORDER BY/}   ;# separate sorting pass due to "+" on ORDER BY terms
          216  +
          217  +# The same query with ORDER BY optimizations turned off via built-in test.
          218  +#
          219  +do_test 2.3a {
          220  +  optimization_control db order-by-idx-join 0
          221  +  db cache flush
          222  +  db eval {
          223  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
          224  +  }
          225  +} {one-a one-c two-a two-b three-a three-c}
          226  +do_test 2.3b {
          227  +  db eval {
          228  +    EXPLAIN QUERY PLAN
          229  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
          230  +  }
          231  +} {/ORDER BY/}   ;# separate sorting pass due to disabled optimization
          232  +optimization_control db all 1
          233  +db cache flush
          234  +
          235  +# Reverse order sorts
          236  +#
          237  +do_test 2.4a {
          238  +  db eval {
          239  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
          240  +  }
          241  +} {three-a three-c two-a two-b one-a one-c}
          242  +do_test 2.4b {
          243  +  db eval {
          244  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn
          245  +  }
          246  +} {three-a three-c two-a two-b one-a one-c}  ;# verify same order after sorting
          247  +do_test 2.4c {
          248  +  db eval {
          249  +    EXPLAIN QUERY PLAN
          250  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
          251  +  }
          252  +} {/ORDER BY/}  ;# separate sorting pass due to mixed DESC/ASC
          253  +
          254  +
          255  +do_test 2.5a {
          256  +  db eval {
          257  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          258  +  }
          259  +} {one-c one-a two-b two-a three-c three-a}
          260  +do_test 2.5b {
          261  +  db eval {
          262  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
          263  +  }
          264  +} {one-c one-a two-b two-a three-c three-a}  ;# verify same order after sorting
          265  +do_test 2.5c {
          266  +  db eval {
          267  +    EXPLAIN QUERY PLAN
          268  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          269  +  }
          270  +} {/ORDER BY/}  ;# separate sorting pass due to mixed ASC/DESC
          271  +
          272  +do_test 2.6a {
          273  +  db eval {
          274  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
          275  +  }
          276  +} {three-c three-a two-b two-a one-c one-a}
          277  +do_test 2.6b {
          278  +  db eval {
          279  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
          280  +  }
          281  +} {three-c three-a two-b two-a one-c one-a}  ;# verify same order after sorting
          282  +do_test 2.6c {
          283  +  db eval {
          284  +    EXPLAIN QUERY PLAN
          285  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
          286  +  }
          287  +} {~/ORDER BY/}  ;# ORDER BY optimized-out
          288  +
          289  +
          290  +# Generate another test dataset, but this time using mixed ASC/DESC indices.
          291  +#
          292  +do_test 3.0 {
          293  +  db eval {
          294  +    BEGIN;
          295  +    DROP TABLE album;
          296  +    DROP TABLE track;
          297  +    CREATE TABLE album(
          298  +      aid INTEGER PRIMARY KEY,
          299  +      title TEXT UNIQUE NOT NULL
          300  +    );
          301  +    CREATE TABLE track(
          302  +      tid INTEGER PRIMARY KEY,
          303  +      aid INTEGER NOT NULL REFERENCES album,
          304  +      tn INTEGER NOT NULL,
          305  +      name TEXT,
          306  +      UNIQUE(aid ASC, tn DESC)
          307  +    );
          308  +    INSERT INTO album VALUES(1, '1-one'), (2, '2-two'), (3, '3-three');
          309  +    INSERT INTO track VALUES
          310  +        (NULL, 1, 1, 'one-a'),
          311  +        (NULL, 2, 2, 'two-b'),
          312  +        (NULL, 3, 3, 'three-c'),
          313  +        (NULL, 1, 3, 'one-c'),
          314  +        (NULL, 2, 1, 'two-a'),
          315  +        (NULL, 3, 1, 'three-a');
          316  +    COMMIT;
          317  +  }
          318  +} {}
          319  +do_test 3.1a {
          320  +  db eval {
          321  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          322  +  }
          323  +} {one-c one-a two-b two-a three-c three-a}
          324  +
          325  +# Verify that the ORDER BY clause is optimized out
          326  +#
          327  +do_test 3.1b {
          328  +  db eval {
          329  +    EXPLAIN QUERY PLAN
          330  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          331  +  }
          332  +} {~/ORDER BY/}  ;# ORDER BY optimized out
          333  +
          334  +# The same query with ORDER BY clause optimization disabled via + operators
          335  +# should give exactly the same answer.
          336  +#
          337  +do_test 3.2a {
          338  +  db eval {
          339  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
          340  +  }
          341  +} {one-c one-a two-b two-a three-c three-a}
          342  +
          343  +# The output is sorted manually in this case.
          344  +#
          345  +do_test 3.2b {
          346  +  db eval {
          347  +    EXPLAIN QUERY PLAN
          348  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
          349  +  }
          350  +} {/ORDER BY/}   ;# separate sorting pass due to "+" on ORDER BY terms
          351  +
          352  +# The same query with ORDER BY optimizations turned off via built-in test.
          353  +#
          354  +do_test 3.3a {
          355  +  optimization_control db order-by-idx-join 0
          356  +  db cache flush
          357  +  db eval {
          358  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          359  +  }
          360  +} {one-c one-a two-b two-a three-c three-a}
          361  +do_test 3.3b {
          362  +  db eval {
          363  +    EXPLAIN QUERY PLAN
          364  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
          365  +  }
          366  +} {/ORDER BY/}   ;# separate sorting pass due to disabled optimization
          367  +optimization_control db all 1
          368  +db cache flush
          369  +
          370  +# Without the mixed ASC/DESC on ORDER BY
          371  +#
          372  +do_test 3.4a {
          373  +  db eval {
          374  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
          375  +  }
          376  +} {one-a one-c two-a two-b three-a three-c}
          377  +do_test 3.4b {
          378  +  db eval {
          379  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
          380  +  }
          381  +} {one-a one-c two-a two-b three-a three-c}  ;# verify same order after sorting
          382  +do_test 3.4c {
          383  +  db eval {
          384  +    EXPLAIN QUERY PLAN
          385  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
          386  +  }
          387  +} {/ORDER BY/}  ;# separate sorting pass due to mismatched DESC/ASC
          388  +
          389  +
          390  +do_test 3.5a {
          391  +  db eval {
          392  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
          393  +  }
          394  +} {three-c three-a two-b two-a one-c one-a}
          395  +do_test 3.5b {
          396  +  db eval {
          397  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
          398  +  }
          399  +} {three-c three-a two-b two-a one-c one-a}  ;# verify same order after sorting
          400  +do_test 3.5c {
          401  +  db eval {
          402  +    EXPLAIN QUERY PLAN
          403  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
          404  +  }
          405  +} {/ORDER BY/}  ;# separate sorting pass due to mismatched ASC/DESC
          406  +
          407  +
          408  +do_test 3.6a {
          409  +  db eval {
          410  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
          411  +  }
          412  +} {three-a three-c two-a two-b one-a one-c}
          413  +do_test 3.6b {
          414  +  db eval {
          415  +    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn
          416  +  }
          417  +} {three-a three-c two-a two-b one-a one-c}  ;# verify same order after sorting
          418  +do_test 3.6c {
          419  +  db eval {
          420  +    EXPLAIN QUERY PLAN
          421  +    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
          422  +  }
          423  +} {~/ORDER BY/}  ;# inverted ASC/DESC is optimized out
          424  +
          425  +
          426  +finish_test

Changes to test/permutations.test.

    92     92   
    93     93   if {$::tcl_platform(platform)!="unix"} {
    94     94     set alltests [test_set $alltests -exclude crash.test crash2.test]
    95     95   }
    96     96   set alltests [test_set $alltests -exclude {
    97     97     all.test        async.test         quick.test  veryquick.test
    98     98     memleak.test    permutations.test  soak.test   fts3.test
    99         -  mallocAll.test  rtree.test
           99  +  mallocAll.test  rtree.test         full.test
   100    100   }]
   101    101   
   102    102   set allquicktests [test_set $alltests -exclude {
   103    103     async2.test async3.test backup_ioerr.test corrupt.test
   104    104     corruptC.test crash.test crash2.test crash3.test crash4.test crash5.test
   105    105     crash6.test crash7.test delete3.test e_fts3.test fts3rnd.test
   106    106     fkey_malloc.test fuzz.test fuzz3.test fuzz_malloc.test in2.test loadext.test

Changes to test/shell1.test.

   676    676     catchcmd "test.db" ".width xxx yyy"
   677    677     # this should be treated the same as a '0' width for col 1 and 2
   678    678   } {0 {}}
   679    679   do_test shell1-3.26.4 {
   680    680     catchcmd "test.db" ".width 1 1"
   681    681     # this should be treated the same as a '1' width for col 1 and 2
   682    682   } {0 {}}
          683  +do_test shell1-3.26.5 {
          684  +  catchcmd "test.db" ".mode column\n.width 10 -10\nSELECT 'abcdefg', 123456;"
          685  +  # this should be treated the same as a '1' width for col 1 and 2
          686  +} {0 {abcdefg         123456}}
          687  +do_test shell1-3.26.6 {
          688  +  catchcmd "test.db" ".mode column\n.width -10 10\nSELECT 'abcdefg', 123456;"
          689  +  # this should be treated the same as a '1' width for col 1 and 2
          690  +} {0 {   abcdefg  123456    }}
          691  +
   683    692   
   684    693   # .timer ON|OFF          Turn the CPU timer measurement on or off
   685    694   do_test shell1-3.27.1 {
   686    695     catchcmd "test.db" ".timer"
   687    696   } {1 {Error: unknown command or invalid arguments:  "timer". Enter ".help" for help}}
   688    697   do_test shell1-3.27.2 {
   689    698     catchcmd "test.db" ".timer ON"
................................................................................
   696    705     catchcmd "test.db" ".timer OFF BAD"
   697    706   } {1 {Error: unknown command or invalid arguments:  "timer". Enter ".help" for help}}
   698    707   
   699    708   do_test shell1-3-28.1 {
   700    709     catchcmd test.db \
   701    710        ".log stdout\nSELECT coalesce(sqlite_log(123,'hello'),'456');"
   702    711   } "0 {(123) hello\n456}"
          712  +
          713  +do_test shell1-3-29.1 {
          714  +  catchcmd "test.db" ".print this is a test"
          715  +} {0 {this is a test}}
   703    716   
   704    717   # Test the output of the ".dump" command
   705    718   #
   706    719   do_test shell1-4.1 {
   707    720     db eval {
   708    721       CREATE TABLE t1(x);
   709    722       INSERT INTO t1 VALUES(null), (1), (2.25), ('hello'), (x'807f');

Changes to test/spellfix.test.

    64     64     execsql { CREATE VIRTUAL TABLE t1 USING spellfix1 }
    65     65     foreach word $vocab {
    66     66       execsql { INSERT INTO t1(word) VALUES($word) }
    67     67     }
    68     68   } {}
    69     69   
    70     70   foreach {tn word res} {
    71         -  1   raxpi*     {rasping 5 rasped 5 raspberry 6 rasp 4 rasps 4}
           71  +  1   raxpi*     {rasping 5 rasped 5 ragweed 5 raspberry 6 rasp 4}
    72     72     2   ril*       {rail 4 railed 4 railer 4 railers 4 railing 4}
    73     73     3   rilis*     {realism 6 realist 6 realistic 6 realistically 6 realists 6}
    74     74     4   reail*     {real 3 realest 3 realign 3 realigned 3 realigning 3}
    75     75     5   ras*       {rascal 3 rascally 3 rascals 3 rash 3 rasher 3}
    76     76     6   realistss* {realists 8 realigns 8 realistic 9 realistically 9 realest 7}
    77     77     7   realistss  {realists 8 realist 7 realigns 8 realistic 9 realest 7}
    78     78     8   rllation*  {realities 9 reality 7 rallied 7 railed 4}

Changes to test/stat.test.

    72     72       DROP TABLE t1;
    73     73     }
    74     74   } {}
    75     75   
    76     76   do_execsql_test stat-2.1 {
    77     77     CREATE TABLE t3(a PRIMARY KEY, b);
    78     78     INSERT INTO t3(rowid, a, b) VALUES(2, a_string(111), a_string(222));
    79         -  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    80         -  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    81         -  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    82         -  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
    83         -  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3;
           79  +  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
           80  +   ORDER BY rowid;
           81  +  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
           82  +   ORDER BY rowid;
           83  +  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
           84  +   ORDER BY rowid;
           85  +  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
           86  +   ORDER BY rowid;
           87  +  INSERT INTO t3 SELECT a_string(110+rowid), a_string(221+rowid) FROM t3
           88  +   ORDER BY rowid;
    84     89     SELECT name, path, pageno, pagetype, ncell, payload, unused, mx_payload
    85     90       FROM stat WHERE name != 'sqlite_master';
    86     91   } [list \
    87     92     sqlite_autoindex_t3_1 / 3 internal 3 368 623 125       \
    88     93     sqlite_autoindex_t3_1 /000/ 8 leaf 8 946 46 123        \
    89     94     sqlite_autoindex_t3_1 /001/ 9 leaf 8 988 2 131         \
    90     95     sqlite_autoindex_t3_1 /002/ 15 leaf 7 857 137 132      \

Changes to test/tclsqlite.test.

   139    139     set v [catch {db collation_needed} msg]
   140    140     lappend v $msg
   141    141   } {1 {wrong # args: should be "db collation_needed SCRIPT"}}
   142    142   do_test tcl-1.21 {
   143    143     set v [catch {db total_changes xyz} msg]
   144    144     lappend v $msg
   145    145   } {1 {wrong # args: should be "db total_changes "}}
   146         -do_test tcl-1.20 {
          146  +do_test tcl-1.22 {
   147    147     set v [catch {db copy} msg]
   148    148     lappend v $msg
   149    149   } {1 {wrong # args: should be "db copy CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"}}
   150         -do_test tcl-1.21 {
          150  +do_test tcl-1.23 {
   151    151     set v [catch {sqlite3 db2 test.db -vfs nosuchvfs} msg]
   152    152     lappend v $msg
   153    153   } {1 {no such vfs: nosuchvfs}}
   154    154   
   155    155   catch {unset ::result}
   156    156   do_test tcl-2.1 {
   157    157     execsql "CREATE TABLE t\u0123x(a int, b\u1235 float)"

Changes to test/tester.tcl.

    27     27   #      drop_all_tables        ?DB?
    28     28   #      forcecopy              FROM TO
    29     29   #      forcedelete            FILENAME
    30     30   #
    31     31   # Test the capability of the SQLite version built into the interpreter to
    32     32   # determine if a specific test can be run:
    33     33   #
           34  +#      capable                EXPR
    34     35   #      ifcapable              EXPR
    35     36   #
    36     37   # Calulate checksums based on database contents:
    37     38   #
    38     39   #      dbcksum                DB DBNAME
    39     40   #      allcksum               ?DB?
    40     41   #      cksum                  ?DB?
................................................................................
   130    131   
   131    132   proc getFileRetries {} {
   132    133     if {![info exists ::G(file-retries)]} {
   133    134       #
   134    135       # NOTE: Return the default number of retries for [file] operations.  A
   135    136       #       value of zero or less here means "disabled".
   136    137       #
   137         -    return [expr {$::tcl_platform(platform) eq "windows" ? 10 : 0}]
          138  +    return [expr {$::tcl_platform(platform) eq "windows" ? 50 : 0}]
   138    139     }
   139    140     return $::G(file-retries)
   140    141   }
   141    142   
   142    143   proc getFileRetryDelay {} {
   143    144     if {![info exists ::G(file-retry-delay)]} {
   144    145       #
................................................................................
   558    559     if {![info exists ::G(match)] || [string match $::G(match) $name]} {
   559    560       if {[catch {uplevel #0 "$cmd;\n"} result]} {
   560    561         puts "\nError: $result"
   561    562         fail_test $name
   562    563       } else {
   563    564         if {[regexp {^~?/.*/$} $expected]} {
   564    565           if {[string index $expected 0]=="~"} {
   565         -          set re [string range $expected 2 end-1]
          566  +          set re [string map {# {[-0-9.]+}} [string range $expected 2 end-1]]
   566    567             set ok [expr {![regexp $re $result]}]
   567    568           } else {
   568         -          set re [string range $expected 1 end-1]
          569  +          set re [string map {# {[-0-9.]+}} [string range $expected 1 end-1]]
   569    570             set ok [regexp $re $result]
   570    571           }
   571    572         } else {
   572    573           set ok [expr {[string compare $result $expected]==0}]
   573    574         }
   574    575         if {!$ok} {
          576  +        # if {![info exists ::testprefix] || $::testprefix eq ""} {
          577  +        #   error "no test prefix"
          578  +        # }
   575    579           puts "\nExpected: \[$expected\]\n     Got: \[$result\]"
   576    580           fail_test $name
   577    581         } else {
   578    582           puts " Ok"
   579    583         }
   580    584       }
   581    585     } else {
................................................................................
  1018   1022       }
  1019   1023       append ret $char
  1020   1024       set state $newstate
  1021   1025     }
  1022   1026     if {$state} {append ret )}
  1023   1027     return $ret
  1024   1028   }
         1029  +
         1030  +# Returns non-zero if the capabilities are present; zero otherwise.
         1031  +#
         1032  +proc capable {expr} {
         1033  +  set e [fix_ifcapable_expr $expr]; return [expr ($e)]
         1034  +}
  1025   1035   
  1026   1036   # Evaluate a boolean expression of capabilities.  If true, execute the
  1027   1037   # code.  Omit the code if false.
  1028   1038   #
  1029   1039   proc ifcapable {expr code {else ""} {elsecode ""}} {
  1030   1040     #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
  1031   1041     set e2 [fix_ifcapable_expr $expr]

Changes to test/tkt-385a5b56b9.test.

    35     35   }
    36     36   
    37     37   do_eqp_test 2.1 { SELECT DISTINCT x FROM t2 } {
    38     38     0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2x (~1000000 rows)}
    39     39   }
    40     40   
    41     41   do_eqp_test 2.2 { SELECT DISTINCT y FROM t2 } {
    42         -  0 0 0 {SCAN TABLE t2 (~1000000 rows)}
           42  +  0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2y (~1000000 rows)}
    43     43   }
    44     44   
    45     45   do_eqp_test 2.3 { SELECT DISTINCT x, y FROM t2 WHERE y=10 } {
    46     46     0 0 0 {SEARCH TABLE t2 USING INDEX t2y (y=?) (~1 rows)}
    47     47   }
    48     48   
    49     49   do_eqp_test 2.4 { SELECT DISTINCT x, y FROM t2 WHERE x=10 } {
    50     50     0 0 0 {SEARCH TABLE t2 USING INDEX t2x (x=?) (~1 rows)}
    51     51   }
    52     52   
    53     53   finish_test
    54         -

Changes to test/tkt-5d863f876e.test.

    13     13   # This file implements tests to verify that ticket [5d863f876e] has been
    14     14   # fixed.  
    15     15   #
    16     16   
    17     17   set testdir [file dirname $argv0]
    18     18   source $testdir/tester.tcl
    19     19   source $testdir/lock_common.tcl
    20         -
    21         -ifcapable !wal { finish_test; return }
    22         -if ![wal_is_ok] { finish_test; return }
    23         -
           20  +set ::testprefix tkt-5d863f876e
           21  +ifcapable !wal {finish_test ; return }
    24     22   
    25     23   do_multiclient_test tn {
    26     24     do_test $tn.1 {
    27     25       sql1 {
    28     26         CREATE TABLE t1(a, b);
    29     27         CREATE INDEX i1 ON t1(a, b);
    30     28         INSERT INTO t1 VALUES(1, 2);

Changes to test/tkt-78e04e52ea.test.

    40     40       CREATE INDEX i1 ON ""("" COLLATE nocase);
    41     41     }
    42     42   } {}
    43     43   do_test tkt-78e04-1.4 {
    44     44     execsql {
    45     45       EXPLAIN QUERY PLAN SELECT * FROM "" WHERE "" LIKE 'abc%';
    46     46     }
    47         -} {0 0 0 {SCAN TABLE  (~500000 rows)}}
           47  +} {0 0 0 {SCAN TABLE  USING COVERING INDEX i1 (~500000 rows)}}
    48     48   do_test tkt-78e04-1.5 {
    49     49     execsql {
    50     50       DROP TABLE "";
    51     51       SELECT name FROM sqlite_master;
    52     52     }
    53     53   } {t2}
    54     54   

Changes to test/tkt-80ba201079.test.

    13     13   # resolved.  That ticket is about an incorrect result that appears when
    14     14   # an index is added.  The root cause is that a constant is being used
    15     15   # without initialization when the OR optimization applies in the WHERE clause.
    16     16   #
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20         -set ::testprefix tkt-80ba2
           20  +set ::testprefix tkt-80ba201079
    21     21   
    22     22   do_test tkt-80ba2-100 {
    23     23     db eval {
    24     24       CREATE TABLE t1(a);
    25     25       INSERT INTO t1 VALUES('A');
    26     26       CREATE TABLE t2(b);
    27     27       INSERT INTO t2 VALUES('B');

Changes to test/tkt-cbd054fa6b.test.

    46     46   do_test tkt-cbd05-1.3 {
    47     47     execsql { 
    48     48       SELECT tbl,idx,group_concat(sample,' ') 
    49     49       FROM sqlite_stat3 
    50     50       WHERE idx = 't1_x' 
    51     51       GROUP BY tbl,idx
    52     52     }
    53         -} {t1 t1_x { A B C D E F G H I}}
           53  +} {/t1 t1_x .[ ABCDEFGHI]{10}./}
    54     54   
    55     55   do_test tkt-cbd05-2.1 {
    56     56     db eval {
    57     57       DROP TABLE t1;
    58     58       CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB UNIQUE NOT NULL);
    59     59       CREATE INDEX t1_x ON t1(b);
    60     60       INSERT INTO t1 VALUES(NULL, X'');
................................................................................
    78     78   do_test tkt-cbd05-2.3 {
    79     79     execsql { 
    80     80       SELECT tbl,idx,group_concat(sample,' ') 
    81     81       FROM sqlite_stat3 
    82     82       WHERE idx = 't1_x' 
    83     83       GROUP BY tbl,idx
    84     84     }
    85         -} {t1 t1_x { A B C D E F G H I}}
           85  +} {/t1 t1_x .[ ABCDEFGHI]{10}./}
    86     86   
    87     87   finish_test

Changes to test/triggerC.test.

   218    218   } {
   219    219     do_test triggerC-2.1.$n {
   220    220       catchsql { DROP TRIGGER t2_trig }
   221    221       execsql  { DELETE FROM t2 }
   222    222       execsql  $tdefn
   223    223       catchsql {
   224    224         INSERT INTO t2 VALUES(10);
   225         -      SELECT * FROM t2;
          225  +      SELECT * FROM t2 ORDER BY rowid;
   226    226       }
   227    227     } $rc
   228    228   }
   229    229   
   230    230   do_test triggerC-2.2 {
   231    231     execsql "
   232    232       CREATE TABLE t22(x);
................................................................................
   543    543        2 integer 9.1 text 9.1 real    9.1 real
   544    544     }
   545    545   } {
   546    546     do_test triggerC-4.1.$n {
   547    547       eval concat [execsql " 
   548    548         DELETE FROM log;
   549    549         $insert ; 
   550         -      SELECT * FROM log;
          550  +      SELECT * FROM log ORDER BY rowid;
   551    551       "]
   552    552     } [join $log " "]
   553    553   } 
   554    554   
   555    555   #-------------------------------------------------------------------------
   556    556   # This block of tests, triggerC-5.*, test that DELETE triggers are fired
   557    557   # if a row is deleted as a result of OR REPLACE conflict resolution.
................................................................................
   580    580     6 "INSERT OR REPLACE INTO t5 VALUES(2, 'c')"        {2 b 3 3 c 2} {1 a 2 c}
   581    581     7 "UPDATE OR REPLACE t5 SET a=1, b='b' WHERE a = 3" {1 a 3 2 b 2} {1 b}
   582    582   } {
   583    583     do_test triggerC-5.1.$n {
   584    584       execsql "
   585    585         BEGIN;
   586    586           $dml ;
   587         -        SELECT * FROM t5g;
   588         -        SELECT * FROM t5;
          587  +        SELECT * FROM t5g ORDER BY rowid;
          588  +        SELECT * FROM t5 ORDER BY rowid;
   589    589         ROLLBACK;
   590    590       "
   591    591     } [concat $t5g $t5]
   592    592   }
   593    593   do_test triggerC-5.2.0 {
   594    594     execsql {
   595    595       DROP TRIGGER t5t;
................................................................................
   607    607     6 "INSERT OR REPLACE INTO t5 VALUES(2, 'c')"        {2 b 2 3 c 1} {1 a 2 c}
   608    608     7 "UPDATE OR REPLACE t5 SET a=1, b='b' WHERE a = 3" {1 a 2 2 b 1} {1 b}
   609    609   } {
   610    610     do_test triggerC-5.2.$n {
   611    611       execsql "
   612    612         BEGIN;
   613    613           $dml ;
   614         -        SELECT * FROM t5g;
   615         -        SELECT * FROM t5;
          614  +        SELECT * FROM t5g ORDER BY rowid;
          615  +        SELECT * FROM t5 ORDER BY rowid;
   616    616         ROLLBACK;
   617    617       "
   618    618     } [concat $t5g $t5]
   619    619   }
   620    620   do_test triggerC-5.3.0 {
   621    621     execsql { PRAGMA recursive_triggers = off }
   622    622   } {}
................................................................................
   629    629     6 "INSERT OR REPLACE INTO t5 VALUES(2, 'c')"        {} {1 a 2 c}
   630    630     7 "UPDATE OR REPLACE t5 SET a=1, b='b' WHERE a = 3" {} {1 b}
   631    631   } {
   632    632     do_test triggerC-5.3.$n {
   633    633       execsql "
   634    634         BEGIN;
   635    635           $dml ;
   636         -        SELECT * FROM t5g;
   637         -        SELECT * FROM t5;
          636  +        SELECT * FROM t5g ORDER BY rowid;
          637  +        SELECT * FROM t5 ORDER BY rowid;
   638    638         ROLLBACK;
   639    639       "
   640    640     } [concat $t5g $t5]
   641    641   }
   642    642   do_test triggerC-5.3.8 {
   643    643     execsql { PRAGMA recursive_triggers = on }
   644    644   } {}

Changes to test/unordered.test.

    47     47           {0 0 0 {SCAN TABLE t1 (~42 rows)}}
    48     48       3   "SELECT * FROM t1 WHERE a = ? ORDER BY rowid"
    49     49           {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~1 rows)}}
    50     50           {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~1 rows)} 
    51     51            0 0 0 {USE TEMP B-TREE FOR ORDER BY}}
    52     52       4   "SELECT max(a) FROM t1"
    53     53           {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (~1 rows)}}
    54         -        {0 0 0 {SEARCH TABLE t1 (~1 rows)}}
           54  +        {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (~1 rows)}}
    55     55       5   "SELECT group_concat(b) FROM t1 GROUP BY a"
    56     56           {0 0 0 {SCAN TABLE t1 USING INDEX i1 (~128 rows)}}
    57     57           {0 0 0 {SCAN TABLE t1 (~128 rows)} 0 0 0 {USE TEMP B-TREE FOR GROUP BY}}
    58     58   
    59     59       6   "SELECT * FROM t1 WHERE a = ?"
    60     60           {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~1 rows)}}
    61     61           {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?) (~1 rows)}}

Changes to test/wal8.test.

    22     22   # size from the database file as soon as it is opened (even before the
    23     23   # first read transaction is executed), and the "PRAGMA page_size = XXX"
    24     24   # is a no-op.
    25     25   #
    26     26   set testdir [file dirname $argv0]
    27     27   source $testdir/tester.tcl
    28     28   set ::testprefix wal8
           29  +
    29     30   ifcapable !wal {finish_test ; return }
    30     31   if ![wal_is_ok] { finish_test; return }
    31     32   
    32     33   db close
    33     34   forcedelete test.db test.db-wal
    34     35   
    35     36   sqlite3 db test.db
................................................................................
    85     86   
    86     87   do_execsql_test 3.1 {
    87     88     PRAGMA page_size = 4096;
    88     89     SELECT name FROM sqlite_master;
    89     90   } {t1}
    90     91   
    91     92   finish_test
    92         -

Changes to test/where.test.

  1094   1094       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, y.b DESC
  1095   1095     } 
  1096   1096   } {1/1 1/4 4/1 4/4 sort}
  1097   1097   do_test where-14.3 {
  1098   1098     cksort {
  1099   1099       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b
  1100   1100     } 
  1101         -} {1/1 1/4 4/1 4/4 nosort}
         1101  +} {1/4 1/1 4/4 4/1 nosort}
  1102   1102   do_test where-14.4 {
  1103   1103     cksort {
  1104   1104       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.a, x.b DESC
  1105   1105     } 
  1106         -} {1/1 1/4 4/1 4/4 nosort}
         1106  +} {1/4 1/1 4/4 4/1 nosort}
  1107   1107   do_test where-14.5 {
  1108   1108     # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
  1109   1109     cksort {
  1110   1110       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b
  1111   1111     } 
  1112         -} {4/1 4/4 1/1 1/4 sort}
         1112  +} {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
  1113   1113   do_test where-14.6 {
  1114   1114     # This test case changed from "nosort" to "sort". See ticket 2a5629202f.
  1115   1115     cksort {
  1116   1116       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a||x.b DESC
  1117   1117     } 
  1118         -} {4/1 4/4 1/1 1/4 sort}
         1118  +} {/4/[14] 4/[14] 1/[14] 1/[14] sort/}
  1119   1119   do_test where-14.7 {
  1120   1120     cksort {
  1121   1121       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b
  1122   1122     } 
  1123   1123   } {4/1 4/4 1/1 1/4 sort}
  1124   1124   do_test where-14.7.1 {
  1125   1125     cksort {
................................................................................
  1126   1126       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, y.a||y.b
  1127   1127     } 
  1128   1128   } {4/1 4/4 1/1 1/4 sort}
  1129   1129   do_test where-14.7.2 {
  1130   1130     cksort {
  1131   1131       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, x.a, x.a||x.b
  1132   1132     } 
  1133         -} {4/1 4/4 1/1 1/4 nosort}
         1133  +} {4/4 4/1 1/4 1/1 nosort}
  1134   1134   do_test where-14.8 {
  1135   1135     cksort {
  1136   1136       SELECT x.a || '/' || y.a FROM t8 x, t8 y ORDER BY x.b, y.a||y.b DESC
  1137   1137     } 
  1138   1138   } {4/4 4/1 1/4 1/1 sort}
  1139   1139   do_test where-14.9 {
  1140   1140     cksort {

Changes to test/where9.test.

   688    688           OR f='fghijklmn'
   689    689           OR g='hgfedcb'
   690    690     }
   691    691   } {scan 0 sort 0}   ;#  Add 100 to rowids 5 31 57 82 83 84 85 86 87
   692    692   do_test where9-6.5.4 {
   693    693     db eval {
   694    694       SELECT count(*) FROM t1 UNION ALL
   695         -    SELECT a FROM t1 WHERE a%100 IN (5,31,57,82,83,84,85,86,87);
          695  +    SELECT a FROM t1 WHERE a%100 IN (5,31,57,82,83,84,85,86,87) ORDER BY rowid;
   696    696       ROLLBACK;
   697    697     }
   698    698   } {99 105 131 157 182 183 184 185 186 187}
   699    699   
   700    700   do_test where9-6.6.1 {
   701    701     count_steps {
   702    702       BEGIN;

Changes to test/zerodamage.test.

    14     14   #
    15     15   # The name of this file comes from the fact that we used to call the
    16     16   # POWERSAFE_OVERWRITE property ZERO_DAMAGE.
    17     17   #
    18     18   
    19     19   set testdir [file dirname $argv0]
    20     20   source $testdir/tester.tcl
    21         -set testprefix wal5
           21  +set testprefix zerodamage
    22     22   
    23     23   ifcapable !vtab {
    24     24     finish_test
    25     25     return
    26     26   }
    27     27   
    28     28   # POWERSAFE_OVERWRITE defaults to true
................................................................................
    85     85     sqlite3 db file:test.db?psow=FALSE -uri 1
    86     86     db eval {
    87     87       UPDATE t1 SET y=randomblob(50) WHERE x=124;
    88     88     }
    89     89     concat [file_control_powersafe_overwrite db -1] [set ::max_journal_size]
    90     90   } {0 0 24704}
    91     91   
    92         -# Run a WAL-mode transaction with POWERSAFE_OVERWRITE on to verify that the
    93         -# WAL file does not get too big.
    94         -#
    95         -do_test zerodamage-3.0 {
    96         -  db eval {
    97         -     PRAGMA journal_mode=WAL;
    98         -  }
    99         -  db close
   100         -  sqlite3 db file:test.db?psow=TRUE -uri 1
   101         -  db eval {
   102         -     UPDATE t1 SET y=randomblob(50) WHERE x=124;
   103         -  }
   104         -  file size test.db-wal
   105         -} {1080}
           92  +ifcapable wal {
           93  +  # Run a WAL-mode transaction with POWERSAFE_OVERWRITE on to verify that the
           94  +  # WAL file does not get too big.
           95  +  #
           96  +  do_test zerodamage-3.0 {
           97  +    db eval {
           98  +       PRAGMA journal_mode=WAL;
           99  +    }
          100  +    db close
          101  +    sqlite3 db file:test.db?psow=TRUE -uri 1
          102  +    db eval {
          103  +       UPDATE t1 SET y=randomblob(50) WHERE x=124;
          104  +    }
          105  +    file size test.db-wal
          106  +  } {1080}
   106    107   
   107         -# Repeat the previous with POWERSAFE_OVERWRITE off.  Verify that the WAL file
   108         -# is padded.
   109         -#
   110         -do_test zerodamage-3.1 {
   111         -  db close
   112         -  sqlite3 db file:test.db?psow=FALSE -uri 1
   113         -  db eval {
   114         -     UPDATE t1 SET y=randomblob(50) WHERE x=124;
   115         -  }
   116         -  file size test.db-wal
   117         -} {8416}
          108  +  # Repeat the previous with POWERSAFE_OVERWRITE off.  Verify that the WAL file
          109  +  # is padded.
          110  +  #
          111  +  do_test zerodamage-3.1 {
          112  +    db close
          113  +    sqlite3 db file:test.db?psow=FALSE -uri 1
          114  +    db eval {
          115  +       UPDATE t1 SET y=randomblob(50) WHERE x=124;
          116  +    }
          117  +    file size test.db-wal
          118  +  } {8416}
          119  +}
   118    120   
   119    121   finish_test

Added tool/stack_usage.tcl.

            1  +#!/usr/bin/tclsh
            2  +#
            3  +# Parse the output of 
            4  +#
            5  +#         objdump -d sqlite3.o
            6  +#
            7  +# for x64 and generate a report showing:
            8  +#
            9  +#    (1)  Stack used by each function
           10  +#    (2)  Recursion paths and their aggregate stack depth
           11  +#
           12  +set getStack 0
           13  +while {![eof stdin]} {
           14  +  set line [gets stdin]
           15  +  if {[regexp {^[0-9a-f]+ <([^>]+)>:\s*$} $line all procname]} {
           16  +    set curfunc $procname
           17  +    set root($curfunc) 1
           18  +    set calls($curfunc) {}
           19  +    set calledby($curfunc) {}
           20  +    set recursive($curfunc) {}
           21  +    set stkdepth($curfunc) 0
           22  +    set getStack 1
           23  +    continue
           24  +  }
           25  +  if {[regexp {callq? +[0-9a-z]+ <([^>]+)>} $line all other]} {
           26  +    set key [list $curfunc $other]
           27  +    set callpair($key) 1
           28  +    unset -nocomplain root($curfunc)
           29  +    continue
           30  +  }
           31  +  if {[regexp {sub +\$(0x[0-9a-z]+),%[er]sp} $line all xdepth]} {
           32  +    if {$getStack} {
           33  +      scan $xdepth %x depth
           34  +      set stkdepth($curfunc) $depth
           35  +      set getStack 0
           36  +    }
           37  +    continue
           38  +  }
           39  +}
           40  +
           41  +puts "****************** Stack Usage By Function ********************"
           42  +set sdlist {}
           43  +foreach f [array names stkdepth] {
           44  +  lappend sdlist [list $stkdepth($f) $f]
           45  +}
           46  +foreach sd [lsort -integer -decr -index 0 $sdlist] {
           47  +  foreach {depth fname} $sd break
           48  +  puts [format {%6d %s} $depth $fname]
           49  +}
           50  +
           51  +puts "****************** Stack Usage By Recursion *******************"
           52  +foreach key [array names callpair] {
           53  +  foreach {from to} $key break
           54  +  lappend calls($from) $to
           55  +  # lappend calledby($to) $from
           56  +}
           57  +proc all_descendents {root} {
           58  +  global calls recursive
           59  +  set todo($root) $root
           60  +  set go 1
           61  +  while {$go} {
           62  +    set go 0
           63  +    foreach f [array names todo] {
           64  +      set path $todo($f)
           65  +      unset todo($f)
           66  +      if {![info exists calls($f)]} continue
           67  +      foreach x $calls($f) {
           68  +        if {$x==$root} {
           69  +          lappend recursive($root) [concat $path $root]
           70  +        } elseif {![info exists d($x)]} {
           71  +          set go 1
           72  +          set todo($x) [concat $path $x]
           73  +          set d($x) 1
           74  +        }
           75  +      }
           76  +    }
           77  +  }
           78  +  return [array names d]
           79  +}
           80  +set pathlist {}
           81  +foreach f [array names recursive] {
           82  +  all_descendents $f
           83  +  foreach m $recursive($f) {
           84  +    set depth 0
           85  +    foreach b [lrange $m 0 end-1] {
           86  +      set depth [expr {$depth+$stkdepth($b)}]
           87  +    }
           88  +    lappend pathlist [list $depth $m]
           89  +  }
           90  +}
           91  +foreach path [lsort -integer -decr -index 0 $pathlist] {
           92  +  foreach {depth m} $path break
           93  +  set first [lindex $m 0]
           94  +  puts [format {%6d %s %d} $depth $first $stkdepth($first)]
           95  +  foreach b [lrange $m 1 end] {
           96  +    puts "          $b $stkdepth($b)"
           97  +  }
           98  +}

Changes to tool/vdbe-compress.tcl.

    75     75             append afterUnion \
    76     76                "#if 0  /* local variables moved into u.$sname */\n"
    77     77             set seenDecl 1
    78     78           }
    79     79           append unionDef "    $line\n"
    80     80           append afterUnion $line\n
    81     81           lappend vlist $vname
           82  +      } elseif {[regexp {^#(if|endif)} $line] && [llength $vlist]>0} {
           83  +        append unionDef "$line\n"
           84  +        append afterUnion $line\n
    82     85         } else {
    83     86           break
    84     87         }
    85     88       }
    86     89       if {$seenDecl} {
    87     90         append unionDef   "    \175 $sname;\n"
    88     91         append afterUnion "#endif /* local variables moved into u.$sname */\n"