Fossil

Check-in [63fb653a]
Login

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

Overview
Comment:Update the built-in SQLite to the latest 3.22 alpha version.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:63fb653a7e943ae192e46cb8dbbbe2c792407960b86381cf4b1290f6e4afeaf2
User & Date: drh 2017-12-06 20:53:09
Context
2017-12-06
21:11
Remove in-line JS from the /setup_skin page into the skin.js file. check-in: ca19f8b5 user: drh tags: trunk
20:53
Update the built-in SQLite to the latest 3.22 alpha version. check-in: 63fb653a user: drh tags: trunk
20:39
Remove calls to gebi() in JS modules. check-in: 652267ed user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/shell.c.

  2546   2546            || (z[i]==p->colSeparator[0] &&
  2547   2547                (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
  2548   2548           i = 0;
  2549   2549           break;
  2550   2550         }
  2551   2551       }
  2552   2552       if( i==0 ){
  2553         -      putc('"', out);
  2554         -      for(i=0; z[i]; i++){
  2555         -        if( z[i]=='"' ) putc('"', out);
  2556         -        putc(z[i], out);
  2557         -      }
  2558         -      putc('"', out);
         2553  +      char *zQuoted = sqlite3_mprintf("\"%w\"", z);
         2554  +      utf8_printf(out, "%s", zQuoted);
         2555  +      sqlite3_free(zQuoted);
  2559   2556       }else{
  2560   2557         utf8_printf(out, "%s", z);
  2561   2558       }
  2562   2559     }
  2563   2560     if( bSep ){
  2564   2561       utf8_printf(p->out, "%s", p->colSeparator);
  2565   2562     }
................................................................................
  5254   5251   
  5255   5252     /*
  5256   5253     ** This SELECT statement returns one row for each foreign key constraint
  5257   5254     ** in the schema of the main database. The column values are:
  5258   5255     **
  5259   5256     ** 0. The text of an SQL statement similar to:
  5260   5257     **
  5261         -  **      "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?"
         5258  +  **      "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
  5262   5259     **
  5263         -  **    This is the same SELECT that the foreign keys implementation needs
  5264         -  **    to run internally on child tables. If there is an index that can
         5260  +  **    This SELECT is similar to the one that the foreign keys implementation
         5261  +  **    needs to run internally on child tables. If there is an index that can
  5265   5262     **    be used to optimize this query, then it can also be used by the FK
  5266   5263     **    implementation to optimize DELETE or UPDATE statements on the parent
  5267   5264     **    table.
  5268   5265     **
  5269   5266     ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
  5270   5267     **    the EXPLAIN QUERY PLAN command matches this pattern, then the schema
  5271   5268     **    contains an index that can be used to optimize the query.
................................................................................
  5285   5282     **
  5286   5283     ** 5. The name of the parent table.
  5287   5284     **
  5288   5285     ** These six values are used by the C logic below to generate the report.
  5289   5286     */
  5290   5287     const char *zSql =
  5291   5288     "SELECT "
  5292         -    "     'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '"
         5289  +    "     'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
  5293   5290       "  || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
  5294   5291       "  || fkey_collate_clause("
  5295   5292       "       f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
  5296   5293       ", "
  5297   5294       "     'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
  5298   5295       "  || group_concat('*=?', ' AND ') || ')'"
  5299   5296       ", "
................................................................................
  7278   7275         sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
  7279   7276       }else{
  7280   7277         sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
  7281   7278       }
  7282   7279     }else
  7283   7280   
  7284   7281   #ifndef SQLITE_UNTESTABLE
  7285         -  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
         7282  +  if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
  7286   7283       static const struct {
  7287   7284          const char *zCtrlName;   /* Name of a test-control option */
  7288   7285          int ctrlCode;            /* Integer code for that option */
         7286  +       const char *zUsage;      /* Usage notes */
  7289   7287       } aCtrl[] = {
  7290         -      { "prng_save",             SQLITE_TESTCTRL_PRNG_SAVE              },
  7291         -      { "prng_restore",          SQLITE_TESTCTRL_PRNG_RESTORE           },
  7292         -      { "prng_reset",            SQLITE_TESTCTRL_PRNG_RESET             },
  7293         -      { "bitvec_test",           SQLITE_TESTCTRL_BITVEC_TEST            },
  7294         -      { "fault_install",         SQLITE_TESTCTRL_FAULT_INSTALL          },
  7295         -      { "benign_malloc_hooks",   SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS    },
  7296         -      { "pending_byte",          SQLITE_TESTCTRL_PENDING_BYTE           },
  7297         -      { "assert",                SQLITE_TESTCTRL_ASSERT                 },
  7298         -      { "always",                SQLITE_TESTCTRL_ALWAYS                 },
  7299         -      { "reserve",               SQLITE_TESTCTRL_RESERVE                },
  7300         -      { "optimizations",         SQLITE_TESTCTRL_OPTIMIZATIONS          },
  7301         -      { "iskeyword",             SQLITE_TESTCTRL_ISKEYWORD              },
  7302         -      { "byteorder",             SQLITE_TESTCTRL_BYTEORDER              },
  7303         -      { "never_corrupt",         SQLITE_TESTCTRL_NEVER_CORRUPT          },
  7304         -      { "imposter",              SQLITE_TESTCTRL_IMPOSTER               },
         7288  +      { "always",             SQLITE_TESTCTRL_ALWAYS,        "BOOLEAN"            },
         7289  +      { "assert",             SQLITE_TESTCTRL_ASSERT,        "BOOLEAN"            },
         7290  +    /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, ""          },*/
         7291  +    /*{ "bitvec_test",        SQLITE_TESTCTRL_BITVEC_TEST,   ""                },*/
         7292  +      { "byteorder",          SQLITE_TESTCTRL_BYTEORDER,     ""                   },
         7293  +    /*{ "fault_install",      SQLITE_TESTCTRL_FAULT_INSTALL, ""                }, */
         7294  +      { "imposter",           SQLITE_TESTCTRL_IMPOSTER,   "SCHEMA ON/OFF ROOTPAGE"},
         7295  +#ifdef SQLITE_N_KEYWORD
         7296  +      { "iskeyword",          SQLITE_TESTCTRL_ISKEYWORD,     "IDENTIFIER"         },
         7297  +#endif
         7298  +      { "localtime_fault",    SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN"           },
         7299  +      { "never_corrupt",      SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN"            },
         7300  +      { "optimizations",      SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK"       },
         7301  +      { "pending_byte",       SQLITE_TESTCTRL_PENDING_BYTE,  "OFFSET  "           },
         7302  +      { "prng_reset",         SQLITE_TESTCTRL_PRNG_RESET,    ""                   },
         7303  +      { "prng_restore",       SQLITE_TESTCTRL_PRNG_RESTORE,  ""                   },
         7304  +      { "prng_save",          SQLITE_TESTCTRL_PRNG_SAVE,     ""                   },
         7305  +      { "reserve",            SQLITE_TESTCTRL_RESERVE,       "BYTES-OF-RESERVE"   },
  7305   7306       };
  7306   7307       int testctrl = -1;
  7307         -    int rc2 = 0;
         7308  +    int iCtrl = -1;
         7309  +    int rc2 = 0;    /* 0: usage.  1: %d  2: %x  3: no-output */
         7310  +    int isOk = 0;
  7308   7311       int i, n2;
         7312  +    const char *zCmd = 0;
         7313  +
  7309   7314       open_db(p, 0);
         7315  +    zCmd = nArg>=2 ? azArg[1] : "help";
         7316  +
         7317  +    /* The argument can optionally begin with "-" or "--" */
         7318  +    if( zCmd[0]=='-' && zCmd[1] ){
         7319  +      zCmd++;
         7320  +      if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
         7321  +    }
         7322  +
         7323  +    /* --help lists all test-controls */
         7324  +    if( strcmp(zCmd,"help")==0 ){
         7325  +      utf8_printf(p->out, "Available test-controls:\n");
         7326  +      for(i=0; i<ArraySize(aCtrl); i++){
         7327  +        utf8_printf(p->out, "  .testctrl %s %s\n",
         7328  +                    aCtrl[i].zCtrlName, aCtrl[i].zUsage);
         7329  +      }
         7330  +      rc = 1;
         7331  +      goto meta_command_exit;
         7332  +    }
  7310   7333   
  7311   7334       /* convert testctrl text option to value. allow any unique prefix
  7312   7335       ** of the option name, or a numerical value. */
  7313         -    n2 = strlen30(azArg[1]);
         7336  +    n2 = strlen30(zCmd);
  7314   7337       for(i=0; i<ArraySize(aCtrl); i++){
  7315         -      if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
         7338  +      if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
  7316   7339           if( testctrl<0 ){
  7317   7340             testctrl = aCtrl[i].ctrlCode;
         7341  +          iCtrl = i;
  7318   7342           }else{
  7319         -          utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
  7320         -          testctrl = -1;
  7321         -          break;
         7343  +          utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
         7344  +                              "Use \".testctrl --help\" for help\n", zCmd);
         7345  +          rc = 1;
         7346  +          goto meta_command_exit;
  7322   7347           }
  7323   7348         }
  7324   7349       }
  7325         -    if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
  7326         -    if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
  7327         -      utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
         7350  +    if( testctrl<0 ){
         7351  +      utf8_printf(stderr,"Error: unknown test-control: %s\n"
         7352  +                         "Use \".testctrl --help\" for help\n", zCmd);
  7328   7353       }else{
  7329   7354         switch(testctrl){
  7330   7355   
  7331   7356           /* sqlite3_test_control(int, db, int) */
  7332   7357           case SQLITE_TESTCTRL_OPTIMIZATIONS:
  7333   7358           case SQLITE_TESTCTRL_RESERVE:
  7334   7359             if( nArg==3 ){
  7335   7360               int opt = (int)strtol(azArg[2], 0, 0);
  7336   7361               rc2 = sqlite3_test_control(testctrl, p->db, opt);
  7337         -            raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
  7338         -          } else {
  7339         -            utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
  7340         -                    azArg[1]);
         7362  +            isOk = 3;
  7341   7363             }
  7342   7364             break;
  7343   7365   
  7344   7366           /* sqlite3_test_control(int) */
  7345   7367           case SQLITE_TESTCTRL_PRNG_SAVE:
  7346   7368           case SQLITE_TESTCTRL_PRNG_RESTORE:
  7347   7369           case SQLITE_TESTCTRL_PRNG_RESET:
  7348   7370           case SQLITE_TESTCTRL_BYTEORDER:
  7349   7371             if( nArg==2 ){
  7350   7372               rc2 = sqlite3_test_control(testctrl);
  7351         -            raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
  7352         -          } else {
  7353         -            utf8_printf(stderr,"Error: testctrl %s takes no options\n",
  7354         -                        azArg[1]);
         7373  +            isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
  7355   7374             }
  7356   7375             break;
  7357   7376   
  7358   7377           /* sqlite3_test_control(int, uint) */
  7359   7378           case SQLITE_TESTCTRL_PENDING_BYTE:
  7360   7379             if( nArg==3 ){
  7361   7380               unsigned int opt = (unsigned int)integerValue(azArg[2]);
  7362   7381               rc2 = sqlite3_test_control(testctrl, opt);
  7363         -            raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
  7364         -          } else {
  7365         -            utf8_printf(stderr,"Error: testctrl %s takes a single unsigned"
  7366         -                           " int option\n", azArg[1]);
         7382  +            isOk = 3;
  7367   7383             }
  7368   7384             break;
  7369   7385   
  7370   7386           /* sqlite3_test_control(int, int) */
  7371   7387           case SQLITE_TESTCTRL_ASSERT:
  7372   7388           case SQLITE_TESTCTRL_ALWAYS:
         7389  +          if( nArg==3 ){
         7390  +            int opt = booleanValue(azArg[2]);
         7391  +            rc2 = sqlite3_test_control(testctrl, opt);
         7392  +            isOk = 1;
         7393  +          }
         7394  +          break;
         7395  +
         7396  +        /* sqlite3_test_control(int, int) */
         7397  +        case SQLITE_TESTCTRL_LOCALTIME_FAULT:
  7373   7398           case SQLITE_TESTCTRL_NEVER_CORRUPT:
  7374   7399             if( nArg==3 ){
  7375   7400               int opt = booleanValue(azArg[2]);
  7376   7401               rc2 = sqlite3_test_control(testctrl, opt);
  7377         -            raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
  7378         -          } else {
  7379         -            utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
  7380         -                            azArg[1]);
         7402  +            isOk = 3;
  7381   7403             }
  7382   7404             break;
  7383   7405   
  7384   7406           /* sqlite3_test_control(int, char *) */
  7385   7407   #ifdef SQLITE_N_KEYWORD
  7386   7408           case SQLITE_TESTCTRL_ISKEYWORD:
  7387   7409             if( nArg==3 ){
  7388   7410               const char *opt = azArg[2];
  7389   7411               rc2 = sqlite3_test_control(testctrl, opt);
  7390         -            raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
  7391         -          } else {
  7392         -            utf8_printf(stderr,
  7393         -                        "Error: testctrl %s takes a single char * option\n",
  7394         -                        azArg[1]);
         7412  +            isOk = 1;
  7395   7413             }
  7396   7414             break;
  7397   7415   #endif
  7398   7416   
  7399   7417           case SQLITE_TESTCTRL_IMPOSTER:
  7400   7418             if( nArg==5 ){
  7401   7419               rc2 = sqlite3_test_control(testctrl, p->db,
  7402   7420                             azArg[2],
  7403   7421                             integerValue(azArg[3]),
  7404   7422                             integerValue(azArg[4]));
  7405         -            raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
  7406         -          }else{
  7407         -            raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
         7423  +            isOk = 3;
  7408   7424             }
  7409   7425             break;
  7410         -
  7411         -        case SQLITE_TESTCTRL_BITVEC_TEST:
  7412         -        case SQLITE_TESTCTRL_FAULT_INSTALL:
  7413         -        case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
  7414         -        default:
  7415         -          utf8_printf(stderr,
  7416         -                      "Error: CLI support for testctrl %s not implemented\n",
  7417         -                      azArg[1]);
  7418         -          break;
  7419   7426         }
  7420   7427       }
         7428  +    if( isOk==0 && iCtrl>=0 ){
         7429  +      utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
         7430  +      rc = 1;
         7431  +    }else if( isOk==1 ){
         7432  +      raw_printf(p->out, "%d\n", rc2);
         7433  +    }else if( isOk==2 ){
         7434  +      raw_printf(p->out, "0x%08x\n", rc2);
         7435  +    }
  7421   7436     }else
  7422   7437   #endif /* !defined(SQLITE_UNTESTABLE) */
  7423   7438   
  7424   7439     if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
  7425   7440       open_db(p, 0);
  7426   7441       sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
  7427   7442     }else

Changes to src/sqlite3.c.

  1145   1145   **
  1146   1146   ** See also: [sqlite3_libversion()],
  1147   1147   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1148   1148   ** [sqlite_version()] and [sqlite_source_id()].
  1149   1149   */
  1150   1150   #define SQLITE_VERSION        "3.22.0"
  1151   1151   #define SQLITE_VERSION_NUMBER 3022000
  1152         -#define SQLITE_SOURCE_ID      "2017-11-14 19:34:22 00ec95fcd02bb415dabd7f25fee24856d45d6916c18b2728e97e9bb9b8322ba3"
         1152  +#define SQLITE_SOURCE_ID      "2017-12-06 20:50:08 6a55bd67720451cdf316155cf348103bfce1056a78bcddf0029b45ff0fdbcc71"
  1153   1153   
  1154   1154   /*
  1155   1155   ** CAPI3REF: Run-Time Library Version Numbers
  1156   1156   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1157   1157   **
  1158   1158   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1159   1159   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
 16530  16530     Expr *pWhere;          /* The WHERE clause */
 16531  16531     ExprList *pGroupBy;    /* The GROUP BY clause */
 16532  16532     Expr *pHaving;         /* The HAVING clause */
 16533  16533     ExprList *pOrderBy;    /* The ORDER BY clause */
 16534  16534     Select *pPrior;        /* Prior select in a compound select statement */
 16535  16535     Select *pNext;         /* Next select to the left in a compound */
 16536  16536     Expr *pLimit;          /* LIMIT expression. NULL means not used. */
 16537         -  Expr *pOffset;         /* OFFSET expression. NULL means not used. */
 16538  16537     With *pWith;           /* WITH clause attached to this select. Or NULL. */
 16539  16538   };
 16540  16539   
 16541  16540   /*
 16542  16541   ** Allowed values for Select.selFlags.  The "SF" prefix stands for
 16543  16542   ** "Select Flag".
 16544  16543   **
................................................................................
 17213  17212   /*
 17214  17213   ** The SQLITE_*_BKPT macros are substitutes for the error codes with
 17215  17214   ** the same name but without the _BKPT suffix.  These macros invoke
 17216  17215   ** routines that report the line-number on which the error originated
 17217  17216   ** using sqlite3_log().  The routines also provide a convenient place
 17218  17217   ** to set a debugger breakpoint.
 17219  17218   */
        17219  +SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType);
 17220  17220   SQLITE_PRIVATE int sqlite3CorruptError(int);
 17221  17221   SQLITE_PRIVATE int sqlite3MisuseError(int);
 17222  17222   SQLITE_PRIVATE int sqlite3CantopenError(int);
 17223  17223   #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
 17224  17224   #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
 17225  17225   #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
 17226  17226   #ifdef SQLITE_DEBUG
................................................................................
 17370  17370   SQLITE_PRIVATE void sqlite3StatusDown(int, int);
 17371  17371   SQLITE_PRIVATE void sqlite3StatusHighwater(int, int);
 17372  17372   SQLITE_PRIVATE int sqlite3LookasideUsed(sqlite3*,int*);
 17373  17373   
 17374  17374   /* Access to mutexes used by sqlite3_status() */
 17375  17375   SQLITE_PRIVATE sqlite3_mutex *sqlite3Pcache1Mutex(void);
 17376  17376   SQLITE_PRIVATE sqlite3_mutex *sqlite3MallocMutex(void);
        17377  +
        17378  +#if defined(SQLITE_ENABLE_MULTITHREADED_CHECKS) && !defined(SQLITE_MUTEX_OMIT)
        17379  +SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
        17380  +#else
        17381  +# define sqlite3MutexWarnOnContention(x)
        17382  +#endif
 17377  17383   
 17378  17384   #ifndef SQLITE_OMIT_FLOATING_POINT
 17379  17385   SQLITE_PRIVATE   int sqlite3IsNaN(double);
 17380  17386   #else
 17381  17387   # define sqlite3IsNaN(X)  0
 17382  17388   #endif
 17383  17389   
................................................................................
 17537  17543   SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
 17538  17544   SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
 17539  17545   SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
 17540  17546                             Expr*, int, int, u8);
 17541  17547   SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
 17542  17548   SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
 17543  17549   SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
 17544         -                         Expr*,ExprList*,u32,Expr*,Expr*);
        17550  +                         Expr*,ExprList*,u32,Expr*);
 17545  17551   SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
 17546  17552   SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
 17547  17553   SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
 17548  17554   SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
 17549  17555   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
 17550         -SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
        17556  +SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,char*);
 17551  17557   #endif
 17552         -SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*, Expr*);
 17553         -SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,Expr*);
        17558  +SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
        17559  +SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*);
 17554  17560   SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
 17555  17561   SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
 17556  17562   SQLITE_PRIVATE LogEst sqlite3WhereOutputRowCount(WhereInfo*);
 17557  17563   SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
 17558  17564   SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
 17559  17565   SQLITE_PRIVATE int sqlite3WhereOrderedInnerLoop(WhereInfo*);
 17560  17566   SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
................................................................................
 17670  17676   SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
 17671  17677   SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
 17672  17678   SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
 17673  17679   SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
 17674  17680   SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
 17675  17681   
 17676  17682   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
 17677         -SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,Expr*,int);
        17683  +SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
 17678  17684   #endif
 17679  17685   
 17680  17686   #ifndef SQLITE_OMIT_TRIGGER
 17681  17687   SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
 17682  17688                              Expr*,int, int);
 17683  17689   SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
 17684  17690   SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
