1cce7d176Sdrh /* 2b19a2bc6Sdrh ** 2001 September 15 3cce7d176Sdrh ** 4b19a2bc6Sdrh ** The author disclaims copyright to this source code. In place of 5b19a2bc6Sdrh ** a legal notice, here is a blessing: 6cce7d176Sdrh ** 7b19a2bc6Sdrh ** May you do good and not evil. 8b19a2bc6Sdrh ** May you find forgiveness for yourself and forgive others. 9b19a2bc6Sdrh ** May you share freely, never taking more than you give. 10cce7d176Sdrh ** 11cce7d176Sdrh ************************************************************************* 121ccde15dSdrh ** This file contains routines used for analyzing expressions and 13b19a2bc6Sdrh ** for generating VDBE code that evaluates expressions in SQLite. 14cce7d176Sdrh */ 15cce7d176Sdrh #include "sqliteInt.h" 16a2e00042Sdrh 17e014a838Sdanielk1977 /* 18e014a838Sdanielk1977 ** Return the 'affinity' of the expression pExpr if any. 19e014a838Sdanielk1977 ** 20e014a838Sdanielk1977 ** If pExpr is a column, a reference to a column via an 'AS' alias, 21e014a838Sdanielk1977 ** or a sub-select with a column as the return value, then the 22e014a838Sdanielk1977 ** affinity of that column is returned. Otherwise, 0x00 is returned, 23e014a838Sdanielk1977 ** indicating no affinity for the expression. 24e014a838Sdanielk1977 ** 2560ec914cSpeter.d.reid ** i.e. the WHERE clause expressions in the following statements all 26e014a838Sdanielk1977 ** have an affinity: 27e014a838Sdanielk1977 ** 28e014a838Sdanielk1977 ** CREATE TABLE t1(a); 29e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE a; 30e014a838Sdanielk1977 ** SELECT a AS b FROM t1 WHERE b; 31e014a838Sdanielk1977 ** SELECT * FROM t1 WHERE (select a from t1); 32e014a838Sdanielk1977 */ 33bf3b721fSdanielk1977 char sqlite3ExprAffinity(Expr *pExpr){ 34580c8c18Sdrh int op; 35580c8c18Sdrh pExpr = sqlite3ExprSkipCollate(pExpr); 369bec6fb3Smistachkin if( pExpr->flags & EP_Generic ) return 0; 37580c8c18Sdrh op = pExpr->op; 38487e262fSdrh if( op==TK_SELECT ){ 396ab3a2ecSdanielk1977 assert( pExpr->flags&EP_xIsSelect ); 406ab3a2ecSdanielk1977 return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr); 41a37cdde0Sdanielk1977 } 42487e262fSdrh #ifndef SQLITE_OMIT_CAST 43487e262fSdrh if( op==TK_CAST ){ 4433e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 45fdaac671Sdrh return sqlite3AffinityType(pExpr->u.zToken, 0); 46487e262fSdrh } 47487e262fSdrh #endif 48259a455fSdanielk1977 if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER) 49259a455fSdanielk1977 && pExpr->pTab!=0 50259a455fSdanielk1977 ){ 517d10d5a6Sdrh /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally 527d10d5a6Sdrh ** a TK_COLUMN but was previously evaluated and cached in a register */ 537d10d5a6Sdrh int j = pExpr->iColumn; 547d10d5a6Sdrh if( j<0 ) return SQLITE_AFF_INTEGER; 557d10d5a6Sdrh assert( pExpr->pTab && j<pExpr->pTab->nCol ); 567d10d5a6Sdrh return pExpr->pTab->aCol[j].affinity; 577d10d5a6Sdrh } 58a37cdde0Sdanielk1977 return pExpr->affinity; 59a37cdde0Sdanielk1977 } 60a37cdde0Sdanielk1977 6153db1458Sdrh /* 628b4c40d8Sdrh ** Set the collating sequence for expression pExpr to be the collating 63ae80ddeaSdrh ** sequence named by pToken. Return a pointer to a new Expr node that 64ae80ddeaSdrh ** implements the COLLATE operator. 650a8a406eSdrh ** 660a8a406eSdrh ** If a memory allocation error occurs, that fact is recorded in pParse->db 670a8a406eSdrh ** and the pExpr parameter is returned unchanged. 688b4c40d8Sdrh */ 694ef7efadSdrh Expr *sqlite3ExprAddCollateToken( 704ef7efadSdrh Parse *pParse, /* Parsing context */ 714ef7efadSdrh Expr *pExpr, /* Add the "COLLATE" clause to this expression */ 724ef7efadSdrh const Token *pCollName /* Name of collating sequence */ 734ef7efadSdrh ){ 740a8a406eSdrh if( pCollName->n>0 ){ 75ae80ddeaSdrh Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1); 76ae80ddeaSdrh if( pNew ){ 77ae80ddeaSdrh pNew->pLeft = pExpr; 78a4c3c87eSdrh pNew->flags |= EP_Collate|EP_Skip; 790a8a406eSdrh pExpr = pNew; 80ae80ddeaSdrh } 810a8a406eSdrh } 820a8a406eSdrh return pExpr; 830a8a406eSdrh } 840a8a406eSdrh Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ 850a8a406eSdrh Token s; 86261d8a51Sdrh assert( zC!=0 ); 870a8a406eSdrh s.z = zC; 880a8a406eSdrh s.n = sqlite3Strlen30(s.z); 89261d8a51Sdrh return sqlite3ExprAddCollateToken(pParse, pExpr, &s); 900a8a406eSdrh } 910a8a406eSdrh 920a8a406eSdrh /* 93a4c3c87eSdrh ** Skip over any TK_COLLATE or TK_AS operators and any unlikely() 94a4c3c87eSdrh ** or likelihood() function at the root of an expression. 950a8a406eSdrh */ 960a8a406eSdrh Expr *sqlite3ExprSkipCollate(Expr *pExpr){ 97a4c3c87eSdrh while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){ 98a4c3c87eSdrh if( ExprHasProperty(pExpr, EP_Unlikely) ){ 99cca9f3d2Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 100cca9f3d2Sdrh assert( pExpr->x.pList->nExpr>0 ); 101a4c3c87eSdrh assert( pExpr->op==TK_FUNCTION ); 102cca9f3d2Sdrh pExpr = pExpr->x.pList->a[0].pExpr; 103cca9f3d2Sdrh }else{ 104a4c3c87eSdrh assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS ); 105d91eba96Sdrh pExpr = pExpr->pLeft; 106cca9f3d2Sdrh } 107d91eba96Sdrh } 1080a8a406eSdrh return pExpr; 1098b4c40d8Sdrh } 1108b4c40d8Sdrh 1118b4c40d8Sdrh /* 112ae80ddeaSdrh ** Return the collation sequence for the expression pExpr. If 113ae80ddeaSdrh ** there is no defined collating sequence, return NULL. 114ae80ddeaSdrh ** 115ae80ddeaSdrh ** The collating sequence might be determined by a COLLATE operator 116ae80ddeaSdrh ** or by the presence of a column with a defined collating sequence. 117ae80ddeaSdrh ** COLLATE operators take first precedence. Left operands take 118ae80ddeaSdrh ** precedence over right operands. 1190202b29eSdanielk1977 */ 1207cedc8d4Sdanielk1977 CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){ 121ae80ddeaSdrh sqlite3 *db = pParse->db; 1227cedc8d4Sdanielk1977 CollSeq *pColl = 0; 1237d10d5a6Sdrh Expr *p = pExpr; 124261d8a51Sdrh while( p ){ 125ae80ddeaSdrh int op = p->op; 126fbb24d10Sdrh if( p->flags & EP_Generic ) break; 127ae80ddeaSdrh if( op==TK_CAST || op==TK_UPLUS ){ 128ae80ddeaSdrh p = p->pLeft; 129ae80ddeaSdrh continue; 130ae80ddeaSdrh } 13136e78309Sdan if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){ 1327a66da13Sdrh pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken); 133ae80ddeaSdrh break; 134ae80ddeaSdrh } 135a58d4a96Sdrh if( (op==TK_AGG_COLUMN || op==TK_COLUMN 136ae80ddeaSdrh || op==TK_REGISTER || op==TK_TRIGGER) 137a58d4a96Sdrh && p->pTab!=0 138ae80ddeaSdrh ){ 1397d10d5a6Sdrh /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally 1407d10d5a6Sdrh ** a TK_COLUMN but was previously evaluated and cached in a register */ 1417d10d5a6Sdrh int j = p->iColumn; 1427d10d5a6Sdrh if( j>=0 ){ 143ae80ddeaSdrh const char *zColl = p->pTab->aCol[j].zColl; 144c4a64facSdrh pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); 1450202b29eSdanielk1977 } 1467d10d5a6Sdrh break; 1477d10d5a6Sdrh } 148ae80ddeaSdrh if( p->flags & EP_Collate ){ 149*2308ed38Sdrh if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ 1507d10d5a6Sdrh p = p->pLeft; 151ae80ddeaSdrh }else{ 152*2308ed38Sdrh Expr *pNext = p->pRight; 1536728cd91Sdrh /* The Expr.x union is never used at the same time as Expr.pRight */ 1546728cd91Sdrh assert( p->x.pList==0 || p->pRight==0 ); 1556728cd91Sdrh /* p->flags holds EP_Collate and p->pLeft->flags does not. And 1566728cd91Sdrh ** p->x.pSelect cannot. So if p->x.pLeft exists, it must hold at 1576728cd91Sdrh ** least one EP_Collate. Thus the following two ALWAYS. */ 1586728cd91Sdrh if( p->x.pList!=0 && ALWAYS(!ExprHasProperty(p, EP_xIsSelect)) ){ 159*2308ed38Sdrh int i; 1606728cd91Sdrh for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){ 161*2308ed38Sdrh if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ 162*2308ed38Sdrh pNext = p->x.pList->a[i].pExpr; 163*2308ed38Sdrh break; 164*2308ed38Sdrh } 165*2308ed38Sdrh } 166*2308ed38Sdrh } 167*2308ed38Sdrh p = pNext; 168ae80ddeaSdrh } 169ae80ddeaSdrh }else{ 170ae80ddeaSdrh break; 171ae80ddeaSdrh } 1720202b29eSdanielk1977 } 1737cedc8d4Sdanielk1977 if( sqlite3CheckCollSeq(pParse, pColl) ){ 1747cedc8d4Sdanielk1977 pColl = 0; 1757cedc8d4Sdanielk1977 } 1767cedc8d4Sdanielk1977 return pColl; 1770202b29eSdanielk1977 } 1780202b29eSdanielk1977 1790202b29eSdanielk1977 /* 180626a879aSdrh ** pExpr is an operand of a comparison operator. aff2 is the 181626a879aSdrh ** type affinity of the other operand. This routine returns the 18253db1458Sdrh ** type affinity that should be used for the comparison operator. 18353db1458Sdrh */ 184e014a838Sdanielk1977 char sqlite3CompareAffinity(Expr *pExpr, char aff2){ 185bf3b721fSdanielk1977 char aff1 = sqlite3ExprAffinity(pExpr); 186e014a838Sdanielk1977 if( aff1 && aff2 ){ 1878df447f0Sdrh /* Both sides of the comparison are columns. If one has numeric 1888df447f0Sdrh ** affinity, use that. Otherwise use no affinity. 189e014a838Sdanielk1977 */ 1908a51256cSdrh if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){ 191e014a838Sdanielk1977 return SQLITE_AFF_NUMERIC; 192e014a838Sdanielk1977 }else{ 193e014a838Sdanielk1977 return SQLITE_AFF_NONE; 194e014a838Sdanielk1977 } 195e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 1965f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 1975f6a87b3Sdrh ** results directly. 198e014a838Sdanielk1977 */ 1995f6a87b3Sdrh return SQLITE_AFF_NONE; 200e014a838Sdanielk1977 }else{ 201e014a838Sdanielk1977 /* One side is a column, the other is not. Use the columns affinity. */ 202fe05af87Sdrh assert( aff1==0 || aff2==0 ); 203e014a838Sdanielk1977 return (aff1 + aff2); 204e014a838Sdanielk1977 } 205e014a838Sdanielk1977 } 206e014a838Sdanielk1977 20753db1458Sdrh /* 20853db1458Sdrh ** pExpr is a comparison operator. Return the type affinity that should 20953db1458Sdrh ** be applied to both operands prior to doing the comparison. 21053db1458Sdrh */ 211e014a838Sdanielk1977 static char comparisonAffinity(Expr *pExpr){ 212e014a838Sdanielk1977 char aff; 213e014a838Sdanielk1977 assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT || 214e014a838Sdanielk1977 pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE || 2156a2fe093Sdrh pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT ); 216e014a838Sdanielk1977 assert( pExpr->pLeft ); 217bf3b721fSdanielk1977 aff = sqlite3ExprAffinity(pExpr->pLeft); 218e014a838Sdanielk1977 if( pExpr->pRight ){ 219e014a838Sdanielk1977 aff = sqlite3CompareAffinity(pExpr->pRight, aff); 2206ab3a2ecSdanielk1977 }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 2216ab3a2ecSdanielk1977 aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff); 2226ab3a2ecSdanielk1977 }else if( !aff ){ 223de087bd5Sdrh aff = SQLITE_AFF_NONE; 224e014a838Sdanielk1977 } 225e014a838Sdanielk1977 return aff; 226e014a838Sdanielk1977 } 227e014a838Sdanielk1977 228e014a838Sdanielk1977 /* 229e014a838Sdanielk1977 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc. 230e014a838Sdanielk1977 ** idx_affinity is the affinity of an indexed column. Return true 231e014a838Sdanielk1977 ** if the index with affinity idx_affinity may be used to implement 232e014a838Sdanielk1977 ** the comparison in pExpr. 233e014a838Sdanielk1977 */ 234e014a838Sdanielk1977 int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){ 235e014a838Sdanielk1977 char aff = comparisonAffinity(pExpr); 2368a51256cSdrh switch( aff ){ 2378a51256cSdrh case SQLITE_AFF_NONE: 2388a51256cSdrh return 1; 2398a51256cSdrh case SQLITE_AFF_TEXT: 2408a51256cSdrh return idx_affinity==SQLITE_AFF_TEXT; 2418a51256cSdrh default: 2428a51256cSdrh return sqlite3IsNumericAffinity(idx_affinity); 2438a51256cSdrh } 244e014a838Sdanielk1977 } 245e014a838Sdanielk1977 246a37cdde0Sdanielk1977 /* 24735573356Sdrh ** Return the P5 value that should be used for a binary comparison 248a37cdde0Sdanielk1977 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2. 249a37cdde0Sdanielk1977 */ 25035573356Sdrh static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){ 25135573356Sdrh u8 aff = (char)sqlite3ExprAffinity(pExpr2); 2521bd10f8aSdrh aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull; 25335573356Sdrh return aff; 254a37cdde0Sdanielk1977 } 255a37cdde0Sdanielk1977 256a2e00042Sdrh /* 2570202b29eSdanielk1977 ** Return a pointer to the collation sequence that should be used by 2580202b29eSdanielk1977 ** a binary comparison operator comparing pLeft and pRight. 2590202b29eSdanielk1977 ** 2600202b29eSdanielk1977 ** If the left hand expression has a collating sequence type, then it is 2610202b29eSdanielk1977 ** used. Otherwise the collation sequence for the right hand expression 2620202b29eSdanielk1977 ** is used, or the default (BINARY) if neither expression has a collating 2630202b29eSdanielk1977 ** type. 264bcbb04e5Sdanielk1977 ** 265bcbb04e5Sdanielk1977 ** Argument pRight (but not pLeft) may be a null pointer. In this case, 266bcbb04e5Sdanielk1977 ** it is not considered. 2670202b29eSdanielk1977 */ 268bcbb04e5Sdanielk1977 CollSeq *sqlite3BinaryCompareCollSeq( 269bcbb04e5Sdanielk1977 Parse *pParse, 270bcbb04e5Sdanielk1977 Expr *pLeft, 271bcbb04e5Sdanielk1977 Expr *pRight 272bcbb04e5Sdanielk1977 ){ 273ec41ddacSdrh CollSeq *pColl; 274ec41ddacSdrh assert( pLeft ); 275ae80ddeaSdrh if( pLeft->flags & EP_Collate ){ 276ae80ddeaSdrh pColl = sqlite3ExprCollSeq(pParse, pLeft); 277ae80ddeaSdrh }else if( pRight && (pRight->flags & EP_Collate)!=0 ){ 278ae80ddeaSdrh pColl = sqlite3ExprCollSeq(pParse, pRight); 279ec41ddacSdrh }else{ 280ec41ddacSdrh pColl = sqlite3ExprCollSeq(pParse, pLeft); 2810202b29eSdanielk1977 if( !pColl ){ 2827cedc8d4Sdanielk1977 pColl = sqlite3ExprCollSeq(pParse, pRight); 2830202b29eSdanielk1977 } 284ec41ddacSdrh } 2850202b29eSdanielk1977 return pColl; 2860202b29eSdanielk1977 } 2870202b29eSdanielk1977 2880202b29eSdanielk1977 /* 289be5c89acSdrh ** Generate code for a comparison operator. 290be5c89acSdrh */ 291be5c89acSdrh static int codeCompare( 292be5c89acSdrh Parse *pParse, /* The parsing (and code generating) context */ 293be5c89acSdrh Expr *pLeft, /* The left operand */ 294be5c89acSdrh Expr *pRight, /* The right operand */ 295be5c89acSdrh int opcode, /* The comparison opcode */ 29635573356Sdrh int in1, int in2, /* Register holding operands */ 297be5c89acSdrh int dest, /* Jump here if true. */ 298be5c89acSdrh int jumpIfNull /* If true, jump if either operand is NULL */ 299be5c89acSdrh ){ 30035573356Sdrh int p5; 30135573356Sdrh int addr; 30235573356Sdrh CollSeq *p4; 30335573356Sdrh 30435573356Sdrh p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight); 30535573356Sdrh p5 = binaryCompareP5(pLeft, pRight, jumpIfNull); 30635573356Sdrh addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1, 30735573356Sdrh (void*)p4, P4_COLLSEQ); 3081bd10f8aSdrh sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5); 30935573356Sdrh return addr; 310be5c89acSdrh } 311be5c89acSdrh 3124b5255acSdanielk1977 #if SQLITE_MAX_EXPR_DEPTH>0 3134b5255acSdanielk1977 /* 3144b5255acSdanielk1977 ** Check that argument nHeight is less than or equal to the maximum 3154b5255acSdanielk1977 ** expression depth allowed. If it is not, leave an error message in 3164b5255acSdanielk1977 ** pParse. 3174b5255acSdanielk1977 */ 3187d10d5a6Sdrh int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ 3194b5255acSdanielk1977 int rc = SQLITE_OK; 3204b5255acSdanielk1977 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 3214b5255acSdanielk1977 if( nHeight>mxHeight ){ 3224b5255acSdanielk1977 sqlite3ErrorMsg(pParse, 3234b5255acSdanielk1977 "Expression tree is too large (maximum depth %d)", mxHeight 3244b5255acSdanielk1977 ); 3254b5255acSdanielk1977 rc = SQLITE_ERROR; 3264b5255acSdanielk1977 } 3274b5255acSdanielk1977 return rc; 3284b5255acSdanielk1977 } 3294b5255acSdanielk1977 3304b5255acSdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 3314b5255acSdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 3324b5255acSdanielk1977 ** of any expression tree referenced by the structure passed as the 3334b5255acSdanielk1977 ** first argument. 3344b5255acSdanielk1977 ** 3354b5255acSdanielk1977 ** If this maximum height is greater than the current value pointed 3364b5255acSdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 3374b5255acSdanielk1977 ** value. 3384b5255acSdanielk1977 */ 3394b5255acSdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 3404b5255acSdanielk1977 if( p ){ 3414b5255acSdanielk1977 if( p->nHeight>*pnHeight ){ 3424b5255acSdanielk1977 *pnHeight = p->nHeight; 3434b5255acSdanielk1977 } 3444b5255acSdanielk1977 } 3454b5255acSdanielk1977 } 3464b5255acSdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 3474b5255acSdanielk1977 if( p ){ 3484b5255acSdanielk1977 int i; 3494b5255acSdanielk1977 for(i=0; i<p->nExpr; i++){ 3504b5255acSdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 3514b5255acSdanielk1977 } 3524b5255acSdanielk1977 } 3534b5255acSdanielk1977 } 3544b5255acSdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 3554b5255acSdanielk1977 if( p ){ 3564b5255acSdanielk1977 heightOfExpr(p->pWhere, pnHeight); 3574b5255acSdanielk1977 heightOfExpr(p->pHaving, pnHeight); 3584b5255acSdanielk1977 heightOfExpr(p->pLimit, pnHeight); 3594b5255acSdanielk1977 heightOfExpr(p->pOffset, pnHeight); 3604b5255acSdanielk1977 heightOfExprList(p->pEList, pnHeight); 3614b5255acSdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 3624b5255acSdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 3634b5255acSdanielk1977 heightOfSelect(p->pPrior, pnHeight); 3644b5255acSdanielk1977 } 3654b5255acSdanielk1977 } 3664b5255acSdanielk1977 3674b5255acSdanielk1977 /* 3684b5255acSdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 3694b5255acSdanielk1977 ** argument. An expression with no children, Expr.pList or 3704b5255acSdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 3714b5255acSdanielk1977 ** has a height equal to the maximum height of any other 3724b5255acSdanielk1977 ** referenced Expr plus one. 373*2308ed38Sdrh ** 374*2308ed38Sdrh ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, 375*2308ed38Sdrh ** if appropriate. 3764b5255acSdanielk1977 */ 3774b5255acSdanielk1977 static void exprSetHeight(Expr *p){ 3784b5255acSdanielk1977 int nHeight = 0; 3794b5255acSdanielk1977 heightOfExpr(p->pLeft, &nHeight); 3804b5255acSdanielk1977 heightOfExpr(p->pRight, &nHeight); 3816ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 3826ab3a2ecSdanielk1977 heightOfSelect(p->x.pSelect, &nHeight); 383*2308ed38Sdrh }else if( p->x.pList ){ 3846ab3a2ecSdanielk1977 heightOfExprList(p->x.pList, &nHeight); 385*2308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 3866ab3a2ecSdanielk1977 } 3874b5255acSdanielk1977 p->nHeight = nHeight + 1; 3884b5255acSdanielk1977 } 3894b5255acSdanielk1977 3904b5255acSdanielk1977 /* 3914b5255acSdanielk1977 ** Set the Expr.nHeight variable using the exprSetHeight() function. If 3924b5255acSdanielk1977 ** the height is greater than the maximum allowed expression depth, 3934b5255acSdanielk1977 ** leave an error in pParse. 394*2308ed38Sdrh ** 395*2308ed38Sdrh ** Also propagate all EP_Propagate flags from the Expr.x.pList into 396*2308ed38Sdrh ** Expr.flags. 3974b5255acSdanielk1977 */ 398*2308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 3994b5255acSdanielk1977 exprSetHeight(p); 4007d10d5a6Sdrh sqlite3ExprCheckHeight(pParse, p->nHeight); 4014b5255acSdanielk1977 } 4024b5255acSdanielk1977 4034b5255acSdanielk1977 /* 4044b5255acSdanielk1977 ** Return the maximum height of any expression tree referenced 4054b5255acSdanielk1977 ** by the select statement passed as an argument. 4064b5255acSdanielk1977 */ 4074b5255acSdanielk1977 int sqlite3SelectExprHeight(Select *p){ 4084b5255acSdanielk1977 int nHeight = 0; 4094b5255acSdanielk1977 heightOfSelect(p, &nHeight); 4104b5255acSdanielk1977 return nHeight; 4114b5255acSdanielk1977 } 412*2308ed38Sdrh #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ 413*2308ed38Sdrh /* 414*2308ed38Sdrh ** Propagate all EP_Propagate flags from the Expr.x.pList into 415*2308ed38Sdrh ** Expr.flags. 416*2308ed38Sdrh */ 417*2308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 418*2308ed38Sdrh if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ 419*2308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 420*2308ed38Sdrh } 421*2308ed38Sdrh } 4224b5255acSdanielk1977 #define exprSetHeight(y) 4234b5255acSdanielk1977 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 4244b5255acSdanielk1977 425be5c89acSdrh /* 426b7916a78Sdrh ** This routine is the core allocator for Expr nodes. 427b7916a78Sdrh ** 428a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 429b7916a78Sdrh ** for this node and for the pToken argument is a single allocation 430b7916a78Sdrh ** obtained from sqlite3DbMalloc(). The calling function 431a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 432b7916a78Sdrh ** 433b7916a78Sdrh ** If dequote is true, then the token (if it exists) is dequoted. 434b7916a78Sdrh ** If dequote is false, no dequoting is performance. The deQuote 435b7916a78Sdrh ** parameter is ignored if pToken is NULL or if the token does not 436b7916a78Sdrh ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 437b7916a78Sdrh ** then the EP_DblQuoted flag is set on the expression node. 43833e619fcSdrh ** 43933e619fcSdrh ** Special case: If op==TK_INTEGER and pToken points to a string that 44033e619fcSdrh ** can be translated into a 32-bit integer, then the token is not 44133e619fcSdrh ** stored in u.zToken. Instead, the integer values is written 44233e619fcSdrh ** into u.iValue and the EP_IntValue flag is set. No extra storage 44333e619fcSdrh ** is allocated to hold the integer text and the dequote flag is ignored. 444a76b5dfcSdrh */ 445b7916a78Sdrh Expr *sqlite3ExprAlloc( 446a1644fd8Sdanielk1977 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 44717435752Sdrh int op, /* Expression opcode */ 448b7916a78Sdrh const Token *pToken, /* Token argument. Might be NULL */ 449b7916a78Sdrh int dequote /* True to dequote */ 45017435752Sdrh ){ 451a76b5dfcSdrh Expr *pNew; 45233e619fcSdrh int nExtra = 0; 453cf697396Sshane int iValue = 0; 454b7916a78Sdrh 455b7916a78Sdrh if( pToken ){ 45633e619fcSdrh if( op!=TK_INTEGER || pToken->z==0 45733e619fcSdrh || sqlite3GetInt32(pToken->z, &iValue)==0 ){ 458b7916a78Sdrh nExtra = pToken->n+1; 459d50ffc41Sdrh assert( iValue>=0 ); 46033e619fcSdrh } 461a76b5dfcSdrh } 462b7916a78Sdrh pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra); 463b7916a78Sdrh if( pNew ){ 4641bd10f8aSdrh pNew->op = (u8)op; 465a58fdfb1Sdanielk1977 pNew->iAgg = -1; 466a76b5dfcSdrh if( pToken ){ 46733e619fcSdrh if( nExtra==0 ){ 46833e619fcSdrh pNew->flags |= EP_IntValue; 46933e619fcSdrh pNew->u.iValue = iValue; 47033e619fcSdrh }else{ 471d9da78a2Sdrh int c; 47233e619fcSdrh pNew->u.zToken = (char*)&pNew[1]; 473b07028f7Sdrh assert( pToken->z!=0 || pToken->n==0 ); 474b07028f7Sdrh if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); 47533e619fcSdrh pNew->u.zToken[pToken->n] = 0; 476b7916a78Sdrh if( dequote && nExtra>=3 477d9da78a2Sdrh && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ 47833e619fcSdrh sqlite3Dequote(pNew->u.zToken); 47924fb627aSdrh if( c=='"' ) pNew->flags |= EP_DblQuoted; 480a34001c9Sdrh } 481a34001c9Sdrh } 48233e619fcSdrh } 483b7916a78Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 484b7916a78Sdrh pNew->nHeight = 1; 485b7916a78Sdrh #endif 486a34001c9Sdrh } 487a76b5dfcSdrh return pNew; 488a76b5dfcSdrh } 489a76b5dfcSdrh 490a76b5dfcSdrh /* 491b7916a78Sdrh ** Allocate a new expression node from a zero-terminated token that has 492b7916a78Sdrh ** already been dequoted. 493b7916a78Sdrh */ 494b7916a78Sdrh Expr *sqlite3Expr( 495b7916a78Sdrh sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 496b7916a78Sdrh int op, /* Expression opcode */ 497b7916a78Sdrh const char *zToken /* Token argument. Might be NULL */ 498b7916a78Sdrh ){ 499b7916a78Sdrh Token x; 500b7916a78Sdrh x.z = zToken; 501b7916a78Sdrh x.n = zToken ? sqlite3Strlen30(zToken) : 0; 502b7916a78Sdrh return sqlite3ExprAlloc(db, op, &x, 0); 503b7916a78Sdrh } 504b7916a78Sdrh 505b7916a78Sdrh /* 506b7916a78Sdrh ** Attach subtrees pLeft and pRight to the Expr node pRoot. 507b7916a78Sdrh ** 508b7916a78Sdrh ** If pRoot==NULL that means that a memory allocation error has occurred. 509b7916a78Sdrh ** In that case, delete the subtrees pLeft and pRight. 510b7916a78Sdrh */ 511b7916a78Sdrh void sqlite3ExprAttachSubtrees( 512b7916a78Sdrh sqlite3 *db, 513b7916a78Sdrh Expr *pRoot, 514b7916a78Sdrh Expr *pLeft, 515b7916a78Sdrh Expr *pRight 516b7916a78Sdrh ){ 517b7916a78Sdrh if( pRoot==0 ){ 518b7916a78Sdrh assert( db->mallocFailed ); 519b7916a78Sdrh sqlite3ExprDelete(db, pLeft); 520b7916a78Sdrh sqlite3ExprDelete(db, pRight); 521b7916a78Sdrh }else{ 522b7916a78Sdrh if( pRight ){ 523b7916a78Sdrh pRoot->pRight = pRight; 524885a5b03Sdrh pRoot->flags |= EP_Propagate & pRight->flags; 525b7916a78Sdrh } 526b7916a78Sdrh if( pLeft ){ 527b7916a78Sdrh pRoot->pLeft = pLeft; 528885a5b03Sdrh pRoot->flags |= EP_Propagate & pLeft->flags; 529b7916a78Sdrh } 530b7916a78Sdrh exprSetHeight(pRoot); 531b7916a78Sdrh } 532b7916a78Sdrh } 533b7916a78Sdrh 534b7916a78Sdrh /* 53560ec914cSpeter.d.reid ** Allocate an Expr node which joins as many as two subtrees. 536b7916a78Sdrh ** 537bf664469Sdrh ** One or both of the subtrees can be NULL. Return a pointer to the new 538bf664469Sdrh ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 539bf664469Sdrh ** free the subtrees and return NULL. 540206f3d96Sdrh */ 54117435752Sdrh Expr *sqlite3PExpr( 54217435752Sdrh Parse *pParse, /* Parsing context */ 54317435752Sdrh int op, /* Expression opcode */ 54417435752Sdrh Expr *pLeft, /* Left operand */ 54517435752Sdrh Expr *pRight, /* Right operand */ 54617435752Sdrh const Token *pToken /* Argument token */ 54717435752Sdrh ){ 5485fb52caaSdrh Expr *p; 549655814d2Sdrh if( op==TK_AND && pLeft && pRight && pParse->nErr==0 ){ 5505fb52caaSdrh /* Take advantage of short-circuit false optimization for AND */ 5515fb52caaSdrh p = sqlite3ExprAnd(pParse->db, pLeft, pRight); 5525fb52caaSdrh }else{ 5535fb52caaSdrh p = sqlite3ExprAlloc(pParse->db, op, pToken, 1); 554b7916a78Sdrh sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 5555fb52caaSdrh } 5562b359bdbSdan if( p ) { 5572b359bdbSdan sqlite3ExprCheckHeight(pParse, p->nHeight); 5582b359bdbSdan } 5594e0cff60Sdrh return p; 5604e0cff60Sdrh } 5614e0cff60Sdrh 5624e0cff60Sdrh /* 563991a1985Sdrh ** If the expression is always either TRUE or FALSE (respectively), 564991a1985Sdrh ** then return 1. If one cannot determine the truth value of the 565991a1985Sdrh ** expression at compile-time return 0. 566991a1985Sdrh ** 567991a1985Sdrh ** This is an optimization. If is OK to return 0 here even if 568991a1985Sdrh ** the expression really is always false or false (a false negative). 569991a1985Sdrh ** But it is a bug to return 1 if the expression might have different 570991a1985Sdrh ** boolean values in different circumstances (a false positive.) 5715fb52caaSdrh ** 5725fb52caaSdrh ** Note that if the expression is part of conditional for a 5735fb52caaSdrh ** LEFT JOIN, then we cannot determine at compile-time whether or not 5745fb52caaSdrh ** is it true or false, so always return 0. 5755fb52caaSdrh */ 576991a1985Sdrh static int exprAlwaysTrue(Expr *p){ 577991a1985Sdrh int v = 0; 578991a1985Sdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 579991a1985Sdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 580991a1985Sdrh return v!=0; 581991a1985Sdrh } 5825fb52caaSdrh static int exprAlwaysFalse(Expr *p){ 5835fb52caaSdrh int v = 0; 5845fb52caaSdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 5855fb52caaSdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 5865fb52caaSdrh return v==0; 5875fb52caaSdrh } 5885fb52caaSdrh 5895fb52caaSdrh /* 59091bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 59191bb0eedSdrh ** NULL, then just return the other expression. 5925fb52caaSdrh ** 5935fb52caaSdrh ** If one side or the other of the AND is known to be false, then instead 5945fb52caaSdrh ** of returning an AND expression, just return a constant expression with 5955fb52caaSdrh ** a value of false. 59691bb0eedSdrh */ 5971e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 59891bb0eedSdrh if( pLeft==0 ){ 59991bb0eedSdrh return pRight; 60091bb0eedSdrh }else if( pRight==0 ){ 60191bb0eedSdrh return pLeft; 6025fb52caaSdrh }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ 6035fb52caaSdrh sqlite3ExprDelete(db, pLeft); 6045fb52caaSdrh sqlite3ExprDelete(db, pRight); 6055fb52caaSdrh return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); 60691bb0eedSdrh }else{ 607b7916a78Sdrh Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); 608b7916a78Sdrh sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); 609b7916a78Sdrh return pNew; 610a76b5dfcSdrh } 611a76b5dfcSdrh } 612a76b5dfcSdrh 613a76b5dfcSdrh /* 614a76b5dfcSdrh ** Construct a new expression node for a function with multiple 615a76b5dfcSdrh ** arguments. 616a76b5dfcSdrh */ 61717435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 618a76b5dfcSdrh Expr *pNew; 619633e6d57Sdrh sqlite3 *db = pParse->db; 6204b202ae2Sdanielk1977 assert( pToken ); 621b7916a78Sdrh pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 622a76b5dfcSdrh if( pNew==0 ){ 623d9da78a2Sdrh sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 624a76b5dfcSdrh return 0; 625a76b5dfcSdrh } 6266ab3a2ecSdanielk1977 pNew->x.pList = pList; 6276ab3a2ecSdanielk1977 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 628*2308ed38Sdrh sqlite3ExprSetHeightAndFlags(pParse, pNew); 629a76b5dfcSdrh return pNew; 630a76b5dfcSdrh } 631a76b5dfcSdrh 632a76b5dfcSdrh /* 633fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 634fa6bc000Sdrh ** in the original SQL statement. 635fa6bc000Sdrh ** 636fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 637fa6bc000Sdrh ** variable number. 638fa6bc000Sdrh ** 639fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 640fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 641fa6bc000Sdrh ** the SQL statement comes from an external source. 642fa6bc000Sdrh ** 64351f49f17Sdrh ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number 644fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 64560ec914cSpeter.d.reid ** instance of the wildcard, the next sequential variable number is 646fa6bc000Sdrh ** assigned. 647fa6bc000Sdrh */ 648fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 64917435752Sdrh sqlite3 *db = pParse->db; 650b7916a78Sdrh const char *z; 65117435752Sdrh 652fa6bc000Sdrh if( pExpr==0 ) return; 653c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); 65433e619fcSdrh z = pExpr->u.zToken; 655b7916a78Sdrh assert( z!=0 ); 656b7916a78Sdrh assert( z[0]!=0 ); 657b7916a78Sdrh if( z[1]==0 ){ 658fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 659b7916a78Sdrh assert( z[0]=='?' ); 6608677d308Sdrh pExpr->iColumn = (ynVar)(++pParse->nVar); 661124c0b49Sdrh }else{ 662124c0b49Sdrh ynVar x = 0; 663124c0b49Sdrh u32 n = sqlite3Strlen30(z); 664124c0b49Sdrh if( z[0]=='?' ){ 665fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 666fa6bc000Sdrh ** use it as the variable number */ 667c8d735aeSdan i64 i; 668124c0b49Sdrh int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); 669124c0b49Sdrh pExpr->iColumn = x = (ynVar)i; 670c5499befSdrh testcase( i==0 ); 671c5499befSdrh testcase( i==1 ); 672c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 673c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 674c8d735aeSdan if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 675fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 676bb4957f8Sdrh db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 677124c0b49Sdrh x = 0; 678fa6bc000Sdrh } 679fa6bc000Sdrh if( i>pParse->nVar ){ 6801df2db7fSshaneh pParse->nVar = (int)i; 681fa6bc000Sdrh } 682fa6bc000Sdrh }else{ 68351f49f17Sdrh /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable 684fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 685fa6bc000Sdrh ** has never appeared before, reuse the same variable number 686fa6bc000Sdrh */ 687124c0b49Sdrh ynVar i; 688124c0b49Sdrh for(i=0; i<pParse->nzVar; i++){ 689503a686eSdrh if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ 690124c0b49Sdrh pExpr->iColumn = x = (ynVar)i+1; 691fa6bc000Sdrh break; 692fa6bc000Sdrh } 693fa6bc000Sdrh } 694124c0b49Sdrh if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); 695fa6bc000Sdrh } 696124c0b49Sdrh if( x>0 ){ 697124c0b49Sdrh if( x>pParse->nzVar ){ 698124c0b49Sdrh char **a; 699124c0b49Sdrh a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); 700124c0b49Sdrh if( a==0 ) return; /* Error reported through db->mallocFailed */ 701124c0b49Sdrh pParse->azVar = a; 702124c0b49Sdrh memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); 703124c0b49Sdrh pParse->nzVar = x; 704124c0b49Sdrh } 705124c0b49Sdrh if( z[0]!='?' || pParse->azVar[x-1]==0 ){ 706124c0b49Sdrh sqlite3DbFree(db, pParse->azVar[x-1]); 707124c0b49Sdrh pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); 708fa6bc000Sdrh } 709fa6bc000Sdrh } 710fa6bc000Sdrh } 711bb4957f8Sdrh if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 712832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 713832b2664Sdanielk1977 } 714fa6bc000Sdrh } 715fa6bc000Sdrh 716fa6bc000Sdrh /* 717f6963f99Sdan ** Recursively delete an expression tree. 718a2e00042Sdrh */ 719f6963f99Sdan void sqlite3ExprDelete(sqlite3 *db, Expr *p){ 720f6963f99Sdan if( p==0 ) return; 721d50ffc41Sdrh /* Sanity check: Assert that the IntValue is non-negative if it exists */ 722d50ffc41Sdrh assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); 723c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 724c5cd1249Sdrh /* The Expr.x union is never used at the same time as Expr.pRight */ 725c5cd1249Sdrh assert( p->x.pList==0 || p->pRight==0 ); 726633e6d57Sdrh sqlite3ExprDelete(db, p->pLeft); 727633e6d57Sdrh sqlite3ExprDelete(db, p->pRight); 728c5cd1249Sdrh if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); 7296ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 7306ab3a2ecSdanielk1977 sqlite3SelectDelete(db, p->x.pSelect); 7316ab3a2ecSdanielk1977 }else{ 7326ab3a2ecSdanielk1977 sqlite3ExprListDelete(db, p->x.pList); 7336ab3a2ecSdanielk1977 } 7346ab3a2ecSdanielk1977 } 73533e619fcSdrh if( !ExprHasProperty(p, EP_Static) ){ 736633e6d57Sdrh sqlite3DbFree(db, p); 737a2e00042Sdrh } 73833e619fcSdrh } 739a2e00042Sdrh 740d2687b77Sdrh /* 7416ab3a2ecSdanielk1977 ** Return the number of bytes allocated for the expression structure 7426ab3a2ecSdanielk1977 ** passed as the first argument. This is always one of EXPR_FULLSIZE, 7436ab3a2ecSdanielk1977 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. 7446ab3a2ecSdanielk1977 */ 7456ab3a2ecSdanielk1977 static int exprStructSize(Expr *p){ 7466ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; 7476ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; 7486ab3a2ecSdanielk1977 return EXPR_FULLSIZE; 7496ab3a2ecSdanielk1977 } 7506ab3a2ecSdanielk1977 7516ab3a2ecSdanielk1977 /* 75233e619fcSdrh ** The dupedExpr*Size() routines each return the number of bytes required 75333e619fcSdrh ** to store a copy of an expression or expression tree. They differ in 75433e619fcSdrh ** how much of the tree is measured. 75533e619fcSdrh ** 75633e619fcSdrh ** dupedExprStructSize() Size of only the Expr structure 75733e619fcSdrh ** dupedExprNodeSize() Size of Expr + space for token 75833e619fcSdrh ** dupedExprSize() Expr + token + subtree components 75933e619fcSdrh ** 76033e619fcSdrh *************************************************************************** 76133e619fcSdrh ** 76233e619fcSdrh ** The dupedExprStructSize() function returns two values OR-ed together: 76333e619fcSdrh ** (1) the space required for a copy of the Expr structure only and 76433e619fcSdrh ** (2) the EP_xxx flags that indicate what the structure size should be. 76533e619fcSdrh ** The return values is always one of: 76633e619fcSdrh ** 76733e619fcSdrh ** EXPR_FULLSIZE 76833e619fcSdrh ** EXPR_REDUCEDSIZE | EP_Reduced 76933e619fcSdrh ** EXPR_TOKENONLYSIZE | EP_TokenOnly 77033e619fcSdrh ** 77133e619fcSdrh ** The size of the structure can be found by masking the return value 77233e619fcSdrh ** of this routine with 0xfff. The flags can be found by masking the 77333e619fcSdrh ** return value with EP_Reduced|EP_TokenOnly. 77433e619fcSdrh ** 77533e619fcSdrh ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size 77633e619fcSdrh ** (unreduced) Expr objects as they or originally constructed by the parser. 77733e619fcSdrh ** During expression analysis, extra information is computed and moved into 77833e619fcSdrh ** later parts of teh Expr object and that extra information might get chopped 77933e619fcSdrh ** off if the expression is reduced. Note also that it does not work to 78060ec914cSpeter.d.reid ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal 78133e619fcSdrh ** to reduce a pristine expression tree from the parser. The implementation 78233e619fcSdrh ** of dupedExprStructSize() contain multiple assert() statements that attempt 78333e619fcSdrh ** to enforce this constraint. 7846ab3a2ecSdanielk1977 */ 7856ab3a2ecSdanielk1977 static int dupedExprStructSize(Expr *p, int flags){ 7866ab3a2ecSdanielk1977 int nSize; 78733e619fcSdrh assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ 788aecd8021Sdrh assert( EXPR_FULLSIZE<=0xfff ); 789aecd8021Sdrh assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); 7906ab3a2ecSdanielk1977 if( 0==(flags&EXPRDUP_REDUCE) ){ 7916ab3a2ecSdanielk1977 nSize = EXPR_FULLSIZE; 7926ab3a2ecSdanielk1977 }else{ 793c5cd1249Sdrh assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); 79433e619fcSdrh assert( !ExprHasProperty(p, EP_FromJoin) ); 795c5cd1249Sdrh assert( !ExprHasProperty(p, EP_MemToken) ); 796ebb6a65dSdrh assert( !ExprHasProperty(p, EP_NoReduce) ); 797aecd8021Sdrh if( p->pLeft || p->x.pList ){ 79833e619fcSdrh nSize = EXPR_REDUCEDSIZE | EP_Reduced; 79933e619fcSdrh }else{ 800aecd8021Sdrh assert( p->pRight==0 ); 80133e619fcSdrh nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; 80233e619fcSdrh } 8036ab3a2ecSdanielk1977 } 8046ab3a2ecSdanielk1977 return nSize; 8056ab3a2ecSdanielk1977 } 8066ab3a2ecSdanielk1977 8076ab3a2ecSdanielk1977 /* 80833e619fcSdrh ** This function returns the space in bytes required to store the copy 80933e619fcSdrh ** of the Expr structure and a copy of the Expr.u.zToken string (if that 81033e619fcSdrh ** string is defined.) 8116ab3a2ecSdanielk1977 */ 8126ab3a2ecSdanielk1977 static int dupedExprNodeSize(Expr *p, int flags){ 81333e619fcSdrh int nByte = dupedExprStructSize(p, flags) & 0xfff; 81433e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 81533e619fcSdrh nByte += sqlite3Strlen30(p->u.zToken)+1; 8166ab3a2ecSdanielk1977 } 817bc73971dSdanielk1977 return ROUND8(nByte); 8186ab3a2ecSdanielk1977 } 8196ab3a2ecSdanielk1977 8206ab3a2ecSdanielk1977 /* 8216ab3a2ecSdanielk1977 ** Return the number of bytes required to create a duplicate of the 8226ab3a2ecSdanielk1977 ** expression passed as the first argument. The second argument is a 8236ab3a2ecSdanielk1977 ** mask containing EXPRDUP_XXX flags. 8246ab3a2ecSdanielk1977 ** 8256ab3a2ecSdanielk1977 ** The value returned includes space to create a copy of the Expr struct 82633e619fcSdrh ** itself and the buffer referred to by Expr.u.zToken, if any. 8276ab3a2ecSdanielk1977 ** 8286ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 8296ab3a2ecSdanielk1977 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 8306ab3a2ecSdanielk1977 ** and Expr.pRight variables (but not for any structures pointed to or 8316ab3a2ecSdanielk1977 ** descended from the Expr.x.pList or Expr.x.pSelect variables). 8326ab3a2ecSdanielk1977 */ 8336ab3a2ecSdanielk1977 static int dupedExprSize(Expr *p, int flags){ 8346ab3a2ecSdanielk1977 int nByte = 0; 8356ab3a2ecSdanielk1977 if( p ){ 8366ab3a2ecSdanielk1977 nByte = dupedExprNodeSize(p, flags); 8376ab3a2ecSdanielk1977 if( flags&EXPRDUP_REDUCE ){ 838b7916a78Sdrh nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); 8396ab3a2ecSdanielk1977 } 8406ab3a2ecSdanielk1977 } 8416ab3a2ecSdanielk1977 return nByte; 8426ab3a2ecSdanielk1977 } 8436ab3a2ecSdanielk1977 8446ab3a2ecSdanielk1977 /* 8456ab3a2ecSdanielk1977 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 8466ab3a2ecSdanielk1977 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 84733e619fcSdrh ** to store the copy of expression p, the copies of p->u.zToken 8486ab3a2ecSdanielk1977 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, 84960ec914cSpeter.d.reid ** if any. Before returning, *pzBuffer is set to the first byte past the 8506ab3a2ecSdanielk1977 ** portion of the buffer copied into by this function. 8516ab3a2ecSdanielk1977 */ 8526ab3a2ecSdanielk1977 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ 8536ab3a2ecSdanielk1977 Expr *pNew = 0; /* Value to return */ 8546ab3a2ecSdanielk1977 if( p ){ 8556ab3a2ecSdanielk1977 const int isReduced = (flags&EXPRDUP_REDUCE); 8566ab3a2ecSdanielk1977 u8 *zAlloc; 85733e619fcSdrh u32 staticFlag = 0; 8586ab3a2ecSdanielk1977 8596ab3a2ecSdanielk1977 assert( pzBuffer==0 || isReduced ); 8606ab3a2ecSdanielk1977 8616ab3a2ecSdanielk1977 /* Figure out where to write the new Expr structure. */ 8626ab3a2ecSdanielk1977 if( pzBuffer ){ 8636ab3a2ecSdanielk1977 zAlloc = *pzBuffer; 86433e619fcSdrh staticFlag = EP_Static; 8656ab3a2ecSdanielk1977 }else{ 8666ab3a2ecSdanielk1977 zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags)); 8676ab3a2ecSdanielk1977 } 8686ab3a2ecSdanielk1977 pNew = (Expr *)zAlloc; 8696ab3a2ecSdanielk1977 8706ab3a2ecSdanielk1977 if( pNew ){ 8716ab3a2ecSdanielk1977 /* Set nNewSize to the size allocated for the structure pointed to 8726ab3a2ecSdanielk1977 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or 8736ab3a2ecSdanielk1977 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed 87433e619fcSdrh ** by the copy of the p->u.zToken string (if any). 8756ab3a2ecSdanielk1977 */ 87633e619fcSdrh const unsigned nStructSize = dupedExprStructSize(p, flags); 87733e619fcSdrh const int nNewSize = nStructSize & 0xfff; 87833e619fcSdrh int nToken; 87933e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 88033e619fcSdrh nToken = sqlite3Strlen30(p->u.zToken) + 1; 88133e619fcSdrh }else{ 88233e619fcSdrh nToken = 0; 88333e619fcSdrh } 8846ab3a2ecSdanielk1977 if( isReduced ){ 8856ab3a2ecSdanielk1977 assert( ExprHasProperty(p, EP_Reduced)==0 ); 8866ab3a2ecSdanielk1977 memcpy(zAlloc, p, nNewSize); 8876ab3a2ecSdanielk1977 }else{ 8886ab3a2ecSdanielk1977 int nSize = exprStructSize(p); 8896ab3a2ecSdanielk1977 memcpy(zAlloc, p, nSize); 8906ab3a2ecSdanielk1977 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 8916ab3a2ecSdanielk1977 } 8926ab3a2ecSdanielk1977 89333e619fcSdrh /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 894c5cd1249Sdrh pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); 89533e619fcSdrh pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 89633e619fcSdrh pNew->flags |= staticFlag; 8976ab3a2ecSdanielk1977 89833e619fcSdrh /* Copy the p->u.zToken string, if any. */ 8996ab3a2ecSdanielk1977 if( nToken ){ 90033e619fcSdrh char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; 90133e619fcSdrh memcpy(zToken, p->u.zToken, nToken); 9026ab3a2ecSdanielk1977 } 9036ab3a2ecSdanielk1977 9046ab3a2ecSdanielk1977 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ 9056ab3a2ecSdanielk1977 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ 9066ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 9076ab3a2ecSdanielk1977 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced); 9086ab3a2ecSdanielk1977 }else{ 9096ab3a2ecSdanielk1977 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced); 9106ab3a2ecSdanielk1977 } 9116ab3a2ecSdanielk1977 } 9126ab3a2ecSdanielk1977 9136ab3a2ecSdanielk1977 /* Fill in pNew->pLeft and pNew->pRight. */ 914c5cd1249Sdrh if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ 9156ab3a2ecSdanielk1977 zAlloc += dupedExprNodeSize(p, flags); 9166ab3a2ecSdanielk1977 if( ExprHasProperty(pNew, EP_Reduced) ){ 9176ab3a2ecSdanielk1977 pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc); 9186ab3a2ecSdanielk1977 pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc); 9196ab3a2ecSdanielk1977 } 9206ab3a2ecSdanielk1977 if( pzBuffer ){ 9216ab3a2ecSdanielk1977 *pzBuffer = zAlloc; 9226ab3a2ecSdanielk1977 } 923b7916a78Sdrh }else{ 924c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 9256ab3a2ecSdanielk1977 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); 9266ab3a2ecSdanielk1977 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); 9276ab3a2ecSdanielk1977 } 9286ab3a2ecSdanielk1977 } 929b7916a78Sdrh 930b7916a78Sdrh } 9316ab3a2ecSdanielk1977 } 9326ab3a2ecSdanielk1977 return pNew; 9336ab3a2ecSdanielk1977 } 9346ab3a2ecSdanielk1977 935bfe31e7fSdan /* 936bfe31e7fSdan ** Create and return a deep copy of the object passed as the second 937bfe31e7fSdan ** argument. If an OOM condition is encountered, NULL is returned 938bfe31e7fSdan ** and the db->mallocFailed flag set. 939bfe31e7fSdan */ 940eede6a53Sdan #ifndef SQLITE_OMIT_CTE 941bfe31e7fSdan static With *withDup(sqlite3 *db, With *p){ 9424e9119d9Sdan With *pRet = 0; 9434e9119d9Sdan if( p ){ 9444e9119d9Sdan int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); 9454e9119d9Sdan pRet = sqlite3DbMallocZero(db, nByte); 9464e9119d9Sdan if( pRet ){ 9474e9119d9Sdan int i; 9484e9119d9Sdan pRet->nCte = p->nCte; 9494e9119d9Sdan for(i=0; i<p->nCte; i++){ 9504e9119d9Sdan pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); 9514e9119d9Sdan pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); 9524e9119d9Sdan pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); 9534e9119d9Sdan } 9544e9119d9Sdan } 9554e9119d9Sdan } 9564e9119d9Sdan return pRet; 9574e9119d9Sdan } 958eede6a53Sdan #else 959eede6a53Sdan # define withDup(x,y) 0 960eede6a53Sdan #endif 9614e9119d9Sdan 9626ab3a2ecSdanielk1977 /* 963ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 964ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 965ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 966ff78bd2fSdrh ** without effecting the originals. 967ff78bd2fSdrh ** 9684adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 9694adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 970ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 971ff78bd2fSdrh ** 972ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 9736ab3a2ecSdanielk1977 ** 974b7916a78Sdrh ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 9756ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 9766ab3a2ecSdanielk1977 ** truncated version of the usual Expr structure that will be stored as 9776ab3a2ecSdanielk1977 ** part of the in-memory representation of the database schema. 978ff78bd2fSdrh */ 9796ab3a2ecSdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ 9806ab3a2ecSdanielk1977 return exprDup(db, p, flags, 0); 981ff78bd2fSdrh } 9826ab3a2ecSdanielk1977 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ 983ff78bd2fSdrh ExprList *pNew; 984145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 985ff78bd2fSdrh int i; 986ff78bd2fSdrh if( p==0 ) return 0; 98717435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 988ff78bd2fSdrh if( pNew==0 ) return 0; 989d872bb18Sdrh pNew->nExpr = i = p->nExpr; 990d872bb18Sdrh if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){} 991d872bb18Sdrh pNew->a = pItem = sqlite3DbMallocRaw(db, i*sizeof(p->a[0]) ); 992e0048400Sdanielk1977 if( pItem==0 ){ 993633e6d57Sdrh sqlite3DbFree(db, pNew); 994e0048400Sdanielk1977 return 0; 995e0048400Sdanielk1977 } 996145716b3Sdrh pOldItem = p->a; 997145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 9986ab3a2ecSdanielk1977 Expr *pOldExpr = pOldItem->pExpr; 999b5526ea6Sdrh pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); 100017435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1001b7916a78Sdrh pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); 1002145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 10033e7bc9caSdrh pItem->done = 0; 10042c036cffSdrh pItem->bSpanIsTab = pOldItem->bSpanIsTab; 1005c2acc4e4Sdrh pItem->u = pOldItem->u; 1006ff78bd2fSdrh } 1007ff78bd2fSdrh return pNew; 1008ff78bd2fSdrh } 100993758c8dSdanielk1977 101093758c8dSdanielk1977 /* 101193758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 101293758c8dSdanielk1977 ** the build, then none of the following routines, except for 101393758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 101493758c8dSdanielk1977 ** called with a NULL argument. 101593758c8dSdanielk1977 */ 10166a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 10176a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 10186ab3a2ecSdanielk1977 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ 1019ad3cab52Sdrh SrcList *pNew; 1020ad3cab52Sdrh int i; 1021113088ecSdrh int nByte; 1022ad3cab52Sdrh if( p==0 ) return 0; 1023113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 102417435752Sdrh pNew = sqlite3DbMallocRaw(db, nByte ); 1025ad3cab52Sdrh if( pNew==0 ) return 0; 10264305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 1027ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 10284efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 10294efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 1030ed8a3bb1Sdrh Table *pTab; 103141fb5cd1Sdan pNewItem->pSchema = pOldItem->pSchema; 103217435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 103317435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 103417435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 10354efc4754Sdrh pNewItem->jointype = pOldItem->jointype; 10364efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 10375b6a9ed4Sdrh pNewItem->addrFillSub = pOldItem->addrFillSub; 10385b6a9ed4Sdrh pNewItem->regReturn = pOldItem->regReturn; 1039da79cf0cSdan pNewItem->isCorrelated = pOldItem->isCorrelated; 104021172c4cSdrh pNewItem->viaCoroutine = pOldItem->viaCoroutine; 1041f43fe6e9Sdan pNewItem->isRecursive = pOldItem->isRecursive; 104285574e31Sdanielk1977 pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex); 104385574e31Sdanielk1977 pNewItem->notIndexed = pOldItem->notIndexed; 104485574e31Sdanielk1977 pNewItem->pIndex = pOldItem->pIndex; 1045ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 1046ed8a3bb1Sdrh if( pTab ){ 1047ed8a3bb1Sdrh pTab->nRef++; 1048a1cb183dSdanielk1977 } 10496ab3a2ecSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); 10506ab3a2ecSdanielk1977 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); 105117435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 10526c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 1053ad3cab52Sdrh } 1054ad3cab52Sdrh return pNew; 1055ad3cab52Sdrh } 105617435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 1057ff78bd2fSdrh IdList *pNew; 1058ff78bd2fSdrh int i; 1059ff78bd2fSdrh if( p==0 ) return 0; 106017435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) ); 1061ff78bd2fSdrh if( pNew==0 ) return 0; 10626c535158Sdrh pNew->nId = p->nId; 106317435752Sdrh pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) ); 1064d5d56523Sdanielk1977 if( pNew->a==0 ){ 1065633e6d57Sdrh sqlite3DbFree(db, pNew); 1066d5d56523Sdanielk1977 return 0; 1067d5d56523Sdanielk1977 } 10686c535158Sdrh /* Note that because the size of the allocation for p->a[] is not 10696c535158Sdrh ** necessarily a power of two, sqlite3IdListAppend() may not be called 10706c535158Sdrh ** on the duplicate created by this function. */ 1071ff78bd2fSdrh for(i=0; i<p->nId; i++){ 10724efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 10734efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 107417435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 10754efc4754Sdrh pNewItem->idx = pOldItem->idx; 1076ff78bd2fSdrh } 1077ff78bd2fSdrh return pNew; 1078ff78bd2fSdrh } 10796ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 108023b1b372Sdrh Select *pNew, *pPrior; 1081ff78bd2fSdrh if( p==0 ) return 0; 108217435752Sdrh pNew = sqlite3DbMallocRaw(db, sizeof(*p) ); 1083ff78bd2fSdrh if( pNew==0 ) return 0; 1084b7916a78Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); 10856ab3a2ecSdanielk1977 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); 10866ab3a2ecSdanielk1977 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); 10876ab3a2ecSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); 10886ab3a2ecSdanielk1977 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); 10896ab3a2ecSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); 1090ff78bd2fSdrh pNew->op = p->op; 109123b1b372Sdrh pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); 109223b1b372Sdrh if( pPrior ) pPrior->pNext = pNew; 109323b1b372Sdrh pNew->pNext = 0; 10946ab3a2ecSdanielk1977 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); 10956ab3a2ecSdanielk1977 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); 109692b01d53Sdrh pNew->iLimit = 0; 109792b01d53Sdrh pNew->iOffset = 0; 10987d10d5a6Sdrh pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; 1099b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 1100b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 1101ec2da854Sdrh pNew->nSelectRow = p->nSelectRow; 11024e9119d9Sdan pNew->pWith = withDup(db, p->pWith); 1103eb9b884cSdrh sqlite3SelectSetName(pNew, p->zSelName); 1104ff78bd2fSdrh return pNew; 1105ff78bd2fSdrh } 110693758c8dSdanielk1977 #else 11076ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 110893758c8dSdanielk1977 assert( p==0 ); 110993758c8dSdanielk1977 return 0; 111093758c8dSdanielk1977 } 111193758c8dSdanielk1977 #endif 1112ff78bd2fSdrh 1113ff78bd2fSdrh 1114ff78bd2fSdrh /* 1115a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 1116a76b5dfcSdrh ** initially NULL, then create a new expression list. 1117b7916a78Sdrh ** 1118b7916a78Sdrh ** If a memory allocation error occurs, the entire list is freed and 1119b7916a78Sdrh ** NULL is returned. If non-NULL is returned, then it is guaranteed 1120b7916a78Sdrh ** that the new entry was successfully appended. 1121a76b5dfcSdrh */ 112217435752Sdrh ExprList *sqlite3ExprListAppend( 112317435752Sdrh Parse *pParse, /* Parsing context */ 112417435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 1125b7916a78Sdrh Expr *pExpr /* Expression to be appended. Might be NULL */ 112617435752Sdrh ){ 112717435752Sdrh sqlite3 *db = pParse->db; 1128a76b5dfcSdrh if( pList==0 ){ 112917435752Sdrh pList = sqlite3DbMallocZero(db, sizeof(ExprList) ); 1130a76b5dfcSdrh if( pList==0 ){ 1131d5d56523Sdanielk1977 goto no_mem; 1132a76b5dfcSdrh } 1133d872bb18Sdrh pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0])); 1134d872bb18Sdrh if( pList->a==0 ) goto no_mem; 1135d872bb18Sdrh }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ 1136d5d56523Sdanielk1977 struct ExprList_item *a; 1137d872bb18Sdrh assert( pList->nExpr>0 ); 1138d872bb18Sdrh a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); 1139d5d56523Sdanielk1977 if( a==0 ){ 1140d5d56523Sdanielk1977 goto no_mem; 1141a76b5dfcSdrh } 1142d5d56523Sdanielk1977 pList->a = a; 1143a76b5dfcSdrh } 11444efc4754Sdrh assert( pList->a!=0 ); 1145b7916a78Sdrh if( 1 ){ 11464efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 11474efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 1148e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 1149a76b5dfcSdrh } 1150a76b5dfcSdrh return pList; 1151d5d56523Sdanielk1977 1152d5d56523Sdanielk1977 no_mem: 1153d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 1154633e6d57Sdrh sqlite3ExprDelete(db, pExpr); 1155633e6d57Sdrh sqlite3ExprListDelete(db, pList); 1156d5d56523Sdanielk1977 return 0; 1157a76b5dfcSdrh } 1158a76b5dfcSdrh 1159a76b5dfcSdrh /* 1160b7916a78Sdrh ** Set the ExprList.a[].zName element of the most recently added item 1161b7916a78Sdrh ** on the expression list. 1162b7916a78Sdrh ** 1163b7916a78Sdrh ** pList might be NULL following an OOM error. But pName should never be 1164b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1165b7916a78Sdrh ** is set. 1166b7916a78Sdrh */ 1167b7916a78Sdrh void sqlite3ExprListSetName( 1168b7916a78Sdrh Parse *pParse, /* Parsing context */ 1169b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1170b7916a78Sdrh Token *pName, /* Name to be added */ 1171b7916a78Sdrh int dequote /* True to cause the name to be dequoted */ 1172b7916a78Sdrh ){ 1173b7916a78Sdrh assert( pList!=0 || pParse->db->mallocFailed!=0 ); 1174b7916a78Sdrh if( pList ){ 1175b7916a78Sdrh struct ExprList_item *pItem; 1176b7916a78Sdrh assert( pList->nExpr>0 ); 1177b7916a78Sdrh pItem = &pList->a[pList->nExpr-1]; 1178b7916a78Sdrh assert( pItem->zName==0 ); 1179b7916a78Sdrh pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); 1180b7916a78Sdrh if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName); 1181b7916a78Sdrh } 1182b7916a78Sdrh } 1183b7916a78Sdrh 1184b7916a78Sdrh /* 1185b7916a78Sdrh ** Set the ExprList.a[].zSpan element of the most recently added item 1186b7916a78Sdrh ** on the expression list. 1187b7916a78Sdrh ** 1188b7916a78Sdrh ** pList might be NULL following an OOM error. But pSpan should never be 1189b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1190b7916a78Sdrh ** is set. 1191b7916a78Sdrh */ 1192b7916a78Sdrh void sqlite3ExprListSetSpan( 1193b7916a78Sdrh Parse *pParse, /* Parsing context */ 1194b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1195b7916a78Sdrh ExprSpan *pSpan /* The span to be added */ 1196b7916a78Sdrh ){ 1197b7916a78Sdrh sqlite3 *db = pParse->db; 1198b7916a78Sdrh assert( pList!=0 || db->mallocFailed!=0 ); 1199b7916a78Sdrh if( pList ){ 1200b7916a78Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; 1201b7916a78Sdrh assert( pList->nExpr>0 ); 1202b7916a78Sdrh assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); 1203b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1204b7916a78Sdrh pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, 1205cf697396Sshane (int)(pSpan->zEnd - pSpan->zStart)); 1206b7916a78Sdrh } 1207b7916a78Sdrh } 1208b7916a78Sdrh 1209b7916a78Sdrh /* 12107a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 12117a15a4beSdanielk1977 ** leave an error message in pParse. 12127a15a4beSdanielk1977 */ 12137a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 12147a15a4beSdanielk1977 Parse *pParse, 12157a15a4beSdanielk1977 ExprList *pEList, 12167a15a4beSdanielk1977 const char *zObject 12177a15a4beSdanielk1977 ){ 1218b1a6c3c1Sdrh int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 1219c5499befSdrh testcase( pEList && pEList->nExpr==mx ); 1220c5499befSdrh testcase( pEList && pEList->nExpr==mx+1 ); 1221b1a6c3c1Sdrh if( pEList && pEList->nExpr>mx ){ 12227a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 12237a15a4beSdanielk1977 } 12247a15a4beSdanielk1977 } 12257a15a4beSdanielk1977 12267a15a4beSdanielk1977 /* 1227a76b5dfcSdrh ** Delete an entire expression list. 1228a76b5dfcSdrh */ 1229633e6d57Sdrh void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ 1230a76b5dfcSdrh int i; 1231be5c89acSdrh struct ExprList_item *pItem; 1232a76b5dfcSdrh if( pList==0 ) return; 1233d872bb18Sdrh assert( pList->a!=0 || pList->nExpr==0 ); 1234be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 1235633e6d57Sdrh sqlite3ExprDelete(db, pItem->pExpr); 1236633e6d57Sdrh sqlite3DbFree(db, pItem->zName); 1237b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1238a76b5dfcSdrh } 1239633e6d57Sdrh sqlite3DbFree(db, pList->a); 1240633e6d57Sdrh sqlite3DbFree(db, pList); 1241a76b5dfcSdrh } 1242a76b5dfcSdrh 1243a76b5dfcSdrh /* 1244*2308ed38Sdrh ** Return the bitwise-OR of all Expr.flags fields in the given 1245*2308ed38Sdrh ** ExprList. 1246885a5b03Sdrh */ 1247*2308ed38Sdrh u32 sqlite3ExprListFlags(const ExprList *pList){ 1248885a5b03Sdrh int i; 1249*2308ed38Sdrh u32 m = 0; 1250*2308ed38Sdrh if( pList ){ 1251885a5b03Sdrh for(i=0; i<pList->nExpr; i++){ 1252*2308ed38Sdrh m |= pList->a[i].pExpr->flags; 1253885a5b03Sdrh } 1254*2308ed38Sdrh } 1255*2308ed38Sdrh return m; 1256885a5b03Sdrh } 1257885a5b03Sdrh 1258885a5b03Sdrh /* 1259059b2d50Sdrh ** These routines are Walker callbacks used to check expressions to 1260059b2d50Sdrh ** see if they are "constant" for some definition of constant. The 1261059b2d50Sdrh ** Walker.eCode value determines the type of "constant" we are looking 1262059b2d50Sdrh ** for. 126373b211abSdrh ** 12647d10d5a6Sdrh ** These callback routines are used to implement the following: 1265626a879aSdrh ** 1266059b2d50Sdrh ** sqlite3ExprIsConstant() pWalker->eCode==1 1267059b2d50Sdrh ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 1268059b2d50Sdrh ** sqlite3ExprRefOneTableOnly() pWalker->eCode==3 1269059b2d50Sdrh ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 1270059b2d50Sdrh ** 1271059b2d50Sdrh ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression 1272059b2d50Sdrh ** is found to not be a constant. 127387abf5c0Sdrh ** 1274feada2dfSdrh ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions 1275059b2d50Sdrh ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing 1276059b2d50Sdrh ** an existing schema and 4 when processing a new statement. A bound 1277feada2dfSdrh ** parameter raises an error for new statements, but is silently converted 1278feada2dfSdrh ** to NULL for existing schemas. This allows sqlite_master tables that 1279feada2dfSdrh ** contain a bound parameter because they were generated by older versions 1280feada2dfSdrh ** of SQLite to be parsed by newer versions of SQLite without raising a 1281feada2dfSdrh ** malformed schema error. 1282626a879aSdrh */ 12837d10d5a6Sdrh static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 1284626a879aSdrh 1285059b2d50Sdrh /* If pWalker->eCode is 2 then any term of the expression that comes from 1286059b2d50Sdrh ** the ON or USING clauses of a left join disqualifies the expression 12870a168377Sdrh ** from being considered constant. */ 1288059b2d50Sdrh if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ 1289059b2d50Sdrh pWalker->eCode = 0; 12907d10d5a6Sdrh return WRC_Abort; 12910a168377Sdrh } 12920a168377Sdrh 1293626a879aSdrh switch( pExpr->op ){ 1294eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 1295059b2d50Sdrh ** and either pWalker->eCode==4 or 5 or the function has the 1296059b2d50Sdrh ** SQLITE_FUNC_CONST flag. */ 1297eb55bd2fSdrh case TK_FUNCTION: 129863f84573Sdrh if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ 1299b1fba286Sdrh return WRC_Continue; 1300059b2d50Sdrh }else{ 1301059b2d50Sdrh pWalker->eCode = 0; 1302059b2d50Sdrh return WRC_Abort; 1303b1fba286Sdrh } 1304626a879aSdrh case TK_ID: 1305626a879aSdrh case TK_COLUMN: 1306626a879aSdrh case TK_AGG_FUNCTION: 130713449892Sdrh case TK_AGG_COLUMN: 1308c5499befSdrh testcase( pExpr->op==TK_ID ); 1309c5499befSdrh testcase( pExpr->op==TK_COLUMN ); 1310c5499befSdrh testcase( pExpr->op==TK_AGG_FUNCTION ); 1311c5499befSdrh testcase( pExpr->op==TK_AGG_COLUMN ); 1312059b2d50Sdrh if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ 1313059b2d50Sdrh return WRC_Continue; 1314059b2d50Sdrh }else{ 1315059b2d50Sdrh pWalker->eCode = 0; 13167d10d5a6Sdrh return WRC_Abort; 1317059b2d50Sdrh } 1318feada2dfSdrh case TK_VARIABLE: 1319059b2d50Sdrh if( pWalker->eCode==5 ){ 1320feada2dfSdrh /* Silently convert bound parameters that appear inside of CREATE 1321feada2dfSdrh ** statements into a NULL when parsing the CREATE statement text out 1322feada2dfSdrh ** of the sqlite_master table */ 1323feada2dfSdrh pExpr->op = TK_NULL; 1324059b2d50Sdrh }else if( pWalker->eCode==4 ){ 1325feada2dfSdrh /* A bound parameter in a CREATE statement that originates from 1326feada2dfSdrh ** sqlite3_prepare() causes an error */ 1327059b2d50Sdrh pWalker->eCode = 0; 1328feada2dfSdrh return WRC_Abort; 1329feada2dfSdrh } 1330feada2dfSdrh /* Fall through */ 1331626a879aSdrh default: 1332b74b1017Sdrh testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ 1333b74b1017Sdrh testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ 13347d10d5a6Sdrh return WRC_Continue; 1335626a879aSdrh } 1336626a879aSdrh } 133762c14b34Sdanielk1977 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ 133862c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 1339059b2d50Sdrh pWalker->eCode = 0; 13407d10d5a6Sdrh return WRC_Abort; 13417d10d5a6Sdrh } 1342059b2d50Sdrh static int exprIsConst(Expr *p, int initFlag, int iCur){ 13437d10d5a6Sdrh Walker w; 1344aa87f9a6Sdrh memset(&w, 0, sizeof(w)); 1345059b2d50Sdrh w.eCode = initFlag; 13467d10d5a6Sdrh w.xExprCallback = exprNodeIsConstant; 13477d10d5a6Sdrh w.xSelectCallback = selectNodeIsConstant; 1348059b2d50Sdrh w.u.iCur = iCur; 13497d10d5a6Sdrh sqlite3WalkExpr(&w, p); 1350059b2d50Sdrh return w.eCode; 13517d10d5a6Sdrh } 1352626a879aSdrh 1353626a879aSdrh /* 1354059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1355eb55bd2fSdrh ** and 0 if it involves variables or function calls. 13562398937bSdrh ** 13572398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 13582398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 13592398937bSdrh ** a constant. 1360fef5208cSdrh */ 13614adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 1362059b2d50Sdrh return exprIsConst(p, 1, 0); 1363fef5208cSdrh } 1364fef5208cSdrh 1365fef5208cSdrh /* 1366059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 13670a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 13680a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 13690a168377Sdrh ** an ON or USING clause. 13700a168377Sdrh */ 13710a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 1372059b2d50Sdrh return exprIsConst(p, 2, 0); 13730a168377Sdrh } 13740a168377Sdrh 13750a168377Sdrh /* 1376059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression constant 1377059b2d50Sdrh ** for any single row of the table with cursor iCur. In other words, the 1378059b2d50Sdrh ** expression must not refer to any non-deterministic function nor any 1379059b2d50Sdrh ** table other than iCur. 1380059b2d50Sdrh */ 1381059b2d50Sdrh int sqlite3ExprIsTableConstant(Expr *p, int iCur){ 1382059b2d50Sdrh return exprIsConst(p, 3, iCur); 1383059b2d50Sdrh } 1384059b2d50Sdrh 1385059b2d50Sdrh /* 1386059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1387eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 1388eb55bd2fSdrh ** are any variables. 1389eb55bd2fSdrh ** 1390eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 1391eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 1392eb55bd2fSdrh ** a constant. 1393eb55bd2fSdrh */ 1394feada2dfSdrh int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 1395feada2dfSdrh assert( isInit==0 || isInit==1 ); 1396059b2d50Sdrh return exprIsConst(p, 4+isInit, 0); 1397eb55bd2fSdrh } 1398eb55bd2fSdrh 1399eb55bd2fSdrh /* 140073b211abSdrh ** If the expression p codes a constant integer that is small enough 1401202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 1402202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 1403202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 1404e4de1febSdrh */ 14054adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 140692b01d53Sdrh int rc = 0; 1407cd92e84dSdrh 1408cd92e84dSdrh /* If an expression is an integer literal that fits in a signed 32-bit 1409cd92e84dSdrh ** integer, then the EP_IntValue flag will have already been set */ 1410cd92e84dSdrh assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 1411cd92e84dSdrh || sqlite3GetInt32(p->u.zToken, &rc)==0 ); 1412cd92e84dSdrh 141392b01d53Sdrh if( p->flags & EP_IntValue ){ 141433e619fcSdrh *pValue = p->u.iValue; 1415e4de1febSdrh return 1; 1416e4de1febSdrh } 141792b01d53Sdrh switch( p->op ){ 14184b59ab5eSdrh case TK_UPLUS: { 141992b01d53Sdrh rc = sqlite3ExprIsInteger(p->pLeft, pValue); 1420f6e369a1Sdrh break; 14214b59ab5eSdrh } 1422e4de1febSdrh case TK_UMINUS: { 1423e4de1febSdrh int v; 14244adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 1425f6418891Smistachkin assert( v!=(-2147483647-1) ); 1426e4de1febSdrh *pValue = -v; 142792b01d53Sdrh rc = 1; 1428e4de1febSdrh } 1429e4de1febSdrh break; 1430e4de1febSdrh } 1431e4de1febSdrh default: break; 1432e4de1febSdrh } 143392b01d53Sdrh return rc; 1434e4de1febSdrh } 1435e4de1febSdrh 1436e4de1febSdrh /* 1437039fc32eSdrh ** Return FALSE if there is no chance that the expression can be NULL. 1438039fc32eSdrh ** 1439039fc32eSdrh ** If the expression might be NULL or if the expression is too complex 1440039fc32eSdrh ** to tell return TRUE. 1441039fc32eSdrh ** 1442039fc32eSdrh ** This routine is used as an optimization, to skip OP_IsNull opcodes 1443039fc32eSdrh ** when we know that a value cannot be NULL. Hence, a false positive 1444039fc32eSdrh ** (returning TRUE when in fact the expression can never be NULL) might 1445039fc32eSdrh ** be a small performance hit but is otherwise harmless. On the other 1446039fc32eSdrh ** hand, a false negative (returning FALSE when the result could be NULL) 1447039fc32eSdrh ** will likely result in an incorrect answer. So when in doubt, return 1448039fc32eSdrh ** TRUE. 1449039fc32eSdrh */ 1450039fc32eSdrh int sqlite3ExprCanBeNull(const Expr *p){ 1451039fc32eSdrh u8 op; 1452cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1453039fc32eSdrh op = p->op; 1454039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1455039fc32eSdrh switch( op ){ 1456039fc32eSdrh case TK_INTEGER: 1457039fc32eSdrh case TK_STRING: 1458039fc32eSdrh case TK_FLOAT: 1459039fc32eSdrh case TK_BLOB: 1460039fc32eSdrh return 0; 14617248a8b2Sdrh case TK_COLUMN: 14627248a8b2Sdrh assert( p->pTab!=0 ); 146372673a24Sdrh return ExprHasProperty(p, EP_CanBeNull) || 146472673a24Sdrh (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); 1465039fc32eSdrh default: 1466039fc32eSdrh return 1; 1467039fc32eSdrh } 1468039fc32eSdrh } 1469039fc32eSdrh 1470039fc32eSdrh /* 1471039fc32eSdrh ** Return TRUE if the given expression is a constant which would be 1472039fc32eSdrh ** unchanged by OP_Affinity with the affinity given in the second 1473039fc32eSdrh ** argument. 1474039fc32eSdrh ** 1475039fc32eSdrh ** This routine is used to determine if the OP_Affinity operation 1476039fc32eSdrh ** can be omitted. When in doubt return FALSE. A false negative 1477039fc32eSdrh ** is harmless. A false positive, however, can result in the wrong 1478039fc32eSdrh ** answer. 1479039fc32eSdrh */ 1480039fc32eSdrh int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 1481039fc32eSdrh u8 op; 1482039fc32eSdrh if( aff==SQLITE_AFF_NONE ) return 1; 1483cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1484039fc32eSdrh op = p->op; 1485039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1486039fc32eSdrh switch( op ){ 1487039fc32eSdrh case TK_INTEGER: { 1488039fc32eSdrh return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; 1489039fc32eSdrh } 1490039fc32eSdrh case TK_FLOAT: { 1491039fc32eSdrh return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; 1492039fc32eSdrh } 1493039fc32eSdrh case TK_STRING: { 1494039fc32eSdrh return aff==SQLITE_AFF_TEXT; 1495039fc32eSdrh } 1496039fc32eSdrh case TK_BLOB: { 1497039fc32eSdrh return 1; 1498039fc32eSdrh } 14992f2855b6Sdrh case TK_COLUMN: { 150088376ca7Sdrh assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ 150188376ca7Sdrh return p->iColumn<0 15022f2855b6Sdrh && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); 15032f2855b6Sdrh } 1504039fc32eSdrh default: { 1505039fc32eSdrh return 0; 1506039fc32eSdrh } 1507039fc32eSdrh } 1508039fc32eSdrh } 1509039fc32eSdrh 1510039fc32eSdrh /* 1511c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 1512c4a3c779Sdrh */ 15134adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 15144adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 15154adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 15164adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 1517c4a3c779Sdrh return 0; 1518c4a3c779Sdrh } 1519c4a3c779Sdrh 15209a96b668Sdanielk1977 /* 1521b74b1017Sdrh ** Return true if we are able to the IN operator optimization on a 1522b74b1017Sdrh ** query of the form 1523b287f4b6Sdrh ** 1524b74b1017Sdrh ** x IN (SELECT ...) 1525b287f4b6Sdrh ** 1526b74b1017Sdrh ** Where the SELECT... clause is as specified by the parameter to this 1527b74b1017Sdrh ** routine. 1528b74b1017Sdrh ** 1529b74b1017Sdrh ** The Select object passed in has already been preprocessed and no 1530b74b1017Sdrh ** errors have been found. 1531b287f4b6Sdrh */ 1532b287f4b6Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1533b287f4b6Sdrh static int isCandidateForInOpt(Select *p){ 1534b287f4b6Sdrh SrcList *pSrc; 1535b287f4b6Sdrh ExprList *pEList; 1536b287f4b6Sdrh Table *pTab; 1537b287f4b6Sdrh if( p==0 ) return 0; /* right-hand side of IN is SELECT */ 1538b287f4b6Sdrh if( p->pPrior ) return 0; /* Not a compound SELECT */ 15397d10d5a6Sdrh if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ 1540b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); 1541b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); 15427d10d5a6Sdrh return 0; /* No DISTINCT keyword and no aggregate functions */ 15437d10d5a6Sdrh } 1544b74b1017Sdrh assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ 1545b287f4b6Sdrh if( p->pLimit ) return 0; /* Has no LIMIT clause */ 1546b74b1017Sdrh assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ 1547b287f4b6Sdrh if( p->pWhere ) return 0; /* Has no WHERE clause */ 1548b287f4b6Sdrh pSrc = p->pSrc; 1549d1fa7bcaSdrh assert( pSrc!=0 ); 1550d1fa7bcaSdrh if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ 1551b74b1017Sdrh if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ 1552b287f4b6Sdrh pTab = pSrc->a[0].pTab; 1553b74b1017Sdrh if( NEVER(pTab==0) ) return 0; 1554b74b1017Sdrh assert( pTab->pSelect==0 ); /* FROM clause is not a view */ 1555b287f4b6Sdrh if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ 1556b287f4b6Sdrh pEList = p->pEList; 1557b287f4b6Sdrh if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ 1558b287f4b6Sdrh if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */ 1559b287f4b6Sdrh return 1; 1560b287f4b6Sdrh } 1561b287f4b6Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1562b287f4b6Sdrh 1563b287f4b6Sdrh /* 15641d8cb21fSdan ** Code an OP_Once instruction and allocate space for its flag. Return the 15651d8cb21fSdan ** address of the new instruction. 15661d8cb21fSdan */ 15671d8cb21fSdan int sqlite3CodeOnce(Parse *pParse){ 15681d8cb21fSdan Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 15697d176105Sdrh return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); 15701d8cb21fSdan } 15711d8cb21fSdan 15721d8cb21fSdan /* 15734c259e9fSdrh ** Generate code that checks the left-most column of index table iCur to see if 15744c259e9fSdrh ** it contains any NULL entries. Cause the register at regHasNull to be set 15756be515ebSdrh ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 15766be515ebSdrh ** to be set to NULL if iCur contains one or more NULL values. 15776be515ebSdrh */ 15786be515ebSdrh static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 15796be515ebSdrh int j1; 15806be515ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 15816be515ebSdrh j1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 15826be515ebSdrh sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 15836be515ebSdrh sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 15844c259e9fSdrh VdbeComment((v, "first_entry_in(%d)", iCur)); 15856be515ebSdrh sqlite3VdbeJumpHere(v, j1); 15866be515ebSdrh } 15876be515ebSdrh 1588bb53ecb1Sdrh 1589bb53ecb1Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1590bb53ecb1Sdrh /* 1591bb53ecb1Sdrh ** The argument is an IN operator with a list (not a subquery) on the 1592bb53ecb1Sdrh ** right-hand side. Return TRUE if that list is constant. 1593bb53ecb1Sdrh */ 1594bb53ecb1Sdrh static int sqlite3InRhsIsConstant(Expr *pIn){ 1595bb53ecb1Sdrh Expr *pLHS; 1596bb53ecb1Sdrh int res; 1597bb53ecb1Sdrh assert( !ExprHasProperty(pIn, EP_xIsSelect) ); 1598bb53ecb1Sdrh pLHS = pIn->pLeft; 1599bb53ecb1Sdrh pIn->pLeft = 0; 1600bb53ecb1Sdrh res = sqlite3ExprIsConstant(pIn); 1601bb53ecb1Sdrh pIn->pLeft = pLHS; 1602bb53ecb1Sdrh return res; 1603bb53ecb1Sdrh } 1604bb53ecb1Sdrh #endif 1605bb53ecb1Sdrh 16066be515ebSdrh /* 16079a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 1608d4305ca6Sdrh ** The pX parameter is the expression on the RHS of the IN operator, which 1609d4305ca6Sdrh ** might be either a list of expressions or a subquery. 16109a96b668Sdanielk1977 ** 1611d4305ca6Sdrh ** The job of this routine is to find or create a b-tree object that can 1612d4305ca6Sdrh ** be used either to test for membership in the RHS set or to iterate through 1613d4305ca6Sdrh ** all members of the RHS set, skipping duplicates. 1614d4305ca6Sdrh ** 16153a85625dSdrh ** A cursor is opened on the b-tree object that is the RHS of the IN operator 1616d4305ca6Sdrh ** and pX->iTable is set to the index of that cursor. 1617d4305ca6Sdrh ** 1618b74b1017Sdrh ** The returned value of this function indicates the b-tree type, as follows: 16199a96b668Sdanielk1977 ** 16209a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 16211ccce449Sdrh ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. 16221ccce449Sdrh ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. 16239a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 16249a96b668Sdanielk1977 ** populated epheremal table. 1625bb53ecb1Sdrh ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be 1626bb53ecb1Sdrh ** implemented as a sequence of comparisons. 16279a96b668Sdanielk1977 ** 1628d4305ca6Sdrh ** An existing b-tree might be used if the RHS expression pX is a simple 1629d4305ca6Sdrh ** subquery such as: 16309a96b668Sdanielk1977 ** 16319a96b668Sdanielk1977 ** SELECT <column> FROM <table> 16329a96b668Sdanielk1977 ** 1633d4305ca6Sdrh ** If the RHS of the IN operator is a list or a more complex subquery, then 1634d4305ca6Sdrh ** an ephemeral table might need to be generated from the RHS and then 163560ec914cSpeter.d.reid ** pX->iTable made to point to the ephemeral table instead of an 1636d4305ca6Sdrh ** existing table. 1637d4305ca6Sdrh ** 16383a85625dSdrh ** The inFlags parameter must contain exactly one of the bits 16393a85625dSdrh ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains 16403a85625dSdrh ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a 16413a85625dSdrh ** fast membership test. When the IN_INDEX_LOOP bit is set, the 16423a85625dSdrh ** IN index will be used to loop over all values of the RHS of the 16433a85625dSdrh ** IN operator. 16443a85625dSdrh ** 16453a85625dSdrh ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate 16463a85625dSdrh ** through the set members) then the b-tree must not contain duplicates. 16473a85625dSdrh ** An epheremal table must be used unless the selected <column> is guaranteed 16489a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 1649b74b1017Sdrh ** has a UNIQUE constraint or UNIQUE index. 16500cdc022eSdanielk1977 ** 16513a85625dSdrh ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 16523a85625dSdrh ** for fast set membership tests) then an epheremal table must 16530cdc022eSdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 16540cdc022eSdanielk1977 ** be found with <column> as its left-most column. 16550cdc022eSdanielk1977 ** 1656bb53ecb1Sdrh ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and 1657bb53ecb1Sdrh ** if the RHS of the IN operator is a list (not a subquery) then this 1658bb53ecb1Sdrh ** routine might decide that creating an ephemeral b-tree for membership 1659bb53ecb1Sdrh ** testing is too expensive and return IN_INDEX_NOOP. In that case, the 1660bb53ecb1Sdrh ** calling routine should implement the IN operator using a sequence 1661bb53ecb1Sdrh ** of Eq or Ne comparison operations. 1662bb53ecb1Sdrh ** 1663b74b1017Sdrh ** When the b-tree is being used for membership tests, the calling function 16643a85625dSdrh ** might need to know whether or not the RHS side of the IN operator 1665e21a6e1dSdrh ** contains a NULL. If prRhsHasNull is not a NULL pointer and 16663a85625dSdrh ** if there is any chance that the (...) might contain a NULL value at 16670cdc022eSdanielk1977 ** runtime, then a register is allocated and the register number written 1668e21a6e1dSdrh ** to *prRhsHasNull. If there is no chance that the (...) contains a 1669e21a6e1dSdrh ** NULL value, then *prRhsHasNull is left unchanged. 16700cdc022eSdanielk1977 ** 1671e21a6e1dSdrh ** If a register is allocated and its location stored in *prRhsHasNull, then 16726be515ebSdrh ** the value in that register will be NULL if the b-tree contains one or more 16736be515ebSdrh ** NULL values, and it will be some non-NULL value if the b-tree contains no 16746be515ebSdrh ** NULL values. 16759a96b668Sdanielk1977 */ 1676284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1677e21a6e1dSdrh int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){ 1678b74b1017Sdrh Select *p; /* SELECT to the right of IN operator */ 1679b74b1017Sdrh int eType = 0; /* Type of RHS table. IN_INDEX_* */ 1680b74b1017Sdrh int iTab = pParse->nTab++; /* Cursor of the RHS table */ 16813a85625dSdrh int mustBeUnique; /* True if RHS must be unique */ 1682b8475df8Sdrh Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 16839a96b668Sdanielk1977 16841450bc6eSdrh assert( pX->op==TK_IN ); 16853a85625dSdrh mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 16861450bc6eSdrh 1687b74b1017Sdrh /* Check to see if an existing table or index can be used to 1688b74b1017Sdrh ** satisfy the query. This is preferable to generating a new 1689b74b1017Sdrh ** ephemeral table. 16909a96b668Sdanielk1977 */ 16916ab3a2ecSdanielk1977 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); 1692fd773cf9Sdrh if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ 1693e1fb65a0Sdanielk1977 sqlite3 *db = pParse->db; /* Database connection */ 1694b07028f7Sdrh Table *pTab; /* Table <table>. */ 1695b07028f7Sdrh Expr *pExpr; /* Expression <column> */ 1696bbbdc83bSdrh i16 iCol; /* Index of column <column> */ 1697bbbdc83bSdrh i16 iDb; /* Database idx for pTab */ 1698e1fb65a0Sdanielk1977 1699b07028f7Sdrh assert( p ); /* Because of isCandidateForInOpt(p) */ 1700b07028f7Sdrh assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 1701b07028f7Sdrh assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 1702b07028f7Sdrh assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 1703b07028f7Sdrh pTab = p->pSrc->a[0].pTab; 1704b07028f7Sdrh pExpr = p->pEList->a[0].pExpr; 1705bbbdc83bSdrh iCol = (i16)pExpr->iColumn; 1706b07028f7Sdrh 1707b22f7c83Sdrh /* Code an OP_Transaction and OP_TableLock for <table>. */ 1708e1fb65a0Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 1709e1fb65a0Sdanielk1977 sqlite3CodeVerifySchema(pParse, iDb); 1710e1fb65a0Sdanielk1977 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 17119a96b668Sdanielk1977 17129a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 17139a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 17149a96b668Sdanielk1977 ** successful here. 17159a96b668Sdanielk1977 */ 17169a96b668Sdanielk1977 assert(v); 17179a96b668Sdanielk1977 if( iCol<0 ){ 17187d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); 17197d176105Sdrh VdbeCoverage(v); 17209a96b668Sdanielk1977 17219a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 17229a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 17239a96b668Sdanielk1977 17249a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 17259a96b668Sdanielk1977 }else{ 1726e1fb65a0Sdanielk1977 Index *pIdx; /* Iterator variable */ 1727e1fb65a0Sdanielk1977 17289a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 17299a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 1730e1fb65a0Sdanielk1977 ** to this collation sequence. */ 17319a96b668Sdanielk1977 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); 17329a96b668Sdanielk1977 17339a96b668Sdanielk1977 /* Check that the affinity that will be used to perform the 17349a96b668Sdanielk1977 ** comparison is the same as the affinity of the column. If 17359a96b668Sdanielk1977 ** it is not, it is not possible to use any index. 17369a96b668Sdanielk1977 */ 1737dbaee5e3Sdrh int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity); 17389a96b668Sdanielk1977 17399a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 17409a96b668Sdanielk1977 if( (pIdx->aiColumn[0]==iCol) 1741b74b1017Sdrh && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq 17425f1d1d9cSdrh && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx))) 17439a96b668Sdanielk1977 ){ 17447d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); 17452ec2fb22Sdrh sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); 17462ec2fb22Sdrh sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 1747207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 17481ccce449Sdrh assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); 17491ccce449Sdrh eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; 17509a96b668Sdanielk1977 1751e21a6e1dSdrh if( prRhsHasNull && !pTab->aCol[iCol].notNull ){ 1752e21a6e1dSdrh *prRhsHasNull = ++pParse->nMem; 17536be515ebSdrh sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); 17540cdc022eSdanielk1977 } 1755552fd454Sdrh sqlite3VdbeJumpHere(v, iAddr); 17569a96b668Sdanielk1977 } 17579a96b668Sdanielk1977 } 17589a96b668Sdanielk1977 } 17599a96b668Sdanielk1977 } 17609a96b668Sdanielk1977 1761bb53ecb1Sdrh /* If no preexisting index is available for the IN clause 1762bb53ecb1Sdrh ** and IN_INDEX_NOOP is an allowed reply 1763bb53ecb1Sdrh ** and the RHS of the IN operator is a list, not a subquery 1764bb53ecb1Sdrh ** and the RHS is not contant or has two or fewer terms, 176560ec914cSpeter.d.reid ** then it is not worth creating an ephemeral table to evaluate 1766bb53ecb1Sdrh ** the IN operator so return IN_INDEX_NOOP. 1767bb53ecb1Sdrh */ 1768bb53ecb1Sdrh if( eType==0 1769bb53ecb1Sdrh && (inFlags & IN_INDEX_NOOP_OK) 1770bb53ecb1Sdrh && !ExprHasProperty(pX, EP_xIsSelect) 1771bb53ecb1Sdrh && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) 1772bb53ecb1Sdrh ){ 1773bb53ecb1Sdrh eType = IN_INDEX_NOOP; 1774bb53ecb1Sdrh } 1775bb53ecb1Sdrh 1776bb53ecb1Sdrh 17779a96b668Sdanielk1977 if( eType==0 ){ 17784387006cSdrh /* Could not find an existing table or index to use as the RHS b-tree. 1779b74b1017Sdrh ** We will have to generate an ephemeral table to do the job. 1780b74b1017Sdrh */ 17818e23daf3Sdrh u32 savedNQueryLoop = pParse->nQueryLoop; 17820cdc022eSdanielk1977 int rMayHaveNull = 0; 178341a05b7bSdanielk1977 eType = IN_INDEX_EPH; 17843a85625dSdrh if( inFlags & IN_INDEX_LOOP ){ 17854a5acf8eSdrh pParse->nQueryLoop = 0; 1786c5cd1249Sdrh if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ 178741a05b7bSdanielk1977 eType = IN_INDEX_ROWID; 17880cdc022eSdanielk1977 } 1789e21a6e1dSdrh }else if( prRhsHasNull ){ 1790e21a6e1dSdrh *prRhsHasNull = rMayHaveNull = ++pParse->nMem; 1791cf4d38aaSdrh } 179241a05b7bSdanielk1977 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); 1793cf4d38aaSdrh pParse->nQueryLoop = savedNQueryLoop; 17949a96b668Sdanielk1977 }else{ 17959a96b668Sdanielk1977 pX->iTable = iTab; 17969a96b668Sdanielk1977 } 17979a96b668Sdanielk1977 return eType; 17989a96b668Sdanielk1977 } 1799284f4acaSdanielk1977 #endif 1800626a879aSdrh 1801626a879aSdrh /* 1802d4187c71Sdrh ** Generate code for scalar subqueries used as a subquery expression, EXISTS, 1803d4187c71Sdrh ** or IN operators. Examples: 1804626a879aSdrh ** 18059cbe6352Sdrh ** (SELECT a FROM b) -- subquery 18069cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 18079cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 18089cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 1809fef5208cSdrh ** 18109cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 18119cbe6352Sdrh ** operator or subquery. 181241a05b7bSdanielk1977 ** 181341a05b7bSdanielk1977 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed 181441a05b7bSdanielk1977 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference 181541a05b7bSdanielk1977 ** to some integer key column of a table B-Tree. In this case, use an 181641a05b7bSdanielk1977 ** intkey B-Tree to store the set of IN(...) values instead of the usual 181741a05b7bSdanielk1977 ** (slower) variable length keys B-Tree. 1818fd773cf9Sdrh ** 1819fd773cf9Sdrh ** If rMayHaveNull is non-zero, that means that the operation is an IN 1820fd773cf9Sdrh ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. 18213a85625dSdrh ** All this routine does is initialize the register given by rMayHaveNull 18223a85625dSdrh ** to NULL. Calling routines will take care of changing this register 18233a85625dSdrh ** value to non-NULL if the RHS is NULL-free. 18241450bc6eSdrh ** 18251450bc6eSdrh ** For a SELECT or EXISTS operator, return the register that holds the 18261450bc6eSdrh ** result. For IN operators or if an error occurs, the return value is 0. 1827cce7d176Sdrh */ 182851522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 18291450bc6eSdrh int sqlite3CodeSubselect( 1830fd773cf9Sdrh Parse *pParse, /* Parsing context */ 1831fd773cf9Sdrh Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ 18326be515ebSdrh int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ 1833fd773cf9Sdrh int isRowid /* If true, LHS of IN operator is a rowid */ 183441a05b7bSdanielk1977 ){ 18356be515ebSdrh int jmpIfDynamic = -1; /* One-time test address */ 18361450bc6eSdrh int rReg = 0; /* Register storing resulting */ 1837b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 18381450bc6eSdrh if( NEVER(v==0) ) return 0; 1839ceea3321Sdrh sqlite3ExprCachePush(pParse); 1840fc976065Sdanielk1977 184157dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 184257dbd7b3Sdrh ** if any of the following is true: 184357dbd7b3Sdrh ** 184457dbd7b3Sdrh ** * The right-hand side is a correlated subquery 184557dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 184657dbd7b3Sdrh ** * We are inside a trigger 184757dbd7b3Sdrh ** 184857dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 184957dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1850b3bce662Sdanielk1977 */ 1851c5cd1249Sdrh if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 18526be515ebSdrh jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); 1853b3bce662Sdanielk1977 } 1854b3bce662Sdanielk1977 18554a07e3dbSdan #ifndef SQLITE_OMIT_EXPLAIN 18564a07e3dbSdan if( pParse->explain==2 ){ 18574a07e3dbSdan char *zMsg = sqlite3MPrintf( 18586be515ebSdrh pParse->db, "EXECUTE %s%s SUBQUERY %d", jmpIfDynamic>=0?"":"CORRELATED ", 18594a07e3dbSdan pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId 18604a07e3dbSdan ); 18614a07e3dbSdan sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); 18624a07e3dbSdan } 18634a07e3dbSdan #endif 18644a07e3dbSdan 1865cce7d176Sdrh switch( pExpr->op ){ 1866fef5208cSdrh case TK_IN: { 1867d4187c71Sdrh char affinity; /* Affinity of the LHS of the IN */ 1868b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 1869d4187c71Sdrh Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ 1870323df790Sdrh KeyInfo *pKeyInfo = 0; /* Key information */ 1871d3d39e93Sdrh 187241a05b7bSdanielk1977 affinity = sqlite3ExprAffinity(pLeft); 1873e014a838Sdanielk1977 1874e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 18758cff69dfSdrh ** expression it is handled the same way. An ephemeral table is 1876e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1877e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1878fef5208cSdrh ** 1879e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1880e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1881e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1882e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1883e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1884e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1885e014a838Sdanielk1977 ** is used. 1886fef5208cSdrh */ 1887832508b7Sdrh pExpr->iTable = pParse->nTab++; 188841a05b7bSdanielk1977 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); 1889ad124329Sdrh pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1); 1890e014a838Sdanielk1977 18916ab3a2ecSdanielk1977 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 1892e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1893e014a838Sdanielk1977 ** 1894e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1895e014a838Sdanielk1977 ** table allocated and opened above. 1896e014a838Sdanielk1977 */ 18974387006cSdrh Select *pSelect = pExpr->x.pSelect; 18981013c932Sdrh SelectDest dest; 1899be5c89acSdrh ExprList *pEList; 19001013c932Sdrh 190141a05b7bSdanielk1977 assert( !isRowid ); 19021013c932Sdrh sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 19032b596da8Sdrh dest.affSdst = (u8)affinity; 1904e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 19054387006cSdrh pSelect->iLimit = 0; 19064387006cSdrh testcase( pSelect->selFlags & SF_Distinct ); 1907812ea833Sdrh testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ 19084387006cSdrh if( sqlite3Select(pParse, pSelect, &dest) ){ 19092ec2fb22Sdrh sqlite3KeyInfoUnref(pKeyInfo); 19101450bc6eSdrh return 0; 191194ccde58Sdrh } 19124387006cSdrh pEList = pSelect->pEList; 1913812ea833Sdrh assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ 19143535ec3eSdrh assert( pEList!=0 ); 19153535ec3eSdrh assert( pEList->nExpr>0 ); 19162ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 1917323df790Sdrh pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, 1918be5c89acSdrh pEList->a[0].pExpr); 1919a7d2db17Sdrh }else if( ALWAYS(pExpr->x.pList!=0) ){ 1920fef5208cSdrh /* Case 2: expr IN (exprlist) 1921fef5208cSdrh ** 1922e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1923e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1924e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1925e014a838Sdanielk1977 ** a column, use numeric affinity. 1926fef5208cSdrh */ 1927e014a838Sdanielk1977 int i; 19286ab3a2ecSdanielk1977 ExprList *pList = pExpr->x.pList; 192957dbd7b3Sdrh struct ExprList_item *pItem; 1930ecc31805Sdrh int r1, r2, r3; 193157dbd7b3Sdrh 1932e014a838Sdanielk1977 if( !affinity ){ 19338159a35fSdrh affinity = SQLITE_AFF_NONE; 1934e014a838Sdanielk1977 } 1935323df790Sdrh if( pKeyInfo ){ 19362ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 1937323df790Sdrh pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 1938323df790Sdrh } 1939e014a838Sdanielk1977 1940e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 19412d401ab8Sdrh r1 = sqlite3GetTempReg(pParse); 19422d401ab8Sdrh r2 = sqlite3GetTempReg(pParse); 194337e08081Sdrh if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); 194457dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 194557dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1946e05c929bSdrh int iValToIns; 1947e014a838Sdanielk1977 194857dbd7b3Sdrh /* If the expression is not constant then we will need to 194957dbd7b3Sdrh ** disable the test that was generated above that makes sure 195057dbd7b3Sdrh ** this code only executes once. Because for a non-constant 195157dbd7b3Sdrh ** expression we need to rerun this code each time. 195257dbd7b3Sdrh */ 19536be515ebSdrh if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ 19546be515ebSdrh sqlite3VdbeChangeToNoop(v, jmpIfDynamic); 19556be515ebSdrh jmpIfDynamic = -1; 19564794b980Sdrh } 1957e014a838Sdanielk1977 1958e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 1959e05c929bSdrh if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ 1960e05c929bSdrh sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); 1961e05c929bSdrh }else{ 1962ecc31805Sdrh r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); 196341a05b7bSdanielk1977 if( isRowid ){ 1964e05c929bSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, 1965e05c929bSdrh sqlite3VdbeCurrentAddr(v)+2); 1966688852abSdrh VdbeCoverage(v); 196741a05b7bSdanielk1977 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); 196841a05b7bSdanielk1977 }else{ 1969ecc31805Sdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); 19703c31fc23Sdrh sqlite3ExprCacheAffinityChange(pParse, r3, 1); 19712d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); 1972fef5208cSdrh } 197341a05b7bSdanielk1977 } 1974e05c929bSdrh } 19752d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r1); 19762d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r2); 1977fef5208cSdrh } 1978323df790Sdrh if( pKeyInfo ){ 19792ec2fb22Sdrh sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); 198041a05b7bSdanielk1977 } 1981b3bce662Sdanielk1977 break; 1982fef5208cSdrh } 1983fef5208cSdrh 198451522cd3Sdrh case TK_EXISTS: 1985fd773cf9Sdrh case TK_SELECT: 1986fd773cf9Sdrh default: { 1987fd773cf9Sdrh /* If this has to be a scalar SELECT. Generate code to put the 1988fef5208cSdrh ** value of this select in a memory cell and record the number 1989fd773cf9Sdrh ** of the memory cell in iColumn. If this is an EXISTS, write 1990fd773cf9Sdrh ** an integer 0 (not exists) or 1 (exists) into a memory cell 1991fd773cf9Sdrh ** and record that memory cell in iColumn. 1992fef5208cSdrh */ 1993fd773cf9Sdrh Select *pSel; /* SELECT statement to encode */ 1994fd773cf9Sdrh SelectDest dest; /* How to deal with SELECt result */ 19951398ad36Sdrh 1996cf697396Sshane testcase( pExpr->op==TK_EXISTS ); 1997cf697396Sshane testcase( pExpr->op==TK_SELECT ); 1998cf697396Sshane assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); 1999cf697396Sshane 20006ab3a2ecSdanielk1977 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); 20016ab3a2ecSdanielk1977 pSel = pExpr->x.pSelect; 20021013c932Sdrh sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); 200351522cd3Sdrh if( pExpr->op==TK_SELECT ){ 20046c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 200553932ce8Sdrh dest.iSdst = dest.iSDParm; 20062b596da8Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm); 2007d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 200851522cd3Sdrh }else{ 20096c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 20102b596da8Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 2011d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 201251522cd3Sdrh } 2013633e6d57Sdrh sqlite3ExprDelete(pParse->db, pSel->pLimit); 2014094430ebSdrh pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, 2015094430ebSdrh &sqlite3IntTokens[1]); 201648b5b041Sdrh pSel->iLimit = 0; 20177d10d5a6Sdrh if( sqlite3Select(pParse, pSel, &dest) ){ 20181450bc6eSdrh return 0; 201994ccde58Sdrh } 20202b596da8Sdrh rReg = dest.iSDParm; 2021ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 2022b3bce662Sdanielk1977 break; 202319a775c2Sdrh } 2024cce7d176Sdrh } 2025b3bce662Sdanielk1977 20266be515ebSdrh if( rHasNullFlag ){ 20276be515ebSdrh sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); 20286be515ebSdrh } 20296be515ebSdrh 20306be515ebSdrh if( jmpIfDynamic>=0 ){ 20316be515ebSdrh sqlite3VdbeJumpHere(v, jmpIfDynamic); 2032b3bce662Sdanielk1977 } 2033d2490904Sdrh sqlite3ExprCachePop(pParse); 2034fc976065Sdanielk1977 20351450bc6eSdrh return rReg; 2036cce7d176Sdrh } 203751522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2038cce7d176Sdrh 2039e3365e6cSdrh #ifndef SQLITE_OMIT_SUBQUERY 2040e3365e6cSdrh /* 2041e3365e6cSdrh ** Generate code for an IN expression. 2042e3365e6cSdrh ** 2043e3365e6cSdrh ** x IN (SELECT ...) 2044e3365e6cSdrh ** x IN (value, value, ...) 2045e3365e6cSdrh ** 2046e3365e6cSdrh ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) 2047e3365e6cSdrh ** is an array of zero or more values. The expression is true if the LHS is 2048e3365e6cSdrh ** contained within the RHS. The value of the expression is unknown (NULL) 2049e3365e6cSdrh ** if the LHS is NULL or if the LHS is not contained within the RHS and the 2050e3365e6cSdrh ** RHS contains one or more NULL values. 2051e3365e6cSdrh ** 20526be515ebSdrh ** This routine generates code that jumps to destIfFalse if the LHS is not 2053e3365e6cSdrh ** contained within the RHS. If due to NULLs we cannot determine if the LHS 2054e3365e6cSdrh ** is contained in the RHS then jump to destIfNull. If the LHS is contained 2055e3365e6cSdrh ** within the RHS then fall through. 2056e3365e6cSdrh */ 2057e3365e6cSdrh static void sqlite3ExprCodeIN( 2058e3365e6cSdrh Parse *pParse, /* Parsing and code generating context */ 2059e3365e6cSdrh Expr *pExpr, /* The IN expression */ 2060e3365e6cSdrh int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 2061e3365e6cSdrh int destIfNull /* Jump here if the results are unknown due to NULLs */ 2062e3365e6cSdrh ){ 2063e3365e6cSdrh int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ 2064e3365e6cSdrh char affinity; /* Comparison affinity to use */ 2065e3365e6cSdrh int eType; /* Type of the RHS */ 2066e3365e6cSdrh int r1; /* Temporary use register */ 2067e3365e6cSdrh Vdbe *v; /* Statement under construction */ 2068e3365e6cSdrh 2069e3365e6cSdrh /* Compute the RHS. After this step, the table with cursor 2070e3365e6cSdrh ** pExpr->iTable will contains the values that make up the RHS. 2071e3365e6cSdrh */ 2072e3365e6cSdrh v = pParse->pVdbe; 2073e3365e6cSdrh assert( v!=0 ); /* OOM detected prior to this routine */ 2074e3365e6cSdrh VdbeNoopComment((v, "begin IN expr")); 2075bb53ecb1Sdrh eType = sqlite3FindInIndex(pParse, pExpr, 2076bb53ecb1Sdrh IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, 20773a85625dSdrh destIfFalse==destIfNull ? 0 : &rRhsHasNull); 2078e3365e6cSdrh 2079e3365e6cSdrh /* Figure out the affinity to use to create a key from the results 2080e3365e6cSdrh ** of the expression. affinityStr stores a static string suitable for 2081e3365e6cSdrh ** P4 of OP_MakeRecord. 2082e3365e6cSdrh */ 2083e3365e6cSdrh affinity = comparisonAffinity(pExpr); 2084e3365e6cSdrh 2085e3365e6cSdrh /* Code the LHS, the <expr> from "<expr> IN (...)". 2086e3365e6cSdrh */ 2087e3365e6cSdrh sqlite3ExprCachePush(pParse); 2088e3365e6cSdrh r1 = sqlite3GetTempReg(pParse); 2089e3365e6cSdrh sqlite3ExprCode(pParse, pExpr->pLeft, r1); 2090e3365e6cSdrh 2091bb53ecb1Sdrh /* If sqlite3FindInIndex() did not find or create an index that is 2092bb53ecb1Sdrh ** suitable for evaluating the IN operator, then evaluate using a 2093bb53ecb1Sdrh ** sequence of comparisons. 2094bb53ecb1Sdrh */ 2095bb53ecb1Sdrh if( eType==IN_INDEX_NOOP ){ 2096bb53ecb1Sdrh ExprList *pList = pExpr->x.pList; 2097bb53ecb1Sdrh CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2098bb53ecb1Sdrh int labelOk = sqlite3VdbeMakeLabel(v); 2099bb53ecb1Sdrh int r2, regToFree; 2100bb53ecb1Sdrh int regCkNull = 0; 2101bb53ecb1Sdrh int ii; 2102bb53ecb1Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2103bb53ecb1Sdrh if( destIfNull!=destIfFalse ){ 2104bb53ecb1Sdrh regCkNull = sqlite3GetTempReg(pParse); 2105a976979bSdrh sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); 2106bb53ecb1Sdrh } 2107bb53ecb1Sdrh for(ii=0; ii<pList->nExpr; ii++){ 2108bb53ecb1Sdrh r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); 2109a976979bSdrh if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ 2110bb53ecb1Sdrh sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); 2111bb53ecb1Sdrh } 2112bb53ecb1Sdrh if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ 2113bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, 21144336b0e6Sdrh (void*)pColl, P4_COLLSEQ); 21154336b0e6Sdrh VdbeCoverageIf(v, ii<pList->nExpr-1); 21164336b0e6Sdrh VdbeCoverageIf(v, ii==pList->nExpr-1); 2117bb53ecb1Sdrh sqlite3VdbeChangeP5(v, affinity); 2118bb53ecb1Sdrh }else{ 2119bb53ecb1Sdrh assert( destIfNull==destIfFalse ); 2120bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, 2121bb53ecb1Sdrh (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); 2122bb53ecb1Sdrh sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL); 2123bb53ecb1Sdrh } 2124bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regToFree); 2125bb53ecb1Sdrh } 2126bb53ecb1Sdrh if( regCkNull ){ 2127bb53ecb1Sdrh sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 2128bb53ecb1Sdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 2129bb53ecb1Sdrh } 2130bb53ecb1Sdrh sqlite3VdbeResolveLabel(v, labelOk); 2131bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regCkNull); 2132bb53ecb1Sdrh }else{ 2133bb53ecb1Sdrh 2134094430ebSdrh /* If the LHS is NULL, then the result is either false or NULL depending 2135094430ebSdrh ** on whether the RHS is empty or not, respectively. 2136094430ebSdrh */ 21377248a8b2Sdrh if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ 2138094430ebSdrh if( destIfNull==destIfFalse ){ 2139094430ebSdrh /* Shortcut for the common case where the false and NULL outcomes are 2140094430ebSdrh ** the same. */ 2141688852abSdrh sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v); 2142094430ebSdrh }else{ 2143688852abSdrh int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); 2144094430ebSdrh sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 2145688852abSdrh VdbeCoverage(v); 2146094430ebSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); 2147094430ebSdrh sqlite3VdbeJumpHere(v, addr1); 2148094430ebSdrh } 21497248a8b2Sdrh } 2150e3365e6cSdrh 2151e3365e6cSdrh if( eType==IN_INDEX_ROWID ){ 2152e3365e6cSdrh /* In this case, the RHS is the ROWID of table b-tree 2153e3365e6cSdrh */ 2154688852abSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v); 2155e3365e6cSdrh sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1); 2156688852abSdrh VdbeCoverage(v); 2157e3365e6cSdrh }else{ 2158e3365e6cSdrh /* In this case, the RHS is an index b-tree. 2159e3365e6cSdrh */ 21608cff69dfSdrh sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); 2161e3365e6cSdrh 2162e3365e6cSdrh /* If the set membership test fails, then the result of the 2163e3365e6cSdrh ** "x IN (...)" expression must be either 0 or NULL. If the set 2164e3365e6cSdrh ** contains no NULL values, then the result is 0. If the set 2165e3365e6cSdrh ** contains one or more NULL values, then the result of the 2166e3365e6cSdrh ** expression is also NULL. 2167e3365e6cSdrh */ 2168e80c9b9aSdrh assert( destIfFalse!=destIfNull || rRhsHasNull==0 ); 2169e80c9b9aSdrh if( rRhsHasNull==0 ){ 2170e3365e6cSdrh /* This branch runs if it is known at compile time that the RHS 2171e3365e6cSdrh ** cannot contain NULL values. This happens as the result 2172e3365e6cSdrh ** of a "NOT NULL" constraint in the database schema. 2173e3365e6cSdrh ** 2174e3365e6cSdrh ** Also run this branch if NULL is equivalent to FALSE 2175e3365e6cSdrh ** for this particular IN operator. 2176e3365e6cSdrh */ 21778cff69dfSdrh sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); 2178688852abSdrh VdbeCoverage(v); 2179e3365e6cSdrh }else{ 2180e3365e6cSdrh /* In this branch, the RHS of the IN might contain a NULL and 2181e3365e6cSdrh ** the presence of a NULL on the RHS makes a difference in the 2182e3365e6cSdrh ** outcome. 2183e3365e6cSdrh */ 21846be515ebSdrh int j1; 2185e3365e6cSdrh 2186e3365e6cSdrh /* First check to see if the LHS is contained in the RHS. If so, 21876be515ebSdrh ** then the answer is TRUE the presence of NULLs in the RHS does 21886be515ebSdrh ** not matter. If the LHS is not contained in the RHS, then the 21896be515ebSdrh ** answer is NULL if the RHS contains NULLs and the answer is 21906be515ebSdrh ** FALSE if the RHS is NULL-free. 2191e3365e6cSdrh */ 21928cff69dfSdrh j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); 2193688852abSdrh VdbeCoverage(v); 21946be515ebSdrh sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); 2195552fd454Sdrh VdbeCoverage(v); 2196e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 2197e3365e6cSdrh sqlite3VdbeJumpHere(v, j1); 2198e3365e6cSdrh } 2199e3365e6cSdrh } 2200bb53ecb1Sdrh } 2201e3365e6cSdrh sqlite3ReleaseTempReg(pParse, r1); 2202d2490904Sdrh sqlite3ExprCachePop(pParse); 2203e3365e6cSdrh VdbeComment((v, "end IN expr")); 2204e3365e6cSdrh } 2205e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2206e3365e6cSdrh 2207cce7d176Sdrh /* 2208598f1340Sdrh ** Duplicate an 8-byte value 2209598f1340Sdrh */ 2210598f1340Sdrh static char *dup8bytes(Vdbe *v, const char *in){ 2211598f1340Sdrh char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); 2212598f1340Sdrh if( out ){ 2213598f1340Sdrh memcpy(out, in, 8); 2214598f1340Sdrh } 2215598f1340Sdrh return out; 2216598f1340Sdrh } 2217598f1340Sdrh 221813573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2219598f1340Sdrh /* 2220598f1340Sdrh ** Generate an instruction that will put the floating point 22219cbf3425Sdrh ** value described by z[0..n-1] into register iMem. 22220cf19ed8Sdrh ** 22230cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 22240cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 22250cf19ed8Sdrh ** like the continuation of the number. 2226598f1340Sdrh */ 2227b7916a78Sdrh static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 2228fd773cf9Sdrh if( ALWAYS(z!=0) ){ 2229598f1340Sdrh double value; 2230598f1340Sdrh char *zV; 22319339da1fSdrh sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 2232d0015161Sdrh assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 2233598f1340Sdrh if( negateFlag ) value = -value; 2234598f1340Sdrh zV = dup8bytes(v, (char*)&value); 22359de221dfSdrh sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); 2236598f1340Sdrh } 2237598f1340Sdrh } 223813573c71Sdrh #endif 2239598f1340Sdrh 2240598f1340Sdrh 2241598f1340Sdrh /* 2242fec19aadSdrh ** Generate an instruction that will put the integer describe by 22439cbf3425Sdrh ** text z[0..n-1] into register iMem. 22440cf19ed8Sdrh ** 22455f1d6b61Sshaneh ** Expr.u.zToken is always UTF8 and zero-terminated. 2246fec19aadSdrh */ 224713573c71Sdrh static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 224813573c71Sdrh Vdbe *v = pParse->pVdbe; 224992b01d53Sdrh if( pExpr->flags & EP_IntValue ){ 225033e619fcSdrh int i = pExpr->u.iValue; 2251d50ffc41Sdrh assert( i>=0 ); 225292b01d53Sdrh if( negFlag ) i = -i; 225392b01d53Sdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 2254fd773cf9Sdrh }else{ 22555f1d6b61Sshaneh int c; 22565f1d6b61Sshaneh i64 value; 2257fd773cf9Sdrh const char *z = pExpr->u.zToken; 2258fd773cf9Sdrh assert( z!=0 ); 22599296c18aSdrh c = sqlite3DecOrHexToI64(z, &value); 22605f1d6b61Sshaneh if( c==0 || (c==2 && negFlag) ){ 2261598f1340Sdrh char *zV; 2262158b9cb9Sdrh if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } 2263598f1340Sdrh zV = dup8bytes(v, (char*)&value); 22649de221dfSdrh sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); 2265fec19aadSdrh }else{ 226613573c71Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 226713573c71Sdrh sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); 226813573c71Sdrh #else 22691b7ddc59Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER 22709296c18aSdrh if( sqlite3_strnicmp(z,"0x",2)==0 ){ 22719296c18aSdrh sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); 22721b7ddc59Sdrh }else 22731b7ddc59Sdrh #endif 22741b7ddc59Sdrh { 2275b7916a78Sdrh codeReal(v, z, negFlag, iMem); 22769296c18aSdrh } 227713573c71Sdrh #endif 2278fec19aadSdrh } 2279fec19aadSdrh } 2280c9cf901dSdanielk1977 } 2281fec19aadSdrh 2282ceea3321Sdrh /* 2283ceea3321Sdrh ** Clear a cache entry. 2284ceea3321Sdrh */ 2285ceea3321Sdrh static void cacheEntryClear(Parse *pParse, struct yColCache *p){ 2286ceea3321Sdrh if( p->tempReg ){ 2287ceea3321Sdrh if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 2288ceea3321Sdrh pParse->aTempReg[pParse->nTempReg++] = p->iReg; 2289ceea3321Sdrh } 2290ceea3321Sdrh p->tempReg = 0; 2291ceea3321Sdrh } 2292ceea3321Sdrh } 2293ceea3321Sdrh 2294ceea3321Sdrh 2295ceea3321Sdrh /* 2296ceea3321Sdrh ** Record in the column cache that a particular column from a 2297ceea3321Sdrh ** particular table is stored in a particular register. 2298ceea3321Sdrh */ 2299ceea3321Sdrh void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ 2300ceea3321Sdrh int i; 2301ceea3321Sdrh int minLru; 2302ceea3321Sdrh int idxLru; 2303ceea3321Sdrh struct yColCache *p; 2304ceea3321Sdrh 2305ce8f53d4Sdan /* Unless an error has occurred, register numbers are always positive. */ 2306ce8f53d4Sdan assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); 230720411ea7Sdrh assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ 230820411ea7Sdrh 2309b6da74ebSdrh /* The SQLITE_ColumnCache flag disables the column cache. This is used 2310b6da74ebSdrh ** for testing only - to verify that SQLite always gets the same answer 2311b6da74ebSdrh ** with and without the column cache. 2312b6da74ebSdrh */ 23137e5418e4Sdrh if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; 2314b6da74ebSdrh 231527ee406eSdrh /* First replace any existing entry. 231627ee406eSdrh ** 231727ee406eSdrh ** Actually, the way the column cache is currently used, we are guaranteed 231827ee406eSdrh ** that the object will never already be in cache. Verify this guarantee. 231927ee406eSdrh */ 232027ee406eSdrh #ifndef NDEBUG 2321ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 232227ee406eSdrh assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); 2323ceea3321Sdrh } 232427ee406eSdrh #endif 2325ceea3321Sdrh 2326ceea3321Sdrh /* Find an empty slot and replace it */ 2327ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2328ceea3321Sdrh if( p->iReg==0 ){ 2329ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2330ceea3321Sdrh p->iTable = iTab; 2331ceea3321Sdrh p->iColumn = iCol; 2332ceea3321Sdrh p->iReg = iReg; 2333ceea3321Sdrh p->tempReg = 0; 2334ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2335ceea3321Sdrh return; 2336ceea3321Sdrh } 2337ceea3321Sdrh } 2338ceea3321Sdrh 2339ceea3321Sdrh /* Replace the last recently used */ 2340ceea3321Sdrh minLru = 0x7fffffff; 2341ceea3321Sdrh idxLru = -1; 2342ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2343ceea3321Sdrh if( p->lru<minLru ){ 2344ceea3321Sdrh idxLru = i; 2345ceea3321Sdrh minLru = p->lru; 2346ceea3321Sdrh } 2347ceea3321Sdrh } 234820411ea7Sdrh if( ALWAYS(idxLru>=0) ){ 2349ceea3321Sdrh p = &pParse->aColCache[idxLru]; 2350ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2351ceea3321Sdrh p->iTable = iTab; 2352ceea3321Sdrh p->iColumn = iCol; 2353ceea3321Sdrh p->iReg = iReg; 2354ceea3321Sdrh p->tempReg = 0; 2355ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2356ceea3321Sdrh return; 2357ceea3321Sdrh } 2358ceea3321Sdrh } 2359ceea3321Sdrh 2360ceea3321Sdrh /* 2361f49f3523Sdrh ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. 2362f49f3523Sdrh ** Purge the range of registers from the column cache. 2363ceea3321Sdrh */ 2364f49f3523Sdrh void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ 2365ceea3321Sdrh int i; 2366f49f3523Sdrh int iLast = iReg + nReg - 1; 2367ceea3321Sdrh struct yColCache *p; 2368ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2369f49f3523Sdrh int r = p->iReg; 2370f49f3523Sdrh if( r>=iReg && r<=iLast ){ 2371ceea3321Sdrh cacheEntryClear(pParse, p); 2372ceea3321Sdrh p->iReg = 0; 2373ceea3321Sdrh } 2374ceea3321Sdrh } 2375ceea3321Sdrh } 2376ceea3321Sdrh 2377ceea3321Sdrh /* 2378ceea3321Sdrh ** Remember the current column cache context. Any new entries added 2379ceea3321Sdrh ** added to the column cache after this call are removed when the 2380ceea3321Sdrh ** corresponding pop occurs. 2381ceea3321Sdrh */ 2382ceea3321Sdrh void sqlite3ExprCachePush(Parse *pParse){ 2383ceea3321Sdrh pParse->iCacheLevel++; 23849ac7962aSdrh #ifdef SQLITE_DEBUG 23859ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 23869ac7962aSdrh printf("PUSH to %d\n", pParse->iCacheLevel); 23879ac7962aSdrh } 23889ac7962aSdrh #endif 2389ceea3321Sdrh } 2390ceea3321Sdrh 2391ceea3321Sdrh /* 2392ceea3321Sdrh ** Remove from the column cache any entries that were added since the 2393d2490904Sdrh ** the previous sqlite3ExprCachePush operation. In other words, restore 2394d2490904Sdrh ** the cache to the state it was in prior the most recent Push. 2395ceea3321Sdrh */ 2396d2490904Sdrh void sqlite3ExprCachePop(Parse *pParse){ 2397ceea3321Sdrh int i; 2398ceea3321Sdrh struct yColCache *p; 2399d2490904Sdrh assert( pParse->iCacheLevel>=1 ); 2400d2490904Sdrh pParse->iCacheLevel--; 24019ac7962aSdrh #ifdef SQLITE_DEBUG 24029ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 24039ac7962aSdrh printf("POP to %d\n", pParse->iCacheLevel); 24049ac7962aSdrh } 24059ac7962aSdrh #endif 2406ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2407ceea3321Sdrh if( p->iReg && p->iLevel>pParse->iCacheLevel ){ 2408ceea3321Sdrh cacheEntryClear(pParse, p); 2409ceea3321Sdrh p->iReg = 0; 2410ceea3321Sdrh } 2411ceea3321Sdrh } 2412ceea3321Sdrh } 2413945498f3Sdrh 2414945498f3Sdrh /* 24155cd79239Sdrh ** When a cached column is reused, make sure that its register is 24165cd79239Sdrh ** no longer available as a temp register. ticket #3879: that same 24175cd79239Sdrh ** register might be in the cache in multiple places, so be sure to 24185cd79239Sdrh ** get them all. 24195cd79239Sdrh */ 24205cd79239Sdrh static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ 24215cd79239Sdrh int i; 24225cd79239Sdrh struct yColCache *p; 24235cd79239Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 24245cd79239Sdrh if( p->iReg==iReg ){ 24255cd79239Sdrh p->tempReg = 0; 24265cd79239Sdrh } 24275cd79239Sdrh } 24285cd79239Sdrh } 24295cd79239Sdrh 24305cd79239Sdrh /* 24315c092e8aSdrh ** Generate code to extract the value of the iCol-th column of a table. 24325c092e8aSdrh */ 24335c092e8aSdrh void sqlite3ExprCodeGetColumnOfTable( 24345c092e8aSdrh Vdbe *v, /* The VDBE under construction */ 24355c092e8aSdrh Table *pTab, /* The table containing the value */ 2436313619f5Sdrh int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 24375c092e8aSdrh int iCol, /* Index of the column to extract */ 2438313619f5Sdrh int regOut /* Extract the value into this register */ 24395c092e8aSdrh ){ 24405c092e8aSdrh if( iCol<0 || iCol==pTab->iPKey ){ 24415c092e8aSdrh sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 24425c092e8aSdrh }else{ 24435c092e8aSdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 2444ee0ec8e1Sdrh int x = iCol; 2445ee0ec8e1Sdrh if( !HasRowid(pTab) ){ 2446ee0ec8e1Sdrh x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 2447ee0ec8e1Sdrh } 2448ee0ec8e1Sdrh sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 24495c092e8aSdrh } 24505c092e8aSdrh if( iCol>=0 ){ 24515c092e8aSdrh sqlite3ColumnDefault(v, pTab, iCol, regOut); 24525c092e8aSdrh } 24535c092e8aSdrh } 24545c092e8aSdrh 24555c092e8aSdrh /* 2456945498f3Sdrh ** Generate code that will extract the iColumn-th column from 2457e55cbd72Sdrh ** table pTab and store the column value in a register. An effort 2458e55cbd72Sdrh ** is made to store the column value in register iReg, but this is 2459e55cbd72Sdrh ** not guaranteed. The location of the column value is returned. 2460e55cbd72Sdrh ** 2461e55cbd72Sdrh ** There must be an open cursor to pTab in iTable when this routine 2462e55cbd72Sdrh ** is called. If iColumn<0 then code is generated that extracts the rowid. 2463945498f3Sdrh */ 2464e55cbd72Sdrh int sqlite3ExprCodeGetColumn( 2465e55cbd72Sdrh Parse *pParse, /* Parsing and code generating context */ 24662133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 24672133d822Sdrh int iColumn, /* Index of the table column */ 24682133d822Sdrh int iTable, /* The cursor pointing to the table */ 2469a748fdccSdrh int iReg, /* Store results here */ 2470a748fdccSdrh u8 p5 /* P5 value for OP_Column */ 24712133d822Sdrh ){ 2472e55cbd72Sdrh Vdbe *v = pParse->pVdbe; 2473e55cbd72Sdrh int i; 2474da250ea5Sdrh struct yColCache *p; 2475e55cbd72Sdrh 2476ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2477b6da74ebSdrh if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ 2478ceea3321Sdrh p->lru = pParse->iCacheCnt++; 24795cd79239Sdrh sqlite3ExprCachePinRegister(pParse, p->iReg); 2480da250ea5Sdrh return p->iReg; 2481e55cbd72Sdrh } 2482e55cbd72Sdrh } 2483e55cbd72Sdrh assert( v!=0 ); 24845c092e8aSdrh sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); 2485a748fdccSdrh if( p5 ){ 2486a748fdccSdrh sqlite3VdbeChangeP5(v, p5); 2487a748fdccSdrh }else{ 2488ceea3321Sdrh sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); 2489a748fdccSdrh } 2490e55cbd72Sdrh return iReg; 2491e55cbd72Sdrh } 2492e55cbd72Sdrh 2493e55cbd72Sdrh /* 2494ceea3321Sdrh ** Clear all column cache entries. 2495e55cbd72Sdrh */ 2496ceea3321Sdrh void sqlite3ExprCacheClear(Parse *pParse){ 2497e55cbd72Sdrh int i; 2498ceea3321Sdrh struct yColCache *p; 2499ceea3321Sdrh 25009ac7962aSdrh #if SQLITE_DEBUG 25019ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 25029ac7962aSdrh printf("CLEAR\n"); 25039ac7962aSdrh } 25049ac7962aSdrh #endif 2505ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2506ceea3321Sdrh if( p->iReg ){ 2507ceea3321Sdrh cacheEntryClear(pParse, p); 2508ceea3321Sdrh p->iReg = 0; 2509e55cbd72Sdrh } 2510da250ea5Sdrh } 2511da250ea5Sdrh } 2512e55cbd72Sdrh 2513e55cbd72Sdrh /* 2514da250ea5Sdrh ** Record the fact that an affinity change has occurred on iCount 2515da250ea5Sdrh ** registers starting with iStart. 2516e55cbd72Sdrh */ 2517da250ea5Sdrh void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 2518f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iStart, iCount); 2519e55cbd72Sdrh } 2520e55cbd72Sdrh 2521e55cbd72Sdrh /* 2522b21e7c70Sdrh ** Generate code to move content from registers iFrom...iFrom+nReg-1 2523b21e7c70Sdrh ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. 2524e55cbd72Sdrh */ 2525b21e7c70Sdrh void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ 2526e8e4af76Sdrh assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); 2527079a3072Sdrh sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); 2528236241aeSdrh sqlite3ExprCacheRemove(pParse, iFrom, nReg); 2529945498f3Sdrh } 2530945498f3Sdrh 2531f49f3523Sdrh #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) 253292b01d53Sdrh /* 2533652fbf55Sdrh ** Return true if any register in the range iFrom..iTo (inclusive) 2534652fbf55Sdrh ** is used as part of the column cache. 2535f49f3523Sdrh ** 2536f49f3523Sdrh ** This routine is used within assert() and testcase() macros only 2537f49f3523Sdrh ** and does not appear in a normal build. 2538652fbf55Sdrh */ 2539652fbf55Sdrh static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 2540652fbf55Sdrh int i; 2541ceea3321Sdrh struct yColCache *p; 2542ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2543ceea3321Sdrh int r = p->iReg; 2544f49f3523Sdrh if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ 2545652fbf55Sdrh } 2546652fbf55Sdrh return 0; 2547652fbf55Sdrh } 2548f49f3523Sdrh #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ 2549652fbf55Sdrh 2550652fbf55Sdrh /* 2551a4c3c87eSdrh ** Convert an expression node to a TK_REGISTER 2552a4c3c87eSdrh */ 2553a4c3c87eSdrh static void exprToRegister(Expr *p, int iReg){ 2554a4c3c87eSdrh p->op2 = p->op; 2555a4c3c87eSdrh p->op = TK_REGISTER; 2556a4c3c87eSdrh p->iTable = iReg; 2557a4c3c87eSdrh ExprClearProperty(p, EP_Skip); 2558a4c3c87eSdrh } 2559a4c3c87eSdrh 2560a4c3c87eSdrh /* 2561cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 25622dcef11bSdrh ** expression. Attempt to store the results in register "target". 25632dcef11bSdrh ** Return the register where results are stored. 2564389a1adbSdrh ** 25658b213899Sdrh ** With this routine, there is no guarantee that results will 25662dcef11bSdrh ** be stored in target. The result might be stored in some other 25672dcef11bSdrh ** register if it is convenient to do so. The calling function 25682dcef11bSdrh ** must check the return code and move the results to the desired 25692dcef11bSdrh ** register. 2570cce7d176Sdrh */ 2571678ccce8Sdrh int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 25722dcef11bSdrh Vdbe *v = pParse->pVdbe; /* The VM under construction */ 25732dcef11bSdrh int op; /* The opcode being coded */ 25742dcef11bSdrh int inReg = target; /* Results stored in register inReg */ 25752dcef11bSdrh int regFree1 = 0; /* If non-zero free this temporary register */ 25762dcef11bSdrh int regFree2 = 0; /* If non-zero free this temporary register */ 2577678ccce8Sdrh int r1, r2, r3, r4; /* Various register numbers */ 257820411ea7Sdrh sqlite3 *db = pParse->db; /* The database connection */ 257910d1edf0Sdrh Expr tempX; /* Temporary expression node */ 2580ffe07b2dSdrh 25819cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 258220411ea7Sdrh if( v==0 ){ 258320411ea7Sdrh assert( pParse->db->mallocFailed ); 258420411ea7Sdrh return 0; 258520411ea7Sdrh } 2586389a1adbSdrh 2587389a1adbSdrh if( pExpr==0 ){ 2588389a1adbSdrh op = TK_NULL; 2589389a1adbSdrh }else{ 2590f2bc013cSdrh op = pExpr->op; 2591389a1adbSdrh } 2592f2bc013cSdrh switch( op ){ 259313449892Sdrh case TK_AGG_COLUMN: { 259413449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 259513449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 259613449892Sdrh if( !pAggInfo->directMode ){ 25979de221dfSdrh assert( pCol->iMem>0 ); 25989de221dfSdrh inReg = pCol->iMem; 259913449892Sdrh break; 260013449892Sdrh }else if( pAggInfo->useSortingIdx ){ 26015134d135Sdan sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 2602389a1adbSdrh pCol->iSorterColumn, target); 260313449892Sdrh break; 260413449892Sdrh } 260513449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 260613449892Sdrh } 2607967e8b73Sdrh case TK_COLUMN: { 2608b2b9d3d7Sdrh int iTab = pExpr->iTable; 2609b2b9d3d7Sdrh if( iTab<0 ){ 2610b2b9d3d7Sdrh if( pParse->ckBase>0 ){ 2611b2b9d3d7Sdrh /* Generating CHECK constraints or inserting into partial index */ 2612aa9b8963Sdrh inReg = pExpr->iColumn + pParse->ckBase; 2613b2b9d3d7Sdrh break; 2614c4a3c779Sdrh }else{ 2615b2b9d3d7Sdrh /* Deleting from a partial index */ 2616b2b9d3d7Sdrh iTab = pParse->iPartIdxTab; 26172282792aSdrh } 2618b2b9d3d7Sdrh } 2619b2b9d3d7Sdrh inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 2620b2b9d3d7Sdrh pExpr->iColumn, iTab, target, 2621b2b9d3d7Sdrh pExpr->op2); 2622cce7d176Sdrh break; 2623cce7d176Sdrh } 2624cce7d176Sdrh case TK_INTEGER: { 262513573c71Sdrh codeInteger(pParse, pExpr, 0, target); 2626fec19aadSdrh break; 262751e9a445Sdrh } 262813573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2629598f1340Sdrh case TK_FLOAT: { 263033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 263133e619fcSdrh codeReal(v, pExpr->u.zToken, 0, target); 2632598f1340Sdrh break; 2633598f1340Sdrh } 263413573c71Sdrh #endif 2635fec19aadSdrh case TK_STRING: { 263633e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 263733e619fcSdrh sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0); 2638cce7d176Sdrh break; 2639cce7d176Sdrh } 2640f0863fe5Sdrh case TK_NULL: { 26419de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 2642f0863fe5Sdrh break; 2643f0863fe5Sdrh } 26445338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 2645c572ef7fSdanielk1977 case TK_BLOB: { 26466c8c6cecSdrh int n; 26476c8c6cecSdrh const char *z; 2648ca48c90fSdrh char *zBlob; 264933e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 265033e619fcSdrh assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 265133e619fcSdrh assert( pExpr->u.zToken[1]=='\'' ); 265233e619fcSdrh z = &pExpr->u.zToken[2]; 2653b7916a78Sdrh n = sqlite3Strlen30(z) - 1; 2654b7916a78Sdrh assert( z[n]=='\'' ); 2655ca48c90fSdrh zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 2656ca48c90fSdrh sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 2657c572ef7fSdanielk1977 break; 2658c572ef7fSdanielk1977 } 26595338a5f7Sdanielk1977 #endif 266050457896Sdrh case TK_VARIABLE: { 266133e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 266233e619fcSdrh assert( pExpr->u.zToken!=0 ); 266333e619fcSdrh assert( pExpr->u.zToken[0]!=0 ); 2664eaf52d88Sdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); 266533e619fcSdrh if( pExpr->u.zToken[1]!=0 ){ 266604e9eeadSdrh assert( pExpr->u.zToken[0]=='?' 266704e9eeadSdrh || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); 266804e9eeadSdrh sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); 2669895d7472Sdrh } 267050457896Sdrh break; 267150457896Sdrh } 26724e0cff60Sdrh case TK_REGISTER: { 26739de221dfSdrh inReg = pExpr->iTable; 26744e0cff60Sdrh break; 26754e0cff60Sdrh } 26768b213899Sdrh case TK_AS: { 26777445ffe2Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 26788b213899Sdrh break; 26798b213899Sdrh } 2680487e262fSdrh #ifndef SQLITE_OMIT_CAST 2681487e262fSdrh case TK_CAST: { 2682487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 26832dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 26841735fa88Sdrh if( inReg!=target ){ 26851735fa88Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); 26861735fa88Sdrh inReg = target; 26871735fa88Sdrh } 26884169e430Sdrh sqlite3VdbeAddOp2(v, OP_Cast, target, 26894169e430Sdrh sqlite3AffinityType(pExpr->u.zToken, 0)); 2690c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 2691b3843a82Sdrh sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 2692487e262fSdrh break; 2693487e262fSdrh } 2694487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 2695c9b84a1fSdrh case TK_LT: 2696c9b84a1fSdrh case TK_LE: 2697c9b84a1fSdrh case TK_GT: 2698c9b84a1fSdrh case TK_GE: 2699c9b84a1fSdrh case TK_NE: 2700c9b84a1fSdrh case TK_EQ: { 2701b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2702b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 270335573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 27047d176105Sdrh r1, r2, inReg, SQLITE_STOREP2); 27057d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 27067d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 27077d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 27087d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 27097d176105Sdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 27107d176105Sdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 2711c5499befSdrh testcase( regFree1==0 ); 2712c5499befSdrh testcase( regFree2==0 ); 2713a37cdde0Sdanielk1977 break; 2714c9b84a1fSdrh } 27156a2fe093Sdrh case TK_IS: 27166a2fe093Sdrh case TK_ISNOT: { 27176a2fe093Sdrh testcase( op==TK_IS ); 27186a2fe093Sdrh testcase( op==TK_ISNOT ); 2719b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2720b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 27216a2fe093Sdrh op = (op==TK_IS) ? TK_EQ : TK_NE; 27226a2fe093Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 27236a2fe093Sdrh r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ); 27247d176105Sdrh VdbeCoverageIf(v, op==TK_EQ); 27257d176105Sdrh VdbeCoverageIf(v, op==TK_NE); 27266a2fe093Sdrh testcase( regFree1==0 ); 27276a2fe093Sdrh testcase( regFree2==0 ); 27286a2fe093Sdrh break; 27296a2fe093Sdrh } 2730cce7d176Sdrh case TK_AND: 2731cce7d176Sdrh case TK_OR: 2732cce7d176Sdrh case TK_PLUS: 2733cce7d176Sdrh case TK_STAR: 2734cce7d176Sdrh case TK_MINUS: 2735bf4133cbSdrh case TK_REM: 2736bf4133cbSdrh case TK_BITAND: 2737bf4133cbSdrh case TK_BITOR: 273817c40294Sdrh case TK_SLASH: 2739bf4133cbSdrh case TK_LSHIFT: 2740855eb1cfSdrh case TK_RSHIFT: 27410040077dSdrh case TK_CONCAT: { 27427d176105Sdrh assert( TK_AND==OP_And ); testcase( op==TK_AND ); 27437d176105Sdrh assert( TK_OR==OP_Or ); testcase( op==TK_OR ); 27447d176105Sdrh assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); 27457d176105Sdrh assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); 27467d176105Sdrh assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); 27477d176105Sdrh assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); 27487d176105Sdrh assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); 27497d176105Sdrh assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); 27507d176105Sdrh assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); 27517d176105Sdrh assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); 27527d176105Sdrh assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); 27532dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 27542dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 27555b6afba9Sdrh sqlite3VdbeAddOp3(v, op, r2, r1, target); 2756c5499befSdrh testcase( regFree1==0 ); 2757c5499befSdrh testcase( regFree2==0 ); 27580040077dSdrh break; 27590040077dSdrh } 2760cce7d176Sdrh case TK_UMINUS: { 2761fec19aadSdrh Expr *pLeft = pExpr->pLeft; 2762fec19aadSdrh assert( pLeft ); 276313573c71Sdrh if( pLeft->op==TK_INTEGER ){ 276413573c71Sdrh codeInteger(pParse, pLeft, 1, target); 276513573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 276613573c71Sdrh }else if( pLeft->op==TK_FLOAT ){ 276733e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 276833e619fcSdrh codeReal(v, pLeft->u.zToken, 1, target); 276913573c71Sdrh #endif 27703c84ddffSdrh }else{ 277110d1edf0Sdrh tempX.op = TK_INTEGER; 277210d1edf0Sdrh tempX.flags = EP_IntValue|EP_TokenOnly; 277310d1edf0Sdrh tempX.u.iValue = 0; 277410d1edf0Sdrh r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); 2775e55cbd72Sdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 27762dcef11bSdrh sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 2777c5499befSdrh testcase( regFree2==0 ); 27783c84ddffSdrh } 27799de221dfSdrh inReg = target; 27806e142f54Sdrh break; 27816e142f54Sdrh } 2782bf4133cbSdrh case TK_BITNOT: 27836e142f54Sdrh case TK_NOT: { 27847d176105Sdrh assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); 27857d176105Sdrh assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); 2786e99fa2afSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2787e99fa2afSdrh testcase( regFree1==0 ); 2788e99fa2afSdrh inReg = target; 2789e99fa2afSdrh sqlite3VdbeAddOp2(v, op, r1, inReg); 2790cce7d176Sdrh break; 2791cce7d176Sdrh } 2792cce7d176Sdrh case TK_ISNULL: 2793cce7d176Sdrh case TK_NOTNULL: { 27946a288a33Sdrh int addr; 27957d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 27967d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 27979de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 27982dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2799c5499befSdrh testcase( regFree1==0 ); 28007d176105Sdrh addr = sqlite3VdbeAddOp1(v, op, r1); 28017d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 28027d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 2803a976979bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, target); 28046a288a33Sdrh sqlite3VdbeJumpHere(v, addr); 2805a37cdde0Sdanielk1977 break; 2806f2bc013cSdrh } 28072282792aSdrh case TK_AGG_FUNCTION: { 280813449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 28097e56e711Sdrh if( pInfo==0 ){ 281033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 281133e619fcSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); 28127e56e711Sdrh }else{ 28139de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 28147e56e711Sdrh } 28152282792aSdrh break; 28162282792aSdrh } 2817cce7d176Sdrh case TK_FUNCTION: { 281812ffee8cSdrh ExprList *pFarg; /* List of function arguments */ 281912ffee8cSdrh int nFarg; /* Number of function arguments */ 282012ffee8cSdrh FuncDef *pDef; /* The function definition object */ 282112ffee8cSdrh int nId; /* Length of the function name in bytes */ 282212ffee8cSdrh const char *zId; /* The function name */ 2823693e6719Sdrh u32 constMask = 0; /* Mask of function arguments that are constant */ 282412ffee8cSdrh int i; /* Loop counter */ 282512ffee8cSdrh u8 enc = ENC(db); /* The text encoding used by this database */ 282612ffee8cSdrh CollSeq *pColl = 0; /* A collating sequence */ 282717435752Sdrh 28286ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2829c5cd1249Sdrh if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 283012ffee8cSdrh pFarg = 0; 283112ffee8cSdrh }else{ 283212ffee8cSdrh pFarg = pExpr->x.pList; 283312ffee8cSdrh } 283412ffee8cSdrh nFarg = pFarg ? pFarg->nExpr : 0; 283533e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 283633e619fcSdrh zId = pExpr->u.zToken; 2837b7916a78Sdrh nId = sqlite3Strlen30(zId); 283812ffee8cSdrh pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0); 28390c4de2d9Sdrh if( pDef==0 || pDef->xFunc==0 ){ 2840feb306f5Sdrh sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId); 2841feb306f5Sdrh break; 2842feb306f5Sdrh } 2843ae6bb957Sdrh 2844ae6bb957Sdrh /* Attempt a direct implementation of the built-in COALESCE() and 284560ec914cSpeter.d.reid ** IFNULL() functions. This avoids unnecessary evaluation of 2846ae6bb957Sdrh ** arguments past the first non-NULL argument. 2847ae6bb957Sdrh */ 2848d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ 2849ae6bb957Sdrh int endCoalesce = sqlite3VdbeMakeLabel(v); 2850ae6bb957Sdrh assert( nFarg>=2 ); 2851ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 2852ae6bb957Sdrh for(i=1; i<nFarg; i++){ 2853ae6bb957Sdrh sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); 2854688852abSdrh VdbeCoverage(v); 2855f49f3523Sdrh sqlite3ExprCacheRemove(pParse, target, 1); 2856ae6bb957Sdrh sqlite3ExprCachePush(pParse); 2857ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); 2858d2490904Sdrh sqlite3ExprCachePop(pParse); 2859ae6bb957Sdrh } 2860ae6bb957Sdrh sqlite3VdbeResolveLabel(v, endCoalesce); 2861ae6bb957Sdrh break; 2862ae6bb957Sdrh } 2863ae6bb957Sdrh 2864cca9f3d2Sdrh /* The UNLIKELY() function is a no-op. The result is the value 2865cca9f3d2Sdrh ** of the first argument. 2866cca9f3d2Sdrh */ 2867cca9f3d2Sdrh if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 2868cca9f3d2Sdrh assert( nFarg>=1 ); 2869cca9f3d2Sdrh sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 2870cca9f3d2Sdrh break; 2871cca9f3d2Sdrh } 2872ae6bb957Sdrh 2873d1a01edaSdrh for(i=0; i<nFarg; i++){ 2874d1a01edaSdrh if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ 2875693e6719Sdrh testcase( i==31 ); 2876693e6719Sdrh constMask |= MASKBIT32(i); 2877d1a01edaSdrh } 2878d1a01edaSdrh if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 2879d1a01edaSdrh pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 2880d1a01edaSdrh } 2881d1a01edaSdrh } 288212ffee8cSdrh if( pFarg ){ 2883d1a01edaSdrh if( constMask ){ 2884d1a01edaSdrh r1 = pParse->nMem+1; 2885d1a01edaSdrh pParse->nMem += nFarg; 2886d1a01edaSdrh }else{ 288712ffee8cSdrh r1 = sqlite3GetTempRange(pParse, nFarg); 2888d1a01edaSdrh } 2889a748fdccSdrh 2890a748fdccSdrh /* For length() and typeof() functions with a column argument, 2891a748fdccSdrh ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG 2892a748fdccSdrh ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data 2893a748fdccSdrh ** loading. 2894a748fdccSdrh */ 2895d36e1041Sdrh if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ 28964e245a4cSdrh u8 exprOp; 2897a748fdccSdrh assert( nFarg==1 ); 2898a748fdccSdrh assert( pFarg->a[0].pExpr!=0 ); 28994e245a4cSdrh exprOp = pFarg->a[0].pExpr->op; 29004e245a4cSdrh if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 2901a748fdccSdrh assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 2902a748fdccSdrh assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 2903b1fba286Sdrh testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); 2904b1fba286Sdrh pFarg->a[0].pExpr->op2 = 2905b1fba286Sdrh pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); 2906a748fdccSdrh } 2907a748fdccSdrh } 2908a748fdccSdrh 2909d7d385ddSdrh sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ 2910d1a01edaSdrh sqlite3ExprCodeExprList(pParse, pFarg, r1, 2911d1a01edaSdrh SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); 2912d2490904Sdrh sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ 2913892d3179Sdrh }else{ 291412ffee8cSdrh r1 = 0; 2915892d3179Sdrh } 2916b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 2917a43fa227Sdrh /* Possibly overload the function if the first argument is 2918a43fa227Sdrh ** a virtual table column. 2919a43fa227Sdrh ** 2920a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 2921a43fa227Sdrh ** second argument, not the first, as the argument to test to 2922a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 2923a43fa227Sdrh ** the left operand of infix functions (the operand we want to 2924a43fa227Sdrh ** control overloading) ends up as the second argument to the 2925a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 2926a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 2927a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 2928a43fa227Sdrh */ 292912ffee8cSdrh if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ 293012ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 293112ffee8cSdrh }else if( nFarg>0 ){ 293212ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 2933b7f6f68fSdrh } 2934b7f6f68fSdrh #endif 2935d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 29368b213899Sdrh if( !pColl ) pColl = db->pDfltColl; 293766a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 2938682f68b0Sdanielk1977 } 29392dcef11bSdrh sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, 294066a5167bSdrh (char*)pDef, P4_FUNCDEF); 294112ffee8cSdrh sqlite3VdbeChangeP5(v, (u8)nFarg); 2942d1a01edaSdrh if( nFarg && constMask==0 ){ 294312ffee8cSdrh sqlite3ReleaseTempRange(pParse, r1, nFarg); 29442dcef11bSdrh } 29456ec2733bSdrh break; 29466ec2733bSdrh } 2947fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 2948fe2093d7Sdrh case TK_EXISTS: 294919a775c2Sdrh case TK_SELECT: { 2950c5499befSdrh testcase( op==TK_EXISTS ); 2951c5499befSdrh testcase( op==TK_SELECT ); 29521450bc6eSdrh inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 295319a775c2Sdrh break; 295419a775c2Sdrh } 2955fef5208cSdrh case TK_IN: { 2956e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 2957e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 2958e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 2959e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 296066ba23ceSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 2961e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 2962e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); 2963e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 2964fef5208cSdrh break; 2965fef5208cSdrh } 2966e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2967e3365e6cSdrh 2968e3365e6cSdrh 29692dcef11bSdrh /* 29702dcef11bSdrh ** x BETWEEN y AND z 29712dcef11bSdrh ** 29722dcef11bSdrh ** This is equivalent to 29732dcef11bSdrh ** 29742dcef11bSdrh ** x>=y AND x<=z 29752dcef11bSdrh ** 29762dcef11bSdrh ** X is stored in pExpr->pLeft. 29772dcef11bSdrh ** Y is stored in pExpr->pList->a[0].pExpr. 29782dcef11bSdrh ** Z is stored in pExpr->pList->a[1].pExpr. 29792dcef11bSdrh */ 2980fef5208cSdrh case TK_BETWEEN: { 2981be5c89acSdrh Expr *pLeft = pExpr->pLeft; 29826ab3a2ecSdanielk1977 struct ExprList_item *pLItem = pExpr->x.pList->a; 2983be5c89acSdrh Expr *pRight = pLItem->pExpr; 298435573356Sdrh 2985b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 2986b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 2987c5499befSdrh testcase( regFree1==0 ); 2988c5499befSdrh testcase( regFree2==0 ); 29892dcef11bSdrh r3 = sqlite3GetTempReg(pParse); 2990678ccce8Sdrh r4 = sqlite3GetTempReg(pParse); 299135573356Sdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 29927d176105Sdrh r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v); 2993be5c89acSdrh pLItem++; 2994be5c89acSdrh pRight = pLItem->pExpr; 29952dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 29962dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 2997c5499befSdrh testcase( regFree2==0 ); 2998678ccce8Sdrh codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); 2999688852abSdrh VdbeCoverage(v); 3000678ccce8Sdrh sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); 30012dcef11bSdrh sqlite3ReleaseTempReg(pParse, r3); 3002678ccce8Sdrh sqlite3ReleaseTempReg(pParse, r4); 3003fef5208cSdrh break; 3004fef5208cSdrh } 3005ae80ddeaSdrh case TK_COLLATE: 30064f07e5fbSdrh case TK_UPLUS: { 30072dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 3008a2e00042Sdrh break; 3009a2e00042Sdrh } 30102dcef11bSdrh 3011165921a7Sdan case TK_TRIGGER: { 301265a7cd16Sdan /* If the opcode is TK_TRIGGER, then the expression is a reference 301365a7cd16Sdan ** to a column in the new.* or old.* pseudo-tables available to 301465a7cd16Sdan ** trigger programs. In this case Expr.iTable is set to 1 for the 301565a7cd16Sdan ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 301665a7cd16Sdan ** is set to the column of the pseudo-table to read, or to -1 to 301765a7cd16Sdan ** read the rowid field. 301865a7cd16Sdan ** 301965a7cd16Sdan ** The expression is implemented using an OP_Param opcode. The p1 302065a7cd16Sdan ** parameter is set to 0 for an old.rowid reference, or to (i+1) 302165a7cd16Sdan ** to reference another column of the old.* pseudo-table, where 302265a7cd16Sdan ** i is the index of the column. For a new.rowid reference, p1 is 302365a7cd16Sdan ** set to (n+1), where n is the number of columns in each pseudo-table. 302465a7cd16Sdan ** For a reference to any other column in the new.* pseudo-table, p1 302565a7cd16Sdan ** is set to (n+2+i), where n and i are as defined previously. For 302665a7cd16Sdan ** example, if the table on which triggers are being fired is 302765a7cd16Sdan ** declared as: 302865a7cd16Sdan ** 302965a7cd16Sdan ** CREATE TABLE t1(a, b); 303065a7cd16Sdan ** 303165a7cd16Sdan ** Then p1 is interpreted as follows: 303265a7cd16Sdan ** 303365a7cd16Sdan ** p1==0 -> old.rowid p1==3 -> new.rowid 303465a7cd16Sdan ** p1==1 -> old.a p1==4 -> new.a 303565a7cd16Sdan ** p1==2 -> old.b p1==5 -> new.b 303665a7cd16Sdan */ 30372832ad42Sdan Table *pTab = pExpr->pTab; 303865a7cd16Sdan int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; 303965a7cd16Sdan 304065a7cd16Sdan assert( pExpr->iTable==0 || pExpr->iTable==1 ); 304165a7cd16Sdan assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); 304265a7cd16Sdan assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); 304365a7cd16Sdan assert( p1>=0 && p1<(pTab->nCol*2+2) ); 304465a7cd16Sdan 304565a7cd16Sdan sqlite3VdbeAddOp2(v, OP_Param, p1, target); 304676d462eeSdan VdbeComment((v, "%s.%s -> $%d", 3047165921a7Sdan (pExpr->iTable ? "new" : "old"), 304876d462eeSdan (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), 304976d462eeSdan target 3050165921a7Sdan )); 305165a7cd16Sdan 305244dbca83Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 305365a7cd16Sdan /* If the column has REAL affinity, it may currently be stored as an 3054113762a2Sdrh ** integer. Use OP_RealAffinity to make sure it is really real. 3055113762a2Sdrh ** 3056113762a2Sdrh ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to 3057113762a2Sdrh ** floating point when extracting it from the record. */ 30582832ad42Sdan if( pExpr->iColumn>=0 30592832ad42Sdan && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL 30602832ad42Sdan ){ 30612832ad42Sdan sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 30622832ad42Sdan } 306344dbca83Sdrh #endif 3064165921a7Sdan break; 3065165921a7Sdan } 3066165921a7Sdan 3067165921a7Sdan 30682dcef11bSdrh /* 30692dcef11bSdrh ** Form A: 30702dcef11bSdrh ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 30712dcef11bSdrh ** 30722dcef11bSdrh ** Form B: 30732dcef11bSdrh ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 30742dcef11bSdrh ** 30752dcef11bSdrh ** Form A is can be transformed into the equivalent form B as follows: 30762dcef11bSdrh ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 30772dcef11bSdrh ** WHEN x=eN THEN rN ELSE y END 30782dcef11bSdrh ** 30792dcef11bSdrh ** X (if it exists) is in pExpr->pLeft. 3080c5cd1249Sdrh ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is 3081c5cd1249Sdrh ** odd. The Y is also optional. If the number of elements in x.pList 3082c5cd1249Sdrh ** is even, then Y is omitted and the "otherwise" result is NULL. 30832dcef11bSdrh ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 30842dcef11bSdrh ** 30852dcef11bSdrh ** The result of the expression is the Ri for the first matching Ei, 30862dcef11bSdrh ** or if there is no matching Ei, the ELSE term Y, or if there is 30872dcef11bSdrh ** no ELSE term, NULL. 30882dcef11bSdrh */ 308933cd4909Sdrh default: assert( op==TK_CASE ); { 30902dcef11bSdrh int endLabel; /* GOTO label for end of CASE stmt */ 30912dcef11bSdrh int nextCase; /* GOTO label for next WHEN clause */ 30922dcef11bSdrh int nExpr; /* 2x number of WHEN terms */ 30932dcef11bSdrh int i; /* Loop counter */ 30942dcef11bSdrh ExprList *pEList; /* List of WHEN terms */ 30952dcef11bSdrh struct ExprList_item *aListelem; /* Array of WHEN terms */ 30962dcef11bSdrh Expr opCompare; /* The X==Ei expression */ 30972dcef11bSdrh Expr *pX; /* The X expression */ 30981bd10f8aSdrh Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ 3099ceea3321Sdrh VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) 310017a7f8ddSdrh 31016ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); 31026ab3a2ecSdanielk1977 assert(pExpr->x.pList->nExpr > 0); 31036ab3a2ecSdanielk1977 pEList = pExpr->x.pList; 3104be5c89acSdrh aListelem = pEList->a; 3105be5c89acSdrh nExpr = pEList->nExpr; 31062dcef11bSdrh endLabel = sqlite3VdbeMakeLabel(v); 31072dcef11bSdrh if( (pX = pExpr->pLeft)!=0 ){ 310810d1edf0Sdrh tempX = *pX; 310933cd4909Sdrh testcase( pX->op==TK_COLUMN ); 311010d1edf0Sdrh exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); 3111c5499befSdrh testcase( regFree1==0 ); 31122dcef11bSdrh opCompare.op = TK_EQ; 311310d1edf0Sdrh opCompare.pLeft = &tempX; 31142dcef11bSdrh pTest = &opCompare; 31158b1db07fSdrh /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: 31168b1db07fSdrh ** The value in regFree1 might get SCopy-ed into the file result. 31178b1db07fSdrh ** So make sure that the regFree1 register is not reused for other 31188b1db07fSdrh ** purposes and possibly overwritten. */ 31198b1db07fSdrh regFree1 = 0; 3120cce7d176Sdrh } 3121c5cd1249Sdrh for(i=0; i<nExpr-1; i=i+2){ 3122ceea3321Sdrh sqlite3ExprCachePush(pParse); 31232dcef11bSdrh if( pX ){ 31241bd10f8aSdrh assert( pTest!=0 ); 31252dcef11bSdrh opCompare.pRight = aListelem[i].pExpr; 3126f5905aa7Sdrh }else{ 31272dcef11bSdrh pTest = aListelem[i].pExpr; 312817a7f8ddSdrh } 31292dcef11bSdrh nextCase = sqlite3VdbeMakeLabel(v); 313033cd4909Sdrh testcase( pTest->op==TK_COLUMN ); 31312dcef11bSdrh sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 3132c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 31339de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 31342dcef11bSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); 3135d2490904Sdrh sqlite3ExprCachePop(pParse); 31362dcef11bSdrh sqlite3VdbeResolveLabel(v, nextCase); 3137f570f011Sdrh } 3138c5cd1249Sdrh if( (nExpr&1)!=0 ){ 3139ceea3321Sdrh sqlite3ExprCachePush(pParse); 3140c5cd1249Sdrh sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 3141d2490904Sdrh sqlite3ExprCachePop(pParse); 314217a7f8ddSdrh }else{ 31439de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 314417a7f8ddSdrh } 3145c1f4a19bSdanielk1977 assert( db->mallocFailed || pParse->nErr>0 3146c1f4a19bSdanielk1977 || pParse->iCacheLevel==iCacheLevel ); 31472dcef11bSdrh sqlite3VdbeResolveLabel(v, endLabel); 31486f34903eSdanielk1977 break; 31496f34903eSdanielk1977 } 31505338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 31516f34903eSdanielk1977 case TK_RAISE: { 3152165921a7Sdan assert( pExpr->affinity==OE_Rollback 3153165921a7Sdan || pExpr->affinity==OE_Abort 3154165921a7Sdan || pExpr->affinity==OE_Fail 3155165921a7Sdan || pExpr->affinity==OE_Ignore 3156165921a7Sdan ); 3157e0af83acSdan if( !pParse->pTriggerTab ){ 3158e0af83acSdan sqlite3ErrorMsg(pParse, 3159e0af83acSdan "RAISE() may only be used within a trigger-program"); 3160e0af83acSdan return 0; 3161e0af83acSdan } 3162e0af83acSdan if( pExpr->affinity==OE_Abort ){ 3163e0af83acSdan sqlite3MayAbort(pParse); 3164e0af83acSdan } 316533e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3166e0af83acSdan if( pExpr->affinity==OE_Ignore ){ 3167e0af83acSdan sqlite3VdbeAddOp4( 3168e0af83acSdan v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); 3169688852abSdrh VdbeCoverage(v); 3170e0af83acSdan }else{ 3171433dccfbSdrh sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, 3172f9c8ce3cSdrh pExpr->affinity, pExpr->u.zToken, 0, 0); 3173e0af83acSdan } 3174e0af83acSdan 3175ffe07b2dSdrh break; 317617a7f8ddSdrh } 31775338a5f7Sdanielk1977 #endif 3178ffe07b2dSdrh } 31792dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 31802dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 31812dcef11bSdrh return inReg; 31825b6afba9Sdrh } 31832dcef11bSdrh 31842dcef11bSdrh /* 3185d1a01edaSdrh ** Factor out the code of the given expression to initialization time. 3186d1a01edaSdrh */ 3187d673cddaSdrh void sqlite3ExprCodeAtInit( 3188d673cddaSdrh Parse *pParse, /* Parsing context */ 3189d673cddaSdrh Expr *pExpr, /* The expression to code when the VDBE initializes */ 3190d673cddaSdrh int regDest, /* Store the value in this register */ 3191d673cddaSdrh u8 reusable /* True if this expression is reusable */ 3192d673cddaSdrh ){ 3193d1a01edaSdrh ExprList *p; 3194d9f158e7Sdrh assert( ConstFactorOk(pParse) ); 3195d1a01edaSdrh p = pParse->pConstExpr; 3196d1a01edaSdrh pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); 3197d1a01edaSdrh p = sqlite3ExprListAppend(pParse, p, pExpr); 3198d673cddaSdrh if( p ){ 3199d673cddaSdrh struct ExprList_item *pItem = &p->a[p->nExpr-1]; 3200d673cddaSdrh pItem->u.iConstExprReg = regDest; 3201d673cddaSdrh pItem->reusable = reusable; 3202d673cddaSdrh } 3203d1a01edaSdrh pParse->pConstExpr = p; 3204d1a01edaSdrh } 3205d1a01edaSdrh 3206d1a01edaSdrh /* 32072dcef11bSdrh ** Generate code to evaluate an expression and store the results 32082dcef11bSdrh ** into a register. Return the register number where the results 32092dcef11bSdrh ** are stored. 32102dcef11bSdrh ** 32112dcef11bSdrh ** If the register is a temporary register that can be deallocated, 3212678ccce8Sdrh ** then write its number into *pReg. If the result register is not 32132dcef11bSdrh ** a temporary, then set *pReg to zero. 3214f30a969bSdrh ** 3215f30a969bSdrh ** If pExpr is a constant, then this routine might generate this 3216f30a969bSdrh ** code to fill the register in the initialization section of the 3217f30a969bSdrh ** VDBE program, in order to factor it out of the evaluation loop. 32182dcef11bSdrh */ 32192dcef11bSdrh int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 3220f30a969bSdrh int r2; 3221f30a969bSdrh pExpr = sqlite3ExprSkipCollate(pExpr); 3222d9f158e7Sdrh if( ConstFactorOk(pParse) 3223f30a969bSdrh && pExpr->op!=TK_REGISTER 3224f30a969bSdrh && sqlite3ExprIsConstantNotJoin(pExpr) 3225f30a969bSdrh ){ 3226f30a969bSdrh ExprList *p = pParse->pConstExpr; 3227f30a969bSdrh int i; 3228f30a969bSdrh *pReg = 0; 3229f30a969bSdrh if( p ){ 3230d673cddaSdrh struct ExprList_item *pItem; 3231d673cddaSdrh for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ 3232d673cddaSdrh if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ 3233d673cddaSdrh return pItem->u.iConstExprReg; 3234f30a969bSdrh } 3235f30a969bSdrh } 3236f30a969bSdrh } 3237f30a969bSdrh r2 = ++pParse->nMem; 3238d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); 3239f30a969bSdrh }else{ 32402dcef11bSdrh int r1 = sqlite3GetTempReg(pParse); 3241f30a969bSdrh r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 32422dcef11bSdrh if( r2==r1 ){ 32432dcef11bSdrh *pReg = r1; 32442dcef11bSdrh }else{ 32452dcef11bSdrh sqlite3ReleaseTempReg(pParse, r1); 32462dcef11bSdrh *pReg = 0; 32472dcef11bSdrh } 3248f30a969bSdrh } 32492dcef11bSdrh return r2; 32502dcef11bSdrh } 32512dcef11bSdrh 32522dcef11bSdrh /* 32532dcef11bSdrh ** Generate code that will evaluate expression pExpr and store the 32542dcef11bSdrh ** results in register target. The results are guaranteed to appear 32552dcef11bSdrh ** in register target. 32562dcef11bSdrh */ 325705a86c5cSdrh void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 32589cbf3425Sdrh int inReg; 32599cbf3425Sdrh 32609cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 3261ebc16717Sdrh if( pExpr && pExpr->op==TK_REGISTER ){ 3262ebc16717Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); 3263ebc16717Sdrh }else{ 32649cbf3425Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 32650e359b30Sdrh assert( pParse->pVdbe || pParse->db->mallocFailed ); 32660e359b30Sdrh if( inReg!=target && pParse->pVdbe ){ 32679cbf3425Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 326817a7f8ddSdrh } 3269ebc16717Sdrh } 327005a86c5cSdrh } 327105a86c5cSdrh 327205a86c5cSdrh /* 327305a86c5cSdrh ** Generate code that will evaluate expression pExpr and store the 327405a86c5cSdrh ** results in register target. The results are guaranteed to appear 327505a86c5cSdrh ** in register target. If the expression is constant, then this routine 327605a86c5cSdrh ** might choose to code the expression at initialization time. 327705a86c5cSdrh */ 327805a86c5cSdrh void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 327905a86c5cSdrh if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ 328005a86c5cSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); 328105a86c5cSdrh }else{ 328205a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 328305a86c5cSdrh } 3284cce7d176Sdrh } 3285cce7d176Sdrh 3286cce7d176Sdrh /* 328760ec914cSpeter.d.reid ** Generate code that evaluates the given expression and puts the result 3288de4fcfddSdrh ** in register target. 328925303780Sdrh ** 32902dcef11bSdrh ** Also make a copy of the expression results into another "cache" register 32912dcef11bSdrh ** and modify the expression so that the next time it is evaluated, 32922dcef11bSdrh ** the result is a copy of the cache register. 32932dcef11bSdrh ** 32942dcef11bSdrh ** This routine is used for expressions that are used multiple 32952dcef11bSdrh ** times. They are evaluated once and the results of the expression 32962dcef11bSdrh ** are reused. 329725303780Sdrh */ 329805a86c5cSdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 329925303780Sdrh Vdbe *v = pParse->pVdbe; 330025303780Sdrh int iMem; 330105a86c5cSdrh 330205a86c5cSdrh assert( target>0 ); 330305a86c5cSdrh assert( pExpr->op!=TK_REGISTER ); 330405a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 33052dcef11bSdrh iMem = ++pParse->nMem; 330605a86c5cSdrh sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); 3307a4c3c87eSdrh exprToRegister(pExpr, iMem); 330825303780Sdrh } 33092dcef11bSdrh 33104fa4a54fSdrh #ifdef SQLITE_DEBUG 33117e02e5e6Sdrh /* 33127e02e5e6Sdrh ** Generate a human-readable explanation of an expression tree. 33137e02e5e6Sdrh */ 33144fa4a54fSdrh void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 moreToFollow){ 3315a84203a0Sdrh const char *zBinOp = 0; /* Binary operator */ 3316a84203a0Sdrh const char *zUniOp = 0; /* Unary operator */ 33174fa4a54fSdrh pView = sqlite3TreeViewPush(pView, moreToFollow); 3318a84203a0Sdrh if( pExpr==0 ){ 33194fa4a54fSdrh sqlite3TreeViewLine(pView, "nil"); 33204fa4a54fSdrh sqlite3TreeViewPop(pView); 33214fa4a54fSdrh return; 33227e02e5e6Sdrh } 33234fa4a54fSdrh switch( pExpr->op ){ 3324a84203a0Sdrh case TK_AGG_COLUMN: { 33254fa4a54fSdrh sqlite3TreeViewLine(pView, "AGG{%d:%d}", 332604b8342bSdrh pExpr->iTable, pExpr->iColumn); 3327a84203a0Sdrh break; 33287e02e5e6Sdrh } 3329a84203a0Sdrh case TK_COLUMN: { 3330a84203a0Sdrh if( pExpr->iTable<0 ){ 3331a84203a0Sdrh /* This only happens when coding check constraints */ 33324fa4a54fSdrh sqlite3TreeViewLine(pView, "COLUMN(%d)", pExpr->iColumn); 3333a84203a0Sdrh }else{ 33344fa4a54fSdrh sqlite3TreeViewLine(pView, "{%d:%d}", 333504b8342bSdrh pExpr->iTable, pExpr->iColumn); 3336a84203a0Sdrh } 3337a84203a0Sdrh break; 3338a84203a0Sdrh } 3339a84203a0Sdrh case TK_INTEGER: { 3340a84203a0Sdrh if( pExpr->flags & EP_IntValue ){ 33414fa4a54fSdrh sqlite3TreeViewLine(pView, "%d", pExpr->u.iValue); 3342a84203a0Sdrh }else{ 33434fa4a54fSdrh sqlite3TreeViewLine(pView, "%s", pExpr->u.zToken); 3344a84203a0Sdrh } 3345a84203a0Sdrh break; 3346a84203a0Sdrh } 3347a84203a0Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 3348a84203a0Sdrh case TK_FLOAT: { 33494fa4a54fSdrh sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); 3350a84203a0Sdrh break; 3351a84203a0Sdrh } 3352a84203a0Sdrh #endif 3353a84203a0Sdrh case TK_STRING: { 33544fa4a54fSdrh sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken); 3355a84203a0Sdrh break; 3356a84203a0Sdrh } 3357a84203a0Sdrh case TK_NULL: { 33584fa4a54fSdrh sqlite3TreeViewLine(pView,"NULL"); 3359a84203a0Sdrh break; 3360a84203a0Sdrh } 3361a84203a0Sdrh #ifndef SQLITE_OMIT_BLOB_LITERAL 3362a84203a0Sdrh case TK_BLOB: { 33634fa4a54fSdrh sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken); 3364a84203a0Sdrh break; 3365a84203a0Sdrh } 3366a84203a0Sdrh #endif 3367a84203a0Sdrh case TK_VARIABLE: { 33684fa4a54fSdrh sqlite3TreeViewLine(pView,"VARIABLE(%s,%d)", 3369a84203a0Sdrh pExpr->u.zToken, pExpr->iColumn); 3370a84203a0Sdrh break; 3371a84203a0Sdrh } 3372a84203a0Sdrh case TK_REGISTER: { 33734fa4a54fSdrh sqlite3TreeViewLine(pView,"REGISTER(%d)", pExpr->iTable); 3374a84203a0Sdrh break; 3375a84203a0Sdrh } 3376a84203a0Sdrh case TK_AS: { 33774fa4a54fSdrh sqlite3TreeViewLine(pView,"AS %Q", pExpr->u.zToken); 33784fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); 33794fa4a54fSdrh break; 33804fa4a54fSdrh } 33814fa4a54fSdrh case TK_ID: { 33824fa4a54fSdrh sqlite3TreeViewLine(pView,"ID %Q", pExpr->u.zToken); 3383a84203a0Sdrh break; 3384a84203a0Sdrh } 3385a84203a0Sdrh #ifndef SQLITE_OMIT_CAST 3386a84203a0Sdrh case TK_CAST: { 3387a84203a0Sdrh /* Expressions of the form: CAST(pLeft AS token) */ 33884fa4a54fSdrh sqlite3TreeViewLine(pView,"CAST %Q", pExpr->u.zToken); 33894fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); 3390a84203a0Sdrh break; 3391a84203a0Sdrh } 3392a84203a0Sdrh #endif /* SQLITE_OMIT_CAST */ 3393a84203a0Sdrh case TK_LT: zBinOp = "LT"; break; 3394a84203a0Sdrh case TK_LE: zBinOp = "LE"; break; 3395a84203a0Sdrh case TK_GT: zBinOp = "GT"; break; 3396a84203a0Sdrh case TK_GE: zBinOp = "GE"; break; 3397a84203a0Sdrh case TK_NE: zBinOp = "NE"; break; 3398a84203a0Sdrh case TK_EQ: zBinOp = "EQ"; break; 3399a84203a0Sdrh case TK_IS: zBinOp = "IS"; break; 3400a84203a0Sdrh case TK_ISNOT: zBinOp = "ISNOT"; break; 3401a84203a0Sdrh case TK_AND: zBinOp = "AND"; break; 3402a84203a0Sdrh case TK_OR: zBinOp = "OR"; break; 3403a84203a0Sdrh case TK_PLUS: zBinOp = "ADD"; break; 3404a84203a0Sdrh case TK_STAR: zBinOp = "MUL"; break; 3405a84203a0Sdrh case TK_MINUS: zBinOp = "SUB"; break; 3406a84203a0Sdrh case TK_REM: zBinOp = "REM"; break; 3407a84203a0Sdrh case TK_BITAND: zBinOp = "BITAND"; break; 3408a84203a0Sdrh case TK_BITOR: zBinOp = "BITOR"; break; 3409a84203a0Sdrh case TK_SLASH: zBinOp = "DIV"; break; 3410a84203a0Sdrh case TK_LSHIFT: zBinOp = "LSHIFT"; break; 3411a84203a0Sdrh case TK_RSHIFT: zBinOp = "RSHIFT"; break; 3412a84203a0Sdrh case TK_CONCAT: zBinOp = "CONCAT"; break; 3413ccaba81eSdrh case TK_DOT: zBinOp = "DOT"; break; 3414a84203a0Sdrh 3415a84203a0Sdrh case TK_UMINUS: zUniOp = "UMINUS"; break; 3416a84203a0Sdrh case TK_UPLUS: zUniOp = "UPLUS"; break; 3417a84203a0Sdrh case TK_BITNOT: zUniOp = "BITNOT"; break; 3418a84203a0Sdrh case TK_NOT: zUniOp = "NOT"; break; 3419a84203a0Sdrh case TK_ISNULL: zUniOp = "ISNULL"; break; 3420a84203a0Sdrh case TK_NOTNULL: zUniOp = "NOTNULL"; break; 3421a84203a0Sdrh 34227a66da13Sdrh case TK_COLLATE: { 34234fa4a54fSdrh sqlite3TreeViewLine(pView, "COLLATE %Q", pExpr->u.zToken); 34244fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); 34257a66da13Sdrh break; 34267a66da13Sdrh } 34277a66da13Sdrh 3428a84203a0Sdrh case TK_AGG_FUNCTION: 3429a84203a0Sdrh case TK_FUNCTION: { 3430a84203a0Sdrh ExprList *pFarg; /* List of function arguments */ 3431c5cd1249Sdrh if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 3432a84203a0Sdrh pFarg = 0; 3433a84203a0Sdrh }else{ 3434a84203a0Sdrh pFarg = pExpr->x.pList; 3435a84203a0Sdrh } 34364fa4a54fSdrh if( pExpr->op==TK_AGG_FUNCTION ){ 34374fa4a54fSdrh sqlite3TreeViewLine(pView, "AGG_FUNCTION%d %Q", 3438ed551b95Sdrh pExpr->op2, pExpr->u.zToken); 3439ed551b95Sdrh }else{ 34404fa4a54fSdrh sqlite3TreeViewLine(pView, "FUNCTION %Q", pExpr->u.zToken); 3441ed551b95Sdrh } 3442a84203a0Sdrh if( pFarg ){ 34434fa4a54fSdrh sqlite3TreeViewExprList(pView, pFarg, 0, 0); 34447e02e5e6Sdrh } 3445a84203a0Sdrh break; 3446a84203a0Sdrh } 3447a84203a0Sdrh #ifndef SQLITE_OMIT_SUBQUERY 3448a84203a0Sdrh case TK_EXISTS: { 34494fa4a54fSdrh sqlite3TreeViewLine(pView, "EXISTS-expr"); 34504fa4a54fSdrh sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); 3451a84203a0Sdrh break; 3452a84203a0Sdrh } 3453a84203a0Sdrh case TK_SELECT: { 34544fa4a54fSdrh sqlite3TreeViewLine(pView, "SELECT-expr"); 34554fa4a54fSdrh sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); 3456a84203a0Sdrh break; 3457a84203a0Sdrh } 3458a84203a0Sdrh case TK_IN: { 34594fa4a54fSdrh sqlite3TreeViewLine(pView, "IN"); 34604fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); 3461a84203a0Sdrh if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 34624fa4a54fSdrh sqlite3TreeViewSelect(pView, pExpr->x.pSelect, 0); 3463a84203a0Sdrh }else{ 34644fa4a54fSdrh sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); 3465a84203a0Sdrh } 3466a84203a0Sdrh break; 3467a84203a0Sdrh } 3468a84203a0Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 3469a84203a0Sdrh 3470a84203a0Sdrh /* 3471a84203a0Sdrh ** x BETWEEN y AND z 3472a84203a0Sdrh ** 3473a84203a0Sdrh ** This is equivalent to 3474a84203a0Sdrh ** 3475a84203a0Sdrh ** x>=y AND x<=z 3476a84203a0Sdrh ** 3477a84203a0Sdrh ** X is stored in pExpr->pLeft. 3478a84203a0Sdrh ** Y is stored in pExpr->pList->a[0].pExpr. 3479a84203a0Sdrh ** Z is stored in pExpr->pList->a[1].pExpr. 3480a84203a0Sdrh */ 3481a84203a0Sdrh case TK_BETWEEN: { 3482a84203a0Sdrh Expr *pX = pExpr->pLeft; 3483a84203a0Sdrh Expr *pY = pExpr->x.pList->a[0].pExpr; 3484a84203a0Sdrh Expr *pZ = pExpr->x.pList->a[1].pExpr; 34854fa4a54fSdrh sqlite3TreeViewLine(pView, "BETWEEN"); 34864fa4a54fSdrh sqlite3TreeViewExpr(pView, pX, 1); 34874fa4a54fSdrh sqlite3TreeViewExpr(pView, pY, 1); 34884fa4a54fSdrh sqlite3TreeViewExpr(pView, pZ, 0); 3489a84203a0Sdrh break; 3490a84203a0Sdrh } 3491a84203a0Sdrh case TK_TRIGGER: { 3492a84203a0Sdrh /* If the opcode is TK_TRIGGER, then the expression is a reference 3493a84203a0Sdrh ** to a column in the new.* or old.* pseudo-tables available to 3494a84203a0Sdrh ** trigger programs. In this case Expr.iTable is set to 1 for the 3495a84203a0Sdrh ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 3496a84203a0Sdrh ** is set to the column of the pseudo-table to read, or to -1 to 3497a84203a0Sdrh ** read the rowid field. 3498a84203a0Sdrh */ 34994fa4a54fSdrh sqlite3TreeViewLine(pView, "%s(%d)", 3500a84203a0Sdrh pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn); 3501a84203a0Sdrh break; 3502a84203a0Sdrh } 3503a84203a0Sdrh case TK_CASE: { 35044fa4a54fSdrh sqlite3TreeViewLine(pView, "CASE"); 35054fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); 35064fa4a54fSdrh sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, 0); 3507a84203a0Sdrh break; 3508a84203a0Sdrh } 3509a84203a0Sdrh #ifndef SQLITE_OMIT_TRIGGER 3510a84203a0Sdrh case TK_RAISE: { 3511a84203a0Sdrh const char *zType = "unk"; 3512a84203a0Sdrh switch( pExpr->affinity ){ 3513a84203a0Sdrh case OE_Rollback: zType = "rollback"; break; 3514a84203a0Sdrh case OE_Abort: zType = "abort"; break; 3515a84203a0Sdrh case OE_Fail: zType = "fail"; break; 3516a84203a0Sdrh case OE_Ignore: zType = "ignore"; break; 3517a84203a0Sdrh } 35184fa4a54fSdrh sqlite3TreeViewLine(pView, "RAISE %s(%Q)", zType, pExpr->u.zToken); 3519a84203a0Sdrh break; 3520a84203a0Sdrh } 3521a84203a0Sdrh #endif 35224fa4a54fSdrh default: { 35234fa4a54fSdrh sqlite3TreeViewLine(pView, "op=%d", pExpr->op); 35244fa4a54fSdrh break; 35254fa4a54fSdrh } 3526a84203a0Sdrh } 3527a84203a0Sdrh if( zBinOp ){ 35284fa4a54fSdrh sqlite3TreeViewLine(pView, "%s", zBinOp); 35294fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pLeft, 1); 35304fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pRight, 0); 3531a84203a0Sdrh }else if( zUniOp ){ 35324fa4a54fSdrh sqlite3TreeViewLine(pView, "%s", zUniOp); 35334fa4a54fSdrh sqlite3TreeViewExpr(pView, pExpr->pLeft, 0); 3534a84203a0Sdrh } 35354fa4a54fSdrh sqlite3TreeViewPop(pView); 35367e02e5e6Sdrh } 35374fa4a54fSdrh #endif /* SQLITE_DEBUG */ 35387e02e5e6Sdrh 35394fa4a54fSdrh #ifdef SQLITE_DEBUG 35407e02e5e6Sdrh /* 35417e02e5e6Sdrh ** Generate a human-readable explanation of an expression list. 35427e02e5e6Sdrh */ 35434fa4a54fSdrh void sqlite3TreeViewExprList( 35444fa4a54fSdrh TreeView *pView, 35454fa4a54fSdrh const ExprList *pList, 35464fa4a54fSdrh u8 moreToFollow, 35474fa4a54fSdrh const char *zLabel 35484fa4a54fSdrh ){ 35497e02e5e6Sdrh int i; 35504fa4a54fSdrh pView = sqlite3TreeViewPush(pView, moreToFollow); 35514fa4a54fSdrh if( zLabel==0 || zLabel[0]==0 ) zLabel = "LIST"; 35524fa4a54fSdrh if( pList==0 ){ 35534fa4a54fSdrh sqlite3TreeViewLine(pView, "%s (empty)", zLabel); 3554a84203a0Sdrh }else{ 35554fa4a54fSdrh sqlite3TreeViewLine(pView, "%s", zLabel); 35567e02e5e6Sdrh for(i=0; i<pList->nExpr; i++){ 35574fa4a54fSdrh sqlite3TreeViewExpr(pView, pList->a[i].pExpr, i<pList->nExpr-1); 35584fa4a54fSdrh #if 0 35593e3f1a5bSdrh if( pList->a[i].zName ){ 35603e3f1a5bSdrh sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName); 35613e3f1a5bSdrh } 35623e3f1a5bSdrh if( pList->a[i].bSpanIsTab ){ 35633e3f1a5bSdrh sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan); 35643e3f1a5bSdrh } 35654fa4a54fSdrh #endif 35667e02e5e6Sdrh } 35677e02e5e6Sdrh } 35684fa4a54fSdrh sqlite3TreeViewPop(pView); 3569a84203a0Sdrh } 35707e02e5e6Sdrh #endif /* SQLITE_DEBUG */ 35717e02e5e6Sdrh 3572678ccce8Sdrh /* 3573268380caSdrh ** Generate code that pushes the value of every element of the given 35749cbf3425Sdrh ** expression list into a sequence of registers beginning at target. 3575268380caSdrh ** 3576892d3179Sdrh ** Return the number of elements evaluated. 3577d1a01edaSdrh ** 3578d1a01edaSdrh ** The SQLITE_ECEL_DUP flag prevents the arguments from being 3579d1a01edaSdrh ** filled using OP_SCopy. OP_Copy must be used instead. 3580d1a01edaSdrh ** 3581d1a01edaSdrh ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 3582d1a01edaSdrh ** factored out into initialization code. 3583268380caSdrh */ 35844adee20fSdanielk1977 int sqlite3ExprCodeExprList( 3585268380caSdrh Parse *pParse, /* Parsing context */ 3586389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 3587191b54cbSdrh int target, /* Where to write results */ 3588d1a01edaSdrh u8 flags /* SQLITE_ECEL_* flags */ 3589268380caSdrh ){ 3590268380caSdrh struct ExprList_item *pItem; 35919cbf3425Sdrh int i, n; 3592d1a01edaSdrh u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 35939d8b3072Sdrh assert( pList!=0 ); 35949cbf3425Sdrh assert( target>0 ); 3595d81a142bSdrh assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 3596268380caSdrh n = pList->nExpr; 3597d9f158e7Sdrh if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 3598191b54cbSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 35997445ffe2Sdrh Expr *pExpr = pItem->pExpr; 3600d1a01edaSdrh if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ 3601d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); 3602d1a01edaSdrh }else{ 36037445ffe2Sdrh int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 3604746fd9ccSdrh if( inReg!=target+i ){ 36054eded604Sdrh VdbeOp *pOp; 36064eded604Sdrh Vdbe *v = pParse->pVdbe; 36074eded604Sdrh if( copyOp==OP_Copy 36084eded604Sdrh && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy 36094eded604Sdrh && pOp->p1+pOp->p3+1==inReg 36104eded604Sdrh && pOp->p2+pOp->p3+1==target+i 36114eded604Sdrh ){ 36124eded604Sdrh pOp->p3++; 36134eded604Sdrh }else{ 36144eded604Sdrh sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 36154eded604Sdrh } 3616d1a01edaSdrh } 3617d176611bSdrh } 3618268380caSdrh } 3619f9b596ebSdrh return n; 3620268380caSdrh } 3621268380caSdrh 3622268380caSdrh /* 362336c563a2Sdrh ** Generate code for a BETWEEN operator. 362436c563a2Sdrh ** 362536c563a2Sdrh ** x BETWEEN y AND z 362636c563a2Sdrh ** 362736c563a2Sdrh ** The above is equivalent to 362836c563a2Sdrh ** 362936c563a2Sdrh ** x>=y AND x<=z 363036c563a2Sdrh ** 363136c563a2Sdrh ** Code it as such, taking care to do the common subexpression 363260ec914cSpeter.d.reid ** elimination of x. 363336c563a2Sdrh */ 363436c563a2Sdrh static void exprCodeBetween( 363536c563a2Sdrh Parse *pParse, /* Parsing and code generating context */ 363636c563a2Sdrh Expr *pExpr, /* The BETWEEN expression */ 363736c563a2Sdrh int dest, /* Jump here if the jump is taken */ 363836c563a2Sdrh int jumpIfTrue, /* Take the jump if the BETWEEN is true */ 363936c563a2Sdrh int jumpIfNull /* Take the jump if the BETWEEN is NULL */ 364036c563a2Sdrh ){ 364136c563a2Sdrh Expr exprAnd; /* The AND operator in x>=y AND x<=z */ 364236c563a2Sdrh Expr compLeft; /* The x>=y term */ 364336c563a2Sdrh Expr compRight; /* The x<=z term */ 364436c563a2Sdrh Expr exprX; /* The x subexpression */ 364536c563a2Sdrh int regFree1 = 0; /* Temporary use register */ 364636c563a2Sdrh 364736c563a2Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 364836c563a2Sdrh exprX = *pExpr->pLeft; 364936c563a2Sdrh exprAnd.op = TK_AND; 365036c563a2Sdrh exprAnd.pLeft = &compLeft; 365136c563a2Sdrh exprAnd.pRight = &compRight; 365236c563a2Sdrh compLeft.op = TK_GE; 365336c563a2Sdrh compLeft.pLeft = &exprX; 365436c563a2Sdrh compLeft.pRight = pExpr->x.pList->a[0].pExpr; 365536c563a2Sdrh compRight.op = TK_LE; 365636c563a2Sdrh compRight.pLeft = &exprX; 365736c563a2Sdrh compRight.pRight = pExpr->x.pList->a[1].pExpr; 3658a4c3c87eSdrh exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); 365936c563a2Sdrh if( jumpIfTrue ){ 366036c563a2Sdrh sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); 366136c563a2Sdrh }else{ 366236c563a2Sdrh sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); 366336c563a2Sdrh } 366436c563a2Sdrh sqlite3ReleaseTempReg(pParse, regFree1); 366536c563a2Sdrh 366636c563a2Sdrh /* Ensure adequate test coverage */ 366736c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); 366836c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); 366936c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); 367036c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); 367136c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 ); 367236c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 ); 367336c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 ); 367436c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 ); 367536c563a2Sdrh } 367636c563a2Sdrh 367736c563a2Sdrh /* 3678cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 3679cce7d176Sdrh ** to the label "dest" if the expression is true but execution 3680cce7d176Sdrh ** continues straight thru if the expression is false. 3681f5905aa7Sdrh ** 3682f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 368335573356Sdrh ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 3684f2bc013cSdrh ** 3685f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 3686f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 3687f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 3688f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 3689f2bc013cSdrh ** below verify that the numbers are aligned correctly. 3690cce7d176Sdrh */ 36914adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3692cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3693cce7d176Sdrh int op = 0; 36942dcef11bSdrh int regFree1 = 0; 36952dcef11bSdrh int regFree2 = 0; 36962dcef11bSdrh int r1, r2; 36972dcef11bSdrh 369835573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 369948864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 370033cd4909Sdrh if( NEVER(pExpr==0) ) return; /* No way this can happen */ 3701f2bc013cSdrh op = pExpr->op; 3702f2bc013cSdrh switch( op ){ 3703cce7d176Sdrh case TK_AND: { 37044adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3705c5499befSdrh testcase( jumpIfNull==0 ); 370635573356Sdrh sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 370754e2adb5Sdrh sqlite3ExprCachePush(pParse); 37084adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 37094adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3710d2490904Sdrh sqlite3ExprCachePop(pParse); 3711cce7d176Sdrh break; 3712cce7d176Sdrh } 3713cce7d176Sdrh case TK_OR: { 3714c5499befSdrh testcase( jumpIfNull==0 ); 37154adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 371654e2adb5Sdrh sqlite3ExprCachePush(pParse); 37174adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 3718d2490904Sdrh sqlite3ExprCachePop(pParse); 3719cce7d176Sdrh break; 3720cce7d176Sdrh } 3721cce7d176Sdrh case TK_NOT: { 3722c5499befSdrh testcase( jumpIfNull==0 ); 37234adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 3724cce7d176Sdrh break; 3725cce7d176Sdrh } 3726cce7d176Sdrh case TK_LT: 3727cce7d176Sdrh case TK_LE: 3728cce7d176Sdrh case TK_GT: 3729cce7d176Sdrh case TK_GE: 3730cce7d176Sdrh case TK_NE: 37310ac65892Sdrh case TK_EQ: { 3732c5499befSdrh testcase( jumpIfNull==0 ); 3733b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3734b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 373535573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 37367d176105Sdrh r1, r2, dest, jumpIfNull); 37377d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 37387d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 37397d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 37407d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 37417d176105Sdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 37427d176105Sdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 3743c5499befSdrh testcase( regFree1==0 ); 3744c5499befSdrh testcase( regFree2==0 ); 3745cce7d176Sdrh break; 3746cce7d176Sdrh } 37476a2fe093Sdrh case TK_IS: 37486a2fe093Sdrh case TK_ISNOT: { 37496a2fe093Sdrh testcase( op==TK_IS ); 37506a2fe093Sdrh testcase( op==TK_ISNOT ); 3751b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3752b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 37536a2fe093Sdrh op = (op==TK_IS) ? TK_EQ : TK_NE; 37546a2fe093Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 37557d176105Sdrh r1, r2, dest, SQLITE_NULLEQ); 37567d176105Sdrh VdbeCoverageIf(v, op==TK_EQ); 37577d176105Sdrh VdbeCoverageIf(v, op==TK_NE); 37586a2fe093Sdrh testcase( regFree1==0 ); 37596a2fe093Sdrh testcase( regFree2==0 ); 37606a2fe093Sdrh break; 37616a2fe093Sdrh } 3762cce7d176Sdrh case TK_ISNULL: 3763cce7d176Sdrh case TK_NOTNULL: { 37647d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 37657d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 37662dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 37677d176105Sdrh sqlite3VdbeAddOp2(v, op, r1, dest); 37687d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 37697d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3770c5499befSdrh testcase( regFree1==0 ); 3771cce7d176Sdrh break; 3772cce7d176Sdrh } 3773fef5208cSdrh case TK_BETWEEN: { 37745c03f30aSdrh testcase( jumpIfNull==0 ); 377536c563a2Sdrh exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); 3776fef5208cSdrh break; 3777fef5208cSdrh } 3778bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 3779e3365e6cSdrh case TK_IN: { 3780e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3781e3365e6cSdrh int destIfNull = jumpIfNull ? dest : destIfFalse; 3782e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 3783e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); 3784e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3785e3365e6cSdrh break; 3786e3365e6cSdrh } 3787bb201344Sshaneh #endif 3788cce7d176Sdrh default: { 3789991a1985Sdrh if( exprAlwaysTrue(pExpr) ){ 3790991a1985Sdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); 3791991a1985Sdrh }else if( exprAlwaysFalse(pExpr) ){ 3792991a1985Sdrh /* No-op */ 3793991a1985Sdrh }else{ 37942dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 37952dcef11bSdrh sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 3796688852abSdrh VdbeCoverage(v); 3797c5499befSdrh testcase( regFree1==0 ); 3798c5499befSdrh testcase( jumpIfNull==0 ); 3799991a1985Sdrh } 3800cce7d176Sdrh break; 3801cce7d176Sdrh } 3802cce7d176Sdrh } 38032dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 38042dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 3805cce7d176Sdrh } 3806cce7d176Sdrh 3807cce7d176Sdrh /* 380866b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 3809cce7d176Sdrh ** to the label "dest" if the expression is false but execution 3810cce7d176Sdrh ** continues straight thru if the expression is true. 3811f5905aa7Sdrh ** 3812f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 381335573356Sdrh ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 381435573356Sdrh ** is 0. 3815cce7d176Sdrh */ 38164adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3817cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3818cce7d176Sdrh int op = 0; 38192dcef11bSdrh int regFree1 = 0; 38202dcef11bSdrh int regFree2 = 0; 38212dcef11bSdrh int r1, r2; 38222dcef11bSdrh 382335573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 382448864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 382533cd4909Sdrh if( pExpr==0 ) return; 3826f2bc013cSdrh 3827f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 3828f2bc013cSdrh ** 3829f2bc013cSdrh ** pExpr->op op 3830f2bc013cSdrh ** --------- ---------- 3831f2bc013cSdrh ** TK_ISNULL OP_NotNull 3832f2bc013cSdrh ** TK_NOTNULL OP_IsNull 3833f2bc013cSdrh ** TK_NE OP_Eq 3834f2bc013cSdrh ** TK_EQ OP_Ne 3835f2bc013cSdrh ** TK_GT OP_Le 3836f2bc013cSdrh ** TK_LE OP_Gt 3837f2bc013cSdrh ** TK_GE OP_Lt 3838f2bc013cSdrh ** TK_LT OP_Ge 3839f2bc013cSdrh ** 3840f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 3841f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 3842f2bc013cSdrh ** can compute the mapping above using the following expression. 3843f2bc013cSdrh ** Assert()s verify that the computation is correct. 3844f2bc013cSdrh */ 3845f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 3846f2bc013cSdrh 3847f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 3848f2bc013cSdrh */ 3849f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 3850f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 3851f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 3852f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 3853f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 3854f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 3855f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 3856f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 3857f2bc013cSdrh 3858cce7d176Sdrh switch( pExpr->op ){ 3859cce7d176Sdrh case TK_AND: { 3860c5499befSdrh testcase( jumpIfNull==0 ); 38614adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 386254e2adb5Sdrh sqlite3ExprCachePush(pParse); 38634adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 3864d2490904Sdrh sqlite3ExprCachePop(pParse); 3865cce7d176Sdrh break; 3866cce7d176Sdrh } 3867cce7d176Sdrh case TK_OR: { 38684adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3869c5499befSdrh testcase( jumpIfNull==0 ); 387035573356Sdrh sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 387154e2adb5Sdrh sqlite3ExprCachePush(pParse); 38724adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 38734adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3874d2490904Sdrh sqlite3ExprCachePop(pParse); 3875cce7d176Sdrh break; 3876cce7d176Sdrh } 3877cce7d176Sdrh case TK_NOT: { 38785c03f30aSdrh testcase( jumpIfNull==0 ); 38794adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 3880cce7d176Sdrh break; 3881cce7d176Sdrh } 3882cce7d176Sdrh case TK_LT: 3883cce7d176Sdrh case TK_LE: 3884cce7d176Sdrh case TK_GT: 3885cce7d176Sdrh case TK_GE: 3886cce7d176Sdrh case TK_NE: 3887cce7d176Sdrh case TK_EQ: { 3888c5499befSdrh testcase( jumpIfNull==0 ); 3889b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3890b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 389135573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 38927d176105Sdrh r1, r2, dest, jumpIfNull); 38937d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 38947d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 38957d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 38967d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 38977d176105Sdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 38987d176105Sdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 3899c5499befSdrh testcase( regFree1==0 ); 3900c5499befSdrh testcase( regFree2==0 ); 3901cce7d176Sdrh break; 3902cce7d176Sdrh } 39036a2fe093Sdrh case TK_IS: 39046a2fe093Sdrh case TK_ISNOT: { 39056d4486aeSdrh testcase( pExpr->op==TK_IS ); 39066d4486aeSdrh testcase( pExpr->op==TK_ISNOT ); 3907b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3908b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 39096a2fe093Sdrh op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; 39106a2fe093Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 39117d176105Sdrh r1, r2, dest, SQLITE_NULLEQ); 39127d176105Sdrh VdbeCoverageIf(v, op==TK_EQ); 39137d176105Sdrh VdbeCoverageIf(v, op==TK_NE); 39146a2fe093Sdrh testcase( regFree1==0 ); 39156a2fe093Sdrh testcase( regFree2==0 ); 39166a2fe093Sdrh break; 39176a2fe093Sdrh } 3918cce7d176Sdrh case TK_ISNULL: 3919cce7d176Sdrh case TK_NOTNULL: { 39202dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 39217d176105Sdrh sqlite3VdbeAddOp2(v, op, r1, dest); 39227d176105Sdrh testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); 39237d176105Sdrh testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); 3924c5499befSdrh testcase( regFree1==0 ); 3925cce7d176Sdrh break; 3926cce7d176Sdrh } 3927fef5208cSdrh case TK_BETWEEN: { 39285c03f30aSdrh testcase( jumpIfNull==0 ); 392936c563a2Sdrh exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull); 3930fef5208cSdrh break; 3931fef5208cSdrh } 3932bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 3933e3365e6cSdrh case TK_IN: { 3934e3365e6cSdrh if( jumpIfNull ){ 3935e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, dest); 3936e3365e6cSdrh }else{ 3937e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 3938e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 3939e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 3940e3365e6cSdrh } 3941e3365e6cSdrh break; 3942e3365e6cSdrh } 3943bb201344Sshaneh #endif 3944cce7d176Sdrh default: { 3945991a1985Sdrh if( exprAlwaysFalse(pExpr) ){ 3946991a1985Sdrh sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); 3947991a1985Sdrh }else if( exprAlwaysTrue(pExpr) ){ 3948991a1985Sdrh /* no-op */ 3949991a1985Sdrh }else{ 39502dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 39512dcef11bSdrh sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 3952688852abSdrh VdbeCoverage(v); 3953c5499befSdrh testcase( regFree1==0 ); 3954c5499befSdrh testcase( jumpIfNull==0 ); 3955991a1985Sdrh } 3956cce7d176Sdrh break; 3957cce7d176Sdrh } 3958cce7d176Sdrh } 39592dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 39602dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 3961cce7d176Sdrh } 39622282792aSdrh 39632282792aSdrh /* 39641d9da70aSdrh ** Do a deep comparison of two expression trees. Return 0 if the two 39651d9da70aSdrh ** expressions are completely identical. Return 1 if they differ only 39661d9da70aSdrh ** by a COLLATE operator at the top level. Return 2 if there are differences 39671d9da70aSdrh ** other than the top-level COLLATE operator. 3968d40aab0eSdrh ** 3969619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 3970619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 3971619a1305Sdrh ** 397266518ca7Sdrh ** The pA side might be using TK_REGISTER. If that is the case and pB is 397366518ca7Sdrh ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 397466518ca7Sdrh ** 39751d9da70aSdrh ** Sometimes this routine will return 2 even if the two expressions 3976d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 39771d9da70aSdrh ** identical, we return 2 just to be safe. So if this routine 39781d9da70aSdrh ** returns 2, then you do not really know for certain if the two 39791d9da70aSdrh ** expressions are the same. But if you get a 0 or 1 return, then you 3980d40aab0eSdrh ** can be sure the expressions are the same. In the places where 39811d9da70aSdrh ** this routine is used, it does not hurt to get an extra 2 - that 3982d40aab0eSdrh ** just might result in some slightly slower code. But returning 39831d9da70aSdrh ** an incorrect 0 or 1 could lead to a malfunction. 39842282792aSdrh */ 3985619a1305Sdrh int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ 398610d1edf0Sdrh u32 combinedFlags; 39874b202ae2Sdanielk1977 if( pA==0 || pB==0 ){ 39881d9da70aSdrh return pB==pA ? 0 : 2; 39892282792aSdrh } 399010d1edf0Sdrh combinedFlags = pA->flags | pB->flags; 399110d1edf0Sdrh if( combinedFlags & EP_IntValue ){ 399210d1edf0Sdrh if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ 399310d1edf0Sdrh return 0; 399410d1edf0Sdrh } 39951d9da70aSdrh return 2; 39966ab3a2ecSdanielk1977 } 3997c2acc4e4Sdrh if( pA->op!=pB->op ){ 3998619a1305Sdrh if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ 3999ae80ddeaSdrh return 1; 4000ae80ddeaSdrh } 4001619a1305Sdrh if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ 4002ae80ddeaSdrh return 1; 4003ae80ddeaSdrh } 4004ae80ddeaSdrh return 2; 4005ae80ddeaSdrh } 400610d1edf0Sdrh if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){ 400710d1edf0Sdrh if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ 400810d1edf0Sdrh return pA->op==TK_COLLATE ? 1 : 2; 400910d1edf0Sdrh } 401010d1edf0Sdrh } 401110d1edf0Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; 401285f8aa79Sdrh if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 401310d1edf0Sdrh if( combinedFlags & EP_xIsSelect ) return 2; 4014619a1305Sdrh if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; 4015619a1305Sdrh if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; 4016619a1305Sdrh if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 401785f8aa79Sdrh if( ALWAYS((combinedFlags & EP_Reduced)==0) ){ 4018619a1305Sdrh if( pA->iColumn!=pB->iColumn ) return 2; 401966518ca7Sdrh if( pA->iTable!=pB->iTable 402085f8aa79Sdrh && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; 40211d9da70aSdrh } 40221d9da70aSdrh } 40232646da7eSdrh return 0; 40242646da7eSdrh } 40252282792aSdrh 40268c6f666bSdrh /* 40278c6f666bSdrh ** Compare two ExprList objects. Return 0 if they are identical and 40288c6f666bSdrh ** non-zero if they differ in any way. 40298c6f666bSdrh ** 4030619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4031619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4032619a1305Sdrh ** 40338c6f666bSdrh ** This routine might return non-zero for equivalent ExprLists. The 40348c6f666bSdrh ** only consequence will be disabled optimizations. But this routine 40358c6f666bSdrh ** must never return 0 if the two ExprList objects are different, or 40368c6f666bSdrh ** a malfunction will result. 40378c6f666bSdrh ** 40388c6f666bSdrh ** Two NULL pointers are considered to be the same. But a NULL pointer 40398c6f666bSdrh ** always differs from a non-NULL pointer. 40408c6f666bSdrh */ 4041619a1305Sdrh int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ 40428c6f666bSdrh int i; 40438c6f666bSdrh if( pA==0 && pB==0 ) return 0; 40448c6f666bSdrh if( pA==0 || pB==0 ) return 1; 40458c6f666bSdrh if( pA->nExpr!=pB->nExpr ) return 1; 40468c6f666bSdrh for(i=0; i<pA->nExpr; i++){ 40478c6f666bSdrh Expr *pExprA = pA->a[i].pExpr; 40488c6f666bSdrh Expr *pExprB = pB->a[i].pExpr; 40498c6f666bSdrh if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; 4050619a1305Sdrh if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; 40518c6f666bSdrh } 40528c6f666bSdrh return 0; 40538c6f666bSdrh } 405413449892Sdrh 40552282792aSdrh /* 40564bd5f73fSdrh ** Return true if we can prove the pE2 will always be true if pE1 is 40574bd5f73fSdrh ** true. Return false if we cannot complete the proof or if pE2 might 40584bd5f73fSdrh ** be false. Examples: 40594bd5f73fSdrh ** 4060619a1305Sdrh ** pE1: x==5 pE2: x==5 Result: true 40614bd5f73fSdrh ** pE1: x>0 pE2: x==5 Result: false 4062619a1305Sdrh ** pE1: x=21 pE2: x=21 OR y=43 Result: true 40634bd5f73fSdrh ** pE1: x!=123 pE2: x IS NOT NULL Result: true 4064619a1305Sdrh ** pE1: x!=?1 pE2: x IS NOT NULL Result: true 4065619a1305Sdrh ** pE1: x IS NULL pE2: x IS NOT NULL Result: false 4066619a1305Sdrh ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false 40674bd5f73fSdrh ** 40684bd5f73fSdrh ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has 40694bd5f73fSdrh ** Expr.iTable<0 then assume a table number given by iTab. 40704bd5f73fSdrh ** 40714bd5f73fSdrh ** When in doubt, return false. Returning true might give a performance 40724bd5f73fSdrh ** improvement. Returning false might cause a performance reduction, but 40734bd5f73fSdrh ** it will always give the correct answer and is hence always safe. 40744bd5f73fSdrh */ 40754bd5f73fSdrh int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ 4076619a1305Sdrh if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ 4077619a1305Sdrh return 1; 4078619a1305Sdrh } 4079619a1305Sdrh if( pE2->op==TK_OR 4080619a1305Sdrh && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) 4081619a1305Sdrh || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) 4082619a1305Sdrh ){ 4083619a1305Sdrh return 1; 4084619a1305Sdrh } 4085619a1305Sdrh if( pE2->op==TK_NOTNULL 4086619a1305Sdrh && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 4087619a1305Sdrh && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) 4088619a1305Sdrh ){ 4089619a1305Sdrh return 1; 4090619a1305Sdrh } 4091619a1305Sdrh return 0; 40924bd5f73fSdrh } 40934bd5f73fSdrh 40944bd5f73fSdrh /* 4095030796dfSdrh ** An instance of the following structure is used by the tree walker 4096030796dfSdrh ** to count references to table columns in the arguments of an 4097ed551b95Sdrh ** aggregate function, in order to implement the 4098ed551b95Sdrh ** sqlite3FunctionThisSrc() routine. 4099374fdce4Sdrh */ 4100030796dfSdrh struct SrcCount { 4101030796dfSdrh SrcList *pSrc; /* One particular FROM clause in a nested query */ 4102030796dfSdrh int nThis; /* Number of references to columns in pSrcList */ 4103030796dfSdrh int nOther; /* Number of references to columns in other FROM clauses */ 4104030796dfSdrh }; 4105030796dfSdrh 4106030796dfSdrh /* 4107030796dfSdrh ** Count the number of references to columns. 4108030796dfSdrh */ 4109030796dfSdrh static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 4110fb0a6081Sdrh /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() 4111fb0a6081Sdrh ** is always called before sqlite3ExprAnalyzeAggregates() and so the 4112fb0a6081Sdrh ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If 4113fb0a6081Sdrh ** sqlite3FunctionUsesThisSrc() is used differently in the future, the 4114fb0a6081Sdrh ** NEVER() will need to be removed. */ 4115fb0a6081Sdrh if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 4116374fdce4Sdrh int i; 4117030796dfSdrh struct SrcCount *p = pWalker->u.pSrcCount; 4118030796dfSdrh SrcList *pSrc = p->pSrc; 4119655814d2Sdrh int nSrc = pSrc ? pSrc->nSrc : 0; 4120655814d2Sdrh for(i=0; i<nSrc; i++){ 4121030796dfSdrh if( pExpr->iTable==pSrc->a[i].iCursor ) break; 4122374fdce4Sdrh } 4123655814d2Sdrh if( i<nSrc ){ 4124030796dfSdrh p->nThis++; 4125374fdce4Sdrh }else{ 4126030796dfSdrh p->nOther++; 4127374fdce4Sdrh } 4128374fdce4Sdrh } 4129030796dfSdrh return WRC_Continue; 4130030796dfSdrh } 4131374fdce4Sdrh 4132374fdce4Sdrh /* 4133030796dfSdrh ** Determine if any of the arguments to the pExpr Function reference 4134030796dfSdrh ** pSrcList. Return true if they do. Also return true if the function 4135030796dfSdrh ** has no arguments or has only constant arguments. Return false if pExpr 4136030796dfSdrh ** references columns but not columns of tables found in pSrcList. 4137374fdce4Sdrh */ 4138030796dfSdrh int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ 4139374fdce4Sdrh Walker w; 4140030796dfSdrh struct SrcCount cnt; 4141374fdce4Sdrh assert( pExpr->op==TK_AGG_FUNCTION ); 4142374fdce4Sdrh memset(&w, 0, sizeof(w)); 4143030796dfSdrh w.xExprCallback = exprSrcCount; 4144030796dfSdrh w.u.pSrcCount = &cnt; 4145030796dfSdrh cnt.pSrc = pSrcList; 4146030796dfSdrh cnt.nThis = 0; 4147030796dfSdrh cnt.nOther = 0; 4148030796dfSdrh sqlite3WalkExprList(&w, pExpr->x.pList); 4149030796dfSdrh return cnt.nThis>0 || cnt.nOther==0; 4150374fdce4Sdrh } 4151374fdce4Sdrh 4152374fdce4Sdrh /* 415313449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 415413449892Sdrh ** the new element. Return a negative number if malloc fails. 41552282792aSdrh */ 415617435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 415713449892Sdrh int i; 4158cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 415917435752Sdrh db, 4160cf643729Sdrh pInfo->aCol, 4161cf643729Sdrh sizeof(pInfo->aCol[0]), 4162cf643729Sdrh &pInfo->nColumn, 4163cf643729Sdrh &i 4164cf643729Sdrh ); 416513449892Sdrh return i; 41662282792aSdrh } 416713449892Sdrh 416813449892Sdrh /* 416913449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 417013449892Sdrh ** the new element. Return a negative number if malloc fails. 417113449892Sdrh */ 417217435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 417313449892Sdrh int i; 4174cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 417517435752Sdrh db, 4176cf643729Sdrh pInfo->aFunc, 4177cf643729Sdrh sizeof(pInfo->aFunc[0]), 4178cf643729Sdrh &pInfo->nFunc, 4179cf643729Sdrh &i 4180cf643729Sdrh ); 418113449892Sdrh return i; 41822282792aSdrh } 41832282792aSdrh 41842282792aSdrh /* 41857d10d5a6Sdrh ** This is the xExprCallback for a tree walker. It is used to 41867d10d5a6Sdrh ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 4187626a879aSdrh ** for additional information. 41882282792aSdrh */ 41897d10d5a6Sdrh static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ 41902282792aSdrh int i; 41917d10d5a6Sdrh NameContext *pNC = pWalker->u.pNC; 4192a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 4193a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 419413449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 419513449892Sdrh 41962282792aSdrh switch( pExpr->op ){ 419789c69d00Sdrh case TK_AGG_COLUMN: 4198967e8b73Sdrh case TK_COLUMN: { 41998b213899Sdrh testcase( pExpr->op==TK_AGG_COLUMN ); 42008b213899Sdrh testcase( pExpr->op==TK_COLUMN ); 420113449892Sdrh /* Check to see if the column is in one of the tables in the FROM 420213449892Sdrh ** clause of the aggregate query */ 420320bc393cSdrh if( ALWAYS(pSrcList!=0) ){ 420413449892Sdrh struct SrcList_item *pItem = pSrcList->a; 420513449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 420613449892Sdrh struct AggInfo_col *pCol; 4207c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 420813449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 420913449892Sdrh /* If we reach this point, it means that pExpr refers to a table 421013449892Sdrh ** that is in the FROM clause of the aggregate query. 421113449892Sdrh ** 421213449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 421313449892Sdrh ** is not an entry there already. 421413449892Sdrh */ 42157f906d63Sdrh int k; 421613449892Sdrh pCol = pAggInfo->aCol; 42177f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 421813449892Sdrh if( pCol->iTable==pExpr->iTable && 421913449892Sdrh pCol->iColumn==pExpr->iColumn ){ 42202282792aSdrh break; 42212282792aSdrh } 42222282792aSdrh } 42231e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 42241e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 42251e536953Sdanielk1977 ){ 42267f906d63Sdrh pCol = &pAggInfo->aCol[k]; 42270817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 422813449892Sdrh pCol->iTable = pExpr->iTable; 422913449892Sdrh pCol->iColumn = pExpr->iColumn; 42300a07c107Sdrh pCol->iMem = ++pParse->nMem; 423113449892Sdrh pCol->iSorterColumn = -1; 42325774b806Sdrh pCol->pExpr = pExpr; 423313449892Sdrh if( pAggInfo->pGroupBy ){ 423413449892Sdrh int j, n; 423513449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 423613449892Sdrh struct ExprList_item *pTerm = pGB->a; 423713449892Sdrh n = pGB->nExpr; 423813449892Sdrh for(j=0; j<n; j++, pTerm++){ 423913449892Sdrh Expr *pE = pTerm->pExpr; 424013449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 424113449892Sdrh pE->iColumn==pExpr->iColumn ){ 424213449892Sdrh pCol->iSorterColumn = j; 424313449892Sdrh break; 42442282792aSdrh } 424513449892Sdrh } 424613449892Sdrh } 424713449892Sdrh if( pCol->iSorterColumn<0 ){ 424813449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 424913449892Sdrh } 425013449892Sdrh } 425113449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 425213449892Sdrh ** because it was there before or because we just created it). 425313449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 425413449892Sdrh ** pAggInfo->aCol[] entry. 425513449892Sdrh */ 4256ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 425713449892Sdrh pExpr->pAggInfo = pAggInfo; 425813449892Sdrh pExpr->op = TK_AGG_COLUMN; 4259cf697396Sshane pExpr->iAgg = (i16)k; 426013449892Sdrh break; 426113449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 426213449892Sdrh } /* end loop over pSrcList */ 4263a58fdfb1Sdanielk1977 } 42647d10d5a6Sdrh return WRC_Prune; 42652282792aSdrh } 42662282792aSdrh case TK_AGG_FUNCTION: { 42673a8c4be7Sdrh if( (pNC->ncFlags & NC_InAggFunc)==0 4268ed551b95Sdrh && pWalker->walkerDepth==pExpr->op2 42693a8c4be7Sdrh ){ 427013449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 427113449892Sdrh ** function that is already in the pAggInfo structure 427213449892Sdrh */ 427313449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 427413449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 4275619a1305Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ 42762282792aSdrh break; 42772282792aSdrh } 42782282792aSdrh } 427913449892Sdrh if( i>=pAggInfo->nFunc ){ 428013449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 428113449892Sdrh */ 428214db2665Sdanielk1977 u8 enc = ENC(pParse->db); 42831e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 428413449892Sdrh if( i>=0 ){ 42856ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 428613449892Sdrh pItem = &pAggInfo->aFunc[i]; 428713449892Sdrh pItem->pExpr = pExpr; 42880a07c107Sdrh pItem->iMem = ++pParse->nMem; 428933e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 429013449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 429133e619fcSdrh pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken), 42926ab3a2ecSdanielk1977 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); 4293fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 4294fd357974Sdrh pItem->iDistinct = pParse->nTab++; 4295fd357974Sdrh }else{ 4296fd357974Sdrh pItem->iDistinct = -1; 4297fd357974Sdrh } 42982282792aSdrh } 429913449892Sdrh } 430013449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 430113449892Sdrh */ 4302c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 4303ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 4304cf697396Sshane pExpr->iAgg = (i16)i; 430513449892Sdrh pExpr->pAggInfo = pAggInfo; 43063a8c4be7Sdrh return WRC_Prune; 43076e83a57fSdrh }else{ 43086e83a57fSdrh return WRC_Continue; 43096e83a57fSdrh } 43102282792aSdrh } 4311a58fdfb1Sdanielk1977 } 43127d10d5a6Sdrh return WRC_Continue; 43137d10d5a6Sdrh } 43147d10d5a6Sdrh static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ 4315d5a336efSdrh UNUSED_PARAMETER(pWalker); 4316d5a336efSdrh UNUSED_PARAMETER(pSelect); 43177d10d5a6Sdrh return WRC_Continue; 4318a58fdfb1Sdanielk1977 } 4319626a879aSdrh 4320626a879aSdrh /* 4321e8abb4caSdrh ** Analyze the pExpr expression looking for aggregate functions and 4322e8abb4caSdrh ** for variables that need to be added to AggInfo object that pNC->pAggInfo 4323e8abb4caSdrh ** points to. Additional entries are made on the AggInfo object as 4324e8abb4caSdrh ** necessary. 4325626a879aSdrh ** 4326626a879aSdrh ** This routine should only be called after the expression has been 43277d10d5a6Sdrh ** analyzed by sqlite3ResolveExprNames(). 4328626a879aSdrh */ 4329d2b3e23bSdrh void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 43307d10d5a6Sdrh Walker w; 4331374fdce4Sdrh memset(&w, 0, sizeof(w)); 43327d10d5a6Sdrh w.xExprCallback = analyzeAggregate; 43337d10d5a6Sdrh w.xSelectCallback = analyzeAggregatesInSelect; 43347d10d5a6Sdrh w.u.pNC = pNC; 433520bc393cSdrh assert( pNC->pSrcList!=0 ); 43367d10d5a6Sdrh sqlite3WalkExpr(&w, pExpr); 43372282792aSdrh } 43385d9a4af9Sdrh 43395d9a4af9Sdrh /* 43405d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 43415d9a4af9Sdrh ** expression list. Return the number of errors. 43425d9a4af9Sdrh ** 43435d9a4af9Sdrh ** If an error is found, the analysis is cut short. 43445d9a4af9Sdrh */ 4345d2b3e23bSdrh void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 43465d9a4af9Sdrh struct ExprList_item *pItem; 43475d9a4af9Sdrh int i; 43485d9a4af9Sdrh if( pList ){ 4349d2b3e23bSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 4350d2b3e23bSdrh sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 43515d9a4af9Sdrh } 43525d9a4af9Sdrh } 43535d9a4af9Sdrh } 4354892d3179Sdrh 4355892d3179Sdrh /* 4356ceea3321Sdrh ** Allocate a single new register for use to hold some intermediate result. 4357892d3179Sdrh */ 4358892d3179Sdrh int sqlite3GetTempReg(Parse *pParse){ 4359e55cbd72Sdrh if( pParse->nTempReg==0 ){ 4360892d3179Sdrh return ++pParse->nMem; 4361892d3179Sdrh } 43622f425f6bSdanielk1977 return pParse->aTempReg[--pParse->nTempReg]; 4363892d3179Sdrh } 4364ceea3321Sdrh 4365ceea3321Sdrh /* 4366ceea3321Sdrh ** Deallocate a register, making available for reuse for some other 4367ceea3321Sdrh ** purpose. 4368ceea3321Sdrh ** 4369ceea3321Sdrh ** If a register is currently being used by the column cache, then 437060ec914cSpeter.d.reid ** the deallocation is deferred until the column cache line that uses 4371ceea3321Sdrh ** the register becomes stale. 4372ceea3321Sdrh */ 4373892d3179Sdrh void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 43742dcef11bSdrh if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 4375ceea3321Sdrh int i; 4376ceea3321Sdrh struct yColCache *p; 4377ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 4378ceea3321Sdrh if( p->iReg==iReg ){ 4379ceea3321Sdrh p->tempReg = 1; 4380ceea3321Sdrh return; 4381ceea3321Sdrh } 4382ceea3321Sdrh } 4383892d3179Sdrh pParse->aTempReg[pParse->nTempReg++] = iReg; 4384892d3179Sdrh } 4385892d3179Sdrh } 4386892d3179Sdrh 4387892d3179Sdrh /* 4388892d3179Sdrh ** Allocate or deallocate a block of nReg consecutive registers 4389892d3179Sdrh */ 4390892d3179Sdrh int sqlite3GetTempRange(Parse *pParse, int nReg){ 4391e55cbd72Sdrh int i, n; 4392892d3179Sdrh i = pParse->iRangeReg; 4393e55cbd72Sdrh n = pParse->nRangeReg; 4394f49f3523Sdrh if( nReg<=n ){ 4395f49f3523Sdrh assert( !usedAsColumnCache(pParse, i, i+n-1) ); 4396892d3179Sdrh pParse->iRangeReg += nReg; 4397892d3179Sdrh pParse->nRangeReg -= nReg; 4398892d3179Sdrh }else{ 4399892d3179Sdrh i = pParse->nMem+1; 4400892d3179Sdrh pParse->nMem += nReg; 4401892d3179Sdrh } 4402892d3179Sdrh return i; 4403892d3179Sdrh } 4404892d3179Sdrh void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 4405f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iReg, nReg); 4406892d3179Sdrh if( nReg>pParse->nRangeReg ){ 4407892d3179Sdrh pParse->nRangeReg = nReg; 4408892d3179Sdrh pParse->iRangeReg = iReg; 4409892d3179Sdrh } 4410892d3179Sdrh } 4411cdc69557Sdrh 4412cdc69557Sdrh /* 4413cdc69557Sdrh ** Mark all temporary registers as being unavailable for reuse. 4414cdc69557Sdrh */ 4415cdc69557Sdrh void sqlite3ClearTempRegCache(Parse *pParse){ 4416cdc69557Sdrh pParse->nTempReg = 0; 4417cdc69557Sdrh pParse->nRangeReg = 0; 4418cdc69557Sdrh } 4419