blob: 43da5c208932ce6196eb4a465b814dd4c97dfeb7 [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*/
mistachkind9bc6e82019-05-10 16:16:19 +000017#ifndef SQLITE_WHEREINT_H
18#define SQLITE_WHEREINT_H
drhe54df422013-11-12 18:37:25 +000019
drhe54df422013-11-12 18:37:25 +000020
21/* Forward references
22*/
23typedef struct WhereClause WhereClause;
24typedef struct WhereMaskSet WhereMaskSet;
25typedef struct WhereOrInfo WhereOrInfo;
26typedef struct WhereAndInfo WhereAndInfo;
27typedef struct WhereLevel WhereLevel;
28typedef struct WhereLoop WhereLoop;
29typedef struct WherePath WherePath;
30typedef struct WhereTerm WhereTerm;
31typedef struct WhereLoopBuilder WhereLoopBuilder;
32typedef struct WhereScan WhereScan;
33typedef struct WhereOrCost WhereOrCost;
34typedef struct WhereOrSet WhereOrSet;
drhf8bdcfa2022-04-09 03:06:01 +000035typedef struct WhereMemBlock WhereMemBlock;
drh7c1734b2022-04-09 12:27:20 +000036typedef struct WhereRightJoin WhereRightJoin;
drhf8bdcfa2022-04-09 03:06:01 +000037
38/*
39** This object is a header on a block of allocated memory that will be
40** automatically freed when its WInfo oject is destructed.
41*/
42struct WhereMemBlock {
43 WhereMemBlock *pNext; /* Next block in the chain */
drhcc212e42022-05-10 23:28:12 +000044 u64 sz; /* Bytes of space */
drhf8bdcfa2022-04-09 03:06:01 +000045};
drhe54df422013-11-12 18:37:25 +000046
47/*
drh7c1734b2022-04-09 12:27:20 +000048** Extra information attached to a WhereLevel that is a RIGHT JOIN.
49*/
50struct WhereRightJoin {
51 int iMatch; /* Cursor used to determine prior matched rows */
52 int regBloom; /* Bloom filter for iRJMatch */
53 int regReturn; /* Return register for the interior subroutine */
54 int addrSubrtn; /* Starting address for the interior subroutine */
drhb77c3122022-04-23 18:04:31 +000055 int endSubrtn; /* The last opcode in the interior subroutine */
drh7c1734b2022-04-09 12:27:20 +000056};
57
58/*
drhe54df422013-11-12 18:37:25 +000059** This object contains information needed to implement a single nested
60** loop in WHERE clause.
61**
62** Contrast this object with WhereLoop. This object describes the
63** implementation of the loop. WhereLoop describes the algorithm.
64** This object contains a pointer to the WhereLoop algorithm as one of
65** its elements.
66**
67** The WhereInfo object contains a single instance of this object for
68** each term in the FROM clause (which is to say, for each of the
69** nested loops as implemented). The order of WhereLevel objects determines
70** the loop nested order, with WhereInfo.a[0] being the outer loop and
71** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
72*/
73struct WhereLevel {
74 int iLeftJoin; /* Memory cell used to implement LEFT OUTER JOIN */
75 int iTabCur; /* The VDBE cursor used to access the table */
76 int iIdxCur; /* The VDBE cursor used to access pIdx */
77 int addrBrk; /* Jump here to break out of the loop */
78 int addrNxt; /* Jump here to start the next IN combination */
drhcd8629e2013-11-13 12:27:25 +000079 int addrSkip; /* Jump here for next iteration of skip-scan */
drhe54df422013-11-12 18:37:25 +000080 int addrCont; /* Jump here to continue with the next loop cycle */
81 int addrFirst; /* First instruction of interior of the loop */
82 int addrBody; /* Beginning of the body of this loop */
drhec3dda52019-08-23 13:32:03 +000083 int regBignull; /* big-null flag reg. True if a NULL-scan is needed */
dan15750a22019-08-16 21:07:19 +000084 int addrBignull; /* Jump here for next part of big-null scan */
drh41d2e662015-12-01 21:23:07 +000085#ifndef SQLITE_LIKE_DOESNT_MATCH_BLOBS
drh44aebff2016-05-02 10:25:42 +000086 u32 iLikeRepCntr; /* LIKE range processing counter register (times 2) */
drhf07cf6e2015-03-06 16:45:16 +000087 int addrLikeRep; /* LIKE range processing address */
drh41d2e662015-12-01 21:23:07 +000088#endif
drh2db144c2021-12-01 16:31:02 +000089 int regFilter; /* Bloom filter */
drh7c1734b2022-04-09 12:27:20 +000090 WhereRightJoin *pRJ; /* Extra information for RIGHT JOIN */
drhe54df422013-11-12 18:37:25 +000091 u8 iFrom; /* Which entry in the FROM clause */
drhe39a7322014-02-03 14:04:11 +000092 u8 op, p3, p5; /* Opcode, P3 & P5 of the opcode that ends the loop */
dan15750a22019-08-16 21:07:19 +000093 int p1, p2; /* Operands of the opcode used to end the loop */
drhe54df422013-11-12 18:37:25 +000094 union { /* Information that depends on pWLoop->wsFlags */
95 struct {
96 int nIn; /* Number of entries in aInLoop[] */
97 struct InLoop {
98 int iCur; /* The VDBE cursor used by this IN operator */
99 int addrInTop; /* Top of the IN loop */
drha0368d92018-05-30 00:54:23 +0000100 int iBase; /* Base register of multi-key index record */
101 int nPrefix; /* Number of prior entires in the key */
drhe54df422013-11-12 18:37:25 +0000102 u8 eEndLoopOp; /* IN Loop terminator. OP_Next or OP_Prev */
103 } *aInLoop; /* Information about each nested IN operator */
104 } in; /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
drh04756292021-10-14 19:28:28 +0000105 Index *pCoveringIdx; /* Possible covering index for WHERE_MULTI_OR */
drhe54df422013-11-12 18:37:25 +0000106 } u;
107 struct WhereLoop *pWLoop; /* The selected WhereLoop object */
108 Bitmask notReady; /* FROM entries not usable at this level */
dan6f9702e2014-11-01 20:38:06 +0000109#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
110 int addrVisit; /* Address at which row is visited */
111#endif
drhe54df422013-11-12 18:37:25 +0000112};
113
114/*
115** Each instance of this object represents an algorithm for evaluating one
116** term of a join. Every term of the FROM clause will have at least
117** one corresponding WhereLoop object (unless INDEXED BY constraints
118** prevent a query solution - which is an error) and many terms of the
119** FROM clause will have multiple WhereLoop objects, each describing a
120** potential way of implementing that FROM-clause term, together with
121** dependencies and cost estimates for using the chosen algorithm.
122**
123** Query planning consists of building up a collection of these WhereLoop
124** objects, then computing a particular sequence of WhereLoop objects, with
125** one WhereLoop object per FROM clause term, that satisfy all dependencies
126** and that minimize the overall cost.
127*/
128struct WhereLoop {
129 Bitmask prereq; /* Bitmask of other loops that must run first */
130 Bitmask maskSelf; /* Bitmask identifying table iTab */
131#ifdef SQLITE_DEBUG
132 char cId; /* Symbolic ID of this loop for debugging use */
133#endif
134 u8 iTab; /* Position in FROM clause of table for this loop */
135 u8 iSortIdx; /* Sorting index number. 0==None */
136 LogEst rSetup; /* One-time setup cost (ex: create transient index) */
137 LogEst rRun; /* Cost of running each loop */
138 LogEst nOut; /* Estimated number of output rows */
139 union {
140 struct { /* Information for internal btree tables */
drh5e6790c2013-11-12 20:18:14 +0000141 u16 nEq; /* Number of equality constraints */
dan71c57db2016-07-09 20:23:55 +0000142 u16 nBtm; /* Size of BTM vector */
143 u16 nTop; /* Size of TOP vector */
dana79a0e72019-07-29 14:42:56 +0000144 u16 nDistinctCol; /* Index columns used to sort for DISTINCT */
drhe54df422013-11-12 18:37:25 +0000145 Index *pIndex; /* Index used, or NULL */
146 } btree;
147 struct { /* Information for virtual tables */
148 int idxNum; /* Index number */
drh69b0ce32022-02-04 13:15:01 +0000149 u32 needFree : 1; /* True if sqlite3_free(idxStr) is needed */
150 u32 bOmitOffset : 1; /* True to let virtual table handle offset */
drh0401ace2014-03-18 15:30:27 +0000151 i8 isOrdered; /* True if satisfies ORDER BY */
drhe54df422013-11-12 18:37:25 +0000152 u16 omitMask; /* Terms that may be omitted */
153 char *idxStr; /* Index identifier string */
drh0fe7e7d2022-02-01 14:58:29 +0000154 u32 mHandleIn; /* Terms to handle as IN(...) instead of == */
drhe54df422013-11-12 18:37:25 +0000155 } vtab;
156 } u;
157 u32 wsFlags; /* WHERE_* flags describing the plan */
158 u16 nLTerm; /* Number of entries in aLTerm[] */
drhc8bbce12014-10-21 01:05:09 +0000159 u16 nSkip; /* Number of NULL aLTerm[] entries */
drhe54df422013-11-12 18:37:25 +0000160 /**** whereLoopXfer() copies fields above ***********************/
161# define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
162 u16 nLSlot; /* Number of slots allocated for aLTerm[] */
163 WhereTerm **aLTerm; /* WhereTerms used */
164 WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
drhc8bbce12014-10-21 01:05:09 +0000165 WhereTerm *aLTermSpace[3]; /* Initial aLTerm[] space */
drhe54df422013-11-12 18:37:25 +0000166};
167
168/* This object holds the prerequisites and the cost of running a
169** subquery on one operand of an OR operator in the WHERE clause.
170** See WhereOrSet for additional information
171*/
172struct WhereOrCost {
173 Bitmask prereq; /* Prerequisites */
174 LogEst rRun; /* Cost of running this subquery */
175 LogEst nOut; /* Number of outputs for this subquery */
176};
177
178/* The WhereOrSet object holds a set of possible WhereOrCosts that
179** correspond to the subquery(s) of OR-clause processing. Only the
180** best N_OR_COST elements are retained.
181*/
182#define N_OR_COST 3
183struct WhereOrSet {
184 u16 n; /* Number of valid a[] entries */
185 WhereOrCost a[N_OR_COST]; /* Set of best costs */
186};
187
drhe54df422013-11-12 18:37:25 +0000188/*
189** Each instance of this object holds a sequence of WhereLoop objects
190** that implement some or all of a query plan.
191**
192** Think of each WhereLoop object as a node in a graph with arcs
193** showing dependencies and costs for travelling between nodes. (That is
194** not a completely accurate description because WhereLoop costs are a
195** vector, not a scalar, and because dependencies are many-to-one, not
196** one-to-one as are graph nodes. But it is a useful visualization aid.)
197** Then a WherePath object is a path through the graph that visits some
198** or all of the WhereLoop objects once.
199**
200** The "solver" works by creating the N best WherePath objects of length
201** 1. Then using those as a basis to compute the N best WherePath objects
202** of length 2. And so forth until the length of WherePaths equals the
203** number of nodes in the FROM clause. The best (lowest cost) WherePath
peter.d.reid60ec9142014-09-06 16:39:46 +0000204** at the end is the chosen query plan.
drhe54df422013-11-12 18:37:25 +0000205*/
206struct WherePath {
207 Bitmask maskLoop; /* Bitmask of all WhereLoop objects in this path */
208 Bitmask revLoop; /* aLoop[]s that should be reversed for ORDER BY */
209 LogEst nRow; /* Estimated number of rows generated by this path */
210 LogEst rCost; /* Total cost of this path */
dan50ae31e2014-08-08 16:52:28 +0000211 LogEst rUnsorted; /* Total cost of this path ignoring sorting costs */
drh0401ace2014-03-18 15:30:27 +0000212 i8 isOrdered; /* No. of ORDER BY terms satisfied. -1 for unknown */
drhe54df422013-11-12 18:37:25 +0000213 WhereLoop **aLoop; /* Array of WhereLoop objects implementing this path */
214};
215
216/*
217** The query generator uses an array of instances of this structure to
218** help it analyze the subexpressions of the WHERE clause. Each WHERE
219** clause subexpression is separated from the others by AND operators,
220** usually, or sometimes subexpressions separated by OR.
221**
222** All WhereTerms are collected into a single WhereClause structure.
223** The following identity holds:
224**
225** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
226**
227** When a term is of the form:
228**
229** X <op> <expr>
230**
231** where X is a column name and <op> is one of certain operators,
232** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
233** cursor number and column number for X. WhereTerm.eOperator records
234** the <op> using a bitmask encoding defined by WO_xxx below. The
235** use of a bitmask encoding for the operator allows us to search
236** quickly for terms that match any of several different operators.
237**
238** A WhereTerm might also be two or more subterms connected by OR:
239**
240** (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
241**
242** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
243** and the WhereTerm.u.pOrInfo field points to auxiliary information that
244** is collected about the OR clause.
245**
246** If a term in the WHERE clause does not match either of the two previous
247** categories, then eOperator==0. The WhereTerm.pExpr field is still set
248** to the original subexpression content and wtFlags is set up appropriately
249** but no other fields in the WhereTerm object are meaningful.
250**
251** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
252** but they do so indirectly. A single WhereMaskSet structure translates
253** cursor number into bits and the translated bit is stored in the prereq
254** fields. The translation is used in order to maximize the number of
255** bits that will fit in a Bitmask. The VDBE cursor numbers might be
256** spread out over the non-negative integers. For example, the cursor
257** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet
258** translates these sparse cursor numbers into consecutive integers
259** beginning with 0 in order to make the best possible use of the available
260** bits in the Bitmask. So, in the example above, the cursor numbers
261** would be mapped into integers 0 through 7.
262**
263** The number of terms in a join is limited by the number of bits
264** in prereqRight and prereqAll. The default is 64 bits, hence SQLite
265** is only able to process joins with 64 or fewer tables.
266*/
267struct WhereTerm {
268 Expr *pExpr; /* Pointer to the subexpression that is this term */
drh87c05f02016-10-03 14:44:47 +0000269 WhereClause *pWC; /* The clause this term is part of */
270 LogEst truthProb; /* Probability of truth for this expression */
271 u16 wtFlags; /* TERM_xxx bit flags. See below */
272 u16 eOperator; /* A WO_xx value describing <op> */
273 u8 nChild; /* Number of children that must disable us */
274 u8 eMatchOp; /* Op for vtab MATCH/LIKE/GLOB/REGEXP terms */
drhe54df422013-11-12 18:37:25 +0000275 int iParent; /* Disable pWC->a[iParent] when this term disabled */
276 int leftCursor; /* Cursor number of X in "X <op> <expr>" */
277 union {
drh75fa2662020-09-28 15:49:43 +0000278 struct {
279 int leftColumn; /* Column number of X in "X <op> <expr>" */
280 int iField; /* Field in (?,?,?) IN (SELECT...) vector */
281 } x; /* Opcode other than OP_OR or OP_AND */
drhe54df422013-11-12 18:37:25 +0000282 WhereOrInfo *pOrInfo; /* Extra information if (eOperator & WO_OR)!=0 */
283 WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
284 } u;
drhe54df422013-11-12 18:37:25 +0000285 Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */
286 Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */
287};
288
289/*
290** Allowed values of WhereTerm.wtFlags
291*/
drh7d07a5f2020-01-16 11:51:09 +0000292#define TERM_DYNAMIC 0x0001 /* Need to call sqlite3ExprDelete(db, pExpr) */
293#define TERM_VIRTUAL 0x0002 /* Added by the optimizer. Do not code */
294#define TERM_CODED 0x0004 /* This term is already coded */
295#define TERM_COPIED 0x0008 /* Has a child */
296#define TERM_ORINFO 0x0010 /* Need to free the WhereTerm.u.pOrInfo object */
297#define TERM_ANDINFO 0x0020 /* Need to free the WhereTerm.u.pAndInfo obj */
drh8a95d3d2021-12-15 20:48:15 +0000298#define TERM_OK 0x0040 /* Used during OR-clause processing */
drh6cca0aa2021-01-21 17:54:41 +0000299#define TERM_VNULL 0x0080 /* Manufactured x>NULL or x<=NULL term */
drh7d07a5f2020-01-16 11:51:09 +0000300#define TERM_LIKEOPT 0x0100 /* Virtual terms from the LIKE optimization */
301#define TERM_LIKECOND 0x0200 /* Conditionally this LIKE operator term */
302#define TERM_LIKE 0x0400 /* The original LIKE operator */
303#define TERM_IS 0x0800 /* Term.pExpr is an IS operator */
dand3930b12017-07-10 15:17:30 +0000304#define TERM_VARSELECT 0x1000 /* Term.pExpr contains a correlated sub-query */
drh89efac92020-02-22 16:58:49 +0000305#define TERM_HEURTRUTH 0x2000 /* Heuristic truthProb used */
drhf06cdde2020-02-24 16:46:08 +0000306#ifdef SQLITE_ENABLE_STAT4
307# define TERM_HIGHTRUTH 0x4000 /* Term excludes few rows */
308#else
309# define TERM_HIGHTRUTH 0 /* Only used with STAT4 */
310#endif
drh02e3e042022-02-04 13:05:29 +0000311#define TERM_SLICE 0x8000 /* One slice of a row-value/vector comparison */
drhe54df422013-11-12 18:37:25 +0000312
313/*
314** An instance of the WhereScan object is used as an iterator for locating
315** terms in the WHERE clause that are useful to the query planner.
316*/
317struct WhereScan {
318 WhereClause *pOrigWC; /* Original, innermost WhereClause */
319 WhereClause *pWC; /* WhereClause currently being scanned */
drhf19aa5f2015-12-30 16:51:20 +0000320 const char *zCollName; /* Required collating sequence, if not NULL */
drh6860e6f2015-08-27 18:24:02 +0000321 Expr *pIdxExpr; /* Search for this index expression */
drhe54df422013-11-12 18:37:25 +0000322 int k; /* Resume scanning at this->pWC->a[this->k] */
drhdae2a102021-12-02 04:00:45 +0000323 u32 opMask; /* Acceptable operators */
324 char idxaff; /* Must match this affinity, if zCollName!=NULL */
325 unsigned char iEquiv; /* Current slot in aiCur[] and aiColumn[] */
326 unsigned char nEquiv; /* Number of entries in aiCur[] and aiColumn[] */
drha3f108e2015-08-26 21:08:04 +0000327 int aiCur[11]; /* Cursors in the equivalence class */
328 i16 aiColumn[11]; /* Corresponding column number in the eq-class */
drhe54df422013-11-12 18:37:25 +0000329};
330
331/*
332** An instance of the following structure holds all information about a
333** WHERE clause. Mostly this is a container for one or more WhereTerms.
334**
335** Explanation of pOuter: For a WHERE clause of the form
336**
337** a AND ((b AND c) OR (d AND e)) AND f
338**
339** There are separate WhereClause objects for the whole clause and for
340** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
341** subclauses points to the WhereClause object for the whole clause.
342*/
343struct WhereClause {
344 WhereInfo *pWInfo; /* WHERE clause processing context */
345 WhereClause *pOuter; /* Outer conjunction */
346 u8 op; /* Split operator. TK_AND or TK_OR */
drhda230bd2018-06-09 00:09:58 +0000347 u8 hasOr; /* True if any a[].eOperator is WO_OR */
drhe54df422013-11-12 18:37:25 +0000348 int nTerm; /* Number of terms */
349 int nSlot; /* Number of entries in a[] */
drh132f96f2021-12-08 16:07:22 +0000350 int nBase; /* Number of terms through the last non-Virtual */
drhe54df422013-11-12 18:37:25 +0000351 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
352#if defined(SQLITE_SMALL_STACK)
353 WhereTerm aStatic[1]; /* Initial static space for a[] */
354#else
355 WhereTerm aStatic[8]; /* Initial static space for a[] */
356#endif
357};
358
359/*
360** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
361** a dynamically allocated instance of the following structure.
362*/
363struct WhereOrInfo {
364 WhereClause wc; /* Decomposition into subterms */
365 Bitmask indexable; /* Bitmask of all indexable tables in the clause */
366};
367
368/*
369** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
370** a dynamically allocated instance of the following structure.
371*/
372struct WhereAndInfo {
373 WhereClause wc; /* The subexpression broken out */
374};
375
376/*
377** An instance of the following structure keeps track of a mapping
378** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
379**
380** The VDBE cursor numbers are small integers contained in
drh3b880652022-10-19 11:58:24 +0000381** SrcItem.iCursor and Expr.iTable fields. For any given WHERE
drhe54df422013-11-12 18:37:25 +0000382** clause, the cursor numbers might not begin with 0 and they might
383** contain gaps in the numbering sequence. But we want to make maximum
384** use of the bits in our bitmasks. This structure provides a mapping
385** from the sparse cursor numbers into consecutive integers beginning
386** with 0.
387**
388** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
389** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<<A.
390**
391** For example, if the WHERE clause expression used these VDBE
392** cursors: 4, 5, 8, 29, 57, 73. Then the WhereMaskSet structure
393** would map those cursor numbers into bits 0 through 5.
394**
395** Note that the mapping is not necessarily ordered. In the example
396** above, the mapping might go like this: 4->3, 5->1, 8->2, 29->0,
397** 57->5, 73->4. Or one of 719 other combinations might be used. It
398** does not really matter. What is important is that sparse cursor
399** numbers all get mapped into bit numbers that begin with 0 and contain
400** no gaps.
401*/
402struct WhereMaskSet {
dand3930b12017-07-10 15:17:30 +0000403 int bVarSelect; /* Used by sqlite3WhereExprUsage() */
drhe54df422013-11-12 18:37:25 +0000404 int n; /* Number of assigned cursor values */
405 int ix[BMS]; /* Cursor assigned to each bit */
406};
407
408/*
409** This object is a convenience wrapper holding all information needed
410** to construct WhereLoop objects for a particular query.
411*/
412struct WhereLoopBuilder {
413 WhereInfo *pWInfo; /* Information about this WHERE */
414 WhereClause *pWC; /* WHERE clause terms */
drhe54df422013-11-12 18:37:25 +0000415 WhereLoop *pNew; /* Template WhereLoop */
416 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
drh175b8f02019-08-08 15:24:17 +0000417#ifdef SQLITE_ENABLE_STAT4
drhe54df422013-11-12 18:37:25 +0000418 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
419 int nRecValid; /* Number of valid fields currently in pRec */
420#endif
drh89efac92020-02-22 16:58:49 +0000421 unsigned char bldFlags1; /* First set of SQLITE_BLDF_* flags */
422 unsigned char bldFlags2; /* Second set of SQLITE_BLDF_* flags */
drhfc9098a2018-09-21 18:43:51 +0000423 unsigned int iPlanLimit; /* Search limiter */
drhe54df422013-11-12 18:37:25 +0000424};
425
drha3928dd2017-02-17 15:26:36 +0000426/* Allowed values for WhereLoopBuider.bldFlags */
drh89efac92020-02-22 16:58:49 +0000427#define SQLITE_BLDF1_INDEXED 0x0001 /* An index is used */
428#define SQLITE_BLDF1_UNIQUE 0x0002 /* All keys of a UNIQUE index used */
429
430#define SQLITE_BLDF2_2NDPASS 0x0004 /* Second builder pass needed */
drha3928dd2017-02-17 15:26:36 +0000431
drh6fb5d352018-09-24 12:37:01 +0000432/* The WhereLoopBuilder.iPlanLimit is used to limit the number of
433** index+constraint combinations the query planner will consider for a
434** particular query. If this parameter is unlimited, then certain
435** pathological queries can spend excess time in the sqlite3WhereBegin()
436** routine. The limit is high enough that is should not impact real-world
437** queries.
438**
439** SQLITE_QUERY_PLANNER_LIMIT is the baseline limit. The limit is
440** increased by SQLITE_QUERY_PLANNER_LIMIT_INCR before each term of the FROM
441** clause is processed, so that every table in a join is guaranteed to be
442** able to propose a some index+constraint combinations even if the initial
443** baseline limit was exhausted by prior tables of the join.
444*/
445#ifndef SQLITE_QUERY_PLANNER_LIMIT
446# define SQLITE_QUERY_PLANNER_LIMIT 20000
447#endif
448#ifndef SQLITE_QUERY_PLANNER_LIMIT_INCR
449# define SQLITE_QUERY_PLANNER_LIMIT_INCR 1000
450#endif
451
drhe54df422013-11-12 18:37:25 +0000452/*
drh36e678b2020-01-02 00:45:38 +0000453** Each instance of this object records a change to a single node
454** in an expression tree to cause that node to point to a column
455** of an index rather than an expression or a virtual column. All
456** such transformations need to be undone at the end of WHERE clause
457** processing.
458*/
459typedef struct WhereExprMod WhereExprMod;
460struct WhereExprMod {
461 WhereExprMod *pNext; /* Next translation on a list of them all */
462 Expr *pExpr; /* The Expr node that was transformed */
463 Expr orig; /* Original value of the Expr node */
464};
465
466/*
drhe54df422013-11-12 18:37:25 +0000467** The WHERE clause processing routine has two halves. The
468** first part does the start of the WHERE loop and the second
469** half does the tail of the WHERE loop. An instance of
470** this structure is returned by the first half and passed
471** into the second half to give some continuity.
472**
473** An instance of this object holds the complete state of the query
474** planner.
475*/
476struct WhereInfo {
477 Parse *pParse; /* Parsing and code generating context */
478 SrcList *pTabList; /* List of tables in the join */
479 ExprList *pOrderBy; /* The ORDER BY clause or NULL */
drhe9ba9102017-02-16 20:52:52 +0000480 ExprList *pResultSet; /* Result set of the query */
drhaca19e12017-04-07 19:41:31 +0000481 Expr *pWhere; /* The complete WHERE clause */
drh895bab32022-01-27 16:14:50 +0000482#ifndef SQLITE_OMIT_VIRTUALTABLE
483 Select *pLimit; /* Used to access LIMIT expr/registers for vtabs */
484#endif
drh87c05f02016-10-03 14:44:47 +0000485 int aiCurOnePass[2]; /* OP_OpenWrite cursors for the ONEPASS opt */
486 int iContinue; /* Jump here to continue with next record */
487 int iBreak; /* Jump here to break out of the loop */
488 int savedNQueryLoop; /* pParse->nQueryLoop outside the WHERE loop */
drhe54df422013-11-12 18:37:25 +0000489 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
drh36e678b2020-01-02 00:45:38 +0000490 LogEst iLimit; /* LIMIT if wctrlFlags has WHERE_USE_LIMIT */
drh87c05f02016-10-03 14:44:47 +0000491 u8 nLevel; /* Number of nested loop */
drhddba0c22014-03-18 20:33:42 +0000492 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
drhb0264ee2015-09-14 14:45:50 +0000493 u8 eOnePass; /* ONEPASS_OFF, or _SINGLE, or _MULTI */
drha536df42016-05-19 22:13:37 +0000494 u8 eDistinct; /* One of the WHERE_DISTINCT_* values */
drh17aceeb2020-01-04 19:12:13 +0000495 unsigned bDeferredSeek :1; /* Uses OP_DeferredSeek */
496 unsigned untestedTerms :1; /* Not all WHERE terms resolved by outer loop */
497 unsigned bOrderedInnerLoop:1;/* True if only the inner-most loop is ordered */
498 unsigned sorted :1; /* True if really sorted (not just grouped) */
drh36e678b2020-01-02 00:45:38 +0000499 LogEst nRowOut; /* Estimated number of output rows */
drhe54df422013-11-12 18:37:25 +0000500 int iTop; /* The very beginning of the WHERE loop */
drh5e6d90f2020-08-14 17:39:31 +0000501 int iEndWhere; /* End of the WHERE clause itself */
drh87c05f02016-10-03 14:44:47 +0000502 WhereLoop *pLoops; /* List of all WhereLoop objects */
drh36e678b2020-01-02 00:45:38 +0000503 WhereExprMod *pExprMods; /* Expression modifications */
drhf8bdcfa2022-04-09 03:06:01 +0000504 WhereMemBlock *pMemToFree;/* Memory to free when this object destroyed */
drh87c05f02016-10-03 14:44:47 +0000505 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
drhe54df422013-11-12 18:37:25 +0000506 WhereClause sWC; /* Decomposition of the WHERE clause */
drh87c05f02016-10-03 14:44:47 +0000507 WhereMaskSet sMaskSet; /* Map cursor numbers to bitmasks */
drhe54df422013-11-12 18:37:25 +0000508 WhereLevel a[1]; /* Information about each nest loop in WHERE */
509};
510
511/*
drh6f82e852015-06-06 20:12:09 +0000512** Private interfaces - callable only by other where.c routines.
drh6c1f4ef2015-06-08 14:23:15 +0000513**
514** where.c:
drh6f82e852015-06-06 20:12:09 +0000515*/
516Bitmask sqlite3WhereGetMask(WhereMaskSet*,int);
drhc84a4022016-05-27 12:30:20 +0000517#ifdef WHERETRACE_ENABLED
518void sqlite3WhereClausePrint(WhereClause *pWC);
drhcacdf202019-12-28 13:39:47 +0000519void sqlite3WhereTermPrint(WhereTerm *pTerm, int iTerm);
520void sqlite3WhereLoopPrint(WhereLoop *p, WhereClause *pWC);
drhc84a4022016-05-27 12:30:20 +0000521#endif
drh6f82e852015-06-06 20:12:09 +0000522WhereTerm *sqlite3WhereFindTerm(
523 WhereClause *pWC, /* The WHERE clause to be searched */
524 int iCur, /* Cursor number of LHS */
525 int iColumn, /* Column number of LHS */
526 Bitmask notReady, /* RHS must not overlap with this mask */
527 u32 op, /* Mask of WO_xx values describing operator */
528 Index *pIdx /* Must be compatible with this index, if not NULL */
529);
drhf8bdcfa2022-04-09 03:06:01 +0000530void *sqlite3WhereMalloc(WhereInfo *pWInfo, u64 nByte);
531void *sqlite3WhereRealloc(WhereInfo *pWInfo, void *pOld, u64 nByte);
drh6c1f4ef2015-06-08 14:23:15 +0000532
533/* wherecode.c: */
drh6f82e852015-06-06 20:12:09 +0000534#ifndef SQLITE_OMIT_EXPLAIN
535int sqlite3WhereExplainOneScan(
536 Parse *pParse, /* Parse context */
537 SrcList *pTabList, /* Table list this loop refers to */
538 WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */
drh6f82e852015-06-06 20:12:09 +0000539 u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */
540);
drh6ae49e62021-12-05 20:19:47 +0000541int sqlite3WhereExplainBloomFilter(
542 const Parse *pParse, /* Parse context */
543 const WhereInfo *pWInfo, /* WHERE clause */
544 const WhereLevel *pLevel /* Bloom filter on this level */
545);
drh6f82e852015-06-06 20:12:09 +0000546#else
drhe2188f02018-05-07 11:37:34 +0000547# define sqlite3WhereExplainOneScan(u,v,w,x) 0
drh6ae49e62021-12-05 20:19:47 +0000548# define sqlite3WhereExplainBloomFilter(u,v,w) 0
drh6f82e852015-06-06 20:12:09 +0000549#endif /* SQLITE_OMIT_EXPLAIN */
550#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
551void sqlite3WhereAddScanStatus(
552 Vdbe *v, /* Vdbe to add scanstatus entry to */
553 SrcList *pSrclist, /* FROM clause pLvl reads data from */
554 WhereLevel *pLvl, /* Level to add scanstatus() entry for */
555 int addrExplain /* Address of OP_Explain (or 0) */
556);
557#else
558# define sqlite3WhereAddScanStatus(a, b, c, d) ((void)d)
559#endif
560Bitmask sqlite3WhereCodeOneLoopStart(
drh47df8a22018-12-25 00:15:37 +0000561 Parse *pParse, /* Parsing context */
562 Vdbe *v, /* Prepared statement under construction */
drh6f82e852015-06-06 20:12:09 +0000563 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
564 int iLevel, /* Which level of pWInfo->a[] should be coded */
drh47df8a22018-12-25 00:15:37 +0000565 WhereLevel *pLevel, /* The current level pointer */
drh6f82e852015-06-06 20:12:09 +0000566 Bitmask notReady /* Which tables are currently available */
567);
drh949e2ab2022-04-12 18:04:29 +0000568SQLITE_NOINLINE void sqlite3WhereRightJoinLoop(
569 WhereInfo *pWInfo,
570 int iLevel,
571 WhereLevel *pLevel
572);
drh6f82e852015-06-06 20:12:09 +0000573
drh6c1f4ef2015-06-08 14:23:15 +0000574/* whereexpr.c: */
575void sqlite3WhereClauseInit(WhereClause*,WhereInfo*);
576void sqlite3WhereClauseClear(WhereClause*);
577void sqlite3WhereSplit(WhereClause*,Expr*,u8);
drh895bab32022-01-27 16:14:50 +0000578void sqlite3WhereAddLimit(WhereClause*, Select*);
drh6c1f4ef2015-06-08 14:23:15 +0000579Bitmask sqlite3WhereExprUsage(WhereMaskSet*, Expr*);
drhccf6db52018-06-09 02:49:11 +0000580Bitmask sqlite3WhereExprUsageNN(WhereMaskSet*, Expr*);
drh6c1f4ef2015-06-08 14:23:15 +0000581Bitmask sqlite3WhereExprListUsage(WhereMaskSet*, ExprList*);
582void sqlite3WhereExprAnalyze(SrcList*, WhereClause*);
drh76012942021-02-21 21:04:54 +0000583void sqlite3WhereTabFuncArgs(Parse*, SrcItem*, WhereClause*);
drh6f82e852015-06-06 20:12:09 +0000584
585
586
587
588
589/*
drhe54df422013-11-12 18:37:25 +0000590** Bitmasks for the operators on WhereTerm objects. These are all
591** operators that are of interest to the query planner. An
592** OR-ed combination of these values can be used when searching for
593** particular WhereTerms within a WhereClause.
drh49711602016-04-14 16:40:13 +0000594**
595** Value constraints:
596** WO_EQ == SQLITE_INDEX_CONSTRAINT_EQ
597** WO_LT == SQLITE_INDEX_CONSTRAINT_LT
598** WO_LE == SQLITE_INDEX_CONSTRAINT_LE
599** WO_GT == SQLITE_INDEX_CONSTRAINT_GT
600** WO_GE == SQLITE_INDEX_CONSTRAINT_GE
drhe54df422013-11-12 18:37:25 +0000601*/
drhe8d0c612015-05-14 01:05:25 +0000602#define WO_IN 0x0001
603#define WO_EQ 0x0002
drhe54df422013-11-12 18:37:25 +0000604#define WO_LT (WO_EQ<<(TK_LT-TK_EQ))
605#define WO_LE (WO_EQ<<(TK_LE-TK_EQ))
606#define WO_GT (WO_EQ<<(TK_GT-TK_EQ))
607#define WO_GE (WO_EQ<<(TK_GE-TK_EQ))
drh303a69b2017-09-11 19:47:37 +0000608#define WO_AUX 0x0040 /* Op useful to virtual tables only */
drhe8d0c612015-05-14 01:05:25 +0000609#define WO_IS 0x0080
610#define WO_ISNULL 0x0100
611#define WO_OR 0x0200 /* Two or more OR-connected terms */
612#define WO_AND 0x0400 /* Two or more AND-connected terms */
613#define WO_EQUIV 0x0800 /* Of the form A==B, both columns */
614#define WO_NOOP 0x1000 /* This term does not restrict search space */
drh0f4b5342022-06-01 13:32:47 +0000615#define WO_ROWVAL 0x2000 /* A row-value term */
drhe54df422013-11-12 18:37:25 +0000616
drh0f4b5342022-06-01 13:32:47 +0000617#define WO_ALL 0x3fff /* Mask of all possible WO_* values */
drhe8d0c612015-05-14 01:05:25 +0000618#define WO_SINGLE 0x01ff /* Mask of all non-compound WO_* values */
drhe54df422013-11-12 18:37:25 +0000619
620/*
621** These are definitions of bits in the WhereLoop.wsFlags field.
622** The particular combination of bits in each WhereLoop help to
623** determine the algorithm that WhereLoop represents.
624*/
625#define WHERE_COLUMN_EQ 0x00000001 /* x=EXPR */
626#define WHERE_COLUMN_RANGE 0x00000002 /* x<EXPR and/or x>EXPR */
627#define WHERE_COLUMN_IN 0x00000004 /* x IN (...) */
628#define WHERE_COLUMN_NULL 0x00000008 /* x IS NULL */
629#define WHERE_CONSTRAINT 0x0000000f /* Any of the WHERE_COLUMN_xxx values */
630#define WHERE_TOP_LIMIT 0x00000010 /* x<EXPR or x<=EXPR constraint */
631#define WHERE_BTM_LIMIT 0x00000020 /* x>EXPR or x>=EXPR constraint */
632#define WHERE_BOTH_LIMIT 0x00000030 /* Both x>EXPR and x<EXPR */
633#define WHERE_IDX_ONLY 0x00000040 /* Use index only - omit table */
634#define WHERE_IPK 0x00000100 /* x is the INTEGER PRIMARY KEY */
635#define WHERE_INDEXED 0x00000200 /* WhereLoop.u.btree.pIndex is valid */
636#define WHERE_VIRTUALTABLE 0x00000400 /* WhereLoop.u.vtab is valid */
637#define WHERE_IN_ABLE 0x00000800 /* Able to support an IN operator */
638#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
639#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
640#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
drh2e5ef4e2013-11-13 16:58:54 +0000641#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
drhe39a7322014-02-03 14:04:11 +0000642#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
drh051575c2014-10-25 12:28:25 +0000643#define WHERE_PARTIALIDX 0x00020000 /* The automatic index is partial */
drhf7b0a5f2018-06-07 14:59:22 +0000644#define WHERE_IN_EARLYOUT 0x00040000 /* Perhaps quit IN loops early */
dan15750a22019-08-16 21:07:19 +0000645#define WHERE_BIGNULL_SORT 0x00080000 /* Column nEq of index is BIGNULL */
drh68cf0ac2020-09-28 19:51:54 +0000646#define WHERE_IN_SEEKSCAN 0x00100000 /* Seek-scan optimization for IN */
drh67656ac2021-05-04 23:21:35 +0000647#define WHERE_TRANSCONS 0x00200000 /* Uses a transitive constraint */
drhfa35f5c2021-12-04 13:43:57 +0000648#define WHERE_BLOOMFILTER 0x00400000 /* Consider using a Bloom-filter */
drh7e910f62021-12-09 01:28:15 +0000649#define WHERE_SELFCULL 0x00800000 /* nOut reduced by extra WHERE terms */
drh8f2c0b52022-01-27 21:18:14 +0000650#define WHERE_OMIT_OFFSET 0x01000000 /* Set offset counter to zero */
drha3fc6832022-09-01 10:29:02 +0000651#define WHERE_VIEWSCAN 0x02000000 /* A full-scan of a VIEW or subquery */
mistachkind9bc6e82019-05-10 16:16:19 +0000652
653#endif /* !defined(SQLITE_WHEREINT_H) */