PageRenderTime 69ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/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

Large files files are truncated, but you can click here to view the full 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 ) );

Large files files are truncated, but you can click here to view the full file