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

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

Overview
Comment:Merge all the latest trunk enhancements into the reuse-schema branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | reuse-schema
Files: files | file ages | folders
SHA3-256: 018f3199b9ba043fbbf88ec46b4fa16728f6507e000f4813ed346aaf6665add6
User & Date: drh 2019-05-14 22:25:26
Wiki:reuse-schema
Context
2019-05-24
13:04
Merge recent enhancements from trunk. check-in: ff9f3e09 user: drh tags: reuse-schema
2019-05-14
22:25
Merge all the latest trunk enhancements into the reuse-schema branch. check-in: 018f3199 user: drh tags: reuse-schema
20:25
Fix a problem with the fix for [9cf6c9bb51] (commit [658b84d7]) that could cause a cursor to be left in an invalid state following a (rowid < text-value) search. check-in: bc7d2c16 user: dan tags: trunk
2019-04-15
15:35
Merge recent changes from trunk into reuse-schema check-in: 5c6c9e7f user: drh tags: reuse-schema
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes 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.

  5812   5812     INSERT INTO t1_docsize VALUES(3,X'030003');
  5813   5813   } {}
  5814   5814   
  5815   5815   do_catchsql_test 44.2 {
  5816   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  +  INSERT INTO t1(t1) VALUES('integrity-check');
         6422  +} {1 {database disk image is malformed}}
         6423  +
         6424  +do_catchsql_test 47.2 {
         6425  +  SELECT count(*) FROM (
  6421   6426     SELECT snippet(t1, -1, '.', '..', '[', 50), 
  6422   6427            highlight(t1, 2, '[', ']') FROM t1('g h') 
  6423         -  WHERE rank MATCH 'bm25(1.0, 1.0)' ORDER BY rank;
  6424         -} {1 {database disk image is malformed}}
         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           }
         2344  +        if( p->rc==SQLITE_OK ){
  2123   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           }
         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 ){
  2226   2470           p->rc = prepareFreeAndCollectError(p->dbRbu, &pIter->pSelect, pz,
  2227   2471               sqlite3_mprintf(
  2228         -              "SELECT %s,%s rbu_control%s FROM '%q'%s", 
         2472  +                "SELECT %s,%s rbu_control%s FROM '%q'%s %s %s %s",
  2229   2473                 zCollist, 
  2230   2474                 (rbuIsVacuum(p) ? "0 AS " : ""),
  2231   2475                 zRbuRowid,
  2232         -              pIter->zDataTbl, zLimit
         2476  +                pIter->zDataTbl, (zStart ? zStart : ""), 
         2477  +                (zOrder ? "ORDER BY" : ""), zOrder,
         2478  +                zLimit
  2233   2479               )
  2234   2480           );
  2235   2481         }
         2482  +        sqlite3_free(zStart);
         2483  +        sqlite3_free(zOrder);
         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       }
  2242   2491       sqlite3_free(zCollist);
................................................................................
  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   4710             rbuMainlistAdd(p);
  4463         -        }
  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 ){
         4806  +  if( eStage==RBU_STAGE_OAL ){
  4564   4807         sqlite3_int64 nByte = (iRegion+1) * sizeof(char*);
  4565   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 );
  4566   4815         if( apNew==0 ){
  4567   4816           rc = SQLITE_NOMEM;
  4568   4817         }else{
  4569   4818           memset(&apNew[p->nShm], 0, sizeof(char*) * (1 + iRegion - p->nShm));
  4570   4819           p->apShm = apNew;
  4571   4820           p->nShm = iRegion+1;
  4572   4821         }
  4573         -    }
  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         sqlite3BtreeEnterAll(db);
   238    238         rc = sqlite3Init(db, &zErrDyn);
   239    239         sqlite3BtreeLeaveAll(db);
   240    240         assert( zErrDyn==0 || rc!=SQLITE_OK );
   241    241       }
   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.

  1624   1624     assert( gap<=65536 );
  1625   1625     /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size
  1626   1626     ** and the reserved space is zero (the usual value for reserved space)
  1627   1627     ** then the cell content offset of an empty page wants to be 65536.
  1628   1628     ** However, that integer is too large to be stored in a 2-byte unsigned
  1629   1629     ** integer, so a value of 0 is used in its place. */
  1630   1630     top = get2byte(&data[hdr+5]);
  1631         -  assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
         1631  +  assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */
  1632   1632     if( gap>top ){
  1633   1633       if( top==0 && pPage->pBt->usableSize==65536 ){
  1634   1634         top = 65536;
  1635   1635       }else{
  1636   1636         return SQLITE_CORRUPT_PAGE(pPage);
  1637   1637       }
  1638   1638     }
................................................................................
  1921   1921     /* At this point, nFree contains the sum of the offset to the start
  1922   1922     ** of the cell-content area plus the number of free bytes within
  1923   1923     ** the cell-content area. If this is greater than the usable-size
  1924   1924     ** of the page, then the page must be corrupted. This check also
  1925   1925     ** serves to verify that the offset to the start of the cell-content
  1926   1926     ** area, according to the page header, lies within the page.
  1927   1927     */
  1928         -  if( nFree>usableSize ){
         1928  +  if( nFree>usableSize || nFree<iCellFirst ){
  1929   1929       return SQLITE_CORRUPT_PAGE(pPage);
  1930   1930     }
  1931   1931     pPage->nFree = (u16)(nFree - iCellFirst);
  1932   1932     return SQLITE_OK;
  1933   1933   }
  1934   1934   
  1935   1935   /*
................................................................................
  4148   4148         }
  4149   4149         btreeReleaseAllCursorPages(p);
  4150   4150       }
  4151   4151       sqlite3BtreeLeave(pBtree);
  4152   4152     }
  4153   4153     return rc;
  4154   4154   }
         4155  +
         4156  +/*
         4157  +** Set the pBt->nPage field correctly, according to the current
         4158  +** state of the database.  Assume pBt->pPage1 is valid.
         4159  +*/
         4160  +static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
         4161  +  int nPage = get4byte(&pPage1->aData[28]);
         4162  +  testcase( nPage==0 );
         4163  +  if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
         4164  +  testcase( pBt->nPage!=nPage );
         4165  +  pBt->nPage = nPage;
         4166  +}
  4155   4167   
  4156   4168   /*
  4157   4169   ** Rollback the transaction in progress.
  4158   4170   **
  4159   4171   ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped).
  4160   4172   ** Only write cursors are tripped if writeOnly is true but all cursors are
  4161   4173   ** tripped if writeOnly is false.  Any attempt to use
................................................................................
  4194   4206         rc = rc2;
  4195   4207       }
  4196   4208   
  4197   4209       /* The rollback may have destroyed the pPage1->aData value.  So
  4198   4210       ** call btreeGetPage() on page 1 again to make
  4199   4211       ** sure pPage1->aData is set correctly. */
  4200   4212       if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
  4201         -      int nPage = get4byte(28+(u8*)pPage1->aData);
  4202         -      testcase( nPage==0 );
  4203         -      if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
  4204         -      testcase( pBt->nPage!=nPage );
  4205         -      pBt->nPage = nPage;
         4213  +      btreeSetNPage(pBt, pPage1);
  4206   4214         releasePageOne(pPage1);
  4207   4215       }
  4208   4216       assert( countValidCursors(pBt, 1)==0 );
  4209   4217       pBt->inTransaction = TRANS_READ;
  4210   4218       btreeClearHasContent(pBt);
  4211   4219     }
  4212   4220   
................................................................................
  4278   4286         rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
  4279   4287       }
  4280   4288       if( rc==SQLITE_OK ){
  4281   4289         if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
  4282   4290           pBt->nPage = 0;
  4283   4291         }
  4284   4292         rc = newDatabase(pBt);
  4285         -      pBt->nPage = get4byte(28 + pBt->pPage1->aData);
         4293  +      btreeSetNPage(pBt, pBt->pPage1);
  4286   4294   
  4287         -      /* The database size was written into the offset 28 of the header
  4288         -      ** when the transaction started, so we know that the value at offset
  4289         -      ** 28 is nonzero. */
  4290         -      assert( pBt->nPage>0 );
         4295  +      /* pBt->nPage might be zero if the database was corrupt when 
         4296  +      ** the transaction was started. Otherwise, it must be at least 1.  */
         4297  +      assert( CORRUPT_DB || pBt->nPage>0 );
  4291   4298       }
  4292   4299       sqlite3BtreeLeave(p);
  4293   4300     }
  4294   4301     return rc;
  4295   4302   }
  4296   4303   
  4297   4304   /*
................................................................................
  5291   5298       int ii;
  5292   5299       for(ii=0; ii<pCur->iPage; ii++){
  5293   5300         assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
  5294   5301       }
  5295   5302       assert( pCur->ix==pCur->pPage->nCell-1 );
  5296   5303       assert( pCur->pPage->leaf );
  5297   5304   #endif
         5305  +    *pRes = 0;
  5298   5306       return SQLITE_OK;
  5299   5307     }
  5300   5308   
  5301   5309     rc = moveToRoot(pCur);
  5302   5310     if( rc==SQLITE_OK ){
  5303   5311       assert( pCur->eState==CURSOR_VALID );
  5304   5312       *pRes = 0;
................................................................................
  7633   7641     for(i=0; i<nOld; i++){
  7634   7642       MemPage *pOld = apOld[i];
  7635   7643       int limit = pOld->nCell;
  7636   7644       u8 *aData = pOld->aData;
  7637   7645       u16 maskPage = pOld->maskPage;
  7638   7646       u8 *piCell = aData + pOld->cellOffset;
  7639   7647       u8 *piEnd;
         7648  +    VVA_ONLY( int nCellAtStart = b.nCell; )
  7640   7649   
  7641   7650       /* Verify that all sibling pages are of the same "type" (table-leaf,
  7642   7651       ** table-interior, index-leaf, or index-interior).
  7643   7652       */
  7644   7653       if( pOld->aData[0]!=apOld[0]->aData[0] ){
  7645   7654         rc = SQLITE_CORRUPT_BKPT;
  7646   7655         goto balance_cleanup;
................................................................................
  7661   7670       ** This must be done in advance.  Once the balance starts, the cell
  7662   7671       ** offset section of the btree page will be overwritten and we will no
  7663   7672       ** long be able to find the cells if a pointer to each cell is not saved
  7664   7673       ** first.
  7665   7674       */
  7666   7675       memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
  7667   7676       if( pOld->nOverflow>0 ){
         7677  +      if( limit<pOld->aiOvfl[0] ){
         7678  +        rc = SQLITE_CORRUPT_BKPT;
         7679  +        goto balance_cleanup;
         7680  +      }
  7668   7681         limit = pOld->aiOvfl[0];
  7669   7682         for(j=0; j<limit; j++){
  7670   7683           b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
  7671   7684           piCell += 2;
  7672   7685           b.nCell++;
  7673   7686         }
  7674   7687         for(k=0; k<pOld->nOverflow; k++){
................................................................................
  7680   7693       piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
  7681   7694       while( piCell<piEnd ){
  7682   7695         assert( b.nCell<nMaxCells );
  7683   7696         b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
  7684   7697         piCell += 2;
  7685   7698         b.nCell++;
  7686   7699       }
         7700  +    assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
  7687   7701   
  7688   7702       cntOld[i] = b.nCell;
  7689   7703       if( i<nOld-1 && !leafData){
  7690   7704         u16 sz = (u16)szNew[i];
  7691   7705         u8 *pTemp;
  7692   7706         assert( b.nCell<nMaxCells );
  7693   7707         b.szCell[b.nCell] = sz;
................................................................................
  7980   7994       int iOld = 0;
  7981   7995   
  7982   7996       for(i=0; i<b.nCell; i++){
  7983   7997         u8 *pCell = b.apCell[i];
  7984   7998         while( i==cntOldNext ){
  7985   7999           iOld++;
  7986   8000           assert( iOld<nNew || iOld<nOld );
         8001  +        assert( iOld>=0 && iOld<NB );
  7987   8002           pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
  7988   8003           cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
  7989   8004         }
  7990   8005         if( i==cntNew[iNew] ){
  7991   8006           pNew = apNew[++iNew];
  7992   8007           if( !leafData ) continue;
  7993   8008         }

Changes to src/build.c.

  1380   1380   **     CREATE TABLE xyz(a,b,c,d,e,PRIMARY KEY('a'),UNIQUE('b','c' COLLATE trim)
  1381   1381   **     CREATE INDEX abc ON xyz('c','d' DESC,'e' COLLATE nocase DESC);
  1382   1382   **
  1383   1383   ** This is goofy.  But to preserve backwards compatibility we continue to
  1384   1384   ** accept it.  This routine does the necessary conversion.  It converts
  1385   1385   ** the expression given in its argument from a TK_STRING into a TK_ID
  1386   1386   ** if the expression is just a TK_STRING with an optional COLLATE clause.
  1387         -** If the epxression is anything other than TK_STRING, the expression is
         1387  +** If the expression is anything other than TK_STRING, the expression is
  1388   1388   ** unchanged.
  1389   1389   */
  1390   1390   static void sqlite3StringToId(Expr *p){
  1391   1391     if( p->op==TK_STRING ){
  1392   1392       p->op = TK_ID;
  1393   1393     }else if( p->op==TK_COLLATE && p->pLeft->op==TK_STRING ){
  1394   1394       p->pLeft->op = TK_ID;
................................................................................
  1777   1777       i16 x = pIdx->aiColumn[i];
  1778   1778       assert( x<pIdx->pTable->nCol );
  1779   1779       wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
  1780   1780     }
  1781   1781     pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
  1782   1782   }
  1783   1783   
  1784         -/* Return true if value x is found any of the first nCol entries of aiCol[]
         1784  +/* Return true if column number x is any of the first nCol entries of aiCol[].
         1785  +** This is used to determine if the column number x appears in any of the
         1786  +** first nCol entries of an index.
  1785   1787   */
  1786   1788   static int hasColumn(const i16 *aiCol, int nCol, int x){
  1787         -  while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
         1789  +  while( nCol-- > 0 ){
         1790  +    assert( aiCol[0]>=0 );
         1791  +    if( x==*(aiCol++) ){
         1792  +      return 1;
         1793  +    }
         1794  +  }
         1795  +  return 0;
         1796  +}
         1797  +
         1798  +/*
         1799  +** Return true if any of the first nKey entries of index pIdx exactly
         1800  +** match the iCol-th entry of pPk.  pPk is always a WITHOUT ROWID
         1801  +** PRIMARY KEY index.  pIdx is an index on the same table.  pIdx may
         1802  +** or may not be the same index as pPk.
         1803  +**
         1804  +** The first nKey entries of pIdx are guaranteed to be ordinary columns,
         1805  +** not a rowid or expression.
         1806  +**
         1807  +** This routine differs from hasColumn() in that both the column and the
         1808  +** collating sequence must match for this routine, but for hasColumn() only
         1809  +** the column name must match.
         1810  +*/
         1811  +static int isDupColumn(Index *pIdx, int nKey, Index *pPk, int iCol){
         1812  +  int i, j;
         1813  +  assert( nKey<=pIdx->nColumn );
         1814  +  assert( iCol<MAX(pPk->nColumn,pPk->nKeyCol) );
         1815  +  assert( pPk->idxType==SQLITE_IDXTYPE_PRIMARYKEY );
         1816  +  assert( pPk->pTable->tabFlags & TF_WithoutRowid );
         1817  +  assert( pPk->pTable==pIdx->pTable );
         1818  +  testcase( pPk==pIdx );
         1819  +  j = pPk->aiColumn[iCol];
         1820  +  assert( j!=XN_ROWID && j!=XN_EXPR );
         1821  +  for(i=0; i<nKey; i++){
         1822  +    assert( pIdx->aiColumn[i]>=0 || j>=0 );
         1823  +    if( pIdx->aiColumn[i]==j 
         1824  +     && sqlite3StrICmp(pIdx->azColl[i], pPk->azColl[iCol])==0
         1825  +    ){
         1826  +      return 1;
         1827  +    }
         1828  +  }
  1788   1829     return 0;
  1789   1830   }
  1790   1831   
  1791   1832   /* Recompute the colNotIdxed field of the Index.
  1792   1833   **
  1793   1834   ** colNotIdxed is a bitmask that has a 0 bit representing each indexed
  1794   1835   ** columns that are within the first 63 columns of the table.  The
................................................................................
  1869   1910     if( pTab->iPKey>=0 ){
  1870   1911       ExprList *pList;
  1871   1912       Token ipkToken;
  1872   1913       sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
  1873   1914       pList = sqlite3ExprListAppend(pParse, 0, 
  1874   1915                     sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
  1875   1916       if( pList==0 ) return;
         1917  +    if( IN_RENAME_OBJECT ){
         1918  +      sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
         1919  +    }
  1876   1920       pList->a[0].sortOrder = pParse->iPkSortOrder;
  1877   1921       assert( pParse->pNewTable==pTab );
         1922  +    pTab->iPKey = -1;
  1878   1923       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
  1879   1924                          SQLITE_IDXTYPE_PRIMARYKEY);
  1880   1925       if( db->mallocFailed || pParse->nErr ) return;
  1881   1926       pPk = sqlite3PrimaryKeyIndex(pTab);
  1882         -    pTab->iPKey = -1;
  1883   1927     }else{
  1884   1928       pPk = sqlite3PrimaryKeyIndex(pTab);
  1885   1929       assert( pPk!=0 );
  1886   1930   
  1887   1931       /*
  1888   1932       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
  1889   1933       ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
  1890   1934       ** code assumes the PRIMARY KEY contains no repeated columns.
  1891   1935       */
  1892   1936       for(i=j=1; i<pPk->nKeyCol; i++){
  1893         -      if( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) ){
         1937  +      if( isDupColumn(pPk, j, pPk, i) ){
  1894   1938           pPk->nColumn--;
  1895   1939         }else{
         1940  +        testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
  1896   1941           pPk->aiColumn[j++] = pPk->aiColumn[i];
  1897   1942         }
  1898   1943       }
  1899   1944       pPk->nKeyCol = j;
  1900   1945     }
  1901   1946     assert( pPk!=0 );
  1902   1947     pPk->isCovering = 1;
................................................................................
  1918   1963     /* Update the in-memory representation of all UNIQUE indices by converting
  1919   1964     ** the final rowid column into one or more columns of the PRIMARY KEY.
  1920   1965     */
  1921   1966     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
  1922   1967       int n;
  1923   1968       if( IsPrimaryKeyIndex(pIdx) ) continue;
  1924   1969       for(i=n=0; i<nPk; i++){
  1925         -      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
         1970  +      if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
         1971  +        testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
         1972  +        n++;
         1973  +      }
  1926   1974       }
  1927   1975       if( n==0 ){
  1928   1976         /* This index is a superset of the primary key */
  1929   1977         pIdx->nColumn = pIdx->nKeyCol;
  1930   1978         continue;
  1931   1979       }
  1932   1980       if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
  1933   1981       for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
  1934         -      if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
         1982  +      if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
         1983  +        testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
  1935   1984           pIdx->aiColumn[j] = pPk->aiColumn[i];
  1936   1985           pIdx->azColl[j] = pPk->azColl[i];
         1986  +        if( pPk->aSortOrder[i] ){
         1987  +          /* See ticket https://www.sqlite.org/src/info/bba7b69f9849b5bf */
         1988  +          pIdx->bAscKeyBug = 1;
         1989  +        }
  1937   1990           j++;
  1938   1991         }
  1939   1992       }
  1940   1993       assert( pIdx->nColumn>=pIdx->nKeyCol+n );
  1941   1994       assert( pIdx->nColumn>=j );
  1942   1995     }
  1943   1996   
................................................................................
  3052   3105                            pIndex->nKeyCol); VdbeCoverage(v);
  3053   3106       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
  3054   3107       sqlite3VdbeJumpHere(v, j2);
  3055   3108     }else{
  3056   3109       addr2 = sqlite3VdbeCurrentAddr(v);
  3057   3110     }
  3058   3111     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
         3112  +  if( !pIndex->bAscKeyBug ){
         3113  +    /* This OP_SeekEnd opcode makes index insert for a REINDEX go much
         3114  +    ** faster by avoiding unnecessary seeks.  But the optimization does
         3115  +    ** not work for UNIQUE constraint indexes on WITHOUT ROWID tables
         3116  +    ** with DESC primary keys, since those indexes have there keys in
         3117  +    ** a different order from the main table.
         3118  +    ** See ticket: https://www.sqlite.org/src/info/bba7b69f9849b5bf
         3119  +    */
  3059   3120     sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
         3121  +  }
  3060   3122     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
  3061   3123     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
  3062   3124     sqlite3ReleaseTempReg(pParse, regRecord);
  3063   3125     sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
  3064   3126     sqlite3VdbeJumpHere(v, addr1);
  3065   3127   
  3066   3128     sqlite3VdbeAddOp1(v, OP_Close, iTab);
