Fossil SCM

Update the built-in SQLite to the latest 3.8.1 beta from upstream.

drh 2013-10-10 15:19 trunk
Commit cb29ef2a1e8b7ccdd4c732357ce7d468b3a7a184
2 files changed +395 -279 +1 -1
+395 -279
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -656,11 +656,11 @@
656656
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657657
** [sqlite_version()] and [sqlite_source_id()].
658658
*/
659659
#define SQLITE_VERSION "3.8.1"
660660
#define SQLITE_VERSION_NUMBER 3008001
661
-#define SQLITE_SOURCE_ID "2013-10-07 21:49:16 36d64dc36f18c166b2c93c43579fa3bbb5cd545f"
661
+#define SQLITE_SOURCE_ID "2013-10-10 15:04:52 af7abebeb1f70466833bc766d294d721eaef746f"
662662
663663
/*
664664
** CAPI3REF: Run-Time Library Version Numbers
665665
** KEYWORDS: sqlite3_version, sqlite3_sourceid
666666
**
@@ -8272,10 +8272,35 @@
82728272
typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
82738273
#else
82748274
typedef u32 tRowcnt; /* 32-bit is the default */
82758275
#endif
82768276
8277
+/*
8278
+** Estimated quantities used for query planning are stored as 16-bit
8279
+** logarithms. For quantity X, the value stored is 10*log2(X). This
8280
+** gives a possible range of values of approximately 1.0e986 to 1e-986.
8281
+** But the allowed values are "grainy". Not every value is representable.
8282
+** For example, quantities 16 and 17 are both represented by a LogEst
8283
+** of 40. However, since LogEst quantatites are suppose to be estimates,
8284
+** not exact values, this imprecision is not a problem.
8285
+**
8286
+** "LogEst" is short for "Logarithimic Estimate".
8287
+**
8288
+** Examples:
8289
+** 1 -> 0 20 -> 43 10000 -> 132
8290
+** 2 -> 10 25 -> 46 25000 -> 146
8291
+** 3 -> 16 100 -> 66 1000000 -> 199
8292
+** 4 -> 20 1000 -> 99 1048576 -> 200
8293
+** 10 -> 33 1024 -> 100 4294967296 -> 320
8294
+**
8295
+** The LogEst can be negative to indicate fractional values.
8296
+** Examples:
8297
+**
8298
+** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
8299
+*/
8300
+typedef INT16_TYPE LogEst;
8301
+
82778302
/*
82788303
** Macros to determine whether the machine is big or little endian,
82798304
** evaluated at runtime.
82808305
*/
82818306
#ifdef SQLITE_AMALGAMATION
@@ -10419,11 +10444,12 @@
1041910444
char *zDflt; /* Original text of the default value */
1042010445
char *zType; /* Data type for this column */
1042110446
char *zColl; /* Collating sequence. If NULL, use the default */
1042210447
u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
1042310448
char affinity; /* One of the SQLITE_AFF_... values */
10424
- u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
10449
+ u8 szEst; /* Estimated size of this column. INT==1 */
10450
+ u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */
1042510451
};
1042610452
1042710453
/* Allowed values for Column.colFlags:
1042810454
*/
1042910455
#define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
@@ -10583,10 +10609,11 @@
1058310609
tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
1058410610
int tnum; /* Root BTree node for this table (see note above) */
1058510611
i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
1058610612
i16 nCol; /* Number of columns in this table */
1058710613
u16 nRef; /* Number of pointers to this Table */
10614
+ LogEst szTabRow; /* Estimated size of each table row in bytes */
1058810615
u8 tabFlags; /* Mask of TF_* values */
1058910616
u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
1059010617
#ifndef SQLITE_OMIT_ALTERTABLE
1059110618
int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
1059210619
#endif
@@ -10694,11 +10721,11 @@
1069410721
#define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
1069510722
#define OE_SetNull 7 /* Set the foreign key value to NULL */
1069610723
#define OE_SetDflt 8 /* Set the foreign key value to its default */
1069710724
#define OE_Cascade 9 /* Cascade the changes */
1069810725
10699
-#define OE_Default 99 /* Do whatever the default action is */
10726
+#define OE_Default 10 /* Do whatever the default action is */
1070010727
1070110728
1070210729
/*
1070310730
** An instance of the following structure is passed as the first
1070410731
** argument to sqlite3VdbeKeyCompare and is used to control the
@@ -10781,10 +10808,11 @@
1078110808
Schema *pSchema; /* Schema containing this index */
1078210809
u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
1078310810
char **azColl; /* Array of collation sequence names for index */
1078410811
Expr *pPartIdxWhere; /* WHERE clause for partial indices */
1078510812
int tnum; /* DB Page containing root of this index */
10813
+ LogEst szIdxRow; /* Estimated average row size in bytes */
1078610814
u16 nColumn; /* Number of columns in table used by this index */
1078710815
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1078810816
unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
1078910817
unsigned bUnordered:1; /* Use this index for == or IN queries only */
1079010818
unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
@@ -12187,10 +12215,16 @@
1218712215
SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
1218812216
SQLITE_PRIVATE int sqlite3Atoi(const char*);
1218912217
SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
1219012218
SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
1219112219
SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
12220
+SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
12221
+SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
12222
+#ifndef SQLITE_OMIT_VIRTUALTABLE
12223
+SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
12224
+#endif
12225
+SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
1219212226
1219312227
/*
1219412228
** Routines to read and write variable-length integers. These used to
1219512229
** be defined locally, but now we use the varint routines in the util.c
1219612230
** file. Code should use the MACRO forms below, as the Varint32 versions
@@ -12303,11 +12337,11 @@
1230312337
SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
1230412338
SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
1230512339
SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
1230612340
SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
1230712341
SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
12308
-SQLITE_PRIVATE char sqlite3AffinityType(const char*);
12342
+SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
1230912343
SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
1231012344
SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
1231112345
SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
1231212346
SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
1231312347
SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
@@ -22388,10 +22422,87 @@
2238822422
for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
2238922423
if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
2239022424
}
2239122425
}
2239222426
#endif
22427
+
22428
+/*
22429
+** Find (an approximate) sum of two LogEst values. This computation is
22430
+** not a simple "+" operator because LogEst is stored as a logarithmic
22431
+** value.
22432
+**
22433
+*/
22434
+SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
22435
+ static const unsigned char x[] = {
22436
+ 10, 10, /* 0,1 */
22437
+ 9, 9, /* 2,3 */
22438
+ 8, 8, /* 4,5 */
22439
+ 7, 7, 7, /* 6,7,8 */
22440
+ 6, 6, 6, /* 9,10,11 */
22441
+ 5, 5, 5, /* 12-14 */
22442
+ 4, 4, 4, 4, /* 15-18 */
22443
+ 3, 3, 3, 3, 3, 3, /* 19-24 */
22444
+ 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
22445
+ };
22446
+ if( a>=b ){
22447
+ if( a>b+49 ) return a;
22448
+ if( a>b+31 ) return a+1;
22449
+ return a+x[a-b];
22450
+ }else{
22451
+ if( b>a+49 ) return b;
22452
+ if( b>a+31 ) return b+1;
22453
+ return b+x[b-a];
22454
+ }
22455
+}
22456
+
22457
+/*
22458
+** Convert an integer into a LogEst. In other words, compute a
22459
+** good approximatation for 10*log2(x).
22460
+*/
22461
+SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
22462
+ static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
22463
+ LogEst y = 40;
22464
+ if( x<8 ){
22465
+ if( x<2 ) return 0;
22466
+ while( x<8 ){ y -= 10; x <<= 1; }
22467
+ }else{
22468
+ while( x>255 ){ y += 40; x >>= 4; }
22469
+ while( x>15 ){ y += 10; x >>= 1; }
22470
+ }
22471
+ return a[x&7] + y - 10;
22472
+}
22473
+
22474
+#ifndef SQLITE_OMIT_VIRTUALTABLE
22475
+/*
22476
+** Convert a double into a LogEst
22477
+** In other words, compute an approximation for 10*log2(x).
22478
+*/
22479
+SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
22480
+ u64 a;
22481
+ LogEst e;
22482
+ assert( sizeof(x)==8 && sizeof(a)==8 );
22483
+ if( x<=1 ) return 0;
22484
+ if( x<=2000000000 ) return sqlite3LogEst((u64)x);
22485
+ memcpy(&a, &x, 8);
22486
+ e = (a>>52) - 1022;
22487
+ return e*10;
22488
+}
22489
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
22490
+
22491
+/*
22492
+** Convert a LogEst into an integer.
22493
+*/
22494
+SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
22495
+ u64 n;
22496
+ if( x<10 ) return 1;
22497
+ n = x%10;
22498
+ x /= 10;
22499
+ if( n>=5 ) n -= 2;
22500
+ else if( n>=1 ) n -= 1;
22501
+ if( x>=3 ) return (n+8)<<(x-3);
22502
+ return (n+8)>>(3-x);
22503
+}
2239322504
2239422505
/************** End of util.c ************************************************/
2239522506
/************** Begin file hash.c ********************************************/
2239622507
/*
2239722508
** 2001 September 22
@@ -76084,11 +76195,11 @@
7608476195
return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
7608576196
}
7608676197
#ifndef SQLITE_OMIT_CAST
7608776198
if( op==TK_CAST ){
7608876199
assert( !ExprHasProperty(pExpr, EP_IntValue) );
76089
- return sqlite3AffinityType(pExpr->u.zToken);
76200
+ return sqlite3AffinityType(pExpr->u.zToken, 0);
7609076201
}
7609176202
#endif
7609276203
if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
7609376204
&& pExpr->pTab!=0
7609476205
){
@@ -78504,11 +78615,11 @@
7850478615
case TK_CAST: {
7850578616
/* Expressions of the form: CAST(pLeft AS token) */
7850678617
int aff, to_op;
7850778618
inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
7850878619
assert( !ExprHasProperty(pExpr, EP_IntValue) );
78509
- aff = sqlite3AffinityType(pExpr->u.zToken);
78620
+ aff = sqlite3AffinityType(pExpr->u.zToken, 0);
7851078621
to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
7851178622
assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
7851278623
assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
7851378624
assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
7851478625
assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
@@ -79171,11 +79282,11 @@
7917179282
}
7917279283
#ifndef SQLITE_OMIT_CAST
7917379284
case TK_CAST: {
7917479285
/* Expressions of the form: CAST(pLeft AS token) */
7917579286
const char *zAff = "unk";
79176
- switch( sqlite3AffinityType(pExpr->u.zToken) ){
79287
+ switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){
7917779288
case SQLITE_AFF_TEXT: zAff = "TEXT"; break;
7917879289
case SQLITE_AFF_NONE: zAff = "NONE"; break;
7917979290
case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break;
7918079291
case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break;
7918179292
case SQLITE_AFF_REAL: zAff = "REAL"; break;
@@ -81886,16 +81997,16 @@
8188681997
if( zRet==0 ){
8188781998
sqlite3_result_error_nomem(context);
8188881999
return;
8188982000
}
8189082001
81891
- sqlite3_snprintf(24, zRet, "%lld", p->nRow);
82002
+ sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
8189282003
z = zRet + sqlite3Strlen30(zRet);
8189382004
for(i=0; i<(p->nCol-1); i++){
81894
- i64 nDistinct = p->current.anDLt[i] + 1;
81895
- i64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
81896
- sqlite3_snprintf(24, z, " %lld", iVal);
82005
+ u64 nDistinct = p->current.anDLt[i] + 1;
82006
+ u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
82007
+ sqlite3_snprintf(24, z, " %llu", iVal);
8189782008
z += sqlite3Strlen30(z);
8189882009
assert( p->current.anEq[i] );
8189982010
}
8190082011
assert( z[0]=='\0' && z>zRet );
8190182012
@@ -81932,11 +82043,11 @@
8193282043
sqlite3_result_error_nomem(context);
8193382044
}else{
8193482045
int i;
8193582046
char *z = zRet;
8193682047
for(i=0; i<p->nCol; i++){
81937
- sqlite3_snprintf(24, z, "%lld ", aCnt[i]);
82048
+ sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
8193882049
z += sqlite3Strlen30(z);
8193982050
}
8194082051
assert( z[0]=='\0' && z>zRet );
8194182052
z[-1] = '\0';
8194282053
sqlite3_result_text(context, zRet, -1, sqlite3_free);
@@ -82393,22 +82504,20 @@
8239382504
** The first argument points to a nul-terminated string containing a
8239482505
** list of space separated integers. Read the first nOut of these into
8239582506
** the array aOut[].
8239682507
*/
8239782508
static void decodeIntArray(
82398
- char *zIntArray,
82399
- int nOut,
82400
- tRowcnt *aOut,
82401
- int *pbUnordered
82509
+ char *zIntArray, /* String containing int array to decode */
82510
+ int nOut, /* Number of slots in aOut[] */
82511
+ tRowcnt *aOut, /* Store integers here */
82512
+ Index *pIndex /* Handle extra flags for this index, if not NULL */
8240282513
){
8240382514
char *z = zIntArray;
8240482515
int c;
8240582516
int i;
8240682517
tRowcnt v;
8240782518
82408
- assert( pbUnordered==0 || *pbUnordered==0 );
82409
-
8241082519
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8241182520
if( z==0 ) z = "";
8241282521
#else
8241382522
if( NEVER(z==0) ) z = "";
8241482523
#endif
@@ -82419,12 +82528,23 @@
8241982528
z++;
8242082529
}
8242182530
aOut[i] = v;
8242282531
if( *z==' ' ) z++;
8242382532
}
82424
- if( pbUnordered && strcmp(z, "unordered")==0 ){
82425
- *pbUnordered = 1;
82533
+#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
82534
+ assert( pIndex!=0 );
82535
+#else
82536
+ if( pIndex )
82537
+#endif
82538
+ {
82539
+ if( strcmp(z, "unordered")==0 ){
82540
+ pIndex->bUnordered = 1;
82541
+ }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
82542
+ int v32 = 0;
82543
+ sqlite3GetInt32(z+3, &v32);
82544
+ pIndex->szIdxRow = sqlite3LogEst(v32);
82545
+ }
8242682546
}
8242782547
}
8242882548
8242982549
/*
8243082550
** This callback is invoked once for each index when reading the
@@ -82459,16 +82579,17 @@
8245982579
pIndex = 0;
8246082580
}
8246182581
z = argv[2];
8246282582
8246382583
if( pIndex ){
82464
- int bUnordered = 0;
82465
- decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst,&bUnordered);
82584
+ decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst, pIndex);
8246682585
if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
82467
- pIndex->bUnordered = bUnordered;
8246882586
}else{
82469
- decodeIntArray((char*)z, 1, &pTable->nRowEst, 0);
82587
+ Index fakeIdx;
82588
+ fakeIdx.szIdxRow = pTable->szTabRow;
82589
+ decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
82590
+ pTable->szTabRow = fakeIdx.szIdxRow;
8247082591
}
8247182592
8247282593
return 0;
8247382594
}
8247482595
@@ -84480,11 +84601,11 @@
8448084601
}
8448184602
pTable->zName = zName;
8448284603
pTable->iPKey = -1;
8448384604
pTable->pSchema = db->aDb[iDb].pSchema;
8448484605
pTable->nRef = 1;
84485
- pTable->nRowEst = 1000000;
84606
+ pTable->nRowEst = 1048576;
8448684607
assert( pParse->pNewTable==0 );
8448784608
pParse->pNewTable = pTable;
8448884609
8448984610
/* If this is the magic sqlite_sequence table used by autoincrement,
8449084611
** then record a pointer to this table in the main database structure
@@ -84627,10 +84748,11 @@
8462784748
/* If there is no type specified, columns have the default affinity
8462884749
** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
8462984750
** be called next to set pCol->affinity correctly.
8463084751
*/
8463184752
pCol->affinity = SQLITE_AFF_NONE;
84753
+ pCol->szEst = 1;
8463284754
p->nCol++;
8463384755
}
8463484756
8463584757
/*
8463684758
** This routine is called by the parser while in the middle of
@@ -84668,26 +84790,30 @@
8466884790
** 'DOUB' | SQLITE_AFF_REAL
8466984791
**
8467084792
** If none of the substrings in the above table are found,
8467184793
** SQLITE_AFF_NUMERIC is returned.
8467284794
*/
84673
-SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
84795
+SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
8467484796
u32 h = 0;
8467584797
char aff = SQLITE_AFF_NUMERIC;
84798
+ const char *zChar = 0;
8467684799
84677
- if( zIn ) while( zIn[0] ){
84800
+ if( zIn==0 ) return aff;
84801
+ while( zIn[0] ){
8467884802
h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
8467984803
zIn++;
8468084804
if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
84681
- aff = SQLITE_AFF_TEXT;
84805
+ aff = SQLITE_AFF_TEXT;
84806
+ zChar = zIn;
8468284807
}else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
8468384808
aff = SQLITE_AFF_TEXT;
8468484809
}else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
8468584810
aff = SQLITE_AFF_TEXT;
8468684811
}else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
8468784812
&& (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
8468884813
aff = SQLITE_AFF_NONE;
84814
+ if( zIn[0]=='(' ) zChar = zIn;
8468984815
#ifndef SQLITE_OMIT_FLOATING_POINT
8469084816
}else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
8469184817
&& aff==SQLITE_AFF_NUMERIC ){
8469284818
aff = SQLITE_AFF_REAL;
8469384819
}else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
@@ -84701,10 +84827,32 @@
8470184827
aff = SQLITE_AFF_INTEGER;
8470284828
break;
8470384829
}
8470484830
}
8470584831
84832
+ /* If pszEst is not NULL, store an estimate of the field size. The
84833
+ ** estimate is scaled so that the size of an integer is 1. */
84834
+ if( pszEst ){
84835
+ *pszEst = 1; /* default size is approx 4 bytes */
84836
+ if( aff<=SQLITE_AFF_NONE ){
84837
+ if( zChar ){
84838
+ while( zChar[0] ){
84839
+ if( sqlite3Isdigit(zChar[0]) ){
84840
+ int v;
84841
+ sqlite3GetInt32(zChar, &v);
84842
+ v = v/4 + 1;
84843
+ if( v>255 ) v = 255;
84844
+ *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
84845
+ break;
84846
+ }
84847
+ zChar++;
84848
+ }
84849
+ }else{
84850
+ *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
84851
+ }
84852
+ }
84853
+ }
8470684854
return aff;
8470784855
}
8470884856
8470984857
/*
8471084858
** This routine is called by the parser while in the middle of
@@ -84722,11 +84870,11 @@
8472284870
p = pParse->pNewTable;
8472384871
if( p==0 || NEVER(p->nCol<1) ) return;
8472484872
pCol = &p->aCol[p->nCol-1];
8472584873
assert( pCol->zType==0 );
8472684874
pCol->zType = sqlite3NameFromToken(pParse->db, pType);
84727
- pCol->affinity = sqlite3AffinityType(pCol->zType);
84875
+ pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
8472884876
}
8472984877
8473084878
/*
8473184879
** The expression is the default value for the most recently added column
8473284880
** of the table currently under construction.
@@ -85070,18 +85218,46 @@
8507085218
testcase( pCol->affinity==SQLITE_AFF_REAL );
8507185219
8507285220
zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
8507385221
len = sqlite3Strlen30(zType);
8507485222
assert( pCol->affinity==SQLITE_AFF_NONE
85075
- || pCol->affinity==sqlite3AffinityType(zType) );
85223
+ || pCol->affinity==sqlite3AffinityType(zType, 0) );
8507685224
memcpy(&zStmt[k], zType, len);
8507785225
k += len;
8507885226
assert( k<=n );
8507985227
}
8508085228
sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
8508185229
return zStmt;
8508285230
}
85231
+
85232
+/*
85233
+** Estimate the total row width for a table.
85234
+*/
85235
+static void estimateTableWidth(Table *pTab){
85236
+ unsigned wTable = 0;
85237
+ const Column *pTabCol;
85238
+ int i;
85239
+ for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
85240
+ wTable += pTabCol->szEst;
85241
+ }
85242
+ if( pTab->iPKey<0 ) wTable++;
85243
+ pTab->szTabRow = sqlite3LogEst(wTable*4);
85244
+}
85245
+
85246
+/*
85247
+** Estimate the average size of a row for an index.
85248
+*/
85249
+static void estimateIndexWidth(Index *pIdx){
85250
+ unsigned wIndex = 1;
85251
+ int i;
85252
+ const Column *aCol = pIdx->pTable->aCol;
85253
+ for(i=0; i<pIdx->nColumn; i++){
85254
+ assert( pIdx->aiColumn[i]>=0 && pIdx->aiColumn[i]<pIdx->pTable->nCol );
85255
+ wIndex += aCol[pIdx->aiColumn[i]].szEst;
85256
+ }
85257
+ pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
85258
+}
8508385259
8508485260
/*
8508585261
** This routine is called to report the final ")" that terminates
8508685262
** a CREATE TABLE statement.
8508785263
**
@@ -85105,13 +85281,14 @@
8510585281
Parse *pParse, /* Parse context */
8510685282
Token *pCons, /* The ',' token after the last column defn. */
8510785283
Token *pEnd, /* The final ')' token in the CREATE TABLE */
8510885284
Select *pSelect /* Select from a "CREATE ... AS SELECT" */
8510985285
){
85110
- Table *p;
85111
- sqlite3 *db = pParse->db;
85112
- int iDb;
85286
+ Table *p; /* The new table */
85287
+ sqlite3 *db = pParse->db; /* The database connection */
85288
+ int iDb; /* Database in which the table lives */
85289
+ Index *pIdx; /* An implied index of the table */
8511385290
8511485291
if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
8511585292
return;
8511685293
}
8511785294
p = pParse->pNewTable;
@@ -85126,10 +85303,16 @@
8512685303
*/
8512785304
if( p->pCheck ){
8512885305
sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
8512985306
}
8513085307
#endif /* !defined(SQLITE_OMIT_CHECK) */
85308
+
85309
+ /* Estimate the average row size for the table and for all implied indices */
85310
+ estimateTableWidth(p);
85311
+ for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
85312
+ estimateIndexWidth(pIdx);
85313
+ }
8513185314
8513285315
/* If the db->init.busy is 1 it means we are reading the SQL off the
8513385316
** "sqlite_master" or "sqlite_temp_master" table on the disk.
8513485317
** So do not write to the disk again. Extract the root page number
8513585318
** for the table from the db->init.newTnum field. (The page number
@@ -86085,13 +86268,14 @@
8608586268
sqlite3 *db = pParse->db;
8608686269
Db *pDb; /* The specific table containing the indexed database */
8608786270
int iDb; /* Index of the database that is being written */
8608886271
Token *pName = 0; /* Unqualified name of the index to create */
8608986272
struct ExprList_item *pListItem; /* For looping over pList */
86090
- int nCol;
86091
- int nExtra = 0;
86092
- char *zExtra;
86273
+ const Column *pTabCol; /* A column in the table */
86274
+ int nCol; /* Number of columns */
86275
+ int nExtra = 0; /* Space allocated for zExtra[] */
86276
+ char *zExtra; /* Extra space after the Index object */
8609386277
8609486278
assert( pParse->nErr==0 ); /* Never called with prior errors */
8609586279
if( db->mallocFailed || IN_DECLARE_VTAB ){
8609686280
goto exit_create_index;
8609786281
}
@@ -86314,11 +86498,10 @@
8631486498
** same column more than once cannot be an error because that would
8631586499
** break backwards compatibility - it needs to be a warning.
8631686500
*/
8631786501
for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
8631886502
const char *zColName = pListItem->zName;
86319
- Column *pTabCol;
8632086503
int requestedSortOrder;
8632186504
char *zColl; /* Collation sequence name */
8632286505
8632386506
for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
8632486507
if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
@@ -86351,10 +86534,11 @@
8635186534
requestedSortOrder = pListItem->sortOrder & sortOrderMask;
8635286535
pIndex->aSortOrder[i] = (u8)requestedSortOrder;
8635386536
if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
8635486537
}
8635586538
sqlite3DefaultRowEst(pIndex);
86539
+ if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
8635686540
8635786541
if( pTab==pParse->pNewTable ){
8635886542
/* This routine has been called to create an automatic index as a
8635986543
** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
8636086544
** a PRIMARY KEY or UNIQUE clause following the column definitions.
@@ -96379,30 +96563,34 @@
9637996563
break;
9638096564
9638196565
case PragTyp_INDEX_LIST: if( zRight ){
9638296566
Index *pIdx;
9638396567
Table *pTab;
96568
+ int i;
9638496569
pTab = sqlite3FindTable(db, zRight, zDb);
9638596570
if( pTab ){
9638696571
v = sqlite3GetVdbe(pParse);
96387
- pIdx = pTab->pIndex;
96388
- if( pIdx ){
96389
- int i = 0;
96390
- sqlite3VdbeSetNumCols(v, 3);
96391
- pParse->nMem = 3;
96392
- sqlite3CodeVerifySchema(pParse, iDb);
96393
- sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
96394
- sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
96395
- sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
96396
- while(pIdx){
96397
- sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
96398
- sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
96399
- sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
96400
- sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
96401
- ++i;
96402
- pIdx = pIdx->pNext;
96403
- }
96572
+ sqlite3VdbeSetNumCols(v, 4);
96573
+ pParse->nMem = 4;
96574
+ sqlite3CodeVerifySchema(pParse, iDb);
96575
+ sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
96576
+ sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
96577
+ sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
96578
+ sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "avgrowsize", SQLITE_STATIC);
96579
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, 1);
96580
+ sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
96581
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, 3);
96582
+ sqlite3VdbeAddOp2(v, OP_Integer,
96583
+ (int)sqlite3LogEstToInt(pTab->szTabRow), 4);
96584
+ sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
96585
+ for(pIdx=pTab->pIndex, i=1; pIdx; pIdx=pIdx->pNext, i++){
96586
+ sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
96587
+ sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
96588
+ sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
96589
+ sqlite3VdbeAddOp2(v, OP_Integer,
96590
+ (int)sqlite3LogEstToInt(pIdx->szIdxRow), 4);
96591
+ sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
9640496592
}
9640596593
}
9640696594
}
9640796595
break;
9640896596
@@ -99086,10 +99274,13 @@
9908699274
}
9908799275
9908899276
/*
9908999277
** Return a pointer to a string containing the 'declaration type' of the
9909099278
** expression pExpr. The string may be treated as static by the caller.
99279
+**
99280
+** Also try to estimate the size of the returned value and return that
99281
+** result in *pEstWidth.
9909199282
**
9909299283
** The declaration type is the exact datatype definition extracted from the
9909399284
** original CREATE TABLE statement if the expression is a column. The
9909499285
** declaration type for a ROWID field is INTEGER. Exactly when an expression
9909599286
** is considered a column can be complex in the presence of subqueries. The
@@ -99100,25 +99291,40 @@
9910099291
** SELECT (SELECT col FROM tbl;
9910199292
** SELECT (SELECT col FROM tbl);
9910299293
** SELECT abc FROM (SELECT col AS abc FROM tbl);
9910399294
**
9910499295
** The declaration type for any expression other than a column is NULL.
99296
+**
99297
+** This routine has either 3 or 6 parameters depending on whether or not
99298
+** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
9910599299
*/
99106
-static const char *columnType(
99300
+#ifdef SQLITE_ENABLE_COLUMN_METADATA
99301
+# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
99302
+static const char *columnTypeImpl(
99303
+ NameContext *pNC,
99304
+ Expr *pExpr,
99305
+ const char **pzOrigDb,
99306
+ const char **pzOrigTab,
99307
+ const char **pzOrigCol,
99308
+ u8 *pEstWidth
99309
+){
99310
+ char const *zOrigDb = 0;
99311
+ char const *zOrigTab = 0;
99312
+ char const *zOrigCol = 0;
99313
+#else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
99314
+# define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
99315
+static const char *columnTypeImpl(
9910799316
NameContext *pNC,
9910899317
Expr *pExpr,
99109
- const char **pzOriginDb,
99110
- const char **pzOriginTab,
99111
- const char **pzOriginCol
99318
+ u8 *pEstWidth
9911299319
){
99320
+#endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
9911399321
char const *zType = 0;
99114
- char const *zOriginDb = 0;
99115
- char const *zOriginTab = 0;
99116
- char const *zOriginCol = 0;
9911799322
int j;
99323
+ u8 estWidth = 1;
99324
+
9911899325
if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
99119
-
9912099326
switch( pExpr->op ){
9912199327
case TK_AGG_COLUMN:
9912299328
case TK_COLUMN: {
9912399329
/* The expression is a column. Locate the table the column is being
9912499330
** extracted from in NameContext.pSrcList. This table may be real
@@ -99175,29 +99381,39 @@
9917599381
NameContext sNC;
9917699382
Expr *p = pS->pEList->a[iCol].pExpr;
9917799383
sNC.pSrcList = pS->pSrc;
9917899384
sNC.pNext = pNC;
9917999385
sNC.pParse = pNC->pParse;
99180
- zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
99386
+ zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth);
9918199387
}
9918299388
}else if( ALWAYS(pTab->pSchema) ){
9918399389
/* A real table */
9918499390
assert( !pS );
9918599391
if( iCol<0 ) iCol = pTab->iPKey;
9918699392
assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
99393
+#ifdef SQLITE_ENABLE_COLUMN_METADATA
9918799394
if( iCol<0 ){
9918899395
zType = "INTEGER";
99189
- zOriginCol = "rowid";
99396
+ zOrigCol = "rowid";
9919099397
}else{
9919199398
zType = pTab->aCol[iCol].zType;
99192
- zOriginCol = pTab->aCol[iCol].zName;
99399
+ zOrigCol = pTab->aCol[iCol].zName;
99400
+ estWidth = pTab->aCol[iCol].szEst;
9919399401
}
99194
- zOriginTab = pTab->zName;
99402
+ zOrigTab = pTab->zName;
9919599403
if( pNC->pParse ){
9919699404
int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
99197
- zOriginDb = pNC->pParse->db->aDb[iDb].zName;
99405
+ zOrigDb = pNC->pParse->db->aDb[iDb].zName;
9919899406
}
99407
+#else
99408
+ if( iCol<0 ){
99409
+ zType = "INTEGER";
99410
+ }else{
99411
+ zType = pTab->aCol[iCol].zType;
99412
+ estWidth = pTab->aCol[iCol].szEst;
99413
+ }
99414
+#endif
9919999415
}
9920099416
break;
9920199417
}
9920299418
#ifndef SQLITE_OMIT_SUBQUERY
9920399419
case TK_SELECT: {
@@ -99210,22 +99426,25 @@
9921099426
Expr *p = pS->pEList->a[0].pExpr;
9921199427
assert( ExprHasProperty(pExpr, EP_xIsSelect) );
9921299428
sNC.pSrcList = pS->pSrc;
9921399429
sNC.pNext = pNC;
9921499430
sNC.pParse = pNC->pParse;
99215
- zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
99431
+ zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth);
9921699432
break;
9921799433
}
9921899434
#endif
9921999435
}
99220
-
99221
- if( pzOriginDb ){
99222
- assert( pzOriginTab && pzOriginCol );
99223
- *pzOriginDb = zOriginDb;
99224
- *pzOriginTab = zOriginTab;
99225
- *pzOriginCol = zOriginCol;
99436
+
99437
+#ifdef SQLITE_ENABLE_COLUMN_METADATA
99438
+ if( pzOrigDb ){
99439
+ assert( pzOrigTab && pzOrigCol );
99440
+ *pzOrigDb = zOrigDb;
99441
+ *pzOrigTab = zOrigTab;
99442
+ *pzOrigCol = zOrigCol;
9922699443
}
99444
+#endif
99445
+ if( pEstWidth ) *pEstWidth = estWidth;
9922799446
return zType;
9922899447
}
9922999448
9923099449
/*
9923199450
** Generate code that will tell the VDBE the declaration types of columns
@@ -99247,25 +99466,25 @@
9924799466
const char *zType;
9924899467
#ifdef SQLITE_ENABLE_COLUMN_METADATA
9924999468
const char *zOrigDb = 0;
9925099469
const char *zOrigTab = 0;
9925199470
const char *zOrigCol = 0;
99252
- zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
99471
+ zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
9925399472
9925499473
/* The vdbe must make its own copy of the column-type and other
9925599474
** column specific strings, in case the schema is reset before this
9925699475
** virtual machine is deleted.
9925799476
*/
9925899477
sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
9925999478
sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
9926099479
sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
9926199480
#else
99262
- zType = columnType(&sNC, p, 0, 0, 0);
99481
+ zType = columnType(&sNC, p, 0, 0, 0, 0);
9926399482
#endif
9926499483
sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
9926599484
}
99266
-#endif /* SQLITE_OMIT_DECLTYPE */
99485
+#endif /* !defined(SQLITE_OMIT_DECLTYPE) */
9926799486
}
9926899487
9926999488
/*
9927099489
** Generate code that will tell the VDBE the names of columns
9927199490
** in the result set. This information is used to provide the
@@ -99450,39 +99669,41 @@
9945099669
** This routine requires that all identifiers in the SELECT
9945199670
** statement be resolved.
9945299671
*/
9945399672
static void selectAddColumnTypeAndCollation(
9945499673
Parse *pParse, /* Parsing contexts */
99455
- int nCol, /* Number of columns */
99456
- Column *aCol, /* List of columns */
99674
+ Table *pTab, /* Add column type information to this table */
9945799675
Select *pSelect /* SELECT used to determine types and collations */
9945899676
){
9945999677
sqlite3 *db = pParse->db;
9946099678
NameContext sNC;
9946199679
Column *pCol;
9946299680
CollSeq *pColl;
9946399681
int i;
9946499682
Expr *p;
9946599683
struct ExprList_item *a;
99684
+ u64 szAll = 0;
9946699685
9946799686
assert( pSelect!=0 );
9946899687
assert( (pSelect->selFlags & SF_Resolved)!=0 );
99469
- assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
99688
+ assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
9947099689
if( db->mallocFailed ) return;
9947199690
memset(&sNC, 0, sizeof(sNC));
9947299691
sNC.pSrcList = pSelect->pSrc;
9947399692
a = pSelect->pEList->a;
99474
- for(i=0, pCol=aCol; i<nCol; i++, pCol++){
99693
+ for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
9947599694
p = a[i].pExpr;
99476
- pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
99695
+ pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
99696
+ szAll += pCol->szEst;
9947799697
pCol->affinity = sqlite3ExprAffinity(p);
9947899698
if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
9947999699
pColl = sqlite3ExprCollSeq(pParse, p);
9948099700
if( pColl ){
9948199701
pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
9948299702
}
9948399703
}
99704
+ pTab->szTabRow = sqlite3LogEst(szAll*4);
9948499705
}
9948599706
9948699707
/*
9948799708
** Given a SELECT statement, generate a Table structure that describes
9948899709
** the result set of that SELECT.
@@ -99506,13 +99727,13 @@
9950699727
/* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
9950799728
** is disabled */
9950899729
assert( db->lookaside.bEnabled==0 );
9950999730
pTab->nRef = 1;
9951099731
pTab->zName = 0;
99511
- pTab->nRowEst = 1000000;
99732
+ pTab->nRowEst = 1048576;
9951299733
selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
99513
- selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
99734
+ selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
9951499735
pTab->iPKey = -1;
9951599736
if( db->mallocFailed ){
9951699737
sqlite3DeleteTable(db, pTab);
9951799738
return 0;
9951899739
}
@@ -101420,15 +101641,15 @@
101420101641
assert( pFrom->pTab==0 );
101421101642
sqlite3WalkSelect(pWalker, pSel);
101422101643
pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
101423101644
if( pTab==0 ) return WRC_Abort;
101424101645
pTab->nRef = 1;
101425
- pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
101646
+ pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
101426101647
while( pSel->pPrior ){ pSel = pSel->pPrior; }
101427101648
selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
101428101649
pTab->iPKey = -1;
101429
- pTab->nRowEst = 1000000;
101650
+ pTab->nRowEst = 1048576;
101430101651
pTab->tabFlags |= TF_Ephemeral;
101431101652
#endif
101432101653
}else{
101433101654
/* An ordinary table or view name in the FROM clause */
101434101655
assert( pFrom->pTab==0 );
@@ -101708,11 +101929,11 @@
101708101929
if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
101709101930
/* A sub-query in the FROM clause of a SELECT */
101710101931
Select *pSel = pFrom->pSelect;
101711101932
assert( pSel );
101712101933
while( pSel->pPrior ) pSel = pSel->pPrior;
101713
- selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
101934
+ selectAddColumnTypeAndCollation(pParse, pTab, pSel);
101714101935
}
101715101936
}
101716101937
}
101717101938
return WRC_Continue;
101718101939
}
@@ -102623,17 +102844,11 @@
102623102844
int iRoot = pTab->tnum; /* Root page of scanned b-tree */
102624102845
102625102846
sqlite3CodeVerifySchema(pParse, iDb);
102626102847
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
102627102848
102628
- /* Search for the index that has the least amount of columns. If
102629
- ** there is such an index, and it has less columns than the table
102630
- ** does, then we can assume that it consumes less space on disk and
102631
- ** will therefore be cheaper to scan to determine the query result.
102632
- ** In this case set iRoot to the root page number of the index b-tree
102633
- ** and pKeyInfo to the KeyInfo structure required to navigate the
102634
- ** index.
102849
+ /* Search for the index that has the lowest scan cost.
102635102850
**
102636102851
** (2011-04-15) Do not do a full scan of an unordered index.
102637102852
**
102638102853
** (2013-10-03) Do not count the entires in a partial index.
102639102854
**
@@ -102640,17 +102855,18 @@
102640102855
** In practice the KeyInfo structure will not be used. It is only
102641102856
** passed to keep OP_OpenRead happy.
102642102857
*/
102643102858
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102644102859
if( pIdx->bUnordered==0
102860
+ && pIdx->szIdxRow<pTab->szTabRow
102645102861
&& pIdx->pPartIdxWhere==0
102646
- && (!pBest || pIdx->nColumn<pBest->nColumn)
102862
+ && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
102647102863
){
102648102864
pBest = pIdx;
102649102865
}
102650102866
}
102651
- if( pBest && pBest->nColumn<pTab->nCol ){
102867
+ if( pBest ){
102652102868
iRoot = pBest->tnum;
102653102869
pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
102654102870
}
102655102871
102656102872
/* Open a read-only cursor, execute the OP_Count, close the cursor. */
@@ -106406,30 +106622,10 @@
106406106622
typedef struct WhereLoopBuilder WhereLoopBuilder;
106407106623
typedef struct WhereScan WhereScan;
106408106624
typedef struct WhereOrCost WhereOrCost;
106409106625
typedef struct WhereOrSet WhereOrSet;
106410106626
106411
-/*
106412
-** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The
106413
-** maximum cost for ordinary tables is 64*(2**63) which becomes 6900.
106414
-** (Virtual tables can return a larger cost, but let's assume they do not.)
106415
-** So all costs can be stored in a 16-bit integer without risk
106416
-** of overflow.
106417
-**
106418
-** Costs are estimates, so no effort is made to compute 10*log2(X) exactly.
106419
-** Instead, a close estimate is used. Any value of X=1 is stored as 0.
106420
-** X=2 is 10. X=3 is 16. X=1000 is 99. etc. Negative values are allowed.
106421
-** A WhereCost of -10 means 0.5. WhereCost of -20 means 0.25. And so forth.
106422
-**
106423
-** The tool/wherecosttest.c source file implements a command-line program
106424
-** that will convert WhereCosts to integers, convert integers to WhereCosts
106425
-** and do addition and multiplication on WhereCost values. The wherecosttest
106426
-** command-line program is a useful utility to have around when working with
106427
-** this module.
106428
-*/
106429
-typedef short int WhereCost;
106430
-
106431106627
/*
106432106628
** This object contains information needed to implement a single nested
106433106629
** loop in WHERE clause.
106434106630
**
106435106631
** Contrast this object with WhereLoop. This object describes the
@@ -106490,13 +106686,13 @@
106490106686
#ifdef SQLITE_DEBUG
106491106687
char cId; /* Symbolic ID of this loop for debugging use */
106492106688
#endif
106493106689
u8 iTab; /* Position in FROM clause of table for this loop */
106494106690
u8 iSortIdx; /* Sorting index number. 0==None */
106495
- WhereCost rSetup; /* One-time setup cost (ex: create transient index) */
106496
- WhereCost rRun; /* Cost of running each loop */
106497
- WhereCost nOut; /* Estimated number of output rows */
106691
+ LogEst rSetup; /* One-time setup cost (ex: create transient index) */
106692
+ LogEst rRun; /* Cost of running each loop */
106693
+ LogEst nOut; /* Estimated number of output rows */
106498106694
union {
106499106695
struct { /* Information for internal btree tables */
106500106696
int nEq; /* Number of equality constraints */
106501106697
Index *pIndex; /* Index used, or NULL */
106502106698
} btree;
@@ -106522,12 +106718,12 @@
106522106718
** subquery on one operand of an OR operator in the WHERE clause.
106523106719
** See WhereOrSet for additional information
106524106720
*/
106525106721
struct WhereOrCost {
106526106722
Bitmask prereq; /* Prerequisites */
106527
- WhereCost rRun; /* Cost of running this subquery */
106528
- WhereCost nOut; /* Number of outputs for this subquery */
106723
+ LogEst rRun; /* Cost of running this subquery */
106724
+ LogEst nOut; /* Number of outputs for this subquery */
106529106725
};
106530106726
106531106727
/* The WhereOrSet object holds a set of possible WhereOrCosts that
106532106728
** correspond to the subquery(s) of OR-clause processing. Only the
106533106729
** best N_OR_COST elements are retained.
@@ -106561,12 +106757,12 @@
106561106757
** at the end is the choosen query plan.
106562106758
*/
106563106759
struct WherePath {
106564106760
Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
106565106761
Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
106566
- WhereCost nRow; /* Estimated number of rows generated by this path */
106567
- WhereCost rCost; /* Total cost of this path */
106762
+ LogEst nRow; /* Estimated number of rows generated by this path */
106763
+ LogEst rCost; /* Total cost of this path */
106568106764
u8 isOrdered; /* True if this path satisfies ORDER BY */
106569106765
u8 isOrderedValid; /* True if the isOrdered field is valid */
106570106766
WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
106571106767
};
106572106768
@@ -106628,11 +106824,11 @@
106628106824
union {
106629106825
int leftColumn; /* Column number of X in "X <op> <expr>" */
106630106826
WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
106631106827
WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
106632106828
} u;
106633
- WhereCost truthProb; /* Probability of truth for this expression */
106829
+ LogEst truthProb; /* Probability of truth for this expression */
106634106830
u16 eOperator; /* A WO_xx value describing <op> */
106635106831
u8 wtFlags; /* TERM_xxx bit flags. See below */
106636106832
u8 nChild; /* Number of children that must disable us */
106637106833
WhereClause *pWC; /* The clause this term is part of */
106638106834
Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
@@ -106776,11 +106972,11 @@
106776106972
SrcList *pTabList; /* List of tables in the join */
106777106973
ExprList *pOrderBy; /* The ORDER BY clause or NULL */
106778106974
ExprList *pResultSet; /* Result set. DISTINCT operates on these */
106779106975
WhereLoop *pLoops; /* List of all WhereLoop objects */
106780106976
Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
106781
- WhereCost nRowOut; /* Estimated number of output rows */
106977
+ LogEst nRowOut; /* Estimated number of output rows */
106782106978
u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
106783106979
u8 bOBSat; /* ORDER BY satisfied by indices */
106784106980
u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
106785106981
u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
106786106982
u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
@@ -106836,30 +107032,15 @@
106836107032
#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
106837107033
#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
106838107034
#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
106839107035
#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
106840107036
106841
-
106842
-/* Convert a WhereCost value (10 times log2(X)) into its integer value X.
106843
-** A rough approximation is used. The value returned is not exact.
106844
-*/
106845
-static u64 whereCostToInt(WhereCost x){
106846
- u64 n;
106847
- if( x<10 ) return 1;
106848
- n = x%10;
106849
- x /= 10;
106850
- if( n>=5 ) n -= 2;
106851
- else if( n>=1 ) n -= 1;
106852
- if( x>=3 ) return (n+8)<<(x-3);
106853
- return (n+8)>>(3-x);
106854
-}
106855
-
106856107037
/*
106857107038
** Return the estimated number of output rows from a WHERE clause
106858107039
*/
106859107040
SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
106860
- return whereCostToInt(pWInfo->nRowOut);
107041
+ return sqlite3LogEstToInt(pWInfo->nRowOut);
106861107042
}
106862107043
106863107044
/*
106864107045
** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
106865107046
** WHERE clause returns outputs for DISTINCT processing.
@@ -106917,12 +107098,12 @@
106917107098
** so that pSet keeps the N_OR_COST best entries seen so far.
106918107099
*/
106919107100
static int whereOrInsert(
106920107101
WhereOrSet *pSet, /* The WhereOrSet to be updated */
106921107102
Bitmask prereq, /* Prerequisites of the new entry */
106922
- WhereCost rRun, /* Run-cost of the new entry */
106923
- WhereCost nOut /* Number of outputs for the new entry */
107103
+ LogEst rRun, /* Run-cost of the new entry */
107104
+ LogEst nOut /* Number of outputs for the new entry */
106924107105
){
106925107106
u16 i;
106926107107
WhereOrCost *p;
106927107108
for(i=pSet->n, p=pSet->a; i>0; i--, p++){
106928107109
if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
@@ -107003,13 +107184,10 @@
107003107184
if( pWC->a!=pWC->aStatic ){
107004107185
sqlite3DbFree(db, pWC->a);
107005107186
}
107006107187
}
107007107188
107008
-/* Forward declaration */
107009
-static WhereCost whereCost(tRowcnt x);
107010
-
107011107189
/*
107012107190
** Add a single new WhereTerm entry to the WhereClause object pWC.
107013107191
** The new WhereTerm object is constructed from Expr p and with wtFlags.
107014107192
** The index in pWC->a[] of the new WhereTerm is returned on success.
107015107193
** 0 is returned if the new WhereTerm could not be added due to a memory
@@ -107048,11 +107226,11 @@
107048107226
}
107049107227
pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
107050107228
}
107051107229
pTerm = &pWC->a[idx = pWC->nTerm++];
107052107230
if( p && ExprHasProperty(p, EP_Unlikely) ){
107053
- pTerm->truthProb = whereCost(p->iTable) - 99;
107231
+ pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
107054107232
}else{
107055107233
pTerm->truthProb = -1;
107056107234
}
107057107235
pTerm->pExpr = sqlite3ExprSkipCollate(p);
107058107236
pTerm->wtFlags = wtFlags;
@@ -108312,79 +108490,16 @@
108312108490
}
108313108491
108314108492
return 0;
108315108493
}
108316108494
108317
-/*
108318
-** Find (an approximate) sum of two WhereCosts. This computation is
108319
-** not a simple "+" operator because WhereCost is stored as a logarithmic
108320
-** value.
108321
-**
108322
-*/
108323
-static WhereCost whereCostAdd(WhereCost a, WhereCost b){
108324
- static const unsigned char x[] = {
108325
- 10, 10, /* 0,1 */
108326
- 9, 9, /* 2,3 */
108327
- 8, 8, /* 4,5 */
108328
- 7, 7, 7, /* 6,7,8 */
108329
- 6, 6, 6, /* 9,10,11 */
108330
- 5, 5, 5, /* 12-14 */
108331
- 4, 4, 4, 4, /* 15-18 */
108332
- 3, 3, 3, 3, 3, 3, /* 19-24 */
108333
- 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
108334
- };
108335
- if( a>=b ){
108336
- if( a>b+49 ) return a;
108337
- if( a>b+31 ) return a+1;
108338
- return a+x[a-b];
108339
- }else{
108340
- if( b>a+49 ) return b;
108341
- if( b>a+31 ) return b+1;
108342
- return b+x[b-a];
108343
- }
108344
-}
108345
-
108346
-/*
108347
-** Convert an integer into a WhereCost. In other words, compute a
108348
-** good approximatation for 10*log2(x).
108349
-*/
108350
-static WhereCost whereCost(tRowcnt x){
108351
- static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
108352
- WhereCost y = 40;
108353
- if( x<8 ){
108354
- if( x<2 ) return 0;
108355
- while( x<8 ){ y -= 10; x <<= 1; }
108356
- }else{
108357
- while( x>255 ){ y += 40; x >>= 4; }
108358
- while( x>15 ){ y += 10; x >>= 1; }
108359
- }
108360
- return a[x&7] + y - 10;
108361
-}
108362
-
108363
-#ifndef SQLITE_OMIT_VIRTUALTABLE
108364
-/*
108365
-** Convert a double (as received from xBestIndex of a virtual table)
108366
-** into a WhereCost. In other words, compute an approximation for
108367
-** 10*log2(x).
108368
-*/
108369
-static WhereCost whereCostFromDouble(double x){
108370
- u64 a;
108371
- WhereCost e;
108372
- assert( sizeof(x)==8 && sizeof(a)==8 );
108373
- if( x<=1 ) return 0;
108374
- if( x<=2000000000 ) return whereCost((tRowcnt)x);
108375
- memcpy(&a, &x, 8);
108376
- e = (a>>52) - 1022;
108377
- return e*10;
108378
-}
108379
-#endif /* SQLITE_OMIT_VIRTUALTABLE */
108380108495
108381108496
/*
108382108497
** Estimate the logarithm of the input value to base 2.
108383108498
*/
108384
-static WhereCost estLog(WhereCost N){
108385
- WhereCost x = whereCost(N);
108499
+static LogEst estLog(LogEst N){
108500
+ LogEst x = sqlite3LogEst(N);
108386108501
return x>33 ? x - 33 : 0;
108387108502
}
108388108503
108389108504
/*
108390108505
** Two routines for printing the content of an sqlite3_index_info
@@ -108893,11 +109008,11 @@
108893109008
**
108894109009
** ... FROM t1 WHERE a > ? AND a < ? ...
108895109010
**
108896109011
** then nEq is set to 0.
108897109012
**
108898
-** When this function is called, *pnOut is set to the whereCost() of the
109013
+** When this function is called, *pnOut is set to the sqlite3LogEst() of the
108899109014
** number of rows that the index scan is expected to visit without
108900109015
** considering the range constraints. If nEq is 0, this is the number of
108901109016
** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
108902109017
** to account for the range contraints pLower and pUpper.
108903109018
**
@@ -108909,19 +109024,19 @@
108909109024
static int whereRangeScanEst(
108910109025
Parse *pParse, /* Parsing & code generating context */
108911109026
WhereLoopBuilder *pBuilder,
108912109027
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
108913109028
WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
108914
- WhereCost *pnOut /* IN/OUT: Number of rows visited */
109029
+ WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
108915109030
){
108916109031
int rc = SQLITE_OK;
108917
- int nOut = (int)*pnOut;
108918
- WhereCost nNew;
109032
+ int nOut = pLoop->nOut;
109033
+ int nEq = pLoop->u.btree.nEq;
109034
+ LogEst nNew;
108919109035
108920109036
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108921
- Index *p = pBuilder->pNew->u.btree.pIndex;
108922
- int nEq = pBuilder->pNew->u.btree.nEq;
109037
+ Index *p = pLoop->u.btree.pIndex;
108923109038
108924109039
if( p->nSample>0
108925109040
&& nEq==pBuilder->nRecValid
108926109041
&& nEq<p->nSampleCol
108927109042
&& OptimizationEnabled(pParse->db, SQLITE_Stat3)
@@ -108998,18 +109113,18 @@
108998109113
}
108999109114
109000109115
pBuilder->pRec = pRec;
109001109116
if( rc==SQLITE_OK ){
109002109117
if( iUpper>iLower ){
109003
- nNew = whereCost(iUpper - iLower);
109118
+ nNew = sqlite3LogEst(iUpper - iLower);
109004109119
}else{
109005
- nNew = 10; assert( 10==whereCost(2) );
109120
+ nNew = 10; assert( 10==sqlite3LogEst(2) );
109006109121
}
109007109122
if( nNew<nOut ){
109008109123
nOut = nNew;
109009109124
}
109010
- *pnOut = (WhereCost)nOut;
109125
+ pLoop->nOut = (LogEst)nOut;
109011109126
WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n",
109012109127
(u32)iLower, (u32)iUpper, nOut));
109013109128
return SQLITE_OK;
109014109129
}
109015109130
}
@@ -109020,20 +109135,20 @@
109020109135
assert( pLower || pUpper );
109021109136
/* TUNING: Each inequality constraint reduces the search space 4-fold.
109022109137
** A BETWEEN operator, therefore, reduces the search space 16-fold */
109023109138
nNew = nOut;
109024109139
if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
109025
- nNew -= 20; assert( 20==whereCost(4) );
109140
+ nNew -= 20; assert( 20==sqlite3LogEst(4) );
109026109141
nOut--;
109027109142
}
109028109143
if( pUpper ){
109029
- nNew -= 20; assert( 20==whereCost(4) );
109144
+ nNew -= 20; assert( 20==sqlite3LogEst(4) );
109030109145
nOut--;
109031109146
}
109032109147
if( nNew<10 ) nNew = 10;
109033109148
if( nNew<nOut ) nOut = nNew;
109034
- *pnOut = (WhereCost)nOut;
109149
+ pLoop->nOut = (LogEst)nOut;
109035109150
return rc;
109036109151
}
109037109152
109038109153
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109039109154
/*
@@ -110673,11 +110788,11 @@
110673110788
*/
110674110789
static int whereLoopAddBtreeIndex(
110675110790
WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
110676110791
struct SrcList_item *pSrc, /* FROM clause term being analyzed */
110677110792
Index *pProbe, /* An index on pSrc */
110678
- WhereCost nInMul /* log(Number of iterations due to IN) */
110793
+ LogEst nInMul /* log(Number of iterations due to IN) */
110679110794
){
110680110795
WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
110681110796
Parse *pParse = pWInfo->pParse; /* Parsing context */
110682110797
sqlite3 *db = pParse->db; /* Database connection malloc context */
110683110798
WhereLoop *pNew; /* Template WhereLoop under construction */
@@ -110686,15 +110801,15 @@
110686110801
WhereScan scan; /* Iterator for WHERE terms */
110687110802
Bitmask saved_prereq; /* Original value of pNew->prereq */
110688110803
u16 saved_nLTerm; /* Original value of pNew->nLTerm */
110689110804
int saved_nEq; /* Original value of pNew->u.btree.nEq */
110690110805
u32 saved_wsFlags; /* Original value of pNew->wsFlags */
110691
- WhereCost saved_nOut; /* Original value of pNew->nOut */
110806
+ LogEst saved_nOut; /* Original value of pNew->nOut */
110692110807
int iCol; /* Index of the column in the table */
110693110808
int rc = SQLITE_OK; /* Return code */
110694
- WhereCost nRowEst; /* Estimated index selectivity */
110695
- WhereCost rLogSize; /* Logarithm of table size */
110809
+ LogEst nRowEst; /* Estimated index selectivity */
110810
+ LogEst rLogSize; /* Logarithm of table size */
110696110811
WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
110697110812
110698110813
pNew = pBuilder->pNew;
110699110814
if( db->mallocFailed ) return SQLITE_NOMEM;
110700110815
@@ -110710,11 +110825,11 @@
110710110825
if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
110711110826
110712110827
assert( pNew->u.btree.nEq<=pProbe->nColumn );
110713110828
if( pNew->u.btree.nEq < pProbe->nColumn ){
110714110829
iCol = pProbe->aiColumn[pNew->u.btree.nEq];
110715
- nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
110830
+ nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
110716110831
if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
110717110832
}else{
110718110833
iCol = -1;
110719110834
nRowEst = 0;
110720110835
}
@@ -110724,11 +110839,11 @@
110724110839
saved_nLTerm = pNew->nLTerm;
110725110840
saved_wsFlags = pNew->wsFlags;
110726110841
saved_prereq = pNew->prereq;
110727110842
saved_nOut = pNew->nOut;
110728110843
pNew->rSetup = 0;
110729
- rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
110844
+ rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
110730110845
for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
110731110846
int nIn = 0;
110732110847
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110733110848
int nRecValid = pBuilder->nRecValid;
110734110849
#endif
@@ -110751,14 +110866,14 @@
110751110866
if( pTerm->eOperator & WO_IN ){
110752110867
Expr *pExpr = pTerm->pExpr;
110753110868
pNew->wsFlags |= WHERE_COLUMN_IN;
110754110869
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
110755110870
/* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
110756
- nIn = 46; assert( 46==whereCost(25) );
110871
+ nIn = 46; assert( 46==sqlite3LogEst(25) );
110757110872
}else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
110758110873
/* "x IN (value, value, ...)" */
110759
- nIn = whereCost(pExpr->x.pList->nExpr);
110874
+ nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
110760110875
}
110761110876
pNew->rRun += nIn;
110762110877
pNew->u.btree.nEq++;
110763110878
pNew->nOut = nRowEst + nInMul + nIn;
110764110879
}else if( pTerm->eOperator & (WO_EQ) ){
@@ -110776,11 +110891,11 @@
110776110891
pNew->nOut = nRowEst + nInMul;
110777110892
}else if( pTerm->eOperator & (WO_ISNULL) ){
110778110893
pNew->wsFlags |= WHERE_COLUMN_NULL;
110779110894
pNew->u.btree.nEq++;
110780110895
/* TUNING: IS NULL selects 2 rows */
110781
- nIn = 10; assert( 10==whereCost(2) );
110896
+ nIn = 10; assert( 10==sqlite3LogEst(2) );
110782110897
pNew->nOut = nRowEst + nInMul + nIn;
110783110898
}else if( pTerm->eOperator & (WO_GT|WO_GE) ){
110784110899
testcase( pTerm->eOperator & WO_GT );
110785110900
testcase( pTerm->eOperator & WO_GE );
110786110901
pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
@@ -110796,11 +110911,11 @@
110796110911
pNew->aLTerm[pNew->nLTerm-2] : 0;
110797110912
}
110798110913
if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
110799110914
/* Adjust nOut and rRun for STAT3 range values */
110800110915
assert( pNew->nOut==saved_nOut );
110801
- whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut);
110916
+ whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
110802110917
}
110803110918
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110804110919
if( nInMul==0
110805110920
&& pProbe->nSample
110806110921
&& pNew->u.btree.nEq<=pProbe->nSampleCol
@@ -110816,22 +110931,22 @@
110816110931
&& !ExprHasProperty(pExpr, EP_xIsSelect) ){
110817110932
rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
110818110933
}
110819110934
assert( nOut==0 || rc==SQLITE_OK );
110820110935
if( nOut ){
110821
- nOut = whereCost(nOut);
110936
+ nOut = sqlite3LogEst(nOut);
110822110937
pNew->nOut = MIN(nOut, saved_nOut);
110823110938
}
110824110939
}
110825110940
#endif
110826110941
if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
110827110942
/* Each row involves a step of the index, then a binary search of
110828110943
** the main table */
110829
- pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10);
110944
+ pNew->rRun = sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
110830110945
}
110831110946
/* Step cost for each output row */
110832
- pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut);
110947
+ pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
110833110948
whereLoopOutputAdjust(pBuilder->pWC, pNew, pSrc->iCursor);
110834110949
rc = whereLoopInsert(pBuilder, pNew);
110835110950
if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
110836110951
&& pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
110837110952
){
@@ -110927,18 +111042,20 @@
110927111042
struct SrcList_item *pSrc; /* The FROM clause btree term to add */
110928111043
WhereLoop *pNew; /* Template WhereLoop object */
110929111044
int rc = SQLITE_OK; /* Return code */
110930111045
int iSortIdx = 1; /* Index number */
110931111046
int b; /* A boolean value */
110932
- WhereCost rSize; /* number of rows in the table */
110933
- WhereCost rLogSize; /* Logarithm of the number of rows in the table */
111047
+ LogEst rSize; /* number of rows in the table */
111048
+ LogEst rLogSize; /* Logarithm of the number of rows in the table */
110934111049
WhereClause *pWC; /* The parsed WHERE clause */
111050
+ Table *pTab; /* Table being queried */
110935111051
110936111052
pNew = pBuilder->pNew;
110937111053
pWInfo = pBuilder->pWInfo;
110938111054
pTabList = pWInfo->pTabList;
110939111055
pSrc = pTabList->a + pNew->iTab;
111056
+ pTab = pSrc->pTab;
110940111057
pWC = pBuilder->pWC;
110941111058
assert( !IsVirtual(pSrc->pTab) );
110942111059
110943111060
if( pSrc->pIndex ){
110944111061
/* An INDEXED BY clause specifies a particular index to use */
@@ -110952,22 +111069,22 @@
110952111069
memset(&sPk, 0, sizeof(Index));
110953111070
sPk.nColumn = 1;
110954111071
sPk.aiColumn = &aiColumnPk;
110955111072
sPk.aiRowEst = aiRowEstPk;
110956111073
sPk.onError = OE_Replace;
110957
- sPk.pTable = pSrc->pTab;
110958
- aiRowEstPk[0] = pSrc->pTab->nRowEst;
111074
+ sPk.pTable = pTab;
111075
+ aiRowEstPk[0] = pTab->nRowEst;
110959111076
aiRowEstPk[1] = 1;
110960111077
pFirst = pSrc->pTab->pIndex;
110961111078
if( pSrc->notIndexed==0 ){
110962111079
/* The real indices of the table are only considered if the
110963111080
** NOT INDEXED qualifier is omitted from the FROM clause */
110964111081
sPk.pNext = pFirst;
110965111082
}
110966111083
pProbe = &sPk;
110967111084
}
110968
- rSize = whereCost(pSrc->pTab->nRowEst);
111085
+ rSize = sqlite3LogEst(pTab->nRowEst);
110969111086
rLogSize = estLog(rSize);
110970111087
110971111088
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110972111089
/* Automatic indexes */
110973111090
if( !pBuilder->pOrSet
@@ -110988,17 +111105,17 @@
110988111105
pNew->nLTerm = 1;
110989111106
pNew->aLTerm[0] = pTerm;
110990111107
/* TUNING: One-time cost for computing the automatic index is
110991111108
** approximately 7*N*log2(N) where N is the number of rows in
110992111109
** the table being indexed. */
110993
- pNew->rSetup = rLogSize + rSize + 28; assert( 28==whereCost(7) );
111110
+ pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) );
110994111111
/* TUNING: Each index lookup yields 20 rows in the table. This
110995111112
** is more than the usual guess of 10 rows, since we have no way
110996111113
** of knowning how selective the index will ultimately be. It would
110997111114
** not be unreasonable to make this value much larger. */
110998
- pNew->nOut = 43; assert( 43==whereCost(20) );
110999
- pNew->rRun = whereCostAdd(rLogSize,pNew->nOut);
111115
+ pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
111116
+ pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
111000111117
pNew->wsFlags = WHERE_AUTO_INDEX;
111001111118
pNew->prereq = mExtra | pTerm->prereqRight;
111002111119
rc = whereLoopInsert(pBuilder, pNew);
111003111120
}
111004111121
}
@@ -111028,14 +111145,12 @@
111028111145
111029111146
/* Full table scan */
111030111147
pNew->iSortIdx = b ? iSortIdx : 0;
111031111148
/* TUNING: Cost of full table scan is 3*(N + log2(N)).
111032111149
** + The extra 3 factor is to encourage the use of indexed lookups
111033
- ** over full scans. A smaller constant 2 is used for covering
111034
- ** index scans so that a covering index scan will be favored over
111035
- ** a table scan. */
111036
- pNew->rRun = whereCostAdd(rSize,rLogSize) + 16;
111150
+ ** over full scans. FIXME */
111151
+ pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
111037111152
whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor);
111038111153
rc = whereLoopInsert(pBuilder, pNew);
111039111154
pNew->nOut = rSize;
111040111155
if( rc ) break;
111041111156
}else{
@@ -111044,26 +111159,25 @@
111044111159
111045111160
/* Full scan via index */
111046111161
if( b
111047111162
|| ( m==0
111048111163
&& pProbe->bUnordered==0
111164
+ && pProbe->szIdxRow<pTab->szTabRow
111049111165
&& (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
111050111166
&& sqlite3GlobalConfig.bUseCis
111051111167
&& OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
111052111168
)
111053111169
){
111054111170
pNew->iSortIdx = b ? iSortIdx : 0;
111055111171
if( m==0 ){
111056
- /* TUNING: Cost of a covering index scan is 2*(N + log2(N)).
111057
- ** + The extra 2 factor is to encourage the use of indexed lookups
111058
- ** over index scans. A table scan uses a factor of 3 so that
111059
- ** index scans are favored over table scans.
111060
- ** + If this covering index might also help satisfy the ORDER BY
111061
- ** clause, then the cost is fudged down slightly so that this
111062
- ** index is favored above other indices that have no hope of
111063
- ** helping with the ORDER BY. */
111064
- pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b;
111172
+ /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
111173
+ ** + The extra factor K of between 1.1 and 3.0 that depends
111174
+ ** on the relative sizes of the table and the index. K
111175
+ ** is smaller for smaller indices, thus favoring them.
111176
+ */
111177
+ pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
111178
+ (15*pProbe->szIdxRow)/pTab->szTabRow;
111065111179
}else{
111066111180
assert( b!=0 );
111067111181
/* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
111068111182
** which we will simplify to just N*log2(N) */
111069111183
pNew->rRun = rSize + rLogSize;
@@ -111237,13 +111351,13 @@
111237111351
pIdxInfo->needToFreeIdxStr = 0;
111238111352
pNew->u.vtab.idxStr = pIdxInfo->idxStr;
111239111353
pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
111240111354
&& pIdxInfo->orderByConsumed);
111241111355
pNew->rSetup = 0;
111242
- pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost);
111356
+ pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
111243111357
/* TUNING: Every virtual table query returns 25 rows */
111244
- pNew->nOut = 46; assert( 46==whereCost(25) );
111358
+ pNew->nOut = 46; assert( 46==sqlite3LogEst(25) );
111245111359
whereLoopInsert(pBuilder, pNew);
111246111360
if( pNew->u.vtab.needFree ){
111247111361
sqlite3_free(pNew->u.vtab.idxStr);
111248111362
pNew->u.vtab.needFree = 0;
111249111363
}
@@ -111276,10 +111390,12 @@
111276111390
pWC = pBuilder->pWC;
111277111391
if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
111278111392
pWCEnd = pWC->a + pWC->nTerm;
111279111393
pNew = pBuilder->pNew;
111280111394
memset(&sSum, 0, sizeof(sSum));
111395
+ pItem = pWInfo->pTabList->a + pNew->iTab;
111396
+ iCur = pItem->iCursor;
111281111397
111282111398
for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
111283111399
if( (pTerm->eOperator & WO_OR)!=0
111284111400
&& (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
111285111401
){
@@ -111287,12 +111403,10 @@
111287111403
WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
111288111404
WhereTerm *pOrTerm;
111289111405
int once = 1;
111290111406
int i, j;
111291111407
111292
- pItem = pWInfo->pTabList->a + pNew->iTab;
111293
- iCur = pItem->iCursor;
111294111408
sSubBuild = *pBuilder;
111295111409
sSubBuild.pOrderBy = 0;
111296111410
sSubBuild.pOrSet = &sCur;
111297111411
111298111412
for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
@@ -111329,12 +111443,12 @@
111329111443
whereOrMove(&sPrev, &sSum);
111330111444
sSum.n = 0;
111331111445
for(i=0; i<sPrev.n; i++){
111332111446
for(j=0; j<sCur.n; j++){
111333111447
whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
111334
- whereCostAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
111335
- whereCostAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
111448
+ sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
111449
+ sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
111336111450
}
111337111451
}
111338111452
}
111339111453
}
111340111454
pNew->nLTerm = 1;
@@ -111668,23 +111782,23 @@
111668111782
** costs if nRowEst==0.
111669111783
**
111670111784
** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
111671111785
** error occurs.
111672111786
*/
111673
-static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){
111787
+static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
111674111788
int mxChoice; /* Maximum number of simultaneous paths tracked */
111675111789
int nLoop; /* Number of terms in the join */
111676111790
Parse *pParse; /* Parsing context */
111677111791
sqlite3 *db; /* The database connection */
111678111792
int iLoop; /* Loop counter over the terms of the join */
111679111793
int ii, jj; /* Loop counters */
111680111794
int mxI = 0; /* Index of next entry to replace */
111681
- WhereCost rCost; /* Cost of a path */
111682
- WhereCost nOut; /* Number of outputs */
111683
- WhereCost mxCost = 0; /* Maximum cost of a set of paths */
111684
- WhereCost mxOut = 0; /* Maximum nOut value on the set of paths */
111685
- WhereCost rSortCost; /* Cost to do a sort */
111795
+ LogEst rCost; /* Cost of a path */
111796
+ LogEst nOut; /* Number of outputs */
111797
+ LogEst mxCost = 0; /* Maximum cost of a set of paths */
111798
+ LogEst mxOut = 0; /* Maximum nOut value on the set of paths */
111799
+ LogEst rSortCost; /* Cost to do a sort */
111686111800
int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
111687111801
WherePath *aFrom; /* All nFrom paths at the previous level */
111688111802
WherePath *aTo; /* The nTo best paths at the current level */
111689111803
WherePath *pFrom; /* An element of aFrom[] that we are working on */
111690111804
WherePath *pTo; /* An element of aTo[] that we are working on */
@@ -111717,21 +111831,23 @@
111717111831
/* Seed the search with a single WherePath containing zero WhereLoops.
111718111832
**
111719111833
** TUNING: Do not let the number of iterations go above 25. If the cost
111720111834
** of computing an automatic index is not paid back within the first 25
111721111835
** rows, then do not use the automatic index. */
111722
- aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) );
111836
+ aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
111723111837
nFrom = 1;
111724111838
111725111839
/* Precompute the cost of sorting the final result set, if the caller
111726111840
** to sqlite3WhereBegin() was concerned about sorting */
111727111841
rSortCost = 0;
111728111842
if( pWInfo->pOrderBy==0 || nRowEst==0 ){
111729111843
aFrom[0].isOrderedValid = 1;
111730111844
}else{
111731
- /* TUNING: Estimated cost of sorting is N*log2(N) where N is the
111732
- ** number of output rows. */
111845
+ /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
111846
+ ** number of output rows. The 48 is the expected size of a row to sort.
111847
+ ** FIXME: compute a better estimate of the 48 multiplier based on the
111848
+ ** result set expressions. */
111733111849
rSortCost = nRowEst + estLog(nRowEst);
111734111850
WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
111735111851
}
111736111852
111737111853
/* Compute successively longer WherePaths using the previous generation
@@ -111747,12 +111863,12 @@
111747111863
u8 isOrdered = pFrom->isOrdered;
111748111864
if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
111749111865
if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
111750111866
/* At this point, pWLoop is a candidate to be the next loop.
111751111867
** Compute its cost */
111752
- rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
111753
- rCost = whereCostAdd(rCost, pFrom->rCost);
111868
+ rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
111869
+ rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
111754111870
nOut = pFrom->nRow + pWLoop->nOut;
111755111871
maskNew = pFrom->maskLoop | pWLoop->maskSelf;
111756111872
if( !isOrderedValid ){
111757111873
switch( wherePathSatisfiesOrderBy(pWInfo,
111758111874
pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
@@ -111762,11 +111878,11 @@
111762111878
isOrderedValid = 1;
111763111879
break;
111764111880
case 0: /* No. pFrom+pWLoop will require a separate sort */
111765111881
isOrdered = 0;
111766111882
isOrderedValid = 1;
111767
- rCost = whereCostAdd(rCost, rSortCost);
111883
+ rCost = sqlite3LogEstAdd(rCost, rSortCost);
111768111884
break;
111769111885
default: /* Cannot tell yet. Try again on the next iteration */
111770111886
break;
111771111887
}
111772111888
}else{
@@ -111969,11 +112085,11 @@
111969112085
pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
111970112086
pLoop->aLTerm[0] = pTerm;
111971112087
pLoop->nLTerm = 1;
111972112088
pLoop->u.btree.nEq = 1;
111973112089
/* TUNING: Cost of a rowid lookup is 10 */
111974
- pLoop->rRun = 33; /* 33==whereCost(10) */
112090
+ pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
111975112091
}else{
111976112092
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111977112093
assert( pLoop->aLTermSpace==pLoop->aLTerm );
111978112094
assert( ArraySize(pLoop->aLTermSpace)==4 );
111979112095
if( pIdx->onError==OE_None
@@ -111992,16 +112108,16 @@
111992112108
}
111993112109
pLoop->nLTerm = j;
111994112110
pLoop->u.btree.nEq = j;
111995112111
pLoop->u.btree.pIndex = pIdx;
111996112112
/* TUNING: Cost of a unique index lookup is 15 */
111997
- pLoop->rRun = 39; /* 39==whereCost(15) */
112113
+ pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
111998112114
break;
111999112115
}
112000112116
}
112001112117
if( pLoop->wsFlags ){
112002
- pLoop->nOut = (WhereCost)1;
112118
+ pLoop->nOut = (LogEst)1;
112003112119
pWInfo->a[0].pWLoop = pLoop;
112004112120
pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
112005112121
pWInfo->a[0].iTabCur = iCur;
112006112122
pWInfo->nRowOut = 1;
112007112123
if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1;
112008112124
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -656,11 +656,11 @@
656 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657 ** [sqlite_version()] and [sqlite_source_id()].
658 */
659 #define SQLITE_VERSION "3.8.1"
660 #define SQLITE_VERSION_NUMBER 3008001
661 #define SQLITE_SOURCE_ID "2013-10-07 21:49:16 36d64dc36f18c166b2c93c43579fa3bbb5cd545f"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -8272,10 +8272,35 @@
8272 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
8273 #else
8274 typedef u32 tRowcnt; /* 32-bit is the default */
8275 #endif
8276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8277 /*
8278 ** Macros to determine whether the machine is big or little endian,
8279 ** evaluated at runtime.
8280 */
8281 #ifdef SQLITE_AMALGAMATION
@@ -10419,11 +10444,12 @@
10419 char *zDflt; /* Original text of the default value */
10420 char *zType; /* Data type for this column */
10421 char *zColl; /* Collating sequence. If NULL, use the default */
10422 u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
10423 char affinity; /* One of the SQLITE_AFF_... values */
10424 u16 colFlags; /* Boolean properties. See COLFLAG_ defines below */
 
10425 };
10426
10427 /* Allowed values for Column.colFlags:
10428 */
10429 #define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
@@ -10583,10 +10609,11 @@
10583 tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
10584 int tnum; /* Root BTree node for this table (see note above) */
10585 i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
10586 i16 nCol; /* Number of columns in this table */
10587 u16 nRef; /* Number of pointers to this Table */
 
