/ Check-in [985bfc99]
Login

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

Overview
Comment:Merge latest trunk changes into this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | readonly-wal-recovery
Files: files | file ages | folders
SHA3-256: 985bfc992950625a45a7521bf4c8438cd0170de974dff976968be158ac5922a9
User & Date: dan 2017-11-01 07:06:41
Context
2017-11-01
20:59
If a readonly_shm connection cannot map the *-shm file because no other process is holding the DMS lock, have it read from the database file only, ignoring any content in the wal file. check-in: ce5d13c2 user: dan tags: readonly-wal-recovery
07:06
Merge latest trunk changes into this branch. check-in: 985bfc99 user: dan tags: readonly-wal-recovery
06:59
Fix a race condition in os_unix.c that might allow a client to use a *-shm file corrupted by a power failure if another client fails between locking the *-shm file and truncating it to zero bytes. check-in: d655bfab user: dan tags: readonly-wal-recovery
2017-10-31
14:56
Update the built procedures for the sqlite3_analyzer utility to allow it to be linked with an external sqlite3.o library. Automatically detect a missing dbstat extension and report the compile-time error. check-in: bb39744f user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   672    672   	$(TCLSH_CMD) $(TOP)/tool/vdbe-compress.tcl $(OPTS) <tsrc/vdbe.c >vdbe.new
   673    673   	mv vdbe.new tsrc/vdbe.c
   674    674   	cp fts5.c fts5.h tsrc
   675    675   	touch .target_source
   676    676   
   677    677   sqlite3.c:	.target_source $(TOP)/tool/mksqlite3c.tcl
   678    678   	$(TCLSH_CMD) $(TOP)/tool/mksqlite3c.tcl
   679         -	cp tsrc/shell.c tsrc/sqlite3ext.h .
          679  +	cp tsrc/sqlite3ext.h .
   680    680   	cp $(TOP)/ext/session/sqlite3session.h .
   681    681   
   682    682   sqlite3ext.h:	.target_source
   683    683   	cp tsrc/sqlite3ext.h .
   684    684   
   685    685   tclsqlite3.c:	sqlite3.c
   686    686   	echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c
................................................................................
  1199   1199   
  1200   1200   showjournal$(TEXE):	$(TOP)/tool/showjournal.c sqlite3.lo
  1201   1201   	$(LTLINK) -o $@ $(TOP)/tool/showjournal.c sqlite3.lo $(TLIBS)
  1202   1202   
  1203   1203   showwal$(TEXE):	$(TOP)/tool/showwal.c sqlite3.lo
  1204   1204   	$(LTLINK) -o $@ $(TOP)/tool/showwal.c sqlite3.lo $(TLIBS)
  1205   1205   
         1206  +showshm$(TEXE):	$(TOP)/tool/showshm.c
         1207  +	$(LTLINK) -o $@ $(TOP)/tool/showshm.c
         1208  +
  1206   1209   changeset$(TEXE):	$(TOP)/ext/session/changeset.c sqlite3.lo
  1207   1210   	$(LTLINK) -o $@ $(TOP)/ext/session/changeset.c sqlite3.lo $(TLIBS)
  1208   1211   
  1209   1212   rollback-test$(TEXE):	$(TOP)/tool/rollback-test.c sqlite3.lo
  1210   1213   	$(LTLINK) -o $@ $(TOP)/tool/rollback-test.c sqlite3.lo $(TLIBS)
  1211   1214   
  1212   1215   LogEst$(TEXE):	$(TOP)/tool/logest.c sqlite3.h

Changes to Makefile.msc.

  1645   1645   	del /Q tsrc\sqlite.h.in tsrc\parse.y 2>NUL
  1646   1646   	$(TCLSH_CMD) $(TOP)\tool\vdbe-compress.tcl $(OPTS) < tsrc\vdbe.c > vdbe.new
  1647   1647   	move vdbe.new tsrc\vdbe.c
  1648   1648   	echo > .target_source
  1649   1649   
  1650   1650   sqlite3.c:	.target_source sqlite3ext.h $(MKSQLITE3C_TOOL)
  1651   1651   	$(TCLSH_CMD) $(MKSQLITE3C_TOOL) $(MKSQLITE3C_ARGS)
  1652         -	copy tsrc\shell.c .
  1653   1652   	copy $(TOP)\ext\session\sqlite3session.h .
  1654   1653   
  1655   1654   sqlite3-all.c:	sqlite3.c $(TOP)\tool\split-sqlite3c.tcl
  1656   1655   	$(TCLSH_CMD) $(TOP)\tool\split-sqlite3c.tcl
  1657   1656   # <</mark>>
  1658   1657   
  1659   1658   # Rule to build the amalgamation
................................................................................
  2220   2219   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2221   2220   		$(TOP)\tool\showjournal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2222   2221   
  2223   2222   showwal.exe:	$(TOP)\tool\showwal.c $(SQLITE3C) $(SQLITE3H)
  2224   2223   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2225   2224   		$(TOP)\tool\showwal.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2226   2225   
         2226  +showshm.exe:	$(TOP)\tool\showshm.c
         2227  +	$(LTLINK) $(NO_WARN)	$(TOP)\tool\showshm.c /link $(LDFLAGS) $(LTLINKOPTS)
         2228  +
  2227   2229   changeset.exe:	$(TOP)\ext\session\changeset.c $(SQLITE3C) $(SQLITE3H)
  2228   2230   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \
  2229   2231   		-DSQLITE_ENABLE_SESSION=1 -DSQLITE_ENABLE_PREUPDATE_HOOK=1 \
  2230   2232   		$(TOP)\ext\session\changeset.c $(SQLITE3C) /link $(LDFLAGS) $(LTLINKOPTS)
  2231   2233   
  2232   2234   fts3view.exe:	$(TOP)\ext\fts3\tool\fts3view.c $(SQLITE3C) $(SQLITE3H)
  2233   2235   	$(LTLINK) $(NO_WARN) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION \

Changes to VERSION.

     1         -3.21.0
            1  +3.22.0

