/ Check-in [3deaa6e2]
Login

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

Overview
Comment:Merge the latest trunk and wal2 enhancements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent-pnu-wal2
Files: files | file ages | folders
SHA3-256: 3deaa6e23b2e7f90f91f48435a9a7bfa124b488cd8498d57d8c71a9419cee5db
User & Date: drh 2019-05-14 22:07:36
Wiki:begin-concurrent-pnu-wal2
Context
2019-07-08
21:25
Merge enhancments from wal2, which also merges trunk enhancements. check-in: 08dcb459 user: drh tags: begin-concurrent-pnu-wal2
2019-05-14
22:07
Merge the latest trunk and wal2 enhancements. check-in: 3deaa6e2 user: drh tags: begin-concurrent-pnu-wal2
22:02
Merge the latest trunk enhancements into the wal2 branch. check-in: 23ec34e4 user: drh tags: wal2
2019-04-15
15:28
Merge all recent enhancements. check-in: 1cd536ec user: drh tags: begin-concurrent-pnu-wal2
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to VERSION.

     1         -3.28.0
            1  +3.29.0

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.69 for sqlite 3.28.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.29.0.
     4      4   #
     5      5   #
     6      6   # Copyright (C) 1992-1996, 1998-2012 Free Software Foundation, Inc.
     7      7   #
     8      8   #
     9      9   # This configure script is free software; the Free Software Foundation
    10     10   # gives unlimited permission to copy, distribute and modify it.
................................................................................
   722    722   subdirs=
   723    723   MFLAGS=
   724    724   MAKEFLAGS=
   725    725   
   726    726   # Identity of this package.
   727    727   PACKAGE_NAME='sqlite'
   728    728   PACKAGE_TARNAME='sqlite'
   729         -PACKAGE_VERSION='3.28.0'
   730         -PACKAGE_STRING='sqlite 3.28.0'
          729  +PACKAGE_VERSION='3.29.0'
          730  +PACKAGE_STRING='sqlite 3.29.0'
   731    731   PACKAGE_BUGREPORT=''
   732    732   PACKAGE_URL=''
   733    733   
   734    734   # Factoring default headers for most tests.
   735    735   ac_includes_default="\
   736    736   #include <stdio.h>
   737    737   #ifdef HAVE_SYS_TYPES_H
................................................................................
   859    859   pdfdir
   860    860   dvidir
   861    861   htmldir
   862    862   infodir
   863    863   docdir
   864    864   oldincludedir
   865    865   includedir
   866         -runstatedir
   867    866   localstatedir
   868    867   sharedstatedir
   869    868   sysconfdir
   870    869   datadir
   871    870   datarootdir
   872    871   libexecdir
   873    872   sbindir
................................................................................
   961    960   sbindir='${exec_prefix}/sbin'
   962    961   libexecdir='${exec_prefix}/libexec'
   963    962   datarootdir='${prefix}/share'
   964    963   datadir='${datarootdir}'
   965    964   sysconfdir='${prefix}/etc'
   966    965   sharedstatedir='${prefix}/com'
   967    966   localstatedir='${prefix}/var'
   968         -runstatedir='${localstatedir}/run'
   969    967   includedir='${prefix}/include'
   970    968   oldincludedir='/usr/include'
   971    969   docdir='${datarootdir}/doc/${PACKAGE_TARNAME}'
   972    970   infodir='${datarootdir}/info'
   973    971   htmldir='${docdir}'
   974    972   dvidir='${docdir}'
   975    973   pdfdir='${docdir}'
................................................................................
  1214   1212     -psdir=* | --psdir=* | --psdi=* | --psd=* | --ps=*)
  1215   1213       psdir=$ac_optarg ;;
  1216   1214   
  1217   1215     -q | -quiet | --quiet | --quie | --qui | --qu | --q \
  1218   1216     | -silent | --silent | --silen | --sile | --sil)
  1219   1217       silent=yes ;;
  1220   1218   
  1221         -  -runstatedir | --runstatedir | --runstatedi | --runstated \
  1222         -  | --runstate | --runstat | --runsta | --runst | --runs \
  1223         -  | --run | --ru | --r)
  1224         -    ac_prev=runstatedir ;;
  1225         -  -runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \
  1226         -  | --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \
  1227         -  | --run=* | --ru=* | --r=*)
  1228         -    runstatedir=$ac_optarg ;;
  1229         -
  1230   1219     -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb)
  1231   1220       ac_prev=sbindir ;;
  1232   1221     -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \
  1233   1222     | --sbi=* | --sb=*)
  1234   1223       sbindir=$ac_optarg ;;
  1235   1224   
  1236   1225     -sharedstatedir | --sharedstatedir | --sharedstatedi \
................................................................................
  1360   1349     esac
  1361   1350   fi
  1362   1351   
  1363   1352   # Check all directory arguments for consistency.
  1364   1353   for ac_var in	exec_prefix prefix bindir sbindir libexecdir datarootdir \
  1365   1354   		datadir sysconfdir sharedstatedir localstatedir includedir \
  1366   1355   		oldincludedir docdir infodir htmldir dvidir pdfdir psdir \
  1367         -		libdir localedir mandir runstatedir
         1356  +		libdir localedir mandir
  1368   1357   do
  1369   1358     eval ac_val=\$$ac_var
  1370   1359     # Remove trailing slashes.
  1371   1360     case $ac_val in
  1372   1361       */ )
  1373   1362         ac_val=`expr "X$ac_val" : 'X\(.*[^/]\)' \| "X$ac_val" : 'X\(.*\)'`
  1374   1363         eval $ac_var=\$ac_val;;
................................................................................
  1473   1462   #
  1474   1463   # Report the --help message.
  1475   1464   #
  1476   1465   if test "$ac_init_help" = "long"; then
  1477   1466     # Omit some internal or obsolete options to make the list less imposing.
  1478   1467     # This message is too long to be a string in the A/UX 3.1 sh.
  1479   1468     cat <<_ACEOF
  1480         -\`configure' configures sqlite 3.28.0 to adapt to many kinds of systems.
         1469  +\`configure' configures sqlite 3.29.0 to adapt to many kinds of systems.
  1481   1470   
  1482   1471   Usage: $0 [OPTION]... [VAR=VALUE]...
  1483   1472   
  1484   1473   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1485   1474   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1486   1475   
  1487   1476   Defaults for the options are specified in brackets.
................................................................................
  1513   1502   Fine tuning of the installation directories:
  1514   1503     --bindir=DIR            user executables [EPREFIX/bin]
  1515   1504     --sbindir=DIR           system admin executables [EPREFIX/sbin]
  1516   1505     --libexecdir=DIR        program executables [EPREFIX/libexec]
  1517   1506     --sysconfdir=DIR        read-only single-machine data [PREFIX/etc]
  1518   1507     --sharedstatedir=DIR    modifiable architecture-independent data [PREFIX/com]
  1519   1508     --localstatedir=DIR     modifiable single-machine data [PREFIX/var]
  1520         -  --runstatedir=DIR       modifiable per-process data [LOCALSTATEDIR/run]
  1521   1509     --libdir=DIR            object code libraries [EPREFIX/lib]
  1522   1510     --includedir=DIR        C header files [PREFIX/include]
  1523   1511     --oldincludedir=DIR     C header files for non-gcc [/usr/include]
  1524   1512     --datarootdir=DIR       read-only arch.-independent data root [PREFIX/share]
  1525   1513     --datadir=DIR           read-only architecture-independent data [DATAROOTDIR]
  1526   1514     --infodir=DIR           info documentation [DATAROOTDIR/info]
  1527   1515     --localedir=DIR         locale-dependent data [DATAROOTDIR/locale]
................................................................................
  1539   1527     --build=BUILD     configure for building on BUILD [guessed]
  1540   1528     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1541   1529   _ACEOF
  1542   1530   fi
  1543   1531   
  1544   1532   if test -n "$ac_init_help"; then
  1545   1533     case $ac_init_help in
  1546         -     short | recursive ) echo "Configuration of sqlite 3.28.0:";;
         1534  +     short | recursive ) echo "Configuration of sqlite 3.29.0:";;
  1547   1535      esac
  1548   1536     cat <<\_ACEOF
  1549   1537   
  1550   1538   Optional Features:
  1551   1539     --disable-option-checking  ignore unrecognized --enable/--with options
  1552   1540     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1553   1541     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1665   1653       cd "$ac_pwd" || { ac_status=$?; break; }
  1666   1654     done
  1667   1655   fi
  1668   1656   
  1669   1657   test -n "$ac_init_help" && exit $ac_status
  1670   1658   if $ac_init_version; then
  1671   1659     cat <<\_ACEOF
  1672         -sqlite configure 3.28.0
         1660  +sqlite configure 3.29.0
  1673   1661   generated by GNU Autoconf 2.69
  1674   1662   
  1675   1663   Copyright (C) 2012 Free Software Foundation, Inc.
  1676   1664   This configure script is free software; the Free Software Foundation
  1677   1665   gives unlimited permission to copy, distribute and modify it.
  1678   1666   _ACEOF
  1679   1667     exit
................................................................................
  2084   2072     eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
  2085   2073   
  2086   2074   } # ac_fn_c_check_header_mongrel
  2087   2075   cat >config.log <<_ACEOF
  2088   2076   This file contains any messages produced by compilers while
  2089   2077   running configure, to aid debugging if configure makes a mistake.
  2090   2078   
  2091         -It was created by sqlite $as_me 3.28.0, which was
         2079  +It was created by sqlite $as_me 3.29.0, which was
  2092   2080   generated by GNU Autoconf 2.69.  Invocation command line was
  2093   2081   
  2094   2082     $ $0 $@
  2095   2083   
  2096   2084   _ACEOF
  2097   2085   exec 5>>config.log
  2098   2086   {
................................................................................
  3942   3930   { $as_echo "$as_me:${as_lineno-$LINENO}: checking the name lister ($NM) interface" >&5
  3943   3931   $as_echo_n "checking the name lister ($NM) interface... " >&6; }
  3944   3932   if ${lt_cv_nm_interface+:} false; then :
  3945   3933     $as_echo_n "(cached) " >&6
  3946   3934   else
  3947   3935     lt_cv_nm_interface="BSD nm"
  3948   3936     echo "int some_variable = 0;" > conftest.$ac_ext
  3949         -  (eval echo "\"\$as_me:3949: $ac_compile\"" >&5)
         3937  +  (eval echo "\"\$as_me:3937: $ac_compile\"" >&5)
  3950   3938     (eval "$ac_compile" 2>conftest.err)
  3951   3939     cat conftest.err >&5
  3952         -  (eval echo "\"\$as_me:3952: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
         3940  +  (eval echo "\"\$as_me:3940: $NM \\\"conftest.$ac_objext\\\"\"" >&5)
  3953   3941     (eval "$NM \"conftest.$ac_objext\"" 2>conftest.err > conftest.out)
  3954   3942     cat conftest.err >&5
  3955         -  (eval echo "\"\$as_me:3955: output\"" >&5)
         3943  +  (eval echo "\"\$as_me:3943: output\"" >&5)
  3956   3944     cat conftest.out >&5
  3957   3945     if $GREP 'External.*some_variable' conftest.out > /dev/null; then
  3958   3946       lt_cv_nm_interface="MS dumpbin"
  3959   3947     fi
  3960   3948     rm -f conftest*
  3961   3949   fi
  3962   3950   { $as_echo "$as_me:${as_lineno-$LINENO}: result: $lt_cv_nm_interface" >&5
................................................................................
  5154   5142   	;;
  5155   5143       esac
  5156   5144     fi
  5157   5145     rm -rf conftest*
  5158   5146     ;;
  5159   5147   *-*-irix6*)
  5160   5148     # Find out which ABI we are using.
  5161         -  echo '#line 5161 "configure"' > conftest.$ac_ext
         5149  +  echo '#line 5149 "configure"' > conftest.$ac_ext
  5162   5150     if { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_compile\""; } >&5
  5163   5151     (eval $ac_compile) 2>&5
  5164   5152     ac_status=$?
  5165   5153     $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
  5166   5154     test $ac_status = 0; }; then
  5167   5155       if test "$lt_cv_prog_gnu_ld" = yes; then
  5168   5156         case `/usr/bin/file conftest.$ac_objext` in
................................................................................
  6679   6667      # Note that $ac_compile itself does not contain backslashes and begins
  6680   6668      # with a dollar sign (not a hyphen), so the echo should work correctly.
  6681   6669      # The option is referenced via a variable to avoid confusing sed.
  6682   6670      lt_compile=`echo "$ac_compile" | $SED \
  6683   6671      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  6684   6672      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  6685   6673      -e 's:$: $lt_compiler_flag:'`
  6686         -   (eval echo "\"\$as_me:6686: $lt_compile\"" >&5)
         6674  +   (eval echo "\"\$as_me:6674: $lt_compile\"" >&5)
  6687   6675      (eval "$lt_compile" 2>conftest.err)
  6688   6676      ac_status=$?
  6689   6677      cat conftest.err >&5
  6690         -   echo "$as_me:6690: \$? = $ac_status" >&5
         6678  +   echo "$as_me:6678: \$? = $ac_status" >&5
  6691   6679      if (exit $ac_status) && test -s "$ac_outfile"; then
  6692   6680        # The compiler can only warn and ignore the option if not recognized
  6693   6681        # So say no if there are warnings other than the usual output.
  6694   6682        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  6695   6683        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  6696   6684        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  6697   6685          lt_cv_prog_compiler_rtti_exceptions=yes
................................................................................
  7018   7006      # Note that $ac_compile itself does not contain backslashes and begins
  7019   7007      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7020   7008      # The option is referenced via a variable to avoid confusing sed.
  7021   7009      lt_compile=`echo "$ac_compile" | $SED \
  7022   7010      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7023   7011      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7024   7012      -e 's:$: $lt_compiler_flag:'`
  7025         -   (eval echo "\"\$as_me:7025: $lt_compile\"" >&5)
         7013  +   (eval echo "\"\$as_me:7013: $lt_compile\"" >&5)
  7026   7014      (eval "$lt_compile" 2>conftest.err)
  7027   7015      ac_status=$?
  7028   7016      cat conftest.err >&5
  7029         -   echo "$as_me:7029: \$? = $ac_status" >&5
         7017  +   echo "$as_me:7017: \$? = $ac_status" >&5
  7030   7018      if (exit $ac_status) && test -s "$ac_outfile"; then
  7031   7019        # The compiler can only warn and ignore the option if not recognized
  7032   7020        # So say no if there are warnings other than the usual output.
  7033   7021        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
  7034   7022        $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
  7035   7023        if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
  7036   7024          lt_cv_prog_compiler_pic_works=yes
................................................................................
  7123   7111      # (2) before a word containing "conftest.", or (3) at the end.
  7124   7112      # Note that $ac_compile itself does not contain backslashes and begins
  7125   7113      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7126   7114      lt_compile=`echo "$ac_compile" | $SED \
  7127   7115      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7128   7116      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7129   7117      -e 's:$: $lt_compiler_flag:'`
  7130         -   (eval echo "\"\$as_me:7130: $lt_compile\"" >&5)
         7118  +   (eval echo "\"\$as_me:7118: $lt_compile\"" >&5)
  7131   7119      (eval "$lt_compile" 2>out/conftest.err)
  7132   7120      ac_status=$?
  7133   7121      cat out/conftest.err >&5
  7134         -   echo "$as_me:7134: \$? = $ac_status" >&5
         7122  +   echo "$as_me:7122: \$? = $ac_status" >&5
  7135   7123      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7136   7124      then
  7137   7125        # The compiler can only warn and ignore the option if not recognized
  7138   7126        # So say no if there are warnings
  7139   7127        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7140   7128        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7141   7129        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  7178   7166      # (2) before a word containing "conftest.", or (3) at the end.
  7179   7167      # Note that $ac_compile itself does not contain backslashes and begins
  7180   7168      # with a dollar sign (not a hyphen), so the echo should work correctly.
  7181   7169      lt_compile=`echo "$ac_compile" | $SED \
  7182   7170      -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
  7183   7171      -e 's: [^ ]*conftest\.: $lt_compiler_flag&:; t' \
  7184   7172      -e 's:$: $lt_compiler_flag:'`
  7185         -   (eval echo "\"\$as_me:7185: $lt_compile\"" >&5)
         7173  +   (eval echo "\"\$as_me:7173: $lt_compile\"" >&5)
  7186   7174      (eval "$lt_compile" 2>out/conftest.err)
  7187   7175      ac_status=$?
  7188   7176      cat out/conftest.err >&5
  7189         -   echo "$as_me:7189: \$? = $ac_status" >&5
         7177  +   echo "$as_me:7177: \$? = $ac_status" >&5
  7190   7178      if (exit $ac_status) && test -s out/conftest2.$ac_objext
  7191   7179      then
  7192   7180        # The compiler can only warn and ignore the option if not recognized
  7193   7181        # So say no if there are warnings
  7194   7182        $ECHO "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
  7195   7183        $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
  7196   7184        if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
................................................................................
  9558   9546   else
  9559   9547     	  if test "$cross_compiling" = yes; then :
  9560   9548     lt_cv_dlopen_self=cross
  9561   9549   else
  9562   9550     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9563   9551     lt_status=$lt_dlunknown
  9564   9552     cat > conftest.$ac_ext <<_LT_EOF
  9565         -#line 9565 "configure"
         9553  +#line 9553 "configure"
  9566   9554   #include "confdefs.h"
  9567   9555   
  9568   9556   #if HAVE_DLFCN_H
  9569   9557   #include <dlfcn.h>
  9570   9558   #endif
  9571   9559   
  9572   9560   #include <stdio.h>
................................................................................
  9654   9642   else
  9655   9643     	  if test "$cross_compiling" = yes; then :
  9656   9644     lt_cv_dlopen_self_static=cross
  9657   9645   else
  9658   9646     lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
  9659   9647     lt_status=$lt_dlunknown
  9660   9648     cat > conftest.$ac_ext <<_LT_EOF
  9661         -#line 9661 "configure"
         9649  +#line 9649 "configure"
  9662   9650   #include "confdefs.h"
  9663   9651   
  9664   9652   #if HAVE_DLFCN_H
  9665   9653   #include <dlfcn.h>
  9666   9654   #endif
  9667   9655   
  9668   9656   #include <stdio.h>
................................................................................
 10003   9991   	 cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 10004   9992   /* end confdefs.h.  */
 10005   9993   #include <sys/types.h>
 10006   9994    /* Check that off_t can represent 2**63 - 1 correctly.
 10007   9995       We can't simply define LARGE_OFF_T to be 9223372036854775807,
 10008   9996       since some C++ compilers masquerading as C compilers
 10009   9997       incorrectly reject 9223372036854775807.  */
 10010         -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
         9998  +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
 10011   9999     int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 10012  10000   		       && LARGE_OFF_T % 2147483647 == 1)
 10013  10001   		      ? 1 : -1];
 10014  10002   int
 10015  10003   main ()
 10016  10004   {
 10017  10005   
................................................................................
 10049  10037     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 10050  10038   /* end confdefs.h.  */
 10051  10039   #include <sys/types.h>
 10052  10040    /* Check that off_t can represent 2**63 - 1 correctly.
 10053  10041       We can't simply define LARGE_OFF_T to be 9223372036854775807,
 10054  10042       since some C++ compilers masquerading as C compilers
 10055  10043       incorrectly reject 9223372036854775807.  */
 10056         -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
        10044  +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
 10057  10045     int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 10058  10046   		       && LARGE_OFF_T % 2147483647 == 1)
 10059  10047   		      ? 1 : -1];
 10060  10048   int
 10061  10049   main ()
 10062  10050   {
 10063  10051   
................................................................................
 10073  10061   /* end confdefs.h.  */
 10074  10062   #define _FILE_OFFSET_BITS 64
 10075  10063   #include <sys/types.h>
 10076  10064    /* Check that off_t can represent 2**63 - 1 correctly.
 10077  10065       We can't simply define LARGE_OFF_T to be 9223372036854775807,
 10078  10066       since some C++ compilers masquerading as C compilers
 10079  10067       incorrectly reject 9223372036854775807.  */
 10080         -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
        10068  +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
 10081  10069     int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 10082  10070   		       && LARGE_OFF_T % 2147483647 == 1)
 10083  10071   		      ? 1 : -1];
 10084  10072   int
 10085  10073   main ()
 10086  10074   {
 10087  10075   
................................................................................
 10118  10106     cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 10119  10107   /* end confdefs.h.  */
 10120  10108   #include <sys/types.h>
 10121  10109    /* Check that off_t can represent 2**63 - 1 correctly.
 10122  10110       We can't simply define LARGE_OFF_T to be 9223372036854775807,
 10123  10111       since some C++ compilers masquerading as C compilers
 10124  10112       incorrectly reject 9223372036854775807.  */
 10125         -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
        10113  +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
 10126  10114     int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 10127  10115   		       && LARGE_OFF_T % 2147483647 == 1)
 10128  10116   		      ? 1 : -1];
 10129  10117   int
 10130  10118   main ()
 10131  10119   {
 10132  10120   
................................................................................
 10142  10130   /* end confdefs.h.  */
 10143  10131   #define _LARGE_FILES 1
 10144  10132   #include <sys/types.h>
 10145  10133    /* Check that off_t can represent 2**63 - 1 correctly.
 10146  10134       We can't simply define LARGE_OFF_T to be 9223372036854775807,
 10147  10135       since some C++ compilers masquerading as C compilers
 10148  10136       incorrectly reject 9223372036854775807.  */
 10149         -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31))
        10137  +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62))
 10150  10138     int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721
 10151  10139   		       && LARGE_OFF_T % 2147483647 == 1)
 10152  10140   		      ? 1 : -1];
 10153  10141   int
 10154  10142   main ()
 10155  10143   {
 10156  10144   
................................................................................
 12240  12228   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12241  12229   
 12242  12230   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12243  12231   # Save the log message, to keep $0 and so on meaningful, and to
 12244  12232   # report actual input values of CONFIG_FILES etc. instead of their
 12245  12233   # values after options handling.
 12246  12234   ac_log="
 12247         -This file was extended by sqlite $as_me 3.28.0, which was
        12235  +This file was extended by sqlite $as_me 3.29.0, which was
 12248  12236   generated by GNU Autoconf 2.69.  Invocation command line was
 12249  12237   
 12250  12238     CONFIG_FILES    = $CONFIG_FILES
 12251  12239     CONFIG_HEADERS  = $CONFIG_HEADERS
 12252  12240     CONFIG_LINKS    = $CONFIG_LINKS
 12253  12241     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12254  12242     $ $0 $@
................................................................................
 12306  12294   
 12307  12295   Report bugs to the package provider."
 12308  12296   
 12309  12297   _ACEOF
 12310  12298   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12311  12299   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12312  12300   ac_cs_version="\\
 12313         -sqlite config.status 3.28.0
        12301  +sqlite config.status 3.29.0
 12314  12302   configured by $0, generated by GNU Autoconf 2.69,
 12315  12303     with options \\"\$ac_cs_config\\"
 12316  12304   
 12317  12305   Copyright (C) 2012 Free Software Foundation, Inc.
 12318  12306   This config.status script is free software; the Free Software Foundation
 12319  12307   gives unlimited permission to copy, distribute and modify it."
 12320  12308   

Changes to ext/fts3/fts3_write.c.

  3750   3750       p->aNode = 0;
  3751   3751     }else{
  3752   3752       if( bFirst==0 ){
  3753   3753         p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
  3754   3754       }
  3755   3755       p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
  3756   3756   
  3757         -    if( nPrefix>p->iOff || nSuffix>p->nNode-p->iOff ){
         3757  +    if( nPrefix>p->term.n || nSuffix>p->nNode-p->iOff || nSuffix==0 ){
  3758   3758         return FTS_CORRUPT_VTAB;
  3759   3759       }
  3760   3760       blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
  3761   3761       if( rc==SQLITE_OK ){
  3762   3762         memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
  3763   3763         p->term.n = nPrefix+nSuffix;
  3764   3764         p->iOff += nSuffix;
................................................................................
  3769   3769           }
  3770   3770           p->aDoclist = &p->aNode[p->iOff];
  3771   3771           p->iOff += p->nDoclist;
  3772   3772         }
  3773   3773       }
  3774   3774     }
  3775   3775   
  3776         -  assert( p->iOff<=p->nNode );
         3776  +  assert_fts3_nc( p->iOff<=p->nNode );
  3777   3777     return rc;
  3778   3778   }
  3779   3779   
  3780   3780   /*
  3781   3781   ** Release all dynamic resources held by node-reader object *p.
  3782   3782   */
  3783   3783   static void nodeReaderRelease(NodeReader *p){

Changes to ext/fts5/fts5_buffer.c.

   174    174       /* EOF */
   175    175       *piOff = -1;
   176    176       return 1;  
   177    177     }else{
   178    178       i64 iOff = *piOff;
   179    179       int iVal;
   180    180       fts5FastGetVarint32(a, i, iVal);
   181         -    if( iVal==1 ){
          181  +    if( iVal<=1 ){
          182  +      if( iVal==0 ){
          183  +        *pi = i;
          184  +        return 0;
          185  +      }
   182    186         fts5FastGetVarint32(a, i, iVal);
   183    187         iOff = ((i64)iVal) << 32;
   184    188         fts5FastGetVarint32(a, i, iVal);
          189  +      if( iVal<2 ){
          190  +        /* This is a corrupt record. So stop parsing it here. */
          191  +        *piOff = -1;
          192  +        return 1;
          193  +      }
   185    194       }
   186    195       *piOff = iOff + ((iVal-2) & 0x7FFFFFFF);
   187    196       *pi = i;
   188    197       return 0;
   189    198     }
   190    199   }
   191    200   

Changes to ext/fts5/fts5_index.c.

  4949   4949       Fts5Buffer tmp = {0, 0, 0};
  4950   4950   
  4951   4951       /* The maximum size of the output is equal to the sum of the two 
  4952   4952       ** input sizes + 1 varint (9 bytes). The extra varint is because if the
  4953   4953       ** first rowid in one input is a large negative number, and the first in
  4954   4954       ** the other a non-negative number, the delta for the non-negative
  4955   4955       ** number will be larger on disk than the literal integer value
  4956         -    ** was.  */
  4957         -    if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9) ) return;
         4956  +    ** was.  
         4957  +    **
         4958  +    ** Or, if the input position-lists are corrupt, then the output might
         4959  +    ** include up to 2 extra 10-byte positions created by interpreting -1
         4960  +    ** (the value PoslistNext64() uses for EOF) as a position and appending
         4961  +    ** it to the output. This can happen at most once for each input 
         4962  +    ** position-list, hence two 10 byte paddings.  */
         4963  +    if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n + 9+10+10) ) return;
  4958   4964       fts5DoclistIterInit(p1, &i1);
  4959   4965       fts5DoclistIterInit(p2, &i2);
  4960   4966   
  4961   4967       while( 1 ){
  4962   4968         if( i1.iRowid<i2.iRowid ){
  4963   4969           /* Copy entry from i1 */
  4964   4970           fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
  4965   4971           fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
  4966   4972           fts5DoclistIterNext(&i1);
  4967   4973           if( i1.aPoslist==0 ) break;
         4974  +        assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
  4968   4975         }
  4969   4976         else if( i2.iRowid!=i1.iRowid ){
  4970   4977           /* Copy entry from i2 */
  4971   4978           fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
  4972   4979           fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
  4973   4980           fts5DoclistIterNext(&i2);
  4974   4981           if( i2.aPoslist==0 ) break;
         4982  +        assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
  4975   4983         }
  4976   4984         else{
  4977   4985           /* Merge the two position lists. */ 
  4978   4986           i64 iPos1 = 0;
  4979   4987           i64 iPos2 = 0;
  4980   4988           int iOff1 = 0;
  4981   4989           int iOff2 = 0;
................................................................................
  4991   4999           fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
  4992   5000           fts5BufferZero(&tmp);
  4993   5001           sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
  4994   5002           if( p->rc ) break;
  4995   5003   
  4996   5004           sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
  4997   5005           sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
  4998         -        assert( iPos1>=0 && iPos2>=0 );
         5006  +        assert_nc( iPos1>=0 && iPos2>=0 );
  4999   5007   
  5000   5008           if( iPos1<iPos2 ){
  5001   5009             sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
  5002   5010             sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
  5003   5011           }else{
  5004   5012             sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
  5005   5013             sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
  5006   5014           }
  5007         -
  5008   5015           if( iPos1>=0 && iPos2>=0 ){
  5009   5016             while( 1 ){
  5010   5017               if( iPos1<iPos2 ){
  5011   5018                 if( iPos1!=iPrev ){
  5012   5019                   sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
  5013   5020                 }
  5014   5021                 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
................................................................................
  5025   5032           if( iPos1>=0 ){
  5026   5033             if( iPos1!=iPrev ){
  5027   5034               sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
  5028   5035             }
  5029   5036             aCopy = &a1[iOff1];
  5030   5037             nCopy = i1.nPoslist - iOff1;
  5031   5038           }else{
  5032         -          assert( iPos2>=0 && iPos2!=iPrev );
         5039  +          assert_nc( iPos2>=0 && iPos2!=iPrev );
  5033   5040             sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
  5034   5041             aCopy = &a2[iOff2];
  5035   5042             nCopy = i2.nPoslist - iOff2;
  5036   5043           }
  5037   5044           if( nCopy>0 ){
  5038   5045             fts5BufferSafeAppendBlob(&tmp, aCopy, nCopy);
  5039   5046           }
  5040   5047   
  5041   5048           /* WRITEPOSLISTSIZE */
  5042   5049           fts5BufferSafeAppendVarint(&out, tmp.n * 2);
  5043   5050           fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
  5044   5051           fts5DoclistIterNext(&i1);
  5045   5052           fts5DoclistIterNext(&i2);
  5046         -        assert( out.n<=(p1->n+p2->n+9) );
         5053  +        assert_nc( out.n<=(p1->n+p2->n+9) );
  5047   5054           if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
         5055  +        assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );
  5048   5056         }
  5049   5057       }
  5050   5058   
  5051   5059       if( i1.aPoslist ){
  5052   5060         fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
  5053   5061         fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
  5054   5062       }
  5055   5063       else if( i2.aPoslist ){
  5056   5064         fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
  5057   5065         fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
  5058   5066       }
  5059         -    assert( out.n<=(p1->n+p2->n+9) );
         5067  +    assert_nc( out.n<=(p1->n+p2->n+9) );
  5060   5068   
  5061   5069       fts5BufferSet(&p->rc, p1, out.n, out.p);
  5062   5070       fts5BufferFree(&tmp);
  5063   5071       fts5BufferFree(&out);
  5064   5072     }
  5065   5073   }
  5066   5074   

Changes to ext/fts5/fts5_vocab.c.

   569    569             if( sqlite3Fts5IterEof(pCsr->pIter) ) break;
   570    570           }
   571    571         }
   572    572       }
   573    573     }
   574    574   
   575    575     if( rc==SQLITE_OK && pCsr->bEof==0 && pTab->eType==FTS5_VOCAB_COL ){
   576         -    while( pCsr->aDoc[pCsr->iCol]==0 ) pCsr->iCol++;
   577         -    assert( pCsr->iCol<pCsr->pFts5->pConfig->nCol );
          576  +    for(/* noop */; pCsr->iCol<nCol && pCsr->aDoc[pCsr->iCol]==0; pCsr->iCol++);
          577  +    if( pCsr->iCol==nCol ){
          578  +      rc = FTS5_CORRUPT;
          579  +    }
   578    580     }
   579    581     return rc;
   580    582   }
   581    583   
   582    584   /*
   583    585   ** This is the xFilter implementation for the virtual table.
   584    586   */

Changes to ext/fts5/test/fts5corrupt3.test.

  5809   5809     INSERT INTO t1_content VALUES(3,'a b c','g h i','g h i');
  5810   5810     INSERT INTO t1_docsize VALUES(1,X'030003');
  5811   5811     INSERT INTO t1_docsize VALUES(2,X'030003');
  5812   5812     INSERT INTO t1_docsize VALUES(3,X'030003');
  5813   5813   } {}
  5814   5814   
  5815   5815   do_catchsql_test 44.2 {
  5816         -INSERT INTO t1(t1) VALUES('integrity-check');
         5816  +  INSERT INTO t1(t1) VALUES('integrity-check');
  5817   5817   } {1 {database disk image is malformed}}
  5818   5818   
  5819         -do_catchsql_test 44.2 {
         5819  +do_catchsql_test 44.3 {
  5820   5820     SELECT snippet(t1, -1, '.', '..', '', 2 ) FROM t1('g h') ORDER BY rank; 
  5821         -} {1 {database disk image is malformed}}
         5821  +} {0 {{.g.. .h..} {.g.. h} {.g.. .h..}}}
  5822   5822   
  5823   5823   #--------------------------------------------------------------------------
  5824   5824   reset_db
  5825   5825   do_test 45.0 {
  5826   5826     sqlite3 db {}
  5827   5827     db deserialize [decode_hexdb {
  5828   5828   | size 24576 pagesize 4096 filename crash-0b162c9e69b999.db
................................................................................
  6414   6414   | page 10 offset 36864
  6415   6415   |      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
  6416   6416   |   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
  6417   6417   | end 4b6fc659283f2735616c.db
  6418   6418   }]} {}
  6419   6419   
  6420   6420   do_catchsql_test 47.1 {
  6421         -  SELECT snippet(t1, -1, '.', '..', '[', 50), 
  6422         -         highlight(t1, 2, '[', ']') FROM t1('g h') 
  6423         -  WHERE rank MATCH 'bm25(1.0, 1.0)' ORDER BY rank;
         6421  +  INSERT INTO t1(t1) VALUES('integrity-check');
  6424   6422   } {1 {database disk image is malformed}}
         6423  +
         6424  +do_catchsql_test 47.2 {
         6425  +  SELECT count(*) FROM (
         6426  +      SELECT snippet(t1, -1, '.', '..', '[', 50), 
         6427  +      highlight(t1, 2, '[', ']') FROM t1('g h') 
         6428  +      WHERE rank MATCH 'bm25(1.0, 1.0)' ORDER BY rank
         6429  +  )
         6430  +} {0 3}
  6425   6431   
  6426   6432   #--------------------------------------------------------------------------
  6427   6433   reset_db
  6428   6434   do_test 48.0 {
  6429   6435     sqlite3 db {}
  6430   6436     db deserialize [decode_hexdb {
  6431   6437   | size 32768 pagesize 4096 filename crash-44a8305b4bd86f.db
................................................................................
  8123   8129   |   4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65   heck....optimize
  8124   8130   | end x.db
  8125   8131   }]} {}
  8126   8132   
  8127   8133   do_catchsql_test 57.1 {
  8128   8134     INSERT INTO t1(t1) VALUES('optimize')
  8129   8135   } {1 {database disk image is malformed}}
         8136  +
         8137  +#-------------------------------------------------------------------------
         8138  +reset_db
         8139  +do_test 58.0 {
         8140  +  sqlite3 db {}
         8141  +  db deserialize [decode_hexdb {
         8142  +.open --hexdb
         8143  +| size 24576 pagesize 4096 filename crash-5a5acd0ab42d31.db
         8144  +| page 1 offset 0
         8145  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         8146  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
         8147  +|     96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53   ...............S
         8148  +|    112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00   .....3..........
         8149  +|   3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22   ................
         8150  +|   3600: 06 06 17 11 11 01 31 74 61 62 6c 65 62 62 62 62   ......1tablebbbb
         8151  +|   3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62   .CREATE TABLE bb
         8152  +|   3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65   (a)V.......table
         8153  +|   3648: 74 31 5f 63 2a 6e 66 69 68 74 31 5f 63 6f 6e 66   t1_c*nfiht1_conf
         8154  +|   3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20   ig.CREATE TABLE 
         8155  +|   3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52   't1_config'(k PR
         8156  +|   3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49   IMARY KEY, v) WI
         8157  +|   3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21   THOUT ROWID[...!
         8158  +|   3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 73 73   !...tablet1_doss
         8159  +|   3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52   izet1_docsize.CR
         8160  +|   3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 64   EATE TABLE 't1_d
         8161  +|   3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47   ocsize'(id INTEG
         8162  +|   3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20   ER PRIMARY KEY, 
         8163  +|   3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81   sz BLOB)i.......
         8164  +|   3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69   -tablet1_idxt1_i
         8165  +|   3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20   dx.CREATE TABLE 
         8166  +|   3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20   't1_idx'(segid, 
         8167  +|   3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d   term, pgno, PRIM
         8168  +|   3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74   ARY KEY(segid, t
         8169  +|   3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f   erm)) WITHOUT RO
         8170  +|   3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c   WIDU........tabl
         8171  +|   3936: 65 74 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02   et1_datat1_data.
         8172  +|   3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31   CREATE TABLE 't1
         8173  +|   3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45   _data'(id INTEGE
         8174  +|   3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62   R PRIMARY KEY, b
         8175  +|   4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 11 11   lock BLOB)T.....
         8176  +|   4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41   ...tablet1t1CREA
         8177  +|   4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45   TE VIRTUAL TABLE
         8178  +|   4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61    t1 USING fts5(a
         8179  +|   4064: 2c 62 2c 70 72 65 66 69 78 3d 22 32 2c 32 2c 33   ,b,prefix=.2,2,3
         8180  +|   4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29   ,4., content=..)
         8181  +| page 2 offset 4096
         8182  +|      0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4   ..j.7.L....L....
         8183  +|     16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ae   ...m.K.,........
         8184  +|     32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5   ...l.K.)........
         8185  +|     48: 0d 97 0d 76 0d 54 0d 30 fd 15 0c f3 0c d3 0c b5   ...v.T.0........
         8186  +|     64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0   ...s.T.2........
         8187  +|     80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad   ...~.H..........
         8188  +|     96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 00 00 00 00 00 00   ...m.M.+........
         8189  +|   2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00   ................
         8190  +|   2384: 30 00 00 00 9c 01 03 35 00 03 01 01 12 02 01 12   0......5........
         8191  +|   2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00   ............>...
         8192  +|   2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68   ....4tabl.....wh
         8193  +|   2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c   er.............<
         8194  +|   2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e   .....4fts4.....n
         8195  +|   2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03   umb.............
         8196  +|   2480: 00 3c 00 00 00 16 04 33 74 68 65 03 06 01 01 04   .<.....3the.....
         8197  +|   2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80   ..whe...........
         8198  +|   2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01   ...<.....3num...
         8199  +|   2528: 01 05 01 03 74 61 62 05 62 03 04 0a 19 8c 80 80   ....tab.b.......
         8200  +|   2544: 80 80 0c 03 00 38 00 00 00 14 03 39 a7 68 03 02   .....8.....9.h..
         8201  +|   2560: 04 10 04 33 66 74 73 03 02 02 04 07 18 8c 80 80   ...3fts.........
         8202  +|   2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02   .....6.....2ta..
         8203  +|   2592: 03 02 01 68 03 06 01 01 04 04 07 1b 8c 80 80 80   ...h............
         8204  +|   2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01   ....<.....2nu...
         8205  +|   2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80   ....of..........
         8206  +|   2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03   ......8.....2ft.
         8207  +|   2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80   ....is..........
         8208  +|   2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08   ......6.....1t..
         8209  +|   2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80   ......w.........
         8210  +|   2704: 80 80 07 03 00 3a ff 00 00 15 02 31 6e 03 08 01   .....:.....1n...
         8211  +|   2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80   .....o..........
         8212  +|   2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03   ......6......1f.
         8213  +|   2752: 02 01 f1 01 69 03 06 01 01 03 05 06 1c 8c 80 80   ....i...........
         8214  +|   2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03   .....>.....0the.
         8215  +|   2784: 06 01 01 14 01 05 77 68 65 72 65 03 02 04 0a 15   ......where.....
         8216  +|   2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06   ........0.......
         8217  +|   2816: 06 30 74 61 62 6c cc 03 02 03 07 1c 8c 80 80 80   .0tabl..........
         8218  +|   2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65   ....>.....0numbe
         8219  +|   2848: 72 03 06 01 01 05 01 02 6f 66 02 06 04 0d 13 8c   r.......of......
         8220  +|   2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02   .......,........
         8221  +|   2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03   0n..............
         8222  +|   2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03   .<.....0fts4aux.
         8223  +|   2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00   ....is........*.
         8224  +|   2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88   .....$..........
         8225  +|   2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80   ................
         8226  +|   2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77   ......>.....4row
         8227  +|   2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b   s.......ther....
         8228  +|   2992: 15 88 80 80 80 80 10 03 00 3e 10 00 00 11 02 01   .........>......
         8229  +|   3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80   ...4betw........
         8230  +|   3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77   .....<......3row
         8231  +|   3040: 02 06 01 01 05 01 03 74 68 65 02 08 05 0a 1b 88   .......the......
         8232  +|   3056: 80 80 80 80 0e 03 05 0c 00 00 00 16 01 01 02 04   ................
         8233  +|   3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08   3are.....bet....
         8234  +|   3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32   .........<.....2
         8235  +|   3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06   th........3and..
         8236  +|   3120: 04 01 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16   ...........<....
         8237  +|   3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01   .2in.......ro...
         8238  +|   3152: 01 05 04 09 18 88 80 80 80 80 0b 03 00 36 00 00   .............6..
         8239  +|   3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02   ....2ar.....be..
         8240  +|   3184: 04 05 07 1b 88 80 bf 80 80 0a 03 00 3c 00 00 00   ............<...
         8241  +|   3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02   ..1t........2an.
         8242  +|   3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00   ..............8.
         8243  +|   3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06   ....1n.......r..
         8244  +|   3248: 01 01 05 03 08 17 88 80 80 80 80 08 03 00 34 00   ..............4.
         8245  +|   3264: 01 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01   ....1b.....i....
         8246  +|   3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00   ............8...
         8247  +|   3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01   ...12.....a.....
         8248  +|   3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00   ............<...
         8249  +|   3328: 16 06 30 74 68 65 72 65 02 12 02 00 02 31 31 02   ..0there.....11.
         8250  +|   3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00   ..............0.
         8251  +|   3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 71 01 07   .......0the..q..
         8252  +|   3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01   ..........>.....
         8253  +|   3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73   ...0n.......rows
         8254  +|   3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 00   .............<..
         8255  +|   3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02   ...0between.....
         8256  +|   3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a   in.............:
         8257  +|   3456: 08 f0 00 15 04 30 61 6e 64 02 06 01 01 02 02 02   .....0and.......
         8258  +|   3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00   re..............
         8259  +|   3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32   4.....01.......2
         8260  +|   3504: 02 02 07 04 08 08 84 80 80 80 80 12 03 00 16 00   ................
         8261  +|   3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00   .............<..
         8262  +|   3536: 00 16 05 34 74 61 62 6c 01 06 01 01 05 02 03 65   ...4tabl.......e
         8263  +|   3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c   rm.............<
         8264  +|   3568: 00 00 00 16 05 34 65 61 63 68 01 02 03 01 04 70   .....4each.....p
         8265  +|   3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03   res.............
         8266  +|   3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02   .:.....3ter.....
         8267  +|   3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e   he..............
         8268  +|   3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01   ..<.....3pre....
         8269  +|   3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80   .tab............
         8270  +|   3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 02   ....:.....3for..
         8271  +|   3680: 02 02 02 74 73 01 06 01 01 04 03 f8 1b 84 80 80   ...ts...........
         8272  +|   3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06   .....<.....2th..
         8273  +|   3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84   .....3eac.......
         8274  +|   3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61   .......6.....2ta
         8275  +|   3744: 01 06 01 01 05 02 01 65 00 02 04 04 09 19 84 80   .......e........
         8276  +|   3760: 80 80 80 0a 03 10 38 00 00 00 14 03 32 69 6e 01   ......8.....2in.
         8277  +|   3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80   ......pr........
         8278  +|   3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01   ......6.....2fo.
         8279  +|   3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80   ....t...........
         8280  +|   3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04   .....<.....1t...
         8281  +|   3840: 01 00 03 04 00 03 32 65 61 01 02 03 04 0a 17 84   ......2ea.......
         8282  +|   3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01   .......4.....1i.
         8283  +|   3872: 06 01 01 02 de 01 70 01 02 05 04 08 18 84 80 80   ......p.........
         8284  +|   3888: 80 80 06 03 00 36 00 00 00 13 02 31 65 01 02 03   .....6.....1e...
         8285  +|   3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80   ..f.............
         8286  +|   3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01   ....<.....0term.
         8287  +|   3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80   ....he..........
         8288  +|   3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62   ............0tab
         8289  +|   3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03   le..............
         8290  +|   3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74   .0......0present
         8291  +|   4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00   .............<..
         8292  +|   4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e   ...0fts.......in
         8293  +|   4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a   ...............:
         8294  +|   4048: 00 00 00 15 05 30 65 61 63 68 00 f2 03 01 03 66   .....0each.....f
         8295  +|   4064: 6f 72 01 02 02 04 09 06 01 03 00 12 03 0b 0f 00   or..............
         8296  +|   4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04   ................
         8297  +| page 3 offset 8192
         8298  +|      0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1   ....2.O.........
         8299  +|     16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f c1 0f a9 0f a0   ................
         8300  +|     32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f   .........x.q.h._
         8301  +|     48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13   .V.M.A.8./.&....
         8302  +|     64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d7 0e cd   ................
         8303  +|     80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 96 0e 8e 0e 85   ................
         8304  +|   3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08   ................
         8305  +|   3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e   .....4t ......4.
         8306  +|   3680: 09 04 01 12 34 03 33 74 68 1c 08 04 01 10 01 03   ....4.3th.......
         8307  +|   3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10   3n.......2w.....
         8308  +|   3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04   ..2t.......2n...
         8309  +|   3728: 01 0e 01 03 32 12 08 04 01 0f f1 03 31 74 10 08   ....2.......1t..
         8310  +|   3744: 04 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 30 fc   .....1n.......0.
         8311  +|   3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03   ......0th.......
         8312  +|   3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01   0t.......0nu....
         8313  +|   3792: 10 01 03 30 6e 04 06 04 01 0c 01 05 52 08 04 01   ...0n.......R...
         8314  +|   3808: 10 01 02 34 72 22 07 04 01 0e 01 02 34 20 08 04   ...4r.......4 ..
         8315  +|   3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72   ....3r.......3ar
         8316  +|   3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 b3 02   .......2t.......
         8317  +|   3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01   2i.......2ar....
         8318  +|   3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08   ...1t.......1n..
         8319  +|   3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32   .....1b.......12
         8320  +|   3904: 0e 0b 04 01 16 01 02 30 74 68 65 72 0c 08 04 01   .......0ther....
         8321  +|   3920: 10 01 02 30 74 0a 08 04 01 10 01 02 30 6e 08 08   ...0t.......0n..
         8322  +|   3936: 04 01 10 01 02 30 62 06 09 04 01 10 01 02 30 61   .....0b.......0a
         8323  +|   3952: 04 06 04 01 0c 01 02 02 07 04 09 10 01 34 74 22   .............4t.
         8324  +|   3968: 06 04 09 0e 01 34 20 08 04 09 12 01 33 74 65 1e   .....4 .....3te.
         8325  +|   3984: 07 04 09 10 01 33 70 1c 07 f4 09 11 01 33 66 1a   .....3p......3f.
         8326  +|   4000: 08 04 09 12 01 32 74 68 18 07 04 09 10 01 32 e4   .....2th......2.
         8327  +|   4016: 16 07 04 09 10 01 32 69 14 07 04 09 10 01 32 66   ......2i......2f
         8328  +|   4032: 12 07 04 09 10 01 31 74 10 07 04 09 10 01 31 69   ......1t......1i
         8329  +|   4048: 0e 06 04 09 0e 01 31 0c 08 04 09 12 01 30 74 65   ......1......0te
         8330  +|   4064: 0a 07 04 09 10 01 30 74 08 00 00 00 00 00 00 00   ......0t........
         8331  +| page 4 offset 12288
         8332  +|   4064: 00 00 00 00 00 00 00 00 00 00 00 05 03 03 00 10   ................
         8333  +|   4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04   ................
         8334  +| page 5 offset 16384
         8335  +|      0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00   ................
         8336  +|   4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70   ...............p
         8337  +|   4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04   gsz.....version.
         8338  +| page 6 offset 20480
         8339  +|   4080: 00 00 23 03 02 01 03 03 02 00 00 00 00 00 00 00   ..#.............
         8340  +| end crash-5a5acd0ab42d31.db
         8341  +}]} {}
         8342  +
         8343  +do_execsql_test 58.1 {
         8344  +  SELECT * FROM t1('t*');
         8345  +} {{} {} {} {} {} {}}
         8346  +
         8347  +#-------------------------------------------------------------------------
         8348  +do_test 59.0 {
         8349  +  sqlite3 db {}
         8350  +  db deserialize [decode_hexdb {
         8351  +.open --hexdb
         8352  +| size 32768 pagesize 4096 filename crash-96b136358d01ec.db
         8353  +| page 1 offset 0
         8354  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         8355  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
         8356  +|     96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36   ...............6
         8357  +|    112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00   ...k............
         8358  +|   3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74   .........1tablet
         8359  +|   3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45   2t2.CREATE TABLE
         8360  +|   3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61    t2(x)V.......ta
         8361  +|   3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63   blet1_configt1_c
         8362  +|   3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42   onfig.CREATE TAB
         8363  +|   3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b   LE 't1_config'(k
         8364  +|   3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29    PRIMARY KEY, v)
         8365  +|   3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06    WITHOUT ROWID[.
         8366  +|   3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64   ..!!...tablet1_d
         8367  +|   3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65   ocsizet1_docsize
         8368  +|   3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74   .CREATE TABLE 't
         8369  +|   3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e   1_docsize'(id IN
         8370  +|   3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45   TEGER PRIMARY KE
         8371  +|   3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21   Y, sz BLOB)^...!
         8372  +|   3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74   !...tablet1_cont
         8373  +|   3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52   entt1_content.CR
         8374  +|   3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63   EATE TABLE 't1_c
         8375  +|   3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47   ontent'(id INTEG
         8376  +|   3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20   ER PRIMARY KEY, 
         8377  +|   3776: 63 30 2c 20 63 31 2c d6 63 32 29 69 04 07 17 19   c0, c1,.c2)i....
         8378  +|   3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74   ...-tablet1_idxt
         8379  +|   3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42   1_idx.CREATE TAB
         8380  +|   3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69   LE 't1_idx'(segi
         8381  +|   3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50   d, term, pgno, P
         8382  +|   3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64   RIMARY KEY(segid
         8383  +|   3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54   , term)) WITHOUT
         8384  +|   3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74    ROWIDU........t
         8385  +|   3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61   ablet1_datat1_da
         8386  +|   3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20   ta.CREATE TABLE 
         8387  +|   3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54   't1_data'(id INT
         8388  +|   3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59   EGER PRIMARY KEY
         8389  +|   3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06   , block BLOB)8..
         8390  +|   3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52   ...._tablet1t1CR
         8391  +|   4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42   EATE VIRTUAL TAB
         8392  +|   4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35   LE t1 USING fts5
         8393  +|   4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00   (a,b,c).........
         8394  +| page 3 offset 8192
         8395  +|      0: 0d 00 00 00 03 0c 93 ff 0f e6 0f ef 0c 94 00 00   ................
         8396  +|   3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18   .....J..........
         8397  +|   3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06   ...+.00.........
         8398  +|   3248: 1f 02 03 01 02 03 01 02 03 01 08 32 31 31 36 30   ...........21160
         8399  +|   3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 33 f1   609...........3.
         8400  +|   3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 03 01 02   ..........5.....
         8401  +|   3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d   ......0000000..=
         8402  +|   3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06   ........binary..
         8403  +|   3328: 01 02 02 03 06 01 01 f2 03 06 4e 02 02 03 06 01   ..........N.....
         8404  +|   3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8405  +|   3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         8406  +|   3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70   ............comp
         8407  +|   3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64   iler...........d
         8408  +|   3408: 62 73 74 61 74 07 02 03 01 02 13 01 02 03 02 04   bstat...........
         8409  +|   3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65   ebug...........e
         8410  +|   3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02   nable...........
         8411  +|   3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02   ................
         8412  +|   3472: 01 02 02 01 02 02 01 02 01 f1 02 02 01 02 02 01   ................
         8413  +|   3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02   ................
         8414  +|   3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02   ......xtension..
         8415  +|   3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03   .........fts4...
         8416  +|   3536: 01 02 03 01 02 03 04 01 25 0d 02 03 01 02 03 01   ........%.......
         8417  +|   3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03   ....gcc.........
         8418  +|   3568: 02 06 65 6f 70 6f 6c 79 0f f2 03 01 02 03 01 02   ..eopoly........
         8419  +|   3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02   ...json1........
         8420  +|   3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03   ...load.........
         8421  +|   3616: 00 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05   ..max...........
         8422  +|   3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04   emory...........
         8423  +|   3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e   sys5...........n
         8424  +|   3664: 6f 63 61 73 65 02 06 01 02 02 13 06 00 f2 02 03   ocase...........
         8425  +|   3680: 06 01 02 02 13 06 01 02 02 03 06 01 02 02 03 06   ................
         8426  +|   3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         8427  +|   3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8428  +|   3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02   ...omit.........
         8429  +|   3744: 01 0a 22 74 72 65 65 19 02 03 01 02 03 01 02 03   ...tree.........
         8430  +|   3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06   ..im............
         8431  +|   3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         8432  +|   3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8433  +|   3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         8434  +|   3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01   ..threadsafe....
         8435  +|   3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02   .......vtab.....
         8436  +|   3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01   ......x.........
         8437  +|   3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02   ................
         8438  +|   3888: 01 06 01 11 02 01 06 01 01 02 01 06 01 01 02 01   ................
         8439  +|   3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06   ................
         8440  +|   3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01   ................
         8441  +|   3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         8442  +|   3952: 02 01 06 01 01 01 f1 06 01 01 02 ad 06 01 01 02   ................
         8443  +|   3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01   ................
         8444  +|   3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06   ................
         8445  +|   4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01   ................
         8446  +|   4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         8447  +|   4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c   ................
         8448  +|   4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f   .D...G..........
         8449  +|   4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f   D..@.......$Z$$.
         8450  +|   4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01   ...$............
         8451  +| page 4 offset 12288
         8452  +|      0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
         8453  +|   4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02   ................
         8454  +| page 5 offset 16384
         8455  +|      0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
         8456  +|   3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f   ...........$..%.
         8457  +|   3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49   .THREADSAFE=0XBI
         8458  +|   3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41   NARY.#..%..THREA
         8459  +|   3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f   DSAFE=0XNOCASE..
         8460  +|   3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d   ..%..THREADSAFE=
         8461  +|   3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 45 ed   0XRTRIM.!..3..E.
         8462  +|   3168: 49 54 20 4c 4f 41 44 21 45 58 54 45 4e 53 49 4f   IT LOAD!EXTENSIO
         8463  +|   3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f   NXBINARY. ..3..O
         8464  +|   3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 5a 29   MIT LOAD EXTENZ)
         8465  +|   3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17   ONXNOCASE....3..
         8466  +|   3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53   OMYT LOAD EXTENS
         8467  +|   3248: 49 4f 4e 58 52 54 56 a9 4d 1f 1e 05 00 33 0f 19   IONXRTV.M....3..
         8468  +|   3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30   MAX MEMORY=50000
         8469  +|   3280: 30 30 30 57 42 49 4e 31 52 59 1f 1d 05 00 33 0f   000WBIN1RY....3.
         8470  +|   3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30   .MAX MEMORY=5000
         8471  +|   3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 32   0000XNOCASE....2
         8472  +|   3328: 0f 17 4e 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30   ..NAX MEMORY-500
         8473  +|   3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25   00000XRTRIM....%
         8474  +|   3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42   ..ENABLE RTREEXB
         8475  +|   3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         8476  +|   3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17   LE RTREEYNOCASE.
         8477  +|   3408: 19 66 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52   .f.%..ENABLE RTR
         8478  +|   3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45   EEXRTRIM....)..E
         8479  +|   3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49   NABLE MEMSYS5XBI
         8480  +|   3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         8481  +|   3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45   E MEMSYS5XNOCASE
         8482  +|   3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45   ....)..ENABLE ME
         8483  +|   3504: 4d 53 59 76 35 58 52 54 52 49 4d 18 15 05 10 25   MSYv5XRTRIM....%
         8484  +|   3520: 0f 19 45 4e 40 42 4c 45 20 4a 53 4f 4e 31 58 42   ..EN@BLE JSON1XB
         8485  +|   3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         8486  +|   3552: 4c 45 20 4a 53 4f 4e 32 58 4e 4f 43 41 53 45 17   LE JSON2XNOCASE.
         8487  +|   3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f   ...%..ENABLE JSO
         8488  +|   3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45   N1XRTRIM....)..E
         8489  +|   3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49   NABLE GEOPOLYXBI
         8490  +|   3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 5f 81 42 4c   NARY....)..E_.BL
         8491  +|   3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45   E GEOPOLYXNOCQSE
         8492  +|   3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45   ....)..ENABLE GE
         8493  +|   3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23   OPOLYXRTRIM....#
         8494  +|   3680: 0f 1a 45 4e 41 42 4c 45 20 56 54 43 35 58 42 49   ..ENABLE VTC5XBI
         8495  +|   3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c   NARY....#..ENABL
         8496  +|   3712: 45 20 46 54 53 35 48 4e 4f 43 41 53 45 16 1d 05   E FTS5HNOCASE...
         8497  +|   3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58   .#..ENABLE FTS5X
         8498  +|   3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42   RTRIM....#..ENAB
         8499  +|   3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b   LE FUS4XBINARY..
         8500  +|   3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34   ..#..ENABLE FTS4
         8501  +|   3792: 57 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e   WNOCASE....#..EN
         8502  +|   3808: 41 42 4c 45 20 46 54 53 34 05 52 54 52 49 4d 1e   ABLE FTS4.RTRIM.
         8503  +|   3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8504  +|   3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e   TAT VTABXBINARY.
         8505  +|   3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8506  +|   3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d   TAT VTABXNOCASE.
         8507  +|   3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8508  +|   3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06   TAT VTABXRTRIM..
         8509  +|   3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52   .....DEBUGXBINAR
         8510  +|   3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f   Y.......DEBUGXNO
         8511  +|   3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47   CASE.......DEBUG
         8512  +|   3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d   XRTRIM'...C..COM
         8513  +|   3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20   PILER=gcc-5.4.0 
         8514  +|   4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27   20160609XBINARY'
         8515  +|   4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3f 87   ...C..COMPILER?.
         8516  +|   4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30   cc-5.4.0 2016060
         8517  +|   4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43   9XNOCASE&...C..C
         8518  +|   4064: 45 0d 60 59 4c 45 52 3d 67 63 63 2d 35 2e 34 00   E.`YLER=gcc-5.4.
         8519  +| page 6 offset 20480
         8520  +|   3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01   .$.......#......
         8521  +|   3824: 06 22 03 01 12 02 01 01 06 21 03 00 12 03 01 01   .........!......
         8522  +|   3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 02 01   . ..............
         8523  +|   3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01   ................
         8524  +|   3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01   ................
         8525  +|   3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01   ................
         8526  +|   3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01   ................
         8527  +|   3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01   ................
         8528  +|   3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01   ................
         8529  +|   3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01   ................
         8530  +|   3968: 06 00 03 00 12 02 01 01 06 0f 03 00 12 02 01 01   ................
         8531  +|   3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01   ................
         8532  +|   4000: 06 0c 03 00 12 02 01 01 06 0b 03 10 12 02 01 01   ................
         8533  +|   4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01   ................
         8534  +|   4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01   ................
         8535  +|   4048: 07 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01   ................
         8536  +|   4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01   ................
         8537  +|   4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01   ................
         8538  +| page 7 offset 24576
         8539  +|      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
         8540  +|   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
         8541  +| page 8 offset 28672
         8542  +|   4048: 00 00 00 00 00 00 5d 03 02 2b 69 6e 74 00 00 00   ......]..+int...
         8543  +| end crash-96b136358d01ec.db
         8544  +}]} {}
         8545  +
         8546  +do_catchsql_test 59.1 {
         8547  +  SELECT (matchinfo(591,t1)) FROM t1 WHERE t1 MATCH 'e*eŸ'
         8548  +} {0 {}}
         8549  +
         8550  +#-------------------------------------------------------------------------
         8551  +do_test 60.0 {
         8552  +  sqlite3 db {}
         8553  +  db deserialize [decode_hexdb {
         8554  +.open --hexdb
         8555  +| size 32768 pagesize 4096 filename crash-c77b90b929dc92.db
         8556  +| page 1 offset 0
         8557  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         8558  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
         8559  +|     96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36   ...............6
         8560  +|    112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00   ...k............
         8561  +|   3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74   .........1tablet
         8562  +|   3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45   2t2.CREATE TABLE
         8563  +|   3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61    t2(x)V.......ta
         8564  +|   3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63   blet1_configt1_c
         8565  +|   3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42   onfig.CREATE TAB
         8566  +|   3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b   LE 't1_config'(k
         8567  +|   3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29    PRIMARY KEY, v)
         8568  +|   3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06    WITHOUT ROWID[.
         8569  +|   3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64   ..!!...tablet1_d
         8570  +|   3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65   ocsizet1_docsize
         8571  +|   3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74   .CREATE TABLE 't
         8572  +|   3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e   1_docsize'(id IN
         8573  +|   3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45   TEGER PRIMARY KE
         8574  +|   3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21   Y, sz BLOB)^...!
         8575  +|   3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74   !...tablet1_cont
         8576  +|   3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52   entt1_content.CR
         8577  +|   3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63   EATE TABLE 't1_c
         8578  +|   3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47   ontent'(id INTEG
         8579  +|   3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20   ER PRIMARY KEY, 
         8580  +|   3776: 63 30 2c 20 63 31 2c d6 63 32 29 69 04 07 17 19   c0, c1,.c2)i....
         8581  +|   3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74   ...-tablet1_idxt
         8582  +|   3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42   1_idx.CREATE TAB
         8583  +|   3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69   LE 't1_idx'(segi
         8584  +|   3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50   d, term, pgno, P
         8585  +|   3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64   RIMARY KEY(segid
         8586  +|   3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54   , term)) WITHOUT
         8587  +|   3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74    ROWIDU........t
         8588  +|   3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61   ablet1_datat1_da
         8589  +|   3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20   ta.CREATE TABLE 
         8590  +|   3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54   't1_data'(id INT
         8591  +|   3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59   EGER PRIMARY KEY
         8592  +|   3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06   , block BLOB)8..
         8593  +|   3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52   ...._tablet1t1CR
         8594  +|   4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42   EATE VIRTUAL TAB
         8595  +|   4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35   LE t1 USING fts5
         8596  +|   4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00   (a,b,c).........
         8597  +| page 3 offset 8192
         8598  +|      0: 0d 00 00 00 03 0c 93 ff 0f e6 0f ef 0c 94 00 00   ................
         8599  +|   3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18   .....J..........
         8600  +|   3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06   ...+.00.........
         8601  +|   3248: 1f 02 03 01 02 03 01 02 03 01 08 32 31 31 36 30   ...........21160
         8602  +|   3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 33 f1   609...........3.
         8603  +|   3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 03 01 02   ..........5.....
         8604  +|   3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d   ......0000000..=
         8605  +|   3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06   ........binary..
         8606  +|   3328: 01 02 02 03 06 01 01 f2 03 06 4e 02 02 03 06 01   ..........N.....
         8607  +|   3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8608  +|   3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         8609  +|   3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70   ............comp
         8610  +|   3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64   iler...........d
         8611  +|   3408: 62 73 74 61 74 07 02 03 01 02 13 01 02 03 02 04   bstat...........
         8612  +|   3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65   ebug...........e
         8613  +|   3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02   nable...........
         8614  +|   3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02   ................
         8615  +|   3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01   ................
         8616  +|   3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02   ................
         8617  +|   3504: 02 01 02 02 02 08 76 b4 65 6e 73 69 6f 6e 1f 02   ......v.ension..
         8618  +|   3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03   .........fts4...
         8619  +|   3536: 01 02 03 01 02 03 04 01 25 0d 02 03 01 02 03 01   ........%.......
         8620  +|   3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03   ....gcc.........
         8621  +|   3568: 02 06 65 6f 70 6f 6c 79 0f f2 03 01 02 03 01 02   ..eopoly........
         8622  +|   3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02   ...json1........
         8623  +|   3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03   ...load.........
         8624  +|   3616: 00 03 6d 61 78 1c 02 0c 01 02 02 01 02 02 02 05   ..max...........
         8625  +|   3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04   emory...........
         8626  +|   3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e   sys5...........n
         8627  +|   3664: 6f 63 61 73 65 02 06 01 02 02 13 06 00 f2 02 03   ocase...........
         8628  +|   3680: 06 01 12 02 13 06 01 02 02 03 06 01 02 02 03 06   ................
         8629  +|   3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         8630  +|   3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8631  +|   3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02   ...omit.........
         8632  +|   3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03   ..rtree.........
         8633  +|   3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06   ..im............
         8634  +|   3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         8635  +|   3792: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8636  +|   3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         8637  +|   3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01   ..threadsafe....
         8638  +|   3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02   .......vtab.....
         8639  +|   3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01   ......x.........
         8640  +|   3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02   ................
         8641  +|   3888: 01 06 01 11 02 01 06 01 01 02 01 06 01 01 02 01   ................
         8642  +|   3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06   ................
         8643  +|   3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01   ................
         8644  +|   3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         8645  +|   3952: 02 01 06 01 01 01 f1 06 01 01 02 ad 06 01 01 02   ................
         8646  +|   3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01   ................
         8647  +|   3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06   ................
         8648  +|   4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01   ................
         8649  +|   4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         8650  +|   4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c   ................
         8651  +|   4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f   .D...G..........
         8652  +|   4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f   D..@.......$Z$$.
         8653  +|   4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01   ...$............
         8654  +| page 4 offset 12288
         8655  +|      0: 0a 00 00 00 01 0f 00 00 00 00 00 00 00 00 00 00   ................
         8656  +|   4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02   ................
         8657  +| page 5 offset 16384
         8658  +|      0: 0d 00 00 00 24 0c 0a 00 0f 00 00 00 00 00 00 00   ....$...........
         8659  +|   3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f   ...........$..%.
         8660  +|   3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49   .THREADSAFE=0XBI
         8661  +|   3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41   NARY.#..%..THREA
         8662  +|   3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f   DSAFE=0XNOCASE..
         8663  +|   3136: 05 00 25 0f 17 54 48 52 45 41 44 43 41 46 45 3d   ..%..THREADCAFE=
         8664  +|   3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d   0XRTRIM.!..3..OM
         8665  +|   3168: 49 54 20 4b 4f 41 44 21 45 58 54 45 4e 53 49 4f   IT KOAD!EXTENSIO
         8666  +|   3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f   NXBINARY. ..3..O
         8667  +|   3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49   MIT LOAD EXTENSI
         8668  +|   3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17   ONXNOCASE....3..
         8669  +|   3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53   OMYT LOAD EXTENS
         8670  +|   3248: 49 4f 4e 58 52 54 56 a9 4d 1f 1e 05 00 33 0f 19   IONXRTV.M....3..
         8671  +|   3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30   MAX MEMORY=50000
         8672  +|   3280: 30 30 30 57 42 49 4e 31 52 59 1f 1d 05 00 33 0f   000WBIN1RY....3.
         8673  +|   3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30   .MAX MEMORY=5000
         8674  +|   3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 32   0000XNOCASE....2
         8675  +|   3328: 0f 17 4e 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30   ..NAX MEMORY-500
         8676  +|   3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25   00000XRTRIM....%
         8677  +|   3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42   ..ENABLE RTREEXB
         8678  +|   3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         8679  +|   3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17   LE RTREEYNOCASE.
         8680  +|   3408: 19 66 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52   .f.%..ENABLE RTR
         8681  +|   3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45   EEXRTRIM....)..E
         8682  +|   3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49   NABLE MEMSYS5XBI
         8683  +|   3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         8684  +|   3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45   E MEMSYS5XNOCASE
         8685  +|   3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45   ....)..ENABLE ME
         8686  +|   3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 10 25   MSYS5XRTRIM....%
         8687  +|   3520: 0f 19 45 4e 40 42 4c 45 20 4a 53 4f 4e 31 58 42   ..EN@BLE JSON1XB
         8688  +|   3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         8689  +|   3552: 4c 45 20 4a 53 4f 4e 32 58 4e 4f 43 41 53 45 17   LE JSON2XNOCASE.
         8690  +|   3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f   ...%..ENABLE JSO
         8691  +|   3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45   N1XRTRIM....)..E
         8692  +|   3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49   NABLE GEOPOLYXBI
         8693  +|   3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4f 81 42 4c   NARY....)..EO.BL
         8694  +|   3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45   E GEOPOLYXNOCQSE
         8695  +|   3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45   ....)..ENABLE GE
         8696  +|   3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23   OPOLYXRTRIM....#
         8697  +|   3680: 0f 1a 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49   ..ENABLE FTS5XBI
         8698  +|   3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c   NARY....#..ENABL
         8699  +|   3712: 45 20 46 54 53 35 48 4e 4f 43 41 53 45 16 1d 05   E FTS5HNOCASE...
         8700  +|   3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58   .#..ENABLE FTS5X
         8701  +|   3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42   RTRIM....#..ENAB
         8702  +|   3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b   LE FUS4XBINARY..
         8703  +|   3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34   ..#..ENABLE FTS4
         8704  +|   3792: 57 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e   WNOCASE....#..EN
         8705  +|   3808: 41 42 4c 45 20 46 54 53 34 05 52 54 52 49 4d 1e   ABLE FTS4.RTRIM.
         8706  +|   3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8707  +|   3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e   TAT VTABXBINARY.
         8708  +|   3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8709  +|   3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d   TAT VTABXNOCASE.
         8710  +|   3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8711  +|   3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06   TAT VTABXRTRIM..
         8712  +|   3920: 05 00 17 0f 19 44 45 42 55 47 58 42 8a 4e 41 52   .....DEBUGXB.NAR
         8713  +|   3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f   Y.......DEBUGXNO
         8714  +|   3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47   CASE.......DEBUG
         8715  +|   3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d   XRTRIM'...C..COM
         8716  +|   3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20   PILER=gcc-5.4.0 
         8717  +|   4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27   20160609XBINARY'
         8718  +|   4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3f 87   ...C..COMPILER?.
         8719  +|   4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30   cc-5.4.0 2016060
         8720  +|   4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43   9XNOCASE&...C..C
         8721  +|   4064: 45 0d 60 59 4c 45 52 3d 67 63 63 2d 35 2e 34 2e   E.`YLER=gcc-5.4.
         8722  +|   4080: 30 20 32 30 31 36 30 36 30 39 68 52 54 52 49 4d   0 20160609hRTRIM
         8723  +| page 6 offset 20480
         8724  +|      0: 0d 00 00 00 24 0e 00 00 00 00 00 00 00 00 00 00   ....$...........
         8725  +|   3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01   .$.......#......
         8726  +|   3824: 06 22 03 01 12 02 01 01 06 21 03 00 12 03 01 01   .........!......
         8727  +|   3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 02 01   . ..............
         8728  +|   3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01   ................
         8729  +|   3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01   ................
         8730  +|   3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01   ................
         8731  +|   3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01   ................
         8732  +|   3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01   ................
         8733  +|   3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01   ................
         8734  +|   3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01   ................
         8735  +|   3968: 06 00 03 00 12 02 01 01 06 0f 03 00 12 02 01 01   ................
         8736  +|   3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01   ................
         8737  +|   4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01   ................
         8738  +|   4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01   ................
         8739  +|   4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01   ................
         8740  +|   4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01   ................
         8741  +|   4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01   ................
         8742  +|   4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01   ................
         8743  +| page 7 offset 24576
         8744  +|      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
         8745  +|   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
         8746  +| page 8 offset 28672
         8747  +|   4048: 00 00 00 00 00 00 5d 03 00 00 00 00 00 00 00 00   ......].........
         8748  +| end crash-c77b90b929dc92.db
         8749  +}]} {}
         8750  +
         8751  +
         8752  +do_catchsql_test 60.2 {
         8753  +  SELECT (matchinfo(t1,591)) FROM t1 WHERE t1 MATCH 'e*eŸ'
         8754  +} {0 {}}
         8755  +
         8756  +#-------------------------------------------------------------------------
         8757  +do_test 61.0 {
         8758  +  sqlite3 db {}
         8759  +  db deserialize [decode_hexdb {
         8760  +.open --hexdb
         8761  +| size 28672 pagesize 4096 filename crash-e5fa281edabddf.db
         8762  +| page 1 offset 0
         8763  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         8764  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
         8765  +|     96: 00 00 00 00 0d 0f c7 00 06 0d b6 00 0f 8d 0f 36   ...............6
         8766  +|    112: 0e cb 0e 6b 0e 0e 0d b6 00 00 00 00 00 00 00 00   ...k............
         8767  +|   3504: 00 00 00 00 00 00 56 07 06 17 1f 1f 01 7d 74 61   ......V.......ta
         8768  +|   3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63   blet1_configt1_c
         8769  +|   3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42   onfig.CREATE TAB
         8770  +|   3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b   LE 't1_config'(k
         8771  +|   3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29    PRIMARY KEY, v)
         8772  +|   3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06    WITHOUT ROWID[.
         8773  +|   3600: 07 17 21 21 01 81 01 74 51 62 6c 65 74 31 5f 64   ..!!...tQblet1_d
         8774  +|   3616: 6f 63 73 69 7a 65 74 31 5f 63 6f 63 73 69 7a 65   ocsizet1_cocsize
         8775  +|   3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74   .CREATE TABLE 't
         8776  +|   3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e   1_docsize'(id IN
         8777  +|   3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45   TEGER PRIMARY KE
         8778  +|   3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21   Y, sz BLOB)^...!
         8779  +|   3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74   !...tablet1_cont
         8780  +|   3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52   entt1_content.CR
         8781  +|   3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63   EATE TABLE 't1_c
         8782  +|   3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47   ontent'(id INTEG
         8783  +|   3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20   ER PRIMARY KEY, 
         8784  +|   3776: 63 30 2c 20 63 31 2c 20 63 32 29 69 04 07 17 19   c0, c1, c2)i....
         8785  +|   3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74   ...-tablet1_idxt
         8786  +|   3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42   1_idx.CREATE TAB
         8787  +|   3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69   LE 't1_idx'(segi
         8788  +|   3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50   d, term, pgno, P
         8789  +|   3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64   RIMARY KEY(segid
         8790  +|   3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54   , term)) WITHOUT
         8791  +|   3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74    ROWIDU........t
         8792  +|   3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61   ablet1_datat1_da
         8793  +|   3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20   ta.CREATE TABLE 
         8794  +|   3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54   't1_data'(id INT
         8795  +|   3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59   EGER PRIMARY KEY
         8796  +|   3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06   , block BLOB)8..
         8797  +|   3984: 17 11 11 08 5f 74 61 62 6c 65 74 ea 74 31 43 52   ...._tablet.t1CR
         8798  +|   4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42   EATE VIRTUAL TAB
         8799  +|   4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35   LE t1 USING fts5
         8800  +|   4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00   (a,b,c).........
         8801  +| page 3 offset 8192
         8802  +|      0: 0d 00 00 00 03 0c 94 00 0f e6 0f ef 0c 94 00 00   ................
         8803  +|   3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18   .....J..........
         8804  +|   3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06   ...+.00.........
         8805  +|   3248: 1f 02 13 01 02 03 01 02 03 01 08 32 30 31 36 30   ...........20160
         8806  +|   3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 34 01   609...........4.
         8807  +|   3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 04 01 02   ..........5.....
         8808  +|   3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 04   ......0000000...
         8809  +|   3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06   ........binary..
         8810  +|   3328: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         8811  +|   3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8812  +|   3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         8813  +|   3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70   ............comp
         8814  +|   3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64   iler...........d
         8815  +|   3408: 62 73 74 61 74 07 02 03 01 02 03 01 02 03 02 04   bstat...........
         8816  +|   3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 06 65   ebug...........e
         8817  +|   3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02   nable...........
         8818  +|   3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02   ................
         8819  +|   3472: 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01   ................
         8820  +|   3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02   ................
         8821  +|   3504: 02 01 02 02 02 08 78 74 65 6e 73 69 6f 6e 1f 02   ......xtension..
         8822  +|   3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03   .........fts4...
         8823  +|   3536: 01 02 03 01 02 03 04 01 35 0d 02 03 01 02 03 01   ........5.......
         8824  +|   3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03   ....gcc.........
         8825  +|   3568: 02 06 65 6f 70 6f 6c 79 10 02 03 01 02 03 01 02   ..eopoly........
         8826  +|   3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02   ...json1........
         8827  +|   3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03   ...load.........
         8828  +|   3616: 01 03 6d 61 78 1c 02 02 01 02 02 01 02 02 02 05   ..max...........
         8829  +|   3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04   emory...........
         8830  +|   3648: 73 79 73 35 16 02 03 01 02 03 11 02 03 01 06 6e   sys5...........n
         8831  +|   3664: 6f 63 61 73 65 02 06 01 02 02 03 06 01 02 02 03   ocase...........
         8832  +|   3680: 06 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06   ................
         8833  +|   3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         8834  +|   3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         8835  +|   3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02   ...omit.........
         8836  +|   3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03   ..rtree.........
         8837  +|   3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06   ..im............
         8838  +|   3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         8839  +|   3792: 02 02 03 06 01 02 02 03 06 01 02 01 13 05 01 02   ................
         8840  +|   3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         8841  +|   3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01   ..threadsafe....
         8842  +|   3840: 02 02 01 01 02 01 04 76 74 61 62 07 02 04 01 02   .......vtab.....
         8843  +|   3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01   ......x.........
         8844  +|   3872: 02 0e 16 01 01 02 01 06 01 01 02 01 06 01 02 02   ................
         8845  +|   3888: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01   ................
         8846  +|   3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06   ................
         8847  +|   3920: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01   ................
         8848  +|   3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         8849  +|   3952: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02   ................
         8850  +|   3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01   ................
         8851  +|   3984: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06   ................
         8852  +|   4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 07 01   ................
         8853  +|   4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         8854  +|   4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c   ................
         8855  +|   4048: 12 44 13 11 0f 47 13 0f 0c 0e 11 10 0f 0e 10 0f   .D...G..........
         8856  +|   4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f   D..@.......$Z$$.
         8857  +|   4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01   ...$............
         8858  +| page 4 offset 12288
         8859  +|      0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
         8860  +|   4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02   ................
         8861  +| page 5 offset 16384
         8862  +|      0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
         8863  +|   3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f   ...........$..%.
         8864  +|   3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49   .THREADSAFE=0XBI
         8865  +|   3104: 4e 41 52 59 18 e2 05 00 25 0f 19 54 48 52 45 41   NARY....%..THREA
         8866  +|   3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 22   DSAFE=0XNOCASE..
         8867  +|   3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 45 3d   ..%..THREADSAFE=
         8868  +|   3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d   0XRTRIM.!..3..OM
         8869  +|   3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49 4f   IT LOAD EXTENSIO
         8870  +|   3184: 4e 58 42 49 4e 40 52 59 1f 20 05 00 33 0f 19 4f   NXBIN@RY. ..3..O
         8871  +|   3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49   MIT LOAD EXTENSI
         8872  +|   3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17   ONXNOCASE....3..
         8873  +|   3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53   OMIT LOAD EXTENS
         8874  +|   3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19   IONXRTRIM....3..
         8875  +|   3264: 4d 41 58 20 4e 45 4d 4f 52 59 3d 35 30 30 30 30   MAX NEMORY=50000
         8876  +|   3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f   000XBINARY....3.
         8877  +|   3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 45 30 30 30   .MAX MEMORY=E000
         8878  +|   3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33   0000XNOCASE....3
         8879  +|   3328: 0f 17 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30   ..MAX MEMORY=500
         8880  +|   3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25   00000XRTRIM....%
         8881  +|   3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42   ..ENABLE RTREEXB
         8882  +|   3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         8883  +|   3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17   LE RTREEXNOCASE.
         8884  +|   3408: 19 05 00 25 0f 17 45 4e 41 42 4c 45 20 20 54 52   ...%..ENABLE  TR
         8885  +|   3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45   EEXRTRIM....)..E
         8886  +|   3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49   NABLE MEMSYS5XBI
         8887  +|   3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         8888  +|   3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45   E MEMSYS5XNOCASE
         8889  +|   3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45   ....)..ENABLE ME
         8890  +|   3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 00 25   MSYS5XRTRIM....%
         8891  +|   3520: 0f 19 45 4e 41 42 4c 45 20 4a 53 4f 4e 31 58 42   ..ENABLE JSON1XB
         8892  +|   3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         8893  +|   3552: 4c 45 20 4a 53 4f 4e 31 58 4e 4f 43 41 53 45 17   LE JSON1XNOCASE.
         8894  +|   3568: 13 05 00 25 0f 17 45 4e 41 42 4c 45 20 4a 53 4f   ...%..ENABLE JSO
         8895  +|   3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45   N1XRTRIM....)..E
         8896  +|   3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49   NABLE GEOPOLYXBI
         8897  +|   3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         8898  +|   3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 41 53 45   E GEOPOLYXNOCASE
         8899  +|   3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45   ....)..ENABLE GE
         8900  +|   3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23   OPOLYXRTRIM....#
         8901  +|   3680: 0f 19 45 4e 41 42 4c 45 20 e5 54 53 35 58 42 49   ..ENABLE .TS5XBI
         8902  +|   3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4d   NARY....#..ENABM
         8903  +|   3712: 45 b5 46 54 53 35 58 4e 4f 43 41 53 45 16 0d 05   E.FTS5XNOCASE...
         8904  +|   3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58   .#..ENABLE FTS5X
         8905  +|   3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42   RTRIM....#..ENAB
         8906  +|   3760: 4c 45 20 46 54 53 34 58 42 b7 4e 41 52 59 17 0b   LE FTS4XB.NARY..
         8907  +|   3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34   ..#..ENABLE FTS4
         8908  +|   3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e   XNOCASE....#..EN
         8909  +|   3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e   ABLE FTS4XRTRIM.
         8910  +|   3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8911  +|   3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e   TAT VTABXBINARY.
         8912  +|   3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8913  +|   3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d   TAT VTABXNOCASE.
         8914  +|   3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         8915  +|   3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06   TAT VTABXRTRIM..
         8916  +|   3920: 05 00 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52   .....DEBUGXBINAR
         8917  +|   3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f   Y.......DEBUGXNO
         8918  +|   3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47   CASE.......DEBUG
         8919  +|   3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d   XRTRIM'...C..COM
         8920  +|   3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20   PILER=gcc-5.4.0 
         8921  +|   4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27   20160609XBINARY'
         8922  +|   4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67   ...C..COMPILER=g
         8923  +|   4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30   cc-5.4.0 2016060
         8924  +|   4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 53   9XNOCASE&...C..S
         8925  +|   4064: 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e   OMPILER=gcc-5.4.
         8926  +|   4080: 30 20 32 2f 31 00 00 00 00 00 00 00 00 00 00 00   0 2/1...........
         8927  +| page 6 offset 20480
         8928  +|   3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01   .$.......#......
         8929  +|   3824: 06 22 03 00 12 02 01 01 06 21 03 00 12 03 01 01   .........!......
         8930  +|   3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 01 01   . ..............
         8931  +|   3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01   ................
         8932  +|   3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01   ................
         8933  +|   3888: 06 1a 03 00 12 02 01 01 06 19 03 10 12 02 01 01   ................
         8934  +|   3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01   ................
         8935  +|   3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01   ................
         8936  +|   3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01   ................
         8937  +|   3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01   ................
         8938  +|   3968: 06 10 03 00 12 02 01 01 06 0f 03 00 12 02 01 01   ................
         8939  +|   3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01   ................
         8940  +|   4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01   ................
         8941  +|   4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01   ................
         8942  +|   4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01   ................
         8943  +|   4048: 06 06 03 00 12 01 01 01 06 05 03 01 12 01 01 01   ................
         8944  +|   4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01   ................
         8945  +|   4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01   ................
         8946  +| page 7 offset 24576
         8947  +|      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
         8948  +|   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
         8949  +| end crash-e5fa281edabddf.db
         8950  +}]} {}
         8951  +
         8952  +do_execsql_test 61.1 {
         8953  +  CREATE VIRTUAL TABLE t3 USING fts5vocab('t1'(),'col' );
         8954  +} 
         8955  +
         8956  +do_catchsql_test 62.2 {
         8957  +  SELECT * FROM t3 ORDER BY rowid;
         8958  +} {1 {database disk image is malformed}}
  8130   8959   
  8131   8960   
  8132   8961   sqlite3_fts5_may_be_corrupt 0
  8133   8962   finish_test
  8134   8963   

Added ext/misc/dbdata.c.

            1  +/*
            2  +** 2019-04-17
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +******************************************************************************
           12  +**
           13  +** This file contains an implementation of two eponymous virtual tables,
           14  +** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
           15  +** "sqlite_dbpage" eponymous virtual table be available.
           16  +**
           17  +** SQLITE_DBDATA:
           18  +**   sqlite_dbdata is used to extract data directly from a database b-tree
           19  +**   page and its associated overflow pages, bypassing the b-tree layer.
           20  +**   The table schema is equivalent to:
           21  +**
           22  +**     CREATE TABLE sqlite_dbdata(
           23  +**       pgno INTEGER,
           24  +**       cell INTEGER,
           25  +**       field INTEGER,
           26  +**       value ANY,
           27  +**       schema TEXT HIDDEN
           28  +**     );
           29  +**
           30  +**   IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
           31  +**   FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
           32  +**   "schema".
           33  +**
           34  +**   Each page of the database is inspected. If it cannot be interpreted as
           35  +**   a b-tree page, or if it is a b-tree page containing 0 entries, the
           36  +**   sqlite_dbdata table contains no rows for that page.  Otherwise, the
           37  +**   table contains one row for each field in the record associated with
           38  +**   each cell on the page. For intkey b-trees, the key value is stored in
           39  +**   field -1.
           40  +**
           41  +**   For example, for the database:
           42  +**
           43  +**     CREATE TABLE t1(a, b);     -- root page is page 2
           44  +**     INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
           45  +**     INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
           46  +**
           47  +**   the sqlite_dbdata table contains, as well as from entries related to 
           48  +**   page 1, content equivalent to:
           49  +**
           50  +**     INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
           51  +**         (2, 0, -1, 5     ),
           52  +**         (2, 0,  0, 'v'   ),
           53  +**         (2, 0,  1, 'five'),
           54  +**         (2, 1, -1, 10    ),
           55  +**         (2, 1,  0, 'x'   ),
           56  +**         (2, 1,  1, 'ten' );
           57  +**
           58  +**   If database corruption is encountered, this module does not report an
           59  +**   error. Instead, it attempts to extract as much data as possible and
           60  +**   ignores the corruption.
           61  +**
           62  +** SQLITE_DBPTR:
           63  +**   The sqlite_dbptr table has the following schema:
           64  +**
           65  +**     CREATE TABLE sqlite_dbptr(
           66  +**       pgno INTEGER,
           67  +**       child INTEGER,
           68  +**       schema TEXT HIDDEN
           69  +**     );
           70  +**
           71  +**   It contains one entry for each b-tree pointer between a parent and
           72  +**   child page in the database.
           73  +*/
           74  +#if !defined(SQLITEINT_H) 
           75  +#include "sqlite3ext.h"
           76  +
           77  +typedef unsigned char u8;
           78  +
           79  +#endif
           80  +SQLITE_EXTENSION_INIT1
           81  +#include <string.h>
           82  +#include <assert.h>
           83  +
           84  +#define DBDATA_PADDING_BYTES 100 
           85  +
           86  +typedef struct DbdataTable DbdataTable;
           87  +typedef struct DbdataCursor DbdataCursor;
           88  +
           89  +/* Cursor object */
           90  +struct DbdataCursor {
           91  +  sqlite3_vtab_cursor base;       /* Base class.  Must be first */
           92  +  sqlite3_stmt *pStmt;            /* For fetching database pages */
           93  +
           94  +  int iPgno;                      /* Current page number */
           95  +  u8 *aPage;                      /* Buffer containing page */
           96  +  int nPage;                      /* Size of aPage[] in bytes */
           97  +  int nCell;                      /* Number of cells on aPage[] */
           98  +  int iCell;                      /* Current cell number */
           99  +  int bOnePage;                   /* True to stop after one page */
          100  +  int szDb;
          101  +  sqlite3_int64 iRowid;
          102  +
          103  +  /* Only for the sqlite_dbdata table */
          104  +  u8 *pRec;                       /* Buffer containing current record */
          105  +  int nRec;                       /* Size of pRec[] in bytes */
          106  +  int nHdr;                       /* Size of header in bytes */
          107  +  int iField;                     /* Current field number */
          108  +  u8 *pHdrPtr;
          109  +  u8 *pPtr;
          110  +  
          111  +  sqlite3_int64 iIntkey;          /* Integer key value */
          112  +};
          113  +
          114  +/* Table object */
          115  +struct DbdataTable {
          116  +  sqlite3_vtab base;              /* Base class.  Must be first */
          117  +  sqlite3 *db;                    /* The database connection */
          118  +  sqlite3_stmt *pStmt;            /* For fetching database pages */
          119  +  int bPtr;                       /* True for sqlite3_dbptr table */
          120  +};
          121  +
          122  +/* Column and schema definitions for sqlite_dbdata */
          123  +#define DBDATA_COLUMN_PGNO        0
          124  +#define DBDATA_COLUMN_CELL        1
          125  +#define DBDATA_COLUMN_FIELD       2
          126  +#define DBDATA_COLUMN_VALUE       3
          127  +#define DBDATA_COLUMN_SCHEMA      4
          128  +#define DBDATA_SCHEMA             \
          129  +      "CREATE TABLE x("           \
          130  +      "  pgno INTEGER,"           \
          131  +      "  cell INTEGER,"           \
          132  +      "  field INTEGER,"          \
          133  +      "  value ANY,"              \
          134  +      "  schema TEXT HIDDEN"      \
          135  +      ")"
          136  +
          137  +/* Column and schema definitions for sqlite_dbptr */
          138  +#define DBPTR_COLUMN_PGNO         0
          139  +#define DBPTR_COLUMN_CHILD        1
          140  +#define DBPTR_COLUMN_SCHEMA       2
          141  +#define DBPTR_SCHEMA              \
          142  +      "CREATE TABLE x("           \
          143  +      "  pgno INTEGER,"           \
          144  +      "  child INTEGER,"          \
          145  +      "  schema TEXT HIDDEN"      \
          146  +      ")"
          147  +
          148  +/*
          149  +** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual 
          150  +** table.
          151  +*/
          152  +static int dbdataConnect(
          153  +  sqlite3 *db,
          154  +  void *pAux,
          155  +  int argc, const char *const*argv,
          156  +  sqlite3_vtab **ppVtab,
          157  +  char **pzErr
          158  +){
          159  +  DbdataTable *pTab = 0;
          160  +  int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
          161  +
          162  +  if( rc==SQLITE_OK ){
          163  +    pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
          164  +    if( pTab==0 ){
          165  +      rc = SQLITE_NOMEM;
          166  +    }else{
          167  +      memset(pTab, 0, sizeof(DbdataTable));
          168  +      pTab->db = db;
          169  +      pTab->bPtr = (pAux!=0);
          170  +    }
          171  +  }
          172  +
          173  +  *ppVtab = (sqlite3_vtab*)pTab;
          174  +  return rc;
          175  +}
          176  +
          177  +/*
          178  +** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
          179  +*/
          180  +static int dbdataDisconnect(sqlite3_vtab *pVtab){
          181  +  DbdataTable *pTab = (DbdataTable*)pVtab;
          182  +  if( pTab ){
          183  +    sqlite3_finalize(pTab->pStmt);
          184  +    sqlite3_free(pVtab);
          185  +  }
          186  +  return SQLITE_OK;
          187  +}
          188  +
          189  +/*
          190  +** This function interprets two types of constraints:
          191  +**
          192  +**       schema=?
          193  +**       pgno=?
          194  +**
          195  +** If neither are present, idxNum is set to 0. If schema=? is present,
          196  +** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
          197  +** in idxNum is set.
          198  +**
          199  +** If both parameters are present, schema is in position 0 and pgno in
          200  +** position 1.
          201  +*/
          202  +static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
          203  +  DbdataTable *pTab = (DbdataTable*)tab;
          204  +  int i;
          205  +  int iSchema = -1;
          206  +  int iPgno = -1;
          207  +  int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
          208  +
          209  +  for(i=0; i<pIdx->nConstraint; i++){
          210  +    struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
          211  +    if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
          212  +      if( p->iColumn==colSchema ){
          213  +        if( p->usable==0 ) return SQLITE_CONSTRAINT;
          214  +        iSchema = i;
          215  +      }
          216  +      if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
          217  +        iPgno = i;
          218  +      }
          219  +    }
          220  +  }
          221  +
          222  +  if( iSchema>=0 ){
          223  +    pIdx->aConstraintUsage[iSchema].argvIndex = 1;
          224  +    pIdx->aConstraintUsage[iSchema].omit = 1;
          225  +  }
          226  +  if( iPgno>=0 ){
          227  +    pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
          228  +    pIdx->aConstraintUsage[iPgno].omit = 1;
          229  +    pIdx->estimatedCost = 100;
          230  +    pIdx->estimatedRows =  50;
          231  +
          232  +    if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
          233  +      int iCol = pIdx->aOrderBy[0].iColumn;
          234  +      if( pIdx->nOrderBy==1 ){
          235  +        pIdx->orderByConsumed = (iCol==0 || iCol==1);
          236  +      }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
          237  +        pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
          238  +      }
          239  +    }
          240  +
          241  +  }else{
          242  +    pIdx->estimatedCost = 100000000;
          243  +    pIdx->estimatedRows = 1000000000;
          244  +  }
          245  +  pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
          246  +  return SQLITE_OK;
          247  +}
          248  +
          249  +/*
          250  +** Open a new sqlite_dbdata or sqlite_dbptr cursor.
          251  +*/
          252  +static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
          253  +  DbdataCursor *pCsr;
          254  +
          255  +  pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
          256  +  if( pCsr==0 ){
          257  +    return SQLITE_NOMEM;
          258  +  }else{
          259  +    memset(pCsr, 0, sizeof(DbdataCursor));
          260  +    pCsr->base.pVtab = pVTab;
          261  +  }
          262  +
          263  +  *ppCursor = (sqlite3_vtab_cursor *)pCsr;
          264  +  return SQLITE_OK;
          265  +}
          266  +
          267  +/*
          268  +** Restore a cursor object to the state it was in when first allocated 
          269  +** by dbdataOpen().
          270  +*/
          271  +static void dbdataResetCursor(DbdataCursor *pCsr){
          272  +  DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
          273  +  if( pTab->pStmt==0 ){
          274  +    pTab->pStmt = pCsr->pStmt;
          275  +  }else{
          276  +    sqlite3_finalize(pCsr->pStmt);
          277  +  }
          278  +  pCsr->pStmt = 0;
          279  +  pCsr->iPgno = 1;
          280  +  pCsr->iCell = 0;
          281  +  pCsr->iField = 0;
          282  +  pCsr->bOnePage = 0;
          283  +  sqlite3_free(pCsr->aPage);
          284  +  sqlite3_free(pCsr->pRec);
          285  +  pCsr->pRec = 0;
          286  +  pCsr->aPage = 0;
          287  +}
          288  +
          289  +/*
          290  +** Close an sqlite_dbdata or sqlite_dbptr cursor.
          291  +*/
          292  +static int dbdataClose(sqlite3_vtab_cursor *pCursor){
          293  +  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
          294  +  dbdataResetCursor(pCsr);
          295  +  sqlite3_free(pCsr);
          296  +  return SQLITE_OK;
          297  +}
          298  +
          299  +/* 
          300  +** Utility methods to decode 16 and 32-bit big-endian unsigned integers. 
          301  +*/
          302  +static unsigned int get_uint16(unsigned char *a){
          303  +  return (a[0]<<8)|a[1];
          304  +}
          305  +static unsigned int get_uint32(unsigned char *a){
          306  +  return ((unsigned int)a[0]<<24)
          307  +       | ((unsigned int)a[1]<<16)
          308  +       | ((unsigned int)a[2]<<8)
          309  +       | ((unsigned int)a[3]);
          310  +}
          311  +
          312  +/*
          313  +** Load page pgno from the database via the sqlite_dbpage virtual table.
          314  +** If successful, set (*ppPage) to point to a buffer containing the page
          315  +** data, (*pnPage) to the size of that buffer in bytes and return
          316  +** SQLITE_OK. In this case it is the responsibility of the caller to
          317  +** eventually free the buffer using sqlite3_free().
          318  +**
          319  +** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
          320  +** return an SQLite error code.
          321  +*/
          322  +static int dbdataLoadPage(
          323  +  DbdataCursor *pCsr,             /* Cursor object */
          324  +  unsigned int pgno,              /* Page number of page to load */
          325  +  u8 **ppPage,                    /* OUT: pointer to page buffer */
          326  +  int *pnPage                     /* OUT: Size of (*ppPage) in bytes */
          327  +){
          328  +  int rc2;
          329  +  int rc = SQLITE_OK;
          330  +  sqlite3_stmt *pStmt = pCsr->pStmt;
          331  +
          332  +  *ppPage = 0;
          333  +  *pnPage = 0;
          334  +  sqlite3_bind_int64(pStmt, 2, pgno);
          335  +  if( SQLITE_ROW==sqlite3_step(pStmt) ){
          336  +    int nCopy = sqlite3_column_bytes(pStmt, 0);
          337  +    if( nCopy>0 ){
          338  +      u8 *pPage;
          339  +      pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
          340  +      if( pPage==0 ){
          341  +        rc = SQLITE_NOMEM;
          342  +      }else{
          343  +        const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
          344  +        memcpy(pPage, pCopy, nCopy);
          345  +        memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
          346  +      }
          347  +      *ppPage = pPage;
          348  +      *pnPage = nCopy;
          349  +    }
          350  +  }
          351  +  rc2 = sqlite3_reset(pStmt);
          352  +  if( rc==SQLITE_OK ) rc = rc2;
          353  +
          354  +  return rc;
          355  +}
          356  +
          357  +/*
          358  +** Read a varint.  Put the value in *pVal and return the number of bytes.
          359  +*/
          360  +static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
          361  +  sqlite3_int64 v = 0;
          362  +  int i;
          363  +  for(i=0; i<8; i++){
          364  +    v = (v<<7) + (z[i]&0x7f);
          365  +    if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
          366  +  }
          367  +  v = (v<<8) + (z[i]&0xff);
          368  +  *pVal = v;
          369  +  return 9;
          370  +}
          371  +
          372  +/*
          373  +** Return the number of bytes of space used by an SQLite value of type
          374  +** eType.
          375  +*/
          376  +static int dbdataValueBytes(int eType){
          377  +  switch( eType ){
          378  +    case 0: case 8: case 9:
          379  +    case 10: case 11:
          380  +      return 0;
          381  +    case 1:
          382  +      return 1;
          383  +    case 2:
          384  +      return 2;
          385  +    case 3:
          386  +      return 3;
          387  +    case 4:
          388  +      return 4;
          389  +    case 5:
          390  +      return 6;
          391  +    case 6:
          392  +    case 7:
          393  +      return 8;
          394  +    default:
          395  +      if( eType>0 ){
          396  +        return ((eType-12) / 2);
          397  +      }
          398  +      return 0;
          399  +  }
          400  +}
          401  +
          402  +/*
          403  +** Load a value of type eType from buffer pData and use it to set the
          404  +** result of context object pCtx.
          405  +*/
          406  +static void dbdataValue(
          407  +  sqlite3_context *pCtx, 
          408  +  int eType, 
          409  +  u8 *pData,
          410  +  int nData
          411  +){
          412  +  if( eType>=0 && dbdataValueBytes(eType)<=nData ){
          413  +    switch( eType ){
          414  +      case 0: 
          415  +      case 10: 
          416  +      case 11: 
          417  +        sqlite3_result_null(pCtx);
          418  +        break;
          419  +      
          420  +      case 8: 
          421  +        sqlite3_result_int(pCtx, 0);
          422  +        break;
          423  +      case 9:
          424  +        sqlite3_result_int(pCtx, 1);
          425  +        break;
          426  +  
          427  +      case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
          428  +        sqlite3_uint64 v = (signed char)pData[0];
          429  +        pData++;
          430  +        switch( eType ){
          431  +          case 7:
          432  +          case 6:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
          433  +          case 5:  v = (v<<16) + (pData[0]<<8) + pData[1];  pData += 2;
          434  +          case 4:  v = (v<<8) + pData[0];  pData++;
          435  +          case 3:  v = (v<<8) + pData[0];  pData++;
          436  +          case 2:  v = (v<<8) + pData[0];  pData++;
          437  +        }
          438  +  
          439  +        if( eType==7 ){
          440  +          double r;
          441  +          memcpy(&r, &v, sizeof(r));
          442  +          sqlite3_result_double(pCtx, r);
          443  +        }else{
          444  +          sqlite3_result_int64(pCtx, (sqlite3_int64)v);
          445  +        }
          446  +        break;
          447  +      }
          448  +  
          449  +      default: {
          450  +        int n = ((eType-12) / 2);
          451  +        if( eType % 2 ){
          452  +          sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
          453  +        }else{
          454  +          sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
          455  +        }
          456  +      }
          457  +    }
          458  +  }
          459  +}
          460  +
          461  +/*
          462  +** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
          463  +*/
          464  +static int dbdataNext(sqlite3_vtab_cursor *pCursor){
          465  +  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
          466  +  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
          467  +
          468  +  pCsr->iRowid++;
          469  +  while( 1 ){
          470  +    int rc;
          471  +    int iOff = (pCsr->iPgno==1 ? 100 : 0);
          472  +    int bNextPage = 0;
          473  +
          474  +    if( pCsr->aPage==0 ){
          475  +      while( 1 ){
          476  +        if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
          477  +        rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
          478  +        if( rc!=SQLITE_OK ) return rc;
          479  +        if( pCsr->aPage ) break;
          480  +        pCsr->iPgno++;
          481  +      }
          482  +      pCsr->iCell = pTab->bPtr ? -2 : 0;
          483  +      pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
          484  +    }
          485  +
          486  +    if( pTab->bPtr ){
          487  +      if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
          488  +        pCsr->iCell = pCsr->nCell;
          489  +      }
          490  +      pCsr->iCell++;
          491  +      if( pCsr->iCell>=pCsr->nCell ){
          492  +        sqlite3_free(pCsr->aPage);
          493  +        pCsr->aPage = 0;
          494  +        if( pCsr->bOnePage ) return SQLITE_OK;
          495  +        pCsr->iPgno++;
          496  +      }else{
          497  +        return SQLITE_OK;
          498  +      }
          499  +    }else{
          500  +      /* If there is no record loaded, load it now. */
          501  +      if( pCsr->pRec==0 ){
          502  +        int bHasRowid = 0;
          503  +        int nPointer = 0;
          504  +        sqlite3_int64 nPayload = 0;
          505  +        sqlite3_int64 nHdr = 0;
          506  +        int iHdr;
          507  +        int U, X;
          508  +        int nLocal;
          509  +  
          510  +        switch( pCsr->aPage[iOff] ){
          511  +          case 0x02:
          512  +            nPointer = 4;
          513  +            break;
          514  +          case 0x0a:
          515  +            break;
          516  +          case 0x0d:
          517  +            bHasRowid = 1;
          518  +            break;
          519  +          default:
          520  +            /* This is not a b-tree page with records on it. Continue. */
          521  +            pCsr->iCell = pCsr->nCell;
          522  +            break;
          523  +        }
          524  +
          525  +        if( pCsr->iCell>=pCsr->nCell ){
          526  +          bNextPage = 1;
          527  +        }else{
          528  +  
          529  +          iOff += 8 + nPointer + pCsr->iCell*2;
          530  +          if( iOff>pCsr->nPage ){
          531  +            bNextPage = 1;
          532  +          }else{
          533  +            iOff = get_uint16(&pCsr->aPage[iOff]);
          534  +          }
          535  +    
          536  +          /* For an interior node cell, skip past the child-page number */
          537  +          iOff += nPointer;
          538  +    
          539  +          /* Load the "byte of payload including overflow" field */
          540  +          if( bNextPage || iOff>pCsr->nPage ){
          541  +            bNextPage = 1;
          542  +          }else{
          543  +            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
          544  +          }
          545  +    
          546  +          /* If this is a leaf intkey cell, load the rowid */
          547  +          if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
          548  +            iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
          549  +          }
          550  +    
          551  +          /* Figure out how much data to read from the local page */
          552  +          U = pCsr->nPage;
          553  +          if( bHasRowid ){
          554  +            X = U-35;
          555  +          }else{
          556  +            X = ((U-12)*64/255)-23;
          557  +          }
          558  +          if( nPayload<=X ){
          559  +            nLocal = nPayload;
          560  +          }else{
          561  +            int M, K;
          562  +            M = ((U-12)*32/255)-23;
          563  +            K = M+((nPayload-M)%(U-4));
          564  +            if( K<=X ){
          565  +              nLocal = K;
          566  +            }else{
          567  +              nLocal = M;
          568  +            }
          569  +          }
          570  +
          571  +          if( bNextPage || nLocal+iOff>pCsr->nPage ){
          572  +            bNextPage = 1;
          573  +          }else{
          574  +
          575  +            /* Allocate space for payload. And a bit more to catch small buffer
          576  +            ** overruns caused by attempting to read a varint or similar from 
          577  +            ** near the end of a corrupt record.  */
          578  +            pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
          579  +            if( pCsr->pRec==0 ) return SQLITE_NOMEM;
          580  +            memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
          581  +            pCsr->nRec = nPayload;
          582  +
          583  +            /* Load the nLocal bytes of payload */
          584  +            memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
          585  +            iOff += nLocal;
          586  +
          587  +            /* Load content from overflow pages */
          588  +            if( nPayload>nLocal ){
          589  +              sqlite3_int64 nRem = nPayload - nLocal;
          590  +              unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
          591  +              while( nRem>0 ){
          592  +                u8 *aOvfl = 0;
          593  +                int nOvfl = 0;
          594  +                int nCopy;
          595  +                rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
          596  +                assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
          597  +                if( rc!=SQLITE_OK ) return rc;
          598  +                if( aOvfl==0 ) break;
          599  +
          600  +                nCopy = U-4;
          601  +                if( nCopy>nRem ) nCopy = nRem;
          602  +                memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
          603  +                nRem -= nCopy;
          604  +
          605  +                pgnoOvfl = get_uint32(aOvfl);
          606  +                sqlite3_free(aOvfl);
          607  +              }
          608  +            }
          609  +    
          610  +            iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
          611  +            pCsr->nHdr = nHdr;
          612  +            pCsr->pHdrPtr = &pCsr->pRec[iHdr];
          613  +            pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
          614  +            pCsr->iField = (bHasRowid ? -1 : 0);
          615  +          }
          616  +        }
          617  +      }else{
          618  +        pCsr->iField++;
          619  +        if( pCsr->iField>0 ){
          620  +          sqlite3_int64 iType;
          621  +          if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
          622  +            bNextPage = 1;
          623  +          }else{
          624  +            pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
          625  +            pCsr->pPtr += dbdataValueBytes(iType);
          626  +          }
          627  +        }
          628  +      }
          629  +
          630  +      if( bNextPage ){
          631  +        sqlite3_free(pCsr->aPage);
          632  +        sqlite3_free(pCsr->pRec);
          633  +        pCsr->aPage = 0;
          634  +        pCsr->pRec = 0;
          635  +        if( pCsr->bOnePage ) return SQLITE_OK;
          636  +        pCsr->iPgno++;
          637  +      }else{
          638  +        if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
          639  +          return SQLITE_OK;
          640  +        }
          641  +
          642  +        /* Advance to the next cell. The next iteration of the loop will load
          643  +        ** the record and so on. */
          644  +        sqlite3_free(pCsr->pRec);
          645  +        pCsr->pRec = 0;
          646  +        pCsr->iCell++;
          647  +      }
          648  +    }
          649  +  }
          650  +
          651  +  assert( !"can't get here" );
          652  +  return SQLITE_OK;
          653  +}
          654  +
          655  +/* 
          656  +** Return true if the cursor is at EOF.
          657  +*/
          658  +static int dbdataEof(sqlite3_vtab_cursor *pCursor){
          659  +  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
          660  +  return pCsr->aPage==0;
          661  +}
          662  +
          663  +/* 
          664  +** Determine the size in pages of database zSchema (where zSchema is
          665  +** "main", "temp" or the name of an attached database) and set 
          666  +** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
          667  +** an SQLite error code.
          668  +*/
          669  +static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
          670  +  DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
          671  +  char *zSql = 0;
          672  +  int rc, rc2;
          673  +  sqlite3_stmt *pStmt = 0;
          674  +
          675  +  zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
          676  +  if( zSql==0 ) return SQLITE_NOMEM;
          677  +  rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
          678  +  sqlite3_free(zSql);
          679  +  if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
          680  +    pCsr->szDb = sqlite3_column_int(pStmt, 0);
          681  +  }
          682  +  rc2 = sqlite3_finalize(pStmt);
          683  +  if( rc==SQLITE_OK ) rc = rc2;
          684  +  return rc;
          685  +}
          686  +
          687  +/* 
          688  +** xFilter method for sqlite_dbdata and sqlite_dbptr.
          689  +*/
          690  +static int dbdataFilter(
          691  +  sqlite3_vtab_cursor *pCursor, 
          692  +  int idxNum, const char *idxStr,
          693  +  int argc, sqlite3_value **argv
          694  +){
          695  +  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
          696  +  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
          697  +  int rc = SQLITE_OK;
          698  +  const char *zSchema = "main";
          699  +
          700  +  dbdataResetCursor(pCsr);
          701  +  assert( pCsr->iPgno==1 );
          702  +  if( idxNum & 0x01 ){
          703  +    zSchema = (const char*)sqlite3_value_text(argv[0]);
          704  +  }
          705  +  if( idxNum & 0x02 ){
          706  +    pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
          707  +    pCsr->bOnePage = 1;
          708  +  }else{
          709  +    pCsr->nPage = dbdataDbsize(pCsr, zSchema);
          710  +    rc = dbdataDbsize(pCsr, zSchema);
          711  +  }
          712  +
          713  +  if( rc==SQLITE_OK ){
          714  +    if( pTab->pStmt ){
          715  +      pCsr->pStmt = pTab->pStmt;
          716  +      pTab->pStmt = 0;
          717  +    }else{
          718  +      rc = sqlite3_prepare_v2(pTab->db, 
          719  +          "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
          720  +          &pCsr->pStmt, 0
          721  +      );
          722  +    }
          723  +  }
          724  +  if( rc==SQLITE_OK ){
          725  +    rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
          726  +  }else{
          727  +    pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
          728  +  }
          729  +  if( rc==SQLITE_OK ){
          730  +    rc = dbdataNext(pCursor);
          731  +  }
          732  +  return rc;
          733  +}
          734  +
          735  +/* 
          736  +** Return a column for the sqlite_dbdata or sqlite_dbptr table.
          737  +*/
          738  +static int dbdataColumn(
          739  +  sqlite3_vtab_cursor *pCursor, 
          740  +  sqlite3_context *ctx, 
          741  +  int i
          742  +){
          743  +  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
          744  +  DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
          745  +  if( pTab->bPtr ){
          746  +    switch( i ){
          747  +      case DBPTR_COLUMN_PGNO:
          748  +        sqlite3_result_int64(ctx, pCsr->iPgno);
          749  +        break;
          750  +      case DBPTR_COLUMN_CHILD: {
          751  +        int iOff = pCsr->iPgno==1 ? 100 : 0;
          752  +        if( pCsr->iCell<0 ){
          753  +          iOff += 8;
          754  +        }else{
          755  +          iOff += 12 + pCsr->iCell*2;
          756  +          if( iOff>pCsr->nPage ) return SQLITE_OK;
          757  +          iOff = get_uint16(&pCsr->aPage[iOff]);
          758  +        }
          759  +        if( iOff<=pCsr->nPage ){
          760  +          sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
          761  +        }
          762  +        break;
          763  +      }
          764  +    }
          765  +  }else{
          766  +    switch( i ){
          767  +      case DBDATA_COLUMN_PGNO:
          768  +        sqlite3_result_int64(ctx, pCsr->iPgno);
          769  +        break;
          770  +      case DBDATA_COLUMN_CELL:
          771  +        sqlite3_result_int(ctx, pCsr->iCell);
          772  +        break;
          773  +      case DBDATA_COLUMN_FIELD:
          774  +        sqlite3_result_int(ctx, pCsr->iField);
          775  +        break;
          776  +      case DBDATA_COLUMN_VALUE: {
          777  +        if( pCsr->iField<0 ){
          778  +          sqlite3_result_int64(ctx, pCsr->iIntkey);
          779  +        }else{
          780  +          sqlite3_int64 iType;
          781  +          dbdataGetVarint(pCsr->pHdrPtr, &iType);
          782  +          dbdataValue(
          783  +              ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
          784  +          );
          785  +        }
          786  +        break;
          787  +      }
          788  +    }
          789  +  }
          790  +  return SQLITE_OK;
          791  +}
          792  +
          793  +/* 
          794  +** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
          795  +*/
          796  +static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
          797  +  DbdataCursor *pCsr = (DbdataCursor*)pCursor;
          798  +  *pRowid = pCsr->iRowid;
          799  +  return SQLITE_OK;
          800  +}
          801  +
          802  +
          803  +/*
          804  +** Invoke this routine to register the "sqlite_dbdata" virtual table module
          805  +*/
          806  +static int sqlite3DbdataRegister(sqlite3 *db){
          807  +  static sqlite3_module dbdata_module = {
          808  +    0,                            /* iVersion */
          809  +    0,                            /* xCreate */
          810  +    dbdataConnect,                /* xConnect */
          811  +    dbdataBestIndex,              /* xBestIndex */
          812  +    dbdataDisconnect,             /* xDisconnect */
          813  +    0,                            /* xDestroy */
          814  +    dbdataOpen,                   /* xOpen - open a cursor */
          815  +    dbdataClose,                  /* xClose - close a cursor */
          816  +    dbdataFilter,                 /* xFilter - configure scan constraints */
          817  +    dbdataNext,                   /* xNext - advance a cursor */
          818  +    dbdataEof,                    /* xEof - check for end of scan */
          819  +    dbdataColumn,                 /* xColumn - read data */
          820  +    dbdataRowid,                  /* xRowid - read data */
          821  +    0,                            /* xUpdate */
          822  +    0,                            /* xBegin */
          823  +    0,                            /* xSync */
          824  +    0,                            /* xCommit */
          825  +    0,                            /* xRollback */
          826  +    0,                            /* xFindMethod */
          827  +    0,                            /* xRename */
          828  +    0,                            /* xSavepoint */
          829  +    0,                            /* xRelease */
          830  +    0,                            /* xRollbackTo */
          831  +    0                             /* xShadowName */
          832  +  };
          833  +
          834  +  int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
          835  +  if( rc==SQLITE_OK ){
          836  +    rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
          837  +  }
          838  +  return rc;
          839  +}
          840  +
          841  +#ifdef _WIN32
          842  +__declspec(dllexport)
          843  +#endif
          844  +int sqlite3_dbdata_init(
          845  +  sqlite3 *db, 
          846  +  char **pzErrMsg, 
          847  +  const sqlite3_api_routines *pApi
          848  +){
          849  +  SQLITE_EXTENSION_INIT2(pApi);
          850  +  return sqlite3DbdataRegister(db);
          851  +}

Changes to ext/rbu/rbu_common.tcl.

    85     85     }
    86     86     set rc
    87     87   }
    88     88   
    89     89   proc do_rbu_vacuum_test {tn step {statedb state.db}} {
    90     90     forcedelete $statedb
    91     91     if {$statedb=="" && $step==1} breakpoint
    92         -  uplevel [list do_test $tn.1 [string map [list %state% $statedb] {
    93         -    if {$step==0} { sqlite3rbu_vacuum rbu test.db {%state%}}
           92  +  uplevel [list do_test $tn.1 [string map [list %state% $statedb %step% $step] {
           93  +    if {%step%==0} { sqlite3rbu_vacuum rbu test.db {%state%}}
    94     94       while 1 {
    95         -      if {$step==1} { sqlite3rbu_vacuum rbu test.db {%state%}}
           95  +      if {%step%==1} { sqlite3rbu_vacuum rbu test.db {%state%}}
    96     96         set state [rbu state]
    97     97         check_prestep_state test.db $state
    98     98         set rc [rbu step]
    99     99         check_poststep_state $rc test.db $state
   100    100         if {$rc!="SQLITE_OK"} break
   101         -      if {$step==1} { rbu close }
          101  +      if {%step%==1} { rbu close }
   102    102       }
   103    103       rbu close
   104    104     }] {SQLITE_DONE}]
   105    105   
   106    106     uplevel [list do_execsql_test $tn.2 {
   107    107       PRAGMA integrity_check
   108    108     } ok]
   109    109   }
   110    110   

Changes to ext/rbu/rbufault2.test.

    48     48         {1 SQLITE_CONSTRAINT} \
    49     49         {1 SQLITE_NOMEM} \
    50     50         {1 {SQLITE_NOMEM - unable to open a temporary database file for storing temporary tables}} \
    51     51         {1 {SQLITE_NOMEM - out of memory}} 
    52     52   }
    53     53   
    54     54   
           55  +sqlite3rbu_create_vfs -default rbu ""
           56  +sqlite3 db test.db
           57  +set ::vfsname [file_control_vfsname db]
           58  +do_faultsim_test 2 -faults oom* -prep {
           59  +} -body {
           60  +  file_control_vfsname db
           61  +} 
           62  +db close
           63  +sqlite3rbu_destroy_vfs rbu
    55     64   
    56     65   
    57     66   finish_test

Changes to ext/rbu/rbufault3.test.

    79     79     sqlite3rbu_vacuum rbu test.db test.db2
    80     80     rbu step
    81     81     rbu close
    82     82     faultsim_save_and_close
    83     83   
    84     84     do_faultsim_test 3 -faults $fault -prep {
    85     85       faultsim_restore_and_reopen
    86         -    forcedelete test.db2
    87     86     } -body {
    88     87       sqlite3rbu_vacuum rbu test.db test.db2
    89     88       rbu step
    90     89       rbu close
    91     90     } -test {
    92     91       eval [list faultsim_test_result {0 SQLITE_OK} {*}$::errlist]
    93     92     }
    94         -
    95     93   }
    96     94   
    97     95   finish_test

Added ext/rbu/rbumisc.test.

            1  +# 2014 August 30
            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  +source [file join [file dirname [info script]] rbu_common.tcl]
           14  +set ::testprefix rbumisc
           15  +
           16  +db close
           17  +sqlite3_shutdown
           18  +sqlite3_config_uri 1
           19  +reset_db
           20  +
           21  +proc populate_rbu_db {} {
           22  +  forcedelete rbu.db
           23  +  sqlite3 rbu rbu.db
           24  +  rbu eval {
           25  +    CREATE TABLE data_x1(a, b, c, rbu_control);
           26  +    INSERT INTO data_x1 VALUES(1, 1, 1, 0);
           27  +    INSERT INTO data_x1 VALUES(2, 2, 2, 0);
           28  +
           29  +    CREATE TABLE dat(a, b, c, rbu_control);
           30  +    CREATE TABLE "data x1"(a, b, c, rbu_control);
           31  +    CREATE TABLE datax1(a, b, c, rbu_control);
           32  +    CREATE TABLE data_(a, b, c, rbu_control);
           33  +
           34  +    INSERT INTO "data x1" VALUES(3, 3, 3, 0);
           35  +    INSERT INTO datax1 VALUES(3, 3, 3, 0);
           36  +    INSERT INTO data_ VALUES(3, 3, 3, 0);
           37  +    INSERT INTO dat VALUES(3, 3, 3, 0);
           38  +  }
           39  +  rbu close
           40  +}
           41  +
           42  +#-------------------------------------------------------------------------
           43  +# Ensure that RBU is not confused by oddly named tables in an RBU 
           44  +# database.
           45  +#
           46  +do_execsql_test 1.0 {
           47  +  CREATE TABLE x1(a, b, c INTEGER PRIMARY KEY);
           48  +}
           49  +do_test 1.1 {
           50  +  populate_rbu_db
           51  +} {}
           52  +
           53  +do_test 1.2 {
           54  +  step_rbu test.db rbu.db
           55  +  db eval { SELECT * FROM x1 }
           56  +} {1 1 1 2 2 2}
           57  +
           58  +do_test 1.3 {
           59  +  db eval { DELETE FROM x1 }
           60  +  sqlite3 rbu rbu.db
           61  +  rbu eval { DELETE FROM rbu_state }
           62  +  rbu close
           63  +  step_rbu test.db rbu.db
           64  +  db eval { SELECT * FROM x1 }
           65  +} {1 1 1 2 2 2}
           66  +
           67  +do_test 1.4 {
           68  +  db eval { DELETE FROM x1 }
           69  +  populate_rbu_db
           70  +
           71  +  sqlite3rbu rbu test.db rbu.db
           72  +  rbu step
           73  +  rbu step
           74  +  rbu close
           75  +
           76  +  forcecopy test.db-oal test.db-wal
           77  +  sqlite3rbu rbu test.db rbu.db
           78  +  rbu step
           79  +  list [catch { rbu close } msg] $msg
           80  +} {1 {SQLITE_ERROR - cannot update wal mode database}}
           81  +
           82  +#-------------------------------------------------------------------------
           83  +# Test the effect of a wal file appearing after the target database has
           84  +# been opened, but before it has been locked.
           85  +#
           86  +catch { db close }
           87  +testvfs tvfs -default 1
           88  +
           89  +for {set N 1} {$N < 10} {incr N} {
           90  +  reset_db
           91  +  populate_rbu_db
           92  +  do_execsql_test 2.$N.0 {
           93  +    CREATE TABLE x1(a, b, c INTEGER PRIMARY KEY);
           94  +  }
           95  +  
           96  +  set nAccessCnt 0
           97  +  do_test 2.$N.1 {
           98  +    sqlite3rbu rbu test.db rbu.db
           99  +    rbu step
          100  +    rbu step
          101  +    rbu close
          102  +  } {SQLITE_OK}
          103  +  
          104  +  tvfs script xAccess
          105  +  tvfs filter xAccess
          106  +  set nAccessCnt 0
          107  +  proc xAccess {method file args} {
          108  +    global nAccessCnt
          109  +    if {[file tail $file]=="test.db-wal"} {
          110  +      incr nAccessCnt -1
          111  +      if {$nAccessCnt==0} {
          112  +        set fd [open test.db-wal w]
          113  +        puts -nonewline $fd [string repeat 0 2000]
          114  +        close $fd
          115  +      }
          116  +    }
          117  +    return SQLITE_OK
          118  +  }
          119  +
          120  +  foreach r {
          121  +     {1 {SQLITE_ERROR - cannot update wal mode database}}
          122  +     {0 SQLITE_OK}
          123  +     {1 {SQLITE_CANTOPEN - unable to open database file}}
          124  +  } {
          125  +    set RES($r) 1
          126  +  }
          127  +  do_test 2.$N.2 {
          128  +    set ::nAccessCnt $N
          129  +    set res [list [catch {
          130  +      sqlite3rbu rbu test.db rbu.db
          131  +      rbu step
          132  +      rbu close
          133  +    } msg ] $msg]
          134  +    set RES($res)
          135  +  } {1}
          136  +  catch {rbu close}
          137  +}
          138  +catch {db close}
          139  +catch {tvfs delete}
          140  +
          141  +#-------------------------------------------------------------------------
          142  +testvfs tvfs -default 1
          143  +reset_db
          144  +populate_rbu_db
          145  +do_execsql_test 3.0 {
          146  +  CREATE TABLE x1(a, b, c INTEGER PRIMARY KEY);
          147  +}
          148  +  
          149  +tvfs script xFileControl
          150  +tvfs filter xFileControl
          151  +
          152  +proc xFileControl {method file verb args} {
          153  +  if {$verb=="ZIPVFS" && [info exists ::zipvfs_filecontrol]} {
          154  +    return $::zipvfs_filecontrol 
          155  +  }
          156  +  return "SQLITE_NOTFOUND"
          157  +}
          158  +
          159  +breakpoint
          160  +foreach {tn ret err} {
          161  +  1 SQLITE_OK           0
          162  +  2 SQLITE_ERROR        1
          163  +  3 SQLITE_NOTFOUND     0
          164  +  4 SQLITE_OMIT         1
          165  +} {
          166  +  set ::zipvfs_filecontrol $ret
          167  +  do_test 3.$tn.1 {
          168  +    catch {
          169  +      sqlite3rbu rbu test.db rbu.db
          170  +      rbu step
          171  +      rbu close
          172  +    }
          173  +  } $err
          174  +}
          175  +catch {db close}
          176  +catch {tvfs delete}
          177  +
          178  +#-------------------------------------------------------------------------
          179  +
          180  +finish_test

Changes to ext/rbu/rbupartial.test.

    76     76       SELECT * FROM t1 ORDER BY %A%;
    77     77     } {
    78     78       1 10 {} b   7 8 4 d   10 11 12 e   13 14 {} f
    79     79     }
    80     80   
    81     81     set step 0
    82     82     do_rbu_vacuum_test $tn.1.5 0
           83  +
           84  +  do_test $tn.1.6 {
           85  +    execsql { PRAGMA integrity_check }
           86  +  } {ok}
    83     87     }]
    84     88   }
    85     89   
    86     90   finish_test

Changes to ext/rbu/rbutemplimit.test.

    61     61   }
    62     62   
    63     63   proc step_rbu_cachesize {target rbu stepsize cachesize temp_limit} {
    64     64     set res ""
    65     65     while 1 {
    66     66       sqlite3rbu rbu $target $rbu
    67     67       rbu temp_size_limit $temp_limit
           68  +    if { [rbu temp_size_limit -1]!=$temp_limit } { error "round trip problem!" }
    68     69       sqlite3_exec_nr [rbu db 1] "PRAGMA cache_size = $cachesize"
    69     70       for {set i 0} {$i < $stepsize} {incr i} {
    70     71         set rc [rbu step]
    71     72         set ::A([rbu temp_size]) 1
    72     73         if {$rc!="SQLITE_OK"} break
    73     74       }
    74     75       set res [list [catch {rbu close} msg] $msg]

Added ext/rbu/rbuvacuum4.test.

            1  +# 2019 Jan 3
            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  +# This file contains tests for the RBU module. More specifically, it
           13  +# contains tests to ensure that the sqlite3rbu_vacuum() API works as
           14  +# expected.
           15  +#
           16  +
           17  +source [file join [file dirname [info script]] rbu_common.tcl]
           18  +set testprefix rbuvacuum4
           19  +
           20  +set step 1
           21  +
           22  +do_execsql_test 1.0 {
           23  +  CREATE TABLE t1(a PRIMARY KEY, b, c) WITHOUT ROWID;
           24  +  INSERT INTO t1 VALUES(1, 2, 3);
           25  +  INSERT INTO t1 VALUES(4, 5, 6);
           26  +  INSERT INTO t1 VALUES(7, 8, 9);
           27  +}
           28  +do_rbu_vacuum_test 1.1 1
           29  +
           30  +#-------------------------------------------------------------------------
           31  +reset_db
           32  +
           33  +do_execsql_test 2.0 {
           34  +  CREATE TABLE t1(a, b, c, PRIMARY KEY(a, b, c)) WITHOUT ROWID;
           35  +  INSERT INTO t1 VALUES(1, 2, 3);
           36  +  INSERT INTO t1 VALUES(4, 5, 6);
           37  +  INSERT INTO t1 VALUES(7, 8, 9);
           38  +}
           39  +do_rbu_vacuum_test 2.1 1
           40  +do_execsql_test 2.2 {
           41  +  SELECT * FROM t1;
           42  +} {1 2 3 4 5 6 7 8 9}
           43  +
           44  +#-------------------------------------------------------------------------
           45  +reset_db
           46  +
           47  +do_execsql_test 3.0 {
           48  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
           49  +  CREATE INDEX i1 oN t1(b, c);
           50  +  INSERT INTO t1 VALUES(1, 2, 3);
           51  +  INSERT INTO t1 VALUES(4, 5, 6);
           52  +  INSERT INTO t1 VALUES(7, 8, 9);
           53  +
           54  +  CREATE TABLE t2(a, b, c INTEGER, PRIMARY KEY(c));
           55  +  CREATE INDEX i2 oN t2(b, a);
           56  +  INSERT INTO t2 VALUES('a', 'b', -1);
           57  +  INSERT INTO t2 VALUES('c', 'd', -2);
           58  +  INSERT INTO t2 VALUES('e', 'f', -3);
           59  +}
           60  +
           61  +do_rbu_vacuum_test 3.1 1
           62  +
           63  +do_execsql_test 3.2 {
           64  +  SELECT * FROM t1;
           65  +  SELECT * FROM t2;
           66  +} {1 2 3 4 5 6 7 8 9 e f -3 c d -2 a b -1}
           67  +
           68  +#-------------------------------------------------------------------------
           69  +reset_db
           70  +do_execsql_test 4.0 {
           71  +  CREATE TABLE x1(a, b, c, d, PRIMARY KEY(c, b)) WITHOUT ROWID;
           72  +  INSERT INTO x1 VALUES(1, 1, 1, 1);
           73  +  INSERT INTO x1 VALUES(1, 1, 2, 1);
           74  +  INSERT INTO x1 VALUES(1, 2, 2, 1);
           75  +
           76  +  INSERT INTO x1 VALUES(NULL, 2, 3, NULL);
           77  +  INSERT INTO x1 VALUES(NULL, 2, 4, NULL);
           78  +  INSERT INTO x1 VALUES(NULL, 2, 5, NULL);
           79  +
           80  +  CREATE INDEX x1ad ON x1(d, a);
           81  +  CREATE INDEX x1null ON x1(d, a) WHERE d>15;
           82  +}
           83  +
           84  +do_rbu_vacuum_test 4.1.1 1
           85  +
           86  +do_execsql_test 4.2 {
           87  +  SELECT count(*) fROM x1
           88  +} 6
           89  +
           90  +do_rbu_vacuum_test 4.1.2 0
           91  +
           92  +#-------------------------------------------------------------------------
           93  +reset_db
           94  +do_execsql_test 5.0 {
           95  +  CREATE TABLE "a b c"(a, "b b" PRIMARY KEY, "c c");
           96  +  CREATE INDEX abc1 ON "a b c"(a, "c c");
           97  +
           98  +  INSERT INTO "a b c" VALUES(NULL, 'a', NULL);
           99  +  INSERT INTO "a b c" VALUES(NULL, 'b', NULL);
          100  +  INSERT INTO "a b c" VALUES(NULL, 'c', NULL);
          101  +
          102  +  INSERT INTO "a b c" VALUES(1, 2, 3);
          103  +  INSERT INTO "a b c" VALUES(3, 9, 1);
          104  +  INSERT INTO "a b c" VALUES('aaa', 'bbb', 'ccc');
          105  +
          106  +  CREATE INDEX abc2 ON "a b c"("c c" DESC, a);
          107  +
          108  +  CREATE TABLE x(a);
          109  +  INSERT INTO x VALUES('a'), ('b'), ('d');
          110  +  CREATE UNIQUE INDEX y ON x(a);
          111  +}
          112  +
          113  +do_rbu_vacuum_test 5.1 1
          114  +
          115  +finish_test
          116  +

Changes to ext/rbu/sqlite3rbu.c.

   926    926     sqlite3rbu *p = sqlite3_user_data(pCtx);
   927    927     const char *zIn;
   928    928     assert( argc==1 || argc==2 );
   929    929   
   930    930     zIn = (const char*)sqlite3_value_text(argv[0]);
   931    931     if( zIn ){
   932    932       if( rbuIsVacuum(p) ){
   933         -      if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
          933  +      assert( argc==2 );
          934  +      if( 0==sqlite3_value_int(argv[1]) ){
   934    935           sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
   935    936         }
   936    937       }else{
   937    938         if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
   938    939           int i;
   939    940           for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
   940    941           if( zIn[i]=='_' && zIn[i+1] ){
................................................................................
  1377   1378   
  1378   1379           if( i!=iOrder ){
  1379   1380             SWAP(int, pIter->aiSrcOrder[i], pIter->aiSrcOrder[iOrder]);
  1380   1381             SWAP(char*, pIter->azTblCol[i], pIter->azTblCol[iOrder]);
  1381   1382           }
  1382   1383   
  1383   1384           pIter->azTblType[iOrder] = rbuStrndup(zType, &p->rc);
  1384         -        pIter->abTblPk[iOrder] = (iPk!=0);
         1385  +        assert( iPk>=0 );
         1386  +        pIter->abTblPk[iOrder] = (u8)iPk;
  1385   1387           pIter->abNotNull[iOrder] = (u8)bNotNull || (iPk!=0);
  1386   1388           iOrder++;
  1387   1389         }
  1388   1390       }
  1389   1391   
  1390   1392       rbuFinalize(p, pStmt);
  1391   1393       rbuObjIterCacheIndexedCols(p, pIter);
................................................................................
  1411   1413     for(i=0; i<pIter->nTblCol; i++){
  1412   1414       const char *z = pIter->azTblCol[i];
  1413   1415       zList = rbuMPrintf(p, "%z%s\"%w\"", zList, zSep, z);
  1414   1416       zSep = ", ";
  1415   1417     }
  1416   1418     return zList;
  1417   1419   }
         1420  +
         1421  +/*
         1422  +** Return a comma separated list of the quoted PRIMARY KEY column names,
         1423  +** in order, for the current table. Before each column name, add the text
         1424  +** zPre. After each column name, add the zPost text. Use zSeparator as
         1425  +** the separator text (usually ", ").
         1426  +*/
         1427  +static char *rbuObjIterGetPkList(
         1428  +  sqlite3rbu *p,                  /* RBU object */
         1429  +  RbuObjIter *pIter,              /* Object iterator for column names */
         1430  +  const char *zPre,               /* Before each quoted column name */
         1431  +  const char *zSeparator,         /* Separator to use between columns */
         1432  +  const char *zPost               /* After each quoted column name */
         1433  +){
         1434  +  int iPk = 1;
         1435  +  char *zRet = 0;
         1436  +  const char *zSep = "";
         1437  +  while( 1 ){
         1438  +    int i;
         1439  +    for(i=0; i<pIter->nTblCol; i++){
         1440  +      if( (int)pIter->abTblPk[i]==iPk ){
         1441  +        const char *zCol = pIter->azTblCol[i];
         1442  +        zRet = rbuMPrintf(p, "%z%s%s\"%w\"%s", zRet, zSep, zPre, zCol, zPost);
         1443  +        zSep = zSeparator;
         1444  +        break;
         1445  +      }
         1446  +    }
         1447  +    if( i==pIter->nTblCol ) break;
         1448  +    iPk++;
         1449  +  }
         1450  +  return zRet;
         1451  +}
         1452  +
         1453  +/*
         1454  +** This function is called as part of restarting an RBU vacuum within 
         1455  +** stage 1 of the process (while the *-oal file is being built) while
         1456  +** updating a table (not an index). The table may be a rowid table or
         1457  +** a WITHOUT ROWID table. It queries the target database to find the 
         1458  +** largest key that has already been written to the target table and
         1459  +** constructs a WHERE clause that can be used to extract the remaining
         1460  +** rows from the source table. For a rowid table, the WHERE clause
         1461  +** is of the form:
         1462  +**
         1463  +**     "WHERE _rowid_ > ?"
         1464  +**
         1465  +** and for WITHOUT ROWID tables:
         1466  +**
         1467  +**     "WHERE (key1, key2) > (?, ?)"
         1468  +**
         1469  +** Instead of "?" placeholders, the actual WHERE clauses created by
         1470  +** this function contain literal SQL values.
         1471  +*/
         1472  +static char *rbuVacuumTableStart(
         1473  +  sqlite3rbu *p,                  /* RBU handle */
         1474  +  RbuObjIter *pIter,              /* RBU iterator object */
         1475  +  int bRowid,                     /* True for a rowid table */
         1476  +  const char *zWrite              /* Target table name prefix */
         1477  +){
         1478  +  sqlite3_stmt *pMax = 0;
         1479  +  char *zRet = 0;
         1480  +  if( bRowid ){
         1481  +    p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg, 
         1482  +        sqlite3_mprintf(
         1483  +          "SELECT max(_rowid_) FROM \"%s%w\"", zWrite, pIter->zTbl
         1484  +        )
         1485  +    );
         1486  +    if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
         1487  +      sqlite3_int64 iMax = sqlite3_column_int64(pMax, 0);
         1488  +      zRet = rbuMPrintf(p, " WHERE _rowid_ > %lld ", iMax);
         1489  +    }
         1490  +    rbuFinalize(p, pMax);
         1491  +  }else{
         1492  +    char *zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", " DESC");
         1493  +    char *zSelect = rbuObjIterGetPkList(p, pIter, "quote(", "||','||", ")");
         1494  +    char *zList = rbuObjIterGetPkList(p, pIter, "", ", ", "");
         1495  +
         1496  +    if( p->rc==SQLITE_OK ){
         1497  +      p->rc = prepareFreeAndCollectError(p->dbMain, &pMax, &p->zErrmsg, 
         1498  +          sqlite3_mprintf(
         1499  +            "SELECT %s FROM \"%s%w\" ORDER BY %s LIMIT 1", 
         1500  +                zSelect, zWrite, pIter->zTbl, zOrder
         1501  +          )
         1502  +      );
         1503  +      if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
         1504  +        const char *zVal = (const char*)sqlite3_column_text(pMax, 0);
         1505  +        zRet = rbuMPrintf(p, " WHERE (%s) > (%s) ", zList, zVal);
         1506  +      }
         1507  +      rbuFinalize(p, pMax);
         1508  +    }
         1509  +
         1510  +    sqlite3_free(zOrder);
         1511  +    sqlite3_free(zSelect);
         1512  +    sqlite3_free(zList);
         1513  +  }
         1514  +  return zRet;
         1515  +}
         1516  +
         1517  +/*
         1518  +** This function is called as part of restating an RBU vacuum when the
         1519  +** current operation is writing content to an index. If possible, it
         1520  +** queries the target index b-tree for the largest key already written to
         1521  +** it, then composes and returns an expression that can be used in a WHERE 
         1522  +** clause to select the remaining required rows from the source table. 
         1523  +** It is only possible to return such an expression if:
         1524  +**
         1525  +**   * The index contains no DESC columns, and
         1526  +**   * The last key written to the index before the operation was 
         1527  +**     suspended does not contain any NULL values.
         1528  +**
         1529  +** The expression is of the form:
         1530  +**
         1531  +**   (index-field1, index-field2, ...) > (?, ?, ...)
         1532  +**
         1533  +** except that the "?" placeholders are replaced with literal values.
         1534  +**
         1535  +** If the expression cannot be created, NULL is returned. In this case,
         1536  +** the caller has to use an OFFSET clause to extract only the required 
         1537  +** rows from the sourct table, just as it does for an RBU update operation.
         1538  +*/
         1539  +char *rbuVacuumIndexStart(
         1540  +  sqlite3rbu *p,                  /* RBU handle */
         1541  +  RbuObjIter *pIter               /* RBU iterator object */
         1542  +){
         1543  +  char *zOrder = 0;
         1544  +  char *zLhs = 0;
         1545  +  char *zSelect = 0;
         1546  +  char *zVector = 0;
         1547  +  char *zRet = 0;
         1548  +  int bFailed = 0;
         1549  +  const char *zSep = "";
         1550  +  int iCol = 0;
         1551  +  sqlite3_stmt *pXInfo = 0;
         1552  +
         1553  +  p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
         1554  +      sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", pIter->zIdx)
         1555  +  );
         1556  +  while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
         1557  +    int iCid = sqlite3_column_int(pXInfo, 1);
         1558  +    const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
         1559  +    const char *zCol;
         1560  +    if( sqlite3_column_int(pXInfo, 3) ){
         1561  +      bFailed = 1;
         1562  +      break;
         1563  +    }
         1564  +
         1565  +    if( iCid<0 ){
         1566  +      if( pIter->eType==RBU_PK_IPK ){
         1567  +        int i;
         1568  +        for(i=0; pIter->abTblPk[i]==0; i++);
         1569  +        assert( i<pIter->nTblCol );
         1570  +        zCol = pIter->azTblCol[i];
         1571  +      }else{
         1572  +        zCol = "_rowid_";
         1573  +      }
         1574  +    }else{
         1575  +      zCol = pIter->azTblCol[iCid];
         1576  +    }
         1577  +
         1578  +    zLhs = rbuMPrintf(p, "%z%s \"%w\" COLLATE %Q",
         1579  +        zLhs, zSep, zCol, zCollate
         1580  +        );
         1581  +    zOrder = rbuMPrintf(p, "%z%s \"rbu_imp_%d%w\" COLLATE %Q DESC",
         1582  +        zOrder, zSep, iCol, zCol, zCollate
         1583  +        );
         1584  +    zSelect = rbuMPrintf(p, "%z%s quote(\"rbu_imp_%d%w\")",
         1585  +        zSelect, zSep, iCol, zCol
         1586  +        );
         1587  +    zSep = ", ";
         1588  +    iCol++;
         1589  +  }
         1590  +  rbuFinalize(p, pXInfo);
         1591  +  if( bFailed ) goto index_start_out;
         1592  +
         1593  +  if( p->rc==SQLITE_OK ){
         1594  +    sqlite3_stmt *pSel = 0;
         1595  +
         1596  +    p->rc = prepareFreeAndCollectError(p->dbMain, &pSel, &p->zErrmsg,
         1597  +        sqlite3_mprintf("SELECT %s FROM \"rbu_imp_%w\" ORDER BY %s LIMIT 1",
         1598  +          zSelect, pIter->zTbl, zOrder
         1599  +        )
         1600  +    );
         1601  +    if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSel) ){
         1602  +      zSep = "";
         1603  +      for(iCol=0; iCol<pIter->nCol; iCol++){
         1604  +        const char *zQuoted = (const char*)sqlite3_column_text(pSel, iCol);
         1605  +        if( zQuoted[0]=='N' ){
         1606  +          bFailed = 1;
         1607  +          break;
         1608  +        }
         1609  +        zVector = rbuMPrintf(p, "%z%s%s", zVector, zSep, zQuoted);
         1610  +        zSep = ", ";
         1611  +      }
         1612  +
         1613  +      if( !bFailed ){
         1614  +        zRet = rbuMPrintf(p, "(%s) > (%s)", zLhs, zVector);
         1615  +      }
         1616  +    }
         1617  +    rbuFinalize(p, pSel);
         1618  +  }
         1619  +
         1620  + index_start_out:
         1621  +  sqlite3_free(zOrder);
         1622  +  sqlite3_free(zSelect);
         1623  +  sqlite3_free(zVector);
         1624  +  sqlite3_free(zLhs);
         1625  +  return zRet;
         1626  +}
  1418   1627   
  1419   1628   /*
  1420   1629   ** This function is used to create a SELECT list (the list of SQL 
  1421   1630   ** expressions that follows a SELECT keyword) for a SELECT statement 
  1422   1631   ** used to read from an data_xxx or rbu_tmp_xxx table while updating the 
  1423   1632   ** index object currently indicated by the iterator object passed as the 
  1424   1633   ** second argument. A "PRAGMA index_xinfo = <idxname>" statement is used 
................................................................................
  2088   2297           );
  2089   2298         }
  2090   2299   
  2091   2300         /* Create the SELECT statement to read keys in sorted order */
  2092   2301         if( p->rc==SQLITE_OK ){
  2093   2302           char *zSql;
  2094   2303           if( rbuIsVacuum(p) ){
         2304  +          char *zStart = 0;
         2305  +          if( nOffset ){
         2306  +            zStart = rbuVacuumIndexStart(p, pIter);
         2307  +            if( zStart ){
         2308  +              sqlite3_free(zLimit);
         2309  +              zLimit = 0;
         2310  +            }
         2311  +          }
         2312  +
  2095   2313             zSql = sqlite3_mprintf(
  2096         -              "SELECT %s, 0 AS rbu_control FROM '%q' %s ORDER BY %s%s",
         2314  +              "SELECT %s, 0 AS rbu_control FROM '%q' %s %s %s ORDER BY %s%s",
  2097   2315                 zCollist, 
  2098   2316                 pIter->zDataTbl,
  2099         -              zPart, zCollist, zLimit
         2317  +              zPart, 
         2318  +              (zStart ? (zPart ? "AND" : "WHERE") : ""), zStart,
         2319  +              zCollist, zLimit
  2100   2320             );
         2321  +          sqlite3_free(zStart);
  2101   2322           }else
  2102   2323   
  2103   2324           if( pIter->eType==RBU_PK_EXTERNAL || pIter->eType==RBU_PK_NONE ){
  2104   2325             zSql = sqlite3_mprintf(
  2105   2326                 "SELECT %s, rbu_control FROM %s.'rbu_tmp_%q' %s ORDER BY %s%s",
  2106   2327                 zCollist, p->zStateDb, pIter->zDataTbl,
  2107   2328                 zPart, zCollist, zLimit
................................................................................
  2116   2337                 zCollist, p->zStateDb, pIter->zDataTbl, zPart,
  2117   2338                 zCollist, pIter->zDataTbl, 
  2118   2339                 zPart,
  2119   2340                 (zPart ? "AND" : "WHERE"),
  2120   2341                 zCollist, zLimit
  2121   2342             );
  2122   2343           }
  2123         -        p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz, zSql);
         2344  +        if( p->rc==SQLITE_OK ){
         2345  +          p->rc = prepareFreeAndCollectError(p->dbRbu,&pIter->pSelect,pz,zSql);
         2346  +        }else{
         2347  +          sqlite3_free(zSql);
         2348  +        }
  2124   2349         }
  2125   2350   
  2126   2351         sqlite3_free(zImposterCols);
  2127   2352         sqlite3_free(zImposterPK);
  2128   2353         sqlite3_free(zWhere);
  2129   2354         sqlite3_free(zBind);
  2130   2355         sqlite3_free(zPart);
................................................................................
  2216   2441   
  2217   2442           rbuObjIterPrepareTmpInsert(p, pIter, zCollist, zRbuRowid);
  2218   2443         }
  2219   2444   
  2220   2445         /* Create the SELECT statement to read keys from data_xxx */
  2221   2446         if( p->rc==SQLITE_OK ){
  2222   2447           const char *zRbuRowid = "";
         2448  +        char *zStart = 0;
         2449  +        char *zOrder = 0;
  2223   2450           if( bRbuRowid ){
  2224   2451             zRbuRowid = rbuIsVacuum(p) ? ",_rowid_ " : ",rbu_rowid";
  2225   2452           }
  2226         -        p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
  2227         -            sqlite3_mprintf(
  2228         -              "SELECT %s,%s rbu_control%s FROM '%q'%s", 
  2229         -              zCollist, 
  2230         -              (rbuIsVacuum(p) ? "0 AS " : ""),
  2231         -              zRbuRowid,
  2232         -              pIter->zDataTbl, zLimit
  2233         -            )
  2234         -        );
         2453  +
         2454  +        if( rbuIsVacuum(p) ){
         2455  +          if( nOffset ){
         2456  +            zStart = rbuVacuumTableStart(p, pIter, bRbuRowid, zWrite);
         2457  +            if( zStart ){
         2458  +              sqlite3_free(zLimit);
         2459  +              zLimit = 0;
         2460  +            }
         2461  +          }
         2462  +          if( bRbuRowid ){
         2463  +            zOrder = rbuMPrintf(p, "_rowid_");
         2464  +          }else{
         2465  +            zOrder = rbuObjIterGetPkList(p, pIter, "", ", ", "");
         2466  +          }
         2467  +        }
         2468  +
         2469  +        if( p->rc==SQLITE_OK ){
         2470  +          p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
         2471  +              sqlite3_mprintf(
         2472  +                "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
         2473  +                zCollist, 
         2474  +                (rbuIsVacuum(p) ? "0 AS " : ""),
         2475  +                zRbuRowid,
         2476  +                pIter->zDataTbl, (zStart ? zStart : ""), 
         2477  +                (zOrder ? "ORDER BY" : ""), zOrder,
         2478  +                zLimit
         2479  +              )
         2480  +          );
         2481  +        }
         2482  +        sqlite3_free(zStart);
         2483  +        sqlite3_free(zOrder);
  2235   2484         }
  2236   2485   
  2237   2486         sqlite3_free(zWhere);
  2238   2487         sqlite3_free(zOldlist);
  2239   2488         sqlite3_free(zNewlist);
  2240   2489         sqlite3_free(zBindings);
  2241   2490       }
................................................................................
  4454   4703         rc = xControl(p->pReal, SQLITE_FCNTL_ZIPVFS, &dummy);
  4455   4704         if( rc==SQLITE_OK ){
  4456   4705           rc = SQLITE_ERROR;
  4457   4706           pRbu->zErrmsg = sqlite3_mprintf("rbu/zipvfs setup error");
  4458   4707         }else if( rc==SQLITE_NOTFOUND ){
  4459   4708           pRbu->pTargetFd = p;
  4460   4709           p->pRbu = pRbu;
  4461         -        if( p->openFlags & SQLITE_OPEN_MAIN_DB ){
  4462         -          rbuMainlistAdd(p);
  4463         -        }
         4710  +        rbuMainlistAdd(p);
  4464   4711           if( p->pWalFd ) p->pWalFd->pRbu = pRbu;
  4465   4712           rc = SQLITE_OK;
  4466   4713         }
  4467   4714       }
  4468   4715       return rc;
  4469   4716     }
  4470   4717     else if( op==SQLITE_FCNTL_RBUCNT ){
................................................................................
  4519   4766       /* Magic number 1 is the WAL_CKPT_LOCK lock. Preventing SQLite from
  4520   4767       ** taking this lock also prevents any checkpoints from occurring. 
  4521   4768       ** todo: really, it's not clear why this might occur, as 
  4522   4769       ** wal_autocheckpoint ought to be turned off.  */
  4523   4770       if( ofst==WAL_LOCK_CKPT && n==1 ) rc = SQLITE_BUSY;
  4524   4771     }else{
  4525   4772       int bCapture = 0;
  4526         -    if( n==1 && (flags & SQLITE_SHM_EXCLUSIVE)
  4527         -     && pRbu && pRbu->eStage==RBU_STAGE_CAPTURE
  4528         -     && (ofst==WAL_LOCK_WRITE || ofst==WAL_LOCK_CKPT || ofst==WAL_LOCK_READ0)
  4529         -    ){
         4773  +    if( pRbu && pRbu->eStage==RBU_STAGE_CAPTURE ){
  4530   4774         bCapture = 1;
  4531   4775       }
  4532   4776   
  4533   4777       if( bCapture==0 || 0==(flags & SQLITE_SHM_UNLOCK) ){
  4534   4778         rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags);
  4535   4779         if( bCapture && rc==SQLITE_OK ){
  4536   4780           pRbu->mLock |= (1 << ofst);
................................................................................
  4555   4799     int rc = SQLITE_OK;
  4556   4800     int eStage = (p->pRbu ? p->pRbu->eStage : 0);
  4557   4801   
  4558   4802     /* If not in RBU_STAGE_OAL, allow this call to pass through. Or, if this
  4559   4803     ** rbu is in the RBU_STAGE_OAL state, use heap memory for *-shm space 
  4560   4804     ** instead of a file on disk.  */
  4561   4805     assert( p->openFlags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_TEMP_DB) );
  4562         -  if( eStage==RBU_STAGE_OAL || eStage==RBU_STAGE_MOVE ){
  4563         -    if( iRegion<=p->nShm ){
  4564         -      sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
  4565         -      char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
  4566         -      if( apNew==0 ){
  4567         -        rc = SQLITE_NOMEM;
  4568         -      }else{
  4569         -        memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
  4570         -        p->apShm = apNew;
  4571         -        p->nShm = iRegion+1;
  4572         -      }
         4806  +  if( eStage==RBU_STAGE_OAL ){
         4807  +    sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
         4808  +    char **apNew = (char**)sqlite3_realloc64(p->apShm, nByte);
         4809  +
         4810  +    /* This is an RBU connection that uses its own heap memory for the
         4811  +    ** pages of the *-shm file. Since no other process can have run
         4812  +    ** recovery, the connection must request *-shm pages in order
         4813  +    ** from start to finish.  */
         4814  +    assert( iRegion==p->nShm );
         4815  +    if( apNew==0 ){
         4816  +      rc = SQLITE_NOMEM;
         4817  +    }else{
         4818  +      memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
         4819  +      p->apShm = apNew;
         4820  +      p->nShm = iRegion+1;
  4573   4821       }
  4574   4822   
  4575         -    if( rc==SQLITE_OK && p->apShm[iRegion]==0 ){
         4823  +    if( rc==SQLITE_OK ){
  4576   4824         char *pNew = (char*)sqlite3_malloc64(szRegion);
  4577   4825         if( pNew==0 ){
  4578   4826           rc = SQLITE_NOMEM;
  4579   4827         }else{
  4580   4828           memset(pNew, 0, szRegion);
  4581   4829           p->apShm[iRegion] = pNew;
  4582   4830         }
................................................................................
  4797   5045     **   b) if the *-wal file does not exist, claim that it does anyway,
  4798   5046     **      causing SQLite to call xOpen() to open it. This call will also
  4799   5047     **      be intercepted (see the rbuVfsOpen() function) and the *-oal
  4800   5048     **      file opened instead.
  4801   5049     */
  4802   5050     if( rc==SQLITE_OK && flags==SQLITE_ACCESS_EXISTS ){
  4803   5051       rbu_file *pDb = rbuFindMaindb(pRbuVfs, zPath, 1);
  4804         -    if( pDb && pDb->pRbu && pDb->pRbu->eStage==RBU_STAGE_OAL ){
         5052  +    if( pDb && pDb->pRbu->eStage==RBU_STAGE_OAL ){
         5053  +      assert( pDb->pRbu );
  4805   5054         if( *pResOut ){
  4806   5055           rc = SQLITE_CANTOPEN;
  4807   5056         }else{
  4808   5057           sqlite3_int64 sz = 0;
  4809   5058           rc = rbuVfsFileSize(&pDb->base, &sz);
  4810   5059           *pResOut = (sz>0);
  4811   5060         }

Changes to main.mk.

   736    736   	$(TOP)/ext/misc/fileio.c \
   737    737   	$(TOP)/ext/misc/completion.c \
   738    738   	$(TOP)/ext/misc/sqlar.c \
   739    739   	$(TOP)/ext/expert/sqlite3expert.c \
   740    740   	$(TOP)/ext/expert/sqlite3expert.h \
   741    741   	$(TOP)/ext/misc/zipfile.c \
   742    742   	$(TOP)/ext/misc/memtrace.c \
          743  +	$(TOP)/ext/misc/dbdata.c \
   743    744           $(TOP)/src/test_windirent.c
   744    745   
   745    746   shell.c:	$(SHELL_SRC) $(TOP)/tool/mkshellc.tcl
   746    747   	tclsh $(TOP)/tool/mkshellc.tcl >shell.c
   747    748   
   748    749   
   749    750   

Changes to src/alter.c.

    50     50   ** statement to ensure that the operation has not rendered any schema
    51     51   ** objects unusable.
    52     52   */
    53     53   static void renameTestSchema(Parse *pParse, const char *zDb, int bTemp){
    54     54     sqlite3NestedParse(pParse, 
    55     55         "SELECT 1 "
    56     56         "FROM \"%w\".%s "
    57         -      "WHERE name NOT LIKE 'sqlite_%%'"
           57  +      "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
    58     58         " AND sql NOT LIKE 'create virtual%%'"
    59     59         " AND sqlite_rename_test(%Q, sql, type, name, %d)=NULL ",
    60     60         zDb, MASTER_NAME, 
    61     61         zDb, bTemp
    62     62     );
    63     63   
    64     64     if( bTemp==0 ){
    65     65       sqlite3NestedParse(pParse, 
    66     66           "SELECT 1 "
    67     67           "FROM temp.%s "
    68         -        "WHERE name NOT LIKE 'sqlite_%%'"
           68  +        "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
    69     69           " AND sql NOT LIKE 'create virtual%%'"
    70     70           " AND sqlite_rename_test(%Q, sql, type, name, 1)=NULL ",
    71     71           MASTER_NAME, zDb 
    72     72       );
    73     73     }
    74     74   }
    75     75   
................................................................................
   182    182   
   183    183     /* Rewrite all CREATE TABLE, INDEX, TRIGGER or VIEW statements in
   184    184     ** the schema to use the new table name.  */
   185    185     sqlite3NestedParse(pParse, 
   186    186         "UPDATE \"%w\".%s SET "
   187    187         "sql = sqlite_rename_table(%Q, type, name, sql, %Q, %Q, %d) "
   188    188         "WHERE (type!='index' OR tbl_name=%Q COLLATE nocase)"
   189         -      "AND   name NOT LIKE 'sqlite_%%'"
          189  +      "AND   name NOT LIKE 'sqliteX_%%' ESCAPE 'X'"
   190    190         , zDb, MASTER_NAME, zDb, zTabName, zName, (iDb==1), zTabName
   191    191     );
   192    192   
   193    193     /* Update the tbl_name and name columns of the sqlite_master table
   194    194     ** as required.  */
   195    195     sqlite3NestedParse(pParse,
   196    196         "UPDATE %Q.%s SET "
   197    197             "tbl_name = %Q, "
   198    198             "name = CASE "
   199    199               "WHEN type='table' THEN %Q "
   200         -            "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
          200  +            "WHEN name LIKE 'sqliteX_autoindex%%' ESCAPE 'X' "
          201  +            "     AND type='index' THEN "
   201    202                "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
   202    203               "ELSE name END "
   203    204         "WHERE tbl_name=%Q COLLATE nocase AND "
   204    205             "(type='table' OR type='index' OR type='trigger');", 
   205    206         zDb, MASTER_NAME, 
   206    207         zName, zName, zName, 
   207    208         nTabName, zTabName
................................................................................
   567    568     zNew = sqlite3NameFromToken(db, pNew);
   568    569     if( !zNew ) goto exit_rename_column;
   569    570     assert( pNew->n>0 );
   570    571     bQuote = sqlite3Isquote(pNew->z[0]);
   571    572     sqlite3NestedParse(pParse, 
   572    573         "UPDATE \"%w\".%s SET "
   573    574         "sql = sqlite_rename_column(sql, type, name, %Q, %Q, %d, %Q, %d, %d) "
   574         -      "WHERE name NOT LIKE 'sqlite_%%' AND (type != 'index' OR tbl_name = %Q)"
          575  +      "WHERE name NOT LIKE 'sqliteX_%%' ESCAPE 'X' "
          576  +      " AND (type != 'index' OR tbl_name = %Q)"
   575    577         " AND sql NOT LIKE 'create virtual%%'",
   576    578         zDb, MASTER_NAME, 
   577    579         zDb, pTab->zName, iCol, zNew, bQuote, iSchema==1,
   578    580         pTab->zName
   579    581     );
   580    582   
   581    583     sqlite3NestedParse(pParse, 

Changes to src/attach.c.

   237    237       if( !REOPEN_AS_MEMDB(db) ){
   238    238         rc = sqlite3Init(db, &zErrDyn);
   239    239       }
   240    240       sqlite3BtreeLeaveAll(db);
   241    241       assert( zErrDyn==0 || rc!=SQLITE_OK );
   242    242     }
   243    243   #ifdef SQLITE_USER_AUTHENTICATION
   244         -  if( rc==SQLITE_OK ){
          244  +  if( rc==SQLITE_OK && !REOPEN_AS_MEMDB(db) ){
   245    245       u8 newAuth = 0;
   246    246       rc = sqlite3UserAuthCheckLogin(db, zName, &newAuth);
   247    247       if( newAuth<db->auth.authLevel ){
   248    248         rc = SQLITE_AUTH_USER;
   249    249       }
   250    250     }
   251    251   #endif

Changes to src/backup.c.

   270    270     ** between source and destination.  If there is a difference, try to
   271    271     ** fix the destination to agree with the source.  If that is not possible,
   272    272     ** then the backup cannot proceed.
   273    273     */
   274    274     if( nSrcReserve!=nDestReserve ){
   275    275       u32 newPgsz = nSrcPgsz;
   276    276       rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
   277         -    if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
          277  +    if( rc==SQLITE_OK && newPgsz!=(u32)nSrcPgsz ) rc = SQLITE_READONLY;
   278    278     }
   279    279   #endif
   280    280   
   281    281     /* This loop runs once for each destination page spanned by the source 
   282    282     ** page. For each iteration, variable iOff is set to the byte offset
   283    283     ** of the destination page.
   284    284     */

Changes to src/btree.c.

  1864   1864     assert( gap<=65536 );
  1865   1865     /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
  1866   1866     ** and the reserved space is zero (the usual value for reserved space)
  1867   1867     ** then the cell content offset of an empty page wants to be 65536.
  1868   1868     ** However, that integer is too large to be stored in a 2-byte unsigned
  1869   1869     ** integer, so a value of 0 is used in its place. */
  1870   1870     top = get2byte(&data[hdr+5]);
  1871         -  assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
         1871  +  assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
  1872   1872     if( gap>top ){
  1873   1873       if( top==0 && pPage->pBt->usableSize==65536 ){
  1874   1874         top = 65536;
  1875   1875       }else{
  1876   1876         return SQLITE_CORRUPT_PAGE(pPage);
  1877   1877       }
  1878   1878     }
................................................................................
  2161   2161     /* At this point, nFree contains the sum of the offset to the start
  2162   2162     ** of the cell-content area plus the number of free bytes within
  2163   2163     ** the cell-content area. If this is greater than the usable-size
  2164   2164     ** of the page, then the page must be corrupted. This check also
  2165   2165     ** serves to verify that the offset to the start of the cell-content
  2166   2166     ** area, according to the page header, lies within the page.
  2167   2167     */
  2168         -  if( nFree>usableSize ){
         2168  +  if( nFree>usableSize || nFree<iCellFirst ){
  2169   2169       return SQLITE_CORRUPT_PAGE(pPage);
  2170   2170     }
  2171   2171     pPage->nFree = (u16)(nFree - iCellFirst);
  2172   2172     return SQLITE_OK;
  2173   2173   }
  2174   2174   
  2175   2175   /*
................................................................................
  4595   4595         }
  4596   4596         btreeReleaseAllCursorPages(p);
  4597   4597       }
  4598   4598       sqlite3BtreeLeave(pBtree);
  4599   4599     }
  4600   4600     return rc;
  4601   4601   }
         4602  +
         4603  +/*
         4604  +** Set the pBt->nPage field correctly, according to the current
         4605  +** state of the database.  Assume pBt->pPage1 is valid.
         4606  +*/
         4607  +static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
         4608  +  int nPage = get4byte(&pPage1->aData[28]);
         4609  +  testcase( nPage==0 );
         4610  +  if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
         4611  +  testcase( pBt->nPage!=nPage );
         4612  +  pBt->nPage = nPage;
         4613  +}
  4602   4614   
  4603   4615   /*
  4604   4616   ** Rollback the transaction in progress.
  4605   4617   **
  4606   4618   ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
  4607   4619   ** Only write cursors are tripped if writeOnly is true but all cursors are
  4608   4620   ** tripped if writeOnly is false.  Any attempt to use
................................................................................
  4641   4653         rc = rc2;
  4642   4654       }
  4643   4655   
  4644   4656       /* The rollback may have destroyed the pPage1->aData value.  So
  4645   4657       ** call btreeGetPage() on page 1 again to make
  4646   4658       ** sure pPage1->aData is set correctly. */
  4647   4659       if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
  4648         -      int nPage = get4byte(28+(u8*)pPage1->aData);
  4649         -      testcase( nPage==0 );
  4650         -      if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
  4651         -      testcase( pBt->nPage!=nPage );
  4652         -      pBt->nPage = nPage;
         4660  +      btreeSetNPage(pBt, pPage1);
  4653   4661         releasePageOne(pPage1);
  4654   4662       }
  4655   4663       assert( countValidCursors(pBt, 1)==0 );
  4656   4664       pBt->inTransaction = TRANS_READ;
  4657   4665       btreeClearHasContent(pBt);
  4658   4666     }
  4659   4667   
................................................................................
  4729   4737         rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
  4730   4738       }
  4731   4739       if( rc==SQLITE_OK ){
  4732   4740         if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
  4733   4741           pBt->nPage = 0;
  4734   4742         }
  4735   4743         rc = newDatabase(pBt);
  4736         -      pBt->nPage = get4byte(28 + pBt->pPage1->aData);
         4744  +      btreeSetNPage(pBt, pBt->pPage1);
  4737   4745   
  4738         -      /* The database size was written into the offset 28 of the header
  4739         -      ** when the transaction started, so we know that the value at offset
  4740         -      ** 28 is nonzero. */
  4741         -      assert( pBt->nPage>0 );
         4746  +      /* pBt->nPage might be zero if the database was corrupt when 
         4747  +      ** the transaction was started. Otherwise, it must be at least 1.  */
         4748  +      assert( CORRUPT_DB || pBt->nPage>0 );
  4742   4749       }
  4743   4750       sqlite3BtreeLeave(p);
  4744   4751     }
  4745   4752     return rc;
  4746   4753   }
  4747   4754   
  4748   4755   /*
................................................................................
  5749   5756       int ii;
  5750   5757       for(ii=0; ii<pCur->iPage; ii++){
  5751   5758         assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
  5752   5759       }
  5753   5760       assert( pCur->ix==pCur->pPage->nCell-1 );
  5754   5761       assert( pCur->pPage->leaf );
  5755   5762   #endif
         5763  +    *pRes = 0;
  5756   5764       return SQLITE_OK;
  5757   5765     }
  5758   5766   
  5759   5767     rc = moveToRoot(pCur);
  5760   5768     if( rc==SQLITE_OK ){
  5761   5769       assert( pCur->eState==CURSOR_VALID );
  5762   5770       *pRes = 0;
................................................................................
  8101   8109     for(i=0; i<nOld; i++){
  8102   8110       MemPage *pOld = apOld[i];
  8103   8111       int limit = pOld->nCell;
  8104   8112       u8 *aData = pOld->aData;
  8105   8113       u16 maskPage = pOld->maskPage;
  8106   8114       u8 *piCell = aData + pOld->cellOffset;
  8107   8115       u8 *piEnd;
         8116  +    VVA_ONLY( int nCellAtStart = b.nCell; )
  8108   8117   
  8109   8118       /* Verify that all sibling pages are of the same "type" (table-leaf,
  8110   8119       ** table-interior, index-leaf, or index-interior).
  8111   8120       */
  8112   8121       if( pOld->aData[0]!=apOld[0]->aData[0] ){
  8113   8122         rc = SQLITE_CORRUPT_BKPT;
  8114   8123         goto balance_cleanup;
................................................................................
  8129   8138       ** This must be done in advance.  Once the balance starts, the cell
  8130   8139       ** offset section of the btree page will be overwritten and we will no
  8131   8140       ** long be able to find the cells if a pointer to each cell is not saved
  8132   8141       ** first.
  8133   8142       */
  8134   8143       memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
  8135   8144       if( pOld->nOverflow>0 ){
         8145  +      if( limit<pOld->aiOvfl[0] ){
         8146  +        rc = SQLITE_CORRUPT_BKPT;
         8147  +        goto balance_cleanup;
         8148  +      }
  8136   8149         limit = pOld->aiOvfl[0];
  8137   8150         for(j=0; j<limit; j++){
  8138   8151           b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
  8139   8152           piCell += 2;
  8140   8153           b.nCell++;
  8141   8154         }
  8142   8155         for(k=0; k<pOld->nOverflow; k++){
................................................................................
  8148   8161       piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
  8149   8162       while( piCell<piEnd ){
  8150   8163         assert( b.nCell<nMaxCells );
  8151   8164         b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
  8152   8165         piCell += 2;
  8153   8166         b.nCell++;
  8154   8167       }
         8168  +    assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
  8155   8169   
  8156   8170       cntOld[i] = b.nCell;
  8157   8171       if( i<nOld-1 && !leafData){
  8158   8172         u16 sz = (u16)szNew[i];
  8159   8173         u8 *pTemp;
  8160   8174         assert( b.nCell<nMaxCells );
  8161   8175         b.szCell[b.nCell] = sz;
................................................................................
  8448   8462       int iOld = 0;
  8449   8463   
  8450   8464       for(i=0; i<b.nCell; i++){
  8451   8465         u8 *pCell = b.apCell[i];
  8452   8466         while( i==cntOldNext ){
  8453   8467           iOld++;
  8454   8468           assert( iOld<nNew || iOld<nOld );
         8469  +        assert( iOld>=0 && iOld<NB );
  8455   8470           pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
  8456   8471           cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
  8457   8472         }
  8458   8473         if( i==cntNew[iNew] ){
  8459   8474           pNew = apNew[++iNew];
  8460   8475           if( !leafData ) continue;
  8461   8476         }

Changes to src/build.c.

  1327   1327   **     CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
  1328   1328   **     CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
  1329   1329   **
  1330   1330   ** This is goofy.  But to preserve backwards compatibility we continue to
  1331   1331   ** accept it.  This routine does the necessary conversion.  It converts
  1332   1332   ** the expression given in its argument from a TK_STRING into a TK_ID
  1333   1333   ** if the expression is just a TK_STRING with an optional COLLATE clause.
  1334         -** If the epxression is anything other than TK_STRING, the expression is
         1334  +** If the expression is anything other than TK_STRING, the expression is
  1335   1335   ** unchanged.
  1336   1336   */
  1337   1337   static void sqlite3StringToId(Expr *p){
  1338   1338     if( p->op==TK_STRING ){
  1339   1339       p->op = TK_ID;
  1340   1340     }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
  1341   1341       p->pLeft->op = TK_ID;
................................................................................
  1724   1724       i16 x = pIdx->aiColumn[i];
  1725   1725       assert( x<pIdx->pTable->nCol );
  1726   1726       wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
  1727   1727     }
  1728   1728     pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
  1729   1729   }
  1730   1730   
  1731         -/* Return true if value x is found any of the first nCol entries of aiCol[]
         1731  +/* Return true if column number x is any of the first nCol entries of aiCol[].
         1732  +** This is used to determine if the column number x appears in any of the
         1733  +** first nCol entries of an index.
  1732   1734   */
  1733   1735   static int hasColumn(const i16 *aiCol, int nCol, int x){
  1734         -  while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
         1736  +  while( nCol-- > 0 ){
         1737  +    assert( aiCol[0]>=0 );
         1738  +    if( x==*(aiCol++) ){
         1739  +      return 1;
         1740  +    }
         1741  +  }
         1742  +  return 0;
         1743  +}
         1744  +
         1745  +/*
         1746  +** Return true if any of the first nKey entries of index pIdx exactly
         1747  +** match the iCol-th entry of pPk.  pPk is always a WITHOUT ROWID
         1748  +** PRIMARY KEY index.  pIdx is an index on the same table.  pIdx may
         1749  +** or may not be the same index as pPk.
         1750  +**
         1751  +** The first nKey entries of pIdx are guaranteed to be ordinary columns,
         1752  +** not a rowid or expression.
         1753  +**
         1754  +** This routine differs from hasColumn() in that both the column and the
         1755  +** collating sequence must match for this routine, but for hasColumn() only
         1756  +** the column name must match.
         1757  +*/
         1758  +static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
         1759  +  int i, j;
         1760  +  assert( nKey<=pIdx->nColumn );
         1761  +  assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
         1762  +  assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
         1763  +  assert( pPk->pTable->tabFlags & TF_WithoutRowid );
         1764  +  assert( pPk->pTable==pIdx->pTable );
         1765  +  testcase( pPk==pIdx );
         1766  +  j = pPk->aiColumn[iCol];
         1767  +  assert( j!=XN_ROWID && j!=XN_EXPR );
         1768  +  for(i=0; i<nKey; i++){
         1769  +    assert( pIdx->aiColumn[i]>=0 || j>=0 );
         1770  +    if( pIdx->aiColumn[i]==j 
         1771  +     && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
         1772  +    ){
         1773  +      return 1;
         1774  +    }
         1775  +  }
  1735   1776     return 0;
  1736   1777   }
  1737   1778   
  1738   1779   /* Recompute the colNotIdxed field of the Index.
  1739   1780   **
  1740   1781   ** colNotIdxed is a bitmask that has a 0 bit representing each indexed
  1741   1782   ** columns that are within the first 63 columns of the table.  The
................................................................................
  1816   1857     if( pTab->iPKey>=0 ){
  1817   1858       ExprList *pList;
  1818   1859       Token ipkToken;
  1819   1860       sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
  1820   1861       pList = sqlite3ExprListAppend(pParse, 0, 
  1821   1862                     sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
  1822   1863       if( pList==0 ) return;
         1864  +    if( IN_RENAME_OBJECT ){
         1865  +      sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
         1866  +    }
  1823   1867       pList->a[0].sortOrder = pParse->iPkSortOrder;
  1824   1868       assert( pParse->pNewTable==pTab );
         1869  +    pTab->iPKey = -1;
  1825   1870       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
  1826   1871                          SQLITE_IDXTYPE_PRIMARYKEY);
  1827   1872       if( db->mallocFailed || pParse->nErr ) return;
  1828   1873       pPk = sqlite3PrimaryKeyIndex(pTab);
  1829         -    pTab->iPKey = -1;
  1830   1874     }else{
  1831   1875       pPk = sqlite3PrimaryKeyIndex(pTab);
  1832   1876       assert( pPk!=0 );
  1833   1877   
  1834   1878       /*
  1835   1879       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
  1836   1880       ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
  1837   1881       ** code assumes the PRIMARY KEY contains no repeated columns.
  1838   1882       */
  1839   1883       for(i=j=1; i<pPk->nKeyCol; i++){
  1840         -      if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
         1884  +      if( isDupColumn(pPk, j, pPk, i) ){
  1841   1885           pPk->nColumn--;
  1842   1886         }else{
         1887  +        testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
  1843   1888           pPk->aiColumn[j++] = pPk->aiColumn[i];
  1844   1889         }
  1845   1890       }
  1846   1891       pPk->nKeyCol = j;
  1847   1892     }
  1848   1893     assert( pPk!=0 );
  1849   1894     pPk->isCovering = 1;
................................................................................
  1865   1910     /* Update the in-memory representation of all UNIQUE indices by converting
  1866   1911     ** the final rowid column into one or more columns of the PRIMARY KEY.
  1867   1912     */
  1868   1913     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1869   1914       int n;
  1870   1915       if( IsPrimaryKeyIndex(pIdx) ) continue;
  1871   1916       for(i=n=0; i<nPk; i++){
  1872         -      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
         1917  +      if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
         1918  +        testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
         1919  +        n++;
         1920  +      }
  1873   1921       }
  1874   1922       if( n==0 ){
  1875   1923         /* This index is a superset of the primary key */
  1876   1924         pIdx->nColumn = pIdx->nKeyCol;
  1877   1925         continue;
  1878   1926       }
  1879   1927       if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
  1880   1928       for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
  1881         -      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
         1929  +      if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
         1930  +        testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
  1882   1931           pIdx->aiColumn[j] = pPk->aiColumn[i];
  1883   1932           pIdx->azColl[j] = pPk->azColl[i];
         1933  +        if( pPk->aSortOrder[i] ){
         1934  +          /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
         1935  +          pIdx->bAscKeyBug = 1;
         1936  +        }
  1884   1937           j++;
  1885   1938         }
  1886   1939       }
  1887   1940       assert( pIdx->nColumn>=pIdx->nKeyCol+n );
  1888   1941       assert( pIdx->nColumn>=j );
  1889   1942     }
  1890   1943   
................................................................................
  2998   3051                            pIndex->nKeyCol); VdbeCoverage(v);
  2999   3052       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  3000   3053       sqlite3VdbeJumpHere(v, j2);
  3001   3054     }else{
  3002   3055       addr2 = sqlite3VdbeCurrentAddr(v);
  3003   3056     }
  3004   3057     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
  3005         -  sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
         3058  +  if( !pIndex->bAscKeyBug ){
         3059  +    /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
         3060  +    ** faster by avoiding unnecessary seeks.  But the optimization does
         3061  +    ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
         3062  +    ** with DESC primary keys, since those indexes have there keys in
         3063  +    ** a different order from the main table.
         3064  +    ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
         3065  +    */
         3066  +    sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
         3067  +  }
  3006   3068     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
  3007   3069     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  3008   3070     sqlite3ReleaseTempReg(pParse, regRecord);
  3009   3071     sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
  3010   3072     sqlite3VdbeJumpHere(v, addr1);
  3011   3073   
  3012   3074     sqlite3VdbeAddOp1(v, OP_Close, iTab);
................................................................................
  3390   3452     ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
  3391   3453     ** normal tables (when pPk==0) this will be the rowid.
  3392   3454     */
  3393   3455     if( pPk ){
  3394   3456       for(j=0; j<pPk->nKeyCol; j++){
  3395   3457         int x = pPk->aiColumn[j];
  3396   3458         assert( x>=0 );
  3397         -      if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
         3459  +      if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
  3398   3460           pIndex->nColumn--; 
  3399   3461         }else{
         3462  +        testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
  3400   3463           pIndex->aiColumn[i] = x;
  3401   3464           pIndex->azColl[i] = pPk->azColl[j];
  3402   3465           pIndex->aSortOrder[i] = pPk->aSortOrder[j];
  3403   3466           i++;
  3404   3467         }
  3405   3468       }
  3406   3469       assert( i==pIndex->nColumn );

Changes to src/expr.c.

   769    769     pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
   770    770     if( pNew ){
   771    771       memset(pNew, 0, sizeof(Expr));
   772    772       pNew->op = (u8)op;
   773    773       pNew->iAgg = -1;
   774    774       if( pToken ){
   775    775         if( nExtra==0 ){
   776         -        pNew->flags |= EP_IntValue|EP_Leaf;
          776  +        pNew->flags |= EP_IntValue|EP_Leaf|(iValue?EP_IsTrue:EP_IsFalse);
   777    777           pNew->u.iValue = iValue;
   778    778         }else{
   779    779           pNew->u.zToken = (char*)&pNew[1];
   780    780           assert( pToken->z!=0 || pToken->n==0 );
   781    781           if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
   782    782           pNew->u.zToken[pToken->n] = 0;
   783    783           if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){
................................................................................
   846    846   Expr *sqlite3PExpr(
   847    847     Parse *pParse,          /* Parsing context */
   848    848     int op,                 /* Expression opcode */
   849    849     Expr *pLeft,            /* Left operand */
   850    850     Expr *pRight            /* Right operand */
   851    851   ){
   852    852     Expr *p;
   853         -  if( op==TK_AND && pParse->nErr==0 && !IN_RENAME_OBJECT ){
   854         -    /* Take advantage of short-circuit false optimization for AND */
   855         -    p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
   856         -  }else{
   857         -    p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
   858         -    if( p ){
   859         -      memset(p, 0, sizeof(Expr));
   860         -      p->op = op & 0xff;
   861         -      p->iAgg = -1;
   862         -    }
          853  +  p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
          854  +  if( p ){
          855  +    memset(p, 0, sizeof(Expr));
          856  +    p->op = op & 0xff;
          857  +    p->iAgg = -1;
   863    858       sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
   864         -  }
   865         -  if( p ) {
   866    859       sqlite3ExprCheckHeight(pParse, p->nHeight);
          860  +  }else{
          861  +    sqlite3ExprDelete(pParse->db, pLeft);
          862  +    sqlite3ExprDelete(pParse->db, pRight);
   867    863     }
   868    864     return p;
   869    865   }
   870    866   
   871    867   /*
   872    868   ** Add pSelect to the Expr.x.pSelect field.  Or, if pExpr is NULL (due
   873    869   ** do a memory allocation failure) then delete the pSelect object.
................................................................................
   880    876     }else{
   881    877       assert( pParse->db->mallocFailed );
   882    878       sqlite3SelectDelete(pParse->db, pSelect);
   883    879     }
   884    880   }
   885    881   
   886    882   
   887         -/*
   888         -** If the expression is always either TRUE or FALSE (respectively),
   889         -** then return 1.  If one cannot determine the truth value of the
   890         -** expression at compile-time return 0.
   891         -**
   892         -** This is an optimization.  If is OK to return 0 here even if
   893         -** the expression really is always false or false (a false negative).
   894         -** But it is a bug to return 1 if the expression might have different
   895         -** boolean values in different circumstances (a false positive.)
   896         -**
   897         -** Note that if the expression is part of conditional for a
   898         -** LEFT JOIN, then we cannot determine at compile-time whether or not
   899         -** is it true or false, so always return 0.
   900         -*/
   901         -static int exprAlwaysTrue(Expr *p){
   902         -  int v = 0;
   903         -  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
   904         -  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
   905         -  return v!=0;
   906         -}
   907         -static int exprAlwaysFalse(Expr *p){
   908         -  int v = 0;
   909         -  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
   910         -  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
   911         -  return v==0;
   912         -}
   913         -
   914    883   /*
   915    884   ** Join two expressions using an AND operator.  If either expression is
   916    885   ** NULL, then just return the other expression.
   917    886   **
   918    887   ** If one side or the other of the AND is known to be false, then instead
   919    888   ** of returning an AND expression, just return a constant expression with
   920    889   ** a value of false.
   921    890   */
   922         -Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
   923         -  if( pLeft==0 ){
          891  +Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
          892  +  sqlite3 *db = pParse->db;
          893  +  if( pLeft==0  ){
   924    894       return pRight;
   925    895     }else if( pRight==0 ){
   926    896       return pLeft;
   927         -  }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
          897  +  }else if( pParse->nErr || IN_RENAME_OBJECT ){
          898  +    return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
          899  +  }else if( ExprAlwaysFalse(pLeft) || ExprAlwaysFalse(pRight) ){
   928    900       sqlite3ExprDelete(db, pLeft);
   929    901       sqlite3ExprDelete(db, pRight);
   930    902       return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
   931    903     }else{
   932         -    Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
   933         -    sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
   934         -    return pNew;
          904  +    return sqlite3PExpr(pParse, TK_AND, pLeft, pRight);
   935    905     }
   936    906   }
   937    907   
   938    908   /*
   939    909   ** Construct a new expression node for a function with multiple
   940    910   ** arguments.
   941    911   */
................................................................................
  1817   1787   int sqlite3ExprIdToTrueFalse(Expr *pExpr){
  1818   1788     assert( pExpr->op==TK_ID || pExpr->op==TK_STRING );
  1819   1789     if( !ExprHasProperty(pExpr, EP_Quoted)
  1820   1790      && (sqlite3StrICmp(pExpr->u.zToken, "true")==0
  1821   1791          || sqlite3StrICmp(pExpr->u.zToken, "false")==0)
  1822   1792     ){
  1823   1793       pExpr->op = TK_TRUEFALSE;
         1794  +    ExprSetProperty(pExpr, pExpr->u.zToken[4]==0 ? EP_IsTrue : EP_IsFalse);
  1824   1795       return 1;
  1825   1796     }
  1826   1797     return 0;
  1827   1798   }
  1828   1799   
  1829   1800   /*
  1830   1801   ** The argument must be a TK_TRUEFALSE Expr node.  Return 1 if it is TRUE
................................................................................
  1832   1803   */
  1833   1804   int sqlite3ExprTruthValue(const Expr *pExpr){
  1834   1805     assert( pExpr->op==TK_TRUEFALSE );
  1835   1806     assert( sqlite3StrICmp(pExpr->u.zToken,"true")==0
  1836   1807          || sqlite3StrICmp(pExpr->u.zToken,"false")==0 );
  1837   1808     return pExpr->u.zToken[4]==0;
  1838   1809   }
         1810  +
         1811  +/*
         1812  +** If pExpr is an AND or OR expression, try to simplify it by eliminating
         1813  +** terms that are always true or false.  Return the simplified expression.
         1814  +** Or return the original expression if no simplification is possible.
         1815  +**
         1816  +** Examples:
         1817  +**
         1818  +**     (x<10) AND true                =>   (x<10)
         1819  +**     (x<10) AND false               =>   false
         1820  +**     (x<10) AND (y=22 OR false)     =>   (x<10) AND (y=22)
         1821  +**     (x<10) AND (y=22 OR true)      =>   (x<10)
         1822  +**     (y=22) OR true                 =>   true
         1823  +*/
         1824  +Expr *sqlite3ExprSimplifiedAndOr(Expr *pExpr){
         1825  +  assert( pExpr!=0 );
         1826  +  if( pExpr->op==TK_AND || pExpr->op==TK_OR ){
         1827  +    Expr *pRight = sqlite3ExprSimplifiedAndOr(pExpr->pRight);
         1828  +    Expr *pLeft = sqlite3ExprSimplifiedAndOr(pExpr->pLeft);
         1829  +    if( ExprAlwaysTrue(pLeft) || ExprAlwaysFalse(pRight) ){
         1830  +      pExpr = pExpr->op==TK_AND ? pRight : pLeft;
         1831  +    }else if( ExprAlwaysTrue(pRight) || ExprAlwaysFalse(pLeft) ){
         1832  +      pExpr = pExpr->op==TK_AND ? pLeft : pRight;
         1833  +    }
         1834  +  }
         1835  +  return pExpr;
         1836  +}
  1839   1837   
  1840   1838   
  1841   1839   /*
  1842   1840   ** These routines are Walker callbacks used to check expressions to
  1843   1841   ** see if they are "constant" for some definition of constant.  The
  1844   1842   ** Walker.eCode value determines the type of "constant" we are looking
  1845   1843   ** for.
................................................................................
  2077   2075   ** If the expression p codes a constant integer that is small enough
  2078   2076   ** to fit in a 32-bit integer, return 1 and put the value of the integer
  2079   2077   ** in *pValue.  If the expression is not an integer or if it is too big
  2080   2078   ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
  2081   2079   */
  2082   2080   int sqlite3ExprIsInteger(Expr *p, int *pValue){
  2083   2081     int rc = 0;
  2084         -  if( p==0 ) return 0;  /* Can only happen following on OOM */
         2082  +  if( NEVER(p==0) ) return 0;  /* Used to only happen following on OOM */
  2085   2083   
  2086   2084     /* If an expression is an integer literal that fits in a signed 32-bit
  2087   2085     ** integer, then the EP_IntValue flag will have already been set */
  2088   2086     assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
  2089   2087              || sqlite3GetInt32(p->u.zToken, &rc)==0 );
  2090   2088   
  2091   2089     if( p->flags & EP_IntValue ){
................................................................................
  4424   4422     int r1, r2;
  4425   4423   
  4426   4424     assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
  4427   4425     if( NEVER(v==0) )     return;  /* Existence of VDBE checked by caller */
  4428   4426     if( NEVER(pExpr==0) ) return;  /* No way this can happen */
  4429   4427     op = pExpr->op;
  4430   4428     switch( op ){
  4431         -    case TK_AND: {
  4432         -      int d2 = sqlite3VdbeMakeLabel(pParse);
  4433         -      testcase( jumpIfNull==0 );
  4434         -      sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
  4435         -      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  4436         -      sqlite3VdbeResolveLabel(v, d2);
  4437         -      break;
  4438         -    }
         4429  +    case TK_AND:
  4439   4430       case TK_OR: {
  4440         -      testcase( jumpIfNull==0 );
  4441         -      sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4442         -      sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
         4431  +      Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
         4432  +      if( pAlt!=pExpr ){
         4433  +        sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
         4434  +      }else if( op==TK_AND ){
         4435  +        int d2 = sqlite3VdbeMakeLabel(pParse);
         4436  +        testcase( jumpIfNull==0 );
         4437  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
         4438  +                           jumpIfNull^SQLITE_JUMPIFNULL);
         4439  +        sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
         4440  +        sqlite3VdbeResolveLabel(v, d2);
         4441  +      }else{
         4442  +        testcase( jumpIfNull==0 );
         4443  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
         4444  +        sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
         4445  +      }
  4443   4446         break;
  4444   4447       }
  4445   4448       case TK_NOT: {
  4446   4449         testcase( jumpIfNull==0 );
  4447   4450         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4448   4451         break;
  4449   4452       }
................................................................................
  4521   4524         sqlite3VdbeGoto(v, dest);
  4522   4525         sqlite3VdbeResolveLabel(v, destIfFalse);
  4523   4526         break;
  4524   4527       }
  4525   4528   #endif
  4526   4529       default: {
  4527   4530       default_expr:
  4528         -      if( exprAlwaysTrue(pExpr) ){
         4531  +      if( ExprAlwaysTrue(pExpr) ){
  4529   4532           sqlite3VdbeGoto(v, dest);
  4530         -      }else if( exprAlwaysFalse(pExpr) ){
         4533  +      }else if( ExprAlwaysFalse(pExpr) ){
  4531   4534           /* No-op */
  4532   4535         }else{
  4533   4536           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  4534   4537           sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
  4535   4538           VdbeCoverage(v);
  4536   4539           testcase( regFree1==0 );
  4537   4540           testcase( jumpIfNull==0 );
................................................................................
  4591   4594     assert( pExpr->op!=TK_EQ || op==OP_Ne );
  4592   4595     assert( pExpr->op!=TK_LT || op==OP_Ge );
  4593   4596     assert( pExpr->op!=TK_LE || op==OP_Gt );
  4594   4597     assert( pExpr->op!=TK_GT || op==OP_Le );
  4595   4598     assert( pExpr->op!=TK_GE || op==OP_Lt );
  4596   4599   
  4597   4600     switch( pExpr->op ){
  4598         -    case TK_AND: {
  4599         -      testcase( jumpIfNull==0 );
  4600         -      sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4601         -      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4602         -      break;
  4603         -    }
         4601  +    case TK_AND:
  4604   4602       case TK_OR: {
  4605         -      int d2 = sqlite3VdbeMakeLabel(pParse);
  4606         -      testcase( jumpIfNull==0 );
  4607         -      sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
  4608         -      sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4609         -      sqlite3VdbeResolveLabel(v, d2);
         4603  +      Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
         4604  +      if( pAlt!=pExpr ){
         4605  +        sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
         4606  +      }else if( pExpr->op==TK_AND ){
         4607  +        testcase( jumpIfNull==0 );
         4608  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
         4609  +        sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
         4610  +      }else{
         4611  +        int d2 = sqlite3VdbeMakeLabel(pParse);
         4612  +        testcase( jumpIfNull==0 );
         4613  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
         4614  +                          jumpIfNull^SQLITE_JUMPIFNULL);
         4615  +        sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
         4616  +        sqlite3VdbeResolveLabel(v, d2);
         4617  +      }
  4610   4618         break;
  4611   4619       }
  4612   4620       case TK_NOT: {
  4613   4621         testcase( jumpIfNull==0 );
  4614   4622         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4615   4623         break;
  4616   4624       }
................................................................................
  4691   4699           sqlite3VdbeResolveLabel(v, destIfNull);
  4692   4700         }
  4693   4701         break;
  4694   4702       }
  4695   4703   #endif
  4696   4704       default: {
  4697   4705       default_expr: 
  4698         -      if( exprAlwaysFalse(pExpr) ){
         4706  +      if( ExprAlwaysFalse(pExpr) ){
  4699   4707           sqlite3VdbeGoto(v, dest);
  4700         -      }else if( exprAlwaysTrue(pExpr) ){
         4708  +      }else if( ExprAlwaysTrue(pExpr) ){
  4701   4709           /* no-op */
  4702   4710         }else{
  4703   4711           r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  4704   4712           sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
  4705   4713           VdbeCoverage(v);
  4706   4714           testcase( regFree1==0 );
  4707   4715           testcase( jumpIfNull==0 );
................................................................................
  4895   4903   */
  4896   4904   int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
  4897   4905     return sqlite3ExprCompare(0,
  4898   4906                sqlite3ExprSkipCollate(pA),
  4899   4907                sqlite3ExprSkipCollate(pB),
  4900   4908                iTab);
  4901   4909   }
         4910  +
         4911  +/*
         4912  +** Return non-zero if Expr p can only be true if pNN is not NULL.
         4913  +*/
         4914  +static int exprImpliesNotNull(
         4915  +  Parse *pParse,      /* Parsing context */
         4916  +  Expr *p,            /* The expression to be checked */
         4917  +  Expr *pNN,          /* The expression that is NOT NULL */
         4918  +  int iTab,           /* Table being evaluated */
         4919  +  int seenNot         /* True if p is an operand of NOT */
         4920  +){
         4921  +  assert( p );
         4922  +  assert( pNN );
         4923  +  if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ) return 1;
         4924  +  switch( p->op ){
         4925  +    case TK_IN: {
         4926  +      if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
         4927  +      assert( ExprHasProperty(p,EP_xIsSelect)
         4928  +           || (p->x.pList!=0 && p->x.pList->nExpr>0) );
         4929  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
         4930  +    }
         4931  +    case TK_BETWEEN: {
         4932  +      ExprList *pList = p->x.pList;
         4933  +      assert( pList!=0 );
         4934  +      assert( pList->nExpr==2 );
         4935  +      if( seenNot ) return 0;
         4936  +      if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, seenNot)
         4937  +       || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, seenNot)
         4938  +      ){
         4939  +        return 1;
         4940  +      }
         4941  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
         4942  +    }
         4943  +    case TK_EQ:
         4944  +    case TK_NE:
         4945  +    case TK_LT:
         4946  +    case TK_LE:
         4947  +    case TK_GT:
         4948  +    case TK_GE:
         4949  +    case TK_PLUS:
         4950  +    case TK_MINUS:
         4951  +    case TK_STAR:
         4952  +    case TK_REM:
         4953  +    case TK_BITAND:
         4954  +    case TK_BITOR:
         4955  +    case TK_SLASH:
         4956  +    case TK_LSHIFT:
         4957  +    case TK_RSHIFT: 
         4958  +    case TK_CONCAT: {
         4959  +      if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
         4960  +      /* Fall thru into the next case */
         4961  +    }
         4962  +    case TK_SPAN:
         4963  +    case TK_COLLATE:
         4964  +    case TK_BITNOT:
         4965  +    case TK_UPLUS:
         4966  +    case TK_UMINUS: {
         4967  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
         4968  +    }
         4969  +    case TK_TRUTH: {
         4970  +      if( seenNot ) return 0;
         4971  +      if( p->op2!=TK_IS ) return 0;
         4972  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
         4973  +    }
         4974  +    case TK_NOT: {
         4975  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
         4976  +    }
         4977  +  }
         4978  +  return 0;
         4979  +}
  4902   4980   
  4903   4981   /*
  4904   4982   ** Return true if we can prove the pE2 will always be true if pE1 is
  4905   4983   ** true.  Return false if we cannot complete the proof or if pE2 might
  4906   4984   ** be false.  Examples:
  4907   4985   **
  4908   4986   **     pE1: x==5       pE2: x==5             Result: true
................................................................................
  4931   5009     }
  4932   5010     if( pE2->op==TK_OR
  4933   5011      && (sqlite3ExprImpliesExpr(pParse, pE1, pE2->pLeft, iTab)
  4934   5012                || sqlite3ExprImpliesExpr(pParse, pE1, pE2->pRight, iTab) )
  4935   5013     ){
  4936   5014       return 1;
  4937   5015     }
  4938         -  if( pE2->op==TK_NOTNULL && pE1->op!=TK_ISNULL && pE1->op!=TK_IS ){
  4939         -    Expr *pX = sqlite3ExprSkipCollate(pE1->pLeft);
  4940         -    testcase( pX!=pE1->pLeft );
  4941         -    if( sqlite3ExprCompare(pParse, pX, pE2->pLeft, iTab)==0 ) return 1;
         5016  +  if( pE2->op==TK_NOTNULL
         5017  +   && exprImpliesNotNull(pParse, pE1, pE2->pLeft, iTab, 0)
         5018  +  ){
         5019  +    return 1;
  4942   5020     }
  4943   5021     return 0;
  4944   5022   }
  4945   5023   
  4946   5024   /*
  4947   5025   ** This is the Expr node callback for sqlite3ExprImpliesNotNullRow().
  4948   5026   ** If the expression node requires that the table at pWalker->iCur

Changes to src/fkey.c.

   587    587       iCol = pIdx ? pIdx->aiColumn[i] : -1;
   588    588       pLeft = exprTableRegister(pParse, pTab, regData, iCol);
   589    589       iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
   590    590       assert( iCol>=0 );
   591    591       zCol = pFKey->pFrom->aCol[iCol].zName;
   592    592       pRight = sqlite3Expr(db, TK_ID, zCol);
   593    593       pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight);
   594         -    pWhere = sqlite3ExprAnd(db, pWhere, pEq);
          594  +    pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
   595    595     }
   596    596   
   597    597     /* If the child table is the same as the parent table, then add terms
   598    598     ** to the WHERE clause that prevent this entry from being scanned.
   599    599     ** The added WHERE clause terms are like this:
   600    600     **
   601    601     **     $current_rowid!=rowid
................................................................................
   621    621         assert( pIdx!=0 );
   622    622         for(i=0; i<pIdx->nKeyCol; i++){
   623    623           i16 iCol = pIdx->aiColumn[i];
   624    624           assert( iCol>=0 );
   625    625           pLeft = exprTableRegister(pParse, pTab, regData, iCol);
   626    626           pRight = sqlite3Expr(db, TK_ID, pTab->aCol[iCol].zName);
   627    627           pEq = sqlite3PExpr(pParse, TK_IS, pLeft, pRight);
   628         -        pAll = sqlite3ExprAnd(db, pAll, pEq);
          628  +        pAll = sqlite3ExprAnd(pParse, pAll, pEq);
   629    629         }
   630    630         pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0);
   631    631       }
   632         -    pWhere = sqlite3ExprAnd(db, pWhere, pNe);
          632  +    pWhere = sqlite3ExprAnd(pParse, pWhere, pNe);
   633    633     }
   634    634   
   635    635     /* Resolve the references in the WHERE clause. */
   636    636     memset(&sNameContext, 0, sizeof(NameContext));
   637    637     sNameContext.pSrcList = pSrc;
   638    638     sNameContext.pParse = pParse;
   639    639     sqlite3ResolveExprNames(&sNameContext, pWhere);
................................................................................
  1231   1231         ** parent table are used for the comparison. */
  1232   1232         pEq = sqlite3PExpr(pParse, TK_EQ,
  1233   1233             sqlite3PExpr(pParse, TK_DOT, 
  1234   1234               sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
  1235   1235               sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
  1236   1236             sqlite3ExprAlloc(db, TK_ID, &tFromCol, 0)
  1237   1237         );
  1238         -      pWhere = sqlite3ExprAnd(db, pWhere, pEq);
         1238  +      pWhere = sqlite3ExprAnd(pParse, pWhere, pEq);
  1239   1239   
  1240   1240         /* For ON UPDATE, construct the next term of the WHEN clause.
  1241   1241         ** The final WHEN clause will be like this:
  1242   1242         **
  1243   1243         **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
  1244   1244         */
  1245   1245         if( pChanges ){
................................................................................
  1247   1247               sqlite3PExpr(pParse, TK_DOT, 
  1248   1248                 sqlite3ExprAlloc(db, TK_ID, &tOld, 0),
  1249   1249                 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0)),
  1250   1250               sqlite3PExpr(pParse, TK_DOT, 
  1251   1251                 sqlite3ExprAlloc(db, TK_ID, &tNew, 0),
  1252   1252                 sqlite3ExprAlloc(db, TK_ID, &tToCol, 0))
  1253   1253               );
  1254         -        pWhen = sqlite3ExprAnd(db, pWhen, pEq);
         1254  +        pWhen = sqlite3ExprAnd(pParse, pWhen, pEq);
  1255   1255         }
  1256   1256     
  1257   1257         if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
  1258   1258           Expr *pNew;
  1259   1259           if( action==OE_Cascade ){
  1260   1260             pNew = sqlite3PExpr(pParse, TK_DOT, 
  1261   1261               sqlite3ExprAlloc(db, TK_ID, &tNew, 0),

Changes to src/func.c.

    12     12   ** This file contains the C-language implementations for many of the SQL
    13     13   ** functions of SQLite.  (Some function, and in particular the date and
    14     14   ** time functions, are implemented separately.)
    15     15   */
    16     16   #include "sqliteInt.h"
    17     17   #include <stdlib.h>
    18     18   #include <assert.h>
           19  +#include <math.h>
    19     20   #include "vdbeInt.h"
    20     21   
    21     22   /*
    22     23   ** Return the collating function associated with a function.
    23     24   */
    24     25   static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
    25     26     VdbeOp *pOp;
................................................................................
   392    393       r = -(double)((sqlite_int64)((-r)+0.5));
   393    394     }else{
   394    395       zBuf = sqlite3_mprintf("%.*f",n,r);
   395    396       if( zBuf==0 ){
   396    397         sqlite3_result_error_nomem(context);
   397    398         return;
   398    399       }
   399         -    sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
          400  +    if( !sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8) ){
          401  +      assert( sqlite3_strglob("*Inf", zBuf)==0 );
          402  +      r = zBuf[0]=='-' ? -HUGE_VAL : +HUGE_VAL;
          403  +    } 
   400    404       sqlite3_free(zBuf);
   401    405     }
   402    406     sqlite3_result_double(context, r);
   403    407   }
   404    408   #endif
   405    409   
   406    410   /*
................................................................................
   841    845   #ifdef SQLITE_TEST
   842    846       sqlite3_like_count++;
   843    847   #endif
   844    848       sqlite3_result_int(context, 0);
   845    849       return;
   846    850     }
   847    851   #endif
   848         -  zB = sqlite3_value_text(argv[0]);
   849         -  zA = sqlite3_value_text(argv[1]);
   850    852   
   851    853     /* Limit the length of the LIKE or GLOB pattern to avoid problems
   852    854     ** of deep recursion and N*N behavior in patternCompare().
   853    855     */
   854    856     nPat = sqlite3_value_bytes(argv[0]);
   855    857     testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
   856    858     testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
   857    859     if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
   858    860       sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
   859    861       return;
   860    862     }
   861         -  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
   862         -
   863    863     if( argc==3 ){
   864    864       /* The escape character string must consist of a single UTF-8 character.
   865    865       ** Otherwise, return an error.
   866    866       */
   867    867       const unsigned char *zEsc = sqlite3_value_text(argv[2]);
   868    868       if( zEsc==0 ) return;
   869    869       if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
................................................................................
   871    871             "ESCAPE expression must be a single character", -1);
   872    872         return;
   873    873       }
   874    874       escape = sqlite3Utf8Read(&zEsc);
   875    875     }else{
   876    876       escape = pInfo->matchSet;
   877    877     }
          878  +  zB = sqlite3_value_text(argv[0]);
          879  +  zA = sqlite3_value_text(argv[1]);
   878    880     if( zA && zB ){
   879    881   #ifdef SQLITE_TEST
   880    882       sqlite3_like_count++;
   881    883   #endif
   882    884       sqlite3_result_int(context,
   883    885                         patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
   884    886     }
................................................................................
  1796   1798     assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
  1797   1799     if( rc==SQLITE_NOMEM ){
  1798   1800       sqlite3OomFault(db);
  1799   1801     }
  1800   1802   }
  1801   1803   
  1802   1804   /*
  1803         -** Set the LIKEOPT flag on the 2-argument function with the given name.
  1804         -*/
  1805         -static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
  1806         -  FuncDef *pDef;
  1807         -  pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
  1808         -  if( ALWAYS(pDef) ){
  1809         -    pDef->funcFlags |= flagVal;
  1810         -  }
  1811         -  pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0);
  1812         -  if( pDef ){
  1813         -    pDef->funcFlags |= flagVal;
  1814         -  }
  1815         -}
  1816         -
  1817         -/*
  1818         -** Register the built-in LIKE and GLOB functions.  The caseSensitive
         1805  +** Re-register the built-in LIKE functions.  The caseSensitive
  1819   1806   ** parameter determines whether or not the LIKE operator is case
  1820         -** sensitive.  GLOB is always case sensitive.
         1807  +** sensitive.
  1821   1808   */
  1822   1809   void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
  1823   1810     struct compareInfo *pInfo;
         1811  +  int flags;
  1824   1812     if( caseSensitive ){
  1825   1813       pInfo = (struct compareInfo*)&likeInfoAlt;
         1814  +    flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE;
  1826   1815     }else{
  1827   1816       pInfo = (struct compareInfo*)&likeInfoNorm;
         1817  +    flags = SQLITE_FUNC_LIKE;
  1828   1818     }
  1829   1819     sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
  1830   1820     sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
  1831         -  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
  1832         -      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0, 0, 0);
  1833         -  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
  1834         -  setLikeOptFlag(db, "like", 
  1835         -      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
         1821  +  sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
         1822  +  sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
  1836   1823   }
  1837   1824   
  1838   1825   /*
  1839   1826   ** pExpr points to an expression which implements a function.  If
  1840   1827   ** it is appropriate to apply the LIKE optimization to that function
  1841   1828   ** then set aWc[0] through aWc[2] to the wildcard characters and the
  1842   1829   ** escape character and then return TRUE.  If the function is not a 

Changes to src/insert.c.

   810    810     }
   811    811   
   812    812     /* If this is not a view, open the table and and all indices */
   813    813     if( !isView ){
   814    814       int nIdx;
   815    815       nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
   816    816                                         &iDataCur, &iIdxCur);
   817         -    aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
          817  +    aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+2));
   818    818       if( aRegIdx==0 ){
   819    819         goto insert_cleanup;
   820    820       }
   821    821       for(i=0, pIdx=pTab->pIndex; i<nIdx; pIdx=pIdx->pNext, i++){
   822    822         assert( pIdx );
   823    823         aRegIdx[i] = ++pParse->nMem;
   824    824         pParse->nMem += pIdx->nColumn;
   825    825       }
          826  +    aRegIdx[i] = ++pParse->nMem;  /* Register to store the table record */
   826    827     }
   827    828   #ifndef SQLITE_OMIT_UPSERT
   828    829     if( pUpsert ){
   829    830       if( IsVirtual(pTab) ){
   830    831         sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
   831    832                 pTab->zName);
   832    833         goto insert_cleanup;
................................................................................
  1221   1222   ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
  1222   1223   **
  1223   1224   ** The code generated by this routine will store new index entries into
  1224   1225   ** registers identified by aRegIdx[].  No index entry is created for
  1225   1226   ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
  1226   1227   ** the same as the order of indices on the linked list of indices
  1227   1228   ** at pTab->pIndex.
         1229  +**
         1230  +** (2019-05-07) The generated code also creates a new record for the
         1231  +** main table, if pTab is a rowid table, and stores that record in the
         1232  +** register identified by aRegIdx[nIdx] - in other words in the first
         1233  +** entry of aRegIdx[] past the last index.  It is important that the
         1234  +** record be generated during constraint checks to avoid affinity changes
         1235  +** to the register content that occur after constraint checks but before
         1236  +** the new record is inserted.
  1228   1237   **
  1229   1238   ** The caller must have already opened writeable cursors on the main
  1230   1239   ** table and all applicable indices (that is to say, all indices for which
  1231   1240   ** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
  1232   1241   ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
  1233   1242   ** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
  1234   1243   ** for the first index in the pTab->pIndex list.  Cursors for other indices
................................................................................
  1840   1849   
  1841   1850     /* If the IPK constraint is a REPLACE, run it last */
  1842   1851     if( ipkTop ){
  1843   1852       sqlite3VdbeGoto(v, ipkTop);
  1844   1853       VdbeComment((v, "Do IPK REPLACE"));
  1845   1854       sqlite3VdbeJumpHere(v, ipkBottom);
  1846   1855     }
         1856  +
         1857  +  /* Generate the table record */
         1858  +  if( HasRowid(pTab) ){
         1859  +    int regRec = aRegIdx[ix];
         1860  +    sqlite3VdbeAddOp3(v, OP_MakeRecord, regNewData+1, pTab->nCol, regRec);
         1861  +    sqlite3SetMakeRecordP5(v, pTab);
         1862  +    if( !bAffinityDone ){
         1863  +      sqlite3TableAffinity(v, pTab, 0);
         1864  +    }
         1865  +  }
  1847   1866   
  1848   1867     *pbMayReplace = seenReplace;
  1849   1868     VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
  1850   1869   }
  1851   1870   
  1852   1871   #ifdef SQLITE_ENABLE_NULL_TRIM
  1853   1872   /*
................................................................................
  1890   1909     int update_flags,   /* True for UPDATE, False for INSERT */
  1891   1910     int appendBias,     /* True if this is likely to be an append */
  1892   1911     int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
  1893   1912   ){
  1894   1913     Vdbe *v;            /* Prepared statements under construction */
  1895   1914     Index *pIdx;        /* An index being inserted or updated */
  1896   1915     u8 pik_flags;       /* flag values passed to the btree insert */
  1897         -  int regData;        /* Content registers (after the rowid) */
  1898         -  int regRec;         /* Register holding assembled record for the table */
  1899   1916     int i;              /* Loop counter */
  1900         -  u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
  1901   1917   
  1902   1918     assert( update_flags==0
  1903   1919          || update_flags==OPFLAG_ISUPDATE
  1904   1920          || update_flags==(OPFLAG_ISUPDATE|OPFLAG_SAVEPOSITION)
  1905   1921     );
  1906   1922   
  1907   1923     v = sqlite3GetVdbe(pParse);
  1908   1924     assert( v!=0 );
  1909   1925     assert( pTab->pSelect==0 );  /* This table is not a VIEW */
  1910   1926     for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
  1911   1927       if( aRegIdx[i]==0 ) continue;
  1912         -    bAffinityDone = 1;
  1913   1928       if( pIdx->pPartIdxWhere ){
  1914   1929         sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
  1915   1930         VdbeCoverage(v);
  1916   1931       }
  1917   1932       pik_flags = (useSeekResult ? OPFLAG_USESEEKRESULT : 0);
  1918   1933       if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
  1919   1934         assert( pParse->nested==0 );
................................................................................
  1933   1948       }
  1934   1949       sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i],
  1935   1950                            aRegIdx[i]+1,
  1936   1951                            pIdx->uniqNotNull ? pIdx->nKeyCol: pIdx->nColumn);
  1937   1952       sqlite3VdbeChangeP5(v, pik_flags);
  1938   1953     }
  1939   1954     if( !HasRowid(pTab) ) return;
  1940         -  regData = regNewData + 1;
  1941         -  regRec = sqlite3GetTempReg(pParse);
  1942         -  sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
  1943         -  sqlite3SetMakeRecordP5(v, pTab);
  1944         -  if( !bAffinityDone ){
  1945         -    sqlite3TableAffinity(v, pTab, 0);
  1946         -  }
  1947   1955     if( pParse->nested ){
  1948   1956       pik_flags = 0;
  1949   1957     }else{
  1950   1958       pik_flags = OPFLAG_NCHANGE;
  1951   1959       pik_flags |= (update_flags?update_flags:OPFLAG_LASTROWID);
  1952   1960     }
  1953   1961     if( appendBias ){
  1954   1962       pik_flags |= OPFLAG_APPEND;
  1955   1963     }
  1956   1964     if( useSeekResult ){
  1957   1965       pik_flags |= OPFLAG_USESEEKRESULT;
  1958   1966     }
  1959         -  sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
         1967  +  sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, aRegIdx[i], regNewData);
  1960   1968     if( !pParse->nested ){
  1961   1969       sqlite3VdbeAppendP4(v, pTab, P4_TABLE);
  1962   1970     }
  1963   1971     sqlite3VdbeChangeP5(v, pik_flags);
  1964   1972   }
  1965   1973   
  1966   1974   /*

Changes to src/main.c.

  4100   4100       */
  4101   4101       case SQLITE_TESTCTRL_PARSER_COVERAGE: {
  4102   4102         FILE *out = va_arg(ap, FILE*);
  4103   4103         if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
  4104   4104         break;
  4105   4105       }
  4106   4106   #endif /* defined(YYCOVERAGE) */
         4107  +
         4108  +    /*  sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
         4109  +    **
         4110  +    ** This test-control causes the most recent sqlite3_result_int64() value
         4111  +    ** to be interpreted as a MEM_IntReal instead of as an MEM_Int.  Normally,
         4112  +    ** MEM_IntReal values only arise during an INSERT operation of integer
         4113  +    ** values into a REAL column, so they can be challenging to test.  This
         4114  +    ** test-control enables us to write an intreal() SQL function that can
         4115  +    ** inject an intreal() value at arbitrary places in an SQL statement,
         4116  +    ** for testing purposes.
         4117  +    */
         4118  +    case SQLITE_TESTCTRL_RESULT_INTREAL: {
         4119  +      sqlite3_context *pCtx = va_arg(ap, sqlite3_context*);
         4120  +      sqlite3ResultIntReal(pCtx);
         4121  +      break;
         4122  +    }
  4107   4123     }
  4108   4124     va_end(ap);
  4109   4125   #endif /* SQLITE_UNTESTABLE */
  4110   4126     return rc;
  4111   4127   }
  4112   4128   
  4113   4129   /*

Changes to src/msvc.h.

    29     29   #pragma warning(disable : 4244)
    30     30   #pragma warning(disable : 4305)
    31     31   #pragma warning(disable : 4306)
    32     32   #pragma warning(disable : 4702)
    33     33   #pragma warning(disable : 4706)
    34     34   #endif /* defined(_MSC_VER) */
    35     35   
           36  +#if defined(_MSC_VER) && !defined(_WIN64)
           37  +#undef SQLITE_4_BYTE_ALIGNED_MALLOC
           38  +#define SQLITE_4_BYTE_ALIGNED_MALLOC
           39  +#endif /* defined(_MSC_VER) && !defined(_WIN64) */
           40  +
    36     41   #endif /* SQLITE_MSVC_H */

Changes to src/parse.y.

  1075   1075     if( A ){
  1076   1076       A->x.pList = pList;
  1077   1077     }else{
  1078   1078       sqlite3ExprListDelete(pParse->db, pList);
  1079   1079     }
  1080   1080   }
  1081   1081   
  1082         -expr(A) ::= expr(A) AND(OP) expr(Y).    {A=sqlite3PExpr(pParse,@OP,A,Y);}
         1082  +expr(A) ::= expr(A) AND expr(Y).        {A=sqlite3ExprAnd(pParse,A,Y);}
  1083   1083   expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1084   1084   expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
  1085   1085                                           {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1086   1086   expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1087   1087   expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
  1088   1088                                           {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1089   1089   expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).

Changes to src/pragma.c.

  1418   1418         sqlite3VdbeJumpHere(v, addrTop);
  1419   1419       }
  1420   1420     }
  1421   1421     break;
  1422   1422   #endif /* !defined(SQLITE_OMIT_TRIGGER) */
  1423   1423   #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
  1424   1424   
         1425  +#ifndef SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA
  1425   1426     /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
  1426   1427     ** used will be case sensitive or not depending on the RHS.
  1427   1428     */
  1428   1429     case PragTyp_CASE_SENSITIVE_LIKE: {
  1429   1430       if( zRight ){
  1430   1431         sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
  1431   1432       }
  1432   1433     }
  1433   1434     break;
         1435  +#endif /* SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA */
  1434   1436   
  1435   1437   #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
  1436   1438   # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
  1437   1439   #endif
  1438   1440   
  1439   1441   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  1440   1442     /*    PRAGMA integrity_check

Changes to src/pragma.h.

   174    174   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
   175    175    {/* zName:     */ "cache_spill",
   176    176     /* ePragTyp:  */ PragTyp_CACHE_SPILL,
   177    177     /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
   178    178     /* ColNames:  */ 0, 0,
   179    179     /* iArg:      */ 0 },
   180    180   #endif
          181  +#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
   181    182    {/* zName:     */ "case_sensitive_like",
   182    183     /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
   183    184     /* ePragFlg:  */ PragFlg_NoColumns,
   184    185     /* ColNames:  */ 0, 0,
   185    186     /* iArg:      */ 0 },
          187  +#endif
   186    188    {/* zName:     */ "cell_size_check",
   187    189     /* ePragTyp:  */ PragTyp_FLAG,
   188    190     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
   189    191     /* ColNames:  */ 0, 0,
   190    192     /* iArg:      */ SQLITE_CellSizeCk },
   191    193   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
   192    194    {/* zName:     */ "checkpoint_fullfsync",

Changes to src/resolve.c.

   862    862           }
   863    863         }
   864    864         sqlite3WalkExprList(pWalker, pList);
   865    865         if( is_agg ){
   866    866   #ifndef SQLITE_OMIT_WINDOWFUNC
   867    867           if( pExpr->y.pWin ){
   868    868             Select *pSel = pNC->pWinSelect;
   869         -          sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
          869  +          if( IN_RENAME_OBJECT==0 ){
          870  +            sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
          871  +          }
   870    872             sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
   871    873             sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
   872    874             sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
   873    875             if( 0==pSel->pWin 
   874    876              || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
   875    877             ){
   876    878               pExpr->y.pWin->pNextWin = pSel->pWin;

Changes to src/select.c.

   351    351     pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2);
   352    352     if( pEq && isOuterJoin ){
   353    353       ExprSetProperty(pEq, EP_FromJoin);
   354    354       assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
   355    355       ExprSetVVAProperty(pEq, EP_NoReduce);
   356    356       pEq->iRightJoinTable = (i16)pE2->iTable;
   357    357     }
   358         -  *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
          358  +  *ppWhere = sqlite3ExprAnd(pParse, *ppWhere, pEq);
   359    359   }
   360    360   
   361    361   /*
   362    362   ** Set the EP_FromJoin property on all terms of the given expression.
   363    363   ** And set the Expr.iRightJoinTable to iTable for every term in the
   364    364   ** expression.
   365    365   **
................................................................................
   485    485       }
   486    486   
   487    487       /* Add the ON clause to the end of the WHERE clause, connected by
   488    488       ** an AND operator.
   489    489       */
   490    490       if( pRight->pOn ){
   491    491         if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
   492         -      p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
          492  +      p->pWhere = sqlite3ExprAnd(pParse, p->pWhere, pRight->pOn);
   493    493         pRight->pOn = 0;
   494    494       }
   495    495   
   496    496       /* Create extra terms on the WHERE clause for each column named
   497    497       ** in the USING clause.  Example: If the two tables to be joined are 
   498    498       ** A and B and the USING clause names X, Y, and Z, then add this
   499    499       ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
................................................................................
  4030   4030         pSub->pOrderBy = 0;
  4031   4031       }
  4032   4032       pWhere = pSub->pWhere;
  4033   4033       pSub->pWhere = 0;
  4034   4034       if( isLeftJoin>0 ){
  4035   4035         setJoinExpr(pWhere, iNewParent);
  4036   4036       }
  4037         -    pParent->pWhere = sqlite3ExprAnd(db, pWhere, pParent->pWhere);
         4037  +    pParent->pWhere = sqlite3ExprAnd(pParse, pWhere, pParent->pWhere);
  4038   4038       if( db->mallocFailed==0 ){
  4039   4039         SubstContext x;
  4040   4040         x.pParse = pParse;
  4041   4041         x.iTable = iParent;
  4042   4042         x.iNewTable = iNewParent;
  4043   4043         x.isLeftJoin = isLeftJoin;
  4044   4044         x.pEList = pSub->pEList;
................................................................................
  4365   4365         x.pParse = pParse;
  4366   4366         x.iTable = iCursor;
  4367   4367         x.iNewTable = iCursor;
  4368   4368         x.isLeftJoin = 0;
  4369   4369         x.pEList = pSubq->pEList;
  4370   4370         pNew = substExpr(&x, pNew);
  4371   4371         if( pSubq->selFlags & SF_Aggregate ){
  4372         -        pSubq->pHaving = sqlite3ExprAnd(pParse->db, pSubq->pHaving, pNew);
         4372  +        pSubq->pHaving = sqlite3ExprAnd(pParse, pSubq->pHaving, pNew);
  4373   4373         }else{
  4374         -        pSubq->pWhere = sqlite3ExprAnd(pParse->db, pSubq->pWhere, pNew);
         4374  +        pSubq->pWhere = sqlite3ExprAnd(pParse, pSubq->pWhere, pNew);
  4375   4375         }
  4376   4376         pSubq = pSubq->pPrior;
  4377   4377       }
  4378   4378     }
  4379   4379     return nChng;
  4380   4380   }
  4381   4381   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
................................................................................
  4793   4793     }
  4794   4794     while( pSel->pPrior ){ pSel = pSel->pPrior; }
  4795   4795     sqlite3ColumnsFromExprList(pParse, pSel->pEList,&pTab->nCol,&pTab->aCol);
  4796   4796     pTab->iPKey = -1;
  4797   4797     pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
  4798   4798     pTab->tabFlags |= TF_Ephemeral;
  4799   4799   
  4800         -  return SQLITE_OK;
         4800  +  return pParse->nErr ? SQLITE_ERROR : SQLITE_OK;
  4801   4801   }
  4802   4802   
  4803   4803   /*
  4804   4804   ** This routine is a Walker callback for "expanding" a SELECT statement.
  4805   4805   ** "Expanding" means to do the following:
  4806   4806   **
  4807   4807   **    (1)  Make sure VDBE cursor numbers have been assigned to every
................................................................................
  5414   5414       Select *pS = pWalker->u.pSelect;
  5415   5415       if( sqlite3ExprIsConstantOrGroupBy(pWalker->pParse, pExpr, pS->pGroupBy) ){
  5416   5416         sqlite3 *db = pWalker->pParse->db;
  5417   5417         Expr *pNew = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[1], 0);
  5418   5418         if( pNew ){
  5419   5419           Expr *pWhere = pS->pWhere;
  5420   5420           SWAP(Expr, *pNew, *pExpr);
  5421         -        pNew = sqlite3ExprAnd(db, pWhere, pNew);
         5421  +        pNew = sqlite3ExprAnd(pWalker->pParse, pWhere, pNew);
  5422   5422           pS->pWhere = pNew;
  5423   5423           pWalker->eCode = 1;
  5424   5424         }
  5425   5425       }
  5426   5426       return WRC_Prune;
  5427   5427     }
  5428   5428     return WRC_Continue;
................................................................................
  5477   5477       if( sqlite3_stricmp(pItem->zName, pThis->zName)!=0 ) continue;
  5478   5478       pS1 = pItem->pSelect;
  5479   5479       if( pThis->pSelect->selId!=pS1->selId ){
  5480   5480         /* The query flattener left two different CTE tables with identical
  5481   5481         ** names in the same FROM clause. */
  5482   5482         continue;
  5483   5483       }
  5484         -    if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1) ){
         5484  +    if( sqlite3ExprCompare(0, pThis->pSelect->pWhere, pS1->pWhere, -1)
         5485  +     || sqlite3ExprCompare(0, pThis->pSelect->pHaving, pS1->pHaving, -1) 
         5486  +    ){
  5485   5487         /* The view was modified by some other optimization such as
  5486   5488         ** pushDownWhereTerms() */
  5487   5489         continue;
  5488   5490       }
  5489   5491       return pItem;
  5490   5492     }
  5491   5493     return 0;

Changes to src/shell.c.in.

   943    943   INCLUDE ../ext/misc/memtrace.c
   944    944   #ifdef SQLITE_HAVE_ZLIB
   945    945   INCLUDE ../ext/misc/zipfile.c
   946    946   INCLUDE ../ext/misc/sqlar.c
   947    947   #endif
   948    948   INCLUDE ../ext/expert/sqlite3expert.h
   949    949   INCLUDE ../ext/expert/sqlite3expert.c
          950  +
          951  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
          952  +INCLUDE ../ext/misc/dbdata.c
          953  +#endif
   950    954   
   951    955   #if defined(SQLITE_ENABLE_SESSION)
   952    956   /*
   953    957   ** State information for a single open session
   954    958   */
   955    959   typedef struct OpenSession OpenSession;
   956    960   struct OpenSession {
................................................................................
  1673   1677   /*
  1674   1678   ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
  1675   1679   **
  1676   1680   ** This routine converts some CREATE TABLE statements for shadow tables
  1677   1681   ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
  1678   1682   */
  1679   1683   static void printSchemaLine(FILE *out, const char *z, const char *zTail){
         1684  +  if( z==0 ) return;
         1685  +  if( zTail==0 ) return;
  1680   1686     if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
  1681   1687       utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
  1682   1688     }else{
  1683   1689       utf8_printf(out, "%s%s", z, zTail);
  1684   1690     }
  1685   1691   }
  1686   1692   static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
................................................................................
  3485   3491     ".databases               List names and files of attached databases",
  3486   3492     ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
  3487   3493     ".dbinfo ?DB?             Show status information about the database",
  3488   3494     ".dump ?TABLE? ...        Render all database content as SQL",
  3489   3495     "   Options:",
  3490   3496     "     --preserve-rowids      Include ROWID values in the output",
  3491   3497     "     --newlines             Allow unescaped newline characters in output",
  3492         -  "   TABLE is LIKE pattern for the tables to dump",
         3498  +  "   TABLE is a LIKE pattern for the tables to dump",
  3493   3499     ".echo on|off             Turn command echo on or off",
  3494   3500     ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
  3495   3501     "   Other Modes:",
  3496   3502   #ifdef SQLITE_DEBUG
  3497   3503     "      test                  Show raw EXPLAIN QUERY PLAN output",
  3498   3504     "      trace                 Like \"full\" but also enable \"PRAGMA vdbe_trace\"",
  3499   3505   #endif
................................................................................
  3570   3576     "   --once                    Do no more than one progress interrupt",
  3571   3577     "   --quiet|-q                No output except at interrupts",
  3572   3578     "   --reset                   Reset the count for each input and interrupt",
  3573   3579   #endif
  3574   3580     ".prompt MAIN CONTINUE    Replace the standard prompts",
  3575   3581     ".quit                    Exit this program",
  3576   3582     ".read FILE               Read input from FILE",
         3583  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         3584  +  ".recover                 Recover as much data as possible from corrupt db.",
         3585  +#endif
  3577   3586     ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
  3578   3587     ".save FILE               Write in-memory database into FILE",
  3579   3588     ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
  3580   3589     ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
  3581   3590     "     Options:",
  3582   3591     "         --indent            Try to pretty-print the schema",
  3583   3592     ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
................................................................................
  3854   3863     int nLine;
  3855   3864     int n = 0;
  3856   3865     int pgsz = 0;
  3857   3866     int iOffset = 0;
  3858   3867     int j, k;
  3859   3868     int rc;
  3860   3869     FILE *in;
  3861         -  unsigned char x[16];
         3870  +  unsigned int x[16];
  3862   3871     char zLine[1000];
  3863   3872     if( p->zDbFilename ){
  3864   3873       in = fopen(p->zDbFilename, "r");
  3865   3874       if( in==0 ){
  3866   3875         utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
  3867   3876         return 0;
  3868   3877       }
  3869   3878       nLine = 0;
  3870   3879     }else{
  3871   3880       in = p->in;
  3872   3881       nLine = p->lineno;
         3882  +    if( in==0 ) in = stdin;
  3873   3883     }
  3874   3884     *pnData = 0;
  3875   3885     nLine++;
  3876   3886     if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
  3877   3887     rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
  3878   3888     if( rc!=2 ) goto readHexDb_error;
  3879         -  if( n<=0 ) goto readHexDb_error;
  3880         -  a = sqlite3_malloc( n );
         3889  +  if( n<0 ) goto readHexDb_error;
         3890  +  a = sqlite3_malloc( n ? n : 1 );
  3881   3891     if( a==0 ){
  3882   3892       utf8_printf(stderr, "Out of memory!\n");
  3883   3893       goto readHexDb_error;
  3884   3894     }
  3885   3895     memset(a, 0, n);
  3886   3896     if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
  3887   3897       utf8_printf(stderr, "invalid pagesize\n");
................................................................................
  3892   3902       if( rc==2 ){
  3893   3903         iOffset = k;
  3894   3904         continue;
  3895   3905       }
  3896   3906       if( strncmp(zLine, "| end ", 6)==0 ){
  3897   3907         break;
  3898   3908       }
  3899         -    rc = sscanf(zLine,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
  3900         -                      "  %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
         3909  +    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
  3901   3910                   &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
  3902   3911                   &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
  3903   3912       if( rc==17 ){
  3904   3913         k = iOffset+j;
  3905   3914         if( k+16<=n ){
  3906         -        memcpy(a+k, x, 16);
         3915  +        int ii;
         3916  +        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
  3907   3917         }
  3908   3918       }
  3909   3919     }
  3910   3920     *pnData = n;
  3911   3921     if( in!=p->in ){
  3912   3922       fclose(in);
  3913   3923     }else{
  3914   3924       p->lineno = nLine;
  3915   3925     }
  3916   3926     return a;
  3917   3927   
  3918   3928   readHexDb_error:
  3919         -  if( in!=stdin ){
         3929  +  if( in!=p->in ){
  3920   3930       fclose(in);
  3921   3931     }else{
  3922   3932       while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
  3923   3933         nLine++;
  3924   3934         if(strncmp(zLine, "| end ", 6)==0 ) break;
  3925   3935       }
  3926   3936       p->lineno = nLine;
  3927   3937     }
  3928   3938     sqlite3_free(a);
  3929   3939     utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
  3930   3940     return 0;
  3931   3941   }
  3932   3942   #endif /* SQLITE_ENABLE_DESERIALIZE */
         3943  +
         3944  +/*
         3945  +** Scalar function "shell_int32". The first argument to this function
         3946  +** must be a blob. The second a non-negative integer. This function
         3947  +** reads and returns a 32-bit big-endian integer from byte
         3948  +** offset (4*<arg2>) of the blob.
         3949  +*/
         3950  +static void shellInt32(
         3951  +  sqlite3_context *context, 
         3952  +  int argc, 
         3953  +  sqlite3_value **argv
         3954  +){
         3955  +  const unsigned char *pBlob;
         3956  +  int nBlob;
         3957  +  int iInt;
         3958  +
         3959  +  UNUSED_PARAMETER(argc);
         3960  +  nBlob = sqlite3_value_bytes(argv[0]);
         3961  +  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
         3962  +  iInt = sqlite3_value_int(argv[1]);
         3963  +
         3964  +  if( iInt>=0 && (iInt+1)*4<=nBlob ){
         3965  +    const unsigned char *a = &pBlob[iInt*4];
         3966  +    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
         3967  +                       + ((sqlite3_int64)a[1]<<16)
         3968  +                       + ((sqlite3_int64)a[2]<< 8)
         3969  +                       + ((sqlite3_int64)a[3]<< 0);
         3970  +    sqlite3_result_int64(context, iVal);
         3971  +  }
         3972  +}
         3973  +
         3974  +/*
         3975  +** Scalar function "shell_escape_crnl" used by the .recover command.
         3976  +** The argument passed to this function is the output of built-in
         3977  +** function quote(). If the first character of the input is "'", 
         3978  +** indicating that the value passed to quote() was a text value,
         3979  +** then this function searches the input for "\n" and "\r" characters
         3980  +** and adds a wrapper similar to the following:
         3981  +**
         3982  +**   replace(replace(<input>, '\n', char(10), '\r', char(13));
         3983  +**
         3984  +** Or, if the first character of the input is not "'", then a copy
         3985  +** of the input is returned.
         3986  +*/
         3987  +static void shellEscapeCrnl(
         3988  +  sqlite3_context *context, 
         3989  +  int argc, 
         3990  +  sqlite3_value **argv
         3991  +){
         3992  +  const char *zText = (const char*)sqlite3_value_text(argv[0]);
         3993  +  UNUSED_PARAMETER(argc);
         3994  +  if( zText[0]=='\'' ){
         3995  +    int nText = sqlite3_value_bytes(argv[0]);
         3996  +    int i;
         3997  +    char zBuf1[20];
         3998  +    char zBuf2[20];
         3999  +    const char *zNL = 0;
         4000  +    const char *zCR = 0;
         4001  +    int nCR = 0;
         4002  +    int nNL = 0;
         4003  +
         4004  +    for(i=0; zText[i]; i++){
         4005  +      if( zNL==0 && zText[i]=='\n' ){
         4006  +        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
         4007  +        nNL = (int)strlen(zNL);
         4008  +      }
         4009  +      if( zCR==0 && zText[i]=='\r' ){
         4010  +        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
         4011  +        nCR = (int)strlen(zCR);
         4012  +      }
         4013  +    }
         4014  +
         4015  +    if( zNL || zCR ){
         4016  +      int iOut = 0;
         4017  +      i64 nMax = (nNL > nCR) ? nNL : nCR;
         4018  +      i64 nAlloc = nMax * nText + (nMax+64)*2;
         4019  +      char *zOut = (char*)sqlite3_malloc64(nAlloc);
         4020  +      if( zOut==0 ){
         4021  +        sqlite3_result_error_nomem(context);
         4022  +        return;
         4023  +      }
         4024  +
         4025  +      if( zNL && zCR ){
         4026  +        memcpy(&zOut[iOut], "replace(replace(", 16);
         4027  +        iOut += 16;
         4028  +      }else{
         4029  +        memcpy(&zOut[iOut], "replace(", 8);
         4030  +        iOut += 8;
         4031  +      }
         4032  +      for(i=0; zText[i]; i++){
         4033  +        if( zText[i]=='\n' ){
         4034  +          memcpy(&zOut[iOut], zNL, nNL);
         4035  +          iOut += nNL;
         4036  +        }else if( zText[i]=='\r' ){
         4037  +          memcpy(&zOut[iOut], zCR, nCR);
         4038  +          iOut += nCR;
         4039  +        }else{
         4040  +          zOut[iOut] = zText[i];
         4041  +          iOut++;
         4042  +        }
         4043  +      }
         4044  +
         4045  +      if( zNL ){
         4046  +        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
         4047  +        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
         4048  +        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
         4049  +      }
         4050  +      if( zCR ){
         4051  +        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
         4052  +        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
         4053  +        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
         4054  +      }
         4055  +
         4056  +      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
         4057  +      sqlite3_free(zOut);
         4058  +      return;
         4059  +    }
         4060  +  }
         4061  +
         4062  +  sqlite3_result_value(context, argv[0]);
         4063  +}
  3933   4064   
  3934   4065   /* Flags for open_db().
  3935   4066   **
  3936   4067   ** The default behavior of open_db() is to exit(1) if the database fails to
  3937   4068   ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
  3938   4069   ** but still returns without calling exit.
  3939   4070   **
................................................................................
  3995   4126       }
  3996   4127   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  3997   4128       sqlite3_enable_load_extension(p->db, 1);
  3998   4129   #endif
  3999   4130       sqlite3_fileio_init(p->db, 0, 0);
  4000   4131       sqlite3_shathree_init(p->db, 0, 0);
  4001   4132       sqlite3_completion_init(p->db, 0, 0);
         4133  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         4134  +    sqlite3_dbdata_init(p->db, 0, 0);
         4135  +#endif
  4002   4136   #ifdef SQLITE_HAVE_ZLIB
  4003   4137       sqlite3_zipfile_init(p->db, 0, 0);
  4004   4138       sqlite3_sqlar_init(p->db, 0, 0);
  4005   4139   #endif
  4006   4140       sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
  4007   4141                               shellAddSchemaName, 0, 0);
  4008   4142       sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
  4009   4143                               shellModuleSchema, 0, 0);
  4010   4144       sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
  4011   4145                               shellPutsFunc, 0, 0);
         4146  +    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
         4147  +                            shellEscapeCrnl, 0, 0);
         4148  +    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
         4149  +                            shellInt32, 0, 0);
  4012   4150   #ifndef SQLITE_NOHAVE_SYSTEM
  4013   4151       sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
  4014   4152                               editFunc, 0, 0);
  4015   4153       sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
  4016   4154                               editFunc, 0, 0);
  4017   4155   #endif
  4018   4156       if( p->openMode==SHELL_OPEN_ZIPFILE ){
................................................................................
  4028   4166         int nData = 0;
  4029   4167         unsigned char *aData;
  4030   4168         if( p->openMode==SHELL_OPEN_DESERIALIZE ){
  4031   4169           aData = (unsigned char*)readFile(p->zDbFilename, &nData);
  4032   4170         }else{
  4033   4171           aData = readHexDb(p, &nData);
  4034   4172           if( aData==0 ){
  4035         -          utf8_printf(stderr, "Error in hexdb input\n");
  4036   4173             return;
  4037   4174           }
  4038   4175         }
  4039   4176         rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
  4040   4177                      SQLITE_DESERIALIZE_RESIZEABLE |
  4041   4178                      SQLITE_DESERIALIZE_FREEONCLOSE);
  4042   4179         if( rc ){
................................................................................
  5259   5396    usage:
  5260   5397     raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
  5261   5398     raw_printf(stderr, "Where sub-commands are:\n");
  5262   5399     raw_printf(stderr, "    fkey-indexes\n");
  5263   5400     return SQLITE_ERROR;
  5264   5401   }
  5265   5402   
  5266         -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
  5267         -/*********************************************************************************
  5268         -** The ".archive" or ".ar" command.
  5269         -*/
         5403  +#if !defined SQLITE_OMIT_VIRTUALTABLE
  5270   5404   static void shellPrepare(
  5271   5405     sqlite3 *db, 
  5272   5406     int *pRc, 
  5273   5407     const char *zSql, 
  5274   5408     sqlite3_stmt **ppStmt
  5275   5409   ){
  5276   5410     *ppStmt = 0;
................................................................................
  5281   5415             sqlite3_errmsg(db), sqlite3_errcode(db)
  5282   5416         );
  5283   5417         *pRc = rc;
  5284   5418       }
  5285   5419     }
  5286   5420   }
  5287   5421   
  5288         -static void shellPreparePrintf(
         5422  +/*
         5423  +** Create a prepared statement using printf-style arguments for the SQL.
         5424  +**
         5425  +** This routine is could be marked "static".  But it is not always used,
         5426  +** depending on compile-time options.  By omitting the "static", we avoid
         5427  +** nuisance compiler warnings about "defined but not used".
         5428  +*/
         5429  +void shellPreparePrintf(
  5289   5430     sqlite3 *db, 
  5290   5431     int *pRc, 
  5291   5432     sqlite3_stmt **ppStmt,
  5292   5433     const char *zFmt, 
  5293   5434     ...
  5294   5435   ){
  5295   5436     *ppStmt = 0;
................................................................................
  5304   5445       }else{
  5305   5446         shellPrepare(db, pRc, z, ppStmt);
  5306   5447         sqlite3_free(z);
  5307   5448       }
  5308   5449     }
  5309   5450   }
  5310   5451   
  5311         -static void shellFinalize(
         5452  +/* Finalize the prepared statement created using shellPreparePrintf().
         5453  +**
         5454  +** This routine is could be marked "static".  But it is not always used,
         5455  +** depending on compile-time options.  By omitting the "static", we avoid
         5456  +** nuisance compiler warnings about "defined but not used".
         5457  +*/
         5458  +void shellFinalize(
  5312   5459     int *pRc, 
  5313   5460     sqlite3_stmt *pStmt
  5314   5461   ){
  5315   5462     if( pStmt ){
  5316   5463       sqlite3 *db = sqlite3_db_handle(pStmt);
  5317   5464       int rc = sqlite3_finalize(pStmt);
  5318   5465       if( *pRc==SQLITE_OK ){
................................................................................
  5320   5467           raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
  5321   5468         }
  5322   5469         *pRc = rc;
  5323   5470       }
  5324   5471     }
  5325   5472   }
  5326   5473   
  5327         -static void shellReset(
         5474  +/* Reset the prepared statement created using shellPreparePrintf().
         5475  +**
         5476  +** This routine is could be marked "static".  But it is not always used,
         5477  +** depending on compile-time options.  By omitting the "static", we avoid
         5478  +** nuisance compiler warnings about "defined but not used".
         5479  +*/
         5480  +void shellReset(
  5328   5481     int *pRc, 
  5329   5482     sqlite3_stmt *pStmt
  5330   5483   ){
  5331   5484     int rc = sqlite3_reset(pStmt);
  5332   5485     if( *pRc==SQLITE_OK ){
  5333   5486       if( rc!=SQLITE_OK ){
  5334   5487         sqlite3 *db = sqlite3_db_handle(pStmt);
  5335   5488         raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
  5336   5489       }
  5337   5490       *pRc = rc;
  5338   5491     }
  5339   5492   }
         5493  +#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
         5494  +
         5495  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
         5496  +/*********************************************************************************
         5497  +** The ".archive" or ".ar" command.
         5498  +*/
  5340   5499   /*
  5341   5500   ** Structure representing a single ".ar" command.
  5342   5501   */
  5343   5502   typedef struct ArCommand ArCommand;
  5344   5503   struct ArCommand {
  5345   5504     u8 eCmd;                        /* An AR_CMD_* value */
  5346   5505     u8 bVerbose;                    /* True if --verbose */
................................................................................
  6022   6181   
  6023   6182     return rc;
  6024   6183   }
  6025   6184   /* End of the ".archive" or ".ar" command logic
  6026   6185   **********************************************************************************/
  6027   6186   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
  6028   6187   
         6188  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         6189  +/*
         6190  +** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
         6191  +** Otherwise, the SQL statement or statements in zSql are executed using
         6192  +** database connection db and the error code written to *pRc before
         6193  +** this function returns.
         6194  +*/
         6195  +static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
         6196  +  int rc = *pRc;
         6197  +  if( rc==SQLITE_OK ){
         6198  +    char *zErr = 0;
         6199  +    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
         6200  +    if( rc!=SQLITE_OK ){
         6201  +      raw_printf(stderr, "SQL error: %s\n", zErr);
         6202  +    }
         6203  +    *pRc = rc;
         6204  +  }
         6205  +}
         6206  +
         6207  +/*
         6208  +** Like shellExec(), except that zFmt is a printf() style format string.
         6209  +*/
         6210  +static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
         6211  +  char *z = 0;
         6212  +  if( *pRc==SQLITE_OK ){
         6213  +    va_list ap;
         6214  +    va_start(ap, zFmt);
         6215  +    z = sqlite3_vmprintf(zFmt, ap);
         6216  +    va_end(ap);
         6217  +    if( z==0 ){
         6218  +      *pRc = SQLITE_NOMEM;
         6219  +    }else{
         6220  +      shellExec(db, pRc, z);
         6221  +    }
         6222  +    sqlite3_free(z);
         6223  +  }
         6224  +}
         6225  +
         6226  +/*
         6227  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
         6228  +** Otherwise, an attempt is made to allocate, zero and return a pointer
         6229  +** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
         6230  +** to SQLITE_NOMEM and NULL returned.
         6231  +*/
         6232  +static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
         6233  +  void *pRet = 0;
         6234  +  if( *pRc==SQLITE_OK ){
         6235  +    pRet = sqlite3_malloc64(nByte);
         6236  +    if( pRet==0 ){
         6237  +      *pRc = SQLITE_NOMEM;
         6238  +    }else{
         6239  +      memset(pRet, 0, nByte);
         6240  +    }
         6241  +  }
         6242  +  return pRet;
         6243  +}
         6244  +
         6245  +/*
         6246  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
         6247  +** Otherwise, zFmt is treated as a printf() style string. The result of
         6248  +** formatting it along with any trailing arguments is written into a 
         6249  +** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
         6250  +** It is the responsibility of the caller to eventually free this buffer
         6251  +** using a call to sqlite3_free().
         6252  +** 
         6253  +** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL 
         6254  +** pointer returned.
         6255  +*/
         6256  +static char *shellMPrintf(int *pRc, const char *zFmt, ...){
         6257  +  char *z = 0;
         6258  +  if( *pRc==SQLITE_OK ){
         6259  +    va_list ap;
         6260  +    va_start(ap, zFmt);
         6261  +    z = sqlite3_vmprintf(zFmt, ap);
         6262  +    va_end(ap);
         6263  +    if( z==0 ){
         6264  +      *pRc = SQLITE_NOMEM;
         6265  +    }
         6266  +  }
         6267  +  return z;
         6268  +}
         6269  +
         6270  +/*
         6271  +** When running the ".recover" command, each output table, and the special
         6272  +** orphaned row table if it is required, is represented by an instance
         6273  +** of the following struct.
         6274  +*/
         6275  +typedef struct RecoverTable RecoverTable;
         6276  +struct RecoverTable {
         6277  +  char *zQuoted;                  /* Quoted version of table name */
         6278  +  int nCol;                       /* Number of columns in table */
         6279  +  char **azlCol;                  /* Array of column lists */
         6280  +  int iPk;                        /* Index of IPK column */
         6281  +};
         6282  +
         6283  +/*
         6284  +** Free a RecoverTable object allocated by recoverFindTable() or
         6285  +** recoverOrphanTable().
         6286  +*/
         6287  +static void recoverFreeTable(RecoverTable *pTab){
         6288  +  if( pTab ){
         6289  +    sqlite3_free(pTab->zQuoted);
         6290  +    if( pTab->azlCol ){
         6291  +      int i;
         6292  +      for(i=0; i<=pTab->nCol; i++){
         6293  +        sqlite3_free(pTab->azlCol[i]);
         6294  +      }
         6295  +      sqlite3_free(pTab->azlCol);
         6296  +    }
         6297  +    sqlite3_free(pTab);
         6298  +  }
         6299  +}
         6300  +
         6301  +/*
         6302  +** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
         6303  +** Otherwise, it allocates and returns a RecoverTable object based on the
         6304  +** final four arguments passed to this function. It is the responsibility
         6305  +** of the caller to eventually free the returned object using
         6306  +** recoverFreeTable().
         6307  +*/
         6308  +static RecoverTable *recoverNewTable(
         6309  +  int *pRc,                       /* IN/OUT: Error code */
         6310  +  const char *zName,              /* Name of table */
         6311  +  const char *zSql,               /* CREATE TABLE statement */
         6312  +  int bIntkey, 
         6313  +  int nCol
         6314  +){
         6315  +  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
         6316  +  int rc = *pRc;
         6317  +  RecoverTable *pTab = 0;
         6318  +
         6319  +  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
         6320  +  if( rc==SQLITE_OK ){
         6321  +    int nSqlCol = 0;
         6322  +    int bSqlIntkey = 0;
         6323  +    sqlite3_stmt *pStmt = 0;
         6324  +    
         6325  +    rc = sqlite3_open("", &dbtmp);
         6326  +    if( rc==SQLITE_OK ){
         6327  +      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
         6328  +    }
         6329  +    if( rc==SQLITE_OK ){
         6330  +      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
         6331  +      if( rc==SQLITE_ERROR ){
         6332  +        rc = SQLITE_OK;
         6333  +        goto finished;
         6334  +      }
         6335  +    }
         6336  +    shellPreparePrintf(dbtmp, &rc, &pStmt, 
         6337  +        "SELECT count(*) FROM pragma_table_info(%Q)", zName
         6338  +    );
         6339  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6340  +      nSqlCol = sqlite3_column_int(pStmt, 0);
         6341  +    }
         6342  +    shellFinalize(&rc, pStmt);
         6343  +
         6344  +    if( rc!=SQLITE_OK || nSqlCol<nCol ){
         6345  +      goto finished;
         6346  +    }
         6347  +
         6348  +    shellPreparePrintf(dbtmp, &rc, &pStmt, 
         6349  +      "SELECT ("
         6350  +      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
         6351  +      ") FROM sqlite_master WHERE name = %Q", zName
         6352  +    );
         6353  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6354  +      bSqlIntkey = sqlite3_column_int(pStmt, 0);
         6355  +    }
         6356  +    shellFinalize(&rc, pStmt);
         6357  +
         6358  +    if( bIntkey==bSqlIntkey ){
         6359  +      int i;
         6360  +      const char *zPk = "_rowid_";
         6361  +      sqlite3_stmt *pPkFinder = 0;
         6362  +
         6363  +      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
         6364  +      ** set zPk to the name of the PK column, and pTab->iPk to the index
         6365  +      ** of the column, where columns are 0-numbered from left to right.
         6366  +      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
         6367  +      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
         6368  +      pTab->iPk = -2;
         6369  +      if( bIntkey ){
         6370  +        shellPreparePrintf(dbtmp, &rc, &pPkFinder, 
         6371  +          "SELECT cid, name FROM pragma_table_info(%Q) "
         6372  +          "  WHERE pk=1 AND type='integer' COLLATE nocase"
         6373  +          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
         6374  +          , zName, zName
         6375  +        );
         6376  +        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
         6377  +          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
         6378  +          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
         6379  +        }
         6380  +      }
         6381  +
         6382  +      pTab->zQuoted = shellMPrintf(&rc, "%Q", zName);
         6383  +      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
         6384  +      pTab->nCol = nSqlCol;
         6385  +
         6386  +      if( bIntkey ){
         6387  +        pTab->azlCol[0] = shellMPrintf(&rc, "%Q", zPk);
         6388  +      }else{
         6389  +        pTab->azlCol[0] = shellMPrintf(&rc, "");
         6390  +      }
         6391  +      i = 1;
         6392  +      shellPreparePrintf(dbtmp, &rc, &pStmt, 
         6393  +          "SELECT %Q || group_concat(name, ', ') "
         6394  +          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
         6395  +          "FROM pragma_table_info(%Q)", 
         6396  +          bIntkey ? ", " : "", pTab->iPk, 
         6397  +          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
         6398  +          zName
         6399  +      );
         6400  +      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6401  +        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
         6402  +        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
         6403  +        i++;
         6404  +      }
         6405  +      shellFinalize(&rc, pStmt);
         6406  +
         6407  +      shellFinalize(&rc, pPkFinder);
         6408  +    }
         6409  +  }
         6410  +
         6411  + finished:
         6412  +  sqlite3_close(dbtmp);
         6413  +  *pRc = rc;
         6414  +  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
         6415  +    recoverFreeTable(pTab);
         6416  +    pTab = 0;
         6417  +  }
         6418  +  return pTab;
         6419  +}
         6420  +
         6421  +/*
         6422  +** This function is called to search the schema recovered from the
         6423  +** sqlite_master table of the (possibly) corrupt database as part
         6424  +** of a ".recover" command. Specifically, for a table with root page
         6425  +** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
         6426  +** table must be a WITHOUT ROWID table, or if non-zero, not one of
         6427  +** those.
         6428  +**
         6429  +** If a table is found, a (RecoverTable*) object is returned. Or, if
         6430  +** no such table is found, but bIntkey is false and iRoot is the 
         6431  +** root page of an index in the recovered schema, then (*pbNoop) is
         6432  +** set to true and NULL returned. Or, if there is no such table or
         6433  +** index, NULL is returned and (*pbNoop) set to 0, indicating that
         6434  +** the caller should write data to the orphans table.
         6435  +*/
         6436  +static RecoverTable *recoverFindTable(
         6437  +  ShellState *pState,             /* Shell state object */
         6438  +  int *pRc,                       /* IN/OUT: Error code */
         6439  +  int iRoot,                      /* Root page of table */
         6440  +  int bIntkey,                    /* True for an intkey table */
         6441  +  int nCol,                       /* Number of columns in table */
         6442  +  int *pbNoop                     /* OUT: True if iRoot is root of index */
         6443  +){
         6444  +  sqlite3_stmt *pStmt = 0;
         6445  +  RecoverTable *pRet = 0;
         6446  +  int bNoop = 0;
         6447  +  const char *zSql = 0;
         6448  +  const char *zName = 0;
         6449  +
         6450  +  /* Search the recovered schema for an object with root page iRoot. */
         6451  +  shellPreparePrintf(pState->db, pRc, &pStmt,
         6452  +      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
         6453  +  );
         6454  +  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6455  +    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
         6456  +    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
         6457  +      bNoop = 1;
         6458  +      break;
         6459  +    }
         6460  +    if( sqlite3_stricmp(zType, "table")==0 ){
         6461  +      zName = (const char*)sqlite3_column_text(pStmt, 1);
         6462  +      zSql = (const char*)sqlite3_column_text(pStmt, 2);
         6463  +      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
         6464  +      break;
         6465  +    }
         6466  +  }
         6467  +
         6468  +  shellFinalize(pRc, pStmt);
         6469  +  *pbNoop = bNoop;
         6470  +  return pRet;
         6471  +}
         6472  +
         6473  +/*
         6474  +** Return a RecoverTable object representing the orphans table.
         6475  +*/
         6476  +static RecoverTable *recoverOrphanTable(
         6477  +  ShellState *pState,             /* Shell state object */
         6478  +  int *pRc,                       /* IN/OUT: Error code */
         6479  +  const char *zLostAndFound,      /* Base name for orphans table */
         6480  +  int nCol                        /* Number of user data columns */
         6481  +){
         6482  +  RecoverTable *pTab = 0;
         6483  +  if( nCol>=0 && *pRc==SQLITE_OK ){
         6484  +    int i;
         6485  +
         6486  +    /* This block determines the name of the orphan table. The prefered
         6487  +    ** name is zLostAndFound. But if that clashes with another name
         6488  +    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
         6489  +    ** and so on until a non-clashing name is found.  */
         6490  +    int iTab = 0;
         6491  +    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
         6492  +    sqlite3_stmt *pTest = 0;
         6493  +    shellPrepare(pState->db, pRc,
         6494  +        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
         6495  +    );
         6496  +    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
         6497  +    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
         6498  +      shellReset(pRc, pTest);
         6499  +      sqlite3_free(zTab);
         6500  +      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
         6501  +      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
         6502  +    }
         6503  +    shellFinalize(pRc, pTest);
         6504  +
         6505  +    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
         6506  +    if( pTab ){
         6507  +      pTab->zQuoted = shellMPrintf(pRc, "%Q", zTab);
         6508  +      pTab->nCol = nCol;
         6509  +      pTab->iPk = -2;
         6510  +      if( nCol>0 ){
         6511  +        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
         6512  +        if( pTab->azlCol ){
         6513  +          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
         6514  +          for(i=nCol-1; i>=0; i--){
         6515  +            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
         6516  +          }
         6517  +        }
         6518  +      }
         6519  +
         6520  +      if( *pRc!=SQLITE_OK ){
         6521  +        recoverFreeTable(pTab);
         6522  +        pTab = 0;
         6523  +      }else{
         6524  +        raw_printf(pState->out, 
         6525  +            "CREATE TABLE %s(rootpgno INTEGER, "
         6526  +            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
         6527  +        );
         6528  +        for(i=0; i<nCol; i++){
         6529  +          raw_printf(pState->out, ", c%d", i);
         6530  +        }
         6531  +        raw_printf(pState->out, ");\n");
         6532  +      }
         6533  +    }
         6534  +    sqlite3_free(zTab);
         6535  +  }
         6536  +  return pTab;
         6537  +}
         6538  +
         6539  +/*
         6540  +** This function is called to recover data from the database. A script
         6541  +** to construct a new database containing all recovered data is output
         6542  +** on stream pState->out.
         6543  +*/
         6544  +static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
         6545  +  int rc = SQLITE_OK;
         6546  +  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
         6547  +  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
         6548  +  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
         6549  +  const char *zRecoveryDb = "";   /* Name of "recovery" database */
         6550  +  const char *zLostAndFound = "lost_and_found";
         6551  +  int i;
         6552  +  int nOrphan = -1;
         6553  +  RecoverTable *pOrphan = 0;
         6554  +
         6555  +  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
         6556  +  for(i=1; i<nArg; i++){
         6557  +    char *z = azArg[i];
         6558  +    int n;
         6559  +    if( z[0]=='-' && z[1]=='-' ) z++;
         6560  +    n = strlen(z);
         6561  +    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
         6562  +      bFreelist = 0;
         6563  +    }else
         6564  +    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
         6565  +      i++;
         6566  +      zRecoveryDb = azArg[i];
         6567  +    }else
         6568  +    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
         6569  +      i++;
         6570  +      zLostAndFound = azArg[i];
         6571  +    }
         6572  +    else{
         6573  +      raw_printf(stderr, "unexpected option: %s\n", azArg[i]); 
         6574  +      raw_printf(stderr, "options are:\n");
         6575  +      raw_printf(stderr, "    --freelist-corrupt\n");
         6576  +      raw_printf(stderr, "    --recovery-db DATABASE\n");
         6577  +      raw_printf(stderr, "    --lost-and-found TABLE-NAME\n");
         6578  +      return 1;
         6579  +    }
         6580  +  }
         6581  +
         6582  +  shellExecPrintf(pState->db, &rc,
         6583  +    /* Attach an in-memory database named 'recovery'. Create an indexed 
         6584  +    ** cache of the sqlite_dbptr virtual table. */
         6585  +    "ATTACH %Q AS recovery;"
         6586  +    "DROP TABLE IF EXISTS recovery.dbptr;"
         6587  +    "DROP TABLE IF EXISTS recovery.freelist;"
         6588  +    "DROP TABLE IF EXISTS recovery.map;"
         6589  +    "DROP TABLE IF EXISTS recovery.schema;"
         6590  +    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
         6591  +  );
         6592  +
         6593  +  if( bFreelist ){
         6594  +    shellExec(pState->db, &rc,
         6595  +      "WITH trunk(pgno) AS ("
         6596  +      "  SELECT shell_int32("
         6597  +      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
         6598  +      "      WHERE x>0"
         6599  +      "    UNION"
         6600  +      "  SELECT shell_int32("
         6601  +      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
         6602  +      "      FROM trunk WHERE x>0"
         6603  +      "),"
         6604  +      "freelist(data, n, freepgno) AS ("
         6605  +      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
         6606  +      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
         6607  +      "    UNION ALL"
         6608  +      "  SELECT data, n-1, shell_int32(data, 2+n) "
         6609  +      "      FROM freelist WHERE n>=0"
         6610  +      ")"
         6611  +      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
         6612  +    );
         6613  +  }
         6614  +
         6615  +  shellExec(pState->db, &rc, 
         6616  +    "CREATE TABLE recovery.dbptr("
         6617  +    "      pgno, child, PRIMARY KEY(child, pgno)"
         6618  +    ") WITHOUT ROWID;"
         6619  +    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
         6620  +    "    SELECT * FROM sqlite_dbptr"
         6621  +    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
         6622  +
         6623  +    /* Delete any pointer to page 1. This ensures that page 1 is considered
         6624  +    ** a root page, regardless of how corrupt the db is. */
         6625  +    "DELETE FROM recovery.dbptr WHERE child = 1;"
         6626  +
         6627  +    /* Delete all pointers to any pages that have more than one pointer
         6628  +    ** to them. Such pages will be treated as root pages when recovering
         6629  +    ** data.  */
         6630  +    "DELETE FROM recovery.dbptr WHERE child IN ("
         6631  +    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
         6632  +    ");"
         6633  +
         6634  +    /* Create the "map" table that will (eventually) contain instructions
         6635  +    ** for dealing with each page in the db that contains one or more 
         6636  +    ** records. */
         6637  +    "CREATE TABLE recovery.map("
         6638  +      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
         6639  +    ");"
         6640  +
         6641  +    /* Populate table [map]. If there are circular loops of pages in the
         6642  +    ** database, the following adds all pages in such a loop to the map
         6643  +    ** as individual root pages. This could be handled better.  */
         6644  +    "WITH pages(i, maxlen) AS ("
         6645  +    "  SELECT page_count, ("
         6646  +    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
         6647  +    "  ) FROM pragma_page_count WHERE page_count>0"
         6648  +    "    UNION ALL"
         6649  +    "  SELECT i-1, ("
         6650  +    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
         6651  +    "  ) FROM pages WHERE i>=2"
         6652  +    ")"
         6653  +    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
         6654  +    "  SELECT i, maxlen, NULL, ("
         6655  +    "    WITH p(orig, pgno, parent) AS ("
         6656  +    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
         6657  +    "        UNION "
         6658  +    "      SELECT i, p.parent, "
         6659  +    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
         6660  +    "    )"
         6661  +    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
         6662  +    ") "
         6663  +    "FROM pages WHERE maxlen > 0 AND i NOT IN freelist;"
         6664  +    "UPDATE recovery.map AS o SET intkey = ("
         6665  +    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
         6666  +    ");"
         6667  +
         6668  +    /* Extract data from page 1 and any linked pages into table
         6669  +    ** recovery.schema. With the same schema as an sqlite_master table.  */
         6670  +    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
         6671  +    "INSERT INTO recovery.schema SELECT "
         6672  +    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
         6673  +    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
         6674  +    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
         6675  +    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
         6676  +    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
         6677  +    "FROM sqlite_dbdata WHERE pgno IN ("
         6678  +    "  SELECT pgno FROM recovery.map WHERE root=1"
         6679  +    ")"
         6680  +    "GROUP BY pgno, cell;"
         6681  +    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
         6682  +  );
         6683  +
         6684  +  /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 
         6685  +  ** CREATE TABLE statements that extracted from the existing schema.  */
         6686  +  if( rc==SQLITE_OK ){
         6687  +    sqlite3_stmt *pStmt = 0;
         6688  +    raw_printf(pState->out, "BEGIN;\n");
         6689  +    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
         6690  +    shellPrepare(pState->db, &rc,
         6691  +        "SELECT sql FROM recovery.schema "
         6692  +        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
         6693  +    );
         6694  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6695  +      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
         6696  +      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", 
         6697  +          &zCreateTable[12]
         6698  +      );
         6699  +    }
         6700  +    shellFinalize(&rc, pStmt);
         6701  +  }
         6702  +
         6703  +  /* Figure out if an orphan table will be required. And if so, how many
         6704  +  ** user columns it should contain */
         6705  +  shellPrepare(pState->db, &rc, 
         6706  +      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
         6707  +      , &pLoop
         6708  +  );
         6709  +  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
         6710  +    nOrphan = sqlite3_column_int(pLoop, 0);
         6711  +  }
         6712  +  shellFinalize(&rc, pLoop);
         6713  +  pLoop = 0;
         6714  +
         6715  +  shellPrepare(pState->db, &rc,
         6716  +      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
         6717  +  );
         6718  +  shellPrepare(pState->db, &rc,
         6719  +      "SELECT max(field), group_concat(shell_escape_crnl(quote(value)), ', ')"
         6720  +      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
         6721  +      "GROUP BY cell", &pCells
         6722  +  );
         6723  +
         6724  +  /* Loop through each root page. */
         6725  +  shellPrepare(pState->db, &rc, 
         6726  +      "SELECT root, intkey, max(maxlen) FROM recovery.map" 
         6727  +      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
         6728  +      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
         6729  +      ")", &pLoop
         6730  +  );
         6731  +  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
         6732  +    int iRoot = sqlite3_column_int(pLoop, 0);
         6733  +    int bIntkey = sqlite3_column_int(pLoop, 1);
         6734  +    int nCol = sqlite3_column_int(pLoop, 2);
         6735  +    int bNoop = 0;
         6736  +    RecoverTable *pTab;
         6737  +
         6738  +    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
         6739  +    if( bNoop || rc ) continue;
         6740  +    if( pTab==0 ){
         6741  +      if( pOrphan==0 ){
         6742  +        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
         6743  +      }
         6744  +      pTab = pOrphan;
         6745  +      if( pTab==0 ) break;
         6746  +    }
         6747  +
         6748  +    if( 0==sqlite3_stricmp(pTab->zQuoted, "'sqlite_sequence'") ){
         6749  +      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
         6750  +    }
         6751  +    sqlite3_bind_int(pPages, 1, iRoot);
         6752  +    sqlite3_bind_int(pCells, 2, pTab->iPk);
         6753  +
         6754  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
         6755  +      int iPgno = sqlite3_column_int(pPages, 0);
         6756  +      sqlite3_bind_int(pCells, 1, iPgno);
         6757  +      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
         6758  +        int nField = sqlite3_column_int(pCells, 0);
         6759  +        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
         6760  +
         6761  +        nField = nField+1;
         6762  +        if( pTab==pOrphan ){
         6763  +          raw_printf(pState->out, 
         6764  +              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
         6765  +              pTab->zQuoted, iRoot, iPgno, nField, 
         6766  +              bIntkey ? "" : "NULL, ", zVal, pTab->azlCol[nField]
         6767  +          );
         6768  +        }else{
         6769  +          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 
         6770  +              pTab->zQuoted, pTab->azlCol[nField], zVal
         6771  +          );
         6772  +        }
         6773  +      }
         6774  +      shellReset(&rc, pCells);
         6775  +    }
         6776  +    shellReset(&rc, pPages);
         6777  +    if( pTab!=pOrphan ) recoverFreeTable(pTab);
         6778  +  }
         6779  +  shellFinalize(&rc, pLoop);
         6780  +  shellFinalize(&rc, pPages);
         6781  +  shellFinalize(&rc, pCells);
         6782  +  recoverFreeTable(pOrphan);
         6783  +
         6784  +  /* The rest of the schema */
         6785  +  if( rc==SQLITE_OK ){
         6786  +    sqlite3_stmt *pStmt = 0;
         6787  +    shellPrepare(pState->db, &rc, 
         6788  +        "SELECT sql, name FROM recovery.schema "
         6789  +        "WHERE sql NOT LIKE 'create table%'", &pStmt
         6790  +    );
         6791  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6792  +      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
         6793  +      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
         6794  +        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
         6795  +        char *zPrint = shellMPrintf(&rc, 
         6796  +          "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
         6797  +          zName, zName, zSql
         6798  +        );
         6799  +        raw_printf(pState->out, "%s;\n", zPrint);
         6800  +        sqlite3_free(zPrint);
         6801  +      }else{
         6802  +        raw_printf(pState->out, "%s;\n", zSql);
         6803  +      }
         6804  +    }
         6805  +    shellFinalize(&rc, pStmt);
         6806  +  }
         6807  +
         6808  +  if( rc==SQLITE_OK ){
         6809  +    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
         6810  +    raw_printf(pState->out, "COMMIT;\n");
         6811  +  }
         6812  +  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
         6813  +  return rc;
         6814  +}
         6815  +#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
         6816  +
  6029   6817   
  6030   6818   /*
  6031   6819   ** If an input line begins with "." then invoke this routine to
  6032   6820   ** process that line.
  6033   6821   **
  6034   6822   ** Return 1 on error, 2 to exit, and 0 otherwise.
  6035   6823   */
................................................................................
  6309   7097       }   
  6310   7098     }else
  6311   7099   
  6312   7100     if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
  6313   7101       rc = shell_dbinfo_command(p, nArg, azArg);
  6314   7102     }else
  6315   7103   
         7104  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         7105  +  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
         7106  +    open_db(p, 0);
         7107  +    rc = recoverDatabaseCmd(p, nArg, azArg);
         7108  +  }else
         7109  +#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
         7110  +
  6316   7111     if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
  6317   7112       const char *zLike = 0;
  6318   7113       int i;
  6319   7114       int savedShowHeader = p->showHeader;
  6320   7115       int savedShellFlags = p->shellFlgs;
  6321   7116       ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
  6322   7117       for(i=1; i<nArg; i++){
................................................................................
  6346   7141                              "?--newlines? ?LIKE-PATTERN?\n");
  6347   7142           rc = 1;
  6348   7143           goto meta_command_exit;
  6349   7144         }else{
  6350   7145           zLike = azArg[i];
  6351   7146         }
  6352   7147       }
         7148  +
  6353   7149       open_db(p, 0);
         7150  +
  6354   7151       /* When playing back a "dump", the content might appear in an order
  6355   7152       ** which causes immediate foreign key constraints to be violated.
  6356   7153       ** So disable foreign-key constraint enforcement to prevent problems. */
  6357   7154       raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
  6358   7155       raw_printf(p->out, "BEGIN TRANSACTION;\n");
  6359   7156       p->writableSchema = 0;
  6360   7157       p->showHeader = 0;
................................................................................
  6394   7191       }
  6395   7192       if( p->writableSchema ){
  6396   7193         raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
  6397   7194         p->writableSchema = 0;
  6398   7195       }
  6399   7196       sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
  6400   7197       sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
  6401         -    raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
         7198  +    raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
  6402   7199       p->showHeader = savedShowHeader;
  6403   7200       p->shellFlgs = savedShellFlags;
  6404   7201     }else
  6405   7202   
  6406   7203     if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
  6407   7204       if( nArg==2 ){
  6408   7205         setOrClearFlag(p, SHFLG_Echo, azArg[1]);

Changes to src/sqlite.h.in.

  7315   7315   #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
  7316   7316   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  7317   7317   #define SQLITE_TESTCTRL_BYTEORDER               22
  7318   7318   #define SQLITE_TESTCTRL_ISINIT                  23
  7319   7319   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  7320   7320   #define SQLITE_TESTCTRL_IMPOSTER                25
  7321   7321   #define SQLITE_TESTCTRL_PARSER_COVERAGE         26
  7322         -#define SQLITE_TESTCTRL_LAST                    26  /* Largest TESTCTRL */
         7322  +#define SQLITE_TESTCTRL_RESULT_INTREAL          27
         7323  +#define SQLITE_TESTCTRL_LAST                    27  /* Largest TESTCTRL */
  7323   7324   
  7324   7325   /*
  7325   7326   ** CAPI3REF: SQL Keyword Checking
  7326   7327   **
  7327   7328   ** These routines provide access to the set of SQL language keywords 
  7328   7329   ** recognized by SQLite.  Applications can uses these routines to determine
  7329   7330   ** whether or not a specific identifier needs to be escaped (for example,

Changes to src/sqliteInt.h.

  2261   2261     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
  2262   2262     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  2263   2263     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
  2264   2264     unsigned isCovering:1;   /* True if this is a covering index */
  2265   2265     unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
  2266   2266     unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
  2267   2267     unsigned bNoQuery:1;     /* Do not use this index to optimize queries */
         2268  +  unsigned bAscKeyBug:1;   /* True if the bba7b69f9849b5bf bug applies */
  2268   2269   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2269   2270     int nSample;             /* Number of elements in aSample[] */
  2270   2271     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
  2271   2272     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
  2272   2273     IndexSample *aSample;    /* Samples of the left-most key */
  2273   2274     tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
  2274   2275     tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
................................................................................
  2538   2539   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
  2539   2540   #define EP_Alias     0x400000 /* Is an alias for a result set column */
  2540   2541   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
  2541   2542   #define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
  2542   2543   #define EP_Subrtn   0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
  2543   2544   #define EP_Quoted   0x4000000 /* TK_ID was originally quoted */
  2544   2545   #define EP_Static   0x8000000 /* Held in memory not obtained from malloc() */
         2546  +#define EP_IsTrue  0x10000000 /* Always has boolean value of TRUE */
         2547  +#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
  2545   2548   
  2546   2549   /*
  2547   2550   ** The EP_Propagate mask is a set of properties that automatically propagate
  2548   2551   ** upwards into parent nodes.
  2549   2552   */
  2550   2553   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
  2551   2554   
................................................................................
  2553   2556   ** These macros can be used to test, set, or clear bits in the
  2554   2557   ** Expr.flags field.
  2555   2558   */
  2556   2559   #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
  2557   2560   #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
  2558   2561   #define ExprSetProperty(E,P)     (E)->flags|=(P)
  2559   2562   #define ExprClearProperty(E,P)   (E)->flags&=~(P)
         2563  +#define ExprAlwaysTrue(E)   (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
         2564  +#define ExprAlwaysFalse(E)  (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
  2560   2565   
  2561   2566   /* The ExprSetVVAProperty() macro is used for Verification, Validation,
  2562   2567   ** and Accreditation only.  It works like ExprSetProperty() during VVA
  2563   2568   ** processes but is a no-op for delivery.
  2564   2569   */
  2565   2570   #ifdef SQLITE_DEBUG
  2566   2571   # define ExprSetVVAProperty(E,P)  (E)->flags|=(P)
................................................................................
  3862   3867   int sqlite3NoTempsInRange(Parse*,int,int);
  3863   3868   #endif
  3864   3869   Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  3865   3870   Expr *sqlite3Expr(sqlite3*,int,const char*);
  3866   3871   void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  3867   3872   Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
  3868   3873   void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
  3869         -Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
         3874  +Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
         3875  +Expr *sqlite3ExprSimplifiedAndOr(Expr*);
  3870   3876   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
  3871   3877   void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
  3872   3878   void sqlite3ExprDelete(sqlite3*, Expr*);
  3873   3879   ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  3874   3880   ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
  3875   3881   void sqlite3ExprListSetSortOrder(ExprList*,int);
  3876   3882   void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
................................................................................
  4277   4283   
  4278   4284   const void *sqlite3ValueText(sqlite3_value*, u8);
  4279   4285   int sqlite3ValueBytes(sqlite3_value*, u8);
  4280   4286   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
  4281   4287                           void(*)(void*));
  4282   4288   void sqlite3ValueSetNull(sqlite3_value*);
  4283   4289   void sqlite3ValueFree(sqlite3_value*);
         4290  +#ifndef SQLITE_UNTESTABLE
         4291  +void sqlite3ResultIntReal(sqlite3_context*);
         4292  +#endif
  4284   4293   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  4285   4294   #ifndef SQLITE_OMIT_UTF16
  4286   4295   char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  4287   4296   #endif
  4288   4297   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  4289   4298   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  4290   4299   #ifndef SQLITE_AMALGAMATION

Changes to src/test1.c.

   993    993     sqlite3_context *context, 
   994    994     int argc,  
   995    995     sqlite3_value **argv
   996    996   ){
   997    997     static int cnt = 0;
   998    998     sqlite3_result_int(context, cnt++);
   999    999   }
         1000  +
         1001  +/*
         1002  +** This SQL function returns the integer value of its argument as a MEM_IntReal
         1003  +** value.
         1004  +*/
         1005  +static void intrealFunction(
         1006  +  sqlite3_context *context, 
         1007  +  int argc,  
         1008  +  sqlite3_value **argv
         1009  +){
         1010  +  sqlite3_int64 v = sqlite3_value_int64(argv[0]);
         1011  +  sqlite3_result_int64(context, v);
         1012  +  sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, context);
         1013  +}
  1000   1014   
  1001   1015   /*
  1002   1016   ** Usage:  sqlite3_create_function DB
  1003   1017   **
  1004   1018   ** Call the sqlite3_create_function API on the given database in order
  1005   1019   ** to create a function named "x_coalesce".  This function does the same thing
  1006   1020   ** as the "coalesce" function.  This function also registers an SQL function
................................................................................
  1057   1071       rc = sqlite3_create_function(db, "counter1", -1, SQLITE_UTF8,
  1058   1072             0, nondeterministicFunction, 0, 0);
  1059   1073     }
  1060   1074     if( rc==SQLITE_OK ){
  1061   1075       rc = sqlite3_create_function(db, "counter2", -1, SQLITE_UTF8|SQLITE_DETERMINISTIC,
  1062   1076             0, nondeterministicFunction, 0, 0);
  1063   1077     }
         1078  +
         1079  +  /* The intreal() function converts its argument to an integer and returns
         1080  +  ** it as a MEM_IntReal.
         1081  +  */
         1082  +  if( rc==SQLITE_OK ){
         1083  +    rc = sqlite3_create_function(db, "intreal", 1, SQLITE_UTF8,
         1084  +          0, intrealFunction, 0, 0);
         1085  +  }
  1064   1086   
  1065   1087   #ifndef SQLITE_OMIT_UTF16
  1066   1088     /* Use the sqlite3_create_function16() API here. Mainly for fun, but also 
  1067   1089     ** because it is not tested anywhere else. */
  1068   1090     if( rc==SQLITE_OK ){
  1069   1091       const void *zUtf16;
  1070   1092       sqlite3_value *pVal;
................................................................................
  4268   4290     }else{
  4269   4291       int n = (int)strlen(zSql) + 1;
  4270   4292       zCopy = malloc(n);
  4271   4293       memcpy(zCopy, zSql, n);
  4272   4294     }
  4273   4295     pzTail = objc>=5 ? &zTail : 0;
  4274   4296     rc = sqlite3_prepare_v2(db, zCopy, bytes, &pStmt, pzTail);
         4297  +  if( objc>=5 ){
         4298  +    zTail = &zSql[(zTail - zCopy)];
         4299  +  }
  4275   4300     free(zCopy);
  4276         -  zTail = &zSql[(zTail - zCopy)];
  4277   4301   
  4278   4302     assert(rc==SQLITE_OK || pStmt==0);
  4279   4303     Tcl_ResetResult(interp);
  4280   4304     if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  4281         -  if( rc==SQLITE_OK && zTail && objc>=5 ){
         4305  +  if( rc==SQLITE_OK && objc>=5 && zTail ){
  4282   4306       if( bytes>=0 ){
  4283   4307         bytes = bytes - (int)(zTail-zSql);
  4284   4308       }
  4285   4309       Tcl_ObjSetVar2(interp, objv[4], 0, Tcl_NewStringObj(zTail, bytes), 0);
  4286   4310     }
  4287   4311     if( rc!=SQLITE_OK ){
  4288   4312       assert( pStmt==0 );
................................................................................
  7728   7752     unsigned char *a = 0;
  7729   7753     int n = 0;
  7730   7754     int lineno = 0;
  7731   7755     int i, iNext;
  7732   7756     int iOffset = 0;
  7733   7757     int j, k;
  7734   7758     int rc;
  7735         -  unsigned char x[16];
         7759  +  unsigned int x[16];
  7736   7760     if( objc!=2 ){
  7737   7761       Tcl_WrongNumArgs(interp, 1, objv, "HEXDB");
  7738   7762       return TCL_ERROR;
  7739   7763     }
  7740   7764     zIn = Tcl_GetString(objv[1]);
  7741   7765     for(i=0; zIn[i]; i=iNext){
  7742   7766       lineno++;
................................................................................
  7760   7784         continue;
  7761   7785       }
  7762   7786       rc = sscanf(zIn+i, "| page %d offset %d", &j, &k);
  7763   7787       if( rc==2 ){
  7764   7788         iOffset = k;
  7765   7789         continue;
  7766   7790       }
  7767         -    rc = sscanf(zIn+i,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
  7768         -                      "  %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
         7791  +    rc = sscanf(zIn+i,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
  7769   7792                   &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
  7770   7793                   &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
  7771   7794       if( rc==17 ){
  7772   7795         k = iOffset+j;
  7773   7796         if( k+16<=n ){
  7774         -        memcpy(a+k, x, 16);
         7797  +        int ii;
         7798  +        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
  7775   7799         }
  7776   7800         continue;
  7777   7801       }
  7778   7802     }
  7779   7803     Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n));
  7780   7804     free(a);
  7781   7805     return TCL_OK;

Changes to src/test_fs.c.

   125    125   };
   126    126   
   127    127   struct FsdirCsr {
   128    128     sqlite3_vtab_cursor base;
   129    129     char *zDir;                     /* Buffer containing directory scanned */
   130    130     DIR *pDir;                      /* Open directory */
   131    131     sqlite3_int64 iRowid;
   132         -  struct DIRENT entry;            /* Current entry */
          132  +  struct DIRENT *pEntry;
   133    133   };
   134    134   
   135    135   /*
   136    136   ** This function is the implementation of both the xConnect and xCreate
   137    137   ** methods of the fsdir virtual table.
   138    138   **
   139    139   ** The argv[] array contains the following:
................................................................................
   232    232   /*
   233    233   ** Skip the cursor to the next entry.
   234    234   */
   235    235   static int fsdirNext(sqlite3_vtab_cursor *cur){
   236    236     FsdirCsr *pCsr = (FsdirCsr*)cur;
   237    237   
   238    238     if( pCsr->pDir ){
   239         -    struct DIRENT *pRes = 0;
   240         -    pRes = readdir(pCsr->pDir);
   241         -    if( pRes!=0 ){
   242         -      memcpy(&pCsr->entry, pRes, sizeof(struct DIRENT));
   243         -    }
   244         -    if( pRes==0 ){
          239  +    pCsr->pEntry = readdir(pCsr->pDir);
          240  +    if( pCsr->pEntry==0 ){
   245    241         closedir(pCsr->pDir);
   246    242         pCsr->pDir = 0;
   247    243       }
   248    244       pCsr->iRowid++;
   249    245     }
   250    246   
   251    247     return SQLITE_OK;
................................................................................
   300    296     FsdirCsr *pCsr = (FsdirCsr*)cur;
   301    297     switch( i ){
   302    298       case 0: /* dir */
   303    299         sqlite3_result_text(ctx, pCsr->zDir, -1, SQLITE_STATIC);
   304    300         break;
   305    301   
   306    302       case 1: /* name */
   307         -      sqlite3_result_text(ctx, pCsr->entry.d_name, -1, SQLITE_TRANSIENT);
          303  +      sqlite3_result_text(ctx, pCsr->pEntry->d_name, -1, SQLITE_TRANSIENT);
   308    304         break;
   309    305   
   310    306       default:
   311    307         assert( 0 );
   312    308     }
   313    309   
   314    310     return SQLITE_OK;

Changes to src/test_vfs.c.

   231    231       { SQLITE_OK,       "SQLITE_OK"     },
   232    232       { SQLITE_ERROR,    "SQLITE_ERROR"  },
   233    233       { SQLITE_IOERR,    "SQLITE_IOERR"  },
   234    234       { SQLITE_LOCKED,   "SQLITE_LOCKED" },
   235    235       { SQLITE_BUSY,     "SQLITE_BUSY"   },
   236    236       { SQLITE_READONLY, "SQLITE_READONLY"   },
   237    237       { SQLITE_READONLY_CANTINIT, "SQLITE_READONLY_CANTINIT"   },
          238  +    { SQLITE_NOTFOUND, "SQLITE_NOTFOUND"   },
   238    239       { -1,              "SQLITE_OMIT"   },
   239    240     };
   240    241   
   241    242     const char *z;
   242    243     int i;
   243    244   
   244    245     z = Tcl_GetStringResult(p->interp);
................................................................................
   548    549     if( p->pScript && (p->mask&TESTVFS_FCNTL_MASK) ){
   549    550       struct Fcntl {
   550    551         int iFnctl;
   551    552         const char *zFnctl;
   552    553       } aF[] = {
   553    554         { SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, "BEGIN_ATOMIC_WRITE" },
   554    555         { SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, "COMMIT_ATOMIC_WRITE" },
          556  +      { SQLITE_FCNTL_ZIPVFS, "ZIPVFS" },
   555    557       };
   556    558       int i;
   557    559       for(i=0; i<sizeof(aF)/sizeof(aF[0]); i++){
   558    560         if( op==aF[i].iFnctl ) break;
   559    561       }
   560    562       if( i<sizeof(aF)/sizeof(aF[0]) ){
   561    563         int rc = 0;
   562    564         tvfsExecTcl(p, "xFileControl", 
   563    565             Tcl_NewStringObj(pFd->zFilename, -1), 
   564    566             Tcl_NewStringObj(aF[i].zFnctl, -1),
   565    567             0, 0
   566    568         );
   567    569         tvfsResultCode(p, &rc);
   568         -      if( rc ) return rc;
          570  +      if( rc ) return (rc<0 ? SQLITE_OK : rc);
   569    571       }
   570    572     }
   571    573     return sqlite3OsFileControl(pFd->pReal, op, pArg);
   572    574   }
   573    575   
   574    576   /*
   575    577   ** Return the sector-size in bytes for an tvfs-file.

Changes to src/update.c.

   151    151     Table *pTab;           /* The table to be updated */
   152    152     int addrTop = 0;       /* VDBE instruction address of the start of the loop */
   153    153     WhereInfo *pWInfo;     /* Information about the WHERE clause */
   154    154     Vdbe *v;               /* The virtual database engine */
   155    155     Index *pIdx;           /* For looping over indices */
   156    156     Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
   157    157     int nIdx;              /* Number of indices that need updating */
          158  +  int nAllIdx;           /* Total number of indexes */
   158    159     int iBaseCur;          /* Base cursor number */
   159    160     int iDataCur;          /* Cursor for the canonical data btree */
   160    161     int iIdxCur;           /* Cursor for the first index */
   161    162     sqlite3 *db;           /* The database structure */
   162         -  int *aRegIdx = 0;      /* First register in array assigned to each index */
          163  +  int *aRegIdx = 0;      /* Registers for to each index and the main table */
   163    164     int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
   164    165                            ** an expression for the i-th column of the table.
   165    166                            ** aXRef[i]==-1 if the i-th column is not changed. */
   166    167     u8 *aToOpen;           /* 1 for tables and indices to be opened */
   167    168     u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
   168    169     u8 chngRowid;          /* Rowid changed in a normal table */
   169    170     u8 chngKey;            /* Either chngPk or chngRowid */
................................................................................
   269    270       pParse->nTab = iBaseCur;
   270    271     }
   271    272     pTabList->a[0].iCursor = iDataCur;
   272    273   
   273    274     /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].  
   274    275     ** Initialize aXRef[] and aToOpen[] to their default values.
   275    276     */
   276         -  aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
          277  +  aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx+1) + nIdx+2 );
   277    278     if( aXRef==0 ) goto update_cleanup;
   278    279     aRegIdx = aXRef+pTab->nCol;
   279         -  aToOpen = (u8*)(aRegIdx+nIdx);
          280  +  aToOpen = (u8*)(aRegIdx+nIdx+1);
   280    281     memset(aToOpen, 1, nIdx+1);
   281    282     aToOpen[nIdx+1] = 0;
   282    283     for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
   283    284   
   284    285     /* Initialize the name-context */
   285    286     memset(&sNC, 0, sizeof(sNC));
   286    287     sNC.pParse = pParse;
................................................................................
   362    363     hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
   363    364   
   364    365     /* There is one entry in the aRegIdx[] array for each index on the table
   365    366     ** being updated.  Fill in aRegIdx[] with a register number that will hold
   366    367     ** the key for accessing each index.
   367    368     */
   368    369     if( onError==OE_Replace ) bReplace = 1;
   369         -  for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
          370  +  for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
   370    371       int reg;
   371    372       if( chngKey || hasFK>1 || pIdx==pPk
   372    373        || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
   373    374       ){
   374    375         reg = ++pParse->nMem;
   375    376         pParse->nMem += pIdx->nColumn;
   376    377       }else{
................................................................................
   382    383             if( onError==OE_Default && pIdx->onError==OE_Replace ){
   383    384               bReplace = 1;
   384    385             }
   385    386             break;
   386    387           }
   387    388         }
   388    389       }
   389         -    if( reg==0 ) aToOpen[j+1] = 0;
   390         -    aRegIdx[j] = reg;
          390  +    if( reg==0 ) aToOpen[nAllIdx+1] = 0;
          391  +    aRegIdx[nAllIdx] = reg;
   391    392     }
          393  +  aRegIdx[nAllIdx] = ++pParse->nMem;  /* Register storing the table record */
   392    394     if( bReplace ){
   393    395       /* If REPLACE conflict resolution might be invoked, open cursors on all 
   394    396       ** indexes in case they are needed to delete records.  */
   395    397       memset(aToOpen, 1, nIdx+1);
   396    398     }
   397    399   
   398    400     /* Begin generating code. */
................................................................................
   399    401     v = sqlite3GetVdbe(pParse);
   400    402     if( v==0 ) goto update_cleanup;
   401    403     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
   402    404     sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
   403    405   
   404    406     /* Allocate required registers. */
   405    407     if( !IsVirtual(pTab) ){
   406         -    regRowSet = ++pParse->nMem;
          408  +    /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
          409  +    ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
          410  +    ** reallocated.  aRegIdx[nAllIdx] is the register in which the main
          411  +    ** table record is written.  regRowSet holds the RowSet for the
          412  +    ** two-pass update algorithm. */
          413  +    assert( aRegIdx[nAllIdx]==pParse->nMem );
          414  +    regRowSet = aRegIdx[nAllIdx];
   407    415       regOldRowid = regNewRowid = ++pParse->nMem;
   408    416       if( chngPk || pTrigger || hasFK ){
   409    417         regOld = pParse->nMem + 1;
   410    418         pParse->nMem += pTab->nCol;
   411    419       }
   412    420       if( chngKey || pTrigger || hasFK ){
   413    421         regNewRowid = ++pParse->nMem;
................................................................................
   529    537   
   530    538     if( HasRowid(pTab) ){
   531    539       /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
   532    540       ** mode, write the rowid into the FIFO. In either of the one-pass modes,
   533    541       ** leave it in register regOldRowid.  */
   534    542       sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
   535    543       if( eOnePass==ONEPASS_OFF ){
          544  +      /* We need to use regRowSet, so reallocate aRegIdx[nAllIdx] */
          545  +      aRegIdx[nAllIdx] = ++pParse->nMem;
   536    546         sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
   537    547       }
   538    548     }else{
   539    549       /* Read the PK of the current row into an array of registers. In
   540    550       ** ONEPASS_OFF mode, serialize the array into a record and store it in
   541    551       ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
   542    552       ** the OP_OpenEphemeral instruction to a Noop (the ephemeral table 

Changes to src/util.c.

    13     13   **
    14     14   ** This file contains functions for allocating memory, comparing
    15     15   ** strings, and stuff like that.
    16     16   **
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include <stdarg.h>
    20         -#if HAVE_ISNAN || SQLITE_HAVE_ISNAN
    21         -# include <math.h>
    22         -#endif
           20  +#include <math.h>
    23     21   
    24     22   /*
    25     23   ** Routine needed to support the testcase() macro.
    26     24   */
    27     25   #ifdef SQLITE_COVERAGE_TEST
    28     26   void sqlite3Coverage(int x){
    29     27     static unsigned dummy = 0;
................................................................................
   318    316     }else if( zRight==0 ){
   319    317       return 1;
   320    318     }
   321    319     return sqlite3StrICmp(zLeft, zRight);
   322    320   }
   323    321   int sqlite3StrICmp(const char *zLeft, const char *zRight){
   324    322     unsigned char *a, *b;
   325         -  int c;
          323  +  int c, x;
   326    324     a = (unsigned char *)zLeft;
   327    325     b = (unsigned char *)zRight;
   328    326     for(;;){
   329         -    c = (int)UpperToLower[*a] - (int)UpperToLower[*b];
   330         -    if( c || *a==0 ) break;
          327  +    c = *a;
          328  +    x = *b;
          329  +    if( c==x ){
          330  +      if( c==0 ) break;
          331  +    }else{
          332  +      c = (int)UpperToLower[c] - (int)UpperToLower[x];
          333  +      if( c ) break;
          334  +    }
   331    335       a++;
   332    336       b++;
   333    337     }
   334    338     return c;
   335    339   }
   336    340   int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
   337    341     register unsigned char *a, *b;
................................................................................
   908    912   /*
   909    913   ** Read a 64-bit variable-length integer from memory starting at p[0].
   910    914   ** Return the number of bytes read.  The value is stored in *v.
   911    915   */
   912    916   u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
   913    917     u32 a,b,s;
   914    918   
   915         -  a = *p;
   916         -  /* a: p0 (unmasked) */
   917         -  if (!(a&0x80))
   918         -  {
   919         -    *v = a;
          919  +  if( ((signed char*)p)[0]>=0 ){
          920  +    *v = *p;
   920    921       return 1;
   921    922     }
   922         -
   923         -  p++;
   924         -  b = *p;
   925         -  /* b: p1 (unmasked) */
   926         -  if (!(b&0x80))
   927         -  {
   928         -    a &= 0x7f;
   929         -    a = a<<7;
   930         -    a |= b;
   931         -    *v = a;
          923  +  if( ((signed char*)p)[1]>=0 ){
          924  +    *v = ((u32)(p[0]&0x7f)<<7) | p[1];
   932    925       return 2;
   933    926     }
   934    927   
   935    928     /* Verify that constants are precomputed correctly */
   936    929     assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
   937    930     assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
   938    931   
   939         -  p++;
   940         -  a = a<<14;
          932  +  a = ((u32)p[0])<<14;
          933  +  b = p[1];
          934  +  p += 2;
   941    935     a |= *p;
   942    936     /* a: p0<<14 | p2 (unmasked) */
   943    937     if (!(a&0x80))
   944    938     {
   945    939       a &= SLOT_2_0;
   946    940       b &= 0x7f;
   947    941       b = b<<7;

Changes to src/vacuum.c.

   102    102   ** transient would cause the database file to appear to be deleted
   103    103   ** following reboot.
   104    104   */
   105    105   void sqlite3Vacuum(Parse *pParse, Token *pNm, Expr *pInto){
   106    106     Vdbe *v = sqlite3GetVdbe(pParse);
   107    107     int iDb = 0;
   108    108     if( v==0 ) goto build_vacuum_end;
          109  +  if( pParse->nErr ) goto build_vacuum_end;
   109    110     if( pNm ){
   110    111   #ifndef SQLITE_BUG_COMPATIBLE_20160819
   111    112       /* Default behavior:  Report an error if the argument to VACUUM is
   112    113       ** not recognized */
   113    114       iDb = sqlite3TwoPartName(pParse, pNm, pNm, &pNm);
   114    115       if( iDb<0 ) goto build_vacuum_end;
   115    116   #else

Changes to src/vdbe.c.

   191    191         if( (mNever&0x08)!=0 && (I&0x05)!=0) I |= 0x05; /*NO_TEST*/
   192    192       }
   193    193       sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
   194    194                                       iSrcLine&0xffffff, I, M);
   195    195     }
   196    196   #endif
   197    197   
   198         -/*
   199         -** Convert the given register into a string if it isn't one
   200         -** already. Return non-zero if a malloc() fails.
   201         -*/
   202         -#define Stringify(P, enc) \
   203         -   if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc,0)) \
   204         -     { goto no_mem; }
   205         -
   206    198   /*
   207    199   ** An ephemeral string value (signified by the MEM_Ephem flag) contains
   208    200   ** a pointer to a dynamically allocated string where some other entity
   209    201   ** is responsible for deallocating that string.  Because the register
   210    202   ** does not control the string, it might be deleted without the register
   211    203   ** knowing it.
   212    204   **
................................................................................
   260    252   
   261    253     assert( iCur>=0 && iCur<p->nCursor );
   262    254     if( p->apCsr[iCur] ){ /*OPTIMIZATION-IF-FALSE*/
   263    255       /* Before calling sqlite3VdbeFreeCursor(), ensure the isEphemeral flag
   264    256       ** is clear. Otherwise, if this is an ephemeral cursor created by 
   265    257       ** OP_OpenDup, the cursor will not be closed and will still be part
   266    258       ** of a BtShared.pCursor list.  */
   267         -    p->apCsr[iCur]->isEphemeral = 0;
          259  +    if( p->apCsr[iCur]->pBtx==0 ) p->apCsr[iCur]->isEphemeral = 0;
   268    260       sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
   269    261       p->apCsr[iCur] = 0;
   270    262     }
   271    263     if( SQLITE_OK==sqlite3VdbeMemClearAndResize(pMem, nByte) ){
   272    264       p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
   273    265       memset(pCx, 0, offsetof(VdbeCursor,pAltCursor));
   274    266       pCx->eCurType = eCurType;
................................................................................
   299    291   ** point or exponential notation, the result is only MEM_Real, even
   300    292   ** if there is an exact integer representation of the quantity.
   301    293   */
   302    294   static void applyNumericAffinity(Mem *pRec, int bTryForInt){
   303    295     double rValue;
   304    296     i64 iValue;
   305    297     u8 enc = pRec->enc;
   306         -  assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real))==MEM_Str );
          298  +  assert( (pRec->flags & (MEM_Str|MEM_Int|MEM_Real|MEM_IntReal))==MEM_Str );
   307    299     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
   308    300     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
   309    301       pRec->u.i = iValue;
   310    302       pRec->flags |= MEM_Int;
   311    303     }else{
   312    304       pRec->u.r = rValue;
   313    305       pRec->flags |= MEM_Real;
................................................................................
   356    348     }else if( affinity==SQLITE_AFF_TEXT ){
   357    349       /* Only attempt the conversion to TEXT if there is an integer or real
   358    350       ** representation (blob and NULL do not get converted) but no string
   359    351       ** representation.  It would be harmless to repeat the conversion if 
   360    352       ** there is already a string rep, but it is pointless to waste those
   361    353       ** CPU cycles. */
   362    354       if( 0==(pRec->flags&MEM_Str) ){ /*OPTIMIZATION-IF-FALSE*/
   363         -      if( (pRec->flags&(MEM_Real|MEM_Int)) ){
          355  +      if( (pRec->flags&(MEM_Real|MEM_Int|MEM_IntReal)) ){
          356  +        testcase( pRec->flags & MEM_Int );
          357  +        testcase( pRec->flags & MEM_Real );
          358  +        testcase( pRec->flags & MEM_IntReal );
   364    359           sqlite3VdbeMemStringify(pRec, enc, 1);
   365    360         }
   366    361       }
   367         -    pRec->flags &= ~(MEM_Real|MEM_Int);
          362  +    pRec->flags &= ~(MEM_Real|MEM_Int|MEM_IntReal);
   368    363     }
   369    364   }
   370    365   
   371    366   /*
   372    367   ** Try to convert the type of a function argument or a result column
   373    368   ** into a numeric representation.  Use either INTEGER or REAL whichever
   374    369   ** is appropriate.  But only do the conversion if it is possible without
................................................................................
   399    394   /*
   400    395   ** pMem currently only holds a string type (or maybe a BLOB that we can
   401    396   ** interpret as a string if we want to).  Compute its corresponding
   402    397   ** numeric type, if has one.  Set the pMem->u.r and pMem->u.i fields
   403    398   ** accordingly.
   404    399   */
   405    400   static u16 SQLITE_NOINLINE computeNumericType(Mem *pMem){
   406         -  assert( (pMem->flags & (MEM_Int|MEM_Real))==0 );
          401  +  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 );
   407    402     assert( (pMem->flags & (MEM_Str|MEM_Blob))!=0 );
   408    403     ExpandBlob(pMem);
   409    404     if( sqlite3AtoF(pMem->z, &pMem->u.r, pMem->n, pMem->enc)==0 ){
   410    405       return 0;
   411    406     }
   412    407     if( sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc)==0 ){
   413    408       return MEM_Int;
................................................................................
   419    414   ** Return the numeric type for pMem, either MEM_Int or MEM_Real or both or
   420    415   ** none.  
   421    416   **
   422    417   ** Unlike applyNumericAffinity(), this routine does not modify pMem->flags.
   423    418   ** But it does set pMem->u.r and pMem->u.i appropriately.
   424    419   */
   425    420   static u16 numericType(Mem *pMem){
   426         -  if( pMem->flags & (MEM_Int|MEM_Real) ){
   427         -    return pMem->flags & (MEM_Int|MEM_Real);
          421  +  if( pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal) ){
          422  +    testcase( pMem->flags & MEM_Int );
          423  +    testcase( pMem->flags & MEM_Real );
          424  +    testcase( pMem->flags & MEM_IntReal );
          425  +    return pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal);
   428    426     }
   429    427     if( pMem->flags & (MEM_Str|MEM_Blob) ){
          428  +    testcase( pMem->flags & MEM_Str );
          429  +    testcase( pMem->flags & MEM_Blob );
   430    430       return computeNumericType(pMem);
   431    431     }
   432    432     return 0;
   433    433   }
   434    434   
   435    435   #ifdef SQLITE_DEBUG
   436    436   /*
................................................................................
   518    518   static void memTracePrint(Mem *p){
   519    519     if( p->flags & MEM_Undefined ){
   520    520       printf(" undefined");
   521    521     }else if( p->flags & MEM_Null ){
   522    522       printf(p->flags & MEM_Zero ? " NULL-nochng" : " NULL");
   523    523     }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
   524    524       printf(" si:%lld", p->u.i);
          525  +  }else if( (p->flags & (MEM_IntReal))!=0 ){
          526  +    printf(" ir:%lld", p->u.i);
   525    527     }else if( p->flags & MEM_Int ){
   526    528       printf(" i:%lld", p->u.i);
   527    529   #ifndef SQLITE_OMIT_FLOATING_POINT
   528    530     }else if( p->flags & MEM_Real ){
   529    531       printf(" r:%g", p->u.r);
   530    532   #endif
   531    533     }else if( sqlite3VdbeMemIsRowSet(p) ){
................................................................................
  1459   1461   **   P3 = P2 || P1
  1460   1462   **
  1461   1463   ** It is illegal for P1 and P3 to be the same register. Sometimes,
  1462   1464   ** if P3 is the same register as P2, the implementation is able
  1463   1465   ** to avoid a memcpy().
  1464   1466   */
  1465   1467   case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
  1466         -  i64 nByte;
         1468  +  i64 nByte;          /* Total size of the output string or blob */
         1469  +  u16 flags1;         /* Initial flags for P1 */
         1470  +  u16 flags2;         /* Initial flags for P2 */
  1467   1471   
  1468   1472     pIn1 = &aMem[pOp->p1];
  1469   1473     pIn2 = &aMem[pOp->p2];
  1470   1474     pOut = &aMem[pOp->p3];
         1475  +  testcase( pIn1==pIn2 );
         1476  +  testcase( pOut==pIn2 );
  1471   1477     assert( pIn1!=pOut );
  1472         -  if( (pIn1->flags | pIn2->flags) & MEM_Null ){
         1478  +  flags1 = pIn1->flags;
         1479  +  testcase( flags1 & MEM_Null );
         1480  +  testcase( pIn2->flags & MEM_Null );
         1481  +  if( (flags1 | pIn2->flags) & MEM_Null ){
  1473   1482       sqlite3VdbeMemSetNull(pOut);
  1474   1483       break;
  1475   1484     }
  1476         -  if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
  1477         -  Stringify(pIn1, encoding);
  1478         -  Stringify(pIn2, encoding);
         1485  +  if( (flags1 & (MEM_Str|MEM_Blob))==0 ){
         1486  +    if( sqlite3VdbeMemStringify(pIn1,encoding,0) ) goto no_mem;
         1487  +    flags1 = pIn1->flags & ~MEM_Str;
         1488  +  }else if( (flags1 & MEM_Zero)!=0 ){
         1489  +    if( sqlite3VdbeMemExpandBlob(pIn1) ) goto no_mem;
         1490  +    flags1 = pIn1->flags & ~MEM_Str;
         1491  +  }
         1492  +  flags2 = pIn2->flags;
         1493  +  if( (flags2 & (MEM_Str|MEM_Blob))==0 ){
         1494  +    if( sqlite3VdbeMemStringify(pIn2,encoding,0) ) goto no_mem;
         1495  +    flags2 = pIn2->flags & ~MEM_Str;
         1496  +  }else if( (flags2 & MEM_Zero)!=0 ){
         1497  +    if( sqlite3VdbeMemExpandBlob(pIn2) ) goto no_mem;
         1498  +    flags2 = pIn2->flags & ~MEM_Str;
         1499  +  }
  1479   1500     nByte = pIn1->n + pIn2->n;
  1480   1501     if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  1481   1502       goto too_big;
  1482   1503     }
  1483   1504     if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
  1484   1505       goto no_mem;
  1485   1506     }
  1486   1507     MemSetTypeFlag(pOut, MEM_Str);
  1487   1508     if( pOut!=pIn2 ){
  1488   1509       memcpy(pOut->z, pIn2->z, pIn2->n);
         1510  +    assert( (pIn2->flags & MEM_Dyn) == (flags2 & MEM_Dyn) );
         1511  +    pIn2->flags = flags2;
  1489   1512     }
  1490   1513     memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
         1514  +  assert( (pIn1->flags & MEM_Dyn) == (flags1 & MEM_Dyn) );
         1515  +  pIn1->flags = flags1;
  1491   1516     pOut->z[nByte]=0;
  1492   1517     pOut->z[nByte+1] = 0;
  1493   1518     pOut->flags |= MEM_Term;
  1494   1519     pOut->n = (int)nByte;
  1495   1520     pOut->enc = encoding;
  1496   1521     UPDATE_MAX_BLOBSIZE(pOut);
  1497   1522     break;
................................................................................
  1609   1634       MemSetTypeFlag(pOut, MEM_Int);
  1610   1635   #else
  1611   1636       if( sqlite3IsNaN(rB) ){
  1612   1637         goto arithmetic_result_is_null;
  1613   1638       }
  1614   1639       pOut->u.r = rB;
  1615   1640       MemSetTypeFlag(pOut, MEM_Real);
  1616         -    if( ((type1|type2)&MEM_Real)==0 && !bIntint ){
         1641  +    if( ((type1|type2)&(MEM_Real|MEM_IntReal))==0 && !bIntint ){
  1617   1642         sqlite3VdbeIntegerAffinity(pOut);
  1618   1643       }
  1619   1644   #endif
  1620   1645     }
  1621   1646     break;
  1622   1647   
  1623   1648   arithmetic_result_is_null:
................................................................................
  1780   1805   ** This opcode is used when extracting information from a column that
  1781   1806   ** has REAL affinity.  Such column values may still be stored as
  1782   1807   ** integers, for space efficiency, but after extraction we want them
  1783   1808   ** to have only a real value.
  1784   1809   */
  1785   1810   case OP_RealAffinity: {                  /* in1 */
  1786   1811     pIn1 = &aMem[pOp->p1];
  1787         -  if( pIn1->flags & MEM_Int ){
         1812  +  if( pIn1->flags & (MEM_Int|MEM_IntReal) ){
         1813  +    testcase( pIn1->flags & MEM_Int );
         1814  +    testcase( pIn1->flags & MEM_IntReal );
  1788   1815       sqlite3VdbeMemRealify(pIn1);
  1789   1816     }
  1790   1817     break;
  1791   1818   }
  1792   1819   #endif
  1793   1820   
  1794   1821   #ifndef SQLITE_OMIT_CAST
................................................................................
  1972   1999         break;
  1973   2000       }
  1974   2001     }else{
  1975   2002       /* Neither operand is NULL.  Do a comparison. */
  1976   2003       affinity = pOp->p5 & SQLITE_AFF_MASK;
  1977   2004       if( affinity>=SQLITE_AFF_NUMERIC ){
  1978   2005         if( (flags1 | flags3)&MEM_Str ){
  1979         -        if( (flags1 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
         2006  +        if( (flags1 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
  1980   2007             applyNumericAffinity(pIn1,0);
  1981   2008             assert( flags3==pIn3->flags );
  1982   2009             /* testcase( flags3!=pIn3->flags );
  1983   2010             ** this used to be possible with pIn1==pIn3, but not since
  1984   2011             ** the column cache was removed.  The following assignment
  1985   2012             ** is essentially a no-op.  But, it provides defense-in-depth
  1986   2013             ** in case our analysis is incorrect, so it is left in. */
  1987   2014             flags3 = pIn3->flags;
  1988   2015           }
  1989         -        if( (flags3 & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
         2016  +        if( (flags3 & (MEM_Int|MEM_IntReal|MEM_Real|MEM_Str))==MEM_Str ){
  1990   2017             applyNumericAffinity(pIn3,0);
  1991   2018           }
  1992   2019         }
  1993   2020         /* Handle the common case of integer comparison here, as an
  1994   2021         ** optimization, to avoid a call to sqlite3MemCompare() */
  1995   2022         if( (pIn1->flags & pIn3->flags & MEM_Int)!=0 ){
  1996   2023           if( pIn3->u.i > pIn1->u.i ){ res = +1; goto compare_op; }
  1997   2024           if( pIn3->u.i < pIn1->u.i ){ res = -1; goto compare_op; }
  1998   2025           res = 0;
  1999   2026           goto compare_op;
  2000   2027         }
  2001   2028       }else if( affinity==SQLITE_AFF_TEXT ){
  2002         -      if( (flags1 & MEM_Str)==0 && (flags1 & (MEM_Int|MEM_Real))!=0 ){
         2029  +      if( (flags1 & MEM_Str)==0 && (flags1&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
  2003   2030           testcase( pIn1->flags & MEM_Int );
  2004   2031           testcase( pIn1->flags & MEM_Real );
         2032  +        testcase( pIn1->flags & MEM_IntReal );
  2005   2033           sqlite3VdbeMemStringify(pIn1, encoding, 1);
  2006   2034           testcase( (flags1&MEM_Dyn) != (pIn1->flags&MEM_Dyn) );
  2007   2035           flags1 = (pIn1->flags & ~MEM_TypeMask) | (flags1 & MEM_TypeMask);
  2008   2036           assert( pIn1!=pIn3 );
  2009   2037         }
  2010         -      if( (flags3 & MEM_Str)==0 && (flags3 & (MEM_Int|MEM_Real))!=0 ){
         2038  +      if( (flags3 & MEM_Str)==0 && (flags3&(MEM_Int|MEM_Real|MEM_IntReal))!=0 ){
  2011   2039           testcase( pIn3->flags & MEM_Int );
  2012   2040           testcase( pIn3->flags & MEM_Real );
         2041  +        testcase( pIn3->flags & MEM_IntReal );
  2013   2042           sqlite3VdbeMemStringify(pIn3, encoding, 1);
  2014   2043           testcase( (flags3&MEM_Dyn) != (pIn3->flags&MEM_Dyn) );
  2015   2044           flags3 = (pIn3->flags & ~MEM_TypeMask) | (flags3 & MEM_TypeMask);
  2016   2045         }
  2017   2046       }
  2018   2047       assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
  2019   2048       res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
................................................................................
  2761   2790     const char *zAffinity;   /* The affinity to be applied */
  2762   2791   
  2763   2792     zAffinity = pOp->p4.z;
  2764   2793     assert( zAffinity!=0 );
  2765   2794     assert( pOp->p2>0 );
  2766   2795     assert( zAffinity[pOp->p2]==0 );
  2767   2796     pIn1 = &aMem[pOp->p1];
  2768         -  do{
         2797  +  while( 1 /*edit-by-break*/ ){
  2769   2798       assert( pIn1 <= &p->aMem[(p->nMem+1 - p->nCursor)] );
  2770   2799       assert( memIsValid(pIn1) );
  2771         -    applyAffinity(pIn1, *(zAffinity++), encoding);
         2800  +    applyAffinity(pIn1, zAffinity[0], encoding);
         2801  +    if( zAffinity[0]==SQLITE_AFF_REAL && (pIn1->flags & MEM_Int)!=0 ){
         2802  +      /* When applying REAL affinity, if the result is still MEM_Int, 
         2803  +      ** indicate that REAL is actually desired */
         2804  +      pIn1->flags |= MEM_IntReal;
         2805  +      pIn1->flags &= ~MEM_Int;
         2806  +    }
         2807  +    REGISTER_TRACE((int)(pIn1-aMem), pIn1);
         2808  +    zAffinity++;
         2809  +    if( zAffinity[0]==0 ) break;
  2772   2810       pIn1++;
  2773         -  }while( zAffinity[0] );
         2811  +  }
  2774   2812     break;
  2775   2813   }
  2776   2814   
  2777   2815   /* Opcode: MakeRecord P1 P2 P3 P4 *
  2778   2816   ** Synopsis: r[P3]=mkrec(r[P1@P2])
  2779   2817   **
  2780   2818   ** Convert P2 registers beginning with P1 into the [record format]
................................................................................
  2787   2825   **
  2788   2826   ** The mapping from character to affinity is given by the SQLITE_AFF_
  2789   2827   ** macros defined in sqliteInt.h.
  2790   2828   **
  2791   2829   ** If P4 is NULL then all index fields have the affinity BLOB.
  2792   2830   */
  2793   2831   case OP_MakeRecord: {
  2794         -  u8 *zNewRecord;        /* A buffer to hold the data for the new record */
  2795   2832     Mem *pRec;             /* The new record */
  2796   2833     u64 nData;             /* Number of bytes of data space */
  2797   2834     int nHdr;              /* Number of bytes of header space */
  2798   2835     i64 nByte;             /* Data space required for this record */
  2799   2836     i64 nZero;             /* Number of zero bytes at the end of the record */
  2800   2837     int nVarint;           /* Number of bytes in a varint */
  2801   2838     u32 serial_type;       /* Type field */
  2802   2839     Mem *pData0;           /* First field to be combined into the record */
  2803   2840     Mem *pLast;            /* Last field of the record */
  2804   2841     int nField;            /* Number of fields in the record */
  2805   2842     char *zAffinity;       /* The affinity string for the record */
  2806   2843     int file_format;       /* File format to use for encoding */
  2807         -  int i;                 /* Space used in zNewRecord[] header */
  2808         -  int j;                 /* Space used in zNewRecord[] content */
  2809   2844     u32 len;               /* Length of a field */
         2845  +  u8 *zHdr;              /* Where to write next byte of the header */
         2846  +  u8 *zPayload;          /* Where to write next byte of the payload */
  2810   2847   
  2811   2848     /* Assuming the record contains N fields, the record format looks
  2812   2849     ** like this:
  2813   2850     **
  2814   2851     ** ------------------------------------------------------------------------
  2815   2852     ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 | 
  2816   2853     ** ------------------------------------------------------------------------
................................................................................
  2841   2878   
  2842   2879     /* Apply the requested affinity to all inputs
  2843   2880     */
  2844   2881     assert( pData0<=pLast );
  2845   2882     if( zAffinity ){
  2846   2883       pRec = pData0;
  2847   2884       do{
  2848         -      applyAffinity(pRec++, *(zAffinity++), encoding);
         2885  +      applyAffinity(pRec, zAffinity[0], encoding);
         2886  +      REGISTER_TRACE((int)(pRec-aMem), pRec);
         2887  +      zAffinity++;
         2888  +      pRec++;
  2849   2889         assert( zAffinity[0]==0 || pRec<=pLast );
  2850   2890       }while( zAffinity[0] );
  2851   2891     }
  2852   2892   
  2853   2893   #ifdef SQLITE_ENABLE_NULL_TRIM
  2854   2894     /* NULLs can be safely trimmed from the end of the record, as long as
  2855   2895     ** as the schema format is 2 or more and none of the omitted columns
................................................................................
  2929   2969       if( nByte+nZero>db->aLimit[SQLITE_LIMIT_LENGTH] ){
  2930   2970         goto too_big;
  2931   2971       }
  2932   2972       if( sqlite3VdbeMemClearAndResize(pOut, (int)nByte) ){
  2933   2973         goto no_mem;
  2934   2974       }
  2935   2975     }
  2936         -  zNewRecord = (u8 *)pOut->z;
         2976  +  pOut->n = (int)nByte;
         2977  +  pOut->flags = MEM_Blob;
         2978  +  if( nZero ){
         2979  +    pOut->u.nZero = nZero;
         2980  +    pOut->flags |= MEM_Zero;
         2981  +  }
         2982  +  UPDATE_MAX_BLOBSIZE(pOut);
         2983  +  zHdr = (u8 *)pOut->z;
         2984  +  zPayload = zHdr + nHdr;
  2937   2985   
  2938   2986     /* Write the record */
  2939         -  i = putVarint32(zNewRecord, nHdr);
  2940         -  j = nHdr;
         2987  +  zHdr += putVarint32(zHdr, nHdr);
  2941   2988     assert( pData0<=pLast );
  2942   2989     pRec = pData0;
  2943   2990     do{
  2944   2991       serial_type = pRec->uTemp;
  2945   2992       /* EVIDENCE-OF: R-06529-47362 Following the size varint are one or more
  2946   2993       ** additional varints, one per column. */
  2947         -    i += putVarint32(&zNewRecord[i], serial_type);            /* serial type */
         2994  +    zHdr += putVarint32(zHdr, serial_type);            /* serial type */
  2948   2995       /* EVIDENCE-OF: R-64536-51728 The values for each column in the record
  2949   2996       ** immediately follow the header. */
  2950         -    j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
         2997  +    zPayload += sqlite3VdbeSerialPut(zPayload, pRec, serial_type); /* content */
  2951   2998     }while( (++pRec)<=pLast );
  2952         -  assert( i==nHdr );
  2953         -  assert( j==nByte );
         2999  +  assert( nHdr==(int)(zHdr - (u8*)pOut->z) );
         3000  +  assert( nByte==(int)(zPayload - (u8*)pOut->z) );
  2954   3001   
  2955   3002     assert( pOp->p3>0 && pOp->p3<=(p->nMem+1 - p->nCursor) );
  2956         -  pOut->n = (int)nByte;
  2957         -  pOut->flags = MEM_Blob;
  2958         -  if( nZero ){
  2959         -    pOut->u.nZero = nZero;
  2960         -    pOut->flags |= MEM_Zero;
  2961         -  }
  2962   3003     REGISTER_TRACE(pOp->p3, pOut);
  2963         -  UPDATE_MAX_BLOBSIZE(pOut);
  2964   3004     break;
  2965   3005   }
  2966   3006   
  2967   3007   /* Opcode: Count P1 P2 * * *
  2968   3008   ** Synopsis: r[P2]=count()
  2969   3009   **
  2970   3010   ** Store the number of entries (an integer value) in the table or index 
................................................................................
  2986   3026     break;
  2987   3027   }
  2988   3028   #endif
  2989   3029   
  2990   3030   /* Opcode: Savepoint P1 * * P4 *
  2991   3031   **
  2992   3032   ** Open, release or rollback the savepoint named by parameter P4, depending
  2993         -** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
  2994         -** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
         3033  +** on the value of P1. To open a new savepoint set P1==0 (SAVEPOINT_BEGIN).
         3034  +** To release (commit) an existing savepoint set P1==1 (SAVEPOINT_RELEASE).
         3035  +** To rollback an existing savepoint set P1==2 (SAVEPOINT_ROLLBACK).
  2995   3036   */
  2996   3037   case OP_Savepoint: {
  2997   3038     int p1;                         /* Value of P1 operand */
  2998   3039     char *zName;                    /* Name of savepoint */
  2999   3040     int nName;
  3000   3041     Savepoint *pNew;
  3001   3042     Savepoint *pSavepoint;
................................................................................
  3055   3096           pNew->pNext = db->pSavepoint;
  3056   3097           db->pSavepoint = pNew;
  3057   3098           pNew->nDeferredCons = db->nDeferredCons;
  3058   3099           pNew->nDeferredImmCons = db->nDeferredImmCons;
  3059   3100         }
  3060   3101       }
  3061   3102     }else{
         3103  +    assert( p1==SAVEPOINT_RELEASE || p1==SAVEPOINT_ROLLBACK );
  3062   3104       iSavepoint = 0;
  3063   3105   
  3064   3106       /* Find the named savepoint. If there is no such savepoint, then an
  3065   3107       ** an error is returned to the user.  */
  3066   3108       for(
  3067   3109         pSavepoint = db->pSavepoint; 
  3068   3110         pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
................................................................................
  3109   3151             for(ii=0; ii<db->nDb; ii++){
  3110   3152               rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
  3111   3153                                          SQLITE_ABORT_ROLLBACK,
  3112   3154                                          isSchemaChange==0);
  3113   3155               if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3114   3156             }
  3115   3157           }else{
         3158  +          assert( p1==SAVEPOINT_RELEASE );
  3116   3159             isSchemaChange = 0;
  3117   3160           }
  3118   3161           for(ii=0; ii<db->nDb; ii++){
  3119   3162             rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
  3120   3163             if( rc!=SQLITE_OK ){
  3121   3164               goto abort_due_to_error;
  3122   3165             }
................................................................................
  3145   3188           assert( pSavepoint==db->pSavepoint );
  3146   3189           db->pSavepoint = pSavepoint->pNext;
  3147   3190           sqlite3DbFree(db, pSavepoint);
  3148   3191           if( !isTransaction ){
  3149   3192             db->nSavepoint--;
  3150   3193           }
  3151   3194         }else{
         3195  +        assert( p1==SAVEPOINT_ROLLBACK );
  3152   3196           db->nDeferredCons = pSavepoint->nDeferredCons;
  3153   3197           db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
  3154   3198         }
  3155   3199   
  3156   3200         if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
  3157   3201           rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
  3158   3202           if( rc!=SQLITE_OK ) goto abort_due_to_error;
................................................................................
  3713   3757         SQLITE_OPEN_TRANSIENT_DB;
  3714   3758     assert( pOp->p1>=0 );
  3715   3759     assert( pOp->p2>=0 );
  3716   3760     pCx = p->apCsr[pOp->p1];
  3717   3761     if( pCx ){
  3718   3762       /* If the ephermeral table is already open, erase all existing content
  3719   3763       ** so that the table is empty again, rather than creating a new table. */
  3720         -    rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
         3764  +    assert( pCx->isEphemeral );
         3765  +    if( pCx->pBtx ){
         3766  +      rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
         3767  +    }
  3721   3768     }else{
  3722   3769       pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
  3723   3770       if( pCx==0 ) goto no_mem;
  3724   3771       pCx->nullRow = 1;
  3725   3772       pCx->isEphemeral = 1;
  3726   3773       rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, 
  3727   3774                             BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
................................................................................
  3981   4028     oc = pOp->opcode;
  3982   4029     eqOnly = 0;
  3983   4030     pC->nullRow = 0;
  3984   4031   #ifdef SQLITE_DEBUG
  3985   4032     pC->seekOp = pOp->opcode;
  3986   4033   #endif
  3987   4034   
         4035  +  pC->deferredMoveto = 0;
         4036  +  pC->cacheStatus = CACHE_STALE;
  3988   4037     if( pC->isTable ){
  3989   4038       /* The BTREE_SEEK_EQ flag is only set on index cursors */
  3990   4039       assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
  3991   4040                 || CORRUPT_DB );
  3992   4041   
  3993   4042       /* The input value in P3 might be of any type: integer, real, string,
  3994   4043       ** blob, or NULL.  But it needs to be an integer before we can do
  3995   4044       ** the seek, so convert it. */
  3996   4045       pIn3 = &aMem[pOp->p3];
  3997         -    if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
         4046  +    if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
  3998   4047         applyNumericAffinity(pIn3, 0);
  3999   4048       }
  4000   4049       iKey = sqlite3VdbeIntValue(pIn3);
  4001   4050   
  4002   4051       /* If the P3 value could not be converted into an integer without
  4003   4052       ** loss of information, then special processing is required... */
  4004         -    if( (pIn3->flags & MEM_Int)==0 ){
         4053  +    if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
  4005   4054         if( (pIn3->flags & MEM_Real)==0 ){
  4006         -        /* If the P3 value cannot be converted into any kind of a number,
  4007         -        ** then the seek is not possible, so jump to P2 */
  4008         -        VdbeBranchTaken(1,2); goto jump_to_p2;
  4009         -        break;
  4010         -      }
         4055  +        if( (pIn3->flags & MEM_Null) || oc>=OP_SeekGE ){
         4056  +          VdbeBranchTaken(1,2); goto jump_to_p2;
         4057  +          break;
         4058  +        }else{
         4059  +          rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
         4060  +          if( rc!=SQLITE_OK ) goto abort_due_to_error;
         4061  +          goto seek_not_found;
         4062  +        }
         4063  +      }else
  4011   4064   
  4012   4065         /* If the approximation iKey is larger than the actual real search
  4013   4066         ** term, substitute >= for > and < for <=. e.g. if the search term
  4014   4067         ** is 4.9 and the integer approximation 5:
  4015   4068         **
  4016   4069         **        (x >  4.9)    ->     (x >= 5)
  4017   4070         **        (x <= 4.9)    ->     (x <  5)
................................................................................
  4027   4080         ** term, substitute <= for < and > for >=.  */
  4028   4081         else if( pIn3->u.r>(double)iKey ){
  4029   4082           assert( OP_SeekLE==(OP_SeekLT+1) );
  4030   4083           assert( OP_SeekGT==(OP_SeekGE+1) );
  4031   4084           assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
  4032   4085           if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
  4033   4086         }
  4034         -    } 
         4087  +    }
  4035   4088       rc = sqlite3BtreeMovetoUnpacked(pC->uc.pCursor, 0, (u64)iKey, 0, &res);
  4036   4089       pC->movetoTarget = iKey;  /* Used by OP_Delete */
  4037   4090       if( rc!=SQLITE_OK ){
  4038   4091         goto abort_due_to_error;
  4039   4092       }
  4040   4093     }else{
  4041   4094       /* For a cursor with the BTREE_SEEK_EQ hint, only the OP_SeekGE and
................................................................................
  4081   4134         goto abort_due_to_error;
  4082   4135       }
  4083   4136       if( eqOnly && r.eqSeen==0 ){
  4084   4137         assert( res!=0 );
  4085   4138         goto seek_not_found;
  4086   4139       }
  4087   4140     }
  4088         -  pC->deferredMoveto = 0;
  4089         -  pC->cacheStatus = CACHE_STALE;
  4090   4141   #ifdef SQLITE_TEST
  4091   4142     sqlite3_search_count++;
  4092   4143   #endif
  4093   4144     if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
  4094   4145       if( res<0 || (res==0 && oc==OP_SeekGT) ){
  4095   4146         res = 0;
  4096   4147         rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
................................................................................
  4382   4433   case OP_SeekRowid: {        /* jump, in3 */
  4383   4434     VdbeCursor *pC;
  4384   4435     BtCursor *pCrsr;
  4385   4436     int res;
  4386   4437     u64 iKey;
  4387   4438   
  4388   4439     pIn3 = &aMem[pOp->p3];
  4389         -  if( (pIn3->flags & MEM_Int)==0 ){
         4440  +  testcase( pIn3->flags & MEM_Int );
         4441  +  testcase( pIn3->flags & MEM_IntReal );
         4442  +  if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
  4390   4443       /* Make sure pIn3->u.i contains a valid integer representation of
  4391   4444       ** the key value, but do not change the datatype of the register, as
  4392   4445       ** other parts of the perpared statement might be depending on the
  4393   4446       ** current datatype. */
  4394   4447       u16 origFlags = pIn3->flags;
  4395   4448       int isNotInt;
  4396   4449       applyAffinity(pIn3, SQLITE_AFF_NUMERIC, encoding);

Changes to src/vdbeInt.h.

   241    241   ** flags may coexist with the MEM_Str flag.
   242    242   */
   243    243   #define MEM_Null      0x0001   /* Value is NULL (or a pointer) */
   244    244   #define MEM_Str       0x0002   /* Value is a string */
   245    245   #define MEM_Int       0x0004   /* Value is an integer */
   246    246   #define MEM_Real      0x0008   /* Value is a real number */
   247    247   #define MEM_Blob      0x0010   /* Value is a BLOB */
   248         -#define MEM_AffMask   0x001f   /* Mask of affinity bits */
   249         -#define MEM_FromBind  0x0020   /* Value originates from sqlite3_bind() */
   250         -/* Available          0x0040   */
          248  +#define MEM_IntReal   0x0020   /* MEM_Int that stringifies like MEM_Real */
          249  +#define MEM_AffMask   0x003f   /* Mask of affinity bits */
          250  +#define MEM_FromBind  0x0040   /* Value originates from sqlite3_bind() */
   251    251   #define MEM_Undefined 0x0080   /* Value is undefined */
   252    252   #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
   253         -#define MEM_TypeMask  0xc1df   /* Mask of type bits */
          253  +#define MEM_TypeMask  0xc1bf   /* Mask of type bits */
   254    254   
   255    255   
   256    256   /* Whenever Mem contains a valid string or blob representation, one of
   257    257   ** the following flags must be set to determine the memory management
   258    258   ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
   259    259   ** string is \000 or \u0000 terminated
   260    260   */

Changes to src/vdbeapi.c.

   230    230   #endif /* SQLITE_OMIT_UTF16 */
   231    231   /* EVIDENCE-OF: R-12793-43283 Every value in SQLite has one of five
   232    232   ** fundamental datatypes: 64-bit signed integer 64-bit IEEE floating
   233    233   ** point number string BLOB NULL
   234    234   */
   235    235   int sqlite3_value_type(sqlite3_value* pVal){
   236    236     static const u8 aType[] = {
   237         -     SQLITE_BLOB,     /* 0x00 */
   238         -     SQLITE_NULL,     /* 0x01 */
   239         -     SQLITE_TEXT,     /* 0x02 */
   240         -     SQLITE_NULL,     /* 0x03 */
   241         -     SQLITE_INTEGER,  /* 0x04 */
   242         -     SQLITE_NULL,     /* 0x05 */
   243         -     SQLITE_INTEGER,  /* 0x06 */
   244         -     SQLITE_NULL,     /* 0x07 */
   245         -     SQLITE_FLOAT,    /* 0x08 */
   246         -     SQLITE_NULL,     /* 0x09 */
   247         -     SQLITE_FLOAT,    /* 0x0a */
   248         -     SQLITE_NULL,     /* 0x0b */
   249         -     SQLITE_INTEGER,  /* 0x0c */
   250         -     SQLITE_NULL,     /* 0x0d */
   251         -     SQLITE_INTEGER,  /* 0x0e */
   252         -     SQLITE_NULL,     /* 0x0f */
   253         -     SQLITE_BLOB,     /* 0x10 */
   254         -     SQLITE_NULL,     /* 0x11 */
   255         -     SQLITE_TEXT,     /* 0x12 */
   256         -     SQLITE_NULL,     /* 0x13 */
   257         -     SQLITE_INTEGER,  /* 0x14 */
   258         -     SQLITE_NULL,     /* 0x15 */
   259         -     SQLITE_INTEGER,  /* 0x16 */
   260         -     SQLITE_NULL,     /* 0x17 */
   261         -     SQLITE_FLOAT,    /* 0x18 */
   262         -     SQLITE_NULL,     /* 0x19 */
   263         -     SQLITE_FLOAT,    /* 0x1a */
   264         -     SQLITE_NULL,     /* 0x1b */
   265         -     SQLITE_INTEGER,  /* 0x1c */
   266         -     SQLITE_NULL,     /* 0x1d */
   267         -     SQLITE_INTEGER,  /* 0x1e */
   268         -     SQLITE_NULL,     /* 0x1f */
          237  +     SQLITE_BLOB,     /* 0x00 (not possible) */
          238  +     SQLITE_NULL,     /* 0x01 NULL */
          239  +     SQLITE_TEXT,     /* 0x02 TEXT */
          240  +     SQLITE_NULL,     /* 0x03 (not possible) */
          241  +     SQLITE_INTEGER,  /* 0x04 INTEGER */
          242  +     SQLITE_NULL,     /* 0x05 (not possible) */
          243  +     SQLITE_INTEGER,  /* 0x06 INTEGER + TEXT */
          244  +     SQLITE_NULL,     /* 0x07 (not possible) */
          245  +     SQLITE_FLOAT,    /* 0x08 FLOAT */
          246  +     SQLITE_NULL,     /* 0x09 (not possible) */
          247  +     SQLITE_FLOAT,    /* 0x0a FLOAT + TEXT */
          248  +     SQLITE_NULL,     /* 0x0b (not possible) */
          249  +     SQLITE_INTEGER,  /* 0x0c (not possible) */
          250  +     SQLITE_NULL,     /* 0x0d (not possible) */
          251  +     SQLITE_INTEGER,  /* 0x0e (not possible) */
          252  +     SQLITE_NULL,     /* 0x0f (not possible) */
          253  +     SQLITE_BLOB,     /* 0x10 BLOB */
          254  +     SQLITE_NULL,     /* 0x11 (not possible) */
          255  +     SQLITE_TEXT,     /* 0x12 (not possible) */
          256  +     SQLITE_NULL,     /* 0x13 (not possible) */
          257  +     SQLITE_INTEGER,  /* 0x14 INTEGER + BLOB */
          258  +     SQLITE_NULL,     /* 0x15 (not possible) */
          259  +     SQLITE_INTEGER,  /* 0x16 (not possible) */
          260  +     SQLITE_NULL,     /* 0x17 (not possible) */
          261  +     SQLITE_FLOAT,    /* 0x18 FLOAT + BLOB */
          262  +     SQLITE_NULL,     /* 0x19 (not possible) */
          263  +     SQLITE_FLOAT,    /* 0x1a (not possible) */
          264  +     SQLITE_NULL,     /* 0x1b (not possible) */
          265  +     SQLITE_INTEGER,  /* 0x1c (not possible) */
          266  +     SQLITE_NULL,     /* 0x1d (not possible) */
          267  +     SQLITE_INTEGER,  /* 0x1e (not possible) */
          268  +     SQLITE_NULL,     /* 0x1f (not possible) */
          269  +     SQLITE_FLOAT,    /* 0x20 INTREAL */
          270  +     SQLITE_NULL,     /* 0x21 (not possible) */
          271  +     SQLITE_TEXT,     /* 0x22 INTREAL + TEXT */
          272  +     SQLITE_NULL,     /* 0x23 (not possible) */
          273  +     SQLITE_FLOAT,    /* 0x24 (not possible) */
          274  +     SQLITE_NULL,     /* 0x25 (not possible) */
          275  +     SQLITE_FLOAT,    /* 0x26 (not possible) */
          276  +     SQLITE_NULL,     /* 0x27 (not possible) */
          277  +     SQLITE_FLOAT,    /* 0x28 (not possible) */
          278  +     SQLITE_NULL,     /* 0x29 (not possible) */
          279  +     SQLITE_FLOAT,    /* 0x2a (not possible) */
          280  +     SQLITE_NULL,     /* 0x2b (not possible) */
          281  +     SQLITE_FLOAT,    /* 0x2c (not possible) */
          282  +     SQLITE_NULL,     /* 0x2d (not possible) */
          283  +     SQLITE_FLOAT,    /* 0x2e (not possible) */
          284  +     SQLITE_NULL,     /* 0x2f (not possible) */
          285  +     SQLITE_BLOB,     /* 0x30 (not possible) */
          286  +     SQLITE_NULL,     /* 0x31 (not possible) */
          287  +     SQLITE_TEXT,     /* 0x32 (not possible) */
          288  +     SQLITE_NULL,     /* 0x33 (not possible) */
          289  +     SQLITE_FLOAT,    /* 0x34 (not possible) */
          290  +     SQLITE_NULL,     /* 0x35 (not possible) */
          291  +     SQLITE_FLOAT,    /* 0x36 (not possible) */
          292  +     SQLITE_NULL,     /* 0x37 (not possible) */
          293  +     SQLITE_FLOAT,    /* 0x38 (not possible) */
          294  +     SQLITE_NULL,     /* 0x39 (not possible) */
          295  +     SQLITE_FLOAT,    /* 0x3a (not possible) */
          296  +     SQLITE_NULL,     /* 0x3b (not possible) */
          297  +     SQLITE_FLOAT,    /* 0x3c (not possible) */
          298  +     SQLITE_NULL,     /* 0x3d (not possible) */
          299  +     SQLITE_FLOAT,    /* 0x3e (not possible) */
          300  +     SQLITE_NULL,     /* 0x3f (not possible) */
   269    301     };
          302  +#ifdef SQLITE_DEBUG
          303  +  {
          304  +    int eType = SQLITE_BLOB;
          305  +    if( pVal->flags & MEM_Null ){
          306  +      eType = SQLITE_NULL;
          307  +    }else if( pVal->flags & (MEM_Real|MEM_IntReal) ){
          308  +      eType = SQLITE_FLOAT;
          309  +    }else if( pVal->flags & MEM_Int ){
          310  +      eType = SQLITE_INTEGER;
          311  +    }else if( pVal->flags & MEM_Str ){
          312  +      eType = SQLITE_TEXT;
          313  +    }
          314  +    assert( eType == aType[pVal->flags&MEM_AffMask] );
          315  +  }
          316  +#endif
   270    317     return aType[pVal->flags&MEM_AffMask];
   271    318   }
   272    319   
   273    320   /* Return true if a parameter to xUpdate represents an unchanged column */
   274    321   int sqlite3_value_nochange(sqlite3_value *pVal){
   275    322     return (pVal->flags&(MEM_Null|MEM_Zero))==(MEM_Null|MEM_Zero);
   276    323   }
................................................................................
   511    558   /* An SQLITE_NOMEM error. */
   512    559   void sqlite3_result_error_nomem(sqlite3_context *pCtx){
   513    560     assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
   514    561     sqlite3VdbeMemSetNull(pCtx->pOut);
   515    562     pCtx->isError = SQLITE_NOMEM_BKPT;
   516    563     sqlite3OomFault(pCtx->pOut->db);
   517    564   }
          565  +
          566  +#ifndef SQLITE_UNTESTABLE
          567  +/* Force the INT64 value currently stored as the result to be
          568  +** a MEM_IntReal value.  See the SQLITE_TESTCTRL_RESULT_INTREAL
          569  +** test-control.
          570  +*/
          571  +void sqlite3ResultIntReal(sqlite3_context *pCtx){ 
          572  +  assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
          573  +  if( pCtx->pOut->flags & MEM_Int ){
          574  +    pCtx->pOut->flags &= ~MEM_Int;
          575  +    pCtx->pOut->flags |= MEM_IntReal;
          576  +  }
          577  +}
          578  +#endif
          579  +
   518    580   
   519    581   /*
   520    582   ** This function is called after a transaction has been committed. It 
   521    583   ** invokes callbacks registered with sqlite3_wal_hook() as required.
   522    584   */
   523    585   static int doWalCallbacks(sqlite3 *db){
   524    586     int rc = SQLITE_OK;
................................................................................
  1798   1860   
  1799   1861     pMem = *ppValue = &p->pUnpacked->aMem[iIdx];
  1800   1862     if( iIdx==p->pTab->iPKey ){
  1801   1863       sqlite3VdbeMemSetInt64(pMem, p->iKey1);
  1802   1864     }else if( iIdx>=p->pUnpacked->nField ){
  1803   1865       *ppValue = (sqlite3_value *)columnNullValue();
  1804   1866     }else if( p->pTab->aCol[iIdx].affinity==SQLITE_AFF_REAL ){
  1805         -    if( pMem->flags & MEM_Int ){
         1867  +    if( pMem->flags & (MEM_Int|MEM_IntReal) ){
         1868  +      testcase( pMem->flags & MEM_Int );
         1869  +      testcase( pMem->flags & MEM_IntReal );
  1806   1870         sqlite3VdbeMemRealify(pMem);
  1807   1871       }
  1808   1872     }
  1809   1873   
  1810   1874    preupdate_old_out:
  1811   1875     sqlite3Error(db, rc);
  1812   1876     return sqlite3ApiExit(db, rc);

Changes to src/vdbeaux.c.

  1530   1530         sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
  1531   1531         break;
  1532   1532       }
  1533   1533       case P4_MEM: {
  1534   1534         Mem *pMem = pOp->p4.pMem;
  1535   1535         if( pMem->flags & MEM_Str ){
  1536   1536           zP4 = pMem->z;
  1537         -      }else if( pMem->flags & MEM_Int ){
         1537  +      }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
  1538   1538           sqlite3_str_appendf(&x, "%lld", pMem->u.i);
  1539   1539         }else if( pMem->flags & MEM_Real ){
  1540   1540           sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
  1541   1541         }else if( pMem->flags & MEM_Null ){
  1542   1542           zP4 = "NULL";
  1543   1543         }else{
  1544   1544           assert( pMem->flags & MEM_Blob );
................................................................................
  2910   2910             db->bConcurrent = 0;
  2911   2911             p->nChange = 0;
  2912   2912           }
  2913   2913         }
  2914   2914       }
  2915   2915   
  2916   2916       /* Check for immediate foreign key violations. */
  2917         -    if( p->rc==SQLITE_OK ){
         2917  +    if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
  2918   2918         sqlite3VdbeCheckFk(p, 0);
  2919   2919       }
  2920   2920     
  2921   2921       /* If the auto-commit flag is set and this is the only active writer 
  2922   2922       ** VM, then we do either a commit or rollback of the current transaction. 
  2923   2923       **
  2924   2924       ** Note: This block also runs if one of the special errors handled 
................................................................................
  3438   3438   **
  3439   3439   ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
  3440   3440   ** of SQLite will not understand those serial types.
  3441   3441   */
  3442   3442   
  3443   3443   /*
  3444   3444   ** Return the serial-type for the value stored in pMem.
         3445  +**
         3446  +** This routine might convert a large MEM_IntReal value into MEM_Real.
  3445   3447   */
  3446   3448   u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
  3447   3449     int flags = pMem->flags;
  3448   3450     u32 n;
  3449   3451   
  3450   3452     assert( pLen!=0 );
  3451   3453     if( flags&MEM_Null ){
  3452   3454       *pLen = 0;
  3453   3455       return 0;
  3454   3456     }
  3455         -  if( flags&MEM_Int ){
         3457  +  if( flags&(MEM_Int|MEM_IntReal) ){
  3456   3458       /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  3457   3459   #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
  3458   3460       i64 i = pMem->u.i;
  3459   3461       u64 u;
         3462  +    testcase( flags & MEM_Int );
         3463  +    testcase( flags & MEM_IntReal );
  3460   3464       if( i<0 ){
  3461   3465         u = ~i;
  3462   3466       }else{
  3463   3467         u = i;
  3464   3468       }
  3465   3469       if( u<=127 ){
  3466   3470         if( (i&1)==i && file_format>=4 ){
................................................................................
  3472   3476         }
  3473   3477       }
  3474   3478       if( u<=32767 ){ *pLen = 2; return 2; }
  3475   3479       if( u<=8388607 ){ *pLen = 3; return 3; }
  3476   3480       if( u<=2147483647 ){ *pLen = 4; return 4; }
  3477   3481       if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
  3478   3482       *pLen = 8;
         3483  +    if( flags&MEM_IntReal ){
         3484  +      /* If the value is IntReal and is going to take up 8 bytes to store
         3485  +      ** as an integer, then we might as well make it an 8-byte floating
         3486  +      ** point value */
         3487  +      pMem->u.r = (double)pMem->u.i;
         3488  +      pMem->flags &= ~MEM_IntReal;
         3489  +      pMem->flags |= MEM_Real;
         3490  +      return 7;
         3491  +    }
  3479   3492       return 6;
  3480   3493     }
  3481   3494     if( flags&MEM_Real ){
  3482   3495       *pLen = 8;
  3483   3496       return 7;
  3484   3497     }
  3485   3498     assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
................................................................................
  4127   4140     */
  4128   4141     if( combined_flags&MEM_Null ){
  4129   4142       return (f2&MEM_Null) - (f1&MEM_Null);
  4130   4143     }
  4131   4144   
  4132   4145     /* At least one of the two values is a number
  4133   4146     */
  4134         -  if( combined_flags&(MEM_Int|MEM_Real) ){
  4135         -    if( (f1 & f2 & MEM_Int)!=0 ){
         4147  +  if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){
         4148  +    testcase( combined_flags & MEM_Int );
         4149  +    testcase( combined_flags & MEM_Real );
         4150  +    testcase( combined_flags & MEM_IntReal );
         4151  +    if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){
         4152  +      testcase( f1 & f2 & MEM_Int );
         4153  +      testcase( f1 & f2 & MEM_IntReal );
  4136   4154         if( pMem1->u.i < pMem2->u.i ) return -1;
  4137   4155         if( pMem1->u.i > pMem2->u.i ) return +1;
  4138   4156         return 0;
  4139   4157       }
  4140   4158       if( (f1 & f2 & MEM_Real)!=0 ){
  4141   4159         if( pMem1->u.r < pMem2->u.r ) return -1;
  4142   4160         if( pMem1->u.r > pMem2->u.r ) return +1;
  4143   4161         return 0;
  4144   4162       }
  4145         -    if( (f1&MEM_Int)!=0 ){
         4163  +    if( (f1&(MEM_Int|MEM_IntReal))!=0 ){
         4164  +      testcase( f1 & MEM_Int );
         4165  +      testcase( f1 & MEM_IntReal );
  4146   4166         if( (f2&MEM_Real)!=0 ){
  4147   4167           return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
         4168  +      }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
         4169  +        if( pMem1->u.i < pMem2->u.i ) return -1;
         4170  +        if( pMem1->u.i > pMem2->u.i ) return +1;
         4171  +        return 0;
  4148   4172         }else{
  4149   4173           return -1;
  4150   4174         }
  4151   4175       }
  4152   4176       if( (f1&MEM_Real)!=0 ){
  4153         -      if( (f2&MEM_Int)!=0 ){
         4177  +      if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
         4178  +        testcase( f2 & MEM_Int );
         4179  +        testcase( f2 & MEM_IntReal );
  4154   4180           return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
  4155   4181         }else{
  4156   4182           return -1;
  4157   4183         }
  4158   4184       }
  4159   4185       return +1;
  4160   4186     }
................................................................................
  4295   4321     assert( pPKey2->pKeyInfo->aSortOrder!=0 );
  4296   4322     assert( pPKey2->pKeyInfo->nKeyField>0 );
  4297   4323     assert( idx1<=szHdr1 || CORRUPT_DB );
  4298   4324     do{
  4299   4325       u32 serial_type;
  4300   4326   
  4301   4327       /* RHS is an integer */
  4302         -    if( pRhs->flags & MEM_Int ){
         4328  +    if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
         4329  +      testcase( pRhs->flags & MEM_Int );
         4330  +      testcase( pRhs->flags & MEM_IntReal );
  4303   4331         serial_type = aKey1[idx1];
  4304   4332         testcase( serial_type==12 );
  4305   4333         if( serial_type>=10 ){
  4306   4334           rc = +1;
  4307   4335         }else if( serial_type==0 ){
  4308   4336           rc = -1;
  4309   4337         }else if( serial_type==7 ){
................................................................................
  4640   4668       }
  4641   4669       if( (flags & MEM_Int) ){
  4642   4670         return vdbeRecordCompareInt;
  4643   4671       }
  4644   4672       testcase( flags & MEM_Real );
  4645   4673       testcase( flags & MEM_Null );
  4646   4674       testcase( flags & MEM_Blob );
  4647         -    if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
         4675  +    if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0
         4676  +     && p->pKeyInfo->aColl[0]==0
         4677  +    ){
  4648   4678         assert( flags & MEM_Str );
  4649   4679         return vdbeRecordCompareString;
  4650   4680       }
  4651   4681     }
  4652   4682   
  4653   4683     return sqlite3VdbeRecordCompare;
  4654   4684   }

Changes to src/vdbemem.c.

    14     14   ** stores a single value in the VDBE.  Mem is an opaque structure visible
    15     15   ** only within the VDBE.  Interface routines refer to a Mem using the
    16     16   ** name sqlite_value
    17     17   */
    18     18   #include "sqliteInt.h"
    19     19   #include "vdbeInt.h"
    20     20   
           21  +/* True if X is a power of two.  0 is considered a power of two here.
           22  +** In other words, return true if X has at most one bit set.
           23  +*/
           24  +#define ISPOWEROF2(X)  (((X)&((X)-1))==0)
           25  +
    21     26   #ifdef SQLITE_DEBUG
    22     27   /*
    23     28   ** Check invariants on a Mem object.
    24     29   **
    25     30   ** This routine is intended for use inside of assert() statements, like
    26     31   ** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
    27     32   */
................................................................................
    33     38   
    34     39     /* MEM_Dyn may only be set if Mem.szMalloc==0.  In this way we
    35     40     ** ensure that if Mem.szMalloc>0 then it is safe to do
    36     41     ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
    37     42     ** That saves a few cycles in inner loops. */
    38     43     assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
    39     44   
    40         -  /* Cannot be both MEM_Int and MEM_Real at the same time */
    41         -  assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
           45  +  /* Cannot have more than one of MEM_Int, MEM_Real, or MEM_IntReal */
           46  +  assert( ISPOWEROF2(p->flags & (MEM_Int|MEM_Real|MEM_IntReal)) );
    42     47   
    43     48     if( p->flags & MEM_Null ){
    44     49       /* Cannot be both MEM_Null and some other type */
    45     50       assert( (p->flags & (MEM_Int|MEM_Real|MEM_Str|MEM_Blob|MEM_Agg))==0 );
    46     51   
    47     52       /* If MEM_Null is set, then either the value is a pure NULL (the usual
    48     53       ** case) or it is a pointer set using sqlite3_bind_pointer() or
................................................................................
    87     92         ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
    88     93         ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
    89     94       );
    90     95     }
    91     96     return 1;
    92     97   }
    93     98   #endif
           99  +
          100  +/*
          101  +** Render a Mem object which is one of MEM_Int, MEM_Real, or MEM_IntReal
          102  +** into a buffer.
          103  +*/
          104  +static void vdbeMemRenderNum(int sz, char *zBuf, Mem *p){
          105  +  StrAccum acc;
          106  +  assert( p->flags & (MEM_Int|MEM_Real|MEM_IntReal) );
          107  +  sqlite3StrAccumInit(&acc, 0, zBuf, sz, 0);
          108  +  if( p->flags & MEM_Int ){
          109  +    sqlite3_str_appendf(&acc, "%lld", p->u.i);
          110  +  }else if( p->flags & MEM_IntReal ){
          111  +    sqlite3_str_appendf(&acc, "%!.15g", (double)p->u.i);
          112  +  }else{
          113  +    sqlite3_str_appendf(&acc, "%!.15g", p->u.r);
          114  +  }
          115  +  assert( acc.zText==zBuf && acc.mxAlloc<=0 );
          116  +  zBuf[acc.nChar] = 0; /* Fast version of sqlite3StrAccumFinish(&acc) */
          117  +}
    94    118   
    95    119   #ifdef SQLITE_DEBUG
    96    120   /*
    97    121   ** Check that string value of pMem agrees with its integer or real value.
    98    122   **
    99    123   ** A single int or real value always converts to the same strings.  But
   100    124   ** many different strings can be converted into the same int or real.
................................................................................
   113    137   ** This routine is for use inside of assert() statements only.
   114    138   */
   115    139   int sqlite3VdbeMemConsistentDualRep(Mem *p){
   116    140     char zBuf[100];
   117    141     char *z;
   118    142     int i, j, incr;
   119    143     if( (p->flags & MEM_Str)==0 ) return 1;
   120         -  if( (p->flags & (MEM_Int|MEM_Real))==0 ) return 1;
   121         -  if( p->flags & MEM_Int ){
   122         -    sqlite3_snprintf(sizeof(zBuf),zBuf,"%lld",p->u.i);
   123         -  }else{
   124         -    sqlite3_snprintf(sizeof(zBuf),zBuf,"%!.15g",p->u.r);
   125         -  }
          144  +  if( (p->flags & (MEM_Int|MEM_Real|MEM_IntReal))==0 ) return 1;
          145  +  vdbeMemRenderNum(sizeof(zBuf), zBuf, p);
   126    146     z = p->z;
   127    147     i = j = 0;
   128    148     incr = 1;
   129    149     if( p->enc!=SQLITE_UTF8 ){
   130    150       incr = 2;
   131    151       if( p->enc==SQLITE_UTF16BE ) z++;
   132    152     }
................................................................................
   230    250   /*
   231    251   ** Change the pMem->zMalloc allocation to be at least szNew bytes.
   232    252   ** If pMem->zMalloc already meets or exceeds the requested size, this
   233    253   ** routine is a no-op.
   234    254   **
   235    255   ** Any prior string or blob content in the pMem object may be discarded.
   236    256   ** The pMem->xDel destructor is called, if it exists.  Though MEM_Str
   237         -** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
   238         -** values are preserved.
          257  +** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, MEM_IntReal,
          258  +** and MEM_Null values are preserved.
   239    259   **
   240    260   ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
   241    261   ** if unable to complete the resizing.
   242    262   */
   243    263   int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
   244    264     assert( CORRUPT_DB || szNew>0 );
   245    265     assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
   246    266     if( pMem->szMalloc<szNew ){
   247    267       return sqlite3VdbeMemGrow(pMem, szNew, 0);
   248    268     }
   249    269     assert( (pMem->flags & MEM_Dyn)==0 );
   250    270     pMem->z = pMem->zMalloc;
   251         -  pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
          271  +  pMem->flags &= (MEM_Null|MEM_Int|MEM_Real|MEM_IntReal);
   252    272     return SQLITE_OK;
   253    273   }
   254    274   
   255    275   /*
   256    276   ** It is already known that pMem contains an unterminated string.
   257    277   ** Add the zero terminator.
          278  +**
          279  +** Three bytes of zero are added.  In this way, there is guaranteed
          280  +** to be a double-zero byte at an even byte boundary in order to
          281  +** terminate a UTF16 string, even if the initial size of the buffer
          282  +** is an odd number of bytes.
   258    283   */
   259    284   static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
   260         -  if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
          285  +  if( sqlite3VdbeMemGrow(pMem, pMem->n+3, 1) ){
   261    286       return SQLITE_NOMEM_BKPT;
   262    287     }
   263    288     pMem->z[pMem->n] = 0;
   264    289     pMem->z[pMem->n+1] = 0;
          290  +  pMem->z[pMem->n+2] = 0;
   265    291     pMem->flags |= MEM_Term;
   266    292     return SQLITE_OK;
   267    293   }
   268    294   
   269    295   /*
   270    296   ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
   271    297   ** MEM.zMalloc, where it can be safely written.
................................................................................
   331    357       return SQLITE_OK;   /* Nothing to do */
   332    358     }else{
   333    359       return vdbeMemAddTerminator(pMem);
   334    360     }
   335    361   }
   336    362   
   337    363   /*
   338         -** Add MEM_Str to the set of representations for the given Mem.  Numbers
   339         -** are converted using sqlite3_snprintf().  Converting a BLOB to a string
   340         -** is a no-op.
          364  +** Add MEM_Str to the set of representations for the given Mem.  This
          365  +** routine is only called if pMem is a number of some kind, not a NULL
          366  +** or a BLOB.
   341    367   **
   342         -** Existing representations MEM_Int and MEM_Real are invalidated if
   343         -** bForce is true but are retained if bForce is false.
          368  +** Existing representations MEM_Int, MEM_Real, or MEM_IntReal are invalidated
          369  +** if bForce is true but are retained if bForce is false.
   344    370   **
   345    371   ** A MEM_Null value will never be passed to this function. This function is
   346    372   ** used for converting values to text for returning to the user (i.e. via
   347    373   ** sqlite3_value_text()), or for ensuring that values to be used as btree
   348    374   ** keys are strings. In the former case a NULL pointer is returned the
   349    375   ** user and the latter is an internal programming error.
   350    376   */
   351    377   int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
   352         -  int fg = pMem->flags;
   353    378     const int nByte = 32;
   354    379   
   355    380     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   356         -  assert( !(fg&MEM_Zero) );
   357         -  assert( !(fg&(MEM_Str|MEM_Blob)) );
   358         -  assert( fg&(MEM_Int|MEM_Real) );
          381  +  assert( !(pMem->flags&MEM_Zero) );
          382  +  assert( !(pMem->flags&(MEM_Str|MEM_Blob)) );
          383  +  assert( pMem->flags&(MEM_Int|MEM_Real|MEM_IntReal) );
   359    384     assert( !sqlite3VdbeMemIsRowSet(pMem) );
   360    385     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   361    386   
   362    387   
   363    388     if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
   364    389       pMem->enc = 0;
   365    390       return SQLITE_NOMEM_BKPT;
   366    391     }
   367    392   
   368         -  /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
   369         -  ** string representation of the value. Then, if the required encoding
   370         -  ** is UTF-16le or UTF-16be do a translation.
   371         -  ** 
   372         -  ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
   373         -  */
   374         -  if( fg & MEM_Int ){
   375         -    sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
   376         -  }else{
   377         -    assert( fg & MEM_Real );
   378         -    sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
   379         -  }
          393  +  vdbeMemRenderNum(nByte, pMem->z, pMem);
   380    394     assert( pMem->z!=0 );
   381    395     pMem->n = sqlite3Strlen30NN(pMem->z);
   382    396     pMem->enc = SQLITE_UTF8;
   383    397     pMem->flags |= MEM_Str|MEM_Term;
   384         -  if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
          398  +  if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal);
   385    399     sqlite3VdbeChangeEncoding(pMem, enc);
   386    400     return SQLITE_OK;
   387    401   }
   388    402   
   389    403   /*
   390    404   ** Memory cell pMem contains the context of an aggregate function.
   391    405   ** This routine calls the finalize method for that function.  The
................................................................................
   551    565     return value;
   552    566   }
   553    567   i64 sqlite3VdbeIntValue(Mem *pMem){
   554    568     int flags;
   555    569     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   556    570     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   557    571     flags = pMem->flags;
   558         -  if( flags & MEM_Int ){
          572  +  if( flags & (MEM_Int|MEM_IntReal) ){
          573  +    testcase( flags & MEM_IntReal );
   559    574       return pMem->u.i;
   560    575     }else if( flags & MEM_Real ){
   561    576       return doubleToInt64(pMem->u.r);
   562    577     }else if( flags & (MEM_Str|MEM_Blob) ){
   563    578       assert( pMem->z || pMem->n==0 );
   564    579       return memIntValue(pMem);
   565    580     }else{
................................................................................
   580    595     return val;
   581    596   }
   582    597   double sqlite3VdbeRealValue(Mem *pMem){
   583    598     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   584    599     assert( EIGHT_BYTE_ALIGNMENT(pMem) );
   585    600     if( pMem->flags & MEM_Real ){
   586    601       return pMem->u.r;
   587         -  }else if( pMem->flags & MEM_Int ){
          602  +  }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
          603  +    testcase( pMem->flags & MEM_IntReal );
   588    604       return (double)pMem->u.i;
   589    605     }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
   590    606       return memRealValue(pMem);
   591    607     }else{
   592    608       /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
   593    609       return (double)0;
   594    610     }
................................................................................
   595    611   }
   596    612   
   597    613   /*
   598    614   ** Return 1 if pMem represents true, and return 0 if pMem represents false.
   599    615   ** Return the value ifNull if pMem is NULL.  
   600    616   */
   601    617   int sqlite3VdbeBooleanValue(Mem *pMem, int ifNull){
   602         -  if( pMem->flags & MEM_Int ) return pMem->u.i!=0;
          618  +  testcase( pMem->flags & MEM_IntReal );
          619  +  if( pMem->flags & (MEM_Int|MEM_IntReal) ) return pMem->u.i!=0;
   603    620     if( pMem->flags & MEM_Null ) return ifNull;
   604    621     return sqlite3VdbeRealValue(pMem)!=0.0;
   605    622   }
   606    623   
   607    624   /*
   608    625   ** The MEM structure is already a MEM_Real.  Try to also make it a
   609    626   ** MEM_Int if we can.
................................................................................
   668    685   */
   669    686   static int sqlite3RealSameAsInt(double r1, sqlite3_int64 i){
   670    687     double r2 = (double)i;
   671    688     return memcmp(&r1, &r2, sizeof(r1))==0;
   672    689   }
   673    690   
   674    691   /*
   675         -** Convert pMem so that it has types MEM_Real or MEM_Int or both.
          692  +** Convert pMem so that it has type MEM_Real or MEM_Int.
   676    693   ** Invalidate any prior representations.
   677    694   **
   678    695   ** Every effort is made to force the conversion, even if the input
   679    696   ** is a string that does not look completely like a number.  Convert
   680    697   ** as much of the string as we can and ignore the rest.
   681    698   */
   682    699   int sqlite3VdbeMemNumerify(Mem *pMem){
   683         -  if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
          700  +  testcase( pMem->flags & MEM_Int );
          701  +  testcase( pMem->flags & MEM_Real );
          702  +  testcase( pMem->flags & MEM_IntReal );
          703  +  testcase( pMem->flags & MEM_Null );
          704  +  if( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))==0 ){
   684    705       int rc;
   685    706       assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
   686    707       assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   687    708       rc = sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc);
   688    709       if( rc==0 ){
   689    710         MemSetTypeFlag(pMem, MEM_Int);
   690    711       }else{
................................................................................
   694    715           pMem->u.i = i;
   695    716           MemSetTypeFlag(pMem, MEM_Int);
   696    717         }else{
   697    718           MemSetTypeFlag(pMem, MEM_Real);
   698    719         }
   699    720       }
   700    721     }
   701         -  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
          722  +  assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Null))!=0 );
   702    723     pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
   703    724     return SQLITE_OK;
   704    725   }
   705    726   
   706    727   /*
   707    728   ** Cast the datatype of the value in pMem according to the affinity
   708    729   ** "aff".  Casting is different from applying affinity in that a cast
................................................................................
   737    758       }
   738    759       default: {
   739    760         assert( aff==SQLITE_AFF_TEXT );
   740    761         assert( MEM_Str==(MEM_Blob>>3) );
   741    762         pMem->flags |= (pMem->flags&MEM_Blob)>>3;
   742    763         sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
   743    764         assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
   744         -      pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
          765  +      pMem->flags &= ~(MEM_Int|MEM_Real|MEM_IntReal|MEM_Blob|MEM_Zero);
   745    766         break;
   746    767       }
   747    768     }
   748    769   }
   749    770   
   750    771   /*
   751    772   ** Initialize bulk memory to be a consistent Mem object.
................................................................................
   921    942         /* If pX is marked as a shallow copy of pMem, then verify that
   922    943         ** no significant changes have been made to pX since the OP_SCopy.
   923    944         ** A significant change would indicated a missed call to this
   924    945         ** function for pX.  Minor changes, such as adding or removing a
   925    946         ** dual type, are allowed, as long as the underlying value is the
   926    947         ** same. */
   927    948         u16 mFlags = pMem->flags & pX->flags & pX->mScopyFlags;
   928         -      assert( (mFlags&MEM_Int)==0 || pMem->u.i==pX->u.i );
          949  +      assert( (mFlags&(MEM_Int|MEM_IntReal))==0 || pMem->u.i==pX->u.i );
   929    950         assert( (mFlags&MEM_Real)==0 || pMem->u.r==pX->u.r );
   930    951         assert( (mFlags&MEM_Str)==0  || (pMem->n==pX->n && pMem->z==pX->z) );
   931    952         assert( (mFlags&MEM_Blob)==0  || sqlite3BlobCompare(pMem,pX)==0 );
   932    953         
   933    954         /* pMem is the register that is changing.  But also mark pX as
   934    955         ** undefined so that we can quickly detect the shallow-copy error */
   935    956         pX->flags = MEM_Undefined;
................................................................................
  1484   1505         sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
  1485   1506       }
  1486   1507       if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
  1487   1508         sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
  1488   1509       }else{
  1489   1510         sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
  1490   1511       }
  1491         -    if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
         1512  +    assert( (pVal->flags & MEM_IntReal)==0 );
         1513  +    if( pVal->flags & (MEM_Int|MEM_IntReal|MEM_Real) ){
         1514  +      testcase( pVal->flags & MEM_Int );
         1515  +      testcase( pVal->flags & MEM_Real );
         1516  +      pVal->flags &= ~MEM_Str;
         1517  +    }
  1492   1518       if( enc!=SQLITE_UTF8 ){
  1493   1519         rc = sqlite3VdbeChangeEncoding(pVal, enc);
  1494   1520       }
  1495   1521     }else if( op==TK_UMINUS ) {
  1496   1522       /* This branch happens for multiple negative signs.  Ex: -(-5) */
  1497   1523       if( SQLITE_OK==valueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal,pCtx) 
  1498   1524        && pVal!=0
................................................................................
  1507   1533           pVal->u.i = -pVal->u.i;
  1508   1534         }
  1509   1535         sqlite3ValueApplyAffinity(pVal, affinity, enc);
  1510   1536       }
  1511   1537     }else if( op==TK_NULL ){
  1512   1538       pVal = valueNew(db, pCtx);
  1513   1539       if( pVal==0 ) goto no_mem;
  1514         -    sqlite3VdbeMemNumerify(pVal);
         1540  +    sqlite3VdbeMemSetNull(pVal);
  1515   1541     }
  1516   1542   #ifndef SQLITE_OMIT_BLOB_LITERAL
  1517   1543     else if( op==TK_BLOB ){
  1518   1544       int nVal;
  1519   1545       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
  1520   1546       assert( pExpr->u.zToken[1]=='\'' );
  1521   1547       pVal = valueNew(db, pCtx);

Changes to src/vdbesort.c.

  1824   1824     }
  1825   1825   
  1826   1826     if( pSorter->list.aMemory ){
  1827   1827       int nMin = pSorter->iMemory + nReq;
  1828   1828   
  1829   1829       if( nMin>pSorter->nMemory ){
  1830   1830         u8 *aNew;
  1831         -      int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
  1832   1831         sqlite3_int64 nNew = 2 * (sqlite3_int64)pSorter->nMemory;
         1832  +      int iListOff = -1;
         1833  +      if( pSorter->list.pList ){
         1834  +        iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
         1835  +      }
  1833   1836         while( nNew < nMin ) nNew = nNew*2;
  1834   1837         if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
  1835   1838         if( nNew < nMin ) nNew = nMin;
  1836         -
  1837   1839         aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
  1838   1840         if( !aNew ) return SQLITE_NOMEM_BKPT;
  1839         -      pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
         1841  +      if( iListOff>=0 ){
         1842  +        pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
         1843  +      }
  1840   1844         pSorter->list.aMemory = aNew;
  1841   1845         pSorter->nMemory = nNew;
  1842   1846       }
  1843   1847   
  1844   1848       pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
  1845   1849       pSorter->iMemory += ROUND8(nReq);
  1846   1850       if( pSorter->list.pList ){

Changes to src/vdbetrace.c.

   126    126         }
   127    127         zRawSql += nToken;
   128    128         nextIndex = idx + 1;
   129    129         assert( idx>0 && idx<=p->nVar );
   130    130         pVar = &p->aVar[idx-1];
   131    131         if( pVar->flags & MEM_Null ){
   132    132           sqlite3_str_append(&out, "NULL", 4);
   133         -      }else if( pVar->flags & MEM_Int ){
          133  +      }else if( pVar->flags & (MEM_Int|MEM_IntReal) ){
   134    134           sqlite3_str_appendf(&out, "%lld", pVar->u.i);
   135    135         }else if( pVar->flags & MEM_Real ){
   136    136           sqlite3_str_appendf(&out, "%!.15g", pVar->u.r);
   137    137         }else if( pVar->flags & MEM_Str ){
   138    138           int nOut;  /* Number of bytes of the string text to include in output */
   139    139   #ifndef SQLITE_OMIT_UTF16
   140    140           u8 enc = ENC(db);

Changes to src/vtab.c.

   837    837         if( p->pVtab->nRef>0 ){
   838    838           return SQLITE_LOCKED;
   839    839         }
   840    840       }
   841    841       p = vtabDisconnectAll(db, pTab);
   842    842       xDestroy = p->pMod->pModule->xDestroy;
   843    843       assert( xDestroy!=0 );  /* Checked before the virtual table is created */
          844  +    pTab->nTabRef++;
   844    845       rc = xDestroy(p->pVtab);
   845    846       /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
   846    847       if( rc==SQLITE_OK ){
   847    848         assert( pTab->pVTable==p && p->pNext==0 );
   848    849         p->pVtab = 0;
   849    850         pTab->pVTable = 0;
   850    851         sqlite3VtabUnlock(p);
   851    852       }
          853  +    sqlite3DeleteTable(db, pTab);
   852    854     }
   853    855   
   854    856     return rc;
   855    857   }
   856    858   
   857    859   /*
   858    860   ** This function invokes either the xRollback or xCommit method

Changes to src/where.c.

   733    733       assert( !ExprHasProperty(pExpr, EP_FromJoin)    /* prereq always non-zero */
   734    734            || pExpr->iRightJoinTable!=pSrc->iCursor   /*   for the right-hand   */
   735    735            || pLoop->prereq!=0 );                     /*   table of a LEFT JOIN */
   736    736       if( pLoop->prereq==0
   737    737        && (pTerm->wtFlags & TERM_VIRTUAL)==0
   738    738        && !ExprHasProperty(pExpr, EP_FromJoin)
   739    739        && sqlite3ExprIsTableConstant(pExpr, pSrc->iCursor) ){
   740         -      pPartial = sqlite3ExprAnd(pParse->db, pPartial,
          740  +      pPartial = sqlite3ExprAnd(pParse, pPartial,
   741    741                                   sqlite3ExprDup(pParse->db, pExpr, 0));
   742    742       }
   743    743       if( termCanDriveIndex(pTerm, pSrc, notReady) ){
   744    744         int iCol = pTerm->u.leftColumn;
   745    745         Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
   746    746         testcase( iCol==BMS );
   747    747         testcase( iCol==BMS-1 );

Changes to src/whereInt.h.

    10     10   **
    11     11   *************************************************************************
    12     12   **
    13     13   ** This file contains structure and macro definitions for the query
    14     14   ** planner logic in "where.c".  These definitions are broken out into
    15     15   ** a separate source file for easier editing.
    16     16   */
           17  +#ifndef SQLITE_WHEREINT_H
           18  +#define SQLITE_WHEREINT_H
    17     19   
    18     20   /*
    19     21   ** Trace output macros
    20     22   */
    21     23   #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
    22     24   /***/ extern int sqlite3WhereTrace;
    23     25   #endif
................................................................................
   580    582   #define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
   581    583   #define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
   582    584   #define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
   583    585   #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
   584    586   #define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
   585    587   #define WHERE_PARTIALIDX   0x00020000  /* The automatic index is partial */
   586    588   #define WHERE_IN_EARLYOUT  0x00040000  /* Perhaps quit IN loops early */
          589  +
          590  +#endif /* !defined(SQLITE_WHEREINT_H) */

Changes to src/wherecode.c.

   995    995         sWalker.eCode = 0;
   996    996         sWalker.xExprCallback = codeCursorHintCheckExpr;
   997    997         sqlite3WalkExpr(&sWalker, pTerm->pExpr);
   998    998         if( sWalker.eCode ) continue;
   999    999       }
  1000   1000   
  1001   1001       /* If we survive all prior tests, that means this term is worth hinting */
  1002         -    pExpr = sqlite3ExprAnd(db, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
         1002  +    pExpr = sqlite3ExprAnd(pParse, pExpr, sqlite3ExprDup(db, pTerm->pExpr, 0));
  1003   1003     }
  1004   1004     if( pExpr!=0 ){
  1005   1005       sWalker.xExprCallback = codeCursorHintFixExpr;
  1006   1006       sqlite3WalkExpr(&sWalker, pExpr);
  1007   1007       sqlite3VdbeAddOp4(v, OP_CursorHint, 
  1008   1008                         (sHint.pIdx ? sHint.iIdxCur : sHint.iTabCur), 0, 0,
  1009   1009                         (const char*)pExpr, P4_EXPR);
................................................................................
  1960   1960           if( &pWC->a[iTerm] == pTerm ) continue;
  1961   1961           testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
  1962   1962           testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
  1963   1963           if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
  1964   1964           if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
  1965   1965           testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
  1966   1966           pExpr = sqlite3ExprDup(db, pExpr, 0);
  1967         -        pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
         1967  +        pAndExpr = sqlite3ExprAnd(pParse, pAndExpr, pExpr);
  1968   1968         }
  1969   1969         if( pAndExpr ){
  1970   1970           /* The extra 0x10000 bit on the opcode is masked off and does not
  1971   1971           ** become part of the new Expr.op.  However, it does make the
  1972   1972           ** op==TK_AND comparison inside of sqlite3PExpr() false, and this
  1973   1973           ** prevents sqlite3PExpr() from implementing AND short-circuit 
  1974   1974           ** optimization, which we do not want here. */
................................................................................
  2111   2111         pAndExpr->pLeft = 0;
  2112   2112         sqlite3ExprDelete(db, pAndExpr);
  2113   2113       }
  2114   2114       sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
  2115   2115       sqlite3VdbeGoto(v, pLevel->addrBrk);
  2116   2116       sqlite3VdbeResolveLabel(v, iLoopBody);
  2117   2117   
  2118         -    if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
         2118  +    if( pWInfo->nLevel>1 ){ sqlite3StackFree(db, pOrTab); }
  2119   2119       if( !untestedTerms ) disableTerm(pLevel, pTerm);
  2120   2120     }else
  2121   2121   #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
  2122   2122   
  2123   2123     {
  2124   2124       /* Case 6:  There is no usable index.  We must do a complete
  2125   2125       **          scan of the entire table.

Changes to src/whereexpr.c.

   258    258           char *zNew = pPrefix->u.zToken;
   259    259           zNew[cnt] = 0;
   260    260           for(iFrom=iTo=0; iFrom<cnt; iFrom++){
   261    261             if( zNew[iFrom]==wc[3] ) iFrom++;
   262    262             zNew[iTo++] = zNew[iFrom];
   263    263           }
   264    264           zNew[iTo] = 0;
          265  +        assert( iTo>0 );
   265    266   
   266         -        /* If the RHS begins with a digit or a minus sign, then the LHS must be
          267  +        /* If the RHS begins with a digit or a +/- sign, then the LHS must be
   267    268           ** an ordinary column (not a virtual table column) with TEXT affinity.
   268    269           ** Otherwise the LHS might be numeric and "lhs >= rhs" would be false
   269    270           ** even though "lhs LIKE rhs" is true.  But if the RHS does not start
   270         -        ** with a digit or '-', then "lhs LIKE rhs" will always be false if
          271  +        ** with a digit or +/-, then "lhs LIKE rhs" will always be false if
   271    272           ** the LHS is numeric and so the optimization still works.
   272    273           **
   273    274           ** 2018-09-10 ticket c94369cae9b561b1f996d0054bfab11389f9d033
   274    275           ** The RHS pattern must not be '/%' because the termination condition
   275    276           ** will then become "x<'0'" and if the affinity is numeric, will then
   276    277           ** be converted into "x<0", which is incorrect.
   277    278           */
   278    279           if( sqlite3Isdigit(zNew[0])
   279    280            || zNew[0]=='-'
   280         -         || (zNew[0]+1=='0' && iTo==1)
          281  +         || zNew[0]=='+'
          282  +         || zNew[iTo-1]=='0'-1
   281    283           ){
   282    284             if( pLeft->op!=TK_COLUMN 
   283    285              || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT 
   284    286              || IsVirtual(pLeft->y.pTab)  /* Value might be numeric */
   285    287             ){
   286    288               sqlite3ExprDelete(db, pPrefix);
   287    289               sqlite3ValueFree(pVal);

Changes to src/window.c.

   864    864   /*
   865    865   ** Append a copy of each expression in expression-list pAppend to
   866    866   ** expression list pList. Return a pointer to the result list.
   867    867   */
   868    868   static ExprList *exprListAppendList(
   869    869     Parse *pParse,          /* Parsing context */
   870    870     ExprList *pList,        /* List to which to append. Might be NULL */
   871         -  ExprList *pAppend       /* List of values to append. Might be NULL */
          871  +  ExprList *pAppend,      /* List of values to append. Might be NULL */
          872  +  int bIntToNull
   872    873   ){
   873    874     if( pAppend ){
   874    875       int i;
   875    876       int nInit = pList ? pList->nExpr : 0;
   876    877       for(i=0; i<pAppend->nExpr; i++){
   877    878         Expr *pDup = sqlite3ExprDup(pParse->db, pAppend->a[i].pExpr, 0);
          879  +      if( bIntToNull && pDup && pDup->op==TK_INTEGER ){
          880  +        pDup->op = TK_NULL;
          881  +        pDup->flags &= ~(EP_IntValue|EP_IsTrue|EP_IsFalse);
          882  +      }
   878    883         pList = sqlite3ExprListAppend(pParse, pList, pDup);
   879    884         if( pList ) pList->a[nInit+i].sortOrder = pAppend->a[i].sortOrder;
   880    885       }
   881    886     }
   882    887     return pList;
   883    888   }
   884    889   
................................................................................
   910    915       p->pGroupBy = 0;
   911    916       p->pHaving = 0;
   912    917   
   913    918       /* Create the ORDER BY clause for the sub-select. This is the concatenation
   914    919       ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
   915    920       ** redundant, remove the ORDER BY from the parent SELECT.  */
   916    921       pSort = sqlite3ExprListDup(db, pMWin->pPartition, 0);
   917         -    pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy);
          922  +    pSort = exprListAppendList(pParse, pSort, pMWin->pOrderBy, 1);
   918    923       if( pSort && p->pOrderBy ){
   919    924         if( sqlite3ExprListCompare(pSort, p->pOrderBy, -1)==0 ){
   920    925           sqlite3ExprListDelete(db, p->pOrderBy);
   921    926           p->pOrderBy = 0;
   922    927         }
   923    928       }
   924    929   
................................................................................
   931    936       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pEList, &pSublist);
   932    937       selectWindowRewriteEList(pParse, pMWin, pSrc, p->pOrderBy, &pSublist);
   933    938       pMWin->nBufferCol = (pSublist ? pSublist->nExpr : 0);
   934    939   
   935    940       /* Append the PARTITION BY and ORDER BY expressions to the to the 
   936    941       ** sub-select expression list. They are required to figure out where 
   937    942       ** boundaries for partitions and sets of peer rows lie.  */
   938         -    pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition);
   939         -    pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy);
          943  +    pSublist = exprListAppendList(pParse, pSublist, pMWin->pPartition, 0);
          944  +    pSublist = exprListAppendList(pParse, pSublist, pMWin->pOrderBy, 0);
   940    945   
   941    946       /* Append the arguments passed to each window function to the
   942    947       ** sub-select expression list. Also allocate two registers for each
   943    948       ** window function - one for the accumulator, another for interim
   944    949       ** results.  */
   945    950       for(pWin=pMWin; pWin; pWin=pWin->pNextWin){
   946    951         pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
   947         -      pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList);
          952  +      pSublist = exprListAppendList(pParse, pSublist, pWin->pOwner->x.pList, 0);
   948    953         if( pWin->pFilter ){
   949    954           Expr *pFilter = sqlite3ExprDup(db, pWin->pFilter, 0);
   950    955           pSublist = sqlite3ExprListAppend(pParse, pSublist, pFilter);
   951    956         }
   952    957         pWin->regAccum = ++pParse->nMem;
   953    958         pWin->regResult = ++pParse->nMem;
   954    959         sqlite3VdbeAddOp2(v, OP_Null, 0, pWin->regAccum);

Changes to test/altertab.test.

   555    555     }
   556    556   
   557    557     do_execsql_test 16.4 {
   558    558       SELECT * FROM z1_segments;
   559    559     }
   560    560   }
   561    561   
          562  +#-------------------------------------------------------------------------
          563  +reset_db
          564  +do_execsql_test 17.0 {
          565  +  CREATE TABLE sqlite1234 (id integer);
          566  +  ALTER TABLE sqlite1234 RENAME TO User;
          567  +  SELECT name, sql FROM sqlite_master WHERE sql IS NOT NULL;
          568  +} {
          569  +  User {CREATE TABLE "User" (id integer)}
          570  +}
          571  +
          572  +#-------------------------------------------------------------------------
          573  +reset_db
          574  +do_execsql_test 18.1.0 {
          575  +  CREATE TABLE t0 (c0 INTEGER, PRIMARY KEY(c0)) WITHOUT ROWID;
          576  +}
          577  +breakpoint
          578  +do_execsql_test 18.1.1 {
          579  +  ALTER TABLE t0 RENAME COLUMN c0 TO c1;
          580  +}
          581  +do_execsql_test 18.1.2 {
          582  +  SELECT sql FROM sqlite_master;
          583  +} {{CREATE TABLE t0 (c1 INTEGER, PRIMARY KEY(c1)) WITHOUT ROWID}}
          584  +
          585  +reset_db
          586  +do_execsql_test 18.2.0 {
          587  +  CREATE TABLE t0 (c0 INTEGER, PRIMARY KEY(c0));
          588  +}
          589  +do_execsql_test 18.2.1 {
          590  +  ALTER TABLE t0 RENAME COLUMN c0 TO c1;
          591  +}
          592  +do_execsql_test 18.2.2 {
          593  +  SELECT sql FROM sqlite_master;
          594  +} {{CREATE TABLE t0 (c1 INTEGER, PRIMARY KEY(c1))}}
          595  +
   562    596   finish_test
          597  +

Changes to test/altertab3.test.

   137    137       FOREIGN KEY (Col0) REFERENCES Table0
   138    138     );
   139    139   }
   140    140   
   141    141   do_execsql_test 6.1 {
   142    142     ALTER TABLE Table0 RENAME Col0 TO Col0;
   143    143   }
          144  +
          145  +#-------------------------------------------------------------------------
          146  +reset_db
          147  +do_execsql_test 7.1.0 {
          148  +  CREATE TABLE t1(a,b,c);
          149  +  CREATE TRIGGER AFTER INSERT ON t1 BEGIN
          150  +    SELECT a, rank() OVER w1 FROM t1
          151  +    WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1);
          152  +  END;
          153  +}
          154  +
          155  +do_execsql_test 7.1.2 {
          156  +  ALTER TABLE t1 RENAME TO t1x;
          157  +  SELECT sql FROM sqlite_master;
          158  +} {
          159  +  {CREATE TABLE "t1x"(a,b,c)}
          160  +  {CREATE TRIGGER AFTER INSERT ON "t1x" BEGIN
          161  +    SELECT a, rank() OVER w1 FROM "t1x"
          162  +    WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1);
          163  +  END}
          164  +}
          165  +
          166  +do_execsql_test 7.2.1 {
          167  +  DROP TRIGGER after;
          168  +  CREATE TRIGGER AFTER INSERT ON t1x BEGIN
          169  +    SELECT a, rank() OVER w1 FROM t1x
          170  +    WINDOW w1 AS (PARTITION BY b, percent_rank() OVER w1 ORDER BY d);
          171  +  END;
          172  +}
          173  +
          174  +do_catchsql_test 7.2.2 {
          175  +  ALTER TABLE t1x RENAME TO t1;
          176  +} {1 {error in trigger AFTER: no such column: d}}
   144    177   
   145    178   finish_test
   146    179   
   147    180   

Changes to test/corruptL.test.

   833    833   | end a.db
   834    834   }]} {}
   835    835   
   836    836   
   837    837   do_catchsql_test 8.1 {
   838    838     INSERT INTO t3 SELECT * FROM t2;
   839    839   } {1 {database disk image is malformed}}
          840  +
          841  +#-------------------------------------------------------------------------
          842  +reset_db
          843  +do_test 9.0 {
          844  +  sqlite3 db {}
          845  +  db deserialize [decode_hexdb {
          846  +| size 8192 pagesize 4096 filename crash-ab10597e4e1c32.db
          847  +| page 1 offset 0
          848  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
          849  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
          850  +|     96: 00 00 00 00 0d 00 00 00 01 0f d6 00 0f d6 00 00   ................
          851  +|   4048: 00 00 00 00 00 00 28 01 06 17 11 11 01 3d 74 61   ......(......=ta
          852  +|   4064: 62 6c 65 74 31 74 31 02 43 52 45 41 54 45 20 54   blet1t1.CREATE T
          853  +|   4080: 41 42 4c 45 20 74 31 28 61 2c 62 2c 63 2c 64 29   ABLE t1(a,b,c,d)
          854  +| page 2 offset 4096
          855  +|      0: 0d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
          856  +| end crash-ab10597e4e1c32.db
          857  +}]} {}
          858  +
          859  +do_execsql_test 9.1 {
          860  +  SAVEPOINT one;
          861  +}
          862  +do_catchsql_test 9.3 {
          863  +  INSERT INTO t1(b,c) VALUES(5,6);
          864  +} {1 {database disk image is malformed}}
          865  +do_execsql_test 9.3 {
          866  +  ROLLBACK TO one;
          867  +}
          868  +
          869  +#-------------------------------------------------------------------------
          870  +reset_db
          871  +do_test 10.0 {
          872  +  sqlite3 db {}
          873  +  db deserialize [decode_hexdb {
          874  +| size 180224 pagesize 4096 filename crash-41390d95d613b6.db
          875  +| page 1 offset 0
          876  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
          877  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
          878  +|     96: 00 00 00 00 0d 00 00 00 04 0e e2 00 0f 96 0f 44   ...............D
          879  +|    112: 0f 10 0e e2 00 00 00 00 00 00 00 00 00 00 00 00   ................
          880  +|   3808: 00 00 2c 14 06 17 15 11 01 41 69 6e 64 65 78 74   ..,......Aindext
          881  +|   3824: 41 78 33 74 31 06 43 52 45 41 54 45 20 49 4e 44   Ax3t1.CREATE IND
          882  +|   3840: 45 58 20 74 31 78 32 20 4f 4e 20 74 31 28 62 29   EX t1x2 ON t1(b)
          883  +|   3856: 32 03 06 17 15 11 01 4d 69 6e 64 65 78 74 31 88   2......Mindext1.
          884  +|   3872: 31 74 31 05 43 52 45 41 54 45 20 49 4e 44 45 58   1t1.CREATE INDEX
          885  +|   3888: 20 74 31 78 31 20 4f 4e 20 74 31 28 67 2b 68 2c    t1x1 ON t1(g+h,
          886  +|   3904: 6a 2d 6b 29 50 02 06 17 2b 2b 01 59 74 61 62 6c   j-k)P...++.Ytabl
          887  +|   3920: 65 73 71 6c 69 74 65 5e 73 65 71 74 65 6e 63 65   esqlite^seqtence
          888  +|   3936: 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 04   sqlite_sequence.
          889  +|   3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 71 6c   CREATE TABLE sql
          890  +|   3968: 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d   ite_sequence(nam
          891  +|   3984: 65 2c 73 65 71 29 68 00 07 17 11 11 01 81 3b 74   e,seq)h.......;t
          892  +|   4000: 61 62 6c 65 74 31 74 31 03 43 52 45 41 54 45 20   ablet1t1.CREATE 
          893  +|   4016: 54 41 42 4c 45 20 74 31 28 61 20 49 4e 54 45 47   TABLE t1(a INTEG
          894  +|   4032: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 41   ER PRIMARY KEY A
          895  +|   4048: 55 54 4f 49 4e 43 52 45 4d 45 4e 54 2c 0a 62 2c   UTOINCREMENT,.b,
          896  +|   4064: 63 2c 64 2c 65 2c 66 2c 67 2c 68 2c 6a 2c 6b 2c   c,d,e,f,g,h,j,k,
          897  +|   4080: 6c 2c 6d 2c 6e 2c 6f 2c 70 2c 71 2c 72 2c 73 29   l,m,n,o,p,q,r,s)
          898  +| page 2 offset 4096
          899  +|      0: 01 00 00 00 00 01 00 00 10 00 01 00 00 00 00 01   ................
          900  +|     16: 00 00 00 00 02 00 0f f0 00 15 00 00 00 03 02 00   ................
          901  +|     32: 00 00 d9 05 00 00 00 03 02 00 00 00 00 05 00 00   ................
          902  +|     48: 10 03 02 00 00 00 00 05 00 00 00 03 02 00 00 00   ................
          903  +|     64: 00 05 00 00 00 02 62 00 00 00 00 05 00 00 00 03   ......b.........
          904  +|     80: 02 00 00 00 00 05 00 00 00 03 02 00 00 00 00 05   ................
          905  +|     96: 00 00 00 03 02 00 00 00 00 05 00 00 00 03 05 00   ................
          906  +|    112: 00 00 03 03 01 00 00 23 02 00 00 4f 00 02 00 00   .......#...O....
          907  +|    128: 10 25 02 00 00 00 00 03 00 00 00 23 02 00 00 00   .%.........#....
          908  +|    144: 00 03 00 00 00 23 02 00 00 00 00 03 00 00 00 23   .....#.........#
          909  +|    160: 05 00 08 90 06 05 00 00 00 06 01 ff 00 00 00 03   ................
          910  +|    176: 00 00 00 06 02 00 00 00 00 02 ff 00 00 00 00 00   ................
          911  +| page 3 offset 8192
          912  +|      0: 05 00 00 00 09 0f d0 00 00 00 00 19 0f fb 0f f6   ................
          913  +|     16: 0f f1 10 ec ec e7 0f e2 0f dc 0f d6 0f 00 00 00   ................
          914  +|   1072: 00 97 4c 0a 24 00 ae 00 00 00 00 00 00 00 00 00   ..L.$...........
          915  +|   4048: 00 00 00 16 83 39 ff ff ff 14 81 16 00 00 00 12   .....9..........
          916  +|   4064: 81 02 00 00 00 10 6e 00 00 00 0e 5a 00 00 00 0c   ......n....Z....
          917  +|   4080: 46 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   F...............
          918  +| page 4 offset 12288
          919  +|   1072: 97 4d 32 14 00 ae 00 00 00 00 00 00 00 00 00 00   .M2.............
          920  +|   4080: 00 00 00 00 00 00 00 07 01 03 11 02 74 31 00 bd   ............t1..
          921  +| page 5 offset 16384
          922  +|      0: fa 0f 7c 00 0a 0f 74 00 0f f9 0f eb 0f dd 0f cf   ..|...t.........
          923  +|     16: 0f c1 0f b3 0f a4 0e 94 0f 84 0f 74 0f 74 0f 74   ...........t.t.t
          924  +|     32: 0f 74 0f 64 0f 00 00 00 00 00 00 00 00 00 00 00   .t.d............
          925  +|   3952: 00 00 00 00 07 05 00 00 00 02 00 be 0f 8c 10 07   ................
          926  +|   3968: ff ff 00 00 07 05 00 00 00 02 00 aa 0f 9b f0 08   ................
          927  +|   3984: c8 00 00 00 37 06 00 00 00 01 00 96 0f ac 00 08   ....7...........
          928  +|   4000: 00 00 00 b3 07 15 00 10 00 02 00 82 0f ba 00 07   ................
          929  +|   4016: 00 00 00 06 05 00 00 00 01 6e 0f c8 00 07 00 00   .........n......
          930  +|   4032: 00 06 05 00 00 00 01 5a 03 f6 00 07 00 00 00 06   .......Z........
          931  +|   4048: 05 00 00 00 01 46 0f e4 00 07 00 00 10 06 05 00   .....F..........
          932  +|   4064: 00 00 01 32 10 02 00 07 00 00 00 07 05 00 00 00   ...2............
          933  +|   4080: 01 1d ff ff ff 07 10 00 00 06 05 00 00 00 01 0a   ................
          934  +| page 6 offset 20480
          935  +|    624: 00 00 00 00 00 21 97 00 00 00 00 00 00 00 00 00   .....!..........
          936  +|   1120: 00 00 00 00 00 24 57 3e 00 00 00 00 00 00 00 00   .....$W>........
          937  +|   1616: 00 00 00 00 1f 97 00 00 00 00 00 00 00 00 00 00   ................
          938  +|   2112: 00 00 00 1e 97 3d 00 00 00 00 00 00 00 00 00 00   .....=..........
          939  +|   2608: 00 1d 97 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
          940  +| page 8 offset 28672
          941  +|   1184: 00 00 00 00 00 00 00 00 00 97 4d 1e 13 ff ae 7c   ..........M....|
          942  +|   4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 90   ................
          943  +| page 9 offset 32768
          944  +|    256: 0d 01 c0 00 01 04 30 00 04 30 00 00 00 00 00 00   ......0..0......
          945  +| page 10 offset 36864
          946  +|      0: 0d 00 22 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
          947  +|   4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05   ................
          948  +| page 12 offset 45056
          949  +|      0: 0d 00 00 00 01 04 30 00 00 00 00 00 00 00 00 00   ......0.........
          950  +| page 14 offset 53248
          951  +|      0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00   ......0..0......
          952  +|   1072: 96 4d 5a 14 00 00 00 00 00 00 00 00 00 00 00 00   .MZ.............
          953  +| page 16 offset 61440
          954  +|      0: 0d 00 00 00 01 04 30 00 04 30 00 00 00 00 00 00   ......0..0......
          955  +|   1072: 97 4d 6e 14 00 ae 7b ff ff ff ff 00 00 00 00 00   .Mn.............
          956  +| page 18 offset 69632
          957  +|   1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97   ................
          958  +|   1072: 4d 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   M...............
          959  +|   4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0d   ................
          960  +| page 20 offset 77824
          961  +|   1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97   ................
          962  +|   1072: 4d 81 16 14 00 ae 00 00 00 00 00 00 00 00 00 00   M...............
          963  +|   4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0f   ................
          964  +| page 22 offset 86016
          965  +|      0: 0d 00 00 00 01 04 2f 00 04 2f 01 00 00 00 00 00   ....../../......
          966  +|   1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97   ................
          967  +|   1072: 4d 81 2a 14 00 00 00 00 00 00 00 00 00 00 00 00   M.*.............
          968  +| page 24 offset 94208
          969  +|   1072: 00 97 4c 0a 14 00 ae 7c 00 00 00 00 00 00 00 00   ..L....|........
          970  +| page 25 offset 98304
          971  +|   1056: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97   ................
          972  +|   1072: 4d 81 3e 14 00 ae 7c 00 00 18 ff 00 00 00 00 00   M.>...|.........
          973  +| page 27 offset 106496
          974  +|      0: 00 00 00 00 00 00 00 12 00 00 00 07 00 00 00 1d   ................
          975  +|     16: 00 00 00 09 00 00 00 1f 00 00 00 0b 00 00 00 21   ...............!
          976  +|     32: 00 00 00 0d 00 10 00 25 00 00 00 0f 00 00 00 27   .......%.......'
          977  +|     48: 00 00 00 11 00 00 00 00 00 00 00 00 00 00 00 00   ................
          978  +| page 32 offset 126976
          979  +|   2512: 00 00 00 00 00 00 00 45 21 00 00 00 00 00 00 00   .......E!.......
          980  +| page 35 offset 139264
          981  +|      0: 00 0a 08 44 00 05 02 77 00 0e 11 0a 92 00 00 00   ...D...w........
          982  +|   1120: 00 00 00 00 00 20 97 00 00 00 00 00 00 00 00 00   ..... ..........
          983  +|   1616: 00 00 00 00 22 00 00 00 00 00 00 00 00 00 00 00   ................
          984  +|   2608: 00 00 00 97 3d 04 00 00 00 00 00 00 00 00 00 00   ....=...........
          985  +|   3104: 00 1c 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
          986  +|   3600: 00 97 3d 04 ae 7c 00 00 00 00 00 00 00 00 00 00   ..=..|..........
          987  +|   4080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1a   ................
          988  +| page 36 offset 143360
          989  +|      0: 0a 08 44 00 04 02 00 00 00 00 00 00 00 00 00 00   ..D.............
          990  +|   1120: 00 00 00 00 00 2a 97 3e 04 00 00 00 00 00 00 00   .....*.>........
          991  +|   1616: 00 00 00 00 2c 97 3e 00 00 00 00 00 00 00 00 00   ....,.>.........
          992  +|   2112: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 38   ...............8
          993  +|   2128: 00 00 05 cd 00 00 00 00 00 00 00 00 00 00 00 00   ................
          994  +|   3600: 00 97 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
          995  +| page 38 offset 151552
          996  +|   2464: 00 00 00 00 00 00 00 00 00 6e 00 00 00 00 00 00   .........n......
          997  +| page 40 offset 159744
          998  +|   2512: 00 00 00 00 00 00 00 00 82 00 00 00 00 00 00 00   ................
          999  +| page 42 offset 167936
         1000  +|   2512: 00 00 00 00 00 00 00 96 00 00 00 00 00 00 00 00   ................
         1001  +| page 44 offset 176128
         1002  +|   2512: 00 00 00 00 00 00 00 00 aa 00 00 00 00 00 00 00   ................
         1003  +| end crash-41390d95d613b6.db
         1004  +}]} {}
         1005  +
         1006  +do_catchsql_test 10.1 {
         1007  +  SELECT * FROM t1 WHERE a<='2019-05-09' ORDER BY a DESC;
         1008  +} {1 {database disk image is malformed}}
         1009  +
   840   1010   
   841   1011   finish_test

Added test/dbdata.test.

            1  +# 2019-04-11
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this file is testing the sqlite_dbpage virtual table.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set testprefix dbdata
           18  +
           19  +ifcapable !vtab||!compound {
           20  +  finish_test
           21  +  return
           22  +}
           23  +if { [catch { db enable_load_extension 1 }]
           24  +  || [catch { db eval { SELECT load_extension('../dbdata') } }] 
           25  +} {
           26  +  finish_test
           27  +  return
           28  +}
           29  +
           30  +do_execsql_test 1.0 {
           31  +  CREATE TABLE T1(a, b);
           32  +  INSERT INTO t1(rowid, a ,b) VALUES(5, 'v', 'five');
           33  +  INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
           34  +}
           35  +
           36  +do_execsql_test 1.1 {
           37  +  SELECT pgno, cell, field, quote(value) FROM sqlite_dbdata WHERE pgno=2;
           38  +} {
           39  +  2 0 -1 5 
           40  +  2 0  0 'v' 
           41  +  2 0  1 'five' 
           42  +  2 1 -1 10 
           43  +  2 1  0 'x' 
           44  +  2 1  1 'ten'
           45  +}
           46  +
           47  +breakpoint
           48  +do_execsql_test 1.2 {
           49  +  SELECT pgno, cell, field, quote(value) FROM sqlite_dbdata;
           50  +} {
           51  +  1 0 -1 1 
           52  +  1 0 0 'table' 
           53  +  1 0 1 'T1' 
           54  +  1 0 2 'T1' 
           55  +  1 0 3 2 
           56  +  1 0 4 {'CREATE TABLE T1(a, b)'}
           57  +  2 0 -1 5 
           58  +  2 0  0 'v' 
           59  +  2 0  1 'five' 
           60  +  2 1 -1 10 
           61  +  2 1  0 'x' 
           62  +  2 1  1 'ten'
           63  +}
           64  +
           65  +set big [string repeat big 2000]
           66  +do_execsql_test 1.3 {
           67  +  INSERT INTO t1 VALUES(NULL, $big);
           68  +  SELECT value FROM sqlite_dbdata WHERE pgno=2 AND cell=2 AND field=1;
           69  +} $big
           70  +
           71  +do_execsql_test 1.4 {
           72  +  DELETE FROM t1;
           73  +  INSERT INTO t1 VALUES(NULL, randomblob(5050));
           74  +}
           75  +do_test 1.5 {
           76  +  execsql {
           77  +    SELECT quote(value) FROM sqlite_dbdata WHERE pgno=2 AND cell=0 AND field=1;
           78  +  }
           79  +} [db one {SELECT quote(b) FROM t1}]
           80  +
           81  +#-------------------------------------------------------------------------
           82  +reset_db
           83  +db enable_load_extension 1
           84  +db eval { SELECT load_extension('../dbdata') }
           85  +
           86  +do_execsql_test 2.0 {
           87  +  CREATE TABLE t1(a);
           88  +  CREATE INDEX i1 ON t1(a);
           89  +  WITH s(i) AS (
           90  +    SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10
           91  +  )
           92  +  INSERT INTO t1 SELECT randomblob(900) FROM s;
           93  +}
           94  +
           95  +do_execsql_test 2.1 {
           96  +  SELECT * FROM sqlite_dbptr WHERE pgno=2;
           97  +} {
           98  +  2 25   2 6   2 7   2 9   2 11   2 13   2 15   2 17   2 19   2 21
           99  +}
          100  +
          101  +do_execsql_test 2.2 {
          102  +  SELECT * FROM sqlite_dbptr WHERE pgno=3;
          103  +} {
          104  +  3 24   3 23
          105  +}
          106  +
          107  +do_execsql_test 2.3 {
          108  +  SELECT * FROM sqlite_dbptr
          109  +} {
          110  +  2 25   2 6   2 7   2 9   2 11   2 13   2 15   2 17   2 19   2 21
          111  +  3 24   3 23
          112  +}
          113  +
          114  +
          115  +finish_test

Changes to test/e_expr.test.

  1009   1009   do_test         e_expr-15.1.4 { set likeargs } {def abc X}
  1010   1010   db close
  1011   1011   sqlite3 db test.db
  1012   1012   
  1013   1013   # EVIDENCE-OF: R-22868-25880 The LIKE operator can be made case
  1014   1014   # sensitive using the case_sensitive_like pragma.
  1015   1015   #
  1016         -do_execsql_test e_expr-16.1.1 { SELECT 'abcxyz' LIKE 'ABC%' } 1
  1017         -do_execsql_test e_expr-16.1.2 { PRAGMA case_sensitive_like = 1 } {}
  1018         -do_execsql_test e_expr-16.1.3 { SELECT 'abcxyz' LIKE 'ABC%' } 0
  1019         -do_execsql_test e_expr-16.1.4 { SELECT 'ABCxyz' LIKE 'ABC%' } 1
  1020         -do_execsql_test e_expr-16.1.5 { PRAGMA case_sensitive_like = 0 } {}
  1021         -do_execsql_test e_expr-16.1.6 { SELECT 'abcxyz' LIKE 'ABC%' } 1
  1022         -do_execsql_test e_expr-16.1.7 { SELECT 'ABCxyz' LIKE 'ABC%' } 1
         1016  +do_execsql_test e_expr-16.1.1  { SELECT 'abcxyz' LIKE 'ABC%' } 1
         1017  +do_execsql_test e_expr-16.1.1b { SELECT 'abc%xyz' LIKE 'ABC\%x%' ESCAPE '\' } 1
         1018  +do_execsql_test e_expr-16.1.2  { PRAGMA case_sensitive_like = 1 } {}
         1019  +do_execsql_test e_expr-16.1.3  { SELECT 'abcxyz' LIKE 'ABC%' } 0
         1020  +do_execsql_test e_expr-16.1.3b { SELECT 'abc%xyz' LIKE 'ABC\%X%' ESCAPE '\' } 0
         1021  +do_execsql_test e_expr-16.1.4  { SELECT 'ABCxyz' LIKE 'ABC%' } 1
         1022  +do_execsql_test e_expr-16.1.4b { SELECT 'ABC%xyz' LIKE 'ABC\%x%' ESCAPE '\' } 1
         1023  +do_execsql_test e_expr-16.1.5  { PRAGMA case_sensitive_like = 0 } {}
         1024  +do_execsql_test e_expr-16.1.6  { SELECT 'abcxyz' LIKE 'ABC%' } 1
         1025  +do_execsql_test e_expr-16.1.6b { SELECT 'abc%xyz' LIKE 'ABC\%X%' ESCAPE '\' } 1
         1026  +do_execsql_test e_expr-16.1.7  { SELECT 'ABCxyz' LIKE 'ABC%' } 1
         1027  +do_execsql_test e_expr-16.1.7b { SELECT 'ABC%xyz' LIKE 'ABC\%X%' ESCAPE '\' } 1
  1023   1028   
  1024   1029   # EVIDENCE-OF: R-52087-12043 The GLOB operator is similar to LIKE but
  1025   1030   # uses the Unix file globbing syntax for its wildcards.
  1026   1031   #
  1027   1032   # EVIDENCE-OF: R-09813-17279 Also, GLOB is case sensitive, unlike LIKE.
  1028   1033   #
  1029   1034   do_execsql_test e_expr-17.1.1 { SELECT 'abcxyz' GLOB 'abc%' } 0

Changes to test/fkey7.test.

    78     78       INSERT INTO c4 VALUES(1), (2), (3);
    79     79   
    80     80       ANALYZE;
    81     81       INSERT INTO p4(id) VALUES(4);
    82     82     }
    83     83   }
    84     84   
           85  +
           86  +do_execsql_test 4.0 {
           87  +  PRAGMA foreign_keys = true;
           88  +  CREATE TABLE parent(
           89  +    p PRIMARY KEY
           90  +  );
           91  +  CREATE TABLE child(
           92  +    c UNIQUE REFERENCES parent(p)
           93  +  );
           94  +}
           95  +
           96  +do_catchsql_test 4.1 {
           97  +  INSERT OR FAIL INTO child VALUES(123), (123);
           98  +} {1 {FOREIGN KEY constraint failed}}
           99  +
          100  +do_execsql_test 4.2 {
          101  +  SELECT * FROM child;
          102  +} {}
          103  +
          104  +do_execsql_test 4.3 {
          105  +  PRAGMA foreign_key_check;
          106  +} {}
          107  +
          108  +do_catchsql_test 4.4 {
          109  +  INSERT INTO parent VALUES(123);
          110  +  INSERT OR FAIL INTO child VALUES(123), (123);
          111  +} {1 {UNIQUE constraint failed: child.c}}
          112  +
          113  +do_execsql_test 4.5 {
          114  +  SELECT * FROM child;
          115  +} {123}
          116  +
          117  +do_execsql_test 4.6 {
          118  +  PRAGMA foreign_key_check;
          119  +} {}
          120  +
    85    121   finish_test

Changes to test/fkey8.test.

   192    192   }
   193    193   do_catchsql_test 4.1 {
   194    194     INSERT OR REPLACE INTO t1 VALUES(10000, 20000);
   195    195   } {1 {FOREIGN KEY constraint failed}}
   196    196   do_execsql_test 4.2 {
   197    197     INSERT OR REPLACE INTO t1 VALUES(20000, 20000);
   198    198   }
          199  +
          200  +#-------------------------------------------------------------------------
          201  +reset_db
          202  +do_execsql_test 5.0 {
          203  +  PRAGMA foreign_keys = true;
          204  +  CREATE TABLE parent(
          205  +    p TEXT PRIMARY KEY
          206  +  );
          207  +  CREATE TABLE child(
          208  +    c INTEGER UNIQUE, 
          209  +    FOREIGN KEY(c) REFERENCES parent(p) DEFERRABLE INITIALLY DEFERRED
          210  +  );
          211  +  BEGIN;
          212  +    INSERT INTO child VALUES(123);
          213  +    INSERT INTO parent VALUES('123');
          214  +  COMMIT;
          215  +}
          216  +do_execsql_test 5.1 {
          217  +  PRAGMA integrity_check;
          218  +} {ok}
          219  +
          220  +do_execsql_test 5.2 {
          221  +  INSERT INTO parent VALUES(1200);
          222  +  BEGIN;
          223  +    INSERT INTO child VALUES(456);
          224  +    UPDATE parent SET p = '456' WHERE p=1200;
          225  +  COMMIT;
          226  +}
          227  +do_execsql_test 5.3 {
          228  +  PRAGMA integrity_check;
          229  +} {ok}
   199    230   
   200    231   finish_test
   201    232   

Changes to test/fts3corrupt4.test.

  3911   3911   | end crash-670b15f2955a36.db
  3912   3912   }]} {}
  3913   3913   
  3914   3914   do_catchsql_test 23.1 {
  3915   3915     SELECT 'FyzLy'FROM t1 WHERE t1 MATCH 'j';
  3916   3916   } {1 {database disk image is malformed}}
  3917   3917   
         3918  +#-------------------------------------------------------------------------
         3919  +reset_db
         3920  +do_test 24.0 {
         3921  +  sqlite3 db {}
         3922  +  db deserialize [decode_hexdb {
         3923  +.open --hexdb
         3924  +| size 28672 pagesize 4096 filename crash-369d042958c29b.db
         3925  +| page 1 offset 0
         3926  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         3927  +|     16: 10 00 01 01 00 40 20 20 00 00 03 10 00 00 00 00   .....@  ........
         3928  +|     96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21   ...............!
         3929  +|    112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00   .....~..........
         3930  +|   3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c   ...........1tabl
         3931  +|   3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42   et2t2.CREATE TAB
         3932  +|   3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01   LE t2(x).3......
         3933  +|   3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72   .5tablet1_segdir
         3934  +|   3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45   t1_segdir.CREATE
         3935  +|   3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69    TABLE 't1_segdi
         3936  +|   3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52   r'(level INTEGER
         3937  +|   3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61   ,idx INTEGER,sta
         3938  +|   3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52   rt_block INTEGER
         3939  +|   3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63   ,leaves_end_bloc
         3940  +|   3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c   k INTEGER,end_bl
         3941  +|   3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74   ock INTEGER,root
         3942  +|   3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45    BLOB,PRIMARY KE
         3943  +|   3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06   Y(level, idx))1.
         3944  +|   3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74   ..E...indexsqlit
         3945  +|   3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73   e_autoindex_t1_s
         3946  +|   3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72   egdir_1t1_segdir
         3947  +|   3760: 06 0f c7 00 08 00 10 00 00 66 04 07 17 23 23 01   .........f...##.
         3948  +|   3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e   ..tablet1_segmen
         3949  +|   3792: 64 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52   dst1_segments.CR
         3950  +|   3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73   EATE TABLE 't1_s
         3951  +|   3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64   egments'(blockid
         3952  +|   3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59    INTEGER PRIMARY
         3953  +|   3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42    KEY, block BLOB
         3954  +|   3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74   )j...!!...tablet
         3955  +|   3888: 31 4f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74   1Ocontentt1_cont
         3956  +|   3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45   ent.CREATE TABLE
         3957  +|   3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f    't1_content'(do
         3958  +|   3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 39 4d   cid INTEGER PR9M
         3959  +|   3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20   ARY KEY, 'c0a', 
         3960  +|   3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06   'c1b', 'c2c')8..
         3961  +|   3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52   ...._tablet1t1CR
         3962  +|   4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42   EATE VIRTUAL TAB
         3963  +|   4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33   LE t1 USING fts3
         3964  +|   4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00   (a,b,c).........
         3965  +| page 3 offset 8192
         3966  +|      0: 0d 00 00 00 26 0b 48 0e 0f d8 0f af 0f 86 0f 74   ....&.H........t
         3967  +|     16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5   .a.N./..........
         3968  +|     32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5   ...t.[.@.$......
         3969  +|     48: 0d bb 0d a0 0e 94 03 28 0d 4f 0d 35 0d 1b 05 0b   .......(.O.5....
         3970  +|     64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a   .......x.W.>.$..
         3971  +|   2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00   .........?%..r..
         3972  +|   2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e   .COMPILER=gcc-5.
         3973  +|   2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 55 42   4.0 20160609 DUB
         3974  +|   2928: 55 47 20 45 4e 41 e4 7c 45 20 44 42 53 54 41 54   UG ENA.|E DBSTAT
         3975  +|   2944: e4 d1 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53   ..TAB ENABLE FTS
         3976  +|   2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e   4 ENABLE FTS5 EN
         3977  +|   2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41   ABLE GEOPOLY ENA
         3978  +|   2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45   BLE JSON1 ENABLE
         3979  +|   3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20    MEMSYS5 ENABLE 
         3980  +|   3024: 42 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59   BTREE MAX MEMORY
         3981  +|   3040: 3d 35 30 30 30 30 30 30 30 20 4f 4c 49 54 20 4c   =50000000 OLIT L
         3982  +|   3056: 4f 41 43 20 45 58 54 45 4e 53 49 4f 4e 21 54 48   OAC EXTENSION!TH
         3983  +|   3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f   READSAFE=0.$..%.
         3984  +|   3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49   .THREADSAFE=0XBI
         3985  +|   3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41   NARY.#..%..THREA
         3986  +|   3120: 44 53 41 4b 75 3d 30 58 4d 4f 43 41 53 45 17 22   DSAKu=0XMOCASE..
         3987  +|   3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 46 3d   ..%..THREADSAFF=
         3988  +|   3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d   0XRTRIM.!..3..OM
         3989  +|   3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 52 49 4f   IT LOAD EXTENRIO
         3990  +|   3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f   NXBINARY. ..3..O
         3991  +|   3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49   MIT LOAD EXTENSI
         3992  +|   3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17   ONXNOCASE....3..
         3993  +|   3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53   OMIT LOAD EXTENS
         3994  +|   3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19   IONXRTRIM....3..
         3995  +|   3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30   MAX MEMORY=50000
         3996  +|   3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f   000XBINARY....3.
         3997  +|   3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30   .MAX MEMORY=5000
         3998  +|   3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33   0000XNOCASE....3
         3999  +|   3328: 0f 17 4d 42 b8 20 4d 45 4d 4f 52 59 3d 35 30 30   ..MB. MEMORY=500
         4000  +|   3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25   00000XRTRIM....%
         4001  +|   3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42   ..ENABLE RTREEXB
         4002  +|   3376: 49 4e 41 52 59 18 1a 05 0d a5 0f 19 45 4e 41 42   INARY.......ENAB
         4003  +|   3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17   LE RTREEXNOCASE.
         4004  +|   3408: 19 1c 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52   ...%..ENABLE RTR
         4005  +|   3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45   EEXRTRIM....)..E
         4006  +|   3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49   NABLE MEMSYS5XBI
         4007  +|   3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         4008  +|   3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45   E MEMSYS5XNOCASE
         4009  +|   3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45   ....)..ENABLE ME
         4010  +|   3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 14 05 01 25   MSYS5XRTRIM....%
         4011  +|   3520: 0f 19 45 4e 12 42 4c 45 20 4a 53 4f 4e 31 58 42   ..EN.BLE JSON1XB
         4012  +|   3536: 49 4e 41 52 59 18 14 05 00 25 09 d9 45 4e 41 42   INARY....%..ENAB
         4013  +|   3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17   LE JSO>1XNOCASE.
         4014  +|   3568: 13 05 00 25 0f 17 45 4e 40 42 4c 45 20 4a 53 4f   ...%..EN@BLE JSO
         4015  +|   3584: 4e 31 58 52 54 52 49 4d 1a 12 05 82 29 0f 19 45   N1XRTRIM....)..E
         4016  +|   3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49   NABLE GEOPOLYXBI
         4017  +|   3616: 4e 41 52 59 1a 11 05 c9 29 e8 19 46 4e 41 42 4c   NARY....)..FNABL
         4018  +|   3632: 48 c0 47 45 4f 50 4f 4c 59 58 4e 74 43 41 53 45   H.GEOPOLYXNtCASE
         4019  +|   3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45   ....)..ENABLE GE
         4020  +|   3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23   OPOLYXRTRIM....#
         4021  +|   3680: 0f 19 45 4e 41 42 4c 45 30 46 54 53 35 58 42 49   ..ENABLE0FTS5XBI
         4022  +|   3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c   NARY....#..ENABL
         4023  +|   3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0e 05   E FTS5XNOCASE...
         4024  +|   3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58   .#..ENABLE FTS5X
         4025  +|   3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42   RTRIM....#..ENAB
         4026  +|   3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b   LE FTS4XBINARY..
         4027  +|   3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34   ..#..ENABLE FTS4
         4028  +|   3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e   XNOCASE....#..EN
         4029  +|   3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e   ABLE FTS4XRTRIM.
         4030  +|   3824: 09 05 00 31 0f 19 45 4e 42 42 4c 45 20 44 42 53   ...1..ENBBLE DBS
         4031  +|   3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e   TAT VTABXBINARY.
         4032  +|   3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         4033  +|   3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d   TAT VTABXNOCASE.
         4034  +|   3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         4035  +|   3904: 54 41 54 20 56 54 41 42 58 52 54 52 4a 4d 11 06   TAT VTABXRTRJM..
         4036  +|   3920: 05 f0 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52   .....DEBUGXBINAR
         4037  +|   3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f   Y.......DEBUGXNO
         4038  +|   3952: 43 41 53 45 10 04 05 00 17 0f 16 44 45 42 55 47   CASE.......DEBUG
         4039  +|   3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d   XRTRIM'...C..COM
         4040  +|   3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20   PILER=gcc-5.4.0 
         4041  +|   4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27   20160609XBINARY'
         4042  +|   4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67   ...C..COMPILER=g
         4043  +|   4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30   cc-5.4.0 2016060
         4044  +|   4048: 39 58 4e 4f 43 41 53 45 26 01 06 00 43 0f 17 43   9XNOCASE&...C..C
         4045  +|   4064: 4f 4d 50 49 4b 45 52 3d 67 63 63 2d 35 2e 34 2e   OMPIKER=gcc-5.4.
         4046  +|   4080: 30 20 32 30 31 36 30 36 40 39 58 29 54 52 49 4d   0 201606@9X)TRIM
         4047  +| page 4 offset 12288
         4048  +|      0: 0d 00 10 00 00 10 00 00 00 00 00 00 00 01 00 00   ................
         4049  +| page 5 offset 16384
         4050  +|      0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00   ................
         4051  +|   2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33   ...........0 253
         4052  +|   2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36   ..0.%.....201606
         4053  +|   3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35   09.%....4.%....5
         4054  +|   3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a   .%....0000000.%.
         4055  +|   3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00   ...compiler.%...
         4056  +|   3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75   .dbstat.%....ebu
         4057  +|   3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09   g.%....enable.%.
         4058  +|   3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f   .........xtensio
         4059  +|   3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03   n.%....fts4.%...
         4060  +|   3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01   .5.%....gcc.%...
         4061  +|   3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f   .eopoly.%....jso
         4062  +|   3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00   n1.%....load.%..
         4063  +|   3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6e 6f 72 79   ..max.%....enory
         4064  +|   3184: 03 25 19 00 03 04 ce 79 73 4d 03 25 15 00 00 04   .%.....ysM.%....
         4065  +|   3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03   omit.%....rtree.
         4066  +|   3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03   %....threadsafe.
         4067  +|   3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01   %....vtab.%...P.
         4068  +|   3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30   ........0 835..0
         4069  +|   3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 09   ................
         4070  +|   3280: 51 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07   Q....20160609...
         4071  +|   3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05   .........4......
         4072  +|   3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04   ......5.........
         4073  +|   3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04   ...0000000......
         4074  +|   3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02   ......binary<...
         4075  +|   3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         4076  +|   3376: 00 03 01 02 02 00 02 f1 02 02 00 03 01 02 02 00   ................
         4077  +|   3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03   ................
         4078  +|   3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69   ...........compi
         4079  +|   3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 67   ler............g
         4080  +|   3440: d2 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01   .stat...........
         4081  +|   3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00   .ebug...........
         4082  +|   3472: 06 65 6e 6f 82 6c 65 3f 07 02 00 01 02 00 01 02   .eno.le?........
         4083  +|   3488: b0 01 02 00 01 02 00 11 02 00 01 02 00 01 02 00   ................
         4084  +|   3504: 01 02 00 01 02 00 01 02 00 01 a6 00 01 02 00 01   ................
         4085  +|   3520: 02 05 51 02 00 01 02 00 01 02 00 01 02 00 01 02   ..Q.............
         4086  +|   3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f   .........xtensio
         4087  +|   3552: 6e 09 1f 04 00 01 04 00 00 04 00 00 04 66 74 73   n............fts
         4088  +|   3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d   4............5..
         4089  +|   3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03   ..........gcc...
         4090  +|   3600: 00 01 03 00 01 03 00 01 06 65 6f 70 73 6c 79 09   .........eopsly.
         4091  +|   3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31   ...........json1
         4092  +|   3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64   ............load
         4093  +|   3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09   ............max.
         4094  +|   3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79   ...........emory
         4095  +|   3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35   ............sys5
         4096  +|   3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61   ............noca
         4097  +|   3712: 73 65 3c 02 01 02 02 00 03 01 12 02 00 03 01 02   se<.............
         4098  +|   3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         4099  +|   3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00   ................
         4100  +|   3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00   ................
         4101  +|   3776: 04 6f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00   .omit...........
         4102  +|   3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00   .rtree..........
         4103  +|   3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03   ..im<...........
         4104  +|   3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01   ................
         4105  +|   3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02   ................
         4106  +|   3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         4107  +|   3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02   ...threadsafe...
         4108  +|   3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04   .........vtab...
         4109  +|   3904: 00 01 04 00 01 03 ff ff 01 78 b4 01 01 01 01 02   .........x......
         4110  +|   3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00   ................
         4111  +|   3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01   ................
         4112  +|   3952: 01 01 02 00 01 01 01 07 30 01 01 01 02 00 01 01   ........0.......
         4113  +|   3968: 01 02 00 11 01 01 02 00 01 01 01 02 00 11 01 01   ................
         4114  +|   3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02   ................
         4115  +|   4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00   ................
         4116  +|   4016: 01 01 01 01 ff 01 01 01 02 00 01 01 01 02 00 01   ................
         4117  +|   4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01   ................
         4118  +|   4048: 01 02 00 01 01 09 c2 00 01 01 01 02 00 01 01 01   ................
         4119  +|   4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02   ................
         4120  +|   4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00   ................
         4121  +| page 6 offset 20480
         4122  +|      0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00   ................
         4123  +|   4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09   ................
         4124  +| page 7 offset 24576
         4125  +|      0: 0d 00 00 00 05 0f b8 00 0e f4 0f e9 10 d6 0f c7   ................
         4126  +|   4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f   ...........#auto
         4127  +|   4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65   merge=5...#merge
         4128  +|   4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00   =1..............
         4129  +| end crash-369d042958c29b.db
         4130  +}]} {}
         4131  +
         4132  +do_catchsql_test 24.1 {
         4133  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT '4hE'+x FROM c WHERE x<72)
         4134  +    INSERT INTO t1(a) SELECT randomblob(2829) FROM c;
         4135  +} {0 {}}
         4136  +
         4137  +do_catchsql_test 24.2 {
         4138  +  UPDATE t1 SET b=quote((true) ) WHERE t1 MATCH 'h';
         4139  +} {0 {}}
         4140  +
         4141  +do_catchsql_test 24.3 {
         4142  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<72)
         4143  +    INSERT INTO t1(a) SELECT randomblob(2829) FROM c;
         4144  +} {0 {}}
         4145  +
         4146  +do_catchsql_test 24.4 {
         4147  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT null<<x FROM c WHERE x<72)
         4148  +    INSERT INTO t1(a) SELECT randomblob(2829) FROM c;
         4149  +} {0 {}}
         4150  +
         4151  +do_catchsql_test 24.5 {
         4152  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT 3+x FROM c WHERE x<72)
         4153  +    INSERT INTO t1(a) SELECT randomblob(2829) FROM c;
         4154  +} {0 {}}
         4155  +
         4156  +do_catchsql_test 24.7 {
         4157  +  INSERT INTO t1(t1) SELECT x FROM t2;
         4158  +} {1 {database disk image is malformed}}
         4159  +
         4160  +#-------------------------------------------------------------------------
         4161  +#-------------------------------------------------------------------------
         4162  +reset_db
         4163  +do_test 25.0 {
         4164  +  sqlite3 db {}
         4165  +  db deserialize [decode_hexdb {
         4166  +.open --hexdb
         4167  +| size 28672 pagesize 4096 filename crash-dde9e76ed8ab2d.db
         4168  +| page 1 offset 0
         4169  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         4170  +|     16: 10 00 01 01 00 40 20 20 00 00 03 10 00 00 00 00   .....@  ........
         4171  +|     96: 00 00 00 00 0d 0e b1 00 06 0d a4 00 0f 8d 0f 21   ...............!
         4172  +|    112: 0e b9 0d c8 0e 7e 0d a4 00 00 00 00 00 00 00 00   .....~..........
         4173  +|   3488: 00 00 00 00 22 07 06 17 11 11 01 31 74 61 62 6c   ...........1tabl
         4174  +|   3504: 65 74 32 74 32 07 43 52 45 41 54 45 20 54 41 42   et2t2.CREATE TAB
         4175  +|   3520: 4c 45 20 74 32 28 78 29 81 33 05 07 17 1f 1f 01   LE t2(x).3......
         4176  +|   3536: 82 35 74 61 62 6c 65 74 31 5f 73 65 67 64 69 72   .5tablet1_segdir
         4177  +|   3552: 74 31 5f 73 65 67 64 69 72 05 43 52 45 41 54 45   t1_segdir.CREATE
         4178  +|   3568: 20 54 41 42 4c 45 20 27 74 31 5f 73 65 67 64 69    TABLE 't1_segdi
         4179  +|   3584: 72 27 28 6c 65 76 65 6c 20 49 4e 54 45 47 45 52   r'(level INTEGER
         4180  +|   3600: 2c 69 64 78 20 49 4e 54 45 47 45 52 2c 73 74 61   ,idx INTEGER,sta
         4181  +|   3616: 72 74 5f 62 6c 6f 63 6b 20 49 4e 54 45 47 45 52   rt_block INTEGER
         4182  +|   3632: 2c 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63   ,leaves_end_bloc
         4183  +|   3648: 6b 20 49 4e 54 45 47 45 52 2c 65 6e 64 5f 62 6c   k INTEGER,end_bl
         4184  +|   3664: 6f 63 6b 20 49 4e 54 45 47 45 52 2c 72 6f 6f 74   ock INTEGER,root
         4185  +|   3680: 20 42 4c 4f 42 2c 50 52 49 4d 41 52 59 20 4b 45    BLOB,PRIMARY KE
         4186  +|   3696: 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 29 31 06   Y(level, idx))1.
         4187  +|   3712: 06 17 45 1f 01 00 69 6e 64 65 78 73 71 6c 69 74   ..E...indexsqlit
         4188  +|   3728: 65 5f 61 75 74 6f 69 6e 64 65 78 5f 74 31 5f 73   e_autoindex_t1_s
         4189  +|   3744: 65 67 64 69 72 5f 31 74 31 5f 73 65 67 64 69 72   egdir_1t1_segdir
         4190  +|   3760: 06 0f c7 00 08 00 00 00 00 66 04 07 17 23 23 01   .........f...##.
         4191  +|   3776: 81 13 74 61 62 6c 65 74 31 5f 73 65 67 6d 65 6e   ..tablet1_segmen
         4192  +|   3792: 64 73 74 31 5f 73 65 67 6d 65 6e 74 73 04 43 52   dst1_segments.CR
         4193  +|   3808: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 73   EATE TABLE 't1_s
         4194  +|   3824: 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69 64   egments'(blockid
         4195  +|   3840: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59    INTEGER PRIMARY
         4196  +|   3856: 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42    KEY, block BLOB
         4197  +|   3872: 29 6a 03 07 17 21 21 01 81 1f 74 61 62 6c 65 74   )j...!!...tablet
         4198  +|   3888: 31 5f 63 6f 6e 74 65 6e 74 74 31 5f 63 6f 6e 74   1_contentt1_cont
         4199  +|   3904: 65 6e 74 03 43 52 45 41 54 45 20 54 41 42 4c 45   ent.CREATE TABLE
         4200  +|   3920: 20 27 74 31 5f 63 6f 6e 74 65 6e 74 27 28 64 6f    't1_content'(do
         4201  +|   3936: 63 69 64 20 49 4e 54 45 47 45 52 20 50 52 39 4d   cid INTEGER PR9M
         4202  +|   3952: 41 52 59 20 4b 45 59 2c 20 27 63 30 61 27 2c 20   ARY KEY, 'c0a', 
         4203  +|   3968: 27 63 31 62 27 2c 20 27 63 32 63 27 29 38 02 06   'c1b', 'c2c')8..
         4204  +|   3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52   ...._tablet1t1CR
         4205  +|   4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42   EATE VIRTUAL TAB
         4206  +|   4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 33   LE t1 USING fts3
         4207  +|   4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00   (a,b,c).........
         4208  +| page 3 offset 8192
         4209  +|      0: 0d 00 00 00 26 0b 48 0e 0f d8 0f af 0f 86 0f 74   ....&.H........t
         4210  +|     16: 0f 61 0f 4e 0f 2f 0f 0f 0e ef 0e d7 0e be 0e a5   .a.N./..........
         4211  +|     32: 0e 8d 0e 74 0e 5b 0e 40 0e 24 0e 08 0d ef 0d d5   ...t.[.@.$......
         4212  +|     48: 0d bb 0d a0 0e 94 03 28 0d 4f 0d 35 0d 1b 05 0b   .......(.O.5....
         4213  +|     64: 0c da 0c b9 0c 99 0c 78 0c 57 0c 3e 0c 24 0c 0a   .......x.W.>.$..
         4214  +|   2880: 00 00 00 00 00 00 00 00 81 3f 25 06 00 72 7f 00   .........?%..r..
         4215  +|   2896: 00 43 4f 4d 50 49 4c 45 52 3d 67 63 63 2d 35 2e   .COMPILER=gcc-5.
         4216  +|   2912: 34 2e 30 20 32 30 31 36 30 36 30 39 20 44 55 42   4.0 20160609 DUB
         4217  +|   2928: 55 47 20 45 4e 41 e4 7c 45 20 44 42 53 54 41 54   UG ENA.|E DBSTAT
         4218  +|   2944: e4 46 54 41 42 20 45 4e 41 42 4c 45 20 46 54 53   .FTAB ENABLE FTS
         4219  +|   2960: 34 20 45 4e 41 42 4c 45 20 46 54 53 35 20 45 4e   4 ENABLE FTS5 EN
         4220  +|   2976: 41 42 4c 45 20 47 45 4f 50 4f 4c 59 20 45 4e 41   ABLE GEOPOLY ENA
         4221  +|   2992: 42 4c 45 20 4a 53 4f 4e 31 20 45 4e 41 42 4c 45   BLE JSON1 ENABLE
         4222  +|   3008: 20 4d 45 4d 53 59 53 35 20 45 4e 41 42 4c 45 20    MEMSYS5 ENABLE 
         4223  +|   3024: 42 54 52 45 45 20 4d 41 58 20 4d 45 4d 4f 52 59   BTREE MAX MEMORY
         4224  +|   3040: 3d 35 30 30 30 30 30 30 30 20 4f 4c 49 54 20 4c   =50000000 OLIT L
         4225  +|   3056: 4f 41 43 20 45 58 54 45 4e 53 49 4f 4e 21 54 48   OAC EXTENSION!TH
         4226  +|   3072: 52 45 41 44 53 41 46 45 3d 30 18 24 05 00 25 0f   READSAFE=0.$..%.
         4227  +|   3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49   .THREADSAFE=0XBI
         4228  +|   3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41   NARY.#..%..THREA
         4229  +|   3120: 44 53 41 4b 75 3d 30 58 4d 4f 43 41 53 45 17 22   DSAKu=0XMOCASE..
         4230  +|   3136: 05 00 25 0f 17 54 48 52 45 41 44 53 41 46 46 3d   ..%..THREADSAFF=
         4231  +|   3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d   0XRTRIM.!..3..OM
         4232  +|   3168: 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 52 49 4f   IT LOAD EXTENRIO
         4233  +|   3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f   NXBINARY. ..3..O
         4234  +|   3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49   MIT LOAD EXTENSI
         4235  +|   3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17   ONXNOCASE....3..
         4236  +|   3232: 4f 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53   OMIT LOAD EXTENS
         4237  +|   3248: 49 4f 4e 58 52 54 52 49 4d 1f 1e 05 00 33 0f 19   IONXRTRIM....3..
         4238  +|   3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30   MAX MEMORY=50000
         4239  +|   3280: 30 30 30 58 42 49 4e 41 52 59 1f 1d 05 00 33 0f   000XBINARY....3.
         4240  +|   3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30   .MAX MEMORY=5000
         4241  +|   3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 33   0000XNOCASE....3
         4242  +|   3328: 0f 17 4d 42 b8 20 4d 45 4d 4f 52 59 3d 35 30 30   ..MB. MEMORY=500
         4243  +|   3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25   00000XRTRIM....%
         4244  +|   3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42   ..ENABLE RTREEXB
         4245  +|   3376: 49 4e 41 52 59 18 1a 05 0d a5 0f 19 45 4e 41 42   INARY.......ENAB
         4246  +|   3392: 4c 45 20 52 54 52 45 45 58 4e 4f 43 41 53 45 17   LE RTREEXNOCASE.
         4247  +|   3408: 19 1c 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52   ...%..ENABLE RTR
         4248  +|   3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45   EEXRTRIM....)..E
         4249  +|   3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49   NABLE MEMSYS5XBI
         4250  +|   3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         4251  +|   3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45   E MEMSYS5XNOCASE
         4252  +|   3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45   ....)..ENABLE ME
         4253  +|   3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 14 05 01 25   MSYS5XRTRIM....%
         4254  +|   3520: 0f 19 45 4e 12 42 4c 45 20 4a 53 4f 4e 31 58 42   ..EN.BLE JSON1XB
         4255  +|   3536: 49 4e 41 52 59 18 14 05 00 25 09 d9 45 4e 41 42   INARY....%..ENAB
         4256  +|   3552: 4c 45 20 4a 53 4f 3e 31 58 4e 4f 43 41 53 45 17   LE JSO>1XNOCASE.
         4257  +|   3568: 13 05 00 25 0f 17 45 4e 40 42 4c 45 20 4a 53 4f   ...%..EN@BLE JSO
         4258  +|   3584: 4e 31 58 52 54 52 49 4d 1a 12 05 82 29 0f 19 45   N1XRTRIM....)..E
         4259  +|   3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49   NABLE GEOPOLYXBI
         4260  +|   3616: 4e 41 52 59 1a 11 05 c9 29 e8 19 46 4e 41 42 4c   NARY....)..FNABL
         4261  +|   3632: 48 c0 47 45 4f 50 4f 4c 59 58 4e 74 43 41 53 45   H.GEOPOLYXNtCASE
         4262  +|   3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45   ....)..ENABLE GE
         4263  +|   3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23   OPOLYXRTRIM....#
         4264  +|   3680: 0f 19 45 4e 41 42 4c 45 30 46 54 53 35 58 42 49   ..ENABLE0FTS5XBI
         4265  +|   3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c   NARY....#..ENABL
         4266  +|   3712: 45 20 46 54 53 35 58 4e 4f 43 41 53 45 16 0e 05   E FTS5XNOCASE...
         4267  +|   3728: 00 23 0f 17 45 4e 41 42 4c 45 20 46 54 53 35 58   .#..ENABLE FTS5X
         4268  +|   3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42   RTRIM....#..ENAB
         4269  +|   3760: 4c 45 20 46 54 53 34 58 42 49 4e 41 52 59 17 0b   LE FTS4XBINARY..
         4270  +|   3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34   ..#..ENABLE FTS4
         4271  +|   3792: 58 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e   XNOCASE....#..EN
         4272  +|   3808: 41 42 4c 45 20 46 54 53 34 58 52 54 52 49 4d 1e   ABLE FTS4XRTRIM.
         4273  +|   3824: 09 05 00 31 0f 19 45 4e 42 42 4c 45 20 44 42 53   ...1..ENBBLE DBS
         4274  +|   3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e   TAT VTABXBINARY.
         4275  +|   3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         4276  +|   3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d   TAT VTABXNOCASE.
         4277  +|   3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         4278  +|   3904: 54 41 54 20 56 54 41 42 58 52 54 52 4a 4d 11 06   TAT VTABXRTRJM..
         4279  +|   3920: 05 f0 17 0f 19 44 45 42 55 47 58 42 49 4e 41 52   .....DEBUGXBINAR
         4280  +|   3936: 59 11 05 05 00 17 0e 19 44 45 42 55 47 58 4e 4f   Y.......DEBUGXNO
         4281  +|   3952: 43 41 53 45 10 04 05 00 17 0f 16 44 45 42 55 47   CASE.......DEBUG
         4282  +|   3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d   XRTRIM'...C..COM
         4283  +|   3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20   PILER=gcc-5.4.0 
         4284  +|   4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27   20160609XBINARY'
         4285  +|   4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3d 67   ...C..COMPILER=g
         4286  +|   4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30   cc-5.4.0 2016060
         4287  +|   4048: 39 58 4e 4f 43 41 53 45 26 01 06 00 43 0f 17 43   9XNOCASE&...C..C
         4288  +|   4064: 4f 4d 50 49 4b 45 52 3d 67 63 63 2d 35 2e 34 2e   OMPIKER=gcc-5.4.
         4289  +|   4080: 30 20 32 30 31 36 30 36 40 39 58 29 54 52 49 4d   0 201606@9X)TRIM
         4290  +| page 4 offset 12288
         4291  +|      0: 0d 00 10 00 00 10 00 00 00 00 00 00 00 01 00 00   ................
         4292  +| page 5 offset 16384
         4293  +|      0: 0d 00 00 00 02 0b a0 00 0c ad 0b a0 00 00 00 00   ................
         4294  +|   2976: 82 0a 02 08 08 09 08 08 17 84 06 30 20 32 35 33   ...........0 253
         4295  +|   2992: 00 01 30 04 25 06 1b 00 00 08 32 30 31 36 30 36   ..0.%.....201606
         4296  +|   3008: 30 39 03 25 07 00 00 01 34 03 25 05 00 00 01 35   09.%....4.%....5
         4297  +|   3024: 03 25 04 00 01 07 30 30 30 30 30 30 30 03 25 1a   .%....0000000.%.
         4298  +|   3040: 00 00 08 63 6f 6d 70 69 6c 65 72 03 25 02 00 00   ...compiler.%...
         4299  +|   3056: 06 64 62 73 74 61 74 03 25 0a 00 01 04 65 62 75   .dbstat.%....ebu
         4300  +|   3072: 67 03 25 08 00 00 06 65 6e 61 62 6c 65 09 25 09   g.%....enable.%.
         4301  +|   3088: 05 04 04 04 04 04 00 01 08 78 74 65 6e 73 69 6f   .........xtensio
         4302  +|   3104: 6e 03 25 1d 00 00 04 66 74 73 34 03 25 0d 00 03   n.%....fts4.%...
         4303  +|   3120: 01 35 03 25 0f 00 00 03 67 63 63 03 25 03 00 01   .5.%....gcc.%...
         4304  +|   3136: 06 65 6f 70 6f 6c 79 03 25 11 00 00 05 6a 73 6f   .eopoly.%....jso
         4305  +|   3152: 6e 31 03 25 13 00 00 04 6c 6f 61 64 03 25 1c 00   n1.%....load.%..
         4306  +|   3168: 00 03 6d 61 78 03 25 18 00 01 05 65 6e 6f 72 79   ..max.%....enory
         4307  +|   3184: 03 25 19 00 03 04 ce 79 73 4d 03 25 15 00 00 04   .%.....ysM.%....
         4308  +|   3200: 6f 6d 69 74 03 25 1b 00 00 05 72 74 72 65 65 03   omit.%....rtree.
         4309  +|   3216: 25 17 00 00 0a 74 68 72 65 61 64 73 61 66 65 03   %....threadsafe.
         4310  +|   3232: 25 0e 00 00 04 76 74 61 62 03 25 0b 00 86 50 01   %....vtab.%...P.
         4311  +|   3248: 08 08 08 08 08 17 8d 12 30 20 38 33 35 00 01 30   ........0 835..0
         4312  +|   3264: 12 01 06 00 01 06 00 01 06 00 1f 03 00 01 03 09   ................
         4313  +|   3280: 51 03 00 00 08 32 30 31 36 30 36 30 39 09 01 07   Q....20160609...
         4314  +|   3296: 00 01 07 00 01 07 00 00 01 34 09 01 05 00 01 05   .........4......
         4315  +|   3312: 00 01 05 00 00 01 35 09 01 04 00 01 04 00 01 04   ......5.........
         4316  +|   3328: 00 01 07 30 30 30 30 30 30 30 09 1c 04 00 01 04   ...0000000......
         4317  +|   3344: 00 01 04 00 00 06 62 69 6e 61 72 79 3c 03 01 02   ......binary<...
         4318  +|   3360: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         4319  +|   3376: 00 03 01 02 02 00 02 f1 02 02 00 03 01 02 02 00   ................
         4320  +|   3392: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03   ................
         4321  +|   3408: 01 02 02 00 03 01 02 02 00 00 08 63 6f 6d 70 69   ...........compi
         4322  +|   3424: 6c 65 72 09 01 02 00 01 02 00 01 02 00 00 06 67   ler............g
         4323  +|   3440: d2 73 74 61 74 09 07 03 00 01 03 00 01 03 00 01   .stat...........
         4324  +|   3456: 04 65 62 75 67 09 04 02 00 01 02 00 01 02 00 00   .ebug...........
         4325  +|   3472: 06 65 6e 6f 82 6c 65 3f 07 02 00 01 02 00 01 02   .eno.le?........
         4326  +|   3488: b0 01 02 00 01 02 00 11 02 00 01 02 00 01 02 00   ................
         4327  +|   3504: 01 02 00 01 02 00 01 02 00 01 a6 00 01 02 00 01   ................
         4328  +|   3520: 02 05 51 02 00 01 02 00 01 02 00 01 02 00 01 02   ..Q.............
         4329  +|   3536: 00 01 02 00 01 02 00 01 08 78 74 65 6e 73 69 6f   .........xtensio
         4330  +|   3552: 6e 09 1f 04 00 01 04 00 00 04 00 00 04 66 74 73   n............fts
         4331  +|   3568: 34 09 0a 03 00 01 03 00 01 03 00 03 01 35 09 0d   4............5..
         4332  +|   3584: 03 00 01 03 00 01 03 00 00 03 67 63 63 09 01 03   ..........gcc...
         4333  +|   3600: 00 01 03 00 01 03 00 01 06 65 6f 70 73 6c 79 09   .........eopsly.
         4334  +|   3616: 10 03 00 01 03 00 01 03 00 00 05 6a 73 6f 6e 31   ...........json1
         4335  +|   3632: 09 13 03 00 01 03 00 01 03 00 00 04 6c 6f 61 64   ............load
         4336  +|   3648: 09 1f 03 00 01 03 00 01 03 00 00 03 6d 61 78 09   ............max.
         4337  +|   3664: 1c 02 00 01 02 00 01 02 00 01 05 65 6d 6f 72 79   ...........emory
         4338  +|   3680: 09 1c 03 00 01 03 00 01 03 00 03 04 73 79 73 35   ............sys5
         4339  +|   3696: 09 16 03 00 01 03 00 01 03 00 00 06 6e 6f 63 61   ............noca
         4340  +|   3712: 73 65 3c 02 01 02 02 00 03 01 12 02 00 03 01 02   se<.............
         4341  +|   3728: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         4342  +|   3744: 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00   ................
         4343  +|   3760: 03 01 02 02 00 03 01 02 02 00 03 01 02 02 00 00   ................
         4344  +|   3776: 0e 9f 6d 69 74 09 1f 02 00 01 02 00 01 02 00 00   ..mit...........
         4345  +|   3792: 05 72 74 72 65 65 09 19 03 00 01 03 00 01 03 00   .rtree..........
         4346  +|   3808: 03 02 69 6d 3c 01 01 02 02 00 03 01 02 02 00 03   ..im<...........
         4347  +|   3824: 01 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01   ................
         4348  +|   3840: 02 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02   ................
         4349  +|   3856: 02 00 03 01 02 02 00 03 01 02 02 00 03 01 02 02   ................
         4350  +|   3872: 00 00 0a 74 68 72 65 61 64 73 61 66 65 09 22 02   ...threadsafe...
         4351  +|   3888: 00 01 02 00 01 02 00 00 04 76 74 61 62 09 07 04   .........vtab...
         4352  +|   3904: 00 01 04 00 01 04 00 00 01 78 b4 01 01 01 01 02   .........x......
         4353  +|   3920: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00   ................
         4354  +|   3936: 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01   ................
         4355  +|   3952: 01 01 02 00 01 01 01 07 30 01 01 01 02 00 01 01   ........0.......
         4356  +|   3968: 01 02 00 11 01 01 02 00 01 01 01 02 00 11 01 01   ................
         4357  +|   3984: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02   ................
         4358  +|   4000: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00   ................
         4359  +|   4016: 01 01 01 01 ff 01 01 01 02 00 01 01 01 02 00 01   ................
         4360  +|   4032: 01 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01   ................
         4361  +|   4048: 01 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01   ................
         4362  +|   4064: 02 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02   ................
         4363  +|   4080: 00 01 01 01 02 00 01 01 01 02 00 01 01 01 02 00   ................
         4364  +| page 6 offset 20480
         4365  +|      0: 0a 00 00 00 02 0f f5 00 0f fb 0f f5 00 00 00 00   ................
         4366  +|   4080: 00 00 00 00 00 05 04 08 09 01 02 04 04 08 08 09   ................
         4367  +| page 7 offset 24576
         4368  +|      0: 0d 00 00 00 05 0f b8 00 0e f4 0f e9 10 d6 0f c7   ................
         4369  +|   4016: 00 00 00 00 00 00 00 00 0d 05 02 23 61 75 74 6f   ...........#auto
         4370  +|   4032: 6d 65 72 67 65 3d 35 0d 04 02 23 6d 65 72 67 65   merge=5...#merge
         4371  +|   4048: 3d 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00   =1..............
         4372  +| end crash-dde9e76ed8ab2d.db
         4373  +}]} {}
         4374  +
         4375  +do_catchsql_test 25.1 {
         4376  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x%1 FROM c WHERE x<599237)
         4377  +    INSERT INTO t1( a ) SELECT randomblob(3000) FROM t2 ;
         4378  +} {0 {}}
         4379  +
         4380  +do_catchsql_test 25.2 {
         4381  +  UPDATE t1 SET b=quote((true) ) WHERE t1 MATCH 'h*';
         4382  +} {0 {}}
         4383  +
         4384  +do_catchsql_test 25.3 {
         4385  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x +x FROM c WHERE 72<x)
         4386  +    INSERT INTO t1(a) SELECT randomblob(2829) FROM c;
         4387  +} {0 {}}
         4388  +
         4389  +do_catchsql_test 25.4 {
         4390  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x%1 FROM c WHERE 599237<x)
         4391  +    INSERT INTO t1(a) SELECT randomblob(3000) FROM t2 ;
         4392  +} {0 {}}
         4393  +
         4394  +do_catchsql_test 25.5 {
         4395  +  WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x%1 FROM c WHERE x<599237)
         4396  +    INSERT INTO t1( a ) SELECT randomblob(3000) FROM t2 ;
         4397  +} {0 {}}
         4398  +
         4399  +do_catchsql_test 25.6 {
         4400  +  INSERT INTO t1(t1) SELECT x FROM t2;
         4401  +  INSERT INTO t1(t1) SELECT x FROM t2;
         4402  +} {1 {database disk image is malformed}}
         4403  + 
  3918   4404   finish_test
  3919   4405   

Added test/fts4rename.test.

            1  +# 2019 April 30
            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  +source $testdir/fts3_common.tcl
           16  +set ::testprefix fts4rename
           17  +
           18  +# If SQLITE_ENABLE_FTS3 is defined, omit this file.
           19  +ifcapable !fts3 {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +do_execsql_test 1.0 {
           25  +  CREATE VIRTUAL TABLE temp.t1 USING fts3(a);
           26  +  BEGIN;
           27  +  CREATE TABLE t2(x);
           28  +} {}
           29  +
           30  +do_catchsql_test 1.1 {
           31  +  ALTER TABLE t1_content RENAME c0a TO docid;
           32  +} {1 {duplicate column name: docid}}
           33  +
           34  +do_catchsql_test 1.2 {
           35  +  UPDATE t1 SET Col0 = 1 ;
           36  +} {1 {no such column: Col0}}
           37  +
           38  +do_catchsql_test 1.3 {
           39  +  ROLLBACK;
           40  +  DROP TABLE t1;
           41  +} {0 {}}
           42  +
           43  +finish_test
           44  +

Changes to test/func.test.

   311    311     } {99999999999995.0}
   312    312     do_test func-4.37 {
   313    313       execsql {SELECT round(9999999999999.55,1);}
   314    314     } {9999999999999.6}
   315    315     do_test func-4.38 {
   316    316       execsql {SELECT round(9999999999999.556,2);}
   317    317     } {9999999999999.56}
          318  +  do_execsql_test func-4.39 {
          319  +    SELECT round(1e500), round(-1e500);
          320  +  } {Inf -Inf}
   318    321   }
   319    322   
   320    323   # Test the upper() and lower() functions
   321    324   #
   322    325   do_test func-5.1 {
   323    326     execsql {SELECT upper(t1) FROM tbl1}
   324    327   } {THIS PROGRAM IS FREE SOFTWARE}

Changes to test/fuzzdata7.db.

cannot compute difference between binary files

Changes to test/fuzzdata8.db.

cannot compute difference between binary files

Changes to test/index.test.

   734    734        CREATE TEMP TABLE t6(x);
   735    735        INSERT INTO temp.t6 values(1),(5),(9);
   736    736        CREATE INDEX temp.i21 ON t6(x);
   737    737        SELECT x FROM t6 ORDER BY x DESC;
   738    738     }
   739    739   } {0 {9 5 1}}
   740    740   
   741         -   
          741  +# 2019-05-01 ticket https://www.sqlite.org/src/info/3be1295b264be2fa
          742  +do_execsql_test index-22.0 {
          743  +  DROP TABLE IF EXISTS t1;
          744  +  CREATE TABLE t1(a, b TEXT);
          745  +  CREATE UNIQUE INDEX IF NOT EXISTS x1 ON t1(b==0);
          746  +  CREATE INDEX IF NOT EXISTS x2 ON t1(a || 0) WHERE b;
          747  +  INSERT INTO t1(a,b) VALUES('a',1),('a',0);
          748  +  SELECT a, b, '|' FROM t1;
          749  +} {a 1 | a 0 |}
          750  +
          751  +# 2019-05-10 ticket https://www.sqlite.org/src/info/ae0f637bddc5290b
          752  +do_execsql_test index-23.0 {
          753  +  DROP TABLE t1;
          754  +  CREATE TABLE t1(a TEXT, b REAL);
          755  +  CREATE UNIQUE INDEX t1x1 ON t1(a GLOB b);
          756  +  INSERT INTO t1(a,b) VALUES('0.0','1'),('1.0','1');
          757  +  SELECT * FROM t1;
          758  +  REINDEX;
          759  +} {0.0 1.0 1.0 1.0}
          760  +do_execsql_test index-23.1 {
          761  +  DROP TABLE t1;
          762  +  CREATE TABLE t1(a REAL);
          763  +  CREATE UNIQUE INDEX index_0 ON t1(TYPEOF(a));
          764  +  INSERT OR IGNORE INTO t1(a) VALUES (0.1),(FALSE);
          765  +  SELECT * FROM t1;
          766  +  REINDEX;
          767  +} {0.1}
   742    768   
   743    769   finish_test

Changes to test/index6.test.

   406    406     SELECT 'one', * FROM t2 WHERE x NOT IN (SELECT a FROM t1);
   407    407     CREATE INDEX t1a ON t1(a) WHERE b=1;
   408    408     SELECT 'two', * FROM t2 WHERE x NOT IN (SELECT a FROM t1);
   409    409   } {}
   410    410   do_execsql_test index6-12.2 {
   411    411     SELECT x FROM t2 WHERE x IN (SELECT a FROM t1) ORDER BY +x;
   412    412   } {1 2}
          413  +
          414  +# 2019-05-04
          415  +# Ticket https://www.sqlite.org/src/tktview/5c6955204c392ae763a95
          416  +# Theorem prover error
          417  +#
          418  +do_execsql_test index6-13.1 {
          419  +  DROP TABLE IF EXISTS t0;
          420  +  CREATE TABLE t0(c0);
          421  +  CREATE INDEX index_0 ON t0(c0) WHERE c0 NOT NULL;
          422  +  INSERT INTO t0(c0) VALUES (NULL);
          423  +  SELECT * FROM t0 WHERE c0 OR 1;
          424  +} {{}}
          425  +
          426  +# 2019-05-11
          427  +# Ticket https://sqlite.org/src/tktview/8025674847
          428  +reset_db
          429  +do_execsql_test index6-14.1 {
          430  +  CREATE TABLE IF NOT EXISTS t0 (c0, c1);
          431  +  CREATE INDEX IF NOT EXISTS i0 ON t0(c0, c1) WHERE c0 NOT NULL;
          432  +  INSERT INTO t0(c0, c1) VALUES(NULL, 'row');
          433  +  SELECT * FROM t0 WHERE t0.c0 IS NOT 1;
          434  +} {{} row}
          435  +
          436  +do_execsql_test index6-14.2 {
          437  +  SELECT * FROM t0 WHERE CASE c0 WHEN 0 THEN 0 ELSE 1 END;
          438  +} {{} row}
          439  +
   413    440   finish_test
          441  +

Changes to test/index7.test.

   182    182       CREATE INDEX t1c ON t1(c);
   183    183       ANALYZE;
   184    184       SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
   185    185       PRAGMA integrity_check;
   186    186     }
   187    187   } {t1 {15 1} t1a {10 1} t1b {8 1} t1c {15 1} ok}
   188    188   
   189         -# Queries use partial indices as appropriate times.
          189  +# Queries use partial indices at appropriate times.
   190    190   #
   191    191   do_test index7-2.1 {
   192    192     execsql {
   193    193       CREATE TABLE t2(a,b PRIMARY KEY) without rowid;
   194    194       INSERT INTO t2(a,b) SELECT value, value FROM nums WHERE value<1000;
   195    195       UPDATE t2 SET a=NULL WHERE b%5==0;
   196    196       CREATE INDEX t2a1 ON t2(a) WHERE a IS NOT NULL;

Added test/intreal.test.

            1  +# 2019-05-03
            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  +# Tests to exercise the MEM_IntReal representation of Mem objects.
           12  +#
           13  +set testdir [file dirname $argv0]
           14  +source $testdir/tester.tcl
           15  +set ::testprefix intreal
           16  +
           17  +sqlite3_create_function db
           18  +do_execsql_test 100 {
           19  +  SELECT intreal(5);
           20  +} {5.0}
           21  +do_execsql_test 110 {
           22  +  SELECT intreal(5)=5, 6=intreal(6);
           23  +} {1 1}
           24  +do_execsql_test 120 {
           25  +  SELECT intreal(7)=7.0, 8.0=intreal(8);
           26  +} {1 1}
           27  +do_execsql_test 130 {
           28  +  SELECT typeof(intreal(9));
           29  +} {real}
           30  +do_execsql_test 140 {
           31  +  SELECT 'a'||intreal(11)||'z';
           32  +} {a11.0z}
           33  +
           34  +do_execsql_test 150 {
           35  +  SELECT max(1.0,intreal(2),3.0), max(1,intreal(2),3);
           36  +} {3.0 3}
           37  +do_execsql_test 160 {
           38  +  SELECT max(1.0,intreal(4),3.0), max(1,intreal(4),3);
           39  +} {4.0 4.0}
           40  +do_execsql_test 170 {
           41  +  SELECT max(1.0,intreal(2),intreal(3),4.0),
           42  +         max(1,intreal(2),intreal(3),4);
           43  +} {4.0 4}
           44  +do_execsql_test 180 {
           45  +  SELECT max(1.0,intreal(5),intreal(3),4.0),
           46  +         max(1,intreal(5),intreal(3),4);
           47  +} {5.0 5.0}
           48  +
           49  +
           50  +
           51  +
           52  +finish_test

Changes to test/like3.test.

   173    173   } {/abc}
   174    174   do_eqp_test like3-5.211 {
   175    175     SELECT x FROM t5b WHERE x GLOB '/a*';
   176    176   } {
   177    177     QUERY PLAN
   178    178     `--SEARCH TABLE t5b USING COVERING INDEX sqlite_autoindex_t5b_1 (x>? AND x<?)
   179    179   }
          180  +
          181  +# 2019-05-01
          182  +# another case of the above reported on the mailing list by Manuel Rigger.
          183  +#
          184  +do_execsql_test like3-5.300 {
          185  +  CREATE TABLE t5c (c0 REAL);
          186  +  CREATE INDEX t5c_0 ON t5c(c0 COLLATE NOCASE);
          187  +  INSERT INTO t5c(rowid, c0) VALUES (99,'+/');
          188  +  SELECT * FROM t5c WHERE (c0 LIKE '+/');
          189  +} {+/}
          190  +
          191  +# 2019-05-08
          192  +# Yet another case for the above from Manuel Rigger.
          193  +#
          194  +do_execsql_test like3-5.400 {
          195  +  DROP TABLE IF EXISTS t0;
          196  +  CREATE TABLE t0(c0 INT UNIQUE COLLATE NOCASE);
          197  +  INSERT INTO t0(c0) VALUES ('./');
          198  +  SELECT * FROM t0 WHERE t0.c0 LIKE './';
          199  +} {./}
          200  +
   180    201   
   181    202   # 2019-02-27
   182    203   # Verify that the LIKE optimization works with an ESCAPE clause when
   183    204   # using PRAGMA case_sensitive_like=ON.
   184    205   #
          206  +ifcapable !icu {
   185    207   do_execsql_test like3-6.100 {
   186    208     DROP TABLE IF EXISTS t1;
   187    209     CREATE TABLE t1(path TEXT COLLATE nocase PRIMARY KEY,a,b,c) WITHOUT ROWID;
   188    210   }
   189    211   do_eqp_test like3-6.110 {
   190    212     SELECT * FROM t1 WHERE path LIKE 'a%';
   191    213   } {
................................................................................
   224    246     `--SEARCH TABLE t2 USING INDEX t2path2 (path>? AND path<?)
   225    247   }
   226    248   do_eqp_test like3-6.240 {
   227    249     SELECT * FROM t2 WHERE path LIKE 'a%' ESCAPE '_';
   228    250   } {
   229    251     QUERY PLAN
   230    252     `--SEARCH TABLE t2 USING INDEX t2path2 (path>? AND path<?)
          253  +}
   231    254   }
   232    255   
   233    256   finish_test

Changes to test/oserror.test.

    48     48   #
    49     49   # The xOpen() method of the unix VFS calls getcwd() as well as open().
    50     50   # Although this does not appear to be documented in the man page, on OSX
    51     51   # a call to getcwd() may fail if there are no free file descriptors. So
    52     52   # an error may be reported for either open() or getcwd() here.
    53     53   #
    54     54   if {![clang_sanitize_address]} {
           55  +  unset -nocomplain rc
           56  +  unset -nocomplain nOpen
           57  +  set nOpen 20000
    55     58     do_test 1.1.1 {
    56     59       set ::log [list]
    57         -    list [catch {
    58         -      for {set i 0} {$i < 20000} {incr i} { sqlite3 dbh_$i test.db -readonly 1 }
    59         -    } msg] $msg
    60         -  } {1 {unable to open database file}}
           60  +    set ::rc [catch {
           61  +      for {set i 0} {$i < $::nOpen} {incr i} { sqlite3 dbh_$i test.db -readonly 1 }
           62  +    } msg]
           63  +    if {$::rc==0} {
           64  +      # Some system (ex: Debian) are able to create 20000+ file descriptiors
           65  +      # such systems will not fail here
           66  +      set x ok
           67  +    } elseif {$::rc==1 && $msg=="unable to open database file"} {
           68  +      set x ok
           69  +    } else {
           70  +      set x [list $::rc $msg]
           71  +    }
           72  +  } {ok}
    61     73     do_test 1.1.2 {
    62         -    catch { for {set i 0} {$i < 20000} {incr i} { dbh_$i close } }
    63         -  } {1}
    64         -  do_re_test 1.1.3 { 
    65         -    lindex $::log 0 
    66         -  } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - }
           74  +    catch { for {set i 0} {$i < $::nOpen} {incr i} { dbh_$i close } }
           75  +  } $::rc
           76  +  if {$rc} {
           77  +    do_re_test 1.1.3 { 
           78  +      lindex $::log 0 
           79  +    } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - }
           80  +  }
    67     81   }
    68     82   
    69     83   
    70     84   # Test a failure in open() due to the path being a directory.
    71     85   #
    72     86   do_test 1.2.1 {
    73     87     file mkdir dir.db

Added test/recover.test.

            1  +# 2019 April 23
            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  +# Test the shell tool ".ar" command.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +set testprefix recover
           18  +
           19  +ifcapable !vtab {
           20  +  finish_test; return
           21  +}
           22  +set CLI [test_find_cli]
           23  +
           24  +proc compare_result {db1 db2 sql} {
           25  +  set r1 [$db1 eval $sql]
           26  +  set r2 [$db2 eval $sql]
           27  +  if {$r1 != $r2} {
           28  +  puts "r1: $r1"
           29  +  puts "r2: $r2"
           30  +    error "mismatch for $sql"
           31  +  }
           32  +  return ""
           33  +}
           34  +
           35  +proc compare_dbs {db1 db2} {
           36  +  compare_result $db1 $db2 "SELECT sql FROM sqlite_master ORDER BY 1"
           37  +  foreach tbl [$db1 eval {SELECT name FROM sqlite_master WHERE type='table'}] {
           38  +    compare_result $db1 $db2 "SELECT * FROM $tbl"
           39  +  }
           40  +}
           41  +
           42  +proc do_recover_test {tn {tsql {}} {res {}}} {
           43  +  set fd [open "|$::CLI test.db .recover"]
           44  +  fconfigure $fd -encoding binary
           45  +  fconfigure $fd -translation binary
           46  +  set sql [read $fd]
           47  +  close $fd
           48  +
           49  +  forcedelete test.db2
           50  +  sqlite3 db2 test.db2
           51  +  execsql $sql db2
           52  +  if {$tsql==""} {
           53  +    uplevel [list do_test $tn [list compare_dbs db db2] {}]
           54  +  } else {
           55  +    uplevel [list do_execsql_test -db db2 $tn $tsql $res]
           56  +  }
           57  +  db2 close
           58  +}
           59  +
           60  +set doc {
           61  +  hello
           62  +  world
           63  +}
           64  +do_execsql_test 1.1.1 {
           65  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
           66  +  INSERT INTO t1 VALUES(1, 4, X'1234567800');
           67  +  INSERT INTO t1 VALUES(2, 'test', 8.1);
           68  +  INSERT INTO t1 VALUES(3, $doc, 8.4);
           69  +}
           70  +do_recover_test 1.1.2
           71  +
           72  +do_execsql_test 1.2.1 "
           73  +  DELETE FROM t1;
           74  +  INSERT INTO t1 VALUES(13, 'hello\r\nworld', 13);
           75  +"
           76  +do_recover_test 1.2.2
           77  +
           78  +do_execsql_test 1.3.1 "
           79  +  CREATE TABLE t2(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c);
           80  +  INSERT INTO t2 VALUES(NULL, 1, 2);
           81  +  INSERT INTO t2 VALUES(NULL, 3, 4);
           82  +  INSERT INTO t2 VALUES(NULL, 5, 6);
           83  +  CREATE TABLE t3(i INTEGER PRIMARY KEY AUTOINCREMENT, b, c);
           84  +  INSERT INTO t3 VALUES(NULL, 1, 2);
           85  +  INSERT INTO t3 VALUES(NULL, 3, 4);
           86  +  INSERT INTO t3 VALUES(NULL, 5, 6);
           87  +  DELETE FROM t2;
           88  +"
           89  +do_recover_test 1.3.2
           90  +
           91  +#-------------------------------------------------------------------------
           92  +reset_db
           93  +do_execsql_test 2.1.0 {
           94  +  PRAGMA auto_vacuum = 0;
           95  +  CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID;
           96  +  INSERT INTO t1 VALUES(1, 2, 3);
           97  +  INSERT INTO t1 VALUES(4, 5, 6);
           98  +  INSERT INTO t1 VALUES(7, 8, 9);
           99  +}
          100  +
          101  +do_recover_test 2.1.1
          102  +
          103  +do_execsql_test 2.2.0 {
          104  +  PRAGMA writable_schema = 1;
          105  +  DELETE FROM sqlite_master WHERE name='t1';
          106  +}
          107  +do_recover_test 2.2.1 {
          108  +  SELECT name FROM sqlite_master
          109  +} {lost_and_found}
          110  +
          111  +do_execsql_test 2.3.0 {
          112  +  CREATE TABLE lost_and_found(a, b, c);
          113  +}
          114  +do_recover_test 2.3.1 {
          115  +  SELECT name FROM sqlite_master
          116  +} {lost_and_found lost_and_found_0}
          117  +
          118  +do_execsql_test 2.4.0 {
          119  +  CREATE TABLE lost_and_found_0(a, b, c);
          120  +}
          121  +do_recover_test 2.4.1 {
          122  +  SELECT name FROM sqlite_master;
          123  +  SELECT * FROM lost_and_found_1;
          124  +} {lost_and_found lost_and_found_0 lost_and_found_1
          125  +  2 2 3 {} 2 3 1
          126  +  2 2 3 {} 5 6 4
          127  +  2 2 3 {} 8 9 7
          128  +}
          129  +
          130  +#-------------------------------------------------------------------------
          131  +reset_db
          132  +do_recover_test 3.0
          133  +
          134  +finish_test

Changes to test/reindex.test.

    11     11   # This file implements regression tests for SQLite library.
    12     12   # This file implements tests for the REINDEX command.
    13     13   #
    14     14   # $Id: reindex.test,v 1.4 2008/07/12 14:52:20 drh Exp $
    15     15   
    16     16   set testdir [file dirname $argv0]
    17     17   source $testdir/tester.tcl
           18  +set testprefix reindex
    18     19   
    19     20   # There is nothing to test if REINDEX is disable for this build.
    20     21   #
    21     22   ifcapable {!reindex} {
    22     23     finish_test
    23     24     return
    24     25   }
................................................................................
   163    164       REINDEX;
   164    165     } db2
   165    166   } {1 {no such collation sequence: c2}}
   166    167   
   167    168   do_test reindex-3.99 {
   168    169     db2 close
   169    170   } {}
          171  +
          172  +#-------------------------------------------------------------------------
          173  +foreach {tn wo} {1 "" 2 "WITHOUT ROWID"} {
          174  +  reset_db
          175  +  eval [string map [list %without_rowid% $wo] {
          176  +    do_execsql_test 4.$tn.0 {
          177  +      CREATE TABLE t0 (
          178  +        c0 INTEGER PRIMARY KEY DESC, 
          179  +        c1 UNIQUE DEFAULT NULL
          180  +      ) %without_rowid% ;
          181  +      INSERT INTO t0(c0) VALUES (1), (2), (3), (4), (5);
          182  +      SELECT c0 FROM t0 WHERE c1 IS NULL ORDER BY 1;
          183  +    } {1 2 3 4 5}
          184  +    
          185  +    do_execsql_test 4.$tn.1 {
          186  +      REINDEX;
          187  +    }
          188  +    
          189  +    do_execsql_test 4.$tn.2 {
          190  +      SELECT c0 FROM t0 WHERE c1 IS NULL ORDER BY 1;
          191  +    } {1 2 3 4 5}
          192  +
          193  +    do_execsql_test 4.$tn.3 {
          194  +      SELECT c0 FROM t0 WHERE c1 IS NULL AND c0 IN (1,2,3,4,5);
          195  +    } {1 2 3 4 5}
          196  +
          197  +    do_execsql_test 4.$tn.4 {
          198  +      PRAGMA integrity_check;
          199  +    } {ok}
          200  +  }]
          201  +}
          202  +
          203  +
   170    204   
   171    205   finish_test

Changes to test/releasetest.tcl.

   497    497     if {!$rc} {
   498    498       if {[info exists ::env(TCLSH_CMD)]} {
   499    499         set savedEnv(TCLSH_CMD) $::env(TCLSH_CMD)
   500    500       } else {
   501    501         unset -nocomplain savedEnv(TCLSH_CMD)
   502    502       }
   503    503       set ::env(TCLSH_CMD) [file nativename [info nameofexecutable]]
   504         -    set rc [catch [makeCommand $testtarget $makeOpts $cflags $opts]]
          504  +
          505  +    # Create a file called "makecommand.sh" containing the text of
          506  +    # the make command line.
          507  +    catch {
          508  +      set cmd [makeCommand $testtarget $makeOpts $cflags $opts]
          509  +      set fd [open makecommand.sh w]
          510  +      foreach e $cmd { 
          511  +        if {[string first " " $e]>=0} {
          512  +          puts -nonewline $fd "\"$e\""
          513  +        } else {
          514  +          puts -nonewline $fd $e
          515  +        }
          516  +        puts -nonewline $fd " "
          517  +      }
          518  +      puts $fd ""
          519  +      close $fd
          520  +    } msg
          521  +
          522  +    # Run the make command.
          523  +    set rc [catch {trace_cmd exec {*}$cmd >>& test.log} msg]
   505    524       if {[info exists savedEnv(TCLSH_CMD)]} {
   506    525         set ::env(TCLSH_CMD) $savedEnv(TCLSH_CMD)
   507    526       } else {
   508    527         unset -nocomplain ::env(TCLSH_CMD)
   509    528       }
   510    529     }
   511    530   
................................................................................
   733    752     lappend result >& test.log
   734    753   }
   735    754   
   736    755   # The following procedure returns the "make" command to be executed for the
   737    756   # specified targets, compiler flags, and options.
   738    757   #
   739    758   proc makeCommand { targets makeOpts cflags opts } {
   740         -  set result [list trace_cmd exec]
          759  +  set result [list]
   741    760     if {$::MSVC} {
   742    761       set nmakeDir [file nativename $::SRCDIR]
   743    762       set nmakeFile [file nativename [file join $nmakeDir Makefile.msc]]
   744    763       lappend result nmake /f $nmakeFile TOP=$nmakeDir
   745    764       set tclDir [file nativename [file normalize \
   746    765           [file dirname [file dirname [info nameofexecutable]]]]]
   747    766       lappend result "TCLDIR=$tclDir"
................................................................................
   754    773     foreach makeOpt $makeOpts {
   755    774       lappend result $makeOpt
   756    775     }
   757    776     lappend result clean
   758    777     foreach target $targets {
   759    778       lappend result $target
   760    779     }
   761         -  lappend result CFLAGS=$cflags OPTS=$opts >>& test.log
          780  +  lappend result CFLAGS=$cflags OPTS=$opts
   762    781   }
   763    782   
   764    783   # The following procedure prints its arguments if ::TRACE is true.
   765    784   # And it executes the command of its arguments in the calling context
   766    785   # if ::DRYRUN is false.
   767    786   #
   768    787   proc trace_cmd {args} {

Changes to test/rowid.test.

   655    655   do_test rowid-11.3 {
   656    656     execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc'}
   657    657   } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
   658    658   do_test rowid-11.4 {
   659    659     execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc'}
   660    660   } {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
   661    661   
          662  +do_test rowid-11.asc.1 {
          663  +  execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc' ORDER BY 1 ASC}
          664  +} {}
          665  +do_test rowid-11.asc.2 {
          666  +  execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc' ORDER BY 1 ASC}
          667  +} {}
          668  +do_test rowid-11.asc.3 {
          669  +  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc' ORDER BY 1 ASC}
          670  +} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
          671  +do_test rowid-11.asc.4 {
          672  +  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc' ORDER BY 1 ASC}
          673  +} {1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8}
          674  +
          675  +do_test rowid-11.desc.1 {
          676  +  execsql {SELECT rowid, a FROM t5 WHERE rowid>'abc' ORDER BY 1 DESC}
          677  +} {}
          678  +do_test rowid-11.desc.2 {
          679  +  execsql {SELECT rowid, a FROM t5 WHERE rowid>='abc' ORDER BY 1 DESC}
          680  +} {}
          681  +do_test rowid-11.desc.3 {
          682  +  execsql {SELECT rowid, a FROM t5 WHERE rowid<'abc' ORDER BY 1 DESC}
          683  +} {8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1}
          684  +do_test rowid-11.desc.4 {
          685  +  execsql {SELECT rowid, a FROM t5 WHERE rowid<='abc' ORDER BY 1 DESC}
          686  +} {8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1}
          687  +
   662    688   # Test the automatic generation of rowids when the table already contains
   663    689   # a rowid with the maximum value.
   664    690   #
   665    691   # Once the maximum rowid is taken, rowids are normally chosen at
   666    692   # random.  By by reseting the random number generator, we can cause
   667    693   # the rowid guessing loop to collide with prior rowids, and test the
   668    694   # loop out to its limit of 100 iterations.  After 100 collisions, the
................................................................................
   714    740   db function addrow rowid_addrow_func
   715    741   do_execsql_test rowid-13.1 {
   716    742     CREATE TABLE t13(x);
   717    743     INSERT INTO t13(rowid,x) VALUES(1234,5);
   718    744     SELECT rowid, x, addrow(rowid+1000), '|' FROM t13 LIMIT 3;
   719    745     SELECT last_insert_rowid();
   720    746   } {1234 5 2234 | 2234 4990756 3234 | 3234 10458756 4234 | 4234}
          747  +
          748  +#-------------------------------------------------------------------------
          749  +do_execsql_test rowid-14.0 {
          750  +  CREATE TABLE t14(x INTEGER PRIMARY KEY);
          751  +  INSERT INTO t14(x) VALUES (100);
          752  +}
          753  +do_execsql_test rowid-14.1 {
          754  +  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid ASC;
          755  +} {100}
          756  +do_execsql_test rowid-14.2 {
          757  +  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid DESC;
          758  +} {100}
          759  +
          760  +do_execsql_test rowid-14.3 {
          761  +  DELETE FROM t14;
          762  +  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid ASC;
          763  +} {}
          764  +do_execsql_test rowid-14.4 {
          765  +  SELECT * FROM t14 WHERE x < 'a' ORDER BY rowid DESC;
          766  +} {}
          767  +
          768  +reset_db
          769  +do_execsql_test rowid-15.0 {
          770  +  PRAGMA reverse_unordered_selects=true;
          771  +  CREATE TABLE t1 (c0, c1);
          772  +  CREATE TABLE t2 (c0 INT UNIQUE);
          773  +  INSERT INTO t1(c0, c1) VALUES (0, 0), (0, NULL);
          774  +  INSERT INTO t2(c0) VALUES (1);
          775  +}
          776  +
          777  +do_execsql_test rowid-15.1 {
          778  +  SELECT t2.c0, t1.c1 FROM t1, t2 
          779  +  WHERE (t2.rowid <= 'a') OR (t1.c0 <= t2.c0) LIMIT 100
          780  +} {1 {} 1 0}
          781  +
          782  +do_execsql_test rowid-15.2 {
          783  +  SELECT 1, NULL INTERSECT SELECT * FROM (
          784  +      SELECT t2.c0, t1.c1 FROM t1, t2
          785  +      WHERE ((t2.rowid <= 'a')) OR (t1.c0 <= t2.c0) ORDER BY 'a' DESC LIMIT 100
          786  +  );
          787  +} {1 {}}
          788  +
   721    789   
   722    790   finish_test

Changes to test/select3.test.

   256    256     }
   257    257   } {real}
   258    258   do_test select3-8.2 {
   259    259     execsql {
   260    260       SELECT typeof(sum(a3)) FROM a GROUP BY a1;
   261    261     }
   262    262   } {real}
          263  +
          264  +# 2019-05-09 ticket https://www.sqlite.org/src/tktview/6c1d3febc00b22d457c7
          265  +#
          266  +unset -nocomplain x
          267  +foreach {id x} {
          268  +  100 127
          269  +  101 128
          270  +  102 -127
          271  +  103 -128
          272  +  104 -129
          273  +  110 32767
          274  +  111 32768
          275  +  112 -32767
          276  +  113 -32768
          277  +  114 -32769
          278  +  120 2147483647
          279  +  121 2147483648
          280  +  122 -2147483647
          281  +  123 -2147483648
          282  +  124 -2147483649
          283  +  130 140737488355327
          284  +  131 140737488355328
          285  +  132 -140737488355327
          286  +  133 -140737488355328
          287  +  134 -140737488355329
          288  +  140 9223372036854775807
          289  +  141 -9223372036854775807
          290  +  142 -9223372036854775808
          291  +  143 9223372036854775806
          292  +  144 9223372036854775805
          293  +  145 -9223372036854775806
          294  +  146 -9223372036854775805
          295  +
          296  +} {
          297  +  set x [expr {$x+0}]
          298  +  do_execsql_test select3-8.$id {
          299  +     DROP TABLE IF EXISTS t1;
          300  +     CREATE TABLE t1 (c0, c1 REAL PRIMARY KEY);
          301  +     INSERT INTO t1(c0, c1) VALUES (0, $x), (0, 0);
          302  +     UPDATE t1 SET c0 = NULL;
          303  +     UPDATE OR REPLACE t1 SET c1 = 1;
          304  +     SELECT DISTINCT * FROM t1 WHERE (t1.c0 IS NULL);
          305  +     PRAGMA integrity_check;
          306  +  } {{} 1.0 ok}
          307  +}
   263    308   
   264    309   finish_test

Changes to test/view.test.

   696    696   set res [list {SQLITE_DELETE sqlite_stat1 {} main {}}]
   697    697   ifcapable stat4 { lappend res {SQLITE_DELETE sqlite_stat4 {} main {}} }
   698    698   do_test view-25.2 {
   699    699     set log ""
   700    700     db eval {DROP TABLE t25;}
   701    701     set log
   702    702   } $res
          703  +
          704  +#-------------------------------------------------------------------------
          705  +do_execsql_test view-26.0 {
          706  +  CREATE TABLE t16(a, b, c UNIQUE);
          707  +  INSERT INTO t16 VALUES(1, 1, 1);
          708  +  INSERT INTO t16 VALUES(2, 2, 2);
          709  +  INSERT INTO t16 VALUES(3, 3, 3);
          710  +  CREATE VIEW v16 AS SELECT max(a) AS mx, min(b) AS mn FROM t16 GROUP BY c;
          711  +
          712  +  SELECT * FROM v16 AS one, v16 AS two WHERE one.mx=1;
          713  +} {
          714  +  1 1 1 1 
          715  +  1 1 2 2 
          716  +  1 1 3 3
          717  +}
   703    718   
   704    719   finish_test

Changes to test/wapptest.tcl.

    16     16   #   G(keep)     - Boolean. True to delete no files after each test.
    17     17   #   G(msvc)     - Boolean. True to use MSVC as the compiler.
    18     18   #   G(tcl)      - Use Tcl from this directory for builds.
    19     19   #   G(jobs)     - How many sub-processes to run simultaneously.
    20     20   #
    21     21   set G(platform) $::tcl_platform(os)-$::tcl_platform(machine)
    22     22   set G(test)     Normal
    23         -set G(keep)     0
           23  +set G(keep)     1
    24     24   set G(msvc)     0
    25     25   set G(tcl)      [::tcl::pkgconfig get libdir,install]
    26     26   set G(jobs)     3
    27     27   set G(debug)    0
           28  +
           29  +set G(noui)     0
           30  +set G(stdout)   0
           31  +
    28     32   
    29     33   proc wapptest_init {} {
    30     34     global G
    31     35   
    32         -  set lSave [list platform test keep msvc tcl jobs debug] 
           36  +  set lSave [list platform test keep msvc tcl jobs debug noui stdout] 
    33     37     foreach k $lSave { set A($k) $G($k) }
    34     38     array unset G
    35     39     foreach k $lSave { set G($k) $A($k) }
    36     40   
    37     41     # The root of the SQLite source tree.
    38     42     set G(srcdir)   [file dirname [file dirname [info script]]]
    39     43   
    40         -  # releasetest.tcl script
    41         -  set G(releaseTest) [file join [file dirname [info script]] releasetest.tcl]
    42         -
    43     44     set G(sqlite_version) "unknown"
    44     45   
    45     46     # Either "config", "running" or "stopped":
    46     47     set G(state) "config"
    47     48   
    48     49     set G(hostname) "(unknown host)"
    49     50     catch { set G(hostname) [exec hostname] } 
    50     51     set G(host) $G(hostname)
    51     52     append G(host) " $::tcl_platform(os) $::tcl_platform(osVersion)"
    52     53     append G(host) " $::tcl_platform(machine) $::tcl_platform(byteOrder)"
    53     54   }
    54     55   
    55         -# Check to see if there are uncommitted changes in the SQLite source
    56         -# directory. Return true if there are, or false otherwise.
    57         -#
    58         -proc check_uncommitted {} {
           56  +proc wapptest_run {} {
    59     57     global G
    60         -  set ret 0
    61         -  set pwd [pwd]
    62         -  cd $G(srcdir)
    63         -  if {[catch {exec fossil changes} res]==0 && [string trim $res]!=""} {
    64         -    set ret 1
           58  +  set_test_array
           59  +  set G(state) "running"
           60  +
           61  +  wapptest_openlog
           62  +
           63  +  wapptest_output "Running the following for $G(platform). $G(jobs) jobs."
           64  +  foreach t $G(test_array) {
           65  +    set config [dict get $t config]
           66  +    set target [dict get $t target]
           67  +    wapptest_output [format "    %-25s%s" $config $target]
    65     68     }
    66         -  cd $pwd
    67         -  return $ret
           69  +  wapptest_output [string repeat * 70]
    68     70   }
    69     71   
           72  +# Generate the text for the box at the top of the UI. The current SQLite
           73  +# version, according to fossil, along with a warning if there are 
           74  +# uncommitted changes in the checkout.
           75  +#
    70     76   proc generate_fossil_info {} {
    71     77     global G
    72     78     set pwd [pwd]
    73     79     cd $G(srcdir)
    74         -  if {[catch {exec fossil info}    r1]} return
    75         -  if {[catch {exec fossil changes} r2]} return
           80  +  set rc [catch {
           81  +    set r1 [exec fossil info]
           82  +    set r2 [exec fossil changes]
           83  +  }]
    76     84     cd $pwd
           85  +  if {$rc} return
    77     86   
    78     87     foreach line [split $r1 "\n"] {
    79     88       if {[regexp {^checkout: *(.*)$} $line -> co]} {
    80     89         wapp-trim { <br> %html($co) }
    81     90       }
    82     91     }
    83     92   
................................................................................
   204    213       set G(test.$name.errmsg) "Test did not complete"
   205    214       if {[file readable core]} {
   206    215         append G(test.$name.errmsg) " - core file exists"
   207    216       }
   208    217     }
   209    218   }
   210    219   
          220  +proc wapptest_output {str} {
          221  +  global G
          222  +  if {$G(stdout)} { puts $str }
          223  +  if {[info exists G(log)]} { 
          224  +    puts $G(log) $str 
          225  +    flush $G(log)
          226  +  }
          227  +}
          228  +proc wapptest_openlog {} {
          229  +  global G
          230  +  set G(log) [open wapptest-out.txt w+]
          231  +}
          232  +proc wapptest_closelog {} {
          233  +  global G
          234  +  close $G(log)
          235  +  unset G(log)
          236  +}
          237  +
          238  +proc format_seconds {seconds} {
          239  +  set min [format %.2d [expr ($seconds / 60) % 60]]
          240  +  set  hr [format %.2d [expr $seconds / 3600]]
          241  +  set sec [format %.2d [expr $seconds % 60]]
          242  +  return "$hr:$min:$sec"
          243  +}
          244  +
          245  +# This command is invoked once a slave process has finished running its
          246  +# tests, successfully or otherwise. Parameter $name is the name of the 
          247  +# test, $rc the exit code returned by the slave process.
          248  +#
   211    249   proc slave_test_done {name rc} {
   212    250     global G
   213    251     set G(test.$name.done) [clock seconds]
   214    252     set G(test.$name.nError) 0
   215    253     set G(test.$name.nTest) 0
   216    254     set G(test.$name.errmsg) ""
   217    255     if {$rc} {
   218    256       incr G(test.$name.nError)
   219    257     }
   220    258     if {[file exists $G(test.$name.log)]} {
   221    259       count_tests_and_errors $name $G(test.$name.log)
   222    260     }
          261  +
          262  +  # If the "keep files" checkbox is clear, delete all files except for
          263  +  # the executables and test logs. And any core file that is present.
          264  +  if {$G(keep)==0} {
          265  +    set keeplist {
          266  +      testfixture testfixture.exe
          267  +      sqlite3 sqlite3.exe
          268  +      test.log test-out.txt
          269  +      core
          270  +      wapptest_make.sh
          271  +      wapptest_configure.sh
          272  +      wapptest_run.tcl
          273  +    }
          274  +    foreach f [glob -nocomplain [file join $G(test.$name.dir) *]] {
          275  +      set t [file tail $f]
          276  +      if {[lsearch $keeplist $t]<0} {
          277  +        catch { file delete -force $f }
          278  +      }
          279  +    }
          280  +  }
          281  +
          282  +  # Format a message regarding the success or failure of hte test.
          283  +  set t [format_seconds [expr $G(test.$name.done) - $G(test.$name.start)]]
          284  +  set res "OK"
          285  +  if {$G(test.$name.nError)} { set res "FAILED" }
          286  +  set dots [string repeat . [expr 60 - [string length $name]]]
          287  +  set msg "$name $dots $res ($t)"
          288  +
          289  +  wapptest_output $msg
          290  +  if {[info exists G(test.$name.errmsg)] && $G(test.$name.errmsg)!=""} {
          291  +    wapptest_output "    $G(test.$config.errmsg)"
          292  +  }
   223    293   }
   224    294   
          295  +# This is a fileevent callback invoked each time a file-descriptor that
          296  +# connects this process to a slave process is readable.
          297  +#
   225    298   proc slave_fileevent {name} {
   226    299     global G
   227    300     set fd $G(test.$name.channel)
   228    301   
   229    302     if {[eof $fd]} {
   230    303       fconfigure $fd -blocking 1
   231    304       set rc [catch { close $fd }]
................................................................................
   234    307     } else {
   235    308       set line [gets $fd]
   236    309       if {[string trim $line] != ""} { puts "Trace   : $name - \"$line\"" }
   237    310     }
   238    311   
   239    312     do_some_stuff
   240    313   }
          314  +
          315  +# Return the contents of the "slave script" - the script run by slave 
          316  +# processes to actually perform the test. It does two things:
          317  +#
          318  +#   1. Reads and [exec]s the contents of file wapptest_configure.sh.
          319  +#   2. Reads and [exec]s the contents of file wapptest_make.sh.
          320  +#
          321  +# Step 1 is omitted if the test uses MSVC (which does not use configure).
          322  +#
          323  +proc wapptest_slave_script {} {
          324  +  global G
          325  +  set res {
          326  +    proc readfile {filename} {
          327  +      set fd [open $filename]
          328  +      set data [read $fd]
          329  +      close $fd
          330  +      return $data
          331  +    }
          332  +  }
          333  +
          334  +  if {$G(msvc)==0} { 
          335  +    append res {
          336  +      set cfg  [readfile wapptest_configure.sh]
          337  +      set rc [catch { exec {*}$cfg >& test.log } msg]
          338  +      if {$rc==0} {
          339  +        set make [readfile wapptest_make.sh]
          340  +        set rc [catch { exec {*}$make >>& test.log }]
          341  +      }
          342  +    } 
          343  +  } else { 
          344  +    append res {
          345  +      set make [readfile wapptest_make.sh]
          346  +      set rc [catch { exec {*}$make >>& test.log }]
          347  +    }
          348  +  }
          349  +
          350  +  append res { exit $rc }
          351  +
          352  +  set res
          353  +}
          354  +
          355  +
          356  +# Launch a slave process to run a test.
          357  +#
          358  +proc slave_launch {