10588 u8 tabFlags; /* Mask of TF_* values */
10589 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
10590 #ifndef SQLITE_OMIT_ALTERTABLE
10591 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
10592 #endif
@@ -10694,11 +10721,11 @@
10694 #define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
10695 #define OE_SetNull 7 /* Set the foreign key value to NULL */
10696 #define OE_SetDflt 8 /* Set the foreign key value to its default */
10697 #define OE_Cascade 9 /* Cascade the changes */
10698
10699 #define OE_Default 99 /* Do whatever the default action is */
10700
10701
10702 /*
10703 ** An instance of the following structure is passed as the first
10704 ** argument to sqlite3VdbeKeyCompare and is used to control the
@@ -10781,10 +10808,11 @@
10781 Schema *pSchema; /* Schema containing this index */
10782 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
10783 char **azColl; /* Array of collation sequence names for index */
10784 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
10785 int tnum; /* DB Page containing root of this index */
 
10786 u16 nColumn; /* Number of columns in table used by this index */
10787 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10788 unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
10789 unsigned bUnordered:1; /* Use this index for == or IN queries only */
10790 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
@@ -12187,10 +12215,16 @@
12187 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
12188 SQLITE_PRIVATE int sqlite3Atoi(const char*);
12189 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
12190 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
12191 SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
 
 
 
 
 
 
12192
12193 /*
12194 ** Routines to read and write variable-length integers. These used to
12195 ** be defined locally, but now we use the varint routines in the util.c
12196 ** file. Code should use the MACRO forms below, as the Varint32 versions
@@ -12303,11 +12337,11 @@
12303 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
12304 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
12305 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
12306 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
12307 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
12308 SQLITE_PRIVATE char sqlite3AffinityType(const char*);
12309 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
12310 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
12311 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
12312 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
12313 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
@@ -22388,10 +22422,87 @@
22388 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
22389 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
22390 }
22391 }
22392 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22393
22394 /************** End of util.c ************************************************/
22395 /************** Begin file hash.c ********************************************/
22396 /*
22397 ** 2001 September 22
@@ -76084,11 +76195,11 @@
76084 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
76085 }
76086 #ifndef SQLITE_OMIT_CAST
76087 if( op==TK_CAST ){
76088 assert( !ExprHasProperty(pExpr, EP_IntValue) );
76089 return sqlite3AffinityType(pExpr->u.zToken);
76090 }
76091 #endif
76092 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
76093 && pExpr->pTab!=0
76094 ){
@@ -78504,11 +78615,11 @@
78504 case TK_CAST: {
78505 /* Expressions of the form: CAST(pLeft AS token) */
78506 int aff, to_op;
78507 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
78508 assert( !ExprHasProperty(pExpr, EP_IntValue) );
78509 aff = sqlite3AffinityType(pExpr->u.zToken);
78510 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
78511 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
78512 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
78513 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
78514 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
@@ -79171,11 +79282,11 @@
79171 }
79172 #ifndef SQLITE_OMIT_CAST
79173 case TK_CAST: {
79174 /* Expressions of the form: CAST(pLeft AS token) */
79175 const char *zAff = "unk";
79176 switch( sqlite3AffinityType(pExpr->u.zToken) ){
79177 case SQLITE_AFF_TEXT: zAff = "TEXT"; break;
79178 case SQLITE_AFF_NONE: zAff = "NONE"; break;
79179 case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break;
79180 case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break;
79181 case SQLITE_AFF_REAL: zAff = "REAL"; break;
@@ -81886,16 +81997,16 @@
81886 if( zRet==0 ){
81887 sqlite3_result_error_nomem(context);
81888 return;
81889 }
81890
81891 sqlite3_snprintf(24, zRet, "%lld", p->nRow);
81892 z = zRet + sqlite3Strlen30(zRet);
81893 for(i=0; i<(p->nCol-1); i++){
81894 i64 nDistinct = p->current.anDLt[i] + 1;
81895 i64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
81896 sqlite3_snprintf(24, z, " %lld", iVal);
81897 z += sqlite3Strlen30(z);
81898 assert( p->current.anEq[i] );
81899 }
81900 assert( z[0]=='\0' && z>zRet );
81901
@@ -81932,11 +82043,11 @@
81932 sqlite3_result_error_nomem(context);
81933 }else{
81934 int i;
81935 char *z = zRet;
81936 for(i=0; i<p->nCol; i++){
81937 sqlite3_snprintf(24, z, "%lld ", aCnt[i]);
81938 z += sqlite3Strlen30(z);
81939 }
81940 assert( z[0]=='\0' && z>zRet );
81941 z[-1] = '\0';
81942 sqlite3_result_text(context, zRet, -1, sqlite3_free);
@@ -82393,22 +82504,20 @@
82393 ** The first argument points to a nul-terminated string containing a
82394 ** list of space separated integers. Read the first nOut of these into
82395 ** the array aOut[].
82396 */
82397 static void decodeIntArray(
82398 char *zIntArray,
82399 int nOut,
82400 tRowcnt *aOut,
82401 int *pbUnordered
82402 ){
82403 char *z = zIntArray;
82404 int c;
82405 int i;
82406 tRowcnt v;
82407
82408 assert( pbUnordered==0 || *pbUnordered==0 );
82409
82410 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82411 if( z==0 ) z = "";
82412 #else
82413 if( NEVER(z==0) ) z = "";
82414 #endif
@@ -82419,12 +82528,23 @@
82419 z++;
82420 }
82421 aOut[i] = v;
82422 if( *z==' ' ) z++;
82423 }
82424 if( pbUnordered && strcmp(z, "unordered")==0 ){
82425 *pbUnordered = 1;
 
 
 
 
 
 
 
 
 
 
 
82426 }
82427 }
82428
82429 /*
82430 ** This callback is invoked once for each index when reading the
@@ -82459,16 +82579,17 @@
82459 pIndex = 0;
82460 }
82461 z = argv[2];
82462
82463 if( pIndex ){
82464 int bUnordered = 0;
82465 decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst,&bUnordered);
82466 if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
82467 pIndex->bUnordered = bUnordered;
82468 }else{
82469 decodeIntArray((char*)z, 1, &pTable->nRowEst, 0);
 
 
 
82470 }
82471
82472 return 0;
82473 }
82474
@@ -84480,11 +84601,11 @@
84480 }
84481 pTable->zName = zName;
84482 pTable->iPKey = -1;
84483 pTable->pSchema = db->aDb[iDb].pSchema;
84484 pTable->nRef = 1;
84485 pTable->nRowEst = 1000000;
84486 assert( pParse->pNewTable==0 );
84487 pParse->pNewTable = pTable;
84488
84489 /* If this is the magic sqlite_sequence table used by autoincrement,
84490 ** then record a pointer to this table in the main database structure
@@ -84627,10 +84748,11 @@
84627 /* If there is no type specified, columns have the default affinity
84628 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
84629 ** be called next to set pCol->affinity correctly.
84630 */
84631 pCol->affinity = SQLITE_AFF_NONE;
 