................................................................................
  3444   3506     ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
  3445   3507     ** normal tables (when pPk==0) this will be the rowid.
  3446   3508     */
  3447   3509     if( pPk ){
  3448   3510       for(j=0; j<pPk->nKeyCol; j++){
  3449   3511         int x = pPk->aiColumn[j];
  3450   3512         assert( x>=0 );
  3451         -      if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
         3513  +      if( isDupColumn(pIndex, pIndex->nKeyCol, pPk, j) ){
  3452   3514           pIndex->nColumn--; 
  3453   3515         }else{
         3516  +        testcase( hasColumn(pIndex->aiColumn,pIndex->nKeyCol,x) );
  3454   3517           pIndex->aiColumn[i] = x;
  3455   3518           pIndex->azColl[i] = pPk->azColl[j];
  3456   3519           pIndex->aSortOrder[i] = pPk->aSortOrder[j];
  3457   3520           i++;
  3458   3521         }
  3459   3522       }
  3460   3523       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    853       p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr));
   858    854       if( p ){
   859    855         memset(p, 0, sizeof(Expr));
   860    856         p->op = op & 0xff;
   861    857         p->iAgg = -1;
   862         -    }
   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){
          891  +Expr *sqlite3ExprAnd(Parse *pParse, Expr *pLeft, Expr *pRight){
          892  +  sqlite3 *db = pParse->db;
   923    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: {
         4429  +    case TK_AND:
         4430  +    case TK_OR: {
         4431  +      Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
         4432  +      if( pAlt!=pExpr ){
         4433  +        sqlite3ExprIfTrue(pParse, pAlt, dest, jumpIfNull);
         4434  +      }else if( op==TK_AND ){
  4432   4435         int d2 = sqlite3VdbeMakeLabel(pParse);
  4433   4436         testcase( jumpIfNull==0 );
  4434         -      sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
         4437  +        sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,
         4438  +                           jumpIfNull^SQLITE_JUMPIFNULL);
  4435   4439         sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  4436   4440         sqlite3VdbeResolveLabel(v, d2);
  4437         -      break;
  4438         -    }
  4439         -    case TK_OR: {
         4441  +      }else{
  4440   4442         testcase( jumpIfNull==0 );
  4441   4443         sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  4442   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: {
         4601  +    case TK_AND:
         4602  +    case TK_OR: {
         4603  +      Expr *pAlt = sqlite3ExprSimplifiedAndOr(pExpr);
         4604  +      if( pAlt!=pExpr ){
         4605  +        sqlite3ExprIfFalse(pParse, pAlt, dest, jumpIfNull);
         4606  +      }else if( pExpr->op==TK_AND ){
  4599   4607         testcase( jumpIfNull==0 );
  4600   4608         sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  4601   4609         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4602         -      break;
  4603         -    }
  4604         -    case TK_OR: {
         4610  +      }else{
  4605   4611         int d2 = sqlite3VdbeMakeLabel(pParse);
  4606   4612         testcase( jumpIfNull==0 );
  4607         -      sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
         4613  +        sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2,
         4614  +                          jumpIfNull^SQLITE_JUMPIFNULL);
  4608   4615         sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  4609   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   /*
................................................................................
   839    843   #ifdef SQLITE_TEST
   840    844       sqlite3_like_count++;
   841    845   #endif
   842    846       sqlite3_result_int(context, 0);
   843    847       return;
   844    848     }
   845    849   #endif
   846         -  zB = sqlite3_value_text(argv[0]);
   847         -  zA = sqlite3_value_text(argv[1]);
   848    850   
   849    851     /* Limit the length of the LIKE or GLOB pattern to avoid problems
   850    852     ** of deep recursion and N*N behavior in patternCompare().
   851    853     */
   852    854     nPat = sqlite3_value_bytes(argv[0]);
   853    855     testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
   854    856     testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
   855    857     if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
   856    858       sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
   857    859       return;
   858    860     }
   859         -  assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
   860         -
   861    861     if( argc==3 ){
   862    862       /* The escape character string must consist of a single UTF-8 character.
   863    863       ** Otherwise, return an error.
   864    864       */
   865    865       const unsigned char *zEsc = sqlite3_value_text(argv[2]);
   866    866       if( zEsc==0 ) return;
   867    867       if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
................................................................................
   869    869             "ESCAPE expression must be a single character", -1);
   870    870         return;
   871    871       }
   872    872       escape = sqlite3Utf8Read(&zEsc);
   873    873     }else{
   874    874       escape = pInfo->matchSet;
   875    875     }
          876  +  zB = sqlite3_value_text(argv[0]);
          877  +  zA = sqlite3_value_text(argv[1]);
   876    878     if( zA && zB ){
   877    879   #ifdef SQLITE_TEST
   878    880       sqlite3_like_count++;
   879    881   #endif
   880    882       sqlite3_result_int(context,
   881    883                         patternCompare(zB, zA, pInfo, escape)==SQLITE_MATCH);
   882    884     }
................................................................................
  1794   1796     assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
  1795   1797     if( rc==SQLITE_NOMEM ){
  1796   1798       sqlite3OomFault(db);
  1797   1799     }
  1798   1800   }
  1799   1801   
  1800   1802   /*
  1801         -** Set the LIKEOPT flag on the 2-argument function with the given name.
  1802         -*/
  1803         -static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
  1804         -  FuncDef *pDef;
  1805         -  pDef = sqlite3FindFunction(db, zName, 2, SQLITE_UTF8, 0);
  1806         -  if( ALWAYS(pDef) ){
  1807         -    pDef->funcFlags |= flagVal;
  1808         -  }
  1809         -  pDef = sqlite3FindFunction(db, zName, 3, SQLITE_UTF8, 0);
  1810         -  if( pDef ){
  1811         -    pDef->funcFlags |= flagVal;
  1812         -  }
  1813         -}
  1814         -
  1815         -/*
  1816         -** Register the built-in LIKE and GLOB functions.  The caseSensitive
         1803  +** Re-register the built-in LIKE functions.  The caseSensitive
  1817   1804   ** parameter determines whether or not the LIKE operator is case
  1818         -** sensitive.  GLOB is always case sensitive.
         1805  +** sensitive.
  1819   1806   */
  1820   1807   void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
  1821   1808     struct compareInfo *pInfo;
         1809  +  int flags;
  1822   1810     if( caseSensitive ){
  1823   1811       pInfo = (struct compareInfo*)&likeInfoAlt;
         1812  +    flags = SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE;
  1824   1813     }else{
  1825   1814       pInfo = (struct compareInfo*)&likeInfoNorm;
         1815  +    flags = SQLITE_FUNC_LIKE;
  1826   1816     }
  1827   1817     sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
  1828   1818     sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0, 0, 0);
  1829         -  sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, 
  1830         -      (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0, 0, 0);
  1831         -  setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
  1832         -  setLikeOptFlag(db, "like", 
  1833         -      caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
         1819  +  sqlite3FindFunction(db, "like", 2, SQLITE_UTF8, 0)->funcFlags |= flags;
         1820  +  sqlite3FindFunction(db, "like", 3, SQLITE_UTF8, 0)->funcFlags |= flags;
  1834   1821   }
  1835   1822   
  1836   1823   /*
  1837   1824   ** pExpr points to an expression which implements a function.  If
  1838   1825   ** it is appropriate to apply the LIKE optimization to that function
  1839   1826   ** then set aWc[0] through aWc[2] to the wildcard characters and the
  1840   1827   ** 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.

  4128   4128       */
  4129   4129       case SQLITE_TESTCTRL_PARSER_COVERAGE: {
  4130   4130         FILE *out = va_arg(ap, FILE*);
  4131   4131         if( sqlite3ParserCoverage(out) ) rc = SQLITE_ERROR;
  4132   4132         break;
  4133   4133       }
  4134   4134   #endif /* defined(YYCOVERAGE) */
         4135  +
         4136  +    /*  sqlite3_test_control(SQLITE_TESTCTRL_RESULT_INTREAL, sqlite3_context*);
         4137  +    **
         4138  +    ** This test-control causes the most recent sqlite3_result_int64() value
         4139  +    ** to be interpreted as a MEM_IntReal instead of as an MEM_Int.  Normally,
         4140  +    ** MEM_IntReal values only arise during an INSERT operation of integer
         4141  +    ** values into a REAL column, so they can be challenging to test.  This
         4142  +    ** test-control enables us to write an intreal() SQL function that can
         4143  +    ** inject an intreal() value at arbitrary places in an SQL statement,
         4144  +    ** for testing purposes.
         4145  +    */
         4146  +    case SQLITE_TESTCTRL_RESULT_INTREAL: {
         4147  +      sqlite3_context *pCtx = va_arg(ap, sqlite3_context*);
         4148  +      sqlite3ResultIntReal(pCtx);
         4149  +      break;
         4150  +    }
  4135   4151     }
  4136   4152     va_end(ap);
  4137   4153   #endif /* SQLITE_UNTESTABLE */
  4138   4154     return rc;
  4139   4155   }
  4140   4156   
  4141   4157   /*

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.

  1060   1060     if( A ){
  1061   1061       A->x.pList = pList;
  1062   1062     }else{
  1063   1063       sqlite3ExprListDelete(pParse->db, pList);
  1064   1064     }
  1065   1065   }
  1066   1066   
  1067         -expr(A) ::= expr(A) AND(OP) expr(Y).    {A=sqlite3PExpr(pParse,@OP,A,Y);}
         1067  +expr(A) ::= expr(A) AND expr(Y).        {A=sqlite3ExprAnd(pParse,A,Y);}
  1068   1068   expr(A) ::= expr(A) OR(OP) expr(Y).     {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1069   1069   expr(A) ::= expr(A) LT|GT|GE|LE(OP) expr(Y).
  1070   1070                                           {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1071   1071   expr(A) ::= expr(A) EQ|NE(OP) expr(Y).  {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1072   1072   expr(A) ::= expr(A) BITAND|BITOR|LSHIFT|RSHIFT(OP) expr(Y).
  1073   1073                                           {A=sqlite3PExpr(pParse,@OP,A,Y);}
  1074   1074   expr(A) ::= expr(A) PLUS|MINUS(OP) expr(Y).

Changes to src/pragma.c.

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

Changes to src/pragma.h.

   181    181   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
   182    182    {/* zName:     */ "cache_spill",
   183    183     /* ePragTyp:  */ PragTyp_CACHE_SPILL,
   184    184     /* ePragFlg:  */ PragFlg_Result0|PragFlg_SchemaReq|PragFlg_NoColumns1,
   185    185     /* ColNames:  */ 0, 0,
   186    186     /* iArg:      */ 0 },
   187    187   #endif
          188  +#if !defined(SQLITE_OMIT_CASE_SENSITIVE_LIKE_PRAGMA)
   188    189    {/* zName:     */ "case_sensitive_like",
   189    190     /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
   190    191     /* ePragFlg:  */ PragFlg_NoColumns,
   191    192     /* ColNames:  */ 0, 0,
   192    193     /* iArg:      */ 0 },
          194  +#endif
   193    195    {/* zName:     */ "cell_size_check",
   194    196     /* ePragTyp:  */ PragTyp_FLAG,
   195    197     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
   196    198     /* ColNames:  */ 0, 0,
   197    199     /* iArg:      */ SQLITE_CellSizeCk },
   198    200   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
   199    201    {/* 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  +          if( IN_RENAME_OBJECT==0 ){
   869    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 {
................................................................................
  1674   1678   /*
  1675   1679   ** Print a schema statement.  Part of MODE_Semi and MODE_Pretty output.
  1676   1680   **
  1677   1681   ** This routine converts some CREATE TABLE statements for shadow tables
  1678   1682   ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
  1679   1683   */
  1680   1684   static void printSchemaLine(FILE *out, const char *z, const char *zTail){
         1685  +  if( z==0 ) return;
         1686  +  if( zTail==0 ) return;
  1681   1687     if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
  1682   1688       utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
  1683   1689     }else{
  1684   1690       utf8_printf(out, "%s%s", z, zTail);
  1685   1691     }
  1686   1692   }
  1687   1693   static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
