/ Check-in [7f1e4e4b]
Login

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

Overview
Comment:Merge latest changes from trunk into this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | reuse-schema
Files: files | file ages | folders
SHA3-256: 7f1e4e4b99644f986aa68bd40afddc4886145972eae1106347185eeff52150d6
User & Date: dan 2019-07-26 20:54:05
Wiki:reuse-schema
Context
2019-08-06
11:45
Add an assert() to the code on this branch. check-in: fdd44bbb user: dan tags: reuse-schema
2019-07-26
20:54
Merge latest changes from trunk into this branch. check-in: 7f1e4e4b user: dan tags: reuse-schema
20:33
Update RBU so that it supports indexes on expressions. check-in: 6bfa44da user: dan tags: trunk
2019-07-18
19:50
Add a ctime.c entry for SQLITE_ENABLE_SHARED_SCHEMA. check-in: 892ac945 user: drh tags: reuse-schema
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to VERSION.

     1         -3.29.0
            1  +3.30.0

Changes to configure.

     1      1   #! /bin/sh
     2      2   # Guess values for system-dependent variables and create Makefiles.
     3         -# Generated by GNU Autoconf 2.69 for sqlite 3.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/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   

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

  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   9162   } {0 {}}
         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  +} {1 {database disk image is malformed}}
         9361  +
         9362  +do_catchsql_test 63.2 {
         9363  +  INSERT INTO t1(t1) VALUES('optimize');
         9364  +} {0 {}}
         9365  +
         9366  +breakpoint
         9367  +do_catchsql_test 63.3 {
         9368  +  SELECT * FROM t1 WHERE b MATCH 'thead*thead*theSt*';
         9369  +} {0 {}}
  9163   9370   
  9164   9371   sqlite3_fts5_may_be_corrupt 0
  9165   9372   finish_test
  9166   9373   

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

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/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){
................................................................................
  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 ){
................................................................................
  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   1700       const char *zCol;
  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

Changes to ext/rtree/rtree.c.

  1560   1560     int nConstraint = pCur->nConstraint;
  1561   1561     int ii;
  1562   1562     int eInt;
  1563   1563     RtreeSearchPoint x;
  1564   1564   
  1565   1565     eInt = pRtree->eCoordType==RTREE_COORD_INT32;
  1566   1566     while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
         1567  +    u8 *pCellData;
  1567   1568       pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
  1568   1569       if( rc ) return rc;
  1569   1570       nCell = NCELL(pNode);
  1570   1571       assert( nCell<200 );
         1572  +    pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
  1571   1573       while( p->iCell<nCell ){
  1572   1574         sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
  1573         -      u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
  1574   1575         eWithin = FULLY_WITHIN;
  1575   1576         for(ii=0; ii<nConstraint; ii++){
  1576   1577           RtreeConstraint *pConstraint = pCur->aConstraint + ii;
  1577   1578           if( pConstraint->op>=RTREE_MATCH ){
  1578   1579             rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
  1579   1580                                          &rScore, &eWithin);
  1580   1581             if( rc ) return rc;
  1581   1582           }else if( p->iLevel==1 ){
  1582   1583             rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
  1583   1584           }else{
  1584   1585             rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
  1585   1586           }
  1586         -        if( eWithin==NOT_WITHIN ) break;
         1587  +        if( eWithin==NOT_WITHIN ){
         1588  +          p->iCell++;
         1589  +          pCellData += pRtree->nBytesPerCell;
         1590  +          break;
         1591  +        }
  1587   1592         }
  1588         -      p->iCell++;
  1589   1593         if( eWithin==NOT_WITHIN ) continue;
         1594  +      p->iCell++;
  1590   1595         x.iLevel = p->iLevel - 1;
  1591   1596         if( x.iLevel ){
  1592   1597           x.id = readInt64(pCellData);
         1598  +        for(ii=0; ii<pCur->nPoint; ii++){
         1599  +          if( pCur->aPoint[ii].id==x.id ){
         1600  +            RTREE_IS_CORRUPT(pRtree);
         1601  +            return SQLITE_CORRUPT_VTAB;
         1602  +          }
         1603  +        }
  1593   1604           x.iCell = 0;
  1594   1605         }else{
  1595   1606           x.id = p->id;
  1596   1607           x.iCell = p->iCell - 1;
  1597   1608         }
  1598   1609         if( p->iCell>=nCell ){
  1599   1610           RTREE_QUEUE_TRACE(pCur, "POP-S:");

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.

  6660   6660     assert( *pRC==SQLITE_OK );
  6661   6661     assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
  6662   6662     assert( MX_CELL(pPage->pBt)<=10921 );
  6663   6663     assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB );
  6664   6664     assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
  6665   6665     assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
  6666   6666     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
  6667         -  /* The cell should normally be sized correctly.  However, when moving a
  6668         -  ** malformed cell from a leaf page to an interior page, if the cell size
  6669         -  ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
  6670         -  ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
  6671         -  ** the term after the || in the following assert(). */
  6672         -  assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
         6667  +  assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB );
  6673   6668     assert( pPage->nFree>=0 );
  6674   6669     if( pPage->nOverflow || sz+2>pPage->nFree ){
  6675   6670       if( pTemp ){
  6676   6671         memcpy(pTemp, pCell, sz);
  6677   6672         pCell = pTemp;
  6678   6673       }
  6679   6674       if( iChild ){
................................................................................
  7958   7953       nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
  7959   7954       nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0,
  7960   7955       nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0,
  7961   7956       nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0
  7962   7957     ));
  7963   7958   
  7964   7959     assert( sqlite3PagerIswriteable(pParent->pDbPage) );
         7960  +  assert( nNew>=1 && nNew<=ArraySize(apNew) );
         7961  +  assert( apNew[nNew-1]!=0 );
  7965   7962     put4byte(pRight, apNew[nNew-1]->pgno);
  7966   7963   
  7967   7964     /* If the sibling pages are not leaves, ensure that the right-child pointer
  7968   7965     ** of the right-most new sibling page is set to the value that was 
  7969   7966     ** originally in the same field of the right-most old sibling page. */
  7970   7967     if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){
  7971   7968       MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
................................................................................
  8303   8300     u8 aBalanceQuickSpace[13];
  8304   8301     u8 *pFree = 0;
  8305   8302   
  8306   8303     VVA_ONLY( int balance_quick_called = 0 );
  8307   8304     VVA_ONLY( int balance_deeper_called = 0 );
  8308   8305   
  8309   8306     do {
  8310         -    int iPage = pCur->iPage;
         8307  +    int iPage;
  8311   8308       MemPage *pPage = pCur->pPage;
  8312   8309   
  8313   8310       if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
  8314         -    if( iPage==0 ){
         8311  +    if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
         8312  +      break;
         8313  +    }else if( (iPage = pCur->iPage)==0 ){
  8315   8314         if( pPage->nOverflow ){
  8316   8315           /* The root page of the b-tree is overfull. In this case call the
  8317   8316           ** balance_deeper() function to create a new child for the root-page
  8318   8317           ** and copy the current contents of the root-page to it. The
  8319   8318           ** next iteration of the do-loop will balance the child page.
  8320   8319           */ 
  8321   8320           assert( balance_deeper_called==0 );
................................................................................
  8328   8327             pCur->apPage[0] = pPage;
  8329   8328             pCur->pPage = pCur->apPage[1];
  8330   8329             assert( pCur->pPage->nOverflow );
  8331   8330           }
  8332   8331         }else{
  8333   8332           break;
  8334   8333         }
  8335         -    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
  8336         -      break;
  8337   8334       }else{
  8338   8335         MemPage * const pParent = pCur->apPage[iPage-1];
  8339   8336         int const iIdx = pCur->aiIdx[iPage-1];
  8340   8337   
  8341   8338         rc = sqlite3PagerWrite(pParent->pDbPage);
  8342   8339         if( rc==SQLITE_OK && pParent->nFree<0 ){
  8343   8340           rc = btreeComputeFreeSpace(pParent);
................................................................................
  8725   8722         ** doing that is no faster then skipping this optimization and just
  8726   8723         ** calling dropCell() and insertCell(). 
  8727   8724         **
  8728   8725         ** This optimization cannot be used on an autovacuum database if the
  8729   8726         ** new entry uses overflow pages, as the insertCell() call below is
  8730   8727         ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry.  */
  8731   8728         assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */
  8732         -      if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
         8729  +      if( oldCell < pPage->aData+pPage->hdrOffset+10 ){
         8730  +        return SQLITE_CORRUPT_BKPT;
         8731  +      }
         8732  +      if( oldCell+szNew > pPage->aDataEnd ){
         8733  +        return SQLITE_CORRUPT_BKPT;
         8734  +      }
  8733   8735         memcpy(oldCell, newCell, szNew);
  8734   8736         return SQLITE_OK;
  8735   8737       }
  8736   8738       dropCell(pPage, idx, info.nSize, &rc);
  8737   8739       if( rc ) goto end_insert;
  8738   8740     }else if( loc<0 && pPage->nCell>0 ){
  8739   8741       assert( pPage->leaf );

Changes to src/build.c.

  1882   1882   **
  1883   1883   ** For virtual tables, only (1) is performed.
  1884   1884   */
  1885   1885   static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
  1886   1886     Index *pIdx;
  1887   1887     Index *pPk;
  1888   1888     int nPk;
         1889  +  int nExtra;
  1889   1890     int i, j;
  1890   1891     sqlite3 *db = pParse->db;
  1891   1892     Vdbe *v = pParse->pVdbe;
  1892   1893   
  1893   1894     /* Mark every PRIMARY KEY column as NOT NULL (except for imposter tables)
  1894   1895     */
  1895   1896     if( !db->init.imposterTable ){
................................................................................
  1924   1925       pList->a[0].sortOrder = pParse->iPkSortOrder;
  1925   1926       assert( pParse->pNewTable==pTab );
  1926   1927       pTab->iPKey = -1;
  1927   1928       sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0,
  1928   1929                          SQLITE_IDXTYPE_PRIMARYKEY);
  1929   1930       if( db->mallocFailed || pParse->nErr ) return;
  1930   1931       pPk = sqlite3PrimaryKeyIndex(pTab);
         1932  +    assert( pPk->nKeyCol==1 );
  1931   1933     }else{
  1932   1934       pPk = sqlite3PrimaryKeyIndex(pTab);
  1933   1935       assert( pPk!=0 );
  1934   1936   
  1935   1937       /*
  1936   1938       ** Remove all redundant columns from the PRIMARY KEY.  For example, change
  1937   1939       ** "PRIMARY KEY(a,b,a,b,c,b,c,d)" into just "PRIMARY KEY(a,b,c,d)".  Later
................................................................................
  1938   1940       ** code assumes the PRIMARY KEY contains no repeated columns.
  1939   1941       */
  1940   1942       for(i=j=1; i<pPk->nKeyCol; i++){
  1941   1943         if( isDupColumn(pPk, j, pPk, i) ){
  1942   1944           pPk->nColumn--;
  1943   1945         }else{
  1944   1946           testcase( hasColumn(pPk->aiColumn, j, pPk->aiColumn[i]) );
         1947  +        pPk->azColl[j] = pPk->azColl[i];
         1948  +        pPk->aSortOrder[j] = pPk->aSortOrder[i];
  1945   1949           pPk->aiColumn[j++] = pPk->aiColumn[i];
  1946   1950         }
  1947   1951       }
  1948   1952       pPk->nKeyCol = j;
  1949   1953     }
  1950   1954     assert( pPk!=0 );
  1951   1955     pPk->isCovering = 1;
  1952   1956     if( !db->init.imposterTable ) pPk->uniqNotNull = 1;
  1953         -  nPk = pPk->nKeyCol;
         1957  +  nPk = pPk->nColumn = pPk->nKeyCol;
  1954   1958   
  1955   1959     /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
  1956   1960     ** table entry. This is only required if currently generating VDBE
  1957   1961     ** code for a CREATE TABLE (not when parsing one as part of reading
  1958   1962     ** a database schema).  */
  1959   1963     if( v && pPk->tnum>0 ){
  1960   1964       assert( db->init.busy==0 );
................................................................................
  1996   2000       }
  1997   2001       assert( pIdx->nColumn>=pIdx->nKeyCol+n );
  1998   2002       assert( pIdx->nColumn>=j );
  1999   2003     }
  2000   2004   
  2001   2005     /* Add all table columns to the PRIMARY KEY index
  2002   2006     */
  2003         -  if( nPk<pTab->nCol ){
  2004         -    if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
  2005         -    for(i=0, j=nPk; i<pTab->nCol; i++){
  2006         -      if( !hasColumn(pPk->aiColumn, j, i) ){
  2007         -        assert( j<pPk->nColumn );
  2008         -        pPk->aiColumn[j] = i;
  2009         -        pPk->azColl[j] = sqlite3StrBINARY;
  2010         -        j++;
  2011         -      }
         2007  +  nExtra = 0;
         2008  +  for(i=0; i<pTab->nCol; i++){
         2009  +    if( !hasColumn(pPk->aiColumn, nPk, i) ) nExtra++;
         2010  +  }
         2011  +  if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
         2012  +  for(i=0, j=nPk; i<pTab->nCol; i++){
         2013  +    if( !hasColumn(pPk->aiColumn, j, i) ){
         2014  +      assert( j<pPk->nColumn );
         2015  +      pPk->aiColumn[j] = i;
         2016  +      pPk->azColl[j] = sqlite3StrBINARY;
         2017  +      j++;
  2012   2018       }
  2013         -    assert( pPk->nColumn==j );
  2014         -    assert( pTab->nCol==j );
  2015         -  }else{
  2016         -    pPk->nColumn = pTab->nCol;
  2017   2019     }
         2020  +  assert( pPk->nColumn==j );
         2021  +  assert( pTab->nCol<=j );
  2018   2022     recomputeColumnsNotIndexed(pPk);
  2019   2023   }
  2020   2024   
  2021   2025   #ifndef SQLITE_OMIT_VIRTUALTABLE
  2022   2026   /*
  2023   2027   ** Return true if zName is a shadow table name in the current database
  2024   2028   ** connection.
................................................................................
  3681   3685         ** the Noop with a Goto to jump over the VDBE code generated below. */
  3682   3686         pIndex->tnum = sqlite3VdbeAddOp0(v, OP_Noop);
  3683   3687         sqlite3VdbeAddOp3(v, OP_CreateBtree, iDb, iMem, BTREE_BLOBKEY);
  3684   3688   
  3685   3689         /* Gather the complete text of the CREATE INDEX statement into
  3686   3690         ** the zStmt variable
  3687   3691         */
         3692  +      assert( pName!=0 || pStart==0 );
  3688   3693         if( pStart ){
  3689   3694           int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
  3690   3695           if( pName->z[n-1]==';' ) n--;
  3691   3696           /* A named index with an explicit CREATE INDEX statement */
  3692   3697           zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
  3693   3698               onError==OE_None ? "" : " UNIQUE", n, pName->z);
  3694   3699         }else{

Changes to src/expr.c.

  1036   1036     }
  1037   1037   #endif
  1038   1038     if( !ExprHasProperty(p, (EP_TokenOnly|EP_Leaf)) ){
  1039   1039       /* The Expr.x union is never used at the same time as Expr.pRight */
  1040   1040       assert( p->x.pList==0 || p->pRight==0 );
  1041   1041       if( p->pLeft && p->op!=TK_SELECT_COLUMN ) sqlite3ExprDeleteNN(db, p->pLeft);
  1042   1042       if( p->pRight ){
         1043  +      assert( !ExprHasProperty(p, EP_WinFunc) );
  1043   1044         sqlite3ExprDeleteNN(db, p->pRight);
  1044   1045       }else if( ExprHasProperty(p, EP_xIsSelect) ){
         1046  +      assert( !ExprHasProperty(p, EP_WinFunc) );
  1045   1047         sqlite3SelectDelete(db, p->x.pSelect);
  1046   1048       }else{
  1047   1049         sqlite3ExprListDelete(db, p->x.pList);
  1048         -    }
  1049         -    if( ExprHasProperty(p, EP_WinFunc) ){
  1050         -      assert( p->op==TK_FUNCTION );
  1051         -      sqlite3WindowDelete(db, p->y.pWin);
         1050  +#ifndef SQLITE_OMIT_WINDOWFUNC
         1051  +      if( ExprHasProperty(p, EP_WinFunc) ){
         1052  +        sqlite3WindowDelete(db, p->y.pWin);
         1053  +      }
         1054  +#endif
  1052   1055       }
  1053   1056     }
  1054   1057     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
  1055   1058     if( !ExprHasProperty(p, EP_Static) ){
  1056   1059       sqlite3DbFreeNN(db, p);
  1057   1060     }
  1058   1061   }
