/ Check-in [15593bf3]
Login

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

Overview
Comment:Bring the begin-concurrent-pnu-wal2 branch up to date with all the latest enhancements on trunk.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | begin-concurrent-pnu-wal2
Files: files | file ages | folders
SHA3-256: 15593bf3ad9650ce6a6ef8591240eba1be11974d3827cb86ebf0ecec3acda321
User & Date: drh 2019-09-16 12:29:47
Wiki:begin-concurrent-pnu-wal2
Context
2019-09-30
16:49
Merge in the 3.30.0 beta 1 enhancements. check-in: ebf88726 user: drh tags: begin-concurrent-pnu-wal2
2019-09-16
12:29
Bring the begin-concurrent-pnu-wal2 branch up to date with all the latest enhancements on trunk. check-in: 15593bf3 user: drh tags: begin-concurrent-pnu-wal2
12:23
Merge recent trunk enhancements into the wal2 branch. check-in: da8bc974 user: drh tags: wal2
2019-07-10
18:40
Merge the 3.29.0 updates into the begin-concurrent-pnu-wal2 branch. check-in: 627b428f user: drh tags: begin-concurrent-pnu-wal2
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   606    606   SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   607    607   SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   608    608   SHELL_OPT += -DSQLITE_ENABLE_STMTVTAB
   609    609   SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB
   610    610   SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB
   611    611   SHELL_OPT += -DSQLITE_ENABLE_OFFSET_SQL_FUNC
   612    612   SHELL_OPT += -DSQLITE_ENABLE_DESERIALIZE
   613         -SHELL_OPT += -DSQLITE_INTROSPECTION_PRAGMAS
   614    613   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   615    614   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5 -DSQLITE_OSS_FUZZ
   616    615   FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
   617    616   FUZZCHECK_OPT += -DSQLITE_PRINTF_PRECISION_LIMIT=1000
   618    617   FUZZCHECK_OPT += -DSQLITE_ENABLE_DESERIALIZE
   619    618   FUZZCHECK_OPT += -DSQLITE_ENABLE_FTS4
   620    619   #FUZZCHECK_OPT += -DSQLITE_ENABLE_FTS5

Changes to Makefile.linux-gcc.

    15     15   #    that contains this "Makefile.in" and the "configure.in" script.
    16     16   #
    17     17   TOP = ../sqlite
    18     18   
    19     19   #### C Compiler and options for use in building executables that
    20     20   #    will run on the platform that is doing the build.
    21     21   #
    22         -BCC = gcc -g -O2
           22  +BCC = gcc -g -O0
    23     23   #BCC = /opt/ancic/bin/c89 -0
    24     24   
    25     25   #### If the target operating system supports the "usleep()" system
    26     26   #    call, then define the HAVE_USLEEP macro for all C modules.
    27     27   #
    28     28   #USLEEP = 
    29     29   USLEEP = -DHAVE_USLEEP=1
................................................................................
    34     34   #
    35     35   #THREADSAFE = -DTHREADSAFE=1
    36     36   THREADSAFE = -DTHREADSAFE=0
    37     37   
    38     38   #### Specify any extra linker options needed to make the library
    39     39   #    thread safe
    40     40   #
    41         -#THREADLIB = -lpthread
    42         -THREADLIB = 
           41  +THREADLIB = -lpthread -lm -ldl
           42  +#THREADLIB = 
    43     43   
    44     44   #### Specify any extra libraries needed to access required functions.
    45     45   #
    46     46   #TLIBS = -lrt    # fdatasync on Solaris 8
    47     47   TLIBS = 
    48     48   
    49     49   #### Leave SQLITE_DEBUG undefined for maximum speed.  Use SQLITE_DEBUG=1
................................................................................
    50     50   #    to check for memory leaks.  Use SQLITE_DEBUG=2 to print a log of all
    51     51   #    malloc()s and free()s in order to track down memory leaks.
    52     52   #    
    53     53   #    SQLite uses some expensive assert() statements in the inner loop.
    54     54   #    You can make the library go almost twice as fast if you compile
    55     55   #    with -DNDEBUG=1
    56     56   #
    57         -#OPTS = -DSQLITE_DEBUG=2
    58         -#OPTS = -DSQLITE_DEBUG=1
    59         -#OPTS = 
    60         -OPTS = -DNDEBUG=1
    61         -OPTS += -DHAVE_FDATASYNC=1
           57  +OPTS += -DSQLITE_DEBUG=1
           58  +OPTS += -DSQLITE_ENABLE_WHERETRACE
           59  +OPTS += -DSQLITE_ENABLE_SELECTTRACE
    62     60   
    63     61   #### The suffix to add to executable files.  ".exe" for windows.
    64     62   #    Nothing for unix.
    65     63   #
    66     64   #EXE = .exe
    67     65   EXE =
    68     66   
    69     67   #### C Compile and options for use in building executables that 
    70     68   #    will run on the target platform.  This is usually the same
    71     69   #    as BCC, unless you are cross-compiling.
    72     70   #
    73         -TCC = gcc -O6
           71  +TCC = gcc -O0
    74     72   #TCC = gcc -g -O0 -Wall
    75     73   #TCC = gcc -g -O0 -Wall -fprofile-arcs -ftest-coverage
    76     74   #TCC = /opt/mingw/bin/i386-mingw32-gcc -O6
    77     75   #TCC = /opt/ansic/bin/c89 -O +z -Wl,-a,archive
    78     76   
    79     77   #### Tools used to build a static library.
    80     78   #
................................................................................
    87     85   SO = so
    88     86   SHPREFIX = lib
    89     87   # SO = dll
    90     88   # SHPREFIX =
    91     89   
    92     90   #### Extra compiler options needed for programs that use the TCL library.
    93     91   #
    94         -#TCL_FLAGS =
    95         -#TCL_FLAGS = -DSTATIC_BUILD=1
    96         -TCL_FLAGS = -I/home/drh/tcltk/8.5linux
    97         -#TCL_FLAGS = -I/home/drh/tcltk/8.5win -DSTATIC_BUILD=1
    98         -#TCL_FLAGS = -I/home/drh/tcltk/8.3hpux
           92  +TCL_FLAGS = -I/home/drh/tcl/include/tcl8.6
    99     93   
   100     94   #### Linker options needed to link against the TCL library.
   101     95   #
   102     96   #LIBTCL = -ltcl -lm -ldl
   103         -LIBTCL = /home/drh/tcltk/8.5linux/libtcl8.5g.a -lm -ldl
   104         -#LIBTCL = /home/drh/tcltk/8.5win/libtcl85s.a -lmsvcrt
   105         -#LIBTCL = /home/drh/tcltk/8.3hpux/libtcl8.3.a -ldld -lm -lc
           97  +LIBTCL = /home/drh/tcl/lib/libtcl8.6.a -lm -lpthread -ldl -lz
   106     98   
   107     99   #### Additional objects for SQLite library when TCL support is enabled.
   108    100   #TCLOBJ =
   109    101   TCLOBJ = tclsqlite.o
   110    102   
   111    103   #### Compiler options needed for programs that use the readline() library.
   112    104   #

Changes to Makefile.msc.

    69     69   # If necessary, create a list of harmless compiler warnings to disable when
    70     70   # compiling the various tools.  For the SQLite source code itself, warnings,
    71     71   # if any, will be disabled from within it.
    72     72   #
    73     73   !IFNDEF NO_WARN
    74     74   !IF $(USE_FULLWARN)!=0
    75     75   NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4130 -wd4152 -wd4189 -wd4206
    76         -NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4305 -wd4306 -wd4702 -wd4706
           76  +NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706
    77     77   !ENDIF
    78     78   !ENDIF
    79     79   
    80     80   # Set this non-0 to use the library paths and other options necessary for
    81     81   # Windows Phone 8.1.
    82     82   #
    83     83   !IFNDEF USE_WP81_OPTS
................................................................................
   347    347   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_FTS3=1
   348    348   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_RTREE=1
   349    349   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_GEOPOLY=1
   350    350   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_JSON1=1
   351    351   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_STMTVTAB=1
   352    352   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBPAGE_VTAB=1
   353    353   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DBSTAT_VTAB=1
   354         -OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_INTROSPECTION_PRAGMAS=1
   355    354   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_DESERIALIZE=1
   356    355   !ENDIF
   357    356   OPT_FEATURE_FLAGS = $(OPT_FEATURE_FLAGS) -DSQLITE_ENABLE_COLUMN_METADATA=1
   358    357   !ENDIF
   359    358   
   360    359   # Should the session extension be enabled?  If so, add compilation options
   361    360   # to enable it.

Changes to VERSION.

     1         -3.29.0
            1  +3.30.0

Changes to autoconf/Makefile.msc.

    69     69   # If necessary, create a list of harmless compiler warnings to disable when
    70     70   # compiling the various tools.  For the SQLite source code itself, warnings,
    71     71   # if any, will be disabled from within it.
    72     72   #
    73     73   !IFNDEF NO_WARN
    74     74   !IF $(USE_FULLWARN)!=0
    75     75   NO_WARN = -wd4054 -wd4055 -wd4100 -wd4127 -wd4130 -wd4152 -wd4189 -wd4206
    76         -NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4305 -wd4306 -wd4702 -wd4706
           76  +NO_WARN = $(NO_WARN) -wd4210 -wd4232 -wd4244 -wd4305 -wd4306 -wd4702 -wd4706
    77     77   !ENDIF
    78     78   !ENDIF
    79     79   
    80     80   # Set this non-0 to use the library paths and other options necessary for
    81     81   # Windows Phone 8.1.
    82     82   #
    83     83   !IFNDEF USE_WP81_OPTS

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.29.0.
            3  +# Generated by GNU Autoconf 2.69 for sqlite 3.30.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.29.0'
   730         -PACKAGE_STRING='sqlite 3.29.0'
          729  +PACKAGE_VERSION='3.30.0'
          730  +PACKAGE_STRING='sqlite 3.30.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
................................................................................
  1462   1462   #
  1463   1463   # Report the --help message.
  1464   1464   #
  1465   1465   if test "$ac_init_help" = "long"; then
  1466   1466     # Omit some internal or obsolete options to make the list less imposing.
  1467   1467     # This message is too long to be a string in the A/UX 3.1 sh.
  1468   1468     cat <<_ACEOF
  1469         -\`configure' configures sqlite 3.29.0 to adapt to many kinds of systems.
         1469  +\`configure' configures sqlite 3.30.0 to adapt to many kinds of systems.
  1470   1470   
  1471   1471   Usage: $0 [OPTION]... [VAR=VALUE]...
  1472   1472   
  1473   1473   To assign environment variables (e.g., CC, CFLAGS...), specify them as
  1474   1474   VAR=VALUE.  See below for descriptions of some of the useful variables.
  1475   1475   
  1476   1476   Defaults for the options are specified in brackets.
................................................................................
  1527   1527     --build=BUILD     configure for building on BUILD [guessed]
  1528   1528     --host=HOST       cross-compile to build programs to run on HOST [BUILD]
  1529   1529   _ACEOF
  1530   1530   fi
  1531   1531   
  1532   1532   if test -n "$ac_init_help"; then
  1533   1533     case $ac_init_help in
  1534         -     short | recursive ) echo "Configuration of sqlite 3.29.0:";;
         1534  +     short | recursive ) echo "Configuration of sqlite 3.30.0:";;
  1535   1535      esac
  1536   1536     cat <<\_ACEOF
  1537   1537   
  1538   1538   Optional Features:
  1539   1539     --disable-option-checking  ignore unrecognized --enable/--with options
  1540   1540     --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=no)
  1541   1541     --enable-FEATURE[=ARG]  include FEATURE [ARG=yes]
................................................................................
  1653   1653       cd "$ac_pwd" || { ac_status=$?; break; }
  1654   1654     done
  1655   1655   fi
  1656   1656   
  1657   1657   test -n "$ac_init_help" && exit $ac_status
  1658   1658   if $ac_init_version; then
  1659   1659     cat <<\_ACEOF
  1660         -sqlite configure 3.29.0
         1660  +sqlite configure 3.30.0
  1661   1661   generated by GNU Autoconf 2.69
  1662   1662   
  1663   1663   Copyright (C) 2012 Free Software Foundation, Inc.
  1664   1664   This configure script is free software; the Free Software Foundation
  1665   1665   gives unlimited permission to copy, distribute and modify it.
  1666   1666   _ACEOF
  1667   1667     exit
................................................................................
  2072   2072     eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
  2073   2073   
  2074   2074   } # ac_fn_c_check_header_mongrel
  2075   2075   cat >config.log <<_ACEOF
  2076   2076   This file contains any messages produced by compilers while
  2077   2077   running configure, to aid debugging if configure makes a mistake.
  2078   2078   
  2079         -It was created by sqlite $as_me 3.29.0, which was
         2079  +It was created by sqlite $as_me 3.30.0, which was
  2080   2080   generated by GNU Autoconf 2.69.  Invocation command line was
  2081   2081   
  2082   2082     $ $0 $@
  2083   2083   
  2084   2084   _ACEOF
  2085   2085   exec 5>>config.log
  2086   2086   {
................................................................................
 12228  12228   test $as_write_fail = 0 && chmod +x $CONFIG_STATUS || ac_write_fail=1
 12229  12229   
 12230  12230   cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1
 12231  12231   # Save the log message, to keep $0 and so on meaningful, and to
 12232  12232   # report actual input values of CONFIG_FILES etc. instead of their
 12233  12233   # values after options handling.
 12234  12234   ac_log="
 12235         -This file was extended by sqlite $as_me 3.29.0, which was
        12235  +This file was extended by sqlite $as_me 3.30.0, which was
 12236  12236   generated by GNU Autoconf 2.69.  Invocation command line was
 12237  12237   
 12238  12238     CONFIG_FILES    = $CONFIG_FILES
 12239  12239     CONFIG_HEADERS  = $CONFIG_HEADERS
 12240  12240     CONFIG_LINKS    = $CONFIG_LINKS
 12241  12241     CONFIG_COMMANDS = $CONFIG_COMMANDS
 12242  12242     $ $0 $@
................................................................................
 12294  12294   
 12295  12295   Report bugs to the package provider."
 12296  12296   
 12297  12297   _ACEOF
 12298  12298   cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1
 12299  12299   ac_cs_config="`$as_echo "$ac_configure_args" | sed 's/^ //; s/[\\""\`\$]/\\\\&/g'`"
 12300  12300   ac_cs_version="\\
 12301         -sqlite config.status 3.29.0
        12301  +sqlite config.status 3.30.0
 12302  12302   configured by $0, generated by GNU Autoconf 2.69,
 12303  12303     with options \\"\$ac_cs_config\\"
 12304  12304   
 12305  12305   Copyright (C) 2012 Free Software Foundation, Inc.
 12306  12306   This config.status script is free software; the Free Software Foundation
 12307  12307   gives unlimited permission to copy, distribute and modify it."
 12308  12308   

Changes to ext/expert/expert1.test.

   130    130   } {
   131    131     SELECT a FROM t1 WHERE a=? ORDER BY b;
   132    132   } {
   133    133     CREATE INDEX t1_idx_000123a7 ON t1(a, b);
   134    134     SEARCH TABLE t1 USING COVERING INDEX t1_idx_000123a7 (a=?)
   135    135   }
   136    136   
          137  +if 0 {
   137    138   do_setup_rec_test $tn.6 {
   138    139     CREATE TABLE t1(a, b, c);
   139    140   } {
   140    141     SELECT min(a) FROM t1
   141    142   } {
   142    143     CREATE INDEX t1_idx_00000061 ON t1(a);
   143    144     SEARCH TABLE t1 USING COVERING INDEX t1_idx_00000061
          145  +}
   144    146   }
   145    147   
   146    148   do_setup_rec_test $tn.7 {
   147    149     CREATE TABLE t1(a, b, c);
   148    150   } {
   149    151     SELECT * FROM t1 ORDER BY a, b, c;
   150    152   } {

Changes to ext/fts3/fts3_snippet.c.

   429    429       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
   430    430       if( pPhrase->pTail ){
   431    431         char *pCsr = pPhrase->pTail;
   432    432         int iCsr = pPhrase->iTail;
   433    433   
   434    434         while( iCsr<(iStart+pIter->nSnippet) && iCsr>=iStart ){
   435    435           int j;
   436         -        u64 mPhrase = (u64)1 << i;
          436  +        u64 mPhrase = (u64)1 << (i%64);
   437    437           u64 mPos = (u64)1 << (iCsr - iStart);
   438    438           assert( iCsr>=iStart && (iCsr - iStart)<=64 );
   439         -        assert( i>=0 && i<=64 );
          439  +        assert( i>=0 );
   440    440           if( (mCover|mCovered)&mPhrase ){
   441    441             iScore++;
   442    442           }else{
   443    443             iScore += 1000;
   444    444           }
   445    445           mCover |= mPhrase;
   446    446   

Changes to ext/fts3/fts3_write.c.

  3937   3937     assert_fts3_nc( (pNode->a[0]=='\0')==(aDoclist!=0) );
  3938   3938   
  3939   3939     blobGrowBuffer(pPrev, nTerm, &rc);
  3940   3940     if( rc!=SQLITE_OK ) return rc;
  3941   3941   
  3942   3942     nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
  3943   3943     nSuffix = nTerm - nPrefix;
         3944  +  if( nSuffix<=0 ) return FTS_CORRUPT_VTAB;
  3944   3945     memcpy(pPrev->a, zTerm, nTerm);
  3945   3946     pPrev->n = nTerm;
  3946   3947   
  3947   3948     if( bFirst==0 ){
  3948   3949       pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
  3949   3950     }
  3950   3951     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);

Changes to ext/fts5/fts5Int.h.

   174    174     char *zContent;                 /* content table */ 
   175    175     char *zContentRowid;            /* "content_rowid=" option value */ 
   176    176     int bColumnsize;                /* "columnsize=" option value (dflt==1) */
   177    177     int eDetail;                    /* FTS5_DETAIL_XXX value */
   178    178     char *zContentExprlist;
   179    179     Fts5Tokenizer *pTok;
   180    180     fts5_tokenizer *pTokApi;
          181  +  int bLock;                      /* True when table is preparing statement */
   181    182   
   182    183     /* Values loaded from the %_config table */
   183    184     int iCookie;                    /* Incremented when %_config is modified */
   184    185     int pgsz;                       /* Approximate page size used in %_data */
   185    186     int nAutomerge;                 /* 'automerge' setting */
   186    187     int nCrisisMerge;               /* Maximum allowed segments per level */
   187    188     int nUsermerge;                 /* 'usermerge' setting */
................................................................................
   690    691   */
   691    692   int sqlite3Fts5ExprFirst(Fts5Expr*, Fts5Index *pIdx, i64 iMin, int bDesc);
   692    693   int sqlite3Fts5ExprNext(Fts5Expr*, i64 iMax);
   693    694   int sqlite3Fts5ExprEof(Fts5Expr*);
   694    695   i64 sqlite3Fts5ExprRowid(Fts5Expr*);
   695    696   
   696    697   void sqlite3Fts5ExprFree(Fts5Expr*);
          698  +int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2);
   697    699   
   698    700   /* Called during startup to register a UDF with SQLite */
   699    701   int sqlite3Fts5ExprInit(Fts5Global*, sqlite3*);
   700    702   
   701    703   int sqlite3Fts5ExprPhraseCount(Fts5Expr*);
   702    704   int sqlite3Fts5ExprPhraseSize(Fts5Expr*, int iPhrase);
   703    705   int sqlite3Fts5ExprPoslist(Fts5Expr*, int, const u8 **);

Changes to ext/fts5/fts5_config.c.

   679    679     );
   680    680   
   681    681     assert( zSql || rc==SQLITE_NOMEM );
   682    682     if( zSql ){
   683    683       rc = sqlite3_declare_vtab(pConfig->db, zSql);
   684    684       sqlite3_free(zSql);
   685    685     }
   686         -  
          686  + 
   687    687     return rc;
   688    688   }
   689    689   
   690    690   /*
   691    691   ** Tokenize the text passed via the second and third arguments.
   692    692   **
   693    693   ** The callback is invoked once for each token in the input text. The

Changes to ext/fts5/fts5_expr.c.

   304    304   void sqlite3Fts5ExprFree(Fts5Expr *p){
   305    305     if( p ){
   306    306       sqlite3Fts5ParseNodeFree(p->pRoot);
   307    307       sqlite3_free(p->apExprPhrase);
   308    308       sqlite3_free(p);
   309    309     }
   310    310   }
          311  +
          312  +int sqlite3Fts5ExprAnd(Fts5Expr **pp1, Fts5Expr *p2){
          313  +  Fts5Parse sParse;
          314  +  memset(&sParse, 0, sizeof(sParse));
          315  +
          316  +  if( *pp1 ){
          317  +    Fts5Expr *p1 = *pp1;
          318  +    int nPhrase = p1->nPhrase + p2->nPhrase;
          319  +
          320  +    p1->pRoot = sqlite3Fts5ParseNode(&sParse, FTS5_AND, p1->pRoot, p2->pRoot,0);
          321  +    p2->pRoot = 0;
          322  +
          323  +    if( sParse.rc==SQLITE_OK ){
          324  +      Fts5ExprPhrase **ap = (Fts5ExprPhrase**)sqlite3_realloc(
          325  +          p1->apExprPhrase, nPhrase * sizeof(Fts5ExprPhrase*)
          326  +      );
          327  +      if( ap==0 ){
          328  +        sParse.rc = SQLITE_NOMEM;
          329  +      }else{
          330  +        int i;
          331  +        memmove(&ap[p2->nPhrase], ap, p1->nPhrase*sizeof(Fts5ExprPhrase*));
          332  +        for(i=0; i<p2->nPhrase; i++){
          333  +          ap[i] = p2->apExprPhrase[i];
          334  +        }
          335  +        p1->nPhrase = nPhrase;
          336  +        p1->apExprPhrase = ap;
          337  +      }
          338  +    }
          339  +    sqlite3_free(p2->apExprPhrase);
          340  +    sqlite3_free(p2);
          341  +  }else{
          342  +    *pp1 = p2;
          343  +  }
          344  +
          345  +  return sParse.rc;
          346  +}
   311    347   
   312    348   /*
   313    349   ** Argument pTerm must be a synonym iterator. Return the current rowid
   314    350   ** that it points to.
   315    351   */
   316    352   static i64 fts5ExprSynonymRowid(Fts5ExprTerm *pTerm, int bDesc, int *pbEof){
   317    353     i64 iRet = 0;

Changes to ext/fts5/fts5_index.c.

   686    686         }
   687    687         if( rc!=SQLITE_OK ){
   688    688           sqlite3_free(pRet);
   689    689           pRet = 0;
   690    690         }else{
   691    691           /* TODO1: Fix this */
   692    692           pRet->p[nByte] = 0x00;
          693  +        pRet->p[nByte+1] = 0x00;
   693    694           pRet->szLeaf = fts5GetU16(&pRet->p[2]);
   694    695         }
   695    696       }
   696    697       p->rc = rc;
   697    698       p->nRead++;
   698    699     }
   699    700   
................................................................................
  4992   4993           int nCopy;
  4993   4994           u8 *aCopy;
  4994   4995   
  4995   4996           i64 iPrev = 0;
  4996   4997           Fts5PoslistWriter writer;
  4997   4998           memset(&writer, 0, sizeof(writer));
  4998   4999   
         5000  +        /* See the earlier comment in this function for an explanation of why
         5001  +        ** corrupt input position lists might cause the output to consume
         5002  +        ** at most 20 bytes of unexpected space. */
  4999   5003           fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
  5000   5004           fts5BufferZero(&tmp);
  5001         -        sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
         5005  +        sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist + 10 + 10);
  5002   5006           if( p->rc ) break;
  5003   5007   
  5004   5008           sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
  5005   5009           sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
  5006   5010           assert_nc( iPos1>=0 && iPos2>=0 );
  5007   5011   
  5008   5012           if( iPos1<iPos2 ){
................................................................................
  5042   5046             nCopy = i2.nPoslist - iOff2;
  5043   5047           }
  5044   5048           if( nCopy>0 ){
  5045   5049             fts5BufferSafeAppendBlob(&tmp, aCopy, nCopy);
  5046   5050           }
  5047   5051   
  5048   5052           /* WRITEPOSLISTSIZE */
         5053  +        assert_nc( tmp.n<=i1.nPoslist+i2.nPoslist );
         5054  +        assert( tmp.n<=i1.nPoslist+i2.nPoslist+10+10 );
         5055  +        if( tmp.n>i1.nPoslist+i2.nPoslist ){
         5056  +          if( p->rc==SQLITE_OK ) p->rc = FTS5_CORRUPT;
         5057  +          break;
         5058  +        }
  5049   5059           fts5BufferSafeAppendVarint(&out, tmp.n * 2);
  5050   5060           fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
  5051   5061           fts5DoclistIterNext(&i1);
  5052   5062           fts5DoclistIterNext(&i2);
  5053   5063           assert_nc( out.n<=(p1->n+p2->n+9) );
  5054   5064           if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
  5055   5065           assert( out.n<=((i1.aPoslist-p1->p) + (i2.aPoslist-p2->p)+9+10+10) );

Changes to ext/fts5/fts5_main.c.

   461    461   #endif
   462    462   }
   463    463   
   464    464   /*
   465    465   ** Implementation of the xBestIndex method for FTS5 tables. Within the 
   466    466   ** WHERE constraint, it searches for the following:
   467    467   **
   468         -**   1. A MATCH constraint against the special column.
          468  +**   1. A MATCH constraint against the table column.
   469    469   **   2. A MATCH constraint against the "rank" column.
   470         -**   3. An == constraint against the rowid column.
   471         -**   4. A < or <= constraint against the rowid column.
   472         -**   5. A > or >= constraint against the rowid column.
          470  +**   3. A MATCH constraint against some other column.
          471  +**   4. An == constraint against the rowid column.
          472  +**   5. A < or <= constraint against the rowid column.
          473  +**   6. A > or >= constraint against the rowid column.
   473    474   **
   474         -** Within the ORDER BY, either:
          475  +** Within the ORDER BY, the following are supported:
   475    476   **
   476    477   **   5. ORDER BY rank [ASC|DESC]
   477    478   **   6. ORDER BY rowid [ASC|DESC]
          479  +**
          480  +** Information for the xFilter call is passed via both the idxNum and 
          481  +** idxStr variables. Specifically, idxNum is a bitmask of the following
          482  +** flags used to encode the ORDER BY clause:
          483  +**
          484  +**     FTS5_BI_ORDER_RANK
          485  +**     FTS5_BI_ORDER_ROWID
          486  +**     FTS5_BI_ORDER_DESC
          487  +**
          488  +** idxStr is used to encode data from the WHERE clause. For each argument
          489  +** passed to the xFilter method, the following is appended to idxStr:
          490  +**
          491  +**   Match against table column:            "m"
          492  +**   Match against rank column:             "r"
          493  +**   Match against other column:            "<column-number>"
          494  +**   Equality constraint against the rowid: "="
          495  +**   A < or <= against the rowid:           "<"
          496  +**   A > or >= against the rowid:           ">"
          497  +**
          498  +** This function ensures that there is at most one "r" or "=". And that if
          499  +** there exists an "=" then there is no "<" or ">".
   478    500   **
   479    501   ** Costs are assigned as follows:
   480    502   **
   481    503   **  a) If an unusable MATCH operator is present in the WHERE clause, the
   482    504   **     cost is unconditionally set to 1e50 (a really big number).
   483    505   **
   484    506   **  a) If a MATCH operator is present, the cost depends on the other
................................................................................
   499    521   ** Costs are not modified by the ORDER BY clause.
   500    522   */
   501    523   static int fts5BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
   502    524     Fts5Table *pTab = (Fts5Table*)pVTab;
   503    525     Fts5Config *pConfig = pTab->pConfig;
   504    526     const int nCol = pConfig->nCol;
   505    527     int idxFlags = 0;               /* Parameter passed through to xFilter() */
   506         -  int bHasMatch;
   507         -  int iNext;
   508    528     int i;
   509    529   
   510         -  struct Constraint {
   511         -    int op;                       /* Mask against sqlite3_index_constraint.op */
   512         -    int fts5op;                   /* FTS5 mask for idxFlags */
   513         -    int iCol;                     /* 0==rowid, 1==tbl, 2==rank */
   514         -    int omit;                     /* True to omit this if found */
   515         -    int iConsIndex;               /* Index in pInfo->aConstraint[] */
   516         -  } aConstraint[] = {
   517         -    {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ, 
   518         -                                    FTS5_BI_MATCH,    1, 1, -1},
   519         -    {SQLITE_INDEX_CONSTRAINT_MATCH|SQLITE_INDEX_CONSTRAINT_EQ, 
   520         -                                    FTS5_BI_RANK,     2, 1, -1},
   521         -    {SQLITE_INDEX_CONSTRAINT_EQ,    FTS5_BI_ROWID_EQ, 0, 0, -1},
   522         -    {SQLITE_INDEX_CONSTRAINT_LT|SQLITE_INDEX_CONSTRAINT_LE, 
   523         -                                    FTS5_BI_ROWID_LE, 0, 0, -1},
   524         -    {SQLITE_INDEX_CONSTRAINT_GT|SQLITE_INDEX_CONSTRAINT_GE, 
   525         -                                    FTS5_BI_ROWID_GE, 0, 0, -1},
   526         -  };
          530  +  char *idxStr;
          531  +  int iIdxStr = 0;
          532  +  int iCons = 0;
   527    533   
   528         -  int aColMap[3];
   529         -  aColMap[0] = -1;
   530         -  aColMap[1] = nCol;
   531         -  aColMap[2] = nCol+1;
          534  +  int bSeenEq = 0;
          535  +  int bSeenGt = 0;
          536  +  int bSeenLt = 0;
          537  +  int bSeenMatch = 0;
          538  +  int bSeenRank = 0;
          539  +
   532    540   
   533    541     assert( SQLITE_INDEX_CONSTRAINT_EQ<SQLITE_INDEX_CONSTRAINT_MATCH );
   534    542     assert( SQLITE_INDEX_CONSTRAINT_GT<SQLITE_INDEX_CONSTRAINT_MATCH );
   535    543     assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
   536    544     assert( SQLITE_INDEX_CONSTRAINT_GE<SQLITE_INDEX_CONSTRAINT_MATCH );
   537    545     assert( SQLITE_INDEX_CONSTRAINT_LE<SQLITE_INDEX_CONSTRAINT_MATCH );
   538    546   
   539         -  /* Set idxFlags flags for all WHERE clause terms that will be used. */
          547  +  if( pConfig->bLock ){
          548  +    pTab->base.zErrMsg = sqlite3_mprintf(
          549  +        "recursively defined fts5 content table"
          550  +    );
          551  +    return SQLITE_ERROR;
          552  +  }
          553  +
          554  +  idxStr = (char*)sqlite3_malloc(pInfo->nConstraint * 6 + 1);
          555  +  if( idxStr==0 ) return SQLITE_NOMEM;
          556  +  pInfo->idxStr = idxStr;
          557  +  pInfo->needToFreeIdxStr = 1;
          558  +
   540    559     for(i=0; i<pInfo->nConstraint; i++){
   541    560       struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
   542    561       int iCol = p->iColumn;
   543         -
   544         -    if( (p->op==SQLITE_INDEX_CONSTRAINT_MATCH && iCol>=0 && iCol<=nCol)
   545         -     || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol==nCol)
          562  +    if( p->op==SQLITE_INDEX_CONSTRAINT_MATCH
          563  +     || (p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol>=nCol)
   546    564       ){
   547    565         /* A MATCH operator or equivalent */
   548         -      if( p->usable ){
   549         -        idxFlags = (idxFlags & 0xFFFF) | FTS5_BI_MATCH | (iCol << 16);
   550         -        aConstraint[0].iConsIndex = i;
   551         -      }else{
          566  +      if( p->usable==0 || iCol<0 ){
   552    567           /* As there exists an unusable MATCH constraint this is an 
   553    568           ** unusable plan. Set a prohibitively high cost. */
   554    569           pInfo->estimatedCost = 1e50;
          570  +        assert( iIdxStr < pInfo->nConstraint*6 + 1 );
          571  +        idxStr[iIdxStr] = 0;
   555    572           return SQLITE_OK;
   556         -      }
   557         -    }else if( p->op<=SQLITE_INDEX_CONSTRAINT_MATCH ){
   558         -      int j;
   559         -      for(j=1; j<ArraySize(aConstraint); j++){
   560         -        struct Constraint *pC = &aConstraint[j];
   561         -        if( iCol==aColMap[pC->iCol] && (p->op & pC->op) && p->usable ){
   562         -          pC->iConsIndex = i;
   563         -          idxFlags |= pC->fts5op;
   564         -        }
   565         -      }
   566         -    }
   567         -  }
          573  +      }else{
          574  +        if( iCol==nCol+1 ){
          575  +          if( bSeenRank ) continue;
          576  +          idxStr[iIdxStr++] = 'r';
          577  +          bSeenRank = 1;
          578  +        }else{
          579  +          bSeenMatch = 1;
          580  +          idxStr[iIdxStr++] = 'm';
          581  +          if( iCol<nCol ){
          582  +            sqlite3_snprintf(6, &idxStr[iIdxStr], "%d", iCol);
          583  +            idxStr += strlen(&idxStr[iIdxStr]);
          584  +            assert( idxStr[iIdxStr]=='\0' );
          585  +          }
          586  +        }
          587  +        pInfo->aConstraintUsage[i].argvIndex = ++iCons;
          588  +        pInfo->aConstraintUsage[i].omit = 1;
          589  +      }
          590  +    }
          591  +    else if( p->usable && bSeenEq==0 
          592  +      && p->op==SQLITE_INDEX_CONSTRAINT_EQ && iCol<0 
          593  +    ){
          594  +      idxStr[iIdxStr++] = '=';
          595  +      bSeenEq = 1;
          596  +      pInfo->aConstraintUsage[i].argvIndex = ++iCons;
          597  +    }
          598  +  }
          599  +
          600  +  if( bSeenEq==0 ){
          601  +    for(i=0; i<pInfo->nConstraint; i++){
          602  +      struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
          603  +      if( p->iColumn<0 && p->usable ){
          604  +        int op = p->op;
          605  +        if( op==SQLITE_INDEX_CONSTRAINT_LT || op==SQLITE_INDEX_CONSTRAINT_LE ){
          606  +          if( bSeenLt ) continue;
          607  +          idxStr[iIdxStr++] = '<';
          608  +          pInfo->aConstraintUsage[i].argvIndex = ++iCons;
          609  +          bSeenLt = 1;
          610  +        }else
          611  +        if( op==SQLITE_INDEX_CONSTRAINT_GT || op==SQLITE_INDEX_CONSTRAINT_GE ){
          612  +          if( bSeenGt ) continue;
          613  +          idxStr[iIdxStr++] = '>';
          614  +          pInfo->aConstraintUsage[i].argvIndex = ++iCons;
          615  +          bSeenGt = 1;
          616  +        }
          617  +      }
          618  +    }
          619  +  }
          620  +  idxStr[iIdxStr] = '\0';
   568    621   
   569    622     /* Set idxFlags flags for the ORDER BY clause */
   570    623     if( pInfo->nOrderBy==1 ){
   571    624       int iSort = pInfo->aOrderBy[0].iColumn;
   572         -    if( iSort==(pConfig->nCol+1) && BitFlagTest(idxFlags, FTS5_BI_MATCH) ){
          625  +    if( iSort==(pConfig->nCol+1) && bSeenMatch ){
   573    626         idxFlags |= FTS5_BI_ORDER_RANK;
   574    627       }else if( iSort==-1 ){
   575    628         idxFlags |= FTS5_BI_ORDER_ROWID;
   576    629       }
   577    630       if( BitFlagTest(idxFlags, FTS5_BI_ORDER_RANK|FTS5_BI_ORDER_ROWID) ){
   578    631         pInfo->orderByConsumed = 1;
   579    632         if( pInfo->aOrderBy[0].desc ){
   580    633           idxFlags |= FTS5_BI_ORDER_DESC;
   581    634         }
   582    635       }
   583    636     }
   584    637   
   585    638     /* Calculate the estimated cost based on the flags set in idxFlags. */
   586         -  bHasMatch = BitFlagTest(idxFlags, FTS5_BI_MATCH);
   587         -  if( BitFlagTest(idxFlags, FTS5_BI_ROWID_EQ) ){
   588         -    pInfo->estimatedCost = bHasMatch ? 100.0 : 10.0;
   589         -    if( bHasMatch==0 ) fts5SetUniqueFlag(pInfo);
   590         -  }else if( BitFlagAllTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
   591         -    pInfo->estimatedCost = bHasMatch ? 500.0 : 250000.0;
   592         -  }else if( BitFlagTest(idxFlags, FTS5_BI_ROWID_LE|FTS5_BI_ROWID_GE) ){
   593         -    pInfo->estimatedCost = bHasMatch ? 750.0 : 750000.0;
          639  +  if( bSeenEq ){
          640  +    pInfo->estimatedCost = bSeenMatch ? 100.0 : 10.0;
          641  +    if( bSeenMatch==0 ) fts5SetUniqueFlag(pInfo);
          642  +  }else if( bSeenLt && bSeenGt ){
          643  +    pInfo->estimatedCost = bSeenMatch ? 500.0 : 250000.0;
          644  +  }else if( bSeenLt || bSeenGt ){
          645  +    pInfo->estimatedCost = bSeenMatch ? 750.0 : 750000.0;
   594    646     }else{
   595         -    pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
   596         -  }
   597         -
   598         -  /* Assign argvIndex values to each constraint in use. */
   599         -  iNext = 1;
   600         -  for(i=0; i<ArraySize(aConstraint); i++){
   601         -    struct Constraint *pC = &aConstraint[i];
   602         -    if( pC->iConsIndex>=0 ){
   603         -      pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
   604         -      pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
   605         -    }
          647  +    pInfo->estimatedCost = bSeenMatch ? 1000.0 : 1000000.0;
   606    648     }
   607    649   
   608    650     pInfo->idxNum = idxFlags;
   609    651     return SQLITE_OK;
   610    652   }
   611    653   
   612    654   static int fts5NewTransaction(Fts5FullTable *pTab){
................................................................................
   921    963     ** handles here, rather than preparing a new one for each query. But that
   922    964     ** is not possible as SQLite reference counts the virtual table objects.
   923    965     ** And since the statement required here reads from this very virtual 
   924    966     ** table, saving it creates a circular reference.
   925    967     **
   926    968     ** If SQLite a built-in statement cache, this wouldn't be a problem. */
   927    969     rc = fts5PrepareStatement(&pSorter->pStmt, pConfig,
   928         -      "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(%s%s%s) %s",
          970  +      "SELECT rowid, rank FROM %Q.%Q ORDER BY %s(\"%w\"%s%s) %s",
   929    971         pConfig->zDb, pConfig->zName, zRank, pConfig->zName,
   930    972         (zRankArgs ? ", " : ""),
   931    973         (zRankArgs ? zRankArgs : ""),
   932    974         bDesc ? "DESC" : "ASC"
   933    975     );
   934    976   
   935    977     pCsr->pSorter = pSorter;
................................................................................
   977   1019   
   978   1020     while( z[0]==' ' ) z++;
   979   1021     for(n=0; z[n] && z[n]!=' '; n++);
   980   1022   
   981   1023     assert( pTab->p.base.zErrMsg==0 );
   982   1024     pCsr->ePlan = FTS5_PLAN_SPECIAL;
   983   1025   
   984         -  if( 0==sqlite3_strnicmp("reads", z, n) ){
         1026  +  if( n==5 && 0==sqlite3_strnicmp("reads", z, n) ){
   985   1027       pCsr->iSpecial = sqlite3Fts5IndexReads(pTab->p.pIndex);
   986   1028     }
   987         -  else if( 0==sqlite3_strnicmp("id", z, n) ){
         1029  +  else if( n==2 && 0==sqlite3_strnicmp("id", z, n) ){
   988   1030       pCsr->iSpecial = pCsr->iCsrId;
   989   1031     }
   990   1032     else{
   991   1033       /* An unrecognized directive. Return an error message. */
   992   1034       pTab->p.base.zErrMsg = sqlite3_mprintf("unknown special query: %.*s", n, z);
   993   1035       rc = SQLITE_ERROR;
   994   1036     }
................................................................................
  1121   1163   **   1. Full-text search using a MATCH operator.
  1122   1164   **   2. A by-rowid lookup.
  1123   1165   **   3. A full-table scan.
  1124   1166   */
  1125   1167   static int fts5FilterMethod(
  1126   1168     sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
  1127   1169     int idxNum,                     /* Strategy index */
  1128         -  const char *zUnused,            /* Unused */
         1170  +  const char *idxStr,             /* Unused */
  1129   1171     int nVal,                       /* Number of elements in apVal */
  1130   1172     sqlite3_value **apVal           /* Arguments for the indexing scheme */
  1131   1173   ){
  1132   1174     Fts5FullTable *pTab = (Fts5FullTable*)(pCursor->pVtab);
  1133   1175     Fts5Config *pConfig = pTab->p.pConfig;
  1134   1176     Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
  1135   1177     int rc = SQLITE_OK;             /* Error code */
  1136         -  int iVal = 0;                   /* Counter for apVal[] */
  1137   1178     int bDesc;                      /* True if ORDER BY [rank|rowid] DESC */
  1138   1179     int bOrderByRank;               /* True if ORDER BY rank */
  1139         -  sqlite3_value *pMatch = 0;      /* <tbl> MATCH ? expression (or NULL) */
  1140   1180     sqlite3_value *pRank = 0;       /* rank MATCH ? expression (or NULL) */
  1141   1181     sqlite3_value *pRowidEq = 0;    /* rowid = ? expression (or NULL) */
  1142   1182     sqlite3_value *pRowidLe = 0;    /* rowid <= ? expression (or NULL) */
  1143   1183     sqlite3_value *pRowidGe = 0;    /* rowid >= ? expression (or NULL) */
  1144   1184     int iCol;                       /* Column on LHS of MATCH operator */
  1145   1185     char **pzErrmsg = pConfig->pzErrmsg;
  1146         -
  1147         -  UNUSED_PARAM(zUnused);
  1148         -  UNUSED_PARAM(nVal);
         1186  +  int i;
         1187  +  int iIdxStr = 0;
         1188  +  Fts5Expr *pExpr = 0;
  1149   1189   
  1150   1190     if( pCsr->ePlan ){
  1151   1191       fts5FreeCursorComponents(pCsr);
  1152   1192       memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
  1153   1193     }
  1154   1194   
  1155   1195     assert( pCsr->pStmt==0 );
  1156   1196     assert( pCsr->pExpr==0 );
  1157   1197     assert( pCsr->csrflags==0 );
  1158   1198     assert( pCsr->pRank==0 );
  1159   1199     assert( pCsr->zRank==0 );
  1160   1200     assert( pCsr->zRankArgs==0 );
         1201  +  assert( pTab->pSortCsr==0 || nVal==0 );
  1161   1202   
  1162   1203     assert( pzErrmsg==0 || pzErrmsg==&pTab->p.base.zErrMsg );
  1163   1204     pConfig->pzErrmsg = &pTab->p.base.zErrMsg;
  1164   1205   
  1165         -  /* Decode the arguments passed through to this function.
  1166         -  **
  1167         -  ** Note: The following set of if(...) statements must be in the same
  1168         -  ** order as the corresponding entries in the struct at the top of
  1169         -  ** fts5BestIndexMethod().  */
  1170         -  if( BitFlagTest(idxNum, FTS5_BI_MATCH) ) pMatch = apVal[iVal++];
  1171         -  if( BitFlagTest(idxNum, FTS5_BI_RANK) ) pRank = apVal[iVal++];
  1172         -  if( BitFlagTest(idxNum, FTS5_BI_ROWID_EQ) ) pRowidEq = apVal[iVal++];
  1173         -  if( BitFlagTest(idxNum, FTS5_BI_ROWID_LE) ) pRowidLe = apVal[iVal++];
  1174         -  if( BitFlagTest(idxNum, FTS5_BI_ROWID_GE) ) pRowidGe = apVal[iVal++];
  1175         -  iCol = (idxNum>>16);
  1176         -  assert( iCol>=0 && iCol<=pConfig->nCol );
  1177         -  assert( iVal==nVal );
         1206  +  /* Decode the arguments passed through to this function. */
         1207  +  for(i=0; i<nVal; i++){
         1208  +    switch( idxStr[iIdxStr++] ){
         1209  +      case 'r':
         1210  +        pRank = apVal[i];
         1211  +        break;
         1212  +      case 'm': {
         1213  +        const char *zText = (const char*)sqlite3_value_text(apVal[i]);
         1214  +        if( zText==0 ) zText = "";
         1215  +
         1216  +        if( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' ){
         1217  +          iCol = 0;
         1218  +          do{
         1219  +            iCol = iCol*10 + (idxStr[iIdxStr]-'0');
         1220  +            iIdxStr++;
         1221  +          }while( idxStr[iIdxStr]>='0' && idxStr[iIdxStr]<='9' );
         1222  +        }else{
         1223  +          iCol = pConfig->nCol;
         1224  +        }
         1225  +
         1226  +        if( zText[0]=='*' ){
         1227  +          /* The user has issued a query of the form "MATCH '*...'". This
         1228  +          ** indicates that the MATCH expression is not a full text query,
         1229  +          ** but a request for an internal parameter.  */
         1230  +          rc = fts5SpecialMatch(pTab, pCsr, &zText[1]);
         1231  +          goto filter_out;
         1232  +        }else{
         1233  +          char **pzErr = &pTab->p.base.zErrMsg;
         1234  +          rc = sqlite3Fts5ExprNew(pConfig, iCol, zText, &pExpr, pzErr);
         1235  +          if( rc==SQLITE_OK ){
         1236  +            rc = sqlite3Fts5ExprAnd(&pCsr->pExpr, pExpr);
         1237  +            pExpr = 0;
         1238  +          }
         1239  +          if( rc!=SQLITE_OK ) goto filter_out;
         1240  +        }
         1241  +
         1242  +        break;
         1243  +      }
         1244  +      case '=':
         1245  +        pRowidEq = apVal[i];
         1246  +        break;
         1247  +      case '<':
         1248  +        pRowidLe = apVal[i];
         1249  +        break;
         1250  +      default: assert( idxStr[iIdxStr-1]=='>' );
         1251  +        pRowidGe = apVal[i];
         1252  +        break;
         1253  +    }
         1254  +  }
  1178   1255     bOrderByRank = ((idxNum & FTS5_BI_ORDER_RANK) ? 1 : 0);
  1179   1256     pCsr->bDesc = bDesc = ((idxNum & FTS5_BI_ORDER_DESC) ? 1 : 0);
  1180   1257   
  1181   1258     /* Set the cursor upper and lower rowid limits. Only some strategies 
  1182   1259     ** actually use them. This is ok, as the xBestIndex() method leaves the
  1183   1260     ** sqlite3_index_constraint.omit flag clear for range constraints
  1184   1261     ** on the rowid field.  */
................................................................................
  1197   1274       /* If pSortCsr is non-NULL, then this call is being made as part of 
  1198   1275       ** processing for a "... MATCH <expr> ORDER BY rank" query (ePlan is
  1199   1276       ** set to FTS5_PLAN_SORTED_MATCH). pSortCsr is the cursor that will
  1200   1277       ** return results to the user for this query. The current cursor 
  1201   1278       ** (pCursor) is used to execute the query issued by function 
  1202   1279       ** fts5CursorFirstSorted() above.  */
  1203   1280       assert( pRowidEq==0 && pRowidLe==0 && pRowidGe==0 && pRank==0 );
  1204         -    assert( nVal==0 && pMatch==0 && bOrderByRank==0 && bDesc==0 );
         1281  +    assert( nVal==0 && bOrderByRank==0 && bDesc==0 );
  1205   1282       assert( pCsr->iLastRowid==LARGEST_INT64 );
  1206   1283       assert( pCsr->iFirstRowid==SMALLEST_INT64 );
  1207   1284       if( pTab->pSortCsr->bDesc ){
  1208   1285         pCsr->iLastRowid = pTab->pSortCsr->iFirstRowid;
  1209   1286         pCsr->iFirstRowid = pTab->pSortCsr->iLastRowid;
  1210   1287       }else{
  1211   1288         pCsr->iLastRowid = pTab->pSortCsr->iLastRowid;
  1212   1289         pCsr->iFirstRowid = pTab->pSortCsr->iFirstRowid;
  1213   1290       }
  1214   1291       pCsr->ePlan = FTS5_PLAN_SOURCE;
  1215   1292       pCsr->pExpr = pTab->pSortCsr->pExpr;
  1216   1293       rc = fts5CursorFirst(pTab, pCsr, bDesc);
  1217         -  }else if( pMatch ){
  1218         -    const char *zExpr = (const char*)sqlite3_value_text(apVal[0]);
  1219         -    if( zExpr==0 ) zExpr = "";
  1220         -
         1294  +  }else if( pCsr->pExpr ){
  1221   1295       rc = fts5CursorParseRank(pConfig, pCsr, pRank);
  1222   1296       if( rc==SQLITE_OK ){
  1223         -      if( zExpr[0]=='*' ){
  1224         -        /* The user has issued a query of the form "MATCH '*...'". This
  1225         -        ** indicates that the MATCH expression is not a full text query,
  1226         -        ** but a request for an internal parameter.  */
  1227         -        rc = fts5SpecialMatch(pTab, pCsr, &zExpr[1]);
         1297  +      if( bOrderByRank ){
         1298  +        pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
         1299  +        rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
  1228   1300         }else{
  1229         -        char **pzErr = &pTab->p.base.zErrMsg;
  1230         -        rc = sqlite3Fts5ExprNew(pConfig, iCol, zExpr, &pCsr->pExpr, pzErr);
  1231         -        if( rc==SQLITE_OK ){
  1232         -          if( bOrderByRank ){
  1233         -            pCsr->ePlan = FTS5_PLAN_SORTED_MATCH;
  1234         -            rc = fts5CursorFirstSorted(pTab, pCsr, bDesc);
  1235         -          }else{
  1236         -            pCsr->ePlan = FTS5_PLAN_MATCH;
  1237         -            rc = fts5CursorFirst(pTab, pCsr, bDesc);
  1238         -          }
  1239         -        }
         1301  +        pCsr->ePlan = FTS5_PLAN_MATCH;
         1302  +        rc = fts5CursorFirst(pTab, pCsr, bDesc);
  1240   1303         }
  1241   1304       }
  1242   1305     }else if( pConfig->zContent==0 ){
  1243   1306       *pConfig->pzErrmsg = sqlite3_mprintf(
  1244   1307           "%s: table does not support scanning", pConfig->zName
  1245   1308       );
  1246   1309       rc = SQLITE_ERROR;
................................................................................
  1249   1312       ** by rowid (ePlan==FTS5_PLAN_ROWID).  */
  1250   1313       pCsr->ePlan = (pRowidEq ? FTS5_PLAN_ROWID : FTS5_PLAN_SCAN);
  1251   1314       rc = sqlite3Fts5StorageStmt(
  1252   1315           pTab->pStorage, fts5StmtType(pCsr), &pCsr->pStmt, &pTab->p.base.zErrMsg
  1253   1316       );
  1254   1317       if( rc==SQLITE_OK ){
  1255   1318         if( pCsr->ePlan==FTS5_PLAN_ROWID ){
  1256         -        sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]);
         1319  +        sqlite3_bind_value(pCsr->pStmt, 1, pRowidEq);
  1257   1320         }else{
  1258   1321           sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iFirstRowid);
  1259   1322           sqlite3_bind_int64(pCsr->pStmt, 2, pCsr->iLastRowid);
  1260   1323         }
  1261   1324         rc = fts5NextMethod(pCursor);
  1262   1325       }
  1263   1326     }
  1264   1327   
         1328  + filter_out:
         1329  +  sqlite3Fts5ExprFree(pExpr);
  1265   1330     pConfig->pzErrmsg = pzErrmsg;
  1266   1331     return rc;
  1267   1332   }
  1268   1333   
  1269   1334   /* 
  1270   1335   ** This is the xEof method of the virtual table. SQLite calls this 
  1271   1336   ** routine to find out if it has reached the end of a result set.
................................................................................
  2228   2293     i64 iCsrId;
  2229   2294   
  2230   2295     assert( argc>=1 );
  2231   2296     pAux = (Fts5Auxiliary*)sqlite3_user_data(context);
  2232   2297     iCsrId = sqlite3_value_int64(argv[0]);
  2233   2298   
  2234   2299     pCsr = fts5CursorFromCsrid(pAux->pGlobal, iCsrId);
  2235         -  if( pCsr==0 ){
         2300  +  if( pCsr==0 || pCsr->ePlan==0 ){
  2236   2301       char *zErr = sqlite3_mprintf("no such cursor: %lld", iCsrId);
  2237   2302       sqlite3_result_error(context, zErr, -1);
  2238   2303       sqlite3_free(zErr);
  2239   2304     }else{
  2240   2305       fts5ApiInvoke(pAux, pCsr, context, argc-1, &argv[1]);
  2241   2306     }
  2242   2307   }

Changes to ext/fts5/fts5_storage.c.

   134    134       }
   135    135   
   136    136       if( zSql==0 ){
   137    137         rc = SQLITE_NOMEM;
   138    138       }else{
   139    139         int f = SQLITE_PREPARE_PERSISTENT;
   140    140         if( eStmt>FTS5_STMT_LOOKUP ) f |= SQLITE_PREPARE_NO_VTAB;
          141  +      p->pConfig->bLock++;
   141    142         rc = sqlite3_prepare_v3(pC->db, zSql, -1, f, &p->aStmt[eStmt], 0);
          143  +      p->pConfig->bLock--;
   142    144         sqlite3_free(zSql);
   143    145         if( rc!=SQLITE_OK && pzErrMsg ){
   144    146           *pzErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pC->db));
   145    147         }
   146    148       }
   147    149     }
   148    150   

Changes to ext/fts5/test/fts5content.test.

   249    249     SELECT name FROM sqlite_master;
   250    250   } {xx xx_data xx_idx xx_docsize xx_config}
   251    251   do_execsql_test 6.2 {
   252    252     DROP TABLE xx;
   253    253     SELECT name FROM sqlite_master;
   254    254   } {}
   255    255   
          256  +#---------------------------------------------------------------------------
          257  +# Check that an fts5 table cannot be its own content table.
          258  +#
          259  +reset_db
          260  +do_execsql_test 7.1 {
          261  +  CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 );
          262  +  INSERT INTO t1( a ) VALUES('abc');
          263  +}
          264  +do_catchsql_test 7.2 { 
          265  +  SELECT * FROM t1; 
          266  +} {1 {recursively defined fts5 content table}}
          267  +do_catchsql_test 7.3 { 
          268  +  SELECT * FROM t1('abc'); 
          269  +} {1 {recursively defined fts5 content table}}
   256    270   
   257    271   finish_test
          272  +

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

   763    763   |   4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65   heck....optimize
   764    764   | end c13.db
   765    765   SELECT * FROM t1 WHERE t1 MATCH 'abandon';
   766    766   }]} {}
   767    767   
   768    768   do_catchsql_test 13.1 {
   769    769     SELECT * FROM t1 WHERE t1 MATCH 'abandon'; 
   770         -} {1 {vtable constructor failed: t1}}
          770  +} {/*malformed database schema*/}
   771    771   
   772    772   #-------------------------------------------------------------------------
   773    773   reset_db
   774    774   do_test 14.0 {
   775    775     sqlite3 db {}
   776    776     db deserialize [decode_hexdb {
   777    777   | size 28672 pagesize 4096 filename c14b.db
................................................................................
   954    954   |     48: 01 00 00 10 10 04 02 02 00 00 00 00 00 00 00 00   ................
   955    955   |     64: 70 00 00 00 00 00 00 00 00 00 00 00 70 00 00 00   p...........p...
   956    956   | end c16.db
   957    957   }]} {}
   958    958   
   959    959   do_catchsql_test 15.1 {
   960    960     INSERT INTO t1(t1) VALUES('integrity-check');
   961         -} {1 {database disk image is malformed}}
          961  +} {/*malformed database schema*/}
   962    962   
   963    963   #---------------------------------------------------------------------------
   964    964   #
   965    965   reset_db
   966    966   do_test 16.0 {
   967    967     sqlite3 db {}
   968    968     db deserialize [decode_hexdb {
................................................................................
  3899   3899   |    448: 54 55 41 4c 20 54 41 42 4c 45 20 74 31 20 55 53   TUAL TABLE t1 US
  3900   3900   |    464: 49 4e 47 20 66 74 73 35 28 61 2c 62 2c 63 29 00   ING fts5(a,b,c).
  3901   3901   |    480: 00 00 39 00 00 00 00 00 00 00 00 00 00 00 00 00   ..9.............
  3902   3902   |   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
  3903   3903   | end crash-fed6e90021ba5d.db
  3904   3904   }]} {}
  3905   3905   
  3906         -do_execsql_test 33.1 {
         3906  +do_catchsql_test 33.1 {
  3907   3907     CREATE VIRTUAL TABLE t2 USING fts5vocab('t1','row');
  3908   3908     CREATE VIRTUAL TABLE t3 USING fts5vocab('t1','col');
  3909   3909     CREATE VIRTUAL TABLE t4 USING fts5vocab('t1','instance');
  3910         -}
         3910  +} {/*malformed database schema*/}
  3911   3911   
  3912   3912   do_catchsql_test 33.2 {
  3913   3913     SELECT * FROM t2;
  3914         -} {1 {database disk image is malformed}}
         3914  +} {/*malformed database schema*/}
  3915   3915   
  3916   3916   do_catchsql_test 33.3 {
  3917   3917     SELECT * FROM t2, t3, t4 WHERE t2.term=t3.term AND t3.term=t4.term;
  3918         -} {1 {database disk image is malformed}}
         3918  +} {/*malformed database schema*/}
  3919   3919   
  3920   3920   #-------------------------------------------------------------------------
  3921   3921   reset_db
  3922   3922   do_test 34.0 {
  3923   3923     sqlite3 db {}
  3924   3924     db deserialize [decode_hexdb {
  3925   3925   | size 40960 pagesize 4096 filename crash-a60a9da4c8932f.db
................................................................................
  4633   4633   |      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
  4634   4634   |   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
  4635   4635   | end null-memcmp-param-1..db
  4636   4636   }]} {}
  4637   4637   
  4638   4638   do_catchsql_test 37.1 {
  4639   4639     SELECT * FROM t3;
  4640         -} {1 {database disk image is malformed}}
         4640  +} {/*malformed database schema*/}
  4641   4641   
  4642   4642   #-------------------------------------------------------------------------
  4643   4643   reset_db 
  4644         -do_execsql_test 37.0 {
         4644  +do_execsql_test 37a.0 {
  4645   4645     CREATE VIRTUAL TABLE t1 USING fts5(b, c);
  4646   4646     INSERT INTO t1 VALUES('a', 'b');
  4647   4647     SELECT quote(block) FROM t1_data WHERE rowid=10;
  4648   4648   } {X'000000000101010001010101'}
  4649   4649   
  4650         -do_execsql_test 37.1 {
         4650  +do_execsql_test 37a.1 {
  4651   4651     UPDATE t1_data SET block = X'FFFFFFFF0101010001010101' WHERE rowid = 10;
  4652   4652     SELECT rowid FROM t1('a');
  4653   4653   } {1}
  4654   4654   
  4655   4655   #-------------------------------------------------------------------------
  4656   4656   reset_db 
  4657   4657   do_execsql_test 38.0 {
................................................................................
  4890   4890   |   4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62   ity-check....reb
  4891   4891   |   4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65   uild....optimize
  4892   4892   | end crash-fd2a1313e5b5e9.db
  4893   4893   }]} {}
  4894   4894   
  4895   4895   do_catchsql_test 38.1 {
  4896   4896     UPDATE t1 SET b=quote(zeroblob(200)) WHERE t1 MATCH 'thread*';
  4897         -} {0 {}}
         4897  +} {/*malformed database schema*/}
  4898   4898   
  4899   4899   #-------------------------------------------------------------------------
  4900   4900   reset_db
  4901   4901   do_test 39.0 {
  4902   4902     sqlite3 db {}
  4903   4903     db deserialize [decode_hexdb {
  4904   4904   .open --hexdb
................................................................................
  5322   5322   |   4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04   gsz.....version.
  5323   5323   | page 6 offset 20480
  5324   5324   |      0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00   ................
  5325   5325   |   4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09   ................
  5326   5326   | end crash2.txt.db
  5327   5327   }]} {}
  5328   5328   
  5329         -do_execsql_test 40.1 {
         5329  +do_catchsql_test 40.1 {
  5330   5330     BEGIN;
  5331   5331     INSERT INTO t1(b) VALUES(X'819192e578de3fa24af3733ca8769291a0fee3669f9fddefc5cba913e4225d4b6ce2b04f26b87fad3ee6f9b7d90a1ea62a169bf41e5d32707a6ca5c3d05e4bde05c9d89eaaa8c50e74333d2e9fcd7dfe95528a3a016aac1102d825c5cd70cf99d8a88e0ea7f798d4334386518b7ad359beb168b93aba059a2a3bd93112d65b44c12b9904ea786b204d80531cdf0504bf9b203dbe927061974caf7b9f30cbc3397b61f802e732012a6663d41c3607d6f1c0dbcfd489adac05ca500c0b04439d894cd93a840159225ef73b627e178b9f84b3ffe66cf22a963a8368813ff7961fc47f573211ccec95e0220dcbb3bf429f4a50ba54d7a53784ac51bf');
  5332   5332     INSERT INTO t1(b) VALUES(X'c8ae0d0e7c3175946e62ba2b449511d4eb504079984a20f77969f62206c9f3d7ea25358ab705e6978627290b6d48db9032f815a06a79a4f4b809841a0942eed12954ed166f666111812a508abc3bec87958846edaec0a6fe14564bc0a4b78f1c35ebcacca6bae29cc37ae9b59d8a2d7593af1e47dda0ece2268a98d20febafad037964f139851f9a57f48b3706b01721769071991412044cd6006f1d72eb6eb4aa5ad77e378176db8c15575fbeee47165e38a7c6c5a557ac2dfe11813976eaf6741cf593a9e457053a3c34cddfbe605a6e25419f993de8374fafcd3636509d8416a51dc7bcc14cfca322ae343078f47e23522431c17d0da0c033');
  5333   5333     INSERT INTO t1(b) VALUES(X'dc29a94e873a45a4243fce9b912aaefbadf1d0423e0345793874b356eeb500b92fb05284c1601fe9bad3143f72162f10242cec27c44ebf764c8fc9fb0824e32c4161472a4f914f579e0e8274f08ca1a02e59b9d8eec1f31061f9ccb9ed97a6f06534e991f7992c761489e6a7724f6e9c2b581e77487ded3a986d53c4419bbd3e9747cee300e670dd7294874c77e2ed48da68eaa6c3ec954a09ac410493d98e34d6686e54fbbe80696705f10e040c66093efb40746b33600685c94c664c7942835a9e954866121d5dcfb2cb12e92521ea3df175ee17072502dad9b9c1565f801b2179799011eb7418bfa00323e3157589e648ff7378be233c79b7');
  5334         -}
         5334  +} {/*malformed database schema*/}
  5335   5335   
  5336   5336   do_catchsql_test 40.2 {
  5337   5337     INSERT INTO t1(a,b) VALUES(1,11),(2,22),(3, true ),(4,44);
  5338         -} {1 {database disk image is malformed}}
         5338  +} {/*malformed database schema*/}
  5339   5339   
  5340   5340   #-------------------------------------------------------------------------
  5341   5341   reset_db
  5342   5342   do_execsql_test 41.0 {
  5343   5343     CREATE VIRTUAL TABLE t1 USING fts5(a,b,c);
  5344   5344     REPLACE INTO t1_data VALUES(1,X'255a5824');
  5345   5345     REPLACE INTO t1_data VALUES(10,X'0a1000000102020002010101020101');
................................................................................
  5785   5785   |      0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00   ................
  5786   5786   |   4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 02 09   ................
  5787   5787   | end 89028ffd2c29b679e250.db
  5788   5788   }]} {}
  5789   5789   
  5790   5790   do_catchsql_test 43.1 {
  5791   5791     INSERT INTO t1(t1) VALUES('optimize');
  5792         -} {1 {database disk image is malformed}}
         5792  +} {/*malformed database schema*/}
  5793   5793   
  5794   5794   #-------------------------------------------------------------------------
  5795   5795   reset_db
  5796   5796   do_execsql_test 44.1 {
  5797   5797     CREATE VIRTUAL TABLE t1 USING fts5(a,b unindexed,c,tokenize="porter ascii");
  5798   5798     REPLACE INTO t1_data VALUES(1,X'03090009');
  5799   5799     REPLACE INTO t1_data VALUES(10,X'000000000103030003010101020101030101');
................................................................................
  6043   6043     INSERT INTO t1(t1, rank) VALUES('merge', 5);
  6044   6044     INSERT INTO t1(t1, rank) VALUES('merge', 5);
  6045   6045     INSERT INTO t1(t1, rank) VALUES('merge', 5);
  6046   6046     INSERT INTO t1(t1, rank) VALUES('merge', 5);
  6047   6047     INSERT INTO t1(t1, rank) VALUES('merge', 5);
  6048   6048     INSERT INTO t1(t1, rank) VALUES('merge', 5);
  6049   6049     INSERT INTO t1(t1, rank) VALUES('merge', 5);
  6050         -} {0 {}}
         6050  +} {/*malformed database schema*/}
  6051   6051   
  6052   6052   #--------------------------------------------------------------------------
  6053   6053   reset_db
  6054   6054   do_test 46.0 {
  6055   6055     sqlite3 db {}
  6056   6056     db deserialize [decode_hexdb {
  6057   6057   | size 32768 pagesize 4096 filename crash-1ee8bd451dd1ad.db
................................................................................
  6261   6261   |   4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62   ity-check....reb
  6262   6262   |   4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65   uild....optimize
  6263   6263   | end crash-1ee8bd451dd1ad.db
  6264   6264   }]} {}
  6265   6265   
  6266   6266   do_catchsql_test 46.1 {
  6267   6267     SELECT snippet(t1,'[','', '--',-1,10) FROM t1('*');
  6268         -} {0 {{}}}
         6268  +} {/*malformed database schema*/}
  6269   6269   
  6270   6270   #--------------------------------------------------------------------------
  6271   6271   reset_db
  6272   6272   do_test 47.0 {
  6273   6273     sqlite3 db {}
  6274   6274     db deserialize [decode_hexdb {
  6275   6275   | size 40960 pagesize 4096 filename 4b6fc659283f2735616c.db
................................................................................
  6414   6414   |      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
  6415   6415   |   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
  6416   6416   | end 4b6fc659283f2735616c.db
  6417   6417   }]} {}
  6418   6418   
  6419   6419   do_catchsql_test 47.1 {
  6420   6420     INSERT INTO t1(t1) VALUES('integrity-check');
  6421         -} {1 {database disk image is malformed}}
         6421  +} {/*malformed database schema*/}
  6422   6422   
  6423   6423   do_catchsql_test 47.2 {
  6424   6424     SELECT count(*) FROM (
  6425   6425         SELECT snippet(t1, -1, '.', '..', '[', 50), 
  6426   6426         highlight(t1, 2, '[', ']') FROM t1('g h') 
  6427   6427         WHERE rank MATCH 'bm25(1.0, 1.0)' ORDER BY rank
  6428   6428     )
  6429         -} {0 3}
         6429  +} {/*malformed database schema*/}
  6430   6430   
  6431   6431   #--------------------------------------------------------------------------
  6432   6432   reset_db
  6433   6433   do_test 48.0 {
  6434   6434     sqlite3 db {}
  6435   6435     db deserialize [decode_hexdb {
  6436   6436   | size 32768 pagesize 4096 filename crash-44a8305b4bd86f.db
................................................................................
  6904   6904   
  6905   6905   do_catchsql_test 50.1 {
  6906   6906     SELECT term FROM t4 WHERE term LIKE '»as';
  6907   6907   } {1 {database disk image is malformed}}
  6908   6908   
  6909   6909   #-------------------------------------------------------------------------
  6910   6910   reset_db
  6911         -do_execsql_test 51.1 {
         6911  +do_execsql_test 51.0 {
  6912   6912   BEGIN TRANSACTION;
  6913   6913   PRAGMA writable_schema=ON;
  6914   6914   CREATE VIRTUAL TABLE t1 USING fts5(a,b,c);
  6915   6915   CREATE TABLE IF NOT EXISTS 't1_data'(id INTEGER PRIMARY KEY, block BLOB);
  6916   6916   REPLACE INTO t1_data VALUES(1,X'2eb1182424');
  6917   6917   REPLACE INTO t1_data VALUES(10,X'000000000102080002010101020107');
  6918   6918   INSERT INTO t1_data VALUES(137438953473,X'0000032b0230300102060102060102061f0203010203010203010832303136303630390102070102070102070101340102050102050102050101350102040102040102040207303030303030301c023d010204010204010662696e6172790306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020108636f6d70696c657201020201020201020201066462737461740702030102030102030204656275670402020102020102020107656e61626c6507020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020201020202087874656e73696f6e1f02040102040102040104667473340a02030102030102030401350d020301020301020301036763630102030102030102030206656f706f6c7910020301020301020301056a736f6e3113020301020301020301046c6f61641f020301020301020301036d61781c02020102020102020205656d6f72791c020301020301020304047379733516020301020301020301066e6f6361736502060102020306010202030601020213060102020306010202030601020203060102020306010202030601020203060102020306010202030601020201046f6d69741f0202010202010202010572747265651902030102030102030402696d010601020203060102020306010202030601020203060102020306010202030601020203060102020306010202030601020203060102020306010202010a7468726561647361666522020201020201020201047674616207020401020401020401017801060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102ad060101020106010102010601010201060101020106010101010601010201060101020106010102010601010201060101020106010102010601010201060101020106010102010601010201060101020415130c0c124413110f47130efc0e11100f0e100f440f1040150f');
................................................................................
  6972   6972   INSERT INTO t2 VALUES('integrity-check');
  6973   6973   PRAGMA writable_schema=OFF;
  6974   6974   COMMIT;
  6975   6975   } {}
  6976   6976   
  6977   6977   do_catchsql_test 51.1 {
  6978   6978     SELECT max(rowid)==0 FROM t1('e*');
  6979         -} {0 0}
         6979  +} {1 {database disk image is malformed}}
  6980   6980   
  6981   6981   #--------------------------------------------------------------------------
  6982   6982   reset_db
  6983   6983   do_test 52.0 {
  6984   6984     sqlite3 db {}
  6985   6985     db deserialize [decode_hexdb {
  6986   6986   | size 40960 pagesize 4096 filename crash-2b92f77ddfe191.db
................................................................................
  7126   7126   |      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
  7127   7127   |   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
  7128   7128   | end crash-2b92f77ddfe191.db
  7129   7129   }]} {}
  7130   7130   
  7131   7131   do_catchsql_test 52.1 {
  7132   7132     SELECT fts5_decode(id, block) FROM t1_data;
  7133         -} {1 {database disk image is malformed}}
         7133  +} {/*malformed database schema*/}
  7134   7134   
  7135   7135   #-------------------------------------------------------------------------
  7136   7136   reset_db
  7137   7137   do_test 53.0 {
  7138   7138     sqlite3 db {}
  7139   7139     db deserialize [decode_hexdb {
  7140   7140   | size 24576 pagesize 4096 filename crash-dbe9b7614da103.db
................................................................................
  7342   7342   |   4080: 00 00 03 03 02 01 03 03 02 02 01 02 02 01 0c e9   ................
  7343   7343   | end crash-dbe9b7614da103.db
  7344   7344   }]} {}
  7345   7345   
  7346   7346   do_catchsql_test 53.1 {
  7347   7347     WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x<>1 FROM c WHERE x<10)
  7348   7348       INSERT INTO t1(a) SELECT randomblob(3000) FROM c;
  7349         -} {1 {database disk image is malformed}}
         7349  +} {/*malformed database schema*/}
  7350   7350   
  7351   7351   #-------------------------------------------------------------------------
  7352   7352   reset_db
  7353   7353   do_test 54.0 {
  7354   7354     sqlite3 db {}
  7355   7355     db deserialize [decode_hexdb {
  7356   7356   | size 24576 pagesize 4096 filename crash-03a1855566d9ae.db
................................................................................
  7558   7558   |      0: 0d 00 00 00 03 0f f2 00 0f fc 0f f7 0f f2 00 00   ................
  7559   7559   |   4080: 00 00 23 03 02 01 03 03 02 02 01 02 02 00 f2 09   ..#.............
  7560   7560   | end crash-03a1855566d9ae.db
  7561   7561   }]} {}
  7562   7562   
  7563   7563   do_catchsql_test 54.1 {
  7564   7564     SELECT rowid==-1 FROM t1('t*');
  7565         -} {0 {0 0 0}}
         7565  +} {/*malformed database schema*/}
  7566   7566   
  7567   7567   #-------------------------------------------------------------------------
  7568   7568   reset_db
  7569   7569   do_test 55.0 {
  7570   7570     sqlite3 db {}
  7571   7571     db deserialize [decode_hexdb {
  7572   7572   | size 32768 pagesize 4096 filename crash-b366b5ac0d3887.db
................................................................................
  7773   7773   |      0: 0d 00 00 00 03 0f d6 00 0f f4 0f e9 0f d6 00 00   ................
  7774   7774   |   4048: 00 00 00 00 00 00 11 03 02 2b 69 6e 74 65 77 72   .........+intewr
  7775   7775   |   4064: 69 74 79 2d 63 68 65 63 6b 09 02 02 1b 72 65 62   ity-check....reb
  7776   7776   |   4080: 75 69 6c 64 0a 01 02 1d 6f 70 74 69 6d 69 7a 65   uild....optimize
  7777   7777   | end crash-b366b5ac0d3887.db
  7778   7778   }]} {}
  7779   7779   
  7780         -do_execsql_test 55.1 {
         7780  +do_catchsql_test 55.1 {
  7781   7781     SAVEPOINT one;
  7782   7782     DELETE FROM t1 WHERE a MATCH 'ts';
  7783         -}
         7783  +} {/*malformed database schema*/}
  7784   7784   
  7785   7785   do_execsql_test 55.2 {
  7786   7786     ROLLBACK TO one;
  7787   7787   }
  7788   7788   
  7789   7789   #-------------------------------------------------------------------------
  7790   7790   reset_db
................................................................................
  8009   8009     # may return SQLITE_CONSTRAINT instead of SQLITE_CORRUPT. This is because
  8010   8010     # the corrupt db in the test over-reads the page buffer slightly, with
  8011   8011     # different results depending on whether or not the page-cache is in use.
  8012   8012     if {$res=="1 {constraint failed}"} {
  8013   8013       set res "1 {database disk image is malformed}"
  8014   8014     }
  8015   8015     set res
  8016         -} {1 {database disk image is malformed}}
         8016  +} {/*malformed database schema*/}
  8017   8017   
  8018   8018   #-------------------------------------------------------------------------
  8019   8019   reset_db
  8020   8020   do_test 57.0 {
  8021   8021     sqlite3 db {}
  8022   8022     db deserialize [decode_hexdb {
  8023   8023   | size 28672 pagesize 4096 filename x.db
................................................................................
  8127   8127   |   4064: 64 11 02 02 2b 69 6e 74 65 67 72 69 74 79 2d 63   d...+integrity-c
  8128   8128   |   4080: 68 65 63 6b 0a 01 02 1d 6f 70 74 69 6d 69 7a 65   heck....optimize
  8129   8129   | end x.db
  8130   8130   }]} {}
  8131   8131   
  8132   8132   do_catchsql_test 57.1 {
  8133   8133     INSERT INTO t1(t1) VALUES('optimize')
  8134         -} {1 {database disk image is malformed}}
         8134  +} {/*malformed database schema*/}
  8135   8135   
  8136   8136   #-------------------------------------------------------------------------
  8137   8137   reset_db
  8138   8138   do_test 58.0 {
  8139   8139     sqlite3 db {}
  8140   8140     db deserialize [decode_hexdb {
  8141   8141   .open --hexdb
................................................................................
  8335   8335   |   4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70   ...............p
  8336   8336   |   4080: 67 73 7a 18 0b 03 1b 01 76 65 72 73 69 6f 6e 04   gsz.....version.
  8337   8337   | page 6 offset 20480
  8338   8338   |   4080: 00 00 23 03 02 01 03 03 02 00 00 00 00 00 00 00   ..#.............
  8339   8339   | end crash-5a5acd0ab42d31.db
  8340   8340   }]} {}
  8341   8341   
  8342         -do_execsql_test 58.1 {
         8342  +do_catchsql_test 58.1 {
  8343   8343     SELECT * FROM t1('t*');
  8344         -} {{} {} {} {} {} {}}
         8344  +} {/*malformed database schema*/}
  8345   8345   
  8346   8346   #-------------------------------------------------------------------------
  8347   8347   do_test 59.0 {
  8348   8348     sqlite3 db {}
  8349   8349     db deserialize [decode_hexdb {
  8350   8350   .open --hexdb
  8351   8351   | size 32768 pagesize 4096 filename crash-96b136358d01ec.db
................................................................................
  8540   8540   | page 8 offset 28672
  8541   8541   |   4048: 00 00 00 00 00 00 5d 03 02 2b 69 6e 74 00 00 00   ......]..+int...
  8542   8542   | end crash-96b136358d01ec.db
  8543   8543   }]} {}
  8544   8544   
  8545   8545   do_catchsql_test 59.1 {
  8546   8546     SELECT (matchinfo(591,t1)) FROM t1 WHERE t1 MATCH 'e*eŸ'
  8547         -} {0 {}}
         8547  +} {1 {database disk image is malformed}}
  8548   8548   
  8549   8549   #-------------------------------------------------------------------------
  8550   8550   do_test 60.0 {
  8551   8551     sqlite3 db {}
  8552   8552     db deserialize [decode_hexdb {
  8553   8553   .open --hexdb
  8554   8554   | size 32768 pagesize 4096 filename crash-c77b90b929dc92.db
................................................................................
  8746   8746   |   4048: 00 00 00 00 00 00 5d 03 00 00 00 00 00 00 00 00   ......].........
  8747   8747   | end crash-c77b90b929dc92.db
  8748   8748   }]} {}
  8749   8749   
  8750   8750   
  8751   8751   do_catchsql_test 60.2 {
  8752   8752     SELECT (matchinfo(t1,591)) FROM t1 WHERE t1 MATCH 'e*eŸ'
  8753         -} {0 {}}
         8753  +} {1 {database disk image is malformed}}
  8754   8754   
  8755   8755   #-------------------------------------------------------------------------
  8756   8756   do_test 61.0 {
  8757   8757     sqlite3 db {}
  8758   8758     db deserialize [decode_hexdb {
  8759   8759   .open --hexdb
  8760   8760   | size 28672 pagesize 4096 filename crash-e5fa281edabddf.db
................................................................................
  8944   8944   |   4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01   ................
  8945   8945   | page 7 offset 24576
  8946   8946   |      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
  8947   8947   |   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
  8948   8948   | end crash-e5fa281edabddf.db
  8949   8949   }]} {}
  8950   8950   
  8951         -do_execsql_test 61.1 {
         8951  +do_catchsql_test 61.1 {
  8952   8952     CREATE VIRTUAL TABLE t3 USING fts5vocab('t1'(),'col' );
  8953         -} 
         8953  +} {/*malformed database schema*/}
  8954   8954   
  8955   8955   do_catchsql_test 61.2 {
  8956   8956     SELECT * FROM t3 ORDER BY rowid;
  8957         -} {1 {database disk image is malformed}}
         8957  +} {/*malformed database schema*/}
  8958   8958   
  8959   8959   breakpoint
  8960   8960   #-------------------------------------------------------------------------
  8961   8961   do_test 62.0 {
  8962   8962     sqlite3 db {}
  8963   8963     db deserialize [decode_hexdb {
  8964   8964   .open --hexdb
................................................................................
  9155   9155   |   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
  9156   9156   | end crash-44942694542e1e.db
  9157   9157   }]} {}
  9158   9158   
  9159   9159   do_catchsql_test 62.1 {
  9160   9160     WITH c(x) AS (VALUES(false) UNION ALL SELECT x+1 FROM c WHERE x<72)
  9161   9161       INSERT INTO t1(a) SELECT randomblob(2829) FROM c;
  9162         -} {0 {}}
         9162  +} {/*malformed database schema*/}
         9163  +
         9164  +#---------------------------------------------------------------------------
         9165  +do_test 63.0 {
         9166  +  sqlite3 db {}
         9167  +  db deserialize [decode_hexdb {
         9168  +.open --hexdb
         9169  +| size 24576 pagesize 4096 filename crash-8230e6c3b368f5.db
         9170  +| page 1 offset 0
         9171  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         9172  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
         9173  +|     96: 00 00 00 00 0d 00 00 00 06 0e 0f 00 0f aa 0f 53   ...............S
         9174  +|    112: 0e e8 0e 8b 0e 33 0e 0f 00 00 00 00 00 00 00 00   .....3..........
         9175  +|   3584: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 22   ................
         9176  +|   3600: 06 06 17 11 11 01 31 74 61 62 7c 65 62 63 62 62   ......1tab|ebcbb
         9177  +|   3616: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 62 62   .CREATE TABLE bb
         9178  +|   3632: 28 61 29 56 05 06 17 1f 1f 01 7d 74 61 62 6c 65   (a)V.......table
         9179  +|   3648: 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63 6f 6e 66   t1_configt1_conf
         9180  +|   3664: 69 67 05 43 52 45 41 54 45 20 54 41 42 4c 45 20   ig.CREATE TABLE 
         9181  +|   3680: 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b 20 50 52   't1_config'(k PR
         9182  +|   3696: 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20 57 49   IMARY KEY, v) WI
         9183  +|   3712: 54 48 4f 55 54 20 52 4f 57 49 44 5b 04 07 17 21   THOUT ROWID[...!
         9184  +|   3728: 21 01 81 01 74 61 62 6c 65 74 31 5f 64 6f 63 73   !...tablet1_docs
         9185  +|   3744: 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65 04 43 52   izet1_docsize.CR
         9186  +|   3760: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 9d   EATE TABLE 't1_.
         9187  +|   3776: 6f 63 73 69 7a 65 27 28 69 64 20 49 4e 54 45 47   ocsize'(id INTEG
         9188  +|   3792: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20   ER PRIMARY KEY, 
         9189  +|   3808: 73 7a 20 42 4c 4f 42 29 69 03 07 17 19 19 01 81   sz BLOB)i.......
         9190  +|   3824: 2d 74 61 62 6c 65 74 31 5f 69 64 78 74 31 5f 69   -tablet1_idxt1_i
         9191  +|   3840: 64 78 03 43 52 45 41 54 45 20 54 41 42 4c 45 20   dx.CREATE TABLE 
         9192  +|   3856: 27 74 31 5f 69 64 78 27 28 73 65 67 69 64 2c 20   't1_idx'(segid, 
         9193  +|   3872: 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50 52 49 4d   term, pgno, PRIM
         9194  +|   3888: 41 52 59 20 4b 45 59 28 73 65 67 69 64 2c 20 74   ARY KEY(segid, t
         9195  +|   3904: 65 72 6d 29 29 20 57 49 54 48 4f 55 54 20 52 4f   erm)) WITHOUT RO
         9196  +|   3920: 57 49 44 55 02 07 17 1b 1b 01 81 01 74 61 62 6c   WIDU........tabl
         9197  +|   3936: 65 64 31 5f 64 61 74 61 74 31 5f 64 61 74 61 02   ed1_datat1_data.
         9198  +|   3952: 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74 31   CREATE TABLE 't1
         9199  +|   3968: 5f 64 61 74 61 27 28 69 64 20 49 4e 54 45 47 45   _data'(id INTEGE
         9200  +|   3984: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 62   R PRIMARY KEY, b
         9201  +|   4000: 6c 6f 63 6b 20 42 4c 4f 42 29 54 01 07 17 10 11   lock BLOB)T.....
         9202  +|   4016: 08 81 15 74 61 62 6c 65 74 31 74 31 43 52 45 41   ...tablet1t1CREA
         9203  +|   4032: 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45   TE VIRTUAL TABLE
         9204  +|   4048: 20 74 31 20 55 53 49 4e 47 20 66 74 73 35 28 61    t1 USING fts5(a
         9205  +|   4064: 2c 62 2c 70 72 65 66 69 78 3d 22 31 2c 32 2c 33   ,b,prefix=.1,2,3
         9206  +|   4080: 2c 34 22 2c 20 63 6f 6e 74 65 6e 74 3d 22 22 29   ,4., content=..)
         9207  +| page 2 offset 4096
         9208  +|      0: 0d 0b 6a 00 37 09 4c 02 0f e7 09 4c 0f c6 0f a4   ..j.7.L....L....
         9209  +|     16: 0f 88 0f 6d 0f 4b 0f 2c 0f 0e 0e ec 0e cd 0e ad   ...m.K.,........
         9210  +|     32: 0e 8e 0e 6c 0e 4b 0e 29 0e 08 0d e6 0d c4 0d b5   ...l.K.)........
         9211  +|     48: 0d 97 0d 76 0d 54 0d 31 0d 15 0c f3 0c d3 0c b5   ...v.T.1........
         9212  +|     64: 0c 95 0c 73 0c 54 0c 32 0c 10 0b ee 0b cc 0b b0   ...s.T.2........
         9213  +|     80: 0b 8d 0b 7e 0b 48 0b 2e 0b 0b 0a ef 0a cc 0a ad   ...~.H..........
         9214  +|     96: 0a 8c 0a 6d 0a 4d 0a 2b 0a 0c 09 ec 09 ca 09 a8   ...m.M.+........
         9215  +|    112: 09 86 09 63 0f f1 00 00 00 00 00 00 00 00 00 00   ...c............
         9216  +|   2368: 00 00 00 00 00 00 00 00 00 00 00 00 15 0a 03 00   ................
         9217  +|   2384: 30 00 00 00 01 01 03 35 00 03 01 01 12 02 01 12   0......5........
         9218  +|   2400: 03 01 11 1c 8c 80 80 80 80 10 03 00 3e 00 00 00   ............>...
         9219  +|   2416: 17 01 05 05 34 74 61 62 6c 03 02 03 01 04 77 68   ....4tabl.....wh
         9220  +|   2432: 65 72 03 02 06 09 1b 8c 80 80 80 80 0f 03 00 3c   er.............<
         9221  +|   2448: 00 00 00 16 05 34 66 74 73 34 03 02 02 01 04 6e   .....4fts4.....n
         9222  +|   2464: 75 6d 62 03 06 01 04 09 1b 8c 80 80 80 80 0e 03   umb.............
         9223  +|   2480: 00 3c 00 00 00 16 04 33 74 68 65 13 06 01 01 04   .<.....3the.....
         9224  +|   2496: 01 03 77 68 65 03 02 04 04 0a 1b 8c 80 80 80 80   ..whe...........
         9225  +|   2512: 0d 03 00 3c 00 00 00 16 04 33 6e 75 6d 03 06 01   ...<.....3num...
         9226  +|   2528: 01 05 01 03 75 61 62 03 02 03 04 0a 19 8c 80 80   ....uab.........
         9227  +|   2544: 80 80 0c 03 00 38 00 00 00 14 03 32 ec 68 03 02   .....8.....2.h..
         9228  +|   2560: 04 00 04 33 66 74 73 03 02 02 04 07 18 8c 80 80   ...3fts.........
         9229  +|   2576: 80 80 0b 03 00 36 00 00 00 13 03 32 74 61 03 02   .....6.....2ta..
         9230  +|   2592: 03 02 01 68 03 06 01 01 04 04 17 1b 8c 80 80 80   ...h............
         9231  +|   2608: 80 0a 03 00 3c 00 00 00 16 03 32 6e 75 03 06 01   ....<.....2nu...
         9232  +|   2624: 01 05 01 02 6f 66 03 06 01 01 06 04 09 19 8c 80   ....of..........
         9233  +|   2640: 80 80 80 09 03 00 38 00 00 00 14 03 32 66 74 03   ......8.....2ft.
         9234  +|   2656: 02 02 01 02 69 73 03 06 01 01 03 04 07 18 8c 80   ....is..........
         9235  +|   2672: 80 80 80 08 03 00 36 00 00 00 13 02 31 74 03 08   ......6.....1t..
         9236  +|   2688: 03 01 01 04 01 01 77 03 02 04 04 09 1a 8c 80 80   ......w.........
         9237  +|   2704: 80 80 07 03 00 3a 00 00 00 15 02 31 6e 03 08 01   .....:.....1n...
         9238  +|   2720: 01 02 05 01 01 6f 03 06 01 01 06 04 09 18 8c 80   .....o..........
         9239  +|   2736: 80 80 80 06 03 00 36 00 00 00 13 04 02 31 66 03   ......6......1f.
         9240  +|   2752: 02 02 01 01 69 03 06 01 01 03 05 06 1c 8c 80 80   ....i...........
         9241  +|   2768: 80 80 05 03 00 3e 00 00 00 17 04 30 74 68 65 03   .....>.....0the.
         9242  +|   2784: 06 01 01 04 01 05 77 68 65 72 65 03 02 04 0a 15   ......where.....
         9243  +|   2800: 8c 80 80 80 80 04 03 00 30 00 00 00 11 01 01 06   ........0.......
         9244  +|   2816: 06 30 74 61 62 6c 65 03 02 03 07 1c 8c 80 80 80   .0table.........
         9245  +|   2832: 80 03 03 00 3e 00 00 00 17 07 30 6e 75 6d 62 65   ....>.....0numbe
         9246  +|   2848: 72 03 06 01 01 05 01 02 6f 66 03 06 04 0d 13 8c   r.......of......
         9247  +|   2864: 80 80 80 80 02 03 00 2c 00 00 00 0f 01 01 03 02   .......,........
         9248  +|   2880: 30 6e 03 06 01 01 02 07 1b 8c 80 80 80 80 01 03   0n..............
         9249  +|   2896: 00 3c 00 00 00 16 08 30 66 74 73 34 61 75 78 03   .<.....0fts4aux.
         9250  +|   2912: 02 02 01 02 69 73 03 06 04 0c 00 00 00 14 2a 00   ....is........*.
         9251  +|   2928: 00 00 01 01 02 24 00 02 01 01 12 02 01 12 08 88   .....$..........
         9252  +|   2944: 80 80 80 80 12 03 00 16 00 00 00 05 02 1c 88 80   ................
         9253  +|   2960: 80 80 80 11 03 00 3e 00 00 00 17 05 34 72 6f 77   ......>.....4row
         9254  +|   2976: 73 02 06 01 01 05 01 04 74 68 65 72 02 02 04 0b   s.......ther....
         9255  +|   2992: 15 88 80 80 80 80 10 03 00 30 00 00 00 11 02 01   .........0......
         9256  +|   3008: 01 07 05 34 62 65 74 77 02 02 04 08 1b 88 80 80   ...4betw........
         9257  +|   3024: 80 80 0f 03 00 3c 00 00 00 16 04 04 33 72 6f 77   .....<......3row
         9258  +|   3040: 02 06 01 01 05 01 03 74 68 64 02 08 05 0a 1b 88   .......thd......
         9259  +|   3056: 80 80 80 80 0e 03 00 3c 00 00 00 16 01 01 02 04   .......<........
         9260  +|   3072: 33 61 72 65 02 02 03 01 03 62 65 74 02 02 07 08   3are.....bet....
         9261  +|   3088: 1b 88 80 80 80 80 0d 03 00 3c 00 00 00 16 03 32   .........<.....2
         9262  +|   3104: 74 68 02 08 02 01 01 07 00 04 33 61 6e 64 02 06   th........3and..
         9263  +|   3120: 04 0a 1b 88 80 80 80 80 0c 03 00 3c 00 00 00 16   ...........<....
         9264  +|   3136: 03 32 69 6e 02 06 01 01 06 01 02 72 6f 02 06 01   .2in.......ro...
         9265  +|   3152: 01 43 04 09 18 88 80 80 80 80 0b 03 00 36 00 00   .C...........6..
         9266  +|   3168: 00 13 02 03 32 61 72 02 02 03 01 02 62 65 02 02   ....2ar.....be..
         9267  +|   3184: 04 05 07 1b 88 80 80 80 80 0a 03 00 3c 00 00 00   ............<...
         9268  +|   3200: 16 02 31 74 02 08 02 01 01 07 00 03 32 61 6e 02   ..1t........2an.
         9269  +|   3216: 06 01 01 04 09 19 88 80 80 80 80 09 03 00 38 00   ..............8.
         9270  +|   3232: 00 00 14 02 31 6e 02 06 01 01 03 01 01 72 02 06   ....1n.......r..
         9271  +|   3248: 01 01 05 04 08 17 88 80 80 80 80 08 03 00 34 00   ..............4.
         9272  +|   3264: 00 00 12 02 31 62 02 02 04 01 01 69 02 06 01 01   ....1b.....i....
         9273  +|   3280: 06 04 06 19 88 80 80 80 80 07 03 00 38 00 00 00   ............8...
         9274  +|   3296: 14 04 02 31 32 02 02 05 01 01 61 02 08 03 01 01   ...12.....a.....
         9275  +|   3312: 02 05 06 1b 88 80 80 80 80 06 03 00 3c 00 00 00   ............<...
         9276  +|   3328: 16 06 30 74 68 65 72 65 02 02 01 00 02 30 21 02   ..0there.....0!.
         9277  +|   3344: 06 01 01 04 0a 15 88 80 80 80 80 05 03 00 30 00   ..............0.
         9278  +|   3360: 00 00 11 01 01 05 04 30 74 68 65 02 06 01 01 07   .......0the.....
         9279  +|   3376: 07 1c 88 80 80 80 80 04 03 00 3e 00 00 00 17 01   ..........>.....
         9280  +|   3392: 01 06 02 30 6e 02 06 01 01 03 01 04 72 6f 77 73   ...0n.......rows
         9281  +|   3408: 02 06 07 08 1b 88 80 80 80 80 03 03 00 3c 00 51   .............<.Q
         9282  +|   3424: 00 16 08 30 62 65 74 77 65 65 6e 02 02 04 01 02   ...0between.....
         9283  +|   3440: 69 6e 02 06 04 0c 1a 88 80 80 80 80 02 03 00 3a   in.............:
         9284  +|   3456: 00 00 00 15 04 30 61 6e 64 02 06 01 01 02 02 02   .....0and.......
         9285  +|   3472: 72 65 02 02 03 04 0a 17 88 80 80 80 80 01 03 00   re..............
         9286  +|   3488: 34 00 00 00 12 02 30 31 02 06 01 01 04 01 01 32   4.....01.......2
         9287  +|   3504: 02 02 05 04 08 08 84 80 80 80 80 12 03 00 16 00   ................
         9288  +|   3520: 00 00 05 04 1b 84 80 80 80 80 11 03 00 3c 00 00   .............<..
         9289  +|   3536: 00 16 05 34 74 51 62 6c 01 06 01 01 05 02 03 65   ...4tQbl.......e
         9290  +|   3552: 72 6d 01 02 04 0b 1b 84 80 80 80 80 10 03 00 3c   rm.............<
         9291  +|   3568: 00 00 00 16 05 34 65 17 63 68 01 02 03 01 04 70   .....4e.ch.....p
         9292  +|   3584: 72 65 73 01 02 05 04 09 1a 84 80 80 80 80 0f 03   res.............
         9293  +|   3600: 00 3a 00 00 00 15 04 33 74 65 72 01 02 04 02 02   .:.....3ter.....
         9294  +|   3616: 68 65 01 06 01 01 03 04 08 1b 84 80 80 80 80 0e   he..............
         9295  +|   3632: 03 00 3c 00 00 00 16 04 33 70 72 65 01 02 05 01   ..<.....3pre....
         9296  +|   3648: 03 74 61 62 01 06 01 01 05 04 08 1a 84 80 80 80   .tab............
         9297  +|   3664: 80 0d 03 00 3a 00 00 00 15 04 33 66 6f 72 01 03   ....:.....3for..
         9298  +|   3680: 02 02 02 74 73 01 06 01 01 04 04 08 1b 84 80 80   ...ts...........
         9299  +|   3696: 80 80 0c 03 00 3c 00 00 00 16 03 32 74 68 01 06   .....<.....2th..
         9300  +|   3712: 01 01 03 00 04 33 65 61 63 01 02 03 04 09 18 84   .....3eac.......
         9301  +|   3728: 80 80 80 80 0b 03 00 36 00 00 00 13 03 32 74 61   .......6.....2ta
         9302  +|   3744: 01 06 01 01 05 02 01 65 01 02 04 04 09 19 84 80   .......e........
         9303  +|   3760: 80 80 80 0a 03 00 38 00 00 00 14 03 32 69 6e 01   ......8.....2in.
         9304  +|   3776: 06 01 01 02 01 02 70 72 01 02 05 04 09 18 84 80   ......pr........
         9305  +|   3792: 80 80 80 09 03 00 36 00 00 00 13 03 32 66 6f 01   ......6.....2fo.
         9306  +|   3808: 02 02 02 01 74 01 06 01 01 04 04 07 1b 84 80 80   ....t...........
         9307  +|   3824: 80 80 08 03 00 3c 00 00 00 16 02 31 74 01 0a 04   .....<.....1t...
         9308  +|   3840: 01 01 03 04 00 03 32 65 61 01 02 03 04 0a 17 84   ......2ea.......
         9309  +|   3856: 80 80 80 80 07 03 00 34 00 00 00 12 02 31 69 01   .......4.....1i.
         9310  +|   3872: 06 01 01 02 01 01 70 01 02 05 04 08 18 84 80 80   ......p.........
         9311  +|   3888: 80 80 06 03 00 36 00 00 00 12 02 31 65 01 02 02   .....6.....1e...
         9312  +|   3904: 01 01 66 01 08 02 01 01 04 04 06 1b 84 80 80 80   ..f.............
         9313  +|   3920: 80 05 03 00 3c 00 00 00 16 05 30 74 65 72 6d 01   ....<.....0term.
         9314  +|   3936: 02 04 02 02 68 65 01 06 01 01 03 04 09 14 84 80   ....he..........
         9315  +|   3952: 80 80 80 04 03 00 2e 00 00 00 10 06 30 74 61 62   ............0tab
         9316  +|   3968: 6c 65 01 06 01 01 05 04 15 84 80 80 80 80 03 03   le..............
         9317  +|   3984: 00 30 00 00 00 11 02 08 30 70 72 65 73 65 6e 74   .0......0present
         9318  +|   4000: 01 02 05 05 1b 84 80 80 80 80 02 03 00 3c 00 00   .............<..
         9319  +|   4016: 00 16 04 30 66 74 73 01 06 01 01 04 01 02 69 6e   ...0fts.......in
         9320  +|   4032: 01 06 01 01 04 0a 1a 84 80 80 80 80 01 03 00 3a   ...............:
         9321  +|   4048: 00 00 00 15 05 30 65 61 63 68 01 02 03 01 03 66   .....0each.....f
         9322  +|   4064: 6f 72 01 02 01 f4 09 06 01 03 00 12 03 0b 0f 00   or..............
         9323  +|   4080: 00 08 8c 80 80 80 80 11 03 00 16 00 00 00 05 04   ................
         9324  +| page 3 offset 8192
         9325  +|      0: 0a 00 00 00 32 0e 4f 00 0f fa 0f f1 0f e9 0f e1   ....2.O.........
         9326  +|     16: 0f d8 0f d1 0f c9 0f c1 0f b9 0f b1 0f a9 0f a0   ................
         9327  +|     32: 0f 98 0f 90 0f 87 0f 80 0f 78 0f 71 0f 68 0f 5f   .........x.q.h._
         9328  +|     48: 0f 56 0f 4d 0f 41 0f 38 0f 2f 0f 26 0f 1d 0f 13   .V.M.A.8./.&....
         9329  +|     64: 0f 0a 0f 01 0e f7 0e ee 0e e6 0e dd 0e d6 0e cd   ................
         9330  +|     80: 0e c3 0e ba 0e b0 0e a8 0e 9f 0e 00 00 00 00 00   ................
         9331  +|   3648: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 08   ................
         9332  +|   3664: 04 01 10 01 03 34 74 20 07 04 01 0e 01 03 34 1e   .....4t ......4.
         9333  +|   3680: 09 04 01 12 01 03 33 74 68 1c 08 04 01 10 01 03   ......3th.......
         9334  +|   3696: 33 6e 1a 08 04 01 10 01 03 32 77 18 08 04 01 10   3n.......2w.....
         9335  +|   3712: 01 03 32 74 16 08 04 01 10 01 03 32 6e 14 07 04   ..2t.......2n...
         9336  +|   3728: 01 0e 01 03 32 12 08 04 01 10 01 03 31 74 10 07   ....2.......1t..
         9337  +|   3744: f4 01 10 01 03 31 6e 0e 07 04 01 0e 01 03 31 0c   .....1n.......1.
         9338  +|   3760: 09 04 01 12 01 03 30 74 68 0a 08 04 01 10 01 03   ......0th.......
         9339  +|   3776: 30 74 08 09 04 01 12 01 03 30 6e 75 06 08 04 01   0t.......0nu....
         9340  +|   3792: 10 01 03 30 6e 04 06 04 01 0c 01 03 02 08 04 01   ...0n...........
         9341  +|   3808: 10 01 02 34 73 22 07 04 01 0e 01 02 34 20 08 04   ...4s.......4 ..
         9342  +|   3824: 01 10 01 02 33 72 1e 09 04 01 12 01 02 33 61 72   ....3r.......3ar
         9343  +|   3840: 1c 08 04 01 10 01 02 32 74 1a 08 04 01 10 01 02   .......2t.......
         9344  +|   3856: 32 69 18 09 04 01 12 01 02 32 61 72 16 08 04 01   2i.......2ar....
         9345  +|   3872: 10 01 02 31 74 14 08 04 01 10 01 02 31 6e 12 08   ...1t.......1n..
         9346  +|   3888: 04 01 10 01 02 31 62 10 08 04 01 10 01 02 31 32   .....1b.......12
         9347  +|   3904: 0e 0b 04 01 16 01 02 30 74 00 00 00 00 00 00 00   .......0t.......
         9348  +| page 4 offset 12288
         9349  +|   4064: 00 00 00 00 00 00 00 00 00 00 00 05 02 03 00 10   ................
         9350  +|   4080: 03 05 05 02 03 00 10 04 06 05 01 03 00 10 04 04   ................
         9351  +| page 5 offset 16384
         9352  +|      0: 0a 00 00 00 02 0f eb 00 0f eb 0f f4 00 00 00 00   ................
         9353  +|   4064: 00 00 00 00 00 00 00 00 00 00 00 08 03 15 01 70   ...............p
         9354  +|   4080: 67 73 7a 08 0b 03 1b 01 76 65 72 73 69 6f 6e 04   gsz.....version.
         9355  +| end crash-8230e6c3b368f5.db
         9356  +}]} {}
         9357  +
         9358  +do_catchsql_test 63.1 {
         9359  +  SELECT * FROM t1 WHERE b MATCH 'thead*thead*theSt*';
         9360  +} {/*malformed database schema*/}
         9361  +
         9362  +do_catchsql_test 63.2 {
         9363  +  INSERT INTO t1(t1) VALUES('optimize');
         9364  +} {/*malformed database schema*/}
         9365  +
         9366  +do_catchsql_test 63.3 {
         9367  +  SELECT * FROM t1 WHERE b MATCH 'thead*thead*theSt*';
         9368  +} {/*malformed database schema*/}
         9369  +
         9370  +#---------------------------------------------------------------------------
         9371  +do_test 64.0 {
         9372  +  sqlite3 db {}
         9373  +  db deserialize [decode_hexdb {
         9374  +.open --hexdb
         9375  +| size 28672 pagesize 4096 filename crash-4470f0b94422f7.db
         9376  +| page 1 offset 0
         9377  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         9378  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 06   .....@  ........
         9379  +|     32: 00 00 00 00 00 00 00 00 00 00 00 06 00 00 00 04   ................
         9380  +|     96: 00 00 00 00 0d 00 00 00 06 0d e2 00 0f c4 0f 6a   ...............j
         9381  +|    112: 0e fc 0e 9d 0e 3d 0d e2 00 00 00 00 00 01 00 00   .....=..........
         9382  +|   3552: 00 00 59 06 06 17 21 21 01 7f 74 61 62 6c 65 74   ..Y...!!..tablet
         9383  +|   3568: 74 74 5f 63 6f 6e 66 69 67 74 74 74 5f 63 6f 6e   tt_configttt_con
         9384  +|   3584: 66 69 67 06 43 52 45 41 54 45 20 54 41 42 4c 45   fig.CREATE TABLE
         9385  +|   3600: 20 27 74 74 74 5f 63 6f 6e 66 69 67 27 28 6b 20    'ttt_config'(k 
         9386  +|   3616: 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29 20   PRIMARY KEY, v) 
         9387  +|   3632: 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5e 05 07   WITHOUT ROWID^..
         9388  +|   3648: 17 23 23 01 81 03 74 61 62 6c 65 74 74 74 5f 64   .##...tablettt_d
         9389  +|   3664: 6f 63 73 69 7a 65 74 74 74 5f 64 6f 63 73 69 7a   ocsizettt_docsiz
         9390  +|   3680: 65 05 43 52 45 41 54 45 20 54 41 42 4c 45 20 27   e.CREATE TABLE '
         9391  +|   3696: 74 74 74 5f 64 6f 63 73 69 7a 65 27 28 69 64 20   ttt_docsize'(id 
         9392  +|   3712: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20   INTEGER PRIMARY 
         9393  +|   3728: 4b 45 59 2c 20 73 7a 20 42 4c 4f 42 29 5d 04 07   KEY, sz BLOB)]..
         9394  +|   3744: 17 23 23 01 81 01 74 61 62 6c 65 74 74 74 5f 63   .##...tablettt_c
         9395  +|   3760: 6f 6e 74 65 6e 74 74 74 74 5f 63 6f 6e 74 65 6e   ontentttt_conten
         9396  +|   3776: 74 04 43 52 45 41 54 45 20 54 41 42 4c 45 20 27   t.CREATE TABLE '
         9397  +|   3792: 74 74 74 5f 63 6f 6e 74 65 6e 74 27 28 69 64 20   ttt_content'(id 
         9398  +|   3808: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20   INTEGER PRIMARY 
         9399  +|   3824: 4b 45 59 2c 20 63 30 2c 20 63 31 29 6c 03 07 17   KEY, c0, c1)l...
         9400  +|   3840: 1b 1b 01 81 2f 74 61 62 6c 65 74 74 74 5f 69 64   ..../tablettt_id
         9401  +|   3856: 78 74 74 74 5f 69 64 78 03 43 52 45 41 54 45 20   xttt_idx.CREATE 
         9402  +|   3872: 54 41 42 4c 45 20 27 74 74 74 5f 69 64 78 27 28   TABLE 'ttt_idx'(
         9403  +|   3888: 73 65 67 69 64 2c 20 74 65 72 6d 2c 20 70 67 6e   segid, term, pgn
         9404  +|   3904: 6f 2c 20 50 52 49 4d 41 52 59 20 4b 45 59 28 73   o, PRIMARY KEY(s
         9405  +|   3920: 65 67 69 64 2c 20 74 65 72 6d 29 29 20 57 49 54   egid, term)) WIT
         9406  +|   3936: 48 4f 55 54 20 52 4f 57 49 44 58 02 07 17 1d 1d   HOUT ROWIDX.....
         9407  +|   3952: 01 81 03 74 61 62 6c 65 74 74 74 5f 64 61 74 61   ...tablettt_data
         9408  +|   3968: 74 74 74 5f 64 61 74 61 02 43 52 45 41 54 45 20   ttt_data.CREATE 
         9409  +|   3984: 54 41 42 4c 45 20 27 74 74 74 5f 64 61 74 61 27   TABLE 'ttt_data'
         9410  +|   4000: 28 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d   (id INTEGER PRIM
         9411  +|   4016: 41 52 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42   ARY KEY, block B
         9412  +|   4032: 4c 4f 42 29 3a 01 06 17 13 13 08 5f 74 61 62 6c   LOB):......_tabl
         9413  +|   4048: 65 74 74 74 74 74 74 43 52 45 41 54 45 20 56 49   ettttttCREATE VI
         9414  +|   4064: 52 54 55 41 4c 20 54 41 42 4c 45 20 74 74 74 20   RTUAL TABLE ttt 
         9415  +|   4080: 55 53 49 4e 47 20 66 74 73 35 28 61 2c 20 62 29   USING fts5(a, b)
         9416  +| page 2 offset 4096
         9417  +|      0: 0d 0f 44 00 05 0e 81 00 0f 1a 0e 81 0f af 0f 58   ..D............X
         9418  +|     16: 0e 98 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
         9419  +|   3712: 00 15 0a 03 00 30 00 00 00 00 01 03 03 00 03 01   .....0..........
         9420  +|   3728: 01 01 02 01 01 03 01 01 81 24 8c 80 80 80 80 01   .........$......
         9421  +|   3744: 04 00 82 4c 00 00 00 9b 02 30 65 03 1a 02 05 05   ...L.....0e.....
         9422  +|   3760: 07 05 01 01 04 03 03 08 03 03 01 2e 02 05 05 07   ................
         9423  +|   3776: 05 07 05 07 05 01 01 04 03 03 08 03 03 08 03 03   ................
         9424  +|   3792: 07 f3 03 02 01 65 03 1e 03 05 05 04 05 05 01 00   .....e..........
         9425  +|   3808: 03 06 04 04 06 04 03 01 36 03 05 05 04 06 05 04   ........6.......
         9426  +|   3824: 06 05 04 05 05 01 01 03 06 04 04 06 04 04 06 04   ................
         9427  +|   3840: 04 06 04 03 03 01 65 03 14 04 05 06 f5 05 01 01   ......e.........
         9428  +|   3856: 02 08 09 01 20 04 05 07 05 07 05 07 05 05 01 00   .... ...........
         9429  +|   3872: 02 08 0a 0a 0a 04 01 65 03 02 0a 01 06 0a 0a 0a   .......e........
         9430  +|   3888: 05 01 65 03 06 01 01 0a 01 0a 01 01 0a 0a 0a 04   ..e.............
         9431  +|   3904: 2b 31 21 0b 0f ef 00 14 2a 00 00 00 00 01 02 02   +1!.....*.......
         9432  +|   3920: 00 02 01 01 01 02 01 01 50 88 80 80 80 80 01 04   ........P.......
         9433  +|   3936: 00 81 24 00 00 00 47 02 30 65 02 1a 02 05 05 07   ..$...G.0e......
         9434  +|   3952: 05 01 01 04 03 03 08 03 03 02 01 65 02 1e 03 05   ...........e....
         9435  +|   3968: 05 04 05 05 01 01 03 06 04 04 06 04 03 03 01 65   ...............e
         9436  +|   3984: 02 14 04 05 07 05 05 01 01 02 08 0a 04 01 65 02   ..............e.
         9437  +|   4000: 02 0a 05 01 65 02 06 01 01 0a 04 12 14 0f 06 31   ....e..........1
         9438  +|   4016: 84 80 80 80 80 01 03 00 68 00 00 00 2b 02 30 65   ........h...+.0e
         9439  +|   4032: 01 10 02 05 05 01 01 04 03 03 02 01 65 01 12 03   ............e...
         9440  +|   4048: 05 05 01 01 03 06 04 03 03 01 65 01 0e 04 05 05   ..........e.....
         9441  +|   4064: 01 01 02 08 04 0d 0e 06 01 03 00 12 04 4c 4c 00   .............LL.
         9442  +|   4080: 00 00 11 24 00 00 00 00 01 01 01 00 01 01 01 01   ...$............
         9443  +| page 3 offset 8192
         9444  +|      0: 0a 00 00 00 03 0f ec 00 0f 00 00 00 00 00 00 00   ................
         9445  +|   4064: 00 00 00 00 00 00 00 00 00 00 00 00 06 04 01 0c   ................
         9446  +|   4080: 01 03 02 06 04 01 0c 01 02 02 05 04 09 0c 01 02   ................
         9447  +| page 4 offset 12288
         9448  +|      0: 0d 00 00 00 04 0e 1a 00 0f c7 0f 5b 0e ef 0e 1a   ...........[....
         9449  +|   3600: 00 00 00 00 00 00 00 00 00 00 81 52 04 06 00 81   ...........R....
         9450  +|   3616: 5d 81 55 65 20 65 65 20 65 65 65 20 65 20 65 65   ].Ue ee eee e ee
         9451  +|   3632: 20 65 65 65 20 65 20 65 65 20 65 65 65 66 20 65    eee e ee eeef e
         9452  +|   3648: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65   e eee e ee eee e
         9453  +|   3664: 20 65 65 20 65 65 65 65 20 65 65 20 65 65 65 20    ee eeee ee eee 
         9454  +|   3680: 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65   e ee eee e ee ee
         9455  +|   3696: 65 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65   ee ee eee e ee e
         9456  +|   3712: 65 65 20 65 20 65 65 20 65 65 65 65 65 65 20 65   ee e ee eeeeee e
         9457  +|   3728: 65 20 65 20 65 20 65 20 65 65 20 65 65 65 20 65   e e e e ee eee e
         9458  +|   3744: 65 20 65 65 65 65 65 20 65 65 20 65 20 65 1f 65   e eeeee ee e e.e
         9459  +|   3760: 20 65 65 20 65 65 65 20 65 65 20 65 65 65 65 65    ee eee ee eeeee
         9460  +|   3776: 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65 65    ee e e e ee eee
         9461  +|   3792: 20 65 65 20 65 65 65 65 65 20 65 65 20 65 20 65    ee eeeee ee e e
         9462  +|   3808: 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65 6a    e ee eee ee eej
         9463  +|   3824: 03 03 ff 75 71 65 20 65 65 1f 65 65 65 20 65 20   ...uqe ee.eee e 
         9464  +|   3840: 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65 65   ee eee e ee eeee
         9465  +|   3856: 20 65 65 20 65 65 65 20 65 20 65 65 20 65 65 65    ee eee e ee eee
         9466  +|   3872: 20 65 20 65 65 20 65 65 65 65 65 65 20 65 65 20    e ee eeeeee ee 
         9467  +|   3888: 65 20 65 20 65 20 65 65 20 65 65 65 20 65 65 20   e e e ee eee ee 
         9468  +|   3904: 65 65 65 65 65 20 65 65 20 65 20 65 20 65 20 65   eeeee ee e e e e
         9469  +|   3920: 65 20 65 65 65 20 65 65 20 65 65 6a 02 04 00 75   e eee ee eej...u
         9470  +|   3936: 40 65 20 65 65 20 65 65 65 20 65 20 65 65 20 65   @e ee eee e ee e
         9471  +|   3952: 65 65 20 65 20 65 65 20 65 65 65 65 20 65 65 20   ee e ee eeee ee 
         9472  +|   3968: 65 65 65 20 65 20 65 65 20 65 65 65 20 65 20 65   eee e ee eee e e
         9473  +|   3984: 65 20 65 65 65 65 65 65 20 65 65 20 65 20 65 20   e eeeeee ee e e 
         9474  +|   4000: 65 20 65 65 20 65 65 65 20 65 65 20 65 65 65 65   e ee eee ee eeee
         9475  +|   4016: 65 20 65 65 20 65 20 65 20 65 20 65 65 20 65 65   e ee e e e ee ee
         9476  +|   4032: 65 20 65 65 20 65 65 37 01 04 00 41 3f 65 20 65   e ee ee7...A?e e
         9477  +|   4048: 65 20 65 65 65 20 65 20 65 65 20 65 65 65 20 65   e eee e ee eee e
         9478  +|   4064: 20 65 65 20 65 65 65 65 65 65 20 65 65 20 65 20    ee eeeeee ee e 
         9479  +|   4080: 65 20 65 20 65 65 20 65 65 65 20 65 65 20 65 65   e e ee eee ee ee
         9480  +| page 5 offset 16384
         9481  +|      0: 0d 00 00 00 04 0f e4 00 0f f9 0f f2 0f eb 0f e4   ................
         9482  +|   4064: 00 00 00 00 05 04 03 00 10 21 21 05 03 03 00 10   .........!!.....
         9483  +|   4080: 11 11 05 02 03 00 10 11 11 05 01 03 00 10 09 09   ................
         9484  +| page 6 offset 20480
         9485  +|      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
         9486  +|   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
         9487  +| end crash-4470f0b94422f7.db
         9488  +}]} {}
         9489  +
         9490  +do_catchsql_test 64.1 {
         9491  +  SELECT * FROM ttt('e*');
         9492  +} {1 {database disk image is malformed}}
         9493  +
         9494  +#---------------------------------------------------------------------------
         9495  +do_test 65.0 {
         9496  +  sqlite3 db {}
         9497  +  db deserialize [decode_hexdb {
         9498  +.open --hexdb
         9499  +| size 28672 pagesize 4096 filename crash-3aef66940ace0c.db
         9500  +| page 1 offset 0
         9501  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
         9502  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
         9503  +|     96: 00 00 00 00 0d 0f c7 00 07 0d 92 00 0f 8d 0f 36   ...............6
         9504  +|    112: 0e cb 0e 6b 0e 0e 0d b6 0d 92 00 00 00 00 00 00   ...k............
         9505  +|   3472: 00 00 22 08 06 17 11 11 01 31 74 61 62 6c 65 74   .........1tablet
         9506  +|   3488: 32 74 32 08 43 52 45 41 54 45 20 54 41 42 4c 45   2t2.CREATE TABLE
         9507  +|   3504: 20 74 32 28 78 29 56 07 06 17 1f 1f 01 7d 74 61    t2(x)V.......ta
         9508  +|   3520: 62 6c 65 74 31 5f 63 6f 6e 66 69 67 74 31 5f 63   blet1_configt1_c
         9509  +|   3536: 6f 6e 66 69 67 07 43 52 45 41 54 45 20 54 41 42   onfig.CREATE TAB
         9510  +|   3552: 4c 45 20 27 74 31 5f 63 6f 6e 66 69 67 27 28 6b   LE 't1_config'(k
         9511  +|   3568: 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 76 29    PRIMARY KEY, v)
         9512  +|   3584: 20 57 49 54 48 4f 55 54 20 52 4f 57 49 44 5b 06    WITHOUT ROWID[.
         9513  +|   3600: 07 17 21 21 01 81 01 74 61 62 6c 65 74 31 5f 64   ..!!...tablet1_d
         9514  +|   3616: 6f 63 73 69 7a 65 74 31 5f 64 6f 63 73 69 7a 65   ocsizet1_docsize
         9515  +|   3632: 06 43 52 45 41 54 45 20 54 41 42 4c 45 20 27 74   .CREATE TABLE 't
         9516  +|   3648: 31 5f 64 6f 63 73 69 7a 65 27 28 69 64 20 49 4e   1_docsize'(id IN
         9517  +|   3664: 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45   TEGER PRIMARY KE
         9518  +|   3680: 59 2c 20 73 7a 20 42 4c 4f 42 29 5e 05 07 17 21   Y, sz BLOB)^...!
         9519  +|   3696: 21 01 81 07 74 61 62 6c 65 74 31 5f 63 6f 6e 74   !...tablet1_cont
         9520  +|   3712: 65 6e 74 74 31 5f 63 6f 6e 74 65 6e 74 05 43 52   entt1_content.CR
         9521  +|   3728: 45 41 54 45 20 54 41 42 4c 45 20 27 74 31 5f 63   EATE TABLE 't1_c
         9522  +|   3744: 6f 6e 74 65 6e 74 27 28 69 64 20 49 4e 54 45 47   ontent'(id INTEG
         9523  +|   3760: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20   ER PRIMARY KEY, 
         9524  +|   3776: 63 30 2c 20 63 31 2c d6 63 32 29 69 04 07 17 19   c0, c1,.c2)i....
         9525  +|   3792: 19 01 81 2d 74 61 62 6c 65 74 31 5f 69 64 78 74   ...-tablet1_idxt
         9526  +|   3808: 31 5f 69 64 78 04 43 52 45 41 54 45 20 54 41 42   1_idx.CREATE TAB
         9527  +|   3824: 4c 45 20 27 74 31 5f 69 64 78 27 28 73 65 67 69   LE 't1_idx'(segi
         9528  +|   3840: 64 2c 20 74 65 72 6d 2c 20 70 67 6e 6f 2c 20 50   d, term, pgno, P
         9529  +|   3856: 52 49 4d 41 52 59 20 4b 45 59 28 73 65 67 69 64   RIMARY KEY(segid
         9530  +|   3872: 2c 20 74 65 72 6d 29 29 20 57 49 54 48 4f 55 54   , term)) WITHOUT
         9531  +|   3888: 20 52 4f 57 49 44 55 03 07 17 1b 1b 01 81 01 74    ROWIDU........t
         9532  +|   3904: 61 62 6c 65 74 31 5f 64 61 74 61 74 31 5f 64 61   ablet1_datat1_da
         9533  +|   3920: 74 61 03 43 52 45 41 54 45 20 54 41 42 4c 45 20   ta.CREATE TABLE 
         9534  +|   3936: 27 74 31 5f 64 61 74 61 27 28 69 64 20 49 4e 54   't1_data'(id INT
         9535  +|   3952: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59   EGER PRIMARY KEY
         9536  +|   3968: 2c 20 62 6c 6f 63 6b 20 42 4c 4f 42 29 38 02 06   , block BLOB)8..
         9537  +|   3984: 17 11 11 08 5f 74 61 62 6c 65 74 31 74 31 43 52   ...._tablet1t1CR
         9538  +|   4000: 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42   EATE VIRTUAL TAB
         9539  +|   4016: 4c 45 20 74 31 20 55 53 49 4e 47 20 66 74 73 35   LE t1 USING fts5
         9540  +|   4032: 28 61 2c 62 2c 63 29 00 00 00 00 00 00 00 00 00   (a,b,c).........
         9541  +| page 3 offset 8192
         9542  +|      0: 0d 00 00 00 03 0c 93 ff 0f e6 0f ef 0c 94 00 00   ................
         9543  +|   3216: 00 00 00 00 86 4a 84 80 80 80 80 01 04 00 8d 18   .....J..........
         9544  +|   3232: 00 00 03 2b 02 30 30 01 02 06 01 02 06 01 02 06   ...+.00.........
         9545  +|   3248: 1f 02 03 01 02 03 01 02 03 01 08 32 31 31 36 30   ...........21160
         9546  +|   3264: 36 30 39 01 02 07 01 02 07 01 02 07 01 01 33 f1   609...........3.
         9547  +|   3280: 02 05 01 02 05 01 02 05 01 01 35 01 02 03 01 02   ..........5.....
         9548  +|   3296: 04 01 02 04 02 07 30 30 30 30 30 30 30 1c 02 3d   ......0000000..=
         9549  +|   3312: 01 02 04 01 02 04 01 06 62 69 6e 61 72 79 03 06   ........binary..
         9550  +|   3328: 01 02 02 03 06 01 01 f2 03 06 4e 02 02 03 06 01   ..........N.....
         9551  +|   3344: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         9552  +|   3360: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         9553  +|   3376: 03 06 01 02 02 03 06 01 02 02 01 08 63 6f 6d 70   ............comp
         9554  +|   3392: 69 6c 65 72 01 02 02 01 02 02 01 02 02 01 06 64   iler...........d
         9555  +|   3408: 62 73 74 61 74 07 02 03 01 02 13 01 02 03 02 04   bstat...........
         9556  +|   3424: 65 62 75 67 04 02 02 01 02 02 01 02 02 01 07 65   ebug...........e
         9557  +|   3440: 6e 61 62 6c 65 07 02 02 01 02 02 01 02 02 01 02   nable...........
         9558  +|   3456: 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02 02   ................
         9559  +|   3472: 01 02 02 01 02 01 f1 02 02 01 02 02 01 02 02 01   ................
         9560  +|   3488: 02 02 01 02 02 01 02 02 01 02 02 01 02 02 01 02   ................
         9561  +|   3504: 02 01 02 02 02 08 76 b4 65 6e 73 69 6f 6e 1f 02   ......v.ension..
         9562  +|   3520: 04 01 02 04 01 02 04 01 04 66 74 73 34 0a 02 03   .........fts4...
         9563  +|   3536: 01 02 03 01 02 03 04 01 25 0d 02 03 01 02 03 01   ........%.......
         9564  +|   3552: 02 03 01 03 67 63 63 01 02 03 01 02 03 01 02 03   ....gcc.........
         9565  +|   3568: 02 06 65 6f 70 6f 6c 79 0f f2 03 01 02 03 01 02   ..eopoly........
         9566  +|   3584: 03 01 05 6a 73 6f 6e 31 13 02 03 01 02 03 01 02   ...json1........
         9567  +|   3600: 03 01 04 6c 6f 61 64 1f 02 03 01 02 03 01 02 03   ...load.........
         9568  +|   3616: 00 03 6d 61 78 1c 02 0c 01 02 02 01 02 02 02 05   ..max...........
         9569  +|   3632: 65 6d 6f 72 79 1c 02 03 01 02 03 01 02 03 04 04   emory...........
         9570  +|   3648: 73 79 73 35 16 02 03 01 02 03 01 02 03 01 06 6e   sys5...........n
         9571  +|   3664: 6f 63 61 73 65 02 06 01 02 02 13 06 00 f2 02 03   ocase...........
         9572  +|   3680: 06 01 12 02 13 06 01 02 02 03 06 01 02 02 03 06   ................
         9573  +|   3696: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         9574  +|   3712: 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02   ................
         9575  +|   3728: 02 01 04 6f 6d 69 74 1f 02 02 01 02 02 01 02 02   ...omit.........
         9576  +|   3744: 01 05 72 74 72 65 65 19 02 03 01 02 03 01 02 03   ..rtree.........
         9577  +|   3760: 04 02 69 6d 01 06 01 02 02 03 06 01 02 02 03 06   ..im............
         9578  +|   3776: 01 02 02 03 06 01 02 02 03 06 01 02 02 03 06 01   ................
         9579  +|   3792: 02 02 03 06 01 02 02 03 06 01 02 02 8e 06 01 02   ................
         9580  +|   3808: 02 03 06 01 02 02 03 06 01 02 02 03 06 01 02 02   ................
         9581  +|   3824: 01 0a 74 68 72 65 61 64 73 61 66 65 22 02 02 01   ..threadsafe....
         9582  +|   3840: 02 02 01 02 02 01 04 76 74 61 62 07 02 04 01 02   .......vtab.....
         9583  +|   3856: 04 01 02 04 01 01 78 01 06 01 01 02 01 06 01 01   ......x.........
         9584  +|   3872: 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02   ................
         9585  +|   3888: 01 06 01 11 02 01 06 01 01 02 01 06 01 01 02 01   ................
         9586  +|   3904: 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06   ................
         9587  +|   3920: 01 01 02 01 06 01 01 01 01 06 01 01 02 01 06 01   ................
         9588  +|   3936: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         9589  +|   3952: 02 01 06 01 01 01 f1 06 01 01 02 ad 06 01 01 02   ................
         9590  +|   3968: 01 06 01 01 02 01 06 01 01 02 01 06 01 01 02 01   ................
         9591  +|   3984: 06 01 01 01 01 06 01 01 02 01 06 01 01 02 01 06   ................
         9592  +|   4000: 01 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01   ................
         9593  +|   4016: 01 02 01 06 01 01 02 01 06 01 01 02 01 06 01 01   ................
         9594  +|   4032: 02 01 06 01 01 02 01 06 01 01 02 04 15 13 0c 0c   ................
         9595  +|   4048: 12 44 13 11 0f 47 13 0e fc 0e 11 10 0f 0e 10 0f   .D...G..........
         9596  +|   4064: 44 0f 10 40 15 0f 07 01 03 00 14 24 5a 24 24 0f   D..@.......$Z$$.
         9597  +|   4080: 0a 03 00 24 00 00 00 00 01 01 01 00 01 01 01 01   ...$............
         9598  +| page 4 offset 12288
         9599  +|      0: 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00   ................
         9600  +|   4080: 00 00 00 00 00 00 00 00 00 00 05 04 09 0c 01 02   ................
         9601  +| page 5 offset 16384
         9602  +|      0: 0d 00 00 00 24 0c 0a 00 00 00 00 00 00 00 00 00   ....$...........
         9603  +|   3072: 00 00 00 00 00 00 00 00 00 00 18 24 05 00 25 0f   ...........$..%.
         9604  +|   3088: 19 54 48 52 45 41 44 53 41 46 45 3d 30 58 42 49   .THREADSAFE=0XBI
         9605  +|   3104: 4e 41 52 59 18 23 05 00 25 0f 19 54 48 52 45 41   NARY.#..%..THREA
         9606  +|   3120: 44 53 41 46 45 3d 30 58 4e 4f 43 41 53 45 17 8f   DSAFE=0XNOCASE..
         9607  +|   3136: 05 00 25 0f 17 54 48 52 45 41 44 43 41 46 45 3d   ..%..THREADCAFE=
         9608  +|   3152: 30 58 52 54 52 49 4d 1f 21 05 00 33 0f 19 4f 4d   0XRTRIM.!..3..OM
         9609  +|   3168: 49 54 20 4b 4f 41 44 21 45 58 54 45 4e 53 49 4f   IT KOAD!EXTENSIO
         9610  +|   3184: 4e 58 42 49 4e 41 52 59 1f 20 05 00 33 0f 19 4f   NXBINARY. ..3..O
         9611  +|   3200: 4d 49 54 20 4c 4f 41 44 20 45 58 54 45 4e 53 49   MIT LOAD EXTENSI
         9612  +|   3216: 4f 4e 58 4e 4f 43 41 53 45 1e 1f 05 00 33 0f 17   ONXNOCASE....3..
         9613  +|   3232: 4f 4d 59 54 20 4c 4f 41 44 20 45 58 54 45 4e 53   OMYT LOAD EXTENS
         9614  +|   3248: 49 4f 4e 58 52 54 56 a9 4d 1f 1e 05 00 33 0f 19   IONXRTV.M....3..
         9615  +|   3264: 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30 30   MAX MEMORY=50000
         9616  +|   3280: 30 30 30 57 42 49 4e 31 52 59 1f 1d 05 00 33 0f   000WBIN1RY....3.
         9617  +|   3296: 19 4d 41 58 20 4d 45 4d 4f 52 59 3d 35 30 30 30   .MAX MEMORY=5000
         9618  +|   3312: 30 30 30 30 58 4e 4f 43 41 53 45 1e 1c 05 00 32   0000XNOCASE....2
         9619  +|   3328: 0f 17 4e 41 58 20 4d 45 4d 4f 52 59 2d 35 30 30   ..NAX MEMORY-500
         9620  +|   3344: 30 30 30 30 30 58 52 54 52 49 4d 18 1b 05 00 25   00000XRTRIM....%
         9621  +|   3360: 0f 19 45 4e 41 42 4c 45 20 52 54 52 45 45 58 42   ..ENABLE RTREEXB
         9622  +|   3376: 49 4e 41 52 59 18 1a 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         9623  +|   3392: 4c 45 20 52 54 52 45 45 59 4e 4f 43 41 53 45 17   LE RTREEYNOCASE.
         9624  +|   3408: 19 66 00 25 0f 17 45 4e 41 42 4c 45 20 52 54 52   .f.%..ENABLE RTR
         9625  +|   3424: 45 45 58 52 54 52 49 4d 1a 18 05 00 29 0f 19 45   EEXRTRIM....)..E
         9626  +|   3440: 4e 41 42 4c 45 20 4d 45 4d 53 59 53 35 58 42 49   NABLE MEMSYS5XBI
         9627  +|   3456: 4e 41 52 59 1a 17 05 00 29 0f 19 45 4e 41 42 4c   NARY....)..ENABL
         9628  +|   3472: 45 20 4d 45 4d 53 59 53 35 58 4e 4f 43 41 53 45   E MEMSYS5XNOCASE
         9629  +|   3488: 19 16 05 00 29 0f 17 45 4e 41 42 4c 45 20 4d 45   ....)..ENABLE ME
         9630  +|   3504: 4d 53 59 53 35 58 52 54 52 49 4d 18 15 05 10 25   MSYS5XRTRIM....%
         9631  +|   3520: 0f 19 45 4e 40 42 4c 45 20 4a 53 4f 4e 31 58 42   ..EN@BLE JSON1XB
         9632  +|   3536: 49 4e 41 52 59 18 14 05 00 25 0f 19 45 4e 41 42   INARY....%..ENAB
         9633  +|   3552: 4c 45 20 4a 53 4f 4e 32 58 4e 4f 43 41 53 45 17   LE JSON2XNOCASE.
         9634  +|   3568: 13 05 00 25 0f 17 45 4d 41 42 4c 45 20 4a 53 4f   ...%..EMABLE JSO
         9635  +|   3584: 4e 31 58 52 54 52 49 4d 1a 12 05 00 29 0f 19 45   N1XRTRIM....)..E
         9636  +|   3600: 4e 41 42 4c 45 20 47 45 4f 50 4f 4c 59 58 42 49   NABLE GEOPOLYXBI
         9637  +|   3616: 4e 41 52 59 1a 11 05 00 29 0f 19 45 4f 81 42 4c   NARY....)..EO.BL
         9638  +|   3632: 45 20 47 45 4f 50 4f 4c 59 58 4e 4f 43 51 53 45   E GEOPOLYXNOCQSE
         9639  +|   3648: 19 10 05 00 29 0f 17 45 4e 41 42 4c 45 20 47 45   ....)..ENABLE GE
         9640  +|   3664: 4f 50 4f 4c 59 58 52 54 52 49 4d 17 0f 05 00 23   OPOLYXRTRIM....#
         9641  +|   3680: 0f 1a 45 4e 41 42 4c 45 20 46 54 53 35 58 42 49   ..ENABLE FTS5XBI
         9642  +|   3696: 4e 41 52 59 17 0e 05 00 23 0f 19 45 4e 41 42 4c   NARY....#..ENABL
         9643  +|   3712: 45 20 46 54 53 35 48 4e 4f 43 41 53 45 16 1d 05   E FTS5HNOCASE...
         9644  +|   3728: 00 23 0f a4 45 4e 41 42 4c 45 20 46 54 53 35 58   .#..ENABLE FTS5X
         9645  +|   3744: 52 54 52 49 4d 17 0c 05 00 23 0f 19 45 4e 41 42   RTRIM....#..ENAB
         9646  +|   3760: 4c 45 20 46 55 53 34 58 42 49 4e 41 52 59 17 0b   LE FUS4XBINARY..
         9647  +|   3776: 05 00 23 0f 19 45 4e 41 42 4c 45 20 46 54 53 34   ..#..ENABLE FTS4
         9648  +|   3792: 57 4e 4f 43 41 53 45 16 0a 05 00 23 0f 17 45 4e   WNOCASE....#..EN
         9649  +|   3808: 41 42 4c 45 20 46 54 53 34 05 52 54 52 49 4d 1e   ABLE FTS4.RTRIM.
         9650  +|   3824: 09 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         9651  +|   3840: 54 41 54 20 56 54 41 42 58 42 49 4e 41 52 59 1e   TAT VTABXBINARY.
         9652  +|   3856: 08 05 00 31 0f 19 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         9653  +|   3872: 54 41 54 20 56 54 41 42 58 4e 4f 43 41 53 45 1d   TAT VTABXNOCASE.
         9654  +|   3888: 07 05 00 31 0f 17 45 4e 41 42 4c 45 20 44 42 53   ...1..ENABLE DBS
         9655  +|   3904: 54 41 54 20 56 54 41 42 58 52 54 52 49 4d 11 06   TAT VTABXRTRIM..
         9656  +|   3920: 05 00 17 0f 19 44 45 42 55 47 58 42 8a 4e 41 52   .....DEBUGXB.NAR
         9657  +|   3936: 59 11 05 05 00 17 0f 19 44 45 42 55 47 58 4e 4f   Y.......DEBUGXNO
         9658  +|   3952: 43 41 53 45 10 04 05 00 17 0f 17 44 45 42 55 47   CASE.......DEBUG
         9659  +|   3968: 58 52 54 52 49 4d 27 03 05 00 43 0f 19 43 4f 4d   XRTRIM'...C..COM
         9660  +|   3984: 50 49 4c 45 52 3d 67 63 63 2d 35 2e 34 2e 30 20   PILER=gcc-5.4.0 
         9661  +|   4000: 32 30 31 36 30 36 30 39 58 42 49 4e 41 52 59 27   20160609XBINARY'
         9662  +|   4016: 02 05 00 43 0f 19 43 4f 4d 50 49 4c 45 52 3f 87   ...C..COMPILER?.
         9663  +|   4032: 63 63 2d 35 2e 34 2e 30 20 32 30 31 36 30 36 30   cc-5.4.0 2016060
         9664  +|   4048: 39 58 4e 4f 43 41 53 45 26 01 05 00 43 0f 17 43   9XNOCASE&...C..C
         9665  +|   4064: 45 0d 60 59 4c 45 52 3d 67 63 63 2d 35 2e 34 2d   E.`YLER=gcc-5.4-
         9666  +|   4080: 30 20 32 30 31 36 30 36 30 39 00 00 00 00 00 00   0 20160609......
         9667  +| page 6 offset 20480
         9668  +|   3808: 06 24 03 00 12 02 01 01 06 23 03 00 12 02 01 01   .$.......#......
         9669  +|   3824: 06 22 03 01 12 02 01 01 06 21 03 00 12 03 01 01   .........!......
         9670  +|   3840: 06 20 03 00 12 03 01 01 06 1f 03 00 12 03 02 01   . ..............
         9671  +|   3856: 06 1e 03 00 12 03 01 01 06 1d 03 00 12 03 01 01   ................
         9672  +|   3872: 06 1c 03 00 12 03 01 01 06 1b 03 00 12 02 01 01   ................
         9673  +|   3888: 06 1a 03 00 12 02 01 01 06 19 03 00 12 02 01 01   ................
         9674  +|   3904: 06 18 03 00 12 02 01 01 06 17 03 00 12 02 01 01   ................
         9675  +|   3920: 06 16 03 00 12 02 01 01 06 15 03 00 12 02 01 01   ................
         9676  +|   3936: 06 14 03 00 12 02 01 01 06 13 03 00 12 02 01 01   ................
         9677  +|   3952: 06 12 03 00 12 02 01 01 06 11 03 00 12 02 01 01   ................
         9678  +|   3968: 06 00 03 00 12 02 01 01 06 0f 03 00 12 02 01 01   ................
         9679  +|   3984: 06 0e 03 00 12 02 01 01 06 0d 03 00 12 02 01 01   ................
         9680  +|   4000: 06 0c 03 00 12 02 01 01 06 0b 03 00 12 02 01 01   ................
         9681  +|   4016: 06 0a 03 00 12 02 01 01 06 09 03 00 12 03 01 01   ................
         9682  +|   4032: 06 08 03 00 12 03 01 01 06 07 03 00 12 03 01 01   ................
         9683  +|   4048: 06 06 03 00 12 01 01 01 06 05 03 00 12 01 01 01   ................
         9684  +|   4064: 06 04 03 00 12 01 01 01 06 03 03 00 12 06 01 01   ................
         9685  +|   4080: 06 02 03 00 12 06 01 01 06 01 03 00 12 06 01 01   ................
         9686  +| page 7 offset 24576
         9687  +|      0: 0a 00 00 00 01 0f f4 00 0f f4 00 00 00 00 00 00   ................
         9688  +|   4080: 00 00 00 00 0b 03 1b 01 76 65 72 73 69 6f 6e 04   ........version.
         9689  +| end crash-3aef66940ace0c.db
         9690  +}]} {}
         9691  +
         9692  +do_catchsql_test 65.1 {
         9693  +  SELECT ( MATCH (t1,591)) FROM t1 WHERE t1 MATCH 'e*eŸ'
         9694  +} {1 {database disk image is malformed}}
         9695  +
         9696  +
  9163   9697   
  9164   9698   sqlite3_fts5_may_be_corrupt 0
  9165   9699   finish_test
  9166   9700   

Changes to ext/fts5/test/fts5faultB.test.

   142    142     INSERT INTO t1 VALUES('b c d a');  -- 4
   143    143   }
   144    144   do_faultsim_test 5.1 -faults oom* -body {
   145    145     execsql { SELECT rowid FROM t1('^a OR ^b') }
   146    146   } -test {
   147    147     faultsim_test_result {0 {1 4}}
   148    148   }
          149  +
          150  +#-------------------------------------------------------------------------
          151  +# Test OOM injection in a query with two MATCH expressions
          152  +#
          153  +reset_db
          154  +do_execsql_test 6.0 {
          155  +  CREATE VIRTUAL TABLE t1 USING fts5(a);
          156  +  INSERT INTO t1 VALUES('a b c d');  -- 1
          157  +  INSERT INTO t1 VALUES('d a b c');  -- 2
          158  +  INSERT INTO t1 VALUES('c d a b');  -- 3
          159  +  INSERT INTO t1 VALUES('b c d a');  -- 4
          160  +}
          161  +do_faultsim_test 6.1 -faults oom* -body {
          162  +  execsql { SELECT rowid FROM t1 WHERE t1 MATCH 'a' AND t1 MATCH 'b' }
          163  +} -test {
          164  +  faultsim_test_result {0 {1 2 3 4}}
          165  +}
          166  +do_faultsim_test 6.2 -faults oom* -body {
          167  +  execsql { SELECT rowid FROM t1 WHERE t1 MATCH 'a OR b' AND t1 MATCH 'c OR d' }
          168  +} -test {
          169  +  faultsim_test_result {0 {1 2 3 4}}
          170  +}
   149    171   
   150    172   
   151    173   finish_test

Added ext/fts5/test/fts5misc.test.

            1  +# 2019 September 02
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#*************************************************************************
           11  +# This file implements regression tests for SQLite library.  The
           12  +# focus of this script is testing the FTS5 module.
           13  +#
           14  +
           15  +source [file join [file dirname [info script]] fts5_common.tcl]
           16  +set testprefix fts5misc
           17  +
           18  +# If SQLITE_ENABLE_FTS5 is not defined, omit this file.
           19  +ifcapable !fts5 {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +do_execsql_test 1.0 {
           25  +  CREATE VIRTUAL TABLE t1 USING fts5(a);
           26  +}
           27  +
           28  +do_catchsql_test 1.1.1 { 
           29  +  SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*'); 
           30  +} {1 {unknown special query: }}
           31  +do_catchsql_test 1.1.2 {
           32  +  SELECT a FROM t1
           33  +    WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*'));
           34  +} {1 {unknown special query: }}
           35  +
           36  +do_catchsql_test 1.2.1 { 
           37  +  SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*id'); 
           38  +} {0 {{}}}
           39  +
           40  +do_catchsql_test 1.2.2 {
           41  +  SELECT a FROM t1
           42  +    WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*id'));
           43  +} {0 {}}
           44  +
           45  +do_catchsql_test 1.3.1 { 
           46  +  SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*reads'); 
           47  +} {1 {no such cursor: 1}}
           48  +
           49  +do_catchsql_test 1.3.2 {
           50  +  SELECT a FROM t1
           51  +    WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*reads'));
           52  +} {1 {no such cursor: 1}}
           53  +
           54  +db close
           55  +sqlite3 db test.db
           56  +
           57  +do_catchsql_test 1.3.3 {
           58  +  SELECT a FROM t1
           59  +    WHERE rank = (SELECT highlight(t1, 4, '<b>', '</b>') FROM t1('*reads'));
           60  +} {1 {no such cursor: 1}}
           61  +
           62  +finish_test
           63  +

Added ext/fts5/test/fts5multi.test.

            1  +# 2014 September 13
            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 script is testing the FTS5 module.
           13  +#
           14  +
           15  +source [file join [file dirname [info script]] fts5_common.tcl]
           16  +set testprefix fts5multi
           17  +
           18  +# If SQLITE_ENABLE_FTS5 is not defined, omit this file.
           19  +ifcapable !fts5 {
           20  +  finish_test
           21  +  return
           22  +}
           23  +
           24  +fts5_aux_test_functions db
           25  +
           26  +do_execsql_test 1.0 {
           27  +  CREATE VIRTUAL TABLE t1 USING fts5(a, b, c);
           28  +  INSERT INTO t1 VALUES('gg bb bb'   ,'gg ff gg'   ,'ii ii');
           29  +  INSERT INTO t1 VALUES('dd dd hh kk','jj'         ,'aa');
           30  +  INSERT INTO t1 VALUES('kk gg ee'   ,'hh cc'      ,'hh jj aa cc');
           31  +  INSERT INTO t1 VALUES('hh'         ,'bb jj cc'   ,'kk ii');
           32  +  INSERT INTO t1 VALUES('kk dd kk ii','aa ee aa'   ,'ee');
           33  +  INSERT INTO t1 VALUES('ee'         ,'ff gg kk aa','ee ff ee');
           34  +  INSERT INTO t1 VALUES('ff jj'      ,'gg ee'      ,'kk ee gg kk');
           35  +  INSERT INTO t1 VALUES('ff ee dd hh','kk ee'      ,'gg dd');
           36  +  INSERT INTO t1 VALUES('bb'         ,'aa'         ,'bb aa');
           37  +  INSERT INTO t1 VALUES('hh cc bb'   ,'ff bb'      ,'cc');
           38  +  INSERT INTO t1 VALUES('jj'         ,'ff dd bb aa','dd dd ff ff');
           39  +  INSERT INTO t1 VALUES('ff dd gg dd','gg aa bb ff','cc');
           40  +  INSERT INTO t1 VALUES('ff aa cc jj','kk'         ,'ii dd');
           41  +  INSERT INTO t1 VALUES('jj dd'      ,'cc'         ,'ii hh ee aa');
           42  +  INSERT INTO t1 VALUES('ff ii hh'   ,'dd'         ,'gg');
           43  +  INSERT INTO t1 VALUES('ff dd gg hh','hh'         ,'ff dd');
           44  +  INSERT INTO t1 VALUES('cc cc'      ,'ff dd ff'   ,'bb');
           45  +  INSERT INTO t1 VALUES('ii'         ,'bb ii'      ,'jj kk');
           46  +  INSERT INTO t1 VALUES('ff hh'      ,'hh bb'      ,'bb dd ee');
           47  +  INSERT INTO t1 VALUES('jj kk'      ,'jj'         ,'gg ff cc');
           48  +  INSERT INTO t1 VALUES('dd kk'      ,'ii gg'      ,'dd');
           49  +  INSERT INTO t1 VALUES('cc'         ,'aa ff'      ,'ii');
           50  +  INSERT INTO t1 VALUES('bb ff bb ii','bb kk bb aa','hh ff ii dd');
           51  +  INSERT INTO t1 VALUES('aa'         ,'ee bb jj jj','dd');
           52  +  INSERT INTO t1 VALUES('kk dd cc'   ,'aa jj'      ,'ee aa ff');
           53  +  INSERT INTO t1 VALUES('aa gg aa'   ,'jj'         ,'ii kk hh gg');
           54  +  INSERT INTO t1 VALUES('ff hh aa'   ,'jj ii'      ,'hh dd bb jj');
           55  +  INSERT INTO t1 VALUES('hh'         ,'aa gg kk'   ,'bb ee');
           56  +  INSERT INTO t1 VALUES('bb'         ,'ee'         ,'gg');
           57  +  INSERT INTO t1 VALUES('dd kk'      ,'kk bb aa'   ,'ee');
           58  +}
           59  +
           60  +foreach {tn c1 e1 c2 e2} {
           61  +  1     t1 aa     t1 bb
           62  +  2     a  aa     b  bb
           63  +  3     a  "aa OR bb OR cc"    b  "jj OR ii OR hh"
           64  +  4     t1  "aa AND bb"       t1  "cc"
           65  +  5     c   "kk"               b  "aa OR bb OR cc OR dd OR ee"
           66  +} {
           67  +  if {$c1=="t1"} {
           68  +    set lhs "( $e1 )"
           69  +  } else {
           70  +    set lhs "$c1 : ( $e1 )"
           71  +  }
           72  +  if {$c2=="t1"} {
           73  +    set rhs "( $e2 )"
           74  +  } else {
           75  +    set rhs "$c2 : ( $e2 )"
           76  +  }
           77  +
           78  +  set q1 "t1 MATCH '($lhs) AND ($rhs)'"
           79  +  set q2 "$c1 MATCH '$e1' AND $c2 MATCH '$e2'"
           80  +
           81  +  set ret [execsql "SELECT rowid FROM t1 WHERE $q1"]
           82  +  set N [llength $ret]
           83  +  do_execsql_test 1.$tn.1.($N) "SELECT rowid FROM t1 WHERE $q2" $ret
           84  +
           85  +  set ret [execsql "SELECT fts5_test_poslist(t1) FROM t1 WHERE $q1"]
           86  +  do_execsql_test 1.$tn.2.($N) "
           87  +    SELECT fts5_test_poslist(t1) FROM t1 WHERE $q2
           88  +  " $ret
           89  +}
           90  +
           91  +do_catchsql_test 2.1.1 {
           92  +  SELECT rowid FROM t1 WHERE t1 MATCH '(NOT' AND t1 MATCH 'aa bb';
           93  +} {1 {fts5: syntax error near "NOT"}}
           94  +do_catchsql_test 2.1.2 {
           95  +  SELECT rowid FROM t1 WHERE t1 MATCH 'aa bb' AND t1 MATCH '(NOT';
           96  +} {1 {fts5: syntax error near "NOT"}}
           97  +
           98  +finish_test
           99  +

Changes to ext/fts5/test/fts5plan.test.

    27     27   }
    28     28   
    29     29   do_eqp_test 1.1 {
    30     30     SELECT * FROM t1, f1 WHERE f1 MATCH t1.x
    31     31   } {
    32     32     QUERY PLAN
    33     33     |--SCAN TABLE t1
    34         -  `--SCAN TABLE f1 VIRTUAL TABLE INDEX 65537:
           34  +  `--SCAN TABLE f1 VIRTUAL TABLE INDEX 0:m
    35     35   }
    36     36   
    37     37   do_eqp_test 1.2 {
    38     38     SELECT * FROM t1, f1 WHERE f1 > t1.x
    39     39   } {
    40     40     QUERY PLAN
    41     41     |--SCAN TABLE f1 VIRTUAL TABLE INDEX 0:
................................................................................
    42     42     `--SCAN TABLE t1
    43     43   }
    44     44   
    45     45   do_eqp_test 1.3 {
    46     46     SELECT * FROM f1 WHERE f1 MATCH ? ORDER BY ff
    47     47   } {
    48     48     QUERY PLAN
    49         -  |--SCAN TABLE f1 VIRTUAL TABLE INDEX 65537:
           49  +  |--SCAN TABLE f1 VIRTUAL TABLE INDEX 0:m
    50     50     `--USE TEMP B-TREE FOR ORDER BY
    51     51   }
    52     52   
    53     53   do_eqp_test 1.4 {
    54     54     SELECT * FROM f1 ORDER BY rank
    55     55   } {
    56     56     QUERY PLAN
    57     57     |--SCAN TABLE f1 VIRTUAL TABLE INDEX 0:
    58     58     `--USE TEMP B-TREE FOR ORDER BY
    59     59   }
    60     60   
    61     61   do_eqp_test 1.5 {
    62     62     SELECT * FROM f1 WHERE rank MATCH ?
    63         -} {SCAN TABLE f1 VIRTUAL TABLE INDEX 2:}
           63  +} {SCAN TABLE f1 VIRTUAL TABLE INDEX 0:r}
    64     64   
    65     65   finish_test

Changes to ext/fts5/test/fts5rank.test.

   157    157     )
   158    158     INSERT INTO ttt SELECT 'word ' || i FROM s;
   159    159   }
   160    160   
   161    161   do_execsql_test 5.1 {
   162    162     SELECT rowid FROM ttt('word') WHERE rowid BETWEEN 30 AND 40 ORDER BY rank;
   163    163   } {30 31 32 33 34 35 36 37 38 39 40}
          164  +
          165  +#-------------------------------------------------------------------------
          166  +reset_db
          167  +do_execsql_test 6.0 {
          168  +  CREATE VIRTUAL TABLE "My.Table" USING fts5(Text);
          169  +
          170  +  INSERT INTO "My.Table" VALUES ('hello this is a test');
          171  +  INSERT INTO "My.Table" VALUES ('of trying to order by');
          172  +  INSERT INTO "My.Table" VALUES ('rank on an fts5 table');
          173  +  INSERT INTO "My.Table" VALUES ('that have periods in');
          174  +  INSERT INTO "My.Table" VALUES ('the table names.');
          175  +  INSERT INTO "My.Table" VALUES ('table table table');
          176  +}
          177  +do_execsql_test 6.1 {
          178  +  SELECT * FROM "My.Table" WHERE Text MATCH 'table' ORDER BY rank;
          179  +} {
          180  +  {table table table} {the table names.} {rank on an fts5 table}
          181  +}
   164    182   
   165    183   finish_test

Changes to ext/fts5/test/fts5simple.test.

   463    463   } {11111 11112}
   464    464   do_execsql_test 21.3 {
   465    465     DELETE FROM x1 WHERE rowid=11111;
   466    466     INSERT INTO x1(x1) VALUES('integrity-check');
   467    467     SELECT rowid FROM x1($doc);
   468    468   } {11112}
   469    469   
          470  +#-------------------------------------------------------------------------
          471  +reset_db
          472  +do_execsql_test 22.0 {
          473  +  CREATE VIRTUAL TABLE x1 USING fts5(x);
          474  +  INSERT INTO x1(x) VALUES('a b c');
          475  +  INSERT INTO x1(x) VALUES('x y z');
          476  +  INSERT INTO x1(x) VALUES('c b a');
          477  +  INSERT INTO x1(x) VALUES('z y x');
          478  +}
          479  +
          480  +do_catchsql_test 22.1 {SELECT * FROM x1('')}   {1 {fts5: syntax error near ""}}
          481  +do_catchsql_test 22.2 {SELECT * FROM x1(NULL)} {1 {fts5: syntax error near ""}}
          482  +
   470    483   finish_test

Changes to ext/lsm1/Makefile.

    39     39                $(LSMDIR)/lsm-test/lsmtest_main.c $(LSMDIR)/lsm-test/lsmtest_mem.c \
    40     40                $(LSMDIR)/lsm-test/lsmtest_tdb.c $(LSMDIR)/lsm-test/lsmtest_tdb3.c \
    41     41                $(LSMDIR)/lsm-test/lsmtest_util.c $(LSMDIR)/lsm-test/lsmtest_win32.c
    42     42   
    43     43   
    44     44   # all: lsm.so
    45     45   
    46         -LSMOPTS += -DLSM_MUTEX_PTHREADS=1 -I$(LSMDIR) -DHAVE_ZLIB
           46  +LSMOPTS += -fPIC -DLSM_MUTEX_PTHREADS=1 -I$(LSMDIR) -DHAVE_ZLIB
    47     47   
    48     48   lsm.so:	$(LSMOBJ)
    49         -	$(TCCX) -shared -o lsm.so $(LSMOBJ)
           49  +	$(TCCX) -shared -fPIC -o lsm.so $(LSMOBJ)
    50     50   
    51     51   %.o:	$(LSMDIR)/%.c $(LSMHDR) sqlite3.h
    52     52   	$(TCCX) $(LSMOPTS) -c $<
    53     53   	
    54     54   lsmtest$(EXE): $(LSMOBJ) $(LSMTESTSRC) $(LSMTESTHDR) sqlite3.o
    55     55   	# $(TCPPX) -c $(TOP)/lsm-test/lsmtest_tdb2.cc
    56     56   	$(TCCX) $(LSMOPTS) $(LSMTESTSRC) $(LSMOBJ) sqlite3.o -o lsmtest$(EXE) $(THREADLIB) -lz

Changes to ext/lsm1/lsm_vtab.c.

   838    838     int argIdx = -1;       /* Index of the key== constraint, or -1 if none */
   839    839     int iIdx2 = -1;        /* The index of the second key */
   840    840     int omit1 = 0;
   841    841     int omit2 = 0;
   842    842   
   843    843     const struct sqlite3_index_constraint *pConstraint;
   844    844     pConstraint = pIdxInfo->aConstraint;
   845         -  for(i=0; i<pIdxInfo->nConstraint && idxNum<16; i++, pConstraint++){
          845  +  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
   846    846       if( pConstraint->usable==0 ) continue;
   847    847       if( pConstraint->iColumn!=0 ) continue;
   848    848       switch( pConstraint->op ){
   849    849         case SQLITE_INDEX_CONSTRAINT_EQ: {
   850    850           if( idxNum>0 ){
   851    851             argIdx = i;
   852    852             iIdx2 = -1;

Changes to ext/lsm1/test/lsm1_simple.test.

    84     84     INSERT INTO x1(a,b,c,d) VALUES(15, 11, 22, 33),(8,'banjo',x'333231',NULL),
    85     85         (12,NULL,3.25,-559281390);
    86     86     SELECT quote(a), quote(b), quote(c), quote(d), '|' FROM x1;
    87     87   } {'12' NULL 3.25 -559281390 | '15' 11 22 33 | '8' 'banjo' X'333231' NULL |}
    88     88   do_execsql_test 211 {
    89     89     SELECT quote(a), quote(lsm1_key), quote(lsm1_value), '|' FROM x1;
    90     90   } {'12' X'3132' X'05320000000000000A401FFB42ABE9DB' | '15' X'3135' X'4284C6' | '8' X'38' X'2162616E6A6F1633323105' |}
           91  +do_execsql_test 212 {
           92  +  SELECT quote(a), quote(lsm1_key), quote(lsm1_value) FROM x1 WHERE a='12';
           93  +} {'12' X'3132' X'05320000000000000A401FFB42ABE9DB'}
           94  +
           95  +#-------------------------------------------------------------------------
           96  +reset_db
           97  +forcedelete testlsm.db
           98  +load_lsm1_vtab db
           99  +do_execsql_test 300 {
          100  +  CREATE VIRTUAL TABLE x1 USING lsm1(testlsm.db,a,TEXT,b,c,d);
          101  +}
          102  +do_eqp_test 310 {
          103  +  SELECT * FROM x1 WHERE a=?
          104  +} {SCAN TABLE x1 VIRTUAL TABLE INDEX 0:}
          105  +
          106  +do_eqp_test 320 {
          107  +  SELECT * FROM x1 WHERE a>?
          108  +} {SCAN TABLE x1 VIRTUAL TABLE INDEX 2:}
          109  +
          110  +do_eqp_test 330 {
          111  +  SELECT * FROM x1 WHERE a<?
          112  +} {SCAN TABLE x1 VIRTUAL TABLE INDEX 3:}
          113  +do_eqp_test 340 {
          114  +  SELECT * FROM x1 WHERE a BETWEEN ? AND ?
          115  +} {SCAN TABLE x1 VIRTUAL TABLE INDEX 1:}
          116  +
          117  +#-------------------------------------------------------------------------
          118  +reset_db
          119  +forcedelete testlsm.db
          120  +load_lsm1_vtab db
          121  +do_execsql_test 400 {
          122  +  CREATE VIRTUAL TABLE x1 USING lsm1(testlsm.db,a,TEXT,b);
          123  +  INSERT INTO x1 VALUES('one', 1);
          124  +  INSERT INTO x1 VALUES('two', 2);
          125  +  INSERT INTO x1 VALUES('three', 3);
          126  +  INSERT INTO x1 VALUES('four', 4);
          127  +  INSERT INTO x1 VALUES('five', 5);
          128  +}
          129  +do_execsql_test 410 {
          130  +  SELECT b FROM x1 WHERE a = 'two'
          131  +} {2}
          132  +do_execsql_test 411 {
          133  +  SELECT b FROM x1 WHERE a = 'one'
          134  +} {1}
          135  +do_execsql_test 412 {
          136  +  SELECT b FROM x1 WHERE a = 'five'
          137  +} {5}
    91    138   
          139  +do_execsql_test 420 {
          140  +  SELECT b FROM x1 WHERE a BETWEEN 'one' AND 'three';
          141  +} {1 3}
          142  +do_execsql_test 421 {
          143  +  SELECT b FROM x1 WHERE a BETWEEN 'five' AND 'two';
          144  +} {5 4 1 3 2}
          145  +do_execsql_test 421 {
          146  +  SELECT b FROM x1 WHERE a > 'five';
          147  +} {4 1 3 2}
          148  +do_execsql_test 421 {
          149  +  SELECT b FROM x1 WHERE a <= 'three';
          150  +} {3 1 4 5}
    92    151   
    93    152   finish_test

Changes to ext/misc/carray.c.

    20     20   ** at the address $ptr.  $ptr is a pointer to the array of integers.
    21     21   ** The pointer value must be assigned to $ptr using the
    22     22   ** sqlite3_bind_pointer() interface with a pointer type of "carray".
    23     23   ** For example:
    24     24   **
    25     25   **    static int aX[] = { 53, 9, 17, 2231, 4, 99 };
    26     26   **    int i = sqlite3_bind_parameter_index(pStmt, "$ptr");
    27         -**    sqlite3_bind_value(pStmt, i, aX, "carray", 0);
           27  +**    sqlite3_bind_pointer(pStmt, i, aX, "carray", 0);
    28     28   **
    29     29   ** There is an optional third parameter to determine the datatype of
    30     30   ** the C-language array.  Allowed values of the third parameter are
    31     31   ** 'int32', 'int64', 'double', 'char*'.  Example:
    32     32   **
    33     33   **      SELECT * FROM carray($ptr,10,'char*');
    34     34   **

Changes to ext/misc/fossildelta.c.

    32     32   */
    33     33   #include <string.h>
    34     34   #include <assert.h>
    35     35   #include <stdlib.h>
    36     36   #include "sqlite3ext.h"
    37     37   SQLITE_EXTENSION_INIT1
    38     38   
           39  +#ifndef SQLITE_AMALGAMATION
    39     40   /*
    40     41   ** The "u32" type must be an unsigned 32-bit integer.  Adjust this
    41     42   */
    42     43   typedef unsigned int u32;
    43     44   
    44     45   /*
    45     46   ** Must be a 16-bit value
    46     47   */
    47     48   typedef short int s16;
    48     49   typedef unsigned short int u16;
           50  +
           51  +#endif /* SQLITE_AMALGAMATION */
    49     52   
    50     53   
    51     54   /*
    52     55   ** The width of a hash window in bytes.  The algorithm only works if this
    53     56   ** is a power of 2.
    54     57   */
    55     58   #define NHASH 16

Changes to ext/misc/json1.c.

  1079   1079     int *pApnd,             /* Append nodes to complete path if not NULL */
  1080   1080     const char **pzErr      /* Make *pzErr point to any syntax error in zPath */
  1081   1081   ){
  1082   1082     u32 i, j, nKey;
  1083   1083     const char *zKey;
  1084   1084     JsonNode *pRoot = &pParse->aNode[iRoot];
  1085   1085     if( zPath[0]==0 ) return pRoot;
         1086  +  if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
  1086   1087     if( zPath[0]=='.' ){
  1087   1088       if( pRoot->eType!=JSON_OBJECT ) return 0;
  1088   1089       zPath++;
  1089   1090       if( zPath[0]=='"' ){
  1090   1091         zKey = zPath + 1;
  1091   1092         for(i=1; zPath[i] && zPath[i]!='"'; i++){}
  1092   1093         nKey = i-1;
................................................................................
  1815   1816     JsonString *pStr;
  1816   1817     UNUSED_PARAM(argc);
  1817   1818     pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
  1818   1819     if( pStr ){
  1819   1820       if( pStr->zBuf==0 ){
  1820   1821         jsonInit(pStr, ctx);
  1821   1822         jsonAppendChar(pStr, '[');
  1822         -    }else{
         1823  +    }else if( pStr->nUsed>1 ){
  1823   1824         jsonAppendChar(pStr, ',');
  1824   1825         pStr->pCtx = ctx;
  1825   1826       }
  1826   1827       jsonAppendValue(pStr, argv[0]);
  1827   1828     }
  1828   1829   }
  1829   1830   static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
................................................................................
  1865   1866   static void jsonGroupInverse(
  1866   1867     sqlite3_context *ctx,
  1867   1868     int argc,
  1868   1869     sqlite3_value **argv
  1869   1870   ){
  1870   1871     int i;
  1871   1872     int inStr = 0;
         1873  +  int nNest = 0;
  1872   1874     char *z;
         1875  +  char c;
  1873   1876     JsonString *pStr;
  1874   1877     UNUSED_PARAM(argc);
  1875   1878     UNUSED_PARAM(argv);
  1876   1879     pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
  1877   1880   #ifdef NEVER
  1878   1881     /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
  1879   1882     ** always have been called to initalize it */
  1880   1883     if( NEVER(!pStr) ) return;
  1881   1884   #endif
  1882   1885     z = pStr->zBuf;
  1883         -  for(i=1; z[i]!=',' || inStr; i++){
  1884         -    assert( i<pStr->nUsed );
  1885         -    if( z[i]=='"' ){
         1886  +  for(i=1; (c = z[i])!=',' || inStr || nNest; i++){
         1887  +    if( i>=pStr->nUsed ){
         1888  +      pStr->nUsed = 1;
         1889  +      return;
         1890  +    }
         1891  +    if( c=='"' ){
  1886   1892         inStr = !inStr;
  1887         -    }else if( z[i]=='\\' ){
         1893  +    }else if( c=='\\' ){
  1888   1894         i++;
         1895  +    }else if( !inStr ){
         1896  +      if( c=='{' || c=='[' ) nNest++;
         1897  +      if( c=='}' || c==']' ) nNest--;
  1889   1898       }
  1890   1899     }
  1891   1900     pStr->nUsed -= i;      
  1892   1901     memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
  1893   1902   }
  1894   1903   #else
  1895   1904   # define jsonGroupInverse 0
................................................................................
  1911   1920     u32 n;
  1912   1921     UNUSED_PARAM(argc);
  1913   1922     pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
  1914   1923     if( pStr ){
  1915   1924       if( pStr->zBuf==0 ){
  1916   1925         jsonInit(pStr, ctx);
  1917   1926         jsonAppendChar(pStr, '{');
  1918         -    }else{
         1927  +    }else if( pStr->nUsed>1 ){
  1919   1928         jsonAppendChar(pStr, ',');
  1920   1929         pStr->pCtx = ctx;
  1921   1930       }
  1922   1931       z = (const char*)sqlite3_value_text(argv[0]);
  1923   1932       n = (u32)sqlite3_value_bytes(argv[0]);
  1924   1933       jsonAppendString(pStr, z, n);
  1925   1934       jsonAppendChar(pStr, ':');
................................................................................
  2499   2508     } aMod[] = {
  2500   2509       { "json_each",            &jsonEachModule               },
  2501   2510       { "json_tree",            &jsonTreeModule               },
  2502   2511     };
  2503   2512   #endif
  2504   2513     for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
  2505   2514       rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
  2506         -                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 
         2515  +                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
  2507   2516                                    (void*)&aFunc[i].flag,
  2508   2517                                    aFunc[i].xFunc, 0, 0);
  2509   2518     }
  2510   2519   #ifndef SQLITE_OMIT_WINDOWFUNC
  2511   2520     for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
  2512   2521       rc = sqlite3_create_window_function(db, aAgg[i].zName, aAgg[i].nArg,
  2513         -                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
         2522  +                SQLITE_SUBTYPE | SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
  2514   2523                                    aAgg[i].xStep, aAgg[i].xFinal,
  2515   2524                                    aAgg[i].xValue, jsonGroupInverse, 0);
  2516   2525     }
  2517   2526   #endif
  2518   2527   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2519   2528     for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
  2520   2529       rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);

Changes to ext/misc/regexp.c.

   606    606     return 0;
   607    607   }
   608    608   
   609    609   /* Free and reclaim all the memory used by a previously compiled
   610    610   ** regular expression.  Applications should invoke this routine once
   611    611   ** for every call to re_compile() to avoid memory leaks.
   612    612   */
   613         -void re_free(ReCompiled *pRe){
          613  +static void re_free(ReCompiled *pRe){
   614    614     if( pRe ){
   615    615       sqlite3_free(pRe->aOp);
   616    616       sqlite3_free(pRe->aArg);
   617    617       sqlite3_free(pRe);
   618    618     }
   619    619   }
   620    620   
   621    621   /*
   622    622   ** Compile a textual regular expression in zIn[] into a compiled regular
   623    623   ** expression suitable for us by re_match() and return a pointer to the
   624    624   ** compiled regular expression in *ppRe.  Return NULL on success or an
   625    625   ** error message if something goes wrong.
   626    626   */
   627         -const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
          627  +static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
   628    628     ReCompiled *pRe;
   629    629     const char *zErr;
   630    630     int i, j;
   631    631   
   632    632     *ppRe = 0;
   633    633     pRe = sqlite3_malloc( sizeof(*pRe) );
   634    634     if( pRe==0 ){

Added ext/rbu/rbuexpr.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 rbuexpr
           15  +
           16  +db close
           17  +sqlite3_shutdown
           18  +sqlite3_config_uri 1
           19  +
           20  +sqlite3 db test.db
           21  +
           22  +do_execsql_test 1.0 {
           23  +  CREATE TABLE t1(a, b, c PRIMARY KEY);
           24  +  CREATE INDEX i1 ON t1(a, null, b+1);
           25  +  CREATE INDEX i2 ON t1(a+1, b+1, c+1);
           26  +
           27  +  INSERT INTO t1 VALUES(1, 2, 3);
           28  +  INSERT INTO t1 VALUES(4, 5, 6);
           29  +  INSERT INTO t1 VALUES(7, 8, 9);
           30  +  INSERT INTO t1 VALUES(10, 11, 12);
           31  +
           32  +  PRAGMA integrity_check;
           33  +} {ok}
           34  +
           35  +forcedelete rbu.db
           36  +sqlite3 db2 rbu.db
           37  +do_execsql_test -db db2 1.1 {
           38  +  CREATE TABLE data_t1(a, b, c, rbu_control);
           39  +  INSERT INTO data_t1 VALUES(13, 14, 15, 0);
           40  +  INSERT INTO data_t1 VALUES(NULL, NULL, 6, 1);
           41  +  INSERT INTO data_t1 VALUES(NULL, 'three', 3, '.x.');
           42  +}
           43  +db2 close
           44  +db close
           45  +
           46  +do_test 1.2 {
           47  +  run_rbu test.db rbu.db
           48  +} {SQLITE_DONE}
           49  +
           50  +sqlite3 db test.db
           51  +
           52  +do_execsql_test 1.3 {
           53  +  SELECT * FROM t1 WHERE a=4;
           54  +}
           55  +
           56  +integrity_check 1.4
           57  +
           58  +#-------------------------------------------------------------------------
           59  +#
           60  +reset_db
           61  +do_execsql_test 2.0 {
           62  +  CREATE TABLE t1(c1, c2, c3, i INTEGER PRIMARY KEY);
           63  +  INSERT INTO t1 VALUES('one', 'one', 'one', 1);
           64  +  INSERT INTO t1 VALUES('two', 'two', 'two', 2);
           65  +  INSERT INTO t1 VALUES('three', 'three', 'three', 3);
           66  +  INSERT INTO t1 VALUES('four', 'four', 'four', 4);
           67  +
           68  +  CREATE INDEX i1 ON t1( substr(c1, 1, 2) );
           69  +  CREATE INDEX i2 ON t1( c1 || c2 || c3 );
           70  +  CREATE INDEX i3 ON t1( length(c1) + length(c2) - 1, c3||i );
           71  +}
           72  +
           73  +forcedelete rbu.db
           74  +sqlite3 db2 rbu.db
           75  +do_execsql_test -db db2 2.1 {
           76  +  CREATE TABLE data_t1(c1, c2, c3, i, rbu_control);
           77  +  INSERT INTO data_t1 VALUES(NULL, NULL, NULL, 2, 1);
           78  +  INSERT INTO data_t1 VALUES('thirty', NULL, NULL, 3, 'xx..');
           79  +  INSERT INTO data_t1 VALUES('five', 'five', 'five', 5, 0);
           80  +}
           81  +db2 close
           82  +
           83  +db close
           84  +
           85  +do_test 2.2 {
           86  +  run_rbu test.db rbu.db
           87  +} {SQLITE_DONE}
           88  +
           89  +sqlite3 db test.db
           90  +integrity_check 2.3
           91  +
           92  +finish_test
           93  +

Changes to ext/rbu/rbupartial.test.

    36     36       CREATE INDEX i1b3 ON t1(%B%) WHERE %C%>=5;
    37     37   
    38     38       CREATE INDEX i1c  ON t1(%C%);
    39     39       CREATE INDEX i1c2 ON t1(%C%) WHERE %C% IS NULL;
    40     40       CREATE INDEX i1c3 ON t1(%C%) WHERE %C% IS NOT NULL;
    41     41   
    42     42       CREATE INDEX i1c4 ON t1(%C%) WHERE %D% < 'd';
           43  +    CREATE INDEX i1c5 ON t1(
           44  +        %C%                   -- for (c = ... expressions
           45  +    ) WHERE %D% < 'd';
           46  +    CREATE INDEX i1c6 ON t1(
           47  +        %C% /* Again, for (c=... expr */, %D%
           48  +    ) WHERE %D% < 'd';
           49  +
           50  +    CREATE INDEX i1c7 ON t1(
           51  +        %C% /* As before, for (c=... "expr */) WHERE %D% < 'd';
    43     52     }
    44     53   
    45     54     do_execsql_test $tn.1.1 {
    46     55       INSERT INTO t1 VALUES(0, NULL, NULL, 'a');
    47     56       INSERT INTO t1 VALUES(1, 2, 3, 'b');
    48     57       INSERT INTO t1 VALUES(4, 5, 6, 'c');
    49     58       INSERT INTO t1 VALUES(7, 8, 9, 'd');

Changes to ext/rbu/rbuprogress.test.

   409    409   
   410    410         set R(nopk) $r1
   411    411         set R(vtab) $r2
   412    412         do_sp_test 5.$tn.$bReopen.$tn2.1 $bReopen test.db rbu.db $R($tn)
   413    413       }
   414    414     }
   415    415   }
          416  +
          417  +#-------------------------------------------------------------------------
          418  +# Test that sqlite3_bp_progress() works with an RBU vacuum if there
          419  +# is an rbu_count table in the db being vacuumed.
          420  +#
          421  +reset_db
          422  +do_execsql_test 6.0 {
          423  +  CREATE TABLE t1(a, b, c);
          424  +  CREATE INDEX i1 ON t1(a);
          425  +  CREATE INDEX i2 ON t1(b);
          426  +  WITH s(i) AS (
          427  +    SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
          428  +  )
          429  +  INSERT INTO t1 SELECT i, i, i FROM s;
          430  +  CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
          431  +  INSERT INTO rbu_count VALUES('t1', (SELECT count(*) FROM t1));
          432  +  INSERT INTO rbu_count VALUES('rbu_count', 2);
          433  +}
          434  +
          435  +forcedelete state.db
          436  +do_test 6.1 {
          437  +  set maxA 0
          438  +  set maxB 0
          439  +  sqlite3rbu_vacuum rbu test.db state.db
          440  +  while {[rbu step]=="SQLITE_OK"} {
          441  +    foreach {a b} [rbu bp_progress] {
          442  +      if {$a > $maxA} { set maxA $a }
          443  +      if {$b > $maxB} { set maxB $b }
          444  +    }
          445  +  }
          446  +  list [rbu close] $maxA $maxB
          447  +} {SQLITE_DONE 10000 10000}
   416    448   
   417    449   
   418    450   finish_test

Changes to ext/rbu/sqlite3rbu.c.

   178    178   
   179    179   #define RBU_CREATE_STATE \
   180    180     "CREATE TABLE IF NOT EXISTS %s.rbu_state(k INTEGER PRIMARY KEY, v)"
   181    181   
   182    182   typedef struct RbuFrame RbuFrame;
   183    183   typedef struct RbuObjIter RbuObjIter;
   184    184   typedef struct RbuState RbuState;
          185  +typedef struct RbuSpan RbuSpan;
   185    186   typedef struct rbu_vfs rbu_vfs;
   186    187   typedef struct rbu_file rbu_file;
   187    188   typedef struct RbuUpdateStmt RbuUpdateStmt;
   188    189   
   189    190   #if !defined(SQLITE_AMALGAMATION)
   190    191   typedef unsigned int u32;
   191    192   typedef unsigned short u16;
................................................................................
   221    222   };
   222    223   
   223    224   struct RbuUpdateStmt {
   224    225     char *zMask;                    /* Copy of update mask used with pUpdate */
   225    226     sqlite3_stmt *pUpdate;          /* Last update statement (or NULL) */
   226    227     RbuUpdateStmt *pNext;
   227    228   };
          229  +
          230  +struct RbuSpan {
          231  +  const char *zSpan;
          232  +  int nSpan;
          233  +};
   228    234   
   229    235   /*
   230    236   ** An iterator of this type is used to iterate through all objects in
   231    237   ** the target database that require updating. For each such table, the
   232    238   ** iterator visits, in order:
   233    239   **
   234    240   **     * the table itself, 
................................................................................
   271    277   
   272    278     /* Statements created by rbuObjIterPrepareAll() */
   273    279     int nCol;                       /* Number of columns in current object */
   274    280     sqlite3_stmt *pSelect;          /* Source data */
   275    281     sqlite3_stmt *pInsert;          /* Statement for INSERT operations */
   276    282     sqlite3_stmt *pDelete;          /* Statement for DELETE ops */
   277    283     sqlite3_stmt *pTmpInsert;       /* Insert into rbu_tmp_$zDataTbl */
          284  +  int nIdxCol;
          285  +  RbuSpan *aIdxCol;
          286  +  char *zIdxSql;
   278    287   
   279    288     /* Last UPDATE used (for PK b-tree updates only), or NULL. */
   280    289     RbuUpdateStmt *pRbuUpdate;
   281    290   };
   282    291   
   283    292   /*
   284    293   ** Values for RbuObjIter.eType
................................................................................
   805    814     pUp = pIter->pRbuUpdate;
   806    815     while( pUp ){
   807    816       RbuUpdateStmt *pTmp = pUp->pNext;
   808    817       sqlite3_finalize(pUp->pUpdate);
   809    818       sqlite3_free(pUp);
   810    819       pUp = pTmp;
   811    820     }
          821  +  sqlite3_free(pIter->aIdxCol);
          822  +  sqlite3_free(pIter->zIdxSql);
   812    823     
   813    824     pIter->pSelect = 0;
   814    825     pIter->pInsert = 0;
   815    826     pIter->pDelete = 0;
   816    827     pIter->pRbuUpdate = 0;
   817    828     pIter->pTmpInsert = 0;
   818    829     pIter->nCol = 0;
          830  +  pIter->nIdxCol = 0;
          831  +  pIter->aIdxCol = 0;
          832  +  pIter->zIdxSql = 0;
   819    833   }
   820    834   
   821    835   /*
   822    836   ** Clean up any resources allocated as part of the iterator object passed
   823    837   ** as the only argument.
   824    838   */
   825    839   static void rbuObjIterFinalize(RbuObjIter *pIter){
................................................................................
   926    940     sqlite3rbu *p = sqlite3_user_data(pCtx);
   927    941     const char *zIn;
   928    942     assert( argc==1 || argc==2 );
   929    943   
   930    944     zIn = (const char*)sqlite3_value_text(argv[0]);
   931    945     if( zIn ){
   932    946       if( rbuIsVacuum(p) ){
   933         -      assert( argc==2 );
   934         -      if( 0==sqlite3_value_int(argv[1]) ){
          947  +      assert( argc==2 || argc==1 );
          948  +      if( argc==1 || 0==sqlite3_value_int(argv[1]) ){
   935    949           sqlite3_result_text(pCtx, zIn, -1, SQLITE_STATIC);
   936    950         }
   937    951       }else{
   938    952         if( strlen(zIn)>4 && memcmp("data", zIn, 4)==0 ){
   939    953           int i;
   940    954           for(i=4; zIn[i]>='0' && zIn[i]<='9'; i++);
   941    955           if( zIn[i]=='_' && zIn[i+1] ){
................................................................................
  1085   1099   ** If an OOM condition is encountered when attempting to allocate memory,
  1086   1100   ** output variable (*pRc) is set to SQLITE_NOMEM before returning. Otherwise,
  1087   1101   ** if the allocation succeeds, (*pRc) is left unchanged.
  1088   1102   */
  1089   1103   static char *rbuStrndup(const char *zStr, int *pRc){
  1090   1104     char *zRet = 0;
  1091   1105   
  1092         -  assert( *pRc==SQLITE_OK );
  1093         -  if( zStr ){
  1094         -    size_t nCopy = strlen(zStr) + 1;
  1095         -    zRet = (char*)sqlite3_malloc64(nCopy);
  1096         -    if( zRet ){
  1097         -      memcpy(zRet, zStr, nCopy);
  1098         -    }else{
  1099         -      *pRc = SQLITE_NOMEM;
         1106  +  if( *pRc==SQLITE_OK ){
         1107  +    if( zStr ){
         1108  +      size_t nCopy = strlen(zStr) + 1;
         1109  +      zRet = (char*)sqlite3_malloc64(nCopy);
         1110  +      if( zRet ){
         1111  +        memcpy(zRet, zStr, nCopy);
         1112  +      }else{
         1113  +        *pRc = SQLITE_NOMEM;
         1114  +      }
  1100   1115       }
  1101   1116     }
  1102   1117   
  1103   1118     return zRet;
  1104   1119   }
  1105   1120   
  1106   1121   /*
................................................................................
  1264   1279       }
  1265   1280       p->rc = prepareFreeAndCollectError(p->dbMain, &pXInfo, &p->zErrmsg,
  1266   1281           sqlite3_mprintf("PRAGMA main.index_xinfo = %Q", zIdx)
  1267   1282       );
  1268   1283       while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
  1269   1284         int iCid = sqlite3_column_int(pXInfo, 1);
  1270   1285         if( iCid>=0 ) pIter->abIndexed[iCid] = 1;
         1286  +      if( iCid==-2 ){
         1287  +        memset(pIter->abIndexed, 0x01, sizeof(u8)*pIter->nTblCol);
         1288  +      }
  1271   1289       }
  1272   1290       rbuFinalize(p, pXInfo);
  1273   1291       bIndex = 1;
  1274   1292       pIter->nIndex++;
  1275   1293     }
  1276   1294   
  1277   1295     if( pIter->eType==RBU_PK_WITHOUT_ROWID ){
................................................................................
  1675   1693       );
  1676   1694     }
  1677   1695   
  1678   1696     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXInfo) ){
  1679   1697       int iCid = sqlite3_column_int(pXInfo, 1);
  1680   1698       int bDesc = sqlite3_column_int(pXInfo, 3);
  1681   1699       const char *zCollate = (const char*)sqlite3_column_text(pXInfo, 4);
  1682         -    const char *zCol;
         1700  +    const char *zCol = 0;
  1683   1701       const char *zType;
  1684   1702   
  1685         -    if( iCid<0 ){
  1686         -      /* An integer primary key. If the table has an explicit IPK, use
  1687         -      ** its name. Otherwise, use "rbu_rowid".  */
  1688         -      if( pIter->eType==RBU_PK_IPK ){
  1689         -        int i;
  1690         -        for(i=0; pIter->abTblPk[i]==0; i++);
  1691         -        assert( i<pIter->nTblCol );
  1692         -        zCol = pIter->azTblCol[i];
  1693         -      }else if( rbuIsVacuum(p) ){
  1694         -        zCol = "_rowid_";
         1703  +    if( iCid==-2 ){
         1704  +      int iSeq = sqlite3_column_int(pXInfo, 0);
         1705  +      zRet = sqlite3_mprintf("%z%s(%.*s) COLLATE %Q", zRet, zCom,
         1706  +          pIter->aIdxCol[iSeq].nSpan, pIter->aIdxCol[iSeq].zSpan, zCollate
         1707  +      );
         1708  +      zType = "";
         1709  +    }else {
         1710  +      if( iCid<0 ){
         1711  +        /* An integer primary key. If the table has an explicit IPK, use
         1712  +        ** its name. Otherwise, use "rbu_rowid".  */
         1713  +        if( pIter->eType==RBU_PK_IPK ){
         1714  +          int i;
         1715  +          for(i=0; pIter->abTblPk[i]==0; i++);
         1716  +          assert( i<pIter->nTblCol );
         1717  +          zCol = pIter->azTblCol[i];
         1718  +        }else if( rbuIsVacuum(p) ){
         1719  +          zCol = "_rowid_";
         1720  +        }else{
         1721  +          zCol = "rbu_rowid";
         1722  +        }
         1723  +        zType = "INTEGER";
  1695   1724         }else{
  1696         -        zCol = "rbu_rowid";
         1725  +        zCol = pIter->azTblCol[iCid];
         1726  +        zType = pIter->azTblType[iCid];
  1697   1727         }
  1698         -      zType = "INTEGER";
  1699         -    }else{
  1700         -      zCol = pIter->azTblCol[iCid];
  1701         -      zType = pIter->azTblType[iCid];
         1728  +      zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom,zCol,zCollate);
  1702   1729       }
  1703   1730   
  1704         -    zRet = sqlite3_mprintf("%z%s\"%w\" COLLATE %Q", zRet, zCom, zCol, zCollate);
  1705   1731       if( pIter->bUnique==0 || sqlite3_column_int(pXInfo, 5) ){
  1706   1732         const char *zOrder = (bDesc ? " DESC" : "");
  1707   1733         zImpPK = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\"%s", 
  1708   1734             zImpPK, zCom, nBind, zCol, zOrder
  1709   1735         );
  1710   1736       }
  1711   1737       zImpCols = sqlite3_mprintf("%z%s\"rbu_imp_%d%w\" %s COLLATE %Q", 
................................................................................
  2177   2203   }
  2178   2204   
  2179   2205   static char *rbuObjIterGetIndexWhere(sqlite3rbu *p, RbuObjIter *pIter){
  2180   2206     sqlite3_stmt *pStmt = 0;
  2181   2207     int rc = p->rc;
  2182   2208     char *zRet = 0;
  2183   2209   
         2210  +  assert( pIter->zIdxSql==0 && pIter->nIdxCol==0 && pIter->aIdxCol==0 );
         2211  +
  2184   2212     if( rc==SQLITE_OK ){
  2185   2213       rc = prepareAndCollectError(p->dbMain, &pStmt, &p->zErrmsg,
  2186   2214           "SELECT trim(sql) FROM sqlite_master WHERE type='index' AND name=?"
  2187   2215       );
  2188   2216     }
  2189   2217     if( rc==SQLITE_OK ){
  2190   2218       int rc2;
  2191   2219       rc = sqlite3_bind_text(pStmt, 1, pIter->zIdx, -1, SQLITE_STATIC);
  2192   2220       if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
  2193         -      const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
         2221  +      char *zSql = (char*)sqlite3_column_text(pStmt, 0);
         2222  +      if( zSql ){
         2223  +        pIter->zIdxSql = zSql = rbuStrndup(zSql, &rc);
         2224  +      }
  2194   2225         if( zSql ){
  2195   2226           int nParen = 0;           /* Number of open parenthesis */
  2196   2227           int i;
         2228  +        int iIdxCol = 0;
         2229  +        int nIdxAlloc = 0;
  2197   2230           for(i=0; zSql[i]; i++){
  2198   2231             char c = zSql[i];
         2232  +
         2233  +          /* If necessary, grow the pIter->aIdxCol[] array */
         2234  +          if( iIdxCol==nIdxAlloc ){
         2235  +            RbuSpan *aIdxCol = (RbuSpan*)sqlite3_realloc(
         2236  +                pIter->aIdxCol, (nIdxAlloc+16)*sizeof(RbuSpan)
         2237  +            );
         2238  +            if( aIdxCol==0 ){
         2239  +              rc = SQLITE_NOMEM;
         2240  +              break;
         2241  +            }
         2242  +            pIter->aIdxCol = aIdxCol;
         2243  +            nIdxAlloc += 16;
         2244  +          }
         2245  +
  2199   2246             if( c=='(' ){
         2247  +            if( nParen==0 ){
         2248  +              assert( iIdxCol==0 );
         2249  +              pIter->aIdxCol[0].zSpan = &zSql[i+1];
         2250  +            }
  2200   2251               nParen++;
  2201   2252             }
  2202   2253             else if( c==')' ){
  2203   2254               nParen--;
  2204   2255               if( nParen==0 ){
         2256  +              int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
         2257  +              pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
  2205   2258                 i++;
  2206   2259                 break;
  2207   2260               }
         2261  +          }else if( c==',' && nParen==1 ){
         2262  +            int nSpan = &zSql[i] - pIter->aIdxCol[iIdxCol].zSpan;
         2263  +            pIter->aIdxCol[iIdxCol++].nSpan = nSpan;
         2264  +            pIter->aIdxCol[iIdxCol].zSpan = &zSql[i+1];
  2208   2265             }else if( c=='"' || c=='\'' || c=='`' ){
  2209   2266               for(i++; 1; i++){
  2210   2267                 if( zSql[i]==c ){
  2211   2268                   if( zSql[i+1]!=c ) break;
  2212   2269                   i++;
  2213   2270                 }
  2214   2271               }
  2215   2272             }else if( c=='[' ){
  2216   2273               for(i++; 1; i++){
  2217   2274                 if( zSql[i]==']' ) break;
  2218   2275               }
         2276  +          }else if( c=='-' && zSql[i+1]=='-' ){
         2277  +            for(i=i+2; zSql[i] && zSql[i]!='\n'; i++);
         2278  +            if( zSql[i]=='\0' ) break;
         2279  +          }else if( c=='/' && zSql[i+1]=='*' ){
         2280  +            for(i=i+2; zSql[i] && (zSql[i]!='*' || zSql[i+1]!='/'); i++);
         2281  +            if( zSql[i]=='\0' ) break;
         2282  +            i++;
  2219   2283             }
  2220   2284           }
  2221   2285           if( zSql[i] ){
  2222   2286             zRet = rbuStrndup(&zSql[i], &rc);
  2223   2287           }
         2288  +        pIter->nIdxCol = iIdxCol;
  2224   2289         }
  2225   2290       }
  2226   2291   
  2227   2292       rc2 = sqlite3_finalize(pStmt);
  2228   2293       if( rc==SQLITE_OK ) rc = rc2;
  2229   2294     }
  2230   2295   
................................................................................
  2261   2326         char *zImposterPK = 0;      /* Primary key declaration for imposter */
  2262   2327         char *zWhere = 0;           /* WHERE clause on PK columns */
  2263   2328         char *zBind = 0;
  2264   2329         char *zPart = 0;
  2265   2330         int nBind = 0;
  2266   2331   
  2267   2332         assert( pIter->eType!=RBU_PK_VTAB );
         2333  +      zPart = rbuObjIterGetIndexWhere(p, pIter);
  2268   2334         zCollist = rbuObjIterGetIndexCols(
  2269   2335             p, pIter, &zImposterCols, &zImposterPK, &zWhere, &nBind
  2270   2336         );
  2271   2337         zBind = rbuObjIterGetBindlist(p, nBind);
  2272         -      zPart = rbuObjIterGetIndexWhere(p, pIter);
  2273   2338   
  2274   2339         /* Create the imposter table used to write to this index. */
  2275   2340         sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 0, 1);
  2276   2341         sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->dbMain, "main", 1,tnum);
  2277   2342         rbuMPrintfExec(p, p->dbMain,
  2278   2343             "CREATE TABLE \"rbu_imp_%w\"( %s, PRIMARY KEY( %s ) ) WITHOUT ROWID",
  2279   2344             zTbl, zImposterCols, zImposterPK
................................................................................
  3791   3856     int nVal,
  3792   3857     sqlite3_value **apVal
  3793   3858   ){
  3794   3859     sqlite3rbu *p = (sqlite3rbu*)sqlite3_user_data(pCtx);
  3795   3860     sqlite3_stmt *pStmt = 0;
  3796   3861     char *zErrmsg = 0;
  3797   3862     int rc;
         3863  +  sqlite3 *db = (rbuIsVacuum(p) ? p->dbRbu : p->dbMain);
  3798   3864   
  3799   3865     assert( nVal==1 );
  3800   3866     
  3801         -  rc = prepareFreeAndCollectError(p->dbMain, &pStmt, &zErrmsg, 
         3867  +  rc = prepareFreeAndCollectError(db, &pStmt, &zErrmsg, 
  3802   3868         sqlite3_mprintf("SELECT count(*) FROM sqlite_master "
  3803   3869           "WHERE type='index' AND tbl_name = %Q", sqlite3_value_text(apVal[0]))
  3804   3870     );
  3805   3871     if( rc!=SQLITE_OK ){
  3806   3872       sqlite3_result_error(pCtx, zErrmsg, -1);
  3807   3873     }else{
  3808   3874       int nIndex = 0;
................................................................................
  3809   3875       if( SQLITE_ROW==sqlite3_step(pStmt) ){
  3810   3876         nIndex = sqlite3_column_int(pStmt, 0);
  3811   3877       }
  3812   3878       rc = sqlite3_finalize(pStmt);
  3813   3879       if( rc==SQLITE_OK ){
  3814   3880         sqlite3_result_int(pCtx, nIndex);
  3815   3881       }else{
  3816         -      sqlite3_result_error(pCtx, sqlite3_errmsg(p->dbMain), -1);
         3882  +      sqlite3_result_error(pCtx, sqlite3_errmsg(db), -1);
  3817   3883       }
  3818   3884     }
  3819   3885   
  3820   3886     sqlite3_free(zErrmsg);
  3821   3887   }
  3822   3888   
  3823   3889   /*

Changes to ext/rtree/rtree.c.

   665    665     int rc = SQLITE_OK;
   666    666     RtreeNode *pNode = 0;
   667    667   
   668    668     /* Check if the requested node is already in the hash table. If so,
   669    669     ** increase its reference count and return it.
   670    670     */
   671    671     if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
   672         -    assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
   673    672       if( pParent && !pNode->pParent ){
   674    673         if( nodeInParentChain(pNode, pParent) ){
   675    674           RTREE_IS_CORRUPT(pRtree);
   676    675           return SQLITE_CORRUPT_VTAB;
   677    676         }
   678    677         pParent->nRef++;
   679    678         pNode->pParent = pParent;
          679  +    }else if( pParent && pNode->pParent && pParent!=pNode->pParent ){
          680  +      RTREE_IS_CORRUPT(pRtree);
          681  +      return SQLITE_CORRUPT_VTAB;
   680    682       }
   681    683       pNode->nRef++;
   682    684       *ppNode = pNode;
   683    685       return SQLITE_OK;
   684    686     }
   685    687   
   686    688     if( pRtree->pNodeBlob ){
................................................................................
  1560   1562     int nConstraint = pCur->nConstraint;
  1561   1563     int ii;
  1562   1564     int eInt;
  1563   1565     RtreeSearchPoint x;
  1564   1566   
  1565   1567     eInt = pRtree->eCoordType==RTREE_COORD_INT32;
  1566   1568     while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
         1569  +    u8 *pCellData;
  1567   1570       pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
  1568   1571       if( rc ) return rc;
  1569   1572       nCell = NCELL(pNode);
  1570   1573       assert( nCell<200 );
         1574  +    pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
  1571   1575       while( p->iCell<nCell ){
  1572   1576         sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
  1573         -      u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
  1574   1577         eWithin = FULLY_WITHIN;
  1575   1578         for(ii=0; ii<nConstraint; ii++){
  1576   1579           RtreeConstraint *pConstraint = pCur->aConstraint + ii;
  1577   1580           if( pConstraint->op>=RTREE_MATCH ){
  1578   1581             rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
  1579   1582                                          &rScore, &eWithin);
  1580   1583             if( rc ) return rc;
  1581   1584           }else if( p->iLevel==1 ){
  1582   1585             rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
  1583   1586           }else{
  1584   1587             rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
  1585   1588           }
  1586         -        if( eWithin==NOT_WITHIN ) break;
         1589  +        if( eWithin==NOT_WITHIN ){
         1590  +          p->iCell++;
         1591  +          pCellData += pRtree->nBytesPerCell;
         1592  +          break;
         1593  +        }
  1587   1594         }
  1588         -      p->iCell++;
  1589   1595         if( eWithin==NOT_WITHIN ) continue;
         1596  +      p->iCell++;
  1590   1597         x.iLevel = p->iLevel - 1;
  1591   1598         if( x.iLevel ){
  1592   1599           x.id = readInt64(pCellData);
         1600  +        for(ii=0; ii<pCur->nPoint; ii++){
         1601  +          if( pCur->aPoint[ii].id==x.id ){
         1602  +            RTREE_IS_CORRUPT(pRtree);
         1603  +            return SQLITE_CORRUPT_VTAB;
         1604  +          }
         1605  +        }
  1593   1606           x.iCell = 0;
  1594   1607         }else{
  1595   1608           x.id = p->id;
  1596   1609           x.iCell = p->iCell - 1;
  1597   1610         }
  1598   1611         if( p->iCell>=nCell ){
  1599   1612           RTREE_QUEUE_TRACE(pCur, "POP-S:");

Changes to ext/rtree/rtreefuzz001.test.

   461    461   |   3392: 41 10 00 00 41 20 00 00 00 00 00 00 00 00 07 74   A...A .........t
   462    462   |   3408: 41 00 00 00 41 10 00 00 41 10 00 00 41 20 00 00   A...A...A...A ..
   463    463   |   3424: 00 00 00 00 00 00 07 75 41 10 00 00 41 20 00 00   .......uA...A ..
   464    464   |   3440: 41 10 00 00 41 20 00 00 00 00 00 00 00 00 00 00   A...A ..........
   465    465   | end c1b.db
   466    466     }]
   467    467     catchsql {
          468  +     PRAGMA writable_schema = 1;
   468    469        SELECT rtreecheck('t1');
   469    470     }
   470    471   } {1 {SQL logic error}}
   471    472   
   472    473   do_test rtreefuzz001-200 {
   473    474     sqlite3 db {}
   474    475     db deserialize [decode_hexdb {
................................................................................
   769    770       WITH RECURSIVE
   770    771         c1(x) AS (VALUES(0) UNION ALL SELECT x+1 FROM c1 WHERE x<8),
   771    772         c2(y) AS (VALUES(0) UNION ALL SELECT y+1 FROM c2 WHERE y<5)
   772    773       INSERT INTO t1(id, x0,x1,y0,y1,label)
   773    774         SELECT 1000+x+y*100, x, x+1, y, y+1, printf('box-%d,%d',x,y) FROM c1, c2;
   774    775     }
   775    776   } {1 {database disk image is malformed}}
          777  +
          778  +do_test rtreefuzz001-500 {
          779  +  sqlite3 db {}
          780  +  db deserialize [decode_hexdb {
          781  +| size 16384 pagesize 4096 filename crash-2e81f5dce5cbd4.db
          782  +| page 1 offset 0
          783  +|      0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00   SQLite format 3.
          784  +|     16: 10 00 01 01 00 40 20 20 00 00 00 00 00 00 00 00   .....@  ........
          785  +|     96: 00 00 00 00 0d 00 00 00 05 0e 6d 00 0f c8 0f 7b   ..........m.....
          786  +|    112: 0f 20 0e cd 0e 6d 00 00 00 00 00 00 00 00 00 00   . ...m..........
          787  +|   3680: 00 00 00 00 00 00 00 00 00 00 00 00 00 5e 05 07   .............^..
          788  +|   3696: 17 1f 1f 01 81 0b 74 61 62 6c 65 74 31 5f 70 61   ......tablet1_pa
          789  +|   3712: 72 65 6e 74 74 31 5f 70 61 72 65 6e 74 05 43 52   rentt1_parent.CR
          790  +|   3728: 45 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f 70   EATE TABLE .t1_p
          791  +|   3744: 61 72 65 6e 74 22 28 6e 6f 64 65 6e 6f 20 49 4e   arent.(nodeno IN
          792  +|   3760: 54 45 47 45 42 20 50 52 49 4d 41 52 59 20 4b 45   TEGEB PRIMARY KE
          793  +|   3776: 59 2c 70 61 72 65 6e 74 6e 6f 64 65 29 51 04 06   Y,parentnode)Q..
          794  +|   3792: 17 1b 1b 01 7b 74 61 62 6c 65 74 31 5f 6e 6f 64   .....tablet1_nod
          795  +|   3808: 65 74 31 5f 6e 6f 64 65 04 43 52 45 41 54 45 20   et1_node.CREATE 
          796  +|   3824: 54 41 42 4c 45 20 22 74 31 5f 6e 6f 64 65 22 28   TABLE .t1_node.(
          797  +|   3840: 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50   nodeno INTEGER P
          798  +|   3856: 52 49 4d 41 52 59 20 4b 45 59 2c 64 61 74 61 29   RIMARY KEY,data)
          799  +|   3872: 59 03 07 17 1d 1d 01 81 05 74 61 62 6c 65 84 31   Y........table.1
          800  +|   3888: 5f 72 6f 77 69 64 74 31 5f 72 6f 87 69 64 03 43   _rowidt1_ro.id.C
          801  +|   3904: 52 45 41 54 45 20 54 41 42 4c 45 20 22 74 31 5f   REATE TABLE .t1_
          802  +|   3920: 72 6f 77 69 64 22 28 72 6f 77 69 64 20 49 4e 54   rowid.(rowid INT
          803  +|   3936: 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59   EGER PRIMARY KEY
          804  +|   3952: 2c 6e f8 64 65 6e 6f 2c 61 30 29 4b 02 07 17 11   ,n.deno,a0)K....
          805  +|   3968: 11 08 81 03 74 22 62 6c 65 74 31 74 31 43 52 45   ....t.blet1t1CRE
          806  +|   3984: 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c   ATE VIRTUAL TABL
          807  +|   4000: 45 20 74 31 20 55 53 49 4e 47 20 72 74 72 65 65   E t1 USING rtree
          808  +|   4016: 5f 69 33 32 28 69 cc 2c 78 30 2c 78 31 2c 79 30   _i32(i.,x0,x1,y0
          809  +|   4032: 2c 79 31 2c 2b 65 78 29 36 01 06 17 17 17 01 4d   ,y1,+ex)6......M
          810  +|   4048: 74 61 62 6c 65 63 6f 6f 72 64 63 6f 6f 72 64 02   tablecoordcoord.
          811  +|   4064: 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 6f 6f   CREATE TABLE coo
          812  +|   4080: 71 64 28 76 20 49 4e 54 2c 20 77 20 49 4e 54 29   qd(v INT, w INT)
          813  +| page 2 offset 4096
          814  +|   4016: 00 00 00 00 00 00 00 00 00 00 00 05 0a 03 01 01   ................
          815  +|   4032: 0a 02 05 09 03 01 01 09 02 05 08 03 01 01 08 02   ................
          816  +|   4048: 05 07 03 01 01 07 02 05 06 03 11 01 06 02 05 05   ................
          817  +|   4064: 03 01 01 05 02 05 04 03 01 01 04 02 05 03 03 01   ................
          818  +|   4080: 01 03 02 05 02 03 01 01 02 02 04 01 03 09 01 02   ................
          819  +| page 3 offset 8192
          820  +|      0: 0d 0e 4f 00 64 0b 5a 12 0d bb 0d 84 0f eb 0d c6   ..O.d.Z.........
          821  +|     16: 0f d7 0e cc 0f c1 0f b6 0f ab 0f 9f 0f 94 0d 8f   ................
          822  +|     32: 0f 86 0d d1 0f 62 0f 67 0f 5c 0f 51 1f 46 0f 3a   .....b.g...Q.F.:
          823  +|     48: 0f 30 0d 9a 0f 21 0d dc 0f 00 00 00 00 00 00 00   .0...!..........
          824  +|   2896: 00 00 00 00 00 00 00 00 00 00 0a ce 1a 04 00 01   ................
          825  +|   2912: 17 03 31 30 78 31 30 0a 4e 19 03 ff f1 15 03 31   ..10x10.N......1
          826  +|   2928: 30 78 39 09 ce 18 04 00 01 15 03 31 30 78 38 09   0x9........10x8.
          827  +|   2944: ce 17 04 00 01 15 03 31 30 78 37 09 ce 16 04 00   .......10x7.....
          828  +|   2960: 12 15 03 31 30 78 36 09 ce 15 04 00 01 15 03 31   ...10x6........1
          829  +|   2976: 30 78 35 09 ce 14 04 00 01 15 0d a1 30 78 34 09   0x5.........0x4.
          830  +|   2992: ce 13 04 00 01 15 03 31 30 78 33 09 ce 12 04 00   .......10x3.....
          831  +|   3008: 01 15 03 31 40 78 32 09 ce 11 04 00 01 15 03 31   ...1@x2........1
          832  +|   3024: 30 78 31 09 c6 32 04 00 01 15 03 39 78 31 30 08   0x1..2.....9x10.
          833  +|   3040: c6 31 04 00 01 13 03 39 78 39 08 c6 30 04 00 01   .1.....9x9..0...
          834  +|   3056: 13 03 39 78 38 08 c6 2f 04 00 01 14 03 39 78 37   ..9x8../.....9x7
          835  +|   3072: 08 c6 2e 04 00 01 13 03 39 78 36 08 c6 2d 04 00   ........9x6..-..
          836  +|   3088: 01 13 03 39 78 34 f8 c6 2c 04 00 01 13 03 39 78   ...9x4..,.....9x
          837  +|   3104: 34 08 c6 2b 04 00 60 13 03 39 79 13 08 c6 2a 04   4..+..`..9y...*.
          838  +|   3120: 00 11 13 03 39 78 32 08 c6 29 04 00 01 13 03 39   ....9x2..).....9
          839  +|   3136: 78 31 09 be 4a 04 00 01 15 03 38 78 31 30 08 be   x1..J.....8x10..
          840  +|   3152: 49 04 00 01 13 03 38 78 39 08 be 48 04 00 01 13   I.....8x9..H....
          841  +|   3168: 03 38 77 98 08 be 47 04 00 01 14 23 38 78 37 08   .8w...G....#8x7.
          842  +|   3184: be 46 04 00 01 13 03 38 78 36 08 be 45 04 00 01   .F.....8x6..E...
          843  +|   3200: 13 03 38 78 35 08 be 44 04 00 01 13 03 38 78 34   ..8x5..D.....8x4
          844  +|   3216: 08 be 43 04 00 01 13 03 38 78 33 08 be 42 04 00   ..C.....8x3..B..
          845  +|   3232: 01 13 03 38 78 32 08 be 41 04 00 01 13 03 38 78   ...8x2..A.....8x
          846  +|   3248: 31 09 b6 62 04 00 01 15 03 37 68 31 30 08 b6 61   1..b.....7h10..a
          847  +|   3264: 04 00 01 13 03 37 79 39 08 b6 60 04 00 01 12 f3   .....7y9..`.....
          848  +|   3280: 37 78 38 08 b6 5e 04 00 01 13 03 37 78 37 08 b6   7x8..^.....7x7..
          849  +|   3296: 5e 04 00 01 13 03 37 78 36 08 b6 5d 04 00 01 13   ^.....7x6..]....
          850  +|   3312: 03 37 78 35 08 b6 5c 04 00 00 13 03 37 78 34 08   .7x5........7x4.
          851  +|   3328: b6 5b 04 00 01 13 03 37 78 33 08 b6 5a 04 00 01   .[.....7x3..Z...
          852  +|   3344: 13 03 37 78 32 08 b6 59 04 00 01 13 03 37 78 31   ..7x2..Y.....7x1
          853  +|   3360: 09 ae 7a 04 00 01 15 03 36 78 31 30 08 ae 79 04   ..z.....6x10..y.
          854  +|   3376: 00 01 e2 03 36 78 39 08 ae 78 04 00 01 13 03 36   ....6x9..x.....6
          855  +|   3392: 78 38 08 ae 77 04 00 01 13 03 36 78 37 08 ae 76   x8..w.....6x7..v
          856  +|   3408: 04 00 01 13 03 36 78 36 08 ae 85 04 00 01 13 03   .....6x6........
          857  +|   3424: 36 78 35 08 ae 73 f4 00 01 13 03 36 78 34 08 ae   6x5..s.....6x4..
          858  +|   3440: 73 04 00 01 13 03 36 78 33 08 ae 72 04 00 01 13   s.....6x3..r....
          859  +|   3456: 03 36 78 32 08 87 6a 04 00 01 13 02 3d e8 32 08   .6x2..j.....=.2.
          860  +|   3472: 8f 52 04 00 01 13 02 32 78 32 08 97 3b 04 00 01   .R.....2x2..;...
          861  +|   3488: 13 02 33 78 32 08 9f 22 04 00 01 13 02 34 78 32   ..3x2........4x2
          862  +|   3504: 08 a7 0a 04 00 01 13 02 35 78 32 08 87 69 04 00   ........5x2..i..
          863  +|   3520: 01 13 02 31 78 31 08 87 6c 04 00 01 13 02 31 78   ...1x1..l.....1x
          864  +|   3536: 34 08 8f 54 04 00 01 13 02 32 78 34 08 97 3c 04   4..T.....2x4..<.
          865  +|   3552: 00 01 12 f2 33 78 34 08 9f 24 04 00 01 13 02 34   ....3x4..$.....4
          866  +|   3568: 78 34 08 a7 0c 04 00 01 13 02 35 78 34 0e 6c 00   x4........5x4.l.
          867  +|   3584: 08 ae 71 04 00 01 13 03 36 78 31 09 a7 12 04 00   ..q.....6x1.....
          868  +|   3600: 01 15 02 35 78 31 30 08 a7 11 04 00 01 13 02 35   ...5x10........5
          869  +|   3616: 78 39 08 a7 10 04 00 01 13 02 35 78 38 08 a7 0f   x9........5x8...
          870  +|   3632: 04 00 01 14 02 35 78 37 08 a7 0e 04 00 01 13 02   .....5x7........
          871  +|   3648: 35 78 36 08 a7 0d 04 00 01 13 02 35 78 35 0e 0e   5x6........5x5..
          872  +|   3664: b3 00 08 00 01 00 03 08 a7 0b 04 00 01 13 02 35   ...............5
          873  +|   3680: 78 33 0e d1 00 08 a7 09 04 00 01 13 02 35 78 31   x3...........5x1
          874  +|   3696: 09 9f 2a 04 00 01 15 02 34 78 31 30 03 cf 29 04   ..*.....4x10..).
          875  +|   3712: 00 01 13 02 34 78 39 08 9f 28 04 00 01 13 02 34   ....4x9..(.....4
          876  +|   3728: 78 38 09 9f 27 04 00 01 13 02 34 78 37 08 9f 26   x8..'.....4x7..&
          877  +|   3744: 04 00 01 13 0e a4 78 36 08 9f 25 04 00 01 13 02   ......x6..%.....
          878  +|   3760: 34 78 35 0f 18 00 09 00 09 13 34 78 08 9f 23 04   4x5.......4x..#.
          879  +|   3776: 00 01 13 02 34 78 33 0f 36 00 08 9f 21 04 00 01   ....4x3.6...!...
          880  +|   3792: 13 02 34 78 31 09 97 42 04 00 01 15 02 33 78 31   ..4x1..B.....3x1
          881  +|   3808: 30 08 97 41 04 00 01 13 02 33 78 39 08 97 40 04   0..A.....3x9..@.
          882  +|   3824: 00 01 13 02 33 78 38 18 97 3f 04 00 01 13 02 33   ....3x8..?.....3
          883  +|   3840: 78 37 08 97 3e 04 00 01 13 02 33 78 36 08 97 3d   x7..>.....3x6..=
          884  +|   3856: 04 00 01 13 02 33 78 35 1f 7d 00 09 00 09 13 33   .....3x5.......3
          885  +|   3872: 78 07 97 3b 04 00 01 13 02 33 78 33 0f 9b 00 08   x..;.....3x3....
          886  +|   3888: 97 39 04 00 01 13 02 33 78 31 09 8f 5a 04 00 01   .9.....3x1..Z...
          887  +|   3904: 15 02 32 79 31 30 08 8f 59 04 00 01 13 fa 32 78   ..2y10..Y.....2x
          888  +|   3920: 39 08 8f 58 04 00 01 13 02 32 78 38 08 8f 57 04   9..X.....2x8..W.
          889  +|   3936: 00 01 13 02 32 78 37 08 8f 56 04 00 01 13 02 32   ....2x7..V.....2
          890  +|   3952: 78 36 08 8f 55 04 00 01 13 02 32 78 35 0f e2 00   x6..U.....2x5...
          891  +|   3968: 09 00 09 13 32 78 08 8f 53 04 00 01 13 02 32 78   ....2x..S.....2x
          892  +|   3984: 33 00 00 00 08 8f 51 04 00 01 13 02 aa 78 31 09   3.....Q......x1.
          893  +|   4000: 87 72 04 00 01 15 02 31 78 31 30 08 87 71 04 00   .r.....1x10..q..
          894  +|   4016: 01 13 03 31 78 39 08 87 70 04 00 01 13 02 31 78   ...1x9..p.....1x
          895  +|   4032: 38 08 87 6f 04 00 01 13 02 31 78 37 08 87 6e 04   8..o.....1x7..n.
          896  +|   4048: 00 01 13 02 31 78 36 08 87 6d 04 00 01 13 02 31   ....1x6..m.....1
          897  +|   4064: 7d 25 0f f9 00 08 ff f9 13 31 78 08 87 6b 04 00   .%.......1x..k..
          898  +|   4080: 01 13 02 31 78 33 00 00 00 00 00 08 00 01 00 03   ...1x3..........
          899  +| page 4 offset 12288
          900  +|      0: 0d 00 00 00 03 01 87 00 0b 2d 06 5a 01 87 00 00   .........-.Z....
          901  +|    384: 00 00 00 00 00 00 00 89 50 01 54 00 93 24 00 00   ........P.T..$..
          902  +|    400: 00 32 00 00 00 00 00 00 23 2f 00 00 00 09 00 00   .2......#/......
          903  +|    416: 00 0b 00 00 00 07 00 00 00 09 00 00 00 00 00 00   ................
          904  +|    432: 23 2e 00 00 10 09 00 00 00 0b 00 00 00 06 00 00   #...............
          905  +|    448: 00 08 00 00 00 00 00 00 23 2d 00 00 00 09 00 00   ........#-......
          906  +|    464: 00 0b 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          907  +|    480: 23 2c 00 00 00 09 00 00 00 0b 00 00 00 04 00 00   #,..............
          908  +|    496: 00 06 00 00 00 00 00 00 23 2b 00 00 00 09 00 00   ........#+......
          909  +|    512: 00 0b 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          910  +|    528: 23 2a 00 00 00 09 00 00 00 0b 00 00 00 02 00 00   #*..............
          911  +|    544: 00 04 00 00 00 00 00 00 23 29 00 00 00 09 00 00   ........#)......
          912  +|    560: 00 0b 00 00 00 01 00 00 00 03 00 00 00 00 00 00   ................
          913  +|    576: 1f 4a 00 00 00 08 00 00 00 0a 00 00 00 0a 00 00   .J..............
          914  +|    592: 00 0c 00 00 00 00 00 00 0f 49 00 00 00 08 00 00   .........I......
          915  +|    608: 00 0a 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          916  +|    624: 1f 48 00 00 00 08 00 00 00 0a 00 00 00 08 00 06   .H..............
          917  +|    640: 00 0a 00 00 00 00 00 00 1f 47 00 00 00 08 00 00   .........G......
          918  +|    656: 00 0a 00 00 00 07 00 00 00 09 00 00 00 00 00 00   ................
          919  +|    672: 15 d6 00 00 00 08 00 00 00 0a 00 00 00 06 00 00   ................
          920  +|    688: 00 08 00 00 00 00 00 00 1f 45 00 00 00 08 00 00   .........E......
          921  +|    704: 00 0a 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          922  +|    720: 1f 44 00 00 00 08 00 00 00 0a 00 00 00 04 00 00   .D..............
          923  +|    736: 00 06 00 00 00 00 00 00 1f 43 00 00 00 07 ff ff   .........C......
          924  +|    752: f0 0a 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          925  +|    768: 1f 42 00 00 00 08 00 00 00 0a 00 00 00 01 ff f0   .B..............
          926  +|    784: 00 03 ff ff ff ff ff ff 1f 41 00 00 00 08 00 00   .........A......
          927  +|    800: 00 0a 00 00 00 01 00 00 00 03 00 00 00 00 00 00   ................
          928  +|    816: 1b 62 00 00 00 07 00 00 00 09 00 00 00 0a 00 00   .b..............
          929  +|    832: 00 0c 05 00 00 00 00 00 1b 64 10 00 00 07 00 00   .........d......
          930  +|    848: 00 09 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          931  +|    864: 1b 60 00 00 00 07 00 00 00 09 00 00 00 08 00 00   .`..............
          932  +|    880: 00 0a 00 00 00 00 00 00 1b 5f 00 00 00 07 00 00   ........._......
          933  +|    896: 00 09 00 00 00 07 00 00 00 09 00 00 00 00 00 00   ................
          934  +|    912: 1b 5e 00 00 00 07 00 00 00 09 00 00 00 06 00 00   .^..............
          935  +|    928: 00 08 00 00 00 00 00 00 1b 5d 00 00 00 08 00 00   .........]......
          936  +|    944: 00 09 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          937  +|    960: 1b 5c 00 00 00 07 00 00 00 09 00 00 00 04 00 00   ................
          938  +|    976: 06 46 00 00 00 00 00 00 1b 5b 00 00 00 07 00 00   .F.......[......
          939  +|    992: 00 09 00 00 00 03 00 00 00 04 ff f0 00 00 00 00   ................
          940  +|   1008: 1b 5a 00 00 00 07 00 00 00 19 00 00 00 02 00 00   .Z..............
          941  +|   1024: 00 04 00 00 00 00 00 00 1b 59 00 00 00 07 00 00   .........Y......
          942  +|   1040: 00 09 00 00 00 01 00 00 00 03 00 00 00 00 ff f0   ................
          943  +|   1056: 17 7a 00 00 00 06 00 00 00 08 00 00 00 0a 00 00   .z..............
          944  +|   1072: 00 0c 00 00 00 00 00 00 17 79 00 00 00 06 00 00   .........y......
          945  +|   1088: 00 08 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          946  +|   1104: 17 78 00 00 00 06 00 00 00 08 00 00 00 08 00 00   .x..............
          947  +|   1120: 00 0a 00 00 00 00 00 00 17 77 00 00 00 06 10 00   .........w......
          948  +|   1136: 00 08 00 00 00 07 00 09 c0 09 00 00 00 00 00 00   ................
          949  +|   1152: 17 76 00 00 00 06 00 00 00 08 00 00 00 06 00 00   .v..............
          950  +|   1168: 00 08 00 00 00 00 00 00 17 75 00 00 00 06 00 00   .........u......
          951  +|   1184: 00 08 00 00 00 05 00 00 00 07 00 00 00 00 00 00   ................
          952  +|   1200: 17 74 00 00 00 06 00 00 00 08 00 00 00 03 ff ff   .t..............
          953  +|   1216: f0 06 00 00 00 83 00 00 17 73 00 00 00 06 00 00   .........s......
          954  +|   1232: 00 08 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          955  +|   1248: 17 71 ff 00 00 06 00 00 10 08 00 00 00 02 00 00   .q..............
          956  +|   1264: 00 04 00 00 c0 00 00 00 17 0d 00 00 00 06 00 00   ................
          957  +|   1280: 00 08 00 00 e7 01 00 00 00 03 00 00 09 e0 00 00   ................
          958  +|   1296: 23 30 00 00 00 09 00 00 00 0a 00 00 00 08 00 00   #0..............
          959  +|   1312: 00 0a 00 00 00 00 bb 00 23 31 00 00 00 09 00 00   ........#1......
          960  +|   1328: 00 0b 00 00 00 09 00 00 00 0b 00 00 00 00 00 00   ................
          961  +|   1344: 23 32 00 00 00 09 00 00 00 0b 00 00 00 0a 00 00   #2..............
          962  +|   1360: 00 0c 00 00 00 00 00 00 27 11 00 00 00 0a 00 00   ........'.......
          963  +|   1376: 00 0c 00 00 00 01 00 08 c0 03 00 00 00 00 00 00   ................
          964  +|   1392: 27 12 00 00 00 0a 00 00 00 0c 51 00 00 02 00 00   '.........Q.....
          965  +|   1408: 00 04 6f 00 00 00 00 00 27 13 00 00 00 09 ff ff   ..o.....'.......
          966  +|   1424: 00 0c 00 00 00 03 00 00 00 05 00 00 00 00 00 00   ................
          967  +|   1440: 27 14 00 00 00 0a 00 00 00 00 00 00 00 00 00 00   '...............
          968  +|   1616: 00 00 00 00 00 00 00 00 00 00 89 50 02 04 00 93   ...........P....
          969  +|   1632: 24 00 00 00 32 00 00 00 00 00 00 23 8c 00 00 00   $...2......#....
          970  +|   1648: 05 00 00 00 07 00 00 00 04 00 00 00 06 00 00 00   ................
          971  +|   1664: 00 00 00 0f a4 00 00 00 04 00 00 00 06 00 00 00   ................
          972  +|   1680: 04 00 00 00 06 00 00 00 00 00 00 0b bc 00 00 00   ................
          973  +|   1696: 03 00 00 00 05 00 00 00 04 00 00 00 06 00 00 00   ................
          974  +|   1712: 00 00 00 07 d4 00 00 00 02 00 00 00 04 00 00 00   ................
          975  +|   1728: 04 00 00 00 06 00 00 00 10 00 00 03 ec 00 00 00   ................
          976  +|   1744: 01 00 00 00 03 00 00 00 04 00 00 00 06 00 00 00   ................
          977  +|   1760: 00 00 00 13 8d 00 00 00 05 00 00 00 07 00 00 00   ................
          978  +|   1776: 05 00 00 00 07 00 00 00 00 00 00 0f a5 00 00 00   ................
          979  +|   1792: 04 00 00 00 06 00 00 00 05 00 00 00 07 00 00 00   ................
          980  +|   1808: 00 00 00 0b bd 00 00 00 03 00 00 00 05 00 00 00   ................
          981  +|   1824: 05 00 00 00 07 00 00 00 00 00 00 07 d5 00 00 00   ................
          982  +|   1840: 02 00 00 00 05 00 00 00 05 00 00 00 07 00 00 00   ................
          983  +|   1856: 00 00 00 03 ed 00 00 00 01 00 00 00 03 00 00 00   ................
          984  +|   1872: 05 00 00 00 07 00 00 00 00 00 00 13 8e 00 00 00   ................
          985  +|   1888: 05 00 00 00 07 00 00 00 06 00 00 00 08 00 00 00   ................
          986  +|   1904: 00 00 00 0f a6 00 00 00 04 00 00 00 06 00 00 00   ................
          987  +|   1920: 06 00 00 00 07 ff ff 00 00 00 00 0b be 00 00 00   ................
          988  +|   1936: 0b 40 00 00 05 00 00 00 06 00 00 00 08 00 00 00   .@..............
          989  +|   1952: 00 00 00 07 d6 00 00 00 02 00 00 00 04 00 00 00   ................
          990  +|   1968: 05 00 00 00 08 00 00 00 00 00 00 03 ee 00 00 00   ................
          991  +|   1984: 01 00 00 00 02 ff ff 00 06 00 00 00 08 00 00 00   ................
          992  +|   2000: 00 00 00 13 8f 00 00 00 05 00 00 00 07 00 00 00   ................
          993  +|   2016: 07 00 00 00 09 00 00 00 00 00 00 0f a7 00 00 00   ................
          994  +|   2032: 04 00 00 00 06 00 00 00 07 00 00 00 09 00 00 08   ................
          995  +|   2048: 30 00 00 0b bf 00 00 00 03 00 00 00 05 00 00 00   0...............
          996  +|   2064: 07 00 00 00 09 00 00 00 00 00 00 07 d7 00 00 00   ................
          997  +|   2080: 02 00 00 00 04 00 00 00 07 00 00 00 09 00 00 00   ................
          998  +|   2096: 00 00 00 03 ef 00 00 00 01 00 00 00 03 00 00 00   ................
          999  +|   2112: 07 00 00 00 09 00 00 00 00 00 00 13 90 00 00 00   ................
         1000  +|   2128: 05 00 01 00 07 00 00 00 08 00 00 00 0a 00 00 00   ................
         1001  +|   2144: 00 00 00 0f a8 00 00 00 04 00 00 00 06 00 00 00   ................
         1002  +|   2160: 08 00 00 00 0a 00 00 00 00 00 00 0b f2 00 00 00   ................
         1003  +|   2176: 03 00 00 00 05 00 00 00 08 00 00 00 0a 00 00 01   ................
         1004  +|   2192: 00 00 00 07 d8 00 00 00 02 00 00 00 04 00 00 00   ................
         1005  +|   2208: 08 00 00 00 0a 00 00 00 00 00 00 03 f0 00 00 00   ................
         1006  +|   2224: 01 00 00 00 03 00 00 00 08 00 00 00 09 ff 00 00   ................
         1007  +|   2240: 00 00 00 13 91 00 00 00 05 00 00 00 07 00 00 00   ................
         1008  +|   2256: 09 00 00 00 0b 00 00 00 00 00 00 0f a9 00 00 00   ................
         1009  +|   2272: 04 00 00 00 06 00 00 00 09 00 00 00 0b 00 00 00   ................
         1010  +|   2288: 00 00 00 0b c1 00 00 00 03 00 00 00 05 00 00 00   ................
         1011  +|   2304: 09 00 00 00 0b 00 00 00 00 00 00 07 d9 00 00 00   ................
         1012  +|   2320: 02 00 00 00 04 00 00 00 09 00 00 00 0b 00 00 01   ................
         1013  +|   2336: 00 00 00 03 f0 ff ff 00 01 00 00 00 03 00 00 00   ................
         1014  +|   2352: 09 00 00 00 0b 00 00 00 00 00 00 13 92 00 00 00   ................
         1015  +|   2368: 05 00 00 00 07 00 00 00 0a 00 00 00 0c 00 00 00   ................
         1016  +|   2384: 00 00 00 0f aa 00 00 00 04 00 00 00 06 00 00 00   ................
         1017  +|   2400: 0a 00 00 00 0c 00 00 00 00 00 00 0b c2 00 00 00   ................
         1018  +|   2416: 03 00 00 00 05 00 00 00 0a 00 00 00 0c 00 00 00   ................
         1019  +|   2432: 00 00 00 07 da 00 00 00 02 00 00 00 04 00 00 00   ................
         1020  +|   2448: 0a 00 00 00 0c 00 00 00 00 00 00 03 f2 00 00 00   ................
         1021  +|   2464: 01 00 00 10 03 00 00 00 0a 00 00 00 0c 00 00 00   ................
         1022  +|   2480: 00 00 00 03 eb 00 00 00 01 00 00 00 03 00 00 00   ................
         1023  +|   2496: 03 00 00 00 05 00 00 00 00 00 00 07 d3 00 00 00   ................
         1024  +|   2512: 02 00 00 00 04 00 00 00 03 00 00 00 05 00 00 00   ................
         1025  +|   2528: 00 00 00 0b bb 00 00 00 03 00 00 00 05 00 00 00   ................
         1026  +|   2544: 03 00 00 00 05 00 00 00 00 00 00 0f a3 00 00 00   ................
         1027  +|   2560: 04 00 00 00 06 00 00 00 03 00 00 00 05 00 00 00   ................
         1028  +|   2576: 00 00 00 13 8b 00 00 00 05 00 00 00 07 00 00 00   ................
         1029  +|   2592: 03 00 00 00 05 00 00 00 00 00 00 03 ea 00 00 00   ................
         1030  +|   2608: 01 00 00 00 03 00 00 00 02 00 00 00 04 00 00 00   ................
         1031  +|   2624: 00 00 00 07 d2 00 00 00 02 00 00 00 04 00 00 00   ................
         1032  +|   2640: 02 00 00 00 04 00 00 00 00 00 00 0b ba 00 00 00   ................
         1033  +|   2656: 03 00 00 00 05 00 00 00 02 00 00 00 04 00 00 00   ................
         1034  +|   2672: 00 00 00 0f a1 ff ff ff 04 00 00 00 06 00 00 00   ................
         1035  +|   2688: 02 00 00 00 04 00 00 00 00 00 00 13 8a 00 00 00   ................
         1036  +|   2704: 05 00 00 00 06 ff ff ff f2 00 00 00 04 00 00 00   ................
         1037  +|   2720: 00 00 00 03 e9 00 00 00 01 00 00 00 03 00 00 00   ................
         1038  +|   2736: 01 00 00 00 03 00 00 00 00 00 00 07 d1 00 00 00   ................
         1039  +|   2848: 00 00 00 00 00 00 00 00 00 00 00 00 00 89 50 01   ..............P.
         1040  +|   2864: 04 00 93 24 00 01 00 02 00 00 00 00 00 00 00 02   ...$............
         1041  +|   2880: ff ff ff 06 00 00 00 0c 00 00 00 01 00 00 00 0b   ................
         1042  +|   2896: 00 00 00 00 00 00 00 02 40 00 00 00 00 00 00 00   ........@.......
         1043  +| end crash-2e81f5dce5cbd4.db}]
         1044  +  execsql { PRAGMA writable_schema = 1;}
         1045  +  catchsql {UPDATE t1 SET ex= ex ISNULL}
         1046  +} {1 {database disk image is malformed}}
         1047  +
   776   1048   
   777   1049   finish_test

Changes to ext/session/sqlite3session.c.

  1840   1840   */
  1841   1841   static int sessionBufferGrow(SessionBuffer *p, size_t nByte, int *pRc){
  1842   1842     if( *pRc==SQLITE_OK && (size_t)(p->nAlloc-p->nBuf)<nByte ){
  1843   1843       u8 *aNew;
  1844   1844       i64 nNew = p->nAlloc ? p->nAlloc : 128;
  1845   1845       do {
  1846   1846         nNew = nNew*2;
  1847         -    }while( (nNew-p->nBuf)<nByte );
         1847  +    }while( (size_t)(nNew-p->nBuf)<nByte );
  1848   1848   
  1849   1849       aNew = (u8 *)sqlite3_realloc64(p->aBuf, nNew);
  1850   1850       if( 0==aNew ){
  1851   1851         *pRc = SQLITE_NOMEM;
  1852   1852       }else{
  1853   1853         p->aBuf = aNew;
  1854   1854         p->nAlloc = nNew;

Changes to main.mk.

   525    525   SHELL_OPT += -DSQLITE_ENABLE_RTREE
   526    526   SHELL_OPT += -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   527    527   SHELL_OPT += -DSQLITE_ENABLE_UNKNOWN_SQL_FUNCTION
   528    528   SHELL_OPT += -DSQLITE_ENABLE_STMTVTAB
   529    529   SHELL_OPT += -DSQLITE_ENABLE_DBPAGE_VTAB
   530    530   SHELL_OPT += -DSQLITE_ENABLE_DBSTAT_VTAB
   531    531   SHELL_OPT += -DSQLITE_ENABLE_OFFSET_SQL_FUNC
   532         -SHELL_OPT += -DSQLITE_INTROSPECTION_PRAGMAS
   533    532   FUZZERSHELL_OPT = -DSQLITE_ENABLE_JSON1
   534    533   FUZZCHECK_OPT = -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_MEMSYS5
   535    534   FUZZCHECK_OPT += -DSQLITE_MAX_MEMORY=50000000
   536    535   FUZZCHECK_OPT += -DSQLITE_PRINTF_PRECISION_LIMIT=1000
   537    536   FUZZCHECK_OPT += -DSQLITE_ENABLE_DESERIALIZE
   538    537   FUZZCHECK_OPT += -DSQLITE_ENABLE_FTS4
   539    538   FUZZCHECK_OPT += -DSQLITE_ENABLE_RTREE

Changes to src/alter.c.

   132    132   
   133    133     /* Make sure it is not a system table being altered, or a reserved name
   134    134     ** that the table is being renamed to.
   135    135     */
   136    136     if( SQLITE_OK!=isAlterableTable(pParse, pTab) ){
   137    137       goto exit_rename_table;
   138    138     }
   139         -  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
   140         -    exit_rename_table;
          139  +  if( SQLITE_OK!=sqlite3CheckObjectName(pParse,zName,"table",zName) ){
          140  +    goto exit_rename_table;
   141    141     }
   142    142   
   143    143   #ifndef SQLITE_OMIT_VIEW
   144    144     if( pTab->pSelect ){
   145    145       sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
   146    146       goto exit_rename_table;
   147    147     }

Changes to src/analyze.c.

    23     23   **    CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
    24     24   **
    25     25   ** Additional tables might be added in future releases of SQLite.
    26     26   ** The sqlite_stat2 table is not created or used unless the SQLite version
    27     27   ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
    28     28   ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
    29     29   ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
    30         -** created and used by SQLite versions 3.7.9 and later and with
           30  +** created and used by SQLite versions 3.7.9 through 3.29.0 when
    31     31   ** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
    32         -** is a superset of sqlite_stat2.  The sqlite_stat4 is an enhanced
    33         -** version of sqlite_stat3 and is only available when compiled with
    34         -** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later.  It is
    35         -** not possible to enable both STAT3 and STAT4 at the same time.  If they
    36         -** are both enabled, then STAT4 takes precedence.
           32  +** is a superset of sqlite_stat2 and is also now deprecated.  The
           33  +** sqlite_stat4 is an enhanced version of sqlite_stat3 and is only 
           34  +** available when compiled with SQLITE_ENABLE_STAT4 and in SQLite
           35  +** versions 3.8.1 and later.  STAT4 is the only variant that is still
           36  +** supported.
    37     37   **
    38     38   ** For most applications, sqlite_stat1 provides all the statistics required
    39     39   ** for the query planner to make good choices.
    40     40   **
    41     41   ** Format of sqlite_stat1:
    42     42   **
    43     43   ** There is normally one row per index, with the index identified by the
................................................................................
   140    140   ** integer in the equivalent columns in sqlite_stat4.
   141    141   */
   142    142   #ifndef SQLITE_OMIT_ANALYZE
   143    143   #include "sqliteInt.h"
   144    144   
   145    145   #if defined(SQLITE_ENABLE_STAT4)
   146    146   # define IsStat4     1
   147         -# define IsStat3     0
   148         -#elif defined(SQLITE_ENABLE_STAT3)
   149         -# define IsStat4     0
   150         -# define IsStat3     1
   151    147   #else
   152    148   # define IsStat4     0
   153         -# define IsStat3     0
   154    149   # undef SQLITE_STAT4_SAMPLES
   155    150   # define SQLITE_STAT4_SAMPLES 1
   156    151   #endif
   157         -#define IsStat34    (IsStat3+IsStat4)  /* 1 for STAT3 or STAT4. 0 otherwise */
   158    152   
   159    153   /*
   160    154   ** This routine generates code that opens the sqlite_statN tables.
   161    155   ** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
   162    156   ** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
   163    157   ** appropriate compile-time options are provided.
   164    158   **
................................................................................
   179    173     static const struct {
   180    174       const char *zName;
   181    175       const char *zCols;
   182    176     } aTable[] = {
   183    177       { "sqlite_stat1", "tbl,idx,stat" },
   184    178   #if defined(SQLITE_ENABLE_STAT4)
   185    179       { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
   186         -    { "sqlite_stat3", 0 },
   187         -#elif defined(SQLITE_ENABLE_STAT3)
   188         -    { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
   189         -    { "sqlite_stat4", 0 },
   190    180   #else
   191         -    { "sqlite_stat3", 0 },
   192    181       { "sqlite_stat4", 0 },
   193    182   #endif
          183  +    { "sqlite_stat3", 0 },
   194    184     };
   195    185     int i;
   196    186     sqlite3 *db = pParse->db;
   197    187     Db *pDb;
   198    188     Vdbe *v = sqlite3GetVdbe(pParse);
   199    189     int aRoot[ArraySize(aTable)];
   200    190     u8 aCreateTbl[ArraySize(aTable)];
................................................................................
   267    257   ** information.
   268    258   */
   269    259   typedef struct Stat4Accum Stat4Accum;
   270    260   typedef struct Stat4Sample Stat4Sample;
   271    261   struct Stat4Sample {
   272    262     tRowcnt *anEq;                  /* sqlite_stat4.nEq */
   273    263     tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
   274         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          264  +#ifdef SQLITE_ENABLE_STAT4
   275    265     tRowcnt *anLt;                  /* sqlite_stat4.nLt */
   276    266     union {
   277    267       i64 iRowid;                     /* Rowid in main table of the key */
   278    268       u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
   279    269     } u;
   280    270     u32 nRowid;                     /* Sizeof aRowid[] */
   281    271     u8 isPSample;                   /* True if a periodic sample */
................................................................................
   298    288     int iGet;                 /* Index of current sample accessed by stat_get() */
   299    289     Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
   300    290     sqlite3 *db;              /* Database connection, for malloc() */
   301    291   };
   302    292   
   303    293   /* Reclaim memory used by a Stat4Sample
   304    294   */
   305         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          295  +#ifdef SQLITE_ENABLE_STAT4
   306    296   static void sampleClear(sqlite3 *db, Stat4Sample *p){
   307    297     assert( db!=0 );
   308    298     if( p->nRowid ){
   309    299       sqlite3DbFree(db, p->u.aRowid);
   310    300       p->nRowid = 0;
   311    301     }
   312    302   }
   313    303   #endif
   314    304   
   315    305   /* Initialize the BLOB value of a ROWID
   316    306   */
   317         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          307  +#ifdef SQLITE_ENABLE_STAT4
   318    308   static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
   319    309     assert( db!=0 );
   320    310     if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
   321    311     p->u.aRowid = sqlite3DbMallocRawNN(db, n);
   322    312     if( p->u.aRowid ){
   323    313       p->nRowid = n;
   324    314       memcpy(p->u.aRowid, pData, n);
................................................................................
   326    316       p->nRowid = 0;
   327    317     }
   328    318   }
   329    319   #endif
   330    320   
   331    321   /* Initialize the INTEGER value of a ROWID.
   332    322   */
   333         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          323  +#ifdef SQLITE_ENABLE_STAT4
   334    324   static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
   335    325     assert( db!=0 );
   336    326     if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
   337    327     p->nRowid = 0;
   338    328     p->u.iRowid = iRowid;
   339    329   }
   340    330   #endif
   341    331   
   342    332   
   343    333   /*
   344    334   ** Copy the contents of object (*pFrom) into (*pTo).
   345    335   */
   346         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          336  +#ifdef SQLITE_ENABLE_STAT4
   347    337   static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
   348    338     pTo->isPSample = pFrom->isPSample;
   349    339     pTo->iCol = pFrom->iCol;
   350    340     pTo->iHash = pFrom->iHash;
   351    341     memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
   352    342     memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
   353    343     memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
................................................................................
   360    350   #endif
   361    351   
   362    352   /*
   363    353   ** Reclaim all memory of a Stat4Accum structure.
   364    354   */
   365    355   static void stat4Destructor(void *pOld){
   366    356     Stat4Accum *p = (Stat4Accum*)pOld;
   367         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          357  +#ifdef SQLITE_ENABLE_STAT4
   368    358     int i;
   369    359     for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
   370    360     for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
   371    361     sampleClear(p->db, &p->current);
   372    362   #endif
   373    363     sqlite3DbFree(p->db, p);
   374    364   }
................................................................................
   380    370   **     K:    The number of columns in the index excluding the rowid/pk.
   381    371   **     C:    The number of rows in the index (note 2)
   382    372   **
   383    373   ** Note 1:  In the special case of the covering index that implements a
   384    374   ** WITHOUT ROWID table, N is the number of PRIMARY KEY columns, not the
   385    375   ** total number of columns in the table.
   386    376   **
   387         -** Note 2:  C is only used for STAT3 and STAT4.
          377  +** Note 2:  C is only used for STAT4.
   388    378   **
   389    379   ** For indexes on ordinary rowid tables, N==K+1.  But for indexes on
   390    380   ** WITHOUT ROWID tables, N=K+P where P is the number of columns in the
   391    381   ** PRIMARY KEY of the table.  The covering index that implements the
   392    382   ** original WITHOUT ROWID table as N==K as a special case.
   393    383   **
   394    384   ** This routine allocates the Stat4Accum object in heap memory. The return 
................................................................................
   403    393   ){
   404    394     Stat4Accum *p;
   405    395     int nCol;                       /* Number of columns in index being sampled */
   406    396     int nKeyCol;                    /* Number of key columns */
   407    397     int nColUp;                     /* nCol rounded up for alignment */
   408    398     int n;                          /* Bytes of space to allocate */
   409    399     sqlite3 *db;                    /* Database connection */
   410         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          400  +#ifdef SQLITE_ENABLE_STAT4
   411    401     int mxSample = SQLITE_STAT4_SAMPLES;
   412    402   #endif
   413    403   
   414    404     /* Decode the three function arguments */
   415    405     UNUSED_PARAMETER(argc);
   416    406     nCol = sqlite3_value_int(argv[0]);
   417    407     assert( nCol>0 );
................................................................................
   420    410     assert( nKeyCol<=nCol );
   421    411     assert( nKeyCol>0 );
   422    412   
   423    413     /* Allocate the space required for the Stat4Accum object */
   424    414     n = sizeof(*p) 
   425    415       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
   426    416       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
   427         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          417  +#ifdef SQLITE_ENABLE_STAT4
   428    418       + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
   429    419       + sizeof(Stat4Sample)*(nCol+mxSample)     /* Stat4Accum.aBest[], a[] */
   430    420       + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
   431    421   #endif
   432    422     ;
   433    423     db = sqlite3_context_db_handle(context);
   434    424     p = sqlite3DbMallocZero(db, n);
................................................................................
   440    430     p->db = db;
   441    431     p->nRow = 0;
   442    432     p->nCol = nCol;
   443    433     p->nKeyCol = nKeyCol;
   444    434     p->current.anDLt = (tRowcnt*)&p[1];
   445    435     p->current.anEq = &p->current.anDLt[nColUp];
   446    436   
   447         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          437  +#ifdef SQLITE_ENABLE_STAT4
   448    438     {
   449    439       u8 *pSpace;                     /* Allocated space not yet assigned */
   450    440       int i;                          /* Used to iterate through p->aSample[] */
   451    441   
   452    442       p->iGet = -1;
   453    443       p->mxSample = mxSample;
   454    444       p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[2])/(mxSample/3+1) + 1);
................................................................................
   475    465     /* Return a pointer to the allocated object to the caller.  Note that
   476    466     ** only the pointer (the 2nd parameter) matters.  The size of the object
   477    467     ** (given by the 3rd parameter) is never used and can be any positive
   478    468     ** value. */
   479    469     sqlite3_result_blob(context, p, sizeof(*p), stat4Destructor);
   480    470   }
   481    471   static const FuncDef statInitFuncdef = {
   482         -  2+IsStat34,      /* nArg */
          472  +  2+IsStat4,       /* nArg */
   483    473     SQLITE_UTF8,     /* funcFlags */
   484    474     0,               /* pUserData */
   485    475     0,               /* pNext */
   486    476     statInit,        /* xSFunc */
   487    477     0,               /* xFinalize */
   488    478     0, 0,            /* xValue, xInverse */
   489    479     "stat_init",     /* zName */
................................................................................
   515    505       if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
   516    506     }
   517    507     if( pNew->iHash>pOld->iHash ) return 1;
   518    508     return 0;
   519    509   }
   520    510   #endif
   521    511   
   522         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          512  +#ifdef SQLITE_ENABLE_STAT4
   523    513   /*
   524    514   ** Return true if pNew is to be preferred over pOld.
   525    515   **
   526    516   ** This function assumes that for each argument sample, the contents of
   527    517   ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid. 
   528    518   */
   529    519   static int sampleIsBetter(
................................................................................
   534    524     tRowcnt nEqNew = pNew->anEq[pNew->iCol];
   535    525     tRowcnt nEqOld = pOld->anEq[pOld->iCol];
   536    526   
   537    527     assert( pOld->isPSample==0 && pNew->isPSample==0 );
   538    528     assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
   539    529   
   540    530     if( (nEqNew>nEqOld) ) return 1;
   541         -#ifdef SQLITE_ENABLE_STAT4
   542    531     if( nEqNew==nEqOld ){
   543    532       if( pNew->iCol<pOld->iCol ) return 1;
   544    533       return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
   545    534     }
   546    535     return 0;
   547         -#else
   548         -  return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
   549         -#endif
   550    536   }
   551    537   
   552    538   /*
   553    539   ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
   554    540   ** remove the least desirable sample from p->a[] to make room.
   555    541   */
   556    542   static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
   557    543     Stat4Sample *pSample = 0;
   558    544     int i;
   559    545   
   560    546     assert( IsStat4 || nEqZero==0 );
   561    547   
   562         -#ifdef SQLITE_ENABLE_STAT4
   563    548     /* Stat4Accum.nMaxEqZero is set to the maximum number of leading 0
   564    549     ** values in the anEq[] array of any sample in Stat4Accum.a[]. In
   565    550     ** other words, if nMaxEqZero is n, then it is guaranteed that there
   566    551     ** are no samples with Stat4Sample.anEq[m]==0 for (m>=n). */
   567    552     if( nEqZero>p->nMaxEqZero ){
   568    553       p->nMaxEqZero = nEqZero;
   569    554     }
................................................................................
   589    574       }
   590    575       if( pUpgrade ){
   591    576         pUpgrade->iCol = pNew->iCol;
   592    577         pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
   593    578         goto find_new_min;
   594    579       }
   595    580     }
   596         -#endif
   597    581   
   598    582     /* If necessary, remove sample iMin to make room for the new sample. */
   599    583     if( p->nSample>=p->mxSample ){
   600    584       Stat4Sample *pMin = &p->a[p->iMin];
   601    585       tRowcnt *anEq = pMin->anEq;
   602    586       tRowcnt *anLt = pMin->anLt;
   603    587       tRowcnt *anDLt = pMin->anDLt;
................................................................................
   610    594       pSample->anLt = anLt;
   611    595       p->nSample = p->mxSample-1;
   612    596     }
   613    597   
   614    598     /* The "rows less-than" for the rowid column must be greater than that
   615    599     ** for the last sample in the p->a[] array. Otherwise, the samples would
   616    600     ** be out of order. */
   617         -#ifdef SQLITE_ENABLE_STAT4
   618    601     assert( p->nSample==0 
   619    602          || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
   620         -#endif
   621    603   
   622    604     /* Insert the new sample */
   623    605     pSample = &p->a[p->nSample];
   624    606     sampleCopy(p, pSample, pNew);
   625    607     p->nSample++;
   626    608   
   627    609     /* Zero the first nEqZero entries in the anEq[] array. */
   628    610     memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
   629    611   
   630         -#ifdef SQLITE_ENABLE_STAT4
   631         - find_new_min:
   632         -#endif
          612  +find_new_min:
   633    613     if( p->nSample>=p->mxSample ){
   634    614       int iMin = -1;
   635    615       for(i=0; i<p->mxSample; i++){
   636    616         if( p->a[i].isPSample ) continue;
   637    617         if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
   638    618           iMin = i;
   639    619         }
   640    620       }
   641    621       assert( iMin>=0 );
   642    622       p->iMin = iMin;
   643    623     }
   644    624   }
   645         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
          625  +#endif /* SQLITE_ENABLE_STAT4 */
   646    626   
   647    627   /*
   648    628   ** Field iChng of the index being scanned has changed. So at this point
   649    629   ** p->current contains a sample that reflects the previous row of the
   650    630   ** index. The value of anEq[iChng] and subsequent anEq[] elements are
   651    631   ** correct at this point.
   652    632   */
................................................................................
   679    659           if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
   680    660         }
   681    661       }
   682    662       p->nMaxEqZero = iChng;
   683    663     }
   684    664   #endif
   685    665   
   686         -#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
   687         -  if( iChng==0 ){
   688         -    tRowcnt nLt = p->current.anLt[0];
   689         -    tRowcnt nEq = p->current.anEq[0];
   690         -
   691         -    /* Check if this is to be a periodic sample. If so, add it. */
   692         -    if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
   693         -      p->current.isPSample = 1;
   694         -      sampleInsert(p, &p->current, 0);
   695         -      p->current.isPSample = 0;
   696         -    }else 
   697         -
   698         -    /* Or if it is a non-periodic sample. Add it in this case too. */
   699         -    if( p->nSample<p->mxSample 
   700         -     || sampleIsBetter(p, &p->current, &p->a[p->iMin]) 
   701         -    ){
   702         -      sampleInsert(p, &p->current, 0);
   703         -    }
   704         -  }
   705         -#endif
   706         -
   707         -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
          666  +#ifndef SQLITE_ENABLE_STAT4
   708    667     UNUSED_PARAMETER( p );
   709    668     UNUSED_PARAMETER( iChng );
   710    669   #endif
   711    670   }
   712    671   
   713    672   /*
   714    673   ** Implementation of the stat_push SQL function:  stat_push(P,C,R)
................................................................................
   720    679   **          WITHOUT ROWID tables.
   721    680   **
   722    681   ** This SQL function always returns NULL.  It's purpose it to accumulate
   723    682   ** statistical data and/or samples in the Stat4Accum object about the
   724    683   ** index being analyzed.  The stat_get() SQL function will later be used to
   725    684   ** extract relevant information for constructing the sqlite_statN tables.
   726    685   **
   727         -** The R parameter is only used for STAT3 and STAT4
          686  +** The R parameter is only used for STAT4
   728    687   */
   729    688   static void statPush(
   730    689     sqlite3_context *context,
   731    690     int argc,
   732    691     sqlite3_value **argv
   733    692   ){
   734    693     int i;
................................................................................
   752    711       /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
   753    712       ** to the current row of the index. */
   754    713       for(i=0; i<iChng; i++){
   755    714         p->current.anEq[i]++;
   756    715       }
   757    716       for(i=iChng; i<p->nCol; i++){
   758    717         p->current.anDLt[i]++;
   759         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          718  +#ifdef SQLITE_ENABLE_STAT4
   760    719         p->current.anLt[i] += p->current.anEq[i];
   761    720   #endif
   762    721         p->current.anEq[i] = 1;
   763    722       }
   764    723     }
   765    724     p->nRow++;
   766         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          725  +#ifdef SQLITE_ENABLE_STAT4
   767    726     if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
   768    727       sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
   769    728     }else{
   770    729       sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
   771    730                                          sqlite3_value_blob(argv[2]));
   772    731     }
   773    732     p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
................................................................................
   792    751           sampleCopy(p, &p->aBest[i], &p->current);
   793    752         }
   794    753       }
   795    754     }
   796    755   #endif
   797    756   }
   798    757   static const FuncDef statPushFuncdef = {
   799         -  2+IsStat34,      /* nArg */
          758  +  2+IsStat4,       /* nArg */
   800    759     SQLITE_UTF8,     /* funcFlags */
   801    760     0,               /* pUserData */
   802    761     0,               /* pNext */
   803    762     statPush,        /* xSFunc */
   804    763     0,               /* xFinalize */
   805    764     0, 0,            /* xValue, xInverse */
   806    765     "stat_push",     /* zName */
................................................................................
   823    782   **
   824    783   ** The stat_get(P,J) function is not available to generic SQL.  It is
   825    784   ** inserted as part of a manually constructed bytecode program.  (See
   826    785   ** the callStatGet() routine below.)  It is guaranteed that the P
   827    786   ** parameter will always be a poiner to a Stat4Accum object, never a
   828    787   ** NULL.
   829    788   **
   830         -** If neither STAT3 nor STAT4 are enabled, then J is always
          789  +** If STAT4 is not enabled, then J is always
   831    790   ** STAT_GET_STAT1 and is hence omitted and this routine becomes
   832    791   ** a one-parameter function, stat_get(P), that always returns the
   833    792   ** stat1 table entry information.
   834    793   */
   835    794   static void statGet(
   836    795     sqlite3_context *context,
   837    796     int argc,
   838    797     sqlite3_value **argv
   839    798   ){
   840    799     Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
   841         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   842         -  /* STAT3 and STAT4 have a parameter on this routine. */
          800  +#ifdef SQLITE_ENABLE_STAT4
          801  +  /* STAT4 has a parameter on this routine. */
   843    802     int eCall = sqlite3_value_int(argv[1]);
   844    803     assert( argc==2 );
   845    804     assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ 
   846    805          || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
   847    806          || eCall==STAT_GET_NDLT 
   848    807     );
   849    808     if( eCall==STAT_GET_STAT1 )
................................................................................
   890    849         z += sqlite3Strlen30(z);
   891    850         assert( p->current.anEq[i] );
   892    851       }
   893    852       assert( z[0]=='\0' && z>zRet );
   894    853   
   895    854       sqlite3_result_text(context, zRet, -1, sqlite3_free);
   896    855     }
   897         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          856  +#ifdef SQLITE_ENABLE_STAT4
   898    857     else if( eCall==STAT_GET_ROWID ){
   899    858       if( p->iGet<0 ){
   900    859         samplePushPrevious(p, 0);
   901    860         p->iGet = 0;
   902    861       }
   903    862       if( p->iGet<p->nSample ){
   904    863         Stat4Sample *pS = p->a + p->iGet;
................................................................................
   919    878         default: {
   920    879           aCnt = p->a[p->iGet].anDLt; 
   921    880           p->iGet++;
   922    881           break;
   923    882         }
   924    883       }
   925    884   
   926         -    if( IsStat3 ){
   927         -      sqlite3_result_int64(context, (i64)aCnt[0]);
   928         -    }else{
          885  +    {
   929    886         char *zRet = sqlite3MallocZero(p->nCol * 25);
   930    887         if( zRet==0 ){
   931    888           sqlite3_result_error_nomem(context);
   932    889         }else{
   933    890           int i;
   934    891           char *z = zRet;
   935    892           for(i=0; i<p->nCol; i++){
................................................................................
   938    895           }
   939    896           assert( z[0]=='\0' && z>zRet );
   940    897           z[-1] = '\0';
   941    898           sqlite3_result_text(context, zRet, -1, sqlite3_free);
   942    899         }
   943    900       }
   944    901     }
   945         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
          902  +#endif /* SQLITE_ENABLE_STAT4 */
   946    903   #ifndef SQLITE_DEBUG
   947    904     UNUSED_PARAMETER( argc );
   948    905   #endif
   949    906   }
   950    907   static const FuncDef statGetFuncdef = {
   951         -  1+IsStat34,      /* nArg */
          908  +  1+IsStat4,       /* nArg */
   952    909     SQLITE_UTF8,     /* funcFlags */
   953    910     0,               /* pUserData */
   954    911     0,               /* pNext */
   955    912     statGet,         /* xSFunc */
   956    913     0,               /* xFinalize */
   957    914     0, 0,            /* xValue, xInverse */
   958    915     "stat_get",      /* zName */
   959    916     {0}
   960    917   };
   961    918   
   962    919   static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
   963    920     assert( regOut!=regStat4 && regOut!=regStat4+1 );
   964         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          921  +#ifdef SQLITE_ENABLE_STAT4
   965    922     sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
   966    923   #elif SQLITE_DEBUG
   967    924     assert( iParam==STAT_GET_STAT1 );
   968    925   #else
   969    926     UNUSED_PARAMETER( iParam );
   970    927   #endif
   971    928     sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4, regOut,
   972    929                       (char*)&statGetFuncdef, P4_FUNCDEF);
   973         -  sqlite3VdbeChangeP5(v, 1 + IsStat34);
          930  +  sqlite3VdbeChangeP5(v, 1 + IsStat4);
   974    931   }
   975    932   
   976    933   /*
   977    934   ** Generate code to do an analysis of all indices associated with
   978    935   ** a single table.
   979    936   */
   980    937   static void analyzeOneTable(
................................................................................
   993    950     int i;                       /* Loop counter */
   994    951     int jZeroRows = -1;          /* Jump from here if number of rows is zero */
   995    952     int iDb;                     /* Index of database containing pTab */
   996    953     u8 needTableCnt = 1;         /* True to count the table */
   997    954     int regNewRowid = iMem++;    /* Rowid for the inserted record */
   998    955     int regStat4 = iMem++;       /* Register to hold Stat4Accum object */
   999    956     int regChng = iMem++;        /* Index of changed index field */
  1000         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          957  +#ifdef SQLITE_ENABLE_STAT4
  1001    958     int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
  1002    959   #endif
  1003    960     int regTemp = iMem++;        /* Temporary use register */
  1004    961     int regTabname = iMem++;     /* Register containing table name */
  1005    962     int regIdxname = iMem++;     /* Register containing index name */
  1006    963     int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
  1007    964     int regPrev = iMem;          /* MUST BE LAST (see below) */
................................................................................
  1127   1084       ** 
  1128   1085       **    (1) the number of columns in the index including the rowid
  1129   1086       **        (or for a WITHOUT ROWID table, the number of PK columns),
  1130   1087       **    (2) the number of columns in the key without the rowid/pk
  1131   1088       **    (3) the number of rows in the index,
  1132   1089       **
  1133   1090       **
  1134         -    ** The third argument is only used for STAT3 and STAT4
         1091  +    ** The third argument is only used for STAT4
  1135   1092       */
  1136         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1093  +#ifdef SQLITE_ENABLE_STAT4
  1137   1094       sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+3);
  1138   1095   #endif
  1139   1096       sqlite3VdbeAddOp2(v, OP_Integer, nCol, regStat4+1);
  1140   1097       sqlite3VdbeAddOp2(v, OP_Integer, pIdx->nKeyCol, regStat4+2);
  1141   1098       sqlite3VdbeAddOp4(v, OP_Function0, 0, regStat4+1, regStat4,
  1142   1099                        (char*)&statInitFuncdef, P4_FUNCDEF);
  1143         -    sqlite3VdbeChangeP5(v, 2+IsStat34);
         1100  +    sqlite3VdbeChangeP5(v, 2+IsStat4);
  1144   1101   
  1145   1102       /* Implementation of the following:
  1146   1103       **
  1147   1104       **   Rewind csr
  1148   1105       **   if eof(csr) goto end_of_scan;
  1149   1106       **   regChng = 0
  1150   1107       **   goto next_push_0;
................................................................................
  1207   1164         }
  1208   1165         sqlite3VdbeResolveLabel(v, endDistinctTest);
  1209   1166         sqlite3DbFree(db, aGotoChng);
  1210   1167       }
  1211   1168     
  1212   1169       /*
  1213   1170       **  chng_addr_N:
  1214         -    **   regRowid = idx(rowid)            // STAT34 only
  1215         -    **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
         1171  +    **   regRowid = idx(rowid)            // STAT4 only
         1172  +    **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT4 only
  1216   1173       **   Next csr
  1217   1174       **   if !eof(csr) goto next_row;
  1218   1175       */
  1219         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1176  +#ifdef SQLITE_ENABLE_STAT4
  1220   1177       assert( regRowid==(regStat4+2) );
  1221   1178       if( HasRowid(pTab) ){
  1222   1179         sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
  1223   1180       }else{
  1224   1181         Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
  1225   1182         int j, k, regKey;
  1226   1183         regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
................................................................................
  1233   1190         sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
  1234   1191         sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
  1235   1192       }
  1236   1193   #endif
  1237   1194       assert( regChng==(regStat4+1) );
  1238   1195       sqlite3VdbeAddOp4(v, OP_Function0, 1, regStat4, regTemp,
  1239   1196                        (char*)&statPushFuncdef, P4_FUNCDEF);
  1240         -    sqlite3VdbeChangeP5(v, 2+IsStat34);
         1197  +    sqlite3VdbeChangeP5(v, 2+IsStat4);
  1241   1198       sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
  1242   1199   
  1243   1200       /* Add the entry to the stat1 table. */
  1244   1201       callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
  1245   1202       assert( "BBB"[0]==SQLITE_AFF_TEXT );
  1246   1203       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "BBB", 0);
  1247   1204       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
  1248   1205       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
  1249   1206   #ifdef SQLITE_ENABLE_PREUPDATE_HOOK
  1250   1207       sqlite3VdbeChangeP4(v, -1, (char*)pStat1, P4_TABLE);
  1251   1208   #endif
  1252   1209       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
  1253   1210   
  1254         -    /* Add the entries to the stat3 or stat4 table. */
  1255         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1211  +    /* Add the entries to the stat4 table. */
         1212  +#ifdef SQLITE_ENABLE_STAT4
  1256   1213       {
  1257   1214         int regEq = regStat1;
  1258   1215         int regLt = regStat1+1;
  1259   1216         int regDLt = regStat1+2;
  1260   1217         int regSample = regStat1+3;
  1261   1218         int regCol = regStat1+4;
  1262   1219         int regSampleRowid = regCol + nCol;
................................................................................
  1271   1228         addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
  1272   1229         VdbeCoverage(v);
  1273   1230         callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
  1274   1231         callStatGet(v, regStat4, STAT_GET_NLT, regLt);
  1275   1232         callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
  1276   1233         sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
  1277   1234         VdbeCoverage(v);
  1278         -#ifdef SQLITE_ENABLE_STAT3
  1279         -      sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, 0, regSample);
  1280         -#else
  1281   1235         for(i=0; i<nCol; i++){
  1282   1236           sqlite3ExprCodeLoadIndexColumn(pParse, pIdx, iTabCur, i, regCol+i);
  1283   1237         }
  1284   1238         sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol, regSample);
  1285         -#endif
  1286   1239         sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
  1287   1240         sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
  1288   1241         sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
  1289   1242         sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
  1290   1243         sqlite3VdbeJumpHere(v, addrIsNull);
  1291   1244       }
  1292         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1245  +#endif /* SQLITE_ENABLE_STAT4 */
  1293   1246   
  1294   1247       /* End of analysis */
  1295   1248       sqlite3VdbeJumpHere(v, addrRewind);
  1296   1249     }
  1297   1250   
  1298   1251   
  1299   1252     /* Create a single sqlite_stat1 entry containing NULL as the index
................................................................................
  1460   1413     Index *pIndex          /* Handle extra flags for this index, if not NULL */
  1461   1414   ){
  1462   1415     char *z = zIntArray;
  1463   1416     int c;
  1464   1417     int i;
  1465   1418     tRowcnt v;
  1466   1419   
  1467         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1420  +#ifdef SQLITE_ENABLE_STAT4
  1468   1421     if( z==0 ) z = "";
  1469   1422   #else
  1470   1423     assert( z!=0 );
  1471   1424   #endif
  1472   1425     for(i=0; *z && i<nOut; i++){
  1473   1426       v = 0;
  1474   1427       while( (c=z[0])>='0' && c<='9' ){
  1475   1428         v = v*10 + c - '0';
  1476   1429         z++;
  1477   1430       }
  1478         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1431  +#ifdef SQLITE_ENABLE_STAT4
  1479   1432       if( aOut ) aOut[i] = v;
  1480   1433       if( aLog ) aLog[i] = sqlite3LogEst(v);
  1481   1434   #else
  1482   1435       assert( aOut==0 );
  1483   1436       UNUSED_PARAMETER(aOut);
  1484   1437       assert( aLog!=0 );
  1485   1438       aLog[i] = sqlite3LogEst(v);
  1486   1439   #endif
  1487   1440       if( *z==' ' ) z++;
  1488   1441     }
  1489         -#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
         1442  +#ifndef SQLITE_ENABLE_STAT4
  1490   1443     assert( pIndex!=0 ); {
  1491   1444   #else
  1492   1445     if( pIndex ){
  1493   1446   #endif
  1494   1447       pIndex->bUnordered = 0;
  1495   1448       pIndex->noSkipScan = 0;
  1496   1449       while( z[0] ){
  1497   1450         if( sqlite3_strglob("unordered*", z)==0 ){
  1498   1451           pIndex->bUnordered = 1;
  1499   1452         }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
  1500         -        pIndex->szIdxRow = sqlite3LogEst(sqlite3Atoi(z+3));
         1453  +        int sz = sqlite3Atoi(z+3);
         1454  +        if( sz<2 ) sz = 2;
         1455  +        pIndex->szIdxRow = sqlite3LogEst(sz);
  1501   1456         }else if( sqlite3_strglob("noskipscan*", z)==0 ){
  1502   1457           pIndex->noSkipScan = 1;
  1503   1458         }
  1504   1459   #ifdef SQLITE_ENABLE_COSTMULT
  1505   1460         else if( sqlite3_strglob("costmult=[0-9]*",z)==0 ){
  1506   1461           pIndex->pTable->costMult = sqlite3LogEst(sqlite3Atoi(z+9));
  1507   1462         }
................................................................................
  1547   1502       pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
  1548   1503     }
  1549   1504     z = argv[2];
  1550   1505   
  1551   1506     if( pIndex ){
  1552   1507       tRowcnt *aiRowEst = 0;
  1553   1508       int nCol = pIndex->nKeyCol+1;
  1554         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1509  +#ifdef SQLITE_ENABLE_STAT4
  1555   1510       /* Index.aiRowEst may already be set here if there are duplicate 
  1556   1511       ** sqlite_stat1 entries for this index. In that case just clobber
  1557   1512       ** the old data with the new instead of allocating a new array.  */
  1558   1513       if( pIndex->aiRowEst==0 ){
  1559   1514         pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
  1560   1515         if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
  1561   1516       }
................................................................................
  1583   1538   }
  1584   1539   
  1585   1540   /*
  1586   1541   ** If the Index.aSample variable is not NULL, delete the aSample[] array
  1587   1542   ** and its contents.
  1588   1543   */
  1589   1544   void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
  1590         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1545  +#ifdef SQLITE_ENABLE_STAT4
  1591   1546     if( pIdx->aSample ){
  1592   1547       int j;
  1593   1548       for(j=0; j<pIdx->nSample; j++){
  1594   1549         IndexSample *p = &pIdx->aSample[j];
  1595   1550         sqlite3DbFree(db, p->p);
  1596   1551       }
  1597   1552       sqlite3DbFree(db, pIdx->aSample);
................................................................................
  1599   1554     if( db && db->pnBytesFreed==0 ){
  1600   1555       pIdx->nSample = 0;
  1601   1556       pIdx->aSample = 0;
  1602   1557     }
  1603   1558   #else
  1604   1559     UNUSED_PARAMETER(db);
  1605   1560     UNUSED_PARAMETER(pIdx);
  1606         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1561  +#endif /* SQLITE_ENABLE_STAT4 */
  1607   1562   }
  1608   1563   
  1609         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1564  +#ifdef SQLITE_ENABLE_STAT4
  1610   1565   /*
  1611   1566   ** Populate the pIdx->aAvgEq[] array based on the samples currently
  1612   1567   ** stored in pIdx->aSample[]. 
  1613   1568   */
  1614   1569   static void initAvgEq(Index *pIdx){
  1615   1570     if( pIdx ){
  1616   1571       IndexSample *aSample = pIdx->aSample;
................................................................................
  1680   1635       Table *pTab = sqlite3FindTable(db, zName, zDb);
  1681   1636       if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
  1682   1637     }
  1683   1638     return pIdx;
  1684   1639   }
  1685   1640   
  1686   1641   /*
  1687         -** Load the content from either the sqlite_stat4 or sqlite_stat3 table 
         1642  +** Load the content from either the sqlite_stat4
  1688   1643   ** into the relevant Index.aSample[] arrays.
  1689   1644   **
  1690   1645   ** Arguments zSql1 and zSql2 must point to SQL statements that return
  1691         -** data equivalent to the following (statements are different for stat3,
  1692         -** see the caller of this function for details):
         1646  +** data equivalent to the following:
  1693   1647   **
  1694   1648   **    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
  1695   1649   **    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
  1696   1650   **
  1697   1651   ** where %Q is replaced with the database name before the SQL is executed.
  1698   1652   */
  1699   1653   static int loadStatTbl(
  1700   1654     sqlite3 *db,                  /* Database handle */
  1701         -  int bStat3,                   /* Assume single column records only */
  1702   1655     const char *zSql1,            /* SQL statement 1 (see above) */
  1703   1656     const char *zSql2,            /* SQL statement 2 (see above) */
  1704   1657     const char *zDb               /* Database name (e.g. "main") */
  1705   1658   ){
  1706   1659     int rc;                       /* Result codes from subroutines */
  1707   1660     sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
  1708   1661     char *zSql;                   /* Text of the SQL statement */
................................................................................
  1728   1681       int i;          /* Bytes of space required */
  1729   1682       tRowcnt *pSpace;
  1730   1683   
  1731   1684       zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1732   1685       if( zIndex==0 ) continue;
  1733   1686       nSample = sqlite3_column_int(pStmt, 1);
  1734   1687       pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
  1735         -    assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
  1736         -    /* Index.nSample is non-zero at this point if data has already been
  1737         -    ** loaded from the stat4 table. In this case ignore stat3 data.  */
  1738         -    if( pIdx==0 || pIdx->nSample ) continue;
  1739         -    if( bStat3==0 ){
  1740         -      assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
  1741         -      if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
  1742         -        nIdxCol = pIdx->nKeyCol;
  1743         -      }else{
  1744         -        nIdxCol = pIdx->nColumn;
  1745         -      }
         1688  +    assert( pIdx==0 || pIdx->nSample==0 );
         1689  +    if( pIdx==0 ) continue;
         1690  +    assert( !HasRowid(pIdx->pTable) || pIdx->nColumn==pIdx->nKeyCol+1 );
         1691  +    if( !HasRowid(pIdx->pTable) && IsPrimaryKeyIndex(pIdx) ){
         1692  +      nIdxCol = pIdx->nKeyCol;
         1693  +    }else{
         1694  +      nIdxCol = pIdx->nColumn;
  1746   1695       }
  1747   1696       pIdx->nSampleCol = nIdxCol;
  1748   1697       nByte = sizeof(IndexSample) * nSample;
  1749   1698       nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
  1750   1699       nByte += nIdxCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
  1751   1700   
  1752   1701       pIdx->aSample = sqlite3DbMallocZero(db, nByte);
................................................................................
  1780   1729       int nCol = 1;                 /* Number of columns in index */
  1781   1730   
  1782   1731       zIndex = (char *)sqlite3_column_text(pStmt, 0);
  1783   1732       if( zIndex==0 ) continue;
  1784   1733       pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
  1785   1734       if( pIdx==0 ) continue;
  1786   1735       /* This next condition is true if data has already been loaded from 
  1787         -    ** the sqlite_stat4 table. In this case ignore stat3 data.  */
         1736  +    ** the sqlite_stat4 table. */
  1788   1737       nCol = pIdx->nSampleCol;
  1789         -    if( bStat3 && nCol>1 ) continue;
  1790   1738       if( pIdx!=pPrevIdx ){
  1791   1739         initAvgEq(pPrevIdx);
  1792   1740         pPrevIdx = pIdx;
  1793   1741       }
  1794   1742       pSample = &pIdx->aSample[pIdx->nSample];
  1795   1743       decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
  1796   1744       decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
................................................................................
  1815   1763     }
  1816   1764     rc = sqlite3_finalize(pStmt);
  1817   1765     if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
  1818   1766     return rc;
  1819   1767   }
  1820   1768   
  1821   1769   /*
  1822         -** Load content from the sqlite_stat4 and sqlite_stat3 tables into 
         1770  +** Load content from the sqlite_stat4 table into 
  1823   1771   ** the Index.aSample[] arrays of all indices.
  1824   1772   */
  1825   1773   static int loadStat4(sqlite3 *db, const char *zDb){
  1826   1774     int rc = SQLITE_OK;             /* Result codes from subroutines */
  1827   1775   
  1828   1776     assert( db->lookaside.bDisable );
  1829   1777     if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
  1830         -    rc = loadStatTbl(db, 0,
         1778  +    rc = loadStatTbl(db,
  1831   1779         "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx", 
  1832   1780         "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
  1833   1781         zDb
  1834   1782       );
  1835   1783     }
  1836         -
  1837         -  if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
  1838         -    rc = loadStatTbl(db, 1,
  1839         -      "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx", 
  1840         -      "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
  1841         -      zDb
  1842         -    );
  1843         -  }
  1844         -
  1845   1784     return rc;
  1846   1785   }
  1847         -#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
         1786  +#endif /* SQLITE_ENABLE_STAT4 */
  1848   1787   
  1849   1788   /*
  1850         -** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
         1789  +** Load the content of the sqlite_stat1 and sqlite_stat4 tables. The
  1851   1790   ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
  1852         -** arrays. The contents of sqlite_stat3/4 are used to populate the
         1791  +** arrays. The contents of sqlite_stat4 are used to populate the
  1853   1792   ** Index.aSample[] arrays.
  1854   1793   **
  1855   1794   ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
  1856         -** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined 
  1857         -** during compilation and the sqlite_stat3/4 table is present, no data is 
         1795  +** is returned. In this case, even if SQLITE_ENABLE_STAT4 was defined 
         1796  +** during compilation and the sqlite_stat4 table is present, no data is 
  1858   1797   ** read from it.
  1859   1798   **
  1860         -** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the 
         1799  +** If SQLITE_ENABLE_STAT4 was defined during compilation and the 
  1861   1800   ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
  1862   1801   ** returned. However, in this case, data is read from the sqlite_stat1
  1863   1802   ** table (if it is present) before returning.
  1864   1803   **
  1865   1804   ** If an OOM error occurs, this function always sets db->mallocFailed.
  1866   1805   ** This means if the caller does not care about other errors, the return
  1867   1806   ** code may be ignored.
................................................................................
  1881   1820     for(i=sqliteHashFirst(&pSchema->tblHash); i; i=sqliteHashNext(i)){
  1882   1821       Table *pTab = sqliteHashData(i);
  1883   1822       pTab->tabFlags &= ~TF_HasStat1;
  1884   1823     }
  1885   1824     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1886   1825       Index *pIdx = sqliteHashData(i);
  1887   1826       pIdx->hasStat1 = 0;
  1888         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1827  +#ifdef SQLITE_ENABLE_STAT4
  1889   1828       sqlite3DeleteIndexSamples(db, pIdx);
  1890   1829       pIdx->aSample = 0;
  1891   1830   #endif
  1892   1831     }
  1893   1832   
  1894   1833     /* Load new statistics out of the sqlite_stat1 table */
  1895   1834     sInfo.db = db;
................................................................................
  1909   1848     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
  1910   1849     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1911   1850       Index *pIdx = sqliteHashData(i);
  1912   1851       if( !pIdx->hasStat1 ) sqlite3DefaultRowEst(pIdx);
  1913   1852     }
  1914   1853   
  1915   1854     /* Load the statistics from the sqlite_stat4 table. */
  1916         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         1855  +#ifdef SQLITE_ENABLE_STAT4
  1917   1856     if( rc==SQLITE_OK ){
  1918   1857       db->lookaside.bDisable++;
  1919   1858       rc = loadStat4(db, sInfo.zDatabase);
  1920   1859       db->lookaside.bDisable--;
  1921   1860     }
  1922   1861     for(i=sqliteHashFirst(&pSchema->idxHash); i; i=sqliteHashNext(i)){
  1923   1862       Index *pIdx = sqliteHashData(i);

Changes to src/attach.c.

   295    295     int NotUsed,
   296    296     sqlite3_value **argv
   297    297   ){
   298    298     const char *zName = (const char *)sqlite3_value_text(argv[0]);
   299    299     sqlite3 *db = sqlite3_context_db_handle(context);
   300    300     int i;
   301    301     Db *pDb = 0;
          302  +  HashElem *pEntry;
   302    303     char zErr[128];
   303    304   
   304    305     UNUSED_PARAMETER(NotUsed);
   305    306   
   306    307     if( zName==0 ) zName = "";
   307    308     for(i=0; i<db->nDb; i++){
   308    309       pDb = &db->aDb[i];
................................................................................
   318    319       sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
   319    320       goto detach_error;
   320    321     }
   321    322     if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
   322    323       sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
   323    324       goto detach_error;
   324    325     }
          326  +
          327  +  /* If any TEMP triggers reference the schema being detached, move those
          328  +  ** triggers to reference the TEMP schema itself. */
          329  +  assert( db->aDb[1].pSchema );
          330  +  pEntry = sqliteHashFirst(&db->aDb[1].pSchema->trigHash);
          331  +  while( pEntry ){
          332  +    Trigger *pTrig = (Trigger*)sqliteHashData(pEntry);
          333  +    if( pTrig->pTabSchema==pDb->pSchema ){
          334  +      pTrig->pTabSchema = pTrig->pSchema;
          335  +    }
          336  +    pEntry = sqliteHashNext(pEntry);
          337  +  }
   325    338   
   326    339     sqlite3BtreeClose(pDb->pBt);
   327    340     pDb->pBt = 0;
   328    341     pDb->pSchema = 0;
   329    342     sqlite3CollapseDatabaseArray(db);
   330    343     return;
   331    344   
................................................................................
   556    569     return 0;
   557    570   }
   558    571   int sqlite3FixExpr(
   559    572     DbFixer *pFix,     /* Context of the fixation */
   560    573     Expr *pExpr        /* The expression to be fixed to one database */
   561    574   ){
   562    575     while( pExpr ){
          576  +    ExprSetProperty(pExpr, EP_Indirect);
   563    577       if( pExpr->op==TK_VARIABLE ){
   564    578         if( pFix->pParse->db->init.busy ){
   565    579           pExpr->op = TK_NULL;
   566    580         }else{
   567    581           sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
   568    582           return 1;
   569    583         }

Changes to src/auth.c.

    74     74   ){
    75     75   #ifdef SQLITE_ENABLE_API_ARMOR
    76     76     if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
    77     77   #endif
    78     78     sqlite3_mutex_enter(db->mutex);
    79     79     db->xAuth = (sqlite3_xauth)xAuth;
    80     80     db->pAuthArg = pArg;
    81         -  sqlite3ExpirePreparedStatements(db, 0);
           81  +  if( db->xAuth ) sqlite3ExpirePreparedStatements(db, 1);
    82     82     sqlite3_mutex_leave(db->mutex);
    83     83     return SQLITE_OK;
    84     84   }
    85     85   
    86     86   /*
    87     87   ** Write an error message into pParse->zErrMsg that explains that the
    88     88   ** user-supplied authorization function returned an illegal value.

Changes to src/backup.c.

   615    615   
   616    616     /* Detach this backup from the source pager. */
   617    617     if( p->pDestDb ){
   618    618       p->pSrc->nBackup--;
   619    619     }
   620    620     if( p->isAttached ){
   621    621       pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
          622  +    assert( pp!=0 );
   622    623       while( *pp!=p ){
   623    624         pp = &(*pp)->pNext;
          625  +      assert( pp!=0 );
   624    626       }
   625    627       *pp = p->pNext;
   626    628     }
   627    629   
   628    630     /* If a transaction is still open on the Btree, roll it back. */
   629    631     sqlite3BtreeRollback(p->pDest, SQLITE_OK, 0);
   630    632   

Changes to src/btree.c.

  1883   1883     */
  1884   1884     testcase( gap+2==top );
  1885   1885     testcase( gap+1==top );
  1886   1886     testcase( gap==top );
  1887   1887     if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
  1888   1888       u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
  1889   1889       if( pSpace ){
  1890         -      assert( pSpace>=data && (pSpace - data)<65536 );
  1891         -      *pIdx = (int)(pSpace - data);
  1892         -      return SQLITE_OK;
         1890  +      assert( pSpace+nByte<=data+pPage->pBt->usableSize );
         1891  +      if( (*pIdx = (int)(pSpace-data))<=gap ){
         1892  +        return SQLITE_CORRUPT_PAGE(pPage);
         1893  +      }else{
         1894  +        return SQLITE_OK;
         1895  +      }
  1893   1896       }else if( rc ){
  1894   1897         return rc;
  1895   1898       }
  1896   1899     }
  1897   1900   
  1898   1901     /* The request could not be fulfilled using a freelist slot.  Check
  1899   1902     ** to see if defragmentation is necessary.
................................................................................
  5323   5326           ){
  5324   5327             sqlite3_file *fd = sqlite3PagerFile(pBt->pPager);
  5325   5328             u8 aSave[4];
  5326   5329             u8 *aWrite = &pBuf[-4];
  5327   5330             assert( aWrite>=pBufStart );                         /* due to (6) */
  5328   5331             memcpy(aSave, aWrite, 4);
  5329   5332             rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
         5333  +          if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT;
  5330   5334             nextPage = get4byte(aWrite);
  5331   5335             memcpy(aWrite, aSave, 4);
  5332   5336           }else
  5333   5337   #endif
  5334   5338   
  5335   5339           {
  5336   5340             DbPage *pDbPage;
................................................................................
  7127   7131     assert( *pRC==SQLITE_OK );
  7128   7132     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  7129   7133     assert( MX_CELL(pPage->pBt)<=10921 );
  7130   7134     assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
  7131   7135     assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
  7132   7136     assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
  7133   7137     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  7134         -  /* The cell should normally be sized correctly.  However, when moving a
  7135         -  ** malformed cell from a leaf page to an interior page, if the cell size
  7136         -  ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
  7137         -  ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
  7138         -  ** the term after the || in the following assert(). */
  7139         -  assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
         7138  +  assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
  7140   7139     assert( pPage->nFree>=0 );
  7141   7140     if( pPage->nOverflow || sz+2>pPage->nFree ){
  7142   7141       if( pTemp ){
  7143   7142         memcpy(pTemp, pCell, sz);
  7144   7143         pCell = pTemp;
  7145   7144       }
  7146   7145       if( iChild ){
................................................................................
  7363   7362     u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
  7364   7363     u8 *pData;
  7365   7364     int k;                          /* Current slot in pCArray->apEnd[] */
  7366   7365     u8 *pSrcEnd;                    /* Current pCArray->apEnd[k] value */
  7367   7366   
  7368   7367     assert( i<iEnd );
  7369   7368     j = get2byte(&aData[hdr+5]);
  7370         -  if( NEVER(j>(u32)usableSize) ){ j = 0; }
         7369  +  if( j>(u32)usableSize ){ j = 0; }
  7371   7370     memcpy(&pTmp[j], &aData[j], usableSize - j);
  7372   7371   
  7373   7372     for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
  7374   7373     pSrcEnd = pCArray->apEnd[k];
  7375   7374   
  7376   7375     pData = pEnd;
  7377   7376     while( 1/*exit by break*/ ){
................................................................................
  7455   7454     assert( CORRUPT_DB || pPg->hdrOffset==0 );    /* Never called on page 1 */
  7456   7455     if( iEnd<=iFirst ) return 0;
  7457   7456     for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){}
  7458   7457     pEnd = pCArray->apEnd[k];
  7459   7458     while( 1 /*Exit by break*/ ){
  7460   7459       int sz, rc;
  7461   7460       u8 *pSlot;
  7462         -    sz = cachedCellSize(pCArray, i);
         7461  +    assert( pCArray->szCell[i]!=0 );
         7462  +    sz = pCArray->szCell[i];
  7463   7463       if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
  7464   7464         if( (pData - pBegin)<sz ) return 1;
  7465   7465         pData -= sz;
  7466   7466         pSlot = pData;
  7467   7467       }
  7468   7468       /* pSlot and pCArray->apCell[i] will never overlap on a well-formed
  7469   7469       ** database.  But they might for a corrupt database.  Hence use memmove()
................................................................................
  7616   7616       int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
  7617   7617       if( iCell>=0 && iCell<nNew ){
  7618   7618         pCellptr = &pPg->aCellIdx[iCell * 2];
  7619   7619         if( nCell>iCell ){
  7620   7620           memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
  7621   7621         }
  7622   7622         nCell++;
         7623  +      cachedCellSize(pCArray, iCell+iNew);
  7623   7624         if( pageInsertArray(
  7624   7625               pPg, pBegin, &pData, pCellptr,
  7625   7626               iCell+iNew, 1, pCArray
  7626   7627         ) ) goto editpage_fail;
  7627   7628       }
  7628   7629     }
  7629   7630   
................................................................................
  8140   8141       ** This must be done in advance.  Once the balance starts, the cell
  8141   8142       ** offset section of the btree page will be overwritten and we will no
  8142   8143       ** long be able to find the cells if a pointer to each cell is not saved
  8143   8144       ** first.
  8144   8145       */
  8145   8146       memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
  8146   8147       if( pOld->nOverflow>0 ){
  8147         -      if( limit<pOld->aiOvfl[0] ){
         8148  +      if( NEVER(limit<pOld->aiOvfl[0]) ){
  8148   8149           rc = SQLITE_CORRUPT_BKPT;
  8149   8150           goto balance_cleanup;
  8150   8151         }
  8151   8152         limit = pOld->aiOvfl[0];
  8152   8153         for(j=0; j<limit; j++){
  8153   8154           b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell));
  8154   8155           piCell += 2;
................................................................................
  8426   8427       nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
  8427   8428       nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
  8428   8429       nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
  8429   8430       nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
  8430   8431     ));
  8431   8432   
  8432   8433     assert( sqlite3PagerIswriteable(pParent->pDbPage) );
         8434  +  assert( nNew>=1 && nNew<=ArraySize(apNew) );
         8435  +  assert( apNew[nNew-1]!=0 );
  8433   8436     put4byte(pRight, apNew[nNew-1]->pgno);
  8434   8437   
  8435   8438     /* If the sibling pages are not leaves, ensure that the right-child pointer
  8436   8439     ** of the right-most new sibling page is set to the value that was 
  8437   8440     ** originally in the same field of the right-most old sibling page. */
  8438   8441     if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
  8439   8442       MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
................................................................................
  8771   8774     u8 aBalanceQuickSpace[13];
  8772   8775     u8 *pFree = 0;
  8773   8776   
  8774   8777     VVA_ONLY( int balance_quick_called = 0 );
  8775   8778     VVA_ONLY( int balance_deeper_called = 0 );
  8776   8779   
  8777   8780     do {
  8778         -    int iPage = pCur->iPage;
         8781  +    int iPage;
  8779   8782       MemPage *pPage = pCur->pPage;
  8780   8783   
  8781   8784       if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
  8782         -    if( iPage==0 ){
         8785  +    if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
         8786  +      break;
         8787  +    }else if( (iPage = pCur->iPage)==0 ){
  8783   8788         if( pPage->nOverflow ){
  8784   8789           /* The root page of the b-tree is overfull. In this case call the
  8785   8790           ** balance_deeper() function to create a new child for the root-page
  8786   8791           ** and copy the current contents of the root-page to it. The
  8787   8792           ** next iteration of the do-loop will balance the child page.
  8788   8793           */ 
  8789   8794           assert( balance_deeper_called==0 );
................................................................................
  8796   8801             pCur->apPage[0] = pPage;
  8797   8802             pCur->pPage = pCur->apPage[1];
  8798   8803             assert( pCur->pPage->nOverflow );
  8799   8804           }
  8800   8805         }else{
  8801   8806           break;
  8802   8807         }
  8803         -    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
  8804         -      break;
  8805   8808       }else{
  8806   8809         MemPage * const pParent = pCur->apPage[iPage-1];
  8807   8810         int const iIdx = pCur->aiIdx[iPage-1];
  8808   8811   
  8809   8812         rc = sqlite3PagerWrite(pParent->pDbPage);
  8810   8813         if( rc==SQLITE_OK && pParent->nFree<0 ){
  8811   8814           rc = btreeComputeFreeSpace(pParent);
................................................................................
  8939   8942     int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */
  8940   8943     int rc;                             /* Return code */
  8941   8944     MemPage *pPage = pCur->pPage;       /* Page being written */
  8942   8945     BtShared *pBt;                      /* Btree */
  8943   8946     Pgno ovflPgno;                      /* Next overflow page to write */
  8944   8947     u32 ovflPageSize;                   /* Size to write on overflow page */
  8945   8948   
  8946         -  if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
         8949  +  if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd
         8950  +   || pCur->info.pPayload < pPage->aData + pPage->cellOffset
         8951  +  ){
  8947   8952       return SQLITE_CORRUPT_BKPT;
  8948   8953     }
  8949   8954     /* Overwrite the local portion first */
  8950   8955     rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
  8951   8956                                0, pCur->info.nLocal);
  8952   8957     if( rc ) return rc;
  8953   8958     if( pCur->info.nLocal==nTotal ) return SQLITE_OK;
................................................................................
  9180   9185         goto end_insert;
  9181   9186       }
  9182   9187       oldCell = findCell(pPage, idx);
  9183   9188       if( !pPage->leaf ){
  9184   9189         memcpy(newCell, oldCell, 4);
  9185   9190       }
  9186   9191       rc = clearCell(pPage, oldCell, &info);
         9192  +    testcase( pCur->curFlags & BTCF_ValidOvfl );
         9193  +    invalidateOverflowCache(pCur);
  9187   9194       if( info.nSize==szNew && info.nLocal==info.nPayload 
  9188   9195        && (!REQUIRE_PTRMAP || szNew<pPage->minLocal)
  9189   9196       ){
  9190   9197         /* Overwrite the old cell with the new if they are the same size.
  9191   9198         ** We could also try to do this if the old cell is smaller, then add
  9192   9199         ** the leftover space to the free list.  But experiments show that
  9193   9200         ** doing that is no faster then skipping this optimization and just
  9194   9201         ** calling dropCell() and insertCell(). 
  9195   9202         **
  9196   9203         ** This optimization cannot be used on an autovacuum database if the
  9197   9204         ** new entry uses overflow pages, as the insertCell() call below is
  9198   9205         ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry.  */
  9199   9206         assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
  9200         -      if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
         9207  +      if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
         9208  +        return SQLITE_CORRUPT_BKPT;
         9209  +      }
         9210  +      if( oldCell+szNew > pPage->aDataEnd ){
         9211  +        return SQLITE_CORRUPT_BKPT;
         9212  +      }
  9201   9213         memcpy(oldCell, newCell, szNew);
  9202   9214         return SQLITE_OK;
  9203   9215       }
  9204   9216       dropCell(pPage, idx, info.nSize, &rc);
  9205   9217       if( rc ) goto end_insert;
  9206   9218     }else if( loc<0 && pPage->nCell>0 ){
  9207   9219       assert( pPage->leaf );

Changes to src/build.c.

   454    454   #ifndef SQLITE_OMIT_ANALYZE
   455    455     sqlite3DeleteIndexSamples(db, p);
   456    456   #endif
   457    457     sqlite3ExprDelete(db, p->pPartIdxWhere);
   458    458     sqlite3ExprListDelete(db, p->aColExpr);
   459    459     sqlite3DbFree(db, p->zColAff);
   460    460     if( p->isResized ) sqlite3DbFree(db, (void *)p->azColl);
   461         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
          461  +#ifdef SQLITE_ENABLE_STAT4
   462    462     sqlite3_free(p->aiRowEst);
   463    463   #endif
   464    464     sqlite3DbFree(db, p);
   465    465   }
   466    466   
   467    467   /*
   468    468   ** For the index called zIdxName which is found in the database iDb,
................................................................................
   827    827   
   828    828   /*
   829    829   ** This routine is used to check if the UTF-8 string zName is a legal
   830    830   ** unqualified name for a new schema object (table, index, view or
   831    831   ** trigger). All names are legal except those that begin with the string
   832    832   ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
   833    833   ** is reserved for internal use.
          834  +**
          835  +** When parsing the sqlite_master table, this routine also checks to
          836  +** make sure the "type", "name", and "tbl_name" columns are consistent
          837  +** with the SQL.
   834    838   */
   835         -int sqlite3CheckObjectName(Parse *pParse, const char *zName){
   836         -  if( !pParse->db->init.busy && pParse->nested==0 
   837         -          && sqlite3WritableSchema(pParse->db)==0
   838         -          && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
   839         -    sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
   840         -    return SQLITE_ERROR;
          839  +int sqlite3CheckObjectName(
          840  +  Parse *pParse,            /* Parsing context */
          841  +  const char *zName,        /* Name of the object to check */
          842  +  const char *zType,        /* Type of this object */
          843  +  const char *zTblName      /* Parent table name for triggers and indexes */
          844  +){
          845  +  sqlite3 *db = pParse->db;
          846  +  if( sqlite3WritableSchema(db) || db->init.imposterTable ){
          847  +    /* Skip these error checks for writable_schema=ON */
          848  +    return SQLITE_OK;
          849  +  }
          850  +  if( db->init.busy ){
          851  +    if( sqlite3_stricmp(zType, db->init.azInit[0])
          852  +     || sqlite3_stricmp(zName, db->init.azInit[1])
          853  +     || sqlite3_stricmp(zTblName, db->init.azInit[2])
          854  +    ){
          855  +      if( sqlite3Config.bExtraSchemaChecks ){
          856  +        sqlite3ErrorMsg(pParse, ""); /* corruptSchema() will supply the error */
          857  +        return SQLITE_ERROR;
          858  +      }
          859  +    }
          860  +  }else{
          861  +    if( pParse->nested==0 
          862  +     && 0==sqlite3StrNICmp(zName, "sqlite_", 7)
          863  +    ){
          864  +      sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s",
          865  +                      zName);
          866  +      return SQLITE_ERROR;
          867  +    }
   841    868     }
   842    869     return SQLITE_OK;
   843    870   }
   844    871   
   845    872   /*
   846    873   ** Return the PRIMARY KEY index of a table
   847    874   */
................................................................................
   914    941       zName = sqlite3NameFromToken(db, pName);
   915    942       if( IN_RENAME_OBJECT ){
   916    943         sqlite3RenameTokenMap(pParse, (void*)zName, pName);
   917    944       }
   918    945     }
   919    946     pParse->sNameToken = *pName;
   920    947     if( zName==0 ) return;
   921         -  if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
          948  +  if( sqlite3CheckObjectName(pParse, zName, isView?"view":"table", zName) ){
   922    949       goto begin_table_error;
   923    950     }
   924    951     if( db->init.iDb==1 ) isTemp = 1;
   925    952   #ifndef SQLITE_OMIT_AUTHORIZATION
   926    953     assert( isTemp==0 || isTemp==1 );
   927    954     assert( isView==0 || isView==1 );
   928    955     {
................................................................................
  1414   1441         Expr *pCExpr = sqlite3ExprSkipCollate(pList->a[0].pExpr);
  1415   1442         sqlite3RenameTokenRemap(pParse, &pTab->iPKey, pCExpr);
  1416   1443       }
  1417   1444       pTab->iPKey = iCol;
  1418   1445       pTab->keyConf = (u8)onError;
  1419   1446       assert( autoInc==0 || autoInc==1 );
  1420   1447       pTab->tabFlags |= autoInc*TF_Autoincrement;
  1421         -    if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
         1448  +    if( pList ) pParse->iPkSortOrder = pList->a[0].sortFlags;
  1422   1449     }else if( autoInc ){
  1423   1450   #ifndef SQLITE_OMIT_AUTOINCREMENT
  1424   1451       sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
  1425   1452          "INTEGER PRIMARY KEY");
  1426   1453   #endif
  1427   1454     }else{
  1428   1455       sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
................................................................................
  1829   1856   **
  1830   1857   ** For virtual tables, only (1) is performed.
  1831   1858   */
  1832   1859   static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
  1833   1860     Index *pIdx;
  1834   1861     Index *pPk;
  1835   1862     int nPk;
         1863  +  int nExtra;
  1836   1864     int i, j;
  1837   1865     sqlite3 *db = pParse->db;
  1838   1866     Vdbe *v = pParse->pVdbe;
  1839   1867   
  1840   1868     /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
  1841   1869     */
  1842   1870     if( !db->init.imposterTable ){
................................................................................
  1864   1892       sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
  1865   1893       pList = sqlite3ExprListAppend(pParse, 0, 
  1866   1894                     sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
  1867   1895       if( pList==0 ) return;
  1868   1896       if( IN_RENAME_OBJECT ){
  1869   1897         sqlite3RenameTokenRemap(pParse, pList->a[0].pExpr, &pTab->iPKey);
  1870   1898       }
  1871         -    pList->a[0].sortOrder = pParse->iPkSortOrder;
         1899  +    pList->a[0].sortFlags = pParse->iPkSortOrder;
  1872   1900       assert( pParse->pNewTable==pTab );
  1873   1901       pTab->iPKey = -1;
  1874   1902       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
  1875   1903                          SQLITE_IDXTYPE_PRIMARYKEY);
  1876   1904       if( db->mallocFailed || pParse->nErr ) return;
  1877   1905       pPk = sqlite3PrimaryKeyIndex(pTab);
         1906  +    assert( pPk->nKeyCol==1 );
  1878   1907     }else{
  1879   1908       pPk = sqlite3PrimaryKeyIndex(pTab);
  1880   1909       assert( pPk!=0 );
  1881   1910   
  1882   1911       /*
  1883   1912       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
  1884   1913       ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
................................................................................
  1885   1914       ** code assumes the PRIMARY KEY contains no repeated columns.
  1886   1915       */
  1887   1916       for(i=j=1; i<pPk->nKeyCol; i++){
  1888   1917         if( isDupColumn(pPk, j, pPk, i) ){
  1889   1918           pPk->nColumn--;
  1890   1919         }else{
  1891   1920           testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
         1921  +        pPk->azColl[j] = pPk->azColl[i];
         1922  +        pPk->aSortOrder[j] = pPk->aSortOrder[i];
  1892   1923           pPk->aiColumn[j++] = pPk->aiColumn[i];
  1893   1924         }
  1894   1925       }
  1895   1926       pPk->nKeyCol = j;
  1896   1927     }
  1897   1928     assert( pPk!=0 );
  1898   1929     pPk->isCovering = 1;
  1899   1930     if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
  1900         -  nPk = pPk->nKeyCol;
         1931  +  nPk = pPk->nColumn = pPk->nKeyCol;
  1901   1932   
  1902   1933     /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
  1903   1934     ** table entry. This is only required if currently generating VDBE
  1904   1935     ** code for a CREATE TABLE (not when parsing one as part of reading
  1905   1936     ** a database schema).  */
  1906   1937     if( v && pPk->tnum>0 ){
  1907   1938       assert( db->init.busy==0 );
................................................................................
  1943   1974       }
  1944   1975       assert( pIdx->nColumn>=pIdx->nKeyCol+n );
  1945   1976       assert( pIdx->nColumn>=j );
  1946   1977     }
  1947   1978   
  1948   1979     /* Add all table columns to the PRIMARY KEY index
  1949   1980     */
  1950         -  if( nPk<pTab->nCol ){
  1951         -    if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
  1952         -    for(i=0, j=nPk; i<pTab->nCol; i++){
  1953         -      if( !hasColumn(pPk->aiColumn, j, i) ){
  1954         -        assert( j<pPk->nColumn );
  1955         -        pPk->aiColumn[j] = i;
  1956         -        pPk->azColl[j] = sqlite3StrBINARY;
  1957         -        j++;
  1958         -      }
         1981  +  nExtra = 0;
         1982  +  for(i=0; i<pTab->nCol; i++){
         1983  +    if( !hasColumn(pPk->aiColumn, nPk, i) ) nExtra++;
         1984  +  }
         1985  +  if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
         1986  +  for(i=0, j=nPk; i<pTab->nCol; i++){
         1987  +    if( !hasColumn(pPk->aiColumn, j, i) ){
         1988  +      assert( j<pPk->nColumn );
         1989  +      pPk->aiColumn[j] = i;
         1990  +      pPk->azColl[j] = sqlite3StrBINARY;
         1991  +      j++;
  1959   1992       }
  1960         -    assert( pPk->nColumn==j );
  1961         -    assert( pTab->nCol==j );
  1962         -  }else{
  1963         -    pPk->nColumn = pTab->nCol;
  1964   1993     }
         1994  +  assert( pPk->nColumn==j );
         1995  +  assert( pTab->nCol<=j );
  1965   1996     recomputeColumnsNotIndexed(pPk);
  1966   1997   }
  1967   1998   
  1968   1999   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1969   2000   /*
  1970   2001   ** Return true if zName is a shadow table name in the current database
  1971   2002   ** connection.
................................................................................
  2154   2185         sqlite3MayAbort(pParse);
  2155   2186         sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
  2156   2187         sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
  2157   2188         pParse->nTab = 2;
  2158   2189         addrTop = sqlite3VdbeCurrentAddr(v) + 1;
  2159   2190         sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
  2160   2191         if( pParse->nErr ) return;
  2161         -      pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
         2192  +      pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect, SQLITE_AFF_BLOB);
  2162   2193         if( pSelTab==0 ) return;
  2163   2194         assert( p->aCol==0 );
  2164   2195         p->nCol = pSelTab->nCol;
  2165   2196         p->aCol = pSelTab->aCol;
  2166   2197         pSelTab->nCol = 0;
  2167   2198         pSelTab->aCol = 0;
  2168   2199         sqlite3DeleteTable(db, pSelTab);
................................................................................
  2418   2449       n = pParse->nTab;
  2419   2450       sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
  2420   2451       pTable->nCol = -1;
  2421   2452       db->lookaside.bDisable++;
  2422   2453   #ifndef SQLITE_OMIT_AUTHORIZATION
  2423   2454       xAuth = db->xAuth;
  2424   2455       db->xAuth = 0;
  2425         -    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
         2456  +    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
  2426   2457       db->xAuth = xAuth;
  2427   2458   #else
  2428         -    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
         2459  +    pSelTab = sqlite3ResultSetOfSelect(pParse, pSel, SQLITE_AFF_NONE);
  2429   2460   #endif
  2430   2461       pParse->nTab = n;
  2431   2462       if( pTable->pCheck ){
  2432   2463         /* CREATE VIEW name(arglist) AS ...
  2433   2464         ** The names of the columns in the table are taken from
  2434   2465         ** arglist which is stored in pTable->pCheck.  The pCheck field
  2435   2466         ** normally holds CHECK constraints on an ordinary table, but for
................................................................................
  2437   2468         */
  2438   2469         sqlite3ColumnsFromExprList(pParse, pTable->pCheck, 
  2439   2470                                    &pTable->nCol, &pTable->aCol);
  2440   2471         if( db->mallocFailed==0 
  2441   2472          && pParse->nErr==0
  2442   2473          && pTable->nCol==pSel->pEList->nExpr
  2443   2474         ){
  2444         -        sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel);
         2475  +        sqlite3SelectAddColumnTypeAndCollation(pParse, pTable, pSel,
         2476  +                                               SQLITE_AFF_NONE);
  2445   2477         }
  2446   2478       }else if( pSelTab ){
  2447   2479         /* CREATE VIEW name AS...  without an argument list.  Construct
  2448   2480         ** the column names from the SELECT statement that defines the view.
  2449   2481         */
  2450   2482         assert( pTable->aCol==0 );
  2451   2483         pTable->nCol = pSelTab->nCol;
................................................................................
  3118   3150       p->aSortOrder = (u8*)pExtra;
  3119   3151       p->nColumn = nCol;
  3120   3152       p->nKeyCol = nCol - 1;
  3121   3153       *ppExtra = ((char*)p) + nByte;
  3122   3154     }
  3123   3155     return p;
  3124   3156   }
         3157  +
         3158  +/*
         3159  +** If expression list pList contains an expression that was parsed with
         3160  +** an explicit "NULLS FIRST" or "NULLS LAST" clause, leave an error in
         3161  +** pParse and return non-zero. Otherwise, return zero.
         3162  +*/
         3163  +int sqlite3HasExplicitNulls(Parse *pParse, ExprList *pList){
         3164  +  if( pList ){
         3165  +    int i;
         3166  +    for(i=0; i<pList->nExpr; i++){
         3167  +      if( pList->a[i].bNulls ){
         3168  +        u8 sf = pList->a[i].sortFlags;
         3169  +        sqlite3ErrorMsg(pParse, "unsupported use of NULLS %s", 
         3170  +            (sf==0 || sf==3) ? "FIRST" : "LAST"
         3171  +        );
         3172  +        return 1;
         3173  +      }
         3174  +    }
         3175  +  }
         3176  +  return 0;
         3177  +}
  3125   3178   
  3126   3179   /*
  3127   3180   ** Create a new index for an SQL table.  pName1.pName2 is the name of the index 
  3128   3181   ** and pTblList is the name of the table that is to be indexed.  Both will 
  3129   3182   ** be NULL for a primary key or an index that is created to satisfy a
  3130   3183   ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
  3131   3184   ** as the table to be indexed.  pParse->pNewTable is a table that is
................................................................................
  3169   3222       goto exit_create_index;
  3170   3223     }
  3171   3224     if( IN_DECLARE_VTAB && idxType!=SQLITE_IDXTYPE_PRIMARYKEY ){
  3172   3225       goto exit_create_index;
  3173   3226     }
  3174   3227     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
  3175   3228       goto exit_create_index;
         3229  +  }
         3230  +  if( sqlite3HasExplicitNulls(pParse, pList) ){
         3231  +    goto exit_create_index;
  3176   3232     }
  3177   3233   
  3178   3234     /*
  3179   3235     ** Find the table that is to be indexed.  Return early if not found.
  3180   3236     */
  3181   3237     if( pTblName!=0 ){
  3182   3238   
................................................................................
  3268   3324     ** dealing with a primary key or UNIQUE constraint.  We have to invent our
  3269   3325     ** own name.
  3270   3326     */
  3271   3327     if( pName ){
  3272   3328       zName = sqlite3NameFromToken(db, pName);
  3273   3329       if( zName==0 ) goto exit_create_index;
  3274   3330       assert( pName->z!=0 );
  3275         -    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
         3331  +    if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName,"index",pTab->zName) ){
  3276   3332         goto exit_create_index;
  3277   3333       }
  3278   3334       if( !IN_RENAME_OBJECT ){
  3279   3335         if( !db->init.busy ){
  3280   3336           if( sqlite3FindTable(db, zName, 0)!=0 ){
  3281   3337             sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
  3282   3338             goto exit_create_index;
................................................................................
  3334   3390       Column *pCol = &pTab->aCol[pTab->nCol-1];
  3335   3391       pCol->colFlags |= COLFLAG_UNIQUE;
  3336   3392       sqlite3TokenInit(&prevCol, pCol->zName);
  3337   3393       pList = sqlite3ExprListAppend(pParse, 0,
  3338   3394                 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
  3339   3395       if( pList==0 ) goto exit_create_index;
  3340   3396       assert( pList->nExpr==1 );
  3341         -    sqlite3ExprListSetSortOrder(pList, sortOrder);
         3397  +    sqlite3ExprListSetSortOrder(pList, sortOrder, SQLITE_SO_UNDEFINED);
  3342   3398     }else{
  3343   3399       sqlite3ExprListCheckLength(pParse, pList, "index");
  3344   3400       if( pParse->nErr ) goto exit_create_index;
  3345   3401     }
  3346   3402   
  3347   3403     /* Figure out how many bytes of space are required to store explicitly
  3348   3404     ** specified collation sequence names.
................................................................................
  3452   3508         zColl = pTab->aCol[j].zColl;
  3453   3509       }
  3454   3510       if( !zColl ) zColl = sqlite3StrBINARY;
  3455   3511       if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
  3456   3512         goto exit_create_index;
  3457   3513       }
  3458   3514       pIndex->azColl[i] = zColl;
  3459         -    requestedSortOrder = pListItem->sortOrder & sortOrderMask;
         3515  +    requestedSortOrder = pListItem->sortFlags & sortOrderMask;
  3460   3516       pIndex->aSortOrder[i] = (u8)requestedSortOrder;
  3461   3517     }
  3462   3518   
  3463   3519     /* Append the table key to the end of the index.  For WITHOUT ROWID
  3464   3520     ** tables (when pPk!=0) this will be the declared PRIMARY KEY.  For
  3465   3521     ** normal tables (when pPk==0) this will be the rowid.
  3466   3522     */
................................................................................
  3627   3683         ** the Noop with a Goto to jump over the VDBE code generated below. */
  3628   3684         pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
  3629   3685         sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
  3630   3686   
  3631   3687         /* Gather the complete text of the CREATE INDEX statement into
  3632   3688         ** the zStmt variable
  3633   3689         */
         3690  +      assert( pName!=0 || pStart==0 );
  3634   3691         if( pStart ){
  3635   3692           int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
  3636   3693           if( pName->z[n-1]==';' ) n--;
  3637   3694           /* A named index with an explicit CREATE INDEX statement */
  3638   3695           zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
  3639   3696               onError==OE_None ? "" : " UNIQUE", n, pName->z);
  3640   3697         }else{
................................................................................
  4669   4726     }
  4670   4727     if( pKey ){
  4671   4728       assert( sqlite3KeyInfoIsWriteable(pKey) );
  4672   4729       for(i=0; i<nCol; i++){
  4673   4730         const char *zColl = pIdx->azColl[i];
  4674   4731         pKey->aColl[i] = zColl==sqlite3StrBINARY ? 0 :
  4675   4732                           sqlite3LocateCollSeq(pParse, zColl);
  4676         -      pKey->aSortOrder[i] = pIdx->aSortOrder[i];
         4733  +      pKey->aSortFlags[i] = pIdx->aSortOrder[i];
         4734  +      assert( 0==(pKey->aSortFlags[i] & KEYINFO_ORDER_BIGNULL) );
  4677   4735       }
  4678   4736       if( pParse->nErr ){
  4679   4737         assert( pParse->rc==SQLITE_ERROR_MISSING_COLLSEQ );
  4680   4738         if( pIdx->bNoQuery==0 ){
  4681   4739           /* Deactivate the index because it contains an unknown collating
  4682   4740           ** sequence.  The only way to reactive the index is to reload the
  4683   4741           ** schema.  Adding the missing collating sequence later does not

Changes to src/ctime.c.

   302    302     "ENABLE_SORTER_REFERENCES",
   303    303   #endif
   304    304   #if SQLITE_ENABLE_SQLLOG
   305    305     "ENABLE_SQLLOG",
   306    306   #endif
   307    307   #if defined(SQLITE_ENABLE_STAT4)
   308    308     "ENABLE_STAT4",
   309         -#elif defined(SQLITE_ENABLE_STAT3)
   310         -  "ENABLE_STAT3",
   311    309   #endif
   312    310   #if SQLITE_ENABLE_STMTVTAB
   313    311     "ENABLE_STMTVTAB",
   314    312   #endif
   315    313   #if SQLITE_ENABLE_STMT_SCANSTATUS
   316    314     "ENABLE_STMT_SCANSTATUS",
   317    315   #endif

Changes to src/expr.c.

    40     40   ** CREATE TABLE t1(a);
    41     41   ** SELECT * FROM t1 WHERE a;
    42     42   ** SELECT a AS b FROM t1 WHERE b;
    43     43   ** SELECT * FROM t1 WHERE (select a from t1);
    44     44   */
    45     45   char sqlite3ExprAffinity(Expr *pExpr){
    46     46     int op;
    47         -  if( pExpr->flags & EP_Generic ) return 0;
    48     47     while( ExprHasProperty(pExpr, EP_Skip) ){
    49     48       assert( pExpr->op==TK_COLLATE );
    50     49       pExpr = pExpr->pLeft;
    51     50       assert( pExpr!=0 );
    52     51     }
    53     52     op = pExpr->op;
    54     53     if( op==TK_SELECT ){
................................................................................
    67     66     }
    68     67     if( op==TK_SELECT_COLUMN ){
    69     68       assert( pExpr->pLeft->flags&EP_xIsSelect );
    70     69       return sqlite3ExprAffinity(
    71     70           pExpr->pLeft->x.pSelect->pEList->a[pExpr->iColumn].pExpr
    72     71       );
    73     72     }
    74         -  return pExpr->affinity;
           73  +  return pExpr->affExpr;
    75     74   }
    76     75   
    77     76   /*
    78     77   ** Set the collating sequence for expression pExpr to be the collating
    79     78   ** sequence named by pToken.   Return a pointer to a new Expr node that
    80     79   ** implements the COLLATE operator.
    81     80   **
................................................................................
   102    101     Token s;
   103    102     assert( zC!=0 );
   104    103     sqlite3TokenInit(&s, (char*)zC);
   105    104     return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
   106    105   }
   107    106   
   108    107   /*
   109         -** Skip over any TK_COLLATE operators and any unlikely()
   110         -** or likelihood() function at the root of an expression.
          108  +** Skip over any TK_COLLATE operators.
   111    109   */
   112    110   Expr *sqlite3ExprSkipCollate(Expr *pExpr){
          111  +  while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
          112  +    assert( pExpr->op==TK_COLLATE );
          113  +    pExpr = pExpr->pLeft;
          114  +  }   
          115  +  return pExpr;
          116  +}
          117  +
          118  +/*
          119  +** Skip over any TK_COLLATE operators and/or any unlikely()
          120  +** or likelihood() or likely() functions at the root of an
          121  +** expression.
          122  +*/
          123  +Expr *sqlite3ExprSkipCollateAndLikely(Expr *pExpr){
   113    124     while( pExpr && ExprHasProperty(pExpr, EP_Skip|EP_Unlikely) ){
   114    125       if( ExprHasProperty(pExpr, EP_Unlikely) ){
   115    126         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   116    127         assert( pExpr->x.pList->nExpr>0 );
   117    128         assert( pExpr->op==TK_FUNCTION );
   118    129         pExpr = pExpr->x.pList->a[0].pExpr;
   119    130       }else{
................................................................................
   140    151   */
   141    152   CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
   142    153     sqlite3 *db = pParse->db;
   143    154     CollSeq *pColl = 0;
   144    155     Expr *p = pExpr;
   145    156     while( p ){
   146    157       int op = p->op;
   147         -    if( p->flags & EP_Generic ) break;
   148    158       if( op==TK_REGISTER ) op = p->op2;
   149    159       if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_TRIGGER)
   150    160        && p->y.pTab!=0
   151    161       ){
   152    162         /* op==TK_REGISTER && p->y.pTab!=0 happens when pExpr was originally
   153    163         ** a TK_COLUMN but was previously evaluated and cached in a register */
   154    164         int j = p->iColumn;
................................................................................
   226    236   /*
   227    237   ** pExpr is an operand of a comparison operator.  aff2 is the
   228    238   ** type affinity of the other operand.  This routine returns the
   229    239   ** type affinity that should be used for the comparison operator.
   230    240   */
   231    241   char sqlite3CompareAffinity(Expr *pExpr, char aff2){
   232    242     char aff1 = sqlite3ExprAffinity(pExpr);
   233         -  if( aff1 && aff2 ){
          243  +  if( aff1>SQLITE_AFF_NONE && aff2>SQLITE_AFF_NONE ){
   234    244       /* Both sides of the comparison are columns. If one has numeric
   235    245       ** affinity, use that. Otherwise use no affinity.
   236    246       */
   237    247       if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
   238    248         return SQLITE_AFF_NUMERIC;
   239    249       }else{
   240    250         return SQLITE_AFF_BLOB;
   241    251       }
   242         -  }else if( !aff1 && !aff2 ){
   243         -    /* Neither side of the comparison is a column.  Compare the
   244         -    ** results directly.
   245         -    */
   246         -    return SQLITE_AFF_BLOB;
   247    252     }else{
   248    253       /* One side is a column, the other is not. Use the columns affinity. */
   249         -    assert( aff1==0 || aff2==0 );
   250         -    return (aff1 + aff2);
          254  +    assert( aff1<=SQLITE_AFF_NONE || aff2<=SQLITE_AFF_NONE );
          255  +    return (aff1<=SQLITE_AFF_NONE ? aff2 : aff1) | SQLITE_AFF_NONE;
   251    256     }
   252    257   }
   253    258   
   254    259   /*
   255    260   ** pExpr is a comparison operator.  Return the type affinity that should
   256    261   ** be applied to both operands prior to doing the comparison.
   257    262   */
................................................................................
   276    281   ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
   277    282   ** idx_affinity is the affinity of an indexed column. Return true
   278    283   ** if the index with affinity idx_affinity may be used to implement
   279    284   ** the comparison in pExpr.
   280    285   */
   281    286   int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
   282    287     char aff = comparisonAffinity(pExpr);
   283         -  switch( aff ){
   284         -    case SQLITE_AFF_BLOB:
   285         -      return 1;
   286         -    case SQLITE_AFF_TEXT:
   287         -      return idx_affinity==SQLITE_AFF_TEXT;
   288         -    default:
   289         -      return sqlite3IsNumericAffinity(idx_affinity);
          288  +  if( aff<SQLITE_AFF_TEXT ){
          289  +    return 1;
   290    290     }
          291  +  if( aff==SQLITE_AFF_TEXT ){
          292  +    return idx_affinity==SQLITE_AFF_TEXT;
          293  +  }
          294  +  return sqlite3IsNumericAffinity(idx_affinity);
   291    295   }
   292    296   
   293    297   /*
   294    298   ** Return the P5 value that should be used for a binary comparison
   295    299   ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
   296    300   */
   297    301   static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
................................................................................
  1036   1040     }
  1037   1041   #endif
  1038   1042     if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
  1039   1043       /* The Expr.x union is never used at the same time as Expr.pRight */
  1040   1044       assert( p->x.pList==0 || p->pRight==0 );
  1041   1045       if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
  1042   1046       if( p->pRight ){
         1047  +      assert( !ExprHasProperty(p, EP_WinFunc) );
  1043   1048         sqlite3ExprDeleteNN(db, p->pRight);
  1044   1049       }else if( ExprHasProperty(p, EP_xIsSelect) ){
         1050  +      assert( !ExprHasProperty(p, EP_WinFunc) );
  1045   1051         sqlite3SelectDelete(db, p->x.pSelect);
  1046   1052       }else{
  1047   1053         sqlite3ExprListDelete(db, p->x.pList);
  1048         -    }
  1049         -    if( ExprHasProperty(p, EP_WinFunc) ){
  1050         -      assert( p->op==TK_FUNCTION );
  1051         -      sqlite3WindowDelete(db, p->y.pWin);
         1054  +#ifndef SQLITE_OMIT_WINDOWFUNC
         1055  +      if( ExprHasProperty(p, EP_WinFunc) ){
         1056  +        sqlite3WindowDelete(db, p->y.pWin);
         1057  +      }
         1058  +#endif
  1052   1059       }
  1053   1060     }
  1054   1061     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
  1055   1062     if( !ExprHasProperty(p, EP_Static) ){
  1056   1063       sqlite3DbFreeNN(db, p);
  1057   1064     }
  1058   1065   }
................................................................................
  1079   1086   */
  1080   1087   static int exprStructSize(Expr *p){
  1081   1088     if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
  1082   1089     if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
  1083   1090     return EXPR_FULLSIZE;
  1084   1091   }
  1085   1092   
  1086         -/*
  1087         -** Copy the complete content of an Expr node, taking care not to read
  1088         -** past the end of the structure for a reduced-size version of the source
  1089         -** Expr.
  1090         -*/
  1091         -static void exprNodeCopy(Expr *pDest, Expr *pSrc){
  1092         -  memset(pDest, 0, sizeof(Expr));
  1093         -  memcpy(pDest, pSrc, exprStructSize(pSrc));
  1094         -}
  1095         -
  1096   1093   /*
  1097   1094   ** The dupedExpr*Size() routines each return the number of bytes required
  1098   1095   ** to store a copy of an expression or expression tree.  They differ in
  1099   1096   ** how much of the tree is measured.
  1100   1097   **
  1101   1098   **     dupedExprStructSize()     Size of only the Expr structure 
  1102   1099   **     dupedExprNodeSize()       Size of Expr + space for token
................................................................................
  1328   1325   /*
  1329   1326   ** The gatherSelectWindows() procedure and its helper routine
  1330   1327   ** gatherSelectWindowsCallback() are used to scan all the expressions
  1331   1328   ** an a newly duplicated SELECT statement and gather all of the Window
  1332   1329   ** objects found there, assembling them onto the linked list at Select->pWin.
  1333   1330   */
  1334   1331   static int gatherSelectWindowsCallback(Walker *pWalker, Expr *pExpr){
  1335         -  if( pExpr->op==TK_FUNCTION && pExpr->y.pWin!=0 ){
  1336         -    assert( ExprHasProperty(pExpr, EP_WinFunc) );
  1337         -    pExpr->y.pWin->pNextWin = pWalker->u.pSelect->pWin;
  1338         -    pWalker->u.pSelect->pWin = pExpr->y.pWin;
         1332  +  if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
         1333  +    Select *pSelect = pWalker->u.pSelect;
         1334  +    Window *pWin = pExpr->y.pWin;
         1335  +    assert( pWin );
         1336  +    assert( IsWindowFunc(pExpr) );
         1337  +    assert( pWin->ppThis==0 );
         1338  +    sqlite3WindowLink(pSelect, pWin);
  1339   1339     }
  1340   1340     return WRC_Continue;
  1341   1341   }
  1342   1342   static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
  1343   1343     return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
  1344   1344   }
  1345   1345   static void gatherSelectWindows(Select *p){
................................................................................
  1405   1405           assert( pNewExpr->iColumn==pItem[-1].pExpr->iColumn+1 );
  1406   1406           assert( pPriorSelectCol==pItem[-1].pExpr->pLeft );
  1407   1407           pNewExpr->pLeft = pPriorSelectCol;
  1408   1408         }
  1409   1409       }
  1410   1410       pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
  1411   1411       pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
  1412         -    pItem->sortOrder = pOldItem->sortOrder;
         1412  +    pItem->sortFlags = pOldItem->sortFlags;
  1413   1413       pItem->done = 0;
         1414  +    pItem->bNulls = pOldItem->bNulls;
  1414   1415       pItem->bSpanIsTab = pOldItem->bSpanIsTab;
  1415   1416       pItem->bSorterRef = pOldItem->bSorterRef;
  1416   1417       pItem->u = pOldItem->u;
  1417   1418     }
  1418   1419     return pNew;
  1419   1420   }
  1420   1421   
................................................................................
  1517   1518       pNew->addrOpenEphm[0] = -1;
  1518   1519       pNew->addrOpenEphm[1] = -1;
  1519   1520       pNew->nSelectRow = p->nSelectRow;
  1520   1521       pNew->pWith = withDup(db, p->pWith);
  1521   1522   #ifndef SQLITE_OMIT_WINDOWFUNC
  1522   1523       pNew->pWin = 0;
  1523   1524       pNew->pWinDefn = sqlite3WindowListDup(db, p->pWinDefn);
  1524         -    if( p->pWin ) gatherSelectWindows(pNew);
         1525  +    if( p->pWin && db->mallocFailed==0 ) gatherSelectWindows(pNew);
  1525   1526   #endif
  1526   1527       pNew->selId = p->selId;
  1527   1528       *pp = pNew;
  1528   1529       pp = &pNew->pPrior;
  1529   1530       pNext = pNew;
  1530   1531     }
  1531   1532   
................................................................................
  1626   1627       sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
  1627   1628                       pColumns->nId, n);
  1628   1629       goto vector_append_error;
  1629   1630     }
  1630   1631   
  1631   1632     for(i=0; i<pColumns->nId; i++){
  1632   1633       Expr *pSubExpr = sqlite3ExprForVectorField(pParse, pExpr, i);
         1634  +    assert( pSubExpr!=0 || db->mallocFailed );
         1635  +    assert( pSubExpr==0 || pSubExpr->iTable==0 );
         1636  +    if( pSubExpr==0 ) continue;
         1637  +    pSubExpr->iTable = pColumns->nId;
  1633   1638       pList = sqlite3ExprListAppend(pParse, pList, pSubExpr);
  1634   1639       if( pList ){
  1635   1640         assert( pList->nExpr==iFirst+i+1 );
  1636   1641         pList->a[pList->nExpr-1].zName = pColumns->a[i].zName;
  1637   1642         pColumns->a[i].zName = 0;
  1638   1643       }
  1639   1644     }
................................................................................
  1658   1663     sqlite3IdListDelete(db, pColumns);
  1659   1664     return pList;
  1660   1665   }
  1661   1666   
  1662   1667   /*
  1663   1668   ** Set the sort order for the last element on the given ExprList.
  1664   1669   */
  1665         -void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){
         1670  +void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder, int eNulls){
         1671  +  struct ExprList_item *pItem;
  1666   1672     if( p==0 ) return;
  1667         -  assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 );
  1668   1673     assert( p->nExpr>0 );
  1669         -  if( iSortOrder<0 ){
  1670         -    assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC );
  1671         -    return;
         1674  +
         1675  +  assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC==0 && SQLITE_SO_DESC>0 );
         1676  +  assert( iSortOrder==SQLITE_SO_UNDEFINED 
         1677  +       || iSortOrder==SQLITE_SO_ASC 
         1678  +       || iSortOrder==SQLITE_SO_DESC 
         1679  +  );
         1680  +  assert( eNulls==SQLITE_SO_UNDEFINED 
         1681  +       || eNulls==SQLITE_SO_ASC 
         1682  +       || eNulls==SQLITE_SO_DESC 
         1683  +  );
         1684  +
         1685  +  pItem = &p->a[p->nExpr-1];
         1686  +  assert( pItem->bNulls==0 );
         1687  +  if( iSortOrder==SQLITE_SO_UNDEFINED ){
         1688  +    iSortOrder = SQLITE_SO_ASC;
  1672   1689     }
  1673         -  p->a[p->nExpr-1].sortOrder = (u8)iSortOrder;
         1690  +  pItem->sortFlags = (u8)iSortOrder;
         1691  +
         1692  +  if( eNulls!=SQLITE_SO_UNDEFINED ){
         1693  +    pItem->bNulls = 1;
         1694  +    if( iSortOrder!=eNulls ){
         1695  +      pItem->sortFlags |= KEYINFO_ORDER_BIGNULL;
         1696  +    }
         1697  +  }
  1674   1698   }
  1675   1699   
  1676   1700   /*
  1677   1701   ** Set the ExprList.a[].zName element of the most recently added item
  1678   1702   ** on the expression list.
  1679   1703   **
  1680   1704   ** pList might be NULL following an OOM error.  But pName should never be
................................................................................
  2165   2189   ** This routine is used to determine if the OP_Affinity operation
  2166   2190   ** can be omitted.  When in doubt return FALSE.  A false negative
  2167   2191   ** is harmless.  A false positive, however, can result in the wrong
  2168   2192   ** answer.
  2169   2193   */
  2170   2194   int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
  2171   2195     u8 op;
         2196  +  int unaryMinus = 0;
  2172   2197     if( aff==SQLITE_AFF_BLOB ) return 1;
  2173         -  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
         2198  +  while( p->op==TK_UPLUS || p->op==TK_UMINUS ){
         2199  +    if( p->op==TK_UMINUS ) unaryMinus = 1;
         2200  +    p = p->pLeft;
         2201  +  }
  2174   2202     op = p->op;
  2175   2203     if( op==TK_REGISTER ) op = p->op2;
  2176   2204     switch( op ){
  2177   2205       case TK_INTEGER: {
  2178         -      return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
         2206  +      return aff>=SQLITE_AFF_NUMERIC;
  2179   2207       }
  2180   2208       case TK_FLOAT: {
  2181         -      return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
         2209  +      return aff>=SQLITE_AFF_NUMERIC;
  2182   2210       }
  2183   2211       case TK_STRING: {
  2184         -      return aff==SQLITE_AFF_TEXT;
         2212  +      return !unaryMinus && aff==SQLITE_AFF_TEXT;
  2185   2213       }
  2186   2214       case TK_BLOB: {
  2187         -      return 1;
         2215  +      return !unaryMinus;
  2188   2216       }
  2189   2217       case TK_COLUMN: {
  2190   2218         assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
  2191         -      return p->iColumn<0
  2192         -          && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
         2219  +      return aff>=SQLITE_AFF_NUMERIC && p->iColumn<0;
  2193   2220       }
  2194   2221       default: {
  2195   2222         return 0;
  2196   2223       }
  2197   2224     }
  2198   2225   }
  2199   2226   
................................................................................
  2368   2395   **   CREATE INDEX i1 ON t1(b, c, a);
  2369   2396   **
  2370   2397   ** then aiMap[] is populated with {2, 0, 1}.
  2371   2398   */
  2372   2399   #ifndef SQLITE_OMIT_SUBQUERY
  2373   2400   int sqlite3FindInIndex(
  2374   2401     Parse *pParse,             /* Parsing context */
  2375         -  Expr *pX,                  /* The right-hand side (RHS) of the IN operator */
         2402  +  Expr *pX,                  /* The IN expression */
  2376   2403     u32 inFlags,               /* IN_INDEX_LOOP, _MEMBERSHIP, and/or _NOOP_OK */
  2377   2404     int *prRhsHasNull,         /* Register holding NULL status.  See notes */
  2378   2405     int *aiMap,                /* Mapping from Index fields to RHS fields */
  2379   2406     int *piTab                 /* OUT: index to use */
  2380   2407   ){
  2381   2408     Select *p;                            /* SELECT to the right of IN operator */
  2382   2409     int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
................................................................................
  2793   2820       ** that columns affinity when building index keys. If <expr> is not
  2794   2821       ** a column, use numeric affinity.
  2795   2822       */
  2796   2823       char affinity;            /* Affinity of the LHS of the IN */
  2797   2824       int i;
  2798   2825       ExprList *pList = pExpr->x.pList;
  2799   2826       struct ExprList_item *pItem;
  2800         -    int r1, r2, r3;
         2827  +    int r1, r2;
  2801   2828       affinity = sqlite3ExprAffinity(pLeft);
  2802         -    if( !affinity ){
         2829  +    if( affinity<=SQLITE_AFF_NONE ){
  2803   2830         affinity = SQLITE_AFF_BLOB;
  2804   2831       }
  2805   2832       if( pKeyInfo ){
  2806   2833         assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
  2807   2834         pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  2808   2835       }
  2809   2836   
................................................................................
  2821   2848         if( addrOnce && !sqlite3ExprIsConstant(pE2) ){
  2822   2849           sqlite3VdbeChangeToNoop(v, addrOnce);
  2823   2850           ExprClearProperty(pExpr, EP_Subrtn);
  2824   2851           addrOnce = 0;
  2825   2852         }
  2826   2853   
  2827   2854         /* Evaluate the expression and insert it into the temp table */
  2828         -      r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
  2829         -      sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
  2830         -      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r3, 1);
         2855  +      sqlite3ExprCode(pParse, pE2, r1);
         2856  +      sqlite3VdbeAddOp4(v, OP_MakeRecord, r1, 1, r2, &affinity, 1);
         2857  +      sqlite3VdbeAddOp4Int(v, OP_IdxInsert, iTab, r2, r1, 1);
  2831   2858       }
  2832   2859       sqlite3ReleaseTempReg(pParse, r1);
  2833   2860       sqlite3ReleaseTempReg(pParse, r2);
  2834   2861     }
  2835   2862     if( pKeyInfo ){
  2836   2863       sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
  2837   2864     }
................................................................................
  2849   2876   ** or EXISTS operator:
  2850   2877   **
  2851   2878   **     (SELECT a FROM b)          -- subquery
  2852   2879   **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
  2853   2880   **
  2854   2881   ** The pExpr parameter is the SELECT or EXISTS operator to be coded.
  2855   2882   **
  2856         -** The register that holds the result.  For a multi-column SELECT, 
         2883  +** Return the register that holds the result.  For a multi-column SELECT, 
  2857   2884   ** the result is stored in a contiguous array of registers and the
  2858   2885   ** return value is the register of the left-most result column.
  2859   2886   ** Return 0 if an error occurs.
  2860   2887   */
  2861   2888   #ifndef SQLITE_OMIT_SUBQUERY
  2862   2889   int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
  2863   2890     int addrOnce = 0;           /* Address of OP_Once at top of subroutine */
................................................................................
  3093   3120     if( eType==IN_INDEX_NOOP ){
  3094   3121       ExprList *pList = pExpr->x.pList;
  3095   3122       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  3096   3123       int labelOk = sqlite3VdbeMakeLabel(pParse);
  3097   3124       int r2, regToFree;
  3098   3125       int regCkNull = 0;
  3099   3126       int ii;
         3127  +    int bLhsReal;  /* True if the LHS of the IN has REAL affinity */
  3100   3128       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  3101   3129       if( destIfNull!=destIfFalse ){
  3102   3130         regCkNull = sqlite3GetTempReg(pParse);
  3103   3131         sqlite3VdbeAddOp3(v, OP_BitAnd, rLhs, rLhs, regCkNull);
  3104   3132       }
         3133  +    bLhsReal = sqlite3ExprAffinity(pExpr->pLeft)==SQLITE_AFF_REAL;
  3105   3134       for(ii=0; ii<pList->nExpr; ii++){
  3106         -      r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
         3135  +      if( bLhsReal ){
         3136  +        r2 = regToFree = sqlite3GetTempReg(pParse);
         3137  +        sqlite3ExprCode(pParse, pList->a[ii].pExpr, r2);
         3138  +        sqlite3VdbeAddOp4(v, OP_Affinity, r2, 1, 0, "E", P4_STATIC);
         3139  +      }else{
         3140  +        r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, &regToFree);
         3141  +      }
  3107   3142         if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){
  3108   3143           sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull);
  3109   3144         }
  3110   3145         if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){
  3111   3146           sqlite3VdbeAddOp4(v, OP_Eq, rLhs, labelOk, r2,
  3112   3147                             (void*)pColl, P4_COLLSEQ);
  3113   3148           VdbeCoverageIf(v, ii<pList->nExpr-1);
................................................................................
  3384   3419   
  3385   3420   /*
  3386   3421   ** Convert a scalar expression node to a TK_REGISTER referencing
  3387   3422   ** register iReg.  The caller must ensure that iReg already contains
  3388   3423   ** the correct value for the expression.
  3389   3424   */
  3390   3425   static void exprToRegister(Expr *pExpr, int iReg){
  3391         -  Expr *p = sqlite3ExprSkipCollate(pExpr);
         3426  +  Expr *p = sqlite3ExprSkipCollateAndLikely(pExpr);
  3392   3427     p->op2 = p->op;
  3393   3428     p->op = TK_REGISTER;
  3394   3429     p->iTable = iReg;
  3395   3430     ExprClearProperty(p, EP_Skip);
  3396   3431   }
  3397   3432   
  3398   3433   /*
................................................................................
  3485   3520           ** constraints, and that constant is coded by the pExpr->pLeft
  3486   3521           ** expresssion.  However, make sure the constant has the correct
  3487   3522           ** datatype by applying the Affinity of the table column to the
  3488   3523           ** constant.
  3489   3524           */
  3490   3525           int iReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft,target);
  3491   3526           int aff = sqlite3TableColumnAffinity(pExpr->y.pTab, pExpr->iColumn);
  3492         -        if( aff!=SQLITE_AFF_BLOB ){
         3527  +        if( aff>SQLITE_AFF_BLOB ){
  3493   3528             static const char zAff[] = "B\000C\000D\000E";
  3494   3529             assert( SQLITE_AFF_BLOB=='A' );
  3495   3530             assert( SQLITE_AFF_TEXT=='B' );
  3496   3531             if( iReg!=target ){
  3497   3532               sqlite3VdbeAddOp2(v, OP_SCopy, iReg, target);
  3498   3533               iReg = target;
  3499   3534             }
................................................................................
  3501   3536                               &zAff[(aff-'B')*2], P4_STATIC);
  3502   3537           }
  3503   3538           return iReg;
  3504   3539         }
  3505   3540         if( iTab<0 ){
  3506   3541           if( pParse->iSelfTab<0 ){
  3507   3542             /* Generating CHECK constraints or inserting into partial index */
  3508         -          return pExpr->iColumn - pParse->iSelfTab;
         3543  +          assert( pExpr->y.pTab!=0 );
         3544  +          assert( pExpr->iColumn>=XN_ROWID );
         3545  +          assert( pExpr->iColumn<pExpr->y.pTab->nCol );
         3546  +          if( pExpr->iColumn>=0
         3547  +            && pExpr->y.pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
         3548  +          ){
         3549  +            sqlite3VdbeAddOp2(v, OP_SCopy, pExpr->iColumn - pParse->iSelfTab,
         3550  +                              target);
         3551  +            sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
         3552  +            return target;
         3553  +          }else{
         3554  +            return pExpr->iColumn - pParse->iSelfTab;
         3555  +          }
  3509   3556           }else{
  3510   3557             /* Coding an expression that is part of an index where column names
  3511   3558             ** in the index refer to the table to which the index belongs */
  3512   3559             iTab = pParse->iSelfTab - 1;
  3513   3560           }
  3514   3561         }
  3515   3562         return sqlite3ExprCodeGetColumn(pParse, pExpr->y.pTab,
................................................................................
  3788   3835         */
  3789   3836         if( pDef->funcFlags & SQLITE_FUNC_AFFINITY ){
  3790   3837           const char *azAff[] = { "blob", "text", "numeric", "integer", "real" };
  3791   3838           char aff;
  3792   3839           assert( nFarg==1 );
  3793   3840           aff = sqlite3ExprAffinity(pFarg->a[0].pExpr);
  3794   3841           sqlite3VdbeLoadString(v, target, 
  3795         -                              aff ? azAff[aff-SQLITE_AFF_BLOB] : "none");
         3842  +                (aff<=SQLITE_AFF_NONE) ? "none" : azAff[aff-SQLITE_AFF_BLOB]);
  3796   3843           return target;
  3797   3844         }
  3798   3845   #endif
  3799   3846   
  3800   3847         for(i=0; i<nFarg; i++){
  3801   3848           if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
  3802   3849             testcase( i==31 );
................................................................................
  3896   3943       }
  3897   3944       case TK_SELECT_COLUMN: {
  3898   3945         int n;
  3899   3946         if( pExpr->pLeft->iTable==0 ){
  3900   3947           pExpr->pLeft->iTable = sqlite3CodeSubselect(pParse, pExpr->pLeft);
  3901   3948         }
  3902   3949         assert( pExpr->iTable==0 || pExpr->pLeft->op==TK_SELECT );
  3903         -      if( pExpr->iTable
  3904         -       && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft)) 
         3950  +      if( pExpr->iTable!=0
         3951  +       && pExpr->iTable!=(n = sqlite3ExprVectorSize(pExpr->pLeft))
  3905   3952         ){
  3906   3953           sqlite3ErrorMsg(pParse, "%d columns assigned %d values",
  3907   3954                                   pExpr->iTable, n);
  3908   3955         }
  3909   3956         return pExpr->pLeft->iTable + pExpr->iColumn;
  3910   3957       }
  3911   3958       case TK_IN: {
................................................................................
  4000   4047       }
  4001   4048   
  4002   4049       case TK_VECTOR: {
  4003   4050         sqlite3ErrorMsg(pParse, "row value misused");
  4004   4051         break;
  4005   4052       }
  4006   4053   
         4054  +    /* TK_IF_NULL_ROW Expr nodes are inserted ahead of expressions
         4055  +    ** that derive from the right-hand table of a LEFT JOIN.  The
         4056  +    ** Expr.iTable value is the table number for the right-hand table.
         4057  +    ** The expression is only evaluated if that table is not currently
         4058  +    ** on a LEFT JOIN NULL row.
         4059  +    */
  4007   4060       case TK_IF_NULL_ROW: {
  4008   4061         int addrINR;
         4062  +      u8 okConstFactor = pParse->okConstFactor;
  4009   4063         addrINR = sqlite3VdbeAddOp1(v, OP_IfNullRow, pExpr->iTable);
         4064  +      /* Temporarily disable factoring of constant expressions, since
         4065  +      ** even though expressions may appear to be constant, they are not
         4066  +      ** really constant because they originate from the right-hand side
         4067  +      ** of a LEFT JOIN. */
         4068  +      pParse->okConstFactor = 0;
  4010   4069         inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
         4070  +      pParse->okConstFactor = okConstFactor;
  4011   4071         sqlite3VdbeJumpHere(v, addrINR);
  4012   4072         sqlite3VdbeChangeP3(v, addrINR, inReg);
  4013   4073         break;
  4014   4074       }
  4015   4075   
  4016   4076       /*
  4017   4077       ** Form A:
................................................................................
  4040   4100         int nExpr;                        /* 2x number of WHEN terms */
  4041   4101         int i;                            /* Loop counter */
  4042   4102         ExprList *pEList;                 /* List of WHEN terms */
  4043   4103         struct ExprList_item *aListelem;  /* Array of WHEN terms */
  4044   4104         Expr opCompare;                   /* The X==Ei expression */
  4045   4105         Expr *pX;                         /* The X expression */
  4046   4106         Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
         4107  +      Expr *pDel = 0;
         4108  +      sqlite3 *db = pParse->db;
  4047   4109   
  4048   4110         assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
  4049   4111         assert(pExpr->x.pList->nExpr > 0);
  4050   4112         pEList = pExpr->x.pList;
  4051   4113         aListelem = pEList->a;
  4052   4114         nExpr = pEList->nExpr;
  4053   4115         endLabel = sqlite3VdbeMakeLabel(pParse);
  4054   4116         if( (pX = pExpr->pLeft)!=0 ){
  4055         -        exprNodeCopy(&tempX, pX);
         4117  +        pDel = sqlite3ExprDup(db, pX, 0);
         4118  +        if( db->mallocFailed ){
         4119  +          sqlite3ExprDelete(db, pDel);
         4120  +          break;
         4121  +        }
  4056   4122           testcase( pX->op==TK_COLUMN );
  4057         -        exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
         4123  +        exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
  4058   4124           testcase( regFree1==0 );
  4059   4125           memset(&opCompare, 0, sizeof(opCompare));
  4060   4126           opCompare.op = TK_EQ;
  4061         -        opCompare.pLeft = &tempX;
         4127  +        opCompare.pLeft = pDel;
  4062   4128           pTest = &opCompare;
  4063   4129           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
  4064   4130           ** The value in regFree1 might get SCopy-ed into the file result.
  4065   4131           ** So make sure that the regFree1 register is not reused for other
  4066   4132           ** purposes and possibly overwritten.  */
  4067   4133           regFree1 = 0;
  4068   4134         }
................................................................................
  4082   4148           sqlite3VdbeResolveLabel(v, nextCase);
  4083   4149         }
  4084   4150         if( (nExpr&1)!=0 ){
  4085   4151           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
  4086   4152         }else{
  4087   4153           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
  4088   4154         }
         4155  +      sqlite3ExprDelete(db, pDel);
  4089   4156         sqlite3VdbeResolveLabel(v, endLabel);
  4090   4157         break;
  4091   4158       }
  4092   4159   #ifndef SQLITE_OMIT_TRIGGER
  4093   4160       case TK_RAISE: {
  4094         -      assert( pExpr->affinity==OE_Rollback 
  4095         -           || pExpr->affinity==OE_Abort
  4096         -           || pExpr->affinity==OE_Fail
  4097         -           || pExpr->affinity==OE_Ignore
         4161  +      assert( pExpr->affExpr==OE_Rollback 
         4162  +           || pExpr->affExpr==OE_Abort
         4163  +           || pExpr->affExpr==OE_Fail
         4164  +           || pExpr->affExpr==OE_Ignore
  4098   4165         );
  4099   4166         if( !pParse->pTriggerTab ){
  4100   4167           sqlite3ErrorMsg(pParse,
  4101   4168                          "RAISE() may only be used within a trigger-program");
  4102   4169           return 0;
  4103   4170         }
  4104         -      if( pExpr->affinity==OE_Abort ){
         4171  +      if( pExpr->affExpr==OE_Abort ){
  4105   4172           sqlite3MayAbort(pParse);
  4106   4173         }
  4107   4174         assert( !ExprHasProperty(pExpr, EP_IntValue) );
  4108         -      if( pExpr->affinity==OE_Ignore ){
         4175  +      if( pExpr->affExpr==OE_Ignore ){
  4109   4176           sqlite3VdbeAddOp4(
  4110   4177               v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
  4111   4178           VdbeCoverage(v);
  4112   4179         }else{
  4113   4180           sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
  4114         -                              pExpr->affinity, pExpr->u.zToken, 0, 0);
         4181  +                              pExpr->affExpr, pExpr->u.zToken, 0, 0);
  4115   4182         }
  4116   4183   
  4117   4184         break;
  4118   4185       }
  4119   4186   #endif
  4120   4187     }
  4121   4188     sqlite3ReleaseTempReg(pParse, regFree1);
................................................................................
  4172   4239   **
  4173   4240   ** If pExpr is a constant, then this routine might generate this
  4174   4241   ** code to fill the register in the initialization section of the
  4175   4242   ** VDBE program, in order to factor it out of the evaluation loop.
  4176   4243   */
  4177   4244   int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
  4178   4245     int r2;
  4179         -  pExpr = sqlite3ExprSkipCollate(pExpr);
         4246  +  pExpr = sqlite3ExprSkipCollateAndLikely(pExpr);
  4180   4247     if( ConstFactorOk(pParse)
  4181   4248      && pExpr->op!=TK_REGISTER
  4182   4249      && sqlite3ExprIsConstantNotJoin(pExpr)
  4183   4250     ){
  4184   4251       *pReg  = 0;
  4185   4252       r2 = sqlite3ExprCodeAtInit(pParse, pExpr, -1);
  4186   4253     }else{
................................................................................
  4363   4430   static void exprCodeBetween(
  4364   4431     Parse *pParse,    /* Parsing and code generating context */
  4365   4432     Expr *pExpr,      /* The BETWEEN expression */
  4366   4433     int dest,         /* Jump destination or storage location */
  4367   4434     void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
  4368   4435     int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
  4369   4436   ){
  4370         - Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
         4437  +  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
  4371   4438     Expr compLeft;    /* The  x>=y  term */
  4372   4439     Expr compRight;   /* The  x<=z  term */
  4373         -  Expr exprX;       /* The  x  subexpression */
  4374   4440     int regFree1 = 0; /* Temporary use register */
         4441  +  Expr *pDel = 0;
         4442  +  sqlite3 *db = pParse->db;
  4375   4443   
  4376   4444     memset(&compLeft, 0, sizeof(Expr));
  4377   4445     memset(&compRight, 0, sizeof(Expr));
  4378   4446     memset(&exprAnd, 0, sizeof(Expr));
  4379   4447   
  4380   4448     assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  4381         -  exprNodeCopy(&exprX, pExpr->pLeft);
  4382         -  exprAnd.op = TK_AND;
  4383         -  exprAnd.pLeft = &compLeft;
  4384         -  exprAnd.pRight = &compRight;
  4385         -  compLeft.op = TK_GE;
  4386         -  compLeft.pLeft = &exprX;
  4387         -  compLeft.pRight = pExpr->x.pList->a[0].pExpr;
  4388         -  compRight.op = TK_LE;
  4389         -  compRight.pLeft = &exprX;
  4390         -  compRight.pRight = pExpr->x.pList->a[1].pExpr;
  4391         -  exprToRegister(&exprX, exprCodeVector(pParse, &exprX, &regFree1));
  4392         -  if( xJump ){
  4393         -    xJump(pParse, &exprAnd, dest, jumpIfNull);
  4394         -  }else{
  4395         -    /* Mark the expression is being from the ON or USING clause of a join
  4396         -    ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
  4397         -    ** it into the Parse.pConstExpr list.  We should use a new bit for this,
  4398         -    ** for clarity, but we are out of bits in the Expr.flags field so we
  4399         -    ** have to reuse the EP_FromJoin bit.  Bummer. */
  4400         -    exprX.flags |= EP_FromJoin;
  4401         -    sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
  4402         -  }
  4403         -  sqlite3ReleaseTempReg(pParse, regFree1);
         4449  +  pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
         4450  +  if( db->mallocFailed==0 ){
         4451  +    exprAnd.op = TK_AND;
         4452  +    exprAnd.pLeft = &compLeft;
         4453  +    exprAnd.pRight = &compRight;
         4454  +    compLeft.op = TK_GE;
         4455  +    compLeft.pLeft = pDel;
         4456  +    compLeft.pRight = pExpr->x.pList->a[0].pExpr;
         4457  +    compRight.op = TK_LE;
         4458  +    compRight.pLeft = pDel;
         4459  +    compRight.pRight = pExpr->x.pList->a[1].pExpr;
         4460  +    exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
         4461  +    if( xJump ){
         4462  +      xJump(pParse, &exprAnd, dest, jumpIfNull);
         4463  +    }else{
         4464  +      /* Mark the expression is being from the ON or USING clause of a join
         4465  +      ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
         4466  +      ** it into the Parse.pConstExpr list.  We should use a new bit for this,
         4467  +      ** for clarity, but we are out of bits in the Expr.flags field so we
         4468  +      ** have to reuse the EP_FromJoin bit.  Bummer. */
         4469  +      pDel->flags |= EP_FromJoin;
         4470  +      sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
         4471  +    }
         4472  +    sqlite3ReleaseTempReg(pParse, regFree1);
         4473  +  }
         4474  +  sqlite3ExprDelete(db, pDel);
  4404   4475   
  4405   4476     /* Ensure adequate test coverage */
  4406   4477     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1==0 );
  4407   4478     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1!=0 );
  4408   4479     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1==0 );
  4409   4480     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1!=0 );
  4410   4481     testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
................................................................................
  4835   4906       }
  4836   4907       if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
  4837   4908         return 1;
  4838   4909       }
  4839   4910       return 2;
  4840   4911     }
  4841   4912     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
  4842         -    if( pA->op==TK_FUNCTION ){
         4913  +    if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
  4843   4914         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
  4844   4915   #ifndef SQLITE_OMIT_WINDOWFUNC
  4845         -      /* Justification for the assert():
  4846         -      ** window functions have p->op==TK_FUNCTION but aggregate functions
  4847         -      ** have p->op==TK_AGG_FUNCTION.  So any comparison between an aggregate
  4848         -      ** function and a window function should have failed before reaching
  4849         -      ** this point.  And, it is not possible to have a window function and
  4850         -      ** a scalar function with the same name and number of arguments.  So
  4851         -      ** if we reach this point, either A and B both window functions or
  4852         -      ** neither are a window functions. */
  4853         -      assert( ExprHasProperty(pA,EP_WinFunc)==ExprHasProperty(pB,EP_WinFunc) );
         4916  +      assert( pA->op==pB->op );
         4917  +      if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){
         4918  +        return 2;
         4919  +      }
  4854   4920         if( ExprHasProperty(pA,EP_WinFunc) ){
  4855         -        if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
         4921  +        if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
         4922  +          return 2;
         4923  +        }
  4856   4924         }
  4857   4925   #endif
  4858   4926       }else if( pA->op==TK_NULL ){
  4859   4927         return 0;
  4860   4928       }else if( pA->op==TK_COLLATE ){
  4861   4929         if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
  4862   4930       }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
................................................................................
  4902   4970     int i;
  4903   4971     if( pA==0 && pB==0 ) return 0;
  4904   4972     if( pA==0 || pB==0 ) return 1;
  4905   4973     if( pA->nExpr!=pB->nExpr ) return 1;
  4906   4974     for(i=0; i<pA->nExpr; i++){
  4907   4975       Expr *pExprA = pA->a[i].pExpr;
  4908   4976       Expr *pExprB = pB->a[i].pExpr;
  4909         -    if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
         4977  +    if( pA->a[i].sortFlags!=pB->a[i].sortFlags ) return 1;
  4910   4978       if( sqlite3ExprCompare(0, pExprA, pExprB, iTab) ) return 1;
  4911   4979     }
  4912   4980     return 0;
  4913   4981   }
  4914   4982   
  4915   4983   /*
  4916   4984   ** Like sqlite3ExprCompare() except COLLATE operators at the top-level
  4917   4985   ** are ignored.
  4918   4986   */
  4919   4987   int sqlite3ExprCompareSkip(Expr *pA, Expr *pB, int iTab){
  4920   4988     return sqlite3ExprCompare(0,
  4921         -             sqlite3ExprSkipCollate(pA),
  4922         -             sqlite3ExprSkipCollate(pB),
         4989  +             sqlite3ExprSkipCollateAndLikely(pA),
         4990  +             sqlite3ExprSkipCollateAndLikely(pB),
  4923   4991                iTab);
  4924   4992   }
  4925   4993   
  4926   4994   /*
  4927   4995   ** Return non-zero if Expr p can only be true if pNN is not NULL.
         4996  +**
         4997  +** Or if seenNot is true, return non-zero if Expr p can only be
         4998  +** non-NULL if pNN is not NULL
  4928   4999   */
  4929   5000   static int exprImpliesNotNull(
  4930   5001     Parse *pParse,      /* Parsing context */
  4931   5002     Expr *p,            /* The expression to be checked */
  4932   5003     Expr *pNN,          /* The expression that is NOT NULL */
  4933   5004     int iTab,           /* Table being evaluated */
  4934         -  int seenNot         /* True if p is an operand of NOT */
         5005  +  int seenNot         /* Return true only if p can be any non-NULL value */
  4935   5006   ){
  4936   5007     assert( p );
  4937   5008     assert( pNN );
  4938         -  if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ) return 1;
         5009  +  if( sqlite3ExprCompare(pParse, p, pNN, iTab)==0 ){
         5010  +    return pNN->op!=TK_NULL;
         5011  +  }
  4939   5012     switch( p->op ){
  4940   5013       case TK_IN: {
  4941   5014         if( seenNot && ExprHasProperty(p, EP_xIsSelect) ) return 0;
  4942   5015         assert( ExprHasProperty(p,EP_xIsSelect)
  4943   5016              || (p->x.pList!=0 && p->x.pList->nExpr>0) );
  4944         -      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
         5017  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
  4945   5018       }
  4946   5019       case TK_BETWEEN: {
  4947   5020         ExprList *pList = p->x.pList;
  4948   5021         assert( pList!=0 );
  4949   5022         assert( pList->nExpr==2 );
  4950   5023         if( seenNot ) return 0;
  4951         -      if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, seenNot)
  4952         -       || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, seenNot)
         5024  +      if( exprImpliesNotNull(pParse, pList->a[0].pExpr, pNN, iTab, 1)
         5025  +       || exprImpliesNotNull(pParse, pList->a[1].pExpr, pNN, iTab, 1)
  4953   5026         ){
  4954   5027           return 1;
  4955   5028         }
  4956         -      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
         5029  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
  4957   5030       }
  4958   5031       case TK_EQ:
  4959   5032       case TK_NE:
  4960   5033       case TK_LT:
  4961   5034       case TK_LE:
  4962   5035       case TK_GT:
  4963   5036       case TK_GE:
  4964   5037       case TK_PLUS:
  4965   5038       case TK_MINUS:
         5039  +    case TK_BITOR:
         5040  +    case TK_LSHIFT:
         5041  +    case TK_RSHIFT: 
         5042  +    case TK_CONCAT: 
         5043  +      seenNot = 1;
         5044  +      /* Fall thru */
  4966   5045       case TK_STAR:
  4967   5046       case TK_REM:
  4968   5047       case TK_BITAND:
  4969         -    case TK_BITOR:
  4970         -    case TK_SLASH:
  4971         -    case TK_LSHIFT:
  4972         -    case TK_RSHIFT: 
  4973         -    case TK_CONCAT: {
         5048  +    case TK_SLASH: {
  4974   5049         if( exprImpliesNotNull(pParse, p->pRight, pNN, iTab, seenNot) ) return 1;
  4975   5050         /* Fall thru into the next case */
  4976   5051       }
  4977   5052       case TK_SPAN:
  4978   5053       case TK_COLLATE:
  4979         -    case TK_BITNOT:
  4980   5054       case TK_UPLUS:
  4981   5055       case TK_UMINUS: {
  4982   5056         return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
  4983   5057       }
  4984   5058       case TK_TRUTH: {
  4985   5059         if( seenNot ) return 0;
  4986   5060         if( p->op2!=TK_IS ) return 0;
  4987         -      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, seenNot);
         5061  +      return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
  4988   5062       }
         5063  +    case TK_BITNOT:
  4989   5064       case TK_NOT: {
  4990   5065         return exprImpliesNotNull(pParse, p->pLeft, pNN, iTab, 1);
  4991   5066       }
  4992   5067     }
  4993   5068     return 0;
  4994   5069   }
  4995   5070   
................................................................................
  5047   5122   */
  5048   5123   static int impliesNotNullRow(Walker *pWalker, Expr *pExpr){
  5049   5124     testcase( pExpr->op==TK_AGG_COLUMN );
  5050   5125     testcase( pExpr->op==TK_AGG_FUNCTION );
  5051   5126     if( ExprHasProperty(pExpr, EP_FromJoin) ) return WRC_Prune;
  5052   5127     switch( pExpr->op ){
  5053   5128       case TK_ISNOT:
  5054         -    case TK_NOT:
  5055   5129       case TK_ISNULL:
  5056   5130       case TK_NOTNULL:
  5057   5131       case TK_IS:
  5058   5132       case TK_OR:
  5059   5133       case TK_CASE:
  5060   5134       case TK_IN:
  5061   5135       case TK_FUNCTION:
  5062   5136         testcase( pExpr->op==TK_ISNOT );
  5063         -      testcase( pExpr->op==TK_NOT );
  5064   5137         testcase( pExpr->op==TK_ISNULL );
  5065   5138         testcase( pExpr->op==TK_NOTNULL );
  5066   5139         testcase( pExpr->op==TK_IS );
  5067   5140         testcase( pExpr->op==TK_OR );
  5068   5141         testcase( pExpr->op==TK_CASE );
  5069   5142         testcase( pExpr->op==TK_IN );
  5070   5143         testcase( pExpr->op==TK_FUNCTION );
................................................................................
  5072   5145       case TK_COLUMN:
  5073   5146         if( pWalker->u.iCur==pExpr->iTable ){
  5074   5147           pWalker->eCode = 1;
  5075   5148           return WRC_Abort;
  5076   5149         }
  5077   5150         return WRC_Prune;
  5078   5151   
         5152  +    case TK_AND:
         5153  +      if( sqlite3ExprImpliesNonNullRow(pExpr->pLeft, pWalker->u.iCur)
         5154  +       && sqlite3ExprImpliesNonNullRow(pExpr->pRight, pWalker->u.iCur)
         5155  +      ){
         5156  +        pWalker->eCode = 1;
         5157  +      }
         5158  +      return WRC_Prune;
         5159  +
         5160  +    case TK_BETWEEN:
         5161  +      sqlite3WalkExpr(pWalker, pExpr->pLeft);
         5162  +      return WRC_Prune;
         5163  +
  5079   5164       /* Virtual tables are allowed to use constraints like x=NULL.  So
  5080   5165       ** a term of the form x=y does not prove that y is not null if x
  5081   5166       ** is the column of a virtual table */
  5082   5167       case TK_EQ:
  5083   5168       case TK_NE:
  5084   5169       case TK_LT:
  5085   5170       case TK_LE:
................................................................................
  5092   5177         testcase( pExpr->op==TK_GT );
  5093   5178         testcase( pExpr->op==TK_GE );
  5094   5179         if( (pExpr->pLeft->op==TK_COLUMN && IsVirtual(pExpr->pLeft->y.pTab))
  5095   5180          || (pExpr->pRight->op==TK_COLUMN && IsVirtual(pExpr->pRight->y.pTab))
  5096   5181         ){
  5097   5182          return WRC_Prune;
  5098   5183         }
         5184  +
  5099   5185       default:
  5100   5186         return WRC_Continue;
  5101   5187     }
  5102   5188   }
  5103   5189   
  5104   5190   /*
  5105   5191   ** Return true (non-zero) if expression p can only be true if at least
................................................................................
  5121   5207   ** an ordinary JOIN.  The p argument is the WHERE clause.  If the WHERE
  5122   5208   ** clause requires that some column of the right table of the LEFT JOIN
  5123   5209   ** be non-NULL, then the LEFT JOIN can be safely converted into an
  5124   5210   ** ordinary join.
  5125   5211   */
  5126   5212   int sqlite3ExprImpliesNonNullRow(Expr *p, int iTab){
  5127   5213     Walker w;
  5128         -  p = sqlite3ExprSkipCollate(p);
         5214  +  p = sqlite3ExprSkipCollateAndLikely(p);
  5129   5215     while( p ){
  5130   5216       if( p->op==TK_NOTNULL ){
  5131   5217         p = p->pLeft;
  5132   5218       }else if( p->op==TK_AND ){
  5133   5219         if( sqlite3ExprImpliesNonNullRow(p->pLeft, iTab) ) return 1;
  5134   5220         p = p->pRight;
  5135   5221       }else{
................................................................................
  5227   5313       SrcList *pSrc = p->pSrc;
  5228   5314       int nSrc = pSrc ? pSrc->nSrc : 0;
  5229   5315       for(i=0; i<nSrc; i++){
  5230   5316         if( pExpr->iTable==pSrc->a[i].iCursor ) break;
  5231   5317       }
  5232   5318       if( i<nSrc ){
  5233   5319         p->nThis++;
  5234         -    }else{
         5320  +    }else if( nSrc==0 || pExpr->iTable<pSrc->a[0].iCursor ){
         5321  +      /* In a well-formed parse tree (no name resolution errors),
         5322  +      ** TK_COLUMN nodes with smaller Expr.iTable values are in an
         5323  +      ** outer context.  Those are the only ones to count as "other" */
  5235   5324         p->nOther++;
  5236   5325       }
  5237   5326     }
  5238   5327     return WRC_Continue;
  5239   5328   }
  5240   5329   
  5241   5330   /*
................................................................................
  5244   5333   ** has no arguments or has only constant arguments.  Return false if pExpr
  5245   5334   ** references columns but not columns of tables found in pSrcList.
  5246   5335   */
  5247   5336   int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
  5248   5337     Walker w;
  5249   5338     struct SrcCount cnt;
  5250   5339     assert( pExpr->op==TK_AGG_FUNCTION );
         5340  +  memset(&w, 0, sizeof(w));
  5251   5341     w.xExprCallback = exprSrcCount;
  5252         -  w.xSelectCallback = 0;
         5342  +  w.xSelectCallback = sqlite3SelectWalkNoop;
  5253   5343     w.u.pSrcCount = &cnt;
  5254   5344     cnt.pSrc = pSrcList;
  5255   5345     cnt.nThis = 0;
  5256   5346     cnt.nOther = 0;
  5257   5347     sqlite3WalkExprList(&w, pExpr->x.pList);
  5258   5348     return cnt.nThis>0 || cnt.nOther==0;
  5259   5349   }

Changes to src/fkey.c.

   474    474     sqlite3 *db = pParse->db;
   475    475   
   476    476     pExpr = sqlite3Expr(db, TK_REGISTER, 0);
   477    477     if( pExpr ){
   478    478       if( iCol>=0 && iCol!=pTab->iPKey ){
   479    479         pCol = &pTab->aCol[iCol];
   480    480         pExpr->iTable = regBase + iCol + 1;
   481         -      pExpr->affinity = pCol->affinity;
          481  +      pExpr->affExpr = pCol->affinity;
   482    482         zColl = pCol->zColl;
   483    483         if( zColl==0 ) zColl = db->pDfltColl->zName;
   484    484         pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
   485    485       }else{
   486    486         pExpr->iTable = regBase;
   487         -      pExpr->affinity = SQLITE_AFF_INTEGER;
          487  +      pExpr->affExpr = SQLITE_AFF_INTEGER;
   488    488       }
   489    489     }
   490    490     return pExpr;
   491    491   }
   492    492   
   493    493   /*
   494    494   ** Return an Expr object that refers to column iCol of table pTab which
................................................................................
  1283   1283         Token tFrom;
  1284   1284         Expr *pRaise; 
  1285   1285   
  1286   1286         tFrom.z = zFrom;
  1287   1287         tFrom.n = nFrom;
  1288   1288         pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
  1289   1289         if( pRaise ){
  1290         -        pRaise->affinity = OE_Abort;
         1290  +        pRaise->affExpr = OE_Abort;
  1291   1291         }
  1292   1292         pSelect = sqlite3SelectNew(pParse, 
  1293   1293             sqlite3ExprListAppend(pParse, 0, pRaise),
  1294   1294             sqlite3SrcListAppend(pParse, 0, &tFrom, 0),
  1295   1295             pWhere,
  1296   1296             0, 0, 0, 0, 0
  1297   1297         );
................................................................................
  1328   1328       sqlite3ExprListDelete(db, pList);
  1329   1329       sqlite3SelectDelete(db, pSelect);
  1330   1330       if( db->mallocFailed==1 ){
  1331   1331         fkTriggerDelete(db, pTrigger);
  1332   1332         return 0;
  1333   1333       }
  1334   1334       assert( pStep!=0 );
         1335  +    assert( pTrigger!=0 );
  1335   1336   
  1336   1337       switch( action ){
  1337   1338         case OE_Restrict:
  1338   1339           pStep->op = TK_SELECT; 
  1339   1340           break;
  1340   1341         case OE_Cascade: 
  1341   1342           if( !pChanges ){ 

Changes to src/func.c.

  1984   1984       FUNCTION(coalesce,           0, 0, 0, 0                ),
  1985   1985       FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
  1986   1986     };
  1987   1987   #ifndef SQLITE_OMIT_ALTERTABLE
  1988   1988     sqlite3AlterFunctions();
  1989   1989   #endif
  1990   1990     sqlite3WindowFunctions();
  1991         -#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
  1992         -  sqlite3AnalyzeFunctions();
  1993         -#endif
  1994   1991     sqlite3RegisterDateTimeFunctions();
  1995   1992     sqlite3InsertBuiltinFuncs(aBuiltinFunc, ArraySize(aBuiltinFunc));
  1996   1993   
  1997   1994   #if 0  /* Enable to print out how the built-in functions are hashed */
  1998   1995     {
  1999   1996       int i;
  2000   1997       FuncDef *p;

Changes to src/global.c.

   210    210   SQLITE_WSD struct Sqlite3Config sqlite3Config = {
   211    211      SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
   212    212      1,                         /* bCoreMutex */
   213    213      SQLITE_THREADSAFE==1,      /* bFullMutex */
   214    214      SQLITE_USE_URI,            /* bOpenUri */
   215    215      SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
   216    216      0,                         /* bSmallMalloc */
          217  +   1,                         /* bExtraSchemaChecks */
   217    218      0x7ffffffe,                /* mxStrlen */
   218    219      0,                         /* neverCorrupt */
   219    220      SQLITE_DEFAULT_LOOKASIDE,  /* szLookaside, nLookaside */
   220    221      SQLITE_STMTJRNL_SPILL,     /* nStmtSpill */
   221    222      {0,0,0,0,0,0,0,0},         /* m */
   222    223      {0,0,0,0,0,0,0,0,0},       /* mutex */
   223    224      {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
................................................................................
   256    257   #ifndef SQLITE_UNTESTABLE
   257    258      0,                         /* xTestCallback */
   258    259   #endif
   259    260      0,                         /* bLocaltimeFault */
   260    261      0,                         /* bInternalFunctions */
   261    262      0x7ffffffe,                /* iOnceResetThreshold */
   262    263      SQLITE_DEFAULT_SORTERREF_SIZE,   /* szSorterRef */
          264  +   0,                         /* iPrngSeed */
   263    265   };
   264    266   
   265    267   /*
   266    268   ** Hash table for global functions - functions common to all
   267    269   ** database connections.  After initialization, this table is
   268    270   ** read-only.
   269    271   */

Changes to src/insert.c.

    84     84       pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
    85     85       if( !pIdx->zColAff ){
    86     86         sqlite3OomFault(db);
    87     87         return 0;
    88     88       }
    89     89       for(n=0; n<pIdx->nColumn; n++){
    90     90         i16 x = pIdx->aiColumn[n];
           91  +      char aff;
    91     92         if( x>=0 ){
    92         -        pIdx->zColAff[n] = pTab->aCol[x].affinity;
           93  +        aff = pTab->aCol[x].affinity;
    93     94         }else if( x==XN_ROWID ){
    94         -        pIdx->zColAff[n] = SQLITE_AFF_INTEGER;
           95  +        aff = SQLITE_AFF_INTEGER;
    95     96         }else{
    96         -        char aff;
    97     97           assert( x==XN_EXPR );
    98     98           assert( pIdx->aColExpr!=0 );
    99     99           aff = sqlite3ExprAffinity(pIdx->aColExpr->a[n].pExpr);
   100         -        if( aff==0 ) aff = SQLITE_AFF_BLOB;
   101         -        pIdx->zColAff[n] = aff;
   102    100         }
          101  +      if( aff<SQLITE_AFF_BLOB ) aff = SQLITE_AFF_BLOB;
          102  +      if( aff>SQLITE_AFF_NUMERIC) aff = SQLITE_AFF_NUMERIC;
          103  +      pIdx->zColAff[n] = aff;
   103    104       }
   104    105       pIdx->zColAff[n] = 0;
   105    106     }
   106    107    
   107    108     return pIdx->zColAff;
   108    109   }
   109    110   
................................................................................
   135    136       zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
   136    137       if( !zColAff ){
   137    138         sqlite3OomFault(db);
   138    139         return;
   139    140       }
   140    141   
   141    142       for(i=0; i<pTab->nCol; i++){
          143  +      assert( pTab->aCol[i].affinity!=0 );
   142    144         zColAff[i] = pTab->aCol[i].affinity;
   143    145       }
   144    146       do{
   145    147         zColAff[i--] = 0;
   146         -    }while( i>=0 && zColAff[i]==SQLITE_AFF_BLOB );
          148  +    }while( i>=0 && zColAff[i]<=SQLITE_AFF_BLOB );
   147    149       pTab->zColAff = zColAff;
   148    150     }
   149    151     assert( zColAff!=0 );
   150    152     i = sqlite3Strlen30NN(zColAff);
   151    153     if( i ){
   152    154       if( iReg ){
   153    155         sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
................................................................................
   827    829     }
   828    830   #ifndef SQLITE_OMIT_UPSERT
   829    831     if( pUpsert ){
   830    832       if( IsVirtual(pTab) ){
   831    833         sqlite3ErrorMsg(pParse, "UPSERT not implemented for virtual table \"%s\"",
   832    834                 pTab->zName);
   833    835         goto insert_cleanup;
          836  +    }
          837  +    if( sqlite3HasExplicitNulls(pParse, pUpsert->pUpsertTarget) ){
          838  +      goto insert_cleanup;
   834    839       }
   835    840       pTabList->a[0].iCursor = iDataCur;
   836    841       pUpsert->pUpsertSrc = pTabList;
   837    842       pUpsert->regData = regData;
   838    843       pUpsert->iDataCur = iDataCur;
   839    844       pUpsert->iIdxCur = iIdxCur;
   840    845       if( pUpsert->pUpsertTarget ){

Changes to src/loadext.c.

   457    457   #ifdef SQLITE_ENABLE_NORMALIZE
   458    458     sqlite3_normalized_sql,
   459    459   #else
   460    460     0,
   461    461   #endif
   462    462     /* Version 3.28.0 and later */
   463    463     sqlite3_stmt_isexplain,
   464         -  sqlite3_value_frombind
          464  +  sqlite3_value_frombind,
          465  +  /* Version 3.30.0 and later */
          466  +  sqlite3_drop_modules,
   465    467   };
   466    468   
   467    469   /*
   468    470   ** Attempt to load an SQLite extension library contained in the file
   469    471   ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   470    472   ** default entry point name (sqlite3_extension_init) is used.  Use
   471    473   ** of the default name is recommended.

Changes to src/main.c.

   832    832       default: {
   833    833         static const struct {
   834    834           int op;      /* The opcode */
   835    835           u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
   836    836         } aFlagOp[] = {
   837    837           { SQLITE_DBCONFIG_ENABLE_FKEY,           SQLITE_ForeignKeys    },
   838    838           { SQLITE_DBCONFIG_ENABLE_TRIGGER,        SQLITE_EnableTrigger  },
          839  +        { SQLITE_DBCONFIG_ENABLE_VIEW,           SQLITE_EnableView     },
   839    840           { SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, SQLITE_Fts3Tokenizer  },
   840    841           { SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, SQLITE_LoadExtension  },
   841    842           { SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE,      SQLITE_NoCkptOnClose  },
   842    843           { SQLITE_DBCONFIG_ENABLE_QPSG,           SQLITE_EnableQPSG     },
   843    844           { SQLITE_DBCONFIG_TRIGGER_EQP,           SQLITE_TriggerEQP     },
   844    845           { SQLITE_DBCONFIG_RESET_DATABASE,        SQLITE_ResetDatabase  },
   845    846           { SQLITE_DBCONFIG_DEFENSIVE,             SQLITE_Defensive      },
................................................................................
  1231   1232       }
  1232   1233       sqlite3DbFree(db, pColl);
  1233   1234     }
  1234   1235     sqlite3HashClear(&db->aCollSeq);
  1235   1236   #ifndef SQLITE_OMIT_VIRTUALTABLE
  1236   1237     for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
  1237   1238       Module *pMod = (Module *)sqliteHashData(i);
  1238         -    if( pMod->xDestroy ){
  1239         -      pMod->xDestroy(pMod->pAux);
  1240         -    }
  1241   1239       sqlite3VtabEponymousTableClear(db, pMod);
  1242         -    sqlite3DbFree(db, pMod);
         1240  +    sqlite3VtabModuleUnref(db, pMod);
  1243   1241     }
  1244   1242     sqlite3HashClear(&db->aModule);
  1245   1243   #endif
  1246   1244   
  1247   1245     sqlite3Error(db, SQLITE_OK); /* Deallocates any cached error strings. */
  1248   1246     sqlite3ValueFree(db->pErr);
  1249   1247     sqlite3CloseExtensions(db);
................................................................................
  1716   1714      || (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG)
  1717   1715      || (255<(nName = sqlite3Strlen30( zFunctionName)))
  1718   1716     ){
  1719   1717       return SQLITE_MISUSE_BKPT;
  1720   1718     }
  1721   1719   
  1722   1720     assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
  1723         -  extraFlags = enc &  SQLITE_DETERMINISTIC;
         1721  +  assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
         1722  +  extraFlags = enc &  (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|SQLITE_SUBTYPE);
  1724   1723     enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
  1725   1724     
  1726   1725   #ifndef SQLITE_OMIT_UTF16
  1727   1726     /* If SQLITE_UTF16 is specified as the encoding type, transform this
  1728   1727     ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
  1729   1728     ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
  1730   1729     **
................................................................................
  1779   1778   
  1780   1779     if( pDestructor ){
  1781   1780       pDestructor->nRef++;
  1782   1781     }
  1783   1782     p->u.pDestructor = pDestructor;
  1784   1783     p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
  1785   1784     testcase( p->funcFlags & SQLITE_DETERMINISTIC );
         1785  +  testcase( p->funcFlags & SQLITE_DIRECTONLY );
  1786   1786     p->xSFunc = xSFunc ? xSFunc : xStep;
  1787   1787     p->xFinalize = xFinal;
  1788   1788     p->xValue = xValue;
  1789   1789     p->xInverse = xInverse;
  1790   1790     p->pUserData = pUserData;
  1791   1791     p->nArg = (u16)nArg;
  1792   1792     return SQLITE_OK;
................................................................................
  3071   3071     db->autoCommit = 1;
  3072   3072     db->nextAutovac = -1;
  3073   3073     db->szMmap = sqlite3GlobalConfig.szMmap;
  3074   3074     db->nextPagesize = 0;
  3075   3075     db->nMaxSorterMmap = 0x7FFFFFFF;
  3076   3076     db->flags |= SQLITE_ShortColNames
  3077   3077                    | SQLITE_EnableTrigger
         3078  +                 | SQLITE_EnableView
  3078   3079                    | SQLITE_CacheSpill
  3079   3080   
  3080   3081   /* The SQLITE_DQS compile-time option determines the default settings
  3081   3082   ** for SQLITE_DBCONFIG_DQS_DDL and SQLITE_DBCONFIG_DQS_DML.
  3082   3083   **
  3083   3084   **    SQLITE_DQS     SQLITE_DBCONFIG_DQS_DDL    SQLITE_DBCONFIG_DQS_DML
  3084   3085   **    ----------     -----------------------    -----------------------
................................................................................
  3820   3821       ** this verb acts like PRNG_RESET.
  3821   3822       */
  3822   3823       case SQLITE_TESTCTRL_PRNG_RESTORE: {
  3823   3824         sqlite3PrngRestoreState();
  3824   3825         break;
  3825   3826       }
  3826   3827   
  3827         -    /*
  3828         -    ** Reset the PRNG back to its uninitialized state.  The next call
  3829         -    ** to sqlite3_randomness() will reseed the PRNG using a single call
  3830         -    ** to the xRandomness method of the default VFS.
         3828  +    /*  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, int x, sqlite3 *db);
         3829  +    **
         3830  +    ** Control the seed for the pseudo-random number generator (PRNG) that
         3831  +    ** is built into SQLite.  Cases:
         3832  +    **
         3833  +    **    x!=0 && db!=0       Seed the PRNG to the current value of the
         3834  +    **                        schema cookie in the main database for db, or
         3835  +    **                        x if the schema cookie is zero.  This case
         3836  +    **                        is convenient to use with database fuzzers
         3837  +    **                        as it allows the fuzzer some control over the
         3838  +    **                        the PRNG seed.
         3839  +    **
         3840  +    **    x!=0 && db==0       Seed the PRNG to the value of x.
         3841  +    **
         3842  +    **    x==0 && db==0       Revert to default behavior of using the
         3843  +    **                        xRandomness method on the primary VFS.
         3844  +    **
         3845  +    ** This test-control also resets the PRNG so that the new seed will
         3846  +    ** be used for the next call to sqlite3_randomness().
  3831   3847       */
  3832         -    case SQLITE_TESTCTRL_PRNG_RESET: {
         3848  +    case SQLITE_TESTCTRL_PRNG_SEED: {
         3849  +      int x = va_arg(ap, int);
         3850  +      int y;
         3851  +      sqlite3 *db = va_arg(ap, sqlite3*);
         3852  +      assert( db==0 || db->aDb[0].pSchema!=0 );
         3853  +      if( db && (y = db->aDb[0].pSchema->schema_cookie)!=0 ){ x = y; }
         3854  +      sqlite3Config.iPrngSeed = x;
  3833   3855         sqlite3_randomness(0,0);
  3834   3856         break;
  3835   3857       }
  3836   3858   
  3837   3859       /*
  3838   3860       **  sqlite3_test_control(BITVEC_TEST, size, program)
  3839   3861       **
................................................................................
  4037   4059       ** testing causes certain assert() statements in the code to be activated
  4038   4060       ** that demonstrat invariants on well-formed database files.
  4039   4061       */
  4040   4062       case SQLITE_TESTCTRL_NEVER_CORRUPT: {
  4041   4063         sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
  4042   4064         break;
  4043   4065       }
         4066  +
         4067  +    /*   sqlite3_test_control(SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS, int);
         4068  +    **
         4069  +    ** Set or clear a flag that causes SQLite to verify that type, name,
         4070  +    ** and tbl_name fields of the sqlite_master table.  This is normally
         4071  +    ** on, but it is sometimes useful to turn it off for testing.
         4072  +    */
         4073  +    case SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS: {
         4074  +      sqlite3GlobalConfig.bExtraSchemaChecks = va_arg(ap, int);
         4075  +      break;
         4076  +    }
  4044   4077   
  4045   4078       /* Set the threshold at which OP_Once counters reset back to zero.
  4046   4079       ** By default this is 0x7ffffffe (over 2 billion), but that value is
  4047   4080       ** too big to test in a reasonable amount of time, so this control is
  4048   4081       ** provided to set a small and easily reachable reset value.
  4049   4082       */
  4050   4083       case SQLITE_TESTCTRL_ONCE_RESET_THRESHOLD: {

Changes to src/memjournal.c.

    92     92   ){
    93     93     MemJournal *p = (MemJournal *)pJfd;
    94     94     u8 *zOut = zBuf;
    95     95     int nRead = iAmt;
    96     96     int iChunkOffset;
    97     97     FileChunk *pChunk;
    98     98   
    99         -#if defined(SQLITE_ENABLE_ATOMIC_WRITE) \
   100         - || defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
   101     99     if( (iAmt+iOfst)>p->endpoint.iOffset ){
   102    100       return SQLITE_IOERR_SHORT_READ;
   103    101     }
   104         -#endif
   105         -
   106         -  assert( (iAmt+iOfst)<=p->endpoint.iOffset );
   107    102     assert( p->readpoint.iOffset==0 || p->readpoint.pChunk!=0 );
   108    103     if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
   109    104       sqlite3_int64 iOff = 0;
   110    105       for(pChunk=p->pFirst; 
   111    106           ALWAYS(pChunk) && (iOff+p->nChunkSize)<=iOfst;
   112    107           pChunk=pChunk->pNext
   113    108       ){

Changes to src/os.c.

   254    254     return pVfs->xDlSym(pVfs, pHdle, zSym);
   255    255   }
   256    256   void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
   257    257     pVfs->xDlClose(pVfs, pHandle);
   258    258   }
   259    259   #endif /* SQLITE_OMIT_LOAD_EXTENSION */
   260    260   int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
   261         -  return pVfs->xRandomness(pVfs, nByte, zBufOut);
          261  +  if( sqlite3Config.iPrngSeed ){
          262  +    memset(zBufOut, 0, nByte);
          263  +    if( ALWAYS(nByte>(signed)sizeof(unsigned)) ) nByte = sizeof(unsigned int);
          264  +    memcpy(zBufOut, &sqlite3Config.iPrngSeed, nByte);
          265  +    return SQLITE_OK;
          266  +  }else{
          267  +    return pVfs->xRandomness(pVfs, nByte, zBufOut);
          268  +  }
          269  +  
   262    270   }
   263    271   int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
   264    272     return pVfs->xSleep(pVfs, nMicro);
   265    273   }
   266    274   int sqlite3OsGetLastError(sqlite3_vfs *pVfs){
   267    275     return pVfs->xGetLastError ? pVfs->xGetLastError(pVfs, 0, 0) : 0;
   268    276   }

Changes to src/os_unix.c.

   527    527     { "lstat",         (sqlite3_syscall_ptr)0,              0 },
   528    528   #endif
   529    529   #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
   530    530   
   531    531   #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
   532    532   # ifdef __ANDROID__
   533    533     { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
          534  +#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
   534    535   # else
   535    536     { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
          537  +#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
   536    538   # endif
   537    539   #else
   538    540     { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
   539    541   #endif
   540         -#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
   541    542   
   542    543   }; /* End of the overrideable system calls */
   543    544   
   544    545   
   545    546   /*
   546    547   ** On some systems, calls to fchown() will trigger a message in a security
   547    548   ** log if they come from non-root processes.  So avoid calling fchown() if
................................................................................
  5958   5959                        || pInode->fileId.ino!=(u64)sStat.st_ino) ){
  5959   5960          pInode = pInode->pNext;
  5960   5961       }
  5961   5962       if( pInode ){
  5962   5963         UnixUnusedFd **pp;
  5963   5964         assert( sqlite3_mutex_notheld(pInode->pLockMutex) );
  5964   5965         sqlite3_mutex_enter(pInode->pLockMutex);
         5966  +      flags &= (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
  5965   5967         for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
  5966   5968         pUnused = *pp;
  5967   5969         if( pUnused ){
  5968   5970           *pp = pUnused->pNext;
  5969   5971         }
  5970   5972         sqlite3_mutex_leave(pInode->pLockMutex);
  5971   5973       }
................................................................................
  6261   6263     assert( fd>=0 );
  6262   6264     if( pOutFlags ){
  6263   6265       *pOutFlags = flags;
  6264   6266     }
  6265   6267   
  6266   6268     if( p->pPreallocatedUnused ){
  6267   6269       p->pPreallocatedUnused->fd = fd;
  6268         -    p->pPreallocatedUnused->flags = flags;
         6270  +    p->pPreallocatedUnused->flags = 
         6271  +                          flags & (SQLITE_OPEN_READONLY|SQLITE_OPEN_READWRITE);
  6269   6272     }
  6270   6273   
  6271   6274     if( isDelete ){
  6272   6275   #if OS_VXWORKS
  6273   6276       zPath = zName;
  6274   6277   #elif defined(SQLITE_UNLINK_AFTER_CLOSE)
  6275   6278       zPath = sqlite3_mprintf("%s", zName);
................................................................................
  7792   7795         }
  7793   7796         return rc;
  7794   7797       }
  7795   7798       default: {
  7796   7799         assert( 0 );  /* The call assures that only valid opcodes are sent */
  7797   7800       }
  7798   7801     }
  7799         -  /*NOTREACHED*/
         7802  +  /*NOTREACHED*/ assert(0);
  7800   7803     return SQLITE_ERROR;
  7801   7804   }
  7802   7805   
  7803   7806   /*
  7804   7807   ** Within this division (the proxying locking implementation) the procedures
  7805   7808   ** above this point are all utilities.  The lock-related methods of the
  7806   7809   ** proxy-locking sqlite3_io_method object follow.

Changes to src/os_win.c.

  4211   4211     DWORD flags = FILE_MAP_WRITE | FILE_MAP_READ;
  4212   4212     int rc = SQLITE_OK;
  4213   4213   
  4214   4214     if( !pShm ){
  4215   4215       rc = winOpenSharedMemory(pDbFd);
  4216   4216       if( rc!=SQLITE_OK ) return rc;
  4217   4217       pShm = pDbFd->pShm;
         4218  +    assert( pShm!=0 );
  4218   4219     }
  4219   4220     pShmNode = pShm->pShmNode;
  4220   4221   
  4221   4222     sqlite3_mutex_enter(pShmNode->mutex);
  4222   4223     if( pShmNode->isUnlocked ){
  4223   4224       rc = winLockSharedMemory(pShmNode);
  4224   4225       if( rc!=SQLITE_OK ) goto shmpage_out;
................................................................................
  4513   4514         if( rc!=SQLITE_OK ){
  4514   4515           OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
  4515   4516                    osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
  4516   4517           return rc;
  4517   4518         }
  4518   4519       }
  4519   4520       if( pFd->mmapSize >= iOff+nAmt ){
         4521  +      assert( pFd->pMapRegion!=0 );
  4520   4522         *pp = &((u8 *)pFd->pMapRegion)[iOff];
  4521   4523         pFd->nFetchOut++;
  4522   4524       }
  4523   4525     }
  4524   4526   #endif
  4525   4527   
  4526   4528     OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",

Changes to src/parse.y.

   223    223   %fallback ID
   224    224     ABORT ACTION AFTER ANALYZE ASC ATTACH BEFORE BEGIN BY CASCADE CAST COLUMNKW
   225    225     CONFLICT DATABASE DEFERRED DESC DETACH DO
   226    226     EACH END EXCLUSIVE EXPLAIN FAIL FOR
   227    227     IGNORE IMMEDIATE INITIALLY INSTEAD LIKE_KW MATCH NO PLAN
   228    228     QUERY KEY OF OFFSET PRAGMA RAISE RECURSIVE RELEASE REPLACE RESTRICT ROW ROWS
   229    229     ROLLBACK SAVEPOINT TEMP TRIGGER VACUUM VIEW VIRTUAL WITH WITHOUT
          230  +  NULLS FIRST LAST
   230    231   %ifdef SQLITE_OMIT_COMPOUND_SELECT
   231    232     EXCEPT INTERSECT UNION
   232    233   %endif SQLITE_OMIT_COMPOUND_SELECT
   233    234   %ifndef SQLITE_OMIT_WINDOWFUNC
   234    235     CURRENT FOLLOWING PARTITION PRECEDING RANGE UNBOUNDED
   235    236     EXCLUDE GROUPS OTHERS TIES
   236    237   %endif SQLITE_OMIT_WINDOWFUNC
................................................................................
   469    470   %include {
   470    471     /*
   471    472     ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
   472    473     ** all elements in the list.  And make sure list length does not exceed
   473    474     ** SQLITE_LIMIT_COMPOUND_SELECT.
   474    475     */
   475    476     static void parserDoubleLinkSelect(Parse *pParse, Select *p){
          477  +    assert( p!=0 );
   476    478       if( p->pPrior ){
   477    479         Select *pNext = 0, *pLoop;
   478    480         int mxSelect, cnt = 0;
   479    481         for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
   480    482           pLoop->pNext = pNext;
   481    483           pLoop->selFlags |= SF_Compound;
   482    484         }
................................................................................
   791    793   // sort order.
   792    794   //
   793    795   %type sortlist {ExprList*}
   794    796   %destructor sortlist {sqlite3ExprListDelete(pParse->db, $$);}
   795    797   
   796    798   orderby_opt(A) ::= .                          {A = 0;}
   797    799   orderby_opt(A) ::= ORDER BY sortlist(X).      {A = X;}
   798         -sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z). {
          800  +sortlist(A) ::= sortlist(A) COMMA expr(Y) sortorder(Z) nulls(X). {
   799    801     A = sqlite3ExprListAppend(pParse,A,Y);
   800         -  sqlite3ExprListSetSortOrder(A,Z);
          802  +  sqlite3ExprListSetSortOrder(A,Z,X);
   801    803   }
   802         -sortlist(A) ::= expr(Y) sortorder(Z). {
          804  +sortlist(A) ::= expr(Y) sortorder(Z) nulls(X). {
   803    805     A = sqlite3ExprListAppend(pParse,0,Y); /*A-overwrites-Y*/
   804         -  sqlite3ExprListSetSortOrder(A,Z);
          806  +  sqlite3ExprListSetSortOrder(A,Z,X);
   805    807   }
   806    808   
   807    809   %type sortorder {int}
   808    810   
   809    811   sortorder(A) ::= ASC.           {A = SQLITE_SO_ASC;}
   810    812   sortorder(A) ::= DESC.          {A = SQLITE_SO_DESC;}
   811    813   sortorder(A) ::= .              {A = SQLITE_SO_UNDEFINED;}
   812    814   
          815  +%type nulls {int}
          816  +nulls(A) ::= NULLS FIRST.       {A = SQLITE_SO_ASC;}
          817  +nulls(A) ::= NULLS LAST.        {A = SQLITE_SO_DESC;}
          818  +nulls(A) ::= .                  {A = SQLITE_SO_UNDEFINED;}
          819  +
   813    820   %type groupby_opt {ExprList*}
   814    821   %destructor groupby_opt {sqlite3ExprListDelete(pParse->db, $$);}
   815    822   groupby_opt(A) ::= .                      {A = 0;}
   816    823   groupby_opt(A) ::= GROUP BY nexprlist(X). {A = X;}
   817    824   
   818    825   %type having_opt {Expr*}
   819    826   %destructor having_opt {sqlite3ExprDelete(pParse->db, $$);}
................................................................................
   959    966     ** that created the expression.
   960    967     */
   961    968     static Expr *tokenExpr(Parse *pParse, int op, Token t){
   962    969       Expr *p = sqlite3DbMallocRawNN(pParse->db, sizeof(Expr)+t.n+1);
   963    970       if( p ){
   964    971         /* memset(p, 0, sizeof(Expr)); */
   965    972         p->op = (u8)op;
   966         -      p->affinity = 0;
          973  +      p->affExpr = 0;
   967    974         p->flags = EP_Leaf;
   968    975         p->iAgg = -1;
   969    976         p->pLeft = p->pRight = 0;
   970    977         p->x.pList = 0;
   971    978         p->pAggInfo = 0;
   972    979         p->y.pTab = 0;
   973    980         p->op2 = 0;
................................................................................
  1055   1062     A = sqlite3ExprFunction(pParse, Y, &X, D);
  1056   1063   }
  1057   1064   expr(A) ::= id(X) LP STAR RP. {
  1058   1065     A = sqlite3ExprFunction(pParse, 0, &X, 0);
  1059   1066   }
  1060   1067   
  1061   1068   %ifndef SQLITE_OMIT_WINDOWFUNC
  1062         -expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP over_clause(Z). {
         1069  +expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
  1063   1070     A = sqlite3ExprFunction(pParse, Y, &X, D);
  1064   1071     sqlite3WindowAttach(pParse, A, Z);
  1065   1072   }
  1066         -expr(A) ::= id(X) LP STAR RP over_clause(Z). {
         1073  +expr(A) ::= id(X) LP STAR RP filter_over(Z). {
  1067   1074     A = sqlite3ExprFunction(pParse, 0, &X, 0);
  1068   1075     sqlite3WindowAttach(pParse, A, Z);
  1069   1076   }
  1070   1077   %endif
  1071   1078   
  1072   1079   term(A) ::= CTIME_KW(OP). {
  1073   1080     A = sqlite3ExprFunction(pParse, 0, &OP, 0);
................................................................................
  1186   1193         **      expr1 NOT IN ()
  1187   1194         **
  1188   1195         ** simplify to constants 0 (false) and 1 (true), respectively,
  1189   1196         ** regardless of the value of expr1.
  1190   1197         */
  1191   1198         sqlite3ExprUnmapAndDelete(pParse, A);
  1192   1199         A = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[N],1);
  1193         -    }else if( Y->nExpr==1 ){
  1194         -      /* Expressions of the form:
  1195         -      **
  1196         -      **      expr1 IN (?1)
  1197         -      **      expr1 NOT IN (?2)
  1198         -      **
  1199         -      ** with exactly one value on the RHS can be simplified to something
  1200         -      ** like this:
  1201         -      **
  1202         -      **      expr1 == ?1
  1203         -      **      expr1 <> ?2
  1204         -      **
  1205         -      ** But, the RHS of the == or <> is marked with the EP_Generic flag
  1206         -      ** so that it may not contribute to the computation of comparison
  1207         -      ** affinity or the collating sequence to use for comparison.  Otherwise,
  1208         -      ** the semantics would be subtly different from IN or NOT IN.
  1209         -      */
  1210         -      Expr *pRHS = Y->a[0].pExpr;
  1211         -      Y->a[0].pExpr = 0;
  1212         -      sqlite3ExprListDelete(pParse->db, Y);
  1213         -      /* pRHS cannot be NULL because a malloc error would have been detected
  1214         -      ** before now and control would have never reached this point */
  1215         -      if( ALWAYS(pRHS) ){
  1216         -        pRHS->flags &= ~EP_Collate;
  1217         -        pRHS->flags |= EP_Generic;
  1218         -      }
  1219         -      A = sqlite3PExpr(pParse, N ? TK_NE : TK_EQ, A, pRHS);
  1220   1200       }else{
  1221   1201         A = sqlite3PExpr(pParse, TK_IN, A, 0);
  1222   1202         if( A ){
  1223   1203           A->x.pList = Y;
  1224   1204           sqlite3ExprSetHeightAndFlags(pParse, A);
  1225   1205         }else{
  1226   1206           sqlite3ExprListDelete(pParse->db, Y);
................................................................................
  1518   1498   trigger_cmd(A) ::= scanpt(B) select(X) scanpt(E).
  1519   1499      {A = sqlite3TriggerSelectStep(pParse->db, X, B, E); /*A-overwrites-X*/}
  1520   1500   
  1521   1501   // The special RAISE expression that may occur in trigger programs
  1522   1502   expr(A) ::= RAISE LP IGNORE RP.  {
  1523   1503     A = sqlite3PExpr(pParse, TK_RAISE, 0, 0); 
  1524   1504     if( A ){
  1525         -    A->affinity = OE_Ignore;
         1505  +    A->affExpr = OE_Ignore;
  1526   1506     }
  1527   1507   }
  1528   1508   expr(A) ::= RAISE LP raisetype(T) COMMA nm(Z) RP.  {
  1529   1509     A = sqlite3ExprAlloc(pParse->db, TK_RAISE, &Z, 1); 
  1530   1510     if( A ) {
  1531         -    A->affinity = (char)T;
         1511  +    A->affExpr = (char)T;
  1532   1512     }
  1533   1513   }
  1534   1514   %endif  !SQLITE_OMIT_TRIGGER
  1535   1515   
  1536   1516   %type raisetype {int}
  1537   1517   raisetype(A) ::= ROLLBACK.  {A = OE_Rollback;}
  1538   1518   raisetype(A) ::= ABORT.     {A = OE_Abort;}
................................................................................
  1668   1648   
  1669   1649   %type frame_opt {Window*}
  1670   1650   %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
  1671   1651   
  1672   1652   %type part_opt {ExprList*}
  1673   1653   %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
  1674   1654   
  1675         -%type filter_opt {Expr*}
  1676         -%destructor filter_opt {sqlite3ExprDelete(pParse->db, $$);}
         1655  +%type filter_clause {Expr*}
         1656  +%destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
         1657  +
         1658  +%type over_clause {Window*}
         1659  +%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
         1660  +
         1661  +%type filter_over {Window*}
         1662  +%destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
  1677   1663   
  1678   1664   %type range_or_rows {int}
  1679   1665   
  1680   1666   %type frame_bound {struct FrameBound}
  1681   1667   %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
  1682   1668   %type frame_bound_s {struct FrameBound}
  1683   1669   %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
................................................................................
  1735   1721   frame_exclude(A) ::= GROUP|TIES(X).  {A = @X; /*A-overwrites-X*/}
  1736   1722   
  1737   1723   
  1738   1724   %type window_clause {Window*}
  1739   1725   %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
  1740   1726   window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
  1741   1727   
  1742         -%type over_clause {Window*}
  1743         -%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
  1744         -over_clause(A) ::= filter_opt(W) OVER LP window(Z) RP. {
         1728  +filter_over(A) ::= filter_clause(F) over_clause(O). {
         1729  +  O->pFilter = F;
         1730  +  A = O;
         1731  +}
         1732  +filter_over(A) ::= over_clause(O). {
         1733  +  A = O;
         1734  +}
         1735  +filter_over(A) ::= filter_clause(F). {
         1736  +  A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
         1737  +  if( A ){
         1738  +    A->eFrmType = TK_FILTER;
         1739  +    A->pFilter = F;
         1740  +  }else{
         1741  +    sqlite3ExprDelete(pParse->db, F);
         1742  +  }
         1743  +}
         1744  +
         1745  +over_clause(A) ::= OVER LP window(Z) RP. {
  1745   1746     A = Z;
  1746   1747     assert( A!=0 );
  1747         -  A->pFilter = W;
  1748   1748   }
  1749         -over_clause(A) ::= filter_opt(W) OVER nm(Z). {
         1749  +over_clause(A) ::= OVER nm(Z). {
  1750   1750     A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
  1751   1751     if( A ){
  1752   1752       A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
  1753         -    A->pFilter = W;
  1754         -  }else{
  1755         -    sqlite3ExprDelete(pParse->db, W);
  1756   1753     }
  1757   1754   }
  1758   1755   
  1759         -filter_opt(A) ::= .                            { A = 0; }
  1760         -filter_opt(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
         1756  +filter_clause(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
  1761   1757   %endif /* SQLITE_OMIT_WINDOWFUNC */
  1762   1758   
  1763   1759   /*
  1764   1760   ** The code generator needs some extra TK_ token values for tokens that
  1765   1761   ** are synthesized and do not actually appear in the grammar:
  1766   1762   */
  1767   1763   %token
  1768         -  TRUEFALSE       /* True or false keyword */
  1769         -  ISNOT           /* Combination of IS and NOT */
  1770         -  FUNCTION        /* A function invocation */
  1771   1764     COLUMN          /* Reference to a table column */
  1772   1765     AGG_FUNCTION    /* An aggregate function */
  1773   1766     AGG_COLUMN      /* An aggregated column */
         1767  +  TRUEFALSE       /* True or false keyword */
         1768  +  ISNOT           /* Combination of IS and NOT */
         1769  +  FUNCTION        /* A function invocation */
  1774   1770     UMINUS          /* Unary minus */
  1775   1771     UPLUS           /* Unary plus */
  1776   1772     TRUTH           /* IS TRUE or IS FALSE or IS NOT TRUE or IS NOT FALSE */
  1777   1773     REGISTER        /* Reference to a VDBE register */
  1778   1774     CONCURRENT      /* BEGIN CONCURRENT */
  1779   1775     VECTOR          /* Vector */
  1780   1776     SELECT_COLUMN   /* Choose a single column from a multi-column SELECT */

Changes to src/pcache.c.

   258    258   */
   259    259   int sqlite3PcacheInitialize(void){
   260    260     if( sqlite3GlobalConfig.pcache2.xInit==0 ){
   261    261       /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
   262    262       ** built-in default page cache is used instead of the application defined
   263    263       ** page cache. */
   264    264       sqlite3PCacheSetDefault();
          265  +    assert( sqlite3GlobalConfig.pcache2.xInit!=0 );
   265    266     }
   266    267     return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
   267    268   }
   268    269   void sqlite3PcacheShutdown(void){
   269    270     if( sqlite3GlobalConfig.pcache2.xShutdown ){
   270    271       /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
   271    272       sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);

Changes to src/pcache1.c.

   420    420   */
   421    421   static PgHdr1 *pcache1AllocPage(PCache1 *pCache, int benignMalloc){
   422    422     PgHdr1 *p = 0;
   423    423     void *pPg;
   424    424   
   425    425     assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
   426    426     if( pCache->pFree || (pCache->nPage==0 && pcache1InitBulk(pCache)) ){
          427  +    assert( pCache->pFree!=0 );
   427    428       p = pCache->pFree;
   428    429       pCache->pFree = p->pNext;
   429    430       p->pNext = 0;
   430    431     }else{
   431    432   #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
   432    433       /* The group mutex must be released before pcache1Alloc() is called. This
   433    434       ** is because it might call sqlite3_release_memory(), which assumes that 

Changes to src/pragma.c.

  1154   1154     break;
  1155   1155   #endif
  1156   1156   
  1157   1157     case PragTyp_INDEX_INFO: if( zRight ){
  1158   1158       Index *pIdx;
  1159   1159       Table *pTab;
  1160   1160       pIdx = sqlite3FindIndex(db, zRight, zDb);
         1161  +    if( pIdx==0 ){
         1162  +      /* If there is no index named zRight, check to see if there is a
         1163  +      ** WITHOUT ROWID table named zRight, and if there is, show the
         1164  +      ** structure of the PRIMARY KEY index for that table. */
         1165  +      pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
         1166  +      if( pTab && !HasRowid(pTab) ){
         1167  +        pIdx = sqlite3PrimaryKeyIndex(pTab);
         1168  +      }
         1169  +    }
  1161   1170       if( pIdx ){
  1162   1171         int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
  1163   1172         int i;
  1164   1173         int mx;
  1165   1174         if( pPragma->iArg ){
  1166   1175           /* PRAGMA index_xinfo (newer version with more rows and columns) */
  1167   1176           mx = pIdx->nColumn;
................................................................................
  1233   1242       for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
  1234   1243         CollSeq *pColl = (CollSeq *)sqliteHashData(p);
  1235   1244         sqlite3VdbeMultiLoad(v, 1, "is", i++, pColl->zName);
  1236   1245       }
  1237   1246     }
  1238   1247     break;
  1239   1248   
  1240         -#ifdef SQLITE_INTROSPECTION_PRAGMAS
         1249  +#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
  1241   1250     case PragTyp_FUNCTION_LIST: {
  1242   1251       int i;
  1243   1252       HashElem *j;
  1244   1253       FuncDef *p;
  1245   1254       pParse->nMem = 2;
  1246   1255       for(i=0; i<SQLITE_FUNC_HASH_SZ; i++){
  1247   1256         for(p=sqlite3BuiltinFunctions.a[i]; p; p=p->u.pHash ){

Changes to src/pragma.h.

   307    307    {/* zName:     */ "fullfsync",
   308    308     /* ePragTyp:  */ PragTyp_FLAG,
   309    309     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
   310    310     /* ColNames:  */ 0, 0,
   311    311     /* iArg:      */ SQLITE_FullFSync },
   312    312   #endif
   313    313   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
   314         -#if defined(SQLITE_INTROSPECTION_PRAGMAS)
          314  +#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
   315    315    {/* zName:     */ "function_list",
   316    316     /* ePragTyp:  */ PragTyp_FUNCTION_LIST,
   317    317     /* ePragFlg:  */ PragFlg_Result0,
   318    318     /* ColNames:  */ 41, 2,
   319    319     /* iArg:      */ 0 },
   320    320   #endif
   321    321   #endif
................................................................................
   431    431     /* ePragTyp:  */ PragTyp_MMAP_SIZE,
   432    432     /* ePragFlg:  */ 0,
   433    433     /* ColNames:  */ 0, 0,
   434    434     /* iArg:      */ 0 },
   435    435   #endif
   436    436   #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
   437    437   #if !defined(SQLITE_OMIT_VIRTUALTABLE)
   438         -#if defined(SQLITE_INTROSPECTION_PRAGMAS)
          438  +#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
   439    439    {/* zName:     */ "module_list",
   440    440     /* ePragTyp:  */ PragTyp_MODULE_LIST,
   441    441     /* ePragFlg:  */ PragFlg_Result0,
   442    442     /* ColNames:  */ 9, 1,
   443    443     /* iArg:      */ 0 },
   444    444   #endif
   445    445   #endif
................................................................................
   475    475    {/* zName:     */ "parser_trace",
   476    476     /* ePragTyp:  */ PragTyp_FLAG,
   477    477     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
   478    478     /* ColNames:  */ 0, 0,
   479    479     /* iArg:      */ SQLITE_ParserTrace },
   480    480   #endif
   481    481   #endif
   482         -#if defined(SQLITE_INTROSPECTION_PRAGMAS)
          482  +#if !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS)
   483    483    {/* zName:     */ "pragma_list",
   484    484     /* ePragTyp:  */ PragTyp_PRAGMA_LIST,
   485    485     /* ePragFlg:  */ PragFlg_Result0,
   486    486     /* ColNames:  */ 9, 1,
   487    487     /* iArg:      */ 0 },
   488    488   #endif
   489    489   #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
................................................................................
   673    673    {/* zName:     */ "writable_schema",
   674    674     /* ePragTyp:  */ PragTyp_FLAG,
   675    675     /* ePragFlg:  */ PragFlg_Result0|PragFlg_NoColumns1,
   676    676     /* ColNames:  */ 0, 0,
   677    677     /* iArg:      */ SQLITE_WriteSchema|SQLITE_NoSchemaError },
   678    678   #endif
   679    679   };
   680         -/* Number of pragmas: 62 on by default, 81 total. */
          680  +/* Number of pragmas: 65 on by default, 81 total. */

Changes to src/prepare.c.

    60     60   /*
    61     61   ** This is the callback routine for the code that initializes the
    62     62   ** database.  See sqlite3Init() below for additional information.
    63     63   ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
    64     64   **
    65     65   ** Each callback contains the following information:
    66     66   **
    67         -**     argv[0] = name of thing being created
    68         -**     argv[1] = root page number for table or index. 0 for trigger or view.
    69         -**     argv[2] = SQL text for the CREATE statement.
           67  +**     argv[0] = type of object: "table", "index", "trigger", or "view".
           68  +**     argv[1] = name of thing being created
           69  +**     argv[2] = associated table if an index or trigger
           70  +**     argv[3] = root page number for table or index. 0 for trigger or view.
           71  +**     argv[4] = SQL text for the CREATE statement.
    70     72   **
    71     73   */
    72     74   int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
    73     75     InitData *pData = (InitData*)pInit;
    74     76     sqlite3 *db = pData->db;
    75     77     int iDb = pData->iDb;
    76     78   
    77         -  assert( argc==3 );
           79  +  assert( argc==5 );
    78     80     UNUSED_PARAMETER2(NotUsed, argc);
    79     81     assert( sqlite3_mutex_held(db->mutex) );
    80     82     DbClearProperty(db, iDb, DB_Empty);
    81     83     pData->nInitRow++;
    82     84     if( db->mallocFailed ){
    83         -    corruptSchema(pData, argv[0], 0);
           85  +    corruptSchema(pData, argv[1], 0);
    84     86       return 1;
    85     87     }
    86     88   
    87     89     assert( iDb>=0 && iDb<db->nDb );
    88     90     if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
    89         -  if( argv[1]==0 ){
    90         -    corruptSchema(pData, argv[0], 0);
    91         -  }else if( sqlite3_strnicmp(argv[2],"create ",7)==0 ){
           91  +  if( argv[3]==0 ){
           92  +    corruptSchema(pData, argv[1], 0);
           93  +  }else if( sqlite3_strnicmp(argv[4],"create ",7)==0 ){
    92     94       /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
    93     95       ** But because db->init.busy is set to 1, no VDBE code is generated
    94     96       ** or executed.  All the parser does is build the internal data
    95     97       ** structures that describe the table, index, or view.
    96     98       */
    97     99       int rc;
    98    100       u8 saved_iDb = db->init.iDb;
    99    101       sqlite3_stmt *pStmt;
   100    102       TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
   101    103   
   102    104       assert( db->init.busy );
   103    105       db->init.iDb = iDb;
   104         -    db->init.newTnum = sqlite3Atoi(argv[1]);
          106  +    db->init.newTnum = sqlite3Atoi(argv[3]);
   105    107       db->init.orphanTrigger = 0;
   106         -    TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
          108  +    db->init.azInit = argv;
          109  +    TESTONLY(rcp = ) sqlite3_prepare(db, argv[4], -1, &pStmt, 0);
   107    110       rc = db->errCode;
   108    111       assert( (rc&0xFF)==(rcp&0xFF) );
   109    112       db->init.iDb = saved_iDb;
   110    113       /* assert( saved_iDb==0 || (db->mDbFlags & DBFLAG_Vacuum)!=0 ); */
   111    114       if( SQLITE_OK!=rc ){
   112    115         if( db->init.orphanTrigger ){
   113    116           assert( iDb==1 );
   114    117         }else{
   115         -        pData->rc = rc;
          118  +        if( rc > pData->rc ) pData->rc = rc;
   116    119           if( rc==SQLITE_NOMEM ){
   117    120             sqlite3OomFault(db);
   118    121           }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
   119         -          corruptSchema(pData, argv[0], sqlite3_errmsg(db));
          122  +          corruptSchema(pData, argv[1], sqlite3_errmsg(db));
   120    123           }
   121    124         }
   122    125       }
   123    126       sqlite3_finalize(pStmt);
   124         -  }else if( argv[0]==0 || (argv[2]!=0 && argv[2][0]!=0) ){
   125         -    corruptSchema(pData, argv[0], 0);
          127  +  }else if( argv[1]==0 || (argv[4]!=0 && argv[4][0]!=0) ){
          128  +    corruptSchema(pData, argv[1], 0);
   126    129     }else{
   127    130       /* If the SQL column is blank it means this is an index that
   128    131       ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
   129    132       ** constraint for a CREATE TABLE.  The index should have already
   130    133       ** been created when we processed the CREATE TABLE.  All we have
   131    134       ** to do here is record the root page number for that index.
   132    135       */
   133    136       Index *pIndex;
   134         -    pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zDbSName);
          137  +    pIndex = sqlite3FindIndex(db, argv[1], db->aDb[iDb].zDbSName);
   135    138       if( pIndex==0
   136         -     || sqlite3GetInt32(argv[1],&pIndex->tnum)==0
          139  +     || sqlite3GetInt32(argv[3],&pIndex->tnum)==0
   137    140        || pIndex->tnum<2
   138    141        || sqlite3IndexHasDuplicateRootPage(pIndex)
   139    142       ){
   140         -      corruptSchema(pData, argv[0], pIndex?"invalid rootpage":"orphan index");
          143  +      corruptSchema(pData, argv[1], pIndex?"invalid rootpage":"orphan index");
   141    144       }
   142    145     }
   143    146     return 0;
   144    147   }
   145    148   
   146    149   /*
   147    150   ** Attempt to read the database schema and initialize internal
................................................................................
   154    157   int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg, u32 mFlags){
   155    158     int rc;
   156    159     int i;
   157    160   #ifndef SQLITE_OMIT_DEPRECATED
   158    161     int size;
   159    162   #endif
   160    163     Db *pDb;
   161         -  char const *azArg[4];
          164  +  char const *azArg[6];
   162    165     int meta[5];
   163    166     InitData initData;
   164    167     const char *zMasterName;
   165    168     int openedTransaction = 0;
   166    169   
   167    170     assert( (db->mDbFlags & DBFLAG_SchemaKnownOk)==0 );
   168    171     assert( iDb>=0 && iDb<db->nDb );
................................................................................
   173    176     db->init.busy = 1;
   174    177   
   175    178     /* Construct the in-memory representation schema tables (sqlite_master or
   176    179     ** sqlite_temp_master) by invoking the parser directly.  The appropriate
   177    180     ** table name will be inserted automatically by the parser so we can just
   178    181     ** use the abbreviation "x" here.  The parser will also automatically tag
   179    182     ** the schema table as read-only. */
   180         -  azArg[0] = zMasterName = SCHEMA_TABLE(iDb);
   181         -  azArg[1] = "1";
   182         -  azArg[2] = "CREATE TABLE x(type text,name text,tbl_name text,"
          183  +  azArg[0] = "table";
          184  +  azArg[1] = zMasterName = SCHEMA_TABLE(iDb);
          185  +  azArg[2] = azArg[1];
          186  +  azArg[3] = "1";
          187  +  azArg[4] = "CREATE TABLE x(type text,name text,tbl_name text,"
   183    188                               "rootpage int,sql text)";
   184         -  azArg[3] = 0;
          189  +  azArg[5] = 0;
   185    190     initData.db = db;
   186    191     initData.iDb = iDb;
   187    192     initData.rc = SQLITE_OK;
   188    193     initData.pzErrMsg = pzErrMsg;
   189    194     initData.mInitFlags = mFlags;
   190    195     initData.nInitRow = 0;
   191         -  sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
          196  +  sqlite3InitCallback(&initData, 5, (char **)azArg, 0);
   192    197     if( initData.rc ){
   193    198       rc = initData.rc;
   194    199       goto error_out;
   195    200     }
   196    201   
   197    202     /* Create a cursor to hold the database open
   198    203     */
................................................................................
   310    315   
   311    316     /* Read the schema information out of the schema tables
   312    317     */
   313    318     assert( db->init.busy );
   314    319     {
   315    320       char *zSql;
   316    321       zSql = sqlite3MPrintf(db, 
   317         -        "SELECT name, rootpage, sql FROM \"%w\".%s ORDER BY rowid",
          322  +        "SELECT*FROM\"%w\".%s ORDER BY rowid",
   318    323           db->aDb[iDb].zDbSName, zMasterName);
   319    324   #ifndef SQLITE_OMIT_AUTHORIZATION
   320    325       {
   321    326         sqlite3_xauth xAuth;
   322    327         xAuth = db->xAuth;
   323    328         db->xAuth = 0;
   324    329   #endif
................................................................................
   631    636     }
   632    637     if( pzTail ){
   633    638       *pzTail = sParse.zTail;
   634    639     }
   635    640     rc = sParse.rc;
   636    641   
   637    642   #ifndef SQLITE_OMIT_EXPLAIN
   638         -  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
          643  +  /* Justification for the ALWAYS(): The only way for rc to be SQLITE_OK and
          644  +  ** sParse.pVdbe to be NULL is if the input SQL is an empty string, but in
          645  +  ** that case, sParse.explain will be false. */
          646  +  if( sParse.explain && rc==SQLITE_OK && ALWAYS(sParse.pVdbe) ){
   639    647       static const char * const azColName[] = {
   640    648          "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
   641    649          "id", "parent", "notused", "detail"
   642    650       };
   643    651       int iFirst, mx;
   644    652       if( sParse.explain==2 ){
   645    653         sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
................................................................................
   656    664       }
   657    665     }
   658    666   #endif
   659    667   
   660    668     if( db->init.busy==0 ){
   661    669       sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
   662    670     }
   663         -  if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
   664         -    sqlite3VdbeFinalize(sParse.pVdbe);
          671  +  if( rc!=SQLITE_OK || db->mallocFailed ){
          672  +    if( sParse.pVdbe ) sqlite3VdbeFinalize(sParse.pVdbe);
   665    673       assert(!(*ppStmt));
   666    674     }else{
   667    675       *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
   668    676     }
   669    677   
   670    678     if( zErrMsg ){
   671    679       sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);

Changes to src/resolve.c.

   377    377                 ExprSetProperty(pExpr, EP_Alias);
   378    378               }
   379    379             }else
   380    380   #endif /* SQLITE_OMIT_UPSERT */
   381    381             {
   382    382   #ifndef SQLITE_OMIT_TRIGGER
   383    383               if( iCol<0 ){
   384         -              pExpr->affinity = SQLITE_AFF_INTEGER;
          384  +              pExpr->affExpr = SQLITE_AFF_INTEGER;
   385    385               }else if( pExpr->iTable==0 ){
   386    386                 testcase( iCol==31 );
   387    387                 testcase( iCol==32 );
   388    388                 pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
   389    389               }else{
   390    390                 testcase( iCol==31 );
   391    391                 testcase( iCol==32 );
................................................................................
   409    409        && pMatch
   410    410        && (pNC->ncFlags & NC_IdxExpr)==0
   411    411        && sqlite3IsRowid(zCol)
   412    412        && VisibleRowid(pMatch->pTab)
   413    413       ){
   414    414         cnt = 1;
   415    415         pExpr->iColumn = -1;
   416         -      pExpr->affinity = SQLITE_AFF_INTEGER;
          416  +      pExpr->affExpr = SQLITE_AFF_INTEGER;
   417    417       }
   418    418   
   419    419       /*
   420    420       ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
   421    421       ** might refer to an result-set alias.  This happens, for example, when
   422    422       ** we are resolving names in the WHERE clause of the following command:
   423    423       **
................................................................................
   685    685         assert( pSrcList && pSrcList->nSrc==1 );
   686    686         pItem = pSrcList->a;
   687    687         assert( HasRowid(pItem->pTab) && pItem->pTab->pSelect==0 );
   688    688         pExpr->op = TK_COLUMN;
   689    689         pExpr->y.pTab = pItem->pTab;
   690    690         pExpr->iTable = pItem->iCursor;
   691    691         pExpr->iColumn = -1;
   692         -      pExpr->affinity = SQLITE_AFF_INTEGER;
          692  +      pExpr->affExpr = SQLITE_AFF_INTEGER;
   693    693         break;
   694    694       }
   695    695   #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT)
   696    696             && !defined(SQLITE_OMIT_SUBQUERY) */
   697    697   
   698    698       /* A column name:                    ID
   699    699       ** Or table name and column name:    ID.ID
................................................................................
   745    745         int wrong_num_args = 0;     /* True if wrong number of arguments */
   746    746         int is_agg = 0;             /* True if is an aggregate function */
   747    747         int nId;                    /* Number of characters in function name */
   748    748         const char *zId;            /* The function name. */
   749    749         FuncDef *pDef;              /* Information about the function */
   750    750         u8 enc = ENC(pParse->db);   /* The database encoding */
   751    751         int savedAllowFlags = (pNC->ncFlags & (NC_AllowAgg | NC_AllowWin));
   752         -
          752  +#ifndef SQLITE_OMIT_WINDOWFUNC
          753  +      Window *pWin = (IsWindowFunc(pExpr) ? pExpr->y.pWin : 0);
          754  +#endif
   753    755         assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
   754    756         zId = pExpr->u.zToken;
   755    757         nId = sqlite3Strlen30(zId);
   756    758         pDef = sqlite3FindFunction(pParse->db, zId, n, enc, 0);
   757    759         if( pDef==0 ){
   758    760           pDef = sqlite3FindFunction(pParse->db, zId, -2, enc, 0);
   759    761           if( pDef==0 ){
................................................................................
   817    819            && pParse->nested==0
   818    820            && sqlite3Config.bInternalFunctions==0
   819    821           ){
   820    822             /* Internal-use-only functions are disallowed unless the
   821    823             ** SQL is being compiled using sqlite3NestedParse() */
   822    824             no_such_func = 1;
   823    825             pDef = 0;
          826  +        }else
          827  +        if( (pDef->funcFlags & SQLITE_FUNC_DIRECT)!=0
          828  +         && ExprHasProperty(pExpr, EP_Indirect)
          829  +         && !IN_RENAME_OBJECT
          830  +        ){
          831  +          /* Functions tagged with SQLITE_DIRECTONLY may not be used
          832  +          ** inside of triggers and views */
          833  +          sqlite3ErrorMsg(pParse, "%s() prohibited in triggers and views",
          834  +                          pDef->zName);
   824    835           }
   825    836         }
   826    837   
   827    838         if( 0==IN_RENAME_OBJECT ){
   828    839   #ifndef SQLITE_OMIT_WINDOWFUNC
   829    840           assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
   830    841             || (pDef->xValue==0 && pDef->xInverse==0)
   831    842             || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
   832    843           );
   833         -        if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
          844  +        if( pDef && pDef->xValue==0 && pWin ){
   834    845             sqlite3ErrorMsg(pParse, 
   835    846                 "%.*s() may not be used as a window function", nId, zId
   836    847             );
   837    848             pNC->nErr++;
   838    849           }else if( 
   839    850                 (is_agg && (pNC->ncFlags & NC_AllowAgg)==0)
   840         -           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pExpr->y.pWin)
   841         -           || (is_agg && pExpr->y.pWin && (pNC->ncFlags & NC_AllowWin)==0)
          851  +           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
          852  +           || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
   842    853           ){
   843    854             const char *zType;
   844         -          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){
          855  +          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
   845    856               zType = "window";
   846    857             }else{
   847    858               zType = "aggregate";
   848    859             }
   849    860             sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
   850    861             pNC->nErr++;
   851    862             is_agg = 0;
................................................................................
   865    876             sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
   866    877             pNC->nErr++;
   867    878           }else if( wrong_num_args ){
   868    879             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
   869    880                  nId, zId);
   870    881             pNC->nErr++;
   871    882           }
          883  +#ifndef SQLITE_OMIT_WINDOWFUNC
          884  +        else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
          885  +          sqlite3ErrorMsg(pParse, 
          886  +              "FILTER may not be used with non-aggregate %.*s()", 
          887  +              nId, zId
          888  +          );
          889  +          pNC->nErr++;
          890  +        }
          891  +#endif
   872    892           if( is_agg ){
   873    893             /* Window functions may not be arguments of aggregate functions.
   874    894             ** Or arguments of other window functions. But aggregate functions
   875    895             ** may be arguments for window functions.  */
   876    896   #ifndef SQLITE_OMIT_WINDOWFUNC
   877         -          pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
          897  +          pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
   878    898   #else
   879    899             pNC->ncFlags &= ~NC_AllowAgg;
   880    900   #endif
   881    901           }
   882    902         }
          903  +#ifndef SQLITE_OMIT_WINDOWFUNC
          904  +      else if( ExprHasProperty(pExpr, EP_WinFunc) ){
          905  +        is_agg = 1;
          906  +      }
          907  +#endif
   883    908         sqlite3WalkExprList(pWalker, pList);
   884    909         if( is_agg ){
   885    910   #ifndef SQLITE_OMIT_WINDOWFUNC
   886         -        if( pExpr->y.pWin ){
          911  +        if( pWin ){
   887    912             Select *pSel = pNC->pWinSelect;
          913  +          assert( pWin==pExpr->y.pWin );
   888    914             if( IN_RENAME_OBJECT==0 ){
   889         -            sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
          915  +            sqlite3WindowUpdate(pParse, pSel->pWinDefn, pWin, pDef);
   890    916             }
   891         -          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
   892         -          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
   893         -          sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
   894         -          if( 0==pSel->pWin 
   895         -           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
   896         -          ){
   897         -            pExpr->y.pWin->pNextWin = pSel->pWin;
   898         -            pSel->pWin = pExpr->y.pWin;
   899         -          }
          917  +          sqlite3WalkExprList(pWalker, pWin->pPartition);
          918  +          sqlite3WalkExprList(pWalker, pWin->pOrderBy);
          919  +          sqlite3WalkExpr(pWalker, pWin->pFilter);
          920  +          sqlite3WindowLink(pSel, pWin);
   900    921             pNC->ncFlags |= NC_HasWin;
   901    922           }else
   902    923   #endif /* SQLITE_OMIT_WINDOWFUNC */
   903    924           {
   904    925             NameContext *pNC2 = pNC;
   905    926             pExpr->op = TK_AGG_FUNCTION;
   906    927             pExpr->op2 = 0;
          928  +#ifndef SQLITE_OMIT_WINDOWFUNC
          929  +          if( ExprHasProperty(pExpr, EP_WinFunc) ){
          930  +            sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
          931  +          }
          932  +#endif
   907    933             while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
   908    934               pExpr->op2++;
   909    935               pNC2 = pNC2->pNext;
   910    936             }
   911         -          assert( pDef!=0 );
   912         -          if( pNC2 ){
          937  +          assert( pDef!=0 || IN_RENAME_OBJECT );
          938  +          if( pNC2 && pDef ){
   913    939               assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
   914    940               testcase( (pDef->funcFlags & SQLITE_FUNC_MINMAX)!=0 );
   915    941               pNC2->ncFlags |= NC_HasAgg | (pDef->funcFlags & SQLITE_FUNC_MINMAX);
   916    942   
   917    943             }
   918    944           }
   919    945           pNC->ncFlags |= savedAllowFlags;
................................................................................
   943    969       }
   944    970       case TK_VARIABLE: {
   945    971         notValid(pParse, pNC, "parameters", NC_IsCheck|NC_PartIdx|NC_IdxExpr);
   946    972         break;
   947    973       }
   948    974       case TK_IS:
   949    975       case TK_ISNOT: {
   950         -      Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
          976  +      Expr *pRight = sqlite3ExprSkipCollateAndLikely(pExpr->pRight);
   951    977         assert( !ExprHasProperty(pExpr, EP_Reduced) );
   952    978         /* Handle special cases of "x IS TRUE", "x IS FALSE", "x IS NOT TRUE",
   953    979         ** and "x IS NOT FALSE". */
   954    980         if( pRight->op==TK_ID ){
   955    981           int rc = resolveExprStep(pWalker, pRight);
   956    982           if( rc==WRC_Abort ) return WRC_Abort;
   957    983           if( pRight->op==TK_TRUEFALSE ){
................................................................................
  1154   1180       moreToDo = 0;
  1155   1181       pEList = pSelect->pEList;
  1156   1182       assert( pEList!=0 );
  1157   1183       for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
  1158   1184         int iCol = -1;
  1159   1185         Expr *pE, *pDup;
  1160   1186         if( pItem->done ) continue;
  1161         -      pE = sqlite3ExprSkipCollate(pItem->pExpr);
         1187  +      pE = sqlite3ExprSkipCollateAndLikely(pItem->pExpr);
  1162   1188         if( sqlite3ExprIsInteger(pE, &iCol) ){
  1163   1189           if( iCol<=0 || iCol>pEList->nExpr ){
  1164   1190             resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
  1165   1191             return 1;
  1166   1192           }
  1167   1193         }else{
  1168   1194           iCol = resolveAsName(pParse, pEList, pE);
................................................................................
  1248   1274     const char *zType     /* "ORDER" or "GROUP" */
  1249   1275   ){
  1250   1276     int i;
  1251   1277     sqlite3 *db = pParse->db;
  1252   1278     ExprList *pEList;
  1253   1279     struct ExprList_item *pItem;
  1254   1280   
  1255         -  if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
         1281  +  if( pOrderBy==0 || pParse->db->mallocFailed || IN_RENAME_OBJECT ) return 0;
  1256   1282     if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  1257   1283       sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
  1258   1284       return 1;
  1259   1285     }
  1260   1286     pEList = pSelect->pEList;
  1261   1287     assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
  1262   1288     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
................................................................................
  1270   1296       }
  1271   1297     }
  1272   1298     return 0;
  1273   1299   }
  1274   1300   
  1275   1301   #ifndef SQLITE_OMIT_WINDOWFUNC
  1276   1302   /*
  1277         -** Walker callback for resolveRemoveWindows().
         1303  +** Walker callback for windowRemoveExprFromSelect().
  1278   1304   */
  1279   1305   static int resolveRemoveWindowsCb(Walker *pWalker, Expr *pExpr){
         1306  +  UNUSED_PARAMETER(pWalker);
  1280   1307     if( ExprHasProperty(pExpr, EP_WinFunc) ){
  1281         -    Window **pp;
  1282         -    for(pp=&pWalker->u.pSelect->pWin; *pp; pp=&(*pp)->pNextWin){
  1283         -      if( *pp==pExpr->y.pWin ){
  1284         -        *pp = (*pp)->pNextWin;
  1285         -        break;
  1286         -      }    
  1287         -    }
         1308  +    Window *pWin = pExpr->y.pWin;
         1309  +    sqlite3WindowUnlinkFromSelect(pWin);
  1288   1310     }
  1289   1311     return WRC_Continue;
  1290   1312   }
  1291   1313   
  1292   1314   /*
  1293   1315   ** Remove any Window objects owned by the expression pExpr from the
  1294   1316   ** Select.pWin list of Select object pSelect.
  1295   1317   */
  1296         -static void resolveRemoveWindows(Select *pSelect, Expr *pExpr){
  1297         -  Walker sWalker;
  1298         -  memset(&sWalker, 0, sizeof(Walker));
  1299         -  sWalker.xExprCallback = resolveRemoveWindowsCb;
  1300         -  sWalker.u.pSelect = pSelect;
  1301         -  sqlite3WalkExpr(&sWalker, pExpr);
         1318  +static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
         1319  +  if( pSelect->pWin ){
         1320  +    Walker sWalker;
         1321  +    memset(&sWalker, 0, sizeof(Walker));
         1322  +    sWalker.xExprCallback = resolveRemoveWindowsCb;
         1323  +    sWalker.u.pSelect = pSelect;
         1324  +    sqlite3WalkExpr(&sWalker, pExpr);
         1325  +  }
  1302   1326   }
  1303   1327   #else
  1304         -# define resolveRemoveWindows(x,y)
  1305         -#endif
         1328  +# define windowRemoveExprFromSelect(a, b)
         1329  +#endif /* SQLITE_OMIT_WINDOWFUNC */
  1306   1330   
  1307   1331   /*
  1308   1332   ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
  1309   1333   ** The Name context of the SELECT statement is pNC.  zType is either
  1310   1334   ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
  1311   1335   **
  1312   1336   ** This routine resolves each term of the clause into an expression.
................................................................................
  1335   1359     int nResult;                   /* Number of terms in the result set */
  1336   1360   
  1337   1361     if( pOrderBy==0 ) return 0;
  1338   1362     nResult = pSelect->pEList->nExpr;
  1339   1363     pParse = pNC->pParse;
  1340   1364     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
  1341   1365       Expr *pE = pItem->pExpr;
  1342         -    Expr *pE2 = sqlite3ExprSkipCollate(pE);
         1366  +    Expr *pE2 = sqlite3ExprSkipCollateAndLikely(pE);
  1343   1367       if( zType[0]!='G' ){
  1344   1368         iCol = resolveAsName(pParse, pSelect->pEList, pE2);
  1345   1369         if( iCol>0 ){
  1346   1370           /* If an AS-name match is found, mark this ORDER BY column as being
  1347   1371           ** a copy of the iCol-th result-set column.  The subsequent call to
  1348   1372           ** sqlite3ResolveOrderGroupBy() will convert the expression to a
  1349   1373           ** copy of the iCol-th result-set expression. */
................................................................................
  1369   1393         return 1;
  1370   1394       }
  1371   1395       for(j=0; j<pSelect->pEList->nExpr; j++){
  1372   1396         if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
  1373   1397           /* Since this expresion is being changed into a reference
  1374   1398           ** to an identical expression in the result set, remove all Window
  1375   1399           ** objects belonging to the expression from the Select.pWin list. */
  1376         -        resolveRemoveWindows(pSelect, pE);
         1400  +        windowRemoveExprFromSelect(pSelect, pE);
  1377   1401           pItem->u.x.iOrderByCol = j+1;
  1378   1402         }
  1379   1403       }
  1380   1404     }
  1381   1405     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
  1382   1406   }
  1383   1407   

Changes to src/select.c.

    98     98       sqlite3ExprDelete(db, p->pLimit);
    99     99   #ifndef SQLITE_OMIT_WINDOWFUNC
   100    100       if( OK_IF_ALWAYS_TRUE(p->pWinDefn) ){
   101    101         sqlite3WindowListDelete(db, p->pWinDefn);
   102    102       }
   103    103   #endif
   104    104       if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
          105  +    assert( p->pWin==0 );
   105    106       if( bFree ) sqlite3DbFreeNN(db, p);
   106    107       p = pPrior;
   107    108       bFree = 1;
   108    109     }
   109    110   }
   110    111   
   111    112   /*
................................................................................
   659    660       }
   660    661       VdbeCoverage(v);
   661    662       sqlite3VdbeAddOp3(v, OP_Compare, regPrevKey, regBase, pSort->nOBSat);
   662    663       pOp = sqlite3VdbeGetOp(v, pSort->addrSortIndex);
   663    664       if( pParse->db->mallocFailed ) return;
   664    665       pOp->p2 = nKey + nData;
   665    666       pKI = pOp->p4.pKeyInfo;
   666         -    memset(pKI->aSortOrder, 0, pKI->nKeyField); /* Makes OP_Jump testable */
          667  +    memset(pKI->aSortFlags, 0, pKI->nKeyField); /* Makes OP_Jump testable */
   667    668       sqlite3VdbeChangeP4(v, -1, (char*)pKI, P4_KEYINFO);
   668    669       testcase( pKI->nAllField > pKI->nKeyField+2 );
   669    670       pOp->p4.pKeyInfo = sqlite3KeyInfoFromExprList(pParse,pSort->pOrderBy,nOBSat,
   670    671                                              pKI->nAllField-pKI->nKeyField-1);
   671    672       addrJmp = sqlite3VdbeCurrentAddr(v);
   672    673       sqlite3VdbeAddOp3(v, OP_Jump, addrJmp+1, 0, addrJmp+1); VdbeCoverage(v);
   673    674       pSort->labelBkOut = sqlite3VdbeMakeLabel(pParse);
................................................................................
  1270   1271   ** Allocate a KeyInfo object sufficient for an index of N key columns and
  1271   1272   ** X extra columns.
  1272   1273   */
  1273   1274   KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
  1274   1275     int nExtra = (N+X)*(sizeof(CollSeq*)+1) - sizeof(CollSeq*);
  1275   1276     KeyInfo *p = sqlite3DbMallocRawNN(db, sizeof(KeyInfo) + nExtra);
  1276   1277     if( p ){
  1277         -    p->aSortOrder = (u8*)&p->aColl[N+X];
         1278  +    p->aSortFlags = (u8*)&p->aColl[N+X];
  1278   1279       p->nKeyField = (u16)N;
  1279   1280       p->nAllField = (u16)(N+X);
  1280   1281       p->enc = ENC(db);
  1281   1282       p->db = db;
  1282   1283       p->nRef = 1;
  1283   1284       memset(&p[1], 0, nExtra);
  1284   1285     }else{
................................................................................
  1347   1348   
  1348   1349     nExpr = pList->nExpr;
  1349   1350     pInfo = sqlite3KeyInfoAlloc(db, nExpr-iStart, nExtra+1);
  1350   1351     if( pInfo ){
  1351   1352       assert( sqlite3KeyInfoIsWriteable(pInfo) );
  1352   1353       for(i=iStart, pItem=pList->a+iStart; i<nExpr; i++, pItem++){
  1353   1354         pInfo->aColl[i-iStart] = sqlite3ExprNNCollSeq(pParse, pItem->pExpr);
  1354         -      pInfo->aSortOrder[i-iStart] = pItem->sortOrder;
         1355  +      pInfo->aSortFlags[i-iStart] = pItem->sortFlags;
  1355   1356       }
  1356   1357     }
  1357   1358     return pInfo;
  1358   1359   }
  1359   1360   
  1360   1361   /*
  1361   1362   ** Name of the connection operator, used for error messages.
................................................................................
  1639   1640     char const *zOrigDb = 0;
  1640   1641     char const *zOrigTab = 0;
  1641   1642     char const *zOrigCol = 0;
  1642   1643   #endif
  1643   1644   
  1644   1645     assert( pExpr!=0 );
  1645   1646     assert( pNC->pSrcList!=0 );
  1646         -  assert( pExpr->op!=TK_AGG_COLUMN );  /* This routine runes before aggregates
  1647         -                                       ** are processed */
  1648   1647     switch( pExpr->op ){
  1649   1648       case TK_COLUMN: {
  1650   1649         /* The expression is a column. Locate the table the column is being
  1651   1650         ** extracted from in NameContext.pSrcList. This table may be real
  1652   1651         ** database table or a subquery.
  1653   1652         */
  1654   1653         Table *pTab = 0;            /* Table structure column is extracted from */
................................................................................
  1957   1956   
  1958   1957     for(i=0, pCol=aCol; i<nCol && !db->mallocFailed; i++, pCol++){
  1959   1958       /* Get an appropriate name for the column
  1960   1959       */
  1961   1960       if( (zName = pEList->a[i].zName)!=0 ){
  1962   1961         /* If the column contains an "AS <name>" phrase, use <name> as the name */
  1963   1962       }else{
  1964         -      Expr *pColExpr = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
         1963  +      Expr *pColExpr = sqlite3ExprSkipCollateAndLikely(pEList->a[i].pExpr);
  1965   1964         while( pColExpr->op==TK_DOT ){
  1966   1965           pColExpr = pColExpr->pRight;
  1967   1966           assert( pColExpr!=0 );
  1968   1967         }
  1969         -      assert( pColExpr->op!=TK_AGG_COLUMN );
  1970   1968         if( pColExpr->op==TK_COLUMN ){
  1971   1969           /* For columns use the column name name */
  1972   1970           int iCol = pColExpr->iColumn;
  1973   1971           Table *pTab = pColExpr->y.pTab;
  1974   1972           assert( pTab!=0 );
  1975   1973           if( iCol<0 ) iCol = pTab->iPKey;
  1976   1974           zName = iCol>=0 ? pTab->aCol[iCol].zName : "rowid";
................................................................................
  2030   2028   **
  2031   2029   ** This routine requires that all identifiers in the SELECT
  2032   2030   ** statement be resolved.
  2033   2031   */
  2034   2032   void sqlite3SelectAddColumnTypeAndCollation(
  2035   2033     Parse *pParse,        /* Parsing contexts */
  2036   2034     Table *pTab,          /* Add column type information to this table */
  2037         -  Select *pSelect       /* SELECT used to determine types and collations */
         2035  +  Select *pSelect,      /* SELECT used to determine types and collations */
         2036  +  char aff              /* Default affinity for columns */
  2038   2037   ){
  2039   2038     sqlite3 *db = pParse->db;
  2040   2039     NameContext sNC;
  2041   2040     Column *pCol;
  2042   2041     CollSeq *pColl;
  2043   2042     int i;
  2044   2043     Expr *p;
................................................................................
  2063   2062         n = sqlite3Strlen30(pCol->zName);
  2064   2063         pCol->zName = sqlite3DbReallocOrFree(db, pCol->zName, n+m+2);
  2065   2064         if( pCol->zName ){
  2066   2065           memcpy(&pCol->zName[n+1], zType, m+1);
  2067   2066           pCol->colFlags |= COLFLAG_HASTYPE;
  2068   2067         }
  2069   2068       }
  2070         -    if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_BLOB;
         2069  +    if( pCol->affinity<=SQLITE_AFF_NONE ) pCol->affinity = aff;
  2071   2070       pColl = sqlite3ExprCollSeq(pParse, p);
  2072   2071       if( pColl && pCol->zColl==0 ){
  2073   2072         pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
  2074   2073       }
  2075   2074     }
  2076   2075     pTab->szTabRow = 1; /* Any non-zero value works */
  2077   2076   }
  2078   2077   
  2079   2078   /*
  2080   2079   ** Given a SELECT statement, generate a Table structure that describes
  2081   2080   ** the result set of that SELECT.
  2082   2081   */
  2083         -Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
         2082  +Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect, char aff){
  2084   2083     Table *pTab;
  2085   2084     sqlite3 *db = pParse->db;
  2086   2085     u64 savedFlags;
  2087   2086   
  2088   2087     savedFlags = db->flags;
  2089   2088     db->flags &= ~(u64)SQLITE_FullColNames;
  2090   2089     db->flags |= SQLITE_ShortColNames;
................................................................................
  2096   2095     if( pTab==0 ){
  2097   2096       return 0;
  2098   2097     }
  2099   2098     pTab->nTabRef = 1;
  2100   2099     pTab->zName = 0;
  2101   2100     pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
  2102   2101     sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
  2103         -  sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect);
         2102  +  sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSelect, aff);
  2104   2103     pTab->iPKey = -1;
  2105   2104     if( db->mallocFailed ){
  2106   2105       sqlite3DeleteTable(db, pTab);
  2107   2106       return 0;
  2108   2107     }
  2109   2108     return pTab;
  2110   2109   }
................................................................................
  2250   2249           pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
  2251   2250           if( pColl==0 ) pColl = db->pDfltColl;
  2252   2251           pOrderBy->a[i].pExpr =
  2253   2252             sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
  2254   2253         }
  2255   2254         assert( sqlite3KeyInfoIsWriteable(pRet) );
  2256   2255         pRet->aColl[i] = pColl;
  2257         -      pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
         2256  +      pRet->aSortFlags[i] = pOrderBy->a[i].sortFlags;
  2258   2257       }
  2259   2258     }
  2260   2259   
  2261   2260     return pRet;
  2262   2261   }
  2263   2262   
  2264   2263   #ifndef SQLITE_OMIT_CTE
................................................................................
  2961   2960                              pIn->iSdst, pIn->nSdst);
  2962   2961         sqlite3ReleaseTempReg(pParse, r1);
  2963   2962         break;
  2964   2963       }
  2965   2964   
  2966   2965       /* If this is a scalar select that is part of an expression, then
  2967   2966       ** store the results in the appropriate memory cell and break out
  2968         -    ** of the scan loop.
         2967  +    ** of the scan loop.  Note that the select might return multiple columns
         2968  +    ** if it is the RHS of a row-value IN operator.
  2969   2969       */
  2970   2970       case SRT_Mem: {
  2971         -      assert( pIn->nSdst==1 || pParse->nErr>0 );  testcase( pIn->nSdst!=1 );
  2972         -      sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
         2971  +      if( pParse->nErr==0 ){
         2972  +        testcase( pIn->nSdst>1 );
         2973  +        sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, pIn->nSdst);
         2974  +      }
  2973   2975         /* The LIMIT clause will jump out of the loop for us */
  2974   2976         break;
  2975   2977       }
  2976   2978   #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
  2977   2979   
  2978   2980       /* The results are stored in a sequence of registers
  2979   2981       ** starting at pDest->iSdst.  Then the co-routine yields.
................................................................................
  3222   3224       pParse->nMem += nExpr+1;
  3223   3225       sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
  3224   3226       pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
  3225   3227       if( pKeyDup ){
  3226   3228         assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
  3227   3229         for(i=0; i<nExpr; i++){
  3228   3230           pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
  3229         -        pKeyDup->aSortOrder[i] = 0;
         3231  +        pKeyDup->aSortFlags[i] = 0;
  3230   3232         }
  3231   3233       }
  3232   3234     }
  3233   3235    
  3234   3236     /* Separate the left and the right query from one another
  3235   3237     */
  3236   3238     p->pPrior = 0;
................................................................................
  3472   3474           }
  3473   3475           if( pNew && ExprHasProperty(pExpr,EP_FromJoin) ){
  3474   3476             pNew->iRightJoinTable = pExpr->iRightJoinTable;
  3475   3477             ExprSetProperty(pNew, EP_FromJoin);
  3476   3478           }
  3477   3479           sqlite3ExprDelete(db, pExpr);
  3478   3480           pExpr = pNew;
         3481  +
         3482  +        /* Ensure that the expression now has an implicit collation sequence,
         3483  +        ** just as it did when it was a column of a view or sub-query. */
         3484  +        if( pExpr ){
         3485  +          if( pExpr->op!=TK_COLUMN && pExpr->op!=TK_COLLATE ){
         3486  +            CollSeq *pColl = sqlite3ExprCollSeq(pSubst->pParse, pExpr);
         3487  +            pExpr = sqlite3ExprAddCollateString(pSubst->pParse, pExpr, 
         3488  +                (pColl ? pColl->zName : "BINARY")
         3489  +            );
         3490  +          }
         3491  +          ExprClearProperty(pExpr, EP_Collate);
         3492  +        }
  3479   3493         }
  3480   3494       }
  3481   3495     }else{
  3482   3496       if( pExpr->op==TK_IF_NULL_ROW && pExpr->iTable==pSubst->iTable ){
  3483   3497         pExpr->iTable = pSubst->iNewTable;
  3484   3498       }
  3485   3499       pExpr->pLeft = substExpr(pSubst, pExpr->pLeft);
................................................................................
  3945   3959     ** will scan expressions looking for iParent references and replace
  3946   3960     ** those references with expressions that resolve to the subquery FROM
  3947   3961     ** elements we are now copying in.
  3948   3962     */
  3949   3963     for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
  3950   3964       int nSubSrc;
  3951   3965       u8 jointype = 0;
         3966  +    assert( pSub!=0 );
  3952   3967       pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
  3953   3968       nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
  3954   3969       pSrc = pParent->pSrc;     /* FROM clause of the outer query */
  3955   3970   
  3956   3971       if( pSrc ){
  3957   3972         assert( pParent==p );  /* First time through the loop */
  3958   3973         jointype = pSubitem->fg.jointype;
................................................................................
  4395   4410   ** analysis.
  4396   4411   */
  4397   4412   static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
  4398   4413     int eRet = WHERE_ORDERBY_NORMAL;      /* Return value */
  4399   4414     ExprList *pEList = pFunc->x.pList;    /* Arguments to agg function */
  4400   4415     const char *zFunc;                    /* Name of aggregate function pFunc */
  4401   4416     ExprList *pOrderBy;
  4402         -  u8 sortOrder;
         4417  +  u8 sortFlags;
  4403   4418   
  4404   4419     assert( *ppMinMax==0 );
  4405   4420     assert( pFunc->op==TK_AGG_FUNCTION );
  4406         -  if( pEList==0 || pEList->nExpr!=1 ) return eRet;
         4421  +  assert( !IsWindowFunc(pFunc) );
         4422  +  if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
         4423  +    return eRet;
         4424  +  }
  4407   4425     zFunc = pFunc->u.zToken;
  4408   4426     if( sqlite3StrICmp(zFunc, "min")==0 ){
  4409   4427       eRet = WHERE_ORDERBY_MIN;
  4410         -    sortOrder = SQLITE_SO_ASC;
         4428  +    sortFlags = KEYINFO_ORDER_BIGNULL;
  4411   4429     }else if( sqlite3StrICmp(zFunc, "max")==0 ){
  4412   4430       eRet = WHERE_ORDERBY_MAX;
  4413         -    sortOrder = SQLITE_SO_DESC;
         4431  +    sortFlags = KEYINFO_ORDER_DESC;
  4414   4432     }else{
  4415   4433       return eRet;
  4416   4434     }
  4417   4435     *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
  4418   4436     assert( pOrderBy!=0 || db->mallocFailed );
  4419         -  if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
         4437  +  if( pOrderBy ) pOrderBy->a[0].sortFlags = sortFlags;
  4420   4438     return eRet;
  4421   4439   }
  4422   4440   
  4423   4441   /*
  4424   4442   ** The select statement passed as the first argument is an aggregate query.
  4425   4443   ** The second argument is the associated aggregate-info object. This 
  4426   4444   ** function tests if the SELECT is of the form:
................................................................................
  4446   4464     pExpr = p->pEList->a[0].pExpr;
  4447   4465     assert( pTab && !pTab->pSelect && pExpr );
  4448   4466   
  4449   4467     if( IsVirtual(pTab) ) return 0;
  4450   4468     if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
  4451   4469     if( NEVER(pAggInfo->nFunc==0) ) return 0;
  4452   4470     if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
  4453         -  if( pExpr->flags&EP_Distinct ) return 0;
         4471  +  if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
  4454   4472   
  4455   4473     return pTab;
  4456   4474   }
  4457   4475   
  4458   4476   /*
  4459   4477   ** If the source-list item passed as an argument was augmented with an
  4460   4478   ** INDEXED BY clause, then try to locate the specified index. If there
................................................................................
  4893   4911         }
  4894   4912   #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
  4895   4913         if( IsVirtual(pTab) || pTab->pSelect ){
  4896   4914           i16 nCol;
  4897   4915           u8 eCodeOrig = pWalker->eCode;
  4898   4916           if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
  4899   4917           assert( pFrom->pSelect==0 );
         4918  +        if( pTab->pSelect && (db->flags & SQLITE_EnableView)==0 ){
         4919  +          sqlite3ErrorMsg(pParse, "access to view \"%s\" prohibited",
         4920  +              pTab->zName);
         4921  +        }
  4900   4922           pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
  4901   4923           nCol = pTab->nCol;
  4902   4924           pTab->nCol = -1;
  4903   4925           pWalker->eCode = 1;  /* Turn on Select.selId renumbering */
  4904   4926           sqlite3WalkSelect(pWalker, pFrom->pSelect);
  4905   4927           pWalker->eCode = eCodeOrig;
  4906   4928           pTab->nCol = nCol;
................................................................................
  5186   5208       Table *pTab = pFrom->pTab;
  5187   5209       assert( pTab!=0 );
  5188   5210       if( (pTab->tabFlags & TF_Ephemeral)!=0 ){
  5189   5211         /* A sub-query in the FROM clause of a SELECT */
  5190   5212         Select *pSel = pFrom->pSelect;
  5191   5213         if( pSel ){
  5192   5214           while( pSel->pPrior ) pSel = pSel->pPrior;
  5193         -        sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel);
         5215  +        sqlite3SelectAddColumnTypeAndCollation(pParse, pTab, pSel,
         5216  +                                               SQLITE_AFF_NONE);
  5194   5217         }
  5195   5218       }
  5196   5219     }
  5197   5220   }
  5198   5221   #endif
  5199   5222   
  5200   5223   
................................................................................
  5326   5349     pAggInfo->directMode = 1;
  5327   5350     for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
  5328   5351       int nArg;
  5329   5352       int addrNext = 0;
  5330   5353       int regAgg;
  5331   5354       ExprList *pList = pF->pExpr->x.pList;
  5332   5355       assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
         5356  +    assert( !IsWindowFunc(pF->pExpr) );
         5357  +    if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
         5358  +      Expr *pFilter = pF->pExpr->y.pWin->pFilter;
         5359  +      addrNext = sqlite3VdbeMakeLabel(pParse);
         5360  +      sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
         5361  +    }
  5333   5362       if( pList ){
  5334   5363         nArg = pList->nExpr;
  5335   5364         regAgg = sqlite3GetTempRange(pParse, nArg);
  5336   5365         sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
  5337   5366       }else{
  5338   5367         nArg = 0;
  5339   5368         regAgg = 0;
  5340   5369       }
  5341   5370       if( pF->iDistinct>=0 ){
  5342         -      addrNext = sqlite3VdbeMakeLabel(pParse);
         5371  +      if( addrNext==0 ){ 
         5372  +        addrNext = sqlite3VdbeMakeLabel(pParse);
         5373  +      }
  5343   5374         testcase( nArg==0 );  /* Error condition */
  5344   5375         testcase( nArg>1 );   /* Also an error */
  5345   5376         codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
  5346   5377       }
  5347   5378       if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5348   5379         CollSeq *pColl = 0;
  5349   5380         struct ExprList_item *pItem;
................................................................................
  5838   5869       ** have a column named by the empty string, in which case there is no way to
  5839   5870       ** distinguish between an unreferenced table and an actual reference to the
  5840   5871       ** "" column. The original design was for the fake column name to be a NULL,
  5841   5872       ** which would be unambiguous.  But legacy authorization callbacks might
  5842   5873       ** assume the column name is non-NULL and segfault.  The use of an empty
  5843   5874       ** string for the fake column name seems safer.
  5844   5875       */
  5845         -    if( pItem->colUsed==0 ){
         5876  +    if( pItem->colUsed==0 && pItem->zName!=0 ){
  5846   5877         sqlite3AuthCheck(pParse, SQLITE_READ, pItem->zName, "", pItem->zDatabase);
  5847   5878       }
  5848   5879   
  5849   5880   #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
  5850   5881       /* Generate code for all sub-queries in the FROM clause
  5851   5882       */
  5852   5883       pSub = pItem->pSelect;
  5853   5884       if( pSub==0 ) continue;
  5854   5885   
  5855   5886       /* The code for a subquery should only be generated once, though it is
  5856   5887       ** technically harmless for it to be generated multiple times. The
  5857   5888       ** following assert() will detect if something changes to cause
  5858   5889       ** the same subquery to be coded multiple times, as a signal to the
  5859         -    ** developers to try to optimize the situation. */
  5860         -    assert( pItem->addrFillSub==0 );
         5890  +    ** developers to try to optimize the situation.
         5891  +    **
         5892  +    ** Update 2019-07-24:
         5893  +    ** See ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311cec40.
         5894  +    ** The dbsqlfuzz fuzzer found a case where the same subquery gets
         5895  +    ** coded twice.  So this assert() now becomes a testcase().  It should
         5896  +    ** be very rare, though.
         5897  +    */
         5898  +    testcase( pItem->addrFillSub!=0 );
  5861   5899   
  5862   5900       /* Increment Parse.nHeight by the height of the largest expression
  5863   5901       ** tree referred to by this, the parent select. The child select
  5864   5902       ** may contain expression trees of at most
  5865   5903       ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
  5866   5904       ** more conservative than necessary, but much easier than enforcing
  5867   5905       ** an exact limit.
................................................................................
  5927   5965         ** is a register allocated to hold the subroutine return address
  5928   5966         */
  5929   5967         int topAddr;
  5930   5968         int onceAddr = 0;
  5931   5969         int retAddr;
  5932   5970         struct SrcList_item *pPrior;
  5933   5971   
  5934         -      assert( pItem->addrFillSub==0 );
         5972  +      testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
  5935   5973         pItem->regReturn = ++pParse->nMem;
  5936   5974         topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
  5937   5975         pItem->addrFillSub = topAddr+1;
  5938   5976         if( pItem->fg.isCorrelated==0 ){
  5939   5977           /* If the subquery is not correlated and if we are not inside of
  5940   5978           ** a trigger, then we only need to compute the value of the subquery
  5941   5979           ** once. */
................................................................................
  6218   6256       sAggInfo.nAccumulator = sAggInfo.nColumn;
  6219   6257       if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
  6220   6258         minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
  6221   6259       }else{
  6222   6260         minMaxFlag = WHERE_ORDERBY_NORMAL;
  6223   6261       }
  6224   6262       for(i=0; i<sAggInfo.nFunc; i++){
  6225         -      assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
         6263  +      Expr *pExpr = sAggInfo.aFunc[i].pExpr;
         6264  +      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  6226   6265         sNC.ncFlags |= NC_InAggFunc;
  6227         -      sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
         6266  +      sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
         6267  +#ifndef SQLITE_OMIT_WINDOWFUNC
         6268  +      assert( !IsWindowFunc(pExpr) );
         6269  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
         6270  +        sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
         6271  +      }
         6272  +#endif
  6228   6273         sNC.ncFlags &= ~NC_InAggFunc;
  6229   6274       }
  6230   6275       sAggInfo.mxReg = pParse->nMem;
  6231   6276       if( db->mallocFailed ) goto select_end;
  6232   6277   #if SELECTTRACE_ENABLED
  6233   6278       if( sqlite3SelectTrace & 0x400 ){
  6234   6279         int ii;

Changes to src/shell.c.in.

  1252   1252     f = fopen(zTempFile, bBin ? "wb" : "w");
  1253   1253     if( f==0 ){
  1254   1254       sqlite3_result_error(context, "edit() cannot open temp file", -1);
  1255   1255       goto edit_func_end;
  1256   1256     }
  1257   1257     sz = sqlite3_value_bytes(argv[0]);
  1258   1258     if( bBin ){
  1259         -    x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
         1259  +    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
  1260   1260     }else{
  1261   1261       const char *z = (const char*)sqlite3_value_text(argv[0]);
  1262   1262       /* Remember whether or not the value originally contained \r\n */
  1263   1263       if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
  1264         -    x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
         1264  +    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
  1265   1265     }
  1266   1266     fclose(f);
  1267   1267     f = 0;
  1268   1268     if( x!=sz ){
  1269   1269       sqlite3_result_error(context, "edit() could not write the whole file", -1);
  1270   1270       goto edit_func_end;
  1271   1271     }
................................................................................
  1285   1285       sqlite3_result_error(context,
  1286   1286         "edit() cannot reopen temp file after edit", -1);
  1287   1287       goto edit_func_end;
  1288   1288     }
  1289   1289     fseek(f, 0, SEEK_END);
  1290   1290     sz = ftell(f);
  1291   1291     rewind(f);
  1292         -  p = sqlite3_malloc64( sz+(bBin==0) );
         1292  +  p = sqlite3_malloc64( sz+1 );
  1293   1293     if( p==0 ){
  1294   1294       sqlite3_result_error_nomem(context);
  1295   1295       goto edit_func_end;
  1296   1296     }
  1297         -  x = fread(p, 1, sz, f);
         1297  +  x = fread(p, 1, (size_t)sz, f);
  1298   1298     fclose(f);
  1299   1299     f = 0;
  1300   1300     if( x!=sz ){
  1301   1301       sqlite3_result_error(context, "could not read back the whole file", -1);
  1302   1302       goto edit_func_end;
  1303   1303     }
  1304   1304     if( bBin ){
................................................................................
  1762   1762   static void eqp_render_level(ShellState *p, int iEqpId){
  1763   1763     EQPGraphRow *pRow, *pNext;
  1764   1764     int n = strlen30(p->sGraph.zPrefix);
  1765   1765     char *z;
  1766   1766     for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
  1767   1767       pNext = eqp_next_row(p, iEqpId, pRow);
  1768   1768       z = pRow->zText;
  1769         -    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
         1769  +    utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
         1770  +                pNext ? "|--" : "`--", z);
  1770   1771       if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
  1771   1772         memcpy(&p->sGraph.zPrefix[n], pNext ? "|  " : "   ", 4);
  1772   1773         eqp_render_level(p, pRow->iEqpId);
  1773   1774         p->sGraph.zPrefix[n] = 0;
  1774   1775       }
  1775   1776     }
  1776   1777   }
................................................................................
  1953   1954             j--;
  1954   1955           }
  1955   1956           z[j++] = c;
  1956   1957         }
  1957   1958         while( j>0 && IsSpace(z[j-1]) ){ j--; }
  1958   1959         z[j] = 0;
  1959   1960         if( strlen30(z)>=79 ){
  1960         -        for(i=j=0; (c = z[i])!=0; i++){  /* Copy changes from z[i] back to z[j] */
         1961  +        for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
  1961   1962             if( c==cEnd ){
  1962   1963               cEnd = 0;
  1963   1964             }else if( c=='"' || c=='\'' || c=='`' ){
  1964   1965               cEnd = c;
  1965   1966             }else if( c=='[' ){
  1966   1967               cEnd = ']';
  1967   1968             }else if( c=='-' && z[i+1]=='-' ){
................................................................................
  2532   2533       raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
  2533   2534       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
  2534   2535       raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
  2535   2536       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
  2536   2537       raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
  2537   2538       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
  2538   2539       raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
  2539         -    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
         2540  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
  2540   2541       raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
  2541   2542       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
  2542   2543       raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
  2543   2544       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
  2544   2545       raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
  2545   2546     }
  2546   2547   
................................................................................
  3455   3456   ** start of the description of what that command does.
  3456   3457   */
  3457   3458   static const char *(azHelp[]) = {
  3458   3459   #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
  3459   3460     ".archive ...             Manage SQL archives",
  3460   3461     "   Each command must have exactly one of the following options:",
  3461   3462     "     -c, --create               Create a new archive",
  3462         -  "     -u, --update               Add files or update files with changed mtime",
  3463         -  "     -i, --insert               Like -u but always add even if mtime unchanged",
         3463  +  "     -u, --update               Add or update files with changed mtime",
         3464  +  "     -i, --insert               Like -u but always add even if unchanged",
  3464   3465     "     -t, --list                 List contents of archive",
  3465   3466     "     -x, --extract              Extract files from archive",
  3466   3467     "   Optional arguments:",
  3467   3468     "     -v, --verbose              Print each filename as it is processed",
  3468         -  "     -f FILE, --file FILE       Operate on archive FILE (default is current db)",
  3469         -  "     -a FILE, --append FILE     Operate on FILE opened using the apndvfs VFS",
  3470         -  "     -C DIR, --directory DIR    Change to directory DIR to read/extract files",
         3469  +  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
         3470  +  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
         3471  +  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
  3471   3472     "     -n, --dryrun               Show the SQL that would have occurred",
  3472   3473     "   Examples:",
  3473         -  "     .ar -cf archive.sar foo bar  # Create archive.sar from files foo and bar",
  3474         -  "     .ar -tf archive.sar          # List members of archive.sar",
  3475         -  "     .ar -xvf archive.sar         # Verbosely extract files from archive.sar",
         3474  +  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
         3475  +  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
         3476  +  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
  3476   3477     "   See also:",
  3477   3478     "      http://sqlite.org/cli.html#sqlar_archive_support",
  3478   3479   #endif
  3479   3480   #ifndef SQLITE_OMIT_AUTHORIZATION
  3480   3481     ".auth ON|OFF             Show authorizer callbacks",
  3481   3482   #endif
  3482   3483     ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
  3483   3484     "       --append            Use the appendvfs",
  3484         -  "       --async             Write to FILE without a journal and without fsync()",
         3485  +  "       --async             Write to FILE without journal and fsync()",
  3485   3486     ".bail on|off             Stop after hitting an error.  Default OFF",
  3486   3487     ".binary on|off           Turn binary output on or off.  Default OFF",
  3487   3488     ".cd DIRECTORY            Change the working directory to DIRECTORY",
  3488   3489     ".changes on|off          Show number of rows changed by SQL",
  3489   3490     ".check GLOB              Fail if output since .testcase does not match",
  3490   3491     ".clone NEWDB             Clone data into NEWDB from the existing database",
  3491   3492     ".databases               List names and files of attached databases",
................................................................................
  3497   3498     "     --newlines             Allow unescaped newline characters in output",
  3498   3499     "   TABLE is a LIKE pattern for the tables to dump",
  3499   3500     ".echo on|off             Turn command echo on or off",
  3500   3501     ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
  3501   3502     "   Other Modes:",
  3502   3503   #ifdef SQLITE_DEBUG
  3503   3504     "      test                  Show raw EXPLAIN QUERY PLAN output",
  3504         -  "      trace                 Like \"full\" but also enable \"PRAGMA vdbe_trace\"",
         3505  +  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
  3505   3506   #endif
  3506   3507     "      trigger               Like \"full\" but also show trigger bytecode",
  3507         -  ".excel                   Display the output of next command in a spreadsheet",
         3508  +  ".excel                   Display the output of next command in spreadsheet",
  3508   3509     ".exit ?CODE?             Exit this program with return-code CODE",
  3509         -  ".expert                  EXPERIMENTAL. Suggest indexes for specified queries",
         3510  +  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
  3510   3511   /* Because explain mode comes on automatically now, the ".explain" mode
  3511         -** is removed from the help screen.  It is still supported for legacy, however */
  3512         -/*".explain ?on|off|auto?   Turn EXPLAIN output mode on or off or to automatic",*/
         3512  +** is removed from the help screen. It is still supported for legacy, however */
         3513  +/*".explain ?on|off|auto?   Turn EXPLAIN output mode on or off",*/
  3513   3514     ".filectrl CMD ...        Run various sqlite3_file_control() operations",
  3514   3515     "                           Run \".filectrl\" with no arguments for details",
  3515   3516     ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
  3516   3517     ".headers on|off          Turn display of headers on or off",
  3517   3518     ".help ?-all? ?PATTERN?   Show help text for PATTERN",
  3518   3519     ".import FILE TABLE       Import data from FILE into TABLE",
  3519   3520   #ifndef SQLITE_OMIT_TEST_CONTROL
................................................................................
  3552   3553     "       -e    Invoke system text editor",
  3553   3554     "       -x    Open in a spreadsheet",
  3554   3555     ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
  3555   3556     "     Options:",
  3556   3557     "        --append        Use appendvfs to append database to the end of FILE",
  3557   3558   #ifdef SQLITE_ENABLE_DESERIALIZE
  3558   3559     "        --deserialize   Load into memory useing sqlite3_deserialize()",
  3559         -  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory database",
         3560  +  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
  3560   3561     "        --maxsize N     Maximum size for --hexdb or --deserialized database",
  3561   3562   #endif
  3562   3563     "        --new           Initialize FILE to an empty database",
  3563   3564     "        --readonly      Open FILE readonly",
  3564   3565     "        --zip           FILE is a ZIP archive",
  3565   3566     ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
  3566   3567     "     If FILE begins with '|' then open it as a pipe.",
  3567   3568     ".parameter CMD ...       Manage SQL parameter bindings",
  3568   3569     "   clear                   Erase all bindings",
  3569   3570     "   init                    Initialize the TEMP table that holds bindings",
  3570   3571     "   list                    List the current parameter bindings",
  3571   3572     "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
  3572         -  "                           PARAMETER should start with '$', ':', '@', or '?'",
         3573  +  "                           PARAMETER should start with one of: $ : @ ?",
  3573   3574     "   unset PARAMETER         Remove PARAMETER from the binding table",
  3574   3575     ".print STRING...         Print literal STRING",
  3575   3576   #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
  3576   3577     ".progress N              Invoke progress handler after every N opcodes",
  3577   3578     "   --limit N                 Interrupt after N progress callbacks",
  3578   3579     "   --once                    Do no more than one progress interrupt",
  3579   3580     "   --quiet|-q                No output except at interrupts",
................................................................................
  3580   3581     "   --reset                   Reset the count for each input and interrupt",
  3581   3582   #endif
  3582   3583     ".prompt MAIN CONTINUE    Replace the standard prompts",
  3583   3584     ".quit                    Exit this program",
  3584   3585     ".read FILE               Read input from FILE",
  3585   3586   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
  3586   3587     ".recover                 Recover as much data as possible from corrupt db.",
         3588  +  "   --freelist-corrupt       Assume the freelist is corrupt",
         3589  +  "   --recovery-db NAME       Store recovery metadata in database file NAME",
         3590  +  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
         3591  +  "   --no-rowids              Do not attempt to recover rowid values",
         3592  +  "                            that are not also INTEGER PRIMARY KEYs",
  3587   3593   #endif
  3588   3594     ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
  3589   3595     ".save FILE               Write in-memory database into FILE",
  3590   3596     ".scanstats on|off        Turn sqlite3_stmt_scanstatus() metrics on or off",
  3591   3597     ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
  3592   3598     "     Options:",
  3593   3599     "         --indent            Try to pretty-print the schema",
................................................................................
  3611   3617     "     patchset FILE            Write a patchset into FILE",
  3612   3618     "   If ?NAME? is omitted, the first defined session is used.",
  3613   3619   #endif
  3614   3620     ".sha3sum ...             Compute a SHA3 hash of database content",
  3615   3621     "    Options:",
  3616   3622     "      --schema              Also hash the sqlite_master table",
  3617   3623     "      --sha3-224            Use the sha3-224 algorithm",
  3618         -  "      --sha3-256            Use the sha3-256 algorithm.  This is the default.",
         3624  +  "      --sha3-256            Use the sha3-256 algorithm (default)",
  3619   3625     "      --sha3-384            Use the sha3-384 algorithm",
  3620   3626     "      --sha3-512            Use the sha3-512 algorithm",
  3621   3627     "    Any other argument is a LIKE pattern for tables to hash",
  3622   3628   #ifndef SQLITE_NOHAVE_SYSTEM
  3623   3629     ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
  3624   3630   #endif
  3625   3631     ".show                    Show the current values for various settings",
................................................................................
  3645   3651   #endif
  3646   3652     "    --plain                 Show SQL as it is input",
  3647   3653     "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
  3648   3654     "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
  3649   3655     "    --row                   Trace each row (SQLITE_TRACE_ROW)",
  3650   3656     "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
  3651   3657   #endif /* SQLITE_OMIT_TRACE */
         3658  +#ifdef SQLITE_DEBUG
         3659  +  ".unmodule NAME ...       Unregister virtual table modules",
         3660  +  "    --allexcept             Unregister everything except those named",
         3661  +#endif
  3652   3662     ".vfsinfo ?AUX?           Information about the top-level VFS",
  3653   3663     ".vfslist                 List all available VFSes",
  3654   3664     ".vfsname ?AUX?           Print the name of the VFS stack",
  3655   3665     ".width NUM1 NUM2 ...     Set column widths for \"column\" mode",
  3656   3666     "     Negative values right-justify",
  3657   3667   };
  3658   3668   
................................................................................
  3887   3897     }
  3888   3898     *pnData = 0;
  3889   3899     nLine++;
  3890   3900     if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
  3891   3901     rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
  3892   3902     if( rc!=2 ) goto readHexDb_error;
  3893   3903     if( n<0 ) goto readHexDb_error;
         3904  +  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
         3905  +  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
  3894   3906     a = sqlite3_malloc( n ? n : 1 );
  3895   3907     if( a==0 ){
  3896   3908       utf8_printf(stderr, "Out of memory!\n");
  3897   3909       goto readHexDb_error;
  3898   3910     }
  3899   3911     memset(a, 0, n);
  3900   3912     if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
................................................................................
  3970   3982       sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
  3971   3983                          + ((sqlite3_int64)a[1]<<16)
  3972   3984                          + ((sqlite3_int64)a[2]<< 8)
  3973   3985                          + ((sqlite3_int64)a[3]<< 0);
  3974   3986       sqlite3_result_int64(context, iVal);
  3975   3987     }
  3976   3988   }
         3989  +
         3990  +/*
         3991  +** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
         3992  +** using "..." with internal double-quote characters doubled.
         3993  +*/
         3994  +static void shellIdQuote(
         3995  +  sqlite3_context *context, 
         3996  +  int argc, 
         3997  +  sqlite3_value **argv
         3998  +){
         3999  +  const char *zName = (const char*)sqlite3_value_text(argv[0]);
         4000  +  UNUSED_PARAMETER(argc);
         4001  +  if( zName ){
         4002  +    char *z = sqlite3_mprintf("\"%w\"", zName);
         4003  +    sqlite3_result_text(context, z, -1, sqlite3_free);
         4004  +  }
         4005  +}
  3977   4006   
  3978   4007   /*
  3979   4008   ** Scalar function "shell_escape_crnl" used by the .recover command.
  3980   4009   ** The argument passed to this function is the output of built-in
  3981   4010   ** function quote(). If the first character of the input is "'", 
  3982   4011   ** indicating that the value passed to quote() was a text value,
  3983   4012   ** then this function searches the input for "\n" and "\r" characters
................................................................................
  4147   4176                               shellModuleSchema, 0, 0);
  4148   4177       sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
  4149   4178                               shellPutsFunc, 0, 0);
  4150   4179       sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
  4151   4180                               shellEscapeCrnl, 0, 0);
  4152   4181       sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
  4153   4182                               shellInt32, 0, 0);
         4183  +    sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
         4184  +                            shellIdQuote, 0, 0);
  4154   4185   #ifndef SQLITE_NOHAVE_SYSTEM
  4155   4186       sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
  4156   4187                               editFunc, 0, 0);
  4157   4188       sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
  4158   4189                               editFunc, 0, 0);
  4159   4190   #endif
  4160   4191       if( p->openMode==SHELL_OPEN_ZIPFILE ){
................................................................................
  5494   5525       }
  5495   5526       *pRc = rc;
  5496   5527     }
  5497   5528   }
  5498   5529   #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
  5499   5530   
  5500   5531   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
  5501         -/*********************************************************************************
         5532  +/******************************************************************************
  5502   5533   ** The ".archive" or ".ar" command.
  5503   5534   */
  5504   5535   /*
  5505   5536   ** Structure representing a single ".ar" command.
  5506   5537   */
  5507   5538   typedef struct ArCommand ArCommand;
  5508   5539   struct ArCommand {
................................................................................
  5692   5723               }
  5693   5724               if( pOpt->bArg ){
  5694   5725                 if( i<(n-1) ){
  5695   5726                   zArg = &z[i+1];
  5696   5727                   i = n;
  5697   5728                 }else{
  5698   5729                   if( iArg>=(nArg-1) ){
  5699         -                  return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
         5730  +                  return arErrorMsg(pAr, "option requires an argument: %c",
         5731  +                                    z[i]);
  5700   5732                   }
  5701   5733                   zArg = azArg[++iArg];
  5702   5734                 }
  5703   5735               }
  5704   5736               if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
  5705   5737             }
  5706   5738           }else if( z[2]=='\0' ){
................................................................................
  6080   6112     return rc;
  6081   6113   }
  6082   6114   
  6083   6115   /*
  6084   6116   ** Implementation of ".ar" dot command.
  6085   6117   */
  6086   6118   static int arDotCommand(
  6087         -  ShellState *pState,             /* Current shell tool state */
  6088         -  int fromCmdLine,                /* True if -A command-line option, not .ar cmd */
  6089         -  char **azArg,                   /* Array of arguments passed to dot command */
  6090         -  int nArg                        /* Number of entries in azArg[] */
         6119  +  ShellState *pState,          /* Current shell tool state */
         6120  +  int fromCmdLine,             /* True if -A command-line option, not .ar cmd */
         6121  +  char **azArg,                /* Array of arguments passed to dot command */
         6122  +  int nArg                     /* Number of entries in azArg[] */
  6091   6123   ){
  6092   6124     ArCommand cmd;
  6093   6125     int rc;
  6094   6126     memset(&cmd, 0, sizeof(cmd));
  6095   6127     cmd.fromCmdLine = fromCmdLine;
  6096   6128     rc = arParseCommand(azArg, nArg, &cmd);
  6097   6129     if( rc==SQLITE_OK ){
................................................................................
  6183   6215       close_db(cmd.db);
  6184   6216     }
  6185   6217     sqlite3_free(cmd.zSrcTable);
  6186   6218   
  6187   6219     return rc;
  6188   6220   }
  6189   6221   /* End of the ".archive" or ".ar" command logic
  6190         -**********************************************************************************/
         6222  +*******************************************************************************/
  6191   6223   #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
  6192   6224   
  6193   6225   #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
  6194   6226   /*
  6195   6227   ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
  6196   6228   ** Otherwise, the SQL statement or statements in zSql are executed using
  6197   6229   ** database connection db and the error code written to *pRc before
................................................................................
  6324   6356     pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
  6325   6357     if( rc==SQLITE_OK ){
  6326   6358       int nSqlCol = 0;
  6327   6359       int bSqlIntkey = 0;
  6328   6360       sqlite3_stmt *pStmt = 0;
  6329   6361       
  6330   6362       rc = sqlite3_open("", &dbtmp);
         6363  +    if( rc==SQLITE_OK ){
         6364  +      sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
         6365  +                              shellIdQuote, 0, 0);
         6366  +    }
  6331   6367       if( rc==SQLITE_OK ){
  6332   6368         rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
  6333   6369       }
  6334   6370       if( rc==SQLITE_OK ){
  6335   6371         rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
  6336   6372         if( rc==SQLITE_ERROR ){
  6337   6373           rc = SQLITE_OK;
................................................................................
  6380   6416           );
  6381   6417           if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
  6382   6418             pTab->iPk = sqlite3_column_int(pPkFinder, 0);
  6383   6419             zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
  6384   6420           }
  6385   6421         }
  6386   6422   
  6387         -      pTab->zQuoted = shellMPrintf(&rc, "%Q", zName);
         6423  +      pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
  6388   6424         pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
  6389   6425         pTab->nCol = nSqlCol;
  6390   6426   
  6391   6427         if( bIntkey ){
  6392         -        pTab->azlCol[0] = shellMPrintf(&rc, "%Q", zPk);
         6428  +        pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
  6393   6429         }else{
  6394   6430           pTab->azlCol[0] = shellMPrintf(&rc, "");
  6395   6431         }
  6396   6432         i = 1;
  6397   6433         shellPreparePrintf(dbtmp, &rc, &pStmt, 
  6398         -          "SELECT %Q || group_concat(name, ', ') "
         6434  +          "SELECT %Q || group_concat(shell_idquote(name), ', ') "
  6399   6435             "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
  6400   6436             "FROM pragma_table_info(%Q)", 
  6401   6437             bIntkey ? ", " : "", pTab->iPk, 
  6402   6438             bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
  6403   6439             zName
  6404   6440         );
  6405   6441         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
................................................................................
  6505   6541         zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
  6506   6542         sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
  6507   6543       }
  6508   6544       shellFinalize(pRc, pTest);
  6509   6545   
  6510   6546       pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
  6511   6547       if( pTab ){
  6512         -      pTab->zQuoted = shellMPrintf(pRc, "%Q", zTab);
         6548  +      pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
  6513   6549         pTab->nCol = nCol;
  6514   6550         pTab->iPk = -2;
  6515   6551         if( nCol>0 ){
  6516   6552           pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
  6517   6553           if( pTab->azlCol ){
  6518   6554             pTab->azlCol[nCol] = shellMPrintf(pRc, "");
  6519   6555             for(i=nCol-1; i>=0; i--){
................................................................................
  6554   6590     const char *zRecoveryDb = "";   /* Name of "recovery" database */
  6555   6591     const char *zLostAndFound = "lost_and_found";
  6556   6592     int i;
  6557   6593     int nOrphan = -1;
  6558   6594     RecoverTable *pOrphan = 0;
  6559   6595   
  6560   6596     int bFreelist = 1;              /* 0 if --freelist-corrupt is specified */
         6597  +  int bRowids = 1;                /* 0 if --no-rowids */
  6561   6598     for(i=1; i<nArg; i++){
  6562   6599       char *z = azArg[i];
  6563   6600       int n;
  6564   6601       if( z[0]=='-' && z[1]=='-' ) z++;
  6565   6602       n = strlen30(z);
  6566   6603       if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
  6567   6604         bFreelist = 0;
................................................................................
  6569   6606       if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
  6570   6607         i++;
  6571   6608         zRecoveryDb = azArg[i];
  6572   6609       }else
  6573   6610       if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
  6574   6611         i++;
  6575   6612         zLostAndFound = azArg[i];
         6613  +    }else
         6614  +    if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
         6615  +      bRowids = 0;
  6576   6616       }
  6577   6617       else{
  6578         -      raw_printf(stderr, "unexpected option: %s\n", azArg[i]); 
  6579         -      raw_printf(stderr, "options are:\n");
  6580         -      raw_printf(stderr, "    --freelist-corrupt\n");
  6581         -      raw_printf(stderr, "    --recovery-db DATABASE\n");
  6582         -      raw_printf(stderr, "    --lost-and-found TABLE-NAME\n");
         6618  +      utf8_printf(stderr, "unexpected option: %s\n", azArg[i]); 
         6619  +      showHelp(pState->out, azArg[0]);
  6583   6620         return 1;
  6584   6621       }
  6585   6622     }
  6586   6623   
  6587   6624     shellExecPrintf(pState->db, &rc,
  6588   6625       /* Attach an in-memory database named 'recovery'. Create an indexed 
  6589   6626       ** cache of the sqlite_dbptr virtual table. */
         6627  +    "PRAGMA writable_schema = on;"
  6590   6628       "ATTACH %Q AS recovery;"
  6591   6629       "DROP TABLE IF EXISTS recovery.dbptr;"
  6592   6630       "DROP TABLE IF EXISTS recovery.freelist;"
  6593   6631       "DROP TABLE IF EXISTS recovery.map;"
  6594   6632       "DROP TABLE IF EXISTS recovery.schema;"
  6595   6633       "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
  6596   6634     );
................................................................................
  6612   6650         "    UNION ALL"
  6613   6651         "  SELECT data, n-1, shell_int32(data, 2+n) "
  6614   6652         "      FROM freelist WHERE n>=0"
  6615   6653         ")"
  6616   6654         "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
  6617   6655       );
  6618   6656     }
         6657  +
         6658  +  /* If this is an auto-vacuum database, add all pointer-map pages to
         6659  +  ** the freelist table. Do this regardless of whether or not 
         6660  +  ** --freelist-corrupt was specified.  */
         6661  +  shellExec(pState->db, &rc, 
         6662  +    "WITH ptrmap(pgno) AS ("
         6663  +    "  SELECT 2 WHERE shell_int32("
         6664  +    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
         6665  +    "  )"
         6666  +    "    UNION ALL "
         6667  +    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
         6668  +    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
         6669  +    ")"
         6670  +    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
         6671  +  );
  6619   6672   
  6620   6673     shellExec(pState->db, &rc, 
  6621   6674       "CREATE TABLE recovery.dbptr("
  6622   6675       "      pgno, child, PRIMARY KEY(child, pgno)"
  6623   6676       ") WITHOUT ROWID;"
  6624   6677       "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
  6625   6678       "    SELECT * FROM sqlite_dbptr"
................................................................................
  6661   6714       "      SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
  6662   6715       "        UNION "
  6663   6716       "      SELECT i, p.parent, "
  6664   6717       "        (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
  6665   6718       "    )"
  6666   6719       "    SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
  6667   6720       ") "
  6668         -    "FROM pages WHERE maxlen > 0 AND i NOT IN freelist;"
         6721  +    "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
  6669   6722       "UPDATE recovery.map AS o SET intkey = ("
  6670   6723       "  SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
  6671   6724       ");"
  6672   6725   
  6673   6726       /* Extract data from page 1 and any linked pages into table
  6674   6727       ** recovery.schema. With the same schema as an sqlite_master table.  */
  6675   6728       "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
................................................................................
  6686   6739       "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
  6687   6740     );
  6688   6741   
  6689   6742     /* Open a transaction, then print out all non-virtual, non-"sqlite_%" 
  6690   6743     ** CREATE TABLE statements that extracted from the existing schema.  */
  6691   6744     if( rc==SQLITE_OK ){
  6692   6745       sqlite3_stmt *pStmt = 0;
         6746  +    /* ".recover" might output content in an order which causes immediate
         6747  +    ** foreign key constraints to be violated. So disable foreign-key
         6748  +    ** constraint enforcement to prevent problems when running the output
         6749  +    ** script. */
         6750  +    raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
  6693   6751       raw_printf(pState->out, "BEGIN;\n");
  6694   6752       raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
  6695   6753       shellPrepare(pState->db, &rc,
  6696   6754           "SELECT sql FROM recovery.schema "
  6697   6755           "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
  6698   6756       );
  6699   6757       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
................................................................................
  6716   6774     }
  6717   6775     shellFinalize(&rc, pLoop);
  6718   6776     pLoop = 0;
  6719   6777   
  6720   6778     shellPrepare(pState->db, &rc,
  6721   6779         "SELECT pgno FROM recovery.map WHERE root=?", &pPages
  6722   6780     );
         6781  +
  6723   6782     shellPrepare(pState->db, &rc,
  6724         -      "SELECT max(field), group_concat(shell_escape_crnl(quote(value)), ', ')"
         6783  +      "SELECT max(field), group_concat(shell_escape_crnl(quote"
         6784  +      "(case when (? AND field<0) then NULL else value end)"
         6785  +      "), ', ')"
         6786  +      ", min(field) "
  6725   6787         "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
  6726   6788         "GROUP BY cell", &pCells
  6727   6789     );
  6728   6790   
  6729   6791     /* Loop through each root page. */
  6730   6792     shellPrepare(pState->db, &rc, 
  6731   6793         "SELECT root, intkey, max(maxlen) FROM recovery.map" 
................................................................................
  6736   6798     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
  6737   6799       int iRoot = sqlite3_column_int(pLoop, 0);
  6738   6800       int bIntkey = sqlite3_column_int(pLoop, 1);
  6739   6801       int nCol = sqlite3_column_int(pLoop, 2);
  6740   6802       int bNoop = 0;
  6741   6803       RecoverTable *pTab;
  6742   6804   
         6805  +    assert( bIntkey==0 || bIntkey==1 );
  6743   6806       pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
  6744   6807       if( bNoop || rc ) continue;
  6745   6808       if( pTab==0 ){
  6746   6809         if( pOrphan==0 ){
  6747   6810           pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
  6748   6811         }
  6749   6812         pTab = pOrphan;
  6750   6813         if( pTab==0 ) break;
  6751   6814       }
  6752   6815   
  6753         -    if( 0==sqlite3_stricmp(pTab->zQuoted, "'sqlite_sequence'") ){
         6816  +    if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
  6754   6817         raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
  6755   6818       }
  6756   6819       sqlite3_bind_int(pPages, 1, iRoot);
  6757         -    sqlite3_bind_int(pCells, 2, pTab->iPk);
         6820  +    if( bRowids==0 && pTab->iPk<0 ){
         6821  +      sqlite3_bind_int(pCells, 1, 1);
         6822  +    }else{
         6823  +      sqlite3_bind_int(pCells, 1, 0);
         6824  +    }
         6825  +    sqlite3_bind_int(pCells, 3, pTab->iPk);
  6758   6826   
  6759   6827       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
  6760   6828         int iPgno = sqlite3_column_int(pPages, 0);
  6761         -      sqlite3_bind_int(pCells, 1, iPgno);
         6829  +      sqlite3_bind_int(pCells, 2, iPgno);
  6762   6830         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
  6763   6831           int nField = sqlite3_column_int(pCells, 0);
         6832  +        int iMin = sqlite3_column_int(pCells, 2);
  6764   6833           const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
         6834  +
         6835  +        RecoverTable *pTab2 = pTab;
         6836  +        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
         6837  +          if( pOrphan==0 ){
         6838  +            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
         6839  +          }
         6840  +          pTab2 = pOrphan;
         6841  +          if( pTab2==0 ) break;
         6842  +        }
  6765   6843   
  6766   6844           nField = nField+1;
  6767         -        if( pTab==pOrphan ){
         6845  +        if( pTab2==pOrphan ){
  6768   6846             raw_printf(pState->out, 
  6769   6847                 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
  6770         -              pTab->zQuoted, iRoot, iPgno, nField, 
  6771         -              bIntkey ? "" : "NULL, ", zVal, pTab->azlCol[nField]
         6848  +              pTab2->zQuoted, iRoot, iPgno, nField,
         6849  +              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
  6772   6850             );
  6773   6851           }else{
  6774   6852             raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 
  6775         -              pTab->zQuoted, pTab->azlCol[nField], zVal
         6853  +              pTab2->zQuoted, pTab2->azlCol[nField], zVal
  6776   6854             );
  6777   6855           }
  6778   6856         }
  6779   6857         shellReset(&rc, pCells);
  6780   6858       }
  6781   6859       shellReset(&rc, pPages);
  6782   6860       if( pTab!=pOrphan ) recoverFreeTable(pTab);
................................................................................
  6827   6905   ** Return 1 on error, 2 to exit, and 0 otherwise.
  6828   6906   */
  6829   6907   static int do_meta_command(char *zLine, ShellState *p){
  6830   6908     int h = 1;
  6831   6909     int nArg = 0;
  6832   6910     int n, c;
  6833   6911     int rc = 0;
  6834         -  char *azArg[50];
         6912  +  char *azArg[52];
  6835   6913   
  6836   6914   #ifndef SQLITE_OMIT_VIRTUALTABLE
  6837   6915     if( p->expert.pExpert ){
  6838   6916       expertFinish(p, 1, 0);
  6839   6917     }
  6840   6918   #endif
  6841   6919   
  6842   6920     /* Parse the input line into tokens.
  6843   6921     */
  6844         -  while( zLine[h] && nArg<ArraySize(azArg) ){
         6922  +  while( zLine[h] && nArg<ArraySize(azArg)-1 ){
  6845   6923       while( IsSpace(zLine[h]) ){ h++; }
  6846   6924       if( zLine[h]==0 ) break;
  6847   6925       if( zLine[h]=='\'' || zLine[h]=='"' ){
  6848   6926         int delim = zLine[h++];
  6849   6927         azArg[nArg++] = &zLine[h];
  6850   6928         while( zLine[h] && zLine[h]!=delim ){
  6851   6929           if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
................................................................................
  6858   6936       }else{
  6859   6937         azArg[nArg++] = &zLine[h];
  6860   6938         while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
  6861   6939         if( zLine[h] ) zLine[h++] = 0;
  6862   6940         resolve_backslashes(azArg[nArg-1]);
  6863   6941       }
  6864   6942     }
         6943  +  azArg[nArg] = 0;
  6865   6944   
  6866   6945     /* Process the input line.
  6867   6946     */
  6868   6947     if( nArg==0 ) return 0; /* no tokens, no error */
  6869   6948     n = strlen30(azArg[0]);
  6870   6949     c = azArg[0][0];
  6871   6950     clearTempFile(p);
................................................................................
  7073   7152     if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
  7074   7153       static const struct DbConfigChoices {
  7075   7154         const char *zName;
  7076   7155         int op;
  7077   7156       } aDbConfig[] = {
  7078   7157           { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
  7079   7158           { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
         7159  +        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
  7080   7160           { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
  7081   7161           { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
  7082   7162           { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
  7083   7163           { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
  7084   7164           { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
  7085   7165           { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
  7086   7166           { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
................................................................................
  7446   7526       }else{
  7447   7527         raw_printf(p->out, "ANALYZE sqlite_master;\n");
  7448   7528         sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
  7449   7529                      callback, &data, &zErrMsg);
  7450   7530         data.cMode = data.mode = MODE_Insert;
  7451   7531         data.zDestTable = "sqlite_stat1";
  7452   7532         shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
  7453         -      data.zDestTable = "sqlite_stat3";
  7454         -      shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
  7455   7533         data.zDestTable = "sqlite_stat4";
  7456   7534         shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
  7457   7535         raw_printf(p->out, "ANALYZE sqlite_master;\n");
  7458   7536       }
  7459   7537     }else
  7460   7538   
  7461   7539     if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
................................................................................
  8384   8462           const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
  8385   8463           char zScNum[30];
  8386   8464           sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
  8387   8465           appendText(&sSelect, zDiv, 0);
  8388   8466           zDiv = " UNION ALL ";
  8389   8467           appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
  8390   8468           if( sqlite3_stricmp(zDb, "main")!=0 ){
  8391         -          appendText(&sSelect, zDb, '"');
         8469  +          appendText(&sSelect, zDb, '\'');
  8392   8470           }else{
  8393   8471             appendText(&sSelect, "NULL", 0);
  8394   8472           }
  8395   8473           appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
  8396   8474           appendText(&sSelect, zScNum, 0);
  8397   8475           appendText(&sSelect, " AS snum, ", 0);
  8398   8476           appendText(&sSelect, zDb, '\'');
  8399   8477           appendText(&sSelect, " AS sname FROM ", 0);
  8400         -        appendText(&sSelect, zDb, '"');
         8478  +        appendText(&sSelect, zDb, quoteChar(zDb));
  8401   8479           appendText(&sSelect, ".sqlite_master", 0);
  8402   8480         }
  8403   8481         sqlite3_finalize(pStmt);
  8404         -#ifdef SQLITE_INTROSPECTION_PRAGMAS
         8482  +#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
  8405   8483         if( zName ){
  8406   8484           appendText(&sSelect,
  8407   8485              " UNION ALL SELECT shell_module_schema(name),"
  8408         -           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
         8486  +           " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
         8487  +        0);
  8409   8488         }
  8410   8489   #endif
  8411   8490         appendText(&sSelect, ") WHERE ", 0);
  8412   8491         if( zName ){
  8413   8492           char *zQarg = sqlite3_mprintf("%Q", zName);
  8414   8493           int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
  8415   8494                       strchr(zName, '[') != 0;
................................................................................
  8500   8579       */
  8501   8580       if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
  8502   8581         FILE *out = 0;
  8503   8582         if( nCmd!=2 ) goto session_syntax_error;
  8504   8583         if( pSession->p==0 ) goto session_not_open;
  8505   8584         out = fopen(azCmd[1], "wb");
  8506   8585         if( out==0 ){
  8507         -        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
         8586  +        utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
         8587  +                    azCmd[1]);
  8508   8588         }else{
  8509   8589           int szChng;
  8510   8590           void *pChng;
  8511   8591           if( azCmd[0][0]=='c' ){
  8512   8592             rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
  8513   8593           }else{
  8514   8594             rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
................................................................................
  8821   8901           }else
  8822   8902           if( strcmp(z,"debug")==0 ){
  8823   8903             bDebug = 1;
  8824   8904           }else
  8825   8905           {
  8826   8906             utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
  8827   8907                         azArg[i], azArg[0]);
  8828         -          raw_printf(stderr, "Should be one of: --schema"
  8829         -                             " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
         8908  +          showHelp(p->out, azArg[0]);
  8830   8909             rc = 1;
  8831   8910             goto meta_command_exit;
  8832   8911           }
  8833   8912         }else if( zLike ){
  8834   8913           raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
  8835   8914           rc = 1;
  8836   8915           goto meta_command_exit;
................................................................................
  8868   8947                              " ORDER BY name;", 0);
  8869   8948         }else if( strcmp(zTab, "sqlite_sequence")==0 ){
  8870   8949           appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
  8871   8950                              " ORDER BY name;", 0);
  8872   8951         }else if( strcmp(zTab, "sqlite_stat1")==0 ){
  8873   8952           appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
  8874   8953                              " ORDER BY tbl,idx;", 0);
  8875         -      }else if( strcmp(zTab, "sqlite_stat3")==0
  8876         -             || strcmp(zTab, "sqlite_stat4")==0 ){
         8954  +      }else if( strcmp(zTab, "sqlite_stat4")==0 ){
  8877   8955           appendText(&sQuery, "SELECT * FROM ", 0);
  8878   8956           appendText(&sQuery, zTab, 0);
  8879   8957           appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
  8880   8958         }
  8881   8959         appendText(&sSql, zSep, 0);
  8882   8960         appendText(&sSql, sQuery.z, '\'');
  8883   8961         sQuery.n = 0;
................................................................................
  9101   9179   #ifndef SQLITE_UNTESTABLE
  9102   9180     if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
  9103   9181       static const struct {
  9104   9182          const char *zCtrlName;   /* Name of a test-control option */
  9105   9183          int ctrlCode;            /* Integer code for that option */
  9106   9184          const char *zUsage;      /* Usage notes */
  9107   9185       } aCtrl[] = {
  9108         -      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
  9109         -      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
  9110         -    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
  9111         -    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
  9112         -      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
  9113         -    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
  9114         -      { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
  9115         -      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"       },
  9116         -      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
  9117         -      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
  9118         -      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
         9186  +      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"        },
         9187  +      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"        },
         9188  +    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""       },*/
         9189  +    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""             },*/
         9190  +      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""               },
         9191  +      { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN"   },
         9192  +    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""             },*/
         9193  +      { "imposter",         SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
         9194  +      { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN"   },
         9195  +      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"       },
         9196  +      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"        },
         9197  +      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"   },
  9119   9198   #ifdef YYCOVERAGE
  9120         -      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""                 },
         9199  +      { "parser_coverage",    SQLITE_TESTCTRL_PARSER_COVERAGE, ""             },
  9121   9200   #endif
  9122         -      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
  9123         -      { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
  9124         -      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
  9125         -      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
  9126         -      { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
         9201  +      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "       },
         9202  +      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""               },
         9203  +      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""               },
         9204  +      { "prng_seed",          SQLITE_TESTCTRL_PRNG_SEED,     "SEED ?db?"      },
         9205  +      { "reserve",            SQLITE_TESTCTRL_RESERVE,      "BYTES-OF-RESERVE"},
  9127   9206       };
  9128   9207       int testctrl = -1;
  9129   9208       int iCtrl = -1;
  9130   9209       int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
  9131   9210       int isOk = 0;
  9132   9211       int i, n2;
  9133   9212       const char *zCmd = 0;
................................................................................
  9199   9278           case SQLITE_TESTCTRL_PENDING_BYTE:
  9200   9279             if( nArg==3 ){
  9201   9280               unsigned int opt = (unsigned int)integerValue(azArg[2]);
  9202   9281               rc2 = sqlite3_test_control(testctrl, opt);
  9203   9282               isOk = 3;
  9204   9283             }
  9205   9284             break;
         9285  +
         9286  +        /* sqlite3_test_control(int, int, sqlite3*) */
         9287  +        case SQLITE_TESTCTRL_PRNG_SEED:
         9288  +          if( nArg==3 || nArg==4 ){
         9289  +            int ii = (int)integerValue(azArg[2]);
         9290  +            sqlite3 *db;
         9291  +            if( ii==0 && strcmp(azArg[2],"random")==0 ){
         9292  +              sqlite3_randomness(sizeof(ii),&ii);
         9293  +              printf("-- random seed: %d\n", ii);
         9294  +            }
         9295  +            if( nArg==3 ){
         9296  +              db = 0;
         9297  +            }else{
         9298  +              db = p->db;
         9299  +              /* Make sure the schema has been loaded */
         9300  +              sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
         9301  +            }
         9302  +            rc2 = sqlite3_test_control(testctrl, ii, db);
         9303  +            isOk = 3;
         9304  +          }
         9305  +          break;
  9206   9306   
  9207   9307           /* sqlite3_test_control(int, int) */
  9208   9308           case SQLITE_TESTCTRL_ASSERT:
  9209   9309           case SQLITE_TESTCTRL_ALWAYS:
  9210   9310           case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
  9211   9311             if( nArg==3 ){
  9212   9312               int opt = booleanValue(azArg[2]);
................................................................................
  9241   9341               sqlite3_test_control(testctrl, p->out);
  9242   9342               isOk = 3;
  9243   9343             }
  9244   9344   #endif
  9245   9345         }
  9246   9346       }
  9247   9347       if( isOk==0 && iCtrl>=0 ){
  9248         -      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
         9348  +      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
  9249   9349         rc = 1;
  9250   9350       }else if( isOk==1 ){
  9251   9351         raw_printf(p->out, "%d\n", rc2);
  9252   9352       }else if( isOk==2 ){
  9253   9353         raw_printf(p->out, "0x%08x\n", rc2);
  9254   9354       }
  9255   9355     }else
................................................................................
  9318   9418         sqlite3_trace_v2(p->db, 0, 0, 0);
  9319   9419       }else{
  9320   9420         if( mType==0 ) mType = SQLITE_TRACE_STMT;
  9321   9421         sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
  9322   9422       }
  9323   9423     }else
  9324   9424   #endif /* !defined(SQLITE_OMIT_TRACE) */
         9425  +
         9426  +#ifdef SQLITE_DEBUG
         9427  +  if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
         9428  +    int ii;
         9429  +    int lenOpt;
         9430  +    char *zOpt;
         9431  +    if( nArg<2 ){
         9432  +      raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
         9433  +      rc = 1;
         9434  +      goto meta_command_exit;
         9435  +    }
         9436  +    open_db(p, 0);
         9437  +    zOpt = azArg[1];
         9438  +    if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
         9439  +    lenOpt = (int)strlen(zOpt);
         9440  +    if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
         9441  +      assert( azArg[nArg]==0 );
         9442  +      sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
         9443  +    }else{
         9444  +      for(ii=1; ii<nArg; ii++){
         9445  +        sqlite3_create_module(p->db, azArg[ii], 0, 0);
         9446  +      }
         9447  +    }
         9448  +  }else
         9449  +#endif
  9325   9450   
  9326   9451   #if SQLITE_USER_AUTHENTICATION
  9327   9452     if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
  9328   9453       if( nArg<2 ){
  9329   9454         raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
  9330   9455         rc = 1;
  9331   9456         goto meta_command_exit;
................................................................................
  9333   9458       open_db(p, 0);
  9334   9459       if( strcmp(azArg[1],"login")==0 ){
  9335   9460         if( nArg!=4 ){
  9336   9461           raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
  9337   9462           rc = 1;
  9338   9463           goto meta_command_exit;
  9339   9464         }
  9340         -      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
         9465  +      rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
         9466  +                                     strlen30(azArg[3]));
  9341   9467         if( rc ){
  9342   9468           utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
  9343   9469           rc = 1;
  9344   9470         }
  9345   9471       }else if( strcmp(azArg[1],"add")==0 ){
  9346   9472         if( nArg!=5 ){
  9347   9473           raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");

Changes to src/sqlite.h.in.

  2089   2089   ** The first argument is an integer which is 0 to disable triggers,
  2090   2090   ** positive to enable triggers or negative to leave the setting unchanged.
  2091   2091   ** The second parameter is a pointer to an integer into which
  2092   2092   ** is written 0 or 1 to indicate whether triggers are disabled or enabled
  2093   2093   ** following this call.  The second parameter may be a NULL pointer, in
  2094   2094   ** which case the trigger setting is not reported back. </dd>
  2095   2095   **
         2096  +** [[SQLITE_DBCONFIG_ENABLE_VIEW]]
         2097  +** <dt>SQLITE_DBCONFIG_ENABLE_VIEW</dt>
         2098  +** <dd> ^This option is used to enable or disable [CREATE VIEW | views].
         2099  +** There should be two additional arguments.
         2100  +** The first argument is an integer which is 0 to disable views,
         2101  +** positive to enable views or negative to leave the setting unchanged.
         2102  +** The second parameter is a pointer to an integer into which
         2103  +** is written 0 or 1 to indicate whether views are disabled or enabled
         2104  +** following this call.  The second parameter may be a NULL pointer, in
         2105  +** which case the view setting is not reported back. </dd>
         2106  +**
  2096   2107   ** [[SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER]]
  2097   2108   ** <dt>SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER</dt>
  2098   2109   ** <dd> ^This option is used to enable or disable the
  2099   2110   ** [fts3_tokenizer()] function which is part of the
  2100   2111   ** [FTS3] full-text search engine extension.
  2101   2112   ** There should be two additional arguments.
  2102   2113   ** The first argument is an integer which is 0 to disable fts3_tokenizer() or
................................................................................
  2261   2272   #define SQLITE_DBCONFIG_TRIGGER_EQP           1008 /* int int* */
  2262   2273   #define SQLITE_DBCONFIG_RESET_DATABASE        1009 /* int int* */
  2263   2274   #define SQLITE_DBCONFIG_DEFENSIVE             1010 /* int int* */
  2264   2275   #define SQLITE_DBCONFIG_WRITABLE_SCHEMA       1011 /* int int* */
  2265   2276   #define SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    1012 /* int int* */
  2266   2277   #define SQLITE_DBCONFIG_DQS_DML               1013 /* int int* */
  2267   2278   #define SQLITE_DBCONFIG_DQS_DDL               1014 /* int int* */
  2268         -#define SQLITE_DBCONFIG_MAX                   1014 /* Largest DBCONFIG */
         2279  +#define SQLITE_DBCONFIG_ENABLE_VIEW           1015 /* int int* */
         2280  +#define SQLITE_DBCONFIG_MAX                   1015 /* Largest DBCONFIG */
  2269   2281   
  2270   2282   /*
  2271   2283   ** CAPI3REF: Enable Or Disable Extended Result Codes
  2272   2284   ** METHOD: sqlite3
  2273   2285   **
  2274   2286   ** ^The sqlite3_extended_result_codes() routine enables or disables the
  2275   2287   ** [extended result codes] feature of SQLite. ^The extended result
................................................................................
  3810   3822   ** WHERE clause might influence the choice of query plan for a statement,
  3811   3823   ** then the statement will be automatically recompiled, as if there had been 
  3812   3824   ** a schema change, on the first  [sqlite3_step()] call following any change
  3813   3825   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  3814   3826   ** ^The specific value of WHERE-clause [parameter] might influence the 
  3815   3827   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  3816   3828   ** or [GLOB] operator or if the parameter is compared to an indexed column
  3817         -** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
         3829  +** and the [SQLITE_ENABLE_STAT4] compile-time option is enabled.
  3818   3830   ** </li>
  3819   3831   ** </ol>
  3820   3832   **
  3821   3833   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  3822   3834   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  3823   3835   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  3824   3836   ** sqlite3_prepare_v2() interface works exactly the same as
................................................................................
  4844   4856   ** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
  4845   4857   ** to signal that the function will always return the same result given
  4846   4858   ** the same inputs within a single SQL statement.  Most SQL functions are
  4847   4859   ** deterministic.  The built-in [random()] SQL function is an example of a
  4848   4860   ** function that is not deterministic.  The SQLite query planner is able to
  4849   4861   ** perform additional optimizations on deterministic functions, so use
  4850   4862   ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
         4863  +** ^The fourth parameter may also optionally include the [SQLITE_DIRECTONLY]
         4864  +** flag, which if present prevents the function from being invoked from
         4865  +** within VIEWs or TRIGGERs.
  4851   4866   **
  4852   4867   ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
  4853   4868   ** function can gain access to this pointer using [sqlite3_user_data()].)^
  4854   4869   **
  4855   4870   ** ^The sixth, seventh and eighth parameters passed to the three
  4856   4871   ** "sqlite3_create_function*" functions, xFunc, xStep and xFinal, are
  4857   4872   ** pointers to C-language functions that implement the SQL function or
................................................................................
  4961   4976   /*
  4962   4977   ** CAPI3REF: Function Flags
  4963   4978   **
  4964   4979   ** These constants may be ORed together with the 
  4965   4980   ** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
  4966   4981   ** to [sqlite3_create_function()], [sqlite3_create_function16()], or
  4967   4982   ** [sqlite3_create_function_v2()].
         4983  +**
         4984  +** The SQLITE_DETERMINISTIC flag means that the new function will always
         4985  +** maps the same inputs into the same output.  The abs() function is
         4986  +** deterministic, for example, but randomblob() is not.
         4987  +**
         4988  +** The SQLITE_DIRECTONLY flag means that the function may only be invoked
         4989  +** from top-level SQL, and cannot be used in VIEWs or TRIGGERs.
         4990  +**
         4991  +** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
         4992  +** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
         4993  +** Specifying this flag makes no difference for scalar or aggregate user
         4994  +** functions. However, if it is not specified for a user-defined window
         4995  +** function, then any sub-types belonging to arguments passed to the window
         4996  +** function may be discarded before the window function is called (i.e.
         4997  +** sqlite3_value_subtype() will always return 0).
  4968   4998   */
  4969         -#define SQLITE_DETERMINISTIC    0x800
         4999  +#define SQLITE_DETERMINISTIC    0x000000800
         5000  +#define SQLITE_DIRECTONLY       0x000080000
         5001  +#define SQLITE_SUBTYPE          0x000100000
  4970   5002   
  4971   5003   /*
  4972   5004   ** CAPI3REF: Deprecated Functions
  4973   5005   ** DEPRECATED
  4974   5006   **
  4975   5007   ** These functions are [deprecated].  In order to maintain
  4976   5008   ** backwards compatibility with older code, these functions continue 
................................................................................
  6608   6640   ** is a pointer to a destructor for the pClientData.  ^SQLite will
  6609   6641   ** invoke the destructor function (if it is not NULL) when SQLite
  6610   6642   ** no longer needs the pClientData pointer.  ^The destructor will also
  6611   6643   ** be invoked if the call to sqlite3_create_module_v2() fails.
  6612   6644   ** ^The sqlite3_create_module()
  6613   6645   ** interface is equivalent to sqlite3_create_module_v2() with a NULL
  6614   6646   ** destructor.
         6647  +**
         6648  +** ^If the third parameter (the pointer to the sqlite3_module object) is
         6649  +** NULL then no new module is create and any existing modules with the
         6650  +** same name are dropped.
         6651  +**
         6652  +** See also: [sqlite3_drop_modules()]
  6615   6653   */
  6616   6654   int sqlite3_create_module(
  6617   6655     sqlite3 *db,               /* SQLite connection to register module with */
  6618   6656     const char *zName,         /* Name of the module */
  6619   6657     const sqlite3_module *p,   /* Methods for the module */
  6620   6658     void *pClientData          /* Client data for xCreate/xConnect */
  6621   6659   );
................................................................................
  6622   6660   int sqlite3_create_module_v2(
  6623   6661     sqlite3 *db,               /* SQLite connection to register module with */
  6624   6662     const char *zName,         /* Name of the module */
  6625   6663     const sqlite3_module *p,   /* Methods for the module */
  6626   6664     void *pClientData,         /* Client data for xCreate/xConnect */
  6627   6665     void(*xDestroy)(void*)     /* Module destructor function */
  6628   6666   );
         6667  +
         6668  +/*
         6669  +** CAPI3REF: Remove Unnecessary Virtual Table Implementations
         6670  +** METHOD: sqlite3
         6671  +**
         6672  +** ^The sqlite3_drop_modules(D,L) interface removes all virtual
         6673  +** table modules from database connection D except those named on list L.
         6674  +** The L parameter must be either NULL or a pointer to an array of pointers
         6675  +** to strings where the array is terminated by a single NULL pointer.
         6676  +** ^If the L parameter is NULL, then all virtual table modules are removed.
         6677  +**
         6678  +** See also: [sqlite3_create_module()]
         6679  +*/
         6680  +int sqlite3_drop_modules(
         6681  +  sqlite3 *db,                /* Remove modules from this connection */
         6682  +  const char **azKeep         /* Except, do not remove the ones named here */
         6683  +);
  6629   6684   
  6630   6685   /*
  6631   6686   ** CAPI3REF: Virtual Table Instance Object
  6632   6687   ** KEYWORDS: sqlite3_vtab
  6633   6688   **
  6634   6689   ** Every [virtual table module] implementation uses a subclass
  6635   6690   ** of this object to describe a particular instance
................................................................................
  7331   7386   ** without notice.  These values are for testing purposes only.
  7332   7387   ** Applications should not use any of these parameters or the
  7333   7388   ** [sqlite3_test_control()] interface.
  7334   7389   */
  7335   7390   #define SQLITE_TESTCTRL_FIRST                    5
  7336   7391   #define SQLITE_TESTCTRL_PRNG_SAVE                5
  7337   7392   #define SQLITE_TESTCTRL_PRNG_RESTORE             6
  7338         -#define SQLITE_TESTCTRL_PRNG_RESET               7
         7393  +#define SQLITE_TESTCTRL_PRNG_RESET               7  /* NOT USED */
  7339   7394   #define SQLITE_TESTCTRL_BITVEC_TEST              8
  7340   7395   #define SQLITE_TESTCTRL_FAULT_INSTALL            9
  7341   7396   #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
  7342   7397   #define SQLITE_TESTCTRL_PENDING_BYTE            11
  7343   7398   #define SQLITE_TESTCTRL_ASSERT                  12
  7344   7399   #define SQLITE_TESTCTRL_ALWAYS                  13
  7345   7400   #define SQLITE_TESTCTRL_RESERVE                 14
................................................................................
  7354   7409   #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
  7355   7410   #define SQLITE_TESTCTRL_BYTEORDER               22
  7356   7411   #define SQLITE_TESTCTRL_ISINIT                  23
  7357   7412   #define SQLITE_TESTCTRL_SORTER_MMAP             24
  7358   7413   #define SQLITE_TESTCTRL_IMPOSTER                25
  7359   7414   #define SQLITE_TESTCTRL_PARSER_COVERAGE         26
  7360   7415   #define SQLITE_TESTCTRL_RESULT_INTREAL          27
  7361         -#define SQLITE_TESTCTRL_LAST                    27  /* Largest TESTCTRL */
         7416  +#define SQLITE_TESTCTRL_PRNG_SEED               28
         7417  +#define SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS     29
         7418  +#define SQLITE_TESTCTRL_LAST                    29  /* Largest TESTCTRL */
  7362   7419   
  7363   7420   /*
  7364   7421   ** CAPI3REF: SQL Keyword Checking
  7365   7422   **
  7366   7423   ** These routines provide access to the set of SQL language keywords 
  7367   7424   ** recognized by SQLite.  Applications can uses these routines to determine
  7368   7425   ** whether or not a specific identifier needs to be escaped (for example,

Changes to src/sqlite3ext.h.

   318    318                               void (*xInv)(sqlite3_context*,int,sqlite3_value**),
   319    319                               void(*xDestroy)(void*));
   320    320     /* Version 3.26.0 and later */
   321    321     const char *(*normalized_sql)(sqlite3_stmt*);
   322    322     /* Version 3.28.0 and later */
   323    323     int (*stmt_isexplain)(sqlite3_stmt*);
   324    324     int (*value_frombind)(sqlite3_value*);
          325  +  /* Version 3.30.0 and later */
          326  +  int (*drop_modules)(sqlite3*,const char**);
   325    327   };
   326    328   
   327    329   /*
   328    330   ** This is the function signature used for all extension entry points.  It
   329    331   ** is also defined in the file "loadext.c".
   330    332   */
   331    333   typedef int (*sqlite3_loadext_entry)(
................................................................................
   610    612   /* Version 3.25.0 and later */
   611    613   #define sqlite3_create_window_function sqlite3_api->create_window_function
   612    614   /* Version 3.26.0 and later */
   613    615   #define sqlite3_normalized_sql         sqlite3_api->normalized_sql
   614    616   /* Version 3.28.0 and later */
   615    617   #define sqlite3_stmt_isexplain         sqlite3_api->isexplain
   616    618   #define sqlite3_value_frombind         sqlite3_api->frombind
          619  +/* Version 3.30.0 and later */
          620  +#define sqlite3_drop_modules           sqlite3_api->drop_modules
   617    621   #endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
   618    622   
   619    623   #if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
   620    624     /* This case when the file really is being compiled as a loadable 
   621    625     ** extension */
   622    626   # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
   623    627   # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;

Changes to src/sqliteInt.h.

   208    208   ** that vary from one machine to the next.
   209    209   **
   210    210   ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
   211    211   ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
   212    212   ** So we have to define the macros in different ways depending on the
   213    213   ** compiler.
   214    214   */
   215         -#if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
          215  +#if defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
          216  +# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
          217  +# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
          218  +#elif defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
   216    219   # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
   217    220   # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
   218    221   #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
   219    222   # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
   220    223   # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
   221         -#elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
   222         -# define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
   223         -# define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
   224    224   #else                          /* Generates a warning - but it always works */
   225    225   # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
   226    226   # define SQLITE_PTR_TO_INT(X)  ((int)(X))
   227    227   #endif
   228    228   
   229    229   /*
   230    230   ** A macro to hint to the compiler that a function should not be
................................................................................
   931    931   # define SQLITE_DEFAULT_MMAP_SIZE 0
   932    932   #endif
   933    933   #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
   934    934   # undef SQLITE_DEFAULT_MMAP_SIZE
   935    935   # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
   936    936   #endif
   937    937   
   938         -/*
   939         -** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
   940         -** Priority is given to SQLITE_ENABLE_STAT4.  If either are defined, also
   941         -** define SQLITE_ENABLE_STAT3_OR_STAT4
   942         -*/
   943         -#ifdef SQLITE_ENABLE_STAT4
   944         -# undef SQLITE_ENABLE_STAT3
   945         -# define SQLITE_ENABLE_STAT3_OR_STAT4 1
   946         -#elif SQLITE_ENABLE_STAT3
   947         -# define SQLITE_ENABLE_STAT3_OR_STAT4 1
   948         -#elif SQLITE_ENABLE_STAT3_OR_STAT4
   949         -# undef SQLITE_ENABLE_STAT3_OR_STAT4
   950         -#endif
   951         -
   952    938   /*
   953    939   ** SELECTTRACE_ENABLED will be either 1 or 0 depending on whether or not
   954    940   ** the Select query generator tracing logic is turned on.
   955    941   */
   956    942   #if defined(SQLITE_ENABLE_SELECTTRACE)
   957    943   # define SELECTTRACE_ENABLED 1
   958    944   #else
................................................................................
  1419   1405     struct sqlite3InitInfo {      /* Information used during initialization */
  1420   1406       int newTnum;                /* Rootpage of table being initialized */
  1421   1407       u8 iDb;                     /* Which db file is being initialized */
  1422   1408       u8 busy;                    /* TRUE if currently initializing */
  1423   1409       unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
  1424   1410       unsigned imposterTable : 1; /* Building an imposter table */
  1425   1411       unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
         1412  +    char **azInit;              /* "type", "name", and "tbl_name" columns */
  1426   1413     } init;
  1427   1414     int nVdbeActive;              /* Number of VDBEs currently running */
  1428   1415     int nVdbeRead;                /* Number of active VDBEs that read or write */
  1429   1416     int nVdbeWrite;               /* Number of active VDBEs that read and write */
  1430   1417     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
  1431   1418     int nVDestroy;                /* Number of active OP_VDestroy operations */
  1432   1419     int nExtension;               /* Number of loaded extensions */
................................................................................
  1557   1544   #define SQLITE_TriggerEQP     0x01000000  /* Show trigger EXPLAIN QUERY PLAN */
  1558   1545   #define SQLITE_ResetDatabase  0x02000000  /* Reset the database */
  1559   1546   #define SQLITE_LegacyAlter    0x04000000  /* Legacy ALTER TABLE behaviour */
  1560   1547   #define SQLITE_NoSchemaError  0x08000000  /* Do not report schema parse errors*/
  1561   1548   #define SQLITE_Defensive      0x10000000  /* Input SQL is likely hostile */
  1562   1549   #define SQLITE_DqsDDL         0x20000000  /* dbl-quoted strings allowed in DDL*/
  1563   1550   #define SQLITE_DqsDML         0x40000000  /* dbl-quoted strings allowed in DML*/
         1551  +#define SQLITE_EnableView     0x80000000  /* Enable the use of views */
  1564   1552   
  1565   1553   #define SQLITE_NoopUpdate     0x01000000  /* UPDATE operations are no-ops */
  1566   1554   /* Flags used only if debugging */
  1567   1555   #define HI(X)  ((u64)(X)<<32)
  1568   1556   #ifdef SQLITE_DEBUG
  1569         -#define SQLITE_SqlTrace       HI(0x0001)  /* Debug print SQL as it executes */
  1570         -#define SQLITE_VdbeListing    HI(0x0002)  /* Debug listings of VDBE progs */
  1571         -#define SQLITE_VdbeTrace      HI(0x0004)  /* True to trace VDBE execution */
  1572         -#define SQLITE_VdbeAddopTrace HI(0x0008)  /* Trace sqlite3VdbeAddOp() calls */
  1573         -#define SQLITE_VdbeEQP        HI(0x0010)  /* Debug EXPLAIN QUERY PLAN */
  1574         -#define SQLITE_ParserTrace    HI(0x0020)  /* PRAGMA parser_trace=ON */
         1557  +#define SQLITE_SqlTrace       HI(0x0100000) /* Debug print SQL as it executes */
         1558  +#define SQLITE_VdbeListing    HI(0x0200000) /* Debug listings of VDBE progs */
         1559  +#define SQLITE_VdbeTrace      HI(0x0400000) /* True to trace VDBE execution */
         1560  +#define SQLITE_VdbeAddopTrace HI(0x0800000) /* Trace sqlite3VdbeAddOp() calls */
         1561  +#define SQLITE_VdbeEQP        HI(0x1000000) /* Debug EXPLAIN QUERY PLAN */
         1562  +#define SQLITE_ParserTrace    HI(0x2000000) /* PRAGMA parser_trace=ON */
  1575   1563   #endif
  1576   1564   
  1577   1565   /*
  1578   1566   ** Allowed values for sqlite3.mDbFlags
  1579   1567   */
  1580   1568   #define DBFLAG_SchemaChange   0x0001  /* Uncommitted Hash table changes */
  1581   1569   #define DBFLAG_PreferBuiltin  0x0002  /* Preference to built-in funcs */
................................................................................
  1595   1583   #define SQLITE_DistinctOpt    0x0010   /* DISTINCT using indexes */
  1596   1584   #define SQLITE_CoverIdxScan   0x0020   /* Covering index scans */
  1597   1585   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
  1598   1586   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
  1599   1587   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
  1600   1588   #define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
  1601   1589   #define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
  1602         -#define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
  1603         -   /* TH3 expects the Stat34  ^^^^^^ value to be 0x0800.  Don't change it */
         1590  +#define SQLITE_Stat4          0x0800   /* Use STAT4 data */
         1591  +   /* TH3 expects the Stat4   ^^^^^^ value to be 0x0800.  Don't change it */
  1604   1592   #define SQLITE_PushDown       0x1000   /* The push-down optimization */
  1605   1593   #define SQLITE_SimplifyJoin   0x2000   /* Convert LEFT JOIN to JOIN */
  1606   1594   #define SQLITE_SkipScan       0x4000   /* Skip-scans */
  1607   1595   #define SQLITE_PropagateConst 0x8000   /* The constant propagation opt */
  1608   1596   #define SQLITE_AllOpts        0xffff   /* All optimizations */
  1609   1597   
  1610   1598   /*
................................................................................
  1684   1672   ** are assert() statements in the code to verify this.
  1685   1673   **
  1686   1674   ** Value constraints (enforced via assert()):
  1687   1675   **     SQLITE_FUNC_MINMAX    ==  NC_MinMaxAgg      == SF_MinMaxAgg
  1688   1676   **     SQLITE_FUNC_LENGTH    ==  OPFLAG_LENGTHARG
  1689   1677   **     SQLITE_FUNC_TYPEOF    ==  OPFLAG_TYPEOFARG
  1690   1678   **     SQLITE_FUNC_CONSTANT  ==  SQLITE_DETERMINISTIC from the API
         1679  +**     SQLITE_FUNC_DIRECT    ==  SQLITE_DIRECTONLY from the API
  1691   1680   **     SQLITE_FUNC_ENCMASK   depends on SQLITE_UTF* macros in the API
  1692   1681   */
  1693   1682   #define SQLITE_FUNC_ENCMASK  0x0003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
  1694   1683   #define SQLITE_FUNC_LIKE     0x0004 /* Candidate for the LIKE optimization */
  1695   1684   #define SQLITE_FUNC_CASE     0x0008 /* Case-sensitive LIKE-type function */
  1696   1685   #define SQLITE_FUNC_EPHEM    0x0010 /* Ephemeral.  Delete with VDBE */
  1697   1686   #define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
................................................................................
  1704   1693   #define SQLITE_FUNC_MINMAX   0x1000 /* True for min() and max() aggregates */
  1705   1694   #define SQLITE_FUNC_SLOCHNG  0x2000 /* "Slow Change". Value constant during a
  1706   1695                                       ** single query - might change over time */
  1707   1696   #define SQLITE_FUNC_AFFINITY 0x4000 /* Built-in affinity() function */
  1708   1697   #define SQLITE_FUNC_OFFSET   0x8000 /* Built-in sqlite_offset() function */
  1709   1698   #define SQLITE_FUNC_WINDOW   0x00010000 /* Built-in window-only function */
  1710   1699   #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
         1700  +#define SQLITE_FUNC_DIRECT   0x00080000 /* Not for use in TRIGGERs or VIEWs */
         1701  +#define SQLITE_FUNC_SUBTYPE  0x00100000 /* Result likely to have sub-type */
  1711   1702   
  1712   1703   /*
  1713   1704   ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
  1714   1705   ** used to create the initializers for the FuncDef structures.
  1715   1706   **
  1716   1707   **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
  1717   1708   **     Used to create a scalar function definition of a function zName
................................................................................
  1817   1808   ** Each SQLite module (virtual table definition) is defined by an
  1818   1809   ** instance of the following structure, stored in the sqlite3.aModule
  1819   1810   ** hash table.
  1820   1811   */
  1821   1812   struct Module {
  1822   1813     const sqlite3_module *pModule;       /* Callback pointers */
  1823   1814     const char *zName;                   /* Name passed to create_module() */
         1815  +  int nRefModule;                      /* Number of pointers to this object */
  1824   1816     void *pAux;                          /* pAux passed to create_module() */
  1825   1817     void (*xDestroy)(void *);            /* Module destructor function */
  1826   1818     Table *pEpoTab;                      /* Eponymous table for this module */
  1827   1819   };
  1828   1820   
  1829   1821   /*
  1830   1822   ** information about each column of an SQL table is held in an instance
................................................................................
  1882   1874   ** But rather than start with 0 or 1, we begin with 'A'.  That way,
  1883   1875   ** when multiple affinity types are concatenated into a string and
  1884   1876   ** used as the P4 operand, they will be more readable.
  1885   1877   **
  1886   1878   ** Note also that the numeric types are grouped together so that testing
  1887   1879   ** for a numeric type is a single comparison.  And the BLOB type is first.
  1888   1880   */
  1889         -#define SQLITE_AFF_BLOB     'A'
  1890         -#define SQLITE_AFF_TEXT     'B'
  1891         -#define SQLITE_AFF_NUMERIC  'C'
  1892         -#define SQLITE_AFF_INTEGER  'D'
  1893         -#define SQLITE_AFF_REAL     'E'
         1881  +#define SQLITE_AFF_NONE     0x40  /* '@' */
         1882  +#define SQLITE_AFF_BLOB     0x41  /* 'A' */
         1883  +#define SQLITE_AFF_TEXT     0x42  /* 'B' */
         1884  +#define SQLITE_AFF_NUMERIC  0x43  /* 'C' */
         1885  +#define SQLITE_AFF_INTEGER  0x44  /* 'D' */
         1886  +#define SQLITE_AFF_REAL     0x45  /* 'E' */
  1894   1887   
  1895   1888   #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
  1896   1889   
  1897   1890   /*
  1898   1891   ** The SQLITE_AFF_MASK values masks off the significant bits of an
  1899   1892   ** affinity value.
  1900   1893   */
................................................................................
  2154   2147   */
  2155   2148   struct KeyInfo {
  2156   2149     u32 nRef;           /* Number of references to this KeyInfo object */
  2157   2150     u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
  2158   2151     u16 nKeyField;      /* Number of key columns in the index */
  2159   2152     u16 nAllField;      /* Total columns, including key plus others */
  2160   2153     sqlite3 *db;        /* The database connection */
  2161         -  u8 *aSortOrder;     /* Sort order for each column. */
         2154  +  u8 *aSortFlags;     /* Sort order for each column. */
  2162   2155     CollSeq *aColl[1];  /* Collating sequence for each term of the key */
  2163   2156   };
         2157  +
         2158  +#define KEYINFO_ORDER_DESC    0x01
         2159  +#define KEYINFO_ORDER_BIGNULL 0x02
  2164   2160   
  2165   2161   /*
  2166   2162   ** This object holds a record which has been parsed out into individual
  2167   2163   ** fields, for the purposes of doing a comparison.
  2168   2164   **
  2169   2165   ** A record is an object that contains one or more fields of data.
  2170   2166   ** Records are used to store the content of a table row and to store
................................................................................
  2265   2261     unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
  2266   2262     unsigned isResized:1;    /* True if resizeIndexObject() has been called */
  2267   2263     unsigned isCovering:1;   /* True if this is a covering index */
  2268   2264     unsigned noSkipScan:1;   /* Do not try to use skip-scan if true */
  2269   2265     unsigned hasStat1:1;     /* aiRowLogEst values come from sqlite_stat1 */
  2270   2266     unsigned bNoQuery:1;     /* Do not use this index to optimize queries */
  2271   2267     unsigned bAscKeyBug:1;   /* True if the bba7b69f9849b5bf bug applies */
  2272         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
         2268  +#ifdef SQLITE_ENABLE_STAT4
  2273   2269     int nSample;             /* Number of elements in aSample[] */
  2274   2270     int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
  2275   2271     tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
  2276   2272     IndexSample *aSample;    /* Samples of the left-most key */
  2277   2273     tRowcnt *aiRowEst;       /* Non-logarithmic stat1 data for this index */
  2278   2274     tRowcnt nRowEst0;        /* Non-logarithmic number of rows in the index */
  2279   2275   #endif
................................................................................
  2297   2293   /* The Index.aiColumn[] values are normally positive integer.  But
  2298   2294   ** there are some negative values that have special meaning:
  2299   2295   */
  2300   2296   #define XN_ROWID     (-1)     /* Indexed column is the rowid */
  2301   2297   #define XN_EXPR      (-2)     /* Indexed column is an expression */
  2302   2298   
  2303   2299   /*
  2304         -** Each sample stored in the sqlite_stat3 table is represented in memory
         2300  +** Each sample stored in the sqlite_stat4 table is represented in memory
  2305   2301   ** using a structure of this type.  See documentation at the top of the
  2306   2302   ** analyze.c source file for additional information.
  2307   2303   */
  2308   2304   struct IndexSample {
  2309   2305     void *p;          /* Pointer to sampled record */
  2310   2306     int n;            /* Size of record in bytes */
  2311   2307     tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
................................................................................
  2455   2451   ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
  2456   2452   ** are contained within the same memory allocation.  Note, however, that
  2457   2453   ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
  2458   2454   ** allocated, regardless of whether or not EP_Reduced is set.
  2459   2455   */
  2460   2456   struct Expr {
  2461   2457     u8 op;                 /* Operation performed by this node */
  2462         -  char affinity;         /* The affinity of the column or 0 if not a column */
         2458  +  char affExpr;          /* affinity, or RAISE type */
  2463   2459     u32 flags;             /* Various flags.  EP_* See below */
  2464   2460     union {
  2465   2461       char *zToken;          /* Token value. Zero terminated and dequoted */
  2466   2462       int iValue;            /* Non-negative integer value if EP_IntValue */
  2467   2463     } u;
  2468   2464   
  2469   2465     /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
................................................................................
  2486   2482   #if SQLITE_MAX_EXPR_DEPTH>0
  2487   2483     int nHeight;           /* Height of the tree headed by this node */
  2488   2484   #endif
  2489   2485     int iTable;            /* TK_COLUMN: cursor number of table holding column
  2490   2486                            ** TK_REGISTER: register number
  2491   2487                            ** TK_TRIGGER: 1 -> new, 0 -> old
  2492   2488                            ** EP_Unlikely:  134217728 times likelihood
         2489  +                         ** TK_SELECT_COLUMN: Number of columns on the LHS
  2493   2490                            ** TK_SELECT: 1st register of result vector */
  2494   2491     ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
  2495   2492                            ** TK_VARIABLE: variable number (always >= 1).
  2496   2493                            ** TK_SELECT_COLUMN: column of the result vector */
  2497   2494     i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
  2498   2495     i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
  2499   2496     u8 op2;                /* TK_REGISTER/TK_TRUTH: original value of Expr.op
  2500   2497                            ** TK_COLUMN: the value of p5 for OP_Column
  2501   2498                            ** TK_AGG_FUNCTION: nesting depth */
  2502   2499     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  2503   2500     union {
  2504   2501       Table *pTab;           /* TK_COLUMN: Table containing column. Can be NULL
  2505   2502                              ** for a column of an index on an expression */
  2506         -    Window *pWin;          /* TK_FUNCTION: Window definition for the func */
         2503  +    Window *pWin;          /* EP_WinFunc: Window/Filter defn for a function */
  2507   2504       struct {               /* TK_IN, TK_SELECT, and TK_EXISTS */
  2508   2505         int iAddr;             /* Subroutine entry address */
  2509   2506         int regReturn;         /* Register used to hold return address */
  2510   2507       } sub;
  2511   2508     } y;
  2512   2509   };
  2513   2510   
................................................................................
  2514   2511   /*
  2515   2512   ** The following are the meanings of bits in the Expr.flags field.
  2516   2513   ** Value restrictions:
  2517   2514   **
  2518   2515   **          EP_Agg == NC_HasAgg == SF_HasAgg
  2519   2516   **          EP_Win == NC_HasWin
  2520   2517   */
  2521         -#define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
  2522         -#define EP_Distinct  0x000002 /* Aggregate function with DISTINCT keyword */
  2523         -#define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
  2524         -#define EP_FixedCol  0x000008 /* TK_Column with a known fixed value */
  2525         -#define EP_Agg       0x000010 /* Contains one or more aggregate functions */
  2526         -#define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  2527         -#define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  2528         -#define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
  2529         -#define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
  2530         -#define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
  2531         -#define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
  2532         -#define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
  2533         -#define EP_Skip      0x001000 /* Operator does not contribute to affinity */
  2534         -#define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
  2535         -#define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
  2536         -#define EP_Win       0x008000 /* Contains window functions */
  2537         -#define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
  2538         -#define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
  2539         -#define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
  2540         -#define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
  2541         -#define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
  2542         -#define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
  2543         -#define EP_Alias     0x400000 /* Is an alias for a result set column */
  2544         -#define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
  2545         -#define EP_WinFunc  0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
  2546         -#define EP_Subrtn   0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
  2547         -#define EP_Quoted   0x4000000 /* TK_ID was originally quoted */
  2548         -#define EP_Static   0x8000000 /* Held in memory not obtained from malloc() */
  2549         -#define EP_IsTrue  0x10000000 /* Always has boolean value of TRUE */
  2550         -#define EP_IsFalse 0x20000000 /* Always has boolean value of FALSE */
         2518  +#define EP_FromJoin   0x000001 /* Originates in ON/USING clause of outer join */
         2519  +#define EP_Distinct   0x000002 /* Aggregate function with DISTINCT keyword */
         2520  +#define EP_HasFunc    0x000004 /* Contains one or more functions of any kind */
         2521  +#define EP_FixedCol   0x000008 /* TK_Column with a known fixed value */
         2522  +#define EP_Agg        0x000010 /* Contains one or more aggregate functions */
         2523  +#define EP_VarSelect  0x000020 /* pSelect is correlated, not constant */
         2524  +#define EP_DblQuoted  0x000040 /* token.z was originally in "..." */
         2525  +#define EP_InfixFunc  0x000080 /* True for an infix function: LIKE, GLOB, etc */
         2526  +#define EP_Collate    0x000100 /* Tree contains a TK_COLLATE operator */
         2527  +  /*                  0x000200 Available for reuse */
         2528  +#define EP_IntValue   0x000400 /* Integer value contained in u.iValue */
         2529  +#define EP_xIsSelect  0x000800 /* x.pSelect is valid (otherwise x.pList is) */
         2530  +#define EP_Skip       0x001000 /* Operator does not contribute to affinity */
         2531  +#define EP_Reduced    0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
         2532  +#define EP_TokenOnly  0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
         2533  +#define EP_Win        0x008000 /* Contains window functions */
         2534  +#define EP_MemToken   0x010000 /* Need to sqlite3DbFree() Expr.zToken */
         2535  +#define EP_NoReduce   0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
         2536  +#define EP_Unlikely   0x040000 /* unlikely() or likelihood() function */
         2537  +#define EP_ConstFunc  0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
         2538  +#define EP_CanBeNull  0x100000 /* Can be null despite NOT NULL constraint */
         2539  +#define EP_Subquery   0x200000 /* Tree contains a TK_SELECT operator */
         2540  +#define EP_Alias      0x400000 /* Is an alias for a result set column */
         2541  +#define EP_Leaf       0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
         2542  +#define EP_WinFunc   0x1000000 /* TK_FUNCTION with Expr.y.pWin set */
         2543  +#define EP_Subrtn    0x2000000 /* Uses Expr.y.sub. TK_IN, _SELECT, or _EXISTS */
         2544  +#define EP_Quoted    0x4000000 /* TK_ID was originally quoted */
         2545  +#define EP_Static    0x8000000 /* Held in memory not obtained from malloc() */
         2546  +#define EP_IsTrue   0x10000000 /* Always has boolean value of TRUE */
         2547  +#define EP_IsFalse  0x20000000 /* Always has boolean value of FALSE */
         2548  +#define EP_Indirect 0x40000000 /* Contained within a TRIGGER or a VIEW */
  2551   2549   
  2552   2550   /*
  2553   2551   ** The EP_Propagate mask is a set of properties that automatically propagate
  2554   2552   ** upwards into parent nodes.
  2555   2553   */
  2556   2554   #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
  2557   2555   
................................................................................
  2586   2584   #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
  2587   2585   
  2588   2586   /*
  2589   2587   ** Flags passed to the sqlite3ExprDup() function. See the header comment
  2590   2588   ** above sqlite3ExprDup() for details.
  2591   2589   */
  2592   2590   #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
         2591  +
         2592  +/*
         2593  +** True if the expression passed as an argument was a function with
         2594  +** an OVER() clause (a window function).
         2595  +*/
         2596  +#define IsWindowFunc(p) ( \
         2597  +    ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
         2598  +)
  2593   2599   
  2594   2600   /*
  2595   2601   ** A list of expressions.  Each expression may optionally have a
  2596   2602   ** name.  An expr/name combination can be used in several ways, such
  2597   2603   ** as the list of "expr AS ID" fields following a "SELECT" or in the
  2598   2604   ** list of "ID = expr" items in an UPDATE.  A list of expressions can
  2599   2605   ** also be used as the argument to a function, in which case the a.zName
................................................................................
  2609   2615   */
  2610   2616   struct ExprList {
  2611   2617     int nExpr;             /* Number of expressions on the list */
  2612   2618     struct ExprList_item { /* For each expression in the list */
  2613   2619       Expr *pExpr;            /* The parse tree for this expression */
  2614   2620       char *zName;            /* Token associated with this expression */
  2615   2621       char *zSpan;            /* Original text of the expression */
  2616         -    u8 sortOrder;           /* 1 for DESC or 0 for ASC */
         2622  +    u8 sortFlags;           /* Mask of KEYINFO_ORDER_* flags */
  2617   2623       unsigned done :1;       /* A flag to indicate when processing is finished */
  2618   2624       unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
  2619   2625       unsigned reusable :1;   /* Constant expression is reusable */
  2620   2626       unsigned bSorterRef :1; /* Defer evaluation until after sorting */
         2627  +    unsigned bNulls: 1;     /* True if explicit "NULLS FIRST/LAST" */
  2621   2628       union {
  2622   2629         struct {
  2623   2630           u16 iOrderByCol;      /* For ORDER BY, column number in result set */
  2624   2631           u16 iAlias;           /* Index into Parse.aAlias[] for zName */
  2625   2632         } x;
  2626   2633         int iConstExprReg;      /* Register in which Expr value is cached */
  2627   2634       } u;
................................................................................
  2904   2911   #define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
  2905   2912   #define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
  2906   2913   #define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
  2907   2914   #define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
  2908   2915   #define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
  2909   2916   #define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
  2910   2917   #define SF_ComplexResult  0x40000  /* Result contains subquery or function */
         2918  +#define SF_WhereBegin     0x80000  /* Really a WhereBegin() call.  Debug Only */
  2911   2919   
  2912   2920   /*
  2913   2921   ** The results of a SELECT can be distributed in several ways, as defined
  2914   2922   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2915   2923   ** Type".
  2916   2924   **
  2917   2925   **     SRT_Union       Store results as a key in a temporary index
................................................................................
  3408   3416   /*
  3409   3417   ** Structure containing global configuration data for the SQLite library.
  3410   3418   **
  3411   3419   ** This structure also contains some state information.
  3412   3420   */
  3413   3421   struct Sqlite3Config {
  3414   3422     int bMemstat;                     /* True to enable memory status */
  3415         -  int bCoreMutex;                   /* True to enable core mutexing */
  3416         -  int bFullMutex;                   /* True to enable full mutexing */
  3417         -  int bOpenUri;                     /* True to interpret filenames as URIs */
  3418         -  int bUseCis;                      /* Use covering indices for full-scans */
  3419         -  int bSmallMalloc;                 /* Avoid large memory allocations if true */
         3423  +  u8 bCoreMutex;                    /* True to enable core mutexing */
         3424  +  u8 bFullMutex;                    /* True to enable full mutexing */
         3425  +  u8 bOpenUri;                      /* True to interpret filenames as URIs */
         3426  +  u8 bUseCis;                       /* Use covering indices for full-scans */
         3427  +  u8 bSmallMalloc;                  /* Avoid large memory allocations if true */
         3428  +  u8 bExtraSchemaChecks;            /* Verify type,name,tbl_name in schema */
  3420   3429     int mxStrlen;                     /* Maximum string length */
  3421   3430     int neverCorrupt;                 /* Database is always well-formed */
  3422   3431     int szLookaside;                  /* Default lookaside buffer size */
  3423   3432     int nLookaside;                   /* Default lookaside buffer count */
  3424   3433     int nStmtSpill;                   /* Stmt-journal spill-to-disk threshold */
  3425   3434     sqlite3_mem_methods m;            /* Low-level memory allocation interface */
  3426   3435     sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
................................................................................
  3464   3473   #ifndef SQLITE_UNTESTABLE
  3465   3474     int (*xTestCallback)(int);        /* Invoked by sqlite3FaultSim() */
  3466   3475   #endif
  3467   3476     int bLocaltimeFault;              /* True to fail localtime() calls */
  3468   3477     int bInternalFunctions;           /* Internal SQL functions are visible */
  3469   3478     int iOnceResetThreshold;          /* When to reset OP_Once counters */
  3470   3479     u32 szSorterRef;                  /* Min size in bytes to use sorter-refs */
         3480  +  unsigned int iPrngSeed;           /* Alternative fixed seed for the PRNG */
  3471   3481   };
  3472   3482   
  3473   3483   /*
  3474   3484   ** This macro is used inside of assert() statements to indicate that
  3475   3485   ** the assert is only valid on a well-formed database.  Instead of:
  3476   3486   **
  3477   3487   **     assert( X );
................................................................................
  3560   3570   struct TreeView {
  3561   3571     int iLevel;             /* Which level of the tree we are on */
  3562   3572     u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
  3563   3573   };
  3564   3574   #endif /* SQLITE_DEBUG */
  3565   3575   
  3566   3576   /*
  3567         -** This object is used in various ways, all related to window functions
         3577  +** This object is used in various ways, most (but not all) related to window
         3578  +** functions.
  3568   3579   **
  3569   3580   **   (1) A single instance of this structure is attached to the
  3570         -**       the Expr.pWin field for each window function in an expression tree.
         3581  +**       the Expr.y.pWin field for each window function in an expression tree.
  3571   3582   **       This object holds the information contained in the OVER clause,
  3572   3583   **       plus additional fields used during code generation.
  3573   3584   **
  3574   3585   **   (2) All window functions in a single SELECT form a linked-list
  3575   3586   **       attached to Select.pWin.  The Window.pFunc and Window.pExpr
  3576   3587   **       fields point back to the expression that is the window function.
  3577   3588   **
  3578   3589   **   (3) The terms of the WINDOW clause of a SELECT are instances of this
  3579   3590   **       object on a linked list attached to Select.pWinDefn.
  3580   3591   **
         3592  +**   (4) For an aggregate function with a FILTER clause, an instance
         3593  +**       of this object is stored in Expr.y.pWin with eFrmType set to
         3594  +**       TK_FILTER. In this case the only field used is Window.pFilter.
         3595  +**
  3581   3596   ** The uses (1) and (2) are really the same Window object that just happens
  3582   3597   ** to be accessible in two different ways.  Use case (3) are separate objects.
  3583   3598   */
  3584   3599   struct Window {
  3585   3600     char *zName;            /* Name of window (may be NULL) */
  3586   3601     char *zBase;            /* Name of base window for chaining (may be NULL) */
  3587   3602     ExprList *pPartition;   /* PARTITION BY clause */
................................................................................
  3589   3604     u8 eFrmType;            /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
  3590   3605     u8 eStart;              /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
  3591   3606     u8 eEnd;                /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
  3592   3607     u8 bImplicitFrame;      /* True if frame was implicitly specified */
  3593   3608     u8 eExclude;            /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
  3594   3609     Expr *pStart;           /* Expression for "<expr> PRECEDING" */
  3595   3610     Expr *pEnd;             /* Expression for "<expr> FOLLOWING" */
         3611  +  Window **ppThis;        /* Pointer to this object in Select.pWin list */
  3596   3612     Window *pNextWin;       /* Next window function belonging to this SELECT */
  3597   3613     Expr *pFilter;          /* The FILTER expression */
  3598   3614     FuncDef *pFunc;         /* The function */
  3599   3615     int iEphCsr;            /* Partition buffer or Peer buffer */
  3600         -  int regAccum;
  3601         -  int regResult;
         3616  +  int regAccum;           /* Accumulator */
         3617  +  int regResult;          /* Interim result */
  3602   3618     int csrApp;             /* Function cursor (used by min/max) */
  3603   3619     int regApp;             /* Function register (also used by min/max) */
  3604   3620     int regPart;            /* Array of registers for PARTITION BY values */
  3605   3621     Expr *pOwner;           /* Expression object this window is attached to */
  3606   3622     int nBufferCol;         /* Number of columns in buffer table */
  3607   3623     int iArgCol;            /* Offset of first argument for this function */
  3608   3624     int regOne;             /* Register containing constant value 1 */
  3609   3625     int regStartRowid;
  3610   3626     int regEndRowid;
         3627  +  u8 bExprArgs;           /* Defer evaluation of window function arguments
         3628  +                          ** due to the SQLITE_SUBTYPE flag */
  3611   3629   };
  3612   3630   
  3613   3631   #ifndef SQLITE_OMIT_WINDOWFUNC
  3614   3632   void sqlite3WindowDelete(sqlite3*, Window*);
         3633  +void sqlite3WindowUnlinkFromSelect(Window*);
  3615   3634   void sqlite3WindowListDelete(sqlite3 *db, Window *p);
  3616   3635   Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
  3617   3636   void sqlite3WindowAttach(Parse*, Expr*, Window*);
  3618         -int sqlite3WindowCompare(Parse*, Window*, Window*);
         3637  +void sqlite3WindowLink(Select *pSel, Window *pWin);
         3638  +int sqlite3WindowCompare(Parse*, Window*, Window*, int);
  3619   3639   void sqlite3WindowCodeInit(Parse*, Window*);
  3620   3640   void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
  3621   3641   int sqlite3WindowRewrite(Parse*, Select*);
  3622   3642   int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
  3623   3643   void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
  3624   3644   Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
  3625   3645   Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
................................................................................
  3883   3903   Expr *sqlite3ExprSimplifiedAndOr(Expr*);
  3884   3904   Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
  3885   3905   void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
  3886   3906   void sqlite3ExprDelete(sqlite3*, Expr*);
  3887   3907   void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
  3888   3908   ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
  3889   3909   ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
  3890         -void sqlite3ExprListSetSortOrder(ExprList*,int);
         3910  +void sqlite3ExprListSetSortOrder(ExprList*,int,int);
  3891   3911   void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
  3892   3912   void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
  3893   3913   void sqlite3ExprListDelete(sqlite3*, ExprList*);
  3894   3914   u32 sqlite3ExprListFlags(const ExprList*);
  3895   3915   int sqlite3IndexHasDuplicateRootPage(Index*);
  3896   3916   int sqlite3Init(sqlite3*, char**);
  3897   3917   int sqlite3InitCallback(void*, int, char**, char**);
................................................................................
  3902   3922   #endif
  3903   3923   void sqlite3ResetAllSchemasOfConnection(sqlite3*);
  3904   3924   void sqlite3ResetOneSchema(sqlite3*,int);
  3905   3925   void sqlite3CollapseDatabaseArray(sqlite3*);
  3906   3926   void sqlite3CommitInternalChanges(sqlite3*);
  3907   3927   void sqlite3DeleteColumnNames(sqlite3*,Table*);
  3908   3928   int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
  3909         -void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
  3910         -Table *sqlite3ResultSetOfSelect(Parse*,Select*);
         3929  +void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*,char);
         3930  +Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
  3911   3931   void sqlite3OpenMasterTable(Parse *, int);
  3912   3932   Index *sqlite3PrimaryKeyIndex(Table*);
  3913   3933   i16 sqlite3ColumnOfIndex(Index*, i16);
  3914   3934   void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
  3915   3935   #if SQLITE_ENABLE_HIDDEN_COLUMNS
  3916   3936     void sqlite3ColumnPropertiesFromName(Table*, Column*);
  3917   3937   #else
................................................................................
  4206   4226   u32 sqlite3Utf8Read(const u8**);
  4207   4227   LogEst sqlite3LogEst(u64);
  4208   4228   LogEst sqlite3LogEstAdd(LogEst,LogEst);
  4209   4229   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4210   4230   LogEst sqlite3LogEstFromDouble(double);
  4211   4231   #endif
  4212   4232   #if defined(SQLITE_ENABLE_STMT_SCANSTATUS) || \
  4213         -    defined(SQLITE_ENABLE_STAT3_OR_STAT4) || \
         4233  +    defined(SQLITE_ENABLE_STAT4) || \
  4214   4234       defined(SQLITE_EXPLAIN_ESTIMATED_ROWS)
  4215   4235   u64 sqlite3LogEstToInt(LogEst);
  4216   4236   #endif
  4217   4237   VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
  4218   4238   const char *sqlite3VListNumToName(VList*,int);
  4219   4239   int sqlite3VListNameToNum(VList*,const char*,int);
  4220   4240   
................................................................................
  4272   4292   CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
  4273   4293   CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
  4274   4294   CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
  4275   4295   int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
  4276   4296   Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
  4277   4297   Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
  4278   4298   Expr *sqlite3ExprSkipCollate(Expr*);
         4299  +Expr *sqlite3ExprSkipCollateAndLikely(Expr*);
  4279   4300   int sqlite3CheckCollSeq(Parse *, CollSeq *);
  4280   4301   int sqlite3WritableSchema(sqlite3*);
  4281         -int sqlite3CheckObjectName(Parse *, const char *);
         4302  +int sqlite3CheckObjectName(Parse*, const char*,const char*,const char*);
  4282   4303   void sqlite3VdbeSetChanges(sqlite3 *, int);
  4283   4304   int sqlite3AddInt64(i64*,i64);
  4284   4305   int sqlite3SubInt64(i64*,i64);
  4285   4306   int sqlite3MulInt64(i64*,i64);
  4286   4307   int sqlite3AbsInt32(int);
  4287   4308   #ifdef SQLITE_ENABLE_8_3_NAMES
  4288   4309   void sqlite3FileSuffix3(const char*, char*);
................................................................................
  4361   4382   Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
  4362   4383   int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
  4363   4384   KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
  4364   4385   void sqlite3KeyInfoUnref(KeyInfo*);
  4365   4386   KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
  4366   4387   KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
  4367   4388   KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
         4389  +int sqlite3HasExplicitNulls(Parse*, ExprList*);
  4368   4390   
  4369   4391   #ifdef SQLITE_DEBUG
  4370   4392   int sqlite3KeyInfoIsWriteable(KeyInfo*);
  4371   4393   #endif
  4372   4394   int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
  4373   4395     void (*)(sqlite3_context*,int,sqlite3_value **),
  4374   4396     void (*)(sqlite3_context*,int,sqlite3_value **), 
................................................................................
  4393   4415   
  4394   4416   #ifndef SQLITE_OMIT_SUBQUERY
  4395   4417   int sqlite3ExprCheckIN(Parse*, Expr*);
  4396   4418   #else
  4397   4419   # define sqlite3ExprCheckIN(x,y) SQLITE_OK
  4398   4420   #endif
  4399   4421   
  4400         -#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  4401         -void sqlite3AnalyzeFunctions(void);
         4422  +#ifdef SQLITE_ENABLE_STAT4
  4402   4423   int sqlite3Stat4ProbeSetValue(
  4403   4424       Parse*,Index*,UnpackedRecord**,Expr*,int,int,int*);
  4404   4425   int sqlite3Stat4ValueFromExpr(Parse*, Expr*, u8, sqlite3_value**);
  4405   4426   void sqlite3Stat4ProbeFree(UnpackedRecord*);
  4406   4427   int sqlite3Stat4Column(sqlite3*, const void*, int, int, sqlite3_value**);
  4407   4428   char sqlite3IndexColumnAffinity(sqlite3*, Index*, int);
  4408   4429   #endif
................................................................................
  4441   4462   #  define sqlite3VtabClear(Y)
  4442   4463   #  define sqlite3VtabSync(X,Y) SQLITE_OK
  4443   4464   #  define sqlite3VtabRollback(X)
  4444   4465   #  define sqlite3VtabCommit(X)
  4445   4466   #  define sqlite3VtabInSync(db) 0
  4446   4467   #  define sqlite3VtabLock(X)
  4447   4468   #  define sqlite3VtabUnlock(X)
         4469  +#  define sqlite3VtabModuleUnref(D,X)
  4448   4470   #  define sqlite3VtabUnlockList(X)
  4449   4471   #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
  4450   4472   #  define sqlite3GetVTable(X,Y)  ((VTable*)0)
  4451   4473   #else
  4452   4474      void sqlite3VtabClear(sqlite3 *db, Table*);
  4453   4475      void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
  4454   4476      int sqlite3VtabSync(sqlite3 *db, Vdbe*);
  4455   4477      int sqlite3VtabRollback(sqlite3 *db);
  4456   4478      int sqlite3VtabCommit(sqlite3 *db);
  4457   4479      void sqlite3VtabLock(VTable *);
  4458   4480      void sqlite3VtabUnlock(VTable *);
         4481  +   void sqlite3VtabModuleUnref(sqlite3*,Module*);
  4459   4482      void sqlite3VtabUnlockList(sqlite3*);
  4460   4483      int sqlite3VtabSavepoint(sqlite3 *, int, int);
  4461   4484      void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
  4462   4485      VTable *sqlite3GetVTable(sqlite3*, Table*);
  4463   4486      Module *sqlite3VtabCreateModule(
  4464   4487        sqlite3*,
  4465   4488        const char*,

Changes to src/tclsqlite.c.

  1913   1913     SqliteDb *pDb = (SqliteDb*)cd;
  1914   1914     int choice;
  1915   1915     int rc = TCL_OK;
  1916   1916     static const char *DB_strs[] = {
  1917   1917       "authorizer",             "backup",                "bind_fallback",
  1918   1918       "busy",                   "cache",                 "changes",
  1919   1919       "close",                  "collate",               "collation_needed",
  1920         -    "commit_hook",            "complete",              "copy",
  1921         -    "deserialize",            "enable_load_extension", "errorcode",
  1922         -    "eval",                   "exists",                "function",
  1923         -    "incrblob",               "interrupt",             "last_insert_rowid",
  1924         -    "nullvalue",              "onecolumn",             "preupdate",
  1925         -    "profile",                "progress",              "rekey",
  1926         -    "restore",                "rollback_hook",         "serialize",
  1927         -    "status",                 "timeout",               "total_changes",
  1928         -    "trace",                  "trace_v2",              "transaction",
  1929         -    "unlock_notify",          "update_hook",           "version",
  1930         -    "wal_hook",               0                        
         1920  +    "commit_hook",            "complete",              "config",
         1921  +    "copy",                   "deserialize",           "enable_load_extension",
         1922  +    "errorcode",              "eval",                  "exists",
         1923  +    "function",               "incrblob",              "interrupt",
         1924  +    "last_insert_rowid",      "nullvalue",             "onecolumn",
         1925  +    "preupdate",              "profile",               "progress",
         1926  +    "rekey",                  "restore",               "rollback_hook",
         1927  +    "serialize",              "status",                "timeout",
         1928  +    "total_changes",          "trace",                 "trace_v2",
         1929  +    "transaction",            "unlock_notify",         "update_hook",
         1930  +    "version",                "wal_hook",              0
  1931   1931     };
  1932   1932     enum DB_enum {
  1933   1933       DB_AUTHORIZER,            DB_BACKUP,               DB_BIND_FALLBACK,
  1934   1934       DB_BUSY,                  DB_CACHE,                DB_CHANGES,
  1935   1935       DB_CLOSE,                 DB_COLLATE,              DB_COLLATION_NEEDED,
  1936         -    DB_COMMIT_HOOK,           DB_COMPLETE,             DB_COPY,
  1937         -    DB_DESERIALIZE,           DB_ENABLE_LOAD_EXTENSION,DB_ERRORCODE,
  1938         -    DB_EVAL,                  DB_EXISTS,               DB_FUNCTION,
  1939         -    DB_INCRBLOB,              DB_INTERRUPT,            DB_LAST_INSERT_ROWID,
  1940         -    DB_NULLVALUE,             DB_ONECOLUMN,            DB_PREUPDATE,
  1941         -    DB_PROFILE,               DB_PROGRESS,             DB_REKEY,
  1942         -    DB_RESTORE,               DB_ROLLBACK_HOOK,        DB_SERIALIZE,
  1943         -    DB_STATUS,                DB_TIMEOUT,              DB_TOTAL_CHANGES,
  1944         -    DB_TRACE,                 DB_TRACE_V2,             DB_TRANSACTION,
  1945         -    DB_UNLOCK_NOTIFY,         DB_UPDATE_HOOK,          DB_VERSION,
  1946         -    DB_WAL_HOOK             
         1936  +    DB_COMMIT_HOOK,           DB_COMPLETE,             DB_CONFIG,
         1937  +    DB_COPY,                  DB_DESERIALIZE,          DB_ENABLE_LOAD_EXTENSION,
         1938  +    DB_ERRORCODE,             DB_EVAL,                 DB_EXISTS,
         1939  +    DB_FUNCTION,              DB_INCRBLOB,             DB_INTERRUPT,
         1940  +    DB_LAST_INSERT_ROWID,     DB_NULLVALUE,            DB_ONECOLUMN,
         1941  +    DB_PREUPDATE,             DB_PROFILE,              DB_PROGRESS,
         1942  +    DB_REKEY,                 DB_RESTORE,              DB_ROLLBACK_HOOK,
         1943  +    DB_SERIALIZE,             DB_STATUS,               DB_TIMEOUT,
         1944  +    DB_TOTAL_CHANGES,         DB_TRACE,                DB_TRACE_V2,
         1945  +    DB_TRANSACTION,           DB_UNLOCK_NOTIFY,        DB_UPDATE_HOOK,
         1946  +    DB_VERSION,               DB_WAL_HOOK             
  1947   1947     };
  1948   1948     /* don't leave trailing commas on DB_enum, it confuses the AIX xlc compiler */
  1949   1949   
  1950   1950     if( objc<2 ){
  1951   1951       Tcl_WrongNumArgs(interp, 1, objv, "SUBCOMMAND ...");
  1952   1952       return TCL_ERROR;
  1953   1953     }
................................................................................
  2326   2326       }
  2327   2327       isComplete = sqlite3_complete( Tcl_GetStringFromObj(objv[2], 0) );
  2328   2328       pResult = Tcl_GetObjResult(interp);
  2329   2329       Tcl_SetBooleanObj(pResult, isComplete);
  2330   2330   #endif
  2331   2331       break;
  2332   2332     }
         2333  +
         2334  +  /*    $db config ?OPTION? ?BOOLEAN?
         2335  +  **
         2336  +  ** Configure the database connection using the sqlite3_db_config()
         2337  +  ** interface.
         2338  +  */
         2339  +  case DB_CONFIG: {
         2340  +    static const struct DbConfigChoices {
         2341  +      const char *zName;
         2342  +      int op;
         2343  +    } aDbConfig[] = {
         2344  +        { "enable_fkey",        SQLITE_DBCONFIG_ENABLE_FKEY           },
         2345  +        { "enable_trigger",     SQLITE_DBCONFIG_ENABLE_TRIGGER        },
         2346  +        { "enable_view",        SQLITE_DBCONFIG_ENABLE_VIEW           },
         2347  +        { "fts3_tokenizer",     SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
         2348  +        { "load_extension",     SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
         2349  +        { "no_ckpt_on_close",   SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE      },
         2350  +        { "enable_qpsg",        SQLITE_DBCONFIG_ENABLE_QPSG           },
         2351  +        { "trigger_eqp",        SQLITE_DBCONFIG_TRIGGER_EQP           },
         2352  +        { "reset_database",     SQLITE_DBCONFIG_RESET_DATABASE        },
         2353  +        { "defensive",          SQLITE_DBCONFIG_DEFENSIVE             },
         2354  +        { "writable_schema",    SQLITE_DBCONFIG_WRITABLE_SCHEMA       },
         2355  +        { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE    },
         2356  +        { "dqs_dml",            SQLITE_DBCONFIG_DQS_DML               },
         2357  +        { "dqs_ddl",            SQLITE_DBCONFIG_DQS_DDL               },
         2358  +    };
         2359  +    Tcl_Obj *pResult;
         2360  +    int ii;
         2361  +    if( objc>4 ){
         2362  +      Tcl_WrongNumArgs(interp, 2, objv, "?OPTION? ?BOOLEAN?");
         2363  +      return TCL_ERROR;
         2364  +    }
         2365  +    if( objc==2 ){
         2366  +      /* With no arguments, list all configuration options and with the
         2367  +      ** current value */
         2368  +      pResult = Tcl_NewListObj(0,0);
         2369  +      for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){
         2370  +        int v = 0;
         2371  +        sqlite3_db_config(pDb->db, aDbConfig[ii].op, -1, &v);
         2372  +        Tcl_ListObjAppendElement(interp, pResult,
         2373  +           Tcl_NewStringObj(aDbConfig[ii].zName,-1));
         2374  +        Tcl_ListObjAppendElement(interp, pResult,
         2375  +           Tcl_NewIntObj(v));
         2376  +      }
         2377  +    }else{
         2378  +      const char *zOpt = Tcl_GetString(objv[2]);
         2379  +      int onoff = -1;
         2380  +      int v = 0;
         2381  +      if( zOpt[0]=='-' ) zOpt++;
         2382  +      for(ii=0; ii<sizeof(aDbConfig)/sizeof(aDbConfig[0]); ii++){
         2383  +        if( strcmp(aDbConfig[ii].zName, zOpt)==0 ) break;
         2384  +      }
         2385  +      if( ii>=sizeof(aDbConfig)/sizeof(aDbConfig[0]) ){
         2386  +        Tcl_AppendResult(interp, "unknown config option: \"", zOpt,
         2387  +                                "\"", (void*)0);
         2388  +        return TCL_ERROR;
         2389  +      }
         2390  +      if( objc==4 ){
         2391  +        if( Tcl_GetBooleanFromObj(interp, objv[3], &onoff) ){
         2392  +          return TCL_ERROR;
         2393  +        }
         2394  +      }
         2395  +      sqlite3_db_config(pDb->db, aDbConfig[ii].op, onoff, &v);
         2396  +      pResult = Tcl_NewIntObj(v);
         2397  +    }
         2398  +    Tcl_SetObjResult(interp, pResult);
         2399  +    break;
         2400  +  }
  2333   2401   
  2334   2402     /*    $db copy conflict-algorithm table filename ?SEPARATOR? ?NULLINDICATOR?
  2335   2403     **
  2336   2404     ** Copy data into table from filename, optionally using SEPARATOR
  2337   2405     ** as column separators.  If a column contains a null string, or the
  2338   2406     ** value of NULLINDICATOR, a NULL is inserted for the column.
  2339   2407     ** conflict-algorithm is one of the sqlite conflict algorithms:
................................................................................
  2737   2805         cd2[1] = (void *)pScript;
  2738   2806         rc = DbEvalNextCmd(cd2, interp, TCL_OK);
  2739   2807       }
  2740   2808       break;
  2741   2809     }
  2742   2810   
  2743   2811     /*
  2744         -  **     $db function NAME [-argcount N] [-deterministic] SCRIPT
         2812  +  **     $db function NAME [OPTIONS] SCRIPT
  2745   2813     **
  2746   2814     ** Create a new SQL function called NAME.  Whenever that function is
  2747   2815     ** called, invoke SCRIPT to evaluate the function.
         2816  +  **
         2817  +  ** Options:
         2818  +  **         --argcount N           Function has exactly N arguments
         2819  +  **         --deterministic        The function is pure
         2820  +  **         --directonly           Prohibit use inside triggers and views
         2821  +  **         --returntype TYPE      Specify the return type of the function
  2748   2822     */
  2749   2823     case DB_FUNCTION: {
  2750   2824       int flags = SQLITE_UTF8;
  2751   2825       SqlFunc *pFunc;
  2752   2826       Tcl_Obj *pScript;
  2753   2827       char *zName;
  2754   2828       int nArg = -1;
................................................................................
  2772   2846                              (char*)0);
  2773   2847             return TCL_ERROR;
  2774   2848           }
  2775   2849           i++;
  2776   2850         }else
  2777   2851         if( n>1 && strncmp(z, "-deterministic",n)==0 ){
  2778   2852           flags |= SQLITE_DETERMINISTIC;
         2853  +      }else
         2854  +      if( n>1 && strncmp(z, "-directonly",n)==0 ){
         2855  +        flags |= SQLITE_DIRECTONLY;
  2779   2856         }else
  2780   2857         if( n>1 && strncmp(z, "-returntype", n)==0 ){
  2781   2858           const char *azType[] = {"integer", "real", "text", "blob", "any", 0};
  2782   2859           assert( SQLITE_INTEGER==1 && SQLITE_FLOAT==2 && SQLITE_TEXT==3 );
  2783   2860           assert( SQLITE_BLOB==4 && SQLITE_NULL==5 );
  2784   2861           if( i==(objc-2) ){
  2785   2862             Tcl_AppendResult(interp, "option requires an argument: ", z,(char*)0);
................................................................................
  2788   2865           i++;
  2789   2866           if( Tcl_GetIndexFromObj(interp, objv[i], azType, "type", 0, &eType) ){
  2790   2867             return TCL_ERROR;
  2791   2868           }
  2792   2869           eType++;
  2793   2870         }else{
  2794   2871           Tcl_AppendResult(interp, "bad option \"", z,
  2795         -            "\": must be -argcount, -deterministic or -returntype", (char*)0
         2872  +            "\": must be -argcount, -deterministic, -directonly,"
         2873  +            " or -returntype", (char*)0
  2796   2874           );
  2797   2875           return TCL_ERROR;
  2798   2876         }
  2799   2877       }
  2800   2878   
  2801   2879       pScript = objv[objc-1];
  2802   2880       zName = Tcl_GetStringFromObj(objv[2], 0);

Changes to src/test1.c.

  1105   1105     }
  1106   1106   #endif
  1107   1107   
  1108   1108     if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
  1109   1109     Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
  1110   1110     return TCL_OK;
  1111   1111   }
         1112  +
         1113  +/*
         1114  +** Usage:  sqlite3_drop_modules DB ?NAME ...?
         1115  +**
         1116  +** Invoke the sqlite3_drop_modules(D,L) interface on database
         1117  +** connection DB, in order to drop all modules except those named in
         1118  +** the argument.
         1119  +*/
         1120  +static int SQLITE_TCLAPI test_drop_modules(
         1121  +  void *NotUsed,
         1122  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         1123  +  int argc,              /* Number of arguments */
         1124  +  char **argv            /* Text of each argument */
         1125  +){
         1126  +  sqlite3 *db;
         1127  +
         1128  +  if( argc!=2 ){
         1129  +    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
         1130  +       " DB\"", 0);
         1131  +    return TCL_ERROR;
         1132  +  }
         1133  +  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
         1134  +  sqlite3_drop_modules(db, argc>2 ? (const char**)(argv+2) : 0);
         1135  +  return TCL_OK;
         1136  +}
  1112   1137   
  1113   1138   /*
  1114   1139   ** Routines to implement the x_count() aggregate function.
  1115   1140   **
  1116   1141   ** x_count() counts the number of non-null arguments.  But there are
  1117   1142   ** some twists for testing purposes.
  1118   1143   **
................................................................................
  6369   6394   */
  6370   6395   static int SQLITE_TCLAPI reset_prng_state(
  6371   6396     ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  6372   6397     Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  6373   6398     int objc,              /* Number of arguments */
  6374   6399     Tcl_Obj *CONST objv[]  /* Command arguments */
  6375   6400   ){
  6376         -  sqlite3_test_control(SQLITE_TESTCTRL_PRNG_RESET);
         6401  +  sqlite3_randomness(0,0);
         6402  +  return TCL_OK;
         6403  +}
         6404  +/*
         6405  +** tclcmd:  prng_seed INT ?DB?
         6406  +**
         6407  +** Set up the SQLITE_TESTCTRL_PRNG_SEED pragma with parameter INT and DB.
         6408  +** INT is an integer.  DB is a database connection, or a NULL pointer if
         6409  +** omitted.
         6410  +**
         6411  +** When INT!=0 and DB!=0, set the PRNG seed to the value of the schema
         6412  +** cookie for DB, or to INT if the schema cookie happens to be zero.
         6413  +**
         6414  +** When INT!=0 and DB==0, set the PRNG seed to just INT.
         6415  +**
         6416  +** If INT==0 and DB==0 then use the default procedure of calling the
         6417  +** xRandomness method on the default VFS to get the PRNG seed.
         6418  +*/
         6419  +static int SQLITE_TCLAPI prng_seed(
         6420  +  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
         6421  +  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
         6422  +  int objc,              /* Number of arguments */
         6423  +  Tcl_Obj *CONST objv[]  /* Command arguments */
         6424  +){
         6425  +  int i = 0;