Skip to content
Browse files

Fix the ANALYZE command so that it takes collating sequences into acc…

…ount

when gathering index statistics.
  • Loading branch information...
1 parent 41f27df commit 9f914ce784efda3f8a90486b87a757d83a9a5603 Unknown committed Jan 4, 2011
Showing with 37 additions and 2 deletions.
  1. +6 −2 src/analyze.c
  2. +31 −0 test/analyze4.test
View
8 src/analyze.c
@@ -233,6 +233,7 @@ static void analyzeOneTable(
sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
for(i=0; i<nCol; i++){
+ CollSeq *pColl;
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
if( i==0 ){
#ifdef SQLITE_ENABLE_STAT2
@@ -269,8 +270,11 @@ static void analyzeOneTable(
/* Always record the very first row */
sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
}
- sqlite3VdbeAddOp3(v, OP_Ne, regCol, 0, iMem+nCol+i+1);
- /**** TODO: add collating sequence *****/
+ assert( pIdx->azColl!=0 );
+ assert( pIdx->azColl[i]!=0 );
+ pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
+ sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
+ (char*)pColl, P4_COLLSEQ);
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
}
if( db->mallocFailed ){
View
31 test/analyze4.test
@@ -14,6 +14,8 @@
# a NULL value count as distinct rows for the purposes of analyze
# statistics.
#
+# Also include test cases for collating sequences on indices.
+#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@@ -77,4 +79,33 @@ do_test analyze4-1.3 {
}
} {t1a {128 1} t1b {128 128} t1bcd {128 128 4 2} t1cbd {128 4 4 2} t1cdb {128 4 2 2}}
+# Verify that collating sequences are taken into account when computing
+# ANALYZE statistics.
+#
+do_test analyze4-2.0 {
+ db eval {
+ CREATE TABLE t2(
+ x INTEGER PRIMARY KEY,
+ a TEXT COLLATE nocase,
+ b TEXT COLLATE rtrim,
+ c TEXT COLLATE binary
+ );
+ CREATE INDEX t2a ON t2(a);
+ CREATE INDEX t2b ON t2(b);
+ CREATE INDEX t2c ON t2(c);
+ CREATE INDEX t2c2 ON t2(c COLLATE nocase);
+ CREATE INDEX t2c3 ON t2(c COLLATE rtrim);
+ INSERT INTO t2 VALUES(1, 'abc', 'abc', 'abc');
+ INSERT INTO t2 VALUES(2, 'abC', 'abC', 'abC');
+ INSERT INTO t2 VALUES(3, 'abc ', 'abc ', 'abc ');
+ INSERT INTO t2 VALUES(4, 'abC ', 'abC ', 'abC ');
+ INSERT INTO t2 VALUES(5, 'aBc', 'aBc', 'aBc');
+ INSERT INTO t2 VALUES(6, 'aBC', 'aBC', 'aBC');
+ INSERT INTO t2 VALUES(7, 'aBc ', 'aBc ', 'aBc ');
+ INSERT INTO t2 VALUES(8, 'aBC ', 'aBC ', 'aBC ');
+ ANALYZE;
+ SELECT idx, stat FROM sqlite_stat1 WHERE tbl='t2' ORDER BY idx;
+ }
+} {t2a {8 4} t2b {8 2} t2c {8 1} t2c2 {8 4} t2c3 {8 2}}
+
finish_test

0 comments on commit 9f914ce

Please sign in to comment.
Something went wrong with that request. Please try again.