84632 p->nCol++;
84633 }
84634
84635 /*
84636 ** This routine is called by the parser while in the middle of
@@ -84668,26 +84790,30 @@
84668 ** 'DOUB' | SQLITE_AFF_REAL
84669 **
84670 ** If none of the substrings in the above table are found,
84671 ** SQLITE_AFF_NUMERIC is returned.
84672 */
84673 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){
84674 u32 h = 0;
84675 char aff = SQLITE_AFF_NUMERIC;
 
84676
84677 if( zIn ) while( zIn[0] ){
 
84678 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
84679 zIn++;
84680 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
84681 aff = SQLITE_AFF_TEXT;
 
84682 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
84683 aff = SQLITE_AFF_TEXT;
84684 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
84685 aff = SQLITE_AFF_TEXT;
84686 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
84687 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
84688 aff = SQLITE_AFF_NONE;
 
84689 #ifndef SQLITE_OMIT_FLOATING_POINT
84690 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
84691 && aff==SQLITE_AFF_NUMERIC ){
84692 aff = SQLITE_AFF_REAL;
84693 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
@@ -84701,10 +84827,32 @@
84701 aff = SQLITE_AFF_INTEGER;
84702 break;
84703 }
84704 }
84705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84706 return aff;
84707 }
84708
84709 /*
84710 ** This routine is called by the parser while in the middle of
@@ -84722,11 +84870,11 @@
84722 p = pParse->pNewTable;
84723 if( p==0 || NEVER(p->nCol<1) ) return;
84724 pCol = &p->aCol[p->nCol-1];
84725 assert( pCol->zType==0 );
84726 pCol->zType = sqlite3NameFromToken(pParse->db, pType);
84727 pCol->affinity = sqlite3AffinityType(pCol->zType);
84728 }
84729
84730 /*
84731 ** The expression is the default value for the most recently added column
84732 ** of the table currently under construction.
@@ -85070,18 +85218,46 @@
85070 testcase( pCol->affinity==SQLITE_AFF_REAL );
85071
85072 zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
85073 len = sqlite3Strlen30(zType);
85074 assert( pCol->affinity==SQLITE_AFF_NONE
85075 || pCol->affinity==sqlite3AffinityType(zType) );
85076 memcpy(&zStmt[k], zType, len);
85077 k += len;
85078 assert( k<=n );
85079 }
85080 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
85081 return zStmt;
85082 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85083
85084 /*
85085 ** This routine is called to report the final ")" that terminates
85086 ** a CREATE TABLE statement.
85087 **
@@ -85105,13 +85281,14 @@
85105 Parse *pParse, /* Parse context */
85106 Token *pCons, /* The ',' token after the last column defn. */
85107 Token *pEnd, /* The final ')' token in the CREATE TABLE */
85108 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
85109 ){
85110 Table *p;
85111 sqlite3 *db = pParse->db;
85112 int iDb;
 
85113
85114 if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
85115 return;
85116 }
85117 p = pParse->pNewTable;
@@ -85126,10 +85303,16 @@
85126 */
85127 if( p->pCheck ){
85128 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
85129 }
85130 #endif /* !defined(SQLITE_OMIT_CHECK) */
 
 
 
 
 
 
85131
85132 /* If the db->init.busy is 1 it means we are reading the SQL off the
85133 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
85134 ** So do not write to the disk again. Extract the root page number
85135 ** for the table from the db->init.newTnum field. (The page number
@@ -86085,13 +86268,14 @@
86085 sqlite3 *db = pParse->db;
86086 Db *pDb; /* The specific table containing the indexed database */
86087 int iDb; /* Index of the database that is being written */
86088 Token *pName = 0; /* Unqualified name of the index to create */
86089 struct ExprList_item *pListItem; /* For looping over pList */
86090 int nCol;
86091 int nExtra = 0;
86092 char *zExtra;
 
