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 */ 7280103fc6Sdan const Token *pCollName, /* Name of collating sequence */ 7380103fc6Sdan int dequote /* True to dequote pCollName */ 744ef7efadSdrh ){ 750a8a406eSdrh if( pCollName->n>0 ){ 7680103fc6Sdan Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, dequote); 77ae80ddeaSdrh if( pNew ){ 78ae80ddeaSdrh pNew->pLeft = pExpr; 79a4c3c87eSdrh pNew->flags |= EP_Collate|EP_Skip; 800a8a406eSdrh pExpr = pNew; 81ae80ddeaSdrh } 820a8a406eSdrh } 830a8a406eSdrh return pExpr; 840a8a406eSdrh } 850a8a406eSdrh Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){ 860a8a406eSdrh Token s; 87261d8a51Sdrh assert( zC!=0 ); 8840aced5cSdrh sqlite3TokenInit(&s, (char*)zC); 8980103fc6Sdan return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0); 900a8a406eSdrh } 910a8a406eSdrh 920a8a406eSdrh /* 930b8d255cSdrh ** Skip over any TK_COLLATE 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{ 1040b8d255cSdrh assert( pExpr->op==TK_COLLATE ); 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 ){ 1492308ed38Sdrh if( p->pLeft && (p->pLeft->flags & EP_Collate)!=0 ){ 1507d10d5a6Sdrh p = p->pLeft; 151ae80ddeaSdrh }else{ 1522308ed38Sdrh 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)) ){ 1592308ed38Sdrh int i; 1606728cd91Sdrh for(i=0; ALWAYS(i<p->x.pList->nExpr); i++){ 1612308ed38Sdrh if( ExprHasProperty(p->x.pList->a[i].pExpr, EP_Collate) ){ 1622308ed38Sdrh pNext = p->x.pList->a[i].pExpr; 1632308ed38Sdrh break; 1642308ed38Sdrh } 1652308ed38Sdrh } 1662308ed38Sdrh } 1672308ed38Sdrh 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{ 19305883a34Sdrh return SQLITE_AFF_BLOB; 194e014a838Sdanielk1977 } 195e014a838Sdanielk1977 }else if( !aff1 && !aff2 ){ 1965f6a87b3Sdrh /* Neither side of the comparison is a column. Compare the 1975f6a87b3Sdrh ** results directly. 198e014a838Sdanielk1977 */ 19905883a34Sdrh return SQLITE_AFF_BLOB; 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 ){ 22305883a34Sdrh aff = SQLITE_AFF_BLOB; 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 ){ 23705883a34Sdrh case SQLITE_AFF_BLOB: 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. 3732308ed38Sdrh ** 3742308ed38Sdrh ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, 3752308ed38Sdrh ** 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); 3832308ed38Sdrh }else if( p->x.pList ){ 3846ab3a2ecSdanielk1977 heightOfExprList(p->x.pList, &nHeight); 3852308ed38Sdrh 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. 3942308ed38Sdrh ** 3952308ed38Sdrh ** Also propagate all EP_Propagate flags from the Expr.x.pList into 3962308ed38Sdrh ** Expr.flags. 3974b5255acSdanielk1977 */ 3982308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 39974893a4cSdrh if( pParse->nErr ) return; 4004b5255acSdanielk1977 exprSetHeight(p); 4017d10d5a6Sdrh sqlite3ExprCheckHeight(pParse, p->nHeight); 4024b5255acSdanielk1977 } 4034b5255acSdanielk1977 4044b5255acSdanielk1977 /* 4054b5255acSdanielk1977 ** Return the maximum height of any expression tree referenced 4064b5255acSdanielk1977 ** by the select statement passed as an argument. 4074b5255acSdanielk1977 */ 4084b5255acSdanielk1977 int sqlite3SelectExprHeight(Select *p){ 4094b5255acSdanielk1977 int nHeight = 0; 4104b5255acSdanielk1977 heightOfSelect(p, &nHeight); 4114b5255acSdanielk1977 return nHeight; 4124b5255acSdanielk1977 } 4132308ed38Sdrh #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ 4142308ed38Sdrh /* 4152308ed38Sdrh ** Propagate all EP_Propagate flags from the Expr.x.pList into 4162308ed38Sdrh ** Expr.flags. 4172308ed38Sdrh */ 4182308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 4192308ed38Sdrh if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ 4202308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 4212308ed38Sdrh } 4222308ed38Sdrh } 4234b5255acSdanielk1977 #define exprSetHeight(y) 4244b5255acSdanielk1977 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 4254b5255acSdanielk1977 426be5c89acSdrh /* 427b7916a78Sdrh ** This routine is the core allocator for Expr nodes. 428b7916a78Sdrh ** 429a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 430b7916a78Sdrh ** for this node and for the pToken argument is a single allocation 431b7916a78Sdrh ** obtained from sqlite3DbMalloc(). The calling function 432a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 433b7916a78Sdrh ** 434b7916a78Sdrh ** If dequote is true, then the token (if it exists) is dequoted. 435e792b5b4Sdrh ** If dequote is false, no dequoting is performed. The deQuote 436b7916a78Sdrh ** parameter is ignored if pToken is NULL or if the token does not 437b7916a78Sdrh ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 438b7916a78Sdrh ** then the EP_DblQuoted flag is set on the expression node. 43933e619fcSdrh ** 44033e619fcSdrh ** Special case: If op==TK_INTEGER and pToken points to a string that 44133e619fcSdrh ** can be translated into a 32-bit integer, then the token is not 44233e619fcSdrh ** stored in u.zToken. Instead, the integer values is written 44333e619fcSdrh ** into u.iValue and the EP_IntValue flag is set. No extra storage 44433e619fcSdrh ** is allocated to hold the integer text and the dequote flag is ignored. 445a76b5dfcSdrh */ 446b7916a78Sdrh Expr *sqlite3ExprAlloc( 447a1644fd8Sdanielk1977 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 44817435752Sdrh int op, /* Expression opcode */ 449b7916a78Sdrh const Token *pToken, /* Token argument. Might be NULL */ 450b7916a78Sdrh int dequote /* True to dequote */ 45117435752Sdrh ){ 452a76b5dfcSdrh Expr *pNew; 45333e619fcSdrh int nExtra = 0; 454cf697396Sshane int iValue = 0; 455b7916a78Sdrh 456575fad65Sdrh assert( db!=0 ); 457b7916a78Sdrh if( pToken ){ 45833e619fcSdrh if( op!=TK_INTEGER || pToken->z==0 45933e619fcSdrh || sqlite3GetInt32(pToken->z, &iValue)==0 ){ 460b7916a78Sdrh nExtra = pToken->n+1; 461d50ffc41Sdrh assert( iValue>=0 ); 46233e619fcSdrh } 463a76b5dfcSdrh } 464575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); 465b7916a78Sdrh if( pNew ){ 466ca3862dcSdrh memset(pNew, 0, sizeof(Expr)); 4671bd10f8aSdrh pNew->op = (u8)op; 468a58fdfb1Sdanielk1977 pNew->iAgg = -1; 469a76b5dfcSdrh if( pToken ){ 47033e619fcSdrh if( nExtra==0 ){ 47133e619fcSdrh pNew->flags |= EP_IntValue; 47233e619fcSdrh pNew->u.iValue = iValue; 47333e619fcSdrh }else{ 474d9da78a2Sdrh int c; 47533e619fcSdrh pNew->u.zToken = (char*)&pNew[1]; 476b07028f7Sdrh assert( pToken->z!=0 || pToken->n==0 ); 477b07028f7Sdrh if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); 47833e619fcSdrh pNew->u.zToken[pToken->n] = 0; 479b7916a78Sdrh if( dequote && nExtra>=3 480d9da78a2Sdrh && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){ 48133e619fcSdrh sqlite3Dequote(pNew->u.zToken); 48224fb627aSdrh if( c=='"' ) pNew->flags |= EP_DblQuoted; 483a34001c9Sdrh } 484a34001c9Sdrh } 48533e619fcSdrh } 486b7916a78Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 487b7916a78Sdrh pNew->nHeight = 1; 488b7916a78Sdrh #endif 489a34001c9Sdrh } 490a76b5dfcSdrh return pNew; 491a76b5dfcSdrh } 492a76b5dfcSdrh 493a76b5dfcSdrh /* 494b7916a78Sdrh ** Allocate a new expression node from a zero-terminated token that has 495b7916a78Sdrh ** already been dequoted. 496b7916a78Sdrh */ 497b7916a78Sdrh Expr *sqlite3Expr( 498b7916a78Sdrh sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 499b7916a78Sdrh int op, /* Expression opcode */ 500b7916a78Sdrh const char *zToken /* Token argument. Might be NULL */ 501b7916a78Sdrh ){ 502b7916a78Sdrh Token x; 503b7916a78Sdrh x.z = zToken; 504b7916a78Sdrh x.n = zToken ? sqlite3Strlen30(zToken) : 0; 505b7916a78Sdrh return sqlite3ExprAlloc(db, op, &x, 0); 506b7916a78Sdrh } 507b7916a78Sdrh 508b7916a78Sdrh /* 509b7916a78Sdrh ** Attach subtrees pLeft and pRight to the Expr node pRoot. 510b7916a78Sdrh ** 511b7916a78Sdrh ** If pRoot==NULL that means that a memory allocation error has occurred. 512b7916a78Sdrh ** In that case, delete the subtrees pLeft and pRight. 513b7916a78Sdrh */ 514b7916a78Sdrh void sqlite3ExprAttachSubtrees( 515b7916a78Sdrh sqlite3 *db, 516b7916a78Sdrh Expr *pRoot, 517b7916a78Sdrh Expr *pLeft, 518b7916a78Sdrh Expr *pRight 519b7916a78Sdrh ){ 520b7916a78Sdrh if( pRoot==0 ){ 521b7916a78Sdrh assert( db->mallocFailed ); 522b7916a78Sdrh sqlite3ExprDelete(db, pLeft); 523b7916a78Sdrh sqlite3ExprDelete(db, pRight); 524b7916a78Sdrh }else{ 525b7916a78Sdrh if( pRight ){ 526b7916a78Sdrh pRoot->pRight = pRight; 527885a5b03Sdrh pRoot->flags |= EP_Propagate & pRight->flags; 528b7916a78Sdrh } 529b7916a78Sdrh if( pLeft ){ 530b7916a78Sdrh pRoot->pLeft = pLeft; 531885a5b03Sdrh pRoot->flags |= EP_Propagate & pLeft->flags; 532b7916a78Sdrh } 533b7916a78Sdrh exprSetHeight(pRoot); 534b7916a78Sdrh } 535b7916a78Sdrh } 536b7916a78Sdrh 537b7916a78Sdrh /* 53860ec914cSpeter.d.reid ** Allocate an Expr node which joins as many as two subtrees. 539b7916a78Sdrh ** 540bf664469Sdrh ** One or both of the subtrees can be NULL. Return a pointer to the new 541bf664469Sdrh ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 542bf664469Sdrh ** free the subtrees and return NULL. 543206f3d96Sdrh */ 54417435752Sdrh Expr *sqlite3PExpr( 54517435752Sdrh Parse *pParse, /* Parsing context */ 54617435752Sdrh int op, /* Expression opcode */ 54717435752Sdrh Expr *pLeft, /* Left operand */ 54817435752Sdrh Expr *pRight, /* Right operand */ 54917435752Sdrh const Token *pToken /* Argument token */ 55017435752Sdrh ){ 5515fb52caaSdrh Expr *p; 5521167d327Sdrh if( op==TK_AND && pParse->nErr==0 ){ 5535fb52caaSdrh /* Take advantage of short-circuit false optimization for AND */ 5545fb52caaSdrh p = sqlite3ExprAnd(pParse->db, pLeft, pRight); 5555fb52caaSdrh }else{ 5561167d327Sdrh p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1); 557b7916a78Sdrh sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 5585fb52caaSdrh } 5592b359bdbSdan if( p ) { 5602b359bdbSdan sqlite3ExprCheckHeight(pParse, p->nHeight); 5612b359bdbSdan } 5624e0cff60Sdrh return p; 5634e0cff60Sdrh } 5644e0cff60Sdrh 5654e0cff60Sdrh /* 566991a1985Sdrh ** If the expression is always either TRUE or FALSE (respectively), 567991a1985Sdrh ** then return 1. If one cannot determine the truth value of the 568991a1985Sdrh ** expression at compile-time return 0. 569991a1985Sdrh ** 570991a1985Sdrh ** This is an optimization. If is OK to return 0 here even if 571991a1985Sdrh ** the expression really is always false or false (a false negative). 572991a1985Sdrh ** But it is a bug to return 1 if the expression might have different 573991a1985Sdrh ** boolean values in different circumstances (a false positive.) 5745fb52caaSdrh ** 5755fb52caaSdrh ** Note that if the expression is part of conditional for a 5765fb52caaSdrh ** LEFT JOIN, then we cannot determine at compile-time whether or not 5775fb52caaSdrh ** is it true or false, so always return 0. 5785fb52caaSdrh */ 579991a1985Sdrh static int exprAlwaysTrue(Expr *p){ 580991a1985Sdrh int v = 0; 581991a1985Sdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 582991a1985Sdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 583991a1985Sdrh return v!=0; 584991a1985Sdrh } 5855fb52caaSdrh static int exprAlwaysFalse(Expr *p){ 5865fb52caaSdrh int v = 0; 5875fb52caaSdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 5885fb52caaSdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 5895fb52caaSdrh return v==0; 5905fb52caaSdrh } 5915fb52caaSdrh 5925fb52caaSdrh /* 59391bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 59491bb0eedSdrh ** NULL, then just return the other expression. 5955fb52caaSdrh ** 5965fb52caaSdrh ** If one side or the other of the AND is known to be false, then instead 5975fb52caaSdrh ** of returning an AND expression, just return a constant expression with 5985fb52caaSdrh ** a value of false. 59991bb0eedSdrh */ 6001e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 60191bb0eedSdrh if( pLeft==0 ){ 60291bb0eedSdrh return pRight; 60391bb0eedSdrh }else if( pRight==0 ){ 60491bb0eedSdrh return pLeft; 6055fb52caaSdrh }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ 6065fb52caaSdrh sqlite3ExprDelete(db, pLeft); 6075fb52caaSdrh sqlite3ExprDelete(db, pRight); 6085fb52caaSdrh return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); 60991bb0eedSdrh }else{ 610b7916a78Sdrh Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); 611b7916a78Sdrh sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); 612b7916a78Sdrh return pNew; 613a76b5dfcSdrh } 614a76b5dfcSdrh } 615a76b5dfcSdrh 616a76b5dfcSdrh /* 617a76b5dfcSdrh ** Construct a new expression node for a function with multiple 618a76b5dfcSdrh ** arguments. 619a76b5dfcSdrh */ 62017435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 621a76b5dfcSdrh Expr *pNew; 622633e6d57Sdrh sqlite3 *db = pParse->db; 6234b202ae2Sdanielk1977 assert( pToken ); 624b7916a78Sdrh pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 625a76b5dfcSdrh if( pNew==0 ){ 626d9da78a2Sdrh sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 627a76b5dfcSdrh return 0; 628a76b5dfcSdrh } 6296ab3a2ecSdanielk1977 pNew->x.pList = pList; 6306ab3a2ecSdanielk1977 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 6312308ed38Sdrh sqlite3ExprSetHeightAndFlags(pParse, pNew); 632a76b5dfcSdrh return pNew; 633a76b5dfcSdrh } 634a76b5dfcSdrh 635a76b5dfcSdrh /* 636fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 637fa6bc000Sdrh ** in the original SQL statement. 638fa6bc000Sdrh ** 639fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 640fa6bc000Sdrh ** variable number. 641fa6bc000Sdrh ** 642fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 643fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 644fa6bc000Sdrh ** the SQL statement comes from an external source. 645fa6bc000Sdrh ** 64651f49f17Sdrh ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number 647fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 64860ec914cSpeter.d.reid ** instance of the wildcard, the next sequential variable number is 649fa6bc000Sdrh ** assigned. 650fa6bc000Sdrh */ 651fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 65217435752Sdrh sqlite3 *db = pParse->db; 653b7916a78Sdrh const char *z; 65417435752Sdrh 655fa6bc000Sdrh if( pExpr==0 ) return; 656c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); 65733e619fcSdrh z = pExpr->u.zToken; 658b7916a78Sdrh assert( z!=0 ); 659b7916a78Sdrh assert( z[0]!=0 ); 660b7916a78Sdrh if( z[1]==0 ){ 661fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 662b7916a78Sdrh assert( z[0]=='?' ); 6638677d308Sdrh pExpr->iColumn = (ynVar)(++pParse->nVar); 664124c0b49Sdrh }else{ 665124c0b49Sdrh ynVar x = 0; 666124c0b49Sdrh u32 n = sqlite3Strlen30(z); 667124c0b49Sdrh if( z[0]=='?' ){ 668fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 669fa6bc000Sdrh ** use it as the variable number */ 670c8d735aeSdan i64 i; 671124c0b49Sdrh int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); 672124c0b49Sdrh pExpr->iColumn = x = (ynVar)i; 673c5499befSdrh testcase( i==0 ); 674c5499befSdrh testcase( i==1 ); 675c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 676c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 677c8d735aeSdan if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 678fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 679bb4957f8Sdrh db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 680124c0b49Sdrh x = 0; 681fa6bc000Sdrh } 682fa6bc000Sdrh if( i>pParse->nVar ){ 6831df2db7fSshaneh pParse->nVar = (int)i; 684fa6bc000Sdrh } 685fa6bc000Sdrh }else{ 68651f49f17Sdrh /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable 687fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 688fa6bc000Sdrh ** has never appeared before, reuse the same variable number 689fa6bc000Sdrh */ 690124c0b49Sdrh ynVar i; 691124c0b49Sdrh for(i=0; i<pParse->nzVar; i++){ 692503a686eSdrh if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ 693124c0b49Sdrh pExpr->iColumn = x = (ynVar)i+1; 694fa6bc000Sdrh break; 695fa6bc000Sdrh } 696fa6bc000Sdrh } 697124c0b49Sdrh if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); 698fa6bc000Sdrh } 699124c0b49Sdrh if( x>0 ){ 700124c0b49Sdrh if( x>pParse->nzVar ){ 701124c0b49Sdrh char **a; 702124c0b49Sdrh a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); 7034a642b60Sdrh if( a==0 ){ 7044a642b60Sdrh assert( db->mallocFailed ); /* Error reported through mallocFailed */ 7054a642b60Sdrh return; 7064a642b60Sdrh } 707124c0b49Sdrh pParse->azVar = a; 708124c0b49Sdrh memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); 709124c0b49Sdrh pParse->nzVar = x; 710124c0b49Sdrh } 711124c0b49Sdrh if( z[0]!='?' || pParse->azVar[x-1]==0 ){ 712124c0b49Sdrh sqlite3DbFree(db, pParse->azVar[x-1]); 713124c0b49Sdrh pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); 714fa6bc000Sdrh } 715fa6bc000Sdrh } 716fa6bc000Sdrh } 717bb4957f8Sdrh if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 718832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 719832b2664Sdanielk1977 } 720fa6bc000Sdrh } 721fa6bc000Sdrh 722fa6bc000Sdrh /* 723f6963f99Sdan ** Recursively delete an expression tree. 724a2e00042Sdrh */ 725f6963f99Sdan void sqlite3ExprDelete(sqlite3 *db, Expr *p){ 726f6963f99Sdan if( p==0 ) return; 727d50ffc41Sdrh /* Sanity check: Assert that the IntValue is non-negative if it exists */ 728d50ffc41Sdrh assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); 729c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 730c5cd1249Sdrh /* The Expr.x union is never used at the same time as Expr.pRight */ 731c5cd1249Sdrh assert( p->x.pList==0 || p->pRight==0 ); 732633e6d57Sdrh sqlite3ExprDelete(db, p->pLeft); 733633e6d57Sdrh sqlite3ExprDelete(db, p->pRight); 734c5cd1249Sdrh if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); 7356ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 7366ab3a2ecSdanielk1977 sqlite3SelectDelete(db, p->x.pSelect); 7376ab3a2ecSdanielk1977 }else{ 7386ab3a2ecSdanielk1977 sqlite3ExprListDelete(db, p->x.pList); 7396ab3a2ecSdanielk1977 } 7406ab3a2ecSdanielk1977 } 74133e619fcSdrh if( !ExprHasProperty(p, EP_Static) ){ 742633e6d57Sdrh sqlite3DbFree(db, p); 743a2e00042Sdrh } 74433e619fcSdrh } 745a2e00042Sdrh 746d2687b77Sdrh /* 7476ab3a2ecSdanielk1977 ** Return the number of bytes allocated for the expression structure 7486ab3a2ecSdanielk1977 ** passed as the first argument. This is always one of EXPR_FULLSIZE, 7496ab3a2ecSdanielk1977 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. 7506ab3a2ecSdanielk1977 */ 7516ab3a2ecSdanielk1977 static int exprStructSize(Expr *p){ 7526ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; 7536ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; 7546ab3a2ecSdanielk1977 return EXPR_FULLSIZE; 7556ab3a2ecSdanielk1977 } 7566ab3a2ecSdanielk1977 7576ab3a2ecSdanielk1977 /* 75833e619fcSdrh ** The dupedExpr*Size() routines each return the number of bytes required 75933e619fcSdrh ** to store a copy of an expression or expression tree. They differ in 76033e619fcSdrh ** how much of the tree is measured. 76133e619fcSdrh ** 76233e619fcSdrh ** dupedExprStructSize() Size of only the Expr structure 76333e619fcSdrh ** dupedExprNodeSize() Size of Expr + space for token 76433e619fcSdrh ** dupedExprSize() Expr + token + subtree components 76533e619fcSdrh ** 76633e619fcSdrh *************************************************************************** 76733e619fcSdrh ** 76833e619fcSdrh ** The dupedExprStructSize() function returns two values OR-ed together: 76933e619fcSdrh ** (1) the space required for a copy of the Expr structure only and 77033e619fcSdrh ** (2) the EP_xxx flags that indicate what the structure size should be. 77133e619fcSdrh ** The return values is always one of: 77233e619fcSdrh ** 77333e619fcSdrh ** EXPR_FULLSIZE 77433e619fcSdrh ** EXPR_REDUCEDSIZE | EP_Reduced 77533e619fcSdrh ** EXPR_TOKENONLYSIZE | EP_TokenOnly 77633e619fcSdrh ** 77733e619fcSdrh ** The size of the structure can be found by masking the return value 77833e619fcSdrh ** of this routine with 0xfff. The flags can be found by masking the 77933e619fcSdrh ** return value with EP_Reduced|EP_TokenOnly. 78033e619fcSdrh ** 78133e619fcSdrh ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size 78233e619fcSdrh ** (unreduced) Expr objects as they or originally constructed by the parser. 78333e619fcSdrh ** During expression analysis, extra information is computed and moved into 78433e619fcSdrh ** later parts of teh Expr object and that extra information might get chopped 78533e619fcSdrh ** off if the expression is reduced. Note also that it does not work to 78660ec914cSpeter.d.reid ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal 78733e619fcSdrh ** to reduce a pristine expression tree from the parser. The implementation 78833e619fcSdrh ** of dupedExprStructSize() contain multiple assert() statements that attempt 78933e619fcSdrh ** to enforce this constraint. 7906ab3a2ecSdanielk1977 */ 7916ab3a2ecSdanielk1977 static int dupedExprStructSize(Expr *p, int flags){ 7926ab3a2ecSdanielk1977 int nSize; 79333e619fcSdrh assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ 794aecd8021Sdrh assert( EXPR_FULLSIZE<=0xfff ); 795aecd8021Sdrh assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); 7966ab3a2ecSdanielk1977 if( 0==(flags&EXPRDUP_REDUCE) ){ 7976ab3a2ecSdanielk1977 nSize = EXPR_FULLSIZE; 7986ab3a2ecSdanielk1977 }else{ 799c5cd1249Sdrh assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); 80033e619fcSdrh assert( !ExprHasProperty(p, EP_FromJoin) ); 801c5cd1249Sdrh assert( !ExprHasProperty(p, EP_MemToken) ); 802ebb6a65dSdrh assert( !ExprHasProperty(p, EP_NoReduce) ); 803aecd8021Sdrh if( p->pLeft || p->x.pList ){ 80433e619fcSdrh nSize = EXPR_REDUCEDSIZE | EP_Reduced; 80533e619fcSdrh }else{ 806aecd8021Sdrh assert( p->pRight==0 ); 80733e619fcSdrh nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; 80833e619fcSdrh } 8096ab3a2ecSdanielk1977 } 8106ab3a2ecSdanielk1977 return nSize; 8116ab3a2ecSdanielk1977 } 8126ab3a2ecSdanielk1977 8136ab3a2ecSdanielk1977 /* 81433e619fcSdrh ** This function returns the space in bytes required to store the copy 81533e619fcSdrh ** of the Expr structure and a copy of the Expr.u.zToken string (if that 81633e619fcSdrh ** string is defined.) 8176ab3a2ecSdanielk1977 */ 8186ab3a2ecSdanielk1977 static int dupedExprNodeSize(Expr *p, int flags){ 81933e619fcSdrh int nByte = dupedExprStructSize(p, flags) & 0xfff; 82033e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 82133e619fcSdrh nByte += sqlite3Strlen30(p->u.zToken)+1; 8226ab3a2ecSdanielk1977 } 823bc73971dSdanielk1977 return ROUND8(nByte); 8246ab3a2ecSdanielk1977 } 8256ab3a2ecSdanielk1977 8266ab3a2ecSdanielk1977 /* 8276ab3a2ecSdanielk1977 ** Return the number of bytes required to create a duplicate of the 8286ab3a2ecSdanielk1977 ** expression passed as the first argument. The second argument is a 8296ab3a2ecSdanielk1977 ** mask containing EXPRDUP_XXX flags. 8306ab3a2ecSdanielk1977 ** 8316ab3a2ecSdanielk1977 ** The value returned includes space to create a copy of the Expr struct 83233e619fcSdrh ** itself and the buffer referred to by Expr.u.zToken, if any. 8336ab3a2ecSdanielk1977 ** 8346ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 8356ab3a2ecSdanielk1977 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 8366ab3a2ecSdanielk1977 ** and Expr.pRight variables (but not for any structures pointed to or 8376ab3a2ecSdanielk1977 ** descended from the Expr.x.pList or Expr.x.pSelect variables). 8386ab3a2ecSdanielk1977 */ 8396ab3a2ecSdanielk1977 static int dupedExprSize(Expr *p, int flags){ 8406ab3a2ecSdanielk1977 int nByte = 0; 8416ab3a2ecSdanielk1977 if( p ){ 8426ab3a2ecSdanielk1977 nByte = dupedExprNodeSize(p, flags); 8436ab3a2ecSdanielk1977 if( flags&EXPRDUP_REDUCE ){ 844b7916a78Sdrh nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); 8456ab3a2ecSdanielk1977 } 8466ab3a2ecSdanielk1977 } 8476ab3a2ecSdanielk1977 return nByte; 8486ab3a2ecSdanielk1977 } 8496ab3a2ecSdanielk1977 8506ab3a2ecSdanielk1977 /* 8516ab3a2ecSdanielk1977 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 8526ab3a2ecSdanielk1977 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 85333e619fcSdrh ** to store the copy of expression p, the copies of p->u.zToken 8546ab3a2ecSdanielk1977 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, 85560ec914cSpeter.d.reid ** if any. Before returning, *pzBuffer is set to the first byte past the 8566ab3a2ecSdanielk1977 ** portion of the buffer copied into by this function. 8576ab3a2ecSdanielk1977 */ 8586ab3a2ecSdanielk1977 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){ 8596ab3a2ecSdanielk1977 Expr *pNew = 0; /* Value to return */ 86072ea29d7Sdrh assert( flags==0 || flags==EXPRDUP_REDUCE ); 861575fad65Sdrh assert( db!=0 ); 8626ab3a2ecSdanielk1977 if( p ){ 8636ab3a2ecSdanielk1977 const int isReduced = (flags&EXPRDUP_REDUCE); 8646ab3a2ecSdanielk1977 u8 *zAlloc; 86533e619fcSdrh u32 staticFlag = 0; 8666ab3a2ecSdanielk1977 8676ab3a2ecSdanielk1977 assert( pzBuffer==0 || isReduced ); 8686ab3a2ecSdanielk1977 8696ab3a2ecSdanielk1977 /* Figure out where to write the new Expr structure. */ 8706ab3a2ecSdanielk1977 if( pzBuffer ){ 8716ab3a2ecSdanielk1977 zAlloc = *pzBuffer; 87233e619fcSdrh staticFlag = EP_Static; 8736ab3a2ecSdanielk1977 }else{ 874575fad65Sdrh zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, flags)); 8756ab3a2ecSdanielk1977 } 8766ab3a2ecSdanielk1977 pNew = (Expr *)zAlloc; 8776ab3a2ecSdanielk1977 8786ab3a2ecSdanielk1977 if( pNew ){ 8796ab3a2ecSdanielk1977 /* Set nNewSize to the size allocated for the structure pointed to 8806ab3a2ecSdanielk1977 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or 8816ab3a2ecSdanielk1977 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed 88233e619fcSdrh ** by the copy of the p->u.zToken string (if any). 8836ab3a2ecSdanielk1977 */ 88433e619fcSdrh const unsigned nStructSize = dupedExprStructSize(p, flags); 88533e619fcSdrh const int nNewSize = nStructSize & 0xfff; 88633e619fcSdrh int nToken; 88733e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 88833e619fcSdrh nToken = sqlite3Strlen30(p->u.zToken) + 1; 88933e619fcSdrh }else{ 89033e619fcSdrh nToken = 0; 89133e619fcSdrh } 8926ab3a2ecSdanielk1977 if( isReduced ){ 8936ab3a2ecSdanielk1977 assert( ExprHasProperty(p, EP_Reduced)==0 ); 8946ab3a2ecSdanielk1977 memcpy(zAlloc, p, nNewSize); 8956ab3a2ecSdanielk1977 }else{ 8963e6a1411Sdan u32 nSize = (u32)exprStructSize(p); 8976ab3a2ecSdanielk1977 memcpy(zAlloc, p, nSize); 89872ea29d7Sdrh if( nSize<EXPR_FULLSIZE ){ 8996ab3a2ecSdanielk1977 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 9006ab3a2ecSdanielk1977 } 90172ea29d7Sdrh } 9026ab3a2ecSdanielk1977 90333e619fcSdrh /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 904c5cd1249Sdrh pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); 90533e619fcSdrh pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 90633e619fcSdrh pNew->flags |= staticFlag; 9076ab3a2ecSdanielk1977 90833e619fcSdrh /* Copy the p->u.zToken string, if any. */ 9096ab3a2ecSdanielk1977 if( nToken ){ 91033e619fcSdrh char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; 91133e619fcSdrh memcpy(zToken, p->u.zToken, nToken); 9126ab3a2ecSdanielk1977 } 9136ab3a2ecSdanielk1977 9146ab3a2ecSdanielk1977 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ 9156ab3a2ecSdanielk1977 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ 9166ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 9176ab3a2ecSdanielk1977 pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced); 9186ab3a2ecSdanielk1977 }else{ 9196ab3a2ecSdanielk1977 pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced); 9206ab3a2ecSdanielk1977 } 9216ab3a2ecSdanielk1977 } 9226ab3a2ecSdanielk1977 9236ab3a2ecSdanielk1977 /* Fill in pNew->pLeft and pNew->pRight. */ 924c5cd1249Sdrh if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ 9256ab3a2ecSdanielk1977 zAlloc += dupedExprNodeSize(p, flags); 9266ab3a2ecSdanielk1977 if( ExprHasProperty(pNew, EP_Reduced) ){ 9276ab3a2ecSdanielk1977 pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc); 9286ab3a2ecSdanielk1977 pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc); 9296ab3a2ecSdanielk1977 } 9306ab3a2ecSdanielk1977 if( pzBuffer ){ 9316ab3a2ecSdanielk1977 *pzBuffer = zAlloc; 9326ab3a2ecSdanielk1977 } 933b7916a78Sdrh }else{ 934c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 9356ab3a2ecSdanielk1977 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); 9366ab3a2ecSdanielk1977 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); 9376ab3a2ecSdanielk1977 } 9386ab3a2ecSdanielk1977 } 939b7916a78Sdrh 940b7916a78Sdrh } 9416ab3a2ecSdanielk1977 } 9426ab3a2ecSdanielk1977 return pNew; 9436ab3a2ecSdanielk1977 } 9446ab3a2ecSdanielk1977 9456ab3a2ecSdanielk1977 /* 946bfe31e7fSdan ** Create and return a deep copy of the object passed as the second 947bfe31e7fSdan ** argument. If an OOM condition is encountered, NULL is returned 948bfe31e7fSdan ** and the db->mallocFailed flag set. 949bfe31e7fSdan */ 950eede6a53Sdan #ifndef SQLITE_OMIT_CTE 951bfe31e7fSdan static With *withDup(sqlite3 *db, With *p){ 9524e9119d9Sdan With *pRet = 0; 9534e9119d9Sdan if( p ){ 9544e9119d9Sdan int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); 9554e9119d9Sdan pRet = sqlite3DbMallocZero(db, nByte); 9564e9119d9Sdan if( pRet ){ 9574e9119d9Sdan int i; 9584e9119d9Sdan pRet->nCte = p->nCte; 9594e9119d9Sdan for(i=0; i<p->nCte; i++){ 9604e9119d9Sdan pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); 9614e9119d9Sdan pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); 9624e9119d9Sdan pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); 9634e9119d9Sdan } 9644e9119d9Sdan } 9654e9119d9Sdan } 9664e9119d9Sdan return pRet; 9674e9119d9Sdan } 968eede6a53Sdan #else 969eede6a53Sdan # define withDup(x,y) 0 970eede6a53Sdan #endif 9714e9119d9Sdan 972a76b5dfcSdrh /* 973ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 974ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 975ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 976ff78bd2fSdrh ** without effecting the originals. 977ff78bd2fSdrh ** 9784adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 9794adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 980ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 981ff78bd2fSdrh ** 982ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 9836ab3a2ecSdanielk1977 ** 984b7916a78Sdrh ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 9856ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 9866ab3a2ecSdanielk1977 ** truncated version of the usual Expr structure that will be stored as 9876ab3a2ecSdanielk1977 ** part of the in-memory representation of the database schema. 988ff78bd2fSdrh */ 9896ab3a2ecSdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ 99072ea29d7Sdrh assert( flags==0 || flags==EXPRDUP_REDUCE ); 9916ab3a2ecSdanielk1977 return exprDup(db, p, flags, 0); 992ff78bd2fSdrh } 9936ab3a2ecSdanielk1977 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ 994ff78bd2fSdrh ExprList *pNew; 995145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 996ff78bd2fSdrh int i; 997575fad65Sdrh assert( db!=0 ); 998ff78bd2fSdrh if( p==0 ) return 0; 999575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1000ff78bd2fSdrh if( pNew==0 ) return 0; 1001d872bb18Sdrh pNew->nExpr = i = p->nExpr; 1002d872bb18Sdrh if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){} 1003575fad65Sdrh pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) ); 1004e0048400Sdanielk1977 if( pItem==0 ){ 1005633e6d57Sdrh sqlite3DbFree(db, pNew); 1006e0048400Sdanielk1977 return 0; 1007e0048400Sdanielk1977 } 1008145716b3Sdrh pOldItem = p->a; 1009145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 10106ab3a2ecSdanielk1977 Expr *pOldExpr = pOldItem->pExpr; 1011b5526ea6Sdrh pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); 101217435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1013b7916a78Sdrh pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); 1014145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 10153e7bc9caSdrh pItem->done = 0; 10162c036cffSdrh pItem->bSpanIsTab = pOldItem->bSpanIsTab; 1017c2acc4e4Sdrh pItem->u = pOldItem->u; 1018ff78bd2fSdrh } 1019ff78bd2fSdrh return pNew; 1020ff78bd2fSdrh } 102193758c8dSdanielk1977 102293758c8dSdanielk1977 /* 102393758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 102493758c8dSdanielk1977 ** the build, then none of the following routines, except for 102593758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 102693758c8dSdanielk1977 ** called with a NULL argument. 102793758c8dSdanielk1977 */ 10286a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 10296a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 10306ab3a2ecSdanielk1977 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ 1031ad3cab52Sdrh SrcList *pNew; 1032ad3cab52Sdrh int i; 1033113088ecSdrh int nByte; 1034575fad65Sdrh assert( db!=0 ); 1035ad3cab52Sdrh if( p==0 ) return 0; 1036113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 1037575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, nByte ); 1038ad3cab52Sdrh if( pNew==0 ) return 0; 10394305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 1040ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 10414efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 10424efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 1043ed8a3bb1Sdrh Table *pTab; 104441fb5cd1Sdan pNewItem->pSchema = pOldItem->pSchema; 104517435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 104617435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 104717435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 10488a48b9c0Sdrh pNewItem->fg = pOldItem->fg; 10494efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 10505b6a9ed4Sdrh pNewItem->addrFillSub = pOldItem->addrFillSub; 10515b6a9ed4Sdrh pNewItem->regReturn = pOldItem->regReturn; 10528a48b9c0Sdrh if( pNewItem->fg.isIndexedBy ){ 10538a48b9c0Sdrh pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); 10548a48b9c0Sdrh } 10558a48b9c0Sdrh pNewItem->pIBIndex = pOldItem->pIBIndex; 10568a48b9c0Sdrh if( pNewItem->fg.isTabFunc ){ 10578a48b9c0Sdrh pNewItem->u1.pFuncArg = 10588a48b9c0Sdrh sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); 10598a48b9c0Sdrh } 1060ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 1061ed8a3bb1Sdrh if( pTab ){ 1062ed8a3bb1Sdrh pTab->nRef++; 1063a1cb183dSdanielk1977 } 10646ab3a2ecSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); 10656ab3a2ecSdanielk1977 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); 106617435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 10676c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 1068ad3cab52Sdrh } 1069ad3cab52Sdrh return pNew; 1070ad3cab52Sdrh } 107117435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 1072ff78bd2fSdrh IdList *pNew; 1073ff78bd2fSdrh int i; 1074575fad65Sdrh assert( db!=0 ); 1075ff78bd2fSdrh if( p==0 ) return 0; 1076575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1077ff78bd2fSdrh if( pNew==0 ) return 0; 10786c535158Sdrh pNew->nId = p->nId; 1079575fad65Sdrh pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); 1080d5d56523Sdanielk1977 if( pNew->a==0 ){ 1081633e6d57Sdrh sqlite3DbFree(db, pNew); 1082d5d56523Sdanielk1977 return 0; 1083d5d56523Sdanielk1977 } 10846c535158Sdrh /* Note that because the size of the allocation for p->a[] is not 10856c535158Sdrh ** necessarily a power of two, sqlite3IdListAppend() may not be called 10866c535158Sdrh ** on the duplicate created by this function. */ 1087ff78bd2fSdrh for(i=0; i<p->nId; i++){ 10884efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 10894efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 109017435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 10914efc4754Sdrh pNewItem->idx = pOldItem->idx; 1092ff78bd2fSdrh } 1093ff78bd2fSdrh return pNew; 1094ff78bd2fSdrh } 10956ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 109623b1b372Sdrh Select *pNew, *pPrior; 1097575fad65Sdrh assert( db!=0 ); 1098ff78bd2fSdrh if( p==0 ) return 0; 1099575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); 1100ff78bd2fSdrh if( pNew==0 ) return 0; 1101b7916a78Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); 11026ab3a2ecSdanielk1977 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); 11036ab3a2ecSdanielk1977 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); 11046ab3a2ecSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); 11056ab3a2ecSdanielk1977 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); 11066ab3a2ecSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); 1107ff78bd2fSdrh pNew->op = p->op; 110823b1b372Sdrh pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); 110923b1b372Sdrh if( pPrior ) pPrior->pNext = pNew; 111023b1b372Sdrh pNew->pNext = 0; 11116ab3a2ecSdanielk1977 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); 11126ab3a2ecSdanielk1977 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); 111392b01d53Sdrh pNew->iLimit = 0; 111492b01d53Sdrh pNew->iOffset = 0; 11157d10d5a6Sdrh pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; 1116b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 1117b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 1118ec2da854Sdrh pNew->nSelectRow = p->nSelectRow; 11194e9119d9Sdan pNew->pWith = withDup(db, p->pWith); 1120eb9b884cSdrh sqlite3SelectSetName(pNew, p->zSelName); 1121ff78bd2fSdrh return pNew; 1122ff78bd2fSdrh } 112393758c8dSdanielk1977 #else 11246ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 112593758c8dSdanielk1977 assert( p==0 ); 112693758c8dSdanielk1977 return 0; 112793758c8dSdanielk1977 } 112893758c8dSdanielk1977 #endif 1129ff78bd2fSdrh 1130ff78bd2fSdrh 1131ff78bd2fSdrh /* 1132a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 1133a76b5dfcSdrh ** initially NULL, then create a new expression list. 1134b7916a78Sdrh ** 1135b7916a78Sdrh ** If a memory allocation error occurs, the entire list is freed and 1136b7916a78Sdrh ** NULL is returned. If non-NULL is returned, then it is guaranteed 1137b7916a78Sdrh ** that the new entry was successfully appended. 1138a76b5dfcSdrh */ 113917435752Sdrh ExprList *sqlite3ExprListAppend( 114017435752Sdrh Parse *pParse, /* Parsing context */ 114117435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 1142b7916a78Sdrh Expr *pExpr /* Expression to be appended. Might be NULL */ 114317435752Sdrh ){ 114417435752Sdrh sqlite3 *db = pParse->db; 1145575fad65Sdrh assert( db!=0 ); 1146a76b5dfcSdrh if( pList==0 ){ 1147575fad65Sdrh pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); 1148a76b5dfcSdrh if( pList==0 ){ 1149d5d56523Sdanielk1977 goto no_mem; 1150a76b5dfcSdrh } 1151c263f7c4Sdrh pList->nExpr = 0; 1152575fad65Sdrh pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0])); 1153d872bb18Sdrh if( pList->a==0 ) goto no_mem; 1154d872bb18Sdrh }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ 1155d5d56523Sdanielk1977 struct ExprList_item *a; 1156d872bb18Sdrh assert( pList->nExpr>0 ); 1157d872bb18Sdrh a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); 1158d5d56523Sdanielk1977 if( a==0 ){ 1159d5d56523Sdanielk1977 goto no_mem; 1160a76b5dfcSdrh } 1161d5d56523Sdanielk1977 pList->a = a; 1162a76b5dfcSdrh } 11634efc4754Sdrh assert( pList->a!=0 ); 1164b7916a78Sdrh if( 1 ){ 11654efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 11664efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 1167e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 1168a76b5dfcSdrh } 1169a76b5dfcSdrh return pList; 1170d5d56523Sdanielk1977 1171d5d56523Sdanielk1977 no_mem: 1172d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 1173633e6d57Sdrh sqlite3ExprDelete(db, pExpr); 1174633e6d57Sdrh sqlite3ExprListDelete(db, pList); 1175d5d56523Sdanielk1977 return 0; 1176a76b5dfcSdrh } 1177a76b5dfcSdrh 1178a76b5dfcSdrh /* 1179bc622bc0Sdrh ** Set the sort order for the last element on the given ExprList. 1180bc622bc0Sdrh */ 1181bc622bc0Sdrh void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ 1182bc622bc0Sdrh if( p==0 ) return; 1183bc622bc0Sdrh assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); 1184bc622bc0Sdrh assert( p->nExpr>0 ); 1185bc622bc0Sdrh if( iSortOrder<0 ){ 1186bc622bc0Sdrh assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); 1187bc622bc0Sdrh return; 1188bc622bc0Sdrh } 1189bc622bc0Sdrh p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; 1190bc622bc0Sdrh } 1191bc622bc0Sdrh 1192bc622bc0Sdrh /* 1193b7916a78Sdrh ** Set the ExprList.a[].zName element of the most recently added item 1194b7916a78Sdrh ** on the expression list. 1195b7916a78Sdrh ** 1196b7916a78Sdrh ** pList might be NULL following an OOM error. But pName should never be 1197b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1198b7916a78Sdrh ** is set. 1199b7916a78Sdrh */ 1200b7916a78Sdrh void sqlite3ExprListSetName( 1201b7916a78Sdrh Parse *pParse, /* Parsing context */ 1202b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1203b7916a78Sdrh Token *pName, /* Name to be added */ 1204b7916a78Sdrh int dequote /* True to cause the name to be dequoted */ 1205b7916a78Sdrh ){ 1206b7916a78Sdrh assert( pList!=0 || pParse->db->mallocFailed!=0 ); 1207b7916a78Sdrh if( pList ){ 1208b7916a78Sdrh struct ExprList_item *pItem; 1209b7916a78Sdrh assert( pList->nExpr>0 ); 1210b7916a78Sdrh pItem = &pList->a[pList->nExpr-1]; 1211b7916a78Sdrh assert( pItem->zName==0 ); 1212b7916a78Sdrh pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); 1213b7916a78Sdrh if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName); 1214b7916a78Sdrh } 1215b7916a78Sdrh } 1216b7916a78Sdrh 1217b7916a78Sdrh /* 1218b7916a78Sdrh ** Set the ExprList.a[].zSpan element of the most recently added item 1219b7916a78Sdrh ** on the expression list. 1220b7916a78Sdrh ** 1221b7916a78Sdrh ** pList might be NULL following an OOM error. But pSpan should never be 1222b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1223b7916a78Sdrh ** is set. 1224b7916a78Sdrh */ 1225b7916a78Sdrh void sqlite3ExprListSetSpan( 1226b7916a78Sdrh Parse *pParse, /* Parsing context */ 1227b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1228b7916a78Sdrh ExprSpan *pSpan /* The span to be added */ 1229b7916a78Sdrh ){ 1230b7916a78Sdrh sqlite3 *db = pParse->db; 1231b7916a78Sdrh assert( pList!=0 || db->mallocFailed!=0 ); 1232b7916a78Sdrh if( pList ){ 1233b7916a78Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; 1234b7916a78Sdrh assert( pList->nExpr>0 ); 1235b7916a78Sdrh assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); 1236b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1237b7916a78Sdrh pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, 1238cf697396Sshane (int)(pSpan->zEnd - pSpan->zStart)); 1239b7916a78Sdrh } 1240b7916a78Sdrh } 1241b7916a78Sdrh 1242b7916a78Sdrh /* 12437a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 12447a15a4beSdanielk1977 ** leave an error message in pParse. 12457a15a4beSdanielk1977 */ 12467a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 12477a15a4beSdanielk1977 Parse *pParse, 12487a15a4beSdanielk1977 ExprList *pEList, 12497a15a4beSdanielk1977 const char *zObject 12507a15a4beSdanielk1977 ){ 1251b1a6c3c1Sdrh int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 1252c5499befSdrh testcase( pEList && pEList->nExpr==mx ); 1253c5499befSdrh testcase( pEList && pEList->nExpr==mx+1 ); 1254b1a6c3c1Sdrh if( pEList && pEList->nExpr>mx ){ 12557a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 12567a15a4beSdanielk1977 } 12577a15a4beSdanielk1977 } 12587a15a4beSdanielk1977 12597a15a4beSdanielk1977 /* 1260a76b5dfcSdrh ** Delete an entire expression list. 1261a76b5dfcSdrh */ 1262633e6d57Sdrh void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ 1263a76b5dfcSdrh int i; 1264be5c89acSdrh struct ExprList_item *pItem; 1265a76b5dfcSdrh if( pList==0 ) return; 1266d872bb18Sdrh assert( pList->a!=0 || pList->nExpr==0 ); 1267be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 1268633e6d57Sdrh sqlite3ExprDelete(db, pItem->pExpr); 1269633e6d57Sdrh sqlite3DbFree(db, pItem->zName); 1270b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1271a76b5dfcSdrh } 1272633e6d57Sdrh sqlite3DbFree(db, pList->a); 1273633e6d57Sdrh sqlite3DbFree(db, pList); 1274a76b5dfcSdrh } 1275a76b5dfcSdrh 1276a76b5dfcSdrh /* 12772308ed38Sdrh ** Return the bitwise-OR of all Expr.flags fields in the given 12782308ed38Sdrh ** ExprList. 1279885a5b03Sdrh */ 12802308ed38Sdrh u32 sqlite3ExprListFlags(const ExprList *pList){ 1281885a5b03Sdrh int i; 12822308ed38Sdrh u32 m = 0; 12832308ed38Sdrh if( pList ){ 1284885a5b03Sdrh for(i=0; i<pList->nExpr; i++){ 1285d0c73053Sdrh Expr *pExpr = pList->a[i].pExpr; 1286*de845c2fSdrh assert( pExpr!=0 ); 1287*de845c2fSdrh m |= pExpr->flags; 1288885a5b03Sdrh } 12892308ed38Sdrh } 12902308ed38Sdrh return m; 1291885a5b03Sdrh } 1292885a5b03Sdrh 1293885a5b03Sdrh /* 1294059b2d50Sdrh ** These routines are Walker callbacks used to check expressions to 1295059b2d50Sdrh ** see if they are "constant" for some definition of constant. The 1296059b2d50Sdrh ** Walker.eCode value determines the type of "constant" we are looking 1297059b2d50Sdrh ** for. 129873b211abSdrh ** 12997d10d5a6Sdrh ** These callback routines are used to implement the following: 1300626a879aSdrh ** 1301059b2d50Sdrh ** sqlite3ExprIsConstant() pWalker->eCode==1 1302059b2d50Sdrh ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 1303fcb9f4f3Sdrh ** sqlite3ExprIsTableConstant() pWalker->eCode==3 1304059b2d50Sdrh ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 130587abf5c0Sdrh ** 1306059b2d50Sdrh ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression 1307059b2d50Sdrh ** is found to not be a constant. 130887abf5c0Sdrh ** 1309feada2dfSdrh ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions 1310059b2d50Sdrh ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing 1311059b2d50Sdrh ** an existing schema and 4 when processing a new statement. A bound 1312feada2dfSdrh ** parameter raises an error for new statements, but is silently converted 1313feada2dfSdrh ** to NULL for existing schemas. This allows sqlite_master tables that 1314feada2dfSdrh ** contain a bound parameter because they were generated by older versions 1315feada2dfSdrh ** of SQLite to be parsed by newer versions of SQLite without raising a 1316feada2dfSdrh ** malformed schema error. 1317626a879aSdrh */ 13187d10d5a6Sdrh static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 1319626a879aSdrh 1320059b2d50Sdrh /* If pWalker->eCode is 2 then any term of the expression that comes from 1321059b2d50Sdrh ** the ON or USING clauses of a left join disqualifies the expression 13220a168377Sdrh ** from being considered constant. */ 1323059b2d50Sdrh if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ 1324059b2d50Sdrh pWalker->eCode = 0; 13257d10d5a6Sdrh return WRC_Abort; 13260a168377Sdrh } 13270a168377Sdrh 1328626a879aSdrh switch( pExpr->op ){ 1329eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 1330059b2d50Sdrh ** and either pWalker->eCode==4 or 5 or the function has the 1331059b2d50Sdrh ** SQLITE_FUNC_CONST flag. */ 1332eb55bd2fSdrh case TK_FUNCTION: 133363f84573Sdrh if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ 1334b1fba286Sdrh return WRC_Continue; 1335059b2d50Sdrh }else{ 1336059b2d50Sdrh pWalker->eCode = 0; 1337059b2d50Sdrh return WRC_Abort; 1338b1fba286Sdrh } 1339626a879aSdrh case TK_ID: 1340626a879aSdrh case TK_COLUMN: 1341626a879aSdrh case TK_AGG_FUNCTION: 134213449892Sdrh case TK_AGG_COLUMN: 1343c5499befSdrh testcase( pExpr->op==TK_ID ); 1344c5499befSdrh testcase( pExpr->op==TK_COLUMN ); 1345c5499befSdrh testcase( pExpr->op==TK_AGG_FUNCTION ); 1346c5499befSdrh testcase( pExpr->op==TK_AGG_COLUMN ); 1347059b2d50Sdrh if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ 1348059b2d50Sdrh return WRC_Continue; 1349059b2d50Sdrh }else{ 1350059b2d50Sdrh pWalker->eCode = 0; 13517d10d5a6Sdrh return WRC_Abort; 1352059b2d50Sdrh } 1353feada2dfSdrh case TK_VARIABLE: 1354059b2d50Sdrh if( pWalker->eCode==5 ){ 1355feada2dfSdrh /* Silently convert bound parameters that appear inside of CREATE 1356feada2dfSdrh ** statements into a NULL when parsing the CREATE statement text out 1357feada2dfSdrh ** of the sqlite_master table */ 1358feada2dfSdrh pExpr->op = TK_NULL; 1359059b2d50Sdrh }else if( pWalker->eCode==4 ){ 1360feada2dfSdrh /* A bound parameter in a CREATE statement that originates from 1361feada2dfSdrh ** sqlite3_prepare() causes an error */ 1362059b2d50Sdrh pWalker->eCode = 0; 1363feada2dfSdrh return WRC_Abort; 1364feada2dfSdrh } 1365feada2dfSdrh /* Fall through */ 1366626a879aSdrh default: 1367b74b1017Sdrh testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ 1368b74b1017Sdrh testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ 13697d10d5a6Sdrh return WRC_Continue; 1370626a879aSdrh } 1371626a879aSdrh } 137262c14b34Sdanielk1977 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ 137362c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 1374059b2d50Sdrh pWalker->eCode = 0; 13757d10d5a6Sdrh return WRC_Abort; 13767d10d5a6Sdrh } 1377059b2d50Sdrh static int exprIsConst(Expr *p, int initFlag, int iCur){ 13787d10d5a6Sdrh Walker w; 1379aa87f9a6Sdrh memset(&w, 0, sizeof(w)); 1380059b2d50Sdrh w.eCode = initFlag; 13817d10d5a6Sdrh w.xExprCallback = exprNodeIsConstant; 13827d10d5a6Sdrh w.xSelectCallback = selectNodeIsConstant; 1383059b2d50Sdrh w.u.iCur = iCur; 13847d10d5a6Sdrh sqlite3WalkExpr(&w, p); 1385059b2d50Sdrh return w.eCode; 13867d10d5a6Sdrh } 1387626a879aSdrh 1388626a879aSdrh /* 1389059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1390eb55bd2fSdrh ** and 0 if it involves variables or function calls. 13912398937bSdrh ** 13922398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 13932398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 13942398937bSdrh ** a constant. 1395fef5208cSdrh */ 13964adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 1397059b2d50Sdrh return exprIsConst(p, 1, 0); 1398fef5208cSdrh } 1399fef5208cSdrh 1400fef5208cSdrh /* 1401059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 14020a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 14030a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 14040a168377Sdrh ** an ON or USING clause. 14050a168377Sdrh */ 14060a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 1407059b2d50Sdrh return exprIsConst(p, 2, 0); 14080a168377Sdrh } 14090a168377Sdrh 14100a168377Sdrh /* 1411fcb9f4f3Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1412059b2d50Sdrh ** for any single row of the table with cursor iCur. In other words, the 1413059b2d50Sdrh ** expression must not refer to any non-deterministic function nor any 1414059b2d50Sdrh ** table other than iCur. 1415059b2d50Sdrh */ 1416059b2d50Sdrh int sqlite3ExprIsTableConstant(Expr *p, int iCur){ 1417059b2d50Sdrh return exprIsConst(p, 3, iCur); 1418059b2d50Sdrh } 1419059b2d50Sdrh 1420059b2d50Sdrh /* 1421059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1422eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 1423eb55bd2fSdrh ** are any variables. 1424eb55bd2fSdrh ** 1425eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 1426eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 1427eb55bd2fSdrh ** a constant. 1428eb55bd2fSdrh */ 1429feada2dfSdrh int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 1430feada2dfSdrh assert( isInit==0 || isInit==1 ); 1431059b2d50Sdrh return exprIsConst(p, 4+isInit, 0); 1432eb55bd2fSdrh } 1433eb55bd2fSdrh 14345b88bc4bSdrh #ifdef SQLITE_ENABLE_CURSOR_HINTS 14355b88bc4bSdrh /* 14365b88bc4bSdrh ** Walk an expression tree. Return 1 if the expression contains a 14375b88bc4bSdrh ** subquery of some kind. Return 0 if there are no subqueries. 14385b88bc4bSdrh */ 14395b88bc4bSdrh int sqlite3ExprContainsSubquery(Expr *p){ 14405b88bc4bSdrh Walker w; 14415b88bc4bSdrh memset(&w, 0, sizeof(w)); 1442bec2476aSdrh w.eCode = 1; 14435b88bc4bSdrh w.xExprCallback = sqlite3ExprWalkNoop; 14445b88bc4bSdrh w.xSelectCallback = selectNodeIsConstant; 14455b88bc4bSdrh sqlite3WalkExpr(&w, p); 144607194bffSdrh return w.eCode==0; 14475b88bc4bSdrh } 14485b88bc4bSdrh #endif 14495b88bc4bSdrh 1450eb55bd2fSdrh /* 145173b211abSdrh ** If the expression p codes a constant integer that is small enough 1452202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 1453202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 1454202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 1455e4de1febSdrh */ 14564adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 145792b01d53Sdrh int rc = 0; 1458cd92e84dSdrh 1459cd92e84dSdrh /* If an expression is an integer literal that fits in a signed 32-bit 1460cd92e84dSdrh ** integer, then the EP_IntValue flag will have already been set */ 1461cd92e84dSdrh assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 1462cd92e84dSdrh || sqlite3GetInt32(p->u.zToken, &rc)==0 ); 1463cd92e84dSdrh 146492b01d53Sdrh if( p->flags & EP_IntValue ){ 146533e619fcSdrh *pValue = p->u.iValue; 1466e4de1febSdrh return 1; 1467e4de1febSdrh } 146892b01d53Sdrh switch( p->op ){ 14694b59ab5eSdrh case TK_UPLUS: { 147092b01d53Sdrh rc = sqlite3ExprIsInteger(p->pLeft, pValue); 1471f6e369a1Sdrh break; 14724b59ab5eSdrh } 1473e4de1febSdrh case TK_UMINUS: { 1474e4de1febSdrh int v; 14754adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 1476f6418891Smistachkin assert( v!=(-2147483647-1) ); 1477e4de1febSdrh *pValue = -v; 147892b01d53Sdrh rc = 1; 1479e4de1febSdrh } 1480e4de1febSdrh break; 1481e4de1febSdrh } 1482e4de1febSdrh default: break; 1483e4de1febSdrh } 148492b01d53Sdrh return rc; 1485e4de1febSdrh } 1486e4de1febSdrh 1487e4de1febSdrh /* 1488039fc32eSdrh ** Return FALSE if there is no chance that the expression can be NULL. 1489039fc32eSdrh ** 1490039fc32eSdrh ** If the expression might be NULL or if the expression is too complex 1491039fc32eSdrh ** to tell return TRUE. 1492039fc32eSdrh ** 1493039fc32eSdrh ** This routine is used as an optimization, to skip OP_IsNull opcodes 1494039fc32eSdrh ** when we know that a value cannot be NULL. Hence, a false positive 1495039fc32eSdrh ** (returning TRUE when in fact the expression can never be NULL) might 1496039fc32eSdrh ** be a small performance hit but is otherwise harmless. On the other 1497039fc32eSdrh ** hand, a false negative (returning FALSE when the result could be NULL) 1498039fc32eSdrh ** will likely result in an incorrect answer. So when in doubt, return 1499039fc32eSdrh ** TRUE. 1500039fc32eSdrh */ 1501039fc32eSdrh int sqlite3ExprCanBeNull(const Expr *p){ 1502039fc32eSdrh u8 op; 1503cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1504039fc32eSdrh op = p->op; 1505039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1506039fc32eSdrh switch( op ){ 1507039fc32eSdrh case TK_INTEGER: 1508039fc32eSdrh case TK_STRING: 1509039fc32eSdrh case TK_FLOAT: 1510039fc32eSdrh case TK_BLOB: 1511039fc32eSdrh return 0; 15127248a8b2Sdrh case TK_COLUMN: 15137248a8b2Sdrh assert( p->pTab!=0 ); 151472673a24Sdrh return ExprHasProperty(p, EP_CanBeNull) || 151572673a24Sdrh (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); 1516039fc32eSdrh default: 1517039fc32eSdrh return 1; 1518039fc32eSdrh } 1519039fc32eSdrh } 1520039fc32eSdrh 1521039fc32eSdrh /* 1522039fc32eSdrh ** Return TRUE if the given expression is a constant which would be 1523039fc32eSdrh ** unchanged by OP_Affinity with the affinity given in the second 1524039fc32eSdrh ** argument. 1525039fc32eSdrh ** 1526039fc32eSdrh ** This routine is used to determine if the OP_Affinity operation 1527039fc32eSdrh ** can be omitted. When in doubt return FALSE. A false negative 1528039fc32eSdrh ** is harmless. A false positive, however, can result in the wrong 1529039fc32eSdrh ** answer. 1530039fc32eSdrh */ 1531039fc32eSdrh int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 1532039fc32eSdrh u8 op; 153305883a34Sdrh if( aff==SQLITE_AFF_BLOB ) return 1; 1534cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1535039fc32eSdrh op = p->op; 1536039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1537039fc32eSdrh switch( op ){ 1538039fc32eSdrh case TK_INTEGER: { 1539039fc32eSdrh return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; 1540039fc32eSdrh } 1541039fc32eSdrh case TK_FLOAT: { 1542039fc32eSdrh return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; 1543039fc32eSdrh } 1544039fc32eSdrh case TK_STRING: { 1545039fc32eSdrh return aff==SQLITE_AFF_TEXT; 1546039fc32eSdrh } 1547039fc32eSdrh case TK_BLOB: { 1548039fc32eSdrh return 1; 1549039fc32eSdrh } 15502f2855b6Sdrh case TK_COLUMN: { 155188376ca7Sdrh assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ 155288376ca7Sdrh return p->iColumn<0 15532f2855b6Sdrh && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); 15542f2855b6Sdrh } 1555039fc32eSdrh default: { 1556039fc32eSdrh return 0; 1557039fc32eSdrh } 1558039fc32eSdrh } 1559039fc32eSdrh } 1560039fc32eSdrh 1561039fc32eSdrh /* 1562c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 1563c4a3c779Sdrh */ 15644adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 15654adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 15664adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 15674adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 1568c4a3c779Sdrh return 0; 1569c4a3c779Sdrh } 1570c4a3c779Sdrh 15719a96b668Sdanielk1977 /* 157269c355bdSdrh ** pX is the RHS of an IN operator. If pX is a SELECT statement 157369c355bdSdrh ** that can be simplified to a direct table access, then return 157469c355bdSdrh ** a pointer to the SELECT statement. If pX is not a SELECT statement, 157569c355bdSdrh ** or if the SELECT statement needs to be manifested into a transient 157669c355bdSdrh ** table, then return NULL. 1577b287f4b6Sdrh */ 1578b287f4b6Sdrh #ifndef SQLITE_OMIT_SUBQUERY 157969c355bdSdrh static Select *isCandidateForInOpt(Expr *pX){ 158069c355bdSdrh Select *p; 1581b287f4b6Sdrh SrcList *pSrc; 1582b287f4b6Sdrh ExprList *pEList; 158369c355bdSdrh Expr *pRes; 1584b287f4b6Sdrh Table *pTab; 158569c355bdSdrh if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ 158669c355bdSdrh if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ 158769c355bdSdrh p = pX->x.pSelect; 1588b287f4b6Sdrh if( p->pPrior ) return 0; /* Not a compound SELECT */ 15897d10d5a6Sdrh if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ 1590b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); 1591b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); 15927d10d5a6Sdrh return 0; /* No DISTINCT keyword and no aggregate functions */ 15937d10d5a6Sdrh } 1594b74b1017Sdrh assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ 1595b287f4b6Sdrh if( p->pLimit ) return 0; /* Has no LIMIT clause */ 1596b74b1017Sdrh assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ 1597b287f4b6Sdrh if( p->pWhere ) return 0; /* Has no WHERE clause */ 1598b287f4b6Sdrh pSrc = p->pSrc; 1599d1fa7bcaSdrh assert( pSrc!=0 ); 1600d1fa7bcaSdrh if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ 1601b74b1017Sdrh if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ 1602b287f4b6Sdrh pTab = pSrc->a[0].pTab; 160369c355bdSdrh assert( pTab!=0 ); 1604b74b1017Sdrh assert( pTab->pSelect==0 ); /* FROM clause is not a view */ 1605b287f4b6Sdrh if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ 1606b287f4b6Sdrh pEList = p->pEList; 1607b287f4b6Sdrh if( pEList->nExpr!=1 ) return 0; /* One column in the result set */ 160890730c9eSdrh pRes = pEList->a[0].pExpr; 160990730c9eSdrh if( pRes->op!=TK_COLUMN ) return 0; /* Result is a column */ 161069c355bdSdrh assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ 161169c355bdSdrh return p; 1612b287f4b6Sdrh } 1613b287f4b6Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1614b287f4b6Sdrh 1615b287f4b6Sdrh /* 16161d8cb21fSdan ** Code an OP_Once instruction and allocate space for its flag. Return the 16171d8cb21fSdan ** address of the new instruction. 16181d8cb21fSdan */ 16191d8cb21fSdan int sqlite3CodeOnce(Parse *pParse){ 16201d8cb21fSdan Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 16211d8cb21fSdan return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); 16221d8cb21fSdan } 16231d8cb21fSdan 16241d8cb21fSdan /* 16254c259e9fSdrh ** Generate code that checks the left-most column of index table iCur to see if 16264c259e9fSdrh ** it contains any NULL entries. Cause the register at regHasNull to be set 16276be515ebSdrh ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 16286be515ebSdrh ** to be set to NULL if iCur contains one or more NULL values. 16296be515ebSdrh */ 16306be515ebSdrh static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 1631728e0f91Sdrh int addr1; 16326be515ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 1633728e0f91Sdrh addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 16346be515ebSdrh sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 16356be515ebSdrh sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 16364c259e9fSdrh VdbeComment((v, "first_entry_in(%d)", iCur)); 1637728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 16386be515ebSdrh } 16396be515ebSdrh 1640bb53ecb1Sdrh 1641bb53ecb1Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1642bb53ecb1Sdrh /* 1643bb53ecb1Sdrh ** The argument is an IN operator with a list (not a subquery) on the 1644bb53ecb1Sdrh ** right-hand side. Return TRUE if that list is constant. 1645bb53ecb1Sdrh */ 1646bb53ecb1Sdrh static int sqlite3InRhsIsConstant(Expr *pIn){ 1647bb53ecb1Sdrh Expr *pLHS; 1648bb53ecb1Sdrh int res; 1649bb53ecb1Sdrh assert( !ExprHasProperty(pIn, EP_xIsSelect) ); 1650bb53ecb1Sdrh pLHS = pIn->pLeft; 1651bb53ecb1Sdrh pIn->pLeft = 0; 1652bb53ecb1Sdrh res = sqlite3ExprIsConstant(pIn); 1653bb53ecb1Sdrh pIn->pLeft = pLHS; 1654bb53ecb1Sdrh return res; 1655bb53ecb1Sdrh } 1656bb53ecb1Sdrh #endif 1657bb53ecb1Sdrh 16586be515ebSdrh /* 16599a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 1660d4305ca6Sdrh ** The pX parameter is the expression on the RHS of the IN operator, which 1661d4305ca6Sdrh ** might be either a list of expressions or a subquery. 16629a96b668Sdanielk1977 ** 1663d4305ca6Sdrh ** The job of this routine is to find or create a b-tree object that can 1664d4305ca6Sdrh ** be used either to test for membership in the RHS set or to iterate through 1665d4305ca6Sdrh ** all members of the RHS set, skipping duplicates. 1666d4305ca6Sdrh ** 16673a85625dSdrh ** A cursor is opened on the b-tree object that is the RHS of the IN operator 1668d4305ca6Sdrh ** and pX->iTable is set to the index of that cursor. 1669d4305ca6Sdrh ** 1670b74b1017Sdrh ** The returned value of this function indicates the b-tree type, as follows: 16719a96b668Sdanielk1977 ** 16729a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 16731ccce449Sdrh ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. 16741ccce449Sdrh ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. 16759a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 16769a96b668Sdanielk1977 ** populated epheremal table. 1677bb53ecb1Sdrh ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be 1678bb53ecb1Sdrh ** implemented as a sequence of comparisons. 16799a96b668Sdanielk1977 ** 1680d4305ca6Sdrh ** An existing b-tree might be used if the RHS expression pX is a simple 1681d4305ca6Sdrh ** subquery such as: 16829a96b668Sdanielk1977 ** 16839a96b668Sdanielk1977 ** SELECT <column> FROM <table> 16849a96b668Sdanielk1977 ** 1685d4305ca6Sdrh ** If the RHS of the IN operator is a list or a more complex subquery, then 1686d4305ca6Sdrh ** an ephemeral table might need to be generated from the RHS and then 168760ec914cSpeter.d.reid ** pX->iTable made to point to the ephemeral table instead of an 1688d4305ca6Sdrh ** existing table. 1689d4305ca6Sdrh ** 16903a85625dSdrh ** The inFlags parameter must contain exactly one of the bits 16913a85625dSdrh ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains 16923a85625dSdrh ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a 16933a85625dSdrh ** fast membership test. When the IN_INDEX_LOOP bit is set, the 16943a85625dSdrh ** IN index will be used to loop over all values of the RHS of the 16953a85625dSdrh ** IN operator. 16963a85625dSdrh ** 16973a85625dSdrh ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate 16983a85625dSdrh ** through the set members) then the b-tree must not contain duplicates. 16993a85625dSdrh ** An epheremal table must be used unless the selected <column> is guaranteed 17009a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 1701b74b1017Sdrh ** has a UNIQUE constraint or UNIQUE index. 17020cdc022eSdanielk1977 ** 17033a85625dSdrh ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 17043a85625dSdrh ** for fast set membership tests) then an epheremal table must 17050cdc022eSdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 17060cdc022eSdanielk1977 ** be found with <column> as its left-most column. 17070cdc022eSdanielk1977 ** 1708bb53ecb1Sdrh ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and 1709bb53ecb1Sdrh ** if the RHS of the IN operator is a list (not a subquery) then this 1710bb53ecb1Sdrh ** routine might decide that creating an ephemeral b-tree for membership 1711bb53ecb1Sdrh ** testing is too expensive and return IN_INDEX_NOOP. In that case, the 1712bb53ecb1Sdrh ** calling routine should implement the IN operator using a sequence 1713bb53ecb1Sdrh ** of Eq or Ne comparison operations. 1714bb53ecb1Sdrh ** 1715b74b1017Sdrh ** When the b-tree is being used for membership tests, the calling function 17163a85625dSdrh ** might need to know whether or not the RHS side of the IN operator 1717e21a6e1dSdrh ** contains a NULL. If prRhsHasNull is not a NULL pointer and 17183a85625dSdrh ** if there is any chance that the (...) might contain a NULL value at 17190cdc022eSdanielk1977 ** runtime, then a register is allocated and the register number written 1720e21a6e1dSdrh ** to *prRhsHasNull. If there is no chance that the (...) contains a 1721e21a6e1dSdrh ** NULL value, then *prRhsHasNull is left unchanged. 17220cdc022eSdanielk1977 ** 1723e21a6e1dSdrh ** If a register is allocated and its location stored in *prRhsHasNull, then 17246be515ebSdrh ** the value in that register will be NULL if the b-tree contains one or more 17256be515ebSdrh ** NULL values, and it will be some non-NULL value if the b-tree contains no 17266be515ebSdrh ** NULL values. 17279a96b668Sdanielk1977 */ 1728284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1729e21a6e1dSdrh int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){ 1730b74b1017Sdrh Select *p; /* SELECT to the right of IN operator */ 1731b74b1017Sdrh int eType = 0; /* Type of RHS table. IN_INDEX_* */ 1732b74b1017Sdrh int iTab = pParse->nTab++; /* Cursor of the RHS table */ 17333a85625dSdrh int mustBeUnique; /* True if RHS must be unique */ 1734b8475df8Sdrh Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 17359a96b668Sdanielk1977 17361450bc6eSdrh assert( pX->op==TK_IN ); 17373a85625dSdrh mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 17381450bc6eSdrh 1739b74b1017Sdrh /* Check to see if an existing table or index can be used to 1740b74b1017Sdrh ** satisfy the query. This is preferable to generating a new 1741b74b1017Sdrh ** ephemeral table. 17429a96b668Sdanielk1977 */ 174369c355bdSdrh if( pParse->nErr==0 && (p = isCandidateForInOpt(pX))!=0 ){ 1744e1fb65a0Sdanielk1977 sqlite3 *db = pParse->db; /* Database connection */ 1745b07028f7Sdrh Table *pTab; /* Table <table>. */ 1746b07028f7Sdrh Expr *pExpr; /* Expression <column> */ 1747bbbdc83bSdrh i16 iCol; /* Index of column <column> */ 1748bbbdc83bSdrh i16 iDb; /* Database idx for pTab */ 1749e1fb65a0Sdanielk1977 1750b07028f7Sdrh assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 1751b07028f7Sdrh assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 1752b07028f7Sdrh assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 1753b07028f7Sdrh pTab = p->pSrc->a[0].pTab; 1754b07028f7Sdrh pExpr = p->pEList->a[0].pExpr; 1755bbbdc83bSdrh iCol = (i16)pExpr->iColumn; 1756b07028f7Sdrh 1757b22f7c83Sdrh /* Code an OP_Transaction and OP_TableLock for <table>. */ 1758e1fb65a0Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 1759e1fb65a0Sdanielk1977 sqlite3CodeVerifySchema(pParse, iDb); 1760e1fb65a0Sdanielk1977 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 17619a96b668Sdanielk1977 17629a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 17639a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 17649a96b668Sdanielk1977 ** successful here. 17659a96b668Sdanielk1977 */ 17669a96b668Sdanielk1977 assert(v); 17679a96b668Sdanielk1977 if( iCol<0 ){ 17687d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); 17697d176105Sdrh VdbeCoverage(v); 17709a96b668Sdanielk1977 17719a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 17729a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 17739a96b668Sdanielk1977 17749a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 17759a96b668Sdanielk1977 }else{ 1776e1fb65a0Sdanielk1977 Index *pIdx; /* Iterator variable */ 1777e1fb65a0Sdanielk1977 17789a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 17799a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 1780e1fb65a0Sdanielk1977 ** to this collation sequence. */ 17819a96b668Sdanielk1977 CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); 17829a96b668Sdanielk1977 17839a96b668Sdanielk1977 /* Check that the affinity that will be used to perform the 17849a96b668Sdanielk1977 ** comparison is the same as the affinity of the column. If 17859a96b668Sdanielk1977 ** it is not, it is not possible to use any index. 17869a96b668Sdanielk1977 */ 1787dbaee5e3Sdrh int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity); 17889a96b668Sdanielk1977 17899a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 17909a96b668Sdanielk1977 if( (pIdx->aiColumn[0]==iCol) 1791b74b1017Sdrh && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq 17925f1d1d9cSdrh && (!mustBeUnique || (pIdx->nKeyCol==1 && IsUniqueIndex(pIdx))) 17939a96b668Sdanielk1977 ){ 17947d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); 17952ec2fb22Sdrh sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); 17962ec2fb22Sdrh sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 1797207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 17981ccce449Sdrh assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); 17991ccce449Sdrh eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; 18009a96b668Sdanielk1977 1801e21a6e1dSdrh if( prRhsHasNull && !pTab->aCol[iCol].notNull ){ 1802e21a6e1dSdrh *prRhsHasNull = ++pParse->nMem; 18036be515ebSdrh sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); 18040cdc022eSdanielk1977 } 1805552fd454Sdrh sqlite3VdbeJumpHere(v, iAddr); 18069a96b668Sdanielk1977 } 18079a96b668Sdanielk1977 } 18089a96b668Sdanielk1977 } 18099a96b668Sdanielk1977 } 18109a96b668Sdanielk1977 1811bb53ecb1Sdrh /* If no preexisting index is available for the IN clause 1812bb53ecb1Sdrh ** and IN_INDEX_NOOP is an allowed reply 1813bb53ecb1Sdrh ** and the RHS of the IN operator is a list, not a subquery 1814bb53ecb1Sdrh ** and the RHS is not contant or has two or fewer terms, 181560ec914cSpeter.d.reid ** then it is not worth creating an ephemeral table to evaluate 1816bb53ecb1Sdrh ** the IN operator so return IN_INDEX_NOOP. 1817bb53ecb1Sdrh */ 1818bb53ecb1Sdrh if( eType==0 1819bb53ecb1Sdrh && (inFlags & IN_INDEX_NOOP_OK) 1820bb53ecb1Sdrh && !ExprHasProperty(pX, EP_xIsSelect) 1821bb53ecb1Sdrh && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) 1822bb53ecb1Sdrh ){ 1823bb53ecb1Sdrh eType = IN_INDEX_NOOP; 1824bb53ecb1Sdrh } 1825bb53ecb1Sdrh 1826bb53ecb1Sdrh 18279a96b668Sdanielk1977 if( eType==0 ){ 18284387006cSdrh /* Could not find an existing table or index to use as the RHS b-tree. 1829b74b1017Sdrh ** We will have to generate an ephemeral table to do the job. 1830b74b1017Sdrh */ 18318e23daf3Sdrh u32 savedNQueryLoop = pParse->nQueryLoop; 18320cdc022eSdanielk1977 int rMayHaveNull = 0; 183341a05b7bSdanielk1977 eType = IN_INDEX_EPH; 18343a85625dSdrh if( inFlags & IN_INDEX_LOOP ){ 18354a5acf8eSdrh pParse->nQueryLoop = 0; 1836c5cd1249Sdrh if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ 183741a05b7bSdanielk1977 eType = IN_INDEX_ROWID; 18380cdc022eSdanielk1977 } 1839e21a6e1dSdrh }else if( prRhsHasNull ){ 1840e21a6e1dSdrh *prRhsHasNull = rMayHaveNull = ++pParse->nMem; 1841cf4d38aaSdrh } 184241a05b7bSdanielk1977 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); 1843cf4d38aaSdrh pParse->nQueryLoop = savedNQueryLoop; 18449a96b668Sdanielk1977 }else{ 18459a96b668Sdanielk1977 pX->iTable = iTab; 18469a96b668Sdanielk1977 } 18479a96b668Sdanielk1977 return eType; 18489a96b668Sdanielk1977 } 1849284f4acaSdanielk1977 #endif 1850626a879aSdrh 1851626a879aSdrh /* 1852d4187c71Sdrh ** Generate code for scalar subqueries used as a subquery expression, EXISTS, 1853d4187c71Sdrh ** or IN operators. Examples: 1854626a879aSdrh ** 18559cbe6352Sdrh ** (SELECT a FROM b) -- subquery 18569cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 18579cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 18589cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 1859fef5208cSdrh ** 18609cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 18619cbe6352Sdrh ** operator or subquery. 186241a05b7bSdanielk1977 ** 186341a05b7bSdanielk1977 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed 186441a05b7bSdanielk1977 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference 186541a05b7bSdanielk1977 ** to some integer key column of a table B-Tree. In this case, use an 186641a05b7bSdanielk1977 ** intkey B-Tree to store the set of IN(...) values instead of the usual 186741a05b7bSdanielk1977 ** (slower) variable length keys B-Tree. 1868fd773cf9Sdrh ** 1869fd773cf9Sdrh ** If rMayHaveNull is non-zero, that means that the operation is an IN 1870fd773cf9Sdrh ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. 18713a85625dSdrh ** All this routine does is initialize the register given by rMayHaveNull 18723a85625dSdrh ** to NULL. Calling routines will take care of changing this register 18733a85625dSdrh ** value to non-NULL if the RHS is NULL-free. 18741450bc6eSdrh ** 18751450bc6eSdrh ** For a SELECT or EXISTS operator, return the register that holds the 18761450bc6eSdrh ** result. For IN operators or if an error occurs, the return value is 0. 1877cce7d176Sdrh */ 187851522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 18791450bc6eSdrh int sqlite3CodeSubselect( 1880fd773cf9Sdrh Parse *pParse, /* Parsing context */ 1881fd773cf9Sdrh Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ 18826be515ebSdrh int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ 1883fd773cf9Sdrh int isRowid /* If true, LHS of IN operator is a rowid */ 188441a05b7bSdanielk1977 ){ 18856be515ebSdrh int jmpIfDynamic = -1; /* One-time test address */ 18861450bc6eSdrh int rReg = 0; /* Register storing resulting */ 1887b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 18881450bc6eSdrh if( NEVER(v==0) ) return 0; 1889ceea3321Sdrh sqlite3ExprCachePush(pParse); 1890fc976065Sdanielk1977 189157dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 189257dbd7b3Sdrh ** if any of the following is true: 189357dbd7b3Sdrh ** 189457dbd7b3Sdrh ** * The right-hand side is a correlated subquery 189557dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 189657dbd7b3Sdrh ** * We are inside a trigger 189757dbd7b3Sdrh ** 189857dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 189957dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 1900b3bce662Sdanielk1977 */ 1901c5cd1249Sdrh if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 19026be515ebSdrh jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); 1903b3bce662Sdanielk1977 } 1904b3bce662Sdanielk1977 19054a07e3dbSdan #ifndef SQLITE_OMIT_EXPLAIN 19064a07e3dbSdan if( pParse->explain==2 ){ 190762aaa6caSdrh char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d", 190862aaa6caSdrh jmpIfDynamic>=0?"":"CORRELATED ", 190962aaa6caSdrh pExpr->op==TK_IN?"LIST":"SCALAR", 191062aaa6caSdrh pParse->iNextSelectId 19114a07e3dbSdan ); 19124a07e3dbSdan sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); 19134a07e3dbSdan } 19144a07e3dbSdan #endif 19154a07e3dbSdan 1916cce7d176Sdrh switch( pExpr->op ){ 1917fef5208cSdrh case TK_IN: { 1918d4187c71Sdrh char affinity; /* Affinity of the LHS of the IN */ 1919b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 1920d4187c71Sdrh Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ 1921323df790Sdrh KeyInfo *pKeyInfo = 0; /* Key information */ 1922d3d39e93Sdrh 192341a05b7bSdanielk1977 affinity = sqlite3ExprAffinity(pLeft); 1924e014a838Sdanielk1977 1925e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 19268cff69dfSdrh ** expression it is handled the same way. An ephemeral table is 1927e014a838Sdanielk1977 ** filled with single-field index keys representing the results 1928e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 1929fef5208cSdrh ** 1930e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 1931e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 1932e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 1933e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 1934e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 1935e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 1936e014a838Sdanielk1977 ** is used. 1937fef5208cSdrh */ 1938832508b7Sdrh pExpr->iTable = pParse->nTab++; 193941a05b7bSdanielk1977 addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); 1940ad124329Sdrh pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1); 1941e014a838Sdanielk1977 19426ab3a2ecSdanielk1977 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 1943e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 1944e014a838Sdanielk1977 ** 1945e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 1946e014a838Sdanielk1977 ** table allocated and opened above. 1947e014a838Sdanielk1977 */ 19484387006cSdrh Select *pSelect = pExpr->x.pSelect; 19491013c932Sdrh SelectDest dest; 1950be5c89acSdrh ExprList *pEList; 19511013c932Sdrh 195241a05b7bSdanielk1977 assert( !isRowid ); 19531013c932Sdrh sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 19542b596da8Sdrh dest.affSdst = (u8)affinity; 1955e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 19564387006cSdrh pSelect->iLimit = 0; 19574387006cSdrh testcase( pSelect->selFlags & SF_Distinct ); 1958812ea833Sdrh testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ 19594387006cSdrh if( sqlite3Select(pParse, pSelect, &dest) ){ 19602ec2fb22Sdrh sqlite3KeyInfoUnref(pKeyInfo); 19611450bc6eSdrh return 0; 196294ccde58Sdrh } 19634387006cSdrh pEList = pSelect->pEList; 1964812ea833Sdrh assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ 19653535ec3eSdrh assert( pEList!=0 ); 19663535ec3eSdrh assert( pEList->nExpr>0 ); 19672ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 1968323df790Sdrh pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, 1969be5c89acSdrh pEList->a[0].pExpr); 1970a7d2db17Sdrh }else if( ALWAYS(pExpr->x.pList!=0) ){ 1971fef5208cSdrh /* Case 2: expr IN (exprlist) 1972fef5208cSdrh ** 1973e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 1974e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 1975e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 1976e014a838Sdanielk1977 ** a column, use numeric affinity. 1977fef5208cSdrh */ 1978e014a838Sdanielk1977 int i; 19796ab3a2ecSdanielk1977 ExprList *pList = pExpr->x.pList; 198057dbd7b3Sdrh struct ExprList_item *pItem; 1981ecc31805Sdrh int r1, r2, r3; 198257dbd7b3Sdrh 1983e014a838Sdanielk1977 if( !affinity ){ 198405883a34Sdrh affinity = SQLITE_AFF_BLOB; 1985e014a838Sdanielk1977 } 1986323df790Sdrh if( pKeyInfo ){ 19872ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 1988323df790Sdrh pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 1989323df790Sdrh } 1990e014a838Sdanielk1977 1991e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 19922d401ab8Sdrh r1 = sqlite3GetTempReg(pParse); 19932d401ab8Sdrh r2 = sqlite3GetTempReg(pParse); 199437e08081Sdrh if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); 199557dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 199657dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 1997e05c929bSdrh int iValToIns; 1998e014a838Sdanielk1977 199957dbd7b3Sdrh /* If the expression is not constant then we will need to 200057dbd7b3Sdrh ** disable the test that was generated above that makes sure 200157dbd7b3Sdrh ** this code only executes once. Because for a non-constant 200257dbd7b3Sdrh ** expression we need to rerun this code each time. 200357dbd7b3Sdrh */ 20046be515ebSdrh if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ 20056be515ebSdrh sqlite3VdbeChangeToNoop(v, jmpIfDynamic); 20066be515ebSdrh jmpIfDynamic = -1; 20074794b980Sdrh } 2008e014a838Sdanielk1977 2009e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 2010e05c929bSdrh if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ 2011e05c929bSdrh sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); 2012e05c929bSdrh }else{ 2013ecc31805Sdrh r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); 201441a05b7bSdanielk1977 if( isRowid ){ 2015e05c929bSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, 2016e05c929bSdrh sqlite3VdbeCurrentAddr(v)+2); 2017688852abSdrh VdbeCoverage(v); 201841a05b7bSdanielk1977 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); 201941a05b7bSdanielk1977 }else{ 2020ecc31805Sdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); 20213c31fc23Sdrh sqlite3ExprCacheAffinityChange(pParse, r3, 1); 20222d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); 2023fef5208cSdrh } 202441a05b7bSdanielk1977 } 2025e05c929bSdrh } 20262d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r1); 20272d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r2); 2028fef5208cSdrh } 2029323df790Sdrh if( pKeyInfo ){ 20302ec2fb22Sdrh sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); 203141a05b7bSdanielk1977 } 2032b3bce662Sdanielk1977 break; 2033fef5208cSdrh } 2034fef5208cSdrh 203551522cd3Sdrh case TK_EXISTS: 2036fd773cf9Sdrh case TK_SELECT: 2037fd773cf9Sdrh default: { 2038fd773cf9Sdrh /* If this has to be a scalar SELECT. Generate code to put the 2039fef5208cSdrh ** value of this select in a memory cell and record the number 2040fd773cf9Sdrh ** of the memory cell in iColumn. If this is an EXISTS, write 2041fd773cf9Sdrh ** an integer 0 (not exists) or 1 (exists) into a memory cell 2042fd773cf9Sdrh ** and record that memory cell in iColumn. 2043fef5208cSdrh */ 2044fd773cf9Sdrh Select *pSel; /* SELECT statement to encode */ 2045fd773cf9Sdrh SelectDest dest; /* How to deal with SELECt result */ 20461398ad36Sdrh 2047cf697396Sshane testcase( pExpr->op==TK_EXISTS ); 2048cf697396Sshane testcase( pExpr->op==TK_SELECT ); 2049cf697396Sshane assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); 2050cf697396Sshane 20516ab3a2ecSdanielk1977 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); 20526ab3a2ecSdanielk1977 pSel = pExpr->x.pSelect; 20531013c932Sdrh sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); 205451522cd3Sdrh if( pExpr->op==TK_SELECT ){ 20556c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 205653932ce8Sdrh dest.iSdst = dest.iSDParm; 20572b596da8Sdrh sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm); 2058d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 205951522cd3Sdrh }else{ 20606c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 20612b596da8Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 2062d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 206351522cd3Sdrh } 2064633e6d57Sdrh sqlite3ExprDelete(pParse->db, pSel->pLimit); 2065094430ebSdrh pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, 2066094430ebSdrh &sqlite3IntTokens[1]); 206748b5b041Sdrh pSel->iLimit = 0; 2068772460fdSdrh pSel->selFlags &= ~SF_MultiValue; 20697d10d5a6Sdrh if( sqlite3Select(pParse, pSel, &dest) ){ 20701450bc6eSdrh return 0; 207194ccde58Sdrh } 20722b596da8Sdrh rReg = dest.iSDParm; 2073ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 2074b3bce662Sdanielk1977 break; 207519a775c2Sdrh } 2076cce7d176Sdrh } 2077b3bce662Sdanielk1977 20786be515ebSdrh if( rHasNullFlag ){ 20796be515ebSdrh sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); 2080b3bce662Sdanielk1977 } 20816be515ebSdrh 20826be515ebSdrh if( jmpIfDynamic>=0 ){ 20836be515ebSdrh sqlite3VdbeJumpHere(v, jmpIfDynamic); 2084b3bce662Sdanielk1977 } 2085d2490904Sdrh sqlite3ExprCachePop(pParse); 2086fc976065Sdanielk1977 20871450bc6eSdrh return rReg; 2088cce7d176Sdrh } 208951522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2090cce7d176Sdrh 2091e3365e6cSdrh #ifndef SQLITE_OMIT_SUBQUERY 2092e3365e6cSdrh /* 2093e3365e6cSdrh ** Generate code for an IN expression. 2094e3365e6cSdrh ** 2095e3365e6cSdrh ** x IN (SELECT ...) 2096e3365e6cSdrh ** x IN (value, value, ...) 2097e3365e6cSdrh ** 2098e3365e6cSdrh ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) 2099e3365e6cSdrh ** is an array of zero or more values. The expression is true if the LHS is 2100e3365e6cSdrh ** contained within the RHS. The value of the expression is unknown (NULL) 2101e3365e6cSdrh ** if the LHS is NULL or if the LHS is not contained within the RHS and the 2102e3365e6cSdrh ** RHS contains one or more NULL values. 2103e3365e6cSdrh ** 21046be515ebSdrh ** This routine generates code that jumps to destIfFalse if the LHS is not 2105e3365e6cSdrh ** contained within the RHS. If due to NULLs we cannot determine if the LHS 2106e3365e6cSdrh ** is contained in the RHS then jump to destIfNull. If the LHS is contained 2107e3365e6cSdrh ** within the RHS then fall through. 2108e3365e6cSdrh */ 2109e3365e6cSdrh static void sqlite3ExprCodeIN( 2110e3365e6cSdrh Parse *pParse, /* Parsing and code generating context */ 2111e3365e6cSdrh Expr *pExpr, /* The IN expression */ 2112e3365e6cSdrh int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 2113e3365e6cSdrh int destIfNull /* Jump here if the results are unknown due to NULLs */ 2114e3365e6cSdrh ){ 2115e3365e6cSdrh int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ 2116e3365e6cSdrh char affinity; /* Comparison affinity to use */ 2117e3365e6cSdrh int eType; /* Type of the RHS */ 2118e3365e6cSdrh int r1; /* Temporary use register */ 2119e3365e6cSdrh Vdbe *v; /* Statement under construction */ 2120e3365e6cSdrh 2121e3365e6cSdrh /* Compute the RHS. After this step, the table with cursor 2122e3365e6cSdrh ** pExpr->iTable will contains the values that make up the RHS. 2123e3365e6cSdrh */ 2124e3365e6cSdrh v = pParse->pVdbe; 2125e3365e6cSdrh assert( v!=0 ); /* OOM detected prior to this routine */ 2126e3365e6cSdrh VdbeNoopComment((v, "begin IN expr")); 2127bb53ecb1Sdrh eType = sqlite3FindInIndex(pParse, pExpr, 2128bb53ecb1Sdrh IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, 21293a85625dSdrh destIfFalse==destIfNull ? 0 : &rRhsHasNull); 2130e3365e6cSdrh 2131e3365e6cSdrh /* Figure out the affinity to use to create a key from the results 2132e3365e6cSdrh ** of the expression. affinityStr stores a static string suitable for 2133e3365e6cSdrh ** P4 of OP_MakeRecord. 2134e3365e6cSdrh */ 2135e3365e6cSdrh affinity = comparisonAffinity(pExpr); 2136e3365e6cSdrh 2137e3365e6cSdrh /* Code the LHS, the <expr> from "<expr> IN (...)". 2138e3365e6cSdrh */ 2139e3365e6cSdrh sqlite3ExprCachePush(pParse); 2140e3365e6cSdrh r1 = sqlite3GetTempReg(pParse); 2141e3365e6cSdrh sqlite3ExprCode(pParse, pExpr->pLeft, r1); 2142e3365e6cSdrh 2143bb53ecb1Sdrh /* If sqlite3FindInIndex() did not find or create an index that is 2144bb53ecb1Sdrh ** suitable for evaluating the IN operator, then evaluate using a 2145bb53ecb1Sdrh ** sequence of comparisons. 2146bb53ecb1Sdrh */ 2147bb53ecb1Sdrh if( eType==IN_INDEX_NOOP ){ 2148bb53ecb1Sdrh ExprList *pList = pExpr->x.pList; 2149bb53ecb1Sdrh CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2150bb53ecb1Sdrh int labelOk = sqlite3VdbeMakeLabel(v); 2151bb53ecb1Sdrh int r2, regToFree; 2152bb53ecb1Sdrh int regCkNull = 0; 2153bb53ecb1Sdrh int ii; 2154bb53ecb1Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2155bb53ecb1Sdrh if( destIfNull!=destIfFalse ){ 2156bb53ecb1Sdrh regCkNull = sqlite3GetTempReg(pParse); 2157a976979bSdrh sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); 2158bb53ecb1Sdrh } 2159bb53ecb1Sdrh for(ii=0; ii<pList->nExpr; ii++){ 2160bb53ecb1Sdrh r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); 2161a976979bSdrh if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ 2162bb53ecb1Sdrh sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); 2163bb53ecb1Sdrh } 2164bb53ecb1Sdrh if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ 2165bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, 21664336b0e6Sdrh (void*)pColl, P4_COLLSEQ); 21674336b0e6Sdrh VdbeCoverageIf(v, ii<pList->nExpr-1); 21684336b0e6Sdrh VdbeCoverageIf(v, ii==pList->nExpr-1); 2169bb53ecb1Sdrh sqlite3VdbeChangeP5(v, affinity); 2170bb53ecb1Sdrh }else{ 2171bb53ecb1Sdrh assert( destIfNull==destIfFalse ); 2172bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, 2173bb53ecb1Sdrh (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); 2174bb53ecb1Sdrh sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL); 2175bb53ecb1Sdrh } 2176bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regToFree); 2177bb53ecb1Sdrh } 2178bb53ecb1Sdrh if( regCkNull ){ 2179bb53ecb1Sdrh sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 2180076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2181bb53ecb1Sdrh } 2182bb53ecb1Sdrh sqlite3VdbeResolveLabel(v, labelOk); 2183bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regCkNull); 2184bb53ecb1Sdrh }else{ 2185bb53ecb1Sdrh 2186094430ebSdrh /* If the LHS is NULL, then the result is either false or NULL depending 2187094430ebSdrh ** on whether the RHS is empty or not, respectively. 2188094430ebSdrh */ 21897248a8b2Sdrh if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ 2190094430ebSdrh if( destIfNull==destIfFalse ){ 2191094430ebSdrh /* Shortcut for the common case where the false and NULL outcomes are 2192094430ebSdrh ** the same. */ 2193688852abSdrh sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v); 2194094430ebSdrh }else{ 2195688852abSdrh int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); 2196094430ebSdrh sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 2197688852abSdrh VdbeCoverage(v); 2198076e85f5Sdrh sqlite3VdbeGoto(v, destIfNull); 2199094430ebSdrh sqlite3VdbeJumpHere(v, addr1); 2200094430ebSdrh } 22017248a8b2Sdrh } 2202e3365e6cSdrh 2203e3365e6cSdrh if( eType==IN_INDEX_ROWID ){ 2204e3365e6cSdrh /* In this case, the RHS is the ROWID of table b-tree 2205e3365e6cSdrh */ 2206688852abSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v); 2207e3365e6cSdrh sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1); 2208688852abSdrh VdbeCoverage(v); 2209e3365e6cSdrh }else{ 2210e3365e6cSdrh /* In this case, the RHS is an index b-tree. 2211e3365e6cSdrh */ 22128cff69dfSdrh sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); 2213e3365e6cSdrh 2214e3365e6cSdrh /* If the set membership test fails, then the result of the 2215e3365e6cSdrh ** "x IN (...)" expression must be either 0 or NULL. If the set 2216e3365e6cSdrh ** contains no NULL values, then the result is 0. If the set 2217e3365e6cSdrh ** contains one or more NULL values, then the result of the 2218e3365e6cSdrh ** expression is also NULL. 2219e3365e6cSdrh */ 2220e80c9b9aSdrh assert( destIfFalse!=destIfNull || rRhsHasNull==0 ); 2221e80c9b9aSdrh if( rRhsHasNull==0 ){ 2222e3365e6cSdrh /* This branch runs if it is known at compile time that the RHS 2223e3365e6cSdrh ** cannot contain NULL values. This happens as the result 2224e3365e6cSdrh ** of a "NOT NULL" constraint in the database schema. 2225e3365e6cSdrh ** 2226e3365e6cSdrh ** Also run this branch if NULL is equivalent to FALSE 2227e3365e6cSdrh ** for this particular IN operator. 2228e3365e6cSdrh */ 22298cff69dfSdrh sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); 2230688852abSdrh VdbeCoverage(v); 2231e3365e6cSdrh }else{ 2232e3365e6cSdrh /* In this branch, the RHS of the IN might contain a NULL and 2233e3365e6cSdrh ** the presence of a NULL on the RHS makes a difference in the 2234e3365e6cSdrh ** outcome. 2235e3365e6cSdrh */ 2236728e0f91Sdrh int addr1; 2237e3365e6cSdrh 2238e3365e6cSdrh /* First check to see if the LHS is contained in the RHS. If so, 22396be515ebSdrh ** then the answer is TRUE the presence of NULLs in the RHS does 22406be515ebSdrh ** not matter. If the LHS is not contained in the RHS, then the 22416be515ebSdrh ** answer is NULL if the RHS contains NULLs and the answer is 22426be515ebSdrh ** FALSE if the RHS is NULL-free. 2243e3365e6cSdrh */ 2244728e0f91Sdrh addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); 2245688852abSdrh VdbeCoverage(v); 22466be515ebSdrh sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); 2247552fd454Sdrh VdbeCoverage(v); 2248076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2249728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 2250e3365e6cSdrh } 2251e3365e6cSdrh } 2252bb53ecb1Sdrh } 2253e3365e6cSdrh sqlite3ReleaseTempReg(pParse, r1); 2254d2490904Sdrh sqlite3ExprCachePop(pParse); 2255e3365e6cSdrh VdbeComment((v, "end IN expr")); 2256e3365e6cSdrh } 2257e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2258e3365e6cSdrh 225913573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2260598f1340Sdrh /* 2261598f1340Sdrh ** Generate an instruction that will put the floating point 22629cbf3425Sdrh ** value described by z[0..n-1] into register iMem. 22630cf19ed8Sdrh ** 22640cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 22650cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 22660cf19ed8Sdrh ** like the continuation of the number. 2267598f1340Sdrh */ 2268b7916a78Sdrh static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 2269fd773cf9Sdrh if( ALWAYS(z!=0) ){ 2270598f1340Sdrh double value; 22719339da1fSdrh sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 2272d0015161Sdrh assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 2273598f1340Sdrh if( negateFlag ) value = -value; 227497bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); 2275598f1340Sdrh } 2276598f1340Sdrh } 227713573c71Sdrh #endif 2278598f1340Sdrh 2279598f1340Sdrh 2280598f1340Sdrh /* 2281fec19aadSdrh ** Generate an instruction that will put the integer describe by 22829cbf3425Sdrh ** text z[0..n-1] into register iMem. 22830cf19ed8Sdrh ** 22845f1d6b61Sshaneh ** Expr.u.zToken is always UTF8 and zero-terminated. 2285fec19aadSdrh */ 228613573c71Sdrh static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 228713573c71Sdrh Vdbe *v = pParse->pVdbe; 228892b01d53Sdrh if( pExpr->flags & EP_IntValue ){ 228933e619fcSdrh int i = pExpr->u.iValue; 2290d50ffc41Sdrh assert( i>=0 ); 229192b01d53Sdrh if( negFlag ) i = -i; 229292b01d53Sdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 2293fd773cf9Sdrh }else{ 22945f1d6b61Sshaneh int c; 22955f1d6b61Sshaneh i64 value; 2296fd773cf9Sdrh const char *z = pExpr->u.zToken; 2297fd773cf9Sdrh assert( z!=0 ); 22989296c18aSdrh c = sqlite3DecOrHexToI64(z, &value); 22995f1d6b61Sshaneh if( c==0 || (c==2 && negFlag) ){ 2300158b9cb9Sdrh if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } 230197bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); 2302fec19aadSdrh }else{ 230313573c71Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 230413573c71Sdrh sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); 230513573c71Sdrh #else 23061b7ddc59Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER 23079296c18aSdrh if( sqlite3_strnicmp(z,"0x",2)==0 ){ 23089296c18aSdrh sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); 23091b7ddc59Sdrh }else 23101b7ddc59Sdrh #endif 23111b7ddc59Sdrh { 2312b7916a78Sdrh codeReal(v, z, negFlag, iMem); 23139296c18aSdrh } 231413573c71Sdrh #endif 2315fec19aadSdrh } 2316fec19aadSdrh } 2317c9cf901dSdanielk1977 } 2318fec19aadSdrh 2319ceea3321Sdrh /* 2320ceea3321Sdrh ** Clear a cache entry. 2321ceea3321Sdrh */ 2322ceea3321Sdrh static void cacheEntryClear(Parse *pParse, struct yColCache *p){ 2323ceea3321Sdrh if( p->tempReg ){ 2324ceea3321Sdrh if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 2325ceea3321Sdrh pParse->aTempReg[pParse->nTempReg++] = p->iReg; 2326ceea3321Sdrh } 2327ceea3321Sdrh p->tempReg = 0; 2328ceea3321Sdrh } 2329ceea3321Sdrh } 2330ceea3321Sdrh 2331ceea3321Sdrh 2332ceea3321Sdrh /* 2333ceea3321Sdrh ** Record in the column cache that a particular column from a 2334ceea3321Sdrh ** particular table is stored in a particular register. 2335ceea3321Sdrh */ 2336ceea3321Sdrh void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ 2337ceea3321Sdrh int i; 2338ceea3321Sdrh int minLru; 2339ceea3321Sdrh int idxLru; 2340ceea3321Sdrh struct yColCache *p; 2341ceea3321Sdrh 2342ce8f53d4Sdan /* Unless an error has occurred, register numbers are always positive. */ 2343ce8f53d4Sdan assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); 234420411ea7Sdrh assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ 234520411ea7Sdrh 2346b6da74ebSdrh /* The SQLITE_ColumnCache flag disables the column cache. This is used 2347b6da74ebSdrh ** for testing only - to verify that SQLite always gets the same answer 2348b6da74ebSdrh ** with and without the column cache. 2349b6da74ebSdrh */ 23507e5418e4Sdrh if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; 2351b6da74ebSdrh 235227ee406eSdrh /* First replace any existing entry. 235327ee406eSdrh ** 235427ee406eSdrh ** Actually, the way the column cache is currently used, we are guaranteed 235527ee406eSdrh ** that the object will never already be in cache. Verify this guarantee. 235627ee406eSdrh */ 235727ee406eSdrh #ifndef NDEBUG 2358ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 235927ee406eSdrh assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); 2360ceea3321Sdrh } 236127ee406eSdrh #endif 2362ceea3321Sdrh 2363ceea3321Sdrh /* Find an empty slot and replace it */ 2364ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2365ceea3321Sdrh if( p->iReg==0 ){ 2366ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2367ceea3321Sdrh p->iTable = iTab; 2368ceea3321Sdrh p->iColumn = iCol; 2369ceea3321Sdrh p->iReg = iReg; 2370ceea3321Sdrh p->tempReg = 0; 2371ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2372ceea3321Sdrh return; 2373ceea3321Sdrh } 2374ceea3321Sdrh } 2375ceea3321Sdrh 2376ceea3321Sdrh /* Replace the last recently used */ 2377ceea3321Sdrh minLru = 0x7fffffff; 2378ceea3321Sdrh idxLru = -1; 2379ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2380ceea3321Sdrh if( p->lru<minLru ){ 2381ceea3321Sdrh idxLru = i; 2382ceea3321Sdrh minLru = p->lru; 2383ceea3321Sdrh } 2384ceea3321Sdrh } 238520411ea7Sdrh if( ALWAYS(idxLru>=0) ){ 2386ceea3321Sdrh p = &pParse->aColCache[idxLru]; 2387ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2388ceea3321Sdrh p->iTable = iTab; 2389ceea3321Sdrh p->iColumn = iCol; 2390ceea3321Sdrh p->iReg = iReg; 2391ceea3321Sdrh p->tempReg = 0; 2392ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2393ceea3321Sdrh return; 2394ceea3321Sdrh } 2395ceea3321Sdrh } 2396ceea3321Sdrh 2397ceea3321Sdrh /* 2398f49f3523Sdrh ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. 2399f49f3523Sdrh ** Purge the range of registers from the column cache. 2400ceea3321Sdrh */ 2401f49f3523Sdrh void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ 2402ceea3321Sdrh int i; 2403f49f3523Sdrh int iLast = iReg + nReg - 1; 2404ceea3321Sdrh struct yColCache *p; 2405ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2406f49f3523Sdrh int r = p->iReg; 2407f49f3523Sdrh if( r>=iReg && r<=iLast ){ 2408ceea3321Sdrh cacheEntryClear(pParse, p); 2409ceea3321Sdrh p->iReg = 0; 2410ceea3321Sdrh } 2411ceea3321Sdrh } 2412ceea3321Sdrh } 2413ceea3321Sdrh 2414ceea3321Sdrh /* 2415ceea3321Sdrh ** Remember the current column cache context. Any new entries added 2416ceea3321Sdrh ** added to the column cache after this call are removed when the 2417ceea3321Sdrh ** corresponding pop occurs. 2418ceea3321Sdrh */ 2419ceea3321Sdrh void sqlite3ExprCachePush(Parse *pParse){ 2420ceea3321Sdrh pParse->iCacheLevel++; 24219ac7962aSdrh #ifdef SQLITE_DEBUG 24229ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 24239ac7962aSdrh printf("PUSH to %d\n", pParse->iCacheLevel); 24249ac7962aSdrh } 24259ac7962aSdrh #endif 2426ceea3321Sdrh } 2427ceea3321Sdrh 2428ceea3321Sdrh /* 2429ceea3321Sdrh ** Remove from the column cache any entries that were added since the 2430d2490904Sdrh ** the previous sqlite3ExprCachePush operation. In other words, restore 2431d2490904Sdrh ** the cache to the state it was in prior the most recent Push. 2432ceea3321Sdrh */ 2433d2490904Sdrh void sqlite3ExprCachePop(Parse *pParse){ 2434ceea3321Sdrh int i; 2435ceea3321Sdrh struct yColCache *p; 2436d2490904Sdrh assert( pParse->iCacheLevel>=1 ); 2437d2490904Sdrh pParse->iCacheLevel--; 24389ac7962aSdrh #ifdef SQLITE_DEBUG 24399ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 24409ac7962aSdrh printf("POP to %d\n", pParse->iCacheLevel); 24419ac7962aSdrh } 24429ac7962aSdrh #endif 2443ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2444ceea3321Sdrh if( p->iReg && p->iLevel>pParse->iCacheLevel ){ 2445ceea3321Sdrh cacheEntryClear(pParse, p); 2446ceea3321Sdrh p->iReg = 0; 2447ceea3321Sdrh } 2448ceea3321Sdrh } 2449ceea3321Sdrh } 2450945498f3Sdrh 2451945498f3Sdrh /* 24525cd79239Sdrh ** When a cached column is reused, make sure that its register is 24535cd79239Sdrh ** no longer available as a temp register. ticket #3879: that same 24545cd79239Sdrh ** register might be in the cache in multiple places, so be sure to 24555cd79239Sdrh ** get them all. 24565cd79239Sdrh */ 24575cd79239Sdrh static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ 24585cd79239Sdrh int i; 24595cd79239Sdrh struct yColCache *p; 24605cd79239Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 24615cd79239Sdrh if( p->iReg==iReg ){ 24625cd79239Sdrh p->tempReg = 0; 24635cd79239Sdrh } 24645cd79239Sdrh } 24655cd79239Sdrh } 24665cd79239Sdrh 24671f9ca2c8Sdrh /* Generate code that will load into register regOut a value that is 24681f9ca2c8Sdrh ** appropriate for the iIdxCol-th column of index pIdx. 24691f9ca2c8Sdrh */ 24701f9ca2c8Sdrh void sqlite3ExprCodeLoadIndexColumn( 24711f9ca2c8Sdrh Parse *pParse, /* The parsing context */ 24721f9ca2c8Sdrh Index *pIdx, /* The index whose column is to be loaded */ 24731f9ca2c8Sdrh int iTabCur, /* Cursor pointing to a table row */ 24741f9ca2c8Sdrh int iIdxCol, /* The column of the index to be loaded */ 24751f9ca2c8Sdrh int regOut /* Store the index column value in this register */ 24761f9ca2c8Sdrh ){ 24771f9ca2c8Sdrh i16 iTabCol = pIdx->aiColumn[iIdxCol]; 24784b92f98cSdrh if( iTabCol==XN_EXPR ){ 24791f9ca2c8Sdrh assert( pIdx->aColExpr ); 24801f9ca2c8Sdrh assert( pIdx->aColExpr->nExpr>iIdxCol ); 24811f9ca2c8Sdrh pParse->iSelfTab = iTabCur; 24821c75c9d7Sdrh sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); 24834b92f98cSdrh }else{ 24844b92f98cSdrh sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, 24854b92f98cSdrh iTabCol, regOut); 24864b92f98cSdrh } 24871f9ca2c8Sdrh } 24881f9ca2c8Sdrh 24895cd79239Sdrh /* 24905c092e8aSdrh ** Generate code to extract the value of the iCol-th column of a table. 24915c092e8aSdrh */ 24925c092e8aSdrh void sqlite3ExprCodeGetColumnOfTable( 24935c092e8aSdrh Vdbe *v, /* The VDBE under construction */ 24945c092e8aSdrh Table *pTab, /* The table containing the value */ 2495313619f5Sdrh int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 24965c092e8aSdrh int iCol, /* Index of the column to extract */ 2497313619f5Sdrh int regOut /* Extract the value into this register */ 24985c092e8aSdrh ){ 24995c092e8aSdrh if( iCol<0 || iCol==pTab->iPKey ){ 25005c092e8aSdrh sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 25015c092e8aSdrh }else{ 25025c092e8aSdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 2503ee0ec8e1Sdrh int x = iCol; 2504ee0ec8e1Sdrh if( !HasRowid(pTab) ){ 2505ee0ec8e1Sdrh x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 2506ee0ec8e1Sdrh } 2507ee0ec8e1Sdrh sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 25085c092e8aSdrh } 25095c092e8aSdrh if( iCol>=0 ){ 25105c092e8aSdrh sqlite3ColumnDefault(v, pTab, iCol, regOut); 25115c092e8aSdrh } 25125c092e8aSdrh } 25135c092e8aSdrh 25145c092e8aSdrh /* 2515945498f3Sdrh ** Generate code that will extract the iColumn-th column from 2516ce78bc6eSdrh ** table pTab and store the column value in a register. 2517ce78bc6eSdrh ** 2518ce78bc6eSdrh ** An effort is made to store the column value in register iReg. This 2519ce78bc6eSdrh ** is not garanteeed for GetColumn() - the result can be stored in 2520ce78bc6eSdrh ** any register. But the result is guaranteed to land in register iReg 2521ce78bc6eSdrh ** for GetColumnToReg(). 2522e55cbd72Sdrh ** 2523e55cbd72Sdrh ** There must be an open cursor to pTab in iTable when this routine 2524e55cbd72Sdrh ** is called. If iColumn<0 then code is generated that extracts the rowid. 2525945498f3Sdrh */ 2526e55cbd72Sdrh int sqlite3ExprCodeGetColumn( 2527e55cbd72Sdrh Parse *pParse, /* Parsing and code generating context */ 25282133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 25292133d822Sdrh int iColumn, /* Index of the table column */ 25302133d822Sdrh int iTable, /* The cursor pointing to the table */ 2531a748fdccSdrh int iReg, /* Store results here */ 2532ce78bc6eSdrh u8 p5 /* P5 value for OP_Column + FLAGS */ 25332133d822Sdrh ){ 2534e55cbd72Sdrh Vdbe *v = pParse->pVdbe; 2535e55cbd72Sdrh int i; 2536da250ea5Sdrh struct yColCache *p; 2537e55cbd72Sdrh 2538ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2539b6da74ebSdrh if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ 2540ceea3321Sdrh p->lru = pParse->iCacheCnt++; 25415cd79239Sdrh sqlite3ExprCachePinRegister(pParse, p->iReg); 2542da250ea5Sdrh return p->iReg; 2543e55cbd72Sdrh } 2544e55cbd72Sdrh } 2545e55cbd72Sdrh assert( v!=0 ); 25465c092e8aSdrh sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); 2547a748fdccSdrh if( p5 ){ 2548a748fdccSdrh sqlite3VdbeChangeP5(v, p5); 2549a748fdccSdrh }else{ 2550ceea3321Sdrh sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); 2551a748fdccSdrh } 2552e55cbd72Sdrh return iReg; 2553e55cbd72Sdrh } 2554ce78bc6eSdrh void sqlite3ExprCodeGetColumnToReg( 2555ce78bc6eSdrh Parse *pParse, /* Parsing and code generating context */ 2556ce78bc6eSdrh Table *pTab, /* Description of the table we are reading from */ 2557ce78bc6eSdrh int iColumn, /* Index of the table column */ 2558ce78bc6eSdrh int iTable, /* The cursor pointing to the table */ 2559ce78bc6eSdrh int iReg /* Store results here */ 2560ce78bc6eSdrh ){ 2561ce78bc6eSdrh int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); 2562ce78bc6eSdrh if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); 2563ce78bc6eSdrh } 2564ce78bc6eSdrh 2565e55cbd72Sdrh 2566e55cbd72Sdrh /* 2567ceea3321Sdrh ** Clear all column cache entries. 2568e55cbd72Sdrh */ 2569ceea3321Sdrh void sqlite3ExprCacheClear(Parse *pParse){ 2570e55cbd72Sdrh int i; 2571ceea3321Sdrh struct yColCache *p; 2572ceea3321Sdrh 25739ac7962aSdrh #if SQLITE_DEBUG 25749ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 25759ac7962aSdrh printf("CLEAR\n"); 25769ac7962aSdrh } 25779ac7962aSdrh #endif 2578ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2579ceea3321Sdrh if( p->iReg ){ 2580ceea3321Sdrh cacheEntryClear(pParse, p); 2581ceea3321Sdrh p->iReg = 0; 2582e55cbd72Sdrh } 2583da250ea5Sdrh } 2584da250ea5Sdrh } 2585e55cbd72Sdrh 2586e55cbd72Sdrh /* 2587da250ea5Sdrh ** Record the fact that an affinity change has occurred on iCount 2588da250ea5Sdrh ** registers starting with iStart. 2589e55cbd72Sdrh */ 2590da250ea5Sdrh void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 2591f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iStart, iCount); 2592e55cbd72Sdrh } 2593e55cbd72Sdrh 2594e55cbd72Sdrh /* 2595b21e7c70Sdrh ** Generate code to move content from registers iFrom...iFrom+nReg-1 2596b21e7c70Sdrh ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. 2597e55cbd72Sdrh */ 2598b21e7c70Sdrh void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ 2599e8e4af76Sdrh assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); 2600079a3072Sdrh sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); 2601236241aeSdrh sqlite3ExprCacheRemove(pParse, iFrom, nReg); 2602945498f3Sdrh } 2603945498f3Sdrh 2604f49f3523Sdrh #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) 260592b01d53Sdrh /* 2606652fbf55Sdrh ** Return true if any register in the range iFrom..iTo (inclusive) 2607652fbf55Sdrh ** is used as part of the column cache. 2608f49f3523Sdrh ** 2609f49f3523Sdrh ** This routine is used within assert() and testcase() macros only 2610f49f3523Sdrh ** and does not appear in a normal build. 2611652fbf55Sdrh */ 2612652fbf55Sdrh static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 2613652fbf55Sdrh int i; 2614ceea3321Sdrh struct yColCache *p; 2615ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2616ceea3321Sdrh int r = p->iReg; 2617f49f3523Sdrh if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ 2618652fbf55Sdrh } 2619652fbf55Sdrh return 0; 2620652fbf55Sdrh } 2621f49f3523Sdrh #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ 2622652fbf55Sdrh 2623652fbf55Sdrh /* 2624a4c3c87eSdrh ** Convert an expression node to a TK_REGISTER 2625a4c3c87eSdrh */ 2626a4c3c87eSdrh static void exprToRegister(Expr *p, int iReg){ 2627a4c3c87eSdrh p->op2 = p->op; 2628a4c3c87eSdrh p->op = TK_REGISTER; 2629a4c3c87eSdrh p->iTable = iReg; 2630a4c3c87eSdrh ExprClearProperty(p, EP_Skip); 2631a4c3c87eSdrh } 2632a4c3c87eSdrh 2633a4c3c87eSdrh /* 2634cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 26352dcef11bSdrh ** expression. Attempt to store the results in register "target". 26362dcef11bSdrh ** Return the register where results are stored. 2637389a1adbSdrh ** 26388b213899Sdrh ** With this routine, there is no guarantee that results will 26392dcef11bSdrh ** be stored in target. The result might be stored in some other 26402dcef11bSdrh ** register if it is convenient to do so. The calling function 26412dcef11bSdrh ** must check the return code and move the results to the desired 26422dcef11bSdrh ** register. 2643cce7d176Sdrh */ 2644678ccce8Sdrh int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 26452dcef11bSdrh Vdbe *v = pParse->pVdbe; /* The VM under construction */ 26462dcef11bSdrh int op; /* The opcode being coded */ 26472dcef11bSdrh int inReg = target; /* Results stored in register inReg */ 26482dcef11bSdrh int regFree1 = 0; /* If non-zero free this temporary register */ 26492dcef11bSdrh int regFree2 = 0; /* If non-zero free this temporary register */ 2650678ccce8Sdrh int r1, r2, r3, r4; /* Various register numbers */ 265120411ea7Sdrh sqlite3 *db = pParse->db; /* The database connection */ 265210d1edf0Sdrh Expr tempX; /* Temporary expression node */ 2653ffe07b2dSdrh 26549cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 265520411ea7Sdrh if( v==0 ){ 265620411ea7Sdrh assert( pParse->db->mallocFailed ); 265720411ea7Sdrh return 0; 265820411ea7Sdrh } 2659389a1adbSdrh 2660389a1adbSdrh if( pExpr==0 ){ 2661389a1adbSdrh op = TK_NULL; 2662389a1adbSdrh }else{ 2663f2bc013cSdrh op = pExpr->op; 2664389a1adbSdrh } 2665f2bc013cSdrh switch( op ){ 266613449892Sdrh case TK_AGG_COLUMN: { 266713449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 266813449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 266913449892Sdrh if( !pAggInfo->directMode ){ 26709de221dfSdrh assert( pCol->iMem>0 ); 26719de221dfSdrh inReg = pCol->iMem; 267213449892Sdrh break; 267313449892Sdrh }else if( pAggInfo->useSortingIdx ){ 26745134d135Sdan sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 2675389a1adbSdrh pCol->iSorterColumn, target); 267613449892Sdrh break; 267713449892Sdrh } 267813449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 267913449892Sdrh } 2680967e8b73Sdrh case TK_COLUMN: { 2681b2b9d3d7Sdrh int iTab = pExpr->iTable; 2682b2b9d3d7Sdrh if( iTab<0 ){ 2683b2b9d3d7Sdrh if( pParse->ckBase>0 ){ 2684b2b9d3d7Sdrh /* Generating CHECK constraints or inserting into partial index */ 2685aa9b8963Sdrh inReg = pExpr->iColumn + pParse->ckBase; 2686b2b9d3d7Sdrh break; 2687c4a3c779Sdrh }else{ 26881f9ca2c8Sdrh /* Coding an expression that is part of an index where column names 26891f9ca2c8Sdrh ** in the index refer to the table to which the index belongs */ 26901f9ca2c8Sdrh iTab = pParse->iSelfTab; 26912282792aSdrh } 2692b2b9d3d7Sdrh } 2693b2b9d3d7Sdrh inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 2694b2b9d3d7Sdrh pExpr->iColumn, iTab, target, 2695b2b9d3d7Sdrh pExpr->op2); 2696cce7d176Sdrh break; 2697cce7d176Sdrh } 2698cce7d176Sdrh case TK_INTEGER: { 269913573c71Sdrh codeInteger(pParse, pExpr, 0, target); 2700fec19aadSdrh break; 270151e9a445Sdrh } 270213573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2703598f1340Sdrh case TK_FLOAT: { 270433e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 270533e619fcSdrh codeReal(v, pExpr->u.zToken, 0, target); 2706598f1340Sdrh break; 2707598f1340Sdrh } 270813573c71Sdrh #endif 2709fec19aadSdrh case TK_STRING: { 271033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 2711076e85f5Sdrh sqlite3VdbeLoadString(v, target, pExpr->u.zToken); 2712cce7d176Sdrh break; 2713cce7d176Sdrh } 2714f0863fe5Sdrh case TK_NULL: { 27159de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 2716f0863fe5Sdrh break; 2717f0863fe5Sdrh } 27185338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 2719c572ef7fSdanielk1977 case TK_BLOB: { 27206c8c6cecSdrh int n; 27216c8c6cecSdrh const char *z; 2722ca48c90fSdrh char *zBlob; 272333e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 272433e619fcSdrh assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 272533e619fcSdrh assert( pExpr->u.zToken[1]=='\'' ); 272633e619fcSdrh z = &pExpr->u.zToken[2]; 2727b7916a78Sdrh n = sqlite3Strlen30(z) - 1; 2728b7916a78Sdrh assert( z[n]=='\'' ); 2729ca48c90fSdrh zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 2730ca48c90fSdrh sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 2731c572ef7fSdanielk1977 break; 2732c572ef7fSdanielk1977 } 27335338a5f7Sdanielk1977 #endif 273450457896Sdrh case TK_VARIABLE: { 273533e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 273633e619fcSdrh assert( pExpr->u.zToken!=0 ); 273733e619fcSdrh assert( pExpr->u.zToken[0]!=0 ); 2738eaf52d88Sdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); 273933e619fcSdrh if( pExpr->u.zToken[1]!=0 ){ 274004e9eeadSdrh assert( pExpr->u.zToken[0]=='?' 274104e9eeadSdrh || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); 274204e9eeadSdrh sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); 2743895d7472Sdrh } 274450457896Sdrh break; 274550457896Sdrh } 27464e0cff60Sdrh case TK_REGISTER: { 27479de221dfSdrh inReg = pExpr->iTable; 27484e0cff60Sdrh break; 27494e0cff60Sdrh } 2750487e262fSdrh #ifndef SQLITE_OMIT_CAST 2751487e262fSdrh case TK_CAST: { 2752487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 27532dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 27541735fa88Sdrh if( inReg!=target ){ 27551735fa88Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); 27561735fa88Sdrh inReg = target; 27571735fa88Sdrh } 27584169e430Sdrh sqlite3VdbeAddOp2(v, OP_Cast, target, 27594169e430Sdrh sqlite3AffinityType(pExpr->u.zToken, 0)); 2760c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 2761b3843a82Sdrh sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 2762487e262fSdrh break; 2763487e262fSdrh } 2764487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 2765c9b84a1fSdrh case TK_LT: 2766c9b84a1fSdrh case TK_LE: 2767c9b84a1fSdrh case TK_GT: 2768c9b84a1fSdrh case TK_GE: 2769c9b84a1fSdrh case TK_NE: 2770c9b84a1fSdrh case TK_EQ: { 2771b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2772b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 277335573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 277435573356Sdrh r1, r2, inReg, SQLITE_STOREP2); 27757d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 27767d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 27777d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 27787d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 27797d176105Sdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 27807d176105Sdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 2781c5499befSdrh testcase( regFree1==0 ); 2782c5499befSdrh testcase( regFree2==0 ); 2783a37cdde0Sdanielk1977 break; 2784c9b84a1fSdrh } 27856a2fe093Sdrh case TK_IS: 27866a2fe093Sdrh case TK_ISNOT: { 27876a2fe093Sdrh testcase( op==TK_IS ); 27886a2fe093Sdrh testcase( op==TK_ISNOT ); 2789b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2790b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 27916a2fe093Sdrh op = (op==TK_IS) ? TK_EQ : TK_NE; 27926a2fe093Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 27936a2fe093Sdrh r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ); 27947d176105Sdrh VdbeCoverageIf(v, op==TK_EQ); 27957d176105Sdrh VdbeCoverageIf(v, op==TK_NE); 27966a2fe093Sdrh testcase( regFree1==0 ); 27976a2fe093Sdrh testcase( regFree2==0 ); 27986a2fe093Sdrh break; 27996a2fe093Sdrh } 2800cce7d176Sdrh case TK_AND: 2801cce7d176Sdrh case TK_OR: 2802cce7d176Sdrh case TK_PLUS: 2803cce7d176Sdrh case TK_STAR: 2804cce7d176Sdrh case TK_MINUS: 2805bf4133cbSdrh case TK_REM: 2806bf4133cbSdrh case TK_BITAND: 2807bf4133cbSdrh case TK_BITOR: 280817c40294Sdrh case TK_SLASH: 2809bf4133cbSdrh case TK_LSHIFT: 2810855eb1cfSdrh case TK_RSHIFT: 28110040077dSdrh case TK_CONCAT: { 28127d176105Sdrh assert( TK_AND==OP_And ); testcase( op==TK_AND ); 28137d176105Sdrh assert( TK_OR==OP_Or ); testcase( op==TK_OR ); 28147d176105Sdrh assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); 28157d176105Sdrh assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); 28167d176105Sdrh assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); 28177d176105Sdrh assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); 28187d176105Sdrh assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); 28197d176105Sdrh assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); 28207d176105Sdrh assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); 28217d176105Sdrh assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); 28227d176105Sdrh assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); 28232dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 28242dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 28255b6afba9Sdrh sqlite3VdbeAddOp3(v, op, r2, r1, target); 2826c5499befSdrh testcase( regFree1==0 ); 2827c5499befSdrh testcase( regFree2==0 ); 28280040077dSdrh break; 28290040077dSdrh } 2830cce7d176Sdrh case TK_UMINUS: { 2831fec19aadSdrh Expr *pLeft = pExpr->pLeft; 2832fec19aadSdrh assert( pLeft ); 283313573c71Sdrh if( pLeft->op==TK_INTEGER ){ 283413573c71Sdrh codeInteger(pParse, pLeft, 1, target); 283513573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 283613573c71Sdrh }else if( pLeft->op==TK_FLOAT ){ 283733e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 283833e619fcSdrh codeReal(v, pLeft->u.zToken, 1, target); 283913573c71Sdrh #endif 28403c84ddffSdrh }else{ 284110d1edf0Sdrh tempX.op = TK_INTEGER; 284210d1edf0Sdrh tempX.flags = EP_IntValue|EP_TokenOnly; 284310d1edf0Sdrh tempX.u.iValue = 0; 284410d1edf0Sdrh r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); 2845e55cbd72Sdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 28462dcef11bSdrh sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 2847c5499befSdrh testcase( regFree2==0 ); 28483c84ddffSdrh } 28499de221dfSdrh inReg = target; 28506e142f54Sdrh break; 28516e142f54Sdrh } 2852bf4133cbSdrh case TK_BITNOT: 28536e142f54Sdrh case TK_NOT: { 28547d176105Sdrh assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); 28557d176105Sdrh assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); 2856e99fa2afSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2857e99fa2afSdrh testcase( regFree1==0 ); 2858e99fa2afSdrh inReg = target; 2859e99fa2afSdrh sqlite3VdbeAddOp2(v, op, r1, inReg); 2860cce7d176Sdrh break; 2861cce7d176Sdrh } 2862cce7d176Sdrh case TK_ISNULL: 2863cce7d176Sdrh case TK_NOTNULL: { 28646a288a33Sdrh int addr; 28657d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 28667d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 28679de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 28682dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 2869c5499befSdrh testcase( regFree1==0 ); 28702dcef11bSdrh addr = sqlite3VdbeAddOp1(v, op, r1); 28717d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 28727d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 2873a976979bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, target); 28746a288a33Sdrh sqlite3VdbeJumpHere(v, addr); 2875a37cdde0Sdanielk1977 break; 2876f2bc013cSdrh } 28772282792aSdrh case TK_AGG_FUNCTION: { 287813449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 28797e56e711Sdrh if( pInfo==0 ){ 288033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 288133e619fcSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); 28827e56e711Sdrh }else{ 28839de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 28847e56e711Sdrh } 28852282792aSdrh break; 28862282792aSdrh } 2887cce7d176Sdrh case TK_FUNCTION: { 288812ffee8cSdrh ExprList *pFarg; /* List of function arguments */ 288912ffee8cSdrh int nFarg; /* Number of function arguments */ 289012ffee8cSdrh FuncDef *pDef; /* The function definition object */ 289112ffee8cSdrh const char *zId; /* The function name */ 2892693e6719Sdrh u32 constMask = 0; /* Mask of function arguments that are constant */ 289312ffee8cSdrh int i; /* Loop counter */ 289412ffee8cSdrh u8 enc = ENC(db); /* The text encoding used by this database */ 289512ffee8cSdrh CollSeq *pColl = 0; /* A collating sequence */ 289617435752Sdrh 28976ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2898c5cd1249Sdrh if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 289912ffee8cSdrh pFarg = 0; 290012ffee8cSdrh }else{ 290112ffee8cSdrh pFarg = pExpr->x.pList; 290212ffee8cSdrh } 290312ffee8cSdrh nFarg = pFarg ? pFarg->nExpr : 0; 290433e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 290533e619fcSdrh zId = pExpr->u.zToken; 290680738d9cSdrh pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); 29072d80151fSdrh if( pDef==0 || pDef->xFinalize!=0 ){ 290880738d9cSdrh sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); 2909feb306f5Sdrh break; 2910feb306f5Sdrh } 2911ae6bb957Sdrh 2912ae6bb957Sdrh /* Attempt a direct implementation of the built-in COALESCE() and 291360ec914cSpeter.d.reid ** IFNULL() functions. This avoids unnecessary evaluation of 2914ae6bb957Sdrh ** arguments past the first non-NULL argument. 2915ae6bb957Sdrh */ 2916d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ 2917ae6bb957Sdrh int endCoalesce = sqlite3VdbeMakeLabel(v); 2918ae6bb957Sdrh assert( nFarg>=2 ); 2919ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 2920ae6bb957Sdrh for(i=1; i<nFarg; i++){ 2921ae6bb957Sdrh sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); 2922688852abSdrh VdbeCoverage(v); 2923f49f3523Sdrh sqlite3ExprCacheRemove(pParse, target, 1); 2924ae6bb957Sdrh sqlite3ExprCachePush(pParse); 2925ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); 2926d2490904Sdrh sqlite3ExprCachePop(pParse); 2927ae6bb957Sdrh } 2928ae6bb957Sdrh sqlite3VdbeResolveLabel(v, endCoalesce); 2929ae6bb957Sdrh break; 2930ae6bb957Sdrh } 2931ae6bb957Sdrh 2932cca9f3d2Sdrh /* The UNLIKELY() function is a no-op. The result is the value 2933cca9f3d2Sdrh ** of the first argument. 2934cca9f3d2Sdrh */ 2935cca9f3d2Sdrh if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 2936cca9f3d2Sdrh assert( nFarg>=1 ); 29375f02ab09Sdrh inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); 2938cca9f3d2Sdrh break; 2939cca9f3d2Sdrh } 2940ae6bb957Sdrh 2941d1a01edaSdrh for(i=0; i<nFarg; i++){ 2942d1a01edaSdrh if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ 2943693e6719Sdrh testcase( i==31 ); 2944693e6719Sdrh constMask |= MASKBIT32(i); 2945d1a01edaSdrh } 2946d1a01edaSdrh if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 2947d1a01edaSdrh pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 2948d1a01edaSdrh } 2949d1a01edaSdrh } 295012ffee8cSdrh if( pFarg ){ 2951d1a01edaSdrh if( constMask ){ 2952d1a01edaSdrh r1 = pParse->nMem+1; 2953d1a01edaSdrh pParse->nMem += nFarg; 2954d1a01edaSdrh }else{ 295512ffee8cSdrh r1 = sqlite3GetTempRange(pParse, nFarg); 2956d1a01edaSdrh } 2957a748fdccSdrh 2958a748fdccSdrh /* For length() and typeof() functions with a column argument, 2959a748fdccSdrh ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG 2960a748fdccSdrh ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data 2961a748fdccSdrh ** loading. 2962a748fdccSdrh */ 2963d36e1041Sdrh if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ 29644e245a4cSdrh u8 exprOp; 2965a748fdccSdrh assert( nFarg==1 ); 2966a748fdccSdrh assert( pFarg->a[0].pExpr!=0 ); 29674e245a4cSdrh exprOp = pFarg->a[0].pExpr->op; 29684e245a4cSdrh if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 2969a748fdccSdrh assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 2970a748fdccSdrh assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 2971b1fba286Sdrh testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); 2972b1fba286Sdrh pFarg->a[0].pExpr->op2 = 2973b1fba286Sdrh pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); 2974a748fdccSdrh } 2975a748fdccSdrh } 2976a748fdccSdrh 2977d7d385ddSdrh sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ 29785579d59fSdrh sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, 2979d1a01edaSdrh SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); 2980d2490904Sdrh sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ 2981892d3179Sdrh }else{ 298212ffee8cSdrh r1 = 0; 2983892d3179Sdrh } 2984b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 2985a43fa227Sdrh /* Possibly overload the function if the first argument is 2986a43fa227Sdrh ** a virtual table column. 2987a43fa227Sdrh ** 2988a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 2989a43fa227Sdrh ** second argument, not the first, as the argument to test to 2990a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 2991a43fa227Sdrh ** the left operand of infix functions (the operand we want to 2992a43fa227Sdrh ** control overloading) ends up as the second argument to the 2993a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 2994a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 2995a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 2996a43fa227Sdrh */ 299712ffee8cSdrh if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ 299812ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 299912ffee8cSdrh }else if( nFarg>0 ){ 300012ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 3001b7f6f68fSdrh } 3002b7f6f68fSdrh #endif 3003d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 30048b213899Sdrh if( !pColl ) pColl = db->pDfltColl; 300566a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 3006682f68b0Sdanielk1977 } 30079c7c913cSdrh sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, 300866a5167bSdrh (char*)pDef, P4_FUNCDEF); 300912ffee8cSdrh sqlite3VdbeChangeP5(v, (u8)nFarg); 3010d1a01edaSdrh if( nFarg && constMask==0 ){ 301112ffee8cSdrh sqlite3ReleaseTempRange(pParse, r1, nFarg); 30122dcef11bSdrh } 30136ec2733bSdrh break; 30146ec2733bSdrh } 3015fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 3016fe2093d7Sdrh case TK_EXISTS: 301719a775c2Sdrh case TK_SELECT: { 3018c5499befSdrh testcase( op==TK_EXISTS ); 3019c5499befSdrh testcase( op==TK_SELECT ); 30201450bc6eSdrh inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 302119a775c2Sdrh break; 302219a775c2Sdrh } 3023fef5208cSdrh case TK_IN: { 3024e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3025e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 3026e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3027e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 302866ba23ceSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 3029e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3030e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); 3031e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 3032fef5208cSdrh break; 3033fef5208cSdrh } 3034e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 3035e3365e6cSdrh 3036e3365e6cSdrh 30372dcef11bSdrh /* 30382dcef11bSdrh ** x BETWEEN y AND z 30392dcef11bSdrh ** 30402dcef11bSdrh ** This is equivalent to 30412dcef11bSdrh ** 30422dcef11bSdrh ** x>=y AND x<=z 30432dcef11bSdrh ** 30442dcef11bSdrh ** X is stored in pExpr->pLeft. 30452dcef11bSdrh ** Y is stored in pExpr->pList->a[0].pExpr. 30462dcef11bSdrh ** Z is stored in pExpr->pList->a[1].pExpr. 30472dcef11bSdrh */ 3048fef5208cSdrh case TK_BETWEEN: { 3049be5c89acSdrh Expr *pLeft = pExpr->pLeft; 30506ab3a2ecSdanielk1977 struct ExprList_item *pLItem = pExpr->x.pList->a; 3051be5c89acSdrh Expr *pRight = pLItem->pExpr; 305235573356Sdrh 3053b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 3054b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 3055c5499befSdrh testcase( regFree1==0 ); 3056c5499befSdrh testcase( regFree2==0 ); 30572dcef11bSdrh r3 = sqlite3GetTempReg(pParse); 3058678ccce8Sdrh r4 = sqlite3GetTempReg(pParse); 305935573356Sdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 30607d176105Sdrh r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v); 3061be5c89acSdrh pLItem++; 3062be5c89acSdrh pRight = pLItem->pExpr; 30632dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 30642dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 3065c5499befSdrh testcase( regFree2==0 ); 3066678ccce8Sdrh codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); 3067688852abSdrh VdbeCoverage(v); 3068678ccce8Sdrh sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); 30692dcef11bSdrh sqlite3ReleaseTempReg(pParse, r3); 3070678ccce8Sdrh sqlite3ReleaseTempReg(pParse, r4); 3071fef5208cSdrh break; 3072fef5208cSdrh } 307394fa9c41Sdrh case TK_SPAN: 3074ae80ddeaSdrh case TK_COLLATE: 30754f07e5fbSdrh case TK_UPLUS: { 30762dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 3077a2e00042Sdrh break; 3078a2e00042Sdrh } 30792dcef11bSdrh 3080165921a7Sdan case TK_TRIGGER: { 308165a7cd16Sdan /* If the opcode is TK_TRIGGER, then the expression is a reference 308265a7cd16Sdan ** to a column in the new.* or old.* pseudo-tables available to 308365a7cd16Sdan ** trigger programs. In this case Expr.iTable is set to 1 for the 308465a7cd16Sdan ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 308565a7cd16Sdan ** is set to the column of the pseudo-table to read, or to -1 to 308665a7cd16Sdan ** read the rowid field. 308765a7cd16Sdan ** 308865a7cd16Sdan ** The expression is implemented using an OP_Param opcode. The p1 308965a7cd16Sdan ** parameter is set to 0 for an old.rowid reference, or to (i+1) 309065a7cd16Sdan ** to reference another column of the old.* pseudo-table, where 309165a7cd16Sdan ** i is the index of the column. For a new.rowid reference, p1 is 309265a7cd16Sdan ** set to (n+1), where n is the number of columns in each pseudo-table. 309365a7cd16Sdan ** For a reference to any other column in the new.* pseudo-table, p1 309465a7cd16Sdan ** is set to (n+2+i), where n and i are as defined previously. For 309565a7cd16Sdan ** example, if the table on which triggers are being fired is 309665a7cd16Sdan ** declared as: 309765a7cd16Sdan ** 309865a7cd16Sdan ** CREATE TABLE t1(a, b); 309965a7cd16Sdan ** 310065a7cd16Sdan ** Then p1 is interpreted as follows: 310165a7cd16Sdan ** 310265a7cd16Sdan ** p1==0 -> old.rowid p1==3 -> new.rowid 310365a7cd16Sdan ** p1==1 -> old.a p1==4 -> new.a 310465a7cd16Sdan ** p1==2 -> old.b p1==5 -> new.b 310565a7cd16Sdan */ 31062832ad42Sdan Table *pTab = pExpr->pTab; 310765a7cd16Sdan int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; 310865a7cd16Sdan 310965a7cd16Sdan assert( pExpr->iTable==0 || pExpr->iTable==1 ); 311065a7cd16Sdan assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); 311165a7cd16Sdan assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); 311265a7cd16Sdan assert( p1>=0 && p1<(pTab->nCol*2+2) ); 311365a7cd16Sdan 311465a7cd16Sdan sqlite3VdbeAddOp2(v, OP_Param, p1, target); 311576d462eeSdan VdbeComment((v, "%s.%s -> $%d", 3116165921a7Sdan (pExpr->iTable ? "new" : "old"), 311776d462eeSdan (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), 311876d462eeSdan target 3119165921a7Sdan )); 312065a7cd16Sdan 312144dbca83Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 312265a7cd16Sdan /* If the column has REAL affinity, it may currently be stored as an 3123113762a2Sdrh ** integer. Use OP_RealAffinity to make sure it is really real. 3124113762a2Sdrh ** 3125113762a2Sdrh ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to 3126113762a2Sdrh ** floating point when extracting it from the record. */ 31272832ad42Sdan if( pExpr->iColumn>=0 31282832ad42Sdan && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL 31292832ad42Sdan ){ 31302832ad42Sdan sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 31312832ad42Sdan } 313244dbca83Sdrh #endif 3133165921a7Sdan break; 3134165921a7Sdan } 3135165921a7Sdan 3136165921a7Sdan 31372dcef11bSdrh /* 31382dcef11bSdrh ** Form A: 31392dcef11bSdrh ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 31402dcef11bSdrh ** 31412dcef11bSdrh ** Form B: 31422dcef11bSdrh ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 31432dcef11bSdrh ** 31442dcef11bSdrh ** Form A is can be transformed into the equivalent form B as follows: 31452dcef11bSdrh ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 31462dcef11bSdrh ** WHEN x=eN THEN rN ELSE y END 31472dcef11bSdrh ** 31482dcef11bSdrh ** X (if it exists) is in pExpr->pLeft. 3149c5cd1249Sdrh ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is 3150c5cd1249Sdrh ** odd. The Y is also optional. If the number of elements in x.pList 3151c5cd1249Sdrh ** is even, then Y is omitted and the "otherwise" result is NULL. 31522dcef11bSdrh ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 31532dcef11bSdrh ** 31542dcef11bSdrh ** The result of the expression is the Ri for the first matching Ei, 31552dcef11bSdrh ** or if there is no matching Ei, the ELSE term Y, or if there is 31562dcef11bSdrh ** no ELSE term, NULL. 31572dcef11bSdrh */ 315833cd4909Sdrh default: assert( op==TK_CASE ); { 31592dcef11bSdrh int endLabel; /* GOTO label for end of CASE stmt */ 31602dcef11bSdrh int nextCase; /* GOTO label for next WHEN clause */ 31612dcef11bSdrh int nExpr; /* 2x number of WHEN terms */ 31622dcef11bSdrh int i; /* Loop counter */ 31632dcef11bSdrh ExprList *pEList; /* List of WHEN terms */ 31642dcef11bSdrh struct ExprList_item *aListelem; /* Array of WHEN terms */ 31652dcef11bSdrh Expr opCompare; /* The X==Ei expression */ 31662dcef11bSdrh Expr *pX; /* The X expression */ 31671bd10f8aSdrh Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ 3168ceea3321Sdrh VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) 316917a7f8ddSdrh 31706ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); 31716ab3a2ecSdanielk1977 assert(pExpr->x.pList->nExpr > 0); 31726ab3a2ecSdanielk1977 pEList = pExpr->x.pList; 3173be5c89acSdrh aListelem = pEList->a; 3174be5c89acSdrh nExpr = pEList->nExpr; 31752dcef11bSdrh endLabel = sqlite3VdbeMakeLabel(v); 31762dcef11bSdrh if( (pX = pExpr->pLeft)!=0 ){ 317710d1edf0Sdrh tempX = *pX; 317833cd4909Sdrh testcase( pX->op==TK_COLUMN ); 317910d1edf0Sdrh exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); 3180c5499befSdrh testcase( regFree1==0 ); 31812dcef11bSdrh opCompare.op = TK_EQ; 318210d1edf0Sdrh opCompare.pLeft = &tempX; 31832dcef11bSdrh pTest = &opCompare; 31848b1db07fSdrh /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: 31858b1db07fSdrh ** The value in regFree1 might get SCopy-ed into the file result. 31868b1db07fSdrh ** So make sure that the regFree1 register is not reused for other 31878b1db07fSdrh ** purposes and possibly overwritten. */ 31888b1db07fSdrh regFree1 = 0; 3189cce7d176Sdrh } 3190c5cd1249Sdrh for(i=0; i<nExpr-1; i=i+2){ 3191ceea3321Sdrh sqlite3ExprCachePush(pParse); 31922dcef11bSdrh if( pX ){ 31931bd10f8aSdrh assert( pTest!=0 ); 31942dcef11bSdrh opCompare.pRight = aListelem[i].pExpr; 3195f5905aa7Sdrh }else{ 31962dcef11bSdrh pTest = aListelem[i].pExpr; 319717a7f8ddSdrh } 31982dcef11bSdrh nextCase = sqlite3VdbeMakeLabel(v); 319933cd4909Sdrh testcase( pTest->op==TK_COLUMN ); 32002dcef11bSdrh sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 3201c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 32029de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 3203076e85f5Sdrh sqlite3VdbeGoto(v, endLabel); 3204d2490904Sdrh sqlite3ExprCachePop(pParse); 32052dcef11bSdrh sqlite3VdbeResolveLabel(v, nextCase); 3206f570f011Sdrh } 3207c5cd1249Sdrh if( (nExpr&1)!=0 ){ 3208ceea3321Sdrh sqlite3ExprCachePush(pParse); 3209c5cd1249Sdrh sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 3210d2490904Sdrh sqlite3ExprCachePop(pParse); 321117a7f8ddSdrh }else{ 32129de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 321317a7f8ddSdrh } 3214c1f4a19bSdanielk1977 assert( db->mallocFailed || pParse->nErr>0 3215c1f4a19bSdanielk1977 || pParse->iCacheLevel==iCacheLevel ); 32162dcef11bSdrh sqlite3VdbeResolveLabel(v, endLabel); 32176f34903eSdanielk1977 break; 32186f34903eSdanielk1977 } 32195338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 32206f34903eSdanielk1977 case TK_RAISE: { 3221165921a7Sdan assert( pExpr->affinity==OE_Rollback 3222165921a7Sdan || pExpr->affinity==OE_Abort 3223165921a7Sdan || pExpr->affinity==OE_Fail 3224165921a7Sdan || pExpr->affinity==OE_Ignore 3225165921a7Sdan ); 3226e0af83acSdan if( !pParse->pTriggerTab ){ 3227e0af83acSdan sqlite3ErrorMsg(pParse, 3228e0af83acSdan "RAISE() may only be used within a trigger-program"); 3229e0af83acSdan return 0; 3230e0af83acSdan } 3231e0af83acSdan if( pExpr->affinity==OE_Abort ){ 3232e0af83acSdan sqlite3MayAbort(pParse); 3233e0af83acSdan } 323433e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3235e0af83acSdan if( pExpr->affinity==OE_Ignore ){ 3236e0af83acSdan sqlite3VdbeAddOp4( 3237e0af83acSdan v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); 3238688852abSdrh VdbeCoverage(v); 3239e0af83acSdan }else{ 3240433dccfbSdrh sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, 3241f9c8ce3cSdrh pExpr->affinity, pExpr->u.zToken, 0, 0); 3242e0af83acSdan } 3243e0af83acSdan 3244ffe07b2dSdrh break; 324517a7f8ddSdrh } 32465338a5f7Sdanielk1977 #endif 3247ffe07b2dSdrh } 32482dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 32492dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 32502dcef11bSdrh return inReg; 32515b6afba9Sdrh } 32522dcef11bSdrh 32532dcef11bSdrh /* 3254d1a01edaSdrh ** Factor out the code of the given expression to initialization time. 3255d1a01edaSdrh */ 3256d673cddaSdrh void sqlite3ExprCodeAtInit( 3257d673cddaSdrh Parse *pParse, /* Parsing context */ 3258d673cddaSdrh Expr *pExpr, /* The expression to code when the VDBE initializes */ 3259d673cddaSdrh int regDest, /* Store the value in this register */ 3260d673cddaSdrh u8 reusable /* True if this expression is reusable */ 3261d673cddaSdrh ){ 3262d1a01edaSdrh ExprList *p; 3263d9f158e7Sdrh assert( ConstFactorOk(pParse) ); 3264d1a01edaSdrh p = pParse->pConstExpr; 3265d1a01edaSdrh pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); 3266d1a01edaSdrh p = sqlite3ExprListAppend(pParse, p, pExpr); 3267d673cddaSdrh if( p ){ 3268d673cddaSdrh struct ExprList_item *pItem = &p->a[p->nExpr-1]; 3269d673cddaSdrh pItem->u.iConstExprReg = regDest; 3270d673cddaSdrh pItem->reusable = reusable; 3271d673cddaSdrh } 3272d1a01edaSdrh pParse->pConstExpr = p; 3273d1a01edaSdrh } 3274d1a01edaSdrh 3275d1a01edaSdrh /* 32762dcef11bSdrh ** Generate code to evaluate an expression and store the results 32772dcef11bSdrh ** into a register. Return the register number where the results 32782dcef11bSdrh ** are stored. 32792dcef11bSdrh ** 32802dcef11bSdrh ** If the register is a temporary register that can be deallocated, 3281678ccce8Sdrh ** then write its number into *pReg. If the result register is not 32822dcef11bSdrh ** a temporary, then set *pReg to zero. 3283f30a969bSdrh ** 3284f30a969bSdrh ** If pExpr is a constant, then this routine might generate this 3285f30a969bSdrh ** code to fill the register in the initialization section of the 3286f30a969bSdrh ** VDBE program, in order to factor it out of the evaluation loop. 32872dcef11bSdrh */ 32882dcef11bSdrh int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 3289f30a969bSdrh int r2; 3290f30a969bSdrh pExpr = sqlite3ExprSkipCollate(pExpr); 3291d9f158e7Sdrh if( ConstFactorOk(pParse) 3292f30a969bSdrh && pExpr->op!=TK_REGISTER 3293f30a969bSdrh && sqlite3ExprIsConstantNotJoin(pExpr) 3294f30a969bSdrh ){ 3295f30a969bSdrh ExprList *p = pParse->pConstExpr; 3296f30a969bSdrh int i; 3297f30a969bSdrh *pReg = 0; 3298f30a969bSdrh if( p ){ 3299d673cddaSdrh struct ExprList_item *pItem; 3300d673cddaSdrh for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ 3301d673cddaSdrh if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ 3302d673cddaSdrh return pItem->u.iConstExprReg; 3303f30a969bSdrh } 3304f30a969bSdrh } 3305f30a969bSdrh } 3306f30a969bSdrh r2 = ++pParse->nMem; 3307d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); 3308f30a969bSdrh }else{ 33092dcef11bSdrh int r1 = sqlite3GetTempReg(pParse); 3310f30a969bSdrh r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 33112dcef11bSdrh if( r2==r1 ){ 33122dcef11bSdrh *pReg = r1; 33132dcef11bSdrh }else{ 33142dcef11bSdrh sqlite3ReleaseTempReg(pParse, r1); 33152dcef11bSdrh *pReg = 0; 33162dcef11bSdrh } 3317f30a969bSdrh } 33182dcef11bSdrh return r2; 33192dcef11bSdrh } 33202dcef11bSdrh 33212dcef11bSdrh /* 33222dcef11bSdrh ** Generate code that will evaluate expression pExpr and store the 33232dcef11bSdrh ** results in register target. The results are guaranteed to appear 33242dcef11bSdrh ** in register target. 33252dcef11bSdrh */ 332605a86c5cSdrh void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 33279cbf3425Sdrh int inReg; 33289cbf3425Sdrh 33299cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 3330ebc16717Sdrh if( pExpr && pExpr->op==TK_REGISTER ){ 3331ebc16717Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); 3332ebc16717Sdrh }else{ 33339cbf3425Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 33341c75c9d7Sdrh assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); 33350e359b30Sdrh if( inReg!=target && pParse->pVdbe ){ 33369cbf3425Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 333717a7f8ddSdrh } 3338ebc16717Sdrh } 3339cce7d176Sdrh } 3340cce7d176Sdrh 3341cce7d176Sdrh /* 33421c75c9d7Sdrh ** Make a transient copy of expression pExpr and then code it using 33431c75c9d7Sdrh ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() 33441c75c9d7Sdrh ** except that the input expression is guaranteed to be unchanged. 33451c75c9d7Sdrh */ 33461c75c9d7Sdrh void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ 33471c75c9d7Sdrh sqlite3 *db = pParse->db; 33481c75c9d7Sdrh pExpr = sqlite3ExprDup(db, pExpr, 0); 33491c75c9d7Sdrh if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); 33501c75c9d7Sdrh sqlite3ExprDelete(db, pExpr); 33511c75c9d7Sdrh } 33521c75c9d7Sdrh 33531c75c9d7Sdrh /* 335405a86c5cSdrh ** Generate code that will evaluate expression pExpr and store the 335505a86c5cSdrh ** results in register target. The results are guaranteed to appear 335605a86c5cSdrh ** in register target. If the expression is constant, then this routine 335705a86c5cSdrh ** might choose to code the expression at initialization time. 335805a86c5cSdrh */ 335905a86c5cSdrh void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 336005a86c5cSdrh if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ 336105a86c5cSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); 336205a86c5cSdrh }else{ 336305a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 336405a86c5cSdrh } 3365cce7d176Sdrh } 3366cce7d176Sdrh 3367cce7d176Sdrh /* 336860ec914cSpeter.d.reid ** Generate code that evaluates the given expression and puts the result 3369de4fcfddSdrh ** in register target. 337025303780Sdrh ** 33712dcef11bSdrh ** Also make a copy of the expression results into another "cache" register 33722dcef11bSdrh ** and modify the expression so that the next time it is evaluated, 33732dcef11bSdrh ** the result is a copy of the cache register. 33742dcef11bSdrh ** 33752dcef11bSdrh ** This routine is used for expressions that are used multiple 33762dcef11bSdrh ** times. They are evaluated once and the results of the expression 33772dcef11bSdrh ** are reused. 337825303780Sdrh */ 337905a86c5cSdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 338025303780Sdrh Vdbe *v = pParse->pVdbe; 338125303780Sdrh int iMem; 338205a86c5cSdrh 338305a86c5cSdrh assert( target>0 ); 338405a86c5cSdrh assert( pExpr->op!=TK_REGISTER ); 338505a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 33862dcef11bSdrh iMem = ++pParse->nMem; 338705a86c5cSdrh sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); 3388a4c3c87eSdrh exprToRegister(pExpr, iMem); 338925303780Sdrh } 33907e02e5e6Sdrh 3391678ccce8Sdrh /* 3392268380caSdrh ** Generate code that pushes the value of every element of the given 33939cbf3425Sdrh ** expression list into a sequence of registers beginning at target. 3394268380caSdrh ** 3395892d3179Sdrh ** Return the number of elements evaluated. 3396d1a01edaSdrh ** 3397d1a01edaSdrh ** The SQLITE_ECEL_DUP flag prevents the arguments from being 3398d1a01edaSdrh ** filled using OP_SCopy. OP_Copy must be used instead. 3399d1a01edaSdrh ** 3400d1a01edaSdrh ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 3401d1a01edaSdrh ** factored out into initialization code. 3402b0df9634Sdrh ** 3403b0df9634Sdrh ** The SQLITE_ECEL_REF flag means that expressions in the list with 3404b0df9634Sdrh ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored 3405b0df9634Sdrh ** in registers at srcReg, and so the value can be copied from there. 3406268380caSdrh */ 34074adee20fSdanielk1977 int sqlite3ExprCodeExprList( 3408268380caSdrh Parse *pParse, /* Parsing context */ 3409389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 3410191b54cbSdrh int target, /* Where to write results */ 34115579d59fSdrh int srcReg, /* Source registers if SQLITE_ECEL_REF */ 3412d1a01edaSdrh u8 flags /* SQLITE_ECEL_* flags */ 3413268380caSdrh ){ 3414268380caSdrh struct ExprList_item *pItem; 34155579d59fSdrh int i, j, n; 3416d1a01edaSdrh u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 34175579d59fSdrh Vdbe *v = pParse->pVdbe; 34189d8b3072Sdrh assert( pList!=0 ); 34199cbf3425Sdrh assert( target>0 ); 3420d81a142bSdrh assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 3421268380caSdrh n = pList->nExpr; 3422d9f158e7Sdrh if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 3423191b54cbSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 34247445ffe2Sdrh Expr *pExpr = pItem->pExpr; 34255579d59fSdrh if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ 34265579d59fSdrh sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); 34275579d59fSdrh }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ 3428d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); 3429d1a01edaSdrh }else{ 34307445ffe2Sdrh int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 3431746fd9ccSdrh if( inReg!=target+i ){ 34324eded604Sdrh VdbeOp *pOp; 34334eded604Sdrh if( copyOp==OP_Copy 34344eded604Sdrh && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy 34354eded604Sdrh && pOp->p1+pOp->p3+1==inReg 34364eded604Sdrh && pOp->p2+pOp->p3+1==target+i 34374eded604Sdrh ){ 34384eded604Sdrh pOp->p3++; 34394eded604Sdrh }else{ 34404eded604Sdrh sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 34414eded604Sdrh } 3442d1a01edaSdrh } 3443d176611bSdrh } 3444268380caSdrh } 3445f9b596ebSdrh return n; 3446268380caSdrh } 3447268380caSdrh 3448268380caSdrh /* 344936c563a2Sdrh ** Generate code for a BETWEEN operator. 345036c563a2Sdrh ** 345136c563a2Sdrh ** x BETWEEN y AND z 345236c563a2Sdrh ** 345336c563a2Sdrh ** The above is equivalent to 345436c563a2Sdrh ** 345536c563a2Sdrh ** x>=y AND x<=z 345636c563a2Sdrh ** 345736c563a2Sdrh ** Code it as such, taking care to do the common subexpression 345860ec914cSpeter.d.reid ** elimination of x. 345936c563a2Sdrh */ 346036c563a2Sdrh static void exprCodeBetween( 346136c563a2Sdrh Parse *pParse, /* Parsing and code generating context */ 346236c563a2Sdrh Expr *pExpr, /* The BETWEEN expression */ 346336c563a2Sdrh int dest, /* Jump here if the jump is taken */ 346436c563a2Sdrh int jumpIfTrue, /* Take the jump if the BETWEEN is true */ 346536c563a2Sdrh int jumpIfNull /* Take the jump if the BETWEEN is NULL */ 346636c563a2Sdrh ){ 346736c563a2Sdrh Expr exprAnd; /* The AND operator in x>=y AND x<=z */ 346836c563a2Sdrh Expr compLeft; /* The x>=y term */ 346936c563a2Sdrh Expr compRight; /* The x<=z term */ 347036c563a2Sdrh Expr exprX; /* The x subexpression */ 347136c563a2Sdrh int regFree1 = 0; /* Temporary use register */ 347236c563a2Sdrh 347336c563a2Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 347436c563a2Sdrh exprX = *pExpr->pLeft; 347536c563a2Sdrh exprAnd.op = TK_AND; 347636c563a2Sdrh exprAnd.pLeft = &compLeft; 347736c563a2Sdrh exprAnd.pRight = &compRight; 347836c563a2Sdrh compLeft.op = TK_GE; 347936c563a2Sdrh compLeft.pLeft = &exprX; 348036c563a2Sdrh compLeft.pRight = pExpr->x.pList->a[0].pExpr; 348136c563a2Sdrh compRight.op = TK_LE; 348236c563a2Sdrh compRight.pLeft = &exprX; 348336c563a2Sdrh compRight.pRight = pExpr->x.pList->a[1].pExpr; 3484a4c3c87eSdrh exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); 348536c563a2Sdrh if( jumpIfTrue ){ 348636c563a2Sdrh sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); 348736c563a2Sdrh }else{ 348836c563a2Sdrh sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); 348936c563a2Sdrh } 349036c563a2Sdrh sqlite3ReleaseTempReg(pParse, regFree1); 349136c563a2Sdrh 349236c563a2Sdrh /* Ensure adequate test coverage */ 349336c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); 349436c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); 349536c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); 349636c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); 349736c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 ); 349836c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 ); 349936c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 ); 350036c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 ); 350136c563a2Sdrh } 350236c563a2Sdrh 350336c563a2Sdrh /* 3504cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 3505cce7d176Sdrh ** to the label "dest" if the expression is true but execution 3506cce7d176Sdrh ** continues straight thru if the expression is false. 3507f5905aa7Sdrh ** 3508f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 350935573356Sdrh ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 3510f2bc013cSdrh ** 3511f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 3512f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 3513f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 3514f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 3515f2bc013cSdrh ** below verify that the numbers are aligned correctly. 3516cce7d176Sdrh */ 35174adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3518cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3519cce7d176Sdrh int op = 0; 35202dcef11bSdrh int regFree1 = 0; 35212dcef11bSdrh int regFree2 = 0; 35222dcef11bSdrh int r1, r2; 35232dcef11bSdrh 352435573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 352548864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 352633cd4909Sdrh if( NEVER(pExpr==0) ) return; /* No way this can happen */ 3527f2bc013cSdrh op = pExpr->op; 3528f2bc013cSdrh switch( op ){ 3529cce7d176Sdrh case TK_AND: { 35304adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3531c5499befSdrh testcase( jumpIfNull==0 ); 353235573356Sdrh sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 353354e2adb5Sdrh sqlite3ExprCachePush(pParse); 35344adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 35354adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3536d2490904Sdrh sqlite3ExprCachePop(pParse); 3537cce7d176Sdrh break; 3538cce7d176Sdrh } 3539cce7d176Sdrh case TK_OR: { 3540c5499befSdrh testcase( jumpIfNull==0 ); 35414adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 354254e2adb5Sdrh sqlite3ExprCachePush(pParse); 35434adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 3544d2490904Sdrh sqlite3ExprCachePop(pParse); 3545cce7d176Sdrh break; 3546cce7d176Sdrh } 3547cce7d176Sdrh case TK_NOT: { 3548c5499befSdrh testcase( jumpIfNull==0 ); 35494adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 3550cce7d176Sdrh break; 3551cce7d176Sdrh } 3552*de845c2fSdrh case TK_IS: 3553*de845c2fSdrh case TK_ISNOT: 3554*de845c2fSdrh testcase( op==TK_IS ); 3555*de845c2fSdrh testcase( op==TK_ISNOT ); 3556*de845c2fSdrh op = (op==TK_IS) ? TK_EQ : TK_NE; 3557*de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 3558*de845c2fSdrh /* Fall thru */ 3559cce7d176Sdrh case TK_LT: 3560cce7d176Sdrh case TK_LE: 3561cce7d176Sdrh case TK_GT: 3562cce7d176Sdrh case TK_GE: 3563cce7d176Sdrh case TK_NE: 35640ac65892Sdrh case TK_EQ: { 3565c5499befSdrh testcase( jumpIfNull==0 ); 3566b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3567b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 356835573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 35692dcef11bSdrh r1, r2, dest, jumpIfNull); 35707d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 35717d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 35727d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 35737d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 3574*de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 3575*de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 3576*de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 3577*de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 3578*de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 3579*de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 35806a2fe093Sdrh testcase( regFree1==0 ); 35816a2fe093Sdrh testcase( regFree2==0 ); 35826a2fe093Sdrh break; 35836a2fe093Sdrh } 3584cce7d176Sdrh case TK_ISNULL: 3585cce7d176Sdrh case TK_NOTNULL: { 35867d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 35877d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 35882dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 35892dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 35907d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 35917d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3592c5499befSdrh testcase( regFree1==0 ); 3593cce7d176Sdrh break; 3594cce7d176Sdrh } 3595fef5208cSdrh case TK_BETWEEN: { 35965c03f30aSdrh testcase( jumpIfNull==0 ); 359736c563a2Sdrh exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); 3598fef5208cSdrh break; 3599fef5208cSdrh } 3600bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 3601e3365e6cSdrh case TK_IN: { 3602e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3603e3365e6cSdrh int destIfNull = jumpIfNull ? dest : destIfFalse; 3604e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 3605076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3606e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3607e3365e6cSdrh break; 3608e3365e6cSdrh } 3609bb201344Sshaneh #endif 3610cce7d176Sdrh default: { 3611991a1985Sdrh if( exprAlwaysTrue(pExpr) ){ 3612076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3613991a1985Sdrh }else if( exprAlwaysFalse(pExpr) ){ 3614991a1985Sdrh /* No-op */ 3615991a1985Sdrh }else{ 36162dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 36172dcef11bSdrh sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 3618688852abSdrh VdbeCoverage(v); 3619c5499befSdrh testcase( regFree1==0 ); 3620c5499befSdrh testcase( jumpIfNull==0 ); 3621991a1985Sdrh } 3622cce7d176Sdrh break; 3623cce7d176Sdrh } 3624cce7d176Sdrh } 36252dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 36262dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 3627cce7d176Sdrh } 3628cce7d176Sdrh 3629cce7d176Sdrh /* 363066b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 3631cce7d176Sdrh ** to the label "dest" if the expression is false but execution 3632cce7d176Sdrh ** continues straight thru if the expression is true. 3633f5905aa7Sdrh ** 3634f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 363535573356Sdrh ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 363635573356Sdrh ** is 0. 3637cce7d176Sdrh */ 36384adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3639cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3640cce7d176Sdrh int op = 0; 36412dcef11bSdrh int regFree1 = 0; 36422dcef11bSdrh int regFree2 = 0; 36432dcef11bSdrh int r1, r2; 36442dcef11bSdrh 364535573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 364648864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 364733cd4909Sdrh if( pExpr==0 ) return; 3648f2bc013cSdrh 3649f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 3650f2bc013cSdrh ** 3651f2bc013cSdrh ** pExpr->op op 3652f2bc013cSdrh ** --------- ---------- 3653f2bc013cSdrh ** TK_ISNULL OP_NotNull 3654f2bc013cSdrh ** TK_NOTNULL OP_IsNull 3655f2bc013cSdrh ** TK_NE OP_Eq 3656f2bc013cSdrh ** TK_EQ OP_Ne 3657f2bc013cSdrh ** TK_GT OP_Le 3658f2bc013cSdrh ** TK_LE OP_Gt 3659f2bc013cSdrh ** TK_GE OP_Lt 3660f2bc013cSdrh ** TK_LT OP_Ge 3661f2bc013cSdrh ** 3662f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 3663f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 3664f2bc013cSdrh ** can compute the mapping above using the following expression. 3665f2bc013cSdrh ** Assert()s verify that the computation is correct. 3666f2bc013cSdrh */ 3667f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 3668f2bc013cSdrh 3669f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 3670f2bc013cSdrh */ 3671f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 3672f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 3673f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 3674f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 3675f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 3676f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 3677f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 3678f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 3679f2bc013cSdrh 3680cce7d176Sdrh switch( pExpr->op ){ 3681cce7d176Sdrh case TK_AND: { 3682c5499befSdrh testcase( jumpIfNull==0 ); 36834adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 368454e2adb5Sdrh sqlite3ExprCachePush(pParse); 36854adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 3686d2490904Sdrh sqlite3ExprCachePop(pParse); 3687cce7d176Sdrh break; 3688cce7d176Sdrh } 3689cce7d176Sdrh case TK_OR: { 36904adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3691c5499befSdrh testcase( jumpIfNull==0 ); 369235573356Sdrh sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 369354e2adb5Sdrh sqlite3ExprCachePush(pParse); 36944adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 36954adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3696d2490904Sdrh sqlite3ExprCachePop(pParse); 3697cce7d176Sdrh break; 3698cce7d176Sdrh } 3699cce7d176Sdrh case TK_NOT: { 37005c03f30aSdrh testcase( jumpIfNull==0 ); 37014adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 3702cce7d176Sdrh break; 3703cce7d176Sdrh } 3704*de845c2fSdrh case TK_IS: 3705*de845c2fSdrh case TK_ISNOT: 3706*de845c2fSdrh testcase( pExpr->op==TK_IS ); 3707*de845c2fSdrh testcase( pExpr->op==TK_ISNOT ); 3708*de845c2fSdrh op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; 3709*de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 3710*de845c2fSdrh /* Fall thru */ 3711cce7d176Sdrh case TK_LT: 3712cce7d176Sdrh case TK_LE: 3713cce7d176Sdrh case TK_GT: 3714cce7d176Sdrh case TK_GE: 3715cce7d176Sdrh case TK_NE: 3716cce7d176Sdrh case TK_EQ: { 3717c5499befSdrh testcase( jumpIfNull==0 ); 3718b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3719b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 372035573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 37212dcef11bSdrh r1, r2, dest, jumpIfNull); 37227d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 37237d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 37247d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 37257d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 3726*de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 3727*de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 3728*de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 3729*de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 3730*de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 3731*de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 37326a2fe093Sdrh testcase( regFree1==0 ); 37336a2fe093Sdrh testcase( regFree2==0 ); 37346a2fe093Sdrh break; 37356a2fe093Sdrh } 3736cce7d176Sdrh case TK_ISNULL: 3737cce7d176Sdrh case TK_NOTNULL: { 37382dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 37392dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 37407d176105Sdrh testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); 37417d176105Sdrh testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); 3742c5499befSdrh testcase( regFree1==0 ); 3743cce7d176Sdrh break; 3744cce7d176Sdrh } 3745fef5208cSdrh case TK_BETWEEN: { 37465c03f30aSdrh testcase( jumpIfNull==0 ); 374736c563a2Sdrh exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull); 3748fef5208cSdrh break; 3749fef5208cSdrh } 3750bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 3751e3365e6cSdrh case TK_IN: { 3752e3365e6cSdrh if( jumpIfNull ){ 3753e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, dest); 3754e3365e6cSdrh }else{ 3755e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 3756e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 3757e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 3758e3365e6cSdrh } 3759e3365e6cSdrh break; 3760e3365e6cSdrh } 3761bb201344Sshaneh #endif 3762cce7d176Sdrh default: { 3763991a1985Sdrh if( exprAlwaysFalse(pExpr) ){ 3764076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3765991a1985Sdrh }else if( exprAlwaysTrue(pExpr) ){ 3766991a1985Sdrh /* no-op */ 3767991a1985Sdrh }else{ 37682dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 37692dcef11bSdrh sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 3770688852abSdrh VdbeCoverage(v); 3771c5499befSdrh testcase( regFree1==0 ); 3772c5499befSdrh testcase( jumpIfNull==0 ); 3773991a1985Sdrh } 3774cce7d176Sdrh break; 3775cce7d176Sdrh } 3776cce7d176Sdrh } 37772dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 37782dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 3779cce7d176Sdrh } 37802282792aSdrh 37812282792aSdrh /* 378272bc8208Sdrh ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before 378372bc8208Sdrh ** code generation, and that copy is deleted after code generation. This 378472bc8208Sdrh ** ensures that the original pExpr is unchanged. 378572bc8208Sdrh */ 378672bc8208Sdrh void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ 378772bc8208Sdrh sqlite3 *db = pParse->db; 378872bc8208Sdrh Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); 378972bc8208Sdrh if( db->mallocFailed==0 ){ 379072bc8208Sdrh sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); 379172bc8208Sdrh } 379272bc8208Sdrh sqlite3ExprDelete(db, pCopy); 379372bc8208Sdrh } 379472bc8208Sdrh 379572bc8208Sdrh 379672bc8208Sdrh /* 37971d9da70aSdrh ** Do a deep comparison of two expression trees. Return 0 if the two 37981d9da70aSdrh ** expressions are completely identical. Return 1 if they differ only 37991d9da70aSdrh ** by a COLLATE operator at the top level. Return 2 if there are differences 38001d9da70aSdrh ** other than the top-level COLLATE operator. 3801d40aab0eSdrh ** 3802619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 3803619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 3804619a1305Sdrh ** 380566518ca7Sdrh ** The pA side might be using TK_REGISTER. If that is the case and pB is 380666518ca7Sdrh ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 380766518ca7Sdrh ** 38081d9da70aSdrh ** Sometimes this routine will return 2 even if the two expressions 3809d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 38101d9da70aSdrh ** identical, we return 2 just to be safe. So if this routine 38111d9da70aSdrh ** returns 2, then you do not really know for certain if the two 38121d9da70aSdrh ** expressions are the same. But if you get a 0 or 1 return, then you 3813d40aab0eSdrh ** can be sure the expressions are the same. In the places where 38141d9da70aSdrh ** this routine is used, it does not hurt to get an extra 2 - that 3815d40aab0eSdrh ** just might result in some slightly slower code. But returning 38161d9da70aSdrh ** an incorrect 0 or 1 could lead to a malfunction. 38172282792aSdrh */ 3818619a1305Sdrh int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ 381910d1edf0Sdrh u32 combinedFlags; 38204b202ae2Sdanielk1977 if( pA==0 || pB==0 ){ 38211d9da70aSdrh return pB==pA ? 0 : 2; 38222282792aSdrh } 382310d1edf0Sdrh combinedFlags = pA->flags | pB->flags; 382410d1edf0Sdrh if( combinedFlags & EP_IntValue ){ 382510d1edf0Sdrh if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ 382610d1edf0Sdrh return 0; 382710d1edf0Sdrh } 38281d9da70aSdrh return 2; 38296ab3a2ecSdanielk1977 } 3830c2acc4e4Sdrh if( pA->op!=pB->op ){ 3831619a1305Sdrh if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ 3832ae80ddeaSdrh return 1; 3833ae80ddeaSdrh } 3834619a1305Sdrh if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ 3835ae80ddeaSdrh return 1; 3836ae80ddeaSdrh } 3837ae80ddeaSdrh return 2; 3838ae80ddeaSdrh } 38392edc5fd7Sdrh if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ 3840390b88a4Sdrh if( pA->op==TK_FUNCTION ){ 3841390b88a4Sdrh if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 3842390b88a4Sdrh }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ 384310d1edf0Sdrh return pA->op==TK_COLLATE ? 1 : 2; 384410d1edf0Sdrh } 384510d1edf0Sdrh } 384610d1edf0Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; 384785f8aa79Sdrh if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 384810d1edf0Sdrh if( combinedFlags & EP_xIsSelect ) return 2; 3849619a1305Sdrh if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; 3850619a1305Sdrh if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; 3851619a1305Sdrh if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 38527693c42fSdrh if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ 3853619a1305Sdrh if( pA->iColumn!=pB->iColumn ) return 2; 385466518ca7Sdrh if( pA->iTable!=pB->iTable 385585f8aa79Sdrh && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; 38561d9da70aSdrh } 38571d9da70aSdrh } 38582646da7eSdrh return 0; 38592646da7eSdrh } 38602282792aSdrh 38618c6f666bSdrh /* 38628c6f666bSdrh ** Compare two ExprList objects. Return 0 if they are identical and 38638c6f666bSdrh ** non-zero if they differ in any way. 38648c6f666bSdrh ** 3865619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 3866619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 3867619a1305Sdrh ** 38688c6f666bSdrh ** This routine might return non-zero for equivalent ExprLists. The 38698c6f666bSdrh ** only consequence will be disabled optimizations. But this routine 38708c6f666bSdrh ** must never return 0 if the two ExprList objects are different, or 38718c6f666bSdrh ** a malfunction will result. 38728c6f666bSdrh ** 38738c6f666bSdrh ** Two NULL pointers are considered to be the same. But a NULL pointer 38748c6f666bSdrh ** always differs from a non-NULL pointer. 38758c6f666bSdrh */ 3876619a1305Sdrh int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ 38778c6f666bSdrh int i; 38788c6f666bSdrh if( pA==0 && pB==0 ) return 0; 38798c6f666bSdrh if( pA==0 || pB==0 ) return 1; 38808c6f666bSdrh if( pA->nExpr!=pB->nExpr ) return 1; 38818c6f666bSdrh for(i=0; i<pA->nExpr; i++){ 38828c6f666bSdrh Expr *pExprA = pA->a[i].pExpr; 38838c6f666bSdrh Expr *pExprB = pB->a[i].pExpr; 38848c6f666bSdrh if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; 3885619a1305Sdrh if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; 38868c6f666bSdrh } 38878c6f666bSdrh return 0; 38888c6f666bSdrh } 388913449892Sdrh 38902282792aSdrh /* 38914bd5f73fSdrh ** Return true if we can prove the pE2 will always be true if pE1 is 38924bd5f73fSdrh ** true. Return false if we cannot complete the proof or if pE2 might 38934bd5f73fSdrh ** be false. Examples: 38944bd5f73fSdrh ** 3895619a1305Sdrh ** pE1: x==5 pE2: x==5 Result: true 38964bd5f73fSdrh ** pE1: x>0 pE2: x==5 Result: false 3897619a1305Sdrh ** pE1: x=21 pE2: x=21 OR y=43 Result: true 38984bd5f73fSdrh ** pE1: x!=123 pE2: x IS NOT NULL Result: true 3899619a1305Sdrh ** pE1: x!=?1 pE2: x IS NOT NULL Result: true 3900619a1305Sdrh ** pE1: x IS NULL pE2: x IS NOT NULL Result: false 3901619a1305Sdrh ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false 39024bd5f73fSdrh ** 39034bd5f73fSdrh ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has 39044bd5f73fSdrh ** Expr.iTable<0 then assume a table number given by iTab. 39054bd5f73fSdrh ** 39064bd5f73fSdrh ** When in doubt, return false. Returning true might give a performance 39074bd5f73fSdrh ** improvement. Returning false might cause a performance reduction, but 39084bd5f73fSdrh ** it will always give the correct answer and is hence always safe. 39094bd5f73fSdrh */ 39104bd5f73fSdrh int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ 3911619a1305Sdrh if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ 3912619a1305Sdrh return 1; 3913619a1305Sdrh } 3914619a1305Sdrh if( pE2->op==TK_OR 3915619a1305Sdrh && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) 3916619a1305Sdrh || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) 3917619a1305Sdrh ){ 3918619a1305Sdrh return 1; 3919619a1305Sdrh } 3920619a1305Sdrh if( pE2->op==TK_NOTNULL 3921619a1305Sdrh && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 3922619a1305Sdrh && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) 3923619a1305Sdrh ){ 3924619a1305Sdrh return 1; 3925619a1305Sdrh } 3926619a1305Sdrh return 0; 39274bd5f73fSdrh } 39284bd5f73fSdrh 39294bd5f73fSdrh /* 3930030796dfSdrh ** An instance of the following structure is used by the tree walker 3931030796dfSdrh ** to count references to table columns in the arguments of an 3932ed551b95Sdrh ** aggregate function, in order to implement the 3933ed551b95Sdrh ** sqlite3FunctionThisSrc() routine. 3934374fdce4Sdrh */ 3935030796dfSdrh struct SrcCount { 3936030796dfSdrh SrcList *pSrc; /* One particular FROM clause in a nested query */ 3937030796dfSdrh int nThis; /* Number of references to columns in pSrcList */ 3938030796dfSdrh int nOther; /* Number of references to columns in other FROM clauses */ 3939030796dfSdrh }; 3940030796dfSdrh 3941030796dfSdrh /* 3942030796dfSdrh ** Count the number of references to columns. 3943030796dfSdrh */ 3944030796dfSdrh static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 3945fb0a6081Sdrh /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() 3946fb0a6081Sdrh ** is always called before sqlite3ExprAnalyzeAggregates() and so the 3947fb0a6081Sdrh ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If 3948fb0a6081Sdrh ** sqlite3FunctionUsesThisSrc() is used differently in the future, the 3949fb0a6081Sdrh ** NEVER() will need to be removed. */ 3950fb0a6081Sdrh if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 3951374fdce4Sdrh int i; 3952030796dfSdrh struct SrcCount *p = pWalker->u.pSrcCount; 3953030796dfSdrh SrcList *pSrc = p->pSrc; 3954655814d2Sdrh int nSrc = pSrc ? pSrc->nSrc : 0; 3955655814d2Sdrh for(i=0; i<nSrc; i++){ 3956030796dfSdrh if( pExpr->iTable==pSrc->a[i].iCursor ) break; 3957374fdce4Sdrh } 3958655814d2Sdrh if( i<nSrc ){ 3959030796dfSdrh p->nThis++; 3960374fdce4Sdrh }else{ 3961030796dfSdrh p->nOther++; 3962374fdce4Sdrh } 3963374fdce4Sdrh } 3964030796dfSdrh return WRC_Continue; 3965030796dfSdrh } 3966374fdce4Sdrh 3967374fdce4Sdrh /* 3968030796dfSdrh ** Determine if any of the arguments to the pExpr Function reference 3969030796dfSdrh ** pSrcList. Return true if they do. Also return true if the function 3970030796dfSdrh ** has no arguments or has only constant arguments. Return false if pExpr 3971030796dfSdrh ** references columns but not columns of tables found in pSrcList. 3972374fdce4Sdrh */ 3973030796dfSdrh int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ 3974374fdce4Sdrh Walker w; 3975030796dfSdrh struct SrcCount cnt; 3976374fdce4Sdrh assert( pExpr->op==TK_AGG_FUNCTION ); 3977374fdce4Sdrh memset(&w, 0, sizeof(w)); 3978030796dfSdrh w.xExprCallback = exprSrcCount; 3979030796dfSdrh w.u.pSrcCount = &cnt; 3980030796dfSdrh cnt.pSrc = pSrcList; 3981030796dfSdrh cnt.nThis = 0; 3982030796dfSdrh cnt.nOther = 0; 3983030796dfSdrh sqlite3WalkExprList(&w, pExpr->x.pList); 3984030796dfSdrh return cnt.nThis>0 || cnt.nOther==0; 3985374fdce4Sdrh } 3986374fdce4Sdrh 3987374fdce4Sdrh /* 398813449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 398913449892Sdrh ** the new element. Return a negative number if malloc fails. 39902282792aSdrh */ 399117435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 399213449892Sdrh int i; 3993cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 399417435752Sdrh db, 3995cf643729Sdrh pInfo->aCol, 3996cf643729Sdrh sizeof(pInfo->aCol[0]), 3997cf643729Sdrh &pInfo->nColumn, 3998cf643729Sdrh &i 3999cf643729Sdrh ); 400013449892Sdrh return i; 40012282792aSdrh } 400213449892Sdrh 400313449892Sdrh /* 400413449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 400513449892Sdrh ** the new element. Return a negative number if malloc fails. 400613449892Sdrh */ 400717435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 400813449892Sdrh int i; 4009cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 401017435752Sdrh db, 4011cf643729Sdrh pInfo->aFunc, 4012cf643729Sdrh sizeof(pInfo->aFunc[0]), 4013cf643729Sdrh &pInfo->nFunc, 4014cf643729Sdrh &i 4015cf643729Sdrh ); 401613449892Sdrh return i; 40172282792aSdrh } 40182282792aSdrh 40192282792aSdrh /* 40207d10d5a6Sdrh ** This is the xExprCallback for a tree walker. It is used to 40217d10d5a6Sdrh ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 4022626a879aSdrh ** for additional information. 40232282792aSdrh */ 40247d10d5a6Sdrh static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ 40252282792aSdrh int i; 40267d10d5a6Sdrh NameContext *pNC = pWalker->u.pNC; 4027a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 4028a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 402913449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 403013449892Sdrh 40312282792aSdrh switch( pExpr->op ){ 403289c69d00Sdrh case TK_AGG_COLUMN: 4033967e8b73Sdrh case TK_COLUMN: { 40348b213899Sdrh testcase( pExpr->op==TK_AGG_COLUMN ); 40358b213899Sdrh testcase( pExpr->op==TK_COLUMN ); 403613449892Sdrh /* Check to see if the column is in one of the tables in the FROM 403713449892Sdrh ** clause of the aggregate query */ 403820bc393cSdrh if( ALWAYS(pSrcList!=0) ){ 403913449892Sdrh struct SrcList_item *pItem = pSrcList->a; 404013449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 404113449892Sdrh struct AggInfo_col *pCol; 4042c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 404313449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 404413449892Sdrh /* If we reach this point, it means that pExpr refers to a table 404513449892Sdrh ** that is in the FROM clause of the aggregate query. 404613449892Sdrh ** 404713449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 404813449892Sdrh ** is not an entry there already. 404913449892Sdrh */ 40507f906d63Sdrh int k; 405113449892Sdrh pCol = pAggInfo->aCol; 40527f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 405313449892Sdrh if( pCol->iTable==pExpr->iTable && 405413449892Sdrh pCol->iColumn==pExpr->iColumn ){ 40552282792aSdrh break; 40562282792aSdrh } 40572282792aSdrh } 40581e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 40591e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 40601e536953Sdanielk1977 ){ 40617f906d63Sdrh pCol = &pAggInfo->aCol[k]; 40620817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 406313449892Sdrh pCol->iTable = pExpr->iTable; 406413449892Sdrh pCol->iColumn = pExpr->iColumn; 40650a07c107Sdrh pCol->iMem = ++pParse->nMem; 406613449892Sdrh pCol->iSorterColumn = -1; 40675774b806Sdrh pCol->pExpr = pExpr; 406813449892Sdrh if( pAggInfo->pGroupBy ){ 406913449892Sdrh int j, n; 407013449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 407113449892Sdrh struct ExprList_item *pTerm = pGB->a; 407213449892Sdrh n = pGB->nExpr; 407313449892Sdrh for(j=0; j<n; j++, pTerm++){ 407413449892Sdrh Expr *pE = pTerm->pExpr; 407513449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 407613449892Sdrh pE->iColumn==pExpr->iColumn ){ 407713449892Sdrh pCol->iSorterColumn = j; 407813449892Sdrh break; 40792282792aSdrh } 408013449892Sdrh } 408113449892Sdrh } 408213449892Sdrh if( pCol->iSorterColumn<0 ){ 408313449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 408413449892Sdrh } 408513449892Sdrh } 408613449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 408713449892Sdrh ** because it was there before or because we just created it). 408813449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 408913449892Sdrh ** pAggInfo->aCol[] entry. 409013449892Sdrh */ 4091ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 409213449892Sdrh pExpr->pAggInfo = pAggInfo; 409313449892Sdrh pExpr->op = TK_AGG_COLUMN; 4094cf697396Sshane pExpr->iAgg = (i16)k; 409513449892Sdrh break; 409613449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 409713449892Sdrh } /* end loop over pSrcList */ 4098a58fdfb1Sdanielk1977 } 40997d10d5a6Sdrh return WRC_Prune; 41002282792aSdrh } 41012282792aSdrh case TK_AGG_FUNCTION: { 41023a8c4be7Sdrh if( (pNC->ncFlags & NC_InAggFunc)==0 4103ed551b95Sdrh && pWalker->walkerDepth==pExpr->op2 41043a8c4be7Sdrh ){ 410513449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 410613449892Sdrh ** function that is already in the pAggInfo structure 410713449892Sdrh */ 410813449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 410913449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 4110619a1305Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ 41112282792aSdrh break; 41122282792aSdrh } 41132282792aSdrh } 411413449892Sdrh if( i>=pAggInfo->nFunc ){ 411513449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 411613449892Sdrh */ 411714db2665Sdanielk1977 u8 enc = ENC(pParse->db); 41181e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 411913449892Sdrh if( i>=0 ){ 41206ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 412113449892Sdrh pItem = &pAggInfo->aFunc[i]; 412213449892Sdrh pItem->pExpr = pExpr; 41230a07c107Sdrh pItem->iMem = ++pParse->nMem; 412433e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 412513449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 412680738d9cSdrh pExpr->u.zToken, 41276ab3a2ecSdanielk1977 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); 4128fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 4129fd357974Sdrh pItem->iDistinct = pParse->nTab++; 4130fd357974Sdrh }else{ 4131fd357974Sdrh pItem->iDistinct = -1; 4132fd357974Sdrh } 41332282792aSdrh } 413413449892Sdrh } 413513449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 413613449892Sdrh */ 4137c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 4138ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 4139cf697396Sshane pExpr->iAgg = (i16)i; 414013449892Sdrh pExpr->pAggInfo = pAggInfo; 41413a8c4be7Sdrh return WRC_Prune; 41426e83a57fSdrh }else{ 41436e83a57fSdrh return WRC_Continue; 41446e83a57fSdrh } 41452282792aSdrh } 4146a58fdfb1Sdanielk1977 } 41477d10d5a6Sdrh return WRC_Continue; 41487d10d5a6Sdrh } 41497d10d5a6Sdrh static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ 4150d5a336efSdrh UNUSED_PARAMETER(pWalker); 4151d5a336efSdrh UNUSED_PARAMETER(pSelect); 41527d10d5a6Sdrh return WRC_Continue; 4153a58fdfb1Sdanielk1977 } 4154626a879aSdrh 4155626a879aSdrh /* 4156e8abb4caSdrh ** Analyze the pExpr expression looking for aggregate functions and 4157e8abb4caSdrh ** for variables that need to be added to AggInfo object that pNC->pAggInfo 4158e8abb4caSdrh ** points to. Additional entries are made on the AggInfo object as 4159e8abb4caSdrh ** necessary. 4160626a879aSdrh ** 4161626a879aSdrh ** This routine should only be called after the expression has been 41627d10d5a6Sdrh ** analyzed by sqlite3ResolveExprNames(). 4163626a879aSdrh */ 4164d2b3e23bSdrh void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 41657d10d5a6Sdrh Walker w; 4166374fdce4Sdrh memset(&w, 0, sizeof(w)); 41677d10d5a6Sdrh w.xExprCallback = analyzeAggregate; 41687d10d5a6Sdrh w.xSelectCallback = analyzeAggregatesInSelect; 41697d10d5a6Sdrh w.u.pNC = pNC; 417020bc393cSdrh assert( pNC->pSrcList!=0 ); 41717d10d5a6Sdrh sqlite3WalkExpr(&w, pExpr); 41722282792aSdrh } 41735d9a4af9Sdrh 41745d9a4af9Sdrh /* 41755d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 41765d9a4af9Sdrh ** expression list. Return the number of errors. 41775d9a4af9Sdrh ** 41785d9a4af9Sdrh ** If an error is found, the analysis is cut short. 41795d9a4af9Sdrh */ 4180d2b3e23bSdrh void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 41815d9a4af9Sdrh struct ExprList_item *pItem; 41825d9a4af9Sdrh int i; 41835d9a4af9Sdrh if( pList ){ 4184d2b3e23bSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 4185d2b3e23bSdrh sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 41865d9a4af9Sdrh } 41875d9a4af9Sdrh } 41885d9a4af9Sdrh } 4189892d3179Sdrh 4190892d3179Sdrh /* 4191ceea3321Sdrh ** Allocate a single new register for use to hold some intermediate result. 4192892d3179Sdrh */ 4193892d3179Sdrh int sqlite3GetTempReg(Parse *pParse){ 4194e55cbd72Sdrh if( pParse->nTempReg==0 ){ 4195892d3179Sdrh return ++pParse->nMem; 4196892d3179Sdrh } 41972f425f6bSdanielk1977 return pParse->aTempReg[--pParse->nTempReg]; 4198892d3179Sdrh } 4199ceea3321Sdrh 4200ceea3321Sdrh /* 4201ceea3321Sdrh ** Deallocate a register, making available for reuse for some other 4202ceea3321Sdrh ** purpose. 4203ceea3321Sdrh ** 4204ceea3321Sdrh ** If a register is currently being used by the column cache, then 420560ec914cSpeter.d.reid ** the deallocation is deferred until the column cache line that uses 4206ceea3321Sdrh ** the register becomes stale. 4207ceea3321Sdrh */ 4208892d3179Sdrh void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 42092dcef11bSdrh if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 4210ceea3321Sdrh int i; 4211ceea3321Sdrh struct yColCache *p; 4212ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 4213ceea3321Sdrh if( p->iReg==iReg ){ 4214ceea3321Sdrh p->tempReg = 1; 4215ceea3321Sdrh return; 4216ceea3321Sdrh } 4217ceea3321Sdrh } 4218892d3179Sdrh pParse->aTempReg[pParse->nTempReg++] = iReg; 4219892d3179Sdrh } 4220892d3179Sdrh } 4221892d3179Sdrh 4222892d3179Sdrh /* 4223892d3179Sdrh ** Allocate or deallocate a block of nReg consecutive registers 4224892d3179Sdrh */ 4225892d3179Sdrh int sqlite3GetTempRange(Parse *pParse, int nReg){ 4226e55cbd72Sdrh int i, n; 4227892d3179Sdrh i = pParse->iRangeReg; 4228e55cbd72Sdrh n = pParse->nRangeReg; 4229f49f3523Sdrh if( nReg<=n ){ 4230f49f3523Sdrh assert( !usedAsColumnCache(pParse, i, i+n-1) ); 4231892d3179Sdrh pParse->iRangeReg += nReg; 4232892d3179Sdrh pParse->nRangeReg -= nReg; 4233892d3179Sdrh }else{ 4234892d3179Sdrh i = pParse->nMem+1; 4235892d3179Sdrh pParse->nMem += nReg; 4236892d3179Sdrh } 4237892d3179Sdrh return i; 4238892d3179Sdrh } 4239892d3179Sdrh void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 4240f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iReg, nReg); 4241892d3179Sdrh if( nReg>pParse->nRangeReg ){ 4242892d3179Sdrh pParse->nRangeReg = nReg; 4243892d3179Sdrh pParse->iRangeReg = iReg; 4244892d3179Sdrh } 4245892d3179Sdrh } 4246cdc69557Sdrh 4247cdc69557Sdrh /* 4248cdc69557Sdrh ** Mark all temporary registers as being unavailable for reuse. 4249cdc69557Sdrh */ 4250cdc69557Sdrh void sqlite3ClearTempRegCache(Parse *pParse){ 4251cdc69557Sdrh pParse->nTempReg = 0; 4252cdc69557Sdrh pParse->nRangeReg = 0; 4253cdc69557Sdrh } 4254