PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/Community.CsharpSqlite/src/where_c.cs

https://bitbucket.org/eumario/csharp-sqlite
C# | 5843 lines | 3727 code | 271 blank | 1845 comment | 1241 complexity | a17d6f46b8cab672bc870d075d46415f 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-05-19 13:26:54 ed1da510a239ea767a01dc332b667119fa3c908ecd7
  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. #if SQLITE_ENABLE_STAT2
  145. //# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
  146. #else
  147. //# define TERM_VNULL 0x00 /* Disabled if not using stat2 */
  148. #endif
  149. const int TERM_DYNAMIC = 0x01; /* Need to call sqlite3ExprDelete(db, ref pExpr) */
  150. const int TERM_VIRTUAL = 0x02; /* Added by the optimizer. Do not code */
  151. const int TERM_CODED = 0x04; /* This term is already coded */
  152. const int TERM_COPIED = 0x08; /* Has a child */
  153. const int TERM_ORINFO = 0x10; /* Need to free the WhereTerm.u.pOrInfo object */
  154. const int TERM_ANDINFO = 0x20; /* Need to free the WhereTerm.u.pAndInfo obj */
  155. const int TERM_OR_OK = 0x40; /* Used during OR-clause processing */
  156. #if SQLITE_ENABLE_STAT2
  157. const int TERM_VNULL = 0x80; /* Manufactured x>NULL or x<=NULL term */
  158. #else
  159. const int TERM_VNULL = 0x00; /* Disabled if not using stat2 */
  160. #endif
  161. /*
  162. ** An instance of the following structure holds all information about a
  163. ** WHERE clause. Mostly this is a container for one or more WhereTerms.
  164. */
  165. public class WhereClause
  166. {
  167. public Parse pParse; /* The parser context */
  168. public WhereMaskSet pMaskSet; /* Mapping of table cursor numbers to bitmasks */
  169. public Bitmask vmask; /* Bitmask identifying virtual table cursors */
  170. public u8 op; /* Split operator. TK_AND or TK_OR */
  171. public int nTerm; /* Number of terms */
  172. public int nSlot; /* Number of entries in a[] */
  173. public WhereTerm[] a; /* Each a[] describes a term of the WHERE cluase */
  174. #if (SQLITE_SMALL_STACK)
  175. public WhereTerm[] aStatic = new WhereTerm[1]; /* Initial static space for a[] */
  176. #else
  177. public WhereTerm[] aStatic = new WhereTerm[8]; /* Initial static space for a[] */
  178. #endif
  179. public void CopyTo( WhereClause wc )
  180. {
  181. wc.pParse = this.pParse;
  182. wc.pMaskSet = new WhereMaskSet();
  183. this.pMaskSet.CopyTo( wc.pMaskSet );
  184. wc.op = this.op;
  185. wc.nTerm = this.nTerm;
  186. wc.nSlot = this.nSlot;
  187. wc.a = (WhereTerm[])this.a.Clone();
  188. wc.aStatic = (WhereTerm[])this.aStatic.Clone();
  189. }
  190. };
  191. /*
  192. ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
  193. ** a dynamically allocated instance of the following structure.
  194. */
  195. public class WhereOrInfo
  196. {
  197. public WhereClause wc = new WhereClause();/* Decomposition into subterms */
  198. public Bitmask indexable; /* Bitmask of all indexable tables in the clause */
  199. };
  200. /*
  201. ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
  202. ** a dynamically allocated instance of the following structure.
  203. */
  204. public class WhereAndInfo
  205. {
  206. public WhereClause wc = new WhereClause(); /* The subexpression broken out */
  207. };
  208. /*
  209. ** An instance of the following structure keeps track of a mapping
  210. ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
  211. **
  212. ** The VDBE cursor numbers are small integers contained in
  213. ** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
  214. ** clause, the cursor numbers might not begin with 0 and they might
  215. ** contain gaps in the numbering sequence. But we want to make maximum
  216. ** use of the bits in our bitmasks. This structure provides a mapping
  217. ** from the sparse cursor numbers into consecutive integers beginning
  218. ** with 0.
  219. **
  220. ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
  221. ** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
  222. **
  223. ** For example, if the WHERE clause expression used these VDBE
  224. ** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
  225. ** would map those cursor numbers into bits 0 through 5.
  226. **
  227. ** Note that the mapping is not necessarily ordered. In the example
  228. ** above, the mapping might go like this: 4.3, 5.1, 8.2, 29.0,
  229. ** 57.5, 73.4. Or one of 719 other combinations might be used. It
  230. ** does not really matter. What is important is that sparse cursor
  231. ** numbers all get mapped into bit numbers that begin with 0 and contain
  232. ** no gaps.
  233. */
  234. public class WhereMaskSet
  235. {
  236. public int n; /* Number of Debug.Assigned cursor values */
  237. public int[] ix = new int[BMS]; /* Cursor Debug.Assigned to each bit */
  238. public void CopyTo( WhereMaskSet wms )
  239. {
  240. wms.n = this.n;
  241. wms.ix = (int[])this.ix.Clone();
  242. }
  243. }
  244. /*
  245. ** A WhereCost object records a lookup strategy and the estimated
  246. ** cost of pursuing that strategy.
  247. */
  248. public class WhereCost
  249. {
  250. public WherePlan plan = new WherePlan();/* The lookup strategy */
  251. public double rCost; /* Overall cost of pursuing this search strategy */
  252. public Bitmask used; /* Bitmask of cursors used by this plan */
  253. public void Clear()
  254. {
  255. plan.Clear();
  256. rCost = 0;
  257. used = 0;
  258. }
  259. };
  260. /*
  261. ** Bitmasks for the operators that indices are able to exploit. An
  262. ** OR-ed combination of these values can be used when searching for
  263. ** terms in the where clause.
  264. */
  265. //#define WO_IN 0x001
  266. //#define WO_EQ 0x002
  267. //#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
  268. //#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
  269. //#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
  270. //#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
  271. //#define WO_MATCH 0x040
  272. //#define WO_ISNULL 0x080
  273. //#define WO_OR 0x100 /* Two or more OR-connected terms */
  274. //#define WO_AND 0x200 /* Two or more AND-connected terms */
  275. //#define WO_NOOP 0x800 /* This term does not restrict search space */
  276. //#define WO_ALL 0xfff /* Mask of all possible WO_* values */
  277. //#define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
  278. const int WO_IN = 0x001;
  279. const int WO_EQ = 0x002;
  280. const int WO_LT = ( WO_EQ << ( TK_LT - TK_EQ ) );
  281. const int WO_LE = ( WO_EQ << ( TK_LE - TK_EQ ) );
  282. const int WO_GT = ( WO_EQ << ( TK_GT - TK_EQ ) );
  283. const int WO_GE = ( WO_EQ << ( TK_GE - TK_EQ ) );
  284. const int WO_MATCH = 0x040;
  285. const int WO_ISNULL = 0x080;
  286. const int WO_OR = 0x100; /* Two or more OR-connected terms */
  287. const int WO_AND = 0x200; /* Two or more AND-connected terms */
  288. const int WO_NOOP = 0x800; /* This term does not restrict search space */
  289. const int WO_ALL = 0xfff; /* Mask of all possible WO_* values */
  290. const int WO_SINGLE = 0x0ff; /* Mask of all non-compound WO_* values */
  291. /*
  292. ** Value for wsFlags returned by bestIndex() and stored in
  293. ** WhereLevel.wsFlags. These flags determine which search
  294. ** strategies are appropriate.
  295. **
  296. ** The least significant 12 bits is reserved as a mask for WO_ values above.
  297. ** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL.
  298. ** But if the table is the right table of a left join, WhereLevel.wsFlags
  299. ** is set to WO_IN|WO_EQ. The WhereLevel.wsFlags field can then be used as
  300. ** the "op" parameter to findTerm when we are resolving equality constraints.
  301. ** ISNULL constraints will then not be used on the right table of a left
  302. ** join. Tickets #2177 and #2189.
  303. */
  304. //#define WHERE_ROWID_EQ 0x00001000 /* rowid=EXPR or rowid IN (...) */
  305. //#define WHERE_ROWID_RANGE 0x00002000 /* rowid<EXPR and/or rowid>EXPR */
  306. //#define WHERE_COLUMN_EQ 0x00010000 /* x=EXPR or x IN (...) or x IS NULL */
  307. //#define WHERE_COLUMN_RANGE 0x00020000 /* x<EXPR and/or x>EXPR */
  308. //#define WHERE_COLUMN_IN 0x00040000 /* x IN (...) */
  309. //#define WHERE_COLUMN_NULL 0x00080000 /* x IS NULL */
  310. //#define WHERE_INDEXED 0x000f0000 /* Anything that uses an index */
  311. //#define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */
  312. //#define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */
  313. //#define WHERE_TOP_LIMIT 0x00100000 /* x<EXPR or x<=EXPR constraint */
  314. //#define WHERE_BTM_LIMIT 0x00200000 /* x>EXPR or x>=EXPR constraint */
  315. //#define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and x<EXPR */
  316. //#define WHERE_IDX_ONLY 0x00800000 /* Use index only - omit table */
  317. //#define WHERE_ORDERBY 0x01000000 /* Output will appear in correct order */
  318. //#define WHERE_REVERSE 0x02000000 /* Scan in reverse order */
  319. //#define WHERE_UNIQUE 0x04000000 /* Selects no more than one row */
  320. //#define WHERE_VIRTUALTABLE 0x08000000 /* Use virtual-table processing */
  321. //#define WHERE_MULTI_OR 0x10000000 /* OR using multiple indices */
  322. //#define WHERE_TEMP_INDEX 0x20000000 /* Uses an ephemeral index */
  323. const int WHERE_ROWID_EQ = 0x00001000;
  324. const int WHERE_ROWID_RANGE = 0x00002000;
  325. const int WHERE_COLUMN_EQ = 0x00010000;
  326. const int WHERE_COLUMN_RANGE = 0x00020000;
  327. const int WHERE_COLUMN_IN = 0x00040000;
  328. const int WHERE_COLUMN_NULL = 0x00080000;
  329. const int WHERE_INDEXED = 0x000f0000;
  330. const int WHERE_IN_ABLE = 0x000f1000;
  331. const int WHERE_NOT_FULLSCAN = 0x100f3000;
  332. const int WHERE_TOP_LIMIT = 0x00100000;
  333. const int WHERE_BTM_LIMIT = 0x00200000;
  334. const int WHERE_BOTH_LIMIT = 0x00300000;
  335. const int WHERE_IDX_ONLY = 0x00800000;
  336. const int WHERE_ORDERBY = 0x01000000;
  337. const int WHERE_REVERSE = 0x02000000;
  338. const int WHERE_UNIQUE = 0x04000000;
  339. const int WHERE_VIRTUALTABLE = 0x08000000;
  340. const int WHERE_MULTI_OR = 0x10000000;
  341. const int WHERE_TEMP_INDEX = 0x20000000;
  342. /*
  343. ** Initialize a preallocated WhereClause structure.
  344. */
  345. static void whereClauseInit(
  346. WhereClause pWC, /* The WhereClause to be initialized */
  347. Parse pParse, /* The parsing context */
  348. WhereMaskSet pMaskSet /* Mapping from table cursor numbers to bitmasks */
  349. )
  350. {
  351. pWC.pParse = pParse;
  352. pWC.pMaskSet = pMaskSet;
  353. pWC.nTerm = 0;
  354. pWC.nSlot = ArraySize( pWC.aStatic ) - 1;
  355. pWC.a = pWC.aStatic;
  356. pWC.vmask = 0;
  357. }
  358. /* Forward reference */
  359. //static void whereClauseClear(WhereClause);
  360. /*
  361. ** Deallocate all memory Debug.Associated with a WhereOrInfo object.
  362. */
  363. static void whereOrInfoDelete( sqlite3 db, WhereOrInfo p )
  364. {
  365. whereClauseClear( p.wc );
  366. sqlite3DbFree( db, ref p );
  367. }
  368. /*
  369. ** Deallocate all memory Debug.Associated with a WhereAndInfo object.
  370. */
  371. static void whereAndInfoDelete( sqlite3 db, WhereAndInfo p )
  372. {
  373. whereClauseClear( p.wc );
  374. sqlite3DbFree( db, ref p );
  375. }
  376. /*
  377. ** Deallocate a WhereClause structure. The WhereClause structure
  378. ** itself is not freed. This routine is the inverse of whereClauseInit().
  379. */
  380. static void whereClauseClear( WhereClause pWC )
  381. {
  382. int i;
  383. WhereTerm a;
  384. sqlite3 db = pWC.pParse.db;
  385. for ( i = pWC.nTerm - 1; i >= 0; i-- )//, a++)
  386. {
  387. a = pWC.a[i];
  388. if ( ( a.wtFlags & TERM_DYNAMIC ) != 0 )
  389. {
  390. sqlite3ExprDelete( db, ref a.pExpr );
  391. }
  392. if ( ( a.wtFlags & TERM_ORINFO ) != 0 )
  393. {
  394. whereOrInfoDelete( db, a.u.pOrInfo );
  395. }
  396. else if ( ( a.wtFlags & TERM_ANDINFO ) != 0 )
  397. {
  398. whereAndInfoDelete( db, a.u.pAndInfo );
  399. }
  400. }
  401. if ( pWC.a != pWC.aStatic )
  402. {
  403. sqlite3DbFree( db, ref pWC.a );
  404. }
  405. }
  406. /*
  407. ** Add a single new WhereTerm entry to the WhereClause object pWC.
  408. ** The new WhereTerm object is constructed from Expr p and with wtFlags.
  409. ** The index in pWC.a[] of the new WhereTerm is returned on success.
  410. ** 0 is returned if the new WhereTerm could not be added due to a memory
  411. ** allocation error. The memory allocation failure will be recorded in
  412. ** the db.mallocFailed flag so that higher-level functions can detect it.
  413. **
  414. ** This routine will increase the size of the pWC.a[] array as necessary.
  415. **
  416. ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
  417. ** for freeing the expression p is Debug.Assumed by the WhereClause object pWC.
  418. ** This is true even if this routine fails to allocate a new WhereTerm.
  419. **
  420. ** WARNING: This routine might reallocate the space used to store
  421. ** WhereTerms. All pointers to WhereTerms should be invalidated after
  422. ** calling this routine. Such pointers may be reinitialized by referencing
  423. ** the pWC.a[] array.
  424. */
  425. static int whereClauseInsert( WhereClause pWC, Expr p, u8 wtFlags )
  426. {
  427. WhereTerm pTerm;
  428. int idx;
  429. testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */
  430. if ( pWC.nTerm >= pWC.nSlot )
  431. {
  432. //WhereTerm pOld = pWC.a;
  433. //sqlite3 db = pWC.pParse.db;
  434. Array.Resize( ref pWC.a, pWC.nSlot * 2 );
  435. //pWC.a = sqlite3DbMallocRaw(db, sizeof(pWC.a[0])*pWC.nSlot*2 );
  436. //if( pWC.a==null ){
  437. // if( wtFlags & TERM_DYNAMIC ){
  438. // sqlite3ExprDelete(db, ref p);
  439. // }
  440. // pWC.a = pOld;
  441. // return 0;
  442. //}
  443. //memcpy(pWC.a, pOld, sizeof(pWC.a[0])*pWC.nTerm);
  444. //if( pOld!=pWC.aStatic ){
  445. // sqlite3DbFree(db, ref pOld);
  446. //}
  447. //pWC.nSlot = sqlite3DbMallocSize(db, pWC.a)/sizeof(pWC.a[0]);
  448. pWC.nSlot = pWC.a.Length - 1;
  449. }
  450. pWC.a[idx = pWC.nTerm++] = new WhereTerm();
  451. pTerm = pWC.a[idx];
  452. pTerm.pExpr = p;
  453. pTerm.wtFlags = wtFlags;
  454. pTerm.pWC = pWC;
  455. pTerm.iParent = -1;
  456. return idx;
  457. }
  458. /*
  459. ** This routine identifies subexpressions in the WHERE clause where
  460. ** each subexpression is separated by the AND operator or some other
  461. ** operator specified in the op parameter. The WhereClause structure
  462. ** is filled with pointers to subexpressions. For example:
  463. **
  464. ** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
  465. ** \________/ \_______________/ \________________/
  466. ** slot[0] slot[1] slot[2]
  467. **
  468. ** The original WHERE clause in pExpr is unaltered. All this routine
  469. ** does is make slot[] entries point to substructure within pExpr.
  470. **
  471. ** In the previous sentence and in the diagram, "slot[]" refers to
  472. ** the WhereClause.a[] array. The slot[] array grows as needed to contain
  473. ** all terms of the WHERE clause.
  474. */
  475. static void whereSplit( WhereClause pWC, Expr pExpr, int op )
  476. {
  477. pWC.op = (u8)op;
  478. if ( pExpr == null )
  479. return;
  480. if ( pExpr.op != op )
  481. {
  482. whereClauseInsert( pWC, pExpr, 0 );
  483. }
  484. else
  485. {
  486. whereSplit( pWC, pExpr.pLeft, op );
  487. whereSplit( pWC, pExpr.pRight, op );
  488. }
  489. }
  490. /*
  491. ** Initialize an expression mask set (a WhereMaskSet object)
  492. */
  493. //#define initMaskSet(P) memset(P, 0, sizeof(*P))
  494. /*
  495. ** Return the bitmask for the given cursor number. Return 0 if
  496. ** iCursor is not in the set.
  497. */
  498. static Bitmask getMask( WhereMaskSet pMaskSet, int iCursor )
  499. {
  500. int i;
  501. Debug.Assert( pMaskSet.n <= (int)sizeof( Bitmask ) * 8 );
  502. for ( i = 0; i < pMaskSet.n; i++ )
  503. {
  504. if ( pMaskSet.ix[i] == iCursor )
  505. {
  506. return ( (Bitmask)1 ) << i;
  507. }
  508. }
  509. return 0;
  510. }
  511. /*
  512. ** Create a new mask for cursor iCursor.
  513. **
  514. ** There is one cursor per table in the FROM clause. The number of
  515. ** tables in the FROM clause is limited by a test early in the
  516. ** sqlite3WhereBegin() routine. So we know that the pMaskSet.ix[]
  517. ** array will never overflow.
  518. */
  519. static void createMask( WhereMaskSet pMaskSet, int iCursor )
  520. {
  521. Debug.Assert( pMaskSet.n < ArraySize( pMaskSet.ix ) );
  522. pMaskSet.ix[pMaskSet.n++] = iCursor;
  523. }
  524. /*
  525. ** This routine walks (recursively) an expression tree and generates
  526. ** a bitmask indicating which tables are used in that expression
  527. ** tree.
  528. **
  529. ** In order for this routine to work, the calling function must have
  530. ** previously invoked sqlite3ResolveExprNames() on the expression. See
  531. ** the header comment on that routine for additional information.
  532. ** The sqlite3ResolveExprNames() routines looks for column names and
  533. ** sets their opcodes to TK_COLUMN and their Expr.iTable fields to
  534. ** the VDBE cursor number of the table. This routine just has to
  535. ** translate the cursor numbers into bitmask values and OR all
  536. ** the bitmasks together.
  537. */
  538. //static Bitmask exprListTableUsage(WhereMaskSet*, ExprList);
  539. //static Bitmask exprSelectTableUsage(WhereMaskSet*, Select);
  540. static Bitmask exprTableUsage( WhereMaskSet pMaskSet, Expr p )
  541. {
  542. Bitmask mask = 0;
  543. if ( p == null )
  544. return 0;
  545. if ( p.op == TK_COLUMN )
  546. {
  547. mask = getMask( pMaskSet, p.iTable );
  548. return mask;
  549. }
  550. mask = exprTableUsage( pMaskSet, p.pRight );
  551. mask |= exprTableUsage( pMaskSet, p.pLeft );
  552. if ( ExprHasProperty( p, EP_xIsSelect ) )
  553. {
  554. mask |= exprSelectTableUsage( pMaskSet, p.x.pSelect );
  555. }
  556. else
  557. {
  558. mask |= exprListTableUsage( pMaskSet, p.x.pList );
  559. }
  560. return mask;
  561. }
  562. static Bitmask exprListTableUsage( WhereMaskSet pMaskSet, ExprList pList )
  563. {
  564. int i;
  565. Bitmask mask = 0;
  566. if ( pList != null )
  567. {
  568. for ( i = 0; i < pList.nExpr; i++ )
  569. {
  570. mask |= exprTableUsage( pMaskSet, pList.a[i].pExpr );
  571. }
  572. }
  573. return mask;
  574. }
  575. static Bitmask exprSelectTableUsage( WhereMaskSet pMaskSet, Select pS )
  576. {
  577. Bitmask mask = 0;
  578. while ( pS != null )
  579. {
  580. mask |= exprListTableUsage( pMaskSet, pS.pEList );
  581. mask |= exprListTableUsage( pMaskSet, pS.pGroupBy );
  582. mask |= exprListTableUsage( pMaskSet, pS.pOrderBy );
  583. mask |= exprTableUsage( pMaskSet, pS.pWhere );
  584. mask |= exprTableUsage( pMaskSet, pS.pHaving );
  585. pS = pS.pPrior;
  586. }
  587. return mask;
  588. }
  589. /*
  590. ** Return TRUE if the given operator is one of the operators that is
  591. ** allowed for an indexable WHERE clause term. The allowed operators are
  592. ** "=", "<", ">", "<=", ">=", and "IN".
  593. **
  594. ** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be
  595. ** of one of the following forms: column = expression column > expression
  596. ** column >= expression column < expression column <= expression
  597. ** expression = column expression > column expression >= column
  598. ** expression < column expression <= column column IN
  599. ** (expression-list) column IN (subquery) column IS NULL
  600. */
  601. static bool allowedOp( int op )
  602. {
  603. Debug.Assert( TK_GT > TK_EQ && TK_GT < TK_GE );
  604. Debug.Assert( TK_LT > TK_EQ && TK_LT < TK_GE );
  605. Debug.Assert( TK_LE > TK_EQ && TK_LE < TK_GE );
  606. Debug.Assert( TK_GE == TK_EQ + 4 );
  607. return op == TK_IN || ( op >= TK_EQ && op <= TK_GE ) || op == TK_ISNULL;
  608. }
  609. /*
  610. ** Swap two objects of type TYPE.
  611. */
  612. //#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
  613. /*
  614. ** Commute a comparison operator. Expressions of the form "X op Y"
  615. ** are converted into "Y op X".
  616. **
  617. ** If a collation sequence is Debug.Associated with either the left or right
  618. ** side of the comparison, it remains Debug.Associated with the same side after
  619. ** the commutation. So "Y collate NOCASE op X" becomes
  620. ** "X collate NOCASE op Y". This is because any collation sequence on
  621. ** the left hand side of a comparison overrides any collation sequence
  622. ** attached to the right. For the same reason the EP_ExpCollate flag
  623. ** is not commuted.
  624. */
  625. static void exprCommute( Parse pParse, Expr pExpr )
  626. {
  627. u16 expRight = (u16)( pExpr.pRight.flags & EP_ExpCollate );
  628. u16 expLeft = (u16)( pExpr.pLeft.flags & EP_ExpCollate );
  629. Debug.Assert( allowedOp( pExpr.op ) && pExpr.op != TK_IN );
  630. pExpr.pRight.pColl = sqlite3ExprCollSeq( pParse, pExpr.pRight );
  631. pExpr.pLeft.pColl = sqlite3ExprCollSeq( pParse, pExpr.pLeft );
  632. SWAP( ref pExpr.pRight.pColl, ref pExpr.pLeft.pColl );
  633. pExpr.pRight.flags = (u16)( ( pExpr.pRight.flags & ~EP_ExpCollate ) | expLeft );
  634. pExpr.pLeft.flags = (u16)( ( pExpr.pLeft.flags & ~EP_ExpCollate ) | expRight );
  635. SWAP( ref pExpr.pRight, ref pExpr.pLeft );
  636. if ( pExpr.op >= TK_GT )
  637. {
  638. Debug.Assert( TK_LT == TK_GT + 2 );
  639. Debug.Assert( TK_GE == TK_LE + 2 );
  640. Debug.Assert( TK_GT > TK_EQ );
  641. Debug.Assert( TK_GT < TK_LE );
  642. Debug.Assert( pExpr.op >= TK_GT && pExpr.op <= TK_GE );
  643. pExpr.op = (u8)( ( ( pExpr.op - TK_GT ) ^ 2 ) + TK_GT );
  644. }
  645. }
  646. /*
  647. ** Translate from TK_xx operator to WO_xx bitmask.
  648. */
  649. static u16 operatorMask( int op )
  650. {
  651. u16 c;
  652. Debug.Assert( allowedOp( op ) );
  653. if ( op == TK_IN )
  654. {
  655. c = WO_IN;
  656. }
  657. else if ( op == TK_ISNULL )
  658. {
  659. c = WO_ISNULL;
  660. }
  661. else
  662. {
  663. Debug.Assert( ( WO_EQ << ( op - TK_EQ ) ) < 0x7fff );
  664. c = (u16)( WO_EQ << ( op - TK_EQ ) );
  665. }
  666. Debug.Assert( op != TK_ISNULL || c == WO_ISNULL );
  667. Debug.Assert( op != TK_IN || c == WO_IN );
  668. Debug.Assert( op != TK_EQ || c == WO_EQ );
  669. Debug.Assert( op != TK_LT || c == WO_LT );
  670. Debug.Assert( op != TK_LE || c == WO_LE );
  671. Debug.Assert( op != TK_GT || c == WO_GT );
  672. Debug.Assert( op != TK_GE || c == WO_GE );
  673. return c;
  674. }
  675. /*
  676. ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
  677. ** where X is a reference to the iColumn of table iCur and <op> is one of
  678. ** the WO_xx operator codes specified by the op parameter.
  679. ** Return a pointer to the term. Return 0 if not found.
  680. */
  681. static WhereTerm findTerm(
  682. WhereClause pWC, /* The WHERE clause to be searched */
  683. int iCur, /* Cursor number of LHS */
  684. int iColumn, /* Column number of LHS */
  685. Bitmask notReady, /* RHS must not overlap with this mask */
  686. u32 op, /* Mask of WO_xx values describing operator */
  687. Index pIdx /* Must be compatible with this index, if not NULL */
  688. )
  689. {
  690. WhereTerm pTerm;
  691. int k;
  692. Debug.Assert( iCur >= 0 );
  693. op &= WO_ALL;
  694. for ( k = pWC.nTerm; k != 0; k-- )//, pTerm++)
  695. {
  696. pTerm = pWC.a[pWC.nTerm - k];
  697. if ( pTerm.leftCursor == iCur
  698. && ( pTerm.prereqRight & notReady ) == 0
  699. && pTerm.u.leftColumn == iColumn
  700. && ( pTerm.eOperator & op ) != 0
  701. )
  702. {
  703. if ( pIdx != null && pTerm.eOperator != WO_ISNULL )
  704. {
  705. Expr pX = pTerm.pExpr;
  706. CollSeq pColl;
  707. char idxaff;
  708. int j;
  709. Parse pParse = pWC.pParse;
  710. idxaff = pIdx.pTable.aCol[iColumn].affinity;
  711. if ( !sqlite3IndexAffinityOk( pX, idxaff ) )
  712. continue;
  713. /* Figure out the collation sequence required from an index for
  714. ** it to be useful for optimising expression pX. Store this
  715. ** value in variable pColl.
  716. */
  717. Debug.Assert( pX.pLeft != null );
  718. pColl = sqlite3BinaryCompareCollSeq( pParse, pX.pLeft, pX.pRight );
  719. Debug.Assert( pColl != null || pParse.nErr != 0 );
  720. for ( j = 0; pIdx.aiColumn[j] != iColumn; j++ )
  721. {
  722. if ( NEVER( j >= pIdx.nColumn ) )
  723. return null;
  724. }
  725. if ( pColl != null && !pColl.zName.Equals( pIdx.azColl[j], StringComparison.OrdinalIgnoreCase ) )
  726. continue;
  727. }
  728. return pTerm;
  729. }
  730. }
  731. return null;
  732. }
  733. /* Forward reference */
  734. //static void exprAnalyze(SrcList*, WhereClause*, int);
  735. /*
  736. ** Call exprAnalyze on all terms in a WHERE clause.
  737. **
  738. **
  739. */
  740. static void exprAnalyzeAll(
  741. SrcList pTabList, /* the FROM clause */
  742. WhereClause pWC /* the WHERE clause to be analyzed */
  743. )
  744. {
  745. int i;
  746. for ( i = pWC.nTerm - 1; i >= 0; i-- )
  747. {
  748. exprAnalyze( pTabList, pWC, i );
  749. }
  750. }
  751. #if !SQLITE_OMIT_LIKE_OPTIMIZATION
  752. /*
  753. ** Check to see if the given expression is a LIKE or GLOB operator that
  754. ** can be optimized using inequality constraints. Return TRUE if it is
  755. ** so and false if not.
  756. **
  757. ** In order for the operator to be optimizible, the RHS must be a string
  758. ** literal that does not begin with a wildcard.
  759. */
  760. static int isLikeOrGlob(
  761. Parse pParse, /* Parsing and code generating context */
  762. Expr pExpr, /* Test this expression */
  763. ref Expr ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */
  764. ref bool pisComplete, /* True if the only wildcard is % in the last character */
  765. ref bool pnoCase /* True if uppercase is equivalent to lowercase */
  766. )
  767. {
  768. string z = null; /* String on RHS of LIKE operator */
  769. Expr pRight, pLeft; /* Right and left size of LIKE operator */
  770. ExprList pList; /* List of operands to the LIKE operator */
  771. int c = 0; /* One character in z[] */
  772. int cnt; /* Number of non-wildcard prefix characters */
  773. char[] wc = new char[3]; /* Wildcard characters */
  774. sqlite3 db = pParse.db; /* Data_base connection */
  775. sqlite3_value pVal = null;
  776. int op; /* Opcode of pRight */
  777. if ( !sqlite3IsLikeFunction( db, pExpr, ref pnoCase, wc ) )
  778. {
  779. return 0;
  780. }
  781. //#if SQLITE_EBCDIC
  782. //if( pnoCase ) return 0;
  783. //#endif
  784. pList = pExpr.x.pList;
  785. pLeft = pList.a[1].pExpr;
  786. if ( pLeft.op != TK_COLUMN || sqlite3ExprAffinity( pLeft ) != SQLITE_AFF_TEXT )
  787. {
  788. /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
  789. ** be the name of an indexed column with TEXT affinity. */
  790. return 0;
  791. }
  792. Debug.Assert( pLeft.iColumn != ( -1 ) ); /* Because IPK never has AFF_TEXT */
  793. pRight = pList.a[0].pExpr;
  794. op = pRight.op;
  795. if ( op == TK_REGISTER )
  796. {
  797. op = pRight.op2;
  798. }
  799. if ( op == TK_VARIABLE )
  800. {
  801. Vdbe pReprepare = pParse.pReprepare;
  802. int iCol = pRight.iColumn;
  803. pVal = sqlite3VdbeGetValue( pReprepare, iCol, (byte)SQLITE_AFF_NONE );
  804. if ( pVal != null && sqlite3_value_type( pVal ) == SQLITE_TEXT )
  805. {
  806. z = sqlite3_value_text( pVal );
  807. }
  808. sqlite3VdbeSetVarmask( pParse.pVdbe, iCol ); /* IMP: R-23257-02778 */
  809. Debug.Assert( pRight.op == TK_VARIABLE || pRight.op == TK_REGISTER );
  810. }
  811. else if ( op == TK_STRING )
  812. {
  813. z = pRight.u.zToken;
  814. }
  815. if ( !String.IsNullOrEmpty( z ) )
  816. {
  817. cnt = 0;
  818. while ( cnt < z.Length && ( c = z[cnt] ) != 0 && c != wc[0] && c != wc[1] && c != wc[2] )
  819. {
  820. cnt++;
  821. }
  822. if ( cnt != 0 && 255 != (u8)z[cnt - 1] )
  823. {
  824. Expr pPrefix;
  825. pisComplete = c == wc[0] && cnt == z.Length - 1;
  826. pPrefix = sqlite3Expr( db, TK_STRING, z );
  827. if ( pPrefix != null )
  828. pPrefix.u.zToken = pPrefix.u.zToken.Substring( 0, cnt );
  829. ppPrefix = pPrefix;
  830. if ( op == TK_VARIABLE )
  831. {
  832. Vdbe v = pParse.pVdbe;
  833. sqlite3VdbeSetVarmask( v, pRight.iColumn ); /* IMP: R-23257-02778 */
  834. if ( pisComplete && pRight.u.zToken.Length > 1 )
  835. {
  836. /* If the rhs of the LIKE expression is a variable, and the current
  837. ** value of the variable means there is no need to invoke the LIKE
  838. ** function, then no OP_Variable will be added to the program.
  839. ** This causes problems for the sqlite3_bind_parameter_name()
  840. ** API. To workaround them, add a dummy OP_Variable here.
  841. */
  842. int r1 = sqlite3GetTempReg( pParse );
  843. sqlite3ExprCodeTarget( pParse, pRight, r1 );
  844. sqlite3VdbeChangeP3( v, sqlite3VdbeCurrentAddr( v ) - 1, 0 );
  845. sqlite3ReleaseTempReg( pParse, r1 );
  846. }
  847. }
  848. }
  849. else
  850. {
  851. z = null;
  852. }
  853. }
  854. sqlite3ValueFree( ref pVal );
  855. return ( z != null ) ? 1 : 0;
  856. }
  857. #endif //* SQLITE_OMIT_LIKE_OPTIMIZATION */
  858. #if !SQLITE_OMIT_VIRTUALTABLE
  859. /*
  860. ** Check to see if the given expression is of the form
  861. **
  862. ** column MATCH expr
  863. **
  864. ** If it is then return TRUE. If not, return FALSE.
  865. */
  866. static int isMatchOfColumn(
  867. Expr pExpr /* Test this expression */
  868. ){
  869. ExprList pList;
  870. if( pExpr.op!=TK_FUNCTION ){
  871. return 0;
  872. }
  873. if( !pExpr.u.zToken.Equals("match", StringComparison.OrdinalIgnoreCase ) ){
  874. return 0;
  875. }
  876. pList = pExpr.x.pList;
  877. if( pList.nExpr!=2 ){
  878. return 0;
  879. }
  880. if( pList.a[1].pExpr.op != TK_COLUMN ){
  881. return 0;
  882. }
  883. return 1;
  884. }
  885. #endif //* SQLITE_OMIT_VIRTUALTABLE */
  886. /*
  887. ** If the pBase expression originated in the ON or USING clause of
  888. ** a join, then transfer the appropriate markings over to derived.
  889. */
  890. static void transferJoinMarkings( Expr pDerived, Expr pBase )
  891. {
  892. pDerived.flags = (u16)( pDerived.flags | pBase.flags & EP_FromJoin );
  893. pDerived.iRightJoinTable = pBase.iRightJoinTable;
  894. }
  895. #if !(SQLITE_OMIT_OR_OPTIMIZATION) && !(SQLITE_OMIT_SUBQUERY)
  896. /*
  897. ** Analyze a term that consists of two or more OR-connected
  898. ** subterms. So in:
  899. **
  900. ** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
  901. ** ^^^^^^^^^^^^^^^^^^^^
  902. **
  903. ** This routine analyzes terms such as the middle term in the above example.
  904. ** A WhereOrTerm object is computed and attached to the term under
  905. ** analysis, regardless of the outcome of the analysis. Hence:
  906. **
  907. ** WhereTerm.wtFlags |= TERM_ORINFO
  908. ** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object
  909. **
  910. ** The term being analyzed must have two or more of OR-connected subterms.
  911. ** A single subterm might be a set of AND-connected sub-subterms.
  912. ** Examples of terms under analysis:
  913. **
  914. ** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
  915. ** (B) x=expr1 OR expr2=x OR x=expr3
  916. ** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
  917. ** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
  918. ** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
  919. **
  920. ** CASE 1:
  921. **
  922. ** If all subterms are of the form T.C=expr for some single column of C
  923. ** a single table T (as shown in example B above) then create a new virtual
  924. ** term that is an equivalent IN expression. In other words, if the term
  925. ** being analyzed is:
  926. **
  927. ** x = expr1 OR expr2 = x OR x = expr3
  928. **
  929. ** then create a new virtual term like this:
  930. **
  931. ** x IN (expr1,expr2,expr3)
  932. **
  933. ** CASE 2:
  934. **
  935. ** If all subterms are indexable by a single table T, then set
  936. **
  937. ** WhereTerm.eOperator = WO_OR
  938. ** WhereTerm.u.pOrInfo.indexable |= the cursor number for table T
  939. **
  940. ** A subterm is "indexable" if it is of the form
  941. ** "T.C <op> <expr>" where C is any column of table T and
  942. ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
  943. ** A subterm is also indexable if it is an AND of two or more
  944. ** subsubterms at least one of which is indexable. Indexable AND
  945. ** subterms have their eOperator set to WO_AND and they have
  946. ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
  947. **
  948. ** From another point of view, "indexable" means that the subterm could
  949. ** potentially be used with an index if an appropriate index exists.
  950. ** This analysis does not consider whether or not the index exists; that
  951. ** is something the bestIndex() routine will determine. This analysis
  952. ** only looks at whether subterms appropriate for indexing exist.
  953. **
  954. ** All examples A through E above all satisfy case 2. But if a term
  955. ** also statisfies case 1 (such as B) we know that the optimizer will
  956. ** always prefer case 1, so in that case we pretend that case 2 is not
  957. ** satisfied.
  958. **
  959. ** It might be the case that multiple tables are indexable. For example,
  960. ** (E) above is indexable on tables P, Q, and R.
  961. **
  962. ** Terms that satisfy case 2 are candidates for lookup by using
  963. ** separate indices to find rowids for each subterm and composing
  964. ** the union of all rowids using a RowSet object. This is similar
  965. ** to "bitmap indices" in other data_base engines.
  966. **
  967. ** OTHERWISE:
  968. **
  969. ** If neither case 1 nor case 2 apply, then leave the eOperator set to
  970. ** zero. This term is not useful for search.
  971. */
  972. static void exprAnalyzeOrTerm(
  973. SrcList pSrc, /* the FROM clause */
  974. WhereClause pWC, /* the complete WHERE clause */
  975. int idxTerm /* Index of the OR-term to be analyzed */
  976. )
  977. {
  978. Parse pParse = pWC.pParse; /* Parser context */
  979. sqlite3 db = pParse.db; /* Data_base connection */
  980. WhereTerm pTerm = pWC.a[idxTerm]; /* The term to be analyzed */
  981. Expr pExpr = pTerm.pExpr; /* The expression of the term */
  982. WhereMaskSet pMaskSet = pWC.pMaskSet; /* Table use masks */
  983. int i; /* Loop counters */
  984. WhereClause pOrWc; /* Breakup of pTerm into subterms */
  985. WhereTerm pOrTerm; /* A Sub-term within the pOrWc */
  986. WhereOrInfo pOrInfo; /* Additional information Debug.Associated with pTerm */
  987. Bitmask chngToIN; /* Tables that might satisfy case 1 */
  988. Bitmask indexable; /* Tables that are indexable, satisfying case 2 */
  989. /*
  990. ** Break the OR clause into its separate subterms. The subterms are
  991. ** stored in a WhereClause structure containing within the WhereOrInfo
  992. ** object that is attached to the original OR clause term.
  993. */
  994. Debug.Assert( ( pTerm.wtFlags & ( TERM_DYNAMIC | TERM_ORINFO | TERM_ANDINFO ) ) == 0 );
  995. Debug.Assert( pExpr.op == TK_OR );
  996. pTerm.u.pOrInfo = pOrInfo = new WhereOrInfo();//sqlite3DbMallocZero(db, sizeof(*pOrInfo));
  997. if ( pOrInfo == null )
  998. return;
  999. pTerm.wtFlags |= TERM_ORINFO;
  1000. pOrWc = pOrInfo.wc;
  1001. whereClauseInit( pOrWc, pWC.pParse, pMaskSet );
  1002. whereSplit( pOrWc, pExpr, TK_OR );
  1003. exprAnalyzeAll( pSrc, pOrWc );
  1004. // if ( db.mallocFailed != 0 ) return;
  1005. Debug.Assert( pOrWc.nTerm >= 2 );
  1006. /*
  1007. ** Compute the set of tables that might satisfy cases 1 or 2.
  1008. */
  1009. indexable = ~(Bitmask)0;
  1010. chngToIN = ~( pWC.vmask );
  1011. for ( i = pOrWc.nTerm - 1; i >= 0 && indexable != 0; i-- )//, pOrTerm++ )
  1012. {
  1013. pOrTerm = pOrWc.a[i];
  1014. if ( ( pOrTerm.eOperator & WO_SINGLE ) == 0 )
  1015. {
  1016. WhereAndInfo pAndInfo;
  1017. Debug.Assert( pOrTerm.eOperator == 0 );
  1018. Debug.Assert( ( pOrTerm.wtFlags & ( TERM_ANDINFO | TERM_ORINFO ) ) == 0 );
  1019. chngToIN = 0;
  1020. pAndInfo = new WhereAndInfo();//sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
  1021. if ( pAndInfo != null )
  1022. {
  1023. WhereClause pAndWC;
  1024. WhereTerm pAndTerm;
  1025. int j;
  1026. Bitmask b = 0;
  1027. pOrTerm.u.pAndInfo = pAndInfo;
  1028. pOrTerm.wtFlags |= TERM_ANDINFO;
  1029. pOrTerm.eOperator = WO_AND;
  1030. pAndWC = pAndInfo.wc;
  1031. whereClauseInit( pAndWC, pWC.pParse, pMaskSet );
  1032. whereSplit( pAndWC, pOrTerm.pExpr, TK_AND );
  1033. exprAnalyzeAll( pSrc, pAndWC );
  1034. //testcase( db.mallocFailed );
  1035. ////if ( 0 == db.mallocFailed )
  1036. {
  1037. for ( j = 0; j < pAndWC.nTerm; j++ )//, pAndTerm++ )
  1038. {
  1039. pAndTerm = pAndWC.a[j];
  1040. Debug.Assert( pAndTerm.pExpr != null );
  1041. if ( allowedOp( pAndTerm.pExpr.op ) )
  1042. {
  1043. b |= getMask( pMaskSet, pAndTerm.leftCursor );
  1044. }
  1045. }
  1046. }
  1047. indexable &= b;
  1048. }
  1049. }
  1050. else if ( ( pOrTerm.wtFlags & TERM_COPIED ) != 0 )
  1051. {
  1052. /* Skip this term for now. We revisit it when we process the
  1053. ** corresponding TERM_VIRTUAL term */
  1054. }
  1055. else
  1056. {
  1057. Bitmask b;
  1058. b = getMask( pMaskSet, pOrTerm.leftCursor );
  1059. if ( ( pOrTerm.wtFlags & TERM_VIRTUAL ) != 0 )
  1060. {
  1061. WhereTerm pOther = pOrWc.a[pOrTerm.iParent];
  1062. b |= getMask( pMaskSet, pOther.leftCursor );
  1063. }
  1064. indexable &= b;
  1065. if ( pOrTerm.eOperator != WO_EQ )
  1066. {
  1067. chngToIN = 0;
  1068. }
  1069. else
  1070. {
  1071. chngToIN &= b;
  1072. }
  1073. }
  1074. }
  1075. /*
  1076. ** Record the set of tables that satisfy case 2. The set might be
  1077. ** empty.
  1078. */
  1079. pOrInfo.indexable = indexable;
  1080. pTerm.eOperator = (u16)( indexable == 0 ? 0 : WO_OR );
  1081. /*
  1082. ** chngToIN holds a set of tables that *might* satisfy case 1. But
  1083. ** we have to do some additional checking to see if case 1 really
  1084. ** is satisfied.
  1085. **
  1086. ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means
  1087. ** that there is no possibility of transforming the OR clause into an
  1088. ** IN operator because one or more terms in the OR clause contain
  1089. ** something other than == on a column in the single table. The 1-bit
  1090. ** case means that every term of the OR clause is of the form
  1091. ** "table.column=expr" for some single table. The one bit that is set
  1092. ** will correspond to the common table. We still need to check to make
  1093. ** sure the same column is used on all terms. The 2-bit case is when
  1094. ** the all terms are of the form "table1.column=table2.column". It
  1095. ** might be possible to form an IN operator with either table1.column
  1096. ** or table2.column as the LHS if either is common to every term of
  1097. ** the OR clause.
  1098. **
  1099. ** Note that terms of the form "table.column1=table.column2" (the
  1100. ** same table on both sizes of the ==) cannot be optimized.
  1101. */
  1102. if ( chngToIN != 0 )
  1103. {
  1104. int okToChngToIN = 0; /* True if the conversion to IN is valid */
  1105. int iColumn = -1; /* Column index on lhs of IN operator */
  1106. int iCursor = -1; /* Table cursor common to all terms */
  1107. int j = 0; /* Loop counter */
  1108. /* Search for a table and column that appears on one side or the
  1109. ** other of the == operator in every subterm. That table and column
  1110. ** will be recorded in iCursor and iColumn. There might not be any
  1111. ** such table and column. Set okToChngToIN if an appropriate table
  1112. ** and column is found but leave okToChngToIN false if not found.
  1113. */
  1114. for ( j = 0; j < 2 && 0 == okToChngToIN; j++ )
  1115. {
  1116. //pOrTerm = pOrWc.a;
  1117. for ( i = pOrWc.nTerm - 1; i >= 0; i-- )//, pOrTerm++)
  1118. {
  1119. pOrTerm = pOrWc.a[pOrWc.nTerm - 1 - i];
  1120. Debug.Assert( pOrTerm.eOperator == WO_EQ );
  1121. pOrTerm.wtFlags = (u8)( pOrTerm.wtFlags & ~TERM_OR_OK );
  1122. if ( pOrTerm.leftCursor == iCursor )
  1123. {
  1124. /* This is the 2-bit case and we are on the second iteration and
  1125. ** current term is from the first iteration. So skip this term. */
  1126. Debug.Assert( j == 1 );
  1127. continue;
  1128. }
  1129. if ( ( chngToIN & getMask( pMaskSet, pOrTerm.leftCursor ) ) == 0 )
  1130. {
  1131. /* This term must be of the form t1.a==t2.b where t2 is in the
  1132. ** chngToIN set but t1 is not. This term will be either preceeded
  1133. ** or follwed by an inverted copy (t2.b==t1.a). Skip this term
  1134. ** and use its inversion. */
  1135. testcase( pOrTerm.wtFlags & TERM_COPIED );
  1136. testcase( pOrTerm.wtFlags & TERM_VIRTUAL );
  1137. Debug.Assert( ( pOrTerm.wtFlags & ( TERM_COPIED | TERM_VIRTUAL ) ) != 0 );
  1138. continue;
  1139. }
  1140. iColumn = pOrTerm.u.leftColumn;
  1141. iCursor = pOrTerm.leftCursor;
  1142. break;
  1143. }
  1144. if ( i < 0 )
  1145. {
  1146. /* No candidate table+column was found. This can only occur
  1147. ** on the second iteration */
  1148. Debug.Assert( j == 1 );
  1149. Debug.Assert( ( chngToIN & ( chngToIN - 1 ) ) == 0 );
  1150. Debug.Assert( chngToIN == getMask( pMaskSet, iCursor ) );
  1151. break;
  1152. }
  1153. testcase( j == 1 );
  1154. /* We have found a candidate table and column. Check to see if that
  1155. ** table and column is common to every term in the OR clause */
  1156. okToChngToIN = 1;
  1157. for ( ; i >= 0 && okToChngToIN != 0; i-- )//, pOrTerm++)
  1158. {
  1159. pOrTerm = pOrWc.a[pOrWc.nTerm - 1 - i];
  1160. Debug.Assert( pOrTerm.eOperator == WO_EQ );
  1161. if ( pOrTerm.leftCursor != iCursor )
  1162. {
  1163. pOrTerm.wtFlags = (u8)( pOrTerm.wtFlags & ~TERM_OR_OK );
  1164. }
  1165. else if ( pOrTerm.u.leftColumn != iColumn )
  1166. {
  1167. okToChngToIN = 0;
  1168. }
  1169. else
  1170. {
  1171. int affLeft, affRight;
  1172. /* If the right-hand side is also a column, then the affinities
  1173. ** of both right and left sides must be such that no type
  1174. ** conversions are required on the right. (Ticket #2249)
  1175. */
  1176. affRight = sqlite3ExprAffinity( pOrTerm.pExpr.pRight );
  1177. affLeft = sqlite3ExprAffinity( pOrTerm.pExpr.pLeft );
  1178. if ( affRight != 0 && affRight != affLeft )
  1179. {
  1180. okToChngToIN = 0;
  1181. }
  1182. else
  1183. {
  1184. pOrTerm.wtFlags |= TERM_OR_OK;
  1185. }
  1186. }
  1187. }
  1188. }
  1189. /* At this point, okToChngToIN is true if original pTerm satisfies
  1190. ** case 1. In that case, construct a new virtual term that is
  1191. ** pTerm converted into an IN operator.
  1192. **
  1193. ** EV: R-00211-15100
  1194. */
  1195. if ( okToChngToIN != 0 )
  1196. {
  1197. Expr pDup; /* A transient duplicate expression */
  1198. ExprList pList = null; /* The RHS of the IN operator */
  1199. Expr pLeft = null; /* The LHS of the IN operator */
  1200. Expr pNew; /* The complete IN operator */
  1201. for ( i = pOrWc.nTerm - 1; i >= 0; i-- )//, pOrTerm++)
  1202. {
  1203. pOrTerm = pOrWc.a[pOrWc.nTerm - 1 - i];
  1204. if ( ( pOrTerm.wtFlags & TERM_OR_OK ) == 0 )
  1205. continue;
  1206. Debug.Assert( pOrTerm.eOperator == WO_EQ );
  1207. Debug.Assert( pOrTerm.leftCursor == iCursor );
  1208. Debug.Assert( pOrTerm.u.leftColumn ==

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