................................................................................
  1079   1082   */
  1080   1083   static int exprStructSize(Expr *p){
  1081   1084     if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
  1082   1085     if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
  1083   1086     return EXPR_FULLSIZE;
  1084   1087   }
  1085   1088   
  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   1089   /*
  1097   1090   ** The dupedExpr*Size() routines each return the number of bytes required
  1098   1091   ** to store a copy of an expression or expression tree.  They differ in
  1099   1092   ** how much of the tree is measured.
  1100   1093   **
  1101   1094   **     dupedExprStructSize()     Size of only the Expr structure 
  1102   1095   **     dupedExprNodeSize()       Size of Expr + space for token
................................................................................
  1328   1321   /*
  1329   1322   ** The gatherSelectWindows() procedure and its helper routine
  1330   1323   ** gatherSelectWindowsCallback() are used to scan all the expressions
  1331   1324   ** an a newly duplicated SELECT statement and gather all of the Window
  1332   1325   ** objects found there, assembling them onto the linked list at Select->pWin.
  1333   1326   */
  1334   1327   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;
         1328  +  if( pExpr->op==TK_FUNCTION && ExprHasProperty(pExpr, EP_WinFunc) ){
         1329  +    Select *pSelect = pWalker->u.pSelect;
         1330  +    Window *pWin = pExpr->y.pWin;
         1331  +    assert( pWin );
         1332  +    assert( IsWindowFunc(pExpr) );
         1333  +    assert( pWin->ppThis==0 );
         1334  +    if( pSelect->pWin ){
         1335  +      pSelect->pWin->ppThis = &pWin->pNextWin;
         1336  +    }
         1337  +    pWin->pNextWin = pSelect->pWin;
         1338  +    pWin->ppThis = &pSelect->pWin;
         1339  +    pSelect->pWin = pWin;
  1339   1340     }
  1340   1341     return WRC_Continue;
  1341   1342   }
  1342   1343   static int gatherSelectWindowsSelectCallback(Walker *pWalker, Select *p){
  1343   1344     return p==pWalker->u.pSelect ? WRC_Continue : WRC_Prune;
  1344   1345   }
  1345   1346   static void gatherSelectWindows(Select *p){
................................................................................
  4040   4041         int nExpr;                        /* 2x number of WHEN terms */
  4041   4042         int i;                            /* Loop counter */
  4042   4043         ExprList *pEList;                 /* List of WHEN terms */
  4043   4044         struct ExprList_item *aListelem;  /* Array of WHEN terms */
  4044   4045         Expr opCompare;                   /* The X==Ei expression */
  4045   4046         Expr *pX;                         /* The X expression */
  4046   4047         Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
         4048  +      Expr *pDel = 0;
         4049  +      sqlite3 *db = pParse->db;
  4047   4050   
  4048   4051         assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
  4049   4052         assert(pExpr->x.pList->nExpr > 0);
  4050   4053         pEList = pExpr->x.pList;
  4051   4054         aListelem = pEList->a;
  4052   4055         nExpr = pEList->nExpr;
  4053   4056         endLabel = sqlite3VdbeMakeLabel(pParse);
  4054   4057         if( (pX = pExpr->pLeft)!=0 ){
  4055         -        exprNodeCopy(&tempX, pX);
         4058  +        pDel = sqlite3ExprDup(db, pX, 0);
         4059  +        if( db->mallocFailed ){
         4060  +          sqlite3ExprDelete(db, pDel);
         4061  +          break;
         4062  +        }
  4056   4063           testcase( pX->op==TK_COLUMN );
  4057         -        exprToRegister(&tempX, exprCodeVector(pParse, &tempX, &regFree1));
         4064  +        exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
  4058   4065           testcase( regFree1==0 );
  4059   4066           memset(&opCompare, 0, sizeof(opCompare));
  4060   4067           opCompare.op = TK_EQ;
  4061         -        opCompare.pLeft = &tempX;
         4068  +        opCompare.pLeft = pDel;
  4062   4069           pTest = &opCompare;
  4063   4070           /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
  4064   4071           ** The value in regFree1 might get SCopy-ed into the file result.
  4065   4072           ** So make sure that the regFree1 register is not reused for other
  4066   4073           ** purposes and possibly overwritten.  */
  4067   4074           regFree1 = 0;
  4068   4075         }
................................................................................
  4082   4089           sqlite3VdbeResolveLabel(v, nextCase);
  4083   4090         }
  4084   4091         if( (nExpr&1)!=0 ){
  4085   4092           sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
  4086   4093         }else{
  4087   4094           sqlite3VdbeAddOp2(v, OP_Null, 0, target);
  4088   4095         }
         4096  +      sqlite3ExprDelete(db, pDel);
  4089   4097         sqlite3VdbeResolveLabel(v, endLabel);
  4090   4098         break;
  4091   4099       }
  4092   4100   #ifndef SQLITE_OMIT_TRIGGER
  4093   4101       case TK_RAISE: {
  4094   4102         assert( pExpr->affinity==OE_Rollback 
  4095   4103              || pExpr->affinity==OE_Abort
................................................................................
  4363   4371   static void exprCodeBetween(
  4364   4372     Parse *pParse,    /* Parsing and code generating context */
  4365   4373     Expr *pExpr,      /* The BETWEEN expression */
  4366   4374     int dest,         /* Jump destination or storage location */
  4367   4375     void (*xJump)(Parse*,Expr*,int,int), /* Action to take */
  4368   4376     int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
  4369   4377   ){
  4370         - Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
         4378  +  Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
  4371   4379     Expr compLeft;    /* The  x>=y  term */
  4372   4380     Expr compRight;   /* The  x<=z  term */
  4373         -  Expr exprX;       /* The  x  subexpression */
  4374   4381     int regFree1 = 0; /* Temporary use register */
         4382  +  Expr *pDel = 0;
         4383  +  sqlite3 *db = pParse->db;
  4375   4384   
  4376   4385     memset(&compLeft, 0, sizeof(Expr));
  4377   4386     memset(&compRight, 0, sizeof(Expr));
  4378   4387     memset(&exprAnd, 0, sizeof(Expr));
  4379   4388   
  4380   4389     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);
         4390  +  pDel = sqlite3ExprDup(db, pExpr->pLeft, 0);
         4391  +  if( db->mallocFailed==0 ){
         4392  +    exprAnd.op = TK_AND;
         4393  +    exprAnd.pLeft = &compLeft;
         4394  +    exprAnd.pRight = &compRight;
         4395  +    compLeft.op = TK_GE;
         4396  +    compLeft.pLeft = pDel;
         4397  +    compLeft.pRight = pExpr->x.pList->a[0].pExpr;
         4398  +    compRight.op = TK_LE;
         4399  +    compRight.pLeft = pDel;
         4400  +    compRight.pRight = pExpr->x.pList->a[1].pExpr;
         4401  +    exprToRegister(pDel, exprCodeVector(pParse, pDel, &regFree1));
         4402  +    if( xJump ){
         4403  +      xJump(pParse, &exprAnd, dest, jumpIfNull);
         4404  +    }else{
         4405  +      /* Mark the expression is being from the ON or USING clause of a join
         4406  +      ** so that the sqlite3ExprCodeTarget() routine will not attempt to move
         4407  +      ** it into the Parse.pConstExpr list.  We should use a new bit for this,
         4408  +      ** for clarity, but we are out of bits in the Expr.flags field so we
         4409  +      ** have to reuse the EP_FromJoin bit.  Bummer. */
         4410  +      pDel->flags |= EP_FromJoin;
         4411  +      sqlite3ExprCodeTarget(pParse, &exprAnd, dest);
         4412  +    }
         4413  +    sqlite3ReleaseTempReg(pParse, regFree1);
         4414  +  }
         4415  +  sqlite3ExprDelete(db, pDel);
  4404   4416   
  4405   4417     /* Ensure adequate test coverage */
  4406   4418     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1==0 );
  4407   4419     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull==0 && regFree1!=0 );
  4408   4420     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1==0 );
  4409   4421     testcase( xJump==sqlite3ExprIfTrue  && jumpIfNull!=0 && regFree1!=0 );
  4410   4422     testcase( xJump==sqlite3ExprIfFalse && jumpIfNull==0 && regFree1==0 );