................................................................................
  2994   3000         );
  2995   3001       }
  2996   3002     }
  2997   3003   
  2998   3004     return rc;
  2999   3005   }
  3000   3006   #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
  3001         -
  3002         -static void shellPrepare(
  3003         -  sqlite3 *db, 
  3004         -  int *pRc, 
  3005         -  const char *zSql, 
  3006         -  sqlite3_stmt **ppStmt
  3007         -){
  3008         -  *ppStmt = 0;
  3009         -  if( *pRc==SQLITE_OK ){
  3010         -    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
  3011         -    if( rc!=SQLITE_OK ){
  3012         -      raw_printf(stderr, "sql error: %s (%d)\n", 
  3013         -          sqlite3_errmsg(db), sqlite3_errcode(db)
  3014         -      );
  3015         -      *pRc = rc;
  3016         -    }
  3017         -  }
  3018         -}
  3019         -
  3020         -static void shellExecPrintf(
  3021         -  sqlite3 *db, 
  3022         -  int *pRc, 
  3023         -  const char *zFmt, 
  3024         -  ...
  3025         -){
  3026         -  if( *pRc==SQLITE_OK ){
  3027         -    va_list ap;
  3028         -    char *z;
  3029         -    va_start(ap, zFmt);
  3030         -    z = sqlite3_vmprintf(zFmt, ap);
  3031         -    va_end(ap);
  3032         -    if( z==0 ){
  3033         -      *pRc = SQLITE_NOMEM;
  3034         -    }else{
  3035         -      *pRc = sqlite3_exec(db, z, 0, 0, 0);
  3036         -      sqlite3_free(z);
  3037         -    }
  3038         -  }
  3039         -}
  3040         -
  3041         -static void shellPreparePrintf(
  3042         -  sqlite3 *db, 
  3043         -  int *pRc, 
  3044         -  sqlite3_stmt **ppStmt,
  3045         -  const char *zFmt, 
  3046         -  ...
  3047         -){
  3048         -  *ppStmt = 0;
  3049         -  if( *pRc==SQLITE_OK ){
  3050         -    va_list ap;
  3051         -    char *z;
  3052         -    va_start(ap, zFmt);
  3053         -    z = sqlite3_vmprintf(zFmt, ap);
  3054         -    va_end(ap);
  3055         -    if( z==0 ){
  3056         -      *pRc = SQLITE_NOMEM;
  3057         -    }else{
  3058         -      shellPrepare(db, pRc, z, ppStmt);
  3059         -      sqlite3_free(z);
  3060         -    }
  3061         -  }
  3062         -}
  3063         -
  3064         -static void shellFinalize(
  3065         -  int *pRc, 
  3066         -  sqlite3_stmt *pStmt
  3067         -){
  3068         -  if( pStmt ){
  3069         -    sqlite3 *db = sqlite3_db_handle(pStmt);
  3070         -    int rc = sqlite3_finalize(pStmt);
  3071         -    if( *pRc==SQLITE_OK ){
  3072         -      if( rc!=SQLITE_OK ){
  3073         -        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
  3074         -      }
  3075         -      *pRc = rc;
  3076         -    }
  3077         -  }
  3078         -}
  3079         -
  3080         -static void shellReset(
  3081         -  int *pRc, 
  3082         -  sqlite3_stmt *pStmt
  3083         -){
  3084         -  int rc = sqlite3_reset(pStmt);
  3085         -  if( *pRc==SQLITE_OK ){
  3086         -    if( rc!=SQLITE_OK ){
  3087         -      sqlite3 *db = sqlite3_db_handle(pStmt);
  3088         -      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
  3089         -    }
  3090         -    *pRc = rc;
  3091         -  }
  3092         -}
  3093         -
  3094         -static int sharedSchemaFix(ShellState *pState, const char *zDb, int eFix){
  3095         -  int rc = SQLITE_OK;
  3096         -  i64 iLast = 0;
  3097         -  int iCookie = 0;
  3098         -  int iAutoVacuum = 0;
  3099         -  sqlite3_stmt *pStmt = 0;
  3100         -
  3101         -  shellExecPrintf(pState->db, &rc, "ATTACH '%q' AS _shared_schema_tmp", zDb);
  3102         -  shellExecPrintf(pState->db, &rc, "PRAGMA writable_schema = 1");
  3103         -  shellExecPrintf(pState->db, &rc, "BEGIN");
  3104         -  shellPreparePrintf(pState->db, &rc, &pStmt, 
  3105         -      "SELECT max(rowid) FROM _shared_schema_tmp.sqlite_master"
  3106         -  );
  3107         -  sqlite3_step(pStmt);
  3108         -  iLast = sqlite3_column_int64(pStmt, 0);
  3109         -  shellFinalize(&rc, pStmt);
  3110         -  shellPreparePrintf(pState->db, &rc, &pStmt,
  3111         -      "INSERT INTO _shared_schema_tmp.sqlite_master SELECT "
  3112         -      "  type, name, tbl_name, ("
  3113         -      "    SELECT rootpage FROM _shared_schema_tmp.sqlite_master WHERE "
  3114         -      "      type IS o.type AND name IS o.name AND rowid<=?"
  3115         -      "  ), sql FROM main.sqlite_master AS o"
  3116         -  );
  3117         -  sqlite3_bind_int64(pStmt, 1, iLast);
  3118         -  sqlite3_step(pStmt);
  3119         -  shellFinalize(&rc, pStmt);
  3120         -
  3121         -  shellExecPrintf(pState->db, &rc,
  3122         -      "DELETE FROM _shared_schema_tmp.sqlite_master WHERE rowid<=%lld",
  3123         -      iLast
  3124         -  );
  3125         -  shellExecPrintf(pState->db, &rc, "COMMIT");
  3126         -  sqlite3_exec(pState->db, "PRAGMA writable_schema = 0", 0, 0, 0);
  3127         -
  3128         -  /* Copy the auto-vacuum setting from main to the target db */
  3129         -  shellPreparePrintf(pState->db, &rc, &pStmt, "PRAGMA main.auto_vacuum");
  3130         -  sqlite3_step(pStmt);
  3131         -  iAutoVacuum = sqlite3_column_int(pStmt, 0);
  3132         -  shellFinalize(&rc, pStmt);
  3133         -  shellExecPrintf(pState->db, &rc, 
  3134         -      "PRAGMA _shared_schema_tmp.auto_vacuum = %d", iAutoVacuum
  3135         -  );
  3136         -
  3137         -  /* Vacuum the db in order to standardize the rootpage numbers. */
  3138         -  shellExecPrintf(pState->db, &rc, "VACUUM _shared_schema_tmp");
  3139         -
  3140         -  /* Set the schema-cookie value to the same as database "main" */
  3141         -  shellPreparePrintf(pState->db, &rc, &pStmt, "PRAGMA main.schema_version");
  3142         -  sqlite3_step(pStmt);
  3143         -  iCookie = sqlite3_column_int(pStmt, 0);
  3144         -  shellFinalize(&rc, pStmt);
  3145         -  shellExecPrintf(pState->db, &rc, 
  3146         -      "PRAGMA _shared_schema_tmp.schema_version = %d", iCookie
  3147         -  );
  3148         -
  3149         -  sqlite3_exec(pState->db, "DETACH _shared_schema_tmp", 0, 0, 0);
  3150         -  return rc;
  3151         -}
  3152         -
  3153         -static int sharedSchemaCheck(ShellState *pState, const char *zDb, int *peFix){
  3154         -  int rc = SQLITE_OK;
  3155         -  int bFailed = 0;
  3156         -  sqlite3_stmt *pStmt = 0;
  3157         -
  3158         -  if( peFix ) *peFix = 0;
  3159         -  shellExecPrintf(pState->db, &rc, "ATTACH '%q' AS _shared_schema_tmp", zDb);
  3160         -
  3161         -  /* Check if this database has the same set of objects as the current db */
  3162         -  shellPreparePrintf(pState->db, &rc, &pStmt, 
  3163         -    "SELECT type, name FROM _shared_schema_tmp.sqlite_master AS o "
  3164         -    "WHERE NOT EXISTS ("
  3165         -    "  SELECT 1 FROM main.sqlite_master "
  3166         -    "    WHERE name IS o.name AND type IS o.type"
  3167         -    ")"
  3168         -    " UNION ALL "
  3169         -    "SELECT type, name FROM main.sqlite_master AS o "
  3170         -    "WHERE NOT EXISTS ("
  3171         -    "  SELECT 1 FROM _shared_schema_tmp.sqlite_master "
  3172         -    "    WHERE name IS o.name AND type IS o.type"
  3173         -    ")"
  3174         -  );
  3175         -  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  3176         -    utf8_printf(pState->out, "%s is NOT compatible (objects)\n", zDb);
  3177         -    bFailed = 1;
  3178         -  }
  3179         -  shellFinalize(&rc, pStmt);
  3180         -
  3181         -  /* Check if this database has the same set of SQL statements as the 
  3182         -  ** current db. */
  3183         -  if( bFailed==0 ){
  3184         -    shellPreparePrintf(pState->db, &rc, &pStmt, 
  3185         -        "SELECT 1 FROM _shared_schema_tmp.sqlite_master AS o "
  3186         -        "WHERE sql IS NOT ("
  3187         -        "  SELECT sql FROM main.sqlite_master "
  3188         -        "    WHERE name IS o.name AND type IS o.type"
  3189         -        ")"
  3190         -    );
  3191         -    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  3192         -      utf8_printf(pState->out, "%s is NOT compatible (SQL)\n", zDb);
  3193         -      bFailed = 1;
  3194         -    }
  3195         -    shellFinalize(&rc, pStmt);
  3196         -  }
  3197         -
  3198         -  /* Check if this database has the same set of root pages as the current 
  3199         -  ** db. */
  3200         -  if( bFailed==0 ){
  3201         -    shellPreparePrintf(pState->db, &rc, &pStmt, 
  3202         -        "SELECT 1 FROM _shared_schema_tmp.sqlite_master AS o "
  3203         -        "WHERE rootpage IS NOT ("
  3204         -        "  SELECT rootpage FROM main.sqlite_master "
  3205         -        "    WHERE name IS o.name AND type IS o.type"
  3206         -        ")"
  3207         -    );
  3208         -    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  3209         -      if( peFix==0 ){
  3210         -        utf8_printf(pState->out, "%s is NOT compatible (root pages)\n", zDb);
  3211         -      }
  3212         -      bFailed = 1;
  3213         -      if( peFix ) *peFix = 1;
  3214         -    }
  3215         -    shellFinalize(&rc, pStmt);
  3216         -  }
  3217         -
  3218         -  if( bFailed==0 ){
  3219         -    shellPreparePrintf(pState->db, &rc, &pStmt, 
  3220         -        "SELECT 1 WHERE ("
  3221         -        "  SELECT group_concat(rootpage || '.' || name || '.' || sql, '.') "
  3222         -        "  FROM _shared_schema_tmp.sqlite_master"
  3223         -        ") IS NOT ("
  3224         -        "  SELECT group_concat(rootpage || '.' || name || '.' || sql, '.') "
  3225         -        "  FROM main.sqlite_master"
  3226         -        ")"
  3227         -    );
  3228         -    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  3229         -      if( peFix==0 ){
  3230         -        utf8_printf(pState->out, 
  3231         -            "%s is NOT compatible (order of sqlite_master rows)\n", zDb
  3232         -        );
  3233         -      }
  3234         -      bFailed = 1;
  3235         -      if( peFix ) *peFix = 2;
  3236         -    }
  3237         -    shellFinalize(&rc, pStmt);
  3238         -  }
  3239         -
  3240         -  if( bFailed==0 ){
  3241         -    int iMain = -1;
  3242         -    int iNew = +1;
  3243         -    shellPreparePrintf(pState->db, &rc, &pStmt, 
  3244         -        "PRAGMA main.schema_version"
  3245         -    );
  3246         -    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  3247         -      iMain = sqlite3_column_int(pStmt, 0);
  3248         -    }
  3249         -    shellFinalize(&rc, pStmt);
  3250         -    shellPreparePrintf(pState->db, &rc, &pStmt, 
  3251         -        "PRAGMA _shared_schema_tmp.schema_version"
  3252         -    );
  3253         -    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  3254         -      iNew = sqlite3_column_int(pStmt, 0);
  3255         -    }
  3256         -    shellFinalize(&rc, pStmt);
  3257         -    if( rc==SQLITE_OK && iMain!=iNew ){
  3258         -      if( peFix==0 ){
  3259         -        utf8_printf(pState->out, 
  3260         -            "%s is NOT compatible (schema cookie)\n", zDb
  3261         -        );
  3262         -      }
  3263         -      bFailed = 1;
  3264         -      if( peFix ) *peFix = 3;
  3265         -    }
  3266         -  }
  3267         -
  3268         -  if( rc==SQLITE_OK && bFailed==0 ){
  3269         -    utf8_printf(pState->out, "%s is compatible\n", zDb);
  3270         -  }
  3271         -
  3272         -  sqlite3_exec(pState->db, "DETACH _shared_schema_tmp", 0, 0, 0);
  3273         -  return rc;
  3274         -}
  3275         -
  3276         -/*
  3277         -** .shared-schema check|fix DB1 DB2...
  3278         -*/
  3279         -static int sharedSchemaDotCommand(
  3280         -  ShellState *pState,             /* Current shell tool state */
  3281         -  char **azArg,                   /* Array of arguments passed to dot command */
  3282         -  int nArg                        /* Number of entries in azArg[] */
  3283         -){
  3284         -  int rc = SQLITE_OK;
  3285         -  int bFix = 0;                   /* Fix databases if possible */
  3286         -  int n1;
  3287         -  int i;
  3288         -  if( nArg<3 ){
  3289         -    goto shared_schema_usage;
  3290         -  }
  3291         -
  3292         -  n1 = (int)strlen(azArg[1]);
  3293         -  if( n1>0 && n1<=3 && memcmp("fix", azArg[1], n1)==0 ){
  3294         -    bFix = 1;
  3295         -  }else if( n1==0 || n1>5 || memcmp("check", azArg[1], n1) ){
  3296         -    goto shared_schema_usage;
  3297         -  }
  3298         -
  3299         -  for(i=2; rc==SQLITE_OK && i<nArg; i++){
  3300         -    int eFix = 0;
  3301         -    rc = sharedSchemaCheck(pState, azArg[i], bFix ? &eFix : 0);
  3302         -    if( rc==SQLITE_OK && bFix && eFix ){
  3303         -      utf8_printf(pState->out, "Fixing %s... ", azArg[i]);
  3304         -      fflush(pState->out);
  3305         -      rc = sharedSchemaFix(pState, azArg[i], eFix);
  3306         -      if( rc==SQLITE_OK ){
  3307         -        rc = sharedSchemaCheck(pState, azArg[i], &eFix);
  3308         -        if( rc==SQLITE_OK && eFix ){
  3309         -          utf8_printf(pState->out, "VACUUMing main... ");
  3310         -          fflush(pState->out);
  3311         -          rc = sqlite3_exec(pState->db, "VACUUM main", 0, 0, 0);
  3312         -          if( rc==SQLITE_OK ){
  3313         -            rc = sharedSchemaCheck(pState, azArg[i], 0);
  3314         -          }
  3315         -        }
  3316         -      }
  3317         -    }
  3318         -  }
  3319         -
  3320         -  return rc;
  3321         - shared_schema_usage:
  3322         -  raw_printf(stderr, "usage: .shared-schema check|fix DB1 DB2...\n");
  3323         -  return SQLITE_ERROR;
  3324         -}
  3325         -
  3326   3007   
  3327   3008   /*
  3328   3009   ** Execute a statement or set of statements.  Print
  3329   3010   ** any result rows/columns depending on the current mode
  3330   3011   ** set via the supplied callback.
  3331   3012   **
  3332   3013   ** This is very similar to SQLite's built-in sqlite3_exec()
................................................................................
  3811   3492     ".databases               List names and files of attached databases",
  3812   3493     ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
  3813   3494     ".dbinfo ?DB?             Show status information about the database",
  3814   3495     ".dump ?TABLE? ...        Render all database content as SQL",
  3815   3496     "   Options:",
  3816   3497     "     --preserve-rowids      Include ROWID values in the output",
  3817   3498     "     --newlines             Allow unescaped newline characters in output",
  3818         -  "   TABLE is LIKE pattern for the tables to dump",
         3499  +  "   TABLE is a LIKE pattern for the tables to dump",
  3819   3500     ".echo on|off             Turn command echo on or off",
  3820   3501     ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
  3821   3502     "   Other Modes:",
  3822   3503   #ifdef SQLITE_DEBUG
  3823   3504     "      test                  Show raw EXPLAIN QUERY PLAN output",
  3824   3505     "      trace                 Like \"full\" but also enable \"PRAGMA vdbe_trace\"",
  3825   3506   #endif
................................................................................
  3896   3577     "   --once                    Do no more than one progress interrupt",
  3897   3578     "   --quiet|-q                No output except at interrupts",
  3898   3579     "   --reset                   Reset the count for each input and interrupt",
  3899   3580   #endif
  3900   3581     ".prompt MAIN CONTINUE    Replace the standard prompts",
  3901   3582     ".quit                    Exit this program",
  3902   3583     ".read FILE               Read input from FILE",
         3584  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         3585  +  ".recover                 Recover as much data as possible from corrupt db.",
         3586  +#endif
  3903   3587     ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
  3904   3588     ".save FILE               Write in-memory database into FILE",
  3905   3589     ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
  3906   3590     ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
  3907   3591     "     Options:",
  3908   3592     "         --indent            Try to pretty-print the schema",
  3909   3593     ".selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
................................................................................
  4180   3864     int nLine;
  4181   3865     int n = 0;
  4182   3866     int pgsz = 0;
  4183   3867     int iOffset = 0;
  4184   3868     int j, k;
  4185   3869     int rc;
  4186   3870     FILE *in;
  4187         -  unsigned char x[16];
         3871  +  unsigned int x[16];
  4188   3872     char zLine[1000];
  4189   3873     if( p->zDbFilename ){
  4190   3874       in = fopen(p->zDbFilename, "r");
  4191   3875       if( in==0 ){
  4192   3876         utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
  4193   3877         return 0;
  4194   3878       }
  4195   3879       nLine = 0;
  4196   3880     }else{
  4197   3881       in = p->in;
  4198   3882       nLine = p->lineno;
         3883  +    if( in==0 ) in = stdin;
  4199   3884     }
  4200   3885     *pnData = 0;
  4201   3886     nLine++;
  4202   3887     if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
  4203   3888     rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
  4204   3889     if( rc!=2 ) goto readHexDb_error;
  4205         -  if( n<=0 ) goto readHexDb_error;
  4206         -  a = sqlite3_malloc( n );
         3890  +  if( n<0 ) goto readHexDb_error;
         3891  +  a = sqlite3_malloc( n ? n : 1 );
  4207   3892     if( a==0 ){
  4208   3893       utf8_printf(stderr, "Out of memory!\n");
  4209   3894       goto readHexDb_error;
  4210   3895     }
  4211   3896     memset(a, 0, n);
  4212   3897     if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
  4213   3898       utf8_printf(stderr, "invalid pagesize\n");
................................................................................
  4218   3903       if( rc==2 ){
  4219   3904         iOffset = k;
  4220   3905         continue;
  4221   3906       }
  4222   3907       if( strncmp(zLine, "| end ", 6)==0 ){
  4223   3908         break;
  4224   3909       }
  4225         -    rc = sscanf(zLine,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
  4226         -                      "  %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
         3910  +    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
  4227   3911                   &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
  4228   3912                   &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
  4229   3913       if( rc==17 ){
  4230   3914         k = iOffset+j;
  4231   3915         if( k+16<=n ){
  4232         -        memcpy(a+k, x, 16);
         3916  +        int ii;
         3917  +        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
  4233   3918         }
  4234   3919       }
  4235   3920     }
  4236   3921     *pnData = n;
  4237   3922     if( in!=p->in ){
  4238   3923       fclose(in);
  4239   3924     }else{
  4240   3925       p->lineno = nLine;
  4241   3926     }
  4242   3927     return a;
  4243   3928   
  4244   3929   readHexDb_error:
  4245         -  if( in!=stdin ){
         3930  +  if( in!=p->in ){
  4246   3931       fclose(in);
  4247   3932     }else{
  4248   3933       while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
  4249   3934         nLine++;
  4250   3935         if(strncmp(zLine, "| end ", 6)==0 ) break;
  4251   3936       }
  4252   3937       p->lineno = nLine;
  4253   3938     }
  4254   3939     sqlite3_free(a);
  4255   3940     utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
  4256   3941     return 0;
  4257   3942   }
  4258   3943   #endif /* SQLITE_ENABLE_DESERIALIZE */
         3944  +
         3945  +/*
         3946  +** Scalar function "shell_int32". The first argument to this function
         3947  +** must be a blob. The second a non-negative integer. This function
         3948  +** reads and returns a 32-bit big-endian integer from byte
         3949  +** offset (4*<arg2>) of the blob.
         3950  +*/
         3951  +static void shellInt32(
         3952  +  sqlite3_context *context, 
         3953  +  int argc, 
         3954  +  sqlite3_value **argv
         3955  +){
         3956  +  const unsigned char *pBlob;
         3957  +  int nBlob;
         3958  +  int iInt;
         3959  +
         3960  +  UNUSED_PARAMETER(argc);
         3961  +  nBlob = sqlite3_value_bytes(argv[0]);
         3962  +  pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
         3963  +  iInt = sqlite3_value_int(argv[1]);
         3964  +
         3965  +  if( iInt>=0 && (iInt+1)*4<=nBlob ){
         3966  +    const unsigned char *a = &pBlob[iInt*4];
         3967  +    sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
         3968  +                       + ((sqlite3_int64)a[1]<<16)
         3969  +                       + ((sqlite3_int64)a[2]<< 8)
         3970  +                       + ((sqlite3_int64)a[3]<< 0);
         3971  +    sqlite3_result_int64(context, iVal);
         3972  +  }
         3973  +}
         3974  +
         3975  +/*
         3976  +** Scalar function "shell_escape_crnl" used by the .recover command.
         3977  +** The argument passed to this function is the output of built-in
         3978  +** function quote(). If the first character of the input is "'", 
         3979  +** indicating that the value passed to quote() was a text value,
         3980  +** then this function searches the input for "\n" and "\r" characters
         3981  +** and adds a wrapper similar to the following:
         3982  +**
         3983  +**   replace(replace(<input>, '\n', char(10), '\r', char(13));
         3984  +**
         3985  +** Or, if the first character of the input is not "'", then a copy
         3986  +** of the input is returned.
         3987  +*/
         3988  +static void shellEscapeCrnl(
         3989  +  sqlite3_context *context, 
         3990  +  int argc, 
         3991  +  sqlite3_value **argv
         3992  +){
         3993  +  const char *zText = (const char*)sqlite3_value_text(argv[0]);
         3994  +  UNUSED_PARAMETER(argc);
         3995  +  if( zText[0]=='\'' ){
         3996  +    int nText = sqlite3_value_bytes(argv[0]);
         3997  +    int i;
         3998  +    char zBuf1[20];
         3999  +    char zBuf2[20];
         4000  +    const char *zNL = 0;
         4001  +    const char *zCR = 0;
         4002  +    int nCR = 0;
         4003  +    int nNL = 0;
         4004  +
         4005  +    for(i=0; zText[i]; i++){
         4006  +      if( zNL==0 && zText[i]=='\n' ){
         4007  +        zNL = unused_string(zText, "\\n", "\\012", zBuf1);
         4008  +        nNL = (int)strlen(zNL);
         4009  +      }
         4010  +      if( zCR==0 && zText[i]=='\r' ){
         4011  +        zCR = unused_string(zText, "\\r", "\\015", zBuf2);
         4012  +        nCR = (int)strlen(zCR);
         4013  +      }
         4014  +    }
         4015  +
         4016  +    if( zNL || zCR ){
         4017  +      int iOut = 0;
         4018  +      i64 nMax = (nNL > nCR) ? nNL : nCR;
         4019  +      i64 nAlloc = nMax * nText + (nMax+64)*2;
         4020  +      char *zOut = (char*)sqlite3_malloc64(nAlloc);
         4021  +      if( zOut==0 ){
         4022  +        sqlite3_result_error_nomem(context);
         4023  +        return;
         4024  +      }
         4025  +
         4026  +      if( zNL && zCR ){
         4027  +        memcpy(&zOut[iOut], "replace(replace(", 16);
         4028  +        iOut += 16;
         4029  +      }else{
         4030  +        memcpy(&zOut[iOut], "replace(", 8);
         4031  +        iOut += 8;
         4032  +      }
         4033  +      for(i=0; zText[i]; i++){
         4034  +        if( zText[i]=='\n' ){
         4035  +          memcpy(&zOut[iOut], zNL, nNL);
         4036  +          iOut += nNL;
         4037  +        }else if( zText[i]=='\r' ){
         4038  +          memcpy(&zOut[iOut], zCR, nCR);
         4039  +          iOut += nCR;
         4040  +        }else{
         4041  +          zOut[iOut] = zText[i];
         4042  +          iOut++;
         4043  +        }
         4044  +      }
         4045  +
         4046  +      if( zNL ){
         4047  +        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
         4048  +        memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
         4049  +        memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
         4050  +      }
         4051  +      if( zCR ){
         4052  +        memcpy(&zOut[iOut], ",'", 2); iOut += 2;
         4053  +        memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
         4054  +        memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
         4055  +      }
         4056  +
         4057  +      sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
         4058  +      sqlite3_free(zOut);
         4059  +      return;
         4060  +    }
         4061  +  }
         4062  +
         4063  +  sqlite3_result_value(context, argv[0]);
         4064  +}
  4259   4065   
  4260   4066   /* Flags for open_db().
  4261   4067   **
  4262   4068   ** The default behavior of open_db() is to exit(1) if the database fails to
  4263   4069   ** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
  4264   4070   ** but still returns without calling exit.
  4265   4071   **
................................................................................
  4326   4132       }
  4327   4133   #ifndef SQLITE_OMIT_LOAD_EXTENSION
  4328   4134       sqlite3_enable_load_extension(p->db, 1);
  4329   4135   #endif
  4330   4136       sqlite3_fileio_init(p->db, 0, 0);
  4331   4137       sqlite3_shathree_init(p->db, 0, 0);
  4332   4138       sqlite3_completion_init(p->db, 0, 0);
         4139  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         4140  +    sqlite3_dbdata_init(p->db, 0, 0);
         4141  +#endif
  4333   4142   #ifdef SQLITE_HAVE_ZLIB
  4334   4143       sqlite3_zipfile_init(p->db, 0, 0);
  4335   4144       sqlite3_sqlar_init(p->db, 0, 0);
  4336   4145   #endif
  4337   4146       sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
  4338   4147                               shellAddSchemaName, 0, 0);
  4339   4148       sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
  4340   4149                               shellModuleSchema, 0, 0);
  4341   4150       sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
  4342   4151                               shellPutsFunc, 0, 0);
         4152  +    sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
         4153  +                            shellEscapeCrnl, 0, 0);
         4154  +    sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
         4155  +                            shellInt32, 0, 0);
  4343   4156   #ifndef SQLITE_NOHAVE_SYSTEM
  4344   4157       sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
  4345   4158                               editFunc, 0, 0);
  4346   4159       sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
  4347   4160                               editFunc, 0, 0);
  4348   4161   #endif
  4349   4162       if( p->openMode==SHELL_OPEN_ZIPFILE ){
................................................................................
  4359   4172         int nData = 0;
  4360   4173         unsigned char *aData;
  4361   4174         if( p->openMode==SHELL_OPEN_DESERIALIZE ){
  4362   4175           aData = (unsigned char*)readFile(p->zDbFilename, &nData);
  4363   4176         }else{
  4364   4177           aData = readHexDb(p, &nData);
  4365   4178           if( aData==0 ){
  4366         -          utf8_printf(stderr, "Error in hexdb input\n");
  4367   4179             return;
  4368   4180           }
  4369   4181         }
  4370   4182         rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
  4371   4183                      SQLITE_DESERIALIZE_RESIZEABLE |
  4372   4184                      SQLITE_DESERIALIZE_FREEONCLOSE);
  4373   4185         if( rc ){
................................................................................
  5590   5402    usage:
  5591   5403     raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
  5592   5404     raw_printf(stderr, "Where sub-commands are:\n");
  5593   5405     raw_printf(stderr, "    fkey-indexes\n");
  5594   5406     return SQLITE_ERROR;
  5595   5407   }
  5596   5408   
         5409  +#if !defined SQLITE_OMIT_VIRTUALTABLE
         5410  +static void shellPrepare(
         5411  +  sqlite3 *db, 
         5412  +  int *pRc, 
         5413  +  const char *zSql, 
         5414  +  sqlite3_stmt **ppStmt
         5415  +){
         5416  +  *ppStmt = 0;
         5417  +  if( *pRc==SQLITE_OK ){
         5418  +    int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
         5419  +    if( rc!=SQLITE_OK ){
         5420  +      raw_printf(stderr, "sql error: %s (%d)\n", 
         5421  +          sqlite3_errmsg(db), sqlite3_errcode(db)
         5422  +      );
         5423  +      *pRc = rc;
         5424  +    }
         5425  +  }
         5426  +}
         5427  +
         5428  +/*
         5429  +** Create a prepared statement using printf-style arguments for the SQL.
         5430  +**
         5431  +** This routine is could be marked "static".  But it is not always used,
         5432  +** depending on compile-time options.  By omitting the "static", we avoid
         5433  +** nuisance compiler warnings about "defined but not used".
         5434  +*/
         5435  +void shellPreparePrintf(
         5436  +  sqlite3 *db, 
         5437  +  int *pRc, 
         5438  +  sqlite3_stmt **ppStmt,
         5439  +  const char *zFmt, 
         5440  +  ...
         5441  +){
         5442  +  *ppStmt = 0;
         5443  +  if( *pRc==SQLITE_OK ){
         5444  +    va_list ap;
         5445  +    char *z;
         5446  +    va_start(ap, zFmt);
         5447  +    z = sqlite3_vmprintf(zFmt, ap);
         5448  +    va_end(ap);
         5449  +    if( z==0 ){
         5450  +      *pRc = SQLITE_NOMEM;
         5451  +    }else{
         5452  +      shellPrepare(db, pRc, z, ppStmt);
         5453  +      sqlite3_free(z);
         5454  +    }
         5455  +  }
         5456  +}
         5457  +
         5458  +/* Finalize the prepared statement created using shellPreparePrintf().
         5459  +**
         5460  +** This routine is could be marked "static".  But it is not always used,
         5461  +** depending on compile-time options.  By omitting the "static", we avoid
         5462  +** nuisance compiler warnings about "defined but not used".
         5463  +*/
         5464  +void shellFinalize(
         5465  +  int *pRc, 
         5466  +  sqlite3_stmt *pStmt
         5467  +){
         5468  +  if( pStmt ){
         5469  +    sqlite3 *db = sqlite3_db_handle(pStmt);
         5470  +    int rc = sqlite3_finalize(pStmt);
         5471  +    if( *pRc==SQLITE_OK ){
         5472  +      if( rc!=SQLITE_OK ){
         5473  +        raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
         5474  +      }
         5475  +      *pRc = rc;
         5476  +    }
         5477  +  }
         5478  +}
         5479  +
         5480  +/* Reset the prepared statement created using shellPreparePrintf().
         5481  +**
         5482  +** This routine is could be marked "static".  But it is not always used,
         5483  +** depending on compile-time options.  By omitting the "static", we avoid
         5484  +** nuisance compiler warnings about "defined but not used".
         5485  +*/
         5486  +void shellReset(
         5487  +  int *pRc, 
         5488  +  sqlite3_stmt *pStmt
         5489  +){
         5490  +  int rc = sqlite3_reset(pStmt);
         5491  +  if( *pRc==SQLITE_OK ){
         5492  +    if( rc!=SQLITE_OK ){
         5493  +      sqlite3 *db = sqlite3_db_handle(pStmt);
         5494  +      raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
         5495  +    }
         5496  +    *pRc = rc;
         5497  +  }
         5498  +}
         5499  +#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
         5500  +
  5597   5501   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
  5598   5502   /*********************************************************************************
  5599   5503   ** The ".archive" or ".ar" command.
  5600   5504   */
  5601         -
  5602   5505   /*
  5603   5506   ** Structure representing a single ".ar" command.
  5604   5507   */
  5605   5508   typedef struct ArCommand ArCommand;
  5606   5509   struct ArCommand {
  5607   5510     u8 eCmd;                        /* An AR_CMD_* value */
  5608   5511     u8 bVerbose;                    /* True if --verbose */
................................................................................
  6284   6187   
  6285   6188     return rc;
  6286   6189   }
  6287   6190   /* End of the ".archive" or ".ar" command logic
  6288   6191   **********************************************************************************/
  6289   6192   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
  6290   6193   
         6194  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         6195  +/*
         6196  +** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
         6197  +** Otherwise, the SQL statement or statements in zSql are executed using
         6198  +** database connection db and the error code written to *pRc before
         6199  +** this function returns.
         6200  +*/
         6201  +static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
         6202  +  int rc = *pRc;
         6203  +  if( rc==SQLITE_OK ){
         6204  +    char *zErr = 0;
         6205  +    rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
         6206  +    if( rc!=SQLITE_OK ){
         6207  +      raw_printf(stderr, "SQL error: %s\n", zErr);
         6208  +    }
         6209  +    *pRc = rc;
         6210  +  }
         6211  +}
         6212  +
         6213  +/*
         6214  +** Like shellExec(), except that zFmt is a printf() style format string.
         6215  +*/
         6216  +static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
         6217  +  char *z = 0;
         6218  +  if( *pRc==SQLITE_OK ){
         6219  +    va_list ap;
         6220  +    va_start(ap, zFmt);
         6221  +    z = sqlite3_vmprintf(zFmt, ap);
         6222  +    va_end(ap);
         6223  +    if( z==0 ){
         6224  +      *pRc = SQLITE_NOMEM;
         6225  +    }else{
         6226  +      shellExec(db, pRc, z);
         6227  +    }
         6228  +    sqlite3_free(z);
         6229  +  }
         6230  +}
         6231  +
         6232  +/*
         6233  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
         6234  +** Otherwise, an attempt is made to allocate, zero and return a pointer
         6235  +** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
         6236  +** to SQLITE_NOMEM and NULL returned.
         6237  +*/
         6238  +static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
         6239  +  void *pRet = 0;
         6240  +  if( *pRc==SQLITE_OK ){
         6241  +    pRet = sqlite3_malloc64(nByte);
         6242  +    if( pRet==0 ){
         6243  +      *pRc = SQLITE_NOMEM;
         6244  +    }else{
         6245  +      memset(pRet, 0, nByte);
         6246  +    }
         6247  +  }
         6248  +  return pRet;
         6249  +}
         6250  +
         6251  +/*
         6252  +** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
         6253  +** Otherwise, zFmt is treated as a printf() style string. The result of
         6254  +** formatting it along with any trailing arguments is written into a 
         6255  +** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
         6256  +** It is the responsibility of the caller to eventually free this buffer
         6257  +** using a call to sqlite3_free().
         6258  +** 
         6259  +** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL 
         6260  +** pointer returned.
         6261  +*/
         6262  +static char *shellMPrintf(int *pRc, const char *zFmt, ...){
         6263  +  char *z = 0;
         6264  +  if( *pRc==SQLITE_OK ){
         6265  +    va_list ap;
         6266  +    va_start(ap, zFmt);
         6267  +    z = sqlite3_vmprintf(zFmt, ap);
         6268  +    va_end(ap);
         6269  +    if( z==0 ){
         6270  +      *pRc = SQLITE_NOMEM;
         6271  +    }
         6272  +  }
         6273  +  return z;
         6274  +}
         6275  +static int sharedSchemaFix(ShellState *pState, const char *zDb, int eFix){
         6276  +  int rc = SQLITE_OK;
         6277  +  i64 iLast = 0;
         6278  +  int iCookie = 0;
         6279  +  int iAutoVacuum = 0;
         6280  +  sqlite3_stmt *pStmt = 0;
         6281  +
         6282  +  shellExecPrintf(pState->db, &rc, "ATTACH '%q' AS _shared_schema_tmp", zDb);
         6283  +  shellExecPrintf(pState->db, &rc, "PRAGMA writable_schema = 1");
         6284  +  shellExecPrintf(pState->db, &rc, "BEGIN");
         6285  +  shellPreparePrintf(pState->db, &rc, &pStmt, 
         6286  +      "SELECT max(rowid) FROM _shared_schema_tmp.sqlite_master"
         6287  +  );
         6288  +  sqlite3_step(pStmt);
         6289  +  iLast = sqlite3_column_int64(pStmt, 0);
         6290  +  shellFinalize(&rc, pStmt);
         6291  +  shellPreparePrintf(pState->db, &rc, &pStmt,
         6292  +      "INSERT INTO _shared_schema_tmp.sqlite_master SELECT "
         6293  +      "  type, name, tbl_name, ("
         6294  +      "    SELECT rootpage FROM _shared_schema_tmp.sqlite_master WHERE "
         6295  +      "      type IS o.type AND name IS o.name AND rowid<=?"
         6296  +      "  ), sql FROM main.sqlite_master AS o"
         6297  +  );
         6298  +  sqlite3_bind_int64(pStmt, 1, iLast);
         6299  +  sqlite3_step(pStmt);
         6300  +  shellFinalize(&rc, pStmt);
         6301  +
         6302  +  shellExecPrintf(pState->db, &rc,
         6303  +      "DELETE FROM _shared_schema_tmp.sqlite_master WHERE rowid<=%lld",
         6304  +      iLast
         6305  +  );
         6306  +  shellExecPrintf(pState->db, &rc, "COMMIT");
         6307  +  sqlite3_exec(pState->db, "PRAGMA writable_schema = 0", 0, 0, 0);
         6308  +
         6309  +  /* Copy the auto-vacuum setting from main to the target db */
         6310  +  shellPreparePrintf(pState->db, &rc, &pStmt, "PRAGMA main.auto_vacuum");
         6311  +  sqlite3_step(pStmt);
         6312  +  iAutoVacuum = sqlite3_column_int(pStmt, 0);
         6313  +  shellFinalize(&rc, pStmt);
         6314  +  shellExecPrintf(pState->db, &rc, 
         6315  +      "PRAGMA _shared_schema_tmp.auto_vacuum = %d", iAutoVacuum
         6316  +  );
         6317  +
         6318  +  /* Vacuum the db in order to standardize the rootpage numbers. */
         6319  +  shellExecPrintf(pState->db, &rc, "VACUUM _shared_schema_tmp");
         6320  +
         6321  +  /* Set the schema-cookie value to the same as database "main" */
         6322  +  shellPreparePrintf(pState->db, &rc, &pStmt, "PRAGMA main.schema_version");
         6323  +  sqlite3_step(pStmt);
         6324  +  iCookie = sqlite3_column_int(pStmt, 0);
         6325  +  shellFinalize(&rc, pStmt);
         6326  +  shellExecPrintf(pState->db, &rc, 
         6327  +      "PRAGMA _shared_schema_tmp.schema_version = %d", iCookie
         6328  +  );
         6329  +
         6330  +  sqlite3_exec(pState->db, "DETACH _shared_schema_tmp", 0, 0, 0);
         6331  +  return rc;
         6332  +}
         6333  +
         6334  +static int sharedSchemaCheck(ShellState *pState, const char *zDb, int *peFix){
         6335  +  int rc = SQLITE_OK;
         6336  +  int bFailed = 0;
         6337  +  sqlite3_stmt *pStmt = 0;
         6338  +
         6339  +  if( peFix ) *peFix = 0;
         6340  +  shellExecPrintf(pState->db, &rc, "ATTACH '%q' AS _shared_schema_tmp", zDb);
         6341  +
         6342  +  /* Check if this database has the same set of objects as the current db */
         6343  +  shellPreparePrintf(pState->db, &rc, &pStmt, 
         6344  +    "SELECT type, name FROM _shared_schema_tmp.sqlite_master AS o "
         6345  +    "WHERE NOT EXISTS ("
         6346  +    "  SELECT 1 FROM main.sqlite_master "
         6347  +    "    WHERE name IS o.name AND type IS o.type"
         6348  +    ")"
         6349  +    " UNION ALL "
         6350  +    "SELECT type, name FROM main.sqlite_master AS o "
         6351  +    "WHERE NOT EXISTS ("
         6352  +    "  SELECT 1 FROM _shared_schema_tmp.sqlite_master "
         6353  +    "    WHERE name IS o.name AND type IS o.type"
         6354  +    ")"
         6355  +  );
         6356  +  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6357  +    utf8_printf(pState->out, "%s is NOT compatible (objects)\n", zDb);
         6358  +    bFailed = 1;
         6359  +  }
         6360  +  shellFinalize(&rc, pStmt);
         6361  +
         6362  +  /* Check if this database has the same set of SQL statements as the 
         6363  +  ** current db. */
         6364  +  if( bFailed==0 ){
         6365  +    shellPreparePrintf(pState->db, &rc, &pStmt, 
         6366  +        "SELECT 1 FROM _shared_schema_tmp.sqlite_master AS o "
         6367  +        "WHERE sql IS NOT ("
         6368  +        "  SELECT sql FROM main.sqlite_master "
         6369  +        "    WHERE name IS o.name AND type IS o.type"
         6370  +        ")"
         6371  +    );
         6372  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6373  +      utf8_printf(pState->out, "%s is NOT compatible (SQL)\n", zDb);
         6374  +      bFailed = 1;
         6375  +    }
         6376  +    shellFinalize(&rc, pStmt);
         6377  +  }
         6378  +
         6379  +  /* Check if this database has the same set of root pages as the current 
         6380  +  ** db. */
         6381  +  if( bFailed==0 ){
         6382  +    shellPreparePrintf(pState->db, &rc, &pStmt, 
         6383  +        "SELECT 1 FROM _shared_schema_tmp.sqlite_master AS o "
         6384  +        "WHERE rootpage IS NOT ("
         6385  +        "  SELECT rootpage FROM main.sqlite_master "
         6386  +        "    WHERE name IS o.name AND type IS o.type"
         6387  +        ")"
         6388  +    );
         6389  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6390  +      if( peFix==0 ){
         6391  +        utf8_printf(pState->out, "%s is NOT compatible (root pages)\n", zDb);
         6392  +      }
         6393  +      bFailed = 1;
         6394  +      if( peFix ) *peFix = 1;
         6395  +    }
         6396  +    shellFinalize(&rc, pStmt);
         6397  +  }
         6398  +
         6399  +  if( bFailed==0 ){
         6400  +    shellPreparePrintf(pState->db, &rc, &pStmt, 
         6401  +        "SELECT 1 WHERE ("
         6402  +        "  SELECT group_concat(rootpage || '.' || name || '.' || sql, '.') "
         6403  +        "  FROM _shared_schema_tmp.sqlite_master"
         6404  +        ") IS NOT ("
         6405  +        "  SELECT group_concat(rootpage || '.' || name || '.' || sql, '.') "
         6406  +        "  FROM main.sqlite_master"
         6407  +        ")"
         6408  +    );
         6409  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6410  +      if( peFix==0 ){
         6411  +        utf8_printf(pState->out, 
         6412  +            "%s is NOT compatible (order of sqlite_master rows)\n", zDb
         6413  +        );
         6414  +      }
         6415  +      bFailed = 1;
         6416  +      if( peFix ) *peFix = 2;
         6417  +    }
         6418  +    shellFinalize(&rc, pStmt);
         6419  +  }
         6420  +
         6421  +  if( bFailed==0 ){
         6422  +    int iMain = -1;
         6423  +    int iNew = +1;
         6424  +    shellPreparePrintf(pState->db, &rc, &pStmt, 
         6425  +        "PRAGMA main.schema_version"
         6426  +    );
         6427  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6428  +      iMain = sqlite3_column_int(pStmt, 0);
         6429  +    }
         6430  +    shellFinalize(&rc, pStmt);
         6431  +    shellPreparePrintf(pState->db, &rc, &pStmt, 
         6432  +        "PRAGMA _shared_schema_tmp.schema_version"
         6433  +    );
         6434  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6435  +      iNew = sqlite3_column_int(pStmt, 0);
         6436  +    }
         6437  +    shellFinalize(&rc, pStmt);
         6438  +    if( rc==SQLITE_OK && iMain!=iNew ){
         6439  +      if( peFix==0 ){
         6440  +        utf8_printf(pState->out, 
         6441  +            "%s is NOT compatible (schema cookie)\n", zDb
         6442  +        );
         6443  +      }
         6444  +      bFailed = 1;
         6445  +      if( peFix ) *peFix = 3;
         6446  +    }
         6447  +  }
         6448  +
         6449  +  if( rc==SQLITE_OK && bFailed==0 ){
         6450  +    utf8_printf(pState->out, "%s is compatible\n", zDb);
         6451  +  }
         6452  +
         6453  +  sqlite3_exec(pState->db, "DETACH _shared_schema_tmp", 0, 0, 0);
         6454  +  return rc;
         6455  +}
         6456  +
         6457  +/*
         6458  +** .shared-schema check|fix DB1 DB2...
         6459  +*/
         6460  +static int sharedSchemaDotCommand(
         6461  +  ShellState *pState,             /* Current shell tool state */
         6462  +  char **azArg,                   /* Array of arguments passed to dot command */
         6463  +  int nArg                        /* Number of entries in azArg[] */
         6464  +){
         6465  +  int rc = SQLITE_OK;
         6466  +  int bFix = 0;                   /* Fix databases if possible */
         6467  +  int n1;
         6468  +  int i;
         6469  +  if( nArg<3 ){
         6470  +    goto shared_schema_usage;
         6471  +  }
         6472  +
         6473  +  n1 = (int)strlen(azArg[1]);
         6474  +  if( n1>0 && n1<=3 && memcmp("fix", azArg[1], n1)==0 ){
         6475  +    bFix = 1;
         6476  +  }else if( n1==0 || n1>5 || memcmp("check", azArg[1], n1) ){
         6477  +    goto shared_schema_usage;
         6478  +  }
         6479  +
         6480  +  for(i=2; rc==SQLITE_OK && i<nArg; i++){
         6481  +    int eFix = 0;
         6482  +    rc = sharedSchemaCheck(pState, azArg[i], bFix ? &eFix : 0);
         6483  +    if( rc==SQLITE_OK && bFix && eFix ){
         6484  +      utf8_printf(pState->out, "Fixing %s... ", azArg[i]);
         6485  +      fflush(pState->out);
         6486  +      rc = sharedSchemaFix(pState, azArg[i], eFix);
         6487  +      if( rc==SQLITE_OK ){
         6488  +        rc = sharedSchemaCheck(pState, azArg[i], &eFix);
         6489  +        if( rc==SQLITE_OK && eFix ){
         6490  +          utf8_printf(pState->out, "VACUUMing main... ");
         6491  +          fflush(pState->out);
         6492  +          rc = sqlite3_exec(pState->db, "VACUUM main", 0, 0, 0);
         6493  +          if( rc==SQLITE_OK ){
         6494  +            rc = sharedSchemaCheck(pState, azArg[i], 0);
         6495  +          }
         6496  +        }
         6497  +      }
         6498  +    }
         6499  +  }
         6500  +
         6501  +  return rc;
         6502  + shared_schema_usage:
         6503  +  raw_printf(stderr, "usage: .shared-schema check|fix DB1 DB2...\n");
         6504  +  return SQLITE_ERROR;
         6505  +}
         6506  +
         6507  +/*
         6508  +** When running the ".recover" command, each output table, and the special
         6509  +** orphaned row table if it is required, is represented by an instance
         6510  +** of the following struct.
         6511  +*/
         6512  +typedef struct RecoverTable RecoverTable;
         6513  +struct RecoverTable {
         6514  +  char *zQuoted;                  /* Quoted version of table name */
         6515  +  int nCol;                       /* Number of columns in table */
         6516  +  char **azlCol;                  /* Array of column lists */
         6517  +  int iPk;                        /* Index of IPK column */
         6518  +};
         6519  +
         6520  +/*
         6521  +** Free a RecoverTable object allocated by recoverFindTable() or
         6522  +** recoverOrphanTable().
         6523  +*/
         6524  +static void recoverFreeTable(RecoverTable *pTab){
         6525  +  if( pTab ){
         6526  +    sqlite3_free(pTab->zQuoted);
         6527  +    if( pTab->azlCol ){
         6528  +      int i;
         6529  +      for(i=0; i<=pTab->nCol; i++){
         6530  +        sqlite3_free(pTab->azlCol[i]);
         6531  +      }
         6532  +      sqlite3_free(pTab->azlCol);
         6533  +    }
         6534  +    sqlite3_free(pTab);
         6535  +  }
         6536  +}
         6537  +
         6538  +/*
         6539  +** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
         6540  +** Otherwise, it allocates and returns a RecoverTable object based on the
         6541  +** final four arguments passed to this function. It is the responsibility
         6542  +** of the caller to eventually free the returned object using
         6543  +** recoverFreeTable().
         6544  +*/
         6545  +static RecoverTable *recoverNewTable(
         6546  +  int *pRc,                       /* IN/OUT: Error code */
         6547  +  const char *zName,              /* Name of table */
         6548  +  const char *zSql,               /* CREATE TABLE statement */
         6549  +  int bIntkey, 
         6550  +  int nCol
         6551  +){
         6552  +  sqlite3 *dbtmp = 0;             /* sqlite3 handle for testing CREATE TABLE */
         6553  +  int rc = *pRc;
         6554  +  RecoverTable *pTab = 0;
         6555  +
         6556  +  pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
         6557  +  if( rc==SQLITE_OK ){
         6558  +    int nSqlCol = 0;
         6559  +    int bSqlIntkey = 0;
         6560  +    sqlite3_stmt *pStmt = 0;
         6561  +    
         6562  +    rc = sqlite3_open("", &dbtmp);
         6563  +    if( rc==SQLITE_OK ){
         6564  +      rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
         6565  +    }
         6566  +    if( rc==SQLITE_OK ){
         6567  +      rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
         6568  +      if( rc==SQLITE_ERROR ){
         6569  +        rc = SQLITE_OK;
         6570  +        goto finished;
         6571  +      }
         6572  +    }
         6573  +    shellPreparePrintf(dbtmp, &rc, &pStmt, 
         6574  +        "SELECT count(*) FROM pragma_table_info(%Q)", zName
         6575  +    );
         6576  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6577  +      nSqlCol = sqlite3_column_int(pStmt, 0);
         6578  +    }
         6579  +    shellFinalize(&rc, pStmt);
         6580  +
         6581  +    if( rc!=SQLITE_OK || nSqlCol<nCol ){
         6582  +      goto finished;
         6583  +    }
         6584  +
         6585  +    shellPreparePrintf(dbtmp, &rc, &pStmt, 
         6586  +      "SELECT ("
         6587  +      "  SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
         6588  +      ") FROM sqlite_master WHERE name = %Q", zName
         6589  +    );
         6590  +    if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6591  +      bSqlIntkey = sqlite3_column_int(pStmt, 0);
         6592  +    }
         6593  +    shellFinalize(&rc, pStmt);
         6594  +
         6595  +    if( bIntkey==bSqlIntkey ){
         6596  +      int i;
         6597  +      const char *zPk = "_rowid_";
         6598  +      sqlite3_stmt *pPkFinder = 0;
         6599  +
         6600  +      /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
         6601  +      ** set zPk to the name of the PK column, and pTab->iPk to the index
         6602  +      ** of the column, where columns are 0-numbered from left to right.
         6603  +      ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
         6604  +      ** leave zPk as "_rowid_" and pTab->iPk at -2.  */
         6605  +      pTab->iPk = -2;
         6606  +      if( bIntkey ){
         6607  +        shellPreparePrintf(dbtmp, &rc, &pPkFinder, 
         6608  +          "SELECT cid, name FROM pragma_table_info(%Q) "
         6609  +          "  WHERE pk=1 AND type='integer' COLLATE nocase"
         6610  +          "  AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
         6611  +          , zName, zName
         6612  +        );
         6613  +        if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
         6614  +          pTab->iPk = sqlite3_column_int(pPkFinder, 0);
         6615  +          zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
         6616  +        }
         6617  +      }
         6618  +
         6619  +      pTab->zQuoted = shellMPrintf(&rc, "%Q", zName);
         6620  +      pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
         6621  +      pTab->nCol = nSqlCol;
         6622  +
         6623  +      if( bIntkey ){
         6624  +        pTab->azlCol[0] = shellMPrintf(&rc, "%Q", zPk);
         6625  +      }else{
         6626  +        pTab->azlCol[0] = shellMPrintf(&rc, "");
         6627  +      }
         6628  +      i = 1;
         6629  +      shellPreparePrintf(dbtmp, &rc, &pStmt, 
         6630  +          "SELECT %Q || group_concat(name, ', ') "
         6631  +          "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
         6632  +          "FROM pragma_table_info(%Q)", 
         6633  +          bIntkey ? ", " : "", pTab->iPk, 
         6634  +          bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
         6635  +          zName
         6636  +      );
         6637  +      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6638  +        const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
         6639  +        pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
         6640  +        i++;
         6641  +      }
         6642  +      shellFinalize(&rc, pStmt);
         6643  +
         6644  +      shellFinalize(&rc, pPkFinder);
         6645  +    }
         6646  +  }
         6647  +
         6648  + finished:
         6649  +  sqlite3_close(dbtmp);
         6650  +  *pRc = rc;
         6651  +  if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
         6652  +    recoverFreeTable(pTab);
         6653  +    pTab = 0;
         6654  +  }
         6655  +  return pTab;
         6656  +}
         6657  +
         6658  +/*
         6659  +** This function is called to search the schema recovered from the
         6660  +** sqlite_master table of the (possibly) corrupt database as part
         6661  +** of a ".recover" command. Specifically, for a table with root page
         6662  +** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
         6663  +** table must be a WITHOUT ROWID table, or if non-zero, not one of
         6664  +** those.
         6665  +**
         6666  +** If a table is found, a (RecoverTable*) object is returned. Or, if
         6667  +** no such table is found, but bIntkey is false and iRoot is the 
         6668  +** root page of an index in the recovered schema, then (*pbNoop) is
         6669  +** set to true and NULL returned. Or, if there is no such table or
         6670  +** index, NULL is returned and (*pbNoop) set to 0, indicating that
         6671  +** the caller should write data to the orphans table.
         6672  +*/
         6673  +static RecoverTable *recoverFindTable(
         6674  +  ShellState *pState,             /* Shell state object */
         6675  +  int *pRc,                       /* IN/OUT: Error code */
         6676  +  int iRoot,                      /* Root page of table */
         6677  +  int bIntkey,                    /* True for an intkey table */
         6678  +  int nCol,                       /* Number of columns in table */
         6679  +  int *pbNoop                     /* OUT: True if iRoot is root of index */
         6680  +){
         6681  +  sqlite3_stmt *pStmt = 0;
         6682  +  RecoverTable *pRet = 0;
         6683  +  int bNoop = 0;
         6684  +  const char *zSql = 0;
         6685  +  const char *zName = 0;
         6686  +
         6687  +  /* Search the recovered schema for an object with root page iRoot. */
         6688  +  shellPreparePrintf(pState->db, pRc, &pStmt,
         6689  +      "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
         6690  +  );
         6691  +  while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6692  +    const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
         6693  +    if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
         6694  +      bNoop = 1;
         6695  +      break;
         6696  +    }
         6697  +    if( sqlite3_stricmp(zType, "table")==0 ){
         6698  +      zName = (const char*)sqlite3_column_text(pStmt, 1);
         6699  +      zSql = (const char*)sqlite3_column_text(pStmt, 2);
         6700  +      pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
         6701  +      break;
         6702  +    }
         6703  +  }
         6704  +
         6705  +  shellFinalize(pRc, pStmt);
         6706  +  *pbNoop = bNoop;
         6707  +  return pRet;
         6708  +}
         6709  +
         6710  +/*
         6711  +** Return a RecoverTable object representing the orphans table.
         6712  +*/
         6713  +static RecoverTable *recoverOrphanTable(
         6714  +  ShellState *pState,             /* Shell state object */
         6715  +  int *pRc,                       /* IN/OUT: Error code */
         6716  +  const char *zLostAndFound,      /* Base name for orphans table */
         6717  +  int nCol                        /* Number of user data columns */
         6718  +){
         6719  +  RecoverTable *pTab = 0;
         6720  +  if( nCol>=0 && *pRc==SQLITE_OK ){
         6721  +    int i;
         6722  +
         6723  +    /* This block determines the name of the orphan table. The prefered
         6724  +    ** name is zLostAndFound. But if that clashes with another name
         6725  +    ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
         6726  +    ** and so on until a non-clashing name is found.  */
         6727  +    int iTab = 0;
         6728  +    char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
         6729  +    sqlite3_stmt *pTest = 0;
         6730  +    shellPrepare(pState->db, pRc,
         6731  +        "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
         6732  +    );
         6733  +    if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
         6734  +    while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
         6735  +      shellReset(pRc, pTest);
         6736  +      sqlite3_free(zTab);
         6737  +      zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
         6738  +      sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
         6739  +    }
         6740  +    shellFinalize(pRc, pTest);
         6741  +
         6742  +    pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
         6743  +    if( pTab ){
         6744  +      pTab->zQuoted = shellMPrintf(pRc, "%Q", zTab);
         6745  +      pTab->nCol = nCol;
         6746  +      pTab->iPk = -2;
         6747  +      if( nCol>0 ){
         6748  +        pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
         6749  +        if( pTab->azlCol ){
         6750  +          pTab->azlCol[nCol] = shellMPrintf(pRc, "");
         6751  +          for(i=nCol-1; i>=0; i--){
         6752  +            pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
         6753  +          }
         6754  +        }
         6755  +      }
         6756  +
         6757  +      if( *pRc!=SQLITE_OK ){
         6758  +        recoverFreeTable(pTab);
         6759  +        pTab = 0;
         6760  +      }else{
         6761  +        raw_printf(pState->out, 
         6762  +            "CREATE TABLE %s(rootpgno INTEGER, "
         6763  +            "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
         6764  +        );
         6765  +        for(i=0; i<nCol; i++){
         6766  +          raw_printf(pState->out, ", c%d", i);
         6767  +        }
         6768  +        raw_printf(pState->out, ");\n");
         6769  +      }
         6770  +    }
         6771  +    sqlite3_free(zTab);
         6772  +  }
         6773  +  return pTab;
         6774  +}
         6775  +
         6776  +/*
         6777  +** This function is called to recover data from the database. A script
         6778  +** to construct a new database containing all recovered data is output
         6779  +** on stream pState->out.
         6780  +*/
         6781  +static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
         6782  +  int rc = SQLITE_OK;
         6783  +  sqlite3_stmt *pLoop = 0;        /* Loop through all root pages */
         6784  +  sqlite3_stmt *pPages = 0;       /* Loop through all pages in a group */
         6785  +  sqlite3_stmt *pCells = 0;       /* Loop through all cells in a page */
         6786  +  const char *zRecoveryDb = "";   /* Name of "recovery" database */
         6787  +  const char *zLostAndFound = "lost_and_found";
         6788  +  int i;
         6789  +  int nOrphan = -1;
         6790  +  RecoverTable *pOrphan = 0;
         6791  +
         6792  +  int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
         6793  +  for(i=1; i<nArg; i++){
         6794  +    char *z = azArg[i];
         6795  +    int n;
         6796  +    if( z[0]=='-' && z[1]=='-' ) z++;
         6797  +    n = strlen(z);
         6798  +    if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
         6799  +      bFreelist = 0;
         6800  +    }else
         6801  +    if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
         6802  +      i++;
         6803  +      zRecoveryDb = azArg[i];
         6804  +    }else
         6805  +    if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
         6806  +      i++;
         6807  +      zLostAndFound = azArg[i];
         6808  +    }
         6809  +    else{
         6810  +      raw_printf(stderr, "unexpected option: %s\n", azArg[i]); 
         6811  +      raw_printf(stderr, "options are:\n");
         6812  +      raw_printf(stderr, "    --freelist-corrupt\n");
         6813  +      raw_printf(stderr, "    --recovery-db DATABASE\n");
         6814  +      raw_printf(stderr, "    --lost-and-found TABLE-NAME\n");
         6815  +      return 1;
         6816  +    }
         6817  +  }
         6818  +
         6819  +  shellExecPrintf(pState->db, &rc,
         6820  +    /* Attach an in-memory database named 'recovery'. Create an indexed 
         6821  +    ** cache of the sqlite_dbptr virtual table. */
         6822  +    "ATTACH %Q AS recovery;"
         6823  +    "DROP TABLE IF EXISTS recovery.dbptr;"
         6824  +    "DROP TABLE IF EXISTS recovery.freelist;"
         6825  +    "DROP TABLE IF EXISTS recovery.map;"
         6826  +    "DROP TABLE IF EXISTS recovery.schema;"
         6827  +    "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
         6828  +  );
         6829  +
         6830  +  if( bFreelist ){
         6831  +    shellExec(pState->db, &rc,
         6832  +      "WITH trunk(pgno) AS ("
         6833  +      "  SELECT shell_int32("
         6834  +      "      (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
         6835  +      "      WHERE x>0"
         6836  +      "    UNION"
         6837  +      "  SELECT shell_int32("
         6838  +      "      (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
         6839  +      "      FROM trunk WHERE x>0"
         6840  +      "),"
         6841  +      "freelist(data, n, freepgno) AS ("
         6842  +      "  SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
         6843  +      "      FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
         6844  +      "    UNION ALL"
         6845  +      "  SELECT data, n-1, shell_int32(data, 2+n) "
         6846  +      "      FROM freelist WHERE n>=0"
         6847  +      ")"
         6848  +      "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
         6849  +    );
         6850  +  }
         6851  +
         6852  +  shellExec(pState->db, &rc, 
         6853  +    "CREATE TABLE recovery.dbptr("
         6854  +    "      pgno, child, PRIMARY KEY(child, pgno)"
         6855  +    ") WITHOUT ROWID;"
         6856  +    "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
         6857  +    "    SELECT * FROM sqlite_dbptr"
         6858  +    "      WHERE pgno NOT IN freelist AND child NOT IN freelist;"
         6859  +
         6860  +    /* Delete any pointer to page 1. This ensures that page 1 is considered
         6861  +    ** a root page, regardless of how corrupt the db is. */
         6862  +    "DELETE FROM recovery.dbptr WHERE child = 1;"
         6863  +
         6864  +    /* Delete all pointers to any pages that have more than one pointer
         6865  +    ** to them. Such pages will be treated as root pages when recovering
         6866  +    ** data.  */
         6867  +    "DELETE FROM recovery.dbptr WHERE child IN ("
         6868  +    "  SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
         6869  +    ");"
         6870  +
         6871  +    /* Create the "map" table that will (eventually) contain instructions
         6872  +    ** for dealing with each page in the db that contains one or more 
         6873  +    ** records. */
         6874  +    "CREATE TABLE recovery.map("
         6875  +      "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
         6876  +    ");"
         6877  +
         6878  +    /* Populate table [map]. If there are circular loops of pages in the
         6879  +    ** database, the following adds all pages in such a loop to the map
         6880  +    ** as individual root pages. This could be handled better.  */
         6881  +    "WITH pages(i, maxlen) AS ("
         6882  +    "  SELECT page_count, ("
         6883  +    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
         6884  +    "  ) FROM pragma_page_count WHERE page_count>0"
         6885  +    "    UNION ALL"
         6886  +    "  SELECT i-1, ("
         6887  +    "    SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
         6888  +    "  ) FROM pages WHERE i>=2"
         6889  +    ")"
         6890  +    "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
         6891  +    "  SELECT i, maxlen, NULL, ("
         6892  +    "    WITH p(orig, pgno, parent) AS ("
         6893  +    "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
         6894  +    "        UNION "
         6895  +    "      SELECT i, p.parent, "
         6896  +    "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
         6897  +    "    )"
         6898  +    "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
         6899  +    ") "
         6900  +    "FROM pages WHERE maxlen > 0 AND i NOT IN freelist;"
         6901  +    "UPDATE recovery.map AS o SET intkey = ("
         6902  +    "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
         6903  +    ");"
         6904  +
         6905  +    /* Extract data from page 1 and any linked pages into table
         6906  +    ** recovery.schema. With the same schema as an sqlite_master table.  */
         6907  +    "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
         6908  +    "INSERT INTO recovery.schema SELECT "
         6909  +    "  max(CASE WHEN field=0 THEN value ELSE NULL END),"
         6910  +    "  max(CASE WHEN field=1 THEN value ELSE NULL END),"
         6911  +    "  max(CASE WHEN field=2 THEN value ELSE NULL END),"
         6912  +    "  max(CASE WHEN field=3 THEN value ELSE NULL END),"
         6913  +    "  max(CASE WHEN field=4 THEN value ELSE NULL END)"
         6914  +    "FROM sqlite_dbdata WHERE pgno IN ("
         6915  +    "  SELECT pgno FROM recovery.map WHERE root=1"
         6916  +    ")"
         6917  +    "GROUP BY pgno, cell;"
         6918  +    "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
         6919  +  );
         6920  +
         6921  +  /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 
         6922  +  ** CREATE TABLE statements that extracted from the existing schema.  */
         6923  +  if( rc==SQLITE_OK ){
         6924  +    sqlite3_stmt *pStmt = 0;
         6925  +    raw_printf(pState->out, "BEGIN;\n");
         6926  +    raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
         6927  +    shellPrepare(pState->db, &rc,
         6928  +        "SELECT sql FROM recovery.schema "
         6929  +        "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
         6930  +    );
         6931  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         6932  +      const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
         6933  +      raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n", 
         6934  +          &zCreateTable[12]
         6935  +      );
         6936  +    }
         6937  +    shellFinalize(&rc, pStmt);
         6938  +  }
         6939  +
         6940  +  /* Figure out if an orphan table will be required. And if so, how many
         6941  +  ** user columns it should contain */
         6942  +  shellPrepare(pState->db, &rc, 
         6943  +      "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
         6944  +      , &pLoop
         6945  +  );
         6946  +  if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
         6947  +    nOrphan = sqlite3_column_int(pLoop, 0);
         6948  +  }
         6949  +  shellFinalize(&rc, pLoop);
         6950  +  pLoop = 0;
         6951  +
         6952  +  shellPrepare(pState->db, &rc,
         6953  +      "SELECT pgno FROM recovery.map WHERE root=?", &pPages
         6954  +  );
         6955  +  shellPrepare(pState->db, &rc,
         6956  +      "SELECT max(field), group_concat(shell_escape_crnl(quote(value)), ', ')"
         6957  +      "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
         6958  +      "GROUP BY cell", &pCells
         6959  +  );
         6960  +
         6961  +  /* Loop through each root page. */
         6962  +  shellPrepare(pState->db, &rc, 
         6963  +      "SELECT root, intkey, max(maxlen) FROM recovery.map" 
         6964  +      " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
         6965  +      "  SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
         6966  +      ")", &pLoop
         6967  +  );
         6968  +  while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
         6969  +    int iRoot = sqlite3_column_int(pLoop, 0);
         6970  +    int bIntkey = sqlite3_column_int(pLoop, 1);
         6971  +    int nCol = sqlite3_column_int(pLoop, 2);
         6972  +    int bNoop = 0;
         6973  +    RecoverTable *pTab;
         6974  +
         6975  +    pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
         6976  +    if( bNoop || rc ) continue;
         6977  +    if( pTab==0 ){
         6978  +      if( pOrphan==0 ){
         6979  +        pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
         6980  +      }
         6981  +      pTab = pOrphan;
         6982  +      if( pTab==0 ) break;
         6983  +    }
         6984  +
         6985  +    if( 0==sqlite3_stricmp(pTab->zQuoted, "'sqlite_sequence'") ){
         6986  +      raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
         6987  +    }
         6988  +    sqlite3_bind_int(pPages, 1, iRoot);
         6989  +    sqlite3_bind_int(pCells, 2, pTab->iPk);
         6990  +
         6991  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
         6992  +      int iPgno = sqlite3_column_int(pPages, 0);
         6993  +      sqlite3_bind_int(pCells, 1, iPgno);
         6994  +      while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
         6995  +        int nField = sqlite3_column_int(pCells, 0);
         6996  +        const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
         6997  +
         6998  +        nField = nField+1;
         6999  +        if( pTab==pOrphan ){
         7000  +          raw_printf(pState->out, 
         7001  +              "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
         7002  +              pTab->zQuoted, iRoot, iPgno, nField, 
         7003  +              bIntkey ? "" : "NULL, ", zVal, pTab->azlCol[nField]
         7004  +          );
         7005  +        }else{
         7006  +          raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 
         7007  +              pTab->zQuoted, pTab->azlCol[nField], zVal
         7008  +          );
         7009  +        }
         7010  +      }
         7011  +      shellReset(&rc, pCells);
         7012  +    }
         7013  +    shellReset(&rc, pPages);
         7014  +    if( pTab!=pOrphan ) recoverFreeTable(pTab);
         7015  +  }
         7016  +  shellFinalize(&rc, pLoop);
         7017  +  shellFinalize(&rc, pPages);
         7018  +  shellFinalize(&rc, pCells);
         7019  +  recoverFreeTable(pOrphan);
         7020  +
         7021  +  /* The rest of the schema */
         7022  +  if( rc==SQLITE_OK ){
         7023  +    sqlite3_stmt *pStmt = 0;
         7024  +    shellPrepare(pState->db, &rc, 
         7025  +        "SELECT sql, name FROM recovery.schema "
         7026  +        "WHERE sql NOT LIKE 'create table%'", &pStmt
         7027  +    );
         7028  +    while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
         7029  +      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
         7030  +      if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
         7031  +        const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
         7032  +        char *zPrint = shellMPrintf(&rc, 
         7033  +          "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
         7034  +          zName, zName, zSql
         7035  +        );
         7036  +        raw_printf(pState->out, "%s;\n", zPrint);
         7037  +        sqlite3_free(zPrint);
         7038  +      }else{
         7039  +        raw_printf(pState->out, "%s;\n", zSql);
         7040  +      }
         7041  +    }
         7042  +    shellFinalize(&rc, pStmt);
         7043  +  }
         7044  +
         7045  +  if( rc==SQLITE_OK ){
         7046  +    raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
         7047  +    raw_printf(pState->out, "COMMIT;\n");
         7048  +  }
         7049  +  sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
         7050  +  return rc;
         7051  +}
         7052  +#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
         7053  +
  6291   7054   
  6292   7055   /*
  6293   7056   ** If an input line begins with "." then invoke this routine to
  6294   7057   ** process that line.
  6295   7058   **
  6296   7059   ** Return 1 on error, 2 to exit, and 0 otherwise.
  6297   7060   */
