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 312*cfbb5e82Sdan /* 313*cfbb5e82Sdan ** If the expression passed as the only argument is of type TK_VECTOR 314*cfbb5e82Sdan ** return the number of expressions in the vector. Or, if the expression 315*cfbb5e82Sdan ** is a sub-select, return the number of columns in the sub-select. For 316*cfbb5e82Sdan ** any other type of expression, return 1. 317*cfbb5e82Sdan */ 31871c57db0Sdan int sqlite3ExprVectorSize(Expr *pExpr){ 31971c57db0Sdan if( (pExpr->flags & EP_Vector)==0 ) return 1; 32071c57db0Sdan if( pExpr->flags & EP_xIsSelect ){ 32171c57db0Sdan return pExpr->x.pSelect->pEList->nExpr; 32271c57db0Sdan } 32371c57db0Sdan return pExpr->x.pList->nExpr; 32471c57db0Sdan } 32571c57db0Sdan 32671c57db0Sdan static Expr *exprVectorField(Expr *pVector, int i){ 327*cfbb5e82Sdan if( (pVector->flags & EP_Vector)==0 ){ 328*cfbb5e82Sdan assert( i==0 ); 329*cfbb5e82Sdan return pVector; 330*cfbb5e82Sdan }else if( pVector->flags & EP_xIsSelect ){ 33171c57db0Sdan return pVector->x.pSelect->pEList->a[i].pExpr; 33271c57db0Sdan } 33371c57db0Sdan return pVector->x.pList->a[i].pExpr; 33471c57db0Sdan } 33571c57db0Sdan 33671c57db0Sdan static void codeVectorCompare(Parse *pParse, Expr *pExpr, int dest){ 33771c57db0Sdan Vdbe *v = pParse->pVdbe; 33871c57db0Sdan Expr *pLeft = pExpr->pLeft; 33971c57db0Sdan Expr *pRight = pExpr->pRight; 34071c57db0Sdan int nLeft = sqlite3ExprVectorSize(pLeft); 34171c57db0Sdan int nRight = sqlite3ExprVectorSize(pRight); 34271c57db0Sdan int addr = sqlite3VdbeMakeLabel(v); 34371c57db0Sdan 34471c57db0Sdan /* Check that both sides of the comparison are vectors, and that 34571c57db0Sdan ** both are the same length. */ 34671c57db0Sdan if( nLeft!=nRight ){ 34771c57db0Sdan sqlite3ErrorMsg(pParse, "invalid use of row value"); 34871c57db0Sdan }else{ 34971c57db0Sdan int p5 = (pExpr->op==TK_IS || pExpr->op==TK_ISNOT) ? SQLITE_NULLEQ : 0; 35071c57db0Sdan int opCmp; 35171c57db0Sdan int opTest; 35271c57db0Sdan int i; 35371c57db0Sdan int p3 = 1; 35471c57db0Sdan int regLeft = 0; 35571c57db0Sdan int regRight = 0; 35671c57db0Sdan 35771c57db0Sdan assert( pExpr->op==TK_EQ || pExpr->op==TK_NE 35871c57db0Sdan || pExpr->op==TK_IS || pExpr->op==TK_ISNOT 35971c57db0Sdan || pExpr->op==TK_LT || pExpr->op==TK_GT 36071c57db0Sdan || pExpr->op==TK_LE || pExpr->op==TK_GE 36171c57db0Sdan ); 36271c57db0Sdan 36371c57db0Sdan switch( pExpr->op ){ 36471c57db0Sdan case TK_EQ: 36571c57db0Sdan case TK_IS: 36671c57db0Sdan opTest = OP_IfNot; 36771c57db0Sdan opCmp = OP_Eq; 36871c57db0Sdan break; 36971c57db0Sdan 37071c57db0Sdan case TK_NE: 37171c57db0Sdan case TK_ISNOT: 37271c57db0Sdan opTest = OP_If; 37371c57db0Sdan opCmp = OP_Ne; 37471c57db0Sdan break; 37571c57db0Sdan 37671c57db0Sdan case TK_LT: 37771c57db0Sdan case TK_LE: 37871c57db0Sdan case TK_GT: 37971c57db0Sdan case TK_GE: 38071c57db0Sdan opCmp = OP_Cmp; 38171c57db0Sdan opTest = OP_CmpTest; 38271c57db0Sdan p3 = pExpr->op; 38371c57db0Sdan break; 38471c57db0Sdan } 38571c57db0Sdan 38671c57db0Sdan if( pLeft->flags & EP_xIsSelect ){ 38771c57db0Sdan assert( pLeft->op==TK_SELECT || pLeft->op==TK_REGISTER ); 38871c57db0Sdan regLeft = sqlite3ExprCodeTarget(pParse, pLeft, 1); 38971c57db0Sdan assert( regLeft!=1 ); 39071c57db0Sdan } 39171c57db0Sdan if( pRight->flags & EP_xIsSelect ){ 39271c57db0Sdan assert( pRight->op==TK_SELECT || pRight->op==TK_REGISTER ); 39371c57db0Sdan regRight = sqlite3ExprCodeTarget(pParse, pRight, 1); 39471c57db0Sdan assert( regRight!=1 ); 39571c57db0Sdan } 39671c57db0Sdan if( pParse->nErr ) return; 39771c57db0Sdan 39871c57db0Sdan for(i=0; i<nLeft; i++){ 39971c57db0Sdan int regFree1 = 0, regFree2 = 0; 40071c57db0Sdan Expr *pL, *pR; 40171c57db0Sdan int r1, r2; 40271c57db0Sdan 40371c57db0Sdan if( regLeft ){ 40471c57db0Sdan pL = pLeft->x.pSelect->pEList->a[i].pExpr; 40571c57db0Sdan r1 = regLeft+i; 40671c57db0Sdan }else{ 40771c57db0Sdan pL = pLeft->x.pList->a[i].pExpr; 40871c57db0Sdan r1 = sqlite3ExprCodeTemp(pParse, pL, ®Free1); 40971c57db0Sdan } 41071c57db0Sdan 41171c57db0Sdan if( regRight ){ 41271c57db0Sdan pR = pRight->x.pSelect->pEList->a[i].pExpr; 41371c57db0Sdan r2 = regRight+i; 41471c57db0Sdan }else{ 41571c57db0Sdan pR = pRight->x.pList->a[i].pExpr; 41671c57db0Sdan r2 = sqlite3ExprCodeTemp(pParse, pR, ®Free1); 41771c57db0Sdan } 41871c57db0Sdan 41971c57db0Sdan codeCompare(pParse, pL, pR, opCmp, r1, r2, dest, SQLITE_STOREP2 | p5); 42071c57db0Sdan sqlite3VdbeAddOp3(v, opTest, dest, addr, p3); 42171c57db0Sdan sqlite3ReleaseTempReg(pParse, regFree1); 42271c57db0Sdan sqlite3ReleaseTempReg(pParse, regFree2); 42371c57db0Sdan } 42471c57db0Sdan } 42571c57db0Sdan 42671c57db0Sdan sqlite3VdbeResolveLabel(v, addr); 42771c57db0Sdan } 42871c57db0Sdan 4294b5255acSdanielk1977 #if SQLITE_MAX_EXPR_DEPTH>0 4304b5255acSdanielk1977 /* 4314b5255acSdanielk1977 ** Check that argument nHeight is less than or equal to the maximum 4324b5255acSdanielk1977 ** expression depth allowed. If it is not, leave an error message in 4334b5255acSdanielk1977 ** pParse. 4344b5255acSdanielk1977 */ 4357d10d5a6Sdrh int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){ 4364b5255acSdanielk1977 int rc = SQLITE_OK; 4374b5255acSdanielk1977 int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH]; 4384b5255acSdanielk1977 if( nHeight>mxHeight ){ 4394b5255acSdanielk1977 sqlite3ErrorMsg(pParse, 4404b5255acSdanielk1977 "Expression tree is too large (maximum depth %d)", mxHeight 4414b5255acSdanielk1977 ); 4424b5255acSdanielk1977 rc = SQLITE_ERROR; 4434b5255acSdanielk1977 } 4444b5255acSdanielk1977 return rc; 4454b5255acSdanielk1977 } 4464b5255acSdanielk1977 4474b5255acSdanielk1977 /* The following three functions, heightOfExpr(), heightOfExprList() 4484b5255acSdanielk1977 ** and heightOfSelect(), are used to determine the maximum height 4494b5255acSdanielk1977 ** of any expression tree referenced by the structure passed as the 4504b5255acSdanielk1977 ** first argument. 4514b5255acSdanielk1977 ** 4524b5255acSdanielk1977 ** If this maximum height is greater than the current value pointed 4534b5255acSdanielk1977 ** to by pnHeight, the second parameter, then set *pnHeight to that 4544b5255acSdanielk1977 ** value. 4554b5255acSdanielk1977 */ 4564b5255acSdanielk1977 static void heightOfExpr(Expr *p, int *pnHeight){ 4574b5255acSdanielk1977 if( p ){ 4584b5255acSdanielk1977 if( p->nHeight>*pnHeight ){ 4594b5255acSdanielk1977 *pnHeight = p->nHeight; 4604b5255acSdanielk1977 } 4614b5255acSdanielk1977 } 4624b5255acSdanielk1977 } 4634b5255acSdanielk1977 static void heightOfExprList(ExprList *p, int *pnHeight){ 4644b5255acSdanielk1977 if( p ){ 4654b5255acSdanielk1977 int i; 4664b5255acSdanielk1977 for(i=0; i<p->nExpr; i++){ 4674b5255acSdanielk1977 heightOfExpr(p->a[i].pExpr, pnHeight); 4684b5255acSdanielk1977 } 4694b5255acSdanielk1977 } 4704b5255acSdanielk1977 } 4714b5255acSdanielk1977 static void heightOfSelect(Select *p, int *pnHeight){ 4724b5255acSdanielk1977 if( p ){ 4734b5255acSdanielk1977 heightOfExpr(p->pWhere, pnHeight); 4744b5255acSdanielk1977 heightOfExpr(p->pHaving, pnHeight); 4754b5255acSdanielk1977 heightOfExpr(p->pLimit, pnHeight); 4764b5255acSdanielk1977 heightOfExpr(p->pOffset, pnHeight); 4774b5255acSdanielk1977 heightOfExprList(p->pEList, pnHeight); 4784b5255acSdanielk1977 heightOfExprList(p->pGroupBy, pnHeight); 4794b5255acSdanielk1977 heightOfExprList(p->pOrderBy, pnHeight); 4804b5255acSdanielk1977 heightOfSelect(p->pPrior, pnHeight); 4814b5255acSdanielk1977 } 4824b5255acSdanielk1977 } 4834b5255acSdanielk1977 4844b5255acSdanielk1977 /* 4854b5255acSdanielk1977 ** Set the Expr.nHeight variable in the structure passed as an 4864b5255acSdanielk1977 ** argument. An expression with no children, Expr.pList or 4874b5255acSdanielk1977 ** Expr.pSelect member has a height of 1. Any other expression 4884b5255acSdanielk1977 ** has a height equal to the maximum height of any other 4894b5255acSdanielk1977 ** referenced Expr plus one. 4902308ed38Sdrh ** 4912308ed38Sdrh ** Also propagate EP_Propagate flags up from Expr.x.pList to Expr.flags, 4922308ed38Sdrh ** if appropriate. 4934b5255acSdanielk1977 */ 4944b5255acSdanielk1977 static void exprSetHeight(Expr *p){ 4954b5255acSdanielk1977 int nHeight = 0; 4964b5255acSdanielk1977 heightOfExpr(p->pLeft, &nHeight); 4974b5255acSdanielk1977 heightOfExpr(p->pRight, &nHeight); 4986ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 4996ab3a2ecSdanielk1977 heightOfSelect(p->x.pSelect, &nHeight); 5002308ed38Sdrh }else if( p->x.pList ){ 5016ab3a2ecSdanielk1977 heightOfExprList(p->x.pList, &nHeight); 5022308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 5036ab3a2ecSdanielk1977 } 5044b5255acSdanielk1977 p->nHeight = nHeight + 1; 5054b5255acSdanielk1977 } 5064b5255acSdanielk1977 5074b5255acSdanielk1977 /* 5084b5255acSdanielk1977 ** Set the Expr.nHeight variable using the exprSetHeight() function. If 5094b5255acSdanielk1977 ** the height is greater than the maximum allowed expression depth, 5104b5255acSdanielk1977 ** leave an error in pParse. 5112308ed38Sdrh ** 5122308ed38Sdrh ** Also propagate all EP_Propagate flags from the Expr.x.pList into 5132308ed38Sdrh ** Expr.flags. 5144b5255acSdanielk1977 */ 5152308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 51674893a4cSdrh if( pParse->nErr ) return; 5174b5255acSdanielk1977 exprSetHeight(p); 5187d10d5a6Sdrh sqlite3ExprCheckHeight(pParse, p->nHeight); 5194b5255acSdanielk1977 } 5204b5255acSdanielk1977 5214b5255acSdanielk1977 /* 5224b5255acSdanielk1977 ** Return the maximum height of any expression tree referenced 5234b5255acSdanielk1977 ** by the select statement passed as an argument. 5244b5255acSdanielk1977 */ 5254b5255acSdanielk1977 int sqlite3SelectExprHeight(Select *p){ 5264b5255acSdanielk1977 int nHeight = 0; 5274b5255acSdanielk1977 heightOfSelect(p, &nHeight); 5284b5255acSdanielk1977 return nHeight; 5294b5255acSdanielk1977 } 5302308ed38Sdrh #else /* ABOVE: Height enforcement enabled. BELOW: Height enforcement off */ 5312308ed38Sdrh /* 5322308ed38Sdrh ** Propagate all EP_Propagate flags from the Expr.x.pList into 5332308ed38Sdrh ** Expr.flags. 5342308ed38Sdrh */ 5352308ed38Sdrh void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p){ 5362308ed38Sdrh if( p && p->x.pList && !ExprHasProperty(p, EP_xIsSelect) ){ 5372308ed38Sdrh p->flags |= EP_Propagate & sqlite3ExprListFlags(p->x.pList); 5382308ed38Sdrh } 5392308ed38Sdrh } 5404b5255acSdanielk1977 #define exprSetHeight(y) 5414b5255acSdanielk1977 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */ 5424b5255acSdanielk1977 543be5c89acSdrh /* 544b7916a78Sdrh ** This routine is the core allocator for Expr nodes. 545b7916a78Sdrh ** 546a76b5dfcSdrh ** Construct a new expression node and return a pointer to it. Memory 547b7916a78Sdrh ** for this node and for the pToken argument is a single allocation 548b7916a78Sdrh ** obtained from sqlite3DbMalloc(). The calling function 549a76b5dfcSdrh ** is responsible for making sure the node eventually gets freed. 550b7916a78Sdrh ** 551b7916a78Sdrh ** If dequote is true, then the token (if it exists) is dequoted. 552e792b5b4Sdrh ** If dequote is false, no dequoting is performed. The deQuote 553b7916a78Sdrh ** parameter is ignored if pToken is NULL or if the token does not 554b7916a78Sdrh ** appear to be quoted. If the quotes were of the form "..." (double-quotes) 555b7916a78Sdrh ** then the EP_DblQuoted flag is set on the expression node. 55633e619fcSdrh ** 55733e619fcSdrh ** Special case: If op==TK_INTEGER and pToken points to a string that 55833e619fcSdrh ** can be translated into a 32-bit integer, then the token is not 55933e619fcSdrh ** stored in u.zToken. Instead, the integer values is written 56033e619fcSdrh ** into u.iValue and the EP_IntValue flag is set. No extra storage 56133e619fcSdrh ** is allocated to hold the integer text and the dequote flag is ignored. 562a76b5dfcSdrh */ 563b7916a78Sdrh Expr *sqlite3ExprAlloc( 564a1644fd8Sdanielk1977 sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 56517435752Sdrh int op, /* Expression opcode */ 566b7916a78Sdrh const Token *pToken, /* Token argument. Might be NULL */ 567b7916a78Sdrh int dequote /* True to dequote */ 56817435752Sdrh ){ 569a76b5dfcSdrh Expr *pNew; 57033e619fcSdrh int nExtra = 0; 571cf697396Sshane int iValue = 0; 572b7916a78Sdrh 573575fad65Sdrh assert( db!=0 ); 574b7916a78Sdrh if( pToken ){ 57533e619fcSdrh if( op!=TK_INTEGER || pToken->z==0 57633e619fcSdrh || sqlite3GetInt32(pToken->z, &iValue)==0 ){ 577b7916a78Sdrh nExtra = pToken->n+1; 578d50ffc41Sdrh assert( iValue>=0 ); 57933e619fcSdrh } 580a76b5dfcSdrh } 581575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra); 582b7916a78Sdrh if( pNew ){ 583ca3862dcSdrh memset(pNew, 0, sizeof(Expr)); 5841bd10f8aSdrh pNew->op = (u8)op; 585a58fdfb1Sdanielk1977 pNew->iAgg = -1; 586a76b5dfcSdrh if( pToken ){ 58733e619fcSdrh if( nExtra==0 ){ 58833e619fcSdrh pNew->flags |= EP_IntValue; 58933e619fcSdrh pNew->u.iValue = iValue; 59033e619fcSdrh }else{ 59133e619fcSdrh pNew->u.zToken = (char*)&pNew[1]; 592b07028f7Sdrh assert( pToken->z!=0 || pToken->n==0 ); 593b07028f7Sdrh if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n); 59433e619fcSdrh pNew->u.zToken[pToken->n] = 0; 595244b9d6eSdrh if( dequote && sqlite3Isquote(pNew->u.zToken[0]) ){ 596244b9d6eSdrh if( pNew->u.zToken[0]=='"' ) pNew->flags |= EP_DblQuoted; 59733e619fcSdrh sqlite3Dequote(pNew->u.zToken); 598a34001c9Sdrh } 599a34001c9Sdrh } 60033e619fcSdrh } 601b7916a78Sdrh #if SQLITE_MAX_EXPR_DEPTH>0 602b7916a78Sdrh pNew->nHeight = 1; 603b7916a78Sdrh #endif 604a34001c9Sdrh } 605a76b5dfcSdrh return pNew; 606a76b5dfcSdrh } 607a76b5dfcSdrh 608a76b5dfcSdrh /* 609b7916a78Sdrh ** Allocate a new expression node from a zero-terminated token that has 610b7916a78Sdrh ** already been dequoted. 611b7916a78Sdrh */ 612b7916a78Sdrh Expr *sqlite3Expr( 613b7916a78Sdrh sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ 614b7916a78Sdrh int op, /* Expression opcode */ 615b7916a78Sdrh const char *zToken /* Token argument. Might be NULL */ 616b7916a78Sdrh ){ 617b7916a78Sdrh Token x; 618b7916a78Sdrh x.z = zToken; 619b7916a78Sdrh x.n = zToken ? sqlite3Strlen30(zToken) : 0; 620b7916a78Sdrh return sqlite3ExprAlloc(db, op, &x, 0); 621b7916a78Sdrh } 622b7916a78Sdrh 623b7916a78Sdrh /* 624b7916a78Sdrh ** Attach subtrees pLeft and pRight to the Expr node pRoot. 625b7916a78Sdrh ** 626b7916a78Sdrh ** If pRoot==NULL that means that a memory allocation error has occurred. 627b7916a78Sdrh ** In that case, delete the subtrees pLeft and pRight. 628b7916a78Sdrh */ 629b7916a78Sdrh void sqlite3ExprAttachSubtrees( 630b7916a78Sdrh sqlite3 *db, 631b7916a78Sdrh Expr *pRoot, 632b7916a78Sdrh Expr *pLeft, 633b7916a78Sdrh Expr *pRight 634b7916a78Sdrh ){ 635b7916a78Sdrh if( pRoot==0 ){ 636b7916a78Sdrh assert( db->mallocFailed ); 637b7916a78Sdrh sqlite3ExprDelete(db, pLeft); 638b7916a78Sdrh sqlite3ExprDelete(db, pRight); 639b7916a78Sdrh }else{ 640b7916a78Sdrh if( pRight ){ 641b7916a78Sdrh pRoot->pRight = pRight; 642885a5b03Sdrh pRoot->flags |= EP_Propagate & pRight->flags; 643b7916a78Sdrh } 644b7916a78Sdrh if( pLeft ){ 645b7916a78Sdrh pRoot->pLeft = pLeft; 646885a5b03Sdrh pRoot->flags |= EP_Propagate & pLeft->flags; 647b7916a78Sdrh } 648b7916a78Sdrh exprSetHeight(pRoot); 649b7916a78Sdrh } 650b7916a78Sdrh } 651b7916a78Sdrh 652b7916a78Sdrh /* 65360ec914cSpeter.d.reid ** Allocate an Expr node which joins as many as two subtrees. 654b7916a78Sdrh ** 655bf664469Sdrh ** One or both of the subtrees can be NULL. Return a pointer to the new 656bf664469Sdrh ** Expr node. Or, if an OOM error occurs, set pParse->db->mallocFailed, 657bf664469Sdrh ** free the subtrees and return NULL. 658206f3d96Sdrh */ 65917435752Sdrh Expr *sqlite3PExpr( 66017435752Sdrh Parse *pParse, /* Parsing context */ 66117435752Sdrh int op, /* Expression opcode */ 66217435752Sdrh Expr *pLeft, /* Left operand */ 66317435752Sdrh Expr *pRight, /* Right operand */ 66417435752Sdrh const Token *pToken /* Argument token */ 66517435752Sdrh ){ 6665fb52caaSdrh Expr *p; 6671167d327Sdrh if( op==TK_AND && pParse->nErr==0 ){ 6685fb52caaSdrh /* Take advantage of short-circuit false optimization for AND */ 6695fb52caaSdrh p = sqlite3ExprAnd(pParse->db, pLeft, pRight); 6705fb52caaSdrh }else{ 6711167d327Sdrh p = sqlite3ExprAlloc(pParse->db, op & TKFLG_MASK, pToken, 1); 672b7916a78Sdrh sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight); 6735fb52caaSdrh } 6742b359bdbSdan if( p ) { 6752b359bdbSdan sqlite3ExprCheckHeight(pParse, p->nHeight); 6762b359bdbSdan } 6774e0cff60Sdrh return p; 6784e0cff60Sdrh } 6794e0cff60Sdrh 6804e0cff60Sdrh /* 68108de4f79Sdrh ** Add pSelect to the Expr.x.pSelect field. Or, if pExpr is NULL (due 68208de4f79Sdrh ** do a memory allocation failure) then delete the pSelect object. 68308de4f79Sdrh */ 68408de4f79Sdrh void sqlite3PExprAddSelect(Parse *pParse, Expr *pExpr, Select *pSelect){ 68508de4f79Sdrh if( pExpr ){ 68608de4f79Sdrh pExpr->x.pSelect = pSelect; 68708de4f79Sdrh ExprSetProperty(pExpr, EP_xIsSelect|EP_Subquery); 68808de4f79Sdrh sqlite3ExprSetHeightAndFlags(pParse, pExpr); 68908de4f79Sdrh }else{ 69008de4f79Sdrh assert( pParse->db->mallocFailed ); 69108de4f79Sdrh sqlite3SelectDelete(pParse->db, pSelect); 69208de4f79Sdrh } 69308de4f79Sdrh } 69408de4f79Sdrh 69508de4f79Sdrh 69608de4f79Sdrh /* 697991a1985Sdrh ** If the expression is always either TRUE or FALSE (respectively), 698991a1985Sdrh ** then return 1. If one cannot determine the truth value of the 699991a1985Sdrh ** expression at compile-time return 0. 700991a1985Sdrh ** 701991a1985Sdrh ** This is an optimization. If is OK to return 0 here even if 702991a1985Sdrh ** the expression really is always false or false (a false negative). 703991a1985Sdrh ** But it is a bug to return 1 if the expression might have different 704991a1985Sdrh ** boolean values in different circumstances (a false positive.) 7055fb52caaSdrh ** 7065fb52caaSdrh ** Note that if the expression is part of conditional for a 7075fb52caaSdrh ** LEFT JOIN, then we cannot determine at compile-time whether or not 7085fb52caaSdrh ** is it true or false, so always return 0. 7095fb52caaSdrh */ 710991a1985Sdrh static int exprAlwaysTrue(Expr *p){ 711991a1985Sdrh int v = 0; 712991a1985Sdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 713991a1985Sdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 714991a1985Sdrh return v!=0; 715991a1985Sdrh } 7165fb52caaSdrh static int exprAlwaysFalse(Expr *p){ 7175fb52caaSdrh int v = 0; 7185fb52caaSdrh if( ExprHasProperty(p, EP_FromJoin) ) return 0; 7195fb52caaSdrh if( !sqlite3ExprIsInteger(p, &v) ) return 0; 7205fb52caaSdrh return v==0; 7215fb52caaSdrh } 7225fb52caaSdrh 7235fb52caaSdrh /* 72491bb0eedSdrh ** Join two expressions using an AND operator. If either expression is 72591bb0eedSdrh ** NULL, then just return the other expression. 7265fb52caaSdrh ** 7275fb52caaSdrh ** If one side or the other of the AND is known to be false, then instead 7285fb52caaSdrh ** of returning an AND expression, just return a constant expression with 7295fb52caaSdrh ** a value of false. 73091bb0eedSdrh */ 7311e536953Sdanielk1977 Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ 73291bb0eedSdrh if( pLeft==0 ){ 73391bb0eedSdrh return pRight; 73491bb0eedSdrh }else if( pRight==0 ){ 73591bb0eedSdrh return pLeft; 7365fb52caaSdrh }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){ 7375fb52caaSdrh sqlite3ExprDelete(db, pLeft); 7385fb52caaSdrh sqlite3ExprDelete(db, pRight); 7395fb52caaSdrh return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0); 74091bb0eedSdrh }else{ 741b7916a78Sdrh Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0); 742b7916a78Sdrh sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight); 743b7916a78Sdrh return pNew; 744a76b5dfcSdrh } 745a76b5dfcSdrh } 746a76b5dfcSdrh 747a76b5dfcSdrh /* 748a76b5dfcSdrh ** Construct a new expression node for a function with multiple 749a76b5dfcSdrh ** arguments. 750a76b5dfcSdrh */ 75117435752Sdrh Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){ 752a76b5dfcSdrh Expr *pNew; 753633e6d57Sdrh sqlite3 *db = pParse->db; 7544b202ae2Sdanielk1977 assert( pToken ); 755b7916a78Sdrh pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1); 756a76b5dfcSdrh if( pNew==0 ){ 757d9da78a2Sdrh sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */ 758a76b5dfcSdrh return 0; 759a76b5dfcSdrh } 7606ab3a2ecSdanielk1977 pNew->x.pList = pList; 7616ab3a2ecSdanielk1977 assert( !ExprHasProperty(pNew, EP_xIsSelect) ); 7622308ed38Sdrh sqlite3ExprSetHeightAndFlags(pParse, pNew); 763a76b5dfcSdrh return pNew; 764a76b5dfcSdrh } 765a76b5dfcSdrh 766a76b5dfcSdrh /* 767fa6bc000Sdrh ** Assign a variable number to an expression that encodes a wildcard 768fa6bc000Sdrh ** in the original SQL statement. 769fa6bc000Sdrh ** 770fa6bc000Sdrh ** Wildcards consisting of a single "?" are assigned the next sequential 771fa6bc000Sdrh ** variable number. 772fa6bc000Sdrh ** 773fa6bc000Sdrh ** Wildcards of the form "?nnn" are assigned the number "nnn". We make 774fa6bc000Sdrh ** sure "nnn" is not too be to avoid a denial of service attack when 775fa6bc000Sdrh ** the SQL statement comes from an external source. 776fa6bc000Sdrh ** 77751f49f17Sdrh ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number 778fa6bc000Sdrh ** as the previous instance of the same wildcard. Or if this is the first 77960ec914cSpeter.d.reid ** instance of the wildcard, the next sequential variable number is 780fa6bc000Sdrh ** assigned. 781fa6bc000Sdrh */ 782fa6bc000Sdrh void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){ 78317435752Sdrh sqlite3 *db = pParse->db; 784b7916a78Sdrh const char *z; 78517435752Sdrh 786fa6bc000Sdrh if( pExpr==0 ) return; 787c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) ); 78833e619fcSdrh z = pExpr->u.zToken; 789b7916a78Sdrh assert( z!=0 ); 790b7916a78Sdrh assert( z[0]!=0 ); 791b7916a78Sdrh if( z[1]==0 ){ 792fa6bc000Sdrh /* Wildcard of the form "?". Assign the next variable number */ 793b7916a78Sdrh assert( z[0]=='?' ); 7948677d308Sdrh pExpr->iColumn = (ynVar)(++pParse->nVar); 795124c0b49Sdrh }else{ 796124c0b49Sdrh ynVar x = 0; 797124c0b49Sdrh u32 n = sqlite3Strlen30(z); 798124c0b49Sdrh if( z[0]=='?' ){ 799fa6bc000Sdrh /* Wildcard of the form "?nnn". Convert "nnn" to an integer and 800fa6bc000Sdrh ** use it as the variable number */ 801c8d735aeSdan i64 i; 802124c0b49Sdrh int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8); 803124c0b49Sdrh pExpr->iColumn = x = (ynVar)i; 804c5499befSdrh testcase( i==0 ); 805c5499befSdrh testcase( i==1 ); 806c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 ); 807c5499befSdrh testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ); 808c8d735aeSdan if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 809fa6bc000Sdrh sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d", 810bb4957f8Sdrh db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]); 811124c0b49Sdrh x = 0; 812fa6bc000Sdrh } 813fa6bc000Sdrh if( i>pParse->nVar ){ 8141df2db7fSshaneh pParse->nVar = (int)i; 815fa6bc000Sdrh } 816fa6bc000Sdrh }else{ 81751f49f17Sdrh /* Wildcards like ":aaa", "$aaa" or "@aaa". Reuse the same variable 818fa6bc000Sdrh ** number as the prior appearance of the same name, or if the name 819fa6bc000Sdrh ** has never appeared before, reuse the same variable number 820fa6bc000Sdrh */ 821124c0b49Sdrh ynVar i; 822124c0b49Sdrh for(i=0; i<pParse->nzVar; i++){ 823503a686eSdrh if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){ 824124c0b49Sdrh pExpr->iColumn = x = (ynVar)i+1; 825fa6bc000Sdrh break; 826fa6bc000Sdrh } 827fa6bc000Sdrh } 828124c0b49Sdrh if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar); 829fa6bc000Sdrh } 830124c0b49Sdrh if( x>0 ){ 831124c0b49Sdrh if( x>pParse->nzVar ){ 832124c0b49Sdrh char **a; 833124c0b49Sdrh a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0])); 8344a642b60Sdrh if( a==0 ){ 8354a642b60Sdrh assert( db->mallocFailed ); /* Error reported through mallocFailed */ 8364a642b60Sdrh return; 8374a642b60Sdrh } 838124c0b49Sdrh pParse->azVar = a; 839124c0b49Sdrh memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0])); 840124c0b49Sdrh pParse->nzVar = x; 841124c0b49Sdrh } 842124c0b49Sdrh if( z[0]!='?' || pParse->azVar[x-1]==0 ){ 843124c0b49Sdrh sqlite3DbFree(db, pParse->azVar[x-1]); 844124c0b49Sdrh pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n); 845fa6bc000Sdrh } 846fa6bc000Sdrh } 847fa6bc000Sdrh } 848bb4957f8Sdrh if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){ 849832b2664Sdanielk1977 sqlite3ErrorMsg(pParse, "too many SQL variables"); 850832b2664Sdanielk1977 } 851fa6bc000Sdrh } 852fa6bc000Sdrh 853fa6bc000Sdrh /* 854f6963f99Sdan ** Recursively delete an expression tree. 855a2e00042Sdrh */ 8564f0010b1Sdrh static SQLITE_NOINLINE void sqlite3ExprDeleteNN(sqlite3 *db, Expr *p){ 8574f0010b1Sdrh assert( p!=0 ); 858d50ffc41Sdrh /* Sanity check: Assert that the IntValue is non-negative if it exists */ 859d50ffc41Sdrh assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 ); 860c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 861c5cd1249Sdrh /* The Expr.x union is never used at the same time as Expr.pRight */ 862c5cd1249Sdrh assert( p->x.pList==0 || p->pRight==0 ); 86371c57db0Sdan if( p->op!=TK_SELECT_COLUMN ) sqlite3ExprDelete(db, p->pLeft); 864633e6d57Sdrh sqlite3ExprDelete(db, p->pRight); 865c5cd1249Sdrh if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken); 8666ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 8676ab3a2ecSdanielk1977 sqlite3SelectDelete(db, p->x.pSelect); 8686ab3a2ecSdanielk1977 }else{ 8696ab3a2ecSdanielk1977 sqlite3ExprListDelete(db, p->x.pList); 8706ab3a2ecSdanielk1977 } 8716ab3a2ecSdanielk1977 } 87233e619fcSdrh if( !ExprHasProperty(p, EP_Static) ){ 873633e6d57Sdrh sqlite3DbFree(db, p); 874a2e00042Sdrh } 87533e619fcSdrh } 8764f0010b1Sdrh void sqlite3ExprDelete(sqlite3 *db, Expr *p){ 8774f0010b1Sdrh if( p ) sqlite3ExprDeleteNN(db, p); 8784f0010b1Sdrh } 879a2e00042Sdrh 880d2687b77Sdrh /* 8816ab3a2ecSdanielk1977 ** Return the number of bytes allocated for the expression structure 8826ab3a2ecSdanielk1977 ** passed as the first argument. This is always one of EXPR_FULLSIZE, 8836ab3a2ecSdanielk1977 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE. 8846ab3a2ecSdanielk1977 */ 8856ab3a2ecSdanielk1977 static int exprStructSize(Expr *p){ 8866ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE; 8876ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE; 8886ab3a2ecSdanielk1977 return EXPR_FULLSIZE; 8896ab3a2ecSdanielk1977 } 8906ab3a2ecSdanielk1977 8916ab3a2ecSdanielk1977 /* 89233e619fcSdrh ** The dupedExpr*Size() routines each return the number of bytes required 89333e619fcSdrh ** to store a copy of an expression or expression tree. They differ in 89433e619fcSdrh ** how much of the tree is measured. 89533e619fcSdrh ** 89633e619fcSdrh ** dupedExprStructSize() Size of only the Expr structure 89733e619fcSdrh ** dupedExprNodeSize() Size of Expr + space for token 89833e619fcSdrh ** dupedExprSize() Expr + token + subtree components 89933e619fcSdrh ** 90033e619fcSdrh *************************************************************************** 90133e619fcSdrh ** 90233e619fcSdrh ** The dupedExprStructSize() function returns two values OR-ed together: 90333e619fcSdrh ** (1) the space required for a copy of the Expr structure only and 90433e619fcSdrh ** (2) the EP_xxx flags that indicate what the structure size should be. 90533e619fcSdrh ** The return values is always one of: 90633e619fcSdrh ** 90733e619fcSdrh ** EXPR_FULLSIZE 90833e619fcSdrh ** EXPR_REDUCEDSIZE | EP_Reduced 90933e619fcSdrh ** EXPR_TOKENONLYSIZE | EP_TokenOnly 91033e619fcSdrh ** 91133e619fcSdrh ** The size of the structure can be found by masking the return value 91233e619fcSdrh ** of this routine with 0xfff. The flags can be found by masking the 91333e619fcSdrh ** return value with EP_Reduced|EP_TokenOnly. 91433e619fcSdrh ** 91533e619fcSdrh ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size 91633e619fcSdrh ** (unreduced) Expr objects as they or originally constructed by the parser. 91733e619fcSdrh ** During expression analysis, extra information is computed and moved into 91833e619fcSdrh ** later parts of teh Expr object and that extra information might get chopped 91933e619fcSdrh ** off if the expression is reduced. Note also that it does not work to 92060ec914cSpeter.d.reid ** make an EXPRDUP_REDUCE copy of a reduced expression. It is only legal 92133e619fcSdrh ** to reduce a pristine expression tree from the parser. The implementation 92233e619fcSdrh ** of dupedExprStructSize() contain multiple assert() statements that attempt 92333e619fcSdrh ** to enforce this constraint. 9246ab3a2ecSdanielk1977 */ 9256ab3a2ecSdanielk1977 static int dupedExprStructSize(Expr *p, int flags){ 9266ab3a2ecSdanielk1977 int nSize; 92733e619fcSdrh assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */ 928aecd8021Sdrh assert( EXPR_FULLSIZE<=0xfff ); 929aecd8021Sdrh assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 ); 9303c19469cSdrh if( 0==flags ){ 9316ab3a2ecSdanielk1977 nSize = EXPR_FULLSIZE; 9326ab3a2ecSdanielk1977 }else{ 933c5cd1249Sdrh assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) ); 93433e619fcSdrh assert( !ExprHasProperty(p, EP_FromJoin) ); 935c5cd1249Sdrh assert( !ExprHasProperty(p, EP_MemToken) ); 936ebb6a65dSdrh assert( !ExprHasProperty(p, EP_NoReduce) ); 937aecd8021Sdrh if( p->pLeft || p->x.pList ){ 93833e619fcSdrh nSize = EXPR_REDUCEDSIZE | EP_Reduced; 93933e619fcSdrh }else{ 940aecd8021Sdrh assert( p->pRight==0 ); 94133e619fcSdrh nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly; 94233e619fcSdrh } 9436ab3a2ecSdanielk1977 } 9446ab3a2ecSdanielk1977 return nSize; 9456ab3a2ecSdanielk1977 } 9466ab3a2ecSdanielk1977 9476ab3a2ecSdanielk1977 /* 94833e619fcSdrh ** This function returns the space in bytes required to store the copy 94933e619fcSdrh ** of the Expr structure and a copy of the Expr.u.zToken string (if that 95033e619fcSdrh ** string is defined.) 9516ab3a2ecSdanielk1977 */ 9526ab3a2ecSdanielk1977 static int dupedExprNodeSize(Expr *p, int flags){ 95333e619fcSdrh int nByte = dupedExprStructSize(p, flags) & 0xfff; 95433e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 95533e619fcSdrh nByte += sqlite3Strlen30(p->u.zToken)+1; 9566ab3a2ecSdanielk1977 } 957bc73971dSdanielk1977 return ROUND8(nByte); 9586ab3a2ecSdanielk1977 } 9596ab3a2ecSdanielk1977 9606ab3a2ecSdanielk1977 /* 9616ab3a2ecSdanielk1977 ** Return the number of bytes required to create a duplicate of the 9626ab3a2ecSdanielk1977 ** expression passed as the first argument. The second argument is a 9636ab3a2ecSdanielk1977 ** mask containing EXPRDUP_XXX flags. 9646ab3a2ecSdanielk1977 ** 9656ab3a2ecSdanielk1977 ** The value returned includes space to create a copy of the Expr struct 96633e619fcSdrh ** itself and the buffer referred to by Expr.u.zToken, if any. 9676ab3a2ecSdanielk1977 ** 9686ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the return value includes 9696ab3a2ecSdanielk1977 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft 9706ab3a2ecSdanielk1977 ** and Expr.pRight variables (but not for any structures pointed to or 9716ab3a2ecSdanielk1977 ** descended from the Expr.x.pList or Expr.x.pSelect variables). 9726ab3a2ecSdanielk1977 */ 9736ab3a2ecSdanielk1977 static int dupedExprSize(Expr *p, int flags){ 9746ab3a2ecSdanielk1977 int nByte = 0; 9756ab3a2ecSdanielk1977 if( p ){ 9766ab3a2ecSdanielk1977 nByte = dupedExprNodeSize(p, flags); 9776ab3a2ecSdanielk1977 if( flags&EXPRDUP_REDUCE ){ 978b7916a78Sdrh nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags); 9796ab3a2ecSdanielk1977 } 9806ab3a2ecSdanielk1977 } 9816ab3a2ecSdanielk1977 return nByte; 9826ab3a2ecSdanielk1977 } 9836ab3a2ecSdanielk1977 9846ab3a2ecSdanielk1977 /* 9856ab3a2ecSdanielk1977 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer 9866ab3a2ecSdanielk1977 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough 98733e619fcSdrh ** to store the copy of expression p, the copies of p->u.zToken 9886ab3a2ecSdanielk1977 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions, 98960ec914cSpeter.d.reid ** if any. Before returning, *pzBuffer is set to the first byte past the 9906ab3a2ecSdanielk1977 ** portion of the buffer copied into by this function. 9916ab3a2ecSdanielk1977 */ 9923c19469cSdrh static Expr *exprDup(sqlite3 *db, Expr *p, int dupFlags, u8 **pzBuffer){ 9933c19469cSdrh Expr *pNew; /* Value to return */ 9943c19469cSdrh u8 *zAlloc; /* Memory space from which to build Expr object */ 9953c19469cSdrh u32 staticFlag; /* EP_Static if space not obtained from malloc */ 9966ab3a2ecSdanielk1977 9973c19469cSdrh assert( db!=0 ); 9983c19469cSdrh assert( p ); 9993c19469cSdrh assert( dupFlags==0 || dupFlags==EXPRDUP_REDUCE ); 10003c19469cSdrh assert( pzBuffer==0 || dupFlags==EXPRDUP_REDUCE ); 10016ab3a2ecSdanielk1977 10026ab3a2ecSdanielk1977 /* Figure out where to write the new Expr structure. */ 10036ab3a2ecSdanielk1977 if( pzBuffer ){ 10046ab3a2ecSdanielk1977 zAlloc = *pzBuffer; 100533e619fcSdrh staticFlag = EP_Static; 10066ab3a2ecSdanielk1977 }else{ 10073c19469cSdrh zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, dupFlags)); 10083c19469cSdrh staticFlag = 0; 10096ab3a2ecSdanielk1977 } 10106ab3a2ecSdanielk1977 pNew = (Expr *)zAlloc; 10116ab3a2ecSdanielk1977 10126ab3a2ecSdanielk1977 if( pNew ){ 10136ab3a2ecSdanielk1977 /* Set nNewSize to the size allocated for the structure pointed to 10146ab3a2ecSdanielk1977 ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or 10156ab3a2ecSdanielk1977 ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed 101633e619fcSdrh ** by the copy of the p->u.zToken string (if any). 10176ab3a2ecSdanielk1977 */ 10183c19469cSdrh const unsigned nStructSize = dupedExprStructSize(p, dupFlags); 101933e619fcSdrh const int nNewSize = nStructSize & 0xfff; 102033e619fcSdrh int nToken; 102133e619fcSdrh if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ 102233e619fcSdrh nToken = sqlite3Strlen30(p->u.zToken) + 1; 102333e619fcSdrh }else{ 102433e619fcSdrh nToken = 0; 102533e619fcSdrh } 10263c19469cSdrh if( dupFlags ){ 10276ab3a2ecSdanielk1977 assert( ExprHasProperty(p, EP_Reduced)==0 ); 10286ab3a2ecSdanielk1977 memcpy(zAlloc, p, nNewSize); 10296ab3a2ecSdanielk1977 }else{ 10303e6a1411Sdan u32 nSize = (u32)exprStructSize(p); 10316ab3a2ecSdanielk1977 memcpy(zAlloc, p, nSize); 103272ea29d7Sdrh if( nSize<EXPR_FULLSIZE ){ 10336ab3a2ecSdanielk1977 memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize); 10346ab3a2ecSdanielk1977 } 103572ea29d7Sdrh } 10366ab3a2ecSdanielk1977 103733e619fcSdrh /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */ 1038c5cd1249Sdrh pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken); 103933e619fcSdrh pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly); 104033e619fcSdrh pNew->flags |= staticFlag; 10416ab3a2ecSdanielk1977 104233e619fcSdrh /* Copy the p->u.zToken string, if any. */ 10436ab3a2ecSdanielk1977 if( nToken ){ 104433e619fcSdrh char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize]; 104533e619fcSdrh memcpy(zToken, p->u.zToken, nToken); 10466ab3a2ecSdanielk1977 } 10476ab3a2ecSdanielk1977 10486ab3a2ecSdanielk1977 if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){ 10496ab3a2ecSdanielk1977 /* Fill in the pNew->x.pSelect or pNew->x.pList member. */ 10506ab3a2ecSdanielk1977 if( ExprHasProperty(p, EP_xIsSelect) ){ 10513c19469cSdrh pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, dupFlags); 10526ab3a2ecSdanielk1977 }else{ 10533c19469cSdrh pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, dupFlags); 10546ab3a2ecSdanielk1977 } 10556ab3a2ecSdanielk1977 } 10566ab3a2ecSdanielk1977 10576ab3a2ecSdanielk1977 /* Fill in pNew->pLeft and pNew->pRight. */ 1058c5cd1249Sdrh if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){ 10593c19469cSdrh zAlloc += dupedExprNodeSize(p, dupFlags); 10606ab3a2ecSdanielk1977 if( ExprHasProperty(pNew, EP_Reduced) ){ 10613c19469cSdrh pNew->pLeft = p->pLeft ? 10623c19469cSdrh exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc) : 0; 10633c19469cSdrh pNew->pRight = p->pRight ? 10643c19469cSdrh exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc) : 0; 10656ab3a2ecSdanielk1977 } 10666ab3a2ecSdanielk1977 if( pzBuffer ){ 10676ab3a2ecSdanielk1977 *pzBuffer = zAlloc; 10686ab3a2ecSdanielk1977 } 1069b7916a78Sdrh }else{ 1070c5cd1249Sdrh if( !ExprHasProperty(p, EP_TokenOnly) ){ 10716ab3a2ecSdanielk1977 pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0); 10726ab3a2ecSdanielk1977 pNew->pRight = sqlite3ExprDup(db, p->pRight, 0); 10736ab3a2ecSdanielk1977 } 10746ab3a2ecSdanielk1977 } 10756ab3a2ecSdanielk1977 } 10766ab3a2ecSdanielk1977 return pNew; 10776ab3a2ecSdanielk1977 } 10786ab3a2ecSdanielk1977 10796ab3a2ecSdanielk1977 /* 1080bfe31e7fSdan ** Create and return a deep copy of the object passed as the second 1081bfe31e7fSdan ** argument. If an OOM condition is encountered, NULL is returned 1082bfe31e7fSdan ** and the db->mallocFailed flag set. 1083bfe31e7fSdan */ 1084eede6a53Sdan #ifndef SQLITE_OMIT_CTE 1085bfe31e7fSdan static With *withDup(sqlite3 *db, With *p){ 10864e9119d9Sdan With *pRet = 0; 10874e9119d9Sdan if( p ){ 10884e9119d9Sdan int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1); 10894e9119d9Sdan pRet = sqlite3DbMallocZero(db, nByte); 10904e9119d9Sdan if( pRet ){ 10914e9119d9Sdan int i; 10924e9119d9Sdan pRet->nCte = p->nCte; 10934e9119d9Sdan for(i=0; i<p->nCte; i++){ 10944e9119d9Sdan pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0); 10954e9119d9Sdan pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0); 10964e9119d9Sdan pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName); 10974e9119d9Sdan } 10984e9119d9Sdan } 10994e9119d9Sdan } 11004e9119d9Sdan return pRet; 11014e9119d9Sdan } 1102eede6a53Sdan #else 1103eede6a53Sdan # define withDup(x,y) 0 1104eede6a53Sdan #endif 11054e9119d9Sdan 1106a76b5dfcSdrh /* 1107ff78bd2fSdrh ** The following group of routines make deep copies of expressions, 1108ff78bd2fSdrh ** expression lists, ID lists, and select statements. The copies can 1109ff78bd2fSdrh ** be deleted (by being passed to their respective ...Delete() routines) 1110ff78bd2fSdrh ** without effecting the originals. 1111ff78bd2fSdrh ** 11124adee20fSdanielk1977 ** The expression list, ID, and source lists return by sqlite3ExprListDup(), 11134adee20fSdanielk1977 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded 1114ad3cab52Sdrh ** by subsequent calls to sqlite*ListAppend() routines. 1115ff78bd2fSdrh ** 1116ad3cab52Sdrh ** Any tables that the SrcList might point to are not duplicated. 11176ab3a2ecSdanielk1977 ** 1118b7916a78Sdrh ** The flags parameter contains a combination of the EXPRDUP_XXX flags. 11196ab3a2ecSdanielk1977 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a 11206ab3a2ecSdanielk1977 ** truncated version of the usual Expr structure that will be stored as 11216ab3a2ecSdanielk1977 ** part of the in-memory representation of the database schema. 1122ff78bd2fSdrh */ 11236ab3a2ecSdanielk1977 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){ 112472ea29d7Sdrh assert( flags==0 || flags==EXPRDUP_REDUCE ); 11253c19469cSdrh return p ? exprDup(db, p, flags, 0) : 0; 1126ff78bd2fSdrh } 11276ab3a2ecSdanielk1977 ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){ 1128ff78bd2fSdrh ExprList *pNew; 1129145716b3Sdrh struct ExprList_item *pItem, *pOldItem; 1130ff78bd2fSdrh int i; 1131575fad65Sdrh assert( db!=0 ); 1132ff78bd2fSdrh if( p==0 ) return 0; 1133575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1134ff78bd2fSdrh if( pNew==0 ) return 0; 1135d872bb18Sdrh pNew->nExpr = i = p->nExpr; 1136d872bb18Sdrh if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){} 1137575fad65Sdrh pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) ); 1138e0048400Sdanielk1977 if( pItem==0 ){ 1139633e6d57Sdrh sqlite3DbFree(db, pNew); 1140e0048400Sdanielk1977 return 0; 1141e0048400Sdanielk1977 } 1142145716b3Sdrh pOldItem = p->a; 1143145716b3Sdrh for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){ 11446ab3a2ecSdanielk1977 Expr *pOldExpr = pOldItem->pExpr; 1145b5526ea6Sdrh pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags); 114617435752Sdrh pItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 1147b7916a78Sdrh pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan); 1148145716b3Sdrh pItem->sortOrder = pOldItem->sortOrder; 11493e7bc9caSdrh pItem->done = 0; 11502c036cffSdrh pItem->bSpanIsTab = pOldItem->bSpanIsTab; 1151c2acc4e4Sdrh pItem->u = pOldItem->u; 1152ff78bd2fSdrh } 1153ff78bd2fSdrh return pNew; 1154ff78bd2fSdrh } 115593758c8dSdanielk1977 115693758c8dSdanielk1977 /* 115793758c8dSdanielk1977 ** If cursors, triggers, views and subqueries are all omitted from 115893758c8dSdanielk1977 ** the build, then none of the following routines, except for 115993758c8dSdanielk1977 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes 116093758c8dSdanielk1977 ** called with a NULL argument. 116193758c8dSdanielk1977 */ 11626a67fe8eSdanielk1977 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \ 11636a67fe8eSdanielk1977 || !defined(SQLITE_OMIT_SUBQUERY) 11646ab3a2ecSdanielk1977 SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){ 1165ad3cab52Sdrh SrcList *pNew; 1166ad3cab52Sdrh int i; 1167113088ecSdrh int nByte; 1168575fad65Sdrh assert( db!=0 ); 1169ad3cab52Sdrh if( p==0 ) return 0; 1170113088ecSdrh nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0); 1171575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, nByte ); 1172ad3cab52Sdrh if( pNew==0 ) return 0; 11734305d103Sdrh pNew->nSrc = pNew->nAlloc = p->nSrc; 1174ad3cab52Sdrh for(i=0; i<p->nSrc; i++){ 11754efc4754Sdrh struct SrcList_item *pNewItem = &pNew->a[i]; 11764efc4754Sdrh struct SrcList_item *pOldItem = &p->a[i]; 1177ed8a3bb1Sdrh Table *pTab; 117841fb5cd1Sdan pNewItem->pSchema = pOldItem->pSchema; 117917435752Sdrh pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase); 118017435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 118117435752Sdrh pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias); 11828a48b9c0Sdrh pNewItem->fg = pOldItem->fg; 11834efc4754Sdrh pNewItem->iCursor = pOldItem->iCursor; 11845b6a9ed4Sdrh pNewItem->addrFillSub = pOldItem->addrFillSub; 11855b6a9ed4Sdrh pNewItem->regReturn = pOldItem->regReturn; 11868a48b9c0Sdrh if( pNewItem->fg.isIndexedBy ){ 11878a48b9c0Sdrh pNewItem->u1.zIndexedBy = sqlite3DbStrDup(db, pOldItem->u1.zIndexedBy); 11888a48b9c0Sdrh } 11898a48b9c0Sdrh pNewItem->pIBIndex = pOldItem->pIBIndex; 11908a48b9c0Sdrh if( pNewItem->fg.isTabFunc ){ 11918a48b9c0Sdrh pNewItem->u1.pFuncArg = 11928a48b9c0Sdrh sqlite3ExprListDup(db, pOldItem->u1.pFuncArg, flags); 11938a48b9c0Sdrh } 1194ed8a3bb1Sdrh pTab = pNewItem->pTab = pOldItem->pTab; 1195ed8a3bb1Sdrh if( pTab ){ 1196ed8a3bb1Sdrh pTab->nRef++; 1197a1cb183dSdanielk1977 } 11986ab3a2ecSdanielk1977 pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags); 11996ab3a2ecSdanielk1977 pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags); 120017435752Sdrh pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing); 12016c18b6e0Sdanielk1977 pNewItem->colUsed = pOldItem->colUsed; 1202ad3cab52Sdrh } 1203ad3cab52Sdrh return pNew; 1204ad3cab52Sdrh } 120517435752Sdrh IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){ 1206ff78bd2fSdrh IdList *pNew; 1207ff78bd2fSdrh int i; 1208575fad65Sdrh assert( db!=0 ); 1209ff78bd2fSdrh if( p==0 ) return 0; 1210575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) ); 1211ff78bd2fSdrh if( pNew==0 ) return 0; 12126c535158Sdrh pNew->nId = p->nId; 1213575fad65Sdrh pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) ); 1214d5d56523Sdanielk1977 if( pNew->a==0 ){ 1215633e6d57Sdrh sqlite3DbFree(db, pNew); 1216d5d56523Sdanielk1977 return 0; 1217d5d56523Sdanielk1977 } 12186c535158Sdrh /* Note that because the size of the allocation for p->a[] is not 12196c535158Sdrh ** necessarily a power of two, sqlite3IdListAppend() may not be called 12206c535158Sdrh ** on the duplicate created by this function. */ 1221ff78bd2fSdrh for(i=0; i<p->nId; i++){ 12224efc4754Sdrh struct IdList_item *pNewItem = &pNew->a[i]; 12234efc4754Sdrh struct IdList_item *pOldItem = &p->a[i]; 122417435752Sdrh pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName); 12254efc4754Sdrh pNewItem->idx = pOldItem->idx; 1226ff78bd2fSdrh } 1227ff78bd2fSdrh return pNew; 1228ff78bd2fSdrh } 12296ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 123023b1b372Sdrh Select *pNew, *pPrior; 1231575fad65Sdrh assert( db!=0 ); 1232ff78bd2fSdrh if( p==0 ) return 0; 1233575fad65Sdrh pNew = sqlite3DbMallocRawNN(db, sizeof(*p) ); 1234ff78bd2fSdrh if( pNew==0 ) return 0; 1235b7916a78Sdrh pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags); 12366ab3a2ecSdanielk1977 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags); 12376ab3a2ecSdanielk1977 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags); 12386ab3a2ecSdanielk1977 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags); 12396ab3a2ecSdanielk1977 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags); 12406ab3a2ecSdanielk1977 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags); 1241ff78bd2fSdrh pNew->op = p->op; 124223b1b372Sdrh pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags); 124323b1b372Sdrh if( pPrior ) pPrior->pNext = pNew; 124423b1b372Sdrh pNew->pNext = 0; 12456ab3a2ecSdanielk1977 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags); 12466ab3a2ecSdanielk1977 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags); 124792b01d53Sdrh pNew->iLimit = 0; 124892b01d53Sdrh pNew->iOffset = 0; 12497d10d5a6Sdrh pNew->selFlags = p->selFlags & ~SF_UsesEphemeral; 1250b9bb7c18Sdrh pNew->addrOpenEphm[0] = -1; 1251b9bb7c18Sdrh pNew->addrOpenEphm[1] = -1; 1252ec2da854Sdrh pNew->nSelectRow = p->nSelectRow; 12534e9119d9Sdan pNew->pWith = withDup(db, p->pWith); 1254eb9b884cSdrh sqlite3SelectSetName(pNew, p->zSelName); 1255ff78bd2fSdrh return pNew; 1256ff78bd2fSdrh } 125793758c8dSdanielk1977 #else 12586ab3a2ecSdanielk1977 Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){ 125993758c8dSdanielk1977 assert( p==0 ); 126093758c8dSdanielk1977 return 0; 126193758c8dSdanielk1977 } 126293758c8dSdanielk1977 #endif 1263ff78bd2fSdrh 1264ff78bd2fSdrh 1265ff78bd2fSdrh /* 1266a76b5dfcSdrh ** Add a new element to the end of an expression list. If pList is 1267a76b5dfcSdrh ** initially NULL, then create a new expression list. 1268b7916a78Sdrh ** 1269b7916a78Sdrh ** If a memory allocation error occurs, the entire list is freed and 1270b7916a78Sdrh ** NULL is returned. If non-NULL is returned, then it is guaranteed 1271b7916a78Sdrh ** that the new entry was successfully appended. 1272a76b5dfcSdrh */ 127317435752Sdrh ExprList *sqlite3ExprListAppend( 127417435752Sdrh Parse *pParse, /* Parsing context */ 127517435752Sdrh ExprList *pList, /* List to which to append. Might be NULL */ 1276b7916a78Sdrh Expr *pExpr /* Expression to be appended. Might be NULL */ 127717435752Sdrh ){ 127817435752Sdrh sqlite3 *db = pParse->db; 1279575fad65Sdrh assert( db!=0 ); 1280a76b5dfcSdrh if( pList==0 ){ 1281575fad65Sdrh pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) ); 1282a76b5dfcSdrh if( pList==0 ){ 1283d5d56523Sdanielk1977 goto no_mem; 1284a76b5dfcSdrh } 1285c263f7c4Sdrh pList->nExpr = 0; 1286575fad65Sdrh pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0])); 1287d872bb18Sdrh if( pList->a==0 ) goto no_mem; 1288d872bb18Sdrh }else if( (pList->nExpr & (pList->nExpr-1))==0 ){ 1289d5d56523Sdanielk1977 struct ExprList_item *a; 1290d872bb18Sdrh assert( pList->nExpr>0 ); 1291d872bb18Sdrh a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0])); 1292d5d56523Sdanielk1977 if( a==0 ){ 1293d5d56523Sdanielk1977 goto no_mem; 1294a76b5dfcSdrh } 1295d5d56523Sdanielk1977 pList->a = a; 1296a76b5dfcSdrh } 12974efc4754Sdrh assert( pList->a!=0 ); 1298b7916a78Sdrh if( 1 ){ 12994efc4754Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr++]; 13004efc4754Sdrh memset(pItem, 0, sizeof(*pItem)); 1301e94ddc9eSdanielk1977 pItem->pExpr = pExpr; 1302a76b5dfcSdrh } 1303a76b5dfcSdrh return pList; 1304d5d56523Sdanielk1977 1305d5d56523Sdanielk1977 no_mem: 1306d5d56523Sdanielk1977 /* Avoid leaking memory if malloc has failed. */ 1307633e6d57Sdrh sqlite3ExprDelete(db, pExpr); 1308633e6d57Sdrh sqlite3ExprListDelete(db, pList); 1309d5d56523Sdanielk1977 return 0; 1310a76b5dfcSdrh } 1311a76b5dfcSdrh 1312a76b5dfcSdrh /* 1313bc622bc0Sdrh ** Set the sort order for the last element on the given ExprList. 1314bc622bc0Sdrh */ 1315bc622bc0Sdrh void sqlite3ExprListSetSortOrder(ExprList *p, int iSortOrder){ 1316bc622bc0Sdrh if( p==0 ) return; 1317bc622bc0Sdrh assert( SQLITE_SO_UNDEFINED<0 && SQLITE_SO_ASC>=0 && SQLITE_SO_DESC>0 ); 1318bc622bc0Sdrh assert( p->nExpr>0 ); 1319bc622bc0Sdrh if( iSortOrder<0 ){ 1320bc622bc0Sdrh assert( p->a[p->nExpr-1].sortOrder==SQLITE_SO_ASC ); 1321bc622bc0Sdrh return; 1322bc622bc0Sdrh } 1323bc622bc0Sdrh p->a[p->nExpr-1].sortOrder = (u8)iSortOrder; 1324bc622bc0Sdrh } 1325bc622bc0Sdrh 1326bc622bc0Sdrh /* 1327b7916a78Sdrh ** Set the ExprList.a[].zName element of the most recently added item 1328b7916a78Sdrh ** on the expression list. 1329b7916a78Sdrh ** 1330b7916a78Sdrh ** pList might be NULL following an OOM error. But pName should never be 1331b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1332b7916a78Sdrh ** is set. 1333b7916a78Sdrh */ 1334b7916a78Sdrh void sqlite3ExprListSetName( 1335b7916a78Sdrh Parse *pParse, /* Parsing context */ 1336b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1337b7916a78Sdrh Token *pName, /* Name to be added */ 1338b7916a78Sdrh int dequote /* True to cause the name to be dequoted */ 1339b7916a78Sdrh ){ 1340b7916a78Sdrh assert( pList!=0 || pParse->db->mallocFailed!=0 ); 1341b7916a78Sdrh if( pList ){ 1342b7916a78Sdrh struct ExprList_item *pItem; 1343b7916a78Sdrh assert( pList->nExpr>0 ); 1344b7916a78Sdrh pItem = &pList->a[pList->nExpr-1]; 1345b7916a78Sdrh assert( pItem->zName==0 ); 1346b7916a78Sdrh pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n); 1347244b9d6eSdrh if( dequote ) sqlite3Dequote(pItem->zName); 1348b7916a78Sdrh } 1349b7916a78Sdrh } 1350b7916a78Sdrh 1351b7916a78Sdrh /* 1352b7916a78Sdrh ** Set the ExprList.a[].zSpan element of the most recently added item 1353b7916a78Sdrh ** on the expression list. 1354b7916a78Sdrh ** 1355b7916a78Sdrh ** pList might be NULL following an OOM error. But pSpan should never be 1356b7916a78Sdrh ** NULL. If a memory allocation fails, the pParse->db->mallocFailed flag 1357b7916a78Sdrh ** is set. 1358b7916a78Sdrh */ 1359b7916a78Sdrh void sqlite3ExprListSetSpan( 1360b7916a78Sdrh Parse *pParse, /* Parsing context */ 1361b7916a78Sdrh ExprList *pList, /* List to which to add the span. */ 1362b7916a78Sdrh ExprSpan *pSpan /* The span to be added */ 1363b7916a78Sdrh ){ 1364b7916a78Sdrh sqlite3 *db = pParse->db; 1365b7916a78Sdrh assert( pList!=0 || db->mallocFailed!=0 ); 1366b7916a78Sdrh if( pList ){ 1367b7916a78Sdrh struct ExprList_item *pItem = &pList->a[pList->nExpr-1]; 1368b7916a78Sdrh assert( pList->nExpr>0 ); 1369b7916a78Sdrh assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr ); 1370b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1371b7916a78Sdrh pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart, 1372cf697396Sshane (int)(pSpan->zEnd - pSpan->zStart)); 1373b7916a78Sdrh } 1374b7916a78Sdrh } 1375b7916a78Sdrh 1376b7916a78Sdrh /* 13777a15a4beSdanielk1977 ** If the expression list pEList contains more than iLimit elements, 13787a15a4beSdanielk1977 ** leave an error message in pParse. 13797a15a4beSdanielk1977 */ 13807a15a4beSdanielk1977 void sqlite3ExprListCheckLength( 13817a15a4beSdanielk1977 Parse *pParse, 13827a15a4beSdanielk1977 ExprList *pEList, 13837a15a4beSdanielk1977 const char *zObject 13847a15a4beSdanielk1977 ){ 1385b1a6c3c1Sdrh int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN]; 1386c5499befSdrh testcase( pEList && pEList->nExpr==mx ); 1387c5499befSdrh testcase( pEList && pEList->nExpr==mx+1 ); 1388b1a6c3c1Sdrh if( pEList && pEList->nExpr>mx ){ 13897a15a4beSdanielk1977 sqlite3ErrorMsg(pParse, "too many columns in %s", zObject); 13907a15a4beSdanielk1977 } 13917a15a4beSdanielk1977 } 13927a15a4beSdanielk1977 13937a15a4beSdanielk1977 /* 1394a76b5dfcSdrh ** Delete an entire expression list. 1395a76b5dfcSdrh */ 1396affa855cSdrh static SQLITE_NOINLINE void exprListDeleteNN(sqlite3 *db, ExprList *pList){ 1397a76b5dfcSdrh int i; 1398be5c89acSdrh struct ExprList_item *pItem; 1399d872bb18Sdrh assert( pList->a!=0 || pList->nExpr==0 ); 1400be5c89acSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 1401633e6d57Sdrh sqlite3ExprDelete(db, pItem->pExpr); 1402633e6d57Sdrh sqlite3DbFree(db, pItem->zName); 1403b7916a78Sdrh sqlite3DbFree(db, pItem->zSpan); 1404a76b5dfcSdrh } 1405633e6d57Sdrh sqlite3DbFree(db, pList->a); 1406633e6d57Sdrh sqlite3DbFree(db, pList); 1407a76b5dfcSdrh } 1408affa855cSdrh void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){ 1409affa855cSdrh if( pList ) exprListDeleteNN(db, pList); 1410affa855cSdrh } 1411a76b5dfcSdrh 1412a76b5dfcSdrh /* 14132308ed38Sdrh ** Return the bitwise-OR of all Expr.flags fields in the given 14142308ed38Sdrh ** ExprList. 1415885a5b03Sdrh */ 14162308ed38Sdrh u32 sqlite3ExprListFlags(const ExprList *pList){ 1417885a5b03Sdrh int i; 14182308ed38Sdrh u32 m = 0; 14192308ed38Sdrh if( pList ){ 1420885a5b03Sdrh for(i=0; i<pList->nExpr; i++){ 1421d0c73053Sdrh Expr *pExpr = pList->a[i].pExpr; 1422de845c2fSdrh assert( pExpr!=0 ); 1423de845c2fSdrh m |= pExpr->flags; 1424885a5b03Sdrh } 14252308ed38Sdrh } 14262308ed38Sdrh return m; 1427885a5b03Sdrh } 1428885a5b03Sdrh 1429885a5b03Sdrh /* 1430059b2d50Sdrh ** These routines are Walker callbacks used to check expressions to 1431059b2d50Sdrh ** see if they are "constant" for some definition of constant. The 1432059b2d50Sdrh ** Walker.eCode value determines the type of "constant" we are looking 1433059b2d50Sdrh ** for. 143473b211abSdrh ** 14357d10d5a6Sdrh ** These callback routines are used to implement the following: 1436626a879aSdrh ** 1437059b2d50Sdrh ** sqlite3ExprIsConstant() pWalker->eCode==1 1438059b2d50Sdrh ** sqlite3ExprIsConstantNotJoin() pWalker->eCode==2 1439fcb9f4f3Sdrh ** sqlite3ExprIsTableConstant() pWalker->eCode==3 1440059b2d50Sdrh ** sqlite3ExprIsConstantOrFunction() pWalker->eCode==4 or 5 144187abf5c0Sdrh ** 1442059b2d50Sdrh ** In all cases, the callbacks set Walker.eCode=0 and abort if the expression 1443059b2d50Sdrh ** is found to not be a constant. 144487abf5c0Sdrh ** 1445feada2dfSdrh ** The sqlite3ExprIsConstantOrFunction() is used for evaluating expressions 1446059b2d50Sdrh ** in a CREATE TABLE statement. The Walker.eCode value is 5 when parsing 1447059b2d50Sdrh ** an existing schema and 4 when processing a new statement. A bound 1448feada2dfSdrh ** parameter raises an error for new statements, but is silently converted 1449feada2dfSdrh ** to NULL for existing schemas. This allows sqlite_master tables that 1450feada2dfSdrh ** contain a bound parameter because they were generated by older versions 1451feada2dfSdrh ** of SQLite to be parsed by newer versions of SQLite without raising a 1452feada2dfSdrh ** malformed schema error. 1453626a879aSdrh */ 14547d10d5a6Sdrh static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){ 1455626a879aSdrh 1456059b2d50Sdrh /* If pWalker->eCode is 2 then any term of the expression that comes from 1457059b2d50Sdrh ** the ON or USING clauses of a left join disqualifies the expression 14580a168377Sdrh ** from being considered constant. */ 1459059b2d50Sdrh if( pWalker->eCode==2 && ExprHasProperty(pExpr, EP_FromJoin) ){ 1460059b2d50Sdrh pWalker->eCode = 0; 14617d10d5a6Sdrh return WRC_Abort; 14620a168377Sdrh } 14630a168377Sdrh 1464626a879aSdrh switch( pExpr->op ){ 1465eb55bd2fSdrh /* Consider functions to be constant if all their arguments are constant 1466059b2d50Sdrh ** and either pWalker->eCode==4 or 5 or the function has the 1467059b2d50Sdrh ** SQLITE_FUNC_CONST flag. */ 1468eb55bd2fSdrh case TK_FUNCTION: 146963f84573Sdrh if( pWalker->eCode>=4 || ExprHasProperty(pExpr,EP_ConstFunc) ){ 1470b1fba286Sdrh return WRC_Continue; 1471059b2d50Sdrh }else{ 1472059b2d50Sdrh pWalker->eCode = 0; 1473059b2d50Sdrh return WRC_Abort; 1474b1fba286Sdrh } 1475626a879aSdrh case TK_ID: 1476626a879aSdrh case TK_COLUMN: 1477626a879aSdrh case TK_AGG_FUNCTION: 147813449892Sdrh case TK_AGG_COLUMN: 1479c5499befSdrh testcase( pExpr->op==TK_ID ); 1480c5499befSdrh testcase( pExpr->op==TK_COLUMN ); 1481c5499befSdrh testcase( pExpr->op==TK_AGG_FUNCTION ); 1482c5499befSdrh testcase( pExpr->op==TK_AGG_COLUMN ); 1483059b2d50Sdrh if( pWalker->eCode==3 && pExpr->iTable==pWalker->u.iCur ){ 1484059b2d50Sdrh return WRC_Continue; 1485059b2d50Sdrh }else{ 1486059b2d50Sdrh pWalker->eCode = 0; 14877d10d5a6Sdrh return WRC_Abort; 1488059b2d50Sdrh } 1489feada2dfSdrh case TK_VARIABLE: 1490059b2d50Sdrh if( pWalker->eCode==5 ){ 1491feada2dfSdrh /* Silently convert bound parameters that appear inside of CREATE 1492feada2dfSdrh ** statements into a NULL when parsing the CREATE statement text out 1493feada2dfSdrh ** of the sqlite_master table */ 1494feada2dfSdrh pExpr->op = TK_NULL; 1495059b2d50Sdrh }else if( pWalker->eCode==4 ){ 1496feada2dfSdrh /* A bound parameter in a CREATE statement that originates from 1497feada2dfSdrh ** sqlite3_prepare() causes an error */ 1498059b2d50Sdrh pWalker->eCode = 0; 1499feada2dfSdrh return WRC_Abort; 1500feada2dfSdrh } 1501feada2dfSdrh /* Fall through */ 1502626a879aSdrh default: 1503b74b1017Sdrh testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */ 1504b74b1017Sdrh testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */ 15057d10d5a6Sdrh return WRC_Continue; 1506626a879aSdrh } 1507626a879aSdrh } 150862c14b34Sdanielk1977 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){ 150962c14b34Sdanielk1977 UNUSED_PARAMETER(NotUsed); 1510059b2d50Sdrh pWalker->eCode = 0; 15117d10d5a6Sdrh return WRC_Abort; 15127d10d5a6Sdrh } 1513059b2d50Sdrh static int exprIsConst(Expr *p, int initFlag, int iCur){ 15147d10d5a6Sdrh Walker w; 1515aa87f9a6Sdrh memset(&w, 0, sizeof(w)); 1516059b2d50Sdrh w.eCode = initFlag; 15177d10d5a6Sdrh w.xExprCallback = exprNodeIsConstant; 15187d10d5a6Sdrh w.xSelectCallback = selectNodeIsConstant; 1519059b2d50Sdrh w.u.iCur = iCur; 15207d10d5a6Sdrh sqlite3WalkExpr(&w, p); 1521059b2d50Sdrh return w.eCode; 15227d10d5a6Sdrh } 1523626a879aSdrh 1524626a879aSdrh /* 1525059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1526eb55bd2fSdrh ** and 0 if it involves variables or function calls. 15272398937bSdrh ** 15282398937bSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 15292398937bSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 15302398937bSdrh ** a constant. 1531fef5208cSdrh */ 15324adee20fSdanielk1977 int sqlite3ExprIsConstant(Expr *p){ 1533059b2d50Sdrh return exprIsConst(p, 1, 0); 1534fef5208cSdrh } 1535fef5208cSdrh 1536fef5208cSdrh /* 1537059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 15380a168377Sdrh ** that does no originate from the ON or USING clauses of a join. 15390a168377Sdrh ** Return 0 if it involves variables or function calls or terms from 15400a168377Sdrh ** an ON or USING clause. 15410a168377Sdrh */ 15420a168377Sdrh int sqlite3ExprIsConstantNotJoin(Expr *p){ 1543059b2d50Sdrh return exprIsConst(p, 2, 0); 15440a168377Sdrh } 15450a168377Sdrh 15460a168377Sdrh /* 1547fcb9f4f3Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1548059b2d50Sdrh ** for any single row of the table with cursor iCur. In other words, the 1549059b2d50Sdrh ** expression must not refer to any non-deterministic function nor any 1550059b2d50Sdrh ** table other than iCur. 1551059b2d50Sdrh */ 1552059b2d50Sdrh int sqlite3ExprIsTableConstant(Expr *p, int iCur){ 1553059b2d50Sdrh return exprIsConst(p, 3, iCur); 1554059b2d50Sdrh } 1555059b2d50Sdrh 1556059b2d50Sdrh /* 1557059b2d50Sdrh ** Walk an expression tree. Return non-zero if the expression is constant 1558eb55bd2fSdrh ** or a function call with constant arguments. Return and 0 if there 1559eb55bd2fSdrh ** are any variables. 1560eb55bd2fSdrh ** 1561eb55bd2fSdrh ** For the purposes of this function, a double-quoted string (ex: "abc") 1562eb55bd2fSdrh ** is considered a variable but a single-quoted string (ex: 'abc') is 1563eb55bd2fSdrh ** a constant. 1564eb55bd2fSdrh */ 1565feada2dfSdrh int sqlite3ExprIsConstantOrFunction(Expr *p, u8 isInit){ 1566feada2dfSdrh assert( isInit==0 || isInit==1 ); 1567059b2d50Sdrh return exprIsConst(p, 4+isInit, 0); 1568eb55bd2fSdrh } 1569eb55bd2fSdrh 15705b88bc4bSdrh #ifdef SQLITE_ENABLE_CURSOR_HINTS 15715b88bc4bSdrh /* 15725b88bc4bSdrh ** Walk an expression tree. Return 1 if the expression contains a 15735b88bc4bSdrh ** subquery of some kind. Return 0 if there are no subqueries. 15745b88bc4bSdrh */ 15755b88bc4bSdrh int sqlite3ExprContainsSubquery(Expr *p){ 15765b88bc4bSdrh Walker w; 15775b88bc4bSdrh memset(&w, 0, sizeof(w)); 1578bec2476aSdrh w.eCode = 1; 15795b88bc4bSdrh w.xExprCallback = sqlite3ExprWalkNoop; 15805b88bc4bSdrh w.xSelectCallback = selectNodeIsConstant; 15815b88bc4bSdrh sqlite3WalkExpr(&w, p); 158207194bffSdrh return w.eCode==0; 15835b88bc4bSdrh } 15845b88bc4bSdrh #endif 15855b88bc4bSdrh 1586eb55bd2fSdrh /* 158773b211abSdrh ** If the expression p codes a constant integer that is small enough 1588202b2df7Sdrh ** to fit in a 32-bit integer, return 1 and put the value of the integer 1589202b2df7Sdrh ** in *pValue. If the expression is not an integer or if it is too big 1590202b2df7Sdrh ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged. 1591e4de1febSdrh */ 15924adee20fSdanielk1977 int sqlite3ExprIsInteger(Expr *p, int *pValue){ 159392b01d53Sdrh int rc = 0; 1594cd92e84dSdrh 1595cd92e84dSdrh /* If an expression is an integer literal that fits in a signed 32-bit 1596cd92e84dSdrh ** integer, then the EP_IntValue flag will have already been set */ 1597cd92e84dSdrh assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0 1598cd92e84dSdrh || sqlite3GetInt32(p->u.zToken, &rc)==0 ); 1599cd92e84dSdrh 160092b01d53Sdrh if( p->flags & EP_IntValue ){ 160133e619fcSdrh *pValue = p->u.iValue; 1602e4de1febSdrh return 1; 1603e4de1febSdrh } 160492b01d53Sdrh switch( p->op ){ 16054b59ab5eSdrh case TK_UPLUS: { 160692b01d53Sdrh rc = sqlite3ExprIsInteger(p->pLeft, pValue); 1607f6e369a1Sdrh break; 16084b59ab5eSdrh } 1609e4de1febSdrh case TK_UMINUS: { 1610e4de1febSdrh int v; 16114adee20fSdanielk1977 if( sqlite3ExprIsInteger(p->pLeft, &v) ){ 1612f6418891Smistachkin assert( v!=(-2147483647-1) ); 1613e4de1febSdrh *pValue = -v; 161492b01d53Sdrh rc = 1; 1615e4de1febSdrh } 1616e4de1febSdrh break; 1617e4de1febSdrh } 1618e4de1febSdrh default: break; 1619e4de1febSdrh } 162092b01d53Sdrh return rc; 1621e4de1febSdrh } 1622e4de1febSdrh 1623e4de1febSdrh /* 1624039fc32eSdrh ** Return FALSE if there is no chance that the expression can be NULL. 1625039fc32eSdrh ** 1626039fc32eSdrh ** If the expression might be NULL or if the expression is too complex 1627039fc32eSdrh ** to tell return TRUE. 1628039fc32eSdrh ** 1629039fc32eSdrh ** This routine is used as an optimization, to skip OP_IsNull opcodes 1630039fc32eSdrh ** when we know that a value cannot be NULL. Hence, a false positive 1631039fc32eSdrh ** (returning TRUE when in fact the expression can never be NULL) might 1632039fc32eSdrh ** be a small performance hit but is otherwise harmless. On the other 1633039fc32eSdrh ** hand, a false negative (returning FALSE when the result could be NULL) 1634039fc32eSdrh ** will likely result in an incorrect answer. So when in doubt, return 1635039fc32eSdrh ** TRUE. 1636039fc32eSdrh */ 1637039fc32eSdrh int sqlite3ExprCanBeNull(const Expr *p){ 1638039fc32eSdrh u8 op; 1639cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1640039fc32eSdrh op = p->op; 1641039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1642039fc32eSdrh switch( op ){ 1643039fc32eSdrh case TK_INTEGER: 1644039fc32eSdrh case TK_STRING: 1645039fc32eSdrh case TK_FLOAT: 1646039fc32eSdrh case TK_BLOB: 1647039fc32eSdrh return 0; 16487248a8b2Sdrh case TK_COLUMN: 16497248a8b2Sdrh assert( p->pTab!=0 ); 165072673a24Sdrh return ExprHasProperty(p, EP_CanBeNull) || 165172673a24Sdrh (p->iColumn>=0 && p->pTab->aCol[p->iColumn].notNull==0); 1652039fc32eSdrh default: 1653039fc32eSdrh return 1; 1654039fc32eSdrh } 1655039fc32eSdrh } 1656039fc32eSdrh 1657039fc32eSdrh /* 1658039fc32eSdrh ** Return TRUE if the given expression is a constant which would be 1659039fc32eSdrh ** unchanged by OP_Affinity with the affinity given in the second 1660039fc32eSdrh ** argument. 1661039fc32eSdrh ** 1662039fc32eSdrh ** This routine is used to determine if the OP_Affinity operation 1663039fc32eSdrh ** can be omitted. When in doubt return FALSE. A false negative 1664039fc32eSdrh ** is harmless. A false positive, however, can result in the wrong 1665039fc32eSdrh ** answer. 1666039fc32eSdrh */ 1667039fc32eSdrh int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){ 1668039fc32eSdrh u8 op; 166905883a34Sdrh if( aff==SQLITE_AFF_BLOB ) return 1; 1670cd7f457eSdrh while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; } 1671039fc32eSdrh op = p->op; 1672039fc32eSdrh if( op==TK_REGISTER ) op = p->op2; 1673039fc32eSdrh switch( op ){ 1674039fc32eSdrh case TK_INTEGER: { 1675039fc32eSdrh return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC; 1676039fc32eSdrh } 1677039fc32eSdrh case TK_FLOAT: { 1678039fc32eSdrh return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC; 1679039fc32eSdrh } 1680039fc32eSdrh case TK_STRING: { 1681039fc32eSdrh return aff==SQLITE_AFF_TEXT; 1682039fc32eSdrh } 1683039fc32eSdrh case TK_BLOB: { 1684039fc32eSdrh return 1; 1685039fc32eSdrh } 16862f2855b6Sdrh case TK_COLUMN: { 168788376ca7Sdrh assert( p->iTable>=0 ); /* p cannot be part of a CHECK constraint */ 168888376ca7Sdrh return p->iColumn<0 16892f2855b6Sdrh && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC); 16902f2855b6Sdrh } 1691039fc32eSdrh default: { 1692039fc32eSdrh return 0; 1693039fc32eSdrh } 1694039fc32eSdrh } 1695039fc32eSdrh } 1696039fc32eSdrh 1697039fc32eSdrh /* 1698c4a3c779Sdrh ** Return TRUE if the given string is a row-id column name. 1699c4a3c779Sdrh */ 17004adee20fSdanielk1977 int sqlite3IsRowid(const char *z){ 17014adee20fSdanielk1977 if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1; 17024adee20fSdanielk1977 if( sqlite3StrICmp(z, "ROWID")==0 ) return 1; 17034adee20fSdanielk1977 if( sqlite3StrICmp(z, "OID")==0 ) return 1; 1704c4a3c779Sdrh return 0; 1705c4a3c779Sdrh } 1706c4a3c779Sdrh 17079a96b668Sdanielk1977 /* 170869c355bdSdrh ** pX is the RHS of an IN operator. If pX is a SELECT statement 170969c355bdSdrh ** that can be simplified to a direct table access, then return 171069c355bdSdrh ** a pointer to the SELECT statement. If pX is not a SELECT statement, 171169c355bdSdrh ** or if the SELECT statement needs to be manifested into a transient 171269c355bdSdrh ** table, then return NULL. 1713b287f4b6Sdrh */ 1714b287f4b6Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1715*cfbb5e82Sdan static Select *isCandidateForInOpt(Expr *pX, int bNullSensitive){ 171669c355bdSdrh Select *p; 1717b287f4b6Sdrh SrcList *pSrc; 1718b287f4b6Sdrh ExprList *pEList; 1719b287f4b6Sdrh Table *pTab; 1720*cfbb5e82Sdan int i; 172169c355bdSdrh if( !ExprHasProperty(pX, EP_xIsSelect) ) return 0; /* Not a subquery */ 172269c355bdSdrh if( ExprHasProperty(pX, EP_VarSelect) ) return 0; /* Correlated subq */ 172369c355bdSdrh p = pX->x.pSelect; 1724b287f4b6Sdrh if( p->pPrior ) return 0; /* Not a compound SELECT */ 17257d10d5a6Sdrh if( p->selFlags & (SF_Distinct|SF_Aggregate) ){ 1726b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); 1727b74b1017Sdrh testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); 17287d10d5a6Sdrh return 0; /* No DISTINCT keyword and no aggregate functions */ 17297d10d5a6Sdrh } 1730b74b1017Sdrh assert( p->pGroupBy==0 ); /* Has no GROUP BY clause */ 1731b287f4b6Sdrh if( p->pLimit ) return 0; /* Has no LIMIT clause */ 1732b74b1017Sdrh assert( p->pOffset==0 ); /* No LIMIT means no OFFSET */ 1733b287f4b6Sdrh if( p->pWhere ) return 0; /* Has no WHERE clause */ 1734b287f4b6Sdrh pSrc = p->pSrc; 1735d1fa7bcaSdrh assert( pSrc!=0 ); 1736d1fa7bcaSdrh if( pSrc->nSrc!=1 ) return 0; /* Single term in FROM clause */ 1737b74b1017Sdrh if( pSrc->a[0].pSelect ) return 0; /* FROM is not a subquery or view */ 1738b287f4b6Sdrh pTab = pSrc->a[0].pTab; 173969c355bdSdrh assert( pTab!=0 ); 1740b74b1017Sdrh assert( pTab->pSelect==0 ); /* FROM clause is not a view */ 1741b287f4b6Sdrh if( IsVirtual(pTab) ) return 0; /* FROM clause not a virtual table */ 1742b287f4b6Sdrh pEList = p->pEList; 1743*cfbb5e82Sdan 1744*cfbb5e82Sdan /* All SELECT results must be columns. If the SELECT returns more than 1745*cfbb5e82Sdan ** one column and the bNullSensitive flag is set, all returned columns 1746*cfbb5e82Sdan ** must be declared NOT NULL. */ 1747*cfbb5e82Sdan for(i=0; i<pEList->nExpr; i++){ 1748*cfbb5e82Sdan Expr *pRes = pEList->a[i].pExpr; 1749*cfbb5e82Sdan if( pRes->op!=TK_COLUMN ) return 0; 175069c355bdSdrh assert( pRes->iTable==pSrc->a[0].iCursor ); /* Not a correlated subquery */ 1751*cfbb5e82Sdan if( pEList->nExpr>1 && bNullSensitive ){ 1752*cfbb5e82Sdan if( pTab->aCol[pRes->iColumn].notNull==0 ) return 0; 1753*cfbb5e82Sdan } 1754*cfbb5e82Sdan } 175569c355bdSdrh return p; 1756b287f4b6Sdrh } 1757b287f4b6Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 1758b287f4b6Sdrh 1759b287f4b6Sdrh /* 17601d8cb21fSdan ** Code an OP_Once instruction and allocate space for its flag. Return the 17611d8cb21fSdan ** address of the new instruction. 17621d8cb21fSdan */ 17631d8cb21fSdan int sqlite3CodeOnce(Parse *pParse){ 17641d8cb21fSdan Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 17651d8cb21fSdan return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++); 17661d8cb21fSdan } 17671d8cb21fSdan 17681d8cb21fSdan /* 17694c259e9fSdrh ** Generate code that checks the left-most column of index table iCur to see if 17704c259e9fSdrh ** it contains any NULL entries. Cause the register at regHasNull to be set 17716be515ebSdrh ** to a non-NULL value if iCur contains no NULLs. Cause register regHasNull 17726be515ebSdrh ** to be set to NULL if iCur contains one or more NULL values. 17736be515ebSdrh */ 17746be515ebSdrh static void sqlite3SetHasNullFlag(Vdbe *v, int iCur, int regHasNull){ 1775728e0f91Sdrh int addr1; 17766be515ebSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, regHasNull); 1777728e0f91Sdrh addr1 = sqlite3VdbeAddOp1(v, OP_Rewind, iCur); VdbeCoverage(v); 17786be515ebSdrh sqlite3VdbeAddOp3(v, OP_Column, iCur, 0, regHasNull); 17796be515ebSdrh sqlite3VdbeChangeP5(v, OPFLAG_TYPEOFARG); 17804c259e9fSdrh VdbeComment((v, "first_entry_in(%d)", iCur)); 1781728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 17826be515ebSdrh } 17836be515ebSdrh 1784bb53ecb1Sdrh 1785bb53ecb1Sdrh #ifndef SQLITE_OMIT_SUBQUERY 1786bb53ecb1Sdrh /* 1787bb53ecb1Sdrh ** The argument is an IN operator with a list (not a subquery) on the 1788bb53ecb1Sdrh ** right-hand side. Return TRUE if that list is constant. 1789bb53ecb1Sdrh */ 1790bb53ecb1Sdrh static int sqlite3InRhsIsConstant(Expr *pIn){ 1791bb53ecb1Sdrh Expr *pLHS; 1792bb53ecb1Sdrh int res; 1793bb53ecb1Sdrh assert( !ExprHasProperty(pIn, EP_xIsSelect) ); 1794bb53ecb1Sdrh pLHS = pIn->pLeft; 1795bb53ecb1Sdrh pIn->pLeft = 0; 1796bb53ecb1Sdrh res = sqlite3ExprIsConstant(pIn); 1797bb53ecb1Sdrh pIn->pLeft = pLHS; 1798bb53ecb1Sdrh return res; 1799bb53ecb1Sdrh } 1800bb53ecb1Sdrh #endif 1801bb53ecb1Sdrh 18026be515ebSdrh /* 18039a96b668Sdanielk1977 ** This function is used by the implementation of the IN (...) operator. 1804d4305ca6Sdrh ** The pX parameter is the expression on the RHS of the IN operator, which 1805d4305ca6Sdrh ** might be either a list of expressions or a subquery. 18069a96b668Sdanielk1977 ** 1807d4305ca6Sdrh ** The job of this routine is to find or create a b-tree object that can 1808d4305ca6Sdrh ** be used either to test for membership in the RHS set or to iterate through 1809d4305ca6Sdrh ** all members of the RHS set, skipping duplicates. 1810d4305ca6Sdrh ** 18113a85625dSdrh ** A cursor is opened on the b-tree object that is the RHS of the IN operator 1812d4305ca6Sdrh ** and pX->iTable is set to the index of that cursor. 1813d4305ca6Sdrh ** 1814b74b1017Sdrh ** The returned value of this function indicates the b-tree type, as follows: 18159a96b668Sdanielk1977 ** 18169a96b668Sdanielk1977 ** IN_INDEX_ROWID - The cursor was opened on a database table. 18171ccce449Sdrh ** IN_INDEX_INDEX_ASC - The cursor was opened on an ascending index. 18181ccce449Sdrh ** IN_INDEX_INDEX_DESC - The cursor was opened on a descending index. 18199a96b668Sdanielk1977 ** IN_INDEX_EPH - The cursor was opened on a specially created and 18209a96b668Sdanielk1977 ** populated epheremal table. 1821bb53ecb1Sdrh ** IN_INDEX_NOOP - No cursor was allocated. The IN operator must be 1822bb53ecb1Sdrh ** implemented as a sequence of comparisons. 18239a96b668Sdanielk1977 ** 1824d4305ca6Sdrh ** An existing b-tree might be used if the RHS expression pX is a simple 1825d4305ca6Sdrh ** subquery such as: 18269a96b668Sdanielk1977 ** 18279a96b668Sdanielk1977 ** SELECT <column> FROM <table> 18289a96b668Sdanielk1977 ** 1829d4305ca6Sdrh ** If the RHS of the IN operator is a list or a more complex subquery, then 1830d4305ca6Sdrh ** an ephemeral table might need to be generated from the RHS and then 183160ec914cSpeter.d.reid ** pX->iTable made to point to the ephemeral table instead of an 1832d4305ca6Sdrh ** existing table. 1833d4305ca6Sdrh ** 18343a85625dSdrh ** The inFlags parameter must contain exactly one of the bits 18353a85625dSdrh ** IN_INDEX_MEMBERSHIP or IN_INDEX_LOOP. If inFlags contains 18363a85625dSdrh ** IN_INDEX_MEMBERSHIP, then the generated table will be used for a 18373a85625dSdrh ** fast membership test. When the IN_INDEX_LOOP bit is set, the 18383a85625dSdrh ** IN index will be used to loop over all values of the RHS of the 18393a85625dSdrh ** IN operator. 18403a85625dSdrh ** 18413a85625dSdrh ** When IN_INDEX_LOOP is used (and the b-tree will be used to iterate 18423a85625dSdrh ** through the set members) then the b-tree must not contain duplicates. 18433a85625dSdrh ** An epheremal table must be used unless the selected <column> is guaranteed 18449a96b668Sdanielk1977 ** to be unique - either because it is an INTEGER PRIMARY KEY or it 1845b74b1017Sdrh ** has a UNIQUE constraint or UNIQUE index. 18460cdc022eSdanielk1977 ** 18473a85625dSdrh ** When IN_INDEX_MEMBERSHIP is used (and the b-tree will be used 18483a85625dSdrh ** for fast set membership tests) then an epheremal table must 18490cdc022eSdanielk1977 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can 18500cdc022eSdanielk1977 ** be found with <column> as its left-most column. 18510cdc022eSdanielk1977 ** 1852bb53ecb1Sdrh ** If the IN_INDEX_NOOP_OK and IN_INDEX_MEMBERSHIP are both set and 1853bb53ecb1Sdrh ** if the RHS of the IN operator is a list (not a subquery) then this 1854bb53ecb1Sdrh ** routine might decide that creating an ephemeral b-tree for membership 1855bb53ecb1Sdrh ** testing is too expensive and return IN_INDEX_NOOP. In that case, the 1856bb53ecb1Sdrh ** calling routine should implement the IN operator using a sequence 1857bb53ecb1Sdrh ** of Eq or Ne comparison operations. 1858bb53ecb1Sdrh ** 1859b74b1017Sdrh ** When the b-tree is being used for membership tests, the calling function 18603a85625dSdrh ** might need to know whether or not the RHS side of the IN operator 1861e21a6e1dSdrh ** contains a NULL. If prRhsHasNull is not a NULL pointer and 18623a85625dSdrh ** if there is any chance that the (...) might contain a NULL value at 18630cdc022eSdanielk1977 ** runtime, then a register is allocated and the register number written 1864e21a6e1dSdrh ** to *prRhsHasNull. If there is no chance that the (...) contains a 1865e21a6e1dSdrh ** NULL value, then *prRhsHasNull is left unchanged. 18660cdc022eSdanielk1977 ** 1867e21a6e1dSdrh ** If a register is allocated and its location stored in *prRhsHasNull, then 18686be515ebSdrh ** the value in that register will be NULL if the b-tree contains one or more 18696be515ebSdrh ** NULL values, and it will be some non-NULL value if the b-tree contains no 18706be515ebSdrh ** NULL values. 18719a96b668Sdanielk1977 */ 1872284f4acaSdanielk1977 #ifndef SQLITE_OMIT_SUBQUERY 1873e21a6e1dSdrh int sqlite3FindInIndex(Parse *pParse, Expr *pX, u32 inFlags, int *prRhsHasNull){ 1874b74b1017Sdrh Select *p; /* SELECT to the right of IN operator */ 1875b74b1017Sdrh int eType = 0; /* Type of RHS table. IN_INDEX_* */ 1876b74b1017Sdrh int iTab = pParse->nTab++; /* Cursor of the RHS table */ 18773a85625dSdrh int mustBeUnique; /* True if RHS must be unique */ 1878b8475df8Sdrh Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ 18799a96b668Sdanielk1977 18801450bc6eSdrh assert( pX->op==TK_IN ); 18813a85625dSdrh mustBeUnique = (inFlags & IN_INDEX_LOOP)!=0; 18821450bc6eSdrh 1883b74b1017Sdrh /* Check to see if an existing table or index can be used to 1884b74b1017Sdrh ** satisfy the query. This is preferable to generating a new 1885b74b1017Sdrh ** ephemeral table. 18869a96b668Sdanielk1977 */ 1887*cfbb5e82Sdan if( pParse->nErr==0 && (p = isCandidateForInOpt(pX, prRhsHasNull!=0))!=0 ){ 1888e1fb65a0Sdanielk1977 sqlite3 *db = pParse->db; /* Database connection */ 1889b07028f7Sdrh Table *pTab; /* Table <table>. */ 1890*cfbb5e82Sdan ExprList *pEList = p->pEList; 1891*cfbb5e82Sdan int nExpr = pEList->nExpr; 1892bbbdc83bSdrh i16 iDb; /* Database idx for pTab */ 1893e1fb65a0Sdanielk1977 1894b07028f7Sdrh assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */ 1895b07028f7Sdrh assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */ 1896b07028f7Sdrh assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */ 1897b07028f7Sdrh pTab = p->pSrc->a[0].pTab; 1898b07028f7Sdrh 1899b22f7c83Sdrh /* Code an OP_Transaction and OP_TableLock for <table>. */ 1900e1fb65a0Sdanielk1977 iDb = sqlite3SchemaToIndex(db, pTab->pSchema); 1901e1fb65a0Sdanielk1977 sqlite3CodeVerifySchema(pParse, iDb); 1902e1fb65a0Sdanielk1977 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); 19039a96b668Sdanielk1977 19049a96b668Sdanielk1977 /* This function is only called from two places. In both cases the vdbe 19059a96b668Sdanielk1977 ** has already been allocated. So assume sqlite3GetVdbe() is always 19069a96b668Sdanielk1977 ** successful here. 19079a96b668Sdanielk1977 */ 19089a96b668Sdanielk1977 assert(v); 1909*cfbb5e82Sdan if( nExpr==1 && pEList->a[0].pExpr->iColumn<0 ){ 19107d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); 19117d176105Sdrh VdbeCoverage(v); 19129a96b668Sdanielk1977 19139a96b668Sdanielk1977 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); 19149a96b668Sdanielk1977 eType = IN_INDEX_ROWID; 19159a96b668Sdanielk1977 19169a96b668Sdanielk1977 sqlite3VdbeJumpHere(v, iAddr); 19179a96b668Sdanielk1977 }else{ 1918e1fb65a0Sdanielk1977 Index *pIdx; /* Iterator variable */ 1919*cfbb5e82Sdan int affinity_ok = 1; 1920*cfbb5e82Sdan int i; 1921*cfbb5e82Sdan 1922*cfbb5e82Sdan /* Check that the affinity that will be used to perform each 1923*cfbb5e82Sdan ** comparison is the same as the affinity of each column. If 1924*cfbb5e82Sdan ** it not, it is not possible to use any index. */ 1925*cfbb5e82Sdan for(i=0; i<nExpr && affinity_ok; i++){ 1926*cfbb5e82Sdan Expr *pLhs = exprVectorField(pX->pLeft, i); 1927*cfbb5e82Sdan int iCol = pEList->a[i].pExpr->iColumn; 1928*cfbb5e82Sdan char idxaff = pTab->aCol[iCol].affinity; 1929*cfbb5e82Sdan char cmpaff = sqlite3CompareAffinity(pLhs, idxaff); 1930*cfbb5e82Sdan switch( cmpaff ){ 1931*cfbb5e82Sdan case SQLITE_AFF_BLOB: 1932*cfbb5e82Sdan break; 1933*cfbb5e82Sdan case SQLITE_AFF_TEXT: 1934*cfbb5e82Sdan affinity_ok = (idxaff==SQLITE_AFF_TEXT); 1935*cfbb5e82Sdan break; 1936*cfbb5e82Sdan default: 1937*cfbb5e82Sdan affinity_ok = sqlite3IsNumericAffinity(idxaff); 1938*cfbb5e82Sdan } 1939*cfbb5e82Sdan } 1940e1fb65a0Sdanielk1977 19419a96b668Sdanielk1977 /* The collation sequence used by the comparison. If an index is to 19429a96b668Sdanielk1977 ** be used in place of a temp-table, it must be ordered according 1943e1fb65a0Sdanielk1977 ** to this collation sequence. */ 19449a96b668Sdanielk1977 19459a96b668Sdanielk1977 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ 1946*cfbb5e82Sdan if( pIdx->nKeyCol<nExpr ) continue; 1947*cfbb5e82Sdan if( mustBeUnique && (pIdx->nKeyCol!=nExpr || !IsUniqueIndex(pIdx)) ){ 1948*cfbb5e82Sdan continue; 1949*cfbb5e82Sdan } 1950*cfbb5e82Sdan 1951*cfbb5e82Sdan for(i=0; i<nExpr; i++){ 1952*cfbb5e82Sdan Expr *pLhs = exprVectorField(pX->pLeft, i); 1953*cfbb5e82Sdan Expr *pRhs = pEList->a[i].pExpr; 1954*cfbb5e82Sdan CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pLhs, pRhs); 1955*cfbb5e82Sdan int j; 1956*cfbb5e82Sdan 1957*cfbb5e82Sdan for(j=0; j<nExpr; j++){ 1958*cfbb5e82Sdan if( pIdx->aiColumn[j]!=pRhs->iColumn ) continue; 1959*cfbb5e82Sdan assert( pIdx->azColl[j] ); 1960*cfbb5e82Sdan if( sqlite3StrICmp(pReq->zName, pIdx->azColl[j])!=0 ) continue; 1961*cfbb5e82Sdan break; 1962*cfbb5e82Sdan } 1963*cfbb5e82Sdan if( j==nExpr ) break; 1964*cfbb5e82Sdan } 1965*cfbb5e82Sdan 1966*cfbb5e82Sdan if( i==nExpr ){ 19677d176105Sdrh int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v); 19682ec2fb22Sdrh sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb); 19692ec2fb22Sdrh sqlite3VdbeSetP4KeyInfo(pParse, pIdx); 1970207872a4Sdanielk1977 VdbeComment((v, "%s", pIdx->zName)); 19711ccce449Sdrh assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 ); 19721ccce449Sdrh eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0]; 19739a96b668Sdanielk1977 1974*cfbb5e82Sdan if( prRhsHasNull && nExpr==1 1975*cfbb5e82Sdan && !pTab->aCol[pEList->a[0].pExpr->iColumn].notNull 1976*cfbb5e82Sdan ){ 19773480bfdaSdan #ifdef SQLITE_ENABLE_COLUMN_USED_MASK 1978*cfbb5e82Sdan i64 mask = (1<<nExpr)-1; 19793480bfdaSdan sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, 1980*cfbb5e82Sdan iTab, 0, 0, (u8*)&mask, P4_INT64); 19813480bfdaSdan #endif 1982e21a6e1dSdrh *prRhsHasNull = ++pParse->nMem; 19836be515ebSdrh sqlite3SetHasNullFlag(v, iTab, *prRhsHasNull); 19840cdc022eSdanielk1977 } 1985552fd454Sdrh sqlite3VdbeJumpHere(v, iAddr); 19869a96b668Sdanielk1977 } 19879a96b668Sdanielk1977 } 19889a96b668Sdanielk1977 } 19899a96b668Sdanielk1977 } 19909a96b668Sdanielk1977 1991bb53ecb1Sdrh /* If no preexisting index is available for the IN clause 1992bb53ecb1Sdrh ** and IN_INDEX_NOOP is an allowed reply 1993bb53ecb1Sdrh ** and the RHS of the IN operator is a list, not a subquery 199471c57db0Sdan ** and the RHS is not constant or has two or fewer terms, 199560ec914cSpeter.d.reid ** then it is not worth creating an ephemeral table to evaluate 1996bb53ecb1Sdrh ** the IN operator so return IN_INDEX_NOOP. 1997bb53ecb1Sdrh */ 1998bb53ecb1Sdrh if( eType==0 1999bb53ecb1Sdrh && (inFlags & IN_INDEX_NOOP_OK) 2000bb53ecb1Sdrh && !ExprHasProperty(pX, EP_xIsSelect) 2001bb53ecb1Sdrh && (!sqlite3InRhsIsConstant(pX) || pX->x.pList->nExpr<=2) 2002bb53ecb1Sdrh ){ 2003bb53ecb1Sdrh eType = IN_INDEX_NOOP; 2004bb53ecb1Sdrh } 2005bb53ecb1Sdrh 20069a96b668Sdanielk1977 if( eType==0 ){ 20074387006cSdrh /* Could not find an existing table or index to use as the RHS b-tree. 2008b74b1017Sdrh ** We will have to generate an ephemeral table to do the job. 2009b74b1017Sdrh */ 20108e23daf3Sdrh u32 savedNQueryLoop = pParse->nQueryLoop; 20110cdc022eSdanielk1977 int rMayHaveNull = 0; 201241a05b7bSdanielk1977 eType = IN_INDEX_EPH; 20133a85625dSdrh if( inFlags & IN_INDEX_LOOP ){ 20144a5acf8eSdrh pParse->nQueryLoop = 0; 2015c5cd1249Sdrh if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){ 201641a05b7bSdanielk1977 eType = IN_INDEX_ROWID; 20170cdc022eSdanielk1977 } 2018e21a6e1dSdrh }else if( prRhsHasNull ){ 2019e21a6e1dSdrh *prRhsHasNull = rMayHaveNull = ++pParse->nMem; 2020cf4d38aaSdrh } 202141a05b7bSdanielk1977 sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); 2022cf4d38aaSdrh pParse->nQueryLoop = savedNQueryLoop; 20239a96b668Sdanielk1977 }else{ 20249a96b668Sdanielk1977 pX->iTable = iTab; 20259a96b668Sdanielk1977 } 20269a96b668Sdanielk1977 return eType; 20279a96b668Sdanielk1977 } 2028284f4acaSdanielk1977 #endif 2029626a879aSdrh 203071c57db0Sdan static char *exprINAffinity(Parse *pParse, Expr *pExpr){ 203171c57db0Sdan Expr *pLeft = pExpr->pLeft; 203271c57db0Sdan int nVal = sqlite3ExprVectorSize(pLeft); 203371c57db0Sdan char *zRet; 203471c57db0Sdan 203571c57db0Sdan zRet = sqlite3DbMallocZero(pParse->db, nVal+1); 203671c57db0Sdan if( zRet ){ 203771c57db0Sdan int i; 203871c57db0Sdan for(i=0; i<nVal; i++){ 203971c57db0Sdan Expr *pA; 204071c57db0Sdan char a; 204171c57db0Sdan if( nVal==1 ){ 204271c57db0Sdan pA = pLeft; 204371c57db0Sdan }else{ 204471c57db0Sdan pA = exprVectorField(pLeft, i); 204571c57db0Sdan } 204671c57db0Sdan a = sqlite3ExprAffinity(pA); 204771c57db0Sdan zRet[i] = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[i].pExpr, a); 204871c57db0Sdan } 204971c57db0Sdan zRet[nVal] = '\0'; 205071c57db0Sdan } 205171c57db0Sdan return zRet; 205271c57db0Sdan } 205371c57db0Sdan 2054626a879aSdrh /* 2055d4187c71Sdrh ** Generate code for scalar subqueries used as a subquery expression, EXISTS, 2056d4187c71Sdrh ** or IN operators. Examples: 2057626a879aSdrh ** 20589cbe6352Sdrh ** (SELECT a FROM b) -- subquery 20599cbe6352Sdrh ** EXISTS (SELECT a FROM b) -- EXISTS subquery 20609cbe6352Sdrh ** x IN (4,5,11) -- IN operator with list on right-hand side 20619cbe6352Sdrh ** x IN (SELECT a FROM b) -- IN operator with subquery on the right 2062fef5208cSdrh ** 20639cbe6352Sdrh ** The pExpr parameter describes the expression that contains the IN 20649cbe6352Sdrh ** operator or subquery. 206541a05b7bSdanielk1977 ** 206641a05b7bSdanielk1977 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed 206741a05b7bSdanielk1977 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference 206841a05b7bSdanielk1977 ** to some integer key column of a table B-Tree. In this case, use an 206941a05b7bSdanielk1977 ** intkey B-Tree to store the set of IN(...) values instead of the usual 207041a05b7bSdanielk1977 ** (slower) variable length keys B-Tree. 2071fd773cf9Sdrh ** 2072fd773cf9Sdrh ** If rMayHaveNull is non-zero, that means that the operation is an IN 2073fd773cf9Sdrh ** (not a SELECT or EXISTS) and that the RHS might contains NULLs. 20743a85625dSdrh ** All this routine does is initialize the register given by rMayHaveNull 20753a85625dSdrh ** to NULL. Calling routines will take care of changing this register 20763a85625dSdrh ** value to non-NULL if the RHS is NULL-free. 20771450bc6eSdrh ** 20781450bc6eSdrh ** For a SELECT or EXISTS operator, return the register that holds the 20791450bc6eSdrh ** result. For IN operators or if an error occurs, the return value is 0. 2080cce7d176Sdrh */ 208151522cd3Sdrh #ifndef SQLITE_OMIT_SUBQUERY 20821450bc6eSdrh int sqlite3CodeSubselect( 2083fd773cf9Sdrh Parse *pParse, /* Parsing context */ 2084fd773cf9Sdrh Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ 20856be515ebSdrh int rHasNullFlag, /* Register that records whether NULLs exist in RHS */ 2086fd773cf9Sdrh int isRowid /* If true, LHS of IN operator is a rowid */ 208741a05b7bSdanielk1977 ){ 20886be515ebSdrh int jmpIfDynamic = -1; /* One-time test address */ 20891450bc6eSdrh int rReg = 0; /* Register storing resulting */ 2090b3bce662Sdanielk1977 Vdbe *v = sqlite3GetVdbe(pParse); 20911450bc6eSdrh if( NEVER(v==0) ) return 0; 2092ceea3321Sdrh sqlite3ExprCachePush(pParse); 2093fc976065Sdanielk1977 209457dbd7b3Sdrh /* This code must be run in its entirety every time it is encountered 209557dbd7b3Sdrh ** if any of the following is true: 209657dbd7b3Sdrh ** 209757dbd7b3Sdrh ** * The right-hand side is a correlated subquery 209857dbd7b3Sdrh ** * The right-hand side is an expression list containing variables 209957dbd7b3Sdrh ** * We are inside a trigger 210057dbd7b3Sdrh ** 210157dbd7b3Sdrh ** If all of the above are false, then we can run this code just once 210257dbd7b3Sdrh ** save the results, and reuse the same result on subsequent invocations. 2103b3bce662Sdanielk1977 */ 2104c5cd1249Sdrh if( !ExprHasProperty(pExpr, EP_VarSelect) ){ 21056be515ebSdrh jmpIfDynamic = sqlite3CodeOnce(pParse); VdbeCoverage(v); 2106b3bce662Sdanielk1977 } 2107b3bce662Sdanielk1977 21084a07e3dbSdan #ifndef SQLITE_OMIT_EXPLAIN 21094a07e3dbSdan if( pParse->explain==2 ){ 211062aaa6caSdrh char *zMsg = sqlite3MPrintf(pParse->db, "EXECUTE %s%s SUBQUERY %d", 211162aaa6caSdrh jmpIfDynamic>=0?"":"CORRELATED ", 211262aaa6caSdrh pExpr->op==TK_IN?"LIST":"SCALAR", 211362aaa6caSdrh pParse->iNextSelectId 21144a07e3dbSdan ); 21154a07e3dbSdan sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); 21164a07e3dbSdan } 21174a07e3dbSdan #endif 21184a07e3dbSdan 2119cce7d176Sdrh switch( pExpr->op ){ 2120fef5208cSdrh case TK_IN: { 2121b9bb7c18Sdrh int addr; /* Address of OP_OpenEphemeral instruction */ 2122d4187c71Sdrh Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ 2123323df790Sdrh KeyInfo *pKeyInfo = 0; /* Key information */ 212471c57db0Sdan int nVal; /* Size of vector pLeft */ 2125d3d39e93Sdrh 212671c57db0Sdan nVal = sqlite3ExprVectorSize(pLeft); 2127e014a838Sdanielk1977 2128e014a838Sdanielk1977 /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)' 21298cff69dfSdrh ** expression it is handled the same way. An ephemeral table is 2130e014a838Sdanielk1977 ** filled with single-field index keys representing the results 2131e014a838Sdanielk1977 ** from the SELECT or the <exprlist>. 2132fef5208cSdrh ** 2133e014a838Sdanielk1977 ** If the 'x' expression is a column value, or the SELECT... 2134e014a838Sdanielk1977 ** statement returns a column value, then the affinity of that 2135e014a838Sdanielk1977 ** column is used to build the index keys. If both 'x' and the 2136e014a838Sdanielk1977 ** SELECT... statement are columns, then numeric affinity is used 2137e014a838Sdanielk1977 ** if either column has NUMERIC or INTEGER affinity. If neither 2138e014a838Sdanielk1977 ** 'x' nor the SELECT... statement are columns, then numeric affinity 2139e014a838Sdanielk1977 ** is used. 2140fef5208cSdrh */ 2141832508b7Sdrh pExpr->iTable = pParse->nTab++; 214271c57db0Sdan addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, 214371c57db0Sdan pExpr->iTable, (isRowid?0:nVal)); 214471c57db0Sdan pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, nVal, 1); 2145e014a838Sdanielk1977 21466ab3a2ecSdanielk1977 if( ExprHasProperty(pExpr, EP_xIsSelect) ){ 2147e014a838Sdanielk1977 /* Case 1: expr IN (SELECT ...) 2148e014a838Sdanielk1977 ** 2149e014a838Sdanielk1977 ** Generate code to write the results of the select into the temporary 2150e014a838Sdanielk1977 ** table allocated and opened above. 2151e014a838Sdanielk1977 */ 21524387006cSdrh Select *pSelect = pExpr->x.pSelect; 215371c57db0Sdan ExprList *pEList = pSelect->pEList; 21541013c932Sdrh 215541a05b7bSdanielk1977 assert( !isRowid ); 215671c57db0Sdan if( pEList->nExpr!=nVal ){ 215771c57db0Sdan sqlite3ErrorMsg(pParse, "SELECT has %d columns - expected %d", 215871c57db0Sdan pEList->nExpr, nVal); 215971c57db0Sdan }else{ 216071c57db0Sdan SelectDest dest; 216171c57db0Sdan int i; 21621013c932Sdrh sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); 216371c57db0Sdan dest.zAffSdst = exprINAffinity(pParse, pExpr); 2164e014a838Sdanielk1977 assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); 21654387006cSdrh pSelect->iLimit = 0; 21664387006cSdrh testcase( pSelect->selFlags & SF_Distinct ); 2167812ea833Sdrh testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */ 21684387006cSdrh if( sqlite3Select(pParse, pSelect, &dest) ){ 216971c57db0Sdan sqlite3DbFree(pParse->db, dest.zAffSdst); 21702ec2fb22Sdrh sqlite3KeyInfoUnref(pKeyInfo); 21711450bc6eSdrh return 0; 217294ccde58Sdrh } 217371c57db0Sdan sqlite3DbFree(pParse->db, dest.zAffSdst); 2174812ea833Sdrh assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */ 21753535ec3eSdrh assert( pEList!=0 ); 21763535ec3eSdrh assert( pEList->nExpr>0 ); 21772ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 217871c57db0Sdan for(i=0; i<nVal; i++){ 217971c57db0Sdan Expr *p = (nVal>1) ? exprVectorField(pLeft, i) : pLeft; 218071c57db0Sdan pKeyInfo->aColl[i] = sqlite3BinaryCompareCollSeq( 218171c57db0Sdan pParse, p, pEList->a[i].pExpr 218271c57db0Sdan ); 218371c57db0Sdan } 218471c57db0Sdan } 2185a7d2db17Sdrh }else if( ALWAYS(pExpr->x.pList!=0) ){ 2186fef5208cSdrh /* Case 2: expr IN (exprlist) 2187fef5208cSdrh ** 2188e014a838Sdanielk1977 ** For each expression, build an index key from the evaluation and 2189e014a838Sdanielk1977 ** store it in the temporary table. If <expr> is a column, then use 2190e014a838Sdanielk1977 ** that columns affinity when building index keys. If <expr> is not 2191e014a838Sdanielk1977 ** a column, use numeric affinity. 2192fef5208cSdrh */ 219371c57db0Sdan char affinity; /* Affinity of the LHS of the IN */ 2194e014a838Sdanielk1977 int i; 21956ab3a2ecSdanielk1977 ExprList *pList = pExpr->x.pList; 219657dbd7b3Sdrh struct ExprList_item *pItem; 2197ecc31805Sdrh int r1, r2, r3; 219857dbd7b3Sdrh 219971c57db0Sdan affinity = sqlite3ExprAffinity(pLeft); 2200e014a838Sdanielk1977 if( !affinity ){ 220105883a34Sdrh affinity = SQLITE_AFF_BLOB; 2202e014a838Sdanielk1977 } 2203323df790Sdrh if( pKeyInfo ){ 22042ec2fb22Sdrh assert( sqlite3KeyInfoIsWriteable(pKeyInfo) ); 2205323df790Sdrh pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2206323df790Sdrh } 2207e014a838Sdanielk1977 2208e014a838Sdanielk1977 /* Loop through each expression in <exprlist>. */ 22092d401ab8Sdrh r1 = sqlite3GetTempReg(pParse); 22102d401ab8Sdrh r2 = sqlite3GetTempReg(pParse); 221137e08081Sdrh if( isRowid ) sqlite3VdbeAddOp2(v, OP_Null, 0, r2); 221257dbd7b3Sdrh for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ 221357dbd7b3Sdrh Expr *pE2 = pItem->pExpr; 2214e05c929bSdrh int iValToIns; 2215e014a838Sdanielk1977 221657dbd7b3Sdrh /* If the expression is not constant then we will need to 221757dbd7b3Sdrh ** disable the test that was generated above that makes sure 221857dbd7b3Sdrh ** this code only executes once. Because for a non-constant 221957dbd7b3Sdrh ** expression we need to rerun this code each time. 222057dbd7b3Sdrh */ 22216be515ebSdrh if( jmpIfDynamic>=0 && !sqlite3ExprIsConstant(pE2) ){ 22226be515ebSdrh sqlite3VdbeChangeToNoop(v, jmpIfDynamic); 22236be515ebSdrh jmpIfDynamic = -1; 22244794b980Sdrh } 2225e014a838Sdanielk1977 2226e014a838Sdanielk1977 /* Evaluate the expression and insert it into the temp table */ 2227e05c929bSdrh if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ 2228e05c929bSdrh sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); 2229e05c929bSdrh }else{ 2230ecc31805Sdrh r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); 223141a05b7bSdanielk1977 if( isRowid ){ 2232e05c929bSdrh sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, 2233e05c929bSdrh sqlite3VdbeCurrentAddr(v)+2); 2234688852abSdrh VdbeCoverage(v); 223541a05b7bSdanielk1977 sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); 223641a05b7bSdanielk1977 }else{ 2237ecc31805Sdrh sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); 22383c31fc23Sdrh sqlite3ExprCacheAffinityChange(pParse, r3, 1); 22392d401ab8Sdrh sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); 2240fef5208cSdrh } 224141a05b7bSdanielk1977 } 2242e05c929bSdrh } 22432d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r1); 22442d401ab8Sdrh sqlite3ReleaseTempReg(pParse, r2); 2245fef5208cSdrh } 2246323df790Sdrh if( pKeyInfo ){ 22472ec2fb22Sdrh sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO); 224841a05b7bSdanielk1977 } 2249b3bce662Sdanielk1977 break; 2250fef5208cSdrh } 2251fef5208cSdrh 225251522cd3Sdrh case TK_EXISTS: 2253fd773cf9Sdrh case TK_SELECT: 2254fd773cf9Sdrh default: { 2255fd773cf9Sdrh /* If this has to be a scalar SELECT. Generate code to put the 2256fef5208cSdrh ** value of this select in a memory cell and record the number 2257fd773cf9Sdrh ** of the memory cell in iColumn. If this is an EXISTS, write 2258fd773cf9Sdrh ** an integer 0 (not exists) or 1 (exists) into a memory cell 2259fd773cf9Sdrh ** and record that memory cell in iColumn. 2260fef5208cSdrh */ 2261fd773cf9Sdrh Select *pSel; /* SELECT statement to encode */ 2262fd773cf9Sdrh SelectDest dest; /* How to deal with SELECt result */ 226371c57db0Sdan int nReg; /* Registers to allocate */ 22641398ad36Sdrh 2265cf697396Sshane testcase( pExpr->op==TK_EXISTS ); 2266cf697396Sshane testcase( pExpr->op==TK_SELECT ); 2267cf697396Sshane assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); 22686ab3a2ecSdanielk1977 assert( ExprHasProperty(pExpr, EP_xIsSelect) ); 226971c57db0Sdan 22706ab3a2ecSdanielk1977 pSel = pExpr->x.pSelect; 227171c57db0Sdan nReg = pExpr->op==TK_SELECT ? pSel->pEList->nExpr : 1; 227271c57db0Sdan sqlite3SelectDestInit(&dest, 0, pParse->nMem+1); 227371c57db0Sdan pParse->nMem += nReg; 227451522cd3Sdrh if( pExpr->op==TK_SELECT ){ 22756c8c8ce0Sdanielk1977 dest.eDest = SRT_Mem; 227653932ce8Sdrh dest.iSdst = dest.iSDParm; 227771c57db0Sdan dest.nSdst = nReg; 227871c57db0Sdan sqlite3VdbeAddOp3(v, OP_Null, 0, dest.iSDParm, dest.iSDParm+nReg-1); 2279d4e70ebdSdrh VdbeComment((v, "Init subquery result")); 228051522cd3Sdrh }else{ 22816c8c8ce0Sdanielk1977 dest.eDest = SRT_Exists; 22822b596da8Sdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm); 2283d4e70ebdSdrh VdbeComment((v, "Init EXISTS result")); 228451522cd3Sdrh } 2285633e6d57Sdrh sqlite3ExprDelete(pParse->db, pSel->pLimit); 2286094430ebSdrh pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, 2287094430ebSdrh &sqlite3IntTokens[1]); 228848b5b041Sdrh pSel->iLimit = 0; 2289772460fdSdrh pSel->selFlags &= ~SF_MultiValue; 22907d10d5a6Sdrh if( sqlite3Select(pParse, pSel, &dest) ){ 22911450bc6eSdrh return 0; 229294ccde58Sdrh } 22932b596da8Sdrh rReg = dest.iSDParm; 2294ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 2295b3bce662Sdanielk1977 break; 229619a775c2Sdrh } 2297cce7d176Sdrh } 2298b3bce662Sdanielk1977 22996be515ebSdrh if( rHasNullFlag ){ 23006be515ebSdrh sqlite3SetHasNullFlag(v, pExpr->iTable, rHasNullFlag); 2301b3bce662Sdanielk1977 } 23026be515ebSdrh 23036be515ebSdrh if( jmpIfDynamic>=0 ){ 23046be515ebSdrh sqlite3VdbeJumpHere(v, jmpIfDynamic); 2305b3bce662Sdanielk1977 } 2306d2490904Sdrh sqlite3ExprCachePop(pParse); 2307fc976065Sdanielk1977 23081450bc6eSdrh return rReg; 2309cce7d176Sdrh } 231051522cd3Sdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2311cce7d176Sdrh 2312e3365e6cSdrh #ifndef SQLITE_OMIT_SUBQUERY 231371c57db0Sdan void exprCodeVectorIN( 231471c57db0Sdan Parse *pParse, /* Parsing and code generating context */ 231571c57db0Sdan Expr *pExpr, /* The IN expression */ 231671c57db0Sdan int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 231771c57db0Sdan int destIfNull /* Jump here if the results are unknown due to NULLs */ 231871c57db0Sdan ){ 231971c57db0Sdan int i; 232071c57db0Sdan int addrNext; 232171c57db0Sdan int iSkip; 232271c57db0Sdan int r1; 232371c57db0Sdan int r2 = sqlite3GetTempReg(pParse); 232471c57db0Sdan int r3 = sqlite3GetTempReg(pParse); 232571c57db0Sdan int r4 = sqlite3GetTempReg(pParse); 232671c57db0Sdan int regResult = sqlite3GetTempReg(pParse); 232771c57db0Sdan int nVal = sqlite3ExprVectorSize(pExpr->pLeft); 232871c57db0Sdan 232971c57db0Sdan Expr *pLeft = pExpr->pLeft; 233071c57db0Sdan Vdbe *v = pParse->pVdbe; 233171c57db0Sdan 233271c57db0Sdan /* Code the LHS, the <expr> from "<expr> IN (...)". Leave the results in 233371c57db0Sdan ** an array of nVal registers starting at r1. */ 233471c57db0Sdan sqlite3ExprCachePush(pParse); 233571c57db0Sdan if( pLeft->flags & EP_xIsSelect ){ 233671c57db0Sdan r1 = sqlite3CodeSubselect(pParse, pLeft, 0, 0); 233771c57db0Sdan }else{ 233871c57db0Sdan r1 = pParse->nMem + 1; 233971c57db0Sdan pParse->nMem += nVal; 234071c57db0Sdan sqlite3ExprCodeExprList(pParse, pLeft->x.pList, r1, 0, 0); 234171c57db0Sdan } 234271c57db0Sdan 234371c57db0Sdan /* Generate an epheremal index containing the contents of the SELECT 234471c57db0Sdan ** to the right of the "<expr> IN (SELECT ...)" expression. The cursor 234571c57db0Sdan ** number for the epheremal table is left in pExpr->iTable. */ 234671c57db0Sdan assert( pExpr->flags & EP_xIsSelect ); 234771c57db0Sdan sqlite3CodeSubselect(pParse, pExpr, 0, 0); 234871c57db0Sdan 234971c57db0Sdan sqlite3VdbeAddOp2(v, OP_Integer, 0, regResult); 235071c57db0Sdan 235171c57db0Sdan /* Iterate through the ephemeral table just populated */ 235271c57db0Sdan addrNext = 1 + sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 235371c57db0Sdan for(i=0; i<nVal; i++){ 235471c57db0Sdan Expr *p; 235571c57db0Sdan CollSeq *pColl; 235671c57db0Sdan p = exprVectorField(pLeft, i); 235771c57db0Sdan pColl = sqlite3ExprCollSeq(pParse, p); 235871c57db0Sdan sqlite3VdbeAddOp3(v, OP_Column, pExpr->iTable, i, r2); 235971c57db0Sdan sqlite3VdbeAddOp4(v, OP_Eq, r1+i, i==0?r3:r4, r2, (void*)pColl,P4_COLLSEQ); 236071c57db0Sdan sqlite3VdbeChangeP5(v, SQLITE_STOREP2); 236171c57db0Sdan VdbeCoverage(v); 236271c57db0Sdan if( i!=0 ){ 236371c57db0Sdan sqlite3VdbeAddOp3(v, OP_And, r3, r4, r4); 236471c57db0Sdan } 236571c57db0Sdan } 236671c57db0Sdan sqlite3VdbeAddOp2(v, OP_If, r4, sqlite3VdbeCurrentAddr(v)+6); 236771c57db0Sdan sqlite3VdbeAddOp2(v, OP_IfNot, r4, sqlite3VdbeCurrentAddr(v)+2); 236871c57db0Sdan sqlite3VdbeAddOp2(v, OP_Null, 0, regResult); 236971c57db0Sdan sqlite3VdbeAddOp2(v, OP_Next, pExpr->iTable, addrNext); 237071c57db0Sdan sqlite3VdbeAddOp3(v, OP_If, regResult, destIfNull, 1); 237171c57db0Sdan sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); 237271c57db0Sdan 237371c57db0Sdan sqlite3ReleaseTempReg(pParse, r2); 237471c57db0Sdan sqlite3ReleaseTempReg(pParse, r3); 237571c57db0Sdan sqlite3ReleaseTempReg(pParse, r4); 237671c57db0Sdan sqlite3ReleaseTempReg(pParse, regResult); 237771c57db0Sdan sqlite3ExprCachePop(pParse); 237871c57db0Sdan } 237971c57db0Sdan #endif 238071c57db0Sdan 238171c57db0Sdan #ifndef SQLITE_OMIT_SUBQUERY 2382e3365e6cSdrh /* 2383e3365e6cSdrh ** Generate code for an IN expression. 2384e3365e6cSdrh ** 2385e3365e6cSdrh ** x IN (SELECT ...) 2386e3365e6cSdrh ** x IN (value, value, ...) 2387e3365e6cSdrh ** 2388e3365e6cSdrh ** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) 2389e3365e6cSdrh ** is an array of zero or more values. The expression is true if the LHS is 2390e3365e6cSdrh ** contained within the RHS. The value of the expression is unknown (NULL) 2391e3365e6cSdrh ** if the LHS is NULL or if the LHS is not contained within the RHS and the 2392e3365e6cSdrh ** RHS contains one or more NULL values. 2393e3365e6cSdrh ** 23946be515ebSdrh ** This routine generates code that jumps to destIfFalse if the LHS is not 2395e3365e6cSdrh ** contained within the RHS. If due to NULLs we cannot determine if the LHS 2396e3365e6cSdrh ** is contained in the RHS then jump to destIfNull. If the LHS is contained 2397e3365e6cSdrh ** within the RHS then fall through. 2398e3365e6cSdrh */ 2399e3365e6cSdrh static void sqlite3ExprCodeIN( 2400e3365e6cSdrh Parse *pParse, /* Parsing and code generating context */ 2401e3365e6cSdrh Expr *pExpr, /* The IN expression */ 2402e3365e6cSdrh int destIfFalse, /* Jump here if LHS is not contained in the RHS */ 2403e3365e6cSdrh int destIfNull /* Jump here if the results are unknown due to NULLs */ 2404e3365e6cSdrh ){ 2405e3365e6cSdrh int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ 2406e3365e6cSdrh char affinity; /* Comparison affinity to use */ 2407e3365e6cSdrh int eType; /* Type of the RHS */ 2408e3365e6cSdrh int r1; /* Temporary use register */ 2409e3365e6cSdrh Vdbe *v; /* Statement under construction */ 2410e3365e6cSdrh 241171c57db0Sdan if( pExpr->pLeft->flags & EP_Vector ){ 241271c57db0Sdan return exprCodeVectorIN(pParse, pExpr, destIfFalse, destIfNull); 241371c57db0Sdan } 241471c57db0Sdan 2415e3365e6cSdrh /* Compute the RHS. After this step, the table with cursor 2416e3365e6cSdrh ** pExpr->iTable will contains the values that make up the RHS. 2417e3365e6cSdrh */ 2418e3365e6cSdrh v = pParse->pVdbe; 2419e3365e6cSdrh assert( v!=0 ); /* OOM detected prior to this routine */ 2420e3365e6cSdrh VdbeNoopComment((v, "begin IN expr")); 2421bb53ecb1Sdrh eType = sqlite3FindInIndex(pParse, pExpr, 2422bb53ecb1Sdrh IN_INDEX_MEMBERSHIP | IN_INDEX_NOOP_OK, 24233a85625dSdrh destIfFalse==destIfNull ? 0 : &rRhsHasNull); 2424e3365e6cSdrh 2425e3365e6cSdrh /* Figure out the affinity to use to create a key from the results 2426e3365e6cSdrh ** of the expression. affinityStr stores a static string suitable for 2427e3365e6cSdrh ** P4 of OP_MakeRecord. 2428e3365e6cSdrh */ 2429e3365e6cSdrh affinity = comparisonAffinity(pExpr); 2430e3365e6cSdrh 2431e3365e6cSdrh /* Code the LHS, the <expr> from "<expr> IN (...)". 2432e3365e6cSdrh */ 2433e3365e6cSdrh sqlite3ExprCachePush(pParse); 2434e3365e6cSdrh r1 = sqlite3GetTempReg(pParse); 2435e3365e6cSdrh sqlite3ExprCode(pParse, pExpr->pLeft, r1); 2436e3365e6cSdrh 2437bb53ecb1Sdrh /* If sqlite3FindInIndex() did not find or create an index that is 2438bb53ecb1Sdrh ** suitable for evaluating the IN operator, then evaluate using a 2439bb53ecb1Sdrh ** sequence of comparisons. 2440bb53ecb1Sdrh */ 2441bb53ecb1Sdrh if( eType==IN_INDEX_NOOP ){ 2442bb53ecb1Sdrh ExprList *pList = pExpr->x.pList; 2443bb53ecb1Sdrh CollSeq *pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); 2444bb53ecb1Sdrh int labelOk = sqlite3VdbeMakeLabel(v); 2445bb53ecb1Sdrh int r2, regToFree; 2446bb53ecb1Sdrh int regCkNull = 0; 2447bb53ecb1Sdrh int ii; 2448bb53ecb1Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 2449bb53ecb1Sdrh if( destIfNull!=destIfFalse ){ 2450bb53ecb1Sdrh regCkNull = sqlite3GetTempReg(pParse); 2451a976979bSdrh sqlite3VdbeAddOp3(v, OP_BitAnd, r1, r1, regCkNull); 2452bb53ecb1Sdrh } 2453bb53ecb1Sdrh for(ii=0; ii<pList->nExpr; ii++){ 2454bb53ecb1Sdrh r2 = sqlite3ExprCodeTemp(pParse, pList->a[ii].pExpr, ®ToFree); 2455a976979bSdrh if( regCkNull && sqlite3ExprCanBeNull(pList->a[ii].pExpr) ){ 2456bb53ecb1Sdrh sqlite3VdbeAddOp3(v, OP_BitAnd, regCkNull, r2, regCkNull); 2457bb53ecb1Sdrh } 2458bb53ecb1Sdrh if( ii<pList->nExpr-1 || destIfNull!=destIfFalse ){ 2459bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Eq, r1, labelOk, r2, 24604336b0e6Sdrh (void*)pColl, P4_COLLSEQ); 24614336b0e6Sdrh VdbeCoverageIf(v, ii<pList->nExpr-1); 24624336b0e6Sdrh VdbeCoverageIf(v, ii==pList->nExpr-1); 2463bb53ecb1Sdrh sqlite3VdbeChangeP5(v, affinity); 2464bb53ecb1Sdrh }else{ 2465bb53ecb1Sdrh assert( destIfNull==destIfFalse ); 2466bb53ecb1Sdrh sqlite3VdbeAddOp4(v, OP_Ne, r1, destIfFalse, r2, 2467bb53ecb1Sdrh (void*)pColl, P4_COLLSEQ); VdbeCoverage(v); 2468bb53ecb1Sdrh sqlite3VdbeChangeP5(v, affinity | SQLITE_JUMPIFNULL); 2469bb53ecb1Sdrh } 2470bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regToFree); 2471bb53ecb1Sdrh } 2472bb53ecb1Sdrh if( regCkNull ){ 2473bb53ecb1Sdrh sqlite3VdbeAddOp2(v, OP_IsNull, regCkNull, destIfNull); VdbeCoverage(v); 2474076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2475bb53ecb1Sdrh } 2476bb53ecb1Sdrh sqlite3VdbeResolveLabel(v, labelOk); 2477bb53ecb1Sdrh sqlite3ReleaseTempReg(pParse, regCkNull); 2478bb53ecb1Sdrh }else{ 2479bb53ecb1Sdrh 2480094430ebSdrh /* If the LHS is NULL, then the result is either false or NULL depending 2481094430ebSdrh ** on whether the RHS is empty or not, respectively. 2482094430ebSdrh */ 24837248a8b2Sdrh if( sqlite3ExprCanBeNull(pExpr->pLeft) ){ 2484094430ebSdrh if( destIfNull==destIfFalse ){ 2485094430ebSdrh /* Shortcut for the common case where the false and NULL outcomes are 2486094430ebSdrh ** the same. */ 2487688852abSdrh sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v); 2488094430ebSdrh }else{ 2489688852abSdrh int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v); 2490094430ebSdrh sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); 2491688852abSdrh VdbeCoverage(v); 2492076e85f5Sdrh sqlite3VdbeGoto(v, destIfNull); 2493094430ebSdrh sqlite3VdbeJumpHere(v, addr1); 2494094430ebSdrh } 24957248a8b2Sdrh } 2496e3365e6cSdrh 2497e3365e6cSdrh if( eType==IN_INDEX_ROWID ){ 2498e3365e6cSdrh /* In this case, the RHS is the ROWID of table b-tree 2499e3365e6cSdrh */ 2500eeb9565aSdrh sqlite3VdbeAddOp3(v, OP_SeekRowid, pExpr->iTable, destIfFalse, r1); 2501688852abSdrh VdbeCoverage(v); 2502e3365e6cSdrh }else{ 2503e3365e6cSdrh /* In this case, the RHS is an index b-tree. 2504e3365e6cSdrh */ 25058cff69dfSdrh sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); 2506e3365e6cSdrh 2507e3365e6cSdrh /* If the set membership test fails, then the result of the 2508e3365e6cSdrh ** "x IN (...)" expression must be either 0 or NULL. If the set 2509e3365e6cSdrh ** contains no NULL values, then the result is 0. If the set 2510e3365e6cSdrh ** contains one or more NULL values, then the result of the 2511e3365e6cSdrh ** expression is also NULL. 2512e3365e6cSdrh */ 2513e80c9b9aSdrh assert( destIfFalse!=destIfNull || rRhsHasNull==0 ); 2514e80c9b9aSdrh if( rRhsHasNull==0 ){ 2515e3365e6cSdrh /* This branch runs if it is known at compile time that the RHS 2516e3365e6cSdrh ** cannot contain NULL values. This happens as the result 2517e3365e6cSdrh ** of a "NOT NULL" constraint in the database schema. 2518e3365e6cSdrh ** 2519e3365e6cSdrh ** Also run this branch if NULL is equivalent to FALSE 2520e3365e6cSdrh ** for this particular IN operator. 2521e3365e6cSdrh */ 25228cff69dfSdrh sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); 2523688852abSdrh VdbeCoverage(v); 2524e3365e6cSdrh }else{ 2525e3365e6cSdrh /* In this branch, the RHS of the IN might contain a NULL and 2526e3365e6cSdrh ** the presence of a NULL on the RHS makes a difference in the 2527e3365e6cSdrh ** outcome. 2528e3365e6cSdrh */ 2529728e0f91Sdrh int addr1; 2530e3365e6cSdrh 2531e3365e6cSdrh /* First check to see if the LHS is contained in the RHS. If so, 25326be515ebSdrh ** then the answer is TRUE the presence of NULLs in the RHS does 25336be515ebSdrh ** not matter. If the LHS is not contained in the RHS, then the 25346be515ebSdrh ** answer is NULL if the RHS contains NULLs and the answer is 25356be515ebSdrh ** FALSE if the RHS is NULL-free. 2536e3365e6cSdrh */ 2537728e0f91Sdrh addr1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); 2538688852abSdrh VdbeCoverage(v); 25396be515ebSdrh sqlite3VdbeAddOp2(v, OP_IsNull, rRhsHasNull, destIfNull); 2540552fd454Sdrh VdbeCoverage(v); 2541076e85f5Sdrh sqlite3VdbeGoto(v, destIfFalse); 2542728e0f91Sdrh sqlite3VdbeJumpHere(v, addr1); 2543e3365e6cSdrh } 2544e3365e6cSdrh } 2545bb53ecb1Sdrh } 2546e3365e6cSdrh sqlite3ReleaseTempReg(pParse, r1); 2547d2490904Sdrh sqlite3ExprCachePop(pParse); 2548e3365e6cSdrh VdbeComment((v, "end IN expr")); 2549e3365e6cSdrh } 2550e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 2551e3365e6cSdrh 255213573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 2553598f1340Sdrh /* 2554598f1340Sdrh ** Generate an instruction that will put the floating point 25559cbf3425Sdrh ** value described by z[0..n-1] into register iMem. 25560cf19ed8Sdrh ** 25570cf19ed8Sdrh ** The z[] string will probably not be zero-terminated. But the 25580cf19ed8Sdrh ** z[n] character is guaranteed to be something that does not look 25590cf19ed8Sdrh ** like the continuation of the number. 2560598f1340Sdrh */ 2561b7916a78Sdrh static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ 2562fd773cf9Sdrh if( ALWAYS(z!=0) ){ 2563598f1340Sdrh double value; 25649339da1fSdrh sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); 2565d0015161Sdrh assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ 2566598f1340Sdrh if( negateFlag ) value = -value; 256797bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Real, 0, iMem, 0, (u8*)&value, P4_REAL); 2568598f1340Sdrh } 2569598f1340Sdrh } 257013573c71Sdrh #endif 2571598f1340Sdrh 2572598f1340Sdrh 2573598f1340Sdrh /* 2574fec19aadSdrh ** Generate an instruction that will put the integer describe by 25759cbf3425Sdrh ** text z[0..n-1] into register iMem. 25760cf19ed8Sdrh ** 25775f1d6b61Sshaneh ** Expr.u.zToken is always UTF8 and zero-terminated. 2578fec19aadSdrh */ 257913573c71Sdrh static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ 258013573c71Sdrh Vdbe *v = pParse->pVdbe; 258192b01d53Sdrh if( pExpr->flags & EP_IntValue ){ 258233e619fcSdrh int i = pExpr->u.iValue; 2583d50ffc41Sdrh assert( i>=0 ); 258492b01d53Sdrh if( negFlag ) i = -i; 258592b01d53Sdrh sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); 2586fd773cf9Sdrh }else{ 25875f1d6b61Sshaneh int c; 25885f1d6b61Sshaneh i64 value; 2589fd773cf9Sdrh const char *z = pExpr->u.zToken; 2590fd773cf9Sdrh assert( z!=0 ); 25919296c18aSdrh c = sqlite3DecOrHexToI64(z, &value); 25925f1d6b61Sshaneh if( c==0 || (c==2 && negFlag) ){ 2593158b9cb9Sdrh if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } 259497bae794Sdrh sqlite3VdbeAddOp4Dup8(v, OP_Int64, 0, iMem, 0, (u8*)&value, P4_INT64); 2595fec19aadSdrh }else{ 259613573c71Sdrh #ifdef SQLITE_OMIT_FLOATING_POINT 259713573c71Sdrh sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); 259813573c71Sdrh #else 25991b7ddc59Sdrh #ifndef SQLITE_OMIT_HEX_INTEGER 26009296c18aSdrh if( sqlite3_strnicmp(z,"0x",2)==0 ){ 26019296c18aSdrh sqlite3ErrorMsg(pParse, "hex literal too big: %s", z); 26021b7ddc59Sdrh }else 26031b7ddc59Sdrh #endif 26041b7ddc59Sdrh { 2605b7916a78Sdrh codeReal(v, z, negFlag, iMem); 26069296c18aSdrh } 260713573c71Sdrh #endif 2608fec19aadSdrh } 2609fec19aadSdrh } 2610c9cf901dSdanielk1977 } 2611fec19aadSdrh 2612bea119cdSdrh #if defined(SQLITE_DEBUG) 2613bea119cdSdrh /* 2614bea119cdSdrh ** Verify the consistency of the column cache 2615bea119cdSdrh */ 2616bea119cdSdrh static int cacheIsValid(Parse *pParse){ 2617bea119cdSdrh int i, n; 2618bea119cdSdrh for(i=n=0; i<SQLITE_N_COLCACHE; i++){ 2619bea119cdSdrh if( pParse->aColCache[i].iReg>0 ) n++; 2620bea119cdSdrh } 2621bea119cdSdrh return n==pParse->nColCache; 2622bea119cdSdrh } 2623bea119cdSdrh #endif 2624bea119cdSdrh 2625ceea3321Sdrh /* 2626ceea3321Sdrh ** Clear a cache entry. 2627ceea3321Sdrh */ 2628ceea3321Sdrh static void cacheEntryClear(Parse *pParse, struct yColCache *p){ 2629ceea3321Sdrh if( p->tempReg ){ 2630ceea3321Sdrh if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 2631ceea3321Sdrh pParse->aTempReg[pParse->nTempReg++] = p->iReg; 2632ceea3321Sdrh } 2633ceea3321Sdrh p->tempReg = 0; 2634ceea3321Sdrh } 2635bea119cdSdrh p->iReg = 0; 2636bea119cdSdrh pParse->nColCache--; 2637ee65eea4Sdan assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); 2638ceea3321Sdrh } 2639ceea3321Sdrh 2640ceea3321Sdrh 2641ceea3321Sdrh /* 2642ceea3321Sdrh ** Record in the column cache that a particular column from a 2643ceea3321Sdrh ** particular table is stored in a particular register. 2644ceea3321Sdrh */ 2645ceea3321Sdrh void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ 2646ceea3321Sdrh int i; 2647ceea3321Sdrh int minLru; 2648ceea3321Sdrh int idxLru; 2649ceea3321Sdrh struct yColCache *p; 2650ceea3321Sdrh 2651ce8f53d4Sdan /* Unless an error has occurred, register numbers are always positive. */ 2652ce8f53d4Sdan assert( iReg>0 || pParse->nErr || pParse->db->mallocFailed ); 265320411ea7Sdrh assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ 265420411ea7Sdrh 2655b6da74ebSdrh /* The SQLITE_ColumnCache flag disables the column cache. This is used 2656b6da74ebSdrh ** for testing only - to verify that SQLite always gets the same answer 2657b6da74ebSdrh ** with and without the column cache. 2658b6da74ebSdrh */ 26597e5418e4Sdrh if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return; 2660b6da74ebSdrh 266127ee406eSdrh /* First replace any existing entry. 266227ee406eSdrh ** 266327ee406eSdrh ** Actually, the way the column cache is currently used, we are guaranteed 266427ee406eSdrh ** that the object will never already be in cache. Verify this guarantee. 266527ee406eSdrh */ 266627ee406eSdrh #ifndef NDEBUG 2667ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 266827ee406eSdrh assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); 2669ceea3321Sdrh } 267027ee406eSdrh #endif 2671ceea3321Sdrh 2672ceea3321Sdrh /* Find an empty slot and replace it */ 2673ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2674ceea3321Sdrh if( p->iReg==0 ){ 2675ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2676ceea3321Sdrh p->iTable = iTab; 2677ceea3321Sdrh p->iColumn = iCol; 2678ceea3321Sdrh p->iReg = iReg; 2679ceea3321Sdrh p->tempReg = 0; 2680ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2681bea119cdSdrh pParse->nColCache++; 2682ee65eea4Sdan assert( pParse->db->mallocFailed || cacheIsValid(pParse) ); 2683ceea3321Sdrh return; 2684ceea3321Sdrh } 2685ceea3321Sdrh } 2686ceea3321Sdrh 2687ceea3321Sdrh /* Replace the last recently used */ 2688ceea3321Sdrh minLru = 0x7fffffff; 2689ceea3321Sdrh idxLru = -1; 2690ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2691ceea3321Sdrh if( p->lru<minLru ){ 2692ceea3321Sdrh idxLru = i; 2693ceea3321Sdrh minLru = p->lru; 2694ceea3321Sdrh } 2695ceea3321Sdrh } 269620411ea7Sdrh if( ALWAYS(idxLru>=0) ){ 2697ceea3321Sdrh p = &pParse->aColCache[idxLru]; 2698ceea3321Sdrh p->iLevel = pParse->iCacheLevel; 2699ceea3321Sdrh p->iTable = iTab; 2700ceea3321Sdrh p->iColumn = iCol; 2701ceea3321Sdrh p->iReg = iReg; 2702ceea3321Sdrh p->tempReg = 0; 2703ceea3321Sdrh p->lru = pParse->iCacheCnt++; 2704bea119cdSdrh assert( cacheIsValid(pParse) ); 2705ceea3321Sdrh return; 2706ceea3321Sdrh } 2707ceea3321Sdrh } 2708ceea3321Sdrh 2709ceea3321Sdrh /* 2710f49f3523Sdrh ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. 2711f49f3523Sdrh ** Purge the range of registers from the column cache. 2712ceea3321Sdrh */ 2713f49f3523Sdrh void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ 2714ceea3321Sdrh struct yColCache *p; 2715bea119cdSdrh if( iReg<=0 || pParse->nColCache==0 ) return; 2716bea119cdSdrh p = &pParse->aColCache[SQLITE_N_COLCACHE-1]; 2717bea119cdSdrh while(1){ 2718bea119cdSdrh if( p->iReg >= iReg && p->iReg < iReg+nReg ) cacheEntryClear(pParse, p); 2719bea119cdSdrh if( p==pParse->aColCache ) break; 2720bea119cdSdrh p--; 2721ceea3321Sdrh } 2722ceea3321Sdrh } 2723ceea3321Sdrh 2724ceea3321Sdrh /* 2725ceea3321Sdrh ** Remember the current column cache context. Any new entries added 2726ceea3321Sdrh ** added to the column cache after this call are removed when the 2727ceea3321Sdrh ** corresponding pop occurs. 2728ceea3321Sdrh */ 2729ceea3321Sdrh void sqlite3ExprCachePush(Parse *pParse){ 2730ceea3321Sdrh pParse->iCacheLevel++; 27319ac7962aSdrh #ifdef SQLITE_DEBUG 27329ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 27339ac7962aSdrh printf("PUSH to %d\n", pParse->iCacheLevel); 27349ac7962aSdrh } 27359ac7962aSdrh #endif 2736ceea3321Sdrh } 2737ceea3321Sdrh 2738ceea3321Sdrh /* 2739ceea3321Sdrh ** Remove from the column cache any entries that were added since the 2740d2490904Sdrh ** the previous sqlite3ExprCachePush operation. In other words, restore 2741d2490904Sdrh ** the cache to the state it was in prior the most recent Push. 2742ceea3321Sdrh */ 2743d2490904Sdrh void sqlite3ExprCachePop(Parse *pParse){ 2744ceea3321Sdrh int i; 2745ceea3321Sdrh struct yColCache *p; 2746d2490904Sdrh assert( pParse->iCacheLevel>=1 ); 2747d2490904Sdrh pParse->iCacheLevel--; 27489ac7962aSdrh #ifdef SQLITE_DEBUG 27499ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 27509ac7962aSdrh printf("POP to %d\n", pParse->iCacheLevel); 27519ac7962aSdrh } 27529ac7962aSdrh #endif 2753ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2754ceea3321Sdrh if( p->iReg && p->iLevel>pParse->iCacheLevel ){ 2755ceea3321Sdrh cacheEntryClear(pParse, p); 2756ceea3321Sdrh } 2757ceea3321Sdrh } 2758ceea3321Sdrh } 2759945498f3Sdrh 2760945498f3Sdrh /* 27615cd79239Sdrh ** When a cached column is reused, make sure that its register is 27625cd79239Sdrh ** no longer available as a temp register. ticket #3879: that same 27635cd79239Sdrh ** register might be in the cache in multiple places, so be sure to 27645cd79239Sdrh ** get them all. 27655cd79239Sdrh */ 27665cd79239Sdrh static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ 27675cd79239Sdrh int i; 27685cd79239Sdrh struct yColCache *p; 27695cd79239Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 27705cd79239Sdrh if( p->iReg==iReg ){ 27715cd79239Sdrh p->tempReg = 0; 27725cd79239Sdrh } 27735cd79239Sdrh } 27745cd79239Sdrh } 27755cd79239Sdrh 27761f9ca2c8Sdrh /* Generate code that will load into register regOut a value that is 27771f9ca2c8Sdrh ** appropriate for the iIdxCol-th column of index pIdx. 27781f9ca2c8Sdrh */ 27791f9ca2c8Sdrh void sqlite3ExprCodeLoadIndexColumn( 27801f9ca2c8Sdrh Parse *pParse, /* The parsing context */ 27811f9ca2c8Sdrh Index *pIdx, /* The index whose column is to be loaded */ 27821f9ca2c8Sdrh int iTabCur, /* Cursor pointing to a table row */ 27831f9ca2c8Sdrh int iIdxCol, /* The column of the index to be loaded */ 27841f9ca2c8Sdrh int regOut /* Store the index column value in this register */ 27851f9ca2c8Sdrh ){ 27861f9ca2c8Sdrh i16 iTabCol = pIdx->aiColumn[iIdxCol]; 27874b92f98cSdrh if( iTabCol==XN_EXPR ){ 27881f9ca2c8Sdrh assert( pIdx->aColExpr ); 27891f9ca2c8Sdrh assert( pIdx->aColExpr->nExpr>iIdxCol ); 27901f9ca2c8Sdrh pParse->iSelfTab = iTabCur; 27911c75c9d7Sdrh sqlite3ExprCodeCopy(pParse, pIdx->aColExpr->a[iIdxCol].pExpr, regOut); 27924b92f98cSdrh }else{ 27934b92f98cSdrh sqlite3ExprCodeGetColumnOfTable(pParse->pVdbe, pIdx->pTable, iTabCur, 27944b92f98cSdrh iTabCol, regOut); 27954b92f98cSdrh } 27961f9ca2c8Sdrh } 27971f9ca2c8Sdrh 27985cd79239Sdrh /* 27995c092e8aSdrh ** Generate code to extract the value of the iCol-th column of a table. 28005c092e8aSdrh */ 28015c092e8aSdrh void sqlite3ExprCodeGetColumnOfTable( 28025c092e8aSdrh Vdbe *v, /* The VDBE under construction */ 28035c092e8aSdrh Table *pTab, /* The table containing the value */ 2804313619f5Sdrh int iTabCur, /* The table cursor. Or the PK cursor for WITHOUT ROWID */ 28055c092e8aSdrh int iCol, /* Index of the column to extract */ 2806313619f5Sdrh int regOut /* Extract the value into this register */ 28075c092e8aSdrh ){ 28085c092e8aSdrh if( iCol<0 || iCol==pTab->iPKey ){ 28095c092e8aSdrh sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); 28105c092e8aSdrh }else{ 28115c092e8aSdrh int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; 2812ee0ec8e1Sdrh int x = iCol; 281335db31b2Sdrh if( !HasRowid(pTab) && !IsVirtual(pTab) ){ 2814ee0ec8e1Sdrh x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol); 2815ee0ec8e1Sdrh } 2816ee0ec8e1Sdrh sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut); 28175c092e8aSdrh } 28185c092e8aSdrh if( iCol>=0 ){ 28195c092e8aSdrh sqlite3ColumnDefault(v, pTab, iCol, regOut); 28205c092e8aSdrh } 28215c092e8aSdrh } 28225c092e8aSdrh 28235c092e8aSdrh /* 2824945498f3Sdrh ** Generate code that will extract the iColumn-th column from 2825ce78bc6eSdrh ** table pTab and store the column value in a register. 2826ce78bc6eSdrh ** 2827ce78bc6eSdrh ** An effort is made to store the column value in register iReg. This 2828ce78bc6eSdrh ** is not garanteeed for GetColumn() - the result can be stored in 2829ce78bc6eSdrh ** any register. But the result is guaranteed to land in register iReg 2830ce78bc6eSdrh ** for GetColumnToReg(). 2831e55cbd72Sdrh ** 2832e55cbd72Sdrh ** There must be an open cursor to pTab in iTable when this routine 2833e55cbd72Sdrh ** is called. If iColumn<0 then code is generated that extracts the rowid. 2834945498f3Sdrh */ 2835e55cbd72Sdrh int sqlite3ExprCodeGetColumn( 2836e55cbd72Sdrh Parse *pParse, /* Parsing and code generating context */ 28372133d822Sdrh Table *pTab, /* Description of the table we are reading from */ 28382133d822Sdrh int iColumn, /* Index of the table column */ 28392133d822Sdrh int iTable, /* The cursor pointing to the table */ 2840a748fdccSdrh int iReg, /* Store results here */ 2841ce78bc6eSdrh u8 p5 /* P5 value for OP_Column + FLAGS */ 28422133d822Sdrh ){ 2843e55cbd72Sdrh Vdbe *v = pParse->pVdbe; 2844e55cbd72Sdrh int i; 2845da250ea5Sdrh struct yColCache *p; 2846e55cbd72Sdrh 2847ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2848b6da74ebSdrh if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ 2849ceea3321Sdrh p->lru = pParse->iCacheCnt++; 28505cd79239Sdrh sqlite3ExprCachePinRegister(pParse, p->iReg); 2851da250ea5Sdrh return p->iReg; 2852e55cbd72Sdrh } 2853e55cbd72Sdrh } 2854e55cbd72Sdrh assert( v!=0 ); 28555c092e8aSdrh sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); 2856a748fdccSdrh if( p5 ){ 2857a748fdccSdrh sqlite3VdbeChangeP5(v, p5); 2858a748fdccSdrh }else{ 2859ceea3321Sdrh sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); 2860a748fdccSdrh } 2861e55cbd72Sdrh return iReg; 2862e55cbd72Sdrh } 2863ce78bc6eSdrh void sqlite3ExprCodeGetColumnToReg( 2864ce78bc6eSdrh Parse *pParse, /* Parsing and code generating context */ 2865ce78bc6eSdrh Table *pTab, /* Description of the table we are reading from */ 2866ce78bc6eSdrh int iColumn, /* Index of the table column */ 2867ce78bc6eSdrh int iTable, /* The cursor pointing to the table */ 2868ce78bc6eSdrh int iReg /* Store results here */ 2869ce78bc6eSdrh ){ 2870ce78bc6eSdrh int r1 = sqlite3ExprCodeGetColumn(pParse, pTab, iColumn, iTable, iReg, 0); 2871ce78bc6eSdrh if( r1!=iReg ) sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, r1, iReg); 2872ce78bc6eSdrh } 2873ce78bc6eSdrh 2874e55cbd72Sdrh 2875e55cbd72Sdrh /* 2876ceea3321Sdrh ** Clear all column cache entries. 2877e55cbd72Sdrh */ 2878ceea3321Sdrh void sqlite3ExprCacheClear(Parse *pParse){ 2879e55cbd72Sdrh int i; 2880ceea3321Sdrh struct yColCache *p; 2881ceea3321Sdrh 28829ac7962aSdrh #if SQLITE_DEBUG 28839ac7962aSdrh if( pParse->db->flags & SQLITE_VdbeAddopTrace ){ 28849ac7962aSdrh printf("CLEAR\n"); 28859ac7962aSdrh } 28869ac7962aSdrh #endif 2887ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2888ceea3321Sdrh if( p->iReg ){ 2889ceea3321Sdrh cacheEntryClear(pParse, p); 2890e55cbd72Sdrh } 2891da250ea5Sdrh } 2892da250ea5Sdrh } 2893e55cbd72Sdrh 2894e55cbd72Sdrh /* 2895da250ea5Sdrh ** Record the fact that an affinity change has occurred on iCount 2896da250ea5Sdrh ** registers starting with iStart. 2897e55cbd72Sdrh */ 2898da250ea5Sdrh void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ 2899f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iStart, iCount); 2900e55cbd72Sdrh } 2901e55cbd72Sdrh 2902e55cbd72Sdrh /* 2903b21e7c70Sdrh ** Generate code to move content from registers iFrom...iFrom+nReg-1 2904b21e7c70Sdrh ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. 2905e55cbd72Sdrh */ 2906b21e7c70Sdrh void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ 2907e8e4af76Sdrh assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo ); 2908079a3072Sdrh sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); 2909236241aeSdrh sqlite3ExprCacheRemove(pParse, iFrom, nReg); 2910945498f3Sdrh } 2911945498f3Sdrh 2912f49f3523Sdrh #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) 291392b01d53Sdrh /* 2914652fbf55Sdrh ** Return true if any register in the range iFrom..iTo (inclusive) 2915652fbf55Sdrh ** is used as part of the column cache. 2916f49f3523Sdrh ** 2917f49f3523Sdrh ** This routine is used within assert() and testcase() macros only 2918f49f3523Sdrh ** and does not appear in a normal build. 2919652fbf55Sdrh */ 2920652fbf55Sdrh static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ 2921652fbf55Sdrh int i; 2922ceea3321Sdrh struct yColCache *p; 2923ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 2924ceea3321Sdrh int r = p->iReg; 2925f49f3523Sdrh if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ 2926652fbf55Sdrh } 2927652fbf55Sdrh return 0; 2928652fbf55Sdrh } 2929f49f3523Sdrh #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ 2930652fbf55Sdrh 2931bea119cdSdrh 2932652fbf55Sdrh /* 2933a4c3c87eSdrh ** Convert an expression node to a TK_REGISTER 2934a4c3c87eSdrh */ 2935a4c3c87eSdrh static void exprToRegister(Expr *p, int iReg){ 2936a4c3c87eSdrh p->op2 = p->op; 2937a4c3c87eSdrh p->op = TK_REGISTER; 2938a4c3c87eSdrh p->iTable = iReg; 2939a4c3c87eSdrh ExprClearProperty(p, EP_Skip); 2940a4c3c87eSdrh } 2941a4c3c87eSdrh 294271c57db0Sdan static void exprCodeBetween(Parse*,Expr*,int,void(*)(Parse*,Expr*,int,int),int); 294371c57db0Sdan 2944a4c3c87eSdrh /* 2945cce7d176Sdrh ** Generate code into the current Vdbe to evaluate the given 29462dcef11bSdrh ** expression. Attempt to store the results in register "target". 29472dcef11bSdrh ** Return the register where results are stored. 2948389a1adbSdrh ** 29498b213899Sdrh ** With this routine, there is no guarantee that results will 29502dcef11bSdrh ** be stored in target. The result might be stored in some other 29512dcef11bSdrh ** register if it is convenient to do so. The calling function 29522dcef11bSdrh ** must check the return code and move the results to the desired 29532dcef11bSdrh ** register. 2954cce7d176Sdrh */ 2955678ccce8Sdrh int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ 29562dcef11bSdrh Vdbe *v = pParse->pVdbe; /* The VM under construction */ 29572dcef11bSdrh int op; /* The opcode being coded */ 29582dcef11bSdrh int inReg = target; /* Results stored in register inReg */ 29592dcef11bSdrh int regFree1 = 0; /* If non-zero free this temporary register */ 29602dcef11bSdrh int regFree2 = 0; /* If non-zero free this temporary register */ 2961678ccce8Sdrh int r1, r2, r3, r4; /* Various register numbers */ 296220411ea7Sdrh sqlite3 *db = pParse->db; /* The database connection */ 296310d1edf0Sdrh Expr tempX; /* Temporary expression node */ 296471c57db0Sdan int p5 = 0; 2965ffe07b2dSdrh 29669cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 296720411ea7Sdrh if( v==0 ){ 296820411ea7Sdrh assert( pParse->db->mallocFailed ); 296920411ea7Sdrh return 0; 297020411ea7Sdrh } 2971389a1adbSdrh 2972389a1adbSdrh if( pExpr==0 ){ 2973389a1adbSdrh op = TK_NULL; 2974389a1adbSdrh }else{ 2975f2bc013cSdrh op = pExpr->op; 2976389a1adbSdrh } 2977f2bc013cSdrh switch( op ){ 297813449892Sdrh case TK_AGG_COLUMN: { 297913449892Sdrh AggInfo *pAggInfo = pExpr->pAggInfo; 298013449892Sdrh struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; 298113449892Sdrh if( !pAggInfo->directMode ){ 29829de221dfSdrh assert( pCol->iMem>0 ); 29839de221dfSdrh inReg = pCol->iMem; 298413449892Sdrh break; 298513449892Sdrh }else if( pAggInfo->useSortingIdx ){ 29865134d135Sdan sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab, 2987389a1adbSdrh pCol->iSorterColumn, target); 298813449892Sdrh break; 298913449892Sdrh } 299013449892Sdrh /* Otherwise, fall thru into the TK_COLUMN case */ 299113449892Sdrh } 2992967e8b73Sdrh case TK_COLUMN: { 2993b2b9d3d7Sdrh int iTab = pExpr->iTable; 2994b2b9d3d7Sdrh if( iTab<0 ){ 2995b2b9d3d7Sdrh if( pParse->ckBase>0 ){ 2996b2b9d3d7Sdrh /* Generating CHECK constraints or inserting into partial index */ 2997aa9b8963Sdrh inReg = pExpr->iColumn + pParse->ckBase; 2998b2b9d3d7Sdrh break; 2999c4a3c779Sdrh }else{ 30001f9ca2c8Sdrh /* Coding an expression that is part of an index where column names 30011f9ca2c8Sdrh ** in the index refer to the table to which the index belongs */ 30021f9ca2c8Sdrh iTab = pParse->iSelfTab; 30032282792aSdrh } 3004b2b9d3d7Sdrh } 3005b2b9d3d7Sdrh inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, 3006b2b9d3d7Sdrh pExpr->iColumn, iTab, target, 3007b2b9d3d7Sdrh pExpr->op2); 3008cce7d176Sdrh break; 3009cce7d176Sdrh } 3010cce7d176Sdrh case TK_INTEGER: { 301113573c71Sdrh codeInteger(pParse, pExpr, 0, target); 3012fec19aadSdrh break; 301351e9a445Sdrh } 301413573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 3015598f1340Sdrh case TK_FLOAT: { 301633e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 301733e619fcSdrh codeReal(v, pExpr->u.zToken, 0, target); 3018598f1340Sdrh break; 3019598f1340Sdrh } 302013573c71Sdrh #endif 3021fec19aadSdrh case TK_STRING: { 302233e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3023076e85f5Sdrh sqlite3VdbeLoadString(v, target, pExpr->u.zToken); 3024cce7d176Sdrh break; 3025cce7d176Sdrh } 3026f0863fe5Sdrh case TK_NULL: { 30279de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3028f0863fe5Sdrh break; 3029f0863fe5Sdrh } 30305338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_BLOB_LITERAL 3031c572ef7fSdanielk1977 case TK_BLOB: { 30326c8c6cecSdrh int n; 30336c8c6cecSdrh const char *z; 3034ca48c90fSdrh char *zBlob; 303533e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 303633e619fcSdrh assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); 303733e619fcSdrh assert( pExpr->u.zToken[1]=='\'' ); 303833e619fcSdrh z = &pExpr->u.zToken[2]; 3039b7916a78Sdrh n = sqlite3Strlen30(z) - 1; 3040b7916a78Sdrh assert( z[n]=='\'' ); 3041ca48c90fSdrh zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); 3042ca48c90fSdrh sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); 3043c572ef7fSdanielk1977 break; 3044c572ef7fSdanielk1977 } 30455338a5f7Sdanielk1977 #endif 304650457896Sdrh case TK_VARIABLE: { 304733e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 304833e619fcSdrh assert( pExpr->u.zToken!=0 ); 304933e619fcSdrh assert( pExpr->u.zToken[0]!=0 ); 3050eaf52d88Sdrh sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); 305133e619fcSdrh if( pExpr->u.zToken[1]!=0 ){ 305204e9eeadSdrh assert( pExpr->u.zToken[0]=='?' 305304e9eeadSdrh || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 ); 305404e9eeadSdrh sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC); 3055895d7472Sdrh } 305650457896Sdrh break; 305750457896Sdrh } 30584e0cff60Sdrh case TK_REGISTER: { 30599de221dfSdrh inReg = pExpr->iTable; 30604e0cff60Sdrh break; 30614e0cff60Sdrh } 3062487e262fSdrh #ifndef SQLITE_OMIT_CAST 3063487e262fSdrh case TK_CAST: { 3064487e262fSdrh /* Expressions of the form: CAST(pLeft AS token) */ 30652dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 30661735fa88Sdrh if( inReg!=target ){ 30671735fa88Sdrh sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); 30681735fa88Sdrh inReg = target; 30691735fa88Sdrh } 30704169e430Sdrh sqlite3VdbeAddOp2(v, OP_Cast, target, 30714169e430Sdrh sqlite3AffinityType(pExpr->u.zToken, 0)); 3072c5499befSdrh testcase( usedAsColumnCache(pParse, inReg, inReg) ); 3073b3843a82Sdrh sqlite3ExprCacheAffinityChange(pParse, inReg, 1); 3074487e262fSdrh break; 3075487e262fSdrh } 3076487e262fSdrh #endif /* SQLITE_OMIT_CAST */ 307771c57db0Sdan case TK_IS: 307871c57db0Sdan case TK_ISNOT: 307971c57db0Sdan op = (op==TK_IS) ? TK_EQ : TK_NE; 308071c57db0Sdan p5 = SQLITE_NULLEQ; 308171c57db0Sdan /* fall-through */ 3082c9b84a1fSdrh case TK_LT: 3083c9b84a1fSdrh case TK_LE: 3084c9b84a1fSdrh case TK_GT: 3085c9b84a1fSdrh case TK_GE: 3086c9b84a1fSdrh case TK_NE: 3087c9b84a1fSdrh case TK_EQ: { 308871c57db0Sdan Expr *pLeft = pExpr->pLeft; 308971c57db0Sdan if( (pLeft->flags & EP_Vector) ){ 309071c57db0Sdan codeVectorCompare(pParse, pExpr, target); 309171c57db0Sdan }else{ 309271c57db0Sdan r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 3093b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 309471c57db0Sdan codeCompare(pParse, pLeft, pExpr->pRight, op, 309571c57db0Sdan r1, r2, inReg, SQLITE_STOREP2 | p5); 30967d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 30977d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 30987d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 30997d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 31007d176105Sdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq); 31017d176105Sdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne); 3102c5499befSdrh testcase( regFree1==0 ); 3103c5499befSdrh testcase( regFree2==0 ); 3104c9b84a1fSdrh } 31056a2fe093Sdrh break; 31066a2fe093Sdrh } 3107cce7d176Sdrh case TK_AND: 3108cce7d176Sdrh case TK_OR: 3109cce7d176Sdrh case TK_PLUS: 3110cce7d176Sdrh case TK_STAR: 3111cce7d176Sdrh case TK_MINUS: 3112bf4133cbSdrh case TK_REM: 3113bf4133cbSdrh case TK_BITAND: 3114bf4133cbSdrh case TK_BITOR: 311517c40294Sdrh case TK_SLASH: 3116bf4133cbSdrh case TK_LSHIFT: 3117855eb1cfSdrh case TK_RSHIFT: 31180040077dSdrh case TK_CONCAT: { 31197d176105Sdrh assert( TK_AND==OP_And ); testcase( op==TK_AND ); 31207d176105Sdrh assert( TK_OR==OP_Or ); testcase( op==TK_OR ); 31217d176105Sdrh assert( TK_PLUS==OP_Add ); testcase( op==TK_PLUS ); 31227d176105Sdrh assert( TK_MINUS==OP_Subtract ); testcase( op==TK_MINUS ); 31237d176105Sdrh assert( TK_REM==OP_Remainder ); testcase( op==TK_REM ); 31247d176105Sdrh assert( TK_BITAND==OP_BitAnd ); testcase( op==TK_BITAND ); 31257d176105Sdrh assert( TK_BITOR==OP_BitOr ); testcase( op==TK_BITOR ); 31267d176105Sdrh assert( TK_SLASH==OP_Divide ); testcase( op==TK_SLASH ); 31277d176105Sdrh assert( TK_LSHIFT==OP_ShiftLeft ); testcase( op==TK_LSHIFT ); 31287d176105Sdrh assert( TK_RSHIFT==OP_ShiftRight ); testcase( op==TK_RSHIFT ); 31297d176105Sdrh assert( TK_CONCAT==OP_Concat ); testcase( op==TK_CONCAT ); 31302dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 31312dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 31325b6afba9Sdrh sqlite3VdbeAddOp3(v, op, r2, r1, target); 3133c5499befSdrh testcase( regFree1==0 ); 3134c5499befSdrh testcase( regFree2==0 ); 31350040077dSdrh break; 31360040077dSdrh } 3137cce7d176Sdrh case TK_UMINUS: { 3138fec19aadSdrh Expr *pLeft = pExpr->pLeft; 3139fec19aadSdrh assert( pLeft ); 314013573c71Sdrh if( pLeft->op==TK_INTEGER ){ 314113573c71Sdrh codeInteger(pParse, pLeft, 1, target); 314213573c71Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 314313573c71Sdrh }else if( pLeft->op==TK_FLOAT ){ 314433e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 314533e619fcSdrh codeReal(v, pLeft->u.zToken, 1, target); 314613573c71Sdrh #endif 31473c84ddffSdrh }else{ 314810d1edf0Sdrh tempX.op = TK_INTEGER; 314910d1edf0Sdrh tempX.flags = EP_IntValue|EP_TokenOnly; 315010d1edf0Sdrh tempX.u.iValue = 0; 315110d1edf0Sdrh r1 = sqlite3ExprCodeTemp(pParse, &tempX, ®Free1); 3152e55cbd72Sdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); 31532dcef11bSdrh sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); 3154c5499befSdrh testcase( regFree2==0 ); 31553c84ddffSdrh } 31569de221dfSdrh inReg = target; 31576e142f54Sdrh break; 31586e142f54Sdrh } 3159bf4133cbSdrh case TK_BITNOT: 31606e142f54Sdrh case TK_NOT: { 31617d176105Sdrh assert( TK_BITNOT==OP_BitNot ); testcase( op==TK_BITNOT ); 31627d176105Sdrh assert( TK_NOT==OP_Not ); testcase( op==TK_NOT ); 3163e99fa2afSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3164e99fa2afSdrh testcase( regFree1==0 ); 3165e99fa2afSdrh inReg = target; 3166e99fa2afSdrh sqlite3VdbeAddOp2(v, op, r1, inReg); 3167cce7d176Sdrh break; 3168cce7d176Sdrh } 3169cce7d176Sdrh case TK_ISNULL: 3170cce7d176Sdrh case TK_NOTNULL: { 31716a288a33Sdrh int addr; 31727d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 31737d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 31749de221dfSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 31752dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3176c5499befSdrh testcase( regFree1==0 ); 31772dcef11bSdrh addr = sqlite3VdbeAddOp1(v, op, r1); 31787d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 31797d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3180a976979bSdrh sqlite3VdbeAddOp2(v, OP_Integer, 0, target); 31816a288a33Sdrh sqlite3VdbeJumpHere(v, addr); 3182a37cdde0Sdanielk1977 break; 3183f2bc013cSdrh } 31842282792aSdrh case TK_AGG_FUNCTION: { 318513449892Sdrh AggInfo *pInfo = pExpr->pAggInfo; 31867e56e711Sdrh if( pInfo==0 ){ 318733e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 318833e619fcSdrh sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); 31897e56e711Sdrh }else{ 31909de221dfSdrh inReg = pInfo->aFunc[pExpr->iAgg].iMem; 31917e56e711Sdrh } 31922282792aSdrh break; 31932282792aSdrh } 3194cce7d176Sdrh case TK_FUNCTION: { 319512ffee8cSdrh ExprList *pFarg; /* List of function arguments */ 319612ffee8cSdrh int nFarg; /* Number of function arguments */ 319712ffee8cSdrh FuncDef *pDef; /* The function definition object */ 319812ffee8cSdrh const char *zId; /* The function name */ 3199693e6719Sdrh u32 constMask = 0; /* Mask of function arguments that are constant */ 320012ffee8cSdrh int i; /* Loop counter */ 320112ffee8cSdrh u8 enc = ENC(db); /* The text encoding used by this database */ 320212ffee8cSdrh CollSeq *pColl = 0; /* A collating sequence */ 320317435752Sdrh 32046ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 3205c5cd1249Sdrh if( ExprHasProperty(pExpr, EP_TokenOnly) ){ 320612ffee8cSdrh pFarg = 0; 320712ffee8cSdrh }else{ 320812ffee8cSdrh pFarg = pExpr->x.pList; 320912ffee8cSdrh } 321012ffee8cSdrh nFarg = pFarg ? pFarg->nExpr : 0; 321133e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 321233e619fcSdrh zId = pExpr->u.zToken; 321380738d9cSdrh pDef = sqlite3FindFunction(db, zId, nFarg, enc, 0); 32142d80151fSdrh if( pDef==0 || pDef->xFinalize!=0 ){ 321580738d9cSdrh sqlite3ErrorMsg(pParse, "unknown function: %s()", zId); 3216feb306f5Sdrh break; 3217feb306f5Sdrh } 3218ae6bb957Sdrh 3219ae6bb957Sdrh /* Attempt a direct implementation of the built-in COALESCE() and 322060ec914cSpeter.d.reid ** IFNULL() functions. This avoids unnecessary evaluation of 3221ae6bb957Sdrh ** arguments past the first non-NULL argument. 3222ae6bb957Sdrh */ 3223d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){ 3224ae6bb957Sdrh int endCoalesce = sqlite3VdbeMakeLabel(v); 3225ae6bb957Sdrh assert( nFarg>=2 ); 3226ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); 3227ae6bb957Sdrh for(i=1; i<nFarg; i++){ 3228ae6bb957Sdrh sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce); 3229688852abSdrh VdbeCoverage(v); 3230f49f3523Sdrh sqlite3ExprCacheRemove(pParse, target, 1); 3231ae6bb957Sdrh sqlite3ExprCachePush(pParse); 3232ae6bb957Sdrh sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target); 3233d2490904Sdrh sqlite3ExprCachePop(pParse); 3234ae6bb957Sdrh } 3235ae6bb957Sdrh sqlite3VdbeResolveLabel(v, endCoalesce); 3236ae6bb957Sdrh break; 3237ae6bb957Sdrh } 3238ae6bb957Sdrh 3239cca9f3d2Sdrh /* The UNLIKELY() function is a no-op. The result is the value 3240cca9f3d2Sdrh ** of the first argument. 3241cca9f3d2Sdrh */ 3242cca9f3d2Sdrh if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){ 3243cca9f3d2Sdrh assert( nFarg>=1 ); 32445f02ab09Sdrh inReg = sqlite3ExprCodeTarget(pParse, pFarg->a[0].pExpr, target); 3245cca9f3d2Sdrh break; 3246cca9f3d2Sdrh } 3247ae6bb957Sdrh 3248d1a01edaSdrh for(i=0; i<nFarg; i++){ 3249d1a01edaSdrh if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){ 3250693e6719Sdrh testcase( i==31 ); 3251693e6719Sdrh constMask |= MASKBIT32(i); 3252d1a01edaSdrh } 3253d1a01edaSdrh if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ 3254d1a01edaSdrh pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); 3255d1a01edaSdrh } 3256d1a01edaSdrh } 325712ffee8cSdrh if( pFarg ){ 3258d1a01edaSdrh if( constMask ){ 3259d1a01edaSdrh r1 = pParse->nMem+1; 3260d1a01edaSdrh pParse->nMem += nFarg; 3261d1a01edaSdrh }else{ 326212ffee8cSdrh r1 = sqlite3GetTempRange(pParse, nFarg); 3263d1a01edaSdrh } 3264a748fdccSdrh 3265a748fdccSdrh /* For length() and typeof() functions with a column argument, 3266a748fdccSdrh ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG 3267a748fdccSdrh ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data 3268a748fdccSdrh ** loading. 3269a748fdccSdrh */ 3270d36e1041Sdrh if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){ 32714e245a4cSdrh u8 exprOp; 3272a748fdccSdrh assert( nFarg==1 ); 3273a748fdccSdrh assert( pFarg->a[0].pExpr!=0 ); 32744e245a4cSdrh exprOp = pFarg->a[0].pExpr->op; 32754e245a4cSdrh if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){ 3276a748fdccSdrh assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG ); 3277a748fdccSdrh assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG ); 3278b1fba286Sdrh testcase( pDef->funcFlags & OPFLAG_LENGTHARG ); 3279b1fba286Sdrh pFarg->a[0].pExpr->op2 = 3280b1fba286Sdrh pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG); 3281a748fdccSdrh } 3282a748fdccSdrh } 3283a748fdccSdrh 3284d7d385ddSdrh sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ 32855579d59fSdrh sqlite3ExprCodeExprList(pParse, pFarg, r1, 0, 3286d1a01edaSdrh SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR); 3287d2490904Sdrh sqlite3ExprCachePop(pParse); /* Ticket 2ea2425d34be */ 3288892d3179Sdrh }else{ 328912ffee8cSdrh r1 = 0; 3290892d3179Sdrh } 3291b7f6f68fSdrh #ifndef SQLITE_OMIT_VIRTUALTABLE 3292a43fa227Sdrh /* Possibly overload the function if the first argument is 3293a43fa227Sdrh ** a virtual table column. 3294a43fa227Sdrh ** 3295a43fa227Sdrh ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the 3296a43fa227Sdrh ** second argument, not the first, as the argument to test to 3297a43fa227Sdrh ** see if it is a column in a virtual table. This is done because 3298a43fa227Sdrh ** the left operand of infix functions (the operand we want to 3299a43fa227Sdrh ** control overloading) ends up as the second argument to the 3300a43fa227Sdrh ** function. The expression "A glob B" is equivalent to 3301a43fa227Sdrh ** "glob(B,A). We want to use the A in "A glob B" to test 3302a43fa227Sdrh ** for function overloading. But we use the B term in "glob(B,A)". 3303a43fa227Sdrh */ 330412ffee8cSdrh if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ 330512ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); 330612ffee8cSdrh }else if( nFarg>0 ){ 330712ffee8cSdrh pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); 3308b7f6f68fSdrh } 3309b7f6f68fSdrh #endif 3310d36e1041Sdrh if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){ 33118b213899Sdrh if( !pColl ) pColl = db->pDfltColl; 331266a5167bSdrh sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); 3313682f68b0Sdanielk1977 } 33149c7c913cSdrh sqlite3VdbeAddOp4(v, OP_Function0, constMask, r1, target, 331566a5167bSdrh (char*)pDef, P4_FUNCDEF); 331612ffee8cSdrh sqlite3VdbeChangeP5(v, (u8)nFarg); 3317d1a01edaSdrh if( nFarg && constMask==0 ){ 331812ffee8cSdrh sqlite3ReleaseTempRange(pParse, r1, nFarg); 33192dcef11bSdrh } 33206ec2733bSdrh break; 33216ec2733bSdrh } 3322fe2093d7Sdrh #ifndef SQLITE_OMIT_SUBQUERY 3323fe2093d7Sdrh case TK_EXISTS: 332419a775c2Sdrh case TK_SELECT: { 3325c5499befSdrh testcase( op==TK_EXISTS ); 3326c5499befSdrh testcase( op==TK_SELECT ); 33271450bc6eSdrh inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); 332819a775c2Sdrh break; 332919a775c2Sdrh } 3330fef5208cSdrh case TK_IN: { 3331e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3332e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 3333e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 3334e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 333566ba23ceSdrh sqlite3VdbeAddOp2(v, OP_Integer, 1, target); 3336e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3337e3365e6cSdrh sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); 3338e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 3339fef5208cSdrh break; 3340fef5208cSdrh } 3341e3365e6cSdrh #endif /* SQLITE_OMIT_SUBQUERY */ 3342e3365e6cSdrh 3343e3365e6cSdrh 33442dcef11bSdrh /* 33452dcef11bSdrh ** x BETWEEN y AND z 33462dcef11bSdrh ** 33472dcef11bSdrh ** This is equivalent to 33482dcef11bSdrh ** 33492dcef11bSdrh ** x>=y AND x<=z 33502dcef11bSdrh ** 33512dcef11bSdrh ** X is stored in pExpr->pLeft. 33522dcef11bSdrh ** Y is stored in pExpr->pList->a[0].pExpr. 33532dcef11bSdrh ** Z is stored in pExpr->pList->a[1].pExpr. 33542dcef11bSdrh */ 3355fef5208cSdrh case TK_BETWEEN: { 335671c57db0Sdan exprCodeBetween(pParse, pExpr, target, 0, 0); 335771c57db0Sdan #if 0 3358be5c89acSdrh Expr *pLeft = pExpr->pLeft; 33596ab3a2ecSdanielk1977 struct ExprList_item *pLItem = pExpr->x.pList->a; 3360be5c89acSdrh Expr *pRight = pLItem->pExpr; 336135573356Sdrh 3362b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); 3363b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 3364c5499befSdrh testcase( regFree1==0 ); 3365c5499befSdrh testcase( regFree2==0 ); 33662dcef11bSdrh r3 = sqlite3GetTempReg(pParse); 3367678ccce8Sdrh r4 = sqlite3GetTempReg(pParse); 336835573356Sdrh codeCompare(pParse, pLeft, pRight, OP_Ge, 33697d176105Sdrh r1, r2, r3, SQLITE_STOREP2); VdbeCoverage(v); 3370be5c89acSdrh pLItem++; 3371be5c89acSdrh pRight = pLItem->pExpr; 33722dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 33732dcef11bSdrh r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); 3374c5499befSdrh testcase( regFree2==0 ); 3375678ccce8Sdrh codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); 3376688852abSdrh VdbeCoverage(v); 3377678ccce8Sdrh sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); 33782dcef11bSdrh sqlite3ReleaseTempReg(pParse, r3); 3379678ccce8Sdrh sqlite3ReleaseTempReg(pParse, r4); 338071c57db0Sdan #endif 3381fef5208cSdrh break; 3382fef5208cSdrh } 338394fa9c41Sdrh case TK_SPAN: 3384ae80ddeaSdrh case TK_COLLATE: 33854f07e5fbSdrh case TK_UPLUS: { 33862dcef11bSdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); 3387a2e00042Sdrh break; 3388a2e00042Sdrh } 33892dcef11bSdrh 3390165921a7Sdan case TK_TRIGGER: { 339165a7cd16Sdan /* If the opcode is TK_TRIGGER, then the expression is a reference 339265a7cd16Sdan ** to a column in the new.* or old.* pseudo-tables available to 339365a7cd16Sdan ** trigger programs. In this case Expr.iTable is set to 1 for the 339465a7cd16Sdan ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn 339565a7cd16Sdan ** is set to the column of the pseudo-table to read, or to -1 to 339665a7cd16Sdan ** read the rowid field. 339765a7cd16Sdan ** 339865a7cd16Sdan ** The expression is implemented using an OP_Param opcode. The p1 339965a7cd16Sdan ** parameter is set to 0 for an old.rowid reference, or to (i+1) 340065a7cd16Sdan ** to reference another column of the old.* pseudo-table, where 340165a7cd16Sdan ** i is the index of the column. For a new.rowid reference, p1 is 340265a7cd16Sdan ** set to (n+1), where n is the number of columns in each pseudo-table. 340365a7cd16Sdan ** For a reference to any other column in the new.* pseudo-table, p1 340465a7cd16Sdan ** is set to (n+2+i), where n and i are as defined previously. For 340565a7cd16Sdan ** example, if the table on which triggers are being fired is 340665a7cd16Sdan ** declared as: 340765a7cd16Sdan ** 340865a7cd16Sdan ** CREATE TABLE t1(a, b); 340965a7cd16Sdan ** 341065a7cd16Sdan ** Then p1 is interpreted as follows: 341165a7cd16Sdan ** 341265a7cd16Sdan ** p1==0 -> old.rowid p1==3 -> new.rowid 341365a7cd16Sdan ** p1==1 -> old.a p1==4 -> new.a 341465a7cd16Sdan ** p1==2 -> old.b p1==5 -> new.b 341565a7cd16Sdan */ 34162832ad42Sdan Table *pTab = pExpr->pTab; 341765a7cd16Sdan int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; 341865a7cd16Sdan 341965a7cd16Sdan assert( pExpr->iTable==0 || pExpr->iTable==1 ); 342065a7cd16Sdan assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol ); 342165a7cd16Sdan assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); 342265a7cd16Sdan assert( p1>=0 && p1<(pTab->nCol*2+2) ); 342365a7cd16Sdan 342465a7cd16Sdan sqlite3VdbeAddOp2(v, OP_Param, p1, target); 342576d462eeSdan VdbeComment((v, "%s.%s -> $%d", 3426165921a7Sdan (pExpr->iTable ? "new" : "old"), 342776d462eeSdan (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), 342876d462eeSdan target 3429165921a7Sdan )); 343065a7cd16Sdan 343144dbca83Sdrh #ifndef SQLITE_OMIT_FLOATING_POINT 343265a7cd16Sdan /* If the column has REAL affinity, it may currently be stored as an 3433113762a2Sdrh ** integer. Use OP_RealAffinity to make sure it is really real. 3434113762a2Sdrh ** 3435113762a2Sdrh ** EVIDENCE-OF: R-60985-57662 SQLite will convert the value back to 3436113762a2Sdrh ** floating point when extracting it from the record. */ 34372832ad42Sdan if( pExpr->iColumn>=0 34382832ad42Sdan && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL 34392832ad42Sdan ){ 34402832ad42Sdan sqlite3VdbeAddOp1(v, OP_RealAffinity, target); 34412832ad42Sdan } 344244dbca83Sdrh #endif 3443165921a7Sdan break; 3444165921a7Sdan } 3445165921a7Sdan 344671c57db0Sdan case TK_VECTOR: { 344771c57db0Sdan sqlite3ErrorMsg(pParse, "invalid use of row value (1)"); 344871c57db0Sdan break; 344971c57db0Sdan } 345071c57db0Sdan 345171c57db0Sdan case TK_SELECT_COLUMN: { 345271c57db0Sdan Expr *pLeft = pExpr->pLeft; 345371c57db0Sdan assert( pLeft ); 345471c57db0Sdan assert( pLeft->op==TK_SELECT || pLeft->op==TK_REGISTER ); 345571c57db0Sdan if( pLeft->op==TK_SELECT ){ 345671c57db0Sdan pLeft->iTable = sqlite3CodeSubselect(pParse, pLeft, 0, 0); 345771c57db0Sdan pLeft->op = TK_REGISTER; 345871c57db0Sdan } 345971c57db0Sdan inReg = pLeft->iTable + pExpr->iColumn; 346071c57db0Sdan break; 346171c57db0Sdan } 3462165921a7Sdan 34632dcef11bSdrh /* 34642dcef11bSdrh ** Form A: 34652dcef11bSdrh ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 34662dcef11bSdrh ** 34672dcef11bSdrh ** Form B: 34682dcef11bSdrh ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END 34692dcef11bSdrh ** 34702dcef11bSdrh ** Form A is can be transformed into the equivalent form B as follows: 34712dcef11bSdrh ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... 34722dcef11bSdrh ** WHEN x=eN THEN rN ELSE y END 34732dcef11bSdrh ** 34742dcef11bSdrh ** X (if it exists) is in pExpr->pLeft. 3475c5cd1249Sdrh ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is 3476c5cd1249Sdrh ** odd. The Y is also optional. If the number of elements in x.pList 3477c5cd1249Sdrh ** is even, then Y is omitted and the "otherwise" result is NULL. 34782dcef11bSdrh ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. 34792dcef11bSdrh ** 34802dcef11bSdrh ** The result of the expression is the Ri for the first matching Ei, 34812dcef11bSdrh ** or if there is no matching Ei, the ELSE term Y, or if there is 34822dcef11bSdrh ** no ELSE term, NULL. 34832dcef11bSdrh */ 348433cd4909Sdrh default: assert( op==TK_CASE ); { 34852dcef11bSdrh int endLabel; /* GOTO label for end of CASE stmt */ 34862dcef11bSdrh int nextCase; /* GOTO label for next WHEN clause */ 34872dcef11bSdrh int nExpr; /* 2x number of WHEN terms */ 34882dcef11bSdrh int i; /* Loop counter */ 34892dcef11bSdrh ExprList *pEList; /* List of WHEN terms */ 34902dcef11bSdrh struct ExprList_item *aListelem; /* Array of WHEN terms */ 34912dcef11bSdrh Expr opCompare; /* The X==Ei expression */ 34922dcef11bSdrh Expr *pX; /* The X expression */ 34931bd10f8aSdrh Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ 3494ceea3321Sdrh VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) 349517a7f8ddSdrh 34966ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); 34976ab3a2ecSdanielk1977 assert(pExpr->x.pList->nExpr > 0); 34986ab3a2ecSdanielk1977 pEList = pExpr->x.pList; 3499be5c89acSdrh aListelem = pEList->a; 3500be5c89acSdrh nExpr = pEList->nExpr; 35012dcef11bSdrh endLabel = sqlite3VdbeMakeLabel(v); 35022dcef11bSdrh if( (pX = pExpr->pLeft)!=0 ){ 350310d1edf0Sdrh tempX = *pX; 350433cd4909Sdrh testcase( pX->op==TK_COLUMN ); 350510d1edf0Sdrh exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, ®Free1)); 3506c5499befSdrh testcase( regFree1==0 ); 35072dcef11bSdrh opCompare.op = TK_EQ; 350810d1edf0Sdrh opCompare.pLeft = &tempX; 35092dcef11bSdrh pTest = &opCompare; 35108b1db07fSdrh /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: 35118b1db07fSdrh ** The value in regFree1 might get SCopy-ed into the file result. 35128b1db07fSdrh ** So make sure that the regFree1 register is not reused for other 35138b1db07fSdrh ** purposes and possibly overwritten. */ 35148b1db07fSdrh regFree1 = 0; 3515cce7d176Sdrh } 3516c5cd1249Sdrh for(i=0; i<nExpr-1; i=i+2){ 3517ceea3321Sdrh sqlite3ExprCachePush(pParse); 35182dcef11bSdrh if( pX ){ 35191bd10f8aSdrh assert( pTest!=0 ); 35202dcef11bSdrh opCompare.pRight = aListelem[i].pExpr; 3521f5905aa7Sdrh }else{ 35222dcef11bSdrh pTest = aListelem[i].pExpr; 352317a7f8ddSdrh } 35242dcef11bSdrh nextCase = sqlite3VdbeMakeLabel(v); 352533cd4909Sdrh testcase( pTest->op==TK_COLUMN ); 35262dcef11bSdrh sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); 3527c5499befSdrh testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); 35289de221dfSdrh sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); 3529076e85f5Sdrh sqlite3VdbeGoto(v, endLabel); 3530d2490904Sdrh sqlite3ExprCachePop(pParse); 35312dcef11bSdrh sqlite3VdbeResolveLabel(v, nextCase); 3532f570f011Sdrh } 3533c5cd1249Sdrh if( (nExpr&1)!=0 ){ 3534ceea3321Sdrh sqlite3ExprCachePush(pParse); 3535c5cd1249Sdrh sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target); 3536d2490904Sdrh sqlite3ExprCachePop(pParse); 353717a7f8ddSdrh }else{ 35389de221dfSdrh sqlite3VdbeAddOp2(v, OP_Null, 0, target); 353917a7f8ddSdrh } 3540c1f4a19bSdanielk1977 assert( db->mallocFailed || pParse->nErr>0 3541c1f4a19bSdanielk1977 || pParse->iCacheLevel==iCacheLevel ); 35422dcef11bSdrh sqlite3VdbeResolveLabel(v, endLabel); 35436f34903eSdanielk1977 break; 35446f34903eSdanielk1977 } 35455338a5f7Sdanielk1977 #ifndef SQLITE_OMIT_TRIGGER 35466f34903eSdanielk1977 case TK_RAISE: { 3547165921a7Sdan assert( pExpr->affinity==OE_Rollback 3548165921a7Sdan || pExpr->affinity==OE_Abort 3549165921a7Sdan || pExpr->affinity==OE_Fail 3550165921a7Sdan || pExpr->affinity==OE_Ignore 3551165921a7Sdan ); 3552e0af83acSdan if( !pParse->pTriggerTab ){ 3553e0af83acSdan sqlite3ErrorMsg(pParse, 3554e0af83acSdan "RAISE() may only be used within a trigger-program"); 3555e0af83acSdan return 0; 3556e0af83acSdan } 3557e0af83acSdan if( pExpr->affinity==OE_Abort ){ 3558e0af83acSdan sqlite3MayAbort(pParse); 3559e0af83acSdan } 356033e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 3561e0af83acSdan if( pExpr->affinity==OE_Ignore ){ 3562e0af83acSdan sqlite3VdbeAddOp4( 3563e0af83acSdan v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); 3564688852abSdrh VdbeCoverage(v); 3565e0af83acSdan }else{ 3566433dccfbSdrh sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER, 3567f9c8ce3cSdrh pExpr->affinity, pExpr->u.zToken, 0, 0); 3568e0af83acSdan } 3569e0af83acSdan 3570ffe07b2dSdrh break; 357117a7f8ddSdrh } 35725338a5f7Sdanielk1977 #endif 3573ffe07b2dSdrh } 35742dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 35752dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 35762dcef11bSdrh return inReg; 35775b6afba9Sdrh } 35782dcef11bSdrh 35792dcef11bSdrh /* 3580d1a01edaSdrh ** Factor out the code of the given expression to initialization time. 3581d1a01edaSdrh */ 3582d673cddaSdrh void sqlite3ExprCodeAtInit( 3583d673cddaSdrh Parse *pParse, /* Parsing context */ 3584d673cddaSdrh Expr *pExpr, /* The expression to code when the VDBE initializes */ 3585d673cddaSdrh int regDest, /* Store the value in this register */ 3586d673cddaSdrh u8 reusable /* True if this expression is reusable */ 3587d673cddaSdrh ){ 3588d1a01edaSdrh ExprList *p; 3589d9f158e7Sdrh assert( ConstFactorOk(pParse) ); 3590d1a01edaSdrh p = pParse->pConstExpr; 3591d1a01edaSdrh pExpr = sqlite3ExprDup(pParse->db, pExpr, 0); 3592d1a01edaSdrh p = sqlite3ExprListAppend(pParse, p, pExpr); 3593d673cddaSdrh if( p ){ 3594d673cddaSdrh struct ExprList_item *pItem = &p->a[p->nExpr-1]; 3595d673cddaSdrh pItem->u.iConstExprReg = regDest; 3596d673cddaSdrh pItem->reusable = reusable; 3597d673cddaSdrh } 3598d1a01edaSdrh pParse->pConstExpr = p; 3599d1a01edaSdrh } 3600d1a01edaSdrh 3601d1a01edaSdrh /* 36022dcef11bSdrh ** Generate code to evaluate an expression and store the results 36032dcef11bSdrh ** into a register. Return the register number where the results 36042dcef11bSdrh ** are stored. 36052dcef11bSdrh ** 36062dcef11bSdrh ** If the register is a temporary register that can be deallocated, 3607678ccce8Sdrh ** then write its number into *pReg. If the result register is not 36082dcef11bSdrh ** a temporary, then set *pReg to zero. 3609f30a969bSdrh ** 3610f30a969bSdrh ** If pExpr is a constant, then this routine might generate this 3611f30a969bSdrh ** code to fill the register in the initialization section of the 3612f30a969bSdrh ** VDBE program, in order to factor it out of the evaluation loop. 36132dcef11bSdrh */ 36142dcef11bSdrh int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ 3615f30a969bSdrh int r2; 3616f30a969bSdrh pExpr = sqlite3ExprSkipCollate(pExpr); 3617d9f158e7Sdrh if( ConstFactorOk(pParse) 3618f30a969bSdrh && pExpr->op!=TK_REGISTER 3619f30a969bSdrh && sqlite3ExprIsConstantNotJoin(pExpr) 3620f30a969bSdrh ){ 3621f30a969bSdrh ExprList *p = pParse->pConstExpr; 3622f30a969bSdrh int i; 3623f30a969bSdrh *pReg = 0; 3624f30a969bSdrh if( p ){ 3625d673cddaSdrh struct ExprList_item *pItem; 3626d673cddaSdrh for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){ 3627d673cddaSdrh if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){ 3628d673cddaSdrh return pItem->u.iConstExprReg; 3629f30a969bSdrh } 3630f30a969bSdrh } 3631f30a969bSdrh } 3632f30a969bSdrh r2 = ++pParse->nMem; 3633d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1); 3634f30a969bSdrh }else{ 36352dcef11bSdrh int r1 = sqlite3GetTempReg(pParse); 3636f30a969bSdrh r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); 36372dcef11bSdrh if( r2==r1 ){ 36382dcef11bSdrh *pReg = r1; 36392dcef11bSdrh }else{ 36402dcef11bSdrh sqlite3ReleaseTempReg(pParse, r1); 36412dcef11bSdrh *pReg = 0; 36422dcef11bSdrh } 3643f30a969bSdrh } 36442dcef11bSdrh return r2; 36452dcef11bSdrh } 36462dcef11bSdrh 36472dcef11bSdrh /* 36482dcef11bSdrh ** Generate code that will evaluate expression pExpr and store the 36492dcef11bSdrh ** results in register target. The results are guaranteed to appear 36502dcef11bSdrh ** in register target. 36512dcef11bSdrh */ 365205a86c5cSdrh void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ 36539cbf3425Sdrh int inReg; 36549cbf3425Sdrh 36559cbf3425Sdrh assert( target>0 && target<=pParse->nMem ); 3656ebc16717Sdrh if( pExpr && pExpr->op==TK_REGISTER ){ 3657ebc16717Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); 3658ebc16717Sdrh }else{ 36599cbf3425Sdrh inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); 36601c75c9d7Sdrh assert( pParse->pVdbe!=0 || pParse->db->mallocFailed ); 36610e359b30Sdrh if( inReg!=target && pParse->pVdbe ){ 36629cbf3425Sdrh sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); 366317a7f8ddSdrh } 3664ebc16717Sdrh } 3665cce7d176Sdrh } 3666cce7d176Sdrh 3667cce7d176Sdrh /* 36681c75c9d7Sdrh ** Make a transient copy of expression pExpr and then code it using 36691c75c9d7Sdrh ** sqlite3ExprCode(). This routine works just like sqlite3ExprCode() 36701c75c9d7Sdrh ** except that the input expression is guaranteed to be unchanged. 36711c75c9d7Sdrh */ 36721c75c9d7Sdrh void sqlite3ExprCodeCopy(Parse *pParse, Expr *pExpr, int target){ 36731c75c9d7Sdrh sqlite3 *db = pParse->db; 36741c75c9d7Sdrh pExpr = sqlite3ExprDup(db, pExpr, 0); 36751c75c9d7Sdrh if( !db->mallocFailed ) sqlite3ExprCode(pParse, pExpr, target); 36761c75c9d7Sdrh sqlite3ExprDelete(db, pExpr); 36771c75c9d7Sdrh } 36781c75c9d7Sdrh 36791c75c9d7Sdrh /* 368005a86c5cSdrh ** Generate code that will evaluate expression pExpr and store the 368105a86c5cSdrh ** results in register target. The results are guaranteed to appear 368205a86c5cSdrh ** in register target. If the expression is constant, then this routine 368305a86c5cSdrh ** might choose to code the expression at initialization time. 368405a86c5cSdrh */ 368505a86c5cSdrh void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){ 368605a86c5cSdrh if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){ 368705a86c5cSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target, 0); 368805a86c5cSdrh }else{ 368905a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 369005a86c5cSdrh } 3691cce7d176Sdrh } 3692cce7d176Sdrh 3693cce7d176Sdrh /* 369460ec914cSpeter.d.reid ** Generate code that evaluates the given expression and puts the result 3695de4fcfddSdrh ** in register target. 369625303780Sdrh ** 36972dcef11bSdrh ** Also make a copy of the expression results into another "cache" register 36982dcef11bSdrh ** and modify the expression so that the next time it is evaluated, 36992dcef11bSdrh ** the result is a copy of the cache register. 37002dcef11bSdrh ** 37012dcef11bSdrh ** This routine is used for expressions that are used multiple 37022dcef11bSdrh ** times. They are evaluated once and the results of the expression 37032dcef11bSdrh ** are reused. 370425303780Sdrh */ 370505a86c5cSdrh void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ 370625303780Sdrh Vdbe *v = pParse->pVdbe; 370725303780Sdrh int iMem; 370805a86c5cSdrh 370905a86c5cSdrh assert( target>0 ); 371005a86c5cSdrh assert( pExpr->op!=TK_REGISTER ); 371105a86c5cSdrh sqlite3ExprCode(pParse, pExpr, target); 37122dcef11bSdrh iMem = ++pParse->nMem; 371305a86c5cSdrh sqlite3VdbeAddOp2(v, OP_Copy, target, iMem); 3714a4c3c87eSdrh exprToRegister(pExpr, iMem); 371525303780Sdrh } 37167e02e5e6Sdrh 3717678ccce8Sdrh /* 3718268380caSdrh ** Generate code that pushes the value of every element of the given 37199cbf3425Sdrh ** expression list into a sequence of registers beginning at target. 3720268380caSdrh ** 3721892d3179Sdrh ** Return the number of elements evaluated. 3722d1a01edaSdrh ** 3723d1a01edaSdrh ** The SQLITE_ECEL_DUP flag prevents the arguments from being 3724d1a01edaSdrh ** filled using OP_SCopy. OP_Copy must be used instead. 3725d1a01edaSdrh ** 3726d1a01edaSdrh ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be 3727d1a01edaSdrh ** factored out into initialization code. 3728b0df9634Sdrh ** 3729b0df9634Sdrh ** The SQLITE_ECEL_REF flag means that expressions in the list with 3730b0df9634Sdrh ** ExprList.a[].u.x.iOrderByCol>0 have already been evaluated and stored 3731b0df9634Sdrh ** in registers at srcReg, and so the value can be copied from there. 3732268380caSdrh */ 37334adee20fSdanielk1977 int sqlite3ExprCodeExprList( 3734268380caSdrh Parse *pParse, /* Parsing context */ 3735389a1adbSdrh ExprList *pList, /* The expression list to be coded */ 3736191b54cbSdrh int target, /* Where to write results */ 37375579d59fSdrh int srcReg, /* Source registers if SQLITE_ECEL_REF */ 3738d1a01edaSdrh u8 flags /* SQLITE_ECEL_* flags */ 3739268380caSdrh ){ 3740268380caSdrh struct ExprList_item *pItem; 37415579d59fSdrh int i, j, n; 3742d1a01edaSdrh u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy; 37435579d59fSdrh Vdbe *v = pParse->pVdbe; 37449d8b3072Sdrh assert( pList!=0 ); 37459cbf3425Sdrh assert( target>0 ); 3746d81a142bSdrh assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ 3747268380caSdrh n = pList->nExpr; 3748d9f158e7Sdrh if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR; 3749191b54cbSdrh for(pItem=pList->a, i=0; i<n; i++, pItem++){ 37507445ffe2Sdrh Expr *pExpr = pItem->pExpr; 37515579d59fSdrh if( (flags & SQLITE_ECEL_REF)!=0 && (j = pList->a[i].u.x.iOrderByCol)>0 ){ 37525579d59fSdrh sqlite3VdbeAddOp2(v, copyOp, j+srcReg-1, target+i); 37535579d59fSdrh }else if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){ 3754d673cddaSdrh sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0); 3755d1a01edaSdrh }else{ 37567445ffe2Sdrh int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); 3757746fd9ccSdrh if( inReg!=target+i ){ 37584eded604Sdrh VdbeOp *pOp; 37594eded604Sdrh if( copyOp==OP_Copy 37604eded604Sdrh && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy 37614eded604Sdrh && pOp->p1+pOp->p3+1==inReg 37624eded604Sdrh && pOp->p2+pOp->p3+1==target+i 37634eded604Sdrh ){ 37644eded604Sdrh pOp->p3++; 37654eded604Sdrh }else{ 37664eded604Sdrh sqlite3VdbeAddOp2(v, copyOp, inReg, target+i); 37674eded604Sdrh } 3768d1a01edaSdrh } 3769d176611bSdrh } 3770268380caSdrh } 3771f9b596ebSdrh return n; 3772268380caSdrh } 3773268380caSdrh 3774268380caSdrh /* 377536c563a2Sdrh ** Generate code for a BETWEEN operator. 377636c563a2Sdrh ** 377736c563a2Sdrh ** x BETWEEN y AND z 377836c563a2Sdrh ** 377936c563a2Sdrh ** The above is equivalent to 378036c563a2Sdrh ** 378136c563a2Sdrh ** x>=y AND x<=z 378236c563a2Sdrh ** 378336c563a2Sdrh ** Code it as such, taking care to do the common subexpression 378460ec914cSpeter.d.reid ** elimination of x. 378536c563a2Sdrh */ 378636c563a2Sdrh static void exprCodeBetween( 378736c563a2Sdrh Parse *pParse, /* Parsing and code generating context */ 378836c563a2Sdrh Expr *pExpr, /* The BETWEEN expression */ 378936c563a2Sdrh int dest, /* Jump here if the jump is taken */ 379071c57db0Sdan void (*xJumpIf)(Parse*,Expr*,int,int), 379136c563a2Sdrh int jumpIfNull /* Take the jump if the BETWEEN is NULL */ 379236c563a2Sdrh ){ 379336c563a2Sdrh Expr exprAnd; /* The AND operator in x>=y AND x<=z */ 379436c563a2Sdrh Expr compLeft; /* The x>=y term */ 379536c563a2Sdrh Expr compRight; /* The x<=z term */ 379636c563a2Sdrh Expr exprX; /* The x subexpression */ 379736c563a2Sdrh int regFree1 = 0; /* Temporary use register */ 379836c563a2Sdrh 379971c57db0Sdan memset(&compLeft, 0, sizeof(Expr)); 380071c57db0Sdan memset(&compRight, 0, sizeof(Expr)); 380171c57db0Sdan memset(&exprAnd, 0, sizeof(Expr)); 380271c57db0Sdan 380336c563a2Sdrh assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 380436c563a2Sdrh exprX = *pExpr->pLeft; 380536c563a2Sdrh exprAnd.op = TK_AND; 380636c563a2Sdrh exprAnd.pLeft = &compLeft; 380736c563a2Sdrh exprAnd.pRight = &compRight; 380836c563a2Sdrh compLeft.op = TK_GE; 380936c563a2Sdrh compLeft.pLeft = &exprX; 381036c563a2Sdrh compLeft.pRight = pExpr->x.pList->a[0].pExpr; 381136c563a2Sdrh compRight.op = TK_LE; 381236c563a2Sdrh compRight.pLeft = &exprX; 381336c563a2Sdrh compRight.pRight = pExpr->x.pList->a[1].pExpr; 381471c57db0Sdan if( (exprX.flags & EP_Vector)==0 ){ 3815a4c3c87eSdrh exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, ®Free1)); 381671c57db0Sdan } 381771c57db0Sdan if( xJumpIf ){ 381871c57db0Sdan xJumpIf(pParse, &exprAnd, dest, jumpIfNull); 381936c563a2Sdrh }else{ 382071c57db0Sdan exprX.flags |= EP_FromJoin; 382171c57db0Sdan sqlite3ExprCodeTarget(pParse, &exprAnd, dest); 382236c563a2Sdrh } 382336c563a2Sdrh sqlite3ReleaseTempReg(pParse, regFree1); 382436c563a2Sdrh 382536c563a2Sdrh /* Ensure adequate test coverage */ 382636c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); 382736c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); 382836c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); 382936c563a2Sdrh testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); 383036c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 ); 383136c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 ); 383236c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 ); 383336c563a2Sdrh testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 ); 383436c563a2Sdrh } 383536c563a2Sdrh 383636c563a2Sdrh /* 3837cce7d176Sdrh ** Generate code for a boolean expression such that a jump is made 3838cce7d176Sdrh ** to the label "dest" if the expression is true but execution 3839cce7d176Sdrh ** continues straight thru if the expression is false. 3840f5905aa7Sdrh ** 3841f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false), then 384235573356Sdrh ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. 3843f2bc013cSdrh ** 3844f2bc013cSdrh ** This code depends on the fact that certain token values (ex: TK_EQ) 3845f2bc013cSdrh ** are the same as opcode values (ex: OP_Eq) that implement the corresponding 3846f2bc013cSdrh ** operation. Special comments in vdbe.c and the mkopcodeh.awk script in 3847f2bc013cSdrh ** the make process cause these values to align. Assert()s in the code 3848f2bc013cSdrh ** below verify that the numbers are aligned correctly. 3849cce7d176Sdrh */ 38504adee20fSdanielk1977 void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3851cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3852cce7d176Sdrh int op = 0; 38532dcef11bSdrh int regFree1 = 0; 38542dcef11bSdrh int regFree2 = 0; 38552dcef11bSdrh int r1, r2; 38562dcef11bSdrh 385735573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 385848864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 385933cd4909Sdrh if( NEVER(pExpr==0) ) return; /* No way this can happen */ 3860f2bc013cSdrh op = pExpr->op; 386171c57db0Sdan switch( op | (pExpr->pLeft ? (pExpr->pLeft->flags & EP_Vector) : 0)){ 3862cce7d176Sdrh case TK_AND: { 38634adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 3864c5499befSdrh testcase( jumpIfNull==0 ); 386535573356Sdrh sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); 386654e2adb5Sdrh sqlite3ExprCachePush(pParse); 38674adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 38684adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 3869d2490904Sdrh sqlite3ExprCachePop(pParse); 3870cce7d176Sdrh break; 3871cce7d176Sdrh } 3872cce7d176Sdrh case TK_OR: { 3873c5499befSdrh testcase( jumpIfNull==0 ); 38744adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 387554e2adb5Sdrh sqlite3ExprCachePush(pParse); 38764adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); 3877d2490904Sdrh sqlite3ExprCachePop(pParse); 3878cce7d176Sdrh break; 3879cce7d176Sdrh } 3880cce7d176Sdrh case TK_NOT: { 3881c5499befSdrh testcase( jumpIfNull==0 ); 38824adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 3883cce7d176Sdrh break; 3884cce7d176Sdrh } 3885de845c2fSdrh case TK_IS: 3886de845c2fSdrh case TK_ISNOT: 3887de845c2fSdrh testcase( op==TK_IS ); 3888de845c2fSdrh testcase( op==TK_ISNOT ); 3889de845c2fSdrh op = (op==TK_IS) ? TK_EQ : TK_NE; 3890de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 3891de845c2fSdrh /* Fall thru */ 3892cce7d176Sdrh case TK_LT: 3893cce7d176Sdrh case TK_LE: 3894cce7d176Sdrh case TK_GT: 3895cce7d176Sdrh case TK_GE: 3896cce7d176Sdrh case TK_NE: 38970ac65892Sdrh case TK_EQ: { 3898c5499befSdrh testcase( jumpIfNull==0 ); 3899b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 3900b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 390135573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 39022dcef11bSdrh r1, r2, dest, jumpIfNull); 39037d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 39047d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 39057d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 39067d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 3907de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 3908de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 3909de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 3910de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 3911de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 3912de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 39136a2fe093Sdrh testcase( regFree1==0 ); 39146a2fe093Sdrh testcase( regFree2==0 ); 39156a2fe093Sdrh break; 39166a2fe093Sdrh } 3917cce7d176Sdrh case TK_ISNULL: 3918cce7d176Sdrh case TK_NOTNULL: { 39197d176105Sdrh assert( TK_ISNULL==OP_IsNull ); testcase( op==TK_ISNULL ); 39207d176105Sdrh assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL ); 39212dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 39222dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 39237d176105Sdrh VdbeCoverageIf(v, op==TK_ISNULL); 39247d176105Sdrh VdbeCoverageIf(v, op==TK_NOTNULL); 3925c5499befSdrh testcase( regFree1==0 ); 3926cce7d176Sdrh break; 3927cce7d176Sdrh } 3928fef5208cSdrh case TK_BETWEEN: { 39295c03f30aSdrh testcase( jumpIfNull==0 ); 393071c57db0Sdan exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfTrue, jumpIfNull); 3931fef5208cSdrh break; 3932fef5208cSdrh } 3933bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 3934e3365e6cSdrh case TK_IN: { 3935e3365e6cSdrh int destIfFalse = sqlite3VdbeMakeLabel(v); 3936e3365e6cSdrh int destIfNull = jumpIfNull ? dest : destIfFalse; 3937e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); 3938076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3939e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfFalse); 3940e3365e6cSdrh break; 3941e3365e6cSdrh } 3942bb201344Sshaneh #endif 3943cce7d176Sdrh default: { 3944991a1985Sdrh if( exprAlwaysTrue(pExpr) ){ 3945076e85f5Sdrh sqlite3VdbeGoto(v, dest); 3946991a1985Sdrh }else if( exprAlwaysFalse(pExpr) ){ 3947991a1985Sdrh /* No-op */ 3948991a1985Sdrh }else{ 39492dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 39502dcef11bSdrh sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); 3951688852abSdrh VdbeCoverage(v); 3952c5499befSdrh testcase( regFree1==0 ); 3953c5499befSdrh testcase( jumpIfNull==0 ); 3954991a1985Sdrh } 3955cce7d176Sdrh break; 3956cce7d176Sdrh } 3957cce7d176Sdrh } 39582dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 39592dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 3960cce7d176Sdrh } 3961cce7d176Sdrh 3962cce7d176Sdrh /* 396366b89c8fSdrh ** Generate code for a boolean expression such that a jump is made 3964cce7d176Sdrh ** to the label "dest" if the expression is false but execution 3965cce7d176Sdrh ** continues straight thru if the expression is true. 3966f5905aa7Sdrh ** 3967f5905aa7Sdrh ** If the expression evaluates to NULL (neither true nor false) then 396835573356Sdrh ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull 396935573356Sdrh ** is 0. 3970cce7d176Sdrh */ 39714adee20fSdanielk1977 void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ 3972cce7d176Sdrh Vdbe *v = pParse->pVdbe; 3973cce7d176Sdrh int op = 0; 39742dcef11bSdrh int regFree1 = 0; 39752dcef11bSdrh int regFree2 = 0; 39762dcef11bSdrh int r1, r2; 39772dcef11bSdrh 397835573356Sdrh assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); 397948864df9Smistachkin if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */ 398033cd4909Sdrh if( pExpr==0 ) return; 3981f2bc013cSdrh 3982f2bc013cSdrh /* The value of pExpr->op and op are related as follows: 3983f2bc013cSdrh ** 3984f2bc013cSdrh ** pExpr->op op 3985f2bc013cSdrh ** --------- ---------- 3986f2bc013cSdrh ** TK_ISNULL OP_NotNull 3987f2bc013cSdrh ** TK_NOTNULL OP_IsNull 3988f2bc013cSdrh ** TK_NE OP_Eq 3989f2bc013cSdrh ** TK_EQ OP_Ne 3990f2bc013cSdrh ** TK_GT OP_Le 3991f2bc013cSdrh ** TK_LE OP_Gt 3992f2bc013cSdrh ** TK_GE OP_Lt 3993f2bc013cSdrh ** TK_LT OP_Ge 3994f2bc013cSdrh ** 3995f2bc013cSdrh ** For other values of pExpr->op, op is undefined and unused. 3996f2bc013cSdrh ** The value of TK_ and OP_ constants are arranged such that we 3997f2bc013cSdrh ** can compute the mapping above using the following expression. 3998f2bc013cSdrh ** Assert()s verify that the computation is correct. 3999f2bc013cSdrh */ 4000f2bc013cSdrh op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); 4001f2bc013cSdrh 4002f2bc013cSdrh /* Verify correct alignment of TK_ and OP_ constants 4003f2bc013cSdrh */ 4004f2bc013cSdrh assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); 4005f2bc013cSdrh assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); 4006f2bc013cSdrh assert( pExpr->op!=TK_NE || op==OP_Eq ); 4007f2bc013cSdrh assert( pExpr->op!=TK_EQ || op==OP_Ne ); 4008f2bc013cSdrh assert( pExpr->op!=TK_LT || op==OP_Ge ); 4009f2bc013cSdrh assert( pExpr->op!=TK_LE || op==OP_Gt ); 4010f2bc013cSdrh assert( pExpr->op!=TK_GT || op==OP_Le ); 4011f2bc013cSdrh assert( pExpr->op!=TK_GE || op==OP_Lt ); 4012f2bc013cSdrh 401371c57db0Sdan switch( pExpr->op | (pExpr->pLeft ? (pExpr->pLeft->flags & EP_Vector) : 0)){ 4014cce7d176Sdrh case TK_AND: { 4015c5499befSdrh testcase( jumpIfNull==0 ); 40164adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); 401754e2adb5Sdrh sqlite3ExprCachePush(pParse); 40184adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 4019d2490904Sdrh sqlite3ExprCachePop(pParse); 4020cce7d176Sdrh break; 4021cce7d176Sdrh } 4022cce7d176Sdrh case TK_OR: { 40234adee20fSdanielk1977 int d2 = sqlite3VdbeMakeLabel(v); 4024c5499befSdrh testcase( jumpIfNull==0 ); 402535573356Sdrh sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); 402654e2adb5Sdrh sqlite3ExprCachePush(pParse); 40274adee20fSdanielk1977 sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); 40284adee20fSdanielk1977 sqlite3VdbeResolveLabel(v, d2); 4029d2490904Sdrh sqlite3ExprCachePop(pParse); 4030cce7d176Sdrh break; 4031cce7d176Sdrh } 4032cce7d176Sdrh case TK_NOT: { 40335c03f30aSdrh testcase( jumpIfNull==0 ); 40344adee20fSdanielk1977 sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); 4035cce7d176Sdrh break; 4036cce7d176Sdrh } 4037de845c2fSdrh case TK_IS: 4038de845c2fSdrh case TK_ISNOT: 4039de845c2fSdrh testcase( pExpr->op==TK_IS ); 4040de845c2fSdrh testcase( pExpr->op==TK_ISNOT ); 4041de845c2fSdrh op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; 4042de845c2fSdrh jumpIfNull = SQLITE_NULLEQ; 4043de845c2fSdrh /* Fall thru */ 4044cce7d176Sdrh case TK_LT: 4045cce7d176Sdrh case TK_LE: 4046cce7d176Sdrh case TK_GT: 4047cce7d176Sdrh case TK_GE: 4048cce7d176Sdrh case TK_NE: 4049cce7d176Sdrh case TK_EQ: { 4050c5499befSdrh testcase( jumpIfNull==0 ); 4051b6da74ebSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 4052b6da74ebSdrh r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); 405335573356Sdrh codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, 40542dcef11bSdrh r1, r2, dest, jumpIfNull); 40557d176105Sdrh assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt); 40567d176105Sdrh assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le); 40577d176105Sdrh assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt); 40587d176105Sdrh assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge); 4059de845c2fSdrh assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); 4060de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull!=SQLITE_NULLEQ); 4061de845c2fSdrh VdbeCoverageIf(v, op==OP_Eq && jumpIfNull==SQLITE_NULLEQ); 4062de845c2fSdrh assert(TK_NE==OP_Ne); testcase(op==OP_Ne); 4063de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull!=SQLITE_NULLEQ); 4064de845c2fSdrh VdbeCoverageIf(v, op==OP_Ne && jumpIfNull==SQLITE_NULLEQ); 40656a2fe093Sdrh testcase( regFree1==0 ); 40666a2fe093Sdrh testcase( regFree2==0 ); 40676a2fe093Sdrh break; 40686a2fe093Sdrh } 4069cce7d176Sdrh case TK_ISNULL: 4070cce7d176Sdrh case TK_NOTNULL: { 40712dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); 40722dcef11bSdrh sqlite3VdbeAddOp2(v, op, r1, dest); 40737d176105Sdrh testcase( op==TK_ISNULL ); VdbeCoverageIf(v, op==TK_ISNULL); 40747d176105Sdrh testcase( op==TK_NOTNULL ); VdbeCoverageIf(v, op==TK_NOTNULL); 4075c5499befSdrh testcase( regFree1==0 ); 4076cce7d176Sdrh break; 4077cce7d176Sdrh } 4078fef5208cSdrh case TK_BETWEEN: { 40795c03f30aSdrh testcase( jumpIfNull==0 ); 408071c57db0Sdan exprCodeBetween(pParse, pExpr, dest, sqlite3ExprIfFalse, jumpIfNull); 4081fef5208cSdrh break; 4082fef5208cSdrh } 4083bb201344Sshaneh #ifndef SQLITE_OMIT_SUBQUERY 4084e3365e6cSdrh case TK_IN: { 4085e3365e6cSdrh if( jumpIfNull ){ 4086e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, dest); 4087e3365e6cSdrh }else{ 4088e3365e6cSdrh int destIfNull = sqlite3VdbeMakeLabel(v); 4089e3365e6cSdrh sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); 4090e3365e6cSdrh sqlite3VdbeResolveLabel(v, destIfNull); 4091e3365e6cSdrh } 4092e3365e6cSdrh break; 4093e3365e6cSdrh } 4094bb201344Sshaneh #endif 4095cce7d176Sdrh default: { 4096991a1985Sdrh if( exprAlwaysFalse(pExpr) ){ 4097076e85f5Sdrh sqlite3VdbeGoto(v, dest); 4098991a1985Sdrh }else if( exprAlwaysTrue(pExpr) ){ 4099991a1985Sdrh /* no-op */ 4100991a1985Sdrh }else{ 41012dcef11bSdrh r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); 41022dcef11bSdrh sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); 4103688852abSdrh VdbeCoverage(v); 4104c5499befSdrh testcase( regFree1==0 ); 4105c5499befSdrh testcase( jumpIfNull==0 ); 4106991a1985Sdrh } 4107cce7d176Sdrh break; 4108cce7d176Sdrh } 4109cce7d176Sdrh } 41102dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree1); 41112dcef11bSdrh sqlite3ReleaseTempReg(pParse, regFree2); 4112cce7d176Sdrh } 41132282792aSdrh 41142282792aSdrh /* 411572bc8208Sdrh ** Like sqlite3ExprIfFalse() except that a copy is made of pExpr before 411672bc8208Sdrh ** code generation, and that copy is deleted after code generation. This 411772bc8208Sdrh ** ensures that the original pExpr is unchanged. 411872bc8208Sdrh */ 411972bc8208Sdrh void sqlite3ExprIfFalseDup(Parse *pParse, Expr *pExpr, int dest,int jumpIfNull){ 412072bc8208Sdrh sqlite3 *db = pParse->db; 412172bc8208Sdrh Expr *pCopy = sqlite3ExprDup(db, pExpr, 0); 412272bc8208Sdrh if( db->mallocFailed==0 ){ 412372bc8208Sdrh sqlite3ExprIfFalse(pParse, pCopy, dest, jumpIfNull); 412472bc8208Sdrh } 412572bc8208Sdrh sqlite3ExprDelete(db, pCopy); 412672bc8208Sdrh } 412772bc8208Sdrh 412872bc8208Sdrh 412972bc8208Sdrh /* 41301d9da70aSdrh ** Do a deep comparison of two expression trees. Return 0 if the two 41311d9da70aSdrh ** expressions are completely identical. Return 1 if they differ only 41321d9da70aSdrh ** by a COLLATE operator at the top level. Return 2 if there are differences 41331d9da70aSdrh ** other than the top-level COLLATE operator. 4134d40aab0eSdrh ** 4135619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4136619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4137619a1305Sdrh ** 413866518ca7Sdrh ** The pA side might be using TK_REGISTER. If that is the case and pB is 413966518ca7Sdrh ** not using TK_REGISTER but is otherwise equivalent, then still return 0. 414066518ca7Sdrh ** 41411d9da70aSdrh ** Sometimes this routine will return 2 even if the two expressions 4142d40aab0eSdrh ** really are equivalent. If we cannot prove that the expressions are 41431d9da70aSdrh ** identical, we return 2 just to be safe. So if this routine 41441d9da70aSdrh ** returns 2, then you do not really know for certain if the two 41451d9da70aSdrh ** expressions are the same. But if you get a 0 or 1 return, then you 4146d40aab0eSdrh ** can be sure the expressions are the same. In the places where 41471d9da70aSdrh ** this routine is used, it does not hurt to get an extra 2 - that 4148d40aab0eSdrh ** just might result in some slightly slower code. But returning 41491d9da70aSdrh ** an incorrect 0 or 1 could lead to a malfunction. 41502282792aSdrh */ 4151619a1305Sdrh int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){ 415210d1edf0Sdrh u32 combinedFlags; 41534b202ae2Sdanielk1977 if( pA==0 || pB==0 ){ 41541d9da70aSdrh return pB==pA ? 0 : 2; 41552282792aSdrh } 415610d1edf0Sdrh combinedFlags = pA->flags | pB->flags; 415710d1edf0Sdrh if( combinedFlags & EP_IntValue ){ 415810d1edf0Sdrh if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){ 415910d1edf0Sdrh return 0; 416010d1edf0Sdrh } 41611d9da70aSdrh return 2; 41626ab3a2ecSdanielk1977 } 4163c2acc4e4Sdrh if( pA->op!=pB->op ){ 4164619a1305Sdrh if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){ 4165ae80ddeaSdrh return 1; 4166ae80ddeaSdrh } 4167619a1305Sdrh if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){ 4168ae80ddeaSdrh return 1; 4169ae80ddeaSdrh } 4170ae80ddeaSdrh return 2; 4171ae80ddeaSdrh } 41722edc5fd7Sdrh if( pA->op!=TK_COLUMN && pA->op!=TK_AGG_COLUMN && pA->u.zToken ){ 4173390b88a4Sdrh if( pA->op==TK_FUNCTION ){ 4174390b88a4Sdrh if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ) return 2; 4175390b88a4Sdrh }else if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){ 417610d1edf0Sdrh return pA->op==TK_COLLATE ? 1 : 2; 417710d1edf0Sdrh } 417810d1edf0Sdrh } 417910d1edf0Sdrh if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; 418085f8aa79Sdrh if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){ 418110d1edf0Sdrh if( combinedFlags & EP_xIsSelect ) return 2; 4182619a1305Sdrh if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2; 4183619a1305Sdrh if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2; 4184619a1305Sdrh if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2; 41857693c42fSdrh if( ALWAYS((combinedFlags & EP_Reduced)==0) && pA->op!=TK_STRING ){ 4186619a1305Sdrh if( pA->iColumn!=pB->iColumn ) return 2; 418766518ca7Sdrh if( pA->iTable!=pB->iTable 418885f8aa79Sdrh && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2; 41891d9da70aSdrh } 41901d9da70aSdrh } 41912646da7eSdrh return 0; 41922646da7eSdrh } 41932282792aSdrh 41948c6f666bSdrh /* 41958c6f666bSdrh ** Compare two ExprList objects. Return 0 if they are identical and 41968c6f666bSdrh ** non-zero if they differ in any way. 41978c6f666bSdrh ** 4198619a1305Sdrh ** If any subelement of pB has Expr.iTable==(-1) then it is allowed 4199619a1305Sdrh ** to compare equal to an equivalent element in pA with Expr.iTable==iTab. 4200619a1305Sdrh ** 42018c6f666bSdrh ** This routine might return non-zero for equivalent ExprLists. The 42028c6f666bSdrh ** only consequence will be disabled optimizations. But this routine 42038c6f666bSdrh ** must never return 0 if the two ExprList objects are different, or 42048c6f666bSdrh ** a malfunction will result. 42058c6f666bSdrh ** 42068c6f666bSdrh ** Two NULL pointers are considered to be the same. But a NULL pointer 42078c6f666bSdrh ** always differs from a non-NULL pointer. 42088c6f666bSdrh */ 4209619a1305Sdrh int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){ 42108c6f666bSdrh int i; 42118c6f666bSdrh if( pA==0 && pB==0 ) return 0; 42128c6f666bSdrh if( pA==0 || pB==0 ) return 1; 42138c6f666bSdrh if( pA->nExpr!=pB->nExpr ) return 1; 42148c6f666bSdrh for(i=0; i<pA->nExpr; i++){ 42158c6f666bSdrh Expr *pExprA = pA->a[i].pExpr; 42168c6f666bSdrh Expr *pExprB = pB->a[i].pExpr; 42178c6f666bSdrh if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; 4218619a1305Sdrh if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1; 42198c6f666bSdrh } 42208c6f666bSdrh return 0; 42218c6f666bSdrh } 422213449892Sdrh 42232282792aSdrh /* 42244bd5f73fSdrh ** Return true if we can prove the pE2 will always be true if pE1 is 42254bd5f73fSdrh ** true. Return false if we cannot complete the proof or if pE2 might 42264bd5f73fSdrh ** be false. Examples: 42274bd5f73fSdrh ** 4228619a1305Sdrh ** pE1: x==5 pE2: x==5 Result: true 42294bd5f73fSdrh ** pE1: x>0 pE2: x==5 Result: false 4230619a1305Sdrh ** pE1: x=21 pE2: x=21 OR y=43 Result: true 42314bd5f73fSdrh ** pE1: x!=123 pE2: x IS NOT NULL Result: true 4232619a1305Sdrh ** pE1: x!=?1 pE2: x IS NOT NULL Result: true 4233619a1305Sdrh ** pE1: x IS NULL pE2: x IS NOT NULL Result: false 4234619a1305Sdrh ** pE1: x IS ?2 pE2: x IS NOT NULL Reuslt: false 42354bd5f73fSdrh ** 42364bd5f73fSdrh ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has 42374bd5f73fSdrh ** Expr.iTable<0 then assume a table number given by iTab. 42384bd5f73fSdrh ** 42394bd5f73fSdrh ** When in doubt, return false. Returning true might give a performance 42404bd5f73fSdrh ** improvement. Returning false might cause a performance reduction, but 42414bd5f73fSdrh ** it will always give the correct answer and is hence always safe. 42424bd5f73fSdrh */ 42434bd5f73fSdrh int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){ 4244619a1305Sdrh if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){ 4245619a1305Sdrh return 1; 4246619a1305Sdrh } 4247619a1305Sdrh if( pE2->op==TK_OR 4248619a1305Sdrh && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab) 4249619a1305Sdrh || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) ) 4250619a1305Sdrh ){ 4251619a1305Sdrh return 1; 4252619a1305Sdrh } 4253619a1305Sdrh if( pE2->op==TK_NOTNULL 4254619a1305Sdrh && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0 4255619a1305Sdrh && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS) 4256619a1305Sdrh ){ 4257619a1305Sdrh return 1; 4258619a1305Sdrh } 4259619a1305Sdrh return 0; 42604bd5f73fSdrh } 42614bd5f73fSdrh 42624bd5f73fSdrh /* 4263030796dfSdrh ** An instance of the following structure is used by the tree walker 4264030796dfSdrh ** to count references to table columns in the arguments of an 4265ed551b95Sdrh ** aggregate function, in order to implement the 4266ed551b95Sdrh ** sqlite3FunctionThisSrc() routine. 4267374fdce4Sdrh */ 4268030796dfSdrh struct SrcCount { 4269030796dfSdrh SrcList *pSrc; /* One particular FROM clause in a nested query */ 4270030796dfSdrh int nThis; /* Number of references to columns in pSrcList */ 4271030796dfSdrh int nOther; /* Number of references to columns in other FROM clauses */ 4272030796dfSdrh }; 4273030796dfSdrh 4274030796dfSdrh /* 4275030796dfSdrh ** Count the number of references to columns. 4276030796dfSdrh */ 4277030796dfSdrh static int exprSrcCount(Walker *pWalker, Expr *pExpr){ 4278fb0a6081Sdrh /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc() 4279fb0a6081Sdrh ** is always called before sqlite3ExprAnalyzeAggregates() and so the 4280fb0a6081Sdrh ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN. If 4281fb0a6081Sdrh ** sqlite3FunctionUsesThisSrc() is used differently in the future, the 4282fb0a6081Sdrh ** NEVER() will need to be removed. */ 4283fb0a6081Sdrh if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){ 4284374fdce4Sdrh int i; 4285030796dfSdrh struct SrcCount *p = pWalker->u.pSrcCount; 4286030796dfSdrh SrcList *pSrc = p->pSrc; 4287655814d2Sdrh int nSrc = pSrc ? pSrc->nSrc : 0; 4288655814d2Sdrh for(i=0; i<nSrc; i++){ 4289030796dfSdrh if( pExpr->iTable==pSrc->a[i].iCursor ) break; 4290374fdce4Sdrh } 4291655814d2Sdrh if( i<nSrc ){ 4292030796dfSdrh p->nThis++; 4293374fdce4Sdrh }else{ 4294030796dfSdrh p->nOther++; 4295374fdce4Sdrh } 4296374fdce4Sdrh } 4297030796dfSdrh return WRC_Continue; 4298030796dfSdrh } 4299374fdce4Sdrh 4300374fdce4Sdrh /* 4301030796dfSdrh ** Determine if any of the arguments to the pExpr Function reference 4302030796dfSdrh ** pSrcList. Return true if they do. Also return true if the function 4303030796dfSdrh ** has no arguments or has only constant arguments. Return false if pExpr 4304030796dfSdrh ** references columns but not columns of tables found in pSrcList. 4305374fdce4Sdrh */ 4306030796dfSdrh int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){ 4307374fdce4Sdrh Walker w; 4308030796dfSdrh struct SrcCount cnt; 4309374fdce4Sdrh assert( pExpr->op==TK_AGG_FUNCTION ); 4310374fdce4Sdrh memset(&w, 0, sizeof(w)); 4311030796dfSdrh w.xExprCallback = exprSrcCount; 4312030796dfSdrh w.u.pSrcCount = &cnt; 4313030796dfSdrh cnt.pSrc = pSrcList; 4314030796dfSdrh cnt.nThis = 0; 4315030796dfSdrh cnt.nOther = 0; 4316030796dfSdrh sqlite3WalkExprList(&w, pExpr->x.pList); 4317030796dfSdrh return cnt.nThis>0 || cnt.nOther==0; 4318374fdce4Sdrh } 4319374fdce4Sdrh 4320374fdce4Sdrh /* 432113449892Sdrh ** Add a new element to the pAggInfo->aCol[] array. Return the index of 432213449892Sdrh ** the new element. Return a negative number if malloc fails. 43232282792aSdrh */ 432417435752Sdrh static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ 432513449892Sdrh int i; 4326cf643729Sdrh pInfo->aCol = sqlite3ArrayAllocate( 432717435752Sdrh db, 4328cf643729Sdrh pInfo->aCol, 4329cf643729Sdrh sizeof(pInfo->aCol[0]), 4330cf643729Sdrh &pInfo->nColumn, 4331cf643729Sdrh &i 4332cf643729Sdrh ); 433313449892Sdrh return i; 43342282792aSdrh } 433513449892Sdrh 433613449892Sdrh /* 433713449892Sdrh ** Add a new element to the pAggInfo->aFunc[] array. Return the index of 433813449892Sdrh ** the new element. Return a negative number if malloc fails. 433913449892Sdrh */ 434017435752Sdrh static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ 434113449892Sdrh int i; 4342cf643729Sdrh pInfo->aFunc = sqlite3ArrayAllocate( 434317435752Sdrh db, 4344cf643729Sdrh pInfo->aFunc, 4345cf643729Sdrh sizeof(pInfo->aFunc[0]), 4346cf643729Sdrh &pInfo->nFunc, 4347cf643729Sdrh &i 4348cf643729Sdrh ); 434913449892Sdrh return i; 43502282792aSdrh } 43512282792aSdrh 43522282792aSdrh /* 43537d10d5a6Sdrh ** This is the xExprCallback for a tree walker. It is used to 43547d10d5a6Sdrh ** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates 4355626a879aSdrh ** for additional information. 43562282792aSdrh */ 43577d10d5a6Sdrh static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ 43582282792aSdrh int i; 43597d10d5a6Sdrh NameContext *pNC = pWalker->u.pNC; 4360a58fdfb1Sdanielk1977 Parse *pParse = pNC->pParse; 4361a58fdfb1Sdanielk1977 SrcList *pSrcList = pNC->pSrcList; 436213449892Sdrh AggInfo *pAggInfo = pNC->pAggInfo; 436313449892Sdrh 43642282792aSdrh switch( pExpr->op ){ 436589c69d00Sdrh case TK_AGG_COLUMN: 4366967e8b73Sdrh case TK_COLUMN: { 43678b213899Sdrh testcase( pExpr->op==TK_AGG_COLUMN ); 43688b213899Sdrh testcase( pExpr->op==TK_COLUMN ); 436913449892Sdrh /* Check to see if the column is in one of the tables in the FROM 437013449892Sdrh ** clause of the aggregate query */ 437120bc393cSdrh if( ALWAYS(pSrcList!=0) ){ 437213449892Sdrh struct SrcList_item *pItem = pSrcList->a; 437313449892Sdrh for(i=0; i<pSrcList->nSrc; i++, pItem++){ 437413449892Sdrh struct AggInfo_col *pCol; 4375c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 437613449892Sdrh if( pExpr->iTable==pItem->iCursor ){ 437713449892Sdrh /* If we reach this point, it means that pExpr refers to a table 437813449892Sdrh ** that is in the FROM clause of the aggregate query. 437913449892Sdrh ** 438013449892Sdrh ** Make an entry for the column in pAggInfo->aCol[] if there 438113449892Sdrh ** is not an entry there already. 438213449892Sdrh */ 43837f906d63Sdrh int k; 438413449892Sdrh pCol = pAggInfo->aCol; 43857f906d63Sdrh for(k=0; k<pAggInfo->nColumn; k++, pCol++){ 438613449892Sdrh if( pCol->iTable==pExpr->iTable && 438713449892Sdrh pCol->iColumn==pExpr->iColumn ){ 43882282792aSdrh break; 43892282792aSdrh } 43902282792aSdrh } 43911e536953Sdanielk1977 if( (k>=pAggInfo->nColumn) 43921e536953Sdanielk1977 && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 43931e536953Sdanielk1977 ){ 43947f906d63Sdrh pCol = &pAggInfo->aCol[k]; 43950817d0dfSdanielk1977 pCol->pTab = pExpr->pTab; 439613449892Sdrh pCol->iTable = pExpr->iTable; 439713449892Sdrh pCol->iColumn = pExpr->iColumn; 43980a07c107Sdrh pCol->iMem = ++pParse->nMem; 439913449892Sdrh pCol->iSorterColumn = -1; 44005774b806Sdrh pCol->pExpr = pExpr; 440113449892Sdrh if( pAggInfo->pGroupBy ){ 440213449892Sdrh int j, n; 440313449892Sdrh ExprList *pGB = pAggInfo->pGroupBy; 440413449892Sdrh struct ExprList_item *pTerm = pGB->a; 440513449892Sdrh n = pGB->nExpr; 440613449892Sdrh for(j=0; j<n; j++, pTerm++){ 440713449892Sdrh Expr *pE = pTerm->pExpr; 440813449892Sdrh if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && 440913449892Sdrh pE->iColumn==pExpr->iColumn ){ 441013449892Sdrh pCol->iSorterColumn = j; 441113449892Sdrh break; 44122282792aSdrh } 441313449892Sdrh } 441413449892Sdrh } 441513449892Sdrh if( pCol->iSorterColumn<0 ){ 441613449892Sdrh pCol->iSorterColumn = pAggInfo->nSortingColumn++; 441713449892Sdrh } 441813449892Sdrh } 441913449892Sdrh /* There is now an entry for pExpr in pAggInfo->aCol[] (either 442013449892Sdrh ** because it was there before or because we just created it). 442113449892Sdrh ** Convert the pExpr to be a TK_AGG_COLUMN referring to that 442213449892Sdrh ** pAggInfo->aCol[] entry. 442313449892Sdrh */ 4424ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 442513449892Sdrh pExpr->pAggInfo = pAggInfo; 442613449892Sdrh pExpr->op = TK_AGG_COLUMN; 4427cf697396Sshane pExpr->iAgg = (i16)k; 442813449892Sdrh break; 442913449892Sdrh } /* endif pExpr->iTable==pItem->iCursor */ 443013449892Sdrh } /* end loop over pSrcList */ 4431a58fdfb1Sdanielk1977 } 44327d10d5a6Sdrh return WRC_Prune; 44332282792aSdrh } 44342282792aSdrh case TK_AGG_FUNCTION: { 44353a8c4be7Sdrh if( (pNC->ncFlags & NC_InAggFunc)==0 4436ed551b95Sdrh && pWalker->walkerDepth==pExpr->op2 44373a8c4be7Sdrh ){ 443813449892Sdrh /* Check to see if pExpr is a duplicate of another aggregate 443913449892Sdrh ** function that is already in the pAggInfo structure 444013449892Sdrh */ 444113449892Sdrh struct AggInfo_func *pItem = pAggInfo->aFunc; 444213449892Sdrh for(i=0; i<pAggInfo->nFunc; i++, pItem++){ 4443619a1305Sdrh if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){ 44442282792aSdrh break; 44452282792aSdrh } 44462282792aSdrh } 444713449892Sdrh if( i>=pAggInfo->nFunc ){ 444813449892Sdrh /* pExpr is original. Make a new entry in pAggInfo->aFunc[] 444913449892Sdrh */ 445014db2665Sdanielk1977 u8 enc = ENC(pParse->db); 44511e536953Sdanielk1977 i = addAggInfoFunc(pParse->db, pAggInfo); 445213449892Sdrh if( i>=0 ){ 44536ab3a2ecSdanielk1977 assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); 445413449892Sdrh pItem = &pAggInfo->aFunc[i]; 445513449892Sdrh pItem->pExpr = pExpr; 44560a07c107Sdrh pItem->iMem = ++pParse->nMem; 445733e619fcSdrh assert( !ExprHasProperty(pExpr, EP_IntValue) ); 445813449892Sdrh pItem->pFunc = sqlite3FindFunction(pParse->db, 445980738d9cSdrh pExpr->u.zToken, 44606ab3a2ecSdanielk1977 pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); 4461fd357974Sdrh if( pExpr->flags & EP_Distinct ){ 4462fd357974Sdrh pItem->iDistinct = pParse->nTab++; 4463fd357974Sdrh }else{ 4464fd357974Sdrh pItem->iDistinct = -1; 4465fd357974Sdrh } 44662282792aSdrh } 446713449892Sdrh } 446813449892Sdrh /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry 446913449892Sdrh */ 4470c5cd1249Sdrh assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) ); 4471ebb6a65dSdrh ExprSetVVAProperty(pExpr, EP_NoReduce); 4472cf697396Sshane pExpr->iAgg = (i16)i; 447313449892Sdrh pExpr->pAggInfo = pAggInfo; 44743a8c4be7Sdrh return WRC_Prune; 44756e83a57fSdrh }else{ 44766e83a57fSdrh return WRC_Continue; 44776e83a57fSdrh } 44782282792aSdrh } 4479a58fdfb1Sdanielk1977 } 44807d10d5a6Sdrh return WRC_Continue; 44817d10d5a6Sdrh } 44827d10d5a6Sdrh static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ 4483d5a336efSdrh UNUSED_PARAMETER(pWalker); 4484d5a336efSdrh UNUSED_PARAMETER(pSelect); 44857d10d5a6Sdrh return WRC_Continue; 4486a58fdfb1Sdanielk1977 } 4487626a879aSdrh 4488626a879aSdrh /* 4489e8abb4caSdrh ** Analyze the pExpr expression looking for aggregate functions and 4490e8abb4caSdrh ** for variables that need to be added to AggInfo object that pNC->pAggInfo 4491e8abb4caSdrh ** points to. Additional entries are made on the AggInfo object as 4492e8abb4caSdrh ** necessary. 4493626a879aSdrh ** 4494626a879aSdrh ** This routine should only be called after the expression has been 44957d10d5a6Sdrh ** analyzed by sqlite3ResolveExprNames(). 4496626a879aSdrh */ 4497d2b3e23bSdrh void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ 44987d10d5a6Sdrh Walker w; 4499374fdce4Sdrh memset(&w, 0, sizeof(w)); 45007d10d5a6Sdrh w.xExprCallback = analyzeAggregate; 45017d10d5a6Sdrh w.xSelectCallback = analyzeAggregatesInSelect; 45027d10d5a6Sdrh w.u.pNC = pNC; 450320bc393cSdrh assert( pNC->pSrcList!=0 ); 45047d10d5a6Sdrh sqlite3WalkExpr(&w, pExpr); 45052282792aSdrh } 45065d9a4af9Sdrh 45075d9a4af9Sdrh /* 45085d9a4af9Sdrh ** Call sqlite3ExprAnalyzeAggregates() for every expression in an 45095d9a4af9Sdrh ** expression list. Return the number of errors. 45105d9a4af9Sdrh ** 45115d9a4af9Sdrh ** If an error is found, the analysis is cut short. 45125d9a4af9Sdrh */ 4513d2b3e23bSdrh void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ 45145d9a4af9Sdrh struct ExprList_item *pItem; 45155d9a4af9Sdrh int i; 45165d9a4af9Sdrh if( pList ){ 4517d2b3e23bSdrh for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){ 4518d2b3e23bSdrh sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); 45195d9a4af9Sdrh } 45205d9a4af9Sdrh } 45215d9a4af9Sdrh } 4522892d3179Sdrh 4523892d3179Sdrh /* 4524ceea3321Sdrh ** Allocate a single new register for use to hold some intermediate result. 4525892d3179Sdrh */ 4526892d3179Sdrh int sqlite3GetTempReg(Parse *pParse){ 4527e55cbd72Sdrh if( pParse->nTempReg==0 ){ 4528892d3179Sdrh return ++pParse->nMem; 4529892d3179Sdrh } 45302f425f6bSdanielk1977 return pParse->aTempReg[--pParse->nTempReg]; 4531892d3179Sdrh } 4532ceea3321Sdrh 4533ceea3321Sdrh /* 4534ceea3321Sdrh ** Deallocate a register, making available for reuse for some other 4535ceea3321Sdrh ** purpose. 4536ceea3321Sdrh ** 4537ceea3321Sdrh ** If a register is currently being used by the column cache, then 453860ec914cSpeter.d.reid ** the deallocation is deferred until the column cache line that uses 4539ceea3321Sdrh ** the register becomes stale. 4540ceea3321Sdrh */ 4541892d3179Sdrh void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ 45422dcef11bSdrh if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){ 4543ceea3321Sdrh int i; 4544ceea3321Sdrh struct yColCache *p; 4545ceea3321Sdrh for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){ 4546ceea3321Sdrh if( p->iReg==iReg ){ 4547ceea3321Sdrh p->tempReg = 1; 4548ceea3321Sdrh return; 4549ceea3321Sdrh } 4550ceea3321Sdrh } 4551892d3179Sdrh pParse->aTempReg[pParse->nTempReg++] = iReg; 4552892d3179Sdrh } 4553892d3179Sdrh } 4554892d3179Sdrh 4555892d3179Sdrh /* 4556892d3179Sdrh ** Allocate or deallocate a block of nReg consecutive registers 4557892d3179Sdrh */ 4558892d3179Sdrh int sqlite3GetTempRange(Parse *pParse, int nReg){ 4559e55cbd72Sdrh int i, n; 4560892d3179Sdrh i = pParse->iRangeReg; 4561e55cbd72Sdrh n = pParse->nRangeReg; 4562f49f3523Sdrh if( nReg<=n ){ 4563f49f3523Sdrh assert( !usedAsColumnCache(pParse, i, i+n-1) ); 4564892d3179Sdrh pParse->iRangeReg += nReg; 4565892d3179Sdrh pParse->nRangeReg -= nReg; 4566892d3179Sdrh }else{ 4567892d3179Sdrh i = pParse->nMem+1; 4568892d3179Sdrh pParse->nMem += nReg; 4569892d3179Sdrh } 4570892d3179Sdrh return i; 4571892d3179Sdrh } 4572892d3179Sdrh void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ 4573f49f3523Sdrh sqlite3ExprCacheRemove(pParse, iReg, nReg); 4574892d3179Sdrh if( nReg>pParse->nRangeReg ){ 4575892d3179Sdrh pParse->nRangeReg = nReg; 4576892d3179Sdrh pParse->iRangeReg = iReg; 4577892d3179Sdrh } 4578892d3179Sdrh } 4579cdc69557Sdrh 4580cdc69557Sdrh /* 4581cdc69557Sdrh ** Mark all temporary registers as being unavailable for reuse. 4582cdc69557Sdrh */ 4583cdc69557Sdrh void sqlite3ClearTempRegCache(Parse *pParse){ 4584cdc69557Sdrh pParse->nTempReg = 0; 4585cdc69557Sdrh pParse->nRangeReg = 0; 4586cdc69557Sdrh } 4587bb9b5f26Sdrh 4588bb9b5f26Sdrh /* 4589bb9b5f26Sdrh ** Validate that no temporary register falls within the range of 4590bb9b5f26Sdrh ** iFirst..iLast, inclusive. This routine is only call from within assert() 4591bb9b5f26Sdrh ** statements. 4592bb9b5f26Sdrh */ 4593bb9b5f26Sdrh #ifdef SQLITE_DEBUG 4594bb9b5f26Sdrh int sqlite3NoTempsInRange(Parse *pParse, int iFirst, int iLast){ 4595bb9b5f26Sdrh int i; 4596bb9b5f26Sdrh if( pParse->nRangeReg>0 4597bb9b5f26Sdrh && pParse->iRangeReg+pParse->nRangeReg<iLast 4598bb9b5f26Sdrh && pParse->iRangeReg>=iFirst 4599bb9b5f26Sdrh ){ 4600bb9b5f26Sdrh return 0; 4601bb9b5f26Sdrh } 4602bb9b5f26Sdrh for(i=0; i<pParse->nTempReg; i++){ 4603bb9b5f26Sdrh if( pParse->aTempReg[i]>=iFirst && pParse->aTempReg[i]<=iLast ){ 4604bb9b5f26Sdrh return 0; 4605bb9b5f26Sdrh } 4606bb9b5f26Sdrh } 4607bb9b5f26Sdrh return 1; 4608bb9b5f26Sdrh } 4609bb9b5f26Sdrh #endif /* SQLITE_DEBUG */ 4610