................................................................................
  4835   4847       }
  4836   4848       if( pB->op==TK_COLLATE && sqlite3ExprCompare(pParse, pA,pB->pLeft,iTab)<2 ){
  4837   4849         return 1;
  4838   4850       }
  4839   4851       return 2;
  4840   4852     }
  4841   4853     if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){
  4842         -    if( pA->op==TK_FUNCTION ){
         4854  +    if( pA->op==TK_FUNCTION || pA->op==TK_AGG_FUNCTION ){
  4843   4855         if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
  4844   4856   #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) );
         4857  +      assert( pA->op==pB->op );
         4858  +      if( ExprHasProperty(pA,EP_WinFunc)!=ExprHasProperty(pB,EP_WinFunc) ){
         4859  +        return 2;
         4860  +      }
  4854   4861         if( ExprHasProperty(pA,EP_WinFunc) ){
  4855         -        if( sqlite3WindowCompare(pParse,pA->y.pWin,pB->y.pWin)!=0 ) return 2;
         4862  +        if( sqlite3WindowCompare(pParse, pA->y.pWin, pB->y.pWin, 1)!=0 ){
         4863  +          return 2;
         4864  +        }
  4856   4865         }
  4857   4866   #endif
  4858   4867       }else if( pA->op==TK_NULL ){
  4859   4868         return 0;
  4860   4869       }else if( pA->op==TK_COLLATE ){
  4861   4870         if( sqlite3_stricmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2;
  4862   4871       }else if( ALWAYS(pB->u.zToken!=0) && strcmp(pA->u.zToken,pB->u.zToken)!=0 ){

Changes to src/fkey.c.

  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/os_unix.c.

   517    517     { "lstat",         (sqlite3_syscall_ptr)0,              0 },
   518    518   #endif
   519    519   #define osLstat      ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
   520    520   
   521    521   #if defined(__linux__) && defined(SQLITE_ENABLE_BATCH_ATOMIC_WRITE)
   522    522   # ifdef __ANDROID__
   523    523     { "ioctl", (sqlite3_syscall_ptr)(int(*)(int, int, ...))ioctl, 0 },
          524  +#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
   524    525   # else
   525    526     { "ioctl",         (sqlite3_syscall_ptr)ioctl,          0 },
          527  +#define osIoctl ((int(*)(int,unsigned long,...))aSyscall[28].pCurrent)
   526    528   # endif
   527    529   #else
   528    530     { "ioctl",         (sqlite3_syscall_ptr)0,              0 },
   529    531   #endif
   530         -#define osIoctl ((int(*)(int,int,...))aSyscall[28].pCurrent)
   531    532   
   532    533   }; /* End of the overrideable system calls */
   533    534   
   534    535   
   535    536   /*
   536    537   ** On some systems, calls to fchown() will trigger a message in a security
   537    538   ** log if they come from non-root processes.  So avoid calling fchown() if
................................................................................
  7599   7600         }
  7600   7601         return rc;
  7601   7602       }
  7602   7603       default: {
  7603   7604         assert( 0 );  /* The call assures that only valid opcodes are sent */
  7604   7605       }
  7605   7606     }
  7606         -  /*NOTREACHED*/
         7607  +  /*NOTREACHED*/ assert(0);
  7607   7608     return SQLITE_ERROR;
  7608   7609   }
  7609   7610   
  7610   7611   /*
  7611   7612   ** Within this division (the proxying locking implementation) the procedures
  7612   7613   ** above this point are all utilities.  The lock-related methods of the
  7613   7614   ** 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.

   454    454   %include {
   455    455     /*
   456    456     ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
   457    457     ** all elements in the list.  And make sure list length does not exceed
   458    458     ** SQLITE_LIMIT_COMPOUND_SELECT.
   459    459     */
   460    460     static void parserDoubleLinkSelect(Parse *pParse, Select *p){
          461  +    assert( p!=0 );
   461    462       if( p->pPrior ){
   462    463         Select *pNext = 0, *pLoop;
   463    464         int mxSelect, cnt = 0;
   464    465         for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
   465    466           pLoop->pNext = pNext;
   466    467           pLoop->selFlags |= SF_Compound;
   467    468         }
................................................................................
  1040   1041     A = sqlite3ExprFunction(pParse, Y, &X, D);
  1041   1042   }
  1042   1043   expr(A) ::= id(X) LP STAR RP. {
  1043   1044     A = sqlite3ExprFunction(pParse, 0, &X, 0);
  1044   1045   }
  1045   1046   
  1046   1047   %ifndef SQLITE_OMIT_WINDOWFUNC
  1047         -expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP over_clause(Z). {
         1048  +expr(A) ::= id(X) LP distinct(D) exprlist(Y) RP filter_over(Z). {
  1048   1049     A = sqlite3ExprFunction(pParse, Y, &X, D);
  1049   1050     sqlite3WindowAttach(pParse, A, Z);
  1050   1051   }
  1051         -expr(A) ::= id(X) LP STAR RP over_clause(Z). {
         1052  +expr(A) ::= id(X) LP STAR RP filter_over(Z). {
  1052   1053     A = sqlite3ExprFunction(pParse, 0, &X, 0);
  1053   1054     sqlite3WindowAttach(pParse, A, Z);
  1054   1055   }
  1055   1056   %endif
  1056   1057   
  1057   1058   term(A) ::= CTIME_KW(OP). {
  1058   1059     A = sqlite3ExprFunction(pParse, 0, &OP, 0);
................................................................................
  1653   1654   
  1654   1655   %type frame_opt {Window*}
  1655   1656   %destructor frame_opt {sqlite3WindowDelete(pParse->db, $$);}
  1656   1657   
  1657   1658   %type part_opt {ExprList*}
  1658   1659   %destructor part_opt {sqlite3ExprListDelete(pParse->db, $$);}
  1659   1660   
  1660         -%type filter_opt {Expr*}
  1661         -%destructor filter_opt {sqlite3ExprDelete(pParse->db, $$);}
         1661  +%type filter_clause {Expr*}
         1662  +%destructor filter_clause {sqlite3ExprDelete(pParse->db, $$);}
         1663  +
         1664  +%type over_clause {Window*}
         1665  +%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
         1666  +
         1667  +%type filter_over {Window*}
         1668  +%destructor filter_over {sqlite3WindowDelete(pParse->db, $$);}
  1662   1669   
  1663   1670   %type range_or_rows {int}
  1664   1671   
  1665   1672   %type frame_bound {struct FrameBound}
  1666   1673   %destructor frame_bound {sqlite3ExprDelete(pParse->db, $$.pExpr);}
  1667   1674   %type frame_bound_s {struct FrameBound}
  1668   1675   %destructor frame_bound_s {sqlite3ExprDelete(pParse->db, $$.pExpr);}
................................................................................
  1720   1727   frame_exclude(A) ::= GROUP|TIES(X).  {A = @X; /*A-overwrites-X*/}
  1721   1728   
  1722   1729   
  1723   1730   %type window_clause {Window*}
  1724   1731   %destructor window_clause {sqlite3WindowListDelete(pParse->db, $$);}
  1725   1732   window_clause(A) ::= WINDOW windowdefn_list(B). { A = B; }
  1726   1733   
  1727         -%type over_clause {Window*}
  1728         -%destructor over_clause {sqlite3WindowDelete(pParse->db, $$);}
  1729         -over_clause(A) ::= filter_opt(W) OVER LP window(Z) RP. {
         1734  +filter_over(A) ::= filter_clause(F) over_clause(O). {
         1735  +  O->pFilter = F;
         1736  +  A = O;
         1737  +}
         1738  +filter_over(A) ::= over_clause(O). {
         1739  +  A = O;
         1740  +}
         1741  +filter_over(A) ::= filter_clause(F). {
         1742  +  A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
         1743  +  if( A ){
         1744  +    A->eFrmType = TK_FILTER;
         1745  +    A->pFilter = F;
         1746  +  }else{
         1747  +    sqlite3ExprDelete(pParse->db, F);
         1748  +  }
         1749  +}
         1750  +
         1751  +over_clause(A) ::= OVER LP window(Z) RP. {
  1730   1752     A = Z;
  1731   1753     assert( A!=0 );
  1732         -  A->pFilter = W;
  1733   1754   }
  1734         -over_clause(A) ::= filter_opt(W) OVER nm(Z). {
         1755  +over_clause(A) ::= OVER nm(Z). {
  1735   1756     A = (Window*)sqlite3DbMallocZero(pParse->db, sizeof(Window));
  1736   1757     if( A ){
  1737   1758       A->zName = sqlite3DbStrNDup(pParse->db, Z.z, Z.n);
  1738         -    A->pFilter = W;
  1739         -  }else{
  1740         -    sqlite3ExprDelete(pParse->db, W);
  1741   1759     }
  1742   1760   }
  1743   1761   
  1744         -filter_opt(A) ::= .                            { A = 0; }
  1745         -filter_opt(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
         1762  +filter_clause(A) ::= FILTER LP WHERE expr(X) RP.  { A = X; }
  1746   1763   %endif /* SQLITE_OMIT_WINDOWFUNC */
  1747   1764   
  1748   1765   /*
  1749   1766   ** The code generator needs some extra TK_ token values for tokens that
  1750   1767   ** are synthesized and do not actually appear in the grammar:
  1751   1768   */
  1752   1769   %token

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.

  1159   1159     break;
  1160   1160   #endif
  1161   1161   
  1162   1162     case PragTyp_INDEX_INFO: if( zRight ){
  1163   1163       Index *pIdx;
  1164   1164       Table *pTab;
  1165   1165       pIdx = sqlite3FindIndex(db, zRight, zDb);
         1166  +    if( pIdx==0 ){
         1167  +      /* If there is no index named zRight, check to see if there is a
         1168  +      ** WITHOUT ROWID table named zRight, and if there is, show the
         1169  +      ** structure of the PRIMARY KEY index for that table. */
         1170  +      pTab = sqlite3LocateTable(pParse, LOCATE_NOERR, zRight, zDb);
         1171  +      if( pTab && !HasRowid(pTab) ){
         1172  +        pIdx = sqlite3PrimaryKeyIndex(pTab);
         1173  +      }
         1174  +    }
  1166   1175       if( pIdx ){
  1167   1176         int iIdxDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
  1168   1177         int i;
  1169   1178         int mx;
  1170   1179         if( pPragma->iArg ){
  1171   1180           /* PRAGMA index_xinfo (newer version with more rows and columns) */
  1172   1181           mx = pIdx->nColumn;

Changes to src/prepare.c.

   724    724     }
   725    725     if( pzTail ){
   726    726       *pzTail = sParse.zTail;
   727    727     }
   728    728     rc = sParse.rc;
   729    729   
   730    730   #ifndef SQLITE_OMIT_EXPLAIN
   731         -  if( rc==SQLITE_OK && sParse.pVdbe && sParse.explain ){
          731  +  /* Justification for the ALWAYS(): The only way for rc to be SQLITE_OK and
          732  +  ** sParse.pVdbe to be NULL is if the input SQL is an empty string, but in
          733  +  ** that case, sParse.explain will be false. */
          734  +  if( sParse.explain && rc==SQLITE_OK && ALWAYS(sParse.pVdbe) ){
   732    735       static const char * const azColName[] = {
   733    736          "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
   734    737          "id", "parent", "notused", "detail"
   735    738       };
   736    739       int iFirst, mx;
   737    740       if( sParse.explain==2 ){
   738    741         sqlite3VdbeSetNumCols(sParse.pVdbe, 4);
................................................................................
   749    752       }
   750    753     }
   751    754   #endif
   752    755   
   753    756     if( db->init.busy==0 ){
   754    757       sqlite3VdbeSetSql(sParse.pVdbe, zSql, (int)(sParse.zTail-zSql), prepFlags);
   755    758     }
   756         -  if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
   757         -    sqlite3VdbeFinalize(sParse.pVdbe);
          759  +  if( rc!=SQLITE_OK || db->mallocFailed ){
          760  +    if( sParse.pVdbe ) sqlite3VdbeFinalize(sParse.pVdbe);
   758    761       assert(!(*ppStmt));
   759    762     }else{
   760    763       *ppStmt = (sqlite3_stmt*)sParse.pVdbe;
   761    764     }
   762    765   
   763    766     if( zErrMsg ){
   764    767       sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);

Changes to src/resolve.c.

   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 ){
................................................................................
   826    828   
   827    829         if( 0==IN_RENAME_OBJECT ){
   828    830   #ifndef SQLITE_OMIT_WINDOWFUNC
   829    831           assert( is_agg==0 || (pDef->funcFlags & SQLITE_FUNC_MINMAX)
   830    832             || (pDef->xValue==0 && pDef->xInverse==0)
   831    833             || (pDef->xValue && pDef->xInverse && pDef->xSFunc && pDef->xFinalize)
   832    834           );
   833         -        if( pDef && pDef->xValue==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
          835  +        if( pDef && pDef->xValue==0 && pWin ){
   834    836             sqlite3ErrorMsg(pParse, 
   835    837                 "%.*s() may not be used as a window function", nId, zId
   836    838             );
   837    839             pNC->nErr++;
   838    840           }else if( 
   839    841                 (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)
          842  +           || (is_agg && (pDef->funcFlags&SQLITE_FUNC_WINDOW) && !pWin)
          843  +           || (is_agg && pWin && (pNC->ncFlags & NC_AllowWin)==0)
   842    844           ){
   843    845             const char *zType;
   844         -          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pExpr->y.pWin ){
          846  +          if( (pDef->funcFlags & SQLITE_FUNC_WINDOW) || pWin ){
   845    847               zType = "window";
   846    848             }else{
   847    849               zType = "aggregate";
   848    850             }
   849    851             sqlite3ErrorMsg(pParse, "misuse of %s function %.*s()",zType,nId,zId);
   850    852             pNC->nErr++;
   851    853             is_agg = 0;
................................................................................
   865    867             sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
   866    868             pNC->nErr++;
   867    869           }else if( wrong_num_args ){
   868    870             sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
   869    871                  nId, zId);
   870    872             pNC->nErr++;
   871    873           }
          874  +#ifndef SQLITE_OMIT_WINDOWFUNC
          875  +        else if( is_agg==0 && ExprHasProperty(pExpr, EP_WinFunc) ){
          876  +          sqlite3ErrorMsg(pParse, 
          877  +              "FILTER may not be used with non-aggregate %.*s()", 
          878  +              nId, zId
          879  +          );
          880  +          pNC->nErr++;
          881  +        }
          882  +#endif
   872    883           if( is_agg ){
   873    884             /* Window functions may not be arguments of aggregate functions.
   874    885             ** Or arguments of other window functions. But aggregate functions
   875    886             ** may be arguments for window functions.  */
   876    887   #ifndef SQLITE_OMIT_WINDOWFUNC
   877         -          pNC->ncFlags &= ~(NC_AllowWin | (!pExpr->y.pWin ? NC_AllowAgg : 0));
          888  +          pNC->ncFlags &= ~(NC_AllowWin | (!pWin ? NC_AllowAgg : 0));
   878    889   #else
   879    890             pNC->ncFlags &= ~NC_AllowAgg;
   880    891   #endif
   881    892           }
   882    893         }
          894  +#ifndef SQLITE_OMIT_WINDOWFUNC
          895  +      else if( ExprHasProperty(pExpr, EP_WinFunc) ){
          896  +        is_agg = 1;
          897  +      }
          898  +#endif
   883    899         sqlite3WalkExprList(pWalker, pList);
   884    900         if( is_agg ){
   885    901   #ifndef SQLITE_OMIT_WINDOWFUNC
   886         -        if( pExpr->y.pWin ){
          902  +        if( pWin ){
   887    903             Select *pSel = pNC->pWinSelect;
          904  +          assert( pWin==pExpr->y.pWin );
   888    905             if( IN_RENAME_OBJECT==0 ){
   889         -            sqlite3WindowUpdate(pParse, pSel->pWinDefn, pExpr->y.pWin, pDef);
          906  +            sqlite3WindowUpdate(pParse, pSel->pWinDefn, pWin, pDef);
   890    907             }
   891         -          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pPartition);
   892         -          sqlite3WalkExprList(pWalker, pExpr->y.pWin->pOrderBy);
   893         -          sqlite3WalkExpr(pWalker, pExpr->y.pWin->pFilter);
          908  +          sqlite3WalkExprList(pWalker, pWin->pPartition);
          909  +          sqlite3WalkExprList(pWalker, pWin->pOrderBy);
          910  +          sqlite3WalkExpr(pWalker, pWin->pFilter);
   894    911             if( 0==pSel->pWin 
   895         -           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pExpr->y.pWin) 
          912  +           || 0==sqlite3WindowCompare(pParse, pSel->pWin, pWin, 0)
   896    913             ){
   897         -            pExpr->y.pWin->pNextWin = pSel->pWin;
   898         -            pSel->pWin = pExpr->y.pWin;
          914  +            pWin->pNextWin = pSel->pWin;
          915  +            if( pSel->pWin ){
          916  +              pSel->pWin->ppThis = &pWin->pNextWin;
          917  +            }
          918  +            pSel->pWin = pWin;
          919  +            pWin->ppThis = &pSel->pWin;
   899    920             }
   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    937             assert( pDef!=0 );
   912    938             if( pNC2 ){
   913    939               assert( SQLITE_FUNC_MINMAX==NC_MinMaxAgg );
................................................................................
  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){
  1280   1306     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         -    }
         1307  +    Window *pWin = pExpr->y.pWin;
         1308  +    sqlite3WindowUnlinkFromSelect(pWin);
  1288   1309     }
  1289   1310     return WRC_Continue;
  1290   1311   }
  1291   1312   
  1292   1313   /*
  1293   1314   ** Remove any Window objects owned by the expression pExpr from the
  1294   1315   ** Select.pWin list of Select object pSelect.
  1295   1316   */
  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);
         1317  +static void windowRemoveExprFromSelect(Select *pSelect, Expr *pExpr){
         1318  +  if( pSelect->pWin ){
         1319  +    Walker sWalker;
         1320  +    memset(&sWalker, 0, sizeof(Walker));
         1321  +    sWalker.xExprCallback = resolveRemoveWindowsCb;
         1322  +    sWalker.u.pSelect = pSelect;
         1323  +    sqlite3WalkExpr(&sWalker, pExpr);
         1324  +  }
  1302   1325   }
  1303   1326   #else
  1304         -# define resolveRemoveWindows(x,y)
  1305         -#endif
         1327  +# define windowRemoveExprFromSelect(a, b)
         1328  +#endif /* SQLITE_OMIT_WINDOWFUNC */
  1306   1329   
  1307   1330   /*
  1308   1331   ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
  1309   1332   ** The Name context of the SELECT statement is pNC.  zType is either
  1310   1333   ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
  1311   1334   **
  1312   1335   ** This routine resolves each term of the clause into an expression.
................................................................................
  1369   1392         return 1;
  1370   1393       }
  1371   1394       for(j=0; j<pSelect->pEList->nExpr; j++){
  1372   1395         if( sqlite3ExprCompare(0, pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
  1373   1396           /* Since this expresion is being changed into a reference
  1374   1397           ** to an identical expression in the result set, remove all Window
  1375   1398           ** objects belonging to the expression from the Select.pWin list. */
  1376         -        resolveRemoveWindows(pSelect, pE);
         1399  +        windowRemoveExprFromSelect(pSelect, pE);
  1377   1400           pItem->u.x.iOrderByCol = j+1;
  1378   1401         }
  1379   1402       }
  1380   1403     }
  1381   1404     return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
  1382   1405   }
  1383   1406   

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   /*
................................................................................
  3945   3946     ** will scan expressions looking for iParent references and replace
  3946   3947     ** those references with expressions that resolve to the subquery FROM
  3947   3948     ** elements we are now copying in.
  3948   3949     */
  3949   3950     for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
  3950   3951       int nSubSrc;
  3951   3952       u8 jointype = 0;
         3953  +    assert( pSub!=0 );
  3952   3954       pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
  3953   3955       nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
  3954   3956       pSrc = pParent->pSrc;     /* FROM clause of the outer query */
  3955   3957   
  3956   3958       if( pSrc ){
  3957   3959         assert( pParent==p );  /* First time through the loop */
  3958   3960         jointype = pSubitem->fg.jointype;
................................................................................
  4399   4401     ExprList *pEList = pFunc->x.pList;    /* Arguments to agg function */
  4400   4402     const char *zFunc;                    /* Name of aggregate function pFunc */
  4401   4403     ExprList *pOrderBy;
  4402   4404     u8 sortOrder;
  4403   4405   
  4404   4406     assert( *ppMinMax==0 );
  4405   4407     assert( pFunc->op==TK_AGG_FUNCTION );
  4406         -  if( pEList==0 || pEList->nExpr!=1 ) return eRet;
         4408  +  assert( !IsWindowFunc(pFunc) );
         4409  +  if( pEList==0 || pEList->nExpr!=1 || ExprHasProperty(pFunc, EP_WinFunc) ){
         4410  +    return eRet;
         4411  +  }
  4407   4412     zFunc = pFunc->u.zToken;
  4408   4413     if( sqlite3StrICmp(zFunc, "min")==0 ){
  4409   4414       eRet = WHERE_ORDERBY_MIN;
  4410   4415       sortOrder = SQLITE_SO_ASC;
  4411   4416     }else if( sqlite3StrICmp(zFunc, "max")==0 ){
  4412   4417       eRet = WHERE_ORDERBY_MAX;
  4413   4418       sortOrder = SQLITE_SO_DESC;
................................................................................
  4446   4451     pExpr = p->pEList->a[0].pExpr;
  4447   4452     assert( pTab && !pTab->pSelect && pExpr );
  4448   4453   
  4449   4454     if( IsVirtual(pTab) ) return 0;
  4450   4455     if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
  4451   4456     if( NEVER(pAggInfo->nFunc==0) ) return 0;
  4452   4457     if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
  4453         -  if( pExpr->flags&EP_Distinct ) return 0;
         4458  +  if( ExprHasProperty(pExpr, EP_Distinct|EP_WinFunc) ) return 0;
  4454   4459   
  4455   4460     return pTab;
  4456   4461   }
  4457   4462   
  4458   4463   /*
  4459   4464   ** If the source-list item passed as an argument was augmented with an
  4460   4465   ** INDEXED BY clause, then try to locate the specified index. If there
................................................................................
  5326   5331     pAggInfo->directMode = 1;
  5327   5332     for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
  5328   5333       int nArg;
  5329   5334       int addrNext = 0;
  5330   5335       int regAgg;
  5331   5336       ExprList *pList = pF->pExpr->x.pList;
  5332   5337       assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
         5338  +    assert( !IsWindowFunc(pF->pExpr) );
         5339  +    if( ExprHasProperty(pF->pExpr, EP_WinFunc) ){
         5340  +      Expr *pFilter = pF->pExpr->y.pWin->pFilter;
         5341  +      addrNext = sqlite3VdbeMakeLabel(pParse);
         5342  +      sqlite3ExprIfFalse(pParse, pFilter, addrNext, SQLITE_JUMPIFNULL);
         5343  +    }
  5333   5344       if( pList ){
  5334   5345         nArg = pList->nExpr;
  5335   5346         regAgg = sqlite3GetTempRange(pParse, nArg);
  5336   5347         sqlite3ExprCodeExprList(pParse, pList, regAgg, 0, SQLITE_ECEL_DUP);
  5337   5348       }else{
  5338   5349         nArg = 0;
  5339   5350         regAgg = 0;
  5340   5351       }
  5341   5352       if( pF->iDistinct>=0 ){
  5342         -      addrNext = sqlite3VdbeMakeLabel(pParse);
         5353  +      if( addrNext==0 ){ 
         5354  +        addrNext = sqlite3VdbeMakeLabel(pParse);
         5355  +      }
  5343   5356         testcase( nArg==0 );  /* Error condition */
  5344   5357         testcase( nArg>1 );   /* Also an error */
  5345   5358         codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
  5346   5359       }
  5347   5360       if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
  5348   5361         CollSeq *pColl = 0;
  5349   5362         struct ExprList_item *pItem;
................................................................................
  5852   5865       pSub = pItem->pSelect;
  5853   5866       if( pSub==0 ) continue;
  5854   5867   
  5855   5868       /* The code for a subquery should only be generated once, though it is
  5856   5869       ** technically harmless for it to be generated multiple times. The
  5857   5870       ** following assert() will detect if something changes to cause
  5858   5871       ** 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 );
         5872  +    ** developers to try to optimize the situation.
         5873  +    **
         5874  +    ** Update 2019-07-24:
         5875  +    ** See ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311cec40.
         5876  +    ** The dbsqlfuzz fuzzer found a case where the same subquery gets
         5877  +    ** coded twice.  So this assert() now becomes a testcase().  It should
         5878  +    ** be very rare, though.
         5879  +    */
         5880  +    testcase( pItem->addrFillSub!=0 );
  5861   5881   
  5862   5882       /* Increment Parse.nHeight by the height of the largest expression
  5863   5883       ** tree referred to by this, the parent select. The child select
  5864   5884       ** may contain expression trees of at most
  5865   5885       ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
  5866   5886       ** more conservative than necessary, but much easier than enforcing
  5867   5887       ** an exact limit.
................................................................................
  5927   5947         ** is a register allocated to hold the subroutine return address
  5928   5948         */
  5929   5949         int topAddr;
  5930   5950         int onceAddr = 0;
  5931   5951         int retAddr;
  5932   5952         struct SrcList_item *pPrior;
  5933   5953   
  5934         -      assert( pItem->addrFillSub==0 );
         5954  +      testcase( pItem->addrFillSub==0 ); /* Ticket c52b09c7f38903b1311 */
  5935   5955         pItem->regReturn = ++pParse->nMem;
  5936   5956         topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
  5937   5957         pItem->addrFillSub = topAddr+1;
  5938   5958         if( pItem->fg.isCorrelated==0 ){
  5939   5959           /* If the subquery is not correlated and if we are not inside of
  5940   5960           ** a trigger, then we only need to compute the value of the subquery
  5941   5961           ** once. */
................................................................................
  6218   6238       sAggInfo.nAccumulator = sAggInfo.nColumn;
  6219   6239       if( p->pGroupBy==0 && p->pHaving==0 && sAggInfo.nFunc==1 ){
  6220   6240         minMaxFlag = minMaxQuery(db, sAggInfo.aFunc[0].pExpr, &pMinMaxOrderBy);
  6221   6241       }else{
  6222   6242         minMaxFlag = WHERE_ORDERBY_NORMAL;
  6223   6243       }
  6224   6244       for(i=0; i<sAggInfo.nFunc; i++){
  6225         -      assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
         6245  +      Expr *pExpr = sAggInfo.aFunc[i].pExpr;
         6246  +      assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
  6226   6247         sNC.ncFlags |= NC_InAggFunc;
  6227         -      sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
         6248  +      sqlite3ExprAnalyzeAggList(&sNC, pExpr->x.pList);
         6249  +#ifndef SQLITE_OMIT_WINDOWFUNC
         6250  +      assert( !IsWindowFunc(pExpr) );
         6251  +      if( ExprHasProperty(pExpr, EP_WinFunc) ){
         6252  +        sqlite3ExprAnalyzeAggregates(&sNC, pExpr->y.pWin->pFilter);
         6253  +      }
         6254  +#endif
  6228   6255         sNC.ncFlags &= ~NC_InAggFunc;
  6229   6256       }
  6230   6257       sAggInfo.mxReg = pParse->nMem;
  6231   6258       if( db->mallocFailed ) goto select_end;
  6232   6259   #if SELECTTRACE_ENABLED
  6233   6260       if( sqlite3SelectTrace & 0x400 ){
  6234   6261         int ii;

Changes to src/shell.c.in.

  1253   1253     f = fopen(zTempFile, bBin ? "wb" : "w");
  1254   1254     if( f==0 ){
  1255   1255       sqlite3_result_error(context, "edit() cannot open temp file", -1);
  1256   1256       goto edit_func_end;
  1257   1257     }
  1258   1258     sz = sqlite3_value_bytes(argv[0]);
  1259   1259     if( bBin ){
  1260         -    x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
         1260  +    x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
  1261   1261     }else{
  1262   1262       const char *z = (const char*)sqlite3_value_text(argv[0]);
  1263   1263       /* Remember whether or not the value originally contained \r\n */
  1264   1264       if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
  1265         -    x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
         1265  +    x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
  1266   1266     }
  1267   1267     fclose(f);
  1268   1268     f = 0;
  1269   1269     if( x!=sz ){
  1270   1270       sqlite3_result_error(context, "edit() could not write the whole file", -1);
  1271   1271       goto edit_func_end;
  1272   1272     }
................................................................................
  1291   1291     sz = ftell(f);
  1292   1292     rewind(f);
  1293   1293     p = sqlite3_malloc64( sz+(bBin==0) );
  1294   1294     if( p==0 ){
  1295   1295       sqlite3_result_error_nomem(context);
  1296   1296       goto edit_func_end;
  1297   1297     }
  1298         -  x = fread(p, 1, sz, f);
         1298  +  x = fread(p, 1, (size_t)sz, f);
  1299   1299     fclose(f);
  1300   1300     f = 0;
  1301   1301     if( x!=sz ){
  1302   1302       sqlite3_result_error(context, "could not read back the whole file", -1);
  1303   1303       goto edit_func_end;
  1304   1304     }
  1305   1305     if( bBin ){
................................................................................
  3894   3894     }
  3895   3895     *pnData = 0;
  3896   3896     nLine++;
  3897   3897     if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
  3898   3898     rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
  3899   3899     if( rc!=2 ) goto readHexDb_error;
  3900   3900     if( n<0 ) goto readHexDb_error;
         3901  +  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
         3902  +  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
  3901   3903     a = sqlite3_malloc( n ? n : 1 );
  3902   3904     if( a==0 ){
  3903   3905       utf8_printf(stderr, "Out of memory!\n");
  3904   3906       goto readHexDb_error;
  3905   3907     }
  3906   3908     memset(a, 0, n);
  3907   3909     if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
................................................................................
  6634   6636         if( bIntkey ){
  6635   6637           pTab->azlCol[0] = shellMPrintf(&rc, "%Q", zPk);
  6636   6638         }else{
  6637   6639           pTab->azlCol[0] = shellMPrintf(&rc, "");
  6638   6640         }
  6639   6641         i = 1;
  6640   6642         shellPreparePrintf(dbtmp, &rc, &pStmt, 
  6641         -          "SELECT %Q || group_concat(name, ', ') "
         6643  +          "SELECT %Q || group_concat(quote(name), ', ') "
  6642   6644             "  FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
  6643   6645             "FROM pragma_table_info(%Q)", 
  6644   6646             bIntkey ? ", " : "", pTab->iPk, 
  6645   6647             bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
  6646   6648             zName
  6647   6649         );
  6648   6650         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
................................................................................
  6826   6828         return 1;
  6827   6829       }
  6828   6830     }
  6829   6831   
  6830   6832     shellExecPrintf(pState->db, &rc,
  6831   6833       /* Attach an in-memory database named 'recovery'. Create an indexed 
  6832   6834       ** cache of the sqlite_dbptr virtual table. */
         6835  +    "PRAGMA writable_schema = on;"
  6833   6836       "ATTACH %Q AS recovery;"
  6834   6837       "DROP TABLE IF EXISTS recovery.dbptr;"
  6835   6838       "DROP TABLE IF EXISTS recovery.freelist;"
  6836   6839       "DROP TABLE IF EXISTS recovery.map;"
  6837   6840       "DROP TABLE IF EXISTS recovery.schema;"
  6838   6841       "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
  6839   6842     );
................................................................................
  6855   6858         "    UNION ALL"
  6856   6859         "  SELECT data, n-1, shell_int32(data, 2+n) "
  6857   6860         "      FROM freelist WHERE n>=0"
  6858   6861         ")"
  6859   6862         "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
  6860   6863       );
  6861   6864     }
         6865  +
         6866  +  /* If this is an auto-vacuum database, add all pointer-map pages to
         6867  +  ** the freelist table. Do this regardless of whether or not 
         6868  +  ** --freelist-corrupt was specified.  */
         6869  +  shellExec(pState->db, &rc, 
         6870  +    "WITH ptrmap(pgno) AS ("
         6871  +    "  SELECT 2 WHERE shell_int32("
         6872  +    "    (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
         6873  +    "  )"
         6874  +    "    UNION ALL "
         6875  +    "  SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
         6876  +    "  FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
         6877  +    ")"
         6878  +    "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
         6879  +  );
  6862   6880   
  6863   6881     shellExec(pState->db, &rc, 
  6864   6882       "CREATE TABLE recovery.dbptr("
  6865   6883       "      pgno, child, PRIMARY KEY(child, pgno)"
  6866   6884       ") WITHOUT ROWID;"
  6867   6885       "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
  6868   6886       "    SELECT * FROM sqlite_dbptr"
................................................................................
  6961   6979     pLoop = 0;
  6962   6980   
  6963   6981     shellPrepare(pState->db, &rc,
  6964   6982         "SELECT pgno FROM recovery.map WHERE root=?", &pPages
  6965   6983     );
  6966   6984     shellPrepare(pState->db, &rc,
  6967   6985         "SELECT max(field), group_concat(shell_escape_crnl(quote(value)), ', ')"
         6986  +      ", min(field) "
  6968   6987         "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
  6969   6988         "GROUP BY cell", &pCells
  6970   6989     );
  6971   6990   
  6972   6991     /* Loop through each root page. */
  6973   6992     shellPrepare(pState->db, &rc, 
  6974   6993         "SELECT root, intkey, max(maxlen) FROM recovery.map" 
................................................................................
  6979   6998     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
  6980   6999       int iRoot = sqlite3_column_int(pLoop, 0);
  6981   7000       int bIntkey = sqlite3_column_int(pLoop, 1);
  6982   7001       int nCol = sqlite3_column_int(pLoop, 2);
  6983   7002       int bNoop = 0;
  6984   7003       RecoverTable *pTab;
  6985   7004   
         7005  +    assert( bIntkey==0 || bIntkey==1 );
  6986   7006       pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
  6987   7007       if( bNoop || rc ) continue;
  6988   7008       if( pTab==0 ){
  6989   7009         if( pOrphan==0 ){
  6990   7010           pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
  6991   7011         }
  6992   7012         pTab = pOrphan;
................................................................................
  7000   7020       sqlite3_bind_int(pCells, 2, pTab->iPk);
  7001   7021   
  7002   7022       while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
  7003   7023         int iPgno = sqlite3_column_int(pPages, 0);
  7004   7024         sqlite3_bind_int(pCells, 1, iPgno);
  7005   7025         while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
  7006   7026           int nField = sqlite3_column_int(pCells, 0);
         7027  +        int iMin = sqlite3_column_int(pCells, 2);
  7007   7028           const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
         7029  +
         7030  +        RecoverTable *pTab2 = pTab;
         7031  +        if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
         7032  +          if( pOrphan==0 ){
         7033  +            pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
         7034  +          }
         7035  +          pTab2 = pOrphan;
         7036  +          if( pTab2==0 ) break;
         7037  +        }
  7008   7038   
  7009   7039           nField = nField+1;
  7010         -        if( pTab==pOrphan ){
         7040  +        if( pTab2==pOrphan ){
  7011   7041             raw_printf(pState->out, 
  7012   7042                 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
  7013         -              pTab->zQuoted, iRoot, iPgno, nField, 
  7014         -              bIntkey ? "" : "NULL, ", zVal, pTab->azlCol[nField]
         7043  +              pTab2->zQuoted, iRoot, iPgno, nField,
         7044  +              iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
  7015   7045             );
  7016   7046           }else{
  7017   7047             raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n", 
  7018         -              pTab->zQuoted, pTab->azlCol[nField], zVal
         7048  +              pTab2->zQuoted, pTab2->azlCol[nField], zVal
  7019   7049             );
  7020   7050           }
  7021   7051         }
  7022   7052         shellReset(&rc, pCells);
  7023   7053       }
  7024   7054       shellReset(&rc, pPages);
  7025   7055       if( pTab!=pOrphan ) recoverFreeTable(pTab);
................................................................................
  8630   8660           const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
  8631   8661           char zScNum[30];
  8632   8662           sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
  8633   8663           appendText(&sSelect, zDiv, 0);
  8634   8664           zDiv = " UNION ALL ";
  8635   8665           appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
  8636   8666           if( sqlite3_stricmp(zDb, "main")!=0 ){
  8637         -          appendText(&sSelect, zDb, '"');
         8667  +          appendText(&sSelect, zDb, '\'');
  8638   8668           }else{
  8639   8669             appendText(&sSelect, "NULL", 0);
  8640   8670           }
  8641   8671           appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
  8642   8672           appendText(&sSelect, zScNum, 0);
  8643   8673           appendText(&sSelect, " AS snum, ", 0);
  8644   8674           appendText(&sSelect, zDb, '\'');
  8645   8675           appendText(&sSelect, " AS sname FROM ", 0);
  8646         -        appendText(&sSelect, zDb, '"');
         8676  +        appendText(&sSelect, zDb, quoteChar(zDb));
  8647   8677           appendText(&sSelect, ".sqlite_master", 0);
  8648   8678         }
  8649   8679         sqlite3_finalize(pStmt);
  8650   8680   #ifdef SQLITE_INTROSPECTION_PRAGMAS
  8651   8681         if( zName ){
  8652   8682           appendText(&sSelect,
  8653   8683              " UNION ALL SELECT shell_module_schema(name),"

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
................................................................................
  2507   2507     u8 op2;                /* TK_REGISTER/TK_TRUTH: original value of Expr.op
  2508   2508                            ** TK_COLUMN: the value of p5 for OP_Column
  2509   2509                            ** TK_AGG_FUNCTION: nesting depth */
  2510   2510     AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
  2511   2511     union {
  2512   2512       Table *pTab;           /* TK_COLUMN: Table containing column. Can be NULL
  2513   2513                              ** for a column of an index on an expression */
  2514         -    Window *pWin;          /* TK_FUNCTION: Window definition for the func */
         2514  +    Window *pWin;          /* EP_WinFunc: Window/Filter defn for a function */
  2515   2515       struct {               /* TK_IN, TK_SELECT, and TK_EXISTS */
  2516   2516         int iAddr;             /* Subroutine entry address */
  2517   2517         int regReturn;         /* Register used to hold return address */
  2518   2518       } sub;
  2519   2519     } y;
  2520   2520   };
  2521   2521   
................................................................................
  2594   2594   #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
  2595   2595   
  2596   2596   /*
  2597   2597   ** Flags passed to the sqlite3ExprDup() function. See the header comment
  2598   2598   ** above sqlite3ExprDup() for details.
  2599   2599   */
  2600   2600   #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
         2601  +
         2602  +/*
         2603  +** True if the expression passed as an argument was a function with
         2604  +** an OVER() clause (a window function).
         2605  +*/
         2606  +#define IsWindowFunc(p) ( \
         2607  +    ExprHasProperty((p), EP_WinFunc) && p->y.pWin->eFrmType!=TK_FILTER \
         2608  +)
  2601   2609   
  2602   2610   /*
  2603   2611   ** A list of expressions.  Each expression may optionally have a
  2604   2612   ** name.  An expr/name combination can be used in several ways, such
  2605   2613   ** as the list of "expr AS ID" fields following a "SELECT" or in the
  2606   2614   ** list of "ID = expr" items in an UPDATE.  A list of expressions can
  2607   2615   ** also be used as the argument to a function, in which case the a.zName
................................................................................
  3572   3580   struct TreeView {
  3573   3581     int iLevel;             /* Which level of the tree we are on */
  3574   3582     u8  bLine[100];         /* Draw vertical in column i if bLine[i] is true */
  3575   3583   };
  3576   3584   #endif /* SQLITE_DEBUG */
  3577   3585   
  3578   3586   /*
  3579         -** This object is used in various ways, all related to window functions
         3587  +** This object is used in various ways, most (but not all) related to window
         3588  +** functions.
  3580   3589   **
  3581   3590   **   (1) A single instance of this structure is attached to the
  3582         -**       the Expr.pWin field for each window function in an expression tree.
         3591  +**       the Expr.y.pWin field for each window function in an expression tree.
  3583   3592   **       This object holds the information contained in the OVER clause,
  3584   3593   **       plus additional fields used during code generation.
  3585   3594   **
  3586   3595   **   (2) All window functions in a single SELECT form a linked-list
  3587   3596   **       attached to Select.pWin.  The Window.pFunc and Window.pExpr
  3588   3597   **       fields point back to the expression that is the window function.
  3589   3598   **
  3590   3599   **   (3) The terms of the WINDOW clause of a SELECT are instances of this
  3591   3600   **       object on a linked list attached to Select.pWinDefn.
  3592   3601   **
         3602  +**   (4) For an aggregate function with a FILTER clause, an instance
         3603  +**       of this object is stored in Expr.y.pWin with eFrmType set to
         3604  +**       TK_FILTER. In this case the only field used is Window.pFilter.
         3605  +**
  3593   3606   ** The uses (1) and (2) are really the same Window object that just happens
  3594   3607   ** to be accessible in two different ways.  Use case (3) are separate objects.
  3595   3608   */
  3596   3609   struct Window {
  3597   3610     char *zName;            /* Name of window (may be NULL) */
  3598   3611     char *zBase;            /* Name of base window for chaining (may be NULL) */
  3599   3612     ExprList *pPartition;   /* PARTITION BY clause */
................................................................................
  3601   3614     u8 eFrmType;            /* TK_RANGE, TK_GROUPS, TK_ROWS, or 0 */
  3602   3615     u8 eStart;              /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
  3603   3616     u8 eEnd;                /* UNBOUNDED, CURRENT, PRECEDING or FOLLOWING */
  3604   3617     u8 bImplicitFrame;      /* True if frame was implicitly specified */
  3605   3618     u8 eExclude;            /* TK_NO, TK_CURRENT, TK_TIES, TK_GROUP, or 0 */
  3606   3619     Expr *pStart;           /* Expression for "<expr> PRECEDING" */
  3607   3620     Expr *pEnd;             /* Expression for "<expr> FOLLOWING" */
         3621  +  Window **ppThis;        /* Pointer to this object in Select.pWin list */
  3608   3622     Window *pNextWin;       /* Next window function belonging to this SELECT */
  3609   3623     Expr *pFilter;          /* The FILTER expression */
  3610   3624     FuncDef *pFunc;         /* The function */
  3611   3625     int iEphCsr;            /* Partition buffer or Peer buffer */
  3612   3626     int regAccum;
  3613   3627     int regResult;
  3614   3628     int csrApp;             /* Function cursor (used by min/max) */
................................................................................
  3620   3634     int regOne;             /* Register containing constant value 1 */
  3621   3635     int regStartRowid;
  3622   3636     int regEndRowid;
  3623   3637   };
  3624   3638   
  3625   3639   #ifndef SQLITE_OMIT_WINDOWFUNC
  3626   3640   void sqlite3WindowDelete(sqlite3*, Window*);
         3641  +void sqlite3WindowUnlinkFromSelect(Window*);
  3627   3642   void sqlite3WindowListDelete(sqlite3 *db, Window *p);
  3628   3643   Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
  3629   3644   void sqlite3WindowAttach(Parse*, Expr*, Window*);
  3630         -int sqlite3WindowCompare(Parse*, Window*, Window*);
         3645  +int sqlite3WindowCompare(Parse*, Window*, Window*, int);
  3631   3646   void sqlite3WindowCodeInit(Parse*, Window*);
  3632   3647   void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
  3633   3648   int sqlite3WindowRewrite(Parse*, Select*);
  3634   3649   int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
  3635   3650   void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
  3636   3651   Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
  3637   3652   Window *sqlite3WindowListDup(sqlite3 *db, Window *p);