................................................................................
  6571   7334       }   
  6572   7335     }else
  6573   7336   
  6574   7337     if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
  6575   7338       rc = shell_dbinfo_command(p, nArg, azArg);
  6576   7339     }else
  6577   7340   
         7341  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
         7342  +  if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
         7343  +    open_db(p, 0);
         7344  +    rc = recoverDatabaseCmd(p, nArg, azArg);
         7345  +  }else
         7346  +#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
         7347  +
  6578   7348     if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
  6579   7349       const char *zLike = 0;
  6580   7350       int i;
  6581   7351       int savedShowHeader = p->showHeader;
  6582   7352       int savedShellFlags = p->shellFlgs;
  6583   7353       ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
  6584   7354       for(i=1; i<nArg; i++){
................................................................................
  6608   7378                              "?--newlines? ?LIKE-PATTERN?\n");
  6609   7379           rc = 1;
  6610   7380           goto meta_command_exit;
  6611   7381         }else{
  6612   7382           zLike = azArg[i];
  6613   7383         }
  6614   7384       }
         7385  +
  6615   7386       open_db(p, 0);
         7387  +
  6616   7388       /* When playing back a "dump", the content might appear in an order
  6617   7389       ** which causes immediate foreign key constraints to be violated.
  6618   7390       ** So disable foreign-key constraint enforcement to prevent problems. */
  6619   7391       raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
  6620   7392       raw_printf(p->out, "BEGIN TRANSACTION;\n");
  6621   7393       p->writableSchema = 0;
  6622   7394       p->showHeader = 0;