................................................................................
 23391  23397   ** allocate a mutex while the system is uninitialized.
 23392  23398   */
 23393  23399   static SQLITE_WSD int mutexIsInit = 0;
 23394  23400   #endif /* SQLITE_DEBUG && !defined(SQLITE_MUTEX_OMIT) */
 23395  23401   
 23396  23402   
 23397  23403   #ifndef SQLITE_MUTEX_OMIT
        23404  +
        23405  +#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
        23406  +/*
        23407  +** This block (enclosed by SQLITE_ENABLE_MULTITHREADED_CHECKS) contains
        23408  +** the implementation of a wrapper around the system default mutex
        23409  +** implementation (sqlite3DefaultMutex()). 
        23410  +**
        23411  +** Most calls are passed directly through to the underlying default
        23412  +** mutex implementation. Except, if a mutex is configured by calling
        23413  +** sqlite3MutexWarnOnContention() on it, then if contention is ever
        23414  +** encountered within xMutexEnter() a warning is emitted via sqlite3_log().
        23415  +**
        23416  +** This type of mutex is used as the database handle mutex when testing
        23417  +** apps that usually use SQLITE_CONFIG_MULTITHREAD mode.
        23418  +*/
        23419  +
        23420  +/* 
        23421  +** Type for all mutexes used when SQLITE_ENABLE_MULTITHREADED_CHECKS
        23422  +** is defined. Variable CheckMutex.mutex is a pointer to the real mutex
        23423  +** allocated by the system mutex implementation. Variable iType is usually set
        23424  +** to the type of mutex requested - SQLITE_MUTEX_RECURSIVE, SQLITE_MUTEX_FAST
        23425  +** or one of the static mutex identifiers. Or, if this is a recursive mutex
        23426  +** that has been configured using sqlite3MutexWarnOnContention(), it is
        23427  +** set to SQLITE_MUTEX_WARNONCONTENTION.
        23428  +*/
        23429  +typedef struct CheckMutex CheckMutex;
        23430  +struct CheckMutex {
        23431  +  int iType;
        23432  +  sqlite3_mutex *mutex;
        23433  +};
        23434  +
        23435  +#define SQLITE_MUTEX_WARNONCONTENTION  (-1)
        23436  +
        23437  +/* 
        23438  +** Pointer to real mutex methods object used by the CheckMutex
        23439  +** implementation. Set by checkMutexInit(). 
        23440  +*/
        23441  +static SQLITE_WSD const sqlite3_mutex_methods *pGlobalMutexMethods;
        23442  +
        23443  +#ifdef SQLITE_DEBUG
        23444  +static int checkMutexHeld(sqlite3_mutex *p){
        23445  +  return pGlobalMutexMethods->xMutexHeld(((CheckMutex*)p)->mutex);
        23446  +}
        23447  +static int checkMutexNotheld(sqlite3_mutex *p){
        23448  +  return pGlobalMutexMethods->xMutexNotheld(((CheckMutex*)p)->mutex);
        23449  +}
        23450  +#endif
        23451  +
        23452  +/*
        23453  +** Initialize and deinitialize the mutex subsystem.
        23454  +*/
        23455  +static int checkMutexInit(void){ 
        23456  +  pGlobalMutexMethods = sqlite3DefaultMutex();
        23457  +  return SQLITE_OK; 
        23458  +}
        23459  +static int checkMutexEnd(void){ 
        23460  +  pGlobalMutexMethods = 0;
        23461  +  return SQLITE_OK; 
        23462  +}
        23463  +
        23464  +/*
        23465  +** Allocate a mutex.
        23466  +*/
        23467  +static sqlite3_mutex *checkMutexAlloc(int iType){
        23468  +  static CheckMutex staticMutexes[] = {
        23469  +    {2, 0}, {3, 0}, {4, 0}, {5, 0},
        23470  +    {6, 0}, {7, 0}, {8, 0}, {9, 0},
        23471  +    {10, 0}, {11, 0}, {12, 0}, {13, 0}
        23472  +  };
        23473  +  CheckMutex *p = 0;
        23474  +
        23475  +  assert( SQLITE_MUTEX_RECURSIVE==1 && SQLITE_MUTEX_FAST==0 );
        23476  +  if( iType<2 ){
        23477  +    p = sqlite3MallocZero(sizeof(CheckMutex));
        23478  +    if( p==0 ) return 0;
        23479  +    p->iType = iType;
        23480  +  }else{
        23481  +#ifdef SQLITE_ENABLE_API_ARMOR
        23482  +    if( iType-2>=ArraySize(staticMutexes) ){
        23483  +      (void)SQLITE_MISUSE_BKPT;
        23484  +      return 0;
        23485  +    }
        23486  +#endif
        23487  +    p = &staticMutexes[iType-2];
        23488  +  }
        23489  +
        23490  +  if( p->mutex==0 ){
        23491  +    p->mutex = pGlobalMutexMethods->xMutexAlloc(iType);
        23492  +    if( p->mutex==0 ){
        23493  +      if( iType<2 ){
        23494  +        sqlite3_free(p);
        23495  +      }
        23496  +      p = 0;
        23497  +    }
        23498  +  }
        23499  +
        23500  +  return (sqlite3_mutex*)p;
        23501  +}
        23502  +
        23503  +/*
        23504  +** Free a mutex.
        23505  +*/
        23506  +static void checkMutexFree(sqlite3_mutex *p){
        23507  +  assert( SQLITE_MUTEX_RECURSIVE<2 );
        23508  +  assert( SQLITE_MUTEX_FAST<2 );
        23509  +  assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
        23510  +
        23511  +#if SQLITE_ENABLE_API_ARMOR
        23512  +  if( ((CheckMutex*)p)->iType<2 )
        23513  +#endif
        23514  +  {
        23515  +    CheckMutex *pCheck = (CheckMutex*)p;
        23516  +    pGlobalMutexMethods->xMutexFree(pCheck->mutex);
        23517  +    sqlite3_free(pCheck);
        23518  +  }
        23519  +#ifdef SQLITE_ENABLE_API_ARMOR
        23520  +  else{
        23521  +    (void)SQLITE_MISUSE_BKPT;
        23522  +  }
        23523  +#endif
        23524  +}
        23525  +
        23526  +/*
        23527  +** Enter the mutex.
        23528  +*/
        23529  +static void checkMutexEnter(sqlite3_mutex *p){
        23530  +  CheckMutex *pCheck = (CheckMutex*)p;
        23531  +  if( pCheck->iType==SQLITE_MUTEX_WARNONCONTENTION ){
        23532  +    if( SQLITE_OK==pGlobalMutexMethods->xMutexTry(pCheck->mutex) ){
        23533  +      return;
        23534  +    }
        23535  +    sqlite3_log(SQLITE_MISUSE, 
        23536  +        "illegal multi-threaded access to database connection"
        23537  +    );
        23538  +  }
        23539  +  pGlobalMutexMethods->xMutexEnter(pCheck->mutex);
        23540  +}
        23541  +
        23542  +/*
        23543  +** Enter the mutex (do not block).
        23544  +*/
        23545  +static int checkMutexTry(sqlite3_mutex *p){
        23546  +  CheckMutex *pCheck = (CheckMutex*)p;
        23547  +  return pGlobalMutexMethods->xMutexTry(pCheck->mutex);
        23548  +}
        23549  +
        23550  +/*
        23551  +** Leave the mutex.
        23552  +*/
        23553  +static void checkMutexLeave(sqlite3_mutex *p){
        23554  +  CheckMutex *pCheck = (CheckMutex*)p;
        23555  +  pGlobalMutexMethods->xMutexLeave(pCheck->mutex);
        23556  +}
        23557  +
        23558  +sqlite3_mutex_methods const *multiThreadedCheckMutex(void){
        23559  +  static const sqlite3_mutex_methods sMutex = {
        23560  +    checkMutexInit,
        23561  +    checkMutexEnd,
        23562  +    checkMutexAlloc,
        23563  +    checkMutexFree,
        23564  +    checkMutexEnter,
        23565  +    checkMutexTry,
        23566  +    checkMutexLeave,
        23567  +#ifdef SQLITE_DEBUG
        23568  +    checkMutexHeld,
        23569  +    checkMutexNotheld
        23570  +#else
        23571  +    0,
        23572  +    0
        23573  +#endif
        23574  +  };
        23575  +  return &sMutex;
        23576  +}
        23577  +
        23578  +/*
        23579  +** Mark the SQLITE_MUTEX_RECURSIVE mutex passed as the only argument as
        23580  +** one on which there should be no contention.
        23581  +*/
        23582  +SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex *p){
        23583  +  if( sqlite3GlobalConfig.mutex.xMutexAlloc==checkMutexAlloc ){
        23584  +    CheckMutex *pCheck = (CheckMutex*)p;
        23585  +    assert( pCheck->iType==SQLITE_MUTEX_RECURSIVE );
        23586  +    pCheck->iType = SQLITE_MUTEX_WARNONCONTENTION;
        23587  +  }
        23588  +}
        23589  +#endif   /* ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS */
        23590  +
 23398  23591   /*
 23399  23592   ** Initialize the mutex system.
 23400  23593   */
 23401  23594   SQLITE_PRIVATE int sqlite3MutexInit(void){ 
 23402  23595     int rc = SQLITE_OK;
 23403  23596     if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
 23404  23597       /* If the xMutexAlloc method has not been set, then the user did not
................................................................................
 23406  23599       ** sqlite3_initialize() being called. This block copies pointers to
 23407  23600       ** the default implementation into the sqlite3GlobalConfig structure.
 23408  23601       */
 23409  23602       sqlite3_mutex_methods const *pFrom;
 23410  23603       sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
 23411  23604   
 23412  23605       if( sqlite3GlobalConfig.bCoreMutex ){
        23606  +#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
        23607  +      pFrom = multiThreadedCheckMutex();
        23608  +#else
 23413  23609         pFrom = sqlite3DefaultMutex();
        23610  +#endif
 23414  23611       }else{
 23415  23612         pFrom = sqlite3NoopMutex();
 23416  23613       }
 23417  23614       pTo->xMutexInit = pFrom->xMutexInit;
 23418  23615       pTo->xMutexEnd = pFrom->xMutexEnd;
 23419  23616       pTo->xMutexFree = pFrom->xMutexFree;
 23420  23617       pTo->xMutexEnter = pFrom->xMutexEnter;
................................................................................
 23532  23729   SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
 23533  23730     assert( p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld );
 23534  23731     return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
 23535  23732   }
 23536  23733   #endif
 23537  23734   
 23538  23735   #endif /* !defined(SQLITE_MUTEX_OMIT) */
        23736  +
 23539  23737   
 23540  23738   /************** End of mutex.c ***********************************************/
 23541  23739   /************** Begin file mutex_noop.c **************************************/
 23542  23740   /*
 23543  23741   ** 2008 October 07
 23544  23742   **
 23545  23743   ** The author disclaims copyright to this source code.  In place of
................................................................................
 26815  27013         n = 0;
 26816  27014         if( p->pSrc && p->pSrc->nSrc ) n++;
 26817  27015         if( p->pWhere ) n++;
 26818  27016         if( p->pGroupBy ) n++;
 26819  27017         if( p->pHaving ) n++;
 26820  27018         if( p->pOrderBy ) n++;
 26821  27019         if( p->pLimit ) n++;
 26822         -      if( p->pOffset ) n++;
 26823  27020       }
 26824  27021       sqlite3TreeViewExprList(pView, p->pEList, (n--)>0, "result-set");
 26825  27022       if( p->pSrc && p->pSrc->nSrc ){
 26826  27023         int i;
 26827  27024         pView = sqlite3TreeViewPush(pView, (n--)>0);
 26828  27025         sqlite3TreeViewLine(pView, "FROM");
 26829  27026         for(i=0; i<p->pSrc->nSrc; i++){
................................................................................
 26872  27069         sqlite3TreeViewPop(pView);
 26873  27070       }
 26874  27071       if( p->pOrderBy ){
 26875  27072         sqlite3TreeViewExprList(pView, p->pOrderBy, (n--)>0, "ORDERBY");
 26876  27073       }
 26877  27074       if( p->pLimit ){
 26878  27075         sqlite3TreeViewItem(pView, "LIMIT", (n--)>0);
 26879         -      sqlite3TreeViewExpr(pView, p->pLimit, 0);
 26880         -      sqlite3TreeViewPop(pView);
 26881         -    }
 26882         -    if( p->pOffset ){
 26883         -      sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
 26884         -      sqlite3TreeViewExpr(pView, p->pOffset, 0);
        27076  +      sqlite3TreeViewExpr(pView, p->pLimit->pLeft, p->pLimit->pRight!=0);
        27077  +      if( p->pLimit->pRight ){
        27078  +        sqlite3TreeViewItem(pView, "OFFSET", (n--)>0);
        27079  +        sqlite3TreeViewExpr(pView, p->pLimit->pRight, 0);
        27080  +        sqlite3TreeViewPop(pView);
        27081  +      }
 26885  27082         sqlite3TreeViewPop(pView);
 26886  27083       }
 26887  27084       if( p->pPrior ){
 26888  27085         const char *zOp = "UNION";
 26889  27086         switch( p->op ){
 26890  27087           case TK_ALL:         zOp = "UNION ALL";  break;
 26891  27088           case TK_INTERSECT:   zOp = "INTERSECT";  break;
................................................................................
 57726  57923     }
 57727  57924   
 57728  57925     assert( pWal->nWiData>0 );
 57729  57926     assert( pWal->apWiData[0]!=0 );
 57730  57927     pInfo = walCkptInfo(pWal);
 57731  57928     if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame
 57732  57929   #ifdef SQLITE_ENABLE_SNAPSHOT
 57733         -   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0
 57734         -     || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr)))
        57930  +   && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0)
 57735  57931   #endif
 57736  57932     ){
 57737  57933       /* The WAL has been completely backfilled (or it is empty).
 57738  57934       ** and can be safely ignored.
 57739  57935       */
 57740  57936       rc = walLockShared(pWal, WAL_READ_LOCK(0));
 57741  57937       walShmBarrier(pWal);
................................................................................
 60147  60343     #define setSharedCacheTableLock(a,b,c) SQLITE_OK
 60148  60344     #define clearAllSharedCacheTableLocks(a)
 60149  60345     #define downgradeAllSharedCacheTableLocks(a)
 60150  60346     #define hasSharedCacheTableLock(a,b,c,d) 1
 60151  60347     #define hasReadConflicts(a, b) 0
 60152  60348   #endif
 60153  60349   
        60350  +/*
        60351  +** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single
        60352  +** (MemPage*) as an argument. The (MemPage*) must not be NULL.
        60353  +**
        60354  +** If SQLITE_DEBUG is not defined, then this macro is equivalent to
        60355  +** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message
        60356  +** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented
        60357  +** with the page number and filename associated with the (MemPage*).
        60358  +*/
        60359  +#ifdef SQLITE_DEBUG
        60360  +int corruptPageError(int lineno, MemPage *p){
        60361  +  char *zMsg = sqlite3_mprintf("database corruption page %d of %s",
        60362  +      (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0)
        60363  +  );
        60364  +  if( zMsg ){
        60365  +    sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
        60366  +  }
        60367  +  sqlite3_free(zMsg);
        60368  +  return SQLITE_CORRUPT_BKPT;
        60369  +}
        60370  +# define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage)
        60371  +#else
        60372  +# define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno)
        60373  +#endif
        60374  +
 60154  60375   #ifndef SQLITE_OMIT_SHARED_CACHE
 60155  60376   
 60156  60377   #ifdef SQLITE_DEBUG
 60157  60378   /*
 60158  60379   **** This function is only used as part of an assert() statement. ***
 60159  60380   **
 60160  60381   ** Check to see if pBtree holds the required locks to read or write to the 
................................................................................
 61435  61656         if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
 61436  61657           u8 *pEnd = &data[cellOffset + nCell*2];
 61437  61658           u8 *pAddr;
 61438  61659           int sz2 = 0;
 61439  61660           int sz = get2byte(&data[iFree+2]);
 61440  61661           int top = get2byte(&data[hdr+5]);
 61441  61662           if( top>=iFree ){
 61442         -          return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61663  +          return SQLITE_CORRUPT_PAGE(pPage);
 61443  61664           }
 61444  61665           if( iFree2 ){
 61445  61666             assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */
 61446  61667             sz2 = get2byte(&data[iFree2+2]);
 61447  61668             assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize );
 61448  61669             memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
 61449  61670             sz += sz2;
................................................................................
 61469  61690       pc = get2byte(pAddr);
 61470  61691       testcase( pc==iCellFirst );
 61471  61692       testcase( pc==iCellLast );
 61472  61693       /* These conditions have already been verified in btreeInitPage()
 61473  61694       ** if PRAGMA cell_size_check=ON.
 61474  61695       */
 61475  61696       if( pc<iCellFirst || pc>iCellLast ){
 61476         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61697  +      return SQLITE_CORRUPT_PAGE(pPage);
 61477  61698       }
 61478  61699       assert( pc>=iCellFirst && pc<=iCellLast );
 61479  61700       size = pPage->xCellSize(pPage, &src[pc]);
 61480  61701       cbrk -= size;
 61481  61702       if( cbrk<iCellFirst || pc+size>usableSize ){
 61482         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61703  +      return SQLITE_CORRUPT_PAGE(pPage);
 61483  61704       }
 61484  61705       assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
 61485  61706       testcase( cbrk+size==usableSize );
 61486  61707       testcase( pc+size==usableSize );
 61487  61708       put2byte(pAddr, cbrk);
 61488  61709       if( temp==0 ){
 61489  61710         int x;
................................................................................
 61495  61716       }
 61496  61717       memcpy(&data[cbrk], &src[pc], size);
 61497  61718     }
 61498  61719     data[hdr+7] = 0;
 61499  61720   
 61500  61721    defragment_out:
 61501  61722     if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
 61502         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61723  +    return SQLITE_CORRUPT_PAGE(pPage);
 61503  61724     }
 61504  61725     assert( cbrk>=iCellFirst );
 61505  61726     put2byte(&data[hdr+5], cbrk);
 61506  61727     data[hdr+1] = 0;
 61507  61728     data[hdr+2] = 0;
 61508  61729     memset(&data[iCellFirst], 0, cbrk-iCellFirst);
 61509  61730     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
................................................................................
 61539  61760       ** freeblock form a big-endian integer which is the size of the freeblock
 61540  61761       ** in bytes, including the 4-byte header. */
 61541  61762       size = get2byte(&aData[pc+2]);
 61542  61763       if( (x = size - nByte)>=0 ){
 61543  61764         testcase( x==4 );
 61544  61765         testcase( x==3 );
 61545  61766         if( size+pc > usableSize ){
 61546         -        *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
        61767  +        *pRc = SQLITE_CORRUPT_PAGE(pPg);
 61547  61768           return 0;
 61548  61769         }else if( x<4 ){
 61549  61770           /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total
 61550  61771           ** number of bytes in fragments may not exceed 60. */
 61551  61772           if( aData[hdr+7]>57 ) return 0;
 61552  61773   
 61553  61774           /* Remove the slot from the free-list. Update the number of
................................................................................
 61562  61783         return &aData[pc + x];
 61563  61784       }
 61564  61785       iAddr = pc;
 61565  61786       pc = get2byte(&aData[pc]);
 61566  61787       if( pc<iAddr+size ) break;
 61567  61788     }
 61568  61789     if( pc ){
 61569         -    *pRc = SQLITE_CORRUPT_PGNO(pPg->pgno);
        61790  +    *pRc = SQLITE_CORRUPT_PAGE(pPg);
 61570  61791     }
 61571  61792   
 61572  61793     return 0;
 61573  61794   }
 61574  61795   
 61575  61796   /*
 61576  61797   ** Allocate nByte bytes of space from within the B-Tree page passed
................................................................................
 61610  61831     ** integer, so a value of 0 is used in its place. */
 61611  61832     top = get2byte(&data[hdr+5]);
 61612  61833     assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */
 61613  61834     if( gap>top ){
 61614  61835       if( top==0 && pPage->pBt->usableSize==65536 ){
 61615  61836         top = 65536;
 61616  61837       }else{
 61617         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61838  +      return SQLITE_CORRUPT_PAGE(pPage);
 61618  61839       }
 61619  61840     }
 61620  61841   
 61621  61842     /* If there is enough space between gap and top for one more cell pointer
 61622  61843     ** array entry offset, and if the freelist is not empty, then search the
 61623  61844     ** freelist looking for a free slot big enough to satisfy the request.
 61624  61845     */
................................................................................
 61700  61921     iPtr = hdr + 1;
 61701  61922     if( data[iPtr+1]==0 && data[iPtr]==0 ){
 61702  61923       iFreeBlk = 0;  /* Shortcut for the case when the freelist is empty */
 61703  61924     }else{
 61704  61925       while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){
 61705  61926         if( iFreeBlk<iPtr+4 ){
 61706  61927           if( iFreeBlk==0 ) break;
 61707         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61928  +        return SQLITE_CORRUPT_PAGE(pPage);
 61708  61929         }
 61709  61930         iPtr = iFreeBlk;
 61710  61931       }
 61711  61932       if( iFreeBlk>pPage->pBt->usableSize-4 ){
 61712         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61933  +      return SQLITE_CORRUPT_PAGE(pPage);
 61713  61934       }
 61714  61935       assert( iFreeBlk>iPtr || iFreeBlk==0 );
 61715  61936     
 61716  61937       /* At this point:
 61717  61938       **    iFreeBlk:   First freeblock after iStart, or zero if none
 61718  61939       **    iPtr:       The address of a pointer to iFreeBlk
 61719  61940       **
 61720  61941       ** Check to see if iFreeBlk should be coalesced onto the end of iStart.
 61721  61942       */
 61722  61943       if( iFreeBlk && iEnd+3>=iFreeBlk ){
 61723  61944         nFrag = iFreeBlk - iEnd;
 61724         -      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61945  +      if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
 61725  61946         iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
 61726  61947         if( iEnd > pPage->pBt->usableSize ){
 61727         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61948  +        return SQLITE_CORRUPT_PAGE(pPage);
 61728  61949         }
 61729  61950         iSize = iEnd - iStart;
 61730  61951         iFreeBlk = get2byte(&data[iFreeBlk]);
 61731  61952       }
 61732  61953     
 61733  61954       /* If iPtr is another freeblock (that is, if iPtr is not the freelist
 61734  61955       ** pointer in the page header) then check to see if iStart should be
 61735  61956       ** coalesced onto the end of iPtr.
 61736  61957       */
 61737  61958       if( iPtr>hdr+1 ){
 61738  61959         int iPtrEnd = iPtr + get2byte(&data[iPtr+2]);
 61739  61960         if( iPtrEnd+3>=iStart ){
 61740         -        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61961  +        if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage);
 61741  61962           nFrag += iStart - iPtrEnd;
 61742  61963           iSize = iEnd - iPtr;
 61743  61964           iStart = iPtr;
 61744  61965         }
 61745  61966       }
 61746         -    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61967  +    if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
 61747  61968       data[hdr+7] -= nFrag;
 61748  61969     }
 61749  61970     x = get2byte(&data[hdr+5]);
 61750  61971     if( iStart<=x ){
 61751  61972       /* The new freeblock is at the beginning of the cell content area,
 61752  61973       ** so just extend the cell content area rather than create another
 61753  61974       ** freelist entry */
 61754         -    if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PGNO(pPage->pgno);
        61975  +    if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage);
 61755  61976       put2byte(&data[hdr+1], iFreeBlk);
 61756  61977       put2byte(&data[hdr+5], iEnd);
 61757  61978     }else{
 61758  61979       /* Insert the new freeblock into the freelist */
 61759  61980       put2byte(&data[iPtr], iStart);
 61760  61981     }
 61761  61982     if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
................................................................................
 61820  62041       pPage->intKeyLeaf = 0;
 61821  62042       pPage->xParseCell = btreeParseCellPtrIndex;
 61822  62043       pPage->maxLocal = pBt->maxLocal;
 61823  62044       pPage->minLocal = pBt->minLocal;
 61824  62045     }else{
 61825  62046       /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is
 61826  62047       ** an error. */
 61827         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62048  +    return SQLITE_CORRUPT_PAGE(pPage);
 61828  62049     }
 61829  62050     pPage->max1bytePayload = pBt->max1bytePayload;
 61830  62051     return SQLITE_OK;
 61831  62052   }
 61832  62053   
 61833  62054   /*
 61834  62055   ** Initialize the auxiliary information for a disk block.
................................................................................
 61861  62082   
 61862  62083     pBt = pPage->pBt;
 61863  62084     hdr = pPage->hdrOffset;
 61864  62085     data = pPage->aData;
 61865  62086     /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating
 61866  62087     ** the b-tree page type. */
 61867  62088     if( decodeFlags(pPage, data[hdr]) ){
 61868         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62089  +    return SQLITE_CORRUPT_PAGE(pPage);
 61869  62090     }
 61870  62091     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
 61871  62092     pPage->maskPage = (u16)(pBt->pageSize - 1);
 61872  62093     pPage->nOverflow = 0;
 61873  62094     usableSize = pBt->usableSize;
 61874  62095     pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize;
 61875  62096     pPage->aDataEnd = &data[usableSize];
................................................................................
 61880  62101     ** interpreted as 65536. */
 61881  62102     top = get2byteNotZero(&data[hdr+5]);
 61882  62103     /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
 61883  62104     ** number of cells on the page. */
 61884  62105     pPage->nCell = get2byte(&data[hdr+3]);
 61885  62106     if( pPage->nCell>MX_CELL(pBt) ){
 61886  62107       /* To many cells for a single page.  The page must be corrupt */
 61887         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62108  +    return SQLITE_CORRUPT_PAGE(pPage);
 61888  62109     }
 61889  62110     testcase( pPage->nCell==MX_CELL(pBt) );
 61890  62111     /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only
 61891  62112     ** possible for a root page of a table that contains no rows) then the
 61892  62113     ** offset to the cell content area will equal the page size minus the
 61893  62114     ** bytes of reserved space. */
 61894  62115     assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB );
................................................................................
 61908  62129   
 61909  62130       if( !pPage->leaf ) iCellLast--;
 61910  62131       for(i=0; i<pPage->nCell; i++){
 61911  62132         pc = get2byteAligned(&data[cellOffset+i*2]);
 61912  62133         testcase( pc==iCellFirst );
 61913  62134         testcase( pc==iCellLast );
 61914  62135         if( pc<iCellFirst || pc>iCellLast ){
 61915         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62136  +        return SQLITE_CORRUPT_PAGE(pPage);
 61916  62137         }
 61917  62138         sz = pPage->xCellSize(pPage, &data[pc]);
 61918  62139         testcase( pc+sz==usableSize );
 61919  62140         if( pc+sz>usableSize ){
 61920         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62141  +        return SQLITE_CORRUPT_PAGE(pPage);
 61921  62142         }
 61922  62143       }
 61923  62144       if( !pPage->leaf ) iCellLast++;
 61924  62145     }  
 61925  62146   
 61926  62147     /* Compute the total free space on the page
 61927  62148     ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the
................................................................................
 61931  62152     nFree = data[hdr+7] + top;  /* Init nFree to non-freeblock free space */
 61932  62153     if( pc>0 ){
 61933  62154       u32 next, size;
 61934  62155       if( pc<iCellFirst ){
 61935  62156         /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will
 61936  62157         ** always be at least one cell before the first freeblock.
 61937  62158         */
 61938         -      return SQLITE_CORRUPT_PGNO(pPage->pgno); 
        62159  +      return SQLITE_CORRUPT_PAGE(pPage); 
 61939  62160       }
 61940  62161       while( 1 ){
 61941  62162         if( pc>iCellLast ){
 61942  62163           /* Freeblock off the end of the page */
 61943         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62164  +        return SQLITE_CORRUPT_PAGE(pPage);
 61944  62165         }
 61945  62166         next = get2byte(&data[pc]);
 61946  62167         size = get2byte(&data[pc+2]);
 61947  62168         nFree = nFree + size;
 61948  62169         if( next<=pc+size+3 ) break;
 61949  62170         pc = next;
 61950  62171       }
 61951  62172       if( next>0 ){
 61952  62173         /* Freeblock not in ascending order */
 61953         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62174  +      return SQLITE_CORRUPT_PAGE(pPage);
 61954  62175       }
 61955  62176       if( pc+size>(unsigned int)usableSize ){
 61956  62177         /* Last freeblock extends past page end */
 61957         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62178  +      return SQLITE_CORRUPT_PAGE(pPage);
 61958  62179       }
 61959  62180     }
 61960  62181   
 61961  62182     /* At this point, nFree contains the sum of the offset to the start
 61962  62183     ** of the cell-content area plus the number of free bytes within
 61963  62184     ** the cell-content area. If this is greater than the usable-size
 61964  62185     ** of the page, then the page must be corrupted. This check also
 61965  62186     ** serves to verify that the offset to the start of the cell-content
 61966  62187     ** area, according to the page header, lies within the page.
 61967  62188     */
 61968  62189     if( nFree>usableSize ){
 61969         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        62190  +    return SQLITE_CORRUPT_PAGE(pPage);
 61970  62191     }
 61971  62192     pPage->nFree = (u16)(nFree - iCellFirst);
 61972  62193     pPage->isInit = 1;
 61973  62194     return SQLITE_OK;
 61974  62195   }
 61975  62196   
 61976  62197   /*
................................................................................
 63493  63714   */
 63494  63715   static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
 63495  63716     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 63496  63717     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
 63497  63718     if( eType==PTRMAP_OVERFLOW2 ){
 63498  63719       /* The pointer is always the first 4 bytes of the page in this case.  */
 63499  63720       if( get4byte(pPage->aData)!=iFrom ){
 63500         -      return SQLITE_CORRUPT_PGNO(pPage->pgno);
        63721  +      return SQLITE_CORRUPT_PAGE(pPage);
 63501  63722       }
 63502  63723       put4byte(pPage->aData, iTo);
 63503  63724     }else{
 63504  63725       int i;
 63505  63726       int nCell;
 63506  63727       int rc;
 63507  63728   
................................................................................
 63512  63733       for(i=0; i<nCell; i++){
 63513  63734         u8 *pCell = findCell(pPage, i);
 63514  63735         if( eType==PTRMAP_OVERFLOW1 ){
 63515  63736           CellInfo info;
 63516  63737           pPage->xParseCell(pPage, pCell, &info);
 63517  63738           if( info.nLocal<info.nPayload ){
 63518  63739             if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
 63519         -            return SQLITE_CORRUPT_PGNO(pPage->pgno);
        63740  +            return SQLITE_CORRUPT_PAGE(pPage);
 63520  63741             }
 63521  63742             if( iFrom==get4byte(pCell+info.nSize-4) ){
 63522  63743               put4byte(pCell+info.nSize-4, iTo);
 63523  63744               break;
 63524  63745             }
 63525  63746           }
 63526  63747         }else{
................................................................................
 63530  63751           }
 63531  63752         }
 63532  63753       }
 63533  63754     
 63534  63755       if( i==nCell ){
 63535  63756         if( eType!=PTRMAP_BTREE || 
 63536  63757             get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
 63537         -        return SQLITE_CORRUPT_PGNO(pPage->pgno);
        63758  +        return SQLITE_CORRUPT_PAGE(pPage);
 63538  63759         }
 63539  63760         put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
 63540  63761       }
 63541  63762     }
 63542  63763     return SQLITE_OK;
 63543  63764   }
 63544  63765   
................................................................................
 64628  64849     assert( aPayload > pPage->aData );
 64629  64850     if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
 64630  64851       /* Trying to read or write past the end of the data is an error.  The
 64631  64852       ** conditional above is really:
 64632  64853       **    &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
 64633  64854       ** but is recast into its current form to avoid integer overflow problems
 64634  64855       */
 64635         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        64856  +    return SQLITE_CORRUPT_PAGE(pPage);
 64636  64857     }
 64637  64858   
 64638  64859     /* Check if data must be read/written to/from the btree page itself. */
 64639  64860     if( offset<pCur->info.nLocal ){
 64640  64861       int a = amt;
 64641  64862       if( a+offset>pCur->info.nLocal ){
 64642  64863         a = pCur->info.nLocal - offset;
................................................................................
 64776  64997         if( rc ) break;
 64777  64998         iIdx++;
 64778  64999       }
 64779  65000     }
 64780  65001   
 64781  65002     if( rc==SQLITE_OK && amt>0 ){
 64782  65003       /* Overflow chain ends prematurely */
 64783         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        65004  +    return SQLITE_CORRUPT_PAGE(pPage);
 64784  65005     }
 64785  65006     return rc;
 64786  65007   }
 64787  65008   
 64788  65009   /*
 64789  65010   ** Read part of the payload for the row at which that cursor pCur is currently
 64790  65011   ** pointing.  "amt" bytes will be transferred into pBuf[].  The transfer
................................................................................
 65054  65275     ** Earlier versions of SQLite assumed that this test could not fail
 65055  65276     ** if the root page was already loaded when this function was called (i.e.
 65056  65277     ** if pCur->iPage>=0). But this is not so if the database is corrupted 
 65057  65278     ** in such a way that page pRoot is linked into a second b-tree table 
 65058  65279     ** (or the freelist).  */
 65059  65280     assert( pRoot->intKey==1 || pRoot->intKey==0 );
 65060  65281     if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
 65061         -    return SQLITE_CORRUPT_PGNO(pCur->pPage->pgno);
        65282  +    return SQLITE_CORRUPT_PAGE(pCur->pPage);
 65062  65283     }
 65063  65284   
 65064  65285   skip_init:  
 65065  65286     pCur->ix = 0;
 65066  65287     pCur->info.nSize = 0;
 65067  65288     pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl);
 65068  65289   
................................................................................
 65327  65548       if( xRecordCompare==0 ){
 65328  65549         for(;;){
 65329  65550           i64 nCellKey;
 65330  65551           pCell = findCellPastPtr(pPage, idx);
 65331  65552           if( pPage->intKeyLeaf ){
 65332  65553             while( 0x80 <= *(pCell++) ){
 65333  65554               if( pCell>=pPage->aDataEnd ){
 65334         -              return SQLITE_CORRUPT_PGNO(pPage->pgno);
        65555  +              return SQLITE_CORRUPT_PAGE(pPage);
 65335  65556               }
 65336  65557             }
 65337  65558           }
 65338  65559           getVarint(pCell, (u64*)&nCellKey);
 65339  65560           if( nCellKey<intKey ){
 65340  65561             lwr = idx+1;
 65341  65562             if( lwr>upr ){ c = -1; break; }
................................................................................
 65401  65622             pPage->xParseCell(pPage, pCellBody, &pCur->info);
 65402  65623             nCell = (int)pCur->info.nKey;
 65403  65624             testcase( nCell<0 );   /* True if key size is 2^32 or more */
 65404  65625             testcase( nCell==0 );  /* Invalid key size:  0x80 0x80 0x00 */
 65405  65626             testcase( nCell==1 );  /* Invalid key size:  0x80 0x80 0x01 */
 65406  65627             testcase( nCell==2 );  /* Minimum legal index key size */
 65407  65628             if( nCell<2 ){
 65408         -            rc = SQLITE_CORRUPT_PGNO(pPage->pgno);
        65629  +            rc = SQLITE_CORRUPT_PAGE(pPage);
 65409  65630               goto moveto_finish;
 65410  65631             }
 65411  65632             pCellKey = sqlite3Malloc( nCell+18 );
 65412  65633             if( pCellKey==0 ){
 65413  65634               rc = SQLITE_NOMEM_BKPT;
 65414  65635               goto moveto_finish;
 65415  65636             }
................................................................................
 66204  66425     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
 66205  66426     pPage->xParseCell(pPage, pCell, pInfo);
 66206  66427     if( pInfo->nLocal==pInfo->nPayload ){
 66207  66428       return SQLITE_OK;  /* No overflow pages. Return without doing anything */
 66208  66429     }
 66209  66430     if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){
 66210  66431       /* Cell extends past end of page */
 66211         -    return SQLITE_CORRUPT_PGNO(pPage->pgno);
        66432  +    return SQLITE_CORRUPT_PAGE(pPage);
 66212  66433     }
 66213  66434     ovflPgno = get4byte(pCell + pInfo->nSize - 4);
 66214  66435     pBt = pPage->pBt;
 66215  66436     assert( pBt->usableSize > 4 );
 66216  66437     ovflPageSize = pBt->usableSize - 4;
 66217  66438     nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
 66218  66439     assert( nOvfl>0 || 
................................................................................
 72101  72322     sqlite3_value *pVal = 0;
 72102  72323     int negInt = 1;
 72103  72324     const char *zNeg = "";
 72104  72325     int rc = SQLITE_OK;
 72105  72326   
 72106  72327     assert( pExpr!=0 );
 72107  72328     while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
        72329  +#if defined(SQLITE_ENABLE_STAT3_OR_STAT4)
        72330  +  if( op==TK_REGISTER ) op = pExpr->op2;
        72331  +#else
 72108  72332     if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
        72333  +#endif
 72109  72334   
 72110  72335     /* Compressed expressions only appear when parsing the DEFAULT clause
 72111  72336     ** on a table column definition, and hence only when pCtx==0.  This
 72112  72337     ** check ensures that an EP_TokenOnly expression is never passed down
 72113  72338     ** into valueFromFunction(). */
 72114  72339     assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
 72115  72340   
................................................................................
 74811  75036         };
 74812  75037         Pager *pPager;   /* Pager associated with pBt */
 74813  75038         needXcommit = 1;
 74814  75039         sqlite3BtreeEnter(pBt);
 74815  75040         pPager = sqlite3BtreePager(pBt);
 74816  75041         if( db->aDb[i].safety_level!=PAGER_SYNCHRONOUS_OFF
 74817  75042          && aMJNeeded[sqlite3PagerGetJournalMode(pPager)]
        75043  +       && sqlite3PagerIsMemdb(pPager)==0
 74818  75044         ){ 
 74819  75045           assert( i!=1 );
 74820  75046           nTrans++;
 74821  75047         }
 74822  75048         rc = sqlite3PagerExclusiveLock(pPager);
 74823  75049         sqlite3BtreeLeave(pBt);
 74824  75050       }
................................................................................
 90554  90780   SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
 90555  90781     if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
 90556  90782     if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
 90557  90783     if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
 90558  90784     if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
 90559  90785     if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
 90560  90786     if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
 90561         -  if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
 90562  90787     return WRC_Continue;
 90563  90788   }
 90564  90789   
 90565  90790   /*
 90566  90791   ** Walk the parse trees associated with all subqueries in the
 90567  90792   ** FROM clause of SELECT statement p.  Do not invoke the select
 90568  90793   ** callback on p, but do invoke it on each FROM clause subquery
................................................................................
 91822  92047       p->selFlags |= SF_Resolved;
 91823  92048   
 91824  92049       /* Resolve the expressions in the LIMIT and OFFSET clauses. These
 91825  92050       ** are not allowed to refer to any names, so pass an empty NameContext.
 91826  92051       */
 91827  92052       memset(&sNC, 0, sizeof(sNC));
 91828  92053       sNC.pParse = pParse;
 91829         -    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
 91830         -        sqlite3ResolveExprNames(&sNC, p->pOffset) ){
        92054  +    if( sqlite3ResolveExprNames(&sNC, p->pLimit) ){
 91831  92055         return WRC_Abort;
 91832  92056       }
 91833  92057   
 91834  92058       /* If the SF_Converted flags is set, then this Select object was
 91835  92059       ** was created by the convertCompoundSelectToSubquery() function.
 91836  92060       ** In this case the ORDER BY clause (p->pOrderBy) should be resolved
 91837  92061       ** as if it were part of the sub-query, not the parent. This block
................................................................................
 92824  93048     }
 92825  93049   }
 92826  93050   static void heightOfSelect(Select *p, int *pnHeight){
 92827  93051     if( p ){
 92828  93052       heightOfExpr(p->pWhere, pnHeight);
 92829  93053       heightOfExpr(p->pHaving, pnHeight);
 92830  93054       heightOfExpr(p->pLimit, pnHeight);
 92831         -    heightOfExpr(p->pOffset, pnHeight);
 92832  93055       heightOfExprList(p->pEList, pnHeight);
 92833  93056       heightOfExprList(p->pGroupBy, pnHeight);
 92834  93057       heightOfExprList(p->pOrderBy, pnHeight);
 92835  93058       heightOfSelect(p->pPrior, pnHeight);
 92836  93059     }
 92837  93060   }
 92838  93061   
................................................................................
 93623  93846       pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
 93624  93847       pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
 93625  93848       pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
 93626  93849       pNew->op = p->op;
 93627  93850       pNew->pNext = pNext;
 93628  93851       pNew->pPrior = 0;
 93629  93852       pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
 93630         -    pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
 93631  93853       pNew->iLimit = 0;
 93632  93854       pNew->iOffset = 0;
 93633  93855       pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
 93634  93856       pNew->addrOpenEphm[0] = -1;
 93635  93857       pNew->addrOpenEphm[1] = -1;
 93636  93858       pNew->nSelectRow = p->nSelectRow;
 93637  93859       pNew->pWith = withDup(db, p->pWith);
................................................................................
 94260  94482     if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
 94261  94483       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
 94262  94484       testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 94263  94485       return 0; /* No DISTINCT keyword and no aggregate functions */
 94264  94486     }
 94265  94487     assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
 94266  94488     if( p->pLimit ) return 0;              /* Has no LIMIT clause */
 94267         -  assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
 94268  94489     if( p->pWhere ) return 0;              /* Has no WHERE clause */
 94269  94490     pSrc = p->pSrc;
 94270  94491     assert( pSrc!=0 );
 94271  94492     if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
 94272  94493     if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
 94273  94494     pTab = pSrc->a[0].pTab;
 94274  94495     assert( pTab!=0 );
................................................................................
 94350  94571   **     SELECT <column1>, <column2>... FROM <table>
 94351  94572   **
 94352  94573   ** If the RHS of the IN operator is a list or a more complex subquery, then
 94353  94574   ** an ephemeral table might need to be generated from the RHS and then
 94354  94575   ** pX->iTable made to point to the ephemeral table instead of an
 94355  94576   ** existing table.
 94356  94577   **
 94357         -** The inFlags parameter must contain exactly one of the bits
 94358         -** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP.  If inFlags contains
 94359         -** IN_INDEX_MEMBERSHIP, then the generated table will be used for a
 94360         -** fast membership test.  When the IN_INDEX_LOOP bit is set, the
 94361         -** IN index will be used to loop over all values of the RHS of the
 94362         -** IN operator.
        94578  +** The inFlags parameter must contain, at a minimum, one of the bits
        94579  +** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP but not both.  If inFlags contains
        94580  +** IN_INDEX_MEMBERSHIP, then the generated table will be used for a fast
        94581  +** membership test.  When the IN_INDEX_LOOP bit is set, the IN index will
        94582  +** be used to loop over all values of the RHS of the IN operator.
 94363  94583   **
 94364  94584   ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate
 94365  94585   ** through the set members) then the b-tree must not contain duplicates.
 94366         -** An epheremal table must be used unless the selected columns are guaranteed
        94586  +** An epheremal table will be created unless the selected columns are guaranteed
 94367  94587   ** to be unique - either because it is an INTEGER PRIMARY KEY or due to
 94368  94588   ** a UNIQUE constraint or index.
 94369  94589   **
 94370  94590   ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 
 94371  94591   ** for fast set membership tests) then an epheremal table must 
 94372  94592   ** be used unless <columns> is a single INTEGER PRIMARY KEY column or an 
 94373  94593   ** index can be found with the specified <columns> as its left-most.
................................................................................
 94900  95120         **
 94901  95121         ** In both cases, the query is augmented with "LIMIT 1".  Any 
 94902  95122         ** preexisting limit is discarded in place of the new LIMIT 1.
 94903  95123         */
 94904  95124         Select *pSel;                         /* SELECT statement to encode */
 94905  95125         SelectDest dest;                      /* How to deal with SELECT result */
 94906  95126         int nReg;                             /* Registers to allocate */
        95127  +      Expr *pLimit;                         /* New limit expression */
 94907  95128   
 94908  95129         testcase( pExpr->op==TK_EXISTS );
 94909  95130         testcase( pExpr->op==TK_SELECT );
 94910  95131         assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
 94911  95132         assert( ExprHasProperty(pExpr, EP_xIsSelect) );
 94912  95133   
 94913  95134         pSel = pExpr->x.pSelect;
................................................................................
 94921  95142           sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1);
 94922  95143           VdbeComment((v, "Init subquery result"));
 94923  95144         }else{
 94924  95145           dest.eDest = SRT_Exists;
 94925  95146           sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
 94926  95147           VdbeComment((v, "Init EXISTS result"));
 94927  95148         }
 94928         -      sqlite3ExprDelete(pParse->db, pSel->pLimit);
 94929         -      pSel->pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,
 94930         -                                  &sqlite3IntTokens[1], 0);
        95149  +      pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0);
        95150  +      if( pSel->pLimit ){
        95151  +        sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
        95152  +        pSel->pLimit->pLeft = pLimit;
        95153  +      }else{
        95154  +        pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
        95155  +      }
 94931  95156         pSel->iLimit = 0;
 94932  95157         pSel->selFlags &= ~SF_MultiValue;
 94933  95158         if( sqlite3Select(pParse, pSel, &dest) ){
 94934  95159           return 0;
 94935  95160         }
 94936  95161         rReg = dest.iSDParm;
 94937  95162         ExprSetVVAProperty(pExpr, EP_NoReduce);
................................................................................
100711 100936       }
100712 100937       if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
100713 100938         return 1;
100714 100939       }
100715 100940       if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
100716 100941         return 1;
100717 100942       }
100718         -    if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
100719         -      return 1;
100720         -    }
100721 100943       pSelect = pSelect->pPrior;
100722 100944     }
100723 100945     return 0;
100724 100946   }
100725 100947   SQLITE_PRIVATE int sqlite3FixExpr(
100726 100948     DbFixer *pFix,     /* Context of the fixation */
100727 100949     Expr *pExpr        /* The expression to be fixed to one database */
................................................................................
106083 106305   */
106084 106306   SQLITE_PRIVATE void sqlite3MaterializeView(
106085 106307     Parse *pParse,       /* Parsing context */
106086 106308     Table *pView,        /* View definition */
106087 106309     Expr *pWhere,        /* Optional WHERE clause to be added */
106088 106310     ExprList *pOrderBy,  /* Optional ORDER BY clause */
106089 106311     Expr *pLimit,        /* Optional LIMIT clause */
106090         -  Expr *pOffset,       /* Optional OFFSET clause */
106091 106312     int iCur             /* Cursor number for ephemeral table */
106092 106313   ){
106093 106314     SelectDest dest;
106094 106315     Select *pSel;
106095 106316     SrcList *pFrom;
106096 106317     sqlite3 *db = pParse->db;
106097 106318     int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
................................................................................
106101 106322       assert( pFrom->nSrc==1 );
106102 106323       pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
106103 106324       pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zDbSName);
106104 106325       assert( pFrom->a[0].pOn==0 );
106105 106326       assert( pFrom->a[0].pUsing==0 );
106106 106327     }
106107 106328     pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, pOrderBy, 
106108         -                          SF_IncludeHidden, pLimit, pOffset);
       106329  +                          SF_IncludeHidden, pLimit);