Changes to src/test1.c.

  7736   7736       for(iNext=i; zIn[iNext] && zIn[iNext]!='\n'; iNext++){}
  7737   7737       if( zIn[iNext]=='\n' ) iNext++;
  7738   7738       while( zIn[i]==' ' || zIn[i]=='\t' ){ i++; }
  7739   7739       if( a==0 ){
  7740   7740         int pgsz;
  7741   7741         rc = sscanf(zIn+i, "| size %d pagesize %d", &n, &pgsz);
  7742   7742         if( rc!=2 ) continue;
         7743  +      if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ){
         7744  +        Tcl_AppendResult(interp, "bad 'pagesize' field", (void*)0);
         7745  +        return TCL_ERROR;
         7746  +      }
         7747  +      n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
  7743   7748         if( n<512 ){
  7744   7749           Tcl_AppendResult(interp, "bad 'size' field", (void*)0);
  7745   7750           return TCL_ERROR;
  7746   7751         }
  7747   7752         a = malloc( n );
  7748   7753         if( a==0 ){
  7749   7754           Tcl_AppendResult(interp, "out of memory", (void*)0);

Changes to src/trigger.c.

   357    357           sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
   358    358     }
   359    359   
   360    360     if( db->init.busy ){
   361    361       Trigger *pLink = pTrig;
   362    362       Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
   363    363       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
          364  +    assert( pLink!=0 );
   364    365       pTrig = sqlite3HashInsert(pHash, zName, pTrig);
   365    366       if( pTrig ){
   366    367         sqlite3OomFault(db);
   367    368       }else if( pLink->pSchema==pLink->pTabSchema ){
   368    369         Table *pTab;
   369    370         pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
   370    371         assert( pTab!=0 );

Changes to src/vdbe.c.

  2923   2923         pLast--;
  2924   2924         nField--;
  2925   2925       }
  2926   2926     }
  2927   2927   #endif
  2928   2928   
  2929   2929     /* Loop through the elements that will make up the record to figure
  2930         -  ** out how much space is required for the new record.
         2930  +  ** out how much space is required for the new record.  After this loop,
         2931  +  ** the Mem.uTemp field of each term should hold the serial-type that will
         2932  +  ** be used for that term in the generated record:
         2933  +  **
         2934  +  **   Mem.uTemp value    type
         2935  +  **   ---------------    ---------------
         2936  +  **      0               NULL
         2937  +  **      1               1-byte signed integer
         2938  +  **      2               2-byte signed integer
         2939  +  **      3               3-byte signed integer
         2940  +  **      4               4-byte signed integer
         2941  +  **      5               6-byte signed integer
         2942  +  **      6               8-byte signed integer
         2943  +  **      7               IEEE float
         2944  +  **      8               Integer constant 0
         2945  +  **      9               Integer constant 1
         2946  +  **     10,11            reserved for expansion
         2947  +  **    N>=12 and even    BLOB
         2948  +  **    N>=13 and odd     text
         2949  +  **
         2950  +  ** The following additional values are computed:
         2951  +  **     nHdr        Number of bytes needed for the record header
         2952  +  **     nData       Number of bytes of data space needed for the record
         2953  +  **     nZero       Zero bytes at the end of the record
  2931   2954     */
  2932   2955     pRec = pLast;
  2933   2956     do{
  2934   2957       assert( memIsValid(pRec) );
  2935         -    serial_type = sqlite3VdbeSerialType(pRec, file_format, &len);
  2936         -    if( pRec->flags & MEM_Zero ){
  2937         -      if( serial_type==0 ){
         2958  +    if( pRec->flags & MEM_Null ){
         2959  +      if( pRec->flags & MEM_Zero ){
  2938   2960           /* Values with MEM_Null and MEM_Zero are created by xColumn virtual
  2939   2961           ** table methods that never invoke sqlite3_result_xxxxx() while
  2940   2962           ** computing an unchanging column value in an UPDATE statement.
  2941   2963           ** Give such values a special internal-use-only serial-type of 10
  2942   2964           ** so that they can be passed through to xUpdate and have
  2943   2965           ** a true sqlite3_value_nochange(). */
  2944   2966           assert( pOp->p5==OPFLAG_NOCHNG_MAGIC || CORRUPT_DB );
  2945         -        serial_type = 10;
  2946         -      }else if( nData ){
  2947         -        if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
         2967  +        pRec->uTemp = 10;
         2968  +      }else{
         2969  +        pRec->uTemp = 0;
         2970  +      }
         2971  +      nHdr++;
         2972  +    }else if( pRec->flags & (MEM_Int|MEM_IntReal) ){
         2973  +      /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
         2974  +      i64 i = pRec->u.i;
         2975  +      u64 u;
         2976  +      testcase( pRec->flags & MEM_Int );
         2977  +      testcase( pRec->flags & MEM_IntReal );
         2978  +      if( i<0 ){
         2979  +        u = ~i;
         2980  +      }else{
         2981  +        u = i;
         2982  +      }
         2983  +      nHdr++;
         2984  +      testcase( u==127 );               testcase( u==128 );
         2985  +      testcase( u==32767 );             testcase( u==32768 );
         2986  +      testcase( u==8388607 );           testcase( u==8388608 );
         2987  +      testcase( u==2147483647 );        testcase( u==2147483648 );
         2988  +      testcase( u==140737488355327LL ); testcase( u==140737488355328LL );
         2989  +      if( u<=127 ){
         2990  +        if( (i&1)==i && file_format>=4 ){
         2991  +          pRec->uTemp = 8+(u32)u;
         2992  +        }else{
         2993  +          nData++;
         2994  +          pRec->uTemp = 1;
         2995  +        }
         2996  +      }else if( u<=32767 ){
         2997  +        nData += 2;
         2998  +        pRec->uTemp = 2;
         2999  +      }else if( u<=8388607 ){
         3000  +        nData += 3;
         3001  +        pRec->uTemp = 3;
         3002  +      }else if( u<=2147483647 ){
         3003  +        nData += 4;
         3004  +        pRec->uTemp = 4;
         3005  +      }else if( u<=140737488355327LL ){
         3006  +        nData += 6;
         3007  +        pRec->uTemp = 5;
  2948   3008         }else{
  2949         -        nZero += pRec->u.nZero;
  2950         -        len -= pRec->u.nZero;
         3009  +        nData += 8;
         3010  +        if( pRec->flags & MEM_IntReal ){
         3011  +          /* If the value is IntReal and is going to take up 8 bytes to store
         3012  +          ** as an integer, then we might as well make it an 8-byte floating
         3013  +          ** point value */
         3014  +          pRec->u.r = (double)pRec->u.i;
         3015  +          pRec->flags &= ~MEM_IntReal;
         3016  +          pRec->flags |= MEM_Real;
         3017  +          pRec->uTemp = 7;
         3018  +        }else{
         3019  +          pRec->uTemp = 6;
         3020  +        }
  2951   3021         }
         3022  +    }else if( pRec->flags & MEM_Real ){
         3023  +      nHdr++;
         3024  +      nData += 8;
         3025  +      pRec->uTemp = 7;
         3026  +    }else{
         3027  +      assert( db->mallocFailed || pRec->flags&(MEM_Str|MEM_Blob) );
         3028  +      assert( pRec->n>=0 );
         3029  +      len = (u32)pRec->n;
         3030  +      serial_type = (len*2) + 12 + ((pRec->flags & MEM_Str)!=0);
         3031  +      if( pRec->flags & MEM_Zero ){
         3032  +        serial_type += pRec->u.nZero*2;
         3033  +        if( nData ){
         3034  +          if( sqlite3VdbeMemExpandBlob(pRec) ) goto no_mem;
         3035  +          len += pRec->u.nZero;
         3036  +        }else{
         3037  +          nZero += pRec->u.nZero;
         3038  +        }
         3039  +      }
         3040  +      nData += len;
         3041  +      nHdr += sqlite3VarintLen(serial_type);
         3042  +      pRec->uTemp = serial_type;
  2952   3043       }
  2953         -    nData += len;
  2954         -    testcase( serial_type==127 );
  2955         -    testcase( serial_type==128 );
  2956         -    nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
  2957         -    pRec->uTemp = serial_type;
  2958   3044       if( pRec==pData0 ) break;
  2959   3045       pRec--;
  2960   3046     }while(1);
  2961   3047   
  2962   3048     /* EVIDENCE-OF: R-22564-11647 The header begins with a single varint
  2963   3049     ** which determines the total number of bytes in the header. The varint
  2964   3050     ** value is the size of the header in bytes including the size varint
................................................................................
  3286   3372           (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
  3287   3373           (iRollback)?"cannot rollback - no transaction is active":
  3288   3374                      "cannot commit - no transaction is active"));
  3289   3375            
  3290   3376       rc = SQLITE_ERROR;
  3291   3377       goto abort_due_to_error;
  3292   3378     }
  3293         -  break;
         3379  +  /*NOTREACHED*/ assert(0);
  3294   3380   }
  3295   3381   
  3296   3382   /* Opcode: Transaction P1 P2 P3 P4 P5
  3297   3383   **
  3298   3384   ** Begin a transaction on database P1 if a transaction is not already
  3299   3385   ** active.
  3300   3386   ** If P2 is non-zero, then a write-transaction is started, or if a 
................................................................................
  4042   4128       iKey = sqlite3VdbeIntValue(pIn3);
  4043   4129   
  4044   4130       /* If the P3 value could not be converted into an integer without
  4045   4131       ** loss of information, then special processing is required... */
  4046   4132       if( (pIn3->flags & (MEM_Int|MEM_IntReal))==0 ){
  4047   4133         if( (pIn3->flags & MEM_Real)==0 ){
  4048   4134           if( (pIn3->flags & MEM_Null) || oc>=OP_SeekGE ){
  4049         -          VdbeBranchTaken(1,2); goto jump_to_p2;
  4050         -          break;
         4135  +          VdbeBranchTaken(1,2);
         4136  +          goto jump_to_p2;
  4051   4137           }else{
  4052   4138             rc = sqlite3BtreeLast(pC->uc.pCursor, &res);
  4053   4139             if( rc!=SQLITE_OK ) goto abort_due_to_error;
  4054   4140             goto seek_not_found;
  4055   4141           }
  4056   4142         }else
  4057   4143   

Changes to src/vdbeInt.h.

   482    482   void sqlite3VdbeError(Vdbe*, const char *, ...);
   483    483   void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
   484    484   void sqliteVdbePopStack(Vdbe*,int);
   485    485   int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
   486    486   int sqlite3VdbeCursorRestore(VdbeCursor*);
   487    487   u32 sqlite3VdbeSerialTypeLen(u32);
   488    488   u8 sqlite3VdbeOneByteSerialTypeLen(u8);
          489  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
   489    490   u32 sqlite3VdbeSerialType(Mem*, int, u32*);
          491  +#endif
   490    492   u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
   491    493   u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
   492    494   void sqlite3VdbeDeleteAuxData(sqlite3*, AuxData**, int, int);
   493    495   
   494    496   int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
   495    497   int sqlite3VdbeIdxKeyCompare(sqlite3*,VdbeCursor*,UnpackedRecord*,int*);
   496    498   int sqlite3VdbeIdxRowid(sqlite3*, BtCursor*, i64*);

Changes to src/vdbeaux.c.

  1905   1905         ** main program. */
  1906   1906         pOp = &p->aOp[i];
  1907   1907       }else{
  1908   1908         /* We are currently listing subprograms.  Figure out which one and
  1909   1909         ** pick up the appropriate opcode. */
  1910   1910         int j;
  1911   1911         i -= p->nOp;
         1912  +      assert( apSub!=0 );
         1913  +      assert( nSub>0 );
  1912   1914         for(j=0; i>=apSub[j]->nOp; j++){
  1913   1915           i -= apSub[j]->nOp;
         1916  +        assert( i<apSub[j]->nOp || j+1<nSub );
  1914   1917         }
  1915   1918         pOp = &apSub[j]->aOp[i];
  1916   1919       }
  1917   1920   
  1918   1921       /* When an OP_Program opcode is encounter (the only opcode that has
  1919   1922       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
  1920   1923       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
................................................................................
  3428   3431   **    N>=12 and even       (N-12)/2        BLOB
  3429   3432   **    N>=13 and odd        (N-13)/2        text
  3430   3433   **
  3431   3434   ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
  3432   3435   ** of SQLite will not understand those serial types.
  3433   3436   */
  3434   3437   
         3438  +#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
  3435   3439   /*
  3436   3440   ** Return the serial-type for the value stored in pMem.
  3437   3441   **
  3438   3442   ** This routine might convert a large MEM_IntReal value into MEM_Real.
         3443  +**
         3444  +** 2019-07-11:  The primary user of this subroutine was the OP_MakeRecord
         3445  +** opcode in the byte-code engine.  But by moving this routine in-line, we
         3446  +** can omit some redundant tests and make that opcode a lot faster.  So
         3447  +** this routine is now only used by the STAT3/4 logic.
  3439   3448   */
  3440   3449   u32 sqlite3VdbeSerialType(Mem *pMem, int file_format, u32 *pLen){
  3441   3450     int flags = pMem->flags;
  3442   3451     u32 n;
  3443   3452   
  3444   3453     assert( pLen!=0 );
  3445   3454     if( flags&MEM_Null ){
................................................................................
  3492   3501     n = (u32)pMem->n;
  3493   3502     if( flags & MEM_Zero ){
  3494   3503       n += pMem->u.nZero;
  3495   3504     }
  3496   3505     *pLen = n;
  3497   3506     return ((n*2) + 12 + ((flags&MEM_Str)!=0));
  3498   3507   }
         3508  +#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
  3499   3509   
  3500   3510   /*
  3501   3511   ** The sizes for serial types less than 128
  3502   3512   */
  3503   3513   static const u8 sqlite3SmallTypeSizes[] = {
  3504   3514           /*  0   1   2   3   4   5   6   7   8   9 */   
  3505   3515   /*   0 */   0,  1,  2,  3,  4,  6,  8,  8,  0,  0,
................................................................................
  4598   4608       if( (szHdr + nStr) > nKey1 ){
  4599   4609         pPKey2->errCode = (u8)SQLITE_CORRUPT_BKPT;
  4600   4610         return 0;    /* Corruption */
  4601   4611       }
  4602   4612       nCmp = MIN( pPKey2->aMem[0].n, nStr );
  4603   4613       res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
  4604   4614   
  4605         -    if( res==0 ){
         4615  +    if( res>0 ){
         4616  +      res = pPKey2->r2;
         4617  +    }else if( res<0 ){
         4618  +      res = pPKey2->r1;
         4619  +    }else{
  4606   4620         res = nStr - pPKey2->aMem[0].n;
  4607   4621         if( res==0 ){
  4608   4622           if( pPKey2->nField>1 ){
  4609   4623             res = sqlite3VdbeRecordCompareWithSkip(nKey1, pKey1, pPKey2, 1);
  4610   4624           }else{
  4611   4625             res = pPKey2->default_rc;
  4612   4626             pPKey2->eqSeen = 1;
  4613   4627           }
  4614   4628         }else if( res>0 ){
  4615   4629           res = pPKey2->r2;
  4616   4630         }else{
  4617   4631           res = pPKey2->r1;
  4618   4632         }
  4619         -    }else if( res>0 ){
  4620         -      res = pPKey2->r2;
  4621         -    }else{
  4622         -      res = pPKey2->r1;
  4623   4633       }
  4624   4634     }
  4625   4635   
  4626   4636     assert( vdbeRecordCompareDebug(nKey1, pKey1, pPKey2, res)
  4627   4637          || CORRUPT_DB
  4628   4638          || pPKey2->pKeyInfo->db->mallocFailed
  4629   4639     );

Changes to src/vdbeblob.c.

   357    357   */
   358    358   int sqlite3_blob_close(sqlite3_blob *pBlob){
   359    359     Incrblob *p = (Incrblob *)pBlob;
   360    360     int rc;
   361    361     sqlite3 *db;
   362    362   
   363    363     if( p ){
          364  +    sqlite3_stmt *pStmt = p->pStmt;
   364    365       db = p->db;
   365    366       sqlite3_mutex_enter(db->mutex);
   366         -    rc = sqlite3_finalize(p->pStmt);
   367    367       sqlite3DbFree(db, p);
   368    368       sqlite3_mutex_leave(db->mutex);
          369  +    rc = sqlite3_finalize(pStmt);
   369    370     }else{
   370    371       rc = SQLITE_OK;
   371    372     }
   372    373     return rc;
   373    374   }
   374    375   
   375    376   /*

Changes to src/vdbesort.c.

  1724   1724   
  1725   1725     if( rc==SQLITE_OK ){
  1726   1726       if( i==nWorker ){
  1727   1727         /* Use the foreground thread for this operation */
  1728   1728         rc = vdbeSorterListToPMA(&pSorter->aTask[nWorker], &pSorter->list);
  1729   1729       }else{
  1730   1730         /* Launch a background thread for this operation */
  1731         -      u8 *aMem = pTask->list.aMemory;
  1732         -      void *pCtx = (void*)pTask;
         1731  +      u8 *aMem;
         1732  +      void *pCtx;
  1733   1733   
         1734  +      assert( pTask!=0 );
  1734   1735         assert( pTask->pThread==0 && pTask->bDone==0 );
  1735   1736         assert( pTask->list.pList==0 );
  1736   1737         assert( pTask->list.aMemory==0 || pSorter->list.aMemory!=0 );
  1737   1738   
         1739  +      aMem = pTask->list.aMemory;
         1740  +      pCtx = (void*)pTask;
  1738   1741         pSorter->iPrev = (u8)(pTask - pSorter->aTask);
  1739   1742         pTask->list = pSorter->list;
  1740   1743         pSorter->list.pList = 0;
  1741   1744         pSorter->list.szPMA = 0;
  1742   1745         if( aMem ){
  1743   1746           pSorter->list.aMemory = aMem;
  1744   1747           pSorter->nMemory = sqlite3MallocSize(aMem);

Changes to src/wal.c.

  3474   3474         bSync = (w.iSyncPoint==iOffset);
  3475   3475         testcase( bSync );
  3476   3476         while( iOffset<w.iSyncPoint ){
  3477   3477           rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
  3478   3478           if( rc ) return rc;
  3479   3479           iOffset += szFrame;
  3480   3480           nExtra++;
         3481  +        assert( pLast!=0 );
  3481   3482         }
  3482   3483       }
  3483   3484       if( bSync ){
  3484   3485         assert( rc==SQLITE_OK );
  3485   3486         rc = sqlite3OsSync(w.pFd, WAL_SYNC_FLAGS(sync_flags));
  3486   3487       }
  3487   3488     }
................................................................................
  3506   3507     */
  3507   3508     iFrame = pWal->hdr.mxFrame;
  3508   3509     for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
  3509   3510       if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
  3510   3511       iFrame++;
  3511   3512       rc = walIndexAppend(pWal, iFrame, p->pgno);
  3512   3513     }
         3514  +  assert( pLast!=0 || nExtra==0 );
  3513   3515     while( rc==SQLITE_OK && nExtra>0 ){
  3514   3516       iFrame++;
  3515   3517       nExtra--;
  3516   3518       rc = walIndexAppend(pWal, iFrame, pLast->pgno);
  3517   3519     }
  3518   3520   
  3519   3521     if( rc==SQLITE_OK ){

Changes to src/walker.c.

    21     21   /*
    22     22   ** Walk all expressions linked into the list of Window objects passed
    23     23   ** as the second argument.
    24     24   */
    25     25   static int walkWindowList(Walker *pWalker, Window *pList){
    26     26     Window *pWin;
    27     27     for(pWin=pList; pWin; pWin=pWin->pNextWin){
    28         -    if( sqlite3WalkExprList(pWalker, pWin->pOrderBy) ) return WRC_Abort;
    29         -    if( sqlite3WalkExprList(pWalker, pWin->pPartition) ) return WRC_Abort;
    30         -    if( sqlite3WalkExpr(pWalker, pWin->pFilter) ) return WRC_Abort;
           28  +    int rc;
           29  +    rc = sqlite3WalkExprList(pWalker, pWin->pOrderBy);
           30  +    if( rc ) return WRC_Abort;
           31  +    rc = sqlite3WalkExprList(pWalker, pWin->pPartition);
           32  +    if( rc ) return WRC_Abort;
           33  +    rc = sqlite3WalkExpr(pWalker, pWin->pFilter);
           34  +    if( rc ) return WRC_Abort;
           35  +
           36  +    /* The next two are purely for calls to sqlite3RenameExprUnmap()
           37  +    ** within sqlite3WindowOffsetExpr().  Because of constraints imposed
           38  +    ** by sqlite3WindowOffsetExpr(), they can never fail.  The results do
           39  +    ** not matter anyhow. */
           40  +    rc = sqlite3WalkExpr(pWalker, pWin->pStart);
           41  +    if( NEVER(rc) ) return WRC_Abort;
           42  +    rc = sqlite3WalkExpr(pWalker, pWin->pEnd);
           43  +    if( NEVER(rc) ) return WRC_Abort;
    31     44     }
    32     45     return WRC_Continue;
    33     46   }
    34     47   #endif
    35     48   
    36     49   /*
    37     50   ** Walk an expression tree.  Invoke the callback once for each node
................................................................................
    59     72     while(1){
    60     73       rc = pWalker->xExprCallback(pWalker, pExpr);
    61     74       if( rc ) return rc & WRC_Abort;
    62     75       if( !ExprHasProperty(pExpr,(EP_TokenOnly|EP_Leaf)) ){
    63     76         if( pExpr->pLeft && walkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
    64     77          assert( pExpr->x.pList==0 || pExpr->pRight==0 );
    65     78         if( pExpr->pRight ){
           79  +        assert( !ExprHasProperty(pExpr, EP_WinFunc) );
    66     80           pExpr = pExpr->pRight;
    67     81           continue;
    68     82         }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
           83  +        assert( !ExprHasProperty(pExpr, EP_WinFunc) );
    69     84           if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
    70         -      }else if( pExpr->x.pList ){
    71         -        if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
    72         -      }
           85  +      }else{
           86  +        if( pExpr->x.pList ){
           87  +          if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
           88  +        }
    73     89   #ifndef SQLITE_OMIT_WINDOWFUNC
    74         -      if( ExprHasProperty(pExpr, EP_WinFunc) ){
    75         -        if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
    76         -      }
           90  +        if( ExprHasProperty(pExpr, EP_WinFunc) ){
           91  +          if( walkWindowList(pWalker, pExpr->y.pWin) ) return WRC_Abort;
           92  +        }
    77     93   #endif
           94  +      }
    78     95       }
    79     96       break;
    80     97     }
    81     98     return WRC_Continue;
    82     99   }
    83    100   int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
    84    101     return pExpr ? walkExpr(pWalker,pExpr) : WRC_Continue;
................................................................................
   112    129     if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
   113    130     if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
   114    131     if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
   115    132   #if !defined(SQLITE_OMIT_WINDOWFUNC) && !defined(SQLITE_OMIT_ALTERTABLE)
   116    133     {
   117    134       Parse *pParse = pWalker->pParse;
   118    135       if( pParse && IN_RENAME_OBJECT ){
          136  +      /* The following may return WRC_Abort if there are unresolvable
          137  +      ** symbols (e.g. a table that does not exist) in a window definition. */
   119    138         int rc = walkWindowList(pWalker, p->pWinDefn);
   120         -      assert( rc==WRC_Continue );
   121    139         return rc;
   122    140       }
   123    141     }
   124    142   #endif
   125    143     return WRC_Continue;
   126    144   }
   127    145   