Changes to src/sqlite.h.in.

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

Changes to src/sqliteInt.h.

  2269   2269     unsigned bUnordered:1;   /* Use this index for == or IN queries only */
  2270   2270     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  2271   2271     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
  2272   2272     unsigned isCovering:1;   /* True if this is a covering index */
  2273   2273     unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
  2274   2274     unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
  2275   2275     unsigned bNoQuery:1;     /* Do not use this index to optimize queries */
         2276  +  unsigned bAscKeyBug:1;   /* True if the bba7b69f9849b5bf bug applies */
  2276   2277   #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  2277   2278     int nSample;             /* Number of elements in aSample[] */
  2278   2279     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
  2279   2280     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
  2280   2281     IndexSample *aSample;    /* Samples of the left-most key */
  2281   2282     tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
  2282   2283     tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
................................................................................
  2546   2547   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
  2547   2548   #define EP_Alias     0x400000 /* Is an alias for a result set column */
  2548   2549   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
  2549   2550   #define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
  2550   2551   #define EP_Subrtn   0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
  2551   2552   #define EP_Quoted   0x4000000 /* TK_ID was originally quoted */
  2552   2553   #define EP_Static   0x8000000 /* Held in memory not obtained from malloc() */
         2554  +#define EP_IsTrue  0x10000000 /* Always has boolean value of TRUE */
         2555  +#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
  2553   2556   
  2554   2557   /*
  2555   2558   ** The EP_Propagate mask is a set of properties that automatically propagate
  2556   2559   ** upwards into parent nodes.
  2557   2560   */
  2558   2561   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
  2559   2562   