106109 106330     sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
106110 106331     sqlite3Select(pParse, pSel, &dest);
106111 106332     sqlite3SelectDelete(db, pSel);
106112 106333   }
106113 106334   #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
106114 106335   
106115 106336   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
................................................................................
106123 106344   */
106124 106345   SQLITE_PRIVATE Expr *sqlite3LimitWhere(
106125 106346     Parse *pParse,               /* The parser context */
106126 106347     SrcList *pSrc,               /* the FROM clause -- which tables to scan */
106127 106348     Expr *pWhere,                /* The WHERE clause.  May be null */
106128 106349     ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
106129 106350     Expr *pLimit,                /* The LIMIT clause.  May be null */
106130         -  Expr *pOffset,               /* The OFFSET clause.  May be null */
106131 106351     char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
106132 106352   ){
106133 106353     sqlite3 *db = pParse->db;
106134 106354     Expr *pLhs = NULL;           /* LHS of IN(SELECT...) operator */
106135 106355     Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
106136 106356     ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
106137 106357     SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
................................................................................
106140 106360   
106141 106361     /* Check that there isn't an ORDER BY without a LIMIT clause.
106142 106362     */
106143 106363     if( pOrderBy && pLimit==0 ) {
106144 106364       sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
106145 106365       sqlite3ExprDelete(pParse->db, pWhere);
106146 106366       sqlite3ExprListDelete(pParse->db, pOrderBy);
106147         -    sqlite3ExprDelete(pParse->db, pLimit);
106148         -    sqlite3ExprDelete(pParse->db, pOffset);
106149 106367       return 0;
106150 106368     }
106151 106369   
106152 106370     /* We only need to generate a select expression if there
106153 106371     ** is a limit/offset term to enforce.
106154 106372     */
106155 106373     if( pLimit == 0 ) {
106156         -    /* if pLimit is null, pOffset will always be null as well. */
106157         -    assert( pOffset == 0 );
106158 106374       return pWhere;
106159 106375     }
106160 106376   
106161 106377     /* Generate a select expression tree to enforce the limit/offset 
106162 106378     ** term for the DELETE or UPDATE statement.  For example:
106163 106379     **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
106164 106380     ** becomes:
................................................................................
106197 106413     pSrc->a[0].pTab = 0;
106198 106414     pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
106199 106415     pSrc->a[0].pTab = pTab;
106200 106416     pSrc->a[0].pIBIndex = 0;
106201 106417   
106202 106418     /* generate the SELECT expression tree. */
106203 106419     pSelect = sqlite3SelectNew(pParse, pEList, pSelectSrc, pWhere, 0 ,0, 
106204         -      pOrderBy,0,pLimit,pOffset
       106420  +      pOrderBy,0,pLimit
106205 106421     );
106206 106422   
106207 106423     /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
106208 106424     pInClause = sqlite3PExpr(pParse, TK_IN, pLhs, 0);
106209 106425     sqlite3PExprAddSelect(pParse, pInClause, pSelect);
106210 106426     return pInClause;
106211 106427   }
................................................................................
106220 106436   **                  pTabList              pWhere
106221 106437   */
106222 106438   SQLITE_PRIVATE void sqlite3DeleteFrom(
106223 106439     Parse *pParse,         /* The parser context */
106224 106440     SrcList *pTabList,     /* The table from which we should delete things */
106225 106441     Expr *pWhere,          /* The WHERE clause.  May be null */
106226 106442     ExprList *pOrderBy,    /* ORDER BY clause. May be null */
106227         -  Expr *pLimit,          /* LIMIT clause. May be null */
106228         -  Expr *pOffset          /* OFFSET clause. May be null */
       106443  +  Expr *pLimit           /* LIMIT clause. May be null */
106229 106444   ){
106230 106445     Vdbe *v;               /* The virtual database engine */
106231 106446     Table *pTab;           /* The table from which records will be deleted */
106232 106447     int i;                 /* Loop counter */
106233 106448     WhereInfo *pWInfo;     /* Information about the WHERE clause */
106234 106449     Index *pIdx;           /* For looping over indices of the table */
106235 106450     int iTabCur;           /* Cursor number for the table */
................................................................................
106294 106509   # undef isView
106295 106510   # define isView 0
106296 106511   #endif
106297 106512   
106298 106513   #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
106299 106514     if( !isView ){
106300 106515       pWhere = sqlite3LimitWhere(
106301         -        pParse, pTabList, pWhere, pOrderBy, pLimit, pOffset, "DELETE"
       106516  +        pParse, pTabList, pWhere, pOrderBy, pLimit, "DELETE"
106302 106517       );
106303 106518       pOrderBy = 0;
106304         -    pLimit = pOffset = 0;
       106519  +    pLimit = 0;
106305 106520     }
106306 106521   #endif
106307 106522   
106308 106523     /* If pTab is really a view, make sure it has been initialized.
106309 106524     */
106310 106525     if( sqlite3ViewGetColumnNames(pParse, pTab) ){
106311 106526       goto delete_from_cleanup;
................................................................................
106349 106564   
106350 106565     /* If we are trying to delete from a view, realize that view into
106351 106566     ** an ephemeral table.
106352 106567     */
106353 106568   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
106354 106569     if( isView ){
106355 106570       sqlite3MaterializeView(pParse, pTab, 
106356         -        pWhere, pOrderBy, pLimit, pOffset, iTabCur
       106571  +        pWhere, pOrderBy, pLimit, iTabCur
106357 106572       );
106358 106573       iDataCur = iIdxCur = iTabCur;
106359 106574       pOrderBy = 0;
106360         -    pLimit = pOffset = 0;
       106575  +    pLimit = 0;
106361 106576     }
106362 106577   #endif
106363 106578   
106364 106579     /* Resolve the column names in the WHERE clause.
106365 106580     */
106366 106581     memset(&sNC, 0, sizeof(sNC));
106367 106582     sNC.pParse = pParse;
................................................................................
106600 106815   delete_from_cleanup:
106601 106816     sqlite3AuthContextPop(&sContext);
106602 106817     sqlite3SrcListDelete(db, pTabList);
106603 106818     sqlite3ExprDelete(db, pWhere);
106604 106819   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) 
106605 106820     sqlite3ExprListDelete(db, pOrderBy);
106606 106821     sqlite3ExprDelete(db, pLimit);
106607         -  sqlite3ExprDelete(db, pOffset);
106608 106822   #endif
106609 106823     sqlite3DbFree(db, aToOpen);
106610 106824     return;
106611 106825   }
106612 106826   /* Make sure "isView" and other macros defined above are undefined. Otherwise
106613 106827   ** they may interfere with compilation of other functions in this file
106614 106828   ** (or in another file, if this file becomes part of the amalgamation).  */
................................................................................
109566 109780         }
109567 109781         if( !p ) return;
109568 109782         iSkip = sqlite3VdbeMakeLabel(v);
109569 109783         sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
109570 109784       }
109571 109785   
109572 109786       pParse->disableTriggers = 1;
109573         -    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0, 0);
       109787  +    sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0, 0, 0);
109574 109788       pParse->disableTriggers = 0;
109575 109789   
109576 109790       /* If the DELETE has generated immediate foreign key constraint 
109577 109791       ** violations, halt the VDBE and return an error at this point, before
109578 109792       ** any modifications to the schema are made. This is because statement
109579 109793       ** transactions are not able to rollback schema changes.  
109580 109794       **
................................................................................
110124 110338         if( pRaise ){
110125 110339           pRaise->affinity = OE_Abort;
110126 110340         }
110127 110341         pSelect = sqlite3SelectNew(pParse, 
110128 110342             sqlite3ExprListAppend(pParse, 0, pRaise),
110129 110343             sqlite3SrcListAppend(db, 0, &tFrom, 0),
110130 110344             pWhere,
110131         -          0, 0, 0, 0, 0, 0
       110345  +          0, 0, 0, 0, 0
110132 110346         );
110133 110347         pWhere = 0;
110134 110348       }
110135 110349   
110136 110350       /* Disable lookaside memory allocation */
110137 110351       db->lookaside.bDisable++;
110138 110352   
................................................................................
112272 112486     ** there is no ORDER BY, we will get an error. */
112273 112487     if( pSelect->pGroupBy ){
112274 112488       return 0;   /* SELECT may not have a GROUP BY clause */
112275 112489     }
112276 112490     if( pSelect->pLimit ){
112277 112491       return 0;   /* SELECT may not have a LIMIT clause */
112278 112492     }
112279         -  assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
112280 112493     if( pSelect->pPrior ){
112281 112494       return 0;   /* SELECT may not be a compound query */
112282 112495     }
112283 112496     if( pSelect->selFlags & SF_Distinct ){
112284 112497       return 0;   /* SELECT may not be DISTINCT */
112285 112498     }
112286 112499     pEList = pSelect->pEList;
................................................................................
118171 118384       sqlite3ExprListDelete(db, p->pEList);
118172 118385       sqlite3SrcListDelete(db, p->pSrc);
118173 118386       sqlite3ExprDelete(db, p->pWhere);
118174 118387       sqlite3ExprListDelete(db, p->pGroupBy);
118175 118388       sqlite3ExprDelete(db, p->pHaving);
118176 118389       sqlite3ExprListDelete(db, p->pOrderBy);
118177 118390       sqlite3ExprDelete(db, p->pLimit);
118178         -    sqlite3ExprDelete(db, p->pOffset);
118179 118391       if( OK_IF_ALWAYS_TRUE(p->pWith) ) sqlite3WithDelete(db, p->pWith);
118180 118392       if( bFree ) sqlite3DbFreeNN(db, p);
118181 118393       p = pPrior;
118182 118394       bFree = 1;
118183 118395     }
118184 118396   }
118185 118397   
................................................................................
118204 118416     ExprList *pEList,     /* which columns to include in the result */
118205 118417     SrcList *pSrc,        /* the FROM clause -- which tables to scan */
118206 118418     Expr *pWhere,         /* the WHERE clause */
118207 118419     ExprList *pGroupBy,   /* the GROUP BY clause */
118208 118420     Expr *pHaving,        /* the HAVING clause */
118209 118421     ExprList *pOrderBy,   /* the ORDER BY clause */
118210 118422     u32 selFlags,         /* Flag parameters, such as SF_Distinct */
118211         -  Expr *pLimit,         /* LIMIT value.  NULL means not used */
118212         -  Expr *pOffset         /* OFFSET value.  NULL means no offset */
       118423  +  Expr *pLimit          /* LIMIT value.  NULL means not used */
118213 118424   ){
118214 118425     Select *pNew;
118215 118426     Select standin;
118216 118427     pNew = sqlite3DbMallocRawNN(pParse->db, sizeof(*pNew) );
118217 118428     if( pNew==0 ){
118218 118429       assert( pParse->db->mallocFailed );
118219 118430       pNew = &standin;
................................................................................
118238 118449     pNew->pWhere = pWhere;
118239 118450     pNew->pGroupBy = pGroupBy;
118240 118451     pNew->pHaving = pHaving;
118241 118452     pNew->pOrderBy = pOrderBy;
118242 118453     pNew->pPrior = 0;
118243 118454     pNew->pNext = 0;
118244 118455     pNew->pLimit = pLimit;
118245         -  pNew->pOffset = pOffset;
118246 118456     pNew->pWith = 0;
118247         -  assert( pOffset==0 || pLimit!=0 || pParse->nErr>0
118248         -                     || pParse->db->mallocFailed!=0 );
118249 118457     if( pParse->db->mallocFailed ) {
118250 118458       clearSelect(pParse->db, pNew, pNew!=&standin);
118251 118459       pNew = 0;
118252 118460     }else{
118253 118461       assert( pNew->pSrc!=0 || pParse->nErr>0 );
118254 118462     }
118255 118463     assert( pNew!=&standin );
................................................................................
119971 120179     }
119972 120180     return sqlite3VdbeCreate(pParse);
119973 120181   }
119974 120182   
119975 120183   
119976 120184   /*
119977 120185   ** Compute the iLimit and iOffset fields of the SELECT based on the
119978         -** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
       120186  +** pLimit expressions.  pLimit->pLeft and pLimit->pRight hold the expressions
119979 120187   ** that appear in the original SQL statement after the LIMIT and OFFSET
119980 120188   ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset 
119981 120189   ** are the integer memory register numbers for counters used to compute 
119982 120190   ** the limit and offset.  If there is no limit and/or offset, then 
119983 120191   ** iLimit and iOffset are negative.
119984 120192   **
119985 120193   ** This routine changes the values of iLimit and iOffset only if
119986         -** a limit or offset is defined by pLimit and pOffset.  iLimit and
119987         -** iOffset should have been preset to appropriate default values (zero)
       120194  +** a limit or offset is defined by pLimit->pLeft and pLimit->pRight.  iLimit
       120195  +** and iOffset should have been preset to appropriate default values (zero)
119988 120196   ** prior to calling this routine.
119989 120197   **
119990 120198   ** The iOffset register (if it exists) is initialized to the value
119991 120199   ** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
119992 120200   ** iOffset+1 is initialized to LIMIT+OFFSET.
119993 120201   **
119994         -** Only if pLimit!=0 or pOffset!=0 do the limit registers get
       120202  +** Only if pLimit->pLeft!=0 do the limit registers get
119995 120203   ** redefined.  The UNION ALL operator uses this property to force
119996 120204   ** the reuse of the same limit and offset registers across multiple
119997 120205   ** SELECT statements.
119998 120206   */
119999 120207   static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
120000 120208     Vdbe *v = 0;
120001 120209     int iLimit = 0;
120002 120210     int iOffset;
120003 120211     int n;
       120212  +  Expr *pLimit = p->pLimit;
       120213  +
120004 120214     if( p->iLimit ) return;
120005 120215   
120006 120216     /* 
120007 120217     ** "LIMIT -1" always shows all rows.  There is some
120008 120218     ** controversy about what the correct behavior should be.
120009 120219     ** The current implementation interprets "LIMIT 0" to mean
120010 120220     ** no rows.
120011 120221     */
120012 120222     sqlite3ExprCacheClear(pParse);
120013         -  assert( p->pOffset==0 || p->pLimit!=0 );
120014         -  if( p->pLimit ){
       120223  +  if( pLimit ){
       120224  +    assert( pLimit->op==TK_LIMIT );
       120225  +    assert( pLimit->pLeft!=0 );
120015 120226       p->iLimit = iLimit = ++pParse->nMem;
120016 120227       v = sqlite3GetVdbe(pParse);
120017 120228       assert( v!=0 );
120018         -    if( sqlite3ExprIsInteger(p->pLimit, &n) ){
       120229  +    if( sqlite3ExprIsInteger(pLimit->pLeft, &n) ){
120019 120230         sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
120020 120231         VdbeComment((v, "LIMIT counter"));
120021 120232         if( n==0 ){
120022 120233           sqlite3VdbeGoto(v, iBreak);
120023 120234         }else if( n>=0 && p->nSelectRow>sqlite3LogEst((u64)n) ){
120024 120235           p->nSelectRow = sqlite3LogEst((u64)n);
120025 120236           p->selFlags |= SF_FixedLimit;
120026 120237         }
120027 120238       }else{
120028         -      sqlite3ExprCode(pParse, p->pLimit, iLimit);
       120239  +      sqlite3ExprCode(pParse, pLimit->pLeft, iLimit);
120029 120240         sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
120030 120241         VdbeComment((v, "LIMIT counter"));
120031 120242         sqlite3VdbeAddOp2(v, OP_IfNot, iLimit, iBreak); VdbeCoverage(v);
120032 120243       }
120033         -    if( p->pOffset ){
       120244  +    if( pLimit->pRight ){
120034 120245         p->iOffset = iOffset = ++pParse->nMem;
120035 120246         pParse->nMem++;   /* Allocate an extra register for limit+offset */
120036         -      sqlite3ExprCode(pParse, p->pOffset, iOffset);
       120247  +      sqlite3ExprCode(pParse, pLimit->pRight, iOffset);
120037 120248         sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
120038 120249         VdbeComment((v, "OFFSET counter"));
120039 120250         sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
120040 120251         VdbeComment((v, "LIMIT+OFFSET"));
120041 120252       }
120042 120253     }
120043 120254   }
................................................................................
120159 120370     int iQueue;                   /* The Queue table */
120160 120371     int iDistinct = 0;            /* To ensure unique results if UNION */
120161 120372     int eDest = SRT_Fifo;         /* How to write to Queue */
120162 120373     SelectDest destQueue;         /* SelectDest targetting the Queue table */
120163 120374     int i;                        /* Loop counter */
120164 120375     int rc;                       /* Result code */
120165 120376     ExprList *pOrderBy;           /* The ORDER BY clause */
120166         -  Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
       120377  +  Expr *pLimit;                 /* Saved LIMIT and OFFSET */
120167 120378     int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
120168 120379   
120169 120380     /* Obtain authorization to do a recursive query */
120170 120381     if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
120171 120382   
120172 120383     /* Process the LIMIT and OFFSET clauses, if they exist */
120173 120384     addrBreak = sqlite3VdbeMakeLabel(v);
120174 120385     p->nSelectRow = 320;  /* 4 billion rows */
120175 120386     computeLimitRegisters(pParse, p, addrBreak);
120176 120387     pLimit = p->pLimit;
120177         -  pOffset = p->pOffset;
120178 120388     regLimit = p->iLimit;
120179 120389     regOffset = p->iOffset;
120180         -  p->pLimit = p->pOffset = 0;
       120390  +  p->pLimit = 0;
120181 120391     p->iLimit = p->iOffset = 0;
120182 120392     pOrderBy = p->pOrderBy;
120183 120393   
120184 120394     /* Locate the cursor number of the Current table */
120185 120395     for(i=0; ALWAYS(i<pSrc->nSrc); i++){
120186 120396       if( pSrc->a[i].fg.isRecursive ){
120187 120397         iCurrent = pSrc->a[i].iCursor;
................................................................................
120266 120476     sqlite3VdbeGoto(v, addrTop);
120267 120477     sqlite3VdbeResolveLabel(v, addrBreak);
120268 120478   
120269 120479   end_of_recursive_query:
120270 120480     sqlite3ExprListDelete(pParse->db, p->pOrderBy);
120271 120481     p->pOrderBy = pOrderBy;
120272 120482     p->pLimit = pLimit;
120273         -  p->pOffset = pOffset;
120274 120483     return;
120275 120484   }
120276 120485   #endif /* SQLITE_OMIT_CTE */
120277 120486   
120278 120487   /* Forward references */
120279 120488   static int multiSelectOrderBy(
120280 120489     Parse *pParse,        /* Parsing context */
................................................................................
120302 120511     int nRow = 1;
120303 120512     int rc = 0;
120304 120513     assert( p->selFlags & SF_MultiValue );
120305 120514     do{
120306 120515       assert( p->selFlags & SF_Values );
120307 120516       assert( p->op==TK_ALL || (p->op==TK_SELECT && p->pPrior==0) );
120308 120517       assert( p->pLimit==0 );
120309         -    assert( p->pOffset==0 );
120310 120518       assert( p->pNext==0 || p->pEList->nExpr==p->pNext->pEList->nExpr );
120311 120519       if( p->pPrior==0 ) break;
120312 120520       assert( p->pPrior->pNext==p );
120313 120521       p = p->pPrior;
120314 120522       nRow++;
120315 120523     }while(1);
120316 120524     while( p ){
................................................................................
120429 120637       case TK_ALL: {
120430 120638         int addr = 0;
120431 120639         int nLimit;
120432 120640         assert( !pPrior->pLimit );
120433 120641         pPrior->iLimit = p->iLimit;
120434 120642         pPrior->iOffset = p->iOffset;
120435 120643         pPrior->pLimit = p->pLimit;
120436         -      pPrior->pOffset = p->pOffset;
120437 120644         explainSetInteger(iSub1, pParse->iNextSelectId);
120438 120645         rc = sqlite3Select(pParse, pPrior, &dest);
120439 120646         p->pLimit = 0;
120440         -      p->pOffset = 0;
120441 120647         if( rc ){
120442 120648           goto multi_select_end;
120443 120649         }
120444 120650         p->pPrior = 0;
120445 120651         p->iLimit = pPrior->iLimit;
120446 120652         p->iOffset = pPrior->iOffset;
120447 120653         if( p->iLimit ){
................................................................................
120455 120661         explainSetInteger(iSub2, pParse->iNextSelectId);
120456 120662         rc = sqlite3Select(pParse, p, &dest);
120457 120663         testcase( rc!=SQLITE_OK );
120458 120664         pDelete = p->pPrior;
120459 120665         p->pPrior = pPrior;
120460 120666         p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
120461 120667         if( pPrior->pLimit
120462         -       && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
       120668  +       && sqlite3ExprIsInteger(pPrior->pLimit->pLeft, &nLimit)
120463 120669          && nLimit>0 && p->nSelectRow > sqlite3LogEst((u64)nLimit) 
120464 120670         ){
120465 120671           p->nSelectRow = sqlite3LogEst((u64)nLimit);
120466 120672         }
120467 120673         if( addr ){
120468 120674           sqlite3VdbeJumpHere(v, addr);
120469 120675         }
................................................................................
120470 120676         break;
120471 120677       }
120472 120678       case TK_EXCEPT:
120473 120679       case TK_UNION: {
120474 120680         int unionTab;    /* Cursor number of the temporary table holding result */
120475 120681         u8 op = 0;       /* One of the SRT_ operations to apply to self */
120476 120682         int priorOp;     /* The SRT_ operation to apply to prior selects */
120477         -      Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
       120683  +      Expr *pLimit;    /* Saved values of p->nLimit  */
120478 120684         int addr;
120479 120685         SelectDest uniondest;
120480 120686   
120481 120687         testcase( p->op==TK_EXCEPT );
120482 120688         testcase( p->op==TK_UNION );
120483 120689         priorOp = SRT_Union;
120484 120690         if( dest.eDest==priorOp ){
120485 120691           /* We can reuse a temporary table generated by a SELECT to our
120486 120692           ** right.
120487 120693           */
120488 120694           assert( p->pLimit==0 );      /* Not allowed on leftward elements */
120489         -        assert( p->pOffset==0 );     /* Not allowed on leftward elements */
120490 120695           unionTab = dest.iSDParm;
120491 120696         }else{
120492 120697           /* We will need to create our own temporary table to hold the
120493 120698           ** intermediate results.
120494 120699           */
120495 120700           unionTab = pParse->nTab++;
120496 120701           assert( p->pOrderBy==0 );
................................................................................
120518 120723         }else{
120519 120724           assert( p->op==TK_UNION );
120520 120725           op = SRT_Union;
120521 120726         }
120522 120727         p->pPrior = 0;
120523 120728         pLimit = p->pLimit;
120524 120729         p->pLimit = 0;
120525         -      pOffset = p->pOffset;
120526         -      p->pOffset = 0;
120527 120730         uniondest.eDest = op;
120528 120731         explainSetInteger(iSub2, pParse->iNextSelectId);
120529 120732         rc = sqlite3Select(pParse, p, &uniondest);
120530 120733         testcase( rc!=SQLITE_OK );
120531 120734         /* Query flattening in sqlite3Select() might refill p->pOrderBy.
120532 120735         ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
120533 120736         sqlite3ExprListDelete(db, p->pOrderBy);
................................................................................
120535 120738         p->pPrior = pPrior;
120536 120739         p->pOrderBy = 0;
120537 120740         if( p->op==TK_UNION ){
120538 120741           p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
120539 120742         }
120540 120743         sqlite3ExprDelete(db, p->pLimit);
120541 120744         p->pLimit = pLimit;
120542         -      p->pOffset = pOffset;
120543 120745         p->iLimit = 0;
120544 120746         p->iOffset = 0;
120545 120747   
120546 120748         /* Convert the data in the temporary table into whatever form
120547 120749         ** it is that we currently need.
120548 120750         */
120549 120751         assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
................................................................................
120563 120765           sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
120564 120766         }
120565 120767         break;
120566 120768       }
120567 120769       default: assert( p->op==TK_INTERSECT ); {
120568 120770         int tab1, tab2;
120569 120771         int iCont, iBreak, iStart;
120570         -      Expr *pLimit, *pOffset;
       120772  +      Expr *pLimit;
120571 120773         int addr;
120572 120774         SelectDest intersectdest;
120573 120775         int r1;
120574 120776   
120575 120777         /* INTERSECT is different from the others since it requires
120576 120778         ** two temporary tables.  Hence it has its own case.  Begin
120577 120779         ** by allocating the tables we will need.
................................................................................
120599 120801         */
120600 120802         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
120601 120803         assert( p->addrOpenEphm[1] == -1 );
120602 120804         p->addrOpenEphm[1] = addr;
120603 120805         p->pPrior = 0;
120604 120806         pLimit = p->pLimit;
120605 120807         p->pLimit = 0;
120606         -      pOffset = p->pOffset;
120607         -      p->pOffset = 0;
120608 120808         intersectdest.iSDParm = tab2;
120609 120809         explainSetInteger(iSub2, pParse->iNextSelectId);
120610 120810         rc = sqlite3Select(pParse, p, &intersectdest);
120611 120811         testcase( rc!=SQLITE_OK );
120612 120812         pDelete = p->pPrior;
120613 120813         p->pPrior = pPrior;
120614 120814         if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
120615 120815         sqlite3ExprDelete(db, p->pLimit);
120616 120816         p->pLimit = pLimit;
120617         -      p->pOffset = pOffset;
120618 120817   
120619 120818         /* Generate code to take the intersection of the two temporary
120620 120819         ** tables.
120621 120820         */
120622 120821         assert( p->pEList );
120623 120822         iBreak = sqlite3VdbeMakeLabel(v);
120624 120823         iCont = sqlite3VdbeMakeLabel(v);
................................................................................
121089 121288                                     regLimitA);
121090 121289       sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
121091 121290     }else{
121092 121291       regLimitA = regLimitB = 0;
121093 121292     }
121094 121293     sqlite3ExprDelete(db, p->pLimit);
121095 121294     p->pLimit = 0;
121096         -  sqlite3ExprDelete(db, p->pOffset);
121097         -  p->pOffset = 0;
121098 121295   
121099 121296     regAddrA = ++pParse->nMem;
121100 121297     regAddrB = ++pParse->nMem;
121101 121298     regOutA = ++pParse->nMem;
121102 121299     regOutB = ++pParse->nMem;
121103 121300     sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
121104 121301     sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
................................................................................
121554 121751     assert( pSubSrc );
121555 121752     /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
121556 121753     ** not arbitrary expressions, we allowed some combining of LIMIT and OFFSET
121557 121754     ** because they could be computed at compile-time.  But when LIMIT and OFFSET
121558 121755     ** became arbitrary expressions, we were forced to add restrictions (13)
121559 121756     ** and (14). */
121560 121757     if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
121561         -  if( pSub->pOffset ) return 0;                          /* Restriction (14) */
       121758  +  if( pSub->pLimit && pSub->pLimit->pRight ) return 0;   /* Restriction (14) */
121562 121759     if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
121563 121760       return 0;                                            /* Restriction (15) */
121564 121761     }
121565 121762     if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
121566 121763     if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (4)  */
121567 121764     if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
121568 121765        return 0;         /* Restrictions (8)(9) */
................................................................................
121702 121899     **
121703 121900     ** We call this the "compound-subquery flattening".
121704 121901     */
121705 121902     for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
121706 121903       Select *pNew;
121707 121904       ExprList *pOrderBy = p->pOrderBy;
121708 121905       Expr *pLimit = p->pLimit;
121709         -    Expr *pOffset = p->pOffset;
121710 121906       Select *pPrior = p->pPrior;
121711 121907       p->pOrderBy = 0;
121712 121908       p->pSrc = 0;
121713 121909       p->pPrior = 0;
121714 121910       p->pLimit = 0;
121715         -    p->pOffset = 0;
121716 121911       pNew = sqlite3SelectDup(db, p, 0);
121717 121912       sqlite3SelectSetName(pNew, pSub->zSelName);
121718         -    p->pOffset = pOffset;
121719 121913       p->pLimit = pLimit;
121720 121914       p->pOrderBy = pOrderBy;
121721 121915       p->pSrc = pSrc;
121722 121916       p->op = TK_ALL;
121723 121917       if( pNew==0 ){
121724 121918         p->pPrior = pPrior;
121725 121919       }else{
................................................................................
122177 122371     p->pWith = 0;
122178 122372     p->selFlags &= ~SF_Compound;
122179 122373     assert( (p->selFlags & SF_Converted)==0 );
122180 122374     p->selFlags |= SF_Converted;
122181 122375     assert( pNew->pPrior!=0 );
122182 122376     pNew->pPrior->pNext = pNew;
122183 122377     pNew->pLimit = 0;
122184         -  pNew->pOffset = 0;
122185 122378     return WRC_Continue;
122186 122379   }
122187 122380   
122188 122381   /*
122189 122382   ** Check to see if the FROM clause term pFrom has table-valued function
122190 122383   ** arguments.  If it does, leave an error message in pParse and return
122191 122384   ** non-zero, since pFrom is not allowed to be a table-valued function.
................................................................................
125075 125268   
125076 125269       switch( pStep->op ){
125077 125270         case TK_UPDATE: {
125078 125271           sqlite3Update(pParse, 
125079 125272             targetSrcList(pParse, pStep),
125080 125273             sqlite3ExprListDup(db, pStep->pExprList, 0), 
125081 125274             sqlite3ExprDup(db, pStep->pWhere, 0), 
125082         -          pParse->eOrconf, 0, 0, 0
       125275  +          pParse->eOrconf, 0, 0
125083 125276           );
125084 125277           break;
125085 125278         }
125086 125279         case TK_INSERT: {
125087 125280           sqlite3Insert(pParse, 
125088 125281             targetSrcList(pParse, pStep),
125089 125282             sqlite3SelectDup(db, pStep->pSelect, 0), 
................................................................................
125091 125284             pParse->eOrconf
125092 125285           );
125093 125286           break;
125094 125287         }
125095 125288         case TK_DELETE: {
125096 125289           sqlite3DeleteFrom(pParse, 
125097 125290             targetSrcList(pParse, pStep),
125098         -          sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0, 0
       125291  +          sqlite3ExprDup(db, pStep->pWhere, 0), 0, 0
125099 125292           );
125100 125293           break;
125101 125294         }
125102 125295         default: assert( pStep->op==TK_SELECT ); {
125103 125296           SelectDest sDest;
125104 125297           Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
125105 125298           sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
................................................................................
125239 125432       if( iEndTrigger ){
125240 125433         sqlite3VdbeResolveLabel(v, iEndTrigger);
125241 125434       }
125242 125435       sqlite3VdbeAddOp0(v, OP_Halt);
125243 125436       VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
125244 125437   
125245 125438       transferParseError(pParse, pSubParse);
125246         -    if( db->mallocFailed==0 ){
       125439  +    if( db->mallocFailed==0 && pParse->nErr==0 ){
125247 125440         pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
125248 125441       }
125249 125442       pProgram->nMem = pSubParse->nMem;
125250 125443       pProgram->nCsr = pSubParse->nTab;
125251 125444       pProgram->token = (void *)pTrigger;
125252 125445       pPrg->aColmask[0] = pSubParse->oldmask;
125253 125446       pPrg->aColmask[1] = pSubParse->newmask;
................................................................................
125560 125753   SQLITE_PRIVATE void sqlite3Update(
125561 125754     Parse *pParse,         /* The parser context */
125562 125755     SrcList *pTabList,     /* The table in which we should change things */
125563 125756     ExprList *pChanges,    /* Things to be changed */
125564 125757     Expr *pWhere,          /* The WHERE clause.  May be null */
125565 125758     int onError,           /* How to handle constraint errors */
125566 125759     ExprList *pOrderBy,    /* ORDER BY clause. May be null */
125567         -  Expr *pLimit,          /* LIMIT clause. May be null */
125568         -  Expr *pOffset          /* OFFSET clause. May be null */
       125760  +  Expr *pLimit           /* LIMIT clause. May be null */
125569 125761   ){
125570 125762     int i, j;              /* Loop counters */
125571 125763     Table *pTab;           /* The table to be updated */
125572 125764     int addrTop = 0;       /* VDBE instruction address of the start of the loop */
125573 125765     WhereInfo *pWInfo;     /* Information about the WHERE clause */
125574 125766     Vdbe *v;               /* The virtual database engine */
125575 125767     Index *pIdx;           /* For looping over indices */
................................................................................
125649 125841   # undef isView
125650 125842   # define isView 0
125651 125843   #endif
125652 125844   
125653 125845   #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
125654 125846     if( !isView ){
125655 125847       pWhere = sqlite3LimitWhere(
125656         -        pParse, pTabList, pWhere, pOrderBy, pLimit, pOffset, "UPDATE"
       125848  +        pParse, pTabList, pWhere, pOrderBy, pLimit, "UPDATE"
125657 125849       );
125658 125850       pOrderBy = 0;
125659         -    pLimit = pOffset = 0;
       125851  +    pLimit = 0;
125660 125852     }
125661 125853   #endif
125662 125854   
125663 125855     if( sqlite3ViewGetColumnNames(pParse, pTab) ){
125664 125856       goto update_cleanup;
125665 125857     }
125666 125858     if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
................................................................................
125825 126017   
125826 126018     /* If we are trying to update a view, realize that view into
125827 126019     ** an ephemeral table.
125828 126020     */
125829 126021   #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
125830 126022     if( isView ){
125831 126023       sqlite3MaterializeView(pParse, pTab, 
125832         -        pWhere, pOrderBy, pLimit, pOffset, iDataCur
       126024  +        pWhere, pOrderBy, pLimit, iDataCur
125833 126025       );
125834 126026       pOrderBy = 0;
125835         -    pLimit = pOffset = 0;
       126027  +    pLimit = 0;
125836 126028     }
125837 126029   #endif
125838 126030   
125839 126031     /* Resolve the column names in all the expressions in the
125840 126032     ** WHERE clause.
125841 126033     */
125842 126034     if( sqlite3ResolveExprNames(&sNC, pWhere) ){
................................................................................
126215 126407     sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
126216 126408     sqlite3SrcListDelete(db, pTabList);
126217 126409     sqlite3ExprListDelete(db, pChanges);
126218 126410     sqlite3ExprDelete(db, pWhere);
126219 126411   #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) 
126220 126412     sqlite3ExprListDelete(db, pOrderBy);
126221 126413     sqlite3ExprDelete(db, pLimit);
126222         -  sqlite3ExprDelete(db, pOffset);
126223 126414   #endif
126224 126415     return;
126225 126416   }
126226 126417   /* Make sure "isView" and other macros defined above are undefined. Otherwise
126227 126418   ** they may interfere with compilation of other functions in this file
126228 126419   ** (or in another file, if this file becomes part of the amalgamation).  */
126229 126420   #ifdef isView
................................................................................
128929 129120       if( sqlite3CompareAffinity(p, zAff[i])==SQLITE_AFF_BLOB
128930 129121        || sqlite3ExprNeedsNoAffinityChange(p, zAff[i])
128931 129122       ){
128932 129123         zAff[i] = SQLITE_AFF_BLOB;
128933 129124       }
128934 129125     }
128935 129126   }
       129127  +
       129128  +
       129129  +/*
       129130  +** pX is an expression of the form:  (vector) IN (SELECT ...)
       129131  +** In other words, it is a vector IN operator with a SELECT clause on the
       129132  +** LHS.  But not all terms in the vector are indexable and the terms might
       129133  +** not be in the correct order for indexing.
       129134  +**
       129135  +** This routine makes a copy of the input pX expression and then adjusts
       129136  +** the vector on the LHS with corresponding changes to the SELECT so that
       129137  +** the vector contains only index terms and those terms are in the correct
       129138  +** order.  The modified IN expression is returned.  The caller is responsible
       129139  +** for deleting the returned expression.
       129140  +**
       129141  +** Example:
       129142  +**
       129143  +**    CREATE TABLE t1(a,b,c,d,e,f);
       129144  +**    CREATE INDEX t1x1 ON t1(e,c);
       129145  +**    SELECT * FROM t1 WHERE (a,b,c,d,e) IN (SELECT v,w,x,y,z FROM t2)
       129146  +**                           \_______________________________________/
       129147  +**                                     The pX expression
       129148  +**
       129149  +** Since only columns e and c can be used with the index, in that order,
       129150  +** the modified IN expression that is returned will be:
       129151  +**
       129152  +**        (e,c) IN (SELECT z,x FROM t2)
       129153  +**
       129154  +** The reduced pX is different from the original (obviously) and thus is
       129155  +** only used for indexing, to improve performance.  The original unaltered
       129156  +** IN expression must also be run on each output row for correctness.
       129157  +*/
       129158  +static Expr *removeUnindexableInClauseTerms(
       129159  +  Parse *pParse,        /* The parsing context */
       129160  +  int iEq,              /* Look at loop terms starting here */
       129161  +  WhereLoop *pLoop,     /* The current loop */
       129162  +  Expr *pX              /* The IN expression to be reduced */
       129163  +){
       129164  +  sqlite3 *db = pParse->db;
       129165  +  Expr *pNew = sqlite3ExprDup(db, pX, 0);
       129166  +  if( db->mallocFailed==0 ){
       129167  +    ExprList *pOrigRhs = pNew->x.pSelect->pEList;  /* Original unmodified RHS */
       129168  +    ExprList *pOrigLhs = pNew->pLeft->x.pList;     /* Original unmodified LHS */
       129169  +    ExprList *pRhs = 0;         /* New RHS after modifications */
       129170  +    ExprList *pLhs = 0;         /* New LHS after mods */
       129171  +    int i;                      /* Loop counter */
       129172  +    Select *pSelect;            /* Pointer to the SELECT on the RHS */
       129173  +
       129174  +    for(i=iEq; i<pLoop->nLTerm; i++){
       129175  +      if( pLoop->aLTerm[i]->pExpr==pX ){
       129176  +        int iField = pLoop->aLTerm[i]->iField - 1;
       129177  +        assert( pOrigRhs->a[iField].pExpr!=0 );
       129178  +        pRhs = sqlite3ExprListAppend(pParse, pRhs, pOrigRhs->a[iField].pExpr);
       129179  +        pOrigRhs->a[iField].pExpr = 0;
       129180  +        assert( pOrigLhs->a[iField].pExpr!=0 );
       129181  +        pLhs = sqlite3ExprListAppend(pParse, pLhs, pOrigLhs->a[iField].pExpr);
       129182  +        pOrigLhs->a[iField].pExpr = 0;
       129183  +      }
       129184  +    }
       129185  +    sqlite3ExprListDelete(db, pOrigRhs);
       129186  +    sqlite3ExprListDelete(db, pOrigLhs);
       129187  +    pNew->pLeft->x.pList = pLhs;
       129188  +    pNew->x.pSelect->pEList = pRhs;
       129189  +    if( pLhs && pLhs->nExpr==1 ){
       129190  +      /* Take care here not to generate a TK_VECTOR containing only a
       129191  +      ** single value. Since the parser never creates such a vector, some
       129192  +      ** of the subroutines do not handle this case.  */
       129193  +      Expr *p = pLhs->a[0].pExpr;
       129194  +      pLhs->a[0].pExpr = 0;
       129195  +      sqlite3ExprDelete(db, pNew->pLeft);
       129196  +      pNew->pLeft = p;
       129197  +    }
       129198  +    pSelect = pNew->x.pSelect;
       129199  +    if( pSelect->pOrderBy ){
       129200  +      /* If the SELECT statement has an ORDER BY clause, zero the 
       129201  +      ** iOrderByCol variables. These are set to non-zero when an 
       129202  +      ** ORDER BY term exactly matches one of the terms of the 
       129203  +      ** result-set. Since the result-set of the SELECT statement may
       129204  +      ** have been modified or reordered, these variables are no longer 
       129205  +      ** set correctly.  Since setting them is just an optimization, 
       129206  +      ** it's easiest just to zero them here.  */
       129207  +      ExprList *pOrderBy = pSelect->pOrderBy;
       129208  +      for(i=0; i<pOrderBy->nExpr; i++){
       129209  +        pOrderBy->a[i].u.x.iOrderByCol = 0;
       129210  +      }
       129211  +    }
       129212  +
       129213  +#if 0
       129214  +    printf("For indexing, change the IN expr:\n");
       129215  +    sqlite3TreeViewExpr(0, pX, 0);
       129216  +    printf("Into:\n");
       129217  +    sqlite3TreeViewExpr(0, pNew, 0);
       129218  +#endif
       129219  +  }
       129220  +  return pNew;
       129221  +}
       129222  +
128936 129223   
128937 129224   /*
128938 129225   ** Generate code for a single equality term of the WHERE clause.  An equality
128939 129226   ** term can be either X=expr or X IN (...).   pTerm is the term to be 
128940 129227   ** coded.
128941 129228   **
128942 129229   ** The current value for the constraint is left in a register, the index
................................................................................
128992 129279       for(i=0; i<iEq; i++){
128993 129280         if( pLoop->aLTerm[i] && pLoop->aLTerm[i]->pExpr==pX ){
128994 129281           disableTerm(pLevel, pTerm);
128995 129282           return iTarget;
128996 129283         }
128997 129284       }
128998 129285       for(i=iEq;i<pLoop->nLTerm; i++){
128999         -      if( ALWAYS(pLoop->aLTerm[i]) && pLoop->aLTerm[i]->pExpr==pX ) nEq++;
       129286  +      assert( pLoop->aLTerm[i]!=0 );
       129287  +      if( pLoop->aLTerm[i]->pExpr==pX ) nEq++;
129000 129288       }
129001 129289   
129002 129290       if( (pX->flags & EP_xIsSelect)==0 || pX->x.pSelect->pEList->nExpr==1 ){
129003 129291         eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, 0);
129004 129292       }else{
129005         -      Select *pSelect = pX->x.pSelect;
129006 129293         sqlite3 *db = pParse->db;
129007         -      u16 savedDbOptFlags = db->dbOptFlags;
129008         -      ExprList *pOrigRhs = pSelect->pEList;
129009         -      ExprList *pOrigLhs = pX->pLeft->x.pList;
129010         -      ExprList *pRhs = 0;         /* New Select.pEList for RHS */
129011         -      ExprList *pLhs = 0;         /* New pX->pLeft vector */
       129294  +      pX = removeUnindexableInClauseTerms(pParse, iEq, pLoop, pX);
129012 129295   
129013         -      for(i=iEq;i<pLoop->nLTerm; i++){
129014         -        if( pLoop->aLTerm[i]->pExpr==pX ){
129015         -          int iField = pLoop->aLTerm[i]->iField - 1;
129016         -          Expr *pNewRhs = sqlite3ExprDup(db, pOrigRhs->a[iField].pExpr, 0);
129017         -          Expr *pNewLhs = sqlite3ExprDup(db, pOrigLhs->a[iField].pExpr, 0);
129018         -
129019         -          pRhs = sqlite3ExprListAppend(pParse, pRhs, pNewRhs);
129020         -          pLhs = sqlite3ExprListAppend(pParse, pLhs, pNewLhs);
129021         -        }
129022         -      }
129023 129296         if( !db->mallocFailed ){
129024         -        Expr *pLeft = pX->pLeft;
129025         -
129026         -        if( pSelect->pOrderBy ){
129027         -          /* If the SELECT statement has an ORDER BY clause, zero the 
129028         -          ** iOrderByCol variables. These are set to non-zero when an 
129029         -          ** ORDER BY term exactly matches one of the terms of the 
129030         -          ** result-set. Since the result-set of the SELECT statement may
129031         -          ** have been modified or reordered, these variables are no longer 
129032         -          ** set correctly.  Since setting them is just an optimization, 
129033         -          ** it's easiest just to zero them here.  */
129034         -          ExprList *pOrderBy = pSelect->pOrderBy;
129035         -          for(i=0; i<pOrderBy->nExpr; i++){
129036         -            pOrderBy->a[i].u.x.iOrderByCol = 0;
129037         -          }
129038         -        }
129039         -
129040         -        /* Take care here not to generate a TK_VECTOR containing only a
129041         -        ** single value. Since the parser never creates such a vector, some
129042         -        ** of the subroutines do not handle this case.  */
129043         -        if( pLhs->nExpr==1 ){
129044         -          pX->pLeft = pLhs->a[0].pExpr;
129045         -        }else{
129046         -          pLeft->x.pList = pLhs;
129047         -          aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int) * nEq);
129048         -          testcase( aiMap==0 );
129049         -        }
129050         -        pSelect->pEList = pRhs;
129051         -        db->dbOptFlags |= SQLITE_QueryFlattener;
       129297  +        aiMap = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*nEq);
129052 129298           eType = sqlite3FindInIndex(pParse, pX, IN_INDEX_LOOP, 0, aiMap);
129053         -        db->dbOptFlags = savedDbOptFlags;
129054         -        testcase( aiMap!=0 && aiMap[0]!=0 );
129055         -        pSelect->pEList = pOrigRhs;
129056         -        pLeft->x.pList = pOrigLhs;
129057         -        pX->pLeft = pLeft;
       129299  +        pTerm->pExpr->iTable = pX->iTable;
129058 129300         }
129059         -      sqlite3ExprListDelete(pParse->db, pLhs);
129060         -      sqlite3ExprListDelete(pParse->db, pRhs);
       129301  +      sqlite3ExprDelete(db, pX);
       129302  +      pX = pTerm->pExpr;
129061 129303       }
129062 129304   
129063 129305       if( eType==IN_INDEX_INDEX_DESC ){
129064 129306         testcase( bRev );
129065 129307         bRev = !bRev;
129066 129308       }
129067 129309       iTab = pX->iTable;
................................................................................
134701 134943                             ** changes "x IN (?)" into "x=?". */
134702 134944         }
134703 134945       }else if( eOp & (WO_EQ|WO_IS) ){
134704 134946         int iCol = pProbe->aiColumn[saved_nEq];
134705 134947         pNew->wsFlags |= WHERE_COLUMN_EQ;
134706 134948         assert( saved_nEq==pNew->u.btree.nEq );
134707 134949         if( iCol==XN_ROWID 
134708         -       || (iCol>0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
       134950  +       || (iCol>=0 && nInMul==0 && saved_nEq==pProbe->nKeyCol-1)
134709 134951         ){
134710 134952           if( iCol>=0 && pProbe->uniqNotNull==0 ){
134711 134953             pNew->wsFlags |= WHERE_UNQ_WANTED;
134712 134954           }else{
134713 134955             pNew->wsFlags |= WHERE_ONEROW;
134714 134956           }
134715 134957         }
................................................................................
136918 137160       }
136919 137161       sqlite3DebugPrintf("\n");
136920 137162       for(ii=0; ii<pWInfo->nLevel; ii++){
136921 137163         whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
136922 137164       }
136923 137165     }
136924 137166   #endif
136925         -  /* Attempt to omit tables from the join that do not effect the result */
       137167  +
       137168  +  /* Attempt to omit tables from the join that do not affect the result.
       137169  +  ** For a table to not affect the result, the following must be true:
       137170  +  **
       137171  +  **   1) The query must not be an aggregate.
       137172  +  **   2) The table must be the RHS of a LEFT JOIN.
       137173  +  **   3) Either the query must be DISTINCT, or else the ON or USING clause
       137174  +  **      must contain a constraint that limits the scan of the table to 
       137175  +  **      at most a single row.
       137176  +  **   4) The table must not be referenced by any part of the query apart
       137177  +  **      from its own USING or ON clause.
       137178  +  **
       137179  +  ** For example, given:
       137180  +  **
       137181  +  **     CREATE TABLE t1(ipk INTEGER PRIMARY KEY, v1);
       137182  +  **     CREATE TABLE t2(ipk INTEGER PRIMARY KEY, v2);
       137183  +  **     CREATE TABLE t3(ipk INTEGER PRIMARY KEY, v3);
       137184  +  **
       137185  +  ** then table t2 can be omitted from the following:
       137186  +  **
       137187  +  **     SELECT v1, v3 FROM t1 
       137188  +  **       LEFT JOIN t2 USING (t1.ipk=t2.ipk)
       137189  +  **       LEFT JOIN t3 USING (t1.ipk=t3.ipk)
       137190  +  **
       137191  +  ** or from:
       137192  +  **
       137193  +  **     SELECT DISTINCT v1, v3 FROM t1 
       137194  +  **       LEFT JOIN t2
       137195  +  **       LEFT JOIN t3 USING (t1.ipk=t3.ipk)
       137196  +  */
       137197  +  notReady = ~(Bitmask)0;
136926 137198     if( pWInfo->nLevel>=2
136927         -   && pResultSet!=0
       137199  +   && pResultSet!=0               /* guarantees condition (1) above */
136928 137200      && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
136929 137201     ){
       137202  +    int i;
136930 137203       Bitmask tabUsed = sqlite3WhereExprListUsage(pMaskSet, pResultSet);
136931 137204       if( sWLB.pOrderBy ){
136932 137205         tabUsed |= sqlite3WhereExprListUsage(pMaskSet, sWLB.pOrderBy);
136933 137206       }
136934         -    while( pWInfo->nLevel>=2 ){
       137207  +    for(i=pWInfo->nLevel-1; i>=1; i--){
136935 137208         WhereTerm *pTerm, *pEnd;
136936         -      pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
136937         -      if( (pWInfo->pTabList->a[pLoop->iTab].fg.jointype & JT_LEFT)==0 ) break;
       137209  +      struct SrcList_item *pItem;
       137210  +      pLoop = pWInfo->a[i].pWLoop;
       137211  +      pItem = &pWInfo->pTabList->a[pLoop->iTab];
       137212  +      if( (pItem->fg.jointype & JT_LEFT)==0 ) continue;
136938 137213         if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
136939 137214          && (pLoop->wsFlags & WHERE_ONEROW)==0
136940 137215         ){
136941         -        break;
       137216  +        continue;
136942 137217         }
136943         -      if( (tabUsed & pLoop->maskSelf)!=0 ) break;
       137218  +      if( (tabUsed & pLoop->maskSelf)!=0 ) continue;
136944 137219         pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
136945 137220         for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
136946         -        if( (pTerm->prereqAll & pLoop->maskSelf)!=0
136947         -         && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
136948         -        ){
136949         -          break;
       137221  +        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
       137222  +          if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
       137223  +           || pTerm->pExpr->iRightJoinTable!=pItem->iCursor
       137224  +          ){
       137225  +            break;
       137226  +          }
136950 137227           }
136951 137228         }
136952         -      if( pTerm<pEnd ) break;
       137229  +      if( pTerm<pEnd ) continue;
136953 137230         WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
       137231  +      notReady &= ~pLoop->maskSelf;
       137232  +      for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
       137233  +        if( (pTerm->prereqAll & pLoop->maskSelf)!=0 ){
       137234  +          pTerm->wtFlags |= TERM_CODED;
       137235  +        }
       137236  +      }
       137237  +      if( i!=pWInfo->nLevel-1 ){
       137238  +        int nByte = (pWInfo->nLevel-1-i) * sizeof(WhereLevel);
       137239  +        memmove(&pWInfo->a[i], &pWInfo->a[i+1], nByte);
       137240  +      }
136954 137241         pWInfo->nLevel--;
136955 137242         nTabList--;
136956 137243       }
136957 137244     }
136958 137245     WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
136959 137246     pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
136960 137247   
................................................................................
137101 137388     pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
137102 137389     if( db->mallocFailed ) goto whereBeginError;
137103 137390   
137104 137391     /* Generate the code to do the search.  Each iteration of the for
137105 137392     ** loop below generates code for a single nested loop of the VM
137106 137393     ** program.
137107 137394     */
137108         -  notReady = ~(Bitmask)0;
137109 137395     for(ii=0; ii<nTabList; ii++){
137110 137396       int addrExplain;
137111 137397       int wsFlags;
137112 137398       pLevel = &pWInfo->a[ii];
137113 137399       wsFlags = pLevel->pWLoop->wsFlags;
137114 137400   #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
137115 137401       if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
................................................................................
137165 137451       pLoop = pLevel->pWLoop;
137166 137452       if( pLevel->op!=OP_Noop ){
137167 137453   #ifndef SQLITE_DISABLE_SKIPAHEAD_DISTINCT
137168 137454         int addrSeek = 0;
137169 137455         Index *pIdx;
137170 137456         int n;
137171 137457         if( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
       137458  +       && i==pWInfo->nLevel-1  /* Ticket [ef9318757b152e3] 2017-10-21 */
137172 137459          && (pLoop->wsFlags & WHERE_INDEXED)!=0
137173 137460          && (pIdx = pLoop->u.btree.pIndex)->hasStat1
137174 137461          && (n = pLoop->u.btree.nIdxCol)>0
137175 137462          && pIdx->aiRowLogEst[n]>=36
137176 137463         ){
137177 137464           int r1 = pParse->nMem+1;
137178 137465           int j, op;
................................................................................
137231 137518       }
137232 137519   #endif
137233 137520       if( pLevel->iLeftJoin ){
137234 137521         int ws = pLoop->wsFlags;
137235 137522         addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
137236 137523         assert( (ws & WHERE_IDX_ONLY)==0 || (ws & WHERE_INDEXED)!=0 );
137237 137524         if( (ws & WHERE_IDX_ONLY)==0 ){
137238         -        sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
       137525  +        assert( pLevel->iTabCur==pTabList->a[pLevel->iFrom].iCursor );
       137526  +        sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iTabCur);
137239 137527         }
137240 137528         if( (ws & WHERE_INDEXED) 
137241 137529          || ((ws & WHERE_MULTI_OR) && pLevel->u.pCovidx) 
137242 137530         ){
137243 137531           sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
137244 137532         }
137245 137533         if( pLevel->op==OP_Return ){
................................................................................
137399 137687   
137400 137688   /*
137401 137689   ** Alternative datatype for the argument to the malloc() routine passed
137402 137690   ** into sqlite3ParserAlloc().  The default is size_t.
137403 137691   */
137404 137692   #define YYMALLOCARGTYPE  u64
137405 137693   
137406         -/*
137407         -** An instance of this structure holds information about the
137408         -** LIMIT clause of a SELECT statement.
137409         -*/
137410         -struct LimitVal {
137411         -  Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
137412         -  Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
137413         -};
137414         -
137415 137694   /*
137416 137695   ** An instance of the following structure describes the event of a
137417 137696   ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
137418 137697   ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
137419 137698   **
137420 137699   **      UPDATE ON (a,b,c)
137421 137700   **
................................................................................
137645 137924     SrcList* yy185;
137646 137925     ExprSpan yy190;
137647 137926     int yy194;
137648 137927     Select* yy243;
137649 137928     IdList* yy254;
137650 137929     With* yy285;
137651 137930     struct TrigEvent yy332;
137652         -  struct LimitVal yy354;
137653 137931     struct {int value; int mask;} yy497;
137654 137932   } YYMINORTYPE;
137655 137933   #ifndef YYSTACKDEPTH
137656 137934   #define YYSTACKDEPTH 100
137657 137935   #endif
137658 137936   #define sqlite3ParserARG_SDECL Parse *pParse;
137659 137937   #define sqlite3ParserARG_PDECL ,Parse *pParse
................................................................................
139901 140179     Select *pLhs = yymsp[-2].minor.yy243;
139902 140180     if( pRhs && pRhs->pPrior ){
139903 140181       SrcList *pFrom;
139904 140182       Token x;
139905 140183       x.n = 0;
139906 140184       parserDoubleLinkSelect(pParse, pRhs);
139907 140185       pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
139908         -    pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
       140186  +    pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
139909 140187     }
139910 140188     if( pRhs ){
139911 140189       pRhs->op = (u8)yymsp[-1].minor.yy194;
139912 140190       pRhs->pPrior = pLhs;
139913 140191       if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
139914 140192       pRhs->selFlags &= ~SF_MultiValue;
139915 140193       if( yymsp[-1].minor.yy194!=TK_ALL ) pParse->hasCompound = 1;
................................................................................
139927 140205   {yymsp[-1].minor.yy194 = TK_ALL;}
139928 140206           break;
139929 140207         case 84: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
139930 140208   {
139931 140209   #if SELECTTRACE_ENABLED
139932 140210     Token s = yymsp[-8].minor.yy0; /*A-overwrites-S*/
139933 140211   #endif
139934         -  yymsp[-8].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy148,yymsp[-5].minor.yy185,yymsp[-4].minor.yy72,yymsp[-3].minor.yy148,yymsp[-2].minor.yy72,yymsp[-1].minor.yy148,yymsp[-7].minor.yy194,yymsp[0].minor.yy354.pLimit,yymsp[0].minor.yy354.pOffset);
       140212  +  yymsp[-8].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy148,yymsp[-5].minor.yy185,yymsp[-4].minor.yy72,yymsp[-3].minor.yy148,yymsp[-2].minor.yy72,yymsp[-1].minor.yy148,yymsp[-7].minor.yy194,yymsp[0].minor.yy72);
139935 140213   #if SELECTTRACE_ENABLED
139936 140214     /* Populate the Select.zSelName[] string that is used to help with
139937 140215     ** query planner debugging, to differentiate between multiple Select
139938 140216     ** objects in a complex query.
139939 140217     **
139940 140218     ** If the SELECT keyword is immediately followed by a C-style comment
139941 140219     ** then extract the first few alphanumeric characters from within that
................................................................................
139956 140234       }
139957 140235     }
139958 140236   #endif /* SELECTRACE_ENABLED */
139959 140237   }
139960 140238           break;
139961 140239         case 85: /* values ::= VALUES LP nexprlist RP */
139962 140240   {
139963         -  yymsp[-3].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,0,SF_Values,0,0);
       140241  +  yymsp[-3].minor.yy243 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,0,SF_Values,0);
139964 140242   }
139965 140243           break;
139966 140244         case 86: /* values ::= values COMMA LP exprlist RP */
139967 140245   {
139968 140246     Select *pRight, *pLeft = yymsp[-4].minor.yy243;
139969         -  pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,0,SF_Values|SF_MultiValue,0,0);
       140247  +  pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy148,0,0,0,0,0,SF_Values|SF_MultiValue,0);
139970 140248     if( ALWAYS(pLeft) ) pLeft->selFlags &= ~SF_MultiValue;
139971 140249     if( pRight ){
139972 140250       pRight->op = TK_ALL;
139973 140251       pRight->pPrior = pLeft;
139974 140252       yymsp[-4].minor.yy243 = pRight;
139975 140253     }else{
139976 140254       yymsp[-4].minor.yy243 = pLeft;
................................................................................
140067 140345           pOld->zName = pOld->zDatabase = 0;
140068 140346           pOld->pSelect = 0;
140069 140347         }
140070 140348         sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy185);
140071 140349       }else{
140072 140350         Select *pSubquery;
140073 140351         sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy185);
140074         -      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy185,0,0,0,0,SF_NestedFrom,0,0);
       140352  +      pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy185,0,0,0,0,SF_NestedFrom,0);
140075 140353         yymsp[-6].minor.yy185 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy185,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy72,yymsp[0].minor.yy254);
140076 140354       }
140077 140355     }
140078 140356           break;
140079 140357         case 104: /* dbnm ::= */
140080 140358         case 113: /* indexed_opt ::= */ yytestcase(yyruleno==113);
140081 140359   {yymsp[1].minor.yy0.z=0; yymsp[1].minor.yy0.n=0;}
................................................................................
140099 140377         case 128: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==128);
140100 140378         case 135: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==135);
140101 140379         case 196: /* case_else ::= ELSE expr */ yytestcase(yyruleno==196);
140102 140380   {yymsp[-1].minor.yy72 = yymsp[0].minor.yy190.pExpr;}
140103 140381           break;
140104 140382         case 112: /* on_opt ::= */
140105 140383         case 127: /* having_opt ::= */ yytestcase(yyruleno==127);
       140384  +      case 129: /* limit_opt ::= */ yytestcase(yyruleno==129);
140106 140385         case 134: /* where_opt ::= */ yytestcase(yyruleno==134);
140107 140386         case 197: /* case_else ::= */ yytestcase(yyruleno==197);
140108 140387         case 199: /* case_operand ::= */ yytestcase(yyruleno==199);
140109 140388   {yymsp[1].minor.yy72 = 0;}
140110 140389           break;
140111 140390         case 114: /* indexed_opt ::= INDEXED BY nm */
140112 140391   {yymsp[-2].minor.yy0 = yymsp[0].minor.yy0;}
................................................................................
140142 140421           break;
140143 140422         case 123: /* sortorder ::= DESC */
140144 140423   {yymsp[0].minor.yy194 = SQLITE_SO_DESC;}
140145 140424           break;
140146 140425         case 124: /* sortorder ::= */
140147 140426   {yymsp[1].minor.yy194 = SQLITE_SO_UNDEFINED;}
140148 140427           break;
140149         -      case 129: /* limit_opt ::= */
140150         -{yymsp[1].minor.yy354.pLimit = 0; yymsp[1].minor.yy354.pOffset = 0;}
140151         -        break;
140152 140428         case 130: /* limit_opt ::= LIMIT expr */
140153         -{yymsp[-1].minor.yy354.pLimit = yymsp[0].minor.yy190.pExpr; yymsp[-1].minor.yy354.pOffset = 0;}
       140429  +{yymsp[-1].minor.yy72 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy190.pExpr,0);}
140154 140430           break;
140155 140431         case 131: /* limit_opt ::= LIMIT expr OFFSET expr */
140156         -{yymsp[-3].minor.yy354.pLimit = yymsp[-2].minor.yy190.pExpr; yymsp[-3].minor.yy354.pOffset = yymsp[0].minor.yy190.pExpr;}
       140432  +{yymsp[-3].minor.yy72 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[-2].minor.yy190.pExpr,yymsp[0].minor.yy190.pExpr);}
140157 140433           break;
140158 140434         case 132: /* limit_opt ::= LIMIT expr COMMA expr */
140159         -{yymsp[-3].minor.yy354.pOffset = yymsp[-2].minor.yy190.pExpr; yymsp[-3].minor.yy354.pLimit = yymsp[0].minor.yy190.pExpr;}
       140435  +{yymsp[-3].minor.yy72 = sqlite3PExpr(pParse,TK_LIMIT,yymsp[0].minor.yy190.pExpr,yymsp[-2].minor.yy190.pExpr);}
140160 140436           break;
140161 140437         case 133: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */
140162 140438   {
140163 140439     sqlite3WithPush(pParse, yymsp[-5].minor.yy285, 1);
140164 140440     sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy185, &yymsp[-1].minor.yy0);
140165         -  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy185,yymsp[0].minor.yy72,0,0,0);
       140441  +  sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy185,yymsp[0].minor.yy72,0,0);
140166 140442   }
140167 140443           break;
140168 140444         case 136: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */
140169 140445   {
140170 140446     sqlite3WithPush(pParse, yymsp[-7].minor.yy285, 1);
140171 140447     sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy185, &yymsp[-3].minor.yy0);
140172 140448     sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy148,"set list"); 
140173         -  sqlite3Update(pParse,yymsp[-4].minor.yy185,yymsp[-1].minor.yy148,yymsp[0].minor.yy72,yymsp[-5].minor.yy194,0,0,0);
       140449  +  sqlite3Update(pParse,yymsp[-4].minor.yy185,yymsp[-1].minor.yy148,yymsp[0].minor.yy72,yymsp[-5].minor.yy194,0,0);
140174 140450   }
140175 140451           break;
140176 140452         case 137: /* setlist ::= setlist COMMA nm EQ expr */
140177 140453   {
140178 140454     yymsp[-4].minor.yy148 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy148, yymsp[0].minor.yy190.pExpr);
140179 140455     sqlite3ExprListSetName(pParse, yymsp[-4].minor.yy148, &yymsp[-2].minor.yy0, 1);
140180 140456   }
................................................................................
140483 140759       exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
140484 140760       yymsp[-4].minor.yy190.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
140485 140761     }
140486 140762           break;
140487 140763         case 191: /* expr ::= expr in_op nm dbnm paren_exprlist */
140488 140764   {
140489 140765       SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0);
140490         -    Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
       140766  +    Select *pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0);
140491 140767       if( yymsp[0].minor.yy148 )  sqlite3SrcListFuncArgs(pParse, pSelect ? pSrc : 0, yymsp[0].minor.yy148);
140492 140768       yymsp[-4].minor.yy190.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy190.pExpr, 0);
140493 140769       sqlite3PExprAddSelect(pParse, yymsp[-4].minor.yy190.pExpr, pSelect);
140494 140770       exprNot(pParse, yymsp[-3].minor.yy194, &yymsp[-4].minor.yy190);
140495 140771       yymsp[-4].minor.yy190.zEnd = yymsp[-1].minor.yy0.z ? &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n] : &yymsp[-2].minor.yy0.z[yymsp[-2].minor.yy0.n];
140496 140772     }
140497 140773           break;
................................................................................
145230 145506     }else if( flags & SQLITE_OPEN_NOMUTEX ){
145231 145507       isThreadsafe = 0;
145232 145508     }else if( flags & SQLITE_OPEN_FULLMUTEX ){
145233 145509       isThreadsafe = 1;
145234 145510     }else{
145235 145511       isThreadsafe = sqlite3GlobalConfig.bFullMutex;
145236 145512     }
       145513  +
145237 145514     if( flags & SQLITE_OPEN_PRIVATECACHE ){
145238 145515       flags &= ~SQLITE_OPEN_SHAREDCACHE;
145239 145516     }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
145240 145517       flags |= SQLITE_OPEN_SHAREDCACHE;
145241 145518     }
145242 145519   
145243 145520     /* Remove harmful bits from the flags parameter
................................................................................
145262 145539                  SQLITE_OPEN_FULLMUTEX |
145263 145540                  SQLITE_OPEN_WAL
145264 145541                );
145265 145542   
145266 145543     /* Allocate the sqlite data structure */
145267 145544     db = sqlite3MallocZero( sizeof(sqlite3) );
145268 145545     if( db==0 ) goto opendb_out;
145269         -  if( isThreadsafe ){
       145546  +  if( isThreadsafe 
       145547  +#ifdef SQLITE_ENABLE_MULTITHREADED_CHECKS
       145548  +   || sqlite3GlobalConfig.bCoreMutex
       145549  +#endif
       145550  +  ){
145270 145551       db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
145271 145552       if( db->mutex==0 ){
145272 145553         sqlite3_free(db);
145273 145554         db = 0;
145274 145555         goto opendb_out;
145275 145556       }
       145557  +    if( isThreadsafe==0 ){
       145558  +      sqlite3MutexWarnOnContention(db->mutex);
       145559  +    }
145276 145560     }
145277 145561     sqlite3_mutex_enter(db->mutex);
145278 145562     db->errMask = 0xff;
145279 145563     db->nDb = 2;
145280 145564     db->magic = SQLITE_MAGIC_BUSY;
145281 145565     db->aDb = db->aDbStatic;
145282 145566   
................................................................................
145752 146036   **
145753 146037   **   1.  Serve as a convenient place to set a breakpoint in a debugger
145754 146038   **       to detect when version error conditions occurs.
145755 146039   **
145756 146040   **   2.  Invoke sqlite3_log() to provide the source code location where
145757 146041   **       a low-level error is first detected.
145758 146042   */
145759         -static int reportError(int iErr, int lineno, const char *zType){
       146043  +SQLITE_PRIVATE int sqlite3ReportError(int iErr, int lineno, const char *zType){
145760 146044     sqlite3_log(iErr, "%s at line %d of [%.10s]",
145761 146045                 zType, lineno, 20+sqlite3_sourceid());
145762 146046     return iErr;
145763 146047   }
145764 146048   SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
145765 146049     testcase( sqlite3GlobalConfig.xLog!=0 );
145766         -  return reportError(SQLITE_CORRUPT, lineno, "database corruption");
       146050  +  return sqlite3ReportError(SQLITE_CORRUPT, lineno, "database corruption");
145767 146051   }
145768 146052   SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
145769 146053     testcase( sqlite3GlobalConfig.xLog!=0 );
145770         -  return reportError(SQLITE_MISUSE, lineno, "misuse");
       146054  +  return sqlite3ReportError(SQLITE_MISUSE, lineno, "misuse");
145771 146055   }
145772 146056   SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
145773 146057     testcase( sqlite3GlobalConfig.xLog!=0 );
145774         -  return reportError(SQLITE_CANTOPEN, lineno, "cannot open file");
       146058  +  return sqlite3ReportError(SQLITE_CANTOPEN, lineno, "cannot open file");
145775 146059   }
145776 146060   #ifdef SQLITE_DEBUG
145777 146061   SQLITE_PRIVATE int sqlite3CorruptPgnoError(int lineno, Pgno pgno){
145778 146062     char zMsg[100];
145779 146063     sqlite3_snprintf(sizeof(zMsg), zMsg, "database corruption page %d", pgno);
145780 146064     testcase( sqlite3GlobalConfig.xLog!=0 );
145781         -  return reportError(SQLITE_CORRUPT, lineno, zMsg);
       146065  +  return sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg);
145782 146066   }
145783 146067   SQLITE_PRIVATE int sqlite3NomemError(int lineno){
145784 146068     testcase( sqlite3GlobalConfig.xLog!=0 );
145785         -  return reportError(SQLITE_NOMEM, lineno, "OOM");
       146069  +  return sqlite3ReportError(SQLITE_NOMEM, lineno, "OOM");
145786 146070   }
145787 146071   SQLITE_PRIVATE int sqlite3IoerrnomemError(int lineno){
145788 146072     testcase( sqlite3GlobalConfig.xLog!=0 );
145789         -  return reportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
       146073  +  return sqlite3ReportError(SQLITE_IOERR_NOMEM, lineno, "I/O OOM error");
145790 146074   }
145791 146075   #endif
145792 146076   
145793 146077   #ifndef SQLITE_OMIT_DEPRECATED
145794 146078   /*
145795 146079   ** This is a convenience routine that makes sure that all thread-specific
145796 146080   ** data for this thread has been deallocated.
................................................................................
186120 186404   static Fts5ExprPhrase *sqlite3Fts5ParseTerm(
186121 186405     Fts5Parse *pParse, 
186122 186406     Fts5ExprPhrase *pPhrase, 
186123 186407     Fts5Token *pToken,
186124 186408     int bPrefix
186125 186409   );
186126 186410   
       186411  +static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase*);
       186412  +
186127 186413   static Fts5ExprNearset *sqlite3Fts5ParseNearset(
186128 186414     Fts5Parse*, 
186129 186415     Fts5ExprNearset*,
186130 186416     Fts5ExprPhrase* 
186131 186417   );
186132 186418   
186133 186419   static Fts5Colset *sqlite3Fts5ParseColset(
................................................................................
186200 186486   #define FTS5_COLON                            5
186201 186487   #define FTS5_MINUS                            6
186202 186488   #define FTS5_LCP                              7
186203 186489   #define FTS5_RCP                              8
186204 186490   #define FTS5_STRING                           9
186205 186491   #define FTS5_LP                              10
186206 186492   #define FTS5_RP                              11
186207         -#define FTS5_COMMA                           12
186208         -#define FTS5_PLUS                            13
186209         -#define FTS5_STAR                            14
       186493  +#define FTS5_CARET                           12
       186494  +#define FTS5_COMMA                           13
       186495  +#define FTS5_PLUS                            14
       186496  +#define FTS5_STAR                            15
186210 186497   
186211 186498   /*
186212 186499   ** 2000-05-29
186213 186500   **
186214 186501   ** The author disclaims copyright to this source code.  In place of
186215 186502   ** a legal notice, here is a blessing:
186216 186503   **
................................................................................
186317 186604   **    fts5YY_NO_ACTION       The fts5yy_action[] code for no-op
186318 186605   */
186319 186606   #ifndef INTERFACE
186320 186607   # define INTERFACE 1
186321 186608   #endif
186322 186609   /************* Begin control #defines *****************************************/
186323 186610   #define fts5YYCODETYPE unsigned char
186324         -#define fts5YYNOCODE 28
       186611  +#define fts5YYNOCODE 29
186325 186612   #define fts5YYACTIONTYPE unsigned char
186326 186613   #define sqlite3Fts5ParserFTS5TOKENTYPE Fts5Token
186327 186614   typedef union {
186328 186615     int fts5yyinit;
186329 186616     sqlite3Fts5ParserFTS5TOKENTYPE fts5yy0;
186330 186617     int fts5yy4;
186331         -  Fts5Colset* fts5yy11;
186332         -  Fts5ExprNode* fts5yy24;
186333         -  Fts5ExprNearset* fts5yy46;
186334         -  Fts5ExprPhrase* fts5yy53;
       186618  +  Fts5ExprPhrase* fts5yy11;
       186619  +  Fts5ExprNearset* fts5yy14;
       186620  +  Fts5Colset* fts5yy43;
       186621  +  Fts5ExprNode* fts5yy54;
186335 186622   } fts5YYMINORTYPE;
186336 186623   #ifndef fts5YYSTACKDEPTH
186337 186624   #define fts5YYSTACKDEPTH 100
186338 186625   #endif
186339 186626   #define sqlite3Fts5ParserARG_SDECL Fts5Parse *pParse;
186340 186627   #define sqlite3Fts5ParserARG_PDECL ,Fts5Parse *pParse
186341 186628   #define sqlite3Fts5ParserARG_FETCH Fts5Parse *pParse = fts5yypParser->pParse
186342 186629   #define sqlite3Fts5ParserARG_STORE fts5yypParser->pParse = pParse
186343         -#define fts5YYNSTATE             33
186344         -#define fts5YYNRULE              27
186345         -#define fts5YY_MAX_SHIFT         32
186346         -#define fts5YY_MIN_SHIFTREDUCE   50
186347         -#define fts5YY_MAX_SHIFTREDUCE   76
186348         -#define fts5YY_MIN_REDUCE        77
186349         -#define fts5YY_MAX_REDUCE        103
186350         -#define fts5YY_ERROR_ACTION      104
186351         -#define fts5YY_ACCEPT_ACTION     105
186352         -#define fts5YY_NO_ACTION         106
       186630  +#define fts5YYNSTATE             35
       186631  +#define fts5YYNRULE              28
       186632  +#define fts5YY_MAX_SHIFT         34
       186633  +#define fts5YY_MIN_SHIFTREDUCE   52
       186634  +#define fts5YY_MAX_SHIFTREDUCE   79
       186635  +#define fts5YY_MIN_REDUCE        80
       186636  +#define fts5YY_MAX_REDUCE        107
       186637  +#define fts5YY_ERROR_ACTION      108
       186638  +#define fts5YY_ACCEPT_ACTION     109
       186639  +#define fts5YY_NO_ACTION         110
186353 186640   /************* End control #defines *******************************************/
186354 186641   
186355 186642   /* Define the fts5yytestcase() macro to be a no-op if is not already defined
186356 186643   ** otherwise.
186357 186644   **
186358 186645   ** Applications can choose to define fts5yytestcase() in the %include section
186359 186646   ** to a macro that can assist in verifying code coverage.  For production
................................................................................
186417 186704   **  fts5yy_shift_ofst[]    For each state, the offset into fts5yy_action for
186418 186705   **                     shifting terminals.
186419 186706   **  fts5yy_reduce_ofst[]   For each state, the offset into fts5yy_action for
186420 186707   **                     shifting non-terminals after a reduce.
186421 186708   **  fts5yy_default[]       Default action for each state.
186422 186709   **
186423 186710   *********** Begin parsing tables **********************************************/
186424         -#define fts5YY_ACTTAB_COUNT (98)
       186711  +#define fts5YY_ACTTAB_COUNT (105)
186425 186712   static const fts5YYACTIONTYPE fts5yy_action[] = {
186426         - /*     0 */   105,   19,   90,    6,   26,   93,   92,   24,   24,   17,
186427         - /*    10 */    90,    6,   26,   16,   92,   54,   24,   18,   90,    6,
186428         - /*    20 */    26,   10,   92,   12,   24,   75,   86,   90,    6,   26,
186429         - /*    30 */    13,   92,   75,   24,   20,   90,    6,   26,  101,   92,
186430         - /*    40 */    56,   24,   27,   90,    6,   26,  100,   92,   21,   24,
186431         - /*    50 */    23,   15,   30,   11,    1,   91,   22,   25,    9,   92,
186432         - /*    60 */     7,   24,    3,    4,    5,    3,    4,    5,    3,   77,
186433         - /*    70 */     4,    5,    3,   61,   23,   15,   60,   11,   80,   12,
186434         - /*    80 */     2,   13,   68,   10,   29,   52,   55,   75,   31,   32,
186435         - /*    90 */     8,   28,    5,    3,   51,   55,   72,   14,
       186713  + /*     0 */   109,   20,   93,    6,   28,   96,   95,   26,   26,   18,
       186714  + /*    10 */    93,    6,   28,   17,   95,   56,   26,   19,   93,    6,
       186715  + /*    20 */    28,   14,   95,  105,   26,   89,   93,    6,   28,   25,
       186716  + /*    30 */    95,   78,   26,   21,   93,    6,   28,  104,   95,   58,
       186717  + /*    40 */    26,   29,   93,    6,   28,   32,   95,   22,   26,   24,
       186718  + /*    50 */    16,   23,   11,    1,   14,   13,   24,   16,   31,   11,
       186719  + /*    60 */     3,   94,   13,   27,    8,   95,   82,   26,    7,    4,
       186720  + /*    70 */     5,    3,    4,    5,    3,   80,    4,    5,    3,   63,
       186721  + /*    80 */    33,   34,   62,   12,    2,   83,   13,   10,   12,   71,
       186722  + /*    90 */    10,   13,   78,    5,    3,   78,    9,   30,   75,   82,
       186723  + /*   100 */    54,   57,   53,   57,   15,
186436 186724   };
186437 186725   static const fts5YYCODETYPE fts5yy_lookahead[] = {
186438         - /*     0 */    16,   17,   18,   19,   20,   22,   22,   24,   24,   17,
186439         - /*    10 */    18,   19,   20,    7,   22,    9,   24,   17,   18,   19,
186440         - /*    20 */    20,   10,   22,    9,   24,   14,   17,   18,   19,   20,
186441         - /*    30 */     9,   22,   14,   24,   17,   18,   19,   20,   26,   22,
186442         - /*    40 */     9,   24,   17,   18,   19,   20,   26,   22,   21,   24,
186443         - /*    50 */     6,    7,   13,    9,   10,   18,   21,   20,    5,   22,
186444         - /*    60 */     5,   24,    3,    1,    2,    3,    1,    2,    3,    0,
186445         - /*    70 */     1,    2,    3,   11,    6,    7,   11,    9,    5,    9,
186446         - /*    80 */    10,    9,   11,   10,   12,    8,    9,   14,   24,   25,
186447         - /*    90 */    23,   24,    2,    3,    8,    9,    9,    9,
       186726  + /*     0 */    17,   18,   19,   20,   21,   23,   23,   25,   25,   18,
       186727  + /*    10 */    19,   20,   21,    7,   23,    9,   25,   18,   19,   20,
       186728  + /*    20 */    21,    9,   23,   27,   25,   18,   19,   20,   21,   25,
       186729  + /*    30 */    23,   15,   25,   18,   19,   20,   21,   27,   23,    9,
       186730  + /*    40 */    25,   18,   19,   20,   21,   14,   23,   22,   25,    6,
       186731  + /*    50 */     7,   22,    9,   10,    9,   12,    6,    7,   13,    9,
       186732  + /*    60 */     3,   19,   12,   21,    5,   23,   28,   25,    5,    1,
       186733  + /*    70 */     2,    3,    1,    2,    3,    0,    1,    2,    3,   11,
       186734  + /*    80 */    25,   26,   11,    9,   10,    5,   12,   10,    9,   11,
       186735  + /*    90 */    10,   12,   15,    2,    3,   15,   24,   25,    9,   28,
       186736  + /*   100 */     8,    9,    8,    9,    9,
186448 186737   };
186449         -#define fts5YY_SHIFT_USE_DFLT (98)
186450         -#define fts5YY_SHIFT_COUNT    (32)
       186738  +#define fts5YY_SHIFT_USE_DFLT (105)
       186739  +#define fts5YY_SHIFT_COUNT    (34)
186451 186740   #define fts5YY_SHIFT_MIN      (0)
186452         -#define fts5YY_SHIFT_MAX      (90)
       186741  +#define fts5YY_SHIFT_MAX      (95)
186453 186742   static const unsigned char fts5yy_shift_ofst[] = {
186454         - /*     0 */    44,   44,   44,   44,   44,   44,   68,   70,   72,   14,
186455         - /*    10 */    21,   73,   11,   18,   18,   31,   31,   62,   65,   69,
186456         - /*    20 */    90,   77,   86,    6,   39,   53,   55,   59,   39,   87,
186457         - /*    30 */    88,   39,   71,
       186743  + /*     0 */    43,   43,   43,   43,   43,   43,   50,   74,   79,   45,
       186744  + /*    10 */    12,   80,   77,   12,   16,   16,   30,   30,   68,   71,
       186745  + /*    20 */    75,   91,   92,   94,    6,   31,   31,   59,   63,   57,
       186746  + /*    30 */    31,   89,   95,   31,   78,
186458 186747   };
186459         -#define fts5YY_REDUCE_USE_DFLT (-18)
186460         -#define fts5YY_REDUCE_COUNT (16)
186461         -#define fts5YY_REDUCE_MIN   (-17)
186462         -#define fts5YY_REDUCE_MAX   (67)
       186748  +#define fts5YY_REDUCE_USE_DFLT (-19)
       186749  +#define fts5YY_REDUCE_COUNT (17)
       186750  +#define fts5YY_REDUCE_MIN   (-18)
       186751  +#define fts5YY_REDUCE_MAX   (72)
186463 186752   static const signed char fts5yy_reduce_ofst[] = {
186464         - /*     0 */   -16,   -8,    0,    9,   17,   25,   37,  -17,   64,  -17,
186465         - /*    10 */    67,   12,   12,   12,   20,   27,   35,
       186753  + /*     0 */   -17,   -9,   -1,    7,   15,   23,   42,  -18,  -18,   55,
       186754  + /*    10 */    72,   -4,   -4,    4,   -4,   10,   25,   29,
186466 186755   };
186467 186756   static const fts5YYACTIONTYPE fts5yy_default[] = {
186468         - /*     0 */   104,  104,  104,  104,  104,  104,   89,  104,   98,  104,
186469         - /*    10 */   104,  103,  103,  103,  103,  104,  104,  104,  104,  104,
186470         - /*    20 */    85,  104,  104,  104,   94,  104,  104,   84,   96,  104,
186471         - /*    30 */   104,   97,  104,
       186757  + /*     0 */   108,  108,  108,  108,  108,  108,   92,  108,  108,  102,
       186758  + /*    10 */   108,  107,  107,  108,  107,  107,  108,  108,  108,  108,
       186759  + /*    20 */   108,   88,  108,  108,  108,   98,   97,  108,  108,   87,
       186760  + /*    30 */   100,  108,  108,  101,  108,
186472 186761   };
186473 186762   /********** End of lemon-generated parsing tables *****************************/
186474 186763   
186475 186764   /* The next table maps tokens (terminal symbols) into fallback tokens.  
186476 186765   ** If a construct like the following:
186477 186766   ** 
186478 186767   **      %fallback ID X Y Z.
................................................................................
186573 186862   #ifndef NDEBUG
186574 186863   /* For tracing shifts, the names of all terminals and nonterminals
186575 186864   ** are required.  The following table supplies these names */
186576 186865   static const char *const fts5yyTokenName[] = { 
186577 186866     "$",             "OR",            "AND",           "NOT",         
186578 186867     "TERM",          "COLON",         "MINUS",         "LCP",         
186579 186868     "RCP",           "STRING",        "LP",            "RP",          
186580         -  "COMMA",         "PLUS",          "STAR",          "error",       
186581         -  "input",         "expr",          "cnearset",      "exprlist",    
186582         -  "colset",        "colsetlist",    "nearset",       "nearphrases", 
186583         -  "phrase",        "neardist_opt",  "star_opt",    
       186869  +  "CARET",         "COMMA",         "PLUS",          "STAR",        
       186870  +  "error",         "input",         "expr",          "cnearset",    
       186871  +  "exprlist",      "colset",        "colsetlist",    "nearset",     
       186872  +  "nearphrases",   "phrase",        "neardist_opt",  "star_opt",    
186584 186873   };
186585 186874   #endif /* NDEBUG */
186586 186875   
186587 186876   #ifndef NDEBUG
186588 186877   /* For tracing reduce actions, the names of all rules are required.
186589 186878   */
186590 186879   static const char *const fts5yyRuleName[] = {
................................................................................
186602 186891    /*  11 */ "expr ::= LP expr RP",
186603 186892    /*  12 */ "expr ::= exprlist",
186604 186893    /*  13 */ "exprlist ::= cnearset",
186605 186894    /*  14 */ "exprlist ::= exprlist cnearset",
186606 186895    /*  15 */ "cnearset ::= nearset",
186607 186896    /*  16 */ "cnearset ::= colset COLON nearset",
186608 186897    /*  17 */ "nearset ::= phrase",
186609         - /*  18 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
186610         - /*  19 */ "nearphrases ::= phrase",
186611         - /*  20 */ "nearphrases ::= nearphrases phrase",
186612         - /*  21 */ "neardist_opt ::=",
186613         - /*  22 */ "neardist_opt ::= COMMA STRING",
186614         - /*  23 */ "phrase ::= phrase PLUS STRING star_opt",
186615         - /*  24 */ "phrase ::= STRING star_opt",
186616         - /*  25 */ "star_opt ::= STAR",
186617         - /*  26 */ "star_opt ::=",
       186898  + /*  18 */ "nearset ::= CARET phrase",
       186899  + /*  19 */ "nearset ::= STRING LP nearphrases neardist_opt RP",
       186900  + /*  20 */ "nearphrases ::= phrase",
       186901  + /*  21 */ "nearphrases ::= nearphrases phrase",
       186902  + /*  22 */ "neardist_opt ::=",
       186903  + /*  23 */ "neardist_opt ::= COMMA STRING",
       186904  + /*  24 */ "phrase ::= phrase PLUS STRING star_opt",
       186905  + /*  25 */ "phrase ::= STRING star_opt",
       186906  + /*  26 */ "star_opt ::= STAR",
       186907  + /*  27 */ "star_opt ::=",
186618 186908   };
186619 186909   #endif /* NDEBUG */
186620 186910   
186621 186911   
186622 186912   #if fts5YYSTACKDEPTH<=0
186623 186913   /*
186624 186914   ** Try to increase the size of the parser stack.  Return the number
................................................................................
186731 187021       ** being destroyed before it is finished parsing.
186732 187022       **
186733 187023       ** Note: during a reduce, the only symbols destroyed are those
186734 187024       ** which appear on the RHS of the rule, but which are *not* used
186735 187025       ** inside the C code.
186736 187026       */
186737 187027   /********* Begin destructor definitions ***************************************/
186738         -    case 16: /* input */
       187028  +    case 17: /* input */
186739 187029   {
186740 187030    (void)pParse; 
186741 187031   }
186742 187032         break;
186743         -    case 17: /* expr */
186744         -    case 18: /* cnearset */
186745         -    case 19: /* exprlist */
       187033  +    case 18: /* expr */
       187034  +    case 19: /* cnearset */
       187035  +    case 20: /* exprlist */
       187036  +{
       187037  + sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy54)); 
       187038  +}
       187039  +      break;
       187040  +    case 21: /* colset */
       187041  +    case 22: /* colsetlist */
186746 187042   {
186747         - sqlite3Fts5ParseNodeFree((fts5yypminor->fts5yy24)); 
       187043  + sqlite3_free((fts5yypminor->fts5yy43)); 
186748 187044   }
186749 187045         break;
186750         -    case 20: /* colset */
186751         -    case 21: /* colsetlist */
       187046  +    case 23: /* nearset */
       187047  +    case 24: /* nearphrases */
186752 187048   {
186753         - sqlite3_free((fts5yypminor->fts5yy11)); 
       187049  + sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy14)); 
186754 187050   }
186755 187051         break;
186756         -    case 22: /* nearset */
186757         -    case 23: /* nearphrases */
       187052  +    case 25: /* phrase */
186758 187053   {
186759         - sqlite3Fts5ParseNearsetFree((fts5yypminor->fts5yy46)); 
186760         -}
186761         -      break;
186762         -    case 24: /* phrase */
186763         -{
186764         - sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy53)); 
       187054  + sqlite3Fts5ParsePhraseFree((fts5yypminor->fts5yy11)); 
186765 187055   }
186766 187056         break;
186767 187057   /********* End destructor definitions *****************************************/
186768 187058       default:  break;   /* If no destructor action specified: do nothing */
186769 187059     }
186770 187060   }
186771 187061   
................................................................................
187011 187301   /* The following table contains information about every rule that
187012 187302   ** is used during the reduce.
187013 187303   */
187014 187304   static const struct {
187015 187305     fts5YYCODETYPE lhs;       /* Symbol on the left-hand side of the rule */
187016 187306     signed char nrhs;     /* Negative of the number of RHS symbols in the rule */
187017 187307   } fts5yyRuleInfo[] = {
187018         -  { 16, -1 },
187019         -  { 20, -4 },
187020         -  { 20, -3 },
       187308  +  { 17, -1 },
       187309  +  { 21, -4 },
       187310  +  { 21, -3 },
       187311  +  { 21, -1 },
       187312  +  { 21, -2 },
       187313  +  { 22, -2 },
       187314  +  { 22, -1 },
       187315  +  { 18, -3 },
       187316  +  { 18, -3 },
       187317  +  { 18, -3 },
       187318  +  { 18, -5 },
       187319  +  { 18, -3 },
       187320  +  { 18, -1 },
187021 187321     { 20, -1 },
187022 187322     { 20, -2 },
187023         -  { 21, -2 },
187024         -  { 21, -1 },
187025         -  { 17, -3 },
187026         -  { 17, -3 },
187027         -  { 17, -3 },
187028         -  { 17, -5 },
187029         -  { 17, -3 },
187030         -  { 17, -1 },
187031 187323     { 19, -1 },
187032         -  { 19, -2 },
187033         -  { 18, -1 },
187034         -  { 18, -3 },
187035         -  { 22, -1 },
187036         -  { 22, -5 },
       187324  +  { 19, -3 },
187037 187325     { 23, -1 },
187038 187326     { 23, -2 },
187039         -  { 25, 0 },
187040         -  { 25, -2 },
187041         -  { 24, -4 },
       187327  +  { 23, -5 },
       187328  +  { 24, -1 },
187042 187329     { 24, -2 },
187043         -  { 26, -1 },
187044 187330     { 26, 0 },
       187331  +  { 26, -2 },
       187332  +  { 25, -4 },
       187333  +  { 25, -2 },
       187334  +  { 27, -1 },
       187335  +  { 27, 0 },
187045 187336   };
187046 187337   
187047 187338   static void fts5yy_accept(fts5yyParser*);  /* Forward Declaration */
187048 187339   
187049 187340   /*
187050 187341   ** Perform a reduce action and the shift that must immediately
187051 187342   ** follow the reduce.
................................................................................
187102 187393     **     { ... }           // User supplied code
187103 187394     **  #line <lineno> <thisfile>
187104 187395     **     break;
187105 187396     */
187106 187397   /********** Begin reduce actions **********************************************/
187107 187398           fts5YYMINORTYPE fts5yylhsminor;
187108 187399         case 0: /* input ::= expr */
187109         -{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy24); }
       187400  +{ sqlite3Fts5ParseFinished(pParse, fts5yymsp[0].minor.fts5yy54); }
187110 187401           break;
187111 187402         case 1: /* colset ::= MINUS LCP colsetlist RCP */
187112 187403   { 
187113         -    fts5yymsp[-3].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
       187404  +    fts5yymsp[-3].minor.fts5yy43 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy43);
187114 187405   }
187115 187406           break;
187116 187407         case 2: /* colset ::= LCP colsetlist RCP */
187117         -{ fts5yymsp[-2].minor.fts5yy11 = fts5yymsp[-1].minor.fts5yy11; }
       187408  +{ fts5yymsp[-2].minor.fts5yy43 = fts5yymsp[-1].minor.fts5yy43; }
187118 187409           break;
187119 187410         case 3: /* colset ::= STRING */
187120 187411   {
187121         -  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
       187412  +  fts5yylhsminor.fts5yy43 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
187122 187413   }
187123         -  fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
       187414  +  fts5yymsp[0].minor.fts5yy43 = fts5yylhsminor.fts5yy43;
187124 187415           break;
187125 187416         case 4: /* colset ::= MINUS STRING */
187126 187417   {
187127         -  fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
187128         -  fts5yymsp[-1].minor.fts5yy11 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy11);
       187418  +  fts5yymsp[-1].minor.fts5yy43 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0);
       187419  +  fts5yymsp[-1].minor.fts5yy43 = sqlite3Fts5ParseColsetInvert(pParse, fts5yymsp[-1].minor.fts5yy43);
187129 187420   }
187130 187421           break;
187131 187422         case 5: /* colsetlist ::= colsetlist STRING */
187132 187423   { 
187133         -  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy11, &fts5yymsp[0].minor.fts5yy0); }
187134         -  fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
       187424  +  fts5yylhsminor.fts5yy43 = sqlite3Fts5ParseColset(pParse, fts5yymsp[-1].minor.fts5yy43, &fts5yymsp[0].minor.fts5yy0); }
       187425  +  fts5yymsp[-1].minor.fts5yy43 = fts5yylhsminor.fts5yy43;
187135 187426           break;
187136 187427         case 6: /* colsetlist ::= STRING */
187137 187428   { 
187138         -  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0); 
       187429  +  fts5yylhsminor.fts5yy43 = sqlite3Fts5ParseColset(pParse, 0, &fts5yymsp[0].minor.fts5yy0); 