Changes to src/where.c.

  2266   2266     WhereTerm *pTerm, *pX;
  2267   2267     Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
  2268   2268     int i, j, k;
  2269   2269     LogEst iReduce = 0;    /* pLoop->nOut should not exceed nRow-iReduce */
  2270   2270   
  2271   2271     assert( (pLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
  2272   2272     for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
         2273  +    assert( pTerm!=0 );
  2273   2274       if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
  2274   2275       if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
  2275   2276       if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
  2276   2277       for(j=pLoop->nLTerm-1; j>=0; j--){
  2277   2278         pX = pLoop->aLTerm[j];
  2278   2279         if( pX==0 ) continue;
  2279   2280         if( pX==pTerm ) break;

Changes to src/window.c.

   744    744   ** Callback function used by selectWindowRewriteEList(). If necessary,
   745    745   ** this function appends to the output expression-list and updates 
   746    746   ** expression (*ppExpr) in place.
   747    747   */
   748    748   static int selectWindowRewriteExprCb(Walker *pWalker, Expr *pExpr){
   749    749     struct WindowRewrite *p = pWalker->u.pRewrite;
   750    750     Parse *pParse = pWalker->pParse;
          751  +  assert( p!=0 );
          752  +  assert( p->pWin!=0 );
   751    753   
   752    754     /* If this function is being called from within a scalar sub-select
   753    755     ** that used by the SELECT statement being processed, only process
   754    756     ** TK_COLUMN expressions that refer to it (the outer SELECT). Do
   755    757     ** not process aggregates or window functions at all, as they belong
   756    758     ** to the scalar sub-select.  */
   757    759     if( p->pSubSelect ){
................................................................................
   843    845     ExprList *pEList,               /* Rewrite expressions in this list */
   844    846     Table *pTab,
   845    847     ExprList **ppSub                /* IN/OUT: Sub-select expression-list */
   846    848   ){
   847    849     Walker sWalker;
   848    850     WindowRewrite sRewrite;
   849    851   
          852  +  assert( pWin!=0 );
   850    853     memset(&sWalker, 0, sizeof(Walker));
   851    854     memset(&sRewrite, 0, sizeof(WindowRewrite));
   852    855   
   853    856     sRewrite.pSub = *ppSub;
   854    857     sRewrite.pWin = pWin;
   855    858     sRewrite.pSrc = pSrc;
   856    859     sRewrite.pTab = pTab;
................................................................................
  1009   1012       }
  1010   1013       if( db->mallocFailed ) rc = SQLITE_NOMEM;
  1011   1014       sqlite3DbFree(db, pTab);
  1012   1015     }
  1013   1016   
  1014   1017     return rc;
  1015   1018   }
         1019  +
         1020  +/*
         1021  +** Unlink the Window object from the Select to which it is attached,
         1022  +** if it is attached.
         1023  +*/
         1024  +void sqlite3WindowUnlinkFromSelect(Window *p){
         1025  +  if( p->ppThis ){
         1026  +    *p->ppThis = p->pNextWin;
         1027  +    if( p->pNextWin ) p->pNextWin->ppThis = p->ppThis;
         1028  +    p->ppThis = 0;
         1029  +  }
         1030  +}
  1016   1031   
  1017   1032   /*
  1018   1033   ** Free the Window object passed as the second argument.
  1019   1034   */
  1020   1035   void sqlite3WindowDelete(sqlite3 *db, Window *p){
  1021   1036     if( p ){
         1037  +    sqlite3WindowUnlinkFromSelect(p);
  1022   1038       sqlite3ExprDelete(db, p->pFilter);
  1023   1039       sqlite3ExprListDelete(db, p->pPartition);
  1024   1040       sqlite3ExprListDelete(db, p->pOrderBy);
  1025   1041       sqlite3ExprDelete(db, p->pEnd);
  1026   1042       sqlite3ExprDelete(db, p->pStart);
  1027   1043       sqlite3DbFree(db, p->zName);
  1028   1044       sqlite3DbFree(db, p->zBase);
................................................................................
  1192   1208   
  1193   1209   /*
  1194   1210   ** Attach window object pWin to expression p.
  1195   1211   */
  1196   1212   void sqlite3WindowAttach(Parse *pParse, Expr *p, Window *pWin){
  1197   1213     if( p ){
  1198   1214       assert( p->op==TK_FUNCTION );
  1199         -    /* This routine is only called for the parser.  If pWin was not
  1200         -    ** allocated due to an OOM, then the parser would fail before ever
  1201         -    ** invoking this routine */
  1202         -    if( ALWAYS(pWin) ){
  1203         -      p->y.pWin = pWin;
  1204         -      ExprSetProperty(p, EP_WinFunc);
  1205         -      pWin->pOwner = p;
  1206         -      if( p->flags & EP_Distinct ){
  1207         -        sqlite3ErrorMsg(pParse,
  1208         -           "DISTINCT is not supported for window functions");
  1209         -      }
         1215  +    assert( pWin );
         1216  +    p->y.pWin = pWin;
         1217  +    ExprSetProperty(p, EP_WinFunc);
         1218  +    pWin->pOwner = p;
         1219  +    if( (p->flags & EP_Distinct) && pWin->eFrmType!=TK_FILTER ){
         1220  +      sqlite3ErrorMsg(pParse,
         1221  +          "DISTINCT is not supported for window functions"
         1222  +      );
  1210   1223       }
  1211   1224     }else{
  1212   1225       sqlite3WindowDelete(pParse->db, pWin);
  1213   1226     }
  1214   1227   }
  1215   1228   
  1216   1229   /*
  1217   1230   ** Return 0 if the two window objects are identical, or non-zero otherwise.
  1218   1231   ** Identical window objects can be processed in a single scan.
  1219   1232   */
  1220         -int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2){
         1233  +int sqlite3WindowCompare(Parse *pParse, Window *p1, Window *p2, int bFilter){
  1221   1234     if( p1->eFrmType!=p2->eFrmType ) return 1;
  1222   1235     if( p1->eStart!=p2->eStart ) return 1;
  1223   1236     if( p1->eEnd!=p2->eEnd ) return 1;
  1224   1237     if( p1->eExclude!=p2->eExclude ) return 1;
  1225   1238     if( sqlite3ExprCompare(pParse, p1->pStart, p2->pStart, -1) ) return 1;
  1226   1239     if( sqlite3ExprCompare(pParse, p1->pEnd, p2->pEnd, -1) ) return 1;
  1227   1240     if( sqlite3ExprListCompare(p1->pPartition, p2->pPartition, -1) ) return 1;
  1228   1241     if( sqlite3ExprListCompare(p1->pOrderBy, p2->pOrderBy, -1) ) return 1;
         1242  +  if( bFilter ){
         1243  +    if( sqlite3ExprCompare(pParse, p1->pFilter, p2->pFilter, -1) ) return 1;
         1244  +  }
  1229   1245     return 0;
  1230   1246   }
  1231   1247   
  1232   1248   
  1233   1249   /*
  1234   1250   ** This is called by code in select.c before it calls sqlite3WhereBegin()
  1235   1251   ** to begin iterating through the sub-query results. It is used to allocate
................................................................................
  1563   1579     int regRowid = 0;               /* AggStep rowid value */
  1564   1580     int regPeer = 0;                /* AggStep peer values */
  1565   1581   
  1566   1582     int nPeer;
  1567   1583     int lblNext;
  1568   1584     int lblBrk;
  1569   1585     int addrNext;
  1570         -  int csr = pMWin->csrApp;
         1586  +  int csr;
  1571   1587   
         1588  +  assert( pMWin!=0 );
         1589  +  csr = pMWin->csrApp;
  1572   1590     nPeer = (pMWin->pOrderBy ? pMWin->pOrderBy->nExpr : 0);
  1573   1591   
  1574   1592     lblNext = sqlite3VdbeMakeLabel(pParse);
  1575   1593     lblBrk = sqlite3VdbeMakeLabel(pParse);
  1576   1594   
  1577   1595     regCRowid = sqlite3GetTempReg(pParse);
  1578   1596     regRowid = sqlite3GetTempReg(pParse);
................................................................................
  2013   2031         pNew->pFunc = p->pFunc;
  2014   2032         pNew->pPartition = sqlite3ExprListDup(db, p->pPartition, 0);
  2015   2033         pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, 0);
  2016   2034         pNew->eFrmType = p->eFrmType;
  2017   2035         pNew->eEnd = p->eEnd;
  2018   2036         pNew->eStart = p->eStart;
  2019   2037         pNew->eExclude = p->eExclude;
         2038  +      pNew->regResult = p->regResult;
  2020   2039         pNew->pStart = sqlite3ExprDup(db, p->pStart, 0);
  2021   2040         pNew->pEnd = sqlite3ExprDup(db, p->pEnd, 0);
  2022   2041         pNew->pOwner = pOwner;
  2023   2042       }
  2024   2043     }
  2025   2044     return pNew;
  2026   2045   }

Changes to test/altertab3.test.

    15     15   set testprefix altertab3
    16     16   
    17     17   # If SQLITE_OMIT_ALTERTABLE is defined, omit this file.
    18     18   ifcapable !altertable {
    19     19     finish_test
    20     20     return
    21     21   }
           22  +
    22     23   
    23     24   ifcapable windowfunc {
    24     25   do_execsql_test 1.0 {
    25     26     CREATE TABLE t1(a, b);
    26     27     CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
    27     28       SELECT sum(b) OVER w FROM t1 WINDOW w AS (ORDER BY a);
    28     29     END;
................................................................................
   233    234     SELECT sql FROM sqlite_master WHERE name='v1';
   234    235   } {
   235    236     {CREATE VIEW v1 AS SELECT * FROM t1 WHERE (
   236    237       SELECT t1.a FROM t1, t2
   237    238     ) IN () OR t1.a=5}
   238    239   }
   239    240   
          241  +#-------------------------------------------------------------------------
          242  +reset_db
          243  +do_execsql_test 11.1 {
          244  +  CREATE TABLE t1(
          245  +      a,b,c,d,e,f,g,h,j,jj,jjb,k,aa,bb,cc,dd,ee DEFAULT 3.14,
          246  +      ff DEFAULT('hiccup'),Wg NOD NULL DEFAULT(false)
          247  +  );
          248  +
          249  +  CREATE TRIGGER b AFTER INSERT ON t1 WHEN new.a BEGIN
          250  +    SELECT a, sum() w3 FROM t1 
          251  +    WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM abc));
          252  +  END;
          253  +}
          254  +
          255  +do_catchsql_test 11.2 {
          256  +  ALTER TABLE t1 RENAME TO t1x;
          257  +} {1 {error in trigger b: no such table: abc}}
          258  +
          259  +do_execsql_test 11.3 {
          260  +  DROP TRIGGER b;
          261  +  CREATE TRIGGER b AFTER INSERT ON t1 WHEN new.a BEGIN
          262  +    SELECT a, sum() w3 FROM t1 
          263  +    WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM t1));
          264  +  END;
          265  +} {}
          266  +
          267  +do_execsql_test 11.4 {
          268  +  ALTER TABLE t1 RENAME TO t1x;
          269  +  SELECT sql FROM sqlite_master WHERE name = 'b';
          270  +} {
          271  +{CREATE TRIGGER b AFTER INSERT ON "t1x" WHEN new.a BEGIN
          272  +    SELECT a, sum() w3 FROM "t1x" 
          273  +    WINDOW b AS (ORDER BY NOT EXISTS(SELECT 1 FROM "t1x"));
          274  +  END}
          275  +}
          276  +
          277  +#-------------------------------------------------------------------------
          278  +reset_db
          279  +do_execsql_test 12.1 {
          280  +CREATE TABLE t1(a,b,c,d,e,f,g,h,j,jj,Zjj,k,aQ,bb,cc,dd,ee DEFAULT 3.14,
          281  +ff DEFAULT('hiccup'),gg NOD NULL DEFAULT(false));
          282  +CREATE TRIGGER AFTER INSERT ON t1 WHEN new.a NOT NULL BEGIN
          283  +
          284  +SELECT b () OVER , dense_rank() OVER d, d () OVER w1
          285  +FROM t1
          286  +WINDOW
          287  +w1 AS
          288  +( w1 ORDER BY d
          289  +ROWS BETWEEN 2 NOT IN(SELECT a, sum(d) w2,max(d)OVER FROM t1
          290  +WINDOW
          291  +w1 AS
          292  +(PARTITION BY d
          293  +ROWS BETWEEN '' PRECEDING AND false FOLLOWING),
          294  +d AS
          295  +(PARTITION BY b ORDER BY d
          296  +ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
          297  +) PRECEDING AND 1 FOLLOWING),
          298  +w2 AS
          299  +(PARTITION BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW),
          300  +w3 AS
          301  +(PARTITION BY b ORDER BY d
          302  +ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)
          303  +;
          304  +SELECT a, sum(d) w2,max(d)OVER FROM t1
          305  +WINDOW
          306  +w1 AS
          307  +(PARTITION BY d
          308  +ROWS BETWEEN '' PRECEDING AND false FOLLOWING),
          309  +d AS
          310  +(PARTITION BY b ORDER BY d
          311  +ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
          312  +;
          313  +
          314  +END;
          315  +}
          316  +
          317  +do_execsql_test 12.2 {
          318  +  ALTER TABLE t1 RENAME TO t1x;
          319  +}
          320  +
          321  +#-------------------------------------------------------------------------
          322  +reset_db
          323  +do_execsql_test 13.1 {
          324  +  CREATE TABLE t1(a);
          325  +  CREATE TRIGGER r1 INSERT ON t1 BEGIN
          326  +    SELECT a(*) OVER (ORDER BY (SELECT 1)) FROM t1;
          327  +  END;
          328  +}
          329  +
          330  +do_execsql_test 13.2 {
          331  +  ALTER TABLE t1 RENAME TO t1x;
          332  +}
          333  +
          334  +#-------------------------------------------------------------------------
          335  +reset_db
          336  +do_execsql_test 14.1 {
          337  +  CREATE TABLE t1(a);
          338  +  CREATE TABLE t2(b);
          339  +  CREATE TRIGGER AFTER INSERT ON t1 BEGIN
          340  +    SELECT sum() FILTER (WHERE (SELECT sum() FILTER (WHERE 0)) AND a);
          341  +  END;
          342  +}
          343  +
          344  +do_catchsql_test 14.2 {
          345  +  ALTER TABLE t1 RENAME TO t1x;
          346  +} {1 {error in trigger AFTER: no such column: a}}
   240    347   
   241    348   finish_test
          349  +

Changes to test/check.test.

   490    490   forcedelete test.db
   491    491   sqlite3 db test.db
   492    492   do_execsql_test 10.1 {
   493    493     CREATE TABLE t1(x);
   494    494     CREATE VIEW v1(y) AS SELECT x FROM t1;
   495    495     PRAGMA integrity_check;
   496    496   } {ok}
          497  +
          498  +#-------------------------------------------------------------------------
          499  +reset_db
          500  +do_execsql_test 11.0 {
          501  +  CREATE TABLE t1 (Col0 CHECK(1 COLLATE BINARY BETWEEN 1 AND 1) ) ;
          502  +}
          503  +do_execsql_test 11.1 {
          504  +  INSERT INTO t1 VALUES (NULL);
          505  +}
          506  +do_execsql_test 11.2 {
          507  +  INSERT  INTO t1 VALUES (NULL);
          508  +}
          509  +
          510  +do_execsql_test 11.3 {
          511  +  CREATE TABLE t2(b, a CHECK(
          512  +      CASE 'abc' COLLATE nocase WHEN a THEN 1 ELSE 0 END)
          513  +  );
          514  +}
          515  +do_execsql_test 11.4 {
          516  +  INSERT INTO t2(a) VALUES('abc');
          517  +}
          518  +do_execsql_test 11.5 {
          519  +  INSERT INTO t2(b, a) VALUES(1, 'abc'||'');
          520  +}
          521  +do_execsql_test 11.6 {
          522  +  INSERT INTO t2(b, a) VALUES(2, 'abc');
          523  +}
          524  +
          525  +finish_test
          526  +
   497    527   
   498    528   finish_test

Added test/checkfault.test.

            1  +# 2019 July 17
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +#
           12  +# This file contains fault-injection test cases for the 
           13  +# sqlite3_db_cacheflush API.
           14  +#
           15  +
           16  +set testdir [file dirname $argv0]
           17  +source $testdir/tester.tcl
           18  +set testprefix cffault
           19  +source $testdir/malloc_common.tcl
           20  +
           21  +do_execsql_test 1.0 {
           22  +  CREATE TABLE t1 (Col0 CHECK(1 COLLATE BINARY BETWEEN 1 AND 1) ) ;
           23  +  CREATE TABLE t2(b, a CHECK(
           24  +      CASE 'abc' COLLATE nocase WHEN a THEN 1 ELSE 0 END)
           25  +  );
           26  +}
           27  +
           28  +do_faultsim_test 1.1 -faults oom* -body {
           29  +  execsql { INSERT INTO t1 VALUES ('ABCDEFG') }
           30  +} -test {
           31  +  faultsim_test_result {0 {}}
           32  +}
           33  +
           34  +do_faultsim_test 1.2 -faults oom* -body {
           35  +  execsql { INSERT INTO t2(a) VALUES('abc') }
           36  +} -test {
           37  +  faultsim_test_result {0 {}}
           38  +}
           39  +
           40  +
           41  +finish_test

Changes to test/close.test.

    74     74       sqlite3_prepare $DB "SELECT * FROM sqlite_master" -1 dummy
    75     75     } msg] $msg
    76     76   } {1 {(21) bad parameter or other API misuse}}
    77     77   
    78     78   do_test 1.4.4 {
    79     79     sqlite3_finalize $STMT
    80     80   } {SQLITE_OK}
           81  +
           82  +do_test 1.5 {
           83  +  set DB [sqlite3_open test.db]
           84  +  sqlite3_blob_open $DB main t1 x 2 0 BLOB
           85  +  sqlite3_close_v2 $DB
           86  +  sqlite3_blob_close $BLOB
           87  +} {}
    81     88   
    82     89   finish_test

