Fix the Parse.nQueryLoop state variable to work with NGQP.

FossilOrigin-Name: f1cac24f06b9c71cfa472fdcf2da4cd8689a7cc3
diff --git a/src/expr.c b/src/expr.c
index c1a27eb..4e834ff 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -1596,15 +1596,15 @@
     /* Could not found an existing table or index to use as the RHS b-tree.
     ** We will have to generate an ephemeral table to do the job.
     */
-    double savedNQueryLoop = pParse->nQueryLoop;
+    u32 savedNQueryLoop = pParse->nQueryLoop;
     int rMayHaveNull = 0;
     eType = IN_INDEX_EPH;
     if( prNotFound ){
       *prNotFound = rMayHaveNull = ++pParse->nMem;
       sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
     }else{
-      testcase( pParse->nQueryLoop>(double)1 );
-      pParse->nQueryLoop = (double)1;
+      testcase( pParse->nQueryLoop>1 );
+      pParse->nQueryLoop = 1;
       if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
         eType = IN_INDEX_ROWID;
       }
diff --git a/src/prepare.c b/src/prepare.c
index d78d83c..319bb50 100644
--- a/src/prepare.c
+++ b/src/prepare.c
@@ -592,7 +592,7 @@
   sqlite3VtabUnlockList(db);
 
   pParse->db = db;
-  pParse->nQueryLoop = (double)1;
+  pParse->nQueryLoop = 1;
   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
     char *zSqlCopy;
     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
@@ -614,7 +614,7 @@
   }else{
     sqlite3RunParser(pParse, zSql, &zErrMsg);
   }
-  assert( 1==(int)pParse->nQueryLoop );
+  assert( 1==pParse->nQueryLoop );
 
   if( db->mallocFailed ){
     pParse->rc = SQLITE_NOMEM;
diff --git a/src/sqliteInt.h b/src/sqliteInt.h
index aa1ea51..d3d6fcf 100644
--- a/src/sqliteInt.h
+++ b/src/sqliteInt.h
@@ -2226,7 +2226,7 @@
   /* Information used while coding trigger programs. */
   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
   Table *pTriggerTab;  /* Table triggers are being coded for */
-  u32 nQueryLoop;      /* Estimated number of iterations of a query */
+  u32 grep nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
   u32 oldmask;         /* Mask of old.* columns referenced */
   u32 newmask;         /* Mask of new.* columns referenced */
   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
diff --git a/src/where.c b/src/where.c
index 330faf7..d493dba 100644
--- a/src/where.c
+++ b/src/where.c
@@ -358,7 +358,7 @@
   WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
   WhereClause sWC;          /* Decomposition of the WHERE clause */
   WhereLoop *pLoops;        /* List of all WhereLoop objects */
-  WhereCost savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
+  int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
   WhereCost nRowOut;        /* Estimated number of output rows */
   WhereLevel a[1];          /* Information about each nest loop in WHERE */
 };
@@ -3099,7 +3099,7 @@
     }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
       zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
 
-      if( flags&WHERE_COLUMN_EQ ){
+      if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
       }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
@@ -4432,7 +4432,7 @@
 
   /* Automatic indexes */
   if( !pBuilder->pBest
-   && pTabList->nSrc>1
+//   && pTabList->nSrc>1
    && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0 
    && !pSrc->viaCoroutine
    && !pSrc->notIndexed
@@ -5074,7 +5074,7 @@
   }
 
   /* Seed the search with a single WherePath containing zero WhereLoops */
-  aFrom[0].nRow = 0;
+  aFrom[0].nRow = pWInfo->pParse->nQueryLoop;
   nFrom = 1;
 
   /* Precompute the cost of sorting the final result set, if the caller
@@ -5615,6 +5615,7 @@
   }
 #endif
   WHERETRACE(("*** Optimizer Finished ***\n"));
+  pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
 
 #if 0  /* FIXME: Add this back in? */
   /* If the caller is an UPDATE or DELETE statement that is requesting