blob: 56646c55e6102b8b81d6b9daa86f6b9e83787c27 [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)
22/***/ int sqlite3WhereTrace = 0;
23#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 */
72 u8 iFrom; /* Which entry in the FROM clause */
73 u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */
74 int p1, p2; /* Operands of the opcode used to ends the loop */
75 union { /* Information that depends on pWLoop->wsFlags */
76 struct {
77 int nIn; /* Number of entries in aInLoop[] */
78 struct InLoop {
79 int iCur; /* The VDBE cursor used by this IN operator */
80 int addrInTop; /* Top of the IN loop */
81 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
82 } *aInLoop; /* Information about each nested IN operator */
83 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
84 Index *pCovidx; /* Possible covering index for WHERE_MULTI_OR */
85 } u;
86 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
87 Bitmask notReady; /* FROM entries not usable at this level */
88};
89
90/*
91** Each instance of this object represents an algorithm for evaluating one
92** term of a join. Every term of the FROM clause will have at least
93** one corresponding WhereLoop object (unless INDEXED BY constraints
94** prevent a query solution - which is an error) and many terms of the
95** FROM clause will have multiple WhereLoop objects, each describing a
96** potential way of implementing that FROM-clause term, together with
97** dependencies and cost estimates for using the chosen algorithm.
98**
99** Query planning consists of building up a collection of these WhereLoop
100** objects, then computing a particular sequence of WhereLoop objects, with
101** one WhereLoop object per FROM clause term, that satisfy all dependencies
102** and that minimize the overall cost.
103*/
104struct WhereLoop {
105 Bitmask prereq; /* Bitmask of other loops that must run first */
106 Bitmask maskSelf; /* Bitmask identifying table iTab */
107#ifdef SQLITE_DEBUG
108 char cId; /* Symbolic ID of this loop for debugging use */
109#endif
110 u8 iTab; /* Position in FROM clause of table for this loop */
111 u8 iSortIdx; /* Sorting index number. 0==None */
112 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
113 LogEst rRun; /* Cost of running each loop */
114 LogEst nOut; /* Estimated number of output rows */
115 union {
116 struct { /* Information for internal btree tables */
drh5e6790c2013-11-12 20:18:14 +0000117 u16 nEq; /* Number of equality constraints */
118 u16 nSkip; /* Number of initial index columns to skip */
drhe54df422013-11-12 18:37:25 +0000119 Index *pIndex; /* Index used, or NULL */
120 } btree;
121 struct { /* Information for virtual tables */
122 int idxNum; /* Index number */
123 u8 needFree; /* True if sqlite3_free(idxStr) is needed */
124 u8 isOrdered; /* True if satisfies ORDER BY */
125 u16 omitMask; /* Terms that may be omitted */
126 char *idxStr; /* Index identifier string */
127 } vtab;
128 } u;
129 u32 wsFlags; /* WHERE_* flags describing the plan */
130 u16 nLTerm; /* Number of entries in aLTerm[] */
131 /**** whereLoopXfer() copies fields above ***********************/
132# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
133 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
134 WhereTerm **aLTerm; /* WhereTerms used */
135 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
136 WhereTerm *aLTermSpace[4]; /* Initial aLTerm[] space */
137};
138
139/* This object holds the prerequisites and the cost of running a
140** subquery on one operand of an OR operator in the WHERE clause.
141** See WhereOrSet for additional information
142*/
143struct WhereOrCost {
144 Bitmask prereq; /* Prerequisites */
145 LogEst rRun; /* Cost of running this subquery */
146 LogEst nOut; /* Number of outputs for this subquery */
147};
148
149/* The WhereOrSet object holds a set of possible WhereOrCosts that
150** correspond to the subquery(s) of OR-clause processing. Only the
151** best N_OR_COST elements are retained.
152*/
153#define N_OR_COST 3
154struct WhereOrSet {
155 u16 n; /* Number of valid a[] entries */
156 WhereOrCost a[N_OR_COST]; /* Set of best costs */
157};
158
159
160/* Forward declaration of methods */
161static int whereLoopResize(sqlite3*, WhereLoop*, int);
162
163/*
164** Each instance of this object holds a sequence of WhereLoop objects
165** that implement some or all of a query plan.
166**
167** Think of each WhereLoop object as a node in a graph with arcs
168** showing dependencies and costs for travelling between nodes. (That is
169** not a completely accurate description because WhereLoop costs are a
170** vector, not a scalar, and because dependencies are many-to-one, not
171** one-to-one as are graph nodes. But it is a useful visualization aid.)
172** Then a WherePath object is a path through the graph that visits some
173** or all of the WhereLoop objects once.
174**
175** The "solver" works by creating the N best WherePath objects of length
176** 1. Then using those as a basis to compute the N best WherePath objects
177** of length 2. And so forth until the length of WherePaths equals the
178** number of nodes in the FROM clause. The best (lowest cost) WherePath
179** at the end is the choosen query plan.
180*/
181struct WherePath {
182 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
183 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
184 LogEst nRow; /* Estimated number of rows generated by this path */
185 LogEst rCost; /* Total cost of this path */
186 u8 isOrdered; /* True if this path satisfies ORDER BY */
187 u8 isOrderedValid; /* True if the isOrdered field is valid */
188 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
189};
190
191/*
192** The query generator uses an array of instances of this structure to
193** help it analyze the subexpressions of the WHERE clause. Each WHERE
194** clause subexpression is separated from the others by AND operators,
195** usually, or sometimes subexpressions separated by OR.
196**
197** All WhereTerms are collected into a single WhereClause structure.
198** The following identity holds:
199**
200** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
201**
202** When a term is of the form:
203**
204** X <op> <expr>
205**
206** where X is a column name and <op> is one of certain operators,
207** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
208** cursor number and column number for X. WhereTerm.eOperator records
209** the <op> using a bitmask encoding defined by WO_xxx below. The
210** use of a bitmask encoding for the operator allows us to search
211** quickly for terms that match any of several different operators.
212**
213** A WhereTerm might also be two or more subterms connected by OR:
214**
215** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
216**
217** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
218** and the WhereTerm.u.pOrInfo field points to auxiliary information that
219** is collected about the OR clause.
220**
221** If a term in the WHERE clause does not match either of the two previous
222** categories, then eOperator==0. The WhereTerm.pExpr field is still set
223** to the original subexpression content and wtFlags is set up appropriately
224** but no other fields in the WhereTerm object are meaningful.
225**
226** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
227** but they do so indirectly. A single WhereMaskSet structure translates
228** cursor number into bits and the translated bit is stored in the prereq
229** fields. The translation is used in order to maximize the number of
230** bits that will fit in a Bitmask. The VDBE cursor numbers might be
231** spread out over the non-negative integers. For example, the cursor
232** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
233** translates these sparse cursor numbers into consecutive integers
234** beginning with 0 in order to make the best possible use of the available
235** bits in the Bitmask. So, in the example above, the cursor numbers
236** would be mapped into integers 0 through 7.
237**
238** The number of terms in a join is limited by the number of bits
239** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
240** is only able to process joins with 64 or fewer tables.
241*/
242struct WhereTerm {
243 Expr *pExpr; /* Pointer to the subexpression that is this term */
244 int iParent; /* Disable pWC->a[iParent] when this term disabled */
245 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
246 union {
247 int leftColumn; /* Column number of X in "X <op> <expr>" */
248 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
249 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
250 } u;
251 LogEst truthProb; /* Probability of truth for this expression */
252 u16 eOperator; /* A WO_xx value describing <op> */
253 u8 wtFlags; /* TERM_xxx bit flags. See below */
254 u8 nChild; /* Number of children that must disable us */
255 WhereClause *pWC; /* The clause this term is part of */
256 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
257 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
258};
259
260/*
261** Allowed values of WhereTerm.wtFlags
262*/
263#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */
264#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */
265#define TERM_CODED 0x04 /* This term is already coded */
266#define TERM_COPIED 0x08 /* Has a child */
267#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
268#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
269#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
270#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
271# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
272#else
273# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
274#endif
275
276/*
277** An instance of the WhereScan object is used as an iterator for locating
278** terms in the WHERE clause that are useful to the query planner.
279*/
280struct WhereScan {
281 WhereClause *pOrigWC; /* Original, innermost WhereClause */
282 WhereClause *pWC; /* WhereClause currently being scanned */
283 char *zCollName; /* Required collating sequence, if not NULL */
284 char idxaff; /* Must match this affinity, if zCollName!=NULL */
285 unsigned char nEquiv; /* Number of entries in aEquiv[] */
286 unsigned char iEquiv; /* Next unused slot in aEquiv[] */
287 u32 opMask; /* Acceptable operators */
288 int k; /* Resume scanning at this->pWC->a[this->k] */
289 int aEquiv[22]; /* Cursor,Column pairs for equivalence classes */
290};
291
292/*
293** An instance of the following structure holds all information about a
294** WHERE clause. Mostly this is a container for one or more WhereTerms.
295**
296** Explanation of pOuter: For a WHERE clause of the form
297**
298** a AND ((b AND c) OR (d AND e)) AND f
299**
300** There are separate WhereClause objects for the whole clause and for
301** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
302** subclauses points to the WhereClause object for the whole clause.
303*/
304struct WhereClause {
305 WhereInfo *pWInfo; /* WHERE clause processing context */
306 WhereClause *pOuter; /* Outer conjunction */
307 u8 op; /* Split operator. TK_AND or TK_OR */
308 int nTerm; /* Number of terms */
309 int nSlot; /* Number of entries in a[] */
310 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
311#if defined(SQLITE_SMALL_STACK)
312 WhereTerm aStatic[1]; /* Initial static space for a[] */
313#else
314 WhereTerm aStatic[8]; /* Initial static space for a[] */
315#endif
316};
317
318/*
319** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
320** a dynamically allocated instance of the following structure.
321*/
322struct WhereOrInfo {
323 WhereClause wc; /* Decomposition into subterms */
324 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
325};
326
327/*
328** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
329** a dynamically allocated instance of the following structure.
330*/
331struct WhereAndInfo {
332 WhereClause wc; /* The subexpression broken out */
333};
334
335/*
336** An instance of the following structure keeps track of a mapping
337** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
338**
339** The VDBE cursor numbers are small integers contained in
340** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE
341** clause, the cursor numbers might not begin with 0 and they might
342** contain gaps in the numbering sequence. But we want to make maximum
343** use of the bits in our bitmasks. This structure provides a mapping
344** from the sparse cursor numbers into consecutive integers beginning
345** with 0.
346**
347** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
348** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
349**
350** For example, if the WHERE clause expression used these VDBE
351** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
352** would map those cursor numbers into bits 0 through 5.
353**
354** Note that the mapping is not necessarily ordered. In the example
355** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
356** 57->5, 73->4. Or one of 719 other combinations might be used. It
357** does not really matter. What is important is that sparse cursor
358** numbers all get mapped into bit numbers that begin with 0 and contain
359** no gaps.
360*/
361struct WhereMaskSet {
362 int n; /* Number of assigned cursor values */
363 int ix[BMS]; /* Cursor assigned to each bit */
364};
365
366/*
367** This object is a convenience wrapper holding all information needed
368** to construct WhereLoop objects for a particular query.
369*/
370struct WhereLoopBuilder {
371 WhereInfo *pWInfo; /* Information about this WHERE */
372 WhereClause *pWC; /* WHERE clause terms */
373 ExprList *pOrderBy; /* ORDER BY clause */
374 WhereLoop *pNew; /* Template WhereLoop */
375 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
376#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
377 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
378 int nRecValid; /* Number of valid fields currently in pRec */
379#endif
380};
381
382/*
383** The WHERE clause processing routine has two halves. The
384** first part does the start of the WHERE loop and the second
385** half does the tail of the WHERE loop. An instance of
386** this structure is returned by the first half and passed
387** into the second half to give some continuity.
388**
389** An instance of this object holds the complete state of the query
390** planner.
391*/
392struct WhereInfo {
393 Parse *pParse; /* Parsing and code generating context */
394 SrcList *pTabList; /* List of tables in the join */
395 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
396 ExprList *pResultSet; /* Result set. DISTINCT operates on these */
397 WhereLoop *pLoops; /* List of all WhereLoop objects */
398 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
399 LogEst nRowOut; /* Estimated number of output rows */
400 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
401 u8 bOBSat; /* ORDER BY satisfied by indices */
402 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
403 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
404 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
405 u8 nLevel; /* Number of nested loop */
406 int iTop; /* The very beginning of the WHERE loop */
407 int iContinue; /* Jump here to continue with next record */
408 int iBreak; /* Jump here to break out of the loop */
409 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
410 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
411 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
412 WhereClause sWC; /* Decomposition of the WHERE clause */
413 WhereLevel a[1]; /* Information about each nest loop in WHERE */
414};
415
416/*
417** Bitmasks for the operators on WhereTerm objects. These are all
418** operators that are of interest to the query planner. An
419** OR-ed combination of these values can be used when searching for
420** particular WhereTerms within a WhereClause.
421*/
422#define WO_IN 0x001
423#define WO_EQ 0x002
424#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
425#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
426#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
427#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
428#define WO_MATCH 0x040
429#define WO_ISNULL 0x080
430#define WO_OR 0x100 /* Two or more OR-connected terms */
431#define WO_AND 0x200 /* Two or more AND-connected terms */
432#define WO_EQUIV 0x400 /* Of the form A==B, both columns */
433#define WO_NOOP 0x800 /* This term does not restrict search space */
434
435#define WO_ALL 0xfff /* Mask of all possible WO_* values */
436#define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */
437
438/*
439** These are definitions of bits in the WhereLoop.wsFlags field.
440** The particular combination of bits in each WhereLoop help to
441** determine the algorithm that WhereLoop represents.
442*/
443#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
444#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
445#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
446#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
447#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
448#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
449#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
450#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
451#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
452#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
453#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
454#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
455#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
456#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
457#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
458#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
drh2e5ef4e2013-11-13 16:58:54 +0000459#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */