0000: 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 /*.** 2001 Septe
0010: 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 mber 15.**.** Th
0020: 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 e author disclai
0030: 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 ms copyright to
0040: 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 this source code
0050: 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a . In place of.*
0060: 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 * a legal notice
0070: 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 , here is a bles
0080: 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d sing:.**.** M
0090: 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 ay you do good a
00a0: 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 nd not evil..**
00b0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 May you find
00c0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 forgiveness for
00d0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 yourself and for
00e0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 give others..**
00f0: 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 May you share
0100: 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 freely, never t
0110: 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 aking more than
0120: 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a you give..**.***
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0160: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0170: 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d ******.** This m
0180: 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 odule contains C
0190: 20 63 6f 64 65 20 74 68 61 74 20 67 65 6e 65 72 code that gener
01a0: 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 75 ates VDBE code u
01b0: 73 65 64 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a sed to process.*
01c0: 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 * the WHERE clau
01d0: 73 65 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d se of SQL statem
01e0: 65 6e 74 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 ents. This modu
01f0: 6c 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c le is responsibl
0200: 65 20 66 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 e for.** generat
0210: 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 ing the code tha
0220: 74 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 t loops through
0230: 61 20 74 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 a table looking
0240: 66 6f 72 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a for applicable.*
0250: 2a 20 72 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 * rows. Indices
0260: 20 61 72 65 20 73 65 6c 65 63 74 65 64 20 61 6e are selected an
0270: 64 20 75 73 65 64 20 74 6f 20 73 70 65 65 64 20 d used to speed
0280: 74 68 65 20 73 65 61 72 63 68 20 77 68 65 6e 20 the search when
0290: 64 6f 69 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 doing.** so is a
02a0: 70 70 6c 69 63 61 62 6c 65 2e 20 20 42 65 63 61 pplicable. Beca
02b0: 75 73 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 use this module
02c0: 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 is responsible f
02d0: 6f 72 20 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 or selecting.**
02e0: 69 6e 64 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 indices, you mig
02f0: 68 74 20 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 ht also think of
0300: 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 this module as
0310: 74 68 65 20 22 71 75 65 72 79 20 6f 70 74 69 6d the "query optim
0320: 69 7a 65 72 22 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 izer"..*/.#inclu
0330: 64 65 20 22 73 71 6c 69 74 65 49 6e 74 2e 68 22 de "sqliteInt.h"
0340: 0a 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f .../*.** Trace o
0350: 75 74 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a utput macros.*/.
0360: 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 #if defined(SQLI
0370: 54 45 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 TE_TEST) || defi
0380: 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 ned(SQLITE_DEBUG
0390: 29 0a 2f 2a 2a 2a 2f 20 69 6e 74 20 73 71 6c 69 )./***/ int sqli
03a0: 74 65 33 57 68 65 72 65 54 72 61 63 65 20 3d 20 te3WhereTrace =
03b0: 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 0;.#endif.#if de
03c0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 fined(SQLITE_DEB
03d0: 55 47 29 20 5c 0a 20 20 20 20 26 26 20 28 64 65 UG) \. && (de
03e0: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 fined(SQLITE_TES
03f0: 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 T) || defined(SQ
0400: 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 57 48 45 52 LITE_ENABLE_WHER
0410: 45 54 52 41 43 45 29 29 0a 23 20 64 65 66 69 6e ETRACE)).# defin
0420: 65 20 57 48 45 52 45 54 52 41 43 45 28 4b 2c 58 e WHERETRACE(K,X
0430: 29 20 20 69 66 28 73 71 6c 69 74 65 33 57 68 65 ) if(sqlite3Whe
0440: 72 65 54 72 61 63 65 26 28 4b 29 29 20 73 71 6c reTrace&(K)) sql
0450: 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 ite3DebugPrintf
0460: 58 0a 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 X.# define WHERE
0470: 54 52 41 43 45 5f 45 4e 41 42 4c 45 44 20 31 0a TRACE_ENABLED 1.
0480: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 57 #else.# define W
0490: 48 45 52 45 54 52 41 43 45 28 4b 2c 58 29 0a 23 HERETRACE(K,X).#
04a0: 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77 61 72 endif../* Forwar
04b0: 64 20 72 65 66 65 72 65 6e 63 65 73 0a 2a 2f 0a d references.*/.
04c0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 typedef struct W
04d0: 68 65 72 65 43 6c 61 75 73 65 20 57 68 65 72 65 hereClause Where
04e0: 43 6c 61 75 73 65 3b 0a 74 79 70 65 64 65 66 20 Clause;.typedef
04f0: 73 74 72 75 63 74 20 57 68 65 72 65 4d 61 73 6b struct WhereMask
0500: 53 65 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74 Set WhereMaskSet
0510: 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
0520: 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 57 68 65 WhereOrInfo Whe
0530: 72 65 4f 72 49 6e 66 6f 3b 0a 74 79 70 65 64 65 reOrInfo;.typede
0540: 66 20 73 74 72 75 63 74 20 57 68 65 72 65 41 6e f struct WhereAn
0550: 64 49 6e 66 6f 20 57 68 65 72 65 41 6e 64 49 6e dInfo WhereAndIn
0560: 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 fo;.typedef stru
0570: 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 57 68 ct WhereLevel Wh
0580: 65 72 65 4c 65 76 65 6c 3b 0a 74 79 70 65 64 65 ereLevel;.typede
0590: 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4c 6f f struct WhereLo
05a0: 6f 70 20 57 68 65 72 65 4c 6f 6f 70 3b 0a 74 79 op WhereLoop;.ty
05b0: 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 pedef struct Whe
05c0: 72 65 50 61 74 68 20 57 68 65 72 65 50 61 74 68 rePath WherePath
05d0: 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 ;.typedef struct
05e0: 20 57 68 65 72 65 54 65 72 6d 20 57 68 65 72 65 WhereTerm Where
05f0: 54 65 72 6d 3b 0a 74 79 70 65 64 65 66 20 73 74 Term;.typedef st
0600: 72 75 63 74 20 57 68 65 72 65 4c 6f 6f 70 42 75 ruct WhereLoopBu
0610: 69 6c 64 65 72 20 57 68 65 72 65 4c 6f 6f 70 42 ilder WhereLoopB
0620: 75 69 6c 64 65 72 3b 0a 74 79 70 65 64 65 66 20 uilder;.typedef
0630: 73 74 72 75 63 74 20 57 68 65 72 65 53 63 61 6e struct WhereScan
0640: 20 57 68 65 72 65 53 63 61 6e 3b 0a 74 79 70 65 WhereScan;.type
0650: 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 def struct Where
0660: 4f 72 43 6f 73 74 20 57 68 65 72 65 4f 72 43 6f OrCost WhereOrCo
0670: 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 st;.typedef stru
0680: 63 74 20 57 68 65 72 65 4f 72 53 65 74 20 57 68 ct WhereOrSet Wh
0690: 65 72 65 4f 72 53 65 74 3b 0a 0a 2f 2a 0a 2a 2a ereOrSet;../*.**
06a0: 20 54 68 69 73 20 6f 62 6a 65 63 74 20 63 6f 6e This object con
06b0: 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f tains informatio
06c0: 6e 20 6e 65 65 64 65 64 20 74 6f 20 69 6d 70 6c n needed to impl
06d0: 65 6d 65 6e 74 20 61 20 73 69 6e 67 6c 65 20 6e ement a single n
06e0: 65 73 74 65 64 0a 2a 2a 20 6c 6f 6f 70 20 69 6e ested.** loop in
06f0: 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a WHERE clause..*
0700: 2a 0a 2a 2a 20 43 6f 6e 74 72 61 73 74 20 74 68 *.** Contrast th
0710: 69 73 20 6f 62 6a 65 63 74 20 77 69 74 68 20 57 is object with W
0720: 68 65 72 65 4c 6f 6f 70 2e 20 20 54 68 69 73 20 hereLoop. This
0730: 6f 62 6a 65 63 74 20 64 65 73 63 72 69 62 65 73 object describes
0740: 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e the.** implemen
0750: 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f tation of the lo
0760: 6f 70 2e 20 20 57 68 65 72 65 4c 6f 6f 70 20 64 op. WhereLoop d
0770: 65 73 63 72 69 62 65 73 20 74 68 65 20 61 6c 67 escribes the alg
0780: 6f 72 69 74 68 6d 2e 0a 2a 2a 20 54 68 69 73 20 orithm..** This
0790: 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20 object contains
07a0: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
07b0: 20 57 68 65 72 65 4c 6f 6f 70 20 61 6c 67 6f 72 WhereLoop algor
07c0: 69 74 68 6d 20 61 73 20 6f 6e 65 20 6f 66 0a 2a ithm as one of.*
07d0: 2a 20 69 74 73 20 65 6c 65 6d 65 6e 74 73 2e 0a * its elements..
07e0: 2a 2a 0a 2a 2a 20 54 68 65 20 57 68 65 72 65 49 **.** The WhereI
07f0: 6e 66 6f 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 nfo object conta
0800: 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 ins a single ins
0810: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 tance of this ob
0820: 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65 61 63 68 ject for.** each
0830: 20 74 65 72 6d 20 69 6e 20 74 68 65 20 46 52 4f term in the FRO
0840: 4d 20 63 6c 61 75 73 65 20 28 77 68 69 63 68 20 M clause (which
0850: 69 73 20 74 6f 20 73 61 79 2c 20 66 6f 72 20 65 is to say, for e
0860: 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 ach of the.** ne
0870: 73 74 65 64 20 6c 6f 6f 70 73 20 61 73 20 69 6d sted loops as im
0880: 70 6c 65 6d 65 6e 74 65 64 29 2e 20 20 54 68 65 plemented). The
0890: 20 6f 72 64 65 72 20 6f 66 20 57 68 65 72 65 4c order of WhereL
08a0: 65 76 65 6c 20 6f 62 6a 65 63 74 73 20 64 65 74 evel objects det
08b0: 65 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6c ermines.** the l
08c0: 6f 6f 70 20 6e 65 73 74 65 64 20 6f 72 64 65 72 oop nested order
08d0: 2c 20 77 69 74 68 20 57 68 65 72 65 49 6e 66 6f , with WhereInfo
08e0: 2e 61 5b 30 5d 20 62 65 69 6e 67 20 74 68 65 20 .a[0] being the
08f0: 6f 75 74 65 72 20 6c 6f 6f 70 20 61 6e 64 0a 2a outer loop and.*
0900: 2a 20 57 68 65 72 65 49 6e 66 6f 2e 61 5b 57 68 * WhereInfo.a[Wh
0910: 65 72 65 49 6e 66 6f 2e 6e 4c 65 76 65 6c 2d 31 ereInfo.nLevel-1
0920: 5d 20 62 65 69 6e 67 20 74 68 65 20 69 6e 6e 65 ] being the inne
0930: 72 20 6c 6f 6f 70 2e 0a 2a 2f 0a 73 74 72 75 63 r loop..*/.struc
0940: 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a 20 t WhereLevel {.
0950: 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20 int iLeftJoin;
0960: 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 /* Memory
0970: 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d cell used to im
0980: 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54 plement LEFT OUT
0990: 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 69 6e 74 ER JOIN */. int
09a0: 20 69 54 61 62 43 75 72 3b 20 20 20 20 20 20 20 iTabCur;
09b0: 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 /* The VDBE c
09c0: 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 ursor used to ac
09d0: 63 65 73 73 20 74 68 65 20 74 61 62 6c 65 20 2a cess the table *
09e0: 2f 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b /. int iIdxCur;
09f0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
0a00: 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65 VDBE cursor use
0a10: 64 20 74 6f 20 61 63 63 65 73 73 20 70 49 64 78 d to access pIdx
0a20: 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 72 */. int addrBr
0a30: 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a k; /* J
0a40: 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 ump here to brea
0a50: 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f k out of the loo
0a60: 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e p */. int addrN
0a70: 78 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 xt; /*
0a80: 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 73 74 61 Jump here to sta
0a90: 72 74 20 74 68 65 20 6e 65 78 74 20 49 4e 20 63 rt the next IN c
0aa0: 6f 6d 62 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 ombination */.
0ab0: 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 int addrCont;
0ac0: 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 /* Jump he
0ad0: 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 re to continue w
0ae0: 69 74 68 20 74 68 65 20 6e 65 78 74 20 6c 6f 6f ith the next loo
0af0: 70 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 p cycle */. int
0b00: 20 61 64 64 72 46 69 72 73 74 3b 20 20 20 20 20 addrFirst;
0b10: 20 20 20 2f 2a 20 46 69 72 73 74 20 69 6e 73 74 /* First inst
0b20: 72 75 63 74 69 6f 6e 20 6f 66 20 69 6e 74 65 72 ruction of inter
0b30: 69 6f 72 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 ior of the loop
0b40: 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 42 6f 64 */. int addrBod
0b50: 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 y; /* Be
0b60: 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 62 ginning of the b
0b70: 6f 64 79 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 ody of this loop
0b80: 20 2a 2f 0a 20 20 75 38 20 69 46 72 6f 6d 3b 20 */. u8 iFrom;
0b90: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 /* W
0ba0: 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 hich entry in th
0bb0: 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f e FROM clause */
0bc0: 0a 20 20 75 38 20 6f 70 2c 20 70 35 3b 20 20 20 . u8 op, p5;
0bd0: 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f /* Opco
0be0: 64 65 20 61 6e 64 20 50 35 20 6f 66 20 74 68 65 de and P5 of the
0bf0: 20 6f 70 63 6f 64 65 20 74 68 61 74 20 65 6e 64 opcode that end
0c00: 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 s the loop */.
0c10: 69 6e 74 20 70 31 2c 20 70 32 3b 20 20 20 20 20 int p1, p2;
0c20: 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 6e 64 /* Operand
0c30: 73 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 s of the opcode
0c40: 75 73 65 64 20 74 6f 20 65 6e 64 73 20 74 68 65 used to ends the
0c50: 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 6e 69 6f 6e loop */. union
0c60: 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 {
0c70: 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
0c80: 74 68 61 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 that depends on
0c90: 70 57 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 pWLoop->wsFlags
0ca0: 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a */. struct {.
0cb0: 20 20 20 20 20 20 69 6e 74 20 6e 49 6e 3b 20 20 int nIn;
0cc0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
0cd0: 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
0ce0: 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b 5d 20 2a 2f in aInLoop[] */
0cf0: 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 49 6e . struct In
0d00: 4c 6f 6f 70 20 7b 0a 20 20 20 20 20 20 20 20 69 Loop {. i
0d10: 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 nt iCur;
0d20: 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 /* The VDB
0d30: 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 62 79 E cursor used by
0d40: 20 74 68 69 73 20 49 4e 20 6f 70 65 72 61 74 6f this IN operato
0d50: 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 r */. int
0d60: 20 61 64 64 72 49 6e 54 6f 70 3b 20 20 20 20 20 addrInTop;
0d70: 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 /* Top of th
0d80: 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 e IN loop */.
0d90: 20 20 20 20 20 75 38 20 65 45 6e 64 4c 6f 6f 70 u8 eEndLoop
0da0: 4f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 Op; /* I
0db0: 4e 20 4c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 6f N Loop terminato
0dc0: 72 2e 20 4f 50 5f 4e 65 78 74 20 6f 72 20 4f 50 r. OP_Next or OP
0dd0: 5f 50 72 65 76 20 2a 2f 0a 20 20 20 20 20 20 7d _Prev */. }
0de0: 20 2a 61 49 6e 4c 6f 6f 70 3b 20 20 20 20 20 20 *aInLoop;
0df0: 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 /* Informat
0e00: 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e ion about each n
0e10: 65 73 74 65 64 20 49 4e 20 6f 70 65 72 61 74 6f ested IN operato
0e20: 72 20 2a 2f 0a 20 20 20 20 7d 20 69 6e 3b 20 20 r */. } in;
0e30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
0e40: 2a 20 55 73 65 64 20 77 68 65 6e 20 70 57 4c 6f * Used when pWLo
0e50: 6f 70 2d 3e 77 73 46 6c 61 67 73 26 57 48 45 52 op->wsFlags&WHER
0e60: 45 5f 49 4e 5f 41 42 4c 45 20 2a 2f 0a 20 20 20 E_IN_ABLE */.
0e70: 20 49 6e 64 65 78 20 2a 70 43 6f 76 69 64 78 3b Index *pCovidx;
0e80: 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 /* Possib
0e90: 6c 65 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 le covering inde
0ea0: 78 20 66 6f 72 20 57 48 45 52 45 5f 4d 55 4c 54 x for WHERE_MULT
0eb0: 49 5f 4f 52 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 I_OR */. } u;.
0ec0: 20 73 74 72 75 63 74 20 57 68 65 72 65 4c 6f 6f struct WhereLoo
0ed0: 70 20 2a 70 57 4c 6f 6f 70 3b 20 20 2f 2a 20 54 p *pWLoop; /* T
0ee0: 68 65 20 73 65 6c 65 63 74 65 64 20 57 68 65 72 he selected Wher
0ef0: 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 20 2a 2f 0a eLoop object */.
0f00: 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 Bitmask notRea
0f10: 64 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 dy; /*
0f20: 46 52 4f 4d 20 65 6e 74 72 69 65 73 20 6e 6f 74 FROM entries not
0f30: 20 75 73 61 62 6c 65 20 61 74 20 74 68 69 73 20 usable at this
0f40: 6c 65 76 65 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a level */.};../*.
0f50: 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65 ** Each instance
0f60: 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 of this object
0f70: 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 61 6c represents an al
0f80: 67 6f 72 69 74 68 6d 20 66 6f 72 20 65 76 61 6c gorithm for eval
0f90: 75 61 74 69 6e 67 20 6f 6e 65 0a 2a 2a 20 74 65 uating one.** te
0fa0: 72 6d 20 6f 66 20 61 20 6a 6f 69 6e 2e 20 20 45 rm of a join. E
0fb0: 76 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 very term of the
0fc0: 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 69 6c FROM clause wil
0fd0: 6c 20 68 61 76 65 20 61 74 20 6c 65 61 73 74 0a l have at least.
0fe0: 2a 2a 20 6f 6e 65 20 63 6f 72 72 65 73 70 6f 6e ** one correspon
0ff0: 64 69 6e 67 20 57 68 65 72 65 4c 6f 6f 70 20 6f ding WhereLoop o
1000: 62 6a 65 63 74 20 28 75 6e 6c 65 73 73 20 49 4e bject (unless IN
1010: 44 45 58 45 44 20 42 59 20 63 6f 6e 73 74 72 61 DEXED BY constra
1020: 69 6e 74 73 0a 2a 2a 20 70 72 65 76 65 6e 74 20 ints.** prevent
1030: 61 20 71 75 65 72 79 20 73 6f 6c 75 74 69 6f 6e a query solution
1040: 20 2d 20 77 68 69 63 68 20 69 73 20 61 6e 20 65 - which is an e
1050: 72 72 6f 72 29 20 61 6e 64 20 6d 61 6e 79 20 74 rror) and many t
1060: 65 72 6d 73 20 6f 66 20 74 68 65 0a 2a 2a 20 46 erms of the.** F
1070: 52 4f 4d 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 ROM clause will
1080: 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 57 68 have multiple Wh
1090: 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 2c ereLoop objects,
10a0: 20 65 61 63 68 20 64 65 73 63 72 69 62 69 6e 67 each describing
10b0: 20 61 0a 2a 2a 20 70 6f 74 65 6e 74 69 61 6c 20 a.** potential
10c0: 77 61 79 20 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 way of implement
10d0: 69 6e 67 20 74 68 61 74 20 46 52 4f 4d 2d 63 6c ing that FROM-cl
10e0: 61 75 73 65 20 74 65 72 6d 2c 20 74 6f 67 65 74 ause term, toget
10f0: 68 65 72 20 77 69 74 68 0a 2a 2a 20 64 65 70 65 her with.** depe
1100: 6e 64 65 6e 63 69 65 73 20 61 6e 64 20 63 6f 73 ndencies and cos
1110: 74 20 65 73 74 69 6d 61 74 65 73 20 66 6f 72 20 t estimates for
1120: 75 73 69 6e 67 20 74 68 65 20 63 68 6f 73 65 6e using the chosen
1130: 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a 2a 0a 2a algorithm..**.*
1140: 2a 20 51 75 65 72 79 20 70 6c 61 6e 6e 69 6e 67 * Query planning
1150: 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 62 75 69 consists of bui
1160: 6c 64 69 6e 67 20 75 70 20 61 20 63 6f 6c 6c 65 lding up a colle
1170: 63 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 ction of these W
1180: 68 65 72 65 4c 6f 6f 70 0a 2a 2a 20 6f 62 6a 65 hereLoop.** obje
1190: 63 74 73 2c 20 74 68 65 6e 20 63 6f 6d 70 75 74 cts, then comput
11a0: 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 ing a particular
11b0: 20 73 65 71 75 65 6e 63 65 20 6f 66 20 57 68 65 sequence of Whe
11c0: 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 2c 20 reLoop objects,
11d0: 77 69 74 68 0a 2a 2a 20 6f 6e 65 20 57 68 65 72 with.** one Wher
11e0: 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 20 70 65 72 eLoop object per
11f0: 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 FROM clause ter
1200: 6d 2c 20 74 68 61 74 20 73 61 74 69 73 66 79 20 m, that satisfy
1210: 61 6c 6c 20 64 65 70 65 6e 64 65 6e 63 69 65 73 all dependencies
1220: 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 6d 69 6e .** and that min
1230: 69 6d 69 7a 65 20 74 68 65 20 6f 76 65 72 61 6c imize the overal
1240: 6c 20 63 6f 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 l cost..*/.struc
1250: 74 20 57 68 65 72 65 4c 6f 6f 70 20 7b 0a 20 20 t WhereLoop {.
1260: 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 3b 20 Bitmask prereq;
1270: 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b /* Bitmask
1280: 20 6f 66 20 6f 74 68 65 72 20 6c 6f 6f 70 73 20 of other loops
1290: 74 68 61 74 20 6d 75 73 74 20 72 75 6e 20 66 69 that must run fi
12a0: 72 73 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b rst */. Bitmask
12b0: 20 6d 61 73 6b 53 65 6c 66 3b 20 20 20 20 20 2f maskSelf; /
12c0: 2a 20 42 69 74 6d 61 73 6b 20 69 64 65 6e 74 69 * Bitmask identi
12d0: 66 79 69 6e 67 20 74 61 62 6c 65 20 69 54 61 62 fying table iTab
12e0: 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 */.#ifdef SQLIT
12f0: 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20 63 E_DEBUG. char c
1300: 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 Id;
1310: 2f 2a 20 53 79 6d 62 6f 6c 69 63 20 49 44 20 6f /* Symbolic ID o
1320: 66 20 74 68 69 73 20 6c 6f 6f 70 20 66 6f 72 20 f this loop for
1330: 64 65 62 75 67 67 69 6e 67 20 75 73 65 20 2a 2f debugging use */
1340: 0a 23 65 6e 64 69 66 0a 20 20 75 38 20 69 54 61 .#endif. u8 iTa
1350: 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 b;
1360: 2f 2a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 20 46 /* Position in F
1370: 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 61 ROM clause of ta
1380: 62 6c 65 20 66 6f 72 20 74 68 69 73 20 6c 6f 6f ble for this loo
1390: 70 20 2a 2f 0a 20 20 75 38 20 69 53 6f 72 74 49 p */. u8 iSortI
13a0: 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 dx; /*
13b0: 53 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 6e 75 Sorting index nu
13c0: 6d 62 65 72 2e 20 20 30 3d 3d 4e 6f 6e 65 20 2a mber. 0==None *
13d0: 2f 0a 20 20 4c 6f 67 45 73 74 20 72 53 65 74 75 /. LogEst rSetu
13e0: 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 p; /* One
13f0: 2d 74 69 6d 65 20 73 65 74 75 70 20 63 6f 73 74 -time setup cost
1400: 20 28 65 78 3a 20 63 72 65 61 74 65 20 74 72 61 (ex: create tra
1410: 6e 73 69 65 6e 74 20 69 6e 64 65 78 29 20 2a 2f nsient index) */
1420: 0a 20 20 4c 6f 67 45 73 74 20 72 52 75 6e 3b 20 . LogEst rRun;
1430: 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 73 74 /* Cost
1440: 20 6f 66 20 72 75 6e 6e 69 6e 67 20 65 61 63 68 of running each
1450: 20 6c 6f 6f 70 20 2a 2f 0a 20 20 4c 6f 67 45 73 loop */. LogEs
1460: 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20 t nOut;
1470: 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e 75 /* Estimated nu
1480: 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 72 mber of output r
1490: 6f 77 73 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b ows */. union {
14a0: 0a 20 20 20 20 73 74 72 75 63 74 20 7b 20 20 20 . struct {
14b0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 /* I
14c0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 69 nformation for i
14d0: 6e 74 65 72 6e 61 6c 20 62 74 72 65 65 20 74 61 nternal btree ta
14e0: 62 6c 65 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e bles */. in
14f0: 74 20 6e 45 71 3b 20 20 20 20 20 20 20 20 20 20 t nEq;
1500: 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1510: 66 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 f equality const
1520: 72 61 69 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 raints */.
1530: 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 Index *pIndex;
1540: 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
1550: 75 73 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f used, or NULL */
1560: 0a 20 20 20 20 7d 20 62 74 72 65 65 3b 0a 20 20 . } btree;.
1570: 20 20 73 74 72 75 63 74 20 7b 20 20 20 20 20 20 struct {
1580: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f /* Info
1590: 72 6d 61 74 69 6f 6e 20 66 6f 72 20 76 69 72 74 rmation for virt
15a0: 75 61 6c 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 ual tables */.
15b0: 20 20 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20 int idxNum;
15c0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
15d0: 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 dex number */.
15e0: 20 20 20 20 75 38 20 6e 65 65 64 46 72 65 65 3b u8 needFree;
15f0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
1600: 75 65 20 69 66 20 73 71 6c 69 74 65 33 5f 66 72 ue if sqlite3_fr
1610: 65 65 28 69 64 78 53 74 72 29 20 69 73 20 6e 65 ee(idxStr) is ne
1620: 65 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 75 38 eded */. u8
1630: 20 69 73 4f 72 64 65 72 65 64 3b 20 20 20 20 20 isOrdered;
1640: 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 /* True if
1650: 73 61 74 69 73 66 69 65 73 20 4f 52 44 45 52 20 satisfies ORDER
1660: 42 59 20 2a 2f 0a 20 20 20 20 20 20 75 31 36 20 BY */. u16
1670: 6f 6d 69 74 4d 61 73 6b 3b 20 20 20 20 20 20 20 omitMask;
1680: 20 20 20 2f 2a 20 54 65 72 6d 73 20 74 68 61 74 /* Terms that
1690: 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20 may be omitted
16a0: 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 69 */. char *i
16b0: 64 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 dxStr;
16c0: 2f 2a 20 49 6e 64 65 78 20 69 64 65 6e 74 69 66 /* Index identif
16d0: 69 65 72 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 ier string */.
16e0: 20 20 7d 20 76 74 61 62 3b 0a 20 20 7d 20 75 3b } vtab;. } u;
16f0: 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73 3b 20 . u32 wsFlags;
1700: 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 /* WHER
1710: 45 5f 2a 20 66 6c 61 67 73 20 64 65 73 63 72 69 E_* flags descri
1720: 62 69 6e 67 20 74 68 65 20 70 6c 61 6e 20 2a 2f bing the plan */
1730: 0a 20 20 75 31 36 20 6e 4c 54 65 72 6d 3b 20 20 . u16 nLTerm;
1740: 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
1750: 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
1760: 20 61 4c 54 65 72 6d 5b 5d 20 2a 2f 0a 20 20 2f aLTerm[] */. /
1770: 2a 2a 2a 2a 20 77 68 65 72 65 4c 6f 6f 70 58 66 **** whereLoopXf
1780: 65 72 28 29 20 63 6f 70 69 65 73 20 66 69 65 6c er() copies fiel
1790: 64 73 20 61 62 6f 76 65 20 2a 2a 2a 2a 2a 2a 2a ds above *******
17a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
17b0: 2f 0a 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 /.# define WHERE
17c0: 5f 4c 4f 4f 50 5f 58 46 45 52 5f 53 5a 20 6f 66 _LOOP_XFER_SZ of
17d0: 66 73 65 74 6f 66 28 57 68 65 72 65 4c 6f 6f 70 fsetof(WhereLoop
17e0: 2c 6e 4c 53 6c 6f 74 29 0a 20 20 75 31 36 20 6e ,nLSlot). u16 n
17f0: 4c 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 LSlot;
1800: 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c /* Number of sl
1810: 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f ots allocated fo
1820: 72 20 61 4c 54 65 72 6d 5b 5d 20 2a 2f 0a 20 20 r aLTerm[] */.
1830: 57 68 65 72 65 54 65 72 6d 20 2a 2a 61 4c 54 65 WhereTerm **aLTe
1840: 72 6d 3b 20 20 20 2f 2a 20 57 68 65 72 65 54 65 rm; /* WhereTe
1850: 72 6d 73 20 75 73 65 64 20 2a 2f 0a 20 20 57 68 rms used */. Wh
1860: 65 72 65 4c 6f 6f 70 20 2a 70 4e 65 78 74 4c 6f ereLoop *pNextLo
1870: 6f 70 3b 20 2f 2a 20 4e 65 78 74 20 57 68 65 72 op; /* Next Wher
1880: 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 20 69 6e 20 eLoop object in
1890: 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 the WhereClause
18a0: 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a */. WhereTerm *
18b0: 61 4c 54 65 72 6d 53 70 61 63 65 5b 34 5d 3b 20 aLTermSpace[4];
18c0: 20 2f 2a 20 49 6e 69 74 69 61 6c 20 61 4c 54 65 /* Initial aLTe
18d0: 72 6d 5b 5d 20 73 70 61 63 65 20 2a 2f 0a 7d 3b rm[] space */.};
18e0: 0a 0a 2f 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 ../* This object
18f0: 20 68 6f 6c 64 73 20 74 68 65 20 70 72 65 72 65 holds the prere
1900: 71 75 69 73 69 74 65 73 20 61 6e 64 20 74 68 65 quisites and the
1910: 20 63 6f 73 74 20 6f 66 20 72 75 6e 6e 69 6e 67 cost of running
1920: 20 61 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 6f a.** subquery o
1930: 6e 20 6f 6e 65 20 6f 70 65 72 61 6e 64 20 6f 66 n one operand of
1940: 20 61 6e 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 an OR operator
1950: 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 in the WHERE cla
1960: 75 73 65 2e 0a 2a 2a 20 53 65 65 20 57 68 65 72 use..** See Wher
1970: 65 4f 72 53 65 74 20 66 6f 72 20 61 64 64 69 74 eOrSet for addit
1980: 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f ional informatio
1990: 6e 20 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 n .*/.struct Whe
19a0: 72 65 4f 72 43 6f 73 74 20 7b 0a 20 20 42 69 74 reOrCost {. Bit
19b0: 6d 61 73 6b 20 70 72 65 72 65 71 3b 20 20 20 20 mask prereq;
19c0: 20 2f 2a 20 50 72 65 72 65 71 75 69 73 69 74 65 /* Prerequisite
19d0: 73 20 2a 2f 0a 20 20 4c 6f 67 45 73 74 20 72 52 s */. LogEst rR
19e0: 75 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f un; /* Co
19f0: 73 74 20 6f 66 20 72 75 6e 6e 69 6e 67 20 74 68 st of running th
1a00: 69 73 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 is subquery */.
1a10: 20 4c 6f 67 45 73 74 20 6e 4f 75 74 3b 20 20 20 LogEst nOut;
1a20: 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1a30: 66 20 6f 75 74 70 75 74 73 20 66 6f 72 20 74 68 f outputs for th
1a40: 69 73 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 7d is subquery */.}
1a50: 3b 0a 0a 2f 2a 20 54 68 65 20 57 68 65 72 65 4f ;../* The WhereO
1a60: 72 53 65 74 20 6f 62 6a 65 63 74 20 68 6f 6c 64 rSet object hold
1a70: 73 20 61 20 73 65 74 20 6f 66 20 70 6f 73 73 69 s a set of possi
1a80: 62 6c 65 20 57 68 65 72 65 4f 72 43 6f 73 74 73 ble WhereOrCosts
1a90: 20 74 68 61 74 0a 2a 2a 20 63 6f 72 72 65 73 70 that.** corresp
1aa0: 6f 6e 64 20 74 6f 20 74 68 65 20 73 75 62 71 75 ond to the subqu
1ab0: 65 72 79 28 73 29 20 6f 66 20 4f 52 2d 63 6c 61 ery(s) of OR-cla
1ac0: 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 use processing.
1ad0: 20 4f 6e 6c 79 20 74 68 65 0a 2a 2a 20 62 65 73 Only the.** bes
1ae0: 74 20 4e 5f 4f 52 5f 43 4f 53 54 20 65 6c 65 6d t N_OR_COST elem
1af0: 65 6e 74 73 20 61 72 65 20 72 65 74 61 69 6e 65 ents are retaine
1b00: 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f d..*/.#define N_
1b10: 4f 52 5f 43 4f 53 54 20 33 0a 73 74 72 75 63 74 OR_COST 3.struct
1b20: 20 57 68 65 72 65 4f 72 53 65 74 20 7b 0a 20 20 WhereOrSet {.
1b30: 75 31 36 20 6e 3b 20 20 20 20 20 20 20 20 20 20 u16 n;
1b40: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1b50: 75 6d 62 65 72 20 6f 66 20 76 61 6c 69 64 20 61 umber of valid a
1b60: 5b 5d 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 [] entries */.
1b70: 57 68 65 72 65 4f 72 43 6f 73 74 20 61 5b 4e 5f WhereOrCost a[N_
1b80: 4f 52 5f 43 4f 53 54 5d 3b 20 20 20 2f 2a 20 53 OR_COST]; /* S
1b90: 65 74 20 6f 66 20 62 65 73 74 20 63 6f 73 74 73 et of best costs
1ba0: 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 */.};.../* Forw
1bb0: 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 ard declaration
1bc0: 6f 66 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 73 74 of methods */.st
1bd0: 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 4c 6f atic int whereLo
1be0: 6f 70 52 65 73 69 7a 65 28 73 71 6c 69 74 65 33 opResize(sqlite3
1bf0: 2a 2c 20 57 68 65 72 65 4c 6f 6f 70 2a 2c 20 69 *, WhereLoop*, i
1c00: 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 nt);../*.** Each
1c10: 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 instance of thi
1c20: 73 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73 20 61 s object holds a
1c30: 20 73 65 71 75 65 6e 63 65 20 6f 66 20 57 68 65 sequence of Whe
1c40: 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 0a 2a reLoop objects.*
1c50: 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 * that implement
1c60: 20 73 6f 6d 65 20 6f 72 20 61 6c 6c 20 6f 66 20 some or all of
1c70: 61 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a 2a 2a a query plan..**
1c80: 0a 2a 2a 20 54 68 69 6e 6b 20 6f 66 20 65 61 63 .** Think of eac
1c90: 68 20 57 68 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 h WhereLoop obje
1ca0: 63 74 20 61 73 20 61 20 6e 6f 64 65 20 69 6e 20 ct as a node in
1cb0: 61 20 67 72 61 70 68 20 77 69 74 68 20 61 72 63 a graph with arc
1cc0: 73 0a 2a 2a 20 73 68 6f 77 69 6e 67 20 64 65 70 s.** showing dep
1cd0: 65 6e 64 65 6e 63 69 65 73 20 61 6e 64 20 63 6f endencies and co
1ce0: 73 74 73 20 66 6f 72 20 74 72 61 76 65 6c 6c 69 sts for travelli
1cf0: 6e 67 20 62 65 74 77 65 65 6e 20 6e 6f 64 65 73 ng between nodes
1d00: 2e 20 20 28 54 68 61 74 20 69 73 0a 2a 2a 20 6e . (That is.** n
1d10: 6f 74 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79 20 ot a completely
1d20: 61 63 63 75 72 61 74 65 20 64 65 73 63 72 69 70 accurate descrip
1d30: 74 69 6f 6e 20 62 65 63 61 75 73 65 20 57 68 65 tion because Whe
1d40: 72 65 4c 6f 6f 70 20 63 6f 73 74 73 20 61 72 65 reLoop costs are
1d50: 20 61 0a 2a 2a 20 76 65 63 74 6f 72 2c 20 6e 6f a.** vector, no
1d60: 74 20 61 20 73 63 61 6c 61 72 2c 20 61 6e 64 20 t a scalar, and
1d70: 62 65 63 61 75 73 65 20 64 65 70 65 6e 64 65 6e because dependen
1d80: 63 69 65 73 20 61 72 65 20 6d 61 6e 79 2d 74 6f cies are many-to
1d90: 2d 6f 6e 65 2c 20 6e 6f 74 0a 2a 2a 20 6f 6e 65 -one, not.** one
1da0: 2d 74 6f 2d 6f 6e 65 20 61 73 20 61 72 65 20 67 -to-one as are g
1db0: 72 61 70 68 20 6e 6f 64 65 73 2e 20 20 42 75 74 raph nodes. But
1dc0: 20 69 74 20 69 73 20 61 20 75 73 65 66 75 6c 20 it is a useful
1dd0: 76 69 73 75 61 6c 69 7a 61 74 69 6f 6e 20 61 69 visualization ai
1de0: 64 2e 29 0a 2a 2a 20 54 68 65 6e 20 61 20 57 68 d.).** Then a Wh
1df0: 65 72 65 50 61 74 68 20 6f 62 6a 65 63 74 20 69 erePath object i
1e00: 73 20 61 20 70 61 74 68 20 74 68 72 6f 75 67 68 s a path through
1e10: 20 74 68 65 20 67 72 61 70 68 20 74 68 61 74 20 the graph that
1e20: 76 69 73 69 74 73 20 73 6f 6d 65 0a 2a 2a 20 6f visits some.** o
1e30: 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 57 68 65 r all of the Whe
1e40: 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 20 6f reLoop objects o
1e50: 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 nce..**.** The "
1e60: 73 6f 6c 76 65 72 22 20 77 6f 72 6b 73 20 62 79 solver" works by
1e70: 20 63 72 65 61 74 69 6e 67 20 74 68 65 20 4e 20 creating the N
1e80: 62 65 73 74 20 57 68 65 72 65 50 61 74 68 20 6f best WherePath o
1e90: 62 6a 65 63 74 73 20 6f 66 20 6c 65 6e 67 74 68 bjects of length
1ea0: 0a 2a 2a 20 31 2e 20 20 54 68 65 6e 20 75 73 69 .** 1. Then usi
1eb0: 6e 67 20 74 68 6f 73 65 20 61 73 20 61 20 62 61 ng those as a ba
1ec0: 73 69 73 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 sis to compute t
1ed0: 68 65 20 4e 20 62 65 73 74 20 57 68 65 72 65 50 he N best WhereP
1ee0: 61 74 68 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 6f ath objects.** o
1ef0: 66 20 6c 65 6e 67 74 68 20 32 2e 20 20 41 6e 64 f length 2. And
1f00: 20 73 6f 20 66 6f 72 74 68 20 75 6e 74 69 6c 20 so forth until
1f10: 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 57 68 the length of Wh
1f20: 65 72 65 50 61 74 68 73 20 65 71 75 61 6c 73 20 erePaths equals
1f30: 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 the.** number of
1f40: 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 20 46 52 nodes in the FR
1f50: 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20 OM clause. The
1f60: 62 65 73 74 20 28 6c 6f 77 65 73 74 20 63 6f 73 best (lowest cos
1f70: 74 29 20 57 68 65 72 65 50 61 74 68 0a 2a 2a 20 t) WherePath.**
1f80: 61 74 20 74 68 65 20 65 6e 64 20 69 73 20 74 68 at the end is th
1f90: 65 20 63 68 6f 6f 73 65 6e 20 71 75 65 72 79 20 e choosen query
1fa0: 70 6c 61 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 plan..*/.struct
1fb0: 57 68 65 72 65 50 61 74 68 20 7b 0a 20 20 42 69 WherePath {. Bi
1fc0: 74 6d 61 73 6b 20 6d 61 73 6b 4c 6f 6f 70 3b 20 tmask maskLoop;
1fd0: 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f /* Bitmask o
1fe0: 66 20 61 6c 6c 20 57 68 65 72 65 4c 6f 6f 70 20 f all WhereLoop
1ff0: 6f 62 6a 65 63 74 73 20 69 6e 20 74 68 69 73 20 objects in this
2000: 70 61 74 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 path */. Bitmas
2010: 6b 20 72 65 76 4c 6f 6f 70 3b 20 20 20 20 20 20 k revLoop;
2020: 2f 2a 20 61 4c 6f 6f 70 5b 5d 73 20 74 68 61 74 /* aLoop[]s that
2030: 20 73 68 6f 75 6c 64 20 62 65 20 72 65 76 65 72 should be rever
2040: 73 65 64 20 66 6f 72 20 4f 52 44 45 52 20 42 59 sed for ORDER BY
2050: 20 2a 2f 0a 20 20 4c 6f 67 45 73 74 20 6e 52 6f */. LogEst nRo
2060: 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 w; /* E
2070: 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20 stimated number
2080: 6f 66 20 72 6f 77 73 20 67 65 6e 65 72 61 74 65 of rows generate
2090: 64 20 62 79 20 74 68 69 73 20 70 61 74 68 20 2a d by this path *
20a0: 2f 0a 20 20 4c 6f 67 45 73 74 20 72 43 6f 73 74 /. LogEst rCost
20b0: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 ; /* Tot
20c0: 61 6c 20 63 6f 73 74 20 6f 66 20 74 68 69 73 20 al cost of this
20d0: 70 61 74 68 20 2a 2f 0a 20 20 75 38 20 69 73 4f path */. u8 isO
20e0: 72 64 65 72 65 64 3b 20 20 20 20 20 20 20 20 20 rdered;
20f0: 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 /* True if this
2100: 70 61 74 68 20 73 61 74 69 73 66 69 65 73 20 4f path satisfies O
2110: 52 44 45 52 20 42 59 20 2a 2f 0a 20 20 75 38 20 RDER BY */. u8
2120: 69 73 4f 72 64 65 72 65 64 56 61 6c 69 64 3b 20 isOrderedValid;
2130: 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 /* True if th
2140: 65 20 69 73 4f 72 64 65 72 65 64 20 66 69 65 6c e isOrdered fiel
2150: 64 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 d is valid */.
2160: 57 68 65 72 65 4c 6f 6f 70 20 2a 2a 61 4c 6f 6f WhereLoop **aLoo
2170: 70 3b 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f p; /* Array o
2180: 66 20 57 68 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 f WhereLoop obje
2190: 63 74 73 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 cts implementing
21a0: 20 74 68 69 73 20 70 61 74 68 20 2a 2f 0a 7d 3b this path */.};
21b0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71 75 65 72 ../*.** The quer
21c0: 79 20 67 65 6e 65 72 61 74 6f 72 20 75 73 65 73 y generator uses
21d0: 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 73 an array of ins
21e0: 74 61 6e 63 65 73 20 6f 66 20 74 68 69 73 20 73 tances of this s
21f0: 74 72 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 68 tructure to.** h
2200: 65 6c 70 20 69 74 20 61 6e 61 6c 79 7a 65 20 74 elp it analyze t
2210: 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e he subexpression
2220: 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 s of the WHERE c
2230: 6c 61 75 73 65 2e 20 20 45 61 63 68 20 57 48 45 lause. Each WHE
2240: 52 45 0a 2a 2a 20 63 6c 61 75 73 65 20 73 75 62 RE.** clause sub
2250: 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 expression is se
2260: 70 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 parated from the
2270: 20 6f 74 68 65 72 73 20 62 79 20 41 4e 44 20 6f others by AND o
2280: 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 75 73 75 perators,.** usu
2290: 61 6c 6c 79 2c 20 6f 72 20 73 6f 6d 65 74 69 6d ally, or sometim
22a0: 65 73 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e es subexpression
22b0: 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20 4f s separated by O
22c0: 52 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 57 68 65 R..**.** All Whe
22d0: 72 65 54 65 72 6d 73 20 61 72 65 20 63 6f 6c 6c reTerms are coll
22e0: 65 63 74 65 64 20 69 6e 74 6f 20 61 20 73 69 6e ected into a sin
22f0: 67 6c 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 gle WhereClause
2300: 73 74 72 75 63 74 75 72 65 2e 20 20 0a 2a 2a 20 structure. .**
2310: 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 64 The following id
2320: 65 6e 74 69 74 79 20 68 6f 6c 64 73 3a 0a 2a 2a entity holds:.**
2330: 0a 2a 2a 20 20 20 20 20 20 20 20 57 68 65 72 65 .** Where
2340: 54 65 72 6d 2e 70 57 43 2d 3e 61 5b 57 68 65 72 Term.pWC->a[Wher
2350: 65 54 65 72 6d 2e 69 64 78 5d 20 3d 3d 20 57 68 eTerm.idx] == Wh
2360: 65 72 65 54 65 72 6d 0a 2a 2a 0a 2a 2a 20 57 68 ereTerm.**.** Wh
2370: 65 6e 20 61 20 74 65 72 6d 20 69 73 20 6f 66 20 en a term is of
2380: 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 the form:.**.**
2390: 20 20 20 20 20 20 20 20 20 20 20 20 20 58 20 3c X <
23a0: 6f 70 3e 20 3c 65 78 70 72 3e 0a 2a 2a 0a 2a 2a op> <expr>.**.**
23b0: 20 77 68 65 72 65 20 58 20 69 73 20 61 20 63 6f where X is a co
23c0: 6c 75 6d 6e 20 6e 61 6d 65 20 61 6e 64 20 3c 6f lumn name and <o
23d0: 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20 63 65 72 p> is one of cer
23e0: 74 61 69 6e 20 6f 70 65 72 61 74 6f 72 73 2c 0a tain operators,.
23f0: 2a 2a 20 74 68 65 6e 20 57 68 65 72 65 54 65 72 ** then WhereTer
2400: 6d 2e 6c 65 66 74 43 75 72 73 6f 72 20 61 6e 64 m.leftCursor and
2410: 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 6c 65 66 WhereTerm.u.lef
2420: 74 43 6f 6c 75 6d 6e 20 72 65 63 6f 72 64 20 74 tColumn record t
2430: 68 65 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d he.** cursor num
2440: 62 65 72 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e ber and column n
2450: 75 6d 62 65 72 20 66 6f 72 20 58 2e 20 20 57 68 umber for X. Wh
2460: 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f ereTerm.eOperato
2470: 72 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 r records.** the
2480: 20 3c 6f 70 3e 20 75 73 69 6e 67 20 61 20 62 69 <op> using a bi
2490: 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 64 tmask encoding d
24a0: 65 66 69 6e 65 64 20 62 79 20 57 4f 5f 78 78 78 efined by WO_xxx
24b0: 20 62 65 6c 6f 77 2e 20 20 54 68 65 0a 2a 2a 20 below. The.**
24c0: 75 73 65 20 6f 66 20 61 20 62 69 74 6d 61 73 6b use of a bitmask
24d0: 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 encoding for th
24e0: 65 20 6f 70 65 72 61 74 6f 72 20 61 6c 6c 6f 77 e operator allow
24f0: 73 20 75 73 20 74 6f 20 73 65 61 72 63 68 0a 2a s us to search.*
2500: 2a 20 71 75 69 63 6b 6c 79 20 66 6f 72 20 74 65 * quickly for te
2510: 72 6d 73 20 74 68 61 74 20 6d 61 74 63 68 20 61 rms that match a
2520: 6e 79 20 6f 66 20 73 65 76 65 72 61 6c 20 64 69 ny of several di
2530: 66 66 65 72 65 6e 74 20 6f 70 65 72 61 74 6f 72 fferent operator
2540: 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 57 68 65 72 65 s..**.** A Where
2550: 54 65 72 6d 20 6d 69 67 68 74 20 61 6c 73 6f 20 Term might also
2560: 62 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 be two or more s
2570: 75 62 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 ubterms connecte
2580: 64 20 62 79 20 4f 52 3a 0a 2a 2a 0a 2a 2a 20 20 d by OR:.**.**
2590: 20 20 20 20 20 20 20 28 74 31 2e 58 20 3c 6f 70 (t1.X <op
25a0: 3e 20 3c 65 78 70 72 3e 29 20 4f 52 20 28 74 31 > <expr>) OR (t1
25b0: 2e 59 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20 .Y <op> <expr>)
25c0: 4f 52 20 2e 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 6e OR .....**.** In
25d0: 20 74 68 69 73 20 73 65 63 6f 6e 64 20 63 61 73 this second cas
25e0: 65 2c 20 77 74 46 6c 61 67 20 68 61 73 20 74 68 e, wtFlag has th
25f0: 65 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 62 69 e TERM_ORINFO bi
2600: 74 20 73 65 74 20 61 6e 64 20 65 4f 70 65 72 61 t set and eOpera
2610: 74 6f 72 3d 3d 57 4f 5f 4f 52 0a 2a 2a 20 61 6e tor==WO_OR.** an
2620: 64 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e d the WhereTerm.
2630: 75 2e 70 4f 72 49 6e 66 6f 20 66 69 65 6c 64 20 u.pOrInfo field
2640: 70 6f 69 6e 74 73 20 74 6f 20 61 75 78 69 6c 69 points to auxili
2650: 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ary information
2660: 74 68 61 74 0a 2a 2a 20 69 73 20 63 6f 6c 6c 65 that.** is colle
2670: 63 74 65 64 20 61 62 6f 75 74 20 74 68 65 20 4f cted about the O
2680: 52 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 R clause..**.**
2690: 49 66 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 If a term in the
26a0: 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 6f WHERE clause do
26b0: 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 65 69 74 es not match eit
26c0: 68 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 70 her of the two p
26d0: 72 65 76 69 6f 75 73 0a 2a 2a 20 63 61 74 65 67 revious.** categ
26e0: 6f 72 69 65 73 2c 20 74 68 65 6e 20 65 4f 70 65 ories, then eOpe
26f0: 72 61 74 6f 72 3d 3d 30 2e 20 20 54 68 65 20 57 rator==0. The W
2700: 68 65 72 65 54 65 72 6d 2e 70 45 78 70 72 20 66 hereTerm.pExpr f
2710: 69 65 6c 64 20 69 73 20 73 74 69 6c 6c 20 73 65 ield is still se
2720: 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6f 72 69 67 t.** to the orig
2730: 69 6e 61 6c 20 73 75 62 65 78 70 72 65 73 73 69 inal subexpressi
2740: 6f 6e 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 77 on content and w
2750: 74 46 6c 61 67 73 20 69 73 20 73 65 74 20 75 70 tFlags is set up
2760: 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 0a 2a appropriately.*
2770: 2a 20 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 * but no other f
2780: 69 65 6c 64 73 20 69 6e 20 74 68 65 20 57 68 65 ields in the Whe
2790: 72 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 61 72 reTerm object ar
27a0: 65 20 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2a e meaningful..**
27b0: 0a 2a 2a 20 57 68 65 6e 20 65 4f 70 65 72 61 74 .** When eOperat
27c0: 6f 72 21 3d 30 2c 20 70 72 65 72 65 71 52 69 67 or!=0, prereqRig
27d0: 68 74 20 61 6e 64 20 70 72 65 72 65 71 41 6c 6c ht and prereqAll
27e0: 20 72 65 63 6f 72 64 20 73 65 74 73 20 6f 66 20 record sets of
27f0: 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 2c 0a cursor numbers,.
2800: 2a 2a 20 62 75 74 20 74 68 65 79 20 64 6f 20 73 ** but they do s
2810: 6f 20 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 41 o indirectly. A
2820: 20 73 69 6e 67 6c 65 20 57 68 65 72 65 4d 61 73 single WhereMas
2830: 6b 53 65 74 20 73 74 72 75 63 74 75 72 65 20 74 kSet structure t
2840: 72 61 6e 73 6c 61 74 65 73 0a 2a 2a 20 63 75 72 ranslates.** cur
2850: 73 6f 72 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 sor number into
2860: 62 69 74 73 20 61 6e 64 20 74 68 65 20 74 72 61 bits and the tra
2870: 6e 73 6c 61 74 65 64 20 62 69 74 20 69 73 20 73 nslated bit is s
2880: 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70 72 65 tored in the pre
2890: 72 65 71 0a 2a 2a 20 66 69 65 6c 64 73 2e 20 20 req.** fields.
28a0: 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 The translation
28b0: 69 73 20 75 73 65 64 20 69 6e 20 6f 72 64 65 72 is used in order
28c0: 20 74 6f 20 6d 61 78 69 6d 69 7a 65 20 74 68 65 to maximize the
28d0: 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 69 number of.** bi
28e0: 74 73 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 ts that will fit
28f0: 20 69 6e 20 61 20 42 69 74 6d 61 73 6b 2e 20 20 in a Bitmask.
2900: 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 The VDBE cursor
2910: 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65 numbers might be
2920: 0a 2a 2a 20 73 70 72 65 61 64 20 6f 75 74 20 6f .** spread out o
2930: 76 65 72 20 74 68 65 20 6e 6f 6e 2d 6e 65 67 61 ver the non-nega
2940: 74 69 76 65 20 69 6e 74 65 67 65 72 73 2e 20 20 tive integers.
2950: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 For example, the
2960: 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 cursor.** numbe
2970: 72 73 20 6d 69 67 68 74 20 62 65 20 33 2c 20 38 rs might be 3, 8
2980: 2c 20 39 2c 20 31 30 2c 20 32 30 2c 20 32 33 2c , 9, 10, 20, 23,
2990: 20 34 31 2c 20 61 6e 64 20 34 35 2e 20 20 54 68 41, and 45. Th
29a0: 65 20 57 68 65 72 65 4d 61 73 6b 53 65 74 0a 2a e WhereMaskSet.*
29b0: 2a 20 74 72 61 6e 73 6c 61 74 65 73 20 74 68 65 * translates the
29c0: 73 65 20 73 70 61 72 73 65 20 63 75 72 73 6f 72 se sparse cursor
29d0: 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f numbers into co
29e0: 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65 67 65 nsecutive intege
29f0: 72 73 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 rs.** beginning
2a00: 77 69 74 68 20 30 20 69 6e 20 6f 72 64 65 72 20 with 0 in order
2a10: 74 6f 20 6d 61 6b 65 20 74 68 65 20 62 65 73 74 to make the best
2a20: 20 70 6f 73 73 69 62 6c 65 20 75 73 65 20 6f 66 possible use of
2a30: 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a the available.*
2a40: 2a 20 62 69 74 73 20 69 6e 20 74 68 65 20 42 69 * bits in the Bi
2a50: 74 6d 61 73 6b 2e 20 20 53 6f 2c 20 69 6e 20 74 tmask. So, in t
2a60: 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 he example above
2a70: 2c 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d , the cursor num
2a80: 62 65 72 73 0a 2a 2a 20 77 6f 75 6c 64 20 62 65 bers.** would be
2a90: 20 6d 61 70 70 65 64 20 69 6e 74 6f 20 69 6e 74 mapped into int
2aa0: 65 67 65 72 73 20 30 20 74 68 72 6f 75 67 68 20 egers 0 through
2ab0: 37 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 7..**.** The num
2ac0: 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 ber of terms in
2ad0: 61 20 6a 6f 69 6e 20 69 73 20 6c 69 6d 69 74 65 a join is limite
2ae0: 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 d by the number
2af0: 6f 66 20 62 69 74 73 0a 2a 2a 20 69 6e 20 70 72 of bits.** in pr
2b00: 65 72 65 71 52 69 67 68 74 20 61 6e 64 20 70 72 ereqRight and pr
2b10: 65 72 65 71 41 6c 6c 2e 20 20 54 68 65 20 64 65 ereqAll. The de
2b20: 66 61 75 6c 74 20 69 73 20 36 34 20 62 69 74 73 fault is 64 bits
2b30: 2c 20 68 65 6e 63 65 20 53 51 4c 69 74 65 0a 2a , hence SQLite.*
2b40: 2a 20 69 73 20 6f 6e 6c 79 20 61 62 6c 65 20 74 * is only able t
2b50: 6f 20 70 72 6f 63 65 73 73 20 6a 6f 69 6e 73 20 o process joins
2b60: 77 69 74 68 20 36 34 20 6f 72 20 66 65 77 65 72 with 64 or fewer
2b70: 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74 72 75 tables..*/.stru
2b80: 63 74 20 57 68 65 72 65 54 65 72 6d 20 7b 0a 20 ct WhereTerm {.
2b90: 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 Expr *pExpr;
2ba0: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e /* Poin
2bb0: 74 65 72 20 74 6f 20 74 68 65 20 73 75 62 65 78 ter to the subex
2bc0: 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 pression that is
2bd0: 20 74 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 this term */.
2be0: 69 6e 74 20 69 50 61 72 65 6e 74 3b 20 20 20 20 int iParent;
2bf0: 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 /* Disab
2c00: 6c 65 20 70 57 43 2d 3e 61 5b 69 50 61 72 65 6e le pWC->a[iParen
2c10: 74 5d 20 77 68 65 6e 20 74 68 69 73 20 74 65 72 t] when this ter
2c20: 6d 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20 m disabled */.
2c30: 69 6e 74 20 6c 65 66 74 43 75 72 73 6f 72 3b 20 int leftCursor;
2c40: 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f /* Curso
2c50: 72 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69 6e r number of X in
2c60: 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 "X <op> <expr>"
2c70: 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 */. union {.
2c80: 20 20 69 6e 74 20 6c 65 66 74 43 6f 6c 75 6d 6e int leftColumn
2c90: 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c ; /* Col
2ca0: 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 umn number of X
2cb0: 69 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 in "X <op> <expr
2cc0: 3e 22 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 4f >" */. WhereO
2cd0: 72 49 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 rInfo *pOrInfo;
2ce0: 20 20 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f 72 /* Extra infor
2cf0: 6d 61 74 69 6f 6e 20 69 66 20 28 65 4f 70 65 72 mation if (eOper
2d00: 61 74 6f 72 20 26 20 57 4f 5f 4f 52 29 21 3d 30 ator & WO_OR)!=0
2d10: 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 41 6e 64 */. WhereAnd
2d20: 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b 20 Info *pAndInfo;
2d30: 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f 72 6d 61 /* Extra informa
2d40: 74 69 6f 6e 20 69 66 20 28 65 4f 70 65 72 61 74 tion if (eOperat
2d50: 6f 72 26 20 57 4f 5f 41 4e 44 29 21 3d 30 20 2a or& WO_AND)!=0 *
2d60: 2f 0a 20 20 7d 20 75 3b 0a 20 20 4c 6f 67 45 73 /. } u;. LogEs
2d70: 74 20 74 72 75 74 68 50 72 6f 62 3b 20 20 20 20 t truthProb;
2d80: 20 20 20 2f 2a 20 50 72 6f 62 61 62 69 6c 69 74 /* Probabilit
2d90: 79 20 6f 66 20 74 72 75 74 68 20 66 6f 72 20 74 y of truth for t
2da0: 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a his expression *
2db0: 2f 0a 20 20 75 31 36 20 65 4f 70 65 72 61 74 6f /. u16 eOperato
2dc0: 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 r; /* A
2dd0: 20 57 4f 5f 78 78 20 76 61 6c 75 65 20 64 65 73 WO_xx value des
2de0: 63 72 69 62 69 6e 67 20 3c 6f 70 3e 20 2a 2f 0a cribing <op> */.
2df0: 20 20 75 38 20 77 74 46 6c 61 67 73 3b 20 20 20 u8 wtFlags;
2e00: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 45 52 /* TER
2e10: 4d 5f 78 78 78 20 62 69 74 20 66 6c 61 67 73 2e M_xxx bit flags.
2e20: 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 See below */.
2e30: 20 75 38 20 6e 43 68 69 6c 64 3b 20 20 20 20 20 u8 nChild;
2e40: 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
2e50: 65 72 20 6f 66 20 63 68 69 6c 64 72 65 6e 20 74 er of children t
2e60: 68 61 74 20 6d 75 73 74 20 64 69 73 61 62 6c 65 hat must disable
2e70: 20 75 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c us */. WhereCl
2e80: 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20 ause *pWC;
2e90: 20 2f 2a 20 54 68 65 20 63 6c 61 75 73 65 20 74 /* The clause t
2ea0: 68 69 73 20 74 65 72 6d 20 69 73 20 70 61 72 74 his term is part
2eb0: 20 6f 66 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b of */. Bitmask
2ec0: 20 70 72 65 72 65 71 52 69 67 68 74 3b 20 20 20 prereqRight;
2ed0: 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 /* Bitmask of t
2ee0: 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 70 45 ables used by pE
2ef0: 78 70 72 2d 3e 70 52 69 67 68 74 20 2a 2f 0a 20 xpr->pRight */.
2f00: 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41 Bitmask prereqA
2f10: 6c 6c 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d ll; /* Bitm
2f20: 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20 72 65 ask of tables re
2f30: 66 65 72 65 6e 63 65 64 20 62 79 20 70 45 78 70 ferenced by pExp
2f40: 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 r */.};../*.** A
2f50: 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 llowed values of
2f60: 20 57 68 65 72 65 54 65 72 6d 2e 77 74 46 6c 61 WhereTerm.wtFla
2f70: 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 gs.*/.#define TE
2f80: 52 4d 5f 44 59 4e 41 4d 49 43 20 20 20 20 30 78 RM_DYNAMIC 0x
2f90: 30 31 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 01 /* Need to
2fa0: 63 61 6c 6c 20 73 71 6c 69 74 65 33 45 78 70 72 call sqlite3Expr
2fb0: 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 Delete(db, pExpr
2fc0: 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 ) */.#define TER
2fd0: 4d 5f 56 49 52 54 55 41 4c 20 20 20 20 30 78 30 M_VIRTUAL 0x0
2fe0: 32 20 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 2 /* Added by
2ff0: 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 the optimizer.
3000: 44 6f 20 6e 6f 74 20 63 6f 64 65 20 2a 2f 0a 23 Do not code */.#
3010: 64 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 44 45 define TERM_CODE
3020: 44 20 20 20 20 20 20 30 78 30 34 20 20 20 2f 2a D 0x04 /*
3030: 20 54 68 69 73 20 74 65 72 6d 20 69 73 20 61 6c This term is al
3040: 72 65 61 64 79 20 63 6f 64 65 64 20 2a 2f 0a 23 ready coded */.#
3050: 64 65 66 69 6e 65 20 54 45 52 4d 5f 43 4f 50 49 define TERM_COPI
3060: 45 44 20 20 20 20 20 30 78 30 38 20 20 20 2f 2a ED 0x08 /*
3070: 20 48 61 73 20 61 20 63 68 69 6c 64 20 2a 2f 0a Has a child */.
3080: 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 49 #define TERM_ORI
3090: 4e 46 4f 20 20 20 20 20 30 78 31 30 20 20 20 2f NFO 0x10 /
30a0: 2a 20 4e 65 65 64 20 74 6f 20 66 72 65 65 20 74 * Need to free t
30b0: 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 he WhereTerm.u.p
30c0: 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f OrInfo object */
30d0: 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 41 4e .#define TERM_AN
30e0: 44 49 4e 46 4f 20 20 20 20 30 78 32 30 20 20 20 DINFO 0x20
30f0: 2f 2a 20 4e 65 65 64 20 74 6f 20 66 72 65 65 20 /* Need to free
3100: 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e 75 2e the WhereTerm.u.
3110: 70 41 6e 64 49 6e 66 6f 20 6f 62 6a 20 2a 2f 0a pAndInfo obj */.
3120: 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 4f 52 5f #define TERM_OR_
3130: 4f 4b 20 20 20 20 20 20 30 78 34 30 20 20 20 2f OK 0x40 /
3140: 2a 20 55 73 65 64 20 64 75 72 69 6e 67 20 4f 52 * Used during OR
3150: 2d 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 -clause processi
3160: 6e 67 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c ng */.#ifdef SQL
3170: 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
3180: 5f 4f 52 5f 53 54 41 54 34 0a 23 20 20 64 65 66 _OR_STAT4.# def
3190: 69 6e 65 20 54 45 52 4d 5f 56 4e 55 4c 4c 20 20 ine TERM_VNULL
31a0: 20 20 30 78 38 30 20 20 20 2f 2a 20 4d 61 6e 75 0x80 /* Manu
31b0: 66 61 63 74 75 72 65 64 20 78 3e 4e 55 4c 4c 20 factured x>NULL
31c0: 6f 72 20 78 3c 3d 4e 55 4c 4c 20 74 65 72 6d 20 or x<=NULL term
31d0: 2a 2f 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 */.#else.# defi
31e0: 6e 65 20 54 45 52 4d 5f 56 4e 55 4c 4c 20 20 20 ne TERM_VNULL
31f0: 20 30 78 30 30 20 20 20 2f 2a 20 44 69 73 61 62 0x00 /* Disab
3200: 6c 65 64 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 led if not using
3210: 20 73 74 61 74 33 20 2a 2f 0a 23 65 6e 64 69 66 stat3 */.#endif
3220: 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
3230: 6e 63 65 20 6f 66 20 74 68 65 20 57 68 65 72 65 nce of the Where
3240: 53 63 61 6e 20 6f 62 6a 65 63 74 20 69 73 20 75 Scan object is u
3250: 73 65 64 20 61 73 20 61 6e 20 69 74 65 72 61 74 sed as an iterat
3260: 6f 72 20 66 6f 72 20 6c 6f 63 61 74 69 6e 67 0a or for locating.
3270: 2a 2a 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 ** terms in the
3280: 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 WHERE clause tha
3290: 74 20 61 72 65 20 75 73 65 66 75 6c 20 74 6f 20 t are useful to
32a0: 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 6e 65 the query planne
32b0: 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 r..*/.struct Whe
32c0: 72 65 53 63 61 6e 20 7b 0a 20 20 57 68 65 72 65 reScan {. Where
32d0: 43 6c 61 75 73 65 20 2a 70 4f 72 69 67 57 43 3b Clause *pOrigWC;
32e0: 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 /* Origina
32f0: 6c 2c 20 69 6e 6e 65 72 6d 6f 73 74 20 57 68 65 l, innermost Whe
3300: 72 65 43 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 reClause */. Wh
3310: 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 ereClause *pWC;
3320: 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 /* Wher
3330: 65 43 6c 61 75 73 65 20 63 75 72 72 65 6e 74 6c eClause currentl
3340: 79 20 62 65 69 6e 67 20 73 63 61 6e 6e 65 64 20 y being scanned
3350: 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c */. char *zColl
3360: 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 Name;
3370: 2f 2a 20 52 65 71 75 69 72 65 64 20 63 6f 6c 6c /* Required coll
3380: 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2c 20 ating sequence,
3390: 69 66 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 20 if not NULL */.
33a0: 20 63 68 61 72 20 69 64 78 61 66 66 3b 20 20 20 char idxaff;
33b0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d /* M
33c0: 75 73 74 20 6d 61 74 63 68 20 74 68 69 73 20 61 ust match this a
33d0: 66 66 69 6e 69 74 79 2c 20 69 66 20 7a 43 6f 6c ffinity, if zCol
33e0: 6c 4e 61 6d 65 21 3d 4e 55 4c 4c 20 2a 2f 0a 20 lName!=NULL */.
33f0: 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e unsigned char n
3400: 45 71 75 69 76 3b 20 20 20 20 20 20 2f 2a 20 4e Equiv; /* N
3410: 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 umber of entries
3420: 20 69 6e 20 61 45 71 75 69 76 5b 5d 20 2a 2f 0a in aEquiv[] */.
3430: 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 unsigned char
3440: 69 45 71 75 69 76 3b 20 20 20 20 20 20 2f 2a 20 iEquiv; /*
3450: 4e 65 78 74 20 75 6e 75 73 65 64 20 73 6c 6f 74 Next unused slot
3460: 20 69 6e 20 61 45 71 75 69 76 5b 5d 20 2a 2f 0a in aEquiv[] */.
3470: 20 20 75 33 32 20 6f 70 4d 61 73 6b 3b 20 20 20 u32 opMask;
3480: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3490: 41 63 63 65 70 74 61 62 6c 65 20 6f 70 65 72 61 Acceptable opera
34a0: 74 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6b 3b tors */. int k;
34b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34c0: 20 20 20 20 20 2f 2a 20 52 65 73 75 6d 65 20 73 /* Resume s
34d0: 63 61 6e 6e 69 6e 67 20 61 74 20 74 68 69 73 2d canning at this-
34e0: 3e 70 57 43 2d 3e 61 5b 74 68 69 73 2d 3e 6b 5d >pWC->a[this->k]
34f0: 20 2a 2f 0a 20 20 69 6e 74 20 61 45 71 75 69 76 */. int aEquiv
3500: 5b 32 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 [22];
3510: 20 2f 2a 20 43 75 72 73 6f 72 2c 43 6f 6c 75 6d /* Cursor,Colum
3520: 6e 20 70 61 69 72 73 20 66 6f 72 20 65 71 75 69 n pairs for equi
3530: 76 61 6c 65 6e 63 65 20 63 6c 61 73 73 65 73 20 valence classes
3540: 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 */.};../*.** An
3550: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
3560: 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
3570: 75 72 65 20 68 6f 6c 64 73 20 61 6c 6c 20 69 6e ure holds all in
3580: 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 formation about
3590: 61 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 a.** WHERE claus
35a0: 65 2e 20 20 4d 6f 73 74 6c 79 20 74 68 69 73 20 e. Mostly this
35b0: 69 73 20 61 20 63 6f 6e 74 61 69 6e 65 72 20 66 is a container f
35c0: 6f 72 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 57 or one or more W
35d0: 68 65 72 65 54 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a hereTerms..**.**
35e0: 20 45 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 Explanation of
35f0: 70 4f 75 74 65 72 3a 20 20 46 6f 72 20 61 20 57 pOuter: For a W
3600: 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
3610: 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 he form.**.**
3620: 20 20 20 20 20 20 20 20 61 20 41 4e 44 20 28 28 a AND ((
3630: 62 20 41 4e 44 20 63 29 20 4f 52 20 28 64 20 41 b AND c) OR (d A
3640: 4e 44 20 65 29 29 20 41 4e 44 20 66 0a 2a 2a 0a ND e)) AND f.**.
3650: 2a 2a 20 54 68 65 72 65 20 61 72 65 20 73 65 70 ** There are sep
3660: 61 72 61 74 65 20 57 68 65 72 65 43 6c 61 75 73 arate WhereClaus
3670: 65 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 74 68 e objects for th
3680: 65 20 77 68 6f 6c 65 20 63 6c 61 75 73 65 20 61 e whole clause a
3690: 6e 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 75 nd for.** the su
36a0: 62 63 6c 61 75 73 65 73 20 22 28 62 20 41 4e 44 bclauses "(b AND
36b0: 20 63 29 22 20 61 6e 64 20 22 28 64 20 41 4e 44 c)" and "(d AND
36c0: 20 65 29 22 2e 20 20 54 68 65 20 70 4f 75 74 65 e)". The pOute
36d0: 72 20 66 69 65 6c 64 20 6f 66 20 74 68 65 0a 2a r field of the.*
36e0: 2a 20 73 75 62 63 6c 61 75 73 65 73 20 70 6f 69 * subclauses poi
36f0: 6e 74 73 20 74 6f 20 74 68 65 20 57 68 65 72 65 nts to the Where
3700: 43 6c 61 75 73 65 20 6f 62 6a 65 63 74 20 66 6f Clause object fo
3710: 72 20 74 68 65 20 77 68 6f 6c 65 20 63 6c 61 75 r the whole clau
3720: 73 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 se..*/.struct Wh
3730: 65 72 65 43 6c 61 75 73 65 20 7b 0a 20 20 57 68 ereClause {. Wh
3740: 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b ereInfo *pWInfo;
3750: 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 /* WHERE
3760: 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e clause processin
3770: 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 g context */. W
3780: 68 65 72 65 43 6c 61 75 73 65 20 2a 70 4f 75 74 hereClause *pOut
3790: 65 72 3b 20 20 20 20 20 2f 2a 20 4f 75 74 65 72 er; /* Outer
37a0: 20 63 6f 6e 6a 75 6e 63 74 69 6f 6e 20 2a 2f 0a conjunction */.
37b0: 20 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 u8 op;
37c0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 /* Sp
37d0: 6c 69 74 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 lit operator. T
37e0: 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f 4f 52 20 2a K_AND or TK_OR *
37f0: 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 /. int nTerm;
3800: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
3810: 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 Number of terms
3820: 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 */. int nSlot;
3830: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
3840: 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 Number of entri
3850: 65 73 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20 57 es in a[] */. W
3860: 68 65 72 65 54 65 72 6d 20 2a 61 3b 20 20 20 20 hereTerm *a;
3870: 20 20 20 20 20 20 20 20 2f 2a 20 45 61 63 68 20 /* Each
3880: 61 5b 5d 20 64 65 73 63 72 69 62 65 73 20 61 20 a[] describes a
3890: 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 term of the WHER
38a0: 45 20 63 6c 75 61 73 65 20 2a 2f 0a 23 69 66 20 E cluase */.#if
38b0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 53 defined(SQLITE_S
38c0: 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 20 20 57 68 MALL_STACK). Wh
38d0: 65 72 65 54 65 72 6d 20 61 53 74 61 74 69 63 5b ereTerm aStatic[
38e0: 31 5d 3b 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 1]; /* Initia
38f0: 6c 20 73 74 61 74 69 63 20 73 70 61 63 65 20 66 l static space f
3900: 6f 72 20 61 5b 5d 20 2a 2f 0a 23 65 6c 73 65 0a or a[] */.#else.
3910: 20 20 57 68 65 72 65 54 65 72 6d 20 61 53 74 61 WhereTerm aSta
3920: 74 69 63 5b 38 5d 3b 20 20 20 20 2f 2a 20 49 6e tic[8]; /* In
3930: 69 74 69 61 6c 20 73 74 61 74 69 63 20 73 70 61 itial static spa
3940: 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23 65 ce for a[] */.#e
3950: 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 ndif.};../*.** A
3960: 20 57 68 65 72 65 54 65 72 6d 20 77 69 74 68 20 WhereTerm with
3970: 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 eOperator==WO_OR
3980: 20 68 61 73 20 69 74 73 20 75 2e 70 4f 72 49 6e has its u.pOrIn
3990: 66 6f 20 70 6f 69 6e 74 65 72 20 73 65 74 20 74 fo pointer set t
39a0: 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d 69 63 61 6c o.** a dynamical
39b0: 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 73 ly allocated ins
39c0: 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c tance of the fol
39d0: 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 lowing structure
39e0: 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 ..*/.struct Wher
39f0: 65 4f 72 49 6e 66 6f 20 7b 0a 20 20 57 68 65 72 eOrInfo {. Wher
3a00: 65 43 6c 61 75 73 65 20 77 63 3b 20 20 20 20 20 eClause wc;
3a10: 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 /* Decompos
3a20: 69 74 69 6f 6e 20 69 6e 74 6f 20 73 75 62 74 65 ition into subte
3a30: 72 6d 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b rms */. Bitmask
3a40: 20 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20 20 indexable;
3a50: 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 /* Bitmask of
3a60: 61 6c 6c 20 69 6e 64 65 78 61 62 6c 65 20 74 61 all indexable ta
3a70: 62 6c 65 73 20 69 6e 20 74 68 65 20 63 6c 61 75 bles in the clau
3a80: 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 se */.};../*.**
3a90: 41 20 57 68 65 72 65 54 65 72 6d 20 77 69 74 68 A WhereTerm with
3aa0: 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 eOperator==WO_A
3ab0: 4e 44 20 68 61 73 20 69 74 73 20 75 2e 70 41 6e ND has its u.pAn
3ac0: 64 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73 65 dInfo pointer se
3ad0: 74 20 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d 69 t to.** a dynami
3ae0: 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 cally allocated
3af0: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 instance of the
3b00: 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 following struct
3b10: 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 ure..*/.struct W
3b20: 68 65 72 65 41 6e 64 49 6e 66 6f 20 7b 0a 20 20 hereAndInfo {.
3b30: 57 68 65 72 65 43 6c 61 75 73 65 20 77 63 3b 20 WhereClause wc;
3b40: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
3b50: 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 62 72 subexpression br
3b60: 6f 6b 65 6e 20 6f 75 74 20 2a 2f 0a 7d 3b 0a 0a oken out */.};..
3b70: 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 /*.** An instanc
3b80: 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 e of the followi
3b90: 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 ng structure kee
3ba0: 70 73 20 74 72 61 63 6b 20 6f 66 20 61 20 6d 61 ps track of a ma
3bb0: 70 70 69 6e 67 0a 2a 2a 20 62 65 74 77 65 65 6e pping.** between
3bc0: 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d VDBE cursor num
3bd0: 62 65 72 73 20 61 6e 64 20 62 69 74 73 20 6f 66 bers and bits of
3be0: 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 69 6e the bitmasks in
3bf0: 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a WhereTerm..**.*
3c00: 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f * The VDBE curso
3c10: 72 20 6e 75 6d 62 65 72 73 20 61 72 65 20 73 6d r numbers are sm
3c20: 61 6c 6c 20 69 6e 74 65 67 65 72 73 20 63 6f 6e all integers con
3c30: 74 61 69 6e 65 64 20 69 6e 20 0a 2a 2a 20 53 72 tained in .** Sr
3c40: 63 4c 69 73 74 5f 69 74 65 6d 2e 69 43 75 72 73 cList_item.iCurs
3c50: 6f 72 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62 or and Expr.iTab
3c60: 6c 65 20 66 69 65 6c 64 73 2e 20 20 46 6f 72 20 le fields. For
3c70: 61 6e 79 20 67 69 76 65 6e 20 57 48 45 52 45 20 any given WHERE
3c80: 0a 2a 2a 20 63 6c 61 75 73 65 2c 20 74 68 65 20 .** clause, the
3c90: 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d cursor numbers m
3ca0: 69 67 68 74 20 6e 6f 74 20 62 65 67 69 6e 20 77 ight not begin w
3cb0: 69 74 68 20 30 20 61 6e 64 20 74 68 65 79 20 6d ith 0 and they m
3cc0: 69 67 68 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 ight.** contain
3cd0: 67 61 70 73 20 69 6e 20 74 68 65 20 6e 75 6d 62 gaps in the numb
3ce0: 65 72 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 ering sequence.
3cf0: 20 42 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20 But we want to
3d00: 6d 61 6b 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 make maximum.**
3d10: 75 73 65 20 6f 66 20 74 68 65 20 62 69 74 73 20 use of the bits
3d20: 69 6e 20 6f 75 72 20 62 69 74 6d 61 73 6b 73 2e in our bitmasks.
3d30: 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 This structure
3d40: 20 70 72 6f 76 69 64 65 73 20 61 20 6d 61 70 70 provides a mapp
3d50: 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 ing.** from the
3d60: 73 70 61 72 73 65 20 63 75 72 73 6f 72 20 6e 75 sparse cursor nu
3d70: 6d 62 65 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65 mbers into conse
3d80: 63 75 74 69 76 65 20 69 6e 74 65 67 65 72 73 20 cutive integers
3d90: 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 beginning.** wit
3da0: 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 57 68 h 0..**.** If Wh
3db0: 65 72 65 4d 61 73 6b 53 65 74 2e 69 78 5b 41 5d ereMaskSet.ix[A]
3dc0: 3d 3d 42 20 69 74 20 6d 65 61 6e 73 20 74 68 61 ==B it means tha
3dd0: 74 20 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f t The A-th bit o
3de0: 66 20 61 20 42 69 74 6d 61 73 6b 0a 2a 2a 20 63 f a Bitmask.** c
3df0: 6f 72 72 65 73 70 6f 6e 64 73 20 56 44 42 45 20 orresponds VDBE
3e00: 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 42 2e cursor number B.
3e10: 20 20 54 68 65 20 41 2d 74 68 20 62 69 74 20 6f The A-th bit o
3e20: 66 20 61 20 62 69 74 6d 61 73 6b 20 69 73 20 31 f a bitmask is 1
3e30: 3c 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 <<A..**.** For e
3e40: 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 57 xample, if the W
3e50: 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 HERE clause expr
3e60: 65 73 73 69 6f 6e 20 75 73 65 64 20 74 68 65 73 ession used thes
3e70: 65 20 56 44 42 45 0a 2a 2a 20 63 75 72 73 6f 72 e VDBE.** cursor
3e80: 73 3a 20 20 34 2c 20 35 2c 20 38 2c 20 32 39 2c s: 4, 5, 8, 29,
3e90: 20 35 37 2c 20 37 33 2e 20 20 54 68 65 6e 20 74 57, 73. Then t
3ea0: 68 65 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 he WhereMaskSet
3eb0: 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 6f structure.** wo
3ec0: 75 6c 64 20 6d 61 70 20 74 68 6f 73 65 20 63 75 uld map those cu
3ed0: 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 rsor numbers int
3ee0: 6f 20 62 69 74 73 20 30 20 74 68 72 6f 75 67 68 o bits 0 through
3ef0: 20 35 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 5..**.** Note t
3f00: 68 61 74 20 74 68 65 20 6d 61 70 70 69 6e 67 20 hat the mapping
3f10: 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 is not necessari
3f20: 6c 79 20 6f 72 64 65 72 65 64 2e 20 20 49 6e 20 ly ordered. In
3f30: 74 68 65 20 65 78 61 6d 70 6c 65 0a 2a 2a 20 61 the example.** a
3f40: 62 6f 76 65 2c 20 74 68 65 20 6d 61 70 70 69 6e bove, the mappin
3f50: 67 20 6d 69 67 68 74 20 67 6f 20 6c 69 6b 65 20 g might go like
3f60: 74 68 69 73 3a 20 20 34 2d 3e 33 2c 20 35 2d 3e this: 4->3, 5->
3f70: 31 2c 20 38 2d 3e 32 2c 20 32 39 2d 3e 30 2c 0a 1, 8->2, 29->0,.
3f80: 2a 2a 20 35 37 2d 3e 35 2c 20 37 33 2d 3e 34 2e ** 57->5, 73->4.
3f90: 20 20 4f 72 20 6f 6e 65 20 6f 66 20 37 31 39 20 Or one of 719
3fa0: 6f 74 68 65 72 20 63 6f 6d 62 69 6e 61 74 69 6f other combinatio
3fb0: 6e 73 20 6d 69 67 68 74 20 62 65 20 75 73 65 64 ns might be used
3fc0: 2e 20 49 74 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 . It.** does not
3fd0: 20 72 65 61 6c 6c 79 20 6d 61 74 74 65 72 2e 20 really matter.
3fe0: 20 57 68 61 74 20 69 73 20 69 6d 70 6f 72 74 61 What is importa
3ff0: 6e 74 20 69 73 20 74 68 61 74 20 73 70 61 72 73 nt is that spars
4000: 65 20 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 e cursor.** numb
4010: 65 72 73 20 61 6c 6c 20 67 65 74 20 6d 61 70 70 ers all get mapp
4020: 65 64 20 69 6e 74 6f 20 62 69 74 20 6e 75 6d 62 ed into bit numb
4030: 65 72 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 ers that begin w
4040: 69 74 68 20 30 20 61 6e 64 20 63 6f 6e 74 61 69 ith 0 and contai
4050: 6e 0a 2a 2a 20 6e 6f 20 67 61 70 73 2e 0a 2a 2f n.** no gaps..*/
4060: 0a 73 74 72 75 63 74 20 57 68 65 72 65 4d 61 73 .struct WhereMas
4070: 6b 53 65 74 20 7b 0a 20 20 69 6e 74 20 6e 3b 20 kSet {. int n;
4080: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4090: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
40a0: 20 6f 66 20 61 73 73 69 67 6e 65 64 20 63 75 72 of assigned cur
40b0: 73 6f 72 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 sor values */.
40c0: 69 6e 74 20 69 78 5b 42 4d 53 5d 3b 20 20 20 20 int ix[BMS];
40d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
40e0: 20 43 75 72 73 6f 72 20 61 73 73 69 67 6e 65 64 Cursor assigned
40f0: 20 74 6f 20 65 61 63 68 20 62 69 74 20 2a 2f 0a to each bit */.
4100: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6f };../*.** This o
4110: 62 6a 65 63 74 20 69 73 20 61 20 63 6f 6e 76 65 bject is a conve
4120: 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72 20 68 nience wrapper h
4130: 6f 6c 64 69 6e 67 20 61 6c 6c 20 69 6e 66 6f 72 olding all infor
4140: 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 0a 2a 2a mation needed.**
4150: 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 57 68 to construct Wh
4160: 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 20 ereLoop objects
4170: 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 for a particular
4180: 20 71 75 65 72 79 2e 0a 2a 2f 0a 73 74 72 75 63 query..*/.struc
4190: 74 20 57 68 65 72 65 4c 6f 6f 70 42 75 69 6c 64 t WhereLoopBuild
41a0: 65 72 20 7b 0a 20 20 57 68 65 72 65 49 6e 66 6f er {. WhereInfo
41b0: 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 *pWInfo;
41c0: 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 /* Information
41d0: 61 62 6f 75 74 20 74 68 69 73 20 57 48 45 52 45 about this WHERE
41e0: 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 */. WhereClaus
41f0: 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20 20 20 e *pWC;
4200: 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 /* WHERE clause
4210: 74 65 72 6d 73 20 2a 2f 0a 20 20 45 78 70 72 4c terms */. ExprL
4220: 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b 20 20 ist *pOrderBy;
4230: 20 20 20 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 /* ORDER BY
4240: 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 clause */. Whe
4250: 72 65 4c 6f 6f 70 20 2a 70 4e 65 77 3b 20 20 20 reLoop *pNew;
4260: 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6c 61 /* Templa
4270: 74 65 20 57 68 65 72 65 4c 6f 6f 70 20 2a 2f 0a te WhereLoop */.
4280: 20 20 57 68 65 72 65 4f 72 53 65 74 20 2a 70 4f WhereOrSet *pO
4290: 72 53 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 rSet; /* R
42a0: 65 63 6f 72 64 20 62 65 73 74 20 6c 6f 6f 70 73 ecord best loops
42b0: 20 68 65 72 65 2c 20 69 66 20 6e 6f 74 20 4e 55 here, if not NU
42c0: 4c 4c 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c LL */.#ifdef SQL
42d0: 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
42e0: 5f 4f 52 5f 53 54 41 54 34 0a 20 20 55 6e 70 61 _OR_STAT4. Unpa
42f0: 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 52 65 63 ckedRecord *pRec
4300: 3b 20 20 20 20 20 2f 2a 20 50 72 6f 62 65 20 66 ; /* Probe f
4310: 6f 72 20 73 74 61 74 34 20 28 69 66 20 72 65 71 or stat4 (if req
4320: 75 69 72 65 64 29 20 2a 2f 0a 20 20 69 6e 74 20 uired) */. int
4330: 6e 52 65 63 56 61 6c 69 64 3b 20 20 20 20 20 20 nRecValid;
4340: 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 /* Number
4350: 6f 66 20 76 61 6c 69 64 20 66 69 65 6c 64 73 20 of valid fields
4360: 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 52 65 currently in pRe
4370: 63 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a c */.#endif.};..
4380: 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20 /*.** The WHERE
4390: 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e clause processin
43a0: 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77 g routine has tw
43b0: 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a o halves. The.*
43c0: 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65 * first part doe
43d0: 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 s the start of t
43e0: 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e he WHERE loop an
43f0: 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 d the second.**
4400: 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61 half does the ta
4410: 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 il of the WHERE
4420: 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e loop. An instan
4430: 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 ce of.** this st
4440: 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72 ructure is retur
4450: 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74 ned by the first
4460: 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64 half and passed
4470: 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63 .** into the sec
4480: 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65 ond half to give
4490: 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79 some continuity
44a0: 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ..**.** An insta
44b0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 nce of this obje
44c0: 63 74 20 68 6f 6c 64 73 20 74 68 65 20 63 6f 6d ct holds the com
44d0: 70 6c 65 74 65 20 73 74 61 74 65 20 6f 66 20 74 plete state of t
44e0: 68 65 20 71 75 65 72 79 0a 2a 2a 20 70 6c 61 6e he query.** plan
44f0: 6e 65 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 ner..*/.struct W
4500: 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 hereInfo {. Par
4510: 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 se *pParse;
4520: 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
4530: 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 g and code gener
4540: 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f ating context */
4550: 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 . SrcList *pTab
4560: 4c 69 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 List; /*
4570: 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 List of tables i
4580: 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 n the join */.
4590: 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
45a0: 42 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 By; /* The
45b0: 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
45c0: 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 or NULL */. Ex
45d0: 70 72 4c 69 73 74 20 2a 70 52 65 73 75 6c 74 53 prList *pResultS
45e0: 65 74 3b 20 20 20 20 20 2f 2a 20 52 65 73 75 6c et; /* Resul
45f0: 74 20 73 65 74 2e 20 44 49 53 54 49 4e 43 54 20 t set. DISTINCT
4600: 6f 70 65 72 61 74 65 73 20 6f 6e 20 74 68 65 73 operates on thes
4610: 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c 6f 6f 70 e */. WhereLoop
4620: 20 2a 70 4c 6f 6f 70 73 3b 20 20 20 20 20 20 20 *pLoops;
4630: 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c 20 /* List of all
4640: 57 68 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 WhereLoop object
4650: 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 72 s */. Bitmask r
4660: 65 76 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 evMask;
4670: 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4f 52 44 45 /* Mask of ORDE
4680: 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 R BY terms that
4690: 6e 65 65 64 20 72 65 76 65 72 73 69 6e 67 20 2a need reversing *
46a0: 2f 0a 20 20 4c 6f 67 45 73 74 20 6e 52 6f 77 4f /. LogEst nRowO
46b0: 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a ut; /*
46c0: 20 45 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 Estimated numbe
46d0: 72 20 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 r of output rows
46e0: 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46 */. u16 wctrlF
46f0: 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 lags;
4700: 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69 6e 61 /* Flags origina
4710: 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20 73 71 lly passed to sq
4720: 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 lite3WhereBegin(
4730: 29 20 2a 2f 0a 20 20 75 38 20 62 4f 42 53 61 74 ) */. u8 bOBSat
4740: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
4750: 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 73 61 74 /* ORDER BY sat
4760: 69 73 66 69 65 64 20 62 79 20 69 6e 64 69 63 65 isfied by indice
4770: 73 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e 65 50 s */. u8 okOneP
4780: 61 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 ass;
4790: 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e /* Ok to use on
47a0: 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d e-pass algorithm
47b0: 20 66 6f 72 20 55 50 44 41 54 45 2f 44 45 4c 45 for UPDATE/DELE
47c0: 54 45 20 2a 2f 0a 20 20 75 38 20 75 6e 74 65 73 TE */. u8 untes
47d0: 74 65 64 54 65 72 6d 73 3b 20 20 20 20 20 20 20 tedTerms;
47e0: 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 20 57 48 45 /* Not all WHE
47f0: 52 45 20 74 65 72 6d 73 20 72 65 73 6f 6c 76 65 RE terms resolve
4800: 64 20 62 79 20 6f 75 74 65 72 20 6c 6f 6f 70 20 d by outer loop
4810: 2a 2f 0a 20 20 75 38 20 65 44 69 73 74 69 6e 63 */. u8 eDistinc
4820: 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f t; /
4830: 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48 45 * One of the WHE
4840: 52 45 5f 44 49 53 54 49 4e 43 54 5f 2a 20 76 61 RE_DISTINCT_* va
4850: 6c 75 65 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 lues below */.
4860: 75 38 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 u8 nLevel;
4870: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
4880: 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 6c 6f ber of nested lo
4890: 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 70 op */. int iTop
48a0: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
48b0: 20 20 2f 2a 20 54 68 65 20 76 65 72 79 20 62 65 /* The very be
48c0: 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 57 ginning of the W
48d0: 48 45 52 45 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 HERE loop */. i
48e0: 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b 20 20 20 nt iContinue;
48f0: 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 /* Jump
4900: 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 here to continu
4910: 65 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f e with next reco
4920: 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 rd */. int iBre
4930: 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ak;
4940: 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
4950: 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 o break out of t
4960: 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
4970: 20 73 61 76 65 64 4e 51 75 65 72 79 4c 6f 6f 70 savedNQueryLoop
4980: 3b 20 20 20 20 20 20 2f 2a 20 70 50 61 72 73 65 ; /* pParse
4990: 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70 20 6f 75 74 ->nQueryLoop out
49a0: 73 69 64 65 20 74 68 65 20 57 48 45 52 45 20 6c side the WHERE l
49b0: 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 oop */. WhereMa
49c0: 73 6b 53 65 74 20 73 4d 61 73 6b 53 65 74 3b 20 skSet sMaskSet;
49d0: 20 20 20 2f 2a 20 4d 61 70 20 63 75 72 73 6f 72 /* Map cursor
49e0: 20 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74 6d numbers to bitm
49f0: 61 73 6b 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 asks */. WhereC
4a00: 6c 61 75 73 65 20 73 57 43 3b 20 20 20 20 20 20 lause sWC;
4a10: 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 /* Decomposi
4a20: 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 48 45 52 tion of the WHER
4a30: 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 E clause */. Wh
4a40: 65 72 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20 ereLevel a[1];
4a50: 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 /* Infor
4a60: 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 mation about eac
4a70: 68 20 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20 57 h nest loop in W
4a80: 48 45 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a HERE */.};../*.*
4a90: 2a 20 42 69 74 6d 61 73 6b 73 20 66 6f 72 20 74 * Bitmasks for t
4aa0: 68 65 20 6f 70 65 72 61 74 6f 72 73 20 6f 6e 20 he operators on
4ab0: 57 68 65 72 65 54 65 72 6d 20 6f 62 6a 65 63 74 WhereTerm object
4ac0: 73 2e 20 20 54 68 65 73 65 20 61 72 65 20 61 6c s. These are al
4ad0: 6c 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 73 20 74 l.** operators t
4ae0: 68 61 74 20 61 72 65 20 6f 66 20 69 6e 74 65 72 hat are of inter
4af0: 65 73 74 20 74 6f 20 74 68 65 20 71 75 65 72 79 est to the query
4b00: 20 70 6c 61 6e 6e 65 72 2e 20 20 41 6e 0a 2a 2a planner. An.**
4b10: 20 4f 52 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69 OR-ed combinati
4b20: 6f 6e 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75 on of these valu
4b30: 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 es can be used w
4b40: 68 65 6e 20 73 65 61 72 63 68 69 6e 67 20 66 6f hen searching fo
4b50: 72 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 r.** particular
4b60: 57 68 65 72 65 54 65 72 6d 73 20 77 69 74 68 69 WhereTerms withi
4b70: 6e 20 61 20 57 68 65 72 65 43 6c 61 75 73 65 2e n a WhereClause.
4b80: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 49 .*/.#define WO_I
4b90: 4e 20 20 20 20 20 30 78 30 30 31 0a 23 64 65 66 N 0x001.#def
4ba0: 69 6e 65 20 57 4f 5f 45 51 20 20 20 20 20 30 78 ine WO_EQ 0x
4bb0: 30 30 32 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c 002.#define WO_L
4bc0: 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 T (WO_EQ<<(T
4bd0: 4b 5f 4c 54 2d 54 4b 5f 45 51 29 29 0a 23 64 65 K_LT-TK_EQ)).#de
4be0: 66 69 6e 65 20 57 4f 5f 4c 45 20 20 20 20 20 28 fine WO_LE (
4bf0: 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 45 2d 54 4b WO_EQ<<(TK_LE-TK
4c00: 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f _EQ)).#define WO
4c10: 5f 47 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c _GT (WO_EQ<<
4c20: 28 54 4b 5f 47 54 2d 54 4b 5f 45 51 29 29 0a 23 (TK_GT-TK_EQ)).#
4c30: 64 65 66 69 6e 65 20 57 4f 5f 47 45 20 20 20 20 define WO_GE
4c40: 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 45 2d (WO_EQ<<(TK_GE-
4c50: 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 TK_EQ)).#define
4c60: 57 4f 5f 4d 41 54 43 48 20 20 30 78 30 34 30 0a WO_MATCH 0x040.
4c70: 23 64 65 66 69 6e 65 20 57 4f 5f 49 53 4e 55 4c #define WO_ISNUL
4c80: 4c 20 30 78 30 38 30 0a 23 64 65 66 69 6e 65 20 L 0x080.#define
4c90: 57 4f 5f 4f 52 20 20 20 20 20 30 78 31 30 30 20 WO_OR 0x100
4ca0: 20 20 20 20 20 20 2f 2a 20 54 77 6f 20 6f 72 20 /* Two or
4cb0: 6d 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 more OR-connecte
4cc0: 64 20 74 65 72 6d 73 20 2a 2f 0a 23 64 65 66 69 d terms */.#defi
4cd0: 6e 65 20 57 4f 5f 41 4e 44 20 20 20 20 30 78 32 ne WO_AND 0x2
4ce0: 30 30 20 20 20 20 20 20 20 2f 2a 20 54 77 6f 20 00 /* Two
4cf0: 6f 72 20 6d 6f 72 65 20 41 4e 44 2d 63 6f 6e 6e or more AND-conn
4d00: 65 63 74 65 64 20 74 65 72 6d 73 20 2a 2f 0a 23 ected terms */.#
4d10: 64 65 66 69 6e 65 20 57 4f 5f 45 51 55 49 56 20 define WO_EQUIV
4d20: 20 30 78 34 30 30 20 20 20 20 20 20 20 2f 2a 20 0x400 /*
4d30: 4f 66 20 74 68 65 20 66 6f 72 6d 20 41 3d 3d 42 Of the form A==B
4d40: 2c 20 62 6f 74 68 20 63 6f 6c 75 6d 6e 73 20 2a , both columns *
4d50: 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4e 4f 4f /.#define WO_NOO
4d60: 50 20 20 20 30 78 38 30 30 20 20 20 20 20 20 20 P 0x800
4d70: 2f 2a 20 54 68 69 73 20 74 65 72 6d 20 64 6f 65 /* This term doe
4d80: 73 20 6e 6f 74 20 72 65 73 74 72 69 63 74 20 73 s not restrict s
4d90: 65 61 72 63 68 20 73 70 61 63 65 20 2a 2f 0a 0a earch space */..
4da0: 23 64 65 66 69 6e 65 20 57 4f 5f 41 4c 4c 20 20 #define WO_ALL
4db0: 20 20 30 78 66 66 66 20 20 20 20 20 20 20 2f 2a 0xfff /*
4dc0: 20 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 70 6f 73 Mask of all pos
4dd0: 73 69 62 6c 65 20 57 4f 5f 2a 20 76 61 6c 75 65 sible WO_* value
4de0: 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f s */.#define WO_
4df0: 53 49 4e 47 4c 45 20 30 78 30 66 66 20 20 20 20 SINGLE 0x0ff
4e00: 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c /* Mask of al
4e10: 6c 20 6e 6f 6e 2d 63 6f 6d 70 6f 75 6e 64 20 57 l non-compound W
4e20: 4f 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 0a 2f O_* values */../
4e30: 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 64 *.** These are d
4e40: 65 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 62 69 efinitions of bi
4e50: 74 73 20 69 6e 20 74 68 65 20 57 68 65 72 65 4c ts in the WhereL
4e60: 6f 6f 70 2e 77 73 46 6c 61 67 73 20 66 69 65 6c oop.wsFlags fiel
4e70: 64 2e 0a 2a 2a 20 54 68 65 20 70 61 72 74 69 63 d..** The partic
4e80: 75 6c 61 72 20 63 6f 6d 62 69 6e 61 74 69 6f 6e ular combination
4e90: 20 6f 66 20 62 69 74 73 20 69 6e 20 65 61 63 68 of bits in each
4ea0: 20 57 68 65 72 65 4c 6f 6f 70 20 68 65 6c 70 20 WhereLoop help
4eb0: 74 6f 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65 20 to.** determine
4ec0: 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 20 74 68 the algorithm th
4ed0: 61 74 20 57 68 65 72 65 4c 6f 6f 70 20 72 65 70 at WhereLoop rep
4ee0: 72 65 73 65 6e 74 73 2e 0a 2a 2f 0a 23 64 65 66 resents..*/.#def
4ef0: 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e ine WHERE_COLUMN
4f00: 5f 45 51 20 20 20 20 30 78 30 30 30 30 30 30 30 _EQ 0x0000000
4f10: 31 20 20 2f 2a 20 78 3d 45 58 50 52 20 2a 2f 0a 1 /* x=EXPR */.
4f20: 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f #define WHERE_CO
4f30: 4c 55 4d 4e 5f 52 41 4e 47 45 20 30 78 30 30 30 LUMN_RANGE 0x000
4f40: 30 30 30 30 32 20 20 2f 2a 20 78 3c 45 58 50 52 00002 /* x<EXPR
4f50: 20 61 6e 64 2f 6f 72 20 78 3e 45 58 50 52 20 2a and/or x>EXPR *
4f60: 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
4f70: 43 4f 4c 55 4d 4e 5f 49 4e 20 20 20 20 30 78 30 COLUMN_IN 0x0
4f80: 30 30 30 30 30 30 34 20 20 2f 2a 20 78 20 49 4e 0000004 /* x IN
4f90: 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e (...) */.#defin
4fa0: 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e e WHERE_COLUMN_N
4fb0: 55 4c 4c 20 20 30 78 30 30 30 30 30 30 30 38 20 ULL 0x00000008
4fc0: 20 2f 2a 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f /* x IS NULL */
4fd0: 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 .#define WHERE_C
4fe0: 4f 4e 53 54 52 41 49 4e 54 20 20 20 30 78 30 30 ONSTRAINT 0x00
4ff0: 30 30 30 30 30 66 20 20 2f 2a 20 41 6e 79 20 6f 00000f /* Any o
5000: 66 20 74 68 65 20 57 48 45 52 45 5f 43 4f 4c 55 f the WHERE_COLU
5010: 4d 4e 5f 78 78 78 20 76 61 6c 75 65 73 20 2a 2f MN_xxx values */
5020: 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 54 .#define WHERE_T
5030: 4f 50 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30 OP_LIMIT 0x00
5040: 30 30 30 30 31 30 20 20 2f 2a 20 78 3c 45 58 50 000010 /* x<EXP
5050: 52 20 6f 72 20 78 3c 3d 45 58 50 52 20 63 6f 6e R or x<=EXPR con
5060: 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 straint */.#defi
5070: 6e 65 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d ne WHERE_BTM_LIM
5080: 49 54 20 20 20 20 30 78 30 30 30 30 30 30 32 30 IT 0x00000020
5090: 20 20 2f 2a 20 78 3e 45 58 50 52 20 6f 72 20 78 /* x>EXPR or x
50a0: 3e 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e >=EXPR constrain
50b0: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 t */.#define WHE
50c0: 52 45 5f 42 4f 54 48 5f 4c 49 4d 49 54 20 20 20 RE_BOTH_LIMIT
50d0: 30 78 30 30 30 30 30 30 33 30 20 20 2f 2a 20 42 0x00000030 /* B
50e0: 6f 74 68 20 78 3e 45 58 50 52 20 61 6e 64 20 78 oth x>EXPR and x
50f0: 3c 45 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e 65 <EXPR */.#define
5100: 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 WHERE_IDX_ONLY
5110: 20 20 20 20 30 78 30 30 30 30 30 30 34 30 20 20 0x00000040
5120: 2f 2a 20 55 73 65 20 69 6e 64 65 78 20 6f 6e 6c /* Use index onl
5130: 79 20 2d 20 6f 6d 69 74 20 74 61 62 6c 65 20 2a y - omit table *
5140: 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
5150: 49 50 4b 20 20 20 20 20 20 20 20 20 20 30 78 30 IPK 0x0
5160: 30 30 30 30 31 30 30 20 20 2f 2a 20 78 20 69 73 0000100 /* x is
5170: 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 the INTEGER PRI
5180: 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 23 64 65 66 MARY KEY */.#def
5190: 69 6e 65 20 57 48 45 52 45 5f 49 4e 44 45 58 45 ine WHERE_INDEXE
51a0: 44 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30 D 0x0000020
51b0: 30 20 20 2f 2a 20 57 68 65 72 65 4c 6f 6f 70 2e 0 /* WhereLoop.
51c0: 75 2e 62 74 72 65 65 2e 70 49 6e 64 65 78 20 69 u.btree.pIndex i
51d0: 73 20 76 61 6c 69 64 20 2a 2f 0a 23 64 65 66 69 s valid */.#defi
51e0: 6e 65 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c ne WHERE_VIRTUAL
51f0: 54 41 42 4c 45 20 30 78 30 30 30 30 30 34 30 30 TABLE 0x00000400
5200: 20 20 2f 2a 20 57 68 65 72 65 4c 6f 6f 70 2e 75 /* WhereLoop.u
5210: 2e 76 74 61 62 20 69 73 20 76 61 6c 69 64 20 2a .vtab is valid *
5220: 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f /.#define WHERE_
5230: 49 4e 5f 41 42 4c 45 20 20 20 20 20 20 30 78 30 IN_ABLE 0x0
5240: 30 30 30 30 38 30 30 20 20 2f 2a 20 41 62 6c 65 0000800 /* Able
5250: 20 74 6f 20 73 75 70 70 6f 72 74 20 61 6e 20 49 to support an I
5260: 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 23 64 N operator */.#d
5270: 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45 52 efine WHERE_ONER
5280: 4f 57 20 20 20 20 20 20 20 30 78 30 30 30 30 31 OW 0x00001
5290: 30 30 30 20 20 2f 2a 20 53 65 6c 65 63 74 73 20 000 /* Selects
52a0: 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 no more than one
52b0: 20 72 6f 77 20 2a 2f 0a 23 64 65 66 69 6e 65 20 row */.#define
52c0: 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 20 WHERE_MULTI_OR
52d0: 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 0x00002000 /
52e0: 2a 20 4f 52 20 75 73 69 6e 67 20 6d 75 6c 74 69 * OR using multi
52f0: 70 6c 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 23 ple indices */.#
5300: 64 65 66 69 6e 65 20 57 48 45 52 45 5f 41 55 54 define WHERE_AUT
5310: 4f 5f 49 4e 44 45 58 20 20 20 30 78 30 30 30 30 O_INDEX 0x0000
5320: 34 30 30 30 20 20 2f 2a 20 55 73 65 73 20 61 6e 4000 /* Uses an
5330: 20 65 70 68 65 6d 65 72 61 6c 20 69 6e 64 65 78 ephemeral index
5340: 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 */../*.** Retur
5350: 6e 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 20 n the estimated
5360: 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74 number of output
5370: 20 72 6f 77 73 20 66 72 6f 6d 20 61 20 57 48 45 rows from a WHE
5380: 52 45 20 63 6c 61 75 73 65 0a 2a 2f 0a 75 36 34 RE clause.*/.u64
5390: 20 73 71 6c 69 74 65 33 57 68 65 72 65 4f 75 74 sqlite3WhereOut
53a0: 70 75 74 52 6f 77 43 6f 75 6e 74 28 57 68 65 72 putRowCount(Wher
53b0: 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a eInfo *pWInfo){.
53c0: 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 return sqlite3
53d0: 4c 6f 67 45 73 74 54 6f 49 6e 74 28 70 57 49 6e LogEstToInt(pWIn
53e0: 66 6f 2d 3e 6e 52 6f 77 4f 75 74 29 3b 0a 7d 0a fo->nRowOut);.}.
53f0: 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6f 6e ./*.** Return on
5400: 65 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f 44 e of the WHERE_D
5410: 49 53 54 49 4e 43 54 5f 78 78 78 78 78 20 76 61 ISTINCT_xxxxx va
5420: 6c 75 65 73 20 74 6f 20 69 6e 64 69 63 61 74 65 lues to indicate
5430: 20 68 6f 77 20 74 68 69 73 0a 2a 2a 20 57 48 45 how this.** WHE
5440: 52 45 20 63 6c 61 75 73 65 20 72 65 74 75 72 6e RE clause return
5450: 73 20 6f 75 74 70 75 74 73 20 66 6f 72 20 44 49 s outputs for DI
5460: 53 54 49 4e 43 54 20 70 72 6f 63 65 73 73 69 6e STINCT processin
5470: 67 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 g..*/.int sqlite
5480: 33 57 68 65 72 65 49 73 44 69 73 74 69 6e 63 74 3WhereIsDistinct
5490: 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e (WhereInfo *pWIn
54a0: 66 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 57 fo){. return pW
54b0: 49 6e 66 6f 2d 3e 65 44 69 73 74 69 6e 63 74 3b Info->eDistinct;
54c0: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
54d0: 20 54 52 55 45 20 69 66 20 74 68 65 20 57 48 45 TRUE if the WHE
54e0: 52 45 20 63 6c 61 75 73 65 20 72 65 74 75 72 6e RE clause return
54f0: 73 20 72 6f 77 73 20 69 6e 20 4f 52 44 45 52 20 s rows in ORDER
5500: 42 59 20 6f 72 64 65 72 2e 0a 2a 2a 20 52 65 74 BY order..** Ret
5510: 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 urn FALSE if the
5520: 20 6f 75 74 70 75 74 20 6e 65 65 64 73 20 74 6f output needs to
5530: 20 62 65 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a 69 be sorted..*/.i
5540: 6e 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 49 nt sqlite3WhereI
5550: 73 4f 72 64 65 72 65 64 28 57 68 65 72 65 49 6e sOrdered(WhereIn
5560: 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 72 fo *pWInfo){. r
5570: 65 74 75 72 6e 20 70 57 49 6e 66 6f 2d 3e 62 4f eturn pWInfo->bO
5580: 42 53 61 74 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a BSat!=0;.}../*.*
5590: 2a 20 52 65 74 75 72 6e 20 74 68 65 20 56 44 42 * Return the VDB
55a0: 45 20 61 64 64 72 65 73 73 20 6f 72 20 6c 61 62 E address or lab
55b0: 65 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 6e el to jump to in
55c0: 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 69 6e order to contin
55d0: 75 65 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c ue.** immediatel
55e0: 79 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 y with the next
55f0: 72 6f 77 20 6f 66 20 61 20 57 48 45 52 45 20 63 row of a WHERE c
5600: 6c 61 75 73 65 2e 0a 2a 2f 0a 69 6e 74 20 73 71 lause..*/.int sq
5610: 6c 69 74 65 33 57 68 65 72 65 43 6f 6e 74 69 6e lite3WhereContin
5620: 75 65 4c 61 62 65 6c 28 57 68 65 72 65 49 6e 66 ueLabel(WhereInf
5630: 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 72 65 o *pWInfo){. re
5640: 74 75 72 6e 20 70 57 49 6e 66 6f 2d 3e 69 43 6f turn pWInfo->iCo
5650: 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ntinue;.}../*.**
5660: 20 52 65 74 75 72 6e 20 74 68 65 20 56 44 42 45 Return the VDBE
5670: 20 61 64 64 72 65 73 73 20 6f 72 20 6c 61 62 65 address or labe
5680: 6c 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 6e 20 l to jump to in
5690: 6f 72 64 65 72 20 74 6f 20 62 72 65 61 6b 0a 2a order to break.*
56a0: 2a 20 6f 75 74 20 6f 66 20 61 20 57 48 45 52 45 * out of a WHERE
56b0: 20 6c 6f 6f 70 2e 0a 2a 2f 0a 69 6e 74 20 73 71 loop..*/.int sq
56c0: 6c 69 74 65 33 57 68 65 72 65 42 72 65 61 6b 4c lite3WhereBreakL
56d0: 61 62 65 6c 28 57 68 65 72 65 49 6e 66 6f 20 2a abel(WhereInfo *
56e0: 70 57 49 6e 66 6f 29 7b 0a 20 20 72 65 74 75 72 pWInfo){. retur
56f0: 6e 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b n pWInfo->iBreak
5700: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 ;.}../*.** Retur
5710: 6e 20 54 52 55 45 20 69 66 20 61 6e 20 55 50 44 n TRUE if an UPD
5720: 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 ATE or DELETE st
5730: 61 74 65 6d 65 6e 74 20 63 61 6e 20 6f 70 65 72 atement can oper
5740: 61 74 65 20 64 69 72 65 63 74 6c 79 20 6f 6e 0a ate directly on.
5750: 2a 2a 20 74 68 65 20 72 6f 77 69 64 73 20 72 65 ** the rowids re
5760: 74 75 72 6e 65 64 20 62 79 20 61 20 57 48 45 52 turned by a WHER
5770: 45 20 63 6c 61 75 73 65 2e 20 20 52 65 74 75 72 E clause. Retur
5780: 6e 20 46 41 4c 53 45 20 69 66 20 64 6f 69 6e 67 n FALSE if doing
5790: 20 61 6e 0a 2a 2a 20 55 50 44 41 54 45 20 6f 72 an.** UPDATE or
57a0: 20 44 45 4c 45 54 45 20 6d 69 67 68 74 20 63 68 DELETE might ch
57b0: 61 6e 67 65 20 73 75 62 73 65 71 75 65 6e 74 20 ange subsequent
57c0: 57 48 45 52 45 20 63 6c 61 75 73 65 20 72 65 73 WHERE clause res
57d0: 75 6c 74 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c ults..*/.int sql
57e0: 69 74 65 33 57 68 65 72 65 4f 6b 4f 6e 65 50 61 ite3WhereOkOnePa
57f0: 73 73 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 ss(WhereInfo *pW
5800: 49 6e 66 6f 29 7b 0a 20 20 72 65 74 75 72 6e 20 Info){. return
5810: 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 pWInfo->okOnePas
5820: 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 s;.}../*.** Move
5830: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
5840: 70 53 72 63 20 69 6e 74 6f 20 70 44 65 73 74 0a pSrc into pDest.
5850: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 */.static void w
5860: 68 65 72 65 4f 72 4d 6f 76 65 28 57 68 65 72 65 hereOrMove(Where
5870: 4f 72 53 65 74 20 2a 70 44 65 73 74 2c 20 57 68 OrSet *pDest, Wh
5880: 65 72 65 4f 72 53 65 74 20 2a 70 53 72 63 29 7b ereOrSet *pSrc){
5890: 0a 20 20 70 44 65 73 74 2d 3e 6e 20 3d 20 70 53 . pDest->n = pS
58a0: 72 63 2d 3e 6e 3b 0a 20 20 6d 65 6d 63 70 79 28 rc->n;. memcpy(
58b0: 70 44 65 73 74 2d 3e 61 2c 20 70 53 72 63 2d 3e pDest->a, pSrc->
58c0: 61 2c 20 70 44 65 73 74 2d 3e 6e 2a 73 69 7a 65 a, pDest->n*size
58d0: 6f 66 28 70 44 65 73 74 2d 3e 61 5b 30 5d 29 29 of(pDest->a[0]))
58e0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 ;.}../*.** Try t
58f0: 6f 20 69 6e 73 65 72 74 20 61 20 6e 65 77 20 70 o insert a new p
5900: 72 65 72 65 71 75 69 73 69 74 65 2f 63 6f 73 74 rerequisite/cost
5910: 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 entry into the
5920: 57 68 65 72 65 4f 72 53 65 74 20 70 53 65 74 2e WhereOrSet pSet.
5930: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 65 .**.** The new e
5940: 6e 74 72 79 20 6d 69 67 68 74 20 6f 76 65 72 77 ntry might overw
5950: 72 69 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 rite an existing
5960: 20 65 6e 74 72 79 2c 20 6f 72 20 69 74 20 6d 69 entry, or it mi
5970: 67 68 74 20 62 65 0a 2a 2a 20 61 70 70 65 6e 64 ght be.** append
5980: 65 64 2c 20 6f 72 20 69 74 20 6d 69 67 68 74 20 ed, or it might
5990: 62 65 20 64 69 73 63 61 72 64 65 64 2e 20 20 44 be discarded. D
59a0: 6f 20 77 68 61 74 65 76 65 72 20 69 73 20 74 68 o whatever is th
59b0: 65 20 72 69 67 68 74 20 74 68 69 6e 67 0a 2a 2a e right thing.**
59c0: 20 73 6f 20 74 68 61 74 20 70 53 65 74 20 6b 65 so that pSet ke
59d0: 65 70 73 20 74 68 65 20 4e 5f 4f 52 5f 43 4f 53 eps the N_OR_COS
59e0: 54 20 62 65 73 74 20 65 6e 74 72 69 65 73 20 73 T best entries s
59f0: 65 65 6e 20 73 6f 20 66 61 72 2e 0a 2a 2f 0a 73 een so far..*/.s
5a00: 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 4f tatic int whereO
5a10: 72 49 6e 73 65 72 74 28 0a 20 20 57 68 65 72 65 rInsert(. Where
5a20: 4f 72 53 65 74 20 2a 70 53 65 74 2c 20 20 20 20 OrSet *pSet,
5a30: 20 20 2f 2a 20 54 68 65 20 57 68 65 72 65 4f 72 /* The WhereOr
5a40: 53 65 74 20 74 6f 20 62 65 20 75 70 64 61 74 65 Set to be update
5a50: 64 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 d */. Bitmask p
5a60: 72 65 72 65 71 2c 20 20 20 20 20 20 20 20 2f 2a rereq, /*
5a70: 20 50 72 65 72 65 71 75 69 73 69 74 65 73 20 6f Prerequisites o
5a80: 66 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 f the new entry
5a90: 2a 2f 0a 20 20 4c 6f 67 45 73 74 20 72 52 75 6e */. LogEst rRun
5aa0: 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 , /* R
5ab0: 75 6e 2d 63 6f 73 74 20 6f 66 20 74 68 65 20 6e un-cost of the n
5ac0: 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 4c 6f ew entry */. Lo
5ad0: 67 45 73 74 20 6e 4f 75 74 20 20 20 20 20 20 20 gEst nOut
5ae0: 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
5af0: 66 20 6f 75 74 70 75 74 73 20 66 6f 72 20 74 68 f outputs for th
5b00: 65 20 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 29 e new entry */.)
5b10: 7b 0a 20 20 75 31 36 20 69 3b 0a 20 20 57 68 65 {. u16 i;. Whe
5b20: 72 65 4f 72 43 6f 73 74 20 2a 70 3b 0a 20 20 66 reOrCost *p;. f
5b30: 6f 72 28 69 3d 70 53 65 74 2d 3e 6e 2c 20 70 3d or(i=pSet->n, p=
5b40: 70 53 65 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d pSet->a; i>0; i-
5b50: 2d 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 -, p++){. if(
5b60: 20 72 52 75 6e 3c 3d 70 2d 3e 72 52 75 6e 20 26 rRun<=p->rRun &
5b70: 26 20 28 70 72 65 72 65 71 20 26 20 70 2d 3e 70 & (prereq & p->p
5b80: 72 65 72 65 71 29 3d 3d 70 72 65 72 65 71 20 29 rereq)==prereq )
5b90: 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 77 68 65 {. goto whe
5ba0: 72 65 4f 72 49 6e 73 65 72 74 5f 64 6f 6e 65 3b reOrInsert_done;
5bb0: 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
5bc0: 2d 3e 72 52 75 6e 3c 3d 72 52 75 6e 20 26 26 20 ->rRun<=rRun &&
5bd0: 28 70 2d 3e 70 72 65 72 65 71 20 26 20 70 72 65 (p->prereq & pre
5be0: 72 65 71 29 3d 3d 70 2d 3e 70 72 65 72 65 71 20 req)==p->prereq
5bf0: 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 ){. return
5c00: 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 0;. }. }. i
5c10: 66 28 20 70 53 65 74 2d 3e 6e 3c 4e 5f 4f 52 5f f( pSet->n<N_OR_
5c20: 43 4f 53 54 20 29 7b 0a 20 20 20 20 70 20 3d 20 COST ){. p =
5c30: 26 70 53 65 74 2d 3e 61 5b 70 53 65 74 2d 3e 6e &pSet->a[pSet->n
5c40: 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 6e 4f 75 74 ++];. p->nOut
5c50: 20 3d 20 6e 4f 75 74 3b 0a 20 20 7d 65 6c 73 65 = nOut;. }else
5c60: 7b 0a 20 20 20 20 70 20 3d 20 70 53 65 74 2d 3e {. p = pSet->
5c70: 61 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 a;. for(i=1;
5c80: 69 3c 70 53 65 74 2d 3e 6e 3b 20 69 2b 2b 29 7b i<pSet->n; i++){
5c90: 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 52 . if( p->rR
5ca0: 75 6e 3e 70 53 65 74 2d 3e 61 5b 69 5d 2e 72 52 un>pSet->a[i].rR
5cb0: 75 6e 20 29 20 70 20 3d 20 70 53 65 74 2d 3e 61 un ) p = pSet->a
5cc0: 20 2b 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 + i;. }.
5cd0: 69 66 28 20 70 2d 3e 72 52 75 6e 3c 3d 72 52 75 if( p->rRun<=rRu
5ce0: 6e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 n ) return 0;.
5cf0: 7d 0a 77 68 65 72 65 4f 72 49 6e 73 65 72 74 5f }.whereOrInsert_
5d00: 64 6f 6e 65 3a 0a 20 20 70 2d 3e 70 72 65 72 65 done:. p->prere
5d10: 71 20 3d 20 70 72 65 72 65 71 3b 0a 20 20 70 2d q = prereq;. p-
5d20: 3e 72 52 75 6e 20 3d 20 72 52 75 6e 3b 0a 20 20 >rRun = rRun;.
5d30: 69 66 28 20 70 2d 3e 6e 4f 75 74 3e 6e 4f 75 74 if( p->nOut>nOut
5d40: 20 29 20 70 2d 3e 6e 4f 75 74 20 3d 20 6e 4f 75 ) p->nOut = nOu
5d50: 74 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d t;. return 1;.}
5d60: 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
5d70: 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 ze a preallocate
5d80: 64 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 d WhereClause st
5d90: 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 ructure..*/.stat
5da0: 69 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 ic void whereCla
5db0: 75 73 65 49 6e 69 74 28 0a 20 20 57 68 65 72 65 useInit(. Where
5dc0: 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 Clause *pWC,
5dd0: 20 20 20 20 2f 2a 20 54 68 65 20 57 68 65 72 65 /* The Where
5de0: 43 6c 61 75 73 65 20 74 6f 20 62 65 20 69 6e 69 Clause to be ini
5df0: 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 57 68 tialized */. Wh
5e00: 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 20 ereInfo *pWInfo
5e10: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 /* The WH
5e20: 45 52 45 20 70 72 6f 63 65 73 73 69 6e 67 20 63 ERE processing c
5e30: 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b 0a 20 20 70 ontext */.){. p
5e40: 57 43 2d 3e 70 57 49 6e 66 6f 20 3d 20 70 57 49 WC->pWInfo = pWI
5e50: 6e 66 6f 3b 0a 20 20 70 57 43 2d 3e 70 4f 75 74 nfo;. pWC->pOut
5e60: 65 72 20 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 6e er = 0;. pWC->n
5e70: 54 65 72 6d 20 3d 20 30 3b 0a 20 20 70 57 43 2d Term = 0;. pWC-
5e80: 3e 6e 53 6c 6f 74 20 3d 20 41 72 72 61 79 53 69 >nSlot = ArraySi
5e90: 7a 65 28 70 57 43 2d 3e 61 53 74 61 74 69 63 29 ze(pWC->aStatic)
5ea0: 3b 0a 20 20 70 57 43 2d 3e 61 20 3d 20 70 57 43 ;. pWC->a = pWC
5eb0: 2d 3e 61 53 74 61 74 69 63 3b 0a 7d 0a 0a 2f 2a ->aStatic;.}../*
5ec0: 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65 6e Forward referen
5ed0: 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 ce */.static voi
5ee0: 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 d whereClauseCle
5ef0: 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 2a 29 ar(WhereClause*)
5f00: 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 ;../*.** Dealloc
5f10: 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 ate all memory a
5f20: 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 ssociated with a
5f30: 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 6f 62 6a WhereOrInfo obj
5f40: 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ect..*/.static v
5f50: 6f 69 64 20 77 68 65 72 65 4f 72 49 6e 66 6f 44 oid whereOrInfoD
5f60: 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 elete(sqlite3 *d
5f70: 62 2c 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a b, WhereOrInfo *
5f80: 70 29 7b 0a 20 20 77 68 65 72 65 43 6c 61 75 73 p){. whereClaus
5f90: 65 43 6c 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a eClear(&p->wc);.
5fa0: 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
5fb0: 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a db, p);.}../*.**
5fc0: 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 Deallocate all
5fd0: 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 memory associate
5fe0: 64 20 77 69 74 68 20 61 20 57 68 65 72 65 41 6e d with a WhereAn
5ff0: 64 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a 2f dInfo object..*/
6000: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 .static void whe
6010: 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28 reAndInfoDelete(
6020: 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65 sqlite3 *db, Whe
6030: 72 65 41 6e 64 49 6e 66 6f 20 2a 70 29 7b 0a 20 reAndInfo *p){.
6040: 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 whereClauseClea
6050: 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c r(&p->wc);. sql
6060: 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 ite3DbFree(db, p
6070: 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c );.}../*.** Deal
6080: 6c 6f 63 61 74 65 20 61 20 57 68 65 72 65 43 6c locate a WhereCl
6090: 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 ause structure.
60a0: 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 The WhereClause
60b0: 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 74 structure.** it
60c0: 73 65 6c 66 20 69 73 20 6e 6f 74 20 66 72 65 65 self is not free
60d0: 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 d. This routine
60e0: 20 69 73 20 74 68 65 20 69 6e 76 65 72 73 65 20 is the inverse
60f0: 6f 66 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e of whereClauseIn
6100: 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 it()..*/.static
6110: 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 void whereClause
6120: 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61 75 73 Clear(WhereClaus
6130: 65 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74 20 69 e *pWC){. int i
6140: 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61 ;. WhereTerm *a
6150: 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 ;. sqlite3 *db
6160: 3d 20 70 57 43 2d 3e 70 57 49 6e 66 6f 2d 3e 70 = pWC->pWInfo->p
6170: 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 66 6f 72 Parse->db;. for
6180: 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d 31 2c (i=pWC->nTerm-1,
6190: 20 61 3d 70 57 43 2d 3e 61 3b 20 69 3e 3d 30 3b a=pWC->a; i>=0;
61a0: 20 69 2d 2d 2c 20 61 2b 2b 29 7b 0a 20 20 20 20 i--, a++){.
61b0: 69 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20 26 if( a->wtFlags &
61c0: 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 29 7b TERM_DYNAMIC ){
61d0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
61e0: 70 72 44 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e prDelete(db, a->
61f0: 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 pExpr);. }.
6200: 20 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 if( a->wtFlags
6210: 20 26 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 29 & TERM_ORINFO )
6220: 7b 0a 20 20 20 20 20 20 77 68 65 72 65 4f 72 49 {. whereOrI
6230: 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c 20 61 2d nfoDelete(db, a-
6240: 3e 75 2e 70 4f 72 49 6e 66 6f 29 3b 0a 20 20 20 >u.pOrInfo);.
6250: 20 7d 65 6c 73 65 20 69 66 28 20 61 2d 3e 77 74 }else if( a->wt
6260: 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 41 4e 44 Flags & TERM_AND
6270: 49 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20 77 68 INFO ){. wh
6280: 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74 65 ereAndInfoDelete
6290: 28 64 62 2c 20 61 2d 3e 75 2e 70 41 6e 64 49 6e (db, a->u.pAndIn
62a0: 66 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 fo);. }. }.
62b0: 20 69 66 28 20 70 57 43 2d 3e 61 21 3d 70 57 43 if( pWC->a!=pWC
62c0: 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 ->aStatic ){.
62d0: 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
62e0: 62 2c 20 70 57 43 2d 3e 61 29 3b 0a 20 20 7d 0a b, pWC->a);. }.
62f0: 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 73 }../*.** Add a s
6300: 69 6e 67 6c 65 20 6e 65 77 20 57 68 65 72 65 54 ingle new WhereT
6310: 65 72 6d 20 65 6e 74 72 79 20 74 6f 20 74 68 65 erm entry to the
6320: 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 6a WhereClause obj
6330: 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68 65 20 ect pWC..** The
6340: 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20 6f 62 new WhereTerm ob
6350: 6a 65 63 74 20 69 73 20 63 6f 6e 73 74 72 75 63 ject is construc
6360: 74 65 64 20 66 72 6f 6d 20 45 78 70 72 20 70 20 ted from Expr p
6370: 61 6e 64 20 77 69 74 68 20 77 74 46 6c 61 67 73 and with wtFlags
6380: 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 69 ..** The index i
6390: 6e 20 70 57 43 2d 3e 61 5b 5d 20 6f 66 20 74 68 n pWC->a[] of th
63a0: 65 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20 e new WhereTerm
63b0: 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73 is returned on s
63c0: 75 63 63 65 73 73 2e 0a 2a 2a 20 30 20 69 73 20 uccess..** 0 is
63d0: 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 returned if the
63e0: 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20 63 6f new WhereTerm co
63f0: 75 6c 64 20 6e 6f 74 20 62 65 20 61 64 64 65 64 uld not be added
6400: 20 64 75 65 20 74 6f 20 61 20 6d 65 6d 6f 72 79 due to a memory
6410: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 .** allocation e
6420: 72 72 6f 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 rror. The memor
6430: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 y allocation fai
6440: 6c 75 72 65 20 77 69 6c 6c 20 62 65 20 72 65 63 lure will be rec
6450: 6f 72 64 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 orded in.** the
6460: 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
6470: 20 66 6c 61 67 20 73 6f 20 74 68 61 74 20 68 69 flag so that hi
6480: 67 68 65 72 2d 6c 65 76 65 6c 20 66 75 6e 63 74 gher-level funct
6490: 69 6f 6e 73 20 63 61 6e 20 64 65 74 65 63 74 20 ions can detect
64a0: 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 it..**.** This r
64b0: 6f 75 74 69 6e 65 20 77 69 6c 6c 20 69 6e 63 72 outine will incr
64c0: 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 ease the size of
64d0: 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72 the pWC->a[] ar
64e0: 72 61 79 20 61 73 20 6e 65 63 65 73 73 61 72 79 ray as necessary
64f0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 ..**.** If the w
6500: 74 46 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 tFlags argument
6510: 69 6e 63 6c 75 64 65 73 20 54 45 52 4d 5f 44 59 includes TERM_DY
6520: 4e 41 4d 49 43 2c 20 74 68 65 6e 20 72 65 73 70 NAMIC, then resp
6530: 6f 6e 73 69 62 69 6c 69 74 79 0a 2a 2a 20 66 6f onsibility.** fo
6540: 72 20 66 72 65 65 69 6e 67 20 74 68 65 20 65 78 r freeing the ex
6550: 70 72 65 73 73 69 6f 6e 20 70 20 69 73 20 61 73 pression p is as
6560: 73 75 6d 65 64 20 62 79 20 74 68 65 20 57 68 65 sumed by the Whe
6570: 72 65 43 6c 61 75 73 65 20 6f 62 6a 65 63 74 20 reClause object
6580: 70 57 43 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 pWC..** This is
6590: 74 72 75 65 20 65 76 65 6e 20 69 66 20 74 68 69 true even if thi
65a0: 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 s routine fails
65b0: 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 to allocate a ne
65c0: 77 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a w WhereTerm..**.
65d0: 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 20 54 68 69 ** WARNING: Thi
65e0: 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 s routine might
65f0: 72 65 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 reallocate the s
6600: 70 61 63 65 20 75 73 65 64 20 74 6f 20 73 74 6f pace used to sto
6610: 72 65 0a 2a 2a 20 57 68 65 72 65 54 65 72 6d 73 re.** WhereTerms
6620: 2e 20 20 41 6c 6c 20 70 6f 69 6e 74 65 72 73 20 . All pointers
6630: 74 6f 20 57 68 65 72 65 54 65 72 6d 73 20 73 68 to WhereTerms sh
6640: 6f 75 6c 64 20 62 65 20 69 6e 76 61 6c 69 64 61 ould be invalida
6650: 74 65 64 20 61 66 74 65 72 0a 2a 2a 20 63 61 6c ted after.** cal
6660: 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e ling this routin
6670: 65 2e 20 20 53 75 63 68 20 70 6f 69 6e 74 65 72 e. Such pointer
6680: 73 20 6d 61 79 20 62 65 20 72 65 69 6e 69 74 69 s may be reiniti
6690: 61 6c 69 7a 65 64 20 62 79 20 72 65 66 65 72 65 alized by refere
66a0: 6e 63 69 6e 67 0a 2a 2a 20 74 68 65 20 70 57 43 ncing.** the pWC
66b0: 2d 3e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a ->a[] array..*/.
66c0: 73 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 static int where
66d0: 43 6c 61 75 73 65 49 6e 73 65 72 74 28 57 68 65 ClauseInsert(Whe
66e0: 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 reClause *pWC, E
66f0: 78 70 72 20 2a 70 2c 20 75 38 20 77 74 46 6c 61 xpr *p, u8 wtFla
6700: 67 73 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d gs){. WhereTerm
6710: 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69 *pTerm;. int i
6720: 64 78 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 dx;. testcase(
6730: 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 wtFlags & TERM_V
6740: 49 52 54 55 41 4c 20 29 3b 0a 20 20 69 66 28 20 IRTUAL );. if(
6750: 70 57 43 2d 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d pWC->nTerm>=pWC-
6760: 3e 6e 53 6c 6f 74 20 29 7b 0a 20 20 20 20 57 68 >nSlot ){. Wh
6770: 65 72 65 54 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 ereTerm *pOld =
6780: 70 57 43 2d 3e 61 3b 0a 20 20 20 20 73 71 6c 69 pWC->a;. sqli
6790: 74 65 33 20 2a 64 62 20 3d 20 70 57 43 2d 3e 70 te3 *db = pWC->p
67a0: 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 2d 3e 64 WInfo->pParse->d
67b0: 62 3b 0a 20 20 20 20 70 57 43 2d 3e 61 20 3d 20 b;. pWC->a =
67c0: 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 sqlite3DbMallocR
67d0: 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 70 57 aw(db, sizeof(pW
67e0: 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 53 C->a[0])*pWC->nS
67f0: 6c 6f 74 2a 32 20 29 3b 0a 20 20 20 20 69 66 28 lot*2 );. if(
6800: 20 70 57 43 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 pWC->a==0 ){.
6810: 20 20 20 20 69 66 28 20 77 74 46 6c 61 67 73 20 if( wtFlags
6820: 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 29 & TERM_DYNAMIC )
6830: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
6840: 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
6850: 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 p);. }.
6860: 20 20 70 57 43 2d 3e 61 20 3d 20 70 4f 6c 64 3b pWC->a = pOld;
6870: 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b . return 0;
6880: 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 . }. memcp
6890: 79 28 70 57 43 2d 3e 61 2c 20 70 4f 6c 64 2c 20 y(pWC->a, pOld,
68a0: 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d sizeof(pWC->a[0]
68b0: 29 2a 70 57 43 2d 3e 6e 54 65 72 6d 29 3b 0a 20 )*pWC->nTerm);.
68c0: 20 20 20 69 66 28 20 70 4f 6c 64 21 3d 70 57 43 if( pOld!=pWC
68d0: 2d 3e 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 ->aStatic ){.
68e0: 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
68f0: 28 64 62 2c 20 70 4f 6c 64 29 3b 0a 20 20 20 20 (db, pOld);.
6900: 7d 0a 20 20 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 }. pWC->nSlot
6910: 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
6920: 6f 63 53 69 7a 65 28 64 62 2c 20 70 57 43 2d 3e ocSize(db, pWC->
6930: 61 29 2f 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61 a)/sizeof(pWC->a
6940: 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 70 54 65 72 [0]);. }. pTer
6950: 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 20 m = &pWC->a[idx
6960: 3d 20 70 57 43 2d 3e 6e 54 65 72 6d 2b 2b 5d 3b = pWC->nTerm++];
6970: 0a 20 20 69 66 28 20 70 20 26 26 20 45 78 70 72 . if( p && Expr
6980: 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 HasProperty(p, E
6990: 50 5f 55 6e 6c 69 6b 65 6c 79 29 20 29 7b 0a 20 P_Unlikely) ){.
69a0: 20 20 20 70 54 65 72 6d 2d 3e 74 72 75 74 68 50 pTerm->truthP
69b0: 72 6f 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 67 rob = sqlite3Log
69c0: 45 73 74 28 70 2d 3e 69 54 61 62 6c 65 29 20 2d Est(p->iTable) -
69d0: 20 39 39 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 99;. }else{.
69e0: 20 20 70 54 65 72 6d 2d 3e 74 72 75 74 68 50 72 pTerm->truthPr
69f0: 6f 62 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 70 ob = -1;. }. p
6a00: 54 65 72 6d 2d 3e 70 45 78 70 72 20 3d 20 73 71 Term->pExpr = sq
6a10: 6c 69 74 65 33 45 78 70 72 53 6b 69 70 43 6f 6c lite3ExprSkipCol
6a20: 6c 61 74 65 28 70 29 3b 0a 20 20 70 54 65 72 6d late(p);. pTerm
6a30: 2d 3e 77 74 46 6c 61 67 73 20 3d 20 77 74 46 6c ->wtFlags = wtFl
6a40: 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d 3e 70 57 ags;. pTerm->pW
6a50: 43 20 3d 20 70 57 43 3b 0a 20 20 70 54 65 72 6d C = pWC;. pTerm
6a60: 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a ->iParent = -1;.
6a70: 20 20 72 65 74 75 72 6e 20 69 64 78 3b 0a 7d 0a return idx;.}.
6a80: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 ./*.** This rout
6a90: 69 6e 65 20 69 64 65 6e 74 69 66 69 65 73 20 73 ine identifies s
6aa0: 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e ubexpressions in
6ab0: 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
6ac0: 65 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 e where.** each
6ad0: 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69 73 subexpression is
6ae0: 20 73 65 70 61 72 61 74 65 64 20 62 79 20 74 68 separated by th
6af0: 65 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 20 6f e AND operator o
6b00: 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 r some other.**
6b10: 6f 70 65 72 61 74 6f 72 20 73 70 65 63 69 66 69 operator specifi
6b20: 65 64 20 69 6e 20 74 68 65 20 6f 70 20 70 61 72 ed in the op par
6b30: 61 6d 65 74 65 72 2e 20 20 54 68 65 20 57 68 65 ameter. The Whe
6b40: 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 reClause structu
6b50: 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20 re.** is filled
6b60: 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f with pointers to
6b70: 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e subexpressions.
6b80: 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a For example:.*
6b90: 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45 20 20 61 *.** WHERE a
6ba0: 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44 20 63 6f =='hello' AND co
6bb0: 61 6c 65 73 63 65 28 62 2c 31 31 29 3c 31 30 20 alesce(b,11)<10
6bc0: 41 4e 44 20 28 63 2b 31 32 21 3d 64 20 4f 52 20 AND (c+12!=d OR
6bd0: 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20 20 20 20 c==22).**
6be0: 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 \________/
6bf0: 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f \____________
6c00: 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f ___/ \______
6c10: 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 __________/.**
6c20: 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 30 slot[0
6c30: 5d 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f ] slo
6c40: 74 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20 20 t[1]
6c50: 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a 0a 2a 2a slot[2].**.**
6c60: 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 57 48 The original WH
6c70: 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 70 45 ERE clause in pE
6c80: 78 70 72 20 69 73 20 75 6e 61 6c 74 65 72 65 64 xpr is unaltered
6c90: 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74 . All this rout
6ca0: 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69 73 20 6d ine.** does is m
6cb0: 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e 74 72 69 ake slot[] entri
6cc0: 65 73 20 70 6f 69 6e 74 20 74 6f 20 73 75 62 73 es point to subs
6cd0: 74 72 75 63 74 75 72 65 20 77 69 74 68 69 6e 20 tructure within
6ce0: 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 pExpr..**.** In
6cf0: 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65 6e the previous sen
6d00: 74 65 6e 63 65 20 61 6e 64 20 69 6e 20 74 68 65 tence and in the
6d10: 20 64 69 61 67 72 61 6d 2c 20 22 73 6c 6f 74 5b diagram, "slot[
6d20: 5d 22 20 72 65 66 65 72 73 20 74 6f 0a 2a 2a 20 ]" refers to.**
6d30: 74 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 2e the WhereClause.
6d40: 61 5b 5d 20 61 72 72 61 79 2e 20 20 54 68 65 20 a[] array. The
6d50: 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20 67 72 6f slot[] array gro
6d60: 77 73 20 61 73 20 6e 65 65 64 65 64 20 74 6f 20 ws as needed to
6d70: 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c 6c 20 74 contain.** all t
6d80: 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 erms of the WHER
6d90: 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 61 E clause..*/.sta
6da0: 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 53 70 tic void whereSp
6db0: 6c 69 74 28 57 68 65 72 65 43 6c 61 75 73 65 20 lit(WhereClause
6dc0: 2a 70 57 43 2c 20 45 78 70 72 20 2a 70 45 78 70 *pWC, Expr *pExp
6dd0: 72 2c 20 75 38 20 6f 70 29 7b 0a 20 20 70 57 43 r, u8 op){. pWC
6de0: 2d 3e 6f 70 20 3d 20 6f 70 3b 0a 20 20 69 66 28 ->op = op;. if(
6df0: 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 pExpr==0 ) retu
6e00: 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d rn;. if( pExpr-
6e10: 3e 6f 70 21 3d 6f 70 20 29 7b 0a 20 20 20 20 77 >op!=op ){. w
6e20: 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 hereClauseInsert
6e30: 28 70 57 43 2c 20 70 45 78 70 72 2c 20 30 29 3b (pWC, pExpr, 0);
6e40: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 . }else{. wh
6e50: 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70 45 ereSplit(pWC, pE
6e60: 78 70 72 2d 3e 70 4c 65 66 74 2c 20 6f 70 29 3b xpr->pLeft, op);
6e70: 0a 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 . whereSplit(
6e80: 70 57 43 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 pWC, pExpr->pRig
6e90: 68 74 2c 20 6f 70 29 3b 0a 20 20 7d 0a 7d 0a 0a ht, op);. }.}..
6ea0: 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 /*.** Initialize
6eb0: 20 61 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 a WhereMaskSet
6ec0: 6f 62 6a 65 63 74 0a 2a 2f 0a 23 64 65 66 69 6e object.*/.#defin
6ed0: 65 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 50 29 e initMaskSet(P)
6ee0: 20 20 28 50 29 2d 3e 6e 3d 30 0a 0a 2f 2a 0a 2a (P)->n=0../*.*
6ef0: 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 69 74 * Return the bit
6f00: 6d 61 73 6b 20 66 6f 72 20 74 68 65 20 67 69 76 mask for the giv
6f10: 65 6e 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 en cursor number
6f20: 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 0a 2a . Return 0 if.*
6f30: 2a 20 69 43 75 72 73 6f 72 20 69 73 20 6e 6f 74 * iCursor is not
6f40: 20 69 6e 20 74 68 65 20 73 65 74 2e 0a 2a 2f 0a in the set..*/.
6f50: 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 67 static Bitmask g
6f60: 65 74 4d 61 73 6b 28 57 68 65 72 65 4d 61 73 6b etMask(WhereMask
6f70: 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69 Set *pMaskSet, i
6f80: 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 69 nt iCursor){. i
6f90: 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 nt i;. assert(
6fa0: 70 4d 61 73 6b 53 65 74 2d 3e 6e 3c 3d 28 69 6e pMaskSet->n<=(in
6fb0: 74 29 73 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b t)sizeof(Bitmask
6fc0: 29 2a 38 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 )*8 );. for(i=0
6fd0: 3b 20 69 3c 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b ; i<pMaskSet->n;
6fe0: 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 i++){. if( p
6ff0: 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d MaskSet->ix[i]==
7000: 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 iCursor ){.
7010: 20 72 65 74 75 72 6e 20 4d 41 53 4b 42 49 54 28 return MASKBIT(
7020: 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 i);. }. }.
7030: 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
7040: 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 ** Create a new
7050: 6d 61 73 6b 20 66 6f 72 20 63 75 72 73 6f 72 20 mask for cursor
7060: 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 iCursor..**.** T
7070: 68 65 72 65 20 69 73 20 6f 6e 65 20 63 75 72 73 here is one curs
7080: 6f 72 20 70 65 72 20 74 61 62 6c 65 20 69 6e 20 or per table in
7090: 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e the FROM clause.
70a0: 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a The number of.
70b0: 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 ** tables in the
70c0: 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 FROM clause is
70d0: 6c 69 6d 69 74 65 64 20 62 79 20 61 20 74 65 73 limited by a tes
70e0: 74 20 65 61 72 6c 79 20 69 6e 20 74 68 65 0a 2a t early in the.*
70f0: 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 * sqlite3WhereBe
7100: 67 69 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20 20 gin() routine.
7110: 53 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 So we know that
7120: 74 68 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69 78 the pMaskSet->ix
7130: 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c 6c [].** array will
7140: 20 6e 65 76 65 72 20 6f 76 65 72 66 6c 6f 77 2e never overflow.
7150: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
7160: 63 72 65 61 74 65 4d 61 73 6b 28 57 68 65 72 65 createMask(Where
7170: 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 MaskSet *pMaskSe
7180: 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29 7b t, int iCursor){
7190: 0a 20 20 61 73 73 65 72 74 28 20 70 4d 61 73 6b . assert( pMask
71a0: 53 65 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53 69 Set->n < ArraySi
71b0: 7a 65 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78 29 ze(pMaskSet->ix)
71c0: 20 29 3b 0a 20 20 70 4d 61 73 6b 53 65 74 2d 3e );. pMaskSet->
71d0: 69 78 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b 2b ix[pMaskSet->n++
71e0: 5d 20 3d 20 69 43 75 72 73 6f 72 3b 0a 7d 0a 0a ] = iCursor;.}..
71f0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 /*.** These rout
7200: 69 6e 65 73 20 77 61 6c 6b 20 28 72 65 63 75 72 ines walk (recur
7210: 73 69 76 65 6c 79 29 20 61 6e 20 65 78 70 72 65 sively) an expre
7220: 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 67 ssion tree and g
7230: 65 6e 65 72 61 74 65 0a 2a 2a 20 61 20 62 69 74 enerate.** a bit
7240: 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 mask indicating
7250: 77 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 65 which tables are
7260: 20 75 73 65 64 20 69 6e 20 74 68 61 74 20 65 78 used in that ex
7270: 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 pression.** tree
7280: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d ..*/.static Bitm
7290: 61 73 6b 20 65 78 70 72 4c 69 73 74 54 61 62 6c ask exprListTabl
72a0: 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b eUsage(WhereMask
72b0: 53 65 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 Set*, ExprList*)
72c0: 3b 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b ;.static Bitmask
72d0: 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 exprSelectTable
72e0: 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53 Usage(WhereMaskS
72f0: 65 74 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 73 et*, Select*);.s
7300: 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 tatic Bitmask ex
7310: 70 72 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 prTableUsage(Whe
7320: 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b reMaskSet *pMask
7330: 53 65 74 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 Set, Expr *p){.
7340: 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 Bitmask mask =
7350: 30 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 0;. if( p==0 )
7360: 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
7370: 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e p->op==TK_COLUMN
7380: 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 67 ){. mask = g
7390: 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c etMask(pMaskSet,
73a0: 20 70 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 p->iTable);.
73b0: 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 20 20 return mask;.
73c0: 7d 0a 20 20 6d 61 73 6b 20 3d 20 65 78 70 72 54 }. mask = exprT
73d0: 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 ableUsage(pMaskS
73e0: 65 74 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a et, p->pRight);.
73f0: 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 mask |= exprTa
7400: 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 bleUsage(pMaskSe
7410: 74 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 t, p->pLeft);.
7420: 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
7430: 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c rty(p, EP_xIsSel
7440: 65 63 74 29 20 29 7b 0a 20 20 20 20 6d 61 73 6b ect) ){. mask
7450: 20 7c 3d 20 65 78 70 72 53 65 6c 65 63 74 54 61 |= exprSelectTa
7460: 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 bleUsage(pMaskSe
7470: 74 2c 20 70 2d 3e 78 2e 70 53 65 6c 65 63 74 29 t, p->x.pSelect)
7480: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d ;. }else{. m
7490: 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 ask |= exprListT
74a0: 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 ableUsage(pMaskS
74b0: 65 74 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b et, p->x.pList);
74c0: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 . }. return ma
74d0: 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69 74 sk;.}.static Bit
74e0: 6d 61 73 6b 20 65 78 70 72 4c 69 73 74 54 61 62 mask exprListTab
74f0: 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 leUsage(WhereMas
7500: 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 kSet *pMaskSet,
7510: 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 ExprList *pList)
7520: 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 69 74 {. int i;. Bit
7530: 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 mask mask = 0;.
7540: 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 if( pList ){.
7550: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 for(i=0; i<pLi
7560: 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b st->nExpr; i++){
7570: 0a 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 . mask |= e
7580: 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d xprTableUsage(pM
7590: 61 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 askSet, pList->a
75a0: 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 [i].pExpr);.
75b0: 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d }. }. return m
75c0: 61 73 6b 3b 0a 7d 0a 73 74 61 74 69 63 20 42 69 ask;.}.static Bi
75d0: 74 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63 74 tmask exprSelect
75e0: 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72 65 TableUsage(Where
75f0: 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 MaskSet *pMaskSe
7600: 74 2c 20 53 65 6c 65 63 74 20 2a 70 53 29 7b 0a t, Select *pS){.
7610: 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d Bitmask mask =
7620: 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 20 0;. while( pS
7630: 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a ){. SrcList *
7640: 70 53 72 63 20 3d 20 70 53 2d 3e 70 53 72 63 3b pSrc = pS->pSrc;
7650: 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 . mask |= exp
7660: 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 rListTableUsage(
7670: 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 45 pMaskSet, pS->pE
7680: 4c 69 73 74 29 3b 0a 20 20 20 20 6d 61 73 6b 20 List);. mask
7690: 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 |= exprListTable
76a0: 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 Usage(pMaskSet,
76b0: 70 53 2d 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 pS->pGroupBy);.
76c0: 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c mask |= exprL
76d0: 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d istTableUsage(pM
76e0: 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 4f 72 64 askSet, pS->pOrd
76f0: 65 72 42 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20 erBy);. mask
7700: 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 |= exprTableUsag
7710: 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e e(pMaskSet, pS->
7720: 70 57 68 65 72 65 29 3b 0a 20 20 20 20 6d 61 73 pWhere);. mas
7730: 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 k |= exprTableUs
7740: 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 age(pMaskSet, pS
7750: 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 ->pHaving);.
7760: 69 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 21 if( ALWAYS(pSrc!
7770: 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 =0) ){. int
7780: 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d i;. for(i=
7790: 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63 3b 0; i<pSrc->nSrc;
77a0: 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 6d i++){. m
77b0: 61 73 6b 20 7c 3d 20 65 78 70 72 53 65 6c 65 63 ask |= exprSelec
77c0: 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 tTableUsage(pMas
77d0: 6b 53 65 74 2c 20 70 53 72 63 2d 3e 61 5b 69 5d kSet, pSrc->a[i]
77e0: 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 .pSelect);.
77f0: 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 mask |= exprT
7800: 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 ableUsage(pMaskS
7810: 65 74 2c 20 70 53 72 63 2d 3e 61 5b 69 5d 2e 70 et, pSrc->a[i].p
7820: 4f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 On);. }.
7830: 20 7d 0a 20 20 20 20 70 53 20 3d 20 70 53 2d 3e }. pS = pS->
7840: 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 pPrior;. }. re
7850: 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a turn mask;.}../*
7860: 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 .** Return TRUE
7870: 69 66 20 74 68 65 20 67 69 76 65 6e 20 6f 70 65 if the given ope
7880: 72 61 74 6f 72 20 69 73 20 6f 6e 65 20 6f 66 20 rator is one of
7890: 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68 the operators th
78a0: 61 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 at is.** allowed
78b0: 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 61 62 6c for an indexabl
78c0: 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 e WHERE clause t
78d0: 65 72 6d 2e 20 20 54 68 65 20 61 6c 6c 6f 77 65 erm. The allowe
78e0: 64 20 6f 70 65 72 61 74 6f 72 73 20 61 72 65 0a d operators are.
78f0: 2a 2a 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3e 22 ** "=", "<", ">"
7900: 2c 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c 20 22 49 , "<=", ">=", "I
7910: 4e 22 2c 20 61 6e 64 20 22 49 53 20 4e 55 4c 4c N", and "IS NULL
7920: 22 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ".*/.static int
7930: 61 6c 6c 6f 77 65 64 4f 70 28 69 6e 74 20 6f 70 allowedOp(int op
7940: 29 7b 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f ){. assert( TK_
7950: 47 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f 47 GT>TK_EQ && TK_G
7960: 54 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 T<TK_GE );. ass
7970: 65 72 74 28 20 54 4b 5f 4c 54 3e 54 4b 5f 45 51 ert( TK_LT>TK_EQ
7980: 20 26 26 20 54 4b 5f 4c 54 3c 54 4b 5f 47 45 20 && TK_LT<TK_GE
7990: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f );. assert( TK_
79a0: 4c 45 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c LE>TK_EQ && TK_L
79b0: 45 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 E<TK_GE );. ass
79c0: 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f 45 ert( TK_GE==TK_E
79d0: 51 2b 34 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 Q+4 );. return
79e0: 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c 20 28 6f 70 op==TK_IN || (op
79f0: 3e 3d 54 4b 5f 45 51 20 26 26 20 6f 70 3c 3d 54 >=TK_EQ && op<=T
7a00: 4b 5f 47 45 29 20 7c 7c 20 6f 70 3d 3d 54 4b 5f K_GE) || op==TK_
7a10: 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a ISNULL;.}../*.**
7a20: 20 53 77 61 70 20 74 77 6f 20 6f 62 6a 65 63 74 Swap two object
7a30: 73 20 6f 66 20 74 79 70 65 20 54 59 50 45 2e 0a s of type TYPE..
7a40: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 57 41 50 28 */.#define SWAP(
7a50: 54 59 50 45 2c 41 2c 42 29 20 7b 54 59 50 45 20 TYPE,A,B) {TYPE
7a60: 74 3d 41 3b 20 41 3d 42 3b 20 42 3d 74 3b 7d 0a t=A; A=B; B=t;}.
7a70: 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 75 74 65 20 61 ./*.** Commute a
7a80: 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 comparison oper
7a90: 61 74 6f 72 2e 20 20 45 78 70 72 65 73 73 69 6f ator. Expressio
7aa0: 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 ns of the form "
7ab0: 58 20 6f 70 20 59 22 0a 2a 2a 20 61 72 65 20 63 X op Y".** are c
7ac0: 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 22 59 onverted into "Y
7ad0: 20 6f 70 20 58 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 op X"..**.** If
7ae0: 20 6c 65 66 74 2f 72 69 67 68 74 20 70 72 65 63 left/right prec
7af0: 65 64 65 6e 63 65 20 72 75 6c 65 73 20 63 6f 6d edence rules com
7b00: 65 20 69 6e 74 6f 20 70 6c 61 79 20 77 68 65 6e e into play when
7b10: 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 74 68 65 determining the
7b20: 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 .** collating se
7b30: 71 75 65 6e 63 65 2c 20 74 68 65 6e 20 43 4f 4c quence, then COL
7b40: 4c 41 54 45 20 6f 70 65 72 61 74 6f 72 73 20 61 LATE operators a
7b50: 72 65 20 61 64 6a 75 73 74 65 64 20 74 6f 20 65 re adjusted to e
7b60: 6e 73 75 72 65 0a 2a 2a 20 74 68 61 74 20 74 68 nsure.** that th
7b70: 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 e collating sequ
7b80: 65 6e 63 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 ence does not ch
7b90: 61 6e 67 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 ange. For examp
7ba0: 6c 65 3a 0a 2a 2a 20 22 59 20 63 6f 6c 6c 61 74 le:.** "Y collat
7bb0: 65 20 4e 4f 43 41 53 45 20 6f 70 20 58 22 20 62 e NOCASE op X" b
7bc0: 65 63 6f 6d 65 73 20 22 58 20 6f 70 20 59 22 20 ecomes "X op Y"
7bd0: 62 65 63 61 75 73 65 20 61 6e 79 20 63 6f 6c 6c because any coll
7be0: 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f ation sequence o
7bf0: 6e 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20 68 61 n.** the left ha
7c00: 6e 64 20 73 69 64 65 20 6f 66 20 61 20 63 6f 6d nd side of a com
7c10: 70 61 72 69 73 6f 6e 20 6f 76 65 72 72 69 64 65 parison override
7c20: 73 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 s any collation
7c30: 73 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61 74 74 sequence .** att
7c40: 61 63 68 65 64 20 74 6f 20 74 68 65 20 72 69 67 ached to the rig
7c50: 68 74 2e 20 46 6f 72 20 74 68 65 20 73 61 6d 65 ht. For the same
7c60: 20 72 65 61 73 6f 6e 20 74 68 65 20 45 50 5f 43 reason the EP_C
7c70: 6f 6c 6c 61 74 65 20 66 6c 61 67 0a 2a 2a 20 69 ollate flag.** i
7c80: 73 20 6e 6f 74 20 63 6f 6d 6d 75 74 65 64 2e 0a s not commuted..
7c90: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 */.static void e
7ca0: 78 70 72 43 6f 6d 6d 75 74 65 28 50 61 72 73 65 xprCommute(Parse
7cb0: 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a *pParse, Expr *
7cc0: 70 45 78 70 72 29 7b 0a 20 20 75 31 36 20 65 78 pExpr){. u16 ex
7cd0: 70 52 69 67 68 74 20 3d 20 28 70 45 78 70 72 2d pRight = (pExpr-
7ce0: 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 >pRight->flags &
7cf0: 20 45 50 5f 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 EP_Collate);.
7d00: 75 31 36 20 65 78 70 4c 65 66 74 20 3d 20 28 70 u16 expLeft = (p
7d10: 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 Expr->pLeft->fla
7d20: 67 73 20 26 20 45 50 5f 43 6f 6c 6c 61 74 65 29 gs & EP_Collate)
7d30: 3b 0a 20 20 61 73 73 65 72 74 28 20 61 6c 6c 6f ;. assert( allo
7d40: 77 65 64 4f 70 28 70 45 78 70 72 2d 3e 6f 70 29 wedOp(pExpr->op)
7d50: 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 && pExpr->op!=T
7d60: 4b 5f 49 4e 20 29 3b 0a 20 20 69 66 28 20 65 78 K_IN );. if( ex
7d70: 70 52 69 67 68 74 3d 3d 65 78 70 4c 65 66 74 20 pRight==expLeft
7d80: 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 ){. /* Either
7d90: 20 58 20 61 6e 64 20 59 20 62 6f 74 68 20 68 61 X and Y both ha
7da0: 76 65 20 43 4f 4c 4c 41 54 45 20 6f 70 65 72 61 ve COLLATE opera
7db0: 74 6f 72 20 6f 72 20 6e 65 69 74 68 65 72 20 64 tor or neither d
7dc0: 6f 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 78 70 o */. if( exp
7dd0: 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 2f Right ){. /
7de0: 2a 20 42 6f 74 68 20 58 20 61 6e 64 20 59 20 68 * Both X and Y h
7df0: 61 76 65 20 43 4f 4c 4c 41 54 45 20 6f 70 65 72 ave COLLATE oper
7e00: 61 74 6f 72 73 2e 20 20 4d 61 6b 65 20 73 75 72 ators. Make sur
7e10: 65 20 58 20 69 73 20 61 6c 77 61 79 73 0a 20 20 e X is always.
7e20: 20 20 20 20 2a 2a 20 75 73 65 64 20 62 79 20 63 ** used by c
7e30: 6c 65 61 72 69 6e 67 20 74 68 65 20 45 50 5f 43 learing the EP_C
7e40: 6f 6c 6c 61 74 65 20 66 6c 61 67 20 66 72 6f 6d ollate flag from
7e50: 20 59 2e 20 2a 2f 0a 20 20 20 20 20 20 70 45 78 Y. */. pEx
7e60: 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 pr->pRight->flag
7e70: 73 20 26 3d 20 7e 45 50 5f 43 6f 6c 6c 61 74 65 s &= ~EP_Collate
7e80: 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 ;. }else if(
7e90: 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 sqlite3ExprCollS
7ea0: 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 eq(pParse, pExpr
7eb0: 2d 3e 70 4c 65 66 74 29 21 3d 30 20 29 7b 0a 20 ->pLeft)!=0 ){.
7ec0: 20 20 20 20 20 2f 2a 20 4e 65 69 74 68 65 72 20 /* Neither
7ed0: 58 20 6e 6f 72 20 59 20 68 61 76 65 20 43 4f 4c X nor Y have COL
7ee0: 4c 41 54 45 20 6f 70 65 72 61 74 6f 72 73 2c 20 LATE operators,
7ef0: 62 75 74 20 58 20 68 61 73 20 61 20 6e 6f 6e 2d but X has a non-
7f00: 64 65 66 61 75 6c 74 0a 20 20 20 20 20 20 2a 2a default. **
7f10: 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 collating seque
7f20: 6e 63 65 2e 20 20 53 6f 20 61 64 64 20 74 68 65 nce. So add the
7f30: 20 45 50 5f 43 6f 6c 6c 61 74 65 20 6d 61 72 6b EP_Collate mark
7f40: 65 72 20 6f 6e 20 58 20 74 6f 20 63 61 75 73 65 er on X to cause
7f50: 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 74 6f 20 . ** it to
7f60: 62 65 20 73 65 61 72 63 68 65 64 20 66 69 72 73 be searched firs
7f70: 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 45 78 70 t. */. pExp
7f80: 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 r->pLeft->flags
7f90: 7c 3d 20 45 50 5f 43 6f 6c 6c 61 74 65 3b 0a 20 |= EP_Collate;.
7fa0: 20 20 20 7d 0a 20 20 7d 0a 20 20 53 57 41 50 28 }. }. SWAP(
7fb0: 45 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70 52 69 Expr*,pExpr->pRi
7fc0: 67 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 ght,pExpr->pLeft
7fd0: 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e );. if( pExpr->
7fe0: 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 op>=TK_GT ){.
7ff0: 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d assert( TK_LT==
8000: 54 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20 20 61 TK_GT+2 );. a
8010: 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b ssert( TK_GE==TK
8020: 5f 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 _LE+2 );. ass
8030: 65 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 ert( TK_GT>TK_EQ
8040: 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
8050: 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 TK_GT<TK_LE );.
8060: 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 assert( pExpr
8070: 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26 20 70 ->op>=TK_GT && p
8080: 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 Expr->op<=TK_GE
8090: 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 );. pExpr->op
80a0: 20 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70 2d 54 = ((pExpr->op-T
80b0: 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a K_GT)^2)+TK_GT;.
80c0: 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 }.}../*.** Tra
80d0: 6e 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b 5f 78 nslate from TK_x
80e0: 78 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 57 4f x operator to WO
80f0: 5f 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a _xx bitmask..*/.
8100: 73 74 61 74 69 63 20 75 31 36 20 6f 70 65 72 61 static u16 opera
8110: 74 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70 29 7b torMask(int op){
8120: 0a 20 20 75 31 36 20 63 3b 0a 20 20 61 73 73 65 . u16 c;. asse
8130: 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 rt( allowedOp(op
8140: 29 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 ) );. if( op==T
8150: 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20 3d 20 K_IN ){. c =
8160: 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 WO_IN;. }else i
8170: 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c f( op==TK_ISNULL
8180: 20 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 ){. c = WO_I
8190: 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a SNULL;. }else{.
81a0: 20 20 20 20 61 73 73 65 72 74 28 20 28 57 4f 5f assert( (WO_
81b0: 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 20 EQ<<(op-TK_EQ))
81c0: 3c 20 30 78 37 66 66 66 20 29 3b 0a 20 20 20 20 < 0x7fff );.
81d0: 63 20 3d 20 28 75 31 36 29 28 57 4f 5f 45 51 3c c = (u16)(WO_EQ<
81e0: 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 3b 0a 20 20 <(op-TK_EQ));.
81f0: 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d }. assert( op!=
8200: 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d TK_ISNULL || c==
8210: 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 61 WO_ISNULL );. a
8220: 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 4e ssert( op!=TK_IN
8230: 20 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a || c==WO_IN );.
8240: 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b assert( op!=TK
8250: 5f 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20 _EQ || c==WO_EQ
8260: 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 );. assert( op!
8270: 3d 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f =TK_LT || c==WO_
8280: 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 LT );. assert(
8290: 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d op!=TK_LE || c==
82a0: 57 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73 65 72 WO_LE );. asser
82b0: 74 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 t( op!=TK_GT ||
82c0: 63 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20 61 73 c==WO_GT );. as
82d0: 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20 sert( op!=TK_GE
82e0: 7c 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20 || c==WO_GE );.
82f0: 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a return c;.}../*
8300: 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 .** Advance to t
8310: 68 65 20 6e 65 78 74 20 57 68 65 72 65 54 65 72 he next WhereTer
8320: 6d 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 61 m that matches a
8330: 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 ccording to the
8340: 63 72 69 74 65 72 69 61 0a 2a 2a 20 65 73 74 61 criteria.** esta
8350: 62 6c 69 73 68 65 64 20 77 68 65 6e 20 74 68 65 blished when the
8360: 20 70 53 63 61 6e 20 6f 62 6a 65 63 74 20 77 61 pScan object wa
8370: 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 s initialized by
8380: 20 77 68 65 72 65 53 63 61 6e 49 6e 69 74 28 29 whereScanInit()
8390: 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c ..** Return NULL
83a0: 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f if there are no
83b0: 20 6d 6f 72 65 20 6d 61 74 63 68 69 6e 67 20 57 more matching W
83c0: 68 65 72 65 54 65 72 6d 73 2e 0a 2a 2f 0a 73 74 hereTerms..*/.st
83d0: 61 74 69 63 20 57 68 65 72 65 54 65 72 6d 20 2a atic WhereTerm *
83e0: 77 68 65 72 65 53 63 61 6e 4e 65 78 74 28 57 68 whereScanNext(Wh
83f0: 65 72 65 53 63 61 6e 20 2a 70 53 63 61 6e 29 7b ereScan *pScan){
8400: 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 . int iCur;
8410: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
8420: 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 4c 48 53 ursor on the LHS
8430: 20 6f 66 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a of the term */.
8440: 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 int iColumn;
8450: 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f /* The co
8460: 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 4c 48 53 20 lumn on the LHS
8470: 6f 66 20 74 68 65 20 74 65 72 6d 2e 20 20 2d 31 of the term. -1
8480: 20 66 6f 72 20 49 50 4b 20 2a 2f 0a 20 20 45 78 for IPK */. Ex
8490: 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20 pr *pX;
84a0: 20 20 20 2f 2a 20 41 6e 20 65 78 70 72 65 73 73 /* An express
84b0: 69 6f 6e 20 62 65 69 6e 67 20 74 65 73 74 65 64 ion being tested
84c0: 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 */. WhereClaus
84d0: 65 20 2a 70 57 43 3b 20 20 20 20 2f 2a 20 53 68 e *pWC; /* Sh
84e0: 6f 72 74 68 61 6e 64 20 66 6f 72 20 70 53 63 61 orthand for pSca
84f0: 6e 2d 3e 70 57 43 20 2a 2f 0a 20 20 57 68 65 72 n->pWC */. Wher
8500: 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 eTerm *pTerm;
8510: 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 62 65 69 /* The term bei
8520: 6e 67 20 74 65 73 74 65 64 20 2a 2f 0a 20 20 69 ng tested */. i
8530: 6e 74 20 6b 20 3d 20 70 53 63 61 6e 2d 3e 6b 3b nt k = pScan->k;
8540: 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 /* Where to
8550: 73 74 61 72 74 20 73 63 61 6e 6e 69 6e 67 20 2a start scanning *
8560: 2f 0a 0a 20 20 77 68 69 6c 65 28 20 70 53 63 61 /.. while( pSca
8570: 6e 2d 3e 69 45 71 75 69 76 3c 3d 70 53 63 61 6e n->iEquiv<=pScan
8580: 2d 3e 6e 45 71 75 69 76 20 29 7b 0a 20 20 20 20 ->nEquiv ){.
8590: 69 43 75 72 20 3d 20 70 53 63 61 6e 2d 3e 61 45 iCur = pScan->aE
85a0: 71 75 69 76 5b 70 53 63 61 6e 2d 3e 69 45 71 75 quiv[pScan->iEqu
85b0: 69 76 2d 32 5d 3b 0a 20 20 20 20 69 43 6f 6c 75 iv-2];. iColu
85c0: 6d 6e 20 3d 20 70 53 63 61 6e 2d 3e 61 45 71 75 mn = pScan->aEqu
85d0: 69 76 5b 70 53 63 61 6e 2d 3e 69 45 71 75 69 76 iv[pScan->iEquiv
85e0: 2d 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 -1];. while(
85f0: 28 70 57 43 20 3d 20 70 53 63 61 6e 2d 3e 70 57 (pWC = pScan->pW
8600: 43 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 C)!=0 ){. f
8610: 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2b or(pTerm=pWC->a+
8620: 6b 3b 20 6b 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b k; k<pWC->nTerm;
8630: 20 6b 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a k++, pTerm++){.
8640: 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65 72 if( pTer
8650: 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 m->leftCursor==i
8660: 43 75 72 20 26 26 20 70 54 65 72 6d 2d 3e 75 2e Cur && pTerm->u.
8670: 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c leftColumn==iCol
8680: 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 umn ){.
8690: 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 if( (pTerm->eOp
86a0: 65 72 61 74 6f 72 20 26 20 57 4f 5f 45 51 55 49 erator & WO_EQUI
86b0: 56 29 21 3d 30 0a 20 20 20 20 20 20 20 20 20 20 V)!=0.
86c0: 20 26 26 20 70 53 63 61 6e 2d 3e 6e 45 71 75 69 && pScan->nEqui
86d0: 76 3c 41 72 72 61 79 53 69 7a 65 28 70 53 63 61 v<ArraySize(pSca
86e0: 6e 2d 3e 61 45 71 75 69 76 29 0a 20 20 20 20 20 n->aEquiv).
86f0: 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
8700: 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 int j;.
8710: 20 20 20 20 20 20 20 70 58 20 3d 20 73 71 6c 69 pX = sqli
8720: 74 65 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 te3ExprSkipColla
8730: 74 65 28 70 54 65 72 6d 2d 3e 70 45 78 70 72 2d te(pTerm->pExpr-
8740: 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 >pRight);.
8750: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58 assert( pX
8760: 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 ->op==TK_COLUMN
8770: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 );. f
8780: 6f 72 28 6a 3d 30 3b 20 6a 3c 70 53 63 61 6e 2d or(j=0; j<pScan-
8790: 3e 6e 45 71 75 69 76 3b 20 6a 2b 3d 32 29 7b 0a >nEquiv; j+=2){.
87a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 if
87b0: 28 20 70 53 63 61 6e 2d 3e 61 45 71 75 69 76 5b ( pScan->aEquiv[
87c0: 6a 5d 3d 3d 70 58 2d 3e 69 54 61 62 6c 65 0a 20 j]==pX->iTable.
87d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 &&
87e0: 20 70 53 63 61 6e 2d 3e 61 45 71 75 69 76 5b 6a pScan->aEquiv[j
87f0: 2b 31 5d 3d 3d 70 58 2d 3e 69 43 6f 6c 75 6d 6e +1]==pX->iColumn
8800: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
8810: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
8820: 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
8830: 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 }.
8840: 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 53 if( j==pS
8850: 63 61 6e 2d 3e 6e 45 71 75 69 76 20 29 7b 0a 20 can->nEquiv ){.
8860: 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63 pSc
8870: 61 6e 2d 3e 61 45 71 75 69 76 5b 6a 5d 20 3d 20 an->aEquiv[j] =
8880: 70 58 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 pX->iTable;.
8890: 20 20 20 20 20 20 20 20 20 20 70 53 63 61 6e 2d pScan-
88a0: 3e 61 45 71 75 69 76 5b 6a 2b 31 5d 20 3d 20 70 >aEquiv[j+1] = p
88b0: 58 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 X->iColumn;.
88c0: 20 20 20 20 20 20 20 20 20 20 70 53 63 61 6e 2d pScan-
88d0: 3e 6e 45 71 75 69 76 20 2b 3d 20 32 3b 0a 20 20 >nEquiv += 2;.
88e0: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
88f0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
8900: 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f if( (pTerm->eO
8910: 70 65 72 61 74 6f 72 20 26 20 70 53 63 61 6e 2d perator & pScan-
8920: 3e 6f 70 4d 61 73 6b 29 21 3d 30 20 29 7b 0a 20 >opMask)!=0 ){.
8930: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 65 /* Ve
8940: 72 69 66 79 20 74 68 65 20 61 66 66 69 6e 69 74 rify the affinit
8950: 79 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20 y and collating
8960: 73 65 71 75 65 6e 63 65 20 6d 61 74 63 68 20 2a sequence match *
8970: 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 /. if
8980: 28 20 70 53 63 61 6e 2d 3e 7a 43 6f 6c 6c 4e 61 ( pScan->zCollNa
8990: 6d 65 20 26 26 20 28 70 54 65 72 6d 2d 3e 65 4f me && (pTerm->eO
89a0: 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e perator & WO_ISN
89b0: 55 4c 4c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ULL)==0 ){.
89c0: 20 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 CollSeq
89d0: 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 *pColl;.
89e0: 20 20 20 20 20 20 20 50 61 72 73 65 20 2a 70 50 Parse *pP
89f0: 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 57 49 6e arse = pWC->pWIn
8a00: 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 fo->pParse;.
8a10: 20 20 20 20 20 20 20 20 20 20 70 58 20 3d 20 70 pX = p
8a20: 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Term->pExpr;.
8a30: 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 if( !
8a40: 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 sqlite3IndexAffi
8a50: 6e 69 74 79 4f 6b 28 70 58 2c 20 70 53 63 61 6e nityOk(pX, pScan
8a60: 2d 3e 69 64 78 61 66 66 29 20 29 7b 0a 20 20 20 ->idxaff) ){.
8a70: 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e con
8a80: 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 tinue;.
8a90: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 }.
8aa0: 20 20 20 20 20 61 73 73 65 72 74 28 70 58 2d 3e assert(pX->
8ab0: 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 20 20 pLeft);.
8ac0: 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 pColl = sq
8ad0: 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 lite3BinaryCompa
8ae0: 72 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 reCollSeq(pParse
8af0: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
8b00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8b10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8b20: 20 20 20 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 pX->pLeft, p
8b30: 58 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 X->pRight);.
8b40: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 if( pC
8b50: 6f 6c 6c 3d 3d 30 20 29 20 70 43 6f 6c 6c 20 3d oll==0 ) pColl =
8b60: 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 pParse->db->pDf
8b70: 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 ltColl;.
8b80: 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
8b90: 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 3StrICmp(pColl->
8ba0: 7a 4e 61 6d 65 2c 20 70 53 63 61 6e 2d 3e 7a 43 zName, pScan->zC
8bb0: 6f 6c 6c 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 ollName) ){.
8bc0: 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 cont
8bd0: 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 inue;.
8be0: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
8bf0: 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
8c00: 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 if( (pTerm->eOpe
8c10: 72 61 74 6f 72 20 26 20 57 4f 5f 45 51 29 21 3d rator & WO_EQ)!=
8c20: 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 0. &
8c30: 26 20 28 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 & (pX = pTerm->p
8c40: 45 78 70 72 2d 3e 70 52 69 67 68 74 29 2d 3e 6f Expr->pRight)->o
8c50: 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 0a 20 20 20 p==TK_COLUMN.
8c60: 20 20 20 20 20 20 20 20 20 20 26 26 20 70 58 2d && pX-
8c70: 3e 69 54 61 62 6c 65 3d 3d 70 53 63 61 6e 2d 3e >iTable==pScan->
8c80: 61 45 71 75 69 76 5b 30 5d 0a 20 20 20 20 20 20 aEquiv[0].
8c90: 20 20 20 20 20 20 20 26 26 20 70 58 2d 3e 69 43 && pX->iC
8ca0: 6f 6c 75 6d 6e 3d 3d 70 53 63 61 6e 2d 3e 61 45 olumn==pScan->aE
8cb0: 71 75 69 76 5b 31 5d 0a 20 20 20 20 20 20 20 20 quiv[1].
8cc0: 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ){.
8cd0: 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 continue;.
8ce0: 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
8cf0: 20 20 20 20 20 20 20 20 20 70 53 63 61 6e 2d 3e pScan->
8d00: 6b 20 3d 20 6b 2b 31 3b 0a 20 20 20 20 20 20 20 k = k+1;.
8d10: 20 20 20 20 20 72 65 74 75 72 6e 20 70 54 65 72 return pTer
8d20: 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 m;. }.
8d30: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d }. }
8d40: 0a 20 20 20 20 20 20 70 53 63 61 6e 2d 3e 70 57 . pScan->pW
8d50: 43 20 3d 20 70 53 63 61 6e 2d 3e 70 57 43 2d 3e C = pScan->pWC->
8d60: 70 4f 75 74 65 72 3b 0a 20 20 20 20 20 20 6b 20 pOuter;. k
8d70: 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 = 0;. }. p
8d80: 53 63 61 6e 2d 3e 70 57 43 20 3d 20 70 53 63 61 Scan->pWC = pSca
8d90: 6e 2d 3e 70 4f 72 69 67 57 43 3b 0a 20 20 20 20 n->pOrigWC;.
8da0: 6b 20 3d 20 30 3b 0a 20 20 20 20 70 53 63 61 6e k = 0;. pScan
8db0: 2d 3e 69 45 71 75 69 76 20 2b 3d 20 32 3b 0a 20 ->iEquiv += 2;.
8dc0: 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d }. return 0;.}
8dd0: 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 ../*.** Initiali
8de0: 7a 65 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 ze a WHERE claus
8df0: 65 20 73 63 61 6e 6e 65 72 20 6f 62 6a 65 63 74 e scanner object
8e00: 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e . Return a poin
8e10: 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69 ter to the.** fi
8e20: 72 73 74 20 6d 61 74 63 68 2e 20 20 52 65 74 75 rst match. Retu
8e30: 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 rn NULL if there
8e40: 20 61 72 65 20 6e 6f 20 6d 61 74 63 68 65 73 2e are no matches.
8e50: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6e 6e .**.** The scann
8e60: 65 72 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 er will be searc
8e70: 68 69 6e 67 20 74 68 65 20 57 48 45 52 45 20 63 hing the WHERE c
8e80: 6c 61 75 73 65 20 70 57 43 2e 20 20 49 74 20 77 lause pWC. It w
8e90: 69 6c 6c 20 6c 6f 6f 6b 0a 2a 2a 20 66 6f 72 20 ill look.** for
8ea0: 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 72 terms of the for
8eb0: 6d 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e m "X <op> <expr>
8ec0: 22 20 77 68 65 72 65 20 58 20 69 73 20 63 6f 6c " where X is col
8ed0: 75 6d 6e 20 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 umn iColumn of t
8ee0: 61 62 6c 65 0a 2a 2a 20 69 43 75 72 2e 20 20 54 able.** iCur. T
8ef0: 68 65 20 3c 6f 70 3e 20 6d 75 73 74 20 62 65 20 he <op> must be
8f00: 6f 6e 65 20 6f 66 20 74 68 65 20 6f 70 65 72 61 one of the opera
8f10: 74 6f 72 73 20 64 65 73 63 72 69 62 65 64 20 62 tors described b
8f20: 79 20 6f 70 4d 61 73 6b 2e 0a 2a 2a 0a 2a 2a 20 y opMask..**.**
8f30: 49 66 20 74 68 65 20 73 65 61 72 63 68 20 69 73 If the search is
8f40: 20 66 6f 72 20 58 20 61 6e 64 20 74 68 65 20 57 for X and the W
8f50: 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 74 HERE clause cont
8f60: 61 69 6e 73 20 74 65 72 6d 73 20 6f 66 20 74 68 ains terms of th
8f70: 65 0a 2a 2a 20 66 6f 72 6d 20 58 3d 59 20 74 68 e.** form X=Y th
8f80: 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 en this routine
8f90: 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75 72 might also retur
8fa0: 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 n terms of the f
8fb0: 6f 72 6d 0a 2a 2a 20 22 59 20 3c 6f 70 3e 20 3c orm.** "Y <op> <
8fc0: 65 78 70 72 3e 22 2e 20 20 54 68 65 20 6e 75 6d expr>". The num
8fd0: 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 ber of levels of
8fe0: 20 74 72 61 6e 73 69 74 69 76 69 74 79 20 69 73 transitivity is
8ff0: 20 6c 69 6d 69 74 65 64 2c 0a 2a 2a 20 62 75 74 limited,.** but
9000: 20 69 73 20 65 6e 6f 75 67 68 20 74 6f 20 68 61 is enough to ha
9010: 6e 64 6c 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e ndle most common
9020: 6c 79 20 6f 63 63 75 72 72 69 6e 67 20 53 51 4c ly occurring SQL
9030: 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a statements..**.
9040: 2a 2a 20 49 66 20 58 20 69 73 20 6e 6f 74 20 74 ** If X is not t
9050: 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 he INTEGER PRIMA
9060: 52 59 20 4b 45 59 20 74 68 65 6e 20 58 20 6d 75 RY KEY then X mu
9070: 73 74 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 st be compatible
9080: 20 77 69 74 68 0a 2a 2a 20 69 6e 64 65 78 20 70 with.** index p
9090: 49 64 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 Idx..*/.static W
90a0: 68 65 72 65 54 65 72 6d 20 2a 77 68 65 72 65 53 hereTerm *whereS
90b0: 63 61 6e 49 6e 69 74 28 0a 20 20 57 68 65 72 65 canInit(. Where
90c0: 53 63 61 6e 20 2a 70 53 63 61 6e 2c 20 20 20 20 Scan *pScan,
90d0: 20 20 20 2f 2a 20 54 68 65 20 57 68 65 72 65 53 /* The WhereS
90e0: 63 61 6e 20 6f 62 6a 65 63 74 20 62 65 69 6e 67 can object being
90f0: 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a initialized */.
9100: 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 WhereClause *p
9110: 57 43 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 WC, /* The
9120: 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f WHERE clause to
9130: 20 62 65 20 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 be scanned */.
9140: 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 int iCur,
9150: 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 /* Curs
9160: 6f 72 20 74 6f 20 73 63 61 6e 20 66 6f 72 20 2a or to scan for *
9170: 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c /. int iColumn,
9180: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
9190: 6f 6c 75 6d 6e 20 74 6f 20 73 63 61 6e 20 66 6f olumn to scan fo
91a0: 72 20 2a 2f 0a 20 20 75 33 32 20 6f 70 4d 61 73 r */. u32 opMas
91b0: 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f k, /
91c0: 2a 20 4f 70 65 72 61 74 6f 72 28 73 29 20 74 6f * Operator(s) to
91d0: 20 73 63 61 6e 20 66 6f 72 20 2a 2f 0a 20 20 49 scan for */. I
91e0: 6e 64 65 78 20 2a 70 49 64 78 20 20 20 20 20 20 ndex *pIdx
91f0: 20 20 20 20 20 20 20 2f 2a 20 4d 75 73 74 20 62 /* Must b
9200: 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 e compatible wit
9210: 68 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a h this index */.
9220: 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 0a 20 20 2f ){. int j;.. /
9230: 2a 20 6d 65 6d 73 65 74 28 70 53 63 61 6e 2c 20 * memset(pScan,
9240: 30 2c 20 73 69 7a 65 6f 66 28 2a 70 53 63 61 6e 0, sizeof(*pScan
9250: 29 29 3b 20 2a 2f 0a 20 20 70 53 63 61 6e 2d 3e )); */. pScan->
9260: 70 4f 72 69 67 57 43 20 3d 20 70 57 43 3b 0a 20 pOrigWC = pWC;.
9270: 20 70 53 63 61 6e 2d 3e 70 57 43 20 3d 20 70 57 pScan->pWC = pW
9280: 43 3b 0a 20 20 69 66 28 20 70 49 64 78 20 26 26 C;. if( pIdx &&
9290: 20 69 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 iColumn>=0 ){.
92a0: 20 20 20 70 53 63 61 6e 2d 3e 69 64 78 61 66 66 pScan->idxaff
92b0: 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d = pIdx->pTable-
92c0: 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 >aCol[iColumn].a
92d0: 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 66 6f 72 ffinity;. for
92e0: 28 6a 3d 30 3b 20 70 49 64 78 2d 3e 61 69 43 6f (j=0; pIdx->aiCo
92f0: 6c 75 6d 6e 5b 6a 5d 21 3d 69 43 6f 6c 75 6d 6e lumn[j]!=iColumn
9300: 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ; j++){. if
9310: 28 20 4e 45 56 45 52 28 6a 3e 3d 70 49 64 78 2d ( NEVER(j>=pIdx-
9320: 3e 6e 43 6f 6c 75 6d 6e 29 20 29 20 72 65 74 75 >nColumn) ) retu
9330: 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 rn 0;. }.
9340: 70 53 63 61 6e 2d 3e 7a 43 6f 6c 6c 4e 61 6d 65 pScan->zCollName
9350: 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b = pIdx->azColl[
9360: 6a 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 j];. }else{.
9370: 20 70 53 63 61 6e 2d 3e 69 64 78 61 66 66 20 3d pScan->idxaff =
9380: 20 30 3b 0a 20 20 20 20 70 53 63 61 6e 2d 3e 7a 0;. pScan->z
9390: 43 6f 6c 6c 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 CollName = 0;.
93a0: 7d 0a 20 20 70 53 63 61 6e 2d 3e 6f 70 4d 61 73 }. pScan->opMas
93b0: 6b 20 3d 20 6f 70 4d 61 73 6b 3b 0a 20 20 70 53 k = opMask;. pS
93c0: 63 61 6e 2d 3e 6b 20 3d 20 30 3b 0a 20 20 70 53 can->k = 0;. pS
93d0: 63 61 6e 2d 3e 61 45 71 75 69 76 5b 30 5d 20 3d can->aEquiv[0] =
93e0: 20 69 43 75 72 3b 0a 20 20 70 53 63 61 6e 2d 3e iCur;. pScan->
93f0: 61 45 71 75 69 76 5b 31 5d 20 3d 20 69 43 6f 6c aEquiv[1] = iCol
9400: 75 6d 6e 3b 0a 20 20 70 53 63 61 6e 2d 3e 6e 45 umn;. pScan->nE
9410: 71 75 69 76 20 3d 20 32 3b 0a 20 20 70 53 63 61 quiv = 2;. pSca
9420: 6e 2d 3e 69 45 71 75 69 76 20 3d 20 32 3b 0a 20 n->iEquiv = 2;.
9430: 20 72 65 74 75 72 6e 20 77 68 65 72 65 53 63 61 return whereSca
9440: 6e 4e 65 78 74 28 70 53 63 61 6e 29 3b 0a 7d 0a nNext(pScan);.}.
9450: 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66 6f ./*.** Search fo
9460: 72 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 r a term in the
9470: 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 WHERE clause tha
9480: 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d t is of the form
9490: 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 "X <op> <expr>"
94a0: 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20 61 .** where X is a
94b0: 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 reference to th
94c0: 65 20 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 e iColumn of tab
94d0: 6c 65 20 69 43 75 72 20 61 6e 64 20 3c 6f 70 3e le iCur and <op>
94e0: 20 69 73 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 is one of.** th
94f0: 65 20 57 4f 5f 78 78 20 6f 70 65 72 61 74 6f 72 e WO_xx operator
9500: 20 63 6f 64 65 73 20 73 70 65 63 69 66 69 65 64 codes specified
9510: 20 62 79 20 74 68 65 20 6f 70 20 70 61 72 61 6d by the op param
9520: 65 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 eter..** Return
9530: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
9540: 20 74 65 72 6d 2e 20 20 52 65 74 75 72 6e 20 30 term. Return 0
9550: 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a if not found..*
9560: 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 20 72 65 *.** The term re
9570: 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 79 20 turned might by
9580: 59 3d 3c 65 78 70 72 3e 20 69 66 20 74 68 65 72 Y=<expr> if ther
9590: 65 20 69 73 20 61 6e 6f 74 68 65 72 20 63 6f 6e e is another con
95a0: 73 74 72 61 69 6e 74 20 69 6e 0a 2a 2a 20 74 68 straint in.** th
95b0: 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 e WHERE clause t
95c0: 68 61 74 20 73 70 65 63 69 66 69 65 73 20 74 68 hat specifies th
95d0: 61 74 20 58 3d 59 2e 20 20 41 6e 79 20 73 75 63 at X=Y. Any suc
95e0: 68 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 69 h constraints wi
95f0: 6c 6c 20 62 65 0a 2a 2a 20 69 64 65 6e 74 69 66 ll be.** identif
9600: 69 65 64 20 62 79 20 74 68 65 20 57 4f 5f 45 51 ied by the WO_EQ
9610: 55 49 56 20 62 69 74 20 69 6e 20 74 68 65 20 70 UIV bit in the p
9620: 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 Term->eOperator
9630: 66 69 65 6c 64 2e 20 20 54 68 65 0a 2a 2a 20 61 field. The.** a
9640: 45 71 75 69 76 5b 5d 20 61 72 72 61 79 20 68 6f Equiv[] array ho
9650: 6c 64 73 20 58 20 61 6e 64 20 61 6c 6c 20 69 74 lds X and all it
9660: 73 20 65 71 75 69 76 61 6c 65 6e 74 73 2c 20 77 s equivalents, w
9670: 69 74 68 20 65 61 63 68 20 53 51 4c 20 76 61 72 ith each SQL var
9680: 69 61 62 6c 65 0a 2a 2a 20 74 61 6b 69 6e 67 20 iable.** taking
9690: 75 70 20 74 77 6f 20 73 6c 6f 74 73 20 69 6e 20 up two slots in
96a0: 61 45 71 75 69 76 5b 5d 2e 20 20 54 68 65 20 66 aEquiv[]. The f
96b0: 69 72 73 74 20 73 6c 6f 74 20 69 73 20 66 6f 72 irst slot is for
96c0: 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 the cursor numb
96d0: 65 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 er.** and the se
96e0: 63 6f 6e 64 20 69 73 20 66 6f 72 20 74 68 65 20 cond is for the
96f0: 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 20 20 column number.
9700: 54 68 65 72 65 20 61 72 65 20 32 32 20 73 6c 6f There are 22 slo
9710: 74 73 20 69 6e 20 61 45 71 75 69 76 5b 5d 0a 2a ts in aEquiv[].*
9720: 2a 20 73 6f 20 74 68 61 74 20 6d 65 61 6e 73 20 * so that means
9730: 77 65 20 63 61 6e 20 6c 6f 6f 6b 20 66 6f 72 20 we can look for
9740: 58 20 70 6c 75 73 20 75 70 20 74 6f 20 31 30 20 X plus up to 10
9750: 6f 74 68 65 72 20 65 71 75 69 76 61 6c 65 6e 74 other equivalent
9760: 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 48 65 6e 63 values..** Henc
9770: 65 20 61 20 73 65 61 72 63 68 20 66 6f 72 20 58 e a search for X
9780: 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 3c 65 78 will return <ex
9790: 70 72 3e 20 69 66 20 58 3d 41 31 20 61 6e 64 20 pr> if X=A1 and
97a0: 41 31 3d 41 32 20 61 6e 64 20 41 32 3d 41 33 0a A1=A2 and A2=A3.
97b0: 2a 2a 20 61 6e 64 20 2e 2e 2e 20 61 6e 64 20 41 ** and ... and A
97c0: 39 3d 41 31 30 20 61 6e 64 20 41 31 30 3d 3c 65 9=A10 and A10=<e
97d0: 78 70 72 3e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 xpr>..**.** If t
97e0: 68 65 72 65 20 61 72 65 20 6d 75 6c 74 69 70 6c here are multipl
97f0: 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 57 e terms in the W
9800: 48 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 HERE clause of t
9810: 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 he form "X <op>
9820: 3c 65 78 70 72 3e 22 0a 2a 2a 20 74 68 65 6e 20 <expr>".** then
9830: 74 72 79 20 66 6f 72 20 74 68 65 20 6f 6e 65 20 try for the one
9840: 77 69 74 68 20 6e 6f 20 64 65 70 65 6e 64 65 6e with no dependen
9850: 63 69 65 73 20 6f 6e 20 3c 65 78 70 72 3e 20 2d cies on <expr> -
9860: 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 in other words
9870: 77 68 65 72 65 0a 2a 2a 20 3c 65 78 70 72 3e 20 where.** <expr>
9880: 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 is a constant ex
9890: 70 72 65 73 73 69 6f 6e 20 6f 66 20 73 6f 6d 65 pression of some
98a0: 20 6b 69 6e 64 2e 20 20 4f 6e 6c 79 20 72 65 74 kind. Only ret
98b0: 75 72 6e 20 65 6e 74 72 69 65 73 20 6f 66 0a 2a urn entries of.*
98c0: 2a 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f * the form "X <o
98d0: 70 3e 20 59 22 20 77 68 65 72 65 20 59 20 69 73 p> Y" where Y is
98e0: 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 6e 6f a column in ano
98f0: 74 68 65 72 20 74 61 62 6c 65 20 69 66 20 6e 6f ther table if no
9900: 20 74 65 72 6d 73 20 6f 66 0a 2a 2a 20 74 68 65 terms of.** the
9910: 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 3c 63 form "X <op> <c
9920: 6f 6e 73 74 2d 65 78 70 72 3e 22 20 65 78 69 73 onst-expr>" exis
9930: 74 2e 20 20 20 49 66 20 6e 6f 20 74 65 72 6d 73 t. If no terms
9940: 20 77 69 74 68 20 61 20 63 6f 6e 73 74 61 6e 74 with a constant
9950: 20 52 48 53 0a 2a 2a 20 65 78 69 73 74 2c 20 74 RHS.** exist, t
9960: 72 79 20 74 6f 20 72 65 74 75 72 6e 20 61 20 74 ry to return a t
9970: 65 72 6d 20 74 68 61 74 20 64 6f 65 73 20 6e 6f erm that does no
9980: 74 20 75 73 65 20 57 4f 5f 45 51 55 49 56 2e 0a t use WO_EQUIV..
9990: 2a 2f 0a 73 74 61 74 69 63 20 57 68 65 72 65 54 */.static WhereT
99a0: 65 72 6d 20 2a 66 69 6e 64 54 65 72 6d 28 0a 20 erm *findTerm(.
99b0: 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 WhereClause *pW
99c0: 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 C, /* The WH
99d0: 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 ERE clause to be
99e0: 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 69 searched */. i
99f0: 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 nt iCur,
9a00: 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e /* Cursor n
9a10: 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a umber of LHS */.
9a20: 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 int iColumn,
9a30: 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d /* Colum
9a40: 6e 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 n number of LHS
9a50: 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 */. Bitmask not
9a60: 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20 52 48 Ready, /* RH
9a70: 53 20 6d 75 73 74 20 6e 6f 74 20 6f 76 65 72 6c S must not overl
9a80: 61 70 20 77 69 74 68 20 74 68 69 73 20 6d 61 73 ap with this mas
9a90: 6b 20 2a 2f 0a 20 20 75 33 32 20 6f 70 2c 20 20 k */. u32 op,
9aa0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
9ab0: 4d 61 73 6b 20 6f 66 20 57 4f 5f 78 78 20 76 61 Mask of WO_xx va
9ac0: 6c 75 65 73 20 64 65 73 63 72 69 62 69 6e 67 20 lues describing
9ad0: 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 49 6e operator */. In
9ae0: 64 65 78 20 2a 70 49 64 78 20 20 20 20 20 20 20 dex *pIdx
9af0: 20 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 63 /* Must be c
9b00: 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74 ompatible with t
9b10: 68 69 73 20 69 6e 64 65 78 2c 20 69 66 20 6e 6f his index, if no
9b20: 74 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 57 t NULL */.){. W
9b30: 68 65 72 65 54 65 72 6d 20 2a 70 52 65 73 75 6c hereTerm *pResul
9b40: 74 20 3d 20 30 3b 0a 20 20 57 68 65 72 65 54 65 t = 0;. WhereTe
9b50: 72 6d 20 2a 70 3b 0a 20 20 57 68 65 72 65 53 63 rm *p;. WhereSc
9b60: 61 6e 20 73 63 61 6e 3b 0a 0a 20 20 70 20 3d 20 an scan;.. p =
9b70: 77 68 65 72 65 53 63 61 6e 49 6e 69 74 28 26 73 whereScanInit(&s
9b80: 63 61 6e 2c 20 70 57 43 2c 20 69 43 75 72 2c 20 can, pWC, iCur,
9b90: 69 43 6f 6c 75 6d 6e 2c 20 6f 70 2c 20 70 49 64 iColumn, op, pId
9ba0: 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 x);. while( p )
9bb0: 7b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e 70 72 {. if( (p->pr
9bc0: 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f 74 52 ereqRight & notR
9bd0: 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20 eady)==0 ){.
9be0: 20 20 69 66 28 20 70 2d 3e 70 72 65 72 65 71 52 if( p->prereqR
9bf0: 69 67 68 74 3d 3d 30 20 26 26 20 28 70 2d 3e 65 ight==0 && (p->e
9c00: 4f 70 65 72 61 74 6f 72 26 57 4f 5f 45 51 29 21 Operator&WO_EQ)!
9c10: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 =0 ){. re
9c20: 74 75 72 6e 20 70 3b 0a 20 20 20 20 20 20 7d 0a turn p;. }.
9c30: 20 20 20 20 20 20 69 66 28 20 70 52 65 73 75 6c if( pResul
9c40: 74 3d 3d 30 20 29 20 70 52 65 73 75 6c 74 20 3d t==0 ) pResult =
9c50: 20 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 p;. }. p
9c60: 3d 20 77 68 65 72 65 53 63 61 6e 4e 65 78 74 28 = whereScanNext(
9c70: 26 73 63 61 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 &scan);. }. re
9c80: 74 75 72 6e 20 70 52 65 73 75 6c 74 3b 0a 7d 0a turn pResult;.}.
9c90: 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 ./* Forward refe
9ca0: 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 rence */.static
9cb0: 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 void exprAnalyze
9cc0: 28 53 72 63 4c 69 73 74 2a 2c 20 57 68 65 72 65 (SrcList*, Where
9cd0: 43 6c 61 75 73 65 2a 2c 20 69 6e 74 29 3b 0a 0a Clause*, int);..
9ce0: 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65 78 70 72 41 /*.** Call exprA
9cf0: 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20 74 65 nalyze on all te
9d00: 72 6d 73 20 69 6e 20 61 20 57 48 45 52 45 20 63 rms in a WHERE c
9d10: 6c 61 75 73 65 2e 20 20 0a 2a 2f 0a 73 74 61 74 lause. .*/.stat
9d20: 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c ic void exprAnal
9d30: 79 7a 65 41 6c 6c 28 0a 20 20 53 72 63 4c 69 73 yzeAll(. SrcLis
9d40: 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 t *pTabList,
9d50: 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 /* the FROM c
9d60: 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 lause */. Where
9d70: 43 6c 61 75 73 65 20 2a 70 57 43 20 20 20 20 20 Clause *pWC
9d80: 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 /* the WHERE
9d90: 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 61 6e clause to be an
9da0: 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 alyzed */.){. i
9db0: 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 70 57 nt i;. for(i=pW
9dc0: 43 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 C->nTerm-1; i>=0
9dd0: 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 65 78 70 72 ; i--){. expr
9de0: 41 6e 61 6c 79 7a 65 28 70 54 61 62 4c 69 73 74 Analyze(pTabList
9df0: 2c 20 70 57 43 2c 20 69 29 3b 0a 20 20 7d 0a 7d , pWC, i);. }.}
9e00: 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 ..#ifndef SQLITE
9e10: 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d _OMIT_LIKE_OPTIM
9e20: 49 5a 41 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68 IZATION./*.** Ch
9e30: 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 eck to see if th
9e40: 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 e given expressi
9e50: 6f 6e 20 69 73 20 61 20 4c 49 4b 45 20 6f 72 20 on is a LIKE or
9e60: 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 74 68 GLOB operator th
9e70: 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 70 74 at.** can be opt
9e80: 69 6d 69 7a 65 64 20 75 73 69 6e 67 20 69 6e 65 imized using ine
9e90: 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 quality constrai
9ea0: 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 54 52 55 nts. Return TRU
9eb0: 45 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 73 6f E if it is.** so
9ec0: 20 61 6e 64 20 66 61 6c 73 65 20 69 66 20 6e 6f and false if no
9ed0: 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 t..**.** In orde
9ee0: 72 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 r for the operat
9ef0: 6f 72 20 74 6f 20 62 65 20 6f 70 74 69 6d 69 7a or to be optimiz
9f00: 69 62 6c 65 2c 20 74 68 65 20 52 48 53 20 6d 75 ible, the RHS mu
9f10: 73 74 20 62 65 20 61 20 73 74 72 69 6e 67 0a 2a st be a string.*
9f20: 2a 20 6c 69 74 65 72 61 6c 20 74 68 61 74 20 64 * literal that d
9f30: 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 oes not begin wi
9f40: 74 68 20 61 20 77 69 6c 64 63 61 72 64 2e 20 20 th a wildcard.
9f50: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 .*/.static int i
9f60: 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 0a 20 20 50 sLikeOrGlob(. P
9f70: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
9f80: 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 /* Parsing and
9f90: 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 code generating
9fa0: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
9fb0: 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 2f r *pExpr, /
9fc0: 2a 20 54 65 73 74 20 74 68 69 73 20 65 78 70 72 * Test this expr
9fd0: 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 ession */. Expr
9fe0: 20 2a 2a 70 70 50 72 65 66 69 78 2c 20 20 2f 2a **ppPrefix, /*
9ff0: 20 50 6f 69 6e 74 65 72 20 74 6f 20 54 4b 5f 53 Pointer to TK_S
a000: 54 52 49 4e 47 20 65 78 70 72 65 73 73 69 6f 6e TRING expression
a010: 20 77 69 74 68 20 70 61 74 74 65 72 6e 20 70 72 with pattern pr
a020: 65 66 69 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 efix */. int *p
a030: 69 73 43 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20 54 isComplete, /* T
a040: 72 75 65 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 rue if the only
a050: 77 69 6c 64 63 61 72 64 20 69 73 20 25 20 69 6e wildcard is % in
a060: 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 the last charac
a070: 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e ter */. int *pn
a080: 6f 43 61 73 65 20 20 20 20 20 20 2f 2a 20 54 72 oCase /* Tr
a090: 75 65 20 69 66 20 75 70 70 65 72 63 61 73 65 20 ue if uppercase
a0a0: 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f is equivalent to
a0b0: 20 6c 6f 77 65 72 63 61 73 65 20 2a 2f 0a 29 7b lowercase */.){
a0c0: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
a0d0: 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
a0e0: 20 53 74 72 69 6e 67 20 6f 6e 20 52 48 53 20 6f String on RHS o
a0f0: 66 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 f LIKE operator
a100: 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 */. Expr *pRigh
a110: 74 2c 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 t, *pLeft;
a120: 2f 2a 20 52 69 67 68 74 20 61 6e 64 20 6c 65 66 /* Right and lef
a130: 74 20 73 69 7a 65 20 6f 66 20 4c 49 4b 45 20 6f t size of LIKE o
a140: 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 perator */. Exp
a150: 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 rList *pList;
a160: 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 /* List
a170: 6f 66 20 6f 70 65 72 61 6e 64 73 20 74 6f 20 74 of operands to t
a180: 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 he LIKE operator
a190: 20 2a 2f 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 */. int c;
a1a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a1b0: 20 2f 2a 20 4f 6e 65 20 63 68 61 72 61 63 74 65 /* One characte
a1c0: 72 20 69 6e 20 7a 5b 5d 20 2a 2f 0a 20 20 69 6e r in z[] */. in
a1d0: 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 t cnt;
a1e0: 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
a1f0: 65 72 20 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 er of non-wildca
a200: 72 64 20 70 72 65 66 69 78 20 63 68 61 72 61 63 rd prefix charac
a210: 74 65 72 73 20 2a 2f 0a 20 20 63 68 61 72 20 77 ters */. char w
a220: 63 5b 33 5d 3b 20 20 20 20 20 20 20 20 20 20 20 c[3];
a230: 20 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 /* Wildcard
a240: 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 characters */.
a250: 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
a260: 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 Parse->db; /* D
a270: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 atabase connecti
a280: 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f on */. sqlite3_
a290: 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30 3b value *pVal = 0;
a2a0: 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 . int op;
a2b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
a2c0: 20 4f 70 63 6f 64 65 20 6f 66 20 70 52 69 67 68 Opcode of pRigh
a2d0: 74 20 2a 2f 0a 0a 20 20 69 66 28 20 21 73 71 6c t */.. if( !sql
a2e0: 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 ite3IsLikeFuncti
a2f0: 6f 6e 28 64 62 2c 20 70 45 78 70 72 2c 20 70 6e on(db, pExpr, pn
a300: 6f 43 61 73 65 2c 20 77 63 29 20 29 7b 0a 20 20 oCase, wc) ){.
a310: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
a320: 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 #ifdef SQLITE_EB
a330: 43 44 49 43 0a 20 20 69 66 28 20 2a 70 6e 6f 43 CDIC. if( *pnoC
a340: 61 73 65 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ase ) return 0;.
a350: 23 65 6e 64 69 66 0a 20 20 70 4c 69 73 74 20 3d #endif. pList =
a360: 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b pExpr->x.pList;
a370: 0a 20 20 70 4c 65 66 74 20 3d 20 70 4c 69 73 74 . pLeft = pList
a380: 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[1].pExpr;.
a390: 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 21 3d 54 if( pLeft->op!=T
a3a0: 4b 5f 43 4f 4c 55 4d 4e 20 0a 20 20 20 7c 7c 20 K_COLUMN . ||
a3b0: 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e sqlite3ExprAffin
a3c0: 69 74 79 28 70 4c 65 66 74 29 21 3d 53 51 4c 49 ity(pLeft)!=SQLI
a3d0: 54 45 5f 41 46 46 5f 54 45 58 54 20 0a 20 20 20 TE_AFF_TEXT .
a3e0: 7c 7c 20 49 73 56 69 72 74 75 61 6c 28 70 4c 65 || IsVirtual(pLe
a3f0: 66 74 2d 3e 70 54 61 62 29 0a 20 20 29 7b 0a 20 ft->pTab). ){.
a400: 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 32 30 /* IMP: R-020
a410: 36 35 2d 34 39 34 36 35 20 54 68 65 20 6c 65 66 65-49465 The lef
a420: 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 t-hand side of t
a430: 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 he LIKE or GLOB
a440: 6f 70 65 72 61 74 6f 72 20 6d 75 73 74 0a 20 20 operator must.
a450: 20 20 2a 2a 20 62 65 20 74 68 65 20 6e 61 6d 65 ** be the name
a460: 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64 20 63 of an indexed c
a470: 6f 6c 75 6d 6e 20 77 69 74 68 20 54 45 58 54 20 olumn with TEXT
a480: 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 affinity. */.
a490: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 return 0;. }.
a4a0: 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e assert( pLeft->
a4b0: 69 43 6f 6c 75 6d 6e 21 3d 28 2d 31 29 20 29 3b iColumn!=(-1) );
a4c0: 20 2f 2a 20 42 65 63 61 75 73 65 20 49 50 4b 20 /* Because IPK
a4d0: 6e 65 76 65 72 20 68 61 73 20 41 46 46 5f 54 45 never has AFF_TE
a4e0: 58 54 20 2a 2f 0a 0a 20 20 70 52 69 67 68 74 20 XT */.. pRight
a4f0: 3d 20 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 = pList->a[0].pE
a500: 78 70 72 3b 0a 20 20 6f 70 20 3d 20 70 52 69 67 xpr;. op = pRig
a510: 68 74 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 ht->op;. if( op
a520: 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 7b ==TK_REGISTER ){
a530: 0a 20 20 20 20 6f 70 20 3d 20 70 52 69 67 68 74 . op = pRight
a540: 2d 3e 6f 70 32 3b 0a 20 20 7d 0a 20 20 69 66 28 ->op2;. }. if(
a550: 20 6f 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 op==TK_VARIABLE
a560: 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 70 52 ){. Vdbe *pR
a570: 65 70 72 65 70 61 72 65 20 3d 20 70 50 61 72 73 eprepare = pPars
a580: 65 2d 3e 70 52 65 70 72 65 70 61 72 65 3b 0a 20 e->pReprepare;.
a590: 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 52 int iCol = pR
a5a0: 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 ight->iColumn;.
a5b0: 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 pVal = sqlite
a5c0: 33 56 64 62 65 47 65 74 42 6f 75 6e 64 56 61 6c 3VdbeGetBoundVal
a5d0: 75 65 28 70 52 65 70 72 65 70 61 72 65 2c 20 69 ue(pReprepare, i
a5e0: 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f Col, SQLITE_AFF_
a5f0: 4e 4f 4e 45 29 3b 0a 20 20 20 20 69 66 28 20 70 NONE);. if( p
a600: 56 61 6c 20 26 26 20 73 71 6c 69 74 65 33 5f 76 Val && sqlite3_v
a610: 61 6c 75 65 5f 74 79 70 65 28 70 56 61 6c 29 3d alue_type(pVal)=
a620: 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 29 7b 0a =SQLITE_TEXT ){.
a630: 20 20 20 20 20 20 7a 20 3d 20 28 63 68 61 72 20 z = (char
a640: 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f *)sqlite3_value_
a650: 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20 20 20 text(pVal);.
a660: 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 }. sqlite3Vdb
a670: 65 53 65 74 56 61 72 6d 61 73 6b 28 70 50 61 72 eSetVarmask(pPar
a680: 73 65 2d 3e 70 56 64 62 65 2c 20 69 43 6f 6c 29 se->pVdbe, iCol)
a690: 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 ;. assert( pR
a6a0: 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 ight->op==TK_VAR
a6b0: 49 41 42 4c 45 20 7c 7c 20 70 52 69 67 68 74 2d IABLE || pRight-
a6c0: 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 >op==TK_REGISTER
a6d0: 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 );. }else if(
a6e0: 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 7b op==TK_STRING ){
a6f0: 0a 20 20 20 20 7a 20 3d 20 70 52 69 67 68 74 2d . z = pRight-
a700: 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 7d 0a 20 >u.zToken;. }.
a710: 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 63 6e if( z ){. cn
a720: 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 t = 0;. while
a730: 28 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d 30 20 ( (c=z[cnt])!=0
a740: 26 26 20 63 21 3d 77 63 5b 30 5d 20 26 26 20 63 && c!=wc[0] && c
a750: 21 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d 77 63 !=wc[1] && c!=wc
a760: 5b 32 5d 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 [2] ){. cnt
a770: 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
a780: 28 20 63 6e 74 21 3d 30 20 26 26 20 32 35 35 21 ( cnt!=0 && 255!
a790: 3d 28 75 38 29 7a 5b 63 6e 74 2d 31 5d 20 29 7b =(u8)z[cnt-1] ){
a7a0: 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 50 72 . Expr *pPr
a7b0: 65 66 69 78 3b 0a 20 20 20 20 20 20 2a 70 69 73 efix;. *pis
a7c0: 43 6f 6d 70 6c 65 74 65 20 3d 20 63 3d 3d 77 63 Complete = c==wc
a7d0: 5b 30 5d 20 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d [0] && z[cnt+1]=
a7e0: 3d 30 3b 0a 20 20 20 20 20 20 70 50 72 65 66 69 =0;. pPrefi
a7f0: 78 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 x = sqlite3Expr(
a800: 64 62 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20 7a db, TK_STRING, z
a810: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 );. if( pPr
a820: 65 66 69 78 20 29 20 70 50 72 65 66 69 78 2d 3e efix ) pPrefix->
a830: 75 2e 7a 54 6f 6b 65 6e 5b 63 6e 74 5d 20 3d 20 u.zToken[cnt] =
a840: 30 3b 0a 20 20 20 20 20 20 2a 70 70 50 72 65 66 0;. *ppPref
a850: 69 78 20 3d 20 70 50 72 65 66 69 78 3b 0a 20 20 ix = pPrefix;.
a860: 20 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 56 if( op==TK_V
a870: 41 52 49 41 42 4c 45 20 29 7b 0a 20 20 20 20 20 ARIABLE ){.
a880: 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 Vdbe *v = pPa
a890: 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 rse->pVdbe;.
a8a0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 sqlite3VdbeS
a8b0: 65 74 56 61 72 6d 61 73 6b 28 76 2c 20 70 52 69 etVarmask(v, pRi
a8c0: 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 ght->iColumn);.
a8d0: 20 20 20 20 20 20 20 69 66 28 20 2a 70 69 73 43 if( *pisC
a8e0: 6f 6d 70 6c 65 74 65 20 26 26 20 70 52 69 67 68 omplete && pRigh
a8f0: 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 20 29 t->u.zToken[1] )
a900: 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 {. /* I
a910: 66 20 74 68 65 20 72 68 73 20 6f 66 20 74 68 65 f the rhs of the
a920: 20 4c 49 4b 45 20 65 78 70 72 65 73 73 69 6f 6e LIKE expression
a930: 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 2c 20 is a variable,
a940: 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 0a and the current.
a950: 20 20 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c ** val
a960: 75 65 20 6f 66 20 74 68 65 20 76 61 72 69 61 62 ue of the variab
a970: 6c 65 20 6d 65 61 6e 73 20 74 68 65 72 65 20 69 le means there i
a980: 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 76 s no need to inv
a990: 6f 6b 65 20 74 68 65 20 4c 49 4b 45 0a 20 20 20 oke the LIKE.
a9a0: 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 ** functi
a9b0: 6f 6e 2c 20 74 68 65 6e 20 6e 6f 20 4f 50 5f 56 on, then no OP_V
a9c0: 61 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65 20 ariable will be
a9d0: 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 72 6f added to the pro
a9e0: 67 72 61 6d 2e 0a 20 20 20 20 20 20 20 20 20 20 gram..
a9f0: 2a 2a 20 54 68 69 73 20 63 61 75 73 65 73 20 70 ** This causes p
aa00: 72 6f 62 6c 65 6d 73 20 66 6f 72 20 74 68 65 20 roblems for the
aa10: 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 sqlite3_bind_par
aa20: 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 0a 20 20 ameter_name().
aa30: 20 20 20 20 20 20 20 20 2a 2a 20 41 50 49 2e 20 ** API.
aa40: 54 6f 20 77 6f 72 6b 61 72 6f 75 6e 64 20 74 68 To workaround th
aa50: 65 6d 2c 20 61 64 64 20 61 20 64 75 6d 6d 79 20 em, add a dummy
aa60: 4f 50 5f 56 61 72 69 61 62 6c 65 20 68 65 72 65 OP_Variable here
aa70: 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 20 0a .. */ .
aa80: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 int r1
aa90: 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
aaa0: 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 pReg(pParse);.
aab0: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 sqlite3E
aac0: 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 xprCodeTarget(pP
aad0: 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72 31 arse, pRight, r1
aae0: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c );. sql
aaf0: 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 ite3VdbeChangeP3
ab00: 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 (v, sqlite3VdbeC
ab10: 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 31 2c urrentAddr(v)-1,
ab20: 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 0);. s
ab30: 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d qlite3ReleaseTem
ab40: 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 pReg(pParse, r1)
ab50: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
ab60: 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 }. }else{.
ab70: 20 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20 z = 0;.
ab80: 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 }. }.. sqlite3
ab90: 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b ValueFree(pVal);
aba0: 0a 20 20 72 65 74 75 72 6e 20 28 7a 21 3d 30 29 . return (z!=0)
abb0: 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 ;.}.#endif /* SQ
abc0: 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f LITE_OMIT_LIKE_O
abd0: 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a PTIMIZATION */..
abe0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
abf0: 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
ac00: 45 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f E./*.** Check to
ac10: 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76 65 see if the give
ac20: 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 n expression is
ac30: 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a of the form.**.*
ac40: 2a 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e * column
ac50: 20 4d 41 54 43 48 20 65 78 70 72 0a 2a 2a 0a 2a MATCH expr.**.*
ac60: 2a 20 49 66 20 69 74 20 69 73 20 74 68 65 6e 20 * If it is then
ac70: 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49 66 return TRUE. If
ac80: 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46 41 4c not, return FAL
ac90: 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e SE..*/.static in
aca0: 74 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d t isMatchOfColum
acb0: 6e 28 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 n(. Expr *pExpr
acc0: 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68 /* Test th
acd0: 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f is expression */
ace0: 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a .){. ExprList *
acf0: 70 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70 45 pList;.. if( pE
ad00: 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 xpr->op!=TK_FUNC
ad10: 54 49 4f 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 TION ){. retu
ad20: 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 rn 0;. }. if(
ad30: 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 sqlite3StrICmp(p
ad40: 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 Expr->u.zToken,"
ad50: 6d 61 74 63 68 22 29 21 3d 30 20 29 7b 0a 20 20 match")!=0 ){.
ad60: 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a return 0;. }.
ad70: 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72 2d pList = pExpr-
ad80: 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 >x.pList;. if(
ad90: 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 20 pList->nExpr!=2
ada0: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
adb0: 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73 74 . }. if( pList
adc0: 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 ->a[1].pExpr->op
add0: 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b != TK_COLUMN ){
ade0: 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 . return 0;.
adf0: 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d }. return 1;.}
ae00: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
ae10: 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
ae20: 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 BLE */../*.** If
ae30: 20 74 68 65 20 70 42 61 73 65 20 65 78 70 72 65 the pBase expre
ae40: 73 73 69 6f 6e 20 6f 72 69 67 69 6e 61 74 65 64 ssion originated
ae50: 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 in the ON or US
ae60: 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a ING clause of.**
ae70: 20 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 72 a join, then tr
ae80: 61 6e 73 66 65 72 20 74 68 65 20 61 70 70 72 6f ansfer the appro
ae90: 70 72 69 61 74 65 20 6d 61 72 6b 69 6e 67 73 20 priate markings
aea0: 6f 76 65 72 20 74 6f 20 64 65 72 69 76 65 64 2e over to derived.
aeb0: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 .*/.static void
aec0: 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72 6b transferJoinMark
aed0: 69 6e 67 73 28 45 78 70 72 20 2a 70 44 65 72 69 ings(Expr *pDeri
aee0: 76 65 64 2c 20 45 78 70 72 20 2a 70 42 61 73 65 ved, Expr *pBase
aef0: 29 7b 0a 20 20 69 66 28 20 70 44 65 72 69 76 65 ){. if( pDerive
af00: 64 20 29 7b 0a 20 20 20 20 70 44 65 72 69 76 65 d ){. pDerive
af10: 64 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 42 61 73 d->flags |= pBas
af20: 65 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 46 72 e->flags & EP_Fr
af30: 6f 6d 4a 6f 69 6e 3b 0a 20 20 20 20 70 44 65 72 omJoin;. pDer
af40: 69 76 65 64 2d 3e 69 52 69 67 68 74 4a 6f 69 6e ived->iRightJoin
af50: 54 61 62 6c 65 20 3d 20 70 42 61 73 65 2d 3e 69 Table = pBase->i
af60: 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a RightJoinTable;.
af70: 20 20 7d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 }.}..#if !defi
af80: 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
af90: 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 OR_OPTIMIZATION)
afa0: 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
afb0: 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
afc0: 59 29 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65 Y)./*.** Analyze
afd0: 20 61 20 74 65 72 6d 20 74 68 61 74 20 63 6f 6e a term that con
afe0: 73 69 73 74 73 20 6f 66 20 74 77 6f 20 6f 72 20 sists of two or
aff0: 6d 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 more OR-connecte
b000: 64 0a 2a 2a 20 73 75 62 74 65 72 6d 73 2e 20 20 d.** subterms.
b010: 53 6f 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 So in:.**.**
b020: 20 2e 2e 2e 20 57 48 45 52 45 20 20 28 61 3d 35 ... WHERE (a=5
b030: 29 20 41 4e 44 20 28 62 3d 37 20 4f 52 20 63 3d ) AND (b=7 OR c=
b040: 39 20 4f 52 20 64 3d 31 33 29 20 41 4e 44 20 28 9 OR d=13) AND (
b050: 64 3d 31 33 29 0a 2a 2a 20 20 20 20 20 20 20 20 d=13).**
b060: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
b070: 20 20 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e ^^^^^^^^^^^^^^
b080: 5e 5e 5e 5e 5e 5e 0a 2a 2a 0a 2a 2a 20 54 68 69 ^^^^^^.**.** Thi
b090: 73 20 72 6f 75 74 69 6e 65 20 61 6e 61 6c 79 7a s routine analyz
b0a0: 65 73 20 74 65 72 6d 73 20 73 75 63 68 20 61 73 es terms such as
b0b0: 20 74 68 65 20 6d 69 64 64 6c 65 20 74 65 72 6d the middle term
b0c0: 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20 65 78 in the above ex
b0d0: 61 6d 70 6c 65 2e 0a 2a 2a 20 41 20 57 68 65 72 ample..** A Wher
b0e0: 65 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74 20 69 eOrTerm object i
b0f0: 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 61 s computed and a
b100: 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 74 ttached to the t
b110: 65 72 6d 20 75 6e 64 65 72 0a 2a 2a 20 61 6e 61 erm under.** ana
b120: 6c 79 73 69 73 2c 20 72 65 67 61 72 64 6c 65 73 lysis, regardles
b130: 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 s of the outcome
b140: 20 6f 66 20 74 68 65 20 61 6e 61 6c 79 73 69 73 of the analysis
b150: 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 . Hence:.**.**
b160: 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 77 74 WhereTerm.wt
b170: 46 6c 61 67 73 20 20 20 7c 3d 20 20 54 45 52 4d Flags |= TERM
b180: 5f 4f 52 49 4e 46 4f 0a 2a 2a 20 20 20 20 20 57 _ORINFO.** W
b190: 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e hereTerm.u.pOrIn
b1a0: 66 6f 20 20 3d 20 20 61 20 64 79 6e 61 6d 69 63 fo = a dynamic
b1b0: 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 ally allocated W
b1c0: 68 65 72 65 4f 72 54 65 72 6d 20 6f 62 6a 65 63 hereOrTerm objec
b1d0: 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d t.**.** The term
b1e0: 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 being analyzed
b1f0: 6d 75 73 74 20 68 61 76 65 20 74 77 6f 20 6f 72 must have two or
b200: 20 6d 6f 72 65 20 6f 66 20 4f 52 2d 63 6f 6e 6e more of OR-conn
b210: 65 63 74 65 64 20 73 75 62 74 65 72 6d 73 2e 0a ected subterms..
b220: 2a 2a 20 41 20 73 69 6e 67 6c 65 20 73 75 62 74 ** A single subt
b230: 65 72 6d 20 6d 69 67 68 74 20 62 65 20 61 20 73 erm might be a s
b240: 65 74 20 6f 66 20 41 4e 44 2d 63 6f 6e 6e 65 63 et of AND-connec
b250: 74 65 64 20 73 75 62 2d 73 75 62 74 65 72 6d 73 ted sub-subterms
b260: 2e 0a 2a 2a 20 45 78 61 6d 70 6c 65 73 20 6f 66 ..** Examples of
b270: 20 74 65 72 6d 73 20 75 6e 64 65 72 20 61 6e 61 terms under ana
b280: 6c 79 73 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 lysis:.**.**
b290: 20 28 41 29 20 20 20 20 20 74 31 2e 78 3d 74 32 (A) t1.x=t2
b2a0: 2e 79 20 4f 52 20 74 31 2e 78 3d 74 32 2e 7a 20 .y OR t1.x=t2.z
b2b0: 4f 52 20 74 31 2e 79 3d 31 35 20 4f 52 20 74 31 OR t1.y=15 OR t1
b2c0: 2e 7a 3d 74 33 2e 61 2b 35 0a 2a 2a 20 20 20 20 .z=t3.a+5.**
b2d0: 20 28 42 29 20 20 20 20 20 78 3d 65 78 70 72 31 (B) x=expr1
b2e0: 20 4f 52 20 65 78 70 72 32 3d 78 20 4f 52 20 78 OR expr2=x OR x
b2f0: 3d 65 78 70 72 33 0a 2a 2a 20 20 20 20 20 28 43 =expr3.** (C
b300: 29 20 20 20 20 20 74 31 2e 78 3d 74 32 2e 79 20 ) t1.x=t2.y
b310: 4f 52 20 28 74 31 2e 78 3d 74 32 2e 7a 20 41 4e OR (t1.x=t2.z AN
b320: 44 20 74 31 2e 79 3d 31 35 29 0a 2a 2a 20 20 20 D t1.y=15).**
b330: 20 20 28 44 29 20 20 20 20 20 78 3d 65 78 70 72 (D) x=expr
b340: 31 20 4f 52 20 28 79 3e 31 31 20 41 4e 44 20 79 1 OR (y>11 AND y
b350: 3c 32 32 20 41 4e 44 20 7a 20 4c 49 4b 45 20 27 <22 AND z LIKE '
b360: 2a 68 65 6c 6c 6f 2a 27 29 0a 2a 2a 20 20 20 20 *hello*').**
b370: 20 28 45 29 20 20 20 20 20 28 70 2e 61 3d 31 20 (E) (p.a=1
b380: 41 4e 44 20 71 2e 62 3d 32 20 41 4e 44 20 72 2e AND q.b=2 AND r.
b390: 63 3d 33 29 20 4f 52 20 28 70 2e 78 3d 34 20 41 c=3) OR (p.x=4 A
b3a0: 4e 44 20 71 2e 79 3d 35 20 41 4e 44 20 72 2e 7a ND q.y=5 AND r.z
b3b0: 3d 36 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20 31 =6).**.** CASE 1
b3c0: 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 :.**.** If all s
b3d0: 75 62 74 65 72 6d 73 20 61 72 65 20 6f 66 20 74 ubterms are of t
b3e0: 68 65 20 66 6f 72 6d 20 54 2e 43 3d 65 78 70 72 he form T.C=expr
b3f0: 20 66 6f 72 20 73 6f 6d 65 20 73 69 6e 67 6c 65 for some single
b400: 20 63 6f 6c 75 6d 6e 20 6f 66 20 43 20 61 6e 64 column of C and
b410: 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 .** a single tab
b420: 6c 65 20 54 20 28 61 73 20 73 68 6f 77 6e 20 69 le T (as shown i
b430: 6e 20 65 78 61 6d 70 6c 65 20 42 20 61 62 6f 76 n example B abov
b440: 65 29 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 e) then create a
b450: 20 6e 65 77 20 76 69 72 74 75 61 6c 0a 2a 2a 20 new virtual.**
b460: 74 65 72 6d 20 74 68 61 74 20 69 73 20 61 6e 20 term that is an
b470: 65 71 75 69 76 61 6c 65 6e 74 20 49 4e 20 65 78 equivalent IN ex
b480: 70 72 65 73 73 69 6f 6e 2e 20 20 49 6e 20 6f 74 pression. In ot
b490: 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 her words, if th
b4a0: 65 20 74 65 72 6d 0a 2a 2a 20 62 65 69 6e 67 20 e term.** being
b4b0: 61 6e 61 6c 79 7a 65 64 20 69 73 3a 0a 2a 2a 0a analyzed is:.**.
b4c0: 2a 2a 20 20 20 20 20 20 78 20 3d 20 65 78 70 72 ** x = expr
b4d0: 31 20 20 4f 52 20 20 65 78 70 72 32 20 3d 20 78 1 OR expr2 = x
b4e0: 20 20 4f 52 20 20 78 20 3d 20 65 78 70 72 33 0a OR x = expr3.
b4f0: 2a 2a 0a 2a 2a 20 74 68 65 6e 20 63 72 65 61 74 **.** then creat
b500: 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 e a new virtual
b510: 74 65 72 6d 20 6c 69 6b 65 20 74 68 69 73 3a 0a term like this:.
b520: 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e 20 **.** x IN
b530: 28 65 78 70 72 31 2c 65 78 70 72 32 2c 65 78 70 (expr1,expr2,exp
b540: 72 33 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20 32 r3).**.** CASE 2
b550: 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 :.**.** If all s
b560: 75 62 74 65 72 6d 73 20 61 72 65 20 69 6e 64 65 ubterms are inde
b570: 78 61 62 6c 65 20 62 79 20 61 20 73 69 6e 67 6c xable by a singl
b580: 65 20 74 61 62 6c 65 20 54 2c 20 74 68 65 6e 20 e table T, then
b590: 73 65 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57 68 set.**.** Wh
b5a0: 65 72 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f ereTerm.eOperato
b5b0: 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3d r =
b5c0: 20 20 57 4f 5f 4f 52 0a 2a 2a 20 20 20 20 20 57 WO_OR.** W
b5d0: 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e hereTerm.u.pOrIn
b5e0: 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 20 7c fo->indexable |
b5f0: 3d 20 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 = the cursor nu
b600: 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20 54 mber for table T
b610: 0a 2a 2a 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d .**.** A subterm
b620: 20 69 73 20 22 69 6e 64 65 78 61 62 6c 65 22 20 is "indexable"
b630: 69 66 20 69 74 20 69 73 20 6f 66 20 74 68 65 20 if it is of the
b640: 66 6f 72 6d 0a 2a 2a 20 22 54 2e 43 20 3c 6f 70 form.** "T.C <op
b650: 3e 20 3c 65 78 70 72 3e 22 20 77 68 65 72 65 20 > <expr>" where
b660: 43 20 69 73 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 C is any column
b670: 6f 66 20 74 61 62 6c 65 20 54 20 61 6e 64 20 0a of table T and .
b680: 2a 2a 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f ** <op> is one o
b690: 66 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3c 3d 22 f "=", "<", "<="
b6a0: 2c 20 22 3e 22 2c 20 22 3e 3d 22 2c 20 22 49 53 , ">", ">=", "IS
b6b0: 20 4e 55 4c 4c 22 2c 20 6f 72 20 22 49 4e 22 2e NULL", or "IN".
b6c0: 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d 20 69 73 .** A subterm is
b6d0: 20 61 6c 73 6f 20 69 6e 64 65 78 61 62 6c 65 20 also indexable
b6e0: 69 66 20 69 74 20 69 73 20 61 6e 20 41 4e 44 20 if it is an AND
b6f0: 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a of two or more.*
b700: 2a 20 73 75 62 73 75 62 74 65 72 6d 73 20 61 74 * subsubterms at
b710: 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66 20 77 68 least one of wh
b720: 69 63 68 20 69 73 20 69 6e 64 65 78 61 62 6c 65 ich is indexable
b730: 2e 20 20 49 6e 64 65 78 61 62 6c 65 20 41 4e 44 . Indexable AND
b740: 20 0a 2a 2a 20 73 75 62 74 65 72 6d 73 20 68 61 .** subterms ha
b750: 76 65 20 74 68 65 69 72 20 65 4f 70 65 72 61 74 ve their eOperat
b760: 6f 72 20 73 65 74 20 74 6f 20 57 4f 5f 41 4e 44 or set to WO_AND
b770: 20 61 6e 64 20 74 68 65 79 20 68 61 76 65 0a 2a and they have.*
b780: 2a 20 75 2e 70 41 6e 64 49 6e 66 6f 20 73 65 74 * u.pAndInfo set
b790: 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c to a dynamicall
b7a0: 79 20 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72 y allocated Wher
b7b0: 65 41 6e 64 54 65 72 6d 20 6f 62 6a 65 63 74 2e eAndTerm object.
b7c0: 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d 20 61 6e 6f 74 .**.** From anot
b7d0: 68 65 72 20 70 6f 69 6e 74 20 6f 66 20 76 69 65 her point of vie
b7e0: 77 2c 20 22 69 6e 64 65 78 61 62 6c 65 22 20 6d w, "indexable" m
b7f0: 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 73 75 eans that the su
b800: 62 74 65 72 6d 20 63 6f 75 6c 64 0a 2a 2a 20 70 bterm could.** p
b810: 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20 75 73 otentially be us
b820: 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 ed with an index
b830: 20 69 66 20 61 6e 20 61 70 70 72 6f 70 72 69 61 if an appropria
b840: 74 65 20 69 6e 64 65 78 20 65 78 69 73 74 73 2e te index exists.
b850: 0a 2a 2a 20 54 68 69 73 20 61 6e 61 6c 79 73 69 .** This analysi
b860: 73 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 73 69 s does not consi
b870: 64 65 72 20 77 68 65 74 68 65 72 20 6f 72 20 6e der whether or n
b880: 6f 74 20 74 68 65 20 69 6e 64 65 78 20 65 78 69 ot the index exi
b890: 73 74 73 3b 20 74 68 61 74 0a 2a 2a 20 69 73 20 sts; that.** is
b8a0: 64 65 63 69 64 65 64 20 65 6c 73 65 77 68 65 72 decided elsewher
b8b0: 65 2e 20 20 54 68 69 73 20 61 6e 61 6c 79 73 69 e. This analysi
b8c0: 73 20 6f 6e 6c 79 20 6c 6f 6f 6b 73 20 61 74 20 s only looks at
b8d0: 77 68 65 74 68 65 72 20 73 75 62 74 65 72 6d 73 whether subterms
b8e0: 0a 2a 2a 20 61 70 70 72 6f 70 72 69 61 74 65 20 .** appropriate
b8f0: 66 6f 72 20 69 6e 64 65 78 69 6e 67 20 65 78 69 for indexing exi
b900: 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 78 st..**.** All ex
b910: 61 6d 70 6c 65 73 20 41 20 74 68 72 6f 75 67 68 amples A through
b920: 20 45 20 61 62 6f 76 65 20 73 61 74 69 73 66 79 E above satisfy
b930: 20 63 61 73 65 20 32 2e 20 20 42 75 74 20 69 66 case 2. But if
b940: 20 61 20 74 65 72 6d 0a 2a 2a 20 61 6c 73 6f 20 a term.** also
b950: 73 74 61 74 69 73 66 69 65 73 20 63 61 73 65 20 statisfies case
b960: 31 20 28 73 75 63 68 20 61 73 20 42 29 20 77 65 1 (such as B) we
b970: 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6f know that the o
b980: 70 74 69 6d 69 7a 65 72 20 77 69 6c 6c 0a 2a 2a ptimizer will.**
b990: 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 20 63 always prefer c
b9a0: 61 73 65 20 31 2c 20 73 6f 20 69 6e 20 74 68 61 ase 1, so in tha
b9b0: 74 20 63 61 73 65 20 77 65 20 70 72 65 74 65 6e t case we preten
b9c0: 64 20 74 68 61 74 20 63 61 73 65 20 32 20 69 73 d that case 2 is
b9d0: 20 6e 6f 74 0a 2a 2a 20 73 61 74 69 73 66 69 65 not.** satisfie
b9e0: 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 69 67 68 d..**.** It migh
b9f0: 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 t be the case th
ba00: 61 74 20 6d 75 6c 74 69 70 6c 65 20 74 61 62 6c at multiple tabl
ba10: 65 73 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65 es are indexable
ba20: 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a . For example,.
ba30: 2a 2a 20 28 45 29 20 61 62 6f 76 65 20 69 73 20 ** (E) above is
ba40: 69 6e 64 65 78 61 62 6c 65 20 6f 6e 20 74 61 62 indexable on tab
ba50: 6c 65 73 20 50 2c 20 51 2c 20 61 6e 64 20 52 2e les P, Q, and R.
ba60: 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20 74 68 61 .**.** Terms tha
ba70: 74 20 73 61 74 69 73 66 79 20 63 61 73 65 20 32 t satisfy case 2
ba80: 20 61 72 65 20 63 61 6e 64 69 64 61 74 65 73 20 are candidates
ba90: 66 6f 72 20 6c 6f 6f 6b 75 70 20 62 79 20 75 73 for lookup by us
baa0: 69 6e 67 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 ing.** separate
bab0: 69 6e 64 69 63 65 73 20 74 6f 20 66 69 6e 64 20 indices to find
bac0: 72 6f 77 69 64 73 20 66 6f 72 20 65 61 63 68 20 rowids for each
bad0: 73 75 62 74 65 72 6d 20 61 6e 64 20 63 6f 6d 70 subterm and comp
bae0: 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 75 6e 69 osing.** the uni
baf0: 6f 6e 20 6f 66 20 61 6c 6c 20 72 6f 77 69 64 73 on of all rowids
bb00: 20 75 73 69 6e 67 20 61 20 52 6f 77 53 65 74 20 using a RowSet
bb10: 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69 73 object. This is
bb20: 20 73 69 6d 69 6c 61 72 0a 2a 2a 20 74 6f 20 22 similar.** to "
bb30: 62 69 74 6d 61 70 20 69 6e 64 69 63 65 73 22 20 bitmap indices"
bb40: 69 6e 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 in other databas
bb50: 65 20 65 6e 67 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a e engines..**.**
bb60: 20 4f 54 48 45 52 57 49 53 45 3a 0a 2a 2a 0a 2a OTHERWISE:.**.*
bb70: 2a 20 49 66 20 6e 65 69 74 68 65 72 20 63 61 73 * If neither cas
bb80: 65 20 31 20 6e 6f 72 20 63 61 73 65 20 32 20 61 e 1 nor case 2 a
bb90: 70 70 6c 79 2c 20 74 68 65 6e 20 6c 65 61 76 65 pply, then leave
bba0: 20 74 68 65 20 65 4f 70 65 72 61 74 6f 72 20 73 the eOperator s
bbb0: 65 74 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 20 20 et to.** zero.
bbc0: 54 68 69 73 20 74 65 72 6d 20 69 73 20 6e 6f 74 This term is not
bbd0: 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 61 72 useful for sear
bbe0: 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ch..*/.static vo
bbf0: 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f 72 id exprAnalyzeOr
bc00: 54 65 72 6d 28 0a 20 20 53 72 63 4c 69 73 74 20 Term(. SrcList
bc10: 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 *pSrc,
bc20: 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c /* the FROM cl
bc30: 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 ause */. WhereC
bc40: 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 lause *pWC,
bc50: 20 20 20 20 2f 2a 20 74 68 65 20 63 6f 6d 70 6c /* the compl
bc60: 65 74 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 ete WHERE clause
bc70: 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65 72 */. int idxTer
bc80: 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m
bc90: 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 /* Index of the
bca0: 4f 52 2d 74 65 72 6d 20 74 6f 20 62 65 20 61 6e OR-term to be an
bcb0: 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 alyzed */.){. W
bcc0: 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f hereInfo *pWInfo
bcd0: 20 3d 20 70 57 43 2d 3e 70 57 49 6e 66 6f 3b 20 = pWC->pWInfo;
bce0: 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 /* WHERE
bcf0: 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e clause processin
bd00: 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 g context */. P
bd10: 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 70 arse *pParse = p
bd20: 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 20 20 WInfo->pParse;
bd30: 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 /* Parser
bd40: 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71 context */. sq
bd50: 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
bd60: 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 se->db;
bd70: 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
bd80: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
bd90: 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 WhereTerm *pTe
bda0: 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 rm = &pWC->a[idx
bdb0: 54 65 72 6d 5d 3b 20 20 20 20 2f 2a 20 54 68 65 Term]; /* The
bdc0: 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c term to be anal
bdd0: 79 7a 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a yzed */. Expr *
bde0: 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 pExpr = pTerm->p
bdf0: 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 Expr;
be00: 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 /* The express
be10: 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 72 6d 20 ion of the term
be20: 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 */. int i;
be30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
be40: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
be50: 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f Loop counters */
be60: 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a . WhereClause *
be70: 70 4f 72 57 63 3b 20 20 20 20 20 20 20 2f 2a 20 pOrWc; /*
be80: 42 72 65 61 6b 75 70 20 6f 66 20 70 54 65 72 6d Breakup of pTerm
be90: 20 69 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a into subterms *
bea0: 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 /. WhereTerm *p
beb0: 4f 72 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a OrTerm; /*
bec0: 20 41 20 53 75 62 2d 74 65 72 6d 20 77 69 74 68 A Sub-term with
bed0: 69 6e 20 74 68 65 20 70 4f 72 57 63 20 2a 2f 0a in the pOrWc */.
bee0: 20 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70 WhereOrInfo *p
bef0: 4f 72 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 41 OrInfo; /* A
bf00: 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d dditional inform
bf10: 61 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 ation associated
bf20: 20 77 69 74 68 20 70 54 65 72 6d 20 2a 2f 0a 20 with pTerm */.
bf30: 20 42 69 74 6d 61 73 6b 20 63 68 6e 67 54 6f 49 Bitmask chngToI
bf40: 4e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 N; /* Ta
bf50: 62 6c 65 73 20 74 68 61 74 20 6d 69 67 68 74 20 bles that might
bf60: 73 61 74 69 73 66 79 20 63 61 73 65 20 31 20 2a satisfy case 1 *
bf70: 2f 0a 20 20 42 69 74 6d 61 73 6b 20 69 6e 64 65 /. Bitmask inde
bf80: 78 61 62 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a xable; /*
bf90: 20 54 61 62 6c 65 73 20 74 68 61 74 20 61 72 65 Tables that are
bfa0: 20 69 6e 64 65 78 61 62 6c 65 2c 20 73 61 74 69 indexable, sati
bfb0: 73 66 79 69 6e 67 20 63 61 73 65 20 32 20 2a 2f sfying case 2 */
bfc0: 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 42 72 65 61 .. /*. ** Brea
bfd0: 6b 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 k the OR clause
bfe0: 69 6e 74 6f 20 69 74 73 20 73 65 70 61 72 61 74 into its separat
bff0: 65 20 73 75 62 74 65 72 6d 73 2e 20 20 54 68 65 e subterms. The
c000: 20 73 75 62 74 65 72 6d 73 20 61 72 65 0a 20 20 subterms are.
c010: 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 61 20 57 ** stored in a W
c020: 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 hereClause struc
c030: 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 ture containing
c040: 77 69 74 68 69 6e 20 74 68 65 20 57 68 65 72 65 within the Where
c050: 4f 72 49 6e 66 6f 0a 20 20 2a 2a 20 6f 62 6a 65 OrInfo. ** obje
c060: 63 74 20 74 68 61 74 20 69 73 20 61 74 74 61 63 ct that is attac
c070: 68 65 64 20 74 6f 20 74 68 65 20 6f 72 69 67 69 hed to the origi
c080: 6e 61 6c 20 4f 52 20 63 6c 61 75 73 65 20 74 65 nal OR clause te
c090: 72 6d 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 rm.. */. asser
c0a0: 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 t( (pTerm->wtFla
c0b0: 67 73 20 26 20 28 54 45 52 4d 5f 44 59 4e 41 4d gs & (TERM_DYNAM
c0c0: 49 43 7c 54 45 52 4d 5f 4f 52 49 4e 46 4f 7c 54 IC|TERM_ORINFO|T
c0d0: 45 52 4d 5f 41 4e 44 49 4e 46 4f 29 29 3d 3d 30 ERM_ANDINFO))==0
c0e0: 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 );. assert( pE
c0f0: 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 xpr->op==TK_OR )
c100: 3b 0a 20 20 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 ;. pTerm->u.pOr
c110: 49 6e 66 6f 20 3d 20 70 4f 72 49 6e 66 6f 20 3d Info = pOrInfo =
c120: 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
c130: 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 Zero(db, sizeof(
c140: 2a 70 4f 72 49 6e 66 6f 29 29 3b 0a 20 20 69 66 *pOrInfo));. if
c150: 28 20 70 4f 72 49 6e 66 6f 3d 3d 30 20 29 20 72 ( pOrInfo==0 ) r
c160: 65 74 75 72 6e 3b 0a 20 20 70 54 65 72 6d 2d 3e eturn;. pTerm->
c170: 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f wtFlags |= TERM_
c180: 4f 52 49 4e 46 4f 3b 0a 20 20 70 4f 72 57 63 20 ORINFO;. pOrWc
c190: 3d 20 26 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a = &pOrInfo->wc;.
c1a0: 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 whereClauseIni
c1b0: 74 28 70 4f 72 57 63 2c 20 70 57 49 6e 66 6f 29 t(pOrWc, pWInfo)
c1c0: 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 ;. whereSplit(p
c1d0: 4f 72 57 63 2c 20 70 45 78 70 72 2c 20 54 4b 5f OrWc, pExpr, TK_
c1e0: 4f 52 29 3b 0a 20 20 65 78 70 72 41 6e 61 6c 79 OR);. exprAnaly
c1f0: 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70 4f 72 57 zeAll(pSrc, pOrW
c200: 63 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 c);. if( db->ma
c210: 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 llocFailed ) ret
c220: 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 urn;. assert( p
c230: 4f 72 57 63 2d 3e 6e 54 65 72 6d 3e 3d 32 20 29 OrWc->nTerm>=2 )
c240: 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6f 6d ;.. /*. ** Com
c250: 70 75 74 65 20 74 68 65 20 73 65 74 20 6f 66 20 pute the set of
c260: 74 61 62 6c 65 73 20 74 68 61 74 20 6d 69 67 68 tables that migh
c270: 74 20 73 61 74 69 73 66 79 20 63 61 73 65 73 20 t satisfy cases
c280: 31 20 6f 72 20 32 2e 0a 20 20 2a 2f 0a 20 20 69 1 or 2.. */. i
c290: 6e 64 65 78 61 62 6c 65 20 3d 20 7e 28 42 69 74 ndexable = ~(Bit
c2a0: 6d 61 73 6b 29 30 3b 0a 20 20 63 68 6e 67 54 6f mask)0;. chngTo
c2b0: 49 4e 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 IN = ~(Bitmask)0
c2c0: 3b 0a 20 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d ;. for(i=pOrWc-
c2d0: 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72 54 65 72 >nTerm-1, pOrTer
c2e0: 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 m=pOrWc->a; i>=0
c2f0: 20 26 26 20 69 6e 64 65 78 61 62 6c 65 3b 20 69 && indexable; i
c300: 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a --, pOrTerm++){.
c310: 20 20 20 20 69 66 28 20 28 70 4f 72 54 65 72 6d if( (pOrTerm
c320: 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f ->eOperator & WO
c330: 5f 53 49 4e 47 4c 45 29 3d 3d 30 20 29 7b 0a 20 _SINGLE)==0 ){.
c340: 20 20 20 20 20 57 68 65 72 65 41 6e 64 49 6e 66 WhereAndInf
c350: 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20 o *pAndInfo;.
c360: 20 20 20 61 73 73 65 72 74 28 20 28 70 4f 72 54 assert( (pOrT
c370: 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 erm->wtFlags & (
c380: 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 7c 54 45 52 TERM_ANDINFO|TER
c390: 4d 5f 4f 52 49 4e 46 4f 29 29 3d 3d 30 20 29 3b M_ORINFO))==0 );
c3a0: 0a 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 . chngToIN
c3b0: 3d 20 30 3b 0a 20 20 20 20 20 20 70 41 6e 64 49 = 0;. pAndI
c3c0: 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d nfo = sqlite3DbM
c3d0: 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a allocRaw(db, siz
c3e0: 65 6f 66 28 2a 70 41 6e 64 49 6e 66 6f 29 29 3b eof(*pAndInfo));
c3f0: 0a 20 20 20 20 20 20 69 66 28 20 70 41 6e 64 49 . if( pAndI
c400: 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 57 nfo ){. W
c410: 68 65 72 65 43 6c 61 75 73 65 20 2a 70 41 6e 64 hereClause *pAnd
c420: 57 43 3b 0a 20 20 20 20 20 20 20 20 57 68 65 72 WC;. Wher
c430: 65 54 65 72 6d 20 2a 70 41 6e 64 54 65 72 6d 3b eTerm *pAndTerm;
c440: 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a . int j;.
c450: 20 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 Bitmask
c460: 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 b = 0;. p
c470: 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e OrTerm->u.pAndIn
c480: 66 6f 20 3d 20 70 41 6e 64 49 6e 66 6f 3b 0a 20 fo = pAndInfo;.
c490: 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e pOrTerm->
c4a0: 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f wtFlags |= TERM_
c4b0: 41 4e 44 49 4e 46 4f 3b 0a 20 20 20 20 20 20 20 ANDINFO;.
c4c0: 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 pOrTerm->eOpera
c4d0: 74 6f 72 20 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20 tor = WO_AND;.
c4e0: 20 20 20 20 20 20 70 41 6e 64 57 43 20 3d 20 26 pAndWC = &
c4f0: 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 pAndInfo->wc;.
c500: 20 20 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 whereClaus
c510: 65 49 6e 69 74 28 70 41 6e 64 57 43 2c 20 70 57 eInit(pAndWC, pW
c520: 43 2d 3e 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 C->pWInfo);.
c530: 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 whereSplit(p
c540: 41 6e 64 57 43 2c 20 70 4f 72 54 65 72 6d 2d 3e AndWC, pOrTerm->
c550: 70 45 78 70 72 2c 20 54 4b 5f 41 4e 44 29 3b 0a pExpr, TK_AND);.
c560: 20 20 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c exprAnal
c570: 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70 41 6e yzeAll(pSrc, pAn
c580: 64 57 43 29 3b 0a 20 20 20 20 20 20 20 20 70 41 dWC);. pA
c590: 6e 64 57 43 2d 3e 70 4f 75 74 65 72 20 3d 20 70 ndWC->pOuter = p
c5a0: 57 43 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 WC;. test
c5b0: 63 61 73 65 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 case( db->malloc
c5c0: 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 Failed );.
c5d0: 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f if( !db->mallo
c5e0: 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 cFailed ){.
c5f0: 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 41 for(j=0, pA
c600: 6e 64 54 65 72 6d 3d 70 41 6e 64 57 43 2d 3e 61 ndTerm=pAndWC->a
c610: 3b 20 6a 3c 70 41 6e 64 57 43 2d 3e 6e 54 65 72 ; j<pAndWC->nTer
c620: 6d 3b 20 6a 2b 2b 2c 20 70 41 6e 64 54 65 72 6d m; j++, pAndTerm
c630: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ++){.
c640: 20 61 73 73 65 72 74 28 20 70 41 6e 64 54 65 72 assert( pAndTer
c650: 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 m->pExpr );.
c660: 20 20 20 20 20 20 20 20 69 66 28 20 61 6c 6c 6f if( allo
c670: 77 65 64 4f 70 28 70 41 6e 64 54 65 72 6d 2d 3e wedOp(pAndTerm->
c680: 70 45 78 70 72 2d 3e 6f 70 29 20 29 7b 0a 20 20 pExpr->op) ){.
c690: 20 20 20 20 20 20 20 20 20 20 20 20 62 20 7c 3d b |=
c6a0: 20 67 65 74 4d 61 73 6b 28 26 70 57 49 6e 66 6f getMask(&pWInfo
c6b0: 2d 3e 73 4d 61 73 6b 53 65 74 2c 20 70 41 6e 64 ->sMaskSet, pAnd
c6c0: 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 Term->leftCursor
c6d0: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d );. }
c6e0: 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
c6f0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 }. i
c700: 6e 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 ndexable &= b;.
c710: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
c720: 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77 74 if( pOrTerm->wt
c730: 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 50 Flags & TERM_COP
c740: 49 45 44 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 IED ){. /*
c750: 53 6b 69 70 20 74 68 69 73 20 74 65 72 6d 20 66 Skip this term f
c760: 6f 72 20 6e 6f 77 2e 20 20 57 65 20 72 65 76 69 or now. We revi
c770: 73 69 74 20 69 74 20 77 68 65 6e 20 77 65 20 70 sit it when we p
c780: 72 6f 63 65 73 73 20 74 68 65 0a 20 20 20 20 20 rocess the.
c790: 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e ** correspondin
c7a0: 67 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 74 g TERM_VIRTUAL t
c7b0: 65 72 6d 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 erm */. }else
c7c0: 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 {. Bitmask
c7d0: 62 3b 0a 20 20 20 20 20 20 62 20 3d 20 67 65 74 b;. b = get
c7e0: 4d 61 73 6b 28 26 70 57 49 6e 66 6f 2d 3e 73 4d Mask(&pWInfo->sM
c7f0: 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d askSet, pOrTerm-
c800: 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a 20 20 >leftCursor);.
c810: 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d if( pOrTerm-
c820: 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f >wtFlags & TERM_
c830: 56 49 52 54 55 41 4c 20 29 7b 0a 20 20 20 20 20 VIRTUAL ){.
c840: 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f WhereTerm *pO
c850: 74 68 65 72 20 3d 20 26 70 4f 72 57 63 2d 3e 61 ther = &pOrWc->a
c860: 5b 70 4f 72 54 65 72 6d 2d 3e 69 50 61 72 65 6e [pOrTerm->iParen
c870: 74 5d 3b 0a 20 20 20 20 20 20 20 20 62 20 7c 3d t];. b |=
c880: 20 67 65 74 4d 61 73 6b 28 26 70 57 49 6e 66 6f getMask(&pWInfo
c890: 2d 3e 73 4d 61 73 6b 53 65 74 2c 20 70 4f 74 68 ->sMaskSet, pOth
c8a0: 65 72 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b er->leftCursor);
c8b0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
c8c0: 6e 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 ndexable &= b;.
c8d0: 20 20 20 20 20 69 66 28 20 28 70 4f 72 54 65 72 if( (pOrTer
c8e0: 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 m->eOperator & W
c8f0: 4f 5f 45 51 29 3d 3d 30 20 29 7b 0a 20 20 20 20 O_EQ)==0 ){.
c900: 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20 30 chngToIN = 0
c910: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
c920: 20 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 chngToIN
c930: 26 3d 20 62 3b 0a 20 20 20 20 20 20 7d 0a 20 20 &= b;. }.
c940: 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 }. }.. /*.
c950: 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 73 65 ** Record the se
c960: 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74 t of tables that
c970: 20 73 61 74 69 73 66 79 20 63 61 73 65 20 32 2e satisfy case 2.
c980: 20 20 54 68 65 20 73 65 74 20 6d 69 67 68 74 20 The set might
c990: 62 65 0a 20 20 2a 2a 20 65 6d 70 74 79 2e 0a 20 be. ** empty..
c9a0: 20 2a 2f 0a 20 20 70 4f 72 49 6e 66 6f 2d 3e 69 */. pOrInfo->i
c9b0: 6e 64 65 78 61 62 6c 65 20 3d 20 69 6e 64 65 78 ndexable = index
c9c0: 61 62 6c 65 3b 0a 20 20 70 54 65 72 6d 2d 3e 65 able;. pTerm->e
c9d0: 4f 70 65 72 61 74 6f 72 20 3d 20 69 6e 64 65 78 Operator = index
c9e0: 61 62 6c 65 3d 3d 30 20 3f 20 30 20 3a 20 57 4f able==0 ? 0 : WO
c9f0: 5f 4f 52 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 _OR;.. /*. **
ca00: 63 68 6e 67 54 6f 49 4e 20 68 6f 6c 64 73 20 61 chngToIN holds a
ca10: 20 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20 74 set of tables t
ca20: 68 61 74 20 2a 6d 69 67 68 74 2a 20 73 61 74 69 hat *might* sati
ca30: 73 66 79 20 63 61 73 65 20 31 2e 20 20 42 75 74 sfy case 1. But
ca40: 0a 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f . ** we have to
ca50: 20 64 6f 20 73 6f 6d 65 20 61 64 64 69 74 69 6f do some additio
ca60: 6e 61 6c 20 63 68 65 63 6b 69 6e 67 20 74 6f 20 nal checking to
ca70: 73 65 65 20 69 66 20 63 61 73 65 20 31 20 72 65 see if case 1 re
ca80: 61 6c 6c 79 0a 20 20 2a 2a 20 69 73 20 73 61 74 ally. ** is sat
ca90: 69 73 66 69 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a isfied.. **. *
caa0: 2a 20 63 68 6e 67 54 6f 49 4e 20 77 69 6c 6c 20 * chngToIN will
cab0: 68 6f 6c 64 20 65 69 74 68 65 72 20 30 2c 20 31 hold either 0, 1
cac0: 2c 20 6f 72 20 32 20 62 69 74 73 2e 20 20 54 68 , or 2 bits. Th
cad0: 65 20 30 2d 62 69 74 20 63 61 73 65 20 6d 65 61 e 0-bit case mea
cae0: 6e 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 ns. ** that the
caf0: 72 65 20 69 73 20 6e 6f 20 70 6f 73 73 69 62 69 re is no possibi
cb00: 6c 69 74 79 20 6f 66 20 74 72 61 6e 73 66 6f 72 lity of transfor
cb10: 6d 69 6e 67 20 74 68 65 20 4f 52 20 63 6c 61 75 ming the OR clau
cb20: 73 65 20 69 6e 74 6f 20 61 6e 0a 20 20 2a 2a 20 se into an. **
cb30: 49 4e 20 6f 70 65 72 61 74 6f 72 20 62 65 63 61 IN operator beca
cb40: 75 73 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 use one or more
cb50: 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 20 terms in the OR
cb60: 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 0a 20 clause contain.
cb70: 20 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 ** something ot
cb80: 68 65 72 20 74 68 61 6e 20 3d 3d 20 6f 6e 20 61 her than == on a
cb90: 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 73 column in the s
cba0: 69 6e 67 6c 65 20 74 61 62 6c 65 2e 20 20 54 68 ingle table. Th
cbb0: 65 20 31 2d 62 69 74 0a 20 20 2a 2a 20 63 61 73 e 1-bit. ** cas
cbc0: 65 20 6d 65 61 6e 73 20 74 68 61 74 20 65 76 65 e means that eve
cbd0: 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f ry term of the O
cbe0: 52 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74 R clause is of t
cbf0: 68 65 20 66 6f 72 6d 0a 20 20 2a 2a 20 22 74 61 he form. ** "ta
cc00: 62 6c 65 2e 63 6f 6c 75 6d 6e 3d 65 78 70 72 22 ble.column=expr"
cc10: 20 66 6f 72 20 73 6f 6d 65 20 73 69 6e 67 6c 65 for some single
cc20: 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6f 6e 65 table. The one
cc30: 20 62 69 74 20 74 68 61 74 20 69 73 20 73 65 74 bit that is set
cc40: 0a 20 20 2a 2a 20 77 69 6c 6c 20 63 6f 72 72 65 . ** will corre
cc50: 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 63 6f 6d spond to the com
cc60: 6d 6f 6e 20 74 61 62 6c 65 2e 20 20 57 65 20 73 mon table. We s
cc70: 74 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 68 65 till need to che
cc80: 63 6b 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 ck to make. **
cc90: 73 75 72 65 20 74 68 65 20 73 61 6d 65 20 63 6f sure the same co
cca0: 6c 75 6d 6e 20 69 73 20 75 73 65 64 20 6f 6e 20 lumn is used on
ccb0: 61 6c 6c 20 74 65 72 6d 73 2e 20 20 54 68 65 20 all terms. The
ccc0: 32 2d 62 69 74 20 63 61 73 65 20 69 73 20 77 68 2-bit case is wh
ccd0: 65 6e 0a 20 20 2a 2a 20 74 68 65 20 61 6c 6c 20 en. ** the all
cce0: 74 65 72 6d 73 20 61 72 65 20 6f 66 20 74 68 65 terms are of the
ccf0: 20 66 6f 72 6d 20 22 74 61 62 6c 65 31 2e 63 6f form "table1.co
cd00: 6c 75 6d 6e 3d 74 61 62 6c 65 32 2e 63 6f 6c 75 lumn=table2.colu
cd10: 6d 6e 22 2e 20 20 49 74 0a 20 20 2a 2a 20 6d 69 mn". It. ** mi
cd20: 67 68 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 ght be possible
cd30: 74 6f 20 66 6f 72 6d 20 61 6e 20 49 4e 20 6f 70 to form an IN op
cd40: 65 72 61 74 6f 72 20 77 69 74 68 20 65 69 74 68 erator with eith
cd50: 65 72 20 74 61 62 6c 65 31 2e 63 6f 6c 75 6d 6e er table1.column
cd60: 0a 20 20 2a 2a 20 6f 72 20 74 61 62 6c 65 32 2e . ** or table2.
cd70: 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 4c 48 column as the LH
cd80: 53 20 69 66 20 65 69 74 68 65 72 20 69 73 20 63 S if either is c
cd90: 6f 6d 6d 6f 6e 20 74 6f 20 65 76 65 72 79 20 74 ommon to every t
cda0: 65 72 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 erm of. ** the
cdb0: 4f 52 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a OR clause.. **.
cdc0: 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 ** Note that t
cdd0: 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 72 6d erms of the form
cde0: 20 22 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e 31 3d "table.column1=
cdf0: 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e 32 22 20 28 table.column2" (
ce00: 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 74 61 the. ** same ta
ce10: 62 6c 65 20 6f 6e 20 62 6f 74 68 20 73 69 7a 65 ble on both size
ce20: 73 20 6f 66 20 74 68 65 20 3d 3d 29 20 63 61 6e s of the ==) can
ce30: 6e 6f 74 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 not be optimized
ce40: 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 68 6e .. */. if( chn
ce50: 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20 69 6e 74 gToIN ){. int
ce60: 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 okToChngToIN =
ce70: 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 0; /* True i
ce80: 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e f the conversion
ce90: 20 74 6f 20 49 4e 20 69 73 20 76 61 6c 69 64 20 to IN is valid
cea0: 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 */. int iColu
ceb0: 6d 6e 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 mn = -1;
cec0: 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 /* Column index
ced0: 20 6f 6e 20 6c 68 73 20 6f 66 20 49 4e 20 6f 70 on lhs of IN op
cee0: 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69 6e erator */. in
cef0: 74 20 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 20 t iCursor = -1;
cf00: 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
cf10: 20 63 75 72 73 6f 72 20 63 6f 6d 6d 6f 6e 20 74 cursor common t
cf20: 6f 20 61 6c 6c 20 74 65 72 6d 73 20 2a 2f 0a 20 o all terms */.
cf30: 20 20 20 69 6e 74 20 6a 20 3d 20 30 3b 20 20 20 int j = 0;
cf40: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
cf50: 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a Loop counter */.
cf60: 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 . /* Search f
cf70: 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20 63 or a table and c
cf80: 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 olumn that appea
cf90: 72 73 20 6f 6e 20 6f 6e 65 20 73 69 64 65 20 6f rs on one side o
cfa0: 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68 r the. ** oth
cfb0: 65 72 20 6f 66 20 74 68 65 20 3d 3d 20 6f 70 65 er of the == ope
cfc0: 72 61 74 6f 72 20 69 6e 20 65 76 65 72 79 20 73 rator in every s
cfd0: 75 62 74 65 72 6d 2e 20 20 54 68 61 74 20 74 61 ubterm. That ta
cfe0: 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 0a 20 ble and column.
cff0: 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 ** will be re
d000: 63 6f 72 64 65 64 20 69 6e 20 69 43 75 72 73 6f corded in iCurso
d010: 72 20 61 6e 64 20 69 43 6f 6c 75 6d 6e 2e 20 20 r and iColumn.
d020: 54 68 65 72 65 20 6d 69 67 68 74 20 6e 6f 74 20 There might not
d030: 62 65 20 61 6e 79 0a 20 20 20 20 2a 2a 20 73 75 be any. ** su
d040: 63 68 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c ch table and col
d050: 75 6d 6e 2e 20 20 53 65 74 20 6f 6b 54 6f 43 68 umn. Set okToCh
d060: 6e 67 54 6f 49 4e 20 69 66 20 61 6e 20 61 70 70 ngToIN if an app
d070: 72 6f 70 72 69 61 74 65 20 74 61 62 6c 65 0a 20 ropriate table.
d080: 20 20 20 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e ** and column
d090: 20 69 73 20 66 6f 75 6e 64 20 62 75 74 20 6c 65 is found but le
d0a0: 61 76 65 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e ave okToChngToIN
d0b0: 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 20 66 6f false if not fo
d0c0: 75 6e 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 und.. */.
d0d0: 66 6f 72 28 6a 3d 30 3b 20 6a 3c 32 20 26 26 20 for(j=0; j<2 &&
d0e0: 21 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 6a !okToChngToIN; j
d0f0: 2b 2b 29 7b 0a 20 20 20 20 20 20 70 4f 72 54 65 ++){. pOrTe
d100: 72 6d 20 3d 20 70 4f 72 57 63 2d 3e 61 3b 0a 20 rm = pOrWc->a;.
d110: 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72 57 63 for(i=pOrWc
d120: 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b ->nTerm-1; i>=0;
d130: 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 i--, pOrTerm++)
d140: 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
d150: 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 ( pOrTerm->eOper
d160: 61 74 6f 72 20 26 20 57 4f 5f 45 51 20 29 3b 0a ator & WO_EQ );.
d170: 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d pOrTerm-
d180: 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54 45 52 >wtFlags &= ~TER
d190: 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 M_OR_OK;.
d1a0: 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 if( pOrTerm->le
d1b0: 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 73 6f ftCursor==iCurso
d1c0: 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f r ){. /
d1d0: 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 32 2d * This is the 2-
d1e0: 62 69 74 20 63 61 73 65 20 61 6e 64 20 77 65 20 bit case and we
d1f0: 61 72 65 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e are on the secon
d200: 64 20 69 74 65 72 61 74 69 6f 6e 20 61 6e 64 0a d iteration and.
d210: 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 75 72 ** cur
d220: 72 65 6e 74 20 74 65 72 6d 20 69 73 20 66 72 6f rent term is fro
d230: 6d 20 74 68 65 20 66 69 72 73 74 20 69 74 65 72 m the first iter
d240: 61 74 69 6f 6e 2e 20 20 53 6f 20 73 6b 69 70 20 ation. So skip
d250: 74 68 69 73 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 this term. */.
d260: 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
d270: 6a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 j==1 );.
d280: 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
d290: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 }. if
d2a0: 28 20 28 63 68 6e 67 54 6f 49 4e 20 26 20 67 65 ( (chngToIN & ge
d2b0: 74 4d 61 73 6b 28 26 70 57 49 6e 66 6f 2d 3e 73 tMask(&pWInfo->s
d2c0: 4d 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d MaskSet, pOrTerm
d2d0: 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 29 3d 3d ->leftCursor))==
d2e0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 0 ){. /
d2f0: 2a 20 54 68 69 73 20 74 65 72 6d 20 6d 75 73 74 * This term must
d300: 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 be of the form
d310: 74 31 2e 61 3d 3d 74 32 2e 62 20 77 68 65 72 65 t1.a==t2.b where
d320: 20 74 32 20 69 73 20 69 6e 20 74 68 65 0a 20 20 t2 is in the.
d330: 20 20 20 20 20 20 20 20 2a 2a 20 63 68 6e 67 54 ** chngT
d340: 6f 49 4e 20 73 65 74 20 62 75 74 20 74 31 20 69 oIN set but t1 i
d350: 73 20 6e 6f 74 2e 20 20 54 68 69 73 20 74 65 72 s not. This ter
d360: 6d 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65 72 m will be either
d370: 20 70 72 65 63 65 65 64 65 64 0a 20 20 20 20 20 preceeded.
d380: 20 20 20 20 20 2a 2a 20 6f 72 20 66 6f 6c 6c 77 ** or follw
d390: 65 64 20 62 79 20 61 6e 20 69 6e 76 65 72 74 65 ed by an inverte
d3a0: 64 20 63 6f 70 79 20 28 74 32 2e 62 3d 3d 74 31 d copy (t2.b==t1
d3b0: 2e 61 29 2e 20 20 53 6b 69 70 20 74 68 69 73 20 .a). Skip this
d3c0: 74 65 72 6d 20 0a 20 20 20 20 20 20 20 20 20 20 term .
d3d0: 2a 2a 20 61 6e 64 20 75 73 65 20 69 74 73 20 69 ** and use its i
d3e0: 6e 76 65 72 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 nversion. */.
d3f0: 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 testcase(
d400: 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 pOrTerm->wtFlag
d410: 73 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 s & TERM_COPIED
d420: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 );. tes
d430: 74 63 61 73 65 28 20 70 4f 72 54 65 72 6d 2d 3e tcase( pOrTerm->
d440: 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 wtFlags & TERM_V
d450: 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 20 20 IRTUAL );.
d460: 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 assert( pOrT
d470: 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 erm->wtFlags & (
d480: 54 45 52 4d 5f 43 4f 50 49 45 44 7c 54 45 52 4d TERM_COPIED|TERM
d490: 5f 56 49 52 54 55 41 4c 29 20 29 3b 0a 20 20 20 _VIRTUAL) );.
d4a0: 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b continue;
d4b0: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
d4c0: 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f 72 iColumn = pOr
d4d0: 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 Term->u.leftColu
d4e0: 6d 6e 3b 0a 20 20 20 20 20 20 20 20 69 43 75 72 mn;. iCur
d4f0: 73 6f 72 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 6c sor = pOrTerm->l
d500: 65 66 74 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 eftCursor;.
d510: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
d520: 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3c 30 20 }. if( i<0
d530: 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f ){. /* No
d540: 20 63 61 6e 64 69 64 61 74 65 20 74 61 62 6c 65 candidate table
d550: 2b 63 6f 6c 75 6d 6e 20 77 61 73 20 66 6f 75 6e +column was foun
d560: 64 2e 20 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c d. This can onl
d570: 79 20 6f 63 63 75 72 0a 20 20 20 20 20 20 20 20 y occur.
d580: 2a 2a 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 ** on the second
d590: 20 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 iteration */.
d5a0: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d assert( j=
d5b0: 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 =1 );. as
d5c0: 73 65 72 74 28 20 49 73 50 6f 77 65 72 4f 66 54 sert( IsPowerOfT
d5d0: 77 6f 28 63 68 6e 67 54 6f 49 4e 29 20 29 3b 0a wo(chngToIN) );.
d5e0: 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 assert(
d5f0: 63 68 6e 67 54 6f 49 4e 3d 3d 67 65 74 4d 61 73 chngToIN==getMas
d600: 6b 28 26 70 57 49 6e 66 6f 2d 3e 73 4d 61 73 6b k(&pWInfo->sMask
d610: 53 65 74 2c 20 69 43 75 72 73 6f 72 29 20 29 3b Set, iCursor) );
d620: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
d630: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 }. te
d640: 73 74 63 61 73 65 28 20 6a 3d 3d 31 20 29 3b 0a stcase( j==1 );.
d650: 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68 61 76 . /* We hav
d660: 65 20 66 6f 75 6e 64 20 61 20 63 61 6e 64 69 64 e found a candid
d670: 61 74 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f ate table and co
d680: 6c 75 6d 6e 2e 20 20 43 68 65 63 6b 20 74 6f 20 lumn. Check to
d690: 73 65 65 20 69 66 20 74 68 61 74 0a 20 20 20 20 see if that.
d6a0: 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 ** table and c
d6b0: 6f 6c 75 6d 6e 20 69 73 20 63 6f 6d 6d 6f 6e 20 olumn is common
d6c0: 74 6f 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e to every term in
d6d0: 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20 2a the OR clause *
d6e0: 2f 0a 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67 /. okToChng
d6f0: 54 6f 49 4e 20 3d 20 31 3b 0a 20 20 20 20 20 20 ToIN = 1;.
d700: 66 6f 72 28 3b 20 69 3e 3d 30 20 26 26 20 6f 6b for(; i>=0 && ok
d710: 54 6f 43 68 6e 67 54 6f 49 4e 3b 20 69 2d 2d 2c ToChngToIN; i--,
d720: 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 pOrTerm++){.
d730: 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 assert( pOr
d740: 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 Term->eOperator
d750: 26 20 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 & WO_EQ );.
d760: 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e if( pOrTerm->
d770: 6c 65 66 74 43 75 72 73 6f 72 21 3d 69 43 75 72 leftCursor!=iCur
d780: 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 sor ){.
d790: 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 pOrTerm->wtFlag
d7a0: 73 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f 4b s &= ~TERM_OR_OK
d7b0: 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
d7c0: 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 75 2e 6c if( pOrTerm->u.l
d7d0: 65 66 74 43 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 eftColumn!=iColu
d7e0: 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 mn ){.
d7f0: 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 30 okToChngToIN = 0
d800: 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
d810: 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 . int a
d820: 66 66 4c 65 66 74 2c 20 61 66 66 52 69 67 68 74 ffLeft, affRight
d830: 3b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 ;. /* I
d840: 66 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 f the right-hand
d850: 20 73 69 64 65 20 69 73 20 61 6c 73 6f 20 61 20 side is also a
d860: 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 column, then the
d870: 20 61 66 66 69 6e 69 74 69 65 73 0a 20 20 20 20 affinities.
d880: 20 20 20 20 20 20 2a 2a 20 6f 66 20 62 6f 74 68 ** of both
d890: 20 72 69 67 68 74 20 61 6e 64 20 6c 65 66 74 20 right and left
d8a0: 73 69 64 65 73 20 6d 75 73 74 20 62 65 20 73 75 sides must be su
d8b0: 63 68 20 74 68 61 74 20 6e 6f 20 74 79 70 65 0a ch that no type.
d8c0: 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e ** con
d8d0: 76 65 72 73 69 6f 6e 73 20 61 72 65 20 72 65 71 versions are req
d8e0: 75 69 72 65 64 20 6f 6e 20 74 68 65 20 72 69 67 uired on the rig
d8f0: 68 74 2e 20 20 28 54 69 63 6b 65 74 20 23 32 32 ht. (Ticket #22
d900: 34 39 29 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 49). */
d910: 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 52 69 . affRi
d920: 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ght = sqlite3Exp
d930: 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 72 rAffinity(pOrTer
d940: 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 m->pExpr->pRight
d950: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66 );. aff
d960: 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 Left = sqlite3Ex
d970: 70 72 41 66 66 69 6e 69 74 79 28 70 4f 72 54 65 prAffinity(pOrTe
d980: 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 rm->pExpr->pLeft
d990: 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 );. if(
d9a0: 20 61 66 66 52 69 67 68 74 21 3d 30 20 26 26 20 affRight!=0 &&
d9b0: 61 66 66 52 69 67 68 74 21 3d 61 66 66 4c 65 66 affRight!=affLef
d9c0: 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 t ){.
d9d0: 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 okToChngToIN =
d9e0: 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 0;. }el
d9f0: 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 se{.
da00: 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 pOrTerm->wtFlags
da10: 20 7c 3d 20 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a |= TERM_OR_OK;.
da20: 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
da30: 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
da40: 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 }.. /* At t
da50: 68 69 73 20 70 6f 69 6e 74 2c 20 6f 6b 54 6f 43 his point, okToC
da60: 68 6e 67 54 6f 49 4e 20 69 73 20 74 72 75 65 20 hngToIN is true
da70: 69 66 20 6f 72 69 67 69 6e 61 6c 20 70 54 65 72 if original pTer
da80: 6d 20 73 61 74 69 73 66 69 65 73 0a 20 20 20 20 m satisfies.
da90: 2a 2a 20 63 61 73 65 20 31 2e 20 20 49 6e 20 74 ** case 1. In t
daa0: 68 61 74 20 63 61 73 65 2c 20 63 6f 6e 73 74 72 hat case, constr
dab0: 75 63 74 20 61 20 6e 65 77 20 76 69 72 74 75 61 uct a new virtua
dac0: 6c 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 0a l term that is .
dad0: 20 20 20 20 2a 2a 20 70 54 65 72 6d 20 63 6f 6e ** pTerm con
dae0: 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 49 verted into an I
daf0: 4e 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 N operator..
db00: 2a 2f 0a 20 20 20 20 69 66 28 20 6f 6b 54 6f 43 */. if( okToC
db10: 68 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20 20 20 hngToIN ){.
db20: 20 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 Expr *pDup;
db30: 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74 72 61 /* A tra
db40: 6e 73 69 65 6e 74 20 64 75 70 6c 69 63 61 74 65 nsient duplicate
db50: 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 expression */.
db60: 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 ExprList *p
db70: 4c 69 73 74 20 3d 20 30 3b 20 20 20 2f 2a 20 54 List = 0; /* T
db80: 68 65 20 52 48 53 20 6f 66 20 74 68 65 20 49 4e he RHS of the IN
db90: 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 operator */.
dba0: 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d Expr *pLeft =
dbb0: 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 0; /* The
dbc0: 20 4c 48 53 20 6f 66 20 74 68 65 20 49 4e 20 6f LHS of the IN o
dbd0: 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 perator */.
dbe0: 20 45 78 70 72 20 2a 70 4e 65 77 3b 20 20 20 20 Expr *pNew;
dbf0: 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 /* The c
dc00: 6f 6d 70 6c 65 74 65 20 49 4e 20 6f 70 65 72 61 omplete IN opera
dc10: 74 6f 72 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f tor */.. fo
dc20: 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d r(i=pOrWc->nTerm
dc30: 2d 31 2c 20 70 4f 72 54 65 72 6d 3d 70 4f 72 57 -1, pOrTerm=pOrW
dc40: 63 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c c->a; i>=0; i--,
dc50: 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 pOrTerm++){.
dc60: 20 20 20 20 20 69 66 28 20 28 70 4f 72 54 65 72 if( (pOrTer
dc70: 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 m->wtFlags & TER
dc80: 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20 29 20 63 6f M_OR_OK)==0 ) co
dc90: 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
dca0: 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d assert( pOrTerm-
dcb0: 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f >eOperator & WO_
dcc0: 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 EQ );. as
dcd0: 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 6c sert( pOrTerm->l
dce0: 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 73 eftCursor==iCurs
dcf0: 6f 72 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 or );. as
dd00: 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 75 sert( pOrTerm->u
dd10: 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f .leftColumn==iCo
dd20: 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 lumn );.
dd30: 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 pDup = sqlite3Ex
dd40: 70 72 44 75 70 28 64 62 2c 20 70 4f 72 54 65 72 prDup(db, pOrTer
dd50: 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 m->pExpr->pRight
dd60: 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 4c , 0);. pL
dd70: 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 ist = sqlite3Exp
dd80: 72 4c 69 73 74 41 70 70 65 6e 64 28 70 57 49 6e rListAppend(pWIn
dd90: 66 6f 2d 3e 70 50 61 72 73 65 2c 20 70 4c 69 73 fo->pParse, pLis
dda0: 74 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 t, pDup);.
ddb0: 20 20 70 4c 65 66 74 20 3d 20 70 4f 72 54 65 72 pLeft = pOrTer
ddc0: 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b m->pExpr->pLeft;
ddd0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 . }. a
dde0: 73 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30 20 ssert( pLeft!=0
ddf0: 29 3b 0a 20 20 20 20 20 20 70 44 75 70 20 3d 20 );. pDup =
de00: 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
de10: 62 2c 20 70 4c 65 66 74 2c 20 30 29 3b 0a 20 20 b, pLeft, 0);.
de20: 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
de30: 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 e3PExpr(pParse,
de40: 54 4b 5f 49 4e 2c 20 70 44 75 70 2c 20 30 2c 20 TK_IN, pDup, 0,
de50: 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 0);. if( pN
de60: 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e ew ){. in
de70: 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 t idxNew;.
de80: 20 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 transferJoinMa
de90: 72 6b 69 6e 67 73 28 70 4e 65 77 2c 20 70 45 78 rkings(pNew, pEx
dea0: 70 72 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 pr);. ass
deb0: 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f ert( !ExprHasPro
dec0: 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 78 perty(pNew, EP_x
ded0: 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 IsSelect) );.
dee0: 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 pNew->x.pLi
def0: 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 st = pList;.
df00: 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 idxNew = whe
df10: 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 reClauseInsert(p
df20: 57 43 2c 20 70 4e 65 77 2c 20 54 45 52 4d 5f 56 WC, pNew, TERM_V
df30: 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 IRTUAL|TERM_DYNA
df40: 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 74 65 MIC);. te
df50: 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d 3d stcase( idxNew==
df60: 30 20 29 3b 0a 20 20 20 20 20 20 20 20 65 78 70 0 );. exp
df70: 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 rAnalyze(pSrc, p
df80: 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 WC, idxNew);.
df90: 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 pTerm = &pW
dfa0: 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 C->a[idxTerm];.
dfb0: 20 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 pWC->a[id
dfc0: 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20 xNew].iParent =
dfd0: 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 idxTerm;.
dfe0: 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d pTerm->nChild =
dff0: 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 1;. }else{
e000: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
e010: 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
e020: 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 b, pList);.
e030: 20 7d 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e }. pTerm->
e040: 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 4e eOperator = WO_N
e050: 4f 4f 50 3b 20 20 2f 2a 20 63 61 73 65 20 31 20 OOP; /* case 1
e060: 74 72 75 6d 70 73 20 63 61 73 65 20 32 20 2a 2f trumps case 2 */
e070: 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e . }. }.}.#en
e080: 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f dif /* !SQLITE_O
e090: 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 MIT_OR_OPTIMIZAT
e0a0: 49 4f 4e 20 26 26 20 21 53 51 4c 49 54 45 5f 4f ION && !SQLITE_O
e0b0: 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a MIT_SUBQUERY */.
e0c0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 ./*.** The input
e0d0: 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 to this routine
e0e0: 20 69 73 20 61 6e 20 57 68 65 72 65 54 65 72 6d is an WhereTerm
e0f0: 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 20 structure with
e100: 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 22 70 45 78 only the.** "pEx
e110: 70 72 22 20 66 69 65 6c 64 20 66 69 6c 6c 65 64 pr" field filled
e120: 20 69 6e 2e 20 20 54 68 65 20 6a 6f 62 20 6f 66 in. The job of
e130: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
e140: 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a to analyze the.
e150: 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e ** subexpression
e160: 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 6c and populate al
e170: 6c 20 74 68 65 20 6f 74 68 65 72 20 66 69 65 6c l the other fiel
e180: 64 73 20 6f 66 20 74 68 65 20 57 68 65 72 65 54 ds of the WhereT
e190: 65 72 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 erm.** structure
e1a0: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 ..**.** If the e
e1b0: 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 xpression is of
e1c0: 74 68 65 20 66 6f 72 6d 20 22 3c 65 78 70 72 3e the form "<expr>
e1d0: 20 3c 6f 70 3e 20 58 22 20 69 74 20 67 65 74 73 <op> X" it gets
e1e0: 20 63 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74 6f 20 commuted.** to
e1f0: 74 68 65 20 73 74 61 6e 64 61 72 64 20 66 6f 72 the standard for
e200: 6d 20 6f 66 20 22 58 20 3c 6f 70 3e 20 3c 65 78 m of "X <op> <ex
e210: 70 72 3e 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 pr>"..**.** If t
e220: 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 he expression is
e230: 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20 of the form "X
e240: 3c 6f 70 3e 20 59 22 20 77 68 65 72 65 20 62 6f <op> Y" where bo
e250: 74 68 20 58 20 61 6e 64 20 59 20 61 72 65 0a 2a th X and Y are.*
e260: 2a 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 * columns, then
e270: 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70 the original exp
e280: 72 65 73 73 69 6f 6e 20 69 73 20 75 6e 63 68 61 ression is uncha
e290: 6e 67 65 64 20 61 6e 64 20 61 20 6e 65 77 20 76 nged and a new v
e2a0: 69 72 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 6f irtual.** term o
e2b0: 66 20 74 68 65 20 66 6f 72 6d 20 22 59 20 3c 6f f the form "Y <o
e2c0: 70 3e 20 58 22 20 69 73 20 61 64 64 65 64 20 74 p> X" is added t
e2d0: 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 o the WHERE clau
e2e0: 73 65 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 7a se and.** analyz
e2f0: 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20 ed separately.
e300: 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 The original ter
e310: 6d 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 m is marked with
e320: 20 54 45 52 4d 5f 43 4f 50 49 45 44 0a 2a 2a 20 TERM_COPIED.**
e330: 61 6e 64 20 74 68 65 20 6e 65 77 20 74 65 72 6d and the new term
e340: 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 is marked with
e350: 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 28 62 65 TERM_DYNAMIC (be
e360: 63 61 75 73 65 20 69 74 27 73 20 70 45 78 70 72 cause it's pExpr
e370: 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 .** needs to be
e380: 66 72 65 65 64 20 77 69 74 68 20 74 68 65 20 57 freed with the W
e390: 68 65 72 65 43 6c 61 75 73 65 29 20 61 6e 64 20 hereClause) and
e3a0: 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 28 62 65 TERM_VIRTUAL (be
e3b0: 63 61 75 73 65 20 69 74 0a 2a 2a 20 69 73 20 61 cause it.** is a
e3c0: 20 63 6f 6d 6d 75 74 65 64 20 63 6f 70 79 20 6f commuted copy o
e3d0: 66 20 61 20 70 72 69 6f 72 20 74 65 72 6d 2e 29 f a prior term.)
e3e0: 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 The original t
e3f0: 65 72 6d 20 68 61 73 20 6e 43 68 69 6c 64 3d 31 erm has nChild=1
e400: 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 70 79 .** and the copy
e410: 20 68 61 73 20 69 64 78 50 61 72 65 6e 74 20 73 has idxParent s
e420: 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 et to the index
e430: 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 of the original
e440: 74 65 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 term..*/.static
e450: 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 void exprAnalyze
e460: 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 (. SrcList *pSr
e470: 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a c, /*
e480: 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 the FROM clause
e490: 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 */. WhereClaus
e4a0: 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 e *pWC,
e4b0: 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 /* the WHERE cla
e4c0: 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 use */. int idx
e4d0: 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 Term
e4e0: 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 /* Index of t
e4f0: 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e he term to be an
e500: 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57 alyzed */.){. W
e510: 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f hereInfo *pWInfo
e520: 20 3d 20 70 57 43 2d 3e 70 57 49 6e 66 6f 3b 20 = pWC->pWInfo;
e530: 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 /* WHERE clause
e540: 70 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 65 processing conte
e550: 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 xt */. WhereTer
e560: 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 m *pTerm;
e570: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
e580: 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 term to be analy
e590: 7a 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 zed */. WhereMa
e5a0: 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 3b skSet *pMaskSet;
e5b0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 /* Set
e5c0: 20 6f 66 20 74 61 62 6c 65 20 69 6e 64 65 78 20 of table index
e5d0: 6d 61 73 6b 73 20 2a 2f 0a 20 20 45 78 70 72 20 masks */. Expr
e5e0: 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 *pExpr;
e5f0: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
e600: 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f he expression to
e610: 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a be analyzed */.
e620: 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 Bitmask prereq
e630: 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 Left;
e640: 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65 73 69 /* Prerequesi
e650: 74 65 73 20 6f 66 20 74 68 65 20 70 45 78 70 72 tes of the pExpr
e660: 2d 3e 70 4c 65 66 74 20 2a 2f 0a 20 20 42 69 74 ->pLeft */. Bit
e670: 6d 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 mask prereqAll;
e680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
e690: 20 50 72 65 72 65 71 75 65 73 69 74 65 73 20 6f Prerequesites o
e6a0: 66 20 70 45 78 70 72 20 2a 2f 0a 20 20 42 69 74 f pExpr */. Bit
e6b0: 6d 61 73 6b 20 65 78 74 72 61 52 69 67 68 74 20 mask extraRight
e6c0: 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a = 0; /*
e6d0: 20 45 78 74 72 61 20 64 65 70 65 6e 64 65 6e 63 Extra dependenc
e6e0: 69 65 73 20 6f 6e 20 4c 45 46 54 20 4a 4f 49 4e ies on LEFT JOIN
e6f0: 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53 74 72 */. Expr *pStr
e700: 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 1 = 0;
e710: 20 20 20 20 20 20 20 2f 2a 20 52 48 53 20 6f 66 /* RHS of
e720: 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65 72 61 LIKE/GLOB opera
e730: 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 tor */. int isC
e740: 6f 6d 70 6c 65 74 65 20 3d 20 30 3b 20 20 20 20 omplete = 0;
e750: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 48 53 /* RHS
e760: 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 65 6e of LIKE/GLOB en
e770: 64 73 20 77 69 74 68 20 77 69 6c 64 63 61 72 64 ds with wildcard
e780: 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 61 73 65 */. int noCase
e790: 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
e7a0: 20 20 20 20 20 20 20 2f 2a 20 4c 49 4b 45 2f 47 /* LIKE/G
e7b0: 4c 4f 42 20 64 69 73 74 69 6e 67 75 69 73 68 65 LOB distinguishe
e7c0: 73 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 s case */. int
e7d0: 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 op;
e7e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
e7f0: 54 6f 70 2d 6c 65 76 65 6c 20 6f 70 65 72 61 74 Top-level operat
e800: 6f 72 2e 20 20 70 45 78 70 72 2d 3e 6f 70 20 2a or. pExpr->op *
e810: 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 /. Parse *pPars
e820: 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 e = pWInfo->pPar
e830: 73 65 3b 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 se; /* Parsing
e840: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c context */. sql
e850: 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
e860: 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 2f 2a e->db; /*
e870: 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 Database connec
e880: 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 64 tion */.. if( d
e890: 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
e8a0: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 ){. return;.
e8b0: 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57 }. pTerm = &pW
e8c0: 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 C->a[idxTerm];.
e8d0: 20 70 4d 61 73 6b 53 65 74 20 3d 20 26 70 57 49 pMaskSet = &pWI
e8e0: 6e 66 6f 2d 3e 73 4d 61 73 6b 53 65 74 3b 0a 20 nfo->sMaskSet;.
e8f0: 20 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e pExpr = pTerm->
e900: 70 45 78 70 72 3b 0a 20 20 61 73 73 65 72 74 28 pExpr;. assert(
e910: 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 pExpr->op!=TK_A
e920: 53 20 26 26 20 70 45 78 70 72 2d 3e 6f 70 21 3d S && pExpr->op!=
e930: 54 4b 5f 43 4f 4c 4c 41 54 45 20 29 3b 0a 20 20 TK_COLLATE );.
e940: 70 72 65 72 65 71 4c 65 66 74 20 3d 20 65 78 70 prereqLeft = exp
e950: 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 rTableUsage(pMas
e960: 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 70 4c 65 kSet, pExpr->pLe
e970: 66 74 29 3b 0a 20 20 6f 70 20 3d 20 70 45 78 70 ft);. op = pExp
e980: 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d r->op;. if( op=
e990: 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20 61 73 =TK_IN ){. as
e9a0: 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 52 69 sert( pExpr->pRi
e9b0: 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 ght==0 );. if
e9c0: 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 ( ExprHasPropert
e9d0: 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 y(pExpr, EP_xIsS
e9e0: 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 elect) ){.
e9f0: 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 pTerm->prereqRig
ea00: 68 74 20 3d 20 65 78 70 72 53 65 6c 65 63 74 54 ht = exprSelectT
ea10: 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 ableUsage(pMaskS
ea20: 65 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 et, pExpr->x.pSe
ea30: 6c 65 63 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 lect);. }else
ea40: 7b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 {. pTerm->p
ea50: 72 65 72 65 71 52 69 67 68 74 20 3d 20 65 78 70 rereqRight = exp
ea60: 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 rListTableUsage(
ea70: 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d pMaskSet, pExpr-
ea80: 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 7d >x.pList);. }
ea90: 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d . }else if( op=
eaa0: 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 =TK_ISNULL ){.
eab0: 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 pTerm->prereqR
eac0: 69 67 68 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 ight = 0;. }els
ead0: 65 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 e{. pTerm->pr
eae0: 65 72 65 71 52 69 67 68 74 20 3d 20 65 78 70 72 ereqRight = expr
eaf0: 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b TableUsage(pMask
eb00: 53 65 74 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 Set, pExpr->pRig
eb10: 68 74 29 3b 0a 20 20 7d 0a 20 20 70 72 65 72 65 ht);. }. prere
eb20: 71 41 6c 6c 20 3d 20 65 78 70 72 54 61 62 6c 65 qAll = exprTable
eb30: 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 Usage(pMaskSet,
eb40: 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 45 78 pExpr);. if( Ex
eb50: 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 prHasProperty(pE
eb60: 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e xpr, EP_FromJoin
eb70: 29 20 29 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b ) ){. Bitmask
eb80: 20 78 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 x = getMask(pMa
eb90: 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 52 skSet, pExpr->iR
eba0: 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 29 3b 0a ightJoinTable);.
ebb0: 20 20 20 20 70 72 65 72 65 71 41 6c 6c 20 7c 3d prereqAll |=
ebc0: 20 78 3b 0a 20 20 20 20 65 78 74 72 61 52 69 67 x;. extraRig
ebd0: 68 74 20 3d 20 78 2d 31 3b 20 20 2f 2a 20 4f 4e ht = x-1; /* ON
ebe0: 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 6d 61 clause terms ma
ebf0: 79 20 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69 y not be used wi
ec00: 74 68 20 61 6e 20 69 6e 64 65 78 0a 20 20 20 20 th an index.
ec10: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ec20: 20 20 20 2a 2a 20 6f 6e 20 6c 65 66 74 20 74 61 ** on left ta
ec30: 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f ble of a LEFT JO
ec40: 49 4e 2e 20 20 54 69 63 6b 65 74 20 23 33 30 31 IN. Ticket #301
ec50: 35 20 2a 2f 0a 20 20 7d 0a 20 20 70 54 65 72 6d 5 */. }. pTerm
ec60: 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 ->prereqAll = pr
ec70: 65 72 65 71 41 6c 6c 3b 0a 20 20 70 54 65 72 6d ereqAll;. pTerm
ec80: 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 2d ->leftCursor = -
ec90: 31 3b 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 1;. pTerm->iPar
eca0: 65 6e 74 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 ent = -1;. pTer
ecb0: 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 m->eOperator = 0
ecc0: 3b 0a 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f ;. if( allowedO
ecd0: 70 28 6f 70 29 20 29 7b 0a 20 20 20 20 45 78 70 p(op) ){. Exp
ece0: 72 20 2a 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 r *pLeft = sqlit
ecf0: 65 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 74 e3ExprSkipCollat
ed00: 65 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b e(pExpr->pLeft);
ed10: 0a 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 . Expr *pRigh
ed20: 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 53 t = sqlite3ExprS
ed30: 6b 69 70 43 6f 6c 6c 61 74 65 28 70 45 78 70 72 kipCollate(pExpr
ed40: 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20 75 ->pRight);. u
ed50: 31 36 20 6f 70 4d 61 73 6b 20 3d 20 28 70 54 65 16 opMask = (pTe
ed60: 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 rm->prereqRight
ed70: 26 20 70 72 65 72 65 71 4c 65 66 74 29 3d 3d 30 & prereqLeft)==0
ed80: 20 3f 20 57 4f 5f 41 4c 4c 20 3a 20 57 4f 5f 45 ? WO_ALL : WO_E
ed90: 51 55 49 56 3b 0a 20 20 20 20 69 66 28 20 70 4c QUIV;. if( pL
eda0: 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 eft->op==TK_COLU
edb0: 4d 4e 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72 MN ){. pTer
edc0: 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 m->leftCursor =
edd0: 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 pLeft->iTable;.
ede0: 20 20 20 20 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 pTerm->u.le
edf0: 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 ftColumn = pLeft
ee00: 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 ->iColumn;.
ee10: 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f pTerm->eOperato
ee20: 72 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b r = operatorMask
ee30: 28 6f 70 29 20 26 20 6f 70 4d 61 73 6b 3b 0a 20 (op) & opMask;.
ee40: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52 69 }. if( pRi
ee50: 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 6f ght && pRight->o
ee60: 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a p==TK_COLUMN ){.
ee70: 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 WhereTerm
ee80: 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 45 78 70 *pNew;. Exp
ee90: 72 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 75 r *pDup;. u
eea0: 31 36 20 65 45 78 74 72 61 4f 70 20 3d 20 30 3b 16 eExtraOp = 0;
eeb0: 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 /* Extra
eec0: 20 62 69 74 73 20 66 6f 72 20 70 4e 65 77 2d 3e bits for pNew->
eed0: 65 4f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 eOperator */.
eee0: 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 if( pTerm->le
eef0: 66 74 43 75 72 73 6f 72 3e 3d 30 20 29 7b 0a 20 ftCursor>=0 ){.
ef00: 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 int idxNe
ef10: 77 3b 0a 20 20 20 20 20 20 20 20 70 44 75 70 20 w;. pDup
ef20: 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 = sqlite3ExprDup
ef30: 28 64 62 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a (db, pExpr, 0);.
ef40: 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e if( db->
ef50: 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
ef60: 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
ef70: 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 3ExprDelete(db,
ef80: 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 pDup);.
ef90: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 return;.
efa0: 20 7d 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65 }. idxNe
efb0: 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 w = whereClauseI
efc0: 6e 73 65 72 74 28 70 57 43 2c 20 70 44 75 70 2c nsert(pWC, pDup,
efd0: 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 TERM_VIRTUAL|TE
efe0: 52 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 RM_DYNAMIC);.
eff0: 20 20 20 20 20 69 66 28 20 69 64 78 4e 65 77 3d if( idxNew=
f000: 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 =0 ) return;.
f010: 20 20 20 20 20 70 4e 65 77 20 3d 20 26 70 57 43 pNew = &pWC
f020: 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 ->a[idxNew];.
f030: 20 20 20 20 20 70 4e 65 77 2d 3e 69 50 61 72 65 pNew->iPare
f040: 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 nt = idxTerm;.
f050: 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 pTerm = &p
f060: 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a WC->a[idxTerm];.
f070: 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e pTerm->n
f080: 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 Child = 1;.
f090: 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 pTerm->wtFlag
f0a0: 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 s |= TERM_COPIED
f0b0: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 ;. if( pE
f0c0: 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 0a 20 xpr->op==TK_EQ.
f0d0: 20 20 20 20 20 20 20 20 26 26 20 21 45 78 70 72 && !Expr
f0e0: 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 HasProperty(pExp
f0f0: 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 0a r, EP_FromJoin).
f100: 20 20 20 20 20 20 20 20 20 26 26 20 4f 70 74 69 && Opti
f110: 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c 65 64 28 mizationEnabled(
f120: 64 62 2c 20 53 51 4c 49 54 45 5f 54 72 61 6e 73 db, SQLITE_Trans
f130: 69 74 69 76 65 29 0a 20 20 20 20 20 20 20 20 29 itive). )
f140: 7b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72 {. pTer
f150: 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 7c 3d 20 m->eOperator |=
f160: 57 4f 5f 45 51 55 49 56 3b 0a 20 20 20 20 20 20 WO_EQUIV;.
f170: 20 20 20 20 65 45 78 74 72 61 4f 70 20 3d 20 57 eExtraOp = W
f180: 4f 5f 45 51 55 49 56 3b 0a 20 20 20 20 20 20 20 O_EQUIV;.
f190: 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }. }else{.
f1a0: 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 70 pDup = p
f1b0: 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 70 4e Expr;. pN
f1c0: 65 77 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 ew = pTerm;.
f1d0: 20 20 7d 0a 20 20 20 20 20 20 65 78 70 72 43 6f }. exprCo
f1e0: 6d 6d 75 74 65 28 70 50 61 72 73 65 2c 20 70 44 mmute(pParse, pD
f1f0: 75 70 29 3b 0a 20 20 20 20 20 20 70 4c 65 66 74 up);. pLeft
f200: 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 53 6b = sqlite3ExprSk
f210: 69 70 43 6f 6c 6c 61 74 65 28 70 44 75 70 2d 3e ipCollate(pDup->
f220: 70 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 70 4e pLeft);. pN
f230: 65 77 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d ew->leftCursor =
f240: 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a pLeft->iTable;.
f250: 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 6c 65 pNew->u.le
f260: 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 ftColumn = pLeft
f270: 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 ->iColumn;.
f280: 20 74 65 73 74 63 61 73 65 28 20 28 70 72 65 72 testcase( (prer
f290: 65 71 4c 65 66 74 20 7c 20 65 78 74 72 61 52 69 eqLeft | extraRi
f2a0: 67 68 74 29 20 21 3d 20 70 72 65 72 65 71 4c 65 ght) != prereqLe
f2b0: 66 74 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 ft );. pNew
f2c0: 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 ->prereqRight =
f2d0: 70 72 65 72 65 71 4c 65 66 74 20 7c 20 65 78 74 prereqLeft | ext
f2e0: 72 61 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 raRight;. p
f2f0: 4e 65 77 2d 3e 70 72 65 72 65 71 41 6c 6c 20 3d New->prereqAll =
f300: 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 prereqAll;.
f310: 20 20 70 4e 65 77 2d 3e 65 4f 70 65 72 61 74 6f pNew->eOperato
f320: 72 20 3d 20 28 6f 70 65 72 61 74 6f 72 4d 61 73 r = (operatorMas
f330: 6b 28 70 44 75 70 2d 3e 6f 70 29 20 2b 20 65 45 k(pDup->op) + eE
f340: 78 74 72 61 4f 70 29 20 26 20 6f 70 4d 61 73 6b xtraOp) & opMask
f350: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 ;. }. }..#if
f360: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
f370: 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a _BETWEEN_OPTIMIZ
f380: 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 66 20 61 20 ATION. /* If a
f390: 74 65 72 6d 20 69 73 20 74 68 65 20 42 45 54 57 term is the BETW
f3a0: 45 45 4e 20 6f 70 65 72 61 74 6f 72 2c 20 63 72 EEN operator, cr
f3b0: 65 61 74 65 20 74 77 6f 20 6e 65 77 20 76 69 72 eate two new vir
f3c0: 74 75 61 6c 20 74 65 72 6d 73 0a 20 20 2a 2a 20 tual terms. **
f3d0: 74 68 61 74 20 64 65 66 69 6e 65 20 74 68 65 20 that define the
f3e0: 72 61 6e 67 65 20 74 68 61 74 20 74 68 65 20 42 range that the B
f3f0: 45 54 57 45 45 4e 20 69 6d 70 6c 65 6d 65 6e 74 ETWEEN implement
f400: 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a s. For example:
f410: 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
f420: 61 20 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20 a BETWEEN b AND
f430: 63 0a 20 20 2a 2a 0a 20 20 2a 2a 20 69 73 20 63 c. **. ** is c
f440: 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 3a 0a 20 onverted into:.
f450: 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 28 61 **. ** (a
f460: 20 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63 BETWEEN b AND c
f470: 29 20 41 4e 44 20 28 61 3e 3d 62 29 20 41 4e 44 ) AND (a>=b) AND
f480: 20 28 61 3c 3d 63 29 0a 20 20 2a 2a 0a 20 20 2a (a<=c). **. *
f490: 2a 20 54 68 65 20 74 77 6f 20 6e 65 77 20 74 65 * The two new te
f4a0: 72 6d 73 20 61 72 65 20 61 64 64 65 64 20 6f 6e rms are added on
f4b0: 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 to the end of th
f4c0: 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62 e WhereClause ob
f4d0: 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e ject.. ** The n
f4e0: 65 77 20 74 65 72 6d 73 20 61 72 65 20 22 64 79 ew terms are "dy
f4f0: 6e 61 6d 69 63 22 20 61 6e 64 20 61 72 65 20 63 namic" and are c
f500: 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 65 20 6f hildren of the o
f510: 72 69 67 69 6e 61 6c 20 42 45 54 57 45 45 4e 0a riginal BETWEEN.
f520: 20 20 2a 2a 20 74 65 72 6d 2e 20 20 54 68 61 74 ** term. That
f530: 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 74 means that if t
f540: 68 65 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20 he BETWEEN term
f550: 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20 63 68 is coded, the ch
f560: 69 6c 64 72 65 6e 20 61 72 65 0a 20 20 2a 2a 20 ildren are. **
f570: 73 6b 69 70 70 65 64 2e 20 20 4f 72 2c 20 69 66 skipped. Or, if
f580: 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72 the children ar
f590: 65 20 73 61 74 69 73 66 69 65 64 20 62 79 20 61 e satisfied by a
f5a0: 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 69 n index, the ori
f5b0: 67 69 6e 61 6c 0a 20 20 2a 2a 20 42 45 54 57 45 ginal. ** BETWE
f5c0: 45 4e 20 74 65 72 6d 20 69 73 20 73 6b 69 70 70 EN term is skipp
f5d0: 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 ed.. */. else
f5e0: 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 if( pExpr->op==T
f5f0: 4b 5f 42 45 54 57 45 45 4e 20 26 26 20 70 57 43 K_BETWEEN && pWC
f600: 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a ->op==TK_AND ){.
f610: 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c ExprList *pL
f620: 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 ist = pExpr->x.p
f630: 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b List;. int i;
f640: 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 . static cons
f650: 74 20 75 38 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b t u8 ops[] = {TK
f660: 5f 47 45 2c 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 _GE, TK_LE};.
f670: 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d assert( pList!=
f680: 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 0 );. assert(
f690: 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 pList->nExpr==2
f6a0: 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b );. for(i=0;
f6b0: 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 i<2; i++){.
f6c0: 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 Expr *pNewExpr
f6d0: 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e ;. int idxN
f6e0: 65 77 3b 0a 20 20 20 20 20 20 70 4e 65 77 45 78 ew;. pNewEx
f6f0: 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 pr = sqlite3PExp
f700: 72 28 70 50 61 72 73 65 2c 20 6f 70 73 5b 69 5d r(pParse, ops[i]
f710: 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
f720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f730: 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 sqlite3ExprDup(d
f740: 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c b, pExpr->pLeft,
f750: 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 0),.
f760: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
f770: 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 sqlite3ExprDup
f780: 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d (db, pList->a[i]
f790: 2e 70 45 78 70 72 2c 20 30 29 2c 20 30 29 3b 0a .pExpr, 0), 0);.
f7a0: 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 4a 6f transferJo
f7b0: 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77 45 inMarkings(pNewE
f7c0: 78 70 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 xpr, pExpr);.
f7d0: 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 idxNew = wher
f7e0: 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 eClauseInsert(pW
f7f0: 43 2c 20 70 4e 65 77 45 78 70 72 2c 20 54 45 52 C, pNewExpr, TER
f800: 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 M_VIRTUAL|TERM_D
f810: 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 74 YNAMIC);. t
f820: 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d estcase( idxNew=
f830: 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 =0 );. expr
f840: 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 Analyze(pSrc, pW
f850: 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20 20 C, idxNew);.
f860: 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e pTerm = &pWC->
f870: 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 a[idxTerm];.
f880: 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d pWC->a[idxNew]
f890: 2e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 .iParent = idxTe
f8a0: 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 rm;. }. pT
f8b0: 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 32 3b erm->nChild = 2;
f8c0: 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 . }.#endif /* S
f8d0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45 54 57 45 QLITE_OMIT_BETWE
f8e0: 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 EN_OPTIMIZATION
f8f0: 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 */..#if !defined
f900: 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f (SQLITE_OMIT_OR_
f910: 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 OPTIMIZATION) &&
f920: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
f930: 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a _OMIT_SUBQUERY).
f940: 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 20 74 /* Analyze a t
f950: 65 72 6d 20 74 68 61 74 20 69 73 20 63 6f 6d 70 erm that is comp
f960: 6f 73 65 64 20 6f 66 20 74 77 6f 20 6f 72 20 6d osed of two or m
f970: 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f 6e ore subterms con
f980: 6e 65 63 74 65 64 20 62 79 0a 20 20 2a 2a 20 61 nected by. ** a
f990: 6e 20 4f 52 20 6f 70 65 72 61 74 6f 72 2e 0a 20 n OR operator..
f9a0: 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28 20 70 */. else if( p
f9b0: 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 Expr->op==TK_OR
f9c0: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
f9d0: 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 WC->op==TK_AND )
f9e0: 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a ;. exprAnalyz
f9f0: 65 4f 72 54 65 72 6d 28 70 53 72 63 2c 20 70 57 eOrTerm(pSrc, pW
fa00: 43 2c 20 69 64 78 54 65 72 6d 29 3b 0a 20 20 20 C, idxTerm);.
fa10: 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 pTerm = &pWC->a
fa20: 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 7d 0a 23 [idxTerm];. }.#
fa30: 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
fa40: 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 OMIT_OR_OPTIMIZA
fa50: 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 TION */..#ifndef
fa60: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b SQLITE_OMIT_LIK
fa70: 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 E_OPTIMIZATION.
fa80: 20 2f 2a 20 41 64 64 20 63 6f 6e 73 74 72 61 69 /* Add constrai
fa90: 6e 74 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 nts to reduce th
faa0: 65 20 73 65 61 72 63 68 20 73 70 61 63 65 20 6f e search space o
fab0: 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 n a LIKE or GLOB
fac0: 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a . ** operator..
fad0: 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 6c 69 6b 65 **. ** A like
fae0: 20 70 61 74 74 65 72 6e 20 6f 66 20 74 68 65 20 pattern of the
faf0: 66 6f 72 6d 20 22 78 20 4c 49 4b 45 20 27 61 62 form "x LIKE 'ab
fb00: 63 25 27 22 20 69 73 20 63 68 61 6e 67 65 64 20 c%'" is changed
fb10: 69 6e 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 73 into constraints
fb20: 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 . **. **
fb30: 20 20 20 20 78 3e 3d 27 61 62 63 27 20 41 4e 44 x>='abc' AND
fb40: 20 78 3c 27 61 62 64 27 20 41 4e 44 20 78 20 4c x<'abd' AND x L
fb50: 49 4b 45 20 27 61 62 63 25 27 0a 20 20 2a 2a 0a IKE 'abc%'. **.
fb60: 20 20 2a 2a 20 54 68 65 20 6c 61 73 74 20 63 68 ** The last ch
fb70: 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 70 aracter of the p
fb80: 72 65 66 69 78 20 22 61 62 63 22 20 69 73 20 69 refix "abc" is i
fb90: 6e 63 72 65 6d 65 6e 74 65 64 20 74 6f 20 66 6f ncremented to fo
fba0: 72 6d 20 74 68 65 0a 20 20 2a 2a 20 74 65 72 6d rm the. ** term
fbb0: 69 6e 61 74 69 6f 6e 20 63 6f 6e 64 69 74 69 6f ination conditio
fbc0: 6e 20 22 61 62 64 22 2e 0a 20 20 2a 2f 0a 20 20 n "abd".. */.
fbd0: 69 66 28 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f if( pWC->op==TK_
fbe0: 41 4e 44 20 0a 20 20 20 26 26 20 69 73 4c 69 6b AND . && isLik
fbf0: 65 4f 72 47 6c 6f 62 28 70 50 61 72 73 65 2c 20 eOrGlob(pParse,
fc00: 70 45 78 70 72 2c 20 26 70 53 74 72 31 2c 20 26 pExpr, &pStr1, &
fc10: 69 73 43 6f 6d 70 6c 65 74 65 2c 20 26 6e 6f 43 isComplete, &noC
fc20: 61 73 65 29 0a 20 20 29 7b 0a 20 20 20 20 45 78 ase). ){. Ex
fc30: 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 pr *pLeft;
fc40: 20 2f 2a 20 4c 48 53 20 6f 66 20 4c 49 4b 45 2f /* LHS of LIKE/
fc50: 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 2a 2f GLOB operator */
fc60: 0a 20 20 20 20 45 78 70 72 20 2a 70 53 74 72 32 . Expr *pStr2
fc70: 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 ; /* Copy
fc80: 6f 66 20 70 53 74 72 31 20 2d 20 52 48 53 20 6f of pStr1 - RHS o
fc90: 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65 72 f LIKE/GLOB oper
fca0: 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 ator */. Expr
fcb0: 20 2a 70 4e 65 77 45 78 70 72 31 3b 0a 20 20 20 *pNewExpr1;.
fcc0: 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 32 Expr *pNewExpr2
fcd0: 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 ;. int idxNew
fce0: 31 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65 1;. int idxNe
fcf0: 77 32 3b 0a 20 20 20 20 54 6f 6b 65 6e 20 73 43 w2;. Token sC
fd00: 6f 6c 6c 53 65 71 4e 61 6d 65 3b 20 20 2f 2a 20 ollSeqName; /*
fd10: 4e 61 6d 65 20 6f 66 20 63 6f 6c 6c 61 74 69 6e Name of collatin
fd20: 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 20 g sequence */..
fd30: 20 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 pLeft = pExpr
fd40: 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e ->x.pList->a[1].
fd50: 70 45 78 70 72 3b 0a 20 20 20 20 70 53 74 72 32 pExpr;. pStr2
fd60: 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 = sqlite3ExprDu
fd70: 70 28 64 62 2c 20 70 53 74 72 31 2c 20 30 29 3b p(db, pStr1, 0);
fd80: 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 . if( !db->ma
fd90: 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 llocFailed ){.
fda0: 20 20 20 20 75 38 20 63 2c 20 2a 70 43 3b 20 20 u8 c, *pC;
fdb0: 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 63 68 61 /* Last cha
fdc0: 72 61 63 74 65 72 20 62 65 66 6f 72 65 20 74 68 racter before th
fdd0: 65 20 66 69 72 73 74 20 77 69 6c 64 63 61 72 64 e first wildcard
fde0: 20 2a 2f 0a 20 20 20 20 20 20 70 43 20 3d 20 28 */. pC = (
fdf0: 75 38 2a 29 26 70 53 74 72 32 2d 3e 75 2e 7a 54 u8*)&pStr2->u.zT
fe00: 6f 6b 65 6e 5b 73 71 6c 69 74 65 33 53 74 72 6c oken[sqlite3Strl
fe10: 65 6e 33 30 28 70 53 74 72 32 2d 3e 75 2e 7a 54 en30(pStr2->u.zT
fe20: 6f 6b 65 6e 29 2d 31 5d 3b 0a 20 20 20 20 20 20 oken)-1];.
fe30: 63 20 3d 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 c = *pC;. i
fe40: 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 f( noCase ){.
fe50: 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e /* The poin
fe60: 74 20 69 73 20 74 6f 20 69 6e 63 72 65 6d 65 6e t is to incremen
fe70: 74 20 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 t the last chara
fe80: 63 74 65 72 20 62 65 66 6f 72 65 20 74 68 65 20 cter before the
fe90: 66 69 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a first. **
fea0: 20 77 69 6c 64 63 61 72 64 2e 20 20 42 75 74 20 wildcard. But
feb0: 69 66 20 77 65 20 69 6e 63 72 65 6d 65 6e 74 20 if we increment
fec0: 27 40 27 2c 20 74 68 61 74 20 77 69 6c 6c 20 70 '@', that will p
fed0: 75 73 68 20 69 74 20 69 6e 74 6f 20 74 68 65 0a ush it into the.
fee0: 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 70 68 61 ** alpha
fef0: 62 65 74 69 63 20 72 61 6e 67 65 20 77 68 65 72 betic range wher
ff00: 65 20 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f e case conversio
ff10: 6e 73 20 77 69 6c 6c 20 6d 65 73 73 20 75 70 20 ns will mess up
ff20: 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 the . **
ff30: 69 6e 65 71 75 61 6c 69 74 79 2e 20 20 54 6f 20 inequality. To
ff40: 61 76 6f 69 64 20 74 68 69 73 2c 20 6d 61 6b 65 avoid this, make
ff50: 20 73 75 72 65 20 74 6f 20 61 6c 73 6f 20 72 75 sure to also ru
ff60: 6e 20 74 68 65 20 66 75 6c 6c 0a 20 20 20 20 20 n the full.
ff70: 20 20 20 2a 2a 20 4c 49 4b 45 20 6f 6e 20 61 6c ** LIKE on al
ff80: 6c 20 63 61 6e 64 69 64 61 74 65 20 65 78 70 72 l candidate expr
ff90: 65 73 73 69 6f 6e 73 20 62 79 20 63 6c 65 61 72 essions by clear
ffa0: 69 6e 67 20 74 68 65 20 69 73 43 6f 6d 70 6c 65 ing the isComple
ffb0: 74 65 20 66 6c 61 67 0a 20 20 20 20 20 20 20 20 te flag.
ffc0: 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 */. if( c
ffd0: 3d 3d 27 41 27 2d 31 20 29 20 69 73 43 6f 6d 70 =='A'-1 ) isComp
ffe0: 6c 65 74 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 lete = 0;.
fff0: 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 70 70 c = sqlite3Upp
10000 65 72 54 6f 4c 6f 77 65 72 5b 63 5d 3b 0a 20 20 erToLower[c];.
10010 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 43 20 }. *pC
10020 3d 20 63 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 = c + 1;. }.
10030 20 20 20 73 43 6f 6c 6c 53 65 71 4e 61 6d 65 2e sCollSeqName.
10040 7a 20 3d 20 6e 6f 43 61 73 65 20 3f 20 22 4e 4f z = noCase ? "NO
10050 43 41 53 45 22 20 3a 20 22 42 49 4e 41 52 59 22 CASE" : "BINARY"
10060 3b 0a 20 20 20 20 73 43 6f 6c 6c 53 65 71 4e 61 ;. sCollSeqNa
10070 6d 65 2e 6e 20 3d 20 36 3b 0a 20 20 20 20 70 4e me.n = 6;. pN
10080 65 77 45 78 70 72 31 20 3d 20 73 71 6c 69 74 65 ewExpr1 = sqlite
10090 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c 65 3ExprDup(db, pLe
100a0 66 74 2c 20 30 29 3b 0a 20 20 20 20 70 4e 65 77 ft, 0);. pNew
100b0 45 78 70 72 31 20 3d 20 73 71 6c 69 74 65 33 50 Expr1 = sqlite3P
100c0 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f Expr(pParse, TK_
100d0 47 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 GE, .
100e0 73 71 6c 69 74 65 33 45 78 70 72 41 64 64 43 6f sqlite3ExprAddCo
100f0 6c 6c 61 74 65 54 6f 6b 65 6e 28 70 50 61 72 73 llateToken(pPars
10100 65 2c 70 4e 65 77 45 78 70 72 31 2c 26 73 43 6f e,pNewExpr1,&sCo
10110 6c 6c 53 65 71 4e 61 6d 65 29 2c 0a 20 20 20 20 llSeqName),.
10120 20 20 20 20 20 20 20 70 53 74 72 31 2c 20 30 29 pStr1, 0)
10130 3b 0a 20 20 20 20 74 72 61 6e 73 66 65 72 4a 6f ;. transferJo
10140 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77 45 inMarkings(pNewE
10150 78 70 72 31 2c 20 70 45 78 70 72 29 3b 0a 20 20 xpr1, pExpr);.
10160 20 20 69 64 78 4e 65 77 31 20 3d 20 77 68 65 72 idxNew1 = wher
10170 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 eClauseInsert(pW
10180 43 2c 20 70 4e 65 77 45 78 70 72 31 2c 20 54 45 C, pNewExpr1, TE
10190 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f RM_VIRTUAL|TERM_
101a0 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 DYNAMIC);. te
101b0 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 31 3d stcase( idxNew1=
101c0 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e =0 );. exprAn
101d0 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c alyze(pSrc, pWC,
101e0 20 69 64 78 4e 65 77 31 29 3b 0a 20 20 20 20 70 idxNew1);. p
101f0 4e 65 77 45 78 70 72 32 20 3d 20 73 71 6c 69 74 NewExpr2 = sqlit
10200 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c e3ExprDup(db, pL
10210 65 66 74 2c 20 30 29 3b 0a 20 20 20 20 70 4e 65 eft, 0);. pNe
10220 77 45 78 70 72 32 20 3d 20 73 71 6c 69 74 65 33 wExpr2 = sqlite3
10230 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b PExpr(pParse, TK
10240 5f 4c 54 2c 0a 20 20 20 20 20 20 20 20 20 20 20 _LT,.
10250 73 71 6c 69 74 65 33 45 78 70 72 41 64 64 43 6f sqlite3ExprAddCo
10260 6c 6c 61 74 65 54 6f 6b 65 6e 28 70 50 61 72 73 llateToken(pPars
10270 65 2c 70 4e 65 77 45 78 70 72 32 2c 26 73 43 6f e,pNewExpr2,&sCo
10280 6c 6c 53 65 71 4e 61 6d 65 29 2c 0a 20 20 20 20 llSeqName),.
10290 20 20 20 20 20 20 20 70 53 74 72 32 2c 20 30 29 pStr2, 0)
102a0 3b 0a 20 20 20 20 74 72 61 6e 73 66 65 72 4a 6f ;. transferJo
102b0 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77 45 inMarkings(pNewE
102c0 78 70 72 32 2c 20 70 45 78 70 72 29 3b 0a 20 20 xpr2, pExpr);.
102d0 20 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65 72 idxNew2 = wher
102e0 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 eClauseInsert(pW
102f0 43 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54 45 C, pNewExpr2, TE
10300 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f RM_VIRTUAL|TERM_
10310 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 DYNAMIC);. te
10320 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 32 3d stcase( idxNew2=
10330 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e =0 );. exprAn
10340 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c alyze(pSrc, pWC,
10350 20 69 64 78 4e 65 77 32 29 3b 0a 20 20 20 20 70 idxNew2);. p
10360 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 Term = &pWC->a[i
10370 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 69 66 28 dxTerm];. if(
10380 20 69 73 43 6f 6d 70 6c 65 74 65 20 29 7b 0a 20 isComplete ){.
10390 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e pWC->a[idxN
103a0 65 77 31 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 ew1].iParent = i
103b0 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 57 dxTerm;. pW
103c0 43 2d 3e 61 5b 69 64 78 4e 65 77 32 5d 2e 69 50 C->a[idxNew2].iP
103d0 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b arent = idxTerm;
103e0 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 . pTerm->nC
103f0 68 69 6c 64 20 3d 20 32 3b 0a 20 20 20 20 7d 0a hild = 2;. }.
10400 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 }.#endif /* SQ
10410 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f LITE_OMIT_LIKE_O
10420 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a PTIMIZATION */..
10430 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
10440 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 MIT_VIRTUALTABLE
10450 0a 20 20 2f 2a 20 41 64 64 20 61 20 57 4f 5f 4d . /* Add a WO_M
10460 41 54 43 48 20 61 75 78 69 6c 69 61 72 79 20 74 ATCH auxiliary t
10470 65 72 6d 20 74 6f 20 74 68 65 20 63 6f 6e 73 74 erm to the const
10480 72 61 69 6e 74 20 73 65 74 20 69 66 20 74 68 65 raint set if the
10490 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 65 78 . ** current ex
104a0 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 pression is of t
104b0 68 65 20 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e he form: column
104c0 20 4d 41 54 43 48 20 65 78 70 72 2e 0a 20 20 2a MATCH expr.. *
104d0 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 * This informati
104e0 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 68 on is used by th
104f0 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 e xBestIndex met
10500 68 6f 64 73 20 6f 66 0a 20 20 2a 2a 20 76 69 72 hods of. ** vir
10510 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 54 68 tual tables. Th
10520 65 20 6e 61 74 69 76 65 20 71 75 65 72 79 20 6f e native query o
10530 70 74 69 6d 69 7a 65 72 20 64 6f 65 73 20 6e 6f ptimizer does no
10540 74 20 61 74 74 65 6d 70 74 0a 20 20 2a 2a 20 74 t attempt. ** t
10550 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 77 69 o do anything wi
10560 74 68 20 4d 41 54 43 48 20 66 75 6e 63 74 69 6f th MATCH functio
10570 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 ns.. */. if( i
10580 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 70 sMatchOfColumn(p
10590 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 Expr) ){. int
105a0 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 45 78 70 idxNew;. Exp
105b0 72 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 r *pRight, *pLef
105c0 74 3b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d t;. WhereTerm
105d0 20 2a 70 4e 65 77 54 65 72 6d 3b 0a 20 20 20 20 *pNewTerm;.
105e0 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 43 6f Bitmask prereqCo
105f0 6c 75 6d 6e 2c 20 70 72 65 72 65 71 45 78 70 72 lumn, prereqExpr
10600 3b 0a 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 ;.. pRight =
10610 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e pExpr->x.pList->
10620 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 a[0].pExpr;.
10630 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 78 pLeft = pExpr->x
10640 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 .pList->a[1].pEx
10650 70 72 3b 0a 20 20 20 20 70 72 65 72 65 71 45 78 pr;. prereqEx
10660 70 72 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 pr = exprTableUs
10670 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 52 age(pMaskSet, pR
10680 69 67 68 74 29 3b 0a 20 20 20 20 70 72 65 72 65 ight);. prere
10690 71 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61 qColumn = exprTa
106a0 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 bleUsage(pMaskSe
106b0 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 t, pLeft);. i
106c0 66 28 20 28 70 72 65 72 65 71 45 78 70 72 20 26 f( (prereqExpr &
106d0 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d prereqColumn)==
106e0 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 0 ){. Expr
106f0 2a 70 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20 *pNewExpr;.
10700 20 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 pNewExpr = sqli
10710 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c te3PExpr(pParse,
10720 20 54 4b 5f 4d 41 54 43 48 2c 20 0a 20 20 20 20 TK_MATCH, .
10730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10740 20 20 20 20 20 20 20 20 20 20 30 2c 20 73 71 6c 0, sql
10750 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 ite3ExprDup(db,
10760 70 52 69 67 68 74 2c 20 30 29 2c 20 30 29 3b 0a pRight, 0), 0);.
10770 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 idxNew = w
10780 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 hereClauseInsert
10790 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20 (pWC, pNewExpr,
107a0 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 TERM_VIRTUAL|TER
107b0 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 M_DYNAMIC);.
107c0 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e testcase( idxN
107d0 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 ew==0 );. p
107e0 4e 65 77 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e NewTerm = &pWC->
107f0 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20 a[idxNew];.
10800 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 pNewTerm->prere
10810 71 52 69 67 68 74 20 3d 20 70 72 65 72 65 71 45 qRight = prereqE
10820 78 70 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 xpr;. pNewT
10830 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 erm->leftCursor
10840 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b = pLeft->iTable;
10850 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d . pNewTerm-
10860 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 >u.leftColumn =
10870 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a pLeft->iColumn;.
10880 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e pNewTerm->
10890 65 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 4d eOperator = WO_M
108a0 41 54 43 48 3b 0a 20 20 20 20 20 20 70 4e 65 77 ATCH;. pNew
108b0 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 Term->iParent =
108c0 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 idxTerm;. p
108d0 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 Term = &pWC->a[i
108e0 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 dxTerm];. p
108f0 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 Term->nChild = 1
10900 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 ;. pTerm->w
10910 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 tFlags |= TERM_C
10920 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 70 4e 65 OPIED;. pNe
10930 77 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c wTerm->prereqAll
10940 20 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 = pTerm->prereq
10950 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 All;. }. }.#
10960 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f endif /* SQLITE_
10970 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c OMIT_VIRTUALTABL
10980 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c E */..#ifdef SQL
10990 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
109a0 5f 4f 52 5f 53 54 41 54 34 0a 20 20 2f 2a 20 57 _OR_STAT4. /* W
109b0 68 65 6e 20 73 71 6c 69 74 65 5f 73 74 61 74 33 hen sqlite_stat3
109c0 20 68 69 73 74 6f 67 72 61 6d 20 64 61 74 61 20 histogram data
109d0 69 73 20 61 76 61 69 6c 61 62 6c 65 20 61 6e 20 is available an
109e0 6f 70 65 72 61 74 6f 72 20 6f 66 20 74 68 65 0a operator of the.
109f0 20 20 2a 2a 20 66 6f 72 6d 20 22 78 20 49 53 20 ** form "x IS
10a00 4e 4f 54 20 4e 55 4c 4c 22 20 63 61 6e 20 73 6f NOT NULL" can so
10a10 6d 65 74 69 6d 65 73 20 62 65 20 65 76 61 6c 75 metimes be evalu
10a20 61 74 65 64 20 6d 6f 72 65 20 65 66 66 69 63 69 ated more effici
10a30 65 6e 74 6c 79 0a 20 20 2a 2a 20 61 73 20 22 78 ently. ** as "x
10a40 3e 4e 55 4c 4c 22 20 69 66 20 78 20 69 73 20 6e >NULL" if x is n
10a50 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 ot an INTEGER PR
10a60 49 4d 41 52 59 20 4b 45 59 2e 20 20 53 6f 20 63 IMARY KEY. So c
10a70 6f 6e 73 74 72 75 63 74 20 61 0a 20 20 2a 2a 20 onstruct a. **
10a80 76 69 72 74 75 61 6c 20 74 65 72 6d 20 6f 66 20 virtual term of
10a90 74 68 61 74 20 66 6f 72 6d 2e 0a 20 20 2a 2a 0a that form.. **.
10aa0 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 ** Note that t
10ab0 68 65 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20 he virtual term
10ac0 6d 75 73 74 20 62 65 20 74 61 67 67 65 64 20 77 must be tagged w
10ad0 69 74 68 20 54 45 52 4d 5f 56 4e 55 4c 4c 2e 20 ith TERM_VNULL.
10ae0 20 54 68 69 73 0a 20 20 2a 2a 20 54 45 52 4d 5f This. ** TERM_
10af0 56 4e 55 4c 4c 20 74 61 67 20 77 69 6c 6c 20 73 VNULL tag will s
10b00 75 70 70 72 65 73 73 20 74 68 65 20 6e 6f 74 2d uppress the not-
10b10 6e 75 6c 6c 20 63 68 65 63 6b 20 61 74 20 74 68 null check at th
10b20 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a e beginning. **
10b30 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 57 of the loop. W
10b40 69 74 68 6f 75 74 20 74 68 65 20 54 45 52 4d 5f ithout the TERM_
10b50 56 4e 55 4c 4c 20 66 6c 61 67 2c 20 74 68 65 20 VNULL flag, the
10b60 6e 6f 74 2d 6e 75 6c 6c 20 63 68 65 63 6b 20 61 not-null check a
10b70 74 0a 20 20 2a 2a 20 74 68 65 20 73 74 61 72 74 t. ** the start
10b80 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 77 69 6c of the loop wil
10b90 6c 20 70 72 65 76 65 6e 74 20 61 6e 79 20 72 65 l prevent any re
10ba0 73 75 6c 74 73 20 66 72 6f 6d 20 62 65 69 6e 67 sults from being
10bb0 20 72 65 74 75 72 6e 65 64 2e 0a 20 20 2a 2f 0a returned.. */.
10bc0 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d if( pExpr->op=
10bd0 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 0a 20 20 20 26 =TK_NOTNULL. &
10be0 26 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e & pExpr->pLeft->
10bf0 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 0a 20 20 op==TK_COLUMN.
10c00 20 26 26 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 && pExpr->pLeft
10c10 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 0a 20 20 20 ->iColumn>=0.
10c20 26 26 20 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 45 && OptimizationE
10c30 6e 61 62 6c 65 64 28 64 62 2c 20 53 51 4c 49 54 nabled(db, SQLIT
10c40 45 5f 53 74 61 74 33 29 0a 20 20 29 7b 0a 20 20 E_Stat3). ){.
10c50 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 Expr *pNewExpr
10c60 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 ;. Expr *pLef
10c70 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 t = pExpr->pLeft
10c80 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77 ;. int idxNew
10c90 3b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 ;. WhereTerm
10ca0 2a 70 4e 65 77 54 65 72 6d 3b 0a 0a 20 20 20 20 *pNewTerm;..
10cb0 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 pNewExpr = sqlit
10cc0 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 e3PExpr(pParse,
10cd0 54 4b 5f 47 54 2c 0a 20 20 20 20 20 20 20 20 20 TK_GT,.
10ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10cf0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 sqlite3ExprDu
10d00 70 28 64 62 2c 20 70 4c 65 66 74 2c 20 30 29 2c p(db, pLeft, 0),
10d10 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
10d20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
10d30 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 ite3PExpr(pParse
10d40 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c , TK_NULL, 0, 0,
10d50 20 30 29 2c 20 30 29 3b 0a 0a 20 20 20 20 69 64 0), 0);.. id
10d60 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c 61 75 xNew = whereClau
10d70 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e seInsert(pWC, pN
10d80 65 77 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 ewExpr,.
10d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10da0 20 20 20 20 20 20 54 45 52 4d 5f 56 49 52 54 55 TERM_VIRTU
10db0 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 7c AL|TERM_DYNAMIC|
10dc0 54 45 52 4d 5f 56 4e 55 4c 4c 29 3b 0a 20 20 20 TERM_VNULL);.
10dd0 20 69 66 28 20 69 64 78 4e 65 77 20 29 7b 0a 20 if( idxNew ){.
10de0 20 20 20 20 20 70 4e 65 77 54 65 72 6d 20 3d 20 pNewTerm =
10df0 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77 5d 3b &pWC->a[idxNew];
10e00 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d . pNewTerm-
10e10 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 30 >prereqRight = 0
10e20 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d ;. pNewTerm
10e30 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70 ->leftCursor = p
10e40 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 Left->iTable;.
10e50 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 75 2e pNewTerm->u.
10e60 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 leftColumn = pLe
10e70 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 ft->iColumn;.
10e80 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 65 4f 70 pNewTerm->eOp
10e90 65 72 61 74 6f 72 20 3d 20 57 4f 5f 47 54 3b 0a erator = WO_GT;.
10ea0 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e pNewTerm->
10eb0 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 iParent = idxTer
10ec0 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d m;. pTerm =
10ed0 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d &pWC->a[idxTerm
10ee0 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e ];. pTerm->
10ef0 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 nChild = 1;.
10f00 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 pTerm->wtFlags
10f10 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b |= TERM_COPIED;
10f20 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d . pNewTerm-
10f30 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 54 65 >prereqAll = pTe
10f40 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0a 20 rm->prereqAll;.
10f50 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 }. }.#endif
10f60 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 /* SQLITE_ENABLE
10f70 5f 53 54 41 54 33 5f 4f 52 5f 53 54 41 54 34 20 _STAT3_OR_STAT4
10f80 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 76 65 6e 74 */.. /* Prevent
10f90 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72 6d 73 ON clause terms
10fa0 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 of a LEFT JOIN
10fb0 66 72 6f 6d 20 62 65 69 6e 67 20 75 73 65 64 20 from being used
10fc0 74 6f 20 64 72 69 76 65 0a 20 20 2a 2a 20 61 6e to drive. ** an
10fd0 20 69 6e 64 65 78 20 66 6f 72 20 74 61 62 6c 65 index for table
10fe0 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 s to the left of
10ff0 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a the join.. */.
11000 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 pTerm->prereqR
11010 69 67 68 74 20 7c 3d 20 65 78 74 72 61 52 69 67 ight |= extraRig
11020 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ht;.}../*.** Thi
11030 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 61 72 63 s function searc
11040 68 65 73 20 70 4c 69 73 74 20 66 6f 72 20 61 20 hes pList for a
11050 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 entry that match
11060 65 73 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 es the iCol-th c
11070 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 69 6e 64 65 olumn.** of inde
11080 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 x pIdx..**.** If
11090 20 73 75 63 68 20 61 6e 20 65 78 70 72 65 73 73 such an express
110a0 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2c 20 69 74 ion is found, it
110b0 73 20 69 6e 64 65 78 20 69 6e 20 70 4c 69 73 74 s index in pList
110c0 2d 3e 61 5b 5d 20 69 73 20 72 65 74 75 72 6e 65 ->a[] is returne
110d0 64 2e 20 49 66 0a 2a 2a 20 6e 6f 20 65 78 70 72 d. If.** no expr
110e0 65 73 73 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2c ession is found,
110f0 20 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e -1 is returned.
11100 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 .*/.static int f
11110 69 6e 64 49 6e 64 65 78 43 6f 6c 28 0a 20 20 50 indIndexCol(. P
11120 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
11130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
11140 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 * Parse context
11150 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 */. ExprList *p
11160 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 List,
11170 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 /* Expressi
11180 6f 6e 20 6c 69 73 74 20 74 6f 20 73 65 61 72 63 on list to searc
11190 68 20 2a 2f 0a 20 20 69 6e 74 20 69 42 61 73 65 h */. int iBase
111a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
111b0 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 /* Cursor
111c0 20 66 6f 72 20 74 61 62 6c 65 20 61 73 73 6f 63 for table assoc
111d0 69 61 74 65 64 20 77 69 74 68 20 70 49 64 78 20 iated with pIdx
111e0 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 */. Index *pIdx
111f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
11200 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 74 6f /* Index to
11210 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 match column of
11220 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 20 20 */. int iCol
11230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11240 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 /* Column
11250 6f 66 20 69 6e 64 65 78 20 74 6f 20 6d 61 74 63 of index to matc
11260 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b h */.){. int i;
11270 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a . const char *z
11280 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43 Coll = pIdx->azC
11290 6f 6c 6c 5b 69 43 6f 6c 5d 3b 0a 0a 20 20 66 6f oll[iCol];.. fo
112a0 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e r(i=0; i<pList->
112b0 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
112c0 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 Expr *p = sqlit
112d0 65 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 74 e3ExprSkipCollat
112e0 65 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 e(pList->a[i].pE
112f0 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 70 2d xpr);. if( p-
11300 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 0a 20 >op==TK_COLUMN.
11310 20 20 20 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d && p->iColum
11320 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d n==pIdx->aiColum
11330 6e 5b 69 43 6f 6c 5d 0a 20 20 20 20 20 26 26 20 n[iCol]. &&
11340 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 42 61 73 65 p->iTable==iBase
11350 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 43 6f . ){. Co
11360 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 73 llSeq *pColl = s
11370 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 qlite3ExprCollSe
11380 71 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d q(pParse, pList-
11390 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 >a[i].pExpr);.
113a0 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 if( ALWAYS(p
113b0 43 6f 6c 6c 29 20 26 26 20 30 3d 3d 73 71 6c 69 Coll) && 0==sqli
113c0 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c te3StrICmp(pColl
113d0 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20 ->zName, zColl)
113e0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 ){. retur
113f0 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 n i;. }.
11400 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e }. }.. return
11410 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 -1;.}../*.** Re
11420 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 turn true if the
11430 20 44 49 53 54 49 4e 43 54 20 65 78 70 72 65 73 DISTINCT expres
11440 73 69 6f 6e 2d 6c 69 73 74 20 70 61 73 73 65 64 sion-list passed
11450 20 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72 as the third ar
11460 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 72 65 64 gument.** is red
11470 75 6e 64 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 undant..**.** A
11480 44 49 53 54 49 4e 43 54 20 6c 69 73 74 20 69 73 DISTINCT list is
11490 20 72 65 64 75 6e 64 61 6e 74 20 69 66 20 74 68 redundant if th
114a0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 e database conta
114b0 69 6e 73 20 73 6f 6d 65 20 73 75 62 73 65 74 20 ins some subset
114c0 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 73 20 74 68 of.** columns th
114d0 61 74 20 61 72 65 20 75 6e 69 71 75 65 20 61 6e at are unique an
114e0 64 20 6e 6f 6e 2d 6e 75 6c 6c 2e 0a 2a 2f 0a 73 d non-null..*/.s
114f0 74 61 74 69 63 20 69 6e 74 20 69 73 44 69 73 74 tatic int isDist
11500 69 6e 63 74 52 65 64 75 6e 64 61 6e 74 28 0a 20 inctRedundant(.
11510 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
11520 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
11530 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
11540 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 . SrcList *pTab
11550 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 List, /*
11560 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 The FROM clause
11570 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 */. WhereClause
11580 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 2f *pWC, /
11590 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 * The WHERE clau
115a0 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 se */. ExprList
115b0 20 2a 70 44 69 73 74 69 6e 63 74 20 20 20 20 20 *pDistinct
115c0 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 /* The result
115d0 73 65 74 20 74 68 61 74 20 6e 65 65 64 73 20 74 set that needs t
115e0 6f 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f o be DISTINCT */
115f0 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 .){. Table *pTa
11600 62 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 b;. Index *pIdx
11610 3b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 ;. int i;
11620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
11630 20 20 20 20 0a 20 20 69 6e 74 20 69 42 61 73 65 . int iBase
11640 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 ;.. /* If there
11650 20 69 73 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e is more than on
11660 65 20 74 61 62 6c 65 20 6f 72 20 73 75 62 2d 73 e table or sub-s
11670 65 6c 65 63 74 20 69 6e 20 74 68 65 20 46 52 4f elect in the FRO
11680 4d 20 63 6c 61 75 73 65 20 6f 66 0a 20 20 2a 2a M clause of. **
11690 20 74 68 69 73 20 71 75 65 72 79 2c 20 74 68 65 this query, the
116a0 6e 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 n it will not be
116b0 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 73 68 6f possible to sho
116c0 77 20 74 68 61 74 20 74 68 65 20 44 49 53 54 49 w that the DISTI
116d0 4e 43 54 20 0a 20 20 2a 2a 20 63 6c 61 75 73 65 NCT . ** clause
116e0 20 69 73 20 72 65 64 75 6e 64 61 6e 74 2e 20 2a is redundant. *
116f0 2f 0a 20 20 69 66 28 20 70 54 61 62 4c 69 73 74 /. if( pTabList
11700 2d 3e 6e 53 72 63 21 3d 31 20 29 20 72 65 74 75 ->nSrc!=1 ) retu
11710 72 6e 20 30 3b 0a 20 20 69 42 61 73 65 20 3d 20 rn 0;. iBase =
11720 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 pTabList->a[0].i
11730 43 75 72 73 6f 72 3b 0a 20 20 70 54 61 62 20 3d Cursor;. pTab =
11740 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e pTabList->a[0].
11750 70 54 61 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 pTab;.. /* If a
11760 6e 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 ny of the expres
11770 73 69 6f 6e 73 20 69 73 20 61 6e 20 49 50 4b 20 sions is an IPK
11780 63 6f 6c 75 6d 6e 20 6f 6e 20 74 61 62 6c 65 20 column on table
11790 69 42 61 73 65 2c 20 74 68 65 6e 20 72 65 74 75 iBase, then retu
117a0 72 6e 20 0a 20 20 2a 2a 20 74 72 75 65 2e 20 4e rn . ** true. N
117b0 6f 74 65 3a 20 54 68 65 20 28 70 2d 3e 69 54 61 ote: The (p->iTa
117c0 62 6c 65 3d 3d 69 42 61 73 65 29 20 70 61 72 74 ble==iBase) part
117d0 20 6f 66 20 74 68 69 73 20 74 65 73 74 20 6d 61 of this test ma
117e0 79 20 62 65 20 66 61 6c 73 65 20 69 66 20 74 68 y be false if th
117f0 65 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 53 e. ** current S
11800 45 4c 45 43 54 20 69 73 20 61 20 63 6f 72 72 65 ELECT is a corre
11810 6c 61 74 65 64 20 73 75 62 2d 71 75 65 72 79 2e lated sub-query.
11820 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b . */. for(i=0;
11830 20 69 3c 70 44 69 73 74 69 6e 63 74 2d 3e 6e 45 i<pDistinct->nE
11840 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 xpr; i++){. E
11850 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 xpr *p = sqlite3
11860 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 74 65 28 ExprSkipCollate(
11870 70 44 69 73 74 69 6e 63 74 2d 3e 61 5b 69 5d 2e pDistinct->a[i].
11880 70 45 78 70 72 29 3b 0a 20 20 20 20 69 66 28 20 pExpr);. if(
11890 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e p->op==TK_COLUMN
118a0 20 26 26 20 70 2d 3e 69 54 61 62 6c 65 3d 3d 69 && p->iTable==i
118b0 42 61 73 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 Base && p->iColu
118c0 6d 6e 3c 30 20 29 20 72 65 74 75 72 6e 20 31 3b mn<0 ) return 1;
118d0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 . }.. /* Loop
118e0 74 68 72 6f 75 67 68 20 61 6c 6c 20 69 6e 64 69 through all indi
118f0 63 65 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 ces on the table
11900 2c 20 63 68 65 63 6b 69 6e 67 20 65 61 63 68 20 , checking each
11910 74 6f 20 73 65 65 20 69 66 20 69 74 20 6d 61 6b to see if it mak
11920 65 73 0a 20 20 2a 2a 20 74 68 65 20 44 49 53 54 es. ** the DIST
11930 49 4e 43 54 20 71 75 61 6c 69 66 69 65 72 20 72 INCT qualifier r
11940 65 64 75 6e 64 61 6e 74 2e 20 49 74 20 64 6f 65 edundant. It doe
11950 73 20 73 6f 20 69 66 3a 0a 20 20 2a 2a 0a 20 20 s so if:. **.
11960 2a 2a 20 20 20 31 2e 20 54 68 65 20 69 6e 64 65 ** 1. The inde
11970 78 20 69 73 20 69 74 73 65 6c 66 20 55 4e 49 51 x is itself UNIQ
11980 55 45 2c 20 61 6e 64 0a 20 20 2a 2a 0a 20 20 2a UE, and. **. *
11990 2a 20 20 20 32 2e 20 41 6c 6c 20 6f 66 20 74 68 * 2. All of th
119a0 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 e columns in the
119b0 20 69 6e 64 65 78 20 61 72 65 20 65 69 74 68 65 index are eithe
119c0 72 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 44 r part of the pD
119d0 69 73 74 69 6e 63 74 0a 20 20 2a 2a 20 20 20 20 istinct. **
119e0 20 20 6c 69 73 74 2c 20 6f 72 20 65 6c 73 65 20 list, or else
119f0 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
11a00 20 63 6f 6e 74 61 69 6e 73 20 61 20 74 65 72 6d contains a term
11a10 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 63 6f of the form "co
11a20 6c 3d 58 22 2c 0a 20 20 2a 2a 20 20 20 20 20 20 l=X",. **
11a30 77 68 65 72 65 20 58 20 69 73 20 61 20 63 6f 6e where X is a con
11a40 73 74 61 6e 74 20 76 61 6c 75 65 2e 20 54 68 65 stant value. The
11a50 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 collation seque
11a60 6e 63 65 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a nces of the. **
11a70 20 20 20 20 20 20 63 6f 6d 70 61 72 69 73 6f 6e comparison
11a80 20 61 6e 64 20 73 65 6c 65 63 74 2d 6c 69 73 74 and select-list
11a90 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6d 75 73 expressions mus
11aa0 74 20 6d 61 74 63 68 20 74 68 6f 73 65 20 6f 66 t match those of
11ab0 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 2a 2a the index.. **
11ac0 0a 20 20 2a 2a 20 20 20 33 2e 20 41 6c 6c 20 6f . ** 3. All o
11ad0 66 20 74 68 6f 73 65 20 69 6e 64 65 78 20 63 6f f those index co
11ae0 6c 75 6d 6e 73 20 66 6f 72 20 77 68 69 63 68 20 lumns for which
11af0 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
11b00 20 64 6f 65 73 20 6e 6f 74 0a 20 20 2a 2a 20 20 does not. **
11b10 20 20 20 20 63 6f 6e 74 61 69 6e 20 61 20 22 63 contain a "c
11b20 6f 6c 3d 58 22 20 74 65 72 6d 20 61 72 65 20 73 ol=X" term are s
11b30 75 62 6a 65 63 74 20 74 6f 20 61 20 4e 4f 54 20 ubject to a NOT
11b40 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 2e NULL constraint.
11b50 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 49 64 78 . */. for(pIdx
11b60 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 =pTab->pIndex; p
11b70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e Idx; pIdx=pIdx->
11b80 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 pNext){. if(
11b90 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f pIdx->onError==O
11ba0 45 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e 75 E_None ) continu
11bb0 65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 e;. for(i=0;
11bc0 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b i<pIdx->nColumn;
11bd0 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 i++){. int
11be0 20 69 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 61 69 iCol = pIdx->ai
11bf0 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 Column[i];.
11c00 20 69 66 28 20 30 3d 3d 66 69 6e 64 54 65 72 6d if( 0==findTerm
11c10 28 70 57 43 2c 20 69 42 61 73 65 2c 20 69 43 6f (pWC, iBase, iCo
11c20 6c 2c 20 7e 28 42 69 74 6d 61 73 6b 29 30 2c 20 l, ~(Bitmask)0,
11c30 57 4f 5f 45 51 2c 20 70 49 64 78 29 20 29 7b 0a WO_EQ, pIdx) ){.
11c40 20 20 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 int iIdx
11c50 43 6f 6c 20 3d 20 66 69 6e 64 49 6e 64 65 78 43 Col = findIndexC
11c60 6f 6c 28 70 50 61 72 73 65 2c 20 70 44 69 73 74 ol(pParse, pDist
11c70 69 6e 63 74 2c 20 69 42 61 73 65 2c 20 70 49 64 inct, iBase, pId
11c80 78 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 69 x, i);. i
11c90 66 28 20 69 49 64 78 43 6f 6c 3c 30 20 7c 7c 20 f( iIdxCol<0 ||
11ca0 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d pTab->aCol[pIdx-
11cb0 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 2e 6e 6f >aiColumn[i]].no
11cc0 74 4e 75 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 tNull==0 ){.
11cd0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
11ce0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
11cf0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d }. if( i==
11d00 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b pIdx->nColumn ){
11d10 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 . /* This i
11d20 6e 64 65 78 20 69 6d 70 6c 69 65 73 20 74 68 61 ndex implies tha
11d30 74 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 71 t the DISTINCT q
11d40 75 61 6c 69 66 69 65 72 20 69 73 20 72 65 64 75 ualifier is redu
11d50 6e 64 61 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 ndant. */.
11d60 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a return 1;. }.
11d70 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b }.. return 0;
11d80 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 73 74 69 6d .}.../*.** Estim
11d90 61 74 65 20 74 68 65 20 6c 6f 67 61 72 69 74 68 ate the logarith
11da0 6d 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 76 m of the input v
11db0 61 6c 75 65 20 74 6f 20 62 61 73 65 20 32 2e 0a alue to base 2..
11dc0 2a 2f 0a 73 74 61 74 69 63 20 4c 6f 67 45 73 74 */.static LogEst
11dd0 20 65 73 74 4c 6f 67 28 4c 6f 67 45 73 74 20 4e estLog(LogEst N
11de0 29 7b 0a 20 20 4c 6f 67 45 73 74 20 78 20 3d 20 ){. LogEst x =
11df0 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 4e 29 sqlite3LogEst(N)
11e00 3b 0a 20 20 72 65 74 75 72 6e 20 78 3e 33 33 20 ;. return x>33
11e10 3f 20 78 20 2d 20 33 33 20 3a 20 30 3b 0a 7d 0a ? x - 33 : 0;.}.
11e20 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 72 6f 75 74 69 ./*.** Two routi
11e30 6e 65 73 20 66 6f 72 20 70 72 69 6e 74 69 6e 67 nes for printing
11e40 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
11e50 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 an sqlite3_index
11e60 5f 69 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 _info.** structu
11e70 72 65 2e 20 20 55 73 65 64 20 66 6f 72 20 74 65 re. Used for te
11e80 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 sting and debugg
11e90 69 6e 67 20 6f 6e 6c 79 2e 20 20 49 66 20 6e 65 ing only. If ne
11ea0 69 74 68 65 72 0a 2a 2a 20 53 51 4c 49 54 45 5f ither.** SQLITE_
11eb0 54 45 53 54 20 6f 72 20 53 51 4c 49 54 45 5f 44 TEST or SQLITE_D
11ec0 45 42 55 47 20 61 72 65 20 64 65 66 69 6e 65 64 EBUG are defined
11ed0 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f 75 , then these rou
11ee0 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 2d tines.** are no-
11ef0 6f 70 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 ops..*/.#if !def
11f00 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
11f10 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20 26 _VIRTUALTABLE) &
11f20 26 20 64 65 66 69 6e 65 64 28 57 48 45 52 45 54 & defined(WHERET
11f30 52 41 43 45 5f 45 4e 41 42 4c 45 44 29 0a 73 74 RACE_ENABLED).st
11f40 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f atic void TRACE_
11f50 49 44 58 5f 49 4e 50 55 54 53 28 73 71 6c 69 74 IDX_INPUTS(sqlit
11f60 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 e3_index_info *p
11f70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 ){. int i;. if
11f80 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65 54 ( !sqlite3WhereT
11f90 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 race ) return;.
11fa0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
11fb0 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 Constraint; i++)
11fc0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 {. sqlite3Deb
11fd0 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f 6e 73 ugPrintf(" cons
11fe0 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c 3d traint[%d]: col=
11ff0 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f 70 3d %d termid=%d op=
12000 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e 22 %d usabled=%d\n"
12010 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20 ,. i,.
12020 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e p->aConstrain
12030 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 t[i].iColumn,.
12040 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 p->aConstra
12050 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73 int[i].iTermOffs
12060 65 74 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 et,. p->aC
12070 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 6f 70 2c onstraint[i].op,
12080 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 . p->aCons
12090 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 traint[i].usable
120a0 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 );. }. for(i=0
120b0 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65 72 42 79 3b ; i<p->nOrderBy;
120c0 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 i++){. sqlit
120d0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 e3DebugPrintf("
120e0 20 6f 72 64 65 72 62 79 5b 25 64 5d 3a 20 63 6f orderby[%d]: co
120f0 6c 3d 25 64 20 64 65 73 63 3d 25 64 5c 6e 22 2c l=%d desc=%d\n",
12100 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 . i,.
12110 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d p->aOrderBy[i]
12120 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 .iColumn,.
12130 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e p->aOrderBy[i].
12140 64 65 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 desc);. }.}.sta
12150 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f 49 tic void TRACE_I
12160 44 58 5f 4f 55 54 50 55 54 53 28 73 71 6c 69 74 DX_OUTPUTS(sqlit
12170 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 e3_index_info *p
12180 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 ){. int i;. if
12190 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65 54 ( !sqlite3WhereT
121a0 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 race ) return;.
121b0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e for(i=0; i<p->n
121c0 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 Constraint; i++)
121d0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 {. sqlite3Deb
121e0 75 67 50 72 69 6e 74 66 28 22 20 20 75 73 61 67 ugPrintf(" usag
121f0 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78 3d 25 e[%d]: argvIdx=%
12200 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20 20 d omit=%d\n",.
12210 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20 70 i,. p
12220 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 ->aConstraintUsa
12230 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 2c ge[i].argvIndex,
12240 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 . p->aCons
12250 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 6f traintUsage[i].o
12260 6d 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 mit);. }. sqli
12270 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 te3DebugPrintf("
12280 20 20 69 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c 20 idxNum=%d\n",
12290 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a 20 20 73 71 p->idxNum);. sq
122a0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
122b0 28 22 20 20 69 64 78 53 74 72 3d 25 73 5c 6e 22 (" idxStr=%s\n"
122c0 2c 20 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 , p->idxStr);.
122d0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
122e0 74 66 28 22 20 20 6f 72 64 65 72 42 79 43 6f 6e tf(" orderByCon
122f0 73 75 6d 65 64 3d 25 64 5c 6e 22 2c 20 70 2d 3e sumed=%d\n", p->
12300 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 29 orderByConsumed)
12310 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 ;. sqlite3Debug
12320 50 72 69 6e 74 66 28 22 20 20 65 73 74 69 6d 61 Printf(" estima
12330 74 65 64 43 6f 73 74 3d 25 67 5c 6e 22 2c 20 70 tedCost=%g\n", p
12340 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 29 ->estimatedCost)
12350 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e ;.}.#else.#defin
12360 65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 e TRACE_IDX_INPU
12370 54 53 28 41 29 0a 23 64 65 66 69 6e 65 20 54 52 TS(A).#define TR
12380 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 ACE_IDX_OUTPUTS(
12390 41 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 A).#endif..#ifnd
123a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
123b0 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58 0a 2f UTOMATIC_INDEX./
123c0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 *.** Return TRUE
123d0 20 69 66 20 74 68 65 20 57 48 45 52 45 20 63 6c if the WHERE cl
123e0 61 75 73 65 20 74 65 72 6d 20 70 54 65 72 6d 20 ause term pTerm
123f0 69 73 20 6f 66 20 61 20 66 6f 72 6d 20 77 68 65 is of a form whe
12400 72 65 20 69 74 0a 2a 2a 20 63 6f 75 6c 64 20 62 re it.** could b
12410 65 20 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 e used with an i
12420 6e 64 65 78 20 74 6f 20 61 63 63 65 73 73 20 70 ndex to access p
12430 53 72 63 2c 20 61 73 73 75 6d 69 6e 67 20 61 6e Src, assuming an
12440 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 appropriate.**
12450 69 6e 64 65 78 20 65 78 69 73 74 65 64 2e 0a 2a index existed..*
12460 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 72 /.static int ter
12470 6d 43 61 6e 44 72 69 76 65 49 6e 64 65 78 28 0a mCanDriveIndex(.
12480 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 WhereTerm *pTe
12490 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 rm,
124a0 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 /* WHERE clause
124b0 20 74 65 72 6d 20 74 6f 20 63 68 65 63 6b 20 2a term to check *
124c0 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 /. struct SrcLi
124d0 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 st_item *pSrc,
124e0 20 20 20 2f 2a 20 54 61 62 6c 65 20 77 65 20 61 /* Table we a
124f0 72 65 20 74 72 79 69 6e 67 20 74 6f 20 61 63 63 re trying to acc
12500 65 73 73 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b ess */. Bitmask
12510 20 6e 6f 74 52 65 61 64 79 20 20 20 20 20 20 20 notReady
12520 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
12530 73 20 69 6e 20 6f 75 74 65 72 20 6c 6f 6f 70 73 s in outer loops
12540 20 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a of the join */.
12550 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20 ){. char aff;.
12560 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 if( pTerm->left
12570 43 75 72 73 6f 72 21 3d 70 53 72 63 2d 3e 69 43 Cursor!=pSrc->iC
12580 75 72 73 6f 72 20 29 20 72 65 74 75 72 6e 20 30 ursor ) return 0
12590 3b 0a 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e ;. if( (pTerm->
125a0 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 45 eOperator & WO_E
125b0 51 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 Q)==0 ) return 0
125c0 3b 0a 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e ;. if( (pTerm->
125d0 70 72 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f prereqRight & no
125e0 74 52 65 61 64 79 29 21 3d 30 20 29 20 72 65 74 tReady)!=0 ) ret
125f0 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 54 65 urn 0;. if( pTe
12600 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e rm->u.leftColumn
12610 3c 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 <0 ) return 0;.
12620 20 61 66 66 20 3d 20 70 53 72 63 2d 3e 70 54 61 aff = pSrc->pTa
12630 62 2d 3e 61 43 6f 6c 5b 70 54 65 72 6d 2d 3e 75 b->aCol[pTerm->u
12640 2e 6c 65 66 74 43 6f 6c 75 6d 6e 5d 2e 61 66 66 .leftColumn].aff
12650 69 6e 69 74 79 3b 0a 20 20 69 66 28 20 21 73 71 inity;. if( !sq
12660 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 lite3IndexAffini
12670 74 79 4f 6b 28 70 54 65 72 6d 2d 3e 70 45 78 70 tyOk(pTerm->pExp
12680 72 2c 20 61 66 66 29 20 29 20 72 65 74 75 72 6e r, aff) ) return
12690 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 0;. return 1;.
126a0 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 }.#endif...#ifnd
126b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 ef SQLITE_OMIT_A
126c0 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 58 0a 2f UTOMATIC_INDEX./
126d0 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
126e0 64 65 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 de to construct
126f0 74 68 65 20 49 6e 64 65 78 20 6f 62 6a 65 63 74 the Index object
12700 20 66 6f 72 20 61 6e 20 61 75 74 6f 6d 61 74 69 for an automati
12710 63 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 c index.** and t
12720 6f 20 73 65 74 20 75 70 20 74 68 65 20 57 68 65 o set up the Whe
12730 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74 20 70 reLevel object p
12740 4c 65 76 65 6c 20 73 6f 20 74 68 61 74 20 74 68 Level so that th
12750 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 e code generator
12760 0a 2a 2a 20 6d 61 6b 65 73 20 75 73 65 20 6f 66 .** makes use of
12770 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 69 the automatic i
12780 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ndex..*/.static
12790 76 6f 69 64 20 63 6f 6e 73 74 72 75 63 74 41 75 void constructAu
127a0 74 6f 6d 61 74 69 63 49 6e 64 65 78 28 0a 20 20 tomaticIndex(.
127b0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
127c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
127d0 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 he parsing conte
127e0 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 xt */. WhereCla
127f0 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 use *pWC,
12800 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 /* The WHERE
12810 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 clause */. str
12820 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
12830 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20 *pSrc, /* The
12840 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d FROM clause term
12850 20 74 6f 20 67 65 74 20 74 68 65 20 6e 65 78 74 to get the next
12860 20 69 6e 64 65 78 20 2a 2f 0a 20 20 42 69 74 6d index */. Bitm
12870 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 ask notReady,
12880 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 /* Mask
12890 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 of cursors that
128a0 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c are not availabl
128b0 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 e */. WhereLeve
128c0 6c 20 2a 70 4c 65 76 65 6c 20 20 20 20 20 20 20 l *pLevel
128d0 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65 77 20 /* Write new
128e0 69 6e 64 65 78 20 68 65 72 65 20 2a 2f 0a 29 7b index here */.){
128f0 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 . int nColumn;
12900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12910 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 * Number of colu
12920 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f 6e 73 74 mns in the const
12930 72 75 63 74 65 64 20 69 6e 64 65 78 20 2a 2f 0a ructed index */.
12940 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 WhereTerm *pTe
12950 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a rm; /*
12960 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 6f A single term o
12970 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 f the WHERE clau
12980 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 se */. WhereTer
12990 6d 20 2a 70 57 43 45 6e 64 3b 20 20 20 20 20 20 m *pWCEnd;
129a0 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 57 /* End of pW
129b0 43 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 C->a[] */. int
129c0 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 nByte;
129d0 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 /* Byte
129e0 6f 66 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 of memory needed
129f0 20 66 6f 72 20 70 49 64 78 20 2a 2f 0a 20 20 49 for pIdx */. I
12a00 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 ndex *pIdx;
12a10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 /* Ob
12a20 6a 65 63 74 20 64 65 73 63 72 69 62 69 6e 67 20 ject describing
12a30 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20 69 6e the transient in
12a40 64 65 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 dex */. Vdbe *v
12a50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
12a60 20 20 20 20 20 2f 2a 20 50 72 65 70 61 72 65 64 /* Prepared
12a70 20 73 74 61 74 65 6d 65 6e 74 20 75 6e 64 65 72 statement under
12a80 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f construction */
12a90 0a 20 20 69 6e 74 20 61 64 64 72 49 6e 69 74 3b . int addrInit;
12aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
12ab0 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 * Address of the
12ac0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 initialization
12ad0 62 79 70 61 73 73 20 6a 75 6d 70 20 2a 2f 0a 20 bypass jump */.
12ae0 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 20 Table *pTable;
12af0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12b00 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 The table being
12b10 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 4b 65 79 indexed */. Key
12b20 49 6e 66 6f 20 2a 70 4b 65 79 69 6e 66 6f 3b 20 Info *pKeyinfo;
12b30 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 /* Key
12b40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
12b50 74 68 65 20 69 6e 64 65 78 20 2a 2f 20 20 20 0a the index */ .
12b60 20 20 69 6e 74 20 61 64 64 72 54 6f 70 3b 20 20 int addrTop;
12b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
12b80 20 54 6f 70 20 6f 66 20 74 68 65 20 69 6e 64 65 Top of the inde
12b90 78 20 66 69 6c 6c 20 6c 6f 6f 70 20 2a 2f 0a 20 x fill loop */.
12ba0 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20 int regRecord;
12bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
12bc0 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 Register holding
12bd0 20 61 6e 20 69 6e 64 65 78 20 72 65 63 6f 72 64 an index record
12be0 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 */. int n;
12bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c00 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 63 6f 75 6e /* Column coun
12c10 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 ter */. int i;
12c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
12c30 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
12c40 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 nter */. int mx
12c50 42 69 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 BitCol;
12c60 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d /* Maximum
12c70 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 53 72 63 2d column in pSrc-
12c80 3e 63 6f 6c 55 73 65 64 20 2a 2f 0a 20 20 43 6f >colUsed */. Co
12c90 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 llSeq *pColl;
12ca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c /* Col
12cb0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 lating sequence
12cc0 74 6f 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 2a to on a column *
12cd0 2f 0a 20 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 /. WhereLoop *p
12ce0 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 Loop;
12cf0 2f 2a 20 54 68 65 20 4c 6f 6f 70 20 6f 62 6a 65 /* The Loop obje
12d00 63 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 ct */. Bitmask
12d10 69 64 78 43 6f 6c 73 3b 20 20 20 20 20 20 20 20 idxCols;
12d20 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 6f 66 /* Bitmap of
12d30 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64 20 66 6f columns used fo
12d40 72 20 69 6e 64 65 78 69 6e 67 20 2a 2f 0a 20 20 r indexing */.
12d50 42 69 74 6d 61 73 6b 20 65 78 74 72 61 43 6f 6c Bitmask extraCol
12d60 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 s; /* B
12d70 69 74 6d 61 70 20 6f 66 20 61 64 64 69 74 69 6f itmap of additio
12d80 6e 61 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 nal columns */.
12d90 20 75 38 20 73 65 6e 74 57 61 72 6e 69 6e 67 20 u8 sentWarning
12da0 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 = 0; /*
12db0 54 72 75 65 20 69 66 20 61 20 77 61 72 6e 6e 69 True if a warnni
12dc0 6e 67 20 68 61 73 20 62 65 65 6e 20 69 73 73 75 ng has been issu
12dd0 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 47 65 6e 65 ed */.. /* Gene
12de0 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 6b 69 rate code to ski
12df0 70 20 6f 76 65 72 20 74 68 65 20 63 72 65 61 74 p over the creat
12e00 69 6f 6e 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 ion and initiali
12e10 7a 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 zation of the.
12e20 2a 2a 20 74 72 61 6e 73 69 65 6e 74 20 69 6e 64 ** transient ind
12e30 65 78 20 6f 6e 20 32 6e 64 20 61 6e 64 20 73 75 ex on 2nd and su
12e40 62 73 65 71 75 65 6e 74 20 69 74 65 72 61 74 69 bsequent iterati
12e50 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e ons of the loop.
12e60 20 2a 2f 0a 20 20 76 20 3d 20 70 50 61 72 73 65 */. v = pParse
12e70 2d 3e 70 56 64 62 65 3b 0a 20 20 61 73 73 65 72 ->pVdbe;. asser
12e80 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 61 64 64 t( v!=0 );. add
12e90 72 49 6e 69 74 20 3d 20 73 71 6c 69 74 65 33 43 rInit = sqlite3C
12ea0 6f 64 65 4f 6e 63 65 28 70 50 61 72 73 65 29 3b odeOnce(pParse);
12eb0 0a 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 .. /* Count the
12ec0 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d number of colum
12ed0 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ns that will be
12ee0 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 64 added to the ind
12ef0 65 78 0a 20 20 2a 2a 20 61 6e 64 20 75 73 65 64 ex. ** and used
12f00 20 74 6f 20 6d 61 74 63 68 20 57 48 45 52 45 20 to match WHERE
12f10 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e clause constrain
12f20 74 73 20 2a 2f 0a 20 20 6e 43 6f 6c 75 6d 6e 20 ts */. nColumn
12f30 3d 20 30 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 = 0;. pTable =
12f40 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20 70 57 pSrc->pTab;. pW
12f50 43 45 6e 64 20 3d 20 26 70 57 43 2d 3e 61 5b 70 CEnd = &pWC->a[p
12f60 57 43 2d 3e 6e 54 65 72 6d 5d 3b 0a 20 20 70 4c WC->nTerm];. pL
12f70 6f 6f 70 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 57 oop = pLevel->pW
12f80 4c 6f 6f 70 3b 0a 20 20 69 64 78 43 6f 6c 73 20 Loop;. idxCols
12f90 3d 20 30 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d = 0;. for(pTerm
12fa0 3d 70 57 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70 =pWC->a; pTerm<p
12fb0 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b WCEnd; pTerm++){
12fc0 0a 20 20 20 20 69 66 28 20 74 65 72 6d 43 61 6e . if( termCan
12fd0 44 72 69 76 65 49 6e 64 65 78 28 70 54 65 72 6d DriveIndex(pTerm
12fe0 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 , pSrc, notReady
12ff0 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 ) ){. int i
13000 43 6f 6c 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c Col = pTerm->u.l
13010 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 eftColumn;.
13020 20 42 69 74 6d 61 73 6b 20 63 4d 61 73 6b 20 3d Bitmask cMask =
13030 20 69 43 6f 6c 3e 3d 42 4d 53 20 3f 20 4d 41 53 iCol>=BMS ? MAS
13040 4b 42 49 54 28 42 4d 53 2d 31 29 20 3a 20 4d 41 KBIT(BMS-1) : MA
13050 53 4b 42 49 54 28 69 43 6f 6c 29 3b 0a 20 20 20 SKBIT(iCol);.
13060 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f testcase( iCo
13070 6c 3d 3d 42 4d 53 20 29 3b 0a 20 20 20 20 20 20 l==BMS );.
13080 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d testcase( iCol==
13090 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 20 20 69 BMS-1 );. i
130a0 66 28 20 21 73 65 6e 74 57 61 72 6e 69 6e 67 20 f( !sentWarning
130b0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
130c0 65 33 5f 6c 6f 67 28 53 51 4c 49 54 45 5f 57 41 e3_log(SQLITE_WA
130d0 52 4e 49 4e 47 5f 41 55 54 4f 49 4e 44 45 58 2c RNING_AUTOINDEX,
130e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 61 75 . "au
130f0 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 6f 6e tomatic index on
13100 20 25 73 28 25 73 29 22 2c 20 70 54 61 62 6c 65 %s(%s)", pTable
13110 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 ->zName,.
13120 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f pTable->aCo
13130 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3b 0a l[iCol].zName);.
13140 20 20 20 20 20 20 20 20 73 65 6e 74 57 61 72 6e sentWarn
13150 69 6e 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d ing = 1;. }
13160 0a 20 20 20 20 20 20 69 66 28 20 28 69 64 78 43 . if( (idxC
13170 6f 6c 73 20 26 20 63 4d 61 73 6b 29 3d 3d 30 20 ols & cMask)==0
13180 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 77 ){. if( w
13190 68 65 72 65 4c 6f 6f 70 52 65 73 69 7a 65 28 70 hereLoopResize(p
131a0 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 6f 6f 70 Parse->db, pLoop
131b0 2c 20 6e 43 6f 6c 75 6d 6e 2b 31 29 20 29 20 72 , nColumn+1) ) r
131c0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 70 eturn;. p
131d0 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d 5b 6e 43 6f Loop->aLTerm[nCo
131e0 6c 75 6d 6e 2b 2b 5d 20 3d 20 70 54 65 72 6d 3b lumn++] = pTerm;
131f0 0a 20 20 20 20 20 20 20 20 69 64 78 43 6f 6c 73 . idxCols
13200 20 7c 3d 20 63 4d 61 73 6b 3b 0a 20 20 20 20 20 |= cMask;.
13210 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 }. }. }. a
13220 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3e 30 ssert( nColumn>0
13230 20 29 3b 0a 20 20 70 4c 6f 6f 70 2d 3e 75 2e 62 );. pLoop->u.b
13240 74 72 65 65 2e 6e 45 71 20 3d 20 70 4c 6f 6f 70 tree.nEq = pLoop
13250 2d 3e 6e 4c 54 65 72 6d 20 3d 20 6e 43 6f 6c 75 ->nLTerm = nColu
13260 6d 6e 3b 0a 20 20 70 4c 6f 6f 70 2d 3e 77 73 46 mn;. pLoop->wsF
13270 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 43 4f 4c lags = WHERE_COL
13280 55 4d 4e 5f 45 51 20 7c 20 57 48 45 52 45 5f 49 UMN_EQ | WHERE_I
13290 44 58 5f 4f 4e 4c 59 20 7c 20 57 48 45 52 45 5f DX_ONLY | WHERE_
132a0 49 4e 44 45 58 45 44 0a 20 20 20 20 20 20 20 20 INDEXED.
132b0 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 57 | W
132c0 48 45 52 45 5f 41 55 54 4f 5f 49 4e 44 45 58 3b HERE_AUTO_INDEX;
132d0 0a 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65 .. /* Count the
132e0 20 6e 75 6d 62 65 72 20 6f 66 20 61 64 64 69 74 number of addit
132f0 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 73 20 6e 65 ional columns ne
13300 65 64 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 eded to create a
13310 0a 20 20 2a 2a 20 63 6f 76 65 72 69 6e 67 20 69 . ** covering i
13320 6e 64 65 78 2e 20 20 41 20 22 63 6f 76 65 72 69 ndex. A "coveri
13330 6e 67 20 69 6e 64 65 78 22 20 69 73 20 61 6e 20 ng index" is an
13340 69 6e 64 65 78 20 74 68 61 74 20 63 6f 6e 74 61 index that conta
13350 69 6e 73 20 61 6c 6c 0a 20 20 2a 2a 20 63 6f 6c ins all. ** col
13360 75 6d 6e 73 20 74 68 61 74 20 61 72 65 20 6e 65 umns that are ne
13370 65 64 65 64 20 62 79 20 74 68 65 20 71 75 65 72 eded by the quer
13380 79 2e 20 20 57 69 74 68 20 61 20 63 6f 76 65 72 y. With a cover
13390 69 6e 67 20 69 6e 64 65 78 2c 20 74 68 65 0a 20 ing index, the.
133a0 20 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 ** original tab
133b0 6c 65 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 le never needs t
133c0 6f 20 62 65 20 61 63 63 65 73 73 65 64 2e 20 20 o be accessed.
133d0 41 75 74 6f 6d 61 74 69 63 20 69 6e 64 69 63 65 Automatic indice
133e0 73 20 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 61 s must. ** be a
133f0 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 20 covering index
13400 62 65 63 61 75 73 65 20 74 68 65 20 69 6e 64 65 because the inde
13410 78 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 70 x will not be up
13420 64 61 74 65 64 20 69 66 20 74 68 65 0a 20 20 2a dated if the. *
13430 2a 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 * original table
13440 20 63 68 61 6e 67 65 73 20 61 6e 64 20 74 68 65 changes and the
13450 20 69 6e 64 65 78 20 61 6e 64 20 74 61 62 6c 65 index and table
13460 20 63 61 6e 6e 6f 74 20 62 6f 74 68 20 62 65 20 cannot both be
13470 75 73 65 64 0a 20 20 2a 2a 20 69 66 20 74 68 65 used. ** if the
13480 79 20 67 6f 20 6f 75 74 20 6f 66 20 73 79 6e 63 y go out of sync
13490 2e 0a 20 20 2a 2f 0a 20 20 65 78 74 72 61 43 6f .. */. extraCo
134a0 6c 73 20 3d 20 70 53 72 63 2d 3e 63 6f 6c 55 73 ls = pSrc->colUs
134b0 65 64 20 26 20 28 7e 69 64 78 43 6f 6c 73 20 7c ed & (~idxCols |
134c0 20 4d 41 53 4b 42 49 54 28 42 4d 53 2d 31 29 29 MASKBIT(BMS-1))
134d0 3b 0a 20 20 6d 78 42 69 74 43 6f 6c 20 3d 20 28 ;. mxBitCol = (
134e0 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3e 3d 20 pTable->nCol >=
134f0 42 4d 53 2d 31 29 20 3f 20 42 4d 53 2d 31 20 3a BMS-1) ? BMS-1 :
13500 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20 pTable->nCol;.
13510 20 74 65 73 74 63 61 73 65 28 20 70 54 61 62 6c testcase( pTabl
13520 65 2d 3e 6e 43 6f 6c 3d 3d 42 4d 53 2d 31 20 29 e->nCol==BMS-1 )
13530 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 54 ;. testcase( pT
13540 61 62 6c 65 2d 3e 6e 43 6f 6c 3d 3d 42 4d 53 2d able->nCol==BMS-
13550 32 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2 );. for(i=0;
13560 69 3c 6d 78 42 69 74 43 6f 6c 3b 20 69 2b 2b 29 i<mxBitCol; i++)
13570 7b 0a 20 20 20 20 69 66 28 20 65 78 74 72 61 43 {. if( extraC
13580 6f 6c 73 20 26 20 4d 41 53 4b 42 49 54 28 69 29 ols & MASKBIT(i)
13590 20 29 20 6e 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 ) nColumn++;.
135a0 7d 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 63 6f }. if( pSrc->co
135b0 6c 55 73 65 64 20 26 20 4d 41 53 4b 42 49 54 28 lUsed & MASKBIT(
135c0 42 4d 53 2d 31 29 20 29 7b 0a 20 20 20 20 6e 43 BMS-1) ){. nC
135d0 6f 6c 75 6d 6e 20 2b 3d 20 70 54 61 62 6c 65 2d olumn += pTable-
135e0 3e 6e 43 6f 6c 20 2d 20 42 4d 53 20 2b 20 31 3b >nCol - BMS + 1;
135f0 0a 20 20 7d 0a 20 20 70 4c 6f 6f 70 2d 3e 77 73 . }. pLoop->ws
13600 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 Flags |= WHERE_C
13610 4f 4c 55 4d 4e 5f 45 51 20 7c 20 57 48 45 52 45 OLUMN_EQ | WHERE
13620 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 0a 20 20 2f 2a _IDX_ONLY;.. /*
13630 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 49 Construct the I
13640 6e 64 65 78 20 6f 62 6a 65 63 74 20 74 6f 20 64 ndex object to d
13650 65 73 63 72 69 62 65 20 74 68 69 73 20 69 6e 64 escribe this ind
13660 65 78 20 2a 2f 0a 20 20 6e 42 79 74 65 20 3d 20 ex */. nByte =
13670 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 3b 0a 20 sizeof(Index);.
13680 20 6e 42 79 74 65 20 2b 3d 20 6e 43 6f 6c 75 6d nByte += nColum
13690 6e 2a 73 69 7a 65 6f 66 28 69 6e 74 29 3b 20 20 n*sizeof(int);
136a0 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 43 6f /* Index.aiCo
136b0 6c 75 6d 6e 20 2a 2f 0a 20 20 6e 42 79 74 65 20 lumn */. nByte
136c0 2b 3d 20 6e 43 6f 6c 75 6d 6e 2a 73 69 7a 65 6f += nColumn*sizeo
136d0 66 28 63 68 61 72 2a 29 3b 20 20 20 2f 2a 20 49 f(char*); /* I
136e0 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 2a 2f 0a 20 ndex.azColl */.
136f0 20 6e 42 79 74 65 20 2b 3d 20 6e 43 6f 6c 75 6d nByte += nColum
13700 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
13710 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 53 6f 72 /* Index.aSor
13720 74 4f 72 64 65 72 20 2a 2f 0a 20 20 70 49 64 78 tOrder */. pIdx
13730 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c = sqlite3DbMall
13740 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 ocZero(pParse->d
13750 62 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 b, nByte);. if(
13760 20 70 49 64 78 3d 3d 30 20 29 20 72 65 74 75 72 pIdx==0 ) retur
13770 6e 3b 0a 20 20 70 4c 6f 6f 70 2d 3e 75 2e 62 74 n;. pLoop->u.bt
13780 72 65 65 2e 70 49 6e 64 65 78 20 3d 20 70 49 64 ree.pIndex = pId
13790 78 3b 0a 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c x;. pIdx->azCol
137a0 6c 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 49 64 l = (char**)&pId
137b0 78 5b 31 5d 3b 0a 20 20 70 49 64 78 2d 3e 61 69 x[1];. pIdx->ai
137c0 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 2a 29 26 Column = (int*)&
137d0 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43 6f pIdx->azColl[nCo
137e0 6c 75 6d 6e 5d 3b 0a 20 20 70 49 64 78 2d 3e 61 lumn];. pIdx->a
137f0 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a SortOrder = (u8*
13800 29 26 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e )&pIdx->aiColumn
13810 5b 6e 43 6f 6c 75 6d 6e 5d 3b 0a 20 20 70 49 64 [nColumn];. pId
13820 78 2d 3e 7a 4e 61 6d 65 20 3d 20 22 61 75 74 6f x->zName = "auto
13830 2d 69 6e 64 65 78 22 3b 0a 20 20 70 49 64 78 2d -index";. pIdx-
13840 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f 6c 75 >nColumn = nColu
13850 6d 6e 3b 0a 20 20 70 49 64 78 2d 3e 70 54 61 62 mn;. pIdx->pTab
13860 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 6e le = pTable;. n
13870 20 3d 20 30 3b 0a 20 20 69 64 78 43 6f 6c 73 20 = 0;. idxCols
13880 3d 20 30 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d = 0;. for(pTerm
13890 3d 70 57 43 2d 3e 61 3b 20 70 54 65 72 6d 3c 70 =pWC->a; pTerm<p
138a0 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b WCEnd; pTerm++){
138b0 0a 20 20 20 20 69 66 28 20 74 65 72 6d 43 61 6e . if( termCan
138c0 44 72 69 76 65 49 6e 64 65 78 28 70 54 65 72 6d DriveIndex(pTerm
138d0 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 , pSrc, notReady
138e0 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 ) ){. int i
138f0 43 6f 6c 20 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c Col = pTerm->u.l
13900 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 eftColumn;.
13910 20 42 69 74 6d 61 73 6b 20 63 4d 61 73 6b 20 3d Bitmask cMask =
13920 20 69 43 6f 6c 3e 3d 42 4d 53 20 3f 20 4d 41 53 iCol>=BMS ? MAS
13930 4b 42 49 54 28 42 4d 53 2d 31 29 20 3a 20 4d 41 KBIT(BMS-1) : MA
13940 53 4b 42 49 54 28 69 43 6f 6c 29 3b 0a 20 20 20 SKBIT(iCol);.
13950 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f testcase( iCo
13960 6c 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 l==BMS-1 );.
13970 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c testcase( iCol
13980 3d 3d 42 4d 53 20 29 3b 0a 20 20 20 20 20 20 69 ==BMS );. i
13990 66 28 20 28 69 64 78 43 6f 6c 73 20 26 20 63 4d f( (idxCols & cM
139a0 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ask)==0 ){.
139b0 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54 Expr *pX = pT
139c0 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 erm->pExpr;.
139d0 20 20 20 20 69 64 78 43 6f 6c 73 20 7c 3d 20 63 idxCols |= c
139e0 4d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 70 49 Mask;. pI
139f0 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 5d 20 dx->aiColumn[n]
13a00 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 = pTerm->u.leftC
13a10 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 70 olumn;. p
13a20 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 Coll = sqlite3Bi
13a30 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 naryCompareCollS
13a40 65 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 eq(pParse, pX->p
13a50 4c 65 66 74 2c 20 70 58 2d 3e 70 52 69 67 68 74 Left, pX->pRight
13a60 29 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 2d );. pIdx-
13a70 3e 61 7a 43 6f 6c 6c 5b 6e 5d 20 3d 20 41 4c 57 >azColl[n] = ALW
13a80 41 59 53 28 70 43 6f 6c 6c 29 20 3f 20 70 43 6f AYS(pColl) ? pCo
13a90 6c 6c 2d 3e 7a 4e 61 6d 65 20 3a 20 22 42 49 4e ll->zName : "BIN
13aa0 41 52 59 22 3b 0a 20 20 20 20 20 20 20 20 6e 2b ARY";. n+
13ab0 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d +;. }. }
13ac0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 . }. assert( (
13ad0 75 33 32 29 6e 3d 3d 70 4c 6f 6f 70 2d 3e 75 2e u32)n==pLoop->u.
13ae0 62 74 72 65 65 2e 6e 45 71 20 29 3b 0a 0a 20 20 btree.nEq );..
13af0 2f 2a 20 41 64 64 20 61 64 64 69 74 69 6f 6e 61 /* Add additiona
13b00 6c 20 63 6f 6c 75 6d 6e 73 20 6e 65 65 64 65 64 l columns needed
13b10 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 61 75 74 to make the aut
13b20 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 69 6e 74 omatic index int
13b30 6f 0a 20 20 2a 2a 20 61 20 63 6f 76 65 72 69 6e o. ** a coverin
13b40 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 66 6f 72 g index */. for
13b50 28 69 3d 30 3b 20 69 3c 6d 78 42 69 74 43 6f 6c (i=0; i<mxBitCol
13b60 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 ; i++){. if(
13b70 65 78 74 72 61 43 6f 6c 73 20 26 20 4d 41 53 4b extraCols & MASK
13b80 42 49 54 28 69 29 20 29 7b 0a 20 20 20 20 20 20 BIT(i) ){.
13b90 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e pIdx->aiColumn[n
13ba0 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 70 49 64 ] = i;. pId
13bb0 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 5d 20 3d 20 22 x->azColl[n] = "
13bc0 42 49 4e 41 52 59 22 3b 0a 20 20 20 20 20 20 6e BINARY";. n
13bd0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ++;. }. }.
13be0 69 66 28 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 if( pSrc->colUse
13bf0 64 20 26 20 4d 41 53 4b 42 49 54 28 42 4d 53 2d d & MASKBIT(BMS-
13c00 31 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 1) ){. for(i=
13c10 42 4d 53 2d 31 3b 20 69 3c 70 54 61 62 6c 65 2d BMS-1; i<pTable-
13c20 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 >nCol; i++){.
13c30 20 20 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d pIdx->aiColum
13c40 6e 5b 6e 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 n[n] = i;.
13c50 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 5d 20 pIdx->azColl[n]
13c60 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 20 20 = "BINARY";.
13c70 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d n++;. }. }
13c80 0a 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 6e 43 . assert( n==nC
13c90 6f 6c 75 6d 6e 20 29 3b 0a 0a 20 20 2f 2a 20 43 olumn );.. /* C
13ca0 72 65 61 74 65 20 74 68 65 20 61 75 74 6f 6d 61 reate the automa
13cb0 74 69 63 20 69 6e 64 65 78 20 2a 2f 0a 20 20 70 tic index */. p
13cc0 4b 65 79 69 6e 66 6f 20 3d 20 73 71 6c 69 74 65 Keyinfo = sqlite
13cd0 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 3IndexKeyinfo(pP
13ce0 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 61 arse, pIdx);. a
13cf0 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e 69 ssert( pLevel->i
13d00 49 64 78 43 75 72 3e 3d 30 20 29 3b 0a 20 20 70 IdxCur>=0 );. p
13d10 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d Level->iIdxCur =
13d20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b pParse->nTab++;
13d30 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
13d40 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 41 dOp4(v, OP_OpenA
13d50 75 74 6f 69 6e 64 65 78 2c 20 70 4c 65 76 65 6c utoindex, pLevel
13d60 2d 3e 69 49 64 78 43 75 72 2c 20 6e 43 6f 6c 75 ->iIdxCur, nColu
13d70 6d 6e 2b 31 2c 20 30 2c 0a 20 20 20 20 20 20 20 mn+1, 0,.
13d80 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 (ch
13d90 61 72 2a 29 70 4b 65 79 69 6e 66 6f 2c 20 50 34 ar*)pKeyinfo, P4
13da0 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 _KEYINFO_HANDOFF
13db0 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 );. VdbeComment
13dc0 28 28 76 2c 20 22 66 6f 72 20 25 73 22 2c 20 70 ((v, "for %s", p
13dd0 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a Table->zName));.
13de0 0a 20 20 2f 2a 20 46 69 6c 6c 20 74 68 65 20 61 . /* Fill the a
13df0 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 77 utomatic index w
13e00 69 74 68 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 ith content */.
13e10 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 addrTop = sqlit
13e20 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
13e30 4f 50 5f 52 65 77 69 6e 64 2c 20 70 4c 65 76 65 OP_Rewind, pLeve
13e40 6c 2d 3e 69 54 61 62 43 75 72 29 3b 0a 20 20 72 l->iTabCur);. r
13e50 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 egRecord = sqlit
13e60 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
13e70 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 rse);. sqlite3G
13e80 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 enerateIndexKey(
13e90 70 50 61 72 73 65 2c 20 70 49 64 78 2c 20 70 4c pParse, pIdx, pL
13ea0 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 2c 20 72 evel->iTabCur, r
13eb0 65 67 52 65 63 6f 72 64 2c 20 31 2c 20 30 29 3b egRecord, 1, 0);
13ec0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
13ed0 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e dOp2(v, OP_IdxIn
13ee0 73 65 72 74 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 sert, pLevel->iI
13ef0 64 78 43 75 72 2c 20 72 65 67 52 65 63 6f 72 64 dxCur, regRecord
13f00 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
13f10 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c ChangeP5(v, OPFL
13f20 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 AG_USESEEKRESULT
13f30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
13f40 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 AddOp2(v, OP_Nex
13f50 74 2c 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 t, pLevel->iTabC
13f60 75 72 2c 20 61 64 64 72 54 6f 70 2b 31 29 3b 0a ur, addrTop+1);.
13f70 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
13f80 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f ngeP5(v, SQLITE_
13f90 53 54 4d 54 53 54 41 54 55 53 5f 41 55 54 4f 49 STMTSTATUS_AUTOI
13fa0 4e 44 45 58 29 3b 0a 20 20 73 71 6c 69 74 65 33 NDEX);. sqlite3
13fb0 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 VdbeJumpHere(v,
13fc0 61 64 64 72 54 6f 70 29 3b 0a 20 20 73 71 6c 69 addrTop);. sqli
13fd0 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 te3ReleaseTempRe
13fe0 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 g(pParse, regRec
13ff0 6f 72 64 29 3b 0a 20 20 0a 20 20 2f 2a 20 4a 75 ord);. . /* Ju
14000 6d 70 20 68 65 72 65 20 77 68 65 6e 20 73 6b 69 mp here when ski
14010 70 70 69 6e 67 20 74 68 65 20 69 6e 69 74 69 61 pping the initia
14020 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 lization */. sq
14030 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 lite3VdbeJumpHer
14040 65 28 76 2c 20 61 64 64 72 49 6e 69 74 29 3b 0a e(v, addrInit);.
14050 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 }.#endif /* SQLI
14060 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 4d 41 54 49 TE_OMIT_AUTOMATI
14070 43 5f 49 4e 44 45 58 20 2a 2f 0a 0a 23 69 66 6e C_INDEX */..#ifn
14080 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
14090 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a VIRTUALTABLE./*.
140a0 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 ** Allocate and
140b0 70 6f 70 75 6c 61 74 65 20 61 6e 20 73 71 6c 69 populate an sqli
140c0 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 te3_index_info s
140d0 74 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 tructure. It is
140e0 74 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e 73 69 the .** responsi
140f0 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63 61 bility of the ca
14100 6c 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c ller to eventual
14110 6c 79 20 72 65 6c 65 61 73 65 20 74 68 65 20 73 ly release the s
14120 74 72 75 63 74 75 72 65 0a 2a 2a 20 62 79 20 70 tructure.** by p
14130 61 73 73 69 6e 67 20 74 68 65 20 70 6f 69 6e 74 assing the point
14140 65 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 er returned by t
14150 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 his function to
14160 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e 0a sqlite3_free()..
14170 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 */.static sqlite
14180 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 61 6c 3_index_info *al
14190 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 locateIndexInfo(
141a0 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 . Parse *pParse
141b0 2c 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 ,. WhereClause
141c0 2a 70 57 43 2c 0a 20 20 73 74 72 75 63 74 20 53 *pWC,. struct S
141d0 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 rcList_item *pSr
141e0 63 2c 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 c,. ExprList *p
141f0 4f 72 64 65 72 42 79 0a 29 7b 0a 20 20 69 6e 74 OrderBy.){. int
14200 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 6e 54 65 i, j;. int nTe
14210 72 6d 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c rm;. struct sql
14220 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 ite3_index_const
14230 72 61 69 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b raint *pIdxCons;
14240 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 . struct sqlite
14250 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 3_index_orderby
14260 2a 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 *pIdxOrderBy;.
14270 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
14280 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f ndex_constraint_
14290 75 73 61 67 65 20 2a 70 55 73 61 67 65 3b 0a 20 usage *pUsage;.
142a0 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 WhereTerm *pTer
142b0 6d 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 m;. int nOrderB
142c0 79 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 y;. sqlite3_ind
142d0 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 ex_info *pIdxInf
142e0 6f 3b 0a 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74 o;.. /* Count t
142f0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73 he number of pos
14300 73 69 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75 sible WHERE clau
14310 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72 se constraints r
14320 65 66 65 72 72 69 6e 67 0a 20 20 2a 2a 20 74 6f eferring. ** to
14330 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61 this virtual ta
14340 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6e ble */. for(i=n
14350 54 65 72 6d 3d 30 2c 20 70 54 65 72 6d 3d 70 57 Term=0, pTerm=pW
14360 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 C->a; i<pWC->nTe
14370 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b rm; i++, pTerm++
14380 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d ){. if( pTerm
14390 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20 ->leftCursor !=
143a0 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 pSrc->iCursor )
143b0 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73 continue;. as
143c0 73 65 72 74 28 20 49 73 50 6f 77 65 72 4f 66 54 sert( IsPowerOfT
143d0 77 6f 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 wo(pTerm->eOpera
143e0 74 6f 72 20 26 20 7e 57 4f 5f 45 51 55 49 56 29 tor & ~WO_EQUIV)
143f0 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
14400 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 ( pTerm->eOperat
14410 6f 72 20 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 20 or & WO_IN );.
14420 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 testcase( pTer
14430 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 m->eOperator & W
14440 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 O_ISNULL );.
14450 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 if( pTerm->eOper
14460 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55 4c ator & (WO_ISNUL
14470 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 L) ) continue;.
14480 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 if( pTerm->wt
14490 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 4e 55 Flags & TERM_VNU
144a0 4c 4c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 LL ) continue;.
144b0 20 20 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 7d 0a nTerm++;. }.
144c0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 4f 52 44 . /* If the ORD
144d0 45 52 20 42 59 20 63 6c 61 75 73 65 20 63 6f 6e ER BY clause con
144e0 74 61 69 6e 73 20 6f 6e 6c 79 20 63 6f 6c 75 6d tains only colum
144f0 6e 73 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e ns in the curren
14500 74 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 t . ** virtual
14510 74 61 62 6c 65 20 74 68 65 6e 20 61 6c 6c 6f 63 table then alloc
14520 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 ate space for th
14530 65 20 61 4f 72 64 65 72 42 79 20 70 61 72 74 20 e aOrderBy part
14540 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71 6c 69 of. ** the sqli
14550 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 te3_index_info s
14560 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 tructure.. */.
14570 20 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 nOrderBy = 0;.
14580 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b if( pOrderBy ){
14590 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4f 72 . int n = pOr
145a0 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 derBy->nExpr;.
145b0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 for(i=0; i<n;
145c0 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 i++){. Expr
145d0 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65 72 *pExpr = pOrder
145e0 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a By->a[i].pExpr;.
145f0 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d if( pExpr-
14600 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c >op!=TK_COLUMN |
14610 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 | pExpr->iTable!
14620 3d 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 =pSrc->iCursor )
14630 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 break;. }.
14640 20 20 69 66 28 20 69 3d 3d 6e 29 7b 0a 20 20 20 if( i==n){.
14650 20 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 6e 3b nOrderBy = n;
14660 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
14670 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71 Allocate the sq
14680 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f lite3_index_info
14690 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2f 0a structure. */.
146a0 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 73 71 6c pIdxInfo = sql
146b0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f ite3DbMallocZero
146c0 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a (pParse->db, siz
146d0 65 6f 66 28 2a 70 49 64 78 49 6e 66 6f 29 0a 20 eof(*pIdxInfo).
146e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
146f0 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 69 7a + (siz
14700 65 6f 66 28 2a 70 49 64 78 43 6f 6e 73 29 20 2b eof(*pIdxCons) +
14710 20 73 69 7a 65 6f 66 28 2a 70 55 73 61 67 65 29 sizeof(*pUsage)
14720 29 2a 6e 54 65 72 6d 0a 20 20 20 20 20 20 20 20 )*nTerm.
14730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14740 20 20 20 2b 20 73 69 7a 65 6f 66 28 2a 70 49 64 + sizeof(*pId
14750 78 4f 72 64 65 72 42 79 29 2a 6e 4f 72 64 65 72 xOrderBy)*nOrder
14760 42 79 20 29 3b 0a 20 20 69 66 28 20 70 49 64 78 By );. if( pIdx
14770 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 73 Info==0 ){. s
14780 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
14790 50 61 72 73 65 2c 20 22 6f 75 74 20 6f 66 20 6d Parse, "out of m
147a0 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 72 65 74 emory");. ret
147b0 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a urn 0;. }.. /*
147c0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 Initialize the
147d0 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 structure. The
147e0 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e sqlite3_index_in
147f0 66 6f 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e fo structure con
14800 74 61 69 6e 73 0a 20 20 2a 2a 20 6d 61 6e 79 20 tains. ** many
14810 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 20 fields that are
14820 64 65 63 6c 61 72 65 64 20 22 63 6f 6e 73 74 22 declared "const"
14830 20 74 6f 20 70 72 65 76 65 6e 74 20 78 42 65 73 to prevent xBes
14840 74 49 6e 64 65 78 20 66 72 6f 6d 0a 20 20 2a 2a tIndex from. **
14850 20 63 68 61 6e 67 69 6e 67 20 74 68 65 6d 2e 20 changing them.
14860 20 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73 We have to do s
14870 6f 6d 65 20 66 75 6e 6b 79 20 63 61 73 74 69 6e ome funky castin
14880 67 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 20 20 g in order to.
14890 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 ** initialize th
148a0 6f 73 65 20 66 69 65 6c 64 73 2e 0a 20 20 2a 2f ose fields.. */
148b0 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 28 73 . pIdxCons = (s
148c0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
148d0 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 29 dex_constraint*)
148e0 26 70 49 64 78 49 6e 66 6f 5b 31 5d 3b 0a 20 20 &pIdxInfo[1];.
148f0 70 49 64 78 4f 72 64 65 72 42 79 20 3d 20 28 73 pIdxOrderBy = (s
14900 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
14910 64 65 78 5f 6f 72 64 65 72 62 79 2a 29 26 70 49 dex_orderby*)&pI
14920 64 78 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b 0a 20 dxCons[nTerm];.
14930 20 70 55 73 61 67 65 20 3d 20 28 73 74 72 75 63 pUsage = (struc
14940 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f t sqlite3_index_
14950 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 constraint_usage
14960 2a 29 26 70 49 64 78 4f 72 64 65 72 42 79 5b 6e *)&pIdxOrderBy[n
14970 4f 72 64 65 72 42 79 5d 3b 0a 20 20 2a 28 69 6e OrderBy];. *(in
14980 74 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 t*)&pIdxInfo->nC
14990 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 54 65 72 onstraint = nTer
149a0 6d 3b 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 m;. *(int*)&pId
149b0 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 xInfo->nOrderBy
149c0 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 2a 28 = nOrderBy;. *(
149d0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 struct sqlite3_i
149e0 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a ndex_constraint*
149f0 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f *)&pIdxInfo->aCo
14a00 6e 73 74 72 61 69 6e 74 20 3d 20 70 49 64 78 43 nstraint = pIdxC
14a10 6f 6e 73 3b 0a 20 20 2a 28 73 74 72 75 63 74 20 ons;. *(struct
14a20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 sqlite3_index_or
14a30 64 65 72 62 79 2a 2a 29 26 70 49 64 78 49 6e 66 derby**)&pIdxInf
14a40 6f 2d 3e 61 4f 72 64 65 72 42 79 20 3d 20 70 49 o->aOrderBy = pI
14a50 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 2a 28 73 dxOrderBy;. *(s
14a60 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e truct sqlite3_in
14a70 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 dex_constraint_u
14a80 73 61 67 65 2a 2a 29 26 70 49 64 78 49 6e 66 6f sage**)&pIdxInfo
14a90 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 ->aConstraintUsa
14aa0 67 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 ge =.
14ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14ae0 20 20 20 20 20 20 20 20 70 55 73 61 67 65 3b 0a pUsage;.
14af0 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 . for(i=j=0, pT
14b00 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 erm=pWC->a; i<pW
14b10 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 C->nTerm; i++, p
14b20 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 75 38 20 Term++){. u8
14b30 6f 70 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 op;. if( pTer
14b40 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d m->leftCursor !=
14b50 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 pSrc->iCursor )
14b60 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 continue;. a
14b70 73 73 65 72 74 28 20 49 73 50 6f 77 65 72 4f 66 ssert( IsPowerOf
14b80 54 77 6f 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 Two(pTerm->eOper
14b90 61 74 6f 72 20 26 20 7e 57 4f 5f 45 51 55 49 56 ator & ~WO_EQUIV
14ba0 29 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 ) );. testcas
14bb0 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 e( pTerm->eOpera
14bc0 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 tor & WO_IN );.
14bd0 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 testcase( pTe
14be0 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 rm->eOperator &
14bf0 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 WO_ISNULL );.
14c00 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 if( pTerm->eOpe
14c10 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55 rator & (WO_ISNU
14c20 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a LL) ) continue;.
14c30 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 77 if( pTerm->w
14c40 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 4e tFlags & TERM_VN
14c50 55 4c 4c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a ULL ) continue;.
14c60 20 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e pIdxCons[j].
14c70 69 43 6f 6c 75 6d 6e 20 3d 20 70 54 65 72 6d 2d iColumn = pTerm-
14c80 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 >u.leftColumn;.
14c90 20 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 pIdxCons[j].i
14ca0 54 65 72 6d 4f 66 66 73 65 74 20 3d 20 69 3b 0a TermOffset = i;.
14cb0 20 20 20 20 6f 70 20 3d 20 28 75 38 29 70 54 65 op = (u8)pTe
14cc0 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 rm->eOperator &
14cd0 57 4f 5f 41 4c 4c 3b 0a 20 20 20 20 69 66 28 20 WO_ALL;. if(
14ce0 6f 70 3d 3d 57 4f 5f 49 4e 20 29 20 6f 70 20 3d op==WO_IN ) op =
14cf0 20 57 4f 5f 45 51 3b 0a 20 20 20 20 70 49 64 78 WO_EQ;. pIdx
14d00 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20 6f 70 3b Cons[j].op = op;
14d10 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69 72 65 . /* The dire
14d20 63 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 69 6e ct assignment in
14d30 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6c 69 the previous li
14d40 6e 65 20 69 73 20 70 6f 73 73 69 62 6c 65 20 6f ne is possible o
14d50 6e 6c 79 20 62 65 63 61 75 73 65 0a 20 20 20 20 nly because.
14d60 2a 2a 20 74 68 65 20 57 4f 5f 20 61 6e 64 20 53 ** the WO_ and S
14d70 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 QLITE_INDEX_CONS
14d80 54 52 41 49 4e 54 5f 20 63 6f 64 65 73 20 61 72 TRAINT_ codes ar
14d90 65 20 69 64 65 6e 74 69 63 61 6c 2e 20 20 54 68 e identical. Th
14da0 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 e. ** followi
14db0 6e 67 20 61 73 73 65 72 74 73 20 76 65 72 69 66 ng asserts verif
14dc0 79 20 74 68 69 73 20 66 61 63 74 2e 20 2a 2f 0a y this fact. */.
14dd0 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 45 assert( WO_E
14de0 51 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f Q==SQLITE_INDEX_
14df0 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20 29 3b CONSTRAINT_EQ );
14e00 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f . assert( WO_
14e10 4c 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 LT==SQLITE_INDEX
14e20 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 29 _CONSTRAINT_LT )
14e30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f ;. assert( WO
14e40 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 _LE==SQLITE_INDE
14e50 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20 X_CONSTRAINT_LE
14e60 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 );. assert( W
14e70 4f 5f 47 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 O_GT==SQLITE_IND
14e80 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54 EX_CONSTRAINT_GT
14e90 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 );. assert(
14ea0 57 4f 5f 47 45 3d 3d 53 51 4c 49 54 45 5f 49 4e WO_GE==SQLITE_IN
14eb0 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 DEX_CONSTRAINT_G
14ec0 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 E );. assert(
14ed0 20 57 4f 5f 4d 41 54 43 48 3d 3d 53 51 4c 49 54 WO_MATCH==SQLIT
14ee0 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 E_INDEX_CONSTRAI
14ef0 4e 54 5f 4d 41 54 43 48 20 29 3b 0a 20 20 20 20 NT_MATCH );.
14f00 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 65 assert( pTerm->e
14f10 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 Operator & (WO_I
14f20 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57 4f N|WO_EQ|WO_LT|WO
14f30 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 7c _LE|WO_GT|WO_GE|
14f40 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20 20 20 WO_MATCH) );.
14f50 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 j++;. }. for(
14f60 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b i=0; i<nOrderBy;
14f70 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 i++){. Expr
14f80 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65 72 42 *pExpr = pOrderB
14f90 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 y->a[i].pExpr;.
14fa0 20 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 pIdxOrderBy[i
14fb0 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 ].iColumn = pExp
14fc0 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 r->iColumn;.
14fd0 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 64 pIdxOrderBy[i].d
14fe0 65 73 63 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e esc = pOrderBy->
14ff0 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a a[i].sortOrder;.
15000 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 49 }.. return pI
15010 64 78 49 6e 66 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a dxInfo;.}../*.**
15020 20 54 68 65 20 74 61 62 6c 65 20 6f 62 6a 65 63 The table objec
15030 74 20 72 65 66 65 72 65 6e 63 65 20 70 61 73 73 t reference pass
15040 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 ed as the second
15050 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 argument to thi
15060 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 s function.** mu
15070 73 74 20 72 65 70 72 65 73 65 6e 74 20 61 20 76 st represent a v
15080 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 irtual table. Th
15090 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f is function invo
150a0 6b 65 73 20 74 68 65 20 78 42 65 73 74 49 6e 64 kes the xBestInd
150b0 65 78 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f ex().** method o
150c0 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 f the virtual ta
150d0 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 71 6c ble with the sql
150e0 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 ite3_index_info
150f0 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20 63 object that.** c
15100 6f 6d 65 73 20 69 6e 20 61 73 20 74 68 65 20 33 omes in as the 3
15110 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 rd argument to t
15120 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a his function..**
15130 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 .** If an error
15140 6f 63 63 75 72 73 2c 20 70 50 61 72 73 65 20 69 occurs, pParse i
15150 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 s populated with
15160 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 an error messag
15170 65 20 61 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a e and a.** non-z
15180 65 72 6f 20 76 61 6c 75 65 20 69 73 20 72 65 74 ero value is ret
15190 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 urned. Otherwise
151a0 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64 20 , 0 is returned
151b0 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a 2a and the output.*
151c0 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 73 71 * part of the sq
151d0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f lite3_index_info
151e0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 6c 65 structure is le
151f0 66 74 20 70 6f 70 75 6c 61 74 65 64 2e 0a 2a 2a ft populated..**
15200 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e .** Whether or n
15210 6f 74 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 ot an error is r
15220 65 74 75 72 6e 65 64 2c 20 69 74 20 69 73 20 74 eturned, it is t
15230 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 he responsibilit
15240 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63 61 6c 6c y of the.** call
15250 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c 79 er to eventually
15260 20 66 72 65 65 20 70 2d 3e 69 64 78 53 74 72 20 free p->idxStr
15270 69 66 20 70 2d 3e 6e 65 65 64 54 6f 46 72 65 65 if p->needToFree
15280 49 64 78 53 74 72 20 69 6e 64 69 63 61 74 65 73 IdxStr indicates
15290 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20 69 73 .** that this is
152a0 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 required..*/.st
152b0 61 74 69 63 20 69 6e 74 20 76 74 61 62 42 65 73 atic int vtabBes
152c0 74 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 tIndex(Parse *pP
152d0 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 arse, Table *pTa
152e0 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 b, sqlite3_index
152f0 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 73 71 6c _info *p){. sql
15300 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 ite3_vtab *pVtab
15310 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 = sqlite3GetVTa
15320 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 ble(pParse->db,
15330 70 54 61 62 29 2d 3e 70 56 74 61 62 3b 0a 20 20 pTab)->pVtab;.
15340 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 3b int i;. int rc;
15350 0a 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 49 4e .. TRACE_IDX_IN
15360 50 55 54 53 28 70 29 3b 0a 20 20 72 63 20 3d 20 PUTS(p);. rc =
15370 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e pVtab->pModule->
15380 78 42 65 73 74 49 6e 64 65 78 28 70 56 74 61 62 xBestIndex(pVtab
15390 2c 20 70 29 3b 0a 20 20 54 52 41 43 45 5f 49 44 , p);. TRACE_ID
153a0 58 5f 4f 55 54 50 55 54 53 28 70 29 3b 0a 0a 20 X_OUTPUTS(p);..
153b0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f if( rc!=SQLITE_
153c0 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63 OK ){. if( rc
153d0 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 ==SQLITE_NOMEM )
153e0 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e {. pParse->
153f0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
15400 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 = 1;. }else
15410 69 66 28 20 21 70 56 74 61 62 2d 3e 7a 45 72 72 if( !pVtab->zErr
15420 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c Msg ){. sql
15430 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
15440 72 73 65 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 rse, "%s", sqlit
15450 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 e3ErrStr(rc));.
15460 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
15470 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 sqlite3ErrorMsg(
15480 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 70 56 pParse, "%s", pV
15490 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 tab->zErrMsg);.
154a0 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 }. }. sqlit
154b0 65 33 5f 66 72 65 65 28 70 56 74 61 62 2d 3e 7a e3_free(pVtab->z
154c0 45 72 72 4d 73 67 29 3b 0a 20 20 70 56 74 61 62 ErrMsg);. pVtab
154d0 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a ->zErrMsg = 0;..
154e0 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e for(i=0; i<p->
154f0 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b nConstraint; i++
15500 29 7b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 61 ){. if( !p->a
15510 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 Constraint[i].us
15520 61 62 6c 65 20 26 26 20 70 2d 3e 61 43 6f 6e 73 able && p->aCons
15530 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61 traintUsage[i].a
15540 72 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a 20 20 rgvIndex>0 ){.
15550 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
15560 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 Msg(pParse, .
15570 20 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 73 "table %s
15580 3a 20 78 42 65 73 74 49 6e 64 65 78 20 72 65 74 : xBestIndex ret
15590 75 72 6e 65 64 20 61 6e 20 69 6e 76 61 6c 69 64 urned an invalid
155a0 20 70 6c 61 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e plan", pTab->zN
155b0 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a ame);. }. }.
155c0 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 . return pParse
155d0 2d 3e 6e 45 72 72 3b 0a 7d 0a 23 65 6e 64 69 66 ->nErr;.}.#endif
155e0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c /* !defined(SQL
155f0 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c ITE_OMIT_VIRTUAL
15600 54 41 42 4c 45 29 20 2a 2f 0a 0a 0a 23 69 66 64 TABLE) */...#ifd
15610 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
15620 5f 53 54 41 54 33 5f 4f 52 5f 53 54 41 54 34 0a _STAT3_OR_STAT4.
15630 2f 2a 0a 2a 2a 20 45 73 74 69 6d 61 74 65 20 74 /*.** Estimate t
15640 68 65 20 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 he location of a
15650 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20 particular key
15660 61 6d 6f 6e 67 20 61 6c 6c 20 6b 65 79 73 20 69 among all keys i
15670 6e 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e 20 20 n an.** index.
15680 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 Store the result
15690 73 20 69 6e 20 61 53 74 61 74 20 61 73 20 66 6f s in aStat as fo
156a0 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 llows:.**.**
156b0 61 53 74 61 74 5b 30 5d 20 20 20 20 20 20 45 73 aStat[0] Es
156c0 74 2e 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 t. number of row
156d0 73 20 6c 65 73 73 20 74 68 61 6e 20 70 56 61 6c s less than pVal
156e0 0a 2a 2a 20 20 20 20 61 53 74 61 74 5b 31 5d 20 .** aStat[1]
156f0 20 20 20 20 20 45 73 74 2e 20 6e 75 6d 62 65 72 Est. number
15700 20 6f 66 20 72 6f 77 73 20 65 71 75 61 6c 20 74 of rows equal t
15710 6f 20 70 56 61 6c 0a 2a 2a 0a 2a 2a 20 52 65 74 o pVal.**.** Ret
15720 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e urn SQLITE_OK on
15730 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 success..*/.sta
15740 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 4b 65 tic void whereKe
15750 79 53 74 61 74 73 28 0a 20 20 50 61 72 73 65 20 yStats(. Parse
15760 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
15770 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 /* Databas
15780 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a e connection */.
15790 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 Index *pIdx,
157a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
157b0 20 49 6e 64 65 78 20 74 6f 20 63 6f 6e 73 69 64 Index to consid
157c0 65 72 20 64 6f 6d 61 69 6e 20 6f 66 20 2a 2f 0a er domain of */.
157d0 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 UnpackedRecord
157e0 20 2a 70 52 65 63 2c 20 20 20 20 20 20 20 2f 2a *pRec, /*
157f0 20 56 65 63 74 6f 72 20 6f 66 20 76 61 6c 75 65 Vector of value
15800 73 20 74 6f 20 63 6f 6e 73 69 64 65 72 20 2a 2f s to consider */
15810 0a 20 20 69 6e 74 20 72 6f 75 6e 64 55 70 2c 20 . int roundUp,
15820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
15830 2a 20 52 6f 75 6e 64 20 75 70 20 69 66 20 74 72 * Round up if tr
15840 75 65 2e 20 20 52 6f 75 6e 64 20 64 6f 77 6e 20 ue. Round down
15850 69 66 20 66 61 6c 73 65 20 2a 2f 0a 20 20 74 52 if false */. tR
15860 6f 77 63 6e 74 20 2a 61 53 74 61 74 20 20 20 20 owcnt *aStat
15870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 /* OUT
15880 3a 20 73 74 61 74 73 20 77 72 69 74 74 65 6e 20 : stats written
15890 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 here */.){. Ind
158a0 65 78 53 61 6d 70 6c 65 20 2a 61 53 61 6d 70 6c exSample *aSampl
158b0 65 20 3d 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c e = pIdx->aSampl
158c0 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 e;. int iCol;
158d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
158e0 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 65 71 /* Index of req
158f0 75 69 72 65 64 20 73 74 61 74 73 20 69 6e 20 61 uired stats in a
15900 6e 45 71 5b 5d 20 65 74 63 2e 20 2a 2f 0a 20 20 nEq[] etc. */.
15910 69 6e 74 20 69 4d 69 6e 20 3d 20 30 3b 20 20 20 int iMin = 0;
15920 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
15930 6d 61 6c 6c 65 73 74 20 73 61 6d 70 6c 65 20 6e mallest sample n
15940 6f 74 20 79 65 74 20 74 65 73 74 65 64 20 2a 2f ot yet tested */
15950 0a 20 20 69 6e 74 20 69 20 3d 20 70 49 64 78 2d . int i = pIdx-
15960 3e 6e 53 61 6d 70 6c 65 3b 20 20 20 20 20 20 2f >nSample; /
15970 2a 20 53 6d 61 6c 6c 65 73 74 20 73 61 6d 70 6c * Smallest sampl
15980 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 6f 72 e larger than or
15990 20 65 71 75 61 6c 20 74 6f 20 70 52 65 63 20 2a equal to pRec *
159a0 2f 0a 20 20 69 6e 74 20 69 54 65 73 74 3b 20 20 /. int iTest;
159b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
159c0 2f 2a 20 4e 65 78 74 20 73 61 6d 70 6c 65 20 74 /* Next sample t
159d0 6f 20 74 65 73 74 20 2a 2f 0a 20 20 69 6e 74 20 o test */. int
159e0 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 res;
159f0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c /* Resul
15a00 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 t of comparison
15a10 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a 23 69 operation */..#i
15a20 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 fndef SQLITE_DEB
15a30 55 47 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 UG. UNUSED_PARA
15a40 4d 45 54 45 52 28 20 70 50 61 72 73 65 20 29 3b METER( pParse );
15a50 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 .#endif. assert
15a60 28 20 70 52 65 63 21 3d 30 20 7c 7c 20 70 50 61 ( pRec!=0 || pPa
15a70 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 rse->db->mallocF
15a80 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70 ailed );. if( p
15a90 52 65 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b Rec==0 ) return;
15aa0 0a 20 20 69 43 6f 6c 20 3d 20 70 52 65 63 2d 3e . iCol = pRec->
15ab0 6e 46 69 65 6c 64 20 2d 20 31 3b 0a 20 20 61 73 nField - 1;. as
15ac0 73 65 72 74 28 20 70 49 64 78 2d 3e 6e 53 61 6d sert( pIdx->nSam
15ad0 70 6c 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 ple>0 );. asser
15ae0 74 28 20 70 52 65 63 2d 3e 6e 46 69 65 6c 64 3e t( pRec->nField>
15af0 30 20 26 26 20 69 43 6f 6c 3c 70 49 64 78 2d 3e 0 && iCol<pIdx->
15b00 6e 53 61 6d 70 6c 65 43 6f 6c 20 29 3b 0a 20 20 nSampleCol );.
15b10 64 6f 7b 0a 20 20 20 20 69 54 65 73 74 20 3d 20 do{. iTest =
15b20 28 69 4d 69 6e 2b 69 29 2f 32 3b 0a 20 20 20 20 (iMin+i)/2;.
15b30 72 65 73 20 3d 20 73 71 6c 69 74 65 33 56 64 62 res = sqlite3Vdb
15b40 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 61 eRecordCompare(a
15b50 53 61 6d 70 6c 65 5b 69 54 65 73 74 5d 2e 6e 2c Sample[iTest].n,
15b60 20 61 53 61 6d 70 6c 65 5b 69 54 65 73 74 5d 2e aSample[iTest].
15b70 70 2c 20 70 52 65 63 29 3b 0a 20 20 20 20 69 66 p, pRec);. if
15b80 28 20 72 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 ( res<0 ){.
15b90 20 69 4d 69 6e 20 3d 20 69 54 65 73 74 2b 31 3b iMin = iTest+1;
15ba0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
15bb0 20 20 69 20 3d 20 69 54 65 73 74 3b 0a 20 20 20 i = iTest;.
15bc0 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72 65 73 }. }while( res
15bd0 20 26 26 20 69 4d 69 6e 3c 69 20 29 3b 0a 0a 23 && iMin<i );..#
15be0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 ifdef SQLITE_DEB
15bf0 55 47 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c UG. /* The foll
15c00 6f 77 69 6e 67 20 61 73 73 65 72 74 20 73 74 61 owing assert sta
15c10 74 65 6d 65 6e 74 73 20 63 68 65 63 6b 20 74 68 tements check th
15c20 61 74 20 74 68 65 20 62 69 6e 61 72 79 20 73 65 at the binary se
15c30 61 72 63 68 20 63 6f 64 65 0a 20 20 2a 2a 20 61 arch code. ** a
15c40 62 6f 76 65 20 66 6f 75 6e 64 20 74 68 65 20 72 bove found the r
15c50 69 67 68 74 20 61 6e 73 77 65 72 2e 20 54 68 69 ight answer. Thi
15c60 73 20 62 6c 6f 63 6b 20 73 65 72 76 65 73 20 6e s block serves n
15c70 6f 20 70 75 72 70 6f 73 65 20 6f 74 68 65 72 0a o purpose other.
15c80 20 20 2a 2a 20 74 68 61 6e 20 74 6f 20 69 6e 76 ** than to inv
15c90 6f 6b 65 20 74 68 65 20 61 73 73 65 72 74 73 2e oke the asserts.
15ca0 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 3d 3d */. if( res==
15cb0 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 28 0 ){. /* If (
15cc0 72 65 73 3d 3d 30 29 20 69 73 20 74 72 75 65 2c res==0) is true,
15cd0 20 74 68 65 6e 20 73 61 6d 70 6c 65 20 24 69 20 then sample $i
15ce0 6d 75 73 74 20 62 65 20 65 71 75 61 6c 20 74 6f must be equal to
15cf0 20 70 52 65 63 20 2a 2f 0a 20 20 20 20 61 73 73 pRec */. ass
15d00 65 72 74 28 20 69 3c 70 49 64 78 2d 3e 6e 53 61 ert( i<pIdx->nSa
15d10 6d 70 6c 65 20 29 3b 0a 20 20 20 20 61 73 73 65 mple );. asse
15d20 72 74 28 20 30 3d 3d 73 71 6c 69 74 65 33 56 64 rt( 0==sqlite3Vd
15d30 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 beRecordCompare(
15d40 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e 2c 20 61 53 aSample[i].n, aS
15d50 61 6d 70 6c 65 5b 69 5d 2e 70 2c 20 70 52 65 63 ample[i].p, pRec
15d60 29 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 ). || pP
15d70 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 arse->db->malloc
15d80 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 65 6c 73 Failed );. }els
15d90 65 7b 0a 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 e{. /* Otherw
15da0 69 73 65 2c 20 70 52 65 63 20 6d 75 73 74 20 62 ise, pRec must b
15db0 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 73 e smaller than s
15dc0 61 6d 70 6c 65 20 24 69 20 61 6e 64 20 6c 61 72 ample $i and lar
15dd0 67 65 72 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 ger than. **
15de0 73 61 6d 70 6c 65 20 28 24 69 2d 31 29 2e 20 20 sample ($i-1).
15df0 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 */. assert( i
15e00 3d 3d 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 20 ==pIdx->nSample
15e10 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c . || sql
15e20 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f ite3VdbeRecordCo
15e30 6d 70 61 72 65 28 61 53 61 6d 70 6c 65 5b 69 5d mpare(aSample[i]
15e40 2e 6e 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 70 .n, aSample[i].p
15e50 2c 20 70 52 65 63 29 3e 30 0a 20 20 20 20 20 20 , pRec)>0.
15e60 20 20 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 || pParse->db
15e70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
15e80 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d ;. assert( i=
15e90 3d 30 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 73 =0. || s
15ea0 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 qlite3VdbeRecord
15eb0 43 6f 6d 70 61 72 65 28 61 53 61 6d 70 6c 65 5b Compare(aSample[
15ec0 69 2d 31 5d 2e 6e 2c 20 61 53 61 6d 70 6c 65 5b i-1].n, aSample[
15ed0 69 2d 31 5d 2e 70 2c 20 70 52 65 63 29 3c 30 0a i-1].p, pRec)<0.
15ee0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 50 61 72 || pPar
15ef0 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 se->db->mallocFa
15f00 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 iled );. }.#end
15f10 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49 if /* ifdef SQLI
15f20 54 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 20 20 2f TE_DEBUG */.. /
15f30 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c * At this point,
15f40 20 61 53 61 6d 70 6c 65 5b 69 5d 20 69 73 20 74 aSample[i] is t
15f50 68 65 20 66 69 72 73 74 20 73 61 6d 70 6c 65 20 he first sample
15f60 74 68 61 74 20 69 73 20 67 72 65 61 74 65 72 20 that is greater
15f70 74 68 61 6e 0a 20 20 2a 2a 20 6f 72 20 65 71 75 than. ** or equ
15f80 61 6c 20 74 6f 20 70 56 61 6c 2e 20 20 4f 72 20 al to pVal. Or
15f90 69 66 20 69 3d 3d 70 49 64 78 2d 3e 6e 53 61 6d if i==pIdx->nSam
15fa0 70 6c 65 2c 20 74 68 65 6e 20 61 6c 6c 20 73 61 ple, then all sa
15fb0 6d 70 6c 65 73 20 61 72 65 20 6c 65 73 73 0a 20 mples are less.
15fc0 20 2a 2a 20 74 68 61 6e 20 70 56 61 6c 2e 20 20 ** than pVal.
15fd0 49 66 20 61 53 61 6d 70 6c 65 5b 69 5d 3d 3d 70 If aSample[i]==p
15fe0 56 61 6c 2c 20 74 68 65 6e 20 72 65 73 3d 3d 30 Val, then res==0
15ff0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 .. */. if( res
16000 3d 3d 30 20 29 7b 0a 20 20 20 20 61 53 74 61 74 ==0 ){. aStat
16010 5b 30 5d 20 3d 20 61 53 61 6d 70 6c 65 5b 69 5d [0] = aSample[i]
16020 2e 61 6e 4c 74 5b 69 43 6f 6c 5d 3b 0a 20 20 20 .anLt[iCol];.
16030 20 61 53 74 61 74 5b 31 5d 20 3d 20 61 53 61 6d aStat[1] = aSam
16040 70 6c 65 5b 69 5d 2e 61 6e 45 71 5b 69 43 6f 6c ple[i].anEq[iCol
16050 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ];. }else{.
16060 74 52 6f 77 63 6e 74 20 69 4c 6f 77 65 72 2c 20 tRowcnt iLower,
16070 69 55 70 70 65 72 2c 20 69 47 61 70 3b 0a 20 20 iUpper, iGap;.
16080 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a 20 20 if( i==0 ){.
16090 20 20 20 20 69 4c 6f 77 65 72 20 3d 20 30 3b 0a iLower = 0;.
160a0 20 20 20 20 20 20 69 55 70 70 65 72 20 3d 20 61 iUpper = a
160b0 53 61 6d 70 6c 65 5b 30 5d 2e 61 6e 4c 74 5b 69 Sample[0].anLt[i
160c0 43 6f 6c 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b Col];. }else{
160d0 0a 20 20 20 20 20 20 69 55 70 70 65 72 20 3d 20 . iUpper =
160e0 69 3e 3d 70 49 64 78 2d 3e 6e 53 61 6d 70 6c 65 i>=pIdx->nSample
160f0 20 3f 20 70 49 64 78 2d 3e 61 69 52 6f 77 45 73 ? pIdx->aiRowEs
16100 74 5b 30 5d 20 3a 20 61 53 61 6d 70 6c 65 5b 69 t[0] : aSample[i
16110 5d 2e 61 6e 4c 74 5b 69 43 6f 6c 5d 3b 0a 20 20 ].anLt[iCol];.
16120 20 20 20 20 69 4c 6f 77 65 72 20 3d 20 61 53 61 iLower = aSa
16130 6d 70 6c 65 5b 69 2d 31 5d 2e 61 6e 45 71 5b 69 mple[i-1].anEq[i
16140 43 6f 6c 5d 20 2b 20 61 53 61 6d 70 6c 65 5b 69 Col] + aSample[i
16150 2d 31 5d 2e 61 6e 4c 74 5b 69 43 6f 6c 5d 3b 0a -1].anLt[iCol];.
16160 20 20 20 20 7d 0a 20 20 20 20 61 53 74 61 74 5b }. aStat[
16170 31 5d 20 3d 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 1] = (pIdx->nCol
16180 75 6d 6e 3e 69 43 6f 6c 20 3f 20 70 49 64 78 2d umn>iCol ? pIdx-
16190 3e 61 41 76 67 45 71 5b 69 43 6f 6c 5d 20 3a 20 >aAvgEq[iCol] :
161a0 31 29 3b 0a 20 20 20 20 69 66 28 20 69 4c 6f 77 1);. if( iLow
161b0 65 72 3e 3d 69 55 70 70 65 72 20 29 7b 0a 20 20 er>=iUpper ){.
161c0 20 20 20 20 69 47 61 70 20 3d 20 30 3b 0a 20 20 iGap = 0;.
161d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 }else{. i
161e0 47 61 70 20 3d 20 69 55 70 70 65 72 20 2d 20 69 Gap = iUpper - i
161f0 4c 6f 77 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 Lower;. }.
16200 20 69 66 28 20 72 6f 75 6e 64 55 70 20 29 7b 0a if( roundUp ){.
16210 20 20 20 20 20 20 69 47 61 70 20 3d 20 28 69 47 iGap = (iG
16220 61 70 2a 32 29 2f 33 3b 0a 20 20 20 20 7d 65 6c ap*2)/3;. }el
16230 73 65 7b 0a 20 20 20 20 20 20 69 47 61 70 20 3d se{. iGap =
16240 20 69 47 61 70 2f 33 3b 0a 20 20 20 20 7d 0a 20 iGap/3;. }.
16250 20 20 20 61 53 74 61 74 5b 30 5d 20 3d 20 69 4c aStat[0] = iL
16260 6f 77 65 72 20 2b 20 69 47 61 70 3b 0a 20 20 7d ower + iGap;. }
16270 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c .}.#endif /* SQL
16280 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
16290 5f 4f 52 5f 53 54 41 54 34 20 2a 2f 0a 0a 2f 2a _OR_STAT4 */../*
162a0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f .** This functio
162b0 6e 20 69 73 20 75 73 65 64 20 74 6f 20 65 73 74 n is used to est
162c0 69 6d 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 imate the number
162d0 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 of rows that wi
162e0 6c 6c 20 62 65 20 76 69 73 69 74 65 64 0a 2a 2a ll be visited.**
162f0 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 61 6e 20 by scanning an
16300 69 6e 64 65 78 20 66 6f 72 20 61 20 72 61 6e 67 index for a rang
16310 65 20 6f 66 20 76 61 6c 75 65 73 2e 20 54 68 65 e of values. The
16320 20 72 61 6e 67 65 20 6d 61 79 20 68 61 76 65 20 range may have
16330 61 6e 20 75 70 70 65 72 0a 2a 2a 20 62 6f 75 6e an upper.** boun
16340 64 2c 20 61 20 6c 6f 77 65 72 20 62 6f 75 6e 64 d, a lower bound
16350 2c 20 6f 72 20 62 6f 74 68 2e 20 54 68 65 20 57 , or both. The W
16360 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d HERE clause term
16370 73 20 74 68 61 74 20 73 65 74 20 74 68 65 20 75 s that set the u
16380 70 70 65 72 0a 2a 2a 20 61 6e 64 20 6c 6f 77 65 pper.** and lowe
16390 72 20 62 6f 75 6e 64 73 20 61 72 65 20 72 65 70 r bounds are rep
163a0 72 65 73 65 6e 74 65 64 20 62 79 20 70 4c 6f 77 resented by pLow
163b0 65 72 20 61 6e 64 20 70 55 70 70 65 72 20 72 65 er and pUpper re
163c0 73 70 65 63 74 69 76 65 6c 79 2e 20 46 6f 72 0a spectively. For.
163d0 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 ** example, assu
163e0 6d 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 ming that index
163f0 70 20 69 73 20 6f 6e 20 74 31 28 61 29 3a 0a 2a p is on t1(a):.*
16400 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 *.** ... FROM
16410 74 31 20 57 48 45 52 45 20 61 20 3e 20 3f 20 41 t1 WHERE a > ? A
16420 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 20 ND a < ? ....**
16430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16440 20 20 20 7c 5f 5f 5f 5f 5f 7c 20 20 20 7c 5f 5f |_____| |__
16450 5f 5f 5f 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 ___|.**
16460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 |
16470 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 |.**
16480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16490 20 70 4c 6f 77 65 72 20 20 20 20 70 55 70 70 65 pLower pUppe
164a0 72 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 r.**.** If eithe
164b0 72 20 6f 66 20 74 68 65 20 75 70 70 65 72 20 6f r of the upper o
164c0 72 20 6c 6f 77 65 72 20 62 6f 75 6e 64 20 69 73 r lower bound is
164d0 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20 74 68 not present, th
164e0 65 6e 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 en NULL is passe
164f0 64 20 69 6e 0a 2a 2a 20 70 6c 61 63 65 20 6f 66 d in.** place of
16500 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 the correspondi
16510 6e 67 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a ng WhereTerm..**
16520 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69 6e .** The value in
16530 20 28 70 42 75 69 6c 64 65 72 2d 3e 70 4e 65 77 (pBuilder->pNew
16540 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 29 20 69 ->u.btree.nEq) i
16550 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 s the index of t
16560 68 65 20 69 6e 64 65 78 0a 2a 2a 20 63 6f 6c 75 he index.** colu
16570 6d 6e 20 73 75 62 6a 65 63 74 20 74 6f 20 74 68 mn subject to th
16580 65 20 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 e range constrai
16590 6e 74 2e 20 4f 72 2c 20 65 71 75 69 76 61 6c 65 nt. Or, equivale
165a0 6e 74 6c 79 2c 20 74 68 65 20 6e 75 6d 62 65 72 ntly, the number
165b0 20 6f 66 0a 2a 2a 20 65 71 75 61 6c 69 74 79 20 of.** equality
165c0 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f 70 74 69 constraints opti
165d0 6d 69 7a 65 64 20 62 79 20 74 68 65 20 70 72 6f mized by the pro
165e0 70 6f 73 65 64 20 69 6e 64 65 78 20 73 63 61 6e posed index scan
165f0 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a . For example,.*
16600 2a 20 61 73 73 75 6d 69 6e 67 20 69 6e 64 65 78 * assuming index
16610 20 70 20 69 73 20 6f 6e 20 74 31 28 61 2c 20 62 p is on t1(a, b
16620 29 2c 20 61 6e 64 20 74 68 65 20 53 51 4c 20 71 ), and the SQL q
16630 75 65 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 uery is:.**.**
16640 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 ... FROM t1 WHE
16650 52 45 20 61 20 3d 20 3f 20 41 4e 44 20 62 20 3e RE a = ? AND b >
16660 20 3f 20 41 4e 44 20 62 20 3c 20 3f 20 2e 2e 2e ? AND b < ? ...
16670 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20 .**.** then nEq
16680 69 73 20 73 65 74 20 74 6f 20 31 20 28 61 73 20 is set to 1 (as
16690 74 68 65 20 72 61 6e 67 65 20 72 65 73 74 72 69 the range restri
166a0 63 74 65 64 20 63 6f 6c 75 6d 6e 2c 20 62 2c 20 cted column, b,
166b0 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 0a 2a is the second .*
166c0 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 * left-most colu
166d0 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 29 mn of the index)
166e0 2e 20 4f 72 2c 20 69 66 20 74 68 65 20 71 75 65 . Or, if the que
166f0 72 79 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e ry is:.**.** .
16700 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 .. FROM t1 WHERE
16710 20 61 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20 3f a > ? AND a < ?
16720 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 ....**.** then
16730 6e 45 71 20 69 73 20 73 65 74 20 74 6f 20 30 2e nEq is set to 0.
16740 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 .**.** When this
16750 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c function is cal
16760 6c 65 64 2c 20 2a 70 6e 4f 75 74 20 69 73 20 73 led, *pnOut is s
16770 65 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 et to the sqlite
16780 33 4c 6f 67 45 73 74 28 29 20 6f 66 20 74 68 65 3LogEst() of the
16790 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f .** number of ro
167a0 77 73 20 74 68 61 74 20 74 68 65 20 69 6e 64 65 ws that the inde
167b0 78 20 73 63 61 6e 20 69 73 20 65 78 70 65 63 74 x scan is expect
167c0 65 64 20 74 6f 20 76 69 73 69 74 20 77 69 74 68 ed to visit with
167d0 6f 75 74 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72 out .** consider
167e0 69 6e 67 20 74 68 65 20 72 61 6e 67 65 20 63 6f ing the range co
167f0 6e 73 74 72 61 69 6e 74 73 2e 20 49 66 20 6e 45 nstraints. If nE
16800 71 20 69 73 20 30 2c 20 74 68 69 73 20 69 73 20 q is 0, this is
16810 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 2a the number of .*
16820 2a 20 72 6f 77 73 20 69 6e 20 74 68 65 20 69 6e * rows in the in
16830 64 65 78 2e 20 41 73 73 75 6d 69 6e 67 20 6e 6f dex. Assuming no
16840 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a error occurs, *
16850 70 6e 4f 75 74 20 69 73 20 61 64 6a 75 73 74 65 pnOut is adjuste
16860 64 20 28 72 65 64 75 63 65 64 29 0a 2a 2a 20 74 d (reduced).** t
16870 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20 74 68 o account for th
16880 65 20 72 61 6e 67 65 20 63 6f 6e 74 72 61 69 6e e range contrain
16890 74 73 20 70 4c 6f 77 65 72 20 61 6e 64 20 70 55 ts pLower and pU
168a0 70 70 65 72 2e 0a 2a 2a 20 0a 2a 2a 20 49 6e 20 pper..** .** In
168b0 74 68 65 20 61 62 73 65 6e 63 65 20 6f 66 20 73 the absence of s
168c0 71 6c 69 74 65 5f 73 74 61 74 34 20 41 4e 41 4c qlite_stat4 ANAL
168d0 59 5a 45 20 64 61 74 61 2c 20 6f 72 20 69 66 20 YZE data, or if
168e0 73 75 63 68 20 64 61 74 61 20 63 61 6e 6e 6f 74 such data cannot
168f0 20 62 65 0a 2a 2a 20 75 73 65 64 2c 20 65 61 63 be.** used, eac
16900 68 20 72 61 6e 67 65 20 69 6e 65 71 75 61 6c 69 h range inequali
16910 74 79 20 72 65 64 75 63 65 73 20 74 68 65 20 73 ty reduces the s
16920 65 61 72 63 68 20 73 70 61 63 65 20 62 79 20 61 earch space by a
16930 20 66 61 63 74 6f 72 20 6f 66 20 34 2e 20 0a 2a factor of 4. .*
16940 2a 20 48 65 6e 63 65 20 61 20 70 61 69 72 20 6f * Hence a pair o
16950 66 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 78 f constraints (x
16960 3e 3f 20 41 4e 44 20 78 3c 3f 29 20 72 65 64 75 >? AND x<?) redu
16970 63 65 73 20 74 68 65 20 65 78 70 65 63 74 65 64 ces the expected
16980 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 72 6f number of.** ro
16990 77 73 20 76 69 73 69 74 65 64 20 62 79 20 61 20 ws visited by a
169a0 66 61 63 74 6f 72 20 6f 66 20 31 36 2e 0a 2a 2f factor of 16..*/
169b0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 .static int wher
169c0 65 52 61 6e 67 65 53 63 61 6e 45 73 74 28 0a 20 eRangeScanEst(.
169d0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
169e0 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 /* Parsing
169f0 20 26 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 & code generati
16a00 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
16a10 57 68 65 72 65 4c 6f 6f 70 42 75 69 6c 64 65 72 WhereLoopBuilder
16a20 20 2a 70 42 75 69 6c 64 65 72 2c 0a 20 20 57 68 *pBuilder,. Wh
16a30 65 72 65 54 65 72 6d 20 2a 70 4c 6f 77 65 72 2c ereTerm *pLower,
16a40 20 20 20 2f 2a 20 4c 6f 77 65 72 20 62 6f 75 6e /* Lower boun
16a50 64 20 6f 6e 20 74 68 65 20 72 61 6e 67 65 2e 20 d on the range.
16a60 65 78 3a 20 22 78 3e 31 32 33 22 20 4d 69 67 68 ex: "x>123" Migh
16a70 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 57 t be NULL */. W
16a80 68 65 72 65 54 65 72 6d 20 2a 70 55 70 70 65 72 hereTerm *pUpper
16a90 2c 20 20 20 2f 2a 20 55 70 70 65 72 20 62 6f 75 , /* Upper bou
16aa0 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e 67 65 2e nd on the range.
16ab0 20 65 78 3a 20 22 78 3c 34 35 35 22 20 4d 69 67 ex: "x<455" Mig
16ac0 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 ht be NULL */.
16ad0 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4c 6f 6f 70 WhereLoop *pLoop
16ae0 20 20 20 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 /* Modify t
16af0 68 65 20 2e 6e 4f 75 74 20 61 6e 64 20 6d 61 79 he .nOut and may
16b00 62 65 20 2e 72 52 75 6e 20 66 69 65 6c 64 73 20 be .rRun fields
16b10 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d */.){. int rc =
16b20 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e SQLITE_OK;. in
16b30 74 20 6e 4f 75 74 20 3d 20 70 4c 6f 6f 70 2d 3e t nOut = pLoop->
16b40 6e 4f 75 74 3b 0a 20 20 4c 6f 67 45 73 74 20 6e nOut;. LogEst n
16b50 4e 65 77 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c New;..#ifdef SQL
16b60 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
16b70 5f 4f 52 5f 53 54 41 54 34 0a 20 20 49 6e 64 65 _OR_STAT4. Inde
16b80 78 20 2a 70 20 3d 20 70 4c 6f 6f 70 2d 3e 75 2e x *p = pLoop->u.
16b90 62 74 72 65 65 2e 70 49 6e 64 65 78 3b 0a 20 20 btree.pIndex;.
16ba0 69 6e 74 20 6e 45 71 20 3d 20 70 4c 6f 6f 70 2d int nEq = pLoop-
16bb0 3e 75 2e 62 74 72 65 65 2e 6e 45 71 3b 0a 0a 20 >u.btree.nEq;..
16bc0 20 69 66 28 20 70 2d 3e 6e 53 61 6d 70 6c 65 3e if( p->nSample>
16bd0 30 0a 20 20 20 26 26 20 6e 45 71 3d 3d 70 42 75 0. && nEq==pBu
16be0 69 6c 64 65 72 2d 3e 6e 52 65 63 56 61 6c 69 64 ilder->nRecValid
16bf0 0a 20 20 20 26 26 20 6e 45 71 3c 70 2d 3e 6e 53 . && nEq<p->nS
16c00 61 6d 70 6c 65 43 6f 6c 0a 20 20 20 26 26 20 4f ampleCol. && O
16c10 70 74 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c ptimizationEnabl
16c20 65 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 53 ed(pParse->db, S
16c30 51 4c 49 54 45 5f 53 74 61 74 33 29 20 0a 20 20 QLITE_Stat3) .
16c40 29 7b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 ){. UnpackedR
16c50 65 63 6f 72 64 20 2a 70 52 65 63 20 3d 20 70 42 ecord *pRec = pB
16c60 75 69 6c 64 65 72 2d 3e 70 52 65 63 3b 0a 20 20 uilder->pRec;.
16c70 20 20 74 52 6f 77 63 6e 74 20 61 5b 32 5d 3b 0a tRowcnt a[2];.
16c80 20 20 20 20 75 38 20 61 66 66 3b 0a 0a 20 20 20 u8 aff;..
16c90 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 69 4c 6f /* Variable iLo
16ca0 77 65 72 20 77 69 6c 6c 20 62 65 20 73 65 74 20 wer will be set
16cb0 74 6f 20 74 68 65 20 65 73 74 69 6d 61 74 65 20 to the estimate
16cc0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 of the number of
16cd0 20 72 6f 77 73 20 69 6e 20 0a 20 20 20 20 2a 2a rows in . **
16ce0 20 74 68 65 20 69 6e 64 65 78 20 74 68 61 74 20 the index that
16cf0 61 72 65 20 6c 65 73 73 20 74 68 61 6e 20 74 68 are less than th
16d00 65 20 6c 6f 77 65 72 20 62 6f 75 6e 64 20 6f 66 e lower bound of
16d10 20 74 68 65 20 72 61 6e 67 65 20 71 75 65 72 79 the range query
16d20 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 6c 6f 77 . The. ** low
16d30 65 72 20 62 6f 75 6e 64 20 62 65 69 6e 67 20 74 er bound being t
16d40 68 65 20 63 6f 6e 63 61 74 65 6e 61 74 69 6f 6e he concatenation
16d50 20 6f 66 20 24 50 20 61 6e 64 20 24 4c 2c 20 77 of $P and $L, w
16d60 68 65 72 65 20 24 50 20 69 73 20 74 68 65 0a 20 here $P is the.
16d70 20 20 20 2a 2a 20 6b 65 79 2d 70 72 65 66 69 78 ** key-prefix
16d80 20 66 6f 72 6d 65 64 20 62 79 20 74 68 65 20 6e formed by the n
16d90 45 71 20 76 61 6c 75 65 73 20 6d 61 74 63 68 65 Eq values matche
16da0 64 20 61 67 61 69 6e 73 74 20 74 68 65 20 6e 45 d against the nE
16db0 71 20 6c 65 66 74 2d 6d 6f 73 74 0a 20 20 20 20 q left-most.
16dc0 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 ** columns of th
16dd0 65 20 69 6e 64 65 78 2c 20 61 6e 64 20 24 4c 20 e index, and $L
16de0 69 73 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 is the value in
16df0 70 4c 6f 77 65 72 2e 0a 20 20 20 20 2a 2a 0a 20 pLower.. **.
16e00 20 20 20 2a 2a 20 4f 72 2c 20 69 66 20 70 4c 6f ** Or, if pLo
16e10 77 65 72 20 69 73 20 4e 55 4c 4c 20 6f 72 20 24 wer is NULL or $
16e20 4c 20 63 61 6e 6e 6f 74 20 62 65 20 65 78 74 72 L cannot be extr
16e30 61 63 74 65 64 20 66 72 6f 6d 20 69 74 20 28 62 acted from it (b
16e40 65 63 61 75 73 65 20 69 74 0a 20 20 20 20 2a 2a ecause it. **
16e50 20 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 is not a simple
16e60 20 76 61 72 69 61 62 6c 65 20 6f 72 20 6c 69 74 variable or lit
16e70 65 72 61 6c 20 76 61 6c 75 65 29 2c 20 74 68 65 eral value), the
16e80 20 6c 6f 77 65 72 20 62 6f 75 6e 64 20 6f 66 20 lower bound of
16e90 74 68 65 0a 20 20 20 20 2a 2a 20 72 61 6e 67 65 the. ** range
16ea0 20 69 73 20 24 50 2e 20 44 75 65 20 74 6f 20 61 is $P. Due to a
16eb0 20 71 75 69 72 6b 20 69 6e 20 74 68 65 20 77 61 quirk in the wa
16ec0 79 20 77 68 65 72 65 4b 65 79 53 74 61 74 73 28 y whereKeyStats(
16ed0 29 20 77 6f 72 6b 73 2c 20 65 76 65 6e 0a 20 20 ) works, even.
16ee0 20 20 2a 2a 20 69 66 20 24 4c 20 69 73 20 61 76 ** if $L is av
16ef0 61 69 6c 61 62 6c 65 2c 20 77 68 65 72 65 4b 65 ailable, whereKe
16f00 79 53 74 61 74 73 28 29 20 69 73 20 63 61 6c 6c yStats() is call
16f10 65 64 20 66 6f 72 20 62 6f 74 68 20 28 24 50 29 ed for both ($P)
16f20 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 28 24 50 and . ** ($P
16f30 3a 24 4c 29 20 61 6e 64 20 74 68 65 20 6c 61 72 :$L) and the lar
16f40 67 65 72 20 6f 66 20 74 68 65 20 74 77 6f 20 72 ger of the two r
16f50 65 74 75 72 6e 65 64 20 76 61 6c 75 65 73 20 75 eturned values u
16f60 73 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 sed.. **.
16f70 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c 20 69 55 ** Similarly, iU
16f80 70 70 65 72 20 69 73 20 74 6f 20 62 65 20 73 65 pper is to be se
16f90 74 20 74 6f 20 74 68 65 20 65 73 74 69 6d 61 74 t to the estimat
16fa0 65 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 e of the number
16fb0 6f 66 20 72 6f 77 73 0a 20 20 20 20 2a 2a 20 6c of rows. ** l
16fc0 65 73 73 20 74 68 61 6e 20 74 68 65 20 75 70 70 ess than the upp
16fd0 65 72 20 62 6f 75 6e 64 20 6f 66 20 74 68 65 20 er bound of the
16fe0 72 61 6e 67 65 20 71 75 65 72 79 2e 20 57 68 65 range query. Whe
16ff0 72 65 20 74 68 65 20 75 70 70 65 72 20 62 6f 75 re the upper bou
17000 6e 64 0a 20 20 20 20 2a 2a 20 69 73 20 65 69 74 nd. ** is eit
17010 68 65 72 20 28 24 50 29 20 6f 72 20 28 24 50 3a her ($P) or ($P:
17020 24 55 29 2e 20 41 67 61 69 6e 2c 20 65 76 65 6e $U). Again, even
17030 20 69 66 20 24 55 20 69 73 20 61 76 61 69 6c 61 if $U is availa
17040 62 6c 65 2c 20 62 6f 74 68 20 76 61 6c 75 65 73 ble, both values
17050 0a 20 20 20 20 2a 2a 20 6f 66 20 69 55 70 70 65 . ** of iUppe
17060 72 20 61 72 65 20 72 65 71 75 65 73 74 65 64 20 r are requested
17070 6f 66 20 77 68 65 72 65 4b 65 79 53 74 61 74 73 of whereKeyStats
17080 28 29 20 61 6e 64 20 74 68 65 20 73 6d 61 6c 6c () and the small
17090 65 72 20 75 73 65 64 2e 0a 20 20 20 20 2a 2f 0a er used.. */.
170a0 20 20 20 20 74 52 6f 77 63 6e 74 20 69 4c 6f 77 tRowcnt iLow
170b0 65 72 3b 0a 20 20 20 20 74 52 6f 77 63 6e 74 20 er;. tRowcnt
170c0 69 55 70 70 65 72 3b 0a 0a 20 20 20 20 69 66 28 iUpper;.. if(
170d0 20 6e 45 71 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e nEq==p->nColumn
170e0 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 ){. aff =
170f0 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 SQLITE_AFF_INTEG
17100 45 52 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ER;. }else{.
17110 20 20 20 20 20 61 66 66 20 3d 20 70 2d 3e 70 54 aff = p->pT
17120 61 62 6c 65 2d 3e 61 43 6f 6c 5b 70 2d 3e 61 69 able->aCol[p->ai
17130 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 5d 2e 61 66 66 Column[nEq]].aff
17140 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 inity;. }.
17150 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 4c /* Determine iL
17160 6f 77 65 72 20 61 6e 64 20 69 55 70 70 65 72 20 ower and iUpper
17170 75 73 69 6e 67 20 28 24 50 29 20 6f 6e 6c 79 2e using ($P) only.
17180 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 45 71 3d */. if( nEq=
17190 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c 6f 77 =0 ){. iLow
171a0 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 55 er = 0;. iU
171b0 70 70 65 72 20 3d 20 70 2d 3e 61 69 52 6f 77 45 pper = p->aiRowE
171c0 73 74 5b 30 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 st[0];. }else
171d0 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 65 3a {. /* Note:
171e0 20 74 68 69 73 20 63 61 6c 6c 20 63 6f 75 6c 64 this call could
171f0 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 61 77 be optimized aw
17200 61 79 20 2d 20 73 69 6e 63 65 20 74 68 65 20 73 ay - since the s
17210 61 6d 65 20 76 61 6c 75 65 73 20 6d 75 73 74 20 ame values must
17220 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 . ** have b
17230 65 65 6e 20 72 65 71 75 65 73 74 65 64 20 77 68 een requested wh
17240 65 6e 20 74 65 73 74 69 6e 67 20 6b 65 79 20 24 en testing key $
17250 50 20 69 6e 20 77 68 65 72 65 45 71 75 61 6c 53 P in whereEqualS
17260 63 61 6e 45 73 74 28 29 2e 20 20 2a 2f 0a 20 20 canEst(). */.
17270 20 20 20 20 77 68 65 72 65 4b 65 79 53 74 61 74 whereKeyStat
17280 73 28 70 50 61 72 73 65 2c 20 70 2c 20 70 52 65 s(pParse, p, pRe
17290 63 2c 20 30 2c 20 61 29 3b 0a 20 20 20 20 20 20 c, 0, a);.
172a0 69 4c 6f 77 65 72 20 3d 20 61 5b 30 5d 3b 0a 20 iLower = a[0];.
172b0 20 20 20 20 20 69 55 70 70 65 72 20 3d 20 61 5b iUpper = a[
172c0 30 5d 20 2b 20 61 5b 31 5d 3b 0a 20 20 20 20 7d 0] + a[1];. }
172d0 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 6f 73 73 .. /* If poss
172e0 69 62 6c 65 2c 20 69 6d 70 72 6f 76 65 20 6f 6e ible, improve on
172f0 20 74 68 65 20 69 4c 6f 77 65 72 20 65 73 74 69 the iLower esti
17300 6d 61 74 65 20 75 73 69 6e 67 20 28 24 50 3a 24 mate using ($P:$
17310 4c 29 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 L). */. if( p
17320 4c 6f 77 65 72 20 29 7b 0a 20 20 20 20 20 20 69 Lower ){. i
17330 6e 74 20 62 4f 6b 3b 20 20 20 20 20 20 20 20 20 nt bOk;
17340 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 /* Tr
17350 75 65 20 69 66 20 76 61 6c 75 65 20 69 73 20 65 ue if value is e
17360 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 70 45 xtracted from pE
17370 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 xpr */. Exp
17380 72 20 2a 70 45 78 70 72 20 3d 20 70 4c 6f 77 65 r *pExpr = pLowe
17390 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 r->pExpr->pRight
173a0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
173b0 28 70 4c 6f 77 65 72 2d 3e 65 4f 70 65 72 61 74 (pLower->eOperat
173c0 6f 72 20 26 20 28 57 4f 5f 47 54 7c 57 4f 5f 47 or & (WO_GT|WO_G
173d0 45 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 E))!=0 );.
173e0 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 rc = sqlite3Stat
173f0 34 50 72 6f 62 65 53 65 74 56 61 6c 75 65 28 70 4ProbeSetValue(p
17400 50 61 72 73 65 2c 20 70 2c 20 26 70 52 65 63 2c Parse, p, &pRec,
17410 20 70 45 78 70 72 2c 20 61 66 66 2c 20 6e 45 71 pExpr, aff, nEq
17420 2c 20 26 62 4f 6b 29 3b 0a 20 20 20 20 20 20 69 , &bOk);. i
17430 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
17440 20 26 26 20 62 4f 6b 20 29 7b 0a 20 20 20 20 20 && bOk ){.
17450 20 20 20 74 52 6f 77 63 6e 74 20 69 4e 65 77 3b tRowcnt iNew;
17460 0a 20 20 20 20 20 20 20 20 77 68 65 72 65 4b 65 . whereKe
17470 79 53 74 61 74 73 28 70 50 61 72 73 65 2c 20 70 yStats(pParse, p
17480 2c 20 70 52 65 63 2c 20 30 2c 20 61 29 3b 0a 20 , pRec, 0, a);.
17490 20 20 20 20 20 20 20 69 4e 65 77 20 3d 20 61 5b iNew = a[
174a0 30 5d 20 2b 20 28 28 70 4c 6f 77 65 72 2d 3e 65 0] + ((pLower->e
174b0 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 54 Operator & WO_GT
174c0 29 20 3f 20 61 5b 31 5d 20 3a 20 30 29 3b 0a 20 ) ? a[1] : 0);.
174d0 20 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 3e if( iNew>
174e0 69 4c 6f 77 65 72 20 29 20 69 4c 6f 77 65 72 20 iLower ) iLower
174f0 3d 20 69 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 = iNew;.
17500 6e 4f 75 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a nOut--;. }.
17510 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 }.. /* If
17520 20 70 6f 73 73 69 62 6c 65 2c 20 69 6d 70 72 6f possible, impro
17530 76 65 20 6f 6e 20 74 68 65 20 69 55 70 70 65 72 ve on the iUpper
17540 20 65 73 74 69 6d 61 74 65 20 75 73 69 6e 67 20 estimate using
17550 28 24 50 3a 24 55 29 2e 20 2a 2f 0a 20 20 20 20 ($P:$U). */.
17560 69 66 28 20 70 55 70 70 65 72 20 29 7b 0a 20 20 if( pUpper ){.
17570 20 20 20 20 69 6e 74 20 62 4f 6b 3b 20 20 20 20 int bOk;
17580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
17590 2f 2a 20 54 72 75 65 20 69 66 20 76 61 6c 75 65 /* True if value
175a0 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72 is extracted fr
175b0 6f 6d 20 70 45 78 70 72 20 2a 2f 0a 20 20 20 20 om pExpr */.
175c0 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 Expr *pExpr =
175d0 70 55 70 70 65 72 2d 3e 70 45 78 70 72 2d 3e 70 pUpper->pExpr->p
175e0 52 69 67 68 74 3b 0a 20 20 20 20 20 20 61 73 73 Right;. ass
175f0 65 72 74 28 20 28 70 55 70 70 65 72 2d 3e 65 4f ert( (pUpper->eO
17600 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 4c 54 perator & (WO_LT
17610 7c 57 4f 5f 4c 45 29 29 21 3d 30 20 29 3b 0a 20 |WO_LE))!=0 );.
17620 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 rc = sqlite
17630 33 53 74 61 74 34 50 72 6f 62 65 53 65 74 56 61 3Stat4ProbeSetVa
17640 6c 75 65 28 70 50 61 72 73 65 2c 20 70 2c 20 26 lue(pParse, p, &
17650 70 52 65 63 2c 20 70 45 78 70 72 2c 20 61 66 66 pRec, pExpr, aff
17660 2c 20 6e 45 71 2c 20 26 62 4f 6b 29 3b 0a 20 20 , nEq, &bOk);.
17670 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 if( rc==SQLI
17680 54 45 5f 4f 4b 20 26 26 20 62 4f 6b 20 29 7b 0a TE_OK && bOk ){.
17690 20 20 20 20 20 20 20 20 74 52 6f 77 63 6e 74 20 tRowcnt
176a0 69 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 77 68 iNew;. wh
176b0 65 72 65 4b 65 79 53 74 61 74 73 28 70 50 61 72 ereKeyStats(pPar
176c0 73 65 2c 20 70 2c 20 70 52 65 63 2c 20 31 2c 20 se, p, pRec, 1,
176d0 61 29 3b 0a 20 20 20 20 20 20 20 20 69 4e 65 77 a);. iNew
176e0 20 3d 20 61 5b 30 5d 20 2b 20 28 28 70 55 70 70 = a[0] + ((pUpp
176f0 65 72 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 er->eOperator &
17700 57 4f 5f 4c 45 29 20 3f 20 61 5b 31 5d 20 3a 20 WO_LE) ? a[1] :
17710 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 0);. if(
17720 69 4e 65 77 3c 69 55 70 70 65 72 20 29 20 69 55 iNew<iUpper ) iU
17730 70 70 65 72 20 3d 20 69 4e 65 77 3b 0a 20 20 20 pper = iNew;.
17740 20 20 20 20 20 6e 4f 75 74 2d 2d 3b 0a 20 20 20 nOut--;.
17750 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
17760 70 42 75 69 6c 64 65 72 2d 3e 70 52 65 63 20 3d pBuilder->pRec =
17770 20 70 52 65 63 3b 0a 20 20 20 20 69 66 28 20 72 pRec;. if( r
17780 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a c==SQLITE_OK ){.
17790 20 20 20 20 20 20 69 66 28 20 69 55 70 70 65 72 if( iUpper
177a0 3e 69 4c 6f 77 65 72 20 29 7b 0a 20 20 20 20 20 >iLower ){.
177b0 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 nNew = sqlite
177c0 33 4c 6f 67 45 73 74 28 69 55 70 70 65 72 20 2d 3LogEst(iUpper -
177d0 20 69 4c 6f 77 65 72 29 3b 0a 20 20 20 20 20 20 iLower);.
177e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e }else{. n
177f0 4e 65 77 20 3d 20 31 30 3b 20 20 20 20 20 20 20 New = 10;
17800 20 61 73 73 65 72 74 28 20 31 30 3d 3d 73 71 6c assert( 10==sql
17810 69 74 65 33 4c 6f 67 45 73 74 28 32 29 20 29 3b ite3LogEst(2) );
17820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 . }. i
17830 66 28 20 6e 4e 65 77 3c 6e 4f 75 74 20 29 7b 0a f( nNew<nOut ){.
17840 20 20 20 20 20 20 20 20 6e 4f 75 74 20 3d 20 6e nOut = n
17850 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 New;. }.
17860 20 20 20 70 4c 6f 6f 70 2d 3e 6e 4f 75 74 20 3d pLoop->nOut =
17870 20 28 4c 6f 67 45 73 74 29 6e 4f 75 74 3b 0a 20 (LogEst)nOut;.
17880 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 WHERETRACE(
17890 30 78 31 30 2c 20 28 22 72 61 6e 67 65 20 73 63 0x10, ("range sc
178a0 61 6e 20 72 65 67 69 6f 6e 73 3a 20 25 75 2e 2e an regions: %u..
178b0 25 75 20 20 65 73 74 3d 25 64 5c 6e 22 2c 0a 20 %u est=%d\n",.
178c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
178d0 20 20 20 20 20 20 20 20 28 75 33 32 29 69 4c 6f (u32)iLo
178e0 77 65 72 2c 20 28 75 33 32 29 69 55 70 70 65 72 wer, (u32)iUpper
178f0 2c 20 6e 4f 75 74 29 29 3b 0a 20 20 20 20 20 20 , nOut));.
17900 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
17910 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 ;. }. }.#els
17920 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d e. UNUSED_PARAM
17930 45 54 45 52 28 70 50 61 72 73 65 29 3b 0a 20 20 ETER(pParse);.
17940 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 UNUSED_PARAMETER
17950 28 70 42 75 69 6c 64 65 72 29 3b 0a 23 65 6e 64 (pBuilder);.#end
17960 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 4c 6f if. assert( pLo
17970 77 65 72 20 7c 7c 20 70 55 70 70 65 72 20 29 3b wer || pUpper );
17980 0a 20 20 2f 2a 20 54 55 4e 49 4e 47 3a 20 20 45 . /* TUNING: E
17990 61 63 68 20 69 6e 65 71 75 61 6c 69 74 79 20 63 ach inequality c
179a0 6f 6e 73 74 72 61 69 6e 74 20 72 65 64 75 63 65 onstraint reduce
179b0 73 20 74 68 65 20 73 65 61 72 63 68 20 73 70 61 s the search spa
179c0 63 65 20 34 2d 66 6f 6c 64 2e 0a 20 20 2a 2a 20 ce 4-fold.. **
179d0 41 20 42 45 54 57 45 45 4e 20 6f 70 65 72 61 74 A BETWEEN operat
179e0 6f 72 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 72 or, therefore, r
179f0 65 64 75 63 65 73 20 74 68 65 20 73 65 61 72 63 educes the searc
17a00 68 20 73 70 61 63 65 20 31 36 2d 66 6f 6c 64 20 h space 16-fold
17a10 2a 2f 0a 20 20 6e 4e 65 77 20 3d 20 6e 4f 75 74 */. nNew = nOut
17a20 3b 0a 20 20 69 66 28 20 70 4c 6f 77 65 72 20 26 ;. if( pLower &
17a30 26 20 28 70 4c 6f 77 65 72 2d 3e 77 74 46 6c 61 & (pLower->wtFla
17a40 67 73 20 26 20 54 45 52 4d 5f 56 4e 55 4c 4c 29 gs & TERM_VNULL)
17a50 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 4e 65 77 20 ==0 ){. nNew
17a60 2d 3d 20 32 30 3b 20 20 20 20 20 20 20 20 61 73 -= 20; as
17a70 73 65 72 74 28 20 32 30 3d 3d 73 71 6c 69 74 65 sert( 20==sqlite
17a80 33 4c 6f 67 45 73 74 28 34 29 20 29 3b 0a 20 20 3LogEst(4) );.
17a90 20 20 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 0a 20 20 nOut--;. }.
17aa0 69 66 28 20 70 55 70 70 65 72 20 29 7b 0a 20 20 if( pUpper ){.
17ab0 20 20 6e 4e 65 77 20 2d 3d 20 32 30 3b 20 20 20 nNew -= 20;
17ac0 20 20 20 20 20 61 73 73 65 72 74 28 20 32 30 3d assert( 20=
17ad0 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 34 =sqlite3LogEst(4
17ae0 29 20 29 3b 0a 20 20 20 20 6e 4f 75 74 2d 2d 3b ) );. nOut--;
17af0 0a 20 20 7d 0a 20 20 69 66 28 20 6e 4e 65 77 3c . }. if( nNew<
17b00 31 30 20 29 20 6e 4e 65 77 20 3d 20 31 30 3b 0a 10 ) nNew = 10;.
17b10 20 20 69 66 28 20 6e 4e 65 77 3c 6e 4f 75 74 20 if( nNew<nOut
17b20 29 20 6e 4f 75 74 20 3d 20 6e 4e 65 77 3b 0a 20 ) nOut = nNew;.
17b30 20 70 4c 6f 6f 70 2d 3e 6e 4f 75 74 20 3d 20 28 pLoop->nOut = (
17b40 4c 6f 67 45 73 74 29 6e 4f 75 74 3b 0a 20 20 72 LogEst)nOut;. r
17b50 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 eturn rc;.}..#if
17b60 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
17b70 45 5f 53 54 41 54 33 5f 4f 52 5f 53 54 41 54 34 E_STAT3_OR_STAT4
17b80 0a 2f 2a 0a 2a 2a 20 45 73 74 69 6d 61 74 65 20 ./*.** Estimate
17b90 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f the number of ro
17ba0 77 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 ws that will be
17bb0 72 65 74 75 72 6e 65 64 20 62 61 73 65 64 20 6f returned based o
17bc0 6e 0a 2a 2a 20 61 6e 20 65 71 75 61 6c 69 74 79 n.** an equality
17bd0 20 63 6f 6e 73 74 72 61 69 6e 74 20 78 3d 56 41 constraint x=VA
17be0 4c 55 45 20 61 6e 64 20 77 68 65 72 65 20 74 68 LUE and where th
17bf0 61 74 20 56 41 4c 55 45 20 6f 63 63 75 72 73 20 at VALUE occurs
17c00 69 6e 0a 2a 2a 20 74 68 65 20 68 69 73 74 6f 67 in.** the histog
17c10 72 61 6d 20 64 61 74 61 2e 20 20 54 68 69 73 20 ram data. This
17c20 6f 6e 6c 79 20 77 6f 72 6b 73 20 77 68 65 6e 20 only works when
17c30 78 20 69 73 20 74 68 65 20 6c 65 66 74 2d 6d 6f x is the left-mo
17c40 73 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20 st.** column of
17c50 61 6e 20 69 6e 64 65 78 20 61 6e 64 20 73 71 6c an index and sql
17c60 69 74 65 5f 73 74 61 74 33 20 68 69 73 74 6f 67 ite_stat3 histog
17c70 72 61 6d 20 64 61 74 61 20 69 73 20 61 76 61 69 ram data is avai
17c80 6c 61 62 6c 65 0a 2a 2a 20 66 6f 72 20 74 68 61 lable.** for tha
17c90 74 20 69 6e 64 65 78 2e 20 20 57 68 65 6e 20 70 t index. When p
17ca0 45 78 70 72 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 Expr==NULL that
17cb0 6d 65 61 6e 73 20 74 68 65 20 63 6f 6e 73 74 72 means the constr
17cc0 61 69 6e 74 20 69 73 0a 2a 2a 20 22 78 20 49 53 aint is.** "x IS
17cd0 20 4e 55 4c 4c 22 20 69 6e 73 74 65 61 64 20 6f NULL" instead o
17ce0 66 20 22 78 3d 56 41 4c 55 45 22 2e 0a 2a 2a 0a f "x=VALUE"..**.
17cf0 2a 2a 20 57 72 69 74 65 20 74 68 65 20 65 73 74 ** Write the est
17d00 69 6d 61 74 65 64 20 72 6f 77 20 63 6f 75 6e 74 imated row count
17d10 20 69 6e 74 6f 20 2a 70 6e 52 6f 77 20 61 6e 64 into *pnRow and
17d20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
17d30 4b 2e 20 0a 2a 2a 20 49 66 20 75 6e 61 62 6c 65 K. .** If unable
17d40 20 74 6f 20 6d 61 6b 65 20 61 6e 20 65 73 74 69 to make an esti
17d50 6d 61 74 65 2c 20 6c 65 61 76 65 20 2a 70 6e 52 mate, leave *pnR
17d60 6f 77 20 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 ow unchanged and
17d70 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a return.** non-z
17d80 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 ero..**.** This
17d90 72 6f 75 74 69 6e 65 20 63 61 6e 20 66 61 69 6c routine can fail
17da0 20 69 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 if it is unable
17db0 20 74 6f 20 6c 6f 61 64 20 61 20 63 6f 6c 6c 61 to load a colla
17dc0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 0a 2a 2a ting sequence.**
17dd0 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73 74 required for st
17de0 72 69 6e 67 20 63 6f 6d 70 61 72 69 73 6f 6e 2c ring comparison,
17df0 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f or if unable to
17e00 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 allocate memory
17e10 0a 2a 2a 20 66 6f 72 20 61 20 55 54 46 20 63 6f .** for a UTF co
17e20 6e 76 65 72 73 69 6f 6e 20 72 65 71 75 69 72 65 nversion require
17e30 64 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e d for comparison
17e40 2e 20 20 54 68 65 20 65 72 72 6f 72 20 69 73 20 . The error is
17e50 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 74 68 65 stored.** in the
17e60 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72 pParse structur
17e70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 e..*/.static int
17e80 20 77 68 65 72 65 45 71 75 61 6c 53 63 61 6e 45 whereEqualScanE
17e90 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 st(. Parse *pPa
17ea0 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 rse, /* Pa
17eb0 72 73 69 6e 67 20 26 20 63 6f 64 65 20 67 65 6e rsing & code gen
17ec0 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 erating context
17ed0 2a 2f 0a 20 20 57 68 65 72 65 4c 6f 6f 70 42 75 */. WhereLoopBu
17ee0 69 6c 64 65 72 20 2a 70 42 75 69 6c 64 65 72 2c ilder *pBuilder,
17ef0 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 . Expr *pExpr,
17f00 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 /* Expre
17f10 73 73 69 6f 6e 20 66 6f 72 20 56 41 4c 55 45 20 ssion for VALUE
17f20 69 6e 20 74 68 65 20 78 3d 56 41 4c 55 45 20 63 in the x=VALUE c
17f30 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 74 onstraint */. t
17f40 52 6f 77 63 6e 74 20 2a 70 6e 52 6f 77 20 20 20 Rowcnt *pnRow
17f50 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 /* Write the
17f60 20 72 65 76 69 73 65 64 20 72 6f 77 20 65 73 74 revised row est
17f70 69 6d 61 74 65 20 68 65 72 65 20 2a 2f 0a 29 7b imate here */.){
17f80 0a 20 20 49 6e 64 65 78 20 2a 70 20 3d 20 70 42 . Index *p = pB
17f90 75 69 6c 64 65 72 2d 3e 70 4e 65 77 2d 3e 75 2e uilder->pNew->u.
17fa0 62 74 72 65 65 2e 70 49 6e 64 65 78 3b 0a 20 20 btree.pIndex;.
17fb0 69 6e 74 20 6e 45 71 20 3d 20 70 42 75 69 6c 64 int nEq = pBuild
17fc0 65 72 2d 3e 70 4e 65 77 2d 3e 75 2e 62 74 72 65 er->pNew->u.btre
17fd0 65 2e 6e 45 71 3b 0a 20 20 55 6e 70 61 63 6b 65 e.nEq;. Unpacke
17fe0 64 52 65 63 6f 72 64 20 2a 70 52 65 63 20 3d 20 dRecord *pRec =
17ff0 70 42 75 69 6c 64 65 72 2d 3e 70 52 65 63 3b 0a pBuilder->pRec;.
18000 20 20 75 38 20 61 66 66 3b 20 20 20 20 20 20 20 u8 aff;
18010 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 /* C
18020 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 2a olumn affinity *
18030 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 /. int rc;
18040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
18050 20 53 75 62 66 75 6e 63 74 69 6f 6e 20 72 65 74 Subfunction ret
18060 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 74 52 urn code */. tR
18070 6f 77 63 6e 74 20 61 5b 32 5d 3b 20 20 20 20 20 owcnt a[2];
18080 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 69 /* Stati
18090 73 74 69 63 73 20 2a 2f 0a 20 20 69 6e 74 20 62 stics */. int b
180a0 4f 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e Ok;.. assert( n
180b0 45 71 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 Eq>=1 );. asser
180c0 74 28 20 6e 45 71 3c 3d 28 70 2d 3e 6e 43 6f 6c t( nEq<=(p->nCol
180d0 75 6d 6e 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 umn+1) );. asse
180e0 72 74 28 20 70 2d 3e 61 53 61 6d 70 6c 65 21 3d rt( p->aSample!=
180f0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
18100 2d 3e 6e 53 61 6d 70 6c 65 3e 30 20 29 3b 0a 20 ->nSample>0 );.
18110 20 61 73 73 65 72 74 28 20 70 42 75 69 6c 64 65 assert( pBuilde
18120 72 2d 3e 6e 52 65 63 56 61 6c 69 64 3c 6e 45 71 r->nRecValid<nEq
18130 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 76 61 6c );.. /* If val
18140 75 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 ues are not avai
18150 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c 20 66 69 lable for all fi
18160 65 6c 64 73 20 6f 66 20 74 68 65 20 69 6e 64 65 elds of the inde
18170 78 20 74 6f 20 74 68 65 20 6c 65 66 74 0a 20 20 x to the left.
18180 2a 2a 20 6f 66 20 74 68 69 73 20 6f 6e 65 2c 20 ** of this one,
18190 6e 6f 20 65 73 74 69 6d 61 74 65 20 63 61 6e 20 no estimate can
181a0 62 65 20 6d 61 64 65 2e 20 52 65 74 75 72 6e 20 be made. Return
181b0 53 51 4c 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 2e SQLITE_NOTFOUND.
181c0 20 2a 2f 0a 20 20 69 66 28 20 70 42 75 69 6c 64 */. if( pBuild
181d0 65 72 2d 3e 6e 52 65 63 56 61 6c 69 64 3c 28 6e er->nRecValid<(n
181e0 45 71 2d 31 29 20 29 7b 0a 20 20 20 20 72 65 74 Eq-1) ){. ret
181f0 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 54 46 4f urn SQLITE_NOTFO
18200 55 4e 44 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 UND;. }.. /* T
18210 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 his is an optimi
18220 7a 61 74 69 6f 6e 20 6f 6e 6c 79 2e 20 54 68 65 zation only. The
18230 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 call to sqlite3
18240 53 74 61 74 34 50 72 6f 62 65 53 65 74 56 61 6c Stat4ProbeSetVal
18250 75 65 28 29 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 ue(). ** below
18260 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 would return the
18270 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20 2a 2f same value. */
18280 0a 20 20 69 66 28 20 6e 45 71 3e 70 2d 3e 6e 43 . if( nEq>p->nC
18290 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 2a 70 6e olumn ){. *pn
182a0 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 Row = 1;. ret
182b0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 urn SQLITE_OK;.
182c0 20 7d 0a 0a 20 20 61 66 66 20 3d 20 70 2d 3e 70 }.. aff = p->p
182d0 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 70 2d 3e 61 Table->aCol[p->a
182e0 69 43 6f 6c 75 6d 6e 5b 6e 45 71 2d 31 5d 5d 2e iColumn[nEq-1]].
182f0 61 66 66 69 6e 69 74 79 3b 0a 20 20 72 63 20 3d affinity;. rc =
18300 20 73 71 6c 69 74 65 33 53 74 61 74 34 50 72 6f sqlite3Stat4Pro
18310 62 65 53 65 74 56 61 6c 75 65 28 70 50 61 72 73 beSetValue(pPars
18320 65 2c 20 70 2c 20 26 70 52 65 63 2c 20 70 45 78 e, p, &pRec, pEx
18330 70 72 2c 20 61 66 66 2c 20 6e 45 71 2d 31 2c 20 pr, aff, nEq-1,
18340 26 62 4f 6b 29 3b 0a 20 20 70 42 75 69 6c 64 65 &bOk);. pBuilde
18350 72 2d 3e 70 52 65 63 20 3d 20 70 52 65 63 3b 0a r->pRec = pRec;.
18360 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 if( rc!=SQLITE
18370 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b _OK ) return rc;
18380 0a 20 20 69 66 28 20 62 4f 6b 3d 3d 30 20 29 20 . if( bOk==0 )
18390 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
183a0 54 46 4f 55 4e 44 3b 0a 20 20 70 42 75 69 6c 64 TFOUND;. pBuild
183b0 65 72 2d 3e 6e 52 65 63 56 61 6c 69 64 20 3d 20 er->nRecValid =
183c0 6e 45 71 3b 0a 0a 20 20 77 68 65 72 65 4b 65 79 nEq;.. whereKey
183d0 53 74 61 74 73 28 70 50 61 72 73 65 2c 20 70 2c Stats(pParse, p,
183e0 20 70 52 65 63 2c 20 30 2c 20 61 29 3b 0a 20 20 pRec, 0, a);.
183f0 57 48 45 52 45 54 52 41 43 45 28 30 78 31 30 2c WHERETRACE(0x10,
18400 28 22 65 71 75 61 6c 69 74 79 20 73 63 61 6e 20 ("equality scan
18410 72 65 67 69 6f 6e 73 3a 20 25 64 5c 6e 22 2c 20 regions: %d\n",
18420 28 69 6e 74 29 61 5b 31 5d 29 29 3b 0a 20 20 2a (int)a[1]));. *
18430 70 6e 52 6f 77 20 3d 20 61 5b 31 5d 3b 0a 20 20 pnRow = a[1];.
18440 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a . return rc;.}.
18450 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 #endif /* SQLITE
18460 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 5f 4f 52 _ENABLE_STAT3_OR
18470 5f 53 54 41 54 34 20 2a 2f 0a 0a 23 69 66 64 65 _STAT4 */..#ifde
18480 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
18490 53 54 41 54 33 5f 4f 52 5f 53 54 41 54 34 0a 2f STAT3_OR_STAT4./
184a0 2a 0a 2a 2a 20 45 73 74 69 6d 61 74 65 20 74 68 *.** Estimate th
184b0 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 e number of rows
184c0 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65 that will be re
184d0 74 75 72 6e 65 64 20 62 61 73 65 64 20 6f 6e 0a turned based on.
184e0 2a 2a 20 61 6e 20 49 4e 20 63 6f 6e 73 74 72 61 ** an IN constra
184f0 69 6e 74 20 77 68 65 72 65 20 74 68 65 20 72 69 int where the ri
18500 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 ght-hand side of
18510 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 the IN operator
18520 0a 2a 2a 20 69 73 20 61 20 6c 69 73 74 20 6f 66 .** is a list of
18530 20 76 61 6c 75 65 73 2e 20 20 45 78 61 6d 70 6c values. Exampl
18540 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 e:.**.**
18550 57 48 45 52 45 20 78 20 49 4e 20 28 31 2c 32 2c WHERE x IN (1,2,
18560 33 2c 34 29 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 3,4).**.** Write
18570 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 20 72 the estimated r
18580 6f 77 20 63 6f 75 6e 74 20 69 6e 74 6f 20 2a 70 ow count into *p
18590 6e 52 6f 77 20 61 6e 64 20 72 65 74 75 72 6e 20 nRow and return
185a0 53 51 4c 49 54 45 5f 4f 4b 2e 20 0a 2a 2a 20 49 SQLITE_OK. .** I
185b0 66 20 75 6e 61 62 6c 65 20 74 6f 20 6d 61 6b 65 f unable to make
185c0 20 61 6e 20 65 73 74 69 6d 61 74 65 2c 20 6c 65 an estimate, le
185d0 61 76 65 20 2a 70 6e 52 6f 77 20 75 6e 63 68 61 ave *pnRow uncha
185e0 6e 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e 0a nged and return.
185f0 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a ** non-zero..**.
18600 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
18610 63 61 6e 20 66 61 69 6c 20 69 66 20 69 74 20 69 can fail if it i
18620 73 20 75 6e 61 62 6c 65 20 74 6f 20 6c 6f 61 64 s unable to load
18630 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 a collating seq
18640 75 65 6e 63 65 0a 2a 2a 20 72 65 71 75 69 72 65 uence.** require
18650 64 20 66 6f 72 20 73 74 72 69 6e 67 20 63 6f 6d d for string com
18660 70 61 72 69 73 6f 6e 2c 20 6f 72 20 69 66 20 75 parison, or if u
18670 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 nable to allocat
18680 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 66 6f 72 20 e memory.** for
18690 61 20 55 54 46 20 63 6f 6e 76 65 72 73 69 6f 6e a UTF conversion
186a0 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 63 6f required for co
186b0 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68 65 20 65 mparison. The e
186c0 72 72 6f 72 20 69 73 20 73 74 6f 72 65 64 0a 2a rror is stored.*
186d0 2a 20 69 6e 20 74 68 65 20 70 50 61 72 73 65 20 * in the pParse
186e0 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 structure..*/.st
186f0 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 49 6e atic int whereIn
18700 53 63 61 6e 45 73 74 28 0a 20 20 50 61 72 73 65 ScanEst(. Parse
18710 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
18720 2f 2a 20 50 61 72 73 69 6e 67 20 26 20 63 6f 64 /* Parsing & cod
18730 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e e generating con
18740 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c text */. WhereL
18750 6f 6f 70 42 75 69 6c 64 65 72 20 2a 70 42 75 69 oopBuilder *pBui
18760 6c 64 65 72 2c 0a 20 20 45 78 70 72 4c 69 73 74 lder,. ExprList
18770 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 *pList, /*
18780 54 68 65 20 76 61 6c 75 65 20 6c 69 73 74 20 6f The value list o
18790 6e 20 74 68 65 20 52 48 53 20 6f 66 20 22 78 20 n the RHS of "x
187a0 49 4e 20 28 76 31 2c 76 32 2c 76 33 2c 2e 2e 2e IN (v1,v2,v3,...
187b0 29 22 20 2a 2f 0a 20 20 74 52 6f 77 63 6e 74 20 )" */. tRowcnt
187c0 2a 70 6e 52 6f 77 20 20 20 20 20 20 20 2f 2a 20 *pnRow /*
187d0 57 72 69 74 65 20 74 68 65 20 72 65 76 69 73 65 Write the revise
187e0 64 20 72 6f 77 20 65 73 74 69 6d 61 74 65 20 68 d row estimate h
187f0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 65 ere */.){. Inde
18800 78 20 2a 70 20 3d 20 70 42 75 69 6c 64 65 72 2d x *p = pBuilder-
18810 3e 70 4e 65 77 2d 3e 75 2e 62 74 72 65 65 2e 70 >pNew->u.btree.p
18820 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 6e 52 65 Index;. int nRe
18830 63 56 61 6c 69 64 20 3d 20 70 42 75 69 6c 64 65 cValid = pBuilde
18840 72 2d 3e 6e 52 65 63 56 61 6c 69 64 3b 0a 20 20 r->nRecValid;.
18850 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
18860 4f 4b 3b 20 20 20 20 20 2f 2a 20 53 75 62 66 75 OK; /* Subfu
18870 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 20 63 6f nction return co
18880 64 65 20 2a 2f 0a 20 20 74 52 6f 77 63 6e 74 20 de */. tRowcnt
18890 6e 45 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 nEst;
188a0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77 /* Number of row
188b0 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 74 s for a single t
188c0 65 72 6d 20 2a 2f 0a 20 20 74 52 6f 77 63 6e 74 erm */. tRowcnt
188d0 20 6e 52 6f 77 45 73 74 20 3d 20 30 3b 20 20 20 nRowEst = 0;
188e0 20 2f 2a 20 4e 65 77 20 65 73 74 69 6d 61 74 65 /* New estimate
188f0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f of the number o
18900 66 20 72 6f 77 73 20 2a 2f 0a 20 20 69 6e 74 20 f rows */. int
18910 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
18920 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
18930 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 ter */.. assert
18940 28 20 70 2d 3e 61 53 61 6d 70 6c 65 21 3d 30 20 ( p->aSample!=0
18950 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 );. for(i=0; rc
18960 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 ==SQLITE_OK && i
18970 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 <pList->nExpr; i
18980 2b 2b 29 7b 0a 20 20 20 20 6e 45 73 74 20 3d 20 ++){. nEst =
18990 70 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0a p->aiRowEst[0];.
189a0 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 45 71 rc = whereEq
189b0 75 61 6c 53 63 61 6e 45 73 74 28 70 50 61 72 73 ualScanEst(pPars
189c0 65 2c 20 70 42 75 69 6c 64 65 72 2c 20 70 4c 69 e, pBuilder, pLi
189d0 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 st->a[i].pExpr,
189e0 26 6e 45 73 74 29 3b 0a 20 20 20 20 6e 52 6f 77 &nEst);. nRow
189f0 45 73 74 20 2b 3d 20 6e 45 73 74 3b 0a 20 20 20 Est += nEst;.
18a00 20 70 42 75 69 6c 64 65 72 2d 3e 6e 52 65 63 56 pBuilder->nRecV
18a10 61 6c 69 64 20 3d 20 6e 52 65 63 56 61 6c 69 64 alid = nRecValid
18a20 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d ;. }.. if( rc=
18a30 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 =SQLITE_OK ){.
18a40 20 20 69 66 28 20 6e 52 6f 77 45 73 74 20 3e 20 if( nRowEst >
18a50 70 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 20 29 p->aiRowEst[0] )
18a60 20 6e 52 6f 77 45 73 74 20 3d 20 70 2d 3e 61 69 nRowEst = p->ai
18a70 52 6f 77 45 73 74 5b 30 5d 3b 0a 20 20 20 20 2a RowEst[0];. *
18a80 70 6e 52 6f 77 20 3d 20 6e 52 6f 77 45 73 74 3b pnRow = nRowEst;
18a90 0a 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 . WHERETRACE(
18aa0 30 78 31 30 2c 28 22 49 4e 20 72 6f 77 20 65 73 0x10,("IN row es
18ab0 74 69 6d 61 74 65 3a 20 65 73 74 3d 25 67 5c 6e timate: est=%g\n
18ac0 22 2c 20 6e 52 6f 77 45 73 74 29 29 3b 0a 20 20 ", nRowEst));.
18ad0 7d 0a 20 20 61 73 73 65 72 74 28 20 70 42 75 69 }. assert( pBui
18ae0 6c 64 65 72 2d 3e 6e 52 65 63 56 61 6c 69 64 3d lder->nRecValid=
18af0 3d 6e 52 65 63 56 61 6c 69 64 20 29 3b 0a 20 20 =nRecValid );.
18b00 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e return rc;.}.#en
18b10 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e dif /* SQLITE_EN
18b20 41 42 4c 45 5f 53 54 41 54 33 5f 4f 52 5f 53 54 ABLE_STAT3_OR_ST
18b30 41 54 34 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 69 AT4 */../*.** Di
18b40 73 61 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20 sable a term in
18b50 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
18b60 2e 20 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f . Except, do no
18b70 74 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65 t disable the te
18b80 72 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74 rm.** if it cont
18b90 72 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45 rols a LEFT OUTE
18ba0 52 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69 R JOIN and it di
18bb0 64 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20 d not originate
18bc0 69 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20 in the ON.** or
18bd0 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 USING clause of
18be0 74 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a that join..**.**
18bf0 20 43 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65 Consider the te
18c00 72 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 rm t2.z='ok' in
18c10 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 the following qu
18c20 65 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 eries:.**.** (
18c30 31 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 1) SELECT * FRO
18c40 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 M t1 LEFT JOIN t
18c50 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57 2 ON t1.a=t2.x W
18c60 48 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a HERE t2.z='ok'.*
18c70 2a 20 20 20 28 32 29 20 20 53 45 4c 45 43 54 20 * (2) SELECT
18c80 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a * FROM t1 LEFT J
18c90 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 OIN t2 ON t1.a=t
18ca0 32 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 2.x AND t2.z='ok
18cb0 27 0a 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45 '.** (3) SELE
18cc0 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 CT * FROM t1, t2
18cd0 20 57 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78 WHERE t1.a=t2.x
18ce0 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a AND t2.z='ok'.*
18cf0 2a 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f *.** The t2.z='o
18d00 6b 27 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 k' is disabled i
18d10 6e 20 74 68 65 20 69 6e 20 28 32 29 20 62 65 63 n the in (2) bec
18d20 61 75 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74 ause it originat
18d30 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 es.** in the ON
18d40 63 6c 61 75 73 65 2e 20 20 54 68 65 20 74 65 72 clause. The ter
18d50 6d 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e m is disabled in
18d60 20 28 33 29 20 62 65 63 61 75 73 65 20 69 74 20 (3) because it
18d70 69 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f is not part.** o
18d80 66 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a f a LEFT OUTER J
18d90 4f 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68 OIN. In (1), th
18da0 65 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69 e term is not di
18db0 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 sabled..**.** Di
18dc0 73 61 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63 sabling a term c
18dd0 61 75 73 65 73 20 74 68 61 74 20 74 65 72 6d 20 auses that term
18de0 74 6f 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64 to not be tested
18df0 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f in the inner lo
18e00 6f 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 op.** of the joi
18e10 6e 2e 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73 n. Disabling is
18e20 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e an optimization
18e30 2e 20 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72 . When terms ar
18e40 65 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 e satisfied.** b
18e50 79 20 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69 y indices, we di
18e60 73 61 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72 sable them to pr
18e70 65 76 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20 event redundant
18e80 74 65 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e tests in the inn
18e90 65 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 er.** loop. We
18ea0 77 6f 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f would get the co
18eb0 72 72 65 63 74 20 72 65 73 75 6c 74 73 20 69 66 rrect results if
18ec0 20 6e 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76 nothing were ev
18ed0 65 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20 er disabled,.**
18ee0 62 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20 but joins might
18ef0 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f run a little slo
18f00 77 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20 wer. The trick
18f10 69 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73 is to disable as
18f20 20 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63 much.** as we c
18f30 61 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61 62 an without disab
18f40 6c 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20 ling too much.
18f50 49 66 20 77 65 20 64 69 73 61 62 6c 65 64 20 69 If we disabled i
18f60 6e 20 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a n (1), we'd get.
18f70 2a 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73 ** the wrong ans
18f80 77 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74 wer. See ticket
18f90 20 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63 #813..*/.static
18fa0 20 76 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72 void disableTer
18fb0 6d 28 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c m(WhereLevel *pL
18fc0 65 76 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20 evel, WhereTerm
18fd0 2a 70 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 *pTerm){. if( p
18fe0 54 65 72 6d 0a 20 20 20 20 20 20 26 26 20 28 70 Term. && (p
18ff0 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 Term->wtFlags &
19000 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 0a 20 TERM_CODED)==0.
19010 20 20 20 20 20 26 26 20 28 70 4c 65 76 65 6c 2d && (pLevel-
19020 3e 69 4c 65 66 74 4a 6f 69 6e 3d 3d 30 20 7c 7c >iLeftJoin==0 ||
19030 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 ExprHasProperty
19040 28 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 45 (pTerm->pExpr, E
19050 50 5f 46 72 6f 6d 4a 6f 69 6e 29 29 0a 20 20 20 P_FromJoin)).
19060 20 20 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 6e && (pLevel->n
19070 6f 74 52 65 61 64 79 20 26 20 70 54 65 72 6d 2d otReady & pTerm-
19080 3e 70 72 65 72 65 71 41 6c 6c 29 3d 3d 30 0a 20 >prereqAll)==0.
19090 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 77 ){. pTerm->w
190a0 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 tFlags |= TERM_C
190b0 4f 44 45 44 3b 0a 20 20 20 20 69 66 28 20 70 54 ODED;. if( pT
190c0 65 72 6d 2d 3e 69 50 61 72 65 6e 74 3e 3d 30 20 erm->iParent>=0
190d0 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 ){. WhereTe
190e0 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 54 rm *pOther = &pT
190f0 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70 54 65 72 erm->pWC->a[pTer
19100 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 m->iParent];.
19110 20 20 20 69 66 28 20 28 2d 2d 70 4f 74 68 65 72 if( (--pOther
19120 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30 20 29 7b 0a ->nChild)==0 ){.
19130 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 disableT
19140 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 4f 74 68 erm(pLevel, pOth
19150 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 er);. }.
19160 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
19170 43 6f 64 65 20 61 6e 20 4f 50 5f 41 66 66 69 6e Code an OP_Affin
19180 69 74 79 20 6f 70 63 6f 64 65 20 74 6f 20 61 70 ity opcode to ap
19190 70 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 ply the column a
191a0 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 7a ffinity string z
191b0 41 66 66 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 20 Aff.** to the n
191c0 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 registers starti
191d0 6e 67 20 61 74 20 62 61 73 65 2e 20 0a 2a 2a 0a ng at base. .**.
191e0 2a 2a 20 41 73 20 61 6e 20 6f 70 74 69 6d 69 7a ** As an optimiz
191f0 61 74 69 6f 6e 2c 20 53 51 4c 49 54 45 5f 41 46 ation, SQLITE_AF
19200 46 5f 4e 4f 4e 45 20 65 6e 74 72 69 65 73 20 28 F_NONE entries (
19210 77 68 69 63 68 20 61 72 65 20 6e 6f 2d 6f 70 73 which are no-ops
19220 29 20 61 74 20 74 68 65 0a 2a 2a 20 62 65 67 69 ) at the.** begi
19230 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66 nning and end of
19240 20 7a 41 66 66 20 61 72 65 20 69 67 6e 6f 72 65 zAff are ignore
19250 64 2e 20 20 49 66 20 61 6c 6c 20 65 6e 74 72 69 d. If all entri
19260 65 73 20 69 6e 20 7a 41 66 66 20 61 72 65 0a 2a es in zAff are.*
19270 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e * SQLITE_AFF_NON
19280 45 2c 20 74 68 65 6e 20 6e 6f 20 63 6f 64 65 20 E, then no code
19290 67 65 74 73 20 67 65 6e 65 72 61 74 65 64 2e 0a gets generated..
192a0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
192b0 6e 65 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e ne makes its own
192c0 20 63 6f 70 79 20 6f 66 20 7a 41 66 66 20 73 6f copy of zAff so
192d0 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72 that the caller
192e0 20 69 73 20 66 72 65 65 0a 2a 2a 20 74 6f 20 6d is free.** to m
192f0 6f 64 69 66 79 20 7a 41 66 66 20 61 66 74 65 72 odify zAff after
19300 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
19310 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 turns..*/.static
19320 20 76 6f 69 64 20 63 6f 64 65 41 70 70 6c 79 41 void codeApplyA
19330 66 66 69 6e 69 74 79 28 50 61 72 73 65 20 2a 70 ffinity(Parse *p
19340 50 61 72 73 65 2c 20 69 6e 74 20 62 61 73 65 2c Parse, int base,
19350 20 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 41 int n, char *zA
19360 66 66 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d ff){. Vdbe *v =
19370 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
19380 20 20 69 66 28 20 7a 41 66 66 3d 3d 30 20 29 7b if( zAff==0 ){
19390 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 . assert( pPa
193a0 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 rse->db->mallocF
193b0 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 ailed );. ret
193c0 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 urn;. }. asser
193d0 74 28 20 76 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a t( v!=0 );.. /*
193e0 20 41 64 6a 75 73 74 20 62 61 73 65 20 61 6e 64 Adjust base and
193f0 20 6e 20 74 6f 20 73 6b 69 70 20 6f 76 65 72 20 n to skip over
19400 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 SQLITE_AFF_NONE
19410 65 6e 74 72 69 65 73 20 61 74 20 74 68 65 20 62 entries at the b
19420 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 61 6e eginning. ** an
19430 64 20 65 6e 64 20 6f 66 20 74 68 65 20 61 66 66 d end of the aff
19440 69 6e 69 74 79 20 73 74 72 69 6e 67 2e 0a 20 20 inity string..
19450 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 */. while( n>0
19460 26 26 20 7a 41 66 66 5b 30 5d 3d 3d 53 51 4c 49 && zAff[0]==SQLI
19470 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 TE_AFF_NONE ){.
19480 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 62 61 73 65 n--;. base
19490 2b 2b 3b 0a 20 20 20 20 7a 41 66 66 2b 2b 3b 0a ++;. zAff++;.
194a0 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31 }. while( n>1
194b0 20 26 26 20 7a 41 66 66 5b 6e 2d 31 5d 3d 3d 53 && zAff[n-1]==S
194c0 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 QLITE_AFF_NONE )
194d0 7b 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 7d 0a 0a {. n--;. }..
194e0 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 4f 50 /* Code the OP
194f0 5f 41 66 66 69 6e 69 74 79 20 6f 70 63 6f 64 65 _Affinity opcode
19500 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 if there is any
19510 74 68 69 6e 67 20 6c 65 66 74 20 74 6f 20 64 6f thing left to do
19520 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 3e 30 20 29 . */. if( n>0 )
19530 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
19540 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66 eAddOp2(v, OP_Af
19550 66 69 6e 69 74 79 2c 20 62 61 73 65 2c 20 6e 29 finity, base, n)
19560 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
19570 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c eChangeP4(v, -1,
19580 20 7a 41 66 66 2c 20 6e 29 3b 0a 20 20 20 20 73 zAff, n);. s
19590 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 qlite3ExprCacheA
195a0 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 ffinityChange(pP
195b0 61 72 73 65 2c 20 62 61 73 65 2c 20 6e 29 3b 0a arse, base, n);.
195c0 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 }.}.../*.** Ge
195d0 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 nerate code for
195e0 61 20 73 69 6e 67 6c 65 20 65 71 75 61 6c 69 74 a single equalit
195f0 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 y term of the WH
19600 45 52 45 20 63 6c 61 75 73 65 2e 20 20 41 6e 20 ERE clause. An
19610 65 71 75 61 6c 69 74 79 0a 2a 2a 20 74 65 72 6d equality.** term
19620 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 58 can be either X
19630 3d 65 78 70 72 20 6f 72 20 58 20 49 4e 20 28 2e =expr or X IN (.
19640 2e 2e 29 2e 20 20 20 70 54 65 72 6d 20 69 73 20 ..). pTerm is
19650 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 0a the term to be .
19660 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 ** coded..**.**
19670 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 The current valu
19680 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74 72 e for the constr
19690 61 69 6e 74 20 69 73 20 6c 65 66 74 20 69 6e 20 aint is left in
196a0 72 65 67 69 73 74 65 72 20 69 52 65 67 2e 0a 2a register iReg..*
196b0 2a 0a 2a 2a 20 46 6f 72 20 61 20 63 6f 6e 73 74 *.** For a const
196c0 72 61 69 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 raint of the for
196d0 6d 20 58 3d 65 78 70 72 2c 20 74 68 65 20 65 78 m X=expr, the ex
196e0 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c pression is eval
196f0 75 61 74 65 64 20 61 6e 64 20 69 74 73 0a 2a 2a uated and its.**
19700 20 72 65 73 75 6c 74 20 69 73 20 6c 65 66 74 20 result is left
19710 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 46 on the stack. F
19720 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f or constraints o
19730 66 20 74 68 65 20 66 6f 72 6d 20 58 20 49 4e 20 f the form X IN
19740 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69 73 20 72 6f (...).** this ro
19750 75 74 69 6e 65 20 73 65 74 73 20 75 70 20 61 20 utine sets up a
19760 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 69 loop that will i
19770 74 65 72 61 74 65 20 6f 76 65 72 20 61 6c 6c 20 terate over all
19780 76 61 6c 75 65 73 20 6f 66 20 58 2e 0a 2a 2f 0a values of X..*/.
19790 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 45 static int codeE
197a0 71 75 61 6c 69 74 79 54 65 72 6d 28 0a 20 20 50 qualityTerm(. P
197b0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
197c0 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e /* The parsin
197d0 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 g context */. W
197e0 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 2c hereTerm *pTerm,
197f0 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 6f /* The term o
19800 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 f the WHERE clau
19810 73 65 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a se to be coded *
19820 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a /. WhereLevel *
19830 70 4c 65 76 65 6c 2c 20 2f 2a 20 54 68 65 20 6c pLevel, /* The l
19840 65 76 65 6c 20 6f 66 20 74 68 65 20 46 52 4f 4d evel of the FROM
19850 20 63 6c 61 75 73 65 20 77 65 20 61 72 65 20 77 clause we are w
19860 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f 0a 20 20 69 orking on */. i
19870 6e 74 20 69 45 71 2c 20 20 20 20 20 20 20 20 20 nt iEq,
19880 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 /* Index of t
19890 68 65 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d he equality term
198a0 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 65 76 within this lev
198b0 65 6c 20 2a 2f 0a 20 20 69 6e 74 20 62 52 65 76 el */. int bRev
198c0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 , /* T
198d0 72 75 65 20 66 6f 72 20 72 65 76 65 72 73 65 2d rue for reverse-
198e0 6f 72 64 65 72 20 49 4e 20 6f 70 65 72 61 74 69 order IN operati
198f0 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 ons */. int iTa
19900 72 67 65 74 20 20 20 20 20 20 20 20 20 2f 2a 20 rget /*
19910 41 74 74 65 6d 70 74 20 74 6f 20 6c 65 61 76 65 Attempt to leave
19920 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 69 73 results in this
19930 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 29 7b 0a register */.){.
19940 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 54 65 Expr *pX = pTe
19950 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 56 64 62 rm->pExpr;. Vdb
19960 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 e *v = pParse->p
19970 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 52 65 67 Vdbe;. int iReg
19980 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
19990 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
199a0 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 73 20 2a olding results *
199b0 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 54 61 /.. assert( iTa
199c0 72 67 65 74 3e 30 20 29 3b 0a 20 20 69 66 28 20 rget>0 );. if(
199d0 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 29 7b pX->op==TK_EQ ){
199e0 0a 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 . iReg = sqli
199f0 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 te3ExprCodeTarge
19a00 74 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 t(pParse, pX->pR
19a10 69 67 68 74 2c 20 69 54 61 72 67 65 74 29 3b 0a ight, iTarget);.
19a20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d 3e }else if( pX->
19a30 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b op==TK_ISNULL ){
19a40 0a 20 20 20 20 69 52 65 67 20 3d 20 69 54 61 72 . iReg = iTar
19a50 67 65 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 get;. sqlite3
19a60 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
19a70 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52 65 67 29 3b _Null, 0, iReg);
19a80 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
19a90 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 OMIT_SUBQUERY.
19aa0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 65 }else{. int e
19ab0 54 79 70 65 3b 0a 20 20 20 20 69 6e 74 20 69 54 Type;. int iT
19ac0 61 62 3b 0a 20 20 20 20 73 74 72 75 63 74 20 49 ab;. struct I
19ad0 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a 20 20 20 20 nLoop *pIn;.
19ae0 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4c 6f 6f 70 WhereLoop *pLoop
19af0 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 57 4c 6f 6f = pLevel->pWLoo
19b00 70 3b 0a 0a 20 20 20 20 69 66 28 20 28 70 4c 6f p;.. if( (pLo
19b10 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 op->wsFlags & WH
19b20 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 ERE_VIRTUALTABLE
19b30 29 3d 3d 30 0a 20 20 20 20 20 20 26 26 20 70 4c )==0. && pL
19b40 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e oop->u.btree.pIn
19b50 64 65 78 21 3d 30 0a 20 20 20 20 20 20 26 26 20 dex!=0. &&
19b60 70 4c 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 70 pLoop->u.btree.p
19b70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 Index->aSortOrde
19b80 72 5b 69 45 71 5d 0a 20 20 20 20 29 7b 0a 20 20 r[iEq]. ){.
19b90 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 45 testcase( iE
19ba0 71 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 q==0 );. te
19bb0 73 74 63 61 73 65 28 20 62 52 65 76 20 29 3b 0a stcase( bRev );.
19bc0 20 20 20 20 20 20 62 52 65 76 20 3d 20 21 62 52 bRev = !bR
19bd0 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 ev;. }. as
19be0 73 65 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b sert( pX->op==TK
19bf0 5f 49 4e 20 29 3b 0a 20 20 20 20 69 52 65 67 20 _IN );. iReg
19c00 3d 20 69 54 61 72 67 65 74 3b 0a 20 20 20 20 65 = iTarget;. e
19c10 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 Type = sqlite3Fi
19c20 6e 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 ndInIndex(pParse
19c30 2c 20 70 58 2c 20 30 29 3b 0a 20 20 20 20 69 66 , pX, 0);. if
19c40 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 ( eType==IN_INDE
19c50 58 5f 49 4e 44 45 58 5f 44 45 53 43 20 29 7b 0a X_INDEX_DESC ){.
19c60 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
19c70 62 52 65 76 20 29 3b 0a 20 20 20 20 20 20 62 52 bRev );. bR
19c80 65 76 20 3d 20 21 62 52 65 76 3b 0a 20 20 20 20 ev = !bRev;.
19c90 7d 0a 20 20 20 20 69 54 61 62 20 3d 20 70 58 2d }. iTab = pX-
19ca0 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c >iTable;. sql
19cb0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
19cc0 2c 20 62 52 65 76 20 3f 20 4f 50 5f 4c 61 73 74 , bRev ? OP_Last
19cd0 20 3a 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69 54 : OP_Rewind, iT
19ce0 61 62 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65 ab, 0);. asse
19cf0 72 74 28 20 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c rt( (pLoop->wsFl
19d00 61 67 73 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 ags & WHERE_MULT
19d10 49 5f 4f 52 29 3d 3d 30 20 29 3b 0a 20 20 20 20 I_OR)==0 );.
19d20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 7c pLoop->wsFlags |
19d30 3d 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 3b = WHERE_IN_ABLE;
19d40 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d . if( pLevel-
19d50 3e 75 2e 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a >u.in.nIn==0 ){.
19d60 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64 pLevel->ad
19d70 64 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 drNxt = sqlite3V
19d80 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b dbeMakeLabel(v);
19d90 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 . }. pLeve
19da0 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20 l->u.in.nIn++;.
19db0 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e pLevel->u.in.
19dc0 61 49 6e 4c 6f 6f 70 20 3d 0a 20 20 20 20 20 20 aInLoop =.
19dd0 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f sqlite3DbReallo
19de0 63 4f 72 46 72 65 65 28 70 50 61 72 73 65 2d 3e cOrFree(pParse->
19df0 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e db, pLevel->u.in
19e00 2e 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 .aInLoop,.
19e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19e20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 sizeof(p
19e30 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c Level->u.in.aInL
19e40 6f 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e oop[0])*pLevel->
19e50 75 2e 69 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20 70 u.in.nIn);. p
19e60 49 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 In = pLevel->u.i
19e70 6e 2e 61 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 n.aInLoop;. i
19e80 66 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 20 20 f( pIn ){.
19e90 70 49 6e 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75 pIn += pLevel->u
19ea0 2e 69 6e 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 .in.nIn - 1;.
19eb0 20 20 20 70 49 6e 2d 3e 69 43 75 72 20 3d 20 69 pIn->iCur = i
19ec0 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 65 Tab;. if( e
19ed0 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 Type==IN_INDEX_R
19ee0 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 OWID ){.
19ef0 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d pIn->addrInTop =
19f00 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
19f10 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 p2(v, OP_Rowid,
19f20 69 54 61 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 iTab, iReg);.
19f30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
19f40 20 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 pIn->addrInTop
19f50 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 = sqlite3VdbeAd
19f60 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d dOp3(v, OP_Colum
19f70 6e 2c 20 69 54 61 62 2c 20 30 2c 20 69 52 65 67 n, iTab, 0, iReg
19f80 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
19f90 20 70 49 6e 2d 3e 65 45 6e 64 4c 6f 6f 70 4f 70 pIn->eEndLoopOp
19fa0 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72 65 = bRev ? OP_Pre
19fb0 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 v : OP_Next;.
19fc0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
19fd0 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c dOp1(v, OP_IsNul
19fe0 6c 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 7d 65 l, iReg);. }e
19ff0 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 lse{. pLeve
1a000 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 20 3d 20 30 3b l->u.in.nIn = 0;
1a010 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 . }.#endif.
1a020 7d 0a 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 }. disableTerm(
1a030 70 4c 65 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a pLevel, pTerm);.
1a040 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d return iReg;.}
1a050 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 ../*.** Generate
1a060 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 code that will
1a070 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 3d 3d 20 evaluate all ==
1a080 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e and IN constrain
1a090 74 73 20 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 ts for an.** ind
1a0a0 65 78 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 ex..**.** For ex
1a0b0 61 6d 70 6c 65 2c 20 63 6f 6e 73 69 64 65 72 20 ample, consider
1a0c0 74 61 62 6c 65 20 74 31 28 61 2c 62 2c 63 2c 64 table t1(a,b,c,d
1a0d0 2c 65 2c 66 29 20 77 69 74 68 20 69 6e 64 65 78 ,e,f) with index
1a0e0 20 69 31 28 61 2c 62 2c 63 29 2e 0a 2a 2a 20 53 i1(a,b,c)..** S
1a0f0 75 70 70 6f 73 65 20 74 68 65 20 57 48 45 52 45 uppose the WHERE
1a100 20 63 6c 61 75 73 65 20 69 73 20 74 68 69 73 3a clause is this:
1a110 20 20 61 3d 3d 35 20 41 4e 44 20 62 20 49 4e 20 a==5 AND b IN
1a120 28 31 2c 32 2c 33 29 20 41 4e 44 20 63 3e 35 20 (1,2,3) AND c>5
1a130 41 4e 44 20 63 3c 31 30 0a 2a 2a 20 54 68 65 20 AND c<10.** The
1a140 69 6e 64 65 78 20 68 61 73 20 61 73 20 6d 61 6e index has as man
1a150 79 20 61 73 20 74 68 72 65 65 20 65 71 75 61 6c y as three equal
1a160 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c ity constraints,
1a170 20 62 75 74 20 69 6e 20 74 68 69 73 0a 2a 2a 20 but in this.**
1a180 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 74 68 69 example, the thi
1a190 72 64 20 22 63 22 20 76 61 6c 75 65 20 69 73 20 rd "c" value is
1a1a0 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 2e 20 20 an inequality.
1a1b0 53 6f 20 6f 6e 6c 79 20 74 77 6f 20 0a 2a 2a 20 So only two .**
1a1c0 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 constraints are
1a1d0 63 6f 64 65 64 2e 20 20 54 68 69 73 20 72 6f 75 coded. This rou
1a1e0 74 69 6e 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 tine will genera
1a1f0 74 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 te code to evalu
1a200 61 74 65 0a 2a 2a 20 61 3d 3d 35 20 61 6e 64 20 ate.** a==5 and
1a210 62 20 49 4e 20 28 31 2c 32 2c 33 29 2e 20 20 54 b IN (1,2,3). T
1a220 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 he current value
1a230 73 20 66 6f 72 20 61 20 61 6e 64 20 62 20 77 69 s for a and b wi
1a240 6c 6c 20 62 65 20 73 74 6f 72 65 64 0a 2a 2a 20 ll be stored.**
1a250 69 6e 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 in consecutive r
1a260 65 67 69 73 74 65 72 73 20 61 6e 64 20 74 68 65 egisters and the
1a270 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 66 69 index of the fi
1a280 72 73 74 20 72 65 67 69 73 74 65 72 20 69 73 20 rst register is
1a290 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 returned..**.**
1a2a0 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 In the example a
1a2b0 62 6f 76 65 20 6e 45 71 3d 3d 32 2e 20 20 42 75 bove nEq==2. Bu
1a2c0 74 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e t this subroutin
1a2d0 65 20 77 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 e works for any
1a2e0 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 6e 45 71 20 value.** of nEq
1a2f0 69 6e 63 6c 75 64 69 6e 67 20 30 2e 20 20 49 66 including 0. If
1a300 20 6e 45 71 3d 3d 30 2c 20 74 68 69 73 20 72 6f nEq==0, this ro
1a310 75 74 69 6e 65 20 69 73 20 6e 65 61 72 6c 79 20 utine is nearly
1a320 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 20 54 68 65 20 a no-op..** The
1a330 6f 6e 6c 79 20 74 68 69 6e 67 20 69 74 20 64 6f only thing it do
1a340 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65 20 74 es is allocate t
1a350 68 65 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 20 he pLevel->iMem
1a360 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 0a memory cell and.
1a370 2a 2a 20 63 6f 6d 70 75 74 65 20 74 68 65 20 61 ** compute the a
1a380 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 2e 0a ffinity string..
1a390 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
1a3a0 6e 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 ne always alloca
1a3b0 74 65 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 tes at least one
1a3c0 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 memory cell and
1a3d0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 returns.** the
1a3e0 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20 6d 65 index of that me
1a3f0 6d 6f 72 79 20 63 65 6c 6c 2e 20 54 68 65 20 63 mory cell. The c
1a400 6f 64 65 20 74 68 61 74 0a 2a 2a 20 63 61 6c 6c ode that.** call
1a410 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 s this routine w
1a420 69 6c 6c 20 75 73 65 20 74 68 61 74 20 6d 65 6d ill use that mem
1a430 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 74 6f 72 ory cell to stor
1a440 65 20 74 68 65 20 74 65 72 6d 69 6e 61 74 69 6f e the terminatio
1a450 6e 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65 20 6f n.** key value o
1a460 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49 66 20 f the loop. If
1a470 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e 20 6f one or more IN o
1a480 70 65 72 61 74 6f 72 73 20 61 70 70 65 61 72 2c perators appear,
1a490 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f then.** this ro
1a4a0 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 utine allocates
1a4b0 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6e 45 an additional nE
1a4c0 71 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66 q memory cells f
1a4d0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 or internal.** u
1a4e0 73 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 se..**.** Before
1a4f0 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 41 returning, *pzA
1a500 66 66 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 ff is set to poi
1a510 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 nt to a buffer c
1a520 6f 6e 74 61 69 6e 69 6e 67 20 61 0a 2a 2a 20 63 ontaining a.** c
1a530 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d opy of the colum
1a540 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e n affinity strin
1a550 67 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 61 g of the index a
1a560 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 0a 2a llocated using.*
1a570 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f * sqlite3DbMallo
1a580 63 28 29 2e 20 45 78 63 65 70 74 2c 20 65 6e 74 c(). Except, ent
1a590 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f 70 79 ries in the copy
1a5a0 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 61 of the string a
1a5b0 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 ssociated.** wit
1a5c0 68 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 h equality const
1a5d0 72 61 69 6e 74 73 20 74 68 61 74 20 75 73 65 20 raints that use
1a5e0 4e 4f 4e 45 20 61 66 66 69 6e 69 74 79 20 61 72 NONE affinity ar
1a5f0 65 20 73 65 74 20 74 6f 0a 2a 2a 20 53 51 4c 49 e set to.** SQLI
1a600 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 54 68 69 TE_AFF_NONE. Thi
1a610 73 20 69 73 20 74 6f 20 64 65 61 6c 20 77 69 74 s is to deal wit
1a620 68 20 53 51 4c 20 73 75 63 68 20 61 73 20 74 68 h SQL such as th
1a630 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a e following:.**.
1a640 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c ** CREATE TABL
1a650 45 20 74 31 28 61 20 54 45 58 54 20 50 52 49 4d E t1(a TEXT PRIM
1a660 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 2a 2a 20 ARY KEY, b);.**
1a670 20 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f SELECT ... FRO
1a680 4d 20 74 31 20 41 53 20 74 32 2c 20 74 31 20 57 M t1 AS t2, t1 W
1a690 48 45 52 45 20 74 31 2e 61 20 3d 20 74 32 2e 62 HERE t1.a = t2.b
1a6a0 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 ;.**.** In the e
1a6b0 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 xample above, th
1a6c0 65 20 69 6e 64 65 78 20 6f 6e 20 74 31 28 61 29 e index on t1(a)
1a6d0 20 68 61 73 20 54 45 58 54 20 61 66 66 69 6e 69 has TEXT affini
1a6e0 74 79 2e 20 42 75 74 20 73 69 6e 63 65 0a 2a 2a ty. But since.**
1a6f0 20 74 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 the right hand
1a700 73 69 64 65 20 6f 66 20 74 68 65 20 65 71 75 61 side of the equa
1a710 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 lity constraint
1a720 28 74 32 2e 62 29 20 68 61 73 20 4e 4f 4e 45 20 (t2.b) has NONE
1a730 61 66 66 69 6e 69 74 79 2c 0a 2a 2a 20 6e 6f 20 affinity,.** no
1a740 63 6f 6e 76 65 72 73 69 6f 6e 20 73 68 6f 75 6c conversion shoul
1a750 64 20 62 65 20 61 74 74 65 6d 70 74 65 64 20 62 d be attempted b
1a760 65 66 6f 72 65 20 75 73 69 6e 67 20 61 20 74 32 efore using a t2
1a770 2e 62 20 76 61 6c 75 65 20 61 73 20 70 61 72 74 .b value as part
1a780 20 6f 66 0a 2a 2a 20 61 20 6b 65 79 20 74 6f 20 of.** a key to
1a790 73 65 61 72 63 68 20 74 68 65 20 69 6e 64 65 78 search the index
1a7a0 2e 20 48 65 6e 63 65 20 74 68 65 20 66 69 72 73 . Hence the firs
1a7b0 74 20 62 79 74 65 20 69 6e 20 74 68 65 20 72 65 t byte in the re
1a7c0 74 75 72 6e 65 64 20 61 66 66 69 6e 69 74 79 0a turned affinity.
1a7d0 2a 2a 20 73 74 72 69 6e 67 20 69 6e 20 74 68 69 ** string in thi
1a7e0 73 20 65 78 61 6d 70 6c 65 20 77 6f 75 6c 64 20 s example would
1a7f0 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 be set to SQLITE
1a800 5f 41 46 46 5f 4e 4f 4e 45 2e 0a 2a 2f 0a 73 74 _AFF_NONE..*/.st
1a810 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 6c atic int codeAll
1a820 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 0a 20 EqualityTerms(.
1a830 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
1a840 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
1a850 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 g context */. W
1a860 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 hereLevel *pLeve
1a870 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68 20 6e 65 l, /* Which ne
1a880 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 sted loop of the
1a890 20 46 52 4f 4d 20 77 65 20 61 72 65 20 63 6f 64 FROM we are cod
1a8a0 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 52 65 ing */. int bRe
1a8b0 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f v, /
1a8c0 2a 20 52 65 76 65 72 73 65 20 74 68 65 20 6f 72 * Reverse the or
1a8d0 64 65 72 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 der of IN operat
1a8e0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 ors */. int nEx
1a8f0 74 72 61 52 65 67 2c 20 20 20 20 20 20 20 20 2f traReg, /
1a900 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 * Number of extr
1a910 61 20 72 65 67 69 73 74 65 72 73 20 74 6f 20 61 a registers to a
1a920 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 63 68 61 llocate */. cha
1a930 72 20 2a 2a 70 7a 41 66 66 20 20 20 20 20 20 20 r **pzAff
1a940 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 /* OUT: Set t
1a950 6f 20 70 6f 69 6e 74 20 74 6f 20 61 66 66 69 6e o point to affin
1a960 69 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b ity string */.){
1a970 0a 20 20 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 . int nEq;
1a980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1a990 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f /* The number o
1a9a0 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73 74 f == or IN const
1a9b0 72 61 69 6e 74 73 20 74 6f 20 63 6f 64 65 20 2a raints to code *
1a9c0 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 /. Vdbe *v = pP
1a9d0 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20 arse->pVdbe;
1a9e0 20 20 2f 2a 20 54 68 65 20 76 6d 20 75 6e 64 65 /* The vm unde
1a9f0 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a r construction *
1aa00 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b /. Index *pIdx;
1aa10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aa20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 62 /* The index b
1aa30 65 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 eing used for th
1aa40 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 is loop */. Whe
1aa50 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 reTerm *pTerm;
1aa60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 /* A
1aa70 73 69 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e single constrain
1aa80 74 20 74 65 72 6d 20 2a 2f 0a 20 20 57 68 65 72 t term */. Wher
1aa90 65 4c 6f 6f 70 20 2a 70 4c 6f 6f 70 3b 20 20 20 eLoop *pLoop;
1aaa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
1aab0 20 57 68 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 WhereLoop objec
1aac0 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 t */. int j;
1aad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1aae0 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
1aaf0 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 nter */. int re
1ab00 67 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 gBase;
1ab10 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 /* Base
1ab20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e register */. in
1ab30 74 20 6e 52 65 67 3b 20 20 20 20 20 20 20 20 20 t nReg;
1ab40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1ab50 75 6d 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 umber of registe
1ab60 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a rs to allocate *
1ab70 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 3b 20 /. char *zAff;
1ab80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ab90 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 73 74 /* Affinity st
1aba0 72 69 6e 67 20 74 6f 20 72 65 74 75 72 6e 20 2a ring to return *
1abb0 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6d 6f 64 /.. /* This mod
1abc0 75 6c 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c ule is only call
1abd0 65 64 20 6f 6e 20 71 75 65 72 79 20 70 6c 61 6e ed on query plan
1abe0 73 20 74 68 61 74 20 75 73 65 20 61 6e 20 69 6e s that use an in
1abf0 64 65 78 2e 20 2a 2f 0a 20 20 70 4c 6f 6f 70 20 dex. */. pLoop
1ac00 3d 20 70 4c 65 76 65 6c 2d 3e 70 57 4c 6f 6f 70 = pLevel->pWLoop
1ac10 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4c 6f ;. assert( (pLo
1ac20 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 op->wsFlags & WH
1ac30 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 ERE_VIRTUALTABLE
1ac40 29 3d 3d 30 20 29 3b 0a 20 20 6e 45 71 20 3d 20 )==0 );. nEq =
1ac50 70 4c 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e pLoop->u.btree.n
1ac60 45 71 3b 0a 20 20 70 49 64 78 20 3d 20 70 4c 6f Eq;. pIdx = pLo
1ac70 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 op->u.btree.pInd
1ac80 65 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 ex;. assert( pI
1ac90 64 78 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 46 dx!=0 );.. /* F
1aca0 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 igure out how ma
1acb0 6e 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 ny memory cells
1acc0 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 68 65 we will need the
1acd0 6e 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 6d 2e n allocate them.
1ace0 0a 20 20 2a 2f 0a 20 20 72 65 67 42 61 73 65 20 . */. regBase
1acf0 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b = pParse->nMem +
1ad00 20 31 3b 0a 20 20 6e 52 65 67 20 3d 20 70 4c 6f 1;. nReg = pLo
1ad10 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 20 op->u.btree.nEq
1ad20 2b 20 6e 45 78 74 72 61 52 65 67 3b 0a 20 20 70 + nExtraReg;. p
1ad30 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e Parse->nMem += n
1ad40 52 65 67 3b 0a 0a 20 20 7a 41 66 66 20 3d 20 73 Reg;.. zAff = s
1ad50 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70 qlite3DbStrDup(p
1ad60 50 61 72 73 65 2d 3e 64 62 2c 20 73 71 6c 69 74 Parse->db, sqlit
1ad70 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 e3IndexAffinityS
1ad80 74 72 28 76 2c 20 70 49 64 78 29 29 3b 0a 20 20 tr(v, pIdx));.
1ad90 69 66 28 20 21 7a 41 66 66 20 29 7b 0a 20 20 20 if( !zAff ){.
1ada0 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c pParse->db->mal
1adb0 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 locFailed = 1;.
1adc0 20 7d 0a 0a 20 20 2f 2a 20 45 76 61 6c 75 61 74 }.. /* Evaluat
1add0 65 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 63 e the equality c
1ade0 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2f 0a onstraints. */.
1adf0 20 20 61 73 73 65 72 74 28 20 7a 41 66 66 3d 3d assert( zAff==
1ae00 30 20 7c 7c 20 28 69 6e 74 29 73 74 72 6c 65 6e 0 || (int)strlen
1ae10 28 7a 41 66 66 29 3e 3d 6e 45 71 20 29 3b 0a 20 (zAff)>=nEq );.
1ae20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 45 71 3b for(j=0; j<nEq;
1ae30 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 j++){. int r
1ae40 31 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 70 1;. pTerm = p
1ae50 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d 5b 6a 5d 3b Loop->aLTerm[j];
1ae60 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 . assert( pTe
1ae70 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 rm!=0 );. /*
1ae80 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 72 The following tr
1ae90 75 65 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77 ue for indices w
1aea0 69 74 68 20 72 65 64 75 6e 64 61 6e 74 20 63 6f ith redundant co
1aeb0 6c 75 6d 6e 73 2e 20 0a 20 20 20 20 2a 2a 20 45 lumns. . ** E
1aec0 78 3a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 x: CREATE INDEX
1aed0 69 31 20 4f 4e 20 74 31 28 61 2c 62 2c 61 29 3b i1 ON t1(a,b,a);
1aee0 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 SELECT * FROM t
1aef0 31 20 57 48 45 52 45 20 61 3d 30 20 41 4e 44 20 1 WHERE a=0 AND
1af00 62 3d 30 3b 20 2a 2f 0a 20 20 20 20 74 65 73 74 b=0; */. test
1af10 63 61 73 65 28 20 28 70 54 65 72 6d 2d 3e 77 74 case( (pTerm->wt
1af20 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 Flags & TERM_COD
1af30 45 44 29 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 ED)!=0 );. te
1af40 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 stcase( pTerm->w
1af50 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49 tFlags & TERM_VI
1af60 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 72 31 20 RTUAL );. r1
1af70 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 = codeEqualityTe
1af80 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d rm(pParse, pTerm
1af90 2c 20 70 4c 65 76 65 6c 2c 20 6a 2c 20 62 52 65 , pLevel, j, bRe
1afa0 76 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 v, regBase+j);.
1afb0 20 20 20 69 66 28 20 72 31 21 3d 72 65 67 42 61 if( r1!=regBa
1afc0 73 65 2b 6a 20 29 7b 0a 20 20 20 20 20 20 69 66 se+j ){. if
1afd0 28 20 6e 52 65 67 3d 3d 31 20 29 7b 0a 20 20 20 ( nReg==1 ){.
1afe0 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 sqlite3Rele
1aff0 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 aseTempReg(pPars
1b000 65 2c 20 72 65 67 42 61 73 65 29 3b 0a 20 20 20 e, regBase);.
1b010 20 20 20 20 20 72 65 67 42 61 73 65 20 3d 20 72 regBase = r
1b020 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 1;. }else{.
1b030 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
1b040 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
1b050 53 43 6f 70 79 2c 20 72 31 2c 20 72 65 67 42 61 SCopy, r1, regBa
1b060 73 65 2b 6a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 se+j);. }.
1b070 20 20 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 }. testcas
1b080 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 e( pTerm->eOpera
1b090 74 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 tor & WO_ISNULL
1b0a0 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
1b0b0 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f pTerm->eOperato
1b0c0 72 20 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 r & WO_IN );.
1b0d0 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 if( (pTerm->eOp
1b0e0 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e erator & (WO_ISN
1b0f0 55 4c 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30 20 29 ULL|WO_IN))==0 )
1b100 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 {. Expr *pR
1b110 69 67 68 74 20 3d 20 70 54 65 72 6d 2d 3e 70 45 ight = pTerm->pE
1b120 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 xpr->pRight;.
1b130 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
1b140 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20 deIsNullJump(v,
1b150 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b pRight, regBase+
1b160 6a 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 j, pLevel->addrB
1b170 72 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a rk);. if( z
1b180 41 66 66 20 29 7b 0a 20 20 20 20 20 20 20 20 69 Aff ){. i
1b190 66 28 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 f( sqlite3Compar
1b1a0 65 41 66 66 69 6e 69 74 79 28 70 52 69 67 68 74 eAffinity(pRight
1b1b0 2c 20 7a 41 66 66 5b 6a 5d 29 3d 3d 53 51 4c 49 , zAff[j])==SQLI
1b1c0 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 TE_AFF_NONE ){.
1b1d0 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6a 5d zAff[j]
1b1e0 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f = SQLITE_AFF_NO
1b1f0 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 NE;. }.
1b200 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
1b210 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69 3ExprNeedsNoAffi
1b220 6e 69 74 79 43 68 61 6e 67 65 28 70 52 69 67 68 nityChange(pRigh
1b230 74 2c 20 7a 41 66 66 5b 6a 5d 29 20 29 7b 0a 20 t, zAff[j]) ){.
1b240 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6a 5d zAff[j]
1b250 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f = SQLITE_AFF_NO
1b260 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 NE;. }.
1b270 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a }. }. }.
1b280 20 20 2a 70 7a 41 66 66 20 3d 20 7a 41 66 66 3b *pzAff = zAff;
1b290 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42 61 73 . return regBas
1b2a0 65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 e;.}..#ifndef SQ
1b2b0 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
1b2c0 4e 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 N./*.** This rou
1b2d0 74 69 6e 65 20 69 73 20 61 20 68 65 6c 70 65 72 tine is a helper
1b2e0 20 66 6f 72 20 65 78 70 6c 61 69 6e 49 6e 64 65 for explainInde
1b2f0 78 52 61 6e 67 65 28 29 20 62 65 6c 6f 77 0a 2a xRange() below.*
1b300 2a 0a 2a 2a 20 70 53 74 72 20 68 6f 6c 64 73 20 *.** pStr holds
1b310 74 68 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 the text of an e
1b320 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 xpression that w
1b330 65 20 61 72 65 20 62 75 69 6c 64 69 6e 67 20 75 e are building u
1b340 70 20 6f 6e 65 20 74 65 72 6d 0a 2a 2a 20 61 74 p one term.** at
1b350 20 61 20 74 69 6d 65 2e 20 20 54 68 69 73 20 72 a time. This r
1b360 6f 75 74 69 6e 65 20 61 64 64 73 20 61 20 6e 65 outine adds a ne
1b370 77 20 74 65 72 6d 20 74 6f 20 74 68 65 20 65 6e w term to the en
1b380 64 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 d of the express
1b390 69 6f 6e 2e 0a 2a 2a 20 54 65 72 6d 73 20 61 72 ion..** Terms ar
1b3a0 65 20 73 65 70 61 72 61 74 65 64 20 62 79 20 41 e separated by A
1b3b0 4e 44 20 73 6f 20 61 64 64 20 74 68 65 20 22 41 ND so add the "A
1b3c0 4e 44 22 20 74 65 78 74 20 66 6f 72 20 73 65 63 ND" text for sec
1b3d0 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 ond and subseque
1b3e0 6e 74 0a 2a 2a 20 74 65 72 6d 73 20 6f 6e 6c 79 nt.** terms only
1b3f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
1b400 20 65 78 70 6c 61 69 6e 41 70 70 65 6e 64 54 65 explainAppendTe
1b410 72 6d 28 0a 20 20 53 74 72 41 63 63 75 6d 20 2a rm(. StrAccum *
1b420 70 53 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 pStr,
1b430 20 20 2f 2a 20 54 68 65 20 74 65 78 74 20 65 78 /* The text ex
1b440 70 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 20 62 pression being b
1b450 75 69 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54 uilt */. int iT
1b460 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 erm,
1b470 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f /* Index o
1b480 66 20 74 68 69 73 20 74 65 72 6d 2e 20 20 46 69 f this term. Fi
1b490 72 73 74 20 69 73 20 7a 65 72 6f 20 2a 2f 0a 20 rst is zero */.
1b4a0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f const char *zCo
1b4b0 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 lumn, /*
1b4c0 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 Name of the colu
1b4d0 6d 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 mn */. const ch
1b4e0 61 72 20 2a 7a 4f 70 20 20 20 20 20 20 20 20 20 ar *zOp
1b4f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 /* Name of t
1b500 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 29 he operator */.)
1b510 7b 0a 20 20 69 66 28 20 69 54 65 72 6d 20 29 20 {. if( iTerm )
1b520 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 sqlite3StrAccumA
1b530 70 70 65 6e 64 28 70 53 74 72 2c 20 22 20 41 4e ppend(pStr, " AN
1b540 44 20 22 2c 20 35 29 3b 0a 20 20 73 71 6c 69 74 D ", 5);. sqlit
1b550 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 e3StrAccumAppend
1b560 28 70 53 74 72 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 (pStr, zColumn,
1b570 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 -1);. sqlite3St
1b580 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 53 74 rAccumAppend(pSt
1b590 72 2c 20 7a 4f 70 2c 20 31 29 3b 0a 20 20 73 71 r, zOp, 1);. sq
1b5a0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 lite3StrAccumApp
1b5b0 65 6e 64 28 70 53 74 72 2c 20 22 3f 22 2c 20 31 end(pStr, "?", 1
1b5c0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 );.}../*.** Argu
1b5d0 6d 65 6e 74 20 70 4c 65 76 65 6c 20 64 65 73 63 ment pLevel desc
1b5e0 72 69 62 65 73 20 61 20 73 74 72 61 74 65 67 79 ribes a strategy
1b5f0 20 66 6f 72 20 73 63 61 6e 6e 69 6e 67 20 74 61 for scanning ta
1b600 62 6c 65 20 70 54 61 62 2e 20 54 68 69 73 20 0a ble pTab. This .
1b610 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 ** function retu
1b620 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f rns a pointer to
1b630 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 a string buffer
1b640 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 64 65 containing a de
1b650 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 scription.** of
1b660 74 68 65 20 73 75 62 73 65 74 20 6f 66 20 74 61 the subset of ta
1b670 62 6c 65 20 72 6f 77 73 20 73 63 61 6e 6e 65 64 ble rows scanned
1b680 20 62 79 20 74 68 65 20 73 74 72 61 74 65 67 79 by the strategy
1b690 20 69 6e 20 74 68 65 20 66 6f 72 6d 20 6f 66 20 in the form of
1b6a0 61 6e 0a 2a 2a 20 53 51 4c 20 65 78 70 72 65 73 an.** SQL expres
1b6b0 73 69 6f 6e 2e 20 4f 72 2c 20 69 66 20 61 6c 6c sion. Or, if all
1b6c0 20 72 6f 77 73 20 61 72 65 20 73 63 61 6e 6e 65 rows are scanne
1b6d0 64 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 d, NULL is retur
1b6e0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 ned..**.** For e
1b6f0 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 71 xample, if the q
1b700 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 uery:.**.** SE
1b710 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 LECT * FROM t1 W
1b720 48 45 52 45 20 61 3d 31 20 41 4e 44 20 62 3e 32 HERE a=1 AND b>2
1b730 3b 0a 2a 2a 0a 2a 2a 20 69 73 20 72 75 6e 20 61 ;.**.** is run a
1b740 6e 64 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 nd there is an i
1b750 6e 64 65 78 20 6f 6e 20 28 61 2c 20 62 29 2c 20 ndex on (a, b),
1b760 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 then this functi
1b770 6f 6e 20 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 on returns a.**
1b780 73 74 72 69 6e 67 20 73 69 6d 69 6c 61 72 20 74 string similar t
1b790 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 3d 3f 20 o:.**.** "a=?
1b7a0 41 4e 44 20 62 3e 3f 22 0a 2a 2a 0a 2a 2a 20 54 AND b>?".**.** T
1b7b0 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69 6e he returned poin
1b7c0 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 6d 65 ter points to me
1b7d0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 mory obtained fr
1b7e0 6f 6d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c om sqlite3DbMall
1b7f0 6f 63 28 29 2e 0a 2a 2a 20 49 74 20 69 73 20 74 oc()..** It is t
1b800 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 he responsibilit
1b810 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 y of the caller
1b820 74 6f 20 66 72 65 65 20 74 68 65 20 62 75 66 66 to free the buff
1b830 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a 2a 2a er when it is.**
1b840 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 no longer requi
1b850 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 red..*/.static c
1b860 68 61 72 20 2a 65 78 70 6c 61 69 6e 49 6e 64 65 har *explainInde
1b870 78 52 61 6e 67 65 28 73 71 6c 69 74 65 33 20 2a xRange(sqlite3 *
1b880 64 62 2c 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 db, WhereLoop *p
1b890 4c 6f 6f 70 2c 20 54 61 62 6c 65 20 2a 70 54 61 Loop, Table *pTa
1b8a0 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e b){. Index *pIn
1b8b0 64 65 78 20 3d 20 70 4c 6f 6f 70 2d 3e 75 2e 62 dex = pLoop->u.b
1b8c0 74 72 65 65 2e 70 49 6e 64 65 78 3b 0a 20 20 69 tree.pIndex;. i
1b8d0 6e 74 20 6e 45 71 20 3d 20 70 4c 6f 6f 70 2d 3e nt nEq = pLoop->
1b8e0 75 2e 62 74 72 65 65 2e 6e 45 71 3b 0a 20 20 69 u.btree.nEq;. i
1b8f0 6e 74 20 69 2c 20 6a 3b 0a 20 20 43 6f 6c 75 6d nt i, j;. Colum
1b900 6e 20 2a 61 43 6f 6c 20 3d 20 70 54 61 62 2d 3e n *aCol = pTab->
1b910 61 43 6f 6c 3b 0a 20 20 69 6e 74 20 2a 61 69 43 aCol;. int *aiC
1b920 6f 6c 75 6d 6e 20 3d 20 70 49 6e 64 65 78 2d 3e olumn = pIndex->
1b930 61 69 43 6f 6c 75 6d 6e 3b 0a 20 20 53 74 72 41 aiColumn;. StrA
1b940 63 63 75 6d 20 74 78 74 3b 0a 0a 20 20 69 66 28 ccum txt;.. if(
1b950 20 6e 45 71 3d 3d 30 20 26 26 20 28 70 4c 6f 6f nEq==0 && (pLoo
1b960 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 28 57 48 p->wsFlags & (WH
1b970 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 7c 57 48 ERE_BTM_LIMIT|WH
1b980 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 29 29 3d ERE_TOP_LIMIT))=
1b990 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e =0 ){. return
1b9a0 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 0;. }. sqlite
1b9b0 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 74 3StrAccumInit(&t
1b9c0 78 74 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 xt, 0, 0, SQLITE
1b9d0 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a 20 20 _MAX_LENGTH);.
1b9e0 74 78 74 2e 64 62 20 3d 20 64 62 3b 0a 20 20 73 txt.db = db;. s
1b9f0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 qlite3StrAccumAp
1ba00 70 65 6e 64 28 26 74 78 74 2c 20 22 20 28 22 2c pend(&txt, " (",
1ba10 20 32 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2);. for(i=0;
1ba20 69 3c 6e 45 71 3b 20 69 2b 2b 29 7b 0a 20 20 20 i<nEq; i++){.
1ba30 20 63 68 61 72 20 2a 7a 20 3d 20 28 69 3d 3d 70 char *z = (i==p
1ba40 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 Index->nColumn )
1ba50 20 3f 20 22 72 6f 77 69 64 22 20 3a 20 61 43 6f ? "rowid" : aCo
1ba60 6c 5b 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 2e 7a l[aiColumn[i]].z
1ba70 4e 61 6d 65 3b 0a 20 20 20 20 65 78 70 6c 61 69 Name;. explai
1ba80 6e 41 70 70 65 6e 64 54 65 72 6d 28 26 74 78 74 nAppendTerm(&txt
1ba90 2c 20 69 2c 20 7a 2c 20 22 3d 22 29 3b 0a 20 20 , i, z, "=");.
1baa0 7d 0a 0a 20 20 6a 20 3d 20 69 3b 0a 20 20 69 66 }.. j = i;. if
1bab0 28 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 ( pLoop->wsFlags
1bac0 26 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 &WHERE_BTM_LIMIT
1bad0 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 ){. char *z
1bae0 3d 20 28 6a 3d 3d 70 49 6e 64 65 78 2d 3e 6e 43 = (j==pIndex->nC
1baf0 6f 6c 75 6d 6e 20 29 20 3f 20 22 72 6f 77 69 64 olumn ) ? "rowid
1bb00 22 20 3a 20 61 43 6f 6c 5b 61 69 43 6f 6c 75 6d " : aCol[aiColum
1bb10 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 n[j]].zName;.
1bb20 20 65 78 70 6c 61 69 6e 41 70 70 65 6e 64 54 65 explainAppendTe
1bb30 72 6d 28 26 74 78 74 2c 20 69 2b 2b 2c 20 7a 2c rm(&txt, i++, z,
1bb40 20 22 3e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 ">");. }. if(
1bb50 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 26 pLoop->wsFlags&
1bb60 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 WHERE_TOP_LIMIT
1bb70 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d ){. char *z =
1bb80 20 28 6a 3d 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f (j==pIndex->nCo
1bb90 6c 75 6d 6e 20 29 20 3f 20 22 72 6f 77 69 64 22 lumn ) ? "rowid"
1bba0 20 3a 20 61 43 6f 6c 5b 61 69 43 6f 6c 75 6d 6e : aCol[aiColumn
1bbb0 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 [j]].zName;.
1bbc0 65 78 70 6c 61 69 6e 41 70 70 65 6e 64 54 65 72 explainAppendTer
1bbd0 6d 28 26 74 78 74 2c 20 69 2c 20 7a 2c 20 22 3c m(&txt, i, z, "<
1bbe0 22 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 ");. }. sqlite
1bbf0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 3StrAccumAppend(
1bc00 26 74 78 74 2c 20 22 29 22 2c 20 31 29 3b 0a 20 &txt, ")", 1);.
1bc10 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 return sqlite3S
1bc20 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 74 trAccumFinish(&t
1bc30 78 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 xt);.}../*.** Th
1bc40 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 is function is a
1bc50 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 63 75 no-op unless cu
1bc60 72 72 65 6e 74 6c 79 20 70 72 6f 63 65 73 73 69 rrently processi
1bc70 6e 67 20 61 6e 20 45 58 50 4c 41 49 4e 20 51 55 ng an EXPLAIN QU
1bc80 45 52 59 20 50 4c 41 4e 0a 2a 2a 20 63 6f 6d 6d ERY PLAN.** comm
1bc90 61 6e 64 2e 20 49 66 20 74 68 65 20 71 75 65 72 and. If the quer
1bca0 79 20 62 65 69 6e 67 20 63 6f 6d 70 69 6c 65 64 y being compiled
1bcb0 20 69 73 20 61 6e 20 45 58 50 4c 41 49 4e 20 51 is an EXPLAIN Q
1bcc0 55 45 52 59 20 50 4c 41 4e 2c 20 61 20 73 69 6e UERY PLAN, a sin
1bcd0 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 69 73 gle.** record is
1bce0 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6f 75 added to the ou
1bcf0 74 70 75 74 20 74 6f 20 64 65 73 63 72 69 62 65 tput to describe
1bd00 20 74 68 65 20 74 61 62 6c 65 20 73 63 61 6e 20 the table scan
1bd10 73 74 72 61 74 65 67 79 20 69 6e 20 0a 2a 2a 20 strategy in .**
1bd20 70 4c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 pLevel..*/.stati
1bd30 63 20 76 6f 69 64 20 65 78 70 6c 61 69 6e 4f 6e c void explainOn
1bd40 65 53 63 61 6e 28 0a 20 20 50 61 72 73 65 20 2a eScan(. Parse *
1bd50 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
1bd60 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
1bd70 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 e context */. S
1bd80 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 rcList *pTabList
1bd90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f , /
1bda0 2a 20 54 61 62 6c 65 20 6c 69 73 74 20 74 68 69 * Table list thi
1bdb0 73 20 6c 6f 6f 70 20 72 65 66 65 72 73 20 74 6f s loop refers to
1bdc0 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c */. WhereLevel
1bdd0 20 2a 70 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 *pLevel,
1bde0 20 20 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 6f /* Scan to
1bdf0 20 77 72 69 74 65 20 4f 50 5f 45 78 70 6c 61 69 write OP_Explai
1be00 6e 20 6f 70 63 6f 64 65 20 66 6f 72 20 2a 2f 0a n opcode for */.
1be10 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 int iLevel,
1be20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1be30 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 /* Value for "
1be40 6c 65 76 65 6c 22 20 63 6f 6c 75 6d 6e 20 6f 66 level" column of
1be50 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 output */. int
1be60 20 69 46 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 iFrom,
1be70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1be80 56 61 6c 75 65 20 66 6f 72 20 22 66 72 6f 6d 22 Value for "from"
1be90 20 63 6f 6c 75 6d 6e 20 6f 66 20 6f 75 74 70 75 column of outpu
1bea0 74 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c t */. u16 wctrl
1beb0 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 20 Flags
1bec0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
1bed0 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 passed to sqlite
1bee0 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a 2f 3WhereBegin() */
1bef0 0a 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 .){. if( pParse
1bf00 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a ->explain==2 ){.
1bf10 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 struct SrcLi
1bf20 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d st_item *pItem =
1bf30 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c &pTabList->a[pL
1bf40 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 evel->iFrom];.
1bf50 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
1bf60 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20 20 20 se->pVdbe;
1bf70 2f 2a 20 56 4d 20 62 65 69 6e 67 20 63 6f 6e 73 /* VM being cons
1bf80 74 72 75 63 74 65 64 20 2a 2f 0a 20 20 20 20 73 tructed */. s
1bf90 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 qlite3 *db = pPa
1bfa0 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 2f 2a 20 rse->db; /*
1bfb0 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 Database handle
1bfc0 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 */. char *zMs
1bfd0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 g;
1bfe0 20 20 20 20 20 2f 2a 20 54 65 78 74 20 74 6f 20 /* Text to
1bff0 61 64 64 20 74 6f 20 45 51 50 20 6f 75 74 70 75 add to EQP outpu
1c000 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 49 64 t */. int iId
1c010 20 3d 20 70 50 61 72 73 65 2d 3e 69 53 65 6c 65 = pParse->iSele
1c020 63 74 49 64 3b 20 20 2f 2a 20 53 65 6c 65 63 74 ctId; /* Select
1c030 20 69 64 20 28 6c 65 66 74 2d 6d 6f 73 74 20 6f id (left-most o
1c040 75 74 70 75 74 20 63 6f 6c 75 6d 6e 29 20 2a 2f utput column) */
1c050 0a 20 20 20 20 69 6e 74 20 69 73 53 65 61 72 63 . int isSearc
1c060 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 h;
1c070 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 /* True for a
1c080 20 53 45 41 52 43 48 2e 20 46 61 6c 73 65 20 66 SEARCH. False f
1c090 6f 72 20 53 43 41 4e 2e 20 2a 2f 0a 20 20 20 20 or SCAN. */.
1c0a0 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4c 6f 6f 70 WhereLoop *pLoop
1c0b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
1c0c0 20 54 68 65 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 The controlling
1c0d0 20 57 68 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 WhereLoop objec
1c0e0 74 20 2a 2f 0a 20 20 20 20 75 33 32 20 66 6c 61 t */. u32 fla
1c0f0 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 gs;
1c100 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 /* Flags
1c110 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68 that describe th
1c120 69 73 20 6c 6f 6f 70 20 2a 2f 0a 0a 20 20 20 20 is loop */..
1c130 70 4c 6f 6f 70 20 3d 20 70 4c 65 76 65 6c 2d 3e pLoop = pLevel->
1c140 70 57 4c 6f 6f 70 3b 0a 20 20 20 20 66 6c 61 67 pWLoop;. flag
1c150 73 20 3d 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 s = pLoop->wsFla
1c160 67 73 3b 0a 20 20 20 20 69 66 28 20 28 66 6c 61 gs;. if( (fla
1c170 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f gs&WHERE_MULTI_O
1c180 52 29 20 7c 7c 20 28 77 63 74 72 6c 46 6c 61 67 R) || (wctrlFlag
1c190 73 26 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 s&WHERE_ONETABLE
1c1a0 5f 4f 4e 4c 59 29 20 29 20 72 65 74 75 72 6e 3b _ONLY) ) return;
1c1b0 0a 0a 20 20 20 20 69 73 53 65 61 72 63 68 20 3d .. isSearch =
1c1c0 20 28 66 6c 61 67 73 26 28 57 48 45 52 45 5f 42 (flags&(WHERE_B
1c1d0 54 4d 5f 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54 TM_LIMIT|WHERE_T
1c1e0 4f 50 5f 4c 49 4d 49 54 29 29 21 3d 30 0a 20 20 OP_LIMIT))!=0.
1c1f0 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 66 || ((f
1c200 6c 61 67 73 26 57 48 45 52 45 5f 56 49 52 54 55 lags&WHERE_VIRTU
1c210 41 4c 54 41 42 4c 45 29 3d 3d 30 20 26 26 20 28 ALTABLE)==0 && (
1c220 70 4c 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e pLoop->u.btree.n
1c230 45 71 3e 30 29 29 0a 20 20 20 20 20 20 20 20 20 Eq>0)).
1c240 20 20 20 7c 7c 20 28 77 63 74 72 6c 46 6c 61 67 || (wctrlFlag
1c250 73 26 28 57 48 45 52 45 5f 4f 52 44 45 52 42 59 s&(WHERE_ORDERBY
1c260 5f 4d 49 4e 7c 57 48 45 52 45 5f 4f 52 44 45 52 _MIN|WHERE_ORDER
1c270 42 59 5f 4d 41 58 29 29 3b 0a 0a 20 20 20 20 7a BY_MAX));.. z
1c280 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Msg = sqlite3MPr
1c290 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 69 intf(db, "%s", i
1c2a0 73 53 65 61 72 63 68 3f 22 53 45 41 52 43 48 22 sSearch?"SEARCH"
1c2b0 3a 22 53 43 41 4e 22 29 3b 0a 20 20 20 20 69 66 :"SCAN");. if
1c2c0 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 ( pItem->pSelect
1c2d0 20 29 7b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d ){. zMsg =
1c2e0 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 sqlite3MAppendf
1c2f0 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 53 (db, zMsg, "%s S
1c300 55 42 51 55 45 52 59 20 25 64 22 2c 20 7a 4d 73 UBQUERY %d", zMs
1c310 67 2c 70 49 74 65 6d 2d 3e 69 53 65 6c 65 63 74 g,pItem->iSelect
1c320 49 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a Id);. }else{.
1c330 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c zMsg = sql
1c340 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c ite3MAppendf(db,
1c350 20 7a 4d 73 67 2c 20 22 25 73 20 54 41 42 4c 45 zMsg, "%s TABLE
1c360 20 25 73 22 2c 20 7a 4d 73 67 2c 20 70 49 74 65 %s", zMsg, pIte
1c370 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d m->zName);. }
1c380 0a 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d .. if( pItem-
1c390 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 >zAlias ){.
1c3a0 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d zMsg = sqlite3M
1c3b0 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 Appendf(db, zMsg
1c3c0 2c 20 22 25 73 20 41 53 20 25 73 22 2c 20 7a 4d , "%s AS %s", zM
1c3d0 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 sg, pItem->zAlia
1c3e0 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 s);. }. if
1c3f0 28 20 28 66 6c 61 67 73 20 26 20 28 57 48 45 52 ( (flags & (WHER
1c400 45 5f 49 50 4b 7c 57 48 45 52 45 5f 56 49 52 54 E_IPK|WHERE_VIRT
1c410 55 41 4c 54 41 42 4c 45 29 29 3d 3d 30 0a 20 20 UALTABLE))==0.
1c420 20 20 20 26 26 20 41 4c 57 41 59 53 28 70 4c 6f && ALWAYS(pLo
1c430 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 op->u.btree.pInd
1c440 65 78 21 3d 30 29 0a 20 20 20 20 29 7b 0a 20 20 ex!=0). ){.
1c450 20 20 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 char *zWhere
1c460 20 3d 20 65 78 70 6c 61 69 6e 49 6e 64 65 78 52 = explainIndexR
1c470 61 6e 67 65 28 64 62 2c 20 70 4c 6f 6f 70 2c 20 ange(db, pLoop,
1c480 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 pItem->pTab);.
1c490 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 zMsg = sqlit
1c4a0 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a e3MAppendf(db, z
1c4b0 4d 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Msg,.
1c4c0 20 20 20 20 28 28 66 6c 61 67 73 20 26 20 57 48 ((flags & WH
1c4d0 45 52 45 5f 41 55 54 4f 5f 49 4e 44 45 58 29 20 ERE_AUTO_INDEX)
1c4e0 3f 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ? .
1c4f0 20 20 20 20 20 20 22 25 73 20 55 53 49 4e 47 20 "%s USING
1c500 41 55 54 4f 4d 41 54 49 43 20 25 73 49 4e 44 45 AUTOMATIC %sINDE
1c510 58 25 2e 30 73 25 73 22 20 3a 0a 20 20 20 20 20 X%.0s%s" :.
1c520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 "%
1c530 73 20 55 53 49 4e 47 20 25 73 49 4e 44 45 58 20 s USING %sINDEX
1c540 25 73 25 73 22 29 2c 20 0a 20 20 20 20 20 20 20 %s%s"), .
1c550 20 20 20 20 20 20 20 20 7a 4d 73 67 2c 20 28 28 zMsg, ((
1c560 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 flags & WHERE_ID
1c570 58 5f 4f 4e 4c 59 29 20 3f 20 22 43 4f 56 45 52 X_ONLY) ? "COVER
1c580 49 4e 47 20 22 20 3a 20 22 22 29 2c 0a 20 20 20 ING " : ""),.
1c590 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 6f 6f pLoo
1c5a0 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 65 p->u.btree.pInde
1c5b0 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 x->zName, zWhere
1c5c0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1c5d0 44 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 DbFree(db, zWher
1c5e0 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 e);. }else if
1c5f0 28 20 28 66 6c 61 67 73 20 26 20 57 48 45 52 45 ( (flags & WHERE
1c600 5f 49 50 4b 29 21 3d 30 20 26 26 20 28 66 6c 61 _IPK)!=0 && (fla
1c610 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4e 53 54 gs & WHERE_CONST
1c620 52 41 49 4e 54 29 21 3d 30 20 29 7b 0a 20 20 20 RAINT)!=0 ){.
1c630 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 zMsg = sqlite
1c640 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 3MAppendf(db, zM
1c650 73 67 2c 20 22 25 73 20 55 53 49 4e 47 20 49 4e sg, "%s USING IN
1c660 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 TEGER PRIMARY KE
1c670 59 22 2c 20 7a 4d 73 67 29 3b 0a 0a 20 20 20 20 Y", zMsg);..
1c680 20 20 69 66 28 20 66 6c 61 67 73 26 28 57 48 45 if( flags&(WHE
1c690 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45 RE_COLUMN_EQ|WHE
1c6a0 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 20 29 7b RE_COLUMN_IN) ){
1c6b0 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 . zMsg =
1c6c0 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 sqlite3MAppendf(
1c6d0 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 28 72 db, zMsg, "%s (r
1c6e0 6f 77 69 64 3d 3f 29 22 2c 20 7a 4d 73 67 29 3b owid=?)", zMsg);
1c6f0 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
1c700 20 28 66 6c 61 67 73 26 57 48 45 52 45 5f 42 4f (flags&WHERE_BO
1c710 54 48 5f 4c 49 4d 49 54 29 3d 3d 57 48 45 52 45 TH_LIMIT)==WHERE
1c720 5f 42 4f 54 48 5f 4c 49 4d 49 54 20 29 7b 0a 20 _BOTH_LIMIT ){.
1c730 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 zMsg = sq
1c740 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 lite3MAppendf(db
1c750 2c 20 7a 4d 73 67 2c 20 22 25 73 20 28 72 6f 77 , zMsg, "%s (row
1c760 69 64 3e 3f 20 41 4e 44 20 72 6f 77 69 64 3c 3f id>? AND rowid<?
1c770 29 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 )", zMsg);.
1c780 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 }else if( flags
1c790 26 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 &WHERE_BTM_LIMIT
1c7a0 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 ){. zMsg
1c7b0 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e = sqlite3MAppen
1c7c0 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 df(db, zMsg, "%s
1c7d0 20 28 72 6f 77 69 64 3e 3f 29 22 2c 20 7a 4d 73 (rowid>?)", zMs
1c7e0 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 g);. }else
1c7f0 69 66 28 20 41 4c 57 41 59 53 28 66 6c 61 67 73 if( ALWAYS(flags
1c800 26 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 &WHERE_TOP_LIMIT
1c810 29 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 ) ){. zMs
1c820 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 g = sqlite3MAppe
1c830 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 ndf(db, zMsg, "%
1c840 73 20 28 72 6f 77 69 64 3c 3f 29 22 2c 20 7a 4d s (rowid<?)", zM
1c850 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 sg);. }.
1c860 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 }.#ifndef SQLIT
1c870 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
1c880 42 4c 45 0a 20 20 20 20 65 6c 73 65 20 69 66 28 BLE. else if(
1c890 20 28 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f (flags & WHERE_
1c8a0 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 VIRTUALTABLE)!=0
1c8b0 20 29 7b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d ){. zMsg =
1c8c0 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 sqlite3MAppendf
1c8d0 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56 (db, zMsg, "%s V
1c8e0 49 52 54 55 41 4c 20 54 41 42 4c 45 20 49 4e 44 IRTUAL TABLE IND
1c8f0 45 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73 67 2c EX %d:%s", zMsg,
1c900 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1c910 20 20 20 70 4c 6f 6f 70 2d 3e 75 2e 76 74 61 62 pLoop->u.vtab
1c920 2e 69 64 78 4e 75 6d 2c 20 70 4c 6f 6f 70 2d 3e .idxNum, pLoop->
1c930 75 2e 76 74 61 62 2e 69 64 78 53 74 72 29 3b 0a u.vtab.idxStr);.
1c940 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 }.#endif.
1c950 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d zMsg = sqlite3M
1c960 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 Appendf(db, zMsg
1c970 2c 20 22 25 73 22 2c 20 7a 4d 73 67 29 3b 0a 20 , "%s", zMsg);.
1c980 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
1c990 64 4f 70 34 28 76 2c 20 4f 50 5f 45 78 70 6c 61 dOp4(v, OP_Expla
1c9a0 69 6e 2c 20 69 49 64 2c 20 69 4c 65 76 65 6c 2c in, iId, iLevel,
1c9b0 20 69 46 72 6f 6d 2c 20 7a 4d 73 67 2c 20 50 34 iFrom, zMsg, P4
1c9c0 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d _DYNAMIC);. }.}
1c9d0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 .#else.# define
1c9e0 65 78 70 6c 61 69 6e 4f 6e 65 53 63 61 6e 28 75 explainOneScan(u
1c9f0 2c 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 ,v,w,x,y,z).#end
1ca00 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
1ca10 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 0a 0a 2f T_EXPLAIN */.../
1ca20 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f *.** Generate co
1ca30 64 65 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 de for the start
1ca40 20 6f 66 20 74 68 65 20 69 4c 65 76 65 6c 2d 74 of the iLevel-t
1ca50 68 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 h loop in the WH
1ca60 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 69 6d ERE clause.** im
1ca70 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 65 73 plementation des
1ca80 63 72 69 62 65 64 20 62 79 20 70 57 49 6e 66 6f cribed by pWInfo
1ca90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74 6d ..*/.static Bitm
1caa0 61 73 6b 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 ask codeOneLoopS
1cab0 74 61 72 74 28 0a 20 20 57 68 65 72 65 49 6e 66 tart(. WhereInf
1cac0 6f 20 2a 70 57 49 6e 66 6f 2c 20 20 20 2f 2a 20 o *pWInfo, /*
1cad0 43 6f 6d 70 6c 65 74 65 20 69 6e 66 6f 72 6d 61 Complete informa
1cae0 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 tion about the W
1caf0 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
1cb00 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 20 int iLevel,
1cb10 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 6c /* Which l
1cb20 65 76 65 6c 20 6f 66 20 70 57 49 6e 66 6f 2d 3e evel of pWInfo->
1cb30 61 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 63 6f a[] should be co
1cb40 64 65 64 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b ded */. Bitmask
1cb50 20 6e 6f 74 52 65 61 64 79 20 20 20 20 20 2f 2a notReady /*
1cb60 20 57 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 Which tables ar
1cb70 65 20 63 75 72 72 65 6e 74 6c 79 20 61 76 61 69 e currently avai
1cb80 6c 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e lable */.){. in
1cb90 74 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 t j, k;
1cba0 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 /* Loop count
1cbb0 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 ers */. int iCu
1cbc0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a r; /*
1cbd0 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 The VDBE cursor
1cbe0 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a for the table *
1cbf0 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b /. int addrNxt;
1cc00 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 /* Wher
1cc10 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20 63 6f 6e e to jump to con
1cc20 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e tinue with the n
1cc30 65 78 74 20 49 4e 20 63 61 73 65 20 2a 2f 0a 20 ext IN case */.
1cc40 20 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20 int omitTable;
1cc50 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
1cc60 20 77 65 20 75 73 65 20 74 68 65 20 69 6e 64 65 we use the inde
1cc70 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 x only */. int
1cc80 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 bRev;
1cc90 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e /* True if we n
1cca0 65 65 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 eed to scan in r
1ccb0 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a everse order */.
1ccc0 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c WhereLevel *pL
1ccd0 65 76 65 6c 3b 20 20 2f 2a 20 54 68 65 20 77 68 evel; /* The wh
1cce0 65 72 65 20 6c 65 76 65 6c 20 74 6f 20 62 65 20 ere level to be
1ccf0 63 6f 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 coded */. Where
1cd00 4c 6f 6f 70 20 2a 70 4c 6f 6f 70 3b 20 20 20 20 Loop *pLoop;
1cd10 2f 2a 20 54 68 65 20 57 68 65 72 65 4c 6f 6f 70 /* The WhereLoop
1cd20 20 6f 62 6a 65 63 74 20 62 65 69 6e 67 20 63 6f object being co
1cd30 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c ded */. WhereCl
1cd40 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 2f 2a ause *pWC; /*
1cd50 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f Decomposition o
1cd60 66 20 74 68 65 20 65 6e 74 69 72 65 20 57 48 45 f the entire WHE
1cd70 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 RE clause */. W
1cd80 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b hereTerm *pTerm;
1cd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cda0 2a 20 41 20 57 48 45 52 45 20 63 6c 61 75 73 65 * A WHERE clause
1cdb0 20 74 65 72 6d 20 2a 2f 0a 20 20 50 61 72 73 65 term */. Parse
1cdc0 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 *pParse;
1cdd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 /* Pa
1cde0 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
1cdf0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
1ce00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ce10 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 /* Database c
1ce20 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 56 onnection */. V
1ce30 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 dbe *v;
1ce40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1ce50 2a 20 54 68 65 20 70 72 65 70 61 72 65 64 20 73 * The prepared s
1ce60 74 6d 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 tmt under constr
1ce70 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 uctions */. str
1ce80 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
1ce90 20 2a 70 54 61 62 49 74 65 6d 3b 20 20 2f 2a 20 *pTabItem; /*
1cea0 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d FROM clause term
1ceb0 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a being coded */.
1cec0 20 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 int addrBrk;
1ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1cee0 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 /* Jump here t
1cef0 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 74 o break out of t
1cf00 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 he loop */. int
1cf10 20 61 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 addrCont;
1cf20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cf30 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f 6e Jump here to con
1cf40 74 69 6e 75 65 20 77 69 74 68 20 6e 65 78 74 20 tinue with next
1cf50 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 cycle */. int i
1cf60 52 6f 77 69 64 52 65 67 20 3d 20 30 3b 20 20 20 RowidReg = 0;
1cf70 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 69 73 /* Rowid is
1cf80 20 73 74 6f 72 65 64 20 69 6e 20 74 68 69 73 20 stored in this
1cf90 72 65 67 69 73 74 65 72 2c 20 69 66 20 6e 6f 74 register, if not
1cfa0 20 7a 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 zero */. int i
1cfb0 52 65 6c 65 61 73 65 52 65 67 20 3d 20 30 3b 20 ReleaseReg = 0;
1cfc0 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 /* Temp reg
1cfd0 69 73 74 65 72 20 74 6f 20 66 72 65 65 20 62 65 ister to free be
1cfe0 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 2a fore returning *
1cff0 2f 0a 0a 20 20 70 50 61 72 73 65 20 3d 20 70 57 /.. pParse = pW
1d000 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 Info->pParse;.
1d010 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 v = pParse->pVdb
1d020 65 3b 0a 20 20 70 57 43 20 3d 20 26 70 57 49 6e e;. pWC = &pWIn
1d030 66 6f 2d 3e 73 57 43 3b 0a 20 20 64 62 20 3d 20 fo->sWC;. db =
1d040 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 4c pParse->db;. pL
1d050 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d 3e evel = &pWInfo->
1d060 61 5b 69 4c 65 76 65 6c 5d 3b 0a 20 20 70 4c 6f a[iLevel];. pLo
1d070 6f 70 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 57 4c op = pLevel->pWL
1d080 6f 6f 70 3b 0a 20 20 70 54 61 62 49 74 65 6d 20 oop;. pTabItem
1d090 3d 20 26 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c = &pWInfo->pTabL
1d0a0 69 73 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 ist->a[pLevel->i
1d0b0 46 72 6f 6d 5d 3b 0a 20 20 69 43 75 72 20 3d 20 From];. iCur =
1d0c0 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f pTabItem->iCurso
1d0d0 72 3b 0a 20 20 70 4c 65 76 65 6c 2d 3e 6e 6f 74 r;. pLevel->not
1d0e0 52 65 61 64 79 20 3d 20 6e 6f 74 52 65 61 64 79 Ready = notReady
1d0f0 20 26 20 7e 67 65 74 4d 61 73 6b 28 26 70 57 49 & ~getMask(&pWI
1d100 6e 66 6f 2d 3e 73 4d 61 73 6b 53 65 74 2c 20 69 nfo->sMaskSet, i
1d110 43 75 72 29 3b 0a 20 20 62 52 65 76 20 3d 20 28 Cur);. bRev = (
1d120 70 57 49 6e 66 6f 2d 3e 72 65 76 4d 61 73 6b 3e pWInfo->revMask>
1d130 3e 69 4c 65 76 65 6c 29 26 31 3b 0a 20 20 6f 6d >iLevel)&1;. om
1d140 69 74 54 61 62 6c 65 20 3d 20 28 70 4c 6f 6f 70 itTable = (pLoop
1d150 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 ->wsFlags & WHER
1d160 45 5f 49 44 58 5f 4f 4e 4c 59 29 21 3d 30 20 0a E_IDX_ONLY)!=0 .
1d170 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 70 && (p
1d180 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 WInfo->wctrlFlag
1d190 73 20 26 20 57 48 45 52 45 5f 46 4f 52 43 45 5f s & WHERE_FORCE_
1d1a0 54 41 42 4c 45 29 3d 3d 30 3b 0a 20 20 56 64 62 TABLE)==0;. Vdb
1d1b0 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c eNoopComment((v,
1d1c0 20 22 42 65 67 69 6e 20 4a 6f 69 6e 20 4c 6f 6f "Begin Join Loo
1d1d0 70 20 25 64 22 2c 20 69 4c 65 76 65 6c 29 29 3b p %d", iLevel));
1d1e0 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 6c 61 .. /* Create la
1d1f0 62 65 6c 73 20 66 6f 72 20 74 68 65 20 22 62 72 bels for the "br
1d200 65 61 6b 22 20 61 6e 64 20 22 63 6f 6e 74 69 6e eak" and "contin
1d210 75 65 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 ue" instructions
1d220 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 63 75 . ** for the cu
1d230 72 72 65 6e 74 20 6c 6f 6f 70 2e 20 20 4a 75 6d rrent loop. Jum
1d240 70 20 74 6f 20 61 64 64 72 42 72 6b 20 74 6f 20 p to addrBrk to
1d250 62 72 65 61 6b 20 6f 75 74 20 6f 66 20 61 20 6c break out of a l
1d260 6f 6f 70 2e 0a 20 20 2a 2a 20 4a 75 6d 70 20 74 oop.. ** Jump t
1d270 6f 20 63 6f 6e 74 20 74 6f 20 67 6f 20 69 6d 6d o cont to go imm
1d280 65 64 69 61 74 65 6c 79 20 74 6f 20 74 68 65 20 ediately to the
1d290 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f next iteration o
1d2a0 66 20 74 68 65 0a 20 20 2a 2a 20 6c 6f 6f 70 2e f the. ** loop.
1d2b0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 . **. ** When
1d2c0 74 68 65 72 65 20 69 73 20 61 6e 20 49 4e 20 6f there is an IN o
1d2d0 70 65 72 61 74 6f 72 2c 20 77 65 20 61 6c 73 6f perator, we also
1d2e0 20 68 61 76 65 20 61 20 22 61 64 64 72 4e 78 74 have a "addrNxt
1d2f0 22 20 6c 61 62 65 6c 20 74 68 61 74 0a 20 20 2a " label that. *
1d300 2a 20 6d 65 61 6e 73 20 74 6f 20 63 6f 6e 74 69 * means to conti
1d310 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 nue with the nex
1d320 74 20 49 4e 20 76 61 6c 75 65 20 63 6f 6d 62 69 t IN value combi
1d330 6e 61 74 69 6f 6e 2e 20 20 57 68 65 6e 0a 20 20 nation. When.
1d340 2a 2a 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 ** there are no
1d350 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 IN operators in
1d360 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c the constraints,
1d370 20 74 68 65 20 22 61 64 64 72 4e 78 74 22 20 6c the "addrNxt" l
1d380 61 62 65 6c 0a 20 20 2a 2a 20 69 73 20 74 68 65 abel. ** is the
1d390 20 73 61 6d 65 20 61 73 20 22 61 64 64 72 42 72 same as "addrBr
1d3a0 6b 22 2e 0a 20 20 2a 2f 0a 20 20 61 64 64 72 42 k".. */. addrB
1d3b0 72 6b 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 rk = pLevel->add
1d3c0 72 42 72 6b 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 rBrk = pLevel->a
1d3d0 64 64 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33 ddrNxt = sqlite3
1d3e0 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 VdbeMakeLabel(v)
1d3f0 3b 0a 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 70 ;. addrCont = p
1d400 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 20 Level->addrCont
1d410 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
1d420 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a eLabel(v);.. /*
1d430 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 If this is the
1d440 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 right table of a
1d450 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e LEFT OUTER JOIN
1d460 2c 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 0a 20 , allocate and.
1d470 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 ** initialize a
1d480 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 memory cell tha
1d490 74 20 72 65 63 6f 72 64 73 20 69 66 20 74 68 69 t records if thi
1d4a0 73 20 74 61 62 6c 65 20 6d 61 74 63 68 65 73 20 s table matches
1d4b0 61 6e 79 0a 20 20 2a 2a 20 72 6f 77 20 6f 66 20 any. ** row of
1d4c0 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f the left table o
1d4d0 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f f the join.. */
1d4e0 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 . if( pLevel->i
1d4f0 46 72 6f 6d 3e 30 20 26 26 20 28 70 54 61 62 49 From>0 && (pTabI
1d500 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 tem[0].jointype
1d510 26 20 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29 7b & JT_LEFT)!=0 ){
1d520 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 . pLevel->iLe
1d530 66 74 4a 6f 69 6e 20 3d 20 2b 2b 70 50 61 72 73 ftJoin = ++pPars
1d540 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c e->nMem;. sql
1d550 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
1d560 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c , OP_Integer, 0,
1d570 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f pLevel->iLeftJo
1d580 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d in);. VdbeCom
1d590 6d 65 6e 74 28 28 76 2c 20 22 69 6e 69 74 20 4c ment((v, "init L
1d5a0 45 46 54 20 4a 4f 49 4e 20 6e 6f 2d 6d 61 74 63 EFT JOIN no-matc
1d5b0 68 20 66 6c 61 67 22 29 29 3b 0a 20 20 7d 0a 0a h flag"));. }..
1d5c0 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 /* Special cas
1d5d0 65 20 6f 66 20 61 20 46 52 4f 4d 20 63 6c 61 75 e of a FROM clau
1d5e0 73 65 20 73 75 62 71 75 65 72 79 20 69 6d 70 6c se subquery impl
1d5f0 65 6d 65 6e 74 65 64 20 61 73 20 61 20 63 6f 2d emented as a co-
1d600 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 66 28 routine */. if(
1d610 20 70 54 61 62 49 74 65 6d 2d 3e 76 69 61 43 6f pTabItem->viaCo
1d620 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 69 routine ){. i
1d630 6e 74 20 72 65 67 59 69 65 6c 64 20 3d 20 70 54 nt regYield = pT
1d640 61 62 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 72 abItem->regRetur
1d650 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 n;. sqlite3Vd
1d660 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 beAddOp2(v, OP_I
1d670 6e 74 65 67 65 72 2c 20 70 54 61 62 49 74 65 6d nteger, pTabItem
1d680 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 2d 31 2c ->addrFillSub-1,
1d690 20 72 65 67 59 69 65 6c 64 29 3b 0a 20 20 20 20 regYield);.
1d6a0 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 20 73 71 pLevel->p2 = sq
1d6b0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
1d6c0 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 v, OP_Yield, reg
1d6d0 59 69 65 6c 64 29 3b 0a 20 20 20 20 56 64 62 65 Yield);. Vdbe
1d6e0 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6e 65 78 Comment((v, "nex
1d6f0 74 20 72 6f 77 20 6f 66 20 63 6f 2d 72 6f 75 74 t row of co-rout
1d700 69 6e 65 20 25 73 22 2c 20 70 54 61 62 49 74 65 ine %s", pTabIte
1d710 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 m->pTab->zName))
1d720 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1d730 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 eAddOp2(v, OP_If
1d740 2c 20 72 65 67 59 69 65 6c 64 2b 31 2c 20 61 64 , regYield+1, ad
1d750 64 72 42 72 6b 29 3b 0a 20 20 20 20 70 4c 65 76 drBrk);. pLev
1d760 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 47 6f 74 6f el->op = OP_Goto
1d770 3b 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66 6e 64 ;. }else..#ifnd
1d780 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 ef SQLITE_OMIT_V
1d790 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 IRTUALTABLE. if
1d7a0 28 20 20 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 ( (pLoop->wsFla
1d7b0 67 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 gs & WHERE_VIRTU
1d7c0 41 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 ALTABLE)!=0 ){.
1d7d0 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 54 /* Case 1: T
1d7e0 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69 he table is a vi
1d7f0 72 74 75 61 6c 2d 74 61 62 6c 65 2e 20 20 55 73 rtual-table. Us
1d800 65 20 74 68 65 20 56 46 69 6c 74 65 72 20 61 6e e the VFilter an
1d810 64 20 56 4e 65 78 74 0a 20 20 20 20 2a 2a 20 20 d VNext. **
1d820 20 20 20 20 20 20 20 20 74 6f 20 61 63 63 65 73 to acces
1d830 73 20 74 68 65 20 64 61 74 61 2e 0a 20 20 20 20 s the data..
1d840 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b */. int iReg;
1d850 20 20 20 2f 2a 20 50 33 20 56 61 6c 75 65 20 66 /* P3 Value f
1d860 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20 2a 2f or OP_VFilter */
1d870 0a 20 20 20 20 69 6e 74 20 61 64 64 72 4e 6f 74 . int addrNot
1d880 46 6f 75 6e 64 3b 0a 20 20 20 20 69 6e 74 20 6e Found;. int n
1d890 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 4c 6f Constraint = pLo
1d8a0 6f 70 2d 3e 6e 4c 54 65 72 6d 3b 0a 0a 20 20 20 op->nLTerm;..
1d8b0 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
1d8c0 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20 ePush(pParse);.
1d8d0 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 iReg = sqlite
1d8e0 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 3GetTempRange(pP
1d8f0 61 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e arse, nConstrain
1d900 74 2b 32 29 3b 0a 20 20 20 20 61 64 64 72 4e 6f t+2);. addrNo
1d910 74 46 6f 75 6e 64 20 3d 20 70 4c 65 76 65 6c 2d tFound = pLevel-
1d920 3e 61 64 64 72 42 72 6b 3b 0a 20 20 20 20 66 6f >addrBrk;. fo
1d930 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74 72 r(j=0; j<nConstr
1d940 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 aint; j++){.
1d950 20 20 69 6e 74 20 69 54 61 72 67 65 74 20 3d 20 int iTarget =
1d960 69 52 65 67 2b 6a 2b 32 3b 0a 20 20 20 20 20 20 iReg+j+2;.
1d970 70 54 65 72 6d 20 3d 20 70 4c 6f 6f 70 2d 3e 61 pTerm = pLoop->a
1d980 4c 54 65 72 6d 5b 6a 5d 3b 0a 20 20 20 20 20 20 LTerm[j];.
1d990 69 66 28 20 70 54 65 72 6d 3d 3d 30 20 29 20 63 if( pTerm==0 ) c
1d9a0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 ontinue;. i
1d9b0 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 f( pTerm->eOpera
1d9c0 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a 20 tor & WO_IN ){.
1d9d0 20 20 20 20 20 20 20 63 6f 64 65 45 71 75 61 6c codeEqual
1d9e0 69 74 79 54 65 72 6d 28 70 50 61 72 73 65 2c 20 ityTerm(pParse,
1d9f0 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 6a pTerm, pLevel, j
1da00 2c 20 62 52 65 76 2c 20 69 54 61 72 67 65 74 29 , bRev, iTarget)
1da10 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72 4e 6f ;. addrNo
1da20 74 46 6f 75 6e 64 20 3d 20 70 4c 65 76 65 6c 2d tFound = pLevel-
1da30 3e 61 64 64 72 4e 78 74 3b 0a 20 20 20 20 20 20 >addrNxt;.
1da40 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
1da50 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
1da60 50 61 72 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45 Parse, pTerm->pE
1da70 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69 54 61 xpr->pRight, iTa
1da80 72 67 65 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 rget);. }.
1da90 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
1daa0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
1dab0 5f 49 6e 74 65 67 65 72 2c 20 70 4c 6f 6f 70 2d _Integer, pLoop-
1dac0 3e 75 2e 76 74 61 62 2e 69 64 78 4e 75 6d 2c 20 >u.vtab.idxNum,
1dad0 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 iReg);. sqlit
1dae0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
1daf0 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6e 43 6f 6e OP_Integer, nCon
1db00 73 74 72 61 69 6e 74 2c 20 69 52 65 67 2b 31 29 straint, iReg+1)
1db10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1db20 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 46 eAddOp4(v, OP_VF
1db30 69 6c 74 65 72 2c 20 69 43 75 72 2c 20 61 64 64 ilter, iCur, add
1db40 72 4e 6f 74 46 6f 75 6e 64 2c 20 69 52 65 67 2c rNotFound, iReg,
1db50 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
1db60 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 75 2e pLoop->u.
1db70 76 74 61 62 2e 69 64 78 53 74 72 2c 0a 20 20 20 vtab.idxStr,.
1db80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1db90 20 20 20 70 4c 6f 6f 70 2d 3e 75 2e 76 74 61 62 pLoop->u.vtab
1dba0 2e 6e 65 65 64 46 72 65 65 20 3f 20 50 34 5f 4d .needFree ? P4_M
1dbb0 50 52 49 4e 54 46 20 3a 20 50 34 5f 53 54 41 54 PRINTF : P4_STAT
1dbc0 49 43 29 3b 0a 20 20 20 20 70 4c 6f 6f 70 2d 3e IC);. pLoop->
1dbd0 75 2e 76 74 61 62 2e 6e 65 65 64 46 72 65 65 20 u.vtab.needFree
1dbe0 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 = 0;. for(j=0
1dbf0 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 20 ; j<nConstraint
1dc00 26 26 20 6a 3c 31 36 3b 20 6a 2b 2b 29 7b 0a 20 && j<16; j++){.
1dc10 20 20 20 20 20 69 66 28 20 28 70 4c 6f 6f 70 2d if( (pLoop-
1dc20 3e 75 2e 76 74 61 62 2e 6f 6d 69 74 4d 61 73 6b >u.vtab.omitMask
1dc30 3e 3e 6a 29 26 31 20 29 7b 0a 20 20 20 20 20 20 >>j)&1 ){.
1dc40 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c disableTerm(pL
1dc50 65 76 65 6c 2c 20 70 4c 6f 6f 70 2d 3e 61 4c 54 evel, pLoop->aLT
1dc60 65 72 6d 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d erm[j]);. }
1dc70 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 . }. pLeve
1dc80 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56 4e 65 78 74 l->op = OP_VNext
1dc90 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 ;. pLevel->p1
1dca0 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 4c 65 = iCur;. pLe
1dcb0 76 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 vel->p2 = sqlite
1dcc0 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 3VdbeCurrentAddr
1dcd0 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 (v);. sqlite3
1dce0 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 ReleaseTempRange
1dcf0 28 70 50 61 72 73 65 2c 20 69 52 65 67 2c 20 6e (pParse, iReg, n
1dd00 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b 0a 20 Constraint+2);.
1dd10 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
1dd20 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 chePop(pParse, 1
1dd30 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 );. }else.#endi
1dd40 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 f /* SQLITE_OMIT
1dd50 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f _VIRTUALTABLE */
1dd60 0a 0a 20 20 69 66 28 20 28 70 4c 6f 6f 70 2d 3e .. if( (pLoop->
1dd70 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f wsFlags & WHERE_
1dd80 49 50 4b 29 21 3d 30 0a 20 20 20 26 26 20 28 70 IPK)!=0. && (p
1dd90 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 Loop->wsFlags &
1dda0 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e (WHERE_COLUMN_IN
1ddb0 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 |WHERE_COLUMN_EQ
1ddc0 29 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 2f ))!=0. ){. /
1ddd0 2a 20 43 61 73 65 20 32 3a 20 20 57 65 20 63 61 * Case 2: We ca
1dde0 6e 20 64 69 72 65 63 74 6c 79 20 72 65 66 65 72 n directly refer
1ddf0 65 6e 63 65 20 61 20 73 69 6e 67 6c 65 20 72 6f ence a single ro
1de00 77 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 2a w using an. *
1de10 2a 20 20 20 20 20 20 20 20 20 20 65 71 75 61 6c * equal
1de20 69 74 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 ity comparison a
1de30 67 61 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44 gainst the ROWID
1de40 20 66 69 65 6c 64 2e 20 20 4f 72 0a 20 20 20 20 field. Or.
1de50 2a 2a 20 20 20 20 20 20 20 20 20 20 77 65 20 72 ** we r
1de60 65 66 65 72 65 6e 63 65 20 6d 75 6c 74 69 70 6c eference multipl
1de70 65 20 72 6f 77 73 20 75 73 69 6e 67 20 61 20 22 e rows using a "
1de80 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 22 0a rowid IN (...)".
1de90 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
1dea0 63 6f 6e 73 74 72 75 63 74 2e 0a 20 20 20 20 2a construct.. *
1deb0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c /. assert( pL
1dec0 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 oop->u.btree.nEq
1ded0 3d 3d 31 20 29 3b 0a 20 20 20 20 69 52 65 6c 65 ==1 );. iRele
1dee0 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74 65 33 aseReg = sqlite3
1def0 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
1df00 65 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 e);. pTerm =
1df10 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d 5b 30 5d pLoop->aLTerm[0]
1df20 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 ;. assert( pT
1df30 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 erm!=0 );. as
1df40 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78 sert( pTerm->pEx
1df50 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 pr!=0 );. ass
1df60 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d ert( omitTable==
1df70 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 0 );. testcas
1df80 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 e( pTerm->wtFlag
1df90 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c s & TERM_VIRTUAL
1dfa0 20 29 3b 0a 20 20 20 20 69 52 6f 77 69 64 52 65 );. iRowidRe
1dfb0 67 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 g = codeEquality
1dfc0 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 Term(pParse, pTe
1dfd0 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 30 2c 20 62 rm, pLevel, 0, b
1dfe0 52 65 76 2c 20 69 52 65 6c 65 61 73 65 52 65 67 Rev, iReleaseReg
1dff0 29 3b 0a 20 20 20 20 61 64 64 72 4e 78 74 20 3d );. addrNxt =
1e000 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 pLevel->addrNxt
1e010 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
1e020 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 eAddOp2(v, OP_Mu
1e030 73 74 42 65 49 6e 74 2c 20 69 52 6f 77 69 64 52 stBeInt, iRowidR
1e040 65 67 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20 eg, addrNxt);.
1e050 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1e060 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 Op3(v, OP_NotExi
1e070 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72 4e sts, iCur, addrN
1e080 78 74 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a xt, iRowidReg);.
1e090 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
1e0a0 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e acheAffinityChan
1e0b0 67 65 28 70 50 61 72 73 65 2c 20 69 52 6f 77 69 ge(pParse, iRowi
1e0c0 64 52 65 67 2c 20 31 29 3b 0a 20 20 20 20 73 71 dReg, 1);. sq
1e0d0 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 lite3ExprCacheSt
1e0e0 6f 72 65 28 70 50 61 72 73 65 2c 20 69 43 75 72 ore(pParse, iCur
1e0f0 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52 65 67 29 , -1, iRowidReg)
1e100 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e ;. VdbeCommen
1e110 74 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 t((v, "pk"));.
1e120 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f pLevel->op = O
1e130 50 5f 4e 6f 6f 70 3b 0a 20 20 7d 65 6c 73 65 20 P_Noop;. }else
1e140 69 66 28 20 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c if( (pLoop->wsFl
1e150 61 67 73 20 26 20 57 48 45 52 45 5f 49 50 4b 29 ags & WHERE_IPK)
1e160 21 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 !=0. &&
1e170 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 (pLoop->wsFlags
1e180 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 & WHERE_COLUMN_R
1e190 41 4e 47 45 29 21 3d 30 0a 20 20 29 7b 0a 20 20 ANGE)!=0. ){.
1e1a0 20 20 2f 2a 20 43 61 73 65 20 33 3a 20 20 57 65 /* Case 3: We
1e1b0 20 68 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c have an inequal
1e1c0 69 74 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 ity comparison a
1e1d0 67 61 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44 gainst the ROWID
1e1e0 20 66 69 65 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 field.. */.
1e1f0 20 20 20 69 6e 74 20 74 65 73 74 4f 70 20 3d 20 int testOp =
1e200 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 69 6e 74 OP_Noop;. int
1e210 20 73 74 61 72 74 3b 0a 20 20 20 20 69 6e 74 20 start;. int
1e220 6d 65 6d 45 6e 64 56 61 6c 75 65 20 3d 20 30 3b memEndValue = 0;
1e230 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a . WhereTerm *
1e240 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a pStart, *pEnd;..
1e250 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 assert( omit
1e260 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 Table==0 );.
1e270 6a 20 3d 20 30 3b 0a 20 20 20 20 70 53 74 61 72 j = 0;. pStar
1e280 74 20 3d 20 70 45 6e 64 20 3d 20 30 3b 0a 20 20 t = pEnd = 0;.
1e290 20 20 69 66 28 20 70 4c 6f 6f 70 2d 3e 77 73 46 if( pLoop->wsF
1e2a0 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42 54 4d lags & WHERE_BTM
1e2b0 5f 4c 49 4d 49 54 20 29 20 70 53 74 61 72 74 20 _LIMIT ) pStart
1e2c0 3d 20 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d 5b = pLoop->aLTerm[
1e2d0 6a 2b 2b 5d 3b 0a 20 20 20 20 69 66 28 20 70 4c j++];. if( pL
1e2e0 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 oop->wsFlags & W
1e2f0 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 HERE_TOP_LIMIT )
1e300 20 70 45 6e 64 20 3d 20 70 4c 6f 6f 70 2d 3e 61 pEnd = pLoop->a
1e310 4c 54 65 72 6d 5b 6a 2b 2b 5d 3b 0a 20 20 20 20 LTerm[j++];.
1e320 61 73 73 65 72 74 28 20 70 53 74 61 72 74 21 3d assert( pStart!=
1e330 30 20 7c 7c 20 70 45 6e 64 21 3d 30 20 29 3b 0a 0 || pEnd!=0 );.
1e340 20 20 20 20 69 66 28 20 62 52 65 76 20 29 7b 0a if( bRev ){.
1e350 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 70 53 pTerm = pS
1e360 74 61 72 74 3b 0a 20 20 20 20 20 20 70 53 74 61 tart;. pSta
1e370 72 74 20 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 rt = pEnd;.
1e380 20 70 45 6e 64 20 3d 20 70 54 65 72 6d 3b 0a 20 pEnd = pTerm;.
1e390 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53 74 }. if( pSt
1e3a0 61 72 74 20 29 7b 0a 20 20 20 20 20 20 45 78 70 art ){. Exp
1e3b0 72 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20 r *pX;
1e3c0 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 /* The expres
1e3d0 73 69 6f 6e 20 74 68 61 74 20 64 65 66 69 6e 65 sion that define
1e3e0 73 20 74 68 65 20 73 74 61 72 74 20 62 6f 75 6e s the start boun
1e3f0 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 d */. int r
1e400 31 2c 20 72 54 65 6d 70 3b 20 20 20 20 20 20 20 1, rTemp;
1e410 20 2f 2a 20 52 65 67 69 73 74 65 72 73 20 66 6f /* Registers fo
1e420 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 74 r holding the st
1e430 61 72 74 20 62 6f 75 6e 64 61 72 79 20 2a 2f 0a art boundary */.
1e440 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 6f . /* The fo
1e450 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e 74 llowing constant
1e460 20 6d 61 70 73 20 54 4b 5f 78 78 20 63 6f 64 65 maps TK_xx code
1e470 73 20 69 6e 74 6f 20 63 6f 72 72 65 73 70 6f 6e s into correspon
1e480 64 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20 73 ding . ** s
1e490 65 65 6b 20 6f 70 63 6f 64 65 73 2e 20 20 49 74 eek opcodes. It
1e4a0 20 64 65 70 65 6e 64 73 20 6f 6e 20 61 20 70 61 depends on a pa
1e4b0 72 74 69 63 75 6c 61 72 20 6f 72 64 65 72 69 6e rticular orderin
1e4c0 67 20 6f 66 20 54 4b 5f 78 78 0a 20 20 20 20 20 g of TK_xx.
1e4d0 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 */. const
1e4e0 75 38 20 61 4d 6f 76 65 4f 70 5b 5d 20 3d 20 7b u8 aMoveOp[] = {
1e4f0 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 . /* T
1e500 4b 5f 47 54 20 2a 2f 20 20 4f 50 5f 53 65 65 6b K_GT */ OP_Seek
1e510 47 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2f Gt,. /
1e520 2a 20 54 4b 5f 4c 45 20 2a 2f 20 20 4f 50 5f 53 * TK_LE */ OP_S
1e530 65 65 6b 4c 65 2c 0a 20 20 20 20 20 20 20 20 20 eekLe,.
1e540 20 20 2f 2a 20 54 4b 5f 4c 54 20 2a 2f 20 20 4f /* TK_LT */ O
1e550 50 5f 53 65 65 6b 4c 74 2c 0a 20 20 20 20 20 20 P_SeekLt,.
1e560 20 20 20 20 20 2f 2a 20 54 4b 5f 47 45 20 2a 2f /* TK_GE */
1e570 20 20 4f 50 5f 53 65 65 6b 47 65 0a 20 20 20 20 OP_SeekGe.
1e580 20 20 7d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 };. asser
1e590 74 28 20 54 4b 5f 4c 45 3d 3d 54 4b 5f 47 54 2b t( TK_LE==TK_GT+
1e5a0 31 20 29 3b 20 20 20 20 20 20 2f 2a 20 4d 61 6b 1 ); /* Mak
1e5b0 65 20 73 75 72 65 20 74 68 65 20 6f 72 64 65 72 e sure the order
1e5c0 69 6e 67 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61 ing.. */. a
1e5d0 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b ssert( TK_LT==TK
1e5e0 5f 47 54 2b 32 20 29 3b 20 20 20 20 20 20 2f 2a _GT+2 ); /*
1e5f0 20 20 2e 2e 2e 20 6f 66 20 74 68 65 20 54 4b 5f ... of the TK_
1e600 78 78 20 76 61 6c 75 65 73 2e 2e 2e 20 2a 2f 0a xx values... */.
1e610 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b assert( TK
1e620 5f 47 45 3d 3d 54 4b 5f 47 54 2b 33 20 29 3b 20 _GE==TK_GT+3 );
1e630 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 69 73 20 /* ... is
1e640 63 6f 72 72 65 63 63 74 2e 20 2a 2f 0a 0a 20 20 correcct. */..
1e650 20 20 20 20 61 73 73 65 72 74 28 20 28 70 53 74 assert( (pSt
1e660 61 72 74 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 art->wtFlags & T
1e670 45 52 4d 5f 56 4e 55 4c 4c 29 3d 3d 30 20 29 3b ERM_VNULL)==0 );
1e680 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
1e690 20 70 53 74 61 72 74 2d 3e 77 74 46 6c 61 67 73 pStart->wtFlags
1e6a0 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 & TERM_VIRTUAL
1e6b0 29 3b 0a 20 20 20 20 20 20 70 58 20 3d 20 70 53 );. pX = pS
1e6c0 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20 20 tart->pExpr;.
1e6d0 20 20 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 assert( pX!=0
1e6e0 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 );. testca
1e6f0 73 65 28 20 70 53 74 61 72 74 2d 3e 6c 65 66 74 se( pStart->left
1e700 43 75 72 73 6f 72 21 3d 69 43 75 72 20 29 3b 20 Cursor!=iCur );
1e710 2f 2a 20 74 72 61 6e 73 69 74 69 76 65 20 63 6f /* transitive co
1e720 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 20 nstraints */.
1e730 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 r1 = sqlite3E
1e740 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 xprCodeTemp(pPar
1e750 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 se, pX->pRight,
1e760 26 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 &rTemp);. s
1e770 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
1e780 28 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58 2d 3e (v, aMoveOp[pX->
1e790 6f 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75 72 2c op-TK_GT], iCur,
1e7a0 20 61 64 64 72 42 72 6b 2c 20 72 31 29 3b 0a 20 addrBrk, r1);.
1e7b0 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 VdbeComment
1e7c0 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 ((v, "pk"));.
1e7d0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
1e7e0 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
1e7f0 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 31 29 e(pParse, r1, 1)
1e800 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
1e810 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
1e820 61 72 73 65 2c 20 72 54 65 6d 70 29 3b 0a 20 20 arse, rTemp);.
1e830 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 disableTerm(
1e840 70 4c 65 76 65 6c 2c 20 70 53 74 61 72 74 29 3b pLevel, pStart);
1e850 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1e860 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
1e870 4f 70 32 28 76 2c 20 62 52 65 76 20 3f 20 4f 50 Op2(v, bRev ? OP
1e880 5f 4c 61 73 74 20 3a 20 4f 50 5f 52 65 77 69 6e _Last : OP_Rewin
1e890 64 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b d, iCur, addrBrk
1e8a0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
1e8b0 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 45 pEnd ){. E
1e8c0 78 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 70 xpr *pX;. p
1e8d0 58 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b X = pEnd->pExpr;
1e8e0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1e8f0 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 X!=0 );. as
1e900 73 65 72 74 28 20 28 70 45 6e 64 2d 3e 77 74 46 sert( (pEnd->wtF
1e910 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 4e 55 4c lags & TERM_VNUL
1e920 4c 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 L)==0 );. t
1e930 65 73 74 63 61 73 65 28 20 70 45 6e 64 2d 3e 6c estcase( pEnd->l
1e940 65 66 74 43 75 72 73 6f 72 21 3d 69 43 75 72 20 eftCursor!=iCur
1e950 29 3b 20 2f 2a 20 54 72 61 6e 73 69 74 69 76 65 ); /* Transitive
1e960 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a constraints */.
1e970 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
1e980 70 45 6e 64 2d 3e 77 74 46 6c 61 67 73 20 26 20 pEnd->wtFlags &
1e990 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a TERM_VIRTUAL );.
1e9a0 20 20 20 20 20 20 6d 65 6d 45 6e 64 56 61 6c 75 memEndValu
1e9b0 65 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d e = ++pParse->nM
1e9c0 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 em;. sqlite
1e9d0 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 3ExprCode(pParse
1e9e0 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 6d 65 , pX->pRight, me
1e9f0 6d 45 6e 64 56 61 6c 75 65 29 3b 0a 20 20 20 20 mEndValue);.
1ea00 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b if( pX->op==TK
1ea10 5f 4c 54 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 _LT || pX->op==T
1ea20 4b 5f 47 54 20 29 7b 0a 20 20 20 20 20 20 20 20 K_GT ){.
1ea30 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 testOp = bRev ?
1ea40 4f 50 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 OP_Le : OP_Ge;.
1ea50 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
1ea60 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65 testOp = bRe
1ea70 76 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47 v ? OP_Lt : OP_G
1ea80 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 t;. }.
1ea90 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 disableTerm(pLe
1eaa0 76 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20 20 20 vel, pEnd);.
1eab0 7d 0a 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 }. start = sq
1eac0 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 lite3VdbeCurrent
1ead0 41 64 64 72 28 76 29 3b 0a 20 20 20 20 70 4c 65 Addr(v);. pLe
1eae0 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f vel->op = bRev ?
1eaf0 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65 OP_Prev : OP_Ne
1eb00 78 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e xt;. pLevel->
1eb10 70 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 p1 = iCur;. p
1eb20 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72 Level->p2 = star
1eb30 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 t;. assert( p
1eb40 4c 65 76 65 6c 2d 3e 70 35 3d 3d 30 20 29 3b 0a Level->p5==0 );.
1eb50 20 20 20 20 69 66 28 20 74 65 73 74 4f 70 21 3d if( testOp!=
1eb60 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 OP_Noop ){.
1eb70 20 69 52 6f 77 69 64 52 65 67 20 3d 20 69 52 65 iRowidReg = iRe
1eb80 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74 leaseReg = sqlit
1eb90 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 e3GetTempReg(pPa
1eba0 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 rse);. sqli
1ebb0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
1ebc0 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c OP_Rowid, iCur,
1ebd0 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 iRowidReg);.
1ebe0 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
1ebf0 63 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c cheStore(pParse,
1ec00 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 iCur, -1, iRowi
1ec10 64 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c dReg);. sql
1ec20 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 ite3VdbeAddOp3(v
1ec30 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d 45 6e 64 , testOp, memEnd
1ec40 56 61 6c 75 65 2c 20 61 64 64 72 42 72 6b 2c 20 Value, addrBrk,
1ec50 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 iRowidReg);.
1ec60 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
1ec70 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f ngeP5(v, SQLITE_
1ec80 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 20 53 51 AFF_NUMERIC | SQ
1ec90 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 LITE_JUMPIFNULL)
1eca0 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 ;. }. }else
1ecb0 69 66 28 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 if( pLoop->wsFla
1ecc0 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 gs & WHERE_INDEX
1ecd0 45 44 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 ED ){. /* Cas
1ece0 65 20 34 3a 20 41 20 73 63 61 6e 20 75 73 69 6e e 4: A scan usin
1ecf0 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 20 20 g an index..
1ed00 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 **. **
1ed10 20 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 The WHERE clau
1ed20 73 65 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a se may contain z
1ed30 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65 71 75 61 ero or more equa
1ed40 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20 20 20 20 lity . **
1ed50 20 20 20 20 20 74 65 72 6d 73 20 28 22 3d 3d 22 terms ("=="
1ed60 20 6f 72 20 22 49 4e 22 20 6f 70 65 72 61 74 6f or "IN" operato
1ed70 72 73 29 20 74 68 61 74 20 72 65 66 65 72 20 74 rs) that refer t
1ed80 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a 2a 20 20 o the N. **
1ed90 20 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 left-most
1eda0 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 columns of the
1edb0 69 6e 64 65 78 2e 20 49 74 20 6d 61 79 20 61 6c index. It may al
1edc0 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a so contain. *
1edd0 2a 20 20 20 20 20 20 20 20 20 69 6e 65 71 75 61 * inequa
1ede0 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 lity constraints
1edf0 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d (>, <, >= or <=
1ee00 29 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 65 64 ) on the indexed
1ee10 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
1ee20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65 column that imme
1ee30 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 diately follows
1ee40 74 68 65 20 4e 20 65 71 75 61 6c 69 74 69 65 73 the N equalities
1ee50 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a 2a 20 20 . Only . **
1ee60 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68 74 the right
1ee70 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63 61 6e -most column can
1ee80 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 be an inequalit
1ee90 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d 75 73 y - the rest mus
1eea0 74 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 t. **
1eeb0 20 75 73 65 20 74 68 65 20 22 3d 3d 22 20 61 6e use the "==" an
1eec0 64 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 d "IN" operators
1eed0 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 . For example, i
1eee0 66 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 f the . **
1eef0 20 20 20 20 20 20 69 6e 64 65 78 20 69 73 20 6f index is o
1ef00 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68 65 6e 20 n (x,y,z), then
1ef10 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c the following cl
1ef20 61 75 73 65 73 20 61 72 65 20 61 6c 6c 20 0a 20 auses are all .
1ef30 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 ** op
1ef40 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20 2a 2a 0a timized:. **.
1ef50 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 **
1ef60 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20 20 20 20 x=5. **
1ef70 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 x=5 AND
1ef80 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 y=10. **
1ef90 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 x=5 AND y
1efa0 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 <10. **
1efb0 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3e x=5 AND y>
1efc0 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a 5 AND y<10. *
1efd0 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 * x=5
1efe0 20 41 4e 44 20 79 3d 35 20 41 4e 44 20 7a 3c 3d AND y=5 AND z<=
1eff0 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 10. **. **
1f000 20 20 20 20 20 20 20 20 20 54 68 65 20 7a 3c 31 The z<1
1f010 30 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 0 term of the fo
1f020 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f 74 20 62 llowing cannot b
1f030 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a 20 20 20 e used, only.
1f040 20 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 ** the
1f050 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20 20 2a 2a x=5 term:. **
1f060 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 . **
1f070 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a x=5 AND z<10.
1f080 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 **. **
1f090 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65 20 7a N may be z
1f0a0 65 72 6f 20 69 66 20 74 68 65 72 65 20 61 72 65 ero if there are
1f0b0 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 inequality cons
1f0c0 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 traints.. **
1f0d0 20 20 20 20 20 20 20 20 49 66 20 74 68 65 72 65 If there
1f0e0 20 61 72 65 20 6e 6f 20 69 6e 65 71 75 61 6c 69 are no inequali
1f0f0 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 ty constraints,
1f100 74 68 65 6e 20 4e 20 69 73 20 61 74 0a 20 20 20 then N is at.
1f110 20 2a 2a 20 20 20 20 20 20 20 20 20 6c 65 61 73 ** leas
1f120 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 t one.. **.
1f130 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 69 ** Thi
1f140 73 20 63 61 73 65 20 69 73 20 61 6c 73 6f 20 75 s case is also u
1f150 73 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 61 sed when there a
1f160 72 65 20 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 re no WHERE clau
1f170 73 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 se. **
1f180 20 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 constraints bu
1f190 74 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 65 t an index is se
1f1a0 6c 65 63 74 65 64 20 61 6e 79 77 61 79 2c 20 69 lected anyway, i
1f1b0 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 20 n order. **
1f1c0 20 20 20 20 20 20 20 74 6f 20 66 6f 72 63 65 20 to force
1f1d0 74 68 65 20 6f 75 74 70 75 74 20 6f 72 64 65 72 the output order
1f1e0 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61 to conform to a
1f1f0 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20 20 20 20 n ORDER BY..
1f200 2a 2f 20 20 0a 20 20 20 20 73 74 61 74 69 63 20 */ . static
1f210 63 6f 6e 73 74 20 75 38 20 61 53 74 61 72 74 4f const u8 aStartO
1f220 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30 2c p[] = {. 0,
1f230 0a 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 . 0,.
1f240 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20 OP_Rewind,
1f250 20 20 20 20 20 2f 2a 20 32 3a 20 28 21 73 74 61 /* 2: (!sta
1f260 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 rt_constraints &
1f270 26 20 73 74 61 72 74 45 71 20 26 26 20 20 21 62 & startEq && !b
1f280 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 Rev) */. OP
1f290 5f 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20 20 _Last,
1f2a0 20 20 20 2f 2a 20 33 3a 20 28 21 73 74 61 72 74 /* 3: (!start
1f2b0 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 20 _constraints &&
1f2c0 73 74 61 72 74 45 71 20 26 26 20 20 20 62 52 65 startEq && bRe
1f2d0 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 v) */. OP_S
1f2e0 65 65 6b 47 74 2c 20 20 20 20 20 20 20 20 20 20 eekGt,
1f2f0 20 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f 63 6f /* 4: (start_co
1f300 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 nstraints && !s
1f310 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76 29 tartEq && !bRev)
1f320 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 */. OP_See
1f330 6b 4c 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f kLt, /
1f340 2a 20 35 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 * 5: (start_cons
1f350 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74 61 traints && !sta
1f360 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20 2a rtEq && bRev) *
1f370 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 47 /. OP_SeekG
1f380 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 e, /*
1f390 36 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 6: (start_constr
1f3a0 61 69 6e 74 73 20 20 26 26 20 20 73 74 61 72 74 aints && start
1f3b0 45 71 20 26 26 20 21 62 52 65 76 29 20 2a 2f 0a Eq && !bRev) */.
1f3c0 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 4c 65 20 OP_SeekLe
1f3d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 37 3a /* 7:
1f3e0 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 (start_constrai
1f3f0 6e 74 73 20 20 26 26 20 20 73 74 61 72 74 45 71 nts && startEq
1f400 20 26 26 20 20 62 52 65 76 29 20 2a 2f 0a 20 20 && bRev) */.
1f410 20 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 20 };. static
1f420 63 6f 6e 73 74 20 75 38 20 61 45 6e 64 4f 70 5b const u8 aEndOp[
1f430 5d 20 3d 20 7b 0a 20 20 20 20 20 20 4f 50 5f 4e ] = {. OP_N
1f440 6f 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 oop,
1f450 20 2f 2a 20 30 3a 20 28 21 65 6e 64 5f 63 6f 6e /* 0: (!end_con
1f460 73 74 72 61 69 6e 74 73 29 20 2a 2f 0a 20 20 20 straints) */.
1f470 20 20 20 4f 50 5f 49 64 78 47 45 2c 20 20 20 20 OP_IdxGE,
1f480 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 28 65 /* 1: (e
1f490 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 nd_constraints &
1f4a0 26 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 & !bRev) */.
1f4b0 20 20 4f 50 5f 49 64 78 4c 54 20 20 20 20 20 20 OP_IdxLT
1f4c0 20 20 20 20 20 20 20 2f 2a 20 32 3a 20 28 65 6e /* 2: (en
1f4d0 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26 d_constraints &&
1f4e0 20 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 7d 3b bRev) */. };
1f4f0 0a 20 20 20 20 69 6e 74 20 6e 45 71 20 3d 20 70 . int nEq = p
1f500 4c 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 Loop->u.btree.nE
1f510 71 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 q; /* Number of
1f520 20 3d 3d 20 6f 72 20 49 4e 20 74 65 72 6d 73 20 == or IN terms
1f530 2a 2f 0a 20 20 20 20 69 6e 74 20 69 73 4d 69 6e */. int isMin
1f540 51 75 65 72 79 20 3d 20 30 3b 20 20 20 20 20 20 Query = 0;
1f550 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 /* If this
1f560 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 65 64 is an optimized
1f570 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 2e 2e SELECT min(x)..
1f580 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 42 */. int regB
1f590 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ase;
1f5a0 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65 67 /* Base reg
1f5b0 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f ister holding co
1f5c0 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65 73 20 nstraint values
1f5d0 2a 2f 0a 20 20 20 20 69 6e 74 20 72 31 3b 20 20 */. int r1;
1f5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f5f0 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67 69 /* Temp regi
1f600 73 74 65 72 20 2a 2f 0a 20 20 20 20 57 68 65 72 ster */. Wher
1f610 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 53 74 61 eTerm *pRangeSta
1f620 72 74 20 3d 20 30 3b 20 20 2f 2a 20 49 6e 65 71 rt = 0; /* Ineq
1f630 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e uality constrain
1f640 74 20 61 74 20 72 61 6e 67 65 20 73 74 61 72 74 t at range start
1f650 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65 72 */. WhereTer
1f660 6d 20 2a 70 52 61 6e 67 65 45 6e 64 20 3d 20 30 m *pRangeEnd = 0
1f670 3b 20 20 20 20 2f 2a 20 49 6e 65 71 75 61 6c 69 ; /* Inequali
1f680 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 ty constraint at
1f690 20 72 61 6e 67 65 20 65 6e 64 20 2a 2f 0a 20 20 range end */.
1f6a0 20 20 69 6e 74 20 73 74 61 72 74 45 71 3b 20 20 int startEq;
1f6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1f6c0 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67 65 20 * True if range
1f6d0 73 74 61 72 74 20 75 73 65 73 20 3d 3d 2c 20 3e start uses ==, >
1f6e0 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 = or <= */. i
1f6f0 6e 74 20 65 6e 64 45 71 3b 20 20 20 20 20 20 20 nt endEq;
1f700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
1f710 72 75 65 20 69 66 20 72 61 6e 67 65 20 65 6e 64 rue if range end
1f720 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 uses ==, >= or
1f730 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74 <= */. int st
1f740 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3b art_constraints;
1f750 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 /* Start
1f760 6f 66 20 72 61 6e 67 65 20 69 73 20 63 6f 6e 73 of range is cons
1f770 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 20 20 69 trained */. i
1f780 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 nt nConstraint;
1f790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e /* N
1f7a0 75 6d 62 65 72 20 6f 66 20 63 6f 6e 73 74 72 61 umber of constra
1f7b0 69 6e 74 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 int terms */.
1f7c0 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 Index *pIdx;
1f7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1f7e0 20 54 68 65 20 69 6e 64 65 78 20 77 65 20 77 69 The index we wi
1f7f0 6c 6c 20 62 65 20 75 73 69 6e 67 20 2a 2f 0a 20 ll be using */.
1f800 20 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 int iIdxCur;
1f810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1f820 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 /* The VDBE curs
1f830 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 or for the index
1f840 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 78 74 */. int nExt
1f850 72 61 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20 raReg = 0;
1f860 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f /* Number o
1f870 66 20 65 78 74 72 61 20 72 65 67 69 73 74 65 72 f extra register
1f880 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 20 20 s needed */.
1f890 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 int op;
1f8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1f8b0 49 6e 73 74 72 75 63 74 69 6f 6e 20 6f 70 63 6f Instruction opco
1f8c0 64 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a de */. char *
1f8d0 7a 53 74 61 72 74 41 66 66 3b 20 20 20 20 20 20 zStartAff;
1f8e0 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 /* Affini
1f8f0 74 79 20 66 6f 72 20 73 74 61 72 74 20 6f 66 20 ty for start of
1f900 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 range constraint
1f910 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 */. char *zE
1f920 6e 64 41 66 66 3b 20 20 20 20 20 20 20 20 20 20 ndAff;
1f930 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 /* Affinity
1f940 20 66 6f 72 20 65 6e 64 20 6f 66 20 72 61 6e 67 for end of rang
1f950 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a e constraint */.
1f960 0a 20 20 20 20 70 49 64 78 20 3d 20 70 4c 6f 6f . pIdx = pLoo
1f970 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 65 p->u.btree.pInde
1f980 78 3b 0a 20 20 20 20 69 49 64 78 43 75 72 20 3d x;. iIdxCur =
1f990 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72 pLevel->iIdxCur
1f9a0 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 ;.. /* If thi
1f9b0 73 20 6c 6f 6f 70 20 73 61 74 69 73 66 69 65 73 s loop satisfies
1f9c0 20 61 20 73 6f 72 74 20 6f 72 64 65 72 20 28 70 a sort order (p
1f9d0 4f 72 64 65 72 42 79 29 20 72 65 71 75 65 73 74 OrderBy) request
1f9e0 20 74 68 61 74 20 0a 20 20 20 20 2a 2a 20 77 61 that . ** wa
1f9f0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 s passed to this
1fa00 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d 70 function to imp
1fa10 6c 65 6d 65 6e 74 20 61 20 22 53 45 4c 45 43 54 lement a "SELECT
1fa20 20 6d 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20 20 min(x) ..." .
1fa30 20 20 2a 2a 20 71 75 65 72 79 2c 20 74 68 65 6e ** query, then
1fa40 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c the caller will
1fa50 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74 68 65 20 only allow the
1fa60 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66 6f 72 0a loop to run for.
1fa70 20 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 ** a single
1fa80 69 74 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 iteration. This
1fa90 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 means that the f
1faa0 69 72 73 74 20 72 6f 77 20 72 65 74 75 72 6e 65 irst row returne
1fab0 64 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 d. ** should
1fac0 6e 6f 74 20 68 61 76 65 20 61 20 4e 55 4c 4c 20 not have a NULL
1fad0 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 value stored in
1fae0 27 78 27 2e 20 49 66 20 63 6f 6c 75 6d 6e 20 27 'x'. If column '
1faf0 78 27 20 69 73 0a 20 20 20 20 2a 2a 20 74 68 65 x' is. ** the
1fb00 20 66 69 72 73 74 20 6f 6e 65 20 61 66 74 65 72 first one after
1fb10 20 74 68 65 20 6e 45 71 20 65 71 75 61 6c 69 74 the nEq equalit
1fb20 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e y constraints in
1fb30 20 74 68 65 20 69 6e 64 65 78 2c 0a 20 20 20 20 the index,.
1fb40 2a 2a 20 74 68 69 73 20 72 65 71 75 69 72 65 73 ** this requires
1fb50 20 73 6f 6d 65 20 73 70 65 63 69 61 6c 20 68 61 some special ha
1fb60 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 ndling.. */.
1fb70 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e if( (pWInfo->
1fb80 77 63 74 72 6c 46 6c 61 67 73 26 57 48 45 52 45 wctrlFlags&WHERE
1fb90 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 29 21 3d 30 _ORDERBY_MIN)!=0
1fba0 0a 20 20 20 20 20 26 26 20 28 70 57 49 6e 66 6f . && (pWInfo
1fbb0 2d 3e 62 4f 42 53 61 74 21 3d 30 29 0a 20 20 20 ->bOBSat!=0).
1fbc0 20 20 26 26 20 28 70 49 64 78 2d 3e 6e 43 6f 6c && (pIdx->nCol
1fbd0 75 6d 6e 3e 6e 45 71 29 0a 20 20 20 20 29 7b 0a umn>nEq). ){.
1fbe0 20 20 20 20 20 20 2f 2a 20 61 73 73 65 72 74 28 /* assert(
1fbf0 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 pOrderBy->nExpr
1fc00 3d 3d 31 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 ==1 ); */.
1fc10 2f 2a 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 /* assert( pOrde
1fc20 72 42 79 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d rBy->a[0].pExpr-
1fc30 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e >iColumn==pIdx->
1fc40 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b aiColumn[nEq] );
1fc50 20 2a 2f 0a 20 20 20 20 20 20 69 73 4d 69 6e 51 */. isMinQ
1fc60 75 65 72 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 uery = 1;.
1fc70 6e 45 78 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 nExtraReg = 1;.
1fc80 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e }.. /* Fin
1fc90 64 20 61 6e 79 20 69 6e 65 71 75 61 6c 69 74 79 d any inequality
1fca0 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d constraint term
1fcb0 73 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 s for the start
1fcc0 61 6e 64 20 65 6e 64 20 0a 20 20 20 20 2a 2a 20 and end . **
1fcd0 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20 0a 20 of the range. .
1fce0 20 20 20 2a 2f 0a 20 20 20 20 6a 20 3d 20 6e 45 */. j = nE
1fcf0 71 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 6f 70 q;. if( pLoop
1fd00 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 ->wsFlags & WHER
1fd10 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 7b 0a 20 E_BTM_LIMIT ){.
1fd20 20 20 20 20 20 70 52 61 6e 67 65 53 74 61 72 74 pRangeStart
1fd30 20 3d 20 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d = pLoop->aLTerm
1fd40 5b 6a 2b 2b 5d 3b 0a 20 20 20 20 20 20 6e 45 78 [j++];. nEx
1fd50 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 traReg = 1;.
1fd60 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f 6f 70 2d }. if( pLoop-
1fd70 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 >wsFlags & WHERE
1fd80 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 _TOP_LIMIT ){.
1fd90 20 20 20 20 70 52 61 6e 67 65 45 6e 64 20 3d 20 pRangeEnd =
1fda0 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d 5b 6a 2b pLoop->aLTerm[j+
1fdb0 2b 5d 3b 0a 20 20 20 20 20 20 6e 45 78 74 72 61 +];. nExtra
1fdc0 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a Reg = 1;. }..
1fdd0 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 /* Generate
1fde0 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61 74 65 code to evaluate
1fdf0 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 20 all constraint
1fe00 74 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d 20 6f terms using == o
1fe10 72 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e 64 20 r IN. ** and
1fe20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 73 store the values
1fe30 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d 73 20 of those terms
1fe40 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 72 in an array of r
1fe50 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a 2a 20 egisters. **
1fe60 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 42 starting at regB
1fe70 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ase.. */.
1fe80 72 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41 6c regBase = codeAl
1fe90 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28 70 lEqualityTerms(p
1fea0 50 61 72 73 65 2c 70 4c 65 76 65 6c 2c 62 52 65 Parse,pLevel,bRe
1feb0 76 2c 6e 45 78 74 72 61 52 65 67 2c 26 7a 53 74 v,nExtraReg,&zSt
1fec0 61 72 74 41 66 66 29 3b 0a 20 20 20 20 7a 45 6e artAff);. zEn
1fed0 64 41 66 66 20 3d 20 73 71 6c 69 74 65 33 44 62 dAff = sqlite3Db
1fee0 53 74 72 44 75 70 28 64 62 2c 20 7a 53 74 61 72 StrDup(db, zStar
1fef0 74 41 66 66 29 3b 0a 20 20 20 20 61 64 64 72 4e tAff);. addrN
1ff00 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 xt = pLevel->add
1ff10 72 4e 78 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 rNxt;.. /* If
1ff20 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 20 we are doing a
1ff30 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 73 63 reverse order sc
1ff40 61 6e 20 6f 6e 20 61 6e 20 61 73 63 65 6e 64 69 an on an ascendi
1ff50 6e 67 20 69 6e 64 65 78 2c 20 6f 72 0a 20 20 20 ng index, or.
1ff60 20 2a 2a 20 61 20 66 6f 72 77 61 72 64 20 6f 72 ** a forward or
1ff70 64 65 72 20 73 63 61 6e 20 6f 6e 20 61 20 64 65 der scan on a de
1ff80 73 63 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 scending index,
1ff90 69 6e 74 65 72 63 68 61 6e 67 65 20 74 68 65 20 interchange the
1ffa0 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 20 61 6e . ** start an
1ffb0 64 20 65 6e 64 20 74 65 72 6d 73 20 28 70 52 61 d end terms (pRa
1ffc0 6e 67 65 53 74 61 72 74 20 61 6e 64 20 70 52 61 ngeStart and pRa
1ffd0 6e 67 65 45 6e 64 29 2e 0a 20 20 20 20 2a 2f 0a ngeEnd).. */.
1ffe0 20 20 20 20 69 66 28 20 28 6e 45 71 3c 70 49 64 if( (nEq<pId
1fff0 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 20 62 52 x->nColumn && bR
20000 65 76 3d 3d 28 70 49 64 78 2d 3e 61 53 6f 72 74 ev==(pIdx->aSort
20010 4f 72 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 Order[nEq]==SQLI
20020 54 45 5f 53 4f 5f 41 53 43 29 29 0a 20 20 20 20 TE_SO_ASC)).
20030 20 7c 7c 20 28 62 52 65 76 20 26 26 20 70 49 64 || (bRev && pId
20040 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 6e 45 71 29 x->nColumn==nEq)
20050 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 53 57 . ){. SW
20060 41 50 28 57 68 65 72 65 54 65 72 6d 20 2a 2c 20 AP(WhereTerm *,
20070 70 52 61 6e 67 65 45 6e 64 2c 20 70 52 61 6e 67 pRangeEnd, pRang
20080 65 53 74 61 72 74 29 3b 0a 20 20 20 20 7d 0a 0a eStart);. }..
20090 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52 testcase( pR
200a0 61 6e 67 65 53 74 61 72 74 20 26 26 20 28 70 52 angeStart && (pR
200b0 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 angeStart->eOper
200c0 61 74 6f 72 20 26 20 57 4f 5f 4c 45 29 21 3d 30 ator & WO_LE)!=0
200d0 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 );. testcase
200e0 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26 ( pRangeStart &&
200f0 20 28 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 (pRangeStart->e
20100 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 Operator & WO_GE
20110 29 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 )!=0 );. test
20120 63 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64 20 case( pRangeEnd
20130 26 26 20 28 70 52 61 6e 67 65 45 6e 64 2d 3e 65 && (pRangeEnd->e
20140 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 Operator & WO_LE
20150 29 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 )!=0 );. test
20160 63 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64 20 case( pRangeEnd
20170 26 26 20 28 70 52 61 6e 67 65 45 6e 64 2d 3e 65 && (pRangeEnd->e
20180 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 Operator & WO_GE
20190 29 21 3d 30 20 29 3b 0a 20 20 20 20 73 74 61 72 )!=0 );. star
201a0 74 45 71 20 3d 20 21 70 52 61 6e 67 65 53 74 61 tEq = !pRangeSta
201b0 72 74 20 7c 7c 20 70 52 61 6e 67 65 53 74 61 72 rt || pRangeStar
201c0 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 t->eOperator & (
201d0 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20 20 WO_LE|WO_GE);.
201e0 20 20 65 6e 64 45 71 20 3d 20 20 20 21 70 52 61 endEq = !pRa
201f0 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61 6e 67 65 ngeEnd || pRange
20200 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 End->eOperator &
20210 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a (WO_LE|WO_GE);.
20220 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 start_constr
20230 61 69 6e 74 73 20 3d 20 70 52 61 6e 67 65 53 74 aints = pRangeSt
20240 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b 0a 0a 20 art || nEq>0;..
20250 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 69 /* Seek the i
20260 6e 64 65 78 20 63 75 72 73 6f 72 20 74 6f 20 74 ndex cursor to t
20270 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 he start of the
20280 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6e 43 range. */. nC
20290 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b onstraint = nEq;
202a0 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65 53 . if( pRangeS
202b0 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 45 78 tart ){. Ex
202c0 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 52 61 pr *pRight = pRa
202d0 6e 67 65 53 74 61 72 74 2d 3e 70 45 78 70 72 2d ngeStart->pExpr-
202e0 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 73 >pRight;. s
202f0 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 qlite3ExprCode(p
20300 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72 Parse, pRight, r
20310 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 egBase+nEq);.
20320 20 20 20 69 66 28 20 28 70 52 61 6e 67 65 53 74 if( (pRangeSt
20330 61 72 74 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 art->wtFlags & T
20340 45 52 4d 5f 56 4e 55 4c 4c 29 3d 3d 30 20 29 7b ERM_VNULL)==0 ){
20350 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
20360 45 78 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 ExprCodeIsNullJu
20370 6d 70 28 76 2c 20 70 52 69 67 68 74 2c 20 72 65 mp(v, pRight, re
20380 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 64 72 4e gBase+nEq, addrN
20390 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 xt);. }.
203a0 20 20 20 69 66 28 20 7a 53 74 61 72 74 41 66 66 if( zStartAff
203b0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ){. if(
203c0 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 sqlite3CompareAf
203d0 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a finity(pRight, z
203e0 53 74 61 72 74 41 66 66 5b 6e 45 71 5d 29 3d 3d StartAff[nEq])==
203f0 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 SQLITE_AFF_NONE)
20400 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 {. /* S
20410 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 ince the compari
20420 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72 son is to be per
20430 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63 formed with no c
20440 6f 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20 20 20 onversions.
20450 20 20 20 20 20 2a 2a 20 61 70 70 6c 69 65 64 20 ** applied
20460 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c to the operands,
20470 20 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74 set the affinit
20480 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52 y to apply to pR
20490 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20 ight to .
204a0 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 ** SQLITE_AFF
204b0 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20 _NONE. */.
204c0 20 20 20 20 20 7a 53 74 61 72 74 41 66 66 5b 6e zStartAff[n
204d0 45 71 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 Eq] = SQLITE_AFF
204e0 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d _NONE;. }
204f0 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c . if( sql
20500 69 74 65 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 ite3ExprNeedsNoA
20510 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 52 ffinityChange(pR
20520 69 67 68 74 2c 20 7a 53 74 61 72 74 41 66 66 5b ight, zStartAff[
20530 6e 45 71 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 nEq]) ){.
20540 20 20 20 7a 53 74 61 72 74 41 66 66 5b 6e 45 71 zStartAff[nEq
20550 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e ] = SQLITE_AFF_N
20560 4f 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ONE;. }.
20570 20 20 20 20 20 7d 20 20 0a 20 20 20 20 20 20 6e } . n
20580 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 Constraint++;.
20590 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52 testcase( pR
205a0 61 6e 67 65 53 74 61 72 74 2d 3e 77 74 46 6c 61 angeStart->wtFla
205b0 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 gs & TERM_VIRTUA
205c0 4c 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 L );. }else i
205d0 66 28 20 69 73 4d 69 6e 51 75 65 72 79 20 29 7b f( isMinQuery ){
205e0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
205f0 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e beAddOp2(v, OP_N
20600 75 6c 6c 2c 20 30 2c 20 72 65 67 42 61 73 65 2b ull, 0, regBase+
20610 6e 45 71 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e nEq);. nCon
20620 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 straint++;.
20630 20 73 74 61 72 74 45 71 20 3d 20 30 3b 0a 20 20 startEq = 0;.
20640 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 start_constr
20650 61 69 6e 74 73 20 3d 20 31 3b 0a 20 20 20 20 7d aints = 1;. }
20660 0a 20 20 20 20 63 6f 64 65 41 70 70 6c 79 41 66 . codeApplyAf
20670 66 69 6e 69 74 79 28 70 50 61 72 73 65 2c 20 72 finity(pParse, r
20680 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 egBase, nConstra
20690 69 6e 74 2c 20 7a 53 74 61 72 74 41 66 66 29 3b int, zStartAff);
206a0 0a 20 20 20 20 6f 70 20 3d 20 61 53 74 61 72 74 . op = aStart
206b0 4f 70 5b 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 Op[(start_constr
206c0 61 69 6e 74 73 3c 3c 32 29 20 2b 20 28 73 74 61 aints<<2) + (sta
206d0 72 74 45 71 3c 3c 31 29 20 2b 20 62 52 65 76 5d rtEq<<1) + bRev]
206e0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 ;. assert( op
206f0 21 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 !=0 );. testc
20700 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 52 65 77 69 ase( op==OP_Rewi
20710 6e 64 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 nd );. testca
20720 73 65 28 20 6f 70 3d 3d 4f 50 5f 4c 61 73 74 20 se( op==OP_Last
20730 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
20740 20 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 op==OP_SeekGt )
20750 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
20760 6f 70 3d 3d 4f 50 5f 53 65 65 6b 47 65 20 29 3b op==OP_SeekGe );
20770 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f . testcase( o
20780 70 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a p==OP_SeekLe );.
20790 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 testcase( op
207a0 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 29 3b 0a 20 ==OP_SeekLt );.
207b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
207c0 64 4f 70 34 49 6e 74 28 76 2c 20 6f 70 2c 20 69 dOp4Int(v, op, i
207d0 49 64 78 43 75 72 2c 20 61 64 64 72 4e 78 74 2c IdxCur, addrNxt,
207e0 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 regBase, nConst
207f0 72 61 69 6e 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 raint);.. /*
20800 4c 6f 61 64 20 74 68 65 20 76 61 6c 75 65 20 66 Load the value f
20810 6f 72 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74 or the inequalit
20820 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 y constraint at
20830 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 the end of the.
20840 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 69 66 20 ** range (if
20850 61 6e 79 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 any).. */.
20860 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e nConstraint = n
20870 45 71 3b 0a 20 20 20 20 69 66 28 20 70 52 61 6e Eq;. if( pRan
20880 67 65 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 45 geEnd ){. E
20890 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 52 xpr *pRight = pR
208a0 61 6e 67 65 45 6e 64 2d 3e 70 45 78 70 72 2d 3e angeEnd->pExpr->
208b0 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 73 71 pRight;. sq
208c0 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 52 65 lite3ExprCacheRe
208d0 6d 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67 move(pParse, reg
208e0 42 61 73 65 2b 6e 45 71 2c 20 31 29 3b 0a 20 20 Base+nEq, 1);.
208f0 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
20900 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52 69 67 ode(pParse, pRig
20910 68 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 ht, regBase+nEq)
20920 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 52 61 ;. if( (pRa
20930 6e 67 65 45 6e 64 2d 3e 77 74 46 6c 61 67 73 20 ngeEnd->wtFlags
20940 26 20 54 45 52 4d 5f 56 4e 55 4c 4c 29 3d 3d 30 & TERM_VNULL)==0
20950 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
20960 74 65 33 45 78 70 72 43 6f 64 65 49 73 4e 75 6c te3ExprCodeIsNul
20970 6c 4a 75 6d 70 28 76 2c 20 70 52 69 67 68 74 2c lJump(v, pRight,
20980 20 72 65 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 regBase+nEq, ad
20990 64 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a drNxt);. }.
209a0 20 20 20 20 20 20 69 66 28 20 7a 45 6e 64 41 66 if( zEndAf
209b0 66 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 f ){. if(
209c0 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 sqlite3CompareA
209d0 66 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 ffinity(pRight,
209e0 7a 45 6e 64 41 66 66 5b 6e 45 71 5d 29 3d 3d 53 zEndAff[nEq])==S
209f0 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 7b QLITE_AFF_NONE){
20a00 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 . /* Si
20a10 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73 nce the comparis
20a20 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72 66 on is to be perf
20a30 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63 6f ormed with no co
20a40 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20 20 20 20 nversions.
20a50 20 20 20 20 2a 2a 20 61 70 70 6c 69 65 64 20 74 ** applied t
20a60 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c 20 o the operands,
20a70 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 set the affinity
20a80 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52 69 to apply to pRi
20a90 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20 ght to .
20aa0 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f ** SQLITE_AFF_
20ab0 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20 20 NONE. */.
20ac0 20 20 20 20 7a 45 6e 64 41 66 66 5b 6e 45 71 5d zEndAff[nEq]
20ad0 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f = SQLITE_AFF_NO
20ae0 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 NE;. }.
20af0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
20b00 33 45 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69 3ExprNeedsNoAffi
20b10 6e 69 74 79 43 68 61 6e 67 65 28 70 52 69 67 68 nityChange(pRigh
20b20 74 2c 20 7a 45 6e 64 41 66 66 5b 6e 45 71 5d 29 t, zEndAff[nEq])
20b30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45 ){. zE
20b40 6e 64 41 66 66 5b 6e 45 71 5d 20 3d 20 53 51 4c ndAff[nEq] = SQL
20b50 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 ITE_AFF_NONE;.
20b60 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 }. }
20b70 20 0a 20 20 20 20 20 20 63 6f 64 65 41 70 70 6c . codeAppl
20b80 79 41 66 66 69 6e 69 74 79 28 70 50 61 72 73 65 yAffinity(pParse
20b90 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 71 2b 31 , regBase, nEq+1
20ba0 2c 20 7a 45 6e 64 41 66 66 29 3b 0a 20 20 20 20 , zEndAff);.
20bb0 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b nConstraint++;
20bc0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
20bd0 20 70 52 61 6e 67 65 45 6e 64 2d 3e 77 74 46 6c pRangeEnd->wtFl
20be0 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 ags & TERM_VIRTU
20bf0 41 4c 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 AL );. }.
20c00 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 sqlite3DbFree(db
20c10 2c 20 7a 53 74 61 72 74 41 66 66 29 3b 0a 20 20 , zStartAff);.
20c20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 sqlite3DbFree(
20c30 64 62 2c 20 7a 45 6e 64 41 66 66 29 3b 0a 0a 20 db, zEndAff);..
20c40 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 /* Top of the
20c50 20 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 loop body */.
20c60 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 73 pLevel->p2 = s
20c70 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
20c80 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 20 20 2f tAddr(v);.. /
20c90 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 69 * Check if the i
20ca0 6e 64 65 78 20 63 75 72 73 6f 72 20 69 73 20 70 ndex cursor is p
20cb0 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 ast the end of t
20cc0 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 he range. */.
20cd0 20 6f 70 20 3d 20 61 45 6e 64 4f 70 5b 28 70 52 op = aEndOp[(pR
20ce0 61 6e 67 65 45 6e 64 20 7c 7c 20 6e 45 71 29 20 angeEnd || nEq)
20cf0 2a 20 28 31 20 2b 20 62 52 65 76 29 5d 3b 0a 20 * (1 + bRev)];.
20d00 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d testcase( op=
20d10 3d 4f 50 5f 4e 6f 6f 70 20 29 3b 0a 20 20 20 20 =OP_Noop );.
20d20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 testcase( op==OP
20d30 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 74 65 _IdxGE );. te
20d40 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 stcase( op==OP_I
20d50 64 78 4c 54 20 29 3b 0a 20 20 20 20 69 66 28 20 dxLT );. if(
20d60 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 op!=OP_Noop ){.
20d70 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
20d80 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 6f 70 2c AddOp4Int(v, op,
20d90 20 69 49 64 78 43 75 72 2c 20 61 64 64 72 4e 78 iIdxCur, addrNx
20da0 74 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e t, regBase, nCon
20db0 73 74 72 61 69 6e 74 29 3b 0a 20 20 20 20 20 20 straint);.
20dc0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
20dd0 65 50 35 28 76 2c 20 65 6e 64 45 71 21 3d 62 52 eP5(v, endEq!=bR
20de0 65 76 20 3f 31 3a 30 29 3b 0a 20 20 20 20 7d 0a ev ?1:0);. }.
20df0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 . /* If there
20e00 20 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20 are inequality
20e10 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 63 68 65 constraints, che
20e20 63 6b 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 ck that the valu
20e30 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 e. ** of the
20e40 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 table column tha
20e50 74 20 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79 t the inequality
20e60 20 63 6f 6e 74 72 61 69 6e 73 20 69 73 20 6e 6f contrains is no
20e70 74 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 49 t NULL.. ** I
20e80 66 20 69 74 20 69 73 2c 20 6a 75 6d 70 20 74 6f f it is, jump to
20e90 20 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 the next iterat
20ea0 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e ion of the loop.
20eb0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 31 20 3d . */. r1 =
20ec0 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
20ed0 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 eg(pParse);.
20ee0 74 65 73 74 63 61 73 65 28 20 70 4c 6f 6f 70 2d testcase( pLoop-
20ef0 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 >wsFlags & WHERE
20f00 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20 20 _BTM_LIMIT );.
20f10 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 6f 6f testcase( pLoo
20f20 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 p->wsFlags & WHE
20f30 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 3b 0a RE_TOP_LIMIT );.
20f40 20 20 20 20 69 66 28 20 28 70 4c 6f 6f 70 2d 3e if( (pLoop->
20f50 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 wsFlags & (WHERE
20f60 5f 42 54 4d 5f 4c 49 4d 49 54 7c 57 48 45 52 45 _BTM_LIMIT|WHERE
20f70 5f 54 4f 50 5f 4c 49 4d 49 54 29 29 21 3d 30 20 _TOP_LIMIT))!=0
20f80 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
20f90 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
20fa0 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75 72 _Column, iIdxCur
20fb0 2c 20 6e 45 71 2c 20 72 31 29 3b 0a 20 20 20 20 , nEq, r1);.
20fc0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
20fd0 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c Op2(v, OP_IsNull
20fe0 2c 20 72 31 2c 20 61 64 64 72 43 6f 6e 74 29 3b , r1, addrCont);
20ff0 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
21000 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
21010 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 0a 20 (pParse, r1);..
21020 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 74 /* Seek the t
21030 61 62 6c 65 20 63 75 72 73 6f 72 2c 20 69 66 20 able cursor, if
21040 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20 required */.
21050 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 disableTerm(pLev
21060 65 6c 2c 20 70 52 61 6e 67 65 53 74 61 72 74 29 el, pRangeStart)
21070 3b 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 ;. disableTer
21080 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67 65 m(pLevel, pRange
21090 45 6e 64 29 3b 0a 20 20 20 20 69 66 28 20 21 6f End);. if( !o
210a0 6d 69 74 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 mitTable ){.
210b0 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20 69 52 iRowidReg = iR
210c0 65 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 eleaseReg = sqli
210d0 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
210e0 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c arse);. sql
210f0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
21100 2c 20 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 , OP_IdxRowid, i
21110 49 64 78 43 75 72 2c 20 69 52 6f 77 69 64 52 65 IdxCur, iRowidRe
21120 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 g);. sqlite
21130 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 3ExprCacheStore(
21140 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31 pParse, iCur, -1
21150 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 , iRowidReg);.
21160 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
21170 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 65 6b ddOp2(v, OP_Seek
21180 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 52 65 , iCur, iRowidRe
21190 67 29 3b 20 20 2f 2a 20 44 65 66 65 72 72 65 64 g); /* Deferred
211a0 20 73 65 65 6b 20 2a 2f 0a 20 20 20 20 7d 0a 0a seek */. }..
211b0 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 /* Record th
211c0 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 75 73 e instruction us
211d0 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 ed to terminate
211e0 74 68 65 20 6c 6f 6f 70 2e 20 44 69 73 61 62 6c the loop. Disabl
211f0 65 20 0a 20 20 20 20 2a 2a 20 57 48 45 52 45 20 e . ** WHERE
21200 63 6c 61 75 73 65 20 74 65 72 6d 73 20 6d 61 64 clause terms mad
21210 65 20 72 65 64 75 6e 64 61 6e 74 20 62 79 20 74 e redundant by t
21220 68 65 20 69 6e 64 65 78 20 72 61 6e 67 65 20 73 he index range s
21230 63 61 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 can.. */.
21240 69 66 28 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 if( pLoop->wsFla
21250 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45 52 4f gs & WHERE_ONERO
21260 57 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 W ){. pLeve
21270 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b l->op = OP_Noop;
21280 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 62 . }else if( b
21290 52 65 76 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 Rev ){. pLe
212a0 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 50 72 65 vel->op = OP_Pre
212b0 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 v;. }else{.
212c0 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d pLevel->op =
212d0 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20 7d 0a OP_Next;. }.
212e0 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d pLevel->p1 =
212f0 20 69 49 64 78 43 75 72 3b 0a 20 20 20 20 69 66 iIdxCur;. if
21300 28 20 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 ( (pLoop->wsFlag
21310 73 20 26 20 57 48 45 52 45 5f 43 4f 4e 53 54 52 s & WHERE_CONSTR
21320 41 49 4e 54 29 3d 3d 30 20 29 7b 0a 20 20 20 20 AINT)==0 ){.
21330 20 20 70 4c 65 76 65 6c 2d 3e 70 35 20 3d 20 53 pLevel->p5 = S
21340 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 QLITE_STMTSTATUS
21350 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3b 0a _FULLSCAN_STEP;.
21360 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
21370 20 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d assert( pLevel-
21380 3e 70 35 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a >p5==0 );. }.
21390 20 20 7d 65 6c 73 65 0a 0a 23 69 66 6e 64 65 66 }else..#ifndef
213a0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f SQLITE_OMIT_OR_
213b0 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 69 OPTIMIZATION. i
213c0 66 28 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 f( pLoop->wsFlag
213d0 73 20 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f s & WHERE_MULTI_
213e0 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 OR ){. /* Cas
213f0 65 20 35 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 e 5: Two or mor
21400 65 20 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 e separately ind
21410 65 78 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 exed terms conne
21420 63 74 65 64 20 62 79 20 4f 52 0a 20 20 20 20 2a cted by OR. *
21430 2a 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 *. ** Example
21440 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 :. **. **
21450 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 CREATE TABLE t
21460 31 28 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 1(a,b,c,d);.
21470 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 ** CREATE INDE
21480 58 20 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 X i1 ON t1(a);.
21490 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 ** CREATE I
214a0 4e 44 45 58 20 69 32 20 4f 4e 20 74 31 28 62 29 NDEX i2 ON t1(b)
214b0 3b 0a 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 ;. ** CREAT
214c0 45 20 49 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 E INDEX i3 ON t1
214d0 28 63 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 (c);. **.
214e0 2a 2a 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 ** SELECT * FR
214f0 4f 4d 20 74 31 20 57 48 45 52 45 20 61 3d 35 20 OM t1 WHERE a=5
21500 4f 52 20 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 OR b=7 OR (c=11
21510 41 4e 44 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a AND d=13). **
21520 0a 20 20 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 . ** In the e
21530 78 61 6d 70 6c 65 2c 20 74 68 65 72 65 20 61 72 xample, there ar
21540 65 20 74 68 72 65 65 20 69 6e 64 65 78 65 64 20 e three indexed
21550 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 terms connected
21560 62 79 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 by OR.. ** Th
21570 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f e top of the loo
21580 70 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 p looks like thi
21590 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a s:. **. **
215a0 20 20 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 Null
215b0 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 1
215c0 20 20 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 # Zero the
215d0 20 72 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 rowset in reg 1
215e0 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 . **. ** T
215f0 68 65 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e hen, for each in
21600 64 65 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 dexed term, the
21610 66 6f 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 following. The a
21620 72 67 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 rguments to.
21630 2a 2a 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 ** RowSetTest ar
21640 65 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 e such that the
21650 72 6f 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 rowid of the cur
21660 72 65 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 rent row is inse
21670 72 74 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f rted. ** into
21680 20 74 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20 the RowSet. If
21690 69 74 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 it is already pr
216a0 65 73 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 esent, control s
216b0 6b 69 70 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 kips the. **
216c0 47 6f 73 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 Gosub opcode and
216d0 20 6a 75 6d 70 73 20 73 74 72 61 69 67 68 74 20 jumps straight
216e0 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 to the code gene
216f0 72 61 74 65 64 20 62 79 20 57 68 65 72 65 45 6e rated by WhereEn
21700 64 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 d().. **.
21710 2a 2a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ** sqlite
21720 33 57 68 65 72 65 42 65 67 69 6e 28 3c 74 65 72 3WhereBegin(<ter
21730 6d 3e 29 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 m>). **
21740 20 20 20 20 52 6f 77 53 65 74 54 65 73 74 20 20 RowSetTest
21750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21760 23 20 49 6e 73 65 72 74 20 72 6f 77 69 64 20 69 # Insert rowid i
21770 6e 74 6f 20 72 6f 77 73 65 74 0a 20 20 20 20 2a nto rowset. *
21780 2a 20 20 20 20 20 20 20 20 20 20 47 6f 73 75 62 * Gosub
21790 20 20 20 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 2 A. **
217a0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 sqlite3W
217b0 68 65 72 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a hereEnd(). **
217c0 0a 20 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e . ** Followin
217d0 67 20 74 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 g the above, cod
217e0 65 20 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 e to terminate t
217f0 68 65 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 he loop. Label A
21800 2c 20 74 68 65 20 74 61 72 67 65 74 0a 20 20 20 , the target.
21810 20 2a 2a 20 6f 66 20 74 68 65 20 47 6f 73 75 62 ** of the Gosub
21820 20 61 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f above, jumps to
21830 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e the instruction
21840 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68 65 right after the
21850 20 47 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 Goto.. **.
21860 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 ** Nu
21870 6c 6c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 ll 1
21880 20 20 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f # Zero
21890 20 74 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 the rowset in r
218a0 65 67 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 eg 1. **
218b0 20 20 20 20 20 47 6f 74 6f 20 20 20 20 20 20 20 Goto
218c0 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 B
218d0 20 23 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 # The loop is f
218e0 69 6e 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a inished.. **.
218f0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 ** A:
21900 3c 6c 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 <loop body>
21910 20 20 20 20 20 20 20 20 20 20 20 20 23 20 52 65 # Re
21920 74 75 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 turn data, whate
21930 76 65 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 ver.. **.
21940 2a 2a 20 20 20 20 20 20 20 20 20 20 52 65 74 75 ** Retu
21950 72 6e 20 20 20 20 20 32 20 20 20 20 20 20 20 20 rn 2
21960 20 20 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62 # Jump b
21970 61 63 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62 ack to the Gosub
21980 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
21990 20 20 20 20 20 42 3a 20 3c 61 66 74 65 72 20 74 B: <after t
219a0 68 65 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a he loop>. **.
219b0 20 20 20 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 */. Where
219c0 43 6c 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 Clause *pOrWc;
219d0 20 20 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 /* The OR-clau
219e0 73 65 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e se broken out in
219f0 74 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 to subterms */.
21a00 20 20 20 53 72 63 4c 69 73 74 20 2a 70 4f 72 54 SrcList *pOrT
21a10 61 62 3b 20 20 20 20 20 20 20 2f 2a 20 53 68 6f ab; /* Sho
21a20 72 74 65 6e 65 64 20 74 61 62 6c 65 20 6c 69 73 rtened table lis
21a30 74 20 6f 72 20 4f 52 2d 63 6c 61 75 73 65 20 67 t or OR-clause g
21a40 65 6e 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 eneration */.
21a50 20 49 6e 64 65 78 20 2a 70 43 6f 76 20 3d 20 30 Index *pCov = 0
21a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a ; /*
21a70 20 50 6f 74 65 6e 74 69 61 6c 20 63 6f 76 65 72 Potential cover
21a80 69 6e 67 20 69 6e 64 65 78 20 28 6f 72 20 4e 55 ing index (or NU
21a90 4c 4c 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 LL) */. int i
21aa0 43 6f 76 43 75 72 20 3d 20 70 50 61 72 73 65 2d CovCur = pParse-
21ab0 3e 6e 54 61 62 2b 2b 3b 20 20 2f 2a 20 43 75 72 >nTab++; /* Cur
21ac0 73 6f 72 20 75 73 65 64 20 66 6f 72 20 69 6e 64 sor used for ind
21ad0 65 78 20 73 63 61 6e 73 20 28 69 66 20 61 6e 79 ex scans (if any
21ae0 29 20 2a 2f 0a 0a 20 20 20 20 69 6e 74 20 72 65 ) */.. int re
21af0 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70 50 61 72 gReturn = ++pPar
21b00 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20 20 20 20 se->nMem;
21b10 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
21b20 75 73 65 64 20 77 69 74 68 20 4f 50 5f 47 6f 73 used with OP_Gos
21b30 75 62 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 ub */. int re
21b40 67 52 6f 77 73 65 74 20 3d 20 30 3b 20 20 20 20 gRowset = 0;
21b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21b60 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 /* Register
21b70 66 6f 72 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 for RowSet objec
21b80 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 t */. int reg
21b90 52 6f 77 69 64 20 3d 20 30 3b 20 20 20 20 20 20 Rowid = 0;
21ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21bb0 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 /* Register h
21bc0 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20 2a 2f 0a olding rowid */.
21bd0 20 20 20 20 69 6e 74 20 69 4c 6f 6f 70 42 6f 64 int iLoopBod
21be0 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d y = sqlite3VdbeM
21bf0 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a akeLabel(v); /*
21c00 20 53 74 61 72 74 20 6f 66 20 6c 6f 6f 70 20 62 Start of loop b
21c10 6f 64 79 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 ody */. int i
21c20 52 65 74 49 6e 69 74 3b 20 20 20 20 20 20 20 20 RetInit;
21c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21c40 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 /* Address
21c50 6f 66 20 72 65 67 52 65 74 75 72 6e 20 69 6e 69 of regReturn ini
21c60 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20 75 6e 74 t */. int unt
21c70 65 73 74 65 64 54 65 72 6d 73 20 3d 20 30 3b 20 estedTerms = 0;
21c80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 /* S
21c90 6f 6d 65 20 74 65 72 6d 73 20 6e 6f 74 20 63 6f ome terms not co
21ca0 6d 70 6c 65 74 65 6c 79 20 74 65 73 74 65 64 20 mpletely tested
21cb0 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 */. int ii;
21cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21cd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f /* Loo
21ce0 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 p counter */.
21cf0 20 45 78 70 72 20 2a 70 41 6e 64 45 78 70 72 20 Expr *pAndExpr
21d00 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
21d10 20 20 20 20 2f 2a 20 41 6e 20 22 2e 2e 20 41 4e /* An ".. AN
21d20 44 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 D (...)" express
21d30 69 6f 6e 20 2a 2f 0a 20 20 20 0a 20 20 20 20 70 ion */. . p
21d40 54 65 72 6d 20 3d 20 70 4c 6f 6f 70 2d 3e 61 4c Term = pLoop->aL
21d50 54 65 72 6d 5b 30 5d 3b 0a 20 20 20 20 61 73 73 Term[0];. ass
21d60 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29 3b ert( pTerm!=0 );
21d70 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 . assert( pTe
21d80 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 rm->eOperator &
21d90 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73 73 WO_OR );. ass
21da0 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 ert( (pTerm->wtF
21db0 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e lags & TERM_ORIN
21dc0 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f FO)!=0 );. pO
21dd0 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e rWc = &pTerm->u.
21de0 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 pOrInfo->wc;.
21df0 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 pLevel->op = OP
21e00 5f 52 65 74 75 72 6e 3b 0a 20 20 20 20 70 4c 65 _Return;. pLe
21e10 76 65 6c 2d 3e 70 31 20 3d 20 72 65 67 52 65 74 vel->p1 = regRet
21e20 75 72 6e 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 urn;.. /* Set
21e30 20 75 70 20 61 20 6e 65 77 20 53 72 63 4c 69 73 up a new SrcLis
21e40 74 20 69 6e 20 70 4f 72 54 61 62 20 63 6f 6e 74 t in pOrTab cont
21e50 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 aining the table
21e60 20 62 65 69 6e 67 20 73 63 61 6e 6e 65 64 0a 20 being scanned.
21e70 20 20 20 2a 2a 20 62 79 20 74 68 69 73 20 6c 6f ** by this lo
21e80 6f 70 20 69 6e 20 74 68 65 20 61 5b 30 5d 20 73 op in the a[0] s
21e90 6c 6f 74 20 61 6e 64 20 61 6c 6c 20 6e 6f 74 52 lot and all notR
21ea0 65 61 64 79 20 74 61 62 6c 65 73 20 69 6e 20 61 eady tables in a
21eb0 5b 31 2e 2e 5d 20 73 6c 6f 74 73 2e 0a 20 20 20 [1..] slots..
21ec0 20 2a 2a 20 54 68 69 73 20 62 65 63 6f 6d 65 73 ** This becomes
21ed0 20 74 68 65 20 53 72 63 4c 69 73 74 20 69 6e 20 the SrcList in
21ee0 74 68 65 20 72 65 63 75 72 73 69 76 65 20 63 61 the recursive ca
21ef0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 ll to sqlite3Whe
21f00 72 65 42 65 67 69 6e 28 29 2e 0a 20 20 20 20 2a reBegin().. *
21f10 2f 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f /. if( pWInfo
21f20 2d 3e 6e 4c 65 76 65 6c 3e 31 20 29 7b 0a 20 20 ->nLevel>1 ){.
21f30 20 20 20 20 69 6e 74 20 6e 4e 6f 74 52 65 61 64 int nNotRead
21f40 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 y;
21f50 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 /* The number
21f60 20 6f 66 20 6e 6f 74 52 65 61 64 79 20 74 61 62 of notReady tab
21f70 6c 65 73 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 les */. str
21f80 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
21f90 20 2a 6f 72 69 67 53 72 63 3b 20 20 20 20 20 2f *origSrc; /
21fa0 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 69 73 74 20 * Original list
21fb0 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 of tables */.
21fc0 20 20 20 6e 4e 6f 74 52 65 61 64 79 20 3d 20 70 nNotReady = p
21fd0 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20 2d 20 WInfo->nLevel -
21fe0 69 4c 65 76 65 6c 20 2d 20 31 3b 0a 20 20 20 20 iLevel - 1;.
21ff0 20 20 70 4f 72 54 61 62 20 3d 20 73 71 6c 69 74 pOrTab = sqlit
22000 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28 e3StackAllocRaw(
22010 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 db,.
22020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22030 73 69 7a 65 6f 66 28 2a 70 4f 72 54 61 62 29 2b sizeof(*pOrTab)+
22040 20 6e 4e 6f 74 52 65 61 64 79 2a 73 69 7a 65 6f nNotReady*sizeo
22050 66 28 70 4f 72 54 61 62 2d 3e 61 5b 30 5d 29 29 f(pOrTab->a[0]))
22060 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 ;. if( pOrT
22070 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 6e ab==0 ) return n
22080 6f 74 52 65 61 64 79 3b 0a 20 20 20 20 20 20 70 otReady;. p
22090 4f 72 54 61 62 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 OrTab->nAlloc =
220a0 28 75 38 29 28 6e 4e 6f 74 52 65 61 64 79 20 2b (u8)(nNotReady +
220b0 20 31 29 3b 0a 20 20 20 20 20 20 70 4f 72 54 61 1);. pOrTa
220c0 62 2d 3e 6e 53 72 63 20 3d 20 70 4f 72 54 61 62 b->nSrc = pOrTab
220d0 2d 3e 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 ->nAlloc;.
220e0 6d 65 6d 63 70 79 28 70 4f 72 54 61 62 2d 3e 61 memcpy(pOrTab->a
220f0 2c 20 70 54 61 62 49 74 65 6d 2c 20 73 69 7a 65 , pTabItem, size
22100 6f 66 28 2a 70 54 61 62 49 74 65 6d 29 29 3b 0a of(*pTabItem));.
22110 20 20 20 20 20 20 6f 72 69 67 53 72 63 20 3d 20 origSrc =
22120 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 pWInfo->pTabList
22130 2d 3e 61 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b ->a;. for(k
22140 3d 31 3b 20 6b 3c 3d 6e 4e 6f 74 52 65 61 64 79 =1; k<=nNotReady
22150 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 ; k++){.
22160 6d 65 6d 63 70 79 28 26 70 4f 72 54 61 62 2d 3e memcpy(&pOrTab->
22170 61 5b 6b 5d 2c 20 26 6f 72 69 67 53 72 63 5b 70 a[k], &origSrc[p
22180 4c 65 76 65 6c 5b 6b 5d 2e 69 46 72 6f 6d 5d 2c Level[k].iFrom],
22190 20 73 69 7a 65 6f 66 28 70 4f 72 54 61 62 2d 3e sizeof(pOrTab->
221a0 61 5b 6b 5d 29 29 3b 0a 20 20 20 20 20 20 7d 0a a[k]));. }.
221b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
221c0 20 70 4f 72 54 61 62 20 3d 20 70 57 49 6e 66 6f pOrTab = pWInfo
221d0 2d 3e 70 54 61 62 4c 69 73 74 3b 0a 20 20 20 20 ->pTabList;.
221e0 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 }.. /* Initia
221f0 6c 69 7a 65 20 74 68 65 20 72 6f 77 73 65 74 20 lize the rowset
22200 72 65 67 69 73 74 65 72 20 74 6f 20 63 6f 6e 74 register to cont
22210 61 69 6e 20 4e 55 4c 4c 2e 20 41 6e 20 53 51 4c ain NULL. An SQL
22220 20 4e 55 4c 4c 20 69 73 20 0a 20 20 20 20 2a 2a NULL is . **
22230 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 equivalent to a
22240 6e 20 65 6d 70 74 79 20 72 6f 77 73 65 74 2e 0a n empty rowset..
22250 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c **. ** Al
22260 73 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 72 65 so initialize re
22270 67 52 65 74 75 72 6e 20 74 6f 20 63 6f 6e 74 61 gReturn to conta
22280 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f in the address o
22290 66 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f f the instructio
222a0 6e 20 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 n . ** immedi
222b0 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 ately following
222c0 74 68 65 20 4f 50 5f 52 65 74 75 72 6e 20 61 74 the OP_Return at
222d0 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 the bottom of t
222e0 68 65 20 6c 6f 6f 70 2e 20 54 68 69 73 0a 20 20 he loop. This.
222f0 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 ** is required
22300 20 69 6e 20 61 20 66 65 77 20 6f 62 73 63 75 72 in a few obscur
22310 65 20 4c 45 46 54 20 4a 4f 49 4e 20 63 61 73 65 e LEFT JOIN case
22320 73 20 77 68 65 72 65 20 63 6f 6e 74 72 6f 6c 20 s where control
22330 6a 75 6d 70 73 0a 20 20 20 20 2a 2a 20 6f 76 65 jumps. ** ove
22340 72 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 r the top of the
22350 20 6c 6f 6f 70 20 69 6e 74 6f 20 74 68 65 20 62 loop into the b
22360 6f 64 79 20 6f 66 20 69 74 2e 20 49 6e 20 74 68 ody of it. In th
22370 69 73 20 63 61 73 65 20 74 68 65 20 0a 20 20 20 is case the .
22380 20 2a 2a 20 63 6f 72 72 65 63 74 20 72 65 73 70 ** correct resp
22390 6f 6e 73 65 20 66 6f 72 20 74 68 65 20 65 6e 64 onse for the end
223a0 2d 6f 66 2d 6c 6f 6f 70 20 63 6f 64 65 20 28 74 -of-loop code (t
223b0 68 65 20 4f 50 5f 52 65 74 75 72 6e 29 20 69 73 he OP_Return) is
223c0 20 74 6f 20 0a 20 20 20 20 2a 2a 20 66 61 6c 6c to . ** fall
223d0 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 through to the
223e0 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e next instruction
223f0 2c 20 6a 75 73 74 20 61 73 20 61 6e 20 4f 50 5f , just as an OP_
22400 4e 65 78 74 20 64 6f 65 73 20 69 66 0a 20 20 20 Next does if.
22410 20 2a 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e ** called on an
22420 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 uninitialized c
22430 75 72 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 ursor.. */.
22440 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e 77 if( (pWInfo->w
22450 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 ctrlFlags & WHER
22460 45 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 E_DUPLICATES_OK)
22470 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 67 ==0 ){. reg
22480 52 6f 77 73 65 74 20 3d 20 2b 2b 70 50 61 72 73 Rowset = ++pPars
22490 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72 e->nMem;. r
224a0 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 egRowid = ++pPar
224b0 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 se->nMem;.
224c0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
224d0 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 2(v, OP_Null, 0,
224e0 20 72 65 67 52 6f 77 73 65 74 29 3b 0a 20 20 20 regRowset);.
224f0 20 7d 0a 20 20 20 20 69 52 65 74 49 6e 69 74 20 }. iRetInit
22500 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
22510 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 Op2(v, OP_Intege
22520 72 2c 20 30 2c 20 72 65 67 52 65 74 75 72 6e 29 r, 0, regReturn)
22530 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ;.. /* If the
22540 20 6f 72 69 67 69 6e 61 6c 20 57 48 45 52 45 20 original WHERE
22550 63 6c 61 75 73 65 20 69 73 20 7a 20 6f 66 20 74 clause is z of t
22560 68 65 20 66 6f 72 6d 3a 20 20 28 78 31 20 4f 52 he form: (x1 OR
22570 20 78 32 20 4f 52 20 2e 2e 2e 29 20 41 4e 44 20 x2 OR ...) AND
22580 79 0a 20 20 20 20 2a 2a 20 54 68 65 6e 20 66 6f y. ** Then fo
22590 72 20 65 76 65 72 79 20 74 65 72 6d 20 78 4e 2c r every term xN,
225a0 20 65 76 61 6c 75 61 74 65 20 61 73 20 74 68 65 evaluate as the
225b0 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 3a 20 subexpression:
225c0 78 4e 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 20 xN AND z. **
225d0 54 68 61 74 20 77 61 79 2c 20 74 65 72 6d 73 20 That way, terms
225e0 69 6e 20 79 20 74 68 61 74 20 61 72 65 20 66 61 in y that are fa
225f0 63 74 6f 72 65 64 20 69 6e 74 6f 20 74 68 65 20 ctored into the
22600 64 69 73 6a 75 6e 63 74 69 6f 6e 20 77 69 6c 6c disjunction will
22610 0a 20 20 20 20 2a 2a 20 62 65 20 70 69 63 6b 65 . ** be picke
22620 64 20 75 70 20 62 79 20 74 68 65 20 72 65 63 75 d up by the recu
22630 72 73 69 76 65 20 63 61 6c 6c 73 20 74 6f 20 73 rsive calls to s
22640 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e qlite3WhereBegin
22650 28 29 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2a () below.. **
22660 0a 20 20 20 20 2a 2a 20 41 63 74 75 61 6c 6c 79 . ** Actually
22670 2c 20 65 61 63 68 20 73 75 62 65 78 70 72 65 73 , each subexpres
22680 73 69 6f 6e 20 69 73 20 63 6f 6e 76 65 72 74 65 sion is converte
22690 64 20 74 6f 20 22 78 4e 20 41 4e 44 20 77 22 20 d to "xN AND w"
226a0 77 68 65 72 65 20 77 20 69 73 0a 20 20 20 20 2a where w is. *
226b0 2a 20 74 68 65 20 22 69 6e 74 65 72 65 73 74 69 * the "interesti
226c0 6e 67 22 20 74 65 72 6d 73 20 6f 66 20 7a 20 2d ng" terms of z -
226d0 20 74 65 72 6d 73 20 74 68 61 74 20 64 69 64 20 terms that did
226e0 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20 69 6e not originate in
226f0 20 74 68 65 0a 20 20 20 20 2a 2a 20 4f 4e 20 6f the. ** ON o
22700 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f r USING clause o
22710 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c 20 61 f a LEFT JOIN, a
22720 6e 64 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 nd terms that ar
22730 65 20 75 73 61 62 6c 65 20 61 73 20 0a 20 20 20 e usable as .
22740 20 2a 2a 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 ** indices..
22750 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 **. ** This
22760 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 61 6c 73 optimization als
22770 6f 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 69 o only applies i
22780 66 20 74 68 65 20 28 78 31 20 4f 52 20 78 32 20 f the (x1 OR x2
22790 4f 52 20 2e 2e 2e 29 20 74 65 72 6d 0a 20 20 20 OR ...) term.
227a0 20 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 ** is not conta
227b0 69 6e 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 63 ined in the ON c
227c0 6c 61 75 73 65 20 6f 66 20 61 20 4c 45 46 54 20 lause of a LEFT
227d0 4a 4f 49 4e 2e 0a 20 20 20 20 2a 2a 20 53 65 65 JOIN.. ** See
227e0 20 74 69 63 6b 65 74 20 68 74 74 70 3a 2f 2f 77 ticket http://w
227f0 77 77 2e 73 71 6c 69 74 65 2e 6f 72 67 2f 73 72 ww.sqlite.org/sr
22800 63 2f 69 6e 66 6f 2f 66 32 33 36 39 33 30 34 65 c/info/f2369304e
22810 34 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 4. */. if(
22820 20 70 57 43 2d 3e 6e 54 65 72 6d 3e 31 20 29 7b pWC->nTerm>1 ){
22830 0a 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d . int iTerm
22840 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 54 65 72 ;. for(iTer
22850 6d 3d 30 3b 20 69 54 65 72 6d 3c 70 57 43 2d 3e m=0; iTerm<pWC->
22860 6e 54 65 72 6d 3b 20 69 54 65 72 6d 2b 2b 29 7b nTerm; iTerm++){
22870 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 . Expr *p
22880 45 78 70 72 20 3d 20 70 57 43 2d 3e 61 5b 69 54 Expr = pWC->a[iT
22890 65 72 6d 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 erm].pExpr;.
228a0 20 20 20 20 69 66 28 20 26 70 57 43 2d 3e 61 5b if( &pWC->a[
228b0 69 54 65 72 6d 5d 20 3d 3d 20 70 54 65 72 6d 20 iTerm] == pTerm
228c0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
228d0 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 if( ExprHasP
228e0 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 roperty(pExpr, E
228f0 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 20 63 6f P_FromJoin) ) co
22900 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
22910 69 66 28 20 70 57 43 2d 3e 61 5b 69 54 65 72 6d if( pWC->a[iTerm
22920 5d 2e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 ].wtFlags & (TER
22930 4d 5f 4f 52 49 4e 46 4f 29 20 29 20 63 6f 6e 74 M_ORINFO) ) cont
22940 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66 inue;. if
22950 28 20 28 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d ( (pWC->a[iTerm]
22960 2e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f .eOperator & WO_
22970 41 4c 4c 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e ALL)==0 ) contin
22980 75 65 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 ue;. pExp
22990 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 r = sqlite3ExprD
229a0 75 70 28 64 62 2c 20 70 45 78 70 72 2c 20 30 29 up(db, pExpr, 0)
229b0 3b 0a 20 20 20 20 20 20 20 20 70 41 6e 64 45 78 ;. pAndEx
229c0 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 pr = sqlite3Expr
229d0 41 6e 64 28 64 62 2c 20 70 41 6e 64 45 78 70 72 And(db, pAndExpr
229e0 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 , pExpr);.
229f0 7d 0a 20 20 20 20 20 20 69 66 28 20 70 41 6e 64 }. if( pAnd
22a00 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 Expr ){.
22a10 70 41 6e 64 45 78 70 72 20 3d 20 73 71 6c 69 74 pAndExpr = sqlit
22a20 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 e3PExpr(pParse,
22a30 54 4b 5f 41 4e 44 2c 20 30 2c 20 70 41 6e 64 45 TK_AND, 0, pAndE
22a40 78 70 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d xpr, 0);. }
22a50 0a 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 . }.. for(
22a60 69 69 3d 30 3b 20 69 69 3c 70 4f 72 57 63 2d 3e ii=0; ii<pOrWc->
22a70 6e 54 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 nTerm; ii++){.
22a80 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 WhereTerm *p
22a90 4f 72 54 65 72 6d 20 3d 20 26 70 4f 72 57 63 2d OrTerm = &pOrWc-
22aa0 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 20 20 69 66 >a[ii];. if
22ab0 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 ( pOrTerm->leftC
22ac0 75 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c 20 28 ursor==iCur || (
22ad0 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 pOrTerm->eOperat
22ae0 6f 72 20 26 20 57 4f 5f 41 4e 44 29 21 3d 30 20 or & WO_AND)!=0
22af0 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 ){. Where
22b00 49 6e 66 6f 20 2a 70 53 75 62 57 49 6e 66 6f 3b Info *pSubWInfo;
22b10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 /* Inf
22b20 6f 20 66 6f 72 20 73 69 6e 67 6c 65 20 4f 52 2d o for single OR-
22b30 74 65 72 6d 20 73 63 61 6e 20 2a 2f 0a 20 20 20 term scan */.
22b40 20 20 20 20 20 45 78 70 72 20 2a 70 4f 72 45 78 Expr *pOrEx
22b50 70 72 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 70 45 pr = pOrTerm->pE
22b60 78 70 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 xpr;. if(
22b70 20 70 41 6e 64 45 78 70 72 20 26 26 20 21 45 78 pAndExpr && !Ex
22b80 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f prHasProperty(pO
22b90 72 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f rExpr, EP_FromJo
22ba0 69 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 in) ){.
22bb0 20 70 41 6e 64 45 78 70 72 2d 3e 70 4c 65 66 74 pAndExpr->pLeft
22bc0 20 3d 20 70 4f 72 45 78 70 72 3b 0a 20 20 20 20 = pOrExpr;.
22bd0 20 20 20 20 20 20 70 4f 72 45 78 70 72 20 3d 20 pOrExpr =
22be0 70 41 6e 64 45 78 70 72 3b 0a 20 20 20 20 20 20 pAndExpr;.
22bf0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c }. /* L
22c00 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 61 62 6c oop through tabl
22c10 65 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6d e entries that m
22c20 61 74 63 68 20 74 65 72 6d 20 70 4f 72 54 65 72 atch term pOrTer
22c30 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 53 m. */. pS
22c40 75 62 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 ubWInfo = sqlite
22c50 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 3WhereBegin(pPar
22c60 73 65 2c 20 70 4f 72 54 61 62 2c 20 70 4f 72 45 se, pOrTab, pOrE
22c70 78 70 72 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 xpr, 0, 0,.
22c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22c90 20 20 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 WHERE_OMIT_OP
22ca0 45 4e 5f 43 4c 4f 53 45 20 7c 20 57 48 45 52 45 EN_CLOSE | WHERE
22cb0 5f 41 4e 44 5f 4f 4e 4c 59 20 7c 0a 20 20 20 20 _AND_ONLY |.
22cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22cd0 20 20 20 20 57 48 45 52 45 5f 46 4f 52 43 45 5f WHERE_FORCE_
22ce0 54 41 42 4c 45 20 7c 20 57 48 45 52 45 5f 4f 4e TABLE | WHERE_ON
22cf0 45 54 41 42 4c 45 5f 4f 4e 4c 59 2c 20 69 43 6f ETABLE_ONLY, iCo
22d00 76 43 75 72 29 3b 0a 20 20 20 20 20 20 20 20 61 vCur);. a
22d10 73 73 65 72 74 28 20 70 53 75 62 57 49 6e 66 6f ssert( pSubWInfo
22d20 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 || pParse->nErr
22d30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
22d40 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 iled );.
22d50 69 66 28 20 70 53 75 62 57 49 6e 66 6f 20 29 7b if( pSubWInfo ){
22d60 0a 20 20 20 20 20 20 20 20 20 20 57 68 65 72 65 . Where
22d70 4c 6f 6f 70 20 2a 70 53 75 62 4c 6f 6f 70 3b 0a Loop *pSubLoop;.
22d80 20 20 20 20 20 20 20 20 20 20 65 78 70 6c 61 69 explai
22d90 6e 4f 6e 65 53 63 61 6e 28 0a 20 20 20 20 20 20 nOneScan(.
22da0 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 pParse,
22db0 70 4f 72 54 61 62 2c 20 26 70 53 75 62 57 49 6e pOrTab, &pSubWIn
22dc0 66 6f 2d 3e 61 5b 30 5d 2c 20 69 4c 65 76 65 6c fo->a[0], iLevel
22dd0 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 2c , pLevel->iFrom,
22de0 20 30 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 0. );.
22df0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28 70 if( (p
22e00 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 WInfo->wctrlFlag
22e10 73 20 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 s & WHERE_DUPLIC
22e20 41 54 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 ATES_OK)==0 ){.
22e30 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 int i
22e40 53 65 74 20 3d 20 28 28 69 69 3d 3d 70 4f 72 57 Set = ((ii==pOrW
22e50 63 2d 3e 6e 54 65 72 6d 2d 31 29 3f 2d 31 3a 69 c->nTerm-1)?-1:i
22e60 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 i);.
22e70 69 6e 74 20 72 3b 0a 20 20 20 20 20 20 20 20 20 int r;.
22e80 20 20 20 72 20 3d 20 73 71 6c 69 74 65 33 45 78 r = sqlite3Ex
22e90 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 prCodeGetColumn(
22ea0 70 50 61 72 73 65 2c 20 70 54 61 62 49 74 65 6d pParse, pTabItem
22eb0 2d 3e 70 54 61 62 2c 20 2d 31 2c 20 69 43 75 72 ->pTab, -1, iCur
22ec0 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 , .
22ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22ee0 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 52 regR
22ef0 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 owid, 0);.
22f00 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
22f10 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 eAddOp4Int(v, OP
22f20 5f 52 6f 77 53 65 74 54 65 73 74 2c 20 72 65 67 _RowSetTest, reg
22f30 52 6f 77 73 65 74 2c 0a 20 20 20 20 20 20 20 20 Rowset,.
22f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
22f50 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 sqlite3
22f60 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 VdbeCurrentAddr(
22f70 76 29 2b 32 2c 20 72 2c 20 69 53 65 74 29 3b 0a v)+2, r, iSet);.
22f80 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 }.
22f90 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
22fa0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f eAddOp2(v, OP_Go
22fb0 73 75 62 2c 20 72 65 67 52 65 74 75 72 6e 2c 20 sub, regReturn,
22fc0 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20 20 20 iLoopBody);..
22fd0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 53 /* The pS
22fe0 75 62 57 49 6e 66 6f 2d 3e 75 6e 74 65 73 74 65 ubWInfo->unteste
22ff0 64 54 65 72 6d 73 20 66 6c 61 67 20 6d 65 61 6e dTerms flag mean
23000 73 20 74 68 61 74 20 74 68 69 73 20 4f 52 20 74 s that this OR t
23010 65 72 6d 0a 20 20 20 20 20 20 20 20 20 20 2a 2a erm. **
23020 20 63 6f 6e 74 61 69 6e 65 64 20 6f 6e 65 20 6f contained one o
23030 72 20 6d 6f 72 65 20 41 4e 44 20 74 65 72 6d 20 r more AND term
23040 66 72 6f 6d 20 61 20 6e 6f 74 52 65 61 64 79 20 from a notReady
23050 74 61 62 6c 65 2e 20 20 54 68 65 0a 20 20 20 20 table. The.
23060 20 20 20 20 20 20 2a 2a 20 74 65 72 6d 73 20 66 ** terms f
23070 72 6f 6d 20 74 68 65 20 6e 6f 74 52 65 61 64 79 rom the notReady
23080 20 74 61 62 6c 65 20 63 6f 75 6c 64 20 6e 6f 74 table could not
23090 20 62 65 20 74 65 73 74 65 64 20 61 6e 64 20 77 be tested and w
230a0 69 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a ill. **
230b0 20 6e 65 65 64 20 74 6f 20 62 65 20 74 65 73 74 need to be test
230c0 65 64 20 6c 61 74 65 72 2e 0a 20 20 20 20 20 20 ed later..
230d0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
230e0 20 69 66 28 20 70 53 75 62 57 49 6e 66 6f 2d 3e if( pSubWInfo->
230f0 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20 29 20 untestedTerms )
23100 75 6e 74 65 73 74 65 64 54 65 72 6d 73 20 3d 20 untestedTerms =
23110 31 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 1;.. /*
23120 20 49 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 4f If all of the O
23130 52 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d R-connected term
23140 73 20 61 72 65 20 6f 70 74 69 6d 69 7a 65 64 20 s are optimized
23150 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 0a 20 using the same.
23160 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 ** inde
23170 78 2c 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 x, and the index
23180 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 is opened using
23190 20 74 68 65 20 73 61 6d 65 20 63 75 72 73 6f 72 the same cursor
231a0 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 20 20 number.
231b0 20 20 2a 2a 20 62 79 20 65 61 63 68 20 63 61 6c ** by each cal
231c0 6c 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72 l to sqlite3Wher
231d0 65 42 65 67 69 6e 28 29 20 6d 61 64 65 20 62 79 eBegin() made by
231e0 20 74 68 69 73 20 6c 6f 6f 70 2c 20 69 74 20 6d this loop, it m
231f0 61 79 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 ay. **
23200 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 be possible to u
23210 73 65 20 74 68 61 74 20 69 6e 64 65 78 20 61 73 se that index as
23220 20 61 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 a covering inde
23230 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a x.. **.
23240 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 ** If
23250 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 the call to sqli
23260 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 te3WhereBegin()
23270 61 62 6f 76 65 20 72 65 73 75 6c 74 65 64 20 69 above resulted i
23280 6e 20 61 20 73 63 61 6e 20 74 68 61 74 0a 20 20 n a scan that.
23290 20 20 20 20 20 20 20 20 2a 2a 20 75 73 65 73 20 ** uses
232a0 61 6e 20 69 6e 64 65 78 2c 20 61 6e 64 20 74 68 an index, and th
232b0 69 73 20 69 73 20 65 69 74 68 65 72 20 74 68 65 is is either the
232c0 20 66 69 72 73 74 20 4f 52 2d 63 6f 6e 6e 65 63 first OR-connec
232d0 74 65 64 20 74 65 72 6d 0a 20 20 20 20 20 20 20 ted term.
232e0 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 65 64 20 ** processed
232f0 6f 72 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 or the index is
23300 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 61 74 the same as that
23310 20 75 73 65 64 20 62 79 20 61 6c 6c 20 70 72 65 used by all pre
23320 76 69 6f 75 73 0a 20 20 20 20 20 20 20 20 20 20 vious.
23330 2a 2a 20 74 65 72 6d 73 2c 20 73 65 74 20 70 43 ** terms, set pC
23340 6f 76 20 74 6f 20 74 68 65 20 63 61 6e 64 69 64 ov to the candid
23350 61 74 65 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 ate covering ind
23360 65 78 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 73 ex. Otherwise, s
23370 65 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a et . **
23380 20 70 43 6f 76 20 74 6f 20 4e 55 4c 4c 20 74 6f pCov to NULL to
23390 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 6e indicate that n
233a0 6f 20 63 61 6e 64 69 64 61 74 65 20 63 6f 76 65 o candidate cove
233b0 72 69 6e 67 20 69 6e 64 65 78 20 77 69 6c 6c 20 ring index will
233c0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 . ** be
233d0 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 20 20 available..
233e0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
233f0 20 20 20 70 53 75 62 4c 6f 6f 70 20 3d 20 70 53 pSubLoop = pS
23400 75 62 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 70 57 ubWInfo->a[0].pW
23410 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 20 20 20 20 Loop;.
23420 61 73 73 65 72 74 28 20 28 70 53 75 62 4c 6f 6f assert( (pSubLoo
23430 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 p->wsFlags & WHE
23440 52 45 5f 41 55 54 4f 5f 49 4e 44 45 58 29 3d 3d RE_AUTO_INDEX)==
23450 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 0 );. i
23460 66 28 20 28 70 53 75 62 4c 6f 6f 70 2d 3e 77 73 f( (pSubLoop->ws
23470 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e Flags & WHERE_IN
23480 44 45 58 45 44 29 21 3d 30 0a 20 20 20 20 20 20 DEXED)!=0.
23490 20 20 20 20 20 26 26 20 28 69 69 3d 3d 30 20 7c && (ii==0 |
234a0 7c 20 70 53 75 62 4c 6f 6f 70 2d 3e 75 2e 62 74 | pSubLoop->u.bt
234b0 72 65 65 2e 70 49 6e 64 65 78 3d 3d 70 43 6f 76 ree.pIndex==pCov
234c0 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 ). ){.
234d0 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 asser
234e0 74 28 20 70 53 75 62 57 49 6e 66 6f 2d 3e 61 5b t( pSubWInfo->a[
234f0 30 5d 2e 69 49 64 78 43 75 72 3d 3d 69 43 6f 76 0].iIdxCur==iCov
23500 43 75 72 20 29 3b 0a 20 20 20 20 20 20 20 20 20 Cur );.
23510 20 20 20 70 43 6f 76 20 3d 20 70 53 75 62 4c 6f pCov = pSubLo
23520 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 op->u.btree.pInd
23530 65 78 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 ex;. }e
23540 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
23550 20 70 43 6f 76 20 3d 20 30 3b 0a 20 20 20 20 20 pCov = 0;.
23560 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 }..
23570 20 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65 20 /* Finish the
23580 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 61 62 loop through tab
23590 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 le entries that
235a0 6d 61 74 63 68 20 74 65 72 6d 20 70 4f 72 54 65 match term pOrTe
235b0 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 rm. */.
235c0 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 sqlite3WhereEnd
235d0 28 70 53 75 62 57 49 6e 66 6f 29 3b 0a 20 20 20 (pSubWInfo);.
235e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
235f0 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d }. pLevel-
23600 3e 75 2e 70 43 6f 76 69 64 78 20 3d 20 70 43 6f >u.pCovidx = pCo
23610 76 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 76 20 v;. if( pCov
23620 29 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 ) pLevel->iIdxCu
23630 72 20 3d 20 69 43 6f 76 43 75 72 3b 0a 20 20 20 r = iCovCur;.
23640 20 69 66 28 20 70 41 6e 64 45 78 70 72 20 29 7b if( pAndExpr ){
23650 0a 20 20 20 20 20 20 70 41 6e 64 45 78 70 72 2d . pAndExpr-
23660 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 >pLeft = 0;.
23670 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c sqlite3ExprDel
23680 65 74 65 28 64 62 2c 20 70 41 6e 64 45 78 70 72 ete(db, pAndExpr
23690 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
236a0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 ite3VdbeChangeP1
236b0 28 76 2c 20 69 52 65 74 49 6e 69 74 2c 20 73 71 (v, iRetInit, sq
236c0 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 lite3VdbeCurrent
236d0 41 64 64 72 28 76 29 29 3b 0a 20 20 20 20 73 71 Addr(v));. sq
236e0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
236f0 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 v, OP_Goto, 0, p
23700 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b Level->addrBrk);
23710 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
23720 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 ResolveLabel(v,
23730 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20 20 20 iLoopBody);..
23740 20 69 66 28 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 if( pWInfo->nLe
23750 76 65 6c 3e 31 20 29 20 73 71 6c 69 74 65 33 53 vel>1 ) sqlite3S
23760 74 61 63 6b 46 72 65 65 28 64 62 2c 20 70 4f 72 tackFree(db, pOr
23770 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20 21 75 Tab);. if( !u
23780 6e 74 65 73 74 65 64 54 65 72 6d 73 20 29 20 64 ntestedTerms ) d
23790 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 isableTerm(pLeve
237a0 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20 7d 65 6c l, pTerm);. }el
237b0 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c se.#endif /* SQL
237c0 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 ITE_OMIT_OR_OPTI
237d0 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 7b MIZATION */.. {
237e0 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 36 3a 20 . /* Case 6:
237f0 20 54 68 65 72 65 20 69 73 20 6e 6f 20 75 73 61 There is no usa
23800 62 6c 65 20 69 6e 64 65 78 2e 20 20 57 65 20 6d ble index. We m
23810 75 73 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74 ust do a complet
23820 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 e. **
23830 20 20 73 63 61 6e 20 6f 66 20 74 68 65 20 65 6e scan of the en
23840 74 69 72 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 tire table..
23850 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f */. static co
23860 6e 73 74 20 75 38 20 61 53 74 65 70 5b 5d 20 3d nst u8 aStep[] =
23870 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 4f 50 5f 50 { OP_Next, OP_P
23880 72 65 76 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 rev };. stati
23890 63 20 63 6f 6e 73 74 20 75 38 20 61 53 74 61 72 c const u8 aStar
238a0 74 5b 5d 20 3d 20 7b 20 4f 50 5f 52 65 77 69 6e t[] = { OP_Rewin
238b0 64 2c 20 4f 50 5f 4c 61 73 74 20 7d 3b 0a 20 20 d, OP_Last };.
238c0 20 20 61 73 73 65 72 74 28 20 62 52 65 76 3d 3d assert( bRev==
238d0 30 20 7c 7c 20 62 52 65 76 3d 3d 31 20 29 3b 0a 0 || bRev==1 );.
238e0 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d pLevel->op =
238f0 20 61 53 74 65 70 5b 62 52 65 76 5d 3b 0a 20 20 aStep[bRev];.
23900 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 pLevel->p1 = i
23910 43 75 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d Cur;. pLevel-
23920 3e 70 32 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 >p2 = 1 + sqlite
23930 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 61 3VdbeAddOp2(v, a
23940 53 74 61 72 74 5b 62 52 65 76 5d 2c 20 69 43 75 Start[bRev], iCu
23950 72 2c 20 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 r, addrBrk);.
23960 20 70 4c 65 76 65 6c 2d 3e 70 35 20 3d 20 53 51 pLevel->p5 = SQ
23970 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f LITE_STMTSTATUS_
23980 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3b 0a 20 FULLSCAN_STEP;.
23990 20 7d 0a 0a 20 20 2f 2a 20 49 6e 73 65 72 74 20 }.. /* Insert
239a0 63 6f 64 65 20 74 6f 20 74 65 73 74 20 65 76 65 code to test eve
239b0 72 79 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e ry subexpression
239c0 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d that can be com
239d0 70 6c 65 74 65 6c 79 0a 20 20 2a 2a 20 63 6f 6d pletely. ** com
239e0 70 75 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 puted using the
239f0 63 75 72 72 65 6e 74 20 73 65 74 20 6f 66 20 74 current set of t
23a00 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f ables.. */. fo
23a10 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 r(pTerm=pWC->a,
23a20 6a 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e j=pWC->nTerm; j>
23a30 30 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 0; j--, pTerm++)
23a40 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 3b 0a {. Expr *pE;.
23a50 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 testcase( pT
23a60 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 erm->wtFlags & T
23a70 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 ERM_VIRTUAL );.
23a80 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 testcase( pTe
23a90 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 rm->wtFlags & TE
23aa0 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 RM_CODED );.
23ab0 69 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 if( pTerm->wtFla
23ac0 67 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55 gs & (TERM_VIRTU
23ad0 41 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 AL|TERM_CODED) )
23ae0 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 continue;. i
23af0 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 f( (pTerm->prere
23b00 71 41 6c 6c 20 26 20 70 4c 65 76 65 6c 2d 3e 6e qAll & pLevel->n
23b10 6f 74 52 65 61 64 79 29 21 3d 30 20 29 7b 0a 20 otReady)!=0 ){.
23b20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 testcase( p
23b30 57 49 6e 66 6f 2d 3e 75 6e 74 65 73 74 65 64 54 WInfo->untestedT
23b40 65 72 6d 73 3d 3d 30 0a 20 20 20 20 20 20 20 20 erms==0.
23b50 20 20 20 20 20 20 20 26 26 20 28 70 57 49 6e 66 && (pWInf
23b60 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 20 o->wctrlFlags &
23b70 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f 4f WHERE_ONETABLE_O
23b80 4e 4c 59 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 NLY)!=0 );.
23b90 20 70 57 49 6e 66 6f 2d 3e 75 6e 74 65 73 74 65 pWInfo->unteste
23ba0 64 54 65 72 6d 73 20 3d 20 31 3b 0a 20 20 20 20 dTerms = 1;.
23bb0 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
23bc0 7d 0a 20 20 20 20 70 45 20 3d 20 70 54 65 72 6d }. pE = pTerm
23bd0 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 61 73 73 ->pExpr;. ass
23be0 65 72 74 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 ert( pE!=0 );.
23bf0 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c if( pLevel->iL
23c00 65 66 74 4a 6f 69 6e 20 26 26 20 21 45 78 70 72 eftJoin && !Expr
23c10 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 2c 20 HasProperty(pE,
23c20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a EP_FromJoin) ){.
23c30 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a continue;.
23c40 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
23c50 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 3ExprIfFalse(pPa
23c60 72 73 65 2c 20 70 45 2c 20 61 64 64 72 43 6f 6e rse, pE, addrCon
23c70 74 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 t, SQLITE_JUMPIF
23c80 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 54 65 72 6d NULL);. pTerm
23c90 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 ->wtFlags |= TER
23ca0 4d 5f 43 4f 44 45 44 3b 0a 20 20 7d 0a 0a 20 20 M_CODED;. }..
23cb0 2f 2a 20 49 6e 73 65 72 74 20 63 6f 64 65 20 74 /* Insert code t
23cc0 6f 20 74 65 73 74 20 66 6f 72 20 69 6d 70 6c 69 o test for impli
23cd0 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 62 ed constraints b
23ce0 61 73 65 64 20 6f 6e 20 74 72 61 6e 73 69 74 69 ased on transiti
23cf0 76 69 74 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 vity. ** of the
23d00 20 22 3d 3d 22 20 6f 70 65 72 61 74 6f 72 2e 0a "==" operator..
23d10 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 61 6d 70 6c **. ** Exampl
23d20 65 3a 20 49 66 20 74 68 65 20 57 48 45 52 45 20 e: If the WHERE
23d30 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 clause contains
23d40 22 74 31 2e 61 3d 74 32 2e 62 22 20 61 6e 64 20 "t1.a=t2.b" and
23d50 22 74 32 2e 62 3d 31 32 33 22 0a 20 20 2a 2a 20 "t2.b=123". **
23d60 61 6e 64 20 77 65 20 61 72 65 20 63 6f 64 69 6e and we are codin
23d70 67 20 74 68 65 20 74 31 20 6c 6f 6f 70 20 61 6e g the t1 loop an
23d80 64 20 74 68 65 20 74 32 20 6c 6f 6f 70 20 68 61 d the t2 loop ha
23d90 73 20 6e 6f 74 20 79 65 74 20 63 6f 64 65 64 2c s not yet coded,
23da0 0a 20 20 2a 2a 20 74 68 65 6e 20 77 65 20 63 61 . ** then we ca
23db0 6e 6e 6f 74 20 75 73 65 20 74 68 65 20 22 74 31 nnot use the "t1
23dc0 2e 61 3d 74 32 2e 62 22 20 63 6f 6e 73 74 72 61 .a=t2.b" constra
23dd0 69 6e 74 2c 20 62 75 74 20 77 65 20 63 61 6e 20 int, but we can
23de0 63 6f 64 65 0a 20 20 2a 2a 20 74 68 65 20 69 6d code. ** the im
23df0 70 6c 69 65 64 20 22 74 31 2e 61 3d 31 32 33 22 plied "t1.a=123"
23e00 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 2a constraint.. *
23e10 2f 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 /. for(pTerm=pW
23e20 43 2d 3e 61 2c 20 6a 3d 70 57 43 2d 3e 6e 54 65 C->a, j=pWC->nTe
23e30 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70 54 rm; j>0; j--, pT
23e40 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 erm++){. Expr
23e50 20 2a 70 45 2c 20 2a 70 45 41 6c 74 3b 0a 20 20 *pE, *pEAlt;.
23e60 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 41 6c WhereTerm *pAl
23e70 74 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d t;. if( pTerm
23e80 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 ->wtFlags & (TER
23e90 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 M_VIRTUAL|TERM_C
23ea0 4f 44 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 ODED) ) continue
23eb0 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d ;. if( pTerm-
23ec0 3e 65 4f 70 65 72 61 74 6f 72 21 3d 28 57 4f 5f >eOperator!=(WO_
23ed0 45 51 55 49 56 7c 57 4f 5f 45 51 29 20 29 20 63 EQUIV|WO_EQ) ) c
23ee0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 ontinue;. if(
23ef0 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 pTerm->leftCurs
23f00 6f 72 21 3d 69 43 75 72 20 29 20 63 6f 6e 74 69 or!=iCur ) conti
23f10 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 nue;. if( pLe
23f20 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 vel->iLeftJoin )
23f30 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 continue;. p
23f40 45 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 E = pTerm->pExpr
23f50 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 45 ;. assert( !E
23f60 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
23f70 45 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 E, EP_FromJoin)
23f80 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 );. assert( (
23f90 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 pTerm->prereqRig
23fa0 68 74 20 26 20 70 4c 65 76 65 6c 2d 3e 6e 6f 74 ht & pLevel->not
23fb0 52 65 61 64 79 29 21 3d 30 20 29 3b 0a 20 20 20 Ready)!=0 );.
23fc0 20 70 41 6c 74 20 3d 20 66 69 6e 64 54 65 72 6d pAlt = findTerm
23fd0 28 70 57 43 2c 20 69 43 75 72 2c 20 70 54 65 72 (pWC, iCur, pTer
23fe0 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 2c m->u.leftColumn,
23ff0 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 notReady, WO_EQ
24000 7c 57 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20 |WO_IN, 0);.
24010 69 66 28 20 70 41 6c 74 3d 3d 30 20 29 20 63 6f if( pAlt==0 ) co
24020 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 ntinue;. if(
24030 70 41 6c 74 2d 3e 77 74 46 6c 61 67 73 20 26 20 pAlt->wtFlags &
24040 28 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 (TERM_CODED) ) c
24050 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 74 65 73 ontinue;. tes
24060 74 63 61 73 65 28 20 70 41 6c 74 2d 3e 65 4f 70 tcase( pAlt->eOp
24070 65 72 61 74 6f 72 20 26 20 57 4f 5f 45 51 20 29 erator & WO_EQ )
24080 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 ;. testcase(
24090 70 41 6c 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 pAlt->eOperator
240a0 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20 56 & WO_IN );. V
240b0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 dbeNoopComment((
240c0 76 2c 20 22 62 65 67 69 6e 20 74 72 61 6e 73 69 v, "begin transi
240d0 74 69 76 65 20 63 6f 6e 73 74 72 61 69 6e 74 22 tive constraint"
240e0 29 29 3b 0a 20 20 20 20 70 45 41 6c 74 20 3d 20 ));. pEAlt =
240f0 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f sqlite3StackAllo
24100 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 cRaw(db, sizeof(
24110 2a 70 45 41 6c 74 29 29 3b 0a 20 20 20 20 69 66 *pEAlt));. if
24120 28 20 70 45 41 6c 74 20 29 7b 0a 20 20 20 20 20 ( pEAlt ){.
24130 20 2a 70 45 41 6c 74 20 3d 20 2a 70 41 6c 74 2d *pEAlt = *pAlt-
24140 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 70 45 >pExpr;. pE
24150 41 6c 74 2d 3e 70 4c 65 66 74 20 3d 20 70 45 2d Alt->pLeft = pE-
24160 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 73 71 >pLeft;. sq
24170 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 lite3ExprIfFalse
24180 28 70 50 61 72 73 65 2c 20 70 45 41 6c 74 2c 20 (pParse, pEAlt,
24190 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45 addrCont, SQLITE
241a0 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 _JUMPIFNULL);.
241b0 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b sqlite3Stack
241c0 46 72 65 65 28 64 62 2c 20 70 45 41 6c 74 29 3b Free(db, pEAlt);
241d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a . }. }.. /*
241e0 20 46 6f 72 20 61 20 4c 45 46 54 20 4f 55 54 45 For a LEFT OUTE
241f0 52 20 4a 4f 49 4e 2c 20 67 65 6e 65 72 61 74 65 R JOIN, generate
24200 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 code that will
24210 72 65 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 record the fact
24220 74 68 61 74 0a 20 20 2a 2a 20 61 74 20 6c 65 61 that. ** at lea
24230 73 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 st one row of th
24240 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 68 61 e right table ha
24250 73 20 6d 61 74 63 68 65 64 20 74 68 65 20 6c 65 s matched the le
24260 66 74 20 74 61 62 6c 65 2e 20 20 0a 20 20 2a 2f ft table. . */
24270 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 . if( pLevel->i
24280 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 LeftJoin ){.
24290 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46 69 72 73 pLevel->addrFirs
242a0 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 t = sqlite3VdbeC
242b0 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 urrentAddr(v);.
242c0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
242d0 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
242e0 65 72 2c 20 31 2c 20 70 4c 65 76 65 6c 2d 3e 69 er, 1, pLevel->i
242f0 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 56 LeftJoin);. V
24300 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
24310 72 65 63 6f 72 64 20 4c 45 46 54 20 4a 4f 49 4e record LEFT JOIN
24320 20 68 69 74 22 29 29 3b 0a 20 20 20 20 73 71 6c hit"));. sql
24330 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 ite3ExprCacheCle
24340 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 ar(pParse);.
24350 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 for(pTerm=pWC->a
24360 2c 20 6a 3d 30 3b 20 6a 3c 70 57 43 2d 3e 6e 54 , j=0; j<pWC->nT
24370 65 72 6d 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b erm; j++, pTerm+
24380 2b 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 +){. testca
24390 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 se( pTerm->wtFla
243a0 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 gs & TERM_VIRTUA
243b0 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 L );. testc
243c0 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c ase( pTerm->wtFl
243d0 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 ags & TERM_CODED
243e0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 );. if( pT
243f0 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 erm->wtFlags & (
24400 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 TERM_VIRTUAL|TER
24410 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e 74 69 M_CODED) ) conti
24420 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20 28 nue;. if( (
24430 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c pTerm->prereqAll
24440 20 26 20 70 4c 65 76 65 6c 2d 3e 6e 6f 74 52 65 & pLevel->notRe
24450 61 64 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ady)!=0 ){.
24460 20 20 20 61 73 73 65 72 74 28 20 70 57 49 6e 66 assert( pWInf
24470 6f 2d 3e 75 6e 74 65 73 74 65 64 54 65 72 6d 73 o->untestedTerms
24480 20 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74 );. cont
24490 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 inue;. }.
244a0 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 assert( pTer
244b0 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20 20 20 20 m->pExpr );.
244c0 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 sqlite3ExprIfF
244d0 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54 65 alse(pParse, pTe
244e0 72 6d 2d 3e 70 45 78 70 72 2c 20 61 64 64 72 43 rm->pExpr, addrC
244f0 6f 6e 74 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 ont, SQLITE_JUMP
24500 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 IFNULL);. p
24510 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d Term->wtFlags |=
24520 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 TERM_CODED;.
24530 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 }. }. sqlite3
24540 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
24550 50 61 72 73 65 2c 20 69 52 65 6c 65 61 73 65 52 Parse, iReleaseR
24560 65 67 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 eg);.. return p
24570 4c 65 76 65 6c 2d 3e 6e 6f 74 52 65 61 64 79 3b Level->notReady;
24580 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 .}..#if defined(
24590 57 48 45 52 45 54 52 41 43 45 5f 45 4e 41 42 4c WHERETRACE_ENABL
245a0 45 44 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 ED) && defined(S
245b0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45 QLITE_ENABLE_TRE
245c0 45 5f 45 58 50 4c 41 49 4e 29 0a 2f 2a 0a 2a 2a E_EXPLAIN)./*.**
245d0 20 47 65 6e 65 72 61 74 65 20 22 45 78 70 6c 61 Generate "Expla
245e0 6e 61 74 69 6f 6e 22 20 74 65 78 74 20 66 6f 72 nation" text for
245f0 20 61 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2f a WhereTerm..*/
24600 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 .static void whe
24610 72 65 45 78 70 6c 61 69 6e 54 65 72 6d 28 56 64 reExplainTerm(Vd
24620 62 65 20 2a 76 2c 20 57 68 65 72 65 54 65 72 6d be *v, WhereTerm
24630 20 2a 70 54 65 72 6d 29 7b 0a 20 20 63 68 61 72 *pTerm){. char
24640 20 7a 54 79 70 65 5b 34 5d 3b 0a 20 20 6d 65 6d zType[4];. mem
24650 63 70 79 28 7a 54 79 70 65 2c 20 22 2e 2e 2e 22 cpy(zType, "..."
24660 2c 20 34 29 3b 0a 20 20 69 66 28 20 70 54 65 72 , 4);. if( pTer
24670 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 m->wtFlags & TER
24680 4d 5f 56 49 52 54 55 41 4c 20 29 20 7a 54 79 70 M_VIRTUAL ) zTyp
24690 65 5b 30 5d 20 3d 20 27 56 27 3b 0a 20 20 69 66 e[0] = 'V';. if
246a0 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 ( pTerm->eOperat
246b0 6f 72 20 26 20 57 4f 5f 45 51 55 49 56 20 20 29 or & WO_EQUIV )
246c0 20 7a 54 79 70 65 5b 31 5d 20 3d 20 27 45 27 3b zType[1] = 'E';
246d0 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 . if( ExprHasPr
246e0 6f 70 65 72 74 79 28 70 54 65 72 6d 2d 3e 70 45 operty(pTerm->pE
246f0 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e xpr, EP_FromJoin
24700 29 20 29 20 7a 54 79 70 65 5b 32 5d 20 3d 20 27 ) ) zType[2] = '
24710 4c 27 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 L';. sqlite3Exp
24720 6c 61 69 6e 50 72 69 6e 74 66 28 76 2c 20 22 25 lainPrintf(v, "%
24730 73 20 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 69 s ", zType);. i
24740 66 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 f( (pTerm->wtFla
24750 67 73 20 26 20 28 54 45 52 4d 5f 4f 52 49 4e 46 gs & (TERM_ORINF
24760 4f 7c 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 29 29 O|TERM_ANDINFO))
24770 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ==0 ){. sqlit
24780 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 e3ExplainPrintf(
24790 76 2c 20 22 6c 68 73 3d 25 2d 32 64 20 22 2c 20 v, "lhs=%-2d ",
247a0 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c pTerm->u.leftCol
247b0 75 6d 6e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 umn);. }. sqli
247c0 74 65 33 45 78 70 6c 61 69 6e 45 78 70 72 28 76 te3ExplainExpr(v
247d0 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 29 3b , pTerm->pExpr);
247e0 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 57 48 45 .}.#endif /* WHE
247f0 52 45 54 52 41 43 45 5f 45 4e 41 42 4c 45 44 20 RETRACE_ENABLED
24800 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 && SQLITE_ENABLE
24810 5f 54 52 45 45 5f 45 58 50 4c 41 49 4e 20 2a 2f _TREE_EXPLAIN */
24820 0a 0a 0a 23 69 66 64 65 66 20 57 48 45 52 45 54 ...#ifdef WHERET
24830 52 41 43 45 5f 45 4e 41 42 4c 45 44 0a 2f 2a 0a RACE_ENABLED./*.
24840 2a 2a 20 50 72 69 6e 74 20 61 20 57 68 65 72 65 ** Print a Where
24850 4c 6f 6f 70 20 6f 62 6a 65 63 74 20 66 6f 72 20 Loop object for
24860 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 debugging purpos
24870 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 es.*/.static voi
24880 64 20 77 68 65 72 65 4c 6f 6f 70 50 72 69 6e 74 d whereLoopPrint
24890 28 57 68 65 72 65 4c 6f 6f 70 20 2a 70 2c 20 57 (WhereLoop *p, W
248a0 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 29 hereClause *pWC)
248b0 7b 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 {. WhereInfo *p
248c0 57 49 6e 66 6f 20 3d 20 70 57 43 2d 3e 70 57 49 WInfo = pWC->pWI
248d0 6e 66 6f 3b 0a 20 20 69 6e 74 20 6e 62 20 3d 20 nfo;. int nb =
248e0 31 2b 28 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 1+(pWInfo->pTabL
248f0 69 73 74 2d 3e 6e 53 72 63 2b 37 29 2f 38 3b 0a ist->nSrc+7)/8;.
24900 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 struct SrcList
24910 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 _item *pItem = p
24920 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 2d WInfo->pTabList-
24930 3e 61 20 2b 20 70 2d 3e 69 54 61 62 3b 0a 20 20 >a + p->iTab;.
24940 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 Table *pTab = pI
24950 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 73 71 6c tem->pTab;. sql
24960 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
24970 22 25 63 25 32 64 2e 25 30 2a 6c 6c 78 2e 25 30 "%c%2d.%0*llx.%0
24980 2a 6c 6c 78 22 2c 20 70 2d 3e 63 49 64 2c 0a 20 *llx", p->cId,.
24990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
249a0 20 20 20 20 70 2d 3e 69 54 61 62 2c 20 6e 62 2c p->iTab, nb,
249b0 20 70 2d 3e 6d 61 73 6b 53 65 6c 66 2c 20 6e 62 p->maskSelf, nb
249c0 2c 20 70 2d 3e 70 72 65 72 65 71 29 3b 0a 20 20 , p->prereq);.
249d0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
249e0 74 66 28 22 20 25 31 32 73 22 2c 0a 20 20 20 20 tf(" %12s",.
249f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24a00 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3f pItem->zAlias ?
24a10 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3a pItem->zAlias :
24a20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pTab->zName);.
24a30 20 69 66 28 20 28 70 2d 3e 77 73 46 6c 61 67 73 if( (p->wsFlags
24a40 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c & WHERE_VIRTUAL
24a50 54 41 42 4c 45 29 3d 3d 30 20 29 7b 0a 20 20 20 TABLE)==0 ){.
24a60 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e const char *zN
24a70 61 6d 65 3b 0a 20 20 20 20 20 69 66 28 20 70 2d ame;. if( p-
24a80 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 65 78 20 >u.btree.pIndex
24a90 26 26 20 28 7a 4e 61 6d 65 20 3d 20 70 2d 3e 75 && (zName = p->u
24aa0 2e 62 74 72 65 65 2e 70 49 6e 64 65 78 2d 3e 7a .btree.pIndex->z
24ab0 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 Name)!=0 ){.
24ac0 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 4e if( strncmp(zN
24ad0 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 ame, "sqlite_aut
24ae0 6f 69 6e 64 65 78 5f 22 2c 20 31 37 29 3d 3d 30 oindex_", 17)==0
24af0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 ){. int
24b00 69 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 i = sqlite3Strle
24b10 6e 33 30 28 7a 4e 61 6d 65 29 20 2d 20 31 3b 0a n30(zName) - 1;.
24b20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a while( z
24b30 4e 61 6d 65 5b 69 5d 21 3d 27 5f 27 20 29 20 69 Name[i]!='_' ) i
24b40 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d --;. zNam
24b50 65 20 2b 3d 20 69 3b 0a 20 20 20 20 20 20 7d 0a e += i;. }.
24b60 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 sqlite3Deb
24b70 75 67 50 72 69 6e 74 66 28 22 2e 25 2d 31 36 73 ugPrintf(".%-16s
24b80 20 25 32 64 22 2c 20 7a 4e 61 6d 65 2c 20 70 2d %2d", zName, p-
24b90 3e 75 2e 62 74 72 65 65 2e 6e 45 71 29 3b 0a 20 >u.btree.nEq);.
24ba0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
24bb0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
24bc0 74 66 28 22 25 32 30 73 22 2c 22 22 29 3b 0a 20 tf("%20s","");.
24bd0 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
24be0 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 69 char *z;. i
24bf0 66 28 20 70 2d 3e 75 2e 76 74 61 62 2e 69 64 78 f( p->u.vtab.idx
24c00 53 74 72 20 29 7b 0a 20 20 20 20 20 20 7a 20 3d Str ){. z =
24c10 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 sqlite3_mprintf
24c20 28 22 28 25 64 2c 5c 22 25 73 5c 22 2c 25 78 29 ("(%d,\"%s\",%x)
24c30 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ",.
24c40 20 20 20 70 2d 3e 75 2e 76 74 61 62 2e 69 64 78 p->u.vtab.idx
24c50 4e 75 6d 2c 20 70 2d 3e 75 2e 76 74 61 62 2e 69 Num, p->u.vtab.i
24c60 64 78 53 74 72 2c 20 70 2d 3e 75 2e 76 74 61 62 dxStr, p->u.vtab
24c70 2e 6f 6d 69 74 4d 61 73 6b 29 3b 0a 20 20 20 20 .omitMask);.
24c80 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d }else{. z =
24c90 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 sqlite3_mprintf
24ca0 28 22 28 25 64 2c 25 78 29 22 2c 20 70 2d 3e 75 ("(%d,%x)", p->u
24cb0 2e 76 74 61 62 2e 69 64 78 4e 75 6d 2c 20 70 2d .vtab.idxNum, p-
24cc0 3e 75 2e 76 74 61 62 2e 6f 6d 69 74 4d 61 73 6b >u.vtab.omitMask
24cd0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c );. }. sql
24ce0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
24cf0 22 20 25 2d 31 39 73 22 2c 20 7a 29 3b 0a 20 20 " %-19s", z);.
24d00 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a sqlite3_free(z
24d10 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 );. }. sqlite3
24d20 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 66 20 DebugPrintf(" f
24d30 25 30 34 78 20 4e 20 25 64 22 2c 20 70 2d 3e 77 %04x N %d", p->w
24d40 73 46 6c 61 67 73 2c 20 70 2d 3e 6e 4c 54 65 72 sFlags, p->nLTer
24d50 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 m);. sqlite3Deb
24d60 75 67 50 72 69 6e 74 66 28 22 20 63 6f 73 74 20 ugPrintf(" cost
24d70 25 64 2c 25 64 2c 25 64 5c 6e 22 2c 20 70 2d 3e %d,%d,%d\n", p->
24d80 72 53 65 74 75 70 2c 20 70 2d 3e 72 52 75 6e 2c rSetup, p->rRun,
24d90 20 70 2d 3e 6e 4f 75 74 29 3b 0a 23 69 66 64 65 p->nOut);.#ifde
24da0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f f SQLITE_ENABLE_
24db0 54 52 45 45 5f 45 58 50 4c 41 49 4e 0a 20 20 2f TREE_EXPLAIN. /
24dc0 2a 20 49 66 20 74 68 65 20 30 78 31 30 30 20 62 * If the 0x100 b
24dd0 69 74 20 6f 66 20 77 68 65 72 65 74 72 61 63 69 it of wheretraci
24de0 6e 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 ng is set, then
24df0 73 68 6f 77 20 61 6c 6c 20 6f 66 20 74 68 65 20 show all of the
24e00 63 6f 6e 73 74 72 61 69 6e 74 0a 20 20 2a 2a 20 constraint. **
24e10 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 expressions in t
24e20 68 65 20 57 68 65 72 65 4c 6f 6f 70 2e 61 4c 54 he WhereLoop.aLT
24e30 65 72 6d 5b 5d 20 61 72 72 61 79 2e 0a 20 20 2a erm[] array.. *
24e40 2f 0a 20 20 69 66 28 20 70 2d 3e 6e 4c 54 65 72 /. if( p->nLTer
24e50 6d 20 26 26 20 28 73 71 6c 69 74 65 33 57 68 65 m && (sqlite3Whe
24e60 72 65 54 72 61 63 65 20 26 20 30 78 31 30 30 29 reTrace & 0x100)
24e70 21 3d 30 20 29 7b 20 20 2f 2a 20 57 48 45 52 45 !=0 ){ /* WHERE
24e80 54 52 41 43 45 20 30 78 31 30 30 20 2a 2f 0a 20 TRACE 0x100 */.
24e90 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 int i;. Vd
24ea0 62 65 20 2a 76 20 3d 20 70 57 49 6e 66 6f 2d 3e be *v = pWInfo->
24eb0 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 pParse->pVdbe;.
24ec0 20 20 20 73 71 6c 69 74 65 33 45 78 70 6c 61 69 sqlite3Explai
24ed0 6e 42 65 67 69 6e 28 76 29 3b 0a 20 20 20 20 66 nBegin(v);. f
24ee0 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4c 54 or(i=0; i<p->nLT
24ef0 65 72 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 erm; i++){.
24f00 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 WhereTerm *pTer
24f10 6d 20 3d 20 70 2d 3e 61 4c 54 65 72 6d 5b 69 5d m = p->aLTerm[i]
24f20 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
24f30 78 70 6c 61 69 6e 50 72 69 6e 74 66 28 76 2c 20 xplainPrintf(v,
24f40 22 20 20 28 25 64 29 20 23 25 64 20 22 2c 20 69 " (%d) #%d ", i
24f50 2b 31 2c 20 28 69 6e 74 29 28 70 54 65 72 6d 2d +1, (int)(pTerm-
24f60 70 57 43 2d 3e 61 29 29 3b 0a 20 20 20 20 20 20 pWC->a));.
24f70 73 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 75 sqlite3ExplainPu
24f80 73 68 28 76 29 3b 0a 20 20 20 20 20 20 77 68 65 sh(v);. whe
24f90 72 65 45 78 70 6c 61 69 6e 54 65 72 6d 28 76 2c reExplainTerm(v,
24fa0 20 70 54 65 72 6d 29 3b 0a 20 20 20 20 20 20 73 pTerm);. s
24fb0 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 6f 70 qlite3ExplainPop
24fc0 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 (v);. sqlit
24fd0 65 33 45 78 70 6c 61 69 6e 4e 4c 28 76 29 3b 0a e3ExplainNL(v);.
24fe0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
24ff0 33 45 78 70 6c 61 69 6e 46 69 6e 69 73 68 28 76 3ExplainFinish(v
25000 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 );. sqlite3De
25010 62 75 67 50 72 69 6e 74 66 28 22 25 73 22 2c 20 bugPrintf("%s",
25020 73 71 6c 69 74 65 33 56 64 62 65 45 78 70 6c 61 sqlite3VdbeExpla
25030 6e 61 74 69 6f 6e 28 76 29 29 3b 0a 20 20 7d 0a nation(v));. }.
25040 23 65 6e 64 69 66 0a 7d 0a 23 65 6e 64 69 66 0a #endif.}.#endif.
25050 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 62 ./*.** Convert b
25060 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 ulk memory into
25070 61 20 76 61 6c 69 64 20 57 68 65 72 65 4c 6f 6f a valid WhereLoo
25080 70 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 61 p that can be pa
25090 73 73 65 64 0a 2a 2a 20 74 6f 20 77 68 65 72 65 ssed.** to where
250a0 4c 6f 6f 70 43 6c 65 61 72 20 68 61 72 6d 6c 65 LoopClear harmle
250b0 73 73 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ssly..*/.static
250c0 76 6f 69 64 20 77 68 65 72 65 4c 6f 6f 70 49 6e void whereLoopIn
250d0 69 74 28 57 68 65 72 65 4c 6f 6f 70 20 2a 70 29 it(WhereLoop *p)
250e0 7b 0a 20 20 70 2d 3e 61 4c 54 65 72 6d 20 3d 20 {. p->aLTerm =
250f0 70 2d 3e 61 4c 54 65 72 6d 53 70 61 63 65 3b 0a p->aLTermSpace;.
25100 20 20 70 2d 3e 6e 4c 54 65 72 6d 20 3d 20 30 3b p->nLTerm = 0;
25110 0a 20 20 70 2d 3e 6e 4c 53 6c 6f 74 20 3d 20 41 . p->nLSlot = A
25120 72 72 61 79 53 69 7a 65 28 70 2d 3e 61 4c 54 65 rraySize(p->aLTe
25130 72 6d 53 70 61 63 65 29 3b 0a 20 20 70 2d 3e 77 rmSpace);. p->w
25140 73 46 6c 61 67 73 20 3d 20 30 3b 0a 7d 0a 0a 2f sFlags = 0;.}../
25150 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 57 *.** Clear the W
25160 68 65 72 65 4c 6f 6f 70 2e 75 20 75 6e 69 6f 6e hereLoop.u union
25170 2e 20 20 4c 65 61 76 65 20 57 68 65 72 65 4c 6f . Leave WhereLo
25180 6f 70 2e 70 4c 54 65 72 6d 20 69 6e 74 61 63 74 op.pLTerm intact
25190 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 ..*/.static void
251a0 20 77 68 65 72 65 4c 6f 6f 70 43 6c 65 61 72 55 whereLoopClearU
251b0 6e 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 nion(sqlite3 *db
251c0 2c 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 29 7b , WhereLoop *p){
251d0 0a 20 20 69 66 28 20 70 2d 3e 77 73 46 6c 61 67 . if( p->wsFlag
251e0 73 20 26 20 28 57 48 45 52 45 5f 56 49 52 54 55 s & (WHERE_VIRTU
251f0 41 4c 54 41 42 4c 45 7c 57 48 45 52 45 5f 41 55 ALTABLE|WHERE_AU
25200 54 4f 5f 49 4e 44 45 58 29 20 29 7b 0a 20 20 20 TO_INDEX) ){.
25210 20 69 66 28 20 28 70 2d 3e 77 73 46 6c 61 67 73 if( (p->wsFlags
25220 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c & WHERE_VIRTUAL
25230 54 41 42 4c 45 29 21 3d 30 20 26 26 20 70 2d 3e TABLE)!=0 && p->
25240 75 2e 76 74 61 62 2e 6e 65 65 64 46 72 65 65 20 u.vtab.needFree
25250 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 ){. sqlite3
25260 5f 66 72 65 65 28 70 2d 3e 75 2e 76 74 61 62 2e _free(p->u.vtab.
25270 69 64 78 53 74 72 29 3b 0a 20 20 20 20 20 20 70 idxStr);. p
25280 2d 3e 75 2e 76 74 61 62 2e 6e 65 65 64 46 72 65 ->u.vtab.needFre
25290 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e e = 0;. p->
252a0 75 2e 76 74 61 62 2e 69 64 78 53 74 72 20 3d 20 u.vtab.idxStr =
252b0 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 0;. }else if(
252c0 20 28 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 (p->wsFlags & W
252d0 48 45 52 45 5f 41 55 54 4f 5f 49 4e 44 45 58 29 HERE_AUTO_INDEX)
252e0 21 3d 30 20 26 26 20 70 2d 3e 75 2e 62 74 72 65 !=0 && p->u.btre
252f0 65 2e 70 49 6e 64 65 78 21 3d 30 20 29 7b 0a 20 e.pIndex!=0 ){.
25300 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 sqlite3DbFr
25310 65 65 28 64 62 2c 20 70 2d 3e 75 2e 62 74 72 65 ee(db, p->u.btre
25320 65 2e 70 49 6e 64 65 78 2d 3e 7a 43 6f 6c 41 66 e.pIndex->zColAf
25330 66 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 f);. sqlite
25340 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 75 3DbFree(db, p->u
25350 2e 62 74 72 65 65 2e 70 49 6e 64 65 78 29 3b 0a .btree.pIndex);.
25360 20 20 20 20 20 20 70 2d 3e 75 2e 62 74 72 65 65 p->u.btree
25370 2e 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20 .pIndex = 0;.
25380 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 }. }.}../*.**
25390 44 65 61 6c 6c 6f 63 61 74 65 20 69 6e 74 65 72 Deallocate inter
253a0 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 nal memory used
253b0 62 79 20 61 20 57 68 65 72 65 4c 6f 6f 70 20 6f by a WhereLoop o
253c0 62 6a 65 63 74 0a 2a 2f 0a 73 74 61 74 69 63 20 bject.*/.static
253d0 76 6f 69 64 20 77 68 65 72 65 4c 6f 6f 70 43 6c void whereLoopCl
253e0 65 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c ear(sqlite3 *db,
253f0 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 29 7b 0a WhereLoop *p){.
25400 20 20 69 66 28 20 70 2d 3e 61 4c 54 65 72 6d 21 if( p->aLTerm!
25410 3d 70 2d 3e 61 4c 54 65 72 6d 53 70 61 63 65 20 =p->aLTermSpace
25420 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 ) sqlite3DbFree(
25430 64 62 2c 20 70 2d 3e 61 4c 54 65 72 6d 29 3b 0a db, p->aLTerm);.
25440 20 20 77 68 65 72 65 4c 6f 6f 70 43 6c 65 61 72 whereLoopClear
25450 55 6e 69 6f 6e 28 64 62 2c 20 70 29 3b 0a 20 20 Union(db, p);.
25460 77 68 65 72 65 4c 6f 6f 70 49 6e 69 74 28 70 29 whereLoopInit(p)
25470 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 ;.}../*.** Incre
25480 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 ase the memory a
25490 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f 72 20 70 4c llocation for pL
254a0 6f 6f 70 2d 3e 61 4c 54 65 72 6d 5b 5d 20 74 6f oop->aLTerm[] to
254b0 20 62 65 20 61 74 20 6c 65 61 73 74 20 6e 2e 0a be at least n..
254c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 */.static int wh
254d0 65 72 65 4c 6f 6f 70 52 65 73 69 7a 65 28 73 71 ereLoopResize(sq
254e0 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65 lite3 *db, Where
254f0 4c 6f 6f 70 20 2a 70 2c 20 69 6e 74 20 6e 29 7b Loop *p, int n){
25500 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 2a 70 . WhereTerm **p
25510 61 4e 65 77 3b 0a 20 20 69 66 28 20 70 2d 3e 6e aNew;. if( p->n
25520 4c 53 6c 6f 74 3e 3d 6e 20 29 20 72 65 74 75 72 LSlot>=n ) retur
25530 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 6e n SQLITE_OK;. n
25540 20 3d 20 28 6e 2b 37 29 26 7e 37 3b 0a 20 20 70 = (n+7)&~7;. p
25550 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 aNew = sqlite3Db
25560 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 MallocRaw(db, si
25570 7a 65 6f 66 28 70 2d 3e 61 4c 54 65 72 6d 5b 30 zeof(p->aLTerm[0
25580 5d 29 2a 6e 29 3b 0a 20 20 69 66 28 20 70 61 4e ])*n);. if( paN
25590 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 ew==0 ) return S
255a0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d QLITE_NOMEM;. m
255b0 65 6d 63 70 79 28 70 61 4e 65 77 2c 20 70 2d 3e emcpy(paNew, p->
255c0 61 4c 54 65 72 6d 2c 20 73 69 7a 65 6f 66 28 70 aLTerm, sizeof(p
255d0 2d 3e 61 4c 54 65 72 6d 5b 30 5d 29 2a 70 2d 3e ->aLTerm[0])*p->
255e0 6e 4c 53 6c 6f 74 29 3b 0a 20 20 69 66 28 20 70 nLSlot);. if( p
255f0 2d 3e 61 4c 54 65 72 6d 21 3d 70 2d 3e 61 4c 54 ->aLTerm!=p->aLT
25600 65 72 6d 53 70 61 63 65 20 29 20 73 71 6c 69 74 ermSpace ) sqlit
25610 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e e3DbFree(db, p->
25620 61 4c 54 65 72 6d 29 3b 0a 20 20 70 2d 3e 61 4c aLTerm);. p->aL
25630 54 65 72 6d 20 3d 20 70 61 4e 65 77 3b 0a 20 20 Term = paNew;.
25640 70 2d 3e 6e 4c 53 6c 6f 74 20 3d 20 6e 3b 0a 20 p->nLSlot = n;.
25650 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
25660 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e K;.}../*.** Tran
25670 73 66 65 72 20 63 6f 6e 74 65 6e 74 20 66 72 6f sfer content fro
25680 6d 20 74 68 65 20 73 65 63 6f 6e 64 20 70 4c 6f m the second pLo
25690 6f 70 20 69 6e 74 6f 20 74 68 65 20 66 69 72 73 op into the firs
256a0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
256b0 20 77 68 65 72 65 4c 6f 6f 70 58 66 65 72 28 73 whereLoopXfer(s
256c0 71 6c 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72 qlite3 *db, Wher
256d0 65 4c 6f 6f 70 20 2a 70 54 6f 2c 20 57 68 65 72 eLoop *pTo, Wher
256e0 65 4c 6f 6f 70 20 2a 70 46 72 6f 6d 29 7b 0a 20 eLoop *pFrom){.
256f0 20 77 68 65 72 65 4c 6f 6f 70 43 6c 65 61 72 55 whereLoopClearU
25700 6e 69 6f 6e 28 64 62 2c 20 70 54 6f 29 3b 0a 20 nion(db, pTo);.
25710 20 69 66 28 20 77 68 65 72 65 4c 6f 6f 70 52 65 if( whereLoopRe
25720 73 69 7a 65 28 64 62 2c 20 70 54 6f 2c 20 70 46 size(db, pTo, pF
25730 72 6f 6d 2d 3e 6e 4c 54 65 72 6d 29 20 29 7b 0a rom->nLTerm) ){.
25740 20 20 20 20 6d 65 6d 73 65 74 28 26 70 54 6f 2d memset(&pTo-
25750 3e 75 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 54 >u, 0, sizeof(pT
25760 6f 2d 3e 75 29 29 3b 0a 20 20 20 20 72 65 74 75 o->u));. retu
25770 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b rn SQLITE_NOMEM;
25780 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 54 . }. memcpy(pT
25790 6f 2c 20 70 46 72 6f 6d 2c 20 57 48 45 52 45 5f o, pFrom, WHERE_
257a0 4c 4f 4f 50 5f 58 46 45 52 5f 53 5a 29 3b 0a 20 LOOP_XFER_SZ);.
257b0 20 6d 65 6d 63 70 79 28 70 54 6f 2d 3e 61 4c 54 memcpy(pTo->aLT
257c0 65 72 6d 2c 20 70 46 72 6f 6d 2d 3e 61 4c 54 65 erm, pFrom->aLTe
257d0 72 6d 2c 20 70 54 6f 2d 3e 6e 4c 54 65 72 6d 2a rm, pTo->nLTerm*
257e0 73 69 7a 65 6f 66 28 70 54 6f 2d 3e 61 4c 54 65 sizeof(pTo->aLTe
257f0 72 6d 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20 70 rm[0]));. if( p
25800 46 72 6f 6d 2d 3e 77 73 46 6c 61 67 73 20 26 20 From->wsFlags &
25810 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 WHERE_VIRTUALTAB
25820 4c 45 20 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d LE ){. pFrom-
25830 3e 75 2e 76 74 61 62 2e 6e 65 65 64 46 72 65 65 >u.vtab.needFree
25840 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 = 0;. }else if
25850 28 20 28 70 46 72 6f 6d 2d 3e 77 73 46 6c 61 67 ( (pFrom->wsFlag
25860 73 20 26 20 57 48 45 52 45 5f 41 55 54 4f 5f 49 s & WHERE_AUTO_I
25870 4e 44 45 58 29 21 3d 30 20 29 7b 0a 20 20 20 20 NDEX)!=0 ){.
25880 70 46 72 6f 6d 2d 3e 75 2e 62 74 72 65 65 2e 70 pFrom->u.btree.p
25890 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 7d 0a 20 Index = 0;. }.
258a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f return SQLITE_O
258b0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 K;.}../*.** Dele
258c0 74 65 20 61 20 57 68 65 72 65 4c 6f 6f 70 20 6f te a WhereLoop o
258d0 62 6a 65 63 74 0a 2a 2f 0a 73 74 61 74 69 63 20 bject.*/.static
258e0 76 6f 69 64 20 77 68 65 72 65 4c 6f 6f 70 44 65 void whereLoopDe
258f0 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 lete(sqlite3 *db
25900 2c 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 29 7b , WhereLoop *p){
25910 0a 20 20 77 68 65 72 65 4c 6f 6f 70 43 6c 65 61 . whereLoopClea
25920 72 28 64 62 2c 20 70 29 3b 0a 20 20 73 71 6c 69 r(db, p);. sqli
25930 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 te3DbFree(db, p)
25940 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 ;.}../*.** Free
25950 61 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72 75 a WhereInfo stru
25960 63 74 75 72 65 0a 2a 2f 0a 73 74 61 74 69 63 20 cture.*/.static
25970 76 6f 69 64 20 77 68 65 72 65 49 6e 66 6f 46 72 void whereInfoFr
25980 65 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 ee(sqlite3 *db,
25990 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 WhereInfo *pWInf
259a0 6f 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 o){. if( ALWAYS
259b0 28 70 57 49 6e 66 6f 29 20 29 7b 0a 20 20 20 20 (pWInfo) ){.
259c0 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 whereClauseClear
259d0 28 26 70 57 49 6e 66 6f 2d 3e 73 57 43 29 3b 0a (&pWInfo->sWC);.
259e0 20 20 20 20 77 68 69 6c 65 28 20 70 57 49 6e 66 while( pWInf
259f0 6f 2d 3e 70 4c 6f 6f 70 73 20 29 7b 0a 20 20 20 o->pLoops ){.
25a00 20 20 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 20 WhereLoop *p
25a10 3d 20 70 57 49 6e 66 6f 2d 3e 70 4c 6f 6f 70 73 = pWInfo->pLoops
25a20 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 2d 3e ;. pWInfo->
25a30 70 4c 6f 6f 70 73 20 3d 20 70 2d 3e 70 4e 65 78 pLoops = p->pNex
25a40 74 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 77 68 65 tLoop;. whe
25a50 72 65 4c 6f 6f 70 44 65 6c 65 74 65 28 64 62 2c reLoopDelete(db,
25a60 20 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 p);. }. s
25a70 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
25a80 20 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a pWInfo);. }.}.
25a90 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 6f 72 ./*.** Insert or
25aa0 20 72 65 70 6c 61 63 65 20 61 20 57 68 65 72 65 replace a Where
25ab0 4c 6f 6f 70 20 65 6e 74 72 79 20 75 73 69 6e 67 Loop entry using
25ac0 20 74 68 65 20 74 65 6d 70 6c 61 74 65 20 73 75 the template su
25ad0 70 70 6c 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e pplied..**.** An
25ae0 20 65 78 69 73 74 69 6e 67 20 57 68 65 72 65 4c existing WhereL
25af0 6f 6f 70 20 65 6e 74 72 79 20 6d 69 67 68 74 20 oop entry might
25b00 62 65 20 6f 76 65 72 77 72 69 74 74 65 6e 20 69 be overwritten i
25b10 66 20 74 68 65 20 6e 65 77 20 74 65 6d 70 6c 61 f the new templa
25b20 74 65 0a 2a 2a 20 69 73 20 62 65 74 74 65 72 20 te.** is better
25b30 61 6e 64 20 68 61 73 20 66 65 77 65 72 20 64 65 and has fewer de
25b40 70 65 6e 64 65 6e 63 69 65 73 2e 20 20 4f 72 20 pendencies. Or
25b50 74 68 65 20 74 65 6d 70 6c 61 74 65 20 77 69 6c the template wil
25b60 6c 20 62 65 20 69 67 6e 6f 72 65 64 0a 2a 2a 20 l be ignored.**
25b70 61 6e 64 20 6e 6f 20 69 6e 73 65 72 74 20 77 69 and no insert wi
25b80 6c 6c 20 6f 63 63 75 72 20 69 66 20 61 6e 20 65 ll occur if an e
25b90 78 69 73 74 69 6e 67 20 57 68 65 72 65 4c 6f 6f xisting WhereLoo
25ba0 70 20 69 73 20 66 61 73 74 65 72 20 61 6e 64 20 p is faster and
25bb0 68 61 73 0a 2a 2a 20 66 65 77 65 72 20 64 65 70 has.** fewer dep
25bc0 65 6e 64 65 6e 63 69 65 73 20 74 68 61 6e 20 74 endencies than t
25bd0 68 65 20 74 65 6d 70 6c 61 74 65 2e 20 20 4f 74 he template. Ot
25be0 68 65 72 77 69 73 65 20 61 20 6e 65 77 20 57 68 herwise a new Wh
25bf0 65 72 65 4c 6f 6f 70 20 69 73 0a 2a 2a 20 61 64 ereLoop is.** ad
25c00 64 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65 ded based on the
25c10 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a template..**.**
25c20 20 49 66 20 70 42 75 69 6c 64 65 72 2d 3e 70 4f If pBuilder->pO
25c30 72 53 65 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c rSet is not NULL
25c40 20 74 68 65 6e 20 77 65 20 6f 6e 6c 79 20 63 61 then we only ca
25c50 72 65 20 61 62 6f 75 74 20 6f 6e 6c 79 20 74 68 re about only th
25c60 65 0a 2a 2a 20 70 72 65 72 65 71 75 69 73 69 74 e.** prerequisit
25c70 65 73 20 61 6e 64 20 72 52 75 6e 20 61 6e 64 20 es and rRun and
25c80 6e 4f 75 74 20 63 6f 73 74 73 20 6f 66 20 74 68 nOut costs of th
25c90 65 20 4e 20 62 65 73 74 20 6c 6f 6f 70 73 2e 20 e N best loops.
25ca0 20 54 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 That.** informa
25cb0 74 69 6f 6e 20 69 73 20 67 61 74 68 65 72 65 64 tion is gathered
25cc0 20 69 6e 20 74 68 65 20 70 42 75 69 6c 64 65 72 in the pBuilder
25cd0 2d 3e 70 4f 72 53 65 74 20 6f 62 6a 65 63 74 2e ->pOrSet object.
25ce0 20 20 54 68 69 73 20 73 70 65 63 69 61 6c 0a 2a This special.*
25cf0 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 6f 64 * processing mod
25d00 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 66 e is used only f
25d10 6f 72 20 4f 52 20 63 6c 61 75 73 65 20 70 72 6f or OR clause pro
25d20 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 57 cessing..**.** W
25d30 68 65 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 hen accumulating
25d40 20 6d 75 6c 74 69 70 6c 65 20 6c 6f 6f 70 73 20 multiple loops
25d50 28 77 68 65 6e 20 70 42 75 69 6c 64 65 72 2d 3e (when pBuilder->
25d60 70 4f 72 53 65 74 20 69 73 20 4e 55 4c 4c 29 20 pOrSet is NULL)
25d70 77 65 0a 2a 2a 20 73 74 69 6c 6c 20 6d 69 67 68 we.** still migh
25d80 74 20 6f 76 65 72 77 72 69 74 65 20 73 69 6d 69 t overwrite simi
25d90 6c 61 72 20 6c 6f 6f 70 73 20 77 69 74 68 20 74 lar loops with t
25da0 68 65 20 6e 65 77 20 74 65 6d 70 6c 61 74 65 20 he new template
25db0 69 66 20 74 68 65 0a 2a 2a 20 74 65 6d 70 6c 61 if the.** templa
25dc0 74 65 20 69 73 20 62 65 74 74 65 72 2e 20 20 4c te is better. L
25dd0 6f 6f 70 73 20 6d 61 79 20 62 65 20 6f 76 65 72 oops may be over
25de0 77 72 69 74 74 65 6e 20 69 66 20 74 68 65 20 66 written if the f
25df0 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 63 6f 6e ollowing .** con
25e00 64 69 74 69 6f 6e 73 20 61 72 65 20 6d 65 74 3a ditions are met:
25e10 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 .**.** (1) T
25e20 68 65 79 20 68 61 76 65 20 74 68 65 20 73 61 6d hey have the sam
25e30 65 20 69 54 61 62 2e 0a 2a 2a 20 20 20 20 28 32 e iTab..** (2
25e40 29 20 20 54 68 65 79 20 68 61 76 65 20 74 68 65 ) They have the
25e50 20 73 61 6d 65 20 69 53 6f 72 74 49 64 78 2e 0a same iSortIdx..
25e60 2a 2a 20 20 20 20 28 33 29 20 20 54 68 65 20 74 ** (3) The t
25e70 65 6d 70 6c 61 74 65 20 68 61 73 20 73 61 6d 65 emplate has same
25e80 20 6f 72 20 66 65 77 65 72 20 64 65 70 65 6e 64 or fewer depend
25e90 65 6e 63 69 65 73 20 74 68 61 6e 20 74 68 65 20 encies than the
25ea0 63 75 72 72 65 6e 74 20 6c 6f 6f 70 0a 2a 2a 20 current loop.**
25eb0 20 20 20 28 34 29 20 20 54 68 65 20 74 65 6d 70 (4) The temp
25ec0 6c 61 74 65 20 68 61 73 20 74 68 65 20 73 61 6d late has the sam
25ed0 65 20 6f 72 20 6c 6f 77 65 72 20 63 6f 73 74 20 e or lower cost
25ee0 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74 than the current
25ef0 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 28 35 29 20 loop.** (5)
25f00 20 54 68 65 20 74 65 6d 70 6c 61 74 65 20 75 73 The template us
25f10 65 73 20 6d 6f 72 65 20 74 65 72 6d 73 20 6f 66 es more terms of
25f20 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20 the same index
25f30 62 75 74 20 68 61 73 20 6e 6f 20 61 64 64 69 74 but has no addit
25f40 69 6f 6e 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 ional.**
25f50 20 64 65 70 65 6e 64 65 6e 63 69 65 73 20 20 20 dependencies
25f60 20 20 20 20 20 20 20 0a 2a 2f 0a 73 74 61 74 69 .*/.stati
25f70 63 20 69 6e 74 20 77 68 65 72 65 4c 6f 6f 70 49 c int whereLoopI
25f80 6e 73 65 72 74 28 57 68 65 72 65 4c 6f 6f 70 42 nsert(WhereLoopB
25f90 75 69 6c 64 65 72 20 2a 70 42 75 69 6c 64 65 72 uilder *pBuilder
25fa0 2c 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 54 65 , WhereLoop *pTe
25fb0 6d 70 6c 61 74 65 29 7b 0a 20 20 57 68 65 72 65 mplate){. Where
25fc0 4c 6f 6f 70 20 2a 2a 70 70 50 72 65 76 2c 20 2a Loop **ppPrev, *
25fd0 70 2c 20 2a 70 4e 65 78 74 20 3d 20 30 3b 0a 20 p, *pNext = 0;.
25fe0 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e WhereInfo *pWIn
25ff0 66 6f 20 3d 20 70 42 75 69 6c 64 65 72 2d 3e 70 fo = pBuilder->p
26000 57 49 6e 66 6f 3b 0a 20 20 73 71 6c 69 74 65 33 WInfo;. sqlite3
26010 20 2a 64 62 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 *db = pWInfo->p
26020 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a Parse->db;.. /*
26030 20 49 66 20 70 42 75 69 6c 64 65 72 2d 3e 70 4f If pBuilder->pO
26040 72 53 65 74 20 69 73 20 64 65 66 69 6e 65 64 2c rSet is defined,
26050 20 74 68 65 6e 20 6f 6e 6c 79 20 6b 65 65 70 20 then only keep
26060 74 72 61 63 6b 20 6f 66 20 74 68 65 20 63 6f 73 track of the cos
26070 74 73 0a 20 20 2a 2a 20 61 6e 64 20 70 72 65 72 ts. ** and prer
26080 65 71 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 eqs.. */. if(
26090 70 42 75 69 6c 64 65 72 2d 3e 70 4f 72 53 65 74 pBuilder->pOrSet
260a0 21 3d 30 20 29 7b 0a 23 69 66 20 57 48 45 52 45 !=0 ){.#if WHERE
260b0 54 52 41 43 45 5f 45 4e 41 42 4c 45 44 0a 20 20 TRACE_ENABLED.
260c0 20 20 75 31 36 20 6e 20 3d 20 70 42 75 69 6c 64 u16 n = pBuild
260d0 65 72 2d 3e 70 4f 72 53 65 74 2d 3e 6e 3b 0a 20 er->pOrSet->n;.
260e0 20 20 20 69 6e 74 20 78 20 3d 0a 23 65 6e 64 69 int x =.#endi
260f0 66 0a 20 20 20 20 77 68 65 72 65 4f 72 49 6e 73 f. whereOrIns
26100 65 72 74 28 70 42 75 69 6c 64 65 72 2d 3e 70 4f ert(pBuilder->pO
26110 72 53 65 74 2c 20 70 54 65 6d 70 6c 61 74 65 2d rSet, pTemplate-
26120 3e 70 72 65 72 65 71 2c 20 70 54 65 6d 70 6c 61 >prereq, pTempla
26130 74 65 2d 3e 72 52 75 6e 2c 0a 20 20 20 20 20 20 te->rRun,.
26140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
26150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54 pT
26160 65 6d 70 6c 61 74 65 2d 3e 6e 4f 75 74 29 3b 0a emplate->nOut);.
26170 23 69 66 20 57 48 45 52 45 54 52 41 43 45 5f 45 #if WHERETRACE_E
26180 4e 41 42 4c 45 44 20 2f 2a 20 30 78 38 20 2a 2f NABLED /* 0x8 */
26190 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 . if( sqlite3
261a0 57 68 65 72 65 54 72 61 63 65 20 26 20 30 78 38 WhereTrace & 0x8
261b0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
261c0 33 44 65 62 75 67 50 72 69 6e 74 66 28 78 3f 22 3DebugPrintf(x?"
261d0 20 20 20 6f 72 2d 25 64 3a 20 20 22 3a 22 20 20 or-%d: ":"
261e0 20 6f 72 2d 58 3a 20 20 22 2c 20 6e 29 3b 0a 20 or-X: ", n);.
261f0 20 20 20 20 20 77 68 65 72 65 4c 6f 6f 70 50 72 whereLoopPr
26200 69 6e 74 28 70 54 65 6d 70 6c 61 74 65 2c 20 70 int(pTemplate, p
26210 42 75 69 6c 64 65 72 2d 3e 70 57 43 29 3b 0a 20 Builder->pWC);.
26220 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 }.#endif.
26230 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b return SQLITE_OK
26240 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 ;. }.. /* Sear
26250 63 68 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 ch for an existi
26260 6e 67 20 57 68 65 72 65 4c 6f 6f 70 20 74 6f 20 ng WhereLoop to
26270 6f 76 65 72 77 72 69 74 65 2c 20 6f 72 20 77 68 overwrite, or wh
26280 69 63 68 20 74 61 6b 65 73 0a 20 20 2a 2a 20 70 ich takes. ** p
26290 72 69 6f 72 69 74 79 20 6f 76 65 72 20 70 54 65 riority over pTe
262a0 6d 70 6c 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 66 mplate.. */. f
262b0 6f 72 28 70 70 50 72 65 76 3d 26 70 57 49 6e 66 or(ppPrev=&pWInf
262c0 6f 2d 3e 70 4c 6f 6f 70 73 2c 20 70 3d 2a 70 70 o->pLoops, p=*pp
262d0 50 72 65 76 3b 20 70 3b 20 70 70 50 72 65 76 3d Prev; p; ppPrev=
262e0 26 70 2d 3e 70 4e 65 78 74 4c 6f 6f 70 2c 20 70 &p->pNextLoop, p
262f0 3d 2a 70 70 50 72 65 76 29 7b 0a 20 20 20 20 69 =*ppPrev){. i
26300 66 28 20 70 2d 3e 69 54 61 62 21 3d 70 54 65 6d f( p->iTab!=pTem
26310 70 6c 61 74 65 2d 3e 69 54 61 62 20 7c 7c 20 70 plate->iTab || p
26320 2d 3e 69 53 6f 72 74 49 64 78 21 3d 70 54 65 6d ->iSortIdx!=pTem
26330 70 6c 61 74 65 2d 3e 69 53 6f 72 74 49 64 78 20 plate->iSortIdx
26340 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65 ){. /* If e
26350 69 74 68 65 72 20 74 68 65 20 69 54 61 62 20 6f ither the iTab o
26360 72 20 69 53 6f 72 74 49 64 78 20 76 61 6c 75 65 r iSortIdx value
26370 73 20 66 6f 72 20 74 77 6f 20 57 68 65 72 65 4c s for two WhereL
26380 6f 6f 70 20 61 72 65 20 64 69 66 66 65 72 65 6e oop are differen
26390 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 t. ** then
263a0 74 68 6f 73 65 20 57 68 65 72 65 4c 6f 6f 70 73 those WhereLoops
263b0 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 need to be cons
263c0 69 64 65 72 65 64 20 73 65 70 61 72 61 74 65 6c idered separatel
263d0 79 2e 20 20 4e 65 69 74 68 65 72 20 69 73 0a 20 y. Neither is.
263e0 20 20 20 20 20 2a 2a 20 61 20 63 61 6e 64 69 64 ** a candid
263f0 61 74 65 20 74 6f 20 72 65 70 6c 61 63 65 20 74 ate to replace t
26400 68 65 20 6f 74 68 65 72 2e 20 2a 2f 0a 20 20 20 he other. */.
26410 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 continue;.
26420 20 7d 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 }. /* In the
26430 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 current impleme
26440 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20 72 53 65 ntation, the rSe
26450 74 75 70 20 76 61 6c 75 65 20 69 73 20 65 69 74 tup value is eit
26460 68 65 72 20 7a 65 72 6f 0a 20 20 20 20 2a 2a 20 her zero. **
26470 6f 72 20 74 68 65 20 63 6f 73 74 20 6f 66 20 62 or the cost of b
26480 75 69 6c 64 69 6e 67 20 61 6e 20 61 75 74 6f 6d uilding an autom
26490 61 74 69 63 20 69 6e 64 65 78 20 28 4e 6c 6f 67 atic index (Nlog
264a0 4e 29 20 61 6e 64 20 74 68 65 20 4e 6c 6f 67 4e N) and the NlogN
264b0 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 73 . ** is the s
264c0 61 6d 65 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 ame for compatib
264d0 6c 65 20 57 68 65 72 65 4c 6f 6f 70 73 2e 20 2a le WhereLoops. *
264e0 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d /. assert( p-
264f0 3e 72 53 65 74 75 70 3d 3d 30 20 7c 7c 20 70 54 >rSetup==0 || pT
26500 65 6d 70 6c 61 74 65 2d 3e 72 53 65 74 75 70 3d emplate->rSetup=
26510 3d 30 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 =0 .
26520 20 20 20 20 20 7c 7c 20 70 2d 3e 72 53 65 74 75 || p->rSetu
26530 70 3d 3d 70 54 65 6d 70 6c 61 74 65 2d 3e 72 53 p==pTemplate->rS
26540 65 74 75 70 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 etup );.. /*
26550 77 68 65 72 65 4c 6f 6f 70 41 64 64 42 74 72 65 whereLoopAddBtre
26560 65 28 29 20 61 6c 77 61 79 73 20 67 65 6e 65 72 e() always gener
26570 61 74 65 73 20 61 6e 64 20 69 6e 73 65 72 74 73 ates and inserts
26580 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 69 the automatic i
26590 6e 64 65 78 0a 20 20 20 20 2a 2a 20 63 61 73 65 ndex. ** case
265a0 20 66 69 72 73 74 2e 20 20 48 65 6e 63 65 20 63 first. Hence c
265b0 6f 6d 70 61 74 69 62 6c 65 20 63 61 6e 64 69 64 ompatible candid
265c0 61 74 65 20 57 68 65 72 65 4c 6f 6f 70 73 20 6e ate WhereLoops n
265d0 65 76 65 72 20 68 61 76 65 20 61 20 6c 61 72 67 ever have a larg
265e0 65 72 0a 20 20 20 20 2a 2a 20 72 53 65 74 75 70 er. ** rSetup
265f0 2e 20 43 61 6c 6c 20 74 68 69 73 20 53 45 54 55 . Call this SETU
26600 50 2d 49 4e 56 41 52 49 41 4e 54 20 2a 2f 0a 20 P-INVARIANT */.
26610 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 53 assert( p->rS
26620 65 74 75 70 3e 3d 70 54 65 6d 70 6c 61 74 65 2d etup>=pTemplate-
26630 3e 72 53 65 74 75 70 20 29 3b 0a 0a 20 20 20 20 >rSetup );..
26640 69 66 28 20 28 70 2d 3e 70 72 65 72 65 71 20 26 if( (p->prereq &
26650 20 70 54 65 6d 70 6c 61 74 65 2d 3e 70 72 65 72 pTemplate->prer
26660 65 71 29 3d 3d 70 2d 3e 70 72 65 72 65 71 0a 20 eq)==p->prereq.
26670 20 20 20 20 26 26 20 70 2d 3e 72 53 65 74 75 70 && p->rSetup
26680 3c 3d 70 54 65 6d 70 6c 61 74 65 2d 3e 72 53 65 <=pTemplate->rSe
26690 74 75 70 0a 20 20 20 20 20 26 26 20 70 2d 3e 72 tup. && p->r
266a0 52 75 6e 3c 3d 70 54 65 6d 70 6c 61 74 65 2d 3e Run<=pTemplate->
266b0 72 52 75 6e 0a 20 20 20 20 20 26 26 20 70 2d 3e rRun. && p->
266c0 6e 4f 75 74 3c 3d 70 54 65 6d 70 6c 61 74 65 2d nOut<=pTemplate-
266d0 3e 6e 4f 75 74 0a 20 20 20 20 29 7b 0a 20 20 20 >nOut. ){.
266e0 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 /* This branc
266f0 68 20 74 61 6b 65 6e 20 77 68 65 6e 20 70 20 69 h taken when p i
26700 73 20 65 71 75 61 6c 20 6f 72 20 62 65 74 74 65 s equal or bette
26710 72 20 74 68 61 6e 20 70 54 65 6d 70 6c 61 74 65 r than pTemplate
26720 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 61 6c in . ** al
26730 6c 20 6f 66 20 28 31 29 20 64 65 70 65 6e 64 65 l of (1) depende
26740 6e 63 69 65 73 20 28 32 29 20 73 65 74 75 70 2d ncies (2) setup-
26750 63 6f 73 74 2c 20 28 33 29 20 72 75 6e 2d 63 6f cost, (3) run-co
26760 73 74 2c 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a st, and. **
26770 20 28 34 29 20 6e 75 6d 62 65 72 20 6f 66 20 6f (4) number of o
26780 75 74 70 75 74 20 72 6f 77 73 2e 20 2a 2f 0a 20 utput rows. */.
26790 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
267a0 72 53 65 74 75 70 3d 3d 70 54 65 6d 70 6c 61 74 rSetup==pTemplat
267b0 65 2d 3e 72 53 65 74 75 70 20 29 3b 0a 20 20 20 e->rSetup );.
267c0 20 20 20 69 66 28 20 70 2d 3e 70 72 65 72 65 71 if( p->prereq
267d0 3d 3d 70 54 65 6d 70 6c 61 74 65 2d 3e 70 72 65 ==pTemplate->pre
267e0 72 65 71 0a 20 20 20 20 20 20 20 26 26 20 70 2d req. && p-
267f0 3e 6e 4c 54 65 72 6d 3c 70 54 65 6d 70 6c 61 74 >nLTerm<pTemplat
26800 65 2d 3e 6e 4c 54 65 72 6d 0a 20 20 20 20 20 20 e->nLTerm.
26810 20 26 26 20 28 70 2d 3e 77 73 46 6c 61 67 73 20 && (p->wsFlags
26820 26 20 70 54 65 6d 70 6c 61 74 65 2d 3e 77 73 46 & pTemplate->wsF
26830 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 lags & WHERE_IND
26840 45 58 45 44 29 21 3d 30 0a 20 20 20 20 20 20 20 EXED)!=0.
26850 26 26 20 28 70 2d 3e 75 2e 62 74 72 65 65 2e 70 && (p->u.btree.p
26860 49 6e 64 65 78 3d 3d 70 54 65 6d 70 6c 61 74 65 Index==pTemplate
26870 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 65 78 ->u.btree.pIndex
26880 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 54 . || pT
26890 65 6d 70 6c 61 74 65 2d 3e 72 52 75 6e 2b 70 2d emplate->rRun+p-
268a0 3e 6e 4c 54 65 72 6d 3c 3d 70 2d 3e 72 52 75 6e >nLTerm<=p->rRun
268b0 2b 70 54 65 6d 70 6c 61 74 65 2d 3e 6e 4c 54 65 +pTemplate->nLTe
268c0 72 6d 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 rm). ){.
268d0 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 /* Overwrit
268e0 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 57 68 e an existing Wh
268f0 65 72 65 4c 6f 6f 70 20 77 69 74 68 20 61 6e 20 ereLoop with an
26900 73 69 6d 69 6c 61 72 20 6f 6e 65 20 74 68 61 74 similar one that
26910 20 75 73 65 73 0a 20 20 20 20 20 20 20 20 2a 2a uses. **
26920 20 6d 6f 72 65 20 74 65 72 6d 73 20 6f 66 20 74 more terms of t
26930 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 he index */.
26940 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 pNext = p->p
26950 4e 65 78 74 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 NextLoop;.
26960 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
26970 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a else{. /*
26980 20 70 54 65 6d 70 6c 61 74 65 20 69 73 20 6e 6f pTemplate is no
26990 74 20 68 65 6c 70 66 75 6c 2e 0a 20 20 20 20 20 t helpful..
269a0 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 77 69 74 ** Return wit
269b0 68 6f 75 74 20 63 68 61 6e 67 69 6e 67 20 6f 72 hout changing or
269c0 20 61 64 64 69 6e 67 20 61 6e 79 74 68 69 6e 67 adding anything
269d0 20 2a 2f 0a 20 20 20 20 20 20 20 20 67 6f 74 6f */. goto
269e0 20 77 68 65 72 65 4c 6f 6f 70 49 6e 73 65 72 74 whereLoopInsert
269f0 5f 6e 6f 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 _noop;. }.
26a00 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 70 2d }. if( (p-
26a10 3e 70 72 65 72 65 71 20 26 20 70 54 65 6d 70 6c >prereq & pTempl
26a20 61 74 65 2d 3e 70 72 65 72 65 71 29 3d 3d 70 54 ate->prereq)==pT
26a30 65 6d 70 6c 61 74 65 2d 3e 70 72 65 72 65 71 0a emplate->prereq.
26a40 20 20 20 20 20 26 26 20 70 2d 3e 72 52 75 6e 3e && p->rRun>
26a50 3d 70 54 65 6d 70 6c 61 74 65 2d 3e 72 52 75 6e =pTemplate->rRun
26a60 0a 20 20 20 20 20 26 26 20 70 2d 3e 6e 4f 75 74 . && p->nOut
26a70 3e 3d 70 54 65 6d 70 6c 61 74 65 2d 3e 6e 4f 75 >=pTemplate->nOu
26a80 74 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f t. ){. /
26a90 2a 20 4f 76 65 72 77 72 69 74 65 20 61 6e 20 65 * Overwrite an e
26aa0 78 69 73 74 69 6e 67 20 57 68 65 72 65 4c 6f 6f xisting WhereLoo
26ab0 70 20 77 69 74 68 20 61 20 62 65 74 74 65 72 20 p with a better
26ac0 6f 6e 65 3a 20 6f 6e 65 20 74 68 61 74 20 69 73 one: one that is
26ad0 0a 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 . ** better
26ae0 20 61 74 20 6f 6e 65 20 6f 66 20 28 31 29 20 64 at one of (1) d
26af0 65 70 65 6e 64 65 6e 63 69 65 73 2c 20 28 32 29 ependencies, (2)
26b00 20 73 65 74 75 70 2d 63 6f 73 74 2c 20 28 33 29 setup-cost, (3)
26b10 20 72 75 6e 2d 63 6f 73 74 0a 20 20 20 20 20 20 run-cost.
26b20 2a 2a 20 6f 72 20 28 34 29 20 6e 75 6d 62 65 72 ** or (4) number
26b30 20 6f 66 20 6f 75 74 70 75 74 20 72 6f 77 73 2c of output rows,
26b40 20 61 6e 64 20 69 73 20 6e 6f 20 77 6f 72 73 65 and is no worse
26b50 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 in any of those
26b60 0a 20 20 20 20 20 20 2a 2a 20 63 61 74 65 67 6f . ** catego
26b70 72 69 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 61 ries. */. a
26b80 73 73 65 72 74 28 20 70 2d 3e 72 53 65 74 75 70 ssert( p->rSetup
26b90 3e 3d 70 54 65 6d 70 6c 61 74 65 2d 3e 72 53 65 >=pTemplate->rSe
26ba0 74 75 70 20 29 3b 20 2f 2a 20 53 45 54 55 50 2d tup ); /* SETUP-
26bb0 49 4e 56 41 52 49 41 4e 54 20 61 62 6f 76 65 20 INVARIANT above
26bc0 2a 2f 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d */. pNext =
26bd0 20 70 2d 3e 70 4e 65 78 74 4c 6f 6f 70 3b 0a 20 p->pNextLoop;.
26be0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
26bf0 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 }. }.. /* If w
26c00 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69 e reach this poi
26c10 6e 74 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 nt it means that
26c20 20 65 69 74 68 65 72 20 70 5b 5d 20 73 68 6f 75 either p[] shou
26c30 6c 64 20 62 65 20 6f 76 65 72 77 72 69 74 74 65 ld be overwritte
26c40 6e 0a 20 20 2a 2a 20 77 69 74 68 20 70 54 65 6d n. ** with pTem
26c50 70 6c 61 74 65 5b 5d 20 69 66 20 70 5b 5d 20 65 plate[] if p[] e
26c60 78 69 73 74 73 2c 20 6f 72 20 69 66 20 70 3d 3d xists, or if p==
26c70 4e 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 6f 63 61 NULL then alloca
26c80 74 65 20 61 20 6e 65 77 0a 20 20 2a 2a 20 57 68 te a new. ** Wh
26c90 65 72 65 4c 6f 6f 70 20 61 6e 64 20 69 6e 73 65 ereLoop and inse
26ca0 72 74 20 69 74 2e 0a 20 20 2a 2f 0a 23 69 66 20 rt it.. */.#if
26cb0 57 48 45 52 45 54 52 41 43 45 5f 45 4e 41 42 4c WHERETRACE_ENABL
26cc0 45 44 20 2f 2a 20 30 78 38 20 2a 2f 0a 20 20 69 ED /* 0x8 */. i
26cd0 66 28 20 73 71 6c 69 74 65 33 57 68 65 72 65 54 f( sqlite3WhereT
26ce0 72 61 63 65 20 26 20 30 78 38 20 29 7b 0a 20 20 race & 0x8 ){.
26cf0 20 20 69 66 28 20 70 21 3d 30 20 29 7b 0a 20 20 if( p!=0 ){.
26d00 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 sqlite3Debug
26d10 50 72 69 6e 74 66 28 22 69 6e 73 2d 64 65 6c 3a Printf("ins-del:
26d20 20 20 22 29 3b 0a 20 20 20 20 20 20 77 68 65 72 ");. wher
26d30 65 4c 6f 6f 70 50 72 69 6e 74 28 70 2c 20 70 42 eLoopPrint(p, pB
26d40 75 69 6c 64 65 72 2d 3e 70 57 43 29 3b 0a 20 20 uilder->pWC);.
26d50 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 }. sqlite3D
26d60 65 62 75 67 50 72 69 6e 74 66 28 22 69 6e 73 2d ebugPrintf("ins-
26d70 6e 65 77 3a 20 20 22 29 3b 0a 20 20 20 20 77 68 new: ");. wh
26d80 65 72 65 4c 6f 6f 70 50 72 69 6e 74 28 70 54 65 ereLoopPrint(pTe
26d90 6d 70 6c 61 74 65 2c 20 70 42 75 69 6c 64 65 72 mplate, pBuilder
26da0 2d 3e 70 57 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 ->pWC);. }.#end
26db0 69 66 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b if. if( p==0 ){
26dc0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 . p = sqlite3
26dd0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 DbMallocRaw(db,
26de0 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 6f 6f 70 sizeof(WhereLoop
26df0 29 29 3b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 ));. if( p==0
26e00 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 ) return SQLITE
26e10 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 77 68 65 72 _NOMEM;. wher
26e20 65 4c 6f 6f 70 49 6e 69 74 28 70 29 3b 0a 20 20 eLoopInit(p);.
26e30 7d 0a 20 20 77 68 65 72 65 4c 6f 6f 70 58 66 65 }. whereLoopXfe
26e40 72 28 64 62 2c 20 70 2c 20 70 54 65 6d 70 6c 61 r(db, p, pTempla
26e50 74 65 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 74 4c te);. p->pNextL
26e60 6f 6f 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 2a oop = pNext;. *
26e70 70 70 50 72 65 76 20 3d 20 70 3b 0a 20 20 69 66 ppPrev = p;. if
26e80 28 20 28 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 ( (p->wsFlags &
26e90 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 WHERE_VIRTUALTAB
26ea0 4c 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 49 6e LE)==0 ){. In
26eb0 64 65 78 20 2a 70 49 6e 64 65 78 20 3d 20 70 2d dex *pIndex = p-
26ec0 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 65 78 3b >u.btree.pIndex;
26ed0 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 20 . if( pIndex
26ee0 26 26 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 3d && pIndex->tnum=
26ef0 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 75 =0 ){. p->u
26f00 2e 62 74 72 65 65 2e 70 49 6e 64 65 78 20 3d 20 .btree.pIndex =
26f10 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 0;. }. }. r
26f20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
26f30 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 .. /* Jump here
26f40 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 69 if the insert i
26f50 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 77 68 65 s a no-op */.whe
26f60 72 65 4c 6f 6f 70 49 6e 73 65 72 74 5f 6e 6f 6f reLoopInsert_noo
26f70 70 3a 0a 23 69 66 20 57 48 45 52 45 54 52 41 43 p:.#if WHERETRAC
26f80 45 5f 45 4e 41 42 4c 45 44 20 2f 2a 20 30 78 38 E_ENABLED /* 0x8
26f90 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
26fa0 33 57 68 65 72 65 54 72 61 63 65 20 26 20 30 78 3WhereTrace & 0x
26fb0 38 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 8 ){. sqlite3
26fc0 44 65 62 75 67 50 72 69 6e 74 66 28 22 69 6e 73 DebugPrintf("ins
26fd0 2d 6e 6f 6f 70 3a 20 22 29 3b 0a 20 20 20 20 77 -noop: ");. w
26fe0 68 65 72 65 4c 6f 6f 70 50 72 69 6e 74 28 70 54 hereLoopPrint(pT
26ff0 65 6d 70 6c 61 74 65 2c 20 70 42 75 69 6c 64 65 emplate, pBuilde
27000 72 2d 3e 70 57 43 29 3b 0a 20 20 7d 0a 23 65 6e r->pWC);. }.#en
27010 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c dif. return SQL
27020 49 54 45 5f 4f 4b 3b 20 20 0a 7d 0a 0a 2f 2a 0a ITE_OK; .}../*.
27030 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20 57 68 ** Adjust the Wh
27040 65 72 65 4c 6f 6f 70 2e 6e 4f 75 74 20 76 61 6c ereLoop.nOut val
27050 75 65 20 64 6f 77 6e 77 61 72 64 20 74 6f 20 61 ue downward to a
27060 63 63 6f 75 6e 74 20 66 6f 72 20 74 65 72 6d 73 ccount for terms
27070 20 6f 66 20 74 68 65 0a 2a 2a 20 57 48 45 52 45 of the.** WHERE
27080 20 63 6c 61 75 73 65 20 74 68 61 74 20 72 65 66 clause that ref
27090 65 72 65 6e 63 65 20 74 68 65 20 6c 6f 6f 70 20 erence the loop
270a0 62 75 74 20 77 68 69 63 68 20 61 72 65 20 6e 6f but which are no
270b0 74 20 75 73 65 64 20 62 79 20 61 6e 0a 2a 2a 20 t used by an.**
270c0 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 index..**.** In
270d0 74 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c the current impl
270e0 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20 ementation, the
270f0 66 69 72 73 74 20 65 78 74 72 61 20 57 48 45 52 first extra WHER
27100 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 72 65 E clause term re
27110 64 75 63 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d duces.** the num
27120 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 72 6f ber of output ro
27130 77 73 20 62 79 20 61 20 66 61 63 74 6f 72 20 6f ws by a factor o
27140 66 20 31 30 20 61 6e 64 20 65 61 63 68 20 61 64 f 10 and each ad
27150 64 69 74 69 6f 6e 61 6c 20 74 65 72 6d 0a 2a 2a ditional term.**
27160 20 72 65 64 75 63 65 73 20 74 68 65 20 6e 75 6d reduces the num
27170 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 72 6f ber of output ro
27180 77 73 20 62 79 20 73 71 72 74 28 32 29 2e 0a 2a ws by sqrt(2)..*
27190 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 /.static void wh
271a0 65 72 65 4c 6f 6f 70 4f 75 74 70 75 74 41 64 6a ereLoopOutputAdj
271b0 75 73 74 28 57 68 65 72 65 43 6c 61 75 73 65 20 ust(WhereClause
271c0 2a 70 57 43 2c 20 57 68 65 72 65 4c 6f 6f 70 20 *pWC, WhereLoop
271d0 2a 70 4c 6f 6f 70 29 7b 0a 20 20 57 68 65 72 65 *pLoop){. Where
271e0 54 65 72 6d 20 2a 70 54 65 72 6d 2c 20 2a 70 58 Term *pTerm, *pX
271f0 3b 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 41 ;. Bitmask notA
27200 6c 6c 6f 77 65 64 20 3d 20 7e 28 70 4c 6f 6f 70 llowed = ~(pLoop
27210 2d 3e 70 72 65 72 65 71 7c 70 4c 6f 6f 70 2d 3e ->prereq|pLoop->
27220 6d 61 73 6b 53 65 6c 66 29 3b 0a 20 20 69 6e 74 maskSelf);. int
27230 20 69 2c 20 6a 3b 0a 0a 20 20 69 66 28 20 21 4f i, j;.. if( !O
27240 70 74 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c ptimizationEnabl
27250 65 64 28 70 57 43 2d 3e 70 57 49 6e 66 6f 2d 3e ed(pWC->pWInfo->
27260 70 50 61 72 73 65 2d 3e 64 62 2c 20 53 51 4c 49 pParse->db, SQLI
27270 54 45 5f 41 64 6a 75 73 74 4f 75 74 45 73 74 29 TE_AdjustOutEst)
27280 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a ){. return;.
27290 20 20 7d 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d }. for(i=pWC-
272a0 3e 6e 54 65 72 6d 2c 20 70 54 65 72 6d 3d 70 57 >nTerm, pTerm=pW
272b0 43 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 C->a; i>0; i--,
272c0 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 pTerm++){. if
272d0 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 ( (pTerm->wtFlag
272e0 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c s & TERM_VIRTUAL
272f0 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 )!=0 ) break;.
27300 20 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 if( (pTerm->pr
27310 65 72 65 71 41 6c 6c 20 26 20 70 4c 6f 6f 70 2d ereqAll & pLoop-
27320 3e 6d 61 73 6b 53 65 6c 66 29 3d 3d 30 20 29 20 >maskSelf)==0 )
27330 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 continue;. if
27340 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 ( (pTerm->prereq
27350 41 6c 6c 20 26 20 6e 6f 74 41 6c 6c 6f 77 65 64 All & notAllowed
27360 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b )!=0 ) continue;
27370 0a 20 20 20 20 66 6f 72 28 6a 3d 70 4c 6f 6f 70 . for(j=pLoop
27380 2d 3e 6e 4c 54 65 72 6d 2d 31 3b 20 6a 3e 3d 30 ->nLTerm-1; j>=0
27390 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 70 58 ; j--){. pX
273a0 20 3d 20 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d = pLoop->aLTerm
273b0 5b 6a 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 70 [j];. if( p
273c0 58 3d 3d 70 54 65 72 6d 20 29 20 62 72 65 61 6b X==pTerm ) break
273d0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 58 2d 3e ;. if( pX->
273e0 69 50 61 72 65 6e 74 3e 3d 30 20 26 26 20 28 26 iParent>=0 && (&
273f0 70 57 43 2d 3e 61 5b 70 58 2d 3e 69 50 61 72 65 pWC->a[pX->iPare
27400 6e 74 5d 29 3d 3d 70 54 65 72 6d 20 29 20 62 72 nt])==pTerm ) br
27410 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 eak;. }. i
27420 66 28 20 6a 3c 30 20 29 20 70 4c 6f 6f 70 2d 3e f( j<0 ) pLoop->
27430 6e 4f 75 74 20 2b 3d 20 70 54 65 72 6d 2d 3e 74 nOut += pTerm->t
27440 72 75 74 68 50 72 6f 62 3b 0a 20 20 7d 0a 7d 0a ruthProb;. }.}.
27450 0a 2f 2a 0a 2a 2a 20 57 65 20 68 61 76 65 20 73 ./*.** We have s
27460 6f 20 66 61 72 20 6d 61 74 63 68 65 64 20 70 42 o far matched pB
27470 75 69 6c 64 65 72 2d 3e 70 4e 65 77 2d 3e 75 2e uilder->pNew->u.
27480 62 74 72 65 65 2e 6e 45 71 20 74 65 72 6d 73 20 btree.nEq terms
27490 6f 66 20 74 68 65 20 69 6e 64 65 78 20 70 49 6e of the index pIn
274a0 64 65 78 2e 0a 2a 2a 20 54 72 79 20 74 6f 20 6d dex..** Try to m
274b0 61 74 63 68 20 6f 6e 65 20 6d 6f 72 65 2e 0a 2a atch one more..*
274c0 2a 0a 2a 2a 20 49 66 20 70 50 72 6f 62 65 2d 3e *.** If pProbe->
274d0 74 6e 75 6d 3d 3d 30 2c 20 74 68 61 74 20 6d 65 tnum==0, that me
274e0 61 6e 73 20 70 49 6e 64 65 78 20 69 73 20 61 20 ans pIndex is a
274f0 66 61 6b 65 20 69 6e 64 65 78 20 75 73 65 64 20 fake index used
27500 66 6f 72 20 74 68 65 0a 2a 2a 20 49 4e 54 45 47 for the.** INTEG
27510 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2e 0a ER PRIMARY KEY..
27520 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 */.static int wh
27530 65 72 65 4c 6f 6f 70 41 64 64 42 74 72 65 65 49 ereLoopAddBtreeI
27540 6e 64 65 78 28 0a 20 20 57 68 65 72 65 4c 6f 6f ndex(. WhereLoo
27550 70 42 75 69 6c 64 65 72 20 2a 70 42 75 69 6c 64 pBuilder *pBuild
27560 65 72 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 57 er, /* The W
27570 68 65 72 65 4c 6f 6f 70 20 66 61 63 74 6f 72 79 hereLoop factory
27580 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 */. struct Src
27590 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c List_item *pSrc,
275a0 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c /* FROM cl
275b0 61 75 73 65 20 74 65 72 6d 20 62 65 69 6e 67 20 ause term being
275c0 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 49 6e analyzed */. In
275d0 64 65 78 20 2a 70 50 72 6f 62 65 2c 20 20 20 20 dex *pProbe,
275e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
275f0 20 41 6e 20 69 6e 64 65 78 20 6f 6e 20 70 53 72 An index on pSr
27600 63 20 2a 2f 0a 20 20 4c 6f 67 45 73 74 20 6e 49 c */. LogEst nI
27610 6e 4d 75 6c 20 20 20 20 20 20 20 20 20 20 20 20 nMul
27620 20 20 20 20 20 20 20 2f 2a 20 6c 6f 67 28 4e 75 /* log(Nu
27630 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 69 6f mber of iteratio
27640 6e 73 20 64 75 65 20 74 6f 20 49 4e 29 20 2a 2f ns due to IN) */
27650 0a 29 7b 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 .){. WhereInfo
27660 2a 70 57 49 6e 66 6f 20 3d 20 70 42 75 69 6c 64 *pWInfo = pBuild
27670 65 72 2d 3e 70 57 49 6e 66 6f 3b 20 20 2f 2a 20 er->pWInfo; /*
27680 57 48 45 52 45 20 61 6e 61 6c 79 73 65 20 63 6f WHERE analyse co
27690 6e 74 65 78 74 20 2a 2f 0a 20 20 50 61 72 73 65 ntext */. Parse
276a0 20 2a 70 50 61 72 73 65 20 3d 20 70 57 49 6e 66 *pParse = pWInf
276b0 6f 2d 3e 70 50 61 72 73 65 3b 20 20 20 20 20 20 o->pParse;
276c0 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e /* Parsing con
276d0 74 65 78 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 text */. sqlite
276e0 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 3 *db = pParse->
276f0 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 db; /* Dat
27700 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e abase connection
27710 20 6d 61 6c 6c 6f 63 20 63 6f 6e 74 65 78 74 20 malloc context
27720 2a 2f 0a 20 20 57 68 65 72 65 4c 6f 6f 70 20 2a */. WhereLoop *
27730 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 pNew;
27740 20 20 20 20 20 2f 2a 20 54 65 6d 70 6c 61 74 65 /* Template
27750 20 57 68 65 72 65 4c 6f 6f 70 20 75 6e 64 65 72 WhereLoop under
27760 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f construction */
27770 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 . WhereTerm *pT
27780 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 erm;
27790 20 20 20 2f 2a 20 41 20 57 68 65 72 65 54 65 72 /* A WhereTer
277a0 6d 20 75 6e 64 65 72 20 63 6f 6e 73 69 64 65 72 m under consider
277b0 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f ation */. int o
277c0 70 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 pMask;
277d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 /* Va
277e0 6c 69 64 20 6f 70 65 72 61 74 6f 72 73 20 66 6f lid operators fo
277f0 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f r constraints */
27800 0a 20 20 57 68 65 72 65 53 63 61 6e 20 73 63 61 . WhereScan sca
27810 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 n;
27820 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 /* Iterator f
27830 6f 72 20 57 48 45 52 45 20 74 65 72 6d 73 20 2a or WHERE terms *
27840 2f 0a 20 20 42 69 74 6d 61 73 6b 20 73 61 76 65 /. Bitmask save
27850 64 5f 70 72 65 72 65 71 3b 20 20 20 20 20 20 20 d_prereq;
27860 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 /* Original
27870 76 61 6c 75 65 20 6f 66 20 70 4e 65 77 2d 3e 70 value of pNew->p
27880 72 65 72 65 71 20 2a 2f 0a 20 20 75 31 36 20 73 rereq */. u16 s
27890 61 76 65 64 5f 6e 4c 54 65 72 6d 3b 20 20 20 20 aved_nLTerm;
278a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 /* Or
278b0 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 iginal value of
278c0 70 4e 65 77 2d 3e 6e 4c 54 65 72 6d 20 2a 2f 0a pNew->nLTerm */.
278d0 20 20 69 6e 74 20 73 61 76 65 64 5f 6e 45 71 3b int saved_nEq;
278e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
278f0 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 76 61 /* Original va
27900 6c 75 65 20 6f 66 20 70 4e 65 77 2d 3e 75 2e 62 lue of pNew->u.b
27910 74 72 65 65 2e 6e 45 71 20 2a 2f 0a 20 20 75 33 tree.nEq */. u3
27920 32 20 73 61 76 65 64 5f 77 73 46 6c 61 67 73 3b 2 saved_wsFlags;
27930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
27940 20 4f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 Original value
27950 6f 66 20 70 4e 65 77 2d 3e 77 73 46 6c 61 67 73 of pNew->wsFlags
27960 20 2a 2f 0a 20 20 4c 6f 67 45 73 74 20 73 61 76 */. LogEst sav
27970 65 64 5f 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 ed_nOut;
27980 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 /* Origina
27990 6c 20 76 61 6c 75 65 20 6f 66 20 70 4e 65 77 2d l value of pNew-
279a0 3e 6e 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69 >nOut */. int i
279b0 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 Col;
279c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
279d0 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d dex of the colum
279e0 6e 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a n in the table *
279f0 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c /. int rc = SQL
27a00 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 ITE_OK;
27a10 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f /* Return co
27a20 64 65 20 2a 2f 0a 20 20 4c 6f 67 45 73 74 20 6e de */. LogEst n
27a30 52 6f 77 45 73 74 3b 20 20 20 20 20 20 20 20 20 RowEst;
27a40 20 20 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d /* Estim
27a50 61 74 65 64 20 69 6e 64 65 78 20 73 65 6c 65 63 ated index selec
27a60 74 69 76 69 74 79 20 2a 2f 0a 20 20 4c 6f 67 45 tivity */. LogE
27a70 73 74 20 72 4c 6f 67 53 69 7a 65 3b 20 20 20 20 st rLogSize;
27a80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c /* L
27a90 6f 67 61 72 69 74 68 6d 20 6f 66 20 74 61 62 6c ogarithm of tabl
27aa0 65 20 73 69 7a 65 20 2a 2f 0a 20 20 57 68 65 72 e size */. Wher
27ab0 65 54 65 72 6d 20 2a 70 54 6f 70 20 3d 20 30 2c eTerm *pTop = 0,
27ac0 20 2a 70 42 74 6d 20 3d 20 30 3b 20 2f 2a 20 54 *pBtm = 0; /* T
27ad0 6f 70 20 61 6e 64 20 62 6f 74 74 6f 6d 20 72 61 op and bottom ra
27ae0 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 nge constraints
27af0 2a 2f 0a 0a 20 20 70 4e 65 77 20 3d 20 70 42 75 */.. pNew = pBu
27b00 69 6c 64 65 72 2d 3e 70 4e 65 77 3b 0a 20 20 69 ilder->pNew;. i
27b10 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 f( db->mallocFai
27b20 6c 65 64 20 29 20 72 65 74 75 72 6e 20 53 51 4c led ) return SQL
27b30 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 61 73 ITE_NOMEM;.. as
27b40 73 65 72 74 28 20 28 70 4e 65 77 2d 3e 77 73 46 sert( (pNew->wsF
27b50 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49 52 lags & WHERE_VIR
27b60 54 55 41 4c 54 41 42 4c 45 29 3d 3d 30 20 29 3b TUALTABLE)==0 );
27b70 0a 20 20 61 73 73 65 72 74 28 20 28 70 4e 65 77 . assert( (pNew
27b80 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 ->wsFlags & WHER
27b90 45 5f 54 4f 50 5f 4c 49 4d 49 54 29 3d 3d 30 20 E_TOP_LIMIT)==0
27ba0 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 77 );. if( pNew->w
27bb0 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42 sFlags & WHERE_B
27bc0 54 4d 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 TM_LIMIT ){.
27bd0 6f 70 4d 61 73 6b 20 3d 20 57 4f 5f 4c 54 7c 57 opMask = WO_LT|W
27be0 4f 5f 4c 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 O_LE;. }else if
27bf0 28 20 70 50 72 6f 62 65 2d 3e 74 6e 75 6d 3c 3d ( pProbe->tnum<=
27c00 30 20 7c 7c 20 28 70 53 72 63 2d 3e 6a 6f 69 6e 0 || (pSrc->join
27c10 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 type & JT_LEFT)!
27c20 3d 30 20 29 7b 0a 20 20 20 20 6f 70 4d 61 73 6b =0 ){. opMask
27c30 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 = WO_EQ|WO_IN|W
27c40 4f 5f 47 54 7c 57 4f 5f 47 45 7c 57 4f 5f 4c 54 O_GT|WO_GE|WO_LT
27c50 7c 57 4f 5f 4c 45 3b 0a 20 20 7d 65 6c 73 65 7b |WO_LE;. }else{
27c60 0a 20 20 20 20 6f 70 4d 61 73 6b 20 3d 20 57 4f . opMask = WO
27c70 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e _EQ|WO_IN|WO_ISN
27c80 55 4c 4c 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 7c ULL|WO_GT|WO_GE|
27c90 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 3b 0a 20 20 7d WO_LT|WO_LE;. }
27ca0 0a 20 20 69 66 28 20 70 50 72 6f 62 65 2d 3e 62 . if( pProbe->b
27cb0 55 6e 6f 72 64 65 72 65 64 20 29 20 6f 70 4d 61 Unordered ) opMa
27cc0 73 6b 20 26 3d 20 7e 28 57 4f 5f 47 54 7c 57 4f sk &= ~(WO_GT|WO
27cd0 5f 47 45 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 29 _GE|WO_LT|WO_LE)
27ce0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 ;.. assert( pNe
27cf0 77 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 3c 3d w->u.btree.nEq<=
27d00 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 pProbe->nColumn
27d10 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 75 );. if( pNew->u
27d20 2e 62 74 72 65 65 2e 6e 45 71 20 3c 20 70 50 72 .btree.nEq < pPr
27d30 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a obe->nColumn ){.
27d40 20 20 20 20 69 43 6f 6c 20 3d 20 70 50 72 6f 62 iCol = pProb
27d50 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 70 4e 65 77 e->aiColumn[pNew
27d60 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 5d 3b 0a ->u.btree.nEq];.
27d70 20 20 20 20 6e 52 6f 77 45 73 74 20 3d 20 73 71 nRowEst = sq
27d80 6c 69 74 65 33 4c 6f 67 45 73 74 28 70 50 72 6f lite3LogEst(pPro
27d90 62 65 2d 3e 61 69 52 6f 77 45 73 74 5b 70 4e 65 be->aiRowEst[pNe
27da0 77 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 2b 31 w->u.btree.nEq+1
27db0 5d 29 3b 0a 20 20 20 20 69 66 28 20 6e 52 6f 77 ]);. if( nRow
27dc0 45 73 74 3d 3d 30 20 26 26 20 70 50 72 6f 62 65 Est==0 && pProbe
27dd0 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f ->onError==OE_No
27de0 6e 65 20 29 20 6e 52 6f 77 45 73 74 20 3d 20 31 ne ) nRowEst = 1
27df0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 ;. }else{. i
27e00 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 6e 52 Col = -1;. nR
27e10 6f 77 45 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 owEst = 0;. }.
27e20 20 70 54 65 72 6d 20 3d 20 77 68 65 72 65 53 63 pTerm = whereSc
27e30 61 6e 49 6e 69 74 28 26 73 63 61 6e 2c 20 70 42 anInit(&scan, pB
27e40 75 69 6c 64 65 72 2d 3e 70 57 43 2c 20 70 53 72 uilder->pWC, pSr
27e50 63 2d 3e 69 43 75 72 73 6f 72 2c 20 69 43 6f 6c c->iCursor, iCol
27e60 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
27e70 20 20 20 20 20 20 20 20 20 20 6f 70 4d 61 73 6b opMask
27e80 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20 73 61 76 , pProbe);. sav
27e90 65 64 5f 6e 45 71 20 3d 20 70 4e 65 77 2d 3e 75 ed_nEq = pNew->u
27ea0 2e 62 74 72 65 65 2e 6e 45 71 3b 0a 20 20 73 61 .btree.nEq;. sa
27eb0 76 65 64 5f 6e 4c 54 65 72 6d 20 3d 20 70 4e 65 ved_nLTerm = pNe
27ec0 77 2d 3e 6e 4c 54 65 72 6d 3b 0a 20 20 73 61 76 w->nLTerm;. sav
27ed0 65 64 5f 77 73 46 6c 61 67 73 20 3d 20 70 4e 65 ed_wsFlags = pNe
27ee0 77 2d 3e 77 73 46 6c 61 67 73 3b 0a 20 20 73 61 w->wsFlags;. sa
27ef0 76 65 64 5f 70 72 65 72 65 71 20 3d 20 70 4e 65 ved_prereq = pNe
27f00 77 2d 3e 70 72 65 72 65 71 3b 0a 20 20 73 61 76 w->prereq;. sav
27f10 65 64 5f 6e 4f 75 74 20 3d 20 70 4e 65 77 2d 3e ed_nOut = pNew->
27f20 6e 4f 75 74 3b 0a 20 20 70 4e 65 77 2d 3e 72 53 nOut;. pNew->rS
27f30 65 74 75 70 20 3d 20 30 3b 0a 20 20 72 4c 6f 67 etup = 0;. rLog
27f40 53 69 7a 65 20 3d 20 65 73 74 4c 6f 67 28 73 71 Size = estLog(sq
27f50 6c 69 74 65 33 4c 6f 67 45 73 74 28 70 50 72 6f lite3LogEst(pPro
27f60 62 65 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d 29 be->aiRowEst[0])
27f70 29 3b 0a 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53 );. for(; rc==S
27f80 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 54 65 72 QLITE_OK && pTer
27f90 6d 21 3d 30 3b 20 70 54 65 72 6d 20 3d 20 77 68 m!=0; pTerm = wh
27fa0 65 72 65 53 63 61 6e 4e 65 78 74 28 26 73 63 61 ereScanNext(&sca
27fb0 6e 29 29 7b 0a 20 20 20 20 69 6e 74 20 6e 49 6e n)){. int nIn
27fc0 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c = 0;.#ifdef SQL
27fd0 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 33 ITE_ENABLE_STAT3
27fe0 5f 4f 52 5f 53 54 41 54 34 0a 20 20 20 20 69 6e _OR_STAT4. in
27ff0 74 20 6e 52 65 63 56 61 6c 69 64 20 3d 20 70 42 t nRecValid = pB
28000 75 69 6c 64 65 72 2d 3e 6e 52 65 63 56 61 6c 69 uilder->nRecVali
28010 64 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 d;.#endif. if
28020 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 ( (pTerm->eOpera
28030 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 7c tor==WO_ISNULL |
28040 7c 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 | (pTerm->wtFlag
28050 73 26 54 45 52 4d 5f 56 4e 55 4c 4c 29 21 3d 30 s&TERM_VNULL)!=0
28060 29 0a 20 20 20 20 20 26 26 20 28 69 43 6f 6c 3c ). && (iCol<
28070 30 20 7c 7c 20 70 53 72 63 2d 3e 70 54 61 62 2d 0 || pSrc->pTab-
28080 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e >aCol[iCol].notN
28090 75 6c 6c 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 ull). ){.
280a0 20 20 63 6f 6e 74 69 6e 75 65 3b 20 2f 2a 20 69 continue; /* i
280b0 67 6e 6f 72 65 20 49 53 20 5b 4e 4f 54 5d 20 4e gnore IS [NOT] N
280c0 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 ULL constraints
280d0 6f 6e 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 on NOT NULL colu
280e0 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 mns */. }.
280f0 20 69 66 28 20 70 54 65 72 6d 2d 3e 70 72 65 72 if( pTerm->prer
28100 65 71 52 69 67 68 74 20 26 20 70 4e 65 77 2d 3e eqRight & pNew->
28110 6d 61 73 6b 53 65 6c 66 20 29 20 63 6f 6e 74 69 maskSelf ) conti
28120 6e 75 65 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 nue;.. assert
28130 28 20 70 4e 65 77 2d 3e 6e 4f 75 74 3d 3d 73 61 ( pNew->nOut==sa
28140 76 65 64 5f 6e 4f 75 74 20 29 3b 0a 0a 20 20 20 ved_nOut );..
28150 20 70 4e 65 77 2d 3e 77 73 46 6c 61 67 73 20 3d pNew->wsFlags =
28160 20 73 61 76 65 64 5f 77 73 46 6c 61 67 73 3b 0a saved_wsFlags;.
28170 20 20 20 20 70 4e 65 77 2d 3e 75 2e 62 74 72 65 pNew->u.btre
28180 65 2e 6e 45 71 20 3d 20 73 61 76 65 64 5f 6e 45 e.nEq = saved_nE
28190 71 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 4c 54 q;. pNew->nLT
281a0 65 72 6d 20 3d 20 73 61 76 65 64 5f 6e 4c 54 65 erm = saved_nLTe
281b0 72 6d 3b 0a 20 20 20 20 69 66 28 20 77 68 65 72 rm;. if( wher
281c0 65 4c 6f 6f 70 52 65 73 69 7a 65 28 64 62 2c 20 eLoopResize(db,
281d0 70 4e 65 77 2c 20 70 4e 65 77 2d 3e 6e 4c 54 65 pNew, pNew->nLTe
281e0 72 6d 2b 31 29 20 29 20 62 72 65 61 6b 3b 20 2f rm+1) ) break; /
281f0 2a 20 4f 4f 4d 20 2a 2f 0a 20 20 20 20 70 4e 65 * OOM */. pNe
28200 77 2d 3e 61 4c 54 65 72 6d 5b 70 4e 65 77 2d 3e w->aLTerm[pNew->
28210 6e 4c 54 65 72 6d 2b 2b 5d 20 3d 20 70 54 65 72 nLTerm++] = pTer
28220 6d 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 m;. pNew->pre
28230 72 65 71 20 3d 20 28 73 61 76 65 64 5f 70 72 65 req = (saved_pre
28240 72 65 71 20 7c 20 70 54 65 72 6d 2d 3e 70 72 65 req | pTerm->pre
28250 72 65 71 52 69 67 68 74 29 20 26 20 7e 70 4e 65 reqRight) & ~pNe
28260 77 2d 3e 6d 61 73 6b 53 65 6c 66 3b 0a 20 20 20 w->maskSelf;.
28270 20 70 4e 65 77 2d 3e 72 52 75 6e 20 3d 20 72 4c pNew->rRun = rL
28280 6f 67 53 69 7a 65 3b 20 2f 2a 20 42 61 73 65 6c ogSize; /* Basel
28290 69 6e 65 20 63 6f 73 74 20 69 73 20 6c 6f 67 32 ine cost is log2
282a0 28 4e 29 2e 20 20 41 64 6a 75 73 74 6d 65 6e 74 (N). Adjustment
282b0 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 20 20 69 s below */. i
282c0 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 f( pTerm->eOpera
282d0 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a 20 tor & WO_IN ){.
282e0 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 Expr *pExpr
282f0 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b = pTerm->pExpr;
28300 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 77 73 46 . pNew->wsF
28310 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f lags |= WHERE_CO
28320 4c 55 4d 4e 5f 49 4e 3b 0a 20 20 20 20 20 20 69 LUMN_IN;. i
28330 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 f( ExprHasProper
28340 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 ty(pExpr, EP_xIs
28350 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 Select) ){.
28360 20 20 20 2f 2a 20 22 78 20 49 4e 20 28 53 45 4c /* "x IN (SEL
28370 45 43 54 20 2e 2e 2e 29 22 3a 20 20 54 55 4e 49 ECT ...)": TUNI
28380 4e 47 3a 20 74 68 65 20 53 45 4c 45 43 54 20 72 NG: the SELECT r
28390 65 74 75 72 6e 73 20 32 35 20 72 6f 77 73 20 2a eturns 25 rows *
283a0 2f 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 3d 20 /. nIn =
283b0 34 36 3b 20 20 61 73 73 65 72 74 28 20 34 36 3d 46; assert( 46=
283c0 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 32 =sqlite3LogEst(2
283d0 35 29 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 5) );. }els
283e0 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 45 78 e if( ALWAYS(pEx
283f0 70 72 2d 3e 78 2e 70 4c 69 73 74 20 26 26 20 70 pr->x.pList && p
28400 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e Expr->x.pList->n
28410 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 Expr) ){.
28420 20 2f 2a 20 22 78 20 49 4e 20 28 76 61 6c 75 65 /* "x IN (value
28430 2c 20 76 61 6c 75 65 2c 20 2e 2e 2e 29 22 20 2a , value, ...)" *
28440 2f 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 3d 20 /. nIn =
28450 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 70 45 sqlite3LogEst(pE
28460 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 xpr->x.pList->nE
28470 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 xpr);. }.
28480 20 20 20 20 70 4e 65 77 2d 3e 72 52 75 6e 20 2b pNew->rRun +
28490 3d 20 6e 49 6e 3b 0a 20 20 20 20 20 20 70 4e 65 = nIn;. pNe
284a0 77 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 2b 2b w->u.btree.nEq++
284b0 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6e 4f ;. pNew->nO
284c0 75 74 20 3d 20 6e 52 6f 77 45 73 74 20 2b 20 6e ut = nRowEst + n
284d0 49 6e 4d 75 6c 20 2b 20 6e 49 6e 3b 0a 20 20 20 InMul + nIn;.
284e0 20 7d 65 6c 73 65 20 69 66 28 20 70 54 65 72 6d }else if( pTerm
284f0 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 ->eOperator & (W
28500 4f 5f 45 51 29 20 29 7b 0a 20 20 20 20 20 20 61 O_EQ) ){. a
28510 73 73 65 72 74 28 20 28 70 4e 65 77 2d 3e 77 73 ssert( (pNew->ws
28520 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f 43 Flags & (WHERE_C
28530 4f 4c 55 4d 4e 5f 4e 55 4c 4c 7c 57 48 45 52 45 OLUMN_NULL|WHERE
28540 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 29 21 3d 30 0a _COLUMN_IN))!=0.
28550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28560 20 20 7c 7c 20 6e 49 6e 4d 75 6c 3d 3d 30 20 29 || nInMul==0 )
28570 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 77 73 ;. pNew->ws
28580 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 Flags |= WHERE_C
28590 4f 4c 55 4d 4e 5f 45 51 3b 0a 20 20 20 20 20 20 OLUMN_EQ;.
285a0 69 66 28 20 69 43 6f 6c 3c 30 20 20 0a 20 20 20 if( iCol<0 .
285b0 20 20 20 20 7c 7c 20 28 70 50 72 6f 62 65 2d 3e || (pProbe->
285c0 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 onError!=OE_None
285d0 20 26 26 20 6e 49 6e 4d 75 6c 3d 3d 30 0a 20 20 && nInMul==0.
285e0 20 20 20 20 20 20 20 20 20 26 26 20 70 4e 65 77 && pNew
285f0 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 3d 3d 70 ->u.btree.nEq==p
28600 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 Probe->nColumn-1
28610 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ). ){.
28620 20 20 20 61 73 73 65 72 74 28 20 28 70 4e 65 77 assert( (pNew
28630 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 ->wsFlags & WHER
28640 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 3d 3d 30 20 E_COLUMN_IN)==0
28650 7c 7c 20 69 43 6f 6c 3c 30 20 29 3b 0a 20 20 20 || iCol<0 );.
28660 20 20 20 20 20 70 4e 65 77 2d 3e 77 73 46 6c 61 pNew->wsFla
28670 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f 4e 45 52 gs |= WHERE_ONER
28680 4f 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 OW;. }.
28690 20 20 70 4e 65 77 2d 3e 75 2e 62 74 72 65 65 2e pNew->u.btree.
286a0 6e 45 71 2b 2b 3b 0a 20 20 20 20 20 20 70 4e 65 nEq++;. pNe
286b0 77 2d 3e 6e 4f 75 74 20 3d 20 6e 52 6f 77 45 73 w->nOut = nRowEs
286c0 74 20 2b 20 6e 49 6e 4d 75 6c 3b 0a 20 20 20 20 t + nInMul;.
286d0 7d 65 6c 73 65 20 69 66 28 20 70 54 65 72 6d 2d }else if( pTerm-
286e0 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f >eOperator & (WO
286f0 5f 49 53 4e 55 4c 4c 29 20 29 7b 0a 20 20 20 20 _ISNULL) ){.
28700 20 20 70 4e 65 77 2d 3e 77 73 46 6c 61 67 73 20 pNew->wsFlags
28710 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f |= WHERE_COLUMN_
28720 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 70 4e 65 77 NULL;. pNew
28730 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 2b 2b 3b ->u.btree.nEq++;
28740 0a 20 20 20 20 20 20 2f 2a 20 54 55 4e 49 4e 47 . /* TUNING
28750 3a 20 49 53 20 4e 55 4c 4c 20 73 65 6c 65 63 74 : IS NULL select
28760 73 20 32 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20 s 2 rows */.
28770 20 20 6e 49 6e 20 3d 20 31 30 3b 20 20 61 73 73 nIn = 10; ass
28780 65 72 74 28 20 31 30 3d 3d 73 71 6c 69 74 65 33 ert( 10==sqlite3
28790 4c 6f 67 45 73 74 28 32 29 20 29 3b 0a 20 20 20 LogEst(2) );.
287a0 20 20 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 3d 20 pNew->nOut =
287b0 6e 52 6f 77 45 73 74 20 2b 20 6e 49 6e 4d 75 6c nRowEst + nInMul
287c0 20 2b 20 6e 49 6e 3b 0a 20 20 20 20 7d 65 6c 73 + nIn;. }els
287d0 65 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 e if( pTerm->eOp
287e0 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 47 54 7c erator & (WO_GT|
287f0 57 4f 5f 47 45 29 20 29 7b 0a 20 20 20 20 20 20 WO_GE) ){.
28800 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d testcase( pTerm-
28810 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f >eOperator & WO_
28820 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 GT );. test
28830 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 case( pTerm->eOp
28840 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20 29 erator & WO_GE )
28850 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 77 73 ;. pNew->ws
28860 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43 Flags |= WHERE_C
28870 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52 OLUMN_RANGE|WHER
28880 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20 E_BTM_LIMIT;.
28890 20 20 20 70 42 74 6d 20 3d 20 70 54 65 72 6d 3b pBtm = pTerm;
288a0 0a 20 20 20 20 20 20 70 54 6f 70 20 3d 20 30 3b . pTop = 0;
288b0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
288c0 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d assert( pTerm-
288d0 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f >eOperator & (WO
288e0 5f 4c 54 7c 57 4f 5f 4c 45 29 20 29 3b 0a 20 20 _LT|WO_LE) );.
288f0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 testcase( pT
28900 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 erm->eOperator &
28910 20 57 4f 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20 WO_LT );.
28920 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d testcase( pTerm-
28930 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f >eOperator & WO_
28940 4c 45 20 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 LE );. pNew
28950 2d 3e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 ->wsFlags |= WHE
28960 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c RE_COLUMN_RANGE|
28970 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b WHERE_TOP_LIMIT;
28980 0a 20 20 20 20 20 20 70 54 6f 70 20 3d 20 70 54 . pTop = pT
28990 65 72 6d 3b 0a 20 20 20 20 20 20 70 42 74 6d 20 erm;. pBtm
289a0 3d 20 28 70 4e 65 77 2d 3e 77 73 46 6c 61 67 73 = (pNew->wsFlags
289b0 20 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d & WHERE_BTM_LIM
289c0 49 54 29 21 3d 30 20 3f 0a 20 20 20 20 20 20 20 IT)!=0 ?.
289d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e pN
289e0 65 77 2d 3e 61 4c 54 65 72 6d 5b 70 4e 65 77 2d ew->aLTerm[pNew-
289f0 3e 6e 4c 54 65 72 6d 2d 32 5d 20 3a 20 30 3b 0a >nLTerm-2] : 0;.
28a00 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4e }. if( pN
28a10 65 77 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 ew->wsFlags & WH
28a20 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 ERE_COLUMN_RANGE
28a30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 64 6a ){. /* Adj
28a40 75 73 74 20 6e 4f 75 74 20 61 6e 64 20 72 52 75 ust nOut and rRu
28a50 6e 20 66 6f 72 20 53 54 41 54 33 20 72 61 6e 67 n for STAT3 rang
28a60 65 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 e values */.
28a70 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
28a80 6e 4f 75 74 3d 3d 73 61 76 65 64 5f 6e 4f 75 74 nOut==saved_nOut
28a90 20 29 3b 0a 20 20 20 20 20 20 77 68 65 72 65 52 );. whereR
28aa0 61 6e 67 65 53 63 61 6e 45 73 74 28 70 50 61 72 angeScanEst(pPar
28ab0 73 65 2c 20 70 42 75 69 6c 64 65 72 2c 20 70 42 se, pBuilder, pB
28ac0 74 6d 2c 20 70 54 6f 70 2c 20 70 4e 65 77 29 3b tm, pTop, pNew);
28ad0 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 . }.#ifdef SQ
28ae0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 LITE_ENABLE_STAT
28af0 33 5f 4f 52 5f 53 54 41 54 34 0a 20 20 20 20 69 3_OR_STAT4. i
28b00 66 28 20 6e 49 6e 4d 75 6c 3d 3d 30 20 0a 20 20 f( nInMul==0 .
28b10 20 20 20 26 26 20 70 50 72 6f 62 65 2d 3e 6e 53 && pProbe->nS
28b20 61 6d 70 6c 65 20 0a 20 20 20 20 20 26 26 20 70 ample . && p
28b30 4e 65 77 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 New->u.btree.nEq
28b40 3c 3d 70 50 72 6f 62 65 2d 3e 6e 53 61 6d 70 6c <=pProbe->nSampl
28b50 65 43 6f 6c 0a 20 20 20 20 20 26 26 20 4f 70 74 eCol. && Opt
28b60 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c 65 64 imizationEnabled
28b70 28 64 62 2c 20 53 51 4c 49 54 45 5f 53 74 61 74 (db, SQLITE_Stat
28b80 33 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 3) . ){.
28b90 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 Expr *pExpr = p
28ba0 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Term->pExpr;.
28bb0 20 20 20 74 52 6f 77 63 6e 74 20 6e 4f 75 74 20 tRowcnt nOut
28bc0 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 28 = 0;. if( (
28bd0 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 pTerm->eOperator
28be0 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e & (WO_EQ|WO_ISN
28bf0 55 4c 4c 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 ULL))!=0 ){.
28c00 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 testcase( pT
28c10 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 erm->eOperator &
28c20 20 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 WO_EQ );.
28c30 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 testcase( pTer
28c40 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 m->eOperator & W
28c50 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 O_ISNULL );.
28c60 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 45 71 rc = whereEq
28c70 75 61 6c 53 63 61 6e 45 73 74 28 70 50 61 72 73 ualScanEst(pPars
28c80 65 2c 20 70 42 75 69 6c 64 65 72 2c 20 70 45 78 e, pBuilder, pEx
28c90 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 6e 4f 75 pr->pRight, &nOu
28ca0 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 t);. }else
28cb0 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 if( (pTerm->eOpe
28cc0 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 29 0a 20 rator & WO_IN).
28cd0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 20 &&
28ce0 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 !ExprHasProperty
28cf0 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 (pExpr, EP_xIsSe
28d00 6c 65 63 74 29 20 20 29 7b 0a 20 20 20 20 20 20 lect) ){.
28d10 20 20 72 63 20 3d 20 77 68 65 72 65 49 6e 53 63 rc = whereInSc
28d20 61 6e 45 73 74 28 70 50 61 72 73 65 2c 20 70 42 anEst(pParse, pB
28d30 75 69 6c 64 65 72 2c 20 70 45 78 70 72 2d 3e 78 uilder, pExpr->x
28d40 2e 70 4c 69 73 74 2c 20 26 6e 4f 75 74 29 3b 0a .pList, &nOut);.
28d50 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 }. as
28d60 73 65 72 74 28 20 6e 4f 75 74 3d 3d 30 20 7c 7c sert( nOut==0 ||
28d70 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 rc==SQLITE_OK )
28d80 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 75 74 ;. if( nOut
28d90 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 ){. pNew
28da0 2d 3e 6e 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 ->nOut = sqlite3
28db0 4c 6f 67 45 73 74 28 6e 4f 75 74 29 3b 0a 20 20 LogEst(nOut);.
28dc0 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e if( pNew->
28dd0 6e 4f 75 74 3e 73 61 76 65 64 5f 6e 4f 75 74 20 nOut>saved_nOut
28de0 29 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 3d 20 73 ) pNew->nOut = s
28df0 61 76 65 64 5f 6e 4f 75 74 3b 0a 20 20 20 20 20 aved_nOut;.
28e00 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
28e10 20 20 20 20 69 66 28 20 28 70 4e 65 77 2d 3e 77 if( (pNew->w
28e20 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45 5f sFlags & (WHERE_
28e30 49 44 58 5f 4f 4e 4c 59 7c 57 48 45 52 45 5f 49 IDX_ONLY|WHERE_I
28e40 50 4b 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 PK))==0 ){.
28e50 20 2f 2a 20 45 61 63 68 20 72 6f 77 20 69 6e 76 /* Each row inv
28e60 6f 6c 76 65 73 20 61 20 73 74 65 70 20 6f 66 20 olves a step of
28e70 74 68 65 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 the index, then
28e80 61 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20 a binary search
28e90 6f 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 of. ** the
28ea0 6d 61 69 6e 20 74 61 62 6c 65 20 2a 2f 0a 20 20 main table */.
28eb0 20 20 20 20 70 4e 65 77 2d 3e 72 52 75 6e 20 3d pNew->rRun =
28ec0 20 20 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 41 sqlite3LogEstA
28ed0 64 64 28 70 4e 65 77 2d 3e 72 52 75 6e 2c 72 4c dd(pNew->rRun,rL
28ee0 6f 67 53 69 7a 65 3e 32 37 20 3f 20 72 4c 6f 67 ogSize>27 ? rLog
28ef0 53 69 7a 65 2d 31 37 20 3a 20 31 30 29 3b 0a 20 Size-17 : 10);.
28f00 20 20 20 7d 0a 20 20 20 20 2f 2a 20 53 74 65 70 }. /* Step
28f10 20 63 6f 73 74 20 66 6f 72 20 65 61 63 68 20 6f cost for each o
28f20 75 74 70 75 74 20 72 6f 77 20 2a 2f 0a 20 20 20 utput row */.
28f30 20 70 4e 65 77 2d 3e 72 52 75 6e 20 3d 20 73 71 pNew->rRun = sq
28f40 6c 69 74 65 33 4c 6f 67 45 73 74 41 64 64 28 70 lite3LogEstAdd(p
28f50 4e 65 77 2d 3e 72 52 75 6e 2c 20 70 4e 65 77 2d New->rRun, pNew-
28f60 3e 6e 4f 75 74 29 3b 0a 20 20 20 20 77 68 65 72 >nOut);. wher
28f70 65 4c 6f 6f 70 4f 75 74 70 75 74 41 64 6a 75 73 eLoopOutputAdjus
28f80 74 28 70 42 75 69 6c 64 65 72 2d 3e 70 57 43 2c t(pBuilder->pWC,
28f90 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 63 20 3d pNew);. rc =
28fa0 20 77 68 65 72 65 4c 6f 6f 70 49 6e 73 65 72 74 whereLoopInsert
28fb0 28 70 42 75 69 6c 64 65 72 2c 20 70 4e 65 77 29 (pBuilder, pNew)
28fc0 3b 0a 20 20 20 20 69 66 28 20 28 70 4e 65 77 2d ;. if( (pNew-
28fd0 3e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 >wsFlags & WHERE
28fe0 5f 54 4f 50 5f 4c 49 4d 49 54 29 3d 3d 30 0a 20 _TOP_LIMIT)==0.
28ff0 20 20 20 20 26 26 20 70 4e 65 77 2d 3e 75 2e 62 && pNew->u.b
29000 74 72 65 65 2e 6e 45 71 3c 28 70 50 72 6f 62 65 tree.nEq<(pProbe
29010 2d 3e 6e 43 6f 6c 75 6d 6e 20 2b 20 28 70 50 72 ->nColumn + (pPr
29020 6f 62 65 2d 3e 7a 4e 61 6d 65 21 3d 30 29 29 0a obe->zName!=0)).
29030 20 20 20 20 29 7b 0a 20 20 20 20 20 20 77 68 65 ){. whe
29040 72 65 4c 6f 6f 70 41 64 64 42 74 72 65 65 49 6e reLoopAddBtreeIn
29050 64 65 78 28 70 42 75 69 6c 64 65 72 2c 20 70 53 dex(pBuilder, pS
29060 72 63 2c 20 70 50 72 6f 62 65 2c 20 6e 49 6e 4d rc, pProbe, nInM
29070 75 6c 2b 6e 49 6e 29 3b 0a 20 20 20 20 7d 0a 20 ul+nIn);. }.
29080 20 20 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 3d 20 pNew->nOut =
29090 73 61 76 65 64 5f 6e 4f 75 74 3b 0a 23 69 66 64 saved_nOut;.#ifd
290a0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
290b0 5f 53 54 41 54 33 5f 4f 52 5f 53 54 41 54 34 0a _STAT3_OR_STAT4.
290c0 20 20 20 20 70 42 75 69 6c 64 65 72 2d 3e 6e 52 pBuilder->nR
290d0 65 63 56 61 6c 69 64 20 3d 20 6e 52 65 63 56 61 ecValid = nRecVa
290e0 6c 69 64 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a lid;.#endif. }.
290f0 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 20 3d pNew->prereq =
29100 20 73 61 76 65 64 5f 70 72 65 72 65 71 3b 0a 20 saved_prereq;.
29110 20 70 4e 65 77 2d 3e 75 2e 62 74 72 65 65 2e 6e pNew->u.btree.n
29120 45 71 20 3d 20 73 61 76 65 64 5f 6e 45 71 3b 0a Eq = saved_nEq;.
29130 20 20 70 4e 65 77 2d 3e 77 73 46 6c 61 67 73 20 pNew->wsFlags
29140 3d 20 73 61 76 65 64 5f 77 73 46 6c 61 67 73 3b = saved_wsFlags;
29150 0a 20 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 3d 20 . pNew->nOut =
29160 73 61 76 65 64 5f 6e 4f 75 74 3b 0a 20 20 70 4e saved_nOut;. pN
29170 65 77 2d 3e 6e 4c 54 65 72 6d 20 3d 20 73 61 76 ew->nLTerm = sav
29180 65 64 5f 6e 4c 54 65 72 6d 3b 0a 20 20 72 65 74 ed_nLTerm;. ret
29190 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a urn rc;.}../*.**
291a0 20 52 65 74 75 72 6e 20 54 72 75 65 20 69 66 20 Return True if
291b0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 it is possible t
291c0 68 61 74 20 70 49 6e 64 65 78 20 6d 69 67 68 74 hat pIndex might
291d0 20 62 65 20 75 73 65 66 75 6c 20 69 6e 0a 2a 2a be useful in.**
291e0 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 implementing th
291f0 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
29200 65 20 69 6e 20 70 42 75 69 6c 64 65 72 2e 0a 2a e in pBuilder..*
29210 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 46 61 6c 73 *.** Return Fals
29220 65 20 69 66 20 70 42 75 69 6c 64 65 72 20 64 6f e if pBuilder do
29230 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 es not contain a
29240 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 n ORDER BY claus
29250 65 20 6f 72 0a 2a 2a 20 69 66 20 74 68 65 72 65 e or.** if there
29260 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 70 is no way for p
29270 49 6e 64 65 78 20 74 6f 20 62 65 20 75 73 65 66 Index to be usef
29280 75 6c 20 69 6e 20 69 6d 70 6c 65 6d 65 6e 74 69 ul in implementi
29290 6e 67 20 74 68 61 74 0a 2a 2a 20 4f 52 44 45 52 ng that.** ORDER
292a0 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 BY clause..*/.s
292b0 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 4d tatic int indexM
292c0 69 67 68 74 48 65 6c 70 57 69 74 68 4f 72 64 65 ightHelpWithOrde
292d0 72 42 79 28 0a 20 20 57 68 65 72 65 4c 6f 6f 70 rBy(. WhereLoop
292e0 42 75 69 6c 64 65 72 20 2a 70 42 75 69 6c 64 65 Builder *pBuilde
292f0 72 2c 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 r,. Index *pInd
29300 65 78 2c 0a 20 20 69 6e 74 20 69 43 75 72 73 6f ex,. int iCurso
29310 72 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 r.){. ExprList
29320 2a 70 4f 42 3b 0a 20 20 69 6e 74 20 69 69 2c 20 *pOB;. int ii,
29330 6a 6a 3b 0a 0a 20 20 69 66 28 20 70 49 6e 64 65 jj;.. if( pInde
29340 78 2d 3e 62 55 6e 6f 72 64 65 72 65 64 20 29 20 x->bUnordered )
29350 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
29360 28 70 4f 42 20 3d 20 70 42 75 69 6c 64 65 72 2d (pOB = pBuilder-
29370 3e 70 57 49 6e 66 6f 2d 3e 70 4f 72 64 65 72 42 >pWInfo->pOrderB
29380 79 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 y)==0 ) return 0
29390 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 ;. for(ii=0; ii
293a0 3c 70 4f 42 2d 3e 6e 45 78 70 72 3b 20 69 69 2b <pOB->nExpr; ii+
293b0 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 +){. Expr *pE
293c0 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 xpr = sqlite3Exp
293d0 72 53 6b 69 70 43 6f 6c 6c 61 74 65 28 70 4f 42 rSkipCollate(pOB
293e0 2d 3e 61 5b 69 69 5d 2e 70 45 78 70 72 29 3b 0a ->a[ii].pExpr);.
293f0 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f if( pExpr->o
29400 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 72 p!=TK_COLUMN ) r
29410 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 eturn 0;. if(
29420 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d pExpr->iTable==
29430 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 iCursor ){.
29440 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 70 49 for(jj=0; jj<pI
29450 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a ndex->nColumn; j
29460 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 j++){. if
29470 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e ( pExpr->iColumn
29480 3d 3d 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 ==pIndex->aiColu
29490 6d 6e 5b 6a 6a 5d 20 29 20 72 65 74 75 72 6e 20 mn[jj] ) return
294a0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 1;. }. }
294b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b . }. return 0;
294c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e .}../*.** Return
294d0 20 61 20 62 69 74 6d 61 73 6b 20 77 68 65 72 65 a bitmask where
294e0 20 31 73 20 69 6e 64 69 63 61 74 65 20 74 68 61 1s indicate tha
294f0 74 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 t the correspond
29500 69 6e 67 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a ing column of.**
29510 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 75 73 the table is us
29520 65 64 20 62 79 20 61 6e 20 69 6e 64 65 78 2e 20 ed by an index.
29530 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 Only the first
29540 36 33 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 63 63 columns are c
29550 6f 6e 73 69 64 65 72 65 64 2e 0a 2a 2f 0a 73 74 onsidered..*/.st
29560 61 74 69 63 20 42 69 74 6d 61 73 6b 20 63 6f 6c atic Bitmask col
29570 75 6d 6e 73 49 6e 49 6e 64 65 78 28 49 6e 64 65 umnsInIndex(Inde
29580 78 20 2a 70 49 64 78 29 7b 0a 20 20 42 69 74 6d x *pIdx){. Bitm
29590 61 73 6b 20 6d 20 3d 20 30 3b 0a 20 20 69 6e 74 ask m = 0;. int
295a0 20 6a 3b 0a 20 20 66 6f 72 28 6a 3d 70 49 64 78 j;. for(j=pIdx
295b0 2d 3e 6e 43 6f 6c 75 6d 6e 2d 31 3b 20 6a 3e 3d ->nColumn-1; j>=
295c0 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 0; j--){. int
295d0 20 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c x = pIdx->aiCol
295e0 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 61 73 73 65 umn[j];. asse
295f0 72 74 28 20 78 3e 3d 30 20 29 3b 0a 20 20 20 20 rt( x>=0 );.
29600 74 65 73 74 63 61 73 65 28 20 78 3d 3d 42 4d 53 testcase( x==BMS
29610 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 -1 );. testca
29620 73 65 28 20 78 3d 3d 42 4d 53 2d 32 20 29 3b 0a se( x==BMS-2 );.
29630 20 20 20 20 69 66 28 20 78 3c 42 4d 53 2d 31 20 if( x<BMS-1
29640 29 20 6d 20 7c 3d 20 4d 41 53 4b 42 49 54 28 78 ) m |= MASKBIT(x
29650 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 );. }. return
29660 6d 3b 0a 7d 0a 0a 2f 2a 20 43 68 65 63 6b 20 74 m;.}../* Check t
29670 6f 20 73 65 65 20 69 66 20 61 20 70 61 72 74 69 o see if a parti
29680 61 6c 20 69 6e 64 65 78 20 77 69 74 68 20 70 50 al index with pP
29690 61 72 74 49 6e 64 65 78 57 68 65 72 65 20 63 61 artIndexWhere ca
296a0 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 69 6e 20 n be used.** in
296b0 74 68 65 20 63 75 72 72 65 6e 74 20 71 75 65 72 the current quer
296c0 79 2e 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 y. Return true
296d0 69 66 20 69 74 20 63 61 6e 20 62 65 20 61 6e 64 if it can be and
296e0 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a 2a false if not..*
296f0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65 /.static int whe
29700 72 65 55 73 61 62 6c 65 50 61 72 74 69 61 6c 49 reUsablePartialI
29710 6e 64 65 78 28 69 6e 74 20 69 54 61 62 2c 20 57 ndex(int iTab, W
29720 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c hereClause *pWC,
29730 20 45 78 70 72 20 2a 70 57 68 65 72 65 29 7b 0a Expr *pWhere){.
29740 20 20 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 int i;. Where
29750 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 66 Term *pTerm;. f
29760 6f 72 28 69 3d 30 2c 20 70 54 65 72 6d 3d 70 57 or(i=0, pTerm=pW
29770 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 C->a; i<pWC->nTe
29780 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b rm; i++, pTerm++
29790 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 ){. if( sqlit
297a0 65 33 45 78 70 72 49 6d 70 6c 69 65 73 45 78 70 e3ExprImpliesExp
297b0 72 28 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 r(pTerm->pExpr,
297c0 70 57 68 65 72 65 2c 20 69 54 61 62 29 20 29 20 pWhere, iTab) )
297d0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 return 1;. }.
297e0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
297f0 2a 2a 20 41 64 64 20 61 6c 6c 20 57 68 65 72 65 ** Add all Where
29800 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 20 66 6f 72 Loop objects for
29810 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 a single table
29820 6f 66 20 74 68 65 20 6a 6f 69 6e 20 77 68 65 72 of the join wher
29830 65 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 69 e the table.** i
29840 73 20 69 64 65 6e 66 69 65 64 20 62 79 20 70 42 s idenfied by pB
29850 75 69 6c 64 65 72 2d 3e 70 4e 65 77 2d 3e 69 54 uilder->pNew->iT
29860 61 62 2e 20 20 54 68 61 74 20 74 61 62 6c 65 20 ab. That table
29870 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f is guaranteed to
29880 20 62 65 0a 2a 2a 20 61 20 62 2d 74 72 65 65 20 be.** a b-tree
29890 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 76 69 72 table, not a vir
298a0 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 tual table..*/.s
298b0 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 4c tatic int whereL
298c0 6f 6f 70 41 64 64 42 74 72 65 65 28 0a 20 20 57 oopAddBtree(. W
298d0 68 65 72 65 4c 6f 6f 70 42 75 69 6c 64 65 72 20 hereLoopBuilder
298e0 2a 70 42 75 69 6c 64 65 72 2c 20 2f 2a 20 57 48 *pBuilder, /* WH
298f0 45 52 45 20 63 6c 61 75 73 65 20 69 6e 66 6f 72 ERE clause infor
29900 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 69 74 6d mation */. Bitm
29910 61 73 6b 20 6d 45 78 74 72 61 20 20 20 20 20 20 ask mExtra
29920 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 /* Extra
29930 20 70 72 65 72 65 71 75 65 73 69 74 65 73 20 66 prerequesites f
29940 6f 72 20 75 73 69 6e 67 20 74 68 69 73 20 74 61 or using this ta
29950 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 ble */.){. Wher
29960 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 eInfo *pWInfo;
29970 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 /* WHERE
29980 20 61 6e 61 6c 79 73 69 73 20 63 6f 6e 74 65 78 analysis contex
29990 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50 t */. Index *pP
299a0 72 6f 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 robe;
299b0 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78 20 77 /* An index w
299c0 65 20 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 e are evaluating
299d0 20 2a 2f 0a 20 20 49 6e 64 65 78 20 73 50 6b 3b */. Index sPk;
299e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
299f0 20 20 2f 2a 20 41 20 66 61 6b 65 20 69 6e 64 65 /* A fake inde
29a00 78 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 x object for the
29a10 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a primary key */.
29a20 20 20 74 52 6f 77 63 6e 74 20 61 69 52 6f 77 45 tRowcnt aiRowE
29a30 73 74 50 6b 5b 32 5d 3b 20 20 20 20 20 20 2f 2a stPk[2]; /*
29a40 20 54 68 65 20 61 69 52 6f 77 45 73 74 5b 5d 20 The aiRowEst[]
29a50 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 50 value for the sP
29a60 6b 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 k index */. int
29a70 20 61 69 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31 aiColumnPk = -1
29a80 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
29a90 61 43 6f 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65 20 aColumn[] value
29aa0 66 6f 72 20 74 68 65 20 73 50 6b 20 69 6e 64 65 for the sPk inde
29ab0 78 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a x */. SrcList *
29ac0 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20 20 pTabList;
29ad0 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 /* The FROM c
29ae0 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 lause */. struc
29af0 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
29b00 70 53 72 63 3b 20 20 2f 2a 20 54 68 65 20 46 52 pSrc; /* The FR
29b10 4f 4d 20 63 6c 61 75 73 65 20 62 74 72 65 65 20 OM clause btree
29b20 74 65 72 6d 20 74 6f 20 61 64 64 20 2a 2f 0a 20 term to add */.
29b30 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4e 65 77 WhereLoop *pNew
29b40 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ; /*
29b50 54 65 6d 70 6c 61 74 65 20 57 68 65 72 65 4c 6f Template WhereLo
29b60 6f 70 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 op object */. i
29b70 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f nt rc = SQLITE_O
29b80 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 K; /* Re
29b90 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 turn code */. i
29ba0 6e 74 20 69 53 6f 72 74 49 64 78 20 3d 20 31 3b nt iSortIdx = 1;
29bb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
29bc0 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 dex number */.
29bd0 69 6e 74 20 62 3b 20 20 20 20 20 20 20 20 20 20 int b;
29be0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 /* A
29bf0 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 20 2a boolean value *
29c00 2f 0a 20 20 4c 6f 67 45 73 74 20 72 53 69 7a 65 /. LogEst rSize
29c10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
29c20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 /* number of row
29c30 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a s in the table *
29c40 2f 0a 20 20 4c 6f 67 45 73 74 20 72 4c 6f 67 53 /. LogEst rLogS
29c50 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 ize;
29c60 2f 2a 20 4c 6f 67 61 72 69 74 68 6d 20 6f 66 20 /* Logarithm of
29c70 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f the number of ro
29c80 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 ws in the table
29c90 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 */. WhereClause
29ca0 20 2a 70 57 43 3b 20 20 20 20 20 20 20 20 20 20 *pWC;
29cb0 20 2f 2a 20 54 68 65 20 70 61 72 73 65 64 20 57 /* The parsed W
29cc0 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 HERE clause */.
29cd0 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 Table *pTab;
29ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
29cf0 54 61 62 6c 65 20 62 65 69 6e 67 20 71 75 65 72 Table being quer
29d00 69 65 64 20 2a 2f 0a 20 20 0a 20 20 70 4e 65 77 ied */. . pNew
29d10 20 3d 20 70 42 75 69 6c 64 65 72 2d 3e 70 4e 65 = pBuilder->pNe
29d20 77 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 70 42 w;. pWInfo = pB
29d30 75 69 6c 64 65 72 2d 3e 70 57 49 6e 66 6f 3b 0a uilder->pWInfo;.
29d40 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 57 49 pTabList = pWI
29d50 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 3b 0a 20 nfo->pTabList;.
29d60 20 70 53 72 63 20 3d 20 70 54 61 62 4c 69 73 74 pSrc = pTabList
29d70 2d 3e 61 20 2b 20 70 4e 65 77 2d 3e 69 54 61 62 ->a + pNew->iTab
29d80 3b 0a 20 20 70 54 61 62 20 3d 20 70 53 72 63 2d ;. pTab = pSrc-
29d90 3e 70 54 61 62 3b 0a 20 20 70 57 43 20 3d 20 70 >pTab;. pWC = p
29da0 42 75 69 6c 64 65 72 2d 3e 70 57 43 3b 0a 20 20 Builder->pWC;.
29db0 61 73 73 65 72 74 28 20 21 49 73 56 69 72 74 75 assert( !IsVirtu
29dc0 61 6c 28 70 53 72 63 2d 3e 70 54 61 62 29 20 29 al(pSrc->pTab) )
29dd0 3b 0a 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 ;.. if( pSrc->p
29de0 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 2f 2a 20 Index ){. /*
29df0 41 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c An INDEXED BY cl
29e00 61 75 73 65 20 73 70 65 63 69 66 69 65 73 20 61 ause specifies a
29e10 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65 particular inde
29e20 78 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 20 20 x to use */.
29e30 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d 3e 70 pProbe = pSrc->p
29e40 49 6e 64 65 78 3b 0a 20 20 7d 65 6c 73 65 7b 0a Index;. }else{.
29e50 20 20 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 /* There is
29e60 6e 6f 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c no INDEXED BY cl
29e70 61 75 73 65 2e 20 20 43 72 65 61 74 65 20 61 20 ause. Create a
29e80 66 61 6b 65 20 49 6e 64 65 78 20 6f 62 6a 65 63 fake Index objec
29e90 74 20 69 6e 20 6c 6f 63 61 6c 0a 20 20 20 20 2a t in local. *
29ea0 2a 20 76 61 72 69 61 62 6c 65 20 73 50 6b 20 74 * variable sPk t
29eb0 6f 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 o represent the
29ec0 72 6f 77 69 64 20 70 72 69 6d 61 72 79 20 6b 65 rowid primary ke
29ed0 79 20 69 6e 64 65 78 2e 20 20 4d 61 6b 65 20 74 y index. Make t
29ee0 68 69 73 0a 20 20 20 20 2a 2a 20 66 61 6b 65 20 his. ** fake
29ef0 69 6e 64 65 78 20 74 68 65 20 66 69 72 73 74 20 index the first
29f00 69 6e 20 61 20 63 68 61 69 6e 20 6f 66 20 49 6e in a chain of In
29f10 64 65 78 20 6f 62 6a 65 63 74 73 20 77 69 74 68 dex objects with
29f20 20 61 6c 6c 20 6f 66 20 74 68 65 20 72 65 61 6c all of the real
29f30 0a 20 20 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 . ** indices
29f40 74 6f 20 66 6f 6c 6c 6f 77 20 2a 2f 0a 20 20 20 to follow */.
29f50 20 49 6e 64 65 78 20 2a 70 46 69 72 73 74 3b 20 Index *pFirst;
29f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
29f70 20 2f 2a 20 46 69 72 73 74 20 6f 66 20 72 65 61 /* First of rea
29f80 6c 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 l indices on the
29f90 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 table */. me
29fa0 6d 73 65 74 28 26 73 50 6b 2c 20 30 2c 20 73 69 mset(&sPk, 0, si
29fb0 7a 65 6f 66 28 49 6e 64 65 78 29 29 3b 0a 20 20 zeof(Index));.
29fc0 20 20 73 50 6b 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 sPk.nColumn =
29fd0 31 3b 0a 20 20 20 20 73 50 6b 2e 61 69 43 6f 6c 1;. sPk.aiCol
29fe0 75 6d 6e 20 3d 20 26 61 69 43 6f 6c 75 6d 6e 50 umn = &aiColumnP
29ff0 6b 3b 0a 20 20 20 20 73 50 6b 2e 61 69 52 6f 77 k;. sPk.aiRow
2a000 45 73 74 20 3d 20 61 69 52 6f 77 45 73 74 50 6b Est = aiRowEstPk
2a010 3b 0a 20 20 20 20 73 50 6b 2e 6f 6e 45 72 72 6f ;. sPk.onErro
2a020 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a r = OE_Replace;.
2a030 20 20 20 20 73 50 6b 2e 70 54 61 62 6c 65 20 3d sPk.pTable =
2a040 20 70 54 61 62 3b 0a 20 20 20 20 61 69 52 6f 77 pTab;. aiRow
2a050 45 73 74 50 6b 5b 30 5d 20 3d 20 70 54 61 62 2d EstPk[0] = pTab-
2a060 3e 6e 52 6f 77 45 73 74 3b 0a 20 20 20 20 61 69 >nRowEst;. ai
2a070 52 6f 77 45 73 74 50 6b 5b 31 5d 20 3d 20 31 3b RowEstPk[1] = 1;
2a080 0a 20 20 20 20 70 46 69 72 73 74 20 3d 20 70 53 . pFirst = pS
2a090 72 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64 65 78 rc->pTab->pIndex
2a0a0 3b 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e ;. if( pSrc->
2a0b0 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 29 7b notIndexed==0 ){
2a0c0 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 . /* The re
2a0d0 61 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68 al indices of th
2a0e0 65 20 74 61 62 6c 65 20 61 72 65 20 6f 6e 6c 79 e table are only
2a0f0 20 63 6f 6e 73 69 64 65 72 65 64 20 69 66 20 74 considered if t
2a100 68 65 0a 20 20 20 20 20 20 2a 2a 20 4e 4f 54 20 he. ** NOT
2a110 49 4e 44 45 58 45 44 20 71 75 61 6c 69 66 69 65 INDEXED qualifie
2a120 72 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f r is omitted fro
2a130 6d 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 m the FROM claus
2a140 65 20 2a 2f 0a 20 20 20 20 20 20 73 50 6b 2e 70 e */. sPk.p
2a150 4e 65 78 74 20 3d 20 70 46 69 72 73 74 3b 0a 20 Next = pFirst;.
2a160 20 20 20 7d 0a 20 20 20 20 70 50 72 6f 62 65 20 }. pProbe
2a170 3d 20 26 73 50 6b 3b 0a 20 20 7d 0a 20 20 72 53 = &sPk;. }. rS
2a180 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4c 6f 67 ize = sqlite3Log
2a190 45 73 74 28 70 54 61 62 2d 3e 6e 52 6f 77 45 73 Est(pTab->nRowEs
2a1a0 74 29 3b 0a 20 20 72 4c 6f 67 53 69 7a 65 20 3d t);. rLogSize =
2a1b0 20 65 73 74 4c 6f 67 28 72 53 69 7a 65 29 3b 0a estLog(rSize);.
2a1c0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
2a1d0 4f 4d 49 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 OMIT_AUTOMATIC_I
2a1e0 4e 44 45 58 0a 20 20 2f 2a 20 41 75 74 6f 6d 61 NDEX. /* Automa
2a1f0 74 69 63 20 69 6e 64 65 78 65 73 20 2a 2f 0a 20 tic indexes */.
2a200 20 69 66 28 20 21 70 42 75 69 6c 64 65 72 2d 3e if( !pBuilder->
2a210 70 4f 72 53 65 74 0a 20 20 20 26 26 20 28 70 57 pOrSet. && (pW
2a220 49 6e 66 6f 2d 3e 70 50 61 72 73 65 2d 3e 64 62 Info->pParse->db
2a230 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ->flags & SQLITE
2a240 5f 41 75 74 6f 49 6e 64 65 78 29 21 3d 30 0a 20 _AutoIndex)!=0.
2a250 20 20 26 26 20 70 53 72 63 2d 3e 70 49 6e 64 65 && pSrc->pInde
2a260 78 3d 3d 30 0a 20 20 20 26 26 20 21 70 53 72 63 x==0. && !pSrc
2a270 2d 3e 76 69 61 43 6f 72 6f 75 74 69 6e 65 0a 20 ->viaCoroutine.
2a280 20 20 26 26 20 21 70 53 72 63 2d 3e 6e 6f 74 49 && !pSrc->notI
2a290 6e 64 65 78 65 64 0a 20 20 20 26 26 20 21 70 53 ndexed. && !pS
2a2a0 72 63 2d 3e 69 73 43 6f 72 72 65 6c 61 74 65 64 rc->isCorrelated
2a2b0 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 47 65 6e . ){. /* Gen
2a2c0 65 72 61 74 65 20 61 75 74 6f 2d 69 6e 64 65 78 erate auto-index
2a2d0 20 57 68 65 72 65 4c 6f 6f 70 73 20 2a 2f 0a 20 WhereLoops */.
2a2e0 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 WhereTerm *pT
2a2f0 65 72 6d 3b 0a 20 20 20 20 57 68 65 72 65 54 65 erm;. WhereTe
2a300 72 6d 20 2a 70 57 43 45 6e 64 20 3d 20 70 57 43 rm *pWCEnd = pWC
2a310 2d 3e 61 20 2b 20 70 57 43 2d 3e 6e 54 65 72 6d ->a + pWC->nTerm
2a320 3b 0a 20 20 20 20 66 6f 72 28 70 54 65 72 6d 3d ;. for(pTerm=
2a330 70 57 43 2d 3e 61 3b 20 72 63 3d 3d 53 51 4c 49 pWC->a; rc==SQLI
2a340 54 45 5f 4f 4b 20 26 26 20 70 54 65 72 6d 3c 70 TE_OK && pTerm<p
2a350 57 43 45 6e 64 3b 20 70 54 65 72 6d 2b 2b 29 7b WCEnd; pTerm++){
2a360 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d . if( pTerm
2a370 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 26 20 ->prereqRight &
2a380 70 4e 65 77 2d 3e 6d 61 73 6b 53 65 6c 66 20 29 pNew->maskSelf )
2a390 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
2a3a0 20 69 66 28 20 74 65 72 6d 43 61 6e 44 72 69 76 if( termCanDriv
2a3b0 65 49 6e 64 65 78 28 70 54 65 72 6d 2c 20 70 53 eIndex(pTerm, pS
2a3c0 72 63 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 rc, 0) ){.
2a3d0 20 20 70 4e 65 77 2d 3e 75 2e 62 74 72 65 65 2e pNew->u.btree.
2a3e0 6e 45 71 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 nEq = 1;.
2a3f0 20 70 4e 65 77 2d 3e 75 2e 62 74 72 65 65 2e 70 pNew->u.btree.p
2a400 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 Index = 0;.
2a410 20 20 20 70 4e 65 77 2d 3e 6e 4c 54 65 72 6d 20 pNew->nLTerm
2a420 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 = 1;. pNe
2a430 77 2d 3e 61 4c 54 65 72 6d 5b 30 5d 20 3d 20 70 w->aLTerm[0] = p
2a440 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 2f 2a Term;. /*
2a450 20 54 55 4e 49 4e 47 3a 20 4f 6e 65 2d 74 69 6d TUNING: One-tim
2a460 65 20 63 6f 73 74 20 66 6f 72 20 63 6f 6d 70 75 e cost for compu
2a470 74 69 6e 67 20 74 68 65 20 61 75 74 6f 6d 61 74 ting the automat
2a480 69 63 20 69 6e 64 65 78 20 69 73 0a 20 20 20 20 ic index is.
2a490 20 20 20 20 2a 2a 20 61 70 70 72 6f 78 69 6d 61 ** approxima
2a4a0 74 65 6c 79 20 37 2a 4e 2a 6c 6f 67 32 28 4e 29 tely 7*N*log2(N)
2a4b0 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 where N is the
2a4c0 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 number of rows i
2a4d0 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 n. ** the
2a4e0 20 74 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 table being ind
2a4f0 65 78 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 exed. */.
2a500 20 70 4e 65 77 2d 3e 72 53 65 74 75 70 20 3d 20 pNew->rSetup =
2a510 72 4c 6f 67 53 69 7a 65 20 2b 20 72 53 69 7a 65 rLogSize + rSize
2a520 20 2b 20 32 38 3b 20 20 61 73 73 65 72 74 28 20 + 28; assert(
2a530 32 38 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 28==sqlite3LogEs
2a540 74 28 37 29 20 29 3b 0a 20 20 20 20 20 20 20 20 t(7) );.
2a550 2f 2a 20 54 55 4e 49 4e 47 3a 20 45 61 63 68 20 /* TUNING: Each
2a560 69 6e 64 65 78 20 6c 6f 6f 6b 75 70 20 79 69 65 index lookup yie
2a570 6c 64 73 20 32 30 20 72 6f 77 73 20 69 6e 20 74 lds 20 rows in t
2a580 68 65 20 74 61 62 6c 65 2e 20 20 54 68 69 73 0a he table. This.
2a590 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6d 6f ** is mo
2a5a0 72 65 20 74 68 61 6e 20 74 68 65 20 75 73 75 61 re than the usua
2a5b0 6c 20 67 75 65 73 73 20 6f 66 20 31 30 20 72 6f l guess of 10 ro
2a5c0 77 73 2c 20 73 69 6e 63 65 20 77 65 20 68 61 76 ws, since we hav
2a5d0 65 20 6e 6f 20 77 61 79 0a 20 20 20 20 20 20 20 e no way.
2a5e0 20 2a 2a 20 6f 66 20 6b 6e 6f 77 6e 69 6e 67 20 ** of knowning
2a5f0 68 6f 77 20 73 65 6c 65 63 74 69 76 65 20 74 68 how selective th
2a600 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 75 6c 74 e index will ult
2a610 69 6d 61 74 65 6c 79 20 62 65 2e 20 20 49 74 20 imately be. It
2a620 77 6f 75 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a would. **
2a630 20 6e 6f 74 20 62 65 20 75 6e 72 65 61 73 6f 6e not be unreason
2a640 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20 74 68 69 able to make thi
2a650 73 20 76 61 6c 75 65 20 6d 75 63 68 20 6c 61 72 s value much lar
2a660 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 ger. */.
2a670 70 4e 65 77 2d 3e 6e 4f 75 74 20 3d 20 34 33 3b pNew->nOut = 43;
2a680 20 20 61 73 73 65 72 74 28 20 34 33 3d 3d 73 71 assert( 43==sq
2a690 6c 69 74 65 33 4c 6f 67 45 73 74 28 32 30 29 20 lite3LogEst(20)
2a6a0 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d );. pNew-
2a6b0 3e 72 52 75 6e 20 3d 20 73 71 6c 69 74 65 33 4c >rRun = sqlite3L
2a6c0 6f 67 45 73 74 41 64 64 28 72 4c 6f 67 53 69 7a ogEstAdd(rLogSiz
2a6d0 65 2c 70 4e 65 77 2d 3e 6e 4f 75 74 29 3b 0a 20 e,pNew->nOut);.
2a6e0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 77 73 46 pNew->wsF
2a6f0 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 41 55 54 lags = WHERE_AUT
2a700 4f 5f 49 4e 44 45 58 3b 0a 20 20 20 20 20 20 20 O_INDEX;.
2a710 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 20 3d 20 pNew->prereq =
2a720 6d 45 78 74 72 61 20 7c 20 70 54 65 72 6d 2d 3e mExtra | pTerm->
2a730 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20 prereqRight;.
2a740 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 4c rc = whereL
2a750 6f 6f 70 49 6e 73 65 72 74 28 70 42 75 69 6c 64 oopInsert(pBuild
2a760 65 72 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 20 er, pNew);.
2a770 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e }. }. }.#en
2a780 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
2a790 49 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44 IT_AUTOMATIC_IND
2a7a0 45 58 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 EX */.. /* Loop
2a7b0 20 6f 76 65 72 20 61 6c 6c 20 69 6e 64 69 63 65 over all indice
2a7c0 73 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 3b 20 72 s. */. for(; r
2a7d0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 c==SQLITE_OK &&
2a7e0 70 50 72 6f 62 65 3b 20 70 50 72 6f 62 65 3d 70 pProbe; pProbe=p
2a7f0 50 72 6f 62 65 2d 3e 70 4e 65 78 74 2c 20 69 53 Probe->pNext, iS
2a800 6f 72 74 49 64 78 2b 2b 29 7b 0a 20 20 20 20 69 ortIdx++){. i
2a810 66 28 20 70 50 72 6f 62 65 2d 3e 70 50 61 72 74 f( pProbe->pPart
2a820 49 64 78 57 68 65 72 65 21 3d 30 0a 20 20 20 20 IdxWhere!=0.
2a830 20 26 26 20 21 77 68 65 72 65 55 73 61 62 6c 65 && !whereUsable
2a840 50 61 72 74 69 61 6c 49 6e 64 65 78 28 70 4e 65 PartialIndex(pNe
2a850 77 2d 3e 69 54 61 62 2c 20 70 57 43 2c 20 70 50 w->iTab, pWC, pP
2a860 72 6f 62 65 2d 3e 70 50 61 72 74 49 64 78 57 68 robe->pPartIdxWh
2a870 65 72 65 29 20 29 7b 0a 20 20 20 20 20 20 63 6f ere) ){. co
2a880 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 50 61 72 74 ntinue; /* Part
2a890 69 61 6c 20 69 6e 64 65 78 20 69 6e 61 70 70 72 ial index inappr
2a8a0 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 69 73 opriate for this
2a8b0 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 7d 0a query */. }.
2a8c0 20 20 20 20 70 4e 65 77 2d 3e 75 2e 62 74 72 65 pNew->u.btre
2a8d0 65 2e 6e 45 71 20 3d 20 30 3b 0a 20 20 20 20 70 e.nEq = 0;. p
2a8e0 4e 65 77 2d 3e 6e 4c 54 65 72 6d 20 3d 20 30 3b New->nLTerm = 0;
2a8f0 0a 20 20 20 20 70 4e 65 77 2d 3e 69 53 6f 72 74 . pNew->iSort
2a900 49 64 78 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65 Idx = 0;. pNe
2a910 77 2d 3e 72 53 65 74 75 70 20 3d 20 30 3b 0a 20 w->rSetup = 0;.
2a920 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 20 pNew->prereq
2a930 3d 20 6d 45 78 74 72 61 3b 0a 20 20 20 20 70 4e = mExtra;. pN
2a940 65 77 2d 3e 6e 4f 75 74 20 3d 20 72 53 69 7a 65 ew->nOut = rSize
2a950 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 75 2e 62 74 ;. pNew->u.bt
2a960 72 65 65 2e 70 49 6e 64 65 78 20 3d 20 70 50 72 ree.pIndex = pPr
2a970 6f 62 65 3b 0a 20 20 20 20 62 20 3d 20 69 6e 64 obe;. b = ind
2a980 65 78 4d 69 67 68 74 48 65 6c 70 57 69 74 68 4f exMightHelpWithO
2a990 72 64 65 72 42 79 28 70 42 75 69 6c 64 65 72 2c rderBy(pBuilder,
2a9a0 20 70 50 72 6f 62 65 2c 20 70 53 72 63 2d 3e 69 pProbe, pSrc->i
2a9b0 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 2f 2a 20 Cursor);. /*
2a9c0 54 68 65 20 4f 4e 45 50 41 53 53 5f 44 45 53 49 The ONEPASS_DESI
2a9d0 52 45 44 20 66 6c 61 67 73 20 6e 65 76 65 72 20 RED flags never
2a9e0 6f 63 63 75 72 73 20 74 6f 67 65 74 68 65 72 20 occurs together
2a9f0 77 69 74 68 20 4f 52 44 45 52 20 42 59 20 2a 2f with ORDER BY */
2aa00 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 57 . assert( (pW
2aa10 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 Info->wctrlFlags
2aa20 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 & WHERE_ONEPASS
2aa30 5f 44 45 53 49 52 45 44 29 3d 3d 30 20 7c 7c 20 _DESIRED)==0 ||
2aa40 62 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 b==0 );. if(
2aa50 70 50 72 6f 62 65 2d 3e 74 6e 75 6d 3c 3d 30 20 pProbe->tnum<=0
2aa60 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 ){. /* Inte
2aa70 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 20 ger primary key
2aa80 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 70 index */. p
2aa90 4e 65 77 2d 3e 77 73 46 6c 61 67 73 20 3d 20 57 New->wsFlags = W
2aaa0 48 45 52 45 5f 49 50 4b 3b 0a 0a 20 20 20 20 20 HERE_IPK;..
2aab0 20 2f 2a 20 46 75 6c 6c 20 74 61 62 6c 65 20 73 /* Full table s
2aac0 63 61 6e 20 2a 2f 0a 20 20 20 20 20 20 70 4e 65 can */. pNe
2aad0 77 2d 3e 69 53 6f 72 74 49 64 78 20 3d 20 62 20 w->iSortIdx = b
2aae0 3f 20 69 53 6f 72 74 49 64 78 20 3a 20 30 3b 0a ? iSortIdx : 0;.
2aaf0 20 20 20 20 20 20 2f 2a 20 54 55 4e 49 4e 47 3a /* TUNING:
2ab00 20 43 6f 73 74 20 6f 66 20 66 75 6c 6c 20 74 61 Cost of full ta
2ab10 62 6c 65 20 73 63 61 6e 20 69 73 20 33 2a 28 4e ble scan is 3*(N
2ab20 20 2b 20 6c 6f 67 32 28 4e 29 29 2e 0a 20 20 20 + log2(N))..
2ab30 20 20 20 2a 2a 20 20 2b 20 20 54 68 65 20 65 78 ** + The ex
2ab40 74 72 61 20 33 20 66 61 63 74 6f 72 20 69 73 20 tra 3 factor is
2ab50 74 6f 20 65 6e 63 6f 75 72 61 67 65 20 74 68 65 to encourage the
2ab60 20 75 73 65 20 6f 66 20 69 6e 64 65 78 65 64 20 use of indexed
2ab70 6c 6f 6f 6b 75 70 73 0a 20 20 20 20 20 20 2a 2a lookups. **
2ab80 20 20 20 20 20 6f 76 65 72 20 66 75 6c 6c 20 73 over full s
2ab90 63 61 6e 73 2e 20 20 46 49 58 4d 45 20 2a 2f 0a cans. FIXME */.
2aba0 20 20 20 20 20 20 70 4e 65 77 2d 3e 72 52 75 6e pNew->rRun
2abb0 20 3d 20 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 = sqlite3LogEst
2abc0 41 64 64 28 72 53 69 7a 65 2c 72 4c 6f 67 53 69 Add(rSize,rLogSi
2abd0 7a 65 29 20 2b 20 31 36 3b 0a 20 20 20 20 20 20 ze) + 16;.
2abe0 77 68 65 72 65 4c 6f 6f 70 4f 75 74 70 75 74 41 whereLoopOutputA
2abf0 64 6a 75 73 74 28 70 57 43 2c 20 70 4e 65 77 29 djust(pWC, pNew)
2ac00 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 ;. rc = whe
2ac10 72 65 4c 6f 6f 70 49 6e 73 65 72 74 28 70 42 75 reLoopInsert(pBu
2ac20 69 6c 64 65 72 2c 20 70 4e 65 77 29 3b 0a 20 20 ilder, pNew);.
2ac30 20 20 20 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 3d pNew->nOut =
2ac40 20 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 rSize;. if
2ac50 28 20 72 63 20 29 20 62 72 65 61 6b 3b 0a 20 20 ( rc ) break;.
2ac60 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 42 }else{. B
2ac70 69 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d itmask m = pSrc-
2ac80 3e 63 6f 6c 55 73 65 64 20 26 20 7e 63 6f 6c 75 >colUsed & ~colu
2ac90 6d 6e 73 49 6e 49 6e 64 65 78 28 70 50 72 6f 62 mnsInIndex(pProb
2aca0 65 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e e);. pNew->
2acb0 77 73 46 6c 61 67 73 20 3d 20 28 6d 3d 3d 30 29 wsFlags = (m==0)
2acc0 20 3f 20 28 57 48 45 52 45 5f 49 44 58 5f 4f 4e ? (WHERE_IDX_ON
2acd0 4c 59 7c 57 48 45 52 45 5f 49 4e 44 45 58 45 44 LY|WHERE_INDEXED
2ace0 29 20 3a 20 57 48 45 52 45 5f 49 4e 44 45 58 45 ) : WHERE_INDEXE
2acf0 44 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46 75 6c D;.. /* Ful
2ad00 6c 20 73 63 61 6e 20 76 69 61 20 69 6e 64 65 78 l scan via index
2ad10 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 0a */. if( b.
2ad20 20 20 20 20 20 20 20 7c 7c 20 28 20 6d 3d 3d 30 || ( m==0
2ad30 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50 72 . && pPr
2ad40 6f 62 65 2d 3e 62 55 6e 6f 72 64 65 72 65 64 3d obe->bUnordered=
2ad50 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 =0. && p
2ad60 50 72 6f 62 65 2d 3e 73 7a 49 64 78 52 6f 77 3c Probe->szIdxRow<
2ad70 70 54 61 62 2d 3e 73 7a 54 61 62 52 6f 77 0a 20 pTab->szTabRow.
2ad80 20 20 20 20 20 20 20 20 26 26 20 28 70 57 49 6e && (pWIn
2ad90 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 fo->wctrlFlags &
2ada0 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 WHERE_ONEPASS_D
2adb0 45 53 49 52 45 44 29 3d 3d 30 0a 20 20 20 20 20 ESIRED)==0.
2adc0 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c && sqlite3Gl
2add0 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 55 73 65 43 obalConfig.bUseC
2ade0 69 73 0a 20 20 20 20 20 20 20 20 20 26 26 20 4f is. && O
2adf0 70 74 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c ptimizationEnabl
2ae00 65 64 28 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 ed(pWInfo->pPars
2ae10 65 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 43 6f e->db, SQLITE_Co
2ae20 76 65 72 49 64 78 53 63 61 6e 29 0a 20 20 20 20 verIdxScan).
2ae30 20 20 20 20 20 20 29 0a 20 20 20 20 20 20 29 7b ). ){
2ae40 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 . pNew->i
2ae50 53 6f 72 74 49 64 78 20 3d 20 62 20 3f 20 69 53 SortIdx = b ? iS
2ae60 6f 72 74 49 64 78 20 3a 20 30 3b 0a 20 20 20 20 ortIdx : 0;.
2ae70 20 20 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a if( m==0 ){.
2ae80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 55 4e /* TUN
2ae90 49 4e 47 3a 20 43 6f 73 74 20 6f 66 20 61 20 63 ING: Cost of a c
2aea0 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 20 73 63 overing index sc
2aeb0 61 6e 20 69 73 20 4b 2a 28 4e 20 2b 20 6c 6f 67 an is K*(N + log
2aec0 32 28 4e 29 29 2e 0a 20 20 20 20 20 20 20 20 20 2(N))..
2aed0 20 2a 2a 20 20 2b 20 20 54 68 65 20 65 78 74 72 ** + The extr
2aee0 61 20 66 61 63 74 6f 72 20 4b 20 6f 66 20 62 65 a factor K of be
2aef0 74 77 65 65 6e 20 31 2e 31 20 61 6e 64 20 33 2e tween 1.1 and 3.
2af00 30 20 74 68 61 74 20 64 65 70 65 6e 64 73 0a 20 0 that depends.
2af10 20 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 **
2af20 6f 6e 20 74 68 65 20 72 65 6c 61 74 69 76 65 20 on the relative
2af30 73 69 7a 65 73 20 6f 66 20 74 68 65 20 74 61 62 sizes of the tab
2af40 6c 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 le and the index
2af50 2e 20 20 4b 0a 20 20 20 20 20 20 20 20 20 20 2a . K. *
2af60 2a 20 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 * is smaller
2af70 20 66 6f 72 20 73 6d 61 6c 6c 65 72 20 69 6e 64 for smaller ind
2af80 69 63 65 73 2c 20 74 68 75 73 20 66 61 76 6f 72 ices, thus favor
2af90 69 6e 67 20 74 68 65 6d 2e 0a 20 20 20 20 20 20 ing them..
2afa0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 */.
2afb0 20 70 4e 65 77 2d 3e 72 52 75 6e 20 3d 20 73 71 pNew->rRun = sq
2afc0 6c 69 74 65 33 4c 6f 67 45 73 74 41 64 64 28 72 lite3LogEstAdd(r
2afd0 53 69 7a 65 2c 72 4c 6f 67 53 69 7a 65 29 20 2b Size,rLogSize) +
2afe0 20 31 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 1 +.
2aff0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 31 35 (15
2b000 2a 70 50 72 6f 62 65 2d 3e 73 7a 49 64 78 52 6f *pProbe->szIdxRo
2b010 77 29 2f 70 54 61 62 2d 3e 73 7a 54 61 62 52 6f w)/pTab->szTabRo
2b020 77 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 w;. }else
2b030 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 {. asse
2b040 72 74 28 20 62 21 3d 30 20 29 3b 20 0a 20 20 20 rt( b!=0 ); .
2b050 20 20 20 20 20 20 20 2f 2a 20 54 55 4e 49 4e 47 /* TUNING
2b060 3a 20 43 6f 73 74 20 6f 66 20 73 63 61 6e 6e 69 : Cost of scanni
2b070 6e 67 20 61 20 6e 6f 6e 2d 63 6f 76 65 72 69 6e ng a non-coverin
2b080 67 20 69 6e 64 65 78 20 69 73 20 28 4e 2b 31 29 g index is (N+1)
2b090 2a 6c 6f 67 32 28 4e 29 0a 20 20 20 20 20 20 20 *log2(N).
2b0a0 20 20 20 2a 2a 20 77 68 69 63 68 20 77 65 20 77 ** which we w
2b0b0 69 6c 6c 20 73 69 6d 70 6c 69 66 79 20 74 6f 20 ill simplify to
2b0c0 6a 75 73 74 20 4e 2a 6c 6f 67 32 28 4e 29 20 2a just N*log2(N) *
2b0d0 2f 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77 /. pNew
2b0e0 2d 3e 72 52 75 6e 20 3d 20 72 53 69 7a 65 20 2b ->rRun = rSize +
2b0f0 20 72 4c 6f 67 53 69 7a 65 3b 0a 20 20 20 20 20 rLogSize;.
2b100 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68 65 }. whe
2b110 72 65 4c 6f 6f 70 4f 75 74 70 75 74 41 64 6a 75 reLoopOutputAdju
2b120 73 74 28 70 57 43 2c 20 70 4e 65 77 29 3b 0a 20 st(pWC, pNew);.
2b130 20 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 rc = wher
2b140 65 4c 6f 6f 70 49 6e 73 65 72 74 28 70 42 75 69 eLoopInsert(pBui
2b150 6c 64 65 72 2c 20 70 4e 65 77 29 3b 0a 20 20 20 lder, pNew);.
2b160 20 20 20 20 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 pNew->nOut
2b170 3d 20 72 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 = rSize;.
2b180 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b if( rc ) break;
2b190 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a . }. }..
2b1a0 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 4c 6f rc = whereLo
2b1b0 6f 70 41 64 64 42 74 72 65 65 49 6e 64 65 78 28 opAddBtreeIndex(
2b1c0 70 42 75 69 6c 64 65 72 2c 20 70 53 72 63 2c 20 pBuilder, pSrc,
2b1d0 70 50 72 6f 62 65 2c 20 30 29 3b 0a 23 69 66 64 pProbe, 0);.#ifd
2b1e0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 ef SQLITE_ENABLE
2b1f0 5f 53 54 41 54 33 5f 4f 52 5f 53 54 41 54 34 0a _STAT3_OR_STAT4.
2b200 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 34 sqlite3Stat4
2b210 50 72 6f 62 65 46 72 65 65 28 70 42 75 69 6c 64 ProbeFree(pBuild
2b220 65 72 2d 3e 70 52 65 63 29 3b 0a 20 20 20 20 70 er->pRec);. p
2b230 42 75 69 6c 64 65 72 2d 3e 6e 52 65 63 56 61 6c Builder->nRecVal
2b240 69 64 20 3d 20 30 3b 0a 20 20 20 20 70 42 75 69 id = 0;. pBui
2b250 6c 64 65 72 2d 3e 70 52 65 63 20 3d 20 30 3b 0a lder->pRec = 0;.
2b260 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 #endif.. /* I
2b270 66 20 74 68 65 72 65 20 77 61 73 20 61 6e 20 49 f there was an I
2b280 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 NDEXED BY clause
2b290 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 61 74 , then only that
2b2a0 20 6f 6e 65 20 69 6e 64 65 78 20 69 73 0a 20 20 one index is.
2b2b0 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 2e ** considered.
2b2c0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 72 63 */. if( pSrc
2b2d0 2d 3e 70 49 6e 64 65 78 20 29 20 62 72 65 61 6b ->pIndex ) break
2b2e0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 ;. }. return r
2b2f0 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 c;.}..#ifndef SQ
2b300 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 LITE_OMIT_VIRTUA
2b310 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 41 64 64 LTABLE./*.** Add
2b320 20 61 6c 6c 20 57 68 65 72 65 4c 6f 6f 70 20 6f all WhereLoop o
2b330 62 6a 65 63 74 73 20 66 6f 72 20 61 20 74 61 62 bjects for a tab
2b340 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 20 69 le of the join i
2b350 64 65 6e 74 69 66 69 65 64 20 62 79 0a 2a 2a 20 dentified by.**
2b360 70 42 75 69 6c 64 65 72 2d 3e 70 4e 65 77 2d 3e pBuilder->pNew->
2b370 69 54 61 62 2e 20 20 54 68 61 74 20 74 61 62 6c iTab. That tabl
2b380 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 e is guaranteed
2b390 74 6f 20 62 65 20 61 20 76 69 72 74 75 61 6c 20 to be a virtual
2b3a0 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 table..*/.static
2b3b0 20 69 6e 74 20 77 68 65 72 65 4c 6f 6f 70 41 64 int whereLoopAd
2b3c0 64 56 69 72 74 75 61 6c 28 0a 20 20 57 68 65 72 dVirtual(. Wher
2b3d0 65 4c 6f 6f 70 42 75 69 6c 64 65 72 20 2a 70 42 eLoopBuilder *pB
2b3e0 75 69 6c 64 65 72 20 20 20 2f 2a 20 57 48 45 52 uilder /* WHER
2b3f0 45 20 63 6c 61 75 73 65 20 69 6e 66 6f 72 6d 61 E clause informa
2b400 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 tion */.){. Whe
2b410 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 reInfo *pWInfo;
2b420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 /* WHE
2b430 52 45 20 61 6e 61 6c 79 73 69 73 20 63 6f 6e 74 RE analysis cont
2b440 65 78 74 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a ext */. Parse *
2b450 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 pParse;
2b460 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 /* The par
2b470 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
2b480 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 WhereClause *p
2b490 57 43 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f WC; /
2b4a0 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 * The WHERE clau
2b4b0 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 se */. struct S
2b4c0 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 rcList_item *pSr
2b4d0 63 3b 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d c; /* The FROM
2b4e0 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20 clause term to
2b4f0 73 65 61 72 63 68 20 2a 2f 0a 20 20 54 61 62 6c search */. Tabl
2b500 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 e *pTab;. sqlit
2b510 65 33 20 2a 64 62 3b 0a 20 20 73 71 6c 69 74 65 e3 *db;. sqlite
2b520 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 3_index_info *pI
2b530 64 78 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 dxInfo;. struct
2b540 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 sqlite3_index_c
2b550 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49 64 78 43 onstraint *pIdxC
2b560 6f 6e 73 3b 0a 20 20 73 74 72 75 63 74 20 73 71 ons;. struct sq
2b570 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 lite3_index_cons
2b580 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70 55 traint_usage *pU
2b590 73 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65 72 sage;. WhereTer
2b5a0 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 m *pTerm;. int
2b5b0 69 2c 20 6a 3b 0a 20 20 69 6e 74 20 69 54 65 72 i, j;. int iTer
2b5c0 6d 2c 20 6d 78 54 65 72 6d 3b 0a 20 20 69 6e 74 m, mxTerm;. int
2b5d0 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 nConstraint;.
2b5e0 69 6e 74 20 73 65 65 6e 49 6e 20 3d 20 30 3b 20 int seenIn = 0;
2b5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
2b600 54 72 75 65 20 69 66 20 61 6e 20 49 4e 20 6f 70 True if an IN op
2b610 65 72 61 74 6f 72 20 69 73 20 73 65 65 6e 20 2a erator is seen *
2b620 2f 0a 20 20 69 6e 74 20 73 65 65 6e 56 61 72 20 /. int seenVar
2b630 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 = 0;
2b640 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 6e 6f /* True if a no
2b650 6e 2d 63 6f 6e 73 74 61 6e 74 20 63 6f 6e 73 74 n-constant const
2b660 72 61 69 6e 74 20 69 73 20 73 65 65 6e 20 2a 2f raint is seen */
2b670 0a 20 20 69 6e 74 20 69 50 68 61 73 65 3b 20 20 . int iPhase;
2b680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2b690 2f 2a 20 30 3a 20 63 6f 6e 73 74 20 77 2f 6f 20 /* 0: const w/o
2b6a0 49 4e 2c 20 31 3a 20 63 6f 6e 73 74 2c 20 32 3a IN, 1: const, 2:
2b6b0 20 6e 6f 20 49 4e 2c 20 20 32 3a 20 49 4e 20 2a no IN, 2: IN *
2b6c0 2f 0a 20 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 /. WhereLoop *p
2b6d0 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 New;. int rc =
2b6e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 70 57 SQLITE_OK;.. pW
2b6f0 49 6e 66 6f 20 3d 20 70 42 75 69 6c 64 65 72 2d Info = pBuilder-
2b700 3e 70 57 49 6e 66 6f 3b 0a 20 20 70 50 61 72 73 >pWInfo;. pPars
2b710 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 e = pWInfo->pPar
2b720 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 se;. db = pPars
2b730 65 2d 3e 64 62 3b 0a 20 20 70 57 43 20 3d 20 70 e->db;. pWC = p
2b740 42 75 69 6c 64 65 72 2d 3e 70 57 43 3b 0a 20 20 Builder->pWC;.
2b750 70 4e 65 77 20 3d 20 70 42 75 69 6c 64 65 72 2d pNew = pBuilder-
2b760 3e 70 4e 65 77 3b 0a 20 20 70 53 72 63 20 3d 20 >pNew;. pSrc =
2b770 26 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 &pWInfo->pTabLis
2b780 74 2d 3e 61 5b 70 4e 65 77 2d 3e 69 54 61 62 5d t->a[pNew->iTab]
2b790 3b 0a 20 20 70 54 61 62 20 3d 20 70 53 72 63 2d ;. pTab = pSrc-
2b7a0 3e 70 54 61 62 3b 0a 20 20 61 73 73 65 72 74 28 >pTab;. assert(
2b7b0 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
2b7c0 20 29 3b 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d );. pIdxInfo =
2b7d0 20 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e allocateIndexIn
2b7e0 66 6f 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 fo(pParse, pWC,
2b7f0 70 53 72 63 2c 20 70 42 75 69 6c 64 65 72 2d 3e pSrc, pBuilder->
2b800 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 69 66 28 pOrderBy);. if(
2b810 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 20 72 pIdxInfo==0 ) r
2b820 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d eturn SQLITE_NOM
2b830 45 4d 3b 0a 20 20 70 4e 65 77 2d 3e 70 72 65 72 EM;. pNew->prer
2b840 65 71 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e eq = 0;. pNew->
2b850 72 53 65 74 75 70 20 3d 20 30 3b 0a 20 20 70 4e rSetup = 0;. pN
2b860 65 77 2d 3e 77 73 46 6c 61 67 73 20 3d 20 57 48 ew->wsFlags = WH
2b870 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 ERE_VIRTUALTABLE
2b880 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4c 54 65 72 6d ;. pNew->nLTerm
2b890 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 75 2e = 0;. pNew->u.
2b8a0 76 74 61 62 2e 6e 65 65 64 46 72 65 65 20 3d 20 vtab.needFree =
2b8b0 30 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 70 49 0;. pUsage = pI
2b8c0 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 dxInfo->aConstra
2b8d0 69 6e 74 55 73 61 67 65 3b 0a 20 20 6e 43 6f 6e intUsage;. nCon
2b8e0 73 74 72 61 69 6e 74 20 3d 20 70 49 64 78 49 6e straint = pIdxIn
2b8f0 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b fo->nConstraint;
2b900 0a 20 20 69 66 28 20 77 68 65 72 65 4c 6f 6f 70 . if( whereLoop
2b910 52 65 73 69 7a 65 28 64 62 2c 20 70 4e 65 77 2c Resize(db, pNew,
2b920 20 6e 43 6f 6e 73 74 72 61 69 6e 74 29 20 29 7b nConstraint) ){
2b930 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 . sqlite3DbFr
2b940 65 65 28 64 62 2c 20 70 49 64 78 49 6e 66 6f 29 ee(db, pIdxInfo)
2b950 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c ;. return SQL
2b960 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a ITE_NOMEM;. }..
2b970 20 20 66 6f 72 28 69 50 68 61 73 65 3d 30 3b 20 for(iPhase=0;
2b980 69 50 68 61 73 65 3c 3d 33 3b 20 69 50 68 61 73 iPhase<=3; iPhas
2b990 65 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 73 e++){. if( !s
2b9a0 65 65 6e 49 6e 20 26 26 20 28 69 50 68 61 73 65 eenIn && (iPhase
2b9b0 26 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 &1)!=0 ){.
2b9c0 69 50 68 61 73 65 2b 2b 3b 0a 20 20 20 20 20 20 iPhase++;.
2b9d0 69 66 28 20 69 50 68 61 73 65 3e 33 20 29 20 62 if( iPhase>3 ) b
2b9e0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
2b9f0 69 66 28 20 21 73 65 65 6e 56 61 72 20 26 26 20 if( !seenVar &&
2ba00 69 50 68 61 73 65 3e 31 20 29 20 62 72 65 61 6b iPhase>1 ) break
2ba10 3b 0a 20 20 20 20 70 49 64 78 43 6f 6e 73 20 3d ;. pIdxCons =
2ba20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 *(struct sqlite
2ba30 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 3_index_constrai
2ba40 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e nt**)&pIdxInfo->
2ba50 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20 aConstraint;.
2ba60 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 for(i=0; i<pIdx
2ba70 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e Info->nConstrain
2ba80 74 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 t; i++, pIdxCons
2ba90 2b 2b 29 7b 0a 20 20 20 20 20 20 6a 20 3d 20 70 ++){. j = p
2baa0 49 64 78 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66 IdxCons->iTermOf
2bab0 66 73 65 74 3b 0a 20 20 20 20 20 20 70 54 65 72 fset;. pTer
2bac0 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b 0a m = &pWC->a[j];.
2bad0 20 20 20 20 20 20 73 77 69 74 63 68 28 20 69 50 switch( iP
2bae0 68 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 hase ){.
2baf0 63 61 73 65 20 30 3a 20 20 20 20 2f 2a 20 43 6f case 0: /* Co
2bb00 6e 73 74 61 6e 74 73 20 77 69 74 68 6f 75 74 20 nstants without
2bb10 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 IN operator */.
2bb20 20 20 20 20 20 20 20 20 20 70 49 64 78 43 6f 6e pIdxCon
2bb30 73 2d 3e 75 73 61 62 6c 65 20 3d 20 30 3b 0a 20 s->usable = 0;.
2bb40 20 20 20 20 20 20 20 20 20 69 66 28 20 28 70 54 if( (pT
2bb50 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 erm->eOperator &
2bb60 20 57 4f 5f 49 4e 29 21 3d 30 20 29 7b 0a 20 20 WO_IN)!=0 ){.
2bb70 20 20 20 20 20 20 20 20 20 20 73 65 65 6e 49 6e seenIn
2bb80 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 = 1;.
2bb90 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
2bba0 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 pTerm->prereqRig
2bbb0 68 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 ht!=0 ){.
2bbc0 20 20 20 20 20 73 65 65 6e 56 61 72 20 3d 20 31 seenVar = 1
2bbd0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 ;. }els
2bbe0 65 20 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f e if( (pTerm->eO
2bbf0 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 29 perator & WO_IN)
2bc00 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 ==0 ){.
2bc10 20 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73 61 pIdxCons->usa
2bc20 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 ble = 1;.
2bc30 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 }. b
2bc40 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 reak;. ca
2bc50 73 65 20 31 3a 20 20 20 20 2f 2a 20 43 6f 6e 73 se 1: /* Cons
2bc60 74 61 6e 74 73 20 77 69 74 68 20 49 4e 20 6f 70 tants with IN op
2bc70 65 72 61 74 6f 72 73 20 2a 2f 0a 20 20 20 20 20 erators */.
2bc80 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65 65 assert( see
2bc90 6e 49 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 20 nIn );.
2bca0 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c pIdxCons->usabl
2bcb0 65 20 3d 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 e = (pTerm->prer
2bcc0 65 71 52 69 67 68 74 3d 3d 30 29 3b 0a 20 20 20 eqRight==0);.
2bcd0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
2bce0 20 20 20 20 20 20 63 61 73 65 20 32 3a 20 20 20 case 2:
2bcf0 20 2f 2a 20 56 61 72 69 61 62 6c 65 73 20 77 69 /* Variables wi
2bd00 74 68 6f 75 74 20 49 4e 20 2a 2f 0a 20 20 20 20 thout IN */.
2bd10 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 65 assert( se
2bd20 65 6e 56 61 72 20 29 3b 0a 20 20 20 20 20 20 20 enVar );.
2bd30 20 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73 61 pIdxCons->usa
2bd40 62 6c 65 20 3d 20 28 70 54 65 72 6d 2d 3e 65 4f ble = (pTerm->eO
2bd50 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 29 perator & WO_IN)
2bd60 3d 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 ==0;. b
2bd70 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 reak;. de
2bd80 66 61 75 6c 74 3a 20 20 20 2f 2a 20 56 61 72 69 fault: /* Vari
2bd90 61 62 6c 65 73 20 77 69 74 68 20 49 4e 20 2a 2f ables with IN */
2bda0 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 . asser
2bdb0 74 28 20 73 65 65 6e 56 61 72 20 26 26 20 73 65 t( seenVar && se
2bdc0 65 6e 49 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 enIn );.
2bdd0 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 pIdxCons->usab
2bde0 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 le = 1;.
2bdf0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d break;. }
2be00 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 . }. memse
2be10 74 28 70 55 73 61 67 65 2c 20 30 2c 20 73 69 7a t(pUsage, 0, siz
2be20 65 6f 66 28 70 55 73 61 67 65 5b 30 5d 29 2a 70 eof(pUsage[0])*p
2be30 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 IdxInfo->nConstr
2be40 61 69 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 70 aint);. if( p
2be50 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 IdxInfo->needToF
2be60 72 65 65 49 64 78 53 74 72 20 29 20 73 71 6c 69 reeIdxStr ) sqli
2be70 74 65 33 5f 66 72 65 65 28 70 49 64 78 49 6e 66 te3_free(pIdxInf
2be80 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20 o->idxStr);.
2be90 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 pIdxInfo->idxStr
2bea0 20 3d 20 30 3b 0a 20 20 20 20 70 49 64 78 49 6e = 0;. pIdxIn
2beb0 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 30 3b 0a fo->idxNum = 0;.
2bec0 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 pIdxInfo->ne
2bed0 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d edToFreeIdxStr =
2bee0 20 30 3b 0a 20 20 20 20 70 49 64 78 49 6e 66 6f 0;. pIdxInfo
2bef0 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 ->orderByConsume
2bf00 64 20 3d 20 30 3b 0a 20 20 20 20 70 49 64 78 49 d = 0;. pIdxI
2bf10 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f nfo->estimatedCo
2bf20 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f st = SQLITE_BIG_
2bf30 44 42 4c 20 2f 20 28 64 6f 75 62 6c 65 29 32 3b DBL / (double)2;
2bf40 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62 42 65 . rc = vtabBe
2bf50 73 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 stIndex(pParse,
2bf60 70 54 61 62 2c 20 70 49 64 78 49 6e 66 6f 29 3b pTab, pIdxInfo);
2bf70 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f . if( rc ) go
2bf80 74 6f 20 77 68 65 72 65 4c 6f 6f 70 41 64 64 56 to whereLoopAddV
2bf90 74 61 62 5f 65 78 69 74 3b 0a 20 20 20 20 70 49 tab_exit;. pI
2bfa0 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 dxCons = *(struc
2bfb0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f t sqlite3_index_
2bfc0 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 constraint**)&pI
2bfd0 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 dxInfo->aConstra
2bfe0 69 6e 74 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 int;. pNew->p
2bff0 72 65 72 65 71 20 3d 20 30 3b 0a 20 20 20 20 6d rereq = 0;. m
2c000 78 54 65 72 6d 20 3d 20 2d 31 3b 0a 20 20 20 20 xTerm = -1;.
2c010 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4c assert( pNew->nL
2c020 53 6c 6f 74 3e 3d 6e 43 6f 6e 73 74 72 61 69 6e Slot>=nConstrain
2c030 74 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 t );. for(i=0
2c040 3b 20 69 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b ; i<nConstraint;
2c050 20 69 2b 2b 29 20 70 4e 65 77 2d 3e 61 4c 54 65 i++) pNew->aLTe
2c060 72 6d 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 70 rm[i] = 0;. p
2c070 4e 65 77 2d 3e 75 2e 76 74 61 62 2e 6f 6d 69 74 New->u.vtab.omit
2c080 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 66 6f Mask = 0;. fo
2c090 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6e 73 74 72 r(i=0; i<nConstr
2c0a0 61 69 6e 74 3b 20 69 2b 2b 2c 20 70 49 64 78 43 aint; i++, pIdxC
2c0b0 6f 6e 73 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 ons++){. if
2c0c0 28 20 28 69 54 65 72 6d 20 3d 20 70 55 73 61 67 ( (iTerm = pUsag
2c0d0 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 20 2d e[i].argvIndex -
2c0e0 20 31 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 1)>=0 ){.
2c0f0 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e 73 2d 3e j = pIdxCons->
2c100 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20 iTermOffset;.
2c110 20 20 20 20 20 69 66 28 20 69 54 65 72 6d 3e 3d if( iTerm>=
2c120 6e 43 6f 6e 73 74 72 61 69 6e 74 0a 20 20 20 20 nConstraint.
2c130 20 20 20 20 20 7c 7c 20 6a 3c 30 0a 20 20 20 20 || j<0.
2c140 20 20 20 20 20 7c 7c 20 6a 3e 3d 70 57 43 2d 3e || j>=pWC->
2c150 6e 54 65 72 6d 0a 20 20 20 20 20 20 20 20 20 7c nTerm. |
2c160 7c 20 70 4e 65 77 2d 3e 61 4c 54 65 72 6d 5b 69 | pNew->aLTerm[i
2c170 54 65 72 6d 5d 21 3d 30 0a 20 20 20 20 20 20 20 Term]!=0.
2c180 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 ){. rc
2c190 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b = SQLITE_ERROR;
2c1a0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
2c1b0 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
2c1c0 65 2c 20 22 25 73 2e 78 42 65 73 74 49 6e 64 65 e, "%s.xBestInde
2c1d0 78 28 29 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 22 x() malfunction"
2c1e0 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
2c1f0 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 77 goto w
2c200 68 65 72 65 4c 6f 6f 70 41 64 64 56 74 61 62 5f hereLoopAddVtab_
2c210 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a exit;. }.
2c220 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
2c230 28 20 69 54 65 72 6d 3d 3d 6e 43 6f 6e 73 74 72 ( iTerm==nConstr
2c240 61 69 6e 74 2d 31 20 29 3b 0a 20 20 20 20 20 20 aint-1 );.
2c250 20 20 74 65 73 74 63 61 73 65 28 20 6a 3d 3d 30 testcase( j==0
2c260 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 );. test
2c270 63 61 73 65 28 20 6a 3d 3d 70 57 43 2d 3e 6e 54 case( j==pWC->nT
2c280 65 72 6d 2d 31 20 29 3b 0a 20 20 20 20 20 20 20 erm-1 );.
2c290 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 pTerm = &pWC->a
2c2a0 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 [j];. pNe
2c2b0 77 2d 3e 70 72 65 72 65 71 20 7c 3d 20 70 54 65 w->prereq |= pTe
2c2c0 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 3b rm->prereqRight;
2c2d0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
2c2e0 20 69 54 65 72 6d 3c 70 4e 65 77 2d 3e 6e 4c 53 iTerm<pNew->nLS
2c2f0 6c 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 lot );. p
2c300 4e 65 77 2d 3e 61 4c 54 65 72 6d 5b 69 54 65 72 New->aLTerm[iTer
2c310 6d 5d 20 3d 20 70 54 65 72 6d 3b 0a 20 20 20 20 m] = pTerm;.
2c320 20 20 20 20 69 66 28 20 69 54 65 72 6d 3e 6d 78 if( iTerm>mx
2c330 54 65 72 6d 20 29 20 6d 78 54 65 72 6d 20 3d 20 Term ) mxTerm =
2c340 69 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 74 iTerm;. t
2c350 65 73 74 63 61 73 65 28 20 69 54 65 72 6d 3d 3d estcase( iTerm==
2c360 31 35 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 15 );. te
2c370 73 74 63 61 73 65 28 20 69 54 65 72 6d 3d 3d 31 stcase( iTerm==1
2c380 36 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 6 );. if(
2c390 20 69 54 65 72 6d 3c 31 36 20 26 26 20 70 55 73 iTerm<16 && pUs
2c3a0 61 67 65 5b 69 5d 2e 6f 6d 69 74 20 29 20 70 4e age[i].omit ) pN
2c3b0 65 77 2d 3e 75 2e 76 74 61 62 2e 6f 6d 69 74 4d ew->u.vtab.omitM
2c3c0 61 73 6b 20 7c 3d 20 31 3c 3c 69 54 65 72 6d 3b ask |= 1<<iTerm;
2c3d0 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 54 . if( (pT
2c3e0 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 erm->eOperator &
2c3f0 20 57 4f 5f 49 4e 29 21 3d 30 20 29 7b 0a 20 20 WO_IN)!=0 ){.
2c400 20 20 20 20 20 20 20 20 69 66 28 20 70 55 73 61 if( pUsa
2c410 67 65 5b 69 5d 2e 6f 6d 69 74 3d 3d 30 20 29 7b ge[i].omit==0 ){
2c420 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 . /*
2c430 44 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 Do not attempt t
2c440 6f 20 75 73 65 20 61 6e 20 49 4e 20 63 6f 6e 73 o use an IN cons
2c450 74 72 61 69 6e 74 20 69 66 20 74 68 65 20 76 69 traint if the vi
2c460 72 74 75 61 6c 20 74 61 62 6c 65 0a 20 20 20 20 rtual table.
2c470 20 20 20 20 20 20 20 20 2a 2a 20 73 61 79 73 20 ** says
2c480 74 68 61 74 20 74 68 65 20 65 71 75 69 76 61 6c that the equival
2c490 65 6e 74 20 45 51 20 63 6f 6e 73 74 72 61 69 6e ent EQ constrain
2c4a0 74 20 63 61 6e 6e 6f 74 20 62 65 20 73 61 66 65 t cannot be safe
2c4b0 6c 79 20 6f 6d 69 74 74 65 64 2e 0a 20 20 20 20 ly omitted..
2c4c0 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 77 65 ** If we
2c4d0 20 64 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 75 do attempt to u
2c4e0 73 65 20 73 75 63 68 20 61 20 63 6f 6e 73 74 72 se such a constr
2c4f0 61 69 6e 74 2c 20 73 6f 6d 65 20 72 6f 77 73 20 aint, some rows
2c500 6d 69 67 68 74 20 62 65 0a 20 20 20 20 20 20 20 might be.
2c510 20 20 20 20 20 2a 2a 20 72 65 70 65 61 74 65 64 ** repeated
2c520 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 2e 20 in the output.
2c530 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 */. b
2c540 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
2c550 7d 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 }. /* A
2c560 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 virtual table t
2c570 68 61 74 20 69 73 20 63 6f 6e 73 74 72 61 69 6e hat is constrain
2c580 65 64 20 62 79 20 61 6e 20 49 4e 20 63 6c 61 75 ed by an IN clau
2c590 73 65 20 6d 61 79 20 6e 6f 74 0a 20 20 20 20 20 se may not.
2c5a0 20 20 20 20 20 2a 2a 20 63 6f 6e 73 75 6d 65 20 ** consume
2c5b0 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
2c5c0 75 73 65 20 62 65 63 61 75 73 65 20 28 31 29 20 use because (1)
2c5d0 74 68 65 20 6f 72 64 65 72 20 6f 66 20 49 4e 20 the order of IN
2c5e0 74 65 72 6d 73 0a 20 20 20 20 20 20 20 20 20 20 terms.
2c5f0 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 ** is not necess
2c600 61 72 69 6c 79 20 72 65 6c 61 74 65 64 20 74 6f arily related to
2c610 20 74 68 65 20 6f 72 64 65 72 20 6f 66 20 6f 75 the order of ou
2c620 74 70 75 74 20 74 65 72 6d 73 20 61 6e 64 0a 20 tput terms and.
2c630 20 20 20 20 20 20 20 20 20 2a 2a 20 28 32 29 20 ** (2)
2c640 4d 75 6c 74 69 70 6c 65 20 6f 75 74 70 75 74 73 Multiple outputs
2c650 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 49 from a single I
2c660 4e 20 76 61 6c 75 65 20 77 69 6c 6c 20 6e 6f 74 N value will not
2c670 20 6d 65 72 67 65 0a 20 20 20 20 20 20 20 20 20 merge.
2c680 20 2a 2a 20 74 6f 67 65 74 68 65 72 2e 20 20 2a ** together. *
2c690 2f 0a 20 20 20 20 20 20 20 20 20 20 70 49 64 78 /. pIdx
2c6a0 49 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e Info->orderByCon
2c6b0 73 75 6d 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 sumed = 0;.
2c6c0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
2c6d0 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 3d 6e 43 }. if( i>=nC
2c6e0 6f 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20 20 onstraint ){.
2c6f0 20 20 20 70 4e 65 77 2d 3e 6e 4c 54 65 72 6d 20 pNew->nLTerm
2c700 3d 20 6d 78 54 65 72 6d 2b 31 3b 0a 20 20 20 20 = mxTerm+1;.
2c710 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
2c720 6e 4c 54 65 72 6d 3c 3d 70 4e 65 77 2d 3e 6e 4c nLTerm<=pNew->nL
2c730 53 6c 6f 74 20 29 3b 0a 20 20 20 20 20 20 70 4e Slot );. pN
2c740 65 77 2d 3e 75 2e 76 74 61 62 2e 69 64 78 4e 75 ew->u.vtab.idxNu
2c750 6d 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 m = pIdxInfo->id
2c760 78 4e 75 6d 3b 0a 20 20 20 20 20 20 70 4e 65 77 xNum;. pNew
2c770 2d 3e 75 2e 76 74 61 62 2e 6e 65 65 64 46 72 65 ->u.vtab.needFre
2c780 65 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 e = pIdxInfo->ne
2c790 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 0a edToFreeIdxStr;.
2c7a0 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e pIdxInfo->
2c7b0 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 needToFreeIdxStr
2c7c0 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4e 65 77 = 0;. pNew
2c7d0 2d 3e 75 2e 76 74 61 62 2e 69 64 78 53 74 72 20 ->u.vtab.idxStr
2c7e0 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 = pIdxInfo->idxS
2c7f0 74 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e tr;. pNew->
2c800 75 2e 76 74 61 62 2e 69 73 4f 72 64 65 72 65 64 u.vtab.isOrdered
2c810 20 3d 20 28 75 38 29 28 28 70 49 64 78 49 6e 66 = (u8)((pIdxInf
2c820 6f 2d 3e 6e 4f 72 64 65 72 42 79 21 3d 30 29 0a o->nOrderBy!=0).
2c830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2c850 20 20 20 20 20 26 26 20 70 49 64 78 49 6e 66 6f && pIdxInfo
2c860 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 ->orderByConsume
2c870 64 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e d);. pNew->
2c880 72 53 65 74 75 70 20 3d 20 30 3b 0a 20 20 20 20 rSetup = 0;.
2c890 20 20 70 4e 65 77 2d 3e 72 52 75 6e 20 3d 20 73 pNew->rRun = s
2c8a0 71 6c 69 74 65 33 4c 6f 67 45 73 74 46 72 6f 6d qlite3LogEstFrom
2c8b0 44 6f 75 62 6c 65 28 70 49 64 78 49 6e 66 6f 2d Double(pIdxInfo-
2c8c0 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 29 3b >estimatedCost);
2c8d0 0a 20 20 20 20 20 20 2f 2a 20 54 55 4e 49 4e 47 . /* TUNING
2c8e0 3a 20 45 76 65 72 79 20 76 69 72 74 75 61 6c 20 : Every virtual
2c8f0 74 61 62 6c 65 20 71 75 65 72 79 20 72 65 74 75 table query retu
2c900 72 6e 73 20 32 35 20 72 6f 77 73 20 2a 2f 0a 20 rns 25 rows */.
2c910 20 20 20 20 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 pNew->nOut
2c920 3d 20 34 36 3b 20 20 61 73 73 65 72 74 28 20 34 = 46; assert( 4
2c930 36 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 6==sqlite3LogEst
2c940 28 32 35 29 20 29 3b 0a 20 20 20 20 20 20 77 68 (25) );. wh
2c950 65 72 65 4c 6f 6f 70 49 6e 73 65 72 74 28 70 42 ereLoopInsert(pB
2c960 75 69 6c 64 65 72 2c 20 70 4e 65 77 29 3b 0a 20 uilder, pNew);.
2c970 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 75 if( pNew->u
2c980 2e 76 74 61 62 2e 6e 65 65 64 46 72 65 65 20 29 .vtab.needFree )
2c990 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
2c9a0 33 5f 66 72 65 65 28 70 4e 65 77 2d 3e 75 2e 76 3_free(pNew->u.v
2c9b0 74 61 62 2e 69 64 78 53 74 72 29 3b 0a 20 20 20 tab.idxStr);.
2c9c0 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 76 74 61 pNew->u.vta
2c9d0 62 2e 6e 65 65 64 46 72 65 65 20 3d 20 30 3b 0a b.needFree = 0;.
2c9e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
2c9f0 7d 20 20 0a 0a 77 68 65 72 65 4c 6f 6f 70 41 64 } ..whereLoopAd
2ca00 64 56 74 61 62 5f 65 78 69 74 3a 0a 20 20 69 66 dVtab_exit:. if
2ca10 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 ( pIdxInfo->need
2ca20 54 6f 46 72 65 65 49 64 78 53 74 72 20 29 20 73 ToFreeIdxStr ) s
2ca30 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49 64 78 qlite3_free(pIdx
2ca40 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 Info->idxStr);.
2ca50 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
2ca60 62 2c 20 70 49 64 78 49 6e 66 6f 29 3b 0a 20 20 b, pIdxInfo);.
2ca70 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e return rc;.}.#en
2ca80 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d dif /* SQLITE_OM
2ca90 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 IT_VIRTUALTABLE
2caa0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 57 68 */../*.** Add Wh
2cab0 65 72 65 4c 6f 6f 70 20 65 6e 74 72 69 65 73 20 ereLoop entries
2cac0 74 6f 20 68 61 6e 64 6c 65 20 4f 52 20 74 65 72 to handle OR ter
2cad0 6d 73 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20 ms. This works
2cae0 66 6f 72 20 65 69 74 68 65 72 0a 2a 2a 20 62 74 for either.** bt
2caf0 72 65 65 73 20 6f 72 20 76 69 72 74 75 61 6c 20 rees or virtual
2cb00 74 61 62 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 tables..*/.stati
2cb10 63 20 69 6e 74 20 77 68 65 72 65 4c 6f 6f 70 41 c int whereLoopA
2cb20 64 64 4f 72 28 57 68 65 72 65 4c 6f 6f 70 42 75 ddOr(WhereLoopBu
2cb30 69 6c 64 65 72 20 2a 70 42 75 69 6c 64 65 72 2c ilder *pBuilder,
2cb40 20 42 69 74 6d 61 73 6b 20 6d 45 78 74 72 61 29 Bitmask mExtra)
2cb50 7b 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 {. WhereInfo *p
2cb60 57 49 6e 66 6f 20 3d 20 70 42 75 69 6c 64 65 72 WInfo = pBuilder
2cb70 2d 3e 70 57 49 6e 66 6f 3b 0a 20 20 57 68 65 72 ->pWInfo;. Wher
2cb80 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 0a 20 20 eClause *pWC;.
2cb90 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4e 65 77 3b WhereLoop *pNew;
2cba0 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 . WhereTerm *pT
2cbb0 65 72 6d 2c 20 2a 70 57 43 45 6e 64 3b 0a 20 20 erm, *pWCEnd;.
2cbc0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
2cbd0 4f 4b 3b 0a 20 20 69 6e 74 20 69 43 75 72 3b 0a OK;. int iCur;.
2cbe0 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 74 65 WhereClause te
2cbf0 6d 70 57 43 3b 0a 20 20 57 68 65 72 65 4c 6f 6f mpWC;. WhereLoo
2cc00 70 42 75 69 6c 64 65 72 20 73 53 75 62 42 75 69 pBuilder sSubBui
2cc10 6c 64 3b 0a 20 20 57 68 65 72 65 4f 72 53 65 74 ld;. WhereOrSet
2cc20 20 73 53 75 6d 2c 20 73 43 75 72 2c 20 73 50 72 sSum, sCur, sPr
2cc30 65 76 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 ev;. struct Src
2cc40 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
2cc50 3b 0a 20 20 0a 20 20 70 57 43 20 3d 20 70 42 75 ;. . pWC = pBu
2cc60 69 6c 64 65 72 2d 3e 70 57 43 3b 0a 20 20 69 66 ilder->pWC;. if
2cc70 28 20 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 ( pWInfo->wctrlF
2cc80 6c 61 67 73 20 26 20 57 48 45 52 45 5f 41 4e 44 lags & WHERE_AND
2cc90 5f 4f 4e 4c 59 20 29 20 72 65 74 75 72 6e 20 53 _ONLY ) return S
2cca0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 57 43 45 QLITE_OK;. pWCE
2ccb0 6e 64 20 3d 20 70 57 43 2d 3e 61 20 2b 20 70 57 nd = pWC->a + pW
2ccc0 43 2d 3e 6e 54 65 72 6d 3b 0a 20 20 70 4e 65 77 C->nTerm;. pNew
2ccd0 20 3d 20 70 42 75 69 6c 64 65 72 2d 3e 70 4e 65 = pBuilder->pNe
2cce0 77 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 53 75 w;. memset(&sSu
2ccf0 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 53 75 m, 0, sizeof(sSu
2cd00 6d 29 29 3b 0a 20 20 70 49 74 65 6d 20 3d 20 70 m));. pItem = p
2cd10 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 2d WInfo->pTabList-
2cd20 3e 61 20 2b 20 70 4e 65 77 2d 3e 69 54 61 62 3b >a + pNew->iTab;
2cd30 0a 20 20 69 43 75 72 20 3d 20 70 49 74 65 6d 2d . iCur = pItem-
2cd40 3e 69 43 75 72 73 6f 72 3b 0a 0a 20 20 66 6f 72 >iCursor;.. for
2cd50 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 70 (pTerm=pWC->a; p
2cd60 54 65 72 6d 3c 70 57 43 45 6e 64 20 26 26 20 72 Term<pWCEnd && r
2cd70 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70 54 c==SQLITE_OK; pT
2cd80 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 erm++){. if(
2cd90 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f (pTerm->eOperato
2cda0 72 20 26 20 57 4f 5f 4f 52 29 21 3d 30 0a 20 20 r & WO_OR)!=0.
2cdb0 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 75 2e && (pTerm->u.
2cdc0 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 pOrInfo->indexab
2cdd0 6c 65 20 26 20 70 4e 65 77 2d 3e 6d 61 73 6b 53 le & pNew->maskS
2cde0 65 6c 66 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a elf)!=0 . ){.
2cdf0 20 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 WhereClaus
2ce00 65 20 2a 20 63 6f 6e 73 74 20 70 4f 72 57 43 20 e * const pOrWC
2ce10 3d 20 26 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 = &pTerm->u.pOrI
2ce20 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 57 nfo->wc;. W
2ce30 68 65 72 65 54 65 72 6d 20 2a 20 63 6f 6e 73 74 hereTerm * const
2ce40 20 70 4f 72 57 43 45 6e 64 20 3d 20 26 70 4f 72 pOrWCEnd = &pOr
2ce50 57 43 2d 3e 61 5b 70 4f 72 57 43 2d 3e 6e 54 65 WC->a[pOrWC->nTe
2ce60 72 6d 5d 3b 0a 20 20 20 20 20 20 57 68 65 72 65 rm];. Where
2ce70 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 0a 20 Term *pOrTerm;.
2ce80 20 20 20 20 20 69 6e 74 20 6f 6e 63 65 20 3d 20 int once =
2ce90 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 20 1;. int i,
2cea0 6a 3b 0a 20 20 20 20 0a 20 20 20 20 20 20 73 53 j;. . sS
2ceb0 75 62 42 75 69 6c 64 20 3d 20 2a 70 42 75 69 6c ubBuild = *pBuil
2cec0 64 65 72 3b 0a 20 20 20 20 20 20 73 53 75 62 42 der;. sSubB
2ced0 75 69 6c 64 2e 70 4f 72 64 65 72 42 79 20 3d 20 uild.pOrderBy =
2cee0 30 3b 0a 20 20 20 20 20 20 73 53 75 62 42 75 69 0;. sSubBui
2cef0 6c 64 2e 70 4f 72 53 65 74 20 3d 20 26 73 43 75 ld.pOrSet = &sCu
2cf00 72 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70 4f r;.. for(pO
2cf10 72 54 65 72 6d 3d 70 4f 72 57 43 2d 3e 61 3b 20 rTerm=pOrWC->a;
2cf20 70 4f 72 54 65 72 6d 3c 70 4f 72 57 43 45 6e 64 pOrTerm<pOrWCEnd
2cf30 3b 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 ; pOrTerm++){.
2cf40 20 20 20 20 20 20 69 66 28 20 28 70 4f 72 54 65 if( (pOrTe
2cf50 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 rm->eOperator &
2cf60 57 4f 5f 41 4e 44 29 21 3d 30 20 29 7b 0a 20 20 WO_AND)!=0 ){.
2cf70 20 20 20 20 20 20 20 20 73 53 75 62 42 75 69 6c sSubBuil
2cf80 64 2e 70 57 43 20 3d 20 26 70 4f 72 54 65 72 6d d.pWC = &pOrTerm
2cf90 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 2d 3e 77 63 ->u.pAndInfo->wc
2cfa0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 ;. }else
2cfb0 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 if( pOrTerm->lef
2cfc0 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 7b tCursor==iCur ){
2cfd0 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 . tempW
2cfe0 43 2e 70 57 49 6e 66 6f 20 3d 20 70 57 43 2d 3e C.pWInfo = pWC->
2cff0 70 57 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 pWInfo;.
2d000 20 20 74 65 6d 70 57 43 2e 70 4f 75 74 65 72 20 tempWC.pOuter
2d010 3d 20 70 57 43 3b 0a 20 20 20 20 20 20 20 20 20 = pWC;.
2d020 20 74 65 6d 70 57 43 2e 6f 70 20 3d 20 54 4b 5f tempWC.op = TK_
2d030 41 4e 44 3b 0a 20 20 20 20 20 20 20 20 20 20 74 AND;. t
2d040 65 6d 70 57 43 2e 6e 54 65 72 6d 20 3d 20 31 3b empWC.nTerm = 1;
2d050 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d 70 57 . tempW
2d060 43 2e 61 20 3d 20 70 4f 72 54 65 72 6d 3b 0a 20 C.a = pOrTerm;.
2d070 20 20 20 20 20 20 20 20 20 73 53 75 62 42 75 69 sSubBui
2d080 6c 64 2e 70 57 43 20 3d 20 26 74 65 6d 70 57 43 ld.pWC = &tempWC
2d090 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b ;. }else{
2d0a0 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 . conti
2d0b0 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 nue;. }.
2d0c0 20 20 20 20 20 20 20 73 43 75 72 2e 6e 20 3d 20 sCur.n =
2d0d0 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 0;.#ifndef SQLIT
2d0e0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 E_OMIT_VIRTUALTA
2d0f0 42 4c 45 0a 20 20 20 20 20 20 20 20 69 66 28 20 BLE. if(
2d100 49 73 56 69 72 74 75 61 6c 28 70 49 74 65 6d 2d IsVirtual(pItem-
2d110 3e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 >pTab) ){.
2d120 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 4c 6f rc = whereLo
2d130 6f 70 41 64 64 56 69 72 74 75 61 6c 28 26 73 53 opAddVirtual(&sS
2d140 75 62 42 75 69 6c 64 29 3b 0a 20 20 20 20 20 20 ubBuild);.
2d150 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 for(i=0; i<s
2d160 43 75 72 2e 6e 3b 20 69 2b 2b 29 20 73 43 75 72 Cur.n; i++) sCur
2d170 2e 61 5b 69 5d 2e 70 72 65 72 65 71 20 7c 3d 20 .a[i].prereq |=
2d180 6d 45 78 74 72 61 3b 0a 20 20 20 20 20 20 20 20 mExtra;.
2d190 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 }else.#endif.
2d1a0 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 {.
2d1b0 20 72 63 20 3d 20 77 68 65 72 65 4c 6f 6f 70 41 rc = whereLoopA
2d1c0 64 64 42 74 72 65 65 28 26 73 53 75 62 42 75 69 ddBtree(&sSubBui
2d1d0 6c 64 2c 20 6d 45 78 74 72 61 29 3b 0a 20 20 20 ld, mExtra);.
2d1e0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
2d1f0 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 ssert( rc==SQLIT
2d200 45 5f 4f 4b 20 7c 7c 20 73 43 75 72 2e 6e 3d 3d E_OK || sCur.n==
2d210 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 0 );. if(
2d220 20 73 43 75 72 2e 6e 3d 3d 30 20 29 7b 0a 20 20 sCur.n==0 ){.
2d230 20 20 20 20 20 20 20 20 73 53 75 6d 2e 6e 20 3d sSum.n =
2d240 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 0;. br
2d250 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c eak;. }el
2d260 73 65 20 69 66 28 20 6f 6e 63 65 20 29 7b 0a 20 se if( once ){.
2d270 20 20 20 20 20 20 20 20 20 77 68 65 72 65 4f 72 whereOr
2d280 4d 6f 76 65 28 26 73 53 75 6d 2c 20 26 73 43 75 Move(&sSum, &sCu
2d290 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6f 6e r);. on
2d2a0 63 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 ce = 0;.
2d2b0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 }else{.
2d2c0 20 77 68 65 72 65 4f 72 4d 6f 76 65 28 26 73 50 whereOrMove(&sP
2d2d0 72 65 76 2c 20 26 73 53 75 6d 29 3b 0a 20 20 20 rev, &sSum);.
2d2e0 20 20 20 20 20 20 20 73 53 75 6d 2e 6e 20 3d 20 sSum.n =
2d2f0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 0;. for
2d300 28 69 3d 30 3b 20 69 3c 73 50 72 65 76 2e 6e 3b (i=0; i<sPrev.n;
2d310 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 i++){.
2d320 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 73 43 for(j=0; j<sC
2d330 75 72 2e 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 ur.n; j++){.
2d340 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65 4f whereO
2d350 72 49 6e 73 65 72 74 28 26 73 53 75 6d 2c 20 73 rInsert(&sSum, s
2d360 50 72 65 76 2e 61 5b 69 5d 2e 70 72 65 72 65 71 Prev.a[i].prereq
2d370 20 7c 20 73 43 75 72 2e 61 5b 6a 5d 2e 70 72 65 | sCur.a[j].pre
2d380 72 65 71 2c 0a 20 20 20 20 20 20 20 20 20 20 20 req,.
2d390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2d3a0 20 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 41 64 sqlite3LogEstAd
2d3b0 64 28 73 50 72 65 76 2e 61 5b 69 5d 2e 72 52 75 d(sPrev.a[i].rRu
2d3c0 6e 2c 20 73 43 75 72 2e 61 5b 6a 5d 2e 72 52 75 n, sCur.a[j].rRu
2d3d0 6e 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 n),.
2d3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2d3f0 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 41 64 64 sqlite3LogEstAdd
2d400 28 73 50 72 65 76 2e 61 5b 69 5d 2e 6e 4f 75 74 (sPrev.a[i].nOut
2d410 2c 20 73 43 75 72 2e 61 5b 6a 5d 2e 6e 4f 75 74 , sCur.a[j].nOut
2d420 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ));.
2d430 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 }. }.
2d440 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
2d450 20 20 20 20 20 20 70 4e 65 77 2d 3e 6e 4c 54 65 pNew->nLTe
2d460 72 6d 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4e rm = 1;. pN
2d470 65 77 2d 3e 61 4c 54 65 72 6d 5b 30 5d 20 3d 20 ew->aLTerm[0] =
2d480 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4e 65 pTerm;. pNe
2d490 77 2d 3e 77 73 46 6c 61 67 73 20 3d 20 57 48 45 w->wsFlags = WHE
2d4a0 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a 20 20 20 RE_MULTI_OR;.
2d4b0 20 20 20 70 4e 65 77 2d 3e 72 53 65 74 75 70 20 pNew->rSetup
2d4c0 3d 20 30 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d = 0;. pNew-
2d4d0 3e 69 53 6f 72 74 49 64 78 20 3d 20 30 3b 0a 20 >iSortIdx = 0;.
2d4e0 20 20 20 20 20 6d 65 6d 73 65 74 28 26 70 4e 65 memset(&pNe
2d4f0 77 2d 3e 75 2c 20 30 2c 20 73 69 7a 65 6f 66 28 w->u, 0, sizeof(
2d500 70 4e 65 77 2d 3e 75 29 29 3b 0a 20 20 20 20 20 pNew->u));.
2d510 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 for(i=0; rc==SQ
2d520 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 73 53 75 LITE_OK && i<sSu
2d530 6d 2e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 m.n; i++){.
2d540 20 20 20 2f 2a 20 54 55 4e 49 4e 47 3a 20 4d 75 /* TUNING: Mu
2d550 6c 74 69 70 6c 65 20 62 79 20 33 2e 35 20 66 6f ltiple by 3.5 fo
2d560 72 20 74 68 65 20 73 65 63 6f 6e 64 61 72 79 20 r the secondary
2d570 74 61 62 6c 65 20 6c 6f 6f 6b 75 70 20 2a 2f 0a table lookup */.
2d580 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 72 52 pNew->rR
2d590 75 6e 20 3d 20 73 53 75 6d 2e 61 5b 69 5d 2e 72 un = sSum.a[i].r
2d5a0 52 75 6e 20 2b 20 31 38 3b 0a 20 20 20 20 20 20 Run + 18;.
2d5b0 20 20 70 4e 65 77 2d 3e 6e 4f 75 74 20 3d 20 73 pNew->nOut = s
2d5c0 53 75 6d 2e 61 5b 69 5d 2e 6e 4f 75 74 3b 0a 20 Sum.a[i].nOut;.
2d5d0 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 pNew->pre
2d5e0 72 65 71 20 3d 20 73 53 75 6d 2e 61 5b 69 5d 2e req = sSum.a[i].
2d5f0 70 72 65 72 65 71 3b 0a 20 20 20 20 20 20 20 20 prereq;.
2d600 72 63 20 3d 20 77 68 65 72 65 4c 6f 6f 70 49 6e rc = whereLoopIn
2d610 73 65 72 74 28 70 42 75 69 6c 64 65 72 2c 20 70 sert(pBuilder, p
2d620 4e 65 77 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 New);. }.
2d630 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
2d640 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 rc;.}../*.** Ad
2d650 64 20 61 6c 6c 20 57 68 65 72 65 4c 6f 6f 70 20 d all WhereLoop
2d660 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c 20 objects for all
2d670 74 61 62 6c 65 73 20 0a 2a 2f 0a 73 74 61 74 69 tables .*/.stati
2d680 63 20 69 6e 74 20 77 68 65 72 65 4c 6f 6f 70 41 c int whereLoopA
2d690 64 64 41 6c 6c 28 57 68 65 72 65 4c 6f 6f 70 42 ddAll(WhereLoopB
2d6a0 75 69 6c 64 65 72 20 2a 70 42 75 69 6c 64 65 72 uilder *pBuilder
2d6b0 29 7b 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a ){. WhereInfo *
2d6c0 70 57 49 6e 66 6f 20 3d 20 70 42 75 69 6c 64 65 pWInfo = pBuilde
2d6d0 72 2d 3e 70 57 49 6e 66 6f 3b 0a 20 20 42 69 74 r->pWInfo;. Bit
2d6e0 6d 61 73 6b 20 6d 45 78 74 72 61 20 3d 20 30 3b mask mExtra = 0;
2d6f0 0a 20 20 42 69 74 6d 61 73 6b 20 6d 50 72 69 6f . Bitmask mPrio
2d700 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 54 61 r = 0;. int iTa
2d710 62 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 b;. SrcList *pT
2d720 61 62 4c 69 73 74 20 3d 20 70 57 49 6e 66 6f 2d abList = pWInfo-
2d730 3e 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 74 72 >pTabList;. str
2d740 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d uct SrcList_item
2d750 20 2a 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69 74 *pItem;. sqlit
2d760 65 33 20 2a 64 62 20 3d 20 70 57 49 6e 66 6f 2d e3 *db = pWInfo-
2d770 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 >pParse->db;. i
2d780 6e 74 20 6e 54 61 62 4c 69 73 74 20 3d 20 70 57 nt nTabList = pW
2d790 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 0a 20 20 Info->nLevel;.
2d7a0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f int rc = SQLITE_
2d7b0 4f 4b 3b 0a 20 20 75 38 20 70 72 69 6f 72 4a 6f OK;. u8 priorJo
2d7c0 69 6e 54 79 70 65 20 3d 20 30 3b 0a 20 20 57 68 inType = 0;. Wh
2d7d0 65 72 65 4c 6f 6f 70 20 2a 70 4e 65 77 3b 0a 0a ereLoop *pNew;..
2d7e0 20 20 2f 2a 20 4c 6f 6f 70 20 6f 76 65 72 20 74 /* Loop over t
2d7f0 68 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 he tables in the
2d800 20 6a 6f 69 6e 2c 20 66 72 6f 6d 20 6c 65 66 74 join, from left
2d810 20 74 6f 20 72 69 67 68 74 20 2a 2f 0a 20 20 70 to right */. p
2d820 4e 65 77 20 3d 20 70 42 75 69 6c 64 65 72 2d 3e New = pBuilder->
2d830 70 4e 65 77 3b 0a 20 20 77 68 65 72 65 4c 6f 6f pNew;. whereLoo
2d840 70 49 6e 69 74 28 70 4e 65 77 29 3b 0a 20 20 66 pInit(pNew);. f
2d850 6f 72 28 69 54 61 62 3d 30 2c 20 70 49 74 65 6d or(iTab=0, pItem
2d860 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 54 =pTabList->a; iT
2d870 61 62 3c 6e 54 61 62 4c 69 73 74 3b 20 69 54 61 ab<nTabList; iTa
2d880 62 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 b++, pItem++){.
2d890 20 20 20 70 4e 65 77 2d 3e 69 54 61 62 20 3d 20 pNew->iTab =
2d8a0 69 54 61 62 3b 0a 20 20 20 20 70 4e 65 77 2d 3e iTab;. pNew->
2d8b0 6d 61 73 6b 53 65 6c 66 20 3d 20 67 65 74 4d 61 maskSelf = getMa
2d8c0 73 6b 28 26 70 57 49 6e 66 6f 2d 3e 73 4d 61 73 sk(&pWInfo->sMas
2d8d0 6b 53 65 74 2c 20 70 49 74 65 6d 2d 3e 69 43 75 kSet, pItem->iCu
2d8e0 72 73 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 28 rsor);. if( (
2d8f0 28 70 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 (pItem->jointype
2d900 7c 70 72 69 6f 72 4a 6f 69 6e 54 79 70 65 29 20 |priorJoinType)
2d910 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 43 52 & (JT_LEFT|JT_CR
2d920 4f 53 53 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 OSS))!=0 ){.
2d930 20 20 6d 45 78 74 72 61 20 3d 20 6d 50 72 69 6f mExtra = mPrio
2d940 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 72 69 r;. }. pri
2d950 6f 72 4a 6f 69 6e 54 79 70 65 20 3d 20 70 49 74 orJoinType = pIt
2d960 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 em->jointype;.
2d970 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 if( IsVirtual(
2d980 70 49 74 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a pItem->pTab) ){.
2d990 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 rc = where
2d9a0 4c 6f 6f 70 41 64 64 56 69 72 74 75 61 6c 28 70 LoopAddVirtual(p
2d9b0 42 75 69 6c 64 65 72 29 3b 0a 20 20 20 20 7d 65 Builder);. }e
2d9c0 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 lse{. rc =
2d9d0 77 68 65 72 65 4c 6f 6f 70 41 64 64 42 74 72 65 whereLoopAddBtre
2d9e0 65 28 70 42 75 69 6c 64 65 72 2c 20 6d 45 78 74 e(pBuilder, mExt
2d9f0 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 ra);. }. i
2da00 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b f( rc==SQLITE_OK
2da10 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 ){. rc = w
2da20 68 65 72 65 4c 6f 6f 70 41 64 64 4f 72 28 70 42 hereLoopAddOr(pB
2da30 75 69 6c 64 65 72 2c 20 6d 45 78 74 72 61 29 3b uilder, mExtra);
2da40 0a 20 20 20 20 7d 0a 20 20 20 20 6d 50 72 69 6f . }. mPrio
2da50 72 20 7c 3d 20 70 4e 65 77 2d 3e 6d 61 73 6b 53 r |= pNew->maskS
2da60 65 6c 66 3b 0a 20 20 20 20 69 66 28 20 72 63 20 elf;. if( rc
2da70 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 || db->mallocFai
2da80 6c 65 64 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d led ) break;. }
2da90 0a 20 20 77 68 65 72 65 4c 6f 6f 70 43 6c 65 61 . whereLoopClea
2daa0 72 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 72 r(db, pNew);. r
2dab0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a eturn rc;.}../*.
2dac0 2a 2a 20 45 78 61 6d 69 6e 65 20 61 20 57 68 65 ** Examine a Whe
2dad0 72 65 50 61 74 68 20 28 77 69 74 68 20 74 68 65 rePath (with the
2dae0 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 74 68 65 addition of the
2daf0 20 65 78 74 72 61 20 57 68 65 72 65 4c 6f 6f 70 extra WhereLoop
2db00 20 6f 66 20 74 68 65 20 35 74 68 0a 2a 2a 20 70 of the 5th.** p
2db10 61 72 61 6d 65 74 65 72 73 29 20 74 6f 20 73 65 arameters) to se
2db20 65 20 69 66 20 69 74 20 6f 75 74 70 75 74 73 20 e if it outputs
2db30 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65 71 75 rows in the requ
2db40 65 73 74 65 64 20 4f 52 44 45 52 20 42 59 0a 2a ested ORDER BY.*
2db50 2a 20 28 6f 72 20 47 52 4f 55 50 20 42 59 29 20 * (or GROUP BY)
2db60 77 69 74 68 6f 75 74 20 72 65 71 75 69 72 69 6e without requirin
2db70 67 20 61 20 73 65 70 61 72 61 74 65 20 73 6f 72 g a separate sor
2db80 74 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 52 65 t operation. Re
2db90 74 75 72 6e 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 turn:.** .**
2dba0 30 3a 20 20 4f 52 44 45 52 20 42 59 20 69 73 20 0: ORDER BY is
2dbb0 6e 6f 74 20 73 61 74 69 73 66 69 65 64 2e 20 20 not satisfied.
2dbc0 53 6f 72 74 69 6e 67 20 72 65 71 75 69 72 65 64 Sorting required
2dbd0 0a 2a 2a 20 20 20 20 31 3a 20 20 4f 52 44 45 52 .** 1: ORDER
2dbe0 20 42 59 20 69 73 20 73 61 74 69 73 66 69 65 64 BY is satisfied
2dbf0 2e 20 20 20 20 20 20 4f 6d 69 74 20 73 6f 72 74 . Omit sort
2dc00 69 6e 67 0a 2a 2a 20 20 20 2d 31 3a 20 20 55 6e ing.** -1: Un
2dc10 6b 6e 6f 77 6e 20 61 74 20 74 68 69 73 20 74 69 known at this ti
2dc20 6d 65 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 me.**.** Note th
2dc30 61 74 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f at processing fo
2dc40 72 20 57 48 45 52 45 5f 47 52 4f 55 50 42 59 20 r WHERE_GROUPBY
2dc50 61 6e 64 20 57 48 45 52 45 5f 44 49 53 54 49 4e and WHERE_DISTIN
2dc60 43 54 42 59 20 69 73 20 6e 6f 74 20 61 73 0a 2a CTBY is not as.*
2dc70 2a 20 73 74 72 69 63 74 2e 20 20 57 69 74 68 20 * strict. With
2dc80 47 52 4f 55 50 20 42 59 20 61 6e 64 20 44 49 53 GROUP BY and DIS
2dc90 54 49 4e 43 54 20 74 68 65 20 6f 6e 6c 79 20 72 TINCT the only r
2dca0 65 71 75 69 72 65 6d 65 6e 74 20 69 73 20 74 68 equirement is th
2dcb0 61 74 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 at.** equivalent
2dcc0 20 72 6f 77 73 20 61 70 70 65 61 72 20 69 6d 6d rows appear imm
2dcd0 65 64 69 61 74 65 6c 79 20 61 64 6a 61 63 65 6e ediately adjacen
2dce0 74 20 74 6f 20 6f 6e 65 20 61 6e 6f 74 68 65 72 t to one another
2dcf0 2e 20 20 47 52 4f 55 50 20 42 59 0a 2a 2a 20 61 . GROUP BY.** a
2dd00 6e 64 20 44 49 53 54 49 4e 54 20 64 6f 20 6e 6f nd DISTINT do no
2dd10 74 20 72 65 71 75 69 72 65 20 72 6f 77 73 20 74 t require rows t
2dd20 6f 20 61 70 70 65 61 72 20 69 6e 20 61 6e 79 20 o appear in any
2dd30 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65 72 particular order
2dd40 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 65 as long.** as e
2dd50 71 75 69 76 65 6c 65 6e 74 20 72 6f 77 73 20 61 quivelent rows a
2dd60 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74 re grouped toget
2dd70 68 65 72 2e 20 20 54 68 75 73 20 66 6f 72 20 47 her. Thus for G
2dd80 52 4f 55 50 20 42 59 20 61 6e 64 20 44 49 53 54 ROUP BY and DIST
2dd90 49 4e 43 54 0a 2a 2a 20 74 68 65 20 70 4f 72 64 INCT.** the pOrd
2dda0 65 72 42 79 20 74 65 72 6d 73 20 63 61 6e 20 62 erBy terms can b
2ddb0 65 20 6d 61 74 63 68 65 64 20 69 6e 20 61 6e 79 e matched in any
2ddc0 20 6f 72 64 65 72 2e 20 20 57 69 74 68 20 4f 52 order. With OR
2ddd0 44 45 52 20 42 59 2c 20 74 68 65 20 0a 2a 2a 20 DER BY, the .**
2dde0 70 4f 72 64 65 72 42 79 20 74 65 72 6d 73 20 6d pOrderBy terms m
2ddf0 75 73 74 20 62 65 20 6d 61 74 63 68 65 64 20 69 ust be matched i
2de00 6e 20 73 74 72 69 63 74 20 6c 65 66 74 2d 74 6f n strict left-to
2de10 2d 72 69 67 68 74 20 6f 72 64 65 72 2e 0a 2a 2f -right order..*/
2de20 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 .static int wher
2de30 65 50 61 74 68 53 61 74 69 73 66 69 65 73 4f 72 ePathSatisfiesOr
2de40 64 65 72 42 79 28 0a 20 20 57 68 65 72 65 49 6e derBy(. WhereIn
2de50 66 6f 20 2a 70 57 49 6e 66 6f 2c 20 20 20 20 2f fo *pWInfo, /
2de60 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 * The WHERE clau
2de70 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 se */. ExprList
2de80 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a *pOrderBy, /*
2de90 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f ORDER BY or GRO
2dea0 55 50 20 42 59 20 6f 72 20 44 49 53 54 49 4e 43 UP BY or DISTINC
2deb0 54 20 63 6c 61 75 73 65 20 74 6f 20 63 68 65 63 T clause to chec
2dec0 6b 20 2a 2f 0a 20 20 57 68 65 72 65 50 61 74 68 k */. WherePath
2ded0 20 2a 70 50 61 74 68 2c 20 20 20 20 20 2f 2a 20 *pPath, /*
2dee0 54 68 65 20 57 68 65 72 65 50 61 74 68 20 74 6f The WherePath to
2def0 20 63 68 65 63 6b 20 2a 2f 0a 20 20 75 31 36 20 check */. u16
2df00 77 63 74 72 6c 46 6c 61 67 73 2c 20 20 20 20 20 wctrlFlags,
2df10 20 20 2f 2a 20 4d 69 67 68 74 20 63 6f 6e 74 61 /* Might conta
2df20 69 6e 20 57 48 45 52 45 5f 47 52 4f 55 50 42 59 in WHERE_GROUPBY
2df30 20 6f 72 20 57 48 45 52 45 5f 44 49 53 54 49 4e or WHERE_DISTIN
2df40 43 54 42 59 20 2a 2f 0a 20 20 75 31 36 20 6e 4c CTBY */. u16 nL
2df50 6f 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 oop,
2df60 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 /* Number of ent
2df70 72 69 65 73 20 69 6e 20 70 50 61 74 68 2d 3e 61 ries in pPath->a
2df80 4c 6f 6f 70 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 Loop[] */. Wher
2df90 65 4c 6f 6f 70 20 2a 70 4c 61 73 74 2c 20 20 20 eLoop *pLast,
2dfa0 20 20 2f 2a 20 41 64 64 20 74 68 69 73 20 57 68 /* Add this Wh
2dfb0 65 72 65 4c 6f 6f 70 20 74 6f 20 74 68 65 20 65 ereLoop to the e
2dfc0 6e 64 20 6f 66 20 70 50 61 74 68 2d 3e 61 4c 6f nd of pPath->aLo
2dfd0 6f 70 5b 5d 20 2a 2f 0a 20 20 42 69 74 6d 61 73 op[] */. Bitmas
2dfe0 6b 20 2a 70 52 65 76 4d 61 73 6b 20 20 20 20 20 k *pRevMask
2dff0 2f 2a 20 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 /* OUT: Mask of
2e000 57 68 65 72 65 4c 6f 6f 70 73 20 74 6f 20 72 75 WhereLoops to ru
2e010 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 n in reverse ord
2e020 65 72 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 72 65 er */.){. u8 re
2e030 76 53 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 vSet;
2e040 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65 76 20 /* True if rev
2e050 69 73 20 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 75 38 is known */. u8
2e060 20 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 rev;
2e070 20 20 20 20 2f 2a 20 43 6f 6d 70 6f 73 69 74 65 /* Composite
2e080 20 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 sort order */.
2e090 20 75 38 20 72 65 76 49 64 78 3b 20 20 20 20 20 u8 revIdx;
2e0a0 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
2e0b0 73 6f 72 74 20 6f 72 64 65 72 20 2a 2f 0a 20 20 sort order */.
2e0c0 75 38 20 69 73 4f 72 64 65 72 44 69 73 74 69 6e u8 isOrderDistin
2e0d0 63 74 3b 20 20 20 2f 2a 20 41 6c 6c 20 70 72 69 ct; /* All pri
2e0e0 6f 72 20 57 68 65 72 65 4c 6f 6f 70 73 20 61 72 or WhereLoops ar
2e0f0 65 20 6f 72 64 65 72 2d 64 69 73 74 69 6e 63 74 e order-distinct
2e100 20 2a 2f 0a 20 20 75 38 20 64 69 73 74 69 6e 63 */. u8 distinc
2e110 74 43 6f 6c 75 6d 6e 73 3b 20 20 20 2f 2a 20 54 tColumns; /* T
2e120 72 75 65 20 69 66 20 74 68 65 20 6c 6f 6f 70 20 rue if the loop
2e130 68 61 73 20 55 4e 49 51 55 45 20 4e 4f 54 20 4e has UNIQUE NOT N
2e140 55 4c 4c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 ULL columns */.
2e150 20 75 38 20 69 73 4d 61 74 63 68 3b 20 20 20 20 u8 isMatch;
2e160 20 20 20 20 20 20 20 2f 2a 20 69 43 6f 6c 75 6d /* iColum
2e170 6e 20 6d 61 74 63 68 65 73 20 61 20 74 65 72 6d n matches a term
2e180 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 of the ORDER BY
2e190 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 75 31 36 clause */. u16
2e1a0 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 nColumn;
2e1b0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
2e1c0 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 49 6e 64 65 columns in pInde
2e1d0 78 20 2a 2f 0a 20 20 75 31 36 20 6e 4f 72 64 65 x */. u16 nOrde
2e1e0 72 42 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 rBy; /*
2e1f0 4e 75 6d 62 65 72 20 74 65 72 6d 73 20 69 6e 20 Number terms in
2e200 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
2e210 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f use */. int iLo
2e220 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f op; /
2e230 2a 20 49 6e 64 65 78 20 6f 66 20 57 68 65 72 65 * Index of Where
2e240 4c 6f 6f 70 20 69 6e 20 70 50 61 74 68 20 62 65 Loop in pPath be
2e250 69 6e 67 20 70 72 6f 63 65 73 73 65 64 20 2a 2f ing processed */
2e260 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 . int i, j;
2e270 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
2e280 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 counters */. i
2e290 6e 74 20 69 43 75 72 3b 20 20 20 20 20 20 20 20 nt iCur;
2e2a0 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e /* Cursor n
2e2b0 75 6d 62 65 72 20 66 6f 72 20 63 75 72 72 65 6e umber for curren
2e2c0 74 20 57 68 65 72 65 4c 6f 6f 70 20 2a 2f 0a 20 t WhereLoop */.
2e2d0 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 int iColumn;
2e2e0 20 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c 75 /* A colu
2e2f0 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e mn number within
2e300 20 74 61 62 6c 65 20 69 43 75 72 20 2a 2f 0a 20 table iCur */.
2e310 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4c 6f 6f WhereLoop *pLoo
2e320 70 20 3d 20 30 3b 20 2f 2a 20 43 75 72 72 65 6e p = 0; /* Curren
2e330 74 20 57 68 65 72 65 4c 6f 6f 70 20 62 65 69 6e t WhereLoop bein
2e340 67 20 70 72 6f 63 65 73 73 65 64 2e 20 2a 2f 0a g processed. */.
2e350 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 WhereTerm *pTe
2e360 72 6d 3b 20 20 20 20 20 2f 2a 20 41 20 73 69 6e rm; /* A sin
2e370 67 6c 65 20 74 65 72 6d 20 6f 66 20 74 68 65 20 gle term of the
2e380 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a WHERE clause */.
2e390 20 20 45 78 70 72 20 2a 70 4f 42 45 78 70 72 3b Expr *pOBExpr;
2e3a0 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 /* An ex
2e3b0 70 72 65 73 73 69 6f 6e 20 66 72 6f 6d 20 74 68 pression from th
2e3c0 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
2e3d0 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a e */. CollSeq *
2e3e0 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 2f 2a 20 pColl; /*
2e3f0 43 4f 4c 4c 41 54 45 20 66 75 6e 63 74 69 6f 6e COLLATE function
2e400 20 66 72 6f 6d 20 61 6e 20 4f 52 44 45 52 20 42 from an ORDER B
2e410 59 20 63 6c 61 75 73 65 20 74 65 72 6d 20 2a 2f Y clause term */
2e420 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 . Index *pIndex
2e430 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ; /* The
2e440 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 index associated
2e450 20 77 69 74 68 20 70 4c 6f 6f 70 20 2a 2f 0a 20 with pLoop */.
2e460 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 sqlite3 *db = p
2e470 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 2d 3e 64 WInfo->pParse->d
2e480 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 b; /* Database
2e490 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
2e4a0 42 69 74 6d 61 73 6b 20 6f 62 53 61 74 20 3d 20 Bitmask obSat =
2e4b0 30 3b 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 0; /* Mask of
2e4c0 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 ORDER BY terms
2e4d0 73 61 74 69 73 66 69 65 64 20 73 6f 20 66 61 72 satisfied so far
2e4e0 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6f 62 */. Bitmask ob
2e4f0 44 6f 6e 65 3b 20 20 20 20 20 20 20 2f 2a 20 4d Done; /* M
2e500 61 73 6b 20 6f 66 20 61 6c 6c 20 4f 52 44 45 52 ask of all ORDER
2e510 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a 20 20 42 BY terms */. B
2e520 69 74 6d 61 73 6b 20 6f 72 64 65 72 44 69 73 74 itmask orderDist
2e530 69 6e 63 74 4d 61 73 6b 3b 20 20 2f 2a 20 4d 61 inctMask; /* Ma
2e540 73 6b 20 6f 66 20 61 6c 6c 20 77 65 6c 6c 2d 6f sk of all well-o
2e550 72 64 65 72 65 64 20 6c 6f 6f 70 73 20 2a 2f 0a rdered loops */.
2e560 20 20 42 69 74 6d 61 73 6b 20 72 65 61 64 79 3b Bitmask ready;
2e570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
2e580 20 4d 61 73 6b 20 6f 66 20 69 6e 6e 65 72 20 6c Mask of inner l
2e590 6f 6f 70 73 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 oops */.. /*.
2e5a0 2a 2a 20 57 65 20 73 61 79 20 74 68 65 20 57 68 ** We say the Wh
2e5b0 65 72 65 4c 6f 6f 70 20 69 73 20 22 6f 6e 65 2d ereLoop is "one-
2e5c0 72 6f 77 22 20 69 66 20 69 74 20 67 65 6e 65 72 row" if it gener
2e5d0 61 74 65 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 ates no more tha
2e5e0 6e 20 6f 6e 65 0a 20 20 2a 2a 20 72 6f 77 20 6f n one. ** row o
2e5f0 66 20 6f 75 74 70 75 74 2e 20 20 41 20 57 68 65 f output. A Whe
2e600 72 65 4c 6f 6f 70 20 69 73 20 6f 6e 65 2d 72 6f reLoop is one-ro
2e610 77 20 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 w if all of the
2e620 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 following are tr
2e630 75 65 3a 0a 20 20 2a 2a 20 20 28 61 29 20 41 6c ue:. ** (a) Al
2e640 6c 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 l index columns
2e650 6d 61 74 63 68 20 77 69 74 68 20 57 48 45 52 45 match with WHERE
2e660 5f 43 4f 4c 55 4d 4e 5f 45 51 2e 0a 20 20 2a 2a _COLUMN_EQ.. **
2e670 20 20 28 62 29 20 54 68 65 20 69 6e 64 65 78 20 (b) The index
2e680 69 73 20 75 6e 69 71 75 65 0a 20 20 2a 2a 20 41 is unique. ** A
2e690 6e 79 20 57 68 65 72 65 4c 6f 6f 70 20 77 69 74 ny WhereLoop wit
2e6a0 68 20 61 6e 20 57 48 45 52 45 5f 43 4f 4c 55 4d h an WHERE_COLUM
2e6b0 4e 5f 45 51 20 63 6f 6e 73 74 72 61 69 6e 74 20 N_EQ constraint
2e6c0 6f 6e 20 74 68 65 20 72 6f 77 69 64 20 69 73 20 on the rowid is
2e6d0 6f 6e 65 2d 72 6f 77 2e 0a 20 20 2a 2a 20 45 76 one-row.. ** Ev
2e6e0 65 72 79 20 6f 6e 65 2d 72 6f 77 20 57 68 65 72 ery one-row Wher
2e6f0 65 4c 6f 6f 70 20 77 69 6c 6c 20 68 61 76 65 20 eLoop will have
2e700 74 68 65 20 57 48 45 52 45 5f 4f 4e 45 52 4f 57 the WHERE_ONEROW
2e710 20 62 69 74 20 73 65 74 20 69 6e 20 77 73 46 6c bit set in wsFl
2e720 61 67 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 ags.. **. ** W
2e730 65 20 73 61 79 20 74 68 65 20 57 68 65 72 65 4c e say the WhereL
2e740 6f 6f 70 20 69 73 20 22 6f 72 64 65 72 2d 64 69 oop is "order-di
2e750 73 74 69 6e 63 74 22 20 69 66 20 74 68 65 20 73 stinct" if the s
2e760 65 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 72 et of columns fr
2e770 6f 6d 0a 20 20 2a 2a 20 74 68 61 74 20 57 68 65 om. ** that Whe
2e780 72 65 4c 6f 6f 70 20 74 68 61 74 20 61 72 65 20 reLoop that are
2e790 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 in the ORDER BY
2e7a0 63 6c 61 75 73 65 20 61 72 65 20 64 69 66 66 65 clause are diffe
2e7b0 72 65 6e 74 20 66 6f 72 20 65 76 65 72 79 0a 20 rent for every.
2e7c0 20 2a 2a 20 72 6f 77 20 6f 66 20 74 68 65 20 57 ** row of the W
2e7d0 68 65 72 65 4c 6f 6f 70 2e 20 20 45 76 65 72 79 hereLoop. Every
2e7e0 20 6f 6e 65 2d 72 6f 77 20 57 68 65 72 65 4c 6f one-row WhereLo
2e7f0 6f 70 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 op is automatica
2e800 6c 6c 79 0a 20 20 2a 2a 20 6f 72 64 65 72 2d 64 lly. ** order-d
2e810 69 73 74 69 6e 63 74 2e 20 20 20 41 20 57 68 65 istinct. A Whe
2e820 72 65 4c 6f 6f 70 20 74 68 61 74 20 68 61 73 20 reLoop that has
2e830 6e 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 no columns in th
2e840 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
2e850 65 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 6f 72 e. ** is not or
2e860 64 65 72 2d 64 69 73 74 69 6e 63 74 2e 20 54 6f der-distinct. To
2e870 20 62 65 20 6f 72 64 65 72 2d 64 69 73 74 69 6e be order-distin
2e880 63 74 20 69 73 20 6e 6f 74 20 71 75 69 74 65 20 ct is not quite
2e890 74 68 65 20 73 61 6d 65 20 61 73 20 62 65 69 6e the same as bein
2e8a0 67 0a 20 20 2a 2a 20 55 4e 49 51 55 45 20 73 69 g. ** UNIQUE si
2e8b0 6e 63 65 20 61 20 55 4e 49 51 55 45 20 63 6f 6c nce a UNIQUE col
2e8c0 75 6d 6e 20 6f 72 20 69 6e 64 65 78 20 63 61 6e umn or index can
2e8d0 20 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 72 have multiple r
2e8e0 6f 77 73 20 74 68 61 74 20 0a 20 20 2a 2a 20 61 ows that . ** a
2e8f0 72 65 20 4e 55 4c 4c 20 61 6e 64 20 4e 55 4c 4c re NULL and NULL
2e900 20 76 61 6c 75 65 73 20 61 72 65 20 65 71 75 69 values are equi
2e910 76 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 70 valent for the p
2e920 75 72 70 6f 73 65 20 6f 66 20 6f 72 64 65 72 2d urpose of order-
2e930 64 69 73 74 69 6e 63 74 2e 0a 20 20 2a 2a 20 54 distinct.. ** T
2e940 6f 20 62 65 20 6f 72 64 65 72 2d 64 69 73 74 69 o be order-disti
2e950 6e 63 74 2c 20 74 68 65 20 63 6f 6c 75 6d 6e 73 nct, the columns
2e960 20 6d 75 73 74 20 62 65 20 55 4e 49 51 55 45 20 must be UNIQUE
2e970 61 6e 64 20 4e 4f 54 20 4e 55 4c 4c 2e 0a 20 20 and NOT NULL..
2e980 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 **. ** The rowi
2e990 64 20 66 6f 72 20 61 20 74 61 62 6c 65 20 69 73 d for a table is
2e9a0 20 61 6c 77 61 79 73 20 55 4e 49 51 55 45 20 61 always UNIQUE a
2e9b0 6e 64 20 4e 4f 54 20 4e 55 4c 4c 20 73 6f 20 77 nd NOT NULL so w
2e9c0 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a henever the. **
2e9d0 20 72 6f 77 69 64 20 61 70 70 65 61 72 73 20 69 rowid appears i
2e9e0 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 n the ORDER BY c
2e9f0 6c 61 75 73 65 2c 20 74 68 65 20 63 6f 72 72 65 lause, the corre
2ea00 73 70 6f 6e 64 69 6e 67 20 57 68 65 72 65 4c 6f sponding WhereLo
2ea10 6f 70 20 69 73 0a 20 20 2a 2a 20 61 75 74 6f 6d op is. ** autom
2ea20 61 74 69 63 61 6c 6c 79 20 6f 72 64 65 72 2d 64 atically order-d
2ea30 69 73 74 69 6e 63 74 2e 0a 20 20 2a 2f 0a 0a 20 istinct.. */..
2ea40 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 assert( pOrderB
2ea50 79 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 53 6f y!=0 );.. /* So
2ea60 72 74 61 62 69 6c 69 74 79 20 6f 66 20 76 69 72 rtability of vir
2ea70 74 75 61 6c 20 74 61 62 6c 65 73 20 69 73 20 64 tual tables is d
2ea80 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 etermined by the
2ea90 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68 xBestIndex meth
2eaa0 6f 64 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 76 od. ** of the v
2eab0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 74 73 irtual table its
2eac0 65 6c 66 20 2a 2f 0a 20 20 69 66 28 20 70 4c 61 elf */. if( pLa
2ead0 73 74 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 st->wsFlags & WH
2eae0 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 ERE_VIRTUALTABLE
2eaf0 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61 73 65 ){. testcase
2eb00 28 20 6e 4c 6f 6f 70 3e 30 20 29 3b 20 20 2f 2a ( nLoop>0 ); /*
2eb10 20 54 72 75 65 20 77 68 65 6e 20 6f 75 74 65 72 True when outer
2eb20 20 6c 6f 6f 70 73 20 61 72 65 20 6f 6e 65 2d 72 loops are one-r
2eb30 6f 77 20 61 6e 64 20 6d 61 74 63 68 20 0a 20 20 ow and match .
2eb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2eb50 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 20 4f 52 ** no OR
2eb60 44 45 52 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a DER BY terms */.
2eb70 20 20 20 20 72 65 74 75 72 6e 20 70 4c 61 73 74 return pLast
2eb80 2d 3e 75 2e 76 74 61 62 2e 69 73 4f 72 64 65 72 ->u.vtab.isOrder
2eb90 65 64 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 4c ed;. }. if( nL
2eba0 6f 6f 70 20 26 26 20 4f 70 74 69 6d 69 7a 61 74 oop && Optimizat
2ebb0 69 6f 6e 44 69 73 61 62 6c 65 64 28 64 62 2c 20 ionDisabled(db,
2ebc0 53 51 4c 49 54 45 5f 4f 72 64 65 72 42 79 49 64 SQLITE_OrderById
2ebd0 78 4a 6f 69 6e 29 20 29 20 72 65 74 75 72 6e 20 xJoin) ) return
2ebe0 30 3b 0a 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 0;.. nOrderBy =
2ebf0 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 pOrderBy->nExpr
2ec00 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 4f ;. testcase( nO
2ec10 72 64 65 72 42 79 3d 3d 42 4d 53 2d 31 20 29 3b rderBy==BMS-1 );
2ec20 0a 20 20 69 66 28 20 6e 4f 72 64 65 72 42 79 3e . if( nOrderBy>
2ec30 42 4d 53 2d 31 20 29 20 72 65 74 75 72 6e 20 30 BMS-1 ) return 0
2ec40 3b 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 6f 70 74 ; /* Cannot opt
2ec50 69 6d 69 7a 65 20 6f 76 65 72 6c 79 20 6c 61 72 imize overly lar
2ec60 67 65 20 4f 52 44 45 52 20 42 59 73 20 2a 2f 0a ge ORDER BYs */.
2ec70 20 20 69 73 4f 72 64 65 72 44 69 73 74 69 6e 63 isOrderDistinc
2ec80 74 20 3d 20 31 3b 0a 20 20 6f 62 44 6f 6e 65 20 t = 1;. obDone
2ec90 3d 20 4d 41 53 4b 42 49 54 28 6e 4f 72 64 65 72 = MASKBIT(nOrder
2eca0 42 79 29 2d 31 3b 0a 20 20 6f 72 64 65 72 44 69 By)-1;. orderDi
2ecb0 73 74 69 6e 63 74 4d 61 73 6b 20 3d 20 30 3b 0a stinctMask = 0;.
2ecc0 20 20 72 65 61 64 79 20 3d 20 30 3b 0a 20 20 66 ready = 0;. f
2ecd0 6f 72 28 69 4c 6f 6f 70 3d 30 3b 20 69 73 4f 72 or(iLoop=0; isOr
2ece0 64 65 72 44 69 73 74 69 6e 63 74 20 26 26 20 6f derDistinct && o
2ecf0 62 53 61 74 3c 6f 62 44 6f 6e 65 20 26 26 20 69 bSat<obDone && i
2ed00 4c 6f 6f 70 3c 3d 6e 4c 6f 6f 70 3b 20 69 4c 6f Loop<=nLoop; iLo
2ed10 6f 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 op++){. if( i
2ed20 4c 6f 6f 70 3e 30 20 29 20 72 65 61 64 79 20 7c Loop>0 ) ready |
2ed30 3d 20 70 4c 6f 6f 70 2d 3e 6d 61 73 6b 53 65 6c = pLoop->maskSel
2ed40 66 3b 0a 20 20 20 20 70 4c 6f 6f 70 20 3d 20 69 f;. pLoop = i
2ed50 4c 6f 6f 70 3c 6e 4c 6f 6f 70 20 3f 20 70 50 61 Loop<nLoop ? pPa
2ed60 74 68 2d 3e 61 4c 6f 6f 70 5b 69 4c 6f 6f 70 5d th->aLoop[iLoop]
2ed70 20 3a 20 70 4c 61 73 74 3b 0a 20 20 20 20 61 73 : pLast;. as
2ed80 73 65 72 74 28 20 28 70 4c 6f 6f 70 2d 3e 77 73 sert( (pLoop->ws
2ed90 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49 Flags & WHERE_VI
2eda0 52 54 55 41 4c 54 41 42 4c 45 29 3d 3d 30 20 29 RTUALTABLE)==0 )
2edb0 3b 0a 20 20 20 20 69 43 75 72 20 3d 20 70 57 49 ;. iCur = pWI
2edc0 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 2d 3e 61 nfo->pTabList->a
2edd0 5b 70 4c 6f 6f 70 2d 3e 69 54 61 62 5d 2e 69 43 [pLoop->iTab].iC
2ede0 75 72 73 6f 72 3b 0a 0a 20 20 20 20 2f 2a 20 4d ursor;.. /* M
2edf0 61 72 6b 20 6f 66 66 20 61 6e 79 20 4f 52 44 45 ark off any ORDE
2ee00 52 20 42 59 20 74 65 72 6d 20 58 20 74 68 61 74 R BY term X that
2ee10 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 is a column in
2ee20 74 68 65 20 74 61 62 6c 65 20 6f 66 0a 20 20 20 the table of.
2ee30 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 ** the current
2ee40 6c 6f 6f 70 20 66 6f 72 20 77 68 69 63 68 20 74 loop for which t
2ee50 68 65 72 65 20 69 73 20 74 65 72 6d 20 69 6e 20 here is term in
2ee60 74 68 65 20 57 48 45 52 45 0a 20 20 20 20 2a 2a the WHERE. **
2ee70 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 66 clause of the f
2ee80 6f 72 6d 20 58 20 49 53 20 4e 55 4c 4c 20 6f 72 orm X IS NULL or
2ee90 20 58 3d 3f 20 74 68 61 74 20 72 65 66 65 72 65 X=? that refere
2eea0 6e 63 65 20 6f 6e 6c 79 20 6f 75 74 65 72 0a 20 nce only outer.
2eeb0 20 20 20 2a 2a 20 6c 6f 6f 70 73 2e 0a 20 20 20 ** loops..
2eec0 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b */. for(i=0;
2eed0 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b i<nOrderBy; i++
2eee0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 4d 41 53 ){. if( MAS
2eef0 4b 42 49 54 28 69 29 20 26 20 6f 62 53 61 74 20 KBIT(i) & obSat
2ef00 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
2ef10 20 20 70 4f 42 45 78 70 72 20 3d 20 73 71 6c 69 pOBExpr = sqli
2ef20 74 65 33 45 78 70 72 53 6b 69 70 43 6f 6c 6c 61 te3ExprSkipColla
2ef30 74 65 28 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 te(pOrderBy->a[i
2ef40 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 ].pExpr);.
2ef50 69 66 28 20 70 4f 42 45 78 70 72 2d 3e 6f 70 21 if( pOBExpr->op!
2ef60 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20 63 6f 6e =TK_COLUMN ) con
2ef70 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 tinue;. if(
2ef80 20 70 4f 42 45 78 70 72 2d 3e 69 54 61 62 6c 65 pOBExpr->iTable
2ef90 21 3d 69 43 75 72 20 29 20 63 6f 6e 74 69 6e 75 !=iCur ) continu
2efa0 65 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d e;. pTerm =
2efb0 20 66 69 6e 64 54 65 72 6d 28 26 70 57 49 6e 66 findTerm(&pWInf
2efc0 6f 2d 3e 73 57 43 2c 20 69 43 75 72 2c 20 70 4f o->sWC, iCur, pO
2efd0 42 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 0a BExpr->iColumn,.
2efe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2eff0 20 20 20 20 20 20 20 7e 72 65 61 64 79 2c 20 57 ~ready, W
2f000 4f 5f 45 51 7c 57 4f 5f 49 53 4e 55 4c 4c 2c 20 O_EQ|WO_ISNULL,
2f010 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 0);. if( pT
2f020 65 72 6d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 erm==0 ) continu
2f030 65 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 54 e;. if( (pT
2f040 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 57 erm->eOperator&W
2f050 4f 5f 45 51 29 21 3d 30 20 26 26 20 70 4f 42 45 O_EQ)!=0 && pOBE
2f060 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 xpr->iColumn>=0
2f070 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 ){. const
2f080 20 63 68 61 72 20 2a 7a 31 2c 20 2a 7a 32 3b 0a char *z1, *z2;.
2f090 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 pColl =
2f0a0 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 sqlite3ExprCollS
2f0b0 65 71 28 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 eq(pWInfo->pPars
2f0c0 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 e, pOrderBy->a[i
2f0d0 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 ].pExpr);.
2f0e0 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 if( !pColl ) p
2f0f0 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 Coll = db->pDflt
2f100 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7a 31 Coll;. z1
2f110 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b = pColl->zName;
2f120 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d . pColl =
2f130 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c sqlite3ExprColl
2f140 53 65 71 28 70 57 49 6e 66 6f 2d 3e 70 50 61 72 Seq(pWInfo->pPar
2f150 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 se, pTerm->pExpr
2f160 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 );. if( !
2f170 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 pColl ) pColl =
2f180 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 db->pDfltColl;.
2f190 20 20 20 20 20 20 20 7a 32 20 3d 20 70 43 6f 6c z2 = pCol
2f1a0 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 l->zName;.
2f1b0 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 if( sqlite3Str
2f1c0 49 43 6d 70 28 7a 31 2c 20 7a 32 29 21 3d 30 20 ICmp(z1, z2)!=0
2f1d0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
2f1e0 20 20 7d 0a 20 20 20 20 20 20 6f 62 53 61 74 20 }. obSat
2f1f0 7c 3d 20 4d 41 53 4b 42 49 54 28 69 29 3b 0a 20 |= MASKBIT(i);.
2f200 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 28 70 }.. if( (p
2f210 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 Loop->wsFlags &
2f220 57 48 45 52 45 5f 4f 4e 45 52 4f 57 29 3d 3d 30 WHERE_ONEROW)==0
2f230 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4c ){. if( pL
2f240 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 oop->wsFlags & W
2f250 48 45 52 45 5f 49 50 4b 20 29 7b 0a 20 20 20 20 HERE_IPK ){.
2f260 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a pIndex = 0;.
2f270 20 20 20 20 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 nColumn
2f280 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 = 0;. }else
2f290 20 69 66 28 20 28 70 49 6e 64 65 78 20 3d 20 70 if( (pIndex = p
2f2a0 4c 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 Loop->u.btree.pI
2f2b0 6e 64 65 78 29 3d 3d 30 20 7c 7c 20 70 49 6e 64 ndex)==0 || pInd
2f2c0 65 78 2d 3e 62 55 6e 6f 72 64 65 72 65 64 20 29 ex->bUnordered )
2f2d0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e {. return
2f2e0 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0;. }else{
2f2f0 0a 20 20 20 20 20 20 20 20 6e 43 6f 6c 75 6d 6e . nColumn
2f300 20 3d 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 = pIndex->nColu
2f310 6d 6e 3b 0a 20 20 20 20 20 20 20 20 69 73 4f 72 mn;. isOr
2f320 64 65 72 44 69 73 74 69 6e 63 74 20 3d 20 70 49 derDistinct = pI
2f330 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f ndex->onError!=O
2f340 45 5f 4e 6f 6e 65 3b 0a 20 20 20 20 20 20 7d 0a E_None;. }.
2f350 0a 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 . /* Loop t
2f360 68 72 6f 75 67 68 20 61 6c 6c 20 63 6f 6c 75 6d hrough all colum
2f370 6e 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 ns of the index
2f380 61 6e 64 20 64 65 61 6c 20 77 69 74 68 20 74 68 and deal with th
2f390 65 20 6f 6e 65 73 0a 20 20 20 20 20 20 2a 2a 20 e ones. **
2f3a0 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63 6f 6e that are not con
2f3b0 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d 20 6f strained by == o
2f3c0 72 20 49 4e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 r IN.. */.
2f3d0 20 20 20 20 20 72 65 76 20 3d 20 72 65 76 53 65 rev = revSe
2f3e0 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 69 73 t = 0;. dis
2f3f0 74 69 6e 63 74 43 6f 6c 75 6d 6e 73 20 3d 20 30 tinctColumns = 0
2f400 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b ;. for(j=0;
2f410 20 6a 3c 3d 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b j<=nColumn; j++
2f420 29 7b 0a 20 20 20 20 20 20 20 20 75 38 20 62 4f ){. u8 bO
2f430 6e 63 65 3b 20 20 20 2f 2a 20 54 72 75 65 20 74 nce; /* True t
2f440 6f 20 72 75 6e 20 74 68 65 20 4f 52 44 45 52 20 o run the ORDER
2f450 42 59 20 73 65 61 72 63 68 20 6c 6f 6f 70 20 2a BY search loop *
2f460 2f 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 6b /.. /* Sk
2f470 69 70 20 6f 76 65 72 20 3d 3d 20 61 6e 64 20 49 ip over == and I
2f480 53 20 4e 55 4c 4c 20 74 65 72 6d 73 20 2a 2f 0a S NULL terms */.
2f490 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c 70 4c if( j<pL
2f4a0 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 oop->u.btree.nEq
2f4b0 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 28 69 . && ((i
2f4c0 20 3d 20 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d = pLoop->aLTerm
2f4d0 5b 6a 5d 2d 3e 65 4f 70 65 72 61 74 6f 72 29 20 [j]->eOperator)
2f4e0 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e 55 & (WO_EQ|WO_ISNU
2f4f0 4c 4c 29 29 21 3d 30 0a 20 20 20 20 20 20 20 20 LL))!=0.
2f500 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
2f510 20 69 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 i & WO_ISNULL )
2f520 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 {. te
2f530 73 74 63 61 73 65 28 20 69 73 4f 72 64 65 72 44 stcase( isOrderD
2f540 69 73 74 69 6e 63 74 20 29 3b 0a 20 20 20 20 20 istinct );.
2f550 20 20 20 20 20 20 20 69 73 4f 72 64 65 72 44 69 isOrderDi
2f560 73 74 69 6e 63 74 20 3d 20 30 3b 0a 20 20 20 20 stinct = 0;.
2f570 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
2f580 20 20 63 6f 6e 74 69 6e 75 65 3b 20 20 0a 20 20 continue; .
2f590 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 }..
2f5a0 20 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6c 75 /* Get the colu
2f5b0 6d 6e 20 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 mn number in the
2f5c0 20 74 61 62 6c 65 20 28 69 43 6f 6c 75 6d 6e 29 table (iColumn)
2f5d0 20 61 6e 64 20 73 6f 72 74 20 6f 72 64 65 72 0a and sort order.
2f5e0 20 20 20 20 20 20 20 20 2a 2a 20 28 72 65 76 49 ** (revI
2f5f0 64 78 29 20 66 6f 72 20 74 68 65 20 6a 2d 74 68 dx) for the j-th
2f600 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 column of the i
2f610 6e 64 65 78 2e 0a 20 20 20 20 20 20 20 20 2a 2f ndex.. */
2f620 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3c 6e . if( j<n
2f630 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 Column ){.
2f640 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 69 6e /* Normal in
2f650 64 65 78 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 dex columns */.
2f660 20 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e iColumn
2f670 20 3d 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c = pIndex->aiCol
2f680 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 umn[j];.
2f690 20 20 72 65 76 49 64 78 20 3d 20 70 49 6e 64 65 revIdx = pInde
2f6a0 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6a 5d x->aSortOrder[j]
2f6b0 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 ;. if(
2f6c0 69 43 6f 6c 75 6d 6e 3d 3d 70 49 6e 64 65 78 2d iColumn==pIndex-
2f6d0 3e 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 29 >pTable->iPKey )
2f6e0 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 iColumn = -1;.
2f6f0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
2f700 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 52 /* The R
2f710 4f 57 49 44 20 63 6f 6c 75 6d 6e 20 61 74 20 74 OWID column at t
2f720 68 65 20 65 6e 64 20 2a 2f 0a 20 20 20 20 20 20 he end */.
2f730 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e assert( j==n
2f740 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20 Column );.
2f750 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 iColumn = -1
2f760 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 76 49 ;. revI
2f770 64 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 dx = 0;.
2f780 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6e }.. /* An
2f790 20 75 6e 63 6f 6e 73 74 72 61 69 6e 65 64 20 63 unconstrained c
2f7a0 6f 6c 75 6d 6e 20 74 68 61 74 20 6d 69 67 68 74 olumn that might
2f7b0 20 62 65 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 be NULL means t
2f7c0 68 61 74 20 74 68 69 73 0a 20 20 20 20 20 20 20 hat this.
2f7d0 20 2a 2a 20 57 68 65 72 65 4c 6f 6f 70 20 69 73 ** WhereLoop is
2f7e0 20 6e 6f 74 20 77 65 6c 6c 2d 6f 72 64 65 72 65 not well-ordere
2f7f0 64 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 d . */.
2f800 20 20 20 20 20 20 69 66 28 20 69 73 4f 72 64 65 if( isOrde
2f810 72 44 69 73 74 69 6e 63 74 0a 20 20 20 20 20 20 rDistinct.
2f820 20 20 20 26 26 20 69 43 6f 6c 75 6d 6e 3e 3d 30 && iColumn>=0
2f830 0a 20 20 20 20 20 20 20 20 20 26 26 20 6a 3e 3d . && j>=
2f840 70 4c 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e pLoop->u.btree.n
2f850 45 71 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 Eq. && p
2f860 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 Index->pTable->a
2f870 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 6e 6f 74 Col[iColumn].not
2f880 4e 75 6c 6c 3d 3d 30 0a 20 20 20 20 20 20 20 20 Null==0.
2f890 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 73 4f ){. isO
2f8a0 72 64 65 72 44 69 73 74 69 6e 63 74 20 3d 20 30 rderDistinct = 0
2f8b0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
2f8c0 20 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 /* Find the
2f8d0 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 74 ORDER BY term t
2f8e0 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 hat corresponds
2f8f0 74 6f 20 74 68 65 20 6a 2d 74 68 20 63 6f 6c 75 to the j-th colu
2f900 6d 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 mn. ** of
2f910 20 74 68 65 20 69 6e 64 65 78 20 61 6e 64 20 61 the index and a
2f920 6e 64 20 6d 61 72 6b 20 74 68 61 74 20 4f 52 44 nd mark that ORD
2f930 45 52 20 42 59 20 74 65 72 6d 20 6f 66 66 20 0a ER BY term off .
2f940 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 */.
2f950 20 20 20 62 4f 6e 63 65 20 3d 20 31 3b 0a 20 20 bOnce = 1;.
2f960 20 20 20 20 20 20 69 73 4d 61 74 63 68 20 3d 20 isMatch =
2f970 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 0;. for(i
2f980 3d 30 3b 20 62 4f 6e 63 65 20 26 26 20 69 3c 6e =0; bOnce && i<n
2f990 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 OrderBy; i++){.
2f9a0 20 20 20 20 20 20 20 20 20 69 66 28 20 4d 41 53 if( MAS
2f9b0 4b 42 49 54 28 69 29 20 26 20 6f 62 53 61 74 20 KBIT(i) & obSat
2f9c0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 ) continue;.
2f9d0 20 20 20 20 20 20 70 4f 42 45 78 70 72 20 3d 20 pOBExpr =
2f9e0 73 71 6c 69 74 65 33 45 78 70 72 53 6b 69 70 43 sqlite3ExprSkipC
2f9f0 6f 6c 6c 61 74 65 28 70 4f 72 64 65 72 42 79 2d ollate(pOrderBy-
2fa00 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 >a[i].pExpr);.
2fa10 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
2fa20 28 20 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 ( wctrlFlags & W
2fa30 48 45 52 45 5f 47 52 4f 55 50 42 59 20 29 3b 0a HERE_GROUPBY );.
2fa40 20 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 testca
2fa50 73 65 28 20 77 63 74 72 6c 46 6c 61 67 73 20 26 se( wctrlFlags &
2fa60 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 42 WHERE_DISTINCTB
2fa70 59 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 Y );. i
2fa80 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 f( (wctrlFlags &
2fa90 20 28 57 48 45 52 45 5f 47 52 4f 55 50 42 59 7c (WHERE_GROUPBY|
2faa0 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 42 59 WHERE_DISTINCTBY
2fab0 29 29 3d 3d 30 20 29 20 62 4f 6e 63 65 20 3d 20 ))==0 ) bOnce =
2fac0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 0;. if(
2fad0 20 70 4f 42 45 78 70 72 2d 3e 6f 70 21 3d 54 4b pOBExpr->op!=TK
2fae0 5f 43 4f 4c 55 4d 4e 20 29 20 63 6f 6e 74 69 6e _COLUMN ) contin
2faf0 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ue;. if
2fb00 28 20 70 4f 42 45 78 70 72 2d 3e 69 54 61 62 6c ( pOBExpr->iTabl
2fb10 65 21 3d 69 43 75 72 20 29 20 63 6f 6e 74 69 6e e!=iCur ) contin
2fb20 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 ue;. if
2fb30 28 20 70 4f 42 45 78 70 72 2d 3e 69 43 6f 6c 75 ( pOBExpr->iColu
2fb40 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 29 20 63 6f mn!=iColumn ) co
2fb50 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 ntinue;.
2fb60 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e 3d 30 if( iColumn>=0
2fb70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2fb80 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 pColl = sqlite3E
2fb90 78 70 72 43 6f 6c 6c 53 65 71 28 70 57 49 6e 66 xprCollSeq(pWInf
2fba0 6f 2d 3e 70 50 61 72 73 65 2c 20 70 4f 72 64 65 o->pParse, pOrde
2fbb0 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 rBy->a[i].pExpr)
2fbc0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
2fbd0 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c ( !pColl ) pColl
2fbe0 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c = db->pDfltColl
2fbf0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 ;. if
2fc00 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 ( sqlite3StrICmp
2fc10 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 70 (pColl->zName, p
2fc20 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d Index->azColl[j]
2fc30 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b )!=0 ) continue;
2fc40 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
2fc50 20 20 20 20 20 20 20 69 73 4d 61 74 63 68 20 3d isMatch =
2fc60 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 1;. br
2fc70 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
2fc80 20 20 20 20 20 20 20 69 66 28 20 69 73 4d 61 74 if( isMat
2fc90 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ch ){.
2fca0 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b if( iColumn<0 ){
2fcb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 73 . tes
2fcc0 74 63 61 73 65 28 20 64 69 73 74 69 6e 63 74 43 tcase( distinctC
2fcd0 6f 6c 75 6d 6e 73 3d 3d 30 20 29 3b 0a 20 20 20 olumns==0 );.
2fce0 20 20 20 20 20 20 20 20 20 64 69 73 74 69 6e 63 distinc
2fcf0 74 43 6f 6c 75 6d 6e 73 20 3d 20 31 3b 0a 20 20 tColumns = 1;.
2fd00 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2fd10 20 20 20 20 6f 62 53 61 74 20 7c 3d 20 4d 41 53 obSat |= MAS
2fd20 4b 42 49 54 28 69 29 3b 0a 20 20 20 20 20 20 20 KBIT(i);.
2fd30 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e if( (pWInfo->
2fd40 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 wctrlFlags & WHE
2fd50 52 45 5f 47 52 4f 55 50 42 59 29 3d 3d 30 20 29 RE_GROUPBY)==0 )
2fd60 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a {. /*
2fd70 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 Make sure the s
2fd80 6f 72 74 20 6f 72 64 65 72 20 69 73 20 63 6f 6d ort order is com
2fd90 70 61 74 69 62 6c 65 20 69 6e 20 61 6e 20 4f 52 patible in an OR
2fda0 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 20 DER BY clause..
2fdb0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 53 6f ** So
2fdc0 72 74 20 6f 72 64 65 72 20 69 73 20 69 72 72 65 rt order is irre
2fdd0 6c 65 76 61 6e 74 20 66 6f 72 20 61 20 47 52 4f levant for a GRO
2fde0 55 50 20 42 59 20 63 6c 61 75 73 65 2e 20 2a 2f UP BY clause. */
2fdf0 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 . if(
2fe00 20 72 65 76 53 65 74 20 29 7b 0a 20 20 20 20 20 revSet ){.
2fe10 20 20 20 20 20 20 20 20 20 69 66 28 20 28 72 65 if( (re
2fe20 76 20 5e 20 72 65 76 49 64 78 29 21 3d 70 4f 72 v ^ revIdx)!=pOr
2fe30 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74 derBy->a[i].sort
2fe40 4f 72 64 65 72 20 29 20 72 65 74 75 72 6e 20 30 Order ) return 0
2fe50 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 ;. }e
2fe60 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
2fe70 20 20 20 72 65 76 20 3d 20 72 65 76 49 64 78 20 rev = revIdx
2fe80 5e 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d ^ pOrderBy->a[i]
2fe90 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 .sortOrder;.
2fea0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 65 if( re
2feb0 76 20 29 20 2a 70 52 65 76 4d 61 73 6b 20 7c 3d v ) *pRevMask |=
2fec0 20 4d 41 53 4b 42 49 54 28 69 4c 6f 6f 70 29 3b MASKBIT(iLoop);
2fed0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 . r
2fee0 65 76 53 65 74 20 3d 20 31 3b 0a 20 20 20 20 20 evSet = 1;.
2fef0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2ff00 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c }. }el
2ff10 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a se{. /*
2ff20 20 4e 6f 20 6d 61 74 63 68 20 66 6f 75 6e 64 20 No match found
2ff30 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 */. if(
2ff40 20 6a 3d 3d 30 20 7c 7c 20 6a 3c 6e 43 6f 6c 75 j==0 || j<nColu
2ff50 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 mn ){.
2ff60 20 20 74 65 73 74 63 61 73 65 28 20 69 73 4f 72 testcase( isOr
2ff70 64 65 72 44 69 73 74 69 6e 63 74 21 3d 30 20 29 derDistinct!=0 )
2ff80 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 73 ;. is
2ff90 4f 72 64 65 72 44 69 73 74 69 6e 63 74 20 3d 20 OrderDistinct =
2ffa0 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 0;. }.
2ffb0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
2ffc0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2ffd0 7d 20 2f 2a 20 65 6e 64 20 4c 6f 6f 70 20 6f 76 } /* end Loop ov
2ffe0 65 72 20 61 6c 6c 20 69 6e 64 65 78 20 63 6f 6c er all index col
2fff0 75 6d 6e 73 20 2a 2f 0a 20 20 20 20 20 20 69 66 umns */. if
30000 28 20 64 69 73 74 69 6e 63 74 43 6f 6c 75 6d 6e ( distinctColumn
30010 73 20 29 7b 0a 20 20 20 20 20 20 20 20 74 65 73 s ){. tes
30020 74 63 61 73 65 28 20 69 73 4f 72 64 65 72 44 69 tcase( isOrderDi
30030 73 74 69 6e 63 74 3d 3d 30 20 29 3b 0a 20 20 20 stinct==0 );.
30040 20 20 20 20 20 69 73 4f 72 64 65 72 44 69 73 74 isOrderDist
30050 69 6e 63 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 inct = 1;.
30060 7d 0a 20 20 20 20 7d 20 2f 2a 20 65 6e 64 2d 69 }. } /* end-i
30070 66 20 6e 6f 74 20 6f 6e 65 2d 72 6f 77 20 2a 2f f not one-row */
30080 0a 0a 20 20 20 20 2f 2a 20 4d 61 72 6b 20 6f 66 .. /* Mark of
30090 66 20 61 6e 79 20 6f 74 68 65 72 20 4f 52 44 45 f any other ORDE
300a0 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 R BY terms that
300b0 72 65 66 65 72 65 6e 63 65 20 70 4c 6f 6f 70 20 reference pLoop
300c0 2a 2f 0a 20 20 20 20 69 66 28 20 69 73 4f 72 64 */. if( isOrd
300d0 65 72 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 erDistinct ){.
300e0 20 20 20 20 6f 72 64 65 72 44 69 73 74 69 6e 63 orderDistinc
300f0 74 4d 61 73 6b 20 7c 3d 20 70 4c 6f 6f 70 2d 3e tMask |= pLoop->
30100 6d 61 73 6b 53 65 6c 66 3b 0a 20 20 20 20 20 20 maskSelf;.
30110 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 for(i=0; i<nOrde
30120 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 rBy; i++){.
30130 20 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 20 20 Expr *p;.
30140 20 20 20 20 69 66 28 20 4d 41 53 4b 42 49 54 28 if( MASKBIT(
30150 69 29 20 26 20 6f 62 53 61 74 20 29 20 63 6f 6e i) & obSat ) con
30160 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 70 tinue;. p
30170 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 = pOrderBy->a[i
30180 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 ].pExpr;.
30190 20 69 66 28 20 28 65 78 70 72 54 61 62 6c 65 55 if( (exprTableU
301a0 73 61 67 65 28 26 70 57 49 6e 66 6f 2d 3e 73 4d sage(&pWInfo->sM
301b0 61 73 6b 53 65 74 2c 20 70 29 26 7e 6f 72 64 65 askSet, p)&~orde
301c0 72 44 69 73 74 69 6e 63 74 4d 61 73 6b 29 3d 3d rDistinctMask)==
301d0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 0 ){. o
301e0 62 53 61 74 20 7c 3d 20 4d 41 53 4b 42 49 54 28 bSat |= MASKBIT(
301f0 69 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 i);. }.
30200 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20 }. }. }
30210 2f 2a 20 45 6e 64 20 74 68 65 20 6c 6f 6f 70 20 /* End the loop
30220 6f 76 65 72 20 61 6c 6c 20 57 68 65 72 65 4c 6f over all WhereLo
30230 6f 70 73 20 66 72 6f 6d 20 6f 75 74 65 72 2d 6d ops from outer-m
30240 6f 73 74 20 64 6f 77 6e 20 74 6f 20 69 6e 6e 65 ost down to inne
30250 72 2d 6d 6f 73 74 20 2a 2f 0a 20 20 69 66 28 20 r-most */. if(
30260 6f 62 53 61 74 3d 3d 6f 62 44 6f 6e 65 20 29 20 obSat==obDone )
30270 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 return 1;. if(
30280 21 69 73 4f 72 64 65 72 44 69 73 74 69 6e 63 74 !isOrderDistinct
30290 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 ) return 0;. r
302a0 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 23 69 66 eturn -1;.}..#if
302b0 64 65 66 20 57 48 45 52 45 54 52 41 43 45 5f 45 def WHERETRACE_E
302c0 4e 41 42 4c 45 44 0a 2f 2a 20 46 6f 72 20 64 65 NABLED./* For de
302d0 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 bugging use only
302e0 3a 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 : */.static cons
302f0 74 20 63 68 61 72 20 2a 77 68 65 72 65 50 61 74 t char *wherePat
30300 68 4e 61 6d 65 28 57 68 65 72 65 50 61 74 68 20 hName(WherePath
30310 2a 70 50 61 74 68 2c 20 69 6e 74 20 6e 4c 6f 6f *pPath, int nLoo
30320 70 2c 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4c p, WhereLoop *pL
30330 61 73 74 29 7b 0a 20 20 73 74 61 74 69 63 20 63 ast){. static c
30340 68 61 72 20 7a 4e 61 6d 65 5b 36 35 5d 3b 0a 20 har zName[65];.
30350 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d int i;. for(i=
30360 30 3b 20 69 3c 6e 4c 6f 6f 70 3b 20 69 2b 2b 29 0; i<nLoop; i++)
30370 7b 20 7a 4e 61 6d 65 5b 69 5d 20 3d 20 70 50 61 { zName[i] = pPa
30380 74 68 2d 3e 61 4c 6f 6f 70 5b 69 5d 2d 3e 63 49 th->aLoop[i]->cI
30390 64 3b 20 7d 0a 20 20 69 66 28 20 70 4c 61 73 74 d; }. if( pLast
303a0 20 29 20 7a 4e 61 6d 65 5b 69 2b 2b 5d 20 3d 20 ) zName[i++] =
303b0 70 4c 61 73 74 2d 3e 63 49 64 3b 0a 20 20 7a 4e pLast->cId;. zN
303c0 61 6d 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 72 65 ame[i] = 0;. re
303d0 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 23 65 turn zName;.}.#e
303e0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 ndif.../*.** Giv
303f0 65 6e 20 74 68 65 20 6c 69 73 74 20 6f 66 20 57 en the list of W
30400 68 65 72 65 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 hereLoop objects
30410 20 61 74 20 70 57 49 6e 66 6f 2d 3e 70 4c 6f 6f at pWInfo->pLoo
30420 70 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 ps, this routine
30430 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 74 6f 20 .** attempts to
30440 66 69 6e 64 20 74 68 65 20 6c 6f 77 65 73 74 20 find the lowest
30450 63 6f 73 74 20 70 61 74 68 20 74 68 61 74 20 76 cost path that v
30460 69 73 69 74 73 20 65 61 63 68 20 57 68 65 72 65 isits each Where
30470 4c 6f 6f 70 0a 2a 2a 20 6f 6e 63 65 2e 20 20 54 Loop.** once. T
30480 68 69 73 20 70 61 74 68 20 69 73 20 74 68 65 6e his path is then
30490 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 65 loaded into the
304a0 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 57 4c pWInfo->a[].pWL
304b0 6f 6f 70 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a oop fields..**.*
304c0 2a 20 41 73 73 75 6d 65 20 74 68 61 74 20 74 68 * Assume that th
304d0 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f e total number o
304e0 66 20 6f 75 74 70 75 74 20 72 6f 77 73 20 74 68 f output rows th
304f0 61 74 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 at will need to
30500 62 65 20 73 6f 72 74 65 64 0a 2a 2a 20 77 69 6c be sorted.** wil
30510 6c 20 62 65 20 6e 52 6f 77 45 73 74 20 28 69 6e l be nRowEst (in
30520 20 74 68 65 20 31 30 2a 6c 6f 67 32 20 72 65 70 the 10*log2 rep
30530 72 65 73 65 6e 74 61 74 69 6f 6e 29 2e 20 20 4f resentation). O
30540 72 2c 20 69 67 6e 6f 72 65 20 73 6f 72 74 69 6e r, ignore sortin
30550 67 0a 2a 2a 20 63 6f 73 74 73 20 69 66 20 6e 52 g.** costs if nR
30560 6f 77 45 73 74 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 owEst==0..**.**
30570 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b Return SQLITE_OK
30580 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 53 on success or S
30590 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 66 20 61 QLITE_NOMEM of a
305a0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
305b0 6f 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 on.** error occu
305c0 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e rs..*/.static in
305d0 74 20 77 68 65 72 65 50 61 74 68 53 6f 6c 76 65 t wherePathSolve
305e0 72 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 r(WhereInfo *pWI
305f0 6e 66 6f 2c 20 4c 6f 67 45 73 74 20 6e 52 6f 77 nfo, LogEst nRow
30600 45 73 74 29 7b 0a 20 20 69 6e 74 20 6d 78 43 68 Est){. int mxCh
30610 6f 69 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 oice;
30620 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d /* Maximum num
30630 62 65 72 20 6f 66 20 73 69 6d 75 6c 74 61 6e 65 ber of simultane
30640 6f 75 73 20 70 61 74 68 73 20 74 72 61 63 6b 65 ous paths tracke
30650 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 70 d */. int nLoop
30660 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
30670 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 /* Number of te
30680 72 6d 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 rms in the join
30690 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 */. Parse *pPar
306a0 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f se; /
306b0 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
306c0 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a t */. sqlite3 *
306d0 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 db;
306e0 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 /* The database
306f0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 connection */.
30700 20 69 6e 74 20 69 4c 6f 6f 70 3b 20 20 20 20 20 int iLoop;
30710 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
30720 6f 70 20 63 6f 75 6e 74 65 72 20 6f 76 65 72 20 op counter over
30730 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
30740 20 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 join */. int i
30750 69 2c 20 6a 6a 3b 20 20 20 20 20 20 20 20 20 20 i, jj;
30760 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 /* Loop cou
30770 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6d nters */. int m
30780 78 49 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 xI = 0;
30790 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 /* Index of
307a0 20 6e 65 78 74 20 65 6e 74 72 79 20 74 6f 20 72 next entry to r
307b0 65 70 6c 61 63 65 20 2a 2f 0a 20 20 4c 6f 67 45 eplace */. LogE
307c0 73 74 20 72 43 6f 73 74 3b 20 20 20 20 20 20 20 st rCost;
307d0 20 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66 /* Cost of
307e0 20 61 20 70 61 74 68 20 2a 2f 0a 20 20 4c 6f 67 a path */. Log
307f0 45 73 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 Est nOut;
30800 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
30810 20 6f 66 20 6f 75 74 70 75 74 73 20 2a 2f 0a 20 of outputs */.
30820 20 4c 6f 67 45 73 74 20 6d 78 43 6f 73 74 20 3d LogEst mxCost =
30830 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 0; /* Ma
30840 78 69 6d 75 6d 20 63 6f 73 74 20 6f 66 20 61 20 ximum cost of a
30850 73 65 74 20 6f 66 20 70 61 74 68 73 20 2a 2f 0a set of paths */.
30860 20 20 4c 6f 67 45 73 74 20 6d 78 4f 75 74 20 3d LogEst mxOut =
30870 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 0; /* M
30880 61 78 69 6d 75 6d 20 6e 4f 75 74 20 76 61 6c 75 aximum nOut valu
30890 65 20 6f 6e 20 74 68 65 20 73 65 74 20 6f 66 20 e on the set of
308a0 70 61 74 68 73 20 2a 2f 0a 20 20 4c 6f 67 45 73 paths */. LogEs
308b0 74 20 72 53 6f 72 74 43 6f 73 74 3b 20 20 20 20 t rSortCost;
308c0 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 74 6f 20 /* Cost to
308d0 64 6f 20 61 20 73 6f 72 74 20 2a 2f 0a 20 20 69 do a sort */. i
308e0 6e 74 20 6e 54 6f 2c 20 6e 46 72 6f 6d 3b 20 20 nt nTo, nFrom;
308f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 /* Numb
30900 65 72 20 6f 66 20 76 61 6c 69 64 20 65 6e 74 72 er of valid entr
30910 69 65 73 20 69 6e 20 61 54 6f 5b 5d 20 61 6e 64 ies in aTo[] and
30920 20 61 46 72 6f 6d 5b 5d 20 2a 2f 0a 20 20 57 68 aFrom[] */. Wh
30930 65 72 65 50 61 74 68 20 2a 61 46 72 6f 6d 3b 20 erePath *aFrom;
30940 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 6e /* All n
30950 46 72 6f 6d 20 70 61 74 68 73 20 61 74 20 74 68 From paths at th
30960 65 20 70 72 65 76 69 6f 75 73 20 6c 65 76 65 6c e previous level
30970 20 2a 2f 0a 20 20 57 68 65 72 65 50 61 74 68 20 */. WherePath
30980 2a 61 54 6f 3b 20 20 20 20 20 20 20 20 20 20 20 *aTo;
30990 2f 2a 20 54 68 65 20 6e 54 6f 20 62 65 73 74 20 /* The nTo best
309a0 70 61 74 68 73 20 61 74 20 74 68 65 20 63 75 72 paths at the cur
309b0 72 65 6e 74 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 rent level */.
309c0 57 68 65 72 65 50 61 74 68 20 2a 70 46 72 6f 6d WherePath *pFrom
309d0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 ; /* An
309e0 65 6c 65 6d 65 6e 74 20 6f 66 20 61 46 72 6f 6d element of aFrom
309f0 5b 5d 20 74 68 61 74 20 77 65 20 61 72 65 20 77 [] that we are w
30a00 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f 0a 20 20 57 orking on */. W
30a10 68 65 72 65 50 61 74 68 20 2a 70 54 6f 3b 20 20 herePath *pTo;
30a20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 /* An e
30a30 6c 65 6d 65 6e 74 20 6f 66 20 61 54 6f 5b 5d 20 lement of aTo[]
30a40 74 68 61 74 20 77 65 20 61 72 65 20 77 6f 72 6b that we are work
30a50 69 6e 67 20 6f 6e 20 2a 2f 0a 20 20 57 68 65 72 ing on */. Wher
30a60 65 4c 6f 6f 70 20 2a 70 57 4c 6f 6f 70 3b 20 20 eLoop *pWLoop;
30a70 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 /* One of
30a80 74 68 65 20 57 68 65 72 65 4c 6f 6f 70 20 6f 62 the WhereLoop ob
30a90 6a 65 63 74 73 20 2a 2f 0a 20 20 57 68 65 72 65 jects */. Where
30aa0 4c 6f 6f 70 20 2a 2a 70 58 3b 20 20 20 20 20 20 Loop **pX;
30ab0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 /* Used to
30ac0 64 69 76 79 20 75 70 20 74 68 65 20 70 53 70 61 divy up the pSpa
30ad0 63 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 ce memory */. c
30ae0 68 61 72 20 2a 70 53 70 61 63 65 3b 20 20 20 20 har *pSpace;
30af0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 /* Temp
30b00 6f 72 61 72 79 20 6d 65 6d 6f 72 79 20 75 73 65 orary memory use
30b10 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e d by this routin
30b20 65 20 2a 2f 0a 0a 20 20 70 50 61 72 73 65 20 3d e */.. pParse =
30b30 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 3b pWInfo->pParse;
30b40 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e . db = pParse->
30b50 64 62 3b 0a 20 20 6e 4c 6f 6f 70 20 3d 20 70 57 db;. nLoop = pW
30b60 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 0a 20 20 Info->nLevel;.
30b70 2f 2a 20 54 55 4e 49 4e 47 3a 20 46 6f 72 20 73 /* TUNING: For s
30b80 69 6d 70 6c 65 20 71 75 65 72 69 65 73 2c 20 6f imple queries, o
30b90 6e 6c 79 20 74 68 65 20 62 65 73 74 20 70 61 74 nly the best pat
30ba0 68 20 69 73 20 74 72 61 63 6b 65 64 2e 0a 20 20 h is tracked..
30bb0 2a 2a 20 46 6f 72 20 32 2d 77 61 79 20 6a 6f 69 ** For 2-way joi
30bc0 6e 73 2c 20 74 68 65 20 35 20 62 65 73 74 20 70 ns, the 5 best p
30bd0 61 74 68 73 20 61 72 65 20 66 6f 6c 6c 6f 77 65 aths are followe
30be0 64 2e 0a 20 20 2a 2a 20 46 6f 72 20 6a 6f 69 6e d.. ** For join
30bf0 73 20 6f 66 20 33 20 6f 72 20 6d 6f 72 65 20 74 s of 3 or more t
30c00 61 62 6c 65 73 2c 20 74 72 61 63 6b 20 74 68 65 ables, track the
30c10 20 31 30 20 62 65 73 74 20 70 61 74 68 73 20 2a 10 best paths *
30c20 2f 0a 20 20 6d 78 43 68 6f 69 63 65 20 3d 20 28 /. mxChoice = (
30c30 6e 4c 6f 6f 70 3d 3d 31 29 20 3f 20 31 20 3a 20 nLoop==1) ? 1 :
30c40 28 6e 4c 6f 6f 70 3d 3d 32 20 3f 20 35 20 3a 20 (nLoop==2 ? 5 :
30c50 31 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 10);. assert( n
30c60 4c 6f 6f 70 3c 3d 70 57 49 6e 66 6f 2d 3e 70 54 Loop<=pWInfo->pT
30c70 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a abList->nSrc );.
30c80 20 20 57 48 45 52 45 54 52 41 43 45 28 30 78 30 WHERETRACE(0x0
30c90 30 32 2c 20 28 22 2d 2d 2d 2d 20 62 65 67 69 6e 02, ("---- begin
30ca0 20 73 6f 6c 76 65 72 5c 6e 22 29 29 3b 0a 0a 20 solver\n"));..
30cb0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 /* Allocate and
30cc0 20 69 6e 69 74 69 61 6c 69 7a 65 20 73 70 61 63 initialize spac
30cd0 65 20 66 6f 72 20 61 54 6f 20 61 6e 64 20 61 46 e for aTo and aF
30ce0 72 6f 6d 20 2a 2f 0a 20 20 69 69 20 3d 20 28 73 rom */. ii = (s
30cf0 69 7a 65 6f 66 28 57 68 65 72 65 50 61 74 68 29 izeof(WherePath)
30d00 2b 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 6f 6f +sizeof(WhereLoo
30d10 70 2a 29 2a 6e 4c 6f 6f 70 29 2a 6d 78 43 68 6f p*)*nLoop)*mxCho
30d20 69 63 65 2a 32 3b 0a 20 20 70 53 70 61 63 65 20 ice*2;. pSpace
30d30 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f = sqlite3DbMallo
30d40 63 52 61 77 28 64 62 2c 20 69 69 29 3b 0a 20 20 cRaw(db, ii);.
30d50 69 66 28 20 70 53 70 61 63 65 3d 3d 30 20 29 20 if( pSpace==0 )
30d60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f return SQLITE_NO
30d70 4d 45 4d 3b 0a 20 20 61 54 6f 20 3d 20 28 57 68 MEM;. aTo = (Wh
30d80 65 72 65 50 61 74 68 2a 29 70 53 70 61 63 65 3b erePath*)pSpace;
30d90 0a 20 20 61 46 72 6f 6d 20 3d 20 61 54 6f 2b 6d . aFrom = aTo+m
30da0 78 43 68 6f 69 63 65 3b 0a 20 20 6d 65 6d 73 65 xChoice;. memse
30db0 74 28 61 46 72 6f 6d 2c 20 30 2c 20 73 69 7a 65 t(aFrom, 0, size
30dc0 6f 66 28 61 46 72 6f 6d 5b 30 5d 29 29 3b 0a 20 of(aFrom[0]));.
30dd0 20 70 58 20 3d 20 28 57 68 65 72 65 4c 6f 6f 70 pX = (WhereLoop
30de0 2a 2a 29 28 61 46 72 6f 6d 2b 6d 78 43 68 6f 69 **)(aFrom+mxChoi
30df0 63 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 6d 78 ce);. for(ii=mx
30e00 43 68 6f 69 63 65 2a 32 2c 20 70 46 72 6f 6d 3d Choice*2, pFrom=
30e10 61 54 6f 3b 20 69 69 3e 30 3b 20 69 69 2d 2d 2c aTo; ii>0; ii--,
30e20 20 70 46 72 6f 6d 2b 2b 2c 20 70 58 20 2b 3d 20 pFrom++, pX +=
30e30 6e 4c 6f 6f 70 29 7b 0a 20 20 20 20 70 46 72 6f nLoop){. pFro
30e40 6d 2d 3e 61 4c 6f 6f 70 20 3d 20 70 58 3b 0a 20 m->aLoop = pX;.
30e50 20 7d 0a 0a 20 20 2f 2a 20 53 65 65 64 20 74 68 }.. /* Seed th
30e60 65 20 73 65 61 72 63 68 20 77 69 74 68 20 61 20 e search with a
30e70 73 69 6e 67 6c 65 20 57 68 65 72 65 50 61 74 68 single WherePath
30e80 20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 65 72 6f containing zero
30e90 20 57 68 65 72 65 4c 6f 6f 70 73 2e 0a 20 20 2a WhereLoops.. *
30ea0 2a 0a 20 20 2a 2a 20 54 55 4e 49 4e 47 3a 20 44 *. ** TUNING: D
30eb0 6f 20 6e 6f 74 20 6c 65 74 20 74 68 65 20 6e 75 o not let the nu
30ec0 6d 62 65 72 20 6f 66 20 69 74 65 72 61 74 69 6f mber of iteratio
30ed0 6e 73 20 67 6f 20 61 62 6f 76 65 20 32 35 2e 20 ns go above 25.
30ee0 20 49 66 20 74 68 65 20 63 6f 73 74 0a 20 20 2a If the cost. *
30ef0 2a 20 6f 66 20 63 6f 6d 70 75 74 69 6e 67 20 61 * of computing a
30f00 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 n automatic inde
30f10 78 20 69 73 20 6e 6f 74 20 70 61 69 64 20 62 61 x is not paid ba
30f20 63 6b 20 77 69 74 68 69 6e 20 74 68 65 20 66 69 ck within the fi
30f30 72 73 74 20 32 35 0a 20 20 2a 2a 20 72 6f 77 73 rst 25. ** rows
30f40 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 75 73 , then do not us
30f50 65 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 e the automatic
30f60 69 6e 64 65 78 2e 20 2a 2f 0a 20 20 61 46 72 6f index. */. aFro
30f70 6d 5b 30 5d 2e 6e 52 6f 77 20 3d 20 4d 49 4e 28 m[0].nRow = MIN(
30f80 70 50 61 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f pParse->nQueryLo
30f90 6f 70 2c 20 34 36 29 3b 20 20 61 73 73 65 72 74 op, 46); assert
30fa0 28 20 34 36 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 ( 46==sqlite3Log
30fb0 45 73 74 28 32 35 29 20 29 3b 0a 20 20 6e 46 72 Est(25) );. nFr
30fc0 6f 6d 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 50 72 om = 1;.. /* Pr
30fd0 65 63 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 73 ecompute the cos
30fe0 74 20 6f 66 20 73 6f 72 74 69 6e 67 20 74 68 65 t of sorting the
30ff0 20 66 69 6e 61 6c 20 72 65 73 75 6c 74 20 73 65 final result se
31000 74 2c 20 69 66 20 74 68 65 20 63 61 6c 6c 65 72 t, if the caller
31010 0a 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 . ** to sqlite3
31020 57 68 65 72 65 42 65 67 69 6e 28 29 20 77 61 73 WhereBegin() was
31030 20 63 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 concerned about
31040 20 73 6f 72 74 69 6e 67 20 2a 2f 0a 20 20 72 53 sorting */. rS
31050 6f 72 74 43 6f 73 74 20 3d 20 30 3b 0a 20 20 69 ortCost = 0;. i
31060 66 28 20 70 57 49 6e 66 6f 2d 3e 70 4f 72 64 65 f( pWInfo->pOrde
31070 72 42 79 3d 3d 30 20 7c 7c 20 6e 52 6f 77 45 73 rBy==0 || nRowEs
31080 74 3d 3d 30 20 29 7b 0a 20 20 20 20 61 46 72 6f t==0 ){. aFro
31090 6d 5b 30 5d 2e 69 73 4f 72 64 65 72 65 64 56 61 m[0].isOrderedVa
310a0 6c 69 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 lid = 1;. }else
310b0 7b 0a 20 20 20 20 2f 2a 20 54 55 4e 49 4e 47 3a {. /* TUNING:
310c0 20 45 73 74 69 6d 61 74 65 64 20 63 6f 73 74 20 Estimated cost
310d0 6f 66 20 73 6f 72 74 69 6e 67 20 69 73 20 34 38 of sorting is 48
310e0 2a 4e 2a 6c 6f 67 32 28 4e 29 20 77 68 65 72 65 *N*log2(N) where
310f0 20 4e 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a N is the. **
31100 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 number of outpu
31110 74 20 72 6f 77 73 2e 20 54 68 65 20 34 38 20 69 t rows. The 48 i
31120 73 20 74 68 65 20 65 78 70 65 63 74 65 64 20 73 s the expected s
31130 69 7a 65 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 ize of a row to
31140 73 6f 72 74 2e 20 0a 20 20 20 20 2a 2a 20 46 49 sort. . ** FI
31150 58 4d 45 3a 20 20 63 6f 6d 70 75 74 65 20 61 20 XME: compute a
31160 62 65 74 74 65 72 20 65 73 74 69 6d 61 74 65 20 better estimate
31170 6f 66 20 74 68 65 20 34 38 20 6d 75 6c 74 69 70 of the 48 multip
31180 6c 69 65 72 20 62 61 73 65 64 20 6f 6e 20 74 68 lier based on th
31190 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 20 e. ** result
311a0 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e set expressions.
311b0 20 2a 2f 0a 20 20 20 20 72 53 6f 72 74 43 6f 73 */. rSortCos
311c0 74 20 3d 20 6e 52 6f 77 45 73 74 20 2b 20 65 73 t = nRowEst + es
311d0 74 4c 6f 67 28 6e 52 6f 77 45 73 74 29 3b 0a 20 tLog(nRowEst);.
311e0 20 20 20 57 48 45 52 45 54 52 41 43 45 28 30 78 WHERETRACE(0x
311f0 30 30 32 2c 28 22 2d 2d 2d 2d 20 73 6f 72 74 20 002,("---- sort
31200 63 6f 73 74 3d 25 2d 33 64 5c 6e 22 2c 20 72 53 cost=%-3d\n", rS
31210 6f 72 74 43 6f 73 74 29 29 3b 0a 20 20 7d 0a 0a ortCost));. }..
31220 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 73 75 63 /* Compute suc
31230 63 65 73 73 69 76 65 6c 79 20 6c 6f 6e 67 65 72 cessively longer
31240 20 57 68 65 72 65 50 61 74 68 73 20 75 73 69 6e WherePaths usin
31250 67 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 67 g the previous g
31260 65 6e 65 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 6f eneration. ** o
31270 66 20 57 68 65 72 65 50 61 74 68 73 20 61 73 20 f WherePaths as
31280 74 68 65 20 62 61 73 69 73 20 66 6f 72 20 74 68 the basis for th
31290 65 20 6e 65 78 74 2e 20 20 4b 65 65 70 20 74 72 e next. Keep tr
312a0 61 63 6b 20 6f 66 20 74 68 65 20 6d 78 43 68 6f ack of the mxCho
312b0 69 63 65 0a 20 20 2a 2a 20 62 65 73 74 20 70 61 ice. ** best pa
312c0 74 68 73 20 61 74 20 65 61 63 68 20 67 65 6e 65 ths at each gene
312d0 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 66 6f 72 28 ration */. for(
312e0 69 4c 6f 6f 70 3d 30 3b 20 69 4c 6f 6f 70 3c 6e iLoop=0; iLoop<n
312f0 4c 6f 6f 70 3b 20 69 4c 6f 6f 70 2b 2b 29 7b 0a Loop; iLoop++){.
31300 20 20 20 20 6e 54 6f 20 3d 20 30 3b 0a 20 20 20 nTo = 0;.
31310 20 66 6f 72 28 69 69 3d 30 2c 20 70 46 72 6f 6d for(ii=0, pFrom
31320 3d 61 46 72 6f 6d 3b 20 69 69 3c 6e 46 72 6f 6d =aFrom; ii<nFrom
31330 3b 20 69 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 ; ii++, pFrom++)
31340 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 57 4c 6f {. for(pWLo
31350 6f 70 3d 70 57 49 6e 66 6f 2d 3e 70 4c 6f 6f 70 op=pWInfo->pLoop
31360 73 3b 20 70 57 4c 6f 6f 70 3b 20 70 57 4c 6f 6f s; pWLoop; pWLoo
31370 70 3d 70 57 4c 6f 6f 70 2d 3e 70 4e 65 78 74 4c p=pWLoop->pNextL
31380 6f 6f 70 29 7b 0a 20 20 20 20 20 20 20 20 42 69 oop){. Bi
31390 74 6d 61 73 6b 20 6d 61 73 6b 4e 65 77 3b 0a 20 tmask maskNew;.
313a0 20 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 72 Bitmask r
313b0 65 76 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 20 20 evMask = 0;.
313c0 20 20 20 20 75 38 20 69 73 4f 72 64 65 72 65 64 u8 isOrdered
313d0 56 61 6c 69 64 20 3d 20 70 46 72 6f 6d 2d 3e 69 Valid = pFrom->i
313e0 73 4f 72 64 65 72 65 64 56 61 6c 69 64 3b 0a 20 sOrderedValid;.
313f0 20 20 20 20 20 20 20 75 38 20 69 73 4f 72 64 65 u8 isOrde
31400 72 65 64 20 3d 20 70 46 72 6f 6d 2d 3e 69 73 4f red = pFrom->isO
31410 72 64 65 72 65 64 3b 0a 20 20 20 20 20 20 20 20 rdered;.
31420 69 66 28 20 28 70 57 4c 6f 6f 70 2d 3e 70 72 65 if( (pWLoop->pre
31430 72 65 71 20 26 20 7e 70 46 72 6f 6d 2d 3e 6d 61 req & ~pFrom->ma
31440 73 6b 4c 6f 6f 70 29 21 3d 30 20 29 20 63 6f 6e skLoop)!=0 ) con
31450 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 tinue;. i
31460 66 28 20 28 70 57 4c 6f 6f 70 2d 3e 6d 61 73 6b f( (pWLoop->mask
31470 53 65 6c 66 20 26 20 70 46 72 6f 6d 2d 3e 6d 61 Self & pFrom->ma
31480 73 6b 4c 6f 6f 70 29 21 3d 30 20 29 20 63 6f 6e skLoop)!=0 ) con
31490 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 2f tinue;. /
314a0 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c * At this point,
314b0 20 70 57 4c 6f 6f 70 20 69 73 20 61 20 63 61 6e pWLoop is a can
314c0 64 69 64 61 74 65 20 74 6f 20 62 65 20 74 68 65 didate to be the
314d0 20 6e 65 78 74 20 6c 6f 6f 70 2e 20 0a 20 20 20 next loop. .
314e0 20 20 20 20 20 2a 2a 20 43 6f 6d 70 75 74 65 20 ** Compute
314f0 69 74 73 20 63 6f 73 74 20 2a 2f 0a 20 20 20 20 its cost */.
31500 20 20 20 20 72 43 6f 73 74 20 3d 20 73 71 6c 69 rCost = sqli
31510 74 65 33 4c 6f 67 45 73 74 41 64 64 28 70 57 4c te3LogEstAdd(pWL
31520 6f 6f 70 2d 3e 72 53 65 74 75 70 2c 70 57 4c 6f oop->rSetup,pWLo
31530 6f 70 2d 3e 72 52 75 6e 20 2b 20 70 46 72 6f 6d op->rRun + pFrom
31540 2d 3e 6e 52 6f 77 29 3b 0a 20 20 20 20 20 20 20 ->nRow);.
31550 20 72 43 6f 73 74 20 3d 20 73 71 6c 69 74 65 33 rCost = sqlite3
31560 4c 6f 67 45 73 74 41 64 64 28 72 43 6f 73 74 2c LogEstAdd(rCost,
31570 20 70 46 72 6f 6d 2d 3e 72 43 6f 73 74 29 3b 0a pFrom->rCost);.
31580 20 20 20 20 20 20 20 20 6e 4f 75 74 20 3d 20 70 nOut = p
31590 46 72 6f 6d 2d 3e 6e 52 6f 77 20 2b 20 70 57 4c From->nRow + pWL
315a0 6f 6f 70 2d 3e 6e 4f 75 74 3b 0a 20 20 20 20 20 oop->nOut;.
315b0 20 20 20 6d 61 73 6b 4e 65 77 20 3d 20 70 46 72 maskNew = pFr
315c0 6f 6d 2d 3e 6d 61 73 6b 4c 6f 6f 70 20 7c 20 70 om->maskLoop | p
315d0 57 4c 6f 6f 70 2d 3e 6d 61 73 6b 53 65 6c 66 3b WLoop->maskSelf;
315e0 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73 . if( !is
315f0 4f 72 64 65 72 65 64 56 61 6c 69 64 20 29 7b 0a OrderedValid ){.
31600 20 20 20 20 20 20 20 20 20 20 73 77 69 74 63 68 switch
31610 28 20 77 68 65 72 65 50 61 74 68 53 61 74 69 73 ( wherePathSatis
31620 66 69 65 73 4f 72 64 65 72 42 79 28 70 57 49 6e fiesOrderBy(pWIn
31630 66 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 fo,.
31640 20 20 20 20 20 20 20 20 20 20 20 70 57 49 6e 66 pWInf
31650 6f 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 46 72 o->pOrderBy, pFr
31660 6f 6d 2c 20 70 57 49 6e 66 6f 2d 3e 77 63 74 72 om, pWInfo->wctr
31670 6c 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 lFlags,.
31680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 i
31690 4c 6f 6f 70 2c 20 70 57 4c 6f 6f 70 2c 20 26 72 Loop, pWLoop, &r
316a0 65 76 4d 61 73 6b 29 20 29 7b 0a 20 20 20 20 20 evMask) ){.
316b0 20 20 20 20 20 20 20 63 61 73 65 20 31 3a 20 20 case 1:
316c0 2f 2a 20 59 65 73 2e 20 20 70 46 72 6f 6d 2b 70 /* Yes. pFrom+p
316d0 57 4c 6f 6f 70 20 64 6f 65 73 20 73 61 74 69 73 WLoop does satis
316e0 66 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 fy the ORDER BY
316f0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 clause */.
31700 20 20 20 20 20 20 20 20 69 73 4f 72 64 65 72 65 isOrdere
31710 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 d = 1;.
31720 20 20 20 20 20 69 73 4f 72 64 65 72 65 64 56 61 isOrderedVa
31730 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 lid = 1;.
31740 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
31750 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 30 case 0
31760 3a 20 20 2f 2a 20 4e 6f 2e 20 20 70 46 72 6f 6d : /* No. pFrom
31770 2b 70 57 4c 6f 6f 70 20 77 69 6c 6c 20 72 65 71 +pWLoop will req
31780 75 69 72 65 20 61 20 73 65 70 61 72 61 74 65 20 uire a separate
31790 73 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 sort */.
317a0 20 20 20 20 20 20 69 73 4f 72 64 65 72 65 64 20 isOrdered
317b0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 = 0;.
317c0 20 20 20 69 73 4f 72 64 65 72 65 64 56 61 6c 69 isOrderedVali
317d0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 d = 1;.
317e0 20 20 20 20 20 72 43 6f 73 74 20 3d 20 73 71 6c rCost = sql
317f0 69 74 65 33 4c 6f 67 45 73 74 41 64 64 28 72 43 ite3LogEstAdd(rC
31800 6f 73 74 2c 20 72 53 6f 72 74 43 6f 73 74 29 3b ost, rSortCost);
31810 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 . b
31820 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 reak;.
31830 20 20 64 65 66 61 75 6c 74 3a 20 2f 2a 20 43 61 default: /* Ca
31840 6e 6e 6f 74 20 74 65 6c 6c 20 79 65 74 2e 20 20 nnot tell yet.
31850 54 72 79 20 61 67 61 69 6e 20 6f 6e 20 74 68 65 Try again on the
31860 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 next iteration
31870 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 */.
31880 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
31890 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 }. }els
318a0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 76 e{. rev
318b0 4d 61 73 6b 20 3d 20 70 46 72 6f 6d 2d 3e 72 65 Mask = pFrom->re
318c0 76 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 20 20 7d vLoop;. }
318d0 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 . /* Chec
318e0 6b 20 74 6f 20 73 65 65 20 69 66 20 70 57 4c 6f k to see if pWLo
318f0 6f 70 20 73 68 6f 75 6c 64 20 62 65 20 61 64 64 op should be add
31900 65 64 20 74 6f 20 74 68 65 20 6d 78 43 68 6f 69 ed to the mxChoi
31910 63 65 20 62 65 73 74 20 73 6f 20 66 61 72 20 2a ce best so far *
31920 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 6a /. for(jj
31930 3d 30 2c 20 70 54 6f 3d 61 54 6f 3b 20 6a 6a 3c =0, pTo=aTo; jj<
31940 6e 54 6f 3b 20 6a 6a 2b 2b 2c 20 70 54 6f 2b 2b nTo; jj++, pTo++
31950 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ){. if(
31960 20 70 54 6f 2d 3e 6d 61 73 6b 4c 6f 6f 70 3d 3d pTo->maskLoop==
31970 6d 61 73 6b 4e 65 77 0a 20 20 20 20 20 20 20 20 maskNew.
31980 20 20 20 26 26 20 70 54 6f 2d 3e 69 73 4f 72 64 && pTo->isOrd
31990 65 72 65 64 56 61 6c 69 64 3d 3d 69 73 4f 72 64 eredValid==isOrd
319a0 65 72 65 64 56 61 6c 69 64 0a 20 20 20 20 20 20 eredValid.
319b0 20 20 20 20 20 26 26 20 28 28 70 54 6f 2d 3e 72 && ((pTo->r
319c0 43 6f 73 74 3c 3d 72 43 6f 73 74 20 26 26 20 70 Cost<=rCost && p
319d0 54 6f 2d 3e 6e 52 6f 77 3c 3d 6e 4f 75 74 29 20 To->nRow<=nOut)
319e0 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ||.
319f0 20 20 20 28 70 54 6f 2d 3e 72 43 6f 73 74 3e 3d (pTo->rCost>=
31a00 72 43 6f 73 74 20 26 26 20 70 54 6f 2d 3e 6e 52 rCost && pTo->nR
31a10 6f 77 3e 3d 6e 4f 75 74 29 29 0a 20 20 20 20 20 ow>=nOut)).
31a20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
31a30 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 6a testcase( jj
31a40 3d 3d 6e 54 6f 2d 31 20 29 3b 0a 20 20 20 20 20 ==nTo-1 );.
31a50 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 break;.
31a60 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
31a70 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 }. if(
31a80 6a 6a 3e 3d 6e 54 6f 20 29 7b 0a 20 20 20 20 20 jj>=nTo ){.
31a90 20 20 20 20 20 69 66 28 20 6e 54 6f 3e 3d 6d 78 if( nTo>=mx
31aa0 43 68 6f 69 63 65 20 26 26 20 72 43 6f 73 74 3e Choice && rCost>
31ab0 3d 6d 78 43 6f 73 74 20 29 7b 0a 23 69 66 64 65 =mxCost ){.#ifde
31ac0 66 20 57 48 45 52 45 54 52 41 43 45 5f 45 4e 41 f WHERETRACE_ENA
31ad0 42 4c 45 44 20 2f 2a 20 30 78 34 20 2a 2f 0a 20 BLED /* 0x4 */.
31ae0 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 if( s
31af0 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65 qlite3WhereTrace
31b00 26 30 78 34 20 29 7b 0a 20 20 20 20 20 20 20 20 &0x4 ){.
31b10 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 sqlite3Deb
31b20 75 67 50 72 69 6e 74 66 28 22 53 6b 69 70 20 20 ugPrintf("Skip
31b30 20 25 73 20 63 6f 73 74 3d 25 2d 33 64 2c 25 33 %s cost=%-3d,%3
31b40 64 20 6f 72 64 65 72 3d 25 63 5c 6e 22 2c 0a 20 d order=%c\n",.
31b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
31b60 20 77 68 65 72 65 50 61 74 68 4e 61 6d 65 28 70 wherePathName(p
31b70 46 72 6f 6d 2c 20 69 4c 6f 6f 70 2c 20 70 57 4c From, iLoop, pWL
31b80 6f 6f 70 29 2c 20 72 43 6f 73 74 2c 20 6e 4f 75 oop), rCost, nOu
31b90 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 t,.
31ba0 20 20 20 20 20 69 73 4f 72 64 65 72 65 64 56 61 isOrderedVa
31bb0 6c 69 64 20 3f 20 28 69 73 4f 72 64 65 72 65 64 lid ? (isOrdered
31bc0 20 3f 20 27 59 27 20 3a 20 27 4e 27 29 20 3a 20 ? 'Y' : 'N') :
31bd0 27 3f 27 29 3b 0a 20 20 20 20 20 20 20 20 20 20 '?');.
31be0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 }.#endif.
31bf0 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b continue;
31c00 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
31c10 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 61 20 /* Add a
31c20 6e 65 77 20 50 61 74 68 20 74 6f 20 74 68 65 20 new Path to the
31c30 61 54 6f 5b 5d 20 73 65 74 20 2a 2f 0a 20 20 20 aTo[] set */.
31c40 20 20 20 20 20 20 20 69 66 28 20 6e 54 6f 3c 6d if( nTo<m
31c50 78 43 68 6f 69 63 65 20 29 7b 0a 20 20 20 20 20 xChoice ){.
31c60 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 61 /* Increa
31c70 73 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 se the size of t
31c80 68 65 20 61 54 6f 20 73 65 74 20 62 79 20 6f 6e he aTo set by on
31c90 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 e */.
31ca0 20 6a 6a 20 3d 20 6e 54 6f 2b 2b 3b 0a 20 20 20 jj = nTo++;.
31cb0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
31cc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 /* New
31cd0 20 70 61 74 68 20 72 65 70 6c 61 63 65 73 20 74 path replaces t
31ce0 68 65 20 70 72 69 6f 72 20 77 6f 72 73 74 20 74 he prior worst t
31cf0 6f 20 6b 65 65 70 20 63 6f 75 6e 74 20 62 65 6c o keep count bel
31d00 6f 77 20 6d 78 43 68 6f 69 63 65 20 2a 2f 0a 20 ow mxChoice */.
31d10 20 20 20 20 20 20 20 20 20 20 20 6a 6a 20 3d 20 jj =
31d20 6d 78 49 3b 0a 20 20 20 20 20 20 20 20 20 20 7d mxI;. }
31d30 0a 20 20 20 20 20 20 20 20 20 20 70 54 6f 20 3d . pTo =
31d40 20 26 61 54 6f 5b 6a 6a 5d 3b 0a 23 69 66 64 65 &aTo[jj];.#ifde
31d50 66 20 57 48 45 52 45 54 52 41 43 45 5f 45 4e 41 f WHERETRACE_ENA
31d60 42 4c 45 44 20 2f 2a 20 30 78 34 20 2a 2f 0a 20 BLED /* 0x4 */.
31d70 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c if( sql
31d80 69 74 65 33 57 68 65 72 65 54 72 61 63 65 26 30 ite3WhereTrace&0
31d90 78 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 x4 ){.
31da0 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 sqlite3DebugPr
31db0 69 6e 74 66 28 22 4e 65 77 20 20 20 20 25 73 20 intf("New %s
31dc0 63 6f 73 74 3d 25 2d 33 64 2c 25 33 64 20 6f 72 cost=%-3d,%3d or
31dd0 64 65 72 3d 25 63 5c 6e 22 2c 0a 20 20 20 20 20 der=%c\n",.
31de0 20 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65 where
31df0 50 61 74 68 4e 61 6d 65 28 70 46 72 6f 6d 2c 20 PathName(pFrom,
31e00 69 4c 6f 6f 70 2c 20 70 57 4c 6f 6f 70 29 2c 20 iLoop, pWLoop),
31e10 72 43 6f 73 74 2c 20 6e 4f 75 74 2c 0a 20 20 20 rCost, nOut,.
31e20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 4f isO
31e30 72 64 65 72 65 64 56 61 6c 69 64 20 3f 20 28 69 rderedValid ? (i
31e40 73 4f 72 64 65 72 65 64 20 3f 20 27 59 27 20 3a sOrdered ? 'Y' :
31e50 20 27 4e 27 29 20 3a 20 27 3f 27 29 3b 0a 20 20 'N') : '?');.
31e60 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 }.#endif
31e70 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a . }else{.
31e80 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 54 if( pT
31e90 6f 2d 3e 72 43 6f 73 74 3c 3d 72 43 6f 73 74 20 o->rCost<=rCost
31ea0 26 26 20 70 54 6f 2d 3e 6e 52 6f 77 3c 3d 6e 4f && pTo->nRow<=nO
31eb0 75 74 20 29 7b 0a 23 69 66 64 65 66 20 57 48 45 ut ){.#ifdef WHE
31ec0 52 45 54 52 41 43 45 5f 45 4e 41 42 4c 45 44 20 RETRACE_ENABLED
31ed0 2f 2a 20 30 78 34 20 2a 2f 0a 20 20 20 20 20 20 /* 0x4 */.
31ee0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
31ef0 33 57 68 65 72 65 54 72 61 63 65 26 30 78 34 20 3WhereTrace&0x4
31f00 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 ){.
31f10 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
31f20 6e 74 66 28 0a 20 20 20 20 20 20 20 20 20 20 20 ntf(.
31f30 20 20 20 20 20 20 20 22 53 6b 69 70 20 20 20 25 "Skip %
31f40 73 20 63 6f 73 74 3d 25 2d 33 64 2c 25 33 64 20 s cost=%-3d,%3d
31f50 6f 72 64 65 72 3d 25 63 22 2c 0a 20 20 20 20 20 order=%c",.
31f60 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 65 whe
31f70 72 65 50 61 74 68 4e 61 6d 65 28 70 46 72 6f 6d rePathName(pFrom
31f80 2c 20 69 4c 6f 6f 70 2c 20 70 57 4c 6f 6f 70 29 , iLoop, pWLoop)
31f90 2c 20 72 43 6f 73 74 2c 20 6e 4f 75 74 2c 0a 20 , rCost, nOut,.
31fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
31fb0 20 69 73 4f 72 64 65 72 65 64 56 61 6c 69 64 20 isOrderedValid
31fc0 3f 20 28 69 73 4f 72 64 65 72 65 64 20 3f 20 27 ? (isOrdered ? '
31fd0 59 27 20 3a 20 27 4e 27 29 20 3a 20 27 3f 27 29 Y' : 'N') : '?')
31fe0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
31ff0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
32000 74 66 28 22 20 20 20 76 73 20 25 73 20 63 6f 73 tf(" vs %s cos
32010 74 3d 25 2d 33 64 2c 25 64 20 6f 72 64 65 72 3d t=%-3d,%d order=
32020 25 63 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 %c\n",.
32030 20 20 20 20 20 20 20 20 20 77 68 65 72 65 50 61 wherePa
32040 74 68 4e 61 6d 65 28 70 54 6f 2c 20 69 4c 6f 6f thName(pTo, iLoo
32050 70 2b 31 2c 20 30 29 2c 20 70 54 6f 2d 3e 72 43 p+1, 0), pTo->rC
32060 6f 73 74 2c 20 70 54 6f 2d 3e 6e 52 6f 77 2c 0a ost, pTo->nRow,.
32070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32080 20 20 70 54 6f 2d 3e 69 73 4f 72 64 65 72 65 64 pTo->isOrdered
32090 56 61 6c 69 64 20 3f 20 28 70 54 6f 2d 3e 69 73 Valid ? (pTo->is
320a0 4f 72 64 65 72 65 64 20 3f 20 27 59 27 20 3a 20 Ordered ? 'Y' :
320b0 27 4e 27 29 20 3a 20 27 3f 27 29 3b 0a 20 20 20 'N') : '?');.
320c0 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 }.#endi
320d0 66 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65 f. te
320e0 73 74 63 61 73 65 28 20 70 54 6f 2d 3e 72 43 6f stcase( pTo->rCo
320f0 73 74 3d 3d 72 43 6f 73 74 20 29 3b 0a 20 20 20 st==rCost );.
32100 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 continu
32110 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 e;. }.
32120 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 testcas
32130 65 28 20 70 54 6f 2d 3e 72 43 6f 73 74 3d 3d 72 e( pTo->rCost==r
32140 43 6f 73 74 2b 31 20 29 3b 0a 20 20 20 20 20 20 Cost+1 );.
32150 20 20 20 20 2f 2a 20 41 20 6e 65 77 20 61 6e 64 /* A new and
32160 20 62 65 74 74 65 72 20 73 63 6f 72 65 20 66 6f better score fo
32170 72 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 63 r a previously c
32180 72 65 61 74 65 64 20 65 71 75 69 76 61 6c 65 6e reated equivalen
32190 74 20 70 61 74 68 20 2a 2f 0a 23 69 66 64 65 66 t path */.#ifdef
321a0 20 57 48 45 52 45 54 52 41 43 45 5f 45 4e 41 42 WHERETRACE_ENAB
321b0 4c 45 44 20 2f 2a 20 30 78 34 20 2a 2f 0a 20 20 LED /* 0x4 */.
321c0 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 if( sqli
321d0 74 65 33 57 68 65 72 65 54 72 61 63 65 26 30 78 te3WhereTrace&0x
321e0 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 4 ){.
321f0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
32200 6e 74 66 28 0a 20 20 20 20 20 20 20 20 20 20 20 ntf(.
32210 20 20 20 20 20 22 55 70 64 61 74 65 20 25 73 20 "Update %s
32220 63 6f 73 74 3d 25 2d 33 64 2c 25 33 64 20 6f 72 cost=%-3d,%3d or
32230 64 65 72 3d 25 63 22 2c 0a 20 20 20 20 20 20 20 der=%c",.
32240 20 20 20 20 20 20 20 20 20 77 68 65 72 65 50 61 wherePa
32250 74 68 4e 61 6d 65 28 70 46 72 6f 6d 2c 20 69 4c thName(pFrom, iL
32260 6f 6f 70 2c 20 70 57 4c 6f 6f 70 29 2c 20 72 43 oop, pWLoop), rC
32270 6f 73 74 2c 20 6e 4f 75 74 2c 0a 20 20 20 20 20 ost, nOut,.
32280 20 20 20 20 20 20 20 20 20 20 20 69 73 4f 72 64 isOrd
32290 65 72 65 64 56 61 6c 69 64 20 3f 20 28 69 73 4f eredValid ? (isO
322a0 72 64 65 72 65 64 20 3f 20 27 59 27 20 3a 20 27 rdered ? 'Y' : '
322b0 4e 27 29 20 3a 20 27 3f 27 29 3b 0a 20 20 20 20 N') : '?');.
322c0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 sqlite3D
322d0 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 77 61 ebugPrintf(" wa
322e0 73 20 25 73 20 63 6f 73 74 3d 25 2d 33 64 2c 25 s %s cost=%-3d,%
322f0 33 64 20 6f 72 64 65 72 3d 25 63 5c 6e 22 2c 0a 3d order=%c\n",.
32300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
32310 77 68 65 72 65 50 61 74 68 4e 61 6d 65 28 70 54 wherePathName(pT
32320 6f 2c 20 69 4c 6f 6f 70 2b 31 2c 20 30 29 2c 20 o, iLoop+1, 0),
32330 70 54 6f 2d 3e 72 43 6f 73 74 2c 20 70 54 6f 2d pTo->rCost, pTo-
32340 3e 6e 52 6f 77 2c 0a 20 20 20 20 20 20 20 20 20 >nRow,.
32350 20 20 20 20 20 20 20 70 54 6f 2d 3e 69 73 4f 72 pTo->isOr
32360 64 65 72 65 64 56 61 6c 69 64 20 3f 20 28 70 54 deredValid ? (pT
32370 6f 2d 3e 69 73 4f 72 64 65 72 65 64 20 3f 20 27 o->isOrdered ? '
32380 59 27 20 3a 20 27 4e 27 29 20 3a 20 27 3f 27 29 Y' : 'N') : '?')
32390 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 ;. }.#e
323a0 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 0a 20 ndif. }.
323b0 20 20 20 20 20 20 20 2f 2a 20 70 57 4c 6f 6f 70 /* pWLoop
323c0 20 69 73 20 61 20 77 69 6e 6e 65 72 2e 20 20 41 is a winner. A
323d0 64 64 20 69 74 20 74 6f 20 74 68 65 20 73 65 74 dd it to the set
323e0 20 6f 66 20 62 65 73 74 20 73 6f 20 66 61 72 20 of best so far
323f0 2a 2f 0a 20 20 20 20 20 20 20 20 70 54 6f 2d 3e */. pTo->
32400 6d 61 73 6b 4c 6f 6f 70 20 3d 20 70 46 72 6f 6d maskLoop = pFrom
32410 2d 3e 6d 61 73 6b 4c 6f 6f 70 20 7c 20 70 57 4c ->maskLoop | pWL
32420 6f 6f 70 2d 3e 6d 61 73 6b 53 65 6c 66 3b 0a 20 oop->maskSelf;.
32430 20 20 20 20 20 20 20 70 54 6f 2d 3e 72 65 76 4c pTo->revL
32440 6f 6f 70 20 3d 20 72 65 76 4d 61 73 6b 3b 0a 20 oop = revMask;.
32450 20 20 20 20 20 20 20 70 54 6f 2d 3e 6e 52 6f 77 pTo->nRow
32460 20 3d 20 6e 4f 75 74 3b 0a 20 20 20 20 20 20 20 = nOut;.
32470 20 70 54 6f 2d 3e 72 43 6f 73 74 20 3d 20 72 43 pTo->rCost = rC
32480 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 70 54 6f ost;. pTo
32490 2d 3e 69 73 4f 72 64 65 72 65 64 56 61 6c 69 64 ->isOrderedValid
324a0 20 3d 20 69 73 4f 72 64 65 72 65 64 56 61 6c 69 = isOrderedVali
324b0 64 3b 0a 20 20 20 20 20 20 20 20 70 54 6f 2d 3e d;. pTo->
324c0 69 73 4f 72 64 65 72 65 64 20 3d 20 69 73 4f 72 isOrdered = isOr
324d0 64 65 72 65 64 3b 0a 20 20 20 20 20 20 20 20 6d dered;. m
324e0 65 6d 63 70 79 28 70 54 6f 2d 3e 61 4c 6f 6f 70 emcpy(pTo->aLoop
324f0 2c 20 70 46 72 6f 6d 2d 3e 61 4c 6f 6f 70 2c 20 , pFrom->aLoop,
32500 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 6f 6f 70 sizeof(WhereLoop
32510 2a 29 2a 69 4c 6f 6f 70 29 3b 0a 20 20 20 20 20 *)*iLoop);.
32520 20 20 20 70 54 6f 2d 3e 61 4c 6f 6f 70 5b 69 4c pTo->aLoop[iL
32530 6f 6f 70 5d 20 3d 20 70 57 4c 6f 6f 70 3b 0a 20 oop] = pWLoop;.
32540 20 20 20 20 20 20 20 69 66 28 20 6e 54 6f 3e 3d if( nTo>=
32550 6d 78 43 68 6f 69 63 65 20 29 7b 0a 20 20 20 20 mxChoice ){.
32560 20 20 20 20 20 20 6d 78 49 20 3d 20 30 3b 0a 20 mxI = 0;.
32570 20 20 20 20 20 20 20 20 20 6d 78 43 6f 73 74 20 mxCost
32580 3d 20 61 54 6f 5b 30 5d 2e 72 43 6f 73 74 3b 0a = aTo[0].rCost;.
32590 20 20 20 20 20 20 20 20 20 20 6d 78 4f 75 74 20 mxOut
325a0 3d 20 61 54 6f 5b 30 5d 2e 6e 52 6f 77 3b 0a 20 = aTo[0].nRow;.
325b0 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d for(jj=
325c0 31 2c 20 70 54 6f 3d 26 61 54 6f 5b 31 5d 3b 20 1, pTo=&aTo[1];
325d0 6a 6a 3c 6d 78 43 68 6f 69 63 65 3b 20 6a 6a 2b jj<mxChoice; jj+
325e0 2b 2c 20 70 54 6f 2b 2b 29 7b 0a 20 20 20 20 20 +, pTo++){.
325f0 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 2d 3e if( pTo->
32600 72 43 6f 73 74 3e 6d 78 43 6f 73 74 20 7c 7c 20 rCost>mxCost ||
32610 28 70 54 6f 2d 3e 72 43 6f 73 74 3d 3d 6d 78 43 (pTo->rCost==mxC
32620 6f 73 74 20 26 26 20 70 54 6f 2d 3e 6e 52 6f 77 ost && pTo->nRow
32630 3e 6d 78 4f 75 74 29 20 29 7b 0a 20 20 20 20 20 >mxOut) ){.
32640 20 20 20 20 20 20 20 20 20 6d 78 43 6f 73 74 20 mxCost
32650 3d 20 70 54 6f 2d 3e 72 43 6f 73 74 3b 0a 20 20 = pTo->rCost;.
32660 20 20 20 20 20 20 20 20 20 20 20 20 6d 78 4f 75 mxOu
32670 74 20 3d 20 70 54 6f 2d 3e 6e 52 6f 77 3b 0a 20 t = pTo->nRow;.
32680 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 78 49 mxI
32690 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20 20 = jj;.
326a0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d }. }
326b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
326c0 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66 }. }..#ifdef
326d0 20 57 48 45 52 45 54 52 41 43 45 5f 45 4e 41 42 WHERETRACE_ENAB
326e0 4c 45 44 20 20 2f 2a 20 3e 3d 32 20 2a 2f 0a 20 LED /* >=2 */.
326f0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 68 if( sqlite3Wh
32700 65 72 65 54 72 61 63 65 3e 3d 32 20 29 7b 0a 20 ereTrace>=2 ){.
32710 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 sqlite3Debu
32720 67 50 72 69 6e 74 66 28 22 2d 2d 2d 2d 20 61 66 gPrintf("---- af
32730 74 65 72 20 72 6f 75 6e 64 20 25 64 20 2d 2d 2d ter round %d ---
32740 2d 5c 6e 22 2c 20 69 4c 6f 6f 70 29 3b 0a 20 20 -\n", iLoop);.
32750 20 20 20 20 66 6f 72 28 69 69 3d 30 2c 20 70 54 for(ii=0, pT
32760 6f 3d 61 54 6f 3b 20 69 69 3c 6e 54 6f 3b 20 69 o=aTo; ii<nTo; i
32770 69 2b 2b 2c 20 70 54 6f 2b 2b 29 7b 0a 20 20 20 i++, pTo++){.
32780 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 sqlite3Debu
32790 67 50 72 69 6e 74 66 28 22 20 25 73 20 63 6f 73 gPrintf(" %s cos
327a0 74 3d 25 2d 33 64 20 6e 72 6f 77 3d 25 2d 33 64 t=%-3d nrow=%-3d
327b0 20 6f 72 64 65 72 3d 25 63 22 2c 0a 20 20 20 20 order=%c",.
327c0 20 20 20 20 20 20 20 77 68 65 72 65 50 61 74 68 wherePath
327d0 4e 61 6d 65 28 70 54 6f 2c 20 69 4c 6f 6f 70 2b Name(pTo, iLoop+
327e0 31 2c 20 30 29 2c 20 70 54 6f 2d 3e 72 43 6f 73 1, 0), pTo->rCos
327f0 74 2c 20 70 54 6f 2d 3e 6e 52 6f 77 2c 0a 20 20 t, pTo->nRow,.
32800 20 20 20 20 20 20 20 20 20 70 54 6f 2d 3e 69 73 pTo->is
32810 4f 72 64 65 72 65 64 56 61 6c 69 64 20 3f 20 28 OrderedValid ? (
32820 70 54 6f 2d 3e 69 73 4f 72 64 65 72 65 64 20 3f pTo->isOrdered ?
32830 20 27 59 27 20 3a 20 27 4e 27 29 20 3a 20 27 3f 'Y' : 'N') : '?
32840 27 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 ');. if(
32850 70 54 6f 2d 3e 69 73 4f 72 64 65 72 65 64 56 61 pTo->isOrderedVa
32860 6c 69 64 20 26 26 20 70 54 6f 2d 3e 69 73 4f 72 lid && pTo->isOr
32870 64 65 72 65 64 20 29 7b 0a 20 20 20 20 20 20 20 dered ){.
32880 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 sqlite3DebugP
32890 72 69 6e 74 66 28 22 20 72 65 76 3d 30 78 25 6c rintf(" rev=0x%l
328a0 6c 78 5c 6e 22 2c 20 70 54 6f 2d 3e 72 65 76 4c lx\n", pTo->revL
328b0 6f 6f 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 oop);. }e
328c0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73 lse{. s
328d0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 qlite3DebugPrint
328e0 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20 f("\n");.
328f0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d }. }. }
32900 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 .#endif.. /*
32910 53 77 61 70 20 74 68 65 20 72 6f 6c 65 73 20 6f Swap the roles o
32920 66 20 61 46 72 6f 6d 20 61 6e 64 20 61 54 6f 20 f aFrom and aTo
32930 66 6f 72 20 74 68 65 20 6e 65 78 74 20 67 65 6e for the next gen
32940 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 eration */. p
32950 46 72 6f 6d 20 3d 20 61 54 6f 3b 0a 20 20 20 20 From = aTo;.
32960 61 54 6f 20 3d 20 61 46 72 6f 6d 3b 0a 20 20 20 aTo = aFrom;.
32970 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 3b 0a aFrom = pFrom;.
32980 20 20 20 20 6e 46 72 6f 6d 20 3d 20 6e 54 6f 3b nFrom = nTo;
32990 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 72 6f . }.. if( nFro
329a0 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 m==0 ){. sqli
329b0 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
329c0 73 65 2c 20 22 6e 6f 20 71 75 65 72 79 20 73 6f se, "no query so
329d0 6c 75 74 69 6f 6e 22 29 3b 0a 20 20 20 20 73 71 lution");. sq
329e0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
329f0 70 53 70 61 63 65 29 3b 0a 20 20 20 20 72 65 74 pSpace);. ret
32a00 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
32a10 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 46 69 ;. }. . /* Fi
32a20 6e 64 20 74 68 65 20 6c 6f 77 65 73 74 20 63 6f nd the lowest co
32a30 73 74 20 70 61 74 68 2e 20 20 70 46 72 6f 6d 20 st path. pFrom
32a40 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 70 6f 69 will be left poi
32a50 6e 74 69 6e 67 20 74 6f 20 74 68 61 74 20 70 61 nting to that pa
32a60 74 68 20 2a 2f 0a 20 20 70 46 72 6f 6d 20 3d 20 th */. pFrom =
32a70 61 46 72 6f 6d 3b 0a 20 20 66 6f 72 28 69 69 3d aFrom;. for(ii=
32a80 31 3b 20 69 69 3c 6e 46 72 6f 6d 3b 20 69 69 2b 1; ii<nFrom; ii+
32a90 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 46 72 6f +){. if( pFro
32aa0 6d 2d 3e 72 43 6f 73 74 3e 61 46 72 6f 6d 5b 69 m->rCost>aFrom[i
32ab0 69 5d 2e 72 43 6f 73 74 20 29 20 70 46 72 6f 6d i].rCost ) pFrom
32ac0 20 3d 20 26 61 46 72 6f 6d 5b 69 69 5d 3b 0a 20 = &aFrom[ii];.
32ad0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 57 49 }. assert( pWI
32ae0 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 6e 4c 6f nfo->nLevel==nLo
32af0 6f 70 20 29 3b 0a 20 20 2f 2a 20 4c 6f 61 64 20 op );. /* Load
32b00 74 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 the lowest cost
32b10 70 61 74 68 20 69 6e 74 6f 20 70 57 49 6e 66 6f path into pWInfo
32b20 20 2a 2f 0a 20 20 66 6f 72 28 69 4c 6f 6f 70 3d */. for(iLoop=
32b30 30 3b 20 69 4c 6f 6f 70 3c 6e 4c 6f 6f 70 3b 20 0; iLoop<nLoop;
32b40 69 4c 6f 6f 70 2b 2b 29 7b 0a 20 20 20 20 57 68 iLoop++){. Wh
32b50 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c ereLevel *pLevel
32b60 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 20 2b 20 69 = pWInfo->a + i
32b70 4c 6f 6f 70 3b 0a 20 20 20 20 70 4c 65 76 65 6c Loop;. pLevel
32b80 2d 3e 70 57 4c 6f 6f 70 20 3d 20 70 57 4c 6f 6f ->pWLoop = pWLoo
32b90 70 20 3d 20 70 46 72 6f 6d 2d 3e 61 4c 6f 6f 70 p = pFrom->aLoop
32ba0 5b 69 4c 6f 6f 70 5d 3b 0a 20 20 20 20 70 4c 65 [iLoop];. pLe
32bb0 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d 20 70 57 4c vel->iFrom = pWL
32bc0 6f 6f 70 2d 3e 69 54 61 62 3b 0a 20 20 20 20 70 oop->iTab;. p
32bd0 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 3d Level->iTabCur =
32be0 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 pWInfo->pTabLis
32bf0 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 t->a[pLevel->iFr
32c00 6f 6d 5d 2e 69 43 75 72 73 6f 72 3b 0a 20 20 7d om].iCursor;. }
32c10 0a 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e . if( (pWInfo->
32c20 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 wctrlFlags & WHE
32c30 52 45 5f 57 41 4e 54 5f 44 49 53 54 49 4e 43 54 RE_WANT_DISTINCT
32c40 29 21 3d 30 0a 20 20 20 26 26 20 28 70 57 49 6e )!=0. && (pWIn
32c50 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 fo->wctrlFlags &
32c60 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 42 WHERE_DISTINCTB
32c70 59 29 3d 3d 30 0a 20 20 20 26 26 20 70 57 49 6e Y)==0. && pWIn
32c80 66 6f 2d 3e 65 44 69 73 74 69 6e 63 74 3d 3d 57 fo->eDistinct==W
32c90 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 4e 4f HERE_DISTINCT_NO
32ca0 4f 50 0a 20 20 20 26 26 20 6e 52 6f 77 45 73 74 OP. && nRowEst
32cb0 0a 20 20 29 7b 0a 20 20 20 20 42 69 74 6d 61 73 . ){. Bitmas
32cc0 6b 20 6e 6f 74 55 73 65 64 3b 0a 20 20 20 20 69 k notUsed;. i
32cd0 6e 74 20 72 63 20 3d 20 77 68 65 72 65 50 61 74 nt rc = wherePat
32ce0 68 53 61 74 69 73 66 69 65 73 4f 72 64 65 72 42 hSatisfiesOrderB
32cf0 79 28 70 57 49 6e 66 6f 2c 20 70 57 49 6e 66 6f y(pWInfo, pWInfo
32d00 2d 3e 70 52 65 73 75 6c 74 53 65 74 2c 20 70 46 ->pResultSet, pF
32d10 72 6f 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 rom,.
32d20 20 20 20 20 20 20 57 48 45 52 45 5f 44 49 53 54 WHERE_DIST
32d30 49 4e 43 54 42 59 2c 20 6e 4c 6f 6f 70 2d 31 2c INCTBY, nLoop-1,
32d40 20 70 46 72 6f 6d 2d 3e 61 4c 6f 6f 70 5b 6e 4c pFrom->aLoop[nL
32d50 6f 6f 70 2d 31 5d 2c 20 26 6e 6f 74 55 73 65 64 oop-1], ¬Used
32d60 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 31 );. if( rc==1
32d70 20 29 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74 ) pWInfo->eDist
32d80 69 6e 63 74 20 3d 20 57 48 45 52 45 5f 44 49 53 inct = WHERE_DIS
32d90 54 49 4e 43 54 5f 4f 52 44 45 52 45 44 3b 0a 20 TINCT_ORDERED;.
32da0 20 7d 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e }. if( pFrom->
32db0 69 73 4f 72 64 65 72 65 64 20 29 7b 0a 20 20 20 isOrdered ){.
32dc0 20 69 66 28 20 70 57 49 6e 66 6f 2d 3e 77 63 74 if( pWInfo->wct
32dd0 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f rlFlags & WHERE_
32de0 44 49 53 54 49 4e 43 54 42 59 20 29 7b 0a 20 20 DISTINCTBY ){.
32df0 20 20 20 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 pWInfo->eDis
32e00 74 69 6e 63 74 20 3d 20 57 48 45 52 45 5f 44 49 tinct = WHERE_DI
32e10 53 54 49 4e 43 54 5f 4f 52 44 45 52 45 44 3b 0a STINCT_ORDERED;.
32e20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
32e30 20 70 57 49 6e 66 6f 2d 3e 62 4f 42 53 61 74 20 pWInfo->bOBSat
32e40 3d 20 31 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 = 1;. pWInf
32e50 6f 2d 3e 72 65 76 4d 61 73 6b 20 3d 20 70 46 72 o->revMask = pFr
32e60 6f 6d 2d 3e 72 65 76 4c 6f 6f 70 3b 0a 20 20 20 om->revLoop;.
32e70 20 7d 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d }. }. pWInfo-
32e80 3e 6e 52 6f 77 4f 75 74 20 3d 20 70 46 72 6f 6d >nRowOut = pFrom
32e90 2d 3e 6e 52 6f 77 3b 0a 0a 20 20 2f 2a 20 46 72 ->nRow;.. /* Fr
32ea0 65 65 20 74 65 6d 70 6f 72 61 72 79 20 6d 65 6d ee temporary mem
32eb0 6f 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20 73 ory and return s
32ec0 75 63 63 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 uccess */. sqli
32ed0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 te3DbFree(db, pS
32ee0 70 61 63 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 pace);. return
32ef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a SQLITE_OK;.}../*
32f00 0a 2a 2a 20 4d 6f 73 74 20 71 75 65 72 69 65 73 .** Most queries
32f10 20 75 73 65 20 6f 6e 6c 79 20 61 20 73 69 6e 67 use only a sing
32f20 6c 65 20 74 61 62 6c 65 20 28 74 68 65 79 20 61 le table (they a
32f30 72 65 20 6e 6f 74 20 6a 6f 69 6e 73 29 20 61 6e re not joins) an
32f40 64 20 68 61 76 65 0a 2a 2a 20 73 69 6d 70 6c 65 d have.** simple
32f50 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 == constraints
32f60 61 67 61 69 6e 73 74 20 69 6e 64 65 78 65 64 20 against indexed
32f70 66 69 65 6c 64 73 2e 20 20 54 68 69 73 20 72 6f fields. This ro
32f80 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73 0a 2a utine attempts.*
32f90 2a 20 74 6f 20 70 6c 61 6e 20 74 68 6f 73 65 20 * to plan those
32fa0 73 69 6d 70 6c 65 20 63 61 73 65 73 20 75 73 69 simple cases usi
32fb0 6e 67 20 6d 75 63 68 20 6c 65 73 73 20 63 65 72 ng much less cer
32fc0 65 6d 6f 6e 79 20 74 68 61 6e 20 74 68 65 0a 2a emony than the.*
32fd0 2a 20 67 65 6e 65 72 61 6c 2d 70 75 72 70 6f 73 * general-purpos
32fe0 65 20 71 75 65 72 79 20 70 6c 61 6e 6e 65 72 2c e query planner,
32ff0 20 61 6e 64 20 74 68 65 72 65 62 79 20 79 69 65 and thereby yie
33000 6c 64 20 66 61 73 74 65 72 20 73 71 6c 69 74 65 ld faster sqlite
33010 33 5f 70 72 65 70 61 72 65 28 29 0a 2a 2a 20 74 3_prepare().** t
33020 69 6d 65 73 20 66 6f 72 20 74 68 65 20 63 6f 6d imes for the com
33030 6d 6f 6e 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 mon case..**.**
33040 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 Return non-zero
33050 6f 6e 20 73 75 63 63 65 73 73 2c 20 69 66 20 74 on success, if t
33060 68 69 73 20 71 75 65 72 79 20 63 61 6e 20 62 65 his query can be
33070 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 69 73 handled by this
33080 0a 2a 2a 20 6e 6f 2d 66 72 69 6c 6c 73 20 71 75 .** no-frills qu
33090 65 72 79 20 70 6c 61 6e 6e 65 72 2e 20 20 52 65 ery planner. Re
330a0 74 75 72 6e 20 7a 65 72 6f 20 69 66 20 74 68 69 turn zero if thi
330b0 73 20 71 75 65 72 79 20 6e 65 65 64 73 20 74 68 s query needs th
330c0 65 20 0a 2a 2a 20 67 65 6e 65 72 61 6c 2d 70 75 e .** general-pu
330d0 72 70 6f 73 65 20 71 75 65 72 79 20 70 6c 61 6e rpose query plan
330e0 6e 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 ner..*/.static i
330f0 6e 74 20 77 68 65 72 65 53 68 6f 72 74 43 75 74 nt whereShortCut
33100 28 57 68 65 72 65 4c 6f 6f 70 42 75 69 6c 64 65 (WhereLoopBuilde
33110 72 20 2a 70 42 75 69 6c 64 65 72 29 7b 0a 20 20 r *pBuilder){.
33120 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 WhereInfo *pWInf
33130 6f 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c o;. struct SrcL
33140 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b ist_item *pItem;
33150 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a . WhereClause *
33160 70 57 43 3b 0a 20 20 57 68 65 72 65 54 65 72 6d pWC;. WhereTerm
33170 20 2a 70 54 65 72 6d 3b 0a 20 20 57 68 65 72 65 *pTerm;. Where
33180 4c 6f 6f 70 20 2a 70 4c 6f 6f 70 3b 0a 20 20 69 Loop *pLoop;. i
33190 6e 74 20 69 43 75 72 3b 0a 20 20 69 6e 74 20 6a nt iCur;. int j
331a0 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b ;. Table *pTab;
331b0 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a . Index *pIdx;.
331c0 20 20 0a 20 20 70 57 49 6e 66 6f 20 3d 20 70 42 . pWInfo = pB
331d0 75 69 6c 64 65 72 2d 3e 70 57 49 6e 66 6f 3b 0a uilder->pWInfo;.
331e0 20 20 69 66 28 20 70 57 49 6e 66 6f 2d 3e 77 63 if( pWInfo->wc
331f0 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 trlFlags & WHERE
33200 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 29 20 72 _FORCE_TABLE ) r
33210 65 74 75 72 6e 20 30 3b 0a 20 20 61 73 73 65 72 eturn 0;. asser
33220 74 28 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c t( pWInfo->pTabL
33230 69 73 74 2d 3e 6e 53 72 63 3e 3d 31 20 29 3b 0a ist->nSrc>=1 );.
33240 20 20 70 49 74 65 6d 20 3d 20 70 57 49 6e 66 6f pItem = pWInfo
33250 2d 3e 70 54 61 62 4c 69 73 74 2d 3e 61 3b 0a 20 ->pTabList->a;.
33260 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70 pTab = pItem->p
33270 54 61 62 3b 0a 20 20 69 66 28 20 49 73 56 69 72 Tab;. if( IsVir
33280 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74 tual(pTab) ) ret
33290 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 49 74 urn 0;. if( pIt
332a0 65 6d 2d 3e 7a 49 6e 64 65 78 20 29 20 72 65 74 em->zIndex ) ret
332b0 75 72 6e 20 30 3b 0a 20 20 69 43 75 72 20 3d 20 urn 0;. iCur =
332c0 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a pItem->iCursor;.
332d0 20 20 70 57 43 20 3d 20 26 70 57 49 6e 66 6f 2d pWC = &pWInfo-
332e0 3e 73 57 43 3b 0a 20 20 70 4c 6f 6f 70 20 3d 20 >sWC;. pLoop =
332f0 70 42 75 69 6c 64 65 72 2d 3e 70 4e 65 77 3b 0a pBuilder->pNew;.
33300 20 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 pLoop->wsFlags
33310 20 3d 20 30 3b 0a 20 20 70 54 65 72 6d 20 3d 20 = 0;. pTerm =
33320 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 findTerm(pWC, iC
33330 75 72 2c 20 2d 31 2c 20 30 2c 20 57 4f 5f 45 51 ur, -1, 0, WO_EQ
33340 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 54 65 72 , 0);. if( pTer
33350 6d 20 29 7b 0a 20 20 20 20 70 4c 6f 6f 70 2d 3e m ){. pLoop->
33360 77 73 46 6c 61 67 73 20 3d 20 57 48 45 52 45 5f wsFlags = WHERE_
33370 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45 52 45 5f COLUMN_EQ|WHERE_
33380 49 50 4b 7c 57 48 45 52 45 5f 4f 4e 45 52 4f 57 IPK|WHERE_ONEROW
33390 3b 0a 20 20 20 20 70 4c 6f 6f 70 2d 3e 61 4c 54 ;. pLoop->aLT
333a0 65 72 6d 5b 30 5d 20 3d 20 70 54 65 72 6d 3b 0a erm[0] = pTerm;.
333b0 20 20 20 20 70 4c 6f 6f 70 2d 3e 6e 4c 54 65 72 pLoop->nLTer
333c0 6d 20 3d 20 31 3b 0a 20 20 20 20 70 4c 6f 6f 70 m = 1;. pLoop
333d0 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 20 3d 20 ->u.btree.nEq =
333e0 31 3b 0a 20 20 20 20 2f 2a 20 54 55 4e 49 4e 47 1;. /* TUNING
333f0 3a 20 43 6f 73 74 20 6f 66 20 61 20 72 6f 77 69 : Cost of a rowi
33400 64 20 6c 6f 6f 6b 75 70 20 69 73 20 31 30 20 2a d lookup is 10 *
33410 2f 0a 20 20 20 20 70 4c 6f 6f 70 2d 3e 72 52 75 /. pLoop->rRu
33420 6e 20 3d 20 33 33 3b 20 20 2f 2a 20 33 33 3d 3d n = 33; /* 33==
33430 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 31 30 sqlite3LogEst(10
33440 29 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 ) */. }else{.
33450 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d for(pIdx=pTab-
33460 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 >pIndex; pIdx; p
33470 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 Idx=pIdx->pNext)
33480 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 {. assert(
33490 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 6d 53 70 61 pLoop->aLTermSpa
334a0 63 65 3d 3d 70 4c 6f 6f 70 2d 3e 61 4c 54 65 72 ce==pLoop->aLTer
334b0 6d 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 m );. asser
334c0 74 28 20 41 72 72 61 79 53 69 7a 65 28 70 4c 6f t( ArraySize(pLo
334d0 6f 70 2d 3e 61 4c 54 65 72 6d 53 70 61 63 65 29 op->aLTermSpace)
334e0 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 69 66 28 ==4 );. if(
334f0 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d pIdx->onError==
33500 4f 45 5f 4e 6f 6e 65 20 0a 20 20 20 20 20 20 20 OE_None .
33510 7c 7c 20 70 49 64 78 2d 3e 70 50 61 72 74 49 64 || pIdx->pPartId
33520 78 57 68 65 72 65 21 3d 30 20 0a 20 20 20 20 20 xWhere!=0 .
33530 20 20 7c 7c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 || pIdx->nColu
33540 6d 6e 3e 41 72 72 61 79 53 69 7a 65 28 70 4c 6f mn>ArraySize(pLo
33550 6f 70 2d 3e 61 4c 54 65 72 6d 53 70 61 63 65 29 op->aLTermSpace)
33560 20 0a 20 20 20 20 20 20 29 20 63 6f 6e 74 69 6e . ) contin
33570 75 65 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d ue;. for(j=
33580 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 0; j<pIdx->nColu
33590 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 mn; j++){.
335a0 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 pTerm = findTe
335b0 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 70 49 rm(pWC, iCur, pI
335c0 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 2c dx->aiColumn[j],
335d0 20 30 2c 20 57 4f 5f 45 51 2c 20 70 49 64 78 29 0, WO_EQ, pIdx)
335e0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 ;. if( pT
335f0 65 72 6d 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a erm==0 ) break;.
33600 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61 pLoop->a
33610 4c 54 65 72 6d 5b 6a 5d 20 3d 20 70 54 65 72 6d LTerm[j] = pTerm
33620 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
33630 69 66 28 20 6a 21 3d 70 49 64 78 2d 3e 6e 43 6f if( j!=pIdx->nCo
33640 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b lumn ) continue;
33650 0a 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 77 73 . pLoop->ws
33660 46 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 43 4f Flags = WHERE_CO
33670 4c 55 4d 4e 5f 45 51 7c 57 48 45 52 45 5f 4f 4e LUMN_EQ|WHERE_ON
33680 45 52 4f 57 7c 57 48 45 52 45 5f 49 4e 44 45 58 EROW|WHERE_INDEX
33690 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 ED;. if( (p
336a0 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 26 20 Item->colUsed &
336b0 7e 63 6f 6c 75 6d 6e 73 49 6e 49 6e 64 65 78 28 ~columnsInIndex(
336c0 70 49 64 78 29 29 3d 3d 30 20 29 7b 0a 20 20 20 pIdx))==0 ){.
336d0 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c pLoop->wsFl
336e0 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 49 44 58 ags |= WHERE_IDX
336f0 5f 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 7d 0a 20 _ONLY;. }.
33700 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 6e 4c 54 65 pLoop->nLTe
33710 72 6d 20 3d 20 6a 3b 0a 20 20 20 20 20 20 70 4c rm = j;. pL
33720 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 2e 6e 45 71 oop->u.btree.nEq
33730 20 3d 20 6a 3b 0a 20 20 20 20 20 20 70 4c 6f 6f = j;. pLoo
33740 70 2d 3e 75 2e 62 74 72 65 65 2e 70 49 6e 64 65 p->u.btree.pInde
33750 78 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 x = pIdx;.
33760 2f 2a 20 54 55 4e 49 4e 47 3a 20 43 6f 73 74 20 /* TUNING: Cost
33770 6f 66 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 of a unique inde
33780 78 20 6c 6f 6f 6b 75 70 20 69 73 20 31 35 20 2a x lookup is 15 *
33790 2f 0a 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 72 /. pLoop->r
337a0 52 75 6e 20 3d 20 33 39 3b 20 20 2f 2a 20 33 39 Run = 39; /* 39
337b0 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 ==sqlite3LogEst(
337c0 31 35 29 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 15) */. bre
337d0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 ak;. }. }.
337e0 69 66 28 20 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 if( pLoop->wsFla
337f0 67 73 20 29 7b 0a 20 20 20 20 70 4c 6f 6f 70 2d gs ){. pLoop-
33800 3e 6e 4f 75 74 20 3d 20 28 4c 6f 67 45 73 74 29 >nOut = (LogEst)
33810 31 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 61 1;. pWInfo->a
33820 5b 30 5d 2e 70 57 4c 6f 6f 70 20 3d 20 70 4c 6f [0].pWLoop = pLo
33830 6f 70 3b 0a 20 20 20 20 70 4c 6f 6f 70 2d 3e 6d op;. pLoop->m
33840 61 73 6b 53 65 6c 66 20 3d 20 67 65 74 4d 61 73 askSelf = getMas
33850 6b 28 26 70 57 49 6e 66 6f 2d 3e 73 4d 61 73 6b k(&pWInfo->sMask
33860 53 65 74 2c 20 69 43 75 72 29 3b 0a 20 20 20 20 Set, iCur);.
33870 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 69 54 61 pWInfo->a[0].iTa
33880 62 43 75 72 20 3d 20 69 43 75 72 3b 0a 20 20 20 bCur = iCur;.
33890 20 70 57 49 6e 66 6f 2d 3e 6e 52 6f 77 4f 75 74 pWInfo->nRowOut
338a0 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 70 57 = 1;. if( pW
338b0 49 6e 66 6f 2d 3e 70 4f 72 64 65 72 42 79 20 29 Info->pOrderBy )
338c0 20 70 57 49 6e 66 6f 2d 3e 62 4f 42 53 61 74 20 pWInfo->bOBSat
338d0 3d 20 20 31 3b 0a 20 20 20 20 69 66 28 20 70 57 = 1;. if( pW
338e0 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 Info->wctrlFlags
338f0 20 26 20 57 48 45 52 45 5f 57 41 4e 54 5f 44 49 & WHERE_WANT_DI
33900 53 54 49 4e 43 54 20 29 7b 0a 20 20 20 20 20 20 STINCT ){.
33910 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74 69 6e 63 pWInfo->eDistinc
33920 74 20 3d 20 57 48 45 52 45 5f 44 49 53 54 49 4e t = WHERE_DISTIN
33930 43 54 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20 7d CT_UNIQUE;. }
33940 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 .#ifdef SQLITE_D
33950 45 42 55 47 0a 20 20 20 20 70 4c 6f 6f 70 2d 3e EBUG. pLoop->
33960 63 49 64 20 3d 20 27 30 27 3b 0a 23 65 6e 64 69 cId = '0';.#endi
33970 66 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a f. return 1;.
33980 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a }. return 0;.
33990 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 }../*.** Generat
339a0 65 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 e the beginning
339b0 6f 66 20 74 68 65 20 6c 6f 6f 70 20 75 73 65 64 of the loop used
339c0 20 66 6f 72 20 57 48 45 52 45 20 63 6c 61 75 73 for WHERE claus
339d0 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a e processing..**
339e0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 The return valu
339f0 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 e is a pointer t
33a00 6f 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 o an opaque stru
33a10 63 74 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61 cture that conta
33a20 69 6e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 ins.** informati
33a30 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 74 65 72 on needed to ter
33a40 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e minate the loop.
33a50 20 20 4c 61 74 65 72 2c 20 74 68 65 20 63 61 6c Later, the cal
33a60 6c 69 6e 67 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 ling routine.**
33a70 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71 should invoke sq
33a80 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 20 lite3WhereEnd()
33a90 77 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 20 with the return
33aa0 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 value of this fu
33ab0 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64 nction.** in ord
33ac0 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 er to complete t
33ad0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 he WHERE clause
33ae0 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a processing..**.*
33af0 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 * If an error oc
33b00 63 75 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 curs, this routi
33b10 6e 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e ne returns NULL.
33b20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63 .**.** The basic
33b30 20 69 64 65 61 20 69 73 20 74 6f 20 64 6f 20 61 idea is to do a
33b40 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e nested loop, on
33b50 65 20 6c 6f 6f 70 20 66 6f 72 20 65 61 63 68 20 e loop for each
33b60 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20 table in.** the
33b70 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 FROM clause of a
33b80 20 73 65 6c 65 63 74 2e 20 20 28 49 4e 53 45 52 select. (INSER
33b90 54 20 61 6e 64 20 55 50 44 41 54 45 20 73 74 61 T and UPDATE sta
33ba0 74 65 6d 65 6e 74 73 20 61 72 65 20 74 68 65 0a tements are the.
33bb0 2a 2a 20 73 61 6d 65 20 61 73 20 61 20 53 45 4c ** same as a SEL
33bc0 45 43 54 20 77 69 74 68 20 6f 6e 6c 79 20 61 20 ECT with only a
33bd0 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 single table in
33be0 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e the FROM clause.
33bf0 29 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c ) For.** exampl
33c00 65 2c 20 69 66 20 74 68 65 20 53 51 4c 20 69 73 e, if the SQL is
33c10 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
33c20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d SELECT * FROM
33c30 20 74 31 2c 20 74 32 2c 20 74 33 20 57 48 45 52 t1, t2, t3 WHER
33c40 45 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 E ...;.**.** The
33c50 6e 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 n the code gener
33c60 61 74 65 64 20 69 73 20 63 6f 6e 63 65 70 74 75 ated is conceptu
33c70 61 6c 6c 79 20 6c 69 6b 65 20 74 68 65 20 66 6f ally like the fo
33c80 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 llowing:.**.**
33c90 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31 foreach row1
33ca0 20 69 6e 20 74 31 20 64 6f 20 20 20 20 20 20 20 in t1 do
33cb0 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 \ Code genera
33cc0 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f ted.** fo
33cd0 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74 32 reach row2 in t2
33ce0 20 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 do |-- by
33cf0 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 sqlite3WhereBegi
33d00 6e 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 n().**
33d10 66 6f 72 65 61 63 68 20 72 6f 77 33 20 69 6e 20 foreach row3 in
33d20 74 33 20 64 6f 20 20 20 2f 0a 2a 2a 20 20 20 20 t3 do /.**
33d30 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 ....**
33d40 20 20 20 20 20 20 20 20 65 6e 64 20 20 20 20 20 end
33d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33d60 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61 \ Code genera
33d70 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 65 6e ted.** en
33d80 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 d
33d90 20 20 20 20 20 20 20 20 20 7c 2d 2d 20 62 79 20 |-- by
33da0 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 sqlite3WhereEnd(
33db0 29 0a 2a 2a 20 20 20 20 20 20 65 6e 64 20 20 20 ).** end
33dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
33dd0 20 20 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f /.**.** No
33de0 74 65 20 74 68 61 74 20 74 68 65 20 6c 6f 6f 70 te that the loop
33df0 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 6e s might not be n
33e00 65 73 74 65 64 20 69 6e 20 74 68 65 20 6f 72 64 ested in the ord
33e10 65 72 20 69 6e 20 77 68 69 63 68 20 74 68 65 79 er in which they
33e20 0a 2a 2a 20 61 70 70 65 61 72 20 69 6e 20 74 68 .** appear in th
33e30 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 66 e FROM clause if
33e40 20 61 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64 a different ord
33e50 65 72 20 69 73 20 62 65 74 74 65 72 20 61 62 6c er is better abl
33e60 65 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 e to make.** use
33e70 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 4e 6f of indices. No
33e80 74 65 20 61 6c 73 6f 20 74 68 61 74 20 77 68 65 te also that whe
33e90 6e 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f n the IN operato
33ea0 72 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20 r appears in.**
33eb0 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 the WHERE clause
33ec0 2c 20 69 74 20 6d 69 67 68 74 20 72 65 73 75 6c , it might resul
33ed0 74 20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 t in additional
33ee0 6e 65 73 74 65 64 20 6c 6f 6f 70 73 20 66 6f 72 nested loops for
33ef0 0a 2a 2a 20 73 63 61 6e 6e 69 6e 67 20 74 68 72 .** scanning thr
33f00 6f 75 67 68 20 61 6c 6c 20 76 61 6c 75 65 73 20 ough all values
33f10 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e on the right-han
33f20 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e d side of the IN
33f30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 ..**.** There ar
33f40 65 20 42 74 72 65 65 20 63 75 72 73 6f 72 73 20 e Btree cursors
33f50 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
33f60 65 61 63 68 20 74 61 62 6c 65 2e 20 20 74 31 20 each table. t1
33f70 75 73 65 73 20 63 75 72 73 6f 72 0a 2a 2a 20 6e uses cursor.** n
33f80 75 6d 62 65 72 20 70 54 61 62 4c 69 73 74 2d 3e umber pTabList->
33f90 61 5b 30 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74 a[0].iCursor. t
33fa0 32 20 75 73 65 73 20 74 68 65 20 63 75 72 73 6f 2 uses the curso
33fb0 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d r pTabList->a[1]
33fc0 2e 69 43 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64 .iCursor..** And
33fd0 20 73 6f 20 66 6f 72 74 68 2e 20 20 54 68 69 73 so forth. This
33fe0 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 routine generat
33ff0 65 73 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20 es code to open
34000 74 68 6f 73 65 20 56 44 42 45 20 63 75 72 73 6f those VDBE curso
34010 72 73 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 rs.** and sqlite
34020 33 57 68 65 72 65 45 6e 64 28 29 20 67 65 6e 65 3WhereEnd() gene
34030 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 74 rates the code t
34040 6f 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a o close them..**
34050 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 68 61 .** The code tha
34060 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 t sqlite3WhereBe
34070 67 69 6e 28 29 20 67 65 6e 65 72 61 74 65 73 20 gin() generates
34080 6c 65 61 76 65 73 20 74 68 65 20 63 75 72 73 6f leaves the curso
34090 72 73 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20 70 rs named.** in p
340a0 54 61 62 4c 69 73 74 20 70 6f 69 6e 74 69 6e 67 TabList pointing
340b0 20 61 74 20 74 68 65 69 72 20 61 70 70 72 6f 70 at their approp
340c0 72 69 61 74 65 20 65 6e 74 72 69 65 73 2e 20 20 riate entries.
340d0 54 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a The [...] code.*
340e0 2a 20 63 61 6e 20 75 73 65 20 4f 50 5f 43 6f 6c * can use OP_Col
340f0 75 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f 77 69 64 umn and OP_Rowid
34100 20 6f 70 63 6f 64 65 73 20 6f 6e 20 74 68 65 73 opcodes on thes
34110 65 20 63 75 72 73 6f 72 73 20 74 6f 20 65 78 74 e cursors to ext
34120 72 61 63 74 0a 2a 2a 20 64 61 74 61 20 66 72 6f ract.** data fro
34130 6d 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 61 m the various ta
34140 62 6c 65 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70 bles of the loop
34150 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57 ..**.** If the W
34160 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 65 HERE clause is e
34170 6d 70 74 79 2c 20 74 68 65 20 66 6f 72 65 61 63 mpty, the foreac
34180 68 20 6c 6f 6f 70 73 20 6d 75 73 74 20 65 61 63 h loops must eac
34190 68 20 73 63 61 6e 20 74 68 65 69 72 0a 2a 2a 20 h scan their.**
341a0 65 6e 74 69 72 65 20 74 61 62 6c 65 73 2e 20 20 entire tables.
341b0 54 68 75 73 20 61 20 74 68 72 65 65 2d 77 61 79 Thus a three-way
341c0 20 6a 6f 69 6e 20 69 73 20 61 6e 20 4f 28 4e 5e join is an O(N^
341d0 33 29 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 3) operation. B
341e0 75 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 ut if.** the tab
341f0 6c 65 73 20 68 61 76 65 20 69 6e 64 69 63 65 73 les have indices
34200 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 74 and there are t
34210 65 72 6d 73 20 69 6e 20 74 68 65 20 57 48 45 52 erms in the WHER
34220 45 20 63 6c 61 75 73 65 20 74 68 61 74 0a 2a 2a E clause that.**
34230 20 72 65 66 65 72 20 74 6f 20 74 68 6f 73 65 20 refer to those
34240 69 6e 64 69 63 65 73 2c 20 61 20 63 6f 6d 70 6c indices, a compl
34250 65 74 65 20 74 61 62 6c 65 20 73 63 61 6e 20 63 ete table scan c
34260 61 6e 20 62 65 20 61 76 6f 69 64 65 64 20 61 6e an be avoided an
34270 64 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69 d the.** code wi
34280 6c 6c 20 72 75 6e 20 6d 75 63 68 20 66 61 73 74 ll run much fast
34290 65 72 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 er. Most of the
342a0 20 77 6f 72 6b 20 6f 66 20 74 68 69 73 20 72 6f work of this ro
342b0 75 74 69 6e 65 20 69 73 20 63 68 65 63 6b 69 6e utine is checkin
342c0 67 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74 g.** to see if t
342d0 68 65 72 65 20 61 72 65 20 69 6e 64 69 63 65 73 here are indices
342e0 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 that can be use
342f0 64 20 74 6f 20 73 70 65 65 64 20 75 70 20 74 68 d to speed up th
34300 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 e loop..**.** Te
34310 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45 rms of the WHERE
34320 20 63 6c 61 75 73 65 20 61 72 65 20 61 6c 73 6f clause are also
34330 20 75 73 65 64 20 74 6f 20 6c 69 6d 69 74 20 77 used to limit w
34340 68 69 63 68 20 72 6f 77 73 20 61 63 74 75 61 6c hich rows actual
34350 6c 79 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f ly.** make it to
34360 20 74 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68 the "..." in th
34370 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 20 e middle of the
34380 6c 6f 6f 70 2e 20 20 41 66 74 65 72 20 65 61 63 loop. After eac
34390 68 20 22 66 6f 72 65 61 63 68 22 2c 0a 2a 2a 20 h "foreach",.**
343a0 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 terms of the WHE
343b0 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 75 RE clause that u
343c0 73 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e se only terms in
343d0 20 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f that loop and o
343e0 75 74 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72 uter.** loops ar
343f0 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 e evaluated and
34400 69 66 20 66 61 6c 73 65 20 61 20 6a 75 6d 70 20 if false a jump
34410 69 73 20 6d 61 64 65 20 61 72 6f 75 6e 64 20 61 is made around a
34420 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a ll subsequent.**
34430 20 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f 72 inner loops (or
34440 20 61 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e 2e around the "...
34450 22 20 69 66 20 74 68 65 20 74 65 73 74 20 6f 63 " if the test oc
34460 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20 curs within the
34470 69 6e 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c inner-.** most l
34480 6f 6f 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52 oop).**.** OUTER
34490 20 4a 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 JOINS.**.** An
344a0 6f 75 74 65 72 20 6a 6f 69 6e 20 6f 66 20 74 61 outer join of ta
344b0 62 6c 65 73 20 74 31 20 61 6e 64 20 74 32 20 69 bles t1 and t2 i
344c0 73 20 63 6f 6e 63 65 70 74 61 6c 6c 79 20 63 6f s conceptally co
344d0 64 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a ded as follows:.
344e0 2a 2a 0a 2a 2a 20 20 20 20 66 6f 72 65 61 63 68 **.** foreach
344f0 20 72 6f 77 31 20 69 6e 20 74 31 20 64 6f 0a 2a row1 in t1 do.*
34500 2a 20 20 20 20 20 20 66 6c 61 67 20 3d 20 30 0a * flag = 0.
34510 2a 2a 20 20 20 20 20 20 66 6f 72 65 61 63 68 20 ** foreach
34520 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 0a 2a 2a row2 in t2 do.**
34530 20 20 20 20 20 20 20 20 73 74 61 72 74 3a 0a 2a start:.*
34540 2a 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a * ....*
34550 2a 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 20 * flag
34560 3d 20 31 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0a = 1.** end.
34570 2a 2a 20 20 20 20 20 20 69 66 20 66 6c 61 67 3d ** if flag=
34580 3d 30 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 =0 then.**
34590 20 20 6d 6f 76 65 20 74 68 65 20 72 6f 77 32 20 move the row2
345a0 63 75 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c cursor to a null
345b0 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 67 row.** g
345c0 6f 74 6f 20 73 74 61 72 74 0a 2a 2a 20 20 20 20 oto start.**
345d0 20 20 66 69 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a fi.** end.*
345e0 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 43 4c *.** ORDER BY CL
345f0 41 55 53 45 20 50 52 4f 43 45 53 53 49 4e 47 0a AUSE PROCESSING.
34600 2a 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 **.** pOrderBy i
34610 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 s a pointer to t
34620 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
34630 73 65 20 28 6f 72 20 74 68 65 20 47 52 4f 55 50 se (or the GROUP
34640 20 42 59 20 63 6c 61 75 73 65 0a 2a 2a 20 69 66 BY clause.** if
34650 20 74 68 65 20 57 48 45 52 45 5f 47 52 4f 55 50 the WHERE_GROUP
34660 42 59 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 BY flag is set i
34670 6e 20 77 63 74 72 6c 46 6c 61 67 73 29 20 6f 66 n wctrlFlags) of
34680 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d a SELECT statem
34690 65 6e 74 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 ent.** if there
346a0 69 73 20 6f 6e 65 2e 20 20 49 66 20 74 68 65 72 is one. If ther
346b0 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 e is no ORDER BY
346c0 20 63 6c 61 75 73 65 20 6f 72 20 69 66 20 74 68 clause or if th
346d0 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 is routine.** is
346e0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 6e 20 called from an
346f0 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 UPDATE or DELETE
34700 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e statement, then
34710 20 70 4f 72 64 65 72 42 79 20 69 73 20 4e 55 4c pOrderBy is NUL
34720 4c 2e 0a 2a 2f 0a 57 68 65 72 65 49 6e 66 6f 20 L..*/.WhereInfo
34730 2a 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 *sqlite3WhereBeg
34740 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 in(. Parse *pPa
34750 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 rse, /* T
34760 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 he parser contex
34770 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a t */. SrcList *
34780 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f 2a 20 pTabList, /*
34790 46 52 4f 4d 20 63 6c 61 75 73 65 3a 20 41 20 6c FROM clause: A l
347a0 69 73 74 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 ist of all table
347b0 73 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 s to be scanned
347c0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 */. Expr *pWher
347d0 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e, /* Th
347e0 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a e WHERE clause *
347f0 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f /. ExprList *pO
34800 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e 20 rderBy, /* An
34810 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c ORDER BY clause,
34820 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 or NULL */. Ex
34830 70 72 4c 69 73 74 20 2a 70 52 65 73 75 6c 74 53 prList *pResultS
34840 65 74 2c 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 et, /* Result se
34850 74 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a t of the query *
34860 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46 6c 61 /. u16 wctrlFla
34870 67 73 2c 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 gs, /* One
34880 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f 2a 20 of the WHERE_*
34890 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20 69 6e flags defined in
348a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2f 0a sqliteInt.h */.
348b0 20 20 69 6e 74 20 69 49 64 78 43 75 72 20 20 20 int iIdxCur
348c0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 57 48 /* If WH
348d0 45 52 45 5f 4f 4e 45 54 41 42 4c 45 5f 4f 4e 4c ERE_ONETABLE_ONL
348e0 59 20 69 73 20 73 65 74 2c 20 69 6e 64 65 78 20 Y is set, index
348f0 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a 2f cursor number */
34900 0a 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 57 .){. int nByteW
34910 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 Info;
34920 20 2f 2a 20 4e 75 6d 2e 20 62 79 74 65 73 20 61 /* Num. bytes a
34930 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 57 68 65 llocated for Whe
34940 72 65 49 6e 66 6f 20 73 74 72 75 63 74 20 2a 2f reInfo struct */
34950 0a 20 20 69 6e 74 20 6e 54 61 62 4c 69 73 74 3b . int nTabList;
34960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
34970 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 Number of eleme
34980 6e 74 73 20 69 6e 20 70 54 61 62 4c 69 73 74 20 nts in pTabList
34990 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a */. WhereInfo *
349a0 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 pWInfo;
349b0 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65 20 74 /* Will become t
349c0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 he return value
349d0 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e of this function
349e0 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 */. Vdbe *v =
349f0 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 pParse->pVdbe;
34a00 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 /* The virtual
34a10 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 database engine
34a20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 */. Bitmask not
34a30 52 65 61 64 79 3b 20 20 20 20 20 20 20 20 20 20 Ready;
34a40 2f 2a 20 43 75 72 73 6f 72 73 20 74 68 61 74 20 /* Cursors that
34a50 61 72 65 20 6e 6f 74 20 79 65 74 20 70 6f 73 69 are not yet posi
34a60 74 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68 65 72 tioned */. Wher
34a70 65 4c 6f 6f 70 42 75 69 6c 64 65 72 20 73 57 4c eLoopBuilder sWL
34a80 42 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 57 68 B; /* The Wh
34a90 65 72 65 4c 6f 6f 70 20 62 75 69 6c 64 65 72 20 ereLoop builder
34aa0 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 */. WhereMaskSe
34ab0 74 20 2a 70 4d 61 73 6b 53 65 74 3b 20 20 20 20 t *pMaskSet;
34ac0 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f /* The expressio
34ad0 6e 20 6d 61 73 6b 20 73 65 74 20 2a 2f 0a 20 20 n mask set */.
34ae0 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 WhereLevel *pLev
34af0 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 el; /* A
34b00 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20 single level in
34b10 70 57 49 6e 66 6f 2d 3e 61 5b 5d 20 2a 2f 0a 20 pWInfo->a[] */.
34b20 20 57 68 65 72 65 4c 6f 6f 70 20 2a 70 4c 6f 6f WhereLoop *pLoo
34b30 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 p; /* P
34b40 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 69 6e 67 ointer to a sing
34b50 6c 65 20 57 68 65 72 65 4c 6f 6f 70 20 6f 62 6a le WhereLoop obj
34b60 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b ect */. int ii;
34b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
34b80 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e /* Loop coun
34b90 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 ter */. sqlite3
34ba0 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 *db;
34bb0 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 /* Database
34bc0 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 connection */.
34bd0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 int rc;
34be0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 /* Re
34bf0 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 0a 20 turn code */...
34c00 20 2f 2a 20 56 61 72 69 61 62 6c 65 20 69 6e 69 /* Variable ini
34c10 74 69 61 6c 69 7a 61 74 69 6f 6e 20 2a 2f 0a 20 tialization */.
34c20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 db = pParse->db
34c30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 57 4c 42 ;. memset(&sWLB
34c40 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 57 4c 42 , 0, sizeof(sWLB
34c50 29 29 3b 0a 20 20 73 57 4c 42 2e 70 4f 72 64 65 ));. sWLB.pOrde
34c60 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a rBy = pOrderBy;.
34c70 0a 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 74 68 . /* Disable th
34c80 65 20 44 49 53 54 49 4e 43 54 20 6f 70 74 69 6d e DISTINCT optim
34c90 69 7a 61 74 69 6f 6e 20 69 66 20 53 51 4c 49 54 ization if SQLIT
34ca0 45 5f 44 69 73 74 69 6e 63 74 4f 70 74 20 69 73 E_DistinctOpt is
34cb0 20 73 65 74 20 76 69 61 0a 20 20 2a 2a 20 73 71 set via. ** sq
34cc0 6c 69 74 65 33 5f 74 65 73 74 5f 63 74 72 6c 28 lite3_test_ctrl(
34cd0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f SQLITE_TESTCTRL_
34ce0 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 53 2c 2e 2e OPTIMIZATIONS,..
34cf0 2e 29 20 2a 2f 0a 20 20 69 66 28 20 4f 70 74 69 .) */. if( Opti
34d00 6d 69 7a 61 74 69 6f 6e 44 69 73 61 62 6c 65 64 mizationDisabled
34d10 28 64 62 2c 20 53 51 4c 49 54 45 5f 44 69 73 74 (db, SQLITE_Dist
34d20 69 6e 63 74 4f 70 74 29 20 29 7b 0a 20 20 20 20 inctOpt) ){.
34d30 77 63 74 72 6c 46 6c 61 67 73 20 26 3d 20 7e 57 wctrlFlags &= ~W
34d40 48 45 52 45 5f 57 41 4e 54 5f 44 49 53 54 49 4e HERE_WANT_DISTIN
34d50 43 54 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 CT;. }.. /* Th
34d60 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c e number of tabl
34d70 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 es in the FROM c
34d80 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65 64 lause is limited
34d90 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f by the number o
34da0 66 0a 20 20 2a 2a 20 62 69 74 73 20 69 6e 20 61 f. ** bits in a
34db0 20 42 69 74 6d 61 73 6b 20 0a 20 20 2a 2f 0a 20 Bitmask . */.
34dc0 20 74 65 73 74 63 61 73 65 28 20 70 54 61 62 4c testcase( pTabL
34dd0 69 73 74 2d 3e 6e 53 72 63 3d 3d 42 4d 53 20 29 ist->nSrc==BMS )
34de0 3b 0a 20 20 69 66 28 20 70 54 61 62 4c 69 73 74 ;. if( pTabList
34df0 2d 3e 6e 53 72 63 3e 42 4d 53 20 29 7b 0a 20 20 ->nSrc>BMS ){.
34e00 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 sqlite3ErrorMs
34e10 67 28 70 50 61 72 73 65 2c 20 22 61 74 20 6d 6f g(pParse, "at mo
34e20 73 74 20 25 64 20 74 61 62 6c 65 73 20 69 6e 20 st %d tables in
34e30 61 20 6a 6f 69 6e 22 2c 20 42 4d 53 29 3b 0a 20 a join", BMS);.
34e40 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
34e50 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 .. /* This func
34e60 74 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 20 67 65 tion normally ge
34e70 6e 65 72 61 74 65 73 20 61 20 6e 65 73 74 65 64 nerates a nested
34e80 20 6c 6f 6f 70 20 66 6f 72 20 61 6c 6c 20 74 61 loop for all ta
34e90 62 6c 65 73 20 69 6e 20 0a 20 20 2a 2a 20 70 54 bles in . ** pT
34ea0 61 62 4c 69 73 74 2e 20 20 42 75 74 20 69 66 20 abList. But if
34eb0 74 68 65 20 57 48 45 52 45 5f 4f 4e 45 54 41 42 the WHERE_ONETAB
34ec0 4c 45 5f 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 LE_ONLY flag is
34ed0 73 65 74 2c 20 74 68 65 6e 20 77 65 20 73 68 6f set, then we sho
34ee0 75 6c 64 0a 20 20 2a 2a 20 6f 6e 6c 79 20 67 65 uld. ** only ge
34ef0 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 nerate code for
34f00 74 68 65 20 66 69 72 73 74 20 74 61 62 6c 65 20 the first table
34f10 69 6e 20 70 54 61 62 4c 69 73 74 20 61 6e 64 20 in pTabList and
34f20 61 73 73 75 6d 65 20 74 68 61 74 0a 20 20 2a 2a assume that. **
34f30 20 61 6e 79 20 63 75 72 73 6f 72 73 20 61 73 73 any cursors ass
34f40 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 75 62 ociated with sub
34f50 73 65 71 75 65 6e 74 20 74 61 62 6c 65 73 20 61 sequent tables a
34f60 72 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 re uninitialized
34f70 2e 0a 20 20 2a 2f 0a 20 20 6e 54 61 62 4c 69 73 .. */. nTabLis
34f80 74 20 3d 20 28 77 63 74 72 6c 46 6c 61 67 73 20 t = (wctrlFlags
34f90 26 20 57 48 45 52 45 5f 4f 4e 45 54 41 42 4c 45 & WHERE_ONETABLE
34fa0 5f 4f 4e 4c 59 29 20 3f 20 31 20 3a 20 70 54 61 _ONLY) ? 1 : pTa
34fb0 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 0a 0a 20 20 bList->nSrc;..
34fc0 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 /* Allocate and
34fd0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 57 initialize the W
34fe0 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75 hereInfo structu
34ff0 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 63 re that will bec
35000 6f 6d 65 20 74 68 65 0a 20 20 2a 2a 20 72 65 74 ome the. ** ret
35010 75 72 6e 20 76 61 6c 75 65 2e 20 41 20 73 69 6e urn value. A sin
35020 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 gle allocation i
35030 73 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 s used to store
35040 74 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 20 20 the WhereInfo.
35050 2a 2a 20 73 74 72 75 63 74 2c 20 74 68 65 20 63 ** struct, the c
35060 6f 6e 74 65 6e 74 73 20 6f 66 20 57 68 65 72 65 ontents of Where
35070 49 6e 66 6f 2e 61 5b 5d 2c 20 74 68 65 20 57 68 Info.a[], the Wh
35080 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 ereClause struct
35090 75 72 65 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 ure. ** and the
350a0 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73 74 WhereMaskSet st
350b0 72 75 63 74 75 72 65 2e 20 53 69 6e 63 65 20 57 ructure. Since W
350c0 68 65 72 65 43 6c 61 75 73 65 20 63 6f 6e 74 61 hereClause conta
350d0 69 6e 73 20 61 6e 20 38 2d 62 79 74 65 0a 20 20 ins an 8-byte.
350e0 2a 2a 20 66 69 65 6c 64 20 28 74 79 70 65 20 42 ** field (type B
350f0 69 74 6d 61 73 6b 29 20 69 74 20 6d 75 73 74 20 itmask) it must
35100 62 65 20 61 6c 69 67 6e 65 64 20 6f 6e 20 61 6e be aligned on an
35110 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 8-byte boundary
35120 20 6f 6e 0a 20 20 2a 2a 20 73 6f 6d 65 20 61 72 on. ** some ar
35130 63 68 69 74 65 63 74 75 72 65 73 2e 20 48 65 6e chitectures. Hen
35140 63 65 20 74 68 65 20 52 4f 55 4e 44 38 28 29 20 ce the ROUND8()
35150 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 6e 42 below.. */. nB
35160 79 74 65 57 49 6e 66 6f 20 3d 20 52 4f 55 4e 44 yteWInfo = ROUND
35170 38 28 73 69 7a 65 6f 66 28 57 68 65 72 65 49 6e 8(sizeof(WhereIn
35180 66 6f 29 2b 28 6e 54 61 62 4c 69 73 74 2d 31 29 fo)+(nTabList-1)
35190 2a 73 69 7a 65 6f 66 28 57 68 65 72 65 4c 65 76 *sizeof(WhereLev
351a0 65 6c 29 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d el));. pWInfo =
351b0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
351c0 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 57 49 Zero(db, nByteWI
351d0 6e 66 6f 20 2b 20 73 69 7a 65 6f 66 28 57 68 65 nfo + sizeof(Whe
351e0 72 65 4c 6f 6f 70 29 29 3b 0a 20 20 69 66 28 20 reLoop));. if(
351f0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
35200 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 ){. sqlite3D
35210 62 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f bFree(db, pWInfo
35220 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 );. pWInfo =
35230 30 3b 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 0;. goto wher
35240 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d eBeginError;. }
35250 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 . pWInfo->nLeve
35260 6c 20 3d 20 6e 54 61 62 4c 69 73 74 3b 0a 20 20 l = nTabList;.
35270 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 20 3d pWInfo->pParse =
35280 20 70 50 61 72 73 65 3b 0a 20 20 70 57 49 6e 66 pParse;. pWInf
35290 6f 2d 3e 70 54 61 62 4c 69 73 74 20 3d 20 70 54 o->pTabList = pT
352a0 61 62 4c 69 73 74 3b 0a 20 20 70 57 49 6e 66 6f abList;. pWInfo
352b0 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 ->pOrderBy = pOr
352c0 64 65 72 42 79 3b 0a 20 20 70 57 49 6e 66 6f 2d derBy;. pWInfo-
352d0 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 70 52 >pResultSet = pR
352e0 65 73 75 6c 74 53 65 74 3b 0a 20 20 70 57 49 6e esultSet;. pWIn
352f0 66 6f 2d 3e 69 42 72 65 61 6b 20 3d 20 73 71 6c fo->iBreak = sql
35300 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 ite3VdbeMakeLabe
35310 6c 28 76 29 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e l(v);. pWInfo->
35320 77 63 74 72 6c 46 6c 61 67 73 20 3d 20 77 63 74 wctrlFlags = wct
35330 72 6c 46 6c 61 67 73 3b 0a 20 20 70 57 49 6e 66 rlFlags;. pWInf
35340 6f 2d 3e 73 61 76 65 64 4e 51 75 65 72 79 4c 6f o->savedNQueryLo
35350 6f 70 20 3d 20 70 50 61 72 73 65 2d 3e 6e 51 75 op = pParse->nQu
35360 65 72 79 4c 6f 6f 70 3b 0a 20 20 70 4d 61 73 6b eryLoop;. pMask
35370 53 65 74 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 73 Set = &pWInfo->s
35380 4d 61 73 6b 53 65 74 3b 0a 20 20 73 57 4c 42 2e MaskSet;. sWLB.
35390 70 57 49 6e 66 6f 20 3d 20 70 57 49 6e 66 6f 3b pWInfo = pWInfo;
353a0 0a 20 20 73 57 4c 42 2e 70 57 43 20 3d 20 26 70 . sWLB.pWC = &p
353b0 57 49 6e 66 6f 2d 3e 73 57 43 3b 0a 20 20 73 57 WInfo->sWC;. sW
353c0 4c 42 2e 70 4e 65 77 20 3d 20 28 57 68 65 72 65 LB.pNew = (Where
353d0 4c 6f 6f 70 2a 29 28 28 28 63 68 61 72 2a 29 70 Loop*)(((char*)p
353e0 57 49 6e 66 6f 29 2b 6e 42 79 74 65 57 49 6e 66 WInfo)+nByteWInf
353f0 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 o);. assert( EI
35400 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 GHT_BYTE_ALIGNME
35410 4e 54 28 73 57 4c 42 2e 70 4e 65 77 29 20 29 3b NT(sWLB.pNew) );
35420 0a 20 20 77 68 65 72 65 4c 6f 6f 70 49 6e 69 74 . whereLoopInit
35430 28 73 57 4c 42 2e 70 4e 65 77 29 3b 0a 23 69 66 (sWLB.pNew);.#if
35440 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
35450 0a 20 20 73 57 4c 42 2e 70 4e 65 77 2d 3e 63 49 . sWLB.pNew->cI
35460 64 20 3d 20 27 2a 27 3b 0a 23 65 6e 64 69 66 0a d = '*';.#endif.
35470 0a 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 . /* Split the
35480 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e 74 WHERE clause int
35490 6f 20 73 65 70 61 72 61 74 65 20 73 75 62 65 78 o separate subex
354a0 70 72 65 73 73 69 6f 6e 73 20 77 68 65 72 65 20 pressions where
354b0 65 61 63 68 0a 20 20 2a 2a 20 73 75 62 65 78 70 each. ** subexp
354c0 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 ression is separ
354d0 61 74 65 64 20 62 79 20 61 6e 20 41 4e 44 20 6f ated by an AND o
354e0 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 perator.. */.
354f0 69 6e 69 74 4d 61 73 6b 53 65 74 28 70 4d 61 73 initMaskSet(pMas
35500 6b 53 65 74 29 3b 0a 20 20 77 68 65 72 65 43 6c kSet);. whereCl
35510 61 75 73 65 49 6e 69 74 28 26 70 57 49 6e 66 6f auseInit(&pWInfo
35520 2d 3e 73 57 43 2c 20 70 57 49 6e 66 6f 29 3b 0a ->sWC, pWInfo);.
35530 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
35540 65 43 6f 6e 73 74 61 6e 74 73 28 70 50 61 72 73 eConstants(pPars
35550 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 77 68 e, pWhere);. wh
35560 65 72 65 53 70 6c 69 74 28 26 70 57 49 6e 66 6f ereSplit(&pWInfo
35570 2d 3e 73 57 43 2c 20 70 57 68 65 72 65 2c 20 54 ->sWC, pWhere, T
35580 4b 5f 41 4e 44 29 3b 0a 20 20 73 71 6c 69 74 65 K_AND);. sqlite
35590 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 3CodeVerifySchem
355a0 61 28 70 50 61 72 73 65 2c 20 2d 31 29 3b 20 2f a(pParse, -1); /
355b0 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f 6f * Insert the coo
355c0 6b 69 65 20 76 65 72 69 66 69 65 72 20 47 6f 74 kie verifier Got
355d0 6f 20 2a 2f 0a 20 20 20 20 0a 20 20 2f 2a 20 53 o */. . /* S
355e0 70 65 63 69 61 6c 20 63 61 73 65 3a 20 61 20 57 pecial case: a W
355f0 48 45 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 HERE clause that
35600 20 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20 20 45 is constant. E
35610 76 61 6c 75 61 74 65 20 74 68 65 0a 20 20 2a 2a valuate the. **
35620 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 expression and
35630 65 69 74 68 65 72 20 6a 75 6d 70 20 6f 76 65 72 either jump over
35640 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 all of the code
35650 20 6f 72 20 66 61 6c 6c 20 74 68 72 75 2e 0a 20 or fall thru..
35660 20 2a 2f 0a 20 20 69 66 28 20 70 57 68 65 72 65 */. if( pWhere
35670 20 26 26 20 28 6e 54 61 62 4c 69 73 74 3d 3d 30 && (nTabList==0
35680 20 7c 7c 20 73 71 6c 69 74 65 33 45 78 70 72 49 || sqlite3ExprI
35690 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e sConstantNotJoin
356a0 28 70 57 68 65 72 65 29 29 20 29 7b 0a 20 20 20 (pWhere)) ){.
356b0 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 sqlite3ExprIfFa
356c0 6c 73 65 28 70 50 61 72 73 65 2c 20 70 57 68 65 lse(pParse, pWhe
356d0 72 65 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 re, pWInfo->iBre
356e0 61 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 ak, SQLITE_JUMPI
356f0 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 70 57 68 65 FNULL);. pWhe
35700 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f re = 0;. }.. /
35710 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 * Special case:
35720 4e 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 No FROM clause.
35730 20 2a 2f 0a 20 20 69 66 28 20 6e 54 61 62 4c 69 */. if( nTabLi
35740 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 st==0 ){. if(
35750 20 70 4f 72 64 65 72 42 79 20 29 20 70 57 49 6e pOrderBy ) pWIn
35760 66 6f 2d 3e 62 4f 42 53 61 74 20 3d 20 31 3b 0a fo->bOBSat = 1;.
35770 20 20 20 20 69 66 28 20 77 63 74 72 6c 46 6c 61 if( wctrlFla
35780 67 73 20 26 20 57 48 45 52 45 5f 57 41 4e 54 5f gs & WHERE_WANT_
35790 44 49 53 54 49 4e 43 54 20 29 7b 0a 20 20 20 20 DISTINCT ){.
357a0 20 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 74 69 pWInfo->eDisti
357b0 6e 63 74 20 3d 20 57 48 45 52 45 5f 44 49 53 54 nct = WHERE_DIST
357c0 49 4e 43 54 5f 55 4e 49 51 55 45 3b 0a 20 20 20 INCT_UNIQUE;.
357d0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 }. }.. /* Ass
357e0 69 67 6e 20 61 20 62 69 74 20 66 72 6f 6d 20 74 ign a bit from t
357f0 68 65 20 62 69 74 6d 61 73 6b 20 74 6f 20 65 76 he bitmask to ev
35800 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 ery term in the
35810 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a FROM clause.. *
35820 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 61 73 73 69 *. ** When assi
35830 67 6e 69 6e 67 20 62 69 74 6d 61 73 6b 20 76 61 gning bitmask va
35840 6c 75 65 73 20 74 6f 20 46 52 4f 4d 20 63 6c 61 lues to FROM cla
35850 75 73 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 use cursors, it
35860 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 74 68 65 must be. ** the
35870 20 63 61 73 65 20 74 68 61 74 20 69 66 20 58 20 case that if X
35880 69 73 20 74 68 65 20 62 69 74 6d 61 73 6b 20 66 is the bitmask f
35890 6f 72 20 74 68 65 20 4e 2d 74 68 20 46 52 4f 4d or the N-th FROM
358a0 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 68 65 clause term the
358b0 6e 0a 20 20 2a 2a 20 74 68 65 20 62 69 74 6d 61 n. ** the bitma
358c0 73 6b 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 20 sk for all FROM
358d0 63 6c 61 75 73 65 20 74 65 72 6d 73 20 74 6f 20 clause terms to
358e0 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65 20 the left of the
358f0 4e 2d 74 68 20 74 65 72 6d 0a 20 20 2a 2a 20 69 N-th term. ** i
35900 73 20 28 58 2d 31 29 2e 20 20 20 41 6e 20 65 78 s (X-1). An ex
35910 70 72 65 73 73 69 6f 6e 20 66 72 6f 6d 20 74 68 pression from th
35920 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 e ON clause of a
35930 20 4c 45 46 54 20 4a 4f 49 4e 20 63 61 6e 20 75 LEFT JOIN can u
35940 73 65 0a 20 20 2a 2a 20 69 74 73 20 45 78 70 72 se. ** its Expr
35950 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 .iRightJoinTable
35960 20 76 61 6c 75 65 20 74 6f 20 66 69 6e 64 20 74 value to find t
35970 68 65 20 62 69 74 6d 61 73 6b 20 6f 66 20 74 68 he bitmask of th
35980 65 20 72 69 67 68 74 20 74 61 62 6c 65 0a 20 20 e right table.
35990 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 ** of the join.
359a0 20 53 75 62 74 72 61 63 74 69 6e 67 20 6f 6e 65 Subtracting one
359b0 20 66 72 6f 6d 20 74 68 65 20 72 69 67 68 74 20 from the right
359c0 74 61 62 6c 65 20 62 69 74 6d 61 73 6b 20 67 69 table bitmask gi
359d0 76 65 73 20 61 0a 20 20 2a 2a 20 62 69 74 6d 61 ves a. ** bitma
359e0 73 6b 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 sk for all table
359f0 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 s to the left of
35a00 20 74 68 65 20 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 the join. Know
35a10 69 6e 67 20 74 68 65 20 62 69 74 6d 61 73 6b 0a ing the bitmask.
35a20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62 ** for all tab
35a30 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 les to the left
35a40 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 20 69 of a left join i
35a50 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 69 s important. Ti
35a60 63 6b 65 74 20 23 33 30 31 35 2e 0a 20 20 2a 2a cket #3015.. **
35a70 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 . ** Note that
35a80 62 69 74 6d 61 73 6b 73 20 61 72 65 20 63 72 65 bitmasks are cre
35a90 61 74 65 64 20 66 6f 72 20 61 6c 6c 20 70 54 61 ated for all pTa
35aa0 62 4c 69 73 74 2d 3e 6e 53 72 63 20 74 61 62 6c bList->nSrc tabl
35ab0 65 73 20 69 6e 0a 20 20 2a 2a 20 70 54 61 62 4c es in. ** pTabL
35ac0 69 73 74 2c 20 6e 6f 74 20 6a 75 73 74 20 74 68 ist, not just th
35ad0 65 20 66 69 72 73 74 20 6e 54 61 62 4c 69 73 74 e first nTabList
35ae0 20 74 61 62 6c 65 73 2e 20 20 6e 54 61 62 4c 69 tables. nTabLi
35af0 73 74 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 20 st is normally.
35b00 20 2a 2a 20 65 71 75 61 6c 20 74 6f 20 70 54 61 ** equal to pTa
35b10 62 4c 69 73 74 2d 3e 6e 53 72 63 20 62 75 74 20 bList->nSrc but
35b20 6d 69 67 68 74 20 62 65 20 73 68 6f 72 74 65 6e might be shorten
35b30 65 64 20 74 6f 20 31 20 69 66 20 74 68 65 0a 20 ed to 1 if the.
35b40 20 2a 2a 20 57 48 45 52 45 5f 4f 4e 45 54 41 42 ** WHERE_ONETAB
35b50 4c 45 5f 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 LE_ONLY flag is
35b60 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 set.. */. for(
35b70 69 69 3d 30 3b 20 69 69 3c 70 54 61 62 4c 69 73 ii=0; ii<pTabLis
35b80 74 2d 3e 6e 53 72 63 3b 20 69 69 2b 2b 29 7b 0a t->nSrc; ii++){.
35b90 20 20 20 20 63 72 65 61 74 65 4d 61 73 6b 28 70 createMask(p
35ba0 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 MaskSet, pTabLis
35bb0 74 2d 3e 61 5b 69 69 5d 2e 69 43 75 72 73 6f 72 t->a[ii].iCursor
35bc0 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e );. }.#ifndef N
35bd0 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20 42 69 DEBUG. {. Bi
35be0 74 6d 61 73 6b 20 74 6f 54 68 65 4c 65 66 74 20 tmask toTheLeft
35bf0 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d = 0;. for(ii=
35c00 30 3b 20 69 69 3c 70 54 61 62 4c 69 73 74 2d 3e 0; ii<pTabList->
35c10 6e 53 72 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 nSrc; ii++){.
35c20 20 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d 20 67 Bitmask m = g
35c30 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c etMask(pMaskSet,
35c40 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 69 5d pTabList->a[ii]
35c50 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 .iCursor);.
35c60 20 61 73 73 65 72 74 28 20 28 6d 2d 31 29 3d 3d assert( (m-1)==
35c70 74 6f 54 68 65 4c 65 66 74 20 29 3b 0a 20 20 20 toTheLeft );.
35c80 20 20 20 74 6f 54 68 65 4c 65 66 74 20 7c 3d 20 toTheLeft |=
35c90 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e m;. }. }.#en
35ca0 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a dif.. /* Analyz
35cb0 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 e all of the sub
35cc0 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 4e 6f expressions. No
35cd0 74 65 20 74 68 61 74 20 65 78 70 72 41 6e 61 6c te that exprAnal
35ce0 79 7a 65 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a yze() might. **
35cf0 20 61 64 64 20 6e 65 77 20 76 69 72 74 75 61 6c add new virtual
35d00 20 74 65 72 6d 73 20 6f 6e 74 6f 20 74 68 65 20 terms onto the
35d10 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 end of the WHERE
35d20 20 63 6c 61 75 73 65 2e 20 20 57 65 20 64 6f 20 clause. We do
35d30 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e 74 20 74 6f not. ** want to
35d40 20 61 6e 61 6c 79 7a 65 20 74 68 65 73 65 20 76 analyze these v
35d50 69 72 74 75 61 6c 20 74 65 72 6d 73 2c 20 73 6f irtual terms, so
35d60 20 73 74 61 72 74 20 61 6e 61 6c 79 7a 69 6e 67 start analyzing
35d70 20 61 74 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a at the end. **
35d80 20 61 6e 64 20 77 6f 72 6b 20 66 6f 72 77 61 72 and work forwar
35d90 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 61 64 d so that the ad
35da0 64 65 64 20 76 69 72 74 75 61 6c 20 74 65 72 6d ded virtual term
35db0 73 20 61 72 65 20 6e 65 76 65 72 20 70 72 6f 63 s are never proc
35dc0 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 78 essed.. */. ex
35dd0 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 54 61 prAnalyzeAll(pTa
35de0 62 4c 69 73 74 2c 20 26 70 57 49 6e 66 6f 2d 3e bList, &pWInfo->
35df0 73 57 43 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e sWC);. if( db->
35e00 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
35e10 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65 goto whereBe
35e20 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 ginError;. }..
35e30 20 2f 2a 20 49 66 20 74 68 65 20 4f 52 44 45 52 /* If the ORDER
35e40 20 42 59 20 28 6f 72 20 47 52 4f 55 50 20 42 59 BY (or GROUP BY
35e50 29 20 63 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e ) clause contain
35e60 73 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 s references to
35e70 67 65 6e 65 72 61 6c 0a 20 20 2a 2a 20 65 78 70 general. ** exp
35e80 72 65 73 73 69 6f 6e 73 2c 20 74 68 65 6e 20 77 ressions, then w
35e90 65 20 77 6f 6e 27 74 20 62 65 20 61 62 6c 65 20 e won't be able
35ea0 74 6f 20 73 61 74 69 73 66 79 20 69 74 20 75 73 to satisfy it us
35eb0 69 6e 67 20 69 6e 64 69 63 65 73 2c 20 73 6f 0a ing indices, so.
35ec0 20 20 2a 2a 20 67 6f 20 61 68 65 61 64 20 61 6e ** go ahead an
35ed0 64 20 64 69 73 61 62 6c 65 20 69 74 20 6e 6f 77 d disable it now
35ee0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 .. */. if( pOr
35ef0 64 65 72 42 79 20 26 26 20 28 77 63 74 72 6c 46 derBy && (wctrlF
35f00 6c 61 67 73 20 26 20 57 48 45 52 45 5f 57 41 4e lags & WHERE_WAN
35f10 54 5f 44 49 53 54 49 4e 43 54 29 21 3d 30 20 29 T_DISTINCT)!=0 )
35f20 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 {. for(ii=0;
35f30 69 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 ii<pOrderBy->nEx
35f40 70 72 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 pr; ii++){.
35f50 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 73 Expr *pExpr = s
35f60 71 6c 69 74 65 33 45 78 70 72 53 6b 69 70 43 6f qlite3ExprSkipCo
35f70 6c 6c 61 74 65 28 70 4f 72 64 65 72 42 79 2d 3e llate(pOrderBy->
35f80 61 5b 69 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 a[ii].pExpr);.
35f90 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f if( pExpr->o
35fa0 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a p!=TK_COLUMN ){.
35fb0 20 20 20 20 20 20 20 20 70 57 49 6e 66 6f 2d 3e pWInfo->
35fc0 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 pOrderBy = pOrde
35fd0 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 rBy = 0;.
35fe0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 break;. }e
35ff0 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 69 lse if( pExpr->i
36000 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 Column<0 ){.
36010 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
36020 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
36030 69 66 28 20 77 63 74 72 6c 46 6c 61 67 73 20 26 if( wctrlFlags &
36040 20 57 48 45 52 45 5f 57 41 4e 54 5f 44 49 53 54 WHERE_WANT_DIST
36050 49 4e 43 54 20 29 7b 0a 20 20 20 20 69 66 28 20 INCT ){. if(
36060 69 73 44 69 73 74 69 6e 63 74 52 65 64 75 6e 64 isDistinctRedund
36070 61 6e 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 ant(pParse, pTab
36080 4c 69 73 74 2c 20 26 70 57 49 6e 66 6f 2d 3e 73 List, &pWInfo->s
36090 57 43 2c 20 70 52 65 73 75 6c 74 53 65 74 29 20 WC, pResultSet)
360a0 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 ){. /* The
360b0 44 49 53 54 49 4e 43 54 20 6d 61 72 6b 69 6e 67 DISTINCT marking
360c0 20 69 73 20 70 6f 69 6e 74 6c 65 73 73 2e 20 20 is pointless.
360d0 49 67 6e 6f 72 65 20 69 74 2e 20 2a 2f 0a 20 20 Ignore it. */.
360e0 20 20 20 20 70 57 49 6e 66 6f 2d 3e 65 44 69 73 pWInfo->eDis
360f0 74 69 6e 63 74 20 3d 20 57 48 45 52 45 5f 44 49 tinct = WHERE_DI
36100 53 54 49 4e 43 54 5f 55 4e 49 51 55 45 3b 0a 20 STINCT_UNIQUE;.
36110 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 }else if( pOr
36120 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 20 20 derBy==0 ){.
36130 20 20 2f 2a 20 54 72 79 20 74 6f 20 4f 52 44 45 /* Try to ORDE
36140 52 20 42 59 20 74 68 65 20 72 65 73 75 6c 74 20 R BY the result
36150 73 65 74 20 74 6f 20 6d 61 6b 65 20 64 69 73 74 set to make dist
36160 69 6e 63 74 20 70 72 6f 63 65 73 73 69 6e 67 20 inct processing
36170 65 61 73 69 65 72 20 2a 2f 0a 20 20 20 20 20 20 easier */.
36180 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 pWInfo->wctrlFla
36190 67 73 20 7c 3d 20 57 48 45 52 45 5f 44 49 53 54 gs |= WHERE_DIST
361a0 49 4e 43 54 42 59 3b 0a 20 20 20 20 20 20 70 57 INCTBY;. pW
361b0 49 6e 66 6f 2d 3e 70 4f 72 64 65 72 42 79 20 3d Info->pOrderBy =
361c0 20 70 52 65 73 75 6c 74 53 65 74 3b 0a 20 20 20 pResultSet;.
361d0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e }. }.. /* Con
361e0 73 74 72 75 63 74 20 74 68 65 20 57 68 65 72 65 struct the Where
361f0 4c 6f 6f 70 20 6f 62 6a 65 63 74 73 20 2a 2f 0a Loop objects */.
36200 20 20 57 48 45 52 45 54 52 41 43 45 28 30 78 66 WHERETRACE(0xf
36210 66 66 66 2c 28 22 2a 2a 2a 20 4f 70 74 69 6d 69 fff,("*** Optimi
36220 7a 65 72 20 53 74 61 72 74 20 2a 2a 2a 5c 6e 22 zer Start ***\n"
36230 29 29 3b 0a 20 20 2f 2a 20 44 69 73 70 6c 61 79 ));. /* Display
36240 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 all terms of th
36250 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a e WHERE clause *
36260 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 57 48 /.#if defined(WH
36270 45 52 45 54 52 41 43 45 5f 45 4e 41 42 4c 45 44 ERETRACE_ENABLED
36280 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c ) && defined(SQL
36290 49 54 45 5f 45 4e 41 42 4c 45 5f 54 52 45 45 5f ITE_ENABLE_TREE_
362a0 45 58 50 4c 41 49 4e 29 0a 20 20 69 66 28 20 73 EXPLAIN). if( s
362b0 71 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65 qlite3WhereTrace
362c0 20 26 20 30 78 31 30 30 20 29 7b 0a 20 20 20 20 & 0x100 ){.
362d0 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65 20 int i;. Vdbe
362e0 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 *v = pParse->pVd
362f0 62 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 be;. sqlite3E
36300 78 70 6c 61 69 6e 42 65 67 69 6e 28 76 29 3b 0a xplainBegin(v);.
36310 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 for(i=0; i<s
36320 57 4c 42 2e 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 WLB.pWC->nTerm;
36330 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 i++){. sqli
36340 74 65 33 45 78 70 6c 61 69 6e 50 72 69 6e 74 66 te3ExplainPrintf
36350 28 76 2c 20 22 23 25 64 20 22 2c 20 69 29 3b 0a (v, "#%d ", i);.
36360 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
36370 6c 61 69 6e 50 75 73 68 28 76 29 3b 0a 20 20 20 lainPush(v);.
36380 20 20 20 77 68 65 72 65 45 78 70 6c 61 69 6e 54 whereExplainT
36390 65 72 6d 28 76 2c 20 26 73 57 4c 42 2e 70 57 43 erm(v, &sWLB.pWC
363a0 2d 3e 61 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 ->a[i]);. s
363b0 71 6c 69 74 65 33 45 78 70 6c 61 69 6e 50 6f 70 qlite3ExplainPop
363c0 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 (v);. sqlit
363d0 65 33 45 78 70 6c 61 69 6e 4e 4c 28 76 29 3b 0a e3ExplainNL(v);.
363e0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 }. sqlite
363f0 33 45 78 70 6c 61 69 6e 46 69 6e 69 73 68 28 76 3ExplainFinish(v
36400 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 );. sqlite3De
36410 62 75 67 50 72 69 6e 74 66 28 22 25 73 22 2c 20 bugPrintf("%s",
36420 73 71 6c 69 74 65 33 56 64 62 65 45 78 70 6c 61 sqlite3VdbeExpla
36430 6e 61 74 69 6f 6e 28 76 29 29 3b 0a 20 20 7d 0a nation(v));. }.
36440 23 65 6e 64 69 66 0a 20 20 69 66 28 20 6e 54 61 #endif. if( nTa
36450 62 4c 69 73 74 21 3d 31 20 7c 7c 20 77 68 65 72 bList!=1 || wher
36460 65 53 68 6f 72 74 43 75 74 28 26 73 57 4c 42 29 eShortCut(&sWLB)
36470 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 ==0 ){. rc =
36480 77 68 65 72 65 4c 6f 6f 70 41 64 64 41 6c 6c 28 whereLoopAddAll(
36490 26 73 57 4c 42 29 3b 0a 20 20 20 20 69 66 28 20 &sWLB);. if(
364a0 72 63 20 29 20 67 6f 74 6f 20 77 68 65 72 65 42 rc ) goto whereB
364b0 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 0a 20 20 eginError;. .
364c0 20 20 2f 2a 20 44 69 73 70 6c 61 79 20 61 6c 6c /* Display all
364d0 20 6f 66 20 74 68 65 20 57 68 65 72 65 4c 6f 6f of the WhereLoo
364e0 70 20 6f 62 6a 65 63 74 73 20 69 66 20 77 68 65 p objects if whe
364f0 72 65 74 72 61 63 65 20 69 73 20 65 6e 61 62 6c retrace is enabl
36500 65 64 20 2a 2f 0a 23 69 66 64 65 66 20 57 48 45 ed */.#ifdef WHE
36510 52 45 54 52 41 43 45 5f 45 4e 41 42 4c 45 44 20 RETRACE_ENABLED
36520 2f 2a 20 21 3d 30 20 2a 2f 0a 20 20 20 20 69 66 /* !=0 */. if
36530 28 20 73 71 6c 69 74 65 33 57 68 65 72 65 54 72 ( sqlite3WhereTr
36540 61 63 65 20 29 7b 0a 20 20 20 20 20 20 57 68 65 ace ){. Whe
36550 72 65 4c 6f 6f 70 20 2a 70 3b 0a 20 20 20 20 20 reLoop *p;.
36560 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 73 74 int i;. st
36570 61 74 69 63 20 63 68 61 72 20 7a 4c 61 62 65 6c atic char zLabel
36580 5b 5d 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 [] = "0123456789
36590 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
365a0 71 72 73 74 75 76 77 79 78 7a 22 0a 20 20 20 20 qrstuvwyxz".
365b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
365c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
365d0 20 20 20 22 41 42 43 44 45 46 47 48 49 4a 4b 4c "ABCDEFGHIJKL
365e0 4d 4e 4f 50 51 52 53 54 55 56 57 59 58 5a 22 3b MNOPQRSTUVWYXZ";
365f0 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 57 49 . for(p=pWI
36600 6e 66 6f 2d 3e 70 4c 6f 6f 70 73 2c 20 69 3d 30 nfo->pLoops, i=0
36610 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 4c ; p; p=p->pNextL
36620 6f 6f 70 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 oop, i++){.
36630 20 20 20 70 2d 3e 63 49 64 20 3d 20 7a 4c 61 62 p->cId = zLab
36640 65 6c 5b 69 25 73 69 7a 65 6f 66 28 7a 4c 61 62 el[i%sizeof(zLab
36650 65 6c 29 5d 3b 0a 20 20 20 20 20 20 20 20 77 68 el)];. wh
36660 65 72 65 4c 6f 6f 70 50 72 69 6e 74 28 70 2c 20 ereLoopPrint(p,
36670 73 57 4c 42 2e 70 57 43 29 3b 0a 20 20 20 20 20 sWLB.pWC);.
36680 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a }. }.#endif.
36690 20 20 0a 20 20 20 20 77 68 65 72 65 50 61 74 68 . wherePath
366a0 53 6f 6c 76 65 72 28 70 57 49 6e 66 6f 2c 20 30 Solver(pWInfo, 0
366b0 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d );. if( db->m
366c0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f allocFailed ) go
366d0 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 to whereBeginErr
366e0 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 57 49 6e or;. if( pWIn
366f0 66 6f 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a fo->pOrderBy ){.
36700 20 20 20 20 20 20 20 77 68 65 72 65 50 61 74 68 wherePath
36710 53 6f 6c 76 65 72 28 70 57 49 6e 66 6f 2c 20 70 Solver(pWInfo, p
36720 57 49 6e 66 6f 2d 3e 6e 52 6f 77 4f 75 74 2b 31 WInfo->nRowOut+1
36730 29 3b 0a 20 20 20 20 20 20 20 69 66 28 20 64 62 );. if( db
36740 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
36750 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e goto whereBegin
36760 45 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d Error;. }. }
36770 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 2d 3e 70 . if( pWInfo->p
36780 4f 72 64 65 72 42 79 3d 3d 30 20 26 26 20 28 64 OrderBy==0 && (d
36790 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 b->flags & SQLIT
367a0 45 5f 52 65 76 65 72 73 65 4f 72 64 65 72 29 21 E_ReverseOrder)!
367b0 3d 30 20 29 7b 0a 20 20 20 20 20 70 57 49 6e 66 =0 ){. pWInf
367c0 6f 2d 3e 72 65 76 4d 61 73 6b 20 3d 20 28 42 69 o->revMask = (Bi
367d0 74 6d 61 73 6b 29 28 2d 31 29 3b 0a 20 20 7d 0a tmask)(-1);. }.
367e0 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 if( pParse->nE
367f0 72 72 20 7c 7c 20 4e 45 56 45 52 28 64 62 2d 3e rr || NEVER(db->
36800 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b mallocFailed) ){
36810 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42 . goto whereB
36820 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 23 eginError;. }.#
36830 69 66 64 65 66 20 57 48 45 52 45 54 52 41 43 45 ifdef WHERETRACE
36840 5f 45 4e 41 42 4c 45 44 20 2f 2a 20 21 3d 30 20 _ENABLED /* !=0
36850 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 */. if( sqlite3
36860 57 68 65 72 65 54 72 61 63 65 20 29 7b 0a 20 20 WhereTrace ){.
36870 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 73 71 int ii;. sq
36880 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 lite3DebugPrintf
36890 28 22 2d 2d 2d 2d 20 53 6f 6c 75 74 69 6f 6e 20 ("---- Solution
368a0 6e 52 6f 77 3d 25 64 22 2c 20 70 57 49 6e 66 6f nRow=%d", pWInfo
368b0 2d 3e 6e 52 6f 77 4f 75 74 29 3b 0a 20 20 20 20 ->nRowOut);.
368c0 69 66 28 20 70 57 49 6e 66 6f 2d 3e 62 4f 42 53 if( pWInfo->bOBS
368d0 61 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 at ){. sqli
368e0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 te3DebugPrintf("
368f0 20 4f 52 44 45 52 42 59 3d 30 78 25 6c 6c 78 22 ORDERBY=0x%llx"
36900 2c 20 70 57 49 6e 66 6f 2d 3e 72 65 76 4d 61 73 , pWInfo->revMas
36910 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 77 k);. }. sw
36920 69 74 63 68 28 20 70 57 49 6e 66 6f 2d 3e 65 44 itch( pWInfo->eD
36930 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 20 istinct ){.
36940 20 63 61 73 65 20 57 48 45 52 45 5f 44 49 53 54 case WHERE_DIST
36950 49 4e 43 54 5f 55 4e 49 51 55 45 3a 20 7b 0a 20 INCT_UNIQUE: {.
36960 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 sqlite3De
36970 62 75 67 50 72 69 6e 74 66 28 22 20 20 44 49 53 bugPrintf(" DIS
36980 54 49 4e 43 54 3d 75 6e 69 71 75 65 22 29 3b 0a TINCT=unique");.
36990 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
369a0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 }. cas
369b0 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 e WHERE_DISTINCT
369c0 5f 4f 52 44 45 52 45 44 3a 20 7b 0a 20 20 20 20 _ORDERED: {.
369d0 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 sqlite3Debug
369e0 50 72 69 6e 74 66 28 22 20 20 44 49 53 54 49 4e Printf(" DISTIN
369f0 43 54 3d 6f 72 64 65 72 65 64 22 29 3b 0a 20 20 CT=ordered");.
36a00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
36a10 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 }. case
36a20 57 48 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 WHERE_DISTINCT_U
36a30 4e 4f 52 44 45 52 45 44 3a 20 7b 0a 20 20 20 20 NORDERED: {.
36a40 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 sqlite3Debug
36a50 50 72 69 6e 74 66 28 22 20 20 44 49 53 54 49 4e Printf(" DISTIN
36a60 43 54 3d 75 6e 6f 72 64 65 72 65 64 22 29 3b 0a CT=unordered");.
36a70 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
36a80 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
36a90 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 sqlite3DebugPri
36aa0 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20 66 ntf("\n");. f
36ab0 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 57 49 6e or(ii=0; ii<pWIn
36ac0 66 6f 2d 3e 6e 4c 65 76 65 6c 3b 20 69 69 2b 2b fo->nLevel; ii++
36ad0 29 7b 0a 20 20 20 20 20 20 77 68 65 72 65 4c 6f ){. whereLo
36ae0 6f 70 50 72 69 6e 74 28 70 57 49 6e 66 6f 2d 3e opPrint(pWInfo->
36af0 61 5b 69 69 5d 2e 70 57 4c 6f 6f 70 2c 20 73 57 a[ii].pWLoop, sW
36b00 4c 42 2e 70 57 43 29 3b 0a 20 20 20 20 7d 0a 20 LB.pWC);. }.
36b10 20 7d 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 41 }.#endif. /* A
36b20 74 74 65 6d 70 74 20 74 6f 20 6f 6d 69 74 20 74 ttempt to omit t
36b30 61 62 6c 65 73 20 66 72 6f 6d 20 74 68 65 20 6a ables from the j
36b40 6f 69 6e 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 oin that do not
36b50 65 66 66 65 63 74 20 74 68 65 20 72 65 73 75 6c effect the resul
36b60 74 20 2a 2f 0a 20 20 69 66 28 20 70 57 49 6e 66 t */. if( pWInf
36b70 6f 2d 3e 6e 4c 65 76 65 6c 3e 3d 32 0a 20 20 20 o->nLevel>=2.
36b80 26 26 20 70 52 65 73 75 6c 74 53 65 74 21 3d 30 && pResultSet!=0
36b90 0a 20 20 20 26 26 20 4f 70 74 69 6d 69 7a 61 74 . && Optimizat
36ba0 69 6f 6e 45 6e 61 62 6c 65 64 28 64 62 2c 20 53 ionEnabled(db, S
36bb0 51 4c 49 54 45 5f 4f 6d 69 74 4e 6f 6f 70 4a 6f QLITE_OmitNoopJo
36bc0 69 6e 29 0a 20 20 29 7b 0a 20 20 20 20 42 69 74 in). ){. Bit
36bd0 6d 61 73 6b 20 74 61 62 55 73 65 64 20 3d 20 65 mask tabUsed = e
36be0 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 xprListTableUsag
36bf0 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 52 65 73 e(pMaskSet, pRes
36c00 75 6c 74 53 65 74 29 3b 0a 20 20 20 20 69 66 28 ultSet);. if(
36c10 20 73 57 4c 42 2e 70 4f 72 64 65 72 42 79 20 29 sWLB.pOrderBy )
36c20 20 74 61 62 55 73 65 64 20 7c 3d 20 65 78 70 72 tabUsed |= expr
36c30 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 ListTableUsage(p
36c40 4d 61 73 6b 53 65 74 2c 20 73 57 4c 42 2e 70 4f MaskSet, sWLB.pO
36c50 72 64 65 72 42 79 29 3b 0a 20 20 20 20 77 68 69 rderBy);. whi
36c60 6c 65 28 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 le( pWInfo->nLev
36c70 65 6c 3e 3d 32 20 29 7b 0a 20 20 20 20 20 20 57 el>=2 ){. W
36c80 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 2c hereTerm *pTerm,
36c90 20 2a 70 45 6e 64 3b 0a 20 20 20 20 20 20 70 4c *pEnd;. pL
36ca0 6f 6f 70 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b oop = pWInfo->a[
36cb0 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 2d 31 pWInfo->nLevel-1
36cc0 5d 2e 70 57 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 ].pWLoop;.
36cd0 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e 70 54 61 if( (pWInfo->pTa
36ce0 62 4c 69 73 74 2d 3e 61 5b 70 4c 6f 6f 70 2d 3e bList->a[pLoop->
36cf0 69 54 61 62 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 iTab].jointype &
36d00 20 4a 54 5f 4c 45 46 54 29 3d 3d 30 20 29 20 62 JT_LEFT)==0 ) b
36d10 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66 28 20 reak;. if(
36d20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 (wctrlFlags & WH
36d30 45 52 45 5f 57 41 4e 54 5f 44 49 53 54 49 4e 43 ERE_WANT_DISTINC
36d40 54 29 3d 3d 30 0a 20 20 20 20 20 20 20 26 26 20 T)==0. &&
36d50 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 (pLoop->wsFlags
36d60 26 20 57 48 45 52 45 5f 4f 4e 45 52 4f 57 29 3d & WHERE_ONEROW)=
36d70 3d 30 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 =0. ){.
36d80 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 break;.
36d90 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 74 61 }. if( (ta
36da0 62 55 73 65 64 20 26 20 70 4c 6f 6f 70 2d 3e 6d bUsed & pLoop->m
36db0 61 73 6b 53 65 6c 66 29 21 3d 30 20 29 20 62 72 askSelf)!=0 ) br
36dc0 65 61 6b 3b 0a 20 20 20 20 20 20 70 45 6e 64 20 eak;. pEnd
36dd0 3d 20 73 57 4c 42 2e 70 57 43 2d 3e 61 20 2b 20 = sWLB.pWC->a +
36de0 73 57 4c 42 2e 70 57 43 2d 3e 6e 54 65 72 6d 3b sWLB.pWC->nTerm;
36df0 0a 20 20 20 20 20 20 66 6f 72 28 70 54 65 72 6d . for(pTerm
36e00 3d 73 57 4c 42 2e 70 57 43 2d 3e 61 3b 20 70 54 =sWLB.pWC->a; pT
36e10 65 72 6d 3c 70 45 6e 64 3b 20 70 54 65 72 6d 2b erm<pEnd; pTerm+
36e20 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 +){. if(
36e30 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c (pTerm->prereqAl
36e40 6c 20 26 20 70 4c 6f 6f 70 2d 3e 6d 61 73 6b 53 l & pLoop->maskS
36e50 65 6c 66 29 21 3d 30 0a 20 20 20 20 20 20 20 20 elf)!=0.
36e60 20 26 26 20 21 45 78 70 72 48 61 73 50 72 6f 70 && !ExprHasProp
36e70 65 72 74 79 28 70 54 65 72 6d 2d 3e 70 45 78 70 erty(pTerm->pExp
36e80 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 0a r, EP_FromJoin).
36e90 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 ){.
36ea0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
36eb0 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 }. }.
36ec0 20 20 20 20 69 66 28 20 70 54 65 72 6d 3c 70 45 if( pTerm<pE
36ed0 6e 64 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 nd ) break;.
36ee0 20 20 57 48 45 52 45 54 52 41 43 45 28 30 78 66 WHERETRACE(0xf
36ef0 66 66 66 2c 20 28 22 2d 3e 20 64 72 6f 70 20 6c fff, ("-> drop l
36f00 6f 6f 70 20 25 63 20 6e 6f 74 20 75 73 65 64 5c oop %c not used\
36f10 6e 22 2c 20 70 4c 6f 6f 70 2d 3e 63 49 64 29 29 n", pLoop->cId))
36f20 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 2d 3e ;. pWInfo->
36f30 6e 4c 65 76 65 6c 2d 2d 3b 0a 20 20 20 20 20 20 nLevel--;.
36f40 6e 54 61 62 4c 69 73 74 2d 2d 3b 0a 20 20 20 20 nTabList--;.
36f50 7d 0a 20 20 7d 0a 20 20 57 48 45 52 45 54 52 41 }. }. WHERETRA
36f60 43 45 28 30 78 66 66 66 66 2c 28 22 2a 2a 2a 20 CE(0xffff,("***
36f70 4f 70 74 69 6d 69 7a 65 72 20 46 69 6e 69 73 68 Optimizer Finish
36f80 65 64 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20 70 ed ***\n"));. p
36f90 57 49 6e 66 6f 2d 3e 70 50 61 72 73 65 2d 3e 6e WInfo->pParse->n
36fa0 51 75 65 72 79 4c 6f 6f 70 20 2b 3d 20 70 57 49 QueryLoop += pWI
36fb0 6e 66 6f 2d 3e 6e 52 6f 77 4f 75 74 3b 0a 0a 20 nfo->nRowOut;..
36fc0 20 2f 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 /* If the calle
36fd0 72 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 6f r is an UPDATE o
36fe0 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 r DELETE stateme
36ff0 6e 74 20 74 68 61 74 20 69 73 20 72 65 71 75 65 nt that is reque
37000 73 74 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 75 73 sting. ** to us
37010 65 20 61 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 e a one-pass alg
37020 6f 72 69 74 68 6d 2c 20 64 65 74 65 72 6d 69 6e orithm, determin
37030 65 20 69 66 20 74 68 69 73 20 69 73 20 61 70 70 e if this is app
37040 72 6f 70 72 69 61 74 65 2e 0a 20 20 2a 2a 20 54 ropriate.. ** T
37050 68 65 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f he one-pass algo
37060 72 69 74 68 6d 20 6f 6e 6c 79 20 77 6f 72 6b 73 rithm only works
37070 20 69 66 20 74 68 65 20 57 48 45 52 45 20 63 6c if the WHERE cl
37080 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 73 0a ause constrains.
37090 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 ** the stateme
370a0 6e 74 20 74 6f 20 75 70 64 61 74 65 20 61 20 73 nt to update a s
370b0 69 6e 67 6c 65 20 72 6f 77 2e 0a 20 20 2a 2f 0a ingle row.. */.
370c0 20 20 61 73 73 65 72 74 28 20 28 77 63 74 72 6c assert( (wctrl
370d0 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4e Flags & WHERE_ON
370e0 45 50 41 53 53 5f 44 45 53 49 52 45 44 29 3d 3d EPASS_DESIRED)==
370f0 30 20 7c 7c 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 0 || pWInfo->nLe
37100 76 65 6c 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 vel==1 );. if(
37110 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 (wctrlFlags & WH
37120 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 ERE_ONEPASS_DESI
37130 52 45 44 29 21 3d 30 20 0a 20 20 20 26 26 20 28 RED)!=0 . && (
37140 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 70 57 4c pWInfo->a[0].pWL
37150 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 oop->wsFlags & W
37160 48 45 52 45 5f 4f 4e 45 52 4f 57 29 21 3d 30 20 HERE_ONEROW)!=0
37170 29 7b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 6f ){. pWInfo->o
37180 6b 4f 6e 65 50 61 73 73 20 3d 20 31 3b 0a 20 20 kOnePass = 1;.
37190 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 70 pWInfo->a[0].p
371a0 57 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 WLoop->wsFlags &
371b0 3d 20 7e 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c = ~WHERE_IDX_ONL
371c0 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 Y;. }.. /* Ope
371d0 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 n all tables in
371e0 74 68 65 20 70 54 61 62 4c 69 73 74 20 61 6e 64 the pTabList and
371f0 20 61 6e 79 20 69 6e 64 69 63 65 73 20 73 65 6c any indices sel
37200 65 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73 ected for. ** s
37210 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65 20 74 earching those t
37220 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 6e 6f ables.. */. no
37230 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d 61 tReady = ~(Bitma
37240 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30 sk)0;. for(ii=0
37250 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d , pLevel=pWInfo-
37260 3e 61 3b 20 69 69 3c 6e 54 61 62 4c 69 73 74 3b >a; ii<nTabList;
37270 20 69 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 ii++, pLevel++)
37280 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 {. Table *pTa
37290 62 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 b; /* Table
372a0 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20 69 to open */. i
372b0 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 nt iDb;
372c0 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 /* Index of data
372d0 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 base containing
372e0 74 61 62 6c 65 2f 69 6e 64 65 78 20 2a 2f 0a 20 table/index */.
372f0 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 struct SrcLis
37300 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d t_item *pTabItem
37310 3b 0a 0a 20 20 20 20 70 54 61 62 49 74 65 6d 20 ;.. pTabItem
37320 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 = &pTabList->a[p
37330 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 Level->iFrom];.
37340 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 49 74 pTab = pTabIt
37350 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 44 em->pTab;. iD
37360 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d b = sqlite3Schem
37370 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 aToIndex(db, pTa
37380 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 b->pSchema);.
37390 20 70 4c 6f 6f 70 20 3d 20 70 4c 65 76 65 6c 2d pLoop = pLevel-
373a0 3e 70 57 4c 6f 6f 70 3b 0a 20 20 20 20 69 66 28 >pWLoop;. if(
373b0 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 (pTab->tabFlags
373c0 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 & TF_Ephemeral)
373d0 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 !=0 || pTab->pSe
373e0 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a lect ){. /*
373f0 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 Do nothing */.
37400 20 20 20 7d 65 6c 73 65 0a 23 69 66 6e 64 65 66 }else.#ifndef
37410 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 SQLITE_OMIT_VIR
37420 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 TUALTABLE. if
37430 28 20 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 ( (pLoop->wsFlag
37440 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41 s & WHERE_VIRTUA
37450 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20 LTABLE)!=0 ){.
37460 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a const char *
37470 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 pVTab = (const c
37480 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 har *)sqlite3Get
37490 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 VTable(db, pTab)
374a0 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 ;. int iCur
374b0 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 = pTabItem->iCu
374c0 72 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 rsor;. sqli
374d0 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c te3VdbeAddOp4(v,
374e0 20 4f 50 5f 56 4f 70 65 6e 2c 20 69 43 75 72 2c OP_VOpen, iCur,
374f0 20 30 2c 20 30 2c 20 70 56 54 61 62 2c 20 50 34 0, 0, pVTab, P4
37500 5f 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 _VTAB);. }els
37510 65 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 e if( IsVirtual(
37520 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 2f pTab) ){. /
37530 2a 20 6e 6f 6f 70 20 2a 2f 0a 20 20 20 20 7d 65 * noop */. }e
37540 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 lse.#endif. i
37550 66 28 20 28 70 4c 6f 6f 70 2d 3e 77 73 46 6c 61 f( (pLoop->wsFla
37560 67 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f gs & WHERE_IDX_O
37570 4e 4c 59 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 NLY)==0.
37580 20 26 26 20 28 77 63 74 72 6c 46 6c 61 67 73 20 && (wctrlFlags
37590 26 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 & WHERE_OMIT_OPE
375a0 4e 5f 43 4c 4f 53 45 29 3d 3d 30 20 29 7b 0a 20 N_CLOSE)==0 ){.
375b0 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 57 int op = pW
375c0 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 Info->okOnePass
375d0 3f 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 3a ? OP_OpenWrite :
375e0 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3b 0a 20 20 OP_OpenRead;.
375f0 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 sqlite3OpenT
37600 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 able(pParse, pTa
37610 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 2c 20 bItem->iCursor,
37620 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29 3b 0a iDb, pTab, op);.
37630 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 testcase(
37640 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 !pWInfo->okOnePa
37650 73 73 20 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c ss && pTab->nCol
37660 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20 20 20 20 20 ==BMS-1 );.
37670 20 74 65 73 74 63 61 73 65 28 20 21 70 57 49 6e testcase( !pWIn
37680 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 26 26 fo->okOnePass &&
37690 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d 42 4d 53 pTab->nCol==BMS
376a0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 );. if( !p
376b0 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 WInfo->okOnePass
376c0 20 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c 3c 42 && pTab->nCol<B
376d0 4d 53 20 29 7b 0a 20 20 20 20 20 20 20 20 42 69 MS ){. Bi
376e0 74 6d 61 73 6b 20 62 20 3d 20 70 54 61 62 49 74 tmask b = pTabIt
376f0 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 em->colUsed;.
37700 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a int n = 0;.
37710 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20 62 3b for(; b;
37720 20 62 3d 62 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d 0a b=b>>1, n++){}.
37730 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
37740 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 73 dbeChangeP4(v, s
37750 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
37760 74 41 64 64 72 28 76 29 2d 31 2c 20 0a 20 20 20 tAddr(v)-1, .
37770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
37780 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f SQLITE_
37790 49 4e 54 5f 54 4f 5f 50 54 52 28 6e 29 2c 20 50 INT_TO_PTR(n), P
377a0 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 4_INT32);.
377b0 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 assert( n<=pTa
377c0 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 b->nCol );.
377d0 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }. }else{.
377e0 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 sqlite3Table
377f0 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 Lock(pParse, iDb
37800 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c , pTab->tnum, 0,
37810 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pTab->zName);.
37820 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 6f }. if( pLo
37830 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 57 48 op->wsFlags & WH
37840 45 52 45 5f 49 4e 44 45 58 45 44 20 29 7b 0a 20 ERE_INDEXED ){.
37850 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 78 20 Index *pIx
37860 3d 20 70 4c 6f 6f 70 2d 3e 75 2e 62 74 72 65 65 = pLoop->u.btree
37870 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 4b .pIndex;. K
37880 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 eyInfo *pKey = s
37890 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e qlite3IndexKeyin
378a0 66 6f 28 70 50 61 72 73 65 2c 20 70 49 78 29 3b fo(pParse, pIx);
378b0 0a 20 20 20 20 20 20 2f 2a 20 46 49 58 4d 45 3a . /* FIXME:
378c0 20 20 41 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 As an optimiza
378d0 74 69 6f 6e 20 75 73 65 20 70 54 61 62 49 74 65 tion use pTabIte
378e0 6d 2d 3e 69 43 75 72 73 6f 72 20 69 66 20 57 48 m->iCursor if WH
378f0 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 2a 2f 0a ERE_IDX_ONLY */.
37900 20 20 20 20 20 20 69 6e 74 20 69 49 6e 64 65 78 int iIndex
37910 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 Cur = pLevel->iI
37920 64 78 43 75 72 20 3d 20 69 49 64 78 43 75 72 20 dxCur = iIdxCur
37930 3f 20 69 49 64 78 43 75 72 20 3a 20 70 50 61 72 ? iIdxCur : pPar
37940 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 se->nTab++;.
37950 20 20 61 73 73 65 72 74 28 20 70 49 78 2d 3e 70 assert( pIx->p
37960 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 Schema==pTab->pS
37970 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 61 chema );. a
37980 73 73 65 72 74 28 20 69 49 6e 64 65 78 43 75 72 ssert( iIndexCur
37990 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c >=0 );. sql
379a0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
379b0 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 , OP_OpenRead, i
379c0 49 6e 64 65 78 43 75 72 2c 20 70 49 78 2d 3e 74 IndexCur, pIx->t
379d0 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 num, iDb,.
379e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
379f0 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 (char*)pKey, P
37a00 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 4_KEYINFO_HANDOF
37a10 46 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f F);. VdbeCo
37a20 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 mment((v, "%s",
37a30 70 49 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 pIx->zName));.
37a40 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 43 }. sqlite3C
37a50 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 odeVerifySchema(
37a60 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 pParse, iDb);.
37a70 20 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 notReady &= ~g
37a80 65 74 4d 61 73 6b 28 26 70 57 49 6e 66 6f 2d 3e etMask(&pWInfo->
37a90 73 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74 sMaskSet, pTabIt
37aa0 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 em->iCursor);.
37ab0 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 }. pWInfo->iTop
37ac0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 = sqlite3VdbeCu
37ad0 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 rrentAddr(v);.
37ae0 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
37af0 69 6c 65 64 20 29 20 67 6f 74 6f 20 77 68 65 72 iled ) goto wher
37b00 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a 0a 20 20 eBeginError;..
37b10 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 /* Generate the
37b20 63 6f 64 65 20 74 6f 20 64 6f 20 74 68 65 20 73 code to do the s
37b30 65 61 72 63 68 2e 20 20 45 61 63 68 20 69 74 65 earch. Each ite
37b40 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f ration of the fo
37b50 72 0a 20 20 2a 2a 20 6c 6f 6f 70 20 62 65 6c 6f r. ** loop belo
37b60 77 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 w generates code
37b70 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 6e 65 for a single ne
37b80 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 sted loop of the
37b90 20 56 4d 0a 20 20 2a 2a 20 70 72 6f 67 72 61 6d VM. ** program
37ba0 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 .. */. notRead
37bb0 79 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b y = ~(Bitmask)0;
37bc0 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c . for(ii=0; ii<
37bd0 6e 54 61 62 4c 69 73 74 3b 20 69 69 2b 2b 29 7b nTabList; ii++){
37be0 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 . pLevel = &p
37bf0 57 49 6e 66 6f 2d 3e 61 5b 69 69 5d 3b 0a 23 69 WInfo->a[ii];.#i
37c00 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
37c10 54 5f 41 55 54 4f 4d 41 54 49 43 5f 49 4e 44 45 T_AUTOMATIC_INDE
37c20 58 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 X. if( (pLeve
37c30 6c 2d 3e 70 57 4c 6f 6f 70 2d 3e 77 73 46 6c 61 l->pWLoop->wsFla
37c40 67 73 20 26 20 57 48 45 52 45 5f 41 55 54 4f 5f gs & WHERE_AUTO_
37c50 49 4e 44 45 58 29 21 3d 30 20 29 7b 0a 20 20 20 INDEX)!=0 ){.
37c60 20 20 20 63 6f 6e 73 74 72 75 63 74 41 75 74 6f constructAuto
37c70 6d 61 74 69 63 49 6e 64 65 78 28 70 50 61 72 73 maticIndex(pPars
37c80 65 2c 20 26 70 57 49 6e 66 6f 2d 3e 73 57 43 2c e, &pWInfo->sWC,
37c90 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
37ca0 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c &pTabList->a[pL
37cb0 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 2c 20 6e 6f evel->iFrom], no
37cc0 74 52 65 61 64 79 2c 20 70 4c 65 76 65 6c 29 3b tReady, pLevel);
37cd0 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d . if( db->m
37ce0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f allocFailed ) go
37cf0 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 to whereBeginErr
37d00 6f 72 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 or;. }.#endif
37d10 0a 20 20 20 20 65 78 70 6c 61 69 6e 4f 6e 65 53 . explainOneS
37d20 63 61 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 can(pParse, pTab
37d30 4c 69 73 74 2c 20 70 4c 65 76 65 6c 2c 20 69 69 List, pLevel, ii
37d40 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 2c , pLevel->iFrom,
37d50 20 77 63 74 72 6c 46 6c 61 67 73 29 3b 0a 20 20 wctrlFlags);.
37d60 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 6f pLevel->addrBo
37d70 64 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dy = sqlite3Vdbe
37d80 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a CurrentAddr(v);.
37d90 20 20 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 63 notReady = c
37da0 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72 74 28 odeOneLoopStart(
37db0 70 57 49 6e 66 6f 2c 20 69 69 2c 20 6e 6f 74 52 pWInfo, ii, notR
37dc0 65 61 64 79 29 3b 0a 20 20 20 20 70 57 49 6e 66 eady);. pWInf
37dd0 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 20 3d 20 70 o->iContinue = p
37de0 4c 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 3b Level->addrCont;
37df0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 6e 65 2e . }.. /* Done.
37e00 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70 57 49 */. return pWI
37e10 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 nfo;.. /* Jump
37e20 68 65 72 65 20 69 66 20 6d 61 6c 6c 6f 63 20 66 here if malloc f
37e30 61 69 6c 73 20 2a 2f 0a 77 68 65 72 65 42 65 67 ails */.whereBeg
37e40 69 6e 45 72 72 6f 72 3a 0a 20 20 69 66 28 20 70 inError:. if( p
37e50 57 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70 50 61 WInfo ){. pPa
37e60 72 73 65 2d 3e 6e 51 75 65 72 79 4c 6f 6f 70 20 rse->nQueryLoop
37e70 3d 20 70 57 49 6e 66 6f 2d 3e 73 61 76 65 64 4e = pWInfo->savedN
37e80 51 75 65 72 79 4c 6f 6f 70 3b 0a 20 20 20 20 77 QueryLoop;. w
37e90 68 65 72 65 49 6e 66 6f 46 72 65 65 28 64 62 2c hereInfoFree(db,
37ea0 20 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 20 20 pWInfo);. }.
37eb0 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a return 0;.}../*.
37ec0 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 ** Generate the
37ed0 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45 end of the WHERE
37ee0 20 6c 6f 6f 70 2e 20 20 53 65 65 20 63 6f 6d 6d loop. See comm
37ef0 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73 71 6c 69 ents on .** sqli
37f00 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 te3WhereBegin()
37f10 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 for additional i
37f20 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 76 nformation..*/.v
37f30 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72 65 oid sqlite3Where
37f40 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 20 2a 70 End(WhereInfo *p
37f50 57 49 6e 66 6f 29 7b 0a 20 20 50 61 72 73 65 20 WInfo){. Parse
37f60 2a 70 50 61 72 73 65 20 3d 20 70 57 49 6e 66 6f *pParse = pWInfo
37f70 2d 3e 70 50 61 72 73 65 3b 0a 20 20 56 64 62 65 ->pParse;. Vdbe
37f80 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 *v = pParse->pV
37f90 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 dbe;. int i;.
37fa0 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 WhereLevel *pLev
37fb0 65 6c 3b 0a 20 20 57 68 65 72 65 4c 6f 6f 70 20 el;. WhereLoop
37fc0 2a 70 4c 6f 6f 70 3b 0a 20 20 53 72 63 4c 69 73 *pLoop;. SrcLis
37fd0 74 20 2a 70 54 61 62 4c 69 73 74 20 3d 20 70 57 t *pTabList = pW
37fe0 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 3b 0a Info->pTabList;.
37ff0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 sqlite3 *db =
38000 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f pParse->db;.. /
38010 2a 20 47 65 6e 65 72 61 74 65 20 6c 6f 6f 70 20 * Generate loop
38020 74 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 termination code
38030 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
38040 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 ExprCacheClear(p
38050 50 61 72 73 65 29 3b 0a 20 20 66 6f 72 28 69 3d Parse);. for(i=
38060 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 2d 31 pWInfo->nLevel-1
38070 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 ; i>=0; i--){.
38080 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e pLevel = &pWIn
38090 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4c fo->a[i];. pL
380a0 6f 6f 70 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 57 oop = pLevel->pW
380b0 4c 6f 6f 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 Loop;. sqlite
380c0 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
380d0 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 l(v, pLevel->add
380e0 72 43 6f 6e 74 29 3b 0a 20 20 20 20 69 66 28 20 rCont);. if(
380f0 70 4c 65 76 65 6c 2d 3e 6f 70 21 3d 4f 50 5f 4e pLevel->op!=OP_N
38100 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c oop ){. sql
38110 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
38120 2c 20 70 4c 65 76 65 6c 2d 3e 6f 70 2c 20 70 4c , pLevel->op, pL
38130 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c evel->p1, pLevel
38140 2d 3e 70 32 29 3b 0a 20 20 20 20 20 20 73 71 6c ->p2);. sql
38150 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 ite3VdbeChangeP5
38160 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 70 35 29 3b (v, pLevel->p5);
38170 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
38180 4c 6f 6f 70 2d 3e 77 73 46 6c 61 67 73 20 26 20 Loop->wsFlags &
38190 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2