]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix the ANALYZE command to work with partial indices.
authordrh <drh@noemail.net>
Thu, 1 Aug 2013 04:39:17 +0000 (04:39 +0000)
committerdrh <drh@noemail.net>
Thu, 1 Aug 2013 04:39:17 +0000 (04:39 +0000)
FossilOrigin-Name: 60353124f4e965393ecd864019bdbca1999fb69e

manifest
manifest.uuid
src/analyze.c
test/index6.test

index 3e75f02aa7f88db216c16afeb9fc5b829bc0b49c..b7bf7869eada165dd5210c7667867a3c32af4869 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Test\scases\sand\sbug\sfixes\sfor\sthe\spartial\sindex\slogic.
-D 2013-08-01T03:36:59.964
+C Fix\sthe\sANALYZE\scommand\sto\swork\swith\spartial\sindices.
+D 2013-08-01T04:39:17.756
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 5e41da95d92656a5004b03d3576e8b226858a28e
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -157,7 +157,7 @@ F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
 F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc
 F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad
 F src/alter.c f8db986c03eb0bfb221523fc9bbb9d0b70de3168
-F src/analyze.c 27e541b9b5b48b41eb899b22a49ff42384899151
+F src/analyze.c a33fcb0b3a399d966951feb9f32115106b3ecc2e
 F src/attach.c 1816f5a9eea8d2010fc2b22b44f0f63eb3a62704
 F src/auth.c 523da7fb4979469955d822ff9298352d6b31de34
 F src/backup.c 43b348822db3e4cef48b2ae5a445fbeb6c73a165
@@ -589,7 +589,7 @@ F test/index2.test ee83c6b5e3173a3d7137140d945d9a5d4fdfb9d6
 F test/index3.test 423a25c789fc8cc51aaf2a4370bbdde2d9e9eed7
 F test/index4.test 2983216eb8c86ee62d9ed7cb206b5cc3331c0026
 F test/index5.test fc07c14193c0430814e7a08b5da46888ee795c33
-F test/index6.test 7030e7b77a0809eea33b2a1865379bb8010fba72
+F test/index6.test 9db2067dc06bfa4f664193d860297d50775d31e7
 F test/indexedby.test 0e959308707c808515c3a51363f7a9835027108c
 F test/indexfault.test 31d4ab9a7d2f6e9616933eb079722362a883eb1d
 F test/init.test 15c823093fdabbf7b531fe22cf037134d09587a7
@@ -1104,7 +1104,7 @@ F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/wherecosttest.c f407dc4c79786982a475261866a161cd007947ae
 F tool/win/sqlite.vsix 97894c2790eda7b5bce3cc79cb2a8ec2fde9b3ac
-P fb9044d15ad4fd6ae4a38858c0c0e6fe9d4faa25
-R 4044286eb4e933efbb5ac1ee2d79d687
+P 6b73ae7c123801787c8994113cbeb87ee96ba653
+R 8f1421e98f999d36a1d28b17b8226989
 U drh