187139 187430   }
187140         -  fts5yymsp[0].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
       187431  +  fts5yymsp[0].minor.fts5yy43 = fts5yylhsminor.fts5yy43;
187141 187432           break;
187142 187433         case 7: /* expr ::= expr AND expr */
187143 187434   {
187144         -  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
       187435  +  fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_AND, fts5yymsp[-2].minor.fts5yy54, fts5yymsp[0].minor.fts5yy54, 0);
187145 187436   }
187146         -  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187437  +  fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187147 187438           break;
187148 187439         case 8: /* expr ::= expr OR expr */
187149 187440   {
187150         -  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
       187441  +  fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_OR, fts5yymsp[-2].minor.fts5yy54, fts5yymsp[0].minor.fts5yy54, 0);
187151 187442   }
187152         -  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187443  +  fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187153 187444           break;
187154 187445         case 9: /* expr ::= expr NOT expr */
187155 187446   {
187156         -  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24, 0);
       187447  +  fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_NOT, fts5yymsp[-2].minor.fts5yy54, fts5yymsp[0].minor.fts5yy54, 0);
187157 187448   }
187158         -  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187449  +  fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187159 187450           break;
187160 187451         case 10: /* expr ::= colset COLON LP expr RP */
187161 187452   {
187162         -  sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[-4].minor.fts5yy11);
187163         -  fts5yylhsminor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;
       187453  +  sqlite3Fts5ParseSetColset(pParse, fts5yymsp[-1].minor.fts5yy54, fts5yymsp[-4].minor.fts5yy43);
       187454  +  fts5yylhsminor.fts5yy54 = fts5yymsp[-1].minor.fts5yy54;
187164 187455   }
187165         -  fts5yymsp[-4].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187456  +  fts5yymsp[-4].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187166 187457           break;
187167 187458         case 11: /* expr ::= LP expr RP */
187168         -{fts5yymsp[-2].minor.fts5yy24 = fts5yymsp[-1].minor.fts5yy24;}
       187459  +{fts5yymsp[-2].minor.fts5yy54 = fts5yymsp[-1].minor.fts5yy54;}
187169 187460           break;
187170 187461         case 12: /* expr ::= exprlist */
187171 187462         case 13: /* exprlist ::= cnearset */ fts5yytestcase(fts5yyruleno==13);
187172         -{fts5yylhsminor.fts5yy24 = fts5yymsp[0].minor.fts5yy24;}
187173         -  fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187463  +{fts5yylhsminor.fts5yy54 = fts5yymsp[0].minor.fts5yy54;}
       187464  +  fts5yymsp[0].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187174 187465           break;
187175 187466         case 14: /* exprlist ::= exprlist cnearset */
187176 187467   {
187177         -  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy24, fts5yymsp[0].minor.fts5yy24);
       187468  +  fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseImplicitAnd(pParse, fts5yymsp[-1].minor.fts5yy54, fts5yymsp[0].minor.fts5yy54);
187178 187469   }
187179         -  fts5yymsp[-1].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187470  +  fts5yymsp[-1].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187180 187471           break;
187181 187472         case 15: /* cnearset ::= nearset */
187182 187473   { 
187183         -  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46); 
       187474  +  fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy14); 
187184 187475   }
187185         -  fts5yymsp[0].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187476  +  fts5yymsp[0].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187186 187477           break;
187187 187478         case 16: /* cnearset ::= colset COLON nearset */
187188 187479   { 
187189         -  fts5yylhsminor.fts5yy24 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy46); 
187190         -  sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy24, fts5yymsp[-2].minor.fts5yy11);
       187480  +  fts5yylhsminor.fts5yy54 = sqlite3Fts5ParseNode(pParse, FTS5_STRING, 0, 0, fts5yymsp[0].minor.fts5yy14); 
       187481  +  sqlite3Fts5ParseSetColset(pParse, fts5yylhsminor.fts5yy54, fts5yymsp[-2].minor.fts5yy43);
187191 187482   }
187192         -  fts5yymsp[-2].minor.fts5yy24 = fts5yylhsminor.fts5yy24;
       187483  +  fts5yymsp[-2].minor.fts5yy54 = fts5yylhsminor.fts5yy54;
187193 187484           break;
187194 187485         case 17: /* nearset ::= phrase */
187195         -{ fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); }
187196         -  fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
       187486  +{ fts5yylhsminor.fts5yy14 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy11); }
       187487  +  fts5yymsp[0].minor.fts5yy14 = fts5yylhsminor.fts5yy14;
187197 187488           break;
187198         -      case 18: /* nearset ::= STRING LP nearphrases neardist_opt RP */
       187489  +      case 18: /* nearset ::= CARET phrase */
       187490  +{ 
       187491  +  sqlite3Fts5ParseSetCaret(fts5yymsp[0].minor.fts5yy11);
       187492  +  fts5yymsp[-1].minor.fts5yy14 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy11); 
       187493  +}
       187494  +        break;
       187495  +      case 19: /* nearset ::= STRING LP nearphrases neardist_opt RP */
187199 187496   {
187200 187497     sqlite3Fts5ParseNear(pParse, &fts5yymsp[-4].minor.fts5yy0);
187201         -  sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy46, &fts5yymsp[-1].minor.fts5yy0);
187202         -  fts5yylhsminor.fts5yy46 = fts5yymsp[-2].minor.fts5yy46;
       187498  +  sqlite3Fts5ParseSetDistance(pParse, fts5yymsp[-2].minor.fts5yy14, &fts5yymsp[-1].minor.fts5yy0);
       187499  +  fts5yylhsminor.fts5yy14 = fts5yymsp[-2].minor.fts5yy14;
187203 187500   }
187204         -  fts5yymsp[-4].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
       187501  +  fts5yymsp[-4].minor.fts5yy14 = fts5yylhsminor.fts5yy14;
187205 187502           break;
187206         -      case 19: /* nearphrases ::= phrase */
       187503  +      case 20: /* nearphrases ::= phrase */
187207 187504   { 
187208         -  fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy53); 
       187505  +  fts5yylhsminor.fts5yy14 = sqlite3Fts5ParseNearset(pParse, 0, fts5yymsp[0].minor.fts5yy11); 
187209 187506   }
187210         -  fts5yymsp[0].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
       187507  +  fts5yymsp[0].minor.fts5yy14 = fts5yylhsminor.fts5yy14;
187211 187508           break;
187212         -      case 20: /* nearphrases ::= nearphrases phrase */
       187509  +      case 21: /* nearphrases ::= nearphrases phrase */
187213 187510   {
187214         -  fts5yylhsminor.fts5yy46 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy46, fts5yymsp[0].minor.fts5yy53);
       187511  +  fts5yylhsminor.fts5yy14 = sqlite3Fts5ParseNearset(pParse, fts5yymsp[-1].minor.fts5yy14, fts5yymsp[0].minor.fts5yy11);
187215 187512   }
187216         -  fts5yymsp[-1].minor.fts5yy46 = fts5yylhsminor.fts5yy46;
       187513  +  fts5yymsp[-1].minor.fts5yy14 = fts5yylhsminor.fts5yy14;
187217 187514           break;
187218         -      case 21: /* neardist_opt ::= */
       187515  +      case 22: /* neardist_opt ::= */
187219 187516   { fts5yymsp[1].minor.fts5yy0.p = 0; fts5yymsp[1].minor.fts5yy0.n = 0; }
187220 187517           break;
187221         -      case 22: /* neardist_opt ::= COMMA STRING */
       187518  +      case 23: /* neardist_opt ::= COMMA STRING */
187222 187519   { fts5yymsp[-1].minor.fts5yy0 = fts5yymsp[0].minor.fts5yy0; }
187223 187520           break;
187224         -      case 23: /* phrase ::= phrase PLUS STRING star_opt */
       187521  +      case 24: /* phrase ::= phrase PLUS STRING star_opt */
187225 187522   { 
187226         -  fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy53, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
       187523  +  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseTerm(pParse, fts5yymsp[-3].minor.fts5yy11, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
187227 187524   }
187228         -  fts5yymsp[-3].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
       187525  +  fts5yymsp[-3].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
187229 187526           break;
187230         -      case 24: /* phrase ::= STRING star_opt */
       187527  +      case 25: /* phrase ::= STRING star_opt */
187231 187528   { 
187232         -  fts5yylhsminor.fts5yy53 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
       187529  +  fts5yylhsminor.fts5yy11 = sqlite3Fts5ParseTerm(pParse, 0, &fts5yymsp[-1].minor.fts5yy0, fts5yymsp[0].minor.fts5yy4);
187233 187530   }
187234         -  fts5yymsp[-1].minor.fts5yy53 = fts5yylhsminor.fts5yy53;
       187531  +  fts5yymsp[-1].minor.fts5yy11 = fts5yylhsminor.fts5yy11;
187235 187532           break;
187236         -      case 25: /* star_opt ::= STAR */
       187533  +      case 26: /* star_opt ::= STAR */
187237 187534   { fts5yymsp[0].minor.fts5yy4 = 1; }
187238 187535           break;
187239         -      case 26: /* star_opt ::= */
       187536  +      case 27: /* star_opt ::= */
187240 187537   { fts5yymsp[1].minor.fts5yy4 = 0; }
187241 187538           break;
187242 187539         default:
187243 187540           break;
187244 187541   /********** End reduce actions ************************************************/
187245 187542     };
187246 187543     assert( fts5yyruleno<sizeof(fts5yyRuleInfo)/sizeof(fts5yyRuleInfo[0]) );
................................................................................
189655 189952   #define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
189656 189953   
189657 189954   /*
189658 189955   ** An instance of the following structure represents a single search term
189659 189956   ** or term prefix.
189660 189957   */
189661 189958   struct Fts5ExprTerm {
189662         -  int bPrefix;                    /* True for a prefix term */
       189959  +  u8 bPrefix;                     /* True for a prefix term */
       189960  +  u8 bFirst;                      /* True if token must be first in column */
189663 189961     char *zTerm;                    /* nul-terminated term */
189664 189962     Fts5IndexIter *pIter;           /* Iterator for this term */
189665 189963     Fts5ExprTerm *pSynonym;         /* Pointer to first in list of synonyms */
189666 189964   };
189667 189965   
189668 189966   /*
189669 189967   ** A phrase. One or more terms that must appear in a contiguous sequence
................................................................................
189736 190034       case '{':  tok = FTS5_LCP;   break;
189737 190035       case '}':  tok = FTS5_RCP;   break;
189738 190036       case ':':  tok = FTS5_COLON; break;
189739 190037       case ',':  tok = FTS5_COMMA; break;
189740 190038       case '+':  tok = FTS5_PLUS;  break;
189741 190039       case '*':  tok = FTS5_STAR;  break;
189742 190040       case '-':  tok = FTS5_MINUS; break;
       190041  +    case '^':  tok = FTS5_CARET; break;
189743 190042       case '\0': tok = FTS5_EOF;   break;
189744 190043   
189745 190044       case '"': {
189746 190045         const char *z2;
189747 190046         tok = FTS5_STRING;
189748 190047   
189749 190048         for(z2=&z[1]; 1; z2++){
................................................................................
189995 190294     int *pbMatch                    /* OUT: Set to true if really a match */
189996 190295   ){
189997 190296     Fts5PoslistWriter writer = {0};
189998 190297     Fts5PoslistReader aStatic[4];
189999 190298     Fts5PoslistReader *aIter = aStatic;
190000 190299     int i;
190001 190300     int rc = SQLITE_OK;
       190301  +  int bFirst = pPhrase->aTerm[0].bFirst;
190002 190302     
190003 190303     fts5BufferZero(&pPhrase->poslist);
190004 190304   
190005 190305     /* If the aStatic[] array is not large enough, allocate a large array
190006 190306     ** using sqlite3_malloc(). This approach could be improved upon. */
190007 190307     if( pPhrase->nTerm>ArraySize(aStatic) ){
190008 190308       int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
................................................................................
190049 190349             }
190050 190350             if( pPos->iPos>iAdj ) iPos = pPos->iPos-i;
190051 190351           }
190052 190352         }
190053 190353       }while( bMatch==0 );
190054 190354   
190055 190355       /* Append position iPos to the output */
190056         -    rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
190057         -    if( rc!=SQLITE_OK ) goto ismatch_out;
       190356  +    if( bFirst==0 || FTS5_POS2OFFSET(iPos)==0 ){
       190357  +      rc = sqlite3Fts5PoslistWriterAppend(&pPhrase->poslist, &writer, iPos);
       190358  +      if( rc!=SQLITE_OK ) goto ismatch_out;
       190359  +    }
190058 190360   
190059 190361       for(i=0; i<pPhrase->nTerm; i++){
190060 190362         if( sqlite3Fts5PoslistReaderNext(&aIter[i]) ) goto ismatch_out;
190061 190363       }
190062 190364     }
190063 190365   
190064 190366    ismatch_out:
................................................................................
190304 190606       int i;
190305 190607   
190306 190608       /* Check that each phrase in the nearset matches the current row.
190307 190609       ** Populate the pPhrase->poslist buffers at the same time. If any
190308 190610       ** phrase is not a match, break out of the loop early.  */
190309 190611       for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
190310 190612         Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
190311         -      if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){
       190613  +      if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym 
       190614  +       || pNear->pColset || pPhrase->aTerm[0].bFirst
       190615  +      ){
190312 190616           int bMatch = 0;
190313 190617           rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
190314 190618           if( bMatch==0 ) break;
190315 190619         }else{
190316 190620           Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
190317 190621           fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
190318 190622         }
................................................................................
190485 190789     int bMatch;                     /* True if all terms are at the same rowid */
190486 190790     const int bDesc = pExpr->bDesc;
190487 190791   
190488 190792     /* Check that this node should not be FTS5_TERM */
190489 190793     assert( pNear->nPhrase>1 
190490 190794          || pNear->apPhrase[0]->nTerm>1 
190491 190795          || pNear->apPhrase[0]->aTerm[0].pSynonym
       190796  +       || pNear->apPhrase[0]->aTerm[0].bFirst
190492 190797     );
190493 190798   
190494 190799     /* Initialize iLast, the "lastest" rowid any iterator points to. If the
190495 190800     ** iterator skips through rowids in the default ascending order, this means
190496 190801     ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
190497 190802     ** means the minimum rowid.  */
190498 190803     if( pLeft->aTerm[0].pSynonym ){
................................................................................
191008 191313           sqlite3_free(pSyn);
191009 191314         }
191010 191315       }
191011 191316       if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
191012 191317       sqlite3_free(pPhrase);
191013 191318     }
191014 191319   }
       191320  +
       191321  +/*
       191322  +** Set the "bFirst" flag on the first token of the phrase passed as the
       191323  +** only argument.
       191324  +*/
       191325  +static void sqlite3Fts5ParseSetCaret(Fts5ExprPhrase *pPhrase){
       191326  +  if( pPhrase && pPhrase->nTerm ){
       191327  +    pPhrase->aTerm[0].bFirst = 1;
       191328  +  }
       191329  +}
191015 191330   
191016 191331   /*
191017 191332   ** If argument pNear is NULL, then a new Fts5ExprNearset object is allocated
191018 191333   ** and populated with pPhrase. Or, if pNear is not NULL, phrase pPhrase is
191019 191334   ** appended to it and the results returned.
191020 191335   **
191021 191336   ** If an OOM error occurs, both the pNear and pPhrase objects are freed and
................................................................................
191287 191602           const char *zTerm = p->zTerm;
191288 191603           rc = fts5ParseTokenize((void*)&sCtx, tflags, zTerm, (int)strlen(zTerm),
191289 191604               0, 0);
191290 191605           tflags = FTS5_TOKEN_COLOCATED;
191291 191606         }
191292 191607         if( rc==SQLITE_OK ){
191293 191608           sCtx.pPhrase->aTerm[i].bPrefix = pOrig->aTerm[i].bPrefix;
       191609  +        sCtx.pPhrase->aTerm[i].bFirst = pOrig->aTerm[i].bFirst;
191294 191610         }
191295 191611       }
191296 191612     }else{
191297 191613       /* This happens when parsing a token or quoted phrase that contains
191298 191614       ** no token characters at all. (e.g ... MATCH '""'). */
191299 191615       sCtx.pPhrase = sqlite3Fts5MallocZero(&rc, sizeof(Fts5ExprPhrase));
191300 191616     }
................................................................................
191305 191621       pNew->pConfig = pExpr->pConfig;
191306 191622       pNew->nPhrase = 1;
191307 191623       pNew->apExprPhrase[0] = sCtx.pPhrase;
191308 191624       pNew->pRoot->pNear->apPhrase[0] = sCtx.pPhrase;
191309 191625       pNew->pRoot->pNear->nPhrase = 1;
191310 191626       sCtx.pPhrase->pNode = pNew->pRoot;
191311 191627   
191312         -    if( pOrig->nTerm==1 && pOrig->aTerm[0].pSynonym==0 ){
       191628  +    if( pOrig->nTerm==1 
       191629  +     && pOrig->aTerm[0].pSynonym==0 
       191630  +     && pOrig->aTerm[0].bFirst==0 
       191631  +    ){
191313 191632         pNew->pRoot->eType = FTS5_TERM;
191314 191633         pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
191315 191634       }else{
191316 191635         pNew->pRoot->eType = FTS5_STRING;
191317 191636         pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
191318 191637       }
191319 191638     }else{
................................................................................
191579 191898   
191580 191899   static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
191581 191900     switch( pNode->eType ){
191582 191901       case FTS5_STRING: {
191583 191902         Fts5ExprNearset *pNear = pNode->pNear;
191584 191903         if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1 
191585 191904          && pNear->apPhrase[0]->aTerm[0].pSynonym==0
       191905  +       && pNear->apPhrase[0]->aTerm[0].bFirst==0
191586 191906         ){
191587 191907           pNode->eType = FTS5_TERM;
191588 191908           pNode->xNext = fts5ExprNodeNext_TERM;
191589 191909         }else{
191590 191910           pNode->xNext = fts5ExprNodeNext_STRING;
191591 191911         }
191592 191912         break;
................................................................................
191665 191985             pNear->apPhrase[iPhrase]->pNode = pRet;
191666 191986             if( pNear->apPhrase[iPhrase]->nTerm==0 ){
191667 191987               pRet->xNext = 0;
191668 191988               pRet->eType = FTS5_EOF;
191669 191989             }
191670 191990           }
191671 191991   
191672         -        if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL 
191673         -         && (pNear->nPhrase!=1 || pNear->apPhrase[0]->nTerm>1)
191674         -        ){
191675         -          assert( pParse->rc==SQLITE_OK );
191676         -          pParse->rc = SQLITE_ERROR;
191677         -          assert( pParse->zErr==0 );
191678         -          pParse->zErr = sqlite3_mprintf(
191679         -              "fts5: %s queries are not supported (detail!=full)", 
191680         -              pNear->nPhrase==1 ? "phrase": "NEAR"
191681         -          );
191682         -          sqlite3_free(pRet);
191683         -          pRet = 0;
       191992  +        if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
       191993  +          Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
       191994  +          if( pNear->nPhrase!=1 
       191995  +           || pPhrase->nTerm>1
       191996  +           || (pPhrase->nTerm>0 && pPhrase->aTerm[0].bFirst)
       191997  +          ){
       191998  +            assert( pParse->rc==SQLITE_OK );
       191999  +            pParse->rc = SQLITE_ERROR;
       192000  +            assert( pParse->zErr==0 );
       192001  +            pParse->zErr = sqlite3_mprintf(
       192002  +                "fts5: %s queries are not supported (detail!=full)", 
       192003  +                pNear->nPhrase==1 ? "phrase": "NEAR"
       192004  +                );
       192005  +            sqlite3_free(pRet);
       192006  +            pRet = 0;
       192007  +          }
191684 192008           }
191685         -
191686 192009         }else{
191687 192010           fts5ExprAddChildren(pRet, pLeft);
191688 192011           fts5ExprAddChildren(pRet, pRight);
191689 192012         }
191690 192013       }
191691 192014     }
191692 192015   
................................................................................
202047 202370   static void fts5SourceIdFunc(
202048 202371     sqlite3_context *pCtx,          /* Function call context */
202049 202372     int nArg,                       /* Number of args */
202050 202373     sqlite3_value **apUnused        /* Function arguments */
202051 202374   ){
202052 202375     assert( nArg==0 );
202053 202376     UNUSED_PARAM2(nArg, apUnused);
202054         -  sqlite3_result_text(pCtx, "fts5: 2017-11-14 19:34:22 00ec95fcd02bb415dabd7f25fee24856d45d6916c18b2728e97e9bb9b8322ba3", -1, SQLITE_TRANSIENT);
       202377  +  sqlite3_result_text(pCtx, "fts5: 2017-12-01 18:40:18 5771b1d611b3562ea6c040f3f893073c4d0ee58c35b37ae211520d9aee8ed547", -1, SQLITE_TRANSIENT);
202055 202378   }
202056 202379   
202057 202380   static int fts5Init(sqlite3 *db){
202058 202381     static const sqlite3_module fts5Mod = {
202059 202382       /* iVersion      */ 2,
202060 202383       /* xCreate       */ fts5CreateMethod,
202061 202384       /* xConnect      */ fts5ConnectMethod,
................................................................................
206315 206638   #endif
206316 206639     return rc;
206317 206640   }
206318 206641   #endif /* SQLITE_CORE */
206319 206642   #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
206320 206643   
206321 206644   /************** End of stmt.c ************************************************/
206322         -#if __LINE__!=206322
       206645  +#if __LINE__!=206645
206323 206646   #undef SQLITE_SOURCE_ID
206324         -#define SQLITE_SOURCE_ID      "2017-11-14 19:34:22 00ec95fcd02bb415dabd7f25fee24856d45d6916c18b2728e97e9bb9b832alt2"
       206647  +#define SQLITE_SOURCE_ID      "2017-12-06 20:50:08 6a55bd67720451cdf316155cf348103bfce1056a78bcddf0029b45ff0fdbalt2"
206325 206648   #endif
206326 206649   /* Return the source-id for this library */
206327 206650   SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
206328 206651   /************************** End of sqlite3.c ******************************/

Changes to src/sqlite3.h.

   121    121   **
   122    122   ** See also: [sqlite3_libversion()],
   123    123   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
   124    124   ** [sqlite_version()] and [sqlite_source_id()].
   125    125   */
   126    126   #define SQLITE_VERSION        "3.22.0"
   127    127   #define SQLITE_VERSION_NUMBER 3022000
   128         -#define SQLITE_SOURCE_ID      "2017-11-14 19:34:22 00ec95fcd02bb415dabd7f25fee24856d45d6916c18b2728e97e9bb9b8322ba3"
          128  +#define SQLITE_SOURCE_ID      "2017-12-06 20:50:08 6a55bd67720451cdf316155cf348103bfce1056a78bcddf0029b45ff0fdbcc71"
   129    129   
   130    130   /*
   131    131   ** CAPI3REF: Run-Time Library Version Numbers
   132    132   ** KEYWORDS: sqlite3_version sqlite3_sourceid
   133    133   **
   134    134   ** These interfaces provide the same information as the [SQLITE_VERSION],
   135    135   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros