blob: 5372f4fe13fa5f597858a6cdcd5d5f784fbb8c46 [file] [log] [blame]
drhe54df422013-11-12 18:37:25 +00001/*
2** 2013-11-12
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
13** This file contains structure and macro definitions for the query
14** planner logic in "where.c". These definitions are broken out into
15** a separate source file for easier editing.
16*/
17
18/*
19** Trace output macros
20*/
21#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
drh6f82e852015-06-06 20:12:09 +000022/***/ int sqlite3WhereTrace;
drhe54df422013-11-12 18:37:25 +000023#endif
24#if defined(SQLITE_DEBUG) \
25 && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
26# define WHERETRACE(K,X) if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
27# define WHERETRACE_ENABLED 1
28#else
29# define WHERETRACE(K,X)
30#endif
31
32/* Forward references
33*/
34typedef struct WhereClause WhereClause;
35typedef struct WhereMaskSet WhereMaskSet;
36typedef struct WhereOrInfo WhereOrInfo;
37typedef struct WhereAndInfo WhereAndInfo;
38typedef struct WhereLevel WhereLevel;
39typedef struct WhereLoop WhereLoop;
40typedef struct WherePath WherePath;
41typedef struct WhereTerm WhereTerm;
42typedef struct WhereLoopBuilder WhereLoopBuilder;
43typedef struct WhereScan WhereScan;
44typedef struct WhereOrCost WhereOrCost;
45typedef struct WhereOrSet WhereOrSet;
46
47/*
48** This object contains information needed to implement a single nested
49** loop in WHERE clause.
50**
51** Contrast this object with WhereLoop. This object describes the
52** implementation of the loop. WhereLoop describes the algorithm.
53** This object contains a pointer to the WhereLoop algorithm as one of
54** its elements.
55**
56** The WhereInfo object contains a single instance of this object for
57** each term in the FROM clause (which is to say, for each of the
58** nested loops as implemented). The order of WhereLevel objects determines
59** the loop nested order, with WhereInfo.a[0] being the outer loop and
60** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
61*/
62struct WhereLevel {
63 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
64 int iTabCur; /* The VDBE cursor used to access the table */
65 int iIdxCur; /* The VDBE cursor used to access pIdx */
66 int addrBrk; /* Jump here to break out of the loop */
67 int addrNxt; /* Jump here to start the next IN combination */
drhcd8629e2013-11-13 12:27:25 +000068 int addrSkip; /* Jump here for next iteration of skip-scan */
drhe54df422013-11-12 18:37:25 +000069 int addrCont; /* Jump here to continue with the next loop cycle */
70 int addrFirst; /* First instruction of interior of the loop */
71 int addrBody; /* Beginning of the body of this loop */
drhf07cf6e2015-03-06 16:45:16 +000072 int iLikeRepCntr; /* LIKE range processing counter register */
73 int addrLikeRep; /* LIKE range processing address */
drhe54df422013-11-12 18:37:25 +000074 u8 iFrom; /* Which entry in the FROM clause */
drhe39a7322014-02-03 14:04:11 +000075 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
drhe54df422013-11-12 18:37:25 +000076 int p1, p2; /* Operands of the opcode used to ends the loop */
77 union { /* Information that depends on pWLoop->wsFlags */
78 struct {
79 int nIn; /* Number of entries in aInLoop[] */
80 struct InLoop {
81 int iCur; /* The VDBE cursor used by this IN operator */
82 int addrInTop; /* Top of the IN loop */
83 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
84 } *aInLoop; /* Information about each nested IN operator */
85 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
86 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
87 } u;
88 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
89 Bitmask notReady; /* FROM entries not usable at this level */
dan6f9702e2014-11-01 20:38:06 +000090#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
91 int addrVisit; /* Address at which row is visited */
92#endif
drhe54df422013-11-12 18:37:25 +000093};
94
95/*
96** Each instance of this object represents an algorithm for evaluating one
97** term of a join. Every term of the FROM clause will have at least
98** one corresponding WhereLoop object (unless INDEXED BY constraints
99** prevent a query solution - which is an error) and many terms of the
100** FROM clause will have multiple WhereLoop objects, each describing a
101** potential way of implementing that FROM-clause term, together with
102** dependencies and cost estimates for using the chosen algorithm.
103**
104** Query planning consists of building up a collection of these WhereLoop
105** objects, then computing a particular sequence of WhereLoop objects, with
106** one WhereLoop object per FROM clause term, that satisfy all dependencies
107** and that minimize the overall cost.
108*/
109struct WhereLoop {
110 Bitmask prereq; /* Bitmask of other loops that must run first */
111 Bitmask maskSelf; /* Bitmask identifying table iTab */
112#ifdef SQLITE_DEBUG
113 char cId; /* Symbolic ID of this loop for debugging use */
114#endif
115 u8 iTab; /* Position in FROM clause of table for this loop */
116 u8 iSortIdx; /* Sorting index number. 0==None */
117 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
118 LogEst rRun; /* Cost of running each loop */
119 LogEst nOut; /* Estimated number of output rows */
120 union {
121 struct { /* Information for internal btree tables */
drh5e6790c2013-11-12 20:18:14 +0000122 u16 nEq; /* Number of equality constraints */
drhe54df422013-11-12 18:37:25 +0000123 Index *pIndex; /* Index used, or NULL */
124 } btree;
125 struct { /* Information for virtual tables */
126 int idxNum; /* Index number */
127 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
drh0401ace2014-03-18 15:30:27 +0000128 i8 isOrdered; /* True if satisfies ORDER BY */
drhe54df422013-11-12 18:37:25 +0000129 u16 omitMask; /* Terms that may be omitted */
130 char *idxStr; /* Index identifier string */
131 } vtab;
132 } u;
133 u32 wsFlags; /* WHERE_* flags describing the plan */
134 u16 nLTerm; /* Number of entries in aLTerm[] */
drhc8bbce12014-10-21 01:05:09 +0000135 u16 nSkip; /* Number of NULL aLTerm[] entries */
drhe54df422013-11-12 18:37:25 +0000136 /**** whereLoopXfer() copies fields above ***********************/
137# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
138 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
139 WhereTerm **aLTerm; /* WhereTerms used */
140 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
drhc8bbce12014-10-21 01:05:09 +0000141 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
drhe54df422013-11-12 18:37:25 +0000142};
143
144/* This object holds the prerequisites and the cost of running a
145** subquery on one operand of an OR operator in the WHERE clause.
146** See WhereOrSet for additional information
147*/
148struct WhereOrCost {
149 Bitmask prereq; /* Prerequisites */
150 LogEst rRun; /* Cost of running this subquery */
151 LogEst nOut; /* Number of outputs for this subquery */
152};
153
154/* The WhereOrSet object holds a set of possible WhereOrCosts that
155** correspond to the subquery(s) of OR-clause processing. Only the
156** best N_OR_COST elements are retained.
157*/
158#define N_OR_COST 3
159struct WhereOrSet {
160 u16 n; /* Number of valid a[] entries */
161 WhereOrCost a[N_OR_COST]; /* Set of best costs */
162};
163
drhe54df422013-11-12 18:37:25 +0000164/*
165** Each instance of this object holds a sequence of WhereLoop objects
166** that implement some or all of a query plan.
167**
168** Think of each WhereLoop object as a node in a graph with arcs
169** showing dependencies and costs for travelling between nodes. (That is
170** not a completely accurate description because WhereLoop costs are a
171** vector, not a scalar, and because dependencies are many-to-one, not
172** one-to-one as are graph nodes. But it is a useful visualization aid.)
173** Then a WherePath object is a path through the graph that visits some
174** or all of the WhereLoop objects once.
175**
176** The "solver" works by creating the N best WherePath objects of length
177** 1. Then using those as a basis to compute the N best WherePath objects
178** of length 2. And so forth until the length of WherePaths equals the
179** number of nodes in the FROM clause. The best (lowest cost) WherePath
peter.d.reid60ec9142014-09-06 16:39:46 +0000180** at the end is the chosen query plan.
drhe54df422013-11-12 18:37:25 +0000181*/
182struct WherePath {
183 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
184 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
185 LogEst nRow; /* Estimated number of rows generated by this path */
186 LogEst rCost; /* Total cost of this path */
dan50ae31e2014-08-08 16:52:28 +0000187 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
drh0401ace2014-03-18 15:30:27 +0000188 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
drhe54df422013-11-12 18:37:25 +0000189 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
190};
191
192/*
193** The query generator uses an array of instances of this structure to
194** help it analyze the subexpressions of the WHERE clause. Each WHERE
195** clause subexpression is separated from the others by AND operators,
196** usually, or sometimes subexpressions separated by OR.
197**
198** All WhereTerms are collected into a single WhereClause structure.
199** The following identity holds:
200**
201** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
202**
203** When a term is of the form:
204**
205** X <op> <expr>
206**
207** where X is a column name and <op> is one of certain operators,
208** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
209** cursor number and column number for X. WhereTerm.eOperator records
210** the <op> using a bitmask encoding defined by WO_xxx below. The
211** use of a bitmask encoding for the operator allows us to search
212** quickly for terms that match any of several different operators.
213**
214** A WhereTerm might also be two or more subterms connected by OR:
215**
216** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
217**
218** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
219** and the WhereTerm.u.pOrInfo field points to auxiliary information that
220** is collected about the OR clause.
221**
222** If a term in the WHERE clause does not match either of the two previous
223** categories, then eOperator==0. The WhereTerm.pExpr field is still set
224** to the original subexpression content and wtFlags is set up appropriately
225** but no other fields in the WhereTerm object are meaningful.
226**
227** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
228** but they do so indirectly. A single WhereMaskSet structure translates
229** cursor number into bits and the translated bit is stored in the prereq
230** fields. The translation is used in order to maximize the number of
231** bits that will fit in a Bitmask. The VDBE cursor numbers might be
232** spread out over the non-negative integers. For example, the cursor
233** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
234** translates these sparse cursor numbers into consecutive integers
235** beginning with 0 in order to make the best possible use of the available
236** bits in the Bitmask. So, in the example above, the cursor numbers
237** would be mapped into integers 0 through 7.
238**
239** The number of terms in a join is limited by the number of bits
240** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
241** is only able to process joins with 64 or fewer tables.
242*/
243struct WhereTerm {
244 Expr *pExpr; /* Pointer to the subexpression that is this term */
245 int iParent; /* Disable pWC->a[iParent] when this term disabled */
246 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
247 union {
248 int leftColumn; /* Column number of X in "X <op> <expr>" */
249 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
250 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
251 } u;
252 LogEst truthProb; /* Probability of truth for this expression */
253 u16 eOperator; /* A WO_xx value describing <op> */
drhf07cf6e2015-03-06 16:45:16 +0000254 u16 wtFlags; /* TERM_xxx bit flags. See below */
drhe54df422013-11-12 18:37:25 +0000255 u8 nChild; /* Number of children that must disable us */
256 WhereClause *pWC; /* The clause this term is part of */
257 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
258 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
259};
260
261/*
262** Allowed values of WhereTerm.wtFlags
263*/
264#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */
265#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
266#define TERM_CODED 0x04 /* This term is already coded */
267#define TERM_COPIED 0x08 /* Has a child */
268#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
269#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
270#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
271#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
272# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
273#else
274# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
275#endif
drh8f1a7ed2015-03-06 19:47:38 +0000276#define TERM_LIKEOPT 0x100 /* Virtual terms from the LIKE optimization */
277#define TERM_LIKECOND 0x200 /* Conditionally this LIKE operator term */
278#define TERM_LIKE 0x400 /* The original LIKE operator */
drh9be18702015-05-13 19:33:41 +0000279#define TERM_IS 0x800 /* Term.pExpr is an IS operator */
drhe54df422013-11-12 18:37:25 +0000280
281/*
282** An instance of the WhereScan object is used as an iterator for locating
283** terms in the WHERE clause that are useful to the query planner.
284*/
285struct WhereScan {
286 WhereClause *pOrigWC; /* Original, innermost WhereClause */
287 WhereClause *pWC; /* WhereClause currently being scanned */
288 char *zCollName; /* Required collating sequence, if not NULL */
drh6860e6f2015-08-27 18:24:02 +0000289 Expr *pIdxExpr; /* Search for this index expression */
drhe54df422013-11-12 18:37:25 +0000290 char idxaff; /* Must match this affinity, if zCollName!=NULL */
291 unsigned char nEquiv; /* Number of entries in aEquiv[] */
292 unsigned char iEquiv; /* Next unused slot in aEquiv[] */
293 u32 opMask; /* Acceptable operators */
294 int k; /* Resume scanning at this->pWC->a[this->k] */
drha3f108e2015-08-26 21:08:04 +0000295 int aiCur[11]; /* Cursors in the equivalence class */
296 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
drhe54df422013-11-12 18:37:25 +0000297};
298
299/*
300** An instance of the following structure holds all information about a
301** WHERE clause. Mostly this is a container for one or more WhereTerms.
302**
303** Explanation of pOuter: For a WHERE clause of the form
304**
305** a AND ((b AND c) OR (d AND e)) AND f
306**
307** There are separate WhereClause objects for the whole clause and for
308** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
309** subclauses points to the WhereClause object for the whole clause.
310*/
311struct WhereClause {
312 WhereInfo *pWInfo; /* WHERE clause processing context */
313 WhereClause *pOuter; /* Outer conjunction */
314 u8 op; /* Split operator. TK_AND or TK_OR */
315 int nTerm; /* Number of terms */
316 int nSlot; /* Number of entries in a[] */
317 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
318#if defined(SQLITE_SMALL_STACK)
319 WhereTerm aStatic[1]; /* Initial static space for a[] */
320#else
321 WhereTerm aStatic[8]; /* Initial static space for a[] */
322#endif
323};
324
325/*
326** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
327** a dynamically allocated instance of the following structure.
328*/
329struct WhereOrInfo {
330 WhereClause wc; /* Decomposition into subterms */
331 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
332};
333
334/*
335** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
336** a dynamically allocated instance of the following structure.
337*/
338struct WhereAndInfo {
339 WhereClause wc; /* The subexpression broken out */
340};
341
342/*
343** An instance of the following structure keeps track of a mapping
344** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
345**
346** The VDBE cursor numbers are small integers contained in
347** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
348** clause, the cursor numbers might not begin with 0 and they might
349** contain gaps in the numbering sequence. But we want to make maximum
350** use of the bits in our bitmasks. This structure provides a mapping
351** from the sparse cursor numbers into consecutive integers beginning
352** with 0.
353**
354** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
355** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
356**
357** For example, if the WHERE clause expression used these VDBE
358** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
359** would map those cursor numbers into bits 0 through 5.
360**
361** Note that the mapping is not necessarily ordered. In the example
362** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
363** 57->5, 73->4. Or one of 719 other combinations might be used. It
364** does not really matter. What is important is that sparse cursor
365** numbers all get mapped into bit numbers that begin with 0 and contain
366** no gaps.
367*/
368struct WhereMaskSet {
369 int n; /* Number of assigned cursor values */
370 int ix[BMS]; /* Cursor assigned to each bit */
371};
372
373/*
drh6c1f4ef2015-06-08 14:23:15 +0000374** Initialize a WhereMaskSet object
375*/
376#define initMaskSet(P) (P)->n=0
377
378/*
drhe54df422013-11-12 18:37:25 +0000379** This object is a convenience wrapper holding all information needed
380** to construct WhereLoop objects for a particular query.
381*/
382struct WhereLoopBuilder {
383 WhereInfo *pWInfo; /* Information about this WHERE */
384 WhereClause *pWC; /* WHERE clause terms */
385 ExprList *pOrderBy; /* ORDER BY clause */
386 WhereLoop *pNew; /* Template WhereLoop */
387 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
388#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
389 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
390 int nRecValid; /* Number of valid fields currently in pRec */
391#endif
392};
393
394/*
395** The WHERE clause processing routine has two halves. The
396** first part does the start of the WHERE loop and the second
397** half does the tail of the WHERE loop. An instance of
398** this structure is returned by the first half and passed
399** into the second half to give some continuity.
400**
401** An instance of this object holds the complete state of the query
402** planner.
403*/
404struct WhereInfo {
405 Parse *pParse; /* Parsing and code generating context */
406 SrcList *pTabList; /* List of tables in the join */
407 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
408 ExprList *pResultSet; /* Result set. DISTINCT operates on these */
409 WhereLoop *pLoops; /* List of all WhereLoop objects */
410 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
411 LogEst nRowOut; /* Estimated number of output rows */
412 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
drhddba0c22014-03-18 20:33:42 +0000413 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
dan374cd782014-04-21 13:21:56 +0000414 u8 sorted; /* True if really sorted (not just grouped) */
drhb0264ee2015-09-14 14:45:50 +0000415 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
drhe54df422013-11-12 18:37:25 +0000416 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
417 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
418 u8 nLevel; /* Number of nested loop */
419 int iTop; /* The very beginning of the WHERE loop */
420 int iContinue; /* Jump here to continue with next record */
421 int iBreak; /* Jump here to break out of the loop */
drh1f8bb502015-09-28 23:45:19 +0000422 int addrUseTabCur; /* Do not use index cursor after this address */
drhe54df422013-11-12 18:37:25 +0000423 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
424 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
425 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
426 WhereClause sWC; /* Decomposition of the WHERE clause */
427 WhereLevel a[1]; /* Information about each nest loop in WHERE */
428};
429
430/*
drh6f82e852015-06-06 20:12:09 +0000431** Private interfaces - callable only by other where.c routines.
drh6c1f4ef2015-06-08 14:23:15 +0000432**
433** where.c:
drh6f82e852015-06-06 20:12:09 +0000434*/
435Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
436WhereTerm *sqlite3WhereFindTerm(
437 WhereClause *pWC, /* The WHERE clause to be searched */
438 int iCur, /* Cursor number of LHS */
439 int iColumn, /* Column number of LHS */
440 Bitmask notReady, /* RHS must not overlap with this mask */
441 u32 op, /* Mask of WO_xx values describing operator */
442 Index *pIdx /* Must be compatible with this index, if not NULL */
443);
drh6c1f4ef2015-06-08 14:23:15 +0000444
445/* wherecode.c: */
drh6f82e852015-06-06 20:12:09 +0000446#ifndef SQLITE_OMIT_EXPLAIN
447int sqlite3WhereExplainOneScan(
448 Parse *pParse, /* Parse context */
449 SrcList *pTabList, /* Table list this loop refers to */
450 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
451 int iLevel, /* Value for "level" column of output */
452 int iFrom, /* Value for "from" column of output */
453 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
454);
455#else
456# define sqlite3WhereExplainOneScan(u,v,w,x,y,z) 0
457#endif /* SQLITE_OMIT_EXPLAIN */
458#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
459void sqlite3WhereAddScanStatus(
460 Vdbe *v, /* Vdbe to add scanstatus entry to */
461 SrcList *pSrclist, /* FROM clause pLvl reads data from */
462 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
463 int addrExplain /* Address of OP_Explain (or 0) */
464);
465#else
466# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
467#endif
468Bitmask sqlite3WhereCodeOneLoopStart(
469 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
470 int iLevel, /* Which level of pWInfo->a[] should be coded */
471 Bitmask notReady /* Which tables are currently available */
472);
473
drh6c1f4ef2015-06-08 14:23:15 +0000474/* whereexpr.c: */
475void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
476void sqlite3WhereClauseClear(WhereClause*);
477void sqlite3WhereSplit(WhereClause*,Expr*,u8);
478Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
479Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
480void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
drh01d230c2015-08-19 17:11:37 +0000481void sqlite3WhereTabFuncArgs(Parse*, struct SrcList_item*, WhereClause*);
drh6f82e852015-06-06 20:12:09 +0000482
483
484
485
486
487/*
drhe54df422013-11-12 18:37:25 +0000488** Bitmasks for the operators on WhereTerm objects. These are all
489** operators that are of interest to the query planner. An
490** OR-ed combination of these values can be used when searching for
491** particular WhereTerms within a WhereClause.
492*/
drhe8d0c612015-05-14 01:05:25 +0000493#define WO_IN 0x0001
494#define WO_EQ 0x0002
drhe54df422013-11-12 18:37:25 +0000495#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
496#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
497#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
498#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
drhe8d0c612015-05-14 01:05:25 +0000499#define WO_MATCH 0x0040
500#define WO_IS 0x0080
501#define WO_ISNULL 0x0100
502#define WO_OR 0x0200 /* Two or more OR-connected terms */
503#define WO_AND 0x0400 /* Two or more AND-connected terms */
504#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
505#define WO_NOOP 0x1000 /* This term does not restrict search space */
drhe54df422013-11-12 18:37:25 +0000506
drhe8d0c612015-05-14 01:05:25 +0000507#define WO_ALL 0x1fff /* Mask of all possible WO_* values */
508#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
drhe54df422013-11-12 18:37:25 +0000509
510/*
511** These are definitions of bits in the WhereLoop.wsFlags field.
512** The particular combination of bits in each WhereLoop help to
513** determine the algorithm that WhereLoop represents.
514*/
515#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
516#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
517#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
518#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
519#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
520#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
521#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
522#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
523#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
524#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
525#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
526#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
527#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
528#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
529#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
530#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
drh2e5ef4e2013-11-13 16:58:54 +0000531#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
drhe39a7322014-02-03 14:04:11 +0000532#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
drh051575c2014-10-25 12:28:25 +0000533#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */