/ Check-in [4a470741]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Merge updates from trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | configReadOnly
Files: files | file ages | folders
SHA1: 4a470741b65938dd38418c9c1a6e87dad6a84fc7
User & Date: mistachkin 2012-09-25 12:45:03
Context
2012-10-03
20:20
Add experimental sqlite3_reconfig() interface to more fully support the SQLITE_CONFIG_READONLY option. check-in: 9dc2eaa6 user: mistachkin tags: configReadOnly
2012-09-25
12:45
Merge updates from trunk. check-in: 4a470741 user: mistachkin tags: configReadOnly
2012-09-24
19:50
Remove an unused subfunction parameter and an obsolete comment from the query planner logic in where.c. check-in: 349a55cd user: drh tags: trunk
2012-09-10
10:29
Initial prototype of SQLITE_CONFIG_READONLY. check-in: 7c340165 user: mistachkin tags: configReadOnly
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   884    884   
   885    885   
   886    886   fulltest:	testfixture$(TEXE) sqlite3$(TEXE)
   887    887   	./testfixture$(TEXE) $(TOP)/test/all.test
   888    888   
   889    889   soaktest:	testfixture$(TEXE) sqlite3$(TEXE)
   890    890   	./testfixture$(TEXE) $(TOP)/test/all.test -soak=1
          891  +
          892  +fulltestonly:	testfixture$(TEXE) sqlite3$(TEXE)
          893  +	./testfixture$(TEXE) $(TOP)/test/full.test
   891    894   
   892    895   test:	testfixture$(TEXE) sqlite3$(TEXE)
   893    896   	./testfixture$(TEXE) $(TOP)/test/veryquick.test
   894    897   
   895    898   sqlite3_analyzer.c: sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c $(TOP)/tool/spaceanal.tcl
   896    899   	echo "#define TCLSH 2" > $@
   897    900   	cat sqlite3.c $(TOP)/src/test_stat.c $(TOP)/src/tclsqlite.c >> $@

Changes to Makefile.msc.

  1164   1164   		/link $(LTLINKOPTS) $(LTLIBPATHS) sqlite3res.lo $(LTLIBS) $(TLIBS)
  1165   1165   
  1166   1166   fulltest:	testfixture.exe sqlite3.exe
  1167   1167   	.\testfixture.exe $(TOP)\test\all.test
  1168   1168   
  1169   1169   soaktest:	testfixture.exe sqlite3.exe
  1170   1170   	.\testfixture.exe $(TOP)\test\all.test -soak=1
         1171  +
         1172  +fulltestonly:	testfixture.exe sqlite3.exe
         1173  +	.\testfixture.exe $(TOP)\test\full.test
  1171   1174   
  1172   1175   test:	testfixture.exe sqlite3.exe
  1173   1176   	.\testfixture.exe $(TOP)\test\veryquick.test
  1174   1177   
  1175   1178   sqlite3_analyzer.c: sqlite3.c $(TOP)\src\test_stat.c $(TOP)\src\tclsqlite.c $(TOP)\tool\spaceanal.tcl
  1176   1179   	copy sqlite3.c + $(TOP)\src\test_stat.c + $(TOP)\src\tclsqlite.c $@
  1177   1180   	echo static const char *tclsh_main_loop(void){ >> $@

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.

   560    560   		-o testfixture$(EXE) $(LIBTCL) $(THREADLIB)
   561    561   
   562    562   fulltest:	testfixture$(EXE) sqlite3$(EXE)
   563    563   	./testfixture$(EXE) $(TOP)/test/all.test
   564    564   
   565    565   soaktest:	testfixture$(EXE) sqlite3$(EXE)
   566    566   	./testfixture$(EXE) $(TOP)/test/all.test -soak=1
          567  +
          568  +fulltestonly:	testfixture$(EXE) sqlite3$(EXE)
          569  +	./testfixture$(EXE) $(TOP)/test/full.test
   567    570   
   568    571   test:	testfixture$(EXE) sqlite3$(EXE)
   569    572   	./testfixture$(EXE) $(TOP)/test/veryquick.test
   570    573   
   571    574   # The next two rules are used to support the "threadtest" target. Building
   572    575   # threadtest runs a few thread-safety tests that are implemented in C. This
   573    576   # 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/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;
................................................................................
  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.

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.

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

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      0,                         /* bReadOnly */
   146    151      0x7ffffffe,                /* mxStrlen */
   147    152      128,                       /* szLookaside */
   148    153      500,                       /* nLookaside */
   149    154      {0,0,0,0,0,0,0,0},         /* m */
   150    155      {0,0,0,0,0,0,0,0,0},       /* mutex */
   151    156      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */

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.

   470    470         break;
   471    471       }
   472    472   
   473    473       case SQLITE_CONFIG_URI: {
   474    474         sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
   475    475         break;
   476    476       }
          477  +
          478  +    case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
          479  +      sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
          480  +      break;
          481  +    }
   477    482   
   478    483       case SQLITE_CONFIG_READONLY: {
   479    484         sqlite3GlobalConfig.bReadOnly = va_arg(ap, int);
   480    485         break;
   481    486       }
   482    487   
   483    488       default: {
................................................................................
  1767   1772       return SQLITE_MISUSE_BKPT;
  1768   1773     }
  1769   1774     if( !db || db->mallocFailed ){
  1770   1775       return SQLITE_NOMEM;
  1771   1776     }
  1772   1777     return db->errCode;
  1773   1778   }
         1779  +
         1780  +/*
         1781  +** Return a string that describes the kind of error specified in the
         1782  +** argument.  For now, this simply calls the internal sqlite3ErrStr()
         1783  +** function.
         1784  +*/
         1785  +const char *sqlite3_errstr(int rc){
         1786  +  return sqlite3ErrStr(rc);
         1787  +}
  1774   1788   
  1775   1789   /*
  1776   1790   ** Create a new collating function for database "db".  The name is zName
  1777   1791   ** and the encoding is enc.
  1778   1792   */
  1779   1793   static int createCollation(
  1780   1794     sqlite3* db,
................................................................................
  2753   2767     **     2. The table is not a view and the column name identified an 
  2754   2768     **        explicitly declared column. Copy meta information from *pCol.
  2755   2769     */ 
  2756   2770     if( pCol ){
  2757   2771       zDataType = pCol->zType;
  2758   2772       zCollSeq = pCol->zColl;
  2759   2773       notnull = pCol->notNull!=0;
  2760         -    primarykey  = pCol->isPrimKey!=0;
         2774  +    primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
  2761   2775       autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
  2762   2776     }else{
  2763   2777       zDataType = "INTEGER";
  2764   2778       primarykey = 1;
  2765   2779     }
  2766   2780     if( !zCollSeq ){
  2767   2781       zCollSeq = "BINARY";

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);
................................................................................
  2930   2951     int nName;                         /* Size of zName in bytes */
  2931   2952   
  2932   2953     assert( pDbFd->pShm==0 );    /* Not previously opened */
  2933   2954   
  2934   2955     /* Allocate space for the new sqlite3_shm object.  Also speculatively
  2935   2956     ** allocate space for a new winShmNode and filename.
  2936   2957     */
  2937         -  p = sqlite3_malloc( sizeof(*p) );
         2958  +  p = sqlite3MallocZero( sizeof(*p) );
  2938   2959     if( p==0 ) return SQLITE_IOERR_NOMEM;
  2939         -  memset(p, 0, sizeof(*p));
  2940   2960     nName = sqlite3Strlen30(pDbFd->zPath);
  2941         -  pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
         2961  +  pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
  2942   2962     if( pNew==0 ){
  2943   2963       sqlite3_free(p);
  2944   2964       return SQLITE_IOERR_NOMEM;
  2945   2965     }
  2946         -  memset(pNew, 0, sizeof(*pNew) + nName + 17);
  2947   2966     pNew->zFilename = (char*)&pNew[1];
  2948   2967     sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
  2949   2968     sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename); 
  2950   2969   
  2951   2970     /* Look to see if there is an existing winShmNode that can be used.
  2952   2971     ** If no matching winShmNode currently exists, create a new one.
  2953   2972     */