-Z 8fdacf32e3d306969cff97932cabfb4b
+Z 2b1246919b9a91b92ae0d1de51d4ce2b
index 3d6a3ff1428b5f86fda1d3700a2b73eda6351ec5..8d65f016b3786a5c1c6b2a6817fe3847a97fe2c0 100644 (file)
@@ -1 +1 @@
-6b73ae7c123801787c8994113cbeb87ee96ba653
\ No newline at end of file
+60353124f4e965393ecd864019bdbca1999fb69e
\ No newline at end of file
index 78bbf28b47315eb50b2863da5a57456d15f805d6..d25a9b196cf099270c8b67f08489794c722104af 100644 (file)
@@ -441,6 +441,7 @@ static void analyzeOneTable(
   int endOfLoop;               /* The end of the loop */
   int jZeroRows = -1;          /* Jump from here if number of rows is zero */
   int iDb;                     /* Index of database containing pTab */
+  u8 needTableCnt = 1;         /* True to count the table */
   int regTabname = iMem++;     /* Register containing table name */
   int regIdxname = iMem++;     /* Register containing index name */
   int regStat1 = iMem++;       /* The stat column of sqlite_stat1 */
@@ -500,6 +501,7 @@ static void analyzeOneTable(
     int *aChngAddr;              /* Array of jump instruction addresses */
 
     if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
+    if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
     VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
     nCol = pIdx->nColumn;
     aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
@@ -659,9 +661,7 @@ static void analyzeOneTable(
     ** is never possible.
     */
     sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
-    if( jZeroRows<0 ){
-      jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
-    }
+    jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
     for(i=0; i<nCol; i++){
       sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
@@ -671,32 +671,31 @@ static void analyzeOneTable(
       sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
       sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
     }
+    if( pIdx->pPartIdxWhere!=0 ) sqlite3VdbeJumpHere(v, jZeroRows);
     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
+    if( pIdx->pPartIdxWhere==0 ) sqlite3VdbeJumpHere(v, jZeroRows);
   }
 
-  /* If the table has no indices, create a single sqlite_stat1 entry
-  ** containing NULL as the index name and the row count as the content.
+  /* Create a single sqlite_stat1 entry containing NULL as the index
+  ** name and the row count as the content.
   */
-  if( pTab->pIndex==0 ){
+  if( pOnlyIdx==0 && needTableCnt ){
     sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
     VdbeComment((v, "%s", pTab->zName));
     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
     sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
     jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
-  }else{
+    sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
+    sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
+    sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
+    sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
+    sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
     sqlite3VdbeJumpHere(v, jZeroRows);
-    jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
   }
-  sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
-  sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
-  sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
-  sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
-  sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
   if( pParse->nMem<regRec ) pParse->nMem = regRec;
-  sqlite3VdbeJumpHere(v, jZeroRows);
 }
 
 
@@ -879,8 +878,10 @@ static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
       v = v*10 + c - '0';
       z++;
     }
-    if( i==0 ) pTable->nRowEst = v;
-    if( pIndex==0 ) break;
+    if( i==0 && (pIndex==0 || pIndex->pPartIdxWhere==0) ){
+      if( v>0 ) pTable->nRowEst = v;
+      if( pIndex==0 ) break;
+    }
     pIndex->aiRowEst[i] = v;
     if( *z==' ' ) z++;
     if( strcmp(z, "unordered")==0 ){
index b561b6333fee16296abe7ac6a6d169682b9b6bb5..d283d924c5a203470246d5dd3cf84eeb0104a68f 100644 (file)
@@ -19,12 +19,12 @@ source $testdir/tester.tcl
 load_static_extension db wholenumber;
 do_test index6-1.1 {
   execsql {
-    CREATE TABLE t1(a,b);
+    CREATE TABLE t1(a,b,c);
     CREATE INDEX t1a ON t1(a) WHERE a IS NOT NULL;
     CREATE INDEX t1b ON t1(b) WHERE b>10;
     CREATE VIRTUAL TABLE nums USING wholenumber;
-    INSERT INTO t1(a,b)
-       SELECT CASE WHEN value%3!=0 THEN value END, value
+    INSERT INTO t1(a,b,c)
+       SELECT CASE WHEN value%3!=0 THEN value END, value, value
          FROM nums WHERE value<=20;
     SELECT count(a), count(b) FROM t1;
   }
@@ -32,9 +32,9 @@ do_test index6-1.1 {
 
 do_test index6-1.2 {
   catchsql {
-    CREATE INDEX bad1 ON t1(a,b) WHERE c IS NOT NULL;
+    CREATE INDEX bad1 ON t1(a,b) WHERE x IS NOT NULL;
   }
-} {1 {no such column: c}}
+} {1 {no such column: x}}
 do_test index6-1.3 {
   catchsql {
     CREATE INDEX bad1 ON t1(a,b) WHERE EXISTS(SELECT * FROM t1);
@@ -62,7 +62,7 @@ do_test index6-1.10 {
     SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
     PRAGMA integrity_check;
   }
-} {t1a {14 1} t1b {10 1} ok}
+} {{} 20 t1a {14 1} t1b {10 1} ok}
 
 do_test index6-1.11 {
   execsql {
@@ -71,7 +71,7 @@ do_test index6-1.11 {
     SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
     PRAGMA integrity_check;
   }
-} {t1a {20 1} t1b {10 1} ok}
+} {{} 20 t1a {20 1} t1b {10 1} ok}
 
 do_test index6-1.11 {
   execsql {
@@ -81,7 +81,7 @@ do_test index6-1.11 {
     SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
     PRAGMA integrity_check;
   }
-} {t1a {6 1} t1b {20 1} ok}
+} {{} 20 t1a {6 1} t1b {20 1} ok}
 
 do_test index6-1.12 {
   execsql {
@@ -91,7 +91,7 @@ do_test index6-1.12 {
     SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
     PRAGMA integrity_check;
   }
-} {t1a {13 1} t1b {10 1} ok}
+} {{} 20 t1a {13 1} t1b {10 1} ok}
 
 do_test index6-1.13 {
   execsql {
@@ -100,7 +100,7 @@ do_test index6-1.13 {
     SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
     PRAGMA integrity_check;
   }
-} {t1a {10 1} t1b {8 1} ok}
+} {{} 15 t1a {10 1} t1b {8 1} ok}
 
 do_test index6-1.14 {
   execsql {
@@ -109,6 +109,15 @@ do_test index6-1.14 {
     SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
     PRAGMA integrity_check;
   }
-} {t1a {10 1} t1b {8 1} ok}
+} {{} 15 t1a {10 1} t1b {8 1} ok}
+
+do_test index6-1.15 {
+  execsql {
+    CREATE INDEX t1c ON t1(c);
+    ANALYZE;
+    SELECT idx, stat FROM sqlite_stat1 ORDER BY idx;
+    PRAGMA integrity_check;
+  }
+} {t1a {10 1} t1b {8 1} t1c {15 1} ok}
 
 finish_test