Changes to autoconf/configure.ac.

     8      8   #   --enable-static-shell
     9      9   #   --enable-dynamic-extensions
    10     10   #
    11     11   
    12     12   AC_PREREQ(2.61)
    13     13   AC_INIT(sqlite, --SQLITE-VERSION--, http://www.sqlite.org)
    14     14   AC_CONFIG_SRCDIR([sqlite3.c])
           15  +AC_CONFIG_AUX_DIR([.])
    15     16   
    16     17   # Use automake.
    17     18   AM_INIT_AUTOMAKE([foreign])
    18     19   
    19     20   AC_SYS_LARGEFILE
    20     21   
    21     22   # Check for required programs.

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

Changes to main.mk.

   578    578   	tclsh $(TOP)/tool/vdbe-compress.tcl $(OPTS) <tsrc/vdbe.c >vdbe.new
   579    579   	mv vdbe.new tsrc/vdbe.c
   580    580   	cp fts5.c fts5.h tsrc
   581    581   	touch target_source
   582    582   
   583    583   sqlite3.c:	target_source $(TOP)/tool/mksqlite3c.tcl
   584    584   	tclsh $(TOP)/tool/mksqlite3c.tcl
   585         -	cp tsrc/shell.c tsrc/sqlite3ext.h .
          585  +	cp tsrc/sqlite3ext.h .
   586    586   	cp $(TOP)/ext/session/sqlite3session.h .
   587    587   	echo '#ifndef USE_SYSTEM_SQLITE' >tclsqlite3.c
   588    588   	cat sqlite3.c >>tclsqlite3.c
   589    589   	echo '#endif /* USE_SYSTEM_SQLITE */' >>tclsqlite3.c
   590    590   	cat $(TOP)/src/tclsqlite.c >>tclsqlite3.c
   591    591   
   592    592   sqlite3ext.h:	target_source
................................................................................
   904    904   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showjournal$(EXE) \
   905    905   		$(TOP)/tool/showjournal.c sqlite3.o $(THREADLIB)
   906    906   
   907    907   showwal$(EXE):	$(TOP)/tool/showwal.c sqlite3.o
   908    908   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o showwal$(EXE) \
   909    909   		$(TOP)/tool/showwal.c sqlite3.o $(THREADLIB)
   910    910   
          911  +showshm$(EXE):	$(TOP)/tool/showshm.c
          912  +	$(TCC) -o showshm$(EXE) $(TOP)/tool/showshm.c
          913  +
   911    914   changeset$(EXE):	$(TOP)/ext/session/changeset.c sqlite3.o
   912    915   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o changeset$(EXE) \
   913    916   		$(TOP)/ext/session/changeset.c sqlite3.o $(THREADLIB)
   914    917   
   915    918   fts3view$(EXE):	$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o
   916    919   	$(TCC) -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -o fts3view$(EXE) \
   917    920   		$(TOP)/ext/fts3/tool/fts3view.c sqlite3.o $(THREADLIB)

Changes to src/build.c.

  3848   3848     if( !p && (pOn || pUsing) ){
  3849   3849       sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", 
  3850   3850         (pOn ? "ON" : "USING")
  3851   3851       );
  3852   3852       goto append_from_error;
  3853   3853     }
  3854   3854     p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
  3855         -  if( p==0 || NEVER(p->nSrc==0) ){
         3855  +  if( p==0 ){
  3856   3856       goto append_from_error;
  3857   3857     }
         3858  +  assert( p->nSrc>0 );
  3858   3859     pItem = &p->a[p->nSrc-1];
  3859   3860     assert( pAlias!=0 );
  3860   3861     if( pAlias->n ){
  3861   3862       pItem->zAlias = sqlite3NameFromToken(db, pAlias);
  3862   3863     }
  3863   3864     pItem->pSelect = pSubquery;
  3864   3865     pItem->pOn = pOn;

Changes to src/dbpage.c.

   216    216   static int dbpageFilter(
   217    217     sqlite3_vtab_cursor *pCursor, 
   218    218     int idxNum, const char *idxStr,
   219    219     int argc, sqlite3_value **argv
   220    220   ){
   221    221     DbpageCursor *pCsr = (DbpageCursor *)pCursor;
   222    222     DbpageTable *pTab = (DbpageTable *)pCursor->pVtab;
   223         -  int rc = SQLITE_OK;
          223  +  int rc;
   224    224     sqlite3 *db = pTab->db;
   225    225     Btree *pBt;
   226    226   
   227    227     /* Default setting is no rows of result */
   228    228     pCsr->pgno = 1; 
   229    229     pCsr->mxPgno = 0;
   230    230   
................................................................................
   250    250         pCsr->mxPgno = 0;
   251    251       }else{
   252    252         pCsr->mxPgno = pCsr->pgno;
   253    253       }
   254    254     }else{
   255    255       assert( pCsr->pgno==1 );
   256    256     }
          257  +  if( pCsr->pPage1 ) sqlite3PagerUnrefPageOne(pCsr->pPage1);
   257    258     rc = sqlite3PagerGet(pCsr->pPager, 1, &pCsr->pPage1, 0);
   258    259     return rc;
   259    260   }
   260    261   
   261    262   static int dbpageColumn(
   262    263     sqlite3_vtab_cursor *pCursor, 
   263    264     sqlite3_context *ctx, 

Changes to src/expr.c.

   948    948     assert( pToken );
   949    949     pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
   950    950     if( pNew==0 ){
   951    951       sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
   952    952       return 0;
   953    953     }
   954    954     pNew->x.pList = pList;
          955  +  ExprSetProperty(pNew, EP_HasFunc);
   955    956     assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   956    957     sqlite3ExprSetHeightAndFlags(pParse, pNew);
   957    958     return pNew;
   958    959   }
   959    960   
   960    961   /*
   961    962   ** Assign a variable number to an expression that encodes a wildcard

Changes to src/func.c.

   694    694         ** that point.
   695    695         **
   696    696         ** For a case-insensitive search, set variable cx to be the same as
   697    697         ** c but in the other case and search the input string for either
   698    698         ** c or cx.
   699    699         */
   700    700         if( c<=0x80 ){
   701         -        u32 cx;
          701  +        char zStop[3];
   702    702           int bMatch;
   703    703           if( noCase ){
   704         -          cx = sqlite3Toupper(c);
   705         -          c = sqlite3Tolower(c);
          704  +          zStop[0] = sqlite3Toupper(c);
          705  +          zStop[1] = sqlite3Tolower(c);
          706  +          zStop[2] = 0;
   706    707           }else{
   707         -          cx = c;
          708  +          zStop[0] = c;
          709  +          zStop[1] = 0;
   708    710           }
   709         -        while( (c2 = *(zString++))!=0 ){
   710         -          if( c2!=c && c2!=cx ) continue;
          711  +        while(1){
          712  +          zString += strcspn((const char*)zString, zStop);
          713  +          if( zString[0]==0 ) break;
          714  +          zString++;
   711    715             bMatch = patternCompare(zPattern,zString,pInfo,matchOther);
   712    716             if( bMatch!=SQLITE_NOMATCH ) return bMatch;
   713    717           }
   714    718         }else{
   715    719           int bMatch;
   716    720           while( (c2 = Utf8Read(zString))!=0 ){
   717    721             if( c2!=c ) continue;

Changes to src/insert.c.

   905    905           sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
   906    906         }else if( pSelect ){
   907    907           sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
   908    908         }else{
   909    909           VdbeOp *pOp;
   910    910           sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
   911    911           pOp = sqlite3VdbeGetOp(v, -1);
   912         -        if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
          912  +        assert( pOp!=0 );
          913  +        if( pOp->opcode==OP_Null && !IsVirtual(pTab) ){
   913    914             appendFlag = 1;
   914    915             pOp->opcode = OP_NewRowid;
   915    916             pOp->p1 = iDataCur;
   916    917             pOp->p2 = regRowid;
   917    918             pOp->p3 = regAutoinc;
   918    919           }
   919    920         }

Changes to src/pcache.c.

   562    562   
   563    563   /*
   564    564   ** Make sure the page is marked as clean. If it isn't clean already,
   565    565   ** make it so.
   566    566   */
   567    567   void sqlite3PcacheMakeClean(PgHdr *p){
   568    568     assert( sqlite3PcachePageSanity(p) );
   569         -  if( ALWAYS((p->flags & PGHDR_DIRTY)!=0) ){
   570         -    assert( (p->flags & PGHDR_CLEAN)==0 );
   571         -    pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
   572         -    p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
   573         -    p->flags |= PGHDR_CLEAN;
   574         -    pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
   575         -    assert( sqlite3PcachePageSanity(p) );
   576         -    if( p->nRef==0 ){
   577         -      pcacheUnpin(p);
   578         -    }
          569  +  assert( (p->flags & PGHDR_DIRTY)!=0 );
          570  +  assert( (p->flags & PGHDR_CLEAN)==0 );
          571  +  pcacheManageDirtyList(p, PCACHE_DIRTYLIST_REMOVE);
          572  +  p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC|PGHDR_WRITEABLE);
          573  +  p->flags |= PGHDR_CLEAN;
          574  +  pcacheTrace(("%p.CLEAN %d\n",p->pCache,p->pgno));
          575  +  assert( sqlite3PcachePageSanity(p) );
          576  +  if( p->nRef==0 ){
          577  +    pcacheUnpin(p);
   579    578     }
   580    579   }
   581    580   
   582    581   /*
   583    582   ** Make every page in the cache clean.
   584    583   */
   585    584   void sqlite3PcacheCleanAll(PCache *pCache){

Changes to src/prepare.c.

   469    469     ** We return -1000000 instead of the more usual -1 simply because using
   470    470     ** -1000000 as the incorrect index into db->aDb[] is much 
   471    471     ** more likely to cause a segfault than -1 (of course there are assert()
   472    472     ** statements too, but it never hurts to play the odds).
   473    473     */
   474    474     assert( sqlite3_mutex_held(db->mutex) );
   475    475     if( pSchema ){
   476         -    for(i=0; ALWAYS(i<db->nDb); i++){
          476  +    for(i=0; 1; i++){
          477  +      assert( i<db->nDb );
   477    478         if( db->aDb[i].pSchema==pSchema ){
   478    479           break;
   479    480         }
   480    481       }
   481    482       assert( i>=0 && i<db->nDb );
   482    483     }
   483    484     return i;

Changes to src/select.c.

  3379   3379   **  (18)  If the sub-query is a compound select, then all terms of the
  3380   3380   **        ORDER BY clause of the parent must be simple references to 
  3381   3381   **        columns of the sub-query.
  3382   3382   **
  3383   3383   **  (19)  If the subquery uses LIMIT then the outer query may not
  3384   3384   **        have a WHERE clause.
  3385   3385   **
  3386         -**  (**)  Subsumed into (17d3).  Was: If the sub-query is a compound select,
  3387         -**        then it must not use an ORDER BY clause - Ticket #3773.  Because
  3388         -**        of (17d3), then only way to have a compound subquery is if it is
  3389         -**        the only term in the FROM clause of the outer query.  But if the
  3390         -**        only term in the FROM clause has an ORDER BY, then it will be
  3391         -**        implemented as a co-routine and the flattener will never be called.
         3386  +**  (20)  If the sub-query is a compound select, then it must not use
         3387  +**        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
         3388  +**        somewhat by saying that the terms of the ORDER BY clause must
         3389  +**        appear as unmodified result columns in the outer query.  But we
         3390  +**        have other optimizations in mind to deal with that case.
  3392   3391   **
  3393   3392   **  (21)  If the subquery uses LIMIT then the outer query may not be
  3394   3393   **        DISTINCT.  (See ticket [752e1646fc]).
  3395   3394   **
  3396   3395   **  (22)  The subquery may not be a recursive CTE.
  3397   3396   **
  3398   3397   **  (**)  Subsumed into restriction (17d3).  Was: If the outer query is
................................................................................
  3518   3517   
  3519   3518     /* Restriction (17): If the sub-query is a compound SELECT, then it must
  3520   3519     ** use only the UNION ALL operator. And none of the simple select queries
  3521   3520     ** that make up the compound SELECT are allowed to be aggregate or distinct
  3522   3521     ** queries.
  3523   3522     */
  3524   3523     if( pSub->pPrior ){
         3524  +    if( pSub->pOrderBy ){
         3525  +      return 0;  /* Restriction (20) */
         3526  +    }
  3525   3527       if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
  3526   3528         return 0; /* (17d1), (17d2), or (17d3) */
  3527   3529       }
  3528   3530       for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
  3529   3531         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
  3530   3532         testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
  3531   3533         assert( pSub->pSrc!=0 );
................................................................................
  3552   3554     ** The only way that the recursive part of a CTE can contain a compound
  3553   3555     ** subquery is for the subquery to be one term of a join.  But if the
  3554   3556     ** subquery is a join, then the flattening has already been stopped by
  3555   3557     ** restriction (17d3)
  3556   3558     */
  3557   3559     assert( (p->selFlags & SF_Recursive)==0 || pSub->pPrior==0 );
  3558   3560   
  3559         -  /* Ex-restriction (20):
  3560         -  ** A compound subquery must be the only term in the FROM clause of the
  3561         -  ** outer query by restriction (17d3).  But if that term also has an
  3562         -  ** ORDER BY clause, then the subquery will be implemented by co-routine
  3563         -  ** and so the flattener will never be invoked.  Hence, it is not possible
  3564         -  ** for the subquery to be a compound and have an ORDER BY clause.
  3565         -  */
  3566         -  assert( pSub->pPrior==0 || pSub->pOrderBy==0 );
  3567         -
  3568   3561     /***** If we reach this point, flattening is permitted. *****/
  3569   3562     SELECTTRACE(1,pParse,p,("flatten %s.%p from term %d\n",
  3570   3563                      pSub->zSelName, pSub, iFrom));
  3571   3564   
  3572   3565     /* Authorize the subquery */
  3573   3566     pParse->zAuthContext = pSubitem->zName;
  3574   3567     TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
................................................................................
  3915   3908       }
  3916   3909     }
  3917   3910     return nChng;
  3918   3911   }
  3919   3912   #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
  3920   3913   
  3921   3914   /*
  3922         -** Based on the contents of the AggInfo structure indicated by the first
  3923         -** argument, this function checks if the following are true:
  3924         -**
  3925         -**    * the query contains just a single aggregate function,
  3926         -**    * the aggregate function is either min() or max(), and
  3927         -**    * the argument to the aggregate function is a column value.
  3928         -**
  3929         -** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
  3930         -** is returned as appropriate. Also, *ppMinMax is set to point to the 
  3931         -** list of arguments passed to the aggregate before returning.
  3932         -**
  3933         -** Or, if the conditions above are not met, *ppMinMax is set to 0 and
  3934         -** WHERE_ORDERBY_NORMAL is returned.
  3935         -*/
  3936         -static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
  3937         -  int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
  3938         -
  3939         -  *ppMinMax = 0;
  3940         -  if( pAggInfo->nFunc==1 ){
  3941         -    Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
  3942         -    ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
  3943         -
  3944         -    assert( pExpr->op==TK_AGG_FUNCTION );
  3945         -    if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
  3946         -      const char *zFunc = pExpr->u.zToken;
  3947         -      if( sqlite3StrICmp(zFunc, "min")==0 ){
  3948         -        eRet = WHERE_ORDERBY_MIN;
  3949         -        *ppMinMax = pEList;
  3950         -      }else if( sqlite3StrICmp(zFunc, "max")==0 ){
  3951         -        eRet = WHERE_ORDERBY_MAX;
  3952         -        *ppMinMax = pEList;
  3953         -      }
  3954         -    }
  3955         -  }
  3956         -
  3957         -  assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
         3915  +** The pFunc is the only aggregate function in the query.  Check to see
         3916  +** if the query is a candidate for the min/max optimization. 
         3917  +**
         3918  +** If the query is a candidate for the min/max optimization, then set
         3919  +** *ppMinMax to be an ORDER BY clause to be used for the optimization
         3920  +** and return either WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX depending on
         3921  +** whether pFunc is a min() or max() function.
         3922  +**
         3923  +** If the query is not a candidate for the min/max optimization, return
         3924  +** WHERE_ORDERBY_NORMAL (which must be zero).
         3925  +**
         3926  +** This routine must be called after aggregate functions have been
         3927  +** located but before their arguments have been subjected to aggregate
         3928  +** analysis.
         3929  +*/
         3930  +static u8 minMaxQuery(sqlite3 *db, Expr *pFunc, ExprList **ppMinMax){
         3931  +  int eRet = WHERE_ORDERBY_NORMAL;      /* Return value */
         3932  +  ExprList *pEList = pFunc->x.pList;    /* Arguments to agg function */
         3933  +  const char *zFunc;                    /* Name of aggregate function pFunc */
         3934  +  ExprList *pOrderBy;
         3935  +  u8 sortOrder;
         3936  +
         3937  +  assert( *ppMinMax==0 );
         3938  +  assert( pFunc->op==TK_AGG_FUNCTION );
         3939  +  if( pEList==0 || pEList->nExpr!=1 ) return eRet;
         3940  +  zFunc = pFunc->u.zToken;
         3941  +  if( sqlite3StrICmp(zFunc, "min")==0 ){
         3942  +    eRet = WHERE_ORDERBY_MIN;
         3943  +    sortOrder = SQLITE_SO_ASC;
         3944  +  }else if( sqlite3StrICmp(zFunc, "max")==0 ){
         3945  +    eRet = WHERE_ORDERBY_MAX;
         3946  +    sortOrder = SQLITE_SO_DESC;
         3947  +  }else{
         3948  +    return eRet;
         3949  +  }
         3950  +  *ppMinMax = pOrderBy = sqlite3ExprListDup(db, pEList, 0);
         3951  +  assert( pOrderBy!=0 || db->mallocFailed );
         3952  +  if( pOrderBy ) pOrderBy->a[0].sortOrder = sortOrder;
  3958   3953     return eRet;
  3959   3954   }
  3960   3955   
  3961   3956   /*
  3962   3957   ** The select statement passed as the first argument is an aggregate query.
  3963   3958   ** The second argument is the associated aggregate-info object. This 
  3964   3959   ** function tests if the SELECT is of the form:
................................................................................
  4337   4332     int i, j, k;
  4338   4333     SrcList *pTabList;
  4339   4334     ExprList *pEList;
  4340   4335     struct SrcList_item *pFrom;
  4341   4336     sqlite3 *db = pParse->db;
  4342   4337     Expr *pE, *pRight, *pExpr;
  4343   4338     u16 selFlags = p->selFlags;
         4339  +  u32 elistFlags = 0;
  4344   4340   
  4345   4341     p->selFlags |= SF_Expanded;
  4346   4342     if( db->mallocFailed  ){
  4347   4343       return WRC_Abort;
  4348   4344     }
  4349         -  if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
         4345  +  assert( p->pSrc!=0 );
         4346  +  if( (selFlags & SF_Expanded)!=0 ){
  4350   4347       return WRC_Prune;
  4351   4348     }
  4352   4349     pTabList = p->pSrc;
  4353   4350     pEList = p->pEList;
  4354   4351     if( OK_IF_ALWAYS_TRUE(p->pWith) ){
  4355   4352       sqlite3WithPush(pParse, p->pWith, 0);
  4356   4353     }
................................................................................
  4449   4446     */
  4450   4447     for(k=0; k<pEList->nExpr; k++){
  4451   4448       pE = pEList->a[k].pExpr;
  4452   4449       if( pE->op==TK_ASTERISK ) break;
  4453   4450       assert( pE->op!=TK_DOT || pE->pRight!=0 );
  4454   4451       assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
  4455   4452       if( pE->op==TK_DOT && pE->pRight->op==TK_ASTERISK ) break;
         4453  +    elistFlags |= pE->flags;
  4456   4454     }
  4457   4455     if( k<pEList->nExpr ){
  4458   4456       /*
  4459   4457       ** If we get here it means the result set contains one or more "*"
  4460   4458       ** operators that need to be expanded.  Loop through each expression
  4461   4459       ** in the result set and expand them one by one.
  4462   4460       */
................................................................................
  4464   4462       ExprList *pNew = 0;
  4465   4463       int flags = pParse->db->flags;
  4466   4464       int longNames = (flags & SQLITE_FullColNames)!=0
  4467   4465                         && (flags & SQLITE_ShortColNames)==0;
  4468   4466   
  4469   4467       for(k=0; k<pEList->nExpr; k++){
  4470   4468         pE = a[k].pExpr;
         4469  +      elistFlags |= pE->flags;
  4471   4470         pRight = pE->pRight;
  4472   4471         assert( pE->op!=TK_DOT || pRight!=0 );
  4473   4472         if( pE->op!=TK_ASTERISK
  4474   4473          && (pE->op!=TK_DOT || pRight->op!=TK_ASTERISK)
  4475   4474         ){
  4476   4475           /* This particular expression does not need to be expanded.
  4477   4476           */
................................................................................
  4593   4592             }
  4594   4593           }
  4595   4594         }
  4596   4595       }
  4597   4596       sqlite3ExprListDelete(db, pEList);
  4598   4597       p->pEList = pNew;
  4599   4598     }
  4600         -  if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
  4601         -    sqlite3ErrorMsg(pParse, "too many columns in result set");
  4602         -    return WRC_Abort;
         4599  +  if( p->pEList ){
         4600  +    if( p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
         4601  +      sqlite3ErrorMsg(pParse, "too many columns in result set");
         4602  +      return WRC_Abort;
         4603  +    }
         4604  +    if( (elistFlags & (EP_HasFunc|EP_Subquery))!=0 ){
         4605  +      p->selFlags |= SF_ComplexResult;
         4606  +    }
  4603   4607     }
  4604   4608     return WRC_Continue;
  4605   4609   }
  4606   4610   
  4607   4611   /*
  4608   4612   ** No-op routine for the parse-tree walker.
  4609   4613   **
................................................................................
  5131   5135     Expr *pHaving;         /* The HAVING clause.  May be NULL */
  5132   5136     int rc = 1;            /* Value to return from this function */
  5133   5137     DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
  5134   5138     SortCtx sSort;         /* Info on how to code the ORDER BY clause */
  5135   5139     AggInfo sAggInfo;      /* Information used by aggregate queries */
  5136   5140     int iEnd;              /* Address of the end of the query */
  5137   5141     sqlite3 *db;           /* The database connection */
         5142  +  ExprList *pMinMaxOrderBy = 0;  /* Added ORDER BY for min/max queries */
         5143  +  u8 minMaxFlag;                 /* Flag for min/max queries */
  5138   5144   
  5139   5145   #ifndef SQLITE_OMIT_EXPLAIN
  5140   5146     int iRestoreSelectId = pParse->iSelectId;
  5141   5147     pParse->iSelectId = pParse->iNextSelectId++;
  5142   5148   #endif
  5143   5149   
  5144   5150     db = pParse->db;