................................................................................
  3276   3295       if( !apNew ){
  3277   3296         rc = SQLITE_IOERR_NOMEM;
  3278   3297         goto shmpage_out;
  3279   3298       }
  3280   3299       pShmNode->aRegion = apNew;
  3281   3300   
  3282   3301       while( pShmNode->nRegion<=iRegion ){
  3283         -      HANDLE hMap;                /* file-mapping handle */
         3302  +      HANDLE hMap = NULL;         /* file-mapping handle */
  3284   3303         void *pMap = 0;             /* Mapped memory region */
  3285   3304        
  3286   3305   #if SQLITE_OS_WINRT
  3287   3306         hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
  3288   3307             NULL, PAGE_READWRITE, nByte, NULL
  3289   3308         );
  3290         -#else
         3309  +#elif defined(SQLITE_WIN32_HAS_WIDE)
  3291   3310         hMap = osCreateFileMappingW(pShmNode->hFile.h, 
  3292   3311             NULL, PAGE_READWRITE, 0, nByte, NULL
  3293   3312         );
         3313  +#elif defined(SQLITE_WIN32_HAS_ANSI)
         3314  +      hMap = osCreateFileMappingA(pShmNode->hFile.h, 
         3315  +          NULL, PAGE_READWRITE, 0, nByte, NULL
         3316  +      );
  3294   3317   #endif
  3295   3318         OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
  3296   3319                  (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
  3297   3320                  hMap ? "ok" : "failed"));
  3298   3321         if( hMap ){
  3299   3322           int iOffset = pShmNode->nRegion*szRegion;
  3300   3323           int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
................................................................................
  4035   4058     }else{
  4036   4059       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
  4037   4060     }
  4038   4061     return SQLITE_OK;
  4039   4062   #endif
  4040   4063   
  4041   4064   #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
  4042         -  int nByte;
         4065  +  DWORD nByte;
  4043   4066     void *zConverted;
  4044   4067     char *zOut;
  4045   4068   
  4046   4069     /* If this path name begins with "/X:", where "X" is any alphabetic
  4047   4070     ** character, discard the initial "/" from the pathname.
  4048   4071     */
  4049   4072     if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
................................................................................
  4069   4092     }
  4070   4093     zConverted = convertUtf8Filename(zRelative);
  4071   4094     if( zConverted==0 ){
  4072   4095       return SQLITE_IOERR_NOMEM;
  4073   4096     }
  4074   4097     if( isNT() ){
  4075   4098       LPWSTR zTemp;
  4076         -    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
  4077         -    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
         4099  +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
         4100  +    if( nByte==0 ){
         4101  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4102  +                  "GetFullPathNameW1", zConverted);
         4103  +      sqlite3_free(zConverted);
         4104  +      return SQLITE_CANTOPEN_FULLPATH;
         4105  +    }
         4106  +    nByte += 3;
         4107  +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  4078   4108       if( zTemp==0 ){
  4079   4109         sqlite3_free(zConverted);
  4080   4110         return SQLITE_IOERR_NOMEM;
  4081   4111       }
  4082         -    osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
         4112  +    nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
         4113  +    if( nByte==0 ){
         4114  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4115  +                  "GetFullPathNameW2", zConverted);
         4116  +      sqlite3_free(zConverted);
         4117  +      sqlite3_free(zTemp);
         4118  +      return SQLITE_CANTOPEN_FULLPATH;
         4119  +    }
  4083   4120       sqlite3_free(zConverted);
  4084   4121       zOut = unicodeToUtf8(zTemp);
  4085   4122       sqlite3_free(zTemp);
  4086   4123     }
  4087   4124   #ifdef SQLITE_WIN32_HAS_ANSI
  4088   4125     else{
  4089   4126       char *zTemp;
  4090         -    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
  4091         -    zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
         4127  +    nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
         4128  +    if( nByte==0 ){
         4129  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4130  +                  "GetFullPathNameA1", zConverted);
         4131  +      sqlite3_free(zConverted);
         4132  +      return SQLITE_CANTOPEN_FULLPATH;
         4133  +    }
         4134  +    nByte += 3;
         4135  +    zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
  4092   4136       if( zTemp==0 ){
  4093   4137         sqlite3_free(zConverted);
  4094   4138         return SQLITE_IOERR_NOMEM;
  4095   4139       }
  4096         -    osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
         4140  +    nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
         4141  +    if( nByte==0 ){
         4142  +      winLogError(SQLITE_ERROR, osGetLastError(),
         4143  +                  "GetFullPathNameA2", zConverted);
         4144  +      sqlite3_free(zConverted);
         4145  +      sqlite3_free(zTemp);
         4146  +      return SQLITE_CANTOPEN_FULLPATH;
         4147  +    }
  4097   4148       sqlite3_free(zConverted);
  4098   4149       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
  4099   4150       sqlite3_free(zTemp);
  4100   4151     }
  4101   4152   #endif
  4102   4153     if( zOut ){
  4103   4154       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
................................................................................
  4347   4398       winSetSystemCall,    /* xSetSystemCall */
  4348   4399       winGetSystemCall,    /* xGetSystemCall */
  4349   4400       winNextSystemCall,   /* xNextSystemCall */
  4350   4401     };
  4351   4402   
  4352   4403     /* Double-check that the aSyscall[] array has been constructed
  4353   4404     ** correctly.  See ticket [bb3a86e890c8e96ab] */
  4354         -  assert( ArraySize(aSyscall)==73 );
         4405  +  assert( ArraySize(aSyscall)==74 );
  4355   4406   
  4356   4407   #ifndef SQLITE_OMIT_WAL
  4357   4408     /* get memory map allocation granularity */
  4358   4409     memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
  4359   4410   #if SQLITE_OS_WINRT
  4360   4411     osGetNativeSystemInfo(&winSysInfo);
  4361   4412   #else
................................................................................
  4366   4417   
  4367   4418     sqlite3_vfs_register(&winVfs, 1);
  4368   4419     return SQLITE_OK; 
  4369   4420   }
  4370   4421   
  4371   4422   int sqlite3_os_end(void){ 
  4372   4423   #if SQLITE_OS_WINRT
  4373         -  if( sleepObj != NULL ){
         4424  +  if( sleepObj!=NULL ){
  4374   4425       osCloseHandle(sleepObj);
  4375   4426       sleepObj = NULL;
  4376   4427     }
  4377   4428   #endif
  4378   4429     return SQLITE_OK;
  4379   4430   }
  4380   4431   
  4381   4432   #endif /* SQLITE_OS_WIN */

Changes to src/pager.c.

  4444   4444     /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
  4445   4445     if( zPathname ){
  4446   4446       assert( nPathname>0 );
  4447   4447       pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
  4448   4448       memcpy(pPager->zFilename, zPathname, nPathname);
  4449   4449       if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
  4450   4450       memcpy(pPager->zJournal, zPathname, nPathname);
  4451         -    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
         4451  +    memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
  4452   4452       sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
  4453   4453   #ifndef SQLITE_OMIT_WAL
  4454   4454       pPager->zWal = &pPager->zJournal[nPathname+8+1];
  4455   4455       memcpy(pPager->zWal, zPathname, nPathname);
  4456   4456       memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
  4457   4457       sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
  4458   4458   #endif

Changes to src/pragma.c.

   970    970              pCol->zType ? pCol->zType : "", 0);
   971    971           sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
   972    972           if( pCol->zDflt ){
   973    973             sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
   974    974           }else{
   975    975             sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
   976    976           }
   977         -        sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6);
          977  +        sqlite3VdbeAddOp2(v, OP_Integer,
          978  +                            (pCol->colFlags&COLFLAG_PRIMKEY)!=0, 6);
   978    979           sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
   979    980         }
   980    981       }
   981    982     }else
   982    983   
   983    984     if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){
   984    985       Index *pIdx;
................................................................................
  1229   1230         /* Do the b-tree integrity checks */
  1230   1231         sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
  1231   1232         sqlite3VdbeChangeP5(v, (u8)i);
  1232   1233         addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
  1233   1234         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
  1234   1235            sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
  1235   1236            P4_DYNAMIC);
  1236         -      sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1);
         1237  +      sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
  1237   1238         sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
  1238   1239         sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
  1239   1240         sqlite3VdbeJumpHere(v, addr);
  1240   1241   
  1241   1242         /* Make sure all the indices are constructed correctly.
  1242   1243         */
  1243   1244         for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){

Changes to src/prepare.c.

   130    130   ** database.  iDb==1 should never be used.  iDb>=2 is used for
   131    131   ** auxiliary databases.  Return one of the SQLITE_ error codes to
   132    132   ** indicate success or failure.
   133    133   */
   134    134   static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
   135    135     int rc;
   136    136     int i;
          137  +#ifndef SQLITE_OMIT_DEPRECATED
   137    138     int size;
          139  +#endif
   138    140     Table *pTab;
   139    141     Db *pDb;
   140    142     char const *azArg[4];
   141    143     int meta[5];
   142    144     InitData initData;
   143    145     char const *zMasterSchema;
   144    146     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 */
................................................................................
  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;
................................................................................
  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  +        if( pWInfo->nOBSat>0 ){
  4483   4510             sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak);
  4484   4511             VdbeComment((v, "%s() by index",
  4485   4512                   (flag==WHERE_ORDERBY_MIN?"min":"max")));
  4486   4513           }
  4487   4514           sqlite3WhereEnd(pWInfo);
  4488   4515           finalizeAggFunctions(pParse, &sAggInfo);
  4489   4516         }
  4490   4517   
  4491   4518         pOrderBy = 0;
  4492   4519         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
  4493         -      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, 
         4520  +      selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, 0, 
  4494   4521                         pDest, addrEnd, addrEnd);
  4495   4522         sqlite3ExprListDelete(db, pDel);
  4496   4523       }
  4497   4524       sqlite3VdbeResolveLabel(v, addrEnd);
  4498   4525       
  4499   4526     } /* endif aggregate query */
  4500   4527   
  4501         -  if( distinct>=0 ){
         4528  +  if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
  4502   4529       explainTempTable(pParse, "DISTINCT");
  4503   4530     }
  4504   4531   
  4505   4532     /* If there is an ORDER BY clause, then we need to sort the results
  4506   4533     ** and send them to the callback one by one.
  4507   4534     */
  4508   4535     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
................................................................................
  1557   1558   ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
  1558   1559   ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
  1559   1560   ** connection is opened. If it is globally disabled, filenames are
  1560   1561   ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
  1561   1562   ** database connection is opened. By default, URI handling is globally
  1562   1563   ** disabled. The default value may be changed by compiling with the
  1563   1564   ** [SQLITE_USE_URI] symbol defined.
         1565  +**
         1566  +** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
         1567  +** <dd> This option taks a single integer argument which is interpreted as
         1568  +** a boolean in order to enable or disable the use of covering indices for
         1569  +** full table scans in the query optimizer.  The default setting is determined
         1570  +** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
         1571  +** if that compile-time option is omitted.
         1572  +** The ability to disable the use of covering indices for full table scans
         1573  +** is because some incorrectly coded legacy applications might malfunction
         1574  +** malfunction when the optimization is enabled.  Providing the ability to
         1575  +** disable the optimization allows the older, buggy application code to work
         1576  +** without change even with newer versions of SQLite.
  1564   1577   **
  1565   1578   ** [[SQLITE_CONFIG_READONLY]] <dt>SQLITE_CONFIG_READONLY
  1566   1579   ** <dd> This option takes a single argument of type int. If non-zero, then
  1567   1580   ** read-only mode for opening databases is globally enabled. If the parameter
  1568   1581   ** is zero, then read-only mode for opening databases is globally disabled. If
  1569   1582   ** read-only mode for opening databases is globally enabled, all databases
  1570   1583   ** opened by [sqlite3_open()], [sqlite3_open16()], or specified as part of
................................................................................
  1596   1609   #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
  1597   1610   #define SQLITE_CONFIG_PCACHE       14  /* no-op */
  1598   1611   #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
  1599   1612   #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
  1600   1613   #define SQLITE_CONFIG_URI          17  /* int */
  1601   1614   #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
  1602   1615   #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
  1603         -#define SQLITE_CONFIG_READONLY     20  /* int */
         1616  +#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
         1617  +#define SQLITE_CONFIG_READONLY     21  /* int */
  1604   1618   
  1605   1619   /*
  1606   1620   ** CAPI3REF: Database Connection Configuration Options
  1607   1621   **
  1608   1622   ** These constants are the available integer configuration options that
  1609   1623   ** can be passed as the second argument to the [sqlite3_db_config()] interface.
  1610   1624   **
................................................................................
  2756   2770   **
  2757   2771   ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
  2758   2772   ** text that describes the error, as either UTF-8 or UTF-16 respectively.
  2759   2773   ** ^(Memory to hold the error message string is managed internally.
  2760   2774   ** The application does not need to worry about freeing the result.
  2761   2775   ** However, the error string might be overwritten or deallocated by
  2762   2776   ** subsequent calls to other SQLite interface functions.)^
         2777  +**
         2778  +** ^The sqlite3_errstr() interface returns the English-language text
         2779  +** that describes the [result code], as UTF-8.
         2780  +** ^(Memory to hold the error message string is managed internally
         2781  +** and must not be freed by the application)^.
  2763   2782   **
  2764   2783   ** When the serialized [threading mode] is in use, it might be the
  2765   2784   ** case that a second error occurs on a separate thread in between
  2766   2785   ** the time of the first error and the call to these interfaces.
  2767   2786   ** When that happens, the second error will be reported since these
  2768   2787   ** interfaces always report the most recent result.  To avoid
  2769   2788   ** this, each thread can obtain exclusive use of the [database connection] D
................................................................................
  2775   2794   ** was invoked incorrectly by the application.  In that case, the
  2776   2795   ** error code and message may or may not be set.
  2777   2796   */
  2778   2797   int sqlite3_errcode(sqlite3 *db);
  2779   2798   int sqlite3_extended_errcode(sqlite3 *db);
  2780   2799   const char *sqlite3_errmsg(sqlite3*);
  2781   2800   const void *sqlite3_errmsg16(sqlite3*);
         2801  +const char *sqlite3_errstr(int);
  2782   2802   
  2783   2803   /*
  2784   2804   ** CAPI3REF: SQL Statement Object
  2785   2805   ** KEYWORDS: {prepared statement} {prepared statements}
  2786   2806   **
  2787   2807   ** An instance of this object represents a single SQL statement.
  2788   2808   ** This object is variously known as a "prepared statement" or a
................................................................................
  4736   4756   **
  4737   4757   ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
  4738   4758   ** successfully.  An [error code] is returned otherwise.)^
  4739   4759   **
  4740   4760   ** ^Shared cache is disabled by default. But this might change in
  4741   4761   ** future releases of SQLite.  Applications that care about shared
  4742   4762   ** cache setting should set it explicitly.
         4763  +**
         4764  +** This interface is threadsafe on processors where writing a
         4765  +** 32-bit integer is atomic.
  4743   4766   **
  4744   4767   ** See Also:  [SQLite Shared-Cache Mode]
  4745   4768   */
  4746   4769   int sqlite3_enable_shared_cache(int);
  4747   4770   
  4748   4771   /*
  4749   4772   ** CAPI3REF: Attempt To Free Heap Memory

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;
................................................................................
   958    959   #define SQLITE_EnableTrigger  0x40000000  /* True to enable triggers */
   959    960   
   960    961   /*
   961    962   ** Bits of the sqlite3.flags field that are used by the
   962    963   ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface.
   963    964   ** These must be the low-order bits of the flags field.
   964    965   */
   965         -#define SQLITE_QueryFlattener 0x01        /* Disable query flattening */
   966         -#define SQLITE_ColumnCache    0x02        /* Disable the column cache */
   967         -#define SQLITE_IndexSort      0x04        /* Disable indexes for sorting */
   968         -#define SQLITE_IndexSearch    0x08        /* Disable indexes for searching */
   969         -#define SQLITE_IndexCover     0x10        /* Disable index covering table */
   970         -#define SQLITE_GroupByOrder   0x20        /* Disable GROUPBY cover of ORDERBY */
   971         -#define SQLITE_FactorOutConst 0x40        /* Disable factoring out constants */
   972         -#define SQLITE_IdxRealAsInt   0x80        /* Store REAL as INT in indices */
   973         -#define SQLITE_DistinctOpt    0x80        /* DISTINCT using indexes */
   974         -#define SQLITE_OptMask        0xff        /* Mask of all disablable opts */
          966  +#define SQLITE_QueryFlattener 0x01   /* Disable query flattening */
          967  +#define SQLITE_ColumnCache    0x02   /* Disable the column cache */
          968  +#define SQLITE_GroupByOrder   0x04   /* Disable GROUPBY cover of ORDERBY */
          969  +#define SQLITE_FactorOutConst 0x08   /* Disable factoring out constants */
          970  +#define SQLITE_IdxRealAsInt   0x10   /* Store REAL as INT in indices */
          971  +#define SQLITE_DistinctOpt    0x20   /* DISTINCT using indexes */
          972  +#define SQLITE_CoverIdxScan   0x40   /* Disable covering index scans */
          973  +#define SQLITE_OptMask        0xff   /* Mask of all disablable opts */
   975    974   
   976    975   /*
   977    976   ** Possible values for the sqlite.magic field.
   978    977   ** The numbers are obtained at random and have no special meaning, other
   979    978   ** than being distinct from one another.
   980    979   */
   981    980   #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
................................................................................
  1118   1117   */
  1119   1118   struct Column {
  1120   1119     char *zName;     /* Name of this column */
  1121   1120     Expr *pDflt;     /* Default value of this column */
  1122   1121     char *zDflt;     /* Original text of the default value */
  1123   1122     char *zType;     /* Data type for this column */
  1124   1123     char *zColl;     /* Collating sequence.  If NULL, use the default */
  1125         -  u8 notNull;      /* True if there is a NOT NULL constraint */
  1126         -  u8 isPrimKey;    /* True if this column is part of the PRIMARY KEY */
         1124  +  u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
  1127   1125     char affinity;   /* One of the SQLITE_AFF_... values */
  1128         -#ifndef SQLITE_OMIT_VIRTUALTABLE
  1129         -  u8 isHidden;     /* True if this column is 'hidden' */
  1130         -#endif
         1126  +  u16 colFlags;    /* Boolean properties.  See COLFLAG_ defines below */
  1131   1127   };
  1132   1128   
         1129  +/* Allowed values for Column.colFlags:
         1130  +*/
         1131  +#define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
         1132  +#define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
         1133  +
  1133   1134   /*
  1134   1135   ** A "Collating Sequence" is defined by an instance of the following
  1135   1136   ** structure. Conceptually, a collating sequence consists of a name and
  1136   1137   ** a comparison routine that defines the order of that sequence.
  1137   1138   **
  1138   1139   ** There may two separate implementations of the collation function, one
  1139   1140   ** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that
................................................................................
  1281   1282   ** refers VDBE cursor number that holds the table open, not to the root
  1282   1283   ** page number.  Transient tables are used to hold the results of a
  1283   1284   ** sub-query that appears instead of a real table name in the FROM clause 
  1284   1285   ** of a SELECT statement.
  1285   1286   */
  1286   1287   struct Table {
  1287   1288     char *zName;         /* Name of the table or view */
  1288         -  int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
  1289         -  int nCol;            /* Number of columns in this table */
  1290   1289     Column *aCol;        /* Information about each column */
  1291   1290     Index *pIndex;       /* List of SQL indexes on this table. */
  1292         -  int tnum;            /* Root BTree node for this table (see note above) */
  1293         -  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
  1294   1291     Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
  1295         -  u16 nRef;            /* Number of pointers to this Table */
  1296         -  u8 tabFlags;         /* Mask of TF_* values */
  1297         -  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1298   1292     FKey *pFKey;         /* Linked list of all foreign keys in this table */
  1299   1293     char *zColAff;       /* String defining the affinity of each column */
  1300   1294   #ifndef SQLITE_OMIT_CHECK
  1301   1295     ExprList *pCheck;    /* All CHECK constraints */
  1302   1296   #endif
         1297  +  tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
         1298  +  int tnum;            /* Root BTree node for this table (see note above) */
         1299  +  i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
         1300  +  i16 nCol;            /* Number of columns in this table */
         1301  +  u16 nRef;            /* Number of pointers to this Table */
         1302  +  u8 tabFlags;         /* Mask of TF_* values */
         1303  +  u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
  1303   1304   #ifndef SQLITE_OMIT_ALTERTABLE
  1304   1305     int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
  1305   1306   #endif
  1306   1307   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1307         -  VTable *pVTable;     /* List of VTable objects. */
  1308   1308     int nModuleArg;      /* Number of arguments to the module */
  1309   1309     char **azModuleArg;  /* Text of all module args. [0] is module name */
         1310  +  VTable *pVTable;     /* List of VTable objects. */
  1310   1311   #endif
  1311   1312     Trigger *pTrigger;   /* List of triggers stored in pSchema */
  1312   1313     Schema *pSchema;     /* Schema that contains this table */
  1313   1314     Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
  1314   1315   };
  1315   1316   
  1316   1317   /*
................................................................................
  1326   1327   /*
  1327   1328   ** Test to see whether or not a table is a virtual table.  This is
  1328   1329   ** done as a macro so that it will be optimized out when virtual
  1329   1330   ** table support is omitted from the build.
  1330   1331   */
  1331   1332   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1332   1333   #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
  1333         -#  define IsHiddenColumn(X) ((X)->isHidden)
         1334  +#  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
  1334   1335   #else
  1335   1336   #  define IsVirtual(X)      0
  1336   1337   #  define IsHiddenColumn(X) 0
  1337   1338   #endif
  1338   1339   
  1339   1340   /*
  1340   1341   ** Each foreign key constraint is an instance of the following structure.
................................................................................
  1678   1679     CollSeq *pColl;        /* The collation type of the column or 0 */
  1679   1680   
  1680   1681     /* If the EP_Reduced flag is set in the Expr.flags mask, then no
  1681   1682     ** space is allocated for the fields below this point. An attempt to
  1682   1683     ** access them will result in a segfault or malfunction.
  1683   1684     *********************************************************************/
  1684   1685   
         1686  +#if SQLITE_MAX_EXPR_DEPTH>0
         1687  +  int nHeight;           /* Height of the tree headed by this node */
         1688  +#endif
  1685   1689     int iTable;            /* TK_COLUMN: cursor number of table holding column
  1686   1690                            ** TK_REGISTER: register number
  1687   1691                            ** TK_TRIGGER: 1 -> new, 0 -> old */
  1688   1692     ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
  1689   1693                            ** TK_VARIABLE: variable number (always >= 1). */
  1690   1694     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  1691   1695     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
  1692   1696     u8 flags2;             /* Second set of flags.  EP2_... */
  1693   1697     u8 op2;                /* TK_REGISTER: original value of Expr.op
  1694   1698                            ** TK_COLUMN: the value of p5 for OP_Column
  1695   1699                            ** TK_AGG_FUNCTION: nesting depth */
  1696   1700     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  1697   1701     Table *pTab;           /* Table for TK_COLUMN expressions. */
  1698         -#if SQLITE_MAX_EXPR_DEPTH>0
  1699         -  int nHeight;           /* Height of the tree headed by this node */
  1700         -#endif
  1701   1702   };
  1702   1703   
  1703   1704   /*
  1704   1705   ** The following are the meanings of bits in the Expr.flags field.
  1705   1706   */
  1706   1707   #define EP_FromJoin   0x0001  /* Originated in ON or USING clause of a join */
  1707   1708   #define EP_Agg        0x0002  /* Contains one or more aggregate functions */