86093
86094 assert( pParse->nErr==0 ); /* Never called with prior errors */
86095 if( db->mallocFailed || IN_DECLARE_VTAB ){
86096 goto exit_create_index;
86097 }
@@ -86314,11 +86498,10 @@
86314 ** same column more than once cannot be an error because that would
86315 ** break backwards compatibility - it needs to be a warning.
86316 */
86317 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
86318 const char *zColName = pListItem->zName;
86319 Column *pTabCol;
86320 int requestedSortOrder;
86321 char *zColl; /* Collation sequence name */
86322
86323 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
86324 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
@@ -86351,10 +86534,11 @@
86351 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
86352 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
86353 if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
86354 }
86355 sqlite3DefaultRowEst(pIndex);
 
86356
86357 if( pTab==pParse->pNewTable ){
86358 /* This routine has been called to create an automatic index as a
86359 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
86360 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
@@ -96379,30 +96563,34 @@
96379 break;
96380
96381 case PragTyp_INDEX_LIST: if( zRight ){
96382 Index *pIdx;
96383 Table *pTab;
 
96384 pTab = sqlite3FindTable(db, zRight, zDb);
96385 if( pTab ){
96386 v = sqlite3GetVdbe(pParse);
96387 pIdx = pTab->pIndex;
96388 if( pIdx ){
96389 int i = 0;
96390 sqlite3VdbeSetNumCols(v, 3);
96391 pParse->nMem = 3;
96392 sqlite3CodeVerifySchema(pParse, iDb);
96393 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
96394 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
96395 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
96396 while(pIdx){
96397 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
96398 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
96399 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
96400 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
96401 ++i;
96402 pIdx = pIdx->pNext;
96403 }
 
 
 
96404 }
96405 }
96406 }
96407 break;
96408
@@ -99086,10 +99274,13 @@
99086 }
99087
99088 /*
99089 ** Return a pointer to a string containing the 'declaration type' of the
99090 ** expression pExpr. The string may be treated as static by the caller.
 
 
 
99091 **
99092 ** The declaration type is the exact datatype definition extracted from the
99093 ** original CREATE TABLE statement if the expression is a column. The
99094 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
99095 ** is considered a column can be complex in the presence of subqueries. The
@@ -99100,25 +99291,40 @@
99100 ** SELECT (SELECT col FROM tbl;
99101 ** SELECT (SELECT col FROM tbl);
99102 ** SELECT abc FROM (SELECT col AS abc FROM tbl);
99103 **
99104 ** The declaration type for any expression other than a column is NULL.
 
 
 
99105 */
99106 static const char *columnType(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99107 NameContext *pNC,
99108 Expr *pExpr,
99109 const char **pzOriginDb,
99110 const char **pzOriginTab,
99111 const char **pzOriginCol
99112 ){
 
99113 char const *zType = 0;
99114 char const *zOriginDb = 0;
99115 char const *zOriginTab = 0;
99116 char const *zOriginCol = 0;
99117 int j;
 
 
99118 if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
99119
99120 switch( pExpr->op ){
99121 case TK_AGG_COLUMN:
99122 case TK_COLUMN: {
99123 /* The expression is a column. Locate the table the column is being
99124 ** extracted from in NameContext.pSrcList. This table may be real
@@ -99175,29 +99381,39 @@
99175 NameContext sNC;
99176 Expr *p = pS->pEList->a[iCol].pExpr;
99177 sNC.pSrcList = pS->pSrc;
99178 sNC.pNext = pNC;
99179 sNC.pParse = pNC->pParse;
99180 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
99181 }
99182 }else if( ALWAYS(pTab->pSchema) ){
99183 /* A real table */
99184 assert( !pS );
99185 if( iCol<0 ) iCol = pTab->iPKey;
99186 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
 
99187 if( iCol<0 ){
99188 zType = "INTEGER";
99189 zOriginCol = "rowid";
99190 }else{
99191 zType = pTab->aCol[iCol].zType;
99192 zOriginCol = pTab->aCol[iCol].zName;
 
99193 }
99194 zOriginTab = pTab->zName;
99195 if( pNC->pParse ){
99196 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
99197 zOriginDb = pNC->pParse->db->aDb[iDb].zName;
99198 }
 
 
 
 
 
 
 
 
99199 }
99200 break;
99201 }
99202 #ifndef SQLITE_OMIT_SUBQUERY
99203 case TK_SELECT: {
@@ -99210,22 +99426,25 @@
99210 Expr *p = pS->pEList->a[0].pExpr;
99211 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
99212 sNC.pSrcList = pS->pSrc;
99213 sNC.pNext = pNC;
99214 sNC.pParse = pNC->pParse;
99215 zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol);
99216 break;
99217 }
99218 #endif
99219 }
99220
99221 if( pzOriginDb ){
99222 assert( pzOriginTab && pzOriginCol );
99223 *pzOriginDb = zOriginDb;
99224 *pzOriginTab = zOriginTab;
99225 *pzOriginCol = zOriginCol;
 
99226 }
 
 
99227 return zType;
99228 }
99229
99230 /*
99231 ** Generate code that will tell the VDBE the declaration types of columns
@@ -99247,25 +99466,25 @@
99247 const char *zType;
99248 #ifdef SQLITE_ENABLE_COLUMN_METADATA
99249 const char *zOrigDb = 0;
99250 const char *zOrigTab = 0;
99251 const char *zOrigCol = 0;
99252 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol);
99253
99254 /* The vdbe must make its own copy of the column-type and other
99255 ** column specific strings, in case the schema is reset before this
99256 ** virtual machine is deleted.
99257 */
99258 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
99259 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
99260 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
99261 #else
99262 zType = columnType(&sNC, p, 0, 0, 0);
99263 #endif
99264 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
99265 }
99266 #endif /* SQLITE_OMIT_DECLTYPE */
99267 }
99268
99269 /*
99270 ** Generate code that will tell the VDBE the names of columns
99271 ** in the result set. This information is used to provide the
@@ -99450,39 +99669,41 @@
99450 ** This routine requires that all identifiers in the SELECT
99451 ** statement be resolved.
99452 */
99453 static void selectAddColumnTypeAndCollation(
99454 Parse *pParse, /* Parsing contexts */
99455 int nCol, /* Number of columns */
99456 Column *aCol, /* List of columns */
99457 Select *pSelect /* SELECT used to determine types and collations */
99458 ){
99459 sqlite3 *db = pParse->db;
99460 NameContext sNC;
99461 Column *pCol;
99462 CollSeq *pColl;
99463 int i;
99464 Expr *p;
99465 struct ExprList_item *a;
 
99466
99467 assert( pSelect!=0 );
99468 assert( (pSelect->selFlags & SF_Resolved)!=0 );
99469 assert( nCol==pSelect->pEList->nExpr || db->mallocFailed );
99470 if( db->mallocFailed ) return;
99471 memset(&sNC, 0, sizeof(sNC));
99472 sNC.pSrcList = pSelect->pSrc;
99473 a = pSelect->pEList->a;
99474 for(i=0, pCol=aCol; i<nCol; i++, pCol++){
99475 p = a[i].pExpr;
99476 pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0));
 