................................................................................
  2561   2564   ** These macros can be used to test, set, or clear bits in the
  2562   2565   ** Expr.flags field.
  2563   2566   */
  2564   2567   #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
  2565   2568   #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
  2566   2569   #define ExprSetProperty(E,P)     (E)->flags|=(P)
  2567   2570   #define ExprClearProperty(E,P)   (E)->flags&=~(P)
         2571  +#define ExprAlwaysTrue(E)   (((E)->flags&(EP_FromJoin|EP_IsTrue))==EP_IsTrue)
         2572  +#define ExprAlwaysFalse(E)  (((E)->flags&(EP_FromJoin|EP_IsFalse))==EP_IsFalse)
  2568   2573   
  2569   2574   /* The ExprSetVVAProperty() macro is used for Verification, Validation,
  2570   2575   ** and Accreditation only.  It works like ExprSetProperty() during VVA
  2571   2576   ** processes but is a no-op for delivery.
  2572   2577   */
  2573   2578   #ifdef SQLITE_DEBUG
  2574   2579   # define ExprSetVVAProperty(E,P)  (E)->flags|=(P)
................................................................................
  3874   3879   int sqlite3NoTempsInRange(Parse*,int,int);
  3875   3880   #endif
  3876   3881   Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
  3877   3882   Expr *sqlite3Expr(sqlite3*,int,const char*);
  3878   3883   void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
  3879   3884   Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
  3880   3885   void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
  3881         -Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
         3886  +Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
         3887  +Expr *sqlite3ExprSimplifiedAndOr(Expr*);
  3882   3888   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
  3883   3889   void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
  3884   3890   void sqlite3ExprDelete(sqlite3*, Expr*);
  3885   3891   ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  3886   3892   ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
  3887   3893   void sqlite3ExprListSetSortOrder(ExprList*,int);
  3888   3894   void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