................................................................................
  5217   5223       ** is not a join.  But if the outer query is not a join, then the subquery
  5218   5224       ** will be implemented as a co-routine and there is no advantage to
  5219   5225       ** flattening in that case.
  5220   5226       */
  5221   5227       if( (pSub->selFlags & SF_Aggregate)!=0 ) continue;
  5222   5228       assert( pSub->pGroupBy==0 );
  5223   5229   
  5224         -    /* If the subquery contains an ORDER BY clause and if
         5230  +    /* If the outer query contains a "complex" result set (that is,
         5231  +    ** if the result set of the outer query uses functions or subqueries)
         5232  +    ** and if the subquery contains an ORDER BY clause and if
  5225   5233       ** it will be implemented as a co-routine, then do not flatten.  This
  5226   5234       ** restriction allows SQL constructs like this:
  5227   5235       **
  5228   5236       **  SELECT expensive_function(x)
  5229   5237       **    FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
  5230   5238       **
  5231   5239       ** The expensive_function() is only computed on the 10 rows that
  5232   5240       ** are output, rather than every row of the table.
         5241  +    **
         5242  +    ** The requirement that the outer query have a complex result set
         5243  +    ** means that flattening does occur on simpler SQL constraints without
         5244  +    ** the expensive_function() like:
         5245  +    **
         5246  +    **  SELECT x FROM (SELECT x FROM tab ORDER BY y LIMIT 10);
  5233   5247       */
  5234   5248       if( pSub->pOrderBy!=0
  5235   5249        && i==0
         5250  +     && (p->selFlags & SF_ComplexResult)!=0
  5236   5251        && (pTabList->nSrc==1
  5237   5252            || (pTabList->a[1].fg.jointype&(JT_LEFT|JT_CROSS))!=0)
  5238   5253       ){
  5239   5254         continue;
  5240   5255       }
  5241   5256   
  5242   5257       if( flattenSubquery(pParse, p, i, isAgg) ){
................................................................................
  5647   5662           assert( pWhere==p->pWhere );
  5648   5663           havingToWhere(pParse, pGroupBy, pHaving, &p->pWhere);
  5649   5664           pWhere = p->pWhere;
  5650   5665         }
  5651   5666         sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
  5652   5667       }
  5653   5668       sAggInfo.nAccumulator = sAggInfo.nColumn;
         5669  +    if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
         5670  +      minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
         5671  +    }else{
         5672  +      minMaxFlag = WHERE_ORDERBY_NORMAL;
         5673  +    }
  5654   5674       for(i=0; i<sAggInfo.nFunc; i++){
  5655   5675         assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
  5656   5676         sNC.ncFlags |= NC_InAggFunc;
  5657   5677         sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
  5658   5678         sNC.ncFlags &= ~NC_InAggFunc;
  5659   5679       }
  5660   5680       sAggInfo.mxReg = pParse->nMem;
  5661   5681       if( db->mallocFailed ) goto select_end;
         5682  +#if SELECTTRACE_ENABLED
         5683  +    if( sqlite3SelectTrace & 0x400 ){
         5684  +      int ii;
         5685  +      SELECTTRACE(0x400,pParse,p,("After aggregate analysis:\n"));
         5686  +      sqlite3TreeViewSelect(0, p, 0);
         5687  +      for(ii=0; ii<sAggInfo.nColumn; ii++){
         5688  +        sqlite3DebugPrintf("agg-column[%d] iMem=%d\n",
         5689  +            ii, sAggInfo.aCol[ii].iMem);
         5690  +        sqlite3TreeViewExpr(0, sAggInfo.aCol[ii].pExpr, 0);
         5691  +      }
         5692  +      for(ii=0; ii<sAggInfo.nFunc; ii++){
         5693  +        sqlite3DebugPrintf("agg-func[%d]: iMem=%d\n",
         5694  +            ii, sAggInfo.aFunc[ii].iMem);
         5695  +        sqlite3TreeViewExpr(0, sAggInfo.aFunc[ii].pExpr, 0);
         5696  +      }
         5697  +    }
         5698  +#endif
         5699  +
  5662   5700   
  5663   5701       /* Processing for aggregates with GROUP BY is very different and
  5664   5702       ** much more complex than aggregates without a GROUP BY.
  5665   5703       */
  5666   5704       if( pGroupBy ){
  5667   5705         KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
  5668   5706         int addr1;          /* A-vs-B comparision jump */
................................................................................
  5884   5922         */
  5885   5923         sqlite3VdbeResolveLabel(v, addrReset);
  5886   5924         resetAccumulator(pParse, &sAggInfo);
  5887   5925         sqlite3VdbeAddOp1(v, OP_Return, regReset);
  5888   5926        
  5889   5927       } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
  5890   5928       else {
  5891         -      ExprList *pDel = 0;
  5892   5929   #ifndef SQLITE_OMIT_BTREECOUNT
  5893   5930         Table *pTab;
  5894   5931         if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
  5895   5932           /* If isSimpleCount() returns a pointer to a Table structure, then
  5896   5933           ** the SQL statement is of the form:
  5897   5934           **
  5898   5935           **   SELECT count(*) FROM <tbl>
................................................................................
  5946   5983           }
  5947   5984           sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
  5948   5985           sqlite3VdbeAddOp1(v, OP_Close, iCsr);
  5949   5986           explainSimpleCount(pParse, pTab, pBest);
  5950   5987         }else
  5951   5988   #endif /* SQLITE_OMIT_BTREECOUNT */
  5952   5989         {
  5953         -        /* Check if the query is of one of the following forms:
  5954         -        **
  5955         -        **   SELECT min(x) FROM ...
  5956         -        **   SELECT max(x) FROM ...
  5957         -        **
  5958         -        ** If it is, then ask the code in where.c to attempt to sort results
  5959         -        ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. 
  5960         -        ** If where.c is able to produce results sorted in this order, then
  5961         -        ** add vdbe code to break out of the processing loop after the 
  5962         -        ** first iteration (since the first iteration of the loop is 
  5963         -        ** guaranteed to operate on the row with the minimum or maximum 
  5964         -        ** value of x, the only row required).
  5965         -        **
  5966         -        ** A special flag must be passed to sqlite3WhereBegin() to slightly
  5967         -        ** modify behavior as follows:
  5968         -        **
  5969         -        **   + If the query is a "SELECT min(x)", then the loop coded by
  5970         -        **     where.c should not iterate over any values with a NULL value
  5971         -        **     for x.
  5972         -        **
  5973         -        **   + The optimizer code in where.c (the thing that decides which
  5974         -        **     index or indices to use) should place a different priority on 
  5975         -        **     satisfying the 'ORDER BY' clause than it does in other cases.
  5976         -        **     Refer to code and comments in where.c for details.
  5977         -        */
  5978         -        ExprList *pMinMax = 0;
  5979         -        u8 flag = WHERE_ORDERBY_NORMAL;
  5980         -        
  5981         -        assert( p->pGroupBy==0 );
  5982         -        assert( flag==0 );
  5983         -        if( p->pHaving==0 ){
  5984         -          flag = minMaxQuery(&sAggInfo, &pMinMax);
  5985         -        }
  5986         -        assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
  5987         -
  5988         -        if( flag ){
  5989         -          pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
  5990         -          pDel = pMinMax;
  5991         -          assert( db->mallocFailed || pMinMax!=0 );
  5992         -          if( !db->mallocFailed ){
  5993         -            pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
  5994         -            pMinMax->a[0].pExpr->op = TK_COLUMN;
  5995         -          }
  5996         -        }
  5997         -  
  5998   5990           /* This case runs if the aggregate has no GROUP BY clause.  The
  5999   5991           ** processing is much simpler since there is only a single row
  6000   5992           ** of output.
  6001   5993           */
         5994  +        assert( p->pGroupBy==0 );
  6002   5995           resetAccumulator(pParse, &sAggInfo);
  6003         -        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax, 0,flag,0);
         5996  +
         5997  +        /* If this query is a candidate for the min/max optimization, then
         5998  +        ** minMaxFlag will have been previously set to either
         5999  +        ** WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX and pMinMaxOrderBy will
         6000  +        ** be an appropriate ORDER BY expression for the optimization.
         6001  +        */
         6002  +        assert( minMaxFlag==WHERE_ORDERBY_NORMAL || pMinMaxOrderBy!=0 );
         6003  +        assert( pMinMaxOrderBy==0 || pMinMaxOrderBy->nExpr==1 );
         6004  +
         6005  +        pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMaxOrderBy,
         6006  +                                   0, minMaxFlag, 0);
  6004   6007           if( pWInfo==0 ){
  6005         -          sqlite3ExprListDelete(db, pDel);
  6006   6008             goto select_end;
  6007   6009           }
  6008   6010           updateAccumulator(pParse, &sAggInfo);
  6009         -        assert( pMinMax==0 || pMinMax->nExpr==1 );
  6010   6011           if( sqlite3WhereIsOrdered(pWInfo)>0 ){
  6011   6012             sqlite3VdbeGoto(v, sqlite3WhereBreakLabel(pWInfo));
  6012   6013             VdbeComment((v, "%s() by index",
  6013         -                (flag==WHERE_ORDERBY_MIN?"min":"max")));
         6014  +                (minMaxFlag==WHERE_ORDERBY_MIN?"min":"max")));
  6014   6015           }
  6015   6016           sqlite3WhereEnd(pWInfo);
  6016   6017           finalizeAggFunctions(pParse, &sAggInfo);
  6017   6018         }
  6018   6019   
  6019   6020         sSort.pOrderBy = 0;
  6020   6021         sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
  6021   6022         selectInnerLoop(pParse, p, -1, 0, 0, 
  6022   6023                         pDest, addrEnd, addrEnd);
  6023         -      sqlite3ExprListDelete(db, pDel);
  6024   6024       }
  6025   6025       sqlite3VdbeResolveLabel(v, addrEnd);
  6026   6026       
  6027   6027     } /* endif aggregate query */
  6028   6028   
  6029   6029     if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
  6030   6030       explainTempTable(pParse, "DISTINCT");
