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 66 ******.** This f
0180: 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 ile contains C c
0190: 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 ode routines tha
01a0: 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 t are called by
01b0: 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f the parser.** to
01c0: 20 68 61 6e 64 6c 65 20 53 45 4c 45 43 54 20 73 handle SELECT s
01d0: 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c tatements in SQL
01e0: 69 74 65 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 ite..*/.#include
01f0: 20 22 73 71 6c 69 74 65 49 6e 74 2e 68 22 0a 0a "sqliteInt.h"..
0200: 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f 75 74 70 /*.** Trace outp
0210: 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23 69 66 ut macros.*/.#if
0220: 20 53 45 4c 45 43 54 54 52 41 43 45 5f 45 4e 41 SELECTTRACE_ENA
0230: 42 4c 45 44 0a 2f 2a 2a 2a 2f 20 69 6e 74 20 73 BLED./***/ int s
0240: 71 6c 69 74 65 33 53 65 6c 65 63 74 54 72 61 63 qlite3SelectTrac
0250: 65 20 3d 20 30 3b 0a 23 20 64 65 66 69 6e 65 20 e = 0;.# define
0260: 53 45 4c 45 43 54 54 52 41 43 45 28 4b 2c 50 2c SELECTTRACE(K,P,
0270: 53 2c 58 29 20 20 5c 0a 20 20 69 66 28 73 71 6c S,X) \. if(sql
0280: 69 74 65 33 53 65 6c 65 63 74 54 72 61 63 65 26 ite3SelectTrace&
0290: 28 4b 29 29 20 20 20 5c 0a 20 20 20 20 73 71 6c (K)) \. sql
02a0: 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 ite3DebugPrintf(
02b0: 22 25 2a 73 25 73 2e 25 70 3a 20 22 2c 28 50 29 "%*s%s.%p: ",(P)
02c0: 2d 3e 6e 53 65 6c 65 63 74 49 6e 64 65 6e 74 2a ->nSelectIndent*
02d0: 32 2d 32 2c 22 22 2c 28 53 29 2d 3e 7a 53 65 6c 2-2,"",(S)->zSel
02e0: 4e 61 6d 65 2c 28 53 29 29 2c 5c 0a 20 20 20 20 Name,(S)),\.
02f0: 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e sqlite3DebugPrin
0300: 74 66 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 tf X.#else.# def
0310: 69 6e 65 20 53 45 4c 45 43 54 54 52 41 43 45 28 ine SELECTTRACE(
0320: 4b 2c 50 2c 53 2c 58 29 0a 23 65 6e 64 69 66 0a K,P,S,X).#endif.
0330: 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
0340: 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
0350: 77 69 6e 67 20 6f 62 6a 65 63 74 20 69 73 20 75 wing object is u
0360: 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 69 6e sed to record in
0370: 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 0a formation about.
0380: 2a 2a 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 73 ** how to proces
0390: 73 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b s the DISTINCT k
03a0: 65 79 77 6f 72 64 2c 20 74 6f 20 73 69 6d 70 6c eyword, to simpl
03b0: 69 66 79 20 70 61 73 73 69 6e 67 20 74 68 61 74 ify passing that
03c0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 information.**
03d0: 69 6e 74 6f 20 74 68 65 20 73 65 6c 65 63 74 49 into the selectI
03e0: 6e 6e 65 72 4c 6f 6f 70 28 29 20 72 6f 75 74 69 nnerLoop() routi
03f0: 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 ne..*/.typedef s
0400: 74 72 75 63 74 20 44 69 73 74 69 6e 63 74 43 74 truct DistinctCt
0410: 78 20 44 69 73 74 69 6e 63 74 43 74 78 3b 0a 73 x DistinctCtx;.s
0420: 74 72 75 63 74 20 44 69 73 74 69 6e 63 74 43 74 truct DistinctCt
0430: 78 20 7b 0a 20 20 75 38 20 69 73 54 6e 63 74 3b x {. u8 isTnct;
0440: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 /* True if
0450: 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 the DISTINCT ke
0460: 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 yword is present
0470: 20 2a 2f 0a 20 20 75 38 20 65 54 6e 63 74 54 79 */. u8 eTnctTy
0480: 70 65 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 pe; /* One of
0490: 74 68 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e the WHERE_DISTIN
04a0: 43 54 5f 2a 20 6f 70 65 72 61 74 6f 72 73 20 2a CT_* operators *
04b0: 2f 0a 20 20 69 6e 74 20 74 61 62 54 6e 63 74 3b /. int tabTnct;
04c0: 20 20 20 20 2f 2a 20 45 70 68 65 6d 65 72 61 6c /* Ephemeral
04d0: 20 74 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 table used for
04e0: 44 49 53 54 49 4e 43 54 20 70 72 6f 63 65 73 73 DISTINCT process
04f0: 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 ing */. int add
0500: 72 54 6e 63 74 3b 20 20 20 2f 2a 20 41 64 64 72 rTnct; /* Addr
0510: 65 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 ess of OP_OpenEp
0520: 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20 66 hemeral opcode f
0530: 6f 72 20 74 61 62 54 6e 63 74 20 2a 2f 0a 7d 3b or tabTnct */.};
0540: 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 ../*.** An insta
0550: 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f nce of the follo
0560: 77 69 6e 67 20 6f 62 6a 65 63 74 20 69 73 20 75 wing object is u
0570: 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 69 6e sed to record in
0580: 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 0a formation about.
0590: 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 ** the ORDER BY
05a0: 28 6f 72 20 47 52 4f 55 50 20 42 59 29 20 63 6c (or GROUP BY) cl
05b0: 61 75 73 65 20 6f 66 20 71 75 65 72 79 20 69 73 ause of query is
05c0: 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 0a 2a 2f being coded..*/
05d0: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 .typedef struct
05e0: 53 6f 72 74 43 74 78 20 53 6f 72 74 43 74 78 3b SortCtx SortCtx;
05f0: 0a 73 74 72 75 63 74 20 53 6f 72 74 43 74 78 20 .struct SortCtx
0600: 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f {. ExprList *pO
0610: 72 64 65 72 42 79 3b 20 20 20 2f 2a 20 54 68 65 rderBy; /* The
0620: 20 4f 52 44 45 52 20 42 59 20 28 6f 72 20 47 52 ORDER BY (or GR
0630: 4f 55 50 20 42 59 20 63 6c 61 75 73 65 29 20 2a OUP BY clause) *
0640: 2f 0a 20 20 69 6e 74 20 6e 4f 42 53 61 74 3b 20 /. int nOBSat;
0650: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
0660: 62 65 72 20 6f 66 20 4f 52 44 45 52 20 42 59 20 ber of ORDER BY
0670: 74 65 72 6d 73 20 73 61 74 69 73 66 69 65 64 20 terms satisfied
0680: 62 79 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 by indices */.
0690: 69 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 int iECursor;
06a0: 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 /* Cursor
06b0: 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73 number for the s
06c0: 6f 72 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 orter */. int r
06d0: 65 67 52 65 74 75 72 6e 3b 20 20 20 20 20 20 20 egReturn;
06e0: 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c /* Register hol
06f0: 64 69 6e 67 20 62 6c 6f 63 6b 2d 6f 75 74 70 75 ding block-outpu
0700: 74 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 t return address
0710: 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 42 */. int labelB
0720: 6b 4f 75 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 kOut; /* S
0730: 74 61 72 74 20 6c 61 62 65 6c 20 66 6f 72 20 74 tart label for t
0740: 68 65 20 62 6c 6f 63 6b 2d 6f 75 74 70 75 74 20 he block-output
0750: 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 subroutine */.
0760: 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e 64 65 int addrSortInde
0770: 78 3b 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 x; /* Address
0780: 20 6f 66 20 74 68 65 20 4f 50 5f 53 6f 72 74 65 of the OP_Sorte
0790: 72 4f 70 65 6e 20 6f 72 20 4f 50 5f 4f 70 65 6e rOpen or OP_Open
07a0: 45 70 68 65 6d 65 72 61 6c 20 2a 2f 0a 20 20 75 Ephemeral */. u
07b0: 38 20 73 6f 72 74 46 6c 61 67 73 3b 20 20 20 20 8 sortFlags;
07c0: 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20 /* Zero or
07d0: 6d 6f 72 65 20 53 4f 52 54 46 4c 41 47 5f 2a 20 more SORTFLAG_*
07e0: 62 69 74 73 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 bits */.};.#defi
07f0: 6e 65 20 53 4f 52 54 46 4c 41 47 5f 55 73 65 53 ne SORTFLAG_UseS
0800: 6f 72 74 65 72 20 20 30 78 30 31 20 20 20 2f 2a orter 0x01 /*
0810: 20 55 73 65 20 53 6f 72 74 65 72 4f 70 65 6e 20 Use SorterOpen
0820: 69 6e 73 74 65 61 64 20 6f 66 20 4f 70 65 6e 45 instead of OpenE
0830: 70 68 65 6d 65 72 61 6c 20 2a 2f 0a 0a 2f 2a 0a phemeral */../*.
0840: 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 74 68 ** Delete all th
0850: 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 53 e content of a S
0860: 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 65 2e elect structure.
0870: 20 20 44 65 61 6c 6c 6f 63 61 74 65 20 74 68 65 Deallocate the
0880: 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 74 structure.** it
0890: 73 65 6c 66 20 6f 6e 6c 79 20 69 66 20 62 46 72 self only if bFr
08a0: 65 65 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 ee is true..*/.s
08b0: 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 tatic void clear
08c0: 53 65 6c 65 63 74 28 73 71 6c 69 74 65 33 20 2a Select(sqlite3 *
08d0: 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 db, Select *p, i
08e0: 6e 74 20 62 46 72 65 65 29 7b 0a 20 20 77 68 69 nt bFree){. whi
08f0: 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 53 65 6c le( p ){. Sel
0900: 65 63 74 20 2a 70 50 72 69 6f 72 20 3d 20 70 2d ect *pPrior = p-
0910: 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 73 71 6c >pPrior;. sql
0920: 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
0930: 74 65 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 te(db, p->pEList
0940: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 );. sqlite3Sr
0950: 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 cListDelete(db,
0960: 70 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 73 71 p->pSrc);. sq
0970: 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 lite3ExprDelete(
0980: 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a db, p->pWhere);.
0990: 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c sqlite3ExprL
09a0: 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d istDelete(db, p-
09b0: 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20 >pGroupBy);.
09c0: 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
09d0: 65 28 64 62 2c 20 70 2d 3e 70 48 61 76 69 6e 67 e(db, p->pHaving
09e0: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 );. sqlite3Ex
09f0: 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c prListDelete(db,
0a00: 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 p->pOrderBy);.
0a10: 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 sqlite3ExprDe
0a20: 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d lete(db, p->pLim
0a30: 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 it);. sqlite3
0a40: 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
0a50: 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 ->pOffset);.
0a60: 73 71 6c 69 74 65 33 57 69 74 68 44 65 6c 65 74 sqlite3WithDelet
0a70: 65 28 64 62 2c 20 70 2d 3e 70 57 69 74 68 29 3b e(db, p->pWith);
0a80: 0a 20 20 20 20 69 66 28 20 62 46 72 65 65 20 29 . if( bFree )
0a90: 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
0aa0: 62 2c 20 70 29 3b 0a 20 20 20 20 70 20 3d 20 70 b, p);. p = p
0ab0: 50 72 69 6f 72 3b 0a 20 20 20 20 62 46 72 65 65 Prior;. bFree
0ac0: 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a = 1;. }.}../*.
0ad0: 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 ** Initialize a
0ae0: 53 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63 SelectDest struc
0af0: 74 75 72 65 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ture..*/.void sq
0b00: 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 lite3SelectDestI
0b10: 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74 20 2a nit(SelectDest *
0b20: 70 44 65 73 74 2c 20 69 6e 74 20 65 44 65 73 74 pDest, int eDest
0b30: 2c 20 69 6e 74 20 69 50 61 72 6d 29 7b 0a 20 20 , int iParm){.
0b40: 70 44 65 73 74 2d 3e 65 44 65 73 74 20 3d 20 28 pDest->eDest = (
0b50: 75 38 29 65 44 65 73 74 3b 0a 20 20 70 44 65 73 u8)eDest;. pDes
0b60: 74 2d 3e 69 53 44 50 61 72 6d 20 3d 20 69 50 61 t->iSDParm = iPa
0b70: 72 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 61 66 66 rm;. pDest->aff
0b80: 53 64 73 74 20 3d 20 30 3b 0a 20 20 70 44 65 73 Sdst = 0;. pDes
0b90: 74 2d 3e 69 53 64 73 74 20 3d 20 30 3b 0a 20 20 t->iSdst = 0;.
0ba0: 70 44 65 73 74 2d 3e 6e 53 64 73 74 20 3d 20 30 pDest->nSdst = 0
0bb0: 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f ;.}.../*.** Allo
0bc0: 63 61 74 65 20 61 20 6e 65 77 20 53 65 6c 65 63 cate a new Selec
0bd0: 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 t structure and
0be0: 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 return a pointer
0bf0: 20 74 6f 20 74 68 61 74 0a 2a 2a 20 73 74 72 75 to that.** stru
0c00: 63 74 75 72 65 2e 0a 2a 2f 0a 53 65 6c 65 63 74 cture..*/.Select
0c10: 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e *sqlite3SelectN
0c20: 65 77 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 ew(. Parse *pPa
0c30: 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 rse, /* P
0c40: 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a arsing context *
0c50: 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
0c60: 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 77 68 69 List, /* whi
0c70: 63 68 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e ch columns to in
0c80: 63 6c 75 64 65 20 69 6e 20 74 68 65 20 72 65 73 clude in the res
0c90: 75 6c 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 ult */. SrcList
0ca0: 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 2f *pSrc, /
0cb0: 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 * the FROM claus
0cc0: 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65 e -- which table
0cd0: 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45 s to scan */. E
0ce0: 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 xpr *pWhere,
0cf0: 20 20 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 /* the WHER
0d00: 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 E clause */. Ex
0d10: 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 prList *pGroupBy
0d20: 2c 20 20 20 2f 2a 20 74 68 65 20 47 52 4f 55 50 , /* the GROUP
0d30: 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 BY clause */.
0d40: 45 78 70 72 20 2a 70 48 61 76 69 6e 67 2c 20 20 Expr *pHaving,
0d50: 20 20 20 20 20 20 2f 2a 20 74 68 65 20 48 41 56 /* the HAV
0d60: 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 ING clause */.
0d70: 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
0d80: 42 79 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52 44 By, /* the ORD
0d90: 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a ER BY clause */.
0da0: 20 20 75 31 36 20 73 65 6c 46 6c 61 67 73 2c 20 u16 selFlags,
0db0: 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 20 /* Flag
0dc0: 70 61 72 61 6d 65 74 65 72 73 2c 20 73 75 63 68 parameters, such
0dd0: 20 61 73 20 53 46 5f 44 69 73 74 69 6e 63 74 20 as SF_Distinct
0de0: 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 */. Expr *pLimi
0df0: 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 t, /* LI
0e00: 4d 49 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c MIT value. NULL
0e10: 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 20 means not used
0e20: 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66 73 */. Expr *pOffs
0e30: 65 74 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 46 et /* OF
0e40: 46 53 45 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c FSET value. NUL
0e50: 4c 20 6d 65 61 6e 73 20 6e 6f 20 6f 66 66 73 65 L means no offse
0e60: 74 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 t */.){. Select
0e70: 20 2a 70 4e 65 77 3b 0a 20 20 53 65 6c 65 63 74 *pNew;. Select
0e80: 20 73 74 61 6e 64 69 6e 3b 0a 20 20 73 71 6c 69 standin;. sqli
0e90: 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
0ea0: 2d 3e 64 62 3b 0a 20 20 70 4e 65 77 20 3d 20 73 ->db;. pNew = s
0eb0: 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 qlite3DbMallocZe
0ec0: 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 ro(db, sizeof(*p
0ed0: 4e 65 77 29 20 29 3b 0a 20 20 61 73 73 65 72 74 New) );. assert
0ee0: 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
0ef0: 65 64 20 7c 7c 20 21 70 4f 66 66 73 65 74 20 7c ed || !pOffset |
0f00: 7c 20 70 4c 69 6d 69 74 20 29 3b 20 2f 2a 20 4f | pLimit ); /* O
0f10: 46 46 53 45 54 20 69 6d 70 6c 69 65 73 20 4c 49 FFSET implies LI
0f20: 4d 49 54 20 2a 2f 0a 20 20 69 66 28 20 70 4e 65 MIT */. if( pNe
0f30: 77 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 w==0 ){. asse
0f40: 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 rt( db->mallocFa
0f50: 69 6c 65 64 20 29 3b 0a 20 20 20 20 70 4e 65 77 iled );. pNew
0f60: 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a 20 20 20 = &standin;.
0f70: 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30 2c memset(pNew, 0,
0f80: 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b sizeof(*pNew));
0f90: 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 4c 69 73 . }. if( pELis
0fa0: 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 45 4c 69 t==0 ){. pELi
0fb0: 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 st = sqlite3Expr
0fc0: 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 ListAppend(pPars
0fd0: 65 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70 e, 0, sqlite3Exp
0fe0: 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30 29 29 3b r(db,TK_ALL,0));
0ff0: 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 70 45 4c . }. pNew->pEL
1000: 69 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 ist = pEList;.
1010: 69 66 28 20 70 53 72 63 3d 3d 30 20 29 20 70 53 if( pSrc==0 ) pS
1020: 72 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 rc = sqlite3DbMa
1030: 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
1040: 65 6f 66 28 2a 70 53 72 63 29 29 3b 0a 20 20 70 eof(*pSrc));. p
1050: 4e 65 77 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 New->pSrc = pSrc
1060: 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 ;. pNew->pWhere
1070: 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 70 4e 65 = pWhere;. pNe
1080: 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d 20 70 47 w->pGroupBy = pG
1090: 72 6f 75 70 42 79 3b 0a 20 20 70 4e 65 77 2d 3e roupBy;. pNew->
10a0: 70 48 61 76 69 6e 67 20 3d 20 70 48 61 76 69 6e pHaving = pHavin
10b0: 67 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 g;. pNew->pOrde
10c0: 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a rBy = pOrderBy;.
10d0: 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 pNew->selFlags
10e0: 20 3d 20 73 65 6c 46 6c 61 67 73 3b 0a 20 20 70 = selFlags;. p
10f0: 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c New->op = TK_SEL
1100: 45 43 54 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 ECT;. pNew->pLi
1110: 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 mit = pLimit;.
1120: 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 pNew->pOffset =
1130: 70 4f 66 66 73 65 74 3b 0a 20 20 61 73 73 65 72 pOffset;. asser
1140: 74 28 20 70 4f 66 66 73 65 74 3d 3d 30 20 7c 7c t( pOffset==0 ||
1150: 20 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20 pLimit!=0 );.
1160: 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 pNew->addrOpenEp
1170: 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e hm[0] = -1;. pN
1180: 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d ew->addrOpenEphm
1190: 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 [1] = -1;. if(
11a0: 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
11b0: 20 29 20 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 ) {. clearSe
11c0: 6c 65 63 74 28 64 62 2c 20 70 4e 65 77 2c 20 70 lect(db, pNew, p
11d0: 4e 65 77 21 3d 26 73 74 61 6e 64 69 6e 29 3b 0a New!=&standin);.
11e0: 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 pNew = 0;.
11f0: 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 }else{. asser
1200: 74 28 20 70 4e 65 77 2d 3e 70 53 72 63 21 3d 30 t( pNew->pSrc!=0
1210: 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 || pParse->nErr
1220: 3e 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 >0 );. }. asse
1230: 72 74 28 20 70 4e 65 77 21 3d 26 73 74 61 6e 64 rt( pNew!=&stand
1240: 69 6e 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 in );. return p
1250: 4e 65 77 3b 0a 7d 0a 0a 23 69 66 20 53 45 4c 45 New;.}..#if SELE
1260: 43 54 54 52 41 43 45 5f 45 4e 41 42 4c 45 44 0a CTTRACE_ENABLED.
1270: 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6e 61 /*.** Set the na
1280: 6d 65 20 6f 66 20 61 20 53 65 6c 65 63 74 20 6f me of a Select o
1290: 62 6a 65 63 74 0a 2a 2f 0a 76 6f 69 64 20 73 71 bject.*/.void sq
12a0: 6c 69 74 65 33 53 65 6c 65 63 74 53 65 74 4e 61 lite3SelectSetNa
12b0: 6d 65 28 53 65 6c 65 63 74 20 2a 70 2c 20 63 6f me(Select *p, co
12c0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 nst char *zName)
12d0: 7b 0a 20 20 69 66 28 20 70 20 26 26 20 7a 4e 61 {. if( p && zNa
12e0: 6d 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 me ){. sqlite
12f0: 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 3_snprintf(sizeo
1300: 66 28 70 2d 3e 7a 53 65 6c 4e 61 6d 65 29 2c 20 f(p->zSelName),
1310: 70 2d 3e 7a 53 65 6c 4e 61 6d 65 2c 20 22 25 73 p->zSelName, "%s
1320: 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 7d ", zName);. }.}
1330: 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
1340: 44 65 6c 65 74 65 20 74 68 65 20 67 69 76 65 6e Delete the given
1350: 20 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72 Select structur
1360: 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 e and all of its
1370: 20 73 75 62 73 74 72 75 63 74 75 72 65 73 2e 0a substructures..
1380: 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 33 53 */.void sqlite3S
1390: 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c 69 electDelete(sqli
13a0: 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 te3 *db, Select
13b0: 2a 70 29 7b 0a 20 20 63 6c 65 61 72 53 65 6c 65 *p){. clearSele
13c0: 63 74 28 64 62 2c 20 70 2c 20 31 29 3b 0a 7d 0a ct(db, p, 1);.}.
13d0: 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
13e0: 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 pointer to the r
13f0: 69 67 68 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 ight-most SELECT
1400: 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 61 20 statement in a
1410: 63 6f 6d 70 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 compound..*/.sta
1420: 74 69 63 20 53 65 6c 65 63 74 20 2a 66 69 6e 64 tic Select *find
1430: 52 69 67 68 74 6d 6f 73 74 28 53 65 6c 65 63 74 Rightmost(Select
1440: 20 2a 70 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 *p){. while( p
1450: 2d 3e 70 4e 65 78 74 20 29 20 70 20 3d 20 70 2d ->pNext ) p = p-
1460: 3e 70 4e 65 78 74 3b 0a 20 20 72 65 74 75 72 6e >pNext;. return
1470: 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 p;.}../*.** Giv
1480: 65 6e 20 31 20 74 6f 20 33 20 69 64 65 6e 74 69 en 1 to 3 identi
1490: 66 69 65 72 73 20 70 72 65 63 65 64 69 6e 67 20 fiers preceding
14a0: 74 68 65 20 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 the JOIN keyword
14b0: 2c 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 0a , determine the.
14c0: 2a 2a 20 74 79 70 65 20 6f 66 20 6a 6f 69 6e 2e ** type of join.
14d0: 20 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 Return an inte
14e0: 67 65 72 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 ger constant tha
14f0: 74 20 65 78 70 72 65 73 73 65 73 20 74 68 61 74 t expresses that
1500: 20 74 79 70 65 0a 2a 2a 20 69 6e 20 74 65 72 6d type.** in term
1510: 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 s of the followi
1520: 6e 67 20 62 69 74 20 76 61 6c 75 65 73 3a 0a 2a ng bit values:.*
1530: 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f 49 4e 4e 45 *.** JT_INNE
1540: 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 43 52 4f 53 R.** JT_CROS
1550: 53 0a 2a 2a 20 20 20 20 20 4a 54 5f 4f 55 54 45 S.** JT_OUTE
1560: 52 0a 2a 2a 20 20 20 20 20 4a 54 5f 4e 41 54 55 R.** JT_NATU
1570: 52 41 4c 0a 2a 2a 20 20 20 20 20 4a 54 5f 4c 45 RAL.** JT_LE
1580: 46 54 0a 2a 2a 20 20 20 20 20 4a 54 5f 52 49 47 FT.** JT_RIG
1590: 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20 HT.**.** A full
15a0: 6f 75 74 65 72 20 6a 6f 69 6e 20 69 73 20 74 68 outer join is th
15b0: 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 e combination of
15c0: 20 4a 54 5f 4c 45 46 54 20 61 6e 64 20 4a 54 5f JT_LEFT and JT_
15d0: 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 RIGHT..**.** If
15e0: 61 6e 20 69 6c 6c 65 67 61 6c 20 6f 72 20 75 6e an illegal or un
15f0: 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 supported join t
1600: 79 70 65 20 69 73 20 73 65 65 6e 2c 20 74 68 65 ype is seen, the
1610: 6e 20 73 74 69 6c 6c 20 72 65 74 75 72 6e 0a 2a n still return.*
1620: 2a 20 61 20 6a 6f 69 6e 20 74 79 70 65 2c 20 62 * a join type, b
1630: 75 74 20 70 75 74 20 61 6e 20 65 72 72 6f 72 20 ut put an error
1640: 69 6e 20 74 68 65 20 70 50 61 72 73 65 20 73 74 in the pParse st
1650: 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 69 6e 74 20 ructure..*/.int
1660: 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 sqlite3JoinType(
1670: 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 Parse *pParse, T
1680: 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b 65 6e 20 oken *pA, Token
1690: 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a 70 43 29 7b *pB, Token *pC){
16a0: 0a 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70 65 20 . int jointype
16b0: 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e 20 2a 61 70 = 0;. Token *ap
16c0: 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b 65 6e 20 All[3];. Token
16d0: 2a 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 *p;.
16e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
16f0: 20 2f 2a 20 20 20 30 31 32 33 34 35 36 37 38 39 /* 0123456789
1700: 20 31 32 33 34 35 36 37 38 39 20 31 32 33 34 35 123456789 12345
1710: 36 37 38 39 20 31 32 33 20 2a 2f 0a 20 20 73 74 6789 123 */. st
1720: 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 atic const char
1730: 7a 4b 65 79 54 65 78 74 5b 5d 20 3d 20 22 6e 61 zKeyText[] = "na
1740: 74 75 72 61 6c 65 66 74 6f 75 74 65 72 69 67 68 turaleftouterigh
1750: 74 66 75 6c 6c 69 6e 6e 65 72 63 72 6f 73 73 22 tfullinnercross"
1760: 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 ;. static const
1770: 20 73 74 72 75 63 74 20 7b 0a 20 20 20 20 75 38 struct {. u8
1780: 20 69 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 65 i; /* Be
1790: 67 69 6e 6e 69 6e 67 20 6f 66 20 6b 65 79 77 6f ginning of keywo
17a0: 72 64 20 74 65 78 74 20 69 6e 20 7a 4b 65 79 54 rd text in zKeyT
17b0: 65 78 74 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 ext[] */. u8
17c0: 6e 43 68 61 72 3b 20 20 20 20 2f 2a 20 4c 65 6e nChar; /* Len
17d0: 67 74 68 20 6f 66 20 74 68 65 20 6b 65 79 77 6f gth of the keywo
17e0: 72 64 20 69 6e 20 63 68 61 72 61 63 74 65 72 73 rd in characters
17f0: 20 2a 2f 0a 20 20 20 20 75 38 20 63 6f 64 65 3b */. u8 code;
1800: 20 20 20 20 20 2f 2a 20 4a 6f 69 6e 20 74 79 70 /* Join typ
1810: 65 20 6d 61 73 6b 20 2a 2f 0a 20 20 7d 20 61 4b e mask */. } aK
1820: 65 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a 20 20 20 eyword[] = {.
1830: 20 2f 2a 20 6e 61 74 75 72 61 6c 20 2a 2f 20 7b /* natural */ {
1840: 20 30 2c 20 20 37 2c 20 4a 54 5f 4e 41 54 55 52 0, 7, JT_NATUR
1850: 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 AL
1860: 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 6c 65 66 74 },. /* left
1870: 20 20 20 20 2a 2f 20 7b 20 36 2c 20 20 34 2c 20 */ { 6, 4,
1880: 4a 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45 52 JT_LEFT|JT_OUTER
1890: 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 },.
18a0: 20 2f 2a 20 6f 75 74 65 72 20 20 20 2a 2f 20 7b /* outer */ {
18b0: 20 31 30 2c 20 35 2c 20 4a 54 5f 4f 55 54 45 52 10, 5, JT_OUTER
18c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18d0: 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 72 69 67 68 },. /* righ
18e0: 74 20 20 20 2a 2f 20 7b 20 31 34 2c 20 35 2c 20 t */ { 14, 5,
18f0: 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 JT_RIGHT|JT_OUTE
1900: 52 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 R },.
1910: 20 2f 2a 20 66 75 6c 6c 20 20 20 20 2a 2f 20 7b /* full */ {
1920: 20 31 39 2c 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 19, 4, JT_LEFT|
1930: 4a 54 5f 52 49 47 48 54 7c 4a 54 5f 4f 55 54 45 JT_RIGHT|JT_OUTE
1940: 52 20 7d 2c 0a 20 20 20 20 2f 2a 20 69 6e 6e 65 R },. /* inne
1950: 72 20 20 20 2a 2f 20 7b 20 32 33 2c 20 35 2c 20 r */ { 23, 5,
1960: 4a 54 5f 49 4e 4e 45 52 20 20 20 20 20 20 20 20 JT_INNER
1970: 20 20 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 },.
1980: 20 2f 2a 20 63 72 6f 73 73 20 20 20 2a 2f 20 7b /* cross */ {
1990: 20 32 38 2c 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 28, 5, JT_INNER
19a0: 7c 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 20 20 |JT_CROSS
19b0: 20 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 },. };. int
19c0: 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30 5d i, j;. apAll[0]
19d0: 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c 6c 5b 31 = pA;. apAll[1
19e0: 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41 6c 6c 5b ] = pB;. apAll[
19f0: 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f 72 28 69 2] = pC;. for(i
1a00: 3d 30 3b 20 69 3c 33 20 26 26 20 61 70 41 6c 6c =0; i<3 && apAll
1a10: 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 [i]; i++){. p
1a20: 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20 20 = apAll[i];.
1a30: 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72 61 for(j=0; j<Arra
1a40: 79 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 29 3b ySize(aKeyword);
1a50: 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 j++){. if(
1a60: 20 70 2d 3e 6e 3d 3d 61 4b 65 79 77 6f 72 64 5b p->n==aKeyword[
1a70: 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20 20 20 20 j].nChar .
1a80: 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 53 74 && sqlite3St
1a90: 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70 2d rNICmp((char*)p-
1aa0: 3e 7a 2c 20 26 7a 4b 65 79 54 65 78 74 5b 61 4b >z, &zKeyText[aK
1ab0: 65 79 77 6f 72 64 5b 6a 5d 2e 69 5d 2c 20 70 2d eyword[j].i], p-
1ac0: 3e 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 >n)==0 ){.
1ad0: 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 61 4b jointype |= aK
1ae0: 65 79 77 6f 72 64 5b 6a 5d 2e 63 6f 64 65 3b 0a eyword[j].code;.
1af0: 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1b00: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1b10: 20 74 65 73 74 63 61 73 65 28 20 6a 3d 3d 30 20 testcase( j==0
1b20: 7c 7c 20 6a 3d 3d 31 20 7c 7c 20 6a 3d 3d 32 20 || j==1 || j==2
1b30: 7c 7c 20 6a 3d 3d 33 20 7c 7c 20 6a 3d 3d 34 20 || j==3 || j==4
1b40: 7c 7c 20 6a 3d 3d 35 20 7c 7c 20 6a 3d 3d 36 20 || j==5 || j==6
1b50: 29 3b 0a 20 20 20 20 69 66 28 20 6a 3e 3d 41 72 );. if( j>=Ar
1b60: 72 61 79 53 69 7a 65 28 61 4b 65 79 77 6f 72 64 raySize(aKeyword
1b70: 29 20 29 7b 0a 20 20 20 20 20 20 6a 6f 69 6e 74 ) ){. joint
1b80: 79 70 65 20 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b ype |= JT_ERROR;
1b90: 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
1ba0: 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 0a 20 20 }. }. if(.
1bb0: 20 20 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 28 (jointype & (
1bc0: 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 JT_INNER|JT_OUTE
1bd0: 52 29 29 3d 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a R))==(JT_INNER|J
1be0: 54 5f 4f 55 54 45 52 29 20 7c 7c 0a 20 20 20 20 T_OUTER) ||.
1bf0: 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f (jointype & JT_
1c00: 45 52 52 4f 52 29 21 3d 30 0a 20 20 29 7b 0a 20 ERROR)!=0. ){.
1c10: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1c20: 53 70 20 3d 20 22 20 22 3b 0a 20 20 20 20 61 73 Sp = " ";. as
1c30: 73 65 72 74 28 20 70 42 21 3d 30 20 29 3b 0a 20 sert( pB!=0 );.
1c40: 20 20 20 69 66 28 20 70 43 3d 3d 30 20 29 7b 20 if( pC==0 ){
1c50: 7a 53 70 2b 2b 3b 20 7d 0a 20 20 20 20 73 71 6c zSp++; }. sql
1c60: 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
1c70: 72 73 65 2c 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 rse, "unknown or
1c80: 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f 69 unsupported joi
1c90: 6e 20 74 79 70 65 3a 20 22 0a 20 20 20 20 20 20 n type: ".
1ca0: 20 22 25 54 20 25 54 25 73 25 54 22 2c 20 70 41 "%T %T%s%T", pA
1cb0: 2c 20 70 42 2c 20 7a 53 70 2c 20 70 43 29 3b 0a , pB, zSp, pC);.
1cc0: 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a jointype = J
1cd0: 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 T_INNER;. }else
1ce0: 20 69 66 28 20 28 6a 6f 69 6e 74 79 70 65 20 26 if( (jointype &
1cf0: 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 20 0a 20 JT_OUTER)!=0 .
1d00: 20 20 20 20 20 20 20 20 26 26 20 28 6a 6f 69 6e && (join
1d10: 74 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c type & (JT_LEFT|
1d20: 4a 54 5f 52 49 47 48 54 29 29 21 3d 4a 54 5f 4c JT_RIGHT))!=JT_L
1d30: 45 46 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 EFT ){. sqlit
1d40: 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 e3ErrorMsg(pPars
1d50: 65 2c 20 0a 20 20 20 20 20 20 22 52 49 47 48 54 e, . "RIGHT
1d60: 20 61 6e 64 20 46 55 4c 4c 20 4f 55 54 45 52 20 and FULL OUTER
1d70: 4a 4f 49 4e 73 20 61 72 65 20 6e 6f 74 20 63 75 JOINs are not cu
1d80: 72 72 65 6e 74 6c 79 20 73 75 70 70 6f 72 74 65 rrently supporte
1d90: 64 22 29 3b 0a 20 20 20 20 6a 6f 69 6e 74 79 70 d");. jointyp
1da0: 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 e = JT_INNER;.
1db0: 7d 0a 20 20 72 65 74 75 72 6e 20 6a 6f 69 6e 74 }. return joint
1dc0: 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 ype;.}../*.** Re
1dd0: 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f turn the index o
1de0: 66 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 f a column in a
1df0: 74 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 2d table. Return -
1e00: 31 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 1 if the column.
1e10: 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 ** is not contai
1e20: 6e 65 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 ned in the table
1e30: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
1e40: 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 54 61 62 6c columnIndex(Tabl
1e50: 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 e *pTab, const c
1e60: 68 61 72 20 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e har *zCol){. in
1e70: 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 t i;. for(i=0;
1e80: 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b i<pTab->nCol; i+
1e90: 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 +){. if( sqli
1ea0: 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62 2d te3StrICmp(pTab-
1eb0: 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 >aCol[i].zName,
1ec0: 7a 43 6f 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 zCol)==0 ) retur
1ed0: 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 n i;. }. retur
1ee0: 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 n -1;.}../*.** S
1ef0: 65 61 72 63 68 20 74 68 65 20 66 69 72 73 74 20 earch the first
1f00: 4e 20 74 61 62 6c 65 73 20 69 6e 20 70 53 72 63 N tables in pSrc
1f10: 2c 20 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 , from left to r
1f20: 69 67 68 74 2c 20 6c 6f 6f 6b 69 6e 67 20 66 6f ight, looking fo
1f30: 72 20 61 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61 r a.** table tha
1f40: 74 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e 20 6e t has a column n
1f50: 61 6d 65 64 20 7a 43 6f 6c 2e 20 20 0a 2a 2a 0a amed zCol. .**.
1f60: 2a 2a 20 57 68 65 6e 20 66 6f 75 6e 64 2c 20 73 ** When found, s
1f70: 65 74 20 2a 70 69 54 61 62 20 61 6e 64 20 2a 70 et *piTab and *p
1f80: 69 43 6f 6c 20 74 6f 20 74 68 65 20 74 61 62 6c iCol to the tabl
1f90: 65 20 69 6e 64 65 78 20 61 6e 64 20 63 6f 6c 75 e index and colu
1fa0: 6d 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f 66 20 74 mn index.** of t
1fb0: 68 65 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 he matching colu
1fc0: 6d 6e 20 61 6e 64 20 72 65 74 75 72 6e 20 54 52 mn and return TR
1fd0: 55 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 74 UE..**.** If not
1fe0: 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 46 found, return F
1ff0: 41 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 ALSE..*/.static
2000: 69 6e 74 20 74 61 62 6c 65 41 6e 64 43 6f 6c 75 int tableAndColu
2010: 6d 6e 49 6e 64 65 78 28 0a 20 20 53 72 63 4c 69 mnIndex(. SrcLi
2020: 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 st *pSrc,
2030: 2f 2a 20 41 72 72 61 79 20 6f 66 20 74 61 62 6c /* Array of tabl
2040: 65 73 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a es to search */.
2050: 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 int N,
2060: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
2070: 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 70 53 of tables in pS
2080: 72 63 2d 3e 61 5b 5d 20 74 6f 20 73 65 61 72 63 rc->a[] to searc
2090: 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 h */. const cha
20a0: 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 2f 2a 20 4e r *zCol, /* N
20b0: 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d ame of the colum
20c0: 6e 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 n we are looking
20d0: 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 for */. int *p
20e0: 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f iTab, /
20f0: 2a 20 57 72 69 74 65 20 69 6e 64 65 78 20 6f 66 * Write index of
2100: 20 70 53 72 63 2d 3e 61 5b 5d 20 68 65 72 65 20 pSrc->a[] here
2110: 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 43 6f 6c 20 */. int *piCol
2120: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 /* Wri
2130: 74 65 20 69 6e 64 65 78 20 6f 66 20 70 53 72 63 te index of pSrc
2140: 2d 3e 61 5b 2a 70 69 54 61 62 5d 2e 70 54 61 62 ->a[*piTab].pTab
2150: 2d 3e 61 43 6f 6c 5b 5d 20 68 65 72 65 20 2a 2f ->aCol[] here */
2160: 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 .){. int i;
2170: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f /* Fo
2180: 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 74 r looping over t
2190: 61 62 6c 65 73 20 69 6e 20 70 53 72 63 20 2a 2f ables in pSrc */
21a0: 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 . int iCol;
21b0: 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 /* Index
21c0: 20 6f 66 20 63 6f 6c 75 6d 6e 20 6d 61 74 63 68 of column match
21d0: 69 6e 67 20 7a 43 6f 6c 20 2a 2f 0a 0a 20 20 61 ing zCol */.. a
21e0: 73 73 65 72 74 28 20 28 70 69 54 61 62 3d 3d 30 ssert( (piTab==0
21f0: 29 3d 3d 28 70 69 43 6f 6c 3d 3d 30 29 20 29 3b )==(piCol==0) );
2200: 20 20 2f 2a 20 42 6f 74 68 20 6f 72 20 6e 65 69 /* Both or nei
2210: 74 68 65 72 20 61 72 65 20 4e 55 4c 4c 20 2a 2f ther are NULL */
2220: 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 3b . for(i=0; i<N;
2230: 20 69 2b 2b 29 7b 0a 20 20 20 20 69 43 6f 6c 20 i++){. iCol
2240: 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 53 = columnIndex(pS
2250: 72 63 2d 3e 61 5b 69 5d 2e 70 54 61 62 2c 20 7a rc->a[i].pTab, z
2260: 43 6f 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43 Col);. if( iC
2270: 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 ol>=0 ){. i
2280: 66 28 20 70 69 54 61 62 20 29 7b 0a 20 20 20 20 f( piTab ){.
2290: 20 20 20 20 2a 70 69 54 61 62 20 3d 20 69 3b 0a *piTab = i;.
22a0: 20 20 20 20 20 20 20 20 2a 70 69 43 6f 6c 20 3d *piCol =
22b0: 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 iCol;. }.
22c0: 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 return 1;.
22d0: 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 }. }. retur
22e0: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 n 0;.}../*.** Th
22f0: 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 is function is u
2300: 73 65 64 20 74 6f 20 61 64 64 20 74 65 72 6d 73 sed to add terms
2310: 20 69 6d 70 6c 69 65 64 20 62 79 20 4a 4f 49 4e implied by JOIN
2320: 20 73 79 6e 74 61 78 20 74 6f 20 74 68 65 0a 2a syntax to the.*
2330: 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 * WHERE clause e
2340: 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 61 20 53 xpression of a S
2350: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
2360: 20 54 68 65 20 6e 65 77 20 74 65 72 6d 2c 20 77 The new term, w
2370: 68 69 63 68 0a 2a 2a 20 69 73 20 41 4e 44 65 64 hich.** is ANDed
2380: 20 77 69 74 68 20 74 68 65 20 65 78 69 73 74 69 with the existi
2390: 6e 67 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c ng WHERE clause,
23a0: 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a is of the form:
23b0: 0a 2a 2a 0a 2a 2a 20 20 20 20 28 74 61 62 31 2e .**.** (tab1.
23c0: 63 6f 6c 31 20 3d 20 74 61 62 32 2e 63 6f 6c 32 col1 = tab2.col2
23d0: 29 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 74 61 ).**.** where ta
23e0: 62 31 20 69 73 20 74 68 65 20 69 53 72 63 27 74 b1 is the iSrc't
23f0: 68 20 74 61 62 6c 65 20 69 6e 20 53 72 63 4c 69 h table in SrcLi
2400: 73 74 20 70 53 72 63 20 61 6e 64 20 74 61 62 32 st pSrc and tab2
2410: 20 69 73 20 74 68 65 20 0a 2a 2a 20 28 69 53 72 is the .** (iSr
2420: 63 2b 31 29 27 74 68 2e 20 43 6f 6c 75 6d 6e 20 c+1)'th. Column
2430: 63 6f 6c 31 20 69 73 20 63 6f 6c 75 6d 6e 20 69 col1 is column i
2440: 43 6f 6c 4c 65 66 74 20 6f 66 20 74 61 62 31 2c ColLeft of tab1,
2450: 20 61 6e 64 20 63 6f 6c 32 20 69 73 0a 2a 2a 20 and col2 is.**
2460: 63 6f 6c 75 6d 6e 20 69 43 6f 6c 52 69 67 68 74 column iColRight
2470: 20 6f 66 20 74 61 62 32 2e 0a 2a 2f 0a 73 74 61 of tab2..*/.sta
2480: 74 69 63 20 76 6f 69 64 20 61 64 64 57 68 65 72 tic void addWher
2490: 65 54 65 72 6d 28 0a 20 20 50 61 72 73 65 20 2a eTerm(. Parse *
24a0: 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 pParse,
24b0: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 /* Pars
24c0: 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ing context */.
24d0: 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 SrcList *pSrc,
24e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
24f0: 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c /* List of tabl
2500: 65 73 20 69 6e 20 46 52 4f 4d 20 63 6c 61 75 73 es in FROM claus
2510: 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 66 74 e */. int iLeft
2520: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
2530: 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 /* Index
2540: 6f 66 20 66 69 72 73 74 20 74 61 62 6c 65 20 74 of first table t
2550: 6f 20 6a 6f 69 6e 20 69 6e 20 70 53 72 63 20 2a o join in pSrc *
2560: 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 4c 65 66 74 /. int iColLeft
2570: 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
2580: 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 /* Index of
2590: 63 6f 6c 75 6d 6e 20 69 6e 20 66 69 72 73 74 20 column in first
25a0: 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 table */. int i
25b0: 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 Right,
25c0: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e /* In
25d0: 64 65 78 20 6f 66 20 73 65 63 6f 6e 64 20 74 61 dex of second ta
25e0: 62 6c 65 20 69 6e 20 70 53 72 63 20 2a 2f 0a 20 ble in pSrc */.
25f0: 20 69 6e 74 20 69 43 6f 6c 52 69 67 68 74 2c 20 int iColRight,
2600: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2610: 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c /* Index of col
2620: 75 6d 6e 20 69 6e 20 73 65 63 6f 6e 64 20 74 61 umn in second ta
2630: 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f ble */. int isO
2640: 75 74 65 72 4a 6f 69 6e 2c 20 20 20 20 20 20 20 uterJoin,
2650: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 /* True
2660: 20 69 66 20 74 68 69 73 20 69 73 20 61 6e 20 4f if this is an O
2670: 55 54 45 52 20 6a 6f 69 6e 20 2a 2f 0a 20 20 45 UTER join */. E
2680: 78 70 72 20 2a 2a 70 70 57 68 65 72 65 20 20 20 xpr **ppWhere
2690: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
26a0: 2a 20 49 4e 2f 4f 55 54 3a 20 54 68 65 20 57 48 * IN/OUT: The WH
26b0: 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 61 64 ERE clause to ad
26c0: 64 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c d to */.){. sql
26d0: 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
26e0: 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 20 2a 70 e->db;. Expr *p
26f0: 45 31 3b 0a 20 20 45 78 70 72 20 2a 70 45 32 3b E1;. Expr *pE2;
2700: 0a 20 20 45 78 70 72 20 2a 70 45 71 3b 0a 0a 20 . Expr *pEq;..
2710: 20 61 73 73 65 72 74 28 20 69 4c 65 66 74 3c 69 assert( iLeft<i
2720: 52 69 67 68 74 20 29 3b 0a 20 20 61 73 73 65 72 Right );. asser
2730: 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3e 69 52 t( pSrc->nSrc>iR
2740: 69 67 68 74 20 29 3b 0a 20 20 61 73 73 65 72 74 ight );. assert
2750: 28 20 70 53 72 63 2d 3e 61 5b 69 4c 65 66 74 5d ( pSrc->a[iLeft]
2760: 2e 70 54 61 62 20 29 3b 0a 20 20 61 73 73 65 72 .pTab );. asser
2770: 74 28 20 70 53 72 63 2d 3e 61 5b 69 52 69 67 68 t( pSrc->a[iRigh
2780: 74 5d 2e 70 54 61 62 20 29 3b 0a 0a 20 20 70 45 t].pTab );.. pE
2790: 31 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 1 = sqlite3Creat
27a0: 65 43 6f 6c 75 6d 6e 45 78 70 72 28 64 62 2c 20 eColumnExpr(db,
27b0: 70 53 72 63 2c 20 69 4c 65 66 74 2c 20 69 43 6f pSrc, iLeft, iCo
27c0: 6c 4c 65 66 74 29 3b 0a 20 20 70 45 32 20 3d 20 lLeft);. pE2 =
27d0: 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c sqlite3CreateCol
27e0: 75 6d 6e 45 78 70 72 28 64 62 2c 20 70 53 72 63 umnExpr(db, pSrc
27f0: 2c 20 69 52 69 67 68 74 2c 20 69 43 6f 6c 52 69 , iRight, iColRi
2800: 67 68 74 29 3b 0a 0a 20 20 70 45 71 20 3d 20 73 ght);.. pEq = s
2810: 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 qlite3PExpr(pPar
2820: 73 65 2c 20 54 4b 5f 45 51 2c 20 70 45 31 2c 20 se, TK_EQ, pE1,
2830: 70 45 32 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 pE2, 0);. if( p
2840: 45 71 20 26 26 20 69 73 4f 75 74 65 72 4a 6f 69 Eq && isOuterJoi
2850: 6e 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 n ){. ExprSet
2860: 50 72 6f 70 65 72 74 79 28 70 45 71 2c 20 45 50 Property(pEq, EP
2870: 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 _FromJoin);.
2880: 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
2890: 50 72 6f 70 65 72 74 79 28 70 45 71 2c 20 45 50 Property(pEq, EP
28a0: 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 _TokenOnly|EP_Re
28b0: 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 45 78 duced) );. Ex
28c0: 70 72 53 65 74 56 56 41 50 72 6f 70 65 72 74 79 prSetVVAProperty
28d0: 28 70 45 71 2c 20 45 50 5f 4e 6f 52 65 64 75 63 (pEq, EP_NoReduc
28e0: 65 29 3b 0a 20 20 20 20 70 45 71 2d 3e 69 52 69 e);. pEq->iRi
28f0: 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 28 ghtJoinTable = (
2900: 69 31 36 29 70 45 32 2d 3e 69 54 61 62 6c 65 3b i16)pE2->iTable;
2910: 0a 20 20 7d 0a 20 20 2a 70 70 57 68 65 72 65 20 . }. *ppWhere
2920: 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 = sqlite3ExprAnd
2930: 28 64 62 2c 20 2a 70 70 57 68 65 72 65 2c 20 70 (db, *ppWhere, p
2940: 45 71 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 Eq);.}../*.** Se
2950: 74 20 74 68 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 t the EP_FromJoi
2960: 6e 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 61 6c n property on al
2970: 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 67 l terms of the g
2980: 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e iven expression.
2990: 0a 2a 2a 20 41 6e 64 20 73 65 74 20 74 68 65 20 .** And set the
29a0: 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 Expr.iRightJoinT
29b0: 61 62 6c 65 20 74 6f 20 69 54 61 62 6c 65 20 66 able to iTable f
29c0: 6f 72 20 65 76 65 72 79 20 74 65 72 6d 20 69 6e or every term in
29d0: 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 the.** expressi
29e0: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 50 on..**.** The EP
29f0: 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 _FromJoin proper
2a00: 74 79 20 69 73 20 75 73 65 64 20 6f 6e 20 74 65 ty is used on te
2a10: 72 6d 73 20 6f 66 20 61 6e 20 65 78 70 72 65 73 rms of an expres
2a20: 73 69 6f 6e 20 74 6f 20 74 65 6c 6c 0a 2a 2a 20 sion to tell.**
2a30: 74 68 65 20 4c 45 46 54 20 4f 55 54 45 52 20 4a the LEFT OUTER J
2a40: 4f 49 4e 20 70 72 6f 63 65 73 73 69 6e 67 20 6c OIN processing l
2a50: 6f 67 69 63 20 74 68 61 74 20 74 68 69 73 20 74 ogic that this t
2a60: 65 72 6d 20 69 73 20 70 61 72 74 20 6f 66 20 74 erm is part of t
2a70: 68 65 0a 2a 2a 20 6a 6f 69 6e 20 72 65 73 74 72 he.** join restr
2a80: 69 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 iction specified
2a90: 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 in the ON or US
2aa0: 49 4e 47 20 63 6c 61 75 73 65 20 61 6e 64 20 6e ING clause and n
2ab0: 6f 74 20 61 20 70 61 72 74 0a 2a 2a 20 6f 66 20 ot a part.** of
2ac0: 74 68 65 20 6d 6f 72 65 20 67 65 6e 65 72 61 6c the more general
2ad0: 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 WHERE clause.
2ae0: 54 68 65 73 65 20 74 65 72 6d 73 20 61 72 65 20 These terms are
2af0: 6d 6f 76 65 64 20 6f 76 65 72 20 74 6f 20 74 68 moved over to th
2b00: 65 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 e.** WHERE claus
2b10: 65 20 64 75 72 69 6e 67 20 6a 6f 69 6e 20 70 72 e during join pr
2b20: 6f 63 65 73 73 69 6e 67 20 62 75 74 20 77 65 20 ocessing but we
2b30: 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 need to remember
2b40: 20 74 68 61 74 20 74 68 65 79 0a 2a 2a 20 6f 72 that they.** or
2b50: 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68 65 20 iginated in the
2b60: 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 ON or USING clau
2b70: 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 se..**.** The Ex
2b80: 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 pr.iRightJoinTab
2b90: 6c 65 20 74 65 6c 6c 73 20 74 68 65 20 57 48 45 le tells the WHE
2ba0: 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 RE clause proces
2bb0: 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a 2a 2a sing that the.**
2bc0: 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 65 expression depe
2bd0: 6e 64 73 20 6f 6e 20 74 61 62 6c 65 20 69 52 69 nds on table iRi
2be0: 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 65 76 65 ghtJoinTable eve
2bf0: 6e 20 69 66 20 74 68 61 74 20 74 61 62 6c 65 20 n if that table
2c00: 69 73 20 6e 6f 74 0a 2a 2a 20 65 78 70 6c 69 63 is not.** explic
2c10: 69 74 6c 79 20 6d 65 6e 74 69 6f 6e 65 64 20 69 itly mentioned i
2c20: 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e n the expression
2c30: 2e 20 20 54 68 61 74 20 69 6e 66 6f 72 6d 61 74 . That informat
2c40: 69 6f 6e 20 69 73 20 6e 65 65 64 65 64 0a 2a 2a ion is needed.**
2c50: 20 66 6f 72 20 63 61 73 65 73 20 6c 69 6b 65 20 for cases like
2c60: 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 this:.**.** S
2c70: 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 ELECT * FROM t1
2c80: 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 LEFT JOIN t2 ON
2c90: 74 31 2e 61 3d 74 32 2e 62 20 41 4e 44 20 74 31 t1.a=t2.b AND t1
2ca0: 2e 78 3d 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 .x=5.**.** The w
2cb0: 68 65 72 65 20 63 6c 61 75 73 65 20 6e 65 65 64 here clause need
2cc0: 73 20 74 6f 20 64 65 66 65 72 20 74 68 65 20 68 s to defer the h
2cd0: 61 6e 64 6c 69 6e 67 20 6f 66 20 74 68 65 20 74 andling of the t
2ce0: 31 2e 78 3d 35 0a 2a 2a 20 74 65 72 6d 20 75 6e 1.x=5.** term un
2cf0: 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 74 32 til after the t2
2d00: 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 6a 6f 69 loop of the joi
2d10: 6e 2e 20 20 49 6e 20 74 68 61 74 20 77 61 79 2c n. In that way,
2d20: 20 61 0a 2a 2a 20 4e 55 4c 4c 20 74 32 20 72 6f a.** NULL t2 ro
2d30: 77 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 w will be insert
2d40: 65 64 20 77 68 65 6e 65 76 65 72 20 74 31 2e 78 ed whenever t1.x
2d50: 21 3d 35 2e 20 20 49 66 20 77 65 20 64 6f 20 6e !=5. If we do n
2d60: 6f 74 0a 2a 2a 20 64 65 66 65 72 20 74 68 65 20 ot.** defer the
2d70: 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 31 2e 78 handling of t1.x
2d80: 3d 35 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70 =5, it will be p
2d90: 72 6f 63 65 73 73 65 64 20 69 6d 6d 65 64 69 61 rocessed immedia
2da0: 74 65 6c 79 0a 2a 2a 20 61 66 74 65 72 20 74 68 tely.** after th
2db0: 65 20 74 31 20 6c 6f 6f 70 20 61 6e 64 20 72 6f e t1 loop and ro
2dc0: 77 73 20 77 69 74 68 20 74 31 2e 78 21 3d 35 20 ws with t1.x!=5
2dd0: 77 69 6c 6c 20 6e 65 76 65 72 20 61 70 70 65 61 will never appea
2de0: 72 20 69 6e 0a 2a 2a 20 74 68 65 20 6f 75 74 70 r in.** the outp
2df0: 75 74 2c 20 77 68 69 63 68 20 69 73 20 69 6e 63 ut, which is inc
2e00: 6f 72 72 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 orrect..*/.stati
2e10: 63 20 76 6f 69 64 20 73 65 74 4a 6f 69 6e 45 78 c void setJoinEx
2e20: 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 pr(Expr *p, int
2e30: 69 54 61 62 6c 65 29 7b 0a 20 20 77 68 69 6c 65 iTable){. while
2e40: 28 20 70 20 29 7b 0a 20 20 20 20 45 78 70 72 53 ( p ){. ExprS
2e50: 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 etProperty(p, EP
2e60: 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 _FromJoin);.
2e70: 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 assert( !ExprHas
2e80: 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 Property(p, EP_T
2e90: 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 okenOnly|EP_Redu
2ea0: 63 65 64 29 20 29 3b 0a 20 20 20 20 45 78 70 72 ced) );. Expr
2eb0: 53 65 74 56 56 41 50 72 6f 70 65 72 74 79 28 70 SetVVAProperty(p
2ec0: 2c 20 45 50 5f 4e 6f 52 65 64 75 63 65 29 3b 0a , EP_NoReduce);.
2ed0: 20 20 20 20 70 2d 3e 69 52 69 67 68 74 4a 6f 69 p->iRightJoi
2ee0: 6e 54 61 62 6c 65 20 3d 20 28 69 31 36 29 69 54 nTable = (i16)iT
2ef0: 61 62 6c 65 3b 0a 20 20 20 20 73 65 74 4a 6f 69 able;. setJoi
2f00: 6e 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20 nExpr(p->pLeft,
2f10: 69 54 61 62 6c 65 29 3b 0a 20 20 20 20 70 20 3d iTable);. p =
2f20: 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 7d 20 p->pRight;. }
2f30: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 .}../*.** This r
2f40: 6f 75 74 69 6e 65 20 70 72 6f 63 65 73 73 65 73 outine processes
2f50: 20 74 68 65 20 6a 6f 69 6e 20 69 6e 66 6f 72 6d the join inform
2f60: 61 74 69 6f 6e 20 66 6f 72 20 61 20 53 45 4c 45 ation for a SELE
2f70: 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a CT statement..**
2f80: 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c ON and USING cl
2f90: 61 75 73 65 73 20 61 72 65 20 63 6f 6e 76 65 72 auses are conver
2fa0: 74 65 64 20 69 6e 74 6f 20 65 78 74 72 61 20 74 ted into extra t
2fb0: 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 erms of the WHER
2fc0: 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 20 4e 41 54 E clause..** NAT
2fd0: 55 52 41 4c 20 6a 6f 69 6e 73 20 61 6c 73 6f 20 URAL joins also
2fe0: 63 72 65 61 74 65 20 65 78 74 72 61 20 57 48 45 create extra WHE
2ff0: 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 2e RE clause terms.
3000: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 .**.** The terms
3010: 20 6f 66 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 of a FROM claus
3020: 65 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 e are contained
3030: 69 6e 20 74 68 65 20 53 65 6c 65 63 74 2e 70 53 in the Select.pS
3040: 72 63 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a rc structure..**
3050: 20 54 68 65 20 6c 65 66 74 20 6d 6f 73 74 20 74 The left most t
3060: 61 62 6c 65 20 69 73 20 74 68 65 20 66 69 72 73 able is the firs
3070: 74 20 65 6e 74 72 79 20 69 6e 20 53 65 6c 65 63 t entry in Selec
3080: 74 2e 70 53 72 63 2e 20 20 54 68 65 20 72 69 67 t.pSrc. The rig
3090: 68 74 2d 6d 6f 73 74 0a 2a 2a 20 74 61 62 6c 65 ht-most.** table
30a0: 20 69 73 20 74 68 65 20 6c 61 73 74 20 65 6e 74 is the last ent
30b0: 72 79 2e 20 20 54 68 65 20 6a 6f 69 6e 20 6f 70 ry. The join op
30c0: 65 72 61 74 6f 72 20 69 73 20 68 65 6c 64 20 69 erator is held i
30d0: 6e 20 74 68 65 20 65 6e 74 72 79 20 74 6f 0a 2a n the entry to.*
30e0: 2a 20 74 68 65 20 6c 65 66 74 2e 20 20 54 68 75 * the left. Thu
30f0: 73 20 65 6e 74 72 79 20 30 20 63 6f 6e 74 61 69 s entry 0 contai
3100: 6e 73 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 ns the join oper
3110: 61 74 6f 72 20 66 6f 72 20 74 68 65 20 6a 6f 69 ator for the joi
3120: 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 65 6e 74 n between.** ent
3130: 72 69 65 73 20 30 20 61 6e 64 20 31 2e 20 20 41 ries 0 and 1. A
3140: 6e 79 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 ny ON or USING c
3150: 6c 61 75 73 65 73 20 61 73 73 6f 63 69 61 74 65 lauses associate
3160: 64 20 77 69 74 68 20 74 68 65 20 6a 6f 69 6e 20 d with the join
3170: 61 72 65 0a 2a 2a 20 61 6c 73 6f 20 61 74 74 61 are.** also atta
3180: 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 66 74 ched to the left
3190: 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 entry..**.** Th
31a0: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
31b0: 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 ns the number of
31c0: 20 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 errors encounte
31d0: 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 red..*/.static i
31e0: 6e 74 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 nt sqliteProcess
31f0: 4a 6f 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 Join(Parse *pPar
3200: 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a se, Select *p){.
3210: 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b SrcList *pSrc;
3220: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3230: 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 73 20 /* All tables
3240: 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 in the FROM clau
3250: 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a se */. int i, j
3260: 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
3270: 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
3280: 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 73 74 counters */. st
3290: 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
32a0: 6d 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 2f 2a m *pLeft; /*
32b0: 20 4c 65 66 74 20 74 61 62 6c 65 20 62 65 69 6e Left table bein
32c0: 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 20 20 73 74 g joined */. st
32d0: 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
32e0: 6d 20 2a 70 52 69 67 68 74 3b 20 20 20 20 2f 2a m *pRight; /*
32f0: 20 52 69 67 68 74 20 74 61 62 6c 65 20 62 65 69 Right table bei
3300: 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 0a 20 20 ng joined */..
3310: 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a pSrc = p->pSrc;.
3320: 20 20 70 4c 65 66 74 20 3d 20 26 70 53 72 63 2d pLeft = &pSrc-
3330: 3e 61 5b 30 5d 3b 0a 20 20 70 52 69 67 68 74 20 >a[0];. pRight
3340: 3d 20 26 70 4c 65 66 74 5b 31 5d 3b 0a 20 20 66 = &pLeft[1];. f
3350: 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e or(i=0; i<pSrc->
3360: 6e 53 72 63 2d 31 3b 20 69 2b 2b 2c 20 70 52 69 nSrc-1; i++, pRi
3370: 67 68 74 2b 2b 2c 20 70 4c 65 66 74 2b 2b 29 7b ght++, pLeft++){
3380: 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4c 65 66 . Table *pLef
3390: 74 54 61 62 20 3d 20 70 4c 65 66 74 2d 3e 70 54 tTab = pLeft->pT
33a0: 61 62 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 ab;. Table *p
33b0: 52 69 67 68 74 54 61 62 20 3d 20 70 52 69 67 68 RightTab = pRigh
33c0: 74 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 6e 74 t->pTab;. int
33d0: 20 69 73 4f 75 74 65 72 3b 0a 0a 20 20 20 20 69 isOuter;.. i
33e0: 66 28 20 4e 45 56 45 52 28 70 4c 65 66 74 54 61 f( NEVER(pLeftTa
33f0: 62 3d 3d 30 20 7c 7c 20 70 52 69 67 68 74 54 61 b==0 || pRightTa
3400: 62 3d 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65 b==0) ) continue
3410: 3b 0a 20 20 20 20 69 73 4f 75 74 65 72 20 3d 20 ;. isOuter =
3420: 28 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 (pRight->jointyp
3430: 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 e & JT_OUTER)!=0
3440: 3b 0a 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 ;.. /* When t
3450: 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f he NATURAL keywo
3460: 72 64 20 69 73 20 70 72 65 73 65 6e 74 2c 20 61 rd is present, a
3470: 64 64 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 dd WHERE clause
3480: 74 65 72 6d 73 20 66 6f 72 0a 20 20 20 20 2a 2a terms for. **
3490: 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 74 68 every column th
34a0: 61 74 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 at the two table
34b0: 73 20 68 61 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e s have in common
34c0: 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 .. */. if(
34d0: 20 70 52 69 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 pRight->jointyp
34e0: 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 20 29 e & JT_NATURAL )
34f0: 7b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67 {. if( pRig
3500: 68 74 2d 3e 70 4f 6e 20 7c 7c 20 70 52 69 67 68 ht->pOn || pRigh
3510: 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 t->pUsing ){.
3520: 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
3530: 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 20 rMsg(pParse, "a
3540: 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 NATURAL join may
3550: 20 6e 6f 74 20 68 61 76 65 20 22 0a 20 20 20 20 not have ".
3560: 20 20 20 20 20 20 20 22 61 6e 20 4f 4e 20 6f 72 "an ON or
3570: 20 55 53 49 4e 47 20 63 6c 61 75 73 65 22 2c 20 USING clause",
3580: 30 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 0);. retu
3590: 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 rn 1;. }.
35a0: 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 for(j=0; j<p
35b0: 52 69 67 68 74 54 61 62 2d 3e 6e 43 6f 6c 3b 20 RightTab->nCol;
35c0: 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 j++){. ch
35d0: 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 ar *zName; /*
35e0: 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 Name of column i
35f0: 6e 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c n the right tabl
3600: 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 e */. int
3610: 20 69 4c 65 66 74 3b 20 20 20 20 20 2f 2a 20 4d iLeft; /* M
3620: 61 74 63 68 69 6e 67 20 6c 65 66 74 20 74 61 62 atching left tab
3630: 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e le */. in
3640: 74 20 69 4c 65 66 74 43 6f 6c 3b 20 20 2f 2a 20 t iLeftCol; /*
3650: 4d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 Matching column
3660: 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c in the left tabl
3670: 65 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 7a 4e e */.. zN
3680: 61 6d 65 20 3d 20 70 52 69 67 68 74 54 61 62 2d ame = pRightTab-
3690: 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a >aCol[j].zName;.
36a0: 20 20 20 20 20 20 20 20 69 66 28 20 74 61 62 6c if( tabl
36b0: 65 41 6e 64 43 6f 6c 75 6d 6e 49 6e 64 65 78 28 eAndColumnIndex(
36c0: 70 53 72 63 2c 20 69 2b 31 2c 20 7a 4e 61 6d 65 pSrc, i+1, zName
36d0: 2c 20 26 69 4c 65 66 74 2c 20 26 69 4c 65 66 74 , &iLeft, &iLeft
36e0: 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 Col) ){.
36f0: 20 20 61 64 64 57 68 65 72 65 54 65 72 6d 28 70 addWhereTerm(p
3700: 50 61 72 73 65 2c 20 70 53 72 63 2c 20 69 4c 65 Parse, pSrc, iLe
3710: 66 74 2c 20 69 4c 65 66 74 43 6f 6c 2c 20 69 2b ft, iLeftCol, i+
3720: 31 2c 20 6a 2c 0a 20 20 20 20 20 20 20 20 20 20 1, j,.
3730: 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 4f isO
3740: 75 74 65 72 2c 20 26 70 2d 3e 70 57 68 65 72 65 uter, &p->pWhere
3750: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
3760: 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 }. }..
3770: 2f 2a 20 44 69 73 61 6c 6c 6f 77 20 62 6f 74 68 /* Disallow both
3780: 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c ON and USING cl
3790: 61 75 73 65 73 20 69 6e 20 74 68 65 20 73 61 6d auses in the sam
37a0: 65 20 6a 6f 69 6e 0a 20 20 20 20 2a 2f 0a 20 20 e join. */.
37b0: 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f if( pRight->pO
37c0: 6e 20 26 26 20 70 52 69 67 68 74 2d 3e 70 55 73 n && pRight->pUs
37d0: 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c ing ){. sql
37e0: 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 ite3ErrorMsg(pPa
37f0: 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 68 61 76 rse, "cannot hav
3800: 65 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55 53 e both ON and US
3810: 49 4e 47 20 22 0a 20 20 20 20 20 20 20 20 22 63 ING ". "c
3820: 6c 61 75 73 65 73 20 69 6e 20 74 68 65 20 73 61 lauses in the sa
3830: 6d 65 20 6a 6f 69 6e 22 29 3b 0a 20 20 20 20 20 me join");.
3840: 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d return 1;. }
3850: 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 .. /* Add the
3860: 20 4f 4e 20 63 6c 61 75 73 65 20 74 6f 20 74 68 ON clause to th
3870: 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 e end of the WHE
3880: 52 45 20 63 6c 61 75 73 65 2c 20 63 6f 6e 6e 65 RE clause, conne
3890: 63 74 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 61 cted by. ** a
38a0: 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a n AND operator..
38b0: 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 */. if( p
38c0: 52 69 67 68 74 2d 3e 70 4f 6e 20 29 7b 0a 20 20 Right->pOn ){.
38d0: 20 20 20 20 69 66 28 20 69 73 4f 75 74 65 72 20 if( isOuter
38e0: 29 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 52 ) setJoinExpr(pR
38f0: 69 67 68 74 2d 3e 70 4f 6e 2c 20 70 52 69 67 68 ight->pOn, pRigh
3900: 74 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 t->iCursor);.
3910: 20 20 20 70 2d 3e 70 57 68 65 72 65 20 3d 20 73 p->pWhere = s
3920: 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 70 50 qlite3ExprAnd(pP
3930: 61 72 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 57 68 arse->db, p->pWh
3940: 65 72 65 2c 20 70 52 69 67 68 74 2d 3e 70 4f 6e ere, pRight->pOn
3950: 29 3b 0a 20 20 20 20 20 20 70 52 69 67 68 74 2d );. pRight-
3960: 3e 70 4f 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a >pOn = 0;. }.
3970: 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 65 . /* Create e
3980: 78 74 72 61 20 74 65 72 6d 73 20 6f 6e 20 74 68 xtra terms on th
3990: 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 66 e WHERE clause f
39a0: 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6e or each column n
39b0: 61 6d 65 64 0a 20 20 20 20 2a 2a 20 69 6e 20 74 amed. ** in t
39c0: 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e he USING clause.
39d0: 20 20 45 78 61 6d 70 6c 65 3a 20 49 66 20 74 68 Example: If th
39e0: 65 20 74 77 6f 20 74 61 62 6c 65 73 20 74 6f 20 e two tables to
39f0: 62 65 20 6a 6f 69 6e 65 64 20 61 72 65 20 0a 20 be joined are .
3a00: 20 20 20 2a 2a 20 41 20 61 6e 64 20 42 20 61 6e ** A and B an
3a10: 64 20 74 68 65 20 55 53 49 4e 47 20 63 6c 61 75 d the USING clau
3a20: 73 65 20 6e 61 6d 65 73 20 58 2c 20 59 2c 20 61 se names X, Y, a
3a30: 6e 64 20 5a 2c 20 74 68 65 6e 20 61 64 64 20 74 nd Z, then add t
3a40: 68 69 73 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 his. ** to th
3a50: 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 3a 20 e WHERE clause:
3a60: 20 20 20 41 2e 58 3d 42 2e 58 20 41 4e 44 20 41 A.X=B.X AND A
3a70: 2e 59 3d 42 2e 59 20 41 4e 44 20 41 2e 5a 3d 42 .Y=B.Y AND A.Z=B
3a80: 2e 5a 0a 20 20 20 20 2a 2a 20 52 65 70 6f 72 74 .Z. ** Report
3a90: 20 61 6e 20 65 72 72 6f 72 20 69 66 20 61 6e 79 an error if any
3aa0: 20 63 6f 6c 75 6d 6e 20 6d 65 6e 74 69 6f 6e 65 column mentione
3ab0: 64 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 d in the USING c
3ac0: 6c 61 75 73 65 20 69 73 0a 20 20 20 20 2a 2a 20 lause is. **
3ad0: 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e not contained in
3ae0: 20 62 6f 74 68 20 74 61 62 6c 65 73 20 74 6f 20 both tables to
3af0: 62 65 20 6a 6f 69 6e 65 64 2e 0a 20 20 20 20 2a be joined.. *
3b00: 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 /. if( pRight
3b10: 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 ->pUsing ){.
3b20: 20 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 20 IdList *pList
3b30: 3d 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 = pRight->pUsing
3b40: 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b ;. for(j=0;
3b50: 20 6a 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 6a j<pList->nId; j
3b60: 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 ++){. cha
3b70: 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 2f 2a r *zName; /*
3b80: 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 65 72 Name of the ter
3b90: 6d 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 m in the USING c
3ba0: 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 lause */.
3bb0: 20 69 6e 74 20 69 4c 65 66 74 3b 20 20 20 20 20 int iLeft;
3bc0: 20 20 2f 2a 20 54 61 62 6c 65 20 6f 6e 20 74 68 /* Table on th
3bd0: 65 20 6c 65 66 74 20 77 69 74 68 20 6d 61 74 63 e left with matc
3be0: 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 hing column name
3bf0: 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 */. int
3c00: 69 4c 65 66 74 43 6f 6c 3b 20 20 20 20 2f 2a 20 iLeftCol; /*
3c10: 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 Column number of
3c20: 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e matching column
3c30: 20 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a 2f 0a on the left */.
3c40: 20 20 20 20 20 20 20 20 69 6e 74 20 69 52 69 67 int iRig
3c50: 68 74 43 6f 6c 3b 20 20 20 2f 2a 20 43 6f 6c 75 htCol; /* Colu
3c60: 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20 6d 61 74 mn number of mat
3c70: 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6f 6e 20 ching column on
3c80: 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 0a 20 20 the right */..
3c90: 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 70 4c zName = pL
3ca0: 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b ist->a[j].zName;
3cb0: 0a 20 20 20 20 20 20 20 20 69 52 69 67 68 74 43 . iRightC
3cc0: 6f 6c 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 ol = columnIndex
3cd0: 28 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d (pRightTab, zNam
3ce0: 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 e);. if(
3cf0: 69 52 69 67 68 74 43 6f 6c 3c 30 0a 20 20 20 20 iRightCol<0.
3d00: 20 20 20 20 20 7c 7c 20 21 74 61 62 6c 65 41 6e || !tableAn
3d10: 64 43 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 53 72 dColumnIndex(pSr
3d20: 63 2c 20 69 2b 31 2c 20 7a 4e 61 6d 65 2c 20 26 c, i+1, zName, &
3d30: 69 4c 65 66 74 2c 20 26 69 4c 65 66 74 43 6f 6c iLeft, &iLeftCol
3d40: 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 ). ){.
3d50: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 sqlite3Er
3d60: 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
3d70: 63 61 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73 69 6e cannot join usin
3d80: 67 20 63 6f 6c 75 6d 6e 20 25 73 20 2d 20 63 6f g column %s - co
3d90: 6c 75 6d 6e 20 22 0a 20 20 20 20 20 20 20 20 20 lumn ".
3da0: 20 20 20 22 6e 6f 74 20 70 72 65 73 65 6e 74 20 "not present
3db0: 69 6e 20 62 6f 74 68 20 74 61 62 6c 65 73 22 2c in both tables",
3dc0: 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 zName);.
3dd0: 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 return 1;.
3de0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 }. a
3df0: 64 64 57 68 65 72 65 54 65 72 6d 28 70 50 61 72 ddWhereTerm(pPar
3e00: 73 65 2c 20 70 53 72 63 2c 20 69 4c 65 66 74 2c se, pSrc, iLeft,
3e10: 20 69 4c 65 66 74 43 6f 6c 2c 20 69 2b 31 2c 20 iLeftCol, i+1,
3e20: 69 52 69 67 68 74 43 6f 6c 2c 0a 20 20 20 20 20 iRightCol,.
3e30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
3e40: 69 73 4f 75 74 65 72 2c 20 26 70 2d 3e 70 57 68 isOuter, &p->pWh
3e50: 65 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ere);. }.
3e60: 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e }. }. return
3e70: 20 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 0;.}../* Forwar
3e80: 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 d reference */.s
3e90: 74 61 74 69 63 20 4b 65 79 49 6e 66 6f 20 2a 6b tatic KeyInfo *k
3ea0: 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 eyInfoFromExprLi
3eb0: 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 st(. Parse *pPa
3ec0: 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 rse, /* Pa
3ed0: 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f rsing context */
3ee0: 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 . ExprList *pLi
3ef0: 73 74 2c 20 20 20 20 20 2f 2a 20 46 6f 72 6d 20 st, /* Form
3f00: 74 68 65 20 4b 65 79 49 6e 66 6f 20 6f 62 6a 65 the KeyInfo obje
3f10: 63 74 20 66 72 6f 6d 20 74 68 69 73 20 45 78 70 ct from this Exp
3f20: 72 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 rList */. int i
3f30: 53 74 61 72 74 2c 20 20 20 20 20 20 20 20 20 20 Start,
3f40: 2f 2a 20 42 65 67 69 6e 20 77 69 74 68 20 74 68 /* Begin with th
3f50: 69 73 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 4c 69 is column of pLi
3f60: 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 st */. int nExt
3f70: 72 61 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 ra /*
3f80: 41 64 64 20 74 68 69 73 20 6d 61 6e 79 20 65 78 Add this many ex
3f90: 74 72 61 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 74 tra columns to t
3fa0: 68 65 20 65 6e 64 20 2a 2f 0a 29 3b 0a 0a 2f 2a he end */.);../*
3fb0: 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 .** Generate cod
3fc0: 65 20 74 68 61 74 20 77 69 6c 6c 20 70 75 73 68 e that will push
3fd0: 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 20 72 the record in r
3fe0: 65 67 69 73 74 65 72 73 20 72 65 67 44 61 74 61 egisters regData
3ff0: 0a 2a 2a 20 74 68 72 6f 75 67 68 20 72 65 67 44 .** through regD
4000: 61 74 61 2b 6e 44 61 74 61 2d 31 20 6f 6e 74 6f ata+nData-1 onto
4010: 20 74 68 65 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a the sorter..*/.
4020: 73 74 61 74 69 63 20 76 6f 69 64 20 70 75 73 68 static void push
4030: 4f 6e 74 6f 53 6f 72 74 65 72 28 0a 20 20 50 61 OntoSorter(. Pa
4040: 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 rse *pParse,
4050: 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 /* Parser c
4060: 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 6f 72 74 ontext */. Sort
4070: 43 74 78 20 2a 70 53 6f 72 74 2c 20 20 20 20 20 Ctx *pSort,
4080: 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f /* Informatio
4090: 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44 45 n about the ORDE
40a0: 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 R BY clause */.
40b0: 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 Select *pSelect
40c0: 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 , /* The w
40d0: 68 6f 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74 hole SELECT stat
40e0: 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 ement */. int r
40f0: 65 67 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 egData,
4100: 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 /* First regis
4110: 74 65 72 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 ter holding data
4120: 20 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a 2f to be sorted */
4130: 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 . int nData,
4140: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
4150: 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 ber of elements
4160: 69 6e 20 74 68 65 20 64 61 74 61 20 61 72 72 61 in the data arra
4170: 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 65 66 y */. int nPref
4180: 69 78 52 65 67 20 20 20 20 20 20 20 20 20 2f 2a ixReg /*
4190: 20 4e 6f 2e 20 6f 66 20 72 65 67 20 70 72 69 6f No. of reg prio
41a0: 72 20 74 6f 20 72 65 67 44 61 74 61 20 61 76 61 r to regData ava
41b0: 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 2a ilable for use *
41c0: 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d /.){. Vdbe *v =
41d0: 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 pParse->pVdbe;
41e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
41f0: 20 20 20 20 20 20 20 20 2f 2a 20 53 74 6d 74 20 /* Stmt
4200: 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 under constructi
4210: 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 53 65 71 on */. int bSeq
4220: 20 3d 20 28 28 70 53 6f 72 74 2d 3e 73 6f 72 74 = ((pSort->sort
4230: 46 6c 61 67 73 20 26 20 53 4f 52 54 46 4c 41 47 Flags & SORTFLAG
4240: 5f 55 73 65 53 6f 72 74 65 72 29 3d 3d 30 29 3b _UseSorter)==0);
4250: 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70 . int nExpr = p
4260: 53 6f 72 74 2d 3e 70 4f 72 64 65 72 42 79 2d 3e Sort->pOrderBy->
4270: 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 nExpr;
4280: 20 20 20 20 2f 2a 20 4e 6f 2e 20 6f 66 20 4f 52 /* No. of OR
4290: 44 45 52 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a DER BY terms */.
42a0: 20 20 69 6e 74 20 6e 42 61 73 65 20 3d 20 6e 45 int nBase = nE
42b0: 78 70 72 20 2b 20 62 53 65 71 20 2b 20 6e 44 61 xpr + bSeq + nDa
42c0: 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
42d0: 20 20 20 2f 2a 20 46 69 65 6c 64 73 20 69 6e 20 /* Fields in
42e0: 73 6f 72 74 65 72 20 72 65 63 6f 72 64 20 2a 2f sorter record */
42f0: 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65 3b 20 . int regBase;
4300: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4310: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4320: 20 20 20 20 2f 2a 20 52 65 67 73 20 66 6f 72 20 /* Regs for
4330: 73 6f 72 74 65 72 20 72 65 63 6f 72 64 20 2a 2f sorter record */
4340: 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 . int regRecord
4350: 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 = ++pParse->nMe
4360: 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 m;
4370: 20 20 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 64 /* Assembled
4380: 20 73 6f 72 74 65 72 20 72 65 63 6f 72 64 20 2a sorter record *
4390: 2f 0a 20 20 69 6e 74 20 6e 4f 42 53 61 74 20 3d /. int nOBSat =
43a0: 20 70 53 6f 72 74 2d 3e 6e 4f 42 53 61 74 3b 20 pSort->nOBSat;
43b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
43c0: 20 20 20 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 /* ORDER BY
43d0: 20 74 65 72 6d 73 20 74 6f 20 73 6b 69 70 20 2a terms to skip *
43e0: 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 /. int op;
43f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
4400: 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 /* Opcode
4410: 20 74 6f 20 61 64 64 20 73 6f 72 74 65 72 20 72 to add sorter r
4420: 65 63 6f 72 64 20 74 6f 20 73 6f 72 74 65 72 20 ecord to sorter
4430: 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 62 53 */.. assert( bS
4440: 65 71 3d 3d 30 20 7c 7c 20 62 53 65 71 3d 3d 31 eq==0 || bSeq==1
4450: 20 29 3b 0a 20 20 69 66 28 20 6e 50 72 65 66 69 );. if( nPrefi
4460: 78 52 65 67 20 29 7b 0a 20 20 20 20 61 73 73 65 xReg ){. asse
4470: 72 74 28 20 6e 50 72 65 66 69 78 52 65 67 3d 3d rt( nPrefixReg==
4480: 6e 45 78 70 72 2b 62 53 65 71 20 29 3b 0a 20 20 nExpr+bSeq );.
4490: 20 20 72 65 67 42 61 73 65 20 3d 20 72 65 67 44 regBase = regD
44a0: 61 74 61 20 2d 20 6e 45 78 70 72 20 2d 20 62 53 ata - nExpr - bS
44b0: 65 71 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 eq;. }else{.
44c0: 20 72 65 67 42 61 73 65 20 3d 20 70 50 61 72 73 regBase = pPars
44d0: 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 e->nMem + 1;.
44e0: 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d pParse->nMem +=
44f0: 20 6e 42 61 73 65 3b 0a 20 20 7d 0a 20 20 73 71 nBase;. }. sq
4500: 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 lite3ExprCodeExp
4510: 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 rList(pParse, pS
4520: 6f 72 74 2d 3e 70 4f 72 64 65 72 42 79 2c 20 72 ort->pOrderBy, r
4530: 65 67 42 61 73 65 2c 20 53 51 4c 49 54 45 5f 45 egBase, SQLITE_E
4540: 43 45 4c 5f 44 55 50 29 3b 0a 20 20 69 66 28 20 CEL_DUP);. if(
4550: 62 53 65 71 20 29 7b 0a 20 20 20 20 73 71 6c 69 bSeq ){. sqli
4560: 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
4570: 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c 20 70 53 OP_Sequence, pS
4580: 6f 72 74 2d 3e 69 45 43 75 72 73 6f 72 2c 20 72 ort->iECursor, r
4590: 65 67 42 61 73 65 2b 6e 45 78 70 72 29 3b 0a 20 egBase+nExpr);.
45a0: 20 7d 0a 20 20 69 66 28 20 6e 50 72 65 66 69 78 }. if( nPrefix
45b0: 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 Reg==0 ){. sq
45c0: 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 lite3ExprCodeMov
45d0: 65 28 70 50 61 72 73 65 2c 20 72 65 67 44 61 74 e(pParse, regDat
45e0: 61 2c 20 72 65 67 42 61 73 65 2b 6e 45 78 70 72 a, regBase+nExpr
45f0: 2b 62 53 65 71 2c 20 6e 44 61 74 61 29 3b 0a 20 +bSeq, nData);.
4600: 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 }.. sqlite3Vdb
4610: 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 eAddOp3(v, OP_Ma
4620: 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 keRecord, regBas
4630: 65 2b 6e 4f 42 53 61 74 2c 20 6e 42 61 73 65 2d e+nOBSat, nBase-
4640: 6e 4f 42 53 61 74 2c 20 72 65 67 52 65 63 6f 72 nOBSat, regRecor
4650: 64 29 3b 0a 20 20 69 66 28 20 6e 4f 42 53 61 74 d);. if( nOBSat
4660: 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 >0 ){. int re
4670: 67 50 72 65 76 4b 65 79 3b 20 20 20 2f 2a 20 54 gPrevKey; /* T
4680: 68 65 20 66 69 72 73 74 20 6e 4f 42 53 61 74 20 he first nOBSat
4690: 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 70 columns of the p
46a0: 72 65 76 69 6f 75 73 20 72 6f 77 20 2a 2f 0a 20 revious row */.
46b0: 20 20 20 69 6e 74 20 61 64 64 72 46 69 72 73 74 int addrFirst
46c0: 3b 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 ; /* Address
46d0: 6f 66 20 74 68 65 20 4f 50 5f 49 66 4e 6f 74 20 of the OP_IfNot
46e0: 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 69 6e opcode */. in
46f0: 74 20 61 64 64 72 4a 6d 70 3b 20 20 20 20 20 20 t addrJmp;
4700: 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 /* Address of th
4710: 65 20 4f 50 5f 4a 75 6d 70 20 6f 70 63 6f 64 65 e OP_Jump opcode
4720: 20 2a 2f 0a 20 20 20 20 56 64 62 65 4f 70 20 2a */. VdbeOp *
4730: 70 4f 70 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63 pOp; /* Opc
4740: 6f 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 ode that opens t
4750: 68 65 20 73 6f 72 74 65 72 20 2a 2f 0a 20 20 20 he sorter */.
4760: 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20 20 20 20 int nKey;
4770: 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 /* Number of
4780: 73 6f 72 74 69 6e 67 20 6b 65 79 20 63 6f 6c 75 sorting key colu
4790: 6d 6e 73 2c 20 69 6e 63 6c 75 64 69 6e 67 20 4f mns, including O
47a0: 50 5f 53 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20 P_Sequence */.
47b0: 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 49 3b 20 KeyInfo *pKI;
47c0: 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 /* Original
47d0: 4b 65 79 49 6e 66 6f 20 6f 6e 20 74 68 65 20 73 KeyInfo on the s
47e0: 6f 72 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a 0a orter table */..
47f0: 20 20 20 20 72 65 67 50 72 65 76 4b 65 79 20 3d regPrevKey =
4800: 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b pParse->nMem+1;
4810: 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 . pParse->nMe
4820: 6d 20 2b 3d 20 70 53 6f 72 74 2d 3e 6e 4f 42 53 m += pSort->nOBS
4830: 61 74 3b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 6e at;. nKey = n
4840: 45 78 70 72 20 2d 20 70 53 6f 72 74 2d 3e 6e 4f Expr - pSort->nO
4850: 42 53 61 74 20 2b 20 62 53 65 71 3b 0a 20 20 20 BSat + bSeq;.
4860: 20 69 66 28 20 62 53 65 71 20 29 7b 0a 20 20 20 if( bSeq ){.
4870: 20 20 20 61 64 64 72 46 69 72 73 74 20 3d 20 73 addrFirst = s
4880: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
4890: 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 65 (v, OP_IfNot, re
48a0: 67 42 61 73 65 2b 6e 45 78 70 72 29 3b 20 0a 20 gBase+nExpr); .
48b0: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
48c0: 61 64 64 72 46 69 72 73 74 20 3d 20 73 71 6c 69 addrFirst = sqli
48d0: 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
48e0: 20 4f 50 5f 53 65 71 75 65 6e 63 65 54 65 73 74 OP_SequenceTest
48f0: 2c 20 70 53 6f 72 74 2d 3e 69 45 43 75 72 73 6f , pSort->iECurso
4900: 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 56 64 r);. }. Vd
4910: 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 beCoverage(v);.
4920: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
4930: 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 dOp3(v, OP_Compa
4940: 72 65 2c 20 72 65 67 50 72 65 76 4b 65 79 2c 20 re, regPrevKey,
4950: 72 65 67 42 61 73 65 2c 20 70 53 6f 72 74 2d 3e regBase, pSort->
4960: 6e 4f 42 53 61 74 29 3b 0a 20 20 20 20 70 4f 70 nOBSat);. pOp
4970: 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 = sqlite3VdbeGe
4980: 74 4f 70 28 76 2c 20 70 53 6f 72 74 2d 3e 61 64 tOp(v, pSort->ad
4990: 64 72 53 6f 72 74 49 6e 64 65 78 29 3b 0a 20 20 drSortIndex);.
49a0: 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 if( pParse->db
49b0: 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
49c0: 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 70 4f 70 return;. pOp
49d0: 2d 3e 70 32 20 3d 20 6e 4b 65 79 20 2b 20 6e 44 ->p2 = nKey + nD
49e0: 61 74 61 3b 0a 20 20 20 20 70 4b 49 20 3d 20 70 ata;. pKI = p
49f0: 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b Op->p4.pKeyInfo;
4a00: 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 4b 49 2d . memset(pKI-
4a10: 3e 61 53 6f 72 74 4f 72 64 65 72 2c 20 30 2c 20 >aSortOrder, 0,
4a20: 70 4b 49 2d 3e 6e 46 69 65 6c 64 29 3b 20 2f 2a pKI->nField); /*
4a30: 20 4d 61 6b 65 73 20 4f 50 5f 4a 75 6d 70 20 62 Makes OP_Jump b
4a40: 65 6c 6f 77 20 74 65 73 74 61 62 6c 65 20 2a 2f elow testable */
4a50: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
4a60: 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 ChangeP4(v, -1,
4a70: 28 63 68 61 72 2a 29 70 4b 49 2c 20 50 34 5f 4b (char*)pKI, P4_K
4a80: 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 70 4f 70 EYINFO);. pOp
4a90: 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 ->p4.pKeyInfo =
4aa0: 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c keyInfoFromExprL
4ab0: 69 73 74 28 70 50 61 72 73 65 2c 20 70 53 6f 72 ist(pParse, pSor
4ac0: 74 2d 3e 70 4f 72 64 65 72 42 79 2c 20 6e 4f 42 t->pOrderBy, nOB
4ad0: 53 61 74 2c 20 31 29 3b 0a 20 20 20 20 61 64 64 Sat, 1);. add
4ae0: 72 4a 6d 70 20 3d 20 73 71 6c 69 74 65 33 56 64 rJmp = sqlite3Vd
4af0: 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 beCurrentAddr(v)
4b00: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
4b10: 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 eAddOp3(v, OP_Ju
4b20: 6d 70 2c 20 61 64 64 72 4a 6d 70 2b 31 2c 20 30 mp, addrJmp+1, 0
4b30: 2c 20 61 64 64 72 4a 6d 70 2b 31 29 3b 20 56 64 , addrJmp+1); Vd
4b40: 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 beCoverage(v);.
4b50: 20 20 20 70 53 6f 72 74 2d 3e 6c 61 62 65 6c 42 pSort->labelB
4b60: 6b 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 56 64 kOut = sqlite3Vd
4b70: 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a beMakeLabel(v);.
4b80: 20 20 20 20 70 53 6f 72 74 2d 3e 72 65 67 52 65 pSort->regRe
4b90: 74 75 72 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d turn = ++pParse-
4ba0: 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 >nMem;. sqlit
4bb0: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
4bc0: 4f 50 5f 47 6f 73 75 62 2c 20 70 53 6f 72 74 2d OP_Gosub, pSort-
4bd0: 3e 72 65 67 52 65 74 75 72 6e 2c 20 70 53 6f 72 >regReturn, pSor
4be0: 74 2d 3e 6c 61 62 65 6c 42 6b 4f 75 74 29 3b 0a t->labelBkOut);.
4bf0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
4c00: 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 73 65 ddOp1(v, OP_Rese
4c10: 74 53 6f 72 74 65 72 2c 20 70 53 6f 72 74 2d 3e tSorter, pSort->
4c20: 69 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 iECursor);. s
4c30: 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
4c40: 72 65 28 76 2c 20 61 64 64 72 46 69 72 73 74 29 re(v, addrFirst)
4c50: 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 ;. sqlite3Exp
4c60: 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 rCodeMove(pParse
4c70: 2c 20 72 65 67 42 61 73 65 2c 20 72 65 67 50 72 , regBase, regPr
4c80: 65 76 4b 65 79 2c 20 70 53 6f 72 74 2d 3e 6e 4f evKey, pSort->nO
4c90: 42 53 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 BSat);. sqlit
4ca0: 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 e3VdbeJumpHere(v
4cb0: 2c 20 61 64 64 72 4a 6d 70 29 3b 0a 20 20 7d 0a , addrJmp);. }.
4cc0: 20 20 69 66 28 20 70 53 6f 72 74 2d 3e 73 6f 72 if( pSort->sor
4cd0: 74 46 6c 61 67 73 20 26 20 53 4f 52 54 46 4c 41 tFlags & SORTFLA
4ce0: 47 5f 55 73 65 53 6f 72 74 65 72 20 29 7b 0a 20 G_UseSorter ){.
4cf0: 20 20 20 6f 70 20 3d 20 4f 50 5f 53 6f 72 74 65 op = OP_Sorte
4d00: 72 49 6e 73 65 72 74 3b 0a 20 20 7d 65 6c 73 65 rInsert;. }else
4d10: 7b 0a 20 20 20 20 6f 70 20 3d 20 4f 50 5f 49 64 {. op = OP_Id
4d20: 78 49 6e 73 65 72 74 3b 0a 20 20 7d 0a 20 20 73 xInsert;. }. s
4d30: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
4d40: 28 76 2c 20 6f 70 2c 20 70 53 6f 72 74 2d 3e 69 (v, op, pSort->i
4d50: 45 43 75 72 73 6f 72 2c 20 72 65 67 52 65 63 6f ECursor, regReco
4d60: 72 64 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 rd);. if( pSele
4d70: 63 74 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 ct->iLimit ){.
4d80: 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61 64 64 int addr1, add
4d90: 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d r2;. int iLim
4da0: 69 74 3b 0a 20 20 20 20 69 66 28 20 70 53 65 6c it;. if( pSel
4db0: 65 63 74 2d 3e 69 4f 66 66 73 65 74 20 29 7b 0a ect->iOffset ){.
4dc0: 20 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 iLimit = p
4dd0: 53 65 6c 65 63 74 2d 3e 69 4f 66 66 73 65 74 2b Select->iOffset+
4de0: 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 1;. }else{.
4df0: 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 70 53 65 iLimit = pSe
4e00: 6c 65 63 74 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 lect->iLimit;.
4e10: 20 20 7d 0a 20 20 20 20 61 64 64 72 31 20 3d 20 }. addr1 =
4e20: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
4e30: 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 1(v, OP_IfZero,
4e40: 69 4c 69 6d 69 74 29 3b 20 56 64 62 65 43 6f 76 iLimit); VdbeCov
4e50: 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 73 71 erage(v);. sq
4e60: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
4e70: 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4c v, OP_AddImm, iL
4e80: 69 6d 69 74 2c 20 2d 31 29 3b 0a 20 20 20 20 61 imit, -1);. a
4e90: 64 64 72 32 20 3d 20 73 71 6c 69 74 65 33 56 64 ddr2 = sqlite3Vd
4ea0: 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 beAddOp0(v, OP_G
4eb0: 6f 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 oto);. sqlite
4ec0: 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
4ed0: 20 61 64 64 72 31 29 3b 0a 20 20 20 20 73 71 6c addr1);. sql
4ee0: 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
4ef0: 2c 20 4f 50 5f 4c 61 73 74 2c 20 70 53 6f 72 74 , OP_Last, pSort
4f00: 2d 3e 69 45 43 75 72 73 6f 72 29 3b 0a 20 20 20 ->iECursor);.
4f10: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
4f20: 70 31 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c p1(v, OP_Delete,
4f30: 20 70 53 6f 72 74 2d 3e 69 45 43 75 72 73 6f 72 pSort->iECursor
4f40: 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 );. sqlite3Vd
4f50: 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 beJumpHere(v, ad
4f60: 64 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a dr2);. }.}../*.
4f70: 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 6f 20 69 ** Add code to i
4f80: 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 4f 46 46 mplement the OFF
4f90: 53 45 54 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f SET.*/.static vo
4fa0: 69 64 20 63 6f 64 65 4f 66 66 73 65 74 28 0a 20 id codeOffset(.
4fb0: 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 Vdbe *v,
4fc0: 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 /* Generate c
4fd0: 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 4d ode into this VM
4fe0: 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 */. int iOffse
4ff0: 74 2c 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 t, /* Regis
5000: 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 ter holding the
5010: 6f 66 66 73 65 74 20 63 6f 75 6e 74 65 72 20 2a offset counter *
5020: 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 /. int iContinu
5030: 65 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 e /* Jump he
5040: 72 65 20 74 6f 20 73 6b 69 70 20 74 68 65 20 63 re to skip the c
5050: 75 72 72 65 6e 74 20 72 65 63 6f 72 64 20 2a 2f urrent record */
5060: 0a 29 7b 0a 20 20 69 66 28 20 69 4f 66 66 73 65 .){. if( iOffse
5070: 74 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 t>0 ){. int a
5080: 64 64 72 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 ddr;. addr =
5090: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
50a0: 33 28 76 2c 20 4f 50 5f 49 66 4e 65 67 2c 20 69 3(v, OP_IfNeg, i
50b0: 4f 66 66 73 65 74 2c 20 30 2c 20 2d 31 29 3b 20 Offset, 0, -1);
50c0: 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
50d0: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
50e0: 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 AddOp2(v, OP_Got
50f0: 6f 2c 20 30 2c 20 69 43 6f 6e 74 69 6e 75 65 29 o, 0, iContinue)
5100: 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e ;. VdbeCommen
5110: 74 28 28 76 2c 20 22 73 6b 69 70 20 4f 46 46 53 t((v, "skip OFFS
5120: 45 54 20 72 65 63 6f 72 64 73 22 29 29 3b 0a 20 ET records"));.
5130: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 sqlite3VdbeJu
5140: 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b mpHere(v, addr);
5150: 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 . }.}../*.** Ad
5160: 64 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c d code that will
5170: 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 check to make s
5180: 75 72 65 20 74 68 65 20 4e 20 72 65 67 69 73 74 ure the N regist
5190: 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 ers starting at
51a0: 69 4d 65 6d 0a 2a 2a 20 66 6f 72 6d 20 61 20 64 iMem.** form a d
51b0: 69 73 74 69 6e 63 74 20 65 6e 74 72 79 2e 20 20 istinct entry.
51c0: 69 54 61 62 20 69 73 20 61 20 73 6f 72 74 69 6e iTab is a sortin
51d0: 67 20 69 6e 64 65 78 20 74 68 61 74 20 68 6f 6c g index that hol
51e0: 64 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a ds previously.**
51f0: 20 73 65 65 6e 20 63 6f 6d 62 69 6e 61 74 69 6f seen combinatio
5200: 6e 73 20 6f 66 20 74 68 65 20 4e 20 76 61 6c 75 ns of the N valu
5210: 65 73 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 es. A new entry
5220: 20 69 73 20 6d 61 64 65 20 69 6e 20 69 54 61 62 is made in iTab
5230: 0a 2a 2a 20 69 66 20 74 68 65 20 63 75 72 72 65 .** if the curre
5240: 6e 74 20 4e 20 76 61 6c 75 65 73 20 61 72 65 20 nt N values are
5250: 6e 65 77 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d new..**.** A jum
5260: 70 20 74 6f 20 61 64 64 72 52 65 70 65 61 74 20 p to addrRepeat
5270: 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68 65 20 is made and the
5280: 4e 2b 31 20 76 61 6c 75 65 73 20 61 72 65 20 70 N+1 values are p
5290: 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a opped from the.*
52a0: 2a 20 73 74 61 63 6b 20 69 66 20 74 68 65 20 74 * stack if the t
52b0: 6f 70 20 4e 20 65 6c 65 6d 65 6e 74 73 20 61 72 op N elements ar
52c0: 65 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 2e 0a e not distinct..
52d0: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 */.static void c
52e0: 6f 64 65 44 69 73 74 69 6e 63 74 28 0a 20 20 50 odeDistinct(. P
52f0: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
5300: 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 /* Parsing and
5310: 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 code generating
5320: 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e context */. in
5330: 74 20 69 54 61 62 2c 20 20 20 20 20 20 20 20 20 t iTab,
5340: 20 2f 2a 20 41 20 73 6f 72 74 69 6e 67 20 69 6e /* A sorting in
5350: 64 65 78 20 75 73 65 64 20 74 6f 20 74 65 73 74 dex used to test
5360: 20 66 6f 72 20 64 69 73 74 69 6e 63 74 6e 65 73 for distinctnes
5370: 73 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 52 s */. int addrR
5380: 65 70 65 61 74 2c 20 20 20 20 2f 2a 20 4a 75 6d epeat, /* Jum
5390: 70 20 74 6f 20 68 65 72 65 20 69 66 20 6e 6f 74 p to here if not
53a0: 20 64 69 73 74 69 6e 63 74 20 2a 2f 0a 20 20 69 distinct */. i
53b0: 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 nt N,
53c0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 /* Number of e
53d0: 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 lements */. int
53e0: 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 20 20 iMem
53f0: 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 /* First element
5400: 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 */.){. Vdbe *v
5410: 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 76 ;. int r1;.. v
5420: 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
5430: 3b 0a 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 ;. r1 = sqlite3
5440: 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
5450: 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 e);. sqlite3Vdb
5460: 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 eAddOp4Int(v, OP
5470: 5f 46 6f 75 6e 64 2c 20 69 54 61 62 2c 20 61 64 _Found, iTab, ad
5480: 64 72 52 65 70 65 61 74 2c 20 69 4d 65 6d 2c 20 drRepeat, iMem,
5490: 4e 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 N); VdbeCoverage
54a0: 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 (v);. sqlite3Vd
54b0: 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d beAddOp3(v, OP_M
54c0: 61 6b 65 52 65 63 6f 72 64 2c 20 69 4d 65 6d 2c akeRecord, iMem,
54d0: 20 4e 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 N, r1);. sqlit
54e0: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
54f0: 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 54 OP_IdxInsert, iT
5500: 61 62 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74 ab, r1);. sqlit
5510: 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
5520: 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 7d 0a (pParse, r1);.}.
5530: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
5540: 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a OMIT_SUBQUERY./*
5550: 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 .** Generate an
5560: 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 68 error message wh
5570: 65 6e 20 61 20 53 45 4c 45 43 54 20 69 73 20 75 en a SELECT is u
5580: 73 65 64 20 77 69 74 68 69 6e 20 61 20 73 75 62 sed within a sub
5590: 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 28 65 expression.** (e
55a0: 78 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e 20 28 xample: "a IN (
55b0: 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 SELECT * FROM ta
55c0: 62 6c 65 29 22 29 20 62 75 74 20 69 74 20 68 61 ble)") but it ha
55d0: 73 20 6d 6f 72 65 20 74 68 61 6e 20 31 20 72 65 s more than 1 re
55e0: 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 sult.** column.
55f0: 20 57 65 20 64 6f 20 74 68 69 73 20 69 6e 20 61 We do this in a
5600: 20 73 75 62 72 6f 75 74 69 6e 65 20 62 65 63 61 subroutine beca
5610: 75 73 65 20 74 68 65 20 65 72 72 6f 72 20 75 73 use the error us
5620: 65 64 20 74 6f 20 6f 63 63 75 72 0a 2a 2a 20 69 ed to occur.** i
5630: 6e 20 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 n multiple place
5640: 73 2e 20 20 28 54 68 65 20 65 72 72 6f 72 20 6f s. (The error o
5650: 6e 6c 79 20 6f 63 63 75 72 73 20 69 6e 20 6f 6e nly occurs in on
5660: 65 20 70 6c 61 63 65 20 6e 6f 77 2c 20 62 75 74 e place now, but
5670: 20 77 65 0a 2a 2a 20 72 65 74 61 69 6e 20 74 68 we.** retain th
5680: 65 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 e subroutine to
5690: 6d 69 6e 69 6d 69 7a 65 20 63 6f 64 65 20 64 69 minimize code di
56a0: 73 72 75 70 74 69 6f 6e 2e 29 0a 2a 2f 0a 73 74 sruption.).*/.st
56b0: 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46 6f atic int checkFo
56c0: 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 rMultiColumnSele
56d0: 63 74 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65 ctError(. Parse
56e0: 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 *pParse,
56f0: 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 /* Parse context
5700: 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 . */. SelectDes
5710: 74 20 2a 70 44 65 73 74 2c 20 20 20 2f 2a 20 44 t *pDest, /* D
5720: 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66 20 53 45 estination of SE
5730: 4c 45 43 54 20 72 65 73 75 6c 74 73 20 2a 2f 0a LECT results */.
5740: 20 20 69 6e 74 20 6e 45 78 70 72 20 20 20 20 20 int nExpr
5750: 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
5760: 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d of result colum
5770: 6e 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 ns returned by S
5780: 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e ELECT */.){. in
5790: 74 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d t eDest = pDest-
57a0: 3e 65 44 65 73 74 3b 0a 20 20 69 66 28 20 6e 45 >eDest;. if( nE
57b0: 78 70 72 3e 31 20 26 26 20 28 65 44 65 73 74 3d xpr>1 && (eDest=
57c0: 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65 44 65 73 =SRT_Mem || eDes
57d0: 74 3d 3d 53 52 54 5f 53 65 74 29 20 29 7b 0a 20 t==SRT_Set) ){.
57e0: 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
57f0: 73 67 28 70 50 61 72 73 65 2c 20 22 6f 6e 6c 79 sg(pParse, "only
5800: 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74 a single result
5810: 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 22 0a 20 allowed for ".
5820: 20 20 20 20 20 20 22 61 20 53 45 4c 45 43 54 20 "a SELECT
5830: 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 that is part of
5840: 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b an expression");
5850: 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 . return 1;.
5860: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 }else{. retu
5870: 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 rn 0;. }.}.#end
5880: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 if../*.** This r
5890: 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 outine generates
58a0: 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 the code for th
58b0: 65 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20 e inside of the
58c0: 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20 6f 66 inner loop.** of
58d0: 20 61 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a a SELECT..**.**
58e0: 20 49 66 20 73 72 63 54 61 62 20 69 73 20 6e 65 If srcTab is ne
58f0: 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74 68 65 gative, then the
5900: 20 70 45 4c 69 73 74 20 65 78 70 72 65 73 73 69 pEList expressi
5910: 6f 6e 73 0a 2a 2a 20 61 72 65 20 65 76 61 6c 75 ons.** are evalu
5920: 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f ated in order to
5930: 20 67 65 74 20 74 68 65 20 64 61 74 61 20 66 6f get the data fo
5940: 72 20 74 68 69 73 20 72 6f 77 2e 20 20 49 66 20 r this row. If
5950: 73 72 63 54 61 62 20 69 73 0a 2a 2a 20 7a 65 72 srcTab is.** zer
5960: 6f 20 6f 72 20 6d 6f 72 65 2c 20 74 68 65 6e 20 o or more, then
5970: 64 61 74 61 20 69 73 20 70 75 6c 6c 65 64 20 66 data is pulled f
5980: 72 6f 6d 20 73 72 63 54 61 62 20 61 6e 64 20 70 rom srcTab and p
5990: 45 4c 69 73 74 20 69 73 20 75 73 65 64 20 6f 6e EList is used on
59a0: 6c 79 20 0a 2a 2a 20 74 6f 20 67 65 74 20 6e 75 ly .** to get nu
59b0: 6d 62 65 72 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 mber columns and
59c0: 20 74 68 65 20 64 61 74 61 74 79 70 65 20 66 6f the datatype fo
59d0: 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a r each column..*
59e0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 /.static void se
59f0: 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 0a 20 lectInnerLoop(.
5a00: 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
5a10: 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 /* The
5a20: 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a parser context *
5a30: 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 /. Select *p,
5a40: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 /* T
5a50: 68 65 20 63 6f 6d 70 6c 65 74 65 20 73 65 6c 65 he complete sele
5a60: 63 74 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 ct statement bei
5a70: 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 45 78 ng coded */. Ex
5a80: 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 prList *pEList,
5a90: 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 /* List of
5aa0: 20 76 61 6c 75 65 73 20 62 65 69 6e 67 20 65 78 values being ex
5ab0: 74 72 61 63 74 65 64 20 2a 2f 0a 20 20 69 6e 74 tracted */. int
5ac0: 20 73 72 63 54 61 62 2c 20 20 20 20 20 20 20 20 srcTab,
5ad0: 20 20 20 20 20 2f 2a 20 50 75 6c 6c 20 64 61 74 /* Pull dat
5ae0: 61 20 66 72 6f 6d 20 74 68 69 73 20 74 61 62 6c a from this tabl
5af0: 65 20 2a 2f 0a 20 20 53 6f 72 74 43 74 78 20 2a e */. SortCtx *
5b00: 70 53 6f 72 74 2c 20 20 20 20 20 20 20 20 20 2f pSort, /
5b10: 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 * If not NULL, i
5b20: 6e 66 6f 20 6f 6e 20 68 6f 77 20 74 6f 20 70 72 nfo on how to pr
5b30: 6f 63 65 73 73 20 4f 52 44 45 52 20 42 59 20 2a ocess ORDER BY *
5b40: 2f 0a 20 20 44 69 73 74 69 6e 63 74 43 74 78 20 /. DistinctCtx
5b50: 2a 70 44 69 73 74 69 6e 63 74 2c 20 2f 2a 20 49 *pDistinct, /* I
5b60: 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 6e 66 6f f not NULL, info
5b70: 20 6f 6e 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 on how to proce
5b80: 73 73 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 ss DISTINCT */.
5b90: 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 SelectDest *pDe
5ba0: 73 74 2c 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 st, /* How
5bb0: 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 to dispose of th
5bc0: 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 69 e results */. i
5bd0: 6e 74 20 69 43 6f 6e 74 69 6e 75 65 2c 20 20 20 nt iContinue,
5be0: 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 /* Jump h
5bf0: 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 ere to continue
5c00: 77 69 74 68 20 6e 65 78 74 20 72 6f 77 20 2a 2f with next row */
5c10: 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 . int iBreak
5c20: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 /* Ju
5c30: 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b mp here to break
5c40: 20 6f 75 74 20 6f 66 20 74 68 65 20 69 6e 6e 65 out of the inne
5c50: 72 20 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a 20 20 56 r loop */.){. V
5c60: 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d dbe *v = pParse-
5c70: 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b >pVdbe;. int i;
5c80: 0a 20 20 69 6e 74 20 68 61 73 44 69 73 74 69 6e . int hasDistin
5c90: 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 ct; /* Tr
5ca0: 75 65 20 69 66 20 74 68 65 20 44 49 53 54 49 4e ue if the DISTIN
5cb0: 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 CT keyword is pr
5cc0: 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 esent */. int r
5cd0: 65 67 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20 egResult;
5ce0: 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 /* Start
5cf0: 6f 66 20 6d 65 6d 6f 72 79 20 68 6f 6c 64 69 6e of memory holdin
5d00: 67 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a g result set */.
5d10: 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70 44 int eDest = pD
5d20: 65 73 74 2d 3e 65 44 65 73 74 3b 20 20 20 2f 2a est->eDest; /*
5d30: 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73 65 20 How to dispose
5d40: 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 of results */.
5d50: 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73 int iParm = pDes
5d60: 74 2d 3e 69 53 44 50 61 72 6d 3b 20 2f 2a 20 46 t->iSDParm; /* F
5d70: 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f irst argument to
5d80: 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68 6f 64 disposal method
5d90: 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 75 6c */. int nResul
5da0: 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 tCol;
5db0: 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 /* Number of r
5dc0: 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f esult columns */
5dd0: 0a 20 20 69 6e 74 20 6e 50 72 65 66 69 78 52 65 . int nPrefixRe
5de0: 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f g = 0; /
5df0: 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 * Number of extr
5e00: 61 20 72 65 67 69 73 74 65 72 73 20 62 65 66 6f a registers befo
5e10: 72 65 20 72 65 67 52 65 73 75 6c 74 20 2a 2f 0a re regResult */.
5e20: 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a . assert( v );.
5e30: 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 assert( pEList
5e40: 21 3d 30 20 29 3b 0a 20 20 68 61 73 44 69 73 74 !=0 );. hasDist
5e50: 69 6e 63 74 20 3d 20 70 44 69 73 74 69 6e 63 74 inct = pDistinct
5e60: 20 3f 20 70 44 69 73 74 69 6e 63 74 2d 3e 65 54 ? pDistinct->eT
5e70: 6e 63 74 54 79 70 65 20 3a 20 57 48 45 52 45 5f nctType : WHERE_
5e80: 44 49 53 54 49 4e 43 54 5f 4e 4f 4f 50 3b 0a 20 DISTINCT_NOOP;.
5e90: 20 69 66 28 20 70 53 6f 72 74 20 26 26 20 70 53 if( pSort && pS
5ea0: 6f 72 74 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 ort->pOrderBy==0
5eb0: 20 29 20 70 53 6f 72 74 20 3d 20 30 3b 0a 20 20 ) pSort = 0;.
5ec0: 69 66 28 20 70 53 6f 72 74 3d 3d 30 20 26 26 20 if( pSort==0 &&
5ed0: 21 68 61 73 44 69 73 74 69 6e 63 74 20 29 7b 0a !hasDistinct ){.
5ee0: 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6e assert( iCon
5ef0: 74 69 6e 75 65 21 3d 30 20 29 3b 0a 20 20 20 20 tinue!=0 );.
5f00: 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2d codeOffset(v, p-
5f10: 3e 69 4f 66 66 73 65 74 2c 20 69 43 6f 6e 74 69 >iOffset, iConti
5f20: 6e 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 nue);. }.. /*
5f30: 50 75 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 Pull the request
5f40: 65 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f ed columns.. */
5f50: 0a 20 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d 20 . nResultCol =
5f60: 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 0a pEList->nExpr;..
5f70: 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 53 64 if( pDest->iSd
5f80: 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 st==0 ){. if(
5f90: 20 70 53 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 pSort ){.
5fa0: 6e 50 72 65 66 69 78 52 65 67 20 3d 20 70 53 6f nPrefixReg = pSo
5fb0: 72 74 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 6e 45 rt->pOrderBy->nE
5fc0: 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 21 xpr;. if( !
5fd0: 28 70 53 6f 72 74 2d 3e 73 6f 72 74 46 6c 61 67 (pSort->sortFlag
5fe0: 73 20 26 20 53 4f 52 54 46 4c 41 47 5f 55 73 65 s & SORTFLAG_Use
5ff0: 53 6f 72 74 65 72 29 20 29 20 6e 50 72 65 66 69 Sorter) ) nPrefi
6000: 78 52 65 67 2b 2b 3b 0a 20 20 20 20 20 20 70 50 xReg++;. pP
6010: 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 50 arse->nMem += nP
6020: 72 65 66 69 78 52 65 67 3b 0a 20 20 20 20 7d 0a refixReg;. }.
6030: 20 20 20 20 70 44 65 73 74 2d 3e 69 53 64 73 74 pDest->iSdst
6040: 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b = pParse->nMem+
6050: 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 1;. pParse->n
6060: 4d 65 6d 20 2b 3d 20 6e 52 65 73 75 6c 74 43 6f Mem += nResultCo
6070: 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 l;. }else if( p
6080: 44 65 73 74 2d 3e 69 53 64 73 74 2b 6e 52 65 73 Dest->iSdst+nRes
6090: 75 6c 74 43 6f 6c 20 3e 20 70 50 61 72 73 65 2d ultCol > pParse-
60a0: 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 >nMem ){. /*
60b0: 54 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f 72 This is an error
60c0: 20 63 6f 6e 64 69 74 69 6f 6e 20 74 68 61 74 20 condition that
60d0: 63 61 6e 20 72 65 73 75 6c 74 2c 20 66 6f 72 20 can result, for
60e0: 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 61 20 example, when a
60f0: 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 6f 6e SELECT. ** on
6100: 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 the right-hand
6110: 73 69 64 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 side of an INSER
6120: 54 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 T contains more
6130: 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 74 result columns t
6140: 68 61 6e 0a 20 20 20 20 2a 2a 20 74 68 65 72 65 han. ** there
6150: 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 are columns in
6160: 74 68 65 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 the table on the
6170: 20 6c 65 66 74 2e 20 20 54 68 65 20 65 72 72 6f left. The erro
6180: 72 20 77 69 6c 6c 20 62 65 20 63 61 75 67 68 74 r will be caught
6190: 0a 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 70 6f . ** and repo
61a0: 72 74 65 64 20 6c 61 74 65 72 2e 20 20 42 75 74 rted later. But
61b0: 20 77 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 we need to make
61c0: 20 73 75 72 65 20 65 6e 6f 75 67 68 20 6d 65 6d sure enough mem
61d0: 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 ory is allocated
61e0: 0a 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 . ** to avoid
61f0: 20 6f 74 68 65 72 20 73 70 75 72 69 6f 75 73 20 other spurious
6200: 65 72 72 6f 72 73 20 69 6e 20 74 68 65 20 6d 65 errors in the me
6210: 61 6e 74 69 6d 65 2e 20 2a 2f 0a 20 20 20 20 70 antime. */. p
6220: 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e Parse->nMem += n
6230: 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20 7d 0a 20 ResultCol;. }.
6240: 20 70 44 65 73 74 2d 3e 6e 53 64 73 74 20 3d 20 pDest->nSdst =
6250: 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20 20 72 65 nResultCol;. re
6260: 67 52 65 73 75 6c 74 20 3d 20 70 44 65 73 74 2d gResult = pDest-
6270: 3e 69 53 64 73 74 3b 0a 20 20 69 66 28 20 73 72 >iSdst;. if( sr
6280: 63 54 61 62 3e 3d 30 20 29 7b 0a 20 20 20 20 66 cTab>=0 ){. f
6290: 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 73 75 6c or(i=0; i<nResul
62a0: 74 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 tCol; i++){.
62b0: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
62c0: 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e Op3(v, OP_Column
62d0: 2c 20 73 72 63 54 61 62 2c 20 69 2c 20 72 65 67 , srcTab, i, reg
62e0: 52 65 73 75 6c 74 2b 69 29 3b 0a 20 20 20 20 20 Result+i);.
62f0: 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
6300: 20 22 25 73 22 2c 20 70 45 4c 69 73 74 2d 3e 61 "%s", pEList->a
6310: 5b 69 5d 2e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 [i].zName));.
6320: 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65 }. }else if( e
6330: 44 65 73 74 21 3d 53 52 54 5f 45 78 69 73 74 73 Dest!=SRT_Exists
6340: 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 ){. /* If th
6350: 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 e destination is
6360: 20 61 6e 20 45 58 49 53 54 53 28 2e 2e 2e 29 20 an EXISTS(...)
6370: 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 20 expression, the
6380: 61 63 74 75 61 6c 0a 20 20 20 20 2a 2a 20 76 61 actual. ** va
6390: 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 lues returned by
63a0: 20 74 68 65 20 53 45 4c 45 43 54 20 61 72 65 20 the SELECT are
63b0: 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a 20 20 not required..
63c0: 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 */. sqlite3
63d0: 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 ExprCodeExprList
63e0: 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c (pParse, pEList,
63f0: 20 72 65 67 52 65 73 75 6c 74 2c 0a 20 20 20 20 regResult,.
6400: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65 (e
6410: 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 Dest==SRT_Output
6420: 7c 7c 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 ||eDest==SRT_Cor
6430: 6f 75 74 69 6e 65 29 3f 53 51 4c 49 54 45 5f 45 outine)?SQLITE_E
6440: 43 45 4c 5f 44 55 50 3a 30 29 3b 0a 20 20 7d 0a CEL_DUP:0);. }.
6450: 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 44 49 53 . /* If the DIS
6460: 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 77 61 TINCT keyword wa
6470: 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 74 68 65 s present on the
6480: 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
6490: 74 0a 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20 t. ** and this
64a0: 72 6f 77 20 68 61 73 20 62 65 65 6e 20 73 65 65 row has been see
64b0: 6e 20 62 65 66 6f 72 65 2c 20 74 68 65 6e 20 64 n before, then d
64c0: 6f 20 6e 6f 74 20 6d 61 6b 65 20 74 68 69 73 20 o not make this
64d0: 72 6f 77 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66 row. ** part of
64e0: 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 20 20 2a the result.. *
64f0: 2f 0a 20 20 69 66 28 20 68 61 73 44 69 73 74 69 /. if( hasDisti
6500: 6e 63 74 20 29 7b 0a 20 20 20 20 73 77 69 74 63 nct ){. switc
6510: 68 28 20 70 44 69 73 74 69 6e 63 74 2d 3e 65 54 h( pDistinct->eT
6520: 6e 63 74 54 79 70 65 20 29 7b 0a 20 20 20 20 20 nctType ){.
6530: 20 63 61 73 65 20 57 48 45 52 45 5f 44 49 53 54 case WHERE_DIST
6540: 49 4e 43 54 5f 4f 52 44 45 52 45 44 3a 20 7b 0a INCT_ORDERED: {.
6550: 20 20 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a VdbeOp *
6560: 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 pOp;
6570: 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 72 65 71 /* No longer req
6580: 75 69 72 65 64 20 4f 70 65 6e 45 70 68 65 6d 65 uired OpenEpheme
6590: 72 61 6c 20 69 6e 73 74 72 2e 20 2a 2f 0a 20 20 ral instr. */.
65a0: 20 20 20 20 20 20 69 6e 74 20 69 4a 75 6d 70 3b int iJump;
65b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
65c0: 20 4a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f Jump destinatio
65d0: 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 n */. int
65e0: 20 72 65 67 50 72 65 76 3b 20 20 20 20 20 20 20 regPrev;
65f0: 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 /* Previous
6600: 20 72 6f 77 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a row content */.
6610: 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f . /* Allo
6620: 63 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 74 cate space for t
6630: 68 65 20 70 72 65 76 69 6f 75 73 20 72 6f 77 20 he previous row
6640: 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 67 50 72 */. regPr
6650: 65 76 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 ev = pParse->nMe
6660: 6d 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 50 61 m+1;. pPa
6670: 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 rse->nMem += nRe
6680: 73 75 6c 74 43 6f 6c 3b 0a 0a 20 20 20 20 20 20 sultCol;..
6690: 20 20 2f 2a 20 43 68 61 6e 67 65 20 74 68 65 20 /* Change the
66a0: 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
66b0: 20 63 6f 64 65 64 20 65 61 72 6c 69 65 72 20 74 coded earlier t
66c0: 6f 20 61 6e 20 4f 50 5f 4e 75 6c 6c 0a 20 20 20 o an OP_Null.
66d0: 20 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 ** sets the
66e0: 20 4d 45 4d 5f 43 6c 65 61 72 65 64 20 62 69 74 MEM_Cleared bit
66f0: 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 on the first re
6700: 67 69 73 74 65 72 20 6f 66 20 74 68 65 0a 20 20 gister of the.
6710: 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 ** previou
6720: 73 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 77 s value. This w
6730: 69 6c 6c 20 63 61 75 73 65 20 74 68 65 20 4f 50 ill cause the OP
6740: 5f 4e 65 20 62 65 6c 6f 77 20 74 6f 20 61 6c 77 _Ne below to alw
6750: 61 79 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 ays. ** f
6760: 61 69 6c 20 6f 6e 20 74 68 65 20 66 69 72 73 74 ail on the first
6770: 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 iteration of th
6780: 65 20 6c 6f 6f 70 20 65 76 65 6e 20 69 66 20 74 e loop even if t
6790: 68 65 20 66 69 72 73 74 0a 20 20 20 20 20 20 20 he first.
67a0: 20 2a 2a 20 72 6f 77 20 69 73 20 61 6c 6c 20 4e ** row is all N
67b0: 55 4c 4c 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f ULLs.. */
67c0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
67d0: 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 VdbeChangeToNoop
67e0: 28 76 2c 20 70 44 69 73 74 69 6e 63 74 2d 3e 61 (v, pDistinct->a
67f0: 64 64 72 54 6e 63 74 29 3b 0a 20 20 20 20 20 20 ddrTnct);.
6800: 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 pOp = sqlite3V
6810: 64 62 65 47 65 74 4f 70 28 76 2c 20 70 44 69 73 dbeGetOp(v, pDis
6820: 74 69 6e 63 74 2d 3e 61 64 64 72 54 6e 63 74 29 tinct->addrTnct)
6830: 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f ;. pOp->o
6840: 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 75 6c 6c 3b pcode = OP_Null;
6850: 0a 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 31 . pOp->p1
6860: 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 4f = 1;. pO
6870: 70 2d 3e 70 32 20 3d 20 72 65 67 50 72 65 76 3b p->p2 = regPrev;
6880: 0a 0a 20 20 20 20 20 20 20 20 69 4a 75 6d 70 20 .. iJump
6890: 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
68a0: 72 65 6e 74 41 64 64 72 28 76 29 20 2b 20 6e 52 rentAddr(v) + nR
68b0: 65 73 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 20 20 esultCol;.
68c0: 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 for(i=0; i<nRe
68d0: 73 75 6c 74 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 sultCol; i++){.
68e0: 20 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 CollSeq
68f0: 20 2a 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 *pColl = sqlite
6900: 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 3ExprCollSeq(pPa
6910: 72 73 65 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 rse, pEList->a[i
6920: 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 ].pExpr);.
6930: 20 20 20 20 69 66 28 20 69 3c 6e 52 65 73 75 6c if( i<nResul
6940: 74 43 6f 6c 2d 31 20 29 7b 0a 20 20 20 20 20 20 tCol-1 ){.
6950: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
6960: 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 eAddOp3(v, OP_Ne
6970: 2c 20 72 65 67 52 65 73 75 6c 74 2b 69 2c 20 69 , regResult+i, i
6980: 4a 75 6d 70 2c 20 72 65 67 50 72 65 76 2b 69 29 Jump, regPrev+i)
6990: 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 56 64 ;. Vd
69a0: 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 beCoverage(v);.
69b0: 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
69c0: 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
69d0: 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
69e0: 20 4f 50 5f 45 71 2c 20 72 65 67 52 65 73 75 6c OP_Eq, regResul
69f0: 74 2b 69 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 t+i, iContinue,
6a00: 72 65 67 50 72 65 76 2b 69 29 3b 0a 20 20 20 20 regPrev+i);.
6a10: 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 76 65 VdbeCove
6a20: 72 61 67 65 28 76 29 3b 0a 20 20 20 20 20 20 20 rage(v);.
6a30: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 }.
6a40: 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
6a50: 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 eP4(v, -1, (cons
6a60: 74 20 63 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 t char *)pColl,
6a70: 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 P4_COLLSEQ);.
6a80: 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
6a90: 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 beChangeP5(v, SQ
6aa0: 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20 LITE_NULLEQ);.
6ab0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
6ac0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 56 assert( sqlite3V
6ad0: 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 dbeCurrentAddr(v
6ae0: 29 3d 3d 69 4a 75 6d 70 20 7c 7c 20 70 50 61 72 )==iJump || pPar
6af0: 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 se->db->mallocFa
6b00: 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 iled );.
6b10: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
6b20: 33 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 3(v, OP_Copy, re
6b30: 67 52 65 73 75 6c 74 2c 20 72 65 67 50 72 65 76 gResult, regPrev
6b40: 2c 20 6e 52 65 73 75 6c 74 43 6f 6c 2d 31 29 3b , nResultCol-1);
6b50: 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a . break;.
6b60: 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 63 }.. c
6b70: 61 73 65 20 57 48 45 52 45 5f 44 49 53 54 49 4e ase WHERE_DISTIN
6b80: 43 54 5f 55 4e 49 51 55 45 3a 20 7b 0a 20 20 20 CT_UNIQUE: {.
6b90: 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
6ba0: 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 ChangeToNoop(v,
6bb0: 70 44 69 73 74 69 6e 63 74 2d 3e 61 64 64 72 54 pDistinct->addrT
6bc0: 6e 63 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 nct);. br
6bd0: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 eak;. }..
6be0: 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 default: {.
6bf0: 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 assert( p
6c00: 44 69 73 74 69 6e 63 74 2d 3e 65 54 6e 63 74 54 Distinct->eTnctT
6c10: 79 70 65 3d 3d 57 48 45 52 45 5f 44 49 53 54 49 ype==WHERE_DISTI
6c20: 4e 43 54 5f 55 4e 4f 52 44 45 52 45 44 20 29 3b NCT_UNORDERED );
6c30: 0a 20 20 20 20 20 20 20 20 63 6f 64 65 44 69 73 . codeDis
6c40: 74 69 6e 63 74 28 70 50 61 72 73 65 2c 20 70 44 tinct(pParse, pD
6c50: 69 73 74 69 6e 63 74 2d 3e 74 61 62 54 6e 63 74 istinct->tabTnct
6c60: 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e 52 65 , iContinue, nRe
6c70: 73 75 6c 74 43 6f 6c 2c 20 72 65 67 52 65 73 75 sultCol, regResu
6c80: 6c 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 lt);. bre
6c90: 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 ak;. }.
6ca0: 7d 0a 20 20 20 20 69 66 28 20 70 53 6f 72 74 3d }. if( pSort=
6cb0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65 =0 ){. code
6cc0: 4f 66 66 73 65 74 28 76 2c 20 70 2d 3e 69 4f 66 Offset(v, p->iOf
6cd0: 66 73 65 74 2c 20 69 43 6f 6e 74 69 6e 75 65 29 fset, iContinue)
6ce0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 ;. }. }.. s
6cf0: 77 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a witch( eDest ){.
6d00: 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 6d /* In this m
6d10: 6f 64 65 2c 20 77 72 69 74 65 20 65 61 63 68 20 ode, write each
6d20: 71 75 65 72 79 20 72 65 73 75 6c 74 20 74 6f 20 query result to
6d30: 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 the key of the t
6d40: 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20 emporary. **
6d50: 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 table iParm..
6d60: 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
6d70: 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 TE_OMIT_COMPOUND
6d80: 5f 53 45 4c 45 43 54 0a 20 20 20 20 63 61 73 65 _SELECT. case
6d90: 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 SRT_Union: {.
6da0: 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 int r1;.
6db0: 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 r1 = sqlite3Ge
6dc0: 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
6dd0: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
6de0: 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
6df0: 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 MakeRecord, regR
6e00: 65 73 75 6c 74 2c 20 6e 52 65 73 75 6c 74 43 6f esult, nResultCo
6e10: 6c 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 l, r1);. sq
6e20: 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
6e30: 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c v, OP_IdxInsert,
6e40: 20 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 iParm, r1);.
6e50: 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 sqlite3Releas
6e60: 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c eTempReg(pParse,
6e70: 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61 r1);. brea
6e80: 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a k;. }.. /*
6e90: 20 43 6f 6e 73 74 72 75 63 74 20 61 20 72 65 63 Construct a rec
6ea0: 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 71 75 65 ord from the que
6eb0: 72 79 20 72 65 73 75 6c 74 2c 20 62 75 74 20 69 ry result, but i
6ec0: 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a nstead of. **
6ed0: 20 73 61 76 69 6e 67 20 74 68 61 74 20 72 65 63 saving that rec
6ee0: 6f 72 64 2c 20 75 73 65 20 69 74 20 61 73 20 61 ord, use it as a
6ef0: 20 6b 65 79 20 74 6f 20 64 65 6c 65 74 65 20 65 key to delete e
6f00: 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20 lements from.
6f10: 20 2a 2a 20 74 68 65 20 74 65 6d 70 6f 72 61 72 ** the temporar
6f20: 79 20 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 y table iParm..
6f30: 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 */. case S
6f40: 52 54 5f 45 78 63 65 70 74 3a 20 7b 0a 20 20 20 RT_Except: {.
6f50: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
6f60: 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64 78 44 65 dOp3(v, OP_IdxDe
6f70: 6c 65 74 65 2c 20 69 50 61 72 6d 2c 20 72 65 67 lete, iParm, reg
6f80: 52 65 73 75 6c 74 2c 20 6e 52 65 73 75 6c 74 43 Result, nResultC
6f90: 6f 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b ol);. break
6fa0: 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f ;. }.#endif /
6fb0: 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f * SQLITE_OMIT_CO
6fc0: 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f MPOUND_SELECT */
6fd0: 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 .. /* Store t
6fe0: 68 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 he result as dat
6ff0: 61 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65 a using a unique
7000: 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 key.. */.
7010: 20 63 61 73 65 20 53 52 54 5f 46 69 66 6f 3a 0a case SRT_Fifo:.
7020: 20 20 20 20 63 61 73 65 20 53 52 54 5f 44 69 73 case SRT_Dis
7030: 74 46 69 66 6f 3a 0a 20 20 20 20 63 61 73 65 20 tFifo:. case
7040: 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 SRT_Table:. c
7050: 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 ase SRT_EphemTab
7060: 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 : {. int r1
7070: 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d = sqlite3GetTem
7080: 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e pRange(pParse, n
7090: 50 72 65 66 69 78 52 65 67 2b 31 29 3b 0a 20 20 PrefixReg+1);.
70a0: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 testcase( eD
70b0: 65 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 est==SRT_Table )
70c0: 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
70d0: 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 ( eDest==SRT_Eph
70e0: 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 emTab );. s
70f0: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 qlite3VdbeAddOp3
7100: 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 (v, OP_MakeRecor
7110: 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 52 d, regResult, nR
7120: 65 73 75 6c 74 43 6f 6c 2c 20 72 31 2b 6e 50 72 esultCol, r1+nPr
7130: 65 66 69 78 52 65 67 29 3b 0a 23 69 66 6e 64 65 efixReg);.#ifnde
7140: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 54 f SQLITE_OMIT_CT
7150: 45 0a 20 20 20 20 20 20 69 66 28 20 65 44 65 73 E. if( eDes
7160: 74 3d 3d 53 52 54 5f 44 69 73 74 46 69 66 6f 20 t==SRT_DistFifo
7170: 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 ){. /* If
7180: 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e the destination
7190: 20 69 73 20 44 69 73 74 46 69 66 6f 2c 20 74 68 is DistFifo, th
71a0: 65 6e 20 63 75 72 73 6f 72 20 28 69 50 61 72 6d en cursor (iParm
71b0: 2b 31 29 20 69 73 20 6f 70 65 6e 0a 20 20 20 20 +1) is open.
71c0: 20 20 20 20 2a 2a 20 6f 6e 20 61 6e 20 65 70 68 ** on an eph
71d0: 65 6d 65 72 61 6c 20 69 6e 64 65 78 2e 20 49 66 emeral index. If
71e0: 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 the current row
71f0: 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 65 73 is already pres
7200: 65 6e 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 ent. ** i
7210: 6e 20 74 68 65 20 69 6e 64 65 78 2c 20 64 6f 20 n the index, do
7220: 6e 6f 74 20 77 72 69 74 65 20 69 74 20 74 6f 20 not write it to
7230: 74 68 65 20 6f 75 74 70 75 74 2e 20 49 66 20 6e the output. If n
7240: 6f 74 2c 20 61 64 64 20 74 68 65 0a 20 20 20 20 ot, add the.
7250: 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 72 ** current r
7260: 6f 77 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 ow to the index
7270: 61 6e 64 20 70 72 6f 63 65 65 64 20 77 69 74 68 and proceed with
7280: 20 77 72 69 74 69 6e 67 20 69 74 20 74 6f 20 74 writing it to t
7290: 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 75 he. ** ou
72a0: 74 70 75 74 20 74 61 62 6c 65 20 61 73 20 77 65 tput table as we
72b0: 6c 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 ll. */.
72c0: 69 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 int addr = sqlit
72d0: 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 e3VdbeCurrentAdd
72e0: 72 28 76 29 20 2b 20 34 3b 0a 20 20 20 20 20 20 r(v) + 4;.
72f0: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
7300: 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 Op4Int(v, OP_Fou
7310: 6e 64 2c 20 69 50 61 72 6d 2b 31 2c 20 61 64 64 nd, iParm+1, add
7320: 72 2c 20 72 31 2c 20 30 29 3b 20 56 64 62 65 43 r, r1, 0); VdbeC
7330: 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 overage(v);.
7340: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
7350: 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 ddOp2(v, OP_IdxI
7360: 6e 73 65 72 74 2c 20 69 50 61 72 6d 2b 31 2c 20 nsert, iParm+1,
7370: 72 31 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 r1);. ass
7380: 65 72 74 28 20 70 53 6f 72 74 3d 3d 30 20 29 3b ert( pSort==0 );
7390: 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a . }.#endif.
73a0: 20 20 20 20 20 20 69 66 28 20 70 53 6f 72 74 20 if( pSort
73b0: 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f ){. pushO
73c0: 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65 ntoSorter(pParse
73d0: 2c 20 70 53 6f 72 74 2c 20 70 2c 20 72 31 2b 6e , pSort, p, r1+n
73e0: 50 72 65 66 69 78 52 65 67 2c 20 31 2c 20 6e 50 PrefixReg, 1, nP
73f0: 72 65 66 69 78 52 65 67 29 3b 0a 20 20 20 20 20 refixReg);.
7400: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
7410: 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 int r2 = sqlite3
7420: 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
7430: 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 e);. sqli
7440: 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
7450: 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 OP_NewRowid, iP
7460: 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 arm, r2);.
7470: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
7480: 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 Op3(v, OP_Insert
7490: 2c 20 69 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 , iParm, r1, r2)
74a0: 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
74b0: 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 3VdbeChangeP5(v,
74c0: 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b OPFLAG_APPEND);
74d0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
74e0: 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 ReleaseTempReg(p
74f0: 50 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 Parse, r2);.
7500: 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 }. sqlite
7510: 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 3ReleaseTempRang
7520: 65 28 70 50 61 72 73 65 2c 20 72 31 2c 20 6e 50 e(pParse, r1, nP
7530: 72 65 66 69 78 52 65 67 2b 31 29 3b 0a 20 20 20 refixReg+1);.
7540: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
7550: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
7560: 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 OMIT_SUBQUERY.
7570: 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 /* If we are c
7580: 72 65 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f reating a set fo
7590: 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 r an "expr IN (S
75a0: 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 ELECT ...)" cons
75b0: 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 truct,. ** th
75c0: 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20 en there should
75d0: 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d be a single item
75e0: 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 on the stack.
75f0: 57 72 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a Write this. *
7600: 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 * item into the
7610: 73 65 74 20 74 61 62 6c 65 20 77 69 74 68 20 62 set table with b
7620: 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a ogus data.. *
7630: 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 /. case SRT_S
7640: 65 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 et: {. asse
7650: 72 74 28 20 6e 52 65 73 75 6c 74 43 6f 6c 3d 3d rt( nResultCol==
7660: 31 20 29 3b 0a 20 20 20 20 20 20 70 44 65 73 74 1 );. pDest
7670: 2d 3e 61 66 66 53 64 73 74 20 3d 0a 20 20 20 20 ->affSdst =.
7680: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 sq
7690: 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 lite3CompareAffi
76a0: 6e 69 74 79 28 70 45 4c 69 73 74 2d 3e 61 5b 30 nity(pEList->a[0
76b0: 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d 3e ].pExpr, pDest->
76c0: 61 66 66 53 64 73 74 29 3b 0a 20 20 20 20 20 20 affSdst);.
76d0: 69 66 28 20 70 53 6f 72 74 20 29 7b 0a 20 20 20 if( pSort ){.
76e0: 20 20 20 20 20 2f 2a 20 41 74 20 66 69 72 73 74 /* At first
76f0: 20 67 6c 61 6e 63 65 20 79 6f 75 20 77 6f 75 6c glance you woul
7700: 64 20 74 68 69 6e 6b 20 77 65 20 63 6f 75 6c 64 d think we could
7710: 20 6f 70 74 69 6d 69 7a 65 20 6f 75 74 20 74 68 optimize out th
7720: 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52 44 e. ** ORD
7730: 45 52 20 42 59 20 69 6e 20 74 68 69 73 20 63 61 ER BY in this ca
7740: 73 65 20 73 69 6e 63 65 20 74 68 65 20 6f 72 64 se since the ord
7750: 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e er of entries in
7760: 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 20 the set.
7770: 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 ** does not mat
7780: 74 65 72 2e 20 20 42 75 74 20 74 68 65 72 65 20 ter. But there
7790: 6d 69 67 68 74 20 62 65 20 61 20 4c 49 4d 49 54 might be a LIMIT
77a0: 20 63 6c 61 75 73 65 2c 20 69 6e 20 77 68 69 63 clause, in whic
77b0: 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 h. ** cas
77c0: 65 20 74 68 65 20 6f 72 64 65 72 20 64 6f 65 73 e the order does
77d0: 20 6d 61 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 matter */.
77e0: 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 pushOntoSorte
77f0: 72 28 70 50 61 72 73 65 2c 20 70 53 6f 72 74 2c r(pParse, pSort,
7800: 20 70 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 p, regResult, 1
7810: 2c 20 6e 50 72 65 66 69 78 52 65 67 29 3b 0a 20 , nPrefixReg);.
7820: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
7830: 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c int r1 = sql
7840: 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
7850: 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 Parse);.
7860: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
7870: 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 4(v, OP_MakeReco
7880: 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 31 2c rd, regResult,1,
7890: 72 31 2c 20 26 70 44 65 73 74 2d 3e 61 66 66 53 r1, &pDest->affS
78a0: 64 73 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 dst, 1);.
78b0: 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 sqlite3ExprCach
78c0: 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 eAffinityChange(
78d0: 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c pParse, regResul
78e0: 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 t, 1);. s
78f0: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
7900: 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 (v, OP_IdxInsert
7910: 2c 20 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 , iParm, r1);.
7920: 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c sqlite3Rel
7930: 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 easeTempReg(pPar
7940: 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d se, r1);. }
7950: 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 . break;.
7960: 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 }.. /* If a
7970: 6e 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 ny row exist in
7980: 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 the result set,
7990: 72 65 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 record that fact
79a0: 20 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 and abort..
79b0: 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f */. case SRT_
79c0: 45 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 Exists: {.
79d0: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
79e0: 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
79f0: 20 31 2c 20 69 50 61 72 6d 29 3b 0a 20 20 20 20 1, iParm);.
7a00: 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 /* The LIMIT c
7a10: 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 lause will termi
7a20: 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f nate the loop fo
7a30: 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 r us */. br
7a40: 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 eak;. }..
7a50: 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 /* If this is a
7a60: 73 63 61 6c 61 72 20 73 65 6c 65 63 74 20 74 68 scalar select th
7a70: 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e at is part of an
7a80: 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 expression, the
7a90: 6e 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 n. ** store t
7aa0: 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 he results in th
7ab0: 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 6d 65 e appropriate me
7ac0: 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 mory cell and br
7ad0: 65 61 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f eak out. ** o
7ae0: 66 20 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e f the scan loop.
7af0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 . */. case
7b00: 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 SRT_Mem: {.
7b10: 20 20 61 73 73 65 72 74 28 20 6e 52 65 73 75 6c assert( nResul
7b20: 74 43 6f 6c 3d 3d 31 20 29 3b 0a 20 20 20 20 20 tCol==1 );.
7b30: 20 69 66 28 20 70 53 6f 72 74 20 29 7b 0a 20 20 if( pSort ){.
7b40: 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f pushOntoSo
7b50: 72 74 65 72 28 70 50 61 72 73 65 2c 20 70 53 6f rter(pParse, pSo
7b60: 72 74 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74 rt, p, regResult
7b70: 2c 20 31 2c 20 6e 50 72 65 66 69 78 52 65 67 29 , 1, nPrefixReg)
7b80: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
7b90: 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 assert( r
7ba0: 65 67 52 65 73 75 6c 74 3d 3d 69 50 61 72 6d 20 egResult==iParm
7bb0: 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 );. /* Th
7bc0: 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 e LIMIT clause w
7bd0: 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66 20 ill jump out of
7be0: 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 the loop for us
7bf0: 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 */. }.
7c00: 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 break;. }.#e
7c10: 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 ndif /* #ifndef
7c20: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
7c30: 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20 63 61 73 UERY */.. cas
7c40: 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a e SRT_Coroutine:
7c50: 20 20 20 20 20 20 20 2f 2a 20 53 65 6e 64 20 64 /* Send d
7c60: 61 74 61 20 74 6f 20 61 20 63 6f 2d 72 6f 75 74 ata to a co-rout
7c70: 69 6e 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 ine */. case
7c80: 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 20 20 20 SRT_Output: {
7c90: 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 /* Return t
7ca0: 68 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20 he results */.
7cb0: 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 testcase( eD
7cc0: 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 est==SRT_Corouti
7cd0: 6e 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 ne );. test
7ce0: 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 case( eDest==SRT
7cf0: 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20 20 _Output );.
7d00: 20 69 66 28 20 70 53 6f 72 74 20 29 7b 0a 20 20 if( pSort ){.
7d10: 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f pushOntoSo
7d20: 72 74 65 72 28 70 50 61 72 73 65 2c 20 70 53 6f rter(pParse, pSo
7d30: 72 74 2c 20 70 2c 20 72 65 67 52 65 73 75 6c 74 rt, p, regResult
7d40: 2c 20 6e 52 65 73 75 6c 74 43 6f 6c 2c 20 6e 50 , nResultCol, nP
7d50: 72 65 66 69 78 52 65 67 29 3b 0a 20 20 20 20 20 refixReg);.
7d60: 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73 74 }else if( eDest
7d70: 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 ==SRT_Coroutine
7d80: 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
7d90: 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
7da0: 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d OP_Yield, pDest-
7db0: 3e 69 53 44 50 61 72 6d 29 3b 0a 20 20 20 20 20 >iSDParm);.
7dc0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
7dd0: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
7de0: 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 2(v, OP_ResultRo
7df0: 77 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 52 w, regResult, nR
7e00: 65 73 75 6c 74 43 6f 6c 29 3b 0a 20 20 20 20 20 esultCol);.
7e10: 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
7e20: 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
7e30: 65 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 e(pParse, regRes
7e40: 75 6c 74 2c 20 6e 52 65 73 75 6c 74 43 6f 6c 29 ult, nResultCol)
7e50: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
7e60: 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 break;. }..#i
7e70: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
7e80: 54 5f 43 54 45 0a 20 20 20 20 2f 2a 20 57 72 69 T_CTE. /* Wri
7e90: 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 te the results i
7ea0: 6e 74 6f 20 61 20 70 72 69 6f 72 69 74 79 20 71 nto a priority q
7eb0: 75 65 75 65 20 74 68 61 74 20 69 73 20 6f 72 64 ueue that is ord
7ec0: 65 72 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 0a er according to.
7ed0: 20 20 20 20 2a 2a 20 70 44 65 73 74 2d 3e 70 4f ** pDest->pO
7ee0: 72 64 65 72 42 79 20 28 69 6e 20 70 53 4f 29 2e rderBy (in pSO).
7ef0: 20 20 70 44 65 73 74 2d 3e 69 53 44 50 61 72 6d pDest->iSDParm
7f00: 20 28 69 6e 20 69 50 61 72 6d 29 20 69 73 20 74 (in iParm) is t
7f10: 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20 61 6e he cursor for an
7f20: 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 77 69 . ** index wi
7f30: 74 68 20 70 53 4f 2d 3e 6e 45 78 70 72 2b 32 20 th pSO->nExpr+2
7f40: 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 69 6c 64 20 columns. Build
7f50: 61 20 6b 65 79 20 75 73 69 6e 67 20 70 53 4f 20 a key using pSO
7f60: 66 6f 72 20 74 68 65 20 66 69 72 73 74 0a 20 20 for the first.
7f70: 20 20 2a 2a 20 70 53 4f 2d 3e 6e 45 78 70 72 20 ** pSO->nExpr
7f80: 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6d 61 columns, then ma
7f90: 6b 65 20 73 75 72 65 20 61 6c 6c 20 6b 65 79 73 ke sure all keys
7fa0: 20 61 72 65 20 75 6e 69 71 75 65 20 62 79 20 61 are unique by a
7fb0: 64 64 69 6e 67 20 61 0a 20 20 20 20 2a 2a 20 66 dding a. ** f
7fc0: 69 6e 61 6c 20 4f 50 5f 53 65 71 75 65 6e 63 65 inal OP_Sequence
7fd0: 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 6c 61 column. The la
7fe0: 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 st column is the
7ff0: 20 72 65 63 6f 72 64 20 61 73 20 61 20 62 6c 6f record as a blo
8000: 62 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 b.. */. ca
8010: 73 65 20 53 52 54 5f 44 69 73 74 51 75 65 75 65 se SRT_DistQueue
8020: 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 51 :. case SRT_Q
8030: 75 65 75 65 3a 20 7b 0a 20 20 20 20 20 20 69 6e ueue: {. in
8040: 74 20 6e 4b 65 79 3b 0a 20 20 20 20 20 20 69 6e t nKey;. in
8050: 74 20 72 31 2c 20 72 32 2c 20 72 33 3b 0a 20 20 t r1, r2, r3;.
8060: 20 20 20 20 69 6e 74 20 61 64 64 72 54 65 73 74 int addrTest
8070: 20 3d 20 30 3b 0a 20 20 20 20 20 20 45 78 70 72 = 0;. Expr
8080: 4c 69 73 74 20 2a 70 53 4f 3b 0a 20 20 20 20 20 List *pSO;.
8090: 20 70 53 4f 20 3d 20 70 44 65 73 74 2d 3e 70 4f pSO = pDest->pO
80a0: 72 64 65 72 42 79 3b 0a 20 20 20 20 20 20 61 73 rderBy;. as
80b0: 73 65 72 74 28 20 70 53 4f 20 29 3b 0a 20 20 20 sert( pSO );.
80c0: 20 20 20 6e 4b 65 79 20 3d 20 70 53 4f 2d 3e 6e nKey = pSO->n
80d0: 45 78 70 72 3b 0a 20 20 20 20 20 20 72 31 20 3d Expr;. r1 =
80e0: 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
80f0: 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 eg(pParse);.
8100: 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 r2 = sqlite3Ge
8110: 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 tTempRange(pPars
8120: 65 2c 20 6e 4b 65 79 2b 32 29 3b 0a 20 20 20 20 e, nKey+2);.
8130: 20 20 72 33 20 3d 20 72 32 2b 6e 4b 65 79 2b 31 r3 = r2+nKey+1
8140: 3b 0a 20 20 20 20 20 20 69 66 28 20 65 44 65 73 ;. if( eDes
8150: 74 3d 3d 53 52 54 5f 44 69 73 74 51 75 65 75 65 t==SRT_DistQueue
8160: 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 ){. /* I
8170: 66 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f f the destinatio
8180: 6e 20 69 73 20 44 69 73 74 51 75 65 75 65 2c 20 n is DistQueue,
8190: 74 68 65 6e 20 63 75 72 73 6f 72 20 28 69 50 61 then cursor (iPa
81a0: 72 6d 2b 31 29 20 69 73 20 6f 70 65 6e 0a 20 20 rm+1) is open.
81b0: 20 20 20 20 20 20 2a 2a 20 6f 6e 20 61 20 73 65 ** on a se
81c0: 63 6f 6e 64 20 65 70 68 65 6d 65 72 61 6c 20 69 cond ephemeral i
81d0: 6e 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 ndex that holds
81e0: 61 6c 6c 20 76 61 6c 75 65 73 20 65 76 65 72 79 all values every
81f0: 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20 20 20 previously.
8200: 20 20 20 20 2a 2a 20 61 64 64 65 64 20 74 6f 20 ** added to
8210: 74 68 65 20 71 75 65 75 65 2e 20 2a 2f 0a 20 20 the queue. */.
8220: 20 20 20 20 20 20 61 64 64 72 54 65 73 74 20 3d addrTest =
8230: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
8240: 70 34 49 6e 74 28 76 2c 20 4f 50 5f 46 6f 75 6e p4Int(v, OP_Foun
8250: 64 2c 20 69 50 61 72 6d 2b 31 2c 20 30 2c 20 0a d, iParm+1, 0, .
8260: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8270: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8280: 20 20 20 20 20 20 20 20 72 65 67 52 65 73 75 6c regResul
8290: 74 2c 20 6e 52 65 73 75 6c 74 43 6f 6c 29 3b 0a t, nResultCol);.
82a0: 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 76 65 VdbeCove
82b0: 72 61 67 65 28 76 29 3b 0a 20 20 20 20 20 20 7d rage(v);. }
82c0: 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
82d0: 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d beAddOp3(v, OP_M
82e0: 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 akeRecord, regRe
82f0: 73 75 6c 74 2c 20 6e 52 65 73 75 6c 74 43 6f 6c sult, nResultCol
8300: 2c 20 72 33 29 3b 0a 20 20 20 20 20 20 69 66 28 , r3);. if(
8310: 20 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 74 eDest==SRT_Dist
8320: 51 75 65 75 65 20 29 7b 0a 20 20 20 20 20 20 20 Queue ){.
8330: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
8340: 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 p2(v, OP_IdxInse
8350: 72 74 2c 20 69 50 61 72 6d 2b 31 2c 20 72 33 29 rt, iParm+1, r3)
8360: 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
8370: 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 3VdbeChangeP5(v,
8380: 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 OPFLAG_USESEEKR
8390: 45 53 55 4c 54 29 3b 0a 20 20 20 20 20 20 7d 0a ESULT);. }.
83a0: 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
83b0: 3c 6e 4b 65 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 <nKey; i++){.
83c0: 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
83d0: 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f AddOp2(v, OP_SCo
83e0: 70 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 py,.
83f0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 re
8400: 67 52 65 73 75 6c 74 20 2b 20 70 53 4f 2d 3e 61 gResult + pSO->a
8410: 5b 69 5d 2e 75 2e 78 2e 69 4f 72 64 65 72 42 79 [i].u.x.iOrderBy
8420: 43 6f 6c 20 2d 20 31 2c 0a 20 20 20 20 20 20 20 Col - 1,.
8430: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8440: 20 20 20 72 32 2b 69 29 3b 0a 20 20 20 20 20 20 r2+i);.
8450: 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
8460: 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
8470: 53 65 71 75 65 6e 63 65 2c 20 69 50 61 72 6d 2c Sequence, iParm,
8480: 20 72 32 2b 6e 4b 65 79 29 3b 0a 20 20 20 20 20 r2+nKey);.
8490: 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
84a0: 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 p3(v, OP_MakeRec
84b0: 6f 72 64 2c 20 72 32 2c 20 6e 4b 65 79 2b 32 2c ord, r2, nKey+2,
84c0: 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 r1);. sqli
84d0: 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
84e0: 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 OP_IdxInsert, i
84f0: 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 Parm, r1);.
8500: 20 69 66 28 20 61 64 64 72 54 65 73 74 20 29 20 if( addrTest )
8510: 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 sqlite3VdbeJumpH
8520: 65 72 65 28 76 2c 20 61 64 64 72 54 65 73 74 29 ere(v, addrTest)
8530: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
8540: 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
8550: 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 arse, r1);.
8560: 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
8570: 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c empRange(pParse,
8580: 20 72 32 2c 20 6e 4b 65 79 2b 32 29 3b 0a 20 20 r2, nKey+2);.
8590: 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
85a0: 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 .#endif /* SQLIT
85b0: 45 5f 4f 4d 49 54 5f 43 54 45 20 2a 2f 0a 0a 0a E_OMIT_CTE */...
85c0: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 .#if !defined(SQ
85d0: 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 LITE_OMIT_TRIGGE
85e0: 52 29 0a 20 20 20 20 2f 2a 20 44 69 73 63 61 72 R). /* Discar
85f0: 64 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20 d the results.
8600: 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 This is used for
8610: 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
8620: 74 73 20 69 6e 73 69 64 65 0a 20 20 20 20 2a 2a ts inside. **
8630: 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 54 the body of a T
8640: 52 49 47 47 45 52 2e 20 20 54 68 65 20 70 75 72 RIGGER. The pur
8650: 70 6f 73 65 20 6f 66 20 73 75 63 68 20 73 65 6c pose of such sel
8660: 65 63 74 73 20 69 73 20 74 6f 20 63 61 6c 6c 0a ects is to call.
8670: 20 20 20 20 2a 2a 20 75 73 65 72 2d 64 65 66 69 ** user-defi
8680: 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 ned functions th
8690: 61 74 20 68 61 76 65 20 73 69 64 65 20 65 66 66 at have side eff
86a0: 65 63 74 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 ects. We do not
86b0: 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 61 62 6f care. ** abo
86c0: 75 74 20 74 68 65 20 61 63 74 75 61 6c 20 72 65 ut the actual re
86d0: 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c sults of the sel
86e0: 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ect.. */.
86f0: 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 default: {.
8700: 20 61 73 73 65 72 74 28 20 65 44 65 73 74 3d 3d assert( eDest==
8710: 53 52 54 5f 44 69 73 63 61 72 64 20 29 3b 0a 20 SRT_Discard );.
8720: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
8730: 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 }.#endif. }..
8740: 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 /* Jump to the e
8750: 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 nd of the loop i
8760: 66 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72 f the LIMIT is r
8770: 65 61 63 68 65 64 2e 20 20 45 78 63 65 70 74 2c eached. Except,
8780: 20 69 66 0a 20 20 2a 2a 20 74 68 65 72 65 20 69 if. ** there i
8790: 73 20 61 20 73 6f 72 74 65 72 2c 20 69 6e 20 77 s a sorter, in w
87a0: 68 69 63 68 20 63 61 73 65 20 74 68 65 20 73 6f hich case the so
87b0: 72 74 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 rter has already
87c0: 20 6c 69 6d 69 74 65 64 0a 20 20 2a 2a 20 74 68 limited. ** th
87d0: 65 20 6f 75 74 70 75 74 20 66 6f 72 20 75 73 2e e output for us.
87e0: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 6f 72 . */. if( pSor
87f0: 74 3d 3d 30 20 26 26 20 70 2d 3e 69 4c 69 6d 69 t==0 && p->iLimi
8800: 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 t ){. sqlite3
8810: 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
8820: 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d _IfZero, p->iLim
8830: 69 74 2c 20 69 42 72 65 61 6b 2c 20 2d 31 29 3b it, iBreak, -1);
8840: 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
8850: 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 ;. }.}../*.** A
8860: 6c 6c 6f 63 61 74 65 20 61 20 4b 65 79 49 6e 66 llocate a KeyInf
8870: 6f 20 6f 62 6a 65 63 74 20 73 75 66 66 69 63 69 o object suffici
8880: 65 6e 74 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 ent for an index
8890: 20 6f 66 20 4e 20 6b 65 79 20 63 6f 6c 75 6d 6e of N key column
88a0: 73 20 61 6e 64 0a 2a 2a 20 58 20 65 78 74 72 61 s and.** X extra
88b0: 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2f 0a 4b 65 79 columns..*/.Key
88c0: 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 4b 65 79 Info *sqlite3Key
88d0: 49 6e 66 6f 41 6c 6c 6f 63 28 73 71 6c 69 74 65 InfoAlloc(sqlite
88e0: 33 20 2a 64 62 2c 20 69 6e 74 20 4e 2c 20 69 6e 3 *db, int N, in
88f0: 74 20 58 29 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 t X){. KeyInfo
8900: 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 *p = sqlite3DbMa
8910: 6c 6c 6f 63 5a 65 72 6f 28 30 2c 20 0a 20 20 20 llocZero(0, .
8920: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
8930: 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f 29 20 sizeof(KeyInfo)
8940: 2b 20 28 4e 2b 58 29 2a 28 73 69 7a 65 6f 66 28 + (N+X)*(sizeof(
8950: 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 29 3b 0a 20 CollSeq*)+1));.
8960: 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d if( p ){. p-
8970: 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 >aSortOrder = (u
8980: 38 2a 29 26 70 2d 3e 61 43 6f 6c 6c 5b 4e 2b 58 8*)&p->aColl[N+X
8990: 5d 3b 0a 20 20 20 20 70 2d 3e 6e 46 69 65 6c 64 ];. p->nField
89a0: 20 3d 20 28 75 31 36 29 4e 3b 0a 20 20 20 20 70 = (u16)N;. p
89b0: 2d 3e 6e 58 46 69 65 6c 64 20 3d 20 28 75 31 36 ->nXField = (u16
89c0: 29 58 3b 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d )X;. p->enc =
89d0: 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 2d ENC(db);. p-
89e0: 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 70 2d >db = db;. p-
89f0: 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 7d 65 6c >nRef = 1;. }el
8a00: 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c se{. db->mall
8a10: 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 ocFailed = 1;.
8a20: 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a }. return p;.}.
8a30: 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 ./*.** Deallocat
8a40: 65 20 61 20 4b 65 79 49 6e 66 6f 20 6f 62 6a 65 e a KeyInfo obje
8a50: 63 74 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 ct.*/.void sqlit
8a60: 65 33 4b 65 79 49 6e 66 6f 55 6e 72 65 66 28 4b e3KeyInfoUnref(K
8a70: 65 79 49 6e 66 6f 20 2a 70 29 7b 0a 20 20 69 66 eyInfo *p){. if
8a80: 28 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 ( p ){. asser
8a90: 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a t( p->nRef>0 );.
8aa0: 20 20 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 p->nRef--;.
8ab0: 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d if( p->nRef==
8ac0: 30 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 0 ) sqlite3DbFre
8ad0: 65 28 30 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a e(0, p);. }.}..
8ae0: 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 6e 65 77 /*.** Make a new
8af0: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 pointer to a Ke
8b00: 79 49 6e 66 6f 20 6f 62 6a 65 63 74 0a 2a 2f 0a yInfo object.*/.
8b10: 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 KeyInfo *sqlite3
8b20: 4b 65 79 49 6e 66 6f 52 65 66 28 4b 65 79 49 6e KeyInfoRef(KeyIn
8b30: 66 6f 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 fo *p){. if( p
8b40: 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 ){. assert( p
8b50: 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 ->nRef>0 );.
8b60: 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 p->nRef++;. }.
8b70: 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 23 69 return p;.}..#i
8b80: 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
8b90: 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 G./*.** Return T
8ba0: 52 55 45 20 69 66 20 61 20 4b 65 79 49 6e 66 6f RUE if a KeyInfo
8bb0: 20 6f 62 6a 65 63 74 20 63 61 6e 20 62 65 20 63 object can be c
8bc0: 68 61 6e 67 65 2e 20 20 54 68 65 20 4b 65 79 49 hange. The KeyI
8bd0: 6e 66 6f 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 61 nfo object.** ca
8be0: 6e 20 6f 6e 6c 79 20 62 65 20 63 68 61 6e 67 65 n only be change
8bf0: 64 20 69 66 20 74 68 69 73 20 69 73 20 6a 75 73 d if this is jus
8c00: 74 20 61 20 73 69 6e 67 6c 65 20 72 65 66 65 72 t a single refer
8c10: 65 6e 63 65 20 74 6f 20 74 68 65 20 6f 62 6a 65 ence to the obje
8c20: 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 ct..**.** This r
8c30: 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f outine is used o
8c40: 6e 6c 79 20 69 6e 73 69 64 65 20 6f 66 20 61 73 nly inside of as
8c50: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 sert() statement
8c60: 73 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 s..*/.int sqlite
8c70: 33 4b 65 79 49 6e 66 6f 49 73 57 72 69 74 65 61 3KeyInfoIsWritea
8c80: 62 6c 65 28 4b 65 79 49 6e 66 6f 20 2a 70 29 7b ble(KeyInfo *p){
8c90: 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d return p->nRef=
8ca0: 3d 31 3b 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 =1; }.#endif /*
8cb0: 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a SQLITE_DEBUG */.
8cc0: 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 ./*.** Given an
8cd0: 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c expression list,
8ce0: 20 67 65 6e 65 72 61 74 65 20 61 20 4b 65 79 49 generate a KeyI
8cf0: 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 nfo structure th
8d00: 61 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 at records.** th
8d10: 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 e collating sequ
8d20: 65 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65 78 ence for each ex
8d30: 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 61 74 pression in that
8d40: 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 expression list
8d50: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 ..**.** If the E
8d60: 78 70 72 4c 69 73 74 20 69 73 20 61 6e 20 4f 52 xprList is an OR
8d70: 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 DER BY or GROUP
8d80: 42 59 20 63 6c 61 75 73 65 20 74 68 65 6e 20 74 BY clause then t
8d90: 68 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 he resulting.**
8da0: 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 KeyInfo structur
8db0: 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 e is appropriate
8dc0: 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e for initializin
8dd0: 67 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 g a virtual inde
8de0: 78 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e x to.** implemen
8df0: 74 20 74 68 61 74 20 63 6c 61 75 73 65 2e 20 20 t that clause.
8e00: 49 66 20 74 68 65 20 45 78 70 72 4c 69 73 74 20 If the ExprList
8e10: 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 is the result se
8e20: 74 20 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a 2a t of a SELECT.**
8e30: 20 74 68 65 6e 20 74 68 65 20 4b 65 79 49 6e 66 then the KeyInf
8e40: 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 o structure is a
8e50: 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 ppropriate for i
8e60: 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 nitializing a vi
8e70: 72 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20 74 rtual.** index t
8e80: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44 49 o implement a DI
8e90: 53 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a 0a STINCT test..**.
8ea0: 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 ** Space to hold
8eb0: 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 the KeyInfo str
8ec0: 75 63 74 75 72 65 20 69 73 20 6f 62 74 61 69 6e ucture is obtain
8ed0: 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 ed from malloc.
8ee0: 20 54 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 The calling.**
8ef0: 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 73 70 function is resp
8f00: 6f 6e 73 69 62 6c 65 20 66 6f 72 20 73 65 65 69 onsible for seei
8f10: 6e 67 20 74 68 61 74 20 74 68 69 73 20 73 74 72 ng that this str
8f20: 75 63 74 75 72 65 20 69 73 20 65 76 65 6e 74 75 ucture is eventu
8f30: 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 0a 2a ally.** freed..*
8f40: 2f 0a 73 74 61 74 69 63 20 4b 65 79 49 6e 66 6f /.static KeyInfo
8f50: 20 2a 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 *keyInfoFromExp
8f60: 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a rList(. Parse *
8f70: 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a pParse, /*
8f80: 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 Parsing context
8f90: 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
8fa0: 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 46 6f pList, /* Fo
8fb0: 72 6d 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6f rm the KeyInfo o
8fc0: 62 6a 65 63 74 20 66 72 6f 6d 20 74 68 69 73 20 bject from this
8fd0: 45 78 70 72 4c 69 73 74 20 2a 2f 0a 20 20 69 6e ExprList */. in
8fe0: 74 20 69 53 74 61 72 74 2c 20 20 20 20 20 20 20 t iStart,
8ff0: 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 69 74 68 /* Begin with
9000: 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 6f 66 20 this column of
9010: 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e pList */. int n
9020: 45 78 74 72 61 20 20 20 20 20 20 20 20 20 20 20 Extra
9030: 2f 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e 79 /* Add this many
9040: 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73 20 74 extra columns t
9050: 6f 20 74 68 65 20 65 6e 64 20 2a 2f 0a 29 7b 0a o the end */.){.
9060: 20 20 69 6e 74 20 6e 45 78 70 72 3b 0a 20 20 4b int nExpr;. K
9070: 65 79 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 eyInfo *pInfo;.
9080: 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
9090: 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 _item *pItem;.
90a0: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
90b0: 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
90c0: 69 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70 4c i;.. nExpr = pL
90d0: 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 ist->nExpr;. pI
90e0: 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4b 65 79 nfo = sqlite3Key
90f0: 49 6e 66 6f 41 6c 6c 6f 63 28 64 62 2c 20 6e 45 InfoAlloc(db, nE
9100: 78 70 72 2b 6e 45 78 74 72 61 2d 69 53 74 61 72 xpr+nExtra-iStar
9110: 74 2c 20 31 29 3b 0a 20 20 69 66 28 20 70 49 6e t, 1);. if( pIn
9120: 66 6f 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 fo ){. assert
9130: 28 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f ( sqlite3KeyInfo
9140: 49 73 57 72 69 74 65 61 62 6c 65 28 70 49 6e 66 IsWriteable(pInf
9150: 6f 29 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d o) );. for(i=
9160: 69 53 74 61 72 74 2c 20 70 49 74 65 6d 3d 70 4c iStart, pItem=pL
9170: 69 73 74 2d 3e 61 2b 69 53 74 61 72 74 3b 20 69 ist->a+iStart; i
9180: 3c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 <nExpr; i++, pIt
9190: 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c em++){. Col
91a0: 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 lSeq *pColl;.
91b0: 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 pColl = sqlit
91c0: 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 e3ExprCollSeq(pP
91d0: 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 arse, pItem->pEx
91e0: 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 pr);. if( !
91f0: 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 3d 20 pColl ) pColl =
9200: 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 db->pDfltColl;.
9210: 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c pInfo->aCol
9220: 6c 5b 69 2d 69 53 74 61 72 74 5d 20 3d 20 70 43 l[i-iStart] = pC
9230: 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f oll;. pInfo
9240: 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 2d 69 ->aSortOrder[i-i
9250: 53 74 61 72 74 5d 20 3d 20 70 49 74 65 6d 2d 3e Start] = pItem->
9260: 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d sortOrder;. }
9270: 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 49 . }. return pI
9280: 6e 66 6f 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 nfo;.}..#ifndef
9290: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 SQLITE_OMIT_COMP
92a0: 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a OUND_SELECT./*.*
92b0: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f * Name of the co
92c0: 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 72 61 74 6f nnection operato
92d0: 72 2c 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f r, used for erro
92e0: 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2f 0a 73 r messages..*/.s
92f0: 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 tatic const char
9300: 20 2a 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 69 *selectOpName(i
9310: 6e 74 20 69 64 29 7b 0a 20 20 63 68 61 72 20 2a nt id){. char *
9320: 7a 3b 0a 20 20 73 77 69 74 63 68 28 20 69 64 20 z;. switch( id
9330: 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 ){. case TK_A
9340: 4c 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20 22 55 LL: z = "U
9350: 4e 49 4f 4e 20 41 4c 4c 22 3b 20 20 20 62 72 65 NION ALL"; bre
9360: 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ak;. case TK_
9370: 49 4e 54 45 52 53 45 43 54 3a 20 7a 20 3d 20 22 INTERSECT: z = "
9380: 49 4e 54 45 52 53 45 43 54 22 3b 20 20 20 62 72 INTERSECT"; br
9390: 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 54 4b eak;. case TK
93a0: 5f 45 58 43 45 50 54 3a 20 20 20 20 7a 20 3d 20 _EXCEPT: z =
93b0: 22 45 58 43 45 50 54 22 3b 20 20 20 20 20 20 62 "EXCEPT"; b
93c0: 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c reak;. defaul
93d0: 74 3a 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d t: z =
93e0: 20 22 55 4e 49 4f 4e 22 3b 20 20 20 20 20 20 20 "UNION";
93f0: 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 break;. }. ret
9400: 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64 69 66 20 urn z;.}.#endif
9410: 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 /* SQLITE_OMIT_C
9420: 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a OMPOUND_SELECT *
9430: 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 /..#ifndef SQLIT
9440: 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 2f E_OMIT_EXPLAIN./
9450: 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 61 6e 20 22 *.** Unless an "
9460: 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c EXPLAIN QUERY PL
9470: 41 4e 22 20 63 6f 6d 6d 61 6e 64 20 69 73 20 62 AN" command is b
9480: 65 69 6e 67 20 70 72 6f 63 65 73 73 65 64 2c 20 eing processed,
9490: 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a this function.**
94a0: 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 4f 74 68 is a no-op. Oth
94b0: 65 72 77 69 73 65 2c 20 69 74 20 61 64 64 73 20 erwise, it adds
94c0: 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 a single row of
94d0: 6f 75 74 70 75 74 20 74 6f 20 74 68 65 20 45 51 output to the EQ
94e0: 50 20 72 65 73 75 6c 74 2c 0a 2a 2a 20 77 68 65 P result,.** whe
94f0: 72 65 20 74 68 65 20 63 61 70 74 69 6f 6e 20 69 re the caption i
9500: 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a s of the form:.*
9510: 2a 0a 2a 2a 20 20 20 22 55 53 45 20 54 45 4d 50 *.** "USE TEMP
9520: 20 42 2d 54 52 45 45 20 46 4f 52 20 78 78 78 22 B-TREE FOR xxx"
9530: 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 78 78 78 .**.** where xxx
9540: 20 69 73 20 6f 6e 65 20 6f 66 20 22 44 49 53 54 is one of "DIST
9550: 49 4e 43 54 22 2c 20 22 4f 52 44 45 52 20 42 59 INCT", "ORDER BY
9560: 22 20 6f 72 20 22 47 52 4f 55 50 20 42 59 22 2e " or "GROUP BY".
9570: 20 45 78 61 63 74 6c 79 20 77 68 69 63 68 0a 2a Exactly which.*
9580: 2a 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 * is determined
9590: 62 79 20 74 68 65 20 7a 55 73 61 67 65 20 61 72 by the zUsage ar
95a0: 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 gument..*/.stati
95b0: 63 20 76 6f 69 64 20 65 78 70 6c 61 69 6e 54 65 c void explainTe
95c0: 6d 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 mpTable(Parse *p
95d0: 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 Parse, const cha
95e0: 72 20 2a 7a 55 73 61 67 65 29 7b 0a 20 20 69 66 r *zUsage){. if
95f0: 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 ( pParse->explai
9600: 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 56 64 62 65 n==2 ){. Vdbe
9610: 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 *v = pParse->pV
9620: 64 62 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a dbe;. char *z
9630: 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 Msg = sqlite3MPr
9640: 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62 2c intf(pParse->db,
9650: 20 22 55 53 45 20 54 45 4d 50 20 42 2d 54 52 45 "USE TEMP B-TRE
9660: 45 20 46 4f 52 20 25 73 22 2c 20 7a 55 73 61 67 E FOR %s", zUsag
9670: 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 e);. sqlite3V
9680: 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f dbeAddOp4(v, OP_
9690: 45 78 70 6c 61 69 6e 2c 20 70 50 61 72 73 65 2d Explain, pParse-
96a0: 3e 69 53 65 6c 65 63 74 49 64 2c 20 30 2c 20 30 >iSelectId, 0, 0
96b0: 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d , zMsg, P4_DYNAM
96c0: 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a IC);. }.}../*.*
96d0: 2a 20 41 73 73 69 67 6e 20 65 78 70 72 65 73 73 * Assign express
96e0: 69 6f 6e 20 62 20 74 6f 20 6c 76 61 6c 75 65 20 ion b to lvalue
96f0: 61 2e 20 41 20 73 65 63 6f 6e 64 2c 20 6e 6f 2d a. A second, no-
9700: 6f 70 2c 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 op, version of t
9710: 68 69 73 20 6d 61 63 72 6f 0a 2a 2a 20 69 73 20 his macro.** is
9720: 70 72 6f 76 69 64 65 64 20 77 68 65 6e 20 53 51 provided when SQ
9730: 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
9740: 4e 20 69 73 20 64 65 66 69 6e 65 64 2e 20 54 68 N is defined. Th
9750: 69 73 20 61 6c 6c 6f 77 73 20 74 68 65 20 63 6f is allows the co
9760: 64 65 0a 2a 2a 20 69 6e 20 73 71 6c 69 74 65 33 de.** in sqlite3
9770: 53 65 6c 65 63 74 28 29 20 74 6f 20 61 73 73 69 Select() to assi
9780: 67 6e 20 76 61 6c 75 65 73 20 74 6f 20 73 74 72 gn values to str
9790: 75 63 74 75 72 65 20 6d 65 6d 62 65 72 20 76 61 ucture member va
97a0: 72 69 61 62 6c 65 73 20 74 68 61 74 0a 2a 2a 20 riables that.**
97b0: 6f 6e 6c 79 20 65 78 69 73 74 20 69 66 20 53 51 only exist if SQ
97c0: 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 LITE_OMIT_EXPLAI
97d0: 4e 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 N is not defined
97e0: 20 77 69 74 68 6f 75 74 20 70 6f 6c 6c 75 74 69 without polluti
97f0: 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 ng the.** code w
9800: 69 74 68 20 23 69 66 6e 64 65 66 20 64 69 72 65 ith #ifndef dire
9810: 63 74 69 76 65 73 2e 0a 2a 2f 0a 23 20 64 65 66 ctives..*/.# def
9820: 69 6e 65 20 65 78 70 6c 61 69 6e 53 65 74 49 6e ine explainSetIn
9830: 74 65 67 65 72 28 61 2c 20 62 29 20 61 20 3d 20 teger(a, b) a =
9840: 62 0a 0a 23 65 6c 73 65 0a 2f 2a 20 4e 6f 2d 6f b..#else./* No-o
9850: 70 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 p versions of th
9860: 65 20 65 78 70 6c 61 69 6e 58 58 58 28 29 20 66 e explainXXX() f
9870: 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 6d 61 63 unctions and mac
9880: 72 6f 73 2e 20 2a 2f 0a 23 20 64 65 66 69 6e 65 ros. */.# define
9890: 20 65 78 70 6c 61 69 6e 54 65 6d 70 54 61 62 6c explainTempTabl
98a0: 65 28 79 2c 7a 29 0a 23 20 64 65 66 69 6e 65 20 e(y,z).# define
98b0: 65 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 explainSetIntege
98c0: 72 28 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 r(y,z).#endif..#
98d0: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 if !defined(SQLI
98e0: 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 TE_OMIT_EXPLAIN)
98f0: 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c && !defined(SQL
9900: 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e ITE_OMIT_COMPOUN
9910: 44 5f 53 45 4c 45 43 54 29 0a 2f 2a 0a 2a 2a 20 D_SELECT)./*.**
9920: 55 6e 6c 65 73 73 20 61 6e 20 22 45 58 50 4c 41 Unless an "EXPLA
9930: 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 22 20 63 IN QUERY PLAN" c
9940: 6f 6d 6d 61 6e 64 20 69 73 20 62 65 69 6e 67 20 ommand is being
9950: 70 72 6f 63 65 73 73 65 64 2c 20 74 68 69 73 20 processed, this
9960: 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 61 function.** is a
9970: 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 no-op. Otherwis
9980: 65 2c 20 69 74 20 61 64 64 73 20 61 20 73 69 6e e, it adds a sin
9990: 67 6c 65 20 72 6f 77 20 6f 66 20 6f 75 74 70 75 gle row of outpu
99a0: 74 20 74 6f 20 74 68 65 20 45 51 50 20 72 65 73 t to the EQP res
99b0: 75 6c 74 2c 0a 2a 2a 20 77 68 65 72 65 20 74 68 ult,.** where th
99c0: 65 20 63 61 70 74 69 6f 6e 20 69 73 20 6f 66 20 e caption is of
99d0: 6f 6e 65 20 6f 66 20 74 68 65 20 74 77 6f 20 66 one of the two f
99e0: 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 43 orms:.**.** "C
99f0: 4f 4d 50 4f 53 49 54 45 20 53 55 42 51 55 45 52 OMPOSITE SUBQUER
9a00: 49 45 53 20 69 53 75 62 31 20 61 6e 64 20 69 53 IES iSub1 and iS
9a10: 75 62 32 20 28 6f 70 29 22 0a 2a 2a 20 20 20 22 ub2 (op)".** "
9a20: 43 4f 4d 50 4f 53 49 54 45 20 53 55 42 51 55 45 COMPOSITE SUBQUE
9a30: 52 49 45 53 20 69 53 75 62 31 20 61 6e 64 20 69 RIES iSub1 and i
9a40: 53 75 62 32 20 55 53 49 4e 47 20 54 45 4d 50 20 Sub2 USING TEMP
9a50: 42 2d 54 52 45 45 20 28 6f 70 29 22 0a 2a 2a 0a B-TREE (op)".**.
9a60: 2a 2a 20 77 68 65 72 65 20 69 53 75 62 31 20 61 ** where iSub1 a
9a70: 6e 64 20 69 53 75 62 32 20 61 72 65 20 74 68 65 nd iSub2 are the
9a80: 20 69 6e 74 65 67 65 72 73 20 70 61 73 73 65 64 integers passed
9a90: 20 61 73 20 74 68 65 20 63 6f 72 72 65 73 70 6f as the correspo
9aa0: 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f nding.** functio
9ab0: 6e 20 70 61 72 61 6d 65 74 65 72 73 2c 20 61 6e n parameters, an
9ac0: 64 20 6f 70 20 69 73 20 74 68 65 20 74 65 78 74 d op is the text
9ad0: 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 representation
9ae0: 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 of the parameter
9af0: 0a 2a 2a 20 6f 66 20 74 68 65 20 73 61 6d 65 20 .** of the same
9b00: 6e 61 6d 65 2e 20 54 68 65 20 70 61 72 61 6d 65 name. The parame
9b10: 74 65 72 20 22 6f 70 22 20 6d 75 73 74 20 62 65 ter "op" must be
9b20: 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 4e 49 4f 4e one of TK_UNION
9b30: 2c 20 54 4b 5f 45 58 43 45 50 54 2c 0a 2a 2a 20 , TK_EXCEPT,.**
9b40: 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 6f 72 20 TK_INTERSECT or
9b50: 54 4b 5f 41 4c 4c 2e 20 54 68 65 20 66 69 72 73 TK_ALL. The firs
9b60: 74 20 66 6f 72 6d 20 69 73 20 75 73 65 64 20 69 t form is used i
9b70: 66 20 61 72 67 75 6d 65 6e 74 20 62 55 73 65 54 f argument bUseT
9b80: 6d 70 20 69 73 20 0a 2a 2a 20 66 61 6c 73 65 2c mp is .** false,
9b90: 20 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 66 or the second f
9ba0: 6f 72 6d 20 69 66 20 69 74 20 69 73 20 74 72 75 orm if it is tru
9bb0: 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 e..*/.static voi
9bc0: 64 20 65 78 70 6c 61 69 6e 43 6f 6d 70 6f 73 69 d explainComposi
9bd0: 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 te(. Parse *pPa
9be0: 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 rse,
9bf0: 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 /* Parse c
9c00: 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 ontext */. int
9c10: 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 op,
9c20: 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f /* O
9c30: 6e 65 20 6f 66 20 54 4b 5f 55 4e 49 4f 4e 2c 20 ne of TK_UNION,
9c40: 54 4b 5f 45 58 43 45 50 54 20 65 74 63 2e 20 2a TK_EXCEPT etc. *
9c50: 2f 0a 20 20 69 6e 74 20 69 53 75 62 31 2c 20 20 /. int iSub1,
9c60: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9c70: 20 20 20 20 2f 2a 20 53 75 62 71 75 65 72 79 20 /* Subquery
9c80: 69 64 20 31 20 2a 2f 0a 20 20 69 6e 74 20 69 53 id 1 */. int iS
9c90: 75 62 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 ub2,
9ca0: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 75 62 /* Sub
9cb0: 71 75 65 72 79 20 69 64 20 32 20 2a 2f 0a 20 20 query id 2 */.
9cc0: 69 6e 74 20 62 55 73 65 54 6d 70 20 20 20 20 20 int bUseTmp
9cd0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
9ce0: 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74 65 6d /* True if a tem
9cf0: 70 20 74 61 62 6c 65 20 77 61 73 20 75 73 65 64 p table was used
9d00: 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 */.){. assert(
9d10: 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 7c 7c op==TK_UNION ||
9d20: 20 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 7c op==TK_EXCEPT |
9d30: 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 | op==TK_INTERSE
9d40: 43 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 41 4c 4c CT || op==TK_ALL
9d50: 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 );. if( pParse
9d60: 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a ->explain==2 ){.
9d70: 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 Vdbe *v = pP
9d80: 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 arse->pVdbe;.
9d90: 20 63 68 61 72 20 2a 7a 4d 73 67 20 3d 20 73 71 char *zMsg = sq
9da0: 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 lite3MPrintf(.
9db0: 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 pParse->db
9dc0: 2c 20 22 43 4f 4d 50 4f 55 4e 44 20 53 55 42 51 , "COMPOUND SUBQ
9dd0: 55 45 52 49 45 53 20 25 64 20 41 4e 44 20 25 64 UERIES %d AND %d
9de0: 20 25 73 28 25 73 29 22 2c 20 69 53 75 62 31 2c %s(%s)", iSub1,
9df0: 20 69 53 75 62 32 2c 0a 20 20 20 20 20 20 20 20 iSub2,.
9e00: 62 55 73 65 54 6d 70 3f 22 55 53 49 4e 47 20 54 bUseTmp?"USING T
9e10: 45 4d 50 20 42 2d 54 52 45 45 20 22 3a 22 22 2c EMP B-TREE ":"",
9e20: 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 6f 70 selectOpName(op
9e30: 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c ). );. sql
9e40: 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
9e50: 2c 20 4f 50 5f 45 78 70 6c 61 69 6e 2c 20 70 50 , OP_Explain, pP
9e60: 61 72 73 65 2d 3e 69 53 65 6c 65 63 74 49 64 2c arse->iSelectId,
9e70: 20 30 2c 20 30 2c 20 7a 4d 73 67 2c 20 50 34 5f 0, 0, zMsg, P4_
9e80: 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a DYNAMIC);. }.}.
9e90: 23 65 6c 73 65 0a 2f 2a 20 4e 6f 2d 6f 70 20 76 #else./* No-op v
9ea0: 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 65 ersions of the e
9eb0: 78 70 6c 61 69 6e 58 58 58 28 29 20 66 75 6e 63 xplainXXX() func
9ec0: 74 69 6f 6e 73 20 61 6e 64 20 6d 61 63 72 6f 73 tions and macros
9ed0: 2e 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 65 78 . */.# define ex
9ee0: 70 6c 61 69 6e 43 6f 6d 70 6f 73 69 74 65 28 76 plainComposite(v
9ef0: 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 ,w,x,y,z).#endif
9f00: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 ../*.** If the i
9f10: 6e 6e 65 72 20 6c 6f 6f 70 20 77 61 73 20 67 65 nner loop was ge
9f20: 6e 65 72 61 74 65 64 20 75 73 69 6e 67 20 61 20 nerated using a
9f30: 6e 6f 6e 2d 6e 75 6c 6c 20 70 4f 72 64 65 72 42 non-null pOrderB
9f40: 79 20 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 74 y argument,.** t
9f50: 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 hen the results
9f60: 77 65 72 65 20 70 6c 61 63 65 64 20 69 6e 20 61 were placed in a
9f70: 20 73 6f 72 74 65 72 2e 20 20 41 66 74 65 72 20 sorter. After
9f80: 74 68 65 20 6c 6f 6f 70 20 69 73 20 74 65 72 6d the loop is term
9f90: 69 6e 61 74 65 64 0a 2a 2a 20 77 65 20 6e 65 65 inated.** we nee
9fa0: 64 20 74 6f 20 72 75 6e 20 74 68 65 20 73 6f 72 d to run the sor
9fb0: 74 65 72 20 61 6e 64 20 6f 75 74 70 75 74 20 74 ter and output t
9fc0: 68 65 20 72 65 73 75 6c 74 73 2e 20 20 54 68 65 he results. The
9fd0: 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 72 6f following.** ro
9fe0: 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 utine generates
9ff0: 74 68 65 20 63 6f 64 65 20 6e 65 65 64 65 64 20 the code needed
a000: 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 73 to do that..*/.s
a010: 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 tatic void gener
a020: 61 74 65 53 6f 72 74 54 61 69 6c 28 0a 20 20 50 ateSortTail(. P
a030: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
a040: 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 /* Parsing cont
a050: 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ext */. Select
a060: 2a 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 *p, /* Th
a070: 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 e SELECT stateme
a080: 6e 74 20 2a 2f 0a 20 20 53 6f 72 74 43 74 78 20 nt */. SortCtx
a090: 2a 70 53 6f 72 74 2c 20 20 20 2f 2a 20 49 6e 66 *pSort, /* Inf
a0a0: 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 74 68 65 20 ormation on the
a0b0: 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 ORDER BY clause
a0c0: 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e */. int nColumn
a0d0: 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 , /* Number
a0e0: 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 of columns of d
a0f0: 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 ata */. SelectD
a100: 65 73 74 20 2a 70 44 65 73 74 20 2f 2a 20 57 72 est *pDest /* Wr
a110: 69 74 65 20 74 68 65 20 73 6f 72 74 65 64 20 72 ite the sorted r
a120: 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 esults here */.)
a130: 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 {. Vdbe *v = pP
a140: 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20 arse->pVdbe;
a150: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a160: 20 2f 2a 20 54 68 65 20 70 72 65 70 61 72 65 64 /* The prepared
a170: 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 statement */.
a180: 69 6e 74 20 61 64 64 72 42 72 65 61 6b 20 3d 20 int addrBreak =
a190: 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c sqlite3VdbeMakeL
a1a0: 61 62 65 6c 28 76 29 3b 20 20 20 20 20 2f 2a 20 abel(v); /*
a1b0: 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 65 78 69 Jump here to exi
a1c0: 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 t loop */. int
a1d0: 61 64 64 72 43 6f 6e 74 69 6e 75 65 20 3d 20 73 addrContinue = s
a1e0: 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 qlite3VdbeMakeLa
a1f0: 62 65 6c 28 76 29 3b 20 20 2f 2a 20 4a 75 6d 70 bel(v); /* Jump
a200: 20 68 65 72 65 20 66 6f 72 20 6e 65 78 74 20 63 here for next c
a210: 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 ycle */. int ad
a220: 64 72 3b 0a 20 20 69 6e 74 20 61 64 64 72 4f 6e dr;. int addrOn
a230: 63 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 54 ce = 0;. int iT
a240: 61 62 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a ab;. ExprList *
a250: 70 4f 72 64 65 72 42 79 20 3d 20 70 53 6f 72 74 pOrderBy = pSort
a260: 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 6e ->pOrderBy;. in
a270: 74 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d t eDest = pDest-
a280: 3e 65 44 65 73 74 3b 0a 20 20 69 6e 74 20 69 50 >eDest;. int iP
a290: 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69 53 44 arm = pDest->iSD
a2a0: 50 61 72 6d 3b 0a 20 20 69 6e 74 20 72 65 67 52 Parm;. int regR
a2b0: 6f 77 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 ow;. int regRow
a2c0: 69 64 3b 0a 20 20 69 6e 74 20 6e 4b 65 79 3b 0a id;. int nKey;.
a2d0: 20 20 69 6e 74 20 69 53 6f 72 74 54 61 62 3b 20 int iSortTab;
a2e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
a2f0: 20 20 2f 2a 20 53 6f 72 74 65 72 20 63 75 72 73 /* Sorter curs
a300: 6f 72 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 or to read from
a310: 2a 2f 0a 20 20 69 6e 74 20 6e 53 6f 72 74 44 61 */. int nSortDa
a320: 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 ta;
a330: 20 20 20 20 20 2f 2a 20 54 72 61 69 6c 69 6e 67 /* Trailing
a340: 20 76 61 6c 75 65 73 20 74 6f 20 72 65 61 64 20 values to read
a350: 66 72 6f 6d 20 73 6f 72 74 65 72 20 2a 2f 0a 20 from sorter */.
a360: 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 62 53 int i;. int bS
a370: 65 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 eq;
a380: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
a390: 65 20 69 66 20 73 6f 72 74 65 72 20 72 65 63 6f e if sorter reco
a3a0: 72 64 20 69 6e 63 6c 75 64 65 73 20 73 65 71 2e rd includes seq.
a3b0: 20 6e 6f 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53 no. */.#ifdef S
a3c0: 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 QLITE_ENABLE_EXP
a3d0: 4c 41 49 4e 5f 43 4f 4d 4d 45 4e 54 53 0a 20 20 LAIN_COMMENTS.
a3e0: 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
a3f0: 69 74 65 6d 20 2a 61 4f 75 74 45 78 20 3d 20 70 item *aOutEx = p
a400: 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 0a 23 65 6e ->pEList->a;.#en
a410: 64 69 66 0a 0a 20 20 69 66 28 20 70 53 6f 72 74 dif.. if( pSort
a420: 2d 3e 6c 61 62 65 6c 42 6b 4f 75 74 20 29 7b 0a ->labelBkOut ){.
a430: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
a440: 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 ddOp2(v, OP_Gosu
a450: 62 2c 20 70 53 6f 72 74 2d 3e 72 65 67 52 65 74 b, pSort->regRet
a460: 75 72 6e 2c 20 70 53 6f 72 74 2d 3e 6c 61 62 65 urn, pSort->labe
a470: 6c 42 6b 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c lBkOut);. sql
a480: 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
a490: 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 , OP_Goto, 0, ad
a4a0: 64 72 42 72 65 61 6b 29 3b 0a 20 20 20 20 73 71 drBreak);. sq
a4b0: 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 lite3VdbeResolve
a4c0: 4c 61 62 65 6c 28 76 2c 20 70 53 6f 72 74 2d 3e Label(v, pSort->
a4d0: 6c 61 62 65 6c 42 6b 4f 75 74 29 3b 0a 20 20 7d labelBkOut);. }
a4e0: 0a 20 20 69 54 61 62 20 3d 20 70 53 6f 72 74 2d . iTab = pSort-
a4f0: 3e 69 45 43 75 72 73 6f 72 3b 0a 20 20 69 66 28 >iECursor;. if(
a500: 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 eDest==SRT_Outp
a510: 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 ut || eDest==SRT
a520: 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20 _Coroutine ){.
a530: 20 20 72 65 67 52 6f 77 69 64 20 3d 20 30 3b 0a regRowid = 0;.
a540: 20 20 20 20 72 65 67 52 6f 77 20 3d 20 70 44 65 regRow = pDe
a550: 73 74 2d 3e 69 53 64 73 74 3b 0a 20 20 20 20 6e st->iSdst;. n
a560: 53 6f 72 74 44 61 74 61 20 3d 20 6e 43 6f 6c 75 SortData = nColu
a570: 6d 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 mn;. }else{.
a580: 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 regRowid = sqli
a590: 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
a5a0: 61 72 73 65 29 3b 0a 20 20 20 20 72 65 67 52 6f arse);. regRo
a5b0: 77 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 w = sqlite3GetTe
a5c0: 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 mpReg(pParse);.
a5d0: 20 20 20 6e 53 6f 72 74 44 61 74 61 20 3d 20 31 nSortData = 1
a5e0: 3b 0a 20 20 7d 0a 20 20 6e 4b 65 79 20 3d 20 70 ;. }. nKey = p
a5f0: 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2d OrderBy->nExpr -
a600: 20 70 53 6f 72 74 2d 3e 6e 4f 42 53 61 74 3b 0a pSort->nOBSat;.
a610: 20 20 69 66 28 20 70 53 6f 72 74 2d 3e 73 6f 72 if( pSort->sor
a620: 74 46 6c 61 67 73 20 26 20 53 4f 52 54 46 4c 41 tFlags & SORTFLA
a630: 47 5f 55 73 65 53 6f 72 74 65 72 20 29 7b 0a 20 G_UseSorter ){.
a640: 20 20 20 69 6e 74 20 72 65 67 53 6f 72 74 4f 75 int regSortOu
a650: 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d t = ++pParse->nM
a660: 65 6d 3b 0a 20 20 20 20 69 53 6f 72 74 54 61 62 em;. iSortTab
a670: 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b = pParse->nTab+
a680: 2b 3b 0a 20 20 20 20 69 66 28 20 70 53 6f 72 74 +;. if( pSort
a690: 2d 3e 6c 61 62 65 6c 42 6b 4f 75 74 20 29 7b 0a ->labelBkOut ){.
a6a0: 20 20 20 20 20 20 61 64 64 72 4f 6e 63 65 20 3d addrOnce =
a6b0: 20 73 71 6c 69 74 65 33 43 6f 64 65 4f 6e 63 65 sqlite3CodeOnce
a6c0: 28 70 50 61 72 73 65 29 3b 20 56 64 62 65 43 6f (pParse); VdbeCo
a6d0: 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 7d verage(v);. }
a6e0: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
a6f0: 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 AddOp3(v, OP_Ope
a700: 6e 50 73 65 75 64 6f 2c 20 69 53 6f 72 74 54 61 nPseudo, iSortTa
a710: 62 2c 20 72 65 67 53 6f 72 74 4f 75 74 2c 20 6e b, regSortOut, n
a720: 4b 65 79 2b 31 2b 6e 53 6f 72 74 44 61 74 61 29 Key+1+nSortData)
a730: 3b 0a 20 20 20 20 69 66 28 20 61 64 64 72 4f 6e ;. if( addrOn
a740: 63 65 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 ce ) sqlite3Vdbe
a750: 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 JumpHere(v, addr
a760: 4f 6e 63 65 29 3b 0a 20 20 20 20 61 64 64 72 20 Once);. addr
a770: 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64 62 = 1 + sqlite3Vdb
a780: 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f eAddOp2(v, OP_So
a790: 72 74 65 72 53 6f 72 74 2c 20 69 54 61 62 2c 20 rterSort, iTab,
a7a0: 61 64 64 72 42 72 65 61 6b 29 3b 0a 20 20 20 20 addrBreak);.
a7b0: 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
a7c0: 0a 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74 28 . codeOffset(
a7d0: 76 2c 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20 61 v, p->iOffset, a
a7e0: 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 ddrContinue);.
a7f0: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
a800: 4f 70 33 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 Op3(v, OP_Sorter
a810: 44 61 74 61 2c 20 69 54 61 62 2c 20 72 65 67 53 Data, iTab, regS
a820: 6f 72 74 4f 75 74 2c 20 69 53 6f 72 74 54 61 62 ortOut, iSortTab
a830: 29 3b 0a 20 20 20 20 62 53 65 71 20 3d 20 30 3b );. bSeq = 0;
a840: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 . }else{. ad
a850: 64 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 dr = 1 + sqlite3
a860: 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
a870: 5f 53 6f 72 74 2c 20 69 54 61 62 2c 20 61 64 64 _Sort, iTab, add
a880: 72 42 72 65 61 6b 29 3b 20 56 64 62 65 43 6f 76 rBreak); VdbeCov
a890: 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 63 6f erage(v);. co
a8a0: 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2d 3e 69 deOffset(v, p->i
a8b0: 4f 66 66 73 65 74 2c 20 61 64 64 72 43 6f 6e 74 Offset, addrCont
a8c0: 69 6e 75 65 29 3b 0a 20 20 20 20 69 53 6f 72 74 inue);. iSort
a8d0: 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20 20 Tab = iTab;.
a8e0: 62 53 65 71 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 bSeq = 1;. }.
a8f0: 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 6f 72 74 for(i=0; i<nSort
a900: 44 61 74 61 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Data; i++){.
a910: 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
a920: 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 3(v, OP_Column,
a930: 69 53 6f 72 74 54 61 62 2c 20 6e 4b 65 79 2b 62 iSortTab, nKey+b
a940: 53 65 71 2b 69 2c 20 72 65 67 52 6f 77 2b 69 29 Seq+i, regRow+i)
a950: 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e ;. VdbeCommen
a960: 74 28 28 76 2c 20 22 25 73 22 2c 20 61 4f 75 74 t((v, "%s", aOut
a970: 45 78 5b 69 5d 2e 7a 4e 61 6d 65 20 3f 20 61 4f Ex[i].zName ? aO
a980: 75 74 45 78 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20 utEx[i].zName :
a990: 61 4f 75 74 45 78 5b 69 5d 2e 7a 53 70 61 6e 29 aOutEx[i].zSpan)
a9a0: 29 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 );. }. switch(
a9b0: 20 65 44 65 73 74 20 29 7b 0a 20 20 20 20 63 61 eDest ){. ca
a9c0: 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 se SRT_Table:.
a9d0: 20 20 63 61 73 65 20 53 52 54 5f 45 70 68 65 6d case SRT_Ephem
a9e0: 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 Tab: {. tes
a9f0: 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 tcase( eDest==SR
aa00: 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 T_Table );.
aa10: 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74 testcase( eDest
aa20: 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 ==SRT_EphemTab )
aa30: 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
aa40: 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
aa50: 4e 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c NewRowid, iParm,
aa60: 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 regRowid);.
aa70: 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
aa80: 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 Op3(v, OP_Insert
aa90: 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 2c , iParm, regRow,
aaa0: 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 regRowid);.
aab0: 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
aac0: 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f ngeP5(v, OPFLAG_
aad0: 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 62 APPEND);. b
aae0: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e reak;. }.#ifn
aaf0: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
ab00: 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 SUBQUERY. cas
ab10: 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 e SRT_Set: {.
ab20: 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 assert( nColu
ab30: 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 mn==1 );. s
ab40: 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
ab50: 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 (v, OP_MakeRecor
ab60: 64 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20 72 65 d, regRow, 1, re
ab70: 67 52 6f 77 69 64 2c 0a 20 20 20 20 20 20 20 20 gRowid,.
ab80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
ab90: 26 70 44 65 73 74 2d 3e 61 66 66 53 64 73 74 2c &pDest->affSdst,
aba0: 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 1);. sqlit
abb0: 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e e3ExprCacheAffin
abc0: 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 ityChange(pParse
abd0: 2c 20 72 65 67 52 6f 77 2c 20 31 29 3b 0a 20 20 , regRow, 1);.
abe0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
abf0: 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 ddOp2(v, OP_IdxI
ac00: 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 65 nsert, iParm, re
ac10: 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 62 gRowid);. b
ac20: 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 reak;. }.
ac30: 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b 0a case SRT_Mem: {.
ac40: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 assert( nC
ac50: 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 olumn==1 );.
ac60: 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
ac70: 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 eMove(pParse, re
ac80: 67 52 6f 77 2c 20 69 50 61 72 6d 2c 20 31 29 3b gRow, iParm, 1);
ac90: 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 . /* The LI
aca0: 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 MIT clause will
acb0: 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f terminate the lo
acc0: 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 op for us */.
acd0: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
ace0: 23 65 6e 64 69 66 0a 20 20 20 20 64 65 66 61 75 #endif. defau
acf0: 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 lt: {. asse
ad00: 72 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f rt( eDest==SRT_O
ad10: 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d utput || eDest==
ad20: 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b SRT_Coroutine );
ad30: 20 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 . testcase
ad40: 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 ( eDest==SRT_Out
ad50: 70 75 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 put );. tes
ad60: 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 tcase( eDest==SR
ad70: 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b 0a 20 T_Coroutine );.
ad80: 20 20 20 20 20 69 66 28 20 65 44 65 73 74 3d 3d if( eDest==
ad90: 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 SRT_Output ){.
ada0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
adb0: 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 eAddOp2(v, OP_Re
adc0: 73 75 6c 74 52 6f 77 2c 20 70 44 65 73 74 2d 3e sultRow, pDest->
add0: 69 53 64 73 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b iSdst, nColumn);
ade0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
adf0: 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 ExprCacheAffinit
ae00: 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 yChange(pParse,
ae10: 70 44 65 73 74 2d 3e 69 53 64 73 74 2c 20 6e 43 pDest->iSdst, nC
ae20: 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 65 olumn);. }e
ae30: 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c lse{. sql
ae40: 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 ite3VdbeAddOp1(v
ae50: 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 , OP_Yield, pDes
ae60: 74 2d 3e 69 53 44 50 61 72 6d 29 3b 0a 20 20 20 t->iSDParm);.
ae70: 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b }. break
ae80: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 ;. }. }. if
ae90: 28 20 72 65 67 52 6f 77 69 64 20 29 7b 0a 20 20 ( regRowid ){.
aea0: 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 sqlite3Release
aeb0: 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 TempReg(pParse,
aec0: 72 65 67 52 6f 77 29 3b 0a 20 20 20 20 73 71 6c regRow);. sql
aed0: 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 ite3ReleaseTempR
aee0: 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f eg(pParse, regRo
aef0: 77 69 64 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 wid);. }. /* T
af00: 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 he bottom of the
af10: 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 73 71 6c loop. */. sql
af20: 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
af30: 61 62 65 6c 28 76 2c 20 61 64 64 72 43 6f 6e 74 abel(v, addrCont
af40: 69 6e 75 65 29 3b 0a 20 20 69 66 28 20 70 53 6f inue);. if( pSo
af50: 72 74 2d 3e 73 6f 72 74 46 6c 61 67 73 20 26 20 rt->sortFlags &
af60: 53 4f 52 54 46 4c 41 47 5f 55 73 65 53 6f 72 74 SORTFLAG_UseSort
af70: 65 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 er ){. sqlite
af80: 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
af90: 50 5f 53 6f 72 74 65 72 4e 65 78 74 2c 20 69 54 P_SorterNext, iT
afa0: 61 62 2c 20 61 64 64 72 29 3b 20 56 64 62 65 43 ab, addr); VdbeC
afb0: 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 7d 65 overage(v);. }e
afc0: 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 lse{. sqlite3
afd0: 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
afe0: 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20 61 64 64 _Next, iTab, add
aff0: 72 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 r); VdbeCoverage
b000: 28 76 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 (v);. }. if( p
b010: 53 6f 72 74 2d 3e 72 65 67 52 65 74 75 72 6e 20 Sort->regReturn
b020: 29 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 ) sqlite3VdbeAdd
b030: 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e Op1(v, OP_Return
b040: 2c 20 70 53 6f 72 74 2d 3e 72 65 67 52 65 74 75 , pSort->regRetu
b050: 72 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 rn);. sqlite3Vd
b060: 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 beResolveLabel(v
b070: 2c 20 61 64 64 72 42 72 65 61 6b 29 3b 0a 7d 0a , addrBreak);.}.
b080: 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 ./*.** Return a
b090: 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 pointer to a str
b0a0: 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 ing containing t
b0b0: 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e 20 he 'declaration
b0c0: 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a 20 type' of the.**
b0d0: 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 expression pExpr
b0e0: 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61 79 . The string may
b0f0: 20 62 65 20 74 72 65 61 74 65 64 20 61 73 20 73 be treated as s
b100: 74 61 74 69 63 20 62 79 20 74 68 65 20 63 61 6c tatic by the cal
b110: 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 ler..**.** Also
b120: 74 72 79 20 74 6f 20 65 73 74 69 6d 61 74 65 20 try to estimate
b130: 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 the size of the
b140: 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 61 returned value a
b150: 6e 64 20 72 65 74 75 72 6e 20 74 68 61 74 0a 2a nd return that.*
b160: 2a 20 72 65 73 75 6c 74 20 69 6e 20 2a 70 45 73 * result in *pEs
b170: 74 57 69 64 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 tWidth..**.** Th
b180: 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 e declaration ty
b190: 70 65 20 69 73 20 74 68 65 20 65 78 61 63 74 20 pe is the exact
b1a0: 64 61 74 61 74 79 70 65 20 64 65 66 69 6e 69 74 datatype definit
b1b0: 69 6f 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 ion extracted fr
b1c0: 6f 6d 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e om the.** origin
b1d0: 61 6c 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 al CREATE TABLE
b1e0: 73 74 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 statement if the
b1f0: 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 expression is a
b200: 20 63 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 column. The.**
b210: 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 declaration type
b220: 20 66 6f 72 20 61 20 52 4f 57 49 44 20 66 69 65 for a ROWID fie
b230: 6c 64 20 69 73 20 49 4e 54 45 47 45 52 2e 20 45 ld is INTEGER. E
b240: 78 61 63 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 xactly when an e
b250: 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 xpression.** is
b260: 63 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c considered a col
b270: 75 6d 6e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c umn can be compl
b280: 65 78 20 69 6e 20 74 68 65 20 70 72 65 73 65 6e ex in the presen
b290: 63 65 20 6f 66 20 73 75 62 71 75 65 72 69 65 73 ce of subqueries
b2a0: 2e 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d . The.** result-
b2b0: 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 set expression i
b2c0: 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c n all of the fol
b2d0: 6c 6f 77 69 6e 67 20 53 45 4c 45 43 54 20 73 74 lowing SELECT st
b2e0: 61 74 65 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 atements is .**
b2f0: 63 6f 6e 73 69 64 65 72 65 64 20 61 20 63 6f 6c considered a col
b300: 75 6d 6e 20 62 79 20 74 68 69 73 20 66 75 6e 63 umn by this func
b310: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 tion..**.** SE
b320: 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 LECT col FROM tb
b330: 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 l;.** SELECT (
b340: 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 SELECT col FROM
b350: 74 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 tbl;.** SELECT
b360: 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f (SELECT col FRO
b370: 4d 20 74 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c M tbl);.** SEL
b380: 45 43 54 20 61 62 63 20 46 52 4f 4d 20 28 53 45 ECT abc FROM (SE
b390: 4c 45 43 54 20 63 6f 6c 20 41 53 20 61 62 63 20 LECT col AS abc
b3a0: 46 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a FROM tbl);.** .*
b3b0: 2a 20 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f * The declaratio
b3c0: 6e 20 74 79 70 65 20 66 6f 72 20 61 6e 79 20 65 n type for any e
b3d0: 78 70 72 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 xpression other
b3e0: 74 68 61 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 than a column is
b3f0: 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 NULL..**.** Thi
b400: 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 65 69 s routine has ei
b410: 74 68 65 72 20 33 20 6f 72 20 36 20 70 61 72 61 ther 3 or 6 para
b420: 6d 65 74 65 72 73 20 64 65 70 65 6e 64 69 6e 67 meters depending
b430: 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e on whether or n
b440: 6f 74 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 ot.** the SQLITE
b450: 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
b460: 45 54 41 44 41 54 41 20 63 6f 6d 70 69 6c 65 2d ETADATA compile-
b470: 74 69 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 time option is u
b480: 73 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 sed..*/.#ifdef S
b490: 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
b4a0: 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 UMN_METADATA.# d
b4b0: 65 66 69 6e 65 20 63 6f 6c 75 6d 6e 54 79 70 65 efine columnType
b4c0: 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 29 20 63 6f (A,B,C,D,E,F) co
b4d0: 6c 75 6d 6e 54 79 70 65 49 6d 70 6c 28 41 2c 42 lumnTypeImpl(A,B
b4e0: 2c 43 2c 44 2c 45 2c 46 29 0a 73 74 61 74 69 63 ,C,D,E,F).static
b4f0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c const char *col
b500: 75 6d 6e 54 79 70 65 49 6d 70 6c 28 0a 20 20 4e umnTypeImpl(. N
b510: 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c ameContext *pNC,
b520: 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c . Expr *pExpr,
b530: 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a . const char **
b540: 70 7a 4f 72 69 67 44 62 2c 0a 20 20 63 6f 6e 73 pzOrigDb,. cons
b550: 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 54 t char **pzOrigT
b560: 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 ab,. const char
b570: 20 2a 2a 70 7a 4f 72 69 67 43 6f 6c 2c 0a 20 20 **pzOrigCol,.
b580: 75 38 20 2a 70 45 73 74 57 69 64 74 68 0a 29 7b u8 *pEstWidth.){
b590: 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a . char const *z
b5a0: 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20 20 63 68 OrigDb = 0;. ch
b5b0: 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 54 ar const *zOrigT
b5c0: 61 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 ab = 0;. char c
b5d0: 6f 6e 73 74 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d onst *zOrigCol =
b5e0: 20 30 3b 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 0;.#else /* if
b5f0: 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f !defined(SQLITE_
b600: 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 ENABLE_COLUMN_ME
b610: 54 41 44 41 54 41 29 20 2a 2f 0a 23 20 64 65 66 TADATA) */.# def
b620: 69 6e 65 20 63 6f 6c 75 6d 6e 54 79 70 65 28 41 ine columnType(A
b630: 2c 42 2c 43 2c 44 2c 45 2c 46 29 20 63 6f 6c 75 ,B,C,D,E,F) colu
b640: 6d 6e 54 79 70 65 49 6d 70 6c 28 41 2c 42 2c 46 mnTypeImpl(A,B,F
b650: 29 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 ).static const c
b660: 68 61 72 20 2a 63 6f 6c 75 6d 6e 54 79 70 65 49 har *columnTypeI
b670: 6d 70 6c 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 mpl(. NameConte
b680: 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78 70 72 xt *pNC, . Expr
b690: 20 2a 70 45 78 70 72 2c 0a 20 20 75 38 20 2a 70 *pExpr,. u8 *p
b6a0: 45 73 74 57 69 64 74 68 0a 29 7b 0a 23 65 6e 64 EstWidth.){.#end
b6b0: 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 if /* !defined(S
b6c0: 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c QLITE_ENABLE_COL
b6d0: 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 20 2a 2f UMN_METADATA) */
b6e0: 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a . char const *z
b6f0: 54 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 Type = 0;. int
b700: 6a 3b 0a 20 20 75 38 20 65 73 74 57 69 64 74 68 j;. u8 estWidth
b710: 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 4e 45 56 = 1;.. if( NEV
b720: 45 52 28 70 45 78 70 72 3d 3d 30 29 20 7c 7c 20 ER(pExpr==0) ||
b730: 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3d 3d 30 pNC->pSrcList==0
b740: 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 73 ) return 0;. s
b750: 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 witch( pExpr->op
b760: 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f ){. case TK_
b770: 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 AGG_COLUMN:.
b780: 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 case TK_COLUMN:
b790: 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 {. /* The e
b7a0: 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 xpression is a c
b7b0: 6f 6c 75 6d 6e 2e 20 4c 6f 63 61 74 65 20 74 68 olumn. Locate th
b7c0: 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f 6c 75 e table the colu
b7d0: 6d 6e 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 mn is being.
b7e0: 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64 20 66 ** extracted f
b7f0: 72 6f 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 rom in NameConte
b800: 78 74 2e 70 53 72 63 4c 69 73 74 2e 20 54 68 69 xt.pSrcList. Thi
b810: 73 20 74 61 62 6c 65 20 6d 61 79 20 62 65 20 72 s table may be r
b820: 65 61 6c 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 eal. ** dat
b830: 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 abase table or a
b840: 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20 20 subquery..
b850: 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 */. Table
b860: 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 *pTab = 0;
b870: 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 73 /* Table s
b880: 74 72 75 63 74 75 72 65 20 63 6f 6c 75 6d 6e 20 tructure column
b890: 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72 6f is extracted fro
b8a0: 6d 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 m */. Selec
b8b0: 74 20 2a 70 53 20 3d 20 30 3b 20 20 20 20 20 20 t *pS = 0;
b8c0: 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 /* Select
b8d0: 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 the column is e
b8e0: 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f xtracted from */
b8f0: 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 . int iCol
b900: 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e = pExpr->iColumn
b910: 3b 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 ; /* Index of c
b920: 6f 6c 75 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f olumn in pTab */
b930: 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 . testcase(
b940: 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 pExpr->op==TK_A
b950: 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 GG_COLUMN );.
b960: 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 testcase( pEx
b970: 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d pr->op==TK_COLUM
b980: 4e 20 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 N );. while
b990: 28 20 70 4e 43 20 26 26 20 21 70 54 61 62 20 29 ( pNC && !pTab )
b9a0: 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69 73 {. SrcLis
b9b0: 74 20 2a 70 54 61 62 4c 69 73 74 20 3d 20 70 4e t *pTabList = pN
b9c0: 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 C->pSrcList;.
b9d0: 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 6a 3c 70 for(j=0;j<p
b9e0: 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 26 26 TabList->nSrc &&
b9f0: 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e pTabList->a[j].
ba00: 69 43 75 72 73 6f 72 21 3d 70 45 78 70 72 2d 3e iCursor!=pExpr->
ba10: 69 54 61 62 6c 65 3b 6a 2b 2b 29 3b 0a 20 20 20 iTable;j++);.
ba20: 20 20 20 20 20 69 66 28 20 6a 3c 70 54 61 62 4c if( j<pTabL
ba30: 69 73 74 2d 3e 6e 53 72 63 20 29 7b 0a 20 20 20 ist->nSrc ){.
ba40: 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 pTab = pT
ba50: 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 abList->a[j].pTa
ba60: 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 20 b;. pS
ba70: 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d = pTabList->a[j]
ba80: 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 .pSelect;.
ba90: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 }else{.
baa0: 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e pNC = pNC->pN
bab0: 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ext;. }.
bac0: 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 }.. if
bad0: 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 ( pTab==0 ){.
bae0: 20 20 20 20 20 2f 2a 20 41 74 20 6f 6e 65 20 74 /* At one t
baf0: 69 6d 65 2c 20 63 6f 64 65 20 73 75 63 68 20 61 ime, code such a
bb00: 73 20 22 53 45 4c 45 43 54 20 6e 65 77 2e 78 22 s "SELECT new.x"
bb10: 20 77 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 within a trigge
bb20: 72 20 77 6f 75 6c 64 0a 20 20 20 20 20 20 20 20 r would.
bb30: 2a 2a 20 63 61 75 73 65 20 74 68 69 73 20 63 6f ** cause this co
bb40: 6e 64 69 74 69 6f 6e 20 74 6f 20 72 75 6e 2e 20 ndition to run.
bb50: 20 53 69 6e 63 65 20 74 68 65 6e 2c 20 77 65 20 Since then, we
bb60: 68 61 76 65 20 72 65 73 74 72 75 63 74 75 72 65 have restructure
bb70: 64 20 68 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a d how. **
bb80: 20 74 72 69 67 67 65 72 20 63 6f 64 65 20 69 73 trigger code is
bb90: 20 67 65 6e 65 72 61 74 65 64 20 61 6e 64 20 73 generated and s
bba0: 6f 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e o this condition
bbb0: 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 0a 20 is no longer .
bbc0: 20 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 ** possib
bbd0: 6c 65 2e 20 48 6f 77 65 76 65 72 2c 20 69 74 20 le. However, it
bbe0: 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 74 72 75 can still be tru
bbf0: 65 20 66 6f 72 20 73 74 61 74 65 6d 65 6e 74 73 e for statements
bc00: 20 6c 69 6b 65 0a 20 20 20 20 20 20 20 20 2a 2a like. **
bc10: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a the following:.
bc20: 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
bc30: 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20 54 ** CREATE T
bc40: 41 42 4c 45 20 74 31 28 63 6f 6c 20 49 4e 54 45 ABLE t1(col INTE
bc50: 47 45 52 29 3b 0a 20 20 20 20 20 20 20 20 2a 2a GER);. **
bc60: 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45 43 SELECT (SELEC
bc70: 54 20 74 31 2e 63 6f 6c 29 20 46 52 4f 4d 20 46 T t1.col) FROM F
bc80: 52 4f 4d 20 74 31 3b 0a 20 20 20 20 20 20 20 20 ROM t1;.
bc90: 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 68 **. ** wh
bca0: 65 6e 20 63 6f 6c 75 6d 6e 54 79 70 65 28 29 20 en columnType()
bcb0: 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 is called on the
bcc0: 20 65 78 70 72 65 73 73 69 6f 6e 20 22 74 31 2e expression "t1.
bcd0: 63 6f 6c 22 20 69 6e 20 74 68 65 20 0a 20 20 20 col" in the .
bce0: 20 20 20 20 20 2a 2a 20 73 75 62 2d 73 65 6c 65 ** sub-sele
bcf0: 63 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 ct. In this case
bd00: 2c 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e , set the column
bd10: 20 74 79 70 65 20 74 6f 20 4e 55 4c 4c 2c 20 65 type to NULL, e
bd20: 76 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 ven. ** t
bd30: 68 6f 75 67 68 20 69 74 20 73 68 6f 75 6c 64 20 hough it should
bd40: 72 65 61 6c 6c 79 20 62 65 20 22 49 4e 54 45 47 really be "INTEG
bd50: 45 52 22 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a ER".. **.
bd60: 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 ** This
bd70: 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d is not a problem
bd80: 2c 20 61 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 , as the column
bd90: 74 79 70 65 20 6f 66 20 22 74 31 2e 63 6f 6c 22 type of "t1.col"
bda0: 20 69 73 20 6e 65 76 65 72 0a 20 20 20 20 20 20 is never.
bdb0: 20 20 2a 2a 20 75 73 65 64 2e 20 57 68 65 6e 20 ** used. When
bdc0: 63 6f 6c 75 6d 6e 54 79 70 65 28 29 20 69 73 20 columnType() is
bdd0: 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 65 78 called on the ex
bde0: 70 72 65 73 73 69 6f 6e 20 0a 20 20 20 20 20 20 pression .
bdf0: 20 20 2a 2a 20 22 28 53 45 4c 45 43 54 20 74 31 ** "(SELECT t1
be00: 2e 63 6f 6c 29 22 2c 20 74 68 65 20 63 6f 72 72 .col)", the corr
be10: 65 63 74 20 74 79 70 65 20 69 73 20 72 65 74 75 ect type is retu
be20: 72 6e 65 64 20 28 73 65 65 20 74 68 65 20 54 4b rned (see the TK
be30: 5f 53 45 4c 45 43 54 0a 20 20 20 20 20 20 20 20 _SELECT.
be40: 2a 2a 20 62 72 61 6e 63 68 20 62 65 6c 6f 77 2e ** branch below.
be50: 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 */. bre
be60: 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 ak;. }..
be70: 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 20 assert( pTab
be80: 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 3d 3d && pExpr->pTab==
be90: 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20 69 66 pTab );. if
bea0: 28 20 70 53 20 29 7b 0a 20 20 20 20 20 20 20 20 ( pS ){.
beb0: 2f 2a 20 54 68 65 20 22 74 61 62 6c 65 22 20 69 /* The "table" i
bec0: 73 20 61 63 74 75 61 6c 6c 79 20 61 20 73 75 62 s actually a sub
bed0: 2d 73 65 6c 65 63 74 20 6f 72 20 61 20 76 69 65 -select or a vie
bee0: 77 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c w in the FROM cl
bef0: 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 ause. **
bf00: 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 of the SELECT st
bf10: 61 74 65 6d 65 6e 74 2e 20 52 65 74 75 72 6e 20 atement. Return
bf20: 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 the declaration
bf30: 74 79 70 65 20 61 6e 64 20 6f 72 69 67 69 6e 0a type and origin.
bf40: 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20 ** data
bf50: 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 2d 73 for the result-s
bf60: 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 et column of the
bf70: 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a 20 20 20 sub-select..
bf80: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 */.
bf90: 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 41 if( iCol>=0 && A
bfa0: 4c 57 41 59 53 28 69 43 6f 6c 3c 70 53 2d 3e 70 LWAYS(iCol<pS->p
bfb0: 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b EList->nExpr) ){
bfc0: 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 . /* If
bfd0: 20 69 43 6f 6c 20 69 73 20 6c 65 73 73 20 74 68 iCol is less th
bfe0: 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 an zero, then th
bff0: 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 71 e expression req
c000: 75 65 73 74 73 20 74 68 65 0a 20 20 20 20 20 20 uests the.
c010: 20 20 20 20 2a 2a 20 72 6f 77 69 64 20 6f 66 20 ** rowid of
c020: 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74 20 6f the sub-select o
c030: 72 20 76 69 65 77 2e 20 54 68 69 73 20 65 78 70 r view. This exp
c040: 72 65 73 73 69 6f 6e 20 69 73 20 6c 65 67 61 6c ression is legal
c050: 20 28 73 65 65 20 0a 20 20 20 20 20 20 20 20 20 (see .
c060: 20 2a 2a 20 74 65 73 74 20 63 61 73 65 20 6d 69 ** test case mi
c070: 73 63 32 2e 32 2e 32 29 20 2d 20 69 74 20 61 6c sc2.2.2) - it al
c080: 77 61 79 73 20 65 76 61 6c 75 61 74 65 73 20 74 ways evaluates t
c090: 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 o NULL..
c0a0: 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 4e */. N
c0b0: 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a ameContext sNC;.
c0c0: 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a Expr *
c0d0: 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e p = pS->pEList->
c0e0: 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 a[iCol].pExpr;.
c0f0: 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70 53 72 sNC.pSr
c100: 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 cList = pS->pSrc
c110: 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e ;. sNC.
c120: 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20 20 20 pNext = pNC;.
c130: 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61 72 73 sNC.pPars
c140: 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b e = pNC->pParse;
c150: 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 . zType
c160: 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 = columnType(&s
c170: 4e 43 2c 20 70 2c 26 7a 4f 72 69 67 44 62 2c 26 NC, p,&zOrigDb,&
c180: 7a 4f 72 69 67 54 61 62 2c 26 7a 4f 72 69 67 43 zOrigTab,&zOrigC
c190: 6f 6c 2c 20 26 65 73 74 57 69 64 74 68 29 3b 20 ol, &estWidth);
c1a0: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
c1b0: 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61 62 2d }else if( pTab-
c1c0: 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 >pSchema ){.
c1d0: 20 20 20 20 2f 2a 20 41 20 72 65 61 6c 20 74 61 /* A real ta
c1e0: 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 ble */. a
c1f0: 73 73 65 72 74 28 20 21 70 53 20 29 3b 0a 20 20 ssert( !pS );.
c200: 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 if( iCol<0
c210: 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e ) iCol = pTab->
c220: 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 61 iPKey;. a
c230: 73 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 ssert( iCol==-1
c240: 7c 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 || (iCol>=0 && i
c250: 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 Col<pTab->nCol)
c260: 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 );.#ifdef SQLITE
c270: 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d _ENABLE_COLUMN_M
c280: 45 54 41 44 41 54 41 0a 20 20 20 20 20 20 20 20 ETADATA.
c290: 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 if( iCol<0 ){.
c2a0: 20 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 zType =
c2b0: 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 20 "INTEGER";.
c2c0: 20 20 20 20 20 7a 4f 72 69 67 43 6f 6c 20 3d 20 zOrigCol =
c2d0: 22 72 6f 77 69 64 22 3b 0a 20 20 20 20 20 20 20 "rowid";.
c2e0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
c2f0: 20 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d 3e zType = pTab->
c300: 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 aCol[iCol].zType
c310: 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 4f 72 69 ;. zOri
c320: 67 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f gCol = pTab->aCo
c330: 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 l[iCol].zName;.
c340: 20 20 20 20 20 20 20 20 20 65 73 74 57 69 64 74 estWidt
c350: 68 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 h = pTab->aCol[i
c360: 43 6f 6c 5d 2e 73 7a 45 73 74 3b 0a 20 20 20 20 Col].szEst;.
c370: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 4f }. zO
c380: 72 69 67 54 61 62 20 3d 20 70 54 61 62 2d 3e 7a rigTab = pTab->z
c390: 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 Name;. if
c3a0: 28 20 70 4e 43 2d 3e 70 50 61 72 73 65 20 29 7b ( pNC->pParse ){
c3b0: 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 . int i
c3c0: 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 Db = sqlite3Sche
c3d0: 6d 61 54 6f 49 6e 64 65 78 28 70 4e 43 2d 3e 70 maToIndex(pNC->p
c3e0: 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d Parse->db, pTab-
c3f0: 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 >pSchema);.
c400: 20 20 20 20 20 7a 4f 72 69 67 44 62 20 3d 20 70 zOrigDb = p
c410: 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e NC->pParse->db->
c420: 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a aDb[iDb].zName;.
c430: 20 20 20 20 20 20 20 20 7d 0a 23 65 6c 73 65 0a }.#else.
c440: 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c if( iCol
c450: 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 <0 ){.
c460: 7a 54 79 70 65 20 3d 20 22 49 4e 54 45 47 45 52 zType = "INTEGER
c470: 22 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 ";. }else
c480: 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 {. zTyp
c490: 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 e = pTab->aCol[i
c4a0: 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20 20 Col].zType;.
c4b0: 20 20 20 20 20 20 65 73 74 57 69 64 74 68 20 3d estWidth =
c4c0: 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c pTab->aCol[iCol
c4d0: 5d 2e 73 7a 45 73 74 3b 0a 20 20 20 20 20 20 20 ].szEst;.
c4e0: 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 }.#endif.
c4f0: 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 }. break;.
c500: 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c }.#ifndef SQL
c510: 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 ITE_OMIT_SUBQUER
c520: 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 Y. case TK_SE
c530: 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a LECT: {. /*
c540: 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 The expression
c550: 69 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 2e is a sub-select.
c560: 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 63 6c Return the decl
c570: 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 aration type and
c580: 0a 20 20 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e . ** origin
c590: 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20 73 69 info for the si
c5a0: 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 ngle column in t
c5b0: 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 he result set of
c5c0: 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 the SELECT.
c5d0: 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a ** statement..
c5e0: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e */. N
c5f0: 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a ameContext sNC;.
c600: 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 Select *pS
c610: 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c = pExpr->x.pSel
c620: 65 63 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 ect;. Expr
c630: 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d *p = pS->pEList-
c640: 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 >a[0].pExpr;.
c650: 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72 48 assert( ExprH
c660: 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 asProperty(pExpr
c670: 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 , EP_xIsSelect)
c680: 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 53 72 );. sNC.pSr
c690: 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 cList = pS->pSrc
c6a0: 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 ;. sNC.pNex
c6b0: 74 20 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20 73 t = pNC;. s
c6c0: 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d NC.pParse = pNC-
c6d0: 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20 7a >pParse;. z
c6e0: 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 Type = columnTyp
c6f0: 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 e(&sNC, p, &zOri
c700: 67 44 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 gDb, &zOrigTab,
c710: 26 7a 4f 72 69 67 43 6f 6c 2c 20 26 65 73 74 57 &zOrigCol, &estW
c720: 69 64 74 68 29 3b 20 0a 20 20 20 20 20 20 62 72 idth); . br
c730: 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 eak;. }.#endi
c740: 66 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 f. }..#ifdef SQ
c750: 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 LITE_ENABLE_COLU
c760: 4d 4e 5f 4d 45 54 41 44 41 54 41 20 20 0a 20 20 MN_METADATA .
c770: 69 66 28 20 70 7a 4f 72 69 67 44 62 20 29 7b 0a if( pzOrigDb ){.
c780: 20 20 20 20 61 73 73 65 72 74 28 20 70 7a 4f 72 assert( pzOr
c790: 69 67 54 61 62 20 26 26 20 70 7a 4f 72 69 67 43 igTab && pzOrigC
c7a0: 6f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 ol );. *pzOri
c7b0: 67 44 62 20 3d 20 7a 4f 72 69 67 44 62 3b 0a 20 gDb = zOrigDb;.
c7c0: 20 20 20 2a 70 7a 4f 72 69 67 54 61 62 20 3d 20 *pzOrigTab =
c7d0: 7a 4f 72 69 67 54 61 62 3b 0a 20 20 20 20 2a 70 zOrigTab;. *p
c7e0: 7a 4f 72 69 67 43 6f 6c 20 3d 20 7a 4f 72 69 67 zOrigCol = zOrig
c7f0: 43 6f 6c 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a Col;. }.#endif.
c800: 20 20 69 66 28 20 70 45 73 74 57 69 64 74 68 20 if( pEstWidth
c810: 29 20 2a 70 45 73 74 57 69 64 74 68 20 3d 20 65 ) *pEstWidth = e
c820: 73 74 57 69 64 74 68 3b 0a 20 20 72 65 74 75 72 stWidth;. retur
c830: 6e 20 7a 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a n zType;.}../*.*
c840: 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
c850: 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74 that will tell t
c860: 68 65 20 56 44 42 45 20 74 68 65 20 64 65 63 6c he VDBE the decl
c870: 61 72 61 74 69 6f 6e 20 74 79 70 65 73 20 6f 66 aration types of
c880: 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 columns.** in t
c890: 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a he result set..*
c8a0: 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 /.static void ge
c8b0: 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 nerateColumnType
c8c0: 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 s(. Parse *pPar
c8d0: 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 se, /* Pars
c8e0: 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 er context */.
c8f0: 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 SrcList *pTabLis
c900: 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 t, /* List of t
c910: 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c ables */. ExprL
c920: 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 2f ist *pEList /
c930: 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64 65 * Expressions de
c940: 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c fining the resul
c950: 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e t set */.){.#ifn
c960: 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
c970: 44 45 43 4c 54 59 50 45 0a 20 20 56 64 62 65 20 DECLTYPE. Vdbe
c980: 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 *v = pParse->pVd
c990: 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e be;. int i;. N
c9a0: 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a ameContext sNC;.
c9b0: 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d sNC.pSrcList =
c9c0: 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43 pTabList;. sNC
c9d0: 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 .pParse = pParse
c9e0: 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 ;. for(i=0; i<p
c9f0: 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b EList->nExpr; i+
ca00: 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20 +){. Expr *p
ca10: 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 = pEList->a[i].p
ca20: 45 78 70 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 Expr;. const
ca30: 63 68 61 72 20 2a 7a 54 79 70 65 3b 0a 23 69 66 char *zType;.#if
ca40: 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c def SQLITE_ENABL
ca50: 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 E_COLUMN_METADAT
ca60: 41 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 A. const char
ca70: 20 2a 7a 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20 *zOrigDb = 0;.
ca80: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
ca90: 4f 72 69 67 54 61 62 20 3d 20 30 3b 0a 20 20 20 OrigTab = 0;.
caa0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 const char *zOr
cab0: 69 67 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 7a igCol = 0;. z
cac0: 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 Type = columnTyp
cad0: 65 28 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 e(&sNC, p, &zOri
cae0: 67 44 62 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 gDb, &zOrigTab,
caf0: 26 7a 4f 72 69 67 43 6f 6c 2c 20 30 29 3b 0a 0a &zOrigCol, 0);..
cb00: 20 20 20 20 2f 2a 20 54 68 65 20 76 64 62 65 20 /* The vdbe
cb10: 6d 75 73 74 20 6d 61 6b 65 20 69 74 73 20 6f 77 must make its ow
cb20: 6e 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f n copy of the co
cb30: 6c 75 6d 6e 2d 74 79 70 65 20 61 6e 64 20 6f 74 lumn-type and ot
cb40: 68 65 72 20 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 her . ** colu
cb50: 6d 6e 20 73 70 65 63 69 66 69 63 20 73 74 72 69 mn specific stri
cb60: 6e 67 73 2c 20 69 6e 20 63 61 73 65 20 74 68 65 ngs, in case the
cb70: 20 73 63 68 65 6d 61 20 69 73 20 72 65 73 65 74 schema is reset
cb80: 20 62 65 66 6f 72 65 20 74 68 69 73 0a 20 20 20 before this.
cb90: 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 ** virtual mach
cba0: 69 6e 65 20 69 73 20 64 65 6c 65 74 65 64 2e 0a ine is deleted..
cbb0: 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 */. sqlit
cbc0: 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 e3VdbeSetColName
cbd0: 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 44 (v, i, COLNAME_D
cbe0: 41 54 41 42 41 53 45 2c 20 7a 4f 72 69 67 44 62 ATABASE, zOrigDb
cbf0: 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
cc00: 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 NT);. sqlite3
cc10: 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 VdbeSetColName(v
cc20: 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 , i, COLNAME_TAB
cc30: 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c 20 53 51 LE, zOrigTab, SQ
cc40: 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b LITE_TRANSIENT);
cc50: 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
cc60: 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c SetColName(v, i,
cc70: 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 2c COLNAME_COLUMN,
cc80: 20 7a 4f 72 69 67 43 6f 6c 2c 20 53 51 4c 49 54 zOrigCol, SQLIT
cc90: 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 23 65 E_TRANSIENT);.#e
cca0: 6c 73 65 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 lse. zType =
ccb0: 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c columnType(&sNC,
ccc0: 20 70 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b p, 0, 0, 0, 0);
ccd0: 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 .#endif. sqli
cce0: 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d te3VdbeSetColNam
ccf0: 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f e(v, i, COLNAME_
cd00: 44 45 43 4c 54 59 50 45 2c 20 7a 54 79 70 65 2c DECLTYPE, zType,
cd10: 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e SQLITE_TRANSIEN
cd20: 54 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f T);. }.#endif /
cd30: 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 * !defined(SQLIT
cd40: 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 29 E_OMIT_DECLTYPE)
cd50: 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e */.}../*.** Gen
cd60: 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 erate code that
cd70: 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 will tell the VD
cd80: 42 45 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 BE the names of
cd90: 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 columns.** in th
cda0: 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54 e result set. T
cdb0: 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 his information
cdc0: 69 73 20 75 73 65 64 20 74 6f 20 70 72 6f 76 69 is used to provi
cdd0: 64 65 20 74 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b de the.** azCol[
cde0: 5d 20 76 61 6c 75 65 73 20 69 6e 20 74 68 65 20 ] values in the
cdf0: 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 callback..*/.sta
ce00: 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61 74 tic void generat
ce10: 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 0a 20 20 eColumnNames(.
ce20: 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
ce30: 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f /* Parser co
ce40: 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 ntext */. SrcLi
ce50: 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f st *pTabList, /
ce60: 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 * List of tables
ce70: 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a */. ExprList *
ce80: 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45 78 70 pEList /* Exp
ce90: 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e ressions definin
cea0: 67 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 g the result set
ceb0: 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 */.){. Vdbe *v
cec0: 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
ced0: 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 ;. int i, j;.
cee0: 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 sqlite3 *db = pP
cef0: 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 arse->db;. int
cf00: 66 75 6c 6c 4e 61 6d 65 73 2c 20 73 68 6f 72 74 fullNames, short
cf10: 4e 61 6d 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 Names;..#ifndef
cf20: 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
cf30: 41 49 4e 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 AIN. /* If this
cf40: 20 69 73 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20 is an EXPLAIN,
cf50: 73 6b 69 70 20 74 68 69 73 20 73 74 65 70 20 2a skip this step *
cf60: 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e /. if( pParse->
cf70: 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 explain ){. r
cf80: 65 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 eturn;. }.#endi
cf90: 66 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d f.. if( pParse-
cfa0: 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 7c 7c 20 >colNamesSet ||
cfb0: 4e 45 56 45 52 28 76 3d 3d 30 29 20 7c 7c 20 64 NEVER(v==0) || d
cfc0: 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 b->mallocFailed
cfd0: 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 50 61 72 ) return;. pPar
cfe0: 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 se->colNamesSet
cff0: 3d 20 31 3b 0a 20 20 66 75 6c 6c 4e 61 6d 65 73 = 1;. fullNames
d000: 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 = (db->flags &
d010: 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 SQLITE_FullColNa
d020: 6d 65 73 29 21 3d 30 3b 0a 20 20 73 68 6f 72 74 mes)!=0;. short
d030: 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61 Names = (db->fla
d040: 67 73 20 26 20 53 51 4c 49 54 45 5f 53 68 6f 72 gs & SQLITE_Shor
d050: 74 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20 tColNames)!=0;.
d060: 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e sqlite3VdbeSetN
d070: 75 6d 43 6f 6c 73 28 76 2c 20 70 45 4c 69 73 74 umCols(v, pEList
d080: 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 66 6f 72 28 ->nExpr);. for(
d090: 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e i=0; i<pEList->n
d0a0: 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
d0b0: 45 78 70 72 20 2a 70 3b 0a 20 20 20 20 70 20 3d Expr *p;. p =
d0c0: 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 pEList->a[i].pE
d0d0: 78 70 72 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 xpr;. if( NEV
d0e0: 45 52 28 70 3d 3d 30 29 20 29 20 63 6f 6e 74 69 ER(p==0) ) conti
d0f0: 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45 4c nue;. if( pEL
d100: 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 ist->a[i].zName
d110: 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a ){. char *z
d120: 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 Name = pEList->a
d130: 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 [i].zName;.
d140: 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 sqlite3VdbeSetC
d150: 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c olName(v, i, COL
d160: 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 NAME_NAME, zName
d170: 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 , SQLITE_TRANSIE
d180: 4e 54 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 NT);. }else i
d190: 66 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f f( (p->op==TK_CO
d1a0: 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 LUMN || p->op==T
d1b0: 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26 K_AGG_COLUMN) &&
d1c0: 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20 pTabList ){.
d1d0: 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a Table *pTab;.
d1e0: 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c char *zCol
d1f0: 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c ;. int iCol
d200: 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 = p->iColumn;.
d210: 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 41 4c for(j=0; AL
d220: 57 41 59 53 28 6a 3c 70 54 61 62 4c 69 73 74 2d WAYS(j<pTabList-
d230: 3e 6e 53 72 63 29 3b 20 6a 2b 2b 29 7b 0a 20 20 >nSrc); j++){.
d240: 20 20 20 20 20 20 69 66 28 20 70 54 61 62 4c 69 if( pTabLi
d250: 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 st->a[j].iCursor
d260: 3d 3d 70 2d 3e 69 54 61 62 6c 65 20 29 20 62 72 ==p->iTable ) br
d270: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
d280: 20 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61 assert( j<pTa
d290: 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20 bList->nSrc );.
d2a0: 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 pTab = pTab
d2b0: 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b List->a[j].pTab;
d2c0: 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c . if( iCol<
d2d0: 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 0 ) iCol = pTab-
d2e0: 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 >iPKey;. as
d2f0: 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c sert( iCol==-1 |
d300: 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 | (iCol>=0 && iC
d310: 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 ol<pTab->nCol) )
d320: 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c ;. if( iCol
d330: 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 <0 ){. zC
d340: 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 ol = "rowid";.
d350: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
d360: 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e zCol = pTab->
d370: 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 aCol[iCol].zName
d380: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
d390: 69 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20 if( !shortNames
d3a0: 26 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b && !fullNames ){
d3b0: 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
d3c0: 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 VdbeSetColName(v
d3d0: 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d , i, COLNAME_NAM
d3e0: 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 E, .
d3f0: 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 sqlite3DbStrDup(
d400: 64 62 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d db, pEList->a[i]
d410: 2e 7a 53 70 61 6e 29 2c 20 53 51 4c 49 54 45 5f .zSpan), SQLITE_
d420: 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 DYNAMIC);.
d430: 7d 65 6c 73 65 20 69 66 28 20 66 75 6c 6c 4e 61 }else if( fullNa
d440: 6d 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 63 mes ){. c
d450: 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a har *zName = 0;.
d460: 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 zName =
d470: 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
d480: 62 2c 20 22 25 73 2e 25 73 22 2c 20 70 54 61 62 b, "%s.%s", pTab
d490: 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a ->zName, zCol);.
d4a0: 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
d4b0: 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c dbeSetColName(v,
d4c0: 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 i, COLNAME_NAME
d4d0: 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f , zName, SQLITE_
d4e0: 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 DYNAMIC);.
d4f0: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 }else{. s
d500: 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c qlite3VdbeSetCol
d510: 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 Name(v, i, COLNA
d520: 4d 45 5f 4e 41 4d 45 2c 20 7a 43 6f 6c 2c 20 53 ME_NAME, zCol, S
d530: 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 QLITE_TRANSIENT)
d540: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 ;. }. }e
d550: 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 lse{. const
d560: 20 63 68 61 72 20 2a 7a 20 3d 20 70 45 4c 69 73 char *z = pELis
d570: 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 3b 0a 20 t->a[i].zSpan;.
d580: 20 20 20 20 20 7a 20 3d 20 7a 3d 3d 30 20 3f 20 z = z==0 ?
d590: 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 sqlite3MPrintf(d
d5a0: 62 2c 20 22 63 6f 6c 75 6d 6e 25 64 22 2c 20 69 b, "column%d", i
d5b0: 2b 31 29 20 3a 20 73 71 6c 69 74 65 33 44 62 53 +1) : sqlite3DbS
d5c0: 74 72 44 75 70 28 64 62 2c 20 7a 29 3b 0a 20 20 trDup(db, z);.
d5d0: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 sqlite3VdbeS
d5e0: 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 etColName(v, i,
d5f0: 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 2c COLNAME_NAME, z,
d600: 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 SQLITE_DYNAMIC)
d610: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 67 65 ;. }. }. ge
d620: 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 nerateColumnType
d630: 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 s(pParse, pTabLi
d640: 73 74 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a st, pEList);.}..
d650: 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65 /*.** Given an e
d660: 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 28 xpression list (
d670: 77 68 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 which is really
d680: 74 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72 the list of expr
d690: 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 essions.** that
d6a0: 66 6f 72 6d 20 74 68 65 20 72 65 73 75 6c 74 20 form the result
d6b0: 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 set of a SELECT
d6c0: 73 74 61 74 65 6d 65 6e 74 29 20 63 6f 6d 70 75 statement) compu
d6d0: 74 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a te appropriate.*
d6e0: 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 * column names f
d6f0: 6f 72 20 61 20 74 61 62 6c 65 20 74 68 61 74 20 or a table that
d700: 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 would hold the e
d710: 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a xpression list..
d720: 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 6c 75 6d 6e **.** All column
d730: 20 6e 61 6d 65 73 20 77 69 6c 6c 20 62 65 20 75 names will be u
d740: 6e 69 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c nique..**.** Onl
d750: 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d y the column nam
d760: 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65 64 2e es are computed.
d770: 20 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 2c 20 Column.zType,
d780: 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 2c 0a 2a 2a Column.zColl,.**
d790: 20 61 6e 64 20 6f 74 68 65 72 20 66 69 65 6c 64 and other field
d7a0: 73 20 6f 66 20 43 6f 6c 75 6d 6e 20 61 72 65 20 s of Column are
d7b0: 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 zeroed..**.** Re
d7c0: 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f turn SQLITE_OK o
d7d0: 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 61 n success. If a
d7e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 memory allocati
d7f0: 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c on error occurs,
d800: 0a 2a 2a 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 .** store NULL i
d810: 6e 20 2a 70 61 43 6f 6c 20 61 6e 64 20 30 20 69 n *paCol and 0 i
d820: 6e 20 2a 70 6e 43 6f 6c 20 61 6e 64 20 72 65 74 n *pnCol and ret
d830: 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d urn SQLITE_NOMEM
d840: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 ..*/.static int
d850: 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f selectColumnsFro
d860: 6d 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 mExprList(. Par
d870: 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 se *pParse,
d880: 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 /* Parsing
d890: 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 context */. Exp
d8a0: 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 rList *pEList,
d8b0: 20 20 20 20 20 2f 2a 20 45 78 70 72 20 6c 69 73 /* Expr lis
d8c0: 74 20 66 72 6f 6d 20 77 68 69 63 68 20 74 6f 20 t from which to
d8d0: 64 65 72 69 76 65 20 63 6f 6c 75 6d 6e 20 6e 61 derive column na
d8e0: 6d 65 73 20 2a 2f 0a 20 20 69 31 36 20 2a 70 6e mes */. i16 *pn
d8f0: 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 Col,
d900: 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 /* Write the nu
d910: 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 mber of columns
d920: 68 65 72 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e here */. Column
d930: 20 2a 2a 70 61 43 6f 6c 20 20 20 20 20 20 20 20 **paCol
d940: 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e /* Write the n
d950: 65 77 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20 68 ew column list h
d960: 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 ere */.){. sqli
d970: 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
d980: 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 ->db; /* Datab
d990: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a ase connection *
d9a0: 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 /. int i, j;
d9b0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9c0: 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 /* Loop counters
d9d0: 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 */. int cnt;
d9e0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
d9f0: 20 20 2f 2a 20 49 6e 64 65 78 20 61 64 64 65 64 /* Index added
da00: 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6e 61 6d to make the nam
da10: 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 43 6f e unique */. Co
da20: 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 2a 70 43 6f lumn *aCol, *pCo
da30: 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 l; /* For
da40: 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 72 65 looping over re
da50: 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a sult columns */.
da60: 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20 20 20 int nCol;
da70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
da80: 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d Number of colum
da90: 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 ns in the result
daa0: 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a set */. Expr *
dab0: 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 p;
dac0: 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 /* Express
dad0: 69 6f 6e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 ion for a single
dae0: 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 2a result column *
daf0: 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b /. char *zName;
db00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db10: 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a /* Column name *
db20: 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 /. int nName;
db30: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
db40: 2f 2a 20 53 69 7a 65 20 6f 66 20 6e 61 6d 65 20 /* Size of name
db50: 69 6e 20 7a 4e 61 6d 65 5b 5d 20 2a 2f 0a 0a 20 in zName[] */..
db60: 20 69 66 28 20 70 45 4c 69 73 74 20 29 7b 0a 20 if( pEList ){.
db70: 20 20 20 6e 43 6f 6c 20 3d 20 70 45 4c 69 73 74 nCol = pEList
db80: 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 43 6f ->nExpr;. aCo
db90: 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c l = sqlite3DbMal
dba0: 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 locZero(db, size
dbb0: 6f 66 28 61 43 6f 6c 5b 30 5d 29 2a 6e 43 6f 6c of(aCol[0])*nCol
dbc0: 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 );. testcase(
dbd0: 20 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 7d 65 aCol==0 );. }e
dbe0: 6c 73 65 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 lse{. nCol =
dbf0: 30 3b 0a 20 20 20 20 61 43 6f 6c 20 3d 20 30 3b 0;. aCol = 0;
dc00: 0a 20 20 7d 0a 20 20 2a 70 6e 43 6f 6c 20 3d 20 . }. *pnCol =
dc10: 6e 43 6f 6c 3b 0a 20 20 2a 70 61 43 6f 6c 20 3d nCol;. *paCol =
dc20: 20 61 43 6f 6c 3b 0a 0a 20 20 66 6f 72 28 69 3d aCol;.. for(i=
dc30: 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 0, pCol=aCol; i<
dc40: 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b nCol; i++, pCol+
dc50: 2b 29 7b 0a 20 20 20 20 2f 2a 20 47 65 74 20 61 +){. /* Get a
dc60: 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 6e 61 n appropriate na
dc70: 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d me for the colum
dc80: 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d n. */. p =
dc90: 20 73 71 6c 69 74 65 33 45 78 70 72 53 6b 69 70 sqlite3ExprSkip
dca0: 43 6f 6c 6c 61 74 65 28 70 45 4c 69 73 74 2d 3e Collate(pEList->
dcb0: 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 a[i].pExpr);.
dcc0: 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d 20 70 45 if( (zName = pE
dcd0: 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 List->a[i].zName
dce0: 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a )!=0 ){. /*
dcf0: 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 If the column c
dd00: 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41 53 20 3c ontains an "AS <
dd10: 6e 61 6d 65 3e 22 20 70 68 72 61 73 65 2c 20 75 name>" phrase, u
dd20: 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20 74 68 65 se <name> as the
dd30: 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a name */. z
dd40: 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 Name = sqlite3Db
dd50: 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 StrDup(db, zName
dd60: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 );. }else{.
dd70: 20 20 20 20 45 78 70 72 20 2a 70 43 6f 6c 45 78 Expr *pColEx
dd80: 70 72 20 3d 20 70 3b 20 20 2f 2a 20 54 68 65 20 pr = p; /* The
dd90: 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 expression that
dda0: 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f is the result co
ddb0: 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 lumn name */.
ddc0: 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 Table *pTab;
ddd0: 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
dde0: 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 associated with
ddf0: 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e this expression
de00: 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 */. while(
de10: 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 pColExpr->op==T
de20: 4b 5f 44 4f 54 20 29 7b 0a 20 20 20 20 20 20 20 K_DOT ){.
de30: 20 70 43 6f 6c 45 78 70 72 20 3d 20 70 43 6f 6c pColExpr = pCol
de40: 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 Expr->pRight;.
de50: 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 assert( pC
de60: 6f 6c 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 olExpr!=0 );.
de70: 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 }. if( p
de80: 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ColExpr->op==TK_
de90: 43 4f 4c 55 4d 4e 20 26 26 20 41 4c 57 41 59 53 COLUMN && ALWAYS
dea0: 28 70 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 21 (pColExpr->pTab!
deb0: 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f =0) ){. /
dec0: 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75 73 * For columns us
ded0: 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d e the column nam
dee0: 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 e name */.
def0: 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43 6f int iCol = pCo
df00: 6c 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a lExpr->iColumn;.
df10: 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 pTab = p
df20: 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 ColExpr->pTab;.
df30: 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c if( iCol<
df40: 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 0 ) iCol = pTab-
df50: 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 >iPKey;.
df60: 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d zName = sqlite3M
df70: 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c Printf(db, "%s",
df80: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
df90: 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54 61 62 iCol>=0 ? pTab
dfa0: 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 ->aCol[iCol].zNa
dfb0: 6d 65 20 3a 20 22 72 6f 77 69 64 22 29 3b 0a 20 me : "rowid");.
dfc0: 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 }else if( p
dfd0: 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f ColExpr->op==TK_
dfe0: 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 ID ){. as
dff0: 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 sert( !ExprHasPr
e000: 6f 70 65 72 74 79 28 70 43 6f 6c 45 78 70 72 2c operty(pColExpr,
e010: 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b EP_IntValue) );
e020: 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d . zName =
e030: 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 sqlite3MPrintf(
e040: 64 62 2c 20 22 25 73 22 2c 20 70 43 6f 6c 45 78 db, "%s", pColEx
e050: 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 pr->u.zToken);.
e060: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
e070: 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 6f /* Use the o
e080: 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 riginal text of
e090: 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 the column expre
e0a0: 73 73 69 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d ssion as its nam
e0b0: 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 4e 61 e */. zNa
e0c0: 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 me = sqlite3MPri
e0d0: 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70 45 ntf(db, "%s", pE
e0e0: 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e List->a[i].zSpan
e0f0: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
e100: 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c . if( db->mal
e110: 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 locFailed ){.
e120: 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 sqlite3DbFree
e130: 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 (db, zName);.
e140: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
e150: 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 . /* Make sur
e160: 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d e the column nam
e170: 65 20 69 73 20 75 6e 69 71 75 65 2e 20 20 49 66 e is unique. If
e180: 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 the name is not
e190: 20 75 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20 unique,. **
e1a0: 61 70 70 65 6e 64 20 61 6e 20 69 6e 74 65 67 65 append an intege
e1b0: 72 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f r to the name so
e1c0: 20 74 68 61 74 20 69 74 20 62 65 63 6f 6d 65 73 that it becomes
e1d0: 20 75 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a unique.. */.
e1e0: 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 nName = sqli
e1f0: 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d te3Strlen30(zNam
e200: 65 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 63 6e e);. for(j=cn
e210: 74 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a t=0; j<i; j++){.
e220: 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 if( sqlite
e230: 33 53 74 72 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 3StrICmp(aCol[j]
e240: 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d .zName, zName)==
e250: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 0 ){. cha
e260: 72 20 2a 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 r *zNewName;.
e270: 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 int k;.
e280: 20 20 20 20 66 6f 72 28 6b 3d 6e 4e 61 6d 65 2d for(k=nName-
e290: 31 3b 20 6b 3e 31 20 26 26 20 73 71 6c 69 74 65 1; k>1 && sqlite
e2a0: 33 49 73 64 69 67 69 74 28 7a 4e 61 6d 65 5b 6b 3Isdigit(zName[k
e2b0: 5d 29 3b 20 6b 2d 2d 29 7b 7d 0a 20 20 20 20 20 ]); k--){}.
e2c0: 20 20 20 69 66 28 20 6b 3e 3d 30 20 26 26 20 7a if( k>=0 && z
e2d0: 4e 61 6d 65 5b 6b 5d 3d 3d 27 3a 27 20 29 20 6e Name[k]==':' ) n
e2e0: 4e 61 6d 65 20 3d 20 6b 3b 0a 20 20 20 20 20 20 Name = k;.
e2f0: 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d zName[nName] =
e300: 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65 77 0;. zNew
e310: 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 Name = sqlite3MP
e320: 72 69 6e 74 66 28 64 62 2c 20 22 25 73 3a 25 64 rintf(db, "%s:%d
e330: 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29 ", zName, ++cnt)
e340: 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 ;. sqlite
e350: 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 3DbFree(db, zNam
e360: 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d e);. zNam
e370: 65 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 e = zNewName;.
e380: 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 j = -1;.
e390: 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d if( zName=
e3a0: 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 =0 ) break;.
e3b0: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 }. }. pC
e3c0: 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d ol->zName = zNam
e3d0: 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d e;. }. if( db-
e3e0: 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b >mallocFailed ){
e3f0: 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c . for(j=0; j<
e400: 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73 i; j++){. s
e410: 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c qlite3DbFree(db,
e420: 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3b aCol[j].zName);
e430: 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 . }. sqlit
e440: 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 43 6f e3DbFree(db, aCo
e450: 6c 29 3b 0a 20 20 20 20 2a 70 61 43 6f 6c 20 3d l);. *paCol =
e460: 20 30 3b 0a 20 20 20 20 2a 70 6e 43 6f 6c 20 3d 0;. *pnCol =
e470: 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 0;. return S
e480: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d QLITE_NOMEM;. }
e490: 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 . return SQLITE
e4a0: 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 _OK;.}../*.** Ad
e4b0: 64 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 d type and colla
e4c0: 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e tion information
e4d0: 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c 69 73 to a column lis
e4e0: 74 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61 20 t based on.** a
e4f0: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
e500: 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 6c ..** .** The col
e510: 75 6d 6e 20 6c 69 73 74 20 70 72 65 73 75 6d 61 umn list presuma
e520: 62 6c 79 20 63 61 6d 65 20 66 72 6f 6d 20 73 65 bly came from se
e530: 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 46 lectColumnNamesF
e540: 72 6f 6d 45 78 70 72 4c 69 73 74 28 29 2e 0a 2a romExprList()..*
e550: 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 * The column lis
e560: 74 20 68 61 73 20 6f 6e 6c 79 20 6e 61 6d 65 73 t has only names
e570: 2c 20 6e 6f 74 20 74 79 70 65 73 20 6f 72 20 63 , not types or c
e580: 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 ollations. This
e590: 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 6f 65 73 .** routine goes
e5a0: 20 74 68 72 6f 75 67 68 20 61 6e 64 20 61 64 64 through and add
e5b0: 73 20 74 68 65 20 74 79 70 65 73 20 61 6e 64 20 s the types and
e5c0: 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a collations..**.*
e5d0: 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 * This routine r
e5e0: 65 71 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c equires that all
e5f0: 20 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20 identifiers in
e600: 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 the SELECT.** st
e610: 61 74 65 6d 65 6e 74 20 62 65 20 72 65 73 6f 6c atement be resol
e620: 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 ved..*/.static v
e630: 6f 69 64 20 73 65 6c 65 63 74 41 64 64 43 6f 6c oid selectAddCol
e640: 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74 umnTypeAndCollat
e650: 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 ion(. Parse *pP
e660: 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 arse, /*
e670: 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 73 Parsing contexts
e680: 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 */. Table *pTa
e690: 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 b, /* A
e6a0: 64 64 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 69 dd column type i
e6b0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 nformation to th
e6c0: 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53 65 is table */. Se
e6d0: 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 lect *pSelect
e6e0: 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 75 73 /* SELECT us
e6f0: 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 ed to determine
e700: 74 79 70 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 types and collat
e710: 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c ions */.){. sql
e720: 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 ite3 *db = pPars
e730: 65 2d 3e 64 62 3b 0a 20 20 4e 61 6d 65 43 6f 6e e->db;. NameCon
e740: 74 65 78 74 20 73 4e 43 3b 0a 20 20 43 6f 6c 75 text sNC;. Colu
e750: 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 43 6f 6c 6c mn *pCol;. Coll
e760: 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e Seq *pColl;. in
e770: 74 20 69 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a t i;. Expr *p;.
e780: 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 struct ExprLis
e790: 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 75 36 34 t_item *a;. u64
e7a0: 20 73 7a 41 6c 6c 20 3d 20 30 3b 0a 0a 20 20 61 szAll = 0;.. a
e7b0: 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 21 3d ssert( pSelect!=
e7c0: 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 0 );. assert( (
e7d0: 70 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67 pSelect->selFlag
e7e0: 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 s & SF_Resolved)
e7f0: 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 !=0 );. assert(
e800: 20 70 54 61 62 2d 3e 6e 43 6f 6c 3d 3d 70 53 65 pTab->nCol==pSe
e810: 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 lect->pEList->nE
e820: 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f xpr || db->mallo
e830: 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 cFailed );. if(
e840: 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
e850: 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 d ) return;. me
e860: 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 mset(&sNC, 0, si
e870: 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e zeof(sNC));. sN
e880: 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 C.pSrcList = pSe
e890: 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 61 20 lect->pSrc;. a
e8a0: 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 = pSelect->pELis
e8b0: 74 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 2c t->a;. for(i=0,
e8c0: 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c pCol=pTab->aCol
e8d0: 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 ; i<pTab->nCol;
e8e0: 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 i++, pCol++){.
e8f0: 20 20 70 20 3d 20 61 5b 69 5d 2e 70 45 78 70 72 p = a[i].pExpr
e900: 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 ;. pCol->zTyp
e910: 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 e = sqlite3DbStr
e920: 44 75 70 28 64 62 2c 20 63 6f 6c 75 6d 6e 54 79 Dup(db, columnTy
e930: 70 65 28 26 73 4e 43 2c 20 70 2c 30 2c 30 2c 30 pe(&sNC, p,0,0,0
e940: 2c 20 26 70 43 6f 6c 2d 3e 73 7a 45 73 74 29 29 , &pCol->szEst))
e950: 3b 0a 20 20 20 20 73 7a 41 6c 6c 20 2b 3d 20 70 ;. szAll += p
e960: 43 6f 6c 2d 3e 73 7a 45 73 74 3b 0a 20 20 20 20 Col->szEst;.
e970: 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d pCol->affinity =
e980: 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 sqlite3ExprAffi
e990: 6e 69 74 79 28 70 29 3b 0a 20 20 20 20 69 66 28 nity(p);. if(
e9a0: 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d pCol->affinity=
e9b0: 3d 30 20 29 20 70 43 6f 6c 2d 3e 61 66 66 69 6e =0 ) pCol->affin
e9c0: 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 ity = SQLITE_AFF
e9d0: 5f 4e 4f 4e 45 3b 0a 20 20 20 20 70 43 6f 6c 6c _NONE;. pColl
e9e0: 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
e9f0: 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 29 llSeq(pParse, p)
ea00: 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 ;. if( pColl
ea10: 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a ){. pCol->z
ea20: 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 Coll = sqlite3Db
ea30: 53 74 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 6c StrDup(db, pColl
ea40: 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a ->zName);. }.
ea50: 20 20 7d 0a 20 20 70 54 61 62 2d 3e 73 7a 54 61 }. pTab->szTa
ea60: 62 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 4c 6f bRow = sqlite3Lo
ea70: 67 45 73 74 28 73 7a 41 6c 6c 2a 34 29 3b 0a 7d gEst(szAll*4);.}
ea80: 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 ../*.** Given a
ea90: 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
eaa0: 2c 20 67 65 6e 65 72 61 74 65 20 61 20 54 61 62 , generate a Tab
eab0: 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 le structure tha
eac0: 74 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 74 t describes.** t
ead0: 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 he result set of
eae0: 20 74 68 61 74 20 53 45 4c 45 43 54 2e 0a 2a 2f that SELECT..*/
eaf0: 0a 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 .Table *sqlite3R
eb00: 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 esultSetOfSelect
eb10: 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 (Parse *pParse,
eb20: 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 Select *pSelect)
eb30: 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b {. Table *pTab;
eb40: 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d . sqlite3 *db =
eb50: 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 pParse->db;. i
eb60: 6e 74 20 73 61 76 65 64 46 6c 61 67 73 3b 0a 0a nt savedFlags;..
eb70: 20 20 73 61 76 65 64 46 6c 61 67 73 20 3d 20 64 savedFlags = d
eb80: 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 64 62 2d 3e b->flags;. db->
eb90: 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 flags &= ~SQLITE
eba0: 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 3b 0a 20 _FullColNames;.
ebb0: 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 db->flags |= SQ
ebc0: 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d LITE_ShortColNam
ebd0: 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c es;. sqlite3Sel
ebe0: 65 63 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 ectPrep(pParse,
ebf0: 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69 pSelect, 0);. i
ec00: 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 f( pParse->nErr
ec10: 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 ) return 0;. wh
ec20: 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 ile( pSelect->pP
ec30: 72 69 6f 72 20 29 20 70 53 65 6c 65 63 74 20 3d rior ) pSelect =
ec40: 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 pSelect->pPrior
ec50: 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 ;. db->flags =
ec60: 73 61 76 65 64 46 6c 61 67 73 3b 0a 20 20 70 54 savedFlags;. pT
ec70: 61 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 ab = sqlite3DbMa
ec80: 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a llocZero(db, siz
ec90: 65 6f 66 28 54 61 62 6c 65 29 20 29 3b 0a 20 20 eof(Table) );.
eca0: 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 if( pTab==0 ){.
ecb0: 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
ecc0: 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 . /* The sqlite
ecd0: 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 3ResultSetOfSele
ece0: 63 74 28 29 20 69 73 20 6f 6e 6c 79 20 75 73 65 ct() is only use
ecf0: 64 20 6e 20 63 6f 6e 74 65 78 74 73 20 77 68 65 d n contexts whe
ed00: 72 65 20 6c 6f 6f 6b 61 73 69 64 65 0a 20 20 2a re lookaside. *
ed10: 2a 20 69 73 20 64 69 73 61 62 6c 65 64 20 2a 2f * is disabled */
ed20: 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c . assert( db->l
ed30: 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 ookaside.bEnable
ed40: 64 3d 3d 30 20 29 3b 0a 20 20 70 54 61 62 2d 3e d==0 );. pTab->
ed50: 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62 nRef = 1;. pTab
ed60: 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70 ->zName = 0;. p
ed70: 54 61 62 2d 3e 6e 52 6f 77 4c 6f 67 45 73 74 20 Tab->nRowLogEst
ed80: 3d 20 32 30 30 3b 20 61 73 73 65 72 74 28 20 32 = 200; assert( 2
ed90: 30 30 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 00==sqlite3LogEs
eda0: 74 28 31 30 34 38 35 37 36 29 20 29 3b 0a 20 20 t(1048576) );.
edb0: 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f selectColumnsFro
edc0: 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 mExprList(pParse
edd0: 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 , pSelect->pELis
ede0: 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 t, &pTab->nCol,
edf0: 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 &pTab->aCol);.
ee00: 73 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 selectAddColumnT
ee10: 79 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 ypeAndCollation(
ee20: 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 53 pParse, pTab, pS
ee30: 65 6c 65 63 74 29 3b 0a 20 20 70 54 61 62 2d 3e elect);. pTab->
ee40: 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 69 66 iPKey = -1;. if
ee50: 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
ee60: 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 ed ){. sqlite
ee70: 33 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 3DeleteTable(db,
ee80: 20 70 54 61 62 29 3b 0a 20 20 20 20 72 65 74 75 pTab);. retu
ee90: 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 rn 0;. }. retu
eea0: 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pTab;.}../*.*
eeb0: 2a 20 47 65 74 20 61 20 56 44 42 45 20 66 6f 72 * Get a VDBE for
eec0: 20 74 68 65 20 67 69 76 65 6e 20 70 61 72 73 65 the given parse
eed0: 72 20 63 6f 6e 74 65 78 74 2e 20 20 43 72 65 61 r context. Crea
eee0: 74 65 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 te a new one if
eef0: 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 49 66 necessary..** If
ef00: 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 an error occurs
ef10: 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20 61 6e , return NULL an
ef20: 64 20 6c 65 61 76 65 20 61 20 6d 65 73 73 61 67 d leave a messag
ef30: 65 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a e in pParse..*/.
ef40: 56 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 Vdbe *sqlite3Get
ef50: 56 64 62 65 28 50 61 72 73 65 20 2a 70 50 61 72 Vdbe(Parse *pPar
ef60: 73 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d se){. Vdbe *v =
ef70: 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a pParse->pVdbe;.
ef80: 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 if( v==0 ){.
ef90: 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 v = pParse->pV
efa0: 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 dbe = sqlite3Vdb
efb0: 65 43 72 65 61 74 65 28 70 50 61 72 73 65 29 3b eCreate(pParse);
efc0: 0a 20 20 20 20 69 66 28 20 76 20 29 20 73 71 6c . if( v ) sql
efd0: 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 ite3VdbeAddOp0(v
efe0: 2c 20 4f 50 5f 49 6e 69 74 29 3b 0a 20 20 20 20 , OP_Init);.
eff0: 69 66 28 20 70 50 61 72 73 65 2d 3e 70 54 6f 70 if( pParse->pTop
f000: 6c 65 76 65 6c 3d 3d 30 0a 20 20 20 20 20 26 26 level==0. &&
f010: 20 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 OptimizationEna
f020: 62 6c 65 64 28 70 50 61 72 73 65 2d 3e 64 62 2c bled(pParse->db,
f030: 53 51 4c 49 54 45 5f 46 61 63 74 6f 72 4f 75 74 SQLITE_FactorOut
f040: 43 6f 6e 73 74 29 0a 20 20 20 20 29 7b 0a 20 20 Const). ){.
f050: 20 20 20 20 70 50 61 72 73 65 2d 3e 6f 6b 43 6f pParse->okCo
f060: 6e 73 74 46 61 63 74 6f 72 20 3d 20 31 3b 0a 20 nstFactor = 1;.
f070: 20 20 20 7d 0a 0a 20 20 7d 0a 20 20 72 65 74 75 }.. }. retu
f080: 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 rn v;.}.../*.**
f090: 43 6f 6d 70 75 74 65 20 74 68 65 20 69 4c 69 6d Compute the iLim
f0a0: 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 66 it and iOffset f
f0b0: 69 65 6c 64 73 20 6f 66 20 74 68 65 20 53 45 4c ields of the SEL
f0c0: 45 43 54 20 62 61 73 65 64 20 6f 6e 20 74 68 65 ECT based on the
f0d0: 0a 2a 2a 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 .** pLimit and p
f0e0: 4f 66 66 73 65 74 20 65 78 70 72 65 73 73 69 6f Offset expressio
f0f0: 6e 73 2e 20 20 70 4c 69 6d 69 74 20 61 6e 64 20 ns. pLimit and
f100: 70 4f 66 66 73 65 74 20 68 6f 6c 64 20 74 68 65 pOffset hold the
f110: 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 expressions.**
f120: 74 68 61 74 20 61 70 70 65 61 72 20 69 6e 20 74 that appear in t
f130: 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 he original SQL
f140: 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 statement after
f150: 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 the LIMIT and OF
f160: 46 53 45 54 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 FSET.** keywords
f170: 2e 20 20 4f 72 20 4e 55 4c 4c 20 69 66 20 74 68 . Or NULL if th
f180: 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 61 72 65 ose keywords are
f190: 20 6f 6d 69 74 74 65 64 2e 20 69 4c 69 6d 69 74 omitted. iLimit
f1a0: 20 61 6e 64 20 69 4f 66 66 73 65 74 20 0a 2a 2a and iOffset .**
f1b0: 20 61 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 are the integer
f1c0: 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 memory register
f1d0: 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 63 6f 75 numbers for cou
f1e0: 6e 74 65 72 73 20 75 73 65 64 20 74 6f 20 63 6f nters used to co
f1f0: 6d 70 75 74 65 20 0a 2a 2a 20 74 68 65 20 6c 69 mpute .** the li
f200: 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74 2e 20 mit and offset.
f210: 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 If there is no
f220: 6c 69 6d 69 74 20 61 6e 64 2f 6f 72 20 6f 66 66 limit and/or off
f230: 73 65 74 2c 20 74 68 65 6e 20 0a 2a 2a 20 69 4c set, then .** iL
f240: 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 imit and iOffset
f250: 20 61 72 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a are negative..*
f260: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
f270: 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 76 61 e changes the va
f280: 6c 75 65 73 20 6f 66 20 69 4c 69 6d 69 74 20 61 lues of iLimit a
f290: 6e 64 20 69 4f 66 66 73 65 74 20 6f 6e 6c 79 20 nd iOffset only
f2a0: 69 66 0a 2a 2a 20 61 20 6c 69 6d 69 74 20 6f 72 if.** a limit or
f2b0: 20 6f 66 66 73 65 74 20 69 73 20 64 65 66 69 6e offset is defin
f2c0: 65 64 20 62 79 20 70 4c 69 6d 69 74 20 61 6e 64 ed by pLimit and
f2d0: 20 70 4f 66 66 73 65 74 2e 20 20 69 4c 69 6d 69 pOffset. iLimi
f2e0: 74 20 61 6e 64 0a 2a 2a 20 69 4f 66 66 73 65 74 t and.** iOffset
f2f0: 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 should have bee
f300: 6e 20 70 72 65 73 65 74 20 74 6f 20 61 70 70 72 n preset to appr
f310: 6f 70 72 69 61 74 65 20 64 65 66 61 75 6c 74 20 opriate default
f320: 76 61 6c 75 65 73 20 28 7a 65 72 6f 29 0a 2a 2a values (zero).**
f330: 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e prior to callin
f340: 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a g this routine..
f350: 2a 2a 0a 2a 2a 20 54 68 65 20 69 4f 66 66 73 65 **.** The iOffse
f360: 74 20 72 65 67 69 73 74 65 72 20 28 69 66 20 69 t register (if i
f370: 74 20 65 78 69 73 74 73 29 20 69 73 20 69 6e 69 t exists) is ini
f380: 74 69 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 tialized to the
f390: 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 value.** of the
f3a0: 4f 46 46 53 45 54 2e 20 20 54 68 65 20 69 4c 69 OFFSET. The iLi
f3b0: 6d 69 74 20 72 65 67 69 73 74 65 72 20 69 73 20 mit register is
f3c0: 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 4c initialized to L
f3d0: 49 4d 49 54 2e 20 20 52 65 67 69 73 74 65 72 0a IMIT. Register.
f3e0: 2a 2a 20 69 4f 66 66 73 65 74 2b 31 20 69 73 20 ** iOffset+1 is
f3f0: 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 4c initialized to L
f400: 49 4d 49 54 2b 4f 46 46 53 45 54 2e 0a 2a 2a 0a IMIT+OFFSET..**.
f410: 2a 2a 20 4f 6e 6c 79 20 69 66 20 70 4c 69 6d 69 ** Only if pLimi
f420: 74 21 3d 30 20 6f 72 20 70 4f 66 66 73 65 74 21 t!=0 or pOffset!
f430: 3d 30 20 64 6f 20 74 68 65 20 6c 69 6d 69 74 20 =0 do the limit
f440: 72 65 67 69 73 74 65 72 73 20 67 65 74 0a 2a 2a registers get.**
f450: 20 72 65 64 65 66 69 6e 65 64 2e 20 20 54 68 65 redefined. The
f460: 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 UNION ALL opera
f470: 74 6f 72 20 75 73 65 73 20 74 68 69 73 20 70 72 tor uses this pr
f480: 6f 70 65 72 74 79 20 74 6f 20 66 6f 72 63 65 0a operty to force.
f490: 2a 2a 20 74 68 65 20 72 65 75 73 65 20 6f 66 20 ** the reuse of
f4a0: 74 68 65 20 73 61 6d 65 20 6c 69 6d 69 74 20 61 the same limit a
f4b0: 6e 64 20 6f 66 66 73 65 74 20 72 65 67 69 73 74 nd offset regist
f4c0: 65 72 73 20 61 63 72 6f 73 73 20 6d 75 6c 74 69 ers across multi
f4d0: 70 6c 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 ple.** SELECT st
f4e0: 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 atements..*/.sta
f4f0: 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 tic void compute
f500: 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 50 LimitRegisters(P
f510: 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 arse *pParse, Se
f520: 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 42 72 lect *p, int iBr
f530: 65 61 6b 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 eak){. Vdbe *v
f540: 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4c 69 6d 69 = 0;. int iLimi
f550: 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4f 66 t = 0;. int iOf
f560: 66 73 65 74 3b 0a 20 20 69 6e 74 20 61 64 64 72 fset;. int addr
f570: 31 2c 20 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 1, n;. if( p->i
f580: 4c 69 6d 69 74 20 29 20 72 65 74 75 72 6e 3b 0a Limit ) return;.
f590: 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d . /* . ** "LIM
f5a0: 49 54 20 2d 31 22 20 61 6c 77 61 79 73 20 73 68 IT -1" always sh
f5b0: 6f 77 73 20 61 6c 6c 20 72 6f 77 73 2e 20 20 54 ows all rows. T
f5c0: 68 65 72 65 20 69 73 20 73 6f 6d 65 0a 20 20 2a here is some. *
f5d0: 2a 20 63 6f 6e 74 72 6f 76 65 72 73 79 20 61 62 * controversy ab
f5e0: 6f 75 74 20 77 68 61 74 20 74 68 65 20 63 6f 72 out what the cor
f5f0: 72 65 63 74 20 62 65 68 61 76 69 6f 72 20 73 68 rect behavior sh
f600: 6f 75 6c 64 20 62 65 2e 0a 20 20 2a 2a 20 54 68 ould be.. ** Th
f610: 65 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d e current implem
f620: 65 6e 74 61 74 69 6f 6e 20 69 6e 74 65 72 70 72 entation interpr
f630: 65 74 73 20 22 4c 49 4d 49 54 20 30 22 20 74 6f ets "LIMIT 0" to
f640: 20 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f mean. ** no ro
f650: 77 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 ws.. */. sqlit
f660: 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 e3ExprCacheClear
f670: 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 (pParse);. asse
f680: 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d 3d rt( p->pOffset==
f690: 30 20 7c 7c 20 70 2d 3e 70 4c 69 6d 69 74 21 3d 0 || p->pLimit!=
f6a0: 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4c 0 );. if( p->pL
f6b0: 69 6d 69 74 20 29 7b 0a 20 20 20 20 70 2d 3e 69 imit ){. p->i
f6c0: 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74 20 3d Limit = iLimit =
f6d0: 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
f6e0: 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 . v = sqlite3
f6f0: 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b GetVdbe(pParse);
f700: 0a 20 20 20 20 61 73 73 65 72 74 28 20 76 21 3d . assert( v!=
f710: 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 0 );. if( sql
f720: 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 ite3ExprIsIntege
f730: 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20 26 6e 29 r(p->pLimit, &n)
f740: 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ){. sqlite
f750: 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
f760: 50 5f 49 6e 74 65 67 65 72 2c 20 6e 2c 20 69 4c P_Integer, n, iL
f770: 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 56 64 62 imit);. Vdb
f780: 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 eComment((v, "LI
f790: 4d 49 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a MIT counter"));.
f7a0: 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 if( n==0 )
f7b0: 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
f7c0: 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
f7d0: 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 72 65 61 P_Goto, 0, iBrea
f7e0: 6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 k);. }else
f7f0: 69 66 28 20 6e 3e 3d 30 20 26 26 20 70 2d 3e 6e if( n>=0 && p->n
f800: 53 65 6c 65 63 74 52 6f 77 3e 28 75 36 34 29 6e SelectRow>(u64)n
f810: 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e ){. p->n
f820: 53 65 6c 65 63 74 52 6f 77 20 3d 20 6e 3b 0a 20 SelectRow = n;.
f830: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
f840: 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 {. sqlite3E
f850: 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 xprCode(pParse,
f860: 70 2d 3e 70 4c 69 6d 69 74 2c 20 69 4c 69 6d 69 p->pLimit, iLimi
f870: 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 t);. sqlite
f880: 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 3VdbeAddOp1(v, O
f890: 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4c 69 P_MustBeInt, iLi
f8a0: 6d 69 74 29 3b 20 56 64 62 65 43 6f 76 65 72 61 mit); VdbeCovera
f8b0: 67 65 28 76 29 3b 0a 20 20 20 20 20 20 56 64 62 ge(v);. Vdb
f8c0: 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49 eComment((v, "LI
f8d0: 4d 49 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a MIT counter"));.
f8e0: 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
f8f0: 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 eAddOp2(v, OP_If
f900: 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20 69 42 Zero, iLimit, iB
f910: 72 65 61 6b 29 3b 20 56 64 62 65 43 6f 76 65 72 reak); VdbeCover
f920: 61 67 65 28 76 29 3b 0a 20 20 20 20 7d 0a 20 20 age(v);. }.
f930: 20 20 69 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 if( p->pOffset
f940: 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 ){. p->iOf
f950: 66 73 65 74 20 3d 20 69 4f 66 66 73 65 74 20 3d fset = iOffset =
f960: 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b ++pParse->nMem;
f970: 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e . pParse->n
f980: 4d 65 6d 2b 2b 3b 20 20 20 2f 2a 20 41 6c 6c 6f Mem++; /* Allo
f990: 63 61 74 65 20 61 6e 20 65 78 74 72 61 20 72 65 cate an extra re
f9a0: 67 69 73 74 65 72 20 66 6f 72 20 6c 69 6d 69 74 gister for limit
f9b0: 2b 6f 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 20 +offset */.
f9c0: 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 sqlite3ExprCode
f9d0: 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f 66 66 (pParse, p->pOff
f9e0: 73 65 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 set, iOffset);.
f9f0: 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
fa00: 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 AddOp1(v, OP_Mus
fa10: 74 42 65 49 6e 74 2c 20 69 4f 66 66 73 65 74 29 tBeInt, iOffset)
fa20: 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 ; VdbeCoverage(v
fa30: 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d );. VdbeCom
fa40: 6d 65 6e 74 28 28 76 2c 20 22 4f 46 46 53 45 54 ment((v, "OFFSET
fa50: 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 counter"));.
fa60: 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 addr1 = sqlit
fa70: 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 e3VdbeAddOp1(v,
fa80: 4f 50 5f 49 66 50 6f 73 2c 20 69 4f 66 66 73 65 OP_IfPos, iOffse
fa90: 74 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 t); VdbeCoverage
faa0: 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 (v);. sqlit
fab0: 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
fac0: 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 OP_Integer, 0, i
fad0: 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20 73 Offset);. s
fae0: 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
faf0: 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 re(v, addr1);.
fb00: 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
fb10: 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c ddOp3(v, OP_Add,
fb20: 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 iLimit, iOffset
fb30: 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 , iOffset+1);.
fb40: 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
fb50: 28 76 2c 20 22 4c 49 4d 49 54 2b 4f 46 46 53 45 (v, "LIMIT+OFFSE
fb60: 54 22 29 29 3b 0a 20 20 20 20 20 20 61 64 64 72 T"));. addr
fb70: 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 1 = sqlite3VdbeA
fb80: 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f ddOp1(v, OP_IfPo
fb90: 73 2c 20 69 4c 69 6d 69 74 29 3b 20 56 64 62 65 s, iLimit); Vdbe
fba0: 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 Coverage(v);.
fbb0: 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
fbc0: 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 dOp2(v, OP_Integ
fbd0: 65 72 2c 20 2d 31 2c 20 69 4f 66 66 73 65 74 2b er, -1, iOffset+
fbe0: 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 1);. sqlite
fbf0: 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
fc00: 20 61 64 64 72 31 29 3b 0a 20 20 20 20 7d 0a 20 addr1);. }.
fc10: 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 }.}..#ifndef SQ
fc20: 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 LITE_OMIT_COMPOU
fc30: 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 ND_SELECT./*.**
fc40: 52 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f Return the appro
fc50: 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67 priate collating
fc60: 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 sequence for th
fc70: 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e e iCol-th column
fc80: 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c of.** the resul
fc90: 74 20 73 65 74 20 66 6f 72 20 74 68 65 20 63 6f t set for the co
fca0: 6d 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 73 74 mpound-select st
fcb0: 61 74 65 6d 65 6e 74 20 22 70 22 2e 20 20 52 65 atement "p". Re
fcc0: 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 turn NULL if.**
fcd0: 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 6e the column has n
fce0: 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 o default collat
fcf0: 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a ing sequence..**
fd00: 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e .** The collatin
fd10: 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 g sequence for t
fd20: 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 he compound sele
fd30: 63 74 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d ct is taken from
fd40: 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 the.** left-mos
fd50: 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 73 65 t term of the se
fd60: 6c 65 63 74 20 74 68 61 74 20 68 61 73 20 61 20 lect that has a
fd70: 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
fd80: 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f ce..*/.static Co
fd90: 6c 6c 53 65 71 20 2a 6d 75 6c 74 69 53 65 6c 65 llSeq *multiSele
fda0: 63 74 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 ctCollSeq(Parse
fdb0: 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 *pParse, Select
fdc0: 2a 70 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 *p, int iCol){.
fdd0: 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 74 3b 0a CollSeq *pRet;.
fde0: 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 if( p->pPrior
fdf0: 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 6d 75 ){. pRet = mu
fe00: 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 ltiSelectCollSeq
fe10: 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72 69 (pParse, p->pPri
fe20: 6f 72 2c 20 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c or, iCol);. }el
fe30: 73 65 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 30 se{. pRet = 0
fe40: 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 ;. }. assert(
fe50: 69 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20 69 66 28 iCol>=0 );. if(
fe60: 20 70 52 65 74 3d 3d 30 20 26 26 20 69 43 6f 6c pRet==0 && iCol
fe70: 3c 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 <p->pEList->nExp
fe80: 72 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 r ){. pRet =
fe90: 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 sqlite3ExprCollS
fea0: 65 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 eq(pParse, p->pE
feb0: 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 List->a[iCol].pE
fec0: 78 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 xpr);. }. retu
fed0: 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a rn pRet;.}../*.*
fee0: 2a 20 54 68 65 20 73 65 6c 65 63 74 20 73 74 61 * The select sta
fef0: 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73 tement passed as
ff00: 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 the second para
ff10: 6d 65 74 65 72 20 69 73 20 61 20 63 6f 6d 70 6f meter is a compo
ff20: 75 6e 64 20 53 45 4c 45 43 54 0a 2a 2a 20 77 69 und SELECT.** wi
ff30: 74 68 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 th an ORDER BY c
ff40: 6c 61 75 73 65 2e 20 54 68 69 73 20 66 75 6e 63 lause. This func
ff50: 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 tion allocates a
ff60: 6e 64 20 72 65 74 75 72 6e 73 20 61 20 4b 65 79 nd returns a Key
ff70: 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72 Info.** structur
ff80: 65 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 69 e suitable for i
ff90: 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 mplementing the
ffa0: 4f 52 44 45 52 20 42 59 2e 0a 2a 2a 0a 2a 2a 20 ORDER BY..**.**
ffb0: 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 Space to hold th
ffc0: 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 e KeyInfo struct
ffd0: 75 72 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 ure is obtained
ffe0: 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 54 68 65 from malloc. The
fff0: 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 calling.** func
10000 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 tion is responsi
10010 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 ble for ensuring
10020 20 74 68 61 74 20 74 68 69 73 20 73 74 72 75 63 that this struc
10030 74 75 72 65 20 69 73 20 65 76 65 6e 74 75 61 6c ture is eventual
10040 6c 79 0a 2a 2a 20 66 72 65 65 64 2e 0a 2a 2f 0a ly.** freed..*/.
10050 73 74 61 74 69 63 20 4b 65 79 49 6e 66 6f 20 2a static KeyInfo *
10060 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 multiSelectOrder
10070 42 79 4b 65 79 49 6e 66 6f 28 50 61 72 73 65 20 ByKeyInfo(Parse
10080 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 *pParse, Select
10090 2a 70 2c 20 69 6e 74 20 6e 45 78 74 72 61 29 7b *p, int nExtra){
100a0 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 . ExprList *pOr
100b0 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 derBy = p->pOrde
100c0 72 42 79 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65 rBy;. int nOrde
100d0 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 rBy = p->pOrderB
100e0 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 73 71 6c 69 y->nExpr;. sqli
100f0 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
10100 2d 3e 64 62 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 ->db;. KeyInfo
10110 2a 70 52 65 74 20 3d 20 73 71 6c 69 74 65 33 4b *pRet = sqlite3K
10120 65 79 49 6e 66 6f 41 6c 6c 6f 63 28 64 62 2c 20 eyInfoAlloc(db,
10130 6e 4f 72 64 65 72 42 79 2b 6e 45 78 74 72 61 2c nOrderBy+nExtra,
10140 20 31 29 3b 0a 20 20 69 66 28 20 70 52 65 74 20 1);. if( pRet
10150 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 ){. int i;.
10160 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72 for(i=0; i<nOr
10170 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20 derBy; i++){.
10180 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
10190 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d st_item *pItem =
101a0 20 26 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d &pOrderBy->a[i]
101b0 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 ;. Expr *pT
101c0 65 72 6d 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 erm = pItem->pEx
101d0 70 72 3b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 pr;. CollSe
101e0 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 20 20 20 q *pColl;..
101f0 20 69 66 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 if( pTerm->flag
10200 73 20 26 20 45 50 5f 43 6f 6c 6c 61 74 65 20 29 s & EP_Collate )
10210 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 {. pColl
10220 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c = sqlite3ExprCol
10230 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 54 65 lSeq(pParse, pTe
10240 72 6d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 rm);. }else
10250 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 {. pColl
10260 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f 6c = multiSelectCol
10270 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2c 20 lSeq(pParse, p,
10280 70 49 74 65 6d 2d 3e 75 2e 78 2e 69 4f 72 64 65 pItem->u.x.iOrde
10290 72 42 79 43 6f 6c 2d 31 29 3b 0a 20 20 20 20 20 rByCol-1);.
102a0 20 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30 20 if( pColl==0
102b0 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 ) pColl = db->pD
102c0 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 fltColl;.
102d0 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e pOrderBy->a[i].
102e0 70 45 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20 pExpr =.
102f0 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 64 64 sqlite3ExprAdd
10300 43 6f 6c 6c 61 74 65 53 74 72 69 6e 67 28 70 50 CollateString(pP
10310 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70 43 6f arse, pTerm, pCo
10320 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ll->zName);.
10330 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 }. assert
10340 28 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 6f ( sqlite3KeyInfo
10350 49 73 57 72 69 74 65 61 62 6c 65 28 70 52 65 74 IsWriteable(pRet
10360 29 20 29 3b 0a 20 20 20 20 20 20 70 52 65 74 2d ) );. pRet-
10370 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c >aColl[i] = pCol
10380 6c 3b 0a 20 20 20 20 20 20 70 52 65 74 2d 3e 61 l;. pRet->a
10390 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 SortOrder[i] = p
103a0 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f OrderBy->a[i].so
103b0 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20 rtOrder;. }.
103c0 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 52 65 }.. return pRe
103d0 74 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 t;.}..#ifndef SQ
103e0 4c 49 54 45 5f 4f 4d 49 54 5f 43 54 45 0a 2f 2a LITE_OMIT_CTE./*
103f0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 .** This routine
10400 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 generates VDBE
10410 63 6f 64 65 20 74 6f 20 63 6f 6d 70 75 74 65 20 code to compute
10420 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 the content of a
10430 20 57 49 54 48 20 52 45 43 55 52 53 49 56 45 0a WITH RECURSIVE.
10440 2a 2a 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 ** query of the
10450 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 3c 72 form:.**.** <r
10460 65 63 75 72 73 69 76 65 2d 74 61 62 6c 65 3e 20 ecursive-table>
10470 41 53 20 28 3c 73 65 74 75 70 2d 71 75 65 72 79 AS (<setup-query
10480 3e 20 55 4e 49 4f 4e 20 5b 41 4c 4c 5d 20 3c 72 > UNION [ALL] <r
10490 65 63 75 72 73 69 76 65 2d 71 75 65 72 79 3e 29 ecursive-query>)
104a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
104b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f \___
104c0 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 ________/
104d0 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f \_________
104e0 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 ______/.**
104f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10500 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 20 p->pPrior
10510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10520 20 20 20 20 70 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 p.**.**.** T
10530 68 65 72 65 20 69 73 20 65 78 61 63 74 6c 79 20 here is exactly
10540 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f one reference to
10550 20 74 68 65 20 72 65 63 75 72 73 69 76 65 2d 74 the recursive-t
10560 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d able in the FROM
10570 20 63 6c 61 75 73 65 0a 2a 2a 20 6f 66 20 72 65 clause.** of re
10580 63 75 72 73 69 76 65 2d 71 75 65 72 79 2c 20 6d cursive-query, m
10590 61 72 6b 65 64 20 77 69 74 68 20 74 68 65 20 53 arked with the S
105a0 72 63 4c 69 73 74 2d 3e 61 5b 5d 2e 69 73 52 65 rcList->a[].isRe
105b0 63 75 72 73 69 76 65 20 66 6c 61 67 2e 0a 2a 2a cursive flag..**
105c0 0a 2a 2a 20 54 68 65 20 73 65 74 75 70 2d 71 75 .** The setup-qu
105d0 65 72 79 20 72 75 6e 73 20 6f 6e 63 65 20 74 6f ery runs once to
105e0 20 67 65 6e 65 72 61 74 65 20 61 6e 20 69 6e 69 generate an ini
105f0 74 69 61 6c 20 73 65 74 20 6f 66 20 72 6f 77 73 tial set of rows
10600 20 74 68 61 74 20 67 6f 0a 2a 2a 20 69 6e 74 6f that go.** into
10610 20 61 20 51 75 65 75 65 20 74 61 62 6c 65 2e 20 a Queue table.
10620 20 52 6f 77 73 20 61 72 65 20 65 78 74 72 61 63 Rows are extrac
10630 74 65 64 20 66 72 6f 6d 20 74 68 65 20 51 75 65 ted from the Que
10640 75 65 20 74 61 62 6c 65 20 6f 6e 65 20 62 79 0a ue table one by.
10650 2a 2a 20 6f 6e 65 2e 20 20 45 61 63 68 20 72 6f ** one. Each ro
10660 77 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d w extracted from
10670 20 51 75 65 75 65 20 69 73 20 6f 75 74 70 75 74 Queue is output
10680 20 74 6f 20 70 44 65 73 74 2e 20 20 54 68 65 6e to pDest. Then
10690 20 74 68 65 20 73 69 6e 67 6c 65 0a 2a 2a 20 65 the single.** e
106a0 78 74 72 61 63 74 65 64 20 72 6f 77 20 28 6e 6f xtracted row (no
106b0 77 20 69 6e 20 74 68 65 20 69 43 75 72 72 65 6e w in the iCurren
106c0 74 20 74 61 62 6c 65 29 20 62 65 63 6f 6d 65 73 t table) becomes
106d0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 the content of
106e0 74 68 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 the.** recursive
106f0 2d 74 61 62 6c 65 20 66 6f 72 20 61 20 72 65 63 -table for a rec
10700 75 72 73 69 76 65 2d 71 75 65 72 79 20 72 75 6e ursive-query run
10710 2e 20 20 54 68 65 20 6f 75 74 70 75 74 20 6f 66 . The output of
10720 20 74 68 65 20 72 65 63 75 72 73 69 76 65 2d 71 the recursive-q
10730 75 65 72 79 0a 2a 2a 20 69 73 20 61 64 64 65 64 uery.** is added
10740 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 51 back into the Q
10750 75 65 75 65 20 74 61 62 6c 65 2e 20 20 54 68 65 ueue table. The
10760 6e 20 61 6e 6f 74 68 65 72 20 72 6f 77 20 69 73 n another row is
10770 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 extracted from
10780 51 75 65 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65 Queue.** and the
10790 20 69 74 65 72 61 74 69 6f 6e 20 63 6f 6e 74 69 iteration conti
107a0 6e 75 65 73 20 75 6e 74 69 6c 20 74 68 65 20 51 nues until the Q
107b0 75 65 75 65 20 74 61 62 6c 65 20 69 73 20 65 6d ueue table is em
107c0 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 pty..**.** If th
107d0 65 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 e compound query
107e0 20 6f 70 65 72 61 74 6f 72 20 69 73 20 55 4e 49 operator is UNI
107f0 4f 4e 20 74 68 65 6e 20 6e 6f 20 64 75 70 6c 69 ON then no dupli
10800 63 61 74 65 20 72 6f 77 73 20 61 72 65 20 65 76 cate rows are ev
10810 65 72 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 er.** inserted i
10820 6e 74 6f 20 74 68 65 20 51 75 65 75 65 20 74 61 nto the Queue ta
10830 62 6c 65 2e 20 20 54 68 65 20 69 44 69 73 74 69 ble. The iDisti
10840 6e 63 74 20 74 61 62 6c 65 20 6b 65 65 70 73 20 nct table keeps
10850 61 20 63 6f 70 79 20 6f 66 20 61 6c 6c 20 72 6f a copy of all ro
10860 77 73 0a 2a 2a 20 74 68 61 74 20 68 61 76 65 20 ws.** that have
10870 65 76 65 72 20 62 65 65 6e 20 69 6e 73 65 72 74 ever been insert
10880 65 64 20 69 6e 74 6f 20 51 75 65 75 65 20 61 6e ed into Queue an
10890 64 20 63 61 75 73 65 73 20 64 75 70 6c 69 63 61 d causes duplica
108a0 74 65 73 20 74 6f 20 62 65 0a 2a 2a 20 64 69 73 tes to be.** dis
108b0 63 61 72 64 65 64 2e 20 20 49 66 20 74 68 65 20 carded. If the
108c0 6f 70 65 72 61 74 6f 72 20 69 73 20 55 4e 49 4f operator is UNIO
108d0 4e 20 41 4c 4c 2c 20 74 68 65 6e 20 64 75 70 6c N ALL, then dupl
108e0 69 63 61 74 65 73 20 61 72 65 20 61 6c 6c 6f 77 icates are allow
108f0 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 ed..** .** If th
10900 65 20 71 75 65 72 79 20 68 61 73 20 61 6e 20 4f e query has an O
10910 52 44 45 52 20 42 59 2c 20 74 68 65 6e 20 65 6e RDER BY, then en
10920 74 72 69 65 73 20 69 6e 20 74 68 65 20 51 75 65 tries in the Que
10930 75 65 20 74 61 62 6c 65 20 61 72 65 20 6b 65 70 ue table are kep
10940 74 20 69 6e 0a 2a 2a 20 4f 52 44 45 52 20 42 59 t in.** ORDER BY
10950 20 6f 72 64 65 72 20 61 6e 64 20 74 68 65 20 66 order and the f
10960 69 72 73 74 20 65 6e 74 72 79 20 69 73 20 65 78 irst entry is ex
10970 74 72 61 63 74 65 64 20 66 6f 72 20 65 61 63 68 tracted for each
10980 20 63 79 63 6c 65 2e 20 20 57 69 74 68 6f 75 74 cycle. Without
10990 0a 2a 2a 20 61 6e 20 4f 52 44 45 52 20 42 59 2c .** an ORDER BY,
109a0 20 74 68 65 20 51 75 65 75 65 20 74 61 62 6c 65 the Queue table
109b0 20 69 73 20 6a 75 73 74 20 61 20 46 49 46 4f 2e is just a FIFO.
109c0 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4c 49 4d 49 .**.** If a LIMI
109d0 54 20 63 6c 61 75 73 65 20 69 73 20 70 72 6f 76 T clause is prov
109e0 69 64 65 64 2c 20 74 68 65 6e 20 74 68 65 20 69 ided, then the i
109f0 74 65 72 61 74 69 6f 6e 20 73 74 6f 70 73 20 61 teration stops a
10a00 66 74 65 72 20 4c 49 4d 49 54 20 72 6f 77 73 0a fter LIMIT rows.
10a10 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 75 74 ** have been out
10a20 70 75 74 20 74 6f 20 70 44 65 73 74 2e 20 20 41 put to pDest. A
10a30 20 4c 49 4d 49 54 20 6f 66 20 7a 65 72 6f 20 6d LIMIT of zero m
10a40 65 61 6e 73 20 74 6f 20 6f 75 74 70 75 74 20 6e eans to output n
10a50 6f 20 72 6f 77 73 20 61 6e 64 20 61 0a 2a 2a 20 o rows and a.**
10a60 6e 65 67 61 74 69 76 65 20 4c 49 4d 49 54 20 6d negative LIMIT m
10a70 65 61 6e 73 20 74 6f 20 6f 75 74 70 75 74 20 61 eans to output a
10a80 6c 6c 20 72 6f 77 73 2e 20 20 49 66 20 74 68 65 ll rows. If the
10a90 72 65 20 69 73 20 61 6c 73 6f 20 61 6e 20 4f 46 re is also an OF
10aa0 46 53 45 54 20 63 6c 61 75 73 65 0a 2a 2a 20 77 FSET clause.** w
10ab0 69 74 68 20 61 20 70 6f 73 69 74 69 76 65 20 76 ith a positive v
10ac0 61 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 66 alue, then the f
10ad0 69 72 73 74 20 4f 46 46 53 45 54 20 6f 75 74 70 irst OFFSET outp
10ae0 75 74 73 20 61 72 65 20 64 69 73 63 61 72 64 65 uts are discarde
10af0 64 20 72 61 74 68 65 72 0a 2a 2a 20 74 68 61 6e d rather.** than
10b00 20 62 65 69 6e 67 20 73 65 6e 74 20 74 6f 20 70 being sent to p
10b10 44 65 73 74 2e 20 20 54 68 65 20 4c 49 4d 49 54 Dest. The LIMIT
10b20 20 63 6f 75 6e 74 20 64 6f 65 73 20 6e 6f 74 20 count does not
10b30 62 65 67 69 6e 20 75 6e 74 69 6c 20 61 66 74 65 begin until afte
10b40 72 20 4f 46 46 53 45 54 0a 2a 2a 20 72 6f 77 73 r OFFSET.** rows
10b50 20 68 61 76 65 20 62 65 65 6e 20 73 6b 69 70 70 have been skipp
10b60 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f ed..*/.static vo
10b70 69 64 20 67 65 6e 65 72 61 74 65 57 69 74 68 52 id generateWithR
10b80 65 63 75 72 73 69 76 65 51 75 65 72 79 28 0a 20 ecursiveQuery(.
10b90 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
10ba0 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
10bb0 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 g context */. S
10bc0 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 elect *p,
10bd0 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63 75 /* The recu
10be0 72 73 69 76 65 20 53 45 4c 45 43 54 20 74 6f 20 rsive SELECT to
10bf0 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 be coded */. Se
10c00 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 lectDest *pDest
10c10 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 /* What to d
10c20 6f 20 77 69 74 68 20 71 75 65 72 79 20 72 65 73 o with query res
10c30 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 53 72 63 ults */.){. Src
10c40 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 70 2d 3e List *pSrc = p->
10c50 70 53 72 63 3b 20 20 20 20 20 20 2f 2a 20 54 68 pSrc; /* Th
10c60 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 e FROM clause of
10c70 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20 71 the recursive q
10c80 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 uery */. int nC
10c90 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e ol = p->pEList->
10ca0 6e 45 78 70 72 3b 20 20 2f 2a 20 4e 75 6d 62 65 nExpr; /* Numbe
10cb0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 r of columns in
10cc0 74 68 65 20 72 65 63 75 72 73 69 76 65 20 74 61 the recursive ta
10cd0 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 ble */. Vdbe *v
10ce0 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 = pParse->pVdbe
10cf0 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72 ; /* The pr
10d00 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 epared statement
10d10 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 under construct
10d20 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 ion */. Select
10d30 2a 70 53 65 74 75 70 20 3d 20 70 2d 3e 70 50 72 *pSetup = p->pPr
10d40 69 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 73 65 ior; /* The se
10d50 74 75 70 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 tup query */. i
10d60 6e 74 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20 nt addrTop;
10d70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
10d80 54 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 Top of the loop
10d90 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e */. int addrCon
10da0 74 2c 20 61 64 64 72 42 72 65 61 6b 3b 20 20 20 t, addrBreak;
10db0 20 20 20 2f 2a 20 43 4f 4e 54 49 4e 55 45 20 61 /* CONTINUE a
10dc0 6e 64 20 42 52 45 41 4b 20 61 64 64 72 65 73 73 nd BREAK address
10dd0 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 es */. int iCur
10de0 72 65 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 rent = 0;
10df0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 43 75 72 /* The Cur
10e00 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 rent table */.
10e10 69 6e 74 20 72 65 67 43 75 72 72 65 6e 74 3b 20 int regCurrent;
10e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
10e30 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e Register holdin
10e40 67 20 43 75 72 72 65 6e 74 20 74 61 62 6c 65 20 g Current table
10e50 2a 2f 0a 20 20 69 6e 74 20 69 51 75 65 75 65 3b */. int iQueue;
10e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
10e70 20 20 20 2f 2a 20 54 68 65 20 51 75 65 75 65 20 /* The Queue
10e80 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 table */. int i
10e90 44 69 73 74 69 6e 63 74 20 3d 20 30 3b 20 20 20 Distinct = 0;
10ea0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 20 65 /* To e
10eb0 6e 73 75 72 65 20 75 6e 69 71 75 65 20 72 65 73 nsure unique res
10ec0 75 6c 74 73 20 69 66 20 55 4e 49 4f 4e 20 2a 2f ults if UNION */
10ed0 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20 53 . int eDest = S
10ee0 52 54 5f 46 69 66 6f 3b 20 20 20 20 20 20 20 20 RT_Fifo;
10ef0 20 2f 2a 20 48 6f 77 20 74 6f 20 77 72 69 74 65 /* How to write
10f00 20 74 6f 20 51 75 65 75 65 20 2a 2f 0a 20 20 53 to Queue */. S
10f10 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 51 75 electDest destQu
10f20 65 75 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 eue; /*
10f30 53 65 6c 65 63 74 44 65 73 74 20 74 61 72 67 65 SelectDest targe
10f40 74 74 69 6e 67 20 74 68 65 20 51 75 65 75 65 20 tting the Queue
10f50 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 table */. int i
10f60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;
10f70 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 /* Loop
10f80 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e counter */. in
10f90 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 t rc;
10fa0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
10fb0 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 esult code */.
10fc0 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 ExprList *pOrder
10fd0 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a By; /*
10fe0 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c The ORDER BY cl
10ff0 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a ause */. Expr *
11000 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74 pLimit, *pOffset
11010 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 ; /* Saved
11020 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 LIMIT and OFFSE
11030 54 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69 T */. int regLi
11040 6d 69 74 2c 20 72 65 67 4f 66 66 73 65 74 3b 20 mit, regOffset;
11050 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 /* Register
11060 73 20 75 73 65 64 20 62 79 20 4c 49 4d 49 54 20 s used by LIMIT
11070 61 6e 64 20 4f 46 46 53 45 54 20 2a 2f 0a 0a 20 and OFFSET */..
11080 20 2f 2a 20 4f 62 74 61 69 6e 20 61 75 74 68 6f /* Obtain autho
11090 72 69 7a 61 74 69 6f 6e 20 74 6f 20 64 6f 20 61 rization to do a
110a0 20 72 65 63 75 72 73 69 76 65 20 71 75 65 72 79 recursive query
110b0 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 */. if( sqlite
110c0 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 3AuthCheck(pPars
110d0 65 2c 20 53 51 4c 49 54 45 5f 52 45 43 55 52 53 e, SQLITE_RECURS
110e0 49 56 45 2c 20 30 2c 20 30 2c 20 30 29 20 29 20 IVE, 0, 0, 0) )
110f0 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 50 72 return;.. /* Pr
11100 6f 63 65 73 73 20 74 68 65 20 4c 49 4d 49 54 20 ocess the LIMIT
11110 61 6e 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 and OFFSET claus
11120 65 73 2c 20 69 66 20 74 68 65 79 20 65 78 69 73 es, if they exis
11130 74 20 2a 2f 0a 20 20 61 64 64 72 42 72 65 61 6b t */. addrBreak
11140 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
11150 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 63 6f keLabel(v);. co
11160 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 mputeLimitRegist
11170 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 61 ers(pParse, p, a
11180 64 64 72 42 72 65 61 6b 29 3b 0a 20 20 70 4c 69 ddrBreak);. pLi
11190 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b mit = p->pLimit;
111a0 0a 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e . pOffset = p->
111b0 70 4f 66 66 73 65 74 3b 0a 20 20 72 65 67 4c 69 pOffset;. regLi
111c0 6d 69 74 20 3d 20 70 2d 3e 69 4c 69 6d 69 74 3b mit = p->iLimit;
111d0 0a 20 20 72 65 67 4f 66 66 73 65 74 20 3d 20 70 . regOffset = p
111e0 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 70 2d 3e ->iOffset;. p->
111f0 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4f 66 66 pLimit = p->pOff
11200 73 65 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 4c set = 0;. p->iL
11210 69 6d 69 74 20 3d 20 70 2d 3e 69 4f 66 66 73 65 imit = p->iOffse
11220 74 20 3d 20 30 3b 0a 20 20 70 4f 72 64 65 72 42 t = 0;. pOrderB
11230 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b y = p->pOrderBy;
11240 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 .. /* Locate th
11250 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 e cursor number
11260 6f 66 20 74 68 65 20 43 75 72 72 65 6e 74 20 74 of the Current t
11270 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 69 3d able */. for(i=
11280 30 3b 20 41 4c 57 41 59 53 28 69 3c 70 53 72 63 0; ALWAYS(i<pSrc
11290 2d 3e 6e 53 72 63 29 3b 20 69 2b 2b 29 7b 0a 20 ->nSrc); i++){.
112a0 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61 5b 69 if( pSrc->a[i
112b0 5d 2e 69 73 52 65 63 75 72 73 69 76 65 20 29 7b ].isRecursive ){
112c0 0a 20 20 20 20 20 20 69 43 75 72 72 65 6e 74 20 . iCurrent
112d0 3d 20 70 53 72 63 2d 3e 61 5b 69 5d 2e 69 43 75 = pSrc->a[i].iCu
112e0 72 73 6f 72 3b 0a 20 20 20 20 20 20 62 72 65 61 rsor;. brea
112f0 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 k;. }. }..
11300 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 63 75 72 73 /* Allocate curs
11310 6f 72 73 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 ors numbers for
11320 51 75 65 75 65 20 61 6e 64 20 44 69 73 74 69 6e Queue and Distin
11330 63 74 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 ct. The cursor
11340 6e 75 6d 62 65 72 20 66 6f 72 0a 20 20 2a 2a 20 number for. **
11350 74 68 65 20 44 69 73 74 69 6e 63 74 20 74 61 62 the Distinct tab
11360 6c 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 le must be exact
11370 6c 79 20 6f 6e 65 20 67 72 65 61 74 65 72 20 74 ly one greater t
11380 68 61 6e 20 51 75 65 75 65 20 69 6e 20 6f 72 64 han Queue in ord
11390 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 er. ** for the
113a0 53 52 54 5f 44 69 73 74 46 69 66 6f 20 61 6e 64 SRT_DistFifo and
113b0 20 53 52 54 5f 44 69 73 74 51 75 65 75 65 20 64 SRT_DistQueue d
113c0 65 73 74 69 6e 61 74 69 6f 6e 73 20 74 6f 20 77 estinations to w
113d0 6f 72 6b 2e 20 2a 2f 0a 20 20 69 51 75 65 75 65 ork. */. iQueue
113e0 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b = pParse->nTab+
113f0 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d +;. if( p->op==
11400 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 TK_UNION ){.
11410 65 44 65 73 74 20 3d 20 70 4f 72 64 65 72 42 79 eDest = pOrderBy
11420 20 3f 20 53 52 54 5f 44 69 73 74 51 75 65 75 65 ? SRT_DistQueue
11430 20 3a 20 53 52 54 5f 44 69 73 74 46 69 66 6f 3b : SRT_DistFifo;
11440 0a 20 20 20 20 69 44 69 73 74 69 6e 63 74 20 3d . iDistinct =
11450 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b pParse->nTab++;
11460 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65 44 . }else{. eD
11470 65 73 74 20 3d 20 70 4f 72 64 65 72 42 79 20 3f est = pOrderBy ?
11480 20 53 52 54 5f 51 75 65 75 65 20 3a 20 53 52 54 SRT_Queue : SRT
11490 5f 46 69 66 6f 3b 0a 20 20 7d 0a 20 20 73 71 6c _Fifo;. }. sql
114a0 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e ite3SelectDestIn
114b0 69 74 28 26 64 65 73 74 51 75 65 75 65 2c 20 65 it(&destQueue, e
114c0 44 65 73 74 2c 20 69 51 75 65 75 65 29 3b 0a 0a Dest, iQueue);..
114d0 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 63 75 /* Allocate cu
114e0 72 73 6f 72 73 20 66 6f 72 20 43 75 72 72 65 6e rsors for Curren
114f0 74 2c 20 51 75 65 75 65 2c 20 61 6e 64 20 44 69 t, Queue, and Di
11500 73 74 69 6e 63 74 2e 20 2a 2f 0a 20 20 72 65 67 stinct. */. reg
11510 43 75 72 72 65 6e 74 20 3d 20 2b 2b 70 50 61 72 Current = ++pPar
11520 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 se->nMem;. sqli
11530 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
11540 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 OP_OpenPseudo,
11550 69 43 75 72 72 65 6e 74 2c 20 72 65 67 43 75 72 iCurrent, regCur
11560 72 65 6e 74 2c 20 6e 43 6f 6c 29 3b 0a 20 20 69 rent, nCol);. i
11570 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 f( pOrderBy ){.
11580 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 KeyInfo *pKey
11590 49 6e 66 6f 20 3d 20 6d 75 6c 74 69 53 65 6c 65 Info = multiSele
115a0 63 74 4f 72 64 65 72 42 79 4b 65 79 49 6e 66 6f ctOrderByKeyInfo
115b0 28 70 50 61 72 73 65 2c 20 70 2c 20 31 29 3b 0a (pParse, p, 1);.
115c0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
115d0 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e ddOp4(v, OP_Open
115e0 45 70 68 65 6d 65 72 61 6c 2c 20 69 51 75 65 75 Ephemeral, iQueu
115f0 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 e, pOrderBy->nEx
11600 70 72 2b 32 2c 20 30 2c 0a 20 20 20 20 20 20 20 pr+2, 0,.
11610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 (
11620 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 char*)pKeyInfo,
11630 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 P4_KEYINFO);.
11640 20 64 65 73 74 51 75 65 75 65 2e 70 4f 72 64 65 destQueue.pOrde
11650 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a rBy = pOrderBy;.
11660 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c }else{. sql
11670 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
11680 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 , OP_OpenEphemer
11690 61 6c 2c 20 69 51 75 65 75 65 2c 20 6e 43 6f 6c al, iQueue, nCol
116a0 29 3b 0a 20 20 7d 0a 20 20 56 64 62 65 43 6f 6d );. }. VdbeCom
116b0 6d 65 6e 74 28 28 76 2c 20 22 51 75 65 75 65 20 ment((v, "Queue
116c0 74 61 62 6c 65 22 29 29 3b 0a 20 20 69 66 28 20 table"));. if(
116d0 69 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 iDistinct ){.
116e0 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d p->addrOpenEphm
116f0 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 [0] = sqlite3Vdb
11700 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 eAddOp2(v, OP_Op
11710 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 69 44 69 enEphemeral, iDi
11720 73 74 69 6e 63 74 2c 20 30 29 3b 0a 20 20 20 20 stinct, 0);.
11730 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 p->selFlags |= S
11740 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b F_UsesEphemeral;
11750 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 61 63 . }.. /* Detac
11760 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 h the ORDER BY c
11770 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65 20 63 lause from the c
11780 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 2a ompound SELECT *
11790 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 /. p->pOrderBy
117a0 3d 20 30 3b 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 = 0;.. /* Store
117b0 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 the results of
117c0 74 68 65 20 73 65 74 75 70 2d 71 75 65 72 79 20 the setup-query
117d0 69 6e 20 51 75 65 75 65 2e 20 2a 2f 0a 20 20 70 in Queue. */. p
117e0 53 65 74 75 70 2d 3e 70 4e 65 78 74 20 3d 20 30 Setup->pNext = 0
117f0 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 ;. rc = sqlite3
11800 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
11810 53 65 74 75 70 2c 20 26 64 65 73 74 51 75 65 75 Setup, &destQueu
11820 65 29 3b 0a 20 20 70 53 65 74 75 70 2d 3e 70 4e e);. pSetup->pN
11830 65 78 74 20 3d 20 70 3b 0a 20 20 69 66 28 20 72 ext = p;. if( r
11840 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f c ) goto end_of_
11850 72 65 63 75 72 73 69 76 65 5f 71 75 65 72 79 3b recursive_query;
11860 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 .. /* Find the
11870 6e 65 78 74 20 72 6f 77 20 69 6e 20 74 68 65 20 next row in the
11880 51 75 65 75 65 20 61 6e 64 20 6f 75 74 70 75 74 Queue and output
11890 20 74 68 61 74 20 72 6f 77 20 2a 2f 0a 20 20 61 that row */. a
118a0 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 ddrTop = sqlite3
118b0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
118c0 5f 52 65 77 69 6e 64 2c 20 69 51 75 65 75 65 2c _Rewind, iQueue,
118d0 20 61 64 64 72 42 72 65 61 6b 29 3b 20 56 64 62 addrBreak); Vdb
118e0 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 0a 20 eCoverage(v);..
118f0 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 /* Transfer the
11900 20 6e 65 78 74 20 72 6f 77 20 69 6e 20 51 75 65 next row in Que
11910 75 65 20 6f 76 65 72 20 74 6f 20 43 75 72 72 65 ue over to Curre
11920 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 nt */. sqlite3V
11930 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
11940 4e 75 6c 6c 52 6f 77 2c 20 69 43 75 72 72 65 6e NullRow, iCurren
11950 74 29 3b 20 2f 2a 20 54 6f 20 72 65 73 65 74 20 t); /* To reset
11960 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a column cache */.
11970 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 if( pOrderBy )
11980 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 {. sqlite3Vdb
11990 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f eAddOp3(v, OP_Co
119a0 6c 75 6d 6e 2c 20 69 51 75 65 75 65 2c 20 70 4f lumn, iQueue, pO
119b0 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 2b 31 2c rderBy->nExpr+1,
119c0 20 72 65 67 43 75 72 72 65 6e 74 29 3b 0a 20 20 regCurrent);.
119d0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 }else{. sqlit
119e0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
119f0 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69 51 75 65 OP_RowData, iQue
11a00 75 65 2c 20 72 65 67 43 75 72 72 65 6e 74 29 3b ue, regCurrent);
11a10 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 . }. sqlite3Vd
11a20 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 44 beAddOp1(v, OP_D
11a30 65 6c 65 74 65 2c 20 69 51 75 65 75 65 29 3b 0a elete, iQueue);.
11a40 0a 20 20 2f 2a 20 4f 75 74 70 75 74 20 74 68 65 . /* Output the
11a50 20 73 69 6e 67 6c 65 20 72 6f 77 20 69 6e 20 43 single row in C
11a60 75 72 72 65 6e 74 20 2a 2f 0a 20 20 61 64 64 72 urrent */. addr
11a70 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 Cont = sqlite3Vd
11a80 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a beMakeLabel(v);.
11a90 20 20 63 6f 64 65 4f 66 66 73 65 74 28 76 2c 20 codeOffset(v,
11aa0 72 65 67 4f 66 66 73 65 74 2c 20 61 64 64 72 43 regOffset, addrC
11ab0 6f 6e 74 29 3b 0a 20 20 73 65 6c 65 63 74 49 6e ont);. selectIn
11ac0 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 nerLoop(pParse,
11ad0 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 69 43 p, p->pEList, iC
11ae0 75 72 72 65 6e 74 2c 0a 20 20 20 20 20 20 30 2c urrent,. 0,
11af0 20 30 2c 20 70 44 65 73 74 2c 20 61 64 64 72 43 0, pDest, addrC
11b00 6f 6e 74 2c 20 61 64 64 72 42 72 65 61 6b 29 3b ont, addrBreak);
11b10 0a 20 20 69 66 28 20 72 65 67 4c 69 6d 69 74 20 . if( regLimit
11b20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 ){. sqlite3Vd
11b30 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 beAddOp3(v, OP_I
11b40 66 5a 65 72 6f 2c 20 72 65 67 4c 69 6d 69 74 2c fZero, regLimit,
11b50 20 61 64 64 72 42 72 65 61 6b 2c 20 2d 31 29 3b addrBreak, -1);
11b60 0a 20 20 20 20 56 64 62 65 43 6f 76 65 72 61 67 . VdbeCoverag
11b70 65 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 e(v);. }. sqli
11b80 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
11b90 62 65 6c 28 76 2c 20 61 64 64 72 43 6f 6e 74 29 bel(v, addrCont)
11ba0 3b 0a 0a 20 20 2f 2a 20 45 78 65 63 75 74 65 20 ;.. /* Execute
11bb0 74 68 65 20 72 65 63 75 72 73 69 76 65 20 53 45 the recursive SE
11bc0 4c 45 43 54 20 74 61 6b 69 6e 67 20 74 68 65 20 LECT taking the
11bd0 73 69 6e 67 6c 65 20 72 6f 77 20 69 6e 20 43 75 single row in Cu
11be0 72 72 65 6e 74 20 61 73 0a 20 20 2a 2a 20 74 68 rrent as. ** th
11bf0 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 e value for the
11c00 72 65 63 75 72 73 69 76 65 2d 74 61 62 6c 65 2e recursive-table.
11c10 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c Store the resul
11c20 74 73 20 69 6e 20 74 68 65 20 51 75 65 75 65 2e ts in the Queue.
11c30 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72 69 6f . */. p->pPrio
11c40 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 r = 0;. sqlite3
11c50 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
11c60 2c 20 26 64 65 73 74 51 75 65 75 65 29 3b 0a 20 , &destQueue);.
11c70 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 69 assert( p->pPri
11c80 6f 72 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 70 50 or==0 );. p->pP
11c90 72 69 6f 72 20 3d 20 70 53 65 74 75 70 3b 0a 0a rior = pSetup;..
11ca0 20 20 2f 2a 20 4b 65 65 70 20 72 75 6e 6e 69 6e /* Keep runnin
11cb0 67 20 74 68 65 20 6c 6f 6f 70 20 75 6e 74 69 6c g the loop until
11cc0 20 74 68 65 20 51 75 65 75 65 20 69 73 20 65 6d the Queue is em
11cd0 70 74 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 pty */. sqlite3
11ce0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
11cf0 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 54 6f _Goto, 0, addrTo
11d00 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 p);. sqlite3Vdb
11d10 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c eResolveLabel(v,
11d20 20 61 64 64 72 42 72 65 61 6b 29 3b 0a 0a 65 6e addrBreak);..en
11d30 64 5f 6f 66 5f 72 65 63 75 72 73 69 76 65 5f 71 d_of_recursive_q
11d40 75 65 72 79 3a 0a 20 20 73 71 6c 69 74 65 33 45 uery:. sqlite3E
11d50 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 xprListDelete(pP
11d60 61 72 73 65 2d 3e 64 62 2c 20 70 2d 3e 70 4f 72 arse->db, p->pOr
11d70 64 65 72 42 79 29 3b 0a 20 20 70 2d 3e 70 4f 72 derBy);. p->pOr
11d80 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 derBy = pOrderBy
11d90 3b 0a 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 ;. p->pLimit =
11da0 70 4c 69 6d 69 74 3b 0a 20 20 70 2d 3e 70 4f 66 pLimit;. p->pOf
11db0 66 73 65 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a fset = pOffset;.
11dc0 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e 64 return;.}.#end
11dd0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
11de0 54 5f 43 54 45 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 T_CTE */../* For
11df0 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20 ward references
11e00 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 */.static int mu
11e10 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79 ltiSelectOrderBy
11e20 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 (. Parse *pPars
11e30 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 e, /* Par
11e40 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a sing context */.
11e50 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 Select *p,
11e60 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 /* The r
11e70 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c ight-most of SEL
11e80 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64 ECTs to be coded
11e90 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 */. SelectDest
11ea0 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57 *pDest /* W
11eb0 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71 hat to do with q
11ec0 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a uery results */.
11ed0 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 );.../*.** This
11ee0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 routine is calle
11ef0 64 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 63 d to process a c
11f00 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 66 6f ompound query fo
11f10 72 6d 20 66 72 6f 6d 0a 2a 2a 20 74 77 6f 20 6f rm from.** two o
11f20 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 r more separate
11f30 71 75 65 72 69 65 73 20 75 73 69 6e 67 20 55 4e queries using UN
11f40 49 4f 4e 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 ION, UNION ALL,
11f50 45 58 43 45 50 54 2c 20 6f 72 0a 2a 2a 20 49 4e EXCEPT, or.** IN
11f60 54 45 52 53 45 43 54 0a 2a 2a 0a 2a 2a 20 22 70 TERSECT.**.** "p
11f70 22 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 " points to the
11f80 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68 right-most of th
11f90 65 20 74 77 6f 20 71 75 65 72 69 65 73 2e 20 20 e two queries.
11fa0 74 68 65 20 71 75 65 72 79 20 6f 6e 20 74 68 65 the query on the
11fb0 0a 2a 2a 20 6c 65 66 74 20 69 73 20 70 2d 3e 70 .** left is p->p
11fc0 50 72 69 6f 72 2e 20 20 54 68 65 20 6c 65 66 74 Prior. The left
11fd0 20 71 75 65 72 79 20 63 6f 75 6c 64 20 61 6c 73 query could als
11fe0 6f 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 o be a compound
11ff0 71 75 65 72 79 0a 2a 2a 20 69 6e 20 77 68 69 63 query.** in whic
12000 68 20 63 61 73 65 20 74 68 69 73 20 72 6f 75 74 h case this rout
12010 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c ine will be call
12020 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 ed recursively.
12030 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c .**.** The resul
12040 74 73 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 ts of the total
12050 71 75 65 72 79 20 61 72 65 20 74 6f 20 62 65 20 query are to be
12060 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 64 written into a d
12070 65 73 74 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 estination.** of
12080 20 74 79 70 65 20 65 44 65 73 74 20 77 69 74 68 type eDest with
12090 20 70 61 72 61 6d 65 74 65 72 20 69 50 61 72 6d parameter iParm
120a0 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20 ..**.** Example
120b0 31 3a 20 20 43 6f 6e 73 69 64 65 72 20 61 20 74 1: Consider a t
120c0 68 72 65 65 2d 77 61 79 20 63 6f 6d 70 6f 75 6e hree-way compoun
120d0 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e d SQL statement.
120e0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 .**.** SELEC
120f0 54 20 61 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f T a FROM t1 UNIO
12100 4e 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d 20 N SELECT b FROM
12110 74 32 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 t2 UNION SELECT
12120 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20 c FROM t3.**.**
12130 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 This statement i
12140 73 20 70 61 72 73 65 64 20 75 70 20 61 73 20 66 s parsed up as f
12150 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 ollows:.**.**
12160 20 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 SELECT c FROM
12170 74 33 0a 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a 20 t3.** |.**
12180 20 20 20 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45 `-----> SE
12190 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32 0a 2a LECT b FROM t2.*
121a0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 *
121b0 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 |.**
121c0 20 20 20 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53 `------> S
121d0 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31 0a ELECT a FROM t1.
121e0 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 6f 77 73 **.** The arrows
121f0 20 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d 20 in the diagram
12200 61 62 6f 76 65 20 72 65 70 72 65 73 65 6e 74 20 above represent
12210 74 68 65 20 53 65 6c 65 63 74 2e 70 50 72 69 6f the Select.pPrio
12220 72 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f r pointer..** So
12230 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 if this routine
12240 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 is called with
12250 70 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 p equal to the t
12260 33 20 71 75 65 72 79 2c 20 74 68 65 6e 0a 2a 2a 3 query, then.**
12270 20 70 50 72 69 6f 72 20 77 69 6c 6c 20 62 65 20 pPrior will be
12280 74 68 65 20 74 32 20 71 75 65 72 79 2e 20 20 70 the t2 query. p
12290 2d 3e 6f 70 20 77 69 6c 6c 20 62 65 20 54 4b 5f ->op will be TK_
122a0 55 4e 49 4f 4e 20 69 6e 20 74 68 69 73 20 63 61 UNION in this ca
122b0 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65 se..**.** Notice
122c0 20 74 68 61 74 20 62 65 63 61 75 73 65 20 6f 66 that because of
122d0 20 74 68 65 20 77 61 79 20 53 51 4c 69 74 65 20 the way SQLite
122e0 70 61 72 73 65 73 20 63 6f 6d 70 6f 75 6e 64 20 parses compound
122f0 53 45 4c 45 43 54 73 2c 20 74 68 65 0a 2a 2a 20 SELECTs, the.**
12300 69 6e 64 69 76 69 64 75 61 6c 20 73 65 6c 65 63 individual selec
12310 74 73 20 61 6c 77 61 79 73 20 67 72 6f 75 70 20 ts always group
12320 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 from left to rig
12330 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ht..*/.static in
12340 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 0a 20 t multiSelect(.
12350 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
12360 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
12370 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 g context */. S
12380 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 elect *p,
12390 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 /* The righ
123a0 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 t-most of SELECT
123b0 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f s to be coded */
123c0 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 . SelectDest *p
123d0 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74 Dest /* What
123e0 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72 to do with quer
123f0 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a y results */.){.
12400 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 int rc = SQLIT
12410 45 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65 E_OK; /* Succe
12420 73 73 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73 ss code from a s
12430 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53 ubroutine */. S
12440 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 elect *pPrior;
12450 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20 /* Another
12460 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65 SELECT immediate
12470 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a ly to our left *
12480 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 /. Vdbe *v;
12490 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e /* Gen
124a0 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68 erate code to th
124b0 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c is VDBE */. Sel
124c0 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 20 ectDest dest;
124d0 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74 69 76 /* Alternativ
124e0 65 20 64 61 74 61 20 64 65 73 74 69 6e 61 74 69 e data destinati
124f0 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a on */. Select *
12500 70 44 65 6c 65 74 65 20 3d 20 30 3b 20 20 2f 2a pDelete = 0; /*
12510 20 43 68 61 69 6e 20 6f 66 20 73 69 6d 70 6c 65 Chain of simple
12520 20 73 65 6c 65 63 74 73 20 74 6f 20 64 65 6c 65 selects to dele
12530 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 te */. sqlite3
12540 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a *db; /*
12550 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 Database connec
12560 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 tion */.#ifndef
12570 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c SQLITE_OMIT_EXPL
12580 41 49 4e 0a 20 20 69 6e 74 20 69 53 75 62 31 20 AIN. int iSub1
12590 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 = 0; /* E
125a0 51 50 20 69 64 20 6f 66 20 6c 65 66 74 2d 68 61 QP id of left-ha
125b0 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e nd query */. in
125c0 74 20 69 53 75 62 32 20 3d 20 30 3b 20 20 20 20 t iSub2 = 0;
125d0 20 20 20 20 2f 2a 20 45 51 50 20 69 64 20 6f 66 /* EQP id of
125e0 20 72 69 67 68 74 2d 68 61 6e 64 20 71 75 65 72 right-hand quer
125f0 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 2f y */.#endif.. /
12600 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72 * Make sure ther
12610 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 e is no ORDER BY
12620 20 6f 72 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 or LIMIT clause
12630 20 6f 6e 20 70 72 69 6f 72 20 53 45 4c 45 43 54 on prior SELECT
12640 73 2e 20 20 4f 6e 6c 79 0a 20 20 2a 2a 20 74 68 s. Only. ** th
12650 65 20 6c 61 73 74 20 28 72 69 67 68 74 2d 6d 6f e last (right-mo
12660 73 74 29 20 53 45 4c 45 43 54 20 69 6e 20 74 68 st) SELECT in th
12670 65 20 73 65 72 69 65 73 20 6d 61 79 20 68 61 76 e series may hav
12680 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 e an ORDER BY or
12690 20 4c 49 4d 49 54 2e 0a 20 20 2a 2f 0a 20 20 61 LIMIT.. */. a
126a0 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 ssert( p && p->p
126b0 50 72 69 6f 72 20 29 3b 20 20 2f 2a 20 43 61 6c Prior ); /* Cal
126c0 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 67 75 ling function gu
126d0 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 6d 75 arantees this mu
126e0 63 68 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 ch */. assert(
126f0 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 (p->selFlags & S
12700 46 5f 52 65 63 75 72 73 69 76 65 29 3d 3d 30 20 F_Recursive)==0
12710 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c || p->op==TK_ALL
12720 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e || p->op==TK_UN
12730 49 4f 4e 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 ION );. db = pP
12740 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 50 72 69 arse->db;. pPri
12750 6f 72 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a or = p->pPrior;.
12760 20 20 64 65 73 74 20 3d 20 2a 70 44 65 73 74 3b dest = *pDest;
12770 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 . if( pPrior->p
12780 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73 OrderBy ){. s
12790 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 qlite3ErrorMsg(p
127a0 50 61 72 73 65 2c 22 4f 52 44 45 52 20 42 59 20 Parse,"ORDER BY
127b0 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20 63 6f clause should co
127c0 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f 74 20 me after %s not
127d0 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20 20 73 before",. s
127e0 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f electOpName(p->o
127f0 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b p));. rc = 1;
12800 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f . goto multi_
12810 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a select_end;. }.
12820 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4c if( pPrior->pL
12830 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 imit ){. sqli
12840 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 te3ErrorMsg(pPar
12850 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75 73 65 se,"LIMIT clause
12860 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74 should come aft
12870 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65 er %s not before
12880 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f ",. selectO
12890 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 pName(p->op));.
128a0 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 rc = 1;. g
128b0 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 oto multi_select
128c0 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d _end;. }.. v =
128d0 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 sqlite3GetVdbe(
128e0 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 pParse);. asser
128f0 74 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20 54 t( v!=0 ); /* T
12900 68 65 20 56 44 42 45 20 61 6c 72 65 61 64 79 20 he VDBE already
12910 63 72 65 61 74 65 64 20 62 79 20 63 61 6c 6c 69 created by calli
12920 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a ng function */..
12930 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 /* Create the
12940 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 65 6d 70 destination temp
12950 6f 72 61 72 79 20 74 61 62 6c 65 20 69 66 20 6e orary table if n
12960 65 63 65 73 73 61 72 79 0a 20 20 2a 2f 0a 20 20 ecessary. */.
12970 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d if( dest.eDest==
12980 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a SRT_EphemTab ){.
12990 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 assert( p->p
129a0 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 73 71 6c EList );. sql
129b0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
129c0 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 , OP_OpenEphemer
129d0 61 6c 2c 20 64 65 73 74 2e 69 53 44 50 61 72 6d al, dest.iSDParm
129e0 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 , p->pEList->nEx
129f0 70 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 pr);. sqlite3
12a00 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 VdbeChangeP5(v,
12a10 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 44 29 BTREE_UNORDERED)
12a20 3b 0a 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 ;. dest.eDest
12a30 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b 0a 20 20 = SRT_Table;.
12a40 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 }.. /* Make sur
12a50 65 20 61 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e e all SELECTs in
12a60 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 68 the statement h
12a70 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d ave the same num
12a80 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0a ber of elements.
12a90 20 20 2a 2a 20 69 6e 20 74 68 65 69 72 20 72 65 ** in their re
12aa0 73 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a sult sets.. */.
12ab0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c assert( p->pEL
12ac0 69 73 74 20 26 26 20 70 50 72 69 6f 72 2d 3e 70 ist && pPrior->p
12ad0 45 4c 69 73 74 20 29 3b 0a 20 20 69 66 28 20 70 EList );. if( p
12ae0 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 ->pEList->nExpr!
12af0 3d 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74 2d =pPrior->pEList-
12b00 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66 >nExpr ){. if
12b10 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 ( p->selFlags &
12b20 53 46 5f 56 61 6c 75 65 73 20 29 7b 0a 20 20 20 SF_Values ){.
12b30 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d sqlite3ErrorM
12b40 73 67 28 70 50 61 72 73 65 2c 20 22 61 6c 6c 20 sg(pParse, "all
12b50 56 41 4c 55 45 53 20 6d 75 73 74 20 68 61 76 65 VALUES must have
12b60 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 the same number
12b70 20 6f 66 20 74 65 72 6d 73 22 29 3b 0a 20 20 20 of terms");.
12b80 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 }else{. sq
12b90 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
12ba0 61 72 73 65 2c 20 22 53 45 4c 45 43 54 73 20 74 arse, "SELECTs t
12bb0 6f 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 o the left and r
12bc0 69 67 68 74 20 6f 66 20 25 73 22 0a 20 20 20 20 ight of %s".
12bd0 20 20 20 20 22 20 64 6f 20 6e 6f 74 20 68 61 76 " do not hav
12be0 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 e the same numbe
12bf0 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 r of result colu
12c00 6d 6e 73 22 2c 20 73 65 6c 65 63 74 4f 70 4e 61 mns", selectOpNa
12c10 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 me(p->op));.
12c20 7d 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 }. rc = 1;.
12c30 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c goto multi_sel
12c40 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 23 69 ect_end;. }..#i
12c50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
12c60 54 5f 43 54 45 0a 20 20 69 66 28 20 70 2d 3e 73 T_CTE. if( p->s
12c70 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 63 elFlags & SF_Rec
12c80 75 72 73 69 76 65 20 29 7b 0a 20 20 20 20 67 65 ursive ){. ge
12c90 6e 65 72 61 74 65 57 69 74 68 52 65 63 75 72 73 nerateWithRecurs
12ca0 69 76 65 51 75 65 72 79 28 70 50 61 72 73 65 2c iveQuery(pParse,
12cb0 20 70 2c 20 26 64 65 73 74 29 3b 0a 20 20 7d 65 p, &dest);. }e
12cc0 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a lse.#endif.. /*
12cd0 20 43 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 Compound SELECT
12ce0 73 20 74 68 61 74 20 68 61 76 65 20 61 6e 20 4f s that have an O
12cf0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 RDER BY clause a
12d00 72 65 20 68 61 6e 64 6c 65 64 20 73 65 70 61 72 re handled separ
12d10 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 ately.. */. if
12d20 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b ( p->pOrderBy ){
12d30 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 . return mult
12d40 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28 70 iSelectOrderBy(p
12d50 50 61 72 73 65 2c 20 70 2c 20 70 44 65 73 74 29 Parse, p, pDest)
12d60 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 20 ;. }else.. /*
12d70 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f Generate code fo
12d80 72 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 72 r the left and r
12d90 69 67 68 74 20 53 45 4c 45 43 54 20 73 74 61 74 ight SELECT stat
12da0 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 73 ements.. */. s
12db0 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a witch( p->op ){.
12dc0 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a case TK_ALL:
12dd0 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 {. int add
12de0 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 r = 0;. int
12df0 20 6e 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 61 nLimit;. a
12e00 73 73 65 72 74 28 20 21 70 50 72 69 6f 72 2d 3e ssert( !pPrior->
12e10 70 4c 69 6d 69 74 20 29 3b 0a 20 20 20 20 20 20 pLimit );.
12e20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 20 3d pPrior->iLimit =
12e30 20 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 p->iLimit;.
12e40 20 20 70 50 72 69 6f 72 2d 3e 69 4f 66 66 73 65 pPrior->iOffse
12e50 74 20 3d 20 70 2d 3e 69 4f 66 66 73 65 74 3b 0a t = p->iOffset;.
12e60 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4c pPrior->pL
12e70 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 imit = p->pLimit
12e80 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e ;. pPrior->
12e90 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 pOffset = p->pOf
12ea0 66 73 65 74 3b 0a 20 20 20 20 20 20 65 78 70 6c fset;. expl
12eb0 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69 53 ainSetInteger(iS
12ec0 75 62 31 2c 20 70 50 61 72 73 65 2d 3e 69 4e 65 ub1, pParse->iNe
12ed0 78 74 53 65 6c 65 63 74 49 64 29 3b 0a 20 20 20 xtSelectId);.
12ee0 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 rc = sqlite3S
12ef0 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 elect(pParse, pP
12f00 72 69 6f 72 2c 20 26 64 65 73 74 29 3b 0a 20 20 rior, &dest);.
12f10 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 p->pLimit =
12f20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 0;. p->pOff
12f30 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 set = 0;. i
12f40 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 f( rc ){.
12f50 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 goto multi_sele
12f60 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a ct_end;. }.
12f70 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 p->pPrior
12f80 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c = 0;. p->iL
12f90 69 6d 69 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69 imit = pPrior->i
12fa0 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e Limit;. p->
12fb0 69 4f 66 66 73 65 74 20 3d 20 70 50 72 69 6f 72 iOffset = pPrior
12fc0 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 ->iOffset;.
12fd0 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 if( p->iLimit )
12fe0 7b 0a 20 20 20 20 20 20 20 20 61 64 64 72 20 3d {. addr =
12ff0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
13000 70 31 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c p1(v, OP_IfZero,
13010 20 70 2d 3e 69 4c 69 6d 69 74 29 3b 20 56 64 62 p->iLimit); Vdb
13020 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 eCoverage(v);.
13030 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e VdbeCommen
13040 74 28 28 76 2c 20 22 4a 75 6d 70 20 61 68 65 61 t((v, "Jump ahea
13050 64 20 69 66 20 4c 49 4d 49 54 20 72 65 61 63 68 d if LIMIT reach
13060 65 64 22 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 ed"));. }.
13070 20 20 20 20 20 65 78 70 6c 61 69 6e 53 65 74 49 explainSetI
13080 6e 74 65 67 65 72 28 69 53 75 62 32 2c 20 70 50 nteger(iSub2, pP
13090 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65 63 arse->iNextSelec
130a0 74 49 64 29 3b 0a 20 20 20 20 20 20 72 63 20 3d tId);. rc =
130b0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 sqlite3Select(p
130c0 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73 74 29 Parse, p, &dest)
130d0 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
130e0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 ( rc!=SQLITE_OK
130f0 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65 );. pDelete
13100 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 = p->pPrior;.
13110 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 p->pPrior =
13120 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d pPrior;. p-
13130 3e 6e 53 65 6c 65 63 74 52 6f 77 20 2b 3d 20 70 >nSelectRow += p
13140 50 72 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 52 6f Prior->nSelectRo
13150 77 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 w;. if( pPr
13160 69 6f 72 2d 3e 70 4c 69 6d 69 74 0a 20 20 20 20 ior->pLimit.
13170 20 20 20 26 26 20 73 71 6c 69 74 65 33 45 78 70 && sqlite3Exp
13180 72 49 73 49 6e 74 65 67 65 72 28 70 50 72 69 6f rIsInteger(pPrio
13190 72 2d 3e 70 4c 69 6d 69 74 2c 20 26 6e 4c 69 6d r->pLimit, &nLim
131a0 69 74 29 0a 20 20 20 20 20 20 20 26 26 20 6e 4c it). && nL
131b0 69 6d 69 74 3e 30 20 26 26 20 70 2d 3e 6e 53 65 imit>0 && p->nSe
131c0 6c 65 63 74 52 6f 77 20 3e 20 28 75 36 34 29 6e lectRow > (u64)n
131d0 4c 69 6d 69 74 20 0a 20 20 20 20 20 20 29 7b 0a Limit . ){.
131e0 20 20 20 20 20 20 20 20 70 2d 3e 6e 53 65 6c 65 p->nSele
131f0 63 74 52 6f 77 20 3d 20 6e 4c 69 6d 69 74 3b 0a ctRow = nLimit;.
13200 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
13210 28 20 61 64 64 72 20 29 7b 0a 20 20 20 20 20 20 ( addr ){.
13220 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
13230 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a pHere(v, addr);.
13240 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 }. br
13250 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 eak;. }. c
13260 61 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 0a 20 ase TK_EXCEPT:.
13270 20 20 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f 4e case TK_UNION
13280 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 75 6e : {. int un
13290 69 6f 6e 54 61 62 3b 20 20 20 20 2f 2a 20 43 75 ionTab; /* Cu
132a0 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 rsor number of t
132b0 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 he temporary tab
132c0 6c 65 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c le holding resul
132d0 74 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 6f 70 t */. u8 op
132e0 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f = 0; /* O
132f0 6e 65 20 6f 66 20 74 68 65 20 53 52 54 5f 20 6f ne of the SRT_ o
13300 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 61 70 70 perations to app
13310 6c 79 20 74 6f 20 73 65 6c 66 20 2a 2f 0a 20 20 ly to self */.
13320 20 20 20 20 69 6e 74 20 70 72 69 6f 72 4f 70 3b int priorOp;
13330 20 20 20 20 20 2f 2a 20 54 68 65 20 53 52 54 5f /* The SRT_
13340 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 70 operation to ap
13350 70 6c 79 20 74 6f 20 70 72 69 6f 72 20 73 65 6c ply to prior sel
13360 65 63 74 73 20 2a 2f 0a 20 20 20 20 20 20 45 78 ects */. Ex
13370 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 pr *pLimit, *pOf
13380 66 73 65 74 3b 20 2f 2a 20 53 61 76 65 64 20 76 fset; /* Saved v
13390 61 6c 75 65 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d alues of p->nLim
133a0 69 74 20 61 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 it and p->nOffse
133b0 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 t */. int a
133c0 64 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63 ddr;. Selec
133d0 74 44 65 73 74 20 75 6e 69 6f 6e 64 65 73 74 3b tDest uniondest;
133e0 0a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 .. testcase
133f0 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 ( p->op==TK_EXCE
13400 50 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 PT );. test
13410 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f case( p->op==TK_
13420 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 70 UNION );. p
13430 72 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55 6e 69 riorOp = SRT_Uni
13440 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 on;. if( de
13450 73 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f 72 4f st.eDest==priorO
13460 70 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 p ){. /*
13470 57 65 20 63 61 6e 20 72 65 75 73 65 20 61 20 74 We can reuse a t
13480 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 67 emporary table g
13490 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 53 45 enerated by a SE
134a0 4c 45 43 54 20 74 6f 20 6f 75 72 0a 20 20 20 20 LECT to our.
134b0 20 20 20 20 2a 2a 20 72 69 67 68 74 2e 0a 20 20 ** right..
134c0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 */.
134d0 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4c 69 6d assert( p->pLim
134e0 69 74 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f 2a it==0 ); /*
134f0 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 Not allowed on
13500 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e 74 leftward element
13510 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 s */. ass
13520 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65 74 3d ert( p->pOffset=
13530 3d 30 20 29 3b 20 20 20 20 20 2f 2a 20 4e 6f 74 =0 ); /* Not
13540 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 6c 65 66 74 allowed on left
13550 77 61 72 64 20 65 6c 65 6d 65 6e 74 73 20 2a 2f ward elements */
13560 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54 61 . unionTa
13570 62 20 3d 20 64 65 73 74 2e 69 53 44 50 61 72 6d b = dest.iSDParm
13580 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
13590 20 20 20 20 20 20 20 2f 2a 20 57 65 20 77 69 6c /* We wil
135a0 6c 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 l need to create
135b0 20 6f 75 72 20 6f 77 6e 20 74 65 6d 70 6f 72 61 our own tempora
135c0 72 79 20 74 61 62 6c 65 20 74 6f 20 68 6f 6c 64 ry table to hold
135d0 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 the. **
135e0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 72 65 73 intermediate res
135f0 75 6c 74 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f ults.. */
13600 0a 20 20 20 20 20 20 20 20 75 6e 69 6f 6e 54 61 . unionTa
13610 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 b = pParse->nTab
13620 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 ++;. asse
13630 72 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d rt( p->pOrderBy=
13640 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 64 =0 );. ad
13650 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 dr = sqlite3Vdbe
13660 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 AddOp2(v, OP_Ope
13670 6e 45 70 68 65 6d 65 72 61 6c 2c 20 75 6e 69 6f nEphemeral, unio
13680 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 nTab, 0);.
13690 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64 assert( p->add
136a0 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 3d 20 rOpenEphm[0] ==
136b0 2d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d -1 );. p-
136c0 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d >addrOpenEphm[0]
136d0 20 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 20 = addr;.
136e0 20 66 69 6e 64 52 69 67 68 74 6d 6f 73 74 28 70 findRightmost(p
136f0 29 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 )->selFlags |= S
13700 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b F_UsesEphemeral;
13710 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
13720 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 p->pEList );.
13730 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 }.. /*
13740 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54 20 Code the SELECT
13750 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 6f 75 statements to ou
13760 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2f 0a r left. */.
13770 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 assert( !p
13780 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 Prior->pOrderBy
13790 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
137a0 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 SelectDestInit(&
137b0 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69 6f 72 uniondest, prior
137c0 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b 0a 20 Op, unionTab);.
137d0 20 20 20 20 20 65 78 70 6c 61 69 6e 53 65 74 49 explainSetI
137e0 6e 74 65 67 65 72 28 69 53 75 62 31 2c 20 70 50 nteger(iSub1, pP
137f0 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c 65 63 arse->iNextSelec
13800 74 49 64 29 3b 0a 20 20 20 20 20 20 72 63 20 3d tId);. rc =
13810 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 sqlite3Select(p
13820 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 Parse, pPrior, &
13830 75 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 uniondest);.
13840 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 if( rc ){.
13850 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 goto multi_s
13860 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 elect_end;.
13870 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 }.. /* Cod
13880 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 45 e the current SE
13890 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 LECT statement.
138a0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
138b0 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 ( p->op==TK_EXCE
138c0 50 54 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 70 PT ){. op
138d0 20 3d 20 53 52 54 5f 45 78 63 65 70 74 3b 0a 20 = SRT_Except;.
138e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
138f0 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f assert( p->o
13900 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 3b 0a 20 p==TK_UNION );.
13910 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54 5f op = SRT_
13920 55 6e 69 6f 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 Union;. }.
13930 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d p->pPrior =
13940 20 30 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69 74 0;. pLimit
13950 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 = p->pLimit;.
13960 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 p->pLimit =
13970 30 3b 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74 0;. pOffset
13980 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 = p->pOffset;.
13990 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 p->pOffset
139a0 3d 20 30 3b 0a 20 20 20 20 20 20 75 6e 69 6f 6e = 0;. union
139b0 64 65 73 74 2e 65 44 65 73 74 20 3d 20 6f 70 3b dest.eDest = op;
139c0 0a 20 20 20 20 20 20 65 78 70 6c 61 69 6e 53 65 . explainSe
139d0 74 49 6e 74 65 67 65 72 28 69 53 75 62 32 2c 20 tInteger(iSub2,
139e0 70 50 61 72 73 65 2d 3e 69 4e 65 78 74 53 65 6c pParse->iNextSel
139f0 65 63 74 49 64 29 3b 0a 20 20 20 20 20 20 72 63 ectId);. rc
13a00 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 = sqlite3Select
13a10 28 70 50 61 72 73 65 2c 20 70 2c 20 26 75 6e 69 (pParse, p, &uni
13a20 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 20 20 74 ondest);. t
13a30 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c estcase( rc!=SQL
13a40 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 ITE_OK );.
13a50 2f 2a 20 51 75 65 72 79 20 66 6c 61 74 74 65 6e /* Query flatten
13a60 69 6e 67 20 69 6e 20 73 71 6c 69 74 65 33 53 65 ing in sqlite3Se
13a70 6c 65 63 74 28 29 20 6d 69 67 68 74 20 72 65 66 lect() might ref
13a80 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2e ill p->pOrderBy.
13a90 0a 20 20 20 20 20 20 2a 2a 20 42 65 20 73 75 72 . ** Be sur
13aa0 65 20 74 6f 20 64 65 6c 65 74 65 20 70 2d 3e 70 e to delete p->p
13ab0 4f 72 64 65 72 42 79 2c 20 74 68 65 72 65 66 6f OrderBy, therefo
13ac0 72 65 2c 20 74 6f 20 61 76 6f 69 64 20 61 20 6d re, to avoid a m
13ad0 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f 0a 20 emory leak. */.
13ae0 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 sqlite3Expr
13af0 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 ListDelete(db, p
13b00 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 ->pOrderBy);.
13b10 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70 2d 3e pDelete = p->
13b20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d pPrior;. p-
13b30 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 >pPrior = pPrior
13b40 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 72 64 65 ;. p->pOrde
13b50 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 rBy = 0;. i
13b60 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 f( p->op==TK_UNI
13b70 4f 4e 20 29 20 70 2d 3e 6e 53 65 6c 65 63 74 52 ON ) p->nSelectR
13b80 6f 77 20 2b 3d 20 70 50 72 69 6f 72 2d 3e 6e 53 ow += pPrior->nS
13b90 65 6c 65 63 74 52 6f 77 3b 0a 20 20 20 20 20 20 electRow;.
13ba0 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
13bb0 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 e(db, p->pLimit)
13bc0 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 ;. p->pLimi
13bd0 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 t = pLimit;.
13be0 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 p->pOffset = p
13bf0 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d Offset;. p-
13c00 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 >iLimit = 0;.
13c10 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 p->iOffset =
13c20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 0;.. /* Con
13c30 76 65 72 74 20 74 68 65 20 64 61 74 61 20 69 6e vert the data in
13c40 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 the temporary t
13c50 61 62 6c 65 20 69 6e 74 6f 20 77 68 61 74 65 76 able into whatev
13c60 65 72 20 66 6f 72 6d 0a 20 20 20 20 20 20 2a 2a er form. **
13c70 20 69 74 20 69 73 20 74 68 61 74 20 77 65 20 63 it is that we c
13c80 75 72 72 65 6e 74 6c 79 20 6e 65 65 64 2e 0a 20 urrently need..
13c90 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 */. as
13ca0 73 65 72 74 28 20 75 6e 69 6f 6e 54 61 62 3d 3d sert( unionTab==
13cb0 64 65 73 74 2e 69 53 44 50 61 72 6d 20 7c 7c 20 dest.iSDParm ||
13cc0 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69 6f dest.eDest!=prio
13cd0 72 4f 70 20 29 3b 0a 20 20 20 20 20 20 69 66 28 rOp );. if(
13ce0 20 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69 dest.eDest!=pri
13cf0 6f 72 4f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 orOp ){.
13d00 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 int iCont, iBrea
13d10 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 k, iStart;.
13d20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 assert( p->pE
13d30 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 List );.
13d40 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d if( dest.eDest==
13d50 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 SRT_Output ){.
13d60 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a Select *
13d70 70 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 pFirst = p;.
13d80 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 69 while( pFi
13d90 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 46 rst->pPrior ) pF
13da0 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d 3e 70 irst = pFirst->p
13db0 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 Prior;.
13dc0 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e generateColumnN
13dd0 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 ames(pParse, 0,
13de0 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b pFirst->pEList);
13df0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
13e00 20 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 iBreak = sqli
13e10 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c te3VdbeMakeLabel
13e20 28 76 29 3b 0a 20 20 20 20 20 20 20 20 69 43 6f (v);. iCo
13e30 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 nt = sqlite3Vdbe
13e40 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 MakeLabel(v);.
13e50 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69 6d computeLim
13e60 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72 itRegisters(pPar
13e70 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b 0a se, p, iBreak);.
13e80 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
13e90 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
13ea0 52 65 77 69 6e 64 2c 20 75 6e 69 6f 6e 54 61 62 Rewind, unionTab
13eb0 2c 20 69 42 72 65 61 6b 29 3b 20 56 64 62 65 43 , iBreak); VdbeC
13ec0 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 overage(v);.
13ed0 20 20 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c iStart = sql
13ee0 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 ite3VdbeCurrentA
13ef0 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 20 20 ddr(v);.
13f00 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 selectInnerLoop(
13f10 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 pParse, p, p->pE
13f20 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61 62 2c 0a List, unionTab,.
13f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13f40 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 26 64 0, 0, &d
13f50 65 73 74 2c 20 69 43 6f 6e 74 2c 20 69 42 72 65 est, iCont, iBre
13f60 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c ak);. sql
13f70 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c ite3VdbeResolveL
13f80 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 29 3b 0a abel(v, iCont);.
13f90 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
13fa0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
13fb0 4e 65 78 74 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 Next, unionTab,
13fc0 69 53 74 61 72 74 29 3b 20 56 64 62 65 43 6f 76 iStart); VdbeCov
13fd0 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 20 20 erage(v);.
13fe0 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 sqlite3VdbeRes
13ff0 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72 olveLabel(v, iBr
14000 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71 eak);. sq
14010 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
14020 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 75 6e 69 v, OP_Close, uni
14030 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20 onTab, 0);.
14040 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a }. break;.
14050 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c }. defaul
14060 74 3a 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 t: assert( p->op
14070 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 ==TK_INTERSECT )
14080 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 61 ; {. int ta
14090 62 31 2c 20 74 61 62 32 3b 0a 20 20 20 20 20 20 b1, tab2;.
140a0 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 int iCont, iBrea
140b0 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 k, iStart;.
140c0 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a Expr *pLimit, *
140d0 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 pOffset;. i
140e0 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53 nt addr;. S
140f0 65 6c 65 63 74 44 65 73 74 20 69 6e 74 65 72 73 electDest inters
14100 65 63 74 64 65 73 74 3b 0a 20 20 20 20 20 20 69 ectdest;. i
14110 6e 74 20 72 31 3b 0a 0a 20 20 20 20 20 20 2f 2a nt r1;.. /*
14120 20 49 4e 54 45 52 53 45 43 54 20 69 73 20 64 69 INTERSECT is di
14130 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 fferent from the
14140 20 6f 74 68 65 72 73 20 73 69 6e 63 65 20 69 74 others since it
14150 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20 requires.
14160 2a 2a 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 ** two temporary
14170 20 74 61 62 6c 65 73 2e 20 20 48 65 6e 63 65 20 tables. Hence
14180 69 74 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63 it has its own c
14190 61 73 65 2e 20 20 42 65 67 69 6e 0a 20 20 20 20 ase. Begin.
141a0 20 20 2a 2a 20 62 79 20 61 6c 6c 6f 63 61 74 69 ** by allocati
141b0 6e 67 20 74 68 65 20 74 61 62 6c 65 73 20 77 65 ng the tables we
141c0 20 77 69 6c 6c 20 6e 65 65 64 2e 0a 20 20 20 20 will need..
141d0 20 20 2a 2f 0a 20 20 20 20 20 20 74 61 62 31 20 */. tab1
141e0 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b = pParse->nTab++
141f0 3b 0a 20 20 20 20 20 20 74 61 62 32 20 3d 20 70 ;. tab2 = p
14200 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 Parse->nTab++;.
14210 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
14220 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 0a pOrderBy==0 );..
14230 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c addr = sql
14240 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
14250 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 , OP_OpenEphemer
14260 61 6c 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20 20 al, tab1, 0);.
14270 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 assert( p->a
14280 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d ddrOpenEphm[0] =
14290 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d = -1 );. p-
142a0 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d >addrOpenEphm[0]
142b0 20 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 66 = addr;. f
142c0 69 6e 64 52 69 67 68 74 6d 6f 73 74 28 70 29 2d indRightmost(p)-
142d0 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f >selFlags |= SF_
142e0 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 UsesEphemeral;.
142f0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e assert( p->
14300 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20 20 20 pEList );..
14310 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45 4c /* Code the SEL
14320 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66 74 ECTs to our left
14330 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79 20 into temporary
14340 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a 20 20 table "tab1"..
14350 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
14360 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e ite3SelectDestIn
14370 69 74 28 26 69 6e 74 65 72 73 65 63 74 64 65 73 it(&intersectdes
14380 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 61 t, SRT_Union, ta
14390 62 31 29 3b 0a 20 20 20 20 20 20 65 78 70 6c 61 b1);. expla
143a0 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69 53 75 inSetInteger(iSu
143b0 62 31 2c 20 70 50 61 72 73 65 2d 3e 69 4e 65 78 b1, pParse->iNex
143c0 74 53 65 6c 65 63 74 49 64 29 3b 0a 20 20 20 20 tSelectId);.
143d0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 rc = sqlite3Se
143e0 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 72 lect(pParse, pPr
143f0 69 6f 72 2c 20 26 69 6e 74 65 72 73 65 63 74 64 ior, &intersectd
14400 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 est);. if(
14410 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f rc ){. go
14420 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f to multi_select_
14430 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 end;. }..
14440 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 /* Code the
14450 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20 69 current SELECT i
14460 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 nto temporary ta
14470 62 6c 65 20 22 74 61 62 32 22 0a 20 20 20 20 20 ble "tab2".
14480 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 20 3d */. addr =
14490 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
144a0 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 p2(v, OP_OpenEph
144b0 65 6d 65 72 61 6c 2c 20 74 61 62 32 2c 20 30 29 emeral, tab2, 0)
144c0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
144d0 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b p->addrOpenEphm[
144e0 31 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 1] == -1 );.
144f0 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 p->addrOpenEph
14500 6d 5b 31 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20 m[1] = addr;.
14510 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 p->pPrior = 0
14520 3b 0a 20 20 20 20 20 20 70 4c 69 6d 69 74 20 3d ;. pLimit =
14530 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 p->pLimit;.
14540 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b p->pLimit = 0;
14550 0a 20 20 20 20 20 20 70 4f 66 66 73 65 74 20 3d . pOffset =
14560 20 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20 p->pOffset;.
14570 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 p->pOffset =
14580 30 3b 0a 20 20 20 20 20 20 69 6e 74 65 72 73 65 0;. interse
14590 63 74 64 65 73 74 2e 69 53 44 50 61 72 6d 20 3d ctdest.iSDParm =
145a0 20 74 61 62 32 3b 0a 20 20 20 20 20 20 65 78 70 tab2;. exp
145b0 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 69 lainSetInteger(i
145c0 53 75 62 32 2c 20 70 50 61 72 73 65 2d 3e 69 4e Sub2, pParse->iN
145d0 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0a 20 20 extSelectId);.
145e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 rc = sqlite3
145f0 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
14600 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65 73 74 , &intersectdest
14610 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 );. testcas
14620 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b e( rc!=SQLITE_OK
14630 20 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 );. pDelet
14640 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 e = p->pPrior;.
14650 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d p->pPrior =
14660 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 69 pPrior;. i
14670 66 28 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 f( p->nSelectRow
14680 3e 70 50 72 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 >pPrior->nSelect
14690 52 6f 77 20 29 20 70 2d 3e 6e 53 65 6c 65 63 74 Row ) p->nSelect
146a0 52 6f 77 20 3d 20 70 50 72 69 6f 72 2d 3e 6e 53 Row = pPrior->nS
146b0 65 6c 65 63 74 52 6f 77 3b 0a 20 20 20 20 20 20 electRow;.
146c0 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 sqlite3ExprDelet
146d0 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 e(db, p->pLimit)
146e0 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 ;. p->pLimi
146f0 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 t = pLimit;.
14700 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 p->pOffset = p
14710 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20 20 20 2f Offset;.. /
14720 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 * Generate code
14730 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e 74 65 to take the inte
14740 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 rsection of the
14750 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a 20 20 two temporary.
14760 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0a 20 ** tables..
14770 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 */. as
14780 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 sert( p->pEList
14790 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73 );. if( des
147a0 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 t.eDest==SRT_Out
147b0 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 53 put ){. S
147c0 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 elect *pFirst =
147d0 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 p;. while
147e0 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 ( pFirst->pPrior
147f0 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 ) pFirst = pFir
14800 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 st->pPrior;.
14810 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 generateColu
14820 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 mnNames(pParse,
14830 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 0, pFirst->pELis
14840 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 t);. }.
14850 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 iBreak = sqlit
14860 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
14870 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e 74 20 v);. iCont
14880 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b = sqlite3VdbeMak
14890 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 eLabel(v);.
148a0 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 computeLimitReg
148b0 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70 isters(pParse, p
148c0 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 , iBreak);.
148d0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
148e0 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c p2(v, OP_Rewind,
148f0 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29 3b 20 tab1, iBreak);
14900 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
14910 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 . r1 = sqli
14920 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 te3GetTempReg(pP
14930 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 53 74 arse);. iSt
14940 61 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 art = sqlite3Vdb
14950 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f eAddOp2(v, OP_Ro
14960 77 4b 65 79 2c 20 74 61 62 31 2c 20 72 31 29 3b wKey, tab1, r1);
14970 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
14980 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f beAddOp4Int(v, O
14990 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74 61 62 32 P_NotFound, tab2
149a0 2c 20 69 43 6f 6e 74 2c 20 72 31 2c 20 30 29 3b , iCont, r1, 0);
149b0 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
149c0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 ;. sqlite3R
149d0 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 eleaseTempReg(pP
149e0 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 arse, r1);.
149f0 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 selectInnerLoop
14a00 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 (pParse, p, p->p
14a10 45 4c 69 73 74 2c 20 74 61 62 31 2c 0a 20 20 20 EList, tab1,.
14a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
14a30 20 20 20 30 2c 20 30 2c 20 26 64 65 73 74 2c 20 0, 0, &dest,
14a40 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a iCont, iBreak);.
14a50 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
14a60 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c eResolveLabel(v,
14a70 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20 73 iCont);. s
14a80 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
14a90 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74 61 62 (v, OP_Next, tab
14aa0 31 2c 20 69 53 74 61 72 74 29 3b 20 56 64 62 65 1, iStart); Vdbe
14ab0 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 Coverage(v);.
14ac0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 sqlite3VdbeRe
14ad0 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 solveLabel(v, iB
14ae0 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c reak);. sql
14af0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
14b00 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 32 , OP_Close, tab2
14b10 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 , 0);. sqli
14b20 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
14b30 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31 2c OP_Close, tab1,
14b40 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 0);. break
14b50 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 65 ;. }. }.. e
14b60 78 70 6c 61 69 6e 43 6f 6d 70 6f 73 69 74 65 28 xplainComposite(
14b70 70 50 61 72 73 65 2c 20 70 2d 3e 6f 70 2c 20 69 pParse, p->op, i
14b80 53 75 62 31 2c 20 69 53 75 62 32 2c 20 70 2d 3e Sub1, iSub2, p->
14b90 6f 70 21 3d 54 4b 5f 41 4c 4c 29 3b 0a 0a 20 20 op!=TK_ALL);..
14ba0 2f 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61 /* Compute colla
14bb0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75 ting sequences u
14bc0 73 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65 6d sed by . ** tem
14bd0 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65 porary tables ne
14be0 65 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e eded to implemen
14bf0 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 t the compound s
14c00 65 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61 elect.. ** Atta
14c10 63 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 ch the KeyInfo s
14c20 74 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20 tructure to all
14c30 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 temporary tables
14c40 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 .. **. ** This
14c50 20 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20 section is run
14c60 62 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 by the right-mos
14c70 74 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 t SELECT stateme
14c80 6e 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45 nt only.. ** SE
14c90 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 LECT statements
14ca0 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61 to the left alwa
14cb0 79 73 20 73 6b 69 70 20 74 68 69 73 20 70 61 72 ys skip this par
14cc0 74 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f t. The right-mo
14cd0 73 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d st. ** SELECT m
14ce0 69 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20 74 ight also skip t
14cf0 68 69 73 20 70 61 72 74 20 69 66 20 69 74 20 68 his part if it h
14d00 61 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63 as no ORDER BY c
14d10 6c 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e lause and. ** n
14d20 6f 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61 72 o temp tables ar
14d30 65 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f e required.. */
14d40 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 . if( p->selFla
14d50 67 73 20 26 20 53 46 5f 55 73 65 73 45 70 68 65 gs & SF_UsesEphe
14d60 6d 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 meral ){. int
14d70 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 i;
14d80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f /* Lo
14d90 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 op counter */.
14da0 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 KeyInfo *pKeyI
14db0 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 nfo;
14dc0 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 /* Collating seq
14dd0 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65 uence for the re
14de0 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 sult set */.
14df0 53 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20 Select *pLoop;
14e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
14e10 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 For looping thr
14e20 6f 75 67 68 20 53 45 4c 45 43 54 20 73 74 61 74 ough SELECT stat
14e30 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43 6f ements */. Co
14e40 6c 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20 llSeq **apColl;
14e50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 /* F
14e60 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 or looping throu
14e70 67 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f gh pKeyInfo->aCo
14e80 6c 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 ll[] */. int
14e90 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 nCol;
14ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d /* Num
14eb0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 ber of columns i
14ec0 6e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a n result set */.
14ed0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e . assert( p->
14ee0 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20 20 pNext==0 );.
14ef0 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74 nCol = p->pEList
14f00 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 70 4b 65 ->nExpr;. pKe
14f10 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 4b yInfo = sqlite3K
14f20 65 79 49 6e 66 6f 41 6c 6c 6f 63 28 64 62 2c 20 eyInfoAlloc(db,
14f30 6e 43 6f 6c 2c 20 31 29 3b 0a 20 20 20 20 69 66 nCol, 1);. if
14f40 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 ( !pKeyInfo ){.
14f50 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 rc = SQLITE
14f60 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f _NOMEM;. go
14f70 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f to multi_select_
14f80 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 end;. }. f
14f90 6f 72 28 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 or(i=0, apColl=p
14fa0 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 KeyInfo->aColl;
14fb0 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 i<nCol; i++, apC
14fc0 6f 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 61 oll++){. *a
14fd0 70 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c pColl = multiSel
14fe0 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 ectCollSeq(pPars
14ff0 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 e, p, i);.
15000 69 66 28 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 if( 0==*apColl )
15010 7b 0a 20 20 20 20 20 20 20 20 2a 61 70 43 6f 6c {. *apCol
15020 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c l = db->pDfltCol
15030 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d l;. }. }
15040 0a 0a 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d .. for(pLoop=
15050 70 3b 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d p; pLoop; pLoop=
15060 70 4c 6f 6f 70 2d 3e 70 50 72 69 6f 72 29 7b 0a pLoop->pPrior){.
15070 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 for(i=0; i
15080 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 <2; i++){.
15090 20 20 69 6e 74 20 61 64 64 72 20 3d 20 70 4c 6f int addr = pLo
150a0 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d op->addrOpenEphm
150b0 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 [i];. if(
150c0 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 20 addr<0 ){.
150d0 20 20 20 20 20 2f 2a 20 49 66 20 5b 30 5d 20 69 /* If [0] i
150e0 73 20 75 6e 75 73 65 64 20 74 68 65 6e 20 5b 31 s unused then [1
150f0 5d 20 69 73 20 61 6c 73 6f 20 75 6e 75 73 65 64 ] is also unused
15100 2e 20 20 53 6f 20 77 65 20 63 61 6e 0a 20 20 20 . So we can.
15110 20 20 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 ** always
15120 20 73 61 66 65 6c 79 20 61 62 6f 72 74 20 61 73 safely abort as
15130 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 66 69 72 soon as the fir
15140 73 74 20 75 6e 75 73 65 64 20 73 6c 6f 74 20 69 st unused slot i
15150 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 s found */.
15160 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f assert( pLo
15170 6f 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d op->addrOpenEphm
15180 5b 31 5d 3c 30 20 29 3b 0a 20 20 20 20 20 20 20 [1]<0 );.
15190 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 break;.
151a0 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 }. sqli
151b0 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 te3VdbeChangeP2(
151c0 76 2c 20 61 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a v, addr, nCol);.
151d0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
151e0 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 dbeChangeP4(v, a
151f0 64 64 72 2c 20 28 63 68 61 72 2a 29 73 71 6c 69 ddr, (char*)sqli
15200 74 65 33 4b 65 79 49 6e 66 6f 52 65 66 28 70 4b te3KeyInfoRef(pK
15210 65 79 49 6e 66 6f 29 2c 0a 20 20 20 20 20 20 20 eyInfo),.
15220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15230 20 20 20 20 20 50 34 5f 4b 45 59 49 4e 46 4f 29 P4_KEYINFO)
15240 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d ;. pLoop-
15250 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d >addrOpenEphm[i]
15260 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 = -1;. }.
15270 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
15280 4b 65 79 49 6e 66 6f 55 6e 72 65 66 28 70 4b 65 KeyInfoUnref(pKe
15290 79 49 6e 66 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c yInfo);. }..mul
152a0 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 20 ti_select_end:.
152b0 20 70 44 65 73 74 2d 3e 69 53 64 73 74 20 3d 20 pDest->iSdst =
152c0 64 65 73 74 2e 69 53 64 73 74 3b 0a 20 20 70 44 dest.iSdst;. pD
152d0 65 73 74 2d 3e 6e 53 64 73 74 20 3d 20 64 65 73 est->nSdst = des
152e0 74 2e 6e 53 64 73 74 3b 0a 20 20 73 71 6c 69 74 t.nSdst;. sqlit
152f0 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 e3SelectDelete(d
15300 62 2c 20 70 44 65 6c 65 74 65 29 3b 0a 20 20 72 b, pDelete);. r
15310 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 eturn rc;.}.#end
15320 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 if /* SQLITE_OMI
15330 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 T_COMPOUND_SELEC
15340 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 T */../*.** Code
15350 20 61 6e 20 6f 75 74 70 75 74 20 73 75 62 72 6f an output subro
15360 75 74 69 6e 65 20 66 6f 72 20 61 20 63 6f 72 6f utine for a coro
15370 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 61 utine implementa
15380 74 69 6f 6e 20 6f 66 20 61 0a 2a 2a 20 53 45 4c tion of a.** SEL
15390 45 43 54 20 73 74 61 74 6d 65 6e 74 2e 0a 2a 2a ECT statment..**
153a0 0a 2a 2a 20 54 68 65 20 64 61 74 61 20 74 6f 20 .** The data to
153b0 62 65 20 6f 75 74 70 75 74 20 69 73 20 63 6f 6e be output is con
153c0 74 61 69 6e 65 64 20 69 6e 20 70 49 6e 2d 3e 69 tained in pIn->i
153d0 53 64 73 74 2e 20 20 54 68 65 72 65 20 61 72 65 Sdst. There are
153e0 0a 2a 2a 20 70 49 6e 2d 3e 6e 53 64 73 74 20 63 .** pIn->nSdst c
153f0 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 6f 75 74 olumns to be out
15400 70 75 74 2e 20 20 70 44 65 73 74 20 69 73 20 77 put. pDest is w
15410 68 65 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 here the output
15420 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65 6e should.** be sen
15430 74 2e 0a 2a 2a 0a 2a 2a 20 72 65 67 52 65 74 75 t..**.** regRetu
15440 72 6e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 rn is the number
15450 20 6f 66 20 74 68 65 20 72 65 67 69 73 74 65 72 of the register
15460 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 73 75 62 holding the sub
15470 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 routine.** retur
15480 6e 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a n address..**.**
15490 20 49 66 20 72 65 67 50 72 65 76 3e 30 20 74 68 If regPrev>0 th
154a0 65 6e 20 69 74 20 69 73 20 74 68 65 20 66 69 72 en it is the fir
154b0 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 61 st register in a
154c0 20 76 65 63 74 6f 72 20 74 68 61 74 0a 2a 2a 20 vector that.**
154d0 72 65 63 6f 72 64 73 20 74 68 65 20 70 72 65 76 records the prev
154e0 69 6f 75 73 20 6f 75 74 70 75 74 2e 20 20 6d 65 ious output. me
154f0 6d 5b 72 65 67 50 72 65 76 5d 20 69 73 20 61 20 m[regPrev] is a
15500 66 6c 61 67 20 74 68 61 74 20 69 73 20 66 61 6c flag that is fal
15510 73 65 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 68 se.** if there h
15520 61 73 20 62 65 65 6e 20 6e 6f 20 70 72 65 76 69 as been no previ
15530 6f 75 73 20 6f 75 74 70 75 74 2e 20 20 49 66 20 ous output. If
15540 72 65 67 50 72 65 76 3e 30 20 74 68 65 6e 20 63 regPrev>0 then c
15550 6f 64 65 20 69 73 0a 2a 2a 20 67 65 6e 65 72 61 ode is.** genera
15560 74 65 64 20 74 6f 20 73 75 70 70 72 65 73 73 20 ted to suppress
15570 64 75 70 6c 69 63 61 74 65 73 2e 20 20 70 4b 65 duplicates. pKe
15580 79 49 6e 66 6f 20 69 73 20 75 73 65 64 20 66 6f yInfo is used fo
15590 72 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a 20 6b r comparing.** k
155a0 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 eys..**.** If th
155b0 65 20 4c 49 4d 49 54 20 66 6f 75 6e 64 20 69 6e e LIMIT found in
155c0 20 70 2d 3e 69 4c 69 6d 69 74 20 69 73 20 72 65 p->iLimit is re
155d0 61 63 68 65 64 2c 20 6a 75 6d 70 20 69 6d 6d 65 ached, jump imme
155e0 64 69 61 74 65 6c 79 20 74 6f 0a 2a 2a 20 69 42 diately to.** iB
155f0 72 65 61 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 reak..*/.static
15600 69 6e 74 20 67 65 6e 65 72 61 74 65 4f 75 74 70 int generateOutp
15610 75 74 53 75 62 72 6f 75 74 69 6e 65 28 0a 20 20 utSubroutine(.
15620 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 Parse *pParse,
15630 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 /* Parsi
15640 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 ng context */.
15650 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 Select *p,
15660 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 /* The S
15670 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
15680 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 */. SelectDest
15690 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 *pIn, /*
156a0 43 6f 72 6f 75 74 69 6e 65 20 73 75 70 70 6c 79 Coroutine supply
156b0 69 6e 67 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 ing data */. Se
156c0 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c lectDest *pDest,
156d0 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 /* Where t
156e0 6f 20 73 65 6e 64 20 74 68 65 20 64 61 74 61 20 o send the data
156f0 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 74 75 */. int regRetu
15700 72 6e 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 rn, /*
15710 54 68 65 20 72 65 74 75 72 6e 20 61 64 64 72 65 The return addre
15720 73 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 ss register */.
15730 20 69 6e 74 20 72 65 67 50 72 65 76 2c 20 20 20 int regPrev,
15740 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 /* Prev
15750 69 6f 75 73 20 72 65 73 75 6c 74 20 72 65 67 69 ious result regi
15760 73 74 65 72 2e 20 20 4e 6f 20 75 6e 69 71 75 65 ster. No unique
15770 6e 65 73 73 20 69 66 20 30 20 2a 2f 0a 20 20 4b ness if 0 */. K
15780 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
15790 2c 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f , /* For co
157a0 6d 70 61 72 69 6e 67 20 77 69 74 68 20 70 72 65 mparing with pre
157b0 76 69 6f 75 73 20 65 6e 74 72 79 20 2a 2f 0a 20 vious entry */.
157c0 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 20 20 int iBreak
157d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 /* Jump
157e0 20 68 65 72 65 20 69 66 20 77 65 20 68 69 74 20 here if we hit
157f0 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29 7b 0a the LIMIT */.){.
15800 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
15810 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 se->pVdbe;. int
15820 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 69 6e iContinue;. in
15830 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64 72 20 t addr;.. addr
15840 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 = sqlite3VdbeCur
15850 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69 rentAddr(v);. i
15860 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69 74 Continue = sqlit
15870 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 e3VdbeMakeLabel(
15880 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70 72 65 v);.. /* Suppre
15890 73 73 20 64 75 70 6c 69 63 61 74 65 73 20 66 6f ss duplicates fo
158a0 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c r UNION, EXCEPT,
158b0 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20 0a and INTERSECT .
158c0 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50 72 */. if( regPr
158d0 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 ev ){. int j1
158e0 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d 20 73 , j2;. j1 = s
158f0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
15900 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 65 (v, OP_IfNot, re
15910 67 50 72 65 76 29 3b 20 56 64 62 65 43 6f 76 65 gPrev); VdbeCove
15920 72 61 67 65 28 76 29 3b 0a 20 20 20 20 6a 32 20 rage(v);. j2
15930 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 = sqlite3VdbeAdd
15940 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 Op4(v, OP_Compar
15950 65 2c 20 70 49 6e 2d 3e 69 53 64 73 74 2c 20 72 e, pIn->iSdst, r
15960 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e egPrev+1, pIn->n
15970 53 64 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 Sdst,.
15980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
15990 20 20 20 20 28 63 68 61 72 2a 29 73 71 6c 69 74 (char*)sqlit
159a0 65 33 4b 65 79 49 6e 66 6f 52 65 66 28 70 4b 65 e3KeyInfoRef(pKe
159b0 79 49 6e 66 6f 29 2c 20 50 34 5f 4b 45 59 49 4e yInfo), P4_KEYIN
159c0 46 4f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 FO);. sqlite3
159d0 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 VdbeAddOp3(v, OP
159e0 5f 4a 75 6d 70 2c 20 6a 32 2b 32 2c 20 69 43 6f _Jump, j2+2, iCo
159f0 6e 74 69 6e 75 65 2c 20 6a 32 2b 32 29 3b 20 56 ntinue, j2+2); V
15a00 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a dbeCoverage(v);.
15a10 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a sqlite3VdbeJ
15a20 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a umpHere(v, j1);.
15a30 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
15a40 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 70 79 ddOp3(v, OP_Copy
15a50 2c 20 70 49 6e 2d 3e 69 53 64 73 74 2c 20 72 65 , pIn->iSdst, re
15a60 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e 53 gPrev+1, pIn->nS
15a70 64 73 74 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 dst-1);. sqli
15a80 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
15a90 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 OP_Integer, 1,
15aa0 72 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20 20 regPrev);. }.
15ab0 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e if( pParse->db->
15ac0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 mallocFailed ) r
15ad0 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 53 eturn 0;.. /* S
15ae0 75 70 70 72 65 73 73 20 74 68 65 20 66 69 72 73 uppress the firs
15af0 74 20 4f 46 46 53 45 54 20 65 6e 74 72 69 65 73 t OFFSET entries
15b00 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 if there is an
15b10 4f 46 46 53 45 54 20 63 6c 61 75 73 65 0a 20 20 OFFSET clause.
15b20 2a 2f 0a 20 20 63 6f 64 65 4f 66 66 73 65 74 28 */. codeOffset(
15b30 76 2c 20 70 2d 3e 69 4f 66 66 73 65 74 2c 20 69 v, p->iOffset, i
15b40 43 6f 6e 74 69 6e 75 65 29 3b 0a 0a 20 20 73 77 Continue);.. sw
15b50 69 74 63 68 28 20 70 44 65 73 74 2d 3e 65 44 65 itch( pDest->eDe
15b60 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74 6f st ){. /* Sto
15b70 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 61 73 re the result as
15b80 20 64 61 74 61 20 75 73 69 6e 67 20 61 20 75 6e data using a un
15b90 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20 2a 2f ique key.. */
15ba0 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61 . case SRT_Ta
15bb0 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 ble:. case SR
15bc0 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 T_EphemTab: {.
15bd0 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c int r1 = sql
15be0 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 ite3GetTempReg(p
15bf0 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 6e Parse);. in
15c00 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47 65 t r2 = sqlite3Ge
15c10 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 tTempReg(pParse)
15c20 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
15c30 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d ( pDest->eDest==
15c40 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a 20 20 20 SRT_Table );.
15c50 20 20 20 74 65 73 74 63 61 73 65 28 20 70 44 65 testcase( pDe
15c60 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 st->eDest==SRT_E
15c70 70 68 65 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 phemTab );.
15c80 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
15c90 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 p3(v, OP_MakeRec
15ca0 6f 72 64 2c 20 70 49 6e 2d 3e 69 53 64 73 74 2c ord, pIn->iSdst,
15cb0 20 70 49 6e 2d 3e 6e 53 64 73 74 2c 20 72 31 29 pIn->nSdst, r1)
15cc0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
15cd0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
15ce0 4e 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74 2d NewRowid, pDest-
15cf0 3e 69 53 44 50 61 72 6d 2c 20 72 32 29 3b 0a 20 >iSDParm, r2);.
15d00 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 sqlite3Vdbe
15d10 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 AddOp3(v, OP_Ins
15d20 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 53 44 50 ert, pDest->iSDP
15d30 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20 arm, r1, r2);.
15d40 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
15d50 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 hangeP5(v, OPFLA
15d60 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 G_APPEND);.
15d70 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 sqlite3ReleaseT
15d80 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 empReg(pParse, r
15d90 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 2);. sqlite
15da0 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 3ReleaseTempReg(
15db0 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 pParse, r1);.
15dc0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
15dd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f .#ifndef SQLITE_
15de0 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 OMIT_SUBQUERY.
15df0 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 /* If we are c
15e00 72 65 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f reating a set fo
15e10 72 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 r an "expr IN (S
15e20 45 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 ELECT ...)" cons
15e30 74 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 truct,. ** th
15e40 65 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20 en there should
15e50 62 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d be a single item
15e60 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 on the stack.
15e70 57 72 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a Write this. *
15e80 2a 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 * item into the
15e90 73 65 74 20 74 61 62 6c 65 20 77 69 74 68 20 62 set table with b
15ea0 6f 67 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a ogus data.. *
15eb0 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 /. case SRT_S
15ec0 65 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 et: {. int
15ed0 72 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 r1;. assert
15ee0 28 20 70 49 6e 2d 3e 6e 53 64 73 74 3d 3d 31 20 ( pIn->nSdst==1
15ef0 29 3b 0a 20 20 20 20 20 20 70 44 65 73 74 2d 3e );. pDest->
15f00 61 66 66 53 64 73 74 20 3d 20 0a 20 20 20 20 20 affSdst = .
15f10 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 sqlite3Compa
15f20 72 65 41 66 66 69 6e 69 74 79 28 70 2d 3e 70 45 reAffinity(p->pE
15f30 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 List->a[0].pExpr
15f40 2c 20 70 44 65 73 74 2d 3e 61 66 66 53 64 73 74 , pDest->affSdst
15f50 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 );. r1 = sq
15f60 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
15f70 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 pParse);. s
15f80 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
15f90 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 (v, OP_MakeRecor
15fa0 64 2c 20 70 49 6e 2d 3e 69 53 64 73 74 2c 20 31 d, pIn->iSdst, 1
15fb0 2c 20 72 31 2c 20 26 70 44 65 73 74 2d 3e 61 66 , r1, &pDest->af
15fc0 66 53 64 73 74 2c 31 29 3b 0a 20 20 20 20 20 20 fSdst,1);.
15fd0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 sqlite3ExprCache
15fe0 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 AffinityChange(p
15ff0 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 53 64 73 Parse, pIn->iSds
16000 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c t, 1);. sql
16010 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
16020 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 , OP_IdxInsert,
16030 70 44 65 73 74 2d 3e 69 53 44 50 61 72 6d 2c 20 pDest->iSDParm,
16040 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 r1);. sqlit
16050 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 e3ReleaseTempReg
16060 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 (pParse, r1);.
16070 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d break;. }
16080 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e 65 76 65 ..#if 0 /* Neve
16090 72 20 6f 63 63 75 72 73 20 6f 6e 20 61 6e 20 4f r occurs on an O
160a0 52 44 45 52 20 42 59 20 71 75 65 72 79 20 2a 2f RDER BY query */
160b0 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79 20 72 . /* If any r
160c0 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 ow exist in the
160d0 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65 63 6f result set, reco
160e0 72 64 20 74 68 61 74 20 66 61 63 74 20 61 6e 64 rd that fact and
160f0 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 abort.. */.
16100 20 20 20 63 61 73 65 20 53 52 54 5f 45 78 69 73 case SRT_Exis
16110 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 ts: {. sqli
16120 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
16130 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 OP_Integer, 1,
16140 70 44 65 73 74 2d 3e 69 53 44 50 61 72 6d 29 3b pDest->iSDParm);
16150 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 . /* The LI
16160 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 MIT clause will
16170 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f terminate the lo
16180 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 op for us */.
16190 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a break;. }.
161a0 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 #endif.. /* I
161b0 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61 6c f this is a scal
161c0 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20 69 ar select that i
161d0 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 s part of an exp
161e0 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 ression, then.
161f0 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 ** store the r
16200 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61 70 esults in the ap
16210 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72 79 propriate memory
16220 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b 20 cell and break
16230 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 out. ** of th
16240 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 e scan loop..
16250 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 */. case SRT
16260 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61 73 _Mem: {. as
16270 73 65 72 74 28 20 70 49 6e 2d 3e 6e 53 64 73 74 sert( pIn->nSdst
16280 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c ==1 );. sql
16290 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 ite3ExprCodeMove
162a0 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 53 (pParse, pIn->iS
162b0 64 73 74 2c 20 70 44 65 73 74 2d 3e 69 53 44 50 dst, pDest->iSDP
162c0 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f arm, 1);. /
162d0 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 * The LIMIT clau
162e0 73 65 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 se will jump out
162f0 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 of the loop for
16300 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 us */. bre
16310 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 ak;. }.#endif
16320 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 /* #ifndef SQLI
16330 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 TE_OMIT_SUBQUERY
16340 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 */.. /* The
16350 72 65 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 results are stor
16360 65 64 20 69 6e 20 61 20 73 65 71 75 65 6e 63 65 ed in a sequence
16370 20 6f 66 20 72 65 67 69 73 74 65 72 73 0a 20 20 of registers.
16380 20 20 2a 2a 20 73 74 61 72 74 69 6e 67 20 61 74 ** starting at
16390 20 70 44 65 73 74 2d 3e 69 53 64 73 74 2e 20 20 pDest->iSdst.
163a0 54 68 65 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74 Then the co-rout
163b0 69 6e 65 20 79 69 65 6c 64 73 2e 0a 20 20 20 20 ine yields..
163c0 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f */. case SRT_
163d0 43 6f 72 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20 Coroutine: {.
163e0 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 53 if( pDest->iS
163f0 64 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 dst==0 ){.
16400 20 20 70 44 65 73 74 2d 3e 69 53 64 73 74 20 3d pDest->iSdst =
16410 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
16420 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e ange(pParse, pIn
16430 2d 3e 6e 53 64 73 74 29 3b 0a 20 20 20 20 20 20 ->nSdst);.
16440 20 20 70 44 65 73 74 2d 3e 6e 53 64 73 74 20 3d pDest->nSdst =
16450 20 70 49 6e 2d 3e 6e 53 64 73 74 3b 0a 20 20 20 pIn->nSdst;.
16460 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
16470 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 e3ExprCodeMove(p
16480 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 53 64 73 Parse, pIn->iSds
16490 74 2c 20 70 44 65 73 74 2d 3e 69 53 64 73 74 2c t, pDest->iSdst,
164a0 20 70 44 65 73 74 2d 3e 6e 53 64 73 74 29 3b 0a pDest->nSdst);.
164b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 sqlite3Vdb
164c0 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 eAddOp1(v, OP_Yi
164d0 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 53 44 50 eld, pDest->iSDP
164e0 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 arm);. brea
164f0 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a k;. }.. /*
16500 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 If none of the
16510 61 62 6f 76 65 2c 20 74 68 65 6e 20 74 68 65 20 above, then the
16520 72 65 73 75 6c 74 20 64 65 73 74 69 6e 61 74 69 result destinati
16530 6f 6e 20 6d 75 73 74 20 62 65 0a 20 20 20 20 2a on must be. *
16540 2a 20 53 52 54 5f 4f 75 74 70 75 74 2e 20 20 54 * SRT_Output. T
16550 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e his routine is n
16560 65 76 65 72 20 63 61 6c 6c 65 64 20 77 69 74 68 ever called with
16570 20 61 6e 79 20 6f 74 68 65 72 0a 20 20 20 20 2a any other. *
16580 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6f 74 * destination ot
16590 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 her than the one
165a0 73 20 68 61 6e 64 6c 65 64 20 61 62 6f 76 65 20 s handled above
165b0 6f 72 20 53 52 54 5f 4f 75 74 70 75 74 2e 0a 20 or SRT_Output..
165c0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 **. ** For
165d0 20 53 52 54 5f 4f 75 74 70 75 74 2c 20 72 65 73 SRT_Output, res
165e0 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 ults are stored
165f0 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 in a sequence of
16600 20 72 65 67 69 73 74 65 72 73 2e 20 20 0a 20 20 registers. .
16610 20 20 2a 2a 20 54 68 65 6e 20 74 68 65 20 4f 50 ** Then the OP
16620 5f 52 65 73 75 6c 74 52 6f 77 20 6f 70 63 6f 64 _ResultRow opcod
16630 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 61 75 e is used to cau
16640 73 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 se sqlite3_step(
16650 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72 65 74 75 ) to. ** retu
16660 72 6e 20 74 68 65 20 6e 65 78 74 20 72 6f 77 20 rn the next row
16670 6f 66 20 72 65 73 75 6c 74 2e 0a 20 20 20 20 2a of result.. *
16680 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b /. default: {
16690 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
166a0 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 Dest->eDest==SRT
166b0 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20 20 _Output );.
166c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
166d0 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 p2(v, OP_ResultR
166e0 6f 77 2c 20 70 49 6e 2d 3e 69 53 64 73 74 2c 20 ow, pIn->iSdst,
166f0 70 49 6e 2d 3e 6e 53 64 73 74 29 3b 0a 20 20 20 pIn->nSdst);.
16700 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
16710 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 cheAffinityChang
16720 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 e(pParse, pIn->i
16730 53 64 73 74 2c 20 70 49 6e 2d 3e 6e 53 64 73 74 Sdst, pIn->nSdst
16740 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a );. break;.
16750 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 }. }.. /*
16760 4a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 Jump to the end
16770 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 of the loop if t
16780 68 65 20 4c 49 4d 49 54 20 69 73 20 72 65 61 63 he LIMIT is reac
16790 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 hed.. */. if(
167a0 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 p->iLimit ){.
167b0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
167c0 70 33 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c p3(v, OP_IfZero,
167d0 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42 72 65 p->iLimit, iBre
167e0 61 6b 2c 20 2d 31 29 3b 20 56 64 62 65 43 6f 76 ak, -1); VdbeCov
167f0 65 72 61 67 65 28 76 29 3b 0a 20 20 7d 0a 0a 20 erage(v);. }..
16800 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 /* Generate the
16810 20 73 75 62 72 6f 75 74 69 6e 65 20 72 65 74 75 subroutine retu
16820 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 rn. */. sqlite
16830 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 3VdbeResolveLabe
16840 6c 28 76 2c 20 69 43 6f 6e 74 69 6e 75 65 29 3b l(v, iContinue);
16850 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 . sqlite3VdbeAd
16860 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 dOp1(v, OP_Retur
16870 6e 2c 20 72 65 67 52 65 74 75 72 6e 29 3b 0a 0a n, regReturn);..
16880 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d return addr;.}
16890 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74 65 72 6e 61 74 ../*.** Alternat
168a0 69 76 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c ive compound sel
168b0 65 63 74 20 63 6f 64 65 20 67 65 6e 65 72 61 74 ect code generat
168c0 6f 72 20 66 6f 72 20 63 61 73 65 73 20 77 68 65 or for cases whe
168d0 6e 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61 6e n there.** is an
168e0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
168f0 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73 73 75 6d ..**.** We assum
16900 65 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 e a query of the
16910 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 3a following form:
16920 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 3c 73 65 6c .**.** <sel
16930 65 63 74 41 3e 20 20 3c 6f 70 65 72 61 74 6f 72 ectA> <operator
16940 3e 20 20 3c 73 65 6c 65 63 74 42 3e 20 20 4f 52 > <selectB> OR
16950 44 45 52 20 42 59 20 3c 6f 72 64 65 72 62 79 6c DER BY <orderbyl
16960 69 73 74 3e 0a 2a 2a 0a 2a 2a 20 3c 6f 70 65 72 ist>.**.** <oper
16970 61 74 6f 72 3e 20 69 73 20 6f 6e 65 20 6f 66 20 ator> is one of
16980 55 4e 49 4f 4e 20 41 4c 4c 2c 20 55 4e 49 4f 4e UNION ALL, UNION
16990 2c 20 45 58 43 45 50 54 2c 20 6f 72 20 49 4e 54 , EXCEPT, or INT
169a0 45 52 53 45 43 54 2e 20 20 54 68 65 20 69 64 65 ERSECT. The ide
169b0 61 0a 2a 2a 20 69 73 20 74 6f 20 63 6f 64 65 20 a.** is to code
169c0 62 6f 74 68 20 3c 73 65 6c 65 63 74 41 3e 20 61 both <selectA> a
169d0 6e 64 20 3c 73 65 6c 65 63 74 42 3e 20 77 69 74 nd <selectB> wit
169e0 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 h the ORDER BY c
169f0 6c 61 75 73 65 20 61 73 0a 2a 2a 20 63 6f 2d 72 lause as.** co-r
16a00 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 6e 20 72 outines. Then r
16a10 75 6e 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e un the co-routin
16a20 65 73 20 69 6e 20 70 61 72 61 6c 6c 65 6c 20 61 es in parallel a
16a30 6e 64 20 6d 65 72 67 65 20 74 68 65 20 72 65 73 nd merge the res
16a40 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 ults.** into the
16a50 20 6f 75 74 70 75 74 2e 20 20 49 6e 20 61 64 64 output. In add
16a60 69 74 69 6f 6e 20 74 6f 20 74 68 65 20 74 77 6f ition to the two
16a70 20 63 6f 72 6f 75 74 69 6e 65 73 20 28 63 61 6c coroutines (cal
16a80 6c 65 64 20 73 65 6c 65 63 74 41 20 61 6e 64 0a led selectA and.
16a90 2a 2a 20 73 65 6c 65 63 74 42 29 20 74 68 65 72 ** selectB) ther
16aa0 65 20 61 72 65 20 37 20 73 75 62 72 6f 75 74 69 e are 7 subrouti
16ab0 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 75 nes:.**.** ou
16ac0 74 41 3a 20 20 20 20 4d 6f 76 65 20 74 68 65 20 tA: Move the
16ad0 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20 73 65 output of the se
16ae0 6c 65 63 74 41 20 63 6f 72 6f 75 74 69 6e 65 20 lectA coroutine
16af0 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a into the output.
16b00 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 6f ** o
16b10 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 71 f the compound q
16b20 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 6f uery..**.** o
16b30 75 74 42 3a 20 20 20 20 4d 6f 76 65 20 74 68 65 utB: Move the
16b40 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65 20 73 output of the s
16b50 65 6c 65 63 74 42 20 63 6f 72 6f 75 74 69 6e 65 electB coroutine
16b60 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 into the output
16b70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 .**
16b80 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 of the compound
16b90 71 75 65 72 79 2e 20 20 28 4f 6e 6c 79 20 67 65 query. (Only ge
16ba0 6e 65 72 61 74 65 64 20 66 6f 72 20 55 4e 49 4f nerated for UNIO
16bb0 4e 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 N and.**
16bc0 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 2e 20 UNION ALL.
16bd0 20 45 58 43 45 50 54 20 61 6e 64 20 49 4e 53 45 EXCEPT and INSE
16be0 52 54 53 45 43 54 20 6e 65 76 65 72 20 6f 75 74 RTSECT never out
16bf0 70 75 74 20 61 20 72 6f 77 20 74 68 61 74 0a 2a put a row that.*
16c00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 61 70 * ap
16c10 70 65 61 72 73 20 6f 6e 6c 79 20 69 6e 20 42 2e pears only in B.
16c20 29 0a 2a 2a 0a 2a 2a 20 20 20 20 41 6c 74 42 3a ).**.** AltB:
16c30 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 Called when
16c40 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66 72 there is data fr
16c50 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e om both coroutin
16c60 65 73 20 61 6e 64 20 41 3c 42 2e 0a 2a 2a 0a 2a es and A<B..**.*
16c70 2a 20 20 20 20 41 65 71 42 3a 20 20 20 20 43 61 * AeqB: Ca
16c80 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 lled when there
16c90 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 is data from bot
16ca0 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 h coroutines and
16cb0 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 A==B..**.**
16cc0 41 67 74 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 AgtB: Called
16cd0 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 64 61 when there is da
16ce0 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 ta from both cor
16cf0 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3e 42 2e outines and A>B.
16d00 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20 .**.** EofA:
16d10 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 64 Called when d
16d20 61 74 61 20 69 73 20 65 78 68 61 75 73 74 65 64 ata is exhausted
16d30 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 2e 0a 2a from selectA..*
16d40 2a 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20 20 20 *.** EofB:
16d50 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 64 61 74 Called when dat
16d60 61 20 69 73 20 65 78 68 61 75 73 74 65 64 20 66 a is exhausted f
16d70 72 6f 6d 20 73 65 6c 65 63 74 42 2e 0a 2a 2a 0a rom selectB..**.
16d80 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 ** The implement
16d90 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 74 ation of the lat
16da0 74 65 72 20 66 69 76 65 20 73 75 62 72 6f 75 74 ter five subrout
16db0 69 6e 65 73 20 64 65 70 65 6e 64 20 6f 6e 20 77 ines depend on w
16dc0 68 69 63 68 20 0a 2a 2a 20 3c 6f 70 65 72 61 74 hich .** <operat
16dd0 6f 72 3e 20 69 73 20 75 73 65 64 3a 0a 2a 2a 0a or> is used:.**.
16de0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 **.**
16df0 20 20 55 4e 49 4f 4e 20 41 4c 4c 20 20 20 20 20 UNION ALL
16e00 20 20 20 20 55 4e 49 4f 4e 20 20 20 20 20 20 20 UNION
16e10 20 20 20 20 20 45 58 43 45 50 54 20 20 20 20 20 EXCEPT
16e20 20 20 20 20 20 49 4e 54 45 52 53 45 43 54 0a 2a INTERSECT.*
16e30 2a 20 20 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d * -----
16e40 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d -------- ------
16e50 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d ----------- ---
16e60 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d ----------- ---
16e70 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a --------------.*
16e80 2a 20 20 20 41 6c 74 42 3a 20 20 20 6f 75 74 41 * AltB: outA
16e90 2c 20 6e 65 78 74 41 20 20 20 20 20 20 6f 75 74 , nextA out
16ea0 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 6f A, nextA o
16eb0 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 utA, nextA
16ec0 20 20 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 nextA.**.**
16ed0 20 41 65 71 42 3a 20 20 20 6f 75 74 41 2c 20 6e AeqB: outA, n
16ee0 65 78 74 41 20 20 20 20 20 20 20 20 20 6e 65 78 extA nex
16ef0 74 41 20 20 20 20 20 20 20 20 20 20 20 20 20 6e tA n
16f00 65 78 74 41 20 20 20 20 20 20 20 20 20 6f 75 74 extA out
16f10 41 2c 20 6e 65 78 74 41 0a 2a 2a 0a 2a 2a 20 20 A, nextA.**.**
16f20 20 41 67 74 42 3a 20 20 20 6f 75 74 42 2c 20 6e AgtB: outB, n
16f30 65 78 74 42 20 20 20 20 20 20 6f 75 74 42 2c 20 extB outB,
16f40 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20 6e nextB n
16f50 65 78 74 42 20 20 20 20 20 20 20 20 20 20 20 20 extB
16f60 6e 65 78 74 42 0a 2a 2a 0a 2a 2a 20 20 20 45 6f nextB.**.** Eo
16f70 66 41 3a 20 20 20 6f 75 74 42 2c 20 6e 65 78 74 fA: outB, next
16f80 42 20 20 20 20 20 20 6f 75 74 42 2c 20 6e 65 78 B outB, nex
16f90 74 42 20 20 20 20 20 20 20 20 20 20 68 61 6c 74 tB halt
16fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 68 61 6c hal
16fb0 74 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66 42 3a 20 t.**.** EofB:
16fc0 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 outA, nextA
16fd0 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 outA, nextA
16fe0 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 outA, nextA
16ff0 20 20 20 20 20 20 20 20 20 68 61 6c 74 0a 2a 2a halt.**
17000 0a 2a 2a 20 49 6e 20 74 68 65 20 41 6c 74 42 2c .** In the AltB,
17010 20 41 65 71 42 2c 20 61 6e 64 20 41 67 74 42 20 AeqB, and AgtB
17020 73 75 62 72 6f 75 74 69 6e 65 73 2c 20 61 6e 20 subroutines, an
17030 45 4f 46 20 6f 6e 20 41 20 66 6f 6c 6c 6f 77 69 EOF on A followi
17040 6e 67 20 6e 65 78 74 41 0a 2a 2a 20 63 61 75 73 ng nextA.** caus
17050 65 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20 es an immediate
17060 6a 75 6d 70 20 74 6f 20 45 6f 66 41 20 61 6e 64 jump to EofA and
17070 20 61 6e 20 45 4f 46 20 6f 6e 20 42 20 66 6f 6c an EOF on B fol
17080 6c 6f 77 69 6e 67 20 6e 65 78 74 42 20 63 61 75 lowing nextB cau
17090 73 65 73 0a 2a 2a 20 61 6e 20 69 6d 6d 65 64 69 ses.** an immedi
170a0 61 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66 42 ate jump to EofB
170b0 2e 20 20 57 69 74 68 69 6e 20 45 6f 66 41 20 61 . Within EofA a
170c0 6e 64 20 45 6f 66 42 2c 20 61 6e 64 20 45 4f 46 nd EofB, and EOF
170d0 20 6f 6e 20 65 6e 74 72 79 20 6f 72 0a 2a 2a 20 on entry or.**
170e0 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 58 20 following nextX
170f0 63 61 75 73 65 73 20 61 20 6a 75 6d 70 20 74 6f causes a jump to
17100 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 the end of the
17110 73 65 6c 65 63 74 20 70 72 6f 63 65 73 73 69 6e select processin
17120 67 2e 0a 2a 2a 0a 2a 2a 20 44 75 70 6c 69 63 61 g..**.** Duplica
17130 74 65 20 72 65 6d 6f 76 61 6c 20 69 6e 20 74 68 te removal in th
17140 65 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c e UNION, EXCEPT,
17150 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20 63 and INTERSECT c
17160 61 73 65 73 20 69 73 20 68 61 6e 64 6c 65 64 0a ases is handled.
17170 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 6f 75 ** within the ou
17180 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 2e tput subroutine.
17190 20 20 54 68 65 20 72 65 67 50 72 65 76 20 72 65 The regPrev re
171a0 67 69 73 74 65 72 20 73 65 74 20 68 6f 6c 64 73 gister set holds
171b0 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 0a the previously.
171c0 2a 2a 20 6f 75 74 70 75 74 20 76 61 6c 75 65 2e ** output value.
171d0 20 20 41 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 A comparison i
171e0 73 20 6d 61 64 65 20 61 67 61 69 6e 73 74 20 74 s made against t
171f0 68 69 73 20 76 61 6c 75 65 20 61 6e 64 20 74 68 his value and th
17200 65 20 6f 75 74 70 75 74 0a 2a 2a 20 69 73 20 73 e output.** is s
17210 6b 69 70 70 65 64 20 69 66 20 74 68 65 20 6e 65 kipped if the ne
17220 78 74 20 72 65 73 75 6c 74 73 20 77 6f 75 6c 64 xt results would
17230 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 be the same as
17240 74 68 65 20 70 72 65 76 69 6f 75 73 2e 0a 2a 2a the previous..**
17250 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e .** The implemen
17260 74 61 74 69 6f 6e 20 70 6c 61 6e 20 69 73 20 74 tation plan is t
17270 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
17280 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 two coroutines a
17290 6e 64 20 73 65 76 65 6e 0a 2a 2a 20 73 75 62 72 nd seven.** subr
172a0 6f 75 74 69 6e 65 73 20 66 69 72 73 74 2c 20 74 outines first, t
172b0 68 65 6e 20 70 75 74 20 74 68 65 20 63 6f 6e 74 hen put the cont
172c0 72 6f 6c 20 6c 6f 67 69 63 20 61 74 20 74 68 65 rol logic at the
172d0 20 62 6f 74 74 6f 6d 2e 20 20 4c 69 6b 65 20 74 bottom. Like t
172e0 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 his:.**.**
172f0 20 20 20 20 67 6f 74 6f 20 49 6e 69 74 0a 2a 2a goto Init.**
17300 20 20 20 20 20 63 6f 41 3a 20 63 6f 72 6f 75 74 coA: corout
17310 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 71 75 65 ine for left que
17320 72 79 20 28 41 29 0a 2a 2a 20 20 20 20 20 63 6f ry (A).** co
17330 42 3a 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 B: coroutine for
17340 20 72 69 67 68 74 20 71 75 65 72 79 20 28 42 29 right query (B)
17350 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20 6f 75 74 .** outA: out
17360 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20 41 put one row of A
17370 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20 6f 75 74 .** outB: out
17380 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f 66 20 42 put one row of B
17390 20 28 55 4e 49 4f 4e 20 61 6e 64 20 55 4e 49 4f (UNION and UNIO
173a0 4e 20 41 4c 4c 20 6f 6e 6c 79 29 0a 2a 2a 20 20 N ALL only).**
173b0 20 20 45 6f 66 41 3a 20 2e 2e 2e 0a 2a 2a 20 20 EofA: ....**
173c0 20 20 45 6f 66 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 EofB: ....**
173d0 20 20 41 6c 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 AltB: ....**
173e0 20 20 41 65 71 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 AeqB: ....**
173f0 20 20 41 67 74 42 3a 20 2e 2e 2e 0a 2a 2a 20 20 AgtB: ....**
17400 20 20 49 6e 69 74 3a 20 69 6e 69 74 69 61 6c 69 Init: initiali
17410 7a 65 20 63 6f 72 6f 75 74 69 6e 65 20 72 65 67 ze coroutine reg
17420 69 73 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 isters.**
17430 20 20 20 79 69 65 6c 64 20 63 6f 41 0a 2a 2a 20 yield coA.**
17440 20 20 20 20 20 20 20 20 20 69 66 20 65 6f 66 28 if eof(
17450 41 29 20 67 6f 74 6f 20 45 6f 66 41 0a 2a 2a 20 A) goto EofA.**
17460 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20 63 yield c
17470 6f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 oB.** i
17480 66 20 65 6f 66 28 42 29 20 67 6f 74 6f 20 45 6f f eof(B) goto Eo
17490 66 42 0a 2a 2a 20 20 20 20 43 6d 70 72 3a 20 43 fB.** Cmpr: C
174a0 6f 6d 70 61 72 65 20 41 2c 20 42 0a 2a 2a 20 20 ompare A, B.**
174b0 20 20 20 20 20 20 20 20 4a 75 6d 70 20 41 6c 74 Jump Alt
174c0 42 2c 20 41 65 71 42 2c 20 41 67 74 42 0a 2a 2a B, AeqB, AgtB.**
174d0 20 20 20 20 20 45 6e 64 3a 20 2e 2e 2e 0a 2a 2a End: ....**
174e0 0a 2a 2a 20 57 65 20 63 61 6c 6c 20 41 6c 74 42 .** We call AltB
174f0 2c 20 41 65 71 42 2c 20 41 67 74 42 2c 20 45 6f , AeqB, AgtB, Eo
17500 66 41 2c 20 61 6e 64 20 45 6f 66 42 20 22 73 75 fA, and EofB "su
17510 62 72 6f 75 74 69 6e 65 73 22 20 62 75 74 20 74 broutines" but t
17520 68 65 79 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61 hey are not.** a
17530 63 74 75 61 6c 6c 79 20 63 61 6c 6c 65 64 20 75 ctually called u
17540 73 69 6e 67 20 47 6f 73 75 62 20 61 6e 64 20 74 sing Gosub and t
17550 68 65 79 20 64 6f 20 6e 6f 74 20 52 65 74 75 72 hey do not Retur
17560 6e 2e 20 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 n. EofA and Eof
17570 42 20 6c 6f 6f 70 0a 2a 2a 20 75 6e 74 69 6c 20 B loop.** until
17580 61 6c 6c 20 64 61 74 61 20 69 73 20 65 78 68 61 all data is exha
17590 75 73 74 65 64 20 74 68 65 6e 20 6a 75 6d 70 20 usted then jump
175a0 74 6f 20 74 68 65 20 22 65 6e 64 22 20 6c 61 62 to the "end" lab
175b0 65 2e 20 20 41 6c 74 42 2c 20 41 65 71 42 2c 0a e. AltB, AeqB,.
175c0 2a 2a 20 61 6e 64 20 41 67 74 42 20 6a 75 6d 70 ** and AgtB jump
175d0 20 74 6f 20 65 69 74 68 65 72 20 4c 32 20 6f 72 to either L2 or
175e0 20 74 6f 20 6f 6e 65 20 6f 66 20 45 6f 66 41 20 to one of EofA
175f0 6f 72 20 45 6f 66 42 2e 0a 2a 2f 0a 23 69 66 6e or EofB..*/.#ifn
17600 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
17610 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a COMPOUND_SELECT.
17620 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 static int multi
17630 53 65 6c 65 63 74 4f 72 64 65 72 42 79 28 0a 20 SelectOrderBy(.
17640 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 Parse *pParse,
17650 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e /* Parsin
17660 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 g context */. S
17670 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 elect *p,
17680 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 /* The righ
17690 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 t-most of SELECT
176a0 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f s to be coded */
176b0 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 . SelectDest *p
176c0 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74 Dest /* What
176d0 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72 to do with quer
176e0 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a y results */.){.
176f0 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 int i, j;
17700 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
17710 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 53 65 counters */. Se
17720 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 lect *pPrior;
17730 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53 /* Another S
17740 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65 6c ELECT immediatel
17750 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f y to our left */
17760 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 . Vdbe *v;
17770 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 /* Gene
17780 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68 69 rate code to thi
17790 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c 65 s VDBE */. Sele
177a0 63 74 44 65 73 74 20 64 65 73 74 41 3b 20 20 20 ctDest destA;
177b0 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e /* Destination
177c0 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20 41 for coroutine A
177d0 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 */. SelectDest
177e0 20 64 65 73 74 42 3b 20 20 20 20 20 2f 2a 20 44 destB; /* D
177f0 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 estination for c
17800 6f 72 6f 75 74 69 6e 65 20 42 20 2a 2f 0a 20 20 oroutine B */.
17810 69 6e 74 20 72 65 67 41 64 64 72 41 3b 20 20 20 int regAddrA;
17820 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 /* Address
17830 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65 register for se
17840 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 lect-A coroutine
17850 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64 64 */. int regAdd
17860 72 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 rB; /* A
17870 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 ddress register
17880 66 6f 72 20 73 65 6c 65 63 74 2d 42 20 63 6f 72 for select-B cor
17890 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 outine */. int
178a0 61 64 64 72 53 65 6c 65 63 74 41 3b 20 20 20 20 addrSelectA;
178b0 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
178c0 74 68 65 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 the select-A cor
178d0 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 outine */. int
178e0 61 64 64 72 53 65 6c 65 63 74 42 3b 20 20 20 20 addrSelectB;
178f0 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 /* Address of
17900 74 68 65 20 73 65 6c 65 63 74 2d 42 20 63 6f 72 the select-B cor
17910 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 outine */. int
17920 72 65 67 4f 75 74 41 3b 20 20 20 20 20 20 20 20 regOutA;
17930 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67 /* Address reg
17940 69 73 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75 ister for the ou
17950 74 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69 6e tput-A subroutin
17960 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 e */. int regOu
17970 74 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 tB; /*
17980 41 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 Address register
17990 20 66 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d for the output-
179a0 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a B subroutine */.
179b0 20 20 69 6e 74 20 61 64 64 72 4f 75 74 41 3b 20 int addrOutA;
179c0 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 /* Addre
179d0 73 73 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 ss of the output
179e0 2d 41 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f -A subroutine */
179f0 0a 20 20 69 6e 74 20 61 64 64 72 4f 75 74 42 20 . int addrOutB
17a00 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 = 0; /* Addr
17a10 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74 70 75 ess of the outpu
17a20 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a t-B subroutine *
17a30 2f 0a 20 20 69 6e 74 20 61 64 64 72 45 6f 66 41 /. int addrEofA
17a40 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 ; /* Add
17a50 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 ress of the sele
17a60 63 74 2d 41 2d 65 78 68 61 75 73 74 65 64 20 73 ct-A-exhausted s
17a70 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 ubroutine */. i
17a80 6e 74 20 61 64 64 72 45 6f 66 41 5f 6e 6f 42 3b nt addrEofA_noB;
17a90 20 20 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74 /* Alternat
17aa0 65 20 61 64 64 72 45 6f 66 41 20 69 66 20 42 20 e addrEofA if B
17ab0 69 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 is uninitialized
17ac0 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 45 6f */. int addrEo
17ad0 66 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 fB; /* A
17ae0 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 ddress of the se
17af0 6c 65 63 74 2d 42 2d 65 78 68 61 75 73 74 65 64 lect-B-exhausted
17b00 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 subroutine */.
17b10 20 69 6e 74 20 61 64 64 72 41 6c 74 42 3b 20 20 int addrAltB;
17b20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 /* Addres
17b30 73 20 6f 66 20 74 68 65 20 41 3c 42 20 73 75 62 s of the A<B sub
17b40 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 routine */. int
17b50 20 61 64 64 72 41 65 71 42 3b 20 20 20 20 20 20 addrAeqB;
17b60 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 /* Address of
17b70 20 74 68 65 20 41 3d 3d 42 20 73 75 62 72 6f 75 the A==B subrou
17b80 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 tine */. int ad
17b90 64 72 41 67 74 42 3b 20 20 20 20 20 20 20 20 20 drAgtB;
17ba0 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 /* Address of th
17bb0 65 20 41 3e 42 20 73 75 62 72 6f 75 74 69 6e 65 e A>B subroutine
17bc0 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d */. int regLim
17bd0 69 74 41 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c itA; /* L
17be0 69 6d 69 74 20 72 65 67 69 73 74 65 72 20 66 6f imit register fo
17bf0 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 r select-A */.
17c00 69 6e 74 20 72 65 67 4c 69 6d 69 74 42 3b 20 20 int regLimitB;
17c10 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 /* Limit r
17c20 65 67 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 egister for sele
17c30 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 ct-A */. int re
17c40 67 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 gPrev;
17c50 2f 2a 20 41 20 72 61 6e 67 65 20 6f 66 20 72 65 /* A range of re
17c60 67 69 73 74 65 72 73 20 74 6f 20 68 6f 6c 64 20 gisters to hold
17c70 70 72 65 76 69 6f 75 73 20 6f 75 74 70 75 74 20 previous output
17c80 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 4c 69 */. int savedLi
17c90 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 mit; /* Sa
17ca0 76 65 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e ved value of p->
17cb0 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 iLimit */. int
17cc0 73 61 76 65 64 4f 66 66 73 65 74 3b 20 20 20 20 savedOffset;
17cd0 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 /* Saved value
17ce0 20 6f 66 20 70 2d 3e 69 4f 66 66 73 65 74 20 2a of p->iOffset *
17cf0 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 43 6d 70 /. int labelCmp
17d00 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 r; /* Lab
17d10 65 6c 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 el for the start
17d20 20 6f 66 20 74 68 65 20 6d 65 72 67 65 20 61 6c of the merge al
17d30 67 6f 72 69 74 68 6d 20 2a 2f 0a 20 20 69 6e 74 gorithm */. int
17d40 20 6c 61 62 65 6c 45 6e 64 3b 20 20 20 20 20 20 labelEnd;
17d50 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 /* Label for
17d60 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6f the end of the o
17d70 76 65 72 61 6c 6c 20 53 45 4c 45 43 54 20 73 74 verall SELECT st
17d80 6d 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 mt */. int j1;
17d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
17da0 20 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f Jump instructio
17db0 6e 73 20 74 68 61 74 20 67 65 74 20 72 65 74 61 ns that get reta
17dc0 72 67 65 74 74 65 64 20 2a 2f 0a 20 20 69 6e 74 rgetted */. int
17dd0 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 op;
17de0 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f /* One of TK_
17df0 41 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54 ALL, TK_UNION, T
17e00 4b 5f 45 58 43 45 50 54 2c 20 54 4b 5f 49 4e 54 K_EXCEPT, TK_INT
17e10 45 52 53 45 43 54 20 2a 2f 0a 20 20 4b 65 79 49 ERSECT */. KeyI
17e20 6e 66 6f 20 2a 70 4b 65 79 44 75 70 20 3d 20 30 nfo *pKeyDup = 0
17e30 3b 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 ; /* Comparison
17e40 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 information for
17e50 64 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61 duplicate remova
17e60 6c 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a l */. KeyInfo *
17e70 70 4b 65 79 4d 65 72 67 65 3b 20 20 20 2f 2a 20 pKeyMerge; /*
17e80 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72 Comparison infor
17e90 6d 61 74 69 6f 6e 20 66 6f 72 20 6d 65 72 67 69 mation for mergi
17ea0 6e 67 20 72 6f 77 73 20 2a 2f 0a 20 20 73 71 6c ng rows */. sql
17eb0 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 ite3 *db;
17ec0 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 /* Database c
17ed0 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 onnection */. E
17ee0 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 xprList *pOrderB
17ef0 79 3b 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 y; /* The ORDE
17f00 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 R BY clause */.
17f10 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 int nOrderBy;
17f20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 /* Number
17f30 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 of terms in the
17f40 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 ORDER BY clause
17f50 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d */. int *aPerm
17f60 75 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d ute; /* M
17f70 61 70 70 69 6e 67 20 66 72 6f 6d 20 4f 52 44 45 apping from ORDE
17f80 52 20 42 59 20 74 65 72 6d 73 20 74 6f 20 72 65 R BY terms to re
17f90 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 sult set columns
17fa0 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 */.#ifndef SQLI
17fb0 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a TE_OMIT_EXPLAIN.
17fc0 20 20 69 6e 74 20 69 53 75 62 31 3b 20 20 20 20 int iSub1;
17fd0 20 20 20 20 20 20 20 20 2f 2a 20 45 51 50 20 69 /* EQP i
17fe0 64 20 6f 66 20 6c 65 66 74 2d 68 61 6e 64 20 71 d of left-hand q
17ff0 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 53 uery */. int iS
18000 75 62 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 ub2;
18010 2f 2a 20 45 51 50 20 69 64 20 6f 66 20 72 69 67 /* EQP id of rig
18020 68 74 2d 68 61 6e 64 20 71 75 65 72 79 20 2a 2f ht-hand query */
18030 0a 23 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 .#endif.. asser
18040 74 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 21 3d t( p->pOrderBy!=
18050 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 0 );. assert( p
18060 4b 65 79 44 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 KeyDup==0 ); /*
18070 22 4d 61 6e 61 67 65 64 22 20 63 6f 64 65 20 6e "Managed" code n
18080 65 65 64 73 20 74 68 69 73 2e 20 20 54 69 63 6b eeds this. Tick
18090 65 74 20 23 33 33 38 32 2e 20 2a 2f 0a 20 20 64 et #3382. */. d
180a0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a b = pParse->db;.
180b0 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 v = pParse->pV
180c0 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 dbe;. assert( v
180d0 21 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 !=0 ); /*
180e0 41 6c 72 65 61 64 79 20 74 68 72 6f 77 6e 20 74 Already thrown t
180f0 68 65 20 65 72 72 6f 72 20 69 66 20 56 44 42 45 he error if VDBE
18100 20 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f alloc failed */
18110 0a 20 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73 71 . labelEnd = sq
18120 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 lite3VdbeMakeLab
18130 65 6c 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43 6d el(v);. labelCm
18140 70 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 pr = sqlite3Vdbe
18150 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a MakeLabel(v);...
18160 20 20 2f 2a 20 50 61 74 63 68 20 75 70 20 74 68 /* Patch up th
18170 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
18180 65 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d e. */. op = p-
18190 3e 6f 70 3b 20 20 0a 20 20 70 50 72 69 6f 72 20 >op; . pPrior
181a0 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 = p->pPrior;. a
181b0 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 ssert( pPrior->p
181c0 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 OrderBy==0 );.
181d0 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f pOrderBy = p->pO
181e0 72 64 65 72 42 79 3b 0a 20 20 61 73 73 65 72 74 rderBy;. assert
181f0 28 20 70 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 ( pOrderBy );.
18200 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 nOrderBy = pOrde
18210 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f rBy->nExpr;.. /
18220 2a 20 46 6f 72 20 6f 70 65 72 61 74 6f 72 73 20 * For operators
18230 6f 74 68 65 72 20 74 68 61 6e 20 55 4e 49 4f 4e other than UNION
18240 20 41 4c 4c 20 77 65 20 68 61 76 65 20 74 6f 20 ALL we have to
18250 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20 make sure that.
18260 20 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 ** the ORDER BY
18270 20 63 6c 61 75 73 65 20 63 6f 76 65 72 73 20 65 clause covers e
18280 76 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 very term of the
18290 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 41 64 result set. Ad
182a0 64 0a 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f 20 d. ** terms to
182b0 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 the ORDER BY cla
182c0 75 73 65 20 61 73 20 6e 65 63 65 73 73 61 72 79 use as necessary
182d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 21 .. */. if( op!
182e0 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 66 =TK_ALL ){. f
182f0 6f 72 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c or(i=1; db->mall
18300 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 69 ocFailed==0 && i
18310 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 <=p->pEList->nEx
18320 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 pr; i++){.
18330 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f struct ExprList_
18340 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 item *pItem;.
18350 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65 for(j=0, pIte
18360 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a m=pOrderBy->a; j
18370 3c 6e 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c 20 <nOrderBy; j++,
18380 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 pItem++){.
18390 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d assert( pItem-
183a0 3e 75 2e 78 2e 69 4f 72 64 65 72 42 79 43 6f 6c >u.x.iOrderByCol
183b0 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 >0 );. if
183c0 28 20 70 49 74 65 6d 2d 3e 75 2e 78 2e 69 4f 72 ( pItem->u.x.iOr
183d0 64 65 72 42 79 43 6f 6c 3d 3d 69 20 29 20 62 72 derByCol==i ) br
183e0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
183f0 20 20 20 69 66 28 20 6a 3d 3d 6e 4f 72 64 65 72 if( j==nOrder
18400 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 By ){. Ex
18410 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69 74 pr *pNew = sqlit
18420 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e e3Expr(db, TK_IN
18430 54 45 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20 TEGER, 0);.
18440 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 if( pNew==0 )
18450 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e return SQLITE_N
18460 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70 4e OMEM;. pN
18470 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f ew->flags |= EP_
18480 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 20 20 IntValue;.
18490 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61 6c 75 65 pNew->u.iValue
184a0 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 70 4f = i;. pO
184b0 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33 rderBy = sqlite3
184c0 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 ExprListAppend(p
184d0 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c Parse, pOrderBy,
184e0 20 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 20 pNew);.
184f0 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 20 70 if( pOrderBy ) p
18500 4f 72 64 65 72 42 79 2d 3e 61 5b 6e 4f 72 64 65 OrderBy->a[nOrde
18510 72 42 79 2b 2b 5d 2e 75 2e 78 2e 69 4f 72 64 65 rBy++].u.x.iOrde
18520 72 42 79 43 6f 6c 20 3d 20 28 75 31 36 29 69 3b rByCol = (u16)i;
18530 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
18540 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 }.. /* Compute
18550 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 the comparison
18560 70 65 72 6d 75 74 61 74 69 6f 6e 20 61 6e 64 20 permutation and
18570 6b 65 79 69 6e 66 6f 20 74 68 61 74 20 69 73 20 keyinfo that is
18580 75 73 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 74 used with. ** t
18590 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 he permutation u
185a0 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 sed to determine
185b0 20 69 66 20 74 68 65 20 6e 65 78 74 0a 20 20 2a if the next. *
185c0 2a 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 73 * row of results
185d0 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73 65 6c 65 comes from sele
185e0 63 74 41 20 6f 72 20 73 65 6c 65 63 74 42 2e 20 ctA or selectB.
185f0 20 41 6c 73 6f 20 61 64 64 20 65 78 70 6c 69 63 Also add explic
18600 69 74 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f it. ** collatio
18610 6e 73 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 ns to the ORDER
18620 42 59 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 BY clause terms
18630 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 so that when the
18640 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 2a 2a subqueries. **
18650 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 61 6e to the right an
18660 64 20 74 68 65 20 6c 65 66 74 20 61 72 65 20 65 d the left are e
18670 76 61 6c 75 61 74 65 64 2c 20 74 68 65 79 20 75 valuated, they u
18680 73 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20 se the correct.
18690 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 20 ** collation..
186a0 20 2a 2f 0a 20 20 61 50 65 72 6d 75 74 65 20 3d */. aPermute =
186b0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 sqlite3DbMalloc
186c0 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 Raw(db, sizeof(i
186d0 6e 74 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0a 20 nt)*nOrderBy);.
186e0 20 69 66 28 20 61 50 65 72 6d 75 74 65 20 29 7b if( aPermute ){
186f0 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 . struct Expr
18700 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
18710 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 ;. for(i=0, p
18720 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 Item=pOrderBy->a
18730 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b ; i<nOrderBy; i+
18740 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 +, pItem++){.
18750 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d assert( pItem
18760 2d 3e 75 2e 78 2e 69 4f 72 64 65 72 42 79 43 6f ->u.x.iOrderByCo
18770 6c 3e 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 l>0. &&
18780 20 70 49 74 65 6d 2d 3e 75 2e 78 2e 69 4f 72 64 pItem->u.x.iOrd
18790 65 72 42 79 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 erByCol<=p->pELi
187a0 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 st->nExpr );.
187b0 20 20 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 3d aPermute[i] =
187c0 20 70 49 74 65 6d 2d 3e 75 2e 78 2e 69 4f 72 64 pItem->u.x.iOrd
187d0 65 72 42 79 43 6f 6c 20 2d 20 31 3b 0a 20 20 20 erByCol - 1;.
187e0 20 7d 0a 20 20 20 20 70 4b 65 79 4d 65 72 67 65 }. pKeyMerge
187f0 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 = multiSelectOr
18800 64 65 72 42 79 4b 65 79 49 6e 66 6f 28 70 50 61 derByKeyInfo(pPa
18810 72 73 65 2c 20 70 2c 20 31 29 3b 0a 20 20 7d 65 rse, p, 1);. }e
18820 6c 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d 65 72 lse{. pKeyMer
18830 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f ge = 0;. }.. /
18840 2a 20 52 65 61 74 74 61 63 68 20 74 68 65 20 4f * Reattach the O
18850 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74 RDER BY clause t
18860 6f 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a o the query.. *
18870 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 /. p->pOrderBy
18880 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 50 = pOrderBy;. pP
18890 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20 3d rior->pOrderBy =
188a0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
188b0 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 Dup(pParse->db,
188c0 70 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 0a 20 pOrderBy, 0);..
188d0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 72 /* Allocate a r
188e0 61 6e 67 65 20 6f 66 20 74 65 6d 70 6f 72 61 72 ange of temporar
188f0 79 20 72 65 67 69 73 74 65 72 73 20 61 6e 64 20 y registers and
18900 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64 the KeyInfo need
18910 65 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 ed. ** for the
18920 6c 6f 67 69 63 20 74 68 61 74 20 72 65 6d 6f 76 logic that remov
18930 65 73 20 64 75 70 6c 69 63 61 74 65 20 72 65 73 es duplicate res
18940 75 6c 74 20 72 6f 77 73 20 77 68 65 6e 20 74 68 ult rows when th
18950 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 e. ** operator
18960 69 73 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 is UNION, EXCEPT
18970 2c 20 6f 72 20 49 4e 54 45 52 53 45 43 54 20 28 , or INTERSECT (
18980 62 75 74 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c but not UNION AL
18990 4c 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f L).. */. if( o
189a0 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 p==TK_ALL ){.
189b0 20 72 65 67 50 72 65 76 20 3d 20 30 3b 0a 20 20 regPrev = 0;.
189c0 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e }else{. int n
189d0 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 Expr = p->pEList
189e0 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73 ->nExpr;. ass
189f0 65 72 74 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e ert( nOrderBy>=n
18a00 45 78 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c Expr || db->mall
18a10 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 ocFailed );.
18a20 72 65 67 50 72 65 76 20 3d 20 70 50 61 72 73 65 regPrev = pParse
18a30 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 ->nMem+1;. pP
18a40 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 45 arse->nMem += nE
18a50 78 70 72 2b 31 3b 0a 20 20 20 20 73 71 6c 69 74 xpr+1;. sqlit
18a60 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
18a70 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 OP_Integer, 0, r
18a80 65 67 50 72 65 76 29 3b 0a 20 20 20 20 70 4b 65 egPrev);. pKe
18a90 79 44 75 70 20 3d 20 73 71 6c 69 74 65 33 4b 65 yDup = sqlite3Ke
18aa0 79 49 6e 66 6f 41 6c 6c 6f 63 28 64 62 2c 20 6e yInfoAlloc(db, n
18ab0 45 78 70 72 2c 20 31 29 3b 0a 20 20 20 20 69 66 Expr, 1);. if
18ac0 28 20 70 4b 65 79 44 75 70 20 29 7b 0a 20 20 20 ( pKeyDup ){.
18ad0 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 assert( sqlit
18ae0 65 33 4b 65 79 49 6e 66 6f 49 73 57 72 69 74 65 e3KeyInfoIsWrite
18af0 61 62 6c 65 28 70 4b 65 79 44 75 70 29 20 29 3b able(pKeyDup) );
18b00 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 . for(i=0;
18b10 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 i<nExpr; i++){.
18b20 20 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e pKeyDup->
18b30 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c 74 69 aColl[i] = multi
18b40 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 SelectCollSeq(pP
18b50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20 arse, p, i);.
18b60 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53 pKeyDup->aS
18b70 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 30 3b ortOrder[i] = 0;
18b80 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 . }. }.
18b90 20 7d 0a 20 0a 20 20 2f 2a 20 53 65 70 61 72 61 }. . /* Separa
18ba0 74 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20 te the left and
18bb0 74 68 65 20 72 69 67 68 74 20 71 75 65 72 79 20 the right query
18bc0 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72 from one another
18bd0 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72 69 6f . */. p->pPrio
18be0 72 20 3d 20 30 3b 0a 20 20 70 50 72 69 6f 72 2d r = 0;. pPrior-
18bf0 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 73 71 >pNext = 0;. sq
18c00 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 lite3ResolveOrde
18c10 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65 2c rGroupBy(pParse,
18c20 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c p, p->pOrderBy,
18c30 20 22 4f 52 44 45 52 22 29 3b 0a 20 20 69 66 28 "ORDER");. if(
18c40 20 70 50 72 69 6f 72 2d 3e 70 50 72 69 6f 72 3d pPrior->pPrior=
18c50 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 =0 ){. sqlite
18c60 33 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 3ResolveOrderGro
18c70 75 70 42 79 28 70 50 61 72 73 65 2c 20 70 50 72 upBy(pParse, pPr
18c80 69 6f 72 2c 20 70 50 72 69 6f 72 2d 3e 70 4f 72 ior, pPrior->pOr
18c90 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 3b derBy, "ORDER");
18ca0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 . }.. /* Compu
18cb0 74 65 20 74 68 65 20 6c 69 6d 69 74 20 72 65 67 te the limit reg
18cc0 69 73 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6d 70 isters */. comp
18cd0 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 uteLimitRegister
18ce0 73 28 70 50 61 72 73 65 2c 20 70 2c 20 6c 61 62 s(pParse, p, lab
18cf0 65 6c 45 6e 64 29 3b 0a 20 20 69 66 28 20 70 2d elEnd);. if( p-
18d00 3e 69 4c 69 6d 69 74 20 26 26 20 6f 70 3d 3d 54 >iLimit && op==T
18d10 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 67 K_ALL ){. reg
18d20 4c 69 6d 69 74 41 20 3d 20 2b 2b 70 50 61 72 73 LimitA = ++pPars
18d30 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 e->nMem;. reg
18d40 4c 69 6d 69 74 42 20 3d 20 2b 2b 70 50 61 72 73 LimitB = ++pPars
18d50 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c e->nMem;. sql
18d60 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
18d70 2c 20 4f 50 5f 43 6f 70 79 2c 20 70 2d 3e 69 4f , OP_Copy, p->iO
18d80 66 66 73 65 74 20 3f 20 70 2d 3e 69 4f 66 66 73 ffset ? p->iOffs
18d90 65 74 2b 31 20 3a 20 70 2d 3e 69 4c 69 6d 69 74 et+1 : p->iLimit
18da0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
18db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
18dc0 20 20 20 20 72 65 67 4c 69 6d 69 74 41 29 3b 0a regLimitA);.
18dd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
18de0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 ddOp2(v, OP_Copy
18df0 2c 20 72 65 67 4c 69 6d 69 74 41 2c 20 72 65 67 , regLimitA, reg
18e00 4c 69 6d 69 74 42 29 3b 0a 20 20 7d 65 6c 73 65 LimitB);. }else
18e10 7b 0a 20 20 20 20 72 65 67 4c 69 6d 69 74 41 20 {. regLimitA
18e20 3d 20 72 65 67 4c 69 6d 69 74 42 20 3d 20 30 3b = regLimitB = 0;
18e30 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 . }. sqlite3Ex
18e40 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e prDelete(db, p->
18e50 70 4c 69 6d 69 74 29 3b 0a 20 20 70 2d 3e 70 4c pLimit);. p->pL
18e60 69 6d 69 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 imit = 0;. sqli
18e70 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 te3ExprDelete(db
18e80 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b 0a 20 , p->pOffset);.
18e90 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b p->pOffset = 0;
18ea0 0a 0a 20 20 72 65 67 41 64 64 72 41 20 3d 20 2b .. regAddrA = +
18eb0 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 +pParse->nMem;.
18ec0 20 72 65 67 41 64 64 72 42 20 3d 20 2b 2b 70 50 regAddrB = ++pP
18ed0 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 arse->nMem;. re
18ee0 67 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 gOutA = ++pParse
18ef0 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74 ->nMem;. regOut
18f00 42 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d B = ++pParse->nM
18f10 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c em;. sqlite3Sel
18f20 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 ectDestInit(&des
18f30 74 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e tA, SRT_Coroutin
18f40 65 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 e, regAddrA);.
18f50 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 sqlite3SelectDes
18f60 74 49 6e 69 74 28 26 64 65 73 74 42 2c 20 53 52 tInit(&destB, SR
18f70 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 T_Coroutine, reg
18f80 41 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20 47 65 AddrB);.. /* Ge
18f90 6e 65 72 61 74 65 20 61 20 63 6f 72 6f 75 74 69 nerate a corouti
18fa0 6e 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74 ne to evaluate t
18fb0 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d he SELECT statem
18fc0 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 ent to the. **
18fd0 6c 65 66 74 20 6f 66 20 74 68 65 20 63 6f 6d 70 left of the comp
18fe0 6f 75 6e 64 20 6f 70 65 72 61 74 6f 72 20 2d 20 ound operator -
18ff0 74 68 65 20 22 41 22 20 73 65 6c 65 63 74 2e 0a the "A" select..
19000 20 20 2a 2f 0a 20 20 61 64 64 72 53 65 6c 65 63 */. addrSelec
19010 74 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 tA = sqlite3Vdbe
19020 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b CurrentAddr(v) +
19030 20 31 3b 0a 20 20 6a 31 20 3d 20 73 71 6c 69 74 1;. j1 = sqlit
19040 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 e3VdbeAddOp3(v,
19050 4f 50 5f 49 6e 69 74 43 6f 72 6f 75 74 69 6e 65 OP_InitCoroutine
19060 2c 20 72 65 67 41 64 64 72 41 2c 20 30 2c 20 61 , regAddrA, 0, a
19070 64 64 72 53 65 6c 65 63 74 41 29 3b 0a 20 20 56 ddrSelectA);. V
19080 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
19090 6c 65 66 74 20 53 45 4c 45 43 54 22 29 29 3b 0a left SELECT"));.
190a0 20 20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 pPrior->iLimit
190b0 20 3d 20 72 65 67 4c 69 6d 69 74 41 3b 0a 20 20 = regLimitA;.
190c0 65 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 explainSetIntege
190d0 72 28 69 53 75 62 31 2c 20 70 50 61 72 73 65 2d r(iSub1, pParse-
190e0 3e 69 4e 65 78 74 53 65 6c 65 63 74 49 64 29 3b >iNextSelectId);
190f0 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 . sqlite3Select
19100 28 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c (pParse, pPrior,
19110 20 26 64 65 73 74 41 29 3b 0a 20 20 73 71 6c 69 &destA);. sqli
19120 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
19130 20 4f 50 5f 45 6e 64 43 6f 72 6f 75 74 69 6e 65 OP_EndCoroutine
19140 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73 , regAddrA);. s
19150 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
19160 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 2f 2a re(v, j1);.. /*
19170 20 47 65 6e 65 72 61 74 65 20 61 20 63 6f 72 6f Generate a coro
19180 75 74 69 6e 65 20 74 6f 20 65 76 61 6c 75 61 74 utine to evaluat
19190 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 e the SELECT sta
191a0 74 65 6d 65 6e 74 20 6f 6e 20 0a 20 20 2a 2a 20 tement on . **
191b0 74 68 65 20 72 69 67 68 74 20 2d 20 74 68 65 20 the right - the
191c0 22 42 22 20 73 65 6c 65 63 74 0a 20 20 2a 2f 0a "B" select. */.
191d0 20 20 61 64 64 72 53 65 6c 65 63 74 42 20 3d 20 addrSelectB =
191e0 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
191f0 6e 74 41 64 64 72 28 76 29 20 2b 20 31 3b 0a 20 ntAddr(v) + 1;.
19200 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 j1 = sqlite3Vdb
19210 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e eAddOp3(v, OP_In
19220 69 74 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 itCoroutine, reg
19230 41 64 64 72 42 2c 20 30 2c 20 61 64 64 72 53 65 AddrB, 0, addrSe
19240 6c 65 63 74 42 29 3b 0a 20 20 56 64 62 65 43 6f lectB);. VdbeCo
19250 6d 6d 65 6e 74 28 28 76 2c 20 22 72 69 67 68 74 mment((v, "right
19260 20 53 45 4c 45 43 54 22 29 29 3b 0a 20 20 73 61 SELECT"));. sa
19270 76 65 64 4c 69 6d 69 74 20 3d 20 70 2d 3e 69 4c vedLimit = p->iL
19280 69 6d 69 74 3b 0a 20 20 73 61 76 65 64 4f 66 66 imit;. savedOff
19290 73 65 74 20 3d 20 70 2d 3e 69 4f 66 66 73 65 74 set = p->iOffset
192a0 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 ;. p->iLimit =
192b0 72 65 67 4c 69 6d 69 74 42 3b 0a 20 20 70 2d 3e regLimitB;. p->
192c0 69 4f 66 66 73 65 74 20 3d 20 30 3b 20 20 0a 20 iOffset = 0; .
192d0 20 65 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 explainSetInteg
192e0 65 72 28 69 53 75 62 32 2c 20 70 50 61 72 73 65 er(iSub2, pParse
192f0 2d 3e 69 4e 65 78 74 53 65 6c 65 63 74 49 64 29 ->iNextSelectId)
19300 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 ;. sqlite3Selec
19310 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65 t(pParse, p, &de
19320 73 74 42 29 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69 stB);. p->iLimi
19330 74 20 3d 20 73 61 76 65 64 4c 69 6d 69 74 3b 0a t = savedLimit;.
19340 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 73 p->iOffset = s
19350 61 76 65 64 4f 66 66 73 65 74 3b 0a 20 20 73 71 avedOffset;. sq
19360 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 lite3VdbeAddOp1(
19370 76 2c 20 4f 50 5f 45 6e 64 43 6f 72 6f 75 74 69 v, OP_EndCorouti
19380 6e 65 2c 20 72 65 67 41 64 64 72 42 29 3b 0a 0a ne, regAddrB);..
19390 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 /* Generate a
193a0 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 subroutine that
193b0 6f 75 74 70 75 74 73 20 74 68 65 20 63 75 72 72 outputs the curr
193c0 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 41 ent row of the A
193d0 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20 61 73 20 . ** select as
193e0 74 68 65 20 6e 65 78 74 20 6f 75 74 70 75 74 20 the next output
193f0 72 6f 77 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f row of the compo
19400 75 6e 64 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f und select.. */
19410 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 . VdbeNoopComme
19420 6e 74 28 28 76 2c 20 22 4f 75 74 70 75 74 20 72 nt((v, "Output r
19430 6f 75 74 69 6e 65 20 66 6f 72 20 41 22 29 29 3b outine for A"));
19440 0a 20 20 61 64 64 72 4f 75 74 41 20 3d 20 67 65 . addrOutA = ge
19450 6e 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 nerateOutputSubr
19460 6f 75 74 69 6e 65 28 70 50 61 72 73 65 2c 0a 20 outine(pParse,.
19470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19480 70 2c 20 26 64 65 73 74 41 2c 20 70 44 65 73 74 p, &destA, pDest
19490 2c 20 72 65 67 4f 75 74 41 2c 0a 20 20 20 20 20 , regOutA,.
194a0 20 20 20 20 20 20 20 20 20 20 20 20 72 65 67 50 regP
194b0 72 65 76 2c 20 70 4b 65 79 44 75 70 2c 20 6c 61 rev, pKeyDup, la
194c0 62 65 6c 45 6e 64 29 3b 0a 20 20 0a 20 20 2f 2a belEnd);. . /*
194d0 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 Generate a subr
194e0 6f 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 outine that outp
194f0 75 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 uts the current
19500 72 6f 77 20 6f 66 20 74 68 65 20 42 0a 20 20 2a row of the B. *
19510 2a 20 73 65 6c 65 63 74 20 61 73 20 74 68 65 20 * select as the
19520 6e 65 78 74 20 6f 75 74 70 75 74 20 72 6f 77 20 next output row
19530 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 of the compound
19540 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 select.. */. i
19550 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c f( op==TK_ALL ||
19560 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b op==TK_UNION ){
19570 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d . VdbeNoopCom
19580 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74 70 75 74 ment((v, "Output
19590 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 42 22 29 routine for B")
195a0 29 3b 0a 20 20 20 20 61 64 64 72 4f 75 74 42 20 );. addrOutB
195b0 3d 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 = generateOutput
195c0 53 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72 73 Subroutine(pPars
195d0 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 e,.
195e0 20 20 20 20 70 2c 20 26 64 65 73 74 42 2c 20 70 p, &destB, p
195f0 44 65 73 74 2c 20 72 65 67 4f 75 74 42 2c 0a 20 Dest, regOutB,.
19600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19610 72 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75 70 regPrev, pKeyDup
19620 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d , labelEnd);. }
19630 0a 20 20 73 71 6c 69 74 65 33 4b 65 79 49 6e 66 . sqlite3KeyInf
19640 6f 55 6e 72 65 66 28 70 4b 65 79 44 75 70 29 3b oUnref(pKeyDup);
19650 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 .. /* Generate
19660 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 a subroutine to
19670 72 75 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 run when the res
19680 75 6c 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 ults from select
19690 20 41 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 A. ** are exha
196a0 75 73 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 usted and only d
196b0 61 74 61 20 69 6e 20 73 65 6c 65 63 74 20 42 20 ata in select B
196c0 72 65 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 remains.. */.
196d0 69 66 28 20 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 if( op==TK_EXCEP
196e0 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 T || op==TK_INTE
196f0 52 53 45 43 54 20 29 7b 0a 20 20 20 20 61 64 64 RSECT ){. add
19700 72 45 6f 66 41 5f 6e 6f 42 20 3d 20 61 64 64 72 rEofA_noB = addr
19710 45 6f 66 41 20 3d 20 6c 61 62 65 6c 45 6e 64 3b EofA = labelEnd;
19720 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 . }else{ .
19730 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 VdbeNoopComment(
19740 28 76 2c 20 22 65 6f 66 2d 41 20 73 75 62 72 6f (v, "eof-A subro
19750 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 utine"));. ad
19760 64 72 45 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 drEofA = sqlite3
19770 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 VdbeAddOp2(v, OP
19780 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c _Gosub, regOutB,
19790 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 20 20 addrOutB);.
197a0 61 64 64 72 45 6f 66 41 5f 6e 6f 42 20 3d 20 73 addrEofA_noB = s
197b0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
197c0 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 (v, OP_Yield, re
197d0 67 41 64 64 72 42 2c 20 6c 61 62 65 6c 45 6e 64 gAddrB, labelEnd
197e0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
197f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
19800 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 76 65 VdbeCove
19810 72 61 67 65 28 76 29 3b 0a 20 20 20 20 73 71 6c rage(v);. sql
19820 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
19830 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 , OP_Goto, 0, ad
19840 64 72 45 6f 66 41 29 3b 0a 20 20 20 20 70 2d 3e drEofA);. p->
19850 6e 53 65 6c 65 63 74 52 6f 77 20 2b 3d 20 70 50 nSelectRow += pP
19860 72 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 rior->nSelectRow
19870 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 ;. }.. /* Gene
19880 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e rate a subroutin
19890 65 20 74 6f 20 72 75 6e 20 77 68 65 6e 20 74 68 e to run when th
198a0 65 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 73 e results from s
198b0 65 6c 65 63 74 20 42 0a 20 20 2a 2a 20 61 72 65 elect B. ** are
198c0 20 65 78 68 61 75 73 74 65 64 20 61 6e 64 20 6f exhausted and o
198d0 6e 6c 79 20 64 61 74 61 20 69 6e 20 73 65 6c 65 nly data in sele
198e0 63 74 20 41 20 72 65 6d 61 69 6e 73 2e 0a 20 20 ct A remains..
198f0 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f */. if( op==TK_
19900 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 INTERSECT ){.
19910 20 61 64 64 72 45 6f 66 42 20 3d 20 61 64 64 72 addrEofB = addr
19920 45 6f 66 41 3b 0a 20 20 20 20 69 66 28 20 70 2d EofA;. if( p-
19930 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3e 20 70 50 >nSelectRow > pP
19940 72 69 6f 72 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 rior->nSelectRow
19950 20 29 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 ) p->nSelectRow
19960 20 3d 20 70 50 72 69 6f 72 2d 3e 6e 53 65 6c 65 = pPrior->nSele
19970 63 74 52 6f 77 3b 0a 20 20 7d 65 6c 73 65 7b 20 ctRow;. }else{
19980 20 0a 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f . VdbeNoopCo
19990 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6f 66 2d 42 mment((v, "eof-B
199a0 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a subroutine"));.
199b0 20 20 20 20 61 64 64 72 45 6f 66 42 20 3d 20 73 addrEofB = s
199c0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
199d0 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 (v, OP_Gosub, re
199e0 67 4f 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 gOutA, addrOutA)
199f0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 ;. sqlite3Vdb
19a00 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 59 69 eAddOp2(v, OP_Yi
19a10 65 6c 64 2c 20 72 65 67 41 64 64 72 41 2c 20 6c eld, regAddrA, l
19a20 61 62 65 6c 45 6e 64 29 3b 20 56 64 62 65 43 6f abelEnd); VdbeCo
19a30 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 73 verage(v);. s
19a40 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
19a50 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 (v, OP_Goto, 0,
19a60 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a 0a addrEofB);. }..
19a70 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f /* Generate co
19a80 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 de to handle the
19a90 20 63 61 73 65 20 6f 66 20 41 3c 42 0a 20 20 2a case of A<B. *
19aa0 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d /. VdbeNoopComm
19ab0 65 6e 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42 20 ent((v, "A-lt-B
19ac0 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 subroutine"));.
19ad0 20 61 64 64 72 41 6c 74 42 20 3d 20 73 71 6c 69 addrAltB = sqli
19ae0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
19af0 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 OP_Gosub, regOu
19b00 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 tA, addrOutA);.
19b10 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
19b20 70 32 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 p2(v, OP_Yield,
19b30 72 65 67 41 64 64 72 41 2c 20 61 64 64 72 45 6f regAddrA, addrEo
19b40 66 41 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 fA); VdbeCoverag
19b50 65 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 e(v);. sqlite3V
19b60 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
19b70 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c 43 6d Goto, 0, labelCm
19b80 70 72 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 pr);.. /* Gener
19b90 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 ate code to hand
19ba0 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 le the case of A
19bb0 3d 3d 42 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f ==B. */. if( o
19bc0 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 p==TK_ALL ){.
19bd0 20 61 64 64 72 41 65 71 42 20 3d 20 61 64 64 72 addrAeqB = addr
19be0 41 6c 74 42 3b 0a 20 20 7d 65 6c 73 65 20 69 66 AltB;. }else if
19bf0 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 ( op==TK_INTERSE
19c00 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65 CT ){. addrAe
19c10 71 42 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 qB = addrAltB;.
19c20 20 20 20 61 64 64 72 41 6c 74 42 2b 2b 3b 0a 20 addrAltB++;.
19c30 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 }else{. Vdbe
19c40 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 NoopComment((v,
19c50 22 41 2d 65 71 2d 42 20 73 75 62 72 6f 75 74 69 "A-eq-B subrouti
19c60 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 64 72 41 ne"));. addrA
19c70 65 71 42 20 3d 0a 20 20 20 20 73 71 6c 69 74 65 eqB =. sqlite
19c80 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
19c90 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 P_Yield, regAddr
19ca0 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 20 56 64 A, addrEofA); Vd
19cb0 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 beCoverage(v);.
19cc0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
19cd0 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c dOp2(v, OP_Goto,
19ce0 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0, labelCmpr);.
19cf0 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 }.. /* Genera
19d00 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c te code to handl
19d10 65 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 3e e the case of A>
19d20 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f B. */. VdbeNoo
19d30 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d pComment((v, "A-
19d40 67 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 gt-B subroutine"
19d50 29 29 3b 0a 20 20 61 64 64 72 41 67 74 42 20 3d ));. addrAgtB =
19d60 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
19d70 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69 66 entAddr(v);. if
19d80 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 ( op==TK_ALL ||
19d90 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a op==TK_UNION ){.
19da0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
19db0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 ddOp2(v, OP_Gosu
19dc0 62 2c 20 72 65 67 4f 75 74 42 2c 20 61 64 64 72 b, regOutB, addr
19dd0 4f 75 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71 6c OutB);. }. sql
19de0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
19df0 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 , OP_Yield, regA
19e00 64 64 72 42 2c 20 61 64 64 72 45 6f 66 42 29 3b ddrB, addrEofB);
19e10 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
19e20 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 ;. sqlite3VdbeA
19e30 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f ddOp2(v, OP_Goto
19e40 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b , 0, labelCmpr);
19e50 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 .. /* This code
19e60 20 72 75 6e 73 20 6f 6e 63 65 20 74 6f 20 69 6e runs once to in
19e70 69 74 69 61 6c 69 7a 65 20 65 76 65 72 79 74 68 itialize everyth
19e80 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 ing.. */. sqli
19e90 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 te3VdbeJumpHere(
19ea0 76 2c 20 6a 31 29 3b 0a 20 20 73 71 6c 69 74 65 v, j1);. sqlite
19eb0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
19ec0 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64 64 72 P_Yield, regAddr
19ed0 41 2c 20 61 64 64 72 45 6f 66 41 5f 6e 6f 42 29 A, addrEofA_noB)
19ee0 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 ; VdbeCoverage(v
19ef0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 );. sqlite3Vdbe
19f00 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 59 69 65 AddOp2(v, OP_Yie
19f10 6c 64 2c 20 72 65 67 41 64 64 72 42 2c 20 61 64 ld, regAddrB, ad
19f20 64 72 45 6f 66 42 29 3b 20 56 64 62 65 43 6f 76 drEofB); VdbeCov
19f30 65 72 61 67 65 28 76 29 3b 0a 0a 20 20 2f 2a 20 erage(v);.. /*
19f40 49 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 6d 61 Implement the ma
19f50 69 6e 20 6d 65 72 67 65 20 6c 6f 6f 70 0a 20 20 in merge loop.
19f60 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 */. sqlite3Vdbe
19f70 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 ResolveLabel(v,
19f80 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 20 20 73 71 labelCmpr);. sq
19f90 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 lite3VdbeAddOp4(
19fa0 76 2c 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f v, OP_Permutatio
19fb0 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 n, 0, 0, 0, (cha
19fc0 72 2a 29 61 50 65 72 6d 75 74 65 2c 20 50 34 5f r*)aPermute, P4_
19fd0 49 4e 54 41 52 52 41 59 29 3b 0a 20 20 73 71 6c INTARRAY);. sql
19fe0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 ite3VdbeAddOp4(v
19ff0 2c 20 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 64 65 , OP_Compare, de
1a000 73 74 41 2e 69 53 64 73 74 2c 20 64 65 73 74 42 stA.iSdst, destB
1a010 2e 69 53 64 73 74 2c 20 6e 4f 72 64 65 72 42 79 .iSdst, nOrderBy
1a020 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,.
1a030 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 (char
1a040 2a 29 70 4b 65 79 4d 65 72 67 65 2c 20 50 34 5f *)pKeyMerge, P4_
1a050 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 73 71 6c 69 KEYINFO);. sqli
1a060 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 te3VdbeChangeP5(
1a070 76 2c 20 4f 50 46 4c 41 47 5f 50 45 52 4d 55 54 v, OPFLAG_PERMUT
1a080 45 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 E);. sqlite3Vdb
1a090 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 eAddOp3(v, OP_Ju
1a0a0 6d 70 2c 20 61 64 64 72 41 6c 74 42 2c 20 61 64 mp, addrAltB, ad
1a0b0 64 72 41 65 71 42 2c 20 61 64 64 72 41 67 74 42 drAeqB, addrAgtB
1a0c0 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 ); VdbeCoverage(
1a0d0 76 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 v);.. /* Jump t
1a0e0 6f 20 74 68 65 20 74 68 69 73 20 70 6f 69 6e 74 o the this point
1a0f0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 74 65 72 in order to ter
1a100 6d 69 6e 61 74 65 20 74 68 65 20 71 75 65 72 79 minate the query
1a110 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
1a120 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c VdbeResolveLabel
1a130 28 76 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a (v, labelEnd);..
1a140 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e 75 6d /* Set the num
1a150 62 65 72 20 6f 66 20 6f 75 74 70 75 74 20 63 6f ber of output co
1a160 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 lumns. */. if(
1a170 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 pDest->eDest==S
1a180 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 RT_Output ){.
1a190 20 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 Select *pFirst
1a1a0 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 77 68 = pPrior;. wh
1a1b0 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72 ile( pFirst->pPr
1a1c0 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70 ior ) pFirst = p
1a1d0 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 First->pPrior;.
1a1e0 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d generateColum
1a1f0 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 30 nNames(pParse, 0
1a200 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 , pFirst->pEList
1a210 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 );. }.. /* Rea
1a220 73 73 65 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70 ssembly the comp
1a230 6f 75 6e 64 20 71 75 65 72 79 20 73 6f 20 74 68 ound query so th
1a240 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 66 72 at it will be fr
1a250 65 65 64 20 63 6f 72 72 65 63 74 6c 79 0a 20 20 eed correctly.
1a260 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e ** by the callin
1a270 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 g function */.
1a280 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b if( p->pPrior ){
1a290 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 . sqlite3Sele
1a2a0 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e ctDelete(db, p->
1a2b0 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 pPrior);. }. p
1a2c0 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f ->pPrior = pPrio
1a2d0 72 3b 0a 20 20 70 50 72 69 6f 72 2d 3e 70 4e 65 r;. pPrior->pNe
1a2e0 78 74 20 3d 20 70 3b 0a 0a 20 20 2f 2a 2a 2a 20 xt = p;.. /***
1a2f0 54 42 44 3a 20 20 49 6e 73 65 72 74 20 73 75 62 TBD: Insert sub
1a300 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 6f routine calls to
1a310 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 20 6f close cursors o
1a320 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 0a 20 20 2a n incomplete. *
1a330 2a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20 2a *** subqueries *
1a340 2a 2a 2a 2f 0a 20 20 65 78 70 6c 61 69 6e 43 6f ***/. explainCo
1a350 6d 70 6f 73 69 74 65 28 70 50 61 72 73 65 2c 20 mposite(pParse,
1a360 70 2d 3e 6f 70 2c 20 69 53 75 62 31 2c 20 69 53 p->op, iSub1, iS
1a370 75 62 32 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 ub2, 0);. retur
1a380 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 n SQLITE_OK;.}.#
1a390 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 endif..#if !defi
1a3a0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f ned(SQLITE_OMIT_
1a3b0 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 SUBQUERY) || !de
1a3c0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
1a3d0 54 5f 56 49 45 57 29 0a 2f 2a 20 46 6f 72 77 61 T_VIEW)./* Forwa
1a3e0 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e 73 20 rd Declarations
1a3f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
1a400 75 62 73 74 45 78 70 72 4c 69 73 74 28 73 71 6c ubstExprList(sql
1a410 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a ite3*, ExprList*
1a420 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a , int, ExprList*
1a430 29 3b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 );.static void s
1a440 75 62 73 74 53 65 6c 65 63 74 28 73 71 6c 69 74 ubstSelect(sqlit
1a450 65 33 2a 2c 20 53 65 6c 65 63 74 20 2a 2c 20 69 e3*, Select *, i
1a460 6e 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 29 3b nt, ExprList *);
1a470 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 72 ../*.** Scan thr
1a480 6f 75 67 68 20 74 68 65 20 65 78 70 72 65 73 73 ough the express
1a490 69 6f 6e 20 70 45 78 70 72 2e 20 20 52 65 70 6c ion pExpr. Repl
1a4a0 61 63 65 20 65 76 65 72 79 20 72 65 66 65 72 65 ace every refere
1a4b0 6e 63 65 20 74 6f 0a 2a 2a 20 61 20 63 6f 6c 75 nce to.** a colu
1a4c0 6d 6e 20 69 6e 20 74 61 62 6c 65 20 6e 75 6d 62 mn in table numb
1a4d0 65 72 20 69 54 61 62 6c 65 20 77 69 74 68 20 61 er iTable with a
1a4e0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f copy of the iCo
1a4f0 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65 6e 74 72 79 lumn-th.** entry
1a500 20 69 6e 20 70 45 4c 69 73 74 2e 20 20 28 42 75 in pEList. (Bu
1a510 74 20 6c 65 61 76 65 20 72 65 66 65 72 65 6e 63 t leave referenc
1a520 65 73 20 74 6f 20 74 68 65 20 52 4f 57 49 44 20 es to the ROWID
1a530 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75 6e 63 68 61 column .** uncha
1a540 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 69 nged.).**.** Thi
1a550 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70 61 72 s routine is par
1a560 74 20 6f 66 20 74 68 65 20 66 6c 61 74 74 65 6e t of the flatten
1a570 69 6e 67 20 70 72 6f 63 65 64 75 72 65 2e 20 20 ing procedure.
1a580 41 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 77 68 A subquery.** wh
1a590 6f 73 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 ose result set i
1a5a0 73 20 64 65 66 69 6e 65 64 20 62 79 20 70 45 4c s defined by pEL
1a5b0 69 73 74 20 61 70 70 65 61 72 73 20 61 73 20 65 ist appears as e
1a5c0 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a 2a 20 46 ntry in the.** F
1a5d0 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 ROM clause of a
1a5e0 53 45 4c 45 43 54 20 73 75 63 68 20 74 68 61 74 SELECT such that
1a5f0 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 the VDBE cursor
1a600 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 61 assigned to tha
1a610 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c 61 75 73 65 t.** FORM clause
1a620 20 65 6e 74 72 79 20 69 73 20 69 54 61 62 6c 65 entry is iTable
1a630 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 . This routine
1a640 6d 61 6b 65 20 74 68 65 20 6e 65 63 65 73 73 61 make the necessa
1a650 72 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 ry .** changes t
1a660 6f 20 70 45 78 70 72 20 73 6f 20 74 68 61 74 20 o pExpr so that
1a670 69 74 20 72 65 66 65 72 73 20 64 69 72 65 63 74 it refers direct
1a680 6c 79 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 ly to the source
1a690 20 74 61 62 6c 65 0a 2a 2a 20 6f 66 20 74 68 65 table.** of the
1a6a0 20 73 75 62 71 75 65 72 79 20 72 61 74 68 65 72 subquery rather
1a6b0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 the result set
1a6c0 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e of the subquery.
1a6d0 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 .*/.static Expr
1a6e0 2a 73 75 62 73 74 45 78 70 72 28 0a 20 20 73 71 *substExpr(. sq
1a6f0 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 lite3 *db,
1a700 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61 6c 6c /* Report mall
1a710 6f 63 20 65 72 72 6f 72 73 20 74 6f 20 74 68 69 oc errors to thi
1a720 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a s connection */.
1a730 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 Expr *pExpr,
1a740 20 20 20 20 20 20 2f 2a 20 45 78 70 72 20 69 6e /* Expr in
1a750 20 77 68 69 63 68 20 73 75 62 73 74 69 74 75 74 which substitut
1a760 69 6f 6e 20 6f 63 63 75 72 73 20 2a 2f 0a 20 20 ion occurs */.
1a770 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20 int iTable,
1a780 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 /* Table to
1a790 62 65 20 73 75 62 73 74 69 74 75 74 65 64 20 2a be substituted *
1a7a0 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 /. ExprList *pE
1a7b0 4c 69 73 74 20 20 20 20 2f 2a 20 53 75 62 73 74 List /* Subst
1a7c0 69 74 75 74 65 20 65 78 70 72 65 73 73 69 6f 6e itute expression
1a7d0 73 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45 s */.){. if( pE
1a7e0 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 xpr==0 ) return
1a7f0 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 0;. if( pExpr->
1a800 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 op==TK_COLUMN &&
1a810 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d pExpr->iTable==
1a820 69 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 66 iTable ){. if
1a830 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e ( pExpr->iColumn
1a840 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 <0 ){. pExp
1a850 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b r->op = TK_NULL;
1a860 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 . }else{.
1a870 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 Expr *pNew;.
1a880 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 assert( pELi
1a890 73 74 21 3d 30 20 26 26 20 70 45 78 70 72 2d 3e st!=0 && pExpr->
1a8a0 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74 2d 3e iColumn<pEList->
1a8b0 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61 nExpr );. a
1a8c0 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c ssert( pExpr->pL
1a8d0 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d eft==0 && pExpr-
1a8e0 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 >pRight==0 );.
1a8f0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
1a900 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 e3ExprDup(db, pE
1a910 4c 69 73 74 2d 3e 61 5b 70 45 78 70 72 2d 3e 69 List->a[pExpr->i
1a920 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 30 Column].pExpr, 0
1a930 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
1a940 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 ExprDelete(db, p
1a950 45 78 70 72 29 3b 0a 20 20 20 20 20 20 70 45 78 Expr);. pEx
1a960 70 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d pr = pNew;. }
1a970 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 45 . }else{. pE
1a980 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 73 75 62 xpr->pLeft = sub
1a990 73 74 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 stExpr(db, pExpr
1a9a0 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 2c ->pLeft, iTable,
1a9b0 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 70 45 pEList);. pE
1a9c0 78 70 72 2d 3e 70 52 69 67 68 74 20 3d 20 73 75 xpr->pRight = su
1a9d0 62 73 74 45 78 70 72 28 64 62 2c 20 70 45 78 70 bstExpr(db, pExp
1a9e0 72 2d 3e 70 52 69 67 68 74 2c 20 69 54 61 62 6c r->pRight, iTabl
1a9f0 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 e, pEList);.
1aa00 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 if( ExprHasPrope
1aa10 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 rty(pExpr, EP_xI
1aa20 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 sSelect) ){.
1aa30 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 62 substSelect(db
1aa40 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 , pExpr->x.pSele
1aa50 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 ct, iTable, pELi
1aa60 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a st);. }else{.
1aa70 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72 4c substExprL
1aa80 69 73 74 28 64 62 2c 20 70 45 78 70 72 2d 3e 78 ist(db, pExpr->x
1aa90 2e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c 20 .pList, iTable,
1aaa0 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 pEList);. }.
1aab0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 }. return pExp
1aac0 72 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 r;.}.static void
1aad0 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 0a substExprList(.
1aae0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 sqlite3 *db,
1aaf0 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 /* Report
1ab00 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 68 malloc errors h
1ab10 65 72 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ere */. ExprLis
1ab20 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a t *pList, /*
1ab30 20 4c 69 73 74 20 74 6f 20 73 63 61 6e 20 61 6e List to scan an
1ab40 64 20 69 6e 20 77 68 69 63 68 20 74 6f 20 6d 61 d in which to ma
1ab50 6b 65 20 73 75 62 73 74 69 74 75 74 65 73 20 2a ke substitutes *
1ab60 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 /. int iTable,
1ab70 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c /* Tabl
1ab80 65 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75 e to be substitu
1ab90 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 ted */. ExprLis
1aba0 74 20 2a 70 45 4c 69 73 74 20 20 20 20 20 2f 2a t *pEList /*
1abb0 20 53 75 62 73 74 69 74 75 74 65 20 76 61 6c 75 Substitute valu
1abc0 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 es */.){. int i
1abd0 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 ;. if( pList==0
1abe0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 ) return;. for
1abf0 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e (i=0; i<pList->n
1ac00 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 Expr; i++){.
1ac10 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 pList->a[i].pExp
1ac20 72 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 r = substExpr(db
1ac30 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 , pList->a[i].pE
1ac40 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c xpr, iTable, pEL
1ac50 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 ist);. }.}.stat
1ac60 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c ic void substSel
1ac70 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a ect(. sqlite3 *
1ac80 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 db, /* R
1ac90 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 eport malloc err
1aca0 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 ors here */. Se
1acb0 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 lect *p,
1acc0 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61 /* SELECT sta
1acd0 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20 tement in which
1ace0 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75 to make substitu
1acf0 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 69 tions */. int i
1ad00 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 Table,
1ad10 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 72 /* Table to be r
1ad20 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45 78 70 eplaced */. Exp
1ad30 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 rList *pEList
1ad40 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 /* Substitute
1ad50 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 53 values */.){. S
1ad60 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 rcList *pSrc;.
1ad70 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
1ad80 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e tem *pItem;. in
1ad90 74 20 69 3b 0a 20 20 69 66 28 20 21 70 20 29 20 t i;. if( !p )
1ada0 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73 74 45 return;. substE
1adb0 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 xprList(db, p->p
1adc0 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c 20 70 EList, iTable, p
1add0 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 45 EList);. substE
1ade0 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70 xprList(db, p->p
1adf0 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c 65 2c GroupBy, iTable,
1ae00 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 pEList);. subs
1ae10 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d tExprList(db, p-
1ae20 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61 62 6c >pOrderBy, iTabl
1ae30 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 70 2d e, pEList);. p-
1ae40 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74 >pHaving = subst
1ae50 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 48 61 76 Expr(db, p->pHav
1ae60 69 6e 67 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c ing, iTable, pEL
1ae70 69 73 74 29 3b 0a 20 20 70 2d 3e 70 57 68 65 72 ist);. p->pWher
1ae80 65 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 e = substExpr(db
1ae90 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 69 54 61 , p->pWhere, iTa
1aea0 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 ble, pEList);.
1aeb0 73 75 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20 substSelect(db,
1aec0 70 2d 3e 70 50 72 69 6f 72 2c 20 69 54 61 62 6c p->pPrior, iTabl
1aed0 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 70 53 e, pEList);. pS
1aee0 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 rc = p->pSrc;.
1aef0 61 73 73 65 72 74 28 20 70 53 72 63 20 29 3b 20 assert( pSrc );
1af00 20 2f 2a 20 45 76 65 6e 20 66 6f 72 20 28 53 45 /* Even for (SE
1af10 4c 45 43 54 20 31 29 20 77 65 20 68 61 76 65 3a LECT 1) we have:
1af20 20 70 53 72 63 21 3d 30 20 62 75 74 20 70 53 72 pSrc!=0 but pSr
1af30 63 2d 3e 6e 53 72 63 3d 3d 30 20 2a 2f 0a 20 20 c->nSrc==0 */.
1af40 69 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 29 if( ALWAYS(pSrc)
1af50 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53 ){. for(i=pS
1af60 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d rc->nSrc, pItem=
1af70 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d pSrc->a; i>0; i-
1af80 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 -, pItem++){.
1af90 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 substSelect(d
1afa0 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 b, pItem->pSelec
1afb0 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73 t, iTable, pELis
1afc0 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a t);. }. }.}.
1afd0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e #endif /* !defin
1afe0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ed(SQLITE_OMIT_S
1aff0 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66 UBQUERY) || !def
1b000 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 ined(SQLITE_OMIT
1b010 5f 56 49 45 57 29 20 2a 2f 0a 0a 23 69 66 20 21 _VIEW) */..#if !
1b020 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
1b030 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c MIT_SUBQUERY) ||
1b040 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 !defined(SQLITE
1b050 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 0a 2a _OMIT_VIEW)./*.*
1b060 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 * This routine a
1b070 74 74 65 6d 70 74 73 20 74 6f 20 66 6c 61 74 74 ttempts to flatt
1b080 65 6e 20 73 75 62 71 75 65 72 69 65 73 20 61 73 en subqueries as
1b090 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6f a performance o
1b0a0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 ptimization..**
1b0b0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 This routine ret
1b0c0 75 72 6e 73 20 31 20 69 66 20 69 74 20 6d 61 6b urns 1 if it mak
1b0d0 65 73 20 63 68 61 6e 67 65 73 20 61 6e 64 20 30 es changes and 0
1b0e0 20 69 66 20 6e 6f 20 66 6c 61 74 74 65 6e 69 6e if no flattenin
1b0f0 67 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 g occurs..**.**
1b100 54 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68 To understand th
1b110 65 20 63 6f 6e 63 65 70 74 20 6f 66 20 66 6c 61 e concept of fla
1b120 74 74 65 6e 69 6e 67 2c 20 63 6f 6e 73 69 64 65 ttening, conside
1b130 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a r the following.
1b140 2a 2a 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 ** query:.**.**
1b150 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52 4f SELECT a FRO
1b160 4d 20 28 53 45 4c 45 43 54 20 78 2b 79 20 41 53 M (SELECT x+y AS
1b170 20 61 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 a FROM t1 WHERE
1b180 20 7a 3c 31 30 30 29 20 57 48 45 52 45 20 61 3e z<100) WHERE a>
1b190 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61 5.**.** The defa
1b1a0 75 6c 74 20 77 61 79 20 6f 66 20 69 6d 70 6c 65 ult way of imple
1b1b0 6d 65 6e 74 69 6e 67 20 74 68 69 73 20 71 75 65 menting this que
1b1c0 72 79 20 69 73 20 74 6f 20 65 78 65 63 75 74 65 ry is to execute
1b1d0 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72 79 the.** subquery
1b1e0 20 66 69 72 73 74 20 61 6e 64 20 73 74 6f 72 65 first and store
1b1f0 20 74 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 the results in
1b200 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c a temporary tabl
1b210 65 2c 20 74 68 65 6e 0a 2a 2a 20 72 75 6e 20 74 e, then.** run t
1b220 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 6f he outer query o
1b230 6e 20 74 68 61 74 20 74 65 6d 70 6f 72 61 72 79 n that temporary
1b240 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 65 table. This re
1b250 71 75 69 72 65 73 20 74 77 6f 0a 2a 2a 20 70 61 quires two.** pa
1b260 73 73 65 73 20 6f 76 65 72 20 74 68 65 20 64 61 sses over the da
1b270 74 61 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 ta. Furthermore
1b280 2c 20 62 65 63 61 75 73 65 20 74 68 65 20 74 65 , because the te
1b290 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a 2a 2a mporary table.**
1b2a0 20 68 61 73 20 6e 6f 20 69 6e 64 69 63 65 73 2c has no indices,
1b2b0 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 the WHERE claus
1b2c0 65 20 6f 6e 20 74 68 65 20 6f 75 74 65 72 20 71 e on the outer q
1b2d0 75 65 72 79 20 63 61 6e 6e 6f 74 20 62 65 0a 2a uery cannot be.*
1b2e0 2a 20 6f 70 74 69 6d 69 7a 65 64 2e 0a 2a 2a 0a * optimized..**.
1b2f0 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 ** This routine
1b300 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 77 72 attempts to rewr
1b310 69 74 65 20 71 75 65 72 69 65 73 20 73 75 63 68 ite queries such
1b320 20 61 73 20 74 68 65 20 61 62 6f 76 65 20 69 6e as the above in
1b330 74 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 66 to.** a single f
1b340 6c 61 74 20 73 65 6c 65 63 74 2c 20 6c 69 6b 65 lat select, like
1b350 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 this:.**.**
1b360 20 53 45 4c 45 43 54 20 78 2b 79 20 41 53 20 61 SELECT x+y AS a
1b370 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 7a FROM t1 WHERE z
1b380 3c 31 30 30 20 41 4e 44 20 61 3e 35 0a 2a 2a 0a <100 AND a>5.**.
1b390 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 ** The code gene
1b3a0 72 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 73 rated for this s
1b3b0 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 20 67 69 implification gi
1b3c0 76 65 73 20 74 68 65 20 73 61 6d 65 20 72 65 73 ves the same res
1b3d0 75 6c 74 0a 2a 2a 20 62 75 74 20 6f 6e 6c 79 20 ult.** but only
1b3e0 68 61 73 20 74 6f 20 73 63 61 6e 20 74 68 65 20 has to scan the
1b3f0 64 61 74 61 20 6f 6e 63 65 2e 20 20 41 6e 64 20 data once. And
1b400 62 65 63 61 75 73 65 20 69 6e 64 69 63 65 73 20 because indices
1b410 6d 69 67 68 74 20 0a 2a 2a 20 65 78 69 73 74 20 might .** exist
1b420 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 31 2c on the table t1,
1b430 20 61 20 63 6f 6d 70 6c 65 74 65 20 73 63 61 6e a complete scan
1b440 20 6f 66 20 74 68 65 20 64 61 74 61 20 6d 69 67 of the data mig
1b450 68 74 20 62 65 0a 2a 2a 20 61 76 6f 69 64 65 64 ht be.** avoided
1b460 2e 0a 2a 2a 0a 2a 2a 20 46 6c 61 74 74 65 6e 69 ..**.** Flatteni
1b470 6e 67 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d ng is only attem
1b480 70 74 65 64 20 69 66 20 61 6c 6c 20 6f 66 20 74 pted if all of t
1b490 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
1b4a0 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 true:.**.** (
1b4b0 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72 79 1) The subquery
1b4c0 20 61 6e 64 20 74 68 65 20 6f 75 74 65 72 20 71 and the outer q
1b4d0 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68 uery do not both
1b4e0 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 2e use aggregates.
1b4f0 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 20 54 68 .**.** (2) Th
1b500 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f e subquery is no
1b510 74 20 61 6e 20 61 67 67 72 65 67 61 74 65 20 6f t an aggregate o
1b520 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 r the outer quer
1b530 79 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e y is not a join.
1b540 0a 2a 2a 0a 2a 2a 20 20 20 28 33 29 20 20 54 68 .**.** (3) Th
1b550 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f e subquery is no
1b560 74 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 t the right oper
1b570 61 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 and of a left ou
1b580 74 65 72 20 6a 6f 69 6e 0a 2a 2a 20 20 20 20 20 ter join.**
1b590 20 20 20 28 4f 72 69 67 69 6e 61 6c 6c 79 20 74 (Originally t
1b5a0 69 63 6b 65 74 20 23 33 30 36 2e 20 20 53 74 72 icket #306. Str
1b5b0 65 6e 67 74 68 65 6e 65 64 20 62 79 20 74 69 63 engthened by tic
1b5c0 6b 65 74 20 23 33 33 30 30 29 0a 2a 2a 0a 2a 2a ket #3300).**.**
1b5d0 20 20 20 28 34 29 20 20 54 68 65 20 73 75 62 71 (4) The subq
1b5e0 75 65 72 79 20 69 73 20 6e 6f 74 20 44 49 53 54 uery is not DIST
1b5f0 49 4e 43 54 2e 0a 2a 2a 0a 2a 2a 20 20 28 2a 2a INCT..**.** (**
1b600 29 20 20 41 74 20 6f 6e 65 20 70 6f 69 6e 74 20 ) At one point
1b610 72 65 73 74 72 69 63 74 69 6f 6e 73 20 28 34 29 restrictions (4)
1b620 20 61 6e 64 20 28 35 29 20 64 65 66 69 6e 65 64 and (5) defined
1b630 20 61 20 73 75 62 73 65 74 20 6f 66 20 44 49 53 a subset of DIS
1b640 54 49 4e 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 TINCT.**
1b650 73 75 62 2d 71 75 65 72 69 65 73 20 74 68 61 74 sub-queries that
1b660 20 77 65 72 65 20 65 78 63 6c 75 64 65 64 20 66 were excluded f
1b670 72 6f 6d 20 74 68 69 73 20 6f 70 74 69 6d 69 7a rom this optimiz
1b680 61 74 69 6f 6e 2e 20 52 65 73 74 72 69 63 74 69 ation. Restricti
1b690 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20 28 34 on .** (4
1b6a0 29 20 68 61 73 20 73 69 6e 63 65 20 62 65 65 6e ) has since been
1b6b0 20 65 78 70 61 6e 64 65 64 20 74 6f 20 65 78 63 expanded to exc
1b6c0 6c 75 64 65 20 61 6c 6c 20 44 49 53 54 49 4e 43 lude all DISTINC
1b6d0 54 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a T subqueries..**
1b6e0 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65 20 73 .** (6) The s
1b6f0 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 ubquery does not
1b700 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 20 use aggregates
1b710 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 or the outer que
1b720 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 ry is not.**
1b730 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a 2a 2a DISTINCT..**
1b740 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65 20 73 .** (7) The s
1b750 75 62 71 75 65 72 79 20 68 61 73 20 61 20 46 52 ubquery has a FR
1b760 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 4f 44 4f OM clause. TODO
1b770 3a 20 20 46 6f 72 20 73 75 62 71 75 65 72 69 65 : For subquerie
1b780 73 20 77 69 74 68 6f 75 74 0a 2a 2a 20 20 20 20 s without.**
1b790 20 20 20 20 41 20 46 52 4f 4d 20 63 6c 61 75 73 A FROM claus
1b7a0 65 2c 20 63 6f 6e 73 69 64 65 72 20 61 64 64 69 e, consider addi
1b7b0 6e 67 20 61 20 46 52 4f 4d 20 63 6c 6f 73 65 20 ng a FROM close
1b7c0 77 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c with the special
1b7d0 0a 2a 2a 20 20 20 20 20 20 20 20 74 61 62 6c 65 .** table
1b7e0 20 73 71 6c 69 74 65 5f 6f 6e 63 65 20 74 68 61 sqlite_once tha
1b7f0 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 t consists of a
1b800 73 69 6e 67 6c 65 20 72 6f 77 20 63 6f 6e 74 61 single row conta
1b810 69 6e 69 6e 67 20 61 0a 2a 2a 20 20 20 20 20 20 ining a.**
1b820 20 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 2e 0a 2a single NULL..*
1b830 2a 0a 2a 2a 20 20 20 28 38 29 20 20 54 68 65 20 *.** (8) The
1b840 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f subquery does no
1b850 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 t use LIMIT or t
1b860 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 he outer query i
1b870 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a s not a join..**
1b880 0a 2a 2a 20 20 20 28 39 29 20 20 54 68 65 20 73 .** (9) The s
1b890 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 ubquery does not
1b8a0 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 use LIMIT or th
1b8b0 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f e outer query do
1b8c0 65 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 es not use.**
1b8d0 20 20 20 20 20 61 67 67 72 65 67 61 74 65 73 2e aggregates.
1b8e0 0a 2a 2a 0a 2a 2a 20 20 28 2a 2a 29 20 20 52 65 .**.** (**) Re
1b8f0 73 74 72 69 63 74 69 6f 6e 20 28 31 30 29 20 77 striction (10) w
1b900 61 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 as removed from
1b910 74 68 65 20 63 6f 64 65 20 6f 6e 20 32 30 30 35 the code on 2005
1b920 2d 30 32 2d 30 35 20 62 75 74 20 77 65 0a 2a 2a -02-05 but we.**
1b930 20 20 20 20 20 20 20 20 61 63 63 69 64 65 6e 74 accident
1b940 6c 79 20 63 61 72 72 69 65 64 20 74 68 65 20 63 ly carried the c
1b950 6f 6d 6d 65 6e 74 20 66 6f 72 77 61 72 64 20 75 omment forward u
1b960 6e 74 69 6c 20 32 30 31 34 2d 30 39 2d 31 35 2e ntil 2014-09-15.
1b970 20 20 4f 72 69 67 69 6e 61 6c 0a 2a 2a 20 20 20 Original.**
1b980 20 20 20 20 20 74 65 78 74 3a 20 22 54 68 65 20 text: "The
1b990 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f subquery does no
1b9a0 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 t use aggregates
1b9b0 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 or the outer qu
1b9c0 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 ery does not.**
1b9d0 20 20 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54 use LIMIT
1b9e0 2e 22 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 .".**.** (11)
1b9f0 54 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 The subquery and
1ba00 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 the outer query
1ba10 20 64 6f 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 do not both hav
1ba20 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 e ORDER BY claus
1ba30 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 2a 2a 29 20 es..**.** (**)
1ba40 20 4e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 Not implemented
1ba50 2e 20 20 53 75 62 73 75 6d 65 64 20 69 6e 74 6f . Subsumed into
1ba60 20 72 65 73 74 72 69 63 74 69 6f 6e 20 28 33 29 restriction (3)
1ba70 2e 20 20 57 61 73 20 70 72 65 76 69 6f 75 73 6c . Was previousl
1ba80 79 0a 2a 2a 20 20 20 20 20 20 20 20 61 20 73 65 y.** a se
1ba90 70 61 72 61 74 65 20 72 65 73 74 72 69 63 74 69 parate restricti
1baa0 6f 6e 20 64 65 72 69 76 69 6e 67 20 66 72 6f 6d on deriving from
1bab0 20 74 69 63 6b 65 74 20 23 33 35 30 2e 0a 2a 2a ticket #350..**
1bac0 0a 2a 2a 20 20 28 31 33 29 20 20 54 68 65 20 73 .** (13) The s
1bad0 75 62 71 75 65 72 79 20 61 6e 64 20 6f 75 74 65 ubquery and oute
1bae0 72 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 r query do not b
1baf0 6f 74 68 20 75 73 65 20 4c 49 4d 49 54 2e 0a 2a oth use LIMIT..*
1bb00 2a 0a 2a 2a 20 20 28 31 34 29 20 20 54 68 65 20 *.** (14) The
1bb10 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f subquery does no
1bb20 74 20 75 73 65 20 4f 46 46 53 45 54 2e 0a 2a 2a t use OFFSET..**
1bb30 0a 2a 2a 20 20 28 31 35 29 20 20 54 68 65 20 6f .** (15) The o
1bb40 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f uter query is no
1bb50 74 20 70 61 72 74 20 6f 66 20 61 20 63 6f 6d 70 t part of a comp
1bb60 6f 75 6e 64 20 73 65 6c 65 63 74 20 6f 72 20 74 ound select or t
1bb70 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 73 75 62 he.** sub
1bb80 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 68 query does not h
1bb90 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 ave a LIMIT clau
1bba0 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 28 53 se..** (S
1bbb0 65 65 20 74 69 63 6b 65 74 20 23 32 33 33 39 20 ee ticket #2339
1bbc0 61 6e 64 20 74 69 63 6b 65 74 20 5b 30 32 61 38 and ticket [02a8
1bbd0 65 38 31 64 34 34 5d 29 2e 0a 2a 2a 0a 2a 2a 20 e81d44])..**.**
1bbe0 20 28 31 36 29 20 20 54 68 65 20 6f 75 74 65 72 (16) The outer
1bbf0 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61 6e query is not an
1bc00 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 74 68 aggregate or th
1bc10 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 0a e subquery does.
1bc20 2a 2a 20 20 20 20 20 20 20 20 6e 6f 74 20 63 6f ** not co
1bc30 6e 74 61 69 6e 20 4f 52 44 45 52 20 42 59 2e 20 ntain ORDER BY.
1bc40 20 28 54 69 63 6b 65 74 20 23 32 39 34 32 29 20 (Ticket #2942)
1bc50 20 54 68 69 73 20 75 73 65 64 20 74 6f 20 6e 6f This used to no
1bc60 74 20 6d 61 74 74 65 72 0a 2a 2a 20 20 20 20 20 t matter.**
1bc70 20 20 20 75 6e 74 69 6c 20 77 65 20 69 6e 74 72 until we intr
1bc80 6f 64 75 63 65 64 20 74 68 65 20 67 72 6f 75 70 oduced the group
1bc90 5f 63 6f 6e 63 61 74 28 29 20 66 75 6e 63 74 69 _concat() functi
1bca0 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 20 28 31 37 on. .**.** (17
1bcb0 29 20 20 54 68 65 20 73 75 62 2d 71 75 65 72 79 ) The sub-query
1bcc0 20 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f 75 is not a compou
1bcd0 6e 64 20 73 65 6c 65 63 74 2c 20 6f 72 20 69 74 nd select, or it
1bce0 20 69 73 20 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 is a UNION ALL
1bcf0 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6d 70 6f .** compo
1bd00 75 6e 64 20 63 6c 61 75 73 65 20 6d 61 64 65 20 und clause made
1bd10 75 70 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 6e up entirely of n
1bd20 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 on-aggregate que
1bd30 72 69 65 73 2c 20 61 6e 64 20 0a 2a 2a 20 20 20 ries, and .**
1bd40 20 20 20 20 20 74 68 65 20 70 61 72 65 6e 74 20 the parent
1bd50 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 query:.**.**
1bd60 20 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 69 * is not i
1bd70 74 73 65 6c 66 20 70 61 72 74 20 6f 66 20 61 20 tself part of a
1bd80 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c compound select,
1bd90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69 .** * i
1bda0 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 s not an aggrega
1bdb0 74 65 20 6f 72 20 44 49 53 54 49 4e 43 54 20 71 te or DISTINCT q
1bdc0 75 65 72 79 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 uery, and.**
1bdd0 20 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 61 * is not a
1bde0 20 6a 6f 69 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 join.**.**
1bdf0 20 20 20 54 68 65 20 70 61 72 65 6e 74 20 61 6e The parent an
1be00 64 20 73 75 62 2d 71 75 65 72 79 20 6d 61 79 20 d sub-query may
1be10 63 6f 6e 74 61 69 6e 20 57 48 45 52 45 20 63 6c contain WHERE cl
1be20 61 75 73 65 73 2e 20 53 75 62 6a 65 63 74 20 74 auses. Subject t
1be30 6f 0a 2a 2a 20 20 20 20 20 20 20 20 72 75 6c 65 o.** rule
1be40 73 20 28 31 31 29 2c 20 28 31 33 29 20 61 6e 64 s (11), (13) and
1be50 20 28 31 34 29 2c 20 74 68 65 79 20 6d 61 79 20 (14), they may
1be60 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20 4f 52 44 also contain ORD
1be70 45 52 20 42 59 2c 0a 2a 2a 20 20 20 20 20 20 20 ER BY,.**
1be80 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 LIMIT and OFFSE
1be90 54 20 63 6c 61 75 73 65 73 2e 20 20 54 68 65 20 T clauses. The
1bea0 73 75 62 71 75 65 72 79 20 63 61 6e 6e 6f 74 20 subquery cannot
1beb0 75 73 65 20 61 6e 79 20 63 6f 6d 70 6f 75 6e 64 use any compound
1bec0 0a 2a 2a 20 20 20 20 20 20 20 20 6f 70 65 72 61 .** opera
1bed0 74 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 55 tor other than U
1bee0 4e 49 4f 4e 20 41 4c 4c 20 62 65 63 61 75 73 65 NION ALL because
1bef0 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 63 all the other c
1bf00 6f 6d 70 6f 75 6e 64 0a 2a 2a 20 20 20 20 20 20 ompound.**
1bf10 20 20 6f 70 65 72 61 74 6f 72 73 20 68 61 76 65 operators have
1bf20 20 61 6e 20 69 6d 70 6c 69 65 64 20 44 49 53 54 an implied DIST
1bf30 49 4e 43 54 20 77 68 69 63 68 20 69 73 20 64 69 INCT which is di
1bf40 73 61 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a 20 20 sallowed by.**
1bf50 20 20 20 20 20 20 72 65 73 74 72 69 63 74 69 6f restrictio
1bf60 6e 20 28 34 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 n (4)..**.**
1bf70 20 20 20 20 41 6c 73 6f 2c 20 65 61 63 68 20 63 Also, each c
1bf80 6f 6d 70 6f 6e 65 6e 74 20 6f 66 20 74 68 65 20 omponent of the
1bf90 73 75 62 2d 71 75 65 72 79 20 6d 75 73 74 20 72 sub-query must r
1bfa0 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 6e eturn the same n
1bfb0 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 umber.**
1bfc0 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e of result column
1bfd0 73 2e 20 54 68 69 73 20 69 73 20 61 63 74 75 61 s. This is actua
1bfe0 6c 6c 79 20 61 20 72 65 71 75 69 72 65 6d 65 6e lly a requiremen
1bff0 74 20 66 6f 72 20 61 6e 79 20 63 6f 6d 70 6f 75 t for any compou
1c000 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 53 45 4c nd.** SEL
1c010 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62 ECT statement, b
1c020 75 74 20 61 6c 6c 20 74 68 65 20 63 6f 64 65 20 ut all the code
1c030 68 65 72 65 20 64 6f 65 73 20 69 73 20 6d 61 6b here does is mak
1c040 65 20 73 75 72 65 20 74 68 61 74 20 6e 6f 0a 2a e sure that no.*
1c050 2a 20 20 20 20 20 20 20 20 73 75 63 68 20 28 69 * such (i
1c060 6c 6c 65 67 61 6c 29 20 73 75 62 2d 71 75 65 72 llegal) sub-quer
1c070 79 20 69 73 20 66 6c 61 74 74 65 6e 65 64 2e 20 y is flattened.
1c080 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 The caller will
1c090 64 65 74 65 63 74 20 74 68 65 0a 2a 2a 20 20 20 detect the.**
1c0a0 20 20 20 20 20 73 79 6e 74 61 78 20 65 72 72 6f syntax erro
1c0b0 72 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 64 r and return a d
1c0c0 65 74 61 69 6c 65 64 20 6d 65 73 73 61 67 65 2e etailed message.
1c0d0 0a 2a 2a 0a 2a 2a 20 20 28 31 38 29 20 20 49 66 .**.** (18) If
1c0e0 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 the sub-query i
1c0f0 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c s a compound sel
1c100 65 63 74 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 ect, then all te
1c110 72 6d 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 rms of the.**
1c120 20 20 20 20 20 4f 52 44 45 52 20 62 79 20 63 6c ORDER by cl
1c130 61 75 73 65 20 6f 66 20 74 68 65 20 70 61 72 65 ause of the pare
1c140 6e 74 20 6d 75 73 74 20 62 65 20 73 69 6d 70 6c nt must be simpl
1c150 65 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 e references to
1c160 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6c 75 6d .** colum
1c170 6e 73 20 6f 66 20 74 68 65 20 73 75 62 2d 71 75 ns of the sub-qu
1c180 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 39 29 ery..**.** (19)
1c190 20 20 54 68 65 20 73 75 62 71 75 65 72 79 20 64 The subquery d
1c1a0 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c 49 4d 49 oes not use LIMI
1c1b0 54 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 T or the outer q
1c1c0 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a uery does not.**
1c1d0 20 20 20 20 20 20 20 20 68 61 76 65 20 61 20 57 have a W
1c1e0 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a HERE clause..**.
1c1f0 2a 2a 20 20 28 32 30 29 20 20 49 66 20 74 68 65 ** (20) If the
1c200 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 sub-query is a
1c210 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c compound select,
1c220 20 74 68 65 6e 20 69 74 20 6d 75 73 74 20 6e 6f then it must no
1c230 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 t use.**
1c240 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 an ORDER BY clau
1c250 73 65 2e 20 20 54 69 63 6b 65 74 20 23 33 37 37 se. Ticket #377
1c260 33 2e 20 20 57 65 20 63 6f 75 6c 64 20 72 65 6c 3. We could rel
1c270 61 78 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 ax this constrai
1c280 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 73 6f 6d nt.** som
1c290 65 77 68 61 74 20 62 79 20 73 61 79 69 6e 67 20 ewhat by saying
1c2a0 74 68 61 74 20 74 68 65 20 74 65 72 6d 73 20 6f that the terms o
1c2b0 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 f the ORDER BY c
1c2c0 6c 61 75 73 65 20 6d 75 73 74 0a 2a 2a 20 20 20 lause must.**
1c2d0 20 20 20 20 20 61 70 70 65 61 72 20 61 73 20 75 appear as u
1c2e0 6e 6d 6f 64 69 66 69 65 64 20 72 65 73 75 6c 74 nmodified result
1c2f0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 columns in the
1c300 6f 75 74 65 72 20 71 75 65 72 79 2e 20 20 42 75 outer query. Bu
1c310 74 20 77 65 0a 2a 2a 20 20 20 20 20 20 20 20 68 t we.** h
1c320 61 76 65 20 6f 74 68 65 72 20 6f 70 74 69 6d 69 ave other optimi
1c330 7a 61 74 69 6f 6e 73 20 69 6e 20 6d 69 6e 64 20 zations in mind
1c340 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74 68 61 to deal with tha
1c350 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 28 t case..**.** (
1c360 32 31 29 20 20 54 68 65 20 73 75 62 71 75 65 72 21) The subquer
1c370 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65 20 4c y does not use L
1c380 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75 74 65 IMIT or the oute
1c390 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 0a 2a r query is not.*
1c3a0 2a 20 20 20 20 20 20 20 20 44 49 53 54 49 4e 43 * DISTINC
1c3b0 54 2e 20 20 28 53 65 65 20 74 69 63 6b 65 74 20 T. (See ticket
1c3c0 5b 37 35 32 65 31 36 34 36 66 63 5d 29 2e 0a 2a [752e1646fc])..*
1c3d0 2a 0a 2a 2a 20 20 28 32 32 29 20 20 54 68 65 20 *.** (22) The
1c3e0 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 subquery is not
1c3f0 61 20 72 65 63 75 72 73 69 76 65 20 43 54 45 2e a recursive CTE.
1c400 0a 2a 2a 0a 2a 2a 20 20 28 32 33 29 20 20 54 68 .**.** (23) Th
1c410 65 20 70 61 72 65 6e 74 20 69 73 20 6e 6f 74 20 e parent is not
1c420 61 20 72 65 63 75 72 73 69 76 65 20 43 54 45 2c a recursive CTE,
1c430 20 6f 72 20 74 68 65 20 73 75 62 2d 71 75 65 72 or the sub-quer
1c440 79 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 20 20 y is not a.**
1c450 20 20 20 20 20 63 6f 6d 70 6f 75 6e 64 20 71 75 compound qu
1c460 65 72 79 2e 20 54 68 69 73 20 72 65 73 74 72 69 ery. This restri
1c470 63 74 69 6f 6e 20 69 73 20 62 65 63 61 75 73 65 ction is because
1c480 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 transforming th
1c490 65 0a 2a 2a 20 20 20 20 20 20 20 20 70 61 72 65 e.** pare
1c4a0 6e 74 20 74 6f 20 61 20 63 6f 6d 70 6f 75 6e 64 nt to a compound
1c4b0 20 71 75 65 72 79 20 63 6f 6e 66 75 73 65 73 20 query confuses
1c4c0 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 68 61 the code that ha
1c4d0 6e 64 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 ndles.**
1c4e0 72 65 63 75 72 73 69 76 65 20 71 75 65 72 69 65 recursive querie
1c4f0 73 20 69 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 s in multiSelect
1c500 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 28 32 34 29 20 ()..**.** (24)
1c510 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73 The subquery is
1c520 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74 not an aggregat
1c530 65 20 74 68 61 74 20 75 73 65 73 20 74 68 65 20 e that uses the
1c540 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 29 20 6f built-in min() o
1c550 72 20 0a 2a 2a 20 20 20 20 20 20 20 20 6f 72 20 r .** or
1c560 6d 61 78 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e max() functions.
1c570 20 20 28 57 69 74 68 6f 75 74 20 74 68 69 73 20 (Without this
1c580 72 65 73 74 72 69 63 74 69 6f 6e 2c 20 61 20 71 restriction, a q
1c590 75 65 72 79 20 6c 69 6b 65 3a 0a 2a 2a 20 20 20 uery like:.**
1c5a0 20 20 20 20 20 22 53 45 4c 45 43 54 20 78 20 46 "SELECT x F
1c5b0 52 4f 4d 20 28 53 45 4c 45 43 54 20 6d 61 78 28 ROM (SELECT max(
1c5c0 79 29 2c 20 78 20 46 52 4f 4d 20 74 31 29 22 20 y), x FROM t1)"
1c5d0 77 6f 75 6c 64 20 6e 6f 74 20 6e 65 63 65 73 73 would not necess
1c5e0 61 72 69 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 arily.**
1c5f0 72 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 return the value
1c600 20 58 20 66 6f 72 20 77 68 69 63 68 20 59 20 77 X for which Y w
1c610 61 73 20 6d 61 78 69 6d 61 6c 2e 29 0a 2a 2a 0a as maximal.).**.
1c620 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f **.** In this ro
1c630 75 74 69 6e 65 2c 20 74 68 65 20 22 70 22 20 70 utine, the "p" p
1c640 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f arameter is a po
1c650 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 75 74 inter to the out
1c660 65 72 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 er query..** The
1c670 20 73 75 62 71 75 65 72 79 20 69 73 20 70 2d 3e subquery is p->
1c680 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 20 pSrc->a[iFrom].
1c690 20 69 73 41 67 67 20 69 73 20 74 72 75 65 20 69 isAgg is true i
1c6a0 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 f the outer quer
1c6b0 79 0a 2a 2a 20 75 73 65 73 20 61 67 67 72 65 67 y.** uses aggreg
1c6c0 61 74 65 73 20 61 6e 64 20 73 75 62 71 75 65 72 ates and subquer
1c6d0 79 49 73 41 67 67 20 69 73 20 74 72 75 65 20 69 yIsAgg is true i
1c6e0 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75 f the subquery u
1c6f0 73 65 73 20 61 67 67 72 65 67 61 74 65 73 2e 0a ses aggregates..
1c700 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e **.** If flatten
1c710 69 6e 67 20 69 73 20 6e 6f 74 20 61 74 74 65 6d ing is not attem
1c720 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69 pted, this routi
1c730 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e ne is a no-op an
1c740 64 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 20 d returns 0..**
1c750 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 If flattening is
1c760 20 61 74 74 65 6d 70 74 65 64 20 74 68 69 73 20 attempted this
1c770 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 routine returns
1c780 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 1..**.** All of
1c790 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 the expression a
1c7a0 6e 61 6c 79 73 69 73 20 6d 75 73 74 20 6f 63 63 nalysis must occ
1c7b0 75 72 20 6f 6e 20 62 6f 74 68 20 74 68 65 20 6f ur on both the o
1c7c0 75 74 65 72 20 71 75 65 72 79 20 61 6e 64 0a 2a uter query and.*
1c7d0 2a 20 74 68 65 20 73 75 62 71 75 65 72 79 20 62 * the subquery b
1c7e0 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 efore this routi
1c7f0 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a 73 74 61 74 ne runs..*/.stat
1c800 69 63 20 69 6e 74 20 66 6c 61 74 74 65 6e 53 75 ic int flattenSu
1c810 62 71 75 65 72 79 28 0a 20 20 50 61 72 73 65 20 bquery(. Parse
1c820 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f *pParse, /
1c830 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 * Parsing contex
1c840 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 t */. Select *p
1c850 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 , /* T
1c860 68 65 20 70 61 72 65 6e 74 20 6f 72 20 6f 75 74 he parent or out
1c870 65 72 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d er SELECT statem
1c880 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46 72 ent */. int iFr
1c890 6f 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a om, /*
1c8a0 20 49 6e 64 65 78 20 69 6e 20 70 2d 3e 70 53 72 Index in p->pSr
1c8b0 63 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 69 6e c->a[] of the in
1c8c0 6e 65 72 20 73 75 62 71 75 65 72 79 20 2a 2f 0a ner subquery */.
1c8d0 20 20 69 6e 74 20 69 73 41 67 67 2c 20 20 20 20 int isAgg,
1c8e0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 /* True i
1c8f0 66 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 75 f outer SELECT u
1c900 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75 ses aggregate fu
1c910 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 nctions */. int
1c920 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 20 subqueryIsAgg
1c930 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 /* True if the
1c940 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20 61 subquery uses a
1c950 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f ggregate functio
1c960 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 ns */.){. const
1c970 20 63 68 61 72 20 2a 7a 53 61 76 65 64 41 75 74 char *zSavedAut
1c980 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 hContext = pPars
1c990 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b e->zAuthContext;
1c9a0 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 61 72 65 . Select *pPare
1c9b0 6e 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 nt;. Select *pS
1c9c0 75 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 ub; /* The
1c9d0 20 69 6e 6e 65 72 20 71 75 65 72 79 20 6f 72 20 inner query or
1c9e0 22 73 75 62 71 75 65 72 79 22 20 2a 2f 0a 20 20 "subquery" */.
1c9f0 53 65 6c 65 63 74 20 2a 70 53 75 62 31 3b 20 20 Select *pSub1;
1ca00 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 /* Pointer t
1ca10 6f 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74 20 o the rightmost
1ca20 73 65 6c 65 63 74 20 69 6e 20 73 75 62 2d 71 75 select in sub-qu
1ca30 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 ery */. SrcList
1ca40 20 2a 70 53 72 63 3b 20 20 20 20 20 20 2f 2a 20 *pSrc; /*
1ca50 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 The FROM clause
1ca60 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 of the outer que
1ca70 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 ry */. SrcList
1ca80 2a 70 53 75 62 53 72 63 3b 20 20 20 2f 2a 20 54 *pSubSrc; /* T
1ca90 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f he FROM clause o
1caa0 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 2a f the subquery *
1cab0 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c /. ExprList *pL
1cac0 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 72 ist; /* The r
1cad0 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 esult set of the
1cae0 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a outer query */.
1caf0 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20 20 int iParent;
1cb00 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 63 75 /* VDBE cu
1cb10 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 rsor number of t
1cb20 68 65 20 70 53 75 62 20 72 65 73 75 6c 74 20 73 he pSub result s
1cb30 65 74 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f et temp table */
1cb40 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 . int i;
1cb50 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 /* Loop c
1cb60 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 ounter */. Expr
1cb70 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 *pWhere;
1cb80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
1cb90 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 The WHERE clause
1cba0 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 */. struct Src
1cbb0 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 75 62 69 List_item *pSubi
1cbc0 74 65 6d 3b 20 20 20 2f 2a 20 54 68 65 20 73 75 tem; /* The su
1cbd0 62 71 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69 bquery */. sqli
1cbe0 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 te3 *db = pParse
1cbf0 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 ->db;.. /* Chec
1cc00 6b 20 74 6f 20 73 65 65 20 69 66 20 66 6c 61 74 k to see if flat
1cc10 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69 74 tening is permit
1cc20 74 65 64 2e 20 20 52 65 74 75 72 6e 20 30 20 69 ted. Return 0 i
1cc30 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 f not.. */. as
1cc40 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 sert( p!=0 );.
1cc50 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 69 6f assert( p->pPrio
1cc60 72 3d 3d 30 20 29 3b 20 20 2f 2a 20 55 6e 61 62 r==0 ); /* Unab
1cc70 6c 65 20 74 6f 20 66 6c 61 74 74 65 6e 20 63 6f le to flatten co
1cc80 6d 70 6f 75 6e 64 20 71 75 65 72 69 65 73 20 2a mpound queries *
1cc90 2f 0a 20 20 69 66 28 20 4f 70 74 69 6d 69 7a 61 /. if( Optimiza
1cca0 74 69 6f 6e 44 69 73 61 62 6c 65 64 28 64 62 2c tionDisabled(db,
1ccb0 20 53 51 4c 49 54 45 5f 51 75 65 72 79 46 6c 61 SQLITE_QueryFla
1ccc0 74 74 65 6e 65 72 29 20 29 20 72 65 74 75 72 6e ttener) ) return
1ccd0 20 30 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 0;. pSrc = p->
1cce0 70 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 pSrc;. assert(
1ccf0 70 53 72 63 20 26 26 20 69 46 72 6f 6d 3e 3d 30 pSrc && iFrom>=0
1cd00 20 26 26 20 69 46 72 6f 6d 3c 70 53 72 63 2d 3e && iFrom<pSrc->
1cd10 6e 53 72 63 20 29 3b 0a 20 20 70 53 75 62 69 74 nSrc );. pSubit
1cd20 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 46 em = &pSrc->a[iF
1cd30 72 6f 6d 5d 3b 0a 20 20 69 50 61 72 65 6e 74 20 rom];. iParent
1cd40 3d 20 70 53 75 62 69 74 65 6d 2d 3e 69 43 75 72 = pSubitem->iCur
1cd50 73 6f 72 3b 0a 20 20 70 53 75 62 20 3d 20 70 53 sor;. pSub = pS
1cd60 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b ubitem->pSelect;
1cd70 0a 20 20 61 73 73 65 72 74 28 20 70 53 75 62 21 . assert( pSub!
1cd80 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73 41 67 =0 );. if( isAg
1cd90 67 20 26 26 20 73 75 62 71 75 65 72 79 49 73 41 g && subqueryIsA
1cda0 67 67 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 gg ) return 0;
1cdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
1cdc0 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 * Restriction (1
1cdd0 29 20 20 2a 2f 0a 20 20 69 66 28 20 73 75 62 71 ) */. if( subq
1cde0 75 65 72 79 49 73 41 67 67 20 26 26 20 70 53 72 ueryIsAgg && pSr
1cdf0 63 2d 3e 6e 53 72 63 3e 31 20 29 20 72 65 74 75 c->nSrc>1 ) retu
1ce00 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f rn 0; /
1ce10 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 32 * Restriction (2
1ce20 29 20 20 2a 2f 0a 20 20 70 53 75 62 53 72 63 20 ) */. pSubSrc
1ce30 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 0a 20 20 = pSub->pSrc;.
1ce40 61 73 73 65 72 74 28 20 70 53 75 62 53 72 63 20 assert( pSubSrc
1ce50 29 3b 0a 20 20 2f 2a 20 50 72 69 6f 72 20 74 6f );. /* Prior to
1ce60 20 76 65 72 73 69 6f 6e 20 33 2e 31 2e 32 2c 20 version 3.1.2,
1ce70 77 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f when LIMIT and O
1ce80 46 46 53 45 54 20 68 61 64 20 74 6f 20 62 65 20 FFSET had to be
1ce90 73 69 6d 70 6c 65 20 63 6f 6e 73 74 61 6e 74 73 simple constants
1cea0 2c 0a 20 20 2a 2a 20 6e 6f 74 20 61 72 62 69 74 ,. ** not arbit
1ceb0 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 73 rary expressions
1cec0 2c 20 77 65 20 61 6c 6c 6f 77 65 64 20 73 6f 6d , we allowed som
1ced0 65 20 63 6f 6d 62 69 6e 69 6e 67 20 6f 66 20 4c e combining of L
1cee0 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a IMIT and OFFSET.
1cef0 20 20 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65 ** because the
1cf00 79 20 63 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 y could be compu
1cf10 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 ted at compile-t
1cf20 69 6d 65 2e 20 20 42 75 74 20 77 68 65 6e 20 4c ime. But when L
1cf30 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a IMIT and OFFSET.
1cf40 20 20 2a 2a 20 62 65 63 61 6d 65 20 61 72 62 69 ** became arbi
1cf50 74 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e trary expression
1cf60 73 2c 20 77 65 20 77 65 72 65 20 66 6f 72 63 65 s, we were force
1cf70 64 20 74 6f 20 61 64 64 20 72 65 73 74 72 69 63 d to add restric
1cf80 74 69 6f 6e 73 20 28 31 33 29 0a 20 20 2a 2a 20 tions (13). **
1cf90 61 6e 64 20 28 31 34 29 2e 20 2a 2f 0a 20 20 69 and (14). */. i
1cfa0 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 f( pSub->pLimit
1cfb0 26 26 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 && p->pLimit ) r
1cfc0 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
1cfd0 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 /* Restric
1cfe0 74 69 6f 6e 20 28 31 33 29 20 2a 2f 0a 20 20 69 tion (13) */. i
1cff0 66 28 20 70 53 75 62 2d 3e 70 4f 66 66 73 65 74 f( pSub->pOffset
1d000 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 ) return 0;
1d010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d020 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 /* Restric
1d030 74 69 6f 6e 20 28 31 34 29 20 2a 2f 0a 20 20 69 tion (14) */. i
1d040 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 f( (p->selFlags
1d050 26 20 53 46 5f 43 6f 6d 70 6f 75 6e 64 29 21 3d & SF_Compound)!=
1d060 30 20 26 26 20 70 53 75 62 2d 3e 70 4c 69 6d 69 0 && pSub->pLimi
1d070 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 t ){. return
1d080 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0;
1d090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d0b0 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 35 Restriction (15
1d0c0 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 ) */. }. if( p
1d0d0 53 75 62 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20 SubSrc->nSrc==0
1d0e0 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 ) return 0;
1d0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d100 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e /* Restriction
1d110 20 28 37 29 20 20 2a 2f 0a 20 20 69 66 28 20 70 (7) */. if( p
1d120 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 Sub->selFlags &
1d130 53 46 5f 44 69 73 74 69 6e 63 74 20 29 20 72 65 SF_Distinct ) re
1d140 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 turn 0;
1d150 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e /* Restriction
1d160 20 28 35 29 20 20 2a 2f 0a 20 20 69 66 28 20 70 (5) */. if( p
1d170 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20 28 Sub->pLimit && (
1d180 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 7c 7c 20 pSrc->nSrc>1 ||
1d190 69 73 41 67 67 29 20 29 7b 0a 20 20 20 20 20 72 isAgg) ){. r
1d1a0 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 eturn 0;
1d1b0 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 73 /* Restrictions
1d1c0 20 28 38 29 28 39 29 20 2a 2f 0a 20 20 7d 0a 20 (8)(9) */. }.
1d1d0 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 if( (p->selFlag
1d1e0 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 s & SF_Distinct)
1d1f0 21 3d 30 20 26 26 20 73 75 62 71 75 65 72 79 49 !=0 && subqueryI
1d200 73 41 67 67 20 29 7b 0a 20 20 20 20 20 72 65 74 sAgg ){. ret
1d210 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 2f urn 0; /
1d220 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 36 * Restriction (6
1d230 29 20 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 ) */. }. if(
1d240 70 2d 3e 70 4f 72 64 65 72 42 79 20 26 26 20 70 p->pOrderBy && p
1d250 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b Sub->pOrderBy ){
1d260 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 . return 0;
1d270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1d290 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 /* Res
1d2a0 74 72 69 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f triction (11) */
1d2b0 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 41 67 67 . }. if( isAgg
1d2c0 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 && pSub->pOrder
1d2d0 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 By ) return 0;
1d2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d2f0 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 36 Restriction (16
1d300 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d ) */. if( pSub-
1d310 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 57 >pLimit && p->pW
1d320 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b here ) return 0;
1d330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
1d340 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 39 Restriction (19
1d350 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d ) */. if( pSub-
1d360 3e 70 4c 69 6d 69 74 20 26 26 20 28 70 2d 3e 73 >pLimit && (p->s
1d370 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 elFlags & SF_Dis
1d380 74 69 6e 63 74 29 21 3d 30 20 29 7b 0a 20 20 20 tinct)!=0 ){.
1d390 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 return 0;
1d3a0 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 /* Restricti
1d3b0 6f 6e 20 28 32 31 29 20 2a 2f 0a 20 20 7d 0a 20 on (21) */. }.
1d3c0 20 74 65 73 74 63 61 73 65 28 20 70 53 75 62 2d testcase( pSub-
1d3d0 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 >selFlags & SF_R
1d3e0 65 63 75 72 73 69 76 65 20 29 3b 0a 20 20 74 65 ecursive );. te
1d3f0 73 74 63 61 73 65 28 20 70 53 75 62 2d 3e 73 65 stcase( pSub->se
1d400 6c 46 6c 61 67 73 20 26 20 53 46 5f 4d 69 6e 4d lFlags & SF_MinM
1d410 61 78 41 67 67 20 29 3b 0a 20 20 69 66 28 20 70 axAgg );. if( p
1d420 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 Sub->selFlags &
1d430 28 53 46 5f 52 65 63 75 72 73 69 76 65 7c 53 46 (SF_Recursive|SF
1d440 5f 4d 69 6e 4d 61 78 41 67 67 29 20 29 7b 0a 20 _MinMaxAgg) ){.
1d450 20 20 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 return 0; /*
1d460 52 65 73 74 72 69 63 74 69 6f 6e 73 20 28 32 32 Restrictions (22
1d470 29 20 61 6e 64 20 28 32 34 29 20 2a 2f 0a 20 20 ) and (24) */.
1d480 7d 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 }. if( (p->selF
1d490 6c 61 67 73 20 26 20 53 46 5f 52 65 63 75 72 73 lags & SF_Recurs
1d4a0 69 76 65 29 20 26 26 20 70 53 75 62 2d 3e 70 50 ive) && pSub->pP
1d4b0 72 69 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 rior ){. retu
1d4c0 72 6e 20 30 3b 20 2f 2a 20 52 65 73 74 72 69 63 rn 0; /* Restric
1d4d0 74 69 6f 6e 20 28 32 33 29 20 2a 2f 0a 20 20 7d tion (23) */. }
1d4e0 0a 0a 20 20 2f 2a 20 4f 42 53 4f 4c 45 54 45 20 .. /* OBSOLETE
1d4f0 43 4f 4d 4d 45 4e 54 20 31 3a 0a 20 20 2a 2a 20 COMMENT 1:. **
1d500 52 65 73 74 72 69 63 74 69 6f 6e 20 33 3a 20 20 Restriction 3:
1d510 49 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20 If the subquery
1d520 69 73 20 61 20 6a 6f 69 6e 2c 20 6d 61 6b 65 20 is a join, make
1d530 73 75 72 65 20 74 68 65 20 73 75 62 71 75 65 72 sure the subquer
1d540 79 20 69 73 20 0a 20 20 2a 2a 20 6e 6f 74 20 75 y is . ** not u
1d550 73 65 64 20 61 73 20 74 68 65 20 72 69 67 68 74 sed as the right
1d560 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 6f operand of an o
1d570 75 74 65 72 20 6a 6f 69 6e 2e 20 20 45 78 61 6d uter join. Exam
1d580 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68 69 73 ples of why this
1d590 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 6c 6c . ** is not all
1d5a0 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 owed:. **. **
1d5b0 20 20 20 20 20 20 20 20 74 31 20 4c 45 46 54 20 t1 LEFT
1d5c0 4f 55 54 45 52 20 4a 4f 49 4e 20 28 74 32 20 4a OUTER JOIN (t2 J
1d5d0 4f 49 4e 20 74 33 29 0a 20 20 2a 2a 0a 20 20 2a OIN t3). **. *
1d5e0 2a 20 49 66 20 77 65 20 66 6c 61 74 74 65 6e 20 * If we flatten
1d5f0 74 68 65 20 61 62 6f 76 65 2c 20 77 65 20 77 6f the above, we wo
1d600 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a 20 20 2a uld get. **. *
1d610 2a 20 20 20 20 20 20 20 20 20 28 74 31 20 4c 45 * (t1 LE
1d620 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 74 32 FT OUTER JOIN t2
1d630 29 20 4a 4f 49 4e 20 74 33 0a 20 20 2a 2a 0a 20 ) JOIN t3. **.
1d640 20 2a 2a 20 77 68 69 63 68 20 69 73 20 6e 6f 74 ** which is not
1d650 20 61 74 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 at all the same
1d660 20 74 68 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a thing.. **. *
1d670 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 * OBSOLETE COMME
1d680 4e 54 20 32 3a 0a 20 20 2a 2a 20 52 65 73 74 72 NT 2:. ** Restr
1d690 69 63 74 69 6f 6e 20 31 32 3a 20 20 49 66 20 74 iction 12: If t
1d6a0 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 74 he subquery is t
1d6b0 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e 64 he right operand
1d6c0 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74 65 72 of a left outer
1d6d0 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20 6d 61 6b 65 . ** join, make
1d6e0 20 73 75 72 65 20 74 68 65 20 73 75 62 71 75 65 sure the subque
1d6f0 72 79 20 68 61 73 20 6e 6f 20 57 48 45 52 45 20 ry has no WHERE
1d700 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 20 41 6e 20 clause.. ** An
1d710 65 78 61 6d 70 6c 65 73 20 6f 66 20 77 68 79 20 examples of why
1d720 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6c 6c 6f this is not allo
1d730 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 wed:. **. **
1d740 20 20 20 20 20 20 20 74 31 20 4c 45 46 54 20 4f t1 LEFT O
1d750 55 54 45 52 20 4a 4f 49 4e 20 28 53 45 4c 45 43 UTER JOIN (SELEC
1d760 54 20 2a 20 46 52 4f 4d 20 74 32 20 57 48 45 52 T * FROM t2 WHER
1d770 45 20 74 32 2e 78 3e 30 29 0a 20 20 2a 2a 0a 20 E t2.x>0). **.
1d780 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74 74 65 ** If we flatte
1d790 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77 65 20 n the above, we
1d7a0 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a 20 would get. **.
1d7b0 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31 20 ** (t1
1d7c0 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 LEFT OUTER JOIN
1d7d0 74 32 29 20 57 48 45 52 45 20 74 32 2e 78 3e 30 t2) WHERE t2.x>0
1d7e0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 75 74 20 74 . **. ** But t
1d7f0 68 65 20 74 32 2e 78 3e 30 20 74 65 73 74 20 77 he t2.x>0 test w
1d800 69 6c 6c 20 61 6c 77 61 79 73 20 66 61 69 6c 20 ill always fail
1d810 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f 77 20 6f 66 on a NULL row of
1d820 20 74 32 2c 20 77 68 69 63 68 0a 20 20 2a 2a 20 t2, which. **
1d830 65 66 66 65 63 74 69 76 65 6c 79 20 63 6f 6e 76 effectively conv
1d840 65 72 74 73 20 74 68 65 20 4f 55 54 45 52 20 4a erts the OUTER J
1d850 4f 49 4e 20 69 6e 74 6f 20 61 6e 20 49 4e 4e 45 OIN into an INNE
1d860 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2a 0a 20 20 2a R JOIN.. **. *
1d870 2a 20 54 48 49 53 20 4f 56 45 52 52 49 44 45 53 * THIS OVERRIDES
1d880 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45 4e OBSOLETE COMMEN
1d890 54 53 20 31 20 41 4e 44 20 32 20 41 42 4f 56 45 TS 1 AND 2 ABOVE
1d8a0 3a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23 33 :. ** Ticket #3
1d8b0 33 30 30 20 73 68 6f 77 73 20 74 68 61 74 20 66 300 shows that f
1d8c0 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20 72 69 lattening the ri
1d8d0 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20 4c 45 ght term of a LE
1d8e0 46 54 20 4a 4f 49 4e 0a 20 20 2a 2a 20 69 73 20 FT JOIN. ** is
1d8f0 66 72 61 75 67 68 74 20 77 69 74 68 20 64 61 6e fraught with dan
1d900 67 65 72 2e 20 20 42 65 73 74 20 74 6f 20 61 76 ger. Best to av
1d910 6f 69 64 20 74 68 65 20 77 68 6f 6c 65 20 74 68 oid the whole th
1d920 69 6e 67 2e 20 20 49 66 20 74 68 65 0a 20 20 2a ing. If the. *
1d930 2a 20 73 75 62 71 75 65 72 79 20 69 73 20 74 68 * subquery is th
1d940 65 20 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20 e right term of
1d950 61 20 4c 45 46 54 20 4a 4f 49 4e 2c 20 74 68 65 a LEFT JOIN, the
1d960 6e 20 64 6f 20 6e 6f 74 20 66 6c 61 74 74 65 6e n do not flatten
1d970 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70 53 .. */. if( (pS
1d980 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 ubitem->jointype
1d990 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30 20 & JT_OUTER)!=0
1d9a0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
1d9b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74 72 . }.. /* Restr
1d9c0 69 63 74 69 6f 6e 20 31 37 3a 20 49 66 20 74 68 iction 17: If th
1d9d0 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 e sub-query is a
1d9e0 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1d9f0 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74 0a 20 , then it must.
1da00 20 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 ** use only the
1da10 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 61 UNION ALL opera
1da20 74 6f 72 2e 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 tor. And none of
1da30 20 74 68 65 20 73 69 6d 70 6c 65 20 73 65 6c 65 the simple sele
1da40 63 74 20 71 75 65 72 69 65 73 0a 20 20 2a 2a 20 ct queries. **
1da50 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68 65 that make up the
1da60 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 compound SELECT
1da70 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 are allowed to
1da80 62 65 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 be aggregate or
1da90 64 69 73 74 69 6e 63 74 0a 20 20 2a 2a 20 71 75 distinct. ** qu
1daa0 65 72 69 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 eries.. */. if
1dab0 28 20 70 53 75 62 2d 3e 70 50 72 69 6f 72 20 29 ( pSub->pPrior )
1dac0 7b 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e {. if( pSub->
1dad0 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 pOrderBy ){.
1dae0 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 return 0; /*
1daf0 52 65 73 74 72 69 63 74 69 6f 6e 20 32 30 20 2a Restriction 20 *
1db00 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 /. }. if(
1db10 69 73 41 67 67 20 7c 7c 20 28 70 2d 3e 73 65 6c isAgg || (p->sel
1db20 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 Flags & SF_Disti
1db30 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 72 63 2d nct)!=0 || pSrc-
1db40 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20 20 20 20 >nSrc!=1 ){.
1db50 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 return 0;.
1db60 7d 0a 20 20 20 20 66 6f 72 28 70 53 75 62 31 3d }. for(pSub1=
1db70 70 53 75 62 3b 20 70 53 75 62 31 3b 20 70 53 75 pSub; pSub1; pSu
1db80 62 31 3d 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 b1=pSub1->pPrior
1db90 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 ){. testcas
1dba0 65 28 20 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c e( (pSub1->selFl
1dbb0 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e ags & (SF_Distin
1dbc0 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 ct|SF_Aggregate)
1dbd0 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 29 )==SF_Distinct )
1dbe0 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 ;. testcase
1dbf0 28 20 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c 61 ( (pSub1->selFla
1dc00 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 gs & (SF_Distinc
1dc10 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 t|SF_Aggregate))
1dc20 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65 20 29 ==SF_Aggregate )
1dc30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
1dc40 70 53 75 62 2d 3e 70 53 72 63 21 3d 30 20 29 3b pSub->pSrc!=0 );
1dc50 0a 20 20 20 20 20 20 69 66 28 20 28 70 53 75 62 . if( (pSub
1dc60 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28 53 1->selFlags & (S
1dc70 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41 67 F_Distinct|SF_Ag
1dc80 67 72 65 67 61 74 65 29 29 21 3d 30 0a 20 20 20 gregate))!=0.
1dc90 20 20 20 20 7c 7c 20 28 70 53 75 62 31 2d 3e 70 || (pSub1->p
1dca0 50 72 69 6f 72 20 26 26 20 70 53 75 62 31 2d 3e Prior && pSub1->
1dcb0 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 0a 20 20 20 op!=TK_ALL) .
1dcc0 20 20 20 20 7c 7c 20 70 53 75 62 31 2d 3e 70 53 || pSub1->pS
1dcd0 72 63 2d 3e 6e 53 72 63 3c 31 0a 20 20 20 20 20 rc->nSrc<1.
1dce0 20 20 7c 7c 20 70 53 75 62 2d 3e 70 45 4c 69 73 || pSub->pELis
1dcf0 74 2d 3e 6e 45 78 70 72 21 3d 70 53 75 62 31 2d t->nExpr!=pSub1-
1dd00 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 0a 20 >pEList->nExpr.
1dd10 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 ){.
1dd20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 return 0;.
1dd30 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 }. testcase
1dd40 28 20 70 53 75 62 31 2d 3e 70 53 72 63 2d 3e 6e ( pSub1->pSrc->n
1dd50 53 72 63 3e 31 20 29 3b 0a 20 20 20 20 7d 0a 0a Src>1 );. }..
1dd60 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 /* Restricti
1dd70 6f 6e 20 31 38 2e 20 2a 2f 0a 20 20 20 20 69 66 on 18. */. if
1dd80 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b ( p->pOrderBy ){
1dd90 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 . int ii;.
1dda0 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 for(ii=0; i
1ddb0 69 3c 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 6e i<p->pOrderBy->n
1ddc0 45 78 70 72 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 Expr; ii++){.
1ddd0 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 if( p->pOrd
1dde0 65 72 42 79 2d 3e 61 5b 69 69 5d 2e 75 2e 78 2e erBy->a[ii].u.x.
1ddf0 69 4f 72 64 65 72 42 79 43 6f 6c 3d 3d 30 20 29 iOrderByCol==0 )
1de00 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 return 0;.
1de10 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
1de20 2f 2a 2a 2a 2a 2a 20 49 66 20 77 65 20 72 65 61 /***** If we rea
1de30 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 66 ch this point, f
1de40 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 lattening is per
1de50 6d 69 74 74 65 64 2e 20 2a 2a 2a 2a 2a 2f 0a 20 mitted. *****/.
1de60 20 53 45 4c 45 43 54 54 52 41 43 45 28 31 2c 70 SELECTTRACE(1,p
1de70 50 61 72 73 65 2c 70 2c 28 22 66 6c 61 74 74 65 Parse,p,("flatte
1de80 6e 20 25 73 2e 25 70 20 66 72 6f 6d 20 74 65 72 n %s.%p from ter
1de90 6d 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 m %d\n",.
1dea0 20 20 20 20 20 20 20 20 20 20 20 20 70 53 75 62 pSub
1deb0 2d 3e 7a 53 65 6c 4e 61 6d 65 2c 20 70 53 75 62 ->zSelName, pSub
1dec0 2c 20 69 46 72 6f 6d 29 29 3b 0a 0a 20 20 2f 2a , iFrom));.. /*
1ded0 20 41 75 74 68 6f 72 69 7a 65 20 74 68 65 20 73 Authorize the s
1dee0 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 70 50 61 ubquery */. pPa
1def0 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 rse->zAuthContex
1df00 74 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e t = pSubitem->zN
1df10 61 6d 65 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 ame;. TESTONLY(
1df20 69 20 3d 29 20 73 71 6c 69 74 65 33 41 75 74 68 i =) sqlite3Auth
1df30 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 Check(pParse, SQ
1df40 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 LITE_SELECT, 0,
1df50 30 2c 20 30 29 3b 0a 20 20 74 65 73 74 63 61 73 0, 0);. testcas
1df60 65 28 20 69 3d 3d 53 51 4c 49 54 45 5f 44 45 4e e( i==SQLITE_DEN
1df70 59 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a Y );. pParse->z
1df80 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 AuthContext = zS
1df90 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 3b avedAuthContext;
1dfa0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 75 .. /* If the su
1dfb0 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d b-query is a com
1dfc0 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 pound SELECT sta
1dfd0 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28 62 79 tement, then (by
1dfe0 20 72 65 73 74 72 69 63 74 69 6f 6e 73 0a 20 20 restrictions.
1dff0 2a 2a 20 31 37 20 61 6e 64 20 31 38 20 61 62 6f ** 17 and 18 abo
1e000 76 65 29 20 69 74 20 6d 75 73 74 20 62 65 20 61 ve) it must be a
1e010 20 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64 20 74 UNION ALL and t
1e020 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 he parent query
1e030 6d 75 73 74 20 0a 20 20 2a 2a 20 62 65 20 6f 66 must . ** be of
1e040 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a the form:. **.
1e050 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 ** SELECT
1e060 3c 65 78 70 72 2d 6c 69 73 74 3e 20 46 52 4f 4d <expr-list> FROM
1e070 20 28 3c 73 75 62 2d 71 75 65 72 79 3e 29 20 3c (<sub-query>) <
1e080 77 68 65 72 65 2d 63 6c 61 75 73 65 3e 20 0a 20 where-clause> .
1e090 20 2a 2a 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 65 **. ** followe
1e0a0 64 20 62 79 20 61 6e 79 20 4f 52 44 45 52 20 42 d by any ORDER B
1e0b0 59 2c 20 4c 49 4d 49 54 20 61 6e 64 2f 6f 72 20 Y, LIMIT and/or
1e0c0 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20 OFFSET clauses.
1e0d0 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 This block. **
1e0e0 63 72 65 61 74 65 73 20 4e 2d 31 20 63 6f 70 69 creates N-1 copi
1e0f0 65 73 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 es of the parent
1e100 20 71 75 65 72 79 20 77 69 74 68 6f 75 74 20 61 query without a
1e110 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d ny ORDER BY, LIM
1e120 49 54 20 6f 72 20 0a 20 20 2a 2a 20 4f 46 46 53 IT or . ** OFFS
1e130 45 54 20 63 6c 61 75 73 65 73 20 61 6e 64 20 6a ET clauses and j
1e140 6f 69 6e 73 20 74 68 65 6d 20 74 6f 20 74 68 65 oins them to the
1e150 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 left-hand-side
1e160 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a of the original.
1e170 20 20 2a 2a 20 75 73 69 6e 67 20 55 4e 49 4f 4e ** using UNION
1e180 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 73 2e 20 ALL operators.
1e190 49 6e 20 74 68 69 73 20 63 61 73 65 20 4e 20 69 In this case N i
1e1a0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 s the number of
1e1b0 73 69 6d 70 6c 65 0a 20 20 2a 2a 20 73 65 6c 65 simple. ** sele
1e1c0 63 74 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e ct statements in
1e1d0 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 75 the compound su
1e1e0 62 2d 71 75 65 72 79 2e 0a 20 20 2a 2a 0a 20 20 b-query.. **.
1e1f0 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a ** Example:. **
1e200 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 . ** SELECT
1e210 20 61 2b 31 20 46 52 4f 4d 20 28 0a 20 20 2a 2a a+1 FROM (. **
1e220 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 78 SELECT x
1e230 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20 20 FROM tab. **
1e240 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a UNION ALL.
1e250 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c 45 ** SELE
1e260 43 54 20 79 20 46 52 4f 4d 20 74 61 62 0a 20 20 CT y FROM tab.
1e270 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 ** UNION
1e280 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 ALL. **
1e290 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29 20 SELECT abs(z*2)
1e2a0 46 52 4f 4d 20 74 61 62 32 0a 20 20 2a 2a 20 20 FROM tab2. **
1e2b0 20 20 20 29 20 57 48 45 52 45 20 61 21 3d 35 20 ) WHERE a!=5
1e2c0 4f 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a 0a ORDER BY 1. **.
1e2d0 20 20 2a 2a 20 54 72 61 6e 73 66 6f 72 6d 65 64 ** Transformed
1e2e0 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a into:. **. **
1e2f0 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b 31 20 SELECT x+1
1e300 46 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20 78 FROM tab WHERE x
1e310 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 +1!=5. ** U
1e320 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 NION ALL. **
1e330 20 20 53 45 4c 45 43 54 20 79 2b 31 20 46 52 4f SELECT y+1 FRO
1e340 4d 20 74 61 62 20 57 48 45 52 45 20 79 2b 31 21 M tab WHERE y+1!
1e350 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49 4f =5. ** UNIO
1e360 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53 N ALL. ** S
1e370 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29 2b 31 ELECT abs(z*2)+1
1e380 20 46 52 4f 4d 20 74 61 62 32 20 57 48 45 52 45 FROM tab2 WHERE
1e390 20 61 62 73 28 7a 2a 32 29 2b 31 21 3d 35 0a 20 abs(z*2)+1!=5.
1e3a0 20 2a 2a 20 20 20 20 20 4f 52 44 45 52 20 42 59 ** ORDER BY
1e3b0 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 1. **. ** We
1e3c0 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20 22 63 call this the "c
1e3d0 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 ompound-subquery
1e3e0 20 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0a 20 20 flattening"..
1e3f0 2a 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d 70 53 */. for(pSub=pS
1e400 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53 75 62 ub->pPrior; pSub
1e410 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 ; pSub=pSub->pPr
1e420 69 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65 63 74 ior){. Select
1e430 20 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78 70 72 *pNew;. Expr
1e440 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d List *pOrderBy =
1e450 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 p->pOrderBy;.
1e460 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 20 3d Expr *pLimit =
1e470 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 p->pLimit;.
1e480 45 78 70 72 20 2a 70 4f 66 66 73 65 74 20 3d 20 Expr *pOffset =
1e490 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 p->pOffset;.
1e4a0 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 20 3d Select *pPrior =
1e4b0 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 p->pPrior;.
1e4c0 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b p->pOrderBy = 0;
1e4d0 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 30 . p->pSrc = 0
1e4e0 3b 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 ;. p->pPrior
1e4f0 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d = 0;. p->pLim
1e500 69 74 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 it = 0;. p->p
1e510 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 Offset = 0;.
1e520 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 53 65 pNew = sqlite3Se
1e530 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2c 20 30 lectDup(db, p, 0
1e540 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 );. sqlite3Se
1e550 6c 65 63 74 53 65 74 4e 61 6d 65 28 70 4e 65 77 lectSetName(pNew
1e560 2c 20 70 53 75 62 2d 3e 7a 53 65 6c 4e 61 6d 65 , pSub->zSelName
1e570 29 3b 0a 20 20 20 20 70 2d 3e 70 4f 66 66 73 65 );. p->pOffse
1e580 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 20 t = pOffset;.
1e590 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c 69 p->pLimit = pLi
1e5a0 6d 69 74 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 mit;. p->pOrd
1e5b0 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 3b erBy = pOrderBy;
1e5c0 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 70 . p->pSrc = p
1e5d0 53 72 63 3b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d Src;. p->op =
1e5e0 20 54 4b 5f 41 4c 4c 3b 0a 20 20 20 20 69 66 28 TK_ALL;. if(
1e5f0 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 pNew==0 ){.
1e600 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 p->pPrior = pP
1e610 72 69 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b rior;. }else{
1e620 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 50 72 . pNew->pPr
1e630 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 ior = pPrior;.
1e640 20 20 20 20 69 66 28 20 70 50 72 69 6f 72 20 29 if( pPrior )
1e650 20 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 20 3d pPrior->pNext =
1e660 20 70 4e 65 77 3b 0a 20 20 20 20 20 20 70 4e 65 pNew;. pNe
1e670 77 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 w->pNext = p;.
1e680 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 p->pPrior =
1e690 70 4e 65 77 3b 0a 20 20 20 20 20 20 53 45 4c 45 pNew;. SELE
1e6a0 43 54 54 52 41 43 45 28 32 2c 70 50 61 72 73 65 CTTRACE(2,pParse
1e6b0 2c 70 2c 0a 20 20 20 20 20 20 20 20 20 28 22 63 ,p,. ("c
1e6c0 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 ompound-subquery
1e6d0 20 66 6c 61 74 74 65 6e 65 72 20 63 72 65 61 74 flattener creat
1e6e0 65 73 20 25 73 2e 25 70 20 61 73 20 70 65 65 72 es %s.%p as peer
1e6f0 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 70 4e \n",. pN
1e700 65 77 2d 3e 7a 53 65 6c 4e 61 6d 65 2c 20 70 4e ew->zSelName, pN
1e710 65 77 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 ew));. }.
1e720 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 if( db->mallocFa
1e730 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 31 3b iled ) return 1;
1e740 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e . }.. /* Begin
1e750 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20 flattening the
1e760 69 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f iFrom-th entry o
1e770 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 f the FROM claus
1e780 65 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f e . ** in the o
1e790 75 74 65 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f uter query.. */
1e7a0 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62 31 20 . pSub = pSub1
1e7b0 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c = pSubitem->pSel
1e7c0 65 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 ect;.. /* Delet
1e7d0 65 20 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20 e the transient
1e7e0 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 table structure
1e7f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 associated with
1e800 74 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 the. ** subquer
1e810 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 y. */. sqlite3
1e820 44 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 DbFree(db, pSubi
1e830 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b tem->zDatabase);
1e840 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 . sqlite3DbFree
1e850 28 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a (db, pSubitem->z
1e860 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 Name);. sqlite3
1e870 44 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 DbFree(db, pSubi
1e880 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 tem->zAlias);.
1e890 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 pSubitem->zDatab
1e8a0 61 73 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 ase = 0;. pSubi
1e8b0 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a tem->zName = 0;.
1e8c0 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 pSubitem->zAli
1e8d0 61 73 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 as = 0;. pSubit
1e8e0 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 30 3b em->pSelect = 0;
1e8f0 0a 0a 20 20 2f 2a 20 44 65 66 65 72 20 64 65 6c .. /* Defer del
1e900 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20 eting the Table
1e910 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 object associate
1e920 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 d with the. **
1e930 73 75 62 71 75 65 72 79 20 75 6e 74 69 6c 20 63 subquery until c
1e940 6f 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 69 ode generation i
1e950 73 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 2c s. ** complete,
1e960 20 73 69 6e 63 65 20 74 68 65 72 65 20 6d 61 79 since there may
1e970 20 73 74 69 6c 6c 20 65 78 69 73 74 20 45 78 70 still exist Exp
1e980 72 2e 70 54 61 62 20 65 6e 74 72 69 65 73 20 74 r.pTab entries t
1e990 68 61 74 0a 20 20 2a 2a 20 72 65 66 65 72 20 74 hat. ** refer t
1e9a0 6f 20 74 68 65 20 73 75 62 71 75 65 72 79 20 65 o the subquery e
1e9b0 76 65 6e 20 61 66 74 65 72 20 66 6c 61 74 74 65 ven after flatte
1e9c0 6e 69 6e 67 2e 20 20 54 69 63 6b 65 74 20 23 33 ning. Ticket #3
1e9d0 33 34 36 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 70 346.. **. ** p
1e9e0 53 75 62 69 74 65 6d 2d 3e 70 54 61 62 20 69 73 Subitem->pTab is
1e9f0 20 61 6c 77 61 79 73 20 6e 6f 6e 2d 4e 55 4c 4c always non-NULL
1ea00 20 62 79 20 74 65 73 74 20 72 65 73 74 72 69 63 by test restric
1ea10 74 69 6f 6e 73 20 61 6e 64 20 74 65 73 74 73 20 tions and tests
1ea20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 above.. */. if
1ea30 28 20 41 4c 57 41 59 53 28 70 53 75 62 69 74 65 ( ALWAYS(pSubite
1ea40 6d 2d 3e 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 m->pTab!=0) ){.
1ea50 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 54 6f Table *pTabTo
1ea60 44 65 6c 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e Del = pSubitem->
1ea70 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 pTab;. if( pT
1ea80 61 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d 3d 31 abToDel->nRef==1
1ea90 20 29 7b 0a 20 20 20 20 20 20 50 61 72 73 65 20 ){. Parse
1eaa0 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c *pToplevel = sql
1eab0 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 ite3ParseTopleve
1eac0 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 l(pParse);.
1ead0 20 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e 65 78 pTabToDel->pNex
1eae0 74 5a 6f 6d 62 69 65 20 3d 20 70 54 6f 70 6c 65 tZombie = pTople
1eaf0 76 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b vel->pZombieTab;
1eb00 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c . pToplevel
1eb10 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 ->pZombieTab = p
1eb20 54 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20 7d 65 TabToDel;. }e
1eb30 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62 54 lse{. pTabT
1eb40 6f 44 65 6c 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 oDel->nRef--;.
1eb50 20 20 7d 0a 20 20 20 20 70 53 75 62 69 74 65 6d }. pSubitem
1eb60 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a ->pTab = 0;. }.
1eb70 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 . /* The follow
1eb80 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e ing loop runs on
1eb90 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d ce for each term
1eba0 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 in a compound-s
1ebb0 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 66 6c 61 ubquery. ** fla
1ebc0 74 74 65 6e 69 6e 67 20 28 61 73 20 64 65 73 63 ttening (as desc
1ebd0 72 69 62 65 64 20 61 62 6f 76 65 29 2e 20 20 49 ribed above). I
1ebe0 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 f we are doing a
1ebf0 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e 64 0a different kind.
1ec00 20 20 2a 2a 20 6f 66 20 66 6c 61 74 74 65 6e 69 ** of flatteni
1ec10 6e 67 20 2d 20 61 20 66 6c 61 74 74 65 6e 69 6e ng - a flattenin
1ec20 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 63 g other than a c
1ec30 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 ompound-subquery
1ec40 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d 0a 20 20 flattening -.
1ec50 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 6c 6f 6f ** then this loo
1ec60 70 20 6f 6e 6c 79 20 72 75 6e 73 20 6f 6e 63 65 p only runs once
1ec70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 .. **. ** This
1ec80 20 6c 6f 6f 70 20 6d 6f 76 65 73 20 61 6c 6c 20 loop moves all
1ec90 6f 66 20 74 68 65 20 46 52 4f 4d 20 65 6c 65 6d of the FROM elem
1eca0 65 6e 74 73 20 6f 66 20 74 68 65 20 73 75 62 71 ents of the subq
1ecb0 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 20 uery into the.
1ecc0 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 ** the FROM clau
1ecd0 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 se of the outer
1ece0 71 75 65 72 79 2e 20 20 42 65 66 6f 72 65 20 64 query. Before d
1ecf0 6f 69 6e 67 20 74 68 69 73 2c 20 72 65 6d 65 6d oing this, remem
1ed00 62 65 72 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 ber. ** the cur
1ed10 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 sor number for t
1ed20 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 75 74 65 he original oute
1ed30 72 20 71 75 65 72 79 20 46 52 4f 4d 20 65 6c 65 r query FROM ele
1ed40 6d 65 6e 74 20 69 6e 0a 20 20 2a 2a 20 69 50 61 ment in. ** iPa
1ed50 72 65 6e 74 2e 20 20 54 68 65 20 69 50 61 72 65 rent. The iPare
1ed60 6e 74 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 6e nt cursor will n
1ed70 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 53 ever be used. S
1ed80 75 62 73 65 71 75 65 6e 74 20 63 6f 64 65 0a 20 ubsequent code.
1ed90 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 65 78 ** will scan ex
1eda0 70 72 65 73 73 69 6f 6e 73 20 6c 6f 6f 6b 69 6e pressions lookin
1edb0 67 20 66 6f 72 20 69 50 61 72 65 6e 74 20 72 65 g for iParent re
1edc0 66 65 72 65 6e 63 65 73 20 61 6e 64 20 72 65 70 ferences and rep
1edd0 6c 61 63 65 0a 20 20 2a 2a 20 74 68 6f 73 65 20 lace. ** those
1ede0 72 65 66 65 72 65 6e 63 65 73 20 77 69 74 68 20 references with
1edf0 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 expressions that
1ee00 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 resolve to the
1ee10 73 75 62 71 75 65 72 79 20 46 52 4f 4d 0a 20 20 subquery FROM.
1ee20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 77 65 20 61 ** elements we a
1ee30 72 65 20 6e 6f 77 20 63 6f 70 79 69 6e 67 20 69 re now copying i
1ee40 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50 n.. */. for(pP
1ee50 61 72 65 6e 74 3d 70 3b 20 70 50 61 72 65 6e 74 arent=p; pParent
1ee60 3b 20 70 50 61 72 65 6e 74 3d 70 50 61 72 65 6e ; pParent=pParen
1ee70 74 2d 3e 70 50 72 69 6f 72 2c 20 70 53 75 62 3d t->pPrior, pSub=
1ee80 70 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 pSub->pPrior){.
1ee90 20 20 20 69 6e 74 20 6e 53 75 62 53 72 63 3b 0a int nSubSrc;.
1eea0 20 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 20 u8 jointype
1eeb0 3d 20 30 3b 0a 20 20 20 20 70 53 75 62 53 72 63 = 0;. pSubSrc
1eec0 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 20 20 = pSub->pSrc;
1eed0 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 /* FROM claus
1eee0 65 20 6f 66 20 73 75 62 71 75 65 72 79 20 2a 2f e of subquery */
1eef0 0a 20 20 20 20 6e 53 75 62 53 72 63 20 3d 20 70 . nSubSrc = p
1ef00 53 75 62 53 72 63 2d 3e 6e 53 72 63 3b 20 20 2f SubSrc->nSrc; /
1ef10 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d * Number of term
1ef20 73 20 69 6e 20 73 75 62 71 75 65 72 79 20 46 52 s in subquery FR
1ef30 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 OM clause */.
1ef40 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e 74 2d pSrc = pParent-
1ef50 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a 20 46 52 >pSrc; /* FR
1ef60 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 OM clause of the
1ef70 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a outer query */.
1ef80 0a 20 20 20 20 69 66 28 20 70 53 72 63 20 29 7b . if( pSrc ){
1ef90 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
1efa0 50 61 72 65 6e 74 3d 3d 70 20 29 3b 20 20 2f 2a Parent==p ); /*
1efb0 20 46 69 72 73 74 20 74 69 6d 65 20 74 68 72 6f First time thro
1efc0 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a ugh the loop */.
1efd0 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d jointype =
1efe0 20 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 pSubitem->joint
1eff0 79 70 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a ype;. }else{.
1f000 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 assert( pP
1f010 61 72 65 6e 74 21 3d 70 20 29 3b 20 20 2f 2a 20 arent!=p ); /*
1f020 32 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 2nd and subseque
1f030 6e 74 20 74 69 6d 65 73 20 74 68 72 6f 75 67 68 nt times through
1f040 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 the loop */.
1f050 20 20 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e pSrc = pParen
1f060 74 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65 t->pSrc = sqlite
1f070 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 3SrcListAppend(d
1f080 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 b, 0, 0, 0);.
1f090 20 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 if( pSrc==0 )
1f0a0 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 {. assert
1f0b0 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c ( db->mallocFail
1f0c0 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72 ed );. br
1f0d0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 eak;. }.
1f0e0 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 }.. /* The s
1f0f0 75 62 71 75 65 72 79 20 75 73 65 73 20 61 20 73 ubquery uses a s
1f100 69 6e 67 6c 65 20 73 6c 6f 74 20 6f 66 20 74 68 ingle slot of th
1f110 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 e FROM clause of
1f120 20 74 68 65 20 6f 75 74 65 72 0a 20 20 20 20 2a the outer. *
1f130 2a 20 71 75 65 72 79 2e 20 20 49 66 20 74 68 65 * query. If the
1f140 20 73 75 62 71 75 65 72 79 20 68 61 73 20 6d 6f subquery has mo
1f150 72 65 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d re than one elem
1f160 65 6e 74 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 ent in its FROM
1f170 63 6c 61 75 73 65 2c 0a 20 20 20 20 2a 2a 20 74 clause,. ** t
1f180 68 65 6e 20 65 78 70 61 6e 64 20 74 68 65 20 6f hen expand the o
1f190 75 74 65 72 20 71 75 65 72 79 20 74 6f 20 6d 61 uter query to ma
1f1a0 6b 65 20 73 70 61 63 65 20 66 6f 72 20 69 74 20 ke space for it
1f1b0 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 65 6c 65 6d to hold all elem
1f1c0 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f 66 20 74 ents. ** of t
1f1d0 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20 he subquery..
1f1e0 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 **. ** Examp
1f1f0 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a le:. **. *
1f200 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 * SELECT * FR
1f210 4f 4d 20 74 61 62 41 2c 20 28 53 45 4c 45 43 54 OM tabA, (SELECT
1f220 20 2a 20 46 52 4f 4d 20 73 75 62 31 2c 20 73 75 * FROM sub1, su
1f230 62 32 29 2c 20 74 61 62 42 3b 0a 20 20 20 20 2a b2), tabB;. *
1f240 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 75 74 *. ** The out
1f250 65 72 20 71 75 65 72 79 20 68 61 73 20 33 20 73 er query has 3 s
1f260 6c 6f 74 73 20 69 6e 20 69 74 73 20 46 52 4f 4d lots in its FROM
1f270 20 63 6c 61 75 73 65 2e 20 20 4f 6e 65 20 73 6c clause. One sl
1f280 6f 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ot of the. **
1f290 20 6f 75 74 65 72 20 71 75 65 72 79 20 28 74 68 outer query (th
1f2a0 65 20 6d 69 64 64 6c 65 20 73 6c 6f 74 29 20 69 e middle slot) i
1f2b0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 73 75 s used by the su
1f2c0 62 71 75 65 72 79 2e 20 20 54 68 65 20 6e 65 78 bquery. The nex
1f2d0 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6f t. ** block o
1f2e0 66 20 63 6f 64 65 20 77 69 6c 6c 20 65 78 70 61 f code will expa
1f2f0 6e 64 20 74 68 65 20 6f 75 74 20 71 75 65 72 79 nd the out query
1f300 20 74 6f 20 34 20 73 6c 6f 74 73 2e 20 20 54 68 to 4 slots. Th
1f310 65 20 6d 69 64 64 6c 65 0a 20 20 20 20 2a 2a 20 e middle. **
1f320 73 6c 6f 74 20 69 73 20 65 78 70 61 6e 64 65 64 slot is expanded
1f330 20 74 6f 20 74 77 6f 20 73 6c 6f 74 73 20 69 6e to two slots in
1f340 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 73 order to make s
1f350 70 61 63 65 20 66 6f 72 20 74 68 65 0a 20 20 20 pace for the.
1f360 20 2a 2a 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 ** two elements
1f370 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
1f380 75 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75 use of the subqu
1f390 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 ery.. */.
1f3a0 69 66 28 20 6e 53 75 62 53 72 63 3e 31 20 29 7b if( nSubSrc>1 ){
1f3b0 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e . pParent->
1f3c0 70 53 72 63 20 3d 20 70 53 72 63 20 3d 20 73 71 pSrc = pSrc = sq
1f3d0 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 lite3SrcListEnla
1f3e0 72 67 65 28 64 62 2c 20 70 53 72 63 2c 20 6e 53 rge(db, pSrc, nS
1f3f0 75 62 53 72 63 2d 31 2c 69 46 72 6f 6d 2b 31 29 ubSrc-1,iFrom+1)
1f400 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e ;. if( db->
1f410 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
1f420 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 break;.
1f430 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 }. }..
1f440 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 74 68 /* Transfer th
1f450 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 e FROM clause te
1f460 72 6d 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 rms from the sub
1f470 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 query into the.
1f480 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 ** outer quer
1f490 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f y.. */. fo
1f4a0 72 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53 72 63 r(i=0; i<nSubSrc
1f4b0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 ; i++){. sq
1f4c0 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 lite3IdListDelet
1f4d0 65 28 64 62 2c 20 70 53 72 63 2d 3e 61 5b 69 2b e(db, pSrc->a[i+
1f4e0 69 46 72 6f 6d 5d 2e 70 55 73 69 6e 67 29 3b 0a iFrom].pUsing);.
1f4f0 20 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b pSrc->a[i+
1f500 69 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53 72 63 iFrom] = pSubSrc
1f510 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 6d 65 ->a[i];. me
1f520 6d 73 65 74 28 26 70 53 75 62 53 72 63 2d 3e 61 mset(&pSubSrc->a
1f530 5b 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 [i], 0, sizeof(p
1f540 53 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29 3b 0a SubSrc->a[i]));.
1f550 20 20 20 20 7d 0a 20 20 20 20 70 53 72 63 2d 3e }. pSrc->
1f560 61 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79 70 a[iFrom].jointyp
1f570 65 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 e = jointype;.
1f580 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 62 65 67 69 . /* Now begi
1f590 6e 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 73 n substituting s
1f5a0 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 20 73 ubquery result s
1f5b0 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 66 et expressions f
1f5c0 6f 72 20 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 or . ** refer
1f5d0 65 6e 63 65 73 20 74 6f 20 74 68 65 20 69 50 61 ences to the iPa
1f5e0 72 65 6e 74 20 69 6e 20 74 68 65 20 6f 75 74 65 rent in the oute
1f5f0 72 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a 20 r query.. **
1f600 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a . ** Example:
1f610 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 . **. **
1f620 20 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31 SELECT a+5, b*1
1f630 30 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 0 FROM (SELECT x
1f640 2a 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41 53 *3 AS a, y+10 AS
1f650 20 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45 52 b FROM t1) WHER
1f660 45 20 61 3e 62 3b 0a 20 20 20 20 2a 2a 20 20 20 E a>b;. **
1f670 5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 \
1f680 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f \_________
1f690 5f 5f 5f 5f 20 73 75 62 71 75 65 72 79 20 5f 5f ____ subquery __
1f6a0 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 ________/
1f6b0 20 20 20 2f 0a 20 20 20 20 2a 2a 20 20 20 20 5c /. ** \
1f6c0 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f ________________
1f6d0 5f 5f 5f 5f 5f 20 6f 75 74 65 72 20 71 75 65 72 _____ outer quer
1f6e0 79 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f y ______________
1f6f0 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f ________________
1f700 2f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 /. **. **
1f710 57 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79 We look at every
1f720 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 expression in t
1f730 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61 he outer query a
1f740 6e 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77 nd every place w
1f750 65 20 73 65 65 0a 20 20 20 20 2a 2a 20 22 61 22 e see. ** "a"
1f760 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22 we substitute "
1f770 78 2a 33 22 20 61 6e 64 20 65 76 65 72 79 20 70 x*3" and every p
1f780 6c 61 63 65 20 77 65 20 73 65 65 20 22 62 22 20 lace we see "b"
1f790 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22 79 we substitute "y
1f7a0 2b 31 30 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 +10".. */.
1f7b0 20 70 4c 69 73 74 20 3d 20 70 50 61 72 65 6e 74 pList = pParent
1f7c0 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 66 6f ->pEList;. fo
1f7d0 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e r(i=0; i<pList->
1f7e0 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 nExpr; i++){.
1f7f0 20 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b if( pList->a[
1f800 69 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 i].zName==0 ){.
1f810 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 char *zNa
1f820 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 me = sqlite3DbSt
1f830 72 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2d 3e rDup(db, pList->
1f840 61 5b 69 5d 2e 7a 53 70 61 6e 29 3b 0a 20 20 20 a[i].zSpan);.
1f850 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 71 75 sqlite3Dequ
1f860 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 ote(zName);.
1f870 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e pList->a[i].
1f880 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 zName = zName;.
1f890 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 }. }.
1f8a0 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 substExprList(d
1f8b0 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 b, pParent->pELi
1f8c0 73 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 st, iParent, pSu
1f8d0 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 b->pEList);.
1f8e0 69 66 28 20 69 73 41 67 67 20 29 7b 0a 20 20 20 if( isAgg ){.
1f8f0 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 substExprList
1f900 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 47 (db, pParent->pG
1f910 72 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c roupBy, iParent,
1f920 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a pSub->pEList);.
1f930 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 pParent->p
1f940 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74 45 78 Having = substEx
1f950 70 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e pr(db, pParent->
1f960 70 48 61 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 pHaving, iParent
1f970 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b , pSub->pEList);
1f980 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 . }. if( p
1f990 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b Sub->pOrderBy ){
1f9a0 0a 20 20 20 20 20 20 2f 2a 20 41 74 20 74 68 69 . /* At thi
1f9b0 73 20 70 6f 69 6e 74 2c 20 61 6e 79 20 6e 6f 6e s point, any non
1f9c0 2d 7a 65 72 6f 20 69 4f 72 64 65 72 42 79 43 6f -zero iOrderByCo
1f9d0 6c 20 76 61 6c 75 65 73 20 69 6e 64 69 63 61 74 l values indicat
1f9e0 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20 e that the.
1f9f0 20 2a 2a 20 4f 52 44 45 52 20 42 59 20 63 6f 6c ** ORDER BY col
1fa00 75 6d 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 umn expression i
1fa10 73 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 s identical to t
1fa20 68 65 20 69 4f 72 64 65 72 42 79 43 6f 6c 27 74 he iOrderByCol't
1fa30 68 0a 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 h. ** expre
1fa40 73 73 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 62 ssion returned b
1fa50 79 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 y SELECT stateme
1fa60 6e 74 20 70 53 75 62 2e 20 53 69 6e 63 65 20 74 nt pSub. Since t
1fa70 68 65 73 65 20 76 61 6c 75 65 73 0a 20 20 20 20 hese values.
1fa80 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63 65 ** do not nece
1fa90 73 73 61 72 69 6c 79 20 63 6f 72 72 65 73 70 6f ssarily correspo
1faa0 6e 64 20 74 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e nd to columns in
1fab0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
1fac0 74 20 70 50 61 72 65 6e 74 2c 0a 20 20 20 20 20 t pParent,.
1fad0 20 2a 2a 20 7a 65 72 6f 20 74 68 65 6d 20 62 65 ** zero them be
1fae0 66 6f 72 65 20 74 72 61 6e 73 66 65 72 69 6e 67 fore transfering
1faf0 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c the ORDER BY cl
1fb00 61 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 ause.. **.
1fb10 20 20 20 20 20 2a 2a 20 4e 6f 74 20 64 6f 69 6e ** Not doin
1fb20 67 20 74 68 69 73 20 6d 61 79 20 63 61 75 73 65 g this may cause
1fb30 20 61 6e 20 65 72 72 6f 72 20 69 66 20 61 20 73 an error if a s
1fb40 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 ubsequent call t
1fb50 6f 20 74 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 o this. **
1fb60 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 function attempt
1fb70 73 20 74 6f 20 66 6c 61 74 74 65 6e 20 61 20 63 s to flatten a c
1fb80 6f 6d 70 6f 75 6e 64 20 73 75 62 2d 71 75 65 72 ompound sub-quer
1fb90 79 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 0a 20 y into pParent.
1fba0 20 20 20 20 20 2a 2a 20 28 74 68 65 20 6f 6e 6c ** (the onl
1fbb0 79 20 77 61 79 20 74 68 69 73 20 63 61 6e 20 68 y way this can h
1fbc0 61 70 70 65 6e 20 69 73 20 69 66 20 74 68 65 20 appen is if the
1fbd0 63 6f 6d 70 6f 75 6e 64 20 73 75 62 2d 71 75 65 compound sub-que
1fbe0 72 79 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 63 ry is. ** c
1fbf0 75 72 72 65 6e 74 6c 79 20 70 61 72 74 20 6f 66 urrently part of
1fc00 20 70 53 75 62 2d 3e 70 53 72 63 29 2e 20 53 65 pSub->pSrc). Se
1fc10 65 20 74 69 63 6b 65 74 20 5b 64 31 31 61 36 65 e ticket [d11a6e
1fc20 39 30 38 66 5d 2e 20 20 2a 2f 0a 20 20 20 20 20 908f]. */.
1fc30 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 ExprList *pOrde
1fc40 72 42 79 20 3d 20 70 53 75 62 2d 3e 70 4f 72 64 rBy = pSub->pOrd
1fc50 65 72 42 79 3b 0a 20 20 20 20 20 20 66 6f 72 28 erBy;. for(
1fc60 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d i=0; i<pOrderBy-
1fc70 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 >nExpr; i++){.
1fc80 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e pOrderBy->
1fc90 61 5b 69 5d 2e 75 2e 78 2e 69 4f 72 64 65 72 42 a[i].u.x.iOrderB
1fca0 79 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 yCol = 0;.
1fcb0 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 }. assert(
1fcc0 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 pParent->pOrderB
1fcd0 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 y==0 );. as
1fce0 73 65 72 74 28 20 70 53 75 62 2d 3e 70 50 72 69 sert( pSub->pPri
1fcf0 6f 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 or==0 );. p
1fd00 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 Parent->pOrderBy
1fd10 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 = pOrderBy;.
1fd20 20 20 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 pSub->pOrderB
1fd30 79 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 y = 0;. }else
1fd40 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f if( pParent->pO
1fd50 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 rderBy ){.
1fd60 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 substExprList(db
1fd70 2c 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 , pParent->pOrde
1fd80 72 42 79 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 rBy, iParent, pS
1fd90 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 ub->pEList);.
1fda0 20 7d 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d }. if( pSub-
1fdb0 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 20 >pWhere ){.
1fdc0 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 pWhere = sqlite
1fdd0 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 75 3ExprDup(db, pSu
1fde0 62 2d 3e 70 57 68 65 72 65 2c 20 30 29 3b 0a 20 b->pWhere, 0);.
1fdf0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
1fe00 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20 20 pWhere = 0;.
1fe10 7d 0a 20 20 20 20 69 66 28 20 73 75 62 71 75 65 }. if( subque
1fe20 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20 20 ryIsAgg ){.
1fe30 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 assert( pParent
1fe40 2d 3e 70 48 61 76 69 6e 67 3d 3d 30 20 29 3b 0a ->pHaving==0 );.
1fe50 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 pParent->p
1fe60 48 61 76 69 6e 67 20 3d 20 70 50 61 72 65 6e 74 Having = pParent
1fe70 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20 20 20 20 ->pWhere;.
1fe80 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 pParent->pWhere
1fe90 3d 20 70 57 68 65 72 65 3b 0a 20 20 20 20 20 20 = pWhere;.
1fea0 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67 pParent->pHaving
1feb0 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c = substExpr(db,
1fec0 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e pParent->pHavin
1fed0 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 g, iParent, pSub
1fee0 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 ->pEList);.
1fef0 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e pParent->pHavin
1ff00 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 g = sqlite3ExprA
1ff10 6e 64 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e nd(db, pParent->
1ff20 70 48 61 76 69 6e 67 2c 20 0a 20 20 20 20 20 20 pHaving, .
1ff30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
1ff40 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 sqli
1ff50 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 te3ExprDup(db, p
1ff60 53 75 62 2d 3e 70 48 61 76 69 6e 67 2c 20 30 29 Sub->pHaving, 0)
1ff70 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 );. assert(
1ff80 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 pParent->pGroup
1ff90 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 By==0 );. p
1ffa0 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42 79 Parent->pGroupBy
1ffb0 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 = sqlite3ExprLi
1ffc0 73 74 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e stDup(db, pSub->
1ffd0 70 47 72 6f 75 70 42 79 2c 20 30 29 3b 0a 20 20 pGroupBy, 0);.
1ffe0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 }else{. p
1fff0 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d Parent->pWhere =
20000 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 substExpr(db, p
20010 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c 20 Parent->pWhere,
20020 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e 70 iParent, pSub->p
20030 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70 50 EList);. pP
20040 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d 20 arent->pWhere =
20050 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 sqlite3ExprAnd(d
20060 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 b, pParent->pWhe
20070 72 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 20 re, pWhere);.
20080 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68 65 }. . /* The
20090 20 66 6c 61 74 74 65 6e 65 64 20 71 75 65 72 79 flattened query
200a0 20 69 73 20 64 69 73 74 69 6e 63 74 20 69 66 20 is distinct if
200b0 65 69 74 68 65 72 20 74 68 65 20 69 6e 6e 65 72 either the inner
200c0 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f or the. ** o
200d0 75 74 65 72 20 71 75 65 72 79 20 69 73 20 64 69 uter query is di
200e0 73 74 69 6e 63 74 2e 20 0a 20 20 20 20 2a 2f 0a stinct. . */.
200f0 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 73 65 6c pParent->sel
20100 46 6c 61 67 73 20 7c 3d 20 70 53 75 62 2d 3e 73 Flags |= pSub->s
20110 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 elFlags & SF_Dis
20120 74 69 6e 63 74 3b 0a 20 20 0a 20 20 20 20 2f 2a tinct;. . /*
20130 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 2e . ** SELECT .
20140 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 .. FROM (SELECT
20150 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f 46 46 53 ... LIMIT a OFFS
20160 45 54 20 62 29 20 4c 49 4d 49 54 20 78 20 4f 46 ET b) LIMIT x OF
20170 46 53 45 54 20 79 3b 0a 20 20 20 20 2a 2a 0a 20 FSET y;. **.
20180 20 20 20 2a 2a 20 4f 6e 65 20 69 73 20 74 65 6d ** One is tem
20190 70 74 65 64 20 74 6f 20 74 72 79 20 74 6f 20 61 pted to try to a
201a0 64 64 20 61 20 61 6e 64 20 62 20 74 6f 20 63 6f dd a and b to co
201b0 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d 69 74 73 mbine the limits
201c0 2e 20 20 42 75 74 20 74 68 69 73 0a 20 20 20 20 . But this.
201d0 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b ** does not work
201e0 20 69 66 20 65 69 74 68 65 72 20 6c 69 6d 69 74 if either limit
201f0 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 20 20 is negative..
20200 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 75 */. if( pSu
20210 62 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 b->pLimit ){.
20220 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 4c 69 6d pParent->pLim
20230 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c 69 6d 69 it = pSub->pLimi
20240 74 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e 70 t;. pSub->p
20250 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 7d Limit = 0;. }
20260 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69 61 . }.. /* Finia
20270 6c 6c 79 2c 20 64 65 6c 65 74 65 20 77 68 61 74 lly, delete what
20280 20 69 73 20 6c 65 66 74 20 6f 66 20 74 68 65 20 is left of the
20290 73 75 62 71 75 65 72 79 20 61 6e 64 20 72 65 74 subquery and ret
202a0 75 72 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 urn. ** success
202b0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 .. */. sqlite3
202c0 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c SelectDelete(db,
202d0 20 70 53 75 62 31 29 3b 0a 0a 23 69 66 20 53 45 pSub1);..#if SE
202e0 4c 45 43 54 54 52 41 43 45 5f 45 4e 41 42 4c 45 LECTTRACE_ENABLE
202f0 44 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 D. if( sqlite3S
20300 65 6c 65 63 74 54 72 61 63 65 20 26 20 30 78 31 electTrace & 0x1
20310 30 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 00 ){. sqlite
20320 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 41 66 3DebugPrintf("Af
20330 74 65 72 20 66 6c 61 74 74 65 6e 69 6e 67 3a 5c ter flattening:\
20340 6e 22 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 n");. sqlite3
20350 54 72 65 65 56 69 65 77 53 65 6c 65 63 74 28 30 TreeViewSelect(0
20360 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e , p, 0);. }.#en
20370 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b dif.. return 1;
20380 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 .}.#endif /* !de
20390 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
203a0 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 T_SUBQUERY) || !
203b0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
203c0 4d 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 2f 2a MIT_VIEW) */../*
203d0 0a 2a 2a 20 42 61 73 65 64 20 6f 6e 20 74 68 65 .** Based on the
203e0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 contents of the
203f0 20 41 67 67 49 6e 66 6f 20 73 74 72 75 63 74 75 AggInfo structu
20400 72 65 20 69 6e 64 69 63 61 74 65 64 20 62 79 20 re indicated by
20410 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 the first.** arg
20420 75 6d 65 6e 74 2c 20 74 68 69 73 20 66 75 6e 63 ument, this func
20430 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 74 tion checks if t
20440 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 he following are
20450 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 true:.**.**
20460 2a 20 74 68 65 20 71 75 65 72 79 20 63 6f 6e 74 * the query cont
20470 61 69 6e 73 20 6a 75 73 74 20 61 20 73 69 6e 67 ains just a sing
20480 6c 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e le aggregate fun
20490 63 74 69 6f 6e 2c 0a 2a 2a 20 20 20 20 2a 20 74 ction,.** * t
204a0 68 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e he aggregate fun
204b0 63 74 69 6f 6e 20 69 73 20 65 69 74 68 65 72 20 ction is either
204c0 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29 2c 20 min() or max(),
204d0 61 6e 64 0a 2a 2a 20 20 20 20 2a 20 74 68 65 20 and.** * the
204e0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 argument to the
204f0 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 aggregate functi
20500 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 76 on is a column v
20510 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 alue..**.** If a
20520 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 ll of the above
20530 61 72 65 20 74 72 75 65 2c 20 74 68 65 6e 20 57 are true, then W
20540 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e HERE_ORDERBY_MIN
20550 20 6f 72 20 57 48 45 52 45 5f 4f 52 44 45 52 42 or WHERE_ORDERB
20560 59 5f 4d 41 58 0a 2a 2a 20 69 73 20 72 65 74 75 Y_MAX.** is retu
20570 72 6e 65 64 20 61 73 20 61 70 70 72 6f 70 72 69 rned as appropri
20580 61 74 65 2e 20 41 6c 73 6f 2c 20 2a 70 70 4d 69 ate. Also, *ppMi
20590 6e 4d 61 78 20 69 73 20 73 65 74 20 74 6f 20 70 nMax is set to p
205a0 6f 69 6e 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 oint to the .**
205b0 6c 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 list of argument
205c0 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 s passed to the
205d0 61 67 67 72 65 67 61 74 65 20 62 65 66 6f 72 65 aggregate before
205e0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a returning..**.*
205f0 2a 20 4f 72 2c 20 69 66 20 74 68 65 20 63 6f 6e * Or, if the con
20600 64 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 61 72 ditions above ar
20610 65 20 6e 6f 74 20 6d 65 74 2c 20 2a 70 70 4d 69 e not met, *ppMi
20620 6e 4d 61 78 20 69 73 20 73 65 74 20 74 6f 20 30 nMax is set to 0
20630 20 61 6e 64 0a 2a 2a 20 57 48 45 52 45 5f 4f 52 and.** WHERE_OR
20640 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 69 73 20 DERBY_NORMAL is
20650 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 returned..*/.sta
20660 74 69 63 20 75 38 20 6d 69 6e 4d 61 78 51 75 65 tic u8 minMaxQue
20670 72 79 28 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 ry(AggInfo *pAgg
20680 49 6e 66 6f 2c 20 45 78 70 72 4c 69 73 74 20 2a Info, ExprList *
20690 2a 70 70 4d 69 6e 4d 61 78 29 7b 0a 20 20 69 6e *ppMinMax){. in
206a0 74 20 65 52 65 74 20 3d 20 57 48 45 52 45 5f 4f t eRet = WHERE_O
206b0 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 20 20 RDERBY_NORMAL;
206c0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 /* Retur
206d0 6e 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2a 70 n value */.. *p
206e0 70 4d 69 6e 4d 61 78 20 3d 20 30 3b 0a 20 20 69 pMinMax = 0;. i
206f0 66 28 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 f( pAggInfo->nFu
20700 6e 63 3d 3d 31 20 29 7b 0a 20 20 20 20 45 78 70 nc==1 ){. Exp
20710 72 20 2a 70 45 78 70 72 20 3d 20 70 41 67 67 49 r *pExpr = pAggI
20720 6e 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 2e 70 45 nfo->aFunc[0].pE
20730 78 70 72 3b 20 2f 2a 20 41 67 67 72 65 67 61 74 xpr; /* Aggregat
20740 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 e function */.
20750 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 ExprList *pELi
20760 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c st = pExpr->x.pL
20770 69 73 74 3b 20 20 20 20 20 20 2f 2a 20 41 72 67 ist; /* Arg
20780 75 6d 65 6e 74 73 20 74 6f 20 61 67 67 20 66 75 uments to agg fu
20790 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 20 20 61 nction */.. a
207a0 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 ssert( pExpr->op
207b0 3d 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f ==TK_AGG_FUNCTIO
207c0 4e 20 29 3b 0a 20 20 20 20 69 66 28 20 70 45 4c N );. if( pEL
207d0 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e ist && pEList->n
207e0 45 78 70 72 3d 3d 31 20 26 26 20 70 45 4c 69 73 Expr==1 && pELis
207f0 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f t->a[0].pExpr->o
20800 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e p==TK_AGG_COLUMN
20810 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 ){. const
20820 63 68 61 72 20 2a 7a 46 75 6e 63 20 3d 20 70 45 char *zFunc = pE
20830 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 xpr->u.zToken;.
20840 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 if( sqlite3
20850 53 74 72 49 43 6d 70 28 7a 46 75 6e 63 2c 20 22 StrICmp(zFunc, "
20860 6d 69 6e 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 min")==0 ){.
20870 20 20 20 20 65 52 65 74 20 3d 20 57 48 45 52 45 eRet = WHERE
20880 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3b 0a 20 20 _ORDERBY_MIN;.
20890 20 20 20 20 20 20 2a 70 70 4d 69 6e 4d 61 78 20 *ppMinMax
208a0 3d 20 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20 = pEList;.
208b0 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 }else if( sqlite
208c0 33 53 74 72 49 43 6d 70 28 7a 46 75 6e 63 2c 20 3StrICmp(zFunc,
208d0 22 6d 61 78 22 29 3d 3d 30 20 29 7b 0a 20 20 20 "max")==0 ){.
208e0 20 20 20 20 20 65 52 65 74 20 3d 20 57 48 45 52 eRet = WHER
208f0 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 3b 0a 20 E_ORDERBY_MAX;.
20900 20 20 20 20 20 20 20 2a 70 70 4d 69 6e 4d 61 78 *ppMinMax
20910 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 = pEList;.
20920 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 }. }. }..
20930 61 73 73 65 72 74 28 20 2a 70 70 4d 69 6e 4d 61 assert( *ppMinMa
20940 78 3d 3d 30 20 7c 7c 20 28 2a 70 70 4d 69 6e 4d x==0 || (*ppMinM
20950 61 78 29 2d 3e 6e 45 78 70 72 3d 3d 31 20 29 3b ax)->nExpr==1 );
20960 0a 20 20 72 65 74 75 72 6e 20 65 52 65 74 3b 0a . return eRet;.
20970 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 6c }../*.** The sel
20980 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 70 61 ect statement pa
20990 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 ssed as the firs
209a0 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e t argument is an
209b0 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79 aggregate query
209c0 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 ..** The second
209d0 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 argument is the
209e0 61 73 73 6f 63 69 61 74 65 64 20 61 67 67 72 65 associated aggre
209f0 67 61 74 65 2d 69 6e 66 6f 20 6f 62 6a 65 63 74 gate-info object
20a00 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 . This .** funct
20a10 69 6f 6e 20 74 65 73 74 73 20 69 66 20 74 68 65 ion tests if the
20a20 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74 68 SELECT is of th
20a30 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 e form:.**.**
20a40 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20 SELECT count(*)
20a50 46 52 4f 4d 20 3c 74 62 6c 3e 0a 2a 2a 0a 2a 2a FROM <tbl>.**.**
20a60 20 77 68 65 72 65 20 74 61 62 6c 65 20 69 73 20 where table is
20a70 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 a database table
20a80 2c 20 6e 6f 74 20 61 20 73 75 62 2d 73 65 6c 65 , not a sub-sele
20a90 63 74 20 6f 72 20 76 69 65 77 2e 20 49 66 20 74 ct or view. If t
20aa0 68 65 20 71 75 65 72 79 0a 2a 2a 20 64 6f 65 73 he query.** does
20ab0 20 6d 61 74 63 68 20 74 68 69 73 20 70 61 74 74 match this patt
20ac0 65 72 6e 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e ern, then a poin
20ad0 74 65 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65 ter to the Table
20ae0 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e object represen
20af0 74 69 6e 67 0a 2a 2a 20 3c 74 62 6c 3e 20 69 73 ting.** <tbl> is
20b00 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 returned. Other
20b10 77 69 73 65 2c 20 30 20 69 73 20 72 65 74 75 72 wise, 0 is retur
20b20 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 ned..*/.static T
20b30 61 62 6c 65 20 2a 69 73 53 69 6d 70 6c 65 43 6f able *isSimpleCo
20b40 75 6e 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 41 unt(Select *p, A
20b50 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f ggInfo *pAggInfo
20b60 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 ){. Table *pTab
20b70 3b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b ;. Expr *pExpr;
20b80 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e .. assert( !p->
20b90 70 47 72 6f 75 70 42 79 20 29 3b 0a 0a 20 20 69 pGroupBy );.. i
20ba0 66 28 20 70 2d 3e 70 57 68 65 72 65 20 7c 7c 20 f( p->pWhere ||
20bb0 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 p->pEList->nExpr
20bc0 21 3d 31 20 0a 20 20 20 7c 7c 20 70 2d 3e 70 53 !=1 . || p->pS
20bd0 72 63 2d 3e 6e 53 72 63 21 3d 31 20 7c 7c 20 70 rc->nSrc!=1 || p
20be0 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 ->pSrc->a[0].pSe
20bf0 6c 65 63 74 0a 20 20 29 7b 0a 20 20 20 20 72 65 lect. ){. re
20c00 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 54 turn 0;. }. pT
20c10 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b ab = p->pSrc->a[
20c20 30 5d 2e 70 54 61 62 3b 0a 20 20 70 45 78 70 72 0].pTab;. pExpr
20c30 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b = p->pEList->a[
20c40 30 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73 73 65 0].pExpr;. asse
20c50 72 74 28 20 70 54 61 62 20 26 26 20 21 70 54 61 rt( pTab && !pTa
20c60 62 2d 3e 70 53 65 6c 65 63 74 20 26 26 20 70 45 b->pSelect && pE
20c70 78 70 72 20 29 3b 0a 0a 20 20 69 66 28 20 49 73 xpr );.. if( Is
20c80 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 Virtual(pTab) )
20c90 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 return 0;. if(
20ca0 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 pExpr->op!=TK_AG
20cb0 47 5f 46 55 4e 43 54 49 4f 4e 20 29 20 72 65 74 G_FUNCTION ) ret
20cc0 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 4e 45 56 urn 0;. if( NEV
20cd0 45 52 28 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 ER(pAggInfo->nFu
20ce0 6e 63 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 nc==0) ) return
20cf0 30 3b 0a 20 20 69 66 28 20 28 70 41 67 67 49 6e 0;. if( (pAggIn
20d00 66 6f 2d 3e 61 46 75 6e 63 5b 30 5d 2e 70 46 75 fo->aFunc[0].pFu
20d10 6e 63 2d 3e 66 75 6e 63 46 6c 61 67 73 26 53 51 nc->funcFlags&SQ
20d20 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 29 LITE_FUNC_COUNT)
20d30 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a ==0 ) return 0;.
20d40 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 if( pExpr->fla
20d50 67 73 26 45 50 5f 44 69 73 74 69 6e 63 74 20 29 gs&EP_Distinct )
20d60 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 72 65 return 0;.. re
20d70 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a turn pTab;.}../*
20d80 0a 2a 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 .** If the sourc
20d90 65 2d 6c 69 73 74 20 69 74 65 6d 20 70 61 73 73 e-list item pass
20da0 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e ed as an argumen
20db0 74 20 77 61 73 20 61 75 67 6d 65 6e 74 65 64 20 t was augmented
20dc0 77 69 74 68 20 61 6e 0a 2a 2a 20 49 4e 44 45 58 with an.** INDEX
20dd0 45 44 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 ED BY clause, th
20de0 65 6e 20 74 72 79 20 74 6f 20 6c 6f 63 61 74 65 en try to locate
20df0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 69 the specified i
20e00 6e 64 65 78 2e 20 49 66 20 74 68 65 72 65 0a 2a ndex. If there.*
20e10 2a 20 77 61 73 20 73 75 63 68 20 61 20 63 6c 61 * was such a cla
20e20 75 73 65 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 use and the name
20e30 64 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62 d index cannot b
20e40 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 e found, return
20e50 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 .** SQLITE_ERROR
20e60 20 61 6e 64 20 6c 65 61 76 65 20 61 6e 20 65 72 and leave an er
20e70 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e 20 4f ror in pParse. O
20e80 74 68 65 72 77 69 73 65 2c 20 70 6f 70 75 6c 61 therwise, popula
20e90 74 65 20 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 70 49 te .** pFrom->pI
20ea0 6e 64 65 78 20 61 6e 64 20 72 65 74 75 72 6e 20 ndex and return
20eb0 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 69 6e SQLITE_OK..*/.in
20ec0 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 t sqlite3Indexed
20ed0 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20 2a ByLookup(Parse *
20ee0 70 50 61 72 73 65 2c 20 73 74 72 75 63 74 20 53 pParse, struct S
20ef0 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 rcList_item *pFr
20f00 6f 6d 29 7b 0a 20 20 69 66 28 20 70 46 72 6f 6d om){. if( pFrom
20f10 2d 3e 70 54 61 62 20 26 26 20 70 46 72 6f 6d 2d ->pTab && pFrom-
20f20 3e 7a 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 54 >zIndex ){. T
20f30 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 able *pTab = pFr
20f40 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 63 68 om->pTab;. ch
20f50 61 72 20 2a 7a 49 6e 64 65 78 20 3d 20 70 46 72 ar *zIndex = pFr
20f60 6f 6d 2d 3e 7a 49 6e 64 65 78 3b 0a 20 20 20 20 om->zIndex;.
20f70 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 Index *pIdx;.
20f80 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e for(pIdx=pTab->
20f90 70 49 6e 64 65 78 3b 20 0a 20 20 20 20 20 20 20 pIndex; .
20fa0 20 70 49 64 78 20 26 26 20 73 71 6c 69 74 65 33 pIdx && sqlite3
20fb0 53 74 72 49 43 6d 70 28 70 49 64 78 2d 3e 7a 4e StrICmp(pIdx->zN
20fc0 61 6d 65 2c 20 7a 49 6e 64 65 78 29 3b 20 0a 20 ame, zIndex); .
20fd0 20 20 20 20 20 20 20 70 49 64 78 3d 70 49 64 78 pIdx=pIdx
20fe0 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 3b 0a 20 ->pNext. );.
20ff0 20 20 20 69 66 28 20 21 70 49 64 78 20 29 7b 0a if( !pIdx ){.
21000 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
21010 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e orMsg(pParse, "n
21020 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25 73 o such index: %s
21030 22 2c 20 7a 49 6e 64 65 78 2c 20 30 29 3b 0a 20 ", zIndex, 0);.
21040 20 20 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65 pParse->che
21050 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20 ckSchema = 1;.
21060 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 return SQLIT
21070 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 E_ERROR;. }.
21080 20 20 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 pFrom->pIndex
21090 20 3d 20 70 49 64 78 3b 0a 20 20 7d 0a 20 20 72 = pIdx;. }. r
210a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b eturn SQLITE_OK;
210b0 0a 7d 0a 2f 2a 0a 2a 2a 20 44 65 74 65 63 74 20 .}./*.** Detect
210c0 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 compound SELECT
210d0 73 74 61 74 65 6d 65 6e 74 73 20 74 68 61 74 20 statements that
210e0 75 73 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 use an ORDER BY
210f0 63 6c 61 75 73 65 20 77 69 74 68 20 0a 2a 2a 20 clause with .**
21100 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 63 an alternative c
21110 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
21120 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45 e..**.** SELE
21130 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 45 CT ... FROM t1 E
21140 58 43 45 50 54 20 53 45 4c 45 43 54 20 2e 2e 2e XCEPT SELECT ...
21150 20 46 52 4f 4d 20 74 32 20 4f 52 44 45 52 20 42 FROM t2 ORDER B
21160 59 20 2e 2e 20 43 4f 4c 4c 41 54 45 20 2e 2e 2e Y .. COLLATE ...
21170 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 .**.** These are
21180 20 72 65 77 72 69 74 74 65 6e 20 61 73 20 61 20 rewritten as a
21190 73 75 62 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20 subquery:.**.**
211a0 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d SELECT * FROM
211b0 20 28 53 45 4c 45 43 54 20 2e 2e 2e 20 46 52 4f (SELECT ... FRO
211c0 4d 20 74 31 20 45 58 43 45 50 54 20 53 45 4c 45 M t1 EXCEPT SELE
211d0 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 74 32 29 0a CT ... FROM t2).
211e0 2a 2a 20 20 20 20 20 4f 52 44 45 52 20 42 59 20 ** ORDER BY
211f0 2e 2e 2e 20 43 4f 4c 4c 41 54 45 20 2e 2e 2e 0a ... COLLATE ....
21200 2a 2a 0a 2a 2a 20 54 68 69 73 20 74 72 61 6e 73 **.** This trans
21210 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6e 65 63 formation is nec
21220 65 73 73 61 72 79 20 62 65 63 61 75 73 65 20 74 essary because t
21230 68 65 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 he multiSelectOr
21240 64 65 72 42 79 28 29 20 72 6f 75 74 69 6e 65 0a derBy() routine.
21250 2a 2a 20 61 62 6f 76 65 20 74 68 61 74 20 67 65 ** above that ge
21260 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 nerates the code
21270 20 66 6f 72 20 61 20 63 6f 6d 70 6f 75 6e 64 20 for a compound
21280 53 45 4c 45 43 54 20 77 69 74 68 20 61 6e 20 4f SELECT with an O
21290 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 0a 2a RDER BY clause.*
212a0 2a 20 75 73 65 73 20 61 20 6d 65 72 67 65 20 61 * uses a merge a
212b0 6c 67 6f 72 69 74 68 6d 20 74 68 61 74 20 72 65 lgorithm that re
212c0 71 75 69 72 65 73 20 74 68 65 20 73 61 6d 65 20 quires the same
212d0 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e collating sequen
212e0 63 65 20 6f 6e 20 74 68 65 0a 2a 2a 20 72 65 73 ce on the.** res
212f0 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 61 73 20 6f ult columns as o
21300 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 n the ORDER BY c
21310 6c 61 75 73 65 2e 20 20 53 65 65 20 74 69 63 6b lause. See tick
21320 65 74 0a 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77 et.** http://www
21330 2e 73 71 6c 69 74 65 2e 6f 72 67 2f 73 72 63 2f .sqlite.org/src/
21340 69 6e 66 6f 2f 36 37 30 39 35 37 34 64 32 61 0a info/6709574d2a.
21350 2a 2a 0a 2a 2a 20 54 68 69 73 20 74 72 61 6e 73 **.** This trans
21360 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 6f 6e 6c formation is onl
21370 79 20 6e 65 65 64 65 64 20 66 6f 72 20 45 58 43 y needed for EXC
21380 45 50 54 2c 20 49 4e 54 45 52 53 45 43 54 2c 20 EPT, INTERSECT,
21390 61 6e 64 20 55 4e 49 4f 4e 2e 0a 2a 2a 20 54 68 and UNION..** Th
213a0 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72 e UNION ALL oper
213b0 61 74 6f 72 20 77 6f 72 6b 73 20 66 69 6e 65 20 ator works fine
213c0 77 69 74 68 20 6d 75 6c 74 69 53 65 6c 65 63 74 with multiSelect
213d0 4f 72 64 65 72 42 79 28 29 20 65 76 65 6e 20 77 OrderBy() even w
213e0 68 65 6e 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 hen.** there are
213f0 20 43 4f 4c 4c 41 54 45 20 74 65 72 6d 73 20 69 COLLATE terms i
21400 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 2e 0a n the ORDER BY..
21410 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f */.static int co
21420 6e 76 65 72 74 43 6f 6d 70 6f 75 6e 64 53 65 6c nvertCompoundSel
21430 65 63 74 54 6f 53 75 62 71 75 65 72 79 28 57 61 ectToSubquery(Wa
21440 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 lker *pWalker, S
21450 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 elect *p){. int
21460 20 69 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e i;. Select *pN
21470 65 77 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 58 ew;. Select *pX
21480 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b ;. sqlite3 *db;
21490 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 . struct ExprLi
214a0 73 74 5f 69 74 65 6d 20 2a 61 3b 0a 20 20 53 72 st_item *a;. Sr
214b0 63 4c 69 73 74 20 2a 70 4e 65 77 53 72 63 3b 0a cList *pNewSrc;.
214c0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b Parse *pParse;
214d0 0a 20 20 54 6f 6b 65 6e 20 64 75 6d 6d 79 3b 0a . Token dummy;.
214e0 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 . if( p->pPrior
214f0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 ==0 ) return WRC
21500 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 69 66 28 _Continue;. if(
21510 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 p->pOrderBy==0
21520 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e ) return WRC_Con
21530 74 69 6e 75 65 3b 0a 20 20 66 6f 72 28 70 58 3d tinue;. for(pX=
21540 70 3b 20 70 58 20 26 26 20 28 70 58 2d 3e 6f 70 p; pX && (pX->op
21550 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 70 58 2d 3e ==TK_ALL || pX->
21560 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 29 3b 20 op==TK_SELECT);
21570 70 58 3d 70 58 2d 3e 70 50 72 69 6f 72 29 7b 7d pX=pX->pPrior){}
21580 0a 20 20 69 66 28 20 70 58 3d 3d 30 20 29 20 72 . if( pX==0 ) r
21590 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
215a0 75 65 3b 0a 20 20 61 20 3d 20 70 2d 3e 70 4f 72 ue;. a = p->pOr
215b0 64 65 72 42 79 2d 3e 61 3b 0a 20 20 66 6f 72 28 derBy->a;. for(
215c0 69 3d 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 6e i=p->pOrderBy->n
215d0 45 78 70 72 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d Expr-1; i>=0; i-
215e0 2d 29 7b 0a 20 20 20 20 69 66 28 20 61 5b 69 5d -){. if( a[i]
215f0 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 .pExpr->flags &
21600 45 50 5f 43 6f 6c 6c 61 74 65 20 29 20 62 72 65 EP_Collate ) bre
21610 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c ak;. }. if( i<
21620 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 0 ) return WRC_C
21630 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 2f 2a 20 49 ontinue;.. /* I
21640 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 f we reach this
21650 70 6f 69 6e 74 2c 20 74 68 61 74 20 6d 65 61 6e point, that mean
21660 73 20 74 68 65 20 74 72 61 6e 73 66 6f 72 6d 61 s the transforma
21670 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 tion is required
21680 2e 20 2a 2f 0a 0a 20 20 70 50 61 72 73 65 20 3d . */.. pParse =
21690 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 pWalker->pParse
216a0 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d ;. db = pParse-
216b0 3e 64 62 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 >db;. pNew = sq
216c0 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 lite3DbMallocZer
216d0 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e o(db, sizeof(*pN
216e0 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 ew) );. if( pNe
216f0 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 w==0 ) return WR
21700 43 5f 41 62 6f 72 74 3b 0a 20 20 6d 65 6d 73 65 C_Abort;. memse
21710 74 28 26 64 75 6d 6d 79 2c 20 30 2c 20 73 69 7a t(&dummy, 0, siz
21720 65 6f 66 28 64 75 6d 6d 79 29 29 3b 0a 20 20 70 eof(dummy));. p
21730 4e 65 77 53 72 63 20 3d 20 73 71 6c 69 74 65 33 NewSrc = sqlite3
21740 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f SrcListAppendFro
21750 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 30 2c 30 mTerm(pParse,0,0
21760 2c 30 2c 26 64 75 6d 6d 79 2c 70 4e 65 77 2c 30 ,0,&dummy,pNew,0
21770 2c 30 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 53 ,0);. if( pNewS
21780 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 rc==0 ) return W
21790 52 43 5f 41 62 6f 72 74 3b 0a 20 20 2a 70 4e 65 RC_Abort;. *pNe
217a0 77 20 3d 20 2a 70 3b 0a 20 20 70 2d 3e 70 53 72 w = *p;. p->pSr
217b0 63 20 3d 20 70 4e 65 77 53 72 63 3b 0a 20 20 70 c = pNewSrc;. p
217c0 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 ->pEList = sqlit
217d0 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 e3ExprListAppend
217e0 28 70 50 61 72 73 65 2c 20 30 2c 20 73 71 6c 69 (pParse, 0, sqli
217f0 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41 te3Expr(db, TK_A
21800 4c 4c 2c 20 30 29 29 3b 0a 20 20 70 2d 3e 6f 70 LL, 0));. p->op
21810 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20 20 = TK_SELECT;.
21820 70 2d 3e 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 p->pWhere = 0;.
21830 20 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 pNew->pGroupBy
21840 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 = 0;. pNew->pHa
21850 76 69 6e 67 20 3d 20 30 3b 0a 20 20 70 4e 65 77 ving = 0;. pNew
21860 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a ->pOrderBy = 0;.
21870 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b p->pPrior = 0;
21880 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 30 3b . p->pNext = 0;
21890 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 . p->selFlags &
218a0 3d 20 7e 53 46 5f 43 6f 6d 70 6f 75 6e 64 3b 0a = ~SF_Compound;.
218b0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e assert( pNew->
218c0 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 pPrior!=0 );. p
218d0 4e 65 77 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 New->pPrior->pNe
218e0 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 70 4e 65 xt = pNew;. pNe
218f0 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 w->pLimit = 0;.
21900 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d pNew->pOffset =
21910 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 0;. return WRC
21920 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 23 69 _Continue;.}..#i
21930 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
21940 54 5f 43 54 45 0a 2f 2a 0a 2a 2a 20 41 72 67 75 T_CTE./*.** Argu
21950 6d 65 6e 74 20 70 57 69 74 68 20 28 77 68 69 63 ment pWith (whic
21960 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 70 h may be NULL) p
21970 6f 69 6e 74 73 20 74 6f 20 61 20 6c 69 6e 6b 65 oints to a linke
21980 64 20 6c 69 73 74 20 6f 66 20 6e 65 73 74 65 64 d list of nested
21990 20 0a 2a 2a 20 57 49 54 48 20 63 6f 6e 74 65 78 .** WITH contex
219a0 74 73 2c 20 66 72 6f 6d 20 69 6e 6e 65 72 20 74 ts, from inner t
219b0 6f 20 6f 75 74 65 72 6d 6f 73 74 2e 20 49 66 20 o outermost. If
219c0 74 68 65 20 74 61 62 6c 65 20 69 64 65 6e 74 69 the table identi
219d0 66 69 65 64 20 62 79 20 0a 2a 2a 20 46 52 4f 4d fied by .** FROM
219e0 20 63 6c 61 75 73 65 20 65 6c 65 6d 65 6e 74 20 clause element
219f0 70 49 74 65 6d 20 69 73 20 72 65 61 6c 6c 79 20 pItem is really
21a00 61 20 63 6f 6d 6d 6f 6e 2d 74 61 62 6c 65 2d 65 a common-table-e
21a10 78 70 72 65 73 73 69 6f 6e 20 28 43 54 45 29 20 xpression (CTE)
21a20 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20 .** then return
21a30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 a pointer to the
21a40 20 43 54 45 20 64 65 66 69 6e 69 74 69 6f 6e 20 CTE definition
21a50 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 20 for that table.
21a60 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74 Otherwise.** ret
21a70 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 urn NULL..**.**
21a80 49 66 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 If a non-NULL va
21a90 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2c lue is returned,
21aa0 20 73 65 74 20 2a 70 70 43 6f 6e 74 65 78 74 20 set *ppContext
21ab0 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 to point to the
21ac0 57 69 74 68 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 With.** object t
21ad0 68 61 74 20 74 68 65 20 72 65 74 75 72 6e 65 64 hat the returned
21ae0 20 43 54 45 20 62 65 6c 6f 6e 67 73 20 74 6f 2e CTE belongs to.
21af0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 .*/.static struc
21b00 74 20 43 74 65 20 2a 73 65 61 72 63 68 57 69 74 t Cte *searchWit
21b10 68 28 0a 20 20 57 69 74 68 20 2a 70 57 69 74 68 h(. With *pWith
21b20 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ,
21b30 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 /* Current
21b40 6f 75 74 65 72 6d 6f 73 74 20 57 49 54 48 20 63 outermost WITH c
21b50 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 lause */. struc
21b60 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a t SrcList_item *
21b70 70 49 74 65 6d 2c 20 20 20 20 20 2f 2a 20 46 52 pItem, /* FR
21b80 4f 4d 20 63 6c 61 75 73 65 20 65 6c 65 6d 65 6e OM clause elemen
21b90 74 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a t to resolve */.
21ba0 20 20 57 69 74 68 20 2a 2a 70 70 43 6f 6e 74 65 With **ppConte
21bb0 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 xt
21bc0 20 20 2f 2a 20 4f 55 54 3a 20 57 49 54 48 20 63 /* OUT: WITH c
21bd0 6c 61 75 73 65 20 72 65 74 75 72 6e 20 76 61 6c lause return val
21be0 75 65 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f ue belongs to */
21bf0 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 .){. const char
21c00 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 70 *zName;. if( p
21c10 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 3d Item->zDatabase=
21c20 3d 30 20 26 26 20 28 7a 4e 61 6d 65 20 3d 20 70 =0 && (zName = p
21c30 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 Item->zName)!=0
21c40 29 7b 0a 20 20 20 20 57 69 74 68 20 2a 70 3b 0a ){. With *p;.
21c50 20 20 20 20 66 6f 72 28 70 3d 70 57 69 74 68 3b for(p=pWith;
21c60 20 70 3b 20 70 3d 70 2d 3e 70 4f 75 74 65 72 29 p; p=p->pOuter)
21c70 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 {. int i;.
21c80 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c for(i=0; i<
21c90 70 2d 3e 6e 43 74 65 3b 20 69 2b 2b 29 7b 0a 20 p->nCte; i++){.
21ca0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 if( sqlit
21cb0 65 33 53 74 72 49 43 6d 70 28 7a 4e 61 6d 65 2c e3StrICmp(zName,
21cc0 20 70 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d p->a[i].zName)=
21cd0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 =0 ){.
21ce0 2a 70 70 43 6f 6e 74 65 78 74 20 3d 20 70 3b 0a *ppContext = p;.
21cf0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e return
21d00 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 &p->a[i];.
21d10 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 }. }.
21d20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 }. }. return
21d30 30 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 63 6f 64 0;.}../* The cod
21d40 65 20 67 65 6e 65 72 61 74 6f 72 20 6d 61 69 6e e generator main
21d50 74 61 69 6e 73 20 61 20 73 74 61 63 6b 20 6f 66 tains a stack of
21d60 20 61 63 74 69 76 65 20 57 49 54 48 20 63 6c 61 active WITH cla
21d70 75 73 65 73 0a 2a 2a 20 77 69 74 68 20 74 68 65 uses.** with the
21d80 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 57 49 54 48 inner-most WITH
21d90 20 63 6c 61 75 73 65 20 62 65 69 6e 67 20 61 74 clause being at
21da0 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 the top of the
21db0 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 stack..**.** Thi
21dc0 73 20 72 6f 75 74 69 6e 65 20 70 75 73 68 65 73 s routine pushes
21dd0 20 74 68 65 20 57 49 54 48 20 63 6c 61 75 73 65 the WITH clause
21de0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 passed as the s
21df0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a econd argument.*
21e00 2a 20 6f 6e 74 6f 20 74 68 65 20 74 6f 70 20 6f * onto the top o
21e10 66 20 74 68 65 20 73 74 61 63 6b 2e 20 49 66 20 f the stack. If
21e20 61 72 67 75 6d 65 6e 74 20 62 46 72 65 65 20 69 argument bFree i
21e30 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 s true, then thi
21e40 73 0a 2a 2a 20 57 49 54 48 20 63 6c 61 75 73 65 s.** WITH clause
21e50 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 70 will never be p
21e60 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 opped from the s
21e70 74 61 63 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 tack. In this ca
21e80 73 65 20 69 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 se it.** should
21e90 62 65 20 66 72 65 65 64 20 61 6c 6f 6e 67 20 77 be freed along w
21ea0 69 74 68 20 74 68 65 20 50 61 72 73 65 20 6f 62 ith the Parse ob
21eb0 6a 65 63 74 2e 20 49 6e 20 6f 74 68 65 72 20 63 ject. In other c
21ec0 61 73 65 73 2c 20 77 68 65 6e 0a 2a 2a 20 62 46 ases, when.** bF
21ed0 72 65 65 3d 3d 30 2c 20 74 68 65 20 57 69 74 68 ree==0, the With
21ee0 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 20 object will be
21ef0 66 72 65 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 freed along with
21f00 20 74 68 65 20 53 45 4c 45 43 54 20 0a 2a 2a 20 the SELECT .**
21f10 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 77 statement with w
21f20 68 69 63 68 20 69 74 20 69 73 20 61 73 73 6f 63 hich it is assoc
21f30 69 61 74 65 64 2e 0a 2a 2f 0a 76 6f 69 64 20 73 iated..*/.void s
21f40 71 6c 69 74 65 33 57 69 74 68 50 75 73 68 28 50 qlite3WithPush(P
21f50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 57 69 arse *pParse, Wi
21f60 74 68 20 2a 70 57 69 74 68 2c 20 75 38 20 62 46 th *pWith, u8 bF
21f70 72 65 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 ree){. assert(
21f80 62 46 72 65 65 3d 3d 30 20 7c 7c 20 70 50 61 72 bFree==0 || pPar
21f90 73 65 2d 3e 70 57 69 74 68 3d 3d 30 20 29 3b 0a se->pWith==0 );.
21fa0 20 20 69 66 28 20 70 57 69 74 68 20 29 7b 0a 20 if( pWith ){.
21fb0 20 20 20 70 57 69 74 68 2d 3e 70 4f 75 74 65 72 pWith->pOuter
21fc0 20 3d 20 70 50 61 72 73 65 2d 3e 70 57 69 74 68 = pParse->pWith
21fd0 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 57 ;. pParse->pW
21fe0 69 74 68 20 3d 20 70 57 69 74 68 3b 0a 20 20 20 ith = pWith;.
21ff0 20 70 50 61 72 73 65 2d 3e 62 46 72 65 65 57 69 pParse->bFreeWi
22000 74 68 20 3d 20 62 46 72 65 65 3b 0a 20 20 7d 0a th = bFree;. }.
22010 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 }../*.** This fu
22020 6e 63 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 nction checks if
22030 20 61 72 67 75 6d 65 6e 74 20 70 46 72 6f 6d 20 argument pFrom
22040 72 65 66 65 72 73 20 74 6f 20 61 20 43 54 45 20 refers to a CTE
22050 64 65 63 6c 61 72 65 64 20 62 79 20 0a 2a 2a 20 declared by .**
22060 61 20 57 49 54 48 20 63 6c 61 75 73 65 20 6f 6e a WITH clause on
22070 20 74 68 65 20 73 74 61 63 6b 20 63 75 72 72 65 the stack curre
22080 6e 74 6c 79 20 6d 61 69 6e 74 61 69 6e 65 64 20 ntly maintained
22090 62 79 20 74 68 65 20 70 61 72 73 65 72 2e 20 41 by the parser. A
220a0 6e 64 2c 0a 2a 2a 20 69 66 20 63 75 72 72 65 6e nd,.** if curren
220b0 74 6c 79 20 70 72 6f 63 65 73 73 69 6e 67 20 61 tly processing a
220c0 20 43 54 45 20 65 78 70 72 65 73 73 69 6f 6e 2c CTE expression,
220d0 20 69 66 20 69 74 20 69 73 20 61 20 72 65 63 75 if it is a recu
220e0 72 73 69 76 65 0a 2a 2a 20 72 65 66 65 72 65 6e rsive.** referen
220f0 63 65 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e ce to the curren
22100 74 20 43 54 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 t CTE..**.** If
22110 70 46 72 6f 6d 20 66 61 6c 6c 73 20 69 6e 74 6f pFrom falls into
22120 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 74 either of the t
22130 77 6f 20 63 61 74 65 67 6f 72 69 65 73 20 61 62 wo categories ab
22140 6f 76 65 2c 20 70 46 72 6f 6d 2d 3e 70 54 61 62 ove, pFrom->pTab
22150 0a 2a 2a 20 61 6e 64 20 6f 74 68 65 72 20 66 69 .** and other fi
22160 65 6c 64 73 20 61 72 65 20 70 6f 70 75 6c 61 74 elds are populat
22170 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20 ed accordingly.
22180 54 68 65 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c The caller shoul
22190 64 20 63 68 65 63 6b 0a 2a 2a 20 28 70 46 72 6f d check.** (pFro
221a0 6d 2d 3e 70 54 61 62 21 3d 30 29 20 74 6f 20 64 m->pTab!=0) to d
221b0 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 etermine whether
221c0 20 6f 72 20 6e 6f 74 20 61 20 73 75 63 63 65 73 or not a succes
221d0 73 66 75 6c 20 6d 61 74 63 68 0a 2a 2a 20 77 61 sful match.** wa
221e0 73 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 57 s found..**.** W
221f0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 hether or not a
22200 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 match is found,
22210 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 SQLITE_OK is ret
22220 75 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f urned if no erro
22230 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 49 66 20 r.** occurs. If
22240 61 6e 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 an error does oc
22250 63 75 72 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 cur, an error me
22260 73 73 61 67 65 20 69 73 20 73 74 6f 72 65 64 20 ssage is stored
22270 69 6e 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 72 in the.** parser
22280 20 61 6e 64 20 73 6f 6d 65 20 65 72 72 6f 72 20 and some error
22290 63 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20 code other than
222a0 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e SQLITE_OK return
222b0 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e ed..*/.static in
222c0 74 20 77 69 74 68 45 78 70 61 6e 64 28 0a 20 20 t withExpand(.
222d0 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
222e0 20 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 . struct SrcLi
222f0 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 0a 29 st_item *pFrom.)
22300 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 {. Parse *pPars
22310 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61 e = pWalker->pPa
22320 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a rse;. sqlite3 *
22330 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b db = pParse->db;
22340 0a 20 20 73 74 72 75 63 74 20 43 74 65 20 2a 70 . struct Cte *p
22350 43 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 Cte;
22360 20 20 20 2f 2a 20 4d 61 74 63 68 65 64 20 43 54 /* Matched CT
22370 45 20 28 6f 72 20 4e 55 4c 4c 20 69 66 20 6e 6f E (or NULL if no
22380 20 6d 61 74 63 68 29 20 2a 2f 0a 20 20 57 69 74 match) */. Wit
22390 68 20 2a 70 57 69 74 68 3b 20 20 20 20 20 20 20 h *pWith;
223a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 /*
223b0 57 49 54 48 20 63 6c 61 75 73 65 20 74 68 61 74 WITH clause that
223c0 20 70 43 74 65 20 62 65 6c 6f 6e 67 73 20 74 6f pCte belongs to
223d0 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 */.. assert( p
223e0 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b From->pTab==0 );
223f0 0a 0a 20 20 70 43 74 65 20 3d 20 73 65 61 72 63 .. pCte = searc
22400 68 57 69 74 68 28 70 50 61 72 73 65 2d 3e 70 57 hWith(pParse->pW
22410 69 74 68 2c 20 70 46 72 6f 6d 2c 20 26 70 57 69 ith, pFrom, &pWi
22420 74 68 29 3b 0a 20 20 69 66 28 20 70 43 74 65 20 th);. if( pCte
22430 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 ){. Table *pT
22440 61 62 3b 0a 20 20 20 20 45 78 70 72 4c 69 73 74 ab;. ExprList
22450 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20 53 65 *pEList;. Se
22460 6c 65 63 74 20 2a 70 53 65 6c 3b 0a 20 20 20 20 lect *pSel;.
22470 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 3b 20 20 Select *pLeft;
22480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a /*
22490 20 4c 65 66 74 2d 6d 6f 73 74 20 53 45 4c 45 43 Left-most SELEC
224a0 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 T statement */.
224b0 20 20 20 69 6e 74 20 62 4d 61 79 52 65 63 75 72 int bMayRecur
224c0 73 69 76 65 3b 20 20 20 20 20 20 20 20 20 20 20 sive;
224d0 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 6d 70 /* True if comp
224e0 6f 75 6e 64 20 6a 6f 69 6e 65 64 20 62 79 20 55 ound joined by U
224f0 4e 49 4f 4e 20 5b 41 4c 4c 5d 20 2a 2f 0a 20 20 NION [ALL] */.
22500 20 20 57 69 74 68 20 2a 70 53 61 76 65 64 57 69 With *pSavedWi
22510 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 th;
22520 2f 2a 20 49 6e 69 74 69 61 6c 20 76 61 6c 75 65 /* Initial value
22530 20 6f 66 20 70 50 61 72 73 65 2d 3e 70 57 69 74 of pParse->pWit
22540 68 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20 h */.. /* If
22550 70 43 74 65 2d 3e 7a 45 72 72 20 69 73 20 6e 6f pCte->zErr is no
22560 6e 2d 4e 55 4c 4c 20 61 74 20 74 68 69 73 20 70 n-NULL at this p
22570 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 oint, then this
22580 69 73 20 61 6e 20 69 6c 6c 65 67 61 6c 0a 20 20 is an illegal.
22590 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20 72 ** recursive r
225a0 65 66 65 72 65 6e 63 65 20 74 6f 20 43 54 45 20 eference to CTE
225b0 70 43 74 65 2e 20 4c 65 61 76 65 20 61 6e 20 65 pCte. Leave an e
225c0 72 72 6f 72 20 69 6e 20 70 50 61 72 73 65 20 61 rror in pParse a
225d0 6e 64 20 72 65 74 75 72 6e 0a 20 20 20 20 2a 2a nd return. **
225e0 20 65 61 72 6c 79 2e 20 49 66 20 70 43 74 65 2d early. If pCte-
225f0 3e 7a 45 72 72 20 69 73 20 4e 55 4c 4c 2c 20 74 >zErr is NULL, t
22600 68 65 6e 20 74 68 69 73 20 69 73 20 6e 6f 74 20 hen this is not
22610 61 20 72 65 63 75 72 73 69 76 65 20 72 65 66 65 a recursive refe
22620 72 65 6e 63 65 2e 0a 20 20 20 20 2a 2a 20 49 6e rence.. ** In
22630 20 74 68 69 73 20 63 61 73 65 2c 20 70 72 6f 63 this case, proc
22640 65 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 eed. */. if(
22650 20 70 43 74 65 2d 3e 7a 45 72 72 20 29 7b 0a 20 pCte->zErr ){.
22660 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f sqlite3Erro
22670 72 4d 73 67 28 70 50 61 72 73 65 2c 20 70 43 74 rMsg(pParse, pCt
22680 65 2d 3e 7a 45 72 72 2c 20 70 43 74 65 2d 3e 7a e->zErr, pCte->z
22690 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65 74 Name);. ret
226a0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 urn SQLITE_ERROR
226b0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 ;. }.. ass
226c0 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 ert( pFrom->pTab
226d0 3d 3d 30 20 29 3b 0a 20 20 20 20 70 46 72 6f 6d ==0 );. pFrom
226e0 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20 ->pTab = pTab =
226f0 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
22700 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 ero(db, sizeof(T
22710 61 62 6c 65 29 29 3b 0a 20 20 20 20 69 66 28 20 able));. if(
22720 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e pTab==0 ) return
22730 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
22740 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a pTab->nRef = 1;.
22750 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 pTab->zName
22760 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
22770 70 28 64 62 2c 20 70 43 74 65 2d 3e 7a 4e 61 6d p(db, pCte->zNam
22780 65 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e 69 50 e);. pTab->iP
22790 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 20 20 70 54 Key = -1;. pT
227a0 61 62 2d 3e 6e 52 6f 77 4c 6f 67 45 73 74 20 3d ab->nRowLogEst =
227b0 20 32 30 30 3b 20 61 73 73 65 72 74 28 20 32 30 200; assert( 20
227c0 30 3d 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 0==sqlite3LogEst
227d0 28 31 30 34 38 35 37 36 29 20 29 3b 0a 20 20 20 (1048576) );.
227e0 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 pTab->tabFlags
227f0 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 3b |= TF_Ephemeral;
22800 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 70 53 65 6c . pFrom->pSel
22810 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c ect = sqlite3Sel
22820 65 63 74 44 75 70 28 64 62 2c 20 70 43 74 65 2d ectDup(db, pCte-
22830 3e 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 >pSelect, 0);.
22840 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 if( db->malloc
22850 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 Failed ) return
22860 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 SQLITE_NOMEM;.
22870 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d assert( pFrom-
22880 3e 70 53 65 6c 65 63 74 20 29 3b 0a 0a 20 20 20 >pSelect );..
22890 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 69 /* Check if thi
228a0 73 20 69 73 20 61 20 72 65 63 75 72 73 69 76 65 s is a recursive
228b0 20 43 54 45 2e 20 2a 2f 0a 20 20 20 20 70 53 65 CTE. */. pSe
228c0 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 l = pFrom->pSele
228d0 63 74 3b 0a 20 20 20 20 62 4d 61 79 52 65 63 75 ct;. bMayRecu
228e0 72 73 69 76 65 20 3d 20 28 20 70 53 65 6c 2d 3e rsive = ( pSel->
228f0 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 70 53 op==TK_ALL || pS
22900 65 6c 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e el->op==TK_UNION
22910 20 29 3b 0a 20 20 20 20 69 66 28 20 62 4d 61 79 );. if( bMay
22920 52 65 63 75 72 73 69 76 65 20 29 7b 0a 20 20 20 Recursive ){.
22930 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 int i;.
22940 53 72 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 SrcList *pSrc =
22950 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e pFrom->pSelect->
22960 70 53 72 63 3b 0a 20 20 20 20 20 20 66 6f 72 28 pSrc;. for(
22970 69 3d 30 3b 20 69 3c 70 53 72 63 2d 3e 6e 53 72 i=0; i<pSrc->nSr
22980 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 c; i++){.
22990 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f struct SrcList_
229a0 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 item *pItem = &p
229b0 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 Src->a[i];.
229c0 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 if( pItem->zD
229d0 61 74 61 62 61 73 65 3d 3d 30 20 0a 20 20 20 20 atabase==0 .
229e0 20 20 20 20 20 26 26 20 70 49 74 65 6d 2d 3e 7a && pItem->z
229f0 4e 61 6d 65 21 3d 30 20 0a 20 20 20 20 20 20 20 Name!=0 .
22a00 20 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 && 0==sqlite3S
22a10 74 72 49 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 4e trICmp(pItem->zN
22a20 61 6d 65 2c 20 70 43 74 65 2d 3e 7a 4e 61 6d 65 ame, pCte->zName
22a30 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 ). ){.
22a40 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e pItem->
22a50 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 pTab = pTab;.
22a60 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 73 pItem->is
22a70 52 65 63 75 72 73 69 76 65 20 3d 20 31 3b 0a 20 Recursive = 1;.
22a80 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 6e pTab->n
22a90 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 Ref++;.
22aa0 20 70 53 65 6c 2d 3e 73 65 6c 46 6c 61 67 73 20 pSel->selFlags
22ab0 7c 3d 20 53 46 5f 52 65 63 75 72 73 69 76 65 3b |= SF_Recursive;
22ac0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
22ad0 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a }. }.. /*
22ae0 20 4f 6e 6c 79 20 6f 6e 65 20 72 65 63 75 72 73 Only one recurs
22af0 69 76 65 20 72 65 66 65 72 65 6e 63 65 20 69 73 ive reference is
22b00 20 70 65 72 6d 69 74 74 65 64 2e 20 2a 2f 20 0a permitted. */ .
22b10 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 52 if( pTab->nR
22b20 65 66 3e 32 20 29 7b 0a 20 20 20 20 20 20 73 71 ef>2 ){. sq
22b30 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 0a 20 lite3ErrorMsg(.
22b40 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c pParse,
22b50 20 22 6d 75 6c 74 69 70 6c 65 20 72 65 66 65 72 "multiple refer
22b60 65 6e 63 65 73 20 74 6f 20 72 65 63 75 72 73 69 ences to recursi
22b70 76 65 20 74 61 62 6c 65 3a 20 25 73 22 2c 20 70 ve table: %s", p
22b80 43 74 65 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 Cte->zName.
22b90 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e );. return
22ba0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 SQLITE_ERROR;.
22bb0 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 }. assert(
22bc0 20 70 54 61 62 2d 3e 6e 52 65 66 3d 3d 31 20 7c pTab->nRef==1 |
22bd0 7c 20 28 28 70 53 65 6c 2d 3e 73 65 6c 46 6c 61 | ((pSel->selFla
22be0 67 73 26 53 46 5f 52 65 63 75 72 73 69 76 65 29 gs&SF_Recursive)
22bf0 20 26 26 20 70 54 61 62 2d 3e 6e 52 65 66 3d 3d && pTab->nRef==
22c00 32 20 29 29 3b 0a 0a 20 20 20 20 70 43 74 65 2d 2 ));.. pCte-
22c10 3e 7a 45 72 72 20 3d 20 22 63 69 72 63 75 6c 61 >zErr = "circula
22c20 72 20 72 65 66 65 72 65 6e 63 65 3a 20 25 73 22 r reference: %s"
22c30 3b 0a 20 20 20 20 70 53 61 76 65 64 57 69 74 68 ;. pSavedWith
22c40 20 3d 20 70 50 61 72 73 65 2d 3e 70 57 69 74 68 = pParse->pWith
22c50 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 57 ;. pParse->pW
22c60 69 74 68 20 3d 20 70 57 69 74 68 3b 0a 20 20 20 ith = pWith;.
22c70 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 sqlite3WalkSele
22c80 63 74 28 70 57 61 6c 6b 65 72 2c 20 62 4d 61 79 ct(pWalker, bMay
22c90 52 65 63 75 72 73 69 76 65 20 3f 20 70 53 65 6c Recursive ? pSel
22ca0 2d 3e 70 50 72 69 6f 72 20 3a 20 70 53 65 6c 29 ->pPrior : pSel)
22cb0 3b 0a 0a 20 20 20 20 66 6f 72 28 70 4c 65 66 74 ;.. for(pLeft
22cc0 3d 70 53 65 6c 3b 20 70 4c 65 66 74 2d 3e 70 50 =pSel; pLeft->pP
22cd0 72 69 6f 72 3b 20 70 4c 65 66 74 3d 70 4c 65 66 rior; pLeft=pLef
22ce0 74 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20 20 20 t->pPrior);.
22cf0 70 45 4c 69 73 74 20 3d 20 70 4c 65 66 74 2d 3e pEList = pLeft->
22d00 70 45 4c 69 73 74 3b 0a 20 20 20 20 69 66 28 20 pEList;. if(
22d10 70 43 74 65 2d 3e 70 43 6f 6c 73 20 29 7b 0a 20 pCte->pCols ){.
22d20 20 20 20 20 20 69 66 28 20 70 45 4c 69 73 74 2d if( pEList-
22d30 3e 6e 45 78 70 72 21 3d 70 43 74 65 2d 3e 70 43 >nExpr!=pCte->pC
22d40 6f 6c 73 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 ols->nExpr ){.
22d50 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 sqlite3Err
22d60 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 orMsg(pParse, "t
22d70 61 62 6c 65 20 25 73 20 68 61 73 20 25 64 20 76 able %s has %d v
22d80 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63 6f 6c alues for %d col
22d90 75 6d 6e 73 22 2c 0a 20 20 20 20 20 20 20 20 20 umns",.
22da0 20 20 20 70 43 74 65 2d 3e 7a 4e 61 6d 65 2c 20 pCte->zName,
22db0 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 pEList->nExpr, p
22dc0 43 74 65 2d 3e 70 43 6f 6c 73 2d 3e 6e 45 78 70 Cte->pCols->nExp
22dd0 72 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 r. );.
22de0 20 20 20 20 20 70 50 61 72 73 65 2d 3e 70 57 69 pParse->pWi
22df0 74 68 20 3d 20 70 53 61 76 65 64 57 69 74 68 3b th = pSavedWith;
22e00 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 . return
22e10 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 SQLITE_ERROR;.
22e20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 45 4c 69 }. pELi
22e30 73 74 20 3d 20 70 43 74 65 2d 3e 70 43 6f 6c 73 st = pCte->pCols
22e40 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 65 6c ;. }.. sel
22e50 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 ectColumnsFromEx
22e60 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 prList(pParse, p
22e70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e 6e 43 EList, &pTab->nC
22e80 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f 6c 29 ol, &pTab->aCol)
22e90 3b 0a 20 20 20 20 69 66 28 20 62 4d 61 79 52 65 ;. if( bMayRe
22ea0 63 75 72 73 69 76 65 20 29 7b 0a 20 20 20 20 20 cursive ){.
22eb0 20 69 66 28 20 70 53 65 6c 2d 3e 73 65 6c 46 6c if( pSel->selFl
22ec0 61 67 73 20 26 20 53 46 5f 52 65 63 75 72 73 69 ags & SF_Recursi
22ed0 76 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 ve ){. pC
22ee0 74 65 2d 3e 7a 45 72 72 20 3d 20 22 6d 75 6c 74 te->zErr = "mult
22ef0 69 70 6c 65 20 72 65 63 75 72 73 69 76 65 20 72 iple recursive r
22f00 65 66 65 72 65 6e 63 65 73 3a 20 25 73 22 3b 0a eferences: %s";.
22f10 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 }else{.
22f20 20 20 20 20 20 70 43 74 65 2d 3e 7a 45 72 72 20 pCte->zErr
22f30 3d 20 22 72 65 63 75 72 73 69 76 65 20 72 65 66 = "recursive ref
22f40 65 72 65 6e 63 65 20 69 6e 20 61 20 73 75 62 71 erence in a subq
22f50 75 65 72 79 3a 20 25 73 22 3b 0a 20 20 20 20 20 uery: %s";.
22f60 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
22f70 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b WalkSelect(pWalk
22f80 65 72 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 7d er, pSel);. }
22f90 0a 20 20 20 20 70 43 74 65 2d 3e 7a 45 72 72 20 . pCte->zErr
22fa0 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65 2d = 0;. pParse-
22fb0 3e 70 57 69 74 68 20 3d 20 70 53 61 76 65 64 57 >pWith = pSavedW
22fc0 69 74 68 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 ith;. }.. retu
22fd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a rn SQLITE_OK;.}.
22fe0 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 #endif..#ifndef
22ff0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 54 45 0a SQLITE_OMIT_CTE.
23000 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 45 4c /*.** If the SEL
23010 45 43 54 20 70 61 73 73 65 64 20 61 73 20 74 68 ECT passed as th
23020 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e e second argumen
23030 74 20 68 61 73 20 61 6e 20 61 73 73 6f 63 69 61 t has an associa
23040 74 65 64 20 57 49 54 48 20 0a 2a 2a 20 63 6c 61 ted WITH .** cla
23050 75 73 65 2c 20 70 6f 70 20 69 74 20 66 72 6f 6d use, pop it from
23060 20 74 68 65 20 73 74 61 63 6b 20 73 74 6f 72 65 the stack store
23070 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 d as part of the
23080 20 50 61 72 73 65 20 6f 62 6a 65 63 74 2e 0a 2a Parse object..*
23090 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 *.** This functi
230a0 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74 68 on is used as th
230b0 65 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 e xSelectCallbac
230c0 6b 32 28 29 20 63 61 6c 6c 62 61 63 6b 20 62 79 k2() callback by
230d0 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63 .** sqlite3Selec
230e0 74 45 78 70 61 6e 64 28 29 20 77 68 65 6e 20 77 tExpand() when w
230f0 61 6c 6b 69 6e 67 20 61 20 53 45 4c 45 43 54 20 alking a SELECT
23100 74 72 65 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 tree to resolve
23110 74 61 62 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20 61 table.** names a
23120 6e 64 20 6f 74 68 65 72 20 46 52 4f 4d 20 63 6c nd other FROM cl
23130 61 75 73 65 20 65 6c 65 6d 65 6e 74 73 2e 20 0a ause elements. .
23140 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 */.static void s
23150 65 6c 65 63 74 50 6f 70 57 69 74 68 28 57 61 6c electPopWith(Wal
23160 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 ker *pWalker, Se
23170 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73 lect *p){. Pars
23180 65 20 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c e *pParse = pWal
23190 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 57 ker->pParse;. W
231a0 69 74 68 20 2a 70 57 69 74 68 20 3d 20 66 69 6e ith *pWith = fin
231b0 64 52 69 67 68 74 6d 6f 73 74 28 70 29 2d 3e 70 dRightmost(p)->p
231c0 57 69 74 68 3b 0a 20 20 69 66 28 20 70 57 69 74 With;. if( pWit
231d0 68 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 h!=0 ){. asse
231e0 72 74 28 20 70 50 61 72 73 65 2d 3e 70 57 69 74 rt( pParse->pWit
231f0 68 3d 3d 70 57 69 74 68 20 29 3b 0a 20 20 20 20 h==pWith );.
23200 70 50 61 72 73 65 2d 3e 70 57 69 74 68 20 3d 20 pParse->pWith =
23210 70 57 69 74 68 2d 3e 70 4f 75 74 65 72 3b 0a 20 pWith->pOuter;.
23220 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 }.}.#else.#defi
23230 6e 65 20 73 65 6c 65 63 74 50 6f 70 57 69 74 68 ne selectPopWith
23240 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 0.#endif../*.**
23250 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 This routine is
23260 20 61 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 a Walker callba
23270 63 6b 20 66 6f 72 20 22 65 78 70 61 6e 64 69 6e ck for "expandin
23280 67 22 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 g" a SELECT stat
23290 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78 70 61 6e ement..** "Expan
232a0 64 69 6e 67 22 20 6d 65 61 6e 73 20 74 6f 20 64 ding" means to d
232b0 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a o the following:
232c0 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 4d .**.** (1) M
232d0 61 6b 65 20 73 75 72 65 20 56 44 42 45 20 63 75 ake sure VDBE cu
232e0 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61 76 rsor numbers hav
232f0 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20 e been assigned
23300 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20 20 20 to every.**
23310 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 element of t
23320 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a he FROM clause..
23330 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 46 69 **.** (2) Fi
23340 6c 6c 20 69 6e 20 74 68 65 20 70 54 61 62 4c 69 ll in the pTabLi
23350 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66 69 65 st->a[].pTab fie
23360 6c 64 73 20 69 6e 20 74 68 65 20 53 72 63 4c 69 lds in the SrcLi
23370 73 74 20 74 68 61 74 20 0a 2a 2a 20 20 20 20 20 st that .**
23380 20 20 20 20 64 65 66 69 6e 65 73 20 46 52 4f 4d defines FROM
23390 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e 20 76 clause. When v
233a0 69 65 77 73 20 61 70 70 65 61 72 20 69 6e 20 74 iews appear in t
233b0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a he FROM clause,.
233c0 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c 6c 20 ** fill
233d0 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 53 pTabList->a[].pS
233e0 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f 70 elect with a cop
233f0 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 y of the SELECT
23400 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 statement.**
23410 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c 65 6d that implem
23420 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e 20 20 ents the view.
23430 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f A copy is made o
23440 66 20 74 68 65 20 76 69 65 77 27 73 20 53 45 4c f the view's SEL
23450 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 ECT.** s
23460 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68 61 74 tatement so that
23470 20 77 65 20 63 61 6e 20 66 72 65 65 6c 79 20 6d we can freely m
23480 6f 64 69 66 79 20 6f 72 20 64 65 6c 65 74 65 20 odify or delete
23490 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a that statement.*
234a0 2a 20 20 20 20 20 20 20 20 20 77 69 74 68 6f 75 * withou
234b0 74 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 t worrying about
234c0 20 6d 65 73 73 69 6e 67 20 75 70 20 74 68 65 20 messing up the
234d0 70 65 72 73 69 73 74 65 6e 74 20 72 65 70 72 65 persistent repre
234e0 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 sentation.**
234f0 20 20 20 20 20 6f 66 20 74 68 65 20 76 69 65 77 of the view
23500 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20 ..**.** (3)
23510 41 64 64 20 74 65 72 6d 73 20 74 6f 20 74 68 65 Add terms to the
23520 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f WHERE clause to
23530 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 74 68 65 accommodate the
23540 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 NATURAL keyword
23550 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e 20 6a .** on j
23560 6f 69 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e 20 oins and the ON
23570 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 and USING clause
23580 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a of joins..**.**
23590 20 20 20 20 28 34 29 20 20 53 63 61 6e 20 74 68 (4) Scan th
235a0 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e e list of column
235b0 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 s in the result
235c0 73 65 74 20 28 70 45 4c 69 73 74 29 20 6c 6f 6f set (pEList) loo
235d0 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 king.**
235e0 66 6f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 for instances of
235f0 20 74 68 65 20 22 2a 22 20 6f 70 65 72 61 74 6f the "*" operato
23600 72 20 6f 72 20 74 68 65 20 54 41 42 4c 45 2e 2a r or the TABLE.*
23610 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20 20 operator..**
23620 20 20 20 20 20 20 49 66 20 66 6f 75 6e 64 2c 20 If found,
23630 65 78 70 61 6e 64 20 65 61 63 68 20 22 2a 22 20 expand each "*"
23640 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75 to be every colu
23650 6d 6e 20 69 6e 20 65 76 65 72 79 20 74 61 62 6c mn in every tabl
23660 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 6e 64 e.** and
23670 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20 65 TABLE.* to be e
23680 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 54 very column in T
23690 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 ABLE..**.*/.stat
236a0 69 63 20 69 6e 74 20 73 65 6c 65 63 74 45 78 70 ic int selectExp
236b0 61 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a 70 57 ander(Walker *pW
236c0 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 alker, Select *p
236d0 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 ){. Parse *pPar
236e0 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 se = pWalker->pP
236f0 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a arse;. int i, j
23700 2c 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a , k;. SrcList *
23710 70 54 61 62 4c 69 73 74 3b 0a 20 20 45 78 70 72 pTabList;. Expr
23720 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 List *pEList;.
23730 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 struct SrcList_i
23740 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20 73 71 tem *pFrom;. sq
23750 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 lite3 *db = pPar
23760 73 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 20 2a se->db;. Expr *
23770 70 45 2c 20 2a 70 52 69 67 68 74 2c 20 2a 70 45 pE, *pRight, *pE
23780 78 70 72 3b 0a 20 20 75 31 36 20 73 65 6c 46 6c xpr;. u16 selFl
23790 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 ags = p->selFlag
237a0 73 3b 0a 0a 20 20 70 2d 3e 73 65 6c 46 6c 61 67 s;.. p->selFlag
237b0 73 20 7c 3d 20 53 46 5f 45 78 70 61 6e 64 65 64 s |= SF_Expanded
237c0 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c ;. if( db->mall
237d0 6f 63 46 61 69 6c 65 64 20 20 29 7b 0a 20 20 20 ocFailed ){.
237e0 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 return WRC_Abor
237f0 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 45 56 t;. }. if( NEV
23800 45 52 28 70 2d 3e 70 53 72 63 3d 3d 30 29 20 7c ER(p->pSrc==0) |
23810 7c 20 28 73 65 6c 46 6c 61 67 73 20 26 20 53 46 | (selFlags & SF
23820 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20 29 7b _Expanded)!=0 ){
23830 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f . return WRC_
23840 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70 54 61 Prune;. }. pTa
23850 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b bList = p->pSrc;
23860 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 . pEList = p->p
23870 45 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 EList;. sqlite3
23880 57 69 74 68 50 75 73 68 28 70 50 61 72 73 65 2c WithPush(pParse,
23890 20 66 69 6e 64 52 69 67 68 74 6d 6f 73 74 28 70 findRightmost(p
238a0 29 2d 3e 70 57 69 74 68 2c 20 30 29 3b 0a 0a 20 )->pWith, 0);..
238b0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 63 75 /* Make sure cu
238c0 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61 76 rsor numbers hav
238d0 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20 e been assigned
238e0 74 6f 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 to all entries i
238f0 6e 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 n. ** the FROM
23900 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 53 45 clause of the SE
23910 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a LECT statement..
23920 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 72 */. sqlite3Sr
23930 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f cListAssignCurso
23940 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c rs(pParse, pTabL
23950 69 73 74 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b ist);.. /* Look
23960 20 75 70 20 65 76 65 72 79 20 74 61 62 6c 65 20 up every table
23970 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 46 52 4f named in the FRO
23980 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 M clause of the
23990 73 65 6c 65 63 74 2e 20 20 49 66 0a 20 20 2a 2a select. If. **
239a0 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 an entry of the
239b0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 FROM clause is
239c0 61 20 73 75 62 71 75 65 72 79 20 69 6e 73 74 65 a subquery inste
239d0 61 64 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 ad of a table or
239e0 20 76 69 65 77 2c 0a 20 20 2a 2a 20 74 68 65 6e view,. ** then
239f0 20 63 72 65 61 74 65 20 61 20 74 72 61 6e 73 69 create a transi
23a00 65 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63 74 ent table struct
23a10 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20 ure to describe
23a20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 the subquery..
23a30 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 */. for(i=0, pF
23a40 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b rom=pTabList->a;
23a50 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 i<pTabList->nSr
23a60 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 c; i++, pFrom++)
23a70 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 {. Table *pTa
23a80 62 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 b;. assert( p
23a90 46 72 6f 6d 2d 3e 69 73 52 65 63 75 72 73 69 76 From->isRecursiv
23aa0 65 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d 2d 3e 70 e==0 || pFrom->p
23ab0 54 61 62 20 29 3b 0a 20 20 20 20 69 66 28 20 70 Tab );. if( p
23ac0 46 72 6f 6d 2d 3e 69 73 52 65 63 75 72 73 69 76 From->isRecursiv
23ad0 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 e ) continue;.
23ae0 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 if( pFrom->pTa
23af0 62 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a b!=0 ){. /*
23b00 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 This statement
23b10 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e has already been
23b20 20 70 72 65 70 61 72 65 64 2e 20 20 54 68 65 72 prepared. Ther
23b30 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 e is no need.
23b40 20 20 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74 ** to go furt
23b50 68 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 her. */. as
23b60 73 65 72 74 28 20 69 3d 3d 30 20 29 3b 0a 23 69 sert( i==0 );.#i
23b70 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 fndef SQLITE_OMI
23b80 54 5f 43 54 45 0a 20 20 20 20 20 20 73 65 6c 65 T_CTE. sele
23b90 63 74 50 6f 70 57 69 74 68 28 70 57 61 6c 6b 65 ctPopWith(pWalke
23ba0 72 2c 20 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20 r, p);.#endif.
23bb0 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 return WRC_P
23bc0 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e rune;. }.#ifn
23bd0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f def SQLITE_OMIT_
23be0 43 54 45 0a 20 20 20 20 69 66 28 20 77 69 74 68 CTE. if( with
23bf0 45 78 70 61 6e 64 28 70 57 61 6c 6b 65 72 2c 20 Expand(pWalker,
23c00 70 46 72 6f 6d 29 20 29 20 72 65 74 75 72 6e 20 pFrom) ) return
23c10 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 WRC_Abort;. i
23c20 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 29 f( pFrom->pTab )
23c30 20 7b 7d 20 65 6c 73 65 0a 23 65 6e 64 69 66 0a {} else.#endif.
23c40 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d 3e 7a if( pFrom->z
23c50 4e 61 6d 65 3d 3d 30 20 29 7b 0a 23 69 66 6e 64 Name==0 ){.#ifnd
23c60 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 ef SQLITE_OMIT_S
23c70 55 42 51 55 45 52 59 0a 20 20 20 20 20 20 53 65 UBQUERY. Se
23c80 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 lect *pSel = pFr
23c90 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 om->pSelect;.
23ca0 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65 72 /* A sub-quer
23cb0 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c y in the FROM cl
23cc0 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 ause of a SELECT
23cd0 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 */. assert
23ce0 28 20 70 53 65 6c 21 3d 30 20 29 3b 0a 20 20 20 ( pSel!=0 );.
23cf0 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d assert( pFrom
23d00 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20 ->pTab==0 );.
23d10 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 sqlite3WalkSe
23d20 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53 lect(pWalker, pS
23d30 65 6c 29 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d el);. pFrom
23d40 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 20 3d 20 ->pTab = pTab =
23d50 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a sqlite3DbMallocZ
23d60 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 ero(db, sizeof(T
23d70 61 62 6c 65 29 29 3b 0a 20 20 20 20 20 20 69 66 able));. if
23d80 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 ( pTab==0 ) retu
23d90 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
23da0 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d pTab->nRef =
23db0 20 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 1;. pTab->
23dc0 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d zName = sqlite3M
23dd0 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 Printf(db, "sqli
23de0 74 65 5f 73 71 5f 25 70 22 2c 20 28 76 6f 69 64 te_sq_%p", (void
23df0 2a 29 70 54 61 62 29 3b 0a 20 20 20 20 20 20 77 *)pTab);. w
23e00 68 69 6c 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 hile( pSel->pPri
23e10 6f 72 20 29 7b 20 70 53 65 6c 20 3d 20 70 53 65 or ){ pSel = pSe
23e20 6c 2d 3e 70 50 72 69 6f 72 3b 20 7d 0a 20 20 20 l->pPrior; }.
23e30 20 20 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 selectColumns
23e40 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 FromExprList(pPa
23e50 72 73 65 2c 20 70 53 65 6c 2d 3e 70 45 4c 69 73 rse, pSel->pELis
23e60 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 t, &pTab->nCol,
23e70 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 &pTab->aCol);.
23e80 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 pTab->iPKey
23e90 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 54 61 62 = -1;. pTab
23ea0 2d 3e 6e 52 6f 77 4c 6f 67 45 73 74 20 3d 20 32 ->nRowLogEst = 2
23eb0 30 30 3b 20 61 73 73 65 72 74 28 20 32 30 30 3d 00; assert( 200=
23ec0 3d 73 71 6c 69 74 65 33 4c 6f 67 45 73 74 28 31 =sqlite3LogEst(1
23ed0 30 34 38 35 37 36 29 20 29 3b 0a 20 20 20 20 20 048576) );.
23ee0 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 pTab->tabFlags
23ef0 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 3b |= TF_Ephemeral;
23f00 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 .#endif. }els
23f10 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f e{. /* An o
23f20 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f 72 rdinary table or
23f30 20 76 69 65 77 20 6e 61 6d 65 20 69 6e 20 74 68 view name in th
23f40 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f e FROM clause */
23f50 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 . assert( p
23f60 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b From->pTab==0 );
23f70 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 . pFrom->pT
23f80 61 62 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69 ab = pTab = sqli
23f90 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 49 74 te3LocateTableIt
23fa0 65 6d 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 em(pParse, 0, pF
23fb0 72 6f 6d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 rom);. if(
23fc0 70 54 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e pTab==0 ) return
23fd0 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 WRC_Abort;.
23fe0 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 52 65 66 if( pTab->nRef
23ff0 3d 3d 30 78 66 66 66 66 20 29 7b 0a 20 20 20 20 ==0xffff ){.
24000 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 sqlite3Error
24010 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f Msg(pParse, "too
24020 20 6d 61 6e 79 20 72 65 66 65 72 65 6e 63 65 73 many references
24030 20 74 6f 20 5c 22 25 73 5c 22 3a 20 6d 61 78 20 to \"%s\": max
24040 36 35 35 33 35 22 2c 0a 20 20 20 20 20 20 20 20 65535",.
24050 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b pTab->zName);
24060 0a 20 20 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e . pFrom->
24070 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 pTab = 0;.
24080 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f return WRC_Abo
24090 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 rt;. }.
240a0 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a pTab->nRef++;.
240b0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c #if !defined(SQL
240c0 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c ITE_OMIT_VIEW) |
240d0 7c 20 21 64 65 66 69 6e 65 64 20 28 53 51 4c 49 | !defined (SQLI
240e0 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 TE_OMIT_VIRTUALT
240f0 41 42 4c 45 29 0a 20 20 20 20 20 20 69 66 28 20 ABLE). if(
24100 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 7c 7c pTab->pSelect ||
24110 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 IsVirtual(pTab)
24120 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 ){. /* W
24130 65 20 72 65 61 63 68 20 68 65 72 65 20 69 66 20 e reach here if
24140 74 68 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 20 the named table
24150 69 73 20 61 20 72 65 61 6c 6c 79 20 61 20 76 69 is a really a vi
24160 65 77 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 ew */. if
24170 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 ( sqlite3ViewGet
24180 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 ColumnNames(pPar
24190 73 65 2c 20 70 54 61 62 29 20 29 20 72 65 74 75 se, pTab) ) retu
241a0 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 rn WRC_Abort;.
241b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 assert( pF
241c0 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 rom->pSelect==0
241d0 29 3b 0a 20 20 20 20 20 20 20 20 70 46 72 6f 6d );. pFrom
241e0 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 ->pSelect = sqli
241f0 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c te3SelectDup(db,
24200 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 2c 20 pTab->pSelect,
24210 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 0);. sqli
24220 74 65 33 53 65 6c 65 63 74 53 65 74 4e 61 6d 65 te3SelectSetName
24230 28 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 2c (pFrom->pSelect,
24240 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 pTab->zName);.
24250 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 sqlite3Wa
24260 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 lkSelect(pWalker
24270 2c 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 , pFrom->pSelect
24280 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 );. }.#endi
24290 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 f. }.. /*
242a0 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 64 65 78 Locate the index
242b0 20 6e 61 6d 65 64 20 62 79 20 74 68 65 20 49 4e named by the IN
242c0 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 2c DEXED BY clause,
242d0 20 69 66 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 if any. */.
242e0 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65 78 if( sqlite3Index
242f0 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73 edByLookup(pPars
24300 65 2c 20 70 46 72 6f 6d 29 20 29 7b 0a 20 20 20 e, pFrom) ){.
24310 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 return WRC_Ab
24320 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a ort;. }. }..
24330 20 20 2f 2a 20 50 72 6f 63 65 73 73 20 4e 41 54 /* Process NAT
24340 55 52 41 4c 20 6b 65 79 77 6f 72 64 73 2c 20 61 URAL keywords, a
24350 6e 64 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 nd ON and USING
24360 63 6c 61 75 73 65 73 20 6f 66 20 6a 6f 69 6e 73 clauses of joins
24370 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d .. */. if( db-
24380 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c >mallocFailed ||
24390 20 73 71 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f sqliteProcessJo
243a0 69 6e 28 70 50 61 72 73 65 2c 20 70 29 20 29 7b in(pParse, p) ){
243b0 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f . return WRC_
243c0 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a Abort;. }.. /*
243d0 20 46 6f 72 20 65 76 65 72 79 20 22 2a 22 20 74 For every "*" t
243e0 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20 74 68 hat occurs in th
243f0 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20 69 e column list, i
24400 6e 73 65 72 74 20 74 68 65 20 6e 61 6d 65 73 20 nsert the names
24410 6f 66 0a 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75 of. ** all colu
24420 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 mns in all table
24430 73 2e 20 20 41 6e 64 20 66 6f 72 20 65 76 65 72 s. And for ever
24440 79 20 54 41 42 4c 45 2e 2a 20 69 6e 73 65 72 74 y TABLE.* insert
24450 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 the names. **
24460 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 of all columns i
24470 6e 20 54 41 42 4c 45 2e 20 20 54 68 65 20 70 61 n TABLE. The pa
24480 72 73 65 72 20 69 6e 73 65 72 74 65 64 20 61 20 rser inserted a
24490 73 70 65 63 69 61 6c 20 65 78 70 72 65 73 73 69 special expressi
244a0 6f 6e 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 65 on. ** with the
244b0 20 54 4b 5f 41 4c 4c 20 6f 70 65 72 61 74 6f 72 TK_ALL operator
244c0 20 66 6f 72 20 65 61 63 68 20 22 2a 22 20 74 68 for each "*" th
244d0 61 74 20 69 74 20 66 6f 75 6e 64 20 69 6e 20 74 at it found in t
244e0 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a he column list..
244f0 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 ** The followi
24500 6e 67 20 63 6f 64 65 20 6a 75 73 74 20 68 61 73 ng code just has
24510 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 54 to locate the T
24520 4b 5f 41 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e K_ALL expression
24530 73 20 61 6e 64 20 65 78 70 61 6e 64 0a 20 20 2a s and expand. *
24540 2a 20 65 61 63 68 20 6f 6e 65 20 74 6f 20 74 68 * each one to th
24550 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f e list of all co
24560 6c 75 6d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 lumns in all tab
24570 6c 65 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 les.. **. ** T
24580 68 65 20 66 69 72 73 74 20 6c 6f 6f 70 20 6a 75 he first loop ju
24590 73 74 20 63 68 65 63 6b 73 20 74 6f 20 73 65 65 st checks to see
245a0 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e if there are an
245b0 79 20 22 2a 22 20 6f 70 65 72 61 74 6f 72 73 0a y "*" operators.
245c0 20 20 2a 2a 20 74 68 61 74 20 6e 65 65 64 20 65 ** that need e
245d0 78 70 61 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 xpanding.. */.
245e0 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 for(k=0; k<pELi
245f0 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b st->nExpr; k++){
24600 0a 20 20 20 20 70 45 20 3d 20 70 45 4c 69 73 74 . pE = pEList
24610 2d 3e 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 ->a[k].pExpr;.
24620 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b if( pE->op==TK
24630 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a 20 20 _ALL ) break;.
24640 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70 assert( pE->op
24650 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e !=TK_DOT || pE->
24660 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20 pRight!=0 );.
24670 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70 21 assert( pE->op!
24680 3d 54 4b 5f 44 4f 54 20 7c 7c 20 28 70 45 2d 3e =TK_DOT || (pE->
24690 70 4c 65 66 74 21 3d 30 20 26 26 20 70 45 2d 3e pLeft!=0 && pE->
246a0 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 pLeft->op==TK_ID
246b0 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 45 2d ) );. if( pE-
246c0 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70 >op==TK_DOT && p
246d0 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 E->pRight->op==T
246e0 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a 20 K_ALL ) break;.
246f0 20 7d 0a 20 20 69 66 28 20 6b 3c 70 45 4c 69 73 }. if( k<pELis
24700 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 t->nExpr ){.
24710 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 77 65 20 /*. ** If we
24720 67 65 74 20 68 65 72 65 20 69 74 20 6d 65 61 6e get here it mean
24730 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 s the result set
24740 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 contains one or
24750 20 6d 6f 72 65 20 22 2a 22 0a 20 20 20 20 2a 2a more "*". **
24760 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74 20 operators that
24770 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 61 6e need to be expan
24780 64 65 64 2e 20 20 4c 6f 6f 70 20 74 68 72 6f 75 ded. Loop throu
24790 67 68 20 65 61 63 68 20 65 78 70 72 65 73 73 69 gh each expressi
247a0 6f 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 on. ** in the
247b0 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 20 result set and
247c0 65 78 70 61 6e 64 20 74 68 65 6d 20 6f 6e 65 20 expand them one
247d0 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 by one.. */.
247e0 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 struct ExprLi
247f0 73 74 5f 69 74 65 6d 20 2a 61 20 3d 20 70 45 4c st_item *a = pEL
24800 69 73 74 2d 3e 61 3b 0a 20 20 20 20 45 78 70 72 ist->a;. Expr
24810 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 30 3b 0a List *pNew = 0;.
24820 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 int flags =
24830 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 pParse->db->flag
24840 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6f 6e 67 4e s;. int longN
24850 61 6d 65 73 20 3d 20 28 66 6c 61 67 73 20 26 20 ames = (flags &
24860 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 SQLITE_FullColNa
24870 6d 65 73 29 21 3d 30 0a 20 20 20 20 20 20 20 20 mes)!=0.
24880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 &&
24890 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 (flags & SQLITE
248a0 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 3d _ShortColNames)=
248b0 3d 30 3b 0a 0a 20 20 20 20 2f 2a 20 57 68 65 6e =0;.. /* When
248c0 20 70 72 6f 63 65 73 73 69 6e 67 20 46 52 4f 4d processing FROM
248d0 2d 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 69 -clause subqueri
248e0 65 73 2c 20 69 74 20 69 73 20 61 6c 77 61 79 73 es, it is always
248f0 20 74 68 65 20 63 61 73 65 0a 20 20 20 20 2a 2a the case. **
24900 20 74 68 61 74 20 66 75 6c 6c 5f 63 6f 6c 75 6d that full_colum
24910 6e 5f 6e 61 6d 65 73 3d 4f 46 46 20 61 6e 64 20 n_names=OFF and
24920 73 68 6f 72 74 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d short_column_nam
24930 65 73 3d 4f 4e 2e 20 20 54 68 65 0a 20 20 20 20 es=ON. The.
24940 2a 2a 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 ** sqlite3Result
24950 53 65 74 4f 66 53 65 6c 65 63 74 28 29 20 72 6f SetOfSelect() ro
24960 75 74 69 6e 65 20 6d 61 6b 65 73 20 69 74 20 73 utine makes it s
24970 6f 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 o. */. assert
24980 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 ( (p->selFlags &
24990 20 53 46 5f 4e 65 73 74 65 64 46 72 6f 6d 29 3d SF_NestedFrom)=
249a0 3d 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 =0. ||
249b0 28 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 ((flags & SQLITE
249c0 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 3d 3d _FullColNames)==
249d0 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 0 &&.
249e0 20 20 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 (flags & SQLI
249f0 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 TE_ShortColNames
24a00 29 21 3d 30 29 20 29 3b 0a 0a 20 20 20 20 66 6f )!=0) );.. fo
24a10 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74 2d r(k=0; k<pEList-
24a20 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 >nExpr; k++){.
24a30 20 20 20 20 70 45 20 3d 20 61 5b 6b 5d 2e 70 45 pE = a[k].pE
24a40 78 70 72 3b 0a 20 20 20 20 20 20 70 52 69 67 68 xpr;. pRigh
24a50 74 20 3d 20 70 45 2d 3e 70 52 69 67 68 74 3b 0a t = pE->pRight;.
24a60 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 assert( pE
24a70 2d 3e 6f 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 ->op!=TK_DOT ||
24a80 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20 pRight!=0 );.
24a90 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d 54 if( pE->op!=T
24aa0 4b 5f 41 4c 4c 20 26 26 20 28 70 45 2d 3e 6f 70 K_ALL && (pE->op
24ab0 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 52 69 67 !=TK_DOT || pRig
24ac0 68 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 ht->op!=TK_ALL)
24ad0 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 ){. /* Th
24ae0 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 65 78 is particular ex
24af0 70 72 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f pression does no
24b00 74 20 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 t need to be exp
24b10 61 6e 64 65 64 2e 0a 20 20 20 20 20 20 20 20 2a anded.. *
24b20 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d /. pNew =
24b30 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
24b40 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 Append(pParse, p
24b50 4e 65 77 2c 20 61 5b 6b 5d 2e 70 45 78 70 72 29 New, a[k].pExpr)
24b60 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e ;. if( pN
24b70 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ew ){.
24b80 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 pNew->a[pNew->nE
24b90 78 70 72 2d 31 5d 2e 7a 4e 61 6d 65 20 3d 20 61 xpr-1].zName = a
24ba0 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 [k].zName;.
24bb0 20 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 pNew->a[pNe
24bc0 77 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 53 70 61 w->nExpr-1].zSpa
24bd0 6e 20 3d 20 61 5b 6b 5d 2e 7a 53 70 61 6e 3b 0a n = a[k].zSpan;.
24be0 20 20 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a a[k].z
24bf0 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 Name = 0;.
24c00 20 20 20 20 61 5b 6b 5d 2e 7a 53 70 61 6e 20 3d a[k].zSpan =
24c10 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0;. }.
24c20 20 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72 a[k].pExpr
24c30 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 = 0;. }els
24c40 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 e{. /* Th
24c50 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 is expression is
24c60 20 61 20 22 2a 22 20 6f 72 20 61 20 22 54 41 42 a "*" or a "TAB
24c70 4c 45 2e 2a 22 20 61 6e 64 20 6e 65 65 64 73 20 LE.*" and needs
24c80 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a to be. **
24c90 20 65 78 70 61 6e 64 65 64 2e 20 2a 2f 0a 20 20 expanded. */.
24ca0 20 20 20 20 20 20 69 6e 74 20 74 61 62 6c 65 53 int tableS
24cb0 65 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a een = 0; /*
24cc0 20 53 65 74 20 74 6f 20 31 20 77 68 65 6e 20 54 Set to 1 when T
24cd0 41 42 4c 45 20 6d 61 74 63 68 65 73 20 2a 2f 0a ABLE matches */.
24ce0 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 char *zT
24cf0 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 Name = 0;
24d00 2f 2a 20 74 65 78 74 20 6f 66 20 6e 61 6d 65 20 /* text of name
24d10 6f 66 20 54 41 42 4c 45 20 2a 2f 0a 20 20 20 20 of TABLE */.
24d20 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d if( pE->op==
24d30 54 4b 5f 44 4f 54 20 29 7b 0a 20 20 20 20 20 20 TK_DOT ){.
24d40 20 20 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e assert( pE->
24d50 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20 pLeft!=0 );.
24d60 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 assert( !E
24d70 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 xprHasProperty(p
24d80 45 2d 3e 70 4c 65 66 74 2c 20 45 50 5f 49 6e 74 E->pLeft, EP_Int
24d90 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 Value) );.
24da0 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 70 45 2d zTName = pE-
24db0 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e >pLeft->u.zToken
24dc0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
24dd0 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 for(i=0, pFr
24de0 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 om=pTabList->a;
24df0 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 i<pTabList->nSrc
24e00 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b ; i++, pFrom++){
24e10 0a 20 20 20 20 20 20 20 20 20 20 54 61 62 6c 65 . Table
24e20 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e *pTab = pFrom->
24e30 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 pTab;.
24e40 53 65 6c 65 63 74 20 2a 70 53 75 62 20 3d 20 70 Select *pSub = p
24e50 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 From->pSelect;.
24e60 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a char *z
24e70 54 61 62 4e 61 6d 65 20 3d 20 70 46 72 6f 6d 2d TabName = pFrom-
24e80 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 >zAlias;.
24e90 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
24ea0 53 63 68 65 6d 61 4e 61 6d 65 20 3d 20 30 3b 0a SchemaName = 0;.
24eb0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44 int iD
24ec0 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 b;. if(
24ed0 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a zTabName==0 ){.
24ee0 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 61 62 zTab
24ef0 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 Name = pTab->zNa
24f00 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a me;. }.
24f10 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 62 if( db
24f20 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 ->mallocFailed )
24f30 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 break;.
24f40 20 20 69 66 28 20 70 53 75 62 3d 3d 30 20 7c 7c if( pSub==0 ||
24f50 20 28 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 (pSub->selFlags
24f60 20 26 20 53 46 5f 4e 65 73 74 65 64 46 72 6f 6d & SF_NestedFrom
24f70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 )==0 ){.
24f80 20 20 20 20 70 53 75 62 20 3d 20 30 3b 0a 20 20 pSub = 0;.
24f90 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 if( zT
24fa0 4e 61 6d 65 20 26 26 20 73 71 6c 69 74 65 33 53 Name && sqlite3S
24fb0 74 72 49 43 6d 70 28 7a 54 4e 61 6d 65 2c 20 7a trICmp(zTName, z
24fc0 54 61 62 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 TabName)!=0 ){.
24fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e con
24fe0 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 tinue;.
24ff0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
25000 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 iDb = sqlite3Sc
25010 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 hemaToIndex(db,
25020 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a pTab->pSchema);.
25030 20 20 20 20 20 20 20 20 20 20 20 20 7a 53 63 68 zSch
25040 65 6d 61 4e 61 6d 65 20 3d 20 69 44 62 3e 3d 30 emaName = iDb>=0
25050 20 3f 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e ? db->aDb[iDb].
25060 7a 4e 61 6d 65 20 3a 20 22 2a 22 3b 0a 20 20 20 zName : "*";.
25070 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
25080 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 54 for(j=0; j<pT
25090 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a ab->nCol; j++){.
250a0 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 char
250b0 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e *zName = pTab->
250c0 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 aCol[j].zName;.
250d0 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 char
250e0 2a 7a 43 6f 6c 6e 61 6d 65 3b 20 20 2f 2a 20 54 *zColname; /* T
250f0 68 65 20 63 6f 6d 70 75 74 65 64 20 63 6f 6c 75 he computed colu
25100 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 mn name */.
25110 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 6f char *zTo
25120 46 72 65 65 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f Free; /* Mallo
25130 63 65 64 20 73 74 72 69 6e 67 20 74 68 61 74 20 ced string that
25140 6e 65 65 64 73 20 74 6f 20 62 65 20 66 72 65 65 needs to be free
25150 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 d */.
25160 20 54 6f 6b 65 6e 20 73 43 6f 6c 6e 61 6d 65 3b Token sColname;
25170 20 20 2f 2a 20 43 6f 6d 70 75 74 65 64 20 63 6f /* Computed co
25180 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20 61 20 74 lumn name as a t
25190 6f 6b 65 6e 20 2a 2f 0a 0a 20 20 20 20 20 20 20 oken */..
251a0 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 4e 61 assert( zNa
251b0 6d 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 me );.
251c0 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 26 26 20 if( zTName &&
251d0 70 53 75 62 0a 20 20 20 20 20 20 20 20 20 20 20 pSub.
251e0 20 20 26 26 20 73 71 6c 69 74 65 33 4d 61 74 63 && sqlite3Matc
251f0 68 53 70 61 6e 4e 61 6d 65 28 70 53 75 62 2d 3e hSpanName(pSub->
25200 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 53 70 pEList->a[j].zSp
25210 61 6e 2c 20 30 2c 20 7a 54 4e 61 6d 65 2c 20 30 an, 0, zTName, 0
25220 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 )==0.
25230 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
25240 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 continue;.
25250 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 }..
25260 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 63 /* If a c
25270 6f 6c 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20 olumn is marked
25280 61 73 20 27 68 69 64 64 65 6e 27 20 28 63 75 72 as 'hidden' (cur
25290 72 65 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73 rently only poss
252a0 69 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 ible.
252b0 20 2a 2a 20 66 6f 72 20 76 69 72 74 75 61 6c 20 ** for virtual
252c0 74 61 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20 tables), do not
252d0 69 6e 63 6c 75 64 65 20 69 74 20 69 6e 20 74 68 include it in th
252e0 65 20 65 78 70 61 6e 64 65 64 0a 20 20 20 20 20 e expanded.
252f0 20 20 20 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 ** result
25300 2d 73 65 74 20 6c 69 73 74 2e 0a 20 20 20 20 20 -set list..
25310 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
25320 20 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64 if( IsHidd
25330 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e enColumn(&pTab->
25340 61 43 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20 20 aCol[j]) ){.
25350 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 assert
25360 28 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 (IsVirtual(pTab)
25370 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 );.
25380 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 continue;.
25390 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
253a0 20 20 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d tableSeen =
253b0 20 31 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 20 1;..
253c0 20 69 66 28 20 69 3e 30 20 26 26 20 7a 54 4e 61 if( i>0 && zTNa
253d0 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 me==0 ){.
253e0 20 20 20 20 20 20 20 69 66 28 20 28 70 46 72 6f if( (pFro
253f0 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 m->jointype & JT
25400 5f 4e 41 54 55 52 41 4c 29 21 3d 30 0a 20 20 20 _NATURAL)!=0.
25410 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
25420 74 61 62 6c 65 41 6e 64 43 6f 6c 75 6d 6e 49 6e tableAndColumnIn
25430 64 65 78 28 70 54 61 62 4c 69 73 74 2c 20 69 2c dex(pTabList, i,
25440 20 7a 4e 61 6d 65 2c 20 30 2c 20 30 29 0a 20 20 zName, 0, 0).
25450 20 20 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 ){.
25460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
25470 2a 20 49 6e 20 61 20 4e 41 54 55 52 41 4c 20 6a * In a NATURAL j
25480 6f 69 6e 2c 20 6f 6d 69 74 20 74 68 65 20 6a 6f oin, omit the jo
25490 69 6e 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 in columns from
254a0 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 the .
254b0 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 74 6f ** table to
254c0 20 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 the right of th
254d0 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 20 20 e join */.
254e0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e contin
254f0 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ue;.
25500 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 }.
25510 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 64 4c if( sqlite3IdL
25520 69 73 74 49 6e 64 65 78 28 70 46 72 6f 6d 2d 3e istIndex(pFrom->
25530 70 55 73 69 6e 67 2c 20 7a 4e 61 6d 65 29 3e 3d pUsing, zName)>=
25540 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 0 ){.
25550 20 20 20 20 20 2f 2a 20 49 6e 20 61 20 6a 6f 69 /* In a joi
25560 6e 20 77 69 74 68 20 61 20 55 53 49 4e 47 20 63 n with a USING c
25570 6c 61 75 73 65 2c 20 6f 6d 69 74 20 63 6f 6c 75 lause, omit colu
25580 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20 20 20 20 mns in the.
25590 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75 73 ** us
255a0 69 6e 67 20 63 6c 61 75 73 65 20 66 72 6f 6d 20 ing clause from
255b0 74 68 65 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 the table on the
255c0 20 72 69 67 68 74 2e 20 2a 2f 0a 20 20 20 20 20 right. */.
255d0 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 conti
255e0 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 nue;.
255f0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 }.
25600 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 }. p
25610 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 Right = sqlite3E
25620 78 70 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 7a xpr(db, TK_ID, z
25630 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 Name);.
25640 20 20 20 7a 43 6f 6c 6e 61 6d 65 20 3d 20 7a 4e zColname = zN
25650 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 ame;.
25660 20 7a 54 6f 46 72 65 65 20 3d 20 30 3b 0a 20 20 zToFree = 0;.
25670 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f if( lo
25680 6e 67 4e 61 6d 65 73 20 7c 7c 20 70 54 61 62 4c ngNames || pTabL
25690 69 73 74 2d 3e 6e 53 72 63 3e 31 20 29 7b 0a 20 ist->nSrc>1 ){.
256a0 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 Exp
256b0 72 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 r *pLeft;.
256c0 20 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 pLeft =
256d0 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 sqlite3Expr(db,
256e0 54 4b 5f 49 44 2c 20 7a 54 61 62 4e 61 6d 65 29 TK_ID, zTabName)
256f0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
25700 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 pExpr = sqlite3P
25710 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f Expr(pParse, TK_
25720 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 DOT, pLeft, pRig
25730 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 ht, 0);.
25740 20 20 20 20 20 20 69 66 28 20 7a 53 63 68 65 6d if( zSchem
25750 61 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 aName ){.
25760 20 20 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d pLeft =
25770 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c sqlite3Expr(db,
25780 20 54 4b 5f 49 44 2c 20 7a 53 63 68 65 6d 61 4e TK_ID, zSchemaN
25790 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 ame);.
257a0 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71 pExpr = sq
257b0 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 lite3PExpr(pPars
257c0 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 e, TK_DOT, pLeft
257d0 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 , pExpr, 0);.
257e0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 }.
257f0 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c if( l
25800 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20 ongNames ){.
25810 20 20 20 20 20 20 20 20 20 20 20 20 7a 43 6f 6c zCol
25820 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 name = sqlite3MP
25830 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 rintf(db, "%s.%s
25840 22 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e 61 ", zTabName, zNa
25850 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 me);.
25860 20 20 20 20 20 7a 54 6f 46 72 65 65 20 3d 20 7a zToFree = z
25870 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20 Colname;.
25880 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
25890 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
258a0 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 20 pExpr
258b0 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 = pRight;.
258c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 }.
258d0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 pNew = sqlit
258e0 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 e3ExprListAppend
258f0 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 70 (pParse, pNew, p
25900 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20 Expr);.
25910 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 7a 20 3d 20 sColname.z =
25920 7a 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20 zColname;.
25930 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 6e sColname.n
25940 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e = sqlite3Strlen
25950 33 30 28 7a 43 6f 6c 6e 61 6d 65 29 3b 0a 20 20 30(zColname);.
25960 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 sqlite
25970 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 3ExprListSetName
25980 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 26 (pParse, pNew, &
25990 73 43 6f 6c 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 sColname, 0);.
259a0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 4e if( pN
259b0 65 77 20 26 26 20 28 70 2d 3e 73 65 6c 46 6c 61 ew && (p->selFla
259c0 67 73 20 26 20 53 46 5f 4e 65 73 74 65 64 46 72 gs & SF_NestedFr
259d0 6f 6d 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 om)!=0 ){.
259e0 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20 45 struct E
259f0 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 58 xprList_item *pX
25a00 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 = &pNew->a[pNew
25a10 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 ->nExpr-1];.
25a20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 53 if( pS
25a30 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ub ){.
25a40 20 20 20 20 20 20 70 58 2d 3e 7a 53 70 61 6e 20 pX->zSpan
25a50 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 = sqlite3DbStrDu
25a60 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 45 4c 69 p(db, pSub->pELi
25a70 73 74 2d 3e 61 5b 6a 5d 2e 7a 53 70 61 6e 29 3b st->a[j].zSpan);
25a80 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
25a90 20 74 65 73 74 63 61 73 65 28 20 70 58 2d 3e 7a testcase( pX->z
25aa0 53 70 61 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 20 Span==0 );.
25ab0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a }else{.
25ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25ad0 70 58 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 pX->zSpan = sqli
25ae0 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 te3MPrintf(db, "
25af0 25 73 2e 25 73 2e 25 73 22 2c 0a 20 20 20 20 20 %s.%s.%s",.
25b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
25b20 20 20 20 20 20 20 7a 53 63 68 65 6d 61 4e 61 6d zSchemaNam
25b30 65 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 43 6f e, zTabName, zCo
25b40 6c 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 lname);.
25b50 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 testcase
25b60 28 20 70 58 2d 3e 7a 53 70 61 6e 3d 3d 30 20 29 ( pX->zSpan==0 )
25b70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ;.
25b80 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 }.
25b90 70 58 2d 3e 62 53 70 61 6e 49 73 54 61 62 20 3d pX->bSpanIsTab =
25ba0 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 1;.
25bb0 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 }. sq
25bc0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 lite3DbFree(db,
25bd0 7a 54 6f 46 72 65 65 29 3b 0a 20 20 20 20 20 20 zToFree);.
25be0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a }. }.
25bf0 20 20 20 20 20 20 20 20 69 66 28 20 21 74 61 62 if( !tab
25c00 6c 65 53 65 65 6e 20 29 7b 0a 20 20 20 20 20 20 leSeen ){.
25c10 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 29 if( zTName )
25c20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 {. sq
25c30 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
25c40 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74 arse, "no such t
25c50 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 4e 61 6d able: %s", zTNam
25c60 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 e);. }e
25c70 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 lse{.
25c80 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 sqlite3ErrorMsg
25c90 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 74 61 62 (pParse, "no tab
25ca0 6c 65 73 20 73 70 65 63 69 66 69 65 64 22 29 3b les specified");
25cb0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 . }.
25cc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 }. }.
25cd0 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 }. sqlite3
25ce0 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 ExprListDelete(d
25cf0 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 b, pEList);.
25d00 70 2d 3e 70 45 4c 69 73 74 20 3d 20 70 4e 65 77 p->pEList = pNew
25d10 3b 0a 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 ;. }.#if SQLITE
25d20 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 _MAX_COLUMN. if
25d30 28 20 70 2d 3e 70 45 4c 69 73 74 20 26 26 20 70 ( p->pEList && p
25d40 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e ->pEList->nExpr>
25d50 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 db->aLimit[SQLIT
25d60 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 E_LIMIT_COLUMN]
25d70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 ){. sqlite3Er
25d80 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 rorMsg(pParse, "
25d90 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 too many columns
25da0 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 22 29 in result set")
25db0 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 ;. }.#endif. r
25dc0 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
25dd0 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d ue;.}../*.** No-
25de0 6f 70 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 74 op routine for t
25df0 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 he parse-tree wa
25e00 6c 6b 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e lker..**.** When
25e10 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
25e20 20 74 68 65 20 57 61 6c 6b 65 72 2e 78 45 78 70 the Walker.xExp
25e30 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20 65 rCallback then e
25e40 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 0a xpression trees.
25e50 2a 2a 20 61 72 65 20 77 61 6c 6b 65 64 20 77 69 ** are walked wi
25e60 74 68 6f 75 74 20 61 6e 79 20 61 63 74 69 6f 6e thout any action
25e70 73 20 62 65 69 6e 67 20 74 61 6b 65 6e 20 61 74 s being taken at
25e80 20 65 61 63 68 20 6e 6f 64 65 2e 20 20 50 72 65 each node. Pre
25e90 73 75 6d 61 62 6c 79 2c 0a 2a 2a 20 77 68 65 6e sumably,.** when
25ea0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 this routine is
25eb0 20 75 73 65 64 20 66 6f 72 20 57 61 6c 6b 65 72 used for Walker
25ec0 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 74 .xExprCallback t
25ed0 68 65 6e 20 0a 2a 2a 20 57 61 6c 6b 65 72 2e 78 hen .** Walker.x
25ee0 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 69 SelectCallback i
25ef0 73 20 73 65 74 20 74 6f 20 64 6f 20 73 6f 6d 65 s set to do some
25f00 74 68 69 6e 67 20 75 73 65 66 75 6c 20 66 6f 72 thing useful for
25f10 20 65 76 65 72 79 20 0a 2a 2a 20 73 75 62 71 75 every .** subqu
25f20 65 72 79 20 69 6e 20 74 68 65 20 70 61 72 73 65 ery in the parse
25f30 72 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 r tree..*/.stati
25f40 63 20 69 6e 74 20 65 78 70 72 57 61 6c 6b 4e 6f c int exprWalkNo
25f50 6f 70 28 57 61 6c 6b 65 72 20 2a 4e 6f 74 55 73 op(Walker *NotUs
25f60 65 64 2c 20 45 78 70 72 20 2a 4e 6f 74 55 73 65 ed, Expr *NotUse
25f70 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 d2){. UNUSED_PA
25f80 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 RAMETER2(NotUsed
25f90 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 , NotUsed2);. r
25fa0 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e eturn WRC_Contin
25fb0 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 ue;.}../*.** Thi
25fc0 73 20 72 6f 75 74 69 6e 65 20 22 65 78 70 61 6e s routine "expan
25fd0 64 73 22 20 61 20 53 45 4c 45 43 54 20 73 74 61 ds" a SELECT sta
25fe0 74 65 6d 65 6e 74 20 61 6e 64 20 61 6c 6c 20 6f tement and all o
25ff0 66 20 69 74 73 20 73 75 62 71 75 65 72 69 65 73 f its subqueries
26000 2e 0a 2a 2a 20 46 6f 72 20 61 64 64 69 74 69 6f ..** For additio
26010 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 nal information
26020 6f 6e 20 77 68 61 74 20 69 74 20 6d 65 61 6e 73 on what it means
26030 20 74 6f 20 22 65 78 70 61 6e 64 22 20 61 20 53 to "expand" a S
26040 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 ELECT.** stateme
26050 6e 74 2c 20 73 65 65 20 74 68 65 20 63 6f 6d 6d nt, see the comm
26060 65 6e 74 20 6f 6e 20 74 68 65 20 73 65 6c 65 63 ent on the selec
26070 74 45 78 70 61 6e 64 20 77 6f 72 6b 65 72 20 63 tExpand worker c
26080 61 6c 6c 62 61 63 6b 20 61 62 6f 76 65 2e 0a 2a allback above..*
26090 2a 0a 2a 2a 20 45 78 70 61 6e 64 69 6e 67 20 61 *.** Expanding a
260a0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
260b0 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 73 t is the first s
260c0 74 65 70 20 69 6e 20 70 72 6f 63 65 73 73 69 6e tep in processin
260d0 67 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 g a.** SELECT st
260e0 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 53 45 atement. The SE
260f0 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6d LECT statement m
26100 75 73 74 20 62 65 20 65 78 70 61 6e 64 65 64 20 ust be expanded
26110 62 65 66 6f 72 65 0a 2a 2a 20 6e 61 6d 65 20 72 before.** name r
26120 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 70 65 72 esolution is per
26130 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 formed..**.** If
26140 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 anything goes w
26150 72 6f 6e 67 2c 20 61 6e 20 65 72 72 6f 72 20 6d rong, an error m
26160 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74 65 essage is writte
26170 6e 20 69 6e 74 6f 20 70 50 61 72 73 65 2e 0a 2a n into pParse..*
26180 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 * The calling fu
26190 6e 63 74 69 6f 6e 20 63 61 6e 20 64 65 74 65 63 nction can detec
261a0 74 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 62 79 t the problem by
261b0 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 70 50 61 72 looking at pPar
261c0 73 65 2d 3e 6e 45 72 72 0a 2a 2a 20 61 6e 64 2f se->nErr.** and/
261d0 6f 72 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d or pParse->db->m
261e0 61 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2f 0a allocFailed..*/.
261f0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 static void sqli
26200 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 te3SelectExpand(
26210 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 Parse *pParse, S
26220 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b elect *pSelect){
26230 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 6d . Walker w;. m
26240 65 6d 73 65 74 28 26 77 2c 20 30 2c 20 73 69 7a emset(&w, 0, siz
26250 65 6f 66 28 77 29 29 3b 0a 20 20 77 2e 78 45 78 eof(w));. w.xEx
26260 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 prCallback = exp
26270 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20 20 77 2e 70 rWalkNoop;. w.p
26280 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a Parse = pParse;.
26290 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 68 61 if( pParse->ha
262a0 73 43 6f 6d 70 6f 75 6e 64 20 29 7b 0a 20 20 20 sCompound ){.
262b0 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 w.xSelectCallba
262c0 63 6b 20 3d 20 63 6f 6e 76 65 72 74 43 6f 6d 70 ck = convertComp
262d0 6f 75 6e 64 53 65 6c 65 63 74 54 6f 53 75 62 71 oundSelectToSubq
262e0 75 65 72 79 3b 0a 20 20 20 20 73 71 6c 69 74 65 uery;. sqlite
262f0 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 3WalkSelect(&w,
26300 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d 0a 20 20 pSelect);. }.
26310 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 w.xSelectCallbac
26320 6b 20 3d 20 73 65 6c 65 63 74 45 78 70 61 6e 64 k = selectExpand
26330 65 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 er;. w.xSelectC
26340 61 6c 6c 62 61 63 6b 32 20 3d 20 73 65 6c 65 63 allback2 = selec
26350 74 50 6f 70 57 69 74 68 3b 0a 20 20 73 71 6c 69 tPopWith;. sqli
26360 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 te3WalkSelect(&w
26370 2c 20 70 53 65 6c 65 63 74 29 3b 0a 7d 0a 0a 0a , pSelect);.}...
26380 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
26390 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a MIT_SUBQUERY./*.
263a0 2a 2a 20 54 68 69 73 20 69 73 20 61 20 57 61 6c ** This is a Wal
263b0 6b 65 72 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 ker.xSelectCallb
263c0 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 ack callback for
263d0 20 74 68 65 20 73 71 6c 69 74 65 33 53 65 6c 65 the sqlite3Sele
263e0 63 74 54 79 70 65 49 6e 66 6f 28 29 0a 2a 2a 20 ctTypeInfo().**
263f0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a interface..**.**
26400 20 46 6f 72 20 65 61 63 68 20 46 52 4f 4d 2d 63 For each FROM-c
26410 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 2c 20 lause subquery,
26420 61 64 64 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 add Column.zType
26430 20 61 6e 64 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c and Column.zCol
26440 6c 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e l.** information
26450 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73 74 to the Table st
26460 72 75 63 74 75 72 65 20 74 68 61 74 20 72 65 70 ructure that rep
26470 72 65 73 65 6e 74 73 20 74 68 65 20 72 65 73 75 resents the resu
26480 6c 74 20 73 65 74 0a 2a 2a 20 6f 66 20 74 68 61 lt set.** of tha
26490 74 20 73 75 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a t subquery..**.*
264a0 2a 20 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 * The Table stru
264b0 63 74 75 72 65 20 74 68 61 74 20 72 65 70 72 65 cture that repre
264c0 73 65 6e 74 73 20 74 68 65 20 72 65 73 75 6c 74 sents the result
264d0 20 73 65 74 20 77 61 73 20 63 6f 6e 73 74 72 75 set was constru
264e0 63 74 65 64 0a 2a 2a 20 62 79 20 73 65 6c 65 63 cted.** by selec
264f0 74 45 78 70 61 6e 64 65 72 28 29 20 62 75 74 20 tExpander() but
26500 74 68 65 20 74 79 70 65 20 61 6e 64 20 63 6f 6c the type and col
26510 6c 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 lation informati
26520 6f 6e 20 77 61 73 20 6f 6d 69 74 74 65 64 0a 2a on was omitted.*
26530 2a 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 20 * at that point
26540 62 65 63 61 75 73 65 20 69 64 65 6e 74 69 66 69 because identifi
26550 65 72 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 ers had not yet
26560 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20 been resolved.
26570 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 This.** routine
26580 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 is called after
26590 69 64 65 6e 74 69 66 69 65 72 20 72 65 73 6f 6c identifier resol
265a0 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 ution..*/.static
265b0 20 76 6f 69 64 20 73 65 6c 65 63 74 41 64 64 53 void selectAddS
265c0 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f 28 ubqueryTypeInfo(
265d0 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c Walker *pWalker,
265e0 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 50 Select *p){. P
265f0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20 arse *pParse;.
26600 69 6e 74 20 69 3b 0a 20 20 53 72 63 4c 69 73 74 int i;. SrcList
26610 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 73 74 *pTabList;. st
26620 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 ruct SrcList_ite
26630 6d 20 2a 70 46 72 6f 6d 3b 0a 0a 20 20 61 73 73 m *pFrom;.. ass
26640 65 72 74 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 ert( p->selFlags
26650 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20 29 & SF_Resolved )
26660 3b 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 ;. if( (p->selF
26670 6c 61 67 73 20 26 20 53 46 5f 48 61 73 54 79 70 lags & SF_HasTyp
26680 65 49 6e 66 6f 29 3d 3d 30 20 29 7b 0a 20 20 20 eInfo)==0 ){.
26690 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 p->selFlags |=
266a0 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 3b 0a SF_HasTypeInfo;.
266b0 20 20 20 20 70 50 61 72 73 65 20 3d 20 70 57 61 pParse = pWa
266c0 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 lker->pParse;.
266d0 20 20 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e pTabList = p->
266e0 70 53 72 63 3b 0a 20 20 20 20 66 6f 72 28 69 3d pSrc;. for(i=
266f0 30 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 0, pFrom=pTabLis
26700 74 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 t->a; i<pTabList
26710 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 ->nSrc; i++, pFr
26720 6f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 54 61 62 om++){. Tab
26730 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f 6d le *pTab = pFrom
26740 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 69 66 ->pTab;. if
26750 28 20 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 ( ALWAYS(pTab!=0
26760 29 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46 ) && (pTab->tabF
26770 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d 65 lags & TF_Epheme
26780 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 ral)!=0 ){.
26790 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65 72 /* A sub-quer
267a0 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c y in the FROM cl
267b0 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 ause of a SELECT
267c0 20 2a 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 */. Sele
267d0 63 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d ct *pSel = pFrom
267e0 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 ->pSelect;.
267f0 20 20 20 69 66 28 20 70 53 65 6c 20 29 7b 0a 20 if( pSel ){.
26800 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 while(
26810 70 53 65 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70 pSel->pPrior ) p
26820 53 65 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 Sel = pSel->pPri
26830 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 73 65 or;. se
26840 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 lectAddColumnTyp
26850 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 eAndCollation(pP
26860 61 72 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c arse, pTab, pSel
26870 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
26880 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d }. }. }.}
26890 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 .#endif.../*.**
268a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64 This routine add
268b0 73 20 64 61 74 61 74 79 70 65 20 61 6e 64 20 63 s datatype and c
268c0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 ollating sequenc
268d0 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f e information to
268e0 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 20 73 74 .** the Table st
268f0 72 75 63 74 75 72 65 73 20 6f 66 20 61 6c 6c 20 ructures of all
26900 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71 FROM-clause subq
26910 75 65 72 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53 ueries in a.** S
26920 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e ELECT statement.
26930 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 .**.** Use this
26940 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 6e 61 routine after na
26950 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a me resolution..*
26960 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 /.static void sq
26970 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64 54 79 lite3SelectAddTy
26980 70 65 49 6e 66 6f 28 50 61 72 73 65 20 2a 70 50 peInfo(Parse *pP
26990 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 arse, Select *pS
269a0 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64 65 66 20 elect){.#ifndef
269b0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 SQLITE_OMIT_SUBQ
269c0 55 45 52 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b UERY. Walker w;
269d0 0a 20 20 6d 65 6d 73 65 74 28 26 77 2c 20 30 2c . memset(&w, 0,
269e0 20 73 69 7a 65 6f 66 28 77 29 29 3b 0a 20 20 77 sizeof(w));. w
269f0 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b .xSelectCallback
26a00 32 20 3d 20 73 65 6c 65 63 74 41 64 64 53 75 62 2 = selectAddSub
26a10 71 75 65 72 79 54 79 70 65 49 6e 66 6f 3b 0a 20 queryTypeInfo;.
26a20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b w.xExprCallback
26a30 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b = exprWalkNoop;
26a40 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 . w.pParse = pP
26a50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 arse;. sqlite3W
26a60 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53 alkSelect(&w, pS
26a70 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69 66 0a 7d elect);.#endif.}
26a80 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f .../*.** This ro
26a90 75 74 69 6e 65 20 73 65 74 73 20 75 70 20 61 20 utine sets up a
26aa0 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 SELECT statement
26ab0 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e for processing.
26ac0 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 The.** followi
26ad0 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 ng is accomplish
26ae0 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 ed:.**.** *
26af0 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d VDBE Cursor num
26b00 62 65 72 73 20 61 72 65 20 61 73 73 69 67 6e 65 bers are assigne
26b10 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c d to all FROM-cl
26b20 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 20 ause terms..**
26b30 20 20 20 2a 20 20 45 70 68 65 6d 65 72 61 6c 20 * Ephemeral
26b40 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 Table objects ar
26b50 65 20 63 72 65 61 74 65 64 20 66 6f 72 20 61 6c e created for al
26b60 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 l FROM-clause su
26b70 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 20 20 20 bqueries..**
26b80 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 * ON and USING
26b90 20 63 6c 61 75 73 65 73 20 61 72 65 20 73 68 69 clauses are shi
26ba0 66 74 65 64 20 69 6e 74 6f 20 57 48 45 52 45 20 fted into WHERE
26bb0 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 20 20 statements.**
26bc0 20 20 2a 20 20 57 69 6c 64 63 61 72 64 73 20 22 * Wildcards "
26bd0 2a 22 20 61 6e 64 20 22 54 41 42 4c 45 2e 2a 22 *" and "TABLE.*"
26be0 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 73 20 in result sets
26bf0 61 72 65 20 65 78 70 61 6e 64 65 64 2e 0a 2a 2a are expanded..**
26c00 20 20 20 20 20 2a 20 20 49 64 65 6e 74 69 66 69 * Identifi
26c10 65 72 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f ers in expressio
26c20 6e 20 61 72 65 20 6d 61 74 63 68 65 64 20 74 6f n are matched to
26c30 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 tables..**.** T
26c40 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 74 73 his routine acts
26c50 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 recursively on
26c60 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 20 77 all subqueries w
26c70 69 74 68 69 6e 20 74 68 65 20 53 45 4c 45 43 54 ithin the SELECT
26c80 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 6c 69 74 65 ..*/.void sqlite
26c90 33 53 65 6c 65 63 74 50 72 65 70 28 0a 20 20 50 3SelectPrep(. P
26ca0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 arse *pParse,
26cb0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 /* The par
26cc0 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 ser context */.
26cd0 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 Select *p,
26ce0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 /* The S
26cf0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 ELECT statement
26d00 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a being coded. */.
26d10 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 NameContext *p
26d20 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 OuterNC /* Name
26d30 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 63 6f 6e context for con
26d40 74 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 tainer */.){. s
26d50 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 66 qlite3 *db;. if
26d60 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 29 20 ( NEVER(p==0) )
26d70 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70 return;. db = p
26d80 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
26d90 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
26da0 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 d ) return;. if
26db0 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 ( p->selFlags &
26dc0 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 20 29 SF_HasTypeInfo )
26dd0 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 return;. sqlit
26de0 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 70 e3SelectExpand(p
26df0 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 69 66 28 Parse, p);. if(
26e00 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c pParse->nErr ||
26e10 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 db->mallocFaile
26e20 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 d ) return;. sq
26e30 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 lite3ResolveSele
26e40 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 ctNames(pParse,
26e50 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 p, pOuterNC);.
26e60 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 if( pParse->nErr
26e70 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 || db->mallocFa
26e80 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 iled ) return;.
26e90 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41 64 sqlite3SelectAd
26ea0 64 54 79 70 65 49 6e 66 6f 28 70 50 61 72 73 65 dTypeInfo(pParse
26eb0 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 , p);.}../*.** R
26ec0 65 73 65 74 20 74 68 65 20 61 67 67 72 65 67 61 eset the aggrega
26ed0 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a te accumulator..
26ee0 2a 2a 0a 2a 2a 20 54 68 65 20 61 67 67 72 65 67 **.** The aggreg
26ef0 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 ate accumulator
26f00 69 73 20 61 20 73 65 74 20 6f 66 20 6d 65 6d 6f is a set of memo
26f10 72 79 20 63 65 6c 6c 73 20 74 68 61 74 20 68 6f ry cells that ho
26f20 6c 64 0a 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 ld.** intermedia
26f30 74 65 20 72 65 73 75 6c 74 73 20 77 68 69 6c 65 te results while
26f40 20 63 61 6c 63 75 6c 61 74 69 6e 67 20 61 6e 20 calculating an
26f50 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 69 73 aggregate. This
26f60 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 .** routine gene
26f70 72 61 74 65 73 20 63 6f 64 65 20 74 68 61 74 20 rates code that
26f80 73 74 6f 72 65 73 20 4e 55 4c 4c 73 20 69 6e 20 stores NULLs in
26f90 61 6c 6c 20 6f 66 20 74 68 6f 73 65 20 6d 65 6d all of those mem
26fa0 6f 72 79 0a 2a 2a 20 63 65 6c 6c 73 2e 0a 2a 2f ory.** cells..*/
26fb0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 .static void res
26fc0 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 etAccumulator(Pa
26fd0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67 67 rse *pParse, Agg
26fe0 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b Info *pAggInfo){
26ff0 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 . Vdbe *v = pPa
27000 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e rse->pVdbe;. in
27010 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41 67 t i;. struct Ag
27020 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 75 6e gInfo_func *pFun
27030 63 3b 0a 20 20 69 6e 74 20 6e 52 65 67 20 3d 20 c;. int nReg =
27040 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 pAggInfo->nFunc
27050 2b 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c + pAggInfo->nCol
27060 75 6d 6e 3b 0a 20 20 69 66 28 20 6e 52 65 67 3d umn;. if( nReg=
27070 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 =0 ) return;.#if
27080 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 def SQLITE_DEBUG
27090 0a 20 20 2f 2a 20 56 65 72 69 66 79 20 74 68 61 . /* Verify tha
270a0 74 20 61 6c 6c 20 41 67 67 49 6e 66 6f 20 72 65 t all AggInfo re
270b0 67 69 73 74 65 72 73 20 61 72 65 20 77 69 74 68 gisters are with
270c0 69 6e 20 74 68 65 20 72 61 6e 67 65 20 73 70 65 in the range spe
270d0 63 69 66 69 65 64 20 62 79 0a 20 20 2a 2a 20 41 cified by. ** A
270e0 67 67 49 6e 66 6f 2e 6d 6e 52 65 67 2e 2e 41 67 ggInfo.mnReg..Ag
270f0 67 49 6e 66 6f 2e 6d 78 52 65 67 20 2a 2f 0a 20 gInfo.mxReg */.
27100 20 61 73 73 65 72 74 28 20 6e 52 65 67 3d 3d 70 assert( nReg==p
27110 41 67 67 49 6e 66 6f 2d 3e 6d 78 52 65 67 2d 70 AggInfo->mxReg-p
27120 41 67 67 49 6e 66 6f 2d 3e 6d 6e 52 65 67 2b 31 AggInfo->mnReg+1
27130 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 );. for(i=0; i
27140 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 <pAggInfo->nColu
27150 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 mn; i++){. as
27160 73 65 72 74 28 20 70 41 67 67 49 6e 66 6f 2d 3e sert( pAggInfo->
27170 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d 3e 3d 70 41 aCol[i].iMem>=pA
27180 67 67 49 6e 66 6f 2d 3e 6d 6e 52 65 67 0a 20 20 ggInfo->mnReg.
27190 20 20 20 20 20 20 20 26 26 20 70 41 67 67 49 6e && pAggIn
271a0 66 6f 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65 6d fo->aCol[i].iMem
271b0 3c 3d 70 41 67 67 49 6e 66 6f 2d 3e 6d 78 52 65 <=pAggInfo->mxRe
271c0 67 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 g );. }. for(i
271d0 3d 30 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e =0; i<pAggInfo->
271e0 6e 46 75 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 nFunc; i++){.
271f0 20 61 73 73 65 72 74 28 20 70 41 67 67 49 6e 66 assert( pAggInf
27200 6f 2d 3e 61 46 75 6e 63 5b 69 5d 2e 69 4d 65 6d o->aFunc[i].iMem
27210 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e 6d 6e 52 65 >=pAggInfo->mnRe
27220 67 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 41 g. && pA
27230 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d ggInfo->aFunc[i]
27240 2e 69 4d 65 6d 3c 3d 70 41 67 67 49 6e 66 6f 2d .iMem<=pAggInfo-
27250 3e 6d 78 52 65 67 20 29 3b 0a 20 20 7d 0a 23 65 >mxReg );. }.#e
27260 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 56 64 ndif. sqlite3Vd
27270 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e beAddOp3(v, OP_N
27280 75 6c 6c 2c 20 30 2c 20 70 41 67 67 49 6e 66 6f ull, 0, pAggInfo
27290 2d 3e 6d 6e 52 65 67 2c 20 70 41 67 67 49 6e 66 ->mnReg, pAggInf
272a0 6f 2d 3e 6d 78 52 65 67 29 3b 0a 20 20 66 6f 72 o->mxReg);. for
272b0 28 70 46 75 6e 63 3d 70 41 67 67 49 6e 66 6f 2d (pFunc=pAggInfo-
272c0 3e 61 46 75 6e 63 2c 20 69 3d 30 3b 20 69 3c 70 >aFunc, i=0; i<p
272d0 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 AggInfo->nFunc;
272e0 69 2b 2b 2c 20 70 46 75 6e 63 2b 2b 29 7b 0a 20 i++, pFunc++){.
272f0 20 20 20 69 66 28 20 70 46 75 6e 63 2d 3e 69 44 if( pFunc->iD
27300 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20 istinct>=0 ){.
27310 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 Expr *pE = p
27320 46 75 6e 63 2d 3e 70 45 78 70 72 3b 0a 20 20 20 Func->pExpr;.
27330 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 assert( !Expr
27340 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 2c 20 HasProperty(pE,
27350 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b EP_xIsSelect) );
27360 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 78 . if( pE->x
27370 2e 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 45 2d .pList==0 || pE-
27380 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 >x.pList->nExpr!
27390 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 =1 ){. sq
273a0 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 lite3ErrorMsg(pP
273b0 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43 54 20 arse, "DISTINCT
273c0 61 67 67 72 65 67 61 74 65 73 20 6d 75 73 74 20 aggregates must
273d0 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 have exactly one
273e0 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 22 61 ". "a
273f0 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 20 20 20 rgument");.
27400 20 20 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 pFunc->iDisti
27410 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 nct = -1;.
27420 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 4b }else{. K
27430 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
27440 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 = keyInfoFromEx
27450 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 prList(pParse, p
27460 45 2d 3e 78 2e 70 4c 69 73 74 2c 20 30 2c 20 30 E->x.pList, 0, 0
27470 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 );. sqlit
27480 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
27490 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c OP_OpenEphemeral
274a0 2c 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69 6e , pFunc->iDistin
274b0 63 74 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 ct, 0, 0,.
274c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
274d0 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 (char*)pKeyI
274e0 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 nfo, P4_KEYINFO)
274f0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
27500 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 }.}../*.** Inv
27510 6f 6b 65 20 74 68 65 20 4f 50 5f 41 67 67 46 69 oke the OP_AggFi
27520 6e 61 6c 69 7a 65 20 6f 70 63 6f 64 65 20 66 6f nalize opcode fo
27530 72 20 65 76 65 72 79 20 61 67 67 72 65 67 61 74 r every aggregat
27540 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e e function.** in
27550 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73 74 72 the AggInfo str
27560 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 ucture..*/.stati
27570 63 20 76 6f 69 64 20 66 69 6e 61 6c 69 7a 65 41 c void finalizeA
27580 67 67 46 75 6e 63 74 69 6f 6e 73 28 50 61 72 73 ggFunctions(Pars
27590 65 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49 6e e *pParse, AggIn
275a0 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 fo *pAggInfo){.
275b0 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 Vdbe *v = pPars
275c0 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 e->pVdbe;. int
275d0 69 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 i;. struct AggI
275e0 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a 20 20 nfo_func *pF;.
275f0 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 67 for(i=0, pF=pAgg
27600 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70 Info->aFunc; i<p
27610 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 AggInfo->nFunc;
27620 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20 i++, pF++){.
27630 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 ExprList *pList
27640 3d 20 70 46 2d 3e 70 45 78 70 72 2d 3e 78 2e 70 = pF->pExpr->x.p
27650 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 List;. assert
27660 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 ( !ExprHasProper
27670 74 79 28 70 46 2d 3e 70 45 78 70 72 2c 20 45 50 ty(pF->pExpr, EP
27680 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 _xIsSelect) );.
27690 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
276a0 64 4f 70 34 28 76 2c 20 4f 50 5f 41 67 67 46 69 dOp4(v, OP_AggFi
276b0 6e 61 6c 2c 20 70 46 2d 3e 69 4d 65 6d 2c 20 70 nal, pF->iMem, p
276c0 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 List ? pList->nE
276d0 78 70 72 20 3a 20 30 2c 20 30 2c 0a 20 20 20 20 xpr : 0, 0,.
276e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
276f0 20 20 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75 (void*)pF->pFu
27700 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b nc, P4_FUNCDEF);
27710 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 . }.}../*.** Up
27720 64 61 74 65 20 74 68 65 20 61 63 63 75 6d 75 6c date the accumul
27730 61 74 6f 72 20 6d 65 6d 6f 72 79 20 63 65 6c 6c ator memory cell
27740 73 20 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 s for an aggrega
27750 74 65 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 te based on.** t
27760 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f he current curso
27770 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 r position..*/.s
27780 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 tatic void updat
27790 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 72 eAccumulator(Par
277a0 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49 se *pParse, AggI
277b0 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a nfo *pAggInfo){.
277c0 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 Vdbe *v = pPar
277d0 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 se->pVdbe;. int
277e0 20 69 3b 0a 20 20 69 6e 74 20 72 65 67 48 69 74 i;. int regHit
277f0 20 3d 20 30 3b 0a 20 20 69 6e 74 20 61 64 64 72 = 0;. int addr
27800 48 69 74 54 65 73 74 20 3d 20 30 3b 0a 20 20 73 HitTest = 0;. s
27810 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 truct AggInfo_fu
27820 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63 74 nc *pF;. struct
27830 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 AggInfo_col *pC
27840 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 ;.. pAggInfo->d
27850 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 irectMode = 1;.
27860 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41 67 for(i=0, pF=pAg
27870 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c gInfo->aFunc; i<
27880 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b pAggInfo->nFunc;
27890 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 i++, pF++){.
278a0 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 69 int nArg;. i
278b0 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30 3b nt addrNext = 0;
278c0 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67 67 3b . int regAgg;
278d0 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 . ExprList *p
278e0 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 List = pF->pExpr
278f0 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 61 ->x.pList;. a
27900 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 ssert( !ExprHasP
27910 72 6f 70 65 72 74 79 28 70 46 2d 3e 70 45 78 70 roperty(pF->pExp
27920 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 r, EP_xIsSelect)
27930 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 );. if( pLis
27940 74 20 29 7b 0a 20 20 20 20 20 20 6e 41 72 67 20 t ){. nArg
27950 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a = pList->nExpr;.
27960 20 20 20 20 20 20 72 65 67 41 67 67 20 3d 20 73 regAgg = s
27970 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e qlite3GetTempRan
27980 67 65 28 70 50 61 72 73 65 2c 20 6e 41 72 67 29 ge(pParse, nArg)
27990 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
279a0 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 xprCodeExprList(
279b0 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c 20 72 pParse, pList, r
279c0 65 67 41 67 67 2c 20 53 51 4c 49 54 45 5f 45 43 egAgg, SQLITE_EC
279d0 45 4c 5f 44 55 50 29 3b 0a 20 20 20 20 7d 65 6c EL_DUP);. }el
279e0 73 65 7b 0a 20 20 20 20 20 20 6e 41 72 67 20 3d se{. nArg =
279f0 20 30 3b 0a 20 20 20 20 20 20 72 65 67 41 67 67 0;. regAgg
27a00 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 = 0;. }.
27a10 69 66 28 20 70 46 2d 3e 69 44 69 73 74 69 6e 63 if( pF->iDistinc
27a20 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 64 t>=0 ){. ad
27a30 64 72 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 drNext = sqlite3
27a40 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 VdbeMakeLabel(v)
27a50 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
27a60 6e 41 72 67 3d 3d 31 20 29 3b 0a 20 20 20 20 20 nArg==1 );.
27a70 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50 codeDistinct(pP
27a80 61 72 73 65 2c 20 70 46 2d 3e 69 44 69 73 74 69 arse, pF->iDisti
27a90 6e 63 74 2c 20 61 64 64 72 4e 65 78 74 2c 20 31 nct, addrNext, 1
27aa0 2c 20 72 65 67 41 67 67 29 3b 0a 20 20 20 20 7d , regAgg);. }
27ab0 0a 20 20 20 20 69 66 28 20 70 46 2d 3e 70 46 75 . if( pF->pFu
27ac0 6e 63 2d 3e 66 75 6e 63 46 6c 61 67 73 20 26 20 nc->funcFlags &
27ad0 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 SQLITE_FUNC_NEED
27ae0 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 43 6f COLL ){. Co
27af0 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 llSeq *pColl = 0
27b00 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 ;. struct E
27b10 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 xprList_item *pI
27b20 74 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a tem;. int j
27b30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 ;. assert(
27b40 70 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a 20 pList!=0 ); /*
27b50 70 4c 69 73 74 21 3d 30 20 69 66 20 70 46 2d 3e pList!=0 if pF->
27b60 70 46 75 6e 63 20 68 61 73 20 4e 45 45 44 43 4f pFunc has NEEDCO
27b70 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 LL */. for(
27b80 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 j=0, pItem=pList
27b90 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20 26 26 20 6a ->a; !pColl && j
27ba0 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70 49 74 65 <nArg; j++, pIte
27bb0 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 43 m++){. pC
27bc0 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 oll = sqlite3Exp
27bd0 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c rCollSeq(pParse,
27be0 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a pItem->pExpr);.
27bf0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 }. if
27c00 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 ( !pColl ){.
27c10 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 50 61 72 pColl = pPar
27c20 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74 43 6f 6c se->db->pDfltCol
27c30 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 l;. }.
27c40 20 69 66 28 20 72 65 67 48 69 74 3d 3d 30 20 26 if( regHit==0 &
27c50 26 20 70 41 67 67 49 6e 66 6f 2d 3e 6e 41 63 63 & pAggInfo->nAcc
27c60 75 6d 75 6c 61 74 6f 72 20 29 20 72 65 67 48 69 umulator ) regHi
27c70 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d t = ++pParse->nM
27c80 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 em;. sqlite
27c90 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 3VdbeAddOp4(v, O
27ca0 50 5f 43 6f 6c 6c 53 65 71 2c 20 72 65 67 48 69 P_CollSeq, regHi
27cb0 74 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 20 2a t, 0, 0, (char *
27cc0 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 )pColl, P4_COLLS
27cd0 45 51 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 EQ);. }. s
27ce0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 qlite3VdbeAddOp4
27cf0 28 76 2c 20 4f 50 5f 41 67 67 53 74 65 70 2c 20 (v, OP_AggStep,
27d00 30 2c 20 72 65 67 41 67 67 2c 20 70 46 2d 3e 69 0, regAgg, pF->i
27d10 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 Mem,.
27d20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 (void
27d30 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f *)pF->pFunc, P4_
27d40 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 73 71 FUNCDEF);. sq
27d50 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 lite3VdbeChangeP
27d60 35 28 76 2c 20 28 75 38 29 6e 41 72 67 29 3b 0a 5(v, (u8)nArg);.
27d70 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 sqlite3ExprC
27d80 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e acheAffinityChan
27d90 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 41 67 ge(pParse, regAg
27da0 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 73 71 g, nArg);. sq
27db0 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 lite3ReleaseTemp
27dc0 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 Range(pParse, re
27dd0 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 gAgg, nArg);.
27de0 20 69 66 28 20 61 64 64 72 4e 65 78 74 20 29 7b if( addrNext ){
27df0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
27e00 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 beResolveLabel(v
27e10 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a 20 20 20 , addrNext);.
27e20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 sqlite3ExprCa
27e30 63 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 cheClear(pParse)
27e40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f ;. }. }.. /
27e50 2a 20 42 65 66 6f 72 65 20 70 6f 70 75 6c 61 74 * Before populat
27e60 69 6e 67 20 74 68 65 20 61 63 63 75 6d 75 6c 61 ing the accumula
27e70 74 6f 72 20 72 65 67 69 73 74 65 72 73 2c 20 63 tor registers, c
27e80 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 lear the column
27e90 63 61 63 68 65 2e 0a 20 20 2a 2a 20 4f 74 68 65 cache.. ** Othe
27ea0 72 77 69 73 65 2c 20 69 66 20 61 6e 79 20 6f 66 rwise, if any of
27eb0 20 74 68 65 20 72 65 71 75 69 72 65 64 20 63 6f the required co
27ec0 6c 75 6d 6e 20 76 61 6c 75 65 73 20 61 72 65 20 lumn values are
27ed0 61 6c 72 65 61 64 79 20 70 72 65 73 65 6e 74 20 already present
27ee0 0a 20 20 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 . ** in registe
27ef0 72 73 2c 20 73 71 6c 69 74 65 33 45 78 70 72 43 rs, sqlite3ExprC
27f00 6f 64 65 28 29 20 6d 61 79 20 75 73 65 20 4f 50 ode() may use OP
27f10 5f 53 43 6f 70 79 20 74 6f 20 63 6f 70 79 20 74 _SCopy to copy t
27f20 68 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 74 6f he value. ** to
27f30 20 70 43 2d 3e 69 4d 65 6d 2e 20 42 75 74 20 62 pC->iMem. But b
27f40 79 20 74 68 65 20 74 69 6d 65 20 74 68 65 20 76 y the time the v
27f50 61 6c 75 65 20 69 73 20 75 73 65 64 2c 20 74 68 alue is used, th
27f60 65 20 6f 72 69 67 69 6e 61 6c 20 72 65 67 69 73 e original regis
27f70 74 65 72 0a 20 20 2a 2a 20 6d 61 79 20 68 61 76 ter. ** may hav
27f80 65 20 62 65 65 6e 20 75 73 65 64 2c 20 69 6e 76 e been used, inv
27f90 61 6c 69 64 61 74 69 6e 67 20 74 68 65 20 75 6e alidating the un
27fa0 64 65 72 6c 79 69 6e 67 20 62 75 66 66 65 72 20 derlying buffer
27fb0 68 6f 6c 64 69 6e 67 20 74 68 65 0a 20 20 2a 2a holding the. **
27fc0 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 20 76 61 text or blob va
27fd0 6c 75 65 2e 20 53 65 65 20 74 69 63 6b 65 74 20 lue. See ticket
27fe0 5b 38 38 33 30 33 34 64 63 62 35 5d 2e 0a 20 20 [883034dcb5]..
27ff0 2a 2a 0a 20 20 2a 2a 20 41 6e 6f 74 68 65 72 20 **. ** Another
28000 73 6f 6c 75 74 69 6f 6e 20 77 6f 75 6c 64 20 62 solution would b
28010 65 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 e to change the
28020 4f 50 5f 53 43 6f 70 79 20 75 73 65 64 20 74 6f OP_SCopy used to
28030 20 63 6f 70 79 20 63 61 63 68 65 64 0a 20 20 2a copy cached. *
28040 2a 20 76 61 6c 75 65 73 20 74 6f 20 61 6e 20 4f * values to an O
28050 50 5f 43 6f 70 79 2e 0a 20 20 2a 2f 0a 20 20 69 P_Copy.. */. i
28060 66 28 20 72 65 67 48 69 74 20 29 7b 0a 20 20 20 f( regHit ){.
28070 20 61 64 64 72 48 69 74 54 65 73 74 20 3d 20 73 addrHitTest = s
28080 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 qlite3VdbeAddOp1
28090 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 48 69 (v, OP_If, regHi
280a0 74 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 65 t); VdbeCoverage
280b0 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 (v);. }. sqlit
280c0 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 e3ExprCacheClear
280d0 28 70 50 61 72 73 65 29 3b 0a 20 20 66 6f 72 28 (pParse);. for(
280e0 69 3d 30 2c 20 70 43 3d 70 41 67 67 49 6e 66 6f i=0, pC=pAggInfo
280f0 2d 3e 61 43 6f 6c 3b 20 69 3c 70 41 67 67 49 6e ->aCol; i<pAggIn
28100 66 6f 2d 3e 6e 41 63 63 75 6d 75 6c 61 74 6f 72 fo->nAccumulator
28110 3b 20 69 2b 2b 2c 20 70 43 2b 2b 29 7b 0a 20 20 ; i++, pC++){.
28120 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 sqlite3ExprCod
28130 65 28 70 50 61 72 73 65 2c 20 70 43 2d 3e 70 45 e(pParse, pC->pE
28140 78 70 72 2c 20 70 43 2d 3e 69 4d 65 6d 29 3b 0a xpr, pC->iMem);.
28150 20 20 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e }. pAggInfo->
28160 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b 0a directMode = 0;.
28170 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 sqlite3ExprCac
28180 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b heClear(pParse);
28190 0a 20 20 69 66 28 20 61 64 64 72 48 69 74 54 65 . if( addrHitTe
281a0 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 st ){. sqlite
281b0 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 3VdbeJumpHere(v,
281c0 20 61 64 64 72 48 69 74 54 65 73 74 29 3b 0a 20 addrHitTest);.
281d0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 }.}../*.** Add
281e0 61 20 73 69 6e 67 6c 65 20 4f 50 5f 45 78 70 6c a single OP_Expl
281f0 61 69 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 ain instruction
28200 74 6f 20 74 68 65 20 56 44 42 45 20 74 6f 20 65 to the VDBE to e
28210 78 70 6c 61 69 6e 20 61 20 73 69 6d 70 6c 65 0a xplain a simple.
28220 2a 2a 20 63 6f 75 6e 74 28 2a 29 20 71 75 65 72 ** count(*) quer
28230 79 20 28 22 53 45 4c 45 43 54 20 63 6f 75 6e 74 y ("SELECT count
28240 28 2a 29 20 46 52 4f 4d 20 70 54 61 62 22 29 2e (*) FROM pTab").
28250 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 .*/.#ifndef SQLI
28260 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a TE_OMIT_EXPLAIN.
28270 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 6c static void expl
28280 61 69 6e 53 69 6d 70 6c 65 43 6f 75 6e 74 28 0a ainSimpleCount(.
28290 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c Parse *pParse,
282a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
282b0 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 /* Parse conte
282c0 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 xt */. Table *p
282d0 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 Tab,
282e0 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 /* Table
282f0 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 20 2a being queried *
28300 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 /. Index *pIdx
28310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
28320 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 75 73 65 /* Index use
28330 64 20 74 6f 20 6f 70 74 69 6d 69 7a 65 20 73 63 d to optimize sc
28340 61 6e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 29 an, or NULL */.)
28350 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e {. if( pParse->
28360 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 explain==2 ){.
28370 20 20 69 6e 74 20 62 43 6f 76 65 72 20 3d 20 28 int bCover = (
28380 70 49 64 78 21 3d 30 20 26 26 20 28 48 61 73 52 pIdx!=0 && (HasR
28390 6f 77 69 64 28 70 54 61 62 29 20 7c 7c 20 21 49 owid(pTab) || !I
283a0 73 50 72 69 6d 61 72 79 4b 65 79 49 6e 64 65 78 sPrimaryKeyIndex
283b0 28 70 49 64 78 29 29 29 3b 0a 20 20 20 20 63 68 (pIdx)));. ch
283c0 61 72 20 2a 7a 45 71 70 20 3d 20 73 71 6c 69 74 ar *zEqp = sqlit
283d0 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 e3MPrintf(pParse
283e0 2d 3e 64 62 2c 20 22 53 43 41 4e 20 54 41 42 4c ->db, "SCAN TABL
283f0 45 20 25 73 25 73 25 73 22 2c 0a 20 20 20 20 20 E %s%s%s",.
28400 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a pTab->zName,.
28410 20 20 20 20 20 20 20 20 62 43 6f 76 65 72 20 3f bCover ?
28420 20 22 20 55 53 49 4e 47 20 43 4f 56 45 52 49 4e " USING COVERIN
28430 47 20 49 4e 44 45 58 20 22 20 3a 20 22 22 2c 0a G INDEX " : "",.
28440 20 20 20 20 20 20 20 20 62 43 6f 76 65 72 20 3f bCover ?
28450 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 3a 20 22 pIdx->zName : "
28460 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c ". );. sql
28470 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a ite3VdbeAddOp4(.
28480 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e pParse->
28490 70 56 64 62 65 2c 20 4f 50 5f 45 78 70 6c 61 69 pVdbe, OP_Explai
284a0 6e 2c 20 70 50 61 72 73 65 2d 3e 69 53 65 6c 65 n, pParse->iSele
284b0 63 74 49 64 2c 20 30 2c 20 30 2c 20 7a 45 71 70 ctId, 0, 0, zEqp
284c0 2c 20 50 34 5f 44 59 4e 41 4d 49 43 0a 20 20 20 , P4_DYNAMIC.
284d0 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a );. }.}.#else.
284e0 23 20 64 65 66 69 6e 65 20 65 78 70 6c 61 69 6e # define explain
284f0 53 69 6d 70 6c 65 43 6f 75 6e 74 28 61 2c 62 2c SimpleCount(a,b,
28500 63 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a c).#endif../*.**
28510 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 Generate code f
28520 6f 72 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 or the SELECT st
28530 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 20 69 6e atement given in
28540 20 74 68 65 20 70 20 61 72 67 75 6d 65 6e 74 2e the p argument.
28550 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 .**.** The res
28560 75 6c 74 73 20 61 72 65 20 72 65 74 75 72 6e 65 ults are returne
28570 64 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 d according to t
28580 68 65 20 53 65 6c 65 63 74 44 65 73 74 20 73 74 he SelectDest st
28590 72 75 63 74 75 72 65 2e 0a 2a 2a 20 53 65 65 20 ructure..** See
285a0 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 73 71 6c 69 comments in sqli
285b0 74 65 49 6e 74 2e 68 20 66 6f 72 20 66 75 72 74 teInt.h for furt
285c0 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e her information.
285d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 .**.** This rout
285e0 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 ine returns the
285f0 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 number of errors
28600 2e 20 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73 . If any errors
28610 20 61 72 65 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 are.** encounte
28620 72 65 64 2c 20 74 68 65 6e 20 61 6e 20 61 70 70 red, then an app
28630 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d ropriate error m
28640 65 73 73 61 67 65 20 69 73 20 6c 65 66 74 20 69 essage is left i
28650 6e 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 n.** pParse->zEr
28660 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 rMsg..**.** This
28670 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 4e 4f routine does NO
28680 54 20 66 72 65 65 20 74 68 65 20 53 65 6c 65 63 T free the Selec
28690 74 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73 t structure pass
286a0 65 64 20 69 6e 2e 20 20 54 68 65 0a 2a 2a 20 63 ed in. The.** c
286b0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 alling function
286c0 6e 65 65 64 73 20 74 6f 20 64 6f 20 74 68 61 74 needs to do that
286d0 2e 0a 2a 2f 0a 69 6e 74 20 73 71 6c 69 74 65 33 ..*/.int sqlite3
286e0 53 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65 20 Select(. Parse
286f0 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 *pParse,
28700 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 /* The parser c
28710 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 ontext */. Sele
28720 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 ct *p,
28730 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 /* The SELECT
28740 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 statement being
28750 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 53 65 6c coded. */. Sel
28760 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 ectDest *pDest
28770 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 /* What to d
28780 6f 20 77 69 74 68 20 74 68 65 20 71 75 65 72 79 o with the query
28790 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 results */.){.
287a0 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 int i, j;
287b0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
287c0 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 57 68 counters */. Wh
287d0 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b ereInfo *pWInfo;
287e0 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66 /* Return f
287f0 72 6f 6d 20 73 71 6c 69 74 65 33 57 68 65 72 65 rom sqlite3Where
28800 42 65 67 69 6e 28 29 20 2a 2f 0a 20 20 56 64 62 Begin() */. Vdb
28810 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 e *v;
28820 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 /* The virtu
28830 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 al machine under
28840 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f construction */
28850 0a 20 20 69 6e 74 20 69 73 41 67 67 3b 20 20 20 . int isAgg;
28860 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 /* Tru
28870 65 20 66 6f 72 20 73 65 6c 65 63 74 20 6c 69 73 e for select lis
28880 74 73 20 6c 69 6b 65 20 22 63 6f 75 6e 74 28 2a ts like "count(*
28890 29 22 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 )" */. ExprList
288a0 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20 20 2f *pEList; /
288b0 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e * List of column
288c0 73 20 74 6f 20 65 78 74 72 61 63 74 2e 20 2a 2f s to extract. */
288d0 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 . SrcList *pTab
288e0 4c 69 73 74 3b 20 20 20 20 20 2f 2a 20 4c 69 73 List; /* Lis
288f0 74 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20 73 t of tables to s
28900 65 6c 65 63 74 20 66 72 6f 6d 20 2a 2f 0a 20 20 elect from */.
28910 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 Expr *pWhere;
28920 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 /* The WH
28930 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 ERE clause. May
28940 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 be NULL */. Ex
28950 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 prList *pGroupBy
28960 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55 ; /* The GROU
28970 50 20 42 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 P BY clause. Ma
28980 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 y be NULL */. E
28990 78 70 72 20 2a 70 48 61 76 69 6e 67 3b 20 20 20 xpr *pHaving;
289a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 48 41 56 /* The HAV
289b0 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 ING clause. May
289c0 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e be NULL */. in
289d0 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20 20 20 t rc = 1;
289e0 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f /* Value to
289f0 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 69 return from thi
28a00 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 s function */.
28a10 44 69 73 74 69 6e 63 74 43 74 78 20 73 44 69 73 DistinctCtx sDis
28a20 74 69 6e 63 74 3b 20 2f 2a 20 49 6e 66 6f 20 6f tinct; /* Info o
28a30 6e 20 68 6f 77 20 74 6f 20 63 6f 64 65 20 74 68 n how to code th
28a40 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f e DISTINCT keywo
28a50 72 64 20 2a 2f 0a 20 20 53 6f 72 74 43 74 78 20 rd */. SortCtx
28a60 73 53 6f 72 74 3b 20 20 20 20 20 20 20 20 20 2f sSort; /
28a70 2a 20 49 6e 66 6f 20 6f 6e 20 68 6f 77 20 74 6f * Info on how to
28a80 20 63 6f 64 65 20 74 68 65 20 4f 52 44 45 52 20 code the ORDER
28a90 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 41 BY clause */. A
28aa0 67 67 49 6e 66 6f 20 73 41 67 67 49 6e 66 6f 3b ggInfo sAggInfo;
28ab0 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 /* Informa
28ac0 74 69 6f 6e 20 75 73 65 64 20 62 79 20 61 67 67 tion used by agg
28ad0 72 65 67 61 74 65 20 71 75 65 72 69 65 73 20 2a regate queries *
28ae0 2f 0a 20 20 69 6e 74 20 69 45 6e 64 3b 20 20 20 /. int iEnd;
28af0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 /* Ad
28b00 64 72 65 73 73 20 6f 66 20 74 68 65 20 65 6e 64 dress of the end
28b10 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 2a 2f of the query */
28b20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 . sqlite3 *db;
28b30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 /* The
28b40 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 database connec
28b50 74 69 6f 6e 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 tion */..#ifndef
28b60 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 SQLITE_OMIT_EXP
28b70 4c 41 49 4e 0a 20 20 69 6e 74 20 69 52 65 73 74 LAIN. int iRest
28b80 6f 72 65 53 65 6c 65 63 74 49 64 20 3d 20 70 50 oreSelectId = pP
28b90 61 72 73 65 2d 3e 69 53 65 6c 65 63 74 49 64 3b arse->iSelectId;
28ba0 0a 20 20 70 50 61 72 73 65 2d 3e 69 53 65 6c 65 . pParse->iSele
28bb0 63 74 49 64 20 3d 20 70 50 61 72 73 65 2d 3e 69 ctId = pParse->i
28bc0 4e 65 78 74 53 65 6c 65 63 74 49 64 2b 2b 3b 0a NextSelectId++;.
28bd0 23 65 6e 64 69 66 0a 0a 20 20 64 62 20 3d 20 70 #endif.. db = p
28be0 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 Parse->db;. if(
28bf0 20 70 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c p==0 || db->mal
28c00 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 locFailed || pPa
28c10 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 rse->nErr ){.
28c20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 return 1;. }.
28c30 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 if( sqlite3Auth
28c40 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 Check(pParse, SQ
28c50 4c 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 LITE_SELECT, 0,
28c60 30 2c 20 30 29 20 29 20 72 65 74 75 72 6e 20 31 0, 0) ) return 1
28c70 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 41 67 67 ;. memset(&sAgg
28c80 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 Info, 0, sizeof(
28c90 73 41 67 67 49 6e 66 6f 29 29 3b 0a 23 69 66 20 sAggInfo));.#if
28ca0 53 45 4c 45 43 54 54 52 41 43 45 5f 45 4e 41 42 SELECTTRACE_ENAB
28cb0 4c 45 44 0a 20 20 70 50 61 72 73 65 2d 3e 6e 53 LED. pParse->nS
28cc0 65 6c 65 63 74 49 6e 64 65 6e 74 2b 2b 3b 0a 20 electIndent++;.
28cd0 20 53 45 4c 45 43 54 54 52 41 43 45 28 31 2c 70 SELECTTRACE(1,p
28ce0 50 61 72 73 65 2c 70 2c 20 28 22 62 65 67 69 6e Parse,p, ("begin
28cf0 20 70 72 6f 63 65 73 73 69 6e 67 3a 5c 6e 22 29 processing:\n")
28d00 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 );. if( sqlite3
28d10 53 65 6c 65 63 74 54 72 61 63 65 20 26 20 30 78 SelectTrace & 0x
28d20 31 30 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 100 ){. sqlit
28d30 65 33 54 72 65 65 56 69 65 77 53 65 6c 65 63 74 e3TreeViewSelect
28d40 28 30 2c 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 23 (0, p, 0);. }.#
28d50 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 endif.. assert(
28d60 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 p->pOrderBy==0
28d70 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74 21 || pDest->eDest!
28d80 3d 53 52 54 5f 44 69 73 74 46 69 66 6f 20 29 3b =SRT_DistFifo );
28d90 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f . assert( p->pO
28da0 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 70 44 65 rderBy==0 || pDe
28db0 73 74 2d 3e 65 44 65 73 74 21 3d 53 52 54 5f 46 st->eDest!=SRT_F
28dc0 69 66 6f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 ifo );. assert(
28dd0 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 p->pOrderBy==0
28de0 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74 21 || pDest->eDest!
28df0 3d 53 52 54 5f 44 69 73 74 51 75 65 75 65 20 29 =SRT_DistQueue )
28e00 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 ;. assert( p->p
28e10 4f 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 70 44 OrderBy==0 || pD
28e20 65 73 74 2d 3e 65 44 65 73 74 21 3d 53 52 54 5f est->eDest!=SRT_
28e30 51 75 65 75 65 20 29 3b 0a 20 20 69 66 28 20 49 Queue );. if( I
28e40 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28 gnorableOrderby(
28e50 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 61 73 pDest) ){. as
28e60 73 65 72 74 28 70 44 65 73 74 2d 3e 65 44 65 73 sert(pDest->eDes
28e70 74 3d 3d 53 52 54 5f 45 78 69 73 74 73 20 7c 7c t==SRT_Exists ||
28e80 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 pDest->eDest==S
28e90 52 54 5f 55 6e 69 6f 6e 20 7c 7c 20 0a 20 20 20 RT_Union || .
28ea0 20 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 65 pDest->e
28eb0 44 65 73 74 3d 3d 53 52 54 5f 45 78 63 65 70 74 Dest==SRT_Except
28ec0 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74 || pDest->eDest
28ed0 3d 3d 53 52 54 5f 44 69 73 63 61 72 64 20 7c 7c ==SRT_Discard ||
28ee0 0a 20 20 20 20 20 20 20 20 20 20 20 70 44 65 73 . pDes
28ef0 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 51 75 t->eDest==SRT_Qu
28f00 65 75 65 20 20 7c 7c 20 70 44 65 73 74 2d 3e 65 eue || pDest->e
28f10 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 74 46 69 Dest==SRT_DistFi
28f20 66 6f 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 fo ||.
28f30 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 pDest->eDest==S
28f40 52 54 5f 44 69 73 74 51 75 65 75 65 20 7c 7c 20 RT_DistQueue ||
28f50 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 pDest->eDest==SR
28f60 54 5f 46 69 66 6f 29 3b 0a 20 20 20 20 2f 2a 20 T_Fifo);. /*
28f70 49 66 20 4f 52 44 45 52 20 42 59 20 6d 61 6b 65 If ORDER BY make
28f80 73 20 6e 6f 20 64 69 66 66 65 72 65 6e 63 65 20 s no difference
28f90 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20 74 68 in the output th
28fa0 65 6e 20 6e 65 69 74 68 65 72 20 64 6f 65 73 0a en neither does.
28fb0 20 20 20 20 2a 2a 20 44 49 53 54 49 4e 43 54 20 ** DISTINCT
28fc0 73 6f 20 69 74 20 63 61 6e 20 62 65 20 72 65 6d so it can be rem
28fd0 6f 76 65 64 20 74 6f 6f 2e 20 2a 2f 0a 20 20 20 oved too. */.
28fe0 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 sqlite3ExprList
28ff0 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f Delete(db, p->pO
29000 72 64 65 72 42 79 29 3b 0a 20 20 20 20 70 2d 3e rderBy);. p->
29010 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 pOrderBy = 0;.
29020 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 3d p->selFlags &=
29030 20 7e 53 46 5f 44 69 73 74 69 6e 63 74 3b 0a 20 ~SF_Distinct;.
29040 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 }. sqlite3Sele
29050 63 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 ctPrep(pParse, p
29060 2c 20 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 , 0);. memset(&
29070 73 53 6f 72 74 2c 20 30 2c 20 73 69 7a 65 6f 66 sSort, 0, sizeof
29080 28 73 53 6f 72 74 29 29 3b 0a 20 20 73 53 6f 72 (sSort));. sSor
29090 74 2e 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e t.pOrderBy = p->
290a0 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 54 61 62 pOrderBy;. pTab
290b0 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a List = p->pSrc;.
290c0 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 pEList = p->pE
290d0 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 50 61 72 List;. if( pPar
290e0 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e se->nErr || db->
290f0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a mallocFailed ){.
29100 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f goto select_
29110 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 73 41 67 67 end;. }. isAgg
29120 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 = (p->selFlags
29130 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21 & SF_Aggregate)!
29140 3d 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 =0;. assert( pE
29150 4c 69 73 74 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a List!=0 );.. /*
29160 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e Begin generatin
29170 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 g code.. */. v
29180 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 = sqlite3GetVdb
29190 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 e(pParse);. if(
291a0 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c v==0 ) goto sel
291b0 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 49 ect_end;.. /* I
291c0 66 20 77 72 69 74 69 6e 67 20 74 6f 20 6d 65 6d f writing to mem
291d0 6f 72 79 20 6f 72 20 67 65 6e 65 72 61 74 69 6e ory or generatin
291e0 67 20 61 20 73 65 74 0a 20 20 2a 2a 20 6f 6e 6c g a set. ** onl
291f0 79 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d y a single colum
29200 6e 20 6d 61 79 20 62 65 20 6f 75 74 70 75 74 2e n may be output.
29210 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 . */.#ifndef SQ
29220 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 LITE_OMIT_SUBQUE
29230 52 59 0a 20 20 69 66 28 20 63 68 65 63 6b 46 6f RY. if( checkFo
29240 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 rMultiColumnSele
29250 63 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c 20 ctError(pParse,
29260 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e 6e pDest, pEList->n
29270 45 78 70 72 29 20 29 7b 0a 20 20 20 20 67 6f 74 Expr) ){. got
29280 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 o select_end;.
29290 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 }.#endif.. /* G
292a0 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 enerate code for
292b0 20 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65 73 all sub-queries
292c0 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 in the FROM cla
292d0 75 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 use. */.#if !de
292e0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 fined(SQLITE_OMI
292f0 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 T_SUBQUERY) || !
29300 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f defined(SQLITE_O
29310 4d 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72 28 MIT_VIEW). for(
29320 69 3d 30 3b 20 21 70 2d 3e 70 50 72 69 6f 72 20 i=0; !p->pPrior
29330 26 26 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e && i<pTabList->n
29340 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 Src; i++){. s
29350 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 truct SrcList_it
29360 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61 em *pItem = &pTa
29370 62 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20 20 bList->a[i];.
29380 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 SelectDest dest
29390 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 ;. Select *pS
293a0 75 62 20 3d 20 70 49 74 65 6d 2d 3e 70 53 65 6c ub = pItem->pSel
293b0 65 63 74 3b 0a 20 20 20 20 69 6e 74 20 69 73 41 ect;. int isA
293c0 67 67 53 75 62 3b 0a 0a 20 20 20 20 69 66 28 20 ggSub;.. if(
293d0 70 53 75 62 3d 3d 30 20 29 20 63 6f 6e 74 69 6e pSub==0 ) contin
293e0 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 ue;.. /* Some
293f0 74 69 6d 65 73 20 74 68 65 20 63 6f 64 65 20 66 times the code f
29400 6f 72 20 61 20 73 75 62 71 75 65 72 79 20 77 69 or a subquery wi
29410 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 65 64 20 ll be generated
29420 6d 6f 72 65 20 74 68 61 6e 0a 20 20 20 20 2a 2a more than. **
29430 20 6f 6e 63 65 2c 20 69 66 20 74 68 65 20 73 75 once, if the su
29440 62 71 75 65 72 79 20 69 73 20 70 61 72 74 20 6f bquery is part o
29450 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 f the WHERE clau
29460 73 65 20 69 6e 20 61 20 4c 45 46 54 20 4a 4f 49 se in a LEFT JOI
29470 4e 2c 0a 20 20 20 20 2a 2a 20 66 6f 72 20 65 78 N,. ** for ex
29480 61 6d 70 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 ample. In that
29490 63 61 73 65 2c 20 64 6f 20 6e 6f 74 20 72 65 67 case, do not reg
294a0 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64 65 enerate the code
294b0 20 74 6f 20 6d 61 6e 69 66 65 73 74 0a 20 20 20 to manifest.
294c0 20 2a 2a 20 61 20 76 69 65 77 20 6f 72 20 74 68 ** a view or th
294d0 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 6f 20 e co-routine to
294e0 69 6d 70 6c 65 6d 65 6e 74 20 61 20 76 69 65 77 implement a view
294f0 2e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e 73 . The first ins
29500 74 61 6e 63 65 0a 20 20 20 20 2a 2a 20 69 73 20 tance. ** is
29510 73 75 66 66 69 63 69 65 6e 74 2c 20 74 68 6f 75 sufficient, thou
29520 67 68 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e gh the subroutin
29530 65 20 74 6f 20 6d 61 6e 69 66 65 73 74 20 74 68 e to manifest th
29540 65 20 76 69 65 77 20 64 6f 65 73 20 6e 65 65 64 e view does need
29550 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 6e . ** to be in
29560 76 6f 6b 65 64 20 61 67 61 69 6e 2e 20 2a 2f 0a voked again. */.
29570 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 61 if( pItem->a
29580 64 64 72 46 69 6c 6c 53 75 62 20 29 7b 0a 20 20 ddrFillSub ){.
29590 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 76 if( pItem->v
295a0 69 61 43 6f 72 6f 75 74 69 6e 65 3d 3d 30 20 29 iaCoroutine==0 )
295b0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
295c0 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 3VdbeAddOp2(v, O
295d0 50 5f 47 6f 73 75 62 2c 20 70 49 74 65 6d 2d 3e P_Gosub, pItem->
295e0 72 65 67 52 65 74 75 72 6e 2c 20 70 49 74 65 6d regReturn, pItem
295f0 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 29 3b 0a ->addrFillSub);.
29600 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 6f }. co
29610 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 ntinue;. }..
29620 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 /* Increment
29630 50 61 72 73 65 2e 6e 48 65 69 67 68 74 20 62 79 Parse.nHeight by
29640 20 74 68 65 20 68 65 69 67 68 74 20 6f 66 20 74 the height of t
29650 68 65 20 6c 61 72 67 65 73 74 20 65 78 70 72 65 he largest expre
29660 73 73 69 6f 6e 0a 20 20 20 20 2a 2a 20 74 72 65 ssion. ** tre
29670 65 20 72 65 66 65 72 72 65 64 20 74 6f 20 62 79 e referred to by
29680 20 74 68 69 73 2c 20 74 68 65 20 70 61 72 65 6e this, the paren
29690 74 20 73 65 6c 65 63 74 2e 20 54 68 65 20 63 68 t select. The ch
296a0 69 6c 64 20 73 65 6c 65 63 74 0a 20 20 20 20 2a ild select. *
296b0 2a 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 65 78 * may contain ex
296c0 70 72 65 73 73 69 6f 6e 20 74 72 65 65 73 20 6f pression trees o
296d0 66 20 61 74 20 6d 6f 73 74 0a 20 20 20 20 2a 2a f at most. **
296e0 20 28 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 (SQLITE_MAX_EXP
296f0 52 5f 44 45 50 54 48 2d 50 61 72 73 65 2e 6e 48 R_DEPTH-Parse.nH
29700 65 69 67 68 74 29 20 68 65 69 67 68 74 2e 20 54 eight) height. T
29710 68 69 73 20 69 73 20 61 20 62 69 74 0a 20 20 20 his is a bit.
29720 20 2a 2a 20 6d 6f 72 65 20 63 6f 6e 73 65 72 76 ** more conserv
29730 61 74 69 76 65 20 74 68 61 6e 20 6e 65 63 65 73 ative than neces
29740 73 61 72 79 2c 20 62 75 74 20 6d 75 63 68 20 65 sary, but much e
29750 61 73 69 65 72 20 74 68 61 6e 20 65 6e 66 6f 72 asier than enfor
29760 63 69 6e 67 0a 20 20 20 20 2a 2a 20 61 6e 20 65 cing. ** an e
29770 78 61 63 74 20 6c 69 6d 69 74 2e 0a 20 20 20 20 xact limit..
29780 2a 2f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e */. pParse->n
29790 48 65 69 67 68 74 20 2b 3d 20 73 71 6c 69 74 65 Height += sqlite
297a0 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 3SelectExprHeigh
297b0 74 28 70 29 3b 0a 0a 20 20 20 20 69 73 41 67 67 t(p);.. isAgg
297c0 53 75 62 20 3d 20 28 70 53 75 62 2d 3e 73 65 6c Sub = (pSub->sel
297d0 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65 Flags & SF_Aggre
297e0 67 61 74 65 29 21 3d 30 3b 0a 20 20 20 20 69 66 gate)!=0;. if
297f0 28 20 66 6c 61 74 74 65 6e 53 75 62 71 75 65 72 ( flattenSubquer
29800 79 28 70 50 61 72 73 65 2c 20 70 2c 20 69 2c 20 y(pParse, p, i,
29810 69 73 41 67 67 2c 20 69 73 41 67 67 53 75 62 29 isAgg, isAggSub)
29820 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 ){. /* Thi
29830 73 20 73 75 62 71 75 65 72 79 20 63 61 6e 20 62 s subquery can b
29840 65 20 61 62 73 6f 72 62 65 64 20 69 6e 74 6f 20 e absorbed into
29850 69 74 73 20 70 61 72 65 6e 74 2e 20 2a 2f 0a 20 its parent. */.
29860 20 20 20 20 20 69 66 28 20 69 73 41 67 67 53 75 if( isAggSu
29870 62 20 29 7b 0a 20 20 20 20 20 20 20 20 69 73 41 b ){. isA
29880 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 gg = 1;.
29890 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 p->selFlags |= S
298a0 46 5f 41 67 67 72 65 67 61 74 65 3b 0a 20 20 20 F_Aggregate;.
298b0 20 20 20 7d 0a 20 20 20 20 20 20 69 20 3d 20 2d }. i = -
298c0 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 1;. }else if(
298d0 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d pTabList->nSrc=
298e0 3d 31 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 =1. &&
298f0 20 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 OptimizationEna
29900 62 6c 65 64 28 64 62 2c 20 53 51 4c 49 54 45 5f bled(db, SQLITE_
29910 53 75 62 71 43 6f 72 6f 75 74 69 6e 65 29 0a 20 SubqCoroutine).
29920 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 ){. /* I
29930 6d 70 6c 65 6d 65 6e 74 20 61 20 63 6f 2d 72 6f mplement a co-ro
29940 75 74 69 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 utine that will
29950 72 65 74 75 72 6e 20 61 20 73 69 6e 67 6c 65 20 return a single
29960 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c row of the resul
29970 74 0a 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6f t. ** set o
29980 6e 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f n each invocatio
29990 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 n.. */.
299a0 20 20 69 6e 74 20 61 64 64 72 54 6f 70 20 3d 20 int addrTop =
299b0 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 sqlite3VdbeCurre
299c0 6e 74 41 64 64 72 28 76 29 2b 31 3b 0a 20 20 20 ntAddr(v)+1;.
299d0 20 20 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 74 pItem->regRet
299e0 75 72 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e urn = ++pParse->
299f0 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 nMem;. sqli
29a00 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
29a10 20 4f 50 5f 49 6e 69 74 43 6f 72 6f 75 74 69 6e OP_InitCoroutin
29a20 65 2c 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 74 e, pItem->regRet
29a30 75 72 6e 2c 20 30 2c 20 61 64 64 72 54 6f 70 29 urn, 0, addrTop)
29a40 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d ;. VdbeComm
29a50 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 ent((v, "%s", pI
29a60 74 65 6d 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 tem->pTab->zName
29a70 29 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d ));. pItem-
29a80 3e 61 64 64 72 46 69 6c 6c 53 75 62 20 3d 20 61 >addrFillSub = a
29a90 64 64 72 54 6f 70 3b 0a 20 20 20 20 20 20 73 71 ddrTop;. sq
29aa0 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 lite3SelectDestI
29ab0 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 43 nit(&dest, SRT_C
29ac0 6f 72 6f 75 74 69 6e 65 2c 20 70 49 74 65 6d 2d oroutine, pItem-
29ad0 3e 72 65 67 52 65 74 75 72 6e 29 3b 0a 20 20 20 >regReturn);.
29ae0 20 20 20 65 78 70 6c 61 69 6e 53 65 74 49 6e 74 explainSetInt
29af0 65 67 65 72 28 70 49 74 65 6d 2d 3e 69 53 65 6c eger(pItem->iSel
29b00 65 63 74 49 64 2c 20 28 75 38 29 70 50 61 72 73 ectId, (u8)pPars
29b10 65 2d 3e 69 4e 65 78 74 53 65 6c 65 63 74 49 64 e->iNextSelectId
29b20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
29b30 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 Select(pParse, p
29b40 53 75 62 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 Sub, &dest);.
29b50 20 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d 3e pItem->pTab->
29b60 6e 52 6f 77 4c 6f 67 45 73 74 20 3d 20 73 71 6c nRowLogEst = sql
29b70 69 74 65 33 4c 6f 67 45 73 74 28 70 53 75 62 2d ite3LogEst(pSub-
29b80 3e 6e 53 65 6c 65 63 74 52 6f 77 29 3b 0a 20 20 >nSelectRow);.
29b90 20 20 20 20 70 49 74 65 6d 2d 3e 76 69 61 43 6f pItem->viaCo
29ba0 72 6f 75 74 69 6e 65 20 3d 20 31 3b 0a 20 20 20 routine = 1;.
29bb0 20 20 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 73 pItem->regRes
29bc0 75 6c 74 20 3d 20 64 65 73 74 2e 69 53 64 73 74 ult = dest.iSdst
29bd0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
29be0 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f dbeAddOp1(v, OP_
29bf0 45 6e 64 43 6f 72 6f 75 74 69 6e 65 2c 20 70 49 EndCoroutine, pI
29c00 74 65 6d 2d 3e 72 65 67 52 65 74 75 72 6e 29 3b tem->regReturn);
29c10 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 . sqlite3Vd
29c20 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 beJumpHere(v, ad
29c30 64 72 54 6f 70 2d 31 29 3b 0a 20 20 20 20 20 20 drTop-1);.
29c40 73 71 6c 69 74 65 33 43 6c 65 61 72 54 65 6d 70 sqlite3ClearTemp
29c50 52 65 67 43 61 63 68 65 28 70 50 61 72 73 65 29 RegCache(pParse)
29c60 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
29c70 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 /* Generate a
29c80 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 subroutine that
29c90 20 77 69 6c 6c 20 66 69 6c 6c 20 61 6e 20 65 70 will fill an ep
29ca0 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 77 69 hemeral table wi
29cb0 74 68 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 th. ** the
29cc0 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 content of this
29cd0 73 75 62 71 75 65 72 79 2e 20 20 70 49 74 65 6d subquery. pItem
29ce0 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 20 77 69 ->addrFillSub wi
29cf0 6c 6c 20 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a ll point. *
29d00 2a 20 74 6f 20 74 68 65 20 61 64 64 72 65 73 73 * to the address
29d10 20 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74 65 of the generate
29d20 64 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 70 d subroutine. p
29d30 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 72 6e 0a Item->regReturn.
29d40 20 20 20 20 20 20 2a 2a 20 69 73 20 61 20 72 65 ** is a re
29d50 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74 65 64 gister allocated
29d60 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62 to hold the sub
29d70 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 20 61 routine return a
29d80 64 64 72 65 73 73 0a 20 20 20 20 20 20 2a 2f 0a ddress. */.
29d90 20 20 20 20 20 20 69 6e 74 20 74 6f 70 41 64 64 int topAdd
29da0 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 6f 6e 63 r;. int onc
29db0 65 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 eAddr = 0;.
29dc0 20 69 6e 74 20 72 65 74 41 64 64 72 3b 0a 20 20 int retAddr;.
29dd0 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 assert( pIte
29de0 6d 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 3d 3d m->addrFillSub==
29df0 30 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 0 );. pItem
29e00 2d 3e 72 65 67 52 65 74 75 72 6e 20 3d 20 2b 2b ->regReturn = ++
29e10 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 pParse->nMem;.
29e20 20 20 20 20 74 6f 70 41 64 64 72 20 3d 20 73 71 topAddr = sq
29e30 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 lite3VdbeAddOp2(
29e40 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 v, OP_Integer, 0
29e50 2c 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 , pItem->regRetu
29e60 72 6e 29 3b 0a 20 20 20 20 20 20 70 49 74 65 6d rn);. pItem
29e70 2d 3e 61 64 64 72 46 69 6c 6c 53 75 62 20 3d 20 ->addrFillSub =
29e80 74 6f 70 41 64 64 72 2b 31 3b 0a 20 20 20 20 20 topAddr+1;.
29e90 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 73 43 6f if( pItem->isCo
29ea0 72 72 65 6c 61 74 65 64 3d 3d 30 20 29 7b 0a 20 rrelated==0 ){.
29eb0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 /* If the
29ec0 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 subquery is not
29ed0 20 63 6f 72 72 65 6c 61 74 65 64 20 61 6e 64 20 correlated and
29ee0 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e if we are not in
29ef0 73 69 64 65 20 6f 66 0a 20 20 20 20 20 20 20 20 side of.
29f00 2a 2a 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 ** a trigger, th
29f10 65 6e 20 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20 en we only need
29f20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20 76 to compute the v
29f30 61 6c 75 65 20 6f 66 20 74 68 65 20 73 75 62 71 alue of the subq
29f40 75 65 72 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 uery. **
29f50 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 once. */.
29f60 20 6f 6e 63 65 41 64 64 72 20 3d 20 73 71 6c 69 onceAddr = sqli
29f70 74 65 33 43 6f 64 65 4f 6e 63 65 28 70 50 61 72 te3CodeOnce(pPar
29f80 73 65 29 3b 20 56 64 62 65 43 6f 76 65 72 61 67 se); VdbeCoverag
29f90 65 28 76 29 3b 0a 20 20 20 20 20 20 20 20 56 64 e(v);. Vd
29fa0 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6d beComment((v, "m
29fb0 61 74 65 72 69 61 6c 69 7a 65 20 5c 22 25 73 5c aterialize \"%s\
29fc0 22 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d "", pItem->pTab-
29fd0 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 >zName));.
29fe0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 56 }else{. V
29ff0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 dbeNoopComment((
2a000 76 2c 20 22 6d 61 74 65 72 69 61 6c 69 7a 65 20 v, "materialize
2a010 5c 22 25 73 5c 22 22 2c 20 70 49 74 65 6d 2d 3e \"%s\"", pItem->
2a020 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 pTab->zName));.
2a030 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c }. sql
2a040 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e ite3SelectDestIn
2a050 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70 it(&dest, SRT_Ep
2a060 68 65 6d 54 61 62 2c 20 70 49 74 65 6d 2d 3e 69 hemTab, pItem->i
2a070 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 65 Cursor);. e
2a080 78 70 6c 61 69 6e 53 65 74 49 6e 74 65 67 65 72 xplainSetInteger
2a090 28 70 49 74 65 6d 2d 3e 69 53 65 6c 65 63 74 49 (pItem->iSelectI
2a0a0 64 2c 20 28 75 38 29 70 50 61 72 73 65 2d 3e 69 d, (u8)pParse->i
2a0b0 4e 65 78 74 53 65 6c 65 63 74 49 64 29 3b 0a 20 NextSelectId);.
2a0c0 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 sqlite3Sele
2a0d0 63 74 28 70 50 61 72 73 65 2c 20 70 53 75 62 2c ct(pParse, pSub,
2a0e0 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 &dest);. p
2a0f0 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 6e 52 6f 77 Item->pTab->nRow
2a100 4c 6f 67 45 73 74 20 3d 20 73 71 6c 69 74 65 33 LogEst = sqlite3
2a110 4c 6f 67 45 73 74 28 70 53 75 62 2d 3e 6e 53 65 LogEst(pSub->nSe
2a120 6c 65 63 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 lectRow);.
2a130 69 66 28 20 6f 6e 63 65 41 64 64 72 20 29 20 73 if( onceAddr ) s
2a140 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 qlite3VdbeJumpHe
2a150 72 65 28 76 2c 20 6f 6e 63 65 41 64 64 72 29 3b re(v, onceAddr);
2a160 0a 20 20 20 20 20 20 72 65 74 41 64 64 72 20 3d . retAddr =
2a170 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2a180 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c p1(v, OP_Return,
2a190 20 70 49 74 65 6d 2d 3e 72 65 67 52 65 74 75 72 pItem->regRetur
2a1a0 6e 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f n);. VdbeCo
2a1b0 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20 25 mment((v, "end %
2a1c0 73 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d s", pItem->pTab-
2a1d0 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 >zName));.
2a1e0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 sqlite3VdbeChang
2a1f0 65 50 31 28 76 2c 20 74 6f 70 41 64 64 72 2c 20 eP1(v, topAddr,
2a200 72 65 74 41 64 64 72 29 3b 0a 20 20 20 20 20 20 retAddr);.
2a210 73 71 6c 69 74 65 33 43 6c 65 61 72 54 65 6d 70 sqlite3ClearTemp
2a220 52 65 67 43 61 63 68 65 28 70 50 61 72 73 65 29 RegCache(pParse)
2a230 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 ;. }. if(
2a240 2f 2a 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c /*pParse->nErr |
2a250 7c 2a 2f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 |*/ db->mallocFa
2a260 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f iled ){. go
2a270 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 to select_end;.
2a280 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d }. pParse-
2a290 3e 6e 48 65 69 67 68 74 20 2d 3d 20 73 71 6c 69 >nHeight -= sqli
2a2a0 74 65 33 53 65 6c 65 63 74 45 78 70 72 48 65 69 te3SelectExprHei
2a2b0 67 68 74 28 70 29 3b 0a 20 20 20 20 70 54 61 62 ght(p);. pTab
2a2c0 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a List = p->pSrc;.
2a2d0 20 20 20 20 69 66 28 20 21 49 67 6e 6f 72 61 62 if( !Ignorab
2a2e0 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73 74 29 leOrderby(pDest)
2a2f0 20 29 7b 0a 20 20 20 20 20 20 73 53 6f 72 74 2e ){. sSort.
2a300 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f pOrderBy = p->pO
2a310 72 64 65 72 42 79 3b 0a 20 20 20 20 7d 0a 20 20 rderBy;. }.
2a320 7d 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e }. pEList = p->
2a330 70 45 4c 69 73 74 3b 0a 23 65 6e 64 69 66 0a 20 pEList;.#endif.
2a340 20 70 57 68 65 72 65 20 3d 20 70 2d 3e 70 57 68 pWhere = p->pWh
2a350 65 72 65 3b 0a 20 20 70 47 72 6f 75 70 42 79 20 ere;. pGroupBy
2a360 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 = p->pGroupBy;.
2a370 20 70 48 61 76 69 6e 67 20 3d 20 70 2d 3e 70 48 pHaving = p->pH
2a380 61 76 69 6e 67 3b 0a 20 20 73 44 69 73 74 69 6e aving;. sDistin
2a390 63 74 2e 69 73 54 6e 63 74 20 3d 20 28 70 2d 3e ct.isTnct = (p->
2a3a0 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 selFlags & SF_Di
2a3b0 73 74 69 6e 63 74 29 21 3d 30 3b 0a 0a 23 69 66 stinct)!=0;..#if
2a3c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 ndef SQLITE_OMIT
2a3d0 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 _COMPOUND_SELECT
2a3e0 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 . /* If there i
2a3f0 73 20 61 72 65 20 61 20 73 65 71 75 65 6e 63 65 s are a sequence
2a400 20 6f 66 20 71 75 65 72 69 65 73 2c 20 64 6f 20 of queries, do
2a410 74 68 65 20 65 61 72 6c 69 65 72 20 6f 6e 65 73 the earlier ones
2a420 20 66 69 72 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 first.. */. i
2a430 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a f( p->pPrior ){.
2a440 20 20 20 20 72 63 20 3d 20 6d 75 6c 74 69 53 65 rc = multiSe
2a450 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 lect(pParse, p,
2a460 70 44 65 73 74 29 3b 0a 20 20 20 20 65 78 70 6c pDest);. expl
2a470 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 70 50 ainSetInteger(pP
2a480 61 72 73 65 2d 3e 69 53 65 6c 65 63 74 49 64 2c arse->iSelectId,
2a490 20 69 52 65 73 74 6f 72 65 53 65 6c 65 63 74 49 iRestoreSelectI
2a4a0 64 29 3b 0a 23 69 66 20 53 45 4c 45 43 54 54 52 d);.#if SELECTTR
2a4b0 41 43 45 5f 45 4e 41 42 4c 45 44 0a 20 20 20 20 ACE_ENABLED.
2a4c0 53 45 4c 45 43 54 54 52 41 43 45 28 31 2c 70 50 SELECTTRACE(1,pP
2a4d0 61 72 73 65 2c 70 2c 28 22 65 6e 64 20 63 6f 6d arse,p,("end com
2a4e0 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 70 72 6f pound-select pro
2a4f0 63 65 73 73 69 6e 67 5c 6e 22 29 29 3b 0a 20 20 cessing\n"));.
2a500 20 20 70 50 61 72 73 65 2d 3e 6e 53 65 6c 65 63 pParse->nSelec
2a510 74 49 6e 64 65 6e 74 2d 2d 3b 0a 23 65 6e 64 69 tIndent--;.#endi
2a520 66 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b f. return rc;
2a530 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f . }.#endif.. /
2a540 2a 20 49 66 20 74 68 65 20 71 75 65 72 79 20 69 * If the query i
2a550 73 20 44 49 53 54 49 4e 43 54 20 77 69 74 68 20 s DISTINCT with
2a560 61 6e 20 4f 52 44 45 52 20 42 59 20 62 75 74 20 an ORDER BY but
2a570 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 is not an aggreg
2a580 61 74 65 2c 20 61 6e 64 20 0a 20 20 2a 2a 20 69 ate, and . ** i
2a590 66 20 74 68 65 20 73 65 6c 65 63 74 2d 6c 69 73 f the select-lis
2a5a0 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 t is the same as
2a5b0 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6c 69 the ORDER BY li
2a5c0 73 74 2c 20 74 68 65 6e 20 74 68 69 73 20 71 75 st, then this qu
2a5d0 65 72 79 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 ery. ** can be
2a5e0 72 65 77 72 69 74 74 65 6e 20 61 73 20 61 20 47 rewritten as a G
2a5f0 52 4f 55 50 20 42 59 2e 20 49 6e 20 6f 74 68 65 ROUP BY. In othe
2a600 72 20 77 6f 72 64 73 2c 20 74 68 69 73 3a 0a 20 r words, this:.
2a610 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c **. ** SEL
2a620 45 43 54 20 44 49 53 54 49 4e 43 54 20 78 79 7a ECT DISTINCT xyz
2a630 20 46 52 4f 4d 20 2e 2e 2e 20 4f 52 44 45 52 20 FROM ... ORDER
2a640 42 59 20 78 79 7a 0a 20 20 2a 2a 0a 20 20 2a 2a BY xyz. **. **
2a650 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 is transformed
2a660 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 to:. **. **
2a670 20 20 53 45 4c 45 43 54 20 78 79 7a 20 46 52 4f SELECT xyz FRO
2a680 4d 20 2e 2e 2e 20 47 52 4f 55 50 20 42 59 20 78 M ... GROUP BY x
2a690 79 7a 20 4f 52 44 45 52 20 42 59 20 78 79 7a 0a yz ORDER BY xyz.
2a6a0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 65 **. ** The se
2a6b0 63 6f 6e 64 20 66 6f 72 6d 20 69 73 20 70 72 65 cond form is pre
2a6c0 66 65 72 72 65 64 20 61 73 20 61 20 73 69 6e 67 ferred as a sing
2a6d0 6c 65 20 69 6e 64 65 78 20 28 6f 72 20 74 65 6d le index (or tem
2a6e0 70 2d 74 61 62 6c 65 29 20 6d 61 79 20 62 65 20 p-table) may be
2a6f0 0a 20 20 2a 2a 20 75 73 65 64 20 66 6f 72 20 62 . ** used for b
2a700 6f 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 oth the ORDER BY
2a710 20 61 6e 64 20 44 49 53 54 49 4e 43 54 20 70 72 and DISTINCT pr
2a720 6f 63 65 73 73 69 6e 67 2e 20 41 73 20 6f 72 69 ocessing. As ori
2a730 67 69 6e 61 6c 6c 79 20 0a 20 20 2a 2a 20 77 72 ginally . ** wr
2a740 69 74 74 65 6e 20 74 68 65 20 71 75 65 72 79 20 itten the query
2a750 6d 75 73 74 20 75 73 65 20 61 20 74 65 6d 70 2d must use a temp-
2a760 74 61 62 6c 65 20 66 6f 72 20 61 74 20 6c 65 61 table for at lea
2a770 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52 st one of the OR
2a780 44 45 52 20 0a 20 20 2a 2a 20 42 59 20 61 6e 64 DER . ** BY and
2a790 20 44 49 53 54 49 4e 43 54 2c 20 61 6e 64 20 61 DISTINCT, and a
2a7a0 6e 20 69 6e 64 65 78 20 6f 72 20 73 65 70 61 72 n index or separ
2a7b0 61 74 65 20 74 65 6d 70 2d 74 61 62 6c 65 20 66 ate temp-table f
2a7c0 6f 72 20 74 68 65 20 6f 74 68 65 72 2e 0a 20 20 or the other..
2a7d0 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c */. if( (p->sel
2a7e0 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 Flags & (SF_Dist
2a7f0 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 inct|SF_Aggregat
2a800 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74 e))==SF_Distinct
2a810 20 0a 20 20 20 26 26 20 73 71 6c 69 74 65 33 45 . && sqlite3E
2a820 78 70 72 4c 69 73 74 43 6f 6d 70 61 72 65 28 73 xprListCompare(s
2a830 53 6f 72 74 2e 70 4f 72 64 65 72 42 79 2c 20 70 Sort.pOrderBy, p
2a840 2d 3e 70 45 4c 69 73 74 2c 20 2d 31 29 3d 3d 30 ->pEList, -1)==0
2a850 0a 20 20 29 7b 0a 20 20 20 20 70 2d 3e 73 65 6c . ){. p->sel
2a860 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44 69 73 Flags &= ~SF_Dis
2a870 74 69 6e 63 74 3b 0a 20 20 20 20 70 2d 3e 70 47 tinct;. p->pG
2a880 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33 roupBy = sqlite3
2a890 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 ExprListDup(db,
2a8a0 70 2d 3e 70 45 4c 69 73 74 2c 20 30 29 3b 0a 20 p->pEList, 0);.
2a8b0 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d pGroupBy = p-
2a8c0 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 2f >pGroupBy;. /
2a8d0 2a 20 4e 6f 74 69 63 65 20 74 68 61 74 20 65 76 * Notice that ev
2a8e0 65 6e 20 74 68 6f 75 67 68 74 20 53 46 5f 44 69 en thought SF_Di
2a8f0 73 74 69 6e 63 74 20 68 61 73 20 62 65 65 6e 20 stinct has been
2a900 63 6c 65 61 72 65 64 20 66 72 6f 6d 20 70 2d 3e cleared from p->
2a910 73 65 6c 46 6c 61 67 73 2c 0a 20 20 20 20 2a 2a selFlags,. **
2a920 20 74 68 65 20 73 44 69 73 74 69 6e 63 74 2e 69 the sDistinct.i
2a930 73 54 6e 63 74 20 69 73 20 73 74 69 6c 6c 20 73 sTnct is still s
2a940 65 74 2e 20 20 48 65 6e 63 65 2c 20 69 73 54 6e et. Hence, isTn
2a950 63 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 ct represents th
2a960 65 0a 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 61 e. ** origina
2a970 6c 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 l setting of the
2a980 20 53 46 5f 44 69 73 74 69 6e 63 74 20 66 6c 61 SF_Distinct fla
2a990 67 2c 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65 g, not the curre
2a9a0 6e 74 20 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 nt setting */.
2a9b0 20 20 61 73 73 65 72 74 28 20 73 44 69 73 74 69 assert( sDisti
2a9c0 6e 63 74 2e 69 73 54 6e 63 74 20 29 3b 0a 20 20 nct.isTnct );.
2a9d0 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 }.. /* If there
2a9e0 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 is an ORDER BY
2a9f0 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 68 69 clause, then thi
2aa00 73 20 73 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 69 s sorting. ** i
2aa10 6e 64 65 78 20 6d 69 67 68 74 20 65 6e 64 20 75 ndex might end u
2aa20 70 20 62 65 69 6e 67 20 75 6e 75 73 65 64 20 69 p being unused i
2aa30 66 20 74 68 65 20 64 61 74 61 20 63 61 6e 20 62 f the data can b
2aa40 65 20 0a 20 20 2a 2a 20 65 78 74 72 61 63 74 65 e . ** extracte
2aa50 64 20 69 6e 20 70 72 65 2d 73 6f 72 74 65 64 20 d in pre-sorted
2aa60 6f 72 64 65 72 2e 20 20 49 66 20 74 68 61 74 20 order. If that
2aa70 69 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 is the case, the
2aa80 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 50 5f 4f 70 n the. ** OP_Op
2aa90 65 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 enEphemeral inst
2aaa0 72 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 ruction will be
2aab0 63 68 61 6e 67 65 64 20 74 6f 20 61 6e 20 4f 50 changed to an OP
2aac0 5f 4e 6f 6f 70 20 6f 6e 63 65 0a 20 20 2a 2a 20 _Noop once. **
2aad0 77 65 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 we figure out th
2aae0 61 74 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 at the sorting i
2aaf0 6e 64 65 78 20 69 73 20 6e 6f 74 20 6e 65 65 64 ndex is not need
2ab00 65 64 2e 20 20 54 68 65 20 61 64 64 72 53 6f 72 ed. The addrSor
2ab10 74 49 6e 64 65 78 0a 20 20 2a 2a 20 76 61 72 69 tIndex. ** vari
2ab20 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 able is used to
2ab30 66 61 63 69 6c 69 74 61 74 65 20 74 68 61 74 20 facilitate that
2ab40 63 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 change.. */. i
2ab50 66 28 20 73 53 6f 72 74 2e 70 4f 72 64 65 72 42 f( sSort.pOrderB
2ab60 79 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f y ){. KeyInfo
2ab70 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 *pKeyInfo;.
2ab80 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65 79 49 6e pKeyInfo = keyIn
2ab90 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 foFromExprList(p
2aba0 50 61 72 73 65 2c 20 73 53 6f 72 74 2e 70 4f 72 Parse, sSort.pOr
2abb0 64 65 72 42 79 2c 20 30 2c 20 30 29 3b 0a 20 20 derBy, 0, 0);.
2abc0 20 20 73 53 6f 72 74 2e 69 45 43 75 72 73 6f 72 sSort.iECursor
2abd0 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b = pParse->nTab+
2abe0 2b 3b 0a 20 20 20 20 73 53 6f 72 74 2e 61 64 64 +;. sSort.add
2abf0 72 53 6f 72 74 49 6e 64 65 78 20 3d 0a 20 20 20 rSortIndex =.
2ac00 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2ac10 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 dOp4(v, OP_OpenE
2ac20 70 68 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20 20 phemeral,.
2ac30 20 20 20 20 73 53 6f 72 74 2e 69 45 43 75 72 73 sSort.iECurs
2ac40 6f 72 2c 20 73 53 6f 72 74 2e 70 4f 72 64 65 72 or, sSort.pOrder
2ac50 42 79 2d 3e 6e 45 78 70 72 2b 31 2b 70 45 4c 69 By->nExpr+1+pELi
2ac60 73 74 2d 3e 6e 45 78 70 72 2c 20 30 2c 0a 20 20 st->nExpr, 0,.
2ac70 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 (char*)p
2ac80 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 KeyInfo, P4_KEYI
2ac90 4e 46 4f 0a 20 20 20 20 20 20 29 3b 0a 20 20 7d NFO. );. }
2aca0 65 6c 73 65 7b 0a 20 20 20 20 73 53 6f 72 74 2e else{. sSort.
2acb0 61 64 64 72 53 6f 72 74 49 6e 64 65 78 20 3d 20 addrSortIndex =
2acc0 2d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 -1;. }.. /* If
2acd0 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 64 the output is d
2ace0 65 73 74 69 6e 65 64 20 66 6f 72 20 61 20 74 65 estined for a te
2acf0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 6f mporary table, o
2ad00 70 65 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 0a pen that table..
2ad10 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65 73 74 */. if( pDest
2ad20 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 ->eDest==SRT_Eph
2ad30 65 6d 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c emTab ){. sql
2ad40 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
2ad50 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 , OP_OpenEphemer
2ad60 61 6c 2c 20 70 44 65 73 74 2d 3e 69 53 44 50 61 al, pDest->iSDPa
2ad70 72 6d 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 rm, pEList->nExp
2ad80 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 r);. }.. /* Se
2ad90 74 20 74 68 65 20 6c 69 6d 69 74 65 72 2e 0a 20 t the limiter..
2ada0 20 2a 2f 0a 20 20 69 45 6e 64 20 3d 20 73 71 6c */. iEnd = sql
2adb0 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 ite3VdbeMakeLabe
2adc0 6c 28 76 29 3b 0a 20 20 70 2d 3e 6e 53 65 6c 65 l(v);. p->nSele
2add0 63 74 52 6f 77 20 3d 20 4c 41 52 47 45 53 54 5f ctRow = LARGEST_
2ade0 49 4e 54 36 34 3b 0a 20 20 63 6f 6d 70 75 74 65 INT64;. compute
2adf0 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 LimitRegisters(p
2ae00 50 61 72 73 65 2c 20 70 2c 20 69 45 6e 64 29 3b Parse, p, iEnd);
2ae10 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 . if( p->iLimit
2ae20 3d 3d 30 20 26 26 20 73 53 6f 72 74 2e 61 64 64 ==0 && sSort.add
2ae30 72 53 6f 72 74 49 6e 64 65 78 3e 3d 30 20 29 7b rSortIndex>=0 ){
2ae40 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 . sqlite3Vdbe
2ae50 47 65 74 4f 70 28 76 2c 20 73 53 6f 72 74 2e 61 GetOp(v, sSort.a
2ae60 64 64 72 53 6f 72 74 49 6e 64 65 78 29 2d 3e 6f ddrSortIndex)->o
2ae70 70 63 6f 64 65 20 3d 20 4f 50 5f 53 6f 72 74 65 pcode = OP_Sorte
2ae80 72 4f 70 65 6e 3b 0a 20 20 20 20 73 53 6f 72 74 rOpen;. sSort
2ae90 2e 73 6f 72 74 46 6c 61 67 73 20 7c 3d 20 53 4f .sortFlags |= SO
2aea0 52 54 46 4c 41 47 5f 55 73 65 53 6f 72 74 65 72 RTFLAG_UseSorter
2aeb0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e ;. }.. /* Open
2aec0 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78 a virtual index
2aed0 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 to use for the
2aee0 64 69 73 74 69 6e 63 74 20 73 65 74 2e 0a 20 20 distinct set..
2aef0 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 */. if( p->selF
2af00 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e lags & SF_Distin
2af10 63 74 20 29 7b 0a 20 20 20 20 73 44 69 73 74 69 ct ){. sDisti
2af20 6e 63 74 2e 74 61 62 54 6e 63 74 20 3d 20 70 50 nct.tabTnct = pP
2af30 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 arse->nTab++;.
2af40 20 20 73 44 69 73 74 69 6e 63 74 2e 61 64 64 72 sDistinct.addr
2af50 54 6e 63 74 20 3d 20 73 71 6c 69 74 65 33 56 64 Tnct = sqlite3Vd
2af60 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f beAddOp4(v, OP_O
2af70 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 0a 20 20 penEphemeral,.
2af80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2af90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 44 sD
2afa0 69 73 74 69 6e 63 74 2e 74 61 62 54 6e 63 74 2c istinct.tabTnct,
2afb0 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 0, 0,.
2afc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2afd0 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 6b 65 (char*)ke
2afe0 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73 yInfoFromExprLis
2aff0 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c t(pParse, p->pEL
2b000 69 73 74 2c 30 2c 30 29 2c 0a 20 20 20 20 20 20 ist,0,0),.
2b010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2b020 20 20 20 20 20 20 20 20 20 20 50 34 5f 4b 45 59 P4_KEY
2b030 49 4e 46 4f 29 3b 0a 20 20 20 20 73 71 6c 69 74 INFO);. sqlit
2b040 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 e3VdbeChangeP5(v
2b050 2c 20 42 54 52 45 45 5f 55 4e 4f 52 44 45 52 45 , BTREE_UNORDERE
2b060 44 29 3b 0a 20 20 20 20 73 44 69 73 74 69 6e 63 D);. sDistinc
2b070 74 2e 65 54 6e 63 74 54 79 70 65 20 3d 20 57 48 t.eTnctType = WH
2b080 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 4e 4f ERE_DISTINCT_UNO
2b090 52 44 45 52 45 44 3b 0a 20 20 7d 65 6c 73 65 7b RDERED;. }else{
2b0a0 0a 20 20 20 20 73 44 69 73 74 69 6e 63 74 2e 65 . sDistinct.e
2b0b0 54 6e 63 74 54 79 70 65 20 3d 20 57 48 45 52 45 TnctType = WHERE
2b0c0 5f 44 49 53 54 49 4e 43 54 5f 4e 4f 4f 50 3b 0a _DISTINCT_NOOP;.
2b0d0 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 41 67 }.. if( !isAg
2b0e0 67 20 26 26 20 70 47 72 6f 75 70 42 79 3d 3d 30 g && pGroupBy==0
2b0f0 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 61 67 ){. /* No ag
2b100 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e gregate function
2b110 73 20 61 6e 64 20 6e 6f 20 47 52 4f 55 50 20 42 s and no GROUP B
2b120 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 Y clause */.
2b130 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73 20 3d u16 wctrlFlags =
2b140 20 28 73 44 69 73 74 69 6e 63 74 2e 69 73 54 6e (sDistinct.isTn
2b150 63 74 20 3f 20 57 48 45 52 45 5f 57 41 4e 54 5f ct ? WHERE_WANT_
2b160 44 49 53 54 49 4e 43 54 20 3a 20 30 29 3b 0a 0a DISTINCT : 0);..
2b170 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 /* Begin the
2b180 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20 database scan.
2b190 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 */. pWInfo =
2b1a0 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 sqlite3WhereBegi
2b1b0 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 n(pParse, pTabLi
2b1c0 73 74 2c 20 70 57 68 65 72 65 2c 20 73 53 6f 72 st, pWhere, sSor
2b1d0 74 2e 70 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 t.pOrderBy,.
2b1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2b1f0 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 45 p->pE
2b200 4c 69 73 74 2c 20 77 63 74 72 6c 46 6c 61 67 73 List, wctrlFlags
2b210 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 57 , 0);. if( pW
2b220 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73 Info==0 ) goto s
2b230 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 69 elect_end;. i
2b240 66 28 20 73 71 6c 69 74 65 33 57 68 65 72 65 4f f( sqlite3WhereO
2b250 75 74 70 75 74 52 6f 77 43 6f 75 6e 74 28 70 57 utputRowCount(pW
2b260 49 6e 66 6f 29 20 3c 20 70 2d 3e 6e 53 65 6c 65 Info) < p->nSele
2b270 63 74 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 70 ctRow ){. p
2b280 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 3d 20 73 ->nSelectRow = s
2b290 71 6c 69 74 65 33 57 68 65 72 65 4f 75 74 70 75 qlite3WhereOutpu
2b2a0 74 52 6f 77 43 6f 75 6e 74 28 70 57 49 6e 66 6f tRowCount(pWInfo
2b2b0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
2b2c0 20 73 44 69 73 74 69 6e 63 74 2e 69 73 54 6e 63 sDistinct.isTnc
2b2d0 74 20 26 26 20 73 71 6c 69 74 65 33 57 68 65 72 t && sqlite3Wher
2b2e0 65 49 73 44 69 73 74 69 6e 63 74 28 70 57 49 6e eIsDistinct(pWIn
2b2f0 66 6f 29 20 29 7b 0a 20 20 20 20 20 20 73 44 69 fo) ){. sDi
2b300 73 74 69 6e 63 74 2e 65 54 6e 63 74 54 79 70 65 stinct.eTnctType
2b310 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 49 = sqlite3WhereI
2b320 73 44 69 73 74 69 6e 63 74 28 70 57 49 6e 66 6f sDistinct(pWInfo
2b330 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 );. }. if(
2b340 20 73 53 6f 72 74 2e 70 4f 72 64 65 72 42 79 20 sSort.pOrderBy
2b350 29 7b 0a 20 20 20 20 20 20 73 53 6f 72 74 2e 6e ){. sSort.n
2b360 4f 42 53 61 74 20 3d 20 73 71 6c 69 74 65 33 57 OBSat = sqlite3W
2b370 68 65 72 65 49 73 4f 72 64 65 72 65 64 28 70 57 hereIsOrdered(pW
2b380 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 Info);. if(
2b390 20 73 53 6f 72 74 2e 6e 4f 42 53 61 74 3d 3d 73 sSort.nOBSat==s
2b3a0 53 6f 72 74 2e 70 4f 72 64 65 72 42 79 2d 3e 6e Sort.pOrderBy->n
2b3b0 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 Expr ){.
2b3c0 73 53 6f 72 74 2e 70 4f 72 64 65 72 42 79 20 3d sSort.pOrderBy =
2b3d0 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0;. }.
2b3e0 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 73 6f 72 }.. /* If sor
2b3f0 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61 74 20 ting index that
2b400 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61 was created by a
2b410 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e 45 70 prior OP_OpenEp
2b420 68 65 6d 65 72 61 6c 20 0a 20 20 20 20 2a 2a 20 hemeral . **
2b430 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e 64 65 instruction ende
2b440 64 20 75 70 20 6e 6f 74 20 62 65 69 6e 67 20 6e d up not being n
2b450 65 65 64 65 64 2c 20 74 68 65 6e 20 63 68 61 6e eeded, then chan
2b460 67 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e 45 70 ge the OP_OpenEp
2b470 68 65 6d 65 72 61 6c 0a 20 20 20 20 2a 2a 20 69 hemeral. ** i
2b480 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70 2e 0a nto an OP_Noop..
2b490 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 */. if( s
2b4a0 53 6f 72 74 2e 61 64 64 72 53 6f 72 74 49 6e 64 Sort.addrSortInd
2b4b0 65 78 3e 3d 30 20 26 26 20 73 53 6f 72 74 2e 70 ex>=0 && sSort.p
2b4c0 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b 0a 20 20 OrderBy==0 ){.
2b4d0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 sqlite3VdbeC
2b4e0 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 73 hangeToNoop(v, s
2b4f0 53 6f 72 74 2e 61 64 64 72 53 6f 72 74 49 6e 64 Sort.addrSortInd
2b500 65 78 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 ex);. }..
2b510 2f 2a 20 55 73 65 20 74 68 65 20 73 74 61 6e 64 /* Use the stand
2b520 61 72 64 20 69 6e 6e 65 72 20 6c 6f 6f 70 2e 20 ard inner loop.
2b530 2a 2f 0a 20 20 20 20 73 65 6c 65 63 74 49 6e 6e */. selectInn
2b540 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 erLoop(pParse, p
2b550 2c 20 70 45 4c 69 73 74 2c 20 2d 31 2c 20 26 73 , pEList, -1, &s
2b560 53 6f 72 74 2c 20 26 73 44 69 73 74 69 6e 63 74 Sort, &sDistinct
2b570 2c 20 70 44 65 73 74 2c 0a 20 20 20 20 20 20 20 , pDest,.
2b580 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
2b590 69 74 65 33 57 68 65 72 65 43 6f 6e 74 69 6e 75 ite3WhereContinu
2b5a0 65 4c 61 62 65 6c 28 70 57 49 6e 66 6f 29 2c 0a eLabel(pWInfo),.
2b5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2b5c0 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 sqlite3Where
2b5d0 42 72 65 61 6b 4c 61 62 65 6c 28 70 57 49 6e 66 BreakLabel(pWInf
2b5e0 6f 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 o));.. /* End
2b5f0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 the database sc
2b600 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a an loop.. */.
2b610 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 sqlite3Where
2b620 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 7d End(pWInfo);. }
2b630 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 else{. /* Thi
2b640 73 20 63 61 73 65 20 77 68 65 6e 20 74 68 65 72 s case when ther
2b650 65 20 65 78 69 73 74 20 61 67 67 72 65 67 61 74 e exist aggregat
2b660 65 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 e functions or a
2b670 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 GROUP BY clause
2b680 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 20 . ** or both
2b690 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 */. NameConte
2b6a0 78 74 20 73 4e 43 3b 20 20 20 20 2f 2a 20 4e 61 xt sNC; /* Na
2b6b0 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 me context for p
2b6c0 72 6f 63 65 73 73 69 6e 67 20 61 67 67 72 65 67 rocessing aggreg
2b6d0 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 ate information
2b6e0 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 4d 65 6d */. int iAMem
2b6f0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 ; /* Fi
2b700 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20 rst Mem address
2b710 66 6f 72 20 73 74 6f 72 69 6e 67 20 63 75 72 72 for storing curr
2b720 65 6e 74 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a ent GROUP BY */.
2b730 20 20 20 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20 int iBMem;
2b740 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 /* First
2b750 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72 Mem address for
2b760 20 70 72 65 76 69 6f 75 73 20 47 52 4f 55 50 20 previous GROUP
2b770 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 55 BY */. int iU
2b780 73 65 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a seFlag; /*
2b790 20 4d 65 6d 20 61 64 64 72 65 73 73 20 68 6f 6c Mem address hol
2b7a0 64 69 6e 67 20 66 6c 61 67 20 69 6e 64 69 63 61 ding flag indica
2b7b0 74 69 6e 67 20 74 68 61 74 20 61 74 20 6c 65 61 ting that at lea
2b7c0 73 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 st.
2b7d0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e ** on
2b7e0 65 20 72 6f 77 20 6f 66 20 74 68 65 20 69 6e 70 e row of the inp
2b7f0 75 74 20 74 6f 20 74 68 65 20 61 67 67 72 65 67 ut to the aggreg
2b800 61 74 6f 72 20 68 61 73 20 62 65 65 6e 0a 20 20 ator has been.
2b810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2b820 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 ** process
2b830 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 ed */. int iA
2b840 62 6f 72 74 46 6c 61 67 3b 20 20 20 20 20 2f 2a bortFlag; /*
2b850 20 4d 65 6d 20 61 64 64 72 65 73 73 20 77 68 69 Mem address whi
2b860 63 68 20 63 61 75 73 65 73 20 71 75 65 72 79 20 ch causes query
2b870 61 62 6f 72 74 20 69 66 20 70 6f 73 69 74 69 76 abort if positiv
2b880 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 67 72 6f e */. int gro
2b890 75 70 42 79 53 6f 72 74 3b 20 20 20 20 2f 2a 20 upBySort; /*
2b8a0 52 6f 77 73 20 63 6f 6d 65 20 66 72 6f 6d 20 73 Rows come from s
2b8b0 6f 75 72 63 65 20 69 6e 20 47 52 4f 55 50 20 42 ource in GROUP B
2b8c0 59 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 69 Y order */. i
2b8d0 6e 74 20 61 64 64 72 45 6e 64 3b 20 20 20 20 20 nt addrEnd;
2b8e0 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 72 6f /* End of pro
2b8f0 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 cessing for this
2b900 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20 20 69 SELECT */. i
2b910 6e 74 20 73 6f 72 74 50 54 61 62 20 3d 20 30 3b nt sortPTab = 0;
2b920 20 20 20 2f 2a 20 50 73 65 75 64 6f 74 61 62 6c /* Pseudotabl
2b930 65 20 75 73 65 64 20 74 6f 20 64 65 63 6f 64 65 e used to decode
2b940 20 73 6f 72 74 69 6e 67 20 72 65 73 75 6c 74 73 sorting results
2b950 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 6f 72 74 */. int sort
2b960 4f 75 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4f Out = 0; /* O
2b970 75 74 70 75 74 20 72 65 67 69 73 74 65 72 20 66 utput register f
2b980 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20 2a rom the sorter *
2b990 2f 0a 20 20 20 20 69 6e 74 20 6f 72 64 65 72 42 /. int orderB
2b9a0 79 47 72 70 20 3d 20 30 3b 20 2f 2a 20 54 72 75 yGrp = 0; /* Tru
2b9b0 65 20 69 66 20 74 68 65 20 47 52 4f 55 50 20 42 e if the GROUP B
2b9c0 59 20 61 6e 64 20 4f 52 44 45 52 20 42 59 20 61 Y and ORDER BY a
2b9d0 72 65 20 74 68 65 20 73 61 6d 65 20 2a 2f 0a 0a re the same */..
2b9e0 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e /* Remove an
2b9f0 79 20 61 6e 64 20 61 6c 6c 20 61 6c 69 61 73 65 y and all aliase
2ba00 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 72 65 s between the re
2ba10 73 75 6c 74 20 73 65 74 20 61 6e 64 20 74 68 65 sult set and the
2ba20 0a 20 20 20 20 2a 2a 20 47 52 4f 55 50 20 42 59 . ** GROUP BY
2ba30 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a clause.. */.
2ba40 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 if( pGroupBy
2ba50 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b ){. int k;
2ba60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ba70 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 /* Loop
2ba80 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 counter */.
2ba90 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 struct ExprList
2baa0 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 2f _item *pItem; /
2bab0 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 * For looping ov
2bac0 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e er expression in
2bad0 20 61 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 a list */..
2bae0 20 20 66 6f 72 28 6b 3d 70 2d 3e 70 45 4c 69 73 for(k=p->pELis
2baf0 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d t->nExpr, pItem=
2bb00 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 20 6b 3e p->pEList->a; k>
2bb10 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 0; k--, pItem++)
2bb20 7b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d {. pItem-
2bb30 3e 75 2e 78 2e 69 41 6c 69 61 73 20 3d 20 30 3b >u.x.iAlias = 0;
2bb40 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 . }. f
2bb50 6f 72 28 6b 3d 70 47 72 6f 75 70 42 79 2d 3e 6e or(k=pGroupBy->n
2bb60 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 47 72 6f Expr, pItem=pGro
2bb70 75 70 42 79 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d upBy->a; k>0; k-
2bb80 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 -, pItem++){.
2bb90 20 20 20 20 20 70 49 74 65 6d 2d 3e 75 2e 78 2e pItem->u.x.
2bba0 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 iAlias = 0;.
2bbb0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 2d }. if( p-
2bbc0 3e 6e 53 65 6c 65 63 74 52 6f 77 3e 31 30 30 20 >nSelectRow>100
2bbd0 29 20 70 2d 3e 6e 53 65 6c 65 63 74 52 6f 77 20 ) p->nSelectRow
2bbe0 3d 20 31 30 30 3b 0a 20 20 20 20 7d 65 6c 73 65 = 100;. }else
2bbf0 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 53 65 6c 65 {. p->nSele
2bc00 63 74 52 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d ctRow = 1;. }
2bc10 0a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 ... /* If the
2bc20 72 65 20 69 73 20 62 6f 74 68 20 61 20 47 52 4f re is both a GRO
2bc30 55 50 20 42 59 20 61 6e 64 20 61 6e 20 4f 52 44 UP BY and an ORD
2bc40 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 6e 64 ER BY clause and
2bc50 20 74 68 65 79 20 61 72 65 0a 20 20 20 20 2a 2a they are. **
2bc60 20 69 64 65 6e 74 69 63 61 6c 2c 20 74 68 65 6e identical, then
2bc70 20 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 it may be possi
2bc80 62 6c 65 20 74 6f 20 64 69 73 61 62 6c 65 20 74 ble to disable t
2bc90 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 he ORDER BY clau
2bca0 73 65 20 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 se . ** on th
2bcb0 65 20 67 72 6f 75 6e 64 73 20 74 68 61 74 20 74 e grounds that t
2bcc0 68 65 20 47 52 4f 55 50 20 42 59 20 77 69 6c 6c he GROUP BY will
2bcd0 20 63 61 75 73 65 20 65 6c 65 6d 65 6e 74 73 20 cause elements
2bce0 74 6f 20 63 6f 6d 65 20 6f 75 74 20 0a 20 20 20 to come out .
2bcf0 20 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 72 65 ** in the corre
2bd00 63 74 20 6f 72 64 65 72 2e 20 49 74 20 61 6c 73 ct order. It als
2bd10 6f 20 6d 61 79 20 6e 6f 74 20 2d 20 74 68 65 20 o may not - the
2bd20 47 52 4f 55 50 20 42 59 20 6d 61 79 20 75 73 65 GROUP BY may use
2bd30 20 61 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 a. ** databa
2bd40 73 65 20 69 6e 64 65 78 20 74 68 61 74 20 63 61 se index that ca
2bd50 75 73 65 73 20 72 6f 77 73 20 74 6f 20 62 65 20 uses rows to be
2bd60 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65 72 grouped together
2bd70 20 61 73 20 72 65 71 75 69 72 65 64 0a 20 20 20 as required.
2bd80 20 2a 2a 20 62 75 74 20 6e 6f 74 20 61 63 74 75 ** but not actu
2bd90 61 6c 6c 79 20 73 6f 72 74 65 64 2e 20 45 69 74 ally sorted. Eit
2bda0 68 65 72 20 77 61 79 2c 20 72 65 63 6f 72 64 20 her way, record
2bdb0 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 the fact that th
2bdc0 65 0a 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 e. ** ORDER B
2bdd0 59 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 Y and GROUP BY c
2bde0 6c 61 75 73 65 73 20 61 72 65 20 74 68 65 20 73 lauses are the s
2bdf0 61 6d 65 20 62 79 20 73 65 74 74 69 6e 67 20 74 ame by setting t
2be00 68 65 20 6f 72 64 65 72 42 79 47 72 70 0a 20 20 he orderByGrp.
2be10 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 2e 20 20 ** variable.
2be20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 */. if( sqlit
2be30 65 33 45 78 70 72 4c 69 73 74 43 6f 6d 70 61 72 e3ExprListCompar
2be40 65 28 70 47 72 6f 75 70 42 79 2c 20 73 53 6f 72 e(pGroupBy, sSor
2be50 74 2e 70 4f 72 64 65 72 42 79 2c 20 2d 31 29 3d t.pOrderBy, -1)=
2be60 3d 30 20 29 7b 0a 20 20 20 20 20 20 6f 72 64 65 =0 ){. orde
2be70 72 42 79 47 72 70 20 3d 20 31 3b 0a 20 20 20 20 rByGrp = 1;.
2be80 7d 0a 20 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 }. . /* Creat
2be90 65 20 61 20 6c 61 62 65 6c 20 74 6f 20 6a 75 6d e a label to jum
2bea0 70 20 74 6f 20 77 68 65 6e 20 77 65 20 77 61 6e p to when we wan
2beb0 74 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 71 t to abort the q
2bec0 75 65 72 79 20 2a 2f 0a 20 20 20 20 61 64 64 72 uery */. addr
2bed0 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 End = sqlite3Vdb
2bee0 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a eMakeLabel(v);..
2bef0 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 54 /* Convert T
2bf00 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20 69 K_COLUMN nodes i
2bf10 6e 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d nto TK_AGG_COLUM
2bf20 4e 20 61 6e 64 20 6d 61 6b 65 20 65 6e 74 72 69 N and make entri
2bf30 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20 73 41 67 es in. ** sAg
2bf40 67 49 6e 66 6f 20 66 6f 72 20 61 6c 6c 20 54 4b gInfo for all TK
2bf50 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f _AGG_FUNCTION no
2bf60 64 65 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f des in expressio
2bf70 6e 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a ns of the. **
2bf80 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e SELECT statemen
2bf90 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 t.. */. me
2bfa0 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 mset(&sNC, 0, si
2bfb0 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 zeof(sNC));.
2bfc0 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 sNC.pParse = pPa
2bfd0 72 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 rse;. sNC.pSr
2bfe0 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 cList = pTabList
2bff0 3b 0a 20 20 20 20 73 4e 43 2e 70 41 67 67 49 6e ;. sNC.pAggIn
2c000 66 6f 20 3d 20 26 73 41 67 67 49 6e 66 6f 3b 0a fo = &sAggInfo;.
2c010 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6d 6e 52 sAggInfo.mnR
2c020 65 67 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 eg = pParse->nMe
2c030 6d 2b 31 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 m+1;. sAggInf
2c040 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e o.nSortingColumn
2c050 20 3d 20 70 47 72 6f 75 70 42 79 20 3f 20 70 47 = pGroupBy ? pG
2c060 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 20 3a 20 roupBy->nExpr :
2c070 30 3b 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 0;. sAggInfo.
2c080 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 pGroupBy = pGrou
2c090 70 42 79 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 pBy;. sqlite3
2c0a0 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 ExprAnalyzeAggLi
2c0b0 73 74 28 26 73 4e 43 2c 20 70 45 4c 69 73 74 29 st(&sNC, pEList)
2c0c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 ;. sqlite3Exp
2c0d0 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 rAnalyzeAggList(
2c0e0 26 73 4e 43 2c 20 73 53 6f 72 74 2e 70 4f 72 64 &sNC, sSort.pOrd
2c0f0 65 72 42 79 29 3b 0a 20 20 20 20 69 66 28 20 70 erBy);. if( p
2c100 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 Having ){.
2c110 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 sqlite3ExprAnaly
2c120 7a 65 41 67 67 72 65 67 61 74 65 73 28 26 73 4e zeAggregates(&sN
2c130 43 2c 20 70 48 61 76 69 6e 67 29 3b 0a 20 20 20 C, pHaving);.
2c140 20 7d 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e }. sAggInfo.
2c150 6e 41 63 63 75 6d 75 6c 61 74 6f 72 20 3d 20 73 nAccumulator = s
2c160 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b AggInfo.nColumn;
2c170 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c . for(i=0; i<
2c180 73 41 67 67 49 6e 66 6f 2e 6e 46 75 6e 63 3b 20 sAggInfo.nFunc;
2c190 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 i++){. asse
2c1a0 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 rt( !ExprHasProp
2c1b0 65 72 74 79 28 73 41 67 67 49 6e 66 6f 2e 61 46 erty(sAggInfo.aF
2c1c0 75 6e 63 5b 69 5d 2e 70 45 78 70 72 2c 20 45 50 unc[i].pExpr, EP
2c1d0 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 _xIsSelect) );.
2c1e0 20 20 20 20 20 73 4e 43 2e 6e 63 46 6c 61 67 73 sNC.ncFlags
2c1f0 20 7c 3d 20 4e 43 5f 49 6e 41 67 67 46 75 6e 63 |= NC_InAggFunc
2c200 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 ;. sqlite3E
2c210 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 xprAnalyzeAggLis
2c220 74 28 26 73 4e 43 2c 20 73 41 67 67 49 6e 66 6f t(&sNC, sAggInfo
2c230 2e 61 46 75 6e 63 5b 69 5d 2e 70 45 78 70 72 2d .aFunc[i].pExpr-
2c240 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 >x.pList);.
2c250 20 73 4e 43 2e 6e 63 46 6c 61 67 73 20 26 3d 20 sNC.ncFlags &=
2c260 7e 4e 43 5f 49 6e 41 67 67 46 75 6e 63 3b 0a 20 ~NC_InAggFunc;.
2c270 20 20 20 7d 0a 20 20 20 20 73 41 67 67 49 6e 66 }. sAggInf
2c280 6f 2e 6d 78 52 65 67 20 3d 20 70 50 61 72 73 65 o.mxReg = pParse
2c290 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 66 28 20 ->nMem;. if(
2c2a0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 db->mallocFailed
2c2b0 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 ) goto select_e
2c2c0 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 nd;.. /* Proc
2c2d0 65 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 essing for aggre
2c2e0 67 61 74 65 73 20 77 69 74 68 20 47 52 4f 55 50 gates with GROUP
2c2f0 20 42 59 20 69 73 20 76 65 72 79 20 64 69 66 66 BY is very diff
2c300 65 72 65 6e 74 20 61 6e 64 0a 20 20 20 20 2a 2a erent and. **
2c310 20 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c much more compl
2c320 65 78 20 74 68 61 6e 20 61 67 67 72 65 67 61 74 ex than aggregat
2c330 65 73 20 77 69 74 68 6f 75 74 20 61 20 47 52 4f es without a GRO
2c340 55 50 20 42 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 UP BY.. */.
2c350 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 if( pGroupBy )
2c360 7b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 {. KeyInfo
2c370 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b *pKeyInfo; /* K
2c380 65 79 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f eying informatio
2c390 6e 20 66 6f 72 20 74 68 65 20 67 72 6f 75 70 20 n for the group
2c3a0 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 by clause */.
2c3b0 20 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 int j1;
2c3c0 20 20 20 20 20 20 20 2f 2a 20 41 2d 76 73 2d 42 /* A-vs-B
2c3d0 20 63 6f 6d 70 61 72 69 73 69 6f 6e 20 6a 75 6d comparision jum
2c3e0 70 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 p */. int a
2c3f0 64 64 72 4f 75 74 70 75 74 52 6f 77 3b 20 20 2f ddrOutputRow; /
2c400 2a 20 53 74 61 72 74 20 6f 66 20 73 75 62 72 6f * Start of subro
2c410 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 utine that outpu
2c420 74 73 20 61 20 72 65 73 75 6c 74 20 72 6f 77 20 ts a result row
2c430 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 */. int reg
2c440 4f 75 74 70 75 74 52 6f 77 3b 20 20 20 2f 2a 20 OutputRow; /*
2c450 52 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 72 Return address r
2c460 65 67 69 73 74 65 72 20 66 6f 72 20 6f 75 74 70 egister for outp
2c470 75 74 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f ut subroutine */
2c480 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 53 . int addrS
2c490 65 74 41 62 6f 72 74 3b 20 20 20 2f 2a 20 53 65 etAbort; /* Se
2c4a0 74 20 74 68 65 20 61 62 6f 72 74 20 66 6c 61 67 t the abort flag
2c4b0 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 and return */.
2c4c0 20 20 20 20 20 69 6e 74 20 61 64 64 72 54 6f 70 int addrTop
2c4d0 4f 66 4c 6f 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 OfLoop; /* Top
2c4e0 6f 66 20 74 68 65 20 69 6e 70 75 74 20 6c 6f 6f of the input loo
2c4f0 70 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 p */. int a
2c500 64 64 72 53 6f 72 74 69 6e 67 49 64 78 3b 20 2f ddrSortingIdx; /
2c510 2a 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 * The OP_OpenEph
2c520 65 6d 65 72 61 6c 20 66 6f 72 20 74 68 65 20 73 emeral for the s
2c530 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a orting index */.
2c540 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 52 65 int addrRe
2c550 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 75 62 set; /* Sub
2c560 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 65 73 65 routine for rese
2c570 74 74 69 6e 67 20 74 68 65 20 61 63 63 75 6d 75 tting the accumu
2c580 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 69 lator */. i
2c590 6e 74 20 72 65 67 52 65 73 65 74 3b 20 20 20 20 nt regReset;
2c5a0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 /* Return add
2c5b0 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f ress register fo
2c5c0 72 20 72 65 73 65 74 20 73 75 62 72 6f 75 74 69 r reset subrouti
2c5d0 6e 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 ne */.. /*
2c5e0 49 66 20 74 68 65 72 65 20 69 73 20 61 20 47 52 If there is a GR
2c5f0 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 77 65 OUP BY clause we
2c600 20 6d 69 67 68 74 20 6e 65 65 64 20 61 20 73 6f might need a so
2c610 72 74 69 6e 67 20 69 6e 64 65 78 20 74 6f 0a 20 rting index to.
2c620 20 20 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e ** implemen
2c630 74 20 69 74 2e 20 20 41 6c 6c 6f 63 61 74 65 20 t it. Allocate
2c640 74 68 61 74 20 73 6f 72 74 69 6e 67 20 69 6e 64 that sorting ind
2c650 65 78 20 6e 6f 77 2e 20 20 49 66 20 69 74 20 74 ex now. If it t
2c660 75 72 6e 73 20 6f 75 74 0a 20 20 20 20 20 20 2a urns out. *
2c670 2a 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 * that we do not
2c680 20 6e 65 65 64 20 69 74 20 61 66 74 65 72 20 61 need it after a
2c690 6c 6c 2c 20 74 68 65 20 4f 50 5f 53 6f 72 74 65 ll, the OP_Sorte
2c6a0 72 4f 70 65 6e 20 69 6e 73 74 72 75 63 74 69 6f rOpen instructio
2c6b0 6e 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 n. ** will
2c6c0 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 be converted int
2c6d0 6f 20 61 20 4e 6f 6f 70 2e 20 20 0a 20 20 20 20 o a Noop. .
2c6e0 20 20 2a 2f 0a 20 20 20 20 20 20 73 41 67 67 49 */. sAggI
2c6f0 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 20 3d nfo.sortingIdx =
2c700 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b pParse->nTab++;
2c710 0a 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 . pKeyInfo
2c720 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 = keyInfoFromExp
2c730 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 47 rList(pParse, pG
2c740 72 6f 75 70 42 79 2c 20 30 2c 20 30 29 3b 0a 20 roupBy, 0, 0);.
2c750 20 20 20 20 20 61 64 64 72 53 6f 72 74 69 6e 67 addrSorting
2c760 49 64 78 20 3d 20 73 71 6c 69 74 65 33 56 64 62 Idx = sqlite3Vdb
2c770 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 6f eAddOp4(v, OP_So
2c780 72 74 65 72 4f 70 65 6e 2c 20 0a 20 20 20 20 20 rterOpen, .
2c790 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f sAggInfo.so
2c7a0 72 74 69 6e 67 49 64 78 2c 20 73 41 67 67 49 6e rtingIdx, sAggIn
2c7b0 66 6f 2e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d fo.nSortingColum
2c7c0 6e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 30 2c n, . 0,
2c7d0 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f (char*)pKeyInfo
2c7e0 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 0a , P4_KEYINFO);..
2c7f0 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c /* Initial
2c800 69 7a 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 ize memory locat
2c810 69 6f 6e 73 20 75 73 65 64 20 62 79 20 47 52 4f ions used by GRO
2c820 55 50 20 42 59 20 61 67 67 72 65 67 61 74 65 20 UP BY aggregate
2c830 70 72 6f 63 65 73 73 69 6e 67 0a 20 20 20 20 20 processing.
2c840 20 2a 2f 0a 20 20 20 20 20 20 69 55 73 65 46 6c */. iUseFl
2c850 61 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e ag = ++pParse->n
2c860 4d 65 6d 3b 0a 20 20 20 20 20 20 69 41 62 6f 72 Mem;. iAbor
2c870 74 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73 65 tFlag = ++pParse
2c880 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72 65 ->nMem;. re
2c890 67 4f 75 74 70 75 74 52 6f 77 20 3d 20 2b 2b 70 gOutputRow = ++p
2c8a0 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
2c8b0 20 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 addrOutputRow
2c8c0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 = sqlite3VdbeMa
2c8d0 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 keLabel(v);.
2c8e0 20 20 72 65 67 52 65 73 65 74 20 3d 20 2b 2b 70 regReset = ++p
2c8f0 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 Parse->nMem;.
2c900 20 20 20 61 64 64 72 52 65 73 65 74 20 3d 20 73 addrReset = s
2c910 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 qlite3VdbeMakeLa
2c920 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 41 bel(v);. iA
2c930 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d Mem = pParse->nM
2c940 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 50 em + 1;. pP
2c950 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 47 arse->nMem += pG
2c960 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a 20 roupBy->nExpr;.
2c970 20 20 20 20 20 69 42 4d 65 6d 20 3d 20 70 50 61 iBMem = pPa
2c980 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 rse->nMem + 1;.
2c990 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 pParse->nMe
2c9a0 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e m += pGroupBy->n
2c9b0 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 Expr;. sqli
2c9c0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
2c9d0 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 OP_Integer, 0,
2c9e0 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a 20 20 20 iAbortFlag);.
2c9f0 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 VdbeComment((
2ca00 76 2c 20 22 63 6c 65 61 72 20 61 62 6f 72 74 20 v, "clear abort
2ca10 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20 73 flag"));. s
2ca20 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
2ca30 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 (v, OP_Integer,
2ca40 30 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a 20 20 0, iUseFlag);.
2ca50 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 VdbeComment(
2ca60 28 76 2c 20 22 69 6e 64 69 63 61 74 65 20 61 63 (v, "indicate ac
2ca70 63 75 6d 75 6c 61 74 6f 72 20 65 6d 70 74 79 22 cumulator empty"
2ca80 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 ));. sqlite
2ca90 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 3VdbeAddOp3(v, O
2caa0 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 41 4d 65 6d P_Null, 0, iAMem
2cab0 2c 20 69 41 4d 65 6d 2b 70 47 72 6f 75 70 42 79 , iAMem+pGroupBy
2cac0 2d 3e 6e 45 78 70 72 2d 31 29 3b 0a 0a 20 20 20 ->nExpr-1);..
2cad0 20 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 6c 6f /* Begin a lo
2cae0 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74 op that will ext
2caf0 72 61 63 74 20 61 6c 6c 20 73 6f 75 72 63 65 20 ract all source
2cb00 72 6f 77 73 20 69 6e 20 47 52 4f 55 50 20 42 59 rows in GROUP BY
2cb10 20 6f 72 64 65 72 2e 0a 20 20 20 20 20 20 2a 2a order.. **
2cb20 20 54 68 69 73 20 6d 69 67 68 74 20 69 6e 76 6f This might invo
2cb30 6c 76 65 20 74 77 6f 20 73 65 70 61 72 61 74 65 lve two separate
2cb40 20 6c 6f 6f 70 73 20 77 69 74 68 20 61 6e 20 4f loops with an O
2cb50 50 5f 53 6f 72 74 20 69 6e 20 62 65 74 77 65 65 P_Sort in betwee
2cb60 6e 2c 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 69 n, or. ** i
2cb70 74 20 6d 69 67 68 74 20 62 65 20 61 20 73 69 6e t might be a sin
2cb80 67 6c 65 20 6c 6f 6f 70 20 74 68 61 74 20 75 73 gle loop that us
2cb90 65 73 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 65 es an index to e
2cba0 78 74 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 xtract informati
2cbb0 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 on. ** in t
2cbc0 68 65 20 72 69 67 68 74 20 6f 72 64 65 72 20 74 he right order t
2cbd0 6f 20 62 65 67 69 6e 20 77 69 74 68 2e 0a 20 20 o begin with..
2cbe0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
2cbf0 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
2cc00 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52 , OP_Gosub, regR
2cc10 65 73 65 74 2c 20 61 64 64 72 52 65 73 65 74 29 eset, addrReset)
2cc20 3b 0a 20 20 20 20 20 20 70 57 49 6e 66 6f 20 3d ;. pWInfo =
2cc30 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 sqlite3WhereBeg
2cc40 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c in(pParse, pTabL
2cc50 69 73 74 2c 20 70 57 68 65 72 65 2c 20 70 47 72 ist, pWhere, pGr
2cc60 6f 75 70 42 79 2c 20 30 2c 0a 20 20 20 20 20 20 oupBy, 0,.
2cc70 20 20 20 20 57 48 45 52 45 5f 47 52 4f 55 50 42 WHERE_GROUPB
2cc80 59 20 7c 20 28 6f 72 64 65 72 42 79 47 72 70 20 Y | (orderByGrp
2cc90 3f 20 57 48 45 52 45 5f 53 4f 52 54 42 59 47 52 ? WHERE_SORTBYGR
2cca0 4f 55 50 20 3a 20 30 29 2c 20 30 0a 20 20 20 20 OUP : 0), 0.
2ccb0 20 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 );. if( p
2ccc0 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 WInfo==0 ) goto
2ccd0 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 select_end;.
2cce0 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 68 65 if( sqlite3Whe
2ccf0 72 65 49 73 4f 72 64 65 72 65 64 28 70 57 49 6e reIsOrdered(pWIn
2cd00 66 6f 29 3d 3d 70 47 72 6f 75 70 42 79 2d 3e 6e fo)==pGroupBy->n
2cd10 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20 Expr ){.
2cd20 2f 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 /* The optimizer
2cd30 20 69 73 20 61 62 6c 65 20 74 6f 20 64 65 6c 69 is able to deli
2cd40 76 65 72 20 72 6f 77 73 20 69 6e 20 67 72 6f 75 ver rows in grou
2cd50 70 20 62 79 20 6f 72 64 65 72 20 73 6f 0a 20 20 p by order so.
2cd60 20 20 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e ** we do n
2cd70 6f 74 20 68 61 76 65 20 74 6f 20 73 6f 72 74 2e ot have to sort.
2cd80 20 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 The OP_OpenEph
2cd90 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 77 69 6c emeral table wil
2cda0 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 l be. **
2cdb0 63 61 6e 63 65 6c 6c 65 64 20 6c 61 74 65 72 20 cancelled later
2cdc0 62 65 63 61 75 73 65 20 77 65 20 73 74 69 6c 6c because we still
2cdd0 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 need to use the
2cde0 20 70 4b 65 79 49 6e 66 6f 0a 20 20 20 20 20 20 pKeyInfo.
2cdf0 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 67 72 6f */. gro
2ce00 75 70 42 79 53 6f 72 74 20 3d 20 30 3b 0a 20 20 upBySort = 0;.
2ce10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 }else{.
2ce20 20 20 20 2f 2a 20 52 6f 77 73 20 61 72 65 20 63 /* Rows are c
2ce30 6f 6d 69 6e 67 20 6f 75 74 20 69 6e 20 75 6e 64 oming out in und
2ce40 65 74 65 72 6d 69 6e 65 64 20 6f 72 64 65 72 2e etermined order.
2ce50 20 20 57 65 20 68 61 76 65 20 74 6f 20 70 75 73 We have to pus
2ce60 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 61 63 h. ** eac
2ce70 68 20 72 6f 77 20 69 6e 74 6f 20 61 20 73 6f 72 h row into a sor
2ce80 74 69 6e 67 20 69 6e 64 65 78 2c 20 74 65 72 6d ting index, term
2ce90 69 6e 61 74 65 20 74 68 65 20 66 69 72 73 74 20 inate the first
2cea0 6c 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 2a 2a loop,. **
2ceb0 20 74 68 65 6e 20 6c 6f 6f 70 20 6f 76 65 72 20 then loop over
2cec0 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 the sorting inde
2ced0 78 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 x in order to ge
2cee0 74 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 20 t the output.
2cef0 20 20 20 20 20 2a 2a 20 69 6e 20 73 6f 72 74 65 ** in sorte
2cf00 64 20 6f 72 64 65 72 0a 20 20 20 20 20 20 20 20 d order.
2cf10 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 */. int r
2cf20 65 67 42 61 73 65 3b 0a 20 20 20 20 20 20 20 20 egBase;.
2cf30 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 0a 20 int regRecord;.
2cf40 20 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b int nCol;
2cf50 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 47 72 . int nGr
2cf60 6f 75 70 42 79 3b 0a 0a 20 20 20 20 20 20 20 20 oupBy;..
2cf70 65 78 70 6c 61 69 6e 54 65 6d 70 54 61 62 6c 65 explainTempTable
2cf80 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 (pParse, .
2cf90 20 20 20 20 20 20 28 73 44 69 73 74 69 6e 63 74 (sDistinct
2cfa0 2e 69 73 54 6e 63 74 20 26 26 20 28 70 2d 3e 73 .isTnct && (p->s
2cfb0 65 6c 46 6c 61 67 73 26 53 46 5f 44 69 73 74 69 elFlags&SF_Disti
2cfc0 6e 63 74 29 3d 3d 30 29 20 3f 0a 20 20 20 20 20 nct)==0) ?.
2cfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 "
2cfe0 44 49 53 54 49 4e 43 54 22 20 3a 20 22 47 52 4f DISTINCT" : "GRO
2cff0 55 50 20 42 59 22 29 3b 0a 0a 20 20 20 20 20 20 UP BY");..
2d000 20 20 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 groupBySort =
2d010 31 3b 0a 20 20 20 20 20 20 20 20 6e 47 72 6f 75 1;. nGrou
2d020 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 2d 3e pBy = pGroupBy->
2d030 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 6e nExpr;. n
2d040 43 6f 6c 20 3d 20 6e 47 72 6f 75 70 42 79 3b 0a Col = nGroupBy;.
2d050 20 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72 6f j = nGro
2d060 75 70 42 79 3b 0a 20 20 20 20 20 20 20 20 66 6f upBy;. fo
2d070 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 r(i=0; i<sAggInf
2d080 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b o.nColumn; i++){
2d090 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 . if( s
2d0a0 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d 2e AggInfo.aCol[i].
2d0b0 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a iSorterColumn>=j
2d0c0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 ){.
2d0d0 6e 43 6f 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nCol++;.
2d0e0 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 j++;.
2d0f0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 }. }.
2d100 20 20 20 20 20 20 20 72 65 67 42 61 73 65 20 3d regBase =
2d110 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 sqlite3GetTempR
2d120 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f ange(pParse, nCo
2d130 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 l);. sqli
2d140 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 te3ExprCacheClea
2d150 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 r(pParse);.
2d160 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f sqlite3ExprCo
2d170 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 deExprList(pPars
2d180 65 2c 20 70 47 72 6f 75 70 42 79 2c 20 72 65 67 e, pGroupBy, reg
2d190 42 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 Base, 0);.
2d1a0 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79 3b 0a j = nGroupBy;.
2d1b0 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b for(i=0;
2d1c0 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c i<sAggInfo.nCol
2d1d0 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 umn; i++){.
2d1e0 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49 struct AggI
2d1f0 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 nfo_col *pCol =
2d200 26 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 &sAggInfo.aCol[i
2d210 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 ];. if(
2d220 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f pCol->iSorterCo
2d230 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 lumn>=j ){.
2d240 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 int r1 =
2d250 6a 20 2b 20 72 65 67 42 61 73 65 3b 0a 20 20 20 j + regBase;.
2d260 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 3b int r2;
2d270 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 32 .. r2
2d280 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f = sqlite3ExprCo
2d290 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 deGetColumn(pPar
2d2a0 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 se, .
2d2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2d2c0 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 2c 20 pCol->pTab,
2d2d0 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70 pCol->iColumn, p
2d2e0 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c 20 72 31 2c Col->iTable, r1,
2d2f0 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 0);.
2d300 20 69 66 28 20 72 31 21 3d 72 32 20 29 7b 0a 20 if( r1!=r2 ){.
2d310 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c sql
2d320 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
2d330 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 32 2c 20 , OP_SCopy, r2,
2d340 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 r1);.
2d350 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a }. j
2d360 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a ++;. }.
2d370 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2d380 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 regRecord = sq
2d390 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 lite3GetTempReg(
2d3a0 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 pParse);.
2d3b0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f sqlite3VdbeAddO
2d3c0 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 p3(v, OP_MakeRec
2d3d0 6f 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 ord, regBase, nC
2d3e0 6f 6c 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a ol, regRecord);.
2d3f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
2d400 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
2d410 53 6f 72 74 65 72 49 6e 73 65 72 74 2c 20 73 41 SorterInsert, sA
2d420 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 ggInfo.sortingId
2d430 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 x, regRecord);.
2d440 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 sqlite3Re
2d450 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 leaseTempReg(pPa
2d460 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29 3b rse, regRecord);
2d470 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 . sqlite3
2d480 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 ReleaseTempRange
2d490 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 (pParse, regBase
2d4a0 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 , nCol);.
2d4b0 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 sqlite3WhereEnd
2d4c0 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 (pWInfo);.
2d4d0 20 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 sAggInfo.sorti
2d4e0 6e 67 49 64 78 50 54 61 62 20 3d 20 73 6f 72 74 ngIdxPTab = sort
2d4f0 50 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e PTab = pParse->n
2d500 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 73 Tab++;. s
2d510 6f 72 74 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 ortOut = sqlite3
2d520 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 GetTempReg(pPars
2d530 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 e);. sqli
2d540 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c te3VdbeAddOp3(v,
2d550 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 2c 20 OP_OpenPseudo,
2d560 73 6f 72 74 50 54 61 62 2c 20 73 6f 72 74 4f 75 sortPTab, sortOu
2d570 74 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 t, nCol);.
2d580 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2d590 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 65 72 Op2(v, OP_Sorter
2d5a0 53 6f 72 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 Sort, sAggInfo.s
2d5b0 6f 72 74 69 6e 67 49 64 78 2c 20 61 64 64 72 45 ortingIdx, addrE
2d5c0 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 nd);. Vdb
2d5d0 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52 eComment((v, "GR
2d5e0 4f 55 50 20 42 59 20 73 6f 72 74 22 29 29 3b 20 OUP BY sort"));
2d5f0 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
2d600 0a 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 . sAggInf
2d610 6f 2e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20 o.useSortingIdx
2d620 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c = 1;. sql
2d630 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 ite3ExprCacheCle
2d640 61 72 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 ar(pParse);..
2d650 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49 }.. /* I
2d660 66 20 74 68 65 20 69 6e 64 65 78 20 6f 72 20 74 f the index or t
2d670 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 75 emporary table u
2d680 73 65 64 20 62 79 20 74 68 65 20 47 52 4f 55 50 sed by the GROUP
2d690 20 42 59 20 73 6f 72 74 0a 20 20 20 20 20 20 2a BY sort. *
2d6a0 2a 20 77 69 6c 6c 20 6e 61 74 75 72 61 6c 6c 79 * will naturally
2d6b0 20 64 65 6c 69 76 65 72 20 72 6f 77 73 20 69 6e deliver rows in
2d6c0 20 74 68 65 20 6f 72 64 65 72 20 72 65 71 75 69 the order requi
2d6d0 72 65 64 20 62 79 20 74 68 65 20 4f 52 44 45 52 red by the ORDER
2d6e0 20 42 59 0a 20 20 20 20 20 20 2a 2a 20 63 6c 61 BY. ** cla
2d6f0 75 73 65 2c 20 63 61 6e 63 65 6c 20 74 68 65 20 use, cancel the
2d700 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 ephemeral table
2d710 6f 70 65 6e 20 63 6f 64 65 64 20 65 61 72 6c 69 open coded earli
2d720 65 72 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 er.. **.
2d730 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e ** This is an
2d740 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2d 20 optimization -
2d750 74 68 65 20 63 6f 72 72 65 63 74 20 61 6e 73 77 the correct answ
2d760 65 72 20 73 68 6f 75 6c 64 20 72 65 73 75 6c 74 er should result
2d770 20 72 65 67 61 72 64 6c 65 73 73 2e 0a 20 20 20 regardless..
2d780 20 20 20 2a 2a 20 55 73 65 20 74 68 65 20 53 51 ** Use the SQ
2d790 4c 49 54 45 5f 47 72 6f 75 70 42 79 4f 72 64 65 LITE_GroupByOrde
2d7a0 72 20 66 6c 61 67 20 77 69 74 68 20 53 51 4c 49 r flag with SQLI
2d7b0 54 45 5f 54 45 53 54 43 54 52 4c 5f 4f 50 54 49 TE_TESTCTRL_OPTI
2d7c0 4d 49 5a 45 52 20 74 6f 20 0a 20 20 20 20 20 20 MIZER to .
2d7d0 2a 2a 20 64 69 73 61 62 6c 65 20 74 68 69 73 20 ** disable this
2d7e0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 optimization for
2d7f0 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 testing purpose
2d800 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 s. */. if(
2d810 20 6f 72 64 65 72 42 79 47 72 70 20 26 26 20 4f orderByGrp && O
2d820 70 74 69 6d 69 7a 61 74 69 6f 6e 45 6e 61 62 6c ptimizationEnabl
2d830 65 64 28 64 62 2c 20 53 51 4c 49 54 45 5f 47 72 ed(db, SQLITE_Gr
2d840 6f 75 70 42 79 4f 72 64 65 72 29 20 0a 20 20 20 oupByOrder) .
2d850 20 20 20 20 26 26 20 28 67 72 6f 75 70 42 79 53 && (groupByS
2d860 6f 72 74 20 7c 7c 20 73 71 6c 69 74 65 33 57 68 ort || sqlite3Wh
2d870 65 72 65 49 73 53 6f 72 74 65 64 28 70 57 49 6e ereIsSorted(pWIn
2d880 66 6f 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 fo)). ){.
2d890 20 20 20 20 20 20 73 53 6f 72 74 2e 70 4f 72 64 sSort.pOrd
2d8a0 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 erBy = 0;.
2d8b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 sqlite3VdbeCha
2d8c0 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 73 53 6f ngeToNoop(v, sSo
2d8d0 72 74 2e 61 64 64 72 53 6f 72 74 49 6e 64 65 78 rt.addrSortIndex
2d8e0 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 );. }..
2d8f0 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 /* Evaluate th
2d900 65 20 63 75 72 72 65 6e 74 20 47 52 4f 55 50 20 e current GROUP
2d910 42 59 20 74 65 72 6d 73 20 61 6e 64 20 73 74 6f BY terms and sto
2d920 72 65 20 69 6e 20 62 30 2c 20 62 31 2c 20 62 32 re in b0, b1, b2
2d930 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 28 62 30 .... ** (b0
2d940 20 69 73 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 is memory locat
2d950 69 6f 6e 20 69 42 4d 65 6d 2b 30 2c 20 62 31 20 ion iBMem+0, b1
2d960 69 73 20 69 42 4d 65 6d 2b 31 2c 20 61 6e 64 20 is iBMem+1, and
2d970 73 6f 20 66 6f 72 74 68 29 0a 20 20 20 20 20 20 so forth).
2d980 2a 2a 20 54 68 65 6e 20 63 6f 6d 70 61 72 65 20 ** Then compare
2d990 74 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f 55 the current GROU
2d9a0 50 20 42 59 20 74 65 72 6d 73 20 61 67 61 69 6e P BY terms again
2d9b0 73 74 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 st the GROUP BY
2d9c0 74 65 72 6d 73 0a 20 20 20 20 20 20 2a 2a 20 66 terms. ** f
2d9d0 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73 rom the previous
2d9e0 20 72 6f 77 20 63 75 72 72 65 6e 74 6c 79 20 73 row currently s
2d9f0 74 6f 72 65 64 20 69 6e 20 61 30 2c 20 61 31 2c tored in a0, a1,
2da00 20 61 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a a2.... */.
2da10 20 20 20 20 20 20 61 64 64 72 54 6f 70 4f 66 4c addrTopOfL
2da20 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 oop = sqlite3Vdb
2da30 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b eCurrentAddr(v);
2da40 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 . sqlite3Ex
2da50 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 prCacheClear(pPa
2da60 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 rse);. if(
2da70 67 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 groupBySort ){.
2da80 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 sqlite3Vd
2da90 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 beAddOp3(v, OP_S
2daa0 6f 72 74 65 72 44 61 74 61 2c 20 73 41 67 67 49 orterData, sAggI
2dab0 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 nfo.sortingIdx,
2dac0 73 6f 72 74 4f 75 74 2c 73 6f 72 74 50 54 61 62 sortOut,sortPTab
2dad0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
2dae0 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f for(j=0; j<pGro
2daf0 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b upBy->nExpr; j++
2db00 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 ){. if( g
2db10 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 roupBySort ){.
2db20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 sqlite3V
2db30 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f dbeAddOp3(v, OP_
2db40 43 6f 6c 75 6d 6e 2c 20 73 6f 72 74 50 54 61 62 Column, sortPTab
2db50 2c 20 6a 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 , j, iBMem+j);.
2db60 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 }else{.
2db70 20 20 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f sAggInfo
2db80 2e 64 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b .directMode = 1;
2db90 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 . sqlit
2dba0 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 e3ExprCode(pPars
2dbb0 65 2c 20 70 47 72 6f 75 70 42 79 2d 3e 61 5b 6a e, pGroupBy->a[j
2dbc0 5d 2e 70 45 78 70 72 2c 20 69 42 4d 65 6d 2b 6a ].pExpr, iBMem+j
2dbd0 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
2dbe0 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
2dbf0 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 e3VdbeAddOp4(v,
2dc00 4f 50 5f 43 6f 6d 70 61 72 65 2c 20 69 41 4d 65 OP_Compare, iAMe
2dc10 6d 2c 20 69 42 4d 65 6d 2c 20 70 47 72 6f 75 70 m, iBMem, pGroup
2dc20 42 79 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 By->nExpr,.
2dc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2dc40 20 20 20 20 20 28 63 68 61 72 2a 29 73 71 6c 69 (char*)sqli
2dc50 74 65 33 4b 65 79 49 6e 66 6f 52 65 66 28 70 4b te3KeyInfoRef(pK
2dc60 65 79 49 6e 66 6f 29 2c 20 50 34 5f 4b 45 59 49 eyInfo), P4_KEYI
2dc70 4e 46 4f 29 3b 0a 20 20 20 20 20 20 6a 31 20 3d NFO);. j1 =
2dc80 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 sqlite3VdbeCurr
2dc90 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 entAddr(v);.
2dca0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2dcb0 4f 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 Op3(v, OP_Jump,
2dcc0 6a 31 2b 31 2c 20 30 2c 20 6a 31 2b 31 29 3b 20 j1+1, 0, j1+1);
2dcd0 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 3b VdbeCoverage(v);
2dce0 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 .. /* Gener
2dcf0 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 72 75 ate code that ru
2dd00 6e 73 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 ns whenever the
2dd10 47 52 4f 55 50 20 42 59 20 63 68 61 6e 67 65 73 GROUP BY changes
2dd20 2e 0a 20 20 20 20 20 20 2a 2a 20 43 68 61 6e 67 .. ** Chang
2dd30 65 73 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 es in the GROUP
2dd40 42 59 20 61 72 65 20 64 65 74 65 63 74 65 64 20 BY are detected
2dd50 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 by the previous
2dd60 63 6f 64 65 0a 20 20 20 20 20 20 2a 2a 20 62 6c code. ** bl
2dd70 6f 63 6b 2e 20 20 49 66 20 74 68 65 72 65 20 77 ock. If there w
2dd80 65 72 65 20 6e 6f 20 63 68 61 6e 67 65 73 2c 20 ere no changes,
2dd90 74 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 73 6b this block is sk
2dda0 69 70 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a ipped.. **.
2ddb0 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 63 6f ** This co
2ddc0 64 65 20 63 6f 70 69 65 73 20 63 75 72 72 65 6e de copies curren
2ddd0 74 20 67 72 6f 75 70 20 62 79 20 74 65 72 6d 73 t group by terms
2dde0 20 69 6e 20 62 30 2c 62 31 2c 62 32 2c 2e 2e 2e in b0,b1,b2,...
2ddf0 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 20 74 . ** over t
2de00 6f 20 61 30 2c 61 31 2c 61 32 2e 20 20 49 74 20 o a0,a1,a2. It
2de10 74 68 65 6e 20 63 61 6c 6c 73 20 74 68 65 20 6f then calls the o
2de20 75 74 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 utput subroutine
2de30 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65 . ** and re
2de40 73 65 74 73 20 74 68 65 20 61 67 67 72 65 67 61 sets the aggrega
2de50 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 72 te accumulator r
2de60 65 67 69 73 74 65 72 73 20 69 6e 20 70 72 65 70 egisters in prep
2de70 61 72 61 74 69 6f 6e 0a 20 20 20 20 20 20 2a 2a aration. **
2de80 20 66 6f 72 20 74 68 65 20 6e 65 78 74 20 47 52 for the next GR
2de90 4f 55 50 20 42 59 20 62 61 74 63 68 2e 0a 20 20 OUP BY batch..
2dea0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c */. sql
2deb0 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 ite3ExprCodeMove
2dec0 28 70 50 61 72 73 65 2c 20 69 42 4d 65 6d 2c 20 (pParse, iBMem,
2ded0 69 41 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d iAMem, pGroupBy-
2dee0 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 73 >nExpr);. s
2def0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
2df00 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 (v, OP_Gosub, re
2df10 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72 gOutputRow, addr
2df20 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 OutputRow);.
2df30 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 VdbeComment((v
2df40 2c 20 22 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f , "output one ro
2df50 77 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 w"));. sqli
2df60 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c te3VdbeAddOp2(v,
2df70 20 4f 50 5f 49 66 50 6f 73 2c 20 69 41 62 6f 72 OP_IfPos, iAbor
2df80 74 46 6c 61 67 2c 20 61 64 64 72 45 6e 64 29 3b tFlag, addrEnd);
2df90 20 56 64 62 65 43 6f 76 65 72 61 67 65 28 76 29 VdbeCoverage(v)
2dfa0 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d ;. VdbeComm
2dfb0 65 6e 74 28 28 76 2c 20 22 63 68 65 63 6b 20 61 ent((v, "check a
2dfc0 62 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 bort flag"));.
2dfd0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2dfe0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 ddOp2(v, OP_Gosu
2dff0 62 2c 20 72 65 67 52 65 73 65 74 2c 20 61 64 64 b, regReset, add
2e000 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20 20 56 rReset);. V
2e010 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
2e020 72 65 73 65 74 20 61 63 63 75 6d 75 6c 61 74 6f reset accumulato
2e030 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 r"));.. /*
2e040 55 70 64 61 74 65 20 74 68 65 20 61 67 67 72 65 Update the aggre
2e050 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 gate accumulator
2e060 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63 s based on the c
2e070 6f 6e 74 65 6e 74 20 6f 66 0a 20 20 20 20 20 20 ontent of.
2e080 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 ** the current r
2e090 6f 77 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 ow. */.
2e0a0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d sqlite3VdbeJum
2e0b0 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 pHere(v, j1);.
2e0c0 20 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75 updateAccumu
2e0d0 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 lator(pParse, &s
2e0e0 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 AggInfo);.
2e0f0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2e100 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 2(v, OP_Integer,
2e110 20 31 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a 20 1, iUseFlag);.
2e120 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 VdbeComment
2e130 28 28 76 2c 20 22 69 6e 64 69 63 61 74 65 20 64 ((v, "indicate d
2e140 61 74 61 20 69 6e 20 61 63 63 75 6d 75 6c 61 74 ata in accumulat
2e150 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a or"));.. /*
2e160 20 45 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 End of the loop
2e170 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
2e180 69 66 28 20 67 72 6f 75 70 42 79 53 6f 72 74 20 if( groupBySort
2e190 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 ){. sqlit
2e1a0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
2e1b0 4f 50 5f 53 6f 72 74 65 72 4e 65 78 74 2c 20 73 OP_SorterNext, s
2e1c0 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 AggInfo.sortingI
2e1d0 64 78 2c 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f dx, addrTopOfLoo
2e1e0 70 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 p);. Vdbe
2e1f0 43 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 Coverage(v);.
2e200 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2e210 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e sqlite3WhereEn
2e220 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 d(pWInfo);.
2e230 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 sqlite3VdbeCh
2e240 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 61 64 angeToNoop(v, ad
2e250 64 72 53 6f 72 74 69 6e 67 49 64 78 29 3b 0a 20 drSortingIdx);.
2e260 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a }.. /*
2e270 20 4f 75 74 70 75 74 20 74 68 65 20 66 69 6e 61 Output the fina
2e280 6c 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 0a l row of result.
2e290 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 */. s
2e2a0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 qlite3VdbeAddOp2
2e2b0 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 (v, OP_Gosub, re
2e2c0 67 4f 75 74 70 75 74 52 6f 77 2c 20 61 64 64 72 gOutputRow, addr
2e2d0 4f 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 OutputRow);.
2e2e0 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 VdbeComment((v
2e2f0 2c 20 22 6f 75 74 70 75 74 20 66 69 6e 61 6c 20 , "output final
2e300 72 6f 77 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f row"));.. /
2e310 2a 20 4a 75 6d 70 20 6f 76 65 72 20 74 68 65 20 * Jump over the
2e320 73 75 62 72 6f 75 74 69 6e 65 73 0a 20 20 20 20 subroutines.
2e330 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
2e340 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 e3VdbeAddOp2(v,
2e350 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 OP_Goto, 0, addr
2e360 45 6e 64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 End);.. /*
2e370 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72 6f Generate a subro
2e380 75 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 utine that outpu
2e390 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 ts a single row
2e3a0 6f 66 20 74 68 65 20 72 65 73 75 6c 74 0a 20 20 of the result.
2e3b0 20 20 20 20 2a 2a 20 73 65 74 2e 20 20 54 68 69 ** set. Thi
2e3c0 73 20 73 75 62 72 6f 75 74 69 6e 65 20 66 69 72 s subroutine fir
2e3d0 73 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 st looks at the
2e3e0 69 55 73 65 46 6c 61 67 2e 20 20 49 66 20 69 55 iUseFlag. If iU
2e3f0 73 65 46 6c 61 67 0a 20 20 20 20 20 20 2a 2a 20 seFlag. **
2e400 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 is less than or
2e410 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 equal to zero, t
2e420 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69 73 he subroutine is
2e430 20 61 20 6e 6f 2d 6f 70 2e 20 20 49 66 0a 20 20 a no-op. If.
2e440 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63 65 ** the proce
2e450 73 73 69 6e 67 20 63 61 6c 6c 73 20 66 6f 72 20 ssing calls for
2e460 74 68 65 20 71 75 65 72 79 20 74 6f 20 61 62 6f the query to abo
2e470 72 74 2c 20 74 68 69 73 20 73 75 62 72 6f 75 74 rt, this subrout
2e480 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 ine. ** inc
2e490 72 65 6d 65 6e 74 73 20 74 68 65 20 69 41 62 6f rements the iAbo
2e4a0 72 74 46 6c 61 67 20 6d 65 6d 6f 72 79 20 6c 6f rtFlag memory lo
2e4b0 63 61 74 69 6f 6e 20 62 65 66 6f 72 65 20 72 65 cation before re
2e4c0 74 75 72 6e 69 6e 67 20 69 6e 0a 20 20 20 20 20 turning in.
2e4d0 20 2a 2a 20 6f 72 64 65 72 20 74 6f 20 73 69 67 ** order to sig
2e4e0 6e 61 6c 20 74 68 65 20 63 61 6c 6c 65 72 20 74 nal the caller t
2e4f0 6f 20 61 62 6f 72 74 2e 0a 20 20 20 20 20 20 2a o abort.. *
2e500 2f 0a 20 20 20 20 20 20 61 64 64 72 53 65 74 41 /. addrSetA
2e510 62 6f 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 bort = sqlite3Vd
2e520 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 beCurrentAddr(v)
2e530 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 ;. sqlite3V
2e540 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f dbeAddOp2(v, OP_
2e550 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 41 62 6f Integer, 1, iAbo
2e560 72 74 46 6c 61 67 29 3b 0a 20 20 20 20 20 20 56 rtFlag);. V
2e570 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 dbeComment((v, "
2e580 73 65 74 20 61 62 6f 72 74 20 66 6c 61 67 22 29 set abort flag")
2e590 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 );. sqlite3
2e5a0 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 VdbeAddOp1(v, OP
2e5b0 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 _Return, regOutp
2e5c0 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 73 71 utRow);. sq
2e5d0 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 lite3VdbeResolve
2e5e0 4c 61 62 65 6c 28 76 2c 20 61 64 64 72 4f 75 74 Label(v, addrOut
2e5f0 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 61 putRow);. a
2e600 64 64 72 4f 75 74 70 75 74 52 6f 77 20 3d 20 73 ddrOutputRow = s
2e610 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e qlite3VdbeCurren
2e620 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 tAddr(v);.
2e630 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 sqlite3VdbeAddOp
2e640 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 2(v, OP_IfPos, i
2e650 55 73 65 46 6c 61 67 2c 20 61 64 64 72 4f 75 74 UseFlag, addrOut
2e660 70 75 74 52 6f 77 2b 32 29 3b 20 56 64 62 65 43 putRow+2); VdbeC
2e670 6f 76 65 72 61 67 65 28 76 29 3b 0a 20 20 20 20 overage(v);.
2e680 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 VdbeComment((v
2e690 2c 20 22 47 72 6f 75 70 62 79 20 72 65 73 75 6c , "Groupby resul
2e6a0 74 20 67 65 6e 65 72 61 74 6f 72 20 65 6e 74 72 t generator entr
2e6b0 79 20 70 6f 69 6e 74 22 29 29 3b 0a 20 20 20 20 y point"));.
2e6c0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 sqlite3VdbeAdd
2e6d0 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e Op1(v, OP_Return
2e6e0 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b , regOutputRow);
2e6f0 0a 20 20 20 20 20 20 66 69 6e 61 6c 69 7a 65 41 . finalizeA
2e700 67 67 46 75 6e 63 74 69 6f 6e 73 28 70 50 61 72 ggFunctions(pPar
2e710 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a se, &sAggInfo);.
2e720 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 sqlite3Exp
2e730 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c rIfFalse(pParse,
2e740 20 70 48 61 76 69 6e 67 2c 20 61 64 64 72 4f 75 pHaving, addrOu
2e750 74 70 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49 54 tputRow+1, SQLIT
2e760 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 E_JUMPIFNULL);.
2e770 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 selectInner
2e780 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 Loop(pParse, p,
2e790 70 2d 3e 70 45 4c 69 73 74 2c 20 2d 31 2c 20 26 p->pEList, -1, &
2e7a0 73 53 6f 72 74 2c 0a 20 20 20 20 20 20 20 20 20 sSort,.
2e7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 26 73 44 &sD
2e7c0 69 73 74 69 6e 63 74 2c 20 70 44 65 73 74 2c 0a istinct, pDest,.
2e7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2e7e0 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75 74 addrOutput
2e7f0 52 6f 77 2b 31 2c 20 61 64 64 72 53 65 74 41 62 Row+1, addrSetAb
2e800 6f 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 ort);. sqli
2e810 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c te3VdbeAddOp1(v,
2e820 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f OP_Return, regO
2e830 75 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 utputRow);.
2e840 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c VdbeComment((v,
2e850 20 22 65 6e 64 20 67 72 6f 75 70 62 79 20 72 65 "end groupby re
2e860 73 75 6c 74 20 67 65 6e 65 72 61 74 6f 72 22 29 sult generator")
2e870 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e );.. /* Gen
2e880 65 72 61 74 65 20 61 20 73 75 62 72 6f 75 74 69 erate a subrouti
2e890 6e 65 20 74 68 61 74 20 77 69 6c 6c 20 72 65 73 ne that will res
2e8a0 65 74 20 74 68 65 20 67 72 6f 75 70 2d 62 79 20 et the group-by
2e8b0 61 63 63 75 6d 75 6c 61 74 6f 72 0a 20 20 20 20 accumulator.
2e8c0 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 */. sqlit
2e8d0 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 e3VdbeResolveLab
2e8e0 65 6c 28 76 2c 20 61 64 64 72 52 65 73 65 74 29 el(v, addrReset)
2e8f0 3b 0a 20 20 20 20 20 20 72 65 73 65 74 41 63 63 ;. resetAcc
2e900 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c umulator(pParse,
2e910 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 &sAggInfo);.
2e920 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2e930 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 dOp1(v, OP_Retur
2e940 6e 2c 20 72 65 67 52 65 73 65 74 29 3b 0a 20 20 n, regReset);.
2e950 20 20 20 0a 20 20 20 20 7d 20 2f 2a 20 65 6e 64 . } /* end
2e960 69 66 20 70 47 72 6f 75 70 42 79 2e 20 20 42 65 if pGroupBy. Be
2e970 67 69 6e 20 61 67 67 72 65 67 61 74 65 20 71 75 gin aggregate qu
2e980 65 72 69 65 73 20 77 69 74 68 6f 75 74 20 47 52 eries without GR
2e990 4f 55 50 20 42 59 3a 20 2a 2f 0a 20 20 20 20 65 OUP BY: */. e
2e9a0 6c 73 65 20 7b 0a 20 20 20 20 20 20 45 78 70 72 lse {. Expr
2e9b0 4c 69 73 74 20 2a 70 44 65 6c 20 3d 20 30 3b 0a List *pDel = 0;.
2e9c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f #ifndef SQLITE_O
2e9d0 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 20 MIT_BTREECOUNT.
2e9e0 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 Table *pTab
2e9f0 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 54 61 ;. if( (pTa
2ea00 62 20 3d 20 69 73 53 69 6d 70 6c 65 43 6f 75 6e b = isSimpleCoun
2ea10 74 28 70 2c 20 26 73 41 67 67 49 6e 66 6f 29 29 t(p, &sAggInfo))
2ea20 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f !=0 ){. /
2ea30 2a 20 49 66 20 69 73 53 69 6d 70 6c 65 43 6f 75 * If isSimpleCou
2ea40 6e 74 28 29 20 72 65 74 75 72 6e 73 20 61 20 70 nt() returns a p
2ea50 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 61 62 6c ointer to a Tabl
2ea60 65 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 e structure, the
2ea70 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 n. ** the
2ea80 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 SQL statement i
2ea90 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 s of the form:.
2eaa0 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 **.
2eab0 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 63 6f ** SELECT co
2eac0 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62 6c unt(*) FROM <tbl
2ead0 3e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 >. **.
2eae0 20 20 20 20 20 2a 2a 20 77 68 65 72 65 20 74 68 ** where th
2eaf0 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 e Table structur
2eb00 65 20 72 65 74 75 72 6e 65 64 20 72 65 70 72 65 e returned repre
2eb10 73 65 6e 74 73 20 74 61 62 6c 65 20 3c 74 62 6c sents table <tbl
2eb20 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 >.. **.
2eb30 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 73 74 ** This st
2eb40 61 74 65 6d 65 6e 74 20 69 73 20 73 6f 20 63 6f atement is so co
2eb50 6d 6d 6f 6e 20 74 68 61 74 20 69 74 20 69 73 20 mmon that it is
2eb60 6f 70 74 69 6d 69 7a 65 64 20 73 70 65 63 69 61 optimized specia
2eb70 6c 6c 79 2e 20 54 68 65 0a 20 20 20 20 20 20 20 lly. The.
2eb80 20 2a 2a 20 4f 50 5f 43 6f 75 6e 74 20 69 6e 73 ** OP_Count ins
2eb90 74 72 75 63 74 69 6f 6e 20 69 73 20 65 78 65 63 truction is exec
2eba0 75 74 65 64 20 65 69 74 68 65 72 20 6f 6e 20 74 uted either on t
2ebb0 68 65 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 he intkey table
2ebc0 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 that. **
2ebd0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64 61 74 contains the dat
2ebe0 61 20 66 6f 72 20 74 61 62 6c 65 20 3c 74 62 6c a for table <tbl
2ebf0 3e 20 6f 72 20 6f 6e 20 6f 6e 65 20 6f 66 20 69 > or on one of i
2ec00 74 73 20 69 6e 64 65 78 65 73 2e 20 49 74 0a 20 ts indexes. It.
2ec10 20 20 20 20 20 20 20 2a 2a 20 69 73 20 62 65 74 ** is bet
2ec20 74 65 72 20 74 6f 20 65 78 65 63 75 74 65 20 74 ter to execute t
2ec30 68 65 20 6f 70 20 6f 6e 20 61 6e 20 69 6e 64 65 he op on an inde
2ec40 78 2c 20 61 73 20 69 6e 64 65 78 65 73 20 61 72 x, as indexes ar
2ec50 65 20 61 6c 6d 6f 73 74 0a 20 20 20 20 20 20 20 e almost.
2ec60 20 2a 2a 20 61 6c 77 61 79 73 20 73 70 72 65 61 ** always sprea
2ec70 64 20 61 63 72 6f 73 73 20 6c 65 73 73 20 70 61 d across less pa
2ec80 67 65 73 20 74 68 61 6e 20 74 68 65 69 72 20 63 ges than their c
2ec90 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 orresponding tab
2eca0 6c 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a les.. */.
2ecb0 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e const in
2ecc0 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 t iDb = sqlite3S
2ecd0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 chemaToIndex(pPa
2ece0 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 rse->db, pTab->p
2ecf0 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 Schema);.
2ed00 20 63 6f 6e 73 74 20 69 6e 74 20 69 43 73 72 20 const int iCsr
2ed10 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b = pParse->nTab++
2ed20 3b 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 ; /* Cursor
2ed30 74 6f 20 73 63 61 6e 20 62 2d 74 72 65 65 20 2a to scan b-tree *
2ed40 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 /. Index
2ed50 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 *pIdx;
2ed60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f /
2ed70 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 * Iterator varia
2ed80 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 4b ble */. K
2ed90 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f eyInfo *pKeyInfo
2eda0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 = 0;
2edb0 20 20 20 20 2f 2a 20 4b 65 79 69 6e 66 6f 20 66 /* Keyinfo f
2edc0 6f 72 20 73 63 61 6e 6e 65 64 20 69 6e 64 65 78 or scanned index
2edd0 20 2a 2f 0a 20 20 20 20 20 20 20 20 49 6e 64 65 */. Inde
2ede0 78 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 20 20 x *pBest = 0;
2edf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
2ee00 20 2f 2a 20 42 65 73 74 20 69 6e 64 65 78 20 66 /* Best index f
2ee10 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 ound so far */.
2ee20 20 20 20 20 20 20 20 69 6e 74 20 69 52 6f 6f 74 int iRoot
2ee30 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 20 20 = pTab->tnum;
2ee40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 /* R
2ee50 6f 6f 74 20 70 61 67 65 20 6f 66 20 73 63 61 6e oot page of scan
2ee60 6e 65 64 20 62 2d 74 72 65 65 20 2a 2f 0a 0a 20 ned b-tree */..
2ee70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f sqlite3Co
2ee80 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 deVerifySchema(p
2ee90 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 Parse, iDb);.
2eea0 20 20 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c sqlite3Tabl
2eeb0 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 eLock(pParse, iD
2eec0 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 b, pTab->tnum, 0
2eed0 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a , pTab->zName);.
2eee0 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 65 61 72 . /* Sear
2eef0 63 68 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 ch for the index
2ef00 20 74 68 61 74 20 68 61 73 20 74 68 65 20 6c 6f that has the lo
2ef10 77 65 73 74 20 73 63 61 6e 20 63 6f 73 74 2e 0a west scan cost..
2ef20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
2ef30 20 20 20 2a 2a 20 28 32 30 31 31 2d 30 34 2d 31 ** (2011-04-1
2ef40 35 29 20 44 6f 20 6e 6f 74 20 64 6f 20 61 20 66 5) Do not do a f
2ef50 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 6e 20 75 ull scan of an u
2ef60 6e 6f 72 64 65 72 65 64 20 69 6e 64 65 78 2e 0a nordered index..
2ef70 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
2ef80 20 20 20 2a 2a 20 28 32 30 31 33 2d 31 30 2d 30 ** (2013-10-0
2ef90 33 29 20 44 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 3) Do not count
2efa0 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20 61 the entries in a
2efb0 20 70 61 72 74 69 61 6c 20 69 6e 64 65 78 2e 0a partial index..
2efc0 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 **.
2efd0 20 20 20 2a 2a 20 49 6e 20 70 72 61 63 74 69 63 ** In practic
2efe0 65 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 e the KeyInfo st
2eff0 72 75 63 74 75 72 65 20 77 69 6c 6c 20 6e 6f 74 ructure will not
2f000 20 62 65 20 75 73 65 64 2e 20 49 74 20 69 73 20 be used. It is
2f010 6f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a only . **
2f020 20 70 61 73 73 65 64 20 74 6f 20 6b 65 65 70 20 passed to keep
2f030 4f 50 5f 4f 70 65 6e 52 65 61 64 20 68 61 70 70 OP_OpenRead happ
2f040 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 y.. */.
2f050 20 20 20 20 20 20 69 66 28 20 21 48 61 73 52 6f if( !HasRo
2f060 77 69 64 28 70 54 61 62 29 20 29 20 70 42 65 73 wid(pTab) ) pBes
2f070 74 20 3d 20 73 71 6c 69 74 65 33 50 72 69 6d 61 t = sqlite3Prima
2f080 72 79 4b 65 79 49 6e 64 65 78 28 70 54 61 62 29 ryKeyIndex(pTab)
2f090 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 49 ;. for(pI
2f0a0 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b dx=pTab->pIndex;
2f0b0 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 pIdx; pIdx=pIdx
2f0c0 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 ->pNext){.
2f0d0 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 62 55 if( pIdx->bU
2f0e0 6e 6f 72 64 65 72 65 64 3d 3d 30 0a 20 20 20 20 nordered==0.
2f0f0 20 20 20 20 20 20 20 26 26 20 70 49 64 78 2d 3e && pIdx->
2f100 73 7a 49 64 78 52 6f 77 3c 70 54 61 62 2d 3e 73 szIdxRow<pTab->s
2f110 7a 54 61 62 52 6f 77 0a 20 20 20 20 20 20 20 20 zTabRow.
2f120 20 20 20 26 26 20 70 49 64 78 2d 3e 70 50 61 72 && pIdx->pPar
2f130 74 49 64 78 57 68 65 72 65 3d 3d 30 0a 20 20 20 tIdxWhere==0.
2f140 20 20 20 20 20 20 20 20 26 26 20 28 21 70 42 65 && (!pBe
2f150 73 74 20 7c 7c 20 70 49 64 78 2d 3e 73 7a 49 64 st || pIdx->szId
2f160 78 52 6f 77 3c 70 42 65 73 74 2d 3e 73 7a 49 64 xRow<pBest->szId
2f170 78 52 6f 77 29 0a 20 20 20 20 20 20 20 20 20 20 xRow).
2f180 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 ){. p
2f190 42 65 73 74 20 3d 20 70 49 64 78 3b 0a 20 20 20 Best = pIdx;.
2f1a0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
2f1b0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 }. if( p
2f1c0 42 65 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 Best ){.
2f1d0 20 20 69 52 6f 6f 74 20 3d 20 70 42 65 73 74 2d iRoot = pBest-
2f1e0 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 20 20 20 >tnum;.
2f1f0 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 pKeyInfo = sqli
2f200 74 65 33 4b 65 79 49 6e 66 6f 4f 66 49 6e 64 65 te3KeyInfoOfInde
2f210 78 28 70 50 61 72 73 65 2c 20 70 42 65 73 74 29 x(pParse, pBest)
2f220 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 ;. }..
2f230 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20 72 /* Open a r
2f240 65 61 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 2c ead-only cursor,
2f250 20 65 78 65 63 75 74 65 20 74 68 65 20 4f 50 5f execute the OP_
2f260 43 6f 75 6e 74 2c 20 63 6c 6f 73 65 20 74 68 65 Count, close the
2f270 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 20 20 cursor. */.
2f280 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2f290 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 4f ddOp4Int(v, OP_O
2f2a0 70 65 6e 52 65 61 64 2c 20 69 43 73 72 2c 20 69 penRead, iCsr, i
2f2b0 52 6f 6f 74 2c 20 69 44 62 2c 20 31 29 3b 0a 20 Root, iDb, 1);.
2f2c0 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 if( pKeyI
2f2d0 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 nfo ){.
2f2e0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e sqlite3VdbeChan
2f2f0 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 geP4(v, -1, (cha
2f300 72 20 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 r *)pKeyInfo, P4
2f310 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 _KEYINFO);.
2f320 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c }. sql
2f330 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 ite3VdbeAddOp2(v
2f340 2c 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 43 73 72 , OP_Count, iCsr
2f350 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 , sAggInfo.aFunc
2f360 5b 30 5d 2e 69 4d 65 6d 29 3b 0a 20 20 20 20 20 [0].iMem);.
2f370 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 sqlite3VdbeAd
2f380 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 dOp1(v, OP_Close
2f390 2c 20 69 43 73 72 29 3b 0a 20 20 20 20 20 20 20 , iCsr);.
2f3a0 20 65 78 70 6c 61 69 6e 53 69 6d 70 6c 65 43 6f explainSimpleCo
2f3b0 75 6e 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 unt(pParse, pTab
2f3c0 2c 20 70 42 65 73 74 29 3b 0a 20 20 20 20 20 20 , pBest);.
2f3d0 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 }else.#endif /*
2f3e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 SQLITE_OMIT_BTRE
2f3f0 45 43 4f 55 4e 54 20 2a 2f 0a 20 20 20 20 20 20 ECOUNT */.
2f400 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 {. /* Che
2f410 63 6b 20 69 66 20 74 68 65 20 71 75 65 72 79 20 ck if the query
2f420 69 73 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65 is of one of the
2f430 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 following forms
2f440 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 :. **.
2f450 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43 54 ** SELECT
2f460 20 6d 69 6e 28 78 29 20 46 52 4f 4d 20 2e 2e 2e min(x) FROM ...
2f470 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 . ** SE
2f480 4c 45 43 54 20 6d 61 78 28 78 29 20 46 52 4f 4d LECT max(x) FROM
2f490 20 2e 2e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a .... **.
2f4a0 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20 69 74 ** If it
2f4b0 20 69 73 2c 20 74 68 65 6e 20 61 73 6b 20 74 68 is, then ask th
2f4c0 65 20 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e e code in where.
2f4d0 63 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 c to attempt to
2f4e0 73 6f 72 74 20 72 65 73 75 6c 74 73 0a 20 20 20 sort results.
2f4f0 20 20 20 20 20 2a 2a 20 61 73 20 69 66 20 74 68 ** as if th
2f500 65 72 65 20 77 61 73 20 61 6e 20 22 4f 52 44 45 ere was an "ORDE
2f510 52 20 4f 4e 20 78 22 20 6f 72 20 22 4f 52 44 45 R ON x" or "ORDE
2f520 52 20 4f 4e 20 78 20 44 45 53 43 22 20 63 6c 61 R ON x DESC" cla
2f530 75 73 65 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a use. . **
2f540 20 49 66 20 77 68 65 72 65 2e 63 20 69 73 20 61 If where.c is a
2f550 62 6c 65 20 74 6f 20 70 72 6f 64 75 63 65 20 72 ble to produce r
2f560 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20 69 6e esults sorted in
2f570 20 74 68 69 73 20 6f 72 64 65 72 2c 20 74 68 65 this order, the
2f580 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 64 64 n. ** add
2f590 20 76 64 62 65 20 63 6f 64 65 20 74 6f 20 62 72 vdbe code to br
2f5a0 65 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 70 eak out of the p
2f5b0 72 6f 63 65 73 73 69 6e 67 20 6c 6f 6f 70 20 61 rocessing loop a
2f5c0 66 74 65 72 20 74 68 65 20 0a 20 20 20 20 20 20 fter the .
2f5d0 20 20 2a 2a 20 66 69 72 73 74 20 69 74 65 72 61 ** first itera
2f5e0 74 69 6f 6e 20 28 73 69 6e 63 65 20 74 68 65 20 tion (since the
2f5f0 66 69 72 73 74 20 69 74 65 72 61 74 69 6f 6e 20 first iteration
2f600 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 73 20 0a of the loop is .
2f610 20 20 20 20 20 20 20 20 2a 2a 20 67 75 61 72 61 ** guara
2f620 6e 74 65 65 64 20 74 6f 20 6f 70 65 72 61 74 65 nteed to operate
2f630 20 6f 6e 20 74 68 65 20 72 6f 77 20 77 69 74 68 on the row with
2f640 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 72 20 the minimum or
2f650 6d 61 78 69 6d 75 6d 20 0a 20 20 20 20 20 20 20 maximum .
2f660 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 78 2c 20 ** value of x,
2f670 74 68 65 20 6f 6e 6c 79 20 72 6f 77 20 72 65 71 the only row req
2f680 75 69 72 65 64 29 2e 0a 20 20 20 20 20 20 20 20 uired)..
2f690 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 **. ** A
2f6a0 73 70 65 63 69 61 6c 20 66 6c 61 67 20 6d 75 73 special flag mus
2f6b0 74 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 t be passed to s
2f6c0 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e qlite3WhereBegin
2f6d0 28 29 20 74 6f 20 73 6c 69 67 68 74 6c 79 0a 20 () to slightly.
2f6e0 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 69 66 79 ** modify
2f6f0 20 62 65 68 61 76 69 6f 72 20 61 73 20 66 6f 6c behavior as fol
2f700 6c 6f 77 73 3a 0a 20 20 20 20 20 20 20 20 2a 2a lows:. **
2f710 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 . ** +
2f720 49 66 20 74 68 65 20 71 75 65 72 79 20 69 73 20 If the query is
2f730 61 20 22 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 a "SELECT min(x)
2f740 22 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 6f 70 ", then the loop
2f750 20 63 6f 64 65 64 20 62 79 0a 20 20 20 20 20 20 coded by.
2f760 20 20 2a 2a 20 20 20 20 20 77 68 65 72 65 2e 63 ** where.c
2f770 20 73 68 6f 75 6c 64 20 6e 6f 74 20 69 74 65 72 should not iter
2f780 61 74 65 20 6f 76 65 72 20 61 6e 79 20 76 61 6c ate over any val
2f790 75 65 73 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 ues with a NULL
2f7a0 76 61 6c 75 65 0a 20 20 20 20 20 20 20 20 2a 2a value. **
2f7b0 20 20 20 20 20 66 6f 72 20 78 2e 0a 20 20 20 20 for x..
2f7c0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a **. *
2f7d0 2a 20 20 20 2b 20 54 68 65 20 6f 70 74 69 6d 69 * + The optimi
2f7e0 7a 65 72 20 63 6f 64 65 20 69 6e 20 77 68 65 72 zer code in wher
2f7f0 65 2e 63 20 28 74 68 65 20 74 68 69 6e 67 20 74 e.c (the thing t
2f800 68 61 74 20 64 65 63 69 64 65 73 20 77 68 69 63 hat decides whic
2f810 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 h. **
2f820 20 69 6e 64 65 78 20 6f 72 20 69 6e 64 69 63 65 index or indice
2f830 73 20 74 6f 20 75 73 65 29 20 73 68 6f 75 6c 64 s to use) should
2f840 20 70 6c 61 63 65 20 61 20 64 69 66 66 65 72 65 place a differe
2f850 6e 74 20 70 72 69 6f 72 69 74 79 20 6f 6e 20 0a nt priority on .
2f860 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 73 ** s
2f870 61 74 69 73 66 79 69 6e 67 20 74 68 65 20 27 4f atisfying the 'O
2f880 52 44 45 52 20 42 59 27 20 63 6c 61 75 73 65 20 RDER BY' clause
2f890 74 68 61 6e 20 69 74 20 64 6f 65 73 20 69 6e 20 than it does in
2f8a0 6f 74 68 65 72 20 63 61 73 65 73 2e 0a 20 20 20 other cases..
2f8b0 20 20 20 20 20 2a 2a 20 20 20 20 20 52 65 66 65 ** Refe
2f8c0 72 20 74 6f 20 63 6f 64 65 20 61 6e 64 20 63 6f r to code and co
2f8d0 6d 6d 65 6e 74 73 20 69 6e 20 77 68 65 72 65 2e mments in where.
2f8e0 63 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20 c for details..
2f8f0 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 */.
2f900 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4d 69 6e ExprList *pMin
2f910 4d 61 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 Max = 0;.
2f920 20 75 38 20 66 6c 61 67 20 3d 20 57 48 45 52 45 u8 flag = WHERE
2f930 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b _ORDERBY_NORMAL;
2f940 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 . .
2f950 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 47 72 assert( p->pGr
2f960 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 oupBy==0 );.
2f970 20 20 20 20 61 73 73 65 72 74 28 20 66 6c 61 67 assert( flag
2f980 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 ==0 );. i
2f990 66 28 20 70 2d 3e 70 48 61 76 69 6e 67 3d 3d 30 f( p->pHaving==0
2f9a0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c ){. fl
2f9b0 61 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79 ag = minMaxQuery
2f9c0 28 26 73 41 67 67 49 6e 66 6f 2c 20 26 70 4d 69 (&sAggInfo, &pMi
2f9d0 6e 4d 61 78 29 3b 0a 20 20 20 20 20 20 20 20 7d nMax);. }
2f9e0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 . assert(
2f9f0 20 66 6c 61 67 3d 3d 30 20 7c 7c 20 28 70 4d 69 flag==0 || (pMi
2fa00 6e 4d 61 78 21 3d 30 20 26 26 20 70 4d 69 6e 4d nMax!=0 && pMinM
2fa10 61 78 2d 3e 6e 45 78 70 72 3d 3d 31 29 20 29 3b ax->nExpr==1) );
2fa20 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c .. if( fl
2fa30 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ag ){.
2fa40 70 4d 69 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 pMinMax = sqlite
2fa50 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 3ExprListDup(db,
2fa60 20 70 4d 69 6e 4d 61 78 2c 20 30 29 3b 0a 20 20 pMinMax, 0);.
2fa70 20 20 20 20 20 20 20 20 70 44 65 6c 20 3d 20 70 pDel = p
2fa80 4d 69 6e 4d 61 78 3b 0a 20 20 20 20 20 20 20 20 MinMax;.
2fa90 20 20 69 66 28 20 70 4d 69 6e 4d 61 78 20 26 26 if( pMinMax &&
2faa0 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c !db->mallocFail
2fab0 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 ed ){.
2fac0 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e pMinMax->a[0].
2fad0 73 6f 72 74 4f 72 64 65 72 20 3d 20 66 6c 61 67 sortOrder = flag
2fae0 21 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f !=WHERE_ORDERBY_
2faf0 4d 49 4e 20 3f 31 3a 30 3b 0a 20 20 20 20 20 20 MIN ?1:0;.
2fb00 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 pMinMax->a
2fb10 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 [0].pExpr->op =
2fb20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 TK_COLUMN;.
2fb30 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
2fb40 0a 20 20 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 . . /* T
2fb50 68 69 73 20 63 61 73 65 20 72 75 6e 73 20 69 66 his case runs if
2fb60 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 68 the aggregate h
2fb70 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20 63 as no GROUP BY c
2fb80 6c 61 75 73 65 2e 20 20 54 68 65 0a 20 20 20 20 lause. The.
2fb90 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e ** processin
2fba0 67 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c 65 g is much simple
2fbb0 72 20 73 69 6e 63 65 20 74 68 65 72 65 20 69 73 r since there is
2fbc0 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 72 only a single r
2fbd0 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 ow. ** of
2fbe0 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20 20 output..
2fbf0 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73 65 */. rese
2fc00 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 tAccumulator(pPa
2fc10 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b rse, &sAggInfo);
2fc20 0a 20 20 20 20 20 20 20 20 70 57 49 6e 66 6f 20 . pWInfo
2fc30 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 = sqlite3WhereBe
2fc40 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 gin(pParse, pTab
2fc50 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 70 4d List, pWhere, pM
2fc60 69 6e 4d 61 78 2c 30 2c 66 6c 61 67 2c 30 29 3b inMax,0,flag,0);
2fc70 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 57 49 . if( pWI
2fc80 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 nfo==0 ){.
2fc90 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c sqlite3ExprL
2fca0 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 istDelete(db, pD
2fcb0 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 el);. g
2fcc0 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a oto select_end;.
2fcd0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2fce0 20 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 updateAccumula
2fcf0 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 tor(pParse, &sAg
2fd00 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 gInfo);.
2fd10 61 73 73 65 72 74 28 20 70 4d 69 6e 4d 61 78 3d assert( pMinMax=
2fd20 3d 30 20 7c 7c 20 70 4d 69 6e 4d 61 78 2d 3e 6e =0 || pMinMax->n
2fd30 45 78 70 72 3d 3d 31 20 29 3b 0a 20 20 20 20 20 Expr==1 );.
2fd40 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 68 if( sqlite3Wh
2fd50 65 72 65 49 73 4f 72 64 65 72 65 64 28 70 57 49 ereIsOrdered(pWI
2fd60 6e 66 6f 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 nfo)>0 ){.
2fd70 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 sqlite3VdbeA
2fd80 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f ddOp2(v, OP_Goto
2fd90 2c 20 30 2c 20 73 71 6c 69 74 65 33 57 68 65 72 , 0, sqlite3Wher
2fda0 65 42 72 65 61 6b 4c 61 62 65 6c 28 70 57 49 6e eBreakLabel(pWIn
2fdb0 66 6f 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 fo));.
2fdc0 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 VdbeComment((v,
2fdd0 22 25 73 28 29 20 62 79 20 69 6e 64 65 78 22 2c "%s() by index",
2fde0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 .
2fdf0 20 28 66 6c 61 67 3d 3d 57 48 45 52 45 5f 4f 52 (flag==WHERE_OR
2fe00 44 45 52 42 59 5f 4d 49 4e 3f 22 6d 69 6e 22 3a DERBY_MIN?"min":
2fe10 22 6d 61 78 22 29 29 29 3b 0a 20 20 20 20 20 20 "max")));.
2fe20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 }. sqli
2fe30 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e te3WhereEnd(pWIn
2fe40 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 66 69 6e fo);. fin
2fe50 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e alizeAggFunction
2fe60 73 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 s(pParse, &sAggI
2fe70 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 nfo);. }..
2fe80 20 20 20 20 20 73 53 6f 72 74 2e 70 4f 72 64 65 sSort.pOrde
2fe90 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 rBy = 0;. s
2fea0 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 qlite3ExprIfFals
2feb0 65 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e e(pParse, pHavin
2fec0 67 2c 20 61 64 64 72 45 6e 64 2c 20 53 51 4c 49 g, addrEnd, SQLI
2fed0 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a TE_JUMPIFNULL);.
2fee0 20 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 selectInne
2fef0 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c rLoop(pParse, p,
2ff00 20 70 2d 3e 70 45 4c 69 73 74 2c 20 2d 31 2c 20 p->pEList, -1,
2ff10 30 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 0, 0, .
2ff20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 65 pDe
2ff30 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61 64 64 st, addrEnd, add
2ff40 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 73 71 6c rEnd);. sql
2ff50 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 ite3ExprListDele
2ff60 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 te(db, pDel);.
2ff70 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 }. sqlite3V
2ff80 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 dbeResolveLabel(
2ff90 76 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 v, addrEnd);.
2ffa0 20 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 61 . } /* endif a
2ffb0 67 67 72 65 67 61 74 65 20 71 75 65 72 79 20 2a ggregate query *
2ffc0 2f 0a 0a 20 20 69 66 28 20 73 44 69 73 74 69 6e /.. if( sDistin
2ffd0 63 74 2e 65 54 6e 63 74 54 79 70 65 3d 3d 57 48 ct.eTnctType==WH
2ffe0 45 52 45 5f 44 49 53 54 49 4e 43 54 5f 55 4e 4f ERE_DISTINCT_UNO
2fff0 52 44 45 52 45 44 20 29 7b 0a 20 20 20 20 65 78 RDERED ){. ex
30000 70 6c 61 69 6e 54 65 6d 70 54 61 62 6c 65 28 70 plainTempTable(p
30010 50 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43 54 Parse, "DISTINCT
30020 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 ");. }.. /* If
30030 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 there is an ORD
30040 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 ER BY clause, th
30050 65 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 73 6f en we need to so
30060 72 74 20 74 68 65 20 72 65 73 75 6c 74 73 0a 20 rt the results.
30070 20 2a 2a 20 61 6e 64 20 73 65 6e 64 20 74 68 65 ** and send the
30080 6d 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 m to the callbac
30090 6b 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a 20 20 k one by one..
300a0 2a 2f 0a 20 20 69 66 28 20 73 53 6f 72 74 2e 70 */. if( sSort.p
300b0 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 65 OrderBy ){. e
300c0 78 70 6c 61 69 6e 54 65 6d 70 54 61 62 6c 65 28 xplainTempTable(
300d0 70 50 61 72 73 65 2c 20 73 53 6f 72 74 2e 6e 4f pParse, sSort.nO
300e0 42 53 61 74 3e 30 20 3f 20 22 52 49 47 48 54 20 BSat>0 ? "RIGHT
300f0 50 41 52 54 20 4f 46 20 4f 52 44 45 52 20 42 59 PART OF ORDER BY
30100 22 3a 22 4f 52 44 45 52 20 42 59 22 29 3b 0a 20 ":"ORDER BY");.
30110 20 20 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54 generateSortT
30120 61 69 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 26 ail(pParse, p, &
30130 73 53 6f 72 74 2c 20 70 45 4c 69 73 74 2d 3e 6e sSort, pEList->n
30140 45 78 70 72 2c 20 70 44 65 73 74 29 3b 0a 20 20 Expr, pDest);.
30150 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 }.. /* Jump her
30160 65 20 74 6f 20 73 6b 69 70 20 74 68 69 73 20 71 e to skip this q
30170 75 65 72 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 uery. */. sqli
30180 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 te3VdbeResolveLa
30190 62 65 6c 28 76 2c 20 69 45 6e 64 29 3b 0a 0a 20 bel(v, iEnd);..
301a0 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 77 /* The SELECT w
301b0 61 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 as successfully
301c0 63 6f 64 65 64 2e 20 20 20 53 65 74 20 74 68 65 coded. Set the
301d0 20 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 20 return code to
301e0 30 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 0. ** to indica
301f0 74 65 20 6e 6f 20 65 72 72 6f 72 73 2e 0a 20 20 te no errors..
30200 2a 2f 0a 20 20 72 63 20 3d 20 30 3b 0a 0a 20 20 */. rc = 0;..
30210 2f 2a 20 43 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 /* Control jumps
30220 20 74 6f 20 68 65 72 65 20 69 66 20 61 6e 20 65 to here if an e
30230 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 rror is encounte
30240 72 65 64 20 61 62 6f 76 65 2c 20 6f 72 20 75 70 red above, or up
30250 6f 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73 73 66 on. ** successf
30260 75 6c 20 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 ul coding of the
30270 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2f 0a 73 65 SELECT.. */.se
30280 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 65 78 70 6c lect_end:. expl
30290 61 69 6e 53 65 74 49 6e 74 65 67 65 72 28 70 50 ainSetInteger(pP
302a0 61 72 73 65 2d 3e 69 53 65 6c 65 63 74 49 64 2c arse->iSelectId,
302b0 20 69 52 65 73 74 6f 72 65 53 65 6c 65 63 74 49 iRestoreSelectI
302c0 64 29 3b 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 d);.. /* Identi
302d0 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 fy column names
302e0 69 66 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 if results of th
302f0 65 20 53 45 4c 45 43 54 20 61 72 65 20 74 6f 20 e SELECT are to
30300 62 65 20 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a be output.. */.
30310 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 if( rc==SQLITE
30320 5f 4f 4b 20 26 26 20 70 44 65 73 74 2d 3e 65 44 _OK && pDest->eD
30330 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 est==SRT_Output
30340 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 43 ){. generateC
30350 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 olumnNames(pPars
30360 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 45 4c e, pTabList, pEL
30370 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c ist);. }.. sql
30380 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 73 ite3DbFree(db, s
30390 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a 20 AggInfo.aCol);.
303a0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 sqlite3DbFree(d
303b0 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e b, sAggInfo.aFun
303c0 63 29 3b 0a 23 69 66 20 53 45 4c 45 43 54 54 52 c);.#if SELECTTR
303d0 41 43 45 5f 45 4e 41 42 4c 45 44 0a 20 20 53 45 ACE_ENABLED. SE
303e0 4c 45 43 54 54 52 41 43 45 28 31 2c 70 50 61 72 LECTTRACE(1,pPar
303f0 73 65 2c 70 2c 28 22 65 6e 64 20 70 72 6f 63 65 se,p,("end proce
30400 73 73 69 6e 67 5c 6e 22 29 29 3b 0a 20 20 70 50 ssing\n"));. pP
30410 61 72 73 65 2d 3e 6e 53 65 6c 65 63 74 49 6e 64 arse->nSelectInd
30420 65 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 ent--;.#endif.
30430 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 return rc;.}..#i
30440 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 fdef SQLITE_DEBU
30450 47 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 G./*.** Generate
30460 20 61 20 68 75 6d 61 6e 2d 72 65 61 64 61 62 6c a human-readabl
30470 65 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 e description of
30480 20 61 20 74 68 65 20 53 65 6c 65 63 74 20 6f 62 a the Select ob
30490 6a 65 63 74 2e 0a 2a 2f 0a 76 6f 69 64 20 73 71 ject..*/.void sq
304a0 6c 69 74 65 33 54 72 65 65 56 69 65 77 53 65 6c lite3TreeViewSel
304b0 65 63 74 28 54 72 65 65 56 69 65 77 20 2a 70 56 ect(TreeView *pV
304c0 69 65 77 2c 20 63 6f 6e 73 74 20 53 65 6c 65 63 iew, const Selec
304d0 74 20 2a 70 2c 20 75 38 20 6d 6f 72 65 54 6f 46 t *p, u8 moreToF
304e0 6f 6c 6c 6f 77 29 7b 0a 20 20 69 6e 74 20 6e 20 ollow){. int n
304f0 3d 20 30 3b 0a 20 20 70 56 69 65 77 20 3d 20 73 = 0;. pView = s
30500 71 6c 69 74 65 33 54 72 65 65 56 69 65 77 50 75 qlite3TreeViewPu
30510 73 68 28 70 56 69 65 77 2c 20 6d 6f 72 65 54 6f sh(pView, moreTo
30520 46 6f 6c 6c 6f 77 29 3b 0a 20 20 73 71 6c 69 74 Follow);. sqlit
30530 65 33 54 72 65 65 56 69 65 77 4c 69 6e 65 28 70 e3TreeViewLine(p
30540 56 69 65 77 2c 20 22 53 45 4c 45 43 54 25 73 25 View, "SELECT%s%
30550 73 22 2c 0a 20 20 20 20 28 28 70 2d 3e 73 65 6c s",. ((p->sel
30560 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 Flags & SF_Disti
30570 6e 63 74 29 20 3f 20 22 20 44 49 53 54 49 4e 43 nct) ? " DISTINC
30580 54 22 20 3a 20 22 22 29 2c 0a 20 20 20 20 28 28 T" : ""),. ((
30590 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 p->selFlags & SF
305a0 5f 41 67 67 72 65 67 61 74 65 29 20 3f 20 22 20 _Aggregate) ? "
305b0 61 67 67 5f 66 6c 61 67 22 20 3a 20 22 22 29 0a agg_flag" : "").
305c0 20 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 53 );. if( p->pS
305d0 72 63 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e 6e rc && p->pSrc->n
305e0 53 72 63 20 29 20 6e 2b 2b 3b 0a 20 20 69 66 28 Src ) n++;. if(
305f0 20 70 2d 3e 70 57 68 65 72 65 20 29 20 6e 2b 2b p->pWhere ) n++
30600 3b 0a 20 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 ;. if( p->pGrou
30610 70 42 79 20 29 20 6e 2b 2b 3b 0a 20 20 69 66 28 pBy ) n++;. if(
30620 20 70 2d 3e 70 48 61 76 69 6e 67 20 29 20 6e 2b p->pHaving ) n+
30630 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 +;. if( p->pOrd
30640 65 72 42 79 20 29 20 6e 2b 2b 3b 0a 20 20 69 66 erBy ) n++;. if
30650 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 6e 2b ( p->pLimit ) n+
30660 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4f 66 66 +;. if( p->pOff
30670 73 65 74 20 29 20 6e 2b 2b 3b 0a 20 20 69 66 28 set ) n++;. if(
30680 20 70 2d 3e 70 50 72 69 6f 72 20 29 20 6e 2b 2b p->pPrior ) n++
30690 3b 0a 20 20 73 71 6c 69 74 65 33 54 72 65 65 56 ;. sqlite3TreeV
306a0 69 65 77 45 78 70 72 4c 69 73 74 28 70 56 69 65 iewExprList(pVie
306b0 77 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 28 6e w, p->pEList, (n
306c0 2d 2d 29 3e 30 2c 20 22 72 65 73 75 6c 74 2d 73 --)>0, "result-s
306d0 65 74 22 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 et");. if( p->p
306e0 53 72 63 20 26 26 20 70 2d 3e 70 53 72 63 2d 3e Src && p->pSrc->
306f0 6e 53 72 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 nSrc ){. int
30700 69 3b 0a 20 20 20 20 70 56 69 65 77 20 3d 20 73 i;. pView = s
30710 71 6c 69 74 65 33 54 72 65 65 56 69 65 77 50 75 qlite3TreeViewPu
30720 73 68 28 70 56 69 65 77 2c 20 28 6e 2d 2d 29 3e sh(pView, (n--)>
30730 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 0);. sqlite3T
30740 72 65 65 56 69 65 77 4c 69 6e 65 28 70 56 69 65 reeViewLine(pVie
30750 77 2c 20 22 46 52 4f 4d 22 29 3b 0a 20 20 20 20 w, "FROM");.
30760 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 for(i=0; i<p->pS
30770 72 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a rc->nSrc; i++){.
30780 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 struct Src
30790 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d List_item *pItem
307a0 20 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 = &p->pSrc->a[i
307b0 5d 3b 0a 20 20 20 20 20 20 53 74 72 41 63 63 75 ];. StrAccu
307c0 6d 20 78 3b 0a 20 20 20 20 20 20 63 68 61 72 20 m x;. char
307d0 7a 4c 69 6e 65 5b 31 30 30 5d 3b 0a 20 20 20 20 zLine[100];.
307e0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 sqlite3StrAccu
307f0 6d 49 6e 69 74 28 26 78 2c 20 7a 4c 69 6e 65 2c mInit(&x, zLine,
30800 20 73 69 7a 65 6f 66 28 7a 4c 69 6e 65 29 2c 20 sizeof(zLine),
30810 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 0);. sqlite
30820 33 58 50 72 69 6e 74 66 28 26 78 2c 20 30 2c 20 3XPrintf(&x, 0,
30830 22 7b 25 64 2c 2a 7d 22 2c 20 70 49 74 65 6d 2d "{%d,*}", pItem-
30840 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 >iCursor);.
30850 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74 if( pItem->zDat
30860 61 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 abase ){.
30870 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 sqlite3XPrintf(
30880 26 78 2c 20 30 2c 20 22 20 25 73 2e 25 73 22 2c &x, 0, " %s.%s",
30890 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 pItem->zDatabas
308a0 65 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 e, pItem->zName)
308b0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 ;. }else if
308c0 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 ( pItem->zName )
308d0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 {. sqlite
308e0 33 58 50 72 69 6e 74 66 28 26 78 2c 20 30 2c 20 3XPrintf(&x, 0,
308f0 22 20 25 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 4e " %s", pItem->zN
30900 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ame);. }.
30910 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 if( pItem->p
30920 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 Tab ){. s
30930 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 26 78 qlite3XPrintf(&x
30940 2c 20 30 2c 20 22 20 74 61 62 6e 61 6d 65 3d 25 , 0, " tabname=%
30950 51 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d Q", pItem->pTab-
30960 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d >zName);. }
30970 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d . if( pItem
30980 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 ->zAlias ){.
30990 20 20 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e sqlite3XPrin
309a0 74 66 28 26 78 2c 20 30 2c 20 22 20 28 41 53 20 tf(&x, 0, " (AS
309b0 25 73 29 22 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c %s)", pItem->zAl
309c0 69 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 ias);. }.
309d0 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 6a if( pItem->j
309e0 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 ointype & JT_LEF
309f0 54 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c T ){. sql
30a00 69 74 65 33 58 50 72 69 6e 74 66 28 26 78 2c 20 ite3XPrintf(&x,
30a10 30 2c 20 22 20 4c 45 46 54 2d 4a 4f 49 4e 22 29 0, " LEFT-JOIN")
30a20 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
30a30 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 sqlite3StrAccumF
30a40 69 6e 69 73 68 28 26 78 29 3b 0a 20 20 20 20 20 inish(&x);.
30a50 20 73 71 6c 69 74 65 33 54 72 65 65 56 69 65 77 sqlite3TreeView
30a60 49 74 65 6d 28 70 56 69 65 77 2c 20 7a 4c 69 6e Item(pView, zLin
30a70 65 2c 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 e, i<p->pSrc->nS
30a80 72 63 2d 31 29 3b 20 0a 20 20 20 20 20 20 69 66 rc-1); . if
30a90 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 ( pItem->pSelect
30aa0 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 ){. sqli
30ab0 74 65 33 54 72 65 65 56 69 65 77 53 65 6c 65 63 te3TreeViewSelec
30ac0 74 28 70 56 69 65 77 2c 20 70 49 74 65 6d 2d 3e t(pView, pItem->
30ad0 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 pSelect, 0);.
30ae0 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 }. sqlit
30af0 65 33 54 72 65 65 56 69 65 77 50 6f 70 28 70 56 e3TreeViewPop(pV
30b00 69 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 iew);. }.
30b10 73 71 6c 69 74 65 33 54 72 65 65 56 69 65 77 50 sqlite3TreeViewP
30b20 6f 70 28 70 56 69 65 77 29 3b 0a 20 20 7d 0a 20 op(pView);. }.
30b30 20 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29 if( p->pWhere )
30b40 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 72 65 {. sqlite3Tre
30b50 65 56 69 65 77 49 74 65 6d 28 70 56 69 65 77 2c eViewItem(pView,
30b60 20 22 57 48 45 52 45 22 2c 20 28 6e 2d 2d 29 3e "WHERE", (n--)>
30b70 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 0);. sqlite3T
30b80 72 65 65 56 69 65 77 45 78 70 72 28 70 56 69 65 reeViewExpr(pVie
30b90 77 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 30 29 w, p->pWhere, 0)
30ba0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 72 65 ;. sqlite3Tre
30bb0 65 56 69 65 77 50 6f 70 28 70 56 69 65 77 29 3b eViewPop(pView);
30bc0 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 . }. if( p->pG
30bd0 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 73 71 roupBy ){. sq
30be0 6c 69 74 65 33 54 72 65 65 56 69 65 77 45 78 70 lite3TreeViewExp
30bf0 72 4c 69 73 74 28 70 56 69 65 77 2c 20 70 2d 3e rList(pView, p->
30c00 70 47 72 6f 75 70 42 79 2c 20 28 6e 2d 2d 29 3e pGroupBy, (n--)>
30c10 30 2c 20 22 47 52 4f 55 50 42 59 22 29 3b 0a 20 0, "GROUPBY");.
30c20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 48 61 76 }. if( p->pHav
30c30 69 6e 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 ing ){. sqlit
30c40 65 33 54 72 65 65 56 69 65 77 49 74 65 6d 28 70 e3TreeViewItem(p
30c50 56 69 65 77 2c 20 22 48 41 56 49 4e 47 22 2c 20 View, "HAVING",
30c60 28 6e 2d 2d 29 3e 30 29 3b 0a 20 20 20 20 73 71 (n--)>0);. sq
30c70 6c 69 74 65 33 54 72 65 65 56 69 65 77 45 78 70 lite3TreeViewExp
30c80 72 28 70 56 69 65 77 2c 20 70 2d 3e 70 48 61 76 r(pView, p->pHav
30c90 69 6e 67 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c ing, 0);. sql
30ca0 69 74 65 33 54 72 65 65 56 69 65 77 50 6f 70 28 ite3TreeViewPop(
30cb0 70 56 69 65 77 29 3b 0a 20 20 7d 0a 20 20 69 66 pView);. }. if
30cc0 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b ( p->pOrderBy ){
30cd0 0a 20 20 20 20 73 71 6c 69 74 65 33 54 72 65 65 . sqlite3Tree
30ce0 56 69 65 77 45 78 70 72 4c 69 73 74 28 70 56 69 ViewExprList(pVi
30cf0 65 77 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c ew, p->pOrderBy,
30d00 20 28 6e 2d 2d 29 3e 30 2c 20 22 4f 52 44 45 52 (n--)>0, "ORDER
30d10 42 59 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 BY");. }. if(
30d20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 p->pLimit ){.
30d30 20 73 71 6c 69 74 65 33 54 72 65 65 56 69 65 77 sqlite3TreeView
30d40 49 74 65 6d 28 70 56 69 65 77 2c 20 22 4c 49 4d Item(pView, "LIM
30d50 49 54 22 2c 20 28 6e 2d 2d 29 3e 30 29 3b 0a 20 IT", (n--)>0);.
30d60 20 20 20 73 71 6c 69 74 65 33 54 72 65 65 56 69 sqlite3TreeVi
30d70 65 77 45 78 70 72 28 70 56 69 65 77 2c 20 70 2d ewExpr(pView, p-
30d80 3e 70 4c 69 6d 69 74 2c 20 30 29 3b 0a 20 20 20 >pLimit, 0);.
30d90 20 73 71 6c 69 74 65 33 54 72 65 65 56 69 65 77 sqlite3TreeView
30da0 50 6f 70 28 70 56 69 65 77 29 3b 0a 20 20 7d 0a Pop(pView);. }.
30db0 20 20 69 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 if( p->pOffset
30dc0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 54 ){. sqlite3T
30dd0 72 65 65 56 69 65 77 49 74 65 6d 28 70 56 69 65 reeViewItem(pVie
30de0 77 2c 20 22 4f 46 46 53 45 54 22 2c 20 28 6e 2d w, "OFFSET", (n-
30df0 2d 29 3e 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 -)>0);. sqlit
30e00 65 33 54 72 65 65 56 69 65 77 45 78 70 72 28 70 e3TreeViewExpr(p
30e10 56 69 65 77 2c 20 70 2d 3e 70 4f 66 66 73 65 74 View, p->pOffset
30e20 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
30e30 33 54 72 65 65 56 69 65 77 50 6f 70 28 70 56 69 3TreeViewPop(pVi
30e40 65 77 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 ew);. }. if( p
30e50 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20 ->pPrior ){.
30e60 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 70 20 const char *zOp
30e70 3d 20 22 55 4e 49 4f 4e 22 3b 0a 20 20 20 20 73 = "UNION";. s
30e80 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a witch( p->op ){.
30e90 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c case TK_AL
30ea0 4c 3a 20 20 20 20 20 20 20 20 20 7a 4f 70 20 3d L: zOp =
30eb0 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20 20 62 "UNION ALL"; b
30ec0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 reak;. case
30ed0 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20 20 TK_INTERSECT:
30ee0 20 7a 4f 70 20 3d 20 22 49 4e 54 45 52 53 45 43 zOp = "INTERSEC
30ef0 54 22 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 T"; break;.
30f00 20 20 63 61 73 65 20 54 4b 5f 45 58 43 45 50 54 case TK_EXCEPT
30f10 3a 20 20 20 20 20 20 7a 4f 70 20 3d 20 22 45 58 : zOp = "EX
30f20 43 45 50 54 22 3b 20 20 20 20 20 62 72 65 61 6b CEPT"; break
30f30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 ;. }. sqli
30f40 74 65 33 54 72 65 65 56 69 65 77 49 74 65 6d 28 te3TreeViewItem(
30f50 70 56 69 65 77 2c 20 7a 4f 70 2c 20 28 6e 2d 2d pView, zOp, (n--
30f60 29 3e 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 )>0);. sqlite
30f70 33 54 72 65 65 56 69 65 77 53 65 6c 65 63 74 28 3TreeViewSelect(
30f80 70 56 69 65 77 2c 20 70 2d 3e 70 50 72 69 6f 72 pView, p->pPrior
30f90 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 , 0);. sqlite
30fa0 33 54 72 65 65 56 69 65 77 50 6f 70 28 70 56 69 3TreeViewPop(pVi
30fb0 65 77 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 ew);. }. sqlit
30fc0 65 33 54 72 65 65 56 69 65 77 50 6f 70 28 70 56 e3TreeViewPop(pV
30fd0 69 65 77 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f iew);.}.#endif /
30fe0 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 2a * SQLITE_DEBUG *
30ff0 2f 0a /.