PageRenderTime 128ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 2ms

/drivers/sqlite-wp7/sqlite/where_c.cs

https://bitbucket.org/digitalizarte/coolstorage
C# | 5399 lines | 3421 code | 260 blank | 1718 comment | 1095 complexity | 99becf9cc046cfff8b0de52ef83d9ac1 MD5 | raw file
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. using Bitmask = System.UInt64;
  5. using i16 = System.Int16;
  6. using u8 = System.Byte;
  7. using u16 = System.UInt16;
  8. using u32 = System.UInt32;
  9. using sqlite3_int64 = System.Int64;
  10. namespace Community.CsharpSqlite
  11. {
  12. using sqlite3_value = Sqlite3.Mem;
  13. public partial class Sqlite3
  14. {
  15. /*
  16. ** 2001 September 15
  17. **
  18. ** The author disclaims copyright to this source code. In place of
  19. ** a legal notice, here is a blessing:
  20. **
  21. ** May you do good and not evil.
  22. ** May you find forgiveness for yourself and forgive others.
  23. ** May you share freely, never taking more than you give.
  24. **
  25. *************************************************************************
  26. ** This module contains C code that generates VDBE code used to process
  27. ** the WHERE clause of SQL statements. This module is responsible for
  28. ** generating the code that loops through a table looking for applicable
  29. ** rows. Indices are selected and used to speed the search when doing
  30. ** so is applicable. Because this module is responsible for selecting
  31. ** indices, you might also think of this module as the "query optimizer".
  32. *************************************************************************
  33. ** Included in SQLite3 port to C#-SQLite; 2008 Noah B Hart
  34. ** C#-SQLite is an independent reimplementation of the SQLite software library
  35. **
  36. ** SQLITE_SOURCE_ID: 2011-01-28 17:03:50 ed759d5a9edb3bba5f48f243df47be29e3fe8cd7
  37. **
  38. *************************************************************************
  39. */
  40. //#include "sqliteInt.h"
  41. /*
  42. ** Trace output macros
  43. */
  44. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  45. static bool sqlite3WhereTrace = false;
  46. #endif
  47. #if (SQLITE_TEST) && (SQLITE_DEBUG) && TRACE
  48. //# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X
  49. static void WHERETRACE( string X, params object[] ap ) { if ( sqlite3WhereTrace ) sqlite3DebugPrintf( X, ap ); }
  50. #else
  51. //# define WHERETRACE(X)
  52. static void WHERETRACE( string X, params object[] ap )
  53. {
  54. }
  55. #endif
  56. /* Forward reference
  57. */
  58. //typedef struct WhereClause WhereClause;
  59. //typedef struct WhereMaskSet WhereMaskSet;
  60. //typedef struct WhereOrInfo WhereOrInfo;
  61. //typedef struct WhereAndInfo WhereAndInfo;
  62. //typedef struct WhereCost WhereCost;
  63. /*
  64. ** The query generator uses an array of instances of this structure to
  65. ** help it analyze the subexpressions of the WHERE clause. Each WHERE
  66. ** clause subexpression is separated from the others by AND operators,
  67. ** usually, or sometimes subexpressions separated by OR.
  68. **
  69. ** All WhereTerms are collected into a single WhereClause structure.
  70. ** The following identity holds:
  71. **
  72. ** WhereTerm.pWC.a[WhereTerm.idx] == WhereTerm
  73. **
  74. ** When a term is of the form:
  75. **
  76. ** X <op> <expr>
  77. **
  78. ** where X is a column name and <op> is one of certain operators,
  79. ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
  80. ** cursor number and column number for X. WhereTerm.eOperator records
  81. ** the <op> using a bitmask encoding defined by WO_xxx below. The
  82. ** use of a bitmask encoding for the operator allows us to search
  83. ** quickly for terms that match any of several different operators.
  84. **
  85. ** A WhereTerm might also be two or more subterms connected by OR:
  86. **
  87. ** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
  88. **
  89. ** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR
  90. ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
  91. ** is collected about the
  92. **
  93. ** If a term in the WHERE clause does not match either of the two previous
  94. ** categories, then eOperator==0. The WhereTerm.pExpr field is still set
  95. ** to the original subexpression content and wtFlags is set up appropriately
  96. ** but no other fields in the WhereTerm object are meaningful.
  97. **
  98. ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
  99. ** but they do so indirectly. A single WhereMaskSet structure translates
  100. ** cursor number into bits and the translated bit is stored in the prereq
  101. ** fields. The translation is used in order to maximize the number of
  102. ** bits that will fit in a Bitmask. The VDBE cursor numbers might be
  103. ** spread out over the non-negative integers. For example, the cursor
  104. ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
  105. ** translates these sparse cursor numbers into consecutive integers
  106. ** beginning with 0 in order to make the best possible use of the available
  107. ** bits in the Bitmask. So, in the example above, the cursor numbers
  108. ** would be mapped into integers 0 through 7.
  109. **
  110. ** The number of terms in a join is limited by the number of bits
  111. ** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
  112. ** is only able to process joins with 64 or fewer tables.
  113. */
  114. //typedef struct WhereTerm WhereTerm;
  115. public class WhereTerm
  116. {
  117. public Expr pExpr; /* Pointer to the subexpression that is this term */
  118. public int iParent; /* Disable pWC.a[iParent] when this term disabled */
  119. public int leftCursor; /* Cursor number of X in "X <op> <expr>" */
  120. public class _u
  121. {
  122. public int leftColumn; /* Column number of X in "X <op> <expr>" */
  123. public WhereOrInfo pOrInfo; /* Extra information if eOperator==WO_OR */
  124. public WhereAndInfo pAndInfo; /* Extra information if eOperator==WO_AND */
  125. }
  126. public _u u = new _u();
  127. public u16 eOperator; /* A WO_xx value describing <op> */
  128. public u8 wtFlags; /* TERM_xxx bit flags. See below */
  129. public u8 nChild; /* Number of children that must disable us */
  130. public WhereClause pWC; /* The clause this term is part of */
  131. public Bitmask prereqRight; /* Bitmask of tables used by pExpr.pRight */
  132. public Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
  133. };
  134. /*
  135. ** Allowed values of WhereTerm.wtFlags
  136. */
  137. //#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, ref pExpr) */
  138. //#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
  139. //#define TERM_CODED 0x04 /* This term is already coded */
  140. //#define TERM_COPIED 0x08 /* Has a child */
  141. //#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
  142. //#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
  143. //#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
  144. const int TERM_DYNAMIC = 0x01; /* Need to call sqlite3ExprDelete(db, ref pExpr) */
  145. const int TERM_VIRTUAL = 0x02; /* Added by the optimizer. Do not code */
  146. const int TERM_CODED = 0x04; /* This term is already coded */
  147. const int TERM_COPIED = 0x08; /* Has a child */
  148. const int TERM_ORINFO = 0x10; /* Need to free the WhereTerm.u.pOrInfo object */
  149. const int TERM_ANDINFO = 0x20; /* Need to free the WhereTerm.u.pAndInfo obj */
  150. const int TERM_OR_OK = 0x40; /* Used during OR-clause processing */
  151. /*
  152. ** An instance of the following structure holds all information about a
  153. ** WHERE clause. Mostly this is a container for one or more WhereTerms.
  154. */
  155. public class WhereClause
  156. {
  157. public Parse pParse; /* The parser context */
  158. public WhereMaskSet pMaskSet; /* Mapping of table cursor numbers to bitmasks */
  159. public Bitmask vmask; /* Bitmask identifying virtual table cursors */
  160. public u8 op; /* Split operator. TK_AND or TK_OR */
  161. public int nTerm; /* Number of terms */
  162. public int nSlot; /* Number of entries in a[] */
  163. public WhereTerm[] a; /* Each a[] describes a term of the WHERE cluase */
  164. #if (SQLITE_SMALL_STACK)
  165. public WhereTerm[] aStatic = new WhereTerm[1]; /* Initial static space for a[] */
  166. #else
  167. public WhereTerm[] aStatic = new WhereTerm[8]; /* Initial static space for a[] */
  168. #endif
  169. public void CopyTo( WhereClause wc )
  170. {
  171. wc.pParse = this.pParse;
  172. wc.pMaskSet = new WhereMaskSet();
  173. this.pMaskSet.CopyTo( wc.pMaskSet );
  174. wc.op = this.op;
  175. wc.nTerm = this.nTerm;
  176. wc.nSlot = this.nSlot;
  177. wc.a = (WhereTerm[])this.a.Clone();
  178. wc.aStatic = (WhereTerm[])this.aStatic.Clone();
  179. }
  180. };
  181. /*
  182. ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
  183. ** a dynamically allocated instance of the following structure.
  184. */
  185. public class WhereOrInfo
  186. {
  187. public WhereClause wc = new WhereClause();/* Decomposition into subterms */
  188. public Bitmask indexable; /* Bitmask of all indexable tables in the clause */
  189. };
  190. /*
  191. ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
  192. ** a dynamically allocated instance of the following structure.
  193. */
  194. public class WhereAndInfo
  195. {
  196. public WhereClause wc = new WhereClause(); /* The subexpression broken out */
  197. };
  198. /*
  199. ** An instance of the following structure keeps track of a mapping
  200. ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
  201. **
  202. ** The VDBE cursor numbers are small integers contained in
  203. ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
  204. ** clause, the cursor numbers might not begin with 0 and they might
  205. ** contain gaps in the numbering sequence. But we want to make maximum
  206. ** use of the bits in our bitmasks. This structure provides a mapping
  207. ** from the sparse cursor numbers into consecutive integers beginning
  208. ** with 0.
  209. **
  210. ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
  211. ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
  212. **
  213. ** For example, if the WHERE clause expression used these VDBE
  214. ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
  215. ** would map those cursor numbers into bits 0 through 5.
  216. **
  217. ** Note that the mapping is not necessarily ordered. In the example
  218. ** above, the mapping might go like this: 4.3, 5.1, 8.2, 29.0,
  219. ** 57.5, 73.4. Or one of 719 other combinations might be used. It
  220. ** does not really matter. What is important is that sparse cursor
  221. ** numbers all get mapped into bit numbers that begin with 0 and contain
  222. ** no gaps.
  223. */
  224. public class WhereMaskSet
  225. {
  226. public int n; /* Number of Debug.Assigned cursor values */
  227. public int[] ix = new int[BMS]; /* Cursor Debug.Assigned to each bit */
  228. public void CopyTo( WhereMaskSet wms )
  229. {
  230. wms.n = this.n;
  231. wms.ix = (int[])this.ix.Clone();
  232. }
  233. }
  234. /*
  235. ** A WhereCost object records a lookup strategy and the estimated
  236. ** cost of pursuing that strategy.
  237. */
  238. public class WhereCost
  239. {
  240. public WherePlan plan = new WherePlan();/* The lookup strategy */
  241. public double rCost; /* Overall cost of pursuing this search strategy */
  242. public Bitmask used; /* Bitmask of cursors used by this plan */
  243. public void Clear()
  244. {
  245. plan.Clear();
  246. rCost = 0;
  247. used = 0;
  248. }
  249. };
  250. /*
  251. ** Bitmasks for the operators that indices are able to exploit. An
  252. ** OR-ed combination of these values can be used when searching for
  253. ** terms in the where clause.
  254. */
  255. //#define WO_IN 0x001
  256. //#define WO_EQ 0x002
  257. //#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
  258. //#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
  259. //#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
  260. //#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
  261. //#define WO_MATCH 0x040
  262. //#define WO_ISNULL 0x080
  263. //#define WO_OR 0x100 /* Two or more OR-connected terms */
  264. //#define WO_AND 0x200 /* Two or more AND-connected terms */
  265. //#define WO_ALL 0xfff /* Mask of all possible WO_* values */
  266. //#define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
  267. const int WO_IN = 0x001;
  268. const int WO_EQ = 0x002;
  269. const int WO_LT = ( WO_EQ << ( TK_LT - TK_EQ ) );
  270. const int WO_LE = ( WO_EQ << ( TK_LE - TK_EQ ) );
  271. const int WO_GT = ( WO_EQ << ( TK_GT - TK_EQ ) );
  272. const int WO_GE = ( WO_EQ << ( TK_GE - TK_EQ ) );
  273. const int WO_MATCH = 0x040;
  274. const int WO_ISNULL = 0x080;
  275. const int WO_OR = 0x100; /* Two or more OR-connected terms */
  276. const int WO_AND = 0x200; /* Two or more AND-connected terms */
  277. const int WO_ALL = 0xfff; /* Mask of all possible WO_* values */
  278. const int WO_SINGLE = 0x0ff; /* Mask of all non-compound WO_* values */
  279. /*
  280. ** Value for wsFlags returned by bestIndex() and stored in
  281. ** WhereLevel.wsFlags. These flags determine which search
  282. ** strategies are appropriate.
  283. **
  284. ** The least significant 12 bits is reserved as a mask for WO_ values above.
  285. ** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
  286. ** But if the table is the right table of a left join, WhereLevel.wsFlags
  287. ** is set to WO_IN|WO_EQ. The WhereLevel.wsFlags field can then be used as
  288. ** the "op" parameter to findTerm when we are resolving equality constraints.
  289. ** ISNULL constraints will then not be used on the right table of a left
  290. ** join. Tickets #2177 and #2189.
  291. */
  292. //#define WHERE_ROWID_EQ 0x00001000 /* rowid=EXPR or rowid IN (...) */
  293. //#define WHERE_ROWID_RANGE 0x00002000 /* rowid<EXPR and/or rowid>EXPR */
  294. //#define WHERE_COLUMN_EQ 0x00010000 /* x=EXPR or x IN (...) or x IS NULL */
  295. //#define WHERE_COLUMN_RANGE 0x00020000 /* x<EXPR and/or x>EXPR */
  296. //#define WHERE_COLUMN_IN 0x00040000 /* x IN (...) */
  297. //#define WHERE_COLUMN_NULL 0x00080000 /* x IS NULL */
  298. //#define WHERE_INDEXED 0x000f0000 /* Anything that uses an index */
  299. //#define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */
  300. //#define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */
  301. //#define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */
  302. //#define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */
  303. //#define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and x<EXPR */
  304. //#define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */
  305. //#define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */
  306. //#define WHERE_REVERSE 0x02000000 /* Scan in reverse order */
  307. //#define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */
  308. //#define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
  309. //#define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
  310. //#define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
  311. const int WHERE_ROWID_EQ = 0x00001000;
  312. const int WHERE_ROWID_RANGE = 0x00002000;
  313. const int WHERE_COLUMN_EQ = 0x00010000;
  314. const int WHERE_COLUMN_RANGE = 0x00020000;
  315. const int WHERE_COLUMN_IN = 0x00040000;
  316. const int WHERE_COLUMN_NULL = 0x00080000;
  317. const int WHERE_INDEXED = 0x000f0000;
  318. const int WHERE_IN_ABLE = 0x000f1000;
  319. const int WHERE_NOT_FULLSCAN = 0x100f3000;
  320. const int WHERE_TOP_LIMIT = 0x00100000;
  321. const int WHERE_BTM_LIMIT = 0x00200000;
  322. const int WHERE_BOTH_LIMIT = 0x00300000;
  323. const int WHERE_IDX_ONLY = 0x00800000;
  324. const int WHERE_ORDERBY = 0x01000000;
  325. const int WHERE_REVERSE = 0x02000000;
  326. const int WHERE_UNIQUE = 0x04000000;
  327. const int WHERE_VIRTUALTABLE = 0x08000000;
  328. const int WHERE_MULTI_OR = 0x10000000;
  329. const int WHERE_TEMP_INDEX = 0x20000000;
  330. /*
  331. ** Initialize a preallocated WhereClause structure.
  332. */
  333. static void whereClauseInit(
  334. WhereClause pWC, /* The WhereClause to be initialized */
  335. Parse pParse, /* The parsing context */
  336. WhereMaskSet pMaskSet /* Mapping from table cursor numbers to bitmasks */
  337. )
  338. {
  339. pWC.pParse = pParse;
  340. pWC.pMaskSet = pMaskSet;
  341. pWC.nTerm = 0;
  342. pWC.nSlot = ArraySize( pWC.aStatic ) - 1;
  343. pWC.a = pWC.aStatic;
  344. pWC.vmask = 0;
  345. }
  346. /* Forward reference */
  347. //static void whereClauseClear(WhereClause);
  348. /*
  349. ** Deallocate all memory Debug.Associated with a WhereOrInfo object.
  350. */
  351. static void whereOrInfoDelete( sqlite3 db, WhereOrInfo p )
  352. {
  353. whereClauseClear( p.wc );
  354. sqlite3DbFree( db, ref p );
  355. }
  356. /*
  357. ** Deallocate all memory Debug.Associated with a WhereAndInfo object.
  358. */
  359. static void whereAndInfoDelete( sqlite3 db, WhereAndInfo p )
  360. {
  361. whereClauseClear( p.wc );
  362. sqlite3DbFree( db, ref p );
  363. }
  364. /*
  365. ** Deallocate a WhereClause structure. The WhereClause structure
  366. ** itself is not freed. This routine is the inverse of whereClauseInit().
  367. */
  368. static void whereClauseClear( WhereClause pWC )
  369. {
  370. int i;
  371. WhereTerm a;
  372. sqlite3 db = pWC.pParse.db;
  373. for ( i = pWC.nTerm - 1; i >= 0; i-- )//, a++)
  374. {
  375. a = pWC.a[i];
  376. if ( ( a.wtFlags & TERM_DYNAMIC ) != 0 )
  377. {
  378. sqlite3ExprDelete( db, ref a.pExpr );
  379. }
  380. if ( ( a.wtFlags & TERM_ORINFO ) != 0 )
  381. {
  382. whereOrInfoDelete( db, a.u.pOrInfo );
  383. }
  384. else if ( ( a.wtFlags & TERM_ANDINFO ) != 0 )
  385. {
  386. whereAndInfoDelete( db, a.u.pAndInfo );
  387. }
  388. }
  389. if ( pWC.a != pWC.aStatic )
  390. {
  391. sqlite3DbFree( db, ref pWC.a );
  392. }
  393. }
  394. /*
  395. ** Add a single new WhereTerm entry to the WhereClause object pWC.
  396. ** The new WhereTerm object is constructed from Expr p and with wtFlags.
  397. ** The index in pWC.a[] of the new WhereTerm is returned on success.
  398. ** 0 is returned if the new WhereTerm could not be added due to a memory
  399. ** allocation error. The memory allocation failure will be recorded in
  400. ** the db.mallocFailed flag so that higher-level functions can detect it.
  401. **
  402. ** This routine will increase the size of the pWC.a[] array as necessary.
  403. **
  404. ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
  405. ** for freeing the expression p is Debug.Assumed by the WhereClause object pWC.
  406. ** This is true even if this routine fails to allocate a new WhereTerm.
  407. **
  408. ** WARNING: This routine might reallocate the space used to store
  409. ** WhereTerms. All pointers to WhereTerms should be invalidated after
  410. ** calling this routine. Such pointers may be reinitialized by referencing
  411. ** the pWC.a[] array.
  412. */
  413. static int whereClauseInsert( WhereClause pWC, Expr p, u8 wtFlags )
  414. {
  415. WhereTerm pTerm;
  416. int idx;
  417. testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */
  418. if ( pWC.nTerm >= pWC.nSlot )
  419. {
  420. //WhereTerm pOld = pWC.a;
  421. sqlite3 db = pWC.pParse.db;
  422. Array.Resize( ref pWC.a, pWC.nSlot * 2 );
  423. //pWC.a = sqlite3DbMallocRaw(db, sizeof(pWC.a[0])*pWC.nSlot*2 );
  424. //if( pWC.a==null ){
  425. // if( wtFlags & TERM_DYNAMIC ){
  426. // sqlite3ExprDelete(db, ref p);
  427. // }
  428. // pWC.a = pOld;
  429. // return 0;
  430. //}
  431. //memcpy(pWC.a, pOld, sizeof(pWC.a[0])*pWC.nTerm);
  432. //if( pOld!=pWC.aStatic ){
  433. // sqlite3DbFree(db, ref pOld);
  434. //}
  435. //pWC.nSlot = sqlite3DbMallocSize(db, pWC.a)/sizeof(pWC.a[0]);
  436. pWC.nSlot = pWC.a.Length - 1;
  437. }
  438. pWC.a[idx = pWC.nTerm++] = new WhereTerm();
  439. pTerm = pWC.a[idx];
  440. pTerm.pExpr = p;
  441. pTerm.wtFlags = wtFlags;
  442. pTerm.pWC = pWC;
  443. pTerm.iParent = -1;
  444. return idx;
  445. }
  446. /*
  447. ** This routine identifies subexpressions in the WHERE clause where
  448. ** each subexpression is separated by the AND operator or some other
  449. ** operator specified in the op parameter. The WhereClause structure
  450. ** is filled with pointers to subexpressions. For example:
  451. **
  452. ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
  453. ** \________/ \_______________/ \________________/
  454. ** slot[0] slot[1] slot[2]
  455. **
  456. ** The original WHERE clause in pExpr is unaltered. All this routine
  457. ** does is make slot[] entries point to substructure within pExpr.
  458. **
  459. ** In the previous sentence and in the diagram, "slot[]" refers to
  460. ** the WhereClause.a[] array. The slot[] array grows as needed to contain
  461. ** all terms of the WHERE clause.
  462. */
  463. static void whereSplit( WhereClause pWC, Expr pExpr, int op )
  464. {
  465. pWC.op = (u8)op;
  466. if ( pExpr == null )
  467. return;
  468. if ( pExpr.op != op )
  469. {
  470. whereClauseInsert( pWC, pExpr, 0 );
  471. }
  472. else
  473. {
  474. whereSplit( pWC, pExpr.pLeft, op );
  475. whereSplit( pWC, pExpr.pRight, op );
  476. }
  477. }
  478. /*
  479. ** Initialize an expression mask set (a WhereMaskSet object)
  480. */
  481. //#define initMaskSet(P) memset(P, 0, sizeof(*P))
  482. /*
  483. ** Return the bitmask for the given cursor number. Return 0 if
  484. ** iCursor is not in the set.
  485. */
  486. static Bitmask getMask( WhereMaskSet pMaskSet, int iCursor )
  487. {
  488. int i;
  489. Debug.Assert( pMaskSet.n <= sizeof( Bitmask ) * 8 );
  490. for ( i = 0; i < pMaskSet.n; i++ )
  491. {
  492. if ( pMaskSet.ix[i] == iCursor )
  493. {
  494. return ( (Bitmask)1 ) << i;
  495. }
  496. }
  497. return 0;
  498. }
  499. /*
  500. ** Create a new mask for cursor iCursor.
  501. **
  502. ** There is one cursor per table in the FROM clause. The number of
  503. ** tables in the FROM clause is limited by a test early in the
  504. ** sqlite3WhereBegin() routine. So we know that the pMaskSet.ix[]
  505. ** array will never overflow.
  506. */
  507. static void createMask( WhereMaskSet pMaskSet, int iCursor )
  508. {
  509. Debug.Assert( pMaskSet.n < ArraySize( pMaskSet.ix ) );
  510. pMaskSet.ix[pMaskSet.n++] = iCursor;
  511. }
  512. /*
  513. ** This routine walks (recursively) an expression tree and generates
  514. ** a bitmask indicating which tables are used in that expression
  515. ** tree.
  516. **
  517. ** In order for this routine to work, the calling function must have
  518. ** previously invoked sqlite3ResolveExprNames() on the expression. See
  519. ** the header comment on that routine for additional information.
  520. ** The sqlite3ResolveExprNames() routines looks for column names and
  521. ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
  522. ** the VDBE cursor number of the table. This routine just has to
  523. ** translate the cursor numbers into bitmask values and OR all
  524. ** the bitmasks together.
  525. */
  526. //static Bitmask exprListTableUsage(WhereMaskSet*, ExprList);
  527. //static Bitmask exprSelectTableUsage(WhereMaskSet*, Select);
  528. static Bitmask exprTableUsage( WhereMaskSet pMaskSet, Expr p )
  529. {
  530. Bitmask mask = 0;
  531. if ( p == null )
  532. return 0;
  533. if ( p.op == TK_COLUMN )
  534. {
  535. mask = getMask( pMaskSet, p.iTable );
  536. return mask;
  537. }
  538. mask = exprTableUsage( pMaskSet, p.pRight );
  539. mask |= exprTableUsage( pMaskSet, p.pLeft );
  540. if ( ExprHasProperty( p, EP_xIsSelect ) )
  541. {
  542. mask |= exprSelectTableUsage( pMaskSet, p.x.pSelect );
  543. }
  544. else
  545. {
  546. mask |= exprListTableUsage( pMaskSet, p.x.pList );
  547. }
  548. return mask;
  549. }
  550. static Bitmask exprListTableUsage( WhereMaskSet pMaskSet, ExprList pList )
  551. {
  552. int i;
  553. Bitmask mask = 0;
  554. if ( pList != null )
  555. {
  556. for ( i = 0; i < pList.nExpr; i++ )
  557. {
  558. mask |= exprTableUsage( pMaskSet, pList.a[i].pExpr );
  559. }
  560. }
  561. return mask;
  562. }
  563. static Bitmask exprSelectTableUsage( WhereMaskSet pMaskSet, Select pS )
  564. {
  565. Bitmask mask = 0;
  566. while ( pS != null )
  567. {
  568. mask |= exprListTableUsage( pMaskSet, pS.pEList );
  569. mask |= exprListTableUsage( pMaskSet, pS.pGroupBy );
  570. mask |= exprListTableUsage( pMaskSet, pS.pOrderBy );
  571. mask |= exprTableUsage( pMaskSet, pS.pWhere );
  572. mask |= exprTableUsage( pMaskSet, pS.pHaving );
  573. pS = pS.pPrior;
  574. }
  575. return mask;
  576. }
  577. /*
  578. ** Return TRUE if the given operator is one of the operators that is
  579. ** allowed for an indexable WHERE clause term. The allowed operators are
  580. ** "=", "<", ">", "<=", ">=", and "IN".
  581. **
  582. ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
  583. ** of one of the following forms: column = expression column > expression
  584. ** column >= expression column < expression column <= expression
  585. ** expression = column expression > column expression >= column
  586. ** expression < column expression <= column column IN
  587. ** (expression-list) column IN (subquery) column IS NULL
  588. */
  589. static bool allowedOp( int op )
  590. {
  591. Debug.Assert( TK_GT > TK_EQ && TK_GT < TK_GE );
  592. Debug.Assert( TK_LT > TK_EQ && TK_LT < TK_GE );
  593. Debug.Assert( TK_LE > TK_EQ && TK_LE < TK_GE );
  594. Debug.Assert( TK_GE == TK_EQ + 4 );
  595. return op == TK_IN || ( op >= TK_EQ && op <= TK_GE ) || op == TK_ISNULL;
  596. }
  597. /*
  598. ** Swap two objects of type TYPE.
  599. */
  600. //#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
  601. /*
  602. ** Commute a comparison operator. Expressions of the form "X op Y"
  603. ** are converted into "Y op X".
  604. **
  605. ** If a collation sequence is Debug.Associated with either the left or right
  606. ** side of the comparison, it remains Debug.Associated with the same side after
  607. ** the commutation. So "Y collate NOCASE op X" becomes
  608. ** "X collate NOCASE op Y". This is because any collation sequence on
  609. ** the left hand side of a comparison overrides any collation sequence
  610. ** attached to the right. For the same reason the EP_ExpCollate flag
  611. ** is not commuted.
  612. */
  613. static void exprCommute( Parse pParse, Expr pExpr )
  614. {
  615. u16 expRight = (u16)( pExpr.pRight.flags & EP_ExpCollate );
  616. u16 expLeft = (u16)( pExpr.pLeft.flags & EP_ExpCollate );
  617. Debug.Assert( allowedOp( pExpr.op ) && pExpr.op != TK_IN );
  618. pExpr.pRight.pColl = sqlite3ExprCollSeq( pParse, pExpr.pRight );
  619. pExpr.pLeft.pColl = sqlite3ExprCollSeq( pParse, pExpr.pLeft );
  620. SWAP( ref pExpr.pRight.pColl, ref pExpr.pLeft.pColl );
  621. pExpr.pRight.flags = (u16)( ( pExpr.pRight.flags & ~EP_ExpCollate ) | expLeft );
  622. pExpr.pLeft.flags = (u16)( ( pExpr.pLeft.flags & ~EP_ExpCollate ) | expRight );
  623. SWAP( ref pExpr.pRight, ref pExpr.pLeft );
  624. if ( pExpr.op >= TK_GT )
  625. {
  626. Debug.Assert( TK_LT == TK_GT + 2 );
  627. Debug.Assert( TK_GE == TK_LE + 2 );
  628. Debug.Assert( TK_GT > TK_EQ );
  629. Debug.Assert( TK_GT < TK_LE );
  630. Debug.Assert( pExpr.op >= TK_GT && pExpr.op <= TK_GE );
  631. pExpr.op = (u8)( ( ( pExpr.op - TK_GT ) ^ 2 ) + TK_GT );
  632. }
  633. }
  634. /*
  635. ** Translate from TK_xx operator to WO_xx bitmask.
  636. */
  637. static u16 operatorMask( int op )
  638. {
  639. u16 c;
  640. Debug.Assert( allowedOp( op ) );
  641. if ( op == TK_IN )
  642. {
  643. c = WO_IN;
  644. }
  645. else if ( op == TK_ISNULL )
  646. {
  647. c = WO_ISNULL;
  648. }
  649. else
  650. {
  651. Debug.Assert( ( WO_EQ << ( op - TK_EQ ) ) < 0x7fff );
  652. c = (u16)( WO_EQ << ( op - TK_EQ ) );
  653. }
  654. Debug.Assert( op != TK_ISNULL || c == WO_ISNULL );
  655. Debug.Assert( op != TK_IN || c == WO_IN );
  656. Debug.Assert( op != TK_EQ || c == WO_EQ );
  657. Debug.Assert( op != TK_LT || c == WO_LT );
  658. Debug.Assert( op != TK_LE || c == WO_LE );
  659. Debug.Assert( op != TK_GT || c == WO_GT );
  660. Debug.Assert( op != TK_GE || c == WO_GE );
  661. return c;
  662. }
  663. /*
  664. ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
  665. ** where X is a reference to the iColumn of table iCur and <op> is one of
  666. ** the WO_xx operator codes specified by the op parameter.
  667. ** Return a pointer to the term. Return 0 if not found.
  668. */
  669. static WhereTerm findTerm(
  670. WhereClause pWC, /* The WHERE clause to be searched */
  671. int iCur, /* Cursor number of LHS */
  672. int iColumn, /* Column number of LHS */
  673. Bitmask notReady, /* RHS must not overlap with this mask */
  674. u32 op, /* Mask of WO_xx values describing operator */
  675. Index pIdx /* Must be compatible with this index, if not NULL */
  676. )
  677. {
  678. WhereTerm pTerm;
  679. int k;
  680. Debug.Assert( iCur >= 0 );
  681. op &= WO_ALL;
  682. for ( k = pWC.nTerm; k != 0; k-- )//, pTerm++)
  683. {
  684. pTerm = pWC.a[pWC.nTerm - k];
  685. if ( pTerm.leftCursor == iCur
  686. && ( pTerm.prereqRight & notReady ) == 0
  687. && pTerm.u.leftColumn == iColumn
  688. && ( pTerm.eOperator & op ) != 0
  689. )
  690. {
  691. if ( pIdx != null && pTerm.eOperator != WO_ISNULL )
  692. {
  693. Expr pX = pTerm.pExpr;
  694. CollSeq pColl;
  695. char idxaff;
  696. int j;
  697. Parse pParse = pWC.pParse;
  698. idxaff = pIdx.pTable.aCol[iColumn].affinity;
  699. if ( !sqlite3IndexAffinityOk( pX, idxaff ) )
  700. continue;
  701. /* Figure out the collation sequence required from an index for
  702. ** it to be useful for optimising expression pX. Store this
  703. ** value in variable pColl.
  704. */
  705. Debug.Assert( pX.pLeft != null );
  706. pColl = sqlite3BinaryCompareCollSeq( pParse, pX.pLeft, pX.pRight );
  707. Debug.Assert( pColl != null || pParse.nErr != 0 );
  708. for ( j = 0; pIdx.aiColumn[j] != iColumn; j++ )
  709. {
  710. if ( NEVER( j >= pIdx.nColumn ) )
  711. return null;
  712. }
  713. if ( pColl != null && !pColl.zName.Equals( pIdx.azColl[j] ,StringComparison.InvariantCultureIgnoreCase ) )
  714. continue;
  715. }
  716. return pTerm;
  717. }
  718. }
  719. return null;
  720. }
  721. /* Forward reference */
  722. //static void exprAnalyze(SrcList*, WhereClause*, int);
  723. /*
  724. ** Call exprAnalyze on all terms in a WHERE clause.
  725. **
  726. **
  727. */
  728. static void exprAnalyzeAll(
  729. SrcList pTabList, /* the FROM clause */
  730. WhereClause pWC /* the WHERE clause to be analyzed */
  731. )
  732. {
  733. int i;
  734. for ( i = pWC.nTerm - 1; i >= 0; i-- )
  735. {
  736. exprAnalyze( pTabList, pWC, i );
  737. }
  738. }
  739. #if !SQLITE_OMIT_LIKE_OPTIMIZATION
  740. /*
  741. ** Check to see if the given expression is a LIKE or GLOB operator that
  742. ** can be optimized using inequality constraints. Return TRUE if it is
  743. ** so and false if not.
  744. **
  745. ** In order for the operator to be optimizible, the RHS must be a string
  746. ** literal that does not begin with a wildcard.
  747. */
  748. static int isLikeOrGlob(
  749. Parse pParse, /* Parsing and code generating context */
  750. Expr pExpr, /* Test this expression */
  751. ref Expr ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
  752. ref bool pisComplete, /* True if the only wildcard is % in the last character */
  753. ref bool pnoCase /* True if uppercase is equivalent to lowercase */
  754. )
  755. {
  756. string z = null; /* String on RHS of LIKE operator */
  757. Expr pRight, pLeft; /* Right and left size of LIKE operator */
  758. ExprList pList; /* List of operands to the LIKE operator */
  759. int c = 0; /* One character in z[] */
  760. int cnt; /* Number of non-wildcard prefix characters */
  761. char[] wc = new char[3]; /* Wildcard characters */
  762. sqlite3 db = pParse.db; /* Data_base connection */
  763. sqlite3_value pVal = null;
  764. int op; /* Opcode of pRight */
  765. if ( !sqlite3IsLikeFunction( db, pExpr, ref pnoCase, wc ) )
  766. {
  767. return 0;
  768. }
  769. #if SQLITE_EBCDIC
  770. if( pnoCase ) return 0;
  771. #endif
  772. pList = pExpr.x.pList;
  773. pLeft = pList.a[1].pExpr;
  774. if ( pLeft.op != TK_COLUMN || sqlite3ExprAffinity( pLeft ) != SQLITE_AFF_TEXT )
  775. {
  776. /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
  777. ** be the name of an indexed column with TEXT affinity. */
  778. return 0;
  779. }
  780. Debug.Assert( pLeft.iColumn != ( -1 ) ); /* Because IPK never has AFF_TEXT */
  781. pRight = pList.a[0].pExpr;
  782. op = pRight.op;
  783. if ( op == TK_REGISTER )
  784. {
  785. op = pRight.op2;
  786. }
  787. if ( op == TK_VARIABLE )
  788. {
  789. Vdbe pReprepare = pParse.pReprepare;
  790. int iCol = pRight.iColumn;
  791. pVal = sqlite3VdbeGetValue( pReprepare, iCol, (byte)SQLITE_AFF_NONE );
  792. if ( pVal != null && sqlite3_value_type( pVal ) == SQLITE_TEXT )
  793. {
  794. z = sqlite3_value_text( pVal );
  795. }
  796. sqlite3VdbeSetVarmask( pParse.pVdbe, iCol ); /* IMP: R-23257-02778 */
  797. Debug.Assert( pRight.op == TK_VARIABLE || pRight.op == TK_REGISTER );
  798. }
  799. else if ( op == TK_STRING )
  800. {
  801. z = pRight.u.zToken;
  802. }
  803. if ( !String.IsNullOrEmpty( z ) )
  804. {
  805. cnt = 0;
  806. while ( cnt < z.Length && ( c = z[cnt] ) != 0 && c != wc[0] && c != wc[1] && c != wc[2] )
  807. {
  808. cnt++;
  809. }
  810. if ( cnt != 0 && 255 != (u8)z[cnt - 1] )
  811. {
  812. Expr pPrefix;
  813. pisComplete = c == wc[0] && cnt == z.Length - 1;
  814. pPrefix = sqlite3Expr( db, TK_STRING, z );
  815. if ( pPrefix != null )
  816. pPrefix.u.zToken = pPrefix.u.zToken.Substring( 0, cnt );
  817. ppPrefix = pPrefix;
  818. if ( op == TK_VARIABLE )
  819. {
  820. Vdbe v = pParse.pVdbe;
  821. sqlite3VdbeSetVarmask( v, pRight.iColumn ); /* IMP: R-23257-02778 */
  822. if ( pisComplete && pRight.u.zToken.Length > 1 )
  823. {
  824. /* If the rhs of the LIKE expression is a variable, and the current
  825. ** value of the variable means there is no need to invoke the LIKE
  826. ** function, then no OP_Variable will be added to the program.
  827. ** This causes problems for the sqlite3_bind_parameter_name()
  828. ** API. To workaround them, add a dummy OP_Variable here.
  829. */
  830. int r1 = sqlite3GetTempReg( pParse );
  831. sqlite3ExprCodeTarget( pParse, pRight, r1 );
  832. sqlite3VdbeChangeP3( v, sqlite3VdbeCurrentAddr( v ) - 1, 0 );
  833. sqlite3ReleaseTempReg( pParse, r1 );
  834. }
  835. }
  836. }
  837. else
  838. {
  839. z = null;
  840. }
  841. }
  842. sqlite3ValueFree( ref pVal );
  843. return ( z != null ) ? 1 : 0;
  844. }
  845. #endif //* SQLITE_OMIT_LIKE_OPTIMIZATION */
  846. #if !SQLITE_OMIT_VIRTUALTABLE
  847. /*
  848. ** Check to see if the given expression is of the form
  849. **
  850. ** column MATCH expr
  851. **
  852. ** If it is then return TRUE. If not, return FALSE.
  853. */
  854. static int isMatchOfColumn(
  855. Expr pExpr /* Test this expression */
  856. ){
  857. ExprList pList;
  858. if( pExpr.op!=TK_FUNCTION ){
  859. return 0;
  860. }
  861. if( pExpr.u.zToken.Equals("match", StringComparison.InvariantCultureIgnoreCase ) ){
  862. return 0;
  863. }
  864. pList = pExpr.x.pList;
  865. if( pList.nExpr!=2 ){
  866. return 0;
  867. }
  868. if( pList.a[1].pExpr.op != TK_COLUMN ){
  869. return 0;
  870. }
  871. return 1;
  872. }
  873. #endif //* SQLITE_OMIT_VIRTUALTABLE */
  874. /*
  875. ** If the pBase expression originated in the ON or USING clause of
  876. ** a join, then transfer the appropriate markings over to derived.
  877. */
  878. static void transferJoinMarkings( Expr pDerived, Expr pBase )
  879. {
  880. pDerived.flags = (u16)( pDerived.flags | pBase.flags & EP_FromJoin );
  881. pDerived.iRightJoinTable = pBase.iRightJoinTable;
  882. }
  883. #if !(SQLITE_OMIT_OR_OPTIMIZATION) && !(SQLITE_OMIT_SUBQUERY)
  884. /*
  885. ** Analyze a term that consists of two or more OR-connected
  886. ** subterms. So in:
  887. **
  888. ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
  889. ** ^^^^^^^^^^^^^^^^^^^^
  890. **
  891. ** This routine analyzes terms such as the middle term in the above example.
  892. ** A WhereOrTerm object is computed and attached to the term under
  893. ** analysis, regardless of the outcome of the analysis. Hence:
  894. **
  895. ** WhereTerm.wtFlags |= TERM_ORINFO
  896. ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
  897. **
  898. ** The term being analyzed must have two or more of OR-connected subterms.
  899. ** A single subterm might be a set of AND-connected sub-subterms.
  900. ** Examples of terms under analysis:
  901. **
  902. ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
  903. ** (B) x=expr1 OR expr2=x OR x=expr3
  904. ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
  905. ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
  906. ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
  907. **
  908. ** CASE 1:
  909. **
  910. ** If all subterms are of the form T.C=expr for some single column of C
  911. ** a single table T (as shown in example B above) then create a new virtual
  912. ** term that is an equivalent IN expression. In other words, if the term
  913. ** being analyzed is:
  914. **
  915. ** x = expr1 OR expr2 = x OR x = expr3
  916. **
  917. ** then create a new virtual term like this:
  918. **
  919. ** x IN (expr1,expr2,expr3)
  920. **
  921. ** CASE 2:
  922. **
  923. ** If all subterms are indexable by a single table T, then set
  924. **
  925. ** WhereTerm.eOperator = WO_OR
  926. ** WhereTerm.u.pOrInfo.indexable |= the cursor number for table T
  927. **
  928. ** A subterm is "indexable" if it is of the form
  929. ** "T.C <op> <expr>" where C is any column of table T and
  930. ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
  931. ** A subterm is also indexable if it is an AND of two or more
  932. ** subsubterms at least one of which is indexable. Indexable AND
  933. ** subterms have their eOperator set to WO_AND and they have
  934. ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
  935. **
  936. ** From another point of view, "indexable" means that the subterm could
  937. ** potentially be used with an index if an appropriate index exists.
  938. ** This analysis does not consider whether or not the index exists; that
  939. ** is something the bestIndex() routine will determine. This analysis
  940. ** only looks at whether subterms appropriate for indexing exist.
  941. **
  942. ** All examples A through E above all satisfy case 2. But if a term
  943. ** also statisfies case 1 (such as B) we know that the optimizer will
  944. ** always prefer case 1, so in that case we pretend that case 2 is not
  945. ** satisfied.
  946. **
  947. ** It might be the case that multiple tables are indexable. For example,
  948. ** (E) above is indexable on tables P, Q, and R.
  949. **
  950. ** Terms that satisfy case 2 are candidates for lookup by using
  951. ** separate indices to find rowids for each subterm and composing
  952. ** the union of all rowids using a RowSet object. This is similar
  953. ** to "bitmap indices" in other data_base engines.
  954. **
  955. ** OTHERWISE:
  956. **
  957. ** If neither case 1 nor case 2 apply, then leave the eOperator set to
  958. ** zero. This term is not useful for search.
  959. */
  960. static void exprAnalyzeOrTerm(
  961. SrcList pSrc, /* the FROM clause */
  962. WhereClause pWC, /* the complete WHERE clause */
  963. int idxTerm /* Index of the OR-term to be analyzed */
  964. )
  965. {
  966. Parse pParse = pWC.pParse; /* Parser context */
  967. sqlite3 db = pParse.db; /* Data_base connection */
  968. WhereTerm pTerm = pWC.a[idxTerm]; /* The term to be analyzed */
  969. Expr pExpr = pTerm.pExpr; /* The expression of the term */
  970. WhereMaskSet pMaskSet = pWC.pMaskSet; /* Table use masks */
  971. int i; /* Loop counters */
  972. WhereClause pOrWc; /* Breakup of pTerm into subterms */
  973. WhereTerm pOrTerm; /* A Sub-term within the pOrWc */
  974. WhereOrInfo pOrInfo; /* Additional information Debug.Associated with pTerm */
  975. Bitmask chngToIN; /* Tables that might satisfy case 1 */
  976. Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
  977. /*
  978. ** Break the OR clause into its separate subterms. The subterms are
  979. ** stored in a WhereClause structure containing within the WhereOrInfo
  980. ** object that is attached to the original OR clause term.
  981. */
  982. Debug.Assert( ( pTerm.wtFlags & ( TERM_DYNAMIC | TERM_ORINFO | TERM_ANDINFO ) ) == 0 );
  983. Debug.Assert( pExpr.op == TK_OR );
  984. pTerm.u.pOrInfo = pOrInfo = new WhereOrInfo();//sqlite3DbMallocZero(db, sizeof(*pOrInfo));
  985. if ( pOrInfo == null )
  986. return;
  987. pTerm.wtFlags |= TERM_ORINFO;
  988. pOrWc = pOrInfo.wc;
  989. whereClauseInit( pOrWc, pWC.pParse, pMaskSet );
  990. whereSplit( pOrWc, pExpr, TK_OR );
  991. exprAnalyzeAll( pSrc, pOrWc );
  992. // if ( db.mallocFailed != 0 ) return;
  993. Debug.Assert( pOrWc.nTerm >= 2 );
  994. /*
  995. ** Compute the set of tables that might satisfy cases 1 or 2.
  996. */
  997. indexable = ~(Bitmask)0;
  998. chngToIN = ~( pWC.vmask );
  999. for ( i = pOrWc.nTerm - 1; i >= 0 && indexable != 0; i-- )//, pOrTerm++ )
  1000. {
  1001. pOrTerm = pOrWc.a[i];
  1002. if ( ( pOrTerm.eOperator & WO_SINGLE ) == 0 )
  1003. {
  1004. WhereAndInfo pAndInfo;
  1005. Debug.Assert( pOrTerm.eOperator == 0 );
  1006. Debug.Assert( ( pOrTerm.wtFlags & ( TERM_ANDINFO | TERM_ORINFO ) ) == 0 );
  1007. chngToIN = 0;
  1008. pAndInfo = new WhereAndInfo();//sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
  1009. if ( pAndInfo != null )
  1010. {
  1011. WhereClause pAndWC;
  1012. WhereTerm pAndTerm;
  1013. int j;
  1014. Bitmask b = 0;
  1015. pOrTerm.u.pAndInfo = pAndInfo;
  1016. pOrTerm.wtFlags |= TERM_ANDINFO;
  1017. pOrTerm.eOperator = WO_AND;
  1018. pAndWC = pAndInfo.wc;
  1019. whereClauseInit( pAndWC, pWC.pParse, pMaskSet );
  1020. whereSplit( pAndWC, pOrTerm.pExpr, TK_AND );
  1021. exprAnalyzeAll( pSrc, pAndWC );
  1022. //testcase( db.mallocFailed );
  1023. ////if ( 0 == db.mallocFailed )
  1024. {
  1025. for ( j = 0; j < pAndWC.nTerm; j++ )//, pAndTerm++ )
  1026. {
  1027. pAndTerm = pAndWC.a[j];
  1028. Debug.Assert( pAndTerm.pExpr != null );
  1029. if ( allowedOp( pAndTerm.pExpr.op ) )
  1030. {
  1031. b |= getMask( pMaskSet, pAndTerm.leftCursor );
  1032. }
  1033. }
  1034. }
  1035. indexable &= b;
  1036. }
  1037. }
  1038. else if ( ( pOrTerm.wtFlags & TERM_COPIED ) != 0 )
  1039. {
  1040. /* Skip this term for now. We revisit it when we process the
  1041. ** corresponding TERM_VIRTUAL term */
  1042. }
  1043. else
  1044. {
  1045. Bitmask b;
  1046. b = getMask( pMaskSet, pOrTerm.leftCursor );
  1047. if ( ( pOrTerm.wtFlags & TERM_VIRTUAL ) != 0 )
  1048. {
  1049. WhereTerm pOther = pOrWc.a[pOrTerm.iParent];
  1050. b |= getMask( pMaskSet, pOther.leftCursor );
  1051. }
  1052. indexable &= b;
  1053. if ( pOrTerm.eOperator != WO_EQ )
  1054. {
  1055. chngToIN = 0;
  1056. }
  1057. else
  1058. {
  1059. chngToIN &= b;
  1060. }
  1061. }
  1062. }
  1063. /*
  1064. ** Record the set of tables that satisfy case 2. The set might be
  1065. ** empty.
  1066. */
  1067. pOrInfo.indexable = indexable;
  1068. pTerm.eOperator = (u16)( indexable == 0 ? 0 : WO_OR );
  1069. /*
  1070. ** chngToIN holds a set of tables that *might* satisfy case 1. But
  1071. ** we have to do some additional checking to see if case 1 really
  1072. ** is satisfied.
  1073. **
  1074. ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
  1075. ** that there is no possibility of transforming the OR clause into an
  1076. ** IN operator because one or more terms in the OR clause contain
  1077. ** something other than == on a column in the single table. The 1-bit
  1078. ** case means that every term of the OR clause is of the form
  1079. ** "table.column=expr" for some single table. The one bit that is set
  1080. ** will correspond to the common table. We still need to check to make
  1081. ** sure the same column is used on all terms. The 2-bit case is when
  1082. ** the all terms are of the form "table1.column=table2.column". It
  1083. ** might be possible to form an IN operator with either table1.column
  1084. ** or table2.column as the LHS if either is common to every term of
  1085. ** the OR clause.
  1086. **
  1087. ** Note that terms of the form "table.column1=table.column2" (the
  1088. ** same table on both sizes of the ==) cannot be optimized.
  1089. */
  1090. if ( chngToIN != 0 )
  1091. {
  1092. int okToChngToIN = 0; /* True if the conversion to IN is valid */
  1093. int iColumn = -1; /* Column index on lhs of IN operator */
  1094. int iCursor = -1; /* Table cursor common to all terms */
  1095. int j = 0; /* Loop counter */
  1096. /* Search for a table and column that appears on one side or the
  1097. ** other of the == operator in every subterm. That table and column
  1098. ** will be recorded in iCursor and iColumn. There might not be any
  1099. ** such table and column. Set okToChngToIN if an appropriate table
  1100. ** and column is found but leave okToChngToIN false if not found.
  1101. */
  1102. for ( j = 0; j < 2 && 0 == okToChngToIN; j++ )
  1103. {
  1104. //pOrTerm = pOrWc.a;
  1105. for ( i = pOrWc.nTerm - 1; i >= 0; i-- )//, pOrTerm++)
  1106. {
  1107. pOrTerm = pOrWc.a[pOrWc.nTerm - 1 - i];
  1108. Debug.Assert( pOrTerm.eOperator == WO_EQ );
  1109. pOrTerm.wtFlags = (u8)( pOrTerm.wtFlags & ~TERM_OR_OK );
  1110. if ( pOrTerm.leftCursor == iCursor )
  1111. {
  1112. /* This is the 2-bit case and we are on the second iteration and
  1113. ** current term is from the first iteration. So skip this term. */
  1114. Debug.Assert( j == 1 );
  1115. continue;
  1116. }
  1117. if ( ( chngToIN & getMask( pMaskSet, pOrTerm.leftCursor ) ) == 0 )
  1118. {
  1119. /* This term must be of the form t1.a==t2.b where t2 is in the
  1120. ** chngToIN set but t1 is not. This term will be either preceeded
  1121. ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
  1122. ** and use its inversion. */
  1123. testcase( pOrTerm.wtFlags & TERM_COPIED );
  1124. testcase( pOrTerm.wtFlags & TERM_VIRTUAL );
  1125. Debug.Assert( ( pOrTerm.wtFlags & ( TERM_COPIED | TERM_VIRTUAL ) ) != 0 );
  1126. continue;
  1127. }
  1128. iColumn = pOrTerm.u.leftColumn;
  1129. iCursor = pOrTerm.leftCursor;
  1130. break;
  1131. }
  1132. if ( i < 0 )
  1133. {
  1134. /* No candidate table+column was found. This can only occur
  1135. ** on the second iteration */
  1136. Debug.Assert( j == 1 );
  1137. Debug.Assert( ( chngToIN & ( chngToIN - 1 ) ) == 0 );
  1138. Debug.Assert( chngToIN == getMask( pMaskSet, iCursor ) );
  1139. break;
  1140. }
  1141. testcase( j == 1 );
  1142. /* We have found a candidate table and column. Check to see if that
  1143. ** table and column is common to every term in the OR clause */
  1144. okToChngToIN = 1;
  1145. for ( ; i >= 0 && okToChngToIN != 0; i-- )//, pOrTerm++)
  1146. {
  1147. pOrTerm = pOrWc.a[pOrWc.nTerm - 1 - i];
  1148. Debug.Assert( pOrTerm.eOperator == WO_EQ );
  1149. if ( pOrTerm.leftCursor != iCursor )
  1150. {
  1151. pOrTerm.wtFlags = (u8)( pOrTerm.wtFlags & ~TERM_OR_OK );
  1152. }
  1153. else if ( pOrTerm.u.leftColumn != iColumn )
  1154. {
  1155. okToChngToIN = 0;
  1156. }
  1157. else
  1158. {
  1159. int affLeft, affRight;
  1160. /* If the right-hand side is also a column, then the affinities
  1161. ** of both right and left sides must be such that no type
  1162. ** conversions are required on the right. (Ticket #2249)
  1163. */
  1164. affRight = sqlite3ExprAffinity( pOrTerm.pExpr.pRight );
  1165. affLeft = sqlite3ExprAffinity( pOrTerm.pExpr.pLeft );
  1166. if ( affRight != 0 && affRight != affLeft )
  1167. {
  1168. okToChngToIN = 0;
  1169. }
  1170. else
  1171. {
  1172. pOrTerm.wtFlags |= TERM_OR_OK;
  1173. }
  1174. }
  1175. }
  1176. }
  1177. /* At this point, okToChngToIN is true if original pTerm satisfies
  1178. ** case 1. In that case, construct a new virtual term that is
  1179. ** pTerm converted into an IN operator.
  1180. **
  1181. ** EV: R-00211-15100
  1182. */
  1183. if ( okToChngToIN != 0 )
  1184. {
  1185. Expr pDup; /* A transient duplicate expression */
  1186. ExprList pList = null; /* The RHS of the IN operator */
  1187. Expr pLeft = null; /* The LHS of the IN operator */
  1188. Expr pNew; /* The complete IN operator */
  1189. for ( i = pOrWc.nTerm - 1; i >= 0; i-- )//, pOrTerm++)
  1190. {
  1191. pOrTerm = pOrWc.a[pOrWc.nTerm - 1 - i];
  1192. if ( ( pOrTerm.wtFlags & TERM_OR_OK ) == 0 )
  1193. continue;
  1194. Debug.Assert( pOrTerm.eOperator == WO_EQ );
  1195. Debug.Assert( pOrTerm.leftCursor == iCursor );
  1196. Debug.Assert( pOrTerm.u.leftColumn == iColumn );
  1197. pDup = sqlite3ExprDup( db, pOrTerm.pExpr.pRight, 0 );
  1198. pList = sqlite3ExprListAppend( pWC.pParse, pList, pDup );
  1199. pLeft = pOrTerm.pExpr.pLeft;
  1200. }
  1201. Debug.Assert( pLeft != null );
  1202. pDup = sqlite3ExprDup( db, pLeft, 0 );
  1203. pNew = sqlite3PExpr( pParse, TK_IN, pDup, null, null );
  1204. if ( pNew != null )
  1205. {
  1206. int idxNew;
  1207. transferJoinMarkings( pNew, pExpr );
  1208. Debug.Assert( !ExprHasProperty( pNew, EP_xIsSelect ) );
  1209. pNew.x.pList = pList;
  1210. idxNew = whereClauseInsert( pWC, pNew, TERM_VIRTUAL | TERM_DYNAMIC );
  1211. testcase( idxNew == 0 );
  1212. exprAnalyze( pSrc, pWC, idxNew );
  1213. pTerm = pWC.a[idxTerm];
  1214. pWC.a[idxNew].iParent = idxTerm;
  1215. pTerm.nChild = 1;
  1216. }
  1217. else
  1218. {
  1219. sqlite3ExprListDelete( db, ref pList );
  1220. }
  1221. pTerm.eOperator = 0; /* case 1 trumps case 2 */
  1222. }
  1223. }
  1224. }
  1225. #endif //* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
  1226. /*
  1227. ** The input to this routine is an WhereTerm structure with only the
  1228. ** "pExpr" field filled in. The job of this routine is to analyze the
  1229. ** subexpression and populate all the other fields of the WhereTerm
  1230. ** structure.
  1231. **
  1232. ** If the expression is of the form "<expr> <op> X" it gets commuted
  1233. ** to the standard form of "X <op> <expr>".
  1234. **
  1235. ** If the expression is of the form "X <op> Y" where both X and Y are
  1236. ** columns, then the original expression is unchanged and a new virtual
  1237. ** term of the form "Y <op> X" is added to the WHERE clause and
  1238. ** analyzed separately. The original term is marked with TERM_COPIED
  1239. ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
  1240. ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
  1241. ** is a commuted copy of a prior term.) The original term has nChild=1
  1242. ** and the copy has idxParent set to the index of the original term.
  1243. */
  1244. static void exprAnalyze(
  1245. SrcList pSrc, /* the FROM clause */
  1246. WhereClause pWC, /* the WHERE clause */
  1247. int idxTerm /* Index of the term to be analyzed */
  1248. )
  1249. {
  1250. WhereTerm pTerm; /* The term to be analyzed */
  1251. WhereMaskSet pMaskSet; /* Set of table index masks */
  1252. Expr pExpr; /* The expression to be analyzed */
  1253. Bitmask prereqLeft; /* Prerequesites of the pExpr.pLeft */
  1254. Bitmask prereqAll; /* Prerequesites of pExpr */
  1255. Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */
  1256. Expr pStr1 = null; /* RHS of LIKE/GLOB operator */
  1257. bool isComplete = false; /* RHS of LIKE/GLOB ends with wildcard */
  1258. bool noCase = false; /* LIKE/GLOB distinguishes case */
  1259. int op; /* Top-level operator. pExpr.op */
  1260. Parse pParse = pWC.pParse; /* Parsing context */
  1261. sqlite3 db = pParse.db; /* Data_base connection */
  1262. //if ( db.mallocFailed != 0 )
  1263. //{
  1264. // return;
  1265. //}
  1266. pTerm = pWC.a[idxTerm];
  1267. pMaskSet = pWC.pMaskSet;
  1268. pExpr = pTerm.pExpr;
  1269. prereqLeft = exprTableUsage( pMaskSet, pExpr.pLeft );
  1270. op = pExpr.op;
  1271. if ( op == TK_IN )
  1272. {
  1273. Debug.Assert( pExpr.pRight == null );
  1274. if ( ExprHasProperty( pExpr, EP_xIsSelect ) )
  1275. {
  1276. pTerm.prereqRight = exprSelectTableUsage( pMaskSet, pExpr.x.pSelect );
  1277. }
  1278. else
  1279. {
  1280. pTerm.prereqRight = exprListTableUsage( pMaskSet, pExpr.x.pList );
  1281. }
  1282. }
  1283. else if ( op == TK_ISNULL )
  1284. {
  1285. pTerm.prereqRight = 0;
  1286. }
  1287. else
  1288. {
  1289. pTerm.prereqRight = exprTableUsage( pMaskSet, pExpr.pRight );
  1290. }
  1291. prereqAll = exprTableUsage( pMaskSet, pExpr );
  1292. if ( ExprHasProperty( pExpr, EP_FromJoin ) )
  1293. {
  1294. Bitmask x = getMask( pMaskSet, pExpr.iRightJoinTable );
  1295. prereqAll |= x;
  1296. extraRight = x - 1; /* ON clause terms may not be used with an index
  1297. ** on left table of a LEFT JOIN. Ticket #3015 */
  1298. }
  1299. pTerm.prereqAll = prereqAll;
  1300. pTerm.leftCursor = -1;
  1301. pTerm.iParent = -1;
  1302. pTerm.eOperator = 0;
  1303. if ( allowedOp( op ) && ( pTerm.prereqRight & prereqLeft ) == 0 )
  1304. {
  1305. Expr pLeft = pExpr.pLeft;
  1306. Expr pRight = pExpr.pRight;
  1307. if ( pLeft.op == TK_COLUMN )
  1308. {
  1309. pTerm.leftCursor = pLeft.iTable;
  1310. pTerm.u.leftColumn = pLeft.iColumn;
  1311. pTerm.eOperator = operatorMask( op );
  1312. }
  1313. if ( pRight != null && pRight.op == TK_COLUMN )
  1314. {
  1315. WhereTerm pNew;
  1316. Expr pDup;
  1317. if ( pTerm.leftCursor >= 0 )
  1318. {
  1319. int idxNew;
  1320. pDup = sqlite3ExprDup( db, pExpr, 0 );
  1321. //if ( db.mallocFailed != 0 )
  1322. //{
  1323. // sqlite3ExprDelete( db, ref pDup );
  1324. // return;
  1325. //}
  1326. idxNew = whereClauseInsert( pWC, pDup, TERM_VIRTUAL | TERM_DYNAMIC );
  1327. if ( idxNew == 0 )
  1328. return;
  1329. pNew = pWC.a[idxNew];
  1330. pNew.iParent = idxTerm;
  1331. pTerm = pWC.a[idxTerm];
  1332. pTerm.nChild = 1;
  1333. pTerm.wtFlags |= TERM_COPIED;
  1334. }
  1335. else
  1336. {
  1337. pDup = pExpr;
  1338. pNew = pTerm;
  1339. }
  1340. exprCommute( pParse, pDup );
  1341. pLeft = pDup.pLeft;
  1342. pNew.leftCursor = pLeft.iTable;
  1343. pNew.u.leftColumn = pLeft.iColumn;
  1344. testcase( ( prereqLeft | extraRight ) != prereqLeft );
  1345. pNew.prereqRight = prereqLeft | extraRight;
  1346. pNew.prereqAll = prereqAll;
  1347. pNew.eOperator = operatorMask( pDup.op );
  1348. }
  1349. }
  1350. #if !SQLITE_OMIT_BETWEEN_OPTIMIZATION
  1351. /* If a term is the BETWEEN operator, create two new virtual terms
  1352. ** that define the range that the BETWEEN implements. For example:
  1353. **
  1354. ** a BETWEEN b AND c
  1355. **
  1356. ** is converted into:
  1357. **
  1358. ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
  1359. **
  1360. ** The two new terms are added onto the end of the WhereClause object.
  1361. ** The new terms are "dynamic" and are children of the original BETWEEN
  1362. ** term. That means that if the BETWEEN term is coded, the children are
  1363. ** skipped. Or, if the children are satisfied by an index, the original
  1364. ** BETWEEN term is skipped.
  1365. */
  1366. else if ( pExpr.op == TK_BETWEEN && pWC.op == TK_AND )
  1367. {
  1368. ExprList pList = pExpr.x.pList;
  1369. int i;
  1370. u8[] ops = new u8[] { TK_GE, TK_LE };
  1371. Debug.Assert( pList != null );
  1372. Debug.Assert( pList.nExpr == 2 );
  1373. for ( i = 0; i < 2; i++ )
  1374. {
  1375. Expr pNewExpr;
  1376. int idxNew;
  1377. pNewExpr = sqlite3PExpr( pParse, ops[i],
  1378. sqlite3ExprDup( db, pExpr.pLeft, 0 ),
  1379. sqlite3ExprDup( db, pList.a[i].pExpr, 0 ), null );
  1380. idxNew = whereClauseInsert( pWC, pNewExpr, TERM_VIRTUAL | TERM_DYNAMIC );
  1381. testcase( idxNew == 0 );
  1382. exprAnalyze( pSrc, pWC, idxNew );
  1383. pTerm = pWC.a[idxTerm];
  1384. pWC.a[idxNew].iParent = idxTerm;
  1385. }
  1386. pTerm.nChild = 2;
  1387. }
  1388. #endif //* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
  1389. #if !(SQLITE_OMIT_OR_OPTIMIZATION) && !(SQLITE_OMIT_SUBQUERY)
  1390. /* Analyze a term that is composed of two or more subterms connected by
  1391. ** an OR operator.
  1392. */
  1393. else if ( pExpr.op == TK_OR )
  1394. {
  1395. Debug.Assert( pWC.op == TK_AND );
  1396. exprAnalyzeOrTerm( pSrc, pWC, idxTerm );
  1397. pTerm = pWC.a[idxTerm];
  1398. }
  1399. #endif //* SQLITE_OMIT_OR_OPTIMIZATION */
  1400. #if !SQLITE_OMIT_LIKE_OPTIMIZATION
  1401. /* Add constraints to reduce the search space on a LIKE or GLOB
  1402. ** operator.
  1403. **
  1404. ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
  1405. **
  1406. ** x>='abc' AND x<'abd' AND x LIKE 'abc%'
  1407. **
  1408. ** The last character of the prefix "abc" is incremented to form the
  1409. ** termination condition "abd".
  1410. */
  1411. if ( pWC.op == TK_AND
  1412. && isLikeOrGlob( pParse, pExpr, ref pStr1, ref isComplete, ref noCase ) != 0
  1413. )
  1414. {
  1415. Expr pLeft; /* LHS of LIKE/GLOB operator */
  1416. Expr pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */
  1417. Expr pNewExpr1;
  1418. Expr pNewExpr2;
  1419. int idxNew1;
  1420. int idxNew2;
  1421. CollSeq pColl; /* Collating sequence to use */
  1422. pLeft = pExpr.x.pList.a[1].pExpr;
  1423. pStr2 = sqlite3ExprDup( db, pStr1, 0 );
  1424. ////if ( 0 == db.mallocFailed )
  1425. {
  1426. int c, pC; /* Last character before the first wildcard */
  1427. pC = pStr2.u.zToken[sqlite3Strlen30( pStr2.u.zToken ) - 1];
  1428. c = pC;
  1429. if ( noCase )
  1430. {
  1431. /* The point is to increment the last character before the first
  1432. ** wildcard. But if we increment '@', that will push it into the
  1433. ** alphabetic range where case conversions will mess up the
  1434. ** inequality. To avoid this, make sure to also run the full
  1435. ** LIKE on all candidate expressions by clearing the isComplete flag
  1436. */
  1437. if ( c == 'A' - 1 )
  1438. isComplete = false; /* EV: R-64339-08207 */
  1439. c = sqlite3UpperToLower[c];
  1440. }
  1441. pStr2.u.zToken = pStr2.u.zToken.Substring( 0, sqlite3Strlen30( pStr2.u.zToken ) - 1 ) + (char)( c + 1 );// pC = c + 1;
  1442. }
  1443. pColl = sqlite3FindCollSeq( db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY", 0 );
  1444. pNewExpr1 = sqlite3PExpr( pParse, TK_GE,
  1445. sqlite3ExprSetColl( sqlite3ExprDup( db, pLeft, 0 ), pColl ),
  1446. pStr1, 0 );
  1447. idxNew1 = whereClauseInsert( pWC, pNewExpr1, TERM_VIRTUAL | TERM_DYNAMIC );
  1448. testcase( idxNew1 == 0 );
  1449. exprAnalyze( pSrc, pWC, idxNew1 );
  1450. pNewExpr2 = sqlite3PExpr( pParse, TK_LT,
  1451. sqlite3ExprSetColl( sqlite3ExprDup( db, pLeft, 0 ), pColl ),
  1452. pStr2, null );
  1453. idxNew2 = whereClauseInsert( pWC, pNewExpr2, TERM_VIRTUAL | TERM_DYNAMIC );
  1454. testcase( idxNew2 == 0 );
  1455. exprAnalyze( pSrc, pWC, idxNew2 );
  1456. pTerm = pWC.a[idxTerm];
  1457. if ( isComplete )
  1458. {
  1459. pWC.a[idxNew1].iParent = idxTerm;
  1460. pWC.a[idxNew2].iParent = idxTerm;
  1461. pTerm.nChild = 2;
  1462. }
  1463. }
  1464. #endif //* SQLITE_OMIT_LIKE_OPTIMIZATION */
  1465. #if !SQLITE_OMIT_VIRTUALTABLE
  1466. /* Add a WO_MATCH auxiliary term to the constraint set if the
  1467. ** current expression is of the form: column MATCH expr.
  1468. ** This information is used by the xBestIndex methods of
  1469. ** virtual tables. The native query optimizer does not attempt
  1470. ** to do anything with MATCH functions.
  1471. */
  1472. if ( isMatchOfColumn( pExpr ) )
  1473. {
  1474. int idxNew;
  1475. Expr pRight, pLeft;
  1476. WhereTerm pNewTerm;
  1477. Bitmask prereqColumn, prereqExpr;
  1478. pRight = pExpr.x.pList.a[0].pExpr;
  1479. pLeft = pExpr.x.pList.a[1].pExpr;
  1480. prereqExpr = exprTableUsage( pMaskSet, pRight );
  1481. prereqColumn = exprTableUsage( pMaskSet, pLeft );
  1482. if ( ( prereqExpr & prereqColumn ) == null )
  1483. {
  1484. Expr pNewExpr;
  1485. pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
  1486. 0, sqlite3ExprDup(db, pRight, 0), 0);
  1487. idxNew = whereClauseInsert( pWC, pNewExpr, TERM_VIRTUAL | TERM_DYNAMIC );
  1488. testcase( idxNew == 0 );
  1489. pNewTerm = pWC.a[idxNew];
  1490. pNewTerm.prereqRight = prereqExpr;
  1491. pNewTerm.leftCursor = pLeft.iTable;
  1492. pNewTerm.u.leftColumn = pLeft.iColumn;
  1493. pNewTerm.eOperator = WO_MATCH;
  1494. pNewTerm.iParent = idxTerm;
  1495. pTerm = pWC.a[idxTerm];
  1496. pTerm.nChild = 1;
  1497. pTerm.wtFlags |= TERM_COPIED;
  1498. pNewTerm.prereqAll = pTerm.prereqAll;
  1499. }
  1500. }
  1501. #endif //* SQLITE_OMIT_VIRTUALTABLE */
  1502. /* Prevent ON clause terms of a LEFT JOIN from being used to drive
  1503. ** an index for tables to the left of the join.
  1504. */
  1505. pTerm.prereqRight |= extraRight;
  1506. }
  1507. /*
  1508. ** Return TRUE if any of the expressions in pList.a[iFirst...] contain
  1509. ** a reference to any table other than the iBase table.
  1510. */
  1511. static bool referencesOtherTables(
  1512. ExprList pList, /* Search expressions in ths list */
  1513. WhereMaskSet pMaskSet, /* Mapping from tables to bitmaps */
  1514. int iFirst, /* Be searching with the iFirst-th expression */
  1515. int iBase /* Ignore references to this table */
  1516. )
  1517. {
  1518. Bitmask allowed = ~getMask( pMaskSet, iBase );
  1519. while ( iFirst < pList.nExpr )
  1520. {
  1521. if ( ( exprTableUsage( pMaskSet, pList.a[iFirst++].pExpr ) & allowed ) != 0 )
  1522. {
  1523. return true;
  1524. }
  1525. }
  1526. return false;
  1527. }
  1528. /*
  1529. ** This routine decides if pIdx can be used to satisfy the ORDER BY
  1530. ** clause. If it can, it returns 1. If pIdx cannot satisfy the
  1531. ** ORDER BY clause, this routine returns 0.
  1532. **
  1533. ** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the
  1534. ** left-most table in the FROM clause of that same SELECT statement and
  1535. ** the table has a cursor number of "_base". pIdx is an index on pTab.
  1536. **
  1537. ** nEqCol is the number of columns of pIdx that are used as equality
  1538. ** constraints. Any of these columns may be missing from the ORDER BY
  1539. ** clause and the match can still be a success.
  1540. **
  1541. ** All terms of the ORDER BY that match against the index must be either
  1542. ** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE
  1543. ** index do not need to satisfy this constraint.) The pbRev value is
  1544. ** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if
  1545. ** the ORDER BY clause is all ASC.
  1546. */
  1547. static bool isSortingIndex(
  1548. Parse pParse, /* Parsing context */
  1549. WhereMaskSet pMaskSet, /* Mapping from table cursor numbers to bitmaps */
  1550. Index pIdx, /* The index we are testing */
  1551. int _base, /* Cursor number for the table to be sorted */
  1552. ExprList pOrderBy, /* The ORDER BY clause */
  1553. int nEqCol, /* Number of index columns with == constraints */
  1554. int wsFlags, /* Index usages flags */
  1555. ref int pbRev /* Set to 1 if ORDER BY is DESC */
  1556. )
  1557. {
  1558. int i, j; /* Loop counters */
  1559. int sortOrder = 0; /* XOR of index and ORDER BY sort direction */
  1560. int nTerm; /* Number of ORDER BY terms */
  1561. ExprList_item pTerm; /* A term of the ORDER BY clause */
  1562. sqlite3 db = pParse.db;
  1563. Debug.Assert( pOrderBy != null );
  1564. nTerm = pOrderBy.nExpr;
  1565. Debug.Assert( nTerm > 0 );
  1566. /* Argument pIdx must either point to a 'real' named index structure,
  1567. ** or an index structure allocated on the stack by bestBtreeIndex() to
  1568. ** represent the rowid index that is part of every table. */
  1569. Debug.Assert( !String.IsNullOrEmpty( pIdx.zName ) || ( pIdx.nColumn == 1 && pIdx.aiColumn[0] == -1 ) );
  1570. /* Match terms of the ORDER BY clause against columns of
  1571. ** the index.
  1572. **
  1573. ** Note that indices have pIdx.nColumn regular columns plus
  1574. ** one additional column containing the rowid. The rowid column
  1575. ** of the index is also allowed to match against the ORDER BY
  1576. ** clause.
  1577. */
  1578. for ( i = j = 0; j < nTerm && i <= pIdx.nColumn; i++ )
  1579. {
  1580. pTerm = pOrderBy.a[j];
  1581. Expr pExpr; /* The expression of the ORDER BY pTerm */
  1582. CollSeq pColl; /* The collating sequence of pExpr */
  1583. int termSortOrder; /* Sort order for this term */
  1584. int iColumn; /* The i-th column of the index. -1 for rowid */
  1585. int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */
  1586. string zColl; /* Name of the collating sequence for i-th index term */
  1587. pExpr = pTerm.pExpr;
  1588. if ( pExpr.op != TK_COLUMN || pExpr.iTable != _base )
  1589. {
  1590. /* Can not use an index sort on anything that is not a column in the
  1591. ** left-most table of the FROM clause */
  1592. break;
  1593. }
  1594. pColl = sqlite3ExprCollSeq( pParse, pExpr );
  1595. if ( null == pColl )
  1596. {
  1597. pColl = db.pDfltColl;
  1598. }
  1599. if ( !String.IsNullOrEmpty( pIdx.zName ) && i < pIdx.nColumn )
  1600. {
  1601. iColumn = pIdx.aiColumn[i];
  1602. if ( iColumn == pIdx.pTable.iPKey )
  1603. {
  1604. iColumn = -1;
  1605. }
  1606. iSortOrder = pIdx.aSortOrder[i];
  1607. zColl = pIdx.azColl[i];
  1608. }
  1609. else
  1610. {
  1611. iColumn = -1;
  1612. iSortOrder = 0;
  1613. zColl = pColl.zName;
  1614. }
  1615. if ( pExpr.iColumn != iColumn || !pColl.zName.Equals( zColl ,StringComparison.InvariantCultureIgnoreCase ) )
  1616. {
  1617. /* Term j of the ORDER BY clause does not match column i of the index */
  1618. if ( i < nEqCol )
  1619. {
  1620. /* If an index column that is constrained by == fails to match an
  1621. ** ORDER BY term, that is OK. Just ignore that column of the index
  1622. */
  1623. continue;
  1624. }
  1625. else if ( i == pIdx.nColumn )
  1626. {
  1627. /* Index column i is the rowid. All other terms match. */
  1628. break;
  1629. }
  1630. else
  1631. {
  1632. /* If an index column fails to match and is not constrained by ==
  1633. ** then the index cannot satisfy the ORDER BY constraint.
  1634. */
  1635. return false;
  1636. }
  1637. }
  1638. Debug.Assert( pIdx.aSortOrder != null || iColumn == -1 );
  1639. Debug.Assert( pTerm.sortOrder == 0 || pTerm.sortOrder == 1 );
  1640. Debug.Assert( iSortOrder == 0 || iSortOrder == 1 );
  1641. termSortOrder = iSortOrder ^ pTerm.sortOrder;
  1642. if ( i > nEqCol )
  1643. {
  1644. if ( termSortOrder != sortOrder )
  1645. {
  1646. /* Indices can only be used if all ORDER BY terms past the
  1647. ** equality constraints are all either DESC or ASC. */
  1648. return false;
  1649. }
  1650. }
  1651. else
  1652. {
  1653. sortOrder = termSortOrder;
  1654. }
  1655. j++;
  1656. //pTerm++;
  1657. if ( iColumn < 0 && !referencesOtherTables( pOrderBy, pMaskSet, j, _base ) )
  1658. {
  1659. /* If the indexed column is the primary key and everything matches
  1660. ** so far and none of the ORDER BY terms to the right reference other
  1661. ** tables in the join, then we are Debug.Assured that the index can be used
  1662. ** to sort because the primary key is unique and so none of the other
  1663. ** columns will make any difference
  1664. */
  1665. j = nTerm;
  1666. }
  1667. }
  1668. pbRev = sortOrder != 0 ? 1 : 0;
  1669. if ( j >= nTerm )
  1670. {
  1671. /* All terms of the ORDER BY clause are covered by this index so
  1672. ** this index can be used for sorting. */
  1673. return true;
  1674. }
  1675. if ( pIdx.onError != OE_None && i == pIdx.nColumn
  1676. && ( wsFlags & WHERE_COLUMN_NULL ) == 0
  1677. && !referencesOtherTables( pOrderBy, pMaskSet, j, _base ) )
  1678. {
  1679. /* All terms of this index match some prefix of the ORDER BY clause
  1680. ** and the index is UNIQUE and no terms on the tail of the ORDER BY
  1681. ** clause reference other tables in a join. If this is all true then
  1682. ** the order by clause is superfluous. Not that if the matching
  1683. ** condition is IS NULL then the result is not necessarily unique
  1684. ** even on a UNIQUE index, so disallow those cases. */
  1685. return true;
  1686. }
  1687. return false;
  1688. }
  1689. /*
  1690. ** Prepare a crude estimate of the logarithm of the input value.
  1691. ** The results need not be exact. This is only used for estimating
  1692. ** the total cost of performing operations with O(logN) or O(NlogN)
  1693. ** complexity. Because N is just a guess, it is no great tragedy if
  1694. ** logN is a little off.
  1695. */
  1696. static double estLog( double N )
  1697. {
  1698. double logN = 1;
  1699. double x = 10;
  1700. while ( N > x )
  1701. {
  1702. logN += 1;
  1703. x *= 10;
  1704. }
  1705. return logN;
  1706. }
  1707. /*
  1708. ** Two routines for printing the content of an sqlite3_index_info
  1709. ** structure. Used for testing and debugging only. If neither
  1710. ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
  1711. ** are no-ops.
  1712. */
  1713. #if !(SQLITE_OMIT_VIRTUALTABLE) && (SQLITE_DEBUG)
  1714. static void TRACE_IDX_INPUTS( sqlite3_index_info p )
  1715. {
  1716. int i;
  1717. if ( !sqlite3WhereTrace ) return;
  1718. for ( i = 0 ; i < p.nConstraint ; i++ )
  1719. {
  1720. sqlite3DebugPrintf( " constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
  1721. i,
  1722. p.aConstraint[i].iColumn,
  1723. p.aConstraint[i].iTermOffset,
  1724. p.aConstraint[i].op,
  1725. p.aConstraint[i].usable );
  1726. }
  1727. for ( i = 0 ; i < p.nOrderBy ; i++ )
  1728. {
  1729. sqlite3DebugPrintf( " orderby[%d]: col=%d desc=%d\n",
  1730. i,
  1731. p.aOrderBy[i].iColumn,
  1732. p.aOrderBy[i].desc );
  1733. }
  1734. }
  1735. static void TRACE_IDX_OUTPUTS( sqlite3_index_info p )
  1736. {
  1737. int i;
  1738. if ( !sqlite3WhereTrace ) return;
  1739. for ( i = 0 ; i < p.nConstraint ; i++ )
  1740. {
  1741. sqlite3DebugPrintf( " usage[%d]: argvIdx=%d omit=%d\n",
  1742. i,
  1743. p.aConstraintUsage[i].argvIndex,
  1744. p.aConstraintUsage[i].omit );
  1745. }
  1746. sqlite3DebugPrintf( " idxNum=%d\n", p.idxNum );
  1747. sqlite3DebugPrintf( " idxStr=%s\n", p.idxStr );
  1748. sqlite3DebugPrintf( " orderByConsumed=%d\n", p.orderByConsumed );
  1749. sqlite3DebugPrintf( " estimatedCost=%g\n", p.estimatedCost );
  1750. }
  1751. #else
  1752. //#define TRACE_IDX_INPUTS(A)
  1753. //#define TRACE_IDX_OUTPUTS(A)
  1754. #endif
  1755. /*
  1756. ** Required because bestIndex() is called by bestOrClauseIndex()
  1757. */
  1758. //static void bestIndex(
  1759. //Parse*, WhereClause*, struct SrcList_item*,
  1760. //Bitmask, ExprList*, WhereCost*);
  1761. /*
  1762. ** This routine attempts to find an scanning strategy that can be used
  1763. ** to optimize an 'OR' expression that is part of a WHERE clause.
  1764. **
  1765. ** The table associated with FROM clause term pSrc may be either a
  1766. ** regular B-Tree table or a virtual table.
  1767. */
  1768. static void bestOrClauseIndex(
  1769. Parse pParse, /* The parsing context */
  1770. WhereClause pWC, /* The WHERE clause */
  1771. SrcList_item pSrc, /* The FROM clause term to search */
  1772. Bitmask notReady, /* Mask of cursors not available for indexing */
  1773. Bitmask notValid, /* Cursors not available for any purpose */
  1774. ExprList pOrderBy, /* The ORDER BY clause */
  1775. WhereCost pCost /* Lowest cost query plan */
  1776. )
  1777. {
  1778. #if !SQLITE_OMIT_OR_OPTIMIZATION
  1779. int iCur = pSrc.iCursor; /* The cursor of the table to be accessed */
  1780. Bitmask maskSrc = getMask( pWC.pMaskSet, iCur ); /* Bitmask for pSrc */
  1781. WhereTerm pWCEnd = pWC.a[pWC.nTerm]; /* End of pWC.a[] */
  1782. WhereTerm pTerm; /* A single term of the WHERE clause */
  1783. /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
  1784. ** are used */
  1785. if ( pSrc.notIndexed != 0 || pSrc.pIndex != null )
  1786. {
  1787. return;
  1788. }
  1789. /* Search the WHERE clause terms for a usable WO_OR term. */
  1790. for ( int _pt = 0; _pt < pWC.nTerm; _pt++ )//<pWCEnd; pTerm++)
  1791. {
  1792. pTerm = pWC.a[_pt];
  1793. if ( pTerm.eOperator == WO_OR
  1794. && ( ( pTerm.prereqAll & ~maskSrc ) & notReady ) == 0
  1795. && ( pTerm.u.pOrInfo.indexable & maskSrc ) != 0
  1796. )
  1797. {
  1798. WhereClause pOrWC = pTerm.u.pOrInfo.wc;
  1799. WhereTerm pOrWCEnd = pOrWC.a[pOrWC.nTerm];
  1800. WhereTerm pOrTerm;
  1801. int flags = WHERE_MULTI_OR;
  1802. double rTotal = 0;
  1803. double nRow = 0;
  1804. Bitmask used = 0;
  1805. for ( int _pOrWC = 0; _pOrWC < pOrWC.nTerm; _pOrWC++ )//pOrTerm = pOrWC.a ; pOrTerm < pOrWCEnd ; pOrTerm++ )
  1806. {
  1807. pOrTerm = pOrWC.a[_pOrWC];
  1808. WhereCost sTermCost = null;
  1809. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  1810. WHERETRACE( "... Multi-index OR testing for term %d of %d....\n",
  1811. _pOrWC, pOrWC.nTerm - _pOrWC//( pOrTerm - pOrWC.a ), ( pTerm - pWC.a )
  1812. );
  1813. #endif
  1814. if ( pOrTerm.eOperator == WO_AND )
  1815. {
  1816. WhereClause pAndWC = pOrTerm.u.pAndInfo.wc;
  1817. bestIndex( pParse, pAndWC, pSrc, notReady, notValid, null, ref sTermCost );
  1818. }
  1819. else if ( pOrTerm.leftCursor == iCur )
  1820. {
  1821. WhereClause tempWC = new WhereClause();
  1822. tempWC.pParse = pWC.pParse;
  1823. tempWC.pMaskSet = pWC.pMaskSet;
  1824. tempWC.op = TK_AND;
  1825. tempWC.a = new WhereTerm[2];
  1826. tempWC.a[0] = pOrTerm;
  1827. tempWC.nTerm = 1;
  1828. bestIndex( pParse, tempWC, pSrc, notReady, notValid, null, ref sTermCost );
  1829. }
  1830. else
  1831. {
  1832. continue;
  1833. }
  1834. rTotal += sTermCost.rCost;
  1835. nRow += sTermCost.plan.nRow;
  1836. used |= sTermCost.used;
  1837. if ( rTotal >= pCost.rCost )
  1838. break;
  1839. }
  1840. /* If there is an ORDER BY clause, increase the scan cost to account
  1841. ** for the cost of the sort. */
  1842. if ( pOrderBy != null )
  1843. {
  1844. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  1845. WHERETRACE( "... sorting increases OR cost %.9g to %.9g\n",
  1846. rTotal, rTotal + nRow * estLog( nRow ) );
  1847. #endif
  1848. rTotal += nRow * estLog( nRow );
  1849. }
  1850. /* If the cost of scanning using this OR term for optimization is
  1851. ** less than the current cost stored in pCost, replace the contents
  1852. ** of pCost. */
  1853. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  1854. WHERETRACE( "... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow );
  1855. #endif
  1856. if ( rTotal < pCost.rCost )
  1857. {
  1858. pCost.rCost = rTotal;
  1859. pCost.used = used;
  1860. pCost.plan.nRow = nRow;
  1861. pCost.plan.wsFlags = (uint)flags;
  1862. pCost.plan.u.pTerm = pTerm;
  1863. }
  1864. }
  1865. }
  1866. #endif //* SQLITE_OMIT_OR_OPTIMIZATION */
  1867. }
  1868. #if !SQLITE_OMIT_AUTOMATIC_INDEX
  1869. /*
  1870. ** Return TRUE if the WHERE clause term pTerm is of a form where it
  1871. ** could be used with an index to access pSrc, assuming an appropriate
  1872. ** index existed.
  1873. */
  1874. static int termCanDriveIndex(
  1875. WhereTerm pTerm, /* WHERE clause term to check */
  1876. SrcList_item pSrc, /* Table we are trying to access */
  1877. Bitmask notReady /* Tables in outer loops of the join */
  1878. )
  1879. {
  1880. char aff;
  1881. if ( pTerm.leftCursor != pSrc.iCursor )
  1882. return 0;
  1883. if ( pTerm.eOperator != WO_EQ )
  1884. return 0;
  1885. if ( ( pTerm.prereqRight & notReady ) != 0 )
  1886. return 0;
  1887. aff = pSrc.pTab.aCol[pTerm.u.leftColumn].affinity;
  1888. if ( !sqlite3IndexAffinityOk( pTerm.pExpr, aff ) )
  1889. return 0;
  1890. return 1;
  1891. }
  1892. #endif
  1893. #if !SQLITE_OMIT_AUTOMATIC_INDEX
  1894. /*
  1895. ** If the query plan for pSrc specified in pCost is a full table scan
  1896. ** and indexing is allows (if there is no NOT INDEXED clause) and it
  1897. ** possible to construct a transient index that would perform better
  1898. ** than a full table scan even when the cost of constructing the index
  1899. ** is taken into account, then alter the query plan to use the
  1900. ** transient index.
  1901. */
  1902. static void bestAutomaticIndex(
  1903. Parse pParse, /* The parsing context */
  1904. WhereClause pWC, /* The WHERE clause */
  1905. SrcList_item pSrc, /* The FROM clause term to search */
  1906. Bitmask notReady, /* Mask of cursors that are not available */
  1907. WhereCost pCost /* Lowest cost query plan */
  1908. )
  1909. {
  1910. double nTableRow; /* Rows in the input table */
  1911. double logN; /* log(nTableRow) */
  1912. double costTempIdx; /* per-query cost of the transient index */
  1913. WhereTerm pTerm; /* A single term of the WHERE clause */
  1914. WhereTerm pWCEnd; /* End of pWC.a[] */
  1915. Table pTable; /* Table tht might be indexed */
  1916. if ( ( pParse.db.flags & SQLITE_AutoIndex ) == 0 )
  1917. {
  1918. /* Automatic indices are disabled at run-time */
  1919. return;
  1920. }
  1921. if ( ( pCost.plan.wsFlags & WHERE_NOT_FULLSCAN ) != 0 )
  1922. {
  1923. /* We already have some kind of index in use for this query. */
  1924. return;
  1925. }
  1926. if ( pSrc.notIndexed != 0 )
  1927. {
  1928. /* The NOT INDEXED clause appears in the SQL. */
  1929. return;
  1930. }
  1931. Debug.Assert( pParse.nQueryLoop >= (double)1 );
  1932. pTable = pSrc.pTab;
  1933. nTableRow = pTable.nRowEst;
  1934. logN = estLog( nTableRow );
  1935. costTempIdx = 2 * logN * ( nTableRow / pParse.nQueryLoop + 1 );
  1936. if ( costTempIdx >= pCost.rCost )
  1937. {
  1938. /* The cost of creating the transient table would be greater than
  1939. ** doing the full table scan */
  1940. return;
  1941. }
  1942. /* Search for any equality comparison term */
  1943. //pWCEnd = pWC.a[pWC.nTerm];
  1944. for ( int ipTerm = 0; ipTerm < pWC.nTerm; ipTerm++ )//; pTerm<pWCEnd; pTerm++)
  1945. {
  1946. pTerm = pWC.a[ipTerm];
  1947. if ( termCanDriveIndex( pTerm, pSrc, notReady ) != 0 )
  1948. {
  1949. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  1950. WHERETRACE( "auto-index reduces cost from %.2f to %.2f\n",
  1951. pCost.rCost, costTempIdx );
  1952. #endif
  1953. pCost.rCost = costTempIdx;
  1954. pCost.plan.nRow = logN + 1;
  1955. pCost.plan.wsFlags = WHERE_TEMP_INDEX;
  1956. pCost.used = pTerm.prereqRight;
  1957. break;
  1958. }
  1959. }
  1960. }
  1961. #else
  1962. //# define bestAutomaticIndex(A,B,C,D,E) /* no-op */
  1963. static void bestAutomaticIndex(
  1964. Parse pParse, /* The parsing context */
  1965. WhereClause pWC, /* The WHERE clause */
  1966. SrcList_item pSrc, /* The FROM clause term to search */
  1967. Bitmask notReady, /* Mask of cursors that are not available */
  1968. WhereCost pCost /* Lowest cost query plan */
  1969. ){}
  1970. #endif //* SQLITE_OMIT_AUTOMATIC_INDEX */
  1971. #if !SQLITE_OMIT_AUTOMATIC_INDEX
  1972. /*
  1973. ** Generate code to construct the Index object for an automatic index
  1974. ** and to set up the WhereLevel object pLevel so that the code generator
  1975. ** makes use of the automatic index.
  1976. */
  1977. static void constructAutomaticIndex(
  1978. Parse pParse, /* The parsing context */
  1979. WhereClause pWC, /* The WHERE clause */
  1980. SrcList_item pSrc, /* The FROM clause term to get the next index */
  1981. Bitmask notReady, /* Mask of cursors that are not available */
  1982. WhereLevel pLevel /* Write new index here */
  1983. )
  1984. {
  1985. int nColumn; /* Number of columns in the constructed index */
  1986. WhereTerm pTerm; /* A single term of the WHERE clause */
  1987. WhereTerm pWCEnd; /* End of pWC.a[] */
  1988. int nByte; /* Byte of memory needed for pIdx */
  1989. Index pIdx; /* Object describing the transient index */
  1990. Vdbe v; /* Prepared statement under construction */
  1991. int regIsInit; /* Register set by initialization */
  1992. int addrInit; /* Address of the initialization bypass jump */
  1993. Table pTable; /* The table being indexed */
  1994. KeyInfo pKeyinfo; /* Key information for the index */
  1995. int addrTop; /* Top of the index fill loop */
  1996. int regRecord; /* Register holding an index record */
  1997. int n; /* Column counter */
  1998. int i; /* Loop counter */
  1999. int mxBitCol; /* Maximum column in pSrc.colUsed */
  2000. CollSeq pColl; /* Collating sequence to on a column */
  2001. Bitmask idxCols; /* Bitmap of columns used for indexing */
  2002. Bitmask extraCols; /* Bitmap of additional columns */
  2003. /* Generate code to skip over the creation and initialization of the
  2004. ** transient index on 2nd and subsequent iterations of the loop. */
  2005. v = pParse.pVdbe;
  2006. Debug.Assert( v != null );
  2007. regIsInit = ++pParse.nMem;
  2008. addrInit = sqlite3VdbeAddOp1( v, OP_If, regIsInit );
  2009. sqlite3VdbeAddOp2( v, OP_Integer, 1, regIsInit );
  2010. /* Count the number of columns that will be added to the index
  2011. ** and used to match WHERE clause constraints */
  2012. nColumn = 0;
  2013. pTable = pSrc.pTab;
  2014. //pWCEnd = pWC.a[pWC.nTerm];
  2015. idxCols = 0;
  2016. for ( int ipTerm = 0; ipTerm < pWC.nTerm; ipTerm++ )//; pTerm<pWCEnd; pTerm++)
  2017. {
  2018. pTerm = pWC.a[ipTerm];
  2019. if ( termCanDriveIndex( pTerm, pSrc, notReady ) != 0 )
  2020. {
  2021. int iCol = pTerm.u.leftColumn;
  2022. Bitmask cMask = iCol >= BMS ? ( (Bitmask)1 ) << ( BMS - 1 ) : ( (Bitmask)1 ) << iCol;
  2023. testcase( iCol == BMS );
  2024. testcase( iCol == BMS - 1 );
  2025. if ( ( idxCols & cMask ) == 0 )
  2026. {
  2027. nColumn++;
  2028. idxCols |= cMask;
  2029. }
  2030. }
  2031. }
  2032. Debug.Assert( nColumn > 0 );
  2033. pLevel.plan.nEq = (u32)nColumn;
  2034. /* Count the number of additional columns needed to create a
  2035. ** covering index. A "covering index" is an index that contains all
  2036. ** columns that are needed by the query. With a covering index, the
  2037. ** original table never needs to be accessed. Automatic indices must
  2038. ** be a covering index because the index will not be updated if the
  2039. ** original table changes and the index and table cannot both be used
  2040. ** if they go out of sync.
  2041. */
  2042. extraCols = pSrc.colUsed & ( ~idxCols | ( ( (Bitmask)1 ) << ( BMS - 1 ) ) );
  2043. mxBitCol = ( pTable.nCol >= BMS - 1 ) ? BMS - 1 : pTable.nCol;
  2044. testcase( pTable.nCol == BMS - 1 );
  2045. testcase( pTable.nCol == BMS - 2 );
  2046. for ( i = 0; i < mxBitCol; i++ )
  2047. {
  2048. if ( ( extraCols & ( ( (Bitmask)1 ) << i ) ) != 0 )
  2049. nColumn++;
  2050. }
  2051. if ( ( pSrc.colUsed & ( ( (Bitmask)1 ) << ( BMS - 1 ) ) ) != 0 )
  2052. {
  2053. nColumn += pTable.nCol - BMS + 1;
  2054. }
  2055. pLevel.plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ;
  2056. /* Construct the Index object to describe this index */
  2057. //nByte = sizeof(Index);
  2058. //nByte += nColumn*sizeof(int); /* Index.aiColumn */
  2059. //nByte += nColumn*sizeof(char*); /* Index.azColl */
  2060. //nByte += nColumn; /* Index.aSortOrder */
  2061. //pIdx = sqlite3DbMallocZero(pParse.db, nByte);
  2062. //if( pIdx==null) return;
  2063. pIdx = new Index();
  2064. pLevel.plan.u.pIdx = pIdx;
  2065. pIdx.azColl = new string[nColumn + 1];// pIdx[1];
  2066. pIdx.aiColumn = new int[nColumn + 1];// pIdx.azColl[nColumn];
  2067. pIdx.aSortOrder = new u8[nColumn + 1];// pIdx.aiColumn[nColumn];
  2068. pIdx.zName = "auto-index";
  2069. pIdx.nColumn = nColumn;
  2070. pIdx.pTable = pTable;
  2071. n = 0;
  2072. idxCols = 0;
  2073. //for(pTerm=pWC.a; pTerm<pWCEnd; pTerm++){
  2074. for ( int ipTerm = 0; ipTerm < pWC.nTerm; ipTerm++ )
  2075. {
  2076. pTerm = pWC.a[ipTerm];
  2077. if ( termCanDriveIndex( pTerm, pSrc, notReady ) != 0 )
  2078. {
  2079. int iCol = pTerm.u.leftColumn;
  2080. Bitmask cMask = iCol >= BMS ? ( (Bitmask)1 ) << ( BMS - 1 ) : ( (Bitmask)1 ) << iCol;
  2081. if ( ( idxCols & cMask ) == 0 )
  2082. {
  2083. Expr pX = pTerm.pExpr;
  2084. idxCols |= cMask;
  2085. pIdx.aiColumn[n] = pTerm.u.leftColumn;
  2086. pColl = sqlite3BinaryCompareCollSeq( pParse, pX.pLeft, pX.pRight );
  2087. pIdx.azColl[n] = ALWAYS( pColl != null ) ? pColl.zName : "BINARY";
  2088. n++;
  2089. }
  2090. }
  2091. }
  2092. Debug.Assert( (u32)n == pLevel.plan.nEq );
  2093. /* Add additional columns needed to make the automatic index into
  2094. ** a covering index */
  2095. for ( i = 0; i < mxBitCol; i++ )
  2096. {
  2097. if ( ( extraCols & ( ( (Bitmask)1 ) << i ) ) != 0 )
  2098. {
  2099. pIdx.aiColumn[n] = i;
  2100. pIdx.azColl[n] = "BINARY";
  2101. n++;
  2102. }
  2103. }
  2104. if ( ( pSrc.colUsed & ( ( (Bitmask)1 ) << ( BMS - 1 ) ) ) != 0 )
  2105. {
  2106. for ( i = BMS - 1; i < pTable.nCol; i++ )
  2107. {
  2108. pIdx.aiColumn[n] = i;
  2109. pIdx.azColl[n] = "BINARY";
  2110. n++;
  2111. }
  2112. }
  2113. Debug.Assert( n == nColumn );
  2114. /* Create the automatic index */
  2115. pKeyinfo = sqlite3IndexKeyinfo( pParse, pIdx );
  2116. Debug.Assert( pLevel.iIdxCur >= 0 );
  2117. sqlite3VdbeAddOp4( v, OP_OpenAutoindex, pLevel.iIdxCur, nColumn + 1, 0,
  2118. pKeyinfo, P4_KEYINFO_HANDOFF );
  2119. VdbeComment( v, "for %s", pTable.zName );
  2120. /* Fill the automatic index with content */
  2121. addrTop = sqlite3VdbeAddOp1( v, OP_Rewind, pLevel.iTabCur );
  2122. regRecord = sqlite3GetTempReg( pParse );
  2123. sqlite3GenerateIndexKey( pParse, pIdx, pLevel.iTabCur, regRecord, true );
  2124. sqlite3VdbeAddOp2( v, OP_IdxInsert, pLevel.iIdxCur, regRecord );
  2125. sqlite3VdbeChangeP5( v, OPFLAG_USESEEKRESULT );
  2126. sqlite3VdbeAddOp2( v, OP_Next, pLevel.iTabCur, addrTop + 1 );
  2127. sqlite3VdbeChangeP5( v, SQLITE_STMTSTATUS_AUTOINDEX );
  2128. sqlite3VdbeJumpHere( v, addrTop );
  2129. sqlite3ReleaseTempReg( pParse, regRecord );
  2130. /* Jump here when skipping the initialization */
  2131. sqlite3VdbeJumpHere( v, addrInit );
  2132. }
  2133. #endif //* SQLITE_OMIT_AUTOMATIC_INDEX */
  2134. #if !SQLITE_OMIT_VIRTUALTABLE
  2135. /*
  2136. ** Allocate and populate an sqlite3_index_info structure. It is the
  2137. ** responsibility of the caller to eventually release the structure
  2138. ** by passing the pointer returned by this function to //sqlite3_free().
  2139. */
  2140. static sqlite3_index_info *allocateIndexInfo(
  2141. Parse pParse,
  2142. WhereClause pWC,
  2143. SrcList_item pSrc,
  2144. ExprList *pOrderBy
  2145. ){
  2146. int i, j;
  2147. int nTerm;
  2148. struct sqlite3_index_constraint *pIdxCons;
  2149. struct sqlite3_index_orderby *pIdxOrderBy;
  2150. struct sqlite3_index_constraint_usage *pUsage;
  2151. WhereTerm pTerm;
  2152. int nOrderBy;
  2153. sqlite3_index_info *pIdxInfo;
  2154. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  2155. WHERETRACE("Recomputing index info for %s...\n", pSrc.pTab.zName);
  2156. #endif
  2157. /* Count the number of possible WHERE clause constraints referring
  2158. ** to this virtual table */
  2159. for(i=nTerm=0, pTerm=pWC.a; i<pWC.nTerm; i++, pTerm++){
  2160. if( pTerm.leftCursor != pSrc.iCursor ) continue;
  2161. Debug.Assert( (pTerm.eOperator&(pTerm.eOperator-1))==null );
  2162. testcase( pTerm.eOperator==WO_IN );
  2163. testcase( pTerm.eOperator==WO_ISNULL );
  2164. if( pTerm.eOperator & (WO_IN|WO_ISNULL) ) continue;
  2165. nTerm++;
  2166. }
  2167. /* If the ORDER BY clause contains only columns in the current
  2168. ** virtual table then allocate space for the aOrderBy part of
  2169. ** the sqlite3_index_info structure.
  2170. */
  2171. nOrderBy = 0;
  2172. if( pOrderBy ){
  2173. for(i=0; i<pOrderBy.nExpr; i++){
  2174. Expr pExpr = pOrderBy.a[i].pExpr;
  2175. if( pExpr.op!=TK_COLUMN || pExpr.iTable!=pSrc.iCursor ) break;
  2176. }
  2177. if( i==pOrderBy.nExpr ){
  2178. nOrderBy = pOrderBy.nExpr;
  2179. }
  2180. }
  2181. /* Allocate the sqlite3_index_info structure
  2182. */
  2183. pIdxInfo = new sqlite3_index_info();
  2184. //sqlite3DbMallocZero(pParse.db, sizeof(*pIdxInfo)
  2185. //+ (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
  2186. //+ sizeof(*pIdxOrderBy)*nOrderBy );
  2187. if( pIdxInfo==null ){
  2188. sqlite3ErrorMsg(pParse, "out of memory");
  2189. /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
  2190. return 0;
  2191. }
  2192. /* Initialize the structure. The sqlite3_index_info structure contains
  2193. ** many fields that are declared "const" to prevent xBestIndex from
  2194. ** changing them. We have to do some funky casting in order to
  2195. ** initialize those fields.
  2196. */
  2197. pIdxCons = (sqlite3_index_constraint)pIdxInfo[1];
  2198. pIdxOrderBy = (sqlite3_index_orderby)pIdxCons[nTerm];
  2199. pUsage = (sqlite3_index_constraint_usage)pIdxOrderBy[nOrderBy];
  2200. pIdxInfo.nConstraint = nTerm;
  2201. pIdxInfo.nOrderBy = nOrderBy;
  2202. pIdxInfo.aConstraint = pIdxCons;
  2203. pIdxInfo.aOrderBy = pIdxOrderBy;
  2204. pIdxInfo.aConstraintUsage =
  2205. pUsage;
  2206. for(i=j=0, pTerm=pWC.a; i<pWC.nTerm; i++, pTerm++){
  2207. if( pTerm.leftCursor != pSrc.iCursor ) continue;
  2208. Debug.Assert( (pTerm.eOperator&(pTerm.eOperator-1))==null );
  2209. testcase( pTerm.eOperator==WO_IN );
  2210. testcase( pTerm.eOperator==WO_ISNULL );
  2211. if( pTerm.eOperator & (WO_IN|WO_ISNULL) ) continue;
  2212. pIdxCons[j].iColumn = pTerm.u.leftColumn;
  2213. pIdxCons[j].iTermOffset = i;
  2214. pIdxCons[j].op = (u8)pTerm.eOperator;
  2215. /* The direct Debug.Assignment in the previous line is possible only because
  2216. ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The
  2217. ** following Debug.Asserts verify this fact. */
  2218. Debug.Assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
  2219. Debug.Assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
  2220. Debug.Assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
  2221. Debug.Assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
  2222. Debug.Assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
  2223. Debug.Assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
  2224. Debug.Assert( pTerm.eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
  2225. j++;
  2226. }
  2227. for(i=0; i<nOrderBy; i++){
  2228. Expr pExpr = pOrderBy.a[i].pExpr;
  2229. pIdxOrderBy[i].iColumn = pExpr.iColumn;
  2230. pIdxOrderBy[i].desc = pOrderBy.a[i].sortOrder;
  2231. }
  2232. return pIdxInfo;
  2233. }
  2234. /*
  2235. ** The table object reference passed as the second argument to this function
  2236. ** must represent a virtual table. This function invokes the xBestIndex()
  2237. ** method of the virtual table with the sqlite3_index_info pointer passed
  2238. ** as the argument.
  2239. **
  2240. ** If an error occurs, pParse is populated with an error message and a
  2241. ** non-zero value is returned. Otherwise, 0 is returned and the output
  2242. ** part of the sqlite3_index_info structure is left populated.
  2243. **
  2244. ** Whether or not an error is returned, it is the responsibility of the
  2245. ** caller to eventually free p.idxStr if p.needToFreeIdxStr indicates
  2246. ** that this is required.
  2247. */
  2248. static int vtabBestIndex(Parse pParse, Table pTab, sqlite3_index_info *p){
  2249. sqlite3_vtab *pVtab = sqlite3GetVTable(pParse.db, pTab).pVtab;
  2250. int i;
  2251. int rc;
  2252. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  2253. WHERETRACE("xBestIndex for %s\n", pTab.zName);
  2254. #endif
  2255. TRACE_IDX_INPUTS(p);
  2256. rc = pVtab.pModule.xBestIndex(pVtab, p);
  2257. TRACE_IDX_OUTPUTS(p);
  2258. if( rc!=SQLITE_OK ){
  2259. if( rc==SQLITE_NOMEM ){
  2260. pParse.db.mallocFailed = 1;
  2261. }else if( !pVtab.zErrMsg ){
  2262. sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
  2263. }else{
  2264. sqlite3ErrorMsg(pParse, "%s", pVtab.zErrMsg);
  2265. }
  2266. }
  2267. sqlite3_free(pVtab.zErrMsg);
  2268. pVtab.zErrMsg = 0;
  2269. for(i=0; i<p.nConstraint; i++){
  2270. if( !p.aConstraint[i].usable && p.aConstraintUsage[i].argvIndex>0 ){
  2271. sqlite3ErrorMsg(pParse,
  2272. "table %s: xBestIndex returned an invalid plan", pTab.zName);
  2273. }
  2274. }
  2275. return pParse.nErr;
  2276. }
  2277. /*
  2278. ** Compute the best index for a virtual table.
  2279. **
  2280. ** The best index is computed by the xBestIndex method of the virtual
  2281. ** table module. This routine is really just a wrapper that sets up
  2282. ** the sqlite3_index_info structure that is used to communicate with
  2283. ** xBestIndex.
  2284. **
  2285. ** In a join, this routine might be called multiple times for the
  2286. ** same virtual table. The sqlite3_index_info structure is created
  2287. ** and initialized on the first invocation and reused on all subsequent
  2288. ** invocations. The sqlite3_index_info structure is also used when
  2289. ** code is generated to access the virtual table. The whereInfoDelete()
  2290. ** routine takes care of freeing the sqlite3_index_info structure after
  2291. ** everybody has finished with it.
  2292. */
  2293. static void bestVirtualIndex(
  2294. Parse pParse, /* The parsing context */
  2295. WhereClause pWC, /* The WHERE clause */
  2296. SrcList_item pSrc, /* The FROM clause term to search */
  2297. Bitmask notReady, /* Mask of cursors not available for index */
  2298. Bitmask notValid, /* Cursors not valid for any purpose */
  2299. ExprList pOrderBy, /* The order by clause */
  2300. WhereCost pCost, /* Lowest cost query plan */
  2301. sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */
  2302. ){
  2303. Table pTab = pSrc.pTab;
  2304. sqlite3_index_info *pIdxInfo;
  2305. struct sqlite3_index_constraint *pIdxCons;
  2306. struct sqlite3_index_constraint_usage *pUsage;
  2307. WhereTerm pTerm;
  2308. int i, j;
  2309. int nOrderBy;
  2310. double rCost;
  2311. /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
  2312. ** malloc in allocateIndexInfo() fails and this function returns leaving
  2313. ** wsFlags in an uninitialized state, the caller may behave unpredictably.
  2314. */
  2315. memset(pCost, 0, sizeof(*pCost));
  2316. pCost.plan.wsFlags = WHERE_VIRTUALTABLE;
  2317. /* If the sqlite3_index_info structure has not been previously
  2318. ** allocated and initialized, then allocate and initialize it now.
  2319. */
  2320. pIdxInfo = *ppIdxInfo;
  2321. if( pIdxInfo==0 ){
  2322. *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy);
  2323. }
  2324. if( pIdxInfo==0 ){
  2325. return;
  2326. }
  2327. /* At this point, the sqlite3_index_info structure that pIdxInfo points
  2328. ** to will have been initialized, either during the current invocation or
  2329. ** during some prior invocation. Now we just have to customize the
  2330. ** details of pIdxInfo for the current invocation and pDebug.Ass it to
  2331. ** xBestIndex.
  2332. */
  2333. /* The module name must be defined. Also, by this point there must
  2334. ** be a pointer to an sqlite3_vtab structure. Otherwise
  2335. ** sqlite3ViewGetColumnNames() would have picked up the error.
  2336. */
  2337. Debug.Assert( pTab.azModuleArg && pTab.azModuleArg[0] );
  2338. Debug.Assert( sqlite3GetVTable(pParse.db, pTab) );
  2339. /* Set the aConstraint[].usable fields and initialize all
  2340. ** output variables to zero.
  2341. **
  2342. ** aConstraint[].usable is true for constraints where the right-hand
  2343. ** side contains only references to tables to the left of the current
  2344. ** table. In other words, if the constraint is of the form:
  2345. **
  2346. ** column = expr
  2347. **
  2348. ** and we are evaluating a join, then the constraint on column is
  2349. ** only valid if all tables referenced in expr occur to the left
  2350. ** of the table containing column.
  2351. **
  2352. ** The aConstraints[] array contains entries for all constraints
  2353. ** on the current table. That way we only have to compute it once
  2354. ** even though we might try to pick the best index multiple times.
  2355. ** For each attempt at picking an index, the order of tables in the
  2356. ** join might be different so we have to recompute the usable flag
  2357. ** each time.
  2358. */
  2359. pIdxCons = pIdxInfo.aConstraint;
  2360. pUsage = pIdxInfo.aConstraintUsage;
  2361. for(i=0; i<pIdxInfo.nConstraint; i++, pIdxCons++){
  2362. j = pIdxCons.iTermOffset;
  2363. pTerm = pWC.a[j];
  2364. pIdxCons.usable = (pTerm.prereqRight&notReady) ? 0 : 1;
  2365. pUsage[i] = new sqlite3_index_constraint_usage();
  2366. }
  2367. // memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo.nConstraint);
  2368. if( pIdxInfo.needToFreeIdxStr ){
  2369. //sqlite3_free(ref pIdxInfo.idxStr);
  2370. }
  2371. pIdxInfo.idxStr = 0;
  2372. pIdxInfo.idxNum = 0;
  2373. pIdxInfo.needToFreeIdxStr = 0;
  2374. pIdxInfo.orderByConsumed = 0;
  2375. /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */
  2376. pIdxInfo.estimatedCost = SQLITE_BIG_DBL / ((double)2);
  2377. nOrderBy = pIdxInfo.nOrderBy;
  2378. if( !pOrderBy ){
  2379. pIdxInfo.nOrderBy = 0;
  2380. }
  2381. if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
  2382. return;
  2383. }
  2384. pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo.aConstraint;
  2385. for(i=0; i<pIdxInfo.nConstraint; i++){
  2386. if( pUsage[i].argvIndex>0 ){
  2387. pCost.used |= pWC.a[pIdxCons[i].iTermOffset].prereqRight;
  2388. }
  2389. }
  2390. /* If there is an ORDER BY clause, and the selected virtual table index
  2391. ** does not satisfy it, increase the cost of the scan accordingly. This
  2392. ** matches the processing for non-virtual tables in bestBtreeIndex().
  2393. */
  2394. rCost = pIdxInfo.estimatedCost;
  2395. if( pOrderBy && pIdxInfo.orderByConsumed==0 ){
  2396. rCost += estLog(rCost)*rCost;
  2397. }
  2398. /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the
  2399. ** inital value of lowestCost in this loop. If it is, then the
  2400. ** (cost<lowestCost) test below will never be true.
  2401. **
  2402. ** Use "(double)2" instead of "2.0" in case OMIT_FLOATING_POINT
  2403. ** is defined.
  2404. */
  2405. if( (SQLITE_BIG_DBL/((double)2))<rCost ){
  2406. pCost.rCost = (SQLITE_BIG_DBL/((double)2));
  2407. }else{
  2408. pCost.rCost = rCost;
  2409. }
  2410. pCost.plan.u.pVtabIdx = pIdxInfo;
  2411. if( pIdxInfo.orderByConsumed ){
  2412. pCost.plan.wsFlags |= WHERE_ORDERBY;
  2413. }
  2414. pCost.plan.nEq = 0;
  2415. pIdxInfo.nOrderBy = nOrderBy;
  2416. /* Try to find a more efficient access pattern by using multiple indexes
  2417. ** to optimize an OR expression within the WHERE clause.
  2418. */
  2419. bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
  2420. }
  2421. #endif //* SQLITE_OMIT_VIRTUALTABLE */
  2422. /*
  2423. ** Argument pIdx is a pointer to an index structure that has an array of
  2424. ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
  2425. ** stored in Index.aSample. The domain of values stored in said column
  2426. ** may be thought of as divided into (SQLITE_INDEX_SAMPLES+1) regions.
  2427. ** Region 0 contains all values smaller than the first sample value. Region
  2428. ** 1 contains values larger than or equal to the value of the first sample,
  2429. ** but smaller than the value of the second. And so on.
  2430. **
  2431. ** If successful, this function determines which of the regions value
  2432. ** pVal lies in, sets *piRegion to the region index (a value between 0
  2433. ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
  2434. ** Or, if an OOM occurs while converting text values between encodings,
  2435. ** SQLITE_NOMEM is returned and *piRegion is undefined.
  2436. */
  2437. #if SQLITE_ENABLE_STAT2
  2438. static int whereRangeRegion(
  2439. Parse pParse, /* Database connection */
  2440. Index pIdx, /* Index to consider domain of */
  2441. sqlite3_value pVal, /* Value to consider */
  2442. ref int piRegion /* OUT: Region of domain in which value lies */
  2443. )
  2444. {
  2445. if ( ALWAYS( pVal ) )
  2446. {
  2447. IndexSample[] aSample = pIdx.aSample;
  2448. int i = 0;
  2449. int eType = sqlite3_value_type( pVal );
  2450. if ( eType == SQLITE_INTEGER || eType == SQLITE_FLOAT )
  2451. {
  2452. double r = sqlite3_value_double( pVal );
  2453. for ( i = 0; i < SQLITE_INDEX_SAMPLES; i++ )
  2454. {
  2455. if ( aSample[i].eType == SQLITE_NULL )
  2456. continue;
  2457. if ( aSample[i].eType >= SQLITE_TEXT || aSample[i].u.r > r )
  2458. break;
  2459. }
  2460. }
  2461. else
  2462. {
  2463. sqlite3 db = pParse.db;
  2464. CollSeq pColl;
  2465. string z;
  2466. int n;
  2467. /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
  2468. Debug.Assert( eType == SQLITE_TEXT || eType == SQLITE_BLOB );
  2469. if ( eType == SQLITE_BLOB )
  2470. {
  2471. z = Encoding.UTF8.GetString( sqlite3_value_blob( pVal ) );
  2472. pColl = db.pDfltColl;
  2473. Debug.Assert( pColl.enc == SQLITE_UTF8 );
  2474. }
  2475. else
  2476. {
  2477. pColl = sqlite3GetCollSeq( db, SQLITE_UTF8, null, pIdx.azColl[0] );
  2478. if ( pColl == null )
  2479. {
  2480. sqlite3ErrorMsg( pParse, "no such collation sequence: %s",
  2481. pIdx.azColl );
  2482. return SQLITE_ERROR;
  2483. }
  2484. z = sqlite3ValueText( pVal, pColl.enc );
  2485. //if( !z ){
  2486. // return SQLITE_NOMEM;
  2487. //}
  2488. Debug.Assert( z != "" && pColl != null && pColl.xCmp != null );
  2489. }
  2490. n = sqlite3ValueBytes( pVal, pColl.enc );
  2491. for ( i = 0; i < SQLITE_INDEX_SAMPLES; i++ )
  2492. {
  2493. int r;
  2494. int eSampletype = aSample[i].eType;
  2495. if ( eSampletype == SQLITE_NULL || eSampletype < eType )
  2496. continue;
  2497. if ( ( eSampletype != eType ) )
  2498. break;
  2499. #if !SQLITE_OMIT_UTF16
  2500. if( pColl.enc!=SQLITE_UTF8 ){
  2501. int nSample;
  2502. string zSample;
  2503. zSample = sqlite3Utf8to16(
  2504. db, pColl.enc, aSample[i].u.z, aSample[i].nByte, ref nSample
  2505. );
  2506. zSample = aSample[i].u.z;
  2507. nSample = aSample[i].u.z.Length;
  2508. //if( !zSample ){
  2509. // assert( db.mallocFailed );
  2510. // return SQLITE_NOMEM;
  2511. //}
  2512. r = pColl.xCmp(pColl.pUser, nSample, zSample, n, z);
  2513. sqlite3DbFree(db, ref zSample);
  2514. }else
  2515. #endif
  2516. {
  2517. r = pColl.xCmp( pColl.pUser, aSample[i].nByte, aSample[i].u.z, n, z );
  2518. }
  2519. if ( r > 0 )
  2520. break;
  2521. }
  2522. }
  2523. Debug.Assert( i >= 0 && i <= SQLITE_INDEX_SAMPLES );
  2524. piRegion = i;
  2525. }
  2526. return SQLITE_OK;
  2527. }
  2528. #endif //* #ifdef SQLITE_ENABLE_STAT2 */
  2529. /*
  2530. ** If expression pExpr represents a literal value, set *pp to point to
  2531. ** an sqlite3_value structure containing the same value, with affinity
  2532. ** aff applied to it, before returning. It is the responsibility of the
  2533. ** caller to eventually release this structure by passing it to
  2534. ** sqlite3ValueFree().
  2535. **
  2536. ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
  2537. ** is an SQL variable that currently has a non-NULL value bound to it,
  2538. ** create an sqlite3_value structure containing this value, again with
  2539. ** affinity aff applied to it, instead.
  2540. **
  2541. ** If neither of the above apply, set *pp to NULL.
  2542. **
  2543. ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
  2544. */
  2545. #if SQLITE_ENABLE_STAT2
  2546. static int valueFromExpr(
  2547. Parse pParse,
  2548. Expr pExpr,
  2549. char aff,
  2550. ref sqlite3_value pp
  2551. )
  2552. {
  2553. if ( pExpr.op == TK_VARIABLE
  2554. || ( pExpr.op == TK_REGISTER && pExpr.op2 == TK_VARIABLE )
  2555. )
  2556. {
  2557. int iVar = pExpr.iColumn;
  2558. sqlite3VdbeSetVarmask( pParse.pVdbe, iVar ); /* IMP: R-23257-02778 */
  2559. pp = sqlite3VdbeGetValue( pParse.pReprepare, iVar, (u8)aff );
  2560. return SQLITE_OK;
  2561. }
  2562. return sqlite3ValueFromExpr( pParse.db, pExpr, SQLITE_UTF8, aff, ref pp );
  2563. }
  2564. #endif
  2565. /*
  2566. ** This function is used to estimate the number of rows that will be visited
  2567. ** by scanning an index for a range of values. The range may have an upper
  2568. ** bound, a lower bound, or both. The WHERE clause terms that set the upper
  2569. ** and lower bounds are represented by pLower and pUpper respectively. For
  2570. ** example, assuming that index p is on t1(a):
  2571. **
  2572. ** ... FROM t1 WHERE a > ? AND a < ? ...
  2573. ** |_____| |_____|
  2574. ** | |
  2575. ** pLower pUpper
  2576. **
  2577. ** If either of the upper or lower bound is not present, then NULL is passed in
  2578. ** place of the corresponding WhereTerm.
  2579. **
  2580. ** The nEq parameter is passed the index of the index column subject to the
  2581. ** range constraint. Or, equivalently, the number of equality constraints
  2582. ** optimized by the proposed index scan. For example, assuming index p is
  2583. ** on t1(a, b), and the SQL query is:
  2584. **
  2585. ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
  2586. **
  2587. ** then nEq should be passed the value 1 (as the range restricted column,
  2588. ** b, is the second left-most column of the index). Or, if the query is:
  2589. **
  2590. ** ... FROM t1 WHERE a > ? AND a < ? ...
  2591. **
  2592. ** then nEq should be passed 0.
  2593. **
  2594. ** The returned value is an integer between 1 and 100, inclusive. A return
  2595. ** value of 1 indicates that the proposed range scan is expected to visit
  2596. ** approximately 1/100th (1%) of the rows selected by the nEq equality
  2597. ** constraints (if any). A return value of 100 indicates that it is expected
  2598. ** that the range scan will visit every row (100%) selected by the equality
  2599. ** constraints.
  2600. **
  2601. ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
  2602. ** reduces the search space by 2/3rds. Hence a single constraint (x>?)
  2603. ** results in a return of 33 and a range constraint (x>? AND x<?) results
  2604. ** in a return of 11.
  2605. */
  2606. static int whereRangeScanEst(
  2607. Parse pParse, /* Parsing & code generating context */
  2608. Index p, /* The index containing the range-compared column; "x" */
  2609. int nEq, /* index into p.aCol[] of the range-compared column */
  2610. WhereTerm pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
  2611. WhereTerm pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
  2612. ref int piEst /* OUT: Return value */
  2613. )
  2614. {
  2615. int rc = SQLITE_OK;
  2616. #if SQLITE_ENABLE_STAT2
  2617. if ( nEq == 0 && p.aSample != null )
  2618. {
  2619. sqlite3_value pLowerVal = null;
  2620. sqlite3_value pUpperVal = null;
  2621. int iEst;
  2622. int iLower = 0;
  2623. int iUpper = SQLITE_INDEX_SAMPLES;
  2624. char aff = p.pTable.aCol[p.aiColumn[0]].affinity;
  2625. if ( pLower != null )
  2626. {
  2627. Expr pExpr = pLower.pExpr.pRight;
  2628. rc = valueFromExpr( pParse, pExpr, aff, ref pLowerVal );
  2629. }
  2630. if ( rc == SQLITE_OK && pUpper != null )
  2631. {
  2632. Expr pExpr = pUpper.pExpr.pRight;
  2633. rc = valueFromExpr( pParse, pExpr, aff, ref pUpperVal );
  2634. }
  2635. if ( rc != SQLITE_OK || ( pLowerVal == null && pUpperVal == null ) )
  2636. {
  2637. sqlite3ValueFree( ref pLowerVal );
  2638. sqlite3ValueFree( ref pUpperVal );
  2639. goto range_est_fallback;
  2640. }
  2641. else if ( pLowerVal == null )
  2642. {
  2643. rc = whereRangeRegion( pParse, p, pUpperVal, ref iUpper );
  2644. if ( pLower != null )
  2645. iLower = iUpper / 2;
  2646. }
  2647. else if ( pUpperVal == null )
  2648. {
  2649. rc = whereRangeRegion( pParse, p, pLowerVal, ref iLower );
  2650. if ( pUpper != null )
  2651. iUpper = ( iLower + SQLITE_INDEX_SAMPLES + 1 ) / 2;
  2652. }
  2653. else
  2654. {
  2655. rc = whereRangeRegion( pParse, p, pUpperVal, ref iUpper );
  2656. if ( rc == SQLITE_OK )
  2657. {
  2658. rc = whereRangeRegion( pParse, p, pLowerVal, ref iLower );
  2659. }
  2660. }
  2661. iEst = iUpper - iLower;
  2662. testcase( iEst == SQLITE_INDEX_SAMPLES );
  2663. Debug.Assert( iEst <= SQLITE_INDEX_SAMPLES );
  2664. if ( iEst < 1 )
  2665. {
  2666. iEst = 1;
  2667. }
  2668. sqlite3ValueFree( ref pLowerVal );
  2669. sqlite3ValueFree( ref pUpperVal );
  2670. piEst = ( iEst * 100 ) / SQLITE_INDEX_SAMPLES;
  2671. return rc;
  2672. }
  2673. range_est_fallback:
  2674. #else
  2675. UNUSED_PARAMETER(pParse);
  2676. UNUSED_PARAMETER(p);
  2677. UNUSED_PARAMETER(nEq);
  2678. #endif
  2679. Debug.Assert( pLower != null || pUpper != null );
  2680. if ( pLower != null && pUpper != null )
  2681. {
  2682. piEst = 11;
  2683. }
  2684. else
  2685. {
  2686. piEst = 33;
  2687. }
  2688. return rc;
  2689. }
  2690. /*
  2691. ** Find the query plan for accessing a particular table. Write the
  2692. ** best query plan and its cost into the WhereCost object supplied as the
  2693. ** last parameter.
  2694. **
  2695. ** The lowest cost plan wins. The cost is an estimate of the amount of
  2696. ** CPU and disk I/O need to process the request using the selected plan.
  2697. ** Factors that influence cost include:
  2698. **
  2699. ** * The estimated number of rows that will be retrieved. (The
  2700. ** fewer the better.)
  2701. **
  2702. ** * Whether or not sorting must occur.
  2703. **
  2704. ** * Whether or not there must be separate lookups in the
  2705. ** index and in the main table.
  2706. **
  2707. ** If there was an INDEXED BY clause (pSrc.pIndex) attached to the table in
  2708. ** the SQL statement, then this function only considers plans using the
  2709. ** named index. If no such plan is found, then the returned cost is
  2710. ** SQLITE_BIG_DBL. If a plan is found that uses the named index,
  2711. ** then the cost is calculated in the usual way.
  2712. **
  2713. ** If a NOT INDEXED clause (pSrc.notIndexed!=0) was attached to the table
  2714. ** in the SELECT statement, then no indexes are considered. However, the
  2715. ** selected plan may still take advantage of the tables built-in rowid
  2716. ** index.
  2717. */
  2718. static void bestBtreeIndex(
  2719. Parse pParse, /* The parsing context */
  2720. WhereClause pWC, /* The WHERE clause */
  2721. SrcList_item pSrc, /* The FROM clause term to search */
  2722. Bitmask notReady, /* Mask of cursors not available for indexing */
  2723. Bitmask notValid, /* Cursors not available for any purpose */
  2724. ExprList pOrderBy, /* The ORDER BY clause */
  2725. ref WhereCost pCost /* Lowest cost query plan */
  2726. )
  2727. {
  2728. int iCur = pSrc.iCursor; /* The cursor of the table to be accessed */
  2729. Index pProbe; /* An index we are evaluating */
  2730. Index pIdx; /* Copy of pProbe, or zero for IPK index */
  2731. u32 eqTermMask; /* Current mask of valid equality operators */
  2732. u32 idxEqTermMask; /* Index mask of valid equality operators */
  2733. Index sPk; /* A fake index object for the primary key */
  2734. int[] aiRowEstPk = new int[2]; /* The aiRowEst[] value for the sPk index */
  2735. int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
  2736. int wsFlagMask; /* Allowed flags in pCost.plan.wsFlag */
  2737. /* Initialize the cost to a worst-case value */
  2738. if ( pCost == null )
  2739. pCost = new WhereCost();
  2740. else
  2741. pCost.Clear(); //memset(pCost, 0, sizeof(*pCost));
  2742. pCost.rCost = SQLITE_BIG_DBL;
  2743. /* If the pSrc table is the right table of a LEFT JOIN then we may not
  2744. ** use an index to satisfy IS NULL constraints on that table. This is
  2745. ** because columns might end up being NULL if the table does not match -
  2746. ** a circumstance which the index cannot help us discover. Ticket #2177.
  2747. */
  2748. if ( ( pSrc.jointype & JT_LEFT ) != 0 )
  2749. {
  2750. idxEqTermMask = WO_EQ | WO_IN;
  2751. }
  2752. else
  2753. {
  2754. idxEqTermMask = WO_EQ | WO_IN | WO_ISNULL;
  2755. }
  2756. if ( pSrc.pIndex != null )
  2757. {
  2758. /* An INDEXED BY clause specifies a particular index to use */
  2759. pIdx = pProbe = pSrc.pIndex;
  2760. wsFlagMask = ~( WHERE_ROWID_EQ | WHERE_ROWID_RANGE );
  2761. eqTermMask = idxEqTermMask;
  2762. }
  2763. else
  2764. {
  2765. /* There is no INDEXED BY clause. Create a fake Index object to
  2766. ** represent the primary key */
  2767. Index pFirst; /* Any other index on the table */
  2768. sPk = new Index(); // memset( &sPk, 0, sizeof( Index ) );
  2769. sPk.aSortOrder = new byte[1];
  2770. sPk.azColl = new string[1];
  2771. sPk.azColl[0] = "";
  2772. sPk.nColumn = 1;
  2773. sPk.aiColumn = new int[1];
  2774. sPk.aiColumn[0] = aiColumnPk;
  2775. sPk.aiRowEst = aiRowEstPk;
  2776. sPk.onError = OE_Replace;
  2777. sPk.pTable = pSrc.pTab;
  2778. aiRowEstPk[0] = (int)pSrc.pTab.nRowEst;
  2779. aiRowEstPk[1] = 1;
  2780. pFirst = pSrc.pTab.pIndex;
  2781. if ( pSrc.notIndexed == 0 )
  2782. {
  2783. sPk.pNext = pFirst;
  2784. }
  2785. pProbe = sPk;
  2786. wsFlagMask = ~(
  2787. WHERE_COLUMN_IN | WHERE_COLUMN_EQ | WHERE_COLUMN_NULL | WHERE_COLUMN_RANGE
  2788. );
  2789. eqTermMask = WO_EQ | WO_IN;
  2790. pIdx = null;
  2791. }
  2792. /* Loop over all indices looking for the best one to use
  2793. */
  2794. for ( ; pProbe != null; pIdx = pProbe = pProbe.pNext )
  2795. {
  2796. int[] aiRowEst = pProbe.aiRowEst;
  2797. double cost; /* Cost of using pProbe */
  2798. double nRow; /* Estimated number of rows in result set */
  2799. int rev = 0; /* True to scan in reverse order */
  2800. int wsFlags = 0;
  2801. Bitmask used = 0;
  2802. /* The following variables are populated based on the properties of
  2803. ** scan being evaluated. They are then used to determine the expected
  2804. ** cost and number of rows returned.
  2805. **
  2806. ** nEq:
  2807. ** Number of equality terms that can be implemented using the index.
  2808. **
  2809. ** nInMul:
  2810. ** The "in-multiplier". This is an estimate of how many seek operations
  2811. ** SQLite must perform on the index in question. For example, if the
  2812. ** WHERE clause is:
  2813. **
  2814. ** WHERE a IN (1, 2, 3) AND b IN (4, 5, 6)
  2815. **
  2816. ** SQLite must perform 9 lookups on an index on (a, b), so nInMul is
  2817. ** set to 9. Given the same schema and either of the following WHERE
  2818. ** clauses:
  2819. **
  2820. ** WHERE a = 1
  2821. ** WHERE a >= 2
  2822. **
  2823. ** nInMul is set to 1.
  2824. **
  2825. ** If there exists a WHERE term of the form "x IN (SELECT ...)", then
  2826. ** the sub-select is assumed to return 25 rows for the purposes of
  2827. ** determining nInMul.
  2828. **
  2829. ** bInEst:
  2830. ** Set to true if there was at least one "x IN (SELECT ...)" term used
  2831. ** in determining the value of nInMul.
  2832. **
  2833. ** estBound:
  2834. ** An estimate on the amount of the table that must be searched. A
  2835. ** value of 100 means the entire table is searched. Range constraints
  2836. ** might reduce this to a value less than 100 to indicate that only
  2837. ** a fraction of the table needs searching. In the absence of
  2838. ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
  2839. ** space to 1/3rd its original size. So an x>? constraint reduces
  2840. ** estBound to 33. Two constraints (x>? AND x<?) reduce estBound to 11.
  2841. **
  2842. ** bSort:
  2843. ** Boolean. True if there is an ORDER BY clause that will require an
  2844. ** external sort (i.e. scanning the index being evaluated will not
  2845. ** correctly order records).
  2846. **
  2847. ** bLookup:
  2848. ** Boolean. True if for each index entry visited a lookup on the
  2849. ** corresponding table b-tree is required. This is always false
  2850. ** for the rowid index. For other indexes, it is true unless all the
  2851. ** columns of the table used by the SELECT statement are present in
  2852. ** the index (such an index is sometimes described as a covering index).
  2853. ** For example, given the index on (a, b), the second of the following
  2854. ** two queries requires table b-tree lookups, but the first does not.
  2855. **
  2856. ** SELECT a, b FROM tbl WHERE a = 1;
  2857. ** SELECT a, b, c FROM tbl WHERE a = 1;
  2858. */
  2859. int nEq;
  2860. int bInEst = 0;
  2861. int nInMul = 1;
  2862. int estBound = 100;
  2863. int nBound = 0; /* Number of range constraints seen */
  2864. int bSort = 0;
  2865. int bLookup = 0;
  2866. WhereTerm pTerm; /* A single term of the WHERE clause */
  2867. /* Determine the values of nEq and nInMul */
  2868. for ( nEq = 0; nEq < pProbe.nColumn; nEq++ )
  2869. {
  2870. int j = pProbe.aiColumn[nEq];
  2871. pTerm = findTerm( pWC, iCur, j, notReady, eqTermMask, pIdx );
  2872. if ( pTerm == null )
  2873. break;
  2874. wsFlags |= ( WHERE_COLUMN_EQ | WHERE_ROWID_EQ );
  2875. if ( ( pTerm.eOperator & WO_IN ) != 0 )
  2876. {
  2877. Expr pExpr = pTerm.pExpr;
  2878. wsFlags |= WHERE_COLUMN_IN;
  2879. if ( ExprHasProperty( pExpr, EP_xIsSelect ) )
  2880. {
  2881. nInMul *= 25;
  2882. bInEst = 1;
  2883. }
  2884. else if ( ALWAYS( pExpr.x.pList != null ) )
  2885. {
  2886. nInMul *= pExpr.x.pList.nExpr + 1;
  2887. }
  2888. }
  2889. else if ( ( pTerm.eOperator & WO_ISNULL ) != 0 )
  2890. {
  2891. wsFlags |= WHERE_COLUMN_NULL;
  2892. }
  2893. used |= pTerm.prereqRight;
  2894. }
  2895. /* Determine the value of estBound. */
  2896. if ( nEq < pProbe.nColumn )
  2897. {
  2898. int j = pProbe.aiColumn[nEq];
  2899. if ( findTerm( pWC, iCur, j, notReady, WO_LT | WO_LE | WO_GT | WO_GE, pIdx ) != null )
  2900. {
  2901. WhereTerm pTop = findTerm( pWC, iCur, j, notReady, WO_LT | WO_LE, pIdx );
  2902. WhereTerm pBtm = findTerm( pWC, iCur, j, notReady, WO_GT | WO_GE, pIdx );
  2903. whereRangeScanEst( pParse, pProbe, nEq, pBtm, pTop, ref estBound );
  2904. if ( pTop != null )
  2905. {
  2906. nBound = 1;
  2907. wsFlags |= WHERE_TOP_LIMIT;
  2908. used |= pTop.prereqRight;
  2909. }
  2910. if ( pBtm != null )
  2911. {
  2912. nBound++;
  2913. wsFlags |= WHERE_BTM_LIMIT;
  2914. used |= pBtm.prereqRight;
  2915. }
  2916. wsFlags |= ( WHERE_COLUMN_RANGE | WHERE_ROWID_RANGE );
  2917. }
  2918. }
  2919. else if ( pProbe.onError != OE_None )
  2920. {
  2921. testcase( wsFlags & WHERE_COLUMN_IN );
  2922. testcase( wsFlags & WHERE_COLUMN_NULL );
  2923. if ( ( wsFlags & ( WHERE_COLUMN_IN | WHERE_COLUMN_NULL ) ) == 0 )
  2924. {
  2925. wsFlags |= WHERE_UNIQUE;
  2926. }
  2927. }
  2928. /* If there is an ORDER BY clause and the index being considered will
  2929. ** naturally scan rows in the required order, set the appropriate flags
  2930. ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
  2931. ** will scan rows in a different order, set the bSort variable. */
  2932. if ( pOrderBy != null )
  2933. {
  2934. if ( ( wsFlags & WHERE_COLUMN_IN ) == 0
  2935. && isSortingIndex( pParse, pWC.pMaskSet, pProbe, iCur, pOrderBy,
  2936. nEq, wsFlags, ref rev )
  2937. )
  2938. {
  2939. wsFlags |= WHERE_ROWID_RANGE | WHERE_COLUMN_RANGE | WHERE_ORDERBY;
  2940. wsFlags |= ( rev != 0 ? WHERE_REVERSE : 0 );
  2941. }
  2942. else
  2943. {
  2944. bSort = 1;
  2945. }
  2946. }
  2947. /* If currently calculating the cost of using an index (not the IPK
  2948. ** index), determine if all required column data may be obtained without
  2949. ** using the main table (i.e. if the index is a covering
  2950. ** index for this query). If it is, set the WHERE_IDX_ONLY flag in
  2951. ** wsFlags. Otherwise, set the bLookup variable to true. */
  2952. if ( pIdx != null && wsFlags != 0 )
  2953. {
  2954. Bitmask m = pSrc.colUsed;
  2955. int j;
  2956. for ( j = 0; j < pIdx.nColumn; j++ )
  2957. {
  2958. int x = pIdx.aiColumn[j];
  2959. if ( x < BMS - 1 )
  2960. {
  2961. m &= ~( ( (Bitmask)1 ) << x );
  2962. }
  2963. }
  2964. if ( m == 0 )
  2965. {
  2966. wsFlags |= WHERE_IDX_ONLY;
  2967. }
  2968. else
  2969. {
  2970. bLookup = 1;
  2971. }
  2972. }
  2973. /*
  2974. ** Estimate the number of rows of output. For an IN operator,
  2975. ** do not let the estimate exceed half the rows in the table.
  2976. */
  2977. nRow = (double)( aiRowEst[nEq] * nInMul );
  2978. if ( bInEst != 0 && nRow * 2 > aiRowEst[0] )
  2979. {
  2980. nRow = aiRowEst[0] / 2;
  2981. nInMul = (int)( nRow / aiRowEst[nEq] );
  2982. }
  2983. /* Assume constant cost to access a row and logarithmic cost to
  2984. ** do a binary search. Hence, the initial cost is the number of output
  2985. ** rows plus log2(table-size) times the number of binary searches.
  2986. */
  2987. cost = nRow + nInMul * estLog( aiRowEst[0] );
  2988. /* Adjust the number of rows and the cost downward to reflect rows
  2989. ** that are excluded by range constraints.
  2990. */
  2991. nRow = ( nRow * (double)estBound ) / (double)100;
  2992. cost = ( cost * (double)estBound ) / (double)100;
  2993. /* Add in the estimated cost of sorting the result
  2994. */
  2995. if ( bSort != 0 )
  2996. {
  2997. cost += cost * estLog( cost );
  2998. }
  2999. /* If all information can be taken directly from the index, we avoid
  3000. ** doing table lookups. This reduces the cost by half. (Not really -
  3001. ** this needs to be fixed.)
  3002. */
  3003. if ( pIdx != null && bLookup == 0 )
  3004. {
  3005. cost /= (double)2;
  3006. }
  3007. /**** Cost of using this index has now been computed ****/
  3008. /* If there are additional constraints on this table that cannot
  3009. ** be used with the current index, but which might lower the number
  3010. ** of output rows, adjust the nRow value accordingly. This only
  3011. ** matters if the current index is the least costly, so do not bother
  3012. ** with this step if we already know this index will not be chosen.
  3013. ** Also, never reduce the output row count below 2 using this step.
  3014. **
  3015. ** It is critical that the notValid mask be used here instead of
  3016. ** the notReady mask. When computing an "optimal" index, the notReady
  3017. ** mask will only have one bit set - the bit for the current table.
  3018. ** The notValid mask, on the other hand, always has all bits set for
  3019. ** tables that are not in outer loops. If notReady is used here instead
  3020. ** of notValid, then a optimal index that depends on inner joins loops
  3021. ** might be selected even when there exists an optimal index that has
  3022. ** no such dependency.
  3023. */
  3024. if ( nRow > 2 && cost <= pCost.rCost )
  3025. {
  3026. //int k; /* Loop counter */
  3027. int nSkipEq = nEq; /* Number of == constraints to skip */
  3028. int nSkipRange = nBound; /* Number of < constraints to skip */
  3029. Bitmask thisTab; /* Bitmap for pSrc */
  3030. thisTab = getMask( pWC.pMaskSet, iCur );
  3031. for ( int ipTerm = 0, k = pWC.nTerm; nRow > 2 && k != 0; k--, ipTerm++ )//pTerm++)
  3032. {
  3033. pTerm = pWC.a[ipTerm];
  3034. if ( ( pTerm.wtFlags & TERM_VIRTUAL ) != 0 )
  3035. continue;
  3036. if ( ( pTerm.prereqAll & notValid ) != thisTab )
  3037. continue;
  3038. if ( ( pTerm.eOperator & ( WO_EQ | WO_IN | WO_ISNULL ) ) != 0 )
  3039. {
  3040. if ( nSkipEq != 0 )
  3041. {
  3042. /* Ignore the first nEq equality matches since the index
  3043. ** has already accounted for these */
  3044. nSkipEq--;
  3045. }
  3046. else
  3047. {
  3048. /* Assume each additional equality match reduces the result
  3049. ** set size by a factor of 10 */
  3050. nRow /= 10;
  3051. }
  3052. }
  3053. else if ( ( pTerm.eOperator & ( WO_LT | WO_LE | WO_GT | WO_GE ) ) != 0 )
  3054. {
  3055. if ( nSkipRange != 0 )
  3056. {
  3057. /* Ignore the first nBound range constraints since the index
  3058. ** has already accounted for these */
  3059. nSkipRange--;
  3060. }
  3061. else
  3062. {
  3063. /* Assume each additional range constraint reduces the result
  3064. ** set size by a factor of 3 */
  3065. nRow /= 3;
  3066. }
  3067. }
  3068. else
  3069. {
  3070. /* Any other expression lowers the output row count by half */
  3071. nRow /= 2;
  3072. }
  3073. }
  3074. if ( nRow < 2 )
  3075. nRow = 2;
  3076. }
  3077. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  3078. WHERETRACE(
  3079. "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" +
  3080. " notReady=0x%llx nRow=%.2f cost=%.2f used=0x%llx\n",
  3081. pSrc.pTab.zName, ( pIdx != null ? pIdx.zName : "ipk" ),
  3082. nEq, nInMul, estBound, bSort, bLookup, wsFlags,
  3083. notReady, nRow, cost, used
  3084. );
  3085. #endif
  3086. /* If this index is the best we have seen so far, then record this
  3087. ** index and its cost in the pCost structure.
  3088. */
  3089. if ( ( null == pIdx || wsFlags != 0 )
  3090. && ( cost < pCost.rCost || ( cost <= pCost.rCost && nRow < pCost.plan.nRow ) )
  3091. )
  3092. {
  3093. pCost.rCost = cost;
  3094. pCost.used = used;
  3095. pCost.plan.nRow = nRow;
  3096. pCost.plan.wsFlags = (uint)( wsFlags & wsFlagMask );
  3097. pCost.plan.nEq = (uint)nEq;
  3098. pCost.plan.u.pIdx = pIdx;
  3099. }
  3100. /* If there was an INDEXED BY clause, then only that one index is
  3101. ** considered. */
  3102. if ( pSrc.pIndex != null )
  3103. break;
  3104. /* Reset masks for the next index in the loop */
  3105. wsFlagMask = ~( WHERE_ROWID_EQ | WHERE_ROWID_RANGE );
  3106. eqTermMask = idxEqTermMask;
  3107. }
  3108. /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag
  3109. ** is set, then reverse the order that the index will be scanned
  3110. ** in. This is used for application testing, to help find cases
  3111. ** where application behaviour depends on the (undefined) order that
  3112. ** SQLite outputs rows in in the absence of an ORDER BY clause. */
  3113. if ( null == pOrderBy && ( pParse.db.flags & SQLITE_ReverseOrder ) != 0 )
  3114. {
  3115. pCost.plan.wsFlags |= WHERE_REVERSE;
  3116. }
  3117. Debug.Assert( pOrderBy != null || ( pCost.plan.wsFlags & WHERE_ORDERBY ) == 0 );
  3118. Debug.Assert( pCost.plan.u.pIdx == null || ( pCost.plan.wsFlags & WHERE_ROWID_EQ ) == 0 );
  3119. Debug.Assert( pSrc.pIndex == null
  3120. || pCost.plan.u.pIdx == null
  3121. || pCost.plan.u.pIdx == pSrc.pIndex
  3122. );
  3123. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  3124. WHERETRACE( "best index is: %s\n",
  3125. ( ( pCost.plan.wsFlags & WHERE_NOT_FULLSCAN ) == 0 ? "none" :
  3126. pCost.plan.u.pIdx != null ? pCost.plan.u.pIdx.zName : "ipk" )
  3127. );
  3128. #endif
  3129. bestOrClauseIndex( pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost );
  3130. bestAutomaticIndex( pParse, pWC, pSrc, notReady, pCost );
  3131. pCost.plan.wsFlags |= (u32)eqTermMask;
  3132. }
  3133. /*
  3134. ** Find the query plan for accessing table pSrc.pTab. Write the
  3135. ** best query plan and its cost into the WhereCost object supplied
  3136. ** as the last parameter. This function may calculate the cost of
  3137. ** both real and virtual table scans.
  3138. */
  3139. static void bestIndex(
  3140. Parse pParse, /* The parsing context */
  3141. WhereClause pWC, /* The WHERE clause */
  3142. SrcList_item pSrc, /* The FROM clause term to search */
  3143. Bitmask notReady, /* Mask of cursors not available for indexing */
  3144. Bitmask notValid, /* Cursors not available for any purpose */
  3145. ExprList pOrderBy, /* The ORDER BY clause */
  3146. ref WhereCost pCost /* Lowest cost query plan */
  3147. )
  3148. {
  3149. #if !SQLITE_OMIT_VIRTUALTABLE
  3150. if ( IsVirtual( pSrc.pTab ) )
  3151. {
  3152. sqlite3_index_info p = null;
  3153. bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost, p);
  3154. if( p.needToFreeIdxStr !=0){
  3155. //sqlite3_free(ref p.idxStr);
  3156. }
  3157. sqlite3DbFree(pParse.db, p);
  3158. }
  3159. else
  3160. #endif
  3161. {
  3162. bestBtreeIndex( pParse, pWC, pSrc, notReady, notValid, pOrderBy, ref pCost );
  3163. }
  3164. }
  3165. /*
  3166. ** Disable a term in the WHERE clause. Except, do not disable the term
  3167. ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
  3168. ** or USING clause of that join.
  3169. **
  3170. ** Consider the term t2.z='ok' in the following queries:
  3171. **
  3172. ** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
  3173. ** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
  3174. ** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
  3175. **
  3176. ** The t2.z='ok' is disabled in the in (2) because it originates
  3177. ** in the ON clause. The term is disabled in (3) because it is not part
  3178. ** of a LEFT OUTER JOIN. In (1), the term is not disabled.
  3179. **
  3180. ** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are
  3181. ** completely satisfied by indices.
  3182. **
  3183. ** Disabling a term causes that term to not be tested in the inner loop
  3184. ** of the join. Disabling is an optimization. When terms are satisfied
  3185. ** by indices, we disable them to prevent redundant tests in the inner
  3186. ** loop. We would get the correct results if nothing were ever disabled,
  3187. ** but joins might run a little slower. The trick is to disable as much
  3188. ** as we can without disabling too much. If we disabled in (1), we'd get
  3189. ** the wrong answer. See ticket #813.
  3190. */
  3191. static void disableTerm( WhereLevel pLevel, WhereTerm pTerm )
  3192. {
  3193. if ( pTerm != null
  3194. && ( pTerm.wtFlags & TERM_CODED ) == 0
  3195. && ( pLevel.iLeftJoin == 0 || ExprHasProperty( pTerm.pExpr, EP_FromJoin ) ) )
  3196. {
  3197. pTerm.wtFlags |= TERM_CODED;
  3198. if ( pTerm.iParent >= 0 )
  3199. {
  3200. WhereTerm pOther = pTerm.pWC.a[pTerm.iParent];
  3201. if ( ( --pOther.nChild ) == 0 )
  3202. {
  3203. disableTerm( pLevel, pOther );
  3204. }
  3205. }
  3206. }
  3207. }
  3208. /*
  3209. ** Code an OP_Affinity opcode to apply the column affinity string zAff
  3210. ** to the n registers starting at base.
  3211. **
  3212. ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
  3213. ** beginning and end of zAff are ignored. If all entries in zAff are
  3214. ** SQLITE_AFF_NONE, then no code gets generated.
  3215. **
  3216. ** This routine makes its own copy of zAff so that the caller is free
  3217. ** to modify zAff after this routine returns.
  3218. */
  3219. static void codeApplyAffinity( Parse pParse, int _base, int n, string zAff )
  3220. {
  3221. Vdbe v = pParse.pVdbe;
  3222. //if (zAff == 0)
  3223. //{
  3224. // assert(pParse.db.mallocFailed);
  3225. // return;
  3226. //}
  3227. Debug.Assert( v != null );
  3228. /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
  3229. ** and end of the affinity string.
  3230. */
  3231. while ( n > 0 && zAff[0] == SQLITE_AFF_NONE )
  3232. {
  3233. n--;
  3234. _base++;
  3235. zAff = zAff.Substring( 1 );// zAff++;
  3236. }
  3237. while ( n > 1 && zAff[n - 1] == SQLITE_AFF_NONE )
  3238. {
  3239. n--;
  3240. }
  3241. /* Code the OP_Affinity opcode if there is anything left to do. */
  3242. if ( n > 0 )
  3243. {
  3244. sqlite3VdbeAddOp2( v, OP_Affinity, _base, n );
  3245. sqlite3VdbeChangeP4( v, -1, zAff, n );
  3246. sqlite3ExprCacheAffinityChange( pParse, _base, n );
  3247. }
  3248. }
  3249. /*
  3250. ** Generate code for a single equality term of the WHERE clause. An equality
  3251. ** term can be either X=expr or X IN (...). pTerm is the term to be
  3252. ** coded.
  3253. **
  3254. ** The current value for the constraint is left in register iReg.
  3255. **
  3256. ** For a constraint of the form X=expr, the expression is evaluated and its
  3257. ** result is left on the stack. For constraints of the form X IN (...)
  3258. ** this routine sets up a loop that will iterate over all values of X.
  3259. */
  3260. static int codeEqualityTerm(
  3261. Parse pParse, /* The parsing context */
  3262. WhereTerm pTerm, /* The term of the WHERE clause to be coded */
  3263. WhereLevel pLevel, /* When level of the FROM clause we are working on */
  3264. int iTarget /* Attempt to leave results in this register */
  3265. )
  3266. {
  3267. Expr pX = pTerm.pExpr;
  3268. Vdbe v = pParse.pVdbe;
  3269. int iReg; /* Register holding results */
  3270. Debug.Assert( iTarget > 0 );
  3271. if ( pX.op == TK_EQ )
  3272. {
  3273. iReg = sqlite3ExprCodeTarget( pParse, pX.pRight, iTarget );
  3274. }
  3275. else if ( pX.op == TK_ISNULL )
  3276. {
  3277. iReg = iTarget;
  3278. sqlite3VdbeAddOp2( v, OP_Null, 0, iReg );
  3279. #if !SQLITE_OMIT_SUBQUERY
  3280. }
  3281. else
  3282. {
  3283. int eType;
  3284. int iTab;
  3285. InLoop pIn;
  3286. Debug.Assert( pX.op == TK_IN );
  3287. iReg = iTarget;
  3288. int iDummy = -1;
  3289. eType = sqlite3FindInIndex( pParse, pX, ref iDummy );
  3290. iTab = pX.iTable;
  3291. sqlite3VdbeAddOp2( v, OP_Rewind, iTab, 0 );
  3292. Debug.Assert( ( pLevel.plan.wsFlags & WHERE_IN_ABLE ) != 0 );
  3293. if ( pLevel.u._in.nIn == 0 )
  3294. {
  3295. pLevel.addrNxt = sqlite3VdbeMakeLabel( v );
  3296. }
  3297. pLevel.u._in.nIn++;
  3298. if ( pLevel.u._in.aInLoop == null )
  3299. pLevel.u._in.aInLoop = new InLoop[pLevel.u._in.nIn];
  3300. else
  3301. Array.Resize( ref pLevel.u._in.aInLoop, pLevel.u._in.nIn );
  3302. //sqlite3DbReallocOrFree(pParse.db, pLevel.u._in.aInLoop,
  3303. // sizeof(pLevel.u._in.aInLoop[0])*pLevel.u._in.nIn);
  3304. //pIn = pLevel.u._in.aInLoop;
  3305. if ( pLevel.u._in.aInLoop != null )//(pIn )
  3306. {
  3307. pLevel.u._in.aInLoop[pLevel.u._in.nIn - 1] = new InLoop();
  3308. pIn = pLevel.u._in.aInLoop[pLevel.u._in.nIn - 1];//pIn++
  3309. pIn.iCur = iTab;
  3310. if ( eType == IN_INDEX_ROWID )
  3311. {
  3312. pIn.addrInTop = sqlite3VdbeAddOp2( v, OP_Rowid, iTab, iReg );
  3313. }
  3314. else
  3315. {
  3316. pIn.addrInTop = sqlite3VdbeAddOp3( v, OP_Column, iTab, 0, iReg );
  3317. }
  3318. sqlite3VdbeAddOp1( v, OP_IsNull, iReg );
  3319. }
  3320. else
  3321. {
  3322. pLevel.u._in.nIn = 0;
  3323. }
  3324. #endif
  3325. }
  3326. disableTerm( pLevel, pTerm );
  3327. return iReg;
  3328. }
  3329. /*
  3330. ** Generate code for a single equality term of the WHERE clause. An equality
  3331. ** term can be either X=expr or X IN (...). pTerm is the term to be
  3332. ** coded.
  3333. **
  3334. ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
  3335. ** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10
  3336. ** The index has as many as three equality constraints, but in this
  3337. ** example, the third "c" value is an inequality. So only two
  3338. ** constraints are coded. This routine will generate code to evaluate
  3339. ** a==5 and b IN (1,2,3). The current values for a and b will be stored
  3340. ** in consecutive registers and the index of the first register is returned.
  3341. **
  3342. ** In the example above nEq==2. But this subroutine works for any value
  3343. ** of nEq including 0. If nEq==null, this routine is nearly a no-op.
  3344. ** The only thing it does is allocate the pLevel.iMem memory cell and
  3345. ** compute the affinity string.
  3346. **
  3347. ** This routine always allocates at least one memory cell and returns
  3348. ** the index of that memory cell. The code that
  3349. ** calls this routine will use that memory cell to store the termination
  3350. ** key value of the loop. If one or more IN operators appear, then
  3351. ** this routine allocates an additional nEq memory cells for internal
  3352. ** use.
  3353. **
  3354. ** Before returning, *pzAff is set to point to a buffer containing a
  3355. ** copy of the column affinity string of the index allocated using
  3356. ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
  3357. ** with equality constraints that use NONE affinity are set to
  3358. ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
  3359. **
  3360. ** CREATE TABLE t1(a TEXT PRIMARY KEY, b);
  3361. ** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
  3362. **
  3363. ** In the example above, the index on t1(a) has TEXT affinity. But since
  3364. ** the right hand side of the equality constraint (t2.b) has NONE affinity,
  3365. ** no conversion should be attempted before using a t2.b value as part of
  3366. ** a key to search the index. Hence the first byte in the returned affinity
  3367. ** string in this example would be set to SQLITE_AFF_NONE.
  3368. */
  3369. static int codeAllEqualityTerms(
  3370. Parse pParse, /* Parsing context */
  3371. WhereLevel pLevel, /* Which nested loop of the FROM we are coding */
  3372. WhereClause pWC, /* The WHERE clause */
  3373. Bitmask notReady, /* Which parts of FROM have not yet been coded */
  3374. int nExtraReg, /* Number of extra registers to allocate */
  3375. ref StringBuilder pzAff /* OUT: Set to point to affinity string */
  3376. )
  3377. {
  3378. int nEq = (int)pLevel.plan.nEq; /* The number of == or IN constraints to code */
  3379. Vdbe v = pParse.pVdbe; /* The vm under construction */
  3380. Index pIdx; /* The index being used for this loop */
  3381. int iCur = pLevel.iTabCur; /* The cursor of the table */
  3382. WhereTerm pTerm; /* A single constraint term */
  3383. int j; /* Loop counter */
  3384. int regBase; /* Base register */
  3385. int nReg; /* Number of registers to allocate */
  3386. StringBuilder zAff; /* Affinity string to return */
  3387. /* This module is only called on query plans that use an index. */
  3388. Debug.Assert( ( pLevel.plan.wsFlags & WHERE_INDEXED ) != 0 );
  3389. pIdx = pLevel.plan.u.pIdx;
  3390. /* Figure out how many memory cells we will need then allocate them.
  3391. */
  3392. regBase = pParse.nMem + 1;
  3393. nReg = (int)( pLevel.plan.nEq + nExtraReg );
  3394. pParse.nMem += nReg;
  3395. zAff = new StringBuilder( sqlite3IndexAffinityStr( v, pIdx ) );//sqlite3DbStrDup(pParse.db, sqlite3IndexAffinityStr(v, pIdx));
  3396. //if( !zAff ){
  3397. // pParse.db.mallocFailed = 1;
  3398. //}
  3399. /* Evaluate the equality constraints
  3400. */
  3401. Debug.Assert( pIdx.nColumn >= nEq );
  3402. for ( j = 0; j < nEq; j++ )
  3403. {
  3404. int r1;
  3405. int k = pIdx.aiColumn[j];
  3406. pTerm = findTerm( pWC, iCur, k, notReady, pLevel.plan.wsFlags, pIdx );
  3407. if ( NEVER( pTerm == null ) )
  3408. break;
  3409. /* The following true for indices with redundant columns.
  3410. ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
  3411. testcase( ( pTerm.wtFlags & TERM_CODED ) != 0 );
  3412. testcase( pTerm.wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3413. r1 = codeEqualityTerm( pParse, pTerm, pLevel, regBase + j );
  3414. if ( r1 != regBase + j )
  3415. {
  3416. if ( nReg == 1 )
  3417. {
  3418. sqlite3ReleaseTempReg( pParse, regBase );
  3419. regBase = r1;
  3420. }
  3421. else
  3422. {
  3423. sqlite3VdbeAddOp2( v, OP_SCopy, r1, regBase + j );
  3424. }
  3425. }
  3426. testcase( pTerm.eOperator & WO_ISNULL );
  3427. testcase( pTerm.eOperator & WO_IN );
  3428. if ( ( pTerm.eOperator & ( WO_ISNULL | WO_IN ) ) == 0 )
  3429. {
  3430. Expr pRight = pTerm.pExpr.pRight;
  3431. sqlite3ExprCodeIsNullJump( v, pRight, regBase + j, pLevel.addrBrk );
  3432. if ( zAff.Length > 0 )
  3433. {
  3434. if ( sqlite3CompareAffinity( pRight, zAff[j] ) == SQLITE_AFF_NONE )
  3435. {
  3436. zAff[j] = SQLITE_AFF_NONE;
  3437. }
  3438. if ( ( sqlite3ExprNeedsNoAffinityChange( pRight, zAff[j] ) ) != 0 )
  3439. {
  3440. zAff[j] = SQLITE_AFF_NONE;
  3441. }
  3442. }
  3443. }
  3444. }
  3445. pzAff = zAff;
  3446. return regBase;
  3447. }
  3448. #if !SQLITE_OMIT_EXPLAIN
  3449. /*
  3450. ** This routine is a helper for explainIndexRange() below
  3451. **
  3452. ** pStr holds the text of an expression that we are building up one term
  3453. ** at a time. This routine adds a new term to the end of the expression.
  3454. ** Terms are separated by AND so add the "AND" text for second and subsequent
  3455. ** terms only.
  3456. */
  3457. static void explainAppendTerm(
  3458. StrAccum pStr, /* The text expression being built */
  3459. int iTerm, /* Index of this term. First is zero */
  3460. string zColumn, /* Name of the column */
  3461. string zOp /* Name of the operator */
  3462. )
  3463. {
  3464. if ( iTerm != 0 )
  3465. sqlite3StrAccumAppend( pStr, " AND ", 5 );
  3466. sqlite3StrAccumAppend( pStr, zColumn, -1 );
  3467. sqlite3StrAccumAppend( pStr, zOp, 1 );
  3468. sqlite3StrAccumAppend( pStr, "?", 1 );
  3469. }
  3470. /*
  3471. ** Argument pLevel describes a strategy for scanning table pTab. This
  3472. ** function returns a pointer to a string buffer containing a description
  3473. ** of the subset of table rows scanned by the strategy in the form of an
  3474. ** SQL expression. Or, if all rows are scanned, NULL is returned.
  3475. **
  3476. ** For example, if the query:
  3477. **
  3478. ** SELECT * FROM t1 WHERE a=1 AND b>2;
  3479. **
  3480. ** is run and there is an index on (a, b), then this function returns a
  3481. ** string similar to:
  3482. **
  3483. ** "a=? AND b>?"
  3484. **
  3485. ** The returned pointer points to memory obtained from sqlite3DbMalloc().
  3486. ** It is the responsibility of the caller to free the buffer when it is
  3487. ** no longer required.
  3488. */
  3489. static string explainIndexRange( sqlite3 db, WhereLevel pLevel, Table pTab )
  3490. {
  3491. WherePlan pPlan = pLevel.plan;
  3492. Index pIndex = pPlan.u.pIdx;
  3493. uint nEq = pPlan.nEq;
  3494. int i, j;
  3495. Column[] aCol = pTab.aCol;
  3496. int[] aiColumn = pIndex.aiColumn;
  3497. StrAccum txt = new StrAccum( 100 );
  3498. if ( nEq == 0 && ( pPlan.wsFlags & ( WHERE_BTM_LIMIT | WHERE_TOP_LIMIT ) ) == 0 )
  3499. {
  3500. return null;
  3501. }
  3502. sqlite3StrAccumInit( txt, null, 0, SQLITE_MAX_LENGTH );
  3503. txt.db = db;
  3504. sqlite3StrAccumAppend( txt, " (", 2 );
  3505. for ( i = 0; i < nEq; i++ )
  3506. {
  3507. explainAppendTerm( txt, i, aCol[aiColumn[i]].zName, "=" );
  3508. }
  3509. j = i;
  3510. if ( ( pPlan.wsFlags & WHERE_BTM_LIMIT ) != 0 )
  3511. {
  3512. explainAppendTerm( txt, i++, aCol[aiColumn[j]].zName, ">" );
  3513. }
  3514. if ( ( pPlan.wsFlags & WHERE_TOP_LIMIT ) != 0 )
  3515. {
  3516. explainAppendTerm( txt, i, aCol[aiColumn[j]].zName, "<" );
  3517. }
  3518. sqlite3StrAccumAppend( txt, ")", 1 );
  3519. return sqlite3StrAccumFinish( txt );
  3520. }
  3521. /*
  3522. ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
  3523. ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
  3524. ** record is added to the output to describe the table scan strategy in
  3525. ** pLevel.
  3526. */
  3527. static void explainOneScan(
  3528. Parse pParse, /* Parse context */
  3529. SrcList pTabList, /* Table list this loop refers to */
  3530. WhereLevel pLevel, /* Scan to write OP_Explain opcode for */
  3531. int iLevel, /* Value for "level" column of output */
  3532. int iFrom, /* Value for "from" column of output */
  3533. u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
  3534. )
  3535. {
  3536. if ( pParse.explain == 2 )
  3537. {
  3538. u32 flags = pLevel.plan.wsFlags;
  3539. SrcList_item pItem = pTabList.a[pLevel.iFrom];
  3540. Vdbe v = pParse.pVdbe; /* VM being constructed */
  3541. sqlite3 db = pParse.db; /* Database handle */
  3542. StringBuilder zMsg = new StringBuilder( 1000 ); /* Text to add to EQP output */
  3543. sqlite3_int64 nRow; /* Expected number of rows visited by scan */
  3544. int iId = pParse.iSelectId; /* Select id (left-most output column) */
  3545. bool isSearch; /* True for a SEARCH. False for SCAN. */
  3546. if ( ( flags & WHERE_MULTI_OR ) != 0 || ( wctrlFlags & WHERE_ONETABLE_ONLY ) != 0 )
  3547. return;
  3548. isSearch = ( pLevel.plan.nEq > 0 )
  3549. || ( flags & ( WHERE_BTM_LIMIT | WHERE_TOP_LIMIT ) ) != 0
  3550. || ( wctrlFlags & ( WHERE_ORDERBY_MIN | WHERE_ORDERBY_MAX ) ) != 0;
  3551. zMsg.Append( sqlite3MPrintf( db, "%s", isSearch ? "SEARCH" : "SCAN" ) );
  3552. if ( pItem.pSelect != null )
  3553. {
  3554. zMsg.Append( sqlite3MAppendf( db, null, " SUBQUERY %d", pItem.iSelectId ) );
  3555. }
  3556. else
  3557. {
  3558. zMsg.Append( sqlite3MAppendf( db, null, " TABLE %s", pItem.zName ) );
  3559. }
  3560. if ( pItem.zAlias != null )
  3561. {
  3562. zMsg.Append( sqlite3MAppendf( db, null, " AS %s", pItem.zAlias ) );
  3563. }
  3564. if ( ( flags & WHERE_INDEXED ) != 0 )
  3565. {
  3566. string zWhere = explainIndexRange( db, pLevel, pItem.pTab );
  3567. zMsg.Append( sqlite3MAppendf( db, null, " USING %s%sINDEX%s%s%s",
  3568. ( ( flags & WHERE_TEMP_INDEX ) != 0 ? "AUTOMATIC " : "" ),
  3569. ( ( flags & WHERE_IDX_ONLY ) != 0 ? "COVERING " : "" ),
  3570. ( ( flags & WHERE_TEMP_INDEX ) != 0 ? "" : " " ),
  3571. ( ( flags & WHERE_TEMP_INDEX ) != 0 ? "" : pLevel.plan.u.pIdx.zName ),
  3572. zWhere != null ? zWhere : ""
  3573. ) );
  3574. sqlite3DbFree( db, ref zWhere );
  3575. }
  3576. else if ( ( flags & ( WHERE_ROWID_EQ | WHERE_ROWID_RANGE ) ) != 0 )
  3577. {
  3578. zMsg.Append( " USING INTEGER PRIMARY KEY" );
  3579. if ( ( flags & WHERE_ROWID_EQ ) != 0 )
  3580. {
  3581. zMsg.Append( " (rowid=?)" );
  3582. }
  3583. else if ( ( flags & WHERE_BOTH_LIMIT ) == WHERE_BOTH_LIMIT )
  3584. {
  3585. zMsg.Append( " (rowid>? AND rowid<?)" );
  3586. }
  3587. else if ( ( flags & WHERE_BTM_LIMIT ) != 0 )
  3588. {
  3589. zMsg.Append( " (rowid>?)" );
  3590. }
  3591. else if ( ( flags & WHERE_TOP_LIMIT ) != 0 )
  3592. {
  3593. zMsg.Append( " (rowid<?)" );
  3594. }
  3595. }
  3596. #if !SQLITE_OMIT_VIRTUALTABLE
  3597. else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
  3598. sqlite3_index_info *pVtabIdx = pLevel.plan.u.pVtabIdx;
  3599. zMsg.Append( sqlite3MAppendf(db, null, " VIRTUAL TABLE INDEX %d:%s",
  3600. pVtabIdx.idxNum, pVtabIdx.idxStr);
  3601. }
  3602. #endif
  3603. if ( ( wctrlFlags & ( WHERE_ORDERBY_MIN | WHERE_ORDERBY_MAX ) ) != 0 )
  3604. {
  3605. testcase( wctrlFlags & WHERE_ORDERBY_MIN );
  3606. nRow = 1;
  3607. }
  3608. else
  3609. {
  3610. nRow = (sqlite3_int64)pLevel.plan.nRow;
  3611. }
  3612. zMsg.Append( sqlite3MAppendf( db, null, " (~%lld rows)", nRow ) );
  3613. sqlite3VdbeAddOp4( v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC );
  3614. }
  3615. }
  3616. #else
  3617. //# define explainOneScan(u,v,w,x,y,z)
  3618. static void explainOneScan( Parse u, SrcList v, WhereLevel w, int x, int y, u16 z){}
  3619. #endif //* SQLITE_OMIT_EXPLAIN */
  3620. /*
  3621. ** Generate code for the start of the iLevel-th loop in the WHERE clause
  3622. ** implementation described by pWInfo.
  3623. */
  3624. static Bitmask codeOneLoopStart(
  3625. WhereInfo pWInfo, /* Complete information about the WHERE clause */
  3626. int iLevel, /* Which level of pWInfo.a[] should be coded */
  3627. u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
  3628. Bitmask notReady /* Which tables are currently available */
  3629. )
  3630. {
  3631. int j, k; /* Loop counters */
  3632. int iCur; /* The VDBE cursor for the table */
  3633. int addrNxt; /* Where to jump to continue with the next IN case */
  3634. int omitTable; /* True if we use the index only */
  3635. int bRev; /* True if we need to scan in reverse order */
  3636. WhereLevel pLevel; /* The where level to be coded */
  3637. WhereClause pWC; /* Decomposition of the entire WHERE clause */
  3638. WhereTerm pTerm; /* A WHERE clause term */
  3639. Parse pParse; /* Parsing context */
  3640. Vdbe v; /* The prepared stmt under constructions */
  3641. SrcList_item pTabItem; /* FROM clause term being coded */
  3642. int addrBrk; /* Jump here to break out of the loop */
  3643. int addrCont; /* Jump here to continue with next cycle */
  3644. int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
  3645. int iReleaseReg = 0; /* Temp register to free before returning */
  3646. pParse = pWInfo.pParse;
  3647. v = pParse.pVdbe;
  3648. pWC = pWInfo.pWC;
  3649. pLevel = pWInfo.a[iLevel];
  3650. pTabItem = pWInfo.pTabList.a[pLevel.iFrom];
  3651. iCur = pTabItem.iCursor;
  3652. bRev = ( pLevel.plan.wsFlags & WHERE_REVERSE ) != 0 ? 1 : 0;
  3653. omitTable = ( ( pLevel.plan.wsFlags & WHERE_IDX_ONLY ) != 0
  3654. && ( wctrlFlags & WHERE_FORCE_TABLE ) == 0 ) ? 1 : 0;
  3655. /* Create labels for the "break" and "continue" instructions
  3656. ** for the current loop. Jump to addrBrk to break out of a loop.
  3657. ** Jump to cont to go immediately to the next iteration of the
  3658. ** loop.
  3659. **
  3660. ** When there is an IN operator, we also have a "addrNxt" label that
  3661. ** means to continue with the next IN value combination. When
  3662. ** there are no IN operators in the constraints, the "addrNxt" label
  3663. ** is the same as "addrBrk".
  3664. */
  3665. addrBrk = pLevel.addrBrk = pLevel.addrNxt = sqlite3VdbeMakeLabel( v );
  3666. addrCont = pLevel.addrCont = sqlite3VdbeMakeLabel( v );
  3667. /* If this is the right table of a LEFT OUTER JOIN, allocate and
  3668. ** initialize a memory cell that records if this table matches any
  3669. ** row of the left table of the join.
  3670. */
  3671. if ( pLevel.iFrom > 0 && ( pTabItem.jointype & JT_LEFT ) != 0 )// Check value of pTabItem[0].jointype
  3672. {
  3673. pLevel.iLeftJoin = ++pParse.nMem;
  3674. sqlite3VdbeAddOp2( v, OP_Integer, 0, pLevel.iLeftJoin );
  3675. #if SQLITE_DEBUG
  3676. VdbeComment( v, "init LEFT JOIN no-match flag" );
  3677. #endif
  3678. }
  3679. #if !SQLITE_OMIT_VIRTUALTABLE
  3680. if ( ( pLevel.plan.wsFlags & WHERE_VIRTUALTABLE ) != null )
  3681. {
  3682. /* Case 0: The table is a virtual-table. Use the VFilter and VNext
  3683. ** to access the data.
  3684. */
  3685. int iReg; /* P3 Value for OP_VFilter */
  3686. sqlite3_index_info pVtabIdx = pLevel.plan.u.pVtabIdx;
  3687. int nConstraint = pVtabIdx.nConstraint;
  3688. sqlite3_index_constraint_usage* aUsage =
  3689. pVtabIdx.aConstraintUsage;
  3690. const sqlite3_index_constraint* aConstraint =
  3691. pVtabIdx.aConstraint;
  3692. sqlite3ExprCachePush(pParse);
  3693. iReg = sqlite3GetTempRange( pParse, nConstraint + 2 );
  3694. for ( j = 1 ; j <= nConstraint ; j++ )
  3695. {
  3696. for ( k = 0 ; k < nConstraint ; k++ )
  3697. {
  3698. if ( aUsage[k].argvIndex == j )
  3699. {
  3700. int iTerm = aConstraint[k].iTermOffset;
  3701. sqlite3ExprCode( pParse, pWC.a[iTerm].pExpr.pRight, iReg + j + 1 );
  3702. break;
  3703. }
  3704. }
  3705. if ( k == nConstraint ) break;
  3706. }
  3707. sqlite3VdbeAddOp2( v, OP_Integer, pVtabIdx.idxNum, iReg );
  3708. sqlite3VdbeAddOp2( v, OP_Integer, j - 1, iReg + 1 );
  3709. sqlite3VdbeAddOp4( v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx.idxStr,
  3710. pVtabIdx.needToFreeIdxStr ? P4_MPRINTF : P4_STATIC );
  3711. pVtabIdx.needToFreeIdxStr = 0;
  3712. for ( j = 0 ; j < nConstraint ; j++ )
  3713. {
  3714. if ( aUsage[j].omit )
  3715. {
  3716. int iTerm = aConstraint[j].iTermOffset;
  3717. disableTerm( pLevel, &pWC.a[iTerm] );
  3718. }
  3719. }
  3720. pLevel.op = OP_VNext;
  3721. pLevel.p1 = iCur;
  3722. pLevel.p2 = sqlite3VdbeCurrentAddr( v );
  3723. sqlite3ReleaseTempRange( pParse, iReg, nConstraint + 2 );
  3724. sqlite3ExprCachePop(pParse, 1);
  3725. }
  3726. else
  3727. #endif //* SQLITE_OMIT_VIRTUALTABLE */
  3728. if ( ( pLevel.plan.wsFlags & WHERE_ROWID_EQ ) != 0 )
  3729. {
  3730. /* Case 1: We can directly reference a single row using an
  3731. ** equality comparison against the ROWID field. Or
  3732. ** we reference multiple rows using a "rowid IN (...)"
  3733. ** construct.
  3734. */
  3735. iReleaseReg = sqlite3GetTempReg( pParse );
  3736. pTerm = findTerm( pWC, iCur, -1, notReady, WO_EQ | WO_IN, null );
  3737. Debug.Assert( pTerm != null );
  3738. Debug.Assert( pTerm.pExpr != null );
  3739. Debug.Assert( pTerm.leftCursor == iCur );
  3740. Debug.Assert( omitTable == 0 );
  3741. testcase( pTerm.wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3742. iRowidReg = codeEqualityTerm( pParse, pTerm, pLevel, iReleaseReg );
  3743. addrNxt = pLevel.addrNxt;
  3744. sqlite3VdbeAddOp2( v, OP_MustBeInt, iRowidReg, addrNxt );
  3745. sqlite3VdbeAddOp3( v, OP_NotExists, iCur, addrNxt, iRowidReg );
  3746. sqlite3ExprCacheStore( pParse, iCur, -1, iRowidReg );
  3747. #if SQLITE_DEBUG
  3748. VdbeComment( v, "pk" );
  3749. #endif
  3750. pLevel.op = OP_Noop;
  3751. }
  3752. else if ( ( pLevel.plan.wsFlags & WHERE_ROWID_RANGE ) != 0 )
  3753. {
  3754. /* Case 2: We have an inequality comparison against the ROWID field.
  3755. */
  3756. int testOp = OP_Noop;
  3757. int start;
  3758. int memEndValue = 0;
  3759. WhereTerm pStart, pEnd;
  3760. Debug.Assert( omitTable == 0 );
  3761. pStart = findTerm( pWC, iCur, -1, notReady, WO_GT | WO_GE, null );
  3762. pEnd = findTerm( pWC, iCur, -1, notReady, WO_LT | WO_LE, null );
  3763. if ( bRev != 0 )
  3764. {
  3765. pTerm = pStart;
  3766. pStart = pEnd;
  3767. pEnd = pTerm;
  3768. }
  3769. if ( pStart != null )
  3770. {
  3771. Expr pX; /* The expression that defines the start bound */
  3772. int r1, rTemp = 0; /* Registers for holding the start boundary */
  3773. /* The following constant maps TK_xx codes into corresponding
  3774. ** seek opcodes. It depends on a particular ordering of TK_xx
  3775. */
  3776. u8[] aMoveOp = new u8[]{
  3777. /* TK_GT */ OP_SeekGt,
  3778. /* TK_LE */ OP_SeekLe,
  3779. /* TK_LT */ OP_SeekLt,
  3780. /* TK_GE */ OP_SeekGe
  3781. };
  3782. Debug.Assert( TK_LE == TK_GT + 1 ); /* Make sure the ordering.. */
  3783. Debug.Assert( TK_LT == TK_GT + 2 ); /* ... of the TK_xx values... */
  3784. Debug.Assert( TK_GE == TK_GT + 3 ); /* ... is correcct. */
  3785. testcase( pStart.wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3786. pX = pStart.pExpr;
  3787. Debug.Assert( pX != null );
  3788. Debug.Assert( pStart.leftCursor == iCur );
  3789. r1 = sqlite3ExprCodeTemp( pParse, pX.pRight, ref rTemp );
  3790. sqlite3VdbeAddOp3( v, aMoveOp[pX.op - TK_GT], iCur, addrBrk, r1 );
  3791. #if SQLITE_DEBUG
  3792. VdbeComment( v, "pk" );
  3793. #endif
  3794. sqlite3ExprCacheAffinityChange( pParse, r1, 1 );
  3795. sqlite3ReleaseTempReg( pParse, rTemp );
  3796. disableTerm( pLevel, pStart );
  3797. }
  3798. else
  3799. {
  3800. sqlite3VdbeAddOp2( v, bRev != 0 ? OP_Last : OP_Rewind, iCur, addrBrk );
  3801. }
  3802. if ( pEnd != null )
  3803. {
  3804. Expr pX;
  3805. pX = pEnd.pExpr;
  3806. Debug.Assert( pX != null );
  3807. Debug.Assert( pEnd.leftCursor == iCur );
  3808. testcase( pEnd.wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3809. memEndValue = ++pParse.nMem;
  3810. sqlite3ExprCode( pParse, pX.pRight, memEndValue );
  3811. if ( pX.op == TK_LT || pX.op == TK_GT )
  3812. {
  3813. testOp = bRev != 0 ? OP_Le : OP_Ge;
  3814. }
  3815. else
  3816. {
  3817. testOp = bRev != 0 ? OP_Lt : OP_Gt;
  3818. }
  3819. disableTerm( pLevel, pEnd );
  3820. }
  3821. start = sqlite3VdbeCurrentAddr( v );
  3822. pLevel.op = (u8)( bRev != 0 ? OP_Prev : OP_Next );
  3823. pLevel.p1 = iCur;
  3824. pLevel.p2 = start;
  3825. if ( pStart == null && pEnd == null )
  3826. {
  3827. pLevel.p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  3828. }
  3829. else
  3830. {
  3831. Debug.Assert( pLevel.p5 == 0 );
  3832. }
  3833. if ( testOp != OP_Noop )
  3834. {
  3835. iRowidReg = iReleaseReg = sqlite3GetTempReg( pParse );
  3836. sqlite3VdbeAddOp2( v, OP_Rowid, iCur, iRowidReg );
  3837. sqlite3ExprCacheStore( pParse, iCur, -1, iRowidReg );
  3838. sqlite3VdbeAddOp3( v, testOp, memEndValue, addrBrk, iRowidReg );
  3839. sqlite3VdbeChangeP5( v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL );
  3840. }
  3841. }
  3842. else if ( ( pLevel.plan.wsFlags & ( WHERE_COLUMN_RANGE | WHERE_COLUMN_EQ ) ) != 0 )
  3843. {
  3844. /* Case 3: A scan using an index.
  3845. **
  3846. ** The WHERE clause may contain zero or more equality
  3847. ** terms ("==" or "IN" operators) that refer to the N
  3848. ** left-most columns of the index. It may also contain
  3849. ** inequality constraints (>, <, >= or <=) on the indexed
  3850. ** column that immediately follows the N equalities. Only
  3851. ** the right-most column can be an inequality - the rest must
  3852. ** use the "==" and "IN" operators. For example, if the
  3853. ** index is on (x,y,z), then the following clauses are all
  3854. ** optimized:
  3855. **
  3856. ** x=5
  3857. ** x=5 AND y=10
  3858. ** x=5 AND y<10
  3859. ** x=5 AND y>5 AND y<10
  3860. ** x=5 AND y=5 AND z<=10
  3861. **
  3862. ** The z<10 term of the following cannot be used, only
  3863. ** the x=5 term:
  3864. **
  3865. ** x=5 AND z<10
  3866. **
  3867. ** N may be zero if there are inequality constraints.
  3868. ** If there are no inequality constraints, then N is at
  3869. ** least one.
  3870. **
  3871. ** This case is also used when there are no WHERE clause
  3872. ** constraints but an index is selected anyway, in order
  3873. ** to force the output order to conform to an ORDER BY.
  3874. */
  3875. u8[] aStartOp = new u8[] {
  3876. 0,
  3877. 0,
  3878. OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */
  3879. OP_Last, /* 3: (!start_constraints && startEq && bRev) */
  3880. OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */
  3881. OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */
  3882. OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */
  3883. OP_SeekLe /* 7: (start_constraints && startEq && bRev) */
  3884. };
  3885. u8[] aEndOp = new u8[] {
  3886. OP_Noop, /* 0: (!end_constraints) */
  3887. OP_IdxGE, /* 1: (end_constraints && !bRev) */
  3888. OP_IdxLT /* 2: (end_constraints && bRev) */
  3889. };
  3890. int nEq = (int)pLevel.plan.nEq; /* Number of == or IN terms */
  3891. int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */
  3892. int regBase; /* Base register holding constraint values */
  3893. int r1; /* Temp register */
  3894. WhereTerm pRangeStart = null; /* Inequality constraint at range start */
  3895. WhereTerm pRangeEnd = null; /* Inequality constraint at range end */
  3896. int startEq; /* True if range start uses ==, >= or <= */
  3897. int endEq; /* True if range end uses ==, >= or <= */
  3898. int start_constraints; /* Start of range is constrained */
  3899. int nConstraint; /* Number of constraint terms */
  3900. Index pIdx; /* The index we will be using */
  3901. int iIdxCur; /* The VDBE cursor for the index */
  3902. int nExtraReg = 0; /* Number of extra registers needed */
  3903. int op; /* Instruction opcode */
  3904. StringBuilder zStartAff = new StringBuilder( "" );
  3905. ;/* Affinity for start of range constraint */
  3906. StringBuilder zEndAff; /* Affinity for end of range constraint */
  3907. pIdx = pLevel.plan.u.pIdx;
  3908. iIdxCur = pLevel.iIdxCur;
  3909. k = pIdx.aiColumn[nEq]; /* Column for inequality constraints */
  3910. /* If this loop satisfies a sort order (pOrderBy) request that
  3911. ** was pDebug.Assed to this function to implement a "SELECT min(x) ..."
  3912. ** query, then the caller will only allow the loop to run for
  3913. ** a single iteration. This means that the first row returned
  3914. ** should not have a NULL value stored in 'x'. If column 'x' is
  3915. ** the first one after the nEq equality constraints in the index,
  3916. ** this requires some special handling.
  3917. */
  3918. if ( ( wctrlFlags & WHERE_ORDERBY_MIN ) != 0
  3919. && ( ( pLevel.plan.wsFlags & WHERE_ORDERBY ) != 0 )
  3920. && ( pIdx.nColumn > nEq )
  3921. )
  3922. {
  3923. /* Debug.Assert( pOrderBy.nExpr==1 ); */
  3924. /* Debug.Assert( pOrderBy.a[0].pExpr.iColumn==pIdx.aiColumn[nEq] ); */
  3925. isMinQuery = 1;
  3926. nExtraReg = 1;
  3927. }
  3928. /* Find any inequality constraint terms for the start and end
  3929. ** of the range.
  3930. */
  3931. if ( ( pLevel.plan.wsFlags & WHERE_TOP_LIMIT ) != 0 )
  3932. {
  3933. pRangeEnd = findTerm( pWC, iCur, k, notReady, ( WO_LT | WO_LE ), pIdx );
  3934. nExtraReg = 1;
  3935. }
  3936. if ( ( pLevel.plan.wsFlags & WHERE_BTM_LIMIT ) != 0 )
  3937. {
  3938. pRangeStart = findTerm( pWC, iCur, k, notReady, ( WO_GT | WO_GE ), pIdx );
  3939. nExtraReg = 1;
  3940. }
  3941. /* Generate code to evaluate all constraint terms using == or IN
  3942. ** and store the values of those terms in an array of registers
  3943. ** starting at regBase.
  3944. */
  3945. regBase = codeAllEqualityTerms(
  3946. pParse, pLevel, pWC, notReady, nExtraReg, ref zStartAff
  3947. );
  3948. zEndAff = new StringBuilder( zStartAff.ToString() );//sqlite3DbStrDup(pParse.db, zStartAff);
  3949. addrNxt = pLevel.addrNxt;
  3950. /* If we are doing a reverse order scan on an ascending index, or
  3951. ** a forward order scan on a descending index, interchange the
  3952. ** start and end terms (pRangeStart and pRangeEnd).
  3953. */
  3954. if ( nEq < pIdx.nColumn && bRev == ( pIdx.aSortOrder[nEq] == SQLITE_SO_ASC ? 1 : 0 ) )
  3955. {
  3956. SWAP( ref pRangeEnd, ref pRangeStart );
  3957. }
  3958. testcase( pRangeStart != null && ( pRangeStart.eOperator & WO_LE ) != 0 );
  3959. testcase( pRangeStart != null && ( pRangeStart.eOperator & WO_GE ) != 0 );
  3960. testcase( pRangeEnd != null && ( pRangeEnd.eOperator & WO_LE ) != 0 );
  3961. testcase( pRangeEnd != null && ( pRangeEnd.eOperator & WO_GE ) != 0 );
  3962. startEq = ( null == pRangeStart || ( pRangeStart.eOperator & ( WO_LE | WO_GE ) ) != 0 ) ? 1 : 0;
  3963. endEq = ( null == pRangeEnd || ( pRangeEnd.eOperator & ( WO_LE | WO_GE ) ) != 0 ) ? 1 : 0;
  3964. start_constraints = ( pRangeStart != null || nEq > 0 ) ? 1 : 0;
  3965. /* Seek the index cursor to the start of the range. */
  3966. nConstraint = nEq;
  3967. if ( pRangeStart != null )
  3968. {
  3969. Expr pRight = pRangeStart.pExpr.pRight;
  3970. sqlite3ExprCode( pParse, pRight, regBase + nEq );
  3971. sqlite3ExprCodeIsNullJump( v, pRight, regBase + nEq, addrNxt );
  3972. if ( zStartAff.Length != 0 )
  3973. {
  3974. if ( sqlite3CompareAffinity( pRight, zStartAff[nEq] ) == SQLITE_AFF_NONE )
  3975. {
  3976. /* Since the comparison is to be performed with no conversions
  3977. ** applied to the operands, set the affinity to apply to pRight to
  3978. ** SQLITE_AFF_NONE. */
  3979. zStartAff[nEq] = SQLITE_AFF_NONE;
  3980. }
  3981. if ( ( sqlite3ExprNeedsNoAffinityChange( pRight, zStartAff[nEq] ) ) != 0 )
  3982. {
  3983. zStartAff[nEq] = SQLITE_AFF_NONE;
  3984. }
  3985. }
  3986. nConstraint++;
  3987. testcase( pRangeStart.wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  3988. }
  3989. else if ( isMinQuery != 0 )
  3990. {
  3991. sqlite3VdbeAddOp2( v, OP_Null, 0, regBase + nEq );
  3992. nConstraint++;
  3993. startEq = 0;
  3994. start_constraints = 1;
  3995. }
  3996. codeApplyAffinity( pParse, regBase, nConstraint, zStartAff.ToString() );
  3997. op = aStartOp[( start_constraints << 2 ) + ( startEq << 1 ) + bRev];
  3998. Debug.Assert( op != 0 );
  3999. testcase( op == OP_Rewind );
  4000. testcase( op == OP_Last );
  4001. testcase( op == OP_SeekGt );
  4002. testcase( op == OP_SeekGe );
  4003. testcase( op == OP_SeekLe );
  4004. testcase( op == OP_SeekLt );
  4005. sqlite3VdbeAddOp4Int( v, op, iIdxCur, addrNxt, regBase, nConstraint );
  4006. /* Load the value for the inequality constraint at the end of the
  4007. ** range (if any).
  4008. */
  4009. nConstraint = nEq;
  4010. if ( pRangeEnd != null )
  4011. {
  4012. Expr pRight = pRangeEnd.pExpr.pRight;
  4013. sqlite3ExprCacheRemove( pParse, regBase + nEq, 1 );
  4014. sqlite3ExprCode( pParse, pRight, regBase + nEq );
  4015. sqlite3ExprCodeIsNullJump( v, pRight, regBase + nEq, addrNxt );
  4016. if ( zEndAff.Length > 0 )
  4017. {
  4018. if ( sqlite3CompareAffinity( pRight, zEndAff[nEq] ) == SQLITE_AFF_NONE )
  4019. {
  4020. /* Since the comparison is to be performed with no conversions
  4021. ** applied to the operands, set the affinity to apply to pRight to
  4022. ** SQLITE_AFF_NONE. */
  4023. zEndAff[nEq] = SQLITE_AFF_NONE;
  4024. }
  4025. if ( ( sqlite3ExprNeedsNoAffinityChange( pRight, zEndAff[nEq] ) ) != 0 )
  4026. {
  4027. zEndAff[nEq] = SQLITE_AFF_NONE;
  4028. }
  4029. }
  4030. codeApplyAffinity( pParse, regBase, nEq + 1, zEndAff.ToString() );
  4031. nConstraint++;
  4032. testcase( pRangeEnd.wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */
  4033. }
  4034. sqlite3DbFree( pParse.db, ref zStartAff );
  4035. sqlite3DbFree( pParse.db, ref zEndAff );
  4036. /* Top of the loop body */
  4037. pLevel.p2 = sqlite3VdbeCurrentAddr( v );
  4038. /* Check if the index cursor is past the end of the range. */
  4039. op = aEndOp[( ( pRangeEnd != null || nEq != 0 ) ? 1 : 0 ) * ( 1 + bRev )];
  4040. testcase( op == OP_Noop );
  4041. testcase( op == OP_IdxGE );
  4042. testcase( op == OP_IdxLT );
  4043. if ( op != OP_Noop )
  4044. {
  4045. sqlite3VdbeAddOp4Int( v, op, iIdxCur, addrNxt, regBase, nConstraint );
  4046. sqlite3VdbeChangeP5( v, (u8)( endEq != bRev ? 1 : 0 ) );
  4047. }
  4048. /* If there are inequality constraints, check that the value
  4049. ** of the table column that the inequality contrains is not NULL.
  4050. ** If it is, jump to the next iteration of the loop.
  4051. */
  4052. r1 = sqlite3GetTempReg( pParse );
  4053. testcase( pLevel.plan.wsFlags & WHERE_BTM_LIMIT );
  4054. testcase( pLevel.plan.wsFlags & WHERE_TOP_LIMIT );
  4055. if ( ( pLevel.plan.wsFlags & ( WHERE_BTM_LIMIT | WHERE_TOP_LIMIT ) ) != 0 )
  4056. {
  4057. sqlite3VdbeAddOp3( v, OP_Column, iIdxCur, nEq, r1 );
  4058. sqlite3VdbeAddOp2( v, OP_IsNull, r1, addrCont );
  4059. }
  4060. sqlite3ReleaseTempReg( pParse, r1 );
  4061. /* Seek the table cursor, if required */
  4062. disableTerm( pLevel, pRangeStart );
  4063. disableTerm( pLevel, pRangeEnd );
  4064. if ( 0 == omitTable )
  4065. {
  4066. iRowidReg = iReleaseReg = sqlite3GetTempReg( pParse );
  4067. sqlite3VdbeAddOp2( v, OP_IdxRowid, iIdxCur, iRowidReg );
  4068. sqlite3ExprCacheStore( pParse, iCur, -1, iRowidReg );
  4069. sqlite3VdbeAddOp2( v, OP_Seek, iCur, iRowidReg ); /* Deferred seek */
  4070. }
  4071. /* Record the instruction used to terminate the loop. Disable
  4072. ** WHERE clause terms made redundant by the index range scan.
  4073. */
  4074. pLevel.op = (u8)( bRev != 0 ? OP_Prev : OP_Next );
  4075. pLevel.p1 = iIdxCur;
  4076. }
  4077. else
  4078. #if !SQLITE_OMIT_OR_OPTIMIZATION
  4079. if ( ( pLevel.plan.wsFlags & WHERE_MULTI_OR ) != 0 )
  4080. {
  4081. /* Case 4: Two or more separately indexed terms connected by OR
  4082. **
  4083. ** Example:
  4084. **
  4085. ** CREATE TABLE t1(a,b,c,d);
  4086. ** CREATE INDEX i1 ON t1(a);
  4087. ** CREATE INDEX i2 ON t1(b);
  4088. ** CREATE INDEX i3 ON t1(c);
  4089. **
  4090. ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
  4091. **
  4092. ** In the example, there are three indexed terms connected by OR.
  4093. ** The top of the loop looks like this:
  4094. **
  4095. ** Null 1 # Zero the rowset in reg 1
  4096. **
  4097. ** Then, for each indexed term, the following. The arguments to
  4098. ** RowSetTest are such that the rowid of the current row is inserted
  4099. ** into the RowSet. If it is already present, control skips the
  4100. ** Gosub opcode and jumps straight to the code generated by WhereEnd().
  4101. **
  4102. ** sqlite3WhereBegin(<term>)
  4103. ** RowSetTest # Insert rowid into rowset
  4104. ** Gosub 2 A
  4105. ** sqlite3WhereEnd()
  4106. **
  4107. ** Following the above, code to terminate the loop. Label A, the target
  4108. ** of the Gosub above, jumps to the instruction right after the Goto.
  4109. **
  4110. ** Null 1 # Zero the rowset in reg 1
  4111. ** Goto B # The loop is finished.
  4112. **
  4113. ** A: <loop body> # Return data, whatever.
  4114. **
  4115. ** Return 2 # Jump back to the Gosub
  4116. **
  4117. ** B: <after the loop>
  4118. **
  4119. */
  4120. WhereClause pOrWc; /* The OR-clause broken out into subterms */
  4121. WhereTerm pFinal; /* Final subterm within the OR-clause. */
  4122. SrcList pOrTab; /* Shortened table list or OR-clause generation */
  4123. int regReturn = ++pParse.nMem; /* Register used with OP_Gosub */
  4124. int regRowset = 0; /* Register for RowSet object */
  4125. int regRowid = 0; /* Register holding rowid */
  4126. int iLoopBody = sqlite3VdbeMakeLabel( v );/* Start of loop body */
  4127. int iRetInit; /* Address of regReturn init */
  4128. int untestedTerms = 0; /* Some terms not completely tested */
  4129. int ii;
  4130. pTerm = pLevel.plan.u.pTerm;
  4131. Debug.Assert( pTerm != null );
  4132. Debug.Assert( pTerm.eOperator == WO_OR );
  4133. Debug.Assert( ( pTerm.wtFlags & TERM_ORINFO ) != 0 );
  4134. pOrWc = pTerm.u.pOrInfo.wc;
  4135. pFinal = pOrWc.a[pOrWc.nTerm - 1];
  4136. pLevel.op = OP_Return;
  4137. pLevel.p1 = regReturn;
  4138. /* Set up a new SrcList in pOrTab containing the table being scanned
  4139. ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
  4140. ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
  4141. */
  4142. if ( pWInfo.nLevel > 1 )
  4143. {
  4144. int nNotReady; /* The number of notReady tables */
  4145. SrcList_item[] origSrc; /* Original list of tables */
  4146. nNotReady = pWInfo.nLevel - iLevel - 1;
  4147. //sqlite3StackAllocRaw(pParse.db,
  4148. //sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab.a[0]));
  4149. pOrTab = new SrcList();
  4150. pOrTab.a = new SrcList_item[nNotReady + 1];
  4151. //if( pOrTab==0 ) return notReady;
  4152. pOrTab.nAlloc = (i16)( nNotReady + 1 );
  4153. pOrTab.nSrc = pOrTab.nAlloc;
  4154. pOrTab.a[0] = pTabItem;//memcpy(pOrTab.a, pTabItem, sizeof(*pTabItem));
  4155. origSrc = pWInfo.pTabList.a;
  4156. for ( k = 1; k <= nNotReady; k++ )
  4157. {
  4158. pOrTab.a[k] = origSrc[pWInfo.a[iLevel + k].iFrom];// memcpy(&pOrTab.a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab.a[k]));
  4159. }
  4160. }
  4161. else
  4162. {
  4163. pOrTab = pWInfo.pTabList;
  4164. }
  4165. /* Initialize the rowset register to contain NULL. An SQL NULL is
  4166. ** equivalent to an empty rowset.
  4167. **
  4168. ** Also initialize regReturn to contain the address of the instruction
  4169. ** immediately following the OP_Return at the bottom of the loop. This
  4170. ** is required in a few obscure LEFT JOIN cases where control jumps
  4171. ** over the top of the loop into the body of it. In this case the
  4172. ** correct response for the end-of-loop code (the OP_Return) is to
  4173. ** fall through to the next instruction, just as an OP_Next does if
  4174. ** called on an uninitialized cursor.
  4175. */
  4176. if ( ( wctrlFlags & WHERE_DUPLICATES_OK ) == 0 )
  4177. {
  4178. regRowset = ++pParse.nMem;
  4179. regRowid = ++pParse.nMem;
  4180. sqlite3VdbeAddOp2( v, OP_Null, 0, regRowset );
  4181. }
  4182. iRetInit = sqlite3VdbeAddOp2( v, OP_Integer, 0, regReturn );
  4183. for ( ii = 0; ii < pOrWc.nTerm; ii++ )
  4184. {
  4185. WhereTerm pOrTerm = pOrWc.a[ii];
  4186. if ( pOrTerm.leftCursor == iCur || pOrTerm.eOperator == WO_AND )
  4187. {
  4188. WhereInfo pSubWInfo; /* Info for single OR-term scan */
  4189. /* Loop through table entries that match term pOrTerm. */
  4190. ExprList elDummy = null;
  4191. pSubWInfo = sqlite3WhereBegin( pParse, pOrTab, pOrTerm.pExpr, ref elDummy,
  4192. WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
  4193. WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY );
  4194. if ( pSubWInfo != null )
  4195. {
  4196. explainOneScan(
  4197. pParse, pOrTab, pSubWInfo.a[0], iLevel, pLevel.iFrom, 0
  4198. );
  4199. if ( ( wctrlFlags & WHERE_DUPLICATES_OK ) == 0 )
  4200. {
  4201. int iSet = ( ( ii == pOrWc.nTerm - 1 ) ? -1 : ii );
  4202. int r;
  4203. r = sqlite3ExprCodeGetColumn( pParse, pTabItem.pTab, -1, iCur,
  4204. regRowid );
  4205. sqlite3VdbeAddOp4Int( v, OP_RowSetTest, regRowset,
  4206. sqlite3VdbeCurrentAddr( v ) + 2, r, iSet );
  4207. }
  4208. sqlite3VdbeAddOp2( v, OP_Gosub, regReturn, iLoopBody );
  4209. /* The pSubWInfo.untestedTerms flag means that this OR term
  4210. ** contained one or more AND term from a notReady table. The
  4211. ** terms from the notReady table could not be tested and will
  4212. ** need to be tested later.
  4213. */
  4214. if ( pSubWInfo.untestedTerms != 0 )
  4215. untestedTerms = 1;
  4216. /* Finish the loop through table entries that match term pOrTerm. */
  4217. sqlite3WhereEnd( pSubWInfo );
  4218. }
  4219. }
  4220. }
  4221. sqlite3VdbeChangeP1( v, iRetInit, sqlite3VdbeCurrentAddr( v ) );
  4222. sqlite3VdbeAddOp2( v, OP_Goto, 0, pLevel.addrBrk );
  4223. sqlite3VdbeResolveLabel( v, iLoopBody );
  4224. if ( pWInfo.nLevel > 1 )
  4225. sqlite3DbFree( pParse.db, ref pOrTab );//sqlite3DbFree(pParse.db, pOrTab)
  4226. if ( 0 == untestedTerms )
  4227. disableTerm( pLevel, pTerm );
  4228. }
  4229. else
  4230. #endif //* SQLITE_OMIT_OR_OPTIMIZATION */
  4231. {
  4232. /* Case 5: There is no usable index. We must do a complete
  4233. ** scan of the entire table.
  4234. */
  4235. u8[] aStep = new u8[] { OP_Next, OP_Prev };
  4236. u8[] aStart = new u8[] { OP_Rewind, OP_Last };
  4237. Debug.Assert( bRev == 0 || bRev == 1 );
  4238. Debug.Assert( omitTable == 0 );
  4239. pLevel.op = aStep[bRev];
  4240. pLevel.p1 = iCur;
  4241. pLevel.p2 = 1 + sqlite3VdbeAddOp2( v, aStart[bRev], iCur, addrBrk );
  4242. pLevel.p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
  4243. }
  4244. notReady &= ~getMask( pWC.pMaskSet, iCur );
  4245. /* Insert code to test every subexpression that can be completely
  4246. ** computed using the current set of tables.
  4247. **
  4248. ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
  4249. ** the use of indices become tests that are evaluated against each row of
  4250. ** the relevant input tables.
  4251. */
  4252. k = 0;
  4253. for ( j = pWC.nTerm; j > 0; j-- )//, pTerm++)
  4254. {
  4255. pTerm = pWC.a[pWC.nTerm - j];
  4256. Expr pE;
  4257. testcase( pTerm.wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
  4258. testcase( pTerm.wtFlags & TERM_CODED );
  4259. if ( ( pTerm.wtFlags & ( TERM_VIRTUAL | TERM_CODED ) ) != 0 )
  4260. continue;
  4261. if ( ( pTerm.prereqAll & notReady ) != 0 )
  4262. {
  4263. testcase( pWInfo.untestedTerms == 0
  4264. && ( pWInfo.wctrlFlags & WHERE_ONETABLE_ONLY ) != 0 );
  4265. pWInfo.untestedTerms = 1;
  4266. continue;
  4267. }
  4268. pE = pTerm.pExpr;
  4269. Debug.Assert( pE != null );
  4270. if ( pLevel.iLeftJoin != 0 && !( ( pE.flags & EP_FromJoin ) == EP_FromJoin ) )// !ExprHasProperty(pE, EP_FromJoin) ){
  4271. {
  4272. continue;
  4273. }
  4274. sqlite3ExprIfFalse( pParse, pE, addrCont, SQLITE_JUMPIFNULL );
  4275. k = 1;
  4276. pTerm.wtFlags |= TERM_CODED;
  4277. }
  4278. /* For a LEFT OUTER JOIN, generate code that will record the fact that
  4279. ** at least one row of the right table has matched the left table.
  4280. */
  4281. if ( pLevel.iLeftJoin != 0 )
  4282. {
  4283. pLevel.addrFirst = sqlite3VdbeCurrentAddr( v );
  4284. sqlite3VdbeAddOp2( v, OP_Integer, 1, pLevel.iLeftJoin );
  4285. #if SQLITE_DEBUG
  4286. VdbeComment( v, "record LEFT JOIN hit" );
  4287. #endif
  4288. sqlite3ExprCacheClear( pParse );
  4289. for ( j = 0; j < pWC.nTerm; j++ )//, pTerm++)
  4290. {
  4291. pTerm = pWC.a[j];
  4292. testcase( pTerm.wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
  4293. testcase( pTerm.wtFlags & TERM_CODED );
  4294. if ( ( pTerm.wtFlags & ( TERM_VIRTUAL | TERM_CODED ) ) != 0 )
  4295. continue;
  4296. if ( ( pTerm.prereqAll & notReady ) != 0 )
  4297. {
  4298. Debug.Assert( pWInfo.untestedTerms != 0 );
  4299. continue;
  4300. }
  4301. Debug.Assert( pTerm.pExpr != null );
  4302. sqlite3ExprIfFalse( pParse, pTerm.pExpr, addrCont, SQLITE_JUMPIFNULL );
  4303. pTerm.wtFlags |= TERM_CODED;
  4304. }
  4305. }
  4306. sqlite3ReleaseTempReg( pParse, iReleaseReg );
  4307. return notReady;
  4308. }
  4309. #if (SQLITE_TEST)
  4310. /*
  4311. ** The following variable holds a text description of query plan generated
  4312. ** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin
  4313. ** overwrites the previous. This information is used for testing and
  4314. ** analysis only.
  4315. */
  4316. //char sqlite3_query_plan[BMS*2*40]; /* Text of the join */
  4317. static int nQPlan = 0; /* Next free slow in _query_plan[] */
  4318. #endif //* SQLITE_TEST */
  4319. /*
  4320. ** Free a WhereInfo structure
  4321. */
  4322. static void whereInfoFree( sqlite3 db, WhereInfo pWInfo )
  4323. {
  4324. if ( ALWAYS( pWInfo != null ) )
  4325. {
  4326. int i;
  4327. for ( i = 0; i < pWInfo.nLevel; i++ )
  4328. {
  4329. sqlite3_index_info pInfo = pWInfo.a[i] != null ? pWInfo.a[i].pIdxInfo : null;
  4330. if ( pInfo != null )
  4331. {
  4332. /* Debug.Assert( pInfo.needToFreeIdxStr==0 || db.mallocFailed ); */
  4333. if ( pInfo.needToFreeIdxStr != 0 )
  4334. {
  4335. //sqlite3_free( ref pInfo.idxStr );
  4336. }
  4337. sqlite3DbFree( db, ref pInfo );
  4338. }
  4339. if ( pWInfo.a[i] != null && ( pWInfo.a[i].plan.wsFlags & WHERE_TEMP_INDEX ) != 0 )
  4340. {
  4341. Index pIdx = pWInfo.a[i].plan.u.pIdx;
  4342. if ( pIdx != null )
  4343. {
  4344. sqlite3DbFree( db, ref pIdx.zColAff );
  4345. sqlite3DbFree( db, ref pIdx );
  4346. }
  4347. }
  4348. }
  4349. whereClauseClear( pWInfo.pWC );
  4350. sqlite3DbFree( db, ref pWInfo );
  4351. }
  4352. }
  4353. /*
  4354. ** Generate the beginning of the loop used for WHERE clause processing.
  4355. ** The return value is a pointer to an opaque structure that contains
  4356. ** information needed to terminate the loop. Later, the calling routine
  4357. ** should invoke sqlite3WhereEnd() with the return value of this function
  4358. ** in order to complete the WHERE clause processing.
  4359. **
  4360. ** If an error occurs, this routine returns NULL.
  4361. **
  4362. ** The basic idea is to do a nested loop, one loop for each table in
  4363. ** the FROM clause of a select. (INSERT and UPDATE statements are the
  4364. ** same as a SELECT with only a single table in the FROM clause.) For
  4365. ** example, if the SQL is this:
  4366. **
  4367. ** SELECT * FROM t1, t2, t3 WHERE ...;
  4368. **
  4369. ** Then the code generated is conceptually like the following:
  4370. **
  4371. ** foreach row1 in t1 do \ Code generated
  4372. ** foreach row2 in t2 do |-- by sqlite3WhereBegin()
  4373. ** foreach row3 in t3 do /
  4374. ** ...
  4375. ** end \ Code generated
  4376. ** end |-- by sqlite3WhereEnd()
  4377. ** end /
  4378. **
  4379. ** Note that the loops might not be nested in the order in which they
  4380. ** appear in the FROM clause if a different order is better able to make
  4381. ** use of indices. Note also that when the IN operator appears in
  4382. ** the WHERE clause, it might result in additional nested loops for
  4383. ** scanning through all values on the right-hand side of the IN.
  4384. **
  4385. ** There are Btree cursors Debug.Associated with each table. t1 uses cursor
  4386. ** number pTabList.a[0].iCursor. t2 uses the cursor pTabList.a[1].iCursor.
  4387. ** And so forth. This routine generates code to open those VDBE cursors
  4388. ** and sqlite3WhereEnd() generates the code to close them.
  4389. **
  4390. ** The code that sqlite3WhereBegin() generates leaves the cursors named
  4391. ** in pTabList pointing at their appropriate entries. The [...] code
  4392. ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
  4393. ** data from the various tables of the loop.
  4394. **
  4395. ** If the WHERE clause is empty, the foreach loops must each scan their
  4396. ** entire tables. Thus a three-way join is an O(N^3) operation. But if
  4397. ** the tables have indices and there are terms in the WHERE clause that
  4398. ** refer to those indices, a complete table scan can be avoided and the
  4399. ** code will run much faster. Most of the work of this routine is checking
  4400. ** to see if there are indices that can be used to speed up the loop.
  4401. **
  4402. ** Terms of the WHERE clause are also used to limit which rows actually
  4403. ** make it to the "..." in the middle of the loop. After each "foreach",
  4404. ** terms of the WHERE clause that use only terms in that loop and outer
  4405. ** loops are evaluated and if false a jump is made around all subsequent
  4406. ** inner loops (or around the "..." if the test occurs within the inner-
  4407. ** most loop)
  4408. **
  4409. ** OUTER JOINS
  4410. **
  4411. ** An outer join of tables t1 and t2 is conceptally coded as follows:
  4412. **
  4413. ** foreach row1 in t1 do
  4414. ** flag = 0
  4415. ** foreach row2 in t2 do
  4416. ** start:
  4417. ** ...
  4418. ** flag = 1
  4419. ** end
  4420. ** if flag==null then
  4421. ** move the row2 cursor to a null row
  4422. ** goto start
  4423. ** fi
  4424. ** end
  4425. **
  4426. ** ORDER BY CLAUSE PROCESSING
  4427. **
  4428. ** ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement,
  4429. ** if there is one. If there is no ORDER BY clause or if this routine
  4430. ** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL.
  4431. **
  4432. ** If an index can be used so that the natural output order of the table
  4433. ** scan is correct for the ORDER BY clause, then that index is used and
  4434. ** ppOrderBy is set to NULL. This is an optimization that prevents an
  4435. ** unnecessary sort of the result set if an index appropriate for the
  4436. ** ORDER BY clause already exists.
  4437. **
  4438. ** If the where clause loops cannot be arranged to provide the correct
  4439. ** output order, then the ppOrderBy is unchanged.
  4440. */
  4441. static WhereInfo sqlite3WhereBegin(
  4442. Parse pParse, /* The parser context */
  4443. SrcList pTabList, /* A list of all tables to be scanned */
  4444. Expr pWhere, /* The WHERE clause */
  4445. ref ExprList ppOrderBy, /* An ORDER BY clause, or NULL */
  4446. u16 wctrlFlags /* One of the WHERE_* flags defined in sqliteInt.h */
  4447. )
  4448. {
  4449. int i; /* Loop counter */
  4450. int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */
  4451. int nTabList; /* Number of elements in pTabList */
  4452. WhereInfo pWInfo; /* Will become the return value of this function */
  4453. Vdbe v = pParse.pVdbe; /* The virtual data_base engine */
  4454. Bitmask notReady; /* Cursors that are not yet positioned */
  4455. WhereMaskSet pMaskSet; /* The expression mask set */
  4456. WhereClause pWC = new WhereClause(); /* Decomposition of the WHERE clause */
  4457. SrcList_item pTabItem; /* A single entry from pTabList */
  4458. WhereLevel pLevel; /* A single level in the pWInfo list */
  4459. int iFrom; /* First unused FROM clause element */
  4460. int andFlags; /* AND-ed combination of all pWC.a[].wtFlags */
  4461. sqlite3 db; /* Data_base connection */
  4462. /* The number of tables in the FROM clause is limited by the number of
  4463. ** bits in a Bitmask
  4464. */
  4465. testcase( pTabList.nSrc == BMS );
  4466. if ( pTabList.nSrc > BMS )
  4467. {
  4468. sqlite3ErrorMsg( pParse, "at most %d tables in a join", BMS );
  4469. return null;
  4470. }
  4471. /* This function normally generates a nested loop for all tables in
  4472. ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should
  4473. ** only generate code for the first table in pTabList and assume that
  4474. ** any cursors associated with subsequent tables are uninitialized.
  4475. */
  4476. nTabList = ( ( wctrlFlags & WHERE_ONETABLE_ONLY ) != 0 ) ? 1 : (int)pTabList.nSrc;
  4477. /* Allocate and initialize the WhereInfo structure that will become the
  4478. ** return value. A single allocation is used to store the WhereInfo
  4479. ** struct, the contents of WhereInfo.a[], the WhereClause structure
  4480. ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
  4481. ** field (type Bitmask) it must be aligned on an 8-byte boundary on
  4482. ** some architectures. Hence the ROUND8() below.
  4483. */
  4484. db = pParse.db;
  4485. pWInfo = new WhereInfo();
  4486. //nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
  4487. //pWInfo = sqlite3DbMallocZero( db,
  4488. // nByteWInfo +
  4489. // sizeof( WhereClause ) +
  4490. // sizeof( WhereMaskSet )
  4491. //);
  4492. pWInfo.a = new WhereLevel[pTabList.nSrc];
  4493. //if ( db.mallocFailed != 0 )
  4494. //{
  4495. //sqlite3DbFree(db, pWInfo);
  4496. //pWInfo = 0;
  4497. // goto whereBeginError;
  4498. //}
  4499. pWInfo.nLevel = nTabList;
  4500. pWInfo.pParse = pParse;
  4501. pWInfo.pTabList = pTabList;
  4502. pWInfo.iBreak = sqlite3VdbeMakeLabel( v );
  4503. pWInfo.pWC = pWC = new WhereClause();// (WhereClause )((u8 )pWInfo)[nByteWInfo];
  4504. pWInfo.wctrlFlags = wctrlFlags;
  4505. pWInfo.savedNQueryLoop = pParse.nQueryLoop;
  4506. //pMaskSet = (WhereMaskSet)pWC[1];
  4507. /* Split the WHERE clause into separate subexpressions where each
  4508. ** subexpression is separated by an AND operator.
  4509. */
  4510. pMaskSet = new WhereMaskSet();//initMaskSet(pMaskSet);
  4511. whereClauseInit( pWC, pParse, pMaskSet );
  4512. sqlite3ExprCodeConstants( pParse, pWhere );
  4513. whereSplit( pWC, pWhere, TK_AND ); /* IMP: R-15842-53296 */
  4514. /* Special case: a WHERE clause that is constant. Evaluate the
  4515. ** expression and either jump over all of the code or fall thru.
  4516. */
  4517. if ( pWhere != null && ( nTabList == 0 || sqlite3ExprIsConstantNotJoin( pWhere ) != 0 ) )
  4518. {
  4519. sqlite3ExprIfFalse( pParse, pWhere, pWInfo.iBreak, SQLITE_JUMPIFNULL );
  4520. pWhere = null;
  4521. }
  4522. /* Assign a bit from the bitmask to every term in the FROM clause.
  4523. **
  4524. ** When assigning bitmask values to FROM clause cursors, it must be
  4525. ** the case that if X is the bitmask for the N-th FROM clause term then
  4526. ** the bitmask for all FROM clause terms to the left of the N-th term
  4527. ** is (X-1). An expression from the ON clause of a LEFT JOIN can use
  4528. ** its Expr.iRightJoinTable value to find the bitmask of the right table
  4529. ** of the join. Subtracting one from the right table bitmask gives a
  4530. ** bitmask for all tables to the left of the join. Knowing the bitmask
  4531. ** for all tables to the left of a left join is important. Ticket #3015.
  4532. **
  4533. ** Configure the WhereClause.vmask variable so that bits that correspond
  4534. ** to virtual table cursors are set. This is used to selectively disable
  4535. ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful
  4536. ** with virtual tables.
  4537. **
  4538. ** Note that bitmasks are created for all pTabList.nSrc tables in
  4539. ** pTabList, not just the first nTabList tables. nTabList is normally
  4540. ** equal to pTabList.nSrc but might be shortened to 1 if the
  4541. ** WHERE_ONETABLE_ONLY flag is set.
  4542. */
  4543. Debug.Assert( pWC.vmask == 0 && pMaskSet.n == 0 );
  4544. for ( i = 0; i < pTabList.nSrc; i++ )
  4545. {
  4546. createMask( pMaskSet, pTabList.a[i].iCursor );
  4547. #if !SQLITE_OMIT_VIRTUALTABLE
  4548. if ( ALWAYS( pTabList.a[i].pTab ) && IsVirtual( pTabList.a[i].pTab ) )
  4549. {
  4550. pWC.vmask |= ( (Bitmask)1 << i );
  4551. }
  4552. #endif
  4553. }
  4554. #if !NDEBUG
  4555. {
  4556. Bitmask toTheLeft = 0;
  4557. for ( i = 0; i < pTabList.nSrc; i++ )
  4558. {
  4559. Bitmask m = getMask( pMaskSet, pTabList.a[i].iCursor );
  4560. Debug.Assert( ( m - 1 ) == toTheLeft );
  4561. toTheLeft |= m;
  4562. }
  4563. }
  4564. #endif
  4565. /* Analyze all of the subexpressions. Note that exprAnalyze() might
  4566. ** add new virtual terms onto the end of the WHERE clause. We do not
  4567. ** want to analyze these virtual terms, so start analyzing at the end
  4568. ** and work forward so that the added virtual terms are never processed.
  4569. */
  4570. exprAnalyzeAll( pTabList, pWC );
  4571. //if ( db.mallocFailed != 0 )
  4572. //{
  4573. // goto whereBeginError;
  4574. //}
  4575. /* Chose the best index to use for each table in the FROM clause.
  4576. **
  4577. ** This loop fills in the following fields:
  4578. **
  4579. ** pWInfo.a[].pIdx The index to use for this level of the loop.
  4580. ** pWInfo.a[].wsFlags WHERE_xxx flags Debug.Associated with pIdx
  4581. ** pWInfo.a[].nEq The number of == and IN constraints
  4582. ** pWInfo.a[].iFrom Which term of the FROM clause is being coded
  4583. ** pWInfo.a[].iTabCur The VDBE cursor for the data_base table
  4584. ** pWInfo.a[].iIdxCur The VDBE cursor for the index
  4585. ** pWInfo.a[].pTerm When wsFlags==WO_OR, the OR-clause term
  4586. **
  4587. ** This loop also figures out the nesting order of tables in the FROM
  4588. ** clause.
  4589. */
  4590. notReady = ~(Bitmask)0;
  4591. pTabItem = pTabList.a != null ? pTabList.a[0] : null; //pTabItem = pTabList.a;
  4592. //pLevel = pWInfo.a;
  4593. andFlags = ~0;
  4594. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  4595. WHERETRACE( "*** Optimizer Start ***\n" );
  4596. #endif
  4597. for ( i = iFrom = 0; i < nTabList; i++ )//, pLevel++ )
  4598. {
  4599. pWInfo.a[i] = new WhereLevel();
  4600. pLevel = pWInfo.a[i];
  4601. WhereCost bestPlan; /* Most efficient plan seen so far */
  4602. Index pIdx; /* Index for FROM table at pTabItem */
  4603. int j; /* For looping over FROM tables */
  4604. int bestJ = -1; /* The value of j */
  4605. Bitmask m; /* Bitmask value for j or bestJ */
  4606. int isOptimal; /* Iterator for optimal/non-optimal search */
  4607. int nUnconstrained; /* Number tables without INDEXED BY */
  4608. Bitmask notIndexed; /* Mask of tables that cannot use an index */
  4609. bestPlan = new WhereCost();// memset( &bestPlan, 0, sizeof( bestPlan ) );
  4610. bestPlan.rCost = SQLITE_BIG_DBL;
  4611. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  4612. WHERETRACE( "*** Begin search for loop %d ***\n", i );
  4613. #endif
  4614. /* Loop through the remaining entries in the FROM clause to find the
  4615. ** next nested loop. The loop tests all FROM clause entries
  4616. ** either once or twice.
  4617. **
  4618. ** The first test is always performed if there are two or more entries
  4619. ** remaining and never performed if there is only one FROM clause entry
  4620. ** to choose from. The first test looks for an "optimal" scan. In
  4621. ** this context an optimal scan is one that uses the same strategy
  4622. ** for the given FROM clause entry as would be selected if the entry
  4623. ** were used as the innermost nested loop. In other words, a table
  4624. ** is chosen such that the cost of running that table cannot be reduced
  4625. ** by waiting for other tables to run first. This "optimal" test works
  4626. ** by first assuming that the FROM clause is on the inner loop and finding
  4627. ** its query plan, then checking to see if that query plan uses any
  4628. ** other FROM clause terms that are notReady. If no notReady terms are
  4629. ** used then the "optimal" query plan works.
  4630. **
  4631. ** Note that the WhereCost.nRow parameter for an optimal scan might
  4632. ** not be as small as it would be if the table really were the innermost
  4633. ** join. The nRow value can be reduced by WHERE clause constraints
  4634. ** that do not use indices. But this nRow reduction only happens if the
  4635. ** table really is the innermost join.
  4636. **
  4637. ** The second loop iteration is only performed if no optimal scan
  4638. ** strategies were found by the first iteration. This second iteration
  4639. ** is used to search for the lowest cost scan overall.
  4640. **
  4641. ** Previous versions of SQLite performed only the second iteration -
  4642. ** the next outermost loop was always that with the lowest overall
  4643. ** cost. However, this meant that SQLite could select the wrong plan
  4644. ** for scripts such as the following:
  4645. **
  4646. ** CREATE TABLE t1(a, b);
  4647. ** CREATE TABLE t2(c, d);
  4648. ** SELECT * FROM t2, t1 WHERE t2.rowid = t1.a;
  4649. **
  4650. ** The best strategy is to iterate through table t1 first. However it
  4651. ** is not possible to determine this with a simple greedy algorithm.
  4652. ** Since the cost of a linear scan through table t2 is the same
  4653. ** as the cost of a linear scan through table t1, a simple greedy
  4654. ** algorithm may choose to use t2 for the outer loop, which is a much
  4655. ** costlier approach.
  4656. */
  4657. nUnconstrained = 0;
  4658. notIndexed = 0;
  4659. for ( isOptimal = ( iFrom < nTabList - 1 ) ? 1 : 0; isOptimal >= 0 && bestJ < 0; isOptimal-- )
  4660. {
  4661. Bitmask mask; /* Mask of tables not yet ready */
  4662. for ( j = iFrom; j < nTabList; j++ )//, pTabItem++)
  4663. {
  4664. pTabItem = pTabList.a[j];
  4665. int doNotReorder; /* True if this table should not be reordered */
  4666. WhereCost sCost = new WhereCost(); /* Cost information from best[Virtual]Index() */
  4667. ExprList pOrderBy; /* ORDER BY clause for index to optimize */
  4668. doNotReorder = ( pTabItem.jointype & ( JT_LEFT | JT_CROSS ) ) != 0 ? 1 : 0;
  4669. if ( ( j != iFrom && doNotReorder != 0 ) )
  4670. break;
  4671. m = getMask( pMaskSet, pTabItem.iCursor );
  4672. if ( ( m & notReady ) == 0 )
  4673. {
  4674. if ( j == iFrom )
  4675. iFrom++;
  4676. continue;
  4677. }
  4678. mask = ( isOptimal != 0 ? m : notReady );
  4679. pOrderBy = ( ( i == 0 && ppOrderBy != null ) ? ppOrderBy : null );
  4680. if ( pTabItem.pIndex == null )
  4681. nUnconstrained++;
  4682. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  4683. WHERETRACE( "=== trying table %d with isOptimal=%d ===\n",
  4684. j, isOptimal );
  4685. #endif
  4686. Debug.Assert( pTabItem.pTab != null );
  4687. #if !SQLITE_OMIT_VIRTUALTABLE
  4688. if( IsVirtual(pTabItem.pTab) ){
  4689. sqlite3_index_info **pp = &pWInfo.a[j].pIdxInfo;
  4690. bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy,
  4691. &sCost, pp);
  4692. }else
  4693. #endif
  4694. {
  4695. bestBtreeIndex( pParse, pWC, pTabItem, mask, notReady, pOrderBy,
  4696. ref sCost );
  4697. }
  4698. Debug.Assert( isOptimal != 0 || ( sCost.used & notReady ) == 0 );
  4699. /* If an INDEXED BY clause is present, then the plan must use that
  4700. ** index if it uses any index at all */
  4701. Debug.Assert( pTabItem.pIndex == null
  4702. || ( sCost.plan.wsFlags & WHERE_NOT_FULLSCAN ) == 0
  4703. || sCost.plan.u.pIdx == pTabItem.pIndex );
  4704. if ( isOptimal != 0 && ( sCost.plan.wsFlags & WHERE_NOT_FULLSCAN ) == 0 )
  4705. {
  4706. notIndexed |= m;
  4707. }
  4708. /* Conditions under which this table becomes the best so far:
  4709. **
  4710. ** (1) The table must not depend on other tables that have not
  4711. ** yet run.
  4712. **
  4713. ** (2) A full-table-scan plan cannot supercede another plan unless
  4714. ** it is an "optimal" plan as defined above.
  4715. **
  4716. ** (3) All tables have an INDEXED BY clause or this table lacks an
  4717. ** INDEXED BY clause or this table uses the specific
  4718. ** index specified by its INDEXED BY clause. This rule ensures
  4719. ** that a best-so-far is always selected even if an impossible
  4720. ** combination of INDEXED BY clauses are given. The error
  4721. ** will be detected and relayed back to the application later.
  4722. ** The NEVER() comes about because rule (2) above prevents
  4723. ** An indexable full-table-scan from reaching rule (3).
  4724. **
  4725. ** (4) The plan cost must be lower than prior plans or else the
  4726. ** cost must be the same and the number of rows must be lower.
  4727. */
  4728. if ( ( sCost.used & notReady ) == 0 /* (1) */
  4729. && ( bestJ < 0 || ( notIndexed & m ) != 0 /* (2) */
  4730. || ( sCost.plan.wsFlags & WHERE_NOT_FULLSCAN ) != 0 )
  4731. && ( nUnconstrained == 0 || pTabItem.pIndex == null /* (3) */
  4732. || NEVER( ( sCost.plan.wsFlags & WHERE_NOT_FULLSCAN ) != 0 ) )
  4733. && ( bestJ < 0 || sCost.rCost < bestPlan.rCost /* (4) */
  4734. || ( sCost.rCost <= bestPlan.rCost
  4735. && sCost.plan.nRow < bestPlan.plan.nRow ) )
  4736. )
  4737. {
  4738. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  4739. WHERETRACE( "=== table %d is best so far" +
  4740. " with cost=%g and nRow=%g\n",
  4741. j, sCost.rCost, sCost.plan.nRow );
  4742. #endif
  4743. bestPlan = sCost;
  4744. bestJ = j;
  4745. }
  4746. if ( doNotReorder != 0 )
  4747. break;
  4748. }
  4749. }
  4750. Debug.Assert( bestJ >= 0 );
  4751. Debug.Assert( ( notReady & getMask( pMaskSet, pTabList.a[bestJ].iCursor ) ) != 0 );
  4752. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  4753. WHERETRACE( "*** Optimizer selects table %d for loop %d" +
  4754. " with cost=%g and nRow=%g\n",
  4755. bestJ, i,//pLevel-pWInfo.a,
  4756. bestPlan.rCost, bestPlan.plan.nRow );
  4757. #endif
  4758. if ( ( bestPlan.plan.wsFlags & WHERE_ORDERBY ) != 0 )
  4759. {
  4760. ppOrderBy = null;
  4761. }
  4762. andFlags = (int)( andFlags & bestPlan.plan.wsFlags );
  4763. pLevel.plan = bestPlan.plan;
  4764. testcase( bestPlan.plan.wsFlags & WHERE_INDEXED );
  4765. testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX );
  4766. if ( ( bestPlan.plan.wsFlags & ( WHERE_INDEXED | WHERE_TEMP_INDEX ) ) != 0 )
  4767. {
  4768. pLevel.iIdxCur = pParse.nTab++;
  4769. }
  4770. else
  4771. {
  4772. pLevel.iIdxCur = -1;
  4773. }
  4774. notReady &= ~getMask( pMaskSet, pTabList.a[bestJ].iCursor );
  4775. pLevel.iFrom = (u8)bestJ;
  4776. if ( bestPlan.plan.nRow >= (double)1 )
  4777. {
  4778. pParse.nQueryLoop *= bestPlan.plan.nRow;
  4779. }
  4780. /* Check that if the table scanned by this loop iteration had an
  4781. ** INDEXED BY clause attached to it, that the named index is being
  4782. ** used for the scan. If not, then query compilation has failed.
  4783. ** Return an error.
  4784. */
  4785. pIdx = pTabList.a[bestJ].pIndex;
  4786. if ( pIdx != null )
  4787. {
  4788. if ( ( bestPlan.plan.wsFlags & WHERE_INDEXED ) == 0 )
  4789. {
  4790. sqlite3ErrorMsg( pParse, "cannot use index: %s", pIdx.zName );
  4791. goto whereBeginError;
  4792. }
  4793. else
  4794. {
  4795. /* If an INDEXED BY clause is used, the bestIndex() function is
  4796. ** guaranteed to find the index specified in the INDEXED BY clause
  4797. ** if it find an index at all. */
  4798. Debug.Assert( bestPlan.plan.u.pIdx == pIdx );
  4799. }
  4800. }
  4801. }
  4802. #if (SQLITE_TEST) && (SQLITE_DEBUG)
  4803. WHERETRACE( "*** Optimizer Finished ***\n" );
  4804. #endif
  4805. if ( pParse.nErr != 0 /*|| db.mallocFailed != 0 */ )
  4806. {
  4807. goto whereBeginError;
  4808. }
  4809. /* If the total query only selects a single row, then the ORDER BY
  4810. ** clause is irrelevant.
  4811. */
  4812. if ( ( andFlags & WHERE_UNIQUE ) != 0 && ppOrderBy != null )
  4813. {
  4814. ppOrderBy = null;
  4815. }
  4816. /* If the caller is an UPDATE or DELETE statement that is requesting
  4817. ** to use a one-pDebug.Ass algorithm, determine if this is appropriate.
  4818. ** The one-pass algorithm only works if the WHERE clause constraints
  4819. ** the statement to update a single row.
  4820. */
  4821. Debug.Assert( ( wctrlFlags & WHERE_ONEPASS_DESIRED ) == 0 || pWInfo.nLevel == 1 );
  4822. if ( ( wctrlFlags & WHERE_ONEPASS_DESIRED ) != 0 && ( andFlags & WHERE_UNIQUE ) != 0 )
  4823. {
  4824. pWInfo.okOnePass = 1;
  4825. pWInfo.a[0].plan.wsFlags = (u32)( pWInfo.a[0].plan.wsFlags & ~WHERE_IDX_ONLY );
  4826. }
  4827. /* Open all tables in the pTabList and any indices selected for
  4828. ** searching those tables.
  4829. */
  4830. sqlite3CodeVerifySchema( pParse, -1 ); /* Insert the cookie verifier Goto */
  4831. notReady = ~(Bitmask)0;
  4832. pWInfo.nRowOut = (double)1;
  4833. for ( i = 0; i < nTabList; i++ )//, pLevel++ )
  4834. {
  4835. pLevel = pWInfo.a[i];
  4836. Table pTab; /* Table to open */
  4837. int iDb; /* Index of data_base containing table/index */
  4838. pTabItem = pTabList.a[pLevel.iFrom];
  4839. pTab = pTabItem.pTab;
  4840. pLevel.iTabCur = pTabItem.iCursor;
  4841. pWInfo.nRowOut *= pLevel.plan.nRow;
  4842. iDb = sqlite3SchemaToIndex( db, pTab.pSchema );
  4843. if ( ( pTab.tabFlags & TF_Ephemeral ) != 0 || pTab.pSelect != null )
  4844. {
  4845. /* Do nothing */
  4846. }
  4847. else
  4848. #if !SQLITE_OMIT_VIRTUALTABLE
  4849. if( (pLevel.plan.wsFlags & WHERE_VIRTUALTABLE)!=null ){
  4850. VTable pVTab = sqlite3GetVTable(db, pTab);
  4851. int iCur = pTabItem.iCursor;
  4852. sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0,
  4853. pVTab, P4_VTAB);
  4854. }else
  4855. #endif
  4856. if ( ( pLevel.plan.wsFlags & WHERE_IDX_ONLY ) == 0
  4857. && ( wctrlFlags & WHERE_OMIT_OPEN ) == 0 )
  4858. {
  4859. int op = pWInfo.okOnePass != 0 ? OP_OpenWrite : OP_OpenRead;
  4860. sqlite3OpenTable( pParse, pTabItem.iCursor, iDb, pTab, op );
  4861. testcase( pTab.nCol == BMS - 1 );
  4862. testcase( pTab.nCol == BMS );
  4863. if ( 0 == pWInfo.okOnePass && pTab.nCol < BMS )
  4864. {
  4865. Bitmask b = pTabItem.colUsed;
  4866. int n = 0;
  4867. for ( ; b != 0; b = b >> 1, n++ )
  4868. {
  4869. }
  4870. sqlite3VdbeChangeP4( v, sqlite3VdbeCurrentAddr( v ) - 1,
  4871. n, P4_INT32 );//SQLITE_INT_TO_PTR(n)
  4872. Debug.Assert( n <= pTab.nCol );
  4873. }
  4874. }
  4875. else
  4876. {
  4877. sqlite3TableLock( pParse, iDb, pTab.tnum, 0, pTab.zName );
  4878. }
  4879. #if !SQLITE_OMIT_AUTOMATIC_INDEX
  4880. if ( ( pLevel.plan.wsFlags & WHERE_TEMP_INDEX ) != 0 )
  4881. {
  4882. constructAutomaticIndex( pParse, pWC, pTabItem, notReady, pLevel );
  4883. }
  4884. else
  4885. #endif
  4886. if ( ( pLevel.plan.wsFlags & WHERE_INDEXED ) != 0 )
  4887. {
  4888. Index pIx = pLevel.plan.u.pIdx;
  4889. KeyInfo pKey = sqlite3IndexKeyinfo( pParse, pIx );
  4890. int iIdxCur = pLevel.iIdxCur;
  4891. Debug.Assert( pIx.pSchema == pTab.pSchema );
  4892. Debug.Assert( iIdxCur >= 0 );
  4893. sqlite3VdbeAddOp4( v, OP_OpenRead, iIdxCur, pIx.tnum, iDb,
  4894. pKey, P4_KEYINFO_HANDOFF );
  4895. #if SQLITE_DEBUG
  4896. VdbeComment( v, "%s", pIx.zName );
  4897. #endif
  4898. }
  4899. sqlite3CodeVerifySchema( pParse, iDb );
  4900. notReady &= ~getMask( pWC.pMaskSet, pTabItem.iCursor );
  4901. }
  4902. pWInfo.iTop = sqlite3VdbeCurrentAddr( v );
  4903. //if( db.mallocFailed ) goto whereBeginError;
  4904. /* Generate the code to do the search. Each iteration of the for
  4905. ** loop below generates code for a single nested loop of the VM
  4906. ** program.
  4907. */
  4908. notReady = ~(Bitmask)0;
  4909. for ( i = 0; i < nTabList; i++ )
  4910. {
  4911. pLevel = pWInfo.a[i];
  4912. explainOneScan( pParse, pTabList, pLevel, i, pLevel.iFrom, wctrlFlags );
  4913. notReady = codeOneLoopStart( pWInfo, i, wctrlFlags, notReady );
  4914. pWInfo.iContinue = pLevel.addrCont;
  4915. }
  4916. #if SQLITE_TEST //* For testing and debugging use only */
  4917. /* Record in the query plan information about the current table
  4918. ** and the index used to access it (if any). If the table itself
  4919. ** is not used, its name is just '{}'. If no index is used
  4920. ** the index is listed as "{}". If the primary key is used the
  4921. ** index name is '*'.
  4922. */
  4923. sqlite3_query_plan.sValue = "";
  4924. for ( i = 0; i < nTabList; i++ )
  4925. {
  4926. string z;
  4927. int n;
  4928. pLevel = pWInfo.a[i];
  4929. pTabItem = pTabList.a[pLevel.iFrom];
  4930. z = pTabItem.zAlias;
  4931. if ( z == null )
  4932. z = pTabItem.pTab.zName;
  4933. n = sqlite3Strlen30( z );
  4934. if ( true ) //n+nQPlan < sizeof(sqlite3_query_plan)-10 )
  4935. {
  4936. if ( ( pLevel.plan.wsFlags & WHERE_IDX_ONLY ) != 0 )
  4937. {
  4938. sqlite3_query_plan.Append( "{}" ); //memcpy( &sqlite3_query_plan[nQPlan], "{}", 2 );
  4939. nQPlan += 2;
  4940. }
  4941. else
  4942. {
  4943. sqlite3_query_plan.Append( z ); //memcpy( &sqlite3_query_plan[nQPlan], z, n );
  4944. nQPlan += n;
  4945. }
  4946. sqlite3_query_plan.Append( " " );
  4947. nQPlan++; //sqlite3_query_plan[nQPlan++] = ' ';
  4948. }
  4949. testcase( pLevel.plan.wsFlags & WHERE_ROWID_EQ );
  4950. testcase( pLevel.plan.wsFlags & WHERE_ROWID_RANGE );
  4951. if ( ( pLevel.plan.wsFlags & ( WHERE_ROWID_EQ | WHERE_ROWID_RANGE ) ) != 0 )
  4952. {
  4953. sqlite3_query_plan.Append( "* " ); //memcpy(&sqlite3_query_plan[nQPlan], "* ", 2);
  4954. nQPlan += 2;
  4955. }
  4956. else if ( ( pLevel.plan.wsFlags & WHERE_INDEXED ) != 0 )
  4957. {
  4958. n = sqlite3Strlen30( pLevel.plan.u.pIdx.zName );
  4959. if ( true ) //n+nQPlan < sizeof(sqlite3_query_plan)-2 )//if( n+nQPlan < sizeof(sqlite3_query_plan)-2 )
  4960. {
  4961. sqlite3_query_plan.Append( pLevel.plan.u.pIdx.zName ); //memcpy(&sqlite3_query_plan[nQPlan], pLevel.plan.u.pIdx.zName, n);
  4962. nQPlan += n;
  4963. sqlite3_query_plan.Append( " " ); //sqlite3_query_plan[nQPlan++] = ' ';
  4964. }
  4965. }
  4966. else
  4967. {
  4968. sqlite3_query_plan.Append( "{} " ); //memcpy( &sqlite3_query_plan[nQPlan], "{} ", 3 );
  4969. nQPlan += 3;
  4970. }
  4971. }
  4972. //while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){
  4973. // sqlite3_query_plan[--nQPlan] = 0;
  4974. //}
  4975. //sqlite3_query_plan[nQPlan] = 0;
  4976. sqlite3_query_plan.Trim();
  4977. nQPlan = 0;
  4978. #endif //* SQLITE_TEST // Testing and debugging use only */
  4979. /* Record the continuation address in the WhereInfo structure. Then
  4980. ** clean up and return.
  4981. */
  4982. return pWInfo;
  4983. /* Jump here if malloc fails */
  4984. whereBeginError:
  4985. if ( pWInfo != null )
  4986. {
  4987. pParse.nQueryLoop = pWInfo.savedNQueryLoop;
  4988. whereInfoFree( db, pWInfo );
  4989. }
  4990. return null;
  4991. }
  4992. /*
  4993. ** Generate the end of the WHERE loop. See comments on
  4994. ** sqlite3WhereBegin() for additional information.
  4995. */
  4996. static void sqlite3WhereEnd( WhereInfo pWInfo )
  4997. {
  4998. Parse pParse = pWInfo.pParse;
  4999. Vdbe v = pParse.pVdbe;
  5000. int i;
  5001. WhereLevel pLevel;
  5002. SrcList pTabList = pWInfo.pTabList;
  5003. sqlite3 db = pParse.db;
  5004. /* Generate loop termination code.
  5005. */
  5006. sqlite3ExprCacheClear( pParse );
  5007. for ( i = pWInfo.nLevel - 1; i >= 0; i-- )
  5008. {
  5009. pLevel = pWInfo.a[i];
  5010. sqlite3VdbeResolveLabel( v, pLevel.addrCont );
  5011. if ( pLevel.op != OP_Noop )
  5012. {
  5013. sqlite3VdbeAddOp2( v, pLevel.op, pLevel.p1, pLevel.p2 );
  5014. sqlite3VdbeChangeP5( v, pLevel.p5 );
  5015. }
  5016. if ( ( pLevel.plan.wsFlags & WHERE_IN_ABLE ) != 0 && pLevel.u._in.nIn > 0 )
  5017. {
  5018. InLoop pIn;
  5019. int j;
  5020. sqlite3VdbeResolveLabel( v, pLevel.addrNxt );
  5021. for ( j = pLevel.u._in.nIn; j > 0; j-- )//, pIn--)
  5022. {
  5023. pIn = pLevel.u._in.aInLoop[j - 1];
  5024. sqlite3VdbeJumpHere( v, pIn.addrInTop + 1 );
  5025. sqlite3VdbeAddOp2( v, OP_Next, pIn.iCur, pIn.addrInTop );
  5026. sqlite3VdbeJumpHere( v, pIn.addrInTop - 1 );
  5027. }
  5028. sqlite3DbFree( db, ref pLevel.u._in.aInLoop );
  5029. }
  5030. sqlite3VdbeResolveLabel( v, pLevel.addrBrk );
  5031. if ( pLevel.iLeftJoin != 0 )
  5032. {
  5033. int addr;
  5034. addr = sqlite3VdbeAddOp1( v, OP_IfPos, pLevel.iLeftJoin );
  5035. Debug.Assert( ( pLevel.plan.wsFlags & WHERE_IDX_ONLY ) == 0
  5036. || ( pLevel.plan.wsFlags & WHERE_INDEXED ) != 0 );
  5037. if ( ( pLevel.plan.wsFlags & WHERE_IDX_ONLY ) == 0 )
  5038. {
  5039. sqlite3VdbeAddOp1( v, OP_NullRow, pTabList.a[i].iCursor );
  5040. }
  5041. if ( pLevel.iIdxCur >= 0 )
  5042. {
  5043. sqlite3VdbeAddOp1( v, OP_NullRow, pLevel.iIdxCur );
  5044. }
  5045. if ( pLevel.op == OP_Return )
  5046. {
  5047. sqlite3VdbeAddOp2( v, OP_Gosub, pLevel.p1, pLevel.addrFirst );
  5048. }
  5049. else
  5050. {
  5051. sqlite3VdbeAddOp2( v, OP_Goto, 0, pLevel.addrFirst );
  5052. }
  5053. sqlite3VdbeJumpHere( v, addr );
  5054. }
  5055. }
  5056. /* The "break" point is here, just past the end of the outer loop.
  5057. ** Set it.
  5058. */
  5059. sqlite3VdbeResolveLabel( v, pWInfo.iBreak );
  5060. /* Close all of the cursors that were opened by sqlite3WhereBegin.
  5061. */
  5062. Debug.Assert( pWInfo.nLevel == 1 || pWInfo.nLevel == pTabList.nSrc );
  5063. for ( i = 0; i < pWInfo.nLevel; i++ )// for(i=0, pLevel=pWInfo.a; i<pWInfo.nLevel; i++, pLevel++){
  5064. {
  5065. pLevel = pWInfo.a[i];
  5066. SrcList_item pTabItem = pTabList.a[pLevel.iFrom];
  5067. Table pTab = pTabItem.pTab;
  5068. Debug.Assert( pTab != null );
  5069. if ( ( pTab.tabFlags & TF_Ephemeral ) == 0
  5070. && pTab.pSelect == null
  5071. && ( pWInfo.wctrlFlags & WHERE_OMIT_CLOSE ) == 0
  5072. )
  5073. {
  5074. u32 ws = pLevel.plan.wsFlags;
  5075. if ( 0 == pWInfo.okOnePass && ( ws & WHERE_IDX_ONLY ) == 0 )
  5076. {
  5077. sqlite3VdbeAddOp1( v, OP_Close, pTabItem.iCursor );
  5078. }
  5079. if ( ( ws & WHERE_INDEXED ) != 0 && ( ws & WHERE_TEMP_INDEX ) == 0 )
  5080. {
  5081. sqlite3VdbeAddOp1( v, OP_Close, pLevel.iIdxCur );
  5082. }
  5083. }
  5084. /* If this scan uses an index, make code substitutions to read data
  5085. ** from the index in preference to the table. Sometimes, this means
  5086. ** the table need never be read from. This is a performance boost,
  5087. ** as the vdbe level waits until the table is read before actually
  5088. ** seeking the table cursor to the record corresponding to the current
  5089. ** position in the index.
  5090. **
  5091. ** Calls to the code generator in between sqlite3WhereBegin and
  5092. ** sqlite3WhereEnd will have created code that references the table
  5093. ** directly. This loop scans all that code looking for opcodes
  5094. ** that reference the table and converts them into opcodes that
  5095. ** reference the index.
  5096. */
  5097. if ( ( pLevel.plan.wsFlags & WHERE_INDEXED ) != 0 )///* && 0 == db.mallocFailed */ )
  5098. {
  5099. int k, j, last;
  5100. VdbeOp pOp;
  5101. Index pIdx = pLevel.plan.u.pIdx;
  5102. Debug.Assert( pIdx != null );
  5103. //pOp = sqlite3VdbeGetOp( v, pWInfo.iTop );
  5104. last = sqlite3VdbeCurrentAddr( v );
  5105. for ( k = pWInfo.iTop; k < last; k++ )//, pOp++ )
  5106. {
  5107. pOp = sqlite3VdbeGetOp( v, k );
  5108. if ( pOp.p1 != pLevel.iTabCur )
  5109. continue;
  5110. if ( pOp.opcode == OP_Column )
  5111. {
  5112. for ( j = 0; j < pIdx.nColumn; j++ )
  5113. {
  5114. if ( pOp.p2 == pIdx.aiColumn[j] )
  5115. {
  5116. pOp.p2 = j;
  5117. pOp.p1 = pLevel.iIdxCur;
  5118. break;
  5119. }
  5120. }
  5121. Debug.Assert( ( pLevel.plan.wsFlags & WHERE_IDX_ONLY ) == 0
  5122. || j < pIdx.nColumn );
  5123. }
  5124. else if ( pOp.opcode == OP_Rowid )
  5125. {
  5126. pOp.p1 = pLevel.iIdxCur;
  5127. pOp.opcode = OP_IdxRowid;
  5128. }
  5129. }
  5130. }
  5131. }
  5132. /* Final cleanup
  5133. */
  5134. pParse.nQueryLoop = pWInfo.savedNQueryLoop;
  5135. whereInfoFree( db, pWInfo );
  5136. return;
  5137. }
  5138. }
  5139. }