................................................................................
  6048   6048     rc = (pParse->nErr>0);
  6049   6049   
  6050   6050     /* Control jumps to here if an error is encountered above, or upon
  6051   6051     ** successful coding of the SELECT.
  6052   6052     */
  6053   6053   select_end:
  6054   6054     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
  6055         -
         6055  +  sqlite3ExprListDelete(db, pMinMaxOrderBy);
  6056   6056     sqlite3DbFree(db, sAggInfo.aCol);
  6057   6057     sqlite3DbFree(db, sAggInfo.aFunc);
  6058   6058   #if SELECTTRACE_ENABLED
  6059   6059     SELECTTRACE(1,pParse,p,("end processing\n"));
  6060   6060     pParse->nSelectIndent--;
  6061   6061   #endif
  6062   6062     return rc;
  6063   6063   }

Changes to src/shell.c.in.

  1201   1201       }
  1202   1202     }
  1203   1203     if( bSep ){
  1204   1204       utf8_printf(p->out, "%s", p->colSeparator);
  1205   1205     }
  1206   1206   }
  1207   1207   
  1208         -#ifdef SIGINT
  1209   1208   /*
  1210   1209   ** This routine runs when the user presses Ctrl-C
  1211   1210   */
  1212   1211   static void interrupt_handler(int NotUsed){
  1213   1212     UNUSED_PARAMETER(NotUsed);
  1214   1213     seenInterrupt++;
  1215   1214     if( seenInterrupt>2 ) exit(1);
  1216   1215     if( globalDb ) sqlite3_interrupt(globalDb);
  1217   1216   }
         1217  +
         1218  +#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
         1219  +/*
         1220  +** This routine runs for console events (e.g. Ctrl-C) on Win32
         1221  +*/
         1222  +static BOOL WINAPI ConsoleCtrlHandler(
         1223  +  DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
         1224  +){
         1225  +  if( dwCtrlType==CTRL_C_EVENT ){
         1226  +    interrupt_handler(0);
         1227  +    return TRUE;
         1228  +  }
         1229  +  return FALSE;
         1230  +}
  1218   1231   #endif
  1219   1232   
  1220   1233   #ifndef SQLITE_OMIT_AUTHORIZATION
  1221   1234   /*
  1222   1235   ** When the ".auth ON" is set, the following authorizer callback is
  1223   1236   ** invoked.  It always returns SQLITE_OK.
  1224   1237   */