Added test/filter1.test.

            1  +# 2018 May 8
            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.
           12  +#
           13  +
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +set testprefix filter1
           17  +
           18  +ifcapable !windowfunc {
           19  +  finish_test
           20  +  return
           21  +}
           22  +
           23  +do_execsql_test 1.0 {
           24  +  CREATE TABLE t1(a);
           25  +  CREATE INDEX i1 ON t1(a);
           26  +  INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9);
           27  +}
           28  +
           29  +do_execsql_test 1.1 { SELECT sum(a) FROM t1; } 45
           30  +do_execsql_test 1.2 { SELECT sum(a) FILTER( WHERE a<5 ) FROM t1; } 10
           31  +
           32  +do_execsql_test 1.3 { 
           33  +  SELECT sum(a) FILTER( WHERE a>9 ),
           34  +         sum(a) FILTER( WHERE a>8 ),
           35  +         sum(a) FILTER( WHERE a>7 ),
           36  +         sum(a) FILTER( WHERE a>6 ),
           37  +         sum(a) FILTER( WHERE a>5 ),
           38  +         sum(a) FILTER( WHERE a>4 ),
           39  +         sum(a) FILTER( WHERE a>3 ),
           40  +         sum(a) FILTER( WHERE a>2 ),
           41  +         sum(a) FILTER( WHERE a>1 ),
           42  +         sum(a) FILTER( WHERE a>0 )
           43  +  FROM t1;
           44  +} {{} 9 17 24 30 35 39 42 44 45}
           45  +
           46  +do_execsql_test 1.4 {
           47  +  SELECT max(a) FILTER (WHERE (a % 2)==0) FROM t1
           48  +} {8}
           49  +
           50  +do_execsql_test 1.5 {
           51  +  SELECT min(a) FILTER (WHERE a>4) FROM t1
           52  +} {5}
           53  +
           54  +do_execsql_test 1.6 {
           55  +  SELECT count(*) FILTER (WHERE a!=5) FROM t1
           56  +} {8}
           57  +
           58  +do_execsql_test 1.7 {
           59  +  SELECT min(a) FILTER (WHERE a>3) FROM t1 GROUP BY (a%2) ORDER BY 1;
           60  +} {4 5}
           61  +
           62  +do_execsql_test 1.8 {
           63  +  CREATE VIEW vv AS 
           64  +  SELECT sum(a) FILTER( WHERE a>9 ),
           65  +         sum(a) FILTER( WHERE a>8 ),
           66  +         sum(a) FILTER( WHERE a>7 ),
           67  +         sum(a) FILTER( WHERE a>6 ),
           68  +         sum(a) FILTER( WHERE a>5 ),
           69  +         sum(a) FILTER( WHERE a>4 ),
           70  +         sum(a) FILTER( WHERE a>3 ),
           71  +         sum(a) FILTER( WHERE a>2 ),
           72  +         sum(a) FILTER( WHERE a>1 ),
           73  +         sum(a) FILTER( WHERE a>0 )
           74  +  FROM t1;
           75  +  SELECT * FROM vv;
           76  +} {{} 9 17 24 30 35 39 42 44 45}
           77  +
           78  +
           79  +#-------------------------------------------------------------------------
           80  +# Test some errors:
           81  +#
           82  +#   .1 FILTER on a non-aggregate function,
           83  +#   .2 Window function in FILTER clause,
           84  +#   .3 Aggregate function in FILTER clause,
           85  +#
           86  +reset_db
           87  +do_execsql_test 2.0 {
           88  +  CREATE TABLE t1(a);
           89  +  INSERT INTO t1 VALUES(1), (2), (3), (4), (5), (6), (7), (8), (9);
           90  +}
           91  +
           92  +do_catchsql_test 2.1 {
           93  +  SELECT upper(a) FILTER (WHERE a=1) FROM t1
           94  +} {1 {FILTER may not be used with non-aggregate upper()}}
           95  +
           96  +do_catchsql_test 2.2 {
           97  +  SELECT sum(a) FILTER (WHERE 1 - max(a) OVER () > 0) FROM t1
           98  +} {1 {misuse of window function max()}}
           99  +
          100  +do_catchsql_test 2.3 {
          101  +  SELECT sum(a) FILTER (WHERE 1 - count(a)) FROM t1
          102  +} {1 {misuse of aggregate function count()}}
          103  +
          104  +finish_test
          105  +
          106  +

Added test/filter2.tcl.

            1  +# 2018 May 19
            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 $argv0] pg_common.tcl]
           14  +
           15  +#=========================================================================
           16  +
           17  +
           18  +start_test filter2 "2019 July 2"
           19  +
           20  +ifcapable !windowfunc
           21  +
           22  +execsql_test 1.0 {
           23  +  DROP TABLE IF EXISTS t1;
           24  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
           25  +  INSERT INTO t1 VALUES
           26  +   (1, 7), (2, 3), (3, 5), (4, 30), (5, 26), (6, 23), (7, 27),
           27  +   (8, 3), (9, 17), (10, 26), (11, 33), (12, 25), (13, NULL), (14, 47),
           28  +   (15, 36), (16, 13), (17, 45), (18, 31), (19, 11), (20, 36), (21, 37),
           29  +   (22, 21), (23, 22), (24, 14), (25, 16), (26, 3), (27, 7), (28, 29),
           30  +   (29, 50), (30, 38), (31, 3), (32, 36), (33, 12), (34, 4), (35, 46),
           31  +   (36, 3), (37, 48), (38, 23), (39, NULL), (40, 24), (41, 5), (42, 46),
           32  +   (43, 11), (44, NULL), (45, 18), (46, 25), (47, 15), (48, 18), (49, 23);
           33  +}
           34  +
           35  +execsql_test 1.1 { SELECT sum(b) FROM t1 }
           36  +
           37  +execsql_test 1.2 { SELECT sum(b) FILTER (WHERE a<10) FROM t1 }
           38  +
           39  +execsql_test 1.3 { SELECT count(DISTINCT b) FROM t1 }
           40  +
           41  +execsql_test 1.4 { SELECT count(DISTINCT b) FILTER (WHERE a!=19) FROM t1 }
           42  +
           43  +execsql_test 1.5 { 
           44  +  SELECT min(b) FILTER (WHERE a>19),
           45  +         min(b) FILTER (WHERE a>0),
           46  +         max(a+b) FILTER (WHERE a>19),
           47  +         max(b+a) FILTER (WHERE a BETWEEN 10 AND 40)
           48  +  FROM t1;
           49  +}
           50  +
           51  +execsql_test 1.6 { 
           52  +  SELECT min(b),
           53  +         min(b),
           54  +         max(a+b),
           55  +         max(b+a)
           56  +  FROM t1
           57  +  GROUP BY (a%10)
           58  +  ORDER BY 1, 2, 3, 4;
           59  +}
           60  +
           61  +execsql_test 1.7 { 
           62  +  SELECT min(b) FILTER (WHERE a>19),
           63  +         min(b) FILTER (WHERE a>0),
           64  +         max(a+b) FILTER (WHERE a>19),
           65  +         max(b+a) FILTER (WHERE a BETWEEN 10 AND 40)
           66  +  FROM t1
           67  +  GROUP BY (a%10)
           68  +  ORDER BY 1, 2, 3, 4;
           69  +}
           70  +
           71  +execsql_test 1.8 { 
           72  +  SELECT sum(a+b) FILTER (WHERE a=NULL) FROM t1
           73  +}
           74  +
           75  +execsql_test 1.9 {
           76  +  SELECT (a%5) FROM t1 GROUP BY (a%5) 
           77  +  HAVING sum(b) FILTER (WHERE b<20) > 34
           78  +  ORDER BY 1
           79  +}
           80  +
           81  +execsql_test 1.10 {
           82  +  SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb
           83  +  FROM t1
           84  +  GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34
           85  +  ORDER BY 1
           86  +}
           87  +
           88  +execsql_test 1.11 {
           89  +  SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb
           90  +  FROM t1
           91  +  GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34
           92  +  ORDER BY 2
           93  +}
           94  +
           95  +execsql_test 1.12 {
           96  +  SELECT (a%5), 
           97  +    sum(b) FILTER (WHERE b<20) AS bbb,
           98  +    count(distinct b) FILTER (WHERE b<20 OR a=13) AS ccc
           99  +  FROM t1 GROUP BY (a%5)
          100  +  ORDER BY 2
          101  +}
          102  +
          103  +execsql_test 1.13 {
          104  +  SELECT 
          105  +    string_agg(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=0),
          106  +    string_agg(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=1),
          107  +    count(*) FILTER (WHERE b%2!=0),
          108  +    count(*) FILTER (WHERE b%2!=1)
          109  +  FROM t1;
          110  +}
          111  +
          112  +execsql_float_test 1.14 {
          113  +  SELECT 
          114  +    avg(b) FILTER (WHERE b>a),
          115  +    avg(b) FILTER (WHERE b<a)
          116  +  FROM t1 GROUP BY (a%2) ORDER BY 1,2;
          117  +}
          118  +
          119  +execsql_test 1.15 {
          120  +  SELECT 
          121  +    a/5,
          122  +    sum(b) FILTER (WHERE a%5=0),
          123  +    sum(b) FILTER (WHERE a%5=1),
          124  +    sum(b) FILTER (WHERE a%5=2),
          125  +    sum(b) FILTER (WHERE a%5=3),
          126  +    sum(b) FILTER (WHERE a%5=4)
          127  +  FROM t1 GROUP BY (a/5) ORDER BY 1;
          128  +}
          129  +
          130  +finish_test
          131  +
          132  +

Added test/filter2.test.

            1  +# 2019 July 2
            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.
           12  +#
           13  +
           14  +####################################################
           15  +# DO NOT EDIT! THIS FILE IS AUTOMATICALLY GENERATED!
           16  +####################################################
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +set testprefix filter2
           21  +
           22  +ifcapable !windowfunc { finish_test ; return }
           23  +do_execsql_test 1.0 {
           24  +  DROP TABLE IF EXISTS t1;
           25  +  CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER);
           26  +  INSERT INTO t1 VALUES
           27  +   (1, 7), (2, 3), (3, 5), (4, 30), (5, 26), (6, 23), (7, 27),
           28  +   (8, 3), (9, 17), (10, 26), (11, 33), (12, 25), (13, NULL), (14, 47),
           29  +   (15, 36), (16, 13), (17, 45), (18, 31), (19, 11), (20, 36), (21, 37),
           30  +   (22, 21), (23, 22), (24, 14), (25, 16), (26, 3), (27, 7), (28, 29),
           31  +   (29, 50), (30, 38), (31, 3), (32, 36), (33, 12), (34, 4), (35, 46),
           32  +   (36, 3), (37, 48), (38, 23), (39, NULL), (40, 24), (41, 5), (42, 46),
           33  +   (43, 11), (44, NULL), (45, 18), (46, 25), (47, 15), (48, 18), (49, 23);
           34  +} {}
           35  +
           36  +do_execsql_test 1.1 {
           37  +  SELECT sum(b) FROM t1
           38  +} {1041}
           39  +
           40  +do_execsql_test 1.2 {
           41  +  SELECT sum(b) FILTER (WHERE a<10) FROM t1
           42  +} {141}
           43  +
           44  +do_execsql_test 1.3 {
           45  +  SELECT count(DISTINCT b) FROM t1
           46  +} {31}
           47  +
           48  +do_execsql_test 1.4 {
           49  +  SELECT count(DISTINCT b) FILTER (WHERE a!=19) FROM t1
           50  +} {31}
           51  +
           52  +do_execsql_test 1.5 {
           53  +  SELECT min(b) FILTER (WHERE a>19),
           54  +         min(b) FILTER (WHERE a>0),
           55  +         max(a+b) FILTER (WHERE a>19),
           56  +         max(b+a) FILTER (WHERE a BETWEEN 10 AND 40)
           57  +  FROM t1;
           58  +} {3 3 88 85}
           59  +
           60  +do_execsql_test 1.6 {
           61  +  SELECT min(b),
           62  +         min(b),
           63  +         max(a+b),
           64  +         max(b+a)
           65  +  FROM t1
           66  +  GROUP BY (a%10)
           67  +  ORDER BY 1, 2, 3, 4;
           68  +} {3 3 58 58   3 3 66 66   3 3 71 71   3 3 88 88   4 4 61 61   5 5 54 54
           69  +  7 7 85 85   11 11 79 79   16 16 81 81   24 24 68 68}
           70  +
           71  +do_execsql_test 1.7 {
           72  +  SELECT min(b) FILTER (WHERE a>19),
           73  +         min(b) FILTER (WHERE a>0),
           74  +         max(a+b) FILTER (WHERE a>19),
           75  +         max(b+a) FILTER (WHERE a BETWEEN 10 AND 40)
           76  +  FROM t1
           77  +  GROUP BY (a%10)
           78  +  ORDER BY 1, 2, 3, 4;
           79  +} {3 3 58 58   3 3 71 39   4 4 38 61   7 7 85 85   11 5 54 45   16 16 81 81
           80  +  18 3 66 61   21 3 88 68   23 11 79 79   24 24 68 68}
           81  +
           82  +do_execsql_test 1.8 {
           83  +  SELECT sum(a+b) FILTER (WHERE a=NULL) FROM t1
           84  +} {{}}
           85  +
           86  +do_execsql_test 1.9 {
           87  +  SELECT (a%5) FROM t1 GROUP BY (a%5) 
           88  +  HAVING sum(b) FILTER (WHERE b<20) > 34
           89  +  ORDER BY 1
           90  +} {3   4}
           91  +
           92  +do_execsql_test 1.10 {
           93  +  SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb
           94  +  FROM t1
           95  +  GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34
           96  +  ORDER BY 1
           97  +} {3 49   4 46}
           98  +
           99  +do_execsql_test 1.11 {
          100  +  SELECT (a%5), sum(b) FILTER (WHERE b<20) AS bbb
          101  +  FROM t1
          102  +  GROUP BY (a%5) HAVING sum(b) FILTER (WHERE b<20) >34
          103  +  ORDER BY 2
          104  +} {4 46   3 49}
          105  +
          106  +do_execsql_test 1.12 {
          107  +  SELECT (a%5), 
          108  +    sum(b) FILTER (WHERE b<20) AS bbb,
          109  +    count(distinct b) FILTER (WHERE b<20 OR a=13) AS ccc
          110  +  FROM t1 GROUP BY (a%5)
          111  +  ORDER BY 2
          112  +} {2 25 3   0 34 2   1 34 4   4 46 4   3 49 5}
          113  +
          114  +do_execsql_test 1.13 {
          115  +  SELECT 
          116  +    group_concat(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=0),
          117  +    group_concat(CAST(b AS TEXT), '_') FILTER (WHERE b%2!=1),
          118  +    count(*) FILTER (WHERE b%2!=0),
          119  +    count(*) FILTER (WHERE b%2!=1)
          120  +  FROM t1;
          121  +} {7_3_5_23_27_3_17_33_25_47_13_45_31_11_37_21_3_7_29_3_3_23_5_11_25_15_23 30_26_26_36_36_22_14_16_50_38_36_12_4_46_48_24_46_18_18 27 19}
          122  +
          123  +
          124  +do_test 1.14 {
          125  +  set myres {}
          126  +  foreach r [db eval {SELECT 
          127  +    avg(b) FILTER (WHERE b>a),
          128  +    avg(b) FILTER (WHERE b<a)
          129  +  FROM t1 GROUP BY (a%2) ORDER BY 1,2;}] {
          130  +    lappend myres [format %.4f [set r]]
          131  +  }
          132  +  set res2 {30.8333 13.7273 31.4167 13.0000}
          133  +  set i 0
          134  +  foreach r [set myres] r2 [set res2] {
          135  +    if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} {
          136  +      error "list element [set i] does not match: got=[set r] expected=[set r2]"
          137  +    }
          138  +    incr i
          139  +  }
          140  +  set {} {}
          141  +} {}
          142  +
          143  +do_execsql_test 1.15 {
          144  +  SELECT 
          145  +    a/5,
          146  +    sum(b) FILTER (WHERE a%5=0),
          147  +    sum(b) FILTER (WHERE a%5=1),
          148  +    sum(b) FILTER (WHERE a%5=2),
          149  +    sum(b) FILTER (WHERE a%5=3),
          150  +    sum(b) FILTER (WHERE a%5=4)
          151  +  FROM t1 GROUP BY (a/5) ORDER BY 1;
          152  +} {0 {} 7 3 5 30   1 26 23 27 3 17   2 26 33 25 {} 47   3 36 13 45 31 11
          153  +  4 36 37 21 22 14   5 16 3 7 29 50   6 38 3 36 12 4   7 46 3 48 23 {}
          154  +  8 24 5 46 11 {}   9 18 25 15 18 23}
          155  +
          156  +finish_test

Added test/filterfault.test.

            1  +# 2018 May 8
            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.
           12  +#
           13  +
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +set testprefix filterfault
           17  +
           18  +ifcapable !windowfunc {
           19  +  finish_test
           20  +  return
           21  +}
           22  +
           23  +do_execsql_test 1.0 {
           24  +  CREATE TABLE t1(a, b, c, d);
           25  +  INSERT INTO t1 VALUES(1, 2, 3, 4);
           26  +  INSERT INTO t1 VALUES(5, 6, 7, 8);
           27  +  INSERT INTO t1 VALUES(9, 10, 11, 12);
           28  +}
           29  +faultsim_save_and_close
           30  +
           31  +do_faultsim_test 1 -faults oom-t* -prep {
           32  +  faultsim_restore_and_reopen
           33  +} -body {
           34  +  execsql {
           35  +    SELECT sum(a) FILTER (WHERE b<5),
           36  +           count() FILTER (WHERE d!=c) 
           37  +      FROM t1 GROUP BY c ORDER BY 1;
           38  +  }
           39  +} -test {
           40  +  faultsim_test_result {0 {{} 1 {} 1 1 1}}
           41  +}
           42  +
           43  +
           44  +finish_test

Changes to test/fuzzdata7.db.

cannot compute difference between binary files

Changes to test/fuzzdata8.db.

cannot compute difference between binary files

