expr.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817
  1. /*
  2. ** 2001 September 15
  3. **
  4. ** The author disclaims copyright to this source code. In place of
  5. ** a legal notice, here is a blessing:
  6. **
  7. ** May you do good and not evil.
  8. ** May you find forgiveness for yourself and forgive others.
  9. ** May you share freely, never taking more than you give.
  10. **
  11. *************************************************************************
  12. ** This file contains routines used for analyzing expressions and
  13. ** for generating VDBE code that evaluates expressions in SQLite.
  14. **
  15. ** $Id: expr.c,v 1.320 2007/12/14 15:12:21 drh Exp $
  16. */
  17. #include "sqliteInt.h"
  18. #include <ctype.h>
  19. /*
  20. ** Return the 'affinity' of the expression pExpr if any.
  21. **
  22. ** If pExpr is a column, a reference to a column via an 'AS' alias,
  23. ** or a sub-select with a column as the return value, then the
  24. ** affinity of that column is returned. Otherwise, 0x00 is returned,
  25. ** indicating no affinity for the expression.
  26. **
  27. ** i.e. the WHERE clause expresssions in the following statements all
  28. ** have an affinity:
  29. **
  30. ** CREATE TABLE t1(a);
  31. ** SELECT * FROM t1 WHERE a;
  32. ** SELECT a AS b FROM t1 WHERE b;
  33. ** SELECT * FROM t1 WHERE (select a from t1);
  34. */
  35. char sqlite3ExprAffinity(Expr *pExpr){
  36. int op = pExpr->op;
  37. if( op==TK_SELECT ){
  38. return sqlite3ExprAffinity(pExpr->pSelect->pEList->a[0].pExpr);
  39. }
  40. #ifndef SQLITE_OMIT_CAST
  41. if( op==TK_CAST ){
  42. return sqlite3AffinityType(&pExpr->token);
  43. }
  44. #endif
  45. return pExpr->affinity;
  46. }
  47. /*
  48. ** Set the collating sequence for expression pExpr to be the collating
  49. ** sequence named by pToken. Return a pointer to the revised expression.
  50. ** The collating sequence is marked as "explicit" using the EP_ExpCollate
  51. ** flag. An explicit collating sequence will override implicit
  52. ** collating sequences.
  53. */
  54. Expr *sqlite3ExprSetColl(Parse *pParse, Expr *pExpr, Token *pName){
  55. char *zColl = 0; /* Dequoted name of collation sequence */
  56. CollSeq *pColl;
  57. zColl = sqlite3NameFromToken(pParse->db, pName);
  58. if( pExpr && zColl ){
  59. pColl = sqlite3LocateCollSeq(pParse, zColl, -1);
  60. if( pColl ){
  61. pExpr->pColl = pColl;
  62. pExpr->flags |= EP_ExpCollate;
  63. }
  64. }
  65. sqlite3_free(zColl);
  66. return pExpr;
  67. }
  68. /*
  69. ** Return the default collation sequence for the expression pExpr. If
  70. ** there is no default collation type, return 0.
  71. */
  72. CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
  73. CollSeq *pColl = 0;
  74. if( pExpr ){
  75. int op;
  76. pColl = pExpr->pColl;
  77. op = pExpr->op;
  78. if( (op==TK_CAST || op==TK_UPLUS) && !pColl ){
  79. return sqlite3ExprCollSeq(pParse, pExpr->pLeft);
  80. }
  81. }
  82. if( sqlite3CheckCollSeq(pParse, pColl) ){
  83. pColl = 0;
  84. }
  85. return pColl;
  86. }
  87. /*
  88. ** pExpr is an operand of a comparison operator. aff2 is the
  89. ** type affinity of the other operand. This routine returns the
  90. ** type affinity that should be used for the comparison operator.
  91. */
  92. char sqlite3CompareAffinity(Expr *pExpr, char aff2){
  93. char aff1 = sqlite3ExprAffinity(pExpr);
  94. if( aff1 && aff2 ){
  95. /* Both sides of the comparison are columns. If one has numeric
  96. ** affinity, use that. Otherwise use no affinity.
  97. */
  98. if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
  99. return SQLITE_AFF_NUMERIC;
  100. }else{
  101. return SQLITE_AFF_NONE;
  102. }
  103. }else if( !aff1 && !aff2 ){
  104. /* Neither side of the comparison is a column. Compare the
  105. ** results directly.
  106. */
  107. return SQLITE_AFF_NONE;
  108. }else{
  109. /* One side is a column, the other is not. Use the columns affinity. */
  110. assert( aff1==0 || aff2==0 );
  111. return (aff1 + aff2);
  112. }
  113. }
  114. /*
  115. ** pExpr is a comparison operator. Return the type affinity that should
  116. ** be applied to both operands prior to doing the comparison.
  117. */
  118. static char comparisonAffinity(Expr *pExpr){
  119. char aff;
  120. assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
  121. pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
  122. pExpr->op==TK_NE );
  123. assert( pExpr->pLeft );
  124. aff = sqlite3ExprAffinity(pExpr->pLeft);
  125. if( pExpr->pRight ){
  126. aff = sqlite3CompareAffinity(pExpr->pRight, aff);
  127. }
  128. else if( pExpr->pSelect ){
  129. aff = sqlite3CompareAffinity(pExpr->pSelect->pEList->a[0].pExpr, aff);
  130. }
  131. else if( !aff ){
  132. aff = SQLITE_AFF_NONE;
  133. }
  134. return aff;
  135. }
  136. /*
  137. ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
  138. ** idx_affinity is the affinity of an indexed column. Return true
  139. ** if the index with affinity idx_affinity may be used to implement
  140. ** the comparison in pExpr.
  141. */
  142. int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
  143. char aff = comparisonAffinity(pExpr);
  144. switch( aff ){
  145. case SQLITE_AFF_NONE:
  146. return 1;
  147. case SQLITE_AFF_TEXT:
  148. return idx_affinity==SQLITE_AFF_TEXT;
  149. default:
  150. return sqlite3IsNumericAffinity(idx_affinity);
  151. }
  152. }
  153. /*
  154. ** Return the P1 value that should be used for a binary comparison
  155. ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
  156. ** If jumpIfNull is true, then set the low byte of the returned
  157. ** P1 value to tell the opcode to jump if either expression
  158. ** evaluates to NULL.
  159. */
  160. static int binaryCompareP1(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
  161. char aff = sqlite3ExprAffinity(pExpr2);
  162. return ((int)sqlite3CompareAffinity(pExpr1, aff))+(jumpIfNull?0x100:0);
  163. }
  164. /*
  165. ** Return a pointer to the collation sequence that should be used by
  166. ** a binary comparison operator comparing pLeft and pRight.
  167. **
  168. ** If the left hand expression has a collating sequence type, then it is
  169. ** used. Otherwise the collation sequence for the right hand expression
  170. ** is used, or the default (BINARY) if neither expression has a collating
  171. ** type.
  172. **
  173. ** Argument pRight (but not pLeft) may be a null pointer. In this case,
  174. ** it is not considered.
  175. */
  176. CollSeq *sqlite3BinaryCompareCollSeq(
  177. Parse *pParse,
  178. Expr *pLeft,
  179. Expr *pRight
  180. ){
  181. CollSeq *pColl;
  182. assert( pLeft );
  183. if( pLeft->flags & EP_ExpCollate ){
  184. assert( pLeft->pColl );
  185. pColl = pLeft->pColl;
  186. }else if( pRight && pRight->flags & EP_ExpCollate ){
  187. assert( pRight->pColl );
  188. pColl = pRight->pColl;
  189. }else{
  190. pColl = sqlite3ExprCollSeq(pParse, pLeft);
  191. if( !pColl ){
  192. pColl = sqlite3ExprCollSeq(pParse, pRight);
  193. }
  194. }
  195. return pColl;
  196. }
  197. /*
  198. ** Generate code for a comparison operator.
  199. */
  200. static int codeCompare(
  201. Parse *pParse, /* The parsing (and code generating) context */
  202. Expr *pLeft, /* The left operand */
  203. Expr *pRight, /* The right operand */
  204. int opcode, /* The comparison opcode */
  205. int dest, /* Jump here if true. */
  206. int jumpIfNull /* If true, jump if either operand is NULL */
  207. ){
  208. int p1 = binaryCompareP1(pLeft, pRight, jumpIfNull);
  209. CollSeq *p3 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
  210. return sqlite3VdbeOp3(pParse->pVdbe, opcode, p1, dest, (void*)p3, P3_COLLSEQ);
  211. }
  212. /*
  213. ** Construct a new expression node and return a pointer to it. Memory
  214. ** for this node is obtained from sqlite3_malloc(). The calling function
  215. ** is responsible for making sure the node eventually gets freed.
  216. */
  217. Expr *sqlite3Expr(
  218. sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */
  219. int op, /* Expression opcode */
  220. Expr *pLeft, /* Left operand */
  221. Expr *pRight, /* Right operand */
  222. const Token *pToken /* Argument token */
  223. ){
  224. Expr *pNew;
  225. pNew = sqlite3DbMallocZero(db, sizeof(Expr));
  226. if( pNew==0 ){
  227. /* When malloc fails, delete pLeft and pRight. Expressions passed to
  228. ** this function must always be allocated with sqlite3Expr() for this
  229. ** reason.
  230. */
  231. sqlite3ExprDelete(pLeft);
  232. sqlite3ExprDelete(pRight);
  233. return 0;
  234. }
  235. pNew->op = op;
  236. pNew->pLeft = pLeft;
  237. pNew->pRight = pRight;
  238. pNew->iAgg = -1;
  239. if( pToken ){
  240. assert( pToken->dyn==0 );
  241. pNew->span = pNew->token = *pToken;
  242. }else if( pLeft ){
  243. if( pRight ){
  244. sqlite3ExprSpan(pNew, &pLeft->span, &pRight->span);
  245. if( pRight->flags & EP_ExpCollate ){
  246. pNew->flags |= EP_ExpCollate;
  247. pNew->pColl = pRight->pColl;
  248. }
  249. }
  250. if( pLeft->flags & EP_ExpCollate ){
  251. pNew->flags |= EP_ExpCollate;
  252. pNew->pColl = pLeft->pColl;
  253. }
  254. }
  255. sqlite3ExprSetHeight(pNew);
  256. return pNew;
  257. }
  258. /*
  259. ** Works like sqlite3Expr() except that it takes an extra Parse*
  260. ** argument and notifies the associated connection object if malloc fails.
  261. */
  262. Expr *sqlite3PExpr(
  263. Parse *pParse, /* Parsing context */
  264. int op, /* Expression opcode */
  265. Expr *pLeft, /* Left operand */
  266. Expr *pRight, /* Right operand */
  267. const Token *pToken /* Argument token */
  268. ){
  269. return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken);
  270. }
  271. /*
  272. ** When doing a nested parse, you can include terms in an expression
  273. ** that look like this: #0 #1 #2 ... These terms refer to elements
  274. ** on the stack. "#0" means the top of the stack.
  275. ** "#1" means the next down on the stack. And so forth.
  276. **
  277. ** This routine is called by the parser to deal with on of those terms.
  278. ** It immediately generates code to store the value in a memory location.
  279. ** The returns an expression that will code to extract the value from
  280. ** that memory location as needed.
  281. */
  282. Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){
  283. Vdbe *v = pParse->pVdbe;
  284. Expr *p;
  285. int depth;
  286. if( pParse->nested==0 ){
  287. sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken);
  288. return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
  289. }
  290. if( v==0 ) return 0;
  291. p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken);
  292. if( p==0 ){
  293. return 0; /* Malloc failed */
  294. }
  295. depth = atoi((char*)&pToken->z[1]);
  296. p->iTable = pParse->nMem++;
  297. sqlite3VdbeAddOp(v, OP_Dup, depth, 0);
  298. sqlite3VdbeAddOp(v, OP_MemStore, p->iTable, 1);
  299. return p;
  300. }
  301. /*
  302. ** Join two expressions using an AND operator. If either expression is
  303. ** NULL, then just return the other expression.
  304. */
  305. Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
  306. if( pLeft==0 ){
  307. return pRight;
  308. }else if( pRight==0 ){
  309. return pLeft;
  310. }else{
  311. return sqlite3Expr(db, TK_AND, pLeft, pRight, 0);
  312. }
  313. }
  314. /*
  315. ** Set the Expr.span field of the given expression to span all
  316. ** text between the two given tokens.
  317. */
  318. void sqlite3ExprSpan(Expr *pExpr, Token *pLeft, Token *pRight){
  319. assert( pRight!=0 );
  320. assert( pLeft!=0 );
  321. if( pExpr && pRight->z && pLeft->z ){
  322. assert( pLeft->dyn==0 || pLeft->z[pLeft->n]==0 );
  323. if( pLeft->dyn==0 && pRight->dyn==0 ){
  324. pExpr->span.z = pLeft->z;
  325. pExpr->span.n = pRight->n + (pRight->z - pLeft->z);
  326. }else{
  327. pExpr->span.z = 0;
  328. }
  329. }
  330. }
  331. /*
  332. ** Construct a new expression node for a function with multiple
  333. ** arguments.
  334. */
  335. Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
  336. Expr *pNew;
  337. assert( pToken );
  338. pNew = sqlite3DbMallocZero(pParse->db, sizeof(Expr) );
  339. if( pNew==0 ){
  340. sqlite3ExprListDelete(pList); /* Avoid leaking memory when malloc fails */
  341. return 0;
  342. }
  343. pNew->op = TK_FUNCTION;
  344. pNew->pList = pList;
  345. assert( pToken->dyn==0 );
  346. pNew->token = *pToken;
  347. pNew->span = pNew->token;
  348. sqlite3ExprSetHeight(pNew);
  349. return pNew;
  350. }
  351. /*
  352. ** Assign a variable number to an expression that encodes a wildcard
  353. ** in the original SQL statement.
  354. **
  355. ** Wildcards consisting of a single "?" are assigned the next sequential
  356. ** variable number.
  357. **
  358. ** Wildcards of the form "?nnn" are assigned the number "nnn". We make
  359. ** sure "nnn" is not too be to avoid a denial of service attack when
  360. ** the SQL statement comes from an external source.
  361. **
  362. ** Wildcards of the form ":aaa" or "$aaa" are assigned the same number
  363. ** as the previous instance of the same wildcard. Or if this is the first
  364. ** instance of the wildcard, the next sequenial variable number is
  365. ** assigned.
  366. */
  367. void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
  368. Token *pToken;
  369. sqlite3 *db = pParse->db;
  370. if( pExpr==0 ) return;
  371. pToken = &pExpr->token;
  372. assert( pToken->n>=1 );
  373. assert( pToken->z!=0 );
  374. assert( pToken->z[0]!=0 );
  375. if( pToken->n==1 ){
  376. /* Wildcard of the form "?". Assign the next variable number */
  377. pExpr->iTable = ++pParse->nVar;
  378. }else if( pToken->z[0]=='?' ){
  379. /* Wildcard of the form "?nnn". Convert "nnn" to an integer and
  380. ** use it as the variable number */
  381. int i;
  382. pExpr->iTable = i = atoi((char*)&pToken->z[1]);
  383. if( i<1 || i>SQLITE_MAX_VARIABLE_NUMBER ){
  384. sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
  385. SQLITE_MAX_VARIABLE_NUMBER);
  386. }
  387. if( i>pParse->nVar ){
  388. pParse->nVar = i;
  389. }
  390. }else{
  391. /* Wildcards of the form ":aaa" or "$aaa". Reuse the same variable
  392. ** number as the prior appearance of the same name, or if the name
  393. ** has never appeared before, reuse the same variable number
  394. */
  395. int i, n;
  396. n = pToken->n;
  397. for(i=0; i<pParse->nVarExpr; i++){
  398. Expr *pE;
  399. if( (pE = pParse->apVarExpr[i])!=0
  400. && pE->token.n==n
  401. && memcmp(pE->token.z, pToken->z, n)==0 ){
  402. pExpr->iTable = pE->iTable;
  403. break;
  404. }
  405. }
  406. if( i>=pParse->nVarExpr ){
  407. pExpr->iTable = ++pParse->nVar;
  408. if( pParse->nVarExpr>=pParse->nVarExprAlloc-1 ){
  409. pParse->nVarExprAlloc += pParse->nVarExprAlloc + 10;
  410. pParse->apVarExpr =
  411. sqlite3DbReallocOrFree(
  412. db,
  413. pParse->apVarExpr,
  414. pParse->nVarExprAlloc*sizeof(pParse->apVarExpr[0])
  415. );
  416. }
  417. if( !db->mallocFailed ){
  418. assert( pParse->apVarExpr!=0 );
  419. pParse->apVarExpr[pParse->nVarExpr++] = pExpr;
  420. }
  421. }
  422. }
  423. if( !pParse->nErr && pParse->nVar>SQLITE_MAX_VARIABLE_NUMBER ){
  424. sqlite3ErrorMsg(pParse, "too many SQL variables");
  425. }
  426. }
  427. /*
  428. ** Recursively delete an expression tree.
  429. */
  430. void sqlite3ExprDelete(Expr *p){
  431. if( p==0 ) return;
  432. if( p->span.dyn ) sqlite3_free((char*)p->span.z);
  433. if( p->token.dyn ) sqlite3_free((char*)p->token.z);
  434. sqlite3ExprDelete(p->pLeft);
  435. sqlite3ExprDelete(p->pRight);
  436. sqlite3ExprListDelete(p->pList);
  437. sqlite3SelectDelete(p->pSelect);
  438. sqlite3_free(p);
  439. }
  440. /*
  441. ** The Expr.token field might be a string literal that is quoted.
  442. ** If so, remove the quotation marks.
  443. */
  444. void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
  445. if( ExprHasAnyProperty(p, EP_Dequoted) ){
  446. return;
  447. }
  448. ExprSetProperty(p, EP_Dequoted);
  449. if( p->token.dyn==0 ){
  450. sqlite3TokenCopy(db, &p->token, &p->token);
  451. }
  452. sqlite3Dequote((char*)p->token.z);
  453. }
  454. /*
  455. ** The following group of routines make deep copies of expressions,
  456. ** expression lists, ID lists, and select statements. The copies can
  457. ** be deleted (by being passed to their respective ...Delete() routines)
  458. ** without effecting the originals.
  459. **
  460. ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
  461. ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
  462. ** by subsequent calls to sqlite*ListAppend() routines.
  463. **
  464. ** Any tables that the SrcList might point to are not duplicated.
  465. */
  466. Expr *sqlite3ExprDup(sqlite3 *db, Expr *p){
  467. Expr *pNew;
  468. if( p==0 ) return 0;
  469. pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
  470. if( pNew==0 ) return 0;
  471. memcpy(pNew, p, sizeof(*pNew));
  472. if( p->token.z!=0 ){
  473. pNew->token.z = (u8*)sqlite3DbStrNDup(db, (char*)p->token.z, p->token.n);
  474. pNew->token.dyn = 1;
  475. }else{
  476. assert( pNew->token.z==0 );
  477. }
  478. pNew->span.z = 0;
  479. pNew->pLeft = sqlite3ExprDup(db, p->pLeft);
  480. pNew->pRight = sqlite3ExprDup(db, p->pRight);
  481. pNew->pList = sqlite3ExprListDup(db, p->pList);
  482. pNew->pSelect = sqlite3SelectDup(db, p->pSelect);
  483. return pNew;
  484. }
  485. void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
  486. if( pTo->dyn ) sqlite3_free((char*)pTo->z);
  487. if( pFrom->z ){
  488. pTo->n = pFrom->n;
  489. pTo->z = (u8*)sqlite3DbStrNDup(db, (char*)pFrom->z, pFrom->n);
  490. pTo->dyn = 1;
  491. }else{
  492. pTo->z = 0;
  493. }
  494. }
  495. ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p){
  496. ExprList *pNew;
  497. struct ExprList_item *pItem, *pOldItem;
  498. int i;
  499. if( p==0 ) return 0;
  500. pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
  501. if( pNew==0 ) return 0;
  502. pNew->iECursor = 0;
  503. pNew->nExpr = pNew->nAlloc = p->nExpr;
  504. pNew->a = pItem = sqlite3DbMallocRaw(db, p->nExpr*sizeof(p->a[0]) );
  505. if( pItem==0 ){
  506. sqlite3_free(pNew);
  507. return 0;
  508. }
  509. pOldItem = p->a;
  510. for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
  511. Expr *pNewExpr, *pOldExpr;
  512. pItem->pExpr = pNewExpr = sqlite3ExprDup(db, pOldExpr = pOldItem->pExpr);
  513. if( pOldExpr->span.z!=0 && pNewExpr ){
  514. /* Always make a copy of the span for top-level expressions in the
  515. ** expression list. The logic in SELECT processing that determines
  516. ** the names of columns in the result set needs this information */
  517. sqlite3TokenCopy(db, &pNewExpr->span, &pOldExpr->span);
  518. }
  519. assert( pNewExpr==0 || pNewExpr->span.z!=0
  520. || pOldExpr->span.z==0
  521. || db->mallocFailed );
  522. pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
  523. pItem->sortOrder = pOldItem->sortOrder;
  524. pItem->isAgg = pOldItem->isAgg;
  525. pItem->done = 0;
  526. }
  527. return pNew;
  528. }
  529. /*
  530. ** If cursors, triggers, views and subqueries are all omitted from
  531. ** the build, then none of the following routines, except for
  532. ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
  533. ** called with a NULL argument.
  534. */
  535. #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
  536. || !defined(SQLITE_OMIT_SUBQUERY)
  537. SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p){
  538. SrcList *pNew;
  539. int i;
  540. int nByte;
  541. if( p==0 ) return 0;
  542. nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
  543. pNew = sqlite3DbMallocRaw(db, nByte );
  544. if( pNew==0 ) return 0;
  545. pNew->nSrc = pNew->nAlloc = p->nSrc;
  546. for(i=0; i<p->nSrc; i++){
  547. struct SrcList_item *pNewItem = &pNew->a[i];
  548. struct SrcList_item *pOldItem = &p->a[i];
  549. Table *pTab;
  550. pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
  551. pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
  552. pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
  553. pNewItem->jointype = pOldItem->jointype;
  554. pNewItem->iCursor = pOldItem->iCursor;
  555. pNewItem->isPopulated = pOldItem->isPopulated;
  556. pTab = pNewItem->pTab = pOldItem->pTab;
  557. if( pTab ){
  558. pTab->nRef++;
  559. }
  560. pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect);
  561. pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn);
  562. pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
  563. pNewItem->colUsed = pOldItem->colUsed;
  564. }
  565. return pNew;
  566. }
  567. IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
  568. IdList *pNew;
  569. int i;
  570. if( p==0 ) return 0;
  571. pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
  572. if( pNew==0 ) return 0;
  573. pNew->nId = pNew->nAlloc = p->nId;
  574. pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
  575. if( pNew->a==0 ){
  576. sqlite3_free(pNew);
  577. return 0;
  578. }
  579. for(i=0; i<p->nId; i++){
  580. struct IdList_item *pNewItem = &pNew->a[i];
  581. struct IdList_item *pOldItem = &p->a[i];
  582. pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
  583. pNewItem->idx = pOldItem->idx;
  584. }
  585. return pNew;
  586. }
  587. Select *sqlite3SelectDup(sqlite3 *db, Select *p){
  588. Select *pNew;
  589. if( p==0 ) return 0;
  590. pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
  591. if( pNew==0 ) return 0;
  592. pNew->isDistinct = p->isDistinct;
  593. pNew->pEList = sqlite3ExprListDup(db, p->pEList);
  594. pNew->pSrc = sqlite3SrcListDup(db, p->pSrc);
  595. pNew->pWhere = sqlite3ExprDup(db, p->pWhere);
  596. pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy);
  597. pNew->pHaving = sqlite3ExprDup(db, p->pHaving);
  598. pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy);
  599. pNew->op = p->op;
  600. pNew->pPrior = sqlite3SelectDup(db, p->pPrior);
  601. pNew->pLimit = sqlite3ExprDup(db, p->pLimit);
  602. pNew->pOffset = sqlite3ExprDup(db, p->pOffset);
  603. pNew->iLimit = -1;
  604. pNew->iOffset = -1;
  605. pNew->isResolved = p->isResolved;
  606. pNew->isAgg = p->isAgg;
  607. pNew->usesEphm = 0;
  608. pNew->disallowOrderBy = 0;
  609. pNew->pRightmost = 0;
  610. pNew->addrOpenEphm[0] = -1;
  611. pNew->addrOpenEphm[1] = -1;
  612. pNew->addrOpenEphm[2] = -1;
  613. return pNew;
  614. }
  615. #else
  616. Select *sqlite3SelectDup(sqlite3 *db, Select *p){
  617. assert( p==0 );
  618. return 0;
  619. }
  620. #endif
  621. /*
  622. ** Add a new element to the end of an expression list. If pList is
  623. ** initially NULL, then create a new expression list.
  624. */
  625. ExprList *sqlite3ExprListAppend(
  626. Parse *pParse, /* Parsing context */
  627. ExprList *pList, /* List to which to append. Might be NULL */
  628. Expr *pExpr, /* Expression to be appended */
  629. Token *pName /* AS keyword for the expression */
  630. ){
  631. sqlite3 *db = pParse->db;
  632. if( pList==0 ){
  633. pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
  634. if( pList==0 ){
  635. goto no_mem;
  636. }
  637. assert( pList->nAlloc==0 );
  638. }
  639. if( pList->nAlloc<=pList->nExpr ){
  640. struct ExprList_item *a;
  641. int n = pList->nAlloc*2 + 4;
  642. a = sqlite3DbRealloc(db, pList->a, n*sizeof(pList->a[0]));
  643. if( a==0 ){
  644. goto no_mem;
  645. }
  646. pList->a = a;
  647. pList->nAlloc = n;
  648. }
  649. assert( pList->a!=0 );
  650. if( pExpr || pName ){
  651. struct ExprList_item *pItem = &pList->a[pList->nExpr++];
  652. memset(pItem, 0, sizeof(*pItem));
  653. pItem->zName = sqlite3NameFromToken(db, pName);
  654. pItem->pExpr = pExpr;
  655. }
  656. return pList;
  657. no_mem:
  658. /* Avoid leaking memory if malloc has failed. */
  659. sqlite3ExprDelete(pExpr);
  660. sqlite3ExprListDelete(pList);
  661. return 0;
  662. }
  663. /*
  664. ** If the expression list pEList contains more than iLimit elements,
  665. ** leave an error message in pParse.
  666. */
  667. void sqlite3ExprListCheckLength(
  668. Parse *pParse,
  669. ExprList *pEList,
  670. int iLimit,
  671. const char *zObject
  672. ){
  673. if( pEList && pEList->nExpr>iLimit ){
  674. sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
  675. }
  676. }
  677. #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
  678. /* The following three functions, heightOfExpr(), heightOfExprList()
  679. ** and heightOfSelect(), are used to determine the maximum height
  680. ** of any expression tree referenced by the structure passed as the
  681. ** first argument.
  682. **
  683. ** If this maximum height is greater than the current value pointed
  684. ** to by pnHeight, the second parameter, then set *pnHeight to that
  685. ** value.
  686. */
  687. static void heightOfExpr(Expr *p, int *pnHeight){
  688. if( p ){
  689. if( p->nHeight>*pnHeight ){
  690. *pnHeight = p->nHeight;
  691. }
  692. }
  693. }
  694. static void heightOfExprList(ExprList *p, int *pnHeight){
  695. if( p ){
  696. int i;
  697. for(i=0; i<p->nExpr; i++){
  698. heightOfExpr(p->a[i].pExpr, pnHeight);
  699. }
  700. }
  701. }
  702. static void heightOfSelect(Select *p, int *pnHeight){
  703. if( p ){
  704. heightOfExpr(p->pWhere, pnHeight);
  705. heightOfExpr(p->pHaving, pnHeight);
  706. heightOfExpr(p->pLimit, pnHeight);
  707. heightOfExpr(p->pOffset, pnHeight);
  708. heightOfExprList(p->pEList, pnHeight);
  709. heightOfExprList(p->pGroupBy, pnHeight);
  710. heightOfExprList(p->pOrderBy, pnHeight);
  711. heightOfSelect(p->pPrior, pnHeight);
  712. }
  713. }
  714. /*
  715. ** Set the Expr.nHeight variable in the structure passed as an
  716. ** argument. An expression with no children, Expr.pList or
  717. ** Expr.pSelect member has a height of 1. Any other expression
  718. ** has a height equal to the maximum height of any other
  719. ** referenced Expr plus one.
  720. */
  721. void sqlite3ExprSetHeight(Expr *p){
  722. int nHeight = 0;
  723. heightOfExpr(p->pLeft, &nHeight);
  724. heightOfExpr(p->pRight, &nHeight);
  725. heightOfExprList(p->pList, &nHeight);
  726. heightOfSelect(p->pSelect, &nHeight);
  727. p->nHeight = nHeight + 1;
  728. }
  729. /*
  730. ** Return the maximum height of any expression tree referenced
  731. ** by the select statement passed as an argument.
  732. */
  733. int sqlite3SelectExprHeight(Select *p){
  734. int nHeight = 0;
  735. heightOfSelect(p, &nHeight);
  736. return nHeight;
  737. }
  738. #endif
  739. /*
  740. ** Delete an entire expression list.
  741. */
  742. void sqlite3ExprListDelete(ExprList *pList){
  743. int i;
  744. struct ExprList_item *pItem;
  745. if( pList==0 ) return;
  746. assert( pList->a!=0 || (pList->nExpr==0 && pList->nAlloc==0) );
  747. assert( pList->nExpr<=pList->nAlloc );
  748. for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
  749. sqlite3ExprDelete(pItem->pExpr);
  750. sqlite3_free(pItem->zName);
  751. }
  752. sqlite3_free(pList->a);
  753. sqlite3_free(pList);
  754. }
  755. /*
  756. ** Walk an expression tree. Call xFunc for each node visited.
  757. **
  758. ** The return value from xFunc determines whether the tree walk continues.
  759. ** 0 means continue walking the tree. 1 means do not walk children
  760. ** of the current node but continue with siblings. 2 means abandon
  761. ** the tree walk completely.
  762. **
  763. ** The return value from this routine is 1 to abandon the tree walk
  764. ** and 0 to continue.
  765. **
  766. ** NOTICE: This routine does *not* descend into subqueries.
  767. */
  768. static int walkExprList(ExprList *, int (*)(void *, Expr*), void *);
  769. static int walkExprTree(Expr *pExpr, int (*xFunc)(void*,Expr*), void *pArg){
  770. int rc;
  771. if( pExpr==0 ) return 0;
  772. rc = (*xFunc)(pArg, pExpr);
  773. if( rc==0 ){
  774. if( walkExprTree(pExpr->pLeft, xFunc, pArg) ) return 1;
  775. if( walkExprTree(pExpr->pRight, xFunc, pArg) ) return 1;
  776. if( walkExprList(pExpr->pList, xFunc, pArg) ) return 1;
  777. }
  778. return rc>1;
  779. }
  780. /*
  781. ** Call walkExprTree() for every expression in list p.
  782. */
  783. static int walkExprList(ExprList *p, int (*xFunc)(void *, Expr*), void *pArg){
  784. int i;
  785. struct ExprList_item *pItem;
  786. if( !p ) return 0;
  787. for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
  788. if( walkExprTree(pItem->pExpr, xFunc, pArg) ) return 1;
  789. }
  790. return 0;
  791. }
  792. /*
  793. ** Call walkExprTree() for every expression in Select p, not including
  794. ** expressions that are part of sub-selects in any FROM clause or the LIMIT
  795. ** or OFFSET expressions..
  796. */
  797. static int walkSelectExpr(Select *p, int (*xFunc)(void *, Expr*), void *pArg){
  798. walkExprList(p->pEList, xFunc, pArg);
  799. walkExprTree(p->pWhere, xFunc, pArg);
  800. walkExprList(p->pGroupBy, xFunc, pArg);
  801. walkExprTree(p->pHaving, xFunc, pArg);
  802. walkExprList(p->pOrderBy, xFunc, pArg);
  803. if( p->pPrior ){
  804. walkSelectExpr(p->pPrior, xFunc, pArg);
  805. }
  806. return 0;
  807. }
  808. /*
  809. ** This routine is designed as an xFunc for walkExprTree().
  810. **
  811. ** pArg is really a pointer to an integer. If we can tell by looking
  812. ** at pExpr that the expression that contains pExpr is not a constant
  813. ** expression, then set *pArg to 0 and return 2 to abandon the tree walk.
  814. ** If pExpr does does not disqualify the expression from being a constant
  815. ** then do nothing.
  816. **
  817. ** After walking the whole tree, if no nodes are found that disqualify
  818. ** the expression as constant, then we assume the whole expression
  819. ** is constant. See sqlite3ExprIsConstant() for additional information.
  820. */
  821. static int exprNodeIsConstant(void *pArg, Expr *pExpr){
  822. int *pN = (int*)pArg;
  823. /* If *pArg is 3 then any term of the expression that comes from
  824. ** the ON or USING clauses of a join disqualifies the expression
  825. ** from being considered constant. */
  826. if( (*pN)==3 && ExprHasAnyProperty(pExpr, EP_FromJoin) ){
  827. *pN = 0;
  828. return 2;
  829. }
  830. switch( pExpr->op ){
  831. /* Consider functions to be constant if all their arguments are constant
  832. ** and *pArg==2 */
  833. case TK_FUNCTION:
  834. if( (*pN)==2 ) return 0;
  835. /* Fall through */
  836. case TK_ID:
  837. case TK_COLUMN:
  838. case TK_DOT:
  839. case TK_AGG_FUNCTION:
  840. case TK_AGG_COLUMN:
  841. #ifndef SQLITE_OMIT_SUBQUERY
  842. case TK_SELECT:
  843. case TK_EXISTS:
  844. #endif
  845. *pN = 0;
  846. return 2;
  847. case TK_IN:
  848. if( pExpr->pSelect ){
  849. *pN = 0;
  850. return 2;
  851. }
  852. default:
  853. return 0;
  854. }
  855. }
  856. /*
  857. ** Walk an expression tree. Return 1 if the expression is constant
  858. ** and 0 if it involves variables or function calls.
  859. **
  860. ** For the purposes of this function, a double-quoted string (ex: "abc")
  861. ** is considered a variable but a single-quoted string (ex: 'abc') is
  862. ** a constant.
  863. */
  864. int sqlite3ExprIsConstant(Expr *p){
  865. int isConst = 1;
  866. walkExprTree(p, exprNodeIsConstant, &isConst);
  867. return isConst;
  868. }
  869. /*
  870. ** Walk an expression tree. Return 1 if the expression is constant
  871. ** that does no originate from the ON or USING clauses of a join.
  872. ** Return 0 if it involves variables or function calls or terms from
  873. ** an ON or USING clause.
  874. */
  875. int sqlite3ExprIsConstantNotJoin(Expr *p){
  876. int isConst = 3;
  877. walkExprTree(p, exprNodeIsConstant, &isConst);
  878. return isConst!=0;
  879. }
  880. /*
  881. ** Walk an expression tree. Return 1 if the expression is constant
  882. ** or a function call with constant arguments. Return and 0 if there
  883. ** are any variables.
  884. **
  885. ** For the purposes of this function, a double-quoted string (ex: "abc")
  886. ** is considered a variable but a single-quoted string (ex: 'abc') is
  887. ** a constant.
  888. */
  889. int sqlite3ExprIsConstantOrFunction(Expr *p){
  890. int isConst = 2;
  891. walkExprTree(p, exprNodeIsConstant, &isConst);
  892. return isConst!=0;
  893. }
  894. /*
  895. ** If the expression p codes a constant integer that is small enough
  896. ** to fit in a 32-bit integer, return 1 and put the value of the integer
  897. ** in *pValue. If the expression is not an integer or if it is too big
  898. ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
  899. */
  900. int sqlite3ExprIsInteger(Expr *p, int *pValue){
  901. switch( p->op ){
  902. case TK_INTEGER: {
  903. if( sqlite3GetInt32((char*)p->token.z, pValue) ){
  904. return 1;
  905. }
  906. break;
  907. }
  908. case TK_UPLUS: {
  909. return sqlite3ExprIsInteger(p->pLeft, pValue);
  910. }
  911. case TK_UMINUS: {
  912. int v;
  913. if( sqlite3ExprIsInteger(p->pLeft, &v) ){
  914. *pValue = -v;
  915. return 1;
  916. }
  917. break;
  918. }
  919. default: break;
  920. }
  921. return 0;
  922. }
  923. /*
  924. ** Return TRUE if the given string is a row-id column name.
  925. */
  926. int sqlite3IsRowid(const char *z){
  927. if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
  928. if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
  929. if( sqlite3StrICmp(z, "OID")==0 ) return 1;
  930. return 0;
  931. }
  932. /*
  933. ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
  934. ** that name in the set of source tables in pSrcList and make the pExpr
  935. ** expression node refer back to that source column. The following changes
  936. ** are made to pExpr:
  937. **
  938. ** pExpr->iDb Set the index in db->aDb[] of the database holding
  939. ** the table.
  940. ** pExpr->iTable Set to the cursor number for the table obtained
  941. ** from pSrcList.
  942. ** pExpr->iColumn Set to the column number within the table.
  943. ** pExpr->op Set to TK_COLUMN.
  944. ** pExpr->pLeft Any expression this points to is deleted
  945. ** pExpr->pRight Any expression this points to is deleted.
  946. **
  947. ** The pDbToken is the name of the database (the "X"). This value may be
  948. ** NULL meaning that name is of the form Y.Z or Z. Any available database
  949. ** can be used. The pTableToken is the name of the table (the "Y"). This
  950. ** value can be NULL if pDbToken is also NULL. If pTableToken is NULL it
  951. ** means that the form of the name is Z and that columns from any table
  952. ** can be used.
  953. **
  954. ** If the name cannot be resolved unambiguously, leave an error message
  955. ** in pParse and return non-zero. Return zero on success.
  956. */
  957. static int lookupName(
  958. Parse *pParse, /* The parsing context */
  959. Token *pDbToken, /* Name of the database containing table, or NULL */
  960. Token *pTableToken, /* Name of table containing column, or NULL */
  961. Token *pColumnToken, /* Name of the column. */
  962. NameContext *pNC, /* The name context used to resolve the name */
  963. Expr *pExpr /* Make this EXPR node point to the selected column */
  964. ){
  965. char *zDb = 0; /* Name of the database. The "X" in X.Y.Z */
  966. char *zTab = 0; /* Name of the table. The "Y" in X.Y.Z or Y.Z */
  967. char *zCol = 0; /* Name of the column. The "Z" */
  968. int i, j; /* Loop counters */
  969. int cnt = 0; /* Number of matching column names */
  970. int cntTab = 0; /* Number of matching table names */
  971. sqlite3 *db = pParse->db; /* The database */
  972. struct SrcList_item *pItem; /* Use for looping over pSrcList items */
  973. struct SrcList_item *pMatch = 0; /* The matching pSrcList item */
  974. NameContext *pTopNC = pNC; /* First namecontext in the list */
  975. Schema *pSchema = 0; /* Schema of the expression */
  976. assert( pColumnToken && pColumnToken->z ); /* The Z in X.Y.Z cannot be NULL */
  977. zDb = sqlite3NameFromToken(db, pDbToken);
  978. zTab = sqlite3NameFromToken(db, pTableToken);
  979. zCol = sqlite3NameFromToken(db, pColumnToken);
  980. if( db->mallocFailed ){
  981. goto lookupname_end;
  982. }
  983. pExpr->iTable = -1;
  984. while( pNC && cnt==0 ){
  985. ExprList *pEList;
  986. SrcList *pSrcList = pNC->pSrcList;
  987. if( pSrcList ){
  988. for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
  989. Table *pTab;
  990. int iDb;
  991. Column *pCol;
  992. pTab = pItem->pTab;
  993. assert( pTab!=0 );
  994. iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  995. assert( pTab->nCol>0 );
  996. if( zTab ){
  997. if( pItem->zAlias ){
  998. char *zTabName = pItem->zAlias;
  999. if( sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
  1000. }else{
  1001. char *zTabName = pTab->zName;
  1002. if( zTabName==0 || sqlite3StrICmp(zTabName, zTab)!=0 ) continue;
  1003. if( zDb!=0 && sqlite3StrICmp(db->aDb[iDb].zName, zDb)!=0 ){
  1004. continue;
  1005. }
  1006. }
  1007. }
  1008. if( 0==(cntTab++) ){
  1009. pExpr->iTable = pItem->iCursor;
  1010. pSchema = pTab->pSchema;
  1011. pMatch = pItem;
  1012. }
  1013. for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
  1014. if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
  1015. const char *zColl = pTab->aCol[j].zColl;
  1016. IdList *pUsing;
  1017. cnt++;
  1018. pExpr->iTable = pItem->iCursor;
  1019. pMatch = pItem;
  1020. pSchema = pTab->pSchema;
  1021. /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
  1022. pExpr->iColumn = j==pTab->iPKey ? -1 : j;
  1023. pExpr->affinity = pTab->aCol[j].affinity;
  1024. if( (pExpr->flags & EP_ExpCollate)==0 ){
  1025. pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
  1026. }
  1027. if( i<pSrcList->nSrc-1 ){
  1028. if( pItem[1].jointype & JT_NATURAL ){
  1029. /* If this match occurred in the left table of a natural join,
  1030. ** then skip the right table to avoid a duplicate match */
  1031. pItem++;
  1032. i++;
  1033. }else if( (pUsing = pItem[1].pUsing)!=0 ){
  1034. /* If this match occurs on a column that is in the USING clause
  1035. ** of a join, skip the search of the right table of the join
  1036. ** to avoid a duplicate match there. */
  1037. int k;
  1038. for(k=0; k<pUsing->nId; k++){
  1039. if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ){
  1040. pItem++;
  1041. i++;
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. }
  1052. #ifndef SQLITE_OMIT_TRIGGER
  1053. /* If we have not already resolved the name, then maybe
  1054. ** it is a new.* or old.* trigger argument reference
  1055. */
  1056. if( zDb==0 && zTab!=0 && cnt==0 && pParse->trigStack!=0 ){
  1057. TriggerStack *pTriggerStack = pParse->trigStack;
  1058. Table *pTab = 0;
  1059. if( pTriggerStack->newIdx != -1 && sqlite3StrICmp("new", zTab) == 0 ){
  1060. pExpr->iTable = pTriggerStack->newIdx;
  1061. assert( pTriggerStack->pTab );
  1062. pTab = pTriggerStack->pTab;
  1063. }else if( pTriggerStack->oldIdx != -1 && sqlite3StrICmp("old", zTab)==0 ){
  1064. pExpr->iTable = pTriggerStack->oldIdx;
  1065. assert( pTriggerStack->pTab );
  1066. pTab = pTriggerStack->pTab;
  1067. }
  1068. if( pTab ){
  1069. int iCol;
  1070. Column *pCol = pTab->aCol;
  1071. pSchema = pTab->pSchema;
  1072. cntTab++;
  1073. for(iCol=0; iCol < pTab->nCol; iCol++, pCol++) {
  1074. if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
  1075. const char *zColl = pTab->aCol[iCol].zColl;
  1076. cnt++;
  1077. pExpr->iColumn = iCol==pTab->iPKey ? -1 : iCol;
  1078. pExpr->affinity = pTab->aCol[iCol].affinity;
  1079. if( (pExpr->flags & EP_ExpCollate)==0 ){
  1080. pExpr->pColl = sqlite3FindCollSeq(db, ENC(db), zColl,-1, 0);
  1081. }
  1082. pExpr->pTab = pTab;
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. #endif /* !defined(SQLITE_OMIT_TRIGGER) */
  1089. /*
  1090. ** Perhaps the name is a reference to the ROWID
  1091. */
  1092. if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) ){
  1093. cnt = 1;
  1094. pExpr->iColumn = -1;
  1095. pExpr->affinity = SQLITE_AFF_INTEGER;
  1096. }
  1097. /*
  1098. ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
  1099. ** might refer to an result-set alias. This happens, for example, when
  1100. ** we are resolving names in the WHERE clause of the following command:
  1101. **
  1102. ** SELECT a+b AS x FROM table WHERE x<10;
  1103. **
  1104. ** In cases like this, replace pExpr with a copy of the expression that
  1105. ** forms the result set entry ("a+b" in the example) and return immediately.
  1106. ** Note that the expression in the result set should have already been
  1107. ** resolved by the time the WHERE clause is resolved.
  1108. */
  1109. if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
  1110. for(j=0; j<pEList->nExpr; j++){
  1111. char *zAs = pEList->a[j].zName;
  1112. if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
  1113. Expr *pDup, *pOrig;
  1114. assert( pExpr->pLeft==0 && pExpr->pRight==0 );
  1115. assert( pExpr->pList==0 );
  1116. assert( pExpr->pSelect==0 );
  1117. pOrig = pEList->a[j].pExpr;
  1118. if( !pNC->allowAgg && ExprHasProperty(pOrig, EP_Agg) ){
  1119. sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
  1120. sqlite3_free(zCol);
  1121. return 2;
  1122. }
  1123. pDup = sqlite3ExprDup(db, pOrig);
  1124. if( pExpr->flags & EP_ExpCollate ){
  1125. pDup->pColl = pExpr->pColl;
  1126. pDup->flags |= EP_ExpCollate;
  1127. }
  1128. if( pExpr->span.dyn ) sqlite3_free((char*)pExpr->span.z);
  1129. if( pExpr->token.dyn ) sqlite3_free((char*)pExpr->token.z);
  1130. memcpy(pExpr, pDup, sizeof(*pExpr));
  1131. sqlite3_free(pDup);
  1132. cnt = 1;
  1133. pMatch = 0;
  1134. assert( zTab==0 && zDb==0 );
  1135. goto lookupname_end_2;
  1136. }
  1137. }
  1138. }
  1139. /* Advance to the next name context. The loop will exit when either
  1140. ** we have a match (cnt>0) or when we run out of name contexts.
  1141. */
  1142. if( cnt==0 ){
  1143. pNC = pNC->pNext;
  1144. }
  1145. }
  1146. /*
  1147. ** If X and Y are NULL (in other words if only the column name Z is
  1148. ** supplied) and the value of Z is enclosed in double-quotes, then
  1149. ** Z is a string literal if it doesn't match any column names. In that
  1150. ** case, we need to return right away and not make any changes to
  1151. ** pExpr.
  1152. **
  1153. ** Because no reference was made to outer contexts, the pNC->nRef
  1154. ** fields are not changed in any context.
  1155. */
  1156. if( cnt==0 && zTab==0 && pColumnToken->z[0]=='"' ){
  1157. sqlite3_free(zCol);
  1158. return 0;
  1159. }
  1160. /*
  1161. ** cnt==0 means there was not match. cnt>1 means there were two or
  1162. ** more matches. Either way, we have an error.
  1163. */
  1164. if( cnt!=1 ){
  1165. char *z = 0;
  1166. char *zErr;
  1167. zErr = cnt==0 ? "no such column: %s" : "ambiguous column name: %s";
  1168. if( zDb ){
  1169. sqlite3SetString(&z, zDb, ".", zTab, ".", zCol, (char*)0);
  1170. }else if( zTab ){
  1171. sqlite3SetString(&z, zTab, ".", zCol, (char*)0);
  1172. }else{
  1173. z = sqlite3StrDup(zCol);
  1174. }
  1175. if( z ){
  1176. sqlite3ErrorMsg(pParse, zErr, z);
  1177. sqlite3_free(z);
  1178. pTopNC->nErr++;
  1179. }else{
  1180. db->mallocFailed = 1;
  1181. }
  1182. }
  1183. /* If a column from a table in pSrcList is referenced, then record
  1184. ** this fact in the pSrcList.a[].colUsed bitmask. Column 0 causes
  1185. ** bit 0 to be set. Column 1 sets bit 1. And so forth. If the
  1186. ** column number is greater than the number of bits in the bitmask
  1187. ** then set the high-order bit of the bitmask.
  1188. */
  1189. if( pExpr->iColumn>=0 && pMatch!=0 ){
  1190. int n = pExpr->iColumn;
  1191. if( n>=sizeof(Bitmask)*8 ){
  1192. n = sizeof(Bitmask)*8-1;
  1193. }
  1194. assert( pMatch->iCursor==pExpr->iTable );
  1195. pMatch->colUsed |= ((Bitmask)1)<<n;
  1196. }
  1197. lookupname_end:
  1198. /* Clean up and return
  1199. */
  1200. sqlite3_free(zDb);
  1201. sqlite3_free(zTab);
  1202. sqlite3ExprDelete(pExpr->pLeft);
  1203. pExpr->pLeft = 0;
  1204. sqlite3ExprDelete(pExpr->pRight);
  1205. pExpr->pRight = 0;
  1206. pExpr->op = TK_COLUMN;
  1207. lookupname_end_2:
  1208. sqlite3_free(zCol);
  1209. if( cnt==1 ){
  1210. assert( pNC!=0 );
  1211. sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
  1212. if( pMatch && !pMatch->pSelect ){
  1213. pExpr->pTab = pMatch->pTab;
  1214. }
  1215. /* Increment the nRef value on all name contexts from TopNC up to
  1216. ** the point where the name matched. */
  1217. for(;;){
  1218. assert( pTopNC!=0 );
  1219. pTopNC->nRef++;
  1220. if( pTopNC==pNC ) break;
  1221. pTopNC = pTopNC->pNext;
  1222. }
  1223. return 0;
  1224. } else {
  1225. return 1;
  1226. }
  1227. }
  1228. /*
  1229. ** This routine is designed as an xFunc for walkExprTree().
  1230. **
  1231. ** Resolve symbolic names into TK_COLUMN operators for the current
  1232. ** node in the expression tree. Return 0 to continue the search down
  1233. ** the tree or 2 to abort the tree walk.
  1234. **
  1235. ** This routine also does error checking and name resolution for
  1236. ** function names. The operator for aggregate functions is changed
  1237. ** to TK_AGG_FUNCTION.
  1238. */
  1239. static int nameResolverStep(void *pArg, Expr *pExpr){
  1240. NameContext *pNC = (NameContext*)pArg;
  1241. Parse *pParse;
  1242. if( pExpr==0 ) return 1;
  1243. assert( pNC!=0 );
  1244. pParse = pNC->pParse;
  1245. if( ExprHasAnyProperty(pExpr, EP_Resolved) ) return 1;
  1246. ExprSetProperty(pExpr, EP_Resolved);
  1247. #ifndef NDEBUG
  1248. if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
  1249. SrcList *pSrcList = pNC->pSrcList;
  1250. int i;
  1251. for(i=0; i<pNC->pSrcList->nSrc; i++){
  1252. assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
  1253. }
  1254. }
  1255. #endif
  1256. switch( pExpr->op ){
  1257. /* Double-quoted strings (ex: "abc") are used as identifiers if
  1258. ** possible. Otherwise they remain as strings. Single-quoted
  1259. ** strings (ex: 'abc') are always string literals.
  1260. */
  1261. case TK_STRING: {
  1262. if( pExpr->token.z[0]=='\'' ) break;
  1263. /* Fall thru into the TK_ID case if this is a double-quoted string */
  1264. }
  1265. /* A lone identifier is the name of a column.
  1266. */
  1267. case TK_ID: {
  1268. lookupName(pParse, 0, 0, &pExpr->token, pNC, pExpr);
  1269. return 1;
  1270. }
  1271. /* A table name and column name: ID.ID
  1272. ** Or a database, table and column: ID.ID.ID
  1273. */
  1274. case TK_DOT: {
  1275. Token *pColumn;
  1276. Token *pTable;
  1277. Token *pDb;
  1278. Expr *pRight;
  1279. /* if( pSrcList==0 ) break; */
  1280. pRight = pExpr->pRight;
  1281. if( pRight->op==TK_ID ){
  1282. pDb = 0;
  1283. pTable = &pExpr->pLeft->token;
  1284. pColumn = &pRight->token;
  1285. }else{
  1286. assert( pRight->op==TK_DOT );
  1287. pDb = &pExpr->pLeft->token;
  1288. pTable = &pRight->pLeft->token;
  1289. pColumn = &pRight->pRight->token;
  1290. }
  1291. lookupName(pParse, pDb, pTable, pColumn, pNC, pExpr);
  1292. return 1;
  1293. }
  1294. /* Resolve function names
  1295. */
  1296. case TK_CONST_FUNC:
  1297. case TK_FUNCTION: {
  1298. ExprList *pList = pExpr->pList; /* The argument list */
  1299. int n = pList ? pList->nExpr : 0; /* Number of arguments */
  1300. int no_such_func = 0; /* True if no such function exists */
  1301. int wrong_num_args = 0; /* True if wrong number of arguments */
  1302. int is_agg = 0; /* True if is an aggregate function */
  1303. int i;
  1304. int auth; /* Authorization to use the function */
  1305. int nId; /* Number of characters in function name */
  1306. const char *zId; /* The function name. */
  1307. FuncDef *pDef; /* Information about the function */
  1308. int enc = ENC(pParse->db); /* The database encoding */
  1309. zId = (char*)pExpr->token.z;
  1310. nId = pExpr->token.n;
  1311. pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
  1312. if( pDef==0 ){
  1313. pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, enc, 0);
  1314. if( pDef==0 ){
  1315. no_such_func = 1;
  1316. }else{
  1317. wrong_num_args = 1;
  1318. }
  1319. }else{
  1320. is_agg = pDef->xFunc==0;
  1321. }
  1322. #ifndef SQLITE_OMIT_AUTHORIZATION
  1323. if( pDef ){
  1324. auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
  1325. if( auth!=SQLITE_OK ){
  1326. if( auth==SQLITE_DENY ){
  1327. sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
  1328. pDef->zName);
  1329. pNC->nErr++;
  1330. }
  1331. pExpr->op = TK_NULL;
  1332. return 1;
  1333. }
  1334. }
  1335. #endif
  1336. if( is_agg && !pNC->allowAgg ){
  1337. sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
  1338. pNC->nErr++;
  1339. is_agg = 0;
  1340. }else if( no_such_func ){
  1341. sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
  1342. pNC->nErr++;
  1343. }else if( wrong_num_args ){
  1344. sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
  1345. nId, zId);
  1346. pNC->nErr++;
  1347. }
  1348. if( is_agg ){
  1349. pExpr->op = TK_AGG_FUNCTION;
  1350. pNC->hasAgg = 1;
  1351. }
  1352. if( is_agg ) pNC->allowAgg = 0;
  1353. for(i=0; pNC->nErr==0 && i<n; i++){
  1354. walkExprTree(pList->a[i].pExpr, nameResolverStep, pNC);
  1355. }
  1356. if( is_agg ) pNC->allowAgg = 1;
  1357. /* FIX ME: Compute pExpr->affinity based on the expected return
  1358. ** type of the function
  1359. */
  1360. return is_agg;
  1361. }
  1362. #ifndef SQLITE_OMIT_SUBQUERY
  1363. case TK_SELECT:
  1364. case TK_EXISTS:
  1365. #endif
  1366. case TK_IN: {
  1367. if( pExpr->pSelect ){
  1368. int nRef = pNC->nRef;
  1369. #ifndef SQLITE_OMIT_CHECK
  1370. if( pNC->isCheck ){
  1371. sqlite3ErrorMsg(pParse,"subqueries prohibited in CHECK constraints");
  1372. }
  1373. #endif
  1374. sqlite3SelectResolve(pParse, pExpr->pSelect, pNC);
  1375. assert( pNC->nRef>=nRef );
  1376. if( nRef!=pNC->nRef ){
  1377. ExprSetProperty(pExpr, EP_VarSelect);
  1378. }
  1379. }
  1380. break;
  1381. }
  1382. #ifndef SQLITE_OMIT_CHECK
  1383. case TK_VARIABLE: {
  1384. if( pNC->isCheck ){
  1385. sqlite3ErrorMsg(pParse,"parameters prohibited in CHECK constraints");
  1386. }
  1387. break;
  1388. }
  1389. #endif
  1390. }
  1391. return 0;
  1392. }
  1393. /*
  1394. ** This routine walks an expression tree and resolves references to
  1395. ** table columns. Nodes of the form ID.ID or ID resolve into an
  1396. ** index to the table in the table list and a column offset. The
  1397. ** Expr.opcode for such nodes is changed to TK_COLUMN. The Expr.iTable
  1398. ** value is changed to the index of the referenced table in pTabList
  1399. ** plus the "base" value. The base value will ultimately become the
  1400. ** VDBE cursor number for a cursor that is pointing into the referenced
  1401. ** table. The Expr.iColumn value is changed to the index of the column
  1402. ** of the referenced table. The Expr.iColumn value for the special
  1403. ** ROWID column is -1. Any INTEGER PRIMARY KEY column is tried as an
  1404. ** alias for ROWID.
  1405. **
  1406. ** Also resolve function names and check the functions for proper
  1407. ** usage. Make sure all function names are recognized and all functions
  1408. ** have the correct number of arguments. Leave an error message
  1409. ** in pParse->zErrMsg if anything is amiss. Return the number of errors.
  1410. **
  1411. ** If the expression contains aggregate functions then set the EP_Agg
  1412. ** property on the expression.
  1413. */
  1414. int sqlite3ExprResolveNames(
  1415. NameContext *pNC, /* Namespace to resolve expressions in. */
  1416. Expr *pExpr /* The expression to be analyzed. */
  1417. ){
  1418. int savedHasAgg;
  1419. if( pExpr==0 ) return 0;
  1420. #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
  1421. if( (pExpr->nHeight+pNC->pParse->nHeight)>SQLITE_MAX_EXPR_DEPTH ){
  1422. sqlite3ErrorMsg(pNC->pParse,
  1423. "Expression tree is too large (maximum depth %d)",
  1424. SQLITE_MAX_EXPR_DEPTH
  1425. );
  1426. return 1;
  1427. }
  1428. pNC->pParse->nHeight += pExpr->nHeight;
  1429. #endif
  1430. savedHasAgg = pNC->hasAgg;
  1431. pNC->hasAgg = 0;
  1432. walkExprTree(pExpr, nameResolverStep, pNC);
  1433. #if defined(SQLITE_TEST) || SQLITE_MAX_EXPR_DEPTH>0
  1434. pNC->pParse->nHeight -= pExpr->nHeight;
  1435. #endif
  1436. if( pNC->nErr>0 ){
  1437. ExprSetProperty(pExpr, EP_Error);
  1438. }
  1439. if( pNC->hasAgg ){
  1440. ExprSetProperty(pExpr, EP_Agg);
  1441. }else if( savedHasAgg ){
  1442. pNC->hasAgg = 1;
  1443. }
  1444. return ExprHasProperty(pExpr, EP_Error);
  1445. }
  1446. /*
  1447. ** A pointer instance of this structure is used to pass information
  1448. ** through walkExprTree into codeSubqueryStep().
  1449. */
  1450. typedef struct QueryCoder QueryCoder;
  1451. struct QueryCoder {
  1452. Parse *pParse; /* The parsing context */
  1453. NameContext *pNC; /* Namespace of first enclosing query */
  1454. };
  1455. #ifdef SQLITE_TEST
  1456. int sqlite3_enable_in_opt = 1;
  1457. #else
  1458. #define sqlite3_enable_in_opt 1
  1459. #endif
  1460. /*
  1461. ** This function is used by the implementation of the IN (...) operator.
  1462. ** It's job is to find or create a b-tree structure that may be used
  1463. ** either to test for membership of the (...) set or to iterate through
  1464. ** its members, skipping duplicates.
  1465. **
  1466. ** The cursor opened on the structure (database table, database index
  1467. ** or ephermal table) is stored in pX->iTable before this function returns.
  1468. ** The returned value indicates the structure type, as follows:
  1469. **
  1470. ** IN_INDEX_ROWID - The cursor was opened on a database table.
  1471. ** IN_INDEX_INDEX - The cursor was opened on a database indec.
  1472. ** IN_INDEX_EPH - The cursor was opened on a specially created and
  1473. ** populated epheremal table.
  1474. **
  1475. ** An existing structure may only be used if the SELECT is of the simple
  1476. ** form:
  1477. **
  1478. ** SELECT <column> FROM <table>
  1479. **
  1480. ** If the mustBeUnique parameter is false, the structure will be used
  1481. ** for fast set membership tests. In this case an epheremal table must
  1482. ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
  1483. ** be found with <column> as its left-most column.
  1484. **
  1485. ** If mustBeUnique is true, then the structure will be used to iterate
  1486. ** through the set members, skipping any duplicates. In this case an
  1487. ** epheremal table must be used unless the selected <column> is guaranteed
  1488. ** to be unique - either because it is an INTEGER PRIMARY KEY or it
  1489. ** is unique by virtue of a constraint or implicit index.
  1490. */
  1491. #ifndef SQLITE_OMIT_SUBQUERY
  1492. int sqlite3FindInIndex(Parse *pParse, Expr *pX, int mustBeUnique){
  1493. Select *p;
  1494. int eType = 0;
  1495. int iTab = pParse->nTab++;
  1496. /* The follwing if(...) expression is true if the SELECT is of the
  1497. ** simple form:
  1498. **
  1499. ** SELECT <column> FROM <table>
  1500. **
  1501. ** If this is the case, it may be possible to use an existing table
  1502. ** or index instead of generating an epheremal table.
  1503. */
  1504. if( sqlite3_enable_in_opt
  1505. && (p=pX->pSelect) && !p->pPrior
  1506. && !p->isDistinct && !p->isAgg && !p->pGroupBy
  1507. && p->pSrc && p->pSrc->nSrc==1 && !p->pSrc->a[0].pSelect
  1508. && !p->pSrc->a[0].pTab->pSelect
  1509. && p->pEList->nExpr==1 && p->pEList->a[0].pExpr->op==TK_COLUMN
  1510. && !p->pLimit && !p->pOffset && !p->pWhere
  1511. ){
  1512. sqlite3 *db = pParse->db;
  1513. Index *pIdx;
  1514. Expr *pExpr = p->pEList->a[0].pExpr;
  1515. int iCol = pExpr->iColumn;
  1516. Vdbe *v = sqlite3GetVdbe(pParse);
  1517. /* This function is only called from two places. In both cases the vdbe
  1518. ** has already been allocated. So assume sqlite3GetVdbe() is always
  1519. ** successful here.
  1520. */
  1521. assert(v);
  1522. if( iCol<0 ){
  1523. int iMem = pParse->nMem++;
  1524. int iAddr;
  1525. Table *pTab = p->pSrc->a[0].pTab;
  1526. int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
  1527. sqlite3VdbeUsesBtree(v, iDb);
  1528. sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
  1529. iAddr = sqlite3VdbeAddOp(v, OP_If, 0, iMem);
  1530. sqlite3VdbeAddOp(v, OP_MemInt, 1, iMem);
  1531. sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  1532. eType = IN_INDEX_ROWID;
  1533. sqlite3VdbeJumpHere(v, iAddr);
  1534. }else{
  1535. /* The collation sequence used by the comparison. If an index is to
  1536. ** be used in place of a temp-table, it must be ordered according
  1537. ** to this collation sequence.
  1538. */
  1539. CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
  1540. /* Check that the affinity that will be used to perform the
  1541. ** comparison is the same as the affinity of the column. If
  1542. ** it is not, it is not possible to use any index.
  1543. */
  1544. Table *pTab = p->pSrc->a[0].pTab;
  1545. char aff = comparisonAffinity(pX);
  1546. int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE);
  1547. for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
  1548. if( (pIdx->aiColumn[0]==iCol)
  1549. && (pReq==sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], -1, 0))
  1550. && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
  1551. ){
  1552. int iDb;
  1553. int iMem = pParse->nMem++;
  1554. int iAddr;
  1555. char *pKey;
  1556. pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
  1557. iDb = sqlite3SchemaToIndex(db, pIdx->pSchema);
  1558. sqlite3VdbeUsesBtree(v, iDb);
  1559. sqlite3VdbeAddOp(v, OP_MemLoad, iMem, 0);
  1560. iAddr = sqlite3VdbeAddOp(v, OP_If, 0, iMem);
  1561. sqlite3VdbeAddOp(v, OP_MemInt, 1, iMem);
  1562. sqlite3VdbeAddOp(v, OP_Integer, iDb, 0);
  1563. VdbeComment((v, "# %s", pIdx->zName));
  1564. sqlite3VdbeOp3(v,OP_OpenRead,iTab,pIdx->tnum,pKey,P3_KEYINFO_HANDOFF);
  1565. eType = IN_INDEX_INDEX;
  1566. sqlite3VdbeAddOp(v, OP_SetNumColumns, iTab, pIdx->nColumn);
  1567. sqlite3VdbeJumpHere(v, iAddr);
  1568. }
  1569. }
  1570. }
  1571. }
  1572. if( eType==0 ){
  1573. sqlite3CodeSubselect(pParse, pX);
  1574. eType = IN_INDEX_EPH;
  1575. }else{
  1576. pX->iTable = iTab;
  1577. }
  1578. return eType;
  1579. }
  1580. #endif
  1581. /*
  1582. ** Generate code for scalar subqueries used as an expression
  1583. ** and IN operators. Examples:
  1584. **
  1585. ** (SELECT a FROM b) -- subquery
  1586. ** EXISTS (SELECT a FROM b) -- EXISTS subquery
  1587. ** x IN (4,5,11) -- IN operator with list on right-hand side
  1588. ** x IN (SELECT a FROM b) -- IN operator with subquery on the right
  1589. **
  1590. ** The pExpr parameter describes the expression that contains the IN
  1591. ** operator or subquery.
  1592. */
  1593. #ifndef SQLITE_OMIT_SUBQUERY
  1594. void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
  1595. int testAddr = 0; /* One-time test address */
  1596. Vdbe *v = sqlite3GetVdbe(pParse);
  1597. if( v==0 ) return;
  1598. /* This code must be run in its entirety every time it is encountered
  1599. ** if any of the following is true:
  1600. **
  1601. ** * The right-hand side is a correlated subquery
  1602. ** * The right-hand side is an expression list containing variables
  1603. ** * We are inside a trigger
  1604. **
  1605. ** If all of the above are false, then we can run this code just once
  1606. ** save the results, and reuse the same result on subsequent invocations.
  1607. */
  1608. if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
  1609. int mem = pParse->nMem++;
  1610. sqlite3VdbeAddOp(v, OP_MemLoad, mem, 0);
  1611. testAddr = sqlite3VdbeAddOp(v, OP_If, 0, 0);
  1612. assert( testAddr>0 || pParse->db->mallocFailed );
  1613. sqlite3VdbeAddOp(v, OP_MemInt, 1, mem);
  1614. }
  1615. switch( pExpr->op ){
  1616. case TK_IN: {
  1617. char affinity;
  1618. KeyInfo keyInfo;
  1619. int addr; /* Address of OP_OpenEphemeral instruction */
  1620. affinity = sqlite3ExprAffinity(pExpr->pLeft);
  1621. /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
  1622. ** expression it is handled the same way. A virtual table is
  1623. ** filled with single-field index keys representing the results
  1624. ** from the SELECT or the <exprlist>.
  1625. **
  1626. ** If the 'x' expression is a column value, or the SELECT...
  1627. ** statement returns a column value, then the affinity of that
  1628. ** column is used to build the index keys. If both 'x' and the
  1629. ** SELECT... statement are columns, then numeric affinity is used
  1630. ** if either column has NUMERIC or INTEGER affinity. If neither
  1631. ** 'x' nor the SELECT... statement are columns, then numeric affinity
  1632. ** is used.
  1633. */
  1634. pExpr->iTable = pParse->nTab++;
  1635. addr = sqlite3VdbeAddOp(v, OP_OpenEphemeral, pExpr->iTable, 0);
  1636. memset(&keyInfo, 0, sizeof(keyInfo));
  1637. keyInfo.nField = 1;
  1638. sqlite3VdbeAddOp(v, OP_SetNumColumns, pExpr->iTable, 1);
  1639. if( pExpr->pSelect ){
  1640. /* Case 1: expr IN (SELECT ...)
  1641. **
  1642. ** Generate code to write the results of the select into the temporary
  1643. ** table allocated and opened above.
  1644. */
  1645. int iParm = pExpr->iTable + (((int)affinity)<<16);
  1646. ExprList *pEList;
  1647. assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
  1648. if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
  1649. return;
  1650. }
  1651. pEList = pExpr->pSelect->pEList;
  1652. if( pEList && pEList->nExpr>0 ){
  1653. keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
  1654. pEList->a[0].pExpr);
  1655. }
  1656. }else if( pExpr->pList ){
  1657. /* Case 2: expr IN (exprlist)
  1658. **
  1659. ** For each expression, build an index key from the evaluation and
  1660. ** store it in the temporary table. If <expr> is a column, then use
  1661. ** that columns affinity when building index keys. If <expr> is not
  1662. ** a column, use numeric affinity.
  1663. */
  1664. int i;
  1665. ExprList *pList = pExpr->pList;
  1666. struct ExprList_item *pItem;
  1667. if( !affinity ){
  1668. affinity = SQLITE_AFF_NONE;
  1669. }
  1670. keyInfo.aColl[0] = pExpr->pLeft->pColl;
  1671. /* Loop through each expression in <exprlist>. */
  1672. for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
  1673. Expr *pE2 = pItem->pExpr;
  1674. /* If the expression is not constant then we will need to
  1675. ** disable the test that was generated above that makes sure
  1676. ** this code only executes once. Because for a non-constant
  1677. ** expression we need to rerun this code each time.
  1678. */
  1679. if( testAddr>0 && !sqlite3ExprIsConstant(pE2) ){
  1680. sqlite3VdbeChangeToNoop(v, testAddr-1, 3);
  1681. testAddr = 0;
  1682. }
  1683. /* Evaluate the expression and insert it into the temp table */
  1684. sqlite3ExprCode(pParse, pE2);
  1685. sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1);
  1686. sqlite3VdbeAddOp(v, OP_IdxInsert, pExpr->iTable, 0);
  1687. }
  1688. }
  1689. sqlite3VdbeChangeP3(v, addr, (void *)&keyInfo, P3_KEYINFO);
  1690. break;
  1691. }
  1692. case TK_EXISTS:
  1693. case TK_SELECT: {
  1694. /* This has to be a scalar SELECT. Generate code to put the
  1695. ** value of this select in a memory cell and record the number
  1696. ** of the memory cell in iColumn.
  1697. */
  1698. static const Token one = { (u8*)"1", 0, 1 };
  1699. Select *pSel;
  1700. int iMem;
  1701. int sop;
  1702. pExpr->iColumn = iMem = pParse->nMem++;
  1703. pSel = pExpr->pSelect;
  1704. if( pExpr->op==TK_SELECT ){
  1705. sop = SRT_Mem;
  1706. sqlite3VdbeAddOp(v, OP_MemNull, iMem, 0);
  1707. VdbeComment((v, "# Init subquery result"));
  1708. }else{
  1709. sop = SRT_Exists;
  1710. sqlite3VdbeAddOp(v, OP_MemInt, 0, iMem);
  1711. VdbeComment((v, "# Init EXISTS result"));
  1712. }
  1713. sqlite3ExprDelete(pSel->pLimit);
  1714. pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one);
  1715. if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
  1716. return;
  1717. }
  1718. break;
  1719. }
  1720. }
  1721. if( testAddr ){
  1722. sqlite3VdbeJumpHere(v, testAddr);
  1723. }
  1724. return;
  1725. }
  1726. #endif /* SQLITE_OMIT_SUBQUERY */
  1727. /*
  1728. ** Duplicate an 8-byte value
  1729. */
  1730. static char *dup8bytes(Vdbe *v, const char *in){
  1731. char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
  1732. if( out ){
  1733. memcpy(out, in, 8);
  1734. }
  1735. return out;
  1736. }
  1737. /*
  1738. ** Generate an instruction that will put the floating point
  1739. ** value described by z[0..n-1] on the stack.
  1740. **
  1741. ** The z[] string will probably not be zero-terminated. But the
  1742. ** z[n] character is guaranteed to be something that does not look
  1743. ** like the continuation of the number.
  1744. */
  1745. static void codeReal(Vdbe *v, const char *z, int n, int negateFlag){
  1746. assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
  1747. if( z ){
  1748. double value;
  1749. char *zV;
  1750. assert( !isdigit(z[n]) );
  1751. sqlite3AtoF(z, &value);
  1752. if( negateFlag ) value = -value;
  1753. zV = dup8bytes(v, (char*)&value);
  1754. sqlite3VdbeOp3(v, OP_Real, 0, 0, zV, P3_REAL);
  1755. }
  1756. }
  1757. /*
  1758. ** Generate an instruction that will put the integer describe by
  1759. ** text z[0..n-1] on the stack.
  1760. **
  1761. ** The z[] string will probably not be zero-terminated. But the
  1762. ** z[n] character is guaranteed to be something that does not look
  1763. ** like the continuation of the number.
  1764. */
  1765. static void codeInteger(Vdbe *v, const char *z, int n, int negateFlag){
  1766. assert( z || v==0 || sqlite3VdbeDb(v)->mallocFailed );
  1767. if( z ){
  1768. int i;
  1769. assert( !isdigit(z[n]) );
  1770. if( sqlite3GetInt32(z, &i) ){
  1771. if( negateFlag ) i = -i;
  1772. sqlite3VdbeAddOp(v, OP_Integer, i, 0);
  1773. }else if( sqlite3FitsIn64Bits(z, negateFlag) ){
  1774. i64 value;
  1775. char *zV;
  1776. sqlite3Atoi64(z, &value);
  1777. if( negateFlag ) value = -value;
  1778. zV = dup8bytes(v, (char*)&value);
  1779. sqlite3VdbeOp3(v, OP_Int64, 0, 0, zV, P3_INT64);
  1780. }else{
  1781. codeReal(v, z, n, negateFlag);
  1782. }
  1783. }
  1784. }
  1785. /*
  1786. ** Generate code that will extract the iColumn-th column from
  1787. ** table pTab and push that column value on the stack. There
  1788. ** is an open cursor to pTab in iTable. If iColumn<0 then
  1789. ** code is generated that extracts the rowid.
  1790. */
  1791. void sqlite3ExprCodeGetColumn(Vdbe *v, Table *pTab, int iColumn, int iTable){
  1792. if( iColumn<0 ){
  1793. int op = (pTab && IsVirtual(pTab)) ? OP_VRowid : OP_Rowid;
  1794. sqlite3VdbeAddOp(v, op, iTable, 0);
  1795. }else if( pTab==0 ){
  1796. sqlite3VdbeAddOp(v, OP_Column, iTable, iColumn);
  1797. }else{
  1798. int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
  1799. sqlite3VdbeAddOp(v, op, iTable, iColumn);
  1800. sqlite3ColumnDefault(v, pTab, iColumn);
  1801. #ifndef SQLITE_OMIT_FLOATING_POINT
  1802. if( pTab->aCol[iColumn].affinity==SQLITE_AFF_REAL ){
  1803. sqlite3VdbeAddOp(v, OP_RealAffinity, 0, 0);
  1804. }
  1805. #endif
  1806. }
  1807. }
  1808. /*
  1809. ** Generate code into the current Vdbe to evaluate the given
  1810. ** expression and leave the result on the top of stack.
  1811. **
  1812. ** This code depends on the fact that certain token values (ex: TK_EQ)
  1813. ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
  1814. ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
  1815. ** the make process cause these values to align. Assert()s in the code
  1816. ** below verify that the numbers are aligned correctly.
  1817. */
  1818. void sqlite3ExprCode(Parse *pParse, Expr *pExpr){
  1819. Vdbe *v = pParse->pVdbe;
  1820. int op;
  1821. int stackChng = 1; /* Amount of change to stack depth */
  1822. if( v==0 ) return;
  1823. if( pExpr==0 ){
  1824. sqlite3VdbeAddOp(v, OP_Null, 0, 0);
  1825. return;
  1826. }
  1827. op = pExpr->op;
  1828. switch( op ){
  1829. case TK_AGG_COLUMN: {
  1830. AggInfo *pAggInfo = pExpr->pAggInfo;
  1831. struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
  1832. if( !pAggInfo->directMode ){
  1833. sqlite3VdbeAddOp(v, OP_MemLoad, pCol->iMem, 0);
  1834. break;
  1835. }else if( pAggInfo->useSortingIdx ){
  1836. sqlite3VdbeAddOp(v, OP_Column, pAggInfo->sortingIdx,
  1837. pCol->iSorterColumn);
  1838. break;
  1839. }
  1840. /* Otherwise, fall thru into the TK_COLUMN case */
  1841. }
  1842. case TK_COLUMN: {
  1843. if( pExpr->iTable<0 ){
  1844. /* This only happens when coding check constraints */
  1845. assert( pParse->ckOffset>0 );
  1846. sqlite3VdbeAddOp(v, OP_Dup, pParse->ckOffset-pExpr->iColumn-1, 1);
  1847. }else{
  1848. sqlite3ExprCodeGetColumn(v, pExpr->pTab, pExpr->iColumn, pExpr->iTable);
  1849. }
  1850. break;
  1851. }
  1852. case TK_INTEGER: {
  1853. codeInteger(v, (char*)pExpr->token.z, pExpr->token.n, 0);
  1854. break;
  1855. }
  1856. case TK_FLOAT: {
  1857. codeReal(v, (char*)pExpr->token.z, pExpr->token.n, 0);
  1858. break;
  1859. }
  1860. case TK_STRING: {
  1861. sqlite3DequoteExpr(pParse->db, pExpr);
  1862. sqlite3VdbeOp3(v,OP_String8, 0, 0, (char*)pExpr->token.z, pExpr->token.n);
  1863. break;
  1864. }
  1865. case TK_NULL: {
  1866. sqlite3VdbeAddOp(v, OP_Null, 0, 0);
  1867. break;
  1868. }
  1869. #ifndef SQLITE_OMIT_BLOB_LITERAL
  1870. case TK_BLOB: {
  1871. int n;
  1872. const char *z;
  1873. assert( TK_BLOB==OP_HexBlob );
  1874. n = pExpr->token.n - 3;
  1875. z = (char*)pExpr->token.z + 2;
  1876. assert( n>=0 );
  1877. if( n==0 ){
  1878. z = "";
  1879. }
  1880. sqlite3VdbeOp3(v, op, 0, 0, z, n);
  1881. break;
  1882. }
  1883. #endif
  1884. case TK_VARIABLE: {
  1885. sqlite3VdbeAddOp(v, OP_Variable, pExpr->iTable, 0);
  1886. if( pExpr->token.n>1 ){
  1887. sqlite3VdbeChangeP3(v, -1, (char*)pExpr->token.z, pExpr->token.n);
  1888. }
  1889. break;
  1890. }
  1891. case TK_REGISTER: {
  1892. sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iTable, 0);
  1893. break;
  1894. }
  1895. #ifndef SQLITE_OMIT_CAST
  1896. case TK_CAST: {
  1897. /* Expressions of the form: CAST(pLeft AS token) */
  1898. int aff, to_op;
  1899. sqlite3ExprCode(pParse, pExpr->pLeft);
  1900. aff = sqlite3AffinityType(&pExpr->token);
  1901. to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
  1902. assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT );
  1903. assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE );
  1904. assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
  1905. assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER );
  1906. assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL );
  1907. sqlite3VdbeAddOp(v, to_op, 0, 0);
  1908. stackChng = 0;
  1909. break;
  1910. }
  1911. #endif /* SQLITE_OMIT_CAST */
  1912. case TK_LT:
  1913. case TK_LE:
  1914. case TK_GT:
  1915. case TK_GE:
  1916. case TK_NE:
  1917. case TK_EQ: {
  1918. assert( TK_LT==OP_Lt );
  1919. assert( TK_LE==OP_Le );
  1920. assert( TK_GT==OP_Gt );
  1921. assert( TK_GE==OP_Ge );
  1922. assert( TK_EQ==OP_Eq );
  1923. assert( TK_NE==OP_Ne );
  1924. sqlite3ExprCode(pParse, pExpr->pLeft);
  1925. sqlite3ExprCode(pParse, pExpr->pRight);
  1926. codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 0, 0);
  1927. stackChng = -1;
  1928. break;
  1929. }
  1930. case TK_AND:
  1931. case TK_OR:
  1932. case TK_PLUS:
  1933. case TK_STAR:
  1934. case TK_MINUS:
  1935. case TK_REM:
  1936. case TK_BITAND:
  1937. case TK_BITOR:
  1938. case TK_SLASH:
  1939. case TK_LSHIFT:
  1940. case TK_RSHIFT:
  1941. case TK_CONCAT: {
  1942. assert( TK_AND==OP_And );
  1943. assert( TK_OR==OP_Or );
  1944. assert( TK_PLUS==OP_Add );
  1945. assert( TK_MINUS==OP_Subtract );
  1946. assert( TK_REM==OP_Remainder );
  1947. assert( TK_BITAND==OP_BitAnd );
  1948. assert( TK_BITOR==OP_BitOr );
  1949. assert( TK_SLASH==OP_Divide );
  1950. assert( TK_LSHIFT==OP_ShiftLeft );
  1951. assert( TK_RSHIFT==OP_ShiftRight );
  1952. assert( TK_CONCAT==OP_Concat );
  1953. sqlite3ExprCode(pParse, pExpr->pLeft);
  1954. sqlite3ExprCode(pParse, pExpr->pRight);
  1955. sqlite3VdbeAddOp(v, op, 0, 0);
  1956. stackChng = -1;
  1957. break;
  1958. }
  1959. case TK_UMINUS: {
  1960. Expr *pLeft = pExpr->pLeft;
  1961. assert( pLeft );
  1962. if( pLeft->op==TK_FLOAT || pLeft->op==TK_INTEGER ){
  1963. Token *p = &pLeft->token;
  1964. if( pLeft->op==TK_FLOAT ){
  1965. codeReal(v, (char*)p->z, p->n, 1);
  1966. }else{
  1967. codeInteger(v, (char*)p->z, p->n, 1);
  1968. }
  1969. break;
  1970. }
  1971. /* Fall through into TK_NOT */
  1972. }
  1973. case TK_BITNOT:
  1974. case TK_NOT: {
  1975. assert( TK_BITNOT==OP_BitNot );
  1976. assert( TK_NOT==OP_Not );
  1977. sqlite3ExprCode(pParse, pExpr->pLeft);
  1978. sqlite3VdbeAddOp(v, op, 0, 0);
  1979. stackChng = 0;
  1980. break;
  1981. }
  1982. case TK_ISNULL:
  1983. case TK_NOTNULL: {
  1984. int dest;
  1985. assert( TK_ISNULL==OP_IsNull );
  1986. assert( TK_NOTNULL==OP_NotNull );
  1987. sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
  1988. sqlite3ExprCode(pParse, pExpr->pLeft);
  1989. dest = sqlite3VdbeCurrentAddr(v) + 2;
  1990. sqlite3VdbeAddOp(v, op, 1, dest);
  1991. sqlite3VdbeAddOp(v, OP_AddImm, -1, 0);
  1992. stackChng = 0;
  1993. break;
  1994. }
  1995. case TK_AGG_FUNCTION: {
  1996. AggInfo *pInfo = pExpr->pAggInfo;
  1997. if( pInfo==0 ){
  1998. sqlite3ErrorMsg(pParse, "misuse of aggregate: %T",
  1999. &pExpr->span);
  2000. }else{
  2001. sqlite3VdbeAddOp(v, OP_MemLoad, pInfo->aFunc[pExpr->iAgg].iMem, 0);
  2002. }
  2003. break;
  2004. }
  2005. case TK_CONST_FUNC:
  2006. case TK_FUNCTION: {
  2007. ExprList *pList = pExpr->pList;
  2008. int nExpr = pList ? pList->nExpr : 0;
  2009. FuncDef *pDef;
  2010. int nId;
  2011. const char *zId;
  2012. int constMask = 0;
  2013. int i;
  2014. sqlite3 *db = pParse->db;
  2015. u8 enc = ENC(db);
  2016. CollSeq *pColl = 0;
  2017. zId = (char*)pExpr->token.z;
  2018. nId = pExpr->token.n;
  2019. pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, enc, 0);
  2020. assert( pDef!=0 );
  2021. nExpr = sqlite3ExprCodeExprList(pParse, pList);
  2022. #ifndef SQLITE_OMIT_VIRTUALTABLE
  2023. /* Possibly overload the function if the first argument is
  2024. ** a virtual table column.
  2025. **
  2026. ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
  2027. ** second argument, not the first, as the argument to test to
  2028. ** see if it is a column in a virtual table. This is done because
  2029. ** the left operand of infix functions (the operand we want to
  2030. ** control overloading) ends up as the second argument to the
  2031. ** function. The expression "A glob B" is equivalent to
  2032. ** "glob(B,A). We want to use the A in "A glob B" to test
  2033. ** for function overloading. But we use the B term in "glob(B,A)".
  2034. */
  2035. if( nExpr>=2 && (pExpr->flags & EP_InfixFunc) ){
  2036. pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[1].pExpr);
  2037. }else if( nExpr>0 ){
  2038. pDef = sqlite3VtabOverloadFunction(db, pDef, nExpr, pList->a[0].pExpr);
  2039. }
  2040. #endif
  2041. for(i=0; i<nExpr && i<32; i++){
  2042. if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
  2043. constMask |= (1<<i);
  2044. }
  2045. if( pDef->needCollSeq && !pColl ){
  2046. pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
  2047. }
  2048. }
  2049. if( pDef->needCollSeq ){
  2050. if( !pColl ) pColl = pParse->db->pDfltColl;
  2051. sqlite3VdbeOp3(v, OP_CollSeq, 0, 0, (char *)pColl, P3_COLLSEQ);
  2052. }
  2053. sqlite3VdbeOp3(v, OP_Function, constMask, nExpr, (char*)pDef, P3_FUNCDEF);
  2054. stackChng = 1-nExpr;
  2055. break;
  2056. }
  2057. #ifndef SQLITE_OMIT_SUBQUERY
  2058. case TK_EXISTS:
  2059. case TK_SELECT: {
  2060. if( pExpr->iColumn==0 ){
  2061. sqlite3CodeSubselect(pParse, pExpr);
  2062. }
  2063. sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
  2064. VdbeComment((v, "# load subquery result"));
  2065. break;
  2066. }
  2067. case TK_IN: {
  2068. int addr;
  2069. char affinity;
  2070. int ckOffset = pParse->ckOffset;
  2071. int eType;
  2072. int iLabel = sqlite3VdbeMakeLabel(v);
  2073. eType = sqlite3FindInIndex(pParse, pExpr, 0);
  2074. /* Figure out the affinity to use to create a key from the results
  2075. ** of the expression. affinityStr stores a static string suitable for
  2076. ** P3 of OP_MakeRecord.
  2077. */
  2078. affinity = comparisonAffinity(pExpr);
  2079. sqlite3VdbeAddOp(v, OP_Integer, 1, 0);
  2080. pParse->ckOffset = (ckOffset ? (ckOffset+1) : 0);
  2081. /* Code the <expr> from "<expr> IN (...)". The temporary table
  2082. ** pExpr->iTable contains the values that make up the (...) set.
  2083. */
  2084. sqlite3ExprCode(pParse, pExpr->pLeft);
  2085. addr = sqlite3VdbeCurrentAddr(v);
  2086. sqlite3VdbeAddOp(v, OP_NotNull, -1, addr+4); /* addr + 0 */
  2087. sqlite3VdbeAddOp(v, OP_Pop, 2, 0);
  2088. sqlite3VdbeAddOp(v, OP_Null, 0, 0);
  2089. sqlite3VdbeAddOp(v, OP_Goto, 0, iLabel);
  2090. if( eType==IN_INDEX_ROWID ){
  2091. int iAddr = sqlite3VdbeCurrentAddr(v)+3;
  2092. sqlite3VdbeAddOp(v, OP_MustBeInt, 1, iAddr);
  2093. sqlite3VdbeAddOp(v, OP_NotExists, pExpr->iTable, iAddr);
  2094. sqlite3VdbeAddOp(v, OP_Goto, pExpr->iTable, iLabel);
  2095. }else{
  2096. sqlite3VdbeOp3(v, OP_MakeRecord, 1, 0, &affinity, 1); /* addr + 4 */
  2097. sqlite3VdbeAddOp(v, OP_Found, pExpr->iTable, iLabel);
  2098. }
  2099. sqlite3VdbeAddOp(v, OP_AddImm, -1, 0); /* addr + 6 */
  2100. sqlite3VdbeResolveLabel(v, iLabel);
  2101. break;
  2102. }
  2103. #endif
  2104. case TK_BETWEEN: {
  2105. Expr *pLeft = pExpr->pLeft;
  2106. struct ExprList_item *pLItem = pExpr->pList->a;
  2107. Expr *pRight = pLItem->pExpr;
  2108. sqlite3ExprCode(pParse, pLeft);
  2109. sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
  2110. sqlite3ExprCode(pParse, pRight);
  2111. codeCompare(pParse, pLeft, pRight, OP_Ge, 0, 0);
  2112. sqlite3VdbeAddOp(v, OP_Pull, 1, 0);
  2113. pLItem++;
  2114. pRight = pLItem->pExpr;
  2115. sqlite3ExprCode(pParse, pRight);
  2116. codeCompare(pParse, pLeft, pRight, OP_Le, 0, 0);
  2117. sqlite3VdbeAddOp(v, OP_And, 0, 0);
  2118. break;
  2119. }
  2120. case TK_UPLUS: {
  2121. sqlite3ExprCode(pParse, pExpr->pLeft);
  2122. stackChng = 0;
  2123. break;
  2124. }
  2125. case TK_CASE: {
  2126. int expr_end_label;
  2127. int jumpInst;
  2128. int nExpr;
  2129. int i;
  2130. ExprList *pEList;
  2131. struct ExprList_item *aListelem;
  2132. assert(pExpr->pList);
  2133. assert((pExpr->pList->nExpr % 2) == 0);
  2134. assert(pExpr->pList->nExpr > 0);
  2135. pEList = pExpr->pList;
  2136. aListelem = pEList->a;
  2137. nExpr = pEList->nExpr;
  2138. expr_end_label = sqlite3VdbeMakeLabel(v);
  2139. if( pExpr->pLeft ){
  2140. sqlite3ExprCode(pParse, pExpr->pLeft);
  2141. }
  2142. for(i=0; i<nExpr; i=i+2){
  2143. sqlite3ExprCode(pParse, aListelem[i].pExpr);
  2144. if( pExpr->pLeft ){
  2145. sqlite3VdbeAddOp(v, OP_Dup, 1, 1);
  2146. jumpInst = codeCompare(pParse, pExpr->pLeft, aListelem[i].pExpr,
  2147. OP_Ne, 0, 1);
  2148. sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
  2149. }else{
  2150. jumpInst = sqlite3VdbeAddOp(v, OP_IfNot, 1, 0);
  2151. }
  2152. sqlite3ExprCode(pParse, aListelem[i+1].pExpr);
  2153. sqlite3VdbeAddOp(v, OP_Goto, 0, expr_end_label);
  2154. sqlite3VdbeJumpHere(v, jumpInst);
  2155. }
  2156. if( pExpr->pLeft ){
  2157. sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
  2158. }
  2159. if( pExpr->pRight ){
  2160. sqlite3ExprCode(pParse, pExpr->pRight);
  2161. }else{
  2162. sqlite3VdbeAddOp(v, OP_Null, 0, 0);
  2163. }
  2164. sqlite3VdbeResolveLabel(v, expr_end_label);
  2165. break;
  2166. }
  2167. #ifndef SQLITE_OMIT_TRIGGER
  2168. case TK_RAISE: {
  2169. if( !pParse->trigStack ){
  2170. sqlite3ErrorMsg(pParse,
  2171. "RAISE() may only be used within a trigger-program");
  2172. return;
  2173. }
  2174. if( pExpr->iColumn!=OE_Ignore ){
  2175. assert( pExpr->iColumn==OE_Rollback ||
  2176. pExpr->iColumn == OE_Abort ||
  2177. pExpr->iColumn == OE_Fail );
  2178. sqlite3DequoteExpr(pParse->db, pExpr);
  2179. sqlite3VdbeOp3(v, OP_Halt, SQLITE_CONSTRAINT, pExpr->iColumn,
  2180. (char*)pExpr->token.z, pExpr->token.n);
  2181. } else {
  2182. assert( pExpr->iColumn == OE_Ignore );
  2183. sqlite3VdbeAddOp(v, OP_ContextPop, 0, 0);
  2184. sqlite3VdbeAddOp(v, OP_Goto, 0, pParse->trigStack->ignoreJump);
  2185. VdbeComment((v, "# raise(IGNORE)"));
  2186. }
  2187. stackChng = 0;
  2188. break;
  2189. }
  2190. #endif
  2191. }
  2192. if( pParse->ckOffset ){
  2193. pParse->ckOffset += stackChng;
  2194. assert( pParse->ckOffset );
  2195. }
  2196. }
  2197. #ifndef SQLITE_OMIT_TRIGGER
  2198. /*
  2199. ** Generate code that evalutes the given expression and leaves the result
  2200. ** on the stack. See also sqlite3ExprCode().
  2201. **
  2202. ** This routine might also cache the result and modify the pExpr tree
  2203. ** so that it will make use of the cached result on subsequent evaluations
  2204. ** rather than evaluate the whole expression again. Trivial expressions are
  2205. ** not cached. If the expression is cached, its result is stored in a
  2206. ** memory location.
  2207. */
  2208. void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr){
  2209. Vdbe *v = pParse->pVdbe;
  2210. VdbeOp *pOp;
  2211. int iMem;
  2212. int addr1, addr2;
  2213. if( v==0 ) return;
  2214. addr1 = sqlite3VdbeCurrentAddr(v);
  2215. sqlite3ExprCode(pParse, pExpr);
  2216. addr2 = sqlite3VdbeCurrentAddr(v);
  2217. if( addr2>addr1+1
  2218. || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){
  2219. iMem = pExpr->iTable = pParse->nMem++;
  2220. sqlite3VdbeAddOp(v, OP_MemStore, iMem, 0);
  2221. pExpr->op = TK_REGISTER;
  2222. }
  2223. }
  2224. #endif
  2225. /*
  2226. ** Generate code that pushes the value of every element of the given
  2227. ** expression list onto the stack.
  2228. **
  2229. ** Return the number of elements pushed onto the stack.
  2230. */
  2231. int sqlite3ExprCodeExprList(
  2232. Parse *pParse, /* Parsing context */
  2233. ExprList *pList /* The expression list to be coded */
  2234. ){
  2235. struct ExprList_item *pItem;
  2236. int i, n;
  2237. if( pList==0 ) return 0;
  2238. n = pList->nExpr;
  2239. for(pItem=pList->a, i=n; i>0; i--, pItem++){
  2240. sqlite3ExprCode(pParse, pItem->pExpr);
  2241. }
  2242. return n;
  2243. }
  2244. /*
  2245. ** Generate code for a boolean expression such that a jump is made
  2246. ** to the label "dest" if the expression is true but execution
  2247. ** continues straight thru if the expression is false.
  2248. **
  2249. ** If the expression evaluates to NULL (neither true nor false), then
  2250. ** take the jump if the jumpIfNull flag is true.
  2251. **
  2252. ** This code depends on the fact that certain token values (ex: TK_EQ)
  2253. ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
  2254. ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in
  2255. ** the make process cause these values to align. Assert()s in the code
  2256. ** below verify that the numbers are aligned correctly.
  2257. */
  2258. void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
  2259. Vdbe *v = pParse->pVdbe;
  2260. int op = 0;
  2261. int ckOffset = pParse->ckOffset;
  2262. if( v==0 || pExpr==0 ) return;
  2263. op = pExpr->op;
  2264. switch( op ){
  2265. case TK_AND: {
  2266. int d2 = sqlite3VdbeMakeLabel(v);
  2267. sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2, !jumpIfNull);
  2268. sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  2269. sqlite3VdbeResolveLabel(v, d2);
  2270. break;
  2271. }
  2272. case TK_OR: {
  2273. sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  2274. sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
  2275. break;
  2276. }
  2277. case TK_NOT: {
  2278. sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  2279. break;
  2280. }
  2281. case TK_LT:
  2282. case TK_LE:
  2283. case TK_GT:
  2284. case TK_GE:
  2285. case TK_NE:
  2286. case TK_EQ: {
  2287. assert( TK_LT==OP_Lt );
  2288. assert( TK_LE==OP_Le );
  2289. assert( TK_GT==OP_Gt );
  2290. assert( TK_GE==OP_Ge );
  2291. assert( TK_EQ==OP_Eq );
  2292. assert( TK_NE==OP_Ne );
  2293. sqlite3ExprCode(pParse, pExpr->pLeft);
  2294. sqlite3ExprCode(pParse, pExpr->pRight);
  2295. codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
  2296. break;
  2297. }
  2298. case TK_ISNULL:
  2299. case TK_NOTNULL: {
  2300. assert( TK_ISNULL==OP_IsNull );
  2301. assert( TK_NOTNULL==OP_NotNull );
  2302. sqlite3ExprCode(pParse, pExpr->pLeft);
  2303. sqlite3VdbeAddOp(v, op, 1, dest);
  2304. break;
  2305. }
  2306. case TK_BETWEEN: {
  2307. /* The expression "x BETWEEN y AND z" is implemented as:
  2308. **
  2309. ** 1 IF (x < y) GOTO 3
  2310. ** 2 IF (x <= z) GOTO <dest>
  2311. ** 3 ...
  2312. */
  2313. int addr;
  2314. Expr *pLeft = pExpr->pLeft;
  2315. Expr *pRight = pExpr->pList->a[0].pExpr;
  2316. sqlite3ExprCode(pParse, pLeft);
  2317. sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
  2318. sqlite3ExprCode(pParse, pRight);
  2319. addr = codeCompare(pParse, pLeft, pRight, OP_Lt, 0, !jumpIfNull);
  2320. pRight = pExpr->pList->a[1].pExpr;
  2321. sqlite3ExprCode(pParse, pRight);
  2322. codeCompare(pParse, pLeft, pRight, OP_Le, dest, jumpIfNull);
  2323. sqlite3VdbeAddOp(v, OP_Integer, 0, 0);
  2324. sqlite3VdbeJumpHere(v, addr);
  2325. sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
  2326. break;
  2327. }
  2328. default: {
  2329. sqlite3ExprCode(pParse, pExpr);
  2330. sqlite3VdbeAddOp(v, OP_If, jumpIfNull, dest);
  2331. break;
  2332. }
  2333. }
  2334. pParse->ckOffset = ckOffset;
  2335. }
  2336. /*
  2337. ** Generate code for a boolean expression such that a jump is made
  2338. ** to the label "dest" if the expression is false but execution
  2339. ** continues straight thru if the expression is true.
  2340. **
  2341. ** If the expression evaluates to NULL (neither true nor false) then
  2342. ** jump if jumpIfNull is true or fall through if jumpIfNull is false.
  2343. */
  2344. void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
  2345. Vdbe *v = pParse->pVdbe;
  2346. int op = 0;
  2347. int ckOffset = pParse->ckOffset;
  2348. if( v==0 || pExpr==0 ) return;
  2349. /* The value of pExpr->op and op are related as follows:
  2350. **
  2351. ** pExpr->op op
  2352. ** --------- ----------
  2353. ** TK_ISNULL OP_NotNull
  2354. ** TK_NOTNULL OP_IsNull
  2355. ** TK_NE OP_Eq
  2356. ** TK_EQ OP_Ne
  2357. ** TK_GT OP_Le
  2358. ** TK_LE OP_Gt
  2359. ** TK_GE OP_Lt
  2360. ** TK_LT OP_Ge
  2361. **
  2362. ** For other values of pExpr->op, op is undefined and unused.
  2363. ** The value of TK_ and OP_ constants are arranged such that we
  2364. ** can compute the mapping above using the following expression.
  2365. ** Assert()s verify that the computation is correct.
  2366. */
  2367. op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
  2368. /* Verify correct alignment of TK_ and OP_ constants
  2369. */
  2370. assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
  2371. assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
  2372. assert( pExpr->op!=TK_NE || op==OP_Eq );
  2373. assert( pExpr->op!=TK_EQ || op==OP_Ne );
  2374. assert( pExpr->op!=TK_LT || op==OP_Ge );
  2375. assert( pExpr->op!=TK_LE || op==OP_Gt );
  2376. assert( pExpr->op!=TK_GT || op==OP_Le );
  2377. assert( pExpr->op!=TK_GE || op==OP_Lt );
  2378. switch( pExpr->op ){
  2379. case TK_AND: {
  2380. sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
  2381. sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  2382. break;
  2383. }
  2384. case TK_OR: {
  2385. int d2 = sqlite3VdbeMakeLabel(v);
  2386. sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, !jumpIfNull);
  2387. sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
  2388. sqlite3VdbeResolveLabel(v, d2);
  2389. break;
  2390. }
  2391. case TK_NOT: {
  2392. sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
  2393. break;
  2394. }
  2395. case TK_LT:
  2396. case TK_LE:
  2397. case TK_GT:
  2398. case TK_GE:
  2399. case TK_NE:
  2400. case TK_EQ: {
  2401. sqlite3ExprCode(pParse, pExpr->pLeft);
  2402. sqlite3ExprCode(pParse, pExpr->pRight);
  2403. codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, dest, jumpIfNull);
  2404. break;
  2405. }
  2406. case TK_ISNULL:
  2407. case TK_NOTNULL: {
  2408. sqlite3ExprCode(pParse, pExpr->pLeft);
  2409. sqlite3VdbeAddOp(v, op, 1, dest);
  2410. break;
  2411. }
  2412. case TK_BETWEEN: {
  2413. /* The expression is "x BETWEEN y AND z". It is implemented as:
  2414. **
  2415. ** 1 IF (x >= y) GOTO 3
  2416. ** 2 GOTO <dest>
  2417. ** 3 IF (x > z) GOTO <dest>
  2418. */
  2419. int addr;
  2420. Expr *pLeft = pExpr->pLeft;
  2421. Expr *pRight = pExpr->pList->a[0].pExpr;
  2422. sqlite3ExprCode(pParse, pLeft);
  2423. sqlite3VdbeAddOp(v, OP_Dup, 0, 0);
  2424. sqlite3ExprCode(pParse, pRight);
  2425. addr = sqlite3VdbeCurrentAddr(v);
  2426. codeCompare(pParse, pLeft, pRight, OP_Ge, addr+3, !jumpIfNull);
  2427. sqlite3VdbeAddOp(v, OP_Pop, 1, 0);
  2428. sqlite3VdbeAddOp(v, OP_Goto, 0, dest);
  2429. pRight = pExpr->pList->a[1].pExpr;
  2430. sqlite3ExprCode(pParse, pRight);
  2431. codeCompare(pParse, pLeft, pRight, OP_Gt, dest, jumpIfNull);
  2432. break;
  2433. }
  2434. default: {
  2435. sqlite3ExprCode(pParse, pExpr);
  2436. sqlite3VdbeAddOp(v, OP_IfNot, jumpIfNull, dest);
  2437. break;
  2438. }
  2439. }
  2440. pParse->ckOffset = ckOffset;
  2441. }
  2442. /*
  2443. ** Do a deep comparison of two expression trees. Return TRUE (non-zero)
  2444. ** if they are identical and return FALSE if they differ in any way.
  2445. **
  2446. ** Sometimes this routine will return FALSE even if the two expressions
  2447. ** really are equivalent. If we cannot prove that the expressions are
  2448. ** identical, we return FALSE just to be safe. So if this routine
  2449. ** returns false, then you do not really know for certain if the two
  2450. ** expressions are the same. But if you get a TRUE return, then you
  2451. ** can be sure the expressions are the same. In the places where
  2452. ** this routine is used, it does not hurt to get an extra FALSE - that
  2453. ** just might result in some slightly slower code. But returning
  2454. ** an incorrect TRUE could lead to a malfunction.
  2455. */
  2456. int sqlite3ExprCompare(Expr *pA, Expr *pB){
  2457. int i;
  2458. if( pA==0||pB==0 ){
  2459. return pB==pA;
  2460. }
  2461. if( pA->op!=pB->op ) return 0;
  2462. if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 0;
  2463. if( !sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 0;
  2464. if( !sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 0;
  2465. if( pA->pList ){
  2466. if( pB->pList==0 ) return 0;
  2467. if( pA->pList->nExpr!=pB->pList->nExpr ) return 0;
  2468. for(i=0; i<pA->pList->nExpr; i++){
  2469. if( !sqlite3ExprCompare(pA->pList->a[i].pExpr, pB->pList->a[i].pExpr) ){
  2470. return 0;
  2471. }
  2472. }
  2473. }else if( pB->pList ){
  2474. return 0;
  2475. }
  2476. if( pA->pSelect || pB->pSelect ) return 0;
  2477. if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 0;
  2478. if( pA->op!=TK_COLUMN && pA->token.z ){
  2479. if( pB->token.z==0 ) return 0;
  2480. if( pB->token.n!=pA->token.n ) return 0;
  2481. if( sqlite3StrNICmp((char*)pA->token.z,(char*)pB->token.z,pB->token.n)!=0 ){
  2482. return 0;
  2483. }
  2484. }
  2485. return 1;
  2486. }
  2487. /*
  2488. ** Add a new element to the pAggInfo->aCol[] array. Return the index of
  2489. ** the new element. Return a negative number if malloc fails.
  2490. */
  2491. static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
  2492. int i;
  2493. pInfo->aCol = sqlite3ArrayAllocate(
  2494. db,
  2495. pInfo->aCol,
  2496. sizeof(pInfo->aCol[0]),
  2497. 3,
  2498. &pInfo->nColumn,
  2499. &pInfo->nColumnAlloc,
  2500. &i
  2501. );
  2502. return i;
  2503. }
  2504. /*
  2505. ** Add a new element to the pAggInfo->aFunc[] array. Return the index of
  2506. ** the new element. Return a negative number if malloc fails.
  2507. */
  2508. static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
  2509. int i;
  2510. pInfo->aFunc = sqlite3ArrayAllocate(
  2511. db,
  2512. pInfo->aFunc,
  2513. sizeof(pInfo->aFunc[0]),
  2514. 3,
  2515. &pInfo->nFunc,
  2516. &pInfo->nFuncAlloc,
  2517. &i
  2518. );
  2519. return i;
  2520. }
  2521. /*
  2522. ** This is an xFunc for walkExprTree() used to implement
  2523. ** sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates
  2524. ** for additional information.
  2525. **
  2526. ** This routine analyzes the aggregate function at pExpr.
  2527. */
  2528. static int analyzeAggregate(void *pArg, Expr *pExpr){
  2529. int i;
  2530. NameContext *pNC = (NameContext *)pArg;
  2531. Parse *pParse = pNC->pParse;
  2532. SrcList *pSrcList = pNC->pSrcList;
  2533. AggInfo *pAggInfo = pNC->pAggInfo;
  2534. switch( pExpr->op ){
  2535. case TK_AGG_COLUMN:
  2536. case TK_COLUMN: {
  2537. /* Check to see if the column is in one of the tables in the FROM
  2538. ** clause of the aggregate query */
  2539. if( pSrcList ){
  2540. struct SrcList_item *pItem = pSrcList->a;
  2541. for(i=0; i<pSrcList->nSrc; i++, pItem++){
  2542. struct AggInfo_col *pCol;
  2543. if( pExpr->iTable==pItem->iCursor ){
  2544. /* If we reach this point, it means that pExpr refers to a table
  2545. ** that is in the FROM clause of the aggregate query.
  2546. **
  2547. ** Make an entry for the column in pAggInfo->aCol[] if there
  2548. ** is not an entry there already.
  2549. */
  2550. int k;
  2551. pCol = pAggInfo->aCol;
  2552. for(k=0; k<pAggInfo->nColumn; k++, pCol++){
  2553. if( pCol->iTable==pExpr->iTable &&
  2554. pCol->iColumn==pExpr->iColumn ){
  2555. break;
  2556. }
  2557. }
  2558. if( (k>=pAggInfo->nColumn)
  2559. && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
  2560. ){
  2561. pCol = &pAggInfo->aCol[k];
  2562. pCol->pTab = pExpr->pTab;
  2563. pCol->iTable = pExpr->iTable;
  2564. pCol->iColumn = pExpr->iColumn;
  2565. pCol->iMem = pParse->nMem++;
  2566. pCol->iSorterColumn = -1;
  2567. pCol->pExpr = pExpr;
  2568. if( pAggInfo->pGroupBy ){
  2569. int j, n;
  2570. ExprList *pGB = pAggInfo->pGroupBy;
  2571. struct ExprList_item *pTerm = pGB->a;
  2572. n = pGB->nExpr;
  2573. for(j=0; j<n; j++, pTerm++){
  2574. Expr *pE = pTerm->pExpr;
  2575. if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
  2576. pE->iColumn==pExpr->iColumn ){
  2577. pCol->iSorterColumn = j;
  2578. break;
  2579. }
  2580. }
  2581. }
  2582. if( pCol->iSorterColumn<0 ){
  2583. pCol->iSorterColumn = pAggInfo->nSortingColumn++;
  2584. }
  2585. }
  2586. /* There is now an entry for pExpr in pAggInfo->aCol[] (either
  2587. ** because it was there before or because we just created it).
  2588. ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
  2589. ** pAggInfo->aCol[] entry.
  2590. */
  2591. pExpr->pAggInfo = pAggInfo;
  2592. pExpr->op = TK_AGG_COLUMN;
  2593. pExpr->iAgg = k;
  2594. break;
  2595. } /* endif pExpr->iTable==pItem->iCursor */
  2596. } /* end loop over pSrcList */
  2597. }
  2598. return 1;
  2599. }
  2600. case TK_AGG_FUNCTION: {
  2601. /* The pNC->nDepth==0 test causes aggregate functions in subqueries
  2602. ** to be ignored */
  2603. if( pNC->nDepth==0 ){
  2604. /* Check to see if pExpr is a duplicate of another aggregate
  2605. ** function that is already in the pAggInfo structure
  2606. */
  2607. struct AggInfo_func *pItem = pAggInfo->aFunc;
  2608. for(i=0; i<pAggInfo->nFunc; i++, pItem++){
  2609. if( sqlite3ExprCompare(pItem->pExpr, pExpr) ){
  2610. break;
  2611. }
  2612. }
  2613. if( i>=pAggInfo->nFunc ){
  2614. /* pExpr is original. Make a new entry in pAggInfo->aFunc[]
  2615. */
  2616. u8 enc = ENC(pParse->db);
  2617. i = addAggInfoFunc(pParse->db, pAggInfo);
  2618. if( i>=0 ){
  2619. pItem = &pAggInfo->aFunc[i];
  2620. pItem->pExpr = pExpr;
  2621. pItem->iMem = pParse->nMem++;
  2622. pItem->pFunc = sqlite3FindFunction(pParse->db,
  2623. (char*)pExpr->token.z, pExpr->token.n,
  2624. pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
  2625. if( pExpr->flags & EP_Distinct ){
  2626. pItem->iDistinct = pParse->nTab++;
  2627. }else{
  2628. pItem->iDistinct = -1;
  2629. }
  2630. }
  2631. }
  2632. /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
  2633. */
  2634. pExpr->iAgg = i;
  2635. pExpr->pAggInfo = pAggInfo;
  2636. return 1;
  2637. }
  2638. }
  2639. }
  2640. /* Recursively walk subqueries looking for TK_COLUMN nodes that need
  2641. ** to be changed to TK_AGG_COLUMN. But increment nDepth so that
  2642. ** TK_AGG_FUNCTION nodes in subqueries will be unchanged.
  2643. */
  2644. if( pExpr->pSelect ){
  2645. pNC->nDepth++;
  2646. walkSelectExpr(pExpr->pSelect, analyzeAggregate, pNC);
  2647. pNC->nDepth--;
  2648. }
  2649. return 0;
  2650. }
  2651. /*
  2652. ** Analyze the given expression looking for aggregate functions and
  2653. ** for variables that need to be added to the pParse->aAgg[] array.
  2654. ** Make additional entries to the pParse->aAgg[] array as necessary.
  2655. **
  2656. ** This routine should only be called after the expression has been
  2657. ** analyzed by sqlite3ExprResolveNames().
  2658. **
  2659. ** If errors are seen, leave an error message in zErrMsg and return
  2660. ** the number of errors.
  2661. */
  2662. int sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
  2663. int nErr = pNC->pParse->nErr;
  2664. walkExprTree(pExpr, analyzeAggregate, pNC);
  2665. return pNC->pParse->nErr - nErr;
  2666. }
  2667. /*
  2668. ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
  2669. ** expression list. Return the number of errors.
  2670. **
  2671. ** If an error is found, the analysis is cut short.
  2672. */
  2673. int sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
  2674. struct ExprList_item *pItem;
  2675. int i;
  2676. int nErr = 0;
  2677. if( pList ){
  2678. for(pItem=pList->a, i=0; nErr==0 && i<pList->nExpr; i++, pItem++){
  2679. nErr += sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
  2680. }
  2681. }
  2682. return nErr;
  2683. }