................................................................................
  6685   6698     Argv0 = argv[0];
  6686   6699   
  6687   6700     /* Make sure we have a valid signal handler early, before anything
  6688   6701     ** else is done.
  6689   6702     */
  6690   6703   #ifdef SIGINT
  6691   6704     signal(SIGINT, interrupt_handler);
         6705  +#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
         6706  +  SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
  6692   6707   #endif
  6693   6708   
  6694   6709   #ifdef SQLITE_SHELL_DBNAME_PROC
  6695   6710     {
  6696   6711       /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
  6697   6712       ** of a C-function that will provide the name of the database file.  Use
  6698   6713       ** this compile-time option to embed this shell program in larger

Changes to src/sqliteInt.h.

  2398   2398   };
  2399   2399   
  2400   2400   /*
  2401   2401   ** The following are the meanings of bits in the Expr.flags field.
  2402   2402   */
  2403   2403   #define EP_FromJoin  0x000001 /* Originates in ON/USING clause of outer join */
  2404   2404   #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
  2405         -                  /* 0x000004 // available for use */
         2405  +#define EP_HasFunc   0x000004 /* Contains one or more functions of any kind */
  2406   2406                     /* 0x000008 // available for use */
  2407   2407   #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
  2408   2408   #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
  2409   2409   #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
  2410   2410   #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
  2411   2411   #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE operator */
  2412   2412   #define EP_Generic   0x000200 /* Ignore COLLATE or affinity on this tree */
................................................................................
  2422   2422   #define EP_ConstFunc 0x080000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
  2423   2423   #define EP_CanBeNull 0x100000 /* Can be null despite NOT NULL constraint */
  2424   2424   #define EP_Subquery  0x200000 /* Tree contains a TK_SELECT operator */
  2425   2425   #define EP_Alias     0x400000 /* Is an alias for a result set column */
  2426   2426   #define EP_Leaf      0x800000 /* Expr.pLeft, .pRight, .u.pSelect all NULL */
  2427   2427   
  2428   2428   /*
  2429         -** Combinations of two or more EP_* flags
         2429  +** The EP_Propagate mask is a set of properties that automatically propagate
         2430  +** upwards into parent nodes.
  2430   2431   */
  2431         -#define EP_Propagate (EP_Collate|EP_Subquery) /* Propagate these bits up tree */
         2432  +#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
  2432   2433   
  2433   2434   /*
  2434   2435   ** These macros can be used to test, set, or clear bits in the
  2435   2436   ** Expr.flags field.
  2436   2437   */
  2437   2438   #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
  2438   2439   #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