................................................................................
  4287   4293   
  4288   4294   const void *sqlite3ValueText(sqlite3_value*, u8);
  4289   4295   int sqlite3ValueBytes(sqlite3_value*, u8);
  4290   4296   void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
  4291   4297                           void(*)(void*));
  4292   4298   void sqlite3ValueSetNull(sqlite3_value*);
  4293   4299   void sqlite3ValueFree(sqlite3_value*);
         4300  +#ifndef SQLITE_UNTESTABLE
         4301  +void sqlite3ResultIntReal(sqlite3_context*);
         4302  +#endif
  4294   4303   sqlite3_value *sqlite3ValueNew(sqlite3 *);
  4295   4304   #ifndef SQLITE_OMIT_UTF16
  4296   4305   char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
  4297   4306   #endif
  4298   4307   int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
  4299   4308   void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
  4300   4309   #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);
  4275         -  free(zCopy);
         4297  +  if( objc>=5 ){
  4276   4298     zTail = &zSql[(zTail - zCopy)];
         4299  +  }
         4300  +  free(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 );
................................................................................
  7693   7717     unsigned char *a = 0;
  7694   7718     int n = 0;
  7695   7719     int lineno = 0;
  7696   7720     int i, iNext;
  7697   7721     int iOffset = 0;
  7698   7722     int j, k;
  7699   7723     int rc;
  7700         -  unsigned char x[16];
         7724  +  unsigned int x[16];
  7701   7725     if( objc!=2 ){
  7702   7726       Tcl_WrongNumArgs(interp, 1, objv, "HEXDB");
  7703   7727       return TCL_ERROR;
  7704   7728     }
  7705   7729     zIn = Tcl_GetString(objv[1]);
  7706   7730     for(i=0; zIn[i]; i=iNext){
  7707   7731       lineno++;
................................................................................
  7725   7749         continue;
  7726   7750       }
  7727   7751       rc = sscanf(zIn+i, "| page %d offset %d", &j, &k);
  7728   7752       if( rc==2 ){
  7729   7753         iOffset = k;
  7730   7754         continue;
  7731   7755       }
  7732         -    rc = sscanf(zIn+i,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
  7733         -                      "  %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
         7756  +    rc = sscanf(zIn+i,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
  7734   7757                   &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
  7735   7758                   &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
  7736   7759       if( rc==17 ){
  7737   7760         k = iOffset+j;
  7738   7761         if( k+16<=n ){
  7739         -        memcpy(a+k, x, 16);
         7762  +        int ii;
         7763  +        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
  7740   7764         }
  7741   7765         continue;
  7742   7766       }
  7743   7767     }
  7744   7768     Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(a, n));
  7745   7769     free(a);
  7746   7770     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;
................................................................................
   351    352     hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
   352    353   
   353    354     /* There is one entry in the aRegIdx[] array for each index on the table
   354    355     ** being updated.  Fill in aRegIdx[] with a register number that will hold
   355    356     ** the key for accessing each index.
   356    357     */
   357    358     if( onError==OE_Replace ) bReplace = 1;
   358         -  for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
          359  +  for(nAllIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nAllIdx++){
   359    360       int reg;
   360    361       if( chngKey || hasFK>1 || pIdx==pPk
   361    362        || indexWhereClauseMightChange(pIdx,aXRef,chngRowid)
   362    363       ){
   363    364         reg = ++pParse->nMem;
   364    365         pParse->nMem += pIdx->nColumn;
   365    366       }else{
................................................................................
   371    372             if( onError==OE_Default && pIdx->onError==OE_Replace ){
   372    373               bReplace = 1;
   373    374             }
   374    375             break;
   375    376           }
   376    377         }
   377    378       }
   378         -    if( reg==0 ) aToOpen[j+1] = 0;
   379         -    aRegIdx[j] = reg;
          379  +    if( reg==0 ) aToOpen[nAllIdx+1] = 0;
          380  +    aRegIdx[nAllIdx] = reg;
   380    381     }
          382  +  aRegIdx[nAllIdx] = ++pParse->nMem;  /* Register storing the table record */
   381    383     if( bReplace ){
   382    384       /* If REPLACE conflict resolution might be invoked, open cursors on all 
   383    385       ** indexes in case they are needed to delete records.  */
   384    386       memset(aToOpen, 1, nIdx+1);
   385    387     }
   386    388   
   387    389     /* Begin generating code. */