99477 pCol->affinity = sqlite3ExprAffinity(p);
99478 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
99479 pColl = sqlite3ExprCollSeq(pParse, p);
99480 if( pColl ){
99481 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
99482 }
99483 }
 
99484 }
99485
99486 /*
99487 ** Given a SELECT statement, generate a Table structure that describes
99488 ** the result set of that SELECT.
@@ -99506,13 +99727,13 @@
99506 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
99507 ** is disabled */
99508 assert( db->lookaside.bEnabled==0 );
99509 pTab->nRef = 1;
99510 pTab->zName = 0;
99511 pTab->nRowEst = 1000000;
99512 selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
99513 selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect);
99514 pTab->iPKey = -1;
99515 if( db->mallocFailed ){
99516 sqlite3DeleteTable(db, pTab);
99517 return 0;
99518 }
@@ -101420,15 +101641,15 @@
101420 assert( pFrom->pTab==0 );
101421 sqlite3WalkSelect(pWalker, pSel);
101422 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
101423 if( pTab==0 ) return WRC_Abort;
101424 pTab->nRef = 1;
101425 pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
101426 while( pSel->pPrior ){ pSel = pSel->pPrior; }
101427 selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
101428 pTab->iPKey = -1;
101429 pTab->nRowEst = 1000000;
101430 pTab->tabFlags |= TF_Ephemeral;
101431 #endif
101432 }else{
101433 /* An ordinary table or view name in the FROM clause */
101434 assert( pFrom->pTab==0 );
@@ -101708,11 +101929,11 @@
101708 if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
101709 /* A sub-query in the FROM clause of a SELECT */
101710 Select *pSel = pFrom->pSelect;
101711 assert( pSel );
101712 while( pSel->pPrior ) pSel = pSel->pPrior;
101713 selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel);
101714 }
101715 }
101716 }
101717 return WRC_Continue;
101718 }
@@ -102623,17 +102844,11 @@
102623 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
102624
102625 sqlite3CodeVerifySchema(pParse, iDb);
102626 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
102627
102628 /* Search for the index that has the least amount of columns. If
102629 ** there is such an index, and it has less columns than the table
102630 ** does, then we can assume that it consumes less space on disk and
102631 ** will therefore be cheaper to scan to determine the query result.
102632 ** In this case set iRoot to the root page number of the index b-tree
102633 ** and pKeyInfo to the KeyInfo structure required to navigate the
102634 ** index.
102635 **
102636 ** (2011-04-15) Do not do a full scan of an unordered index.
102637 **
102638 ** (2013-10-03) Do not count the entires in a partial index.
102639 **
@@ -102640,17 +102855,18 @@
102640 ** In practice the KeyInfo structure will not be used. It is only
102641 ** passed to keep OP_OpenRead happy.
102642 */
102643 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102644 if( pIdx->bUnordered==0
 
102645 && pIdx->pPartIdxWhere==0
102646 && (!pBest || pIdx->nColumn<pBest->nColumn)
102647 ){
102648 pBest = pIdx;
102649 }
102650 }
102651 if( pBest && pBest->nColumn<pTab->nCol ){
102652 iRoot = pBest->tnum;
102653 pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
102654 }
102655
102656 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
@@ -106406,30 +106622,10 @@
106406 typedef struct WhereLoopBuilder WhereLoopBuilder;
106407 typedef struct WhereScan WhereScan;
106408 typedef struct WhereOrCost WhereOrCost;
106409 typedef struct WhereOrSet WhereOrSet;
106410
106411 /*
106412 ** Cost X is tracked as 10*log2(X) stored in a 16-bit integer. The
106413 ** maximum cost for ordinary tables is 64*(2**63) which becomes 6900.
106414 ** (Virtual tables can return a larger cost, but let's assume they do not.)
106415 ** So all costs can be stored in a 16-bit integer without risk
106416 ** of overflow.
106417 **
106418 ** Costs are estimates, so no effort is made to compute 10*log2(X) exactly.
106419 ** Instead, a close estimate is used. Any value of X=1 is stored as 0.
106420 ** X=2 is 10. X=3 is 16. X=1000 is 99. etc. Negative values are allowed.
106421 ** A WhereCost of -10 means 0.5. WhereCost of -20 means 0.25. And so forth.
106422 **
106423 ** The tool/wherecosttest.c source file implements a command-line program
106424 ** that will convert WhereCosts to integers, convert integers to WhereCosts
106425 ** and do addition and multiplication on WhereCost values. The wherecosttest
106426 ** command-line program is a useful utility to have around when working with
106427 ** this module.
106428 */
106429 typedef short int WhereCost;
106430
106431 /*
106432 ** This object contains information needed to implement a single nested
106433 ** loop in WHERE clause.
106434 **
106435 ** Contrast this object with WhereLoop. This object describes the
@@ -106490,13 +106686,13 @@
106490 #ifdef SQLITE_DEBUG
106491 char cId; /* Symbolic ID of this loop for debugging use */
106492 #endif
106493 u8 iTab; /* Position in FROM clause of table for this loop */
106494 u8 iSortIdx; /* Sorting index number. 0==None */
106495 WhereCost rSetup; /* One-time setup cost (ex: create transient index) */
106496 WhereCost rRun; /* Cost of running each loop */
106497 WhereCost nOut; /* Estimated number of output rows */
106498 union {
106499 struct { /* Information for internal btree tables */
106500 int nEq; /* Number of equality constraints */
106501 Index *pIndex; /* Index used, or NULL */
106502 } btree;
@@ -106522,12 +106718,12 @@
106522 ** subquery on one operand of an OR operator in the WHERE clause.
106523 ** See WhereOrSet for additional information
106524 */
106525 struct WhereOrCost {
106526 Bitmask prereq; /* Prerequisites */
106527 WhereCost rRun; /* Cost of running this subquery */
106528 WhereCost nOut; /* Number of outputs for this subquery */
106529 };
106530
106531 /* The WhereOrSet object holds a set of possible WhereOrCosts that
106532 ** correspond to the subquery(s) of OR-clause processing. Only the
106533 ** best N_OR_COST elements are retained.
@@ -106561,12 +106757,12 @@
106561 ** at the end is the choosen query plan.
106562 */
106563 struct WherePath {
106564 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
106565 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
106566 WhereCost nRow; /* Estimated number of rows generated by this path */
106567 WhereCost rCost; /* Total cost of this path */
106568 u8 isOrdered; /* True if this path satisfies ORDER BY */
106569 u8 isOrderedValid; /* True if the isOrdered field is valid */
106570 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
106571 };
106572
@@ -106628,11 +106824,11 @@
106628 union {
106629 int leftColumn; /* Column number of X in "X <op> <expr>" */
106630 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
106631 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
106632 } u;
106633 WhereCost truthProb; /* Probability of truth for this expression */
106634 u16 eOperator; /* A WO_xx value describing <op> */
106635 u8 wtFlags; /* TERM_xxx bit flags. See below */
106636 u8 nChild; /* Number of children that must disable us */
106637 WhereClause *pWC; /* The clause this term is part of */
106638 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
@@ -106776,11 +106972,11 @@
106776 SrcList *pTabList; /* List of tables in the join */
106777 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
106778 ExprList *pResultSet; /* Result set. DISTINCT operates on these */
106779 WhereLoop *pLoops; /* List of all WhereLoop objects */
106780 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
106781 WhereCost nRowOut; /* Estimated number of output rows */
106782 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
106783 u8 bOBSat; /* ORDER BY satisfied by indices */
106784 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
106785 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
106786 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
@@ -106836,30 +107032,15 @@
106836 #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
106837 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
106838 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
106839 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
106840
106841
106842 /* Convert a WhereCost value (10 times log2(X)) into its integer value X.
106843 ** A rough approximation is used. The value returned is not exact.
106844 */
106845 static u64 whereCostToInt(WhereCost x){
106846 u64 n;
106847 if( x<10 ) return 1;
106848 n = x%10;
106849 x /= 10;
106850 if( n>=5 ) n -= 2;
106851 else if( n>=1 ) n -= 1;
106852 if( x>=3 ) return (n+8)<<(x-3);
106853 return (n+8)>>(3-x);
106854 }
106855
106856 /*
106857 ** Return the estimated number of output rows from a WHERE clause
106858 */
106859 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
106860 return whereCostToInt(pWInfo->nRowOut);
106861 }
106862
106863 /*
106864 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
106865 ** WHERE clause returns outputs for DISTINCT processing.
@@ -106917,12 +107098,12 @@
106917 ** so that pSet keeps the N_OR_COST best entries seen so far.
106918 */
106919 static int whereOrInsert(
106920 WhereOrSet *pSet, /* The WhereOrSet to be updated */
106921 Bitmask prereq, /* Prerequisites of the new entry */
106922 WhereCost rRun, /* Run-cost of the new entry */
106923 WhereCost nOut /* Number of outputs for the new entry */
106924 ){
106925 u16 i;
106926 WhereOrCost *p;
106927 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
106928 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
@@ -107003,13 +107184,10 @@
107003 if( pWC->a!=pWC->aStatic ){
107004 sqlite3DbFree(db, pWC->a);
107005 }
107006 }
107007
107008 /* Forward declaration */
107009 static WhereCost whereCost(tRowcnt x);
107010
107011 /*
107012 ** Add a single new WhereTerm entry to the WhereClause object pWC.
107013 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
107014 ** The index in pWC->a[] of the new WhereTerm is returned on success.
107015 ** 0 is returned if the new WhereTerm could not be added due to a memory
@@ -107048,11 +107226,11 @@
107048 }
107049 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
107050 }
107051 pTerm = &pWC->a[idx = pWC->nTerm++];
107052 if( p && ExprHasProperty(p, EP_Unlikely) ){
107053 pTerm->truthProb = whereCost(p->iTable) - 99;
107054 }else{
107055 pTerm->truthProb = -1;
107056 }
107057 pTerm->pExpr = sqlite3ExprSkipCollate(p);
107058 pTerm->wtFlags = wtFlags;
@@ -108312,79 +108490,16 @@
108312 }
108313
108314 return 0;
108315 }
108316
108317 /*
108318 ** Find (an approximate) sum of two WhereCosts. This computation is
108319 ** not a simple "+" operator because WhereCost is stored as a logarithmic
108320 ** value.
108321 **
108322 */
108323 static WhereCost whereCostAdd(WhereCost a, WhereCost b){
108324 static const unsigned char x[] = {
108325 10, 10, /* 0,1 */
108326 9, 9, /* 2,3 */
108327 8, 8, /* 4,5 */
108328 7, 7, 7, /* 6,7,8 */
108329 6, 6, 6, /* 9,10,11 */
108330 5, 5, 5, /* 12-14 */
108331 4, 4, 4, 4, /* 15-18 */
108332 3, 3, 3, 3, 3, 3, /* 19-24 */
108333 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
108334 };
108335 if( a>=b ){
108336 if( a>b+49 ) return a;
108337 if( a>b+31 ) return a+1;
108338 return a+x[a-b];
108339 }else{
108340 if( b>a+49 ) return b;
108341 if( b>a+31 ) return b+1;
108342 return b+x[b-a];
108343 }
108344 }
108345
108346 /*
108347 ** Convert an integer into a WhereCost. In other words, compute a
108348 ** good approximatation for 10*log2(x).
108349 */
108350 static WhereCost whereCost(tRowcnt x){
108351 static WhereCost a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
108352 WhereCost y = 40;
108353 if( x<8 ){
108354 if( x<2 ) return 0;
108355 while( x<8 ){ y -= 10; x <<= 1; }
108356 }else{
108357 while( x>255 ){ y += 40; x >>= 4; }
108358 while( x>15 ){ y += 10; x >>= 1; }
108359 }
108360 return a[x&7] + y - 10;
108361 }
108362
108363 #ifndef SQLITE_OMIT_VIRTUALTABLE
108364 /*
108365 ** Convert a double (as received from xBestIndex of a virtual table)
108366 ** into a WhereCost. In other words, compute an approximation for
108367 ** 10*log2(x).
108368 */
108369 static WhereCost whereCostFromDouble(double x){
108370 u64 a;
108371 WhereCost e;
108372 assert( sizeof(x)==8 && sizeof(a)==8 );
108373 if( x<=1 ) return 0;
108374 if( x<=2000000000 ) return whereCost((tRowcnt)x);
108375 memcpy(&a, &x, 8);
108376 e = (a>>52) - 1022;
108377 return e*10;
108378 }
108379 #endif /* SQLITE_OMIT_VIRTUALTABLE */
108380
108381 /*
108382 ** Estimate the logarithm of the input value to base 2.
108383 */
108384 static WhereCost estLog(WhereCost N){
108385 WhereCost x = whereCost(N);
108386 return x>33 ? x - 33 : 0;
108387 }
108388
108389 /*
108390 ** Two routines for printing the content of an sqlite3_index_info
@@ -108893,11 +109008,11 @@
108893 **
108894 ** ... FROM t1 WHERE a > ? AND a < ? ...
108895 **
108896 ** then nEq is set to 0.
108897 **
108898 ** When this function is called, *pnOut is set to the whereCost() of the
108899 ** number of rows that the index scan is expected to visit without
108900 ** considering the range constraints. If nEq is 0, this is the number of
108901 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
108902 ** to account for the range contraints pLower and pUpper.
108903 **
@@ -108909,19 +109024,19 @@
108909 static int whereRangeScanEst(
108910 Parse *pParse, /* Parsing & code generating context */
108911 WhereLoopBuilder *pBuilder,
108912 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
108913 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
108914 WhereCost *pnOut /* IN/OUT: Number of rows visited */
108915 ){
108916 int rc = SQLITE_OK;
108917 int nOut = (int)*pnOut;
108918 WhereCost nNew;
 
108919
108920 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108921 Index *p = pBuilder->pNew->u.btree.pIndex;
108922 int nEq = pBuilder->pNew->u.btree.nEq;
108923
108924 if( p->nSample>0
108925 && nEq==pBuilder->nRecValid
108926 && nEq<p->nSampleCol
108927 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
@@ -108998,18 +109113,18 @@
108998 }
108999
109000 pBuilder->pRec = pRec;
109001 if( rc==SQLITE_OK ){
109002 if( iUpper>iLower ){
109003 nNew = whereCost(iUpper - iLower);
109004 }else{
109005 nNew = 10; assert( 10==whereCost(2) );
109006 }
109007 if( nNew<nOut ){
109008 nOut = nNew;
109009 }
109010 *pnOut = (WhereCost)nOut;
109011 WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n",
109012 (u32)iLower, (u32)iUpper, nOut));
109013 return SQLITE_OK;
109014 }
109015 }
@@ -109020,20 +109135,20 @@
109020 assert( pLower || pUpper );
109021 /* TUNING: Each inequality constraint reduces the search space 4-fold.
109022 ** A BETWEEN operator, therefore, reduces the search space 16-fold */
109023 nNew = nOut;
109024 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
109025 nNew -= 20; assert( 20==whereCost(4) );
109026 nOut--;
109027 }
109028 if( pUpper ){
109029 nNew -= 20; assert( 20==whereCost(4) );
109030 nOut--;
109031 }
109032 if( nNew<10 ) nNew = 10;
109033 if( nNew<nOut ) nOut = nNew;
109034 *pnOut = (WhereCost)nOut;
109035 return rc;
109036 }
109037
109038 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109039 /*
@@ -110673,11 +110788,11 @@
110673 */
110674 static int whereLoopAddBtreeIndex(
110675 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
110676 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
110677 Index *pProbe, /* An index on pSrc */
110678 WhereCost nInMul /* log(Number of iterations due to IN) */
110679 ){
110680 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
110681 Parse *pParse = pWInfo->pParse; /* Parsing context */
110682 sqlite3 *db = pParse->db; /* Database connection malloc context */
110683 WhereLoop *pNew; /* Template WhereLoop under construction */
@@ -110686,15 +110801,15 @@
110686 WhereScan scan; /* Iterator for WHERE terms */
110687 Bitmask saved_prereq; /* Original value of pNew->prereq */
110688 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
110689 int saved_nEq; /* Original value of pNew->u.btree.nEq */
110690 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
110691 WhereCost saved_nOut; /* Original value of pNew->nOut */
110692 int iCol; /* Index of the column in the table */
110693 int rc = SQLITE_OK; /* Return code */
110694 WhereCost nRowEst; /* Estimated index selectivity */
110695 WhereCost rLogSize; /* Logarithm of table size */
110696 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
110697
110698 pNew = pBuilder->pNew;
110699 if( db->mallocFailed ) return SQLITE_NOMEM;
110700
@@ -110710,11 +110825,11 @@
110710 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
110711
110712 assert( pNew->u.btree.nEq<=pProbe->nColumn );
110713 if( pNew->u.btree.nEq < pProbe->nColumn ){
110714 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
110715 nRowEst = whereCost(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
110716 if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
110717 }else{
110718 iCol = -1;
110719 nRowEst = 0;
110720 }
@@ -110724,11 +110839,11 @@
110724 saved_nLTerm = pNew->nLTerm;
110725 saved_wsFlags = pNew->wsFlags;
110726 saved_prereq = pNew->prereq;
110727 saved_nOut = pNew->nOut;
110728 pNew->rSetup = 0;
110729 rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
110730 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
110731 int nIn = 0;
110732 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110733 int nRecValid = pBuilder->nRecValid;
110734 #endif
@@ -110751,14 +110866,14 @@
110751 if( pTerm->eOperator & WO_IN ){
110752 Expr *pExpr = pTerm->pExpr;
110753 pNew->wsFlags |= WHERE_COLUMN_IN;
110754 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
110755 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
110756 nIn = 46; assert( 46==whereCost(25) );
110757 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
110758 /* "x IN (value, value, ...)" */
110759 nIn = whereCost(pExpr->x.pList->nExpr);
110760 }
110761 pNew->rRun += nIn;
110762 pNew->u.btree.nEq++;
110763 pNew->nOut = nRowEst + nInMul + nIn;
110764 }else if( pTerm->eOperator & (WO_EQ) ){
@@ -110776,11 +110891,11 @@
110776 pNew->nOut = nRowEst + nInMul;
110777 }else if( pTerm->eOperator & (WO_ISNULL) ){
110778 pNew->wsFlags |= WHERE_COLUMN_NULL;
110779 pNew->u.btree.nEq++;
110780 /* TUNING: IS NULL selects 2 rows */
110781 nIn = 10; assert( 10==whereCost(2) );
110782 pNew->nOut = nRowEst + nInMul + nIn;
110783 }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
110784 testcase( pTerm->eOperator & WO_GT );
110785 testcase( pTerm->eOperator & WO_GE );
110786 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
@@ -110796,11 +110911,11 @@
110796 pNew->aLTerm[pNew->nLTerm-2] : 0;
110797 }
110798 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
110799 /* Adjust nOut and rRun for STAT3 range values */
110800 assert( pNew->nOut==saved_nOut );
110801 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut);
110802 }
110803 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110804 if( nInMul==0
110805 && pProbe->nSample
110806 && pNew->u.btree.nEq<=pProbe->nSampleCol
@@ -110816,22 +110931,22 @@
110816 && !ExprHasProperty(pExpr, EP_xIsSelect) ){
110817 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
110818 }
110819 assert( nOut==0 || rc==SQLITE_OK );
110820 if( nOut ){
110821 nOut = whereCost(nOut);
110822 pNew->nOut = MIN(nOut, saved_nOut);
110823 }
110824 }
110825 #endif
110826 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
110827 /* Each row involves a step of the index, then a binary search of
110828 ** the main table */
110829 pNew->rRun = whereCostAdd(pNew->rRun, rLogSize>27 ? rLogSize-17 : 10);
110830 }
110831 /* Step cost for each output row */
110832 pNew->rRun = whereCostAdd(pNew->rRun, pNew->nOut);
110833 whereLoopOutputAdjust(pBuilder->pWC, pNew, pSrc->iCursor);
110834 rc = whereLoopInsert(pBuilder, pNew);
110835 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
110836 && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
110837 ){
@@ -110927,18 +111042,20 @@
110927 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
110928 WhereLoop *pNew; /* Template WhereLoop object */
110929 int rc = SQLITE_OK; /* Return code */
110930 int iSortIdx = 1; /* Index number */
110931 int b; /* A boolean value */
110932 WhereCost rSize; /* number of rows in the table */
110933 WhereCost rLogSize; /* Logarithm of the number of rows in the table */
110934 WhereClause *pWC; /* The parsed WHERE clause */
 
110935
110936 pNew = pBuilder->pNew;
110937 pWInfo = pBuilder->pWInfo;
110938 pTabList = pWInfo->pTabList;
110939 pSrc = pTabList->a + pNew->iTab;
 
110940 pWC = pBuilder->pWC;
110941 assert( !IsVirtual(pSrc->pTab) );
110942
110943 if( pSrc->pIndex ){
110944 /* An INDEXED BY clause specifies a particular index to use */
@@ -110952,22 +111069,22 @@
110952 memset(&sPk, 0, sizeof(Index));
110953 sPk.nColumn = 1;
110954 sPk.aiColumn = &aiColumnPk;
110955 sPk.aiRowEst = aiRowEstPk;
110956 sPk.onError = OE_Replace;
110957 sPk.pTable = pSrc->pTab;
110958 aiRowEstPk[0] = pSrc->pTab->nRowEst;
110959 aiRowEstPk[1] = 1;
110960 pFirst = pSrc->pTab->pIndex;
110961 if( pSrc->notIndexed==0 ){
110962 /* The real indices of the table are only considered if the
110963 ** NOT INDEXED qualifier is omitted from the FROM clause */
110964 sPk.pNext = pFirst;
110965 }
110966 pProbe = &sPk;
110967 }
110968 rSize = whereCost(pSrc->pTab->nRowEst);
110969 rLogSize = estLog(rSize);
110970
110971 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110972 /* Automatic indexes */
110973 if( !pBuilder->pOrSet
@@ -110988,17 +111105,17 @@
110988 pNew->nLTerm = 1;
110989 pNew->aLTerm[0] = pTerm;
110990 /* TUNING: One-time cost for computing the automatic index is
110991 ** approximately 7*N*log2(N) where N is the number of rows in
110992 ** the table being indexed. */
110993 pNew->rSetup = rLogSize + rSize + 28; assert( 28==whereCost(7) );
110994 /* TUNING: Each index lookup yields 20 rows in the table. This
110995 ** is more than the usual guess of 10 rows, since we have no way
110996 ** of knowning how selective the index will ultimately be. It would
110997 ** not be unreasonable to make this value much larger. */
110998 pNew->nOut = 43; assert( 43==whereCost(20) );
110999 pNew->rRun = whereCostAdd(rLogSize,pNew->nOut);
111000 pNew->wsFlags = WHERE_AUTO_INDEX;
111001 pNew->prereq = mExtra | pTerm->prereqRight;
111002 rc = whereLoopInsert(pBuilder, pNew);
111003 }
111004 }
@@ -111028,14 +111145,12 @@
111028
111029 /* Full table scan */
111030 pNew->iSortIdx = b ? iSortIdx : 0;
111031 /* TUNING: Cost of full table scan is 3*(N + log2(N)).
111032 ** + The extra 3 factor is to encourage the use of indexed lookups
111033 ** over full scans. A smaller constant 2 is used for covering
111034 ** index scans so that a covering index scan will be favored over
111035 ** a table scan. */
111036 pNew->rRun = whereCostAdd(rSize,rLogSize) + 16;
111037 whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor);
111038 rc = whereLoopInsert(pBuilder, pNew);
111039 pNew->nOut = rSize;
111040 if( rc ) break;
111041 }else{
@@ -111044,26 +111159,25 @@
111044
111045 /* Full scan via index */
111046 if( b
111047 || ( m==0
111048 && pProbe->bUnordered==0
 
111049 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
111050 && sqlite3GlobalConfig.bUseCis
111051 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
111052 )
111053 ){
111054 pNew->iSortIdx = b ? iSortIdx : 0;
111055 if( m==0 ){
111056 /* TUNING: Cost of a covering index scan is 2*(N + log2(N)).
111057 ** + The extra 2 factor is to encourage the use of indexed lookups
111058 ** over index scans. A table scan uses a factor of 3 so that
111059 ** index scans are favored over table scans.
111060 ** + If this covering index might also help satisfy the ORDER BY
111061 ** clause, then the cost is fudged down slightly so that this
111062 ** index is favored above other indices that have no hope of
111063 ** helping with the ORDER BY. */
111064 pNew->rRun = 10 + whereCostAdd(rSize,rLogSize) - b;
111065 }else{
111066 assert( b!=0 );
111067 /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
111068 ** which we will simplify to just N*log2(N) */
111069 pNew->rRun = rSize + rLogSize;
@@ -111237,13 +111351,13 @@
111237 pIdxInfo->needToFreeIdxStr = 0;
111238 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
111239 pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
111240 && pIdxInfo->orderByConsumed);
111241 pNew->rSetup = 0;
111242 pNew->rRun = whereCostFromDouble(pIdxInfo->estimatedCost);
111243 /* TUNING: Every virtual table query returns 25 rows */
111244 pNew->nOut = 46; assert( 46==whereCost(25) );
111245 whereLoopInsert(pBuilder, pNew);
111246 if( pNew->u.vtab.needFree ){
111247 sqlite3_free(pNew->u.vtab.idxStr);
111248 pNew->u.vtab.needFree = 0;
111249 }
@@ -111276,10 +111390,12 @@
111276 pWC = pBuilder->pWC;
111277 if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
111278 pWCEnd = pWC->a + pWC->nTerm;
111279 pNew = pBuilder->pNew;
111280 memset(&sSum, 0, sizeof(sSum));
 
 
111281
111282 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
111283 if( (pTerm->eOperator & WO_OR)!=0
111284 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
111285 ){
@@ -111287,12 +111403,10 @@
111287 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
111288 WhereTerm *pOrTerm;
111289 int once = 1;
111290 int i, j;
111291
111292 pItem = pWInfo->pTabList->a + pNew->iTab;
111293 iCur = pItem->iCursor;
111294 sSubBuild = *pBuilder;
111295 sSubBuild.pOrderBy = 0;
111296 sSubBuild.pOrSet = &sCur;
111297
111298 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
@@ -111329,12 +111443,12 @@
111329 whereOrMove(&sPrev, &sSum);
111330 sSum.n = 0;
111331 for(i=0; i<sPrev.n; i++){
111332 for(j=0; j<sCur.n; j++){
111333 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
111334 whereCostAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
111335 whereCostAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
111336 }
111337 }
111338 }
111339 }
111340 pNew->nLTerm = 1;
@@ -111668,23 +111782,23 @@
111668 ** costs if nRowEst==0.
111669 **
111670 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
111671 ** error occurs.
111672 */
111673 static int wherePathSolver(WhereInfo *pWInfo, WhereCost nRowEst){
111674 int mxChoice; /* Maximum number of simultaneous paths tracked */
111675 int nLoop; /* Number of terms in the join */
111676 Parse *pParse; /* Parsing context */
111677 sqlite3 *db; /* The database connection */
111678 int iLoop; /* Loop counter over the terms of the join */
111679 int ii, jj; /* Loop counters */
111680 int mxI = 0; /* Index of next entry to replace */
111681 WhereCost rCost; /* Cost of a path */
111682 WhereCost nOut; /* Number of outputs */
111683 WhereCost mxCost = 0; /* Maximum cost of a set of paths */
111684 WhereCost mxOut = 0; /* Maximum nOut value on the set of paths */
111685 WhereCost rSortCost; /* Cost to do a sort */
111686 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
111687 WherePath *aFrom; /* All nFrom paths at the previous level */
111688 WherePath *aTo; /* The nTo best paths at the current level */
111689 WherePath *pFrom; /* An element of aFrom[] that we are working on */
111690 WherePath *pTo; /* An element of aTo[] that we are working on */
@@ -111717,21 +111831,23 @@
111717 /* Seed the search with a single WherePath containing zero WhereLoops.
111718 **
111719 ** TUNING: Do not let the number of iterations go above 25. If the cost
111720 ** of computing an automatic index is not paid back within the first 25
111721 ** rows, then do not use the automatic index. */
111722 aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==whereCost(25) );
111723 nFrom = 1;
111724
111725 /* Precompute the cost of sorting the final result set, if the caller
111726 ** to sqlite3WhereBegin() was concerned about sorting */
111727 rSortCost = 0;
111728 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
111729 aFrom[0].isOrderedValid = 1;
111730 }else{
111731 /* TUNING: Estimated cost of sorting is N*log2(N) where N is the
111732 ** number of output rows. */
 
 
111733 rSortCost = nRowEst + estLog(nRowEst);
111734 WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
111735 }
111736
111737 /* Compute successively longer WherePaths using the previous generation
@@ -111747,12 +111863,12 @@
111747 u8 isOrdered = pFrom->isOrdered;
111748 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
111749 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
111750 /* At this point, pWLoop is a candidate to be the next loop.
111751 ** Compute its cost */
111752 rCost = whereCostAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
111753 rCost = whereCostAdd(rCost, pFrom->rCost);
111754 nOut = pFrom->nRow + pWLoop->nOut;
111755 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
111756 if( !isOrderedValid ){
111757 switch( wherePathSatisfiesOrderBy(pWInfo,
111758 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
@@ -111762,11 +111878,11 @@
111762 isOrderedValid = 1;
111763 break;
111764 case 0: /* No. pFrom+pWLoop will require a separate sort */
111765 isOrdered = 0;
111766 isOrderedValid = 1;
111767 rCost = whereCostAdd(rCost, rSortCost);
111768 break;
111769 default: /* Cannot tell yet. Try again on the next iteration */
111770 break;
111771 }
111772 }else{
@@ -111969,11 +112085,11 @@
111969 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
111970 pLoop->aLTerm[0] = pTerm;
111971 pLoop->nLTerm = 1;
111972 pLoop->u.btree.nEq = 1;
111973 /* TUNING: Cost of a rowid lookup is 10 */
111974 pLoop->rRun = 33; /* 33==whereCost(10) */
111975 }else{
111976 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111977 assert( pLoop->aLTermSpace==pLoop->aLTerm );
111978 assert( ArraySize(pLoop->aLTermSpace)==4 );
111979 if( pIdx->onError==OE_None
@@ -111992,16 +112108,16 @@
111992 }
111993 pLoop->nLTerm = j;
111994 pLoop->u.btree.nEq = j;
111995 pLoop->u.btree.pIndex = pIdx;
111996 /* TUNING: Cost of a unique index lookup is 15 */
111997 pLoop->rRun = 39; /* 39==whereCost(15) */
111998 break;
111999 }
112000 }
112001 if( pLoop->wsFlags ){
112002 pLoop->nOut = (WhereCost)1;
112003 pWInfo->a[0].pWLoop = pLoop;
112004 pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
112005 pWInfo->a[0].iTabCur = iCur;
112006 pWInfo->nRowOut = 1;
112007 if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1;
112008
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -656,11 +656,11 @@
656 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657 ** [sqlite_version()] and [sqlite_source_id()].
658 */
659 #define SQLITE_VERSION "3.8.1"
660 #define SQLITE_VERSION_NUMBER 3008001
661 #define SQLITE_SOURCE_ID "2013-10-10 15:04:52 af7abebeb1f70466833bc766d294d721eaef746f"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -8272,10 +8272,35 @@
8272 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
8273 #else
8274 typedef u32 tRowcnt; /* 32-bit is the default */
8275 #endif
8276
8277 /*
8278 ** Estimated quantities used for query planning are stored as 16-bit
8279 ** logarithms. For quantity X, the value stored is 10*log2(X). This
8280 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
8281 ** But the allowed values are "grainy". Not every value is representable.
8282 ** For example, quantities 16 and 17 are both represented by a LogEst
8283 ** of 40. However, since LogEst quantatites are suppose to be estimates,
8284 ** not exact values, this imprecision is not a problem.
8285 **
8286 ** "LogEst" is short for "Logarithimic Estimate".
8287 **
8288 ** Examples:
8289 ** 1 -> 0 20 -> 43 10000 -> 132
8290 ** 2 -> 10 25 -> 46 25000 -> 146
8291 ** 3 -> 16 100 -> 66 1000000 -> 199
8292 ** 4 -> 20 1000 -> 99 1048576 -> 200
8293 ** 10 -> 33 1024 -> 100 4294967296 -> 320
8294 **
8295 ** The LogEst can be negative to indicate fractional values.
8296 ** Examples:
8297 **
8298 ** 0.5 -> -10 0.1 -> -33 0.0625 -> -40
8299 */
8300 typedef INT16_TYPE LogEst;
8301
8302 /*
8303 ** Macros to determine whether the machine is big or little endian,
8304 ** evaluated at runtime.
8305 */
8306 #ifdef SQLITE_AMALGAMATION
@@ -10419,11 +10444,12 @@
10444 char *zDflt; /* Original text of the default value */
10445 char *zType; /* Data type for this column */
10446 char *zColl; /* Collating sequence. If NULL, use the default */
10447 u8 notNull; /* An OE_ code for handling a NOT NULL constraint */
10448 char affinity; /* One of the SQLITE_AFF_... values */
10449 u8 szEst; /* Estimated size of this column. INT==1 */
10450 u8 colFlags; /* Boolean properties. See COLFLAG_ defines below */
10451 };
10452
10453 /* Allowed values for Column.colFlags:
10454 */
10455 #define COLFLAG_PRIMKEY 0x0001 /* Column is part of the primary key */
@@ -10583,10 +10609,11 @@
10609 tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
10610 int tnum; /* Root BTree node for this table (see note above) */
10611 i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
10612 i16 nCol; /* Number of columns in this table */
10613 u16 nRef; /* Number of pointers to this Table */
10614 LogEst szTabRow; /* Estimated size of each table row in bytes */
10615 u8 tabFlags; /* Mask of TF_* values */
10616 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
10617 #ifndef SQLITE_OMIT_ALTERTABLE
10618 int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */
10619 #endif
@@ -10694,11 +10721,11 @@
10721 #define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
10722 #define OE_SetNull 7 /* Set the foreign key value to NULL */
10723 #define OE_SetDflt 8 /* Set the foreign key value to its default */
10724 #define OE_Cascade 9 /* Cascade the changes */
10725
10726 #define OE_Default 10 /* Do whatever the default action is */
10727
10728
10729 /*
10730 ** An instance of the following structure is passed as the first
10731 ** argument to sqlite3VdbeKeyCompare and is used to control the
@@ -10781,10 +10808,11 @@
10808 Schema *pSchema; /* Schema containing this index */
10809 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
10810 char **azColl; /* Array of collation sequence names for index */
10811 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
10812 int tnum; /* DB Page containing root of this index */
10813 LogEst szIdxRow; /* Estimated average row size in bytes */
10814 u16 nColumn; /* Number of columns in table used by this index */
10815 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10816 unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
10817 unsigned bUnordered:1; /* Use this index for == or IN queries only */
10818 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
@@ -12187,10 +12215,16 @@
12215 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
12216 SQLITE_PRIVATE int sqlite3Atoi(const char*);
12217 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
12218 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
12219 SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
12220 SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
12221 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
12222 #ifndef SQLITE_OMIT_VIRTUALTABLE
12223 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
12224 #endif
12225 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
12226
12227 /*
12228 ** Routines to read and write variable-length integers. These used to
12229 ** be defined locally, but now we use the varint routines in the util.c
12230 ** file. Code should use the MACRO forms below, as the Varint32 versions
@@ -12303,11 +12337,11 @@
12337 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
12338 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
12339 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
12340 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
12341 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
12342 SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
12343 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
12344 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
12345 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
12346 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
12347 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
@@ -22388,10 +22422,87 @@
22422 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
22423 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
22424 }
22425 }
22426 #endif
22427
22428 /*
22429 ** Find (an approximate) sum of two LogEst values. This computation is
22430 ** not a simple "+" operator because LogEst is stored as a logarithmic
22431 ** value.
22432 **
22433 */
22434 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
22435 static const unsigned char x[] = {
22436 10, 10, /* 0,1 */
22437 9, 9, /* 2,3 */
22438 8, 8, /* 4,5 */
22439 7, 7, 7, /* 6,7,8 */
22440 6, 6, 6, /* 9,10,11 */
22441 5, 5, 5, /* 12-14 */
22442 4, 4, 4, 4, /* 15-18 */
22443 3, 3, 3, 3, 3, 3, /* 19-24 */
22444 2, 2, 2, 2, 2, 2, 2, /* 25-31 */
22445 };
22446 if( a>=b ){
22447 if( a>b+49 ) return a;
22448 if( a>b+31 ) return a+1;
22449 return a+x[a-b];
22450 }else{
22451 if( b>a+49 ) return b;
22452 if( b>a+31 ) return b+1;
22453 return b+x[b-a];
22454 }
22455 }
22456
22457 /*
22458 ** Convert an integer into a LogEst. In other words, compute a
22459 ** good approximatation for 10*log2(x).
22460 */
22461 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
22462 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
22463 LogEst y = 40;
22464 if( x<8 ){
22465 if( x<2 ) return 0;
22466 while( x<8 ){ y -= 10; x <<= 1; }
22467 }else{
22468 while( x>255 ){ y += 40; x >>= 4; }
22469 while( x>15 ){ y += 10; x >>= 1; }
22470 }
22471 return a[x&7] + y - 10;
22472 }
22473
22474 #ifndef SQLITE_OMIT_VIRTUALTABLE
22475 /*
22476 ** Convert a double into a LogEst
22477 ** In other words, compute an approximation for 10*log2(x).
22478 */
22479 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
22480 u64 a;
22481 LogEst e;
22482 assert( sizeof(x)==8 && sizeof(a)==8 );
22483 if( x<=1 ) return 0;
22484 if( x<=2000000000 ) return sqlite3LogEst((u64)x);
22485 memcpy(&a, &x, 8);
22486 e = (a>>52) - 1022;
22487 return e*10;
22488 }
22489 #endif /* SQLITE_OMIT_VIRTUALTABLE */
22490
22491 /*
22492 ** Convert a LogEst into an integer.
22493 */
22494 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
22495 u64 n;
22496 if( x<10 ) return 1;
22497 n = x%10;
22498 x /= 10;
22499 if( n>=5 ) n -= 2;
22500 else if( n>=1 ) n -= 1;
22501 if( x>=3 ) return (n+8)<<(x-3);
22502 return (n+8)>>(3-x);
22503 }
22504
22505 /************** End of util.c ************************************************/
22506 /************** Begin file hash.c ********************************************/
22507 /*
22508 ** 2001 September 22
@@ -76084,11 +76195,11 @@
76195 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
76196 }
76197 #ifndef SQLITE_OMIT_CAST
76198 if( op==TK_CAST ){
76199 assert( !ExprHasProperty(pExpr, EP_IntValue) );
76200 return sqlite3AffinityType(pExpr->u.zToken, 0);
76201 }
76202 #endif
76203 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
76204 && pExpr->pTab!=0
76205 ){
@@ -78504,11 +78615,11 @@
78615 case TK_CAST: {
78616 /* Expressions of the form: CAST(pLeft AS token) */
78617 int aff, to_op;
78618 inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
78619 assert( !ExprHasProperty(pExpr, EP_IntValue) );
78620 aff = sqlite3AffinityType(pExpr->u.zToken, 0);
78621 to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
78622 assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
78623 assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
78624 assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
78625 assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
@@ -79171,11 +79282,11 @@
79282 }
79283 #ifndef SQLITE_OMIT_CAST
79284 case TK_CAST: {
79285 /* Expressions of the form: CAST(pLeft AS token) */
79286 const char *zAff = "unk";
79287 switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){
79288 case SQLITE_AFF_TEXT: zAff = "TEXT"; break;
79289 case SQLITE_AFF_NONE: zAff = "NONE"; break;
79290 case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break;
79291 case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break;
79292 case SQLITE_AFF_REAL: zAff = "REAL"; break;
@@ -81886,16 +81997,16 @@
81997 if( zRet==0 ){
81998 sqlite3_result_error_nomem(context);
81999 return;
82000 }
82001
82002 sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
82003 z = zRet + sqlite3Strlen30(zRet);
82004 for(i=0; i<(p->nCol-1); i++){
82005 u64 nDistinct = p->current.anDLt[i] + 1;
82006 u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
82007 sqlite3_snprintf(24, z, " %llu", iVal);
82008 z += sqlite3Strlen30(z);
82009 assert( p->current.anEq[i] );
82010 }
82011 assert( z[0]=='\0' && z>zRet );
82012
@@ -81932,11 +82043,11 @@
82043 sqlite3_result_error_nomem(context);
82044 }else{
82045 int i;
82046 char *z = zRet;
82047 for(i=0; i<p->nCol; i++){
82048 sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
82049 z += sqlite3Strlen30(z);
82050 }
82051 assert( z[0]=='\0' && z>zRet );
82052 z[-1] = '\0';
82053 sqlite3_result_text(context, zRet, -1, sqlite3_free);
@@ -82393,22 +82504,20 @@
82504 ** The first argument points to a nul-terminated string containing a
82505 ** list of space separated integers. Read the first nOut of these into
82506 ** the array aOut[].
82507 */
82508 static void decodeIntArray(
82509 char *zIntArray, /* String containing int array to decode */
82510 int nOut, /* Number of slots in aOut[] */
82511 tRowcnt *aOut, /* Store integers here */
82512 Index *pIndex /* Handle extra flags for this index, if not NULL */
82513 ){
82514 char *z = zIntArray;
82515 int c;
82516 int i;
82517 tRowcnt v;
82518
 
 
82519 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82520 if( z==0 ) z = "";
82521 #else
82522 if( NEVER(z==0) ) z = "";
82523 #endif
@@ -82419,12 +82528,23 @@
82528 z++;
82529 }
82530 aOut[i] = v;
82531 if( *z==' ' ) z++;
82532 }
82533 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
82534 assert( pIndex!=0 );
82535 #else
82536 if( pIndex )
82537 #endif
82538 {
82539 if( strcmp(z, "unordered")==0 ){
82540 pIndex->bUnordered = 1;
82541 }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
82542 int v32 = 0;
82543 sqlite3GetInt32(z+3, &v32);
82544 pIndex->szIdxRow = sqlite3LogEst(v32);
82545 }
82546 }
82547 }
82548
82549 /*
82550 ** This callback is invoked once for each index when reading the
@@ -82459,16 +82579,17 @@
82579 pIndex = 0;
82580 }
82581 z = argv[2];
82582
82583 if( pIndex ){
82584 decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst, pIndex);
 
82585 if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
 
82586 }else{
82587 Index fakeIdx;
82588 fakeIdx.szIdxRow = pTable->szTabRow;
82589 decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
82590 pTable->szTabRow = fakeIdx.szIdxRow;
82591 }
82592
82593 return 0;
82594 }
82595
@@ -84480,11 +84601,11 @@
84601 }
84602 pTable->zName = zName;
84603 pTable->iPKey = -1;
84604 pTable->pSchema = db->aDb[iDb].pSchema;
84605 pTable->nRef = 1;
84606 pTable->nRowEst = 1048576;
84607 assert( pParse->pNewTable==0 );
84608 pParse->pNewTable = pTable;
84609
84610 /* If this is the magic sqlite_sequence table used by autoincrement,
84611 ** then record a pointer to this table in the main database structure
@@ -84627,10 +84748,11 @@
84748 /* If there is no type specified, columns have the default affinity
84749 ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
84750 ** be called next to set pCol->affinity correctly.
84751 */
84752 pCol->affinity = SQLITE_AFF_NONE;
84753 pCol->szEst = 1;
84754 p->nCol++;
84755 }
84756
84757 /*
84758 ** This routine is called by the parser while in the middle of
@@ -84668,26 +84790,30 @@
84790 ** 'DOUB' | SQLITE_AFF_REAL
84791 **
84792 ** If none of the substrings in the above table are found,
84793 ** SQLITE_AFF_NUMERIC is returned.
84794 */
84795 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
84796 u32 h = 0;
84797 char aff = SQLITE_AFF_NUMERIC;
84798 const char *zChar = 0;
84799
84800 if( zIn==0 ) return aff;
84801 while( zIn[0] ){
84802 h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
84803 zIn++;
84804 if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */
84805 aff = SQLITE_AFF_TEXT;
84806 zChar = zIn;
84807 }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */
84808 aff = SQLITE_AFF_TEXT;
84809 }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */
84810 aff = SQLITE_AFF_TEXT;
84811 }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */
84812 && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
84813 aff = SQLITE_AFF_NONE;
84814 if( zIn[0]=='(' ) zChar = zIn;
84815 #ifndef SQLITE_OMIT_FLOATING_POINT
84816 }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */
84817 && aff==SQLITE_AFF_NUMERIC ){
84818 aff = SQLITE_AFF_REAL;
84819 }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */
@@ -84701,10 +84827,32 @@
84827 aff = SQLITE_AFF_INTEGER;
84828 break;
84829 }
84830 }
84831
84832 /* If pszEst is not NULL, store an estimate of the field size. The
84833 ** estimate is scaled so that the size of an integer is 1. */
84834 if( pszEst ){
84835 *pszEst = 1; /* default size is approx 4 bytes */
84836 if( aff<=SQLITE_AFF_NONE ){
84837 if( zChar ){
84838 while( zChar[0] ){
84839 if( sqlite3Isdigit(zChar[0]) ){
84840 int v;
84841 sqlite3GetInt32(zChar, &v);
84842 v = v/4 + 1;
84843 if( v>255 ) v = 255;
84844 *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
84845 break;
84846 }
84847 zChar++;
84848 }
84849 }else{
84850 *pszEst = 5; /* BLOB, TEXT, CLOB -> r=5 (approx 20 bytes)*/
84851 }
84852 }
84853 }
84854 return aff;
84855 }
84856
84857 /*
84858 ** This routine is called by the parser while in the middle of
@@ -84722,11 +84870,11 @@
84870 p = pParse->pNewTable;
84871 if( p==0 || NEVER(p->nCol<1) ) return;
84872 pCol = &p->aCol[p->nCol-1];
84873 assert( pCol->zType==0 );
84874 pCol->zType = sqlite3NameFromToken(pParse->db, pType);
84875 pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
84876 }
84877
84878 /*
84879 ** The expression is the default value for the most recently added column
84880 ** of the table currently under construction.
@@ -85070,18 +85218,46 @@
85218 testcase( pCol->affinity==SQLITE_AFF_REAL );
85219
85220 zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
85221 len = sqlite3Strlen30(zType);
85222 assert( pCol->affinity==SQLITE_AFF_NONE
85223 || pCol->affinity==sqlite3AffinityType(zType, 0) );
85224 memcpy(&zStmt[k], zType, len);
85225 k += len;
85226 assert( k<=n );
85227 }
85228 sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
85229 return zStmt;
85230 }
85231
85232 /*
85233 ** Estimate the total row width for a table.
85234 */
85235 static void estimateTableWidth(Table *pTab){
85236 unsigned wTable = 0;
85237 const Column *pTabCol;
85238 int i;
85239 for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
85240 wTable += pTabCol->szEst;
85241 }
85242 if( pTab->iPKey<0 ) wTable++;
85243 pTab->szTabRow = sqlite3LogEst(wTable*4);
85244 }
85245
85246 /*
85247 ** Estimate the average size of a row for an index.
85248 */
85249 static void estimateIndexWidth(Index *pIdx){
85250 unsigned wIndex = 1;
85251 int i;
85252 const Column *aCol = pIdx->pTable->aCol;
85253 for(i=0; i<pIdx->nColumn; i++){
85254 assert( pIdx->aiColumn[i]>=0 && pIdx->aiColumn[i]<pIdx->pTable->nCol );
85255 wIndex += aCol[pIdx->aiColumn[i]].szEst;
85256 }
85257 pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
85258 }
85259
85260 /*
85261 ** This routine is called to report the final ")" that terminates
85262 ** a CREATE TABLE statement.
85263 **
@@ -85105,13 +85281,14 @@
85281 Parse *pParse, /* Parse context */
85282 Token *pCons, /* The ',' token after the last column defn. */
85283 Token *pEnd, /* The final ')' token in the CREATE TABLE */
85284 Select *pSelect /* Select from a "CREATE ... AS SELECT" */
85285 ){
85286 Table *p; /* The new table */
85287 sqlite3 *db = pParse->db; /* The database connection */
85288 int iDb; /* Database in which the table lives */
85289 Index *pIdx; /* An implied index of the table */
85290
85291 if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
85292 return;
85293 }
85294 p = pParse->pNewTable;
@@ -85126,10 +85303,16 @@
85303 */
85304 if( p->pCheck ){
85305 sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
85306 }
85307 #endif /* !defined(SQLITE_OMIT_CHECK) */
85308
85309 /* Estimate the average row size for the table and for all implied indices */
85310 estimateTableWidth(p);
85311 for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
85312 estimateIndexWidth(pIdx);
85313 }
85314
85315 /* If the db->init.busy is 1 it means we are reading the SQL off the
85316 ** "sqlite_master" or "sqlite_temp_master" table on the disk.
85317 ** So do not write to the disk again. Extract the root page number
85318 ** for the table from the db->init.newTnum field. (The page number
@@ -86085,13 +86268,14 @@
86268 sqlite3 *db = pParse->db;
86269 Db *pDb; /* The specific table containing the indexed database */
86270 int iDb; /* Index of the database that is being written */
86271 Token *pName = 0; /* Unqualified name of the index to create */
86272 struct ExprList_item *pListItem; /* For looping over pList */
86273 const Column *pTabCol; /* A column in the table */
86274 int nCol; /* Number of columns */
86275 int nExtra = 0; /* Space allocated for zExtra[] */
86276 char *zExtra; /* Extra space after the Index object */
86277
86278 assert( pParse->nErr==0 ); /* Never called with prior errors */
86279 if( db->mallocFailed || IN_DECLARE_VTAB ){
86280 goto exit_create_index;
86281 }
@@ -86314,11 +86498,10 @@
86498 ** same column more than once cannot be an error because that would
86499 ** break backwards compatibility - it needs to be a warning.
86500 */
86501 for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
86502 const char *zColName = pListItem->zName;
 