................................................................................
  2704   2705   #define NC_PartIdx   0x0002  /* True if resolving a partial index WHERE */
  2705   2706   #define NC_IsCheck   0x0004  /* True if resolving names in a CHECK constraint */
  2706   2707   #define NC_InAggFunc 0x0008  /* True if analyzing arguments to an agg func */
  2707   2708   #define NC_HasAgg    0x0010  /* One or more aggregate functions seen */
  2708   2709   #define NC_IdxExpr   0x0020  /* True if resolving columns of CREATE INDEX */
  2709   2710   #define NC_VarSelect 0x0040  /* A correlated subquery has been seen */
  2710   2711   #define NC_MinMaxAgg 0x1000  /* min/max aggregates seen.  See note above */
         2712  +#define NC_Complex   0x2000  /* True if a function or subquery seen */
  2711   2713   
  2712   2714   /*
  2713   2715   ** An instance of the following structure contains all information
  2714   2716   ** needed to generate code for a single SELECT statement.
  2715   2717   **
  2716   2718   ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
  2717   2719   ** If there is a LIMIT clause, the parser sets nLimit to the value of the
................................................................................
  2774   2776   #define SF_NestedFrom     0x00800  /* Part of a parenthesized FROM clause */
  2775   2777   #define SF_MinMaxAgg      0x01000  /* Aggregate containing min() or max() */
  2776   2778   #define SF_Recursive      0x02000  /* The recursive part of a recursive CTE */
  2777   2779   #define SF_FixedLimit     0x04000  /* nSelectRow set by a constant LIMIT */
  2778   2780   #define SF_MaybeConvert   0x08000  /* Need convertCompoundSelectToSubquery() */
  2779   2781   #define SF_Converted      0x10000  /* By convertCompoundSelectToSubquery() */
  2780   2782   #define SF_IncludeHidden  0x20000  /* Include hidden columns in output */
         2783  +#define SF_ComplexResult  0x40000  /* Result set contains subquery or function */
  2781   2784   
  2782   2785   
  2783   2786   /*
  2784   2787   ** The results of a SELECT can be distributed in several ways, as defined
  2785   2788   ** by one of the following macros.  The "SRT" prefix means "SELECT Result
  2786   2789   ** Type".
  2787   2790   **

Changes to src/vdbeaux.c.

  3039   3039   
  3040   3040   /*
  3041   3041   ** Delete an entire VDBE.
  3042   3042   */
  3043   3043   void sqlite3VdbeDelete(Vdbe *p){
  3044   3044     sqlite3 *db;
  3045   3045   
  3046         -  if( NEVER(p==0) ) return;
         3046  +  assert( p!=0 );
  3047   3047     db = p->db;
  3048   3048     assert( sqlite3_mutex_held(db->mutex) );
  3049   3049     sqlite3VdbeClearObject(db, p);
  3050   3050     if( p->pPrev ){
  3051   3051       p->pPrev->pNext = p->pNext;
  3052   3052     }else{
  3053   3053       assert( db->pVdbe==p );

Changes to src/vdbemem.c.

   346    346   ** This routine calls the finalize method for that function.  The
   347    347   ** result of the aggregate is stored back into pMem.
   348    348   **
   349    349   ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
   350    350   ** otherwise.
   351    351   */
   352    352   int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
   353         -  int rc = SQLITE_OK;
   354         -  if( ALWAYS(pFunc && pFunc->xFinalize) ){
   355         -    sqlite3_context ctx;
   356         -    Mem t;
   357         -    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
   358         -    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
   359         -    memset(&ctx, 0, sizeof(ctx));
   360         -    memset(&t, 0, sizeof(t));
   361         -    t.flags = MEM_Null;
   362         -    t.db = pMem->db;
   363         -    ctx.pOut = &t;
   364         -    ctx.pMem = pMem;
   365         -    ctx.pFunc = pFunc;
   366         -    pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
   367         -    assert( (pMem->flags & MEM_Dyn)==0 );
   368         -    if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
   369         -    memcpy(pMem, &t, sizeof(t));
   370         -    rc = ctx.isError;
   371         -  }
   372         -  return rc;
          353  +  sqlite3_context ctx;
          354  +  Mem t;
          355  +  assert( pFunc!=0 );
          356  +  assert( pFunc->xFinalize!=0 );
          357  +  assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
          358  +  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
          359  +  memset(&ctx, 0, sizeof(ctx));
          360  +  memset(&t, 0, sizeof(t));
          361  +  t.flags = MEM_Null;
          362  +  t.db = pMem->db;
          363  +  ctx.pOut = &t;
          364  +  ctx.pMem = pMem;
          365  +  ctx.pFunc = pFunc;
          366  +  pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
          367  +  assert( (pMem->flags & MEM_Dyn)==0 );
          368  +  if( pMem->szMalloc>0 ) sqlite3DbFreeNN(pMem->db, pMem->zMalloc);
          369  +  memcpy(pMem, &t, sizeof(t));
          370  +  return ctx.isError;
   373    371   }
   374    372   
   375    373   /*
   376    374   ** If the memory cell contains a value that must be freed by
   377    375   ** invoking the external callback in Mem.xDel, then this routine
   378    376   ** will free that value.  It also sets Mem.flags to MEM_Null.
   379    377   **

Changes to src/wal.c.

   127    127   ** WAL-INDEX FORMAT
   128    128   **
   129    129   ** Conceptually, the wal-index is shared memory, though VFS implementations
   130    130   ** might choose to implement the wal-index using a mmapped file.  Because
   131    131   ** the wal-index is shared memory, SQLite does not support journal_mode=WAL 
   132    132   ** on a network filesystem.  All users of the database must be able to
   133    133   ** share memory.
          134  +**
          135  +** In the default unix and windows implementation, the wal-index is a mmapped
          136  +** file whose name is the database name with a "-shm" suffix added.  For that
          137  +** reason, the wal-index is sometimes called the "shm" file.
   134    138   **
   135    139   ** The wal-index is transient.  After a crash, the wal-index can (and should
   136    140   ** be) reconstructed from the original WAL file.  In fact, the VFS is required
   137    141   ** to either truncate or zero the header of the wal-index when the last
   138    142   ** connection to it closes.  Because the wal-index is transient, it can
   139    143   ** use an architecture-specific format; it does not have to be cross-platform.
   140    144   ** Hence, unlike the database and WAL file formats which store all values
................................................................................
   267    271   ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
   268    272   ** returns SQLITE_CANTOPEN.
   269    273   */
   270    274   #define WAL_MAX_VERSION      3007000
   271    275   #define WALINDEX_MAX_VERSION 3007000
   272    276   
   273    277   /*
   274         -** Indices of various locking bytes.   WAL_NREADER is the number
          278  +** Index numbers for various locking bytes.   WAL_NREADER is the number
   275    279   ** of available reader locks and should be at least 3.  The default
   276    280   ** is SQLITE_SHM_NLOCK==8 and  WAL_NREADER==5.
          281  +**
          282  +** Technically, the various VFSes are free to implement these locks however
          283  +** they see fit.  However, compatibility is encouraged so that VFSes can
          284  +** interoperate.  The standard implemention used on both unix and windows
          285  +** is for the index number to indicate a byte offset into the
          286  +** WalCkptInfo.aLock[] array in the wal-index header.  In other words, all
          287  +** locks are on the shm file.  The WALINDEX_LOCK_OFFSET constant (which
          288  +** should be 120) is the location in the shm file for the first locking
          289  +** byte.
   277    290   */
   278    291   #define WAL_WRITE_LOCK         0
   279    292   #define WAL_ALL_BUT_WRITE      1
   280    293   #define WAL_CKPT_LOCK          1
   281    294   #define WAL_RECOVER_LOCK       2
   282    295   #define WAL_READ_LOCK(I)       (3+(I))
   283    296   #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
................................................................................
   393    406   #define WALINDEX_LOCK_OFFSET (sizeof(WalIndexHdr)*2+offsetof(WalCkptInfo,aLock))
   394    407   #define WALINDEX_HDR_SIZE    (sizeof(WalIndexHdr)*2+sizeof(WalCkptInfo))
   395    408   
   396    409   /* Size of header before each frame in wal */
   397    410   #define WAL_FRAME_HDRSIZE 24
   398    411   
   399    412   /* Size of write ahead log header, including checksum. */
   400         -/* #define WAL_HDRSIZE 24 */
   401    413   #define WAL_HDRSIZE 32
   402    414   
   403    415   /* WAL magic value. Either this value, or the same value with the least
   404    416   ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
   405    417   ** big-endian format in the first 4 bytes of a WAL file.
   406    418   **
   407    419   ** If the LSB is set, then the checksums for each frame within the WAL
................................................................................
  2138   2150   ** recovery) return a positive error code.
  2139   2151   **
  2140   2152   ** The useWal parameter is true to force the use of the WAL and disable
  2141   2153   ** the case where the WAL is bypassed because it has been completely
  2142   2154   ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr() 
  2143   2155   ** to make a copy of the wal-index header into pWal->hdr.  If the 
  2144   2156   ** wal-index header has changed, *pChanged is set to 1 (as an indication 
  2145         -** to the caller that the local paget cache is obsolete and needs to be 
         2157  +** to the caller that the local page cache is obsolete and needs to be 
  2146   2158   ** flushed.)  When useWal==1, the wal-index header is assumed to already
  2147   2159   ** be loaded and the pChanged parameter is unused.
  2148   2160   **
  2149   2161   ** The caller must set the cnt parameter to the number of prior calls to
  2150   2162   ** this routine during the current read attempt that returned WAL_RETRY.
  2151   2163   ** This routine will start taking more aggressive measures to clear the
  2152   2164   ** race conditions after multiple WAL_RETRY returns, and after an excessive

Changes to src/walker.c.

   104    104   */
   105    105   int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
   106    106     SrcList *pSrc;
   107    107     int i;
   108    108     struct SrcList_item *pItem;
   109    109   
   110    110     pSrc = p->pSrc;
   111         -  if( ALWAYS(pSrc) ){
   112         -    for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
   113         -      if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
   114         -        return WRC_Abort;
   115         -      }
   116         -      if( pItem->fg.isTabFunc
   117         -       && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
   118         -      ){
   119         -        return WRC_Abort;
   120         -      }
          111  +  assert( pSrc!=0 );
          112  +  for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
          113  +    if( pItem->pSelect && sqlite3WalkSelect(pWalker, pItem->pSelect) ){
          114  +      return WRC_Abort;
          115  +    }
          116  +    if( pItem->fg.isTabFunc
          117  +     && sqlite3WalkExprList(pWalker, pItem->u1.pFuncArg)
          118  +    ){
          119  +      return WRC_Abort;
   121    120       }
   122    121     }
   123    122     return WRC_Continue;
   124    123   } 
   125    124   
   126    125   /*
   127    126   ** Call sqlite3WalkExpr() for every expression in Select statement p.

Changes to src/where.c.

  1859   1859     sqlite3DbFreeNN(db, p);
  1860   1860   }
  1861   1861   
  1862   1862   /*
  1863   1863   ** Free a WhereInfo structure
  1864   1864   */
  1865   1865   static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
  1866         -  if( ALWAYS(pWInfo) ){
  1867         -    int i;
  1868         -    for(i=0; i<pWInfo->nLevel; i++){
  1869         -      WhereLevel *pLevel = &pWInfo->a[i];
  1870         -      if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
  1871         -        sqlite3DbFree(db, pLevel->u.in.aInLoop);
  1872         -      }
         1866  +  int i;
         1867  +  assert( pWInfo!=0 );
         1868  +  for(i=0; i<pWInfo->nLevel; i++){
         1869  +    WhereLevel *pLevel = &pWInfo->a[i];
         1870  +    if( pLevel->pWLoop && (pLevel->pWLoop->wsFlags & WHERE_IN_ABLE) ){
         1871  +      sqlite3DbFree(db, pLevel->u.in.aInLoop);
  1873   1872       }
  1874         -    sqlite3WhereClauseClear(&pWInfo->sWC);
  1875         -    while( pWInfo->pLoops ){
  1876         -      WhereLoop *p = pWInfo->pLoops;
  1877         -      pWInfo->pLoops = p->pNextLoop;
  1878         -      whereLoopDelete(db, p);
  1879         -    }
  1880         -    sqlite3DbFreeNN(db, pWInfo);
  1881   1873     }
         1874  +  sqlite3WhereClauseClear(&pWInfo->sWC);
         1875  +  while( pWInfo->pLoops ){
         1876  +    WhereLoop *p = pWInfo->pLoops;
         1877  +    pWInfo->pLoops = p->pNextLoop;
         1878  +    whereLoopDelete(db, p);
         1879  +  }
         1880  +  sqlite3DbFreeNN(db, pWInfo);
  1882   1881   }
  1883   1882   
  1884   1883   /*
  1885   1884   ** Return TRUE if all of the following are true:
  1886   1885   **
  1887   1886   **   (1)  X has the same or lower cost that Y
  1888   1887   **   (2)  X uses fewer WHERE clause terms than Y

Changes to src/wherecode.c.

   290    290   ** term was originally TERM_LIKE, then the parent gets TERM_LIKECOND instead.
   291    291   ** The TERM_LIKECOND marking indicates that the term should be coded inside
   292    292   ** a conditional such that is only evaluated on the second pass of a
   293    293   ** LIKE-optimization loop, when scanning BLOBs instead of strings.
   294    294   */
   295    295   static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
   296    296     int nLoop = 0;
   297         -  while( ALWAYS(pTerm!=0)
   298         -      && (pTerm->wtFlags & TERM_CODED)==0
          297  +  assert( pTerm!=0 );
          298  +  while( (pTerm->wtFlags & TERM_CODED)==0
   299    299         && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
   300    300         && (pLevel->notReady & pTerm->prereqAll)==0
   301    301     ){
   302    302       if( nLoop && (pTerm->wtFlags & TERM_LIKE)!=0 ){
   303    303         pTerm->wtFlags |= TERM_LIKECOND;
   304    304       }else{
   305    305         pTerm->wtFlags |= TERM_CODED;
   306    306       }
   307    307       if( pTerm->iParent<0 ) break;
   308    308       pTerm = &pTerm->pWC->a[pTerm->iParent];
          309  +    assert( pTerm!=0 );
   309    310       pTerm->nChild--;
   310    311       if( pTerm->nChild!=0 ) break;
   311    312       nLoop++;
   312    313     }
   313    314   }
   314    315   
   315    316   /*

Changes to test/dbpage.test.

    52     52   do_execsql_test 160 {
    53     53     ATTACH ':memory:' AS aux1;
    54     54     PRAGMA aux1.page_size=4096;
    55     55     CREATE TABLE aux1.t2(a,b,c);
    56     56     INSERT INTO t2 VALUES(11,12,13);
    57     57     SELECT pgno, quote(substr(data,1,5)) FROM sqlite_dbpage('aux1');
    58     58   } {1 X'53514C6974' 2 X'0D00000001'}
           59  +do_execsql_test 170 {
           60  +  CREATE TABLE aux1.x3(x,y,z);
           61  +  INSERT INTO x3(x,y,z) VALUES(1,'main',1),(2,'aux1',1);
           62  +  SELECT pgno, schema, substr(data,1,6)
           63  +    FROM sqlite_dbpage, x3
           64  +   WHERE sqlite_dbpage.schema=x3.y AND sqlite_dbpage.pgno=x3.z
           65  +   ORDER BY x3.x;
           66  +} {1 main SQLite 1 aux1 SQLite}
    59     67   
    60     68   do_execsql_test 200 {
    61     69     CREATE TEMP TABLE saved_content(x);
    62     70     INSERT INTO saved_content(x) SELECT data FROM sqlite_dbpage WHERE pgno=4;
    63     71     UPDATE sqlite_dbpage SET data=zeroblob(4096) WHERE pgno=4;
    64     72   } {}
    65     73   do_catchsql_test 210 {

Changes to test/json101.test.

   717    717     /* } */
   718    718   } {1}
   719    719   do_execsql_test json-11.3 {
   720    720     /* Too deep by one { */
   721    721     SELECT json_valid(replace(printf('%.2001c0%.2001c','[','}'),'[','{"a":'));
   722    722     /* } */
   723    723   } {0}
          724  +
          725  +# 2017-10-27.  Demonstrate the ability to access an element from
          726  +# a json structure even though the element name constains a "."
          727  +# character, by quoting the element name in the path.
          728  +#
          729  +do_execsql_test json-12.100 {
          730  +  CREATE TABLE t12(x);
          731  +  INSERT INTO t12(x) VALUES(
          732  +    '{"settings":
          733  +        {"layer2":
          734  +           {"hapax.legomenon":
          735  +              {"forceDisplay":true,
          736  +               "transliterate":true,
          737  +               "add.footnote":true,
          738  +               "summary.report":true},
          739  +            "dis.legomenon":
          740  +              {"forceDisplay":true,
          741  +               "transliterate":false,
          742  +               "add.footnote":false,
          743  +               "summary.report":true},
          744  +            "tris.legomenon":
          745  +              {"forceDisplay":true,
          746  +               "transliterate":false,
          747  +               "add.footnote":false,
          748  +               "summary.report":false}
          749  +           }
          750  +        }
          751  +     }');
          752  +} {}
          753  +do_execsql_test json-12.110 {
          754  +  SELECT json_remove(x, '$.settings.layer2."dis.legomenon".forceDisplay')
          755  +    FROM t12;
          756  +} {{{"settings":{"layer2":{"hapax.legomenon":{"forceDisplay":true,"transliterate":true,"add.footnote":true,"summary.report":true},"dis.legomenon":{"transliterate":false,"add.footnote":false,"summary.report":true},"tris.legomenon":{"forceDisplay":true,"transliterate":false,"add.footnote":false,"summary.report":false}}}}}}
          757  +do_execsql_test json-12.120 {
          758  +  SELECT json_extract(x, '$.settings.layer2."tris.legomenon"."summary.report"')
          759  +    FROM t12;
          760  +} {0}
          761  +
          762  +
          763  +
   724    764   
   725    765   finish_test

Changes to test/minmax2.test.

   379    379   }
   380    380   do_test minmax2-10.12 {
   381    381     execsql {
   382    382       SELECT min(x), max(x) FROM t6;
   383    383     }
   384    384   } {{} {}}
   385    385   
          386  +# 2017-10-26.  Extend the min/max optimization to indexes on expressions
          387  +#
          388  +do_execsql_test minmax2-11.100 {
          389  +  CREATE TABLE t11(a,b,c);
          390  +  INSERT INTO t11(a,b,c) VALUES(1,10,5),(2,8,11),(3,1,4),(4,20,1),(5,16,4);
          391  +  CREATE INDEX t11bc ON t11(b+c);
          392  +  SELECT max(b+c) FROM t11;
          393  +} {21}
          394  +do_execsql_test minmax2-11.110 {
          395  +  SELECT a, max(b+c) FROM t11;
          396  +} {4 21}
          397  +do_test minmax2-11.111 {
          398  +  db eval {SELECT max(b+c) FROM t11}
          399  +  db status step
          400  +} {0}
          401  +do_test minmax2-11.112 {
          402  +  db eval {SELECT max(c+b) FROM t11}
          403  +  db status step
          404  +} {4}
          405  +do_execsql_test minmax2-11.120 {
          406  +  SELECT a, min(b+c) FROM t11;
          407  +} {3 5}
          408  +do_test minmax2-11.121 {
          409  +  db eval {SELECT min(b+c) FROM t11}
          410  +  db status step
          411  +} {0}
          412  +do_test minmax2-11.122 {
          413  +  db eval {SELECT min(c+b) FROM t11}
          414  +  db status step
          415  +} {4}
          416  +do_execsql_test minmax2-11.130 {
          417  +  INSERT INTO t11(a,b,c) VALUES(6,NULL,0),(7,0,NULL);
          418  +  SELECT a, min(b+c) FROM t11;
          419  +} {3 5}
   386    420   
   387    421   finish_test

Changes to test/with1.test.

   999    999     WITH
  1000   1000       x1(a) AS (values(100))
  1001   1001     INSERT INTO t1(x)
  1002   1002       SELECT * FROM (WITH x2(y) AS (SELECT * FROM x1) SELECT y+a FROM x1, x2);
  1003   1003     SELECT * FROM t1;
  1004   1004   } {0 0 0 {SCAN SUBQUERY 1} 0 1 1 {SCAN SUBQUERY 1}}
  1005   1005   
  1006         -
         1006  +# 2017-10-28.
         1007  +# See check-in https://sqlite.org/src/info/0926df095faf72c2
         1008  +# Tried to optimize co-routine processing by changing a Copy opcode
         1009  +# into SCopy.  But OSSFuzz found two (similar) cases where that optimization
         1010  +# does not work.
         1011  +#
         1012  +do_execsql_test 20.1 {
         1013  +  WITH c(i)AS(VALUES(9)UNION SELECT~i FROM c)SELECT max(5)>i fROM c;
         1014  +} {0}
         1015  +do_execsql_test 20.2 {
         1016  +  WITH c(i)AS(VALUES(5)UNIoN SELECT 0)SELECT min(1)-i fROM c;
         1017  +} {1}
  1007   1018   
  1008   1019   finish_test