................................................................................
   388    390     v = sqlite3GetVdbe(pParse);
   389    391     if( v==0 ) goto update_cleanup;
   390    392     if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
   391    393     sqlite3BeginWriteOperation(pParse, pTrigger || hasFK, iDb);
   392    394   
   393    395     /* Allocate required registers. */
   394    396     if( !IsVirtual(pTab) ){
   395         -    regRowSet = ++pParse->nMem;
          397  +    /* For now, regRowSet and aRegIdx[nAllIdx] share the same register.
          398  +    ** If regRowSet turns out to be needed, then aRegIdx[nAllIdx] will be
          399  +    ** reallocated.  aRegIdx[nAllIdx] is the register in which the main
          400  +    ** table record is written.  regRowSet holds the RowSet for the
          401  +    ** two-pass update algorithm. */
          402  +    assert( aRegIdx[nAllIdx]==pParse->nMem );
          403  +    regRowSet = aRegIdx[nAllIdx];
   396    404       regOldRowid = regNewRowid = ++pParse->nMem;
   397    405       if( chngPk || pTrigger || hasFK ){
   398    406         regOld = pParse->nMem + 1;
   399    407         pParse->nMem += pTab->nCol;
   400    408       }
   401    409       if( chngKey || pTrigger || hasFK ){
   402    410         regNewRowid = ++pParse->nMem;
................................................................................
   518    526   
   519    527     if( HasRowid(pTab) ){
   520    528       /* Read the rowid of the current row of the WHERE scan. In ONEPASS_OFF
   521    529       ** mode, write the rowid into the FIFO. In either of the one-pass modes,
   522    530       ** leave it in register regOldRowid.  */
   523    531       sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
   524    532       if( eOnePass==ONEPASS_OFF ){
          533  +      /* We need to use regRowSet, so reallocate aRegIdx[nAllIdx] */
          534  +      aRegIdx[nAllIdx] = ++pParse->nMem;
   525    535         sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
   526    536       }
   527    537     }else{
   528    538       /* Read the PK of the current row into an array of registers. In
   529    539       ** ONEPASS_OFF mode, serialize the array into a record and store it in
   530    540       ** the ephemeral table. Or, in ONEPASS_SINGLE or MULTI mode, change
   531    541       ** 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     20   # include <math.h>
    22         -#endif
    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);
................................................................................
  3108   3150             for(ii=0; ii<db->nDb; ii++){
  3109   3151               rc = sqlite3BtreeTripAllCursors(db->aDb[ii].pBt,
  3110   3152                                          SQLITE_ABORT_ROLLBACK,
  3111   3153                                          isSchemaChange==0);
  3112   3154               if( rc!=SQLITE_OK ) goto abort_due_to_error;
  3113   3155             }
  3114   3156           }else{
         3157  +          assert( p1==SAVEPOINT_RELEASE );
  3115   3158             isSchemaChange = 0;
  3116   3159           }
  3117   3160           for(ii=0; ii<db->nDb; ii++){
  3118   3161             rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
  3119   3162             if( rc!=SQLITE_OK ){
  3120   3163               goto abort_due_to_error;
  3121   3164             }
................................................................................
  3144   3187           assert( pSavepoint==db->pSavepoint );
  3145   3188           db->pSavepoint = pSavepoint->pNext;
  3146   3189           sqlite3DbFree(db, pSavepoint);
  3147   3190           if( !isTransaction ){
  3148   3191             db->nSavepoint--;
  3149   3192           }
  3150   3193         }else{
         3194  +        assert( p1==SAVEPOINT_ROLLBACK );
  3151   3195           db->nDeferredCons = pSavepoint->nDeferredCons;
  3152   3196           db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
  3153   3197         }
  3154   3198   
  3155   3199         if( !isTransaction || p1==SAVEPOINT_ROLLBACK ){
  3156   3200           rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
  3157   3201           if( rc!=SQLITE_OK ) goto abort_due_to_error;
................................................................................
  3682   3726         SQLITE_OPEN_TRANSIENT_DB;
  3683   3727     assert( pOp->p1>=0 );
  3684   3728     assert( pOp->p2>=0 );
  3685   3729     pCx = p->apCsr[pOp->p1];
  3686   3730     if( pCx ){
  3687   3731       /* If the ephermeral table is already open, erase all existing content
  3688   3732       ** so that the table is empty again, rather than creating a new table. */
         3733  +    assert( pCx->isEphemeral );
         3734  +    if( pCx->pBtx ){
  3689   3735       rc = sqlite3BtreeClearTable(pCx->pBtx, pCx->pgnoRoot, 0);
         3736  +    }
  3690   3737     }else{
  3691   3738       pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, CURTYPE_BTREE);
  3692   3739       if( pCx==0 ) goto no_mem;
  3693   3740       pCx->nullRow = 1;
  3694   3741       pCx->isEphemeral = 1;
  3695   3742       rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBtx, 
  3696   3743                             BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5,
................................................................................
  3950   3997     oc = pOp->opcode;
  3951   3998     eqOnly = 0;
  3952   3999     pC->nullRow = 0;
  3953   4000   #ifdef SQLITE_DEBUG
  3954   4001     pC->seekOp = pOp->opcode;
  3955   4002   #endif
  3956   4003   
         4004  +  pC->deferredMoveto = 0;
         4005  +  pC->cacheStatus = CACHE_STALE;
  3957   4006     if( pC->isTable ){
  3958   4007       /* The BTREE_SEEK_EQ flag is only set on index cursors */
  3959   4008       assert( sqlite3BtreeCursorHasHint(pC->uc.pCursor, BTREE_SEEK_EQ)==0
  3960   4009                 || CORRUPT_DB );
  3961   4010   
  3962   4011       /* The input value in P3 might be of any type: integer, real, string,
  3963   4012       ** blob, or NULL.  But it needs to be an integer before we can do
  3964   4013       ** the seek, so convert it. */
  3965   4014       pIn3 = &aMem[pOp->p3];
  3966         -    if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_Str))==MEM_Str ){
         4015  +    if( (pIn3->flags & (MEM_Int|MEM_Real|MEM_IntReal|MEM_Str))==MEM_Str ){
  3967   4016         applyNumericAffinity(pIn3, 0);
  3968   4017       }
  3969   4018       iKey = sqlite3VdbeIntValue(pIn3);
  3970   4019   
  3971   4020       /* If the P3 value could not be converted into an integer without
  3972   4021       ** loss of information, then special processing is required... */
  3973         -    if( (pIn3->flags & MEM_Int)==0 ){
         4022  +    if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
  3974   4023         if( (pIn3->flags & MEM_Real)==0 ){
  3975         -        /* If the P3 value cannot be converted into any kind of a number,
  3976         -        ** then the seek is not possible, so jump to P2 */
         4024  +        if( (pIn3->flags & MEM_Null) || oc>=OP_SeekGE ){
  3977   4025           VdbeBranchTaken(1,2); goto jump_to_p2;
  3978   4026           break;
         4027  +        }else{
         4028  +          rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
         4029  +          if( rc!=SQLITE_OK ) goto abort_due_to_error;
         4030  +          goto seek_not_found;
  3979   4031         }
         4032  +      }else
  3980   4033   
  3981   4034         /* If the approximation iKey is larger than the actual real search
  3982   4035         ** term, substitute >= for > and < for <=. e.g. if the search term
  3983   4036         ** is 4.9 and the integer approximation 5:
  3984   4037         **
  3985   4038         **        (x >  4.9)    ->     (x >= 5)
  3986   4039         **        (x <= 4.9)    ->     (x <  5)
................................................................................
  4050   4103         goto abort_due_to_error;
  4051   4104       }
  4052   4105       if( eqOnly && r.eqSeen==0 ){
  4053   4106         assert( res!=0 );
  4054   4107         goto seek_not_found;
  4055   4108       }
  4056   4109     }
  4057         -  pC->deferredMoveto = 0;
  4058         -  pC->cacheStatus = CACHE_STALE;
  4059   4110   #ifdef SQLITE_TEST
  4060   4111     sqlite3_search_count++;
  4061   4112   #endif
  4062   4113     if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
  4063   4114       if( res<0 || (res==0 && oc==OP_SeekGT) ){
  4064   4115         res = 0;
  4065   4116         rc = sqlite3BtreeNext(pC->uc.pCursor, 0);
................................................................................
  4351   4402   case OP_SeekRowid: {        /* jump, in3 */
  4352   4403     VdbeCursor *pC;
  4353   4404     BtCursor *pCrsr;
  4354   4405     int res;
  4355   4406     u64 iKey;
  4356   4407   
  4357   4408     pIn3 = &aMem[pOp->p3];
  4358         -  if( (pIn3->flags & MEM_Int)==0 ){
         4409  +  testcase( pIn3->flags & MEM_Int );
         4410  +  testcase( pIn3->flags & MEM_IntReal );
         4411  +  if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
  4359   4412       /* Make sure pIn3->u.i contains a valid integer representation of
  4360   4413       ** the key value, but do not change the datatype of the register, as
  4361   4414       ** other parts of the perpared statement might be depending on the
  4362   4415       ** current datatype. */
  4363   4416       u16 origFlags = pIn3->flags;
  4364   4417       int isNotInt;
  4365   4418       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.

  1532   1532         sqlite3_str_appendf(&x, "%.16g", *pOp->p4.pReal);
  1533   1533         break;
  1534   1534       }
  1535   1535       case P4_MEM: {
  1536   1536         Mem *pMem = pOp->p4.pMem;
  1537   1537         if( pMem->flags & MEM_Str ){
  1538   1538           zP4 = pMem->z;
  1539         -      }else if( pMem->flags & MEM_Int ){
         1539  +      }else if( pMem->flags & (MEM_Int|MEM_IntReal) ){
  1540   1540           sqlite3_str_appendf(&x, "%lld", pMem->u.i);
  1541   1541         }else if( pMem->flags & MEM_Real ){
  1542   1542           sqlite3_str_appendf(&x, "%.16g", pMem->u.r);
  1543   1543         }else if( pMem->flags & MEM_Null ){
  1544   1544           zP4 = "NULL";
  1545   1545         }else{
  1546   1546           assert( pMem->flags & MEM_Blob );
................................................................................
  2894   2894             db->autoCommit = 1;
  2895   2895             p->nChange = 0;
  2896   2896           }
  2897   2897         }
  2898   2898       }
  2899   2899   
  2900   2900       /* Check for immediate foreign key violations. */
  2901         -    if( p->rc==SQLITE_OK ){
         2901  +    if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
  2902   2902         sqlite3VdbeCheckFk(p, 0);
  2903   2903       }
  2904   2904     
  2905   2905       /* If the auto-commit flag is set and this is the only active writer 
  2906   2906       ** VM, then we do either a commit or rollback of the current transaction. 
  2907   2907       **
  2908   2908       ** Note: This block also runs if one of the special errors handled 
................................................................................
  3420   3420   **
  3421   3421   ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
  3422   3422   ** of SQLite will not understand those serial types.
  3423   3423   */
  3424   3424   
  3425   3425   /*
  3426   3426   ** Return the serial-type for the value stored in pMem.
         3427  +**
         3428  +** This routine might convert a large MEM_IntReal value into MEM_Real.
  3427   3429   */
  3428   3430   u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
  3429   3431     int flags = pMem->flags;
  3430   3432     u32 n;
  3431   3433   
  3432   3434     assert( pLen!=0 );
  3433   3435     if( flags&MEM_Null ){
  3434   3436       *pLen = 0;
  3435   3437       return 0;
  3436   3438     }
  3437         -  if( flags&MEM_Int ){
         3439  +  if( flags&(MEM_Int|MEM_IntReal) ){
  3438   3440       /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
  3439   3441   #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
  3440   3442       i64 i = pMem->u.i;
  3441   3443       u64 u;
         3444  +    testcase( flags & MEM_Int );
         3445  +    testcase( flags & MEM_IntReal );
  3442   3446       if( i<0 ){
  3443   3447         u = ~i;
  3444   3448       }else{
  3445   3449         u = i;
  3446   3450       }
  3447   3451       if( u<=127 ){
  3448   3452         if( (i&1)==i && file_format>=4 ){
................................................................................
  3454   3458         }
  3455   3459       }
  3456   3460       if( u<=32767 ){ *pLen = 2; return 2; }
  3457   3461       if( u<=8388607 ){ *pLen = 3; return 3; }
  3458   3462       if( u<=2147483647 ){ *pLen = 4; return 4; }
  3459   3463       if( u<=MAX_6BYTE ){ *pLen = 6; return 5; }
  3460   3464       *pLen = 8;
         3465  +    if( flags&MEM_IntReal ){
         3466  +      /* If the value is IntReal and is going to take up 8 bytes to store
         3467  +      ** as an integer, then we might as well make it an 8-byte floating
         3468  +      ** point value */
         3469  +      pMem->u.r = (double)pMem->u.i;
         3470  +      pMem->flags &= ~MEM_IntReal;
         3471  +      pMem->flags |= MEM_Real;
         3472  +      return 7;
         3473  +    }
  3461   3474       return 6;
  3462   3475     }
  3463   3476     if( flags&MEM_Real ){
  3464   3477       *pLen = 8;
  3465   3478       return 7;
  3466   3479     }
  3467   3480     assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
................................................................................
  4109   4122     */
  4110   4123     if( combined_flags&MEM_Null ){
  4111   4124       return (f2&MEM_Null) - (f1&MEM_Null);
  4112   4125     }
  4113   4126   
  4114   4127     /* At least one of the two values is a number
  4115   4128     */
  4116         -  if( combined_flags&(MEM_Int|MEM_Real) ){
  4117         -    if( (f1 & f2 & MEM_Int)!=0 ){
         4129  +  if( combined_flags&(MEM_Int|MEM_Real|MEM_IntReal) ){
         4130  +    testcase( combined_flags & MEM_Int );
         4131  +    testcase( combined_flags & MEM_Real );
         4132  +    testcase( combined_flags & MEM_IntReal );
         4133  +    if( (f1 & f2 & (MEM_Int|MEM_IntReal))!=0 ){
         4134  +      testcase( f1 & f2 & MEM_Int );
         4135  +      testcase( f1 & f2 & MEM_IntReal );
  4118   4136         if( pMem1->u.i < pMem2->u.i ) return -1;
  4119   4137         if( pMem1->u.i > pMem2->u.i ) return +1;
  4120   4138         return 0;
  4121   4139       }
  4122   4140       if( (f1 & f2 & MEM_Real)!=0 ){
  4123   4141         if( pMem1->u.r < pMem2->u.r ) return -1;
  4124   4142         if( pMem1->u.r > pMem2->u.r ) return +1;
  4125   4143         return 0;
  4126   4144       }
  4127         -    if( (f1&MEM_Int)!=0 ){
         4145  +    if( (f1&(MEM_Int|MEM_IntReal))!=0 ){
         4146  +      testcase( f1 & MEM_Int );
         4147  +      testcase( f1 & MEM_IntReal );
  4128   4148         if( (f2&MEM_Real)!=0 ){
  4129   4149           return sqlite3IntFloatCompare(pMem1->u.i, pMem2->u.r);
         4150  +      }else if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
         4151  +        if( pMem1->u.i < pMem2->u.i ) return -1;
         4152  +        if( pMem1->u.i > pMem2->u.i ) return +1;
         4153  +        return 0;
  4130   4154         }else{
  4131   4155           return -1;
  4132   4156         }
  4133   4157       }
  4134   4158       if( (f1&MEM_Real)!=0 ){
  4135         -      if( (f2&MEM_Int)!=0 ){
         4159  +      if( (f2&(MEM_Int|MEM_IntReal))!=0 ){
         4160  +        testcase( f2 & MEM_Int );
         4161  +        testcase( f2 & MEM_IntReal );
  4136   4162           return -sqlite3IntFloatCompare(pMem2->u.i, pMem1->u.r);
  4137   4163         }else{
  4138   4164           return -1;
  4139   4165         }
  4140   4166       }
  4141   4167       return +1;
  4142   4168     }
................................................................................
  4277   4303     assert( pPKey2->pKeyInfo->aSortOrder!=0 );
  4278   4304     assert( pPKey2->pKeyInfo->nKeyField>0 );
  4279   4305     assert( idx1<=szHdr1 || CORRUPT_DB );
  4280   4306     do{
  4281   4307       u32 serial_type;
  4282   4308   
  4283   4309       /* RHS is an integer */
  4284         -    if( pRhs->flags & MEM_Int ){
         4310  +    if( pRhs->flags & (MEM_Int|MEM_IntReal) ){
         4311  +      testcase( pRhs->flags & MEM_Int );
         4312  +      testcase( pRhs->flags & MEM_IntReal );
  4285   4313         serial_type = aKey1[idx1];
  4286   4314         testcase( serial_type==12 );
  4287   4315         if( serial_type>=10 ){
  4288   4316           rc = +1;
  4289   4317         }else if( serial_type==0 ){
  4290   4318           rc = -1;
  4291   4319         }else if( serial_type==7 ){
................................................................................
  4622   4650       }
  4623   4651       if( (flags & MEM_Int) ){
  4624   4652         return vdbeRecordCompareInt;
  4625   4653       }
  4626   4654       testcase( flags & MEM_Real );
  4627   4655       testcase( flags & MEM_Null );
  4628   4656       testcase( flags & MEM_Blob );
  4629         -    if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
         4657  +    if( (flags & (MEM_Real|MEM_IntReal|MEM_Null|MEM_Blob))==0
         4658  +     && p->pKeyInfo->aColl[0]==0
         4659  +    ){
  4630   4660         assert( flags & MEM_Str );
  4631   4661         return vdbeRecordCompareString;
  4632   4662       }
  4633   4663     }
  4634   4664   
  4635   4665     return sqlite3VdbeRecordCompare;
  4636   4666   }

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;
         1841  +      if( iListOff>=0 ){
  1839   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.

   867    867         if( p->pVtab->nRef>0 ){
   868    868           return SQLITE_LOCKED;
   869    869         }
   870    870       }
   871    871       p = vtabDisconnectAll(db, pTab);
   872    872       xDestroy = p->pMod->pModule->xDestroy;
   873    873       assert( xDestroy!=0 );  /* Checked before the virtual table is created */
          874  +    pTab->nTabRef++;
   874    875       rc = xDestroy(p->pVtab);
   875    876       /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
   876    877       if( rc==SQLITE_OK ){
   877    878         assert( pTab->pVTable==p && p->pNext==0 );
   878    879         p->pVtab = 0;
   879    880         pTab->pVTable = 0;
   880    881         sqlite3VtabUnlock(p);
   881    882       }
          883  +    sqlite3DeleteTable(db, pTab);
   882    884     }
   883    885   
   884    886     return rc;
   885    887   }
   886    888   
   887    889   /*
   888    890   ** This function invokes either the xRollback or xCommit method

Changes to src/wal.c.

  2904   2904   
  2905   2905       rc = walHashGet(pWal, iHash, &sLoc);
  2906   2906       if( rc!=SQLITE_OK ){
  2907   2907         return rc;
  2908   2908       }
  2909   2909       nCollide = HASHTABLE_NSLOT;
  2910   2910       for(iKey=walHash(pgno); sLoc.aHash[iKey]; iKey=walNextHash(iKey)){
  2911         -      u32 iFrame = sLoc.aHash[iKey] + sLoc.iZero;
  2912         -      if( iFrame<=iLast && iFrame>=pWal->minFrame
  2913         -       && sLoc.aPgno[sLoc.aHash[iKey]]==pgno ){
         2911  +      u32 iH = sLoc.aHash[iKey];
         2912  +      u32 iFrame = iH + sLoc.iZero;
         2913  +      if( iFrame<=iLast && iFrame>=pWal->minFrame && sLoc.aPgno[iH]==pgno ){
  2914   2914           assert( iFrame>iRead || CORRUPT_DB );
  2915   2915           iRead = iFrame;
  2916   2916         }
  2917   2917         if( (nCollide--)==0 ){
  2918   2918           return SQLITE_CORRUPT_BKPT;
  2919   2919         }
  2920   2920       }

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.

  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   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
  1017   1018   do_execsql_test e_expr-16.1.2 { PRAGMA case_sensitive_like = 1 } {}
  1018   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
  1019   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
  1020   1023   do_execsql_test e_expr-16.1.5 { PRAGMA case_sensitive_like = 0 } {}
  1021   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
  1022   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.

  3910   3910   |   4064: 69 74 79 00 00 00 00 00 00 00 00 00 00 00 00 00   ity.............
  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  +
         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}}
  3917   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  +# 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 |}
   741    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}
           74  +    catch { for {set i 0} {$i < $::nOpen} {incr i} { dbh_$i close } }
           75  +  } $::rc
           76  +  if {$rc} {
    64     77     do_re_test 1.1.3 { 
    65     78       lindex $::log 0 
    66     79     } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - }
    67     80   }
           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
    74     88     set ::log [list]

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  +