86503 int requestedSortOrder;
86504 char *zColl; /* Collation sequence name */
86505
86506 for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
86507 if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
@@ -86351,10 +86534,11 @@
86534 requestedSortOrder = pListItem->sortOrder & sortOrderMask;
86535 pIndex->aSortOrder[i] = (u8)requestedSortOrder;
86536 if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
86537 }
86538 sqlite3DefaultRowEst(pIndex);
86539 if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
86540
86541 if( pTab==pParse->pNewTable ){
86542 /* This routine has been called to create an automatic index as a
86543 ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
86544 ** a PRIMARY KEY or UNIQUE clause following the column definitions.
@@ -96379,30 +96563,34 @@
96563 break;
96564
96565 case PragTyp_INDEX_LIST: if( zRight ){
96566 Index *pIdx;
96567 Table *pTab;
96568 int i;
96569 pTab = sqlite3FindTable(db, zRight, zDb);
96570 if( pTab ){
96571 v = sqlite3GetVdbe(pParse);
96572 sqlite3VdbeSetNumCols(v, 4);
96573 pParse->nMem = 4;
96574 sqlite3CodeVerifySchema(pParse, iDb);
96575 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
96576 sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
96577 sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
96578 sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "avgrowsize", SQLITE_STATIC);
96579 sqlite3VdbeAddOp2(v, OP_Integer, 0, 1);
96580 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
96581 sqlite3VdbeAddOp2(v, OP_Integer, 1, 3);
96582 sqlite3VdbeAddOp2(v, OP_Integer,
96583 (int)sqlite3LogEstToInt(pTab->szTabRow), 4);
96584 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
96585 for(pIdx=pTab->pIndex, i=1; pIdx; pIdx=pIdx->pNext, i++){
96586 sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
96587 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
96588 sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
96589 sqlite3VdbeAddOp2(v, OP_Integer,
96590 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 4);
96591 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
96592 }
96593 }
96594 }
96595 break;
96596
@@ -99086,10 +99274,13 @@
99274 }
99275
99276 /*
99277 ** Return a pointer to a string containing the 'declaration type' of the
99278 ** expression pExpr. The string may be treated as static by the caller.
99279 **
99280 ** Also try to estimate the size of the returned value and return that
99281 ** result in *pEstWidth.
99282 **
99283 ** The declaration type is the exact datatype definition extracted from the
99284 ** original CREATE TABLE statement if the expression is a column. The
99285 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
99286 ** is considered a column can be complex in the presence of subqueries. The
@@ -99100,25 +99291,40 @@
99291 ** SELECT (SELECT col FROM tbl;
99292 ** SELECT (SELECT col FROM tbl);
99293 ** SELECT abc FROM (SELECT col AS abc FROM tbl);
99294 **
99295 ** The declaration type for any expression other than a column is NULL.
99296 **
99297 ** This routine has either 3 or 6 parameters depending on whether or not
99298 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
99299 */
99300 #ifdef SQLITE_ENABLE_COLUMN_METADATA
99301 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
99302 static const char *columnTypeImpl(
99303 NameContext *pNC,
99304 Expr *pExpr,
99305 const char **pzOrigDb,
99306 const char **pzOrigTab,
99307 const char **pzOrigCol,
99308 u8 *pEstWidth
99309 ){
99310 char const *zOrigDb = 0;
99311 char const *zOrigTab = 0;
99312 char const *zOrigCol = 0;
99313 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
99314 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
99315 static const char *columnTypeImpl(
99316 NameContext *pNC,
99317 Expr *pExpr,
99318 u8 *pEstWidth
 
 
99319 ){
99320 #endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
99321 char const *zType = 0;
 
 
 
99322 int j;
99323 u8 estWidth = 1;
99324
99325 if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
 
99326 switch( pExpr->op ){
99327 case TK_AGG_COLUMN:
99328 case TK_COLUMN: {
99329 /* The expression is a column. Locate the table the column is being
99330 ** extracted from in NameContext.pSrcList. This table may be real
@@ -99175,29 +99381,39 @@
99381 NameContext sNC;
99382 Expr *p = pS->pEList->a[iCol].pExpr;
99383 sNC.pSrcList = pS->pSrc;
99384 sNC.pNext = pNC;
99385 sNC.pParse = pNC->pParse;
99386 zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth);
99387 }
99388 }else if( ALWAYS(pTab->pSchema) ){
99389 /* A real table */
99390 assert( !pS );
99391 if( iCol<0 ) iCol = pTab->iPKey;
99392 assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
99393 #ifdef SQLITE_ENABLE_COLUMN_METADATA
99394 if( iCol<0 ){
99395 zType = "INTEGER";
99396 zOrigCol = "rowid";
99397 }else{
99398 zType = pTab->aCol[iCol].zType;
99399 zOrigCol = pTab->aCol[iCol].zName;
99400 estWidth = pTab->aCol[iCol].szEst;
99401 }
99402 zOrigTab = pTab->zName;
99403 if( pNC->pParse ){
99404 int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
99405 zOrigDb = pNC->pParse->db->aDb[iDb].zName;
99406 }
99407 #else
99408 if( iCol<0 ){
99409 zType = "INTEGER";
99410 }else{
99411 zType = pTab->aCol[iCol].zType;
99412 estWidth = pTab->aCol[iCol].szEst;
99413 }
99414 #endif
99415 }
99416 break;
99417 }
99418 #ifndef SQLITE_OMIT_SUBQUERY
99419 case TK_SELECT: {
@@ -99210,22 +99426,25 @@
99426 Expr *p = pS->pEList->a[0].pExpr;
99427 assert( ExprHasProperty(pExpr, EP_xIsSelect) );
99428 sNC.pSrcList = pS->pSrc;
99429 sNC.pNext = pNC;
99430 sNC.pParse = pNC->pParse;
99431 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth);
99432 break;
99433 }
99434 #endif
99435 }
99436
99437 #ifdef SQLITE_ENABLE_COLUMN_METADATA
99438 if( pzOrigDb ){
99439 assert( pzOrigTab && pzOrigCol );
99440 *pzOrigDb = zOrigDb;
99441 *pzOrigTab = zOrigTab;
99442 *pzOrigCol = zOrigCol;
99443 }
99444 #endif
99445 if( pEstWidth ) *pEstWidth = estWidth;
99446 return zType;
99447 }
99448
99449 /*
99450 ** Generate code that will tell the VDBE the declaration types of columns
@@ -99247,25 +99466,25 @@
99466 const char *zType;
99467 #ifdef SQLITE_ENABLE_COLUMN_METADATA
99468 const char *zOrigDb = 0;
99469 const char *zOrigTab = 0;
99470 const char *zOrigCol = 0;
99471 zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
99472
99473 /* The vdbe must make its own copy of the column-type and other
99474 ** column specific strings, in case the schema is reset before this
99475 ** virtual machine is deleted.
99476 */
99477 sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
99478 sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
99479 sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
99480 #else
99481 zType = columnType(&sNC, p, 0, 0, 0, 0);
99482 #endif
99483 sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
99484 }
99485 #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
99486 }
99487
99488 /*
99489 ** Generate code that will tell the VDBE the names of columns
99490 ** in the result set. This information is used to provide the
@@ -99450,39 +99669,41 @@
99669 ** This routine requires that all identifiers in the SELECT
99670 ** statement be resolved.
99671 */
99672 static void selectAddColumnTypeAndCollation(
99673 Parse *pParse, /* Parsing contexts */
99674 Table *pTab, /* Add column type information to this table */
 
99675 Select *pSelect /* SELECT used to determine types and collations */
99676 ){
99677 sqlite3 *db = pParse->db;
99678 NameContext sNC;
99679 Column *pCol;
99680 CollSeq *pColl;
99681 int i;
99682 Expr *p;
99683 struct ExprList_item *a;
99684 u64 szAll = 0;
99685
99686 assert( pSelect!=0 );
99687 assert( (pSelect->selFlags & SF_Resolved)!=0 );
99688 assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
99689 if( db->mallocFailed ) return;
99690 memset(&sNC, 0, sizeof(sNC));
99691 sNC.pSrcList = pSelect->pSrc;
99692 a = pSelect->pEList->a;
99693 for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
99694 p = a[i].pExpr;
99695 pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
99696 szAll += pCol->szEst;
99697 pCol->affinity = sqlite3ExprAffinity(p);
99698 if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
99699 pColl = sqlite3ExprCollSeq(pParse, p);
99700 if( pColl ){
99701 pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
99702 }
99703 }
99704 pTab->szTabRow = sqlite3LogEst(szAll*4);
99705 }
99706
99707 /*
99708 ** Given a SELECT statement, generate a Table structure that describes
99709 ** the result set of that SELECT.
@@ -99506,13 +99727,13 @@
99727 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
99728 ** is disabled */
99729 assert( db->lookaside.bEnabled==0 );
99730 pTab->nRef = 1;
99731 pTab->zName = 0;
99732 pTab->nRowEst = 1048576;
99733 selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
99734 selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
99735 pTab->iPKey = -1;
99736 if( db->mallocFailed ){
99737 sqlite3DeleteTable(db, pTab);
99738 return 0;
99739 }
@@ -101420,15 +101641,15 @@
101641 assert( pFrom->pTab==0 );
101642 sqlite3WalkSelect(pWalker, pSel);
101643 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
101644 if( pTab==0 ) return WRC_Abort;
101645 pTab->nRef = 1;
101646 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
101647 while( pSel->pPrior ){ pSel = pSel->pPrior; }
101648 selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
101649 pTab->iPKey = -1;
101650 pTab->nRowEst = 1048576;
101651 pTab->tabFlags |= TF_Ephemeral;
101652 #endif
101653 }else{
101654 /* An ordinary table or view name in the FROM clause */
101655 assert( pFrom->pTab==0 );
@@ -101708,11 +101929,11 @@
101929 if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
101930 /* A sub-query in the FROM clause of a SELECT */
101931 Select *pSel = pFrom->pSelect;
101932 assert( pSel );
101933 while( pSel->pPrior ) pSel = pSel->pPrior;
101934 selectAddColumnTypeAndCollation(pParse, pTab, pSel);
101935 }
101936 }
101937 }
101938 return WRC_Continue;
101939 }
@@ -102623,17 +102844,11 @@
102844 int iRoot = pTab->tnum; /* Root page of scanned b-tree */
102845
102846 sqlite3CodeVerifySchema(pParse, iDb);
102847 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
102848
102849 /* Search for the index that has the lowest scan cost.
 
 
 
 
 
 
102850 **
102851 ** (2011-04-15) Do not do a full scan of an unordered index.
102852 **
102853 ** (2013-10-03) Do not count the entires in a partial index.
102854 **
@@ -102640,17 +102855,18 @@
102855 ** In practice the KeyInfo structure will not be used. It is only
102856 ** passed to keep OP_OpenRead happy.
102857 */
102858 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
102859 if( pIdx->bUnordered==0
102860 && pIdx->szIdxRow<pTab->szTabRow
102861 && pIdx->pPartIdxWhere==0
102862 && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
102863 ){
102864 pBest = pIdx;
102865 }
102866 }
102867 if( pBest ){
102868 iRoot = pBest->tnum;
102869 pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest);
102870 }
102871
102872 /* Open a read-only cursor, execute the OP_Count, close the cursor. */
@@ -106406,30 +106622,10 @@
106622 typedef struct WhereLoopBuilder WhereLoopBuilder;
106623 typedef struct WhereScan WhereScan;
106624 typedef struct WhereOrCost WhereOrCost;
106625 typedef struct WhereOrSet WhereOrSet;
106626
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106627 /*
106628 ** This object contains information needed to implement a single nested
106629 ** loop in WHERE clause.
106630 **
106631 ** Contrast this object with WhereLoop. This object describes the
@@ -106490,13 +106686,13 @@
106686 #ifdef SQLITE_DEBUG
106687 char cId; /* Symbolic ID of this loop for debugging use */
106688 #endif
106689 u8 iTab; /* Position in FROM clause of table for this loop */
106690 u8 iSortIdx; /* Sorting index number. 0==None */
106691 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
106692 LogEst rRun; /* Cost of running each loop */
106693 LogEst nOut; /* Estimated number of output rows */
106694 union {
106695 struct { /* Information for internal btree tables */
106696 int nEq; /* Number of equality constraints */
106697 Index *pIndex; /* Index used, or NULL */
106698 } btree;
@@ -106522,12 +106718,12 @@
106718 ** subquery on one operand of an OR operator in the WHERE clause.
106719 ** See WhereOrSet for additional information
106720 */
106721 struct WhereOrCost {
106722 Bitmask prereq; /* Prerequisites */
106723 LogEst rRun; /* Cost of running this subquery */
106724 LogEst nOut; /* Number of outputs for this subquery */
106725 };
106726
106727 /* The WhereOrSet object holds a set of possible WhereOrCosts that
106728 ** correspond to the subquery(s) of OR-clause processing. Only the
106729 ** best N_OR_COST elements are retained.
@@ -106561,12 +106757,12 @@
106757 ** at the end is the choosen query plan.
106758 */
106759 struct WherePath {
106760 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
106761 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
106762 LogEst nRow; /* Estimated number of rows generated by this path */
106763 LogEst rCost; /* Total cost of this path */
106764 u8 isOrdered; /* True if this path satisfies ORDER BY */
106765 u8 isOrderedValid; /* True if the isOrdered field is valid */
106766 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
106767 };
106768
@@ -106628,11 +106824,11 @@
106824 union {
106825 int leftColumn; /* Column number of X in "X <op> <expr>" */
106826 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
106827 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
106828 } u;
106829 LogEst truthProb; /* Probability of truth for this expression */
106830 u16 eOperator; /* A WO_xx value describing <op> */
106831 u8 wtFlags; /* TERM_xxx bit flags. See below */
106832 u8 nChild; /* Number of children that must disable us */
106833 WhereClause *pWC; /* The clause this term is part of */
106834 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
@@ -106776,11 +106972,11 @@
106972 SrcList *pTabList; /* List of tables in the join */
106973 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
106974 ExprList *pResultSet; /* Result set. DISTINCT operates on these */
106975 WhereLoop *pLoops; /* List of all WhereLoop objects */
106976 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
106977 LogEst nRowOut; /* Estimated number of output rows */
106978 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
106979 u8 bOBSat; /* ORDER BY satisfied by indices */
106980 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
106981 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
106982 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
@@ -106836,30 +107032,15 @@
107032 #define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
107033 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
107034 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
107035 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
107036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107037 /*
107038 ** Return the estimated number of output rows from a WHERE clause
107039 */
107040 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
107041 return sqlite3LogEstToInt(pWInfo->nRowOut);
107042 }
107043
107044 /*
107045 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
107046 ** WHERE clause returns outputs for DISTINCT processing.
@@ -106917,12 +107098,12 @@
107098 ** so that pSet keeps the N_OR_COST best entries seen so far.
107099 */
107100 static int whereOrInsert(
107101 WhereOrSet *pSet, /* The WhereOrSet to be updated */
107102 Bitmask prereq, /* Prerequisites of the new entry */
107103 LogEst rRun, /* Run-cost of the new entry */
107104 LogEst nOut /* Number of outputs for the new entry */
107105 ){
107106 u16 i;
107107 WhereOrCost *p;
107108 for(i=pSet->n, p=pSet->a; i>0; i--, p++){
107109 if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
@@ -107003,13 +107184,10 @@
107184 if( pWC->a!=pWC->aStatic ){
107185 sqlite3DbFree(db, pWC->a);
107186 }
107187 }
107188
 
 
 
107189 /*
107190 ** Add a single new WhereTerm entry to the WhereClause object pWC.
107191 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
107192 ** The index in pWC->a[] of the new WhereTerm is returned on success.
107193 ** 0 is returned if the new WhereTerm could not be added due to a memory
@@ -107048,11 +107226,11 @@
107226 }
107227 pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
107228 }
107229 pTerm = &pWC->a[idx = pWC->nTerm++];
107230 if( p && ExprHasProperty(p, EP_Unlikely) ){
107231 pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
107232 }else{
107233 pTerm->truthProb = -1;
107234 }
107235 pTerm->pExpr = sqlite3ExprSkipCollate(p);
107236 pTerm->wtFlags = wtFlags;
@@ -108312,79 +108490,16 @@
108490 }
108491
108492 return 0;
108493 }
108494
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108495
108496 /*
108497 ** Estimate the logarithm of the input value to base 2.
108498 */
108499 static LogEst estLog(LogEst N){
108500 LogEst x = sqlite3LogEst(N);
108501 return x>33 ? x - 33 : 0;
108502 }
108503
108504 /*
108505 ** Two routines for printing the content of an sqlite3_index_info
@@ -108893,11 +109008,11 @@
109008 **
109009 ** ... FROM t1 WHERE a > ? AND a < ? ...
109010 **
109011 ** then nEq is set to 0.
109012 **
109013 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
109014 ** number of rows that the index scan is expected to visit without
109015 ** considering the range constraints. If nEq is 0, this is the number of
109016 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
109017 ** to account for the range contraints pLower and pUpper.
109018 **
@@ -108909,19 +109024,19 @@
109024 static int whereRangeScanEst(
109025 Parse *pParse, /* Parsing & code generating context */
109026 WhereLoopBuilder *pBuilder,
109027 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
109028 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
109029 WhereLoop *pLoop /* Modify the .nOut and maybe .rRun fields */
109030 ){
109031 int rc = SQLITE_OK;
109032 int nOut = pLoop->nOut;
109033 int nEq = pLoop->u.btree.nEq;
109034 LogEst nNew;
109035
109036 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109037 Index *p = pLoop->u.btree.pIndex;
 
109038
109039 if( p->nSample>0
109040 && nEq==pBuilder->nRecValid
109041 && nEq<p->nSampleCol
109042 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
@@ -108998,18 +109113,18 @@
109113 }
109114
109115 pBuilder->pRec = pRec;
109116 if( rc==SQLITE_OK ){
109117 if( iUpper>iLower ){
109118 nNew = sqlite3LogEst(iUpper - iLower);
109119 }else{
109120 nNew = 10; assert( 10==sqlite3LogEst(2) );
109121 }
109122 if( nNew<nOut ){
109123 nOut = nNew;
109124 }
109125 pLoop->nOut = (LogEst)nOut;
109126 WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n",
109127 (u32)iLower, (u32)iUpper, nOut));
109128 return SQLITE_OK;
109129 }
109130 }
@@ -109020,20 +109135,20 @@
109135 assert( pLower || pUpper );
109136 /* TUNING: Each inequality constraint reduces the search space 4-fold.
109137 ** A BETWEEN operator, therefore, reduces the search space 16-fold */
109138 nNew = nOut;
109139 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
109140 nNew -= 20; assert( 20==sqlite3LogEst(4) );
109141 nOut--;
109142 }
109143 if( pUpper ){
109144 nNew -= 20; assert( 20==sqlite3LogEst(4) );
109145 nOut--;
109146 }
109147 if( nNew<10 ) nNew = 10;
109148 if( nNew<nOut ) nOut = nNew;
109149 pLoop->nOut = (LogEst)nOut;
109150 return rc;
109151 }
109152
109153 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109154 /*
@@ -110673,11 +110788,11 @@
110788 */
110789 static int whereLoopAddBtreeIndex(
110790 WhereLoopBuilder *pBuilder, /* The WhereLoop factory */
110791 struct SrcList_item *pSrc, /* FROM clause term being analyzed */
110792 Index *pProbe, /* An index on pSrc */
110793 LogEst nInMul /* log(Number of iterations due to IN) */
110794 ){
110795 WhereInfo *pWInfo = pBuilder->pWInfo; /* WHERE analyse context */
110796 Parse *pParse = pWInfo->pParse; /* Parsing context */
110797 sqlite3 *db = pParse->db; /* Database connection malloc context */
110798 WhereLoop *pNew; /* Template WhereLoop under construction */
@@ -110686,15 +110801,15 @@
110801 WhereScan scan; /* Iterator for WHERE terms */
110802 Bitmask saved_prereq; /* Original value of pNew->prereq */
110803 u16 saved_nLTerm; /* Original value of pNew->nLTerm */
110804 int saved_nEq; /* Original value of pNew->u.btree.nEq */
110805 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
110806 LogEst saved_nOut; /* Original value of pNew->nOut */
110807 int iCol; /* Index of the column in the table */
110808 int rc = SQLITE_OK; /* Return code */
110809 LogEst nRowEst; /* Estimated index selectivity */
110810 LogEst rLogSize; /* Logarithm of table size */
110811 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
110812
110813 pNew = pBuilder->pNew;
110814 if( db->mallocFailed ) return SQLITE_NOMEM;
110815
@@ -110710,11 +110825,11 @@
110825 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
110826
110827 assert( pNew->u.btree.nEq<=pProbe->nColumn );
110828 if( pNew->u.btree.nEq < pProbe->nColumn ){
110829 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
110830 nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
110831 if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
110832 }else{
110833 iCol = -1;
110834 nRowEst = 0;
110835 }
@@ -110724,11 +110839,11 @@
110839 saved_nLTerm = pNew->nLTerm;
110840 saved_wsFlags = pNew->wsFlags;
110841 saved_prereq = pNew->prereq;
110842 saved_nOut = pNew->nOut;
110843 pNew->rSetup = 0;
110844 rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
110845 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
110846 int nIn = 0;
110847 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110848 int nRecValid = pBuilder->nRecValid;
110849 #endif
@@ -110751,14 +110866,14 @@
110866 if( pTerm->eOperator & WO_IN ){
110867 Expr *pExpr = pTerm->pExpr;
110868 pNew->wsFlags |= WHERE_COLUMN_IN;
110869 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
110870 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
110871 nIn = 46; assert( 46==sqlite3LogEst(25) );
110872 }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
110873 /* "x IN (value, value, ...)" */
110874 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
110875 }
110876 pNew->rRun += nIn;
110877 pNew->u.btree.nEq++;
110878 pNew->nOut = nRowEst + nInMul + nIn;
110879 }else if( pTerm->eOperator & (WO_EQ) ){
@@ -110776,11 +110891,11 @@
110891 pNew->nOut = nRowEst + nInMul;
110892 }else if( pTerm->eOperator & (WO_ISNULL) ){
110893 pNew->wsFlags |= WHERE_COLUMN_NULL;
110894 pNew->u.btree.nEq++;
110895 /* TUNING: IS NULL selects 2 rows */
110896 nIn = 10; assert( 10==sqlite3LogEst(2) );
110897 pNew->nOut = nRowEst + nInMul + nIn;
110898 }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
110899 testcase( pTerm->eOperator & WO_GT );
110900 testcase( pTerm->eOperator & WO_GE );
110901 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
@@ -110796,11 +110911,11 @@
110911 pNew->aLTerm[pNew->nLTerm-2] : 0;
110912 }
110913 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
110914 /* Adjust nOut and rRun for STAT3 range values */
110915 assert( pNew->nOut==saved_nOut );
110916 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
110917 }
110918 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110919 if( nInMul==0
110920 && pProbe->nSample
110921 && pNew->u.btree.nEq<=pProbe->nSampleCol
@@ -110816,22 +110931,22 @@
110931 && !ExprHasProperty(pExpr, EP_xIsSelect) ){
110932 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
110933 }
110934 assert( nOut==0 || rc==SQLITE_OK );
110935 if( nOut ){
110936 nOut = sqlite3LogEst(nOut);
110937 pNew->nOut = MIN(nOut, saved_nOut);
110938 }
110939 }
110940 #endif
110941 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
110942 /* Each row involves a step of the index, then a binary search of
110943 ** the main table */
110944 pNew->rRun = sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
110945 }
110946 /* Step cost for each output row */
110947 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
110948 whereLoopOutputAdjust(pBuilder->pWC, pNew, pSrc->iCursor);
110949 rc = whereLoopInsert(pBuilder, pNew);
110950 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
110951 && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
110952 ){
@@ -110927,18 +111042,20 @@
111042 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
111043 WhereLoop *pNew; /* Template WhereLoop object */
111044 int rc = SQLITE_OK; /* Return code */
111045 int iSortIdx = 1; /* Index number */
111046 int b; /* A boolean value */
111047 LogEst rSize; /* number of rows in the table */
111048 LogEst rLogSize; /* Logarithm of the number of rows in the table */
111049 WhereClause *pWC; /* The parsed WHERE clause */
111050 Table *pTab; /* Table being queried */
111051
111052 pNew = pBuilder->pNew;
111053 pWInfo = pBuilder->pWInfo;
111054 pTabList = pWInfo->pTabList;
111055 pSrc = pTabList->a + pNew->iTab;
111056 pTab = pSrc->pTab;
111057 pWC = pBuilder->pWC;
111058 assert( !IsVirtual(pSrc->pTab) );
111059
111060 if( pSrc->pIndex ){
111061 /* An INDEXED BY clause specifies a particular index to use */
@@ -110952,22 +111069,22 @@
111069 memset(&sPk, 0, sizeof(Index));
111070 sPk.nColumn = 1;
111071 sPk.aiColumn = &aiColumnPk;
111072 sPk.aiRowEst = aiRowEstPk;
111073 sPk.onError = OE_Replace;
111074 sPk.pTable = pTab;
111075 aiRowEstPk[0] = pTab->nRowEst;
111076 aiRowEstPk[1] = 1;
111077 pFirst = pSrc->pTab->pIndex;
111078 if( pSrc->notIndexed==0 ){
111079 /* The real indices of the table are only considered if the
111080 ** NOT INDEXED qualifier is omitted from the FROM clause */
111081 sPk.pNext = pFirst;
111082 }
111083 pProbe = &sPk;
111084 }
111085 rSize = sqlite3LogEst(pTab->nRowEst);
111086 rLogSize = estLog(rSize);
111087
111088 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
111089 /* Automatic indexes */
111090 if( !pBuilder->pOrSet
@@ -110988,17 +111105,17 @@
111105 pNew->nLTerm = 1;
111106 pNew->aLTerm[0] = pTerm;
111107 /* TUNING: One-time cost for computing the automatic index is
111108 ** approximately 7*N*log2(N) where N is the number of rows in
111109 ** the table being indexed. */
111110 pNew->rSetup = rLogSize + rSize + 28; assert( 28==sqlite3LogEst(7) );
111111 /* TUNING: Each index lookup yields 20 rows in the table. This
111112 ** is more than the usual guess of 10 rows, since we have no way
111113 ** of knowning how selective the index will ultimately be. It would
111114 ** not be unreasonable to make this value much larger. */
111115 pNew->nOut = 43; assert( 43==sqlite3LogEst(20) );
111116 pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
111117 pNew->wsFlags = WHERE_AUTO_INDEX;
111118 pNew->prereq = mExtra | pTerm->prereqRight;
111119 rc = whereLoopInsert(pBuilder, pNew);
111120 }
111121 }
@@ -111028,14 +111145,12 @@
111145
111146 /* Full table scan */
111147 pNew->iSortIdx = b ? iSortIdx : 0;
111148 /* TUNING: Cost of full table scan is 3*(N + log2(N)).
111149 ** + The extra 3 factor is to encourage the use of indexed lookups
111150 ** over full scans. FIXME */
111151 pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
 
 
111152 whereLoopOutputAdjust(pWC, pNew, pSrc->iCursor);
111153 rc = whereLoopInsert(pBuilder, pNew);
111154 pNew->nOut = rSize;
111155 if( rc ) break;
111156 }else{
@@ -111044,26 +111159,25 @@
111159
111160 /* Full scan via index */
111161 if( b
111162 || ( m==0
111163 && pProbe->bUnordered==0
111164 && pProbe->szIdxRow<pTab->szTabRow
111165 && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
111166 && sqlite3GlobalConfig.bUseCis
111167 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
111168 )
111169 ){
111170 pNew->iSortIdx = b ? iSortIdx : 0;
111171 if( m==0 ){
111172 /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
111173 ** + The extra factor K of between 1.1 and 3.0 that depends
111174 ** on the relative sizes of the table and the index. K
111175 ** is smaller for smaller indices, thus favoring them.
111176 */
111177 pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
111178 (15*pProbe->szIdxRow)/pTab->szTabRow;
 
 
111179 }else{
111180 assert( b!=0 );
111181 /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
111182 ** which we will simplify to just N*log2(N) */
111183 pNew->rRun = rSize + rLogSize;
@@ -111237,13 +111351,13 @@
111351 pIdxInfo->needToFreeIdxStr = 0;
111352 pNew->u.vtab.idxStr = pIdxInfo->idxStr;
111353 pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
111354 && pIdxInfo->orderByConsumed);
111355 pNew->rSetup = 0;
111356 pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
111357 /* TUNING: Every virtual table query returns 25 rows */
111358 pNew->nOut = 46; assert( 46==sqlite3LogEst(25) );
111359 whereLoopInsert(pBuilder, pNew);
111360 if( pNew->u.vtab.needFree ){
111361 sqlite3_free(pNew->u.vtab.idxStr);
111362 pNew->u.vtab.needFree = 0;
111363 }
@@ -111276,10 +111390,12 @@
111390 pWC = pBuilder->pWC;
111391 if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
111392 pWCEnd = pWC->a + pWC->nTerm;
111393 pNew = pBuilder->pNew;
111394 memset(&sSum, 0, sizeof(sSum));
111395 pItem = pWInfo->pTabList->a + pNew->iTab;
111396 iCur = pItem->iCursor;
111397
111398 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
111399 if( (pTerm->eOperator & WO_OR)!=0
111400 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
111401 ){
@@ -111287,12 +111403,10 @@
111403 WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
111404 WhereTerm *pOrTerm;
111405 int once = 1;
111406 int i, j;
111407
 
 
111408 sSubBuild = *pBuilder;
111409 sSubBuild.pOrderBy = 0;
111410 sSubBuild.pOrSet = &sCur;
111411
111412 for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
@@ -111329,12 +111443,12 @@
111443 whereOrMove(&sPrev, &sSum);
111444 sSum.n = 0;
111445 for(i=0; i<sPrev.n; i++){
111446 for(j=0; j<sCur.n; j++){
111447 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
111448 sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
111449 sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
111450 }
111451 }
111452 }
111453 }
111454 pNew->nLTerm = 1;
@@ -111668,23 +111782,23 @@
111782 ** costs if nRowEst==0.
111783 **
111784 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
111785 ** error occurs.
111786 */
111787 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
111788 int mxChoice; /* Maximum number of simultaneous paths tracked */
111789 int nLoop; /* Number of terms in the join */
111790 Parse *pParse; /* Parsing context */
111791 sqlite3 *db; /* The database connection */
111792 int iLoop; /* Loop counter over the terms of the join */
111793 int ii, jj; /* Loop counters */
111794 int mxI = 0; /* Index of next entry to replace */
111795 LogEst rCost; /* Cost of a path */
111796 LogEst nOut; /* Number of outputs */
111797 LogEst mxCost = 0; /* Maximum cost of a set of paths */
111798 LogEst mxOut = 0; /* Maximum nOut value on the set of paths */
111799 LogEst rSortCost; /* Cost to do a sort */
111800 int nTo, nFrom; /* Number of valid entries in aTo[] and aFrom[] */
111801 WherePath *aFrom; /* All nFrom paths at the previous level */
111802 WherePath *aTo; /* The nTo best paths at the current level */
111803 WherePath *pFrom; /* An element of aFrom[] that we are working on */
111804 WherePath *pTo; /* An element of aTo[] that we are working on */
@@ -111717,21 +111831,23 @@
111831 /* Seed the search with a single WherePath containing zero WhereLoops.
111832 **
111833 ** TUNING: Do not let the number of iterations go above 25. If the cost
111834 ** of computing an automatic index is not paid back within the first 25
111835 ** rows, then do not use the automatic index. */
111836 aFrom[0].nRow = MIN(pParse->nQueryLoop, 46); assert( 46==sqlite3LogEst(25) );
111837 nFrom = 1;
111838
111839 /* Precompute the cost of sorting the final result set, if the caller
111840 ** to sqlite3WhereBegin() was concerned about sorting */
111841 rSortCost = 0;
111842 if( pWInfo->pOrderBy==0 || nRowEst==0 ){
111843 aFrom[0].isOrderedValid = 1;
111844 }else{
111845 /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
111846 ** number of output rows. The 48 is the expected size of a row to sort.
111847 ** FIXME: compute a better estimate of the 48 multiplier based on the
111848 ** result set expressions. */
111849 rSortCost = nRowEst + estLog(nRowEst);
111850 WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
111851 }
111852
111853 /* Compute successively longer WherePaths using the previous generation
@@ -111747,12 +111863,12 @@
111863 u8 isOrdered = pFrom->isOrdered;
111864 if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
111865 if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
111866 /* At this point, pWLoop is a candidate to be the next loop.
111867 ** Compute its cost */
111868 rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
111869 rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
111870 nOut = pFrom->nRow + pWLoop->nOut;
111871 maskNew = pFrom->maskLoop | pWLoop->maskSelf;
111872 if( !isOrderedValid ){
111873 switch( wherePathSatisfiesOrderBy(pWInfo,
111874 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
@@ -111762,11 +111878,11 @@
111878 isOrderedValid = 1;
111879 break;
111880 case 0: /* No. pFrom+pWLoop will require a separate sort */
111881 isOrdered = 0;
111882 isOrderedValid = 1;
111883 rCost = sqlite3LogEstAdd(rCost, rSortCost);
111884 break;
111885 default: /* Cannot tell yet. Try again on the next iteration */
111886 break;
111887 }
111888 }else{
@@ -111969,11 +112085,11 @@
112085 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
112086 pLoop->aLTerm[0] = pTerm;
112087 pLoop->nLTerm = 1;
112088 pLoop->u.btree.nEq = 1;
112089 /* TUNING: Cost of a rowid lookup is 10 */
112090 pLoop->rRun = 33; /* 33==sqlite3LogEst(10) */
112091 }else{
112092 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
112093 assert( pLoop->aLTermSpace==pLoop->aLTerm );
112094 assert( ArraySize(pLoop->aLTermSpace)==4 );
112095 if( pIdx->onError==OE_None
@@ -111992,16 +112108,16 @@
112108 }
112109 pLoop->nLTerm = j;
112110 pLoop->u.btree.nEq = j;
112111 pLoop->u.btree.pIndex = pIdx;
112112 /* TUNING: Cost of a unique index lookup is 15 */
112113 pLoop->rRun = 39; /* 39==sqlite3LogEst(15) */
112114 break;
112115 }
112116 }
112117 if( pLoop->wsFlags ){
112118 pLoop->nOut = (LogEst)1;
112119 pWInfo->a[0].pWLoop = pLoop;
112120 pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
112121 pWInfo->a[0].iTabCur = iCur;
112122 pWInfo->nRowOut = 1;
112123 if( pWInfo->pOrderBy ) pWInfo->bOBSat = 1;
112124
+1 -1
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.8.1"
111111
#define SQLITE_VERSION_NUMBER 3008001
112
-#define SQLITE_SOURCE_ID "2013-10-07 21:49:16 36d64dc36f18c166b2c93c43579fa3bbb5cd545f"
112
+#define SQLITE_SOURCE_ID "2013-10-10 15:04:52 af7abebeb1f70466833bc766d294d721eaef746f"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
118118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.1"
111 #define SQLITE_VERSION_NUMBER 3008001
112 #define SQLITE_SOURCE_ID "2013-10-07 21:49:16 36d64dc36f18c166b2c93c43579fa3bbb5cd545f"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.1"
111 #define SQLITE_VERSION_NUMBER 3008001
112 #define SQLITE_SOURCE_ID "2013-10-10 15:04:52 af7abebeb1f70466833bc766d294d721eaef746f"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button