................................................................................
  1977   1978   ** The WHERE clause processing routine has two halves.  The
  1978   1979   ** first part does the start of the WHERE loop and the second
  1979   1980   ** half does the tail of the WHERE loop.  An instance of
  1980   1981   ** this structure is returned by the first half and passed
  1981   1982   ** into the second half to give some continuity.
  1982   1983   */
  1983   1984   struct WhereInfo {
  1984         -  Parse *pParse;       /* Parsing and code generating context */
  1985         -  u16 wctrlFlags;      /* Flags originally passed to sqlite3WhereBegin() */
  1986         -  u8 okOnePass;        /* Ok to use one-pass algorithm for UPDATE or DELETE */
  1987         -  u8 untestedTerms;    /* Not all WHERE terms resolved by outer loop */
  1988         -  u8 eDistinct;
  1989         -  SrcList *pTabList;             /* List of tables in the join */
  1990         -  int iTop;                      /* The very beginning of the WHERE loop */
  1991         -  int iContinue;                 /* Jump here to continue with next record */
  1992         -  int iBreak;                    /* Jump here to break out of the loop */
  1993         -  int nLevel;                    /* Number of nested loop */
  1994         -  struct WhereClause *pWC;       /* Decomposition of the WHERE clause */
  1995         -  double savedNQueryLoop;        /* pParse->nQueryLoop outside the WHERE loop */
  1996         -  double nRowOut;                /* Estimated number of output rows */
  1997         -  WhereLevel a[1];               /* Information about each nest loop in WHERE */
         1985  +  Parse *pParse;            /* Parsing and code generating context */
         1986  +  SrcList *pTabList;        /* List of tables in the join */
         1987  +  u16 nOBSat;               /* Number of ORDER BY terms satisfied by indices */
         1988  +  u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
         1989  +  u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
         1990  +  u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
         1991  +  u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
         1992  +  int iTop;                 /* The very beginning of the WHERE loop */
         1993  +  int iContinue;            /* Jump here to continue with next record */
         1994  +  int iBreak;               /* Jump here to break out of the loop */
         1995  +  int nLevel;               /* Number of nested loop */
         1996  +  struct WhereClause *pWC;  /* Decomposition of the WHERE clause */
         1997  +  double savedNQueryLoop;   /* pParse->nQueryLoop outside the WHERE loop */
         1998  +  double nRowOut;           /* Estimated number of output rows */
         1999  +  WhereLevel a[1];          /* Information about each nest loop in WHERE */
  1998   2000   };
  1999   2001   
  2000         -#define WHERE_DISTINCT_UNIQUE 1
  2001         -#define WHERE_DISTINCT_ORDERED 2
         2002  +/* Allowed values for WhereInfo.eDistinct and DistinctCtx.eTnctType */
         2003  +#define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
         2004  +#define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
         2005  +#define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
         2006  +#define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
  2002   2007   
  2003   2008   /*
  2004   2009   ** A NameContext defines a context in which to resolve table and column
  2005   2010   ** names.  The context consists of a list of tables (the pSrcList) field and
  2006   2011   ** a list of named expression (pEList).  The named expression list may
  2007   2012   ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
  2008   2013   ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
................................................................................
  2053   2058   ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
  2054   2059   ** These addresses must be stored so that we can go back and fill in
  2055   2060   ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
  2056   2061   ** the number of columns in P2 can be computed at the same time
  2057   2062   ** as the OP_OpenEphm instruction is coded because not
  2058   2063   ** enough information about the compound query is known at that point.
  2059   2064   ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
  2060         -** for the result set.  The KeyInfo for addrOpenTran[2] contains collating
         2065  +** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
  2061   2066   ** sequences for the ORDER BY clause.
  2062   2067   */
  2063   2068   struct Select {
  2064   2069     ExprList *pEList;      /* The fields of the result */
  2065   2070     u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
  2066         -  char affinity;         /* MakeRecord with this affinity for SRT_Set */
  2067   2071     u16 selFlags;          /* Various SF_* values */
  2068   2072     int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
  2069   2073     int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
  2070   2074     double nSelectRow;     /* Estimated number of result rows */
  2071   2075     SrcList *pSrc;         /* The FROM clause */
  2072   2076     Expr *pWhere;          /* The WHERE clause */
  2073   2077     ExprList *pGroupBy;    /* The GROUP BY clause */
................................................................................
  2110   2114   #define SRT_Mem          6  /* Store result in a memory cell */
  2111   2115   #define SRT_Set          7  /* Store results as keys in an index */
  2112   2116   #define SRT_Table        8  /* Store result as data with an automatic rowid */
  2113   2117   #define SRT_EphemTab     9  /* Create transient tab and store like SRT_Table */
  2114   2118   #define SRT_Coroutine   10  /* Generate a single row of result */
  2115   2119   
  2116   2120   /*
  2117         -** A structure used to customize the behavior of sqlite3Select(). See
  2118         -** comments above sqlite3Select() for details.
         2121  +** An instance of this object describes where to put of the results of
         2122  +** a SELECT statement.
  2119   2123   */
  2120         -typedef struct SelectDest SelectDest;
  2121   2124   struct SelectDest {
  2122         -  u8 eDest;         /* How to dispose of the results */
  2123         -  u8 affSdst;       /* Affinity used when eDest==SRT_Set */
         2125  +  u8 eDest;         /* How to dispose of the results.  On of SRT_* above. */
         2126  +  char affSdst;     /* Affinity used when eDest==SRT_Set */
  2124   2127     int iSDParm;      /* A parameter used by the eDest disposal method */
  2125   2128     int iSdst;        /* Base register where results are written */
  2126   2129     int nSdst;        /* Number of registers allocated */
  2127   2130   };
  2128   2131   
  2129   2132   /*
  2130   2133   ** During code generation of statements that do inserts into AUTOINCREMENT 
................................................................................
  2458   2461   ** This structure also contains some state information.
  2459   2462   */
  2460   2463   struct Sqlite3Config {
  2461   2464     int bMemstat;                     /* True to enable memory status */
  2462   2465     int bCoreMutex;                   /* True to enable core mutexing */
  2463   2466     int bFullMutex;                   /* True to enable full mutexing */
  2464   2467     int bOpenUri;                     /* True to interpret filenames as URIs */
         2468  +  int bUseCis;                      /* Use covering indices for full-scans */
  2465   2469     int bReadOnly;                    /* True to force read-only mode */
  2466   2470     int mxStrlen;                     /* Maximum string length */
  2467   2471     int szLookaside;                  /* Default lookaside buffer size */
  2468   2472     int nLookaside;                   /* Default lookaside buffer count */
  2469   2473     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  2470   2474     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
  2471   2475     sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
................................................................................
  2775   2779   #ifndef SQLITE_OMIT_AUTOINCREMENT
  2776   2780     void sqlite3AutoincrementBegin(Parse *pParse);
  2777   2781     void sqlite3AutoincrementEnd(Parse *pParse);
  2778   2782   #else
  2779   2783   # define sqlite3AutoincrementBegin(X)
  2780   2784   # define sqlite3AutoincrementEnd(X)
  2781   2785   #endif
         2786  +int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
  2782   2787   void sqlite3Insert(Parse*, SrcList*, ExprList*, Select*, IdList*, int);
  2783   2788   void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
  2784   2789   IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
  2785   2790   int sqlite3IdListIndex(IdList*,const char*);
  2786   2791   SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
  2787   2792   SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
  2788   2793   SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
................................................................................
  2804   2809   int sqlite3IsReadOnly(Parse*, Table*, int);
  2805   2810   void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
  2806   2811   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
  2807   2812   Expr *sqlite3LimitWhere(Parse *, SrcList *, Expr *, ExprList *, Expr *, Expr *, char *);
  2808   2813   #endif
  2809   2814   void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
  2810   2815   void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
  2811         -WhereInfo *sqlite3WhereBegin(
  2812         -    Parse*,SrcList*,Expr*,ExprList**,ExprList*,u16,int);
         2816  +WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
  2813   2817   void sqlite3WhereEnd(WhereInfo*);
  2814   2818   int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
  2815   2819   void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
  2816   2820   void sqlite3ExprCodeMove(Parse*, int, int, int);
  2817         -void sqlite3ExprCodeCopy(Parse*, int, int, int);
  2818   2821   void sqlite3ExprCacheStore(Parse*, int, int, int);
  2819   2822   void sqlite3ExprCachePush(Parse*);
  2820   2823   void sqlite3ExprCachePop(Parse*, int);
  2821   2824   void sqlite3ExprCacheRemove(Parse*, int, int);
  2822   2825   void sqlite3ExprCacheClear(Parse*);
  2823   2826   void sqlite3ExprCacheAffinityChange(Parse*, int, int);
  2824   2827   int sqlite3ExprCode(Parse*, Expr*, int);
................................................................................
  2950   2953   int sqlite3FixExprList(DbFixer*, ExprList*);
  2951   2954   int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
  2952   2955   int sqlite3AtoF(const char *z, double*, int, u8);
  2953   2956   int sqlite3GetInt32(const char *, int*);
  2954   2957   int sqlite3Atoi(const char*);
  2955   2958   int sqlite3Utf16ByteLen(const void *pData, int nChar);
  2956   2959   int sqlite3Utf8CharLen(const char *pData, int nByte);
  2957         -u32 sqlite3Utf8Read(const u8*, const u8**);
         2960  +u32 sqlite3Utf8Read(const u8**);
  2958   2961   
  2959   2962   /*
  2960   2963   ** Routines to read and write variable-length integers.  These used to
  2961   2964   ** be defined locally, but now we use the varint routines in the util.c
  2962   2965   ** file.  Code should use the MACRO forms below, as the Varint32 versions
  2963   2966   ** are coded to assume the single byte case is already handled (which 
  2964   2967   ** the MACRO form does).

Changes to src/tclsqlite.c.

    37     37   # include <stdlib.h>
    38     38   # include <string.h>
    39     39   # include <assert.h>
    40     40     typedef unsigned char u8;
    41     41   #endif
    42     42   #include <ctype.h>
    43     43   
    44         -/*
    45         -** This function is used to translate a return code into an error
    46         -** message.
    47         -*/
    48         -#ifndef USE_SYSTEM_SQLITE
    49         -const char *sqlite3ErrStr(int rc);
    50         -#endif
    51         -
    52     44   /*
    53     45    * Windows needs to know which symbols to export.  Unix does not.
    54     46    * BUILD_sqlite should be undefined for Unix.
    55     47    */
    56     48   #ifdef BUILD_sqlite
    57     49   #undef TCL_STORAGE_CLASS
    58     50   #define TCL_STORAGE_CLASS DLLEXPORT
................................................................................
  2562   2554         Tcl_WrongNumArgs(interp, 2, objv, "KEY");
  2563   2555         return TCL_ERROR;
  2564   2556       }
  2565   2557   #ifdef SQLITE_HAS_CODEC
  2566   2558       pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey);
  2567   2559       rc = sqlite3_rekey(pDb->db, pKey, nKey);
  2568   2560       if( rc ){
  2569         -      Tcl_AppendResult(interp, sqlite3ErrStr(rc), 0);
         2561  +      Tcl_AppendResult(interp, sqlite3_errstr(rc), 0);
  2570   2562         rc = TCL_ERROR;
  2571   2563       }
  2572   2564   #endif
  2573   2565       break;
  2574   2566     }
  2575   2567   
  2576   2568     /*    $db restore ?DATABASE? FILENAME
................................................................................
  3040   3032       return TCL_ERROR;
  3041   3033     }
  3042   3034     memset(p, 0, sizeof(*p));
  3043   3035     zFile = Tcl_GetStringFromObj(objv[2], 0);
  3044   3036     zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
  3045   3037     rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs);
  3046   3038     Tcl_DStringFree(&translatedFilename);
  3047         -#ifndef USE_SYSTEM_SQLITE
  3048   3039     if( p->db ){
  3049         -#endif
  3050   3040       if( SQLITE_OK!=sqlite3_errcode(p->db) ){
  3051   3041         zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
  3052   3042         sqlite3_close(p->db);
  3053   3043         p->db = 0;
  3054   3044       }
  3055         -#ifndef USE_SYSTEM_SQLITE
  3056   3045     }else{
  3057         -    zErrMsg = sqlite3_mprintf("%s", sqlite3ErrStr(rc));
         3046  +    zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc));
  3058   3047     }
  3059         -#endif
  3060   3048   #ifdef SQLITE_HAS_CODEC
  3061   3049     if( p->db ){
  3062   3050       sqlite3_key(p->db, pKey, nKey);
  3063   3051     }
  3064   3052   #endif
  3065   3053     if( p->db==0 ){
  3066   3054       Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);

Changes to src/test1.c.

  5932   5932     static const struct {
  5933   5933       const char *zOptName;
  5934   5934       int mask;
  5935   5935     } aOpt[] = {
  5936   5936       { "all",              SQLITE_OptMask        },
  5937   5937       { "query-flattener",  SQLITE_QueryFlattener },
  5938   5938       { "column-cache",     SQLITE_ColumnCache    },
  5939         -    { "index-sort",       SQLITE_IndexSort      },
  5940         -    { "index-search",     SQLITE_IndexSearch    },
  5941         -    { "index-cover",      SQLITE_IndexCover     },
  5942   5939       { "groupby-order",    SQLITE_GroupByOrder   },
  5943   5940       { "factor-constants", SQLITE_FactorOutConst },
  5944   5941       { "real-as-int",      SQLITE_IdxRealAsInt   },
         5942  +    { "distinct-opt",     SQLITE_DistinctOpt    },
         5943  +    { "cover-idx-scan",   SQLITE_CoverIdxScan   },
  5945   5944     };
  5946   5945   
  5947   5946     if( objc!=4 ){
  5948   5947       Tcl_WrongNumArgs(interp, 1, objv, "DB OPT BOOLEAN");
  5949   5948       return TCL_ERROR;
  5950   5949     }
  5951   5950     if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;

Changes to src/test_malloc.c.

  1192   1192     }
  1193   1193   
  1194   1194     rc = sqlite3_config(SQLITE_CONFIG_URI, bOpenUri);
  1195   1195     Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_VOLATILE);
  1196   1196   
  1197   1197     return TCL_OK;
  1198   1198   }
         1199  +
         1200  +/*
         1201  +** Usage:    sqlite3_config_cis  BOOLEAN
         1202  +**
         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  +}
  1199   1228   
  1200   1229   /*
  1201   1230   ** Usage:    sqlite3_config_readonly  BOOLEAN
  1202   1231   **
  1203   1232   ** Enables or disables global read-only mode using SQLITE_CONFIG_READONLY.
  1204   1233   */
  1205   1234   static int test_config_readonly(
................................................................................
  1471   1500        { "sqlite3_db_status",          test_db_status                ,0 },
  1472   1501        { "install_malloc_faultsim",    test_install_malloc_faultsim  ,0 },
  1473   1502        { "sqlite3_config_heap",        test_config_heap              ,0 },
  1474   1503        { "sqlite3_config_memstatus",   test_config_memstatus         ,0 },
  1475   1504        { "sqlite3_config_lookaside",   test_config_lookaside         ,0 },
  1476   1505        { "sqlite3_config_error",       test_config_error             ,0 },
  1477   1506        { "sqlite3_config_uri",         test_config_uri               ,0 },
         1507  +     { "sqlite3_config_cis",         test_config_cis               ,0 },
  1478   1508        { "sqlite3_config_readonly",    test_config_readonly          ,0 },
  1479   1509        { "sqlite3_db_config_lookaside",test_db_config_lookaside      ,0 },
  1480   1510        { "sqlite3_dump_memsys3",       test_dump_memsys3             ,3 },
  1481   1511        { "sqlite3_dump_memsys5",       test_dump_memsys3             ,5 },
  1482   1512        { "sqlite3_install_memsys3",    test_install_memsys3          ,0 },
  1483   1513        { "sqlite3_memdebug_vfs_oom_test", test_vfs_oom_test          ,0 },
  1484   1514     };

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

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 */

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;
................................................................................
  2579   2579       return 0;
  2580   2580     }
  2581   2581     if( flags&MEM_Int ){
  2582   2582       /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  2583   2583   #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
  2584   2584       i64 i = pMem->u.i;
  2585   2585       u64 u;
  2586         -    if( file_format>=4 && (i&1)==i ){
  2587         -      return 8+(u32)i;
  2588         -    }
  2589   2586       if( i<0 ){
  2590   2587         if( i<(-MAX_6BYTE) ) return 6;
  2591   2588         /* Previous test prevents:  u = -(-9223372036854775808) */
  2592   2589         u = -i;
  2593   2590       }else{
  2594   2591         u = i;
  2595   2592       }
  2596         -    if( u<=127 ) return 1;
         2593  +    if( u<=127 ){
         2594  +      return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
         2595  +    }
  2597   2596       if( u<=32767 ) return 2;
  2598   2597       if( u<=8388607 ) return 3;
  2599   2598       if( u<=2147483647 ) return 4;
  2600   2599       if( u<=MAX_6BYTE ) return 5;
  2601   2600       return 6;
  2602   2601     }
  2603   2602     if( flags&MEM_Real ){
................................................................................
  2874   2873       if( !p ) return 0;
  2875   2874     }else{
  2876   2875       p = (UnpackedRecord*)&pSpace[nOff];
  2877   2876       *ppFree = 0;
  2878   2877     }
  2879   2878   
  2880   2879     p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
         2880  +  assert( pKeyInfo->aSortOrder!=0 );
  2881   2881     p->pKeyInfo = pKeyInfo;
  2882   2882     p->nField = pKeyInfo->nField + 1;
  2883   2883     return p;
  2884   2884   }
  2885   2885   
  2886   2886   /*
  2887   2887   ** Given the nKey-byte encoding of a record in pKey[], populate the 
................................................................................
  2967   2967     ** to ignore the compiler warnings and leave this variable uninitialized.
  2968   2968     */
  2969   2969     /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
  2970   2970     
  2971   2971     idx1 = getVarint32(aKey1, szHdr1);
  2972   2972     d1 = szHdr1;
  2973   2973     nField = pKeyInfo->nField;
         2974  +  assert( pKeyInfo->aSortOrder!=0 );
  2974   2975     while( idx1<szHdr1 && i<pPKey2->nField ){
  2975   2976       u32 serial_type1;
  2976   2977   
  2977   2978       /* Read the serial types for the next element in each key. */
  2978   2979       idx1 += getVarint32( aKey1+idx1, serial_type1 );
  2979   2980       if( d1>=nKey1 && sqlite3VdbeSerialTypeLen(serial_type1)>0 ) break;
  2980   2981   
................................................................................
  2986   2987       */
  2987   2988       rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i],
  2988   2989                              i<nField ? pKeyInfo->aColl[i] : 0);
  2989   2990       if( rc!=0 ){
  2990   2991         assert( mem1.zMalloc==0 );  /* See comment below */
  2991   2992   
  2992   2993         /* Invert the result if we are using DESC sort order. */
  2993         -      if( pKeyInfo->aSortOrder && i<nField && pKeyInfo->aSortOrder[i] ){
         2994  +      if( i<nField && pKeyInfo->aSortOrder[i] ){
  2994   2995           rc = -rc;
  2995   2996         }
  2996   2997       
  2997   2998         /* If the PREFIX_SEARCH flag is set and all fields except the final
  2998   2999         ** rowid field were equal, then clear the PREFIX_SEARCH flag and set 
  2999   3000         ** pPKey2->rowid to the value of the rowid field in (pKey1, nKey1).
  3000   3001         ** 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 */
   267    269   
   268    270   /*
   269    271   ** Initialize a preallocated WhereClause structure.
   270    272   */
   271    273   static void whereClauseInit(
   272    274     WhereClause *pWC,        /* The WhereClause to be initialized */
   273    275     Parse *pParse,           /* The parsing context */
................................................................................
  1807   1809   #endif
  1808   1810   
  1809   1811   /* 
  1810   1812   ** Required because bestIndex() is called by bestOrClauseIndex() 
  1811   1813   */
  1812   1814   static void bestIndex(
  1813   1815       Parse*, WhereClause*, struct SrcList_item*,
  1814         -    Bitmask, Bitmask, ExprList*, WhereCost*);
         1816  +    Bitmask, Bitmask, WhereCost*);
  1815   1817   
  1816   1818   /*
  1817   1819   ** This routine attempts to find an scanning strategy that can be used 
  1818   1820   ** to optimize an 'OR' expression that is part of a WHERE clause. 
  1819   1821   **
  1820   1822   ** The table associated with FROM clause term pSrc may be either a
  1821   1823   ** regular B-Tree table or a virtual table.
................................................................................
  1861   1863         for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
  1862   1864           WhereCost sTermCost;
  1863   1865           WHERETRACE(("... Multi-index OR testing for term %d of %d....\n", 
  1864   1866             (pOrTerm - pOrWC->a), (pTerm - pWC->a)
  1865   1867           ));
  1866   1868           if( pOrTerm->eOperator==WO_AND ){
  1867   1869             WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc;
  1868         -          bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
         1870  +          bestIndex(pParse, pAndWC, pSrc, notReady, notValid, &sTermCost);
  1869   1871           }else if( pOrTerm->leftCursor==iCur ){
  1870   1872             WhereClause tempWC;
  1871   1873             tempWC.pParse = pWC->pParse;
  1872   1874             tempWC.pMaskSet = pWC->pMaskSet;
  1873   1875             tempWC.pOuter = pWC;
  1874   1876             tempWC.op = TK_AND;
  1875   1877             tempWC.a = pOrTerm;
  1876   1878             tempWC.wctrlFlags = 0;
  1877   1879             tempWC.nTerm = 1;
  1878         -          bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
         1880  +          bestIndex(pParse, &tempWC, pSrc, notReady, notValid, &sTermCost);
  1879   1881           }else{
  1880   1882             continue;
  1881   1883           }
  1882   1884           rTotal += sTermCost.rCost;
  1883   1885           nRow += sTermCost.plan.nRow;
  1884   1886           used |= sTermCost.used;
  1885   1887           if( rTotal>=pCost->rCost ) break;
................................................................................
  3129   3131       }
  3130   3132   
  3131   3133       /* If currently calculating the cost of using an index (not the IPK
  3132   3134       ** index), determine if all required column data may be obtained without 
  3133   3135       ** using the main table (i.e. if the index is a covering
  3134   3136       ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
  3135   3137       ** wsFlags. Otherwise, set the bLookup variable to true.  */
  3136         -    if( pIdx && wsFlags ){
         3138  +    if( pIdx ){
  3137   3139         Bitmask m = pSrc->colUsed;
  3138   3140         int j;
  3139   3141         for(j=0; j<pIdx->nColumn; j++){
  3140   3142           int x = pIdx->aiColumn[j];
  3141   3143           if( x<BMS-1 ){
  3142   3144             m &= ~(((Bitmask)1)<<x);
  3143   3145           }
................................................................................
  3194   3196       ** on one page and hence more pages have to be fetched.
  3195   3197       **
  3196   3198       ** The ANALYZE command and the sqlite_stat1 and sqlite_stat3 tables do
  3197   3199       ** not give us data on the relative sizes of table and index records.
  3198   3200       ** So this computation assumes table records are about twice as big
  3199   3201       ** as index records
  3200   3202       */
  3201         -    if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
         3203  +    if( wsFlags==WHERE_IDX_ONLY
         3204  +     && (pWC->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
         3205  +     && sqlite3GlobalConfig.bUseCis
         3206  +#ifndef SQLITE_OMIT_BUILTIN_TEST
         3207  +     && (pParse->db->flags & SQLITE_CoverIdxScan)==0
         3208  +#endif
         3209  +    ){
         3210  +      /* This index is not useful for indexing, but it is a covering index.
         3211  +      ** A full-scan of the index might be a little faster than a full-scan
         3212  +      ** of the table, so give this case a cost slightly less than a table
         3213  +      ** scan. */
         3214  +      cost = aiRowEst[0]*3 + pProbe->nColumn;
         3215  +      wsFlags |= WHERE_COVER_SCAN|WHERE_COLUMN_RANGE;
         3216  +    }else if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){
  3202   3217         /* The cost of a full table scan is a number of move operations equal
  3203   3218         ** to the number of rows in the table.
  3204   3219         **
  3205   3220         ** We add an additional 4x penalty to full table scans.  This causes
  3206   3221         ** the cost function to err on the side of choosing an index over
  3207   3222         ** choosing a full scan.  This 4x full-scan penalty is an arguable
  3208   3223         ** decision and one which we expect to revisit in the future.  But
  3209   3224         ** it seems to be working well enough at the moment.
  3210   3225         */
  3211   3226         cost = aiRowEst[0]*4;
         3227  +      wsFlags &= ~WHERE_IDX_ONLY;
  3212   3228       }else{
  3213   3229         log10N = estLog(aiRowEst[0]);
  3214   3230         cost = nRow;
  3215   3231         if( pIdx ){
  3216   3232           if( bLookup ){
  3217   3233             /* For an index lookup followed by a table lookup:
  3218   3234             **    nInMul index searches to find the start of each index range
................................................................................
  3368   3384   }
  3369   3385   
  3370   3386   /*
  3371   3387   ** Find the query plan for accessing table pSrc->pTab. Write the
  3372   3388   ** best query plan and its cost into the WhereCost object supplied 
  3373   3389   ** as the last parameter. This function may calculate the cost of
  3374   3390   ** both real and virtual table scans.
         3391  +**
         3392  +** This function does not take ORDER BY or DISTINCT into account.  Nor
         3393  +** does it remember the virtual table query plan.  All it does is compute
         3394  +** the cost while determining if an OR optimization is applicable.  The
         3395  +** details will be reconsidered later if the optimization is found to be
         3396  +** applicable.
  3375   3397   */
  3376   3398   static void bestIndex(
  3377   3399     Parse *pParse,              /* The parsing context */
  3378   3400     WhereClause *pWC,           /* The WHERE clause */
  3379   3401     struct SrcList_item *pSrc,  /* The FROM clause term to search */
  3380   3402     Bitmask notReady,           /* Mask of cursors not available for indexing */
  3381   3403     Bitmask notValid,           /* Cursors not available for any purpose */
  3382         -  ExprList *pOrderBy,         /* The ORDER BY clause */
  3383   3404     WhereCost *pCost            /* Lowest cost query plan */
  3384   3405   ){
  3385   3406   #ifndef SQLITE_OMIT_VIRTUALTABLE
  3386   3407     if( IsVirtual(pSrc->pTab) ){
  3387   3408       sqlite3_index_info *p = 0;
  3388         -    bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p);
         3409  +    bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, 0, pCost, &p);
  3389   3410       if( p->needToFreeIdxStr ){
  3390   3411         sqlite3_free(p->idxStr);
  3391   3412       }
  3392   3413       sqlite3DbFree(pParse->db, p);
  3393   3414     }else
  3394   3415   #endif
  3395   3416     {
  3396         -    bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, 0, pCost);
         3417  +    bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, 0, 0, pCost);
  3397   3418     }
  3398   3419   }
  3399   3420   
  3400   3421   /*
  3401   3422   ** Disable a term in the WHERE clause.  Except, do not disable the term
  3402   3423   ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  3403   3424   ** or USING clause of that join.
................................................................................
  4248   4269         pLevel->op = OP_Noop;
  4249   4270       }else if( bRev ){
  4250   4271         pLevel->op = OP_Prev;
  4251   4272       }else{
  4252   4273         pLevel->op = OP_Next;
  4253   4274       }
  4254   4275       pLevel->p1 = iIdxCur;
         4276  +    if( pLevel->plan.wsFlags & WHERE_COVER_SCAN ){
         4277  +      pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
         4278  +    }else{
         4279  +      assert( pLevel->p5==0 );
         4280  +    }
  4255   4281     }else
  4256   4282   
  4257   4283   #ifndef SQLITE_OMIT_OR_OPTIMIZATION
  4258   4284     if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){
  4259   4285       /* Case 4:  Two or more separately indexed terms connected by OR
  4260   4286       **
  4261   4287       ** Example:
................................................................................
  4642   4668   **        move the row2 cursor to a null row
  4643   4669   **        goto start
  4644   4670   **      fi
  4645   4671   **    end
  4646   4672   **
  4647   4673   ** ORDER BY CLAUSE PROCESSING
  4648   4674   **
  4649         -** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
         4675  +** pOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
  4650   4676   ** 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.
         4677  +** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
  4652   4678   **
  4653   4679   ** If an index can be used so that the natural output order of the table
  4654   4680   ** 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.
         4681  +** the returned WhereInfo.nOBSat field is set to pOrderBy->nExpr.  This
         4682  +** is an optimization that prevents an unnecessary sort of the result set
         4683  +** if an index appropriate for the ORDER BY clause already exists.
  4658   4684   **
  4659   4685   ** If the where clause loops cannot be arranged to provide the correct
  4660         -** output order, then the *ppOrderBy is unchanged.
         4686  +** output order, then WhereInfo.nOBSat is 0.
  4661   4687   */
  4662   4688   WhereInfo *sqlite3WhereBegin(
  4663   4689     Parse *pParse,        /* The parser context */
  4664   4690     SrcList *pTabList,    /* A list of all tables to be scanned */
  4665   4691     Expr *pWhere,         /* The WHERE clause */
  4666         -  ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */
         4692  +  ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
  4667   4693     ExprList *pDistinct,  /* The select-list for DISTINCT queries - or NULL */
  4668   4694     u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
  4669   4695     int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
  4670   4696   ){
  4671   4697     int i;                     /* Loop counter */
  4672   4698     int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
  4673   4699     int nTabList;              /* Number of elements in pTabList */
  4674   4700     WhereInfo *pWInfo;         /* Will become the return value of this function */
  4675   4701     Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
  4676   4702     Bitmask notReady;          /* Cursors that are not yet positioned */
  4677   4703     WhereMaskSet *pMaskSet;    /* The expression mask set */
  4678   4704     WhereClause *pWC;               /* Decomposition of the WHERE clause */
  4679   4705     struct SrcList_item *pTabItem;  /* A single entry from pTabList */
  4680         -  WhereLevel *pLevel;             /* A single level in the pWInfo list */
         4706  +  WhereLevel *pLevel;             /* A single level in pWInfo->a[] */
  4681   4707     int iFrom;                      /* First unused FROM clause element */
  4682   4708     int andFlags;              /* AND-ed combination of all pWC->a[].wtFlags */
  4683   4709     sqlite3 *db;               /* Database connection */
  4684   4710   
  4685   4711     /* The number of tables in the FROM clause is limited by the number of
  4686   4712     ** bits in a Bitmask 
  4687   4713     */
................................................................................
  4884   4910       nUnconstrained = 0;
  4885   4911       notIndexed = 0;
  4886   4912       for(isOptimal=(iFrom<nTabList-1); isOptimal>=0 && bestJ<0; isOptimal--){
  4887   4913         Bitmask mask;             /* Mask of tables not yet ready */
  4888   4914         for(j=iFrom, pTabItem=&pTabList->a[j]; j<nTabList; j++, pTabItem++){
  4889   4915           int doNotReorder;    /* True if this table should not be reordered */
  4890   4916           WhereCost sCost;     /* Cost information from best[Virtual]Index() */
  4891         -        ExprList *pOrderBy;  /* ORDER BY clause for index to optimize */
         4917  +        ExprList *pOB;       /* ORDER BY clause for index to optimize */
  4892   4918           ExprList *pDist;     /* DISTINCT clause for index to optimize */
  4893   4919     
  4894   4920           doNotReorder =  (pTabItem->jointype & (JT_LEFT|JT_CROSS))!=0;
  4895   4921           if( j!=iFrom && doNotReorder ) break;
  4896   4922           m = getMask(pMaskSet, pTabItem->iCursor);
  4897   4923           if( (m & notReady)==0 ){
  4898   4924             if( j==iFrom ) iFrom++;
  4899   4925             continue;
  4900   4926           }
  4901   4927           mask = (isOptimal ? m : notReady);
  4902         -        pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0);
         4928  +        pOB = (i==0) ? pOrderBy : 0;
  4903   4929           pDist = (i==0 ? pDistinct : 0);
  4904   4930           if( pTabItem->pIndex==0 ) nUnconstrained++;
  4905   4931     
  4906   4932           WHERETRACE(("=== trying table %d with isOptimal=%d ===\n",
  4907   4933                       j, isOptimal));
  4908   4934           assert( pTabItem->pTab );
  4909   4935   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4910   4936           if( IsVirtual(pTabItem->pTab) ){
  4911   4937             sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo;
  4912         -          bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
         4938  +          bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOB,
  4913   4939                              &sCost, pp);
  4914   4940           }else 
  4915   4941   #endif
  4916   4942           {
  4917         -          bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
         4943  +          bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOB,
  4918   4944                 pDist, &sCost);
  4919   4945           }
  4920   4946           assert( isOptimal || (sCost.used&notReady)==0 );
  4921   4947   
  4922   4948           /* If an INDEXED BY clause is present, then the plan must use that
  4923   4949           ** index if it uses any index at all */
  4924   4950           assert( pTabItem->pIndex==0 
................................................................................
  4969   4995         }
  4970   4996       }
  4971   4997       assert( bestJ>=0 );
  4972   4998       assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
  4973   4999       WHERETRACE(("*** Optimizer selects table %d for loop %d"
  4974   5000                   " with cost=%g and nRow=%g\n",
  4975   5001                   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;
         5002  +    if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
         5003  +      pWInfo->nOBSat = pOrderBy->nExpr;
  4979   5004       }
  4980   5005       if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
  4981   5006         assert( pWInfo->eDistinct==0 );
  4982   5007         pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
  4983   5008       }
  4984   5009       andFlags &= bestPlan.plan.wsFlags;
  4985   5010       pLevel->plan = bestPlan.plan;
................................................................................
  5024   5049     if( pParse->nErr || db->mallocFailed ){
  5025   5050       goto whereBeginError;
  5026   5051     }
  5027   5052   
  5028   5053     /* If the total query only selects a single row, then the ORDER BY
  5029   5054     ** clause is irrelevant.
  5030   5055     */
  5031         -  if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
  5032         -    *ppOrderBy = 0;
         5056  +  if( (andFlags & WHERE_UNIQUE)!=0 && pOrderBy ){
         5057  +    pWInfo->nOBSat = pOrderBy->nExpr;
  5033   5058     }
  5034   5059   
  5035   5060     /* If the caller is an UPDATE or DELETE statement that is requesting
  5036   5061     ** to use a one-pass algorithm, determine if this is appropriate.
  5037   5062     ** The one-pass algorithm only works if the WHERE clause constraints
  5038   5063     ** the statement to update a single row.
  5039   5064     */
................................................................................
  5124   5149     ** is not used, its name is just '{}'.  If no index is used
  5125   5150     ** the index is listed as "{}".  If the primary key is used the
  5126   5151     ** index name is '*'.
  5127   5152     */
  5128   5153     for(i=0; i<nTabList; i++){
  5129   5154       char *z;
  5130   5155       int n;
         5156  +    int w;
  5131   5157       pLevel = &pWInfo->a[i];
         5158  +    w = pLevel->plan.wsFlags;
  5132   5159       pTabItem = &pTabList->a[pLevel->iFrom];
  5133   5160       z = pTabItem->zAlias;
  5134   5161       if( z==0 ) z = pTabItem->pTab->zName;
  5135   5162       n = sqlite3Strlen30(z);
  5136   5163       if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){
  5137         -      if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){
         5164  +      if( (w & WHERE_IDX_ONLY)!=0 && (w & WHERE_COVER_SCAN)==0 ){
  5138   5165           memcpy(&sqlite3_query_plan[nQPlan], "{}", 2);
  5139   5166           nQPlan += 2;
  5140   5167         }else{
  5141   5168           memcpy(&sqlite3_query_plan[nQPlan], z, n);
  5142   5169           nQPlan += n;
  5143   5170         }
  5144   5171         sqlite3_query_plan[nQPlan++] = ' ';
  5145   5172       }
  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) ){
         5173  +    testcase( w & WHERE_ROWID_EQ );
         5174  +    testcase( w & WHERE_ROWID_RANGE );
         5175  +    if( w & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){
  5149   5176         memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
  5150   5177         nQPlan += 2;
  5151         -    }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
         5178  +    }else if( (w & WHERE_INDEXED)!=0 && (w & WHERE_COVER_SCAN)==0 ){
  5152   5179         n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName);
  5153   5180         if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){
  5154   5181           memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n);
  5155   5182           nQPlan += n;
  5156   5183           sqlite3_query_plan[nQPlan++] = ' ';
  5157   5184         }
  5158   5185       }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/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.

    81     81     execsql { INSERT INTO t1 VALUES(4, randomblob(600)) }
    82     82     db_write db
    83     83   } {0 4 0}
    84     84   do_test 2.3 { db_write db 1 } {0 4 0}
    85     85   do_test 2.4 { db_write db 0 } {0 0 0}
    86     86   do_test 2.5 { db_write db 1 } {0 0 0}
    87     87   
    88         -do_test 2.6 { 
    89         -  execsql { PRAGMA journal_mode = WAL }
    90         -  db_write db 1
    91         -} {0 1 0}
           88  +ifcapable wal {
           89  +  do_test 2.6 { 
           90  +    execsql { PRAGMA journal_mode = WAL }
           91  +    db_write db 1
           92  +  } {0 1 0}
           93  +}
    92     94   do_test 2.7 { 
    93     95     execsql { INSERT INTO t1 VALUES(5, randomblob(600)) }
    94     96     db_write db
    95     97   } {0 4 0}
    96     98   do_test 2.8 { db_write db 1 } {0 4 0}
    97     99   do_test 2.9 { db_write db 0 } {0 0 0}
    98    100    
    99    101   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.

  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   1026       4,5 f   1 o   7,6   s 3,2 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     }

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/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/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       #
................................................................................
   543    544             set re [string range $expected 1 end-1]
   544    545             set ok [regexp $re $result]
   545    546           }
   546    547         } else {
   547    548           set ok [expr {[string compare $result $expected]==0}]
   548    549         }
   549    550         if {!$ok} {
          551  +        # if {![info exists ::testprefix] || $::testprefix eq ""} {
          552  +        #   error "no test prefix"
          553  +        # }
   550    554           puts "\nExpected: \[$expected\]\n     Got: \[$result\]"
   551    555           fail_test $name
   552    556         } else {
   553    557           puts " Ok"
   554    558         }
   555    559       }
   556    560     } else {
................................................................................
   989    993       }
   990    994       append ret $char
   991    995       set state $newstate
   992    996     }
   993    997     if {$state} {append ret )}
   994    998     return $ret
   995    999   }
         1000  +
         1001  +# Returns non-zero if the capabilities are present; zero otherwise.
         1002  +#
         1003  +proc capable {expr} {
         1004  +  set e [fix_ifcapable_expr $expr]; return [expr ($e)]
         1005  +}
   996   1006   
   997   1007   # Evaluate a boolean expression of capabilities.  If true, execute the
   998   1008   # code.  Omit the code if false.
   999   1009   #
  1000   1010   proc ifcapable {expr code {else ""} {elsecode ""}} {
  1001   1011     #regsub -all {[a-z_0-9]+} $expr {$::sqlite_options(&)} e2
  1002   1012     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  +set ::testprefix tkt-5d863f876e
           21  +ifcapable !wal {finish_test ; return }
    20     22   
    21     23   do_multiclient_test tn {
    22     24     do_test $tn.1 {
    23     25       sql1 {
    24     26         CREATE TABLE t1(a, b);
    25     27         CREATE INDEX i1 ON t1(a, b);
    26     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/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  +ifcapable !wal {finish_test ; return }
    29     30   
    30     31   db close
    31     32   forcedelete test.db test.db-wal
    32     33   
    33     34   sqlite3 db test.db
    34     35   sqlite3 db2 test.db
    35     36   

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/4 4/1 1/4 1/1 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/4 4/1 1/4 1/1 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"