Changes to test/select1.test.

  1095   1095   do_execsql_test select1-17.2 {
  1096   1096     SELECT * FROM t1,(SELECT * FROM t2 WHERE y=2 ORDER BY y,z LIMIT 4);
  1097   1097   } {1 2 3}
  1098   1098   do_execsql_test select1-17.3 {
  1099   1099     SELECT * FROM t1,(SELECT * FROM t2 WHERE y=2
  1100   1100            UNION ALL SELECT * FROM t2 WHERE y=3 ORDER BY y,z LIMIT 4);
  1101   1101   } {1 2 3}
         1102  +
         1103  +# 2019-07-24 Ticket https://sqlite.org/src/tktview/c52b09c7f38903b1311
         1104  +#
         1105  +do_execsql_test select1-18.1 {
         1106  +  DROP TABLE IF EXISTS t1;
         1107  +  DROP TABLE IF EXISTS t2;
         1108  +  CREATE TABLE t1(c);
         1109  +  CREATE TABLE t2(x PRIMARY KEY, y);
         1110  +  INSERT INTO t1(c) VALUES(123);
         1111  +  INSERT INTO t2(x) VALUES(123);
         1112  +  SELECT x FROM t2, t1 WHERE x BETWEEN c AND null OR x AND
         1113  +  x IN ((SELECT x FROM (SELECT x FROM t2, t1 
         1114  +  WHERE x BETWEEN (SELECT x FROM (SELECT x COLLATE rtrim 
         1115  +  FROM t2, t1 WHERE x BETWEEN c AND null
         1116  +  OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null
         1117  +  OR x AND x IN (c)) AND null
         1118  +  OR NOT EXISTS(SELECT -4.81 FROM t1, t2 WHERE x BETWEEN c AND null
         1119  +  OR x AND x IN ((SELECT x FROM (SELECT x FROM t2, t1
         1120  +  WHERE x BETWEEN (SELECT x FROM (SELECT x BETWEEN c AND null
         1121  +  OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null
         1122  +  OR x AND x IN (c)) AND null
         1123  +  OR x AND x IN (c)), t1 WHERE x BETWEEN c AND null
         1124  +  OR x AND x IN (c)))) AND x IN (c)
         1125  +  ), t1 WHERE x BETWEEN c AND null
         1126  +  OR x AND x IN (c)));
         1127  +} {}
         1128  +do_execsql_test select1-18.2 {
         1129  +  DROP TABLE IF EXISTS t1;
         1130  +  DROP TABLE IF EXISTS t2;
         1131  +  CREATE TABLE t1(c);
         1132  +  CREATE TABLE t2(x PRIMARY KEY, y);
         1133  +  INSERT INTO t1(c) VALUES(123);
         1134  +  INSERT INTO t2(x) VALUES(123);
         1135  +  SELECT x FROM t2, t1 WHERE x BETWEEN c AND (c+1) OR x AND
         1136  +  x IN ((SELECT x FROM (SELECT x FROM t2, t1 
         1137  +  WHERE x BETWEEN (SELECT x FROM (SELECT x COLLATE rtrim 
         1138  +  FROM t2, t1 WHERE x BETWEEN c AND (c+1)
         1139  +  OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1)
         1140  +  OR x AND x IN (c)) AND (c+1)
         1141  +  OR NOT EXISTS(SELECT -4.81 FROM t1, t2 WHERE x BETWEEN c AND (c+1)
         1142  +  OR x AND x IN ((SELECT x FROM (SELECT x FROM t2, t1
         1143  +  WHERE x BETWEEN (SELECT x FROM (SELECT x BETWEEN c AND (c+1)
         1144  +  OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1)
         1145  +  OR x AND x IN (c)) AND (c+1)
         1146  +  OR x AND x IN (c)), t1 WHERE x BETWEEN c AND (c+1)
         1147  +  OR x AND x IN (c)))) AND x IN (c)
         1148  +  ), t1 WHERE x BETWEEN c AND (c+1)
         1149  +  OR x AND x IN (c)));
         1150  +} {123}
         1151  +do_execsql_test select1-18.3 {
         1152  +  SELECT 1 FROM t1 WHERE (
         1153  +    SELECT 2 FROM t2 WHERE (
         1154  +      SELECT 3 FROM (
         1155  +        SELECT x FROM t2 WHERE x=c OR x=(SELECT x FROM (VALUES(0)))
         1156  +      ) WHERE x>c OR x=c
         1157  +    )
         1158  +  );
         1159  +} {1}
         1160  +do_execsql_test select1-18.4 {
         1161  +  SELECT 1 FROM t1, t2 WHERE (
         1162  +    SELECT 3 FROM (
         1163  +      SELECT x FROM t2 WHERE x=c OR x=(SELECT x FROM (VALUES(0)))
         1164  +    ) WHERE x>c OR x=c
         1165  +  );
         1166  +} {1}
  1102   1167   
  1103   1168   finish_test

Changes to test/vtab1.test.

   870    870     }
   871    871   } {31429}
   872    872   do_test vtab1.7-13 {
   873    873     execsql {
   874    874       SELECT rowid, a, b, c FROM real_abc
   875    875     }
   876    876   } {}
          877  +
          878  +# PRAGMA index_info and index_xinfo are no-ops on a virtual table
          879  +do_test vtab1.7-14 {
          880  +  execsql {
          881  +    PRAGMA index_info('echo_abc');
          882  +    PRAGMA index_xinfo('echo_abc');
          883  +  }
          884  +} {}
   877    885   
   878    886   ifcapable attach {
   879    887     do_test vtab1.8-1 {
   880    888       set echo_module ""
   881    889       execsql {
   882    890         ATTACH 'test2.db' AS aux;
   883    891         CREATE VIRTUAL TABLE aux.e2 USING echo(real_abc);

Changes to test/window1.test.

   253    253   } {1 {no such column: x}}
   254    254   do_catchsql_test 7.1.6 {
   255    255     SELECT trim(x) OVER (ORDER BY y) FROM t1;
   256    256   } {1 {trim() may not be used as a window function}}
   257    257   do_catchsql_test 7.1.7 {
   258    258     SELECT max(x) OVER abc FROM t1 WINDOW def AS (ORDER BY y);
   259    259   } {1 {no such window: abc}}
          260  +do_catchsql_test 7.1.8 {
          261  +  SELECT row_number(x) OVER () FROM t1
          262  +} {1 {wrong number of arguments to function row_number()}}
   260    263   
   261    264   do_execsql_test 7.2 {
   262    265     SELECT 
   263    266       lead(y) OVER win, 
   264    267       lead(y, 2) OVER win, 
   265    268       lead(y, 3, 'default') OVER win
   266    269     FROM t1
................................................................................
  1163   1166     13 M cc NULL JM | 
  1164   1167     3 C cc 1 {} | 
  1165   1168     4 D cc 8.25 {} | 
  1166   1169     12 L cc 'xyZ' L | 
  1167   1170     11 K cc 'xyz' K |
  1168   1171   }
  1169   1172   
  1170         -finish_test
         1173  +# 2019-07-18
         1174  +# Check-in [7ef7b23cbb1b9ace] (which was itself a fix for ticket
         1175  +# https://www.sqlite.org/src/info/1be72aab9) introduced a new problem
         1176  +# if the LHS of a BETWEEN operator is a WINDOW function.  The problem
         1177  +# was found by (the recently enhanced) dbsqlfuzz.
         1178  +#
         1179  +do_execsql_test 30.0 {
         1180  +  DROP TABLE IF EXISTS t1;
         1181  +  CREATE TABLE t1(a, b, c);
         1182  +  INSERT INTO t1 VALUES('BB','aa',399);
         1183  +  SELECT
         1184  +    count () OVER win1 NOT BETWEEN 'a' AND 'mmm',
         1185  +    count () OVER win3
         1186  +  FROM t1
         1187  +  WINDOW win1 AS (ORDER BY a GROUPS BETWEEN 4 PRECEDING AND 1 FOLLOWING
         1188  +                  EXCLUDE CURRENT ROW),
         1189  +         win2 AS (PARTITION BY b ORDER BY a),
         1190  +         win3 AS (win2 RANGE BETWEEN 5.2 PRECEDING AND true PRECEDING );
         1191  +} {1 1}
  1171   1192   
  1172         -
         1193  +finish_test

Changes to test/window2.tcl.

   420    420   execsql_float_test 4.9 {
   421    421     SELECT 
   422    422       rank() OVER win AS rank,
   423    423       cume_dist() OVER win AS cume_dist FROM t1
   424    424     WINDOW win AS (ORDER BY 1);
   425    425   }
   426    426   
          427  +execsql_test 4.10 {
          428  +  SELECT count(*) OVER (ORDER BY b) FROM t1
          429  +}
          430  +
          431  +execsql_test 4.11 {
          432  +  SELECT count(distinct a) FILTER (WHERE b='odd') FROM t1
          433  +}
   427    434   
   428    435   
   429    436   finish_test
   430    437   
   431    438   

Changes to test/window2.test.

   888    888       if {[set r]<([set r2]-0.0001) || [set r]>([set r2]+0.0001)} {
   889    889         error "list element [set i] does not match: got=[set r] expected=[set r2]"
   890    890       }
   891    891       incr i
   892    892     }
   893    893     set {} {}
   894    894   } {}
          895  +
          896  +do_execsql_test 4.10 {
          897  +  SELECT count(*) OVER (ORDER BY b) FROM t1
          898  +} {3   3   3   6   6   6}
          899  +
          900  +do_execsql_test 4.11 {
          901  +  SELECT count(distinct a) FILTER (WHERE b='odd') FROM t1
          902  +} {3}
   895    903   
   896    904   finish_test

Changes to test/window9.test.

    93     93   
    94     94   do_execsql_test 2.2.1 {
    95     95     SELECT b=='2' FROM t1
    96     96   } {1     0}
    97     97   do_execsql_test 2.2.2 {
    98     98     SELECT b=='2', rank() OVER (ORDER BY a) FROM t1
    99     99   } {1 1   0 2}
          100  +
          101  +#-------------------------------------------------------------------------
          102  +reset_db
          103  +do_execsql_test 3.0 {
          104  +  CREATE TABLE t1(a);
          105  +  CREATE TABLE t2(a,b,c);
          106  +}
          107  +
          108  +do_execsql_test 3.1 {
          109  +  SELECT EXISTS(SELECT 1 FROM t1 ORDER BY sum(a) OVER ()) FROM t1;
          110  +}
          111  +
          112  +do_execsql_test 3.2 {
          113  +  SELECT sum(a) OVER () FROM t2
          114  +   ORDER BY EXISTS(SELECT 1 FROM t2 ORDER BY sum(a) OVER ());
          115  +}
          116  +
          117  +do_catchsql_test 3.3 {
          118  +  SELECT a, sum(a) OVER (ORDER BY a DESC) FROM t2 
          119  +  ORDER BY EXISTS(
          120  +    SELECT 1 FROM t2 ORDER BY sum(a) OVER (ORDER BY a)
          121  +  ) OVER (ORDER BY a);
          122  +} {1 {near "OVER": syntax error}}
          123  +
          124  +do_catchsql_test 3.4 {
          125  +  SELECT y, y+1, y+2 FROM (
          126  +      SELECT c IN (
          127  +        SELECT min(a) OVER (),
          128  +        (abs(row_number() OVER())+22)/19,
          129  +        max(a) OVER () FROM t1
          130  +        ) AS y FROM t2
          131  +      );
          132  +} {1 {sub-select returns 3 columns - expected 1}}
          133  +
   100    134   
   101    135   finish_test
   102    136   

Changes to test/windowerr.tcl.

    60     60     WINDOW win AS (ROWS BETWEEN 'hello' PRECEDING AND 10 FOLLOWING)
    61     61   }
    62     62   errorsql_test 3.2 {
    63     63     SELECT sum(a) OVER win FROM t1
    64     64     WINDOW win AS (ROWS BETWEEN 10 PRECEDING AND x'ABCD' FOLLOWING)
    65     65   }
    66     66   
           67  +errorsql_test 3.3 {
           68  +  SELECT row_number(a) OVER () FROM t1;
           69  +}
    67     70   
    68     71   finish_test
    69     72   

Changes to test/windowerr.test.

   103    103   } } } 1
   104    104   
   105    105   # PG says ERROR:  argument of ROWS must be type bigint, not type bit
   106    106   do_test 3.2 { catch { execsql {
   107    107     SELECT sum(a) OVER win FROM t1
   108    108     WINDOW win AS (ROWS BETWEEN 10 PRECEDING AND x'ABCD' FOLLOWING)
   109    109   } } } 1
          110  +
          111  +# PG says ERROR:  function row_number(integer) does not exist
          112  +do_test 3.3 { catch { execsql {
          113  +  SELECT row_number(a) OVER () FROM t1;
          114  +} } } 1
   110    115   
   111    116   finish_test

Changes to test/without_rowid1.test.

    26     26     INSERT INTO t1 VALUES('dynamic','juliet','flipper','command');
    27     27     INSERT INTO t1 VALUES('journal','sherman','gamma','patriot');
    28     28     INSERT INTO t1 VALUES('arctic','sleep','ammonia','helena');
    29     29     SELECT *, '|' FROM t1 ORDER BY c, a;
    30     30   } {arctic sleep ammonia helena | journal sherman ammonia helena | dynamic juliet flipper command | journal sherman gamma patriot |}
    31     31   
    32     32   integrity_check without_rowid1-1.0ic
           33  +
           34  +do_execsql_test without_rowid1-1.0ixi {
           35  +  SELECT name, key FROM pragma_index_xinfo('t1');
           36  +} {c 1 a 1 b 0 d 0}
    33     37   
    34     38   do_execsql_test without_rowid1-1.1 {
    35     39     SELECT *, '|' FROM t1 ORDER BY +c, a;
    36     40   } {arctic sleep ammonia helena | journal sherman ammonia helena | dynamic juliet flipper command | journal sherman gamma patriot |}
    37     41   
    38     42   do_execsql_test without_rowid1-1.2 {
    39     43     SELECT *, '|' FROM t1 ORDER BY c DESC, a DESC;
................................................................................
   116    120     INSERT INTO t4 VALUES('abc', 'def');
   117    121     SELECT * FROM t4;
   118    122   } {abc def}
   119    123   do_execsql_test 2.1.2 {
   120    124     UPDATE t4 SET a = 'ABC';
   121    125     SELECT * FROM t4;
   122    126   } {ABC def}
          127  +do_execsql_test 2.1.3 {
          128  +  SELECT name, coll, key FROM pragma_index_xinfo('t4');
          129  +} {a nocase 1 b BINARY 0}
   123    130   
   124    131   do_execsql_test 2.2.1 {
   125    132     DROP TABLE t4;
   126    133     CREATE TABLE t4 (b, a COLLATE nocase PRIMARY KEY) WITHOUT ROWID;
   127    134     INSERT INTO t4(a, b) VALUES('abc', 'def');
   128    135     SELECT * FROM t4;
   129    136   } {def abc}
   130    137   
   131    138   do_execsql_test 2.2.2 {
   132    139     UPDATE t4 SET a = 'ABC', b = 'xyz';
   133    140     SELECT * FROM t4;
   134    141   } {xyz ABC}
          142  +
          143  +do_execsql_test 2.2.3 {
          144  +  SELECT name, coll, key FROM pragma_index_xinfo('t4');
          145  +} {a nocase 1 b BINARY 0}
          146  +
   135    147   
   136    148   do_execsql_test 2.3.1 {
   137    149     CREATE TABLE t5 (a, b, PRIMARY KEY(b, a)) WITHOUT ROWID;
   138    150     INSERT INTO t5(a, b) VALUES('abc', 'def');
   139    151     UPDATE t5 SET a='abc', b='def';
   140    152   } {}
          153  +
          154  +do_execsql_test 2.3.2 {
          155  +  SELECT name, coll, key FROM pragma_index_xinfo('t5');
          156  +} {b BINARY 1 a BINARY 1}
          157  +
   141    158   
   142    159   do_execsql_test 2.4.1 {
   143    160     CREATE TABLE t6 (
   144    161       a COLLATE nocase, b, c UNIQUE, PRIMARY KEY(b, a)
   145    162     ) WITHOUT ROWID;
   146    163   
   147    164     INSERT INTO t6(a, b, c) VALUES('abc', 'def', 'ghi');
................................................................................
   148    165     UPDATE t6 SET a='ABC', c='ghi';
   149    166   } {}
   150    167   
   151    168   do_execsql_test 2.4.2 {
   152    169     SELECT * FROM t6 ORDER BY b, a;
   153    170     SELECT * FROM t6 ORDER BY c;
   154    171   } {ABC def ghi ABC def ghi}
          172  +
          173  +do_execsql_test 2.4.3 {
          174  +  SELECT name, coll, key FROM pragma_index_xinfo('t6');
          175  +} {b BINARY 1 a nocase 1 c BINARY 0}
          176  +
   155    177   
   156    178   #-------------------------------------------------------------------------
   157    179   # Unless the destination table is completely empty, the xfer optimization 
   158    180   # is disabled for WITHOUT ROWID tables. The following tests check for
   159    181   # some problems that might occur if this were not the case.
   160    182   #
   161    183   reset_db

Changes to test/without_rowid6.test.

    20     20     CREATE TABLE t1(a,b,c,d,e, PRIMARY KEY(a,b,c,a,b,c,d,a,b,c)) WITHOUT ROWID;
    21     21     CREATE INDEX t1a ON t1(b, b);
    22     22     WITH RECURSIVE
    23     23       c(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM c WHERE i<1000)
    24     24     INSERT INTO t1(a,b,c,d,e) SELECT i, i+1000, printf('x%dy',i), 0, 0 FROM c;
    25     25     ANALYZE;
    26     26   } {}
           27  +do_execsql_test without_rowid6-101 {
           28  +  SELECT name, key FROM pragma_index_xinfo('t1');
           29  +} {a 1 b 1 c 1 d 1 e 0}
    27     30   do_execsql_test without_rowid6-110 {
    28     31     SELECT c FROM t1 WHERE a=123;
    29     32   } {x123y}
    30     33   do_execsql_test without_rowid6-120 {
    31     34     SELECT c FROM t1 WHERE b=1123;
    32     35   } {x123y}
    33     36   do_execsql_test without_rowid6-130 {
................................................................................
    47     50       b UNIQUE,
    48     51       c UNIQUE,
    49     52       PRIMARY KEY(b)
    50     53     ) WITHOUT ROWID;
    51     54     INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9);
    52     55     SELECT a FROM t1 WHERE b>3 ORDER BY b;
    53     56   } {4 1}
           57  +do_execsql_test without_rowid6-201 {
           58  +  SELECT name, key FROM pragma_index_xinfo('t1');
           59  +} {b 1 a 0 c 0}
    54     60   do_execsql_test without_rowid6-210 {
    55     61     EXPLAIN QUERY PLAN
    56     62     SELECT a FROM t1 WHERE b>3 ORDER BY b;
    57     63   } {/SEARCH TABLE t1 USING PRIMARY KEY .b>../}
    58     64   do_execsql_test without_rowid6-220 {
    59     65     PRAGMA index_list(t1);
    60     66   } {/sqlite_autoindex_t1_2 1 pk/}
................................................................................
   101    107     CREATE TABLE t1(a,b,c,
   102    108       UNIQUE(b,c),
   103    109       PRIMARY KEY(b,c)
   104    110     ) WITHOUT ROWID;
   105    111     INSERT INTO t1(a,b,c) VALUES(1,8,3),(4,5,6),(7,2,9);
   106    112     SELECT a FROM t1 WHERE b>3 ORDER BY b;
   107    113   } {4 1}
          114  +do_execsql_test without_rowid6-501 {
          115  +  SELECT name, key FROM pragma_index_xinfo('t1');
          116  +} {b 1 c 1 a 0}
   108    117   do_execsql_test without_rowid6-510 {
   109    118     EXPLAIN QUERY PLAN
   110    119     SELECT a FROM t1 WHERE b>3 ORDER BY b;
   111    120   } {/SEARCH TABLE t1 USING PRIMARY KEY .b>../}
   112    121   do_execsql_test without_rowid6-520 {
   113    122     PRAGMA index_list(t1);
   114    123   } {/sqlite_autoindex_t1_1 1 pk/}

Added test/without_rowid7.test.

            1  +# 2019 July 17
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#*************************************************************************
           11  +# This file implements regression tests for SQLite library.  
           12  +#
           13  +
           14  +set testdir [file dirname $argv0]
           15  +source $testdir/tester.tcl
           16  +set testprefix without_rowid7
           17  +
           18  +do_execsql_test 1.0 {
           19  +  CREATE TABLE t1(a, b COLLATE nocase, PRIMARY KEY(a, a, b)) WITHOUT ROWID;
           20  +}
           21  +
           22  +do_catchsql_test 1.1 {
           23  +  INSERT INTO t1 VALUES(1, 'one'), (1, 'ONE');
           24  +} {1 {UNIQUE constraint failed: t1.a, t1.b}}
           25  +
           26  +
           27  +do_execsql_test 2.0 {
           28  +  CREATE TABLE t2(a, b, PRIMARY KEY(a COLLATE nocase, a)) WITHOUT ROWID;
           29  +}
           30  +
           31  +do_execsql_test 2.1 {
           32  +  INSERT INTO t2 VALUES(1, 'one');
           33  +  SELECT b FROM t2;
           34  +} {one}
           35  +
           36  +do_execsql_test 2.2a {
           37  +  PRAGMA index_info(t2);
           38  +} {0 0 a 1 0 a}
           39  +do_execsql_test 2.2b {
           40  +  SELECT *, '|' FROM pragma_index_info('t2');
           41  +} {0 0 a | 1 0 a |}
           42  +do_execsql_test 2.3a {
           43  +  PRAGMA index_xinfo(t2);
           44  +} {0 0 a 0 nocase 1 1 0 a 0 BINARY 1 2 1 b 0 BINARY 0}
           45  +do_execsql_test 2.3b {
           46  +  SELECT *, '|' FROM pragma_index_xinfo('t2');
           47  +} {0 0 a 0 nocase 1 | 1 0 a 0 BINARY 1 | 2 1 b 0 BINARY 0 |}
           48  +
           49  +do_execsql_test 2.4 {
           50  +  CREATE TABLE t3(a, b, PRIMARY KEY(a COLLATE nocase, a));
           51  +  PRAGMA index_info(t3);
           52  +} {}
           53  +
           54  +
           55  +
           56  +finish_test

Changes to tool/lempar.c.

   563    563   #endif /* NDEBUG */
   564    564             return yy_action[j];
   565    565           }
   566    566         }
   567    567   #endif /* YYWILDCARD */
   568    568         return yy_default[stateno];
   569    569       }else{
          570  +      assert( i>=0 && i<sizeof(yy_action)/sizeof(yy_action[0]) );
   570    571         return yy_action[i];
   571    572       }
   572    573     }while(1);
   573    574   }
   574    575   
   575    576   /*
   576    577   ** Find the appropriate action for a parser given the non-terminal