Added tool/showshm.c.

            1  +/*
            2  +** A utility for printing content from the wal-index or "shm" file.
            3  +*/
            4  +#include <stdio.h>
            5  +#include <ctype.h>
            6  +#include <sys/types.h>
            7  +#include <sys/stat.h>
            8  +#include <fcntl.h>
            9  +#include <assert.h>
           10  +
           11  +#define ISDIGIT(X)  isdigit((unsigned char)(X))
           12  +#define ISPRINT(X)  isprint((unsigned char)(X))
           13  +
           14  +#if !defined(_MSC_VER)
           15  +#include <unistd.h>
           16  +#include <sys/types.h>
           17  +#else
           18  +#include <io.h>
           19  +#endif
           20  +
           21  +#include <stdlib.h>
           22  +#include <string.h>
           23  +
           24  +static int fd = -1;             /* The open SHM file */
           25  +
           26  +/* Report an out-of-memory error and die.
           27  +*/
           28  +static void out_of_memory(void){
           29  +  fprintf(stderr,"Out of memory...\n");
           30  +  exit(1);
           31  +}
           32  +
           33  +/*
           34  +** Read content from the file.
           35  +**
           36  +** Space to hold the content is obtained from malloc() and needs to be
           37  +** freed by the caller.
           38  +*/
           39  +static unsigned char *getContent(int ofst, int nByte){
           40  +  unsigned char *aData;
           41  +  aData = malloc(nByte);
           42  +  if( aData==0 ) out_of_memory();
           43  +  lseek(fd, ofst, SEEK_SET);
           44  +  read(fd, aData, nByte);
           45  +  return aData;
           46  +}
           47  +
           48  +/*
           49  +** Flags values
           50  +*/
           51  +#define FG_HEX     1    /* Show as hex */
           52  +#define FG_NBO     2    /* Native byte order */
           53  +#define FG_PGSZ    4    /* Show as page-size */
           54  +
           55  +/* Print a line of decode output showing a 4-byte integer.
           56  +*/
           57  +static void print_decode_line(
           58  +  unsigned char *aData,      /* Content being decoded */
           59  +  int ofst, int nByte,       /* Start and size of decode */
           60  +  unsigned flg,              /* Display flags */
           61  +  const char *zMsg           /* Message to append */
           62  +){
           63  +  int i, j;
           64  +  int val = aData[ofst];
           65  +  char zBuf[100];
           66  +  sprintf(zBuf, " %03x: %02x", ofst, aData[ofst]);
           67  +  i = (int)strlen(zBuf);
           68  +  for(j=1; j<4; j++){
           69  +    if( j>=nByte ){
           70  +      sprintf(&zBuf[i], "   ");
           71  +    }else{
           72  +      sprintf(&zBuf[i], " %02x", aData[ofst+j]);
           73  +      val = val*256 + aData[ofst+j];
           74  +    }
           75  +    i += (int)strlen(&zBuf[i]);
           76  +  }
           77  +  if( nByte==8 ){
           78  +    for(j=4; j<8; j++){
           79  +      sprintf(&zBuf[i], " %02x", aData[ofst+j]);
           80  +      i += (int)strlen(&zBuf[i]);
           81  +    }
           82  +  }
           83  +  if( flg & FG_NBO ){
           84  +    assert( nByte==4 );
           85  +    memcpy(&val, aData+ofst, 4);
           86  +  }
           87  +  sprintf(&zBuf[i], "            ");
           88  +  i += 12;
           89  +  if( flg & FG_PGSZ ){
           90  +    unsigned short sz;
           91  +    memcpy(&sz, aData+ofst, 2);
           92  +    sprintf(&zBuf[i], "   %9d", sz==1 ? 65536 : sz);
           93  +  }else if( flg & FG_HEX ){
           94  +    sprintf(&zBuf[i], "  0x%08x", val);
           95  +  }else if( nByte<8 ){
           96  +    sprintf(&zBuf[i], "   %9d", val);
           97  +  }
           98  +  printf("%s  %s\n", zBuf, zMsg);
           99  +}
          100  +
          101  +/*
          102  +** Print an instance of the WalIndexHdr object.  ix is either 0 or 1
          103  +** to select which header to print.
          104  +*/
          105  +static void print_index_hdr(unsigned char *aData, int ix){
          106  +  int i;
          107  +  assert( ix==0 || ix==1 );
          108  +  i = ix ? 48 : 0;
          109  +  print_decode_line(aData, 0+i, 4, FG_NBO,  "Wal-index version");
          110  +  print_decode_line(aData, 4+i, 4, 0,       "unused padding");
          111  +  print_decode_line(aData, 8+i, 4, FG_NBO,  "transaction counter");
          112  +  print_decode_line(aData,12+i, 1, 0,       "1 when initialized");
          113  +  print_decode_line(aData,13+i, 1, 0,       "true if WAL cksums are bigendian");
          114  +  print_decode_line(aData,14+i, 2, FG_PGSZ, "database page size");
          115  +  print_decode_line(aData,16+i, 4, FG_NBO,  "mxFrame");
          116  +  print_decode_line(aData,20+i, 4, FG_NBO,  "Size of database in pages");
          117  +  print_decode_line(aData,24+i, 8, 0, "Cksum of last frame in -wal");
          118  +  print_decode_line(aData,32+i, 8, 0,  "Salt values from the -wal");
          119  +  print_decode_line(aData,40+i, 8, 0,  "Cksum over all prior fields");
          120  +}
          121  +
          122  +/*
          123  +** Print the WalCkptInfo object
          124  +*/
          125  +static void print_ckpt_info(unsigned char *aData){
          126  +  const int i = 96;
          127  +  int j;
          128  +  print_decode_line(aData, 0+i, 4, FG_NBO,  "nBackfill");
          129  +  for(j=0; j<5; j++){
          130  +    char zLabel[100];
          131  +    sprintf(zLabel, "aReadMark[%d]", j);
          132  +    print_decode_line(aData, 4*j+4+i, 4, FG_NBO, zLabel);
          133  +  }
          134  +  print_decode_line(aData,24+i, 8, 0,       "aLock");
          135  +  print_decode_line(aData,32+i, 4, FG_NBO,  "nBackfillAttempted");
          136  +  print_decode_line(aData,36+i, 4, FG_NBO,  "notUsed0");
          137  +}
          138  +
          139  +
          140  +int main(int argc, char **argv){
          141  +  unsigned char *aData;
          142  +  if( argc<2 ){
          143  +    fprintf(stderr,"Usage: %s FILENAME\n", argv[0]);
          144  +    exit(1);
          145  +  }
          146  +  fd = open(argv[1], O_RDONLY);
          147  +  if( fd<0 ){
          148  +    fprintf(stderr,"%s: can't open %s\n", argv[0], argv[1]);
          149  +    exit(1);
          150  +  }
          151  +  aData = getContent(0, 136);
          152  +  print_index_hdr(aData, 0);
          153  +  print_index_hdr(aData, 1);
          154  +  print_ckpt_info(aData);
          155  +  free(aData);
          156  +  close(fd);
          157  +  return 0;
          158  +}

Changes to tool/spaceanal.tcl.

     1         -# Run this TCL script using "testfixture" in order get a report that shows
     2         -# how much disk space is used by a particular data to actually store data
            1  +# Run this TCL script using an SQLite-enabled TCL interpreter to get a report
            2  +# on how much disk space is used by a particular data to actually store data
     3      3   # versus how much space is unused.
            4  +#
            5  +# The dbstat virtual table is required.
     4      6   #
     5      7   
     6      8   if {[catch {
     7      9   
     8     10   # Argument $tname is the name of a table within the database opened by
     9     11   # database handle [db]. Return true if it is a WITHOUT ROWID table, or
    10     12   # false otherwise.
................................................................................
   142    144     puts stderr "error trying to open $file_to_analyze: $msg"
   143    145     exit 1
   144    146   }
   145    147   if {$flags(-debug)} {
   146    148     proc dbtrace {txt} {puts $txt; flush stdout;}
   147    149     db trace ::dbtrace
   148    150   }
          151  +
          152  +# Make sure all required compile-time options are available
          153  +#
          154  +if {![db exists {SELECT 1 FROM pragma_compile_options
          155  +                WHERE compile_options='ENABLE_DBSTAT_VTAB'}]} {
          156  +  puts "The SQLite database engine linked with this application\
          157  +        lacks required capabilities. Recompile using the\
          158  +        -DSQLITE_ENABLE_DBSTAT_VTAB compile-time option to fix\
          159  +        this problem."
          160  +  exit 1
          161  +}
   149    162   
   150    163   db eval {SELECT count(*) FROM sqlite_master}
   151    164   set pageSize [expr {wide([db one {PRAGMA page_size}])}]
   152    165   
   153    166   if {$flags(-pageinfo)} {
   154    167     db eval {CREATE VIRTUAL TABLE temp.stat USING dbstat}
   155    168     db eval {SELECT name, path, pageno FROM temp.stat ORDER BY pageno} {

Changes to tool/sqlite3_analyzer.c.in.

    10     10   #define SQLITE_OMIT_DECLTYPE 1
    11     11   #define SQLITE_OMIT_DEPRECATED 1
    12     12   #define SQLITE_OMIT_PROGRESS_CALLBACK 1
    13     13   #define SQLITE_OMIT_SHARED_CACHE 1
    14     14   #define SQLITE_DEFAULT_MEMSTATUS 0
    15     15   #define SQLITE_MAX_EXPR_DEPTH 0
    16     16   #define SQLITE_OMIT_LOAD_EXTENSION 1
           17  +#ifndef USE_EXTERNAL_SQLITE
    17     18   INCLUDE sqlite3.c
           19  +#endif
    18     20   INCLUDE $ROOT/src/tclsqlite.c
    19     21   
    20     22   const char *sqlite3_analyzer_init_proc(Tcl_Interp *interp){
    21     23     (void)interp;
    22     24     return
    23     25   BEGIN_STRING
    24     26   INCLUDE $ROOT/tool/spaceanal.tcl
    25     27   END_STRING
    26     